• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/libmp3lame.c

Go to the documentation of this file.
00001 /*
00002  * Interface to libmp3lame for mp3 encoding
00003  * Copyright (c) 2002 Lennert Buytenhek <buytenh@gnu.org>
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00027 #include "libavutil/intreadwrite.h"
00028 #include "libavutil/log.h"
00029 #include "libavutil/opt.h"
00030 #include "avcodec.h"
00031 #include "mpegaudio.h"
00032 #include <lame/lame.h>
00033 
00034 #define BUFFER_SIZE (7200 + 2 * MPA_FRAME_SIZE + MPA_FRAME_SIZE / 4+1000) // FIXME: Buffer size to small? Adding 1000 to make up for it.
00035 typedef struct Mp3AudioContext {
00036     AVClass *class;
00037     lame_global_flags *gfp;
00038     int stereo;
00039     uint8_t buffer[BUFFER_SIZE];
00040     int buffer_index;
00041     struct {
00042         int *left;
00043         int *right;
00044     } s32_data;
00045     int reservoir;
00046 } Mp3AudioContext;
00047 
00048 static av_cold int MP3lame_encode_init(AVCodecContext *avctx)
00049 {
00050     Mp3AudioContext *s = avctx->priv_data;
00051 
00052     if (avctx->channels > 2) {
00053         av_log(avctx, AV_LOG_ERROR,
00054                "Invalid number of channels %d, must be <= 2\n", avctx->channels);
00055         return AVERROR(EINVAL);
00056     }
00057 
00058     s->stereo = avctx->channels > 1 ? 1 : 0;
00059 
00060     if ((s->gfp = lame_init()) == NULL)
00061         goto err;
00062     lame_set_in_samplerate(s->gfp, avctx->sample_rate);
00063     lame_set_out_samplerate(s->gfp, avctx->sample_rate);
00064     lame_set_num_channels(s->gfp, avctx->channels);
00065     if (avctx->compression_level == FF_COMPRESSION_DEFAULT) {
00066         lame_set_quality(s->gfp, 5);
00067     } else {
00068         lame_set_quality(s->gfp, avctx->compression_level);
00069     }
00070     lame_set_mode(s->gfp, s->stereo ? JOINT_STEREO : MONO);
00071     lame_set_brate(s->gfp, avctx->bit_rate / 1000);
00072     if (avctx->flags & CODEC_FLAG_QSCALE) {
00073         lame_set_brate(s->gfp, 0);
00074         lame_set_VBR(s->gfp, vbr_default);
00075         lame_set_VBR_quality(s->gfp, avctx->global_quality / (float)FF_QP2LAMBDA);
00076     }
00077     lame_set_bWriteVbrTag(s->gfp,0);
00078 #if FF_API_LAME_GLOBAL_OPTS
00079     s->reservoir = avctx->flags2 & CODEC_FLAG2_BIT_RESERVOIR;
00080 #endif
00081     lame_set_disable_reservoir(s->gfp, !s->reservoir);
00082     if (lame_init_params(s->gfp) < 0)
00083         goto err_close;
00084 
00085     avctx->frame_size             = lame_get_framesize(s->gfp);
00086 
00087     if(!(avctx->coded_frame= avcodec_alloc_frame())) {
00088         lame_close(s->gfp);
00089 
00090         return AVERROR(ENOMEM);
00091     }
00092     avctx->coded_frame->key_frame = 1;
00093 
00094     if(AV_SAMPLE_FMT_S32 == avctx->sample_fmt && s->stereo) {
00095         int nelem = 2 * avctx->frame_size;
00096 
00097         if(! (s->s32_data.left = av_malloc(nelem * sizeof(int)))) {
00098             av_freep(&avctx->coded_frame);
00099             lame_close(s->gfp);
00100 
00101             return AVERROR(ENOMEM);
00102         }
00103 
00104         s->s32_data.right = s->s32_data.left + avctx->frame_size;
00105     }
00106 
00107     return 0;
00108 
00109 err_close:
00110     lame_close(s->gfp);
00111 err:
00112     return -1;
00113 }
00114 
00115 static const int sSampleRates[] = {
00116     44100, 48000,  32000, 22050, 24000, 16000, 11025, 12000, 8000, 0
00117 };
00118 
00119 static const int sBitRates[2][3][15] = {
00120     {
00121         { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448 },
00122         { 0, 32, 48, 56, 64,  80,  96,  112, 128, 160, 192, 224, 256, 320, 384 },
00123         { 0, 32, 40, 48, 56,  64,  80,  96,  112, 128, 160, 192, 224, 256, 320 }
00124     },
00125     {
00126         { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256 },
00127         { 0,  8, 16, 24, 32, 40, 48,  56,  64,  80,  96, 112, 128, 144, 160 },
00128         { 0,  8, 16, 24, 32, 40, 48,  56,  64,  80,  96, 112, 128, 144, 160 }
00129     },
00130 };
00131 
00132 static const int sSamplesPerFrame[2][3] = {
00133     { 384, 1152, 1152 },
00134     { 384, 1152,  576 }
00135 };
00136 
00137 static const int sBitsPerSlot[3] = { 32, 8, 8 };
00138 
00139 static int mp3len(void *data, int *samplesPerFrame, int *sampleRate)
00140 {
00141     uint32_t header  = AV_RB32(data);
00142     int layerID      = 3 - ((header >> 17) & 0x03);
00143     int bitRateID    = ((header >> 12) & 0x0f);
00144     int sampleRateID = ((header >> 10) & 0x03);
00145     int bitsPerSlot  = sBitsPerSlot[layerID];
00146     int isPadded     = ((header >> 9) & 0x01);
00147     static int const mode_tab[4] = { 2, 3, 1, 0 };
00148     int mode    = mode_tab[(header >> 19) & 0x03];
00149     int mpeg_id = mode > 0;
00150     int temp0, temp1, bitRate;
00151 
00152     if (((header >> 21) & 0x7ff) != 0x7ff || mode == 3 || layerID == 3 ||
00153         sampleRateID == 3) {
00154         return -1;
00155     }
00156 
00157     if (!samplesPerFrame)
00158         samplesPerFrame = &temp0;
00159     if (!sampleRate)
00160         sampleRate      = &temp1;
00161 
00162     //*isMono = ((header >>  6) & 0x03) == 0x03;
00163 
00164     *sampleRate      = sSampleRates[sampleRateID] >> mode;
00165     bitRate          = sBitRates[mpeg_id][layerID][bitRateID] * 1000;
00166     *samplesPerFrame = sSamplesPerFrame[mpeg_id][layerID];
00167     //av_log(NULL, AV_LOG_DEBUG,
00168     //       "sr:%d br:%d spf:%d l:%d m:%d\n",
00169     //       *sampleRate, bitRate, *samplesPerFrame, layerID, mode);
00170 
00171     return *samplesPerFrame * bitRate / (bitsPerSlot * *sampleRate) + isPadded;
00172 }
00173 
00174 static int MP3lame_encode_frame(AVCodecContext *avctx, unsigned char *frame,
00175                                 int buf_size, void *data)
00176 {
00177     Mp3AudioContext *s = avctx->priv_data;
00178     int len;
00179     int lame_result;
00180 
00181     /* lame 3.91 dies on '1-channel interleaved' data */
00182 
00183     if (!data){
00184         lame_result= lame_encode_flush(
00185                 s->gfp,
00186                 s->buffer + s->buffer_index,
00187                 BUFFER_SIZE - s->buffer_index
00188                 );
00189 #if 2147483647 == INT_MAX
00190     }else if(AV_SAMPLE_FMT_S32 == avctx->sample_fmt){
00191         if (s->stereo) {
00192             int32_t *rp = data;
00193             int32_t *mp = rp + 2*avctx->frame_size;
00194             int *wpl = s->s32_data.left;
00195             int *wpr = s->s32_data.right;
00196 
00197             while (rp < mp) {
00198                 *wpl++ = *rp++;
00199                 *wpr++ = *rp++;
00200             }
00201 
00202             lame_result = lame_encode_buffer_int(
00203                 s->gfp,
00204                 s->s32_data.left,
00205                 s->s32_data.right,
00206                 avctx->frame_size,
00207                 s->buffer + s->buffer_index,
00208                 BUFFER_SIZE - s->buffer_index
00209                 );
00210         } else {
00211             lame_result = lame_encode_buffer_int(
00212                 s->gfp,
00213                 data,
00214                 data,
00215                 avctx->frame_size,
00216                 s->buffer + s->buffer_index,
00217                 BUFFER_SIZE - s->buffer_index
00218                 );
00219         }
00220 #endif
00221     }else{
00222         if (s->stereo) {
00223             lame_result = lame_encode_buffer_interleaved(
00224                 s->gfp,
00225                 data,
00226                 avctx->frame_size,
00227                 s->buffer + s->buffer_index,
00228                 BUFFER_SIZE - s->buffer_index
00229                 );
00230         } else {
00231             lame_result = lame_encode_buffer(
00232                 s->gfp,
00233                 data,
00234                 data,
00235                 avctx->frame_size,
00236                 s->buffer + s->buffer_index,
00237                 BUFFER_SIZE - s->buffer_index
00238                 );
00239         }
00240     }
00241 
00242     if (lame_result < 0) {
00243         if (lame_result == -1) {
00244             /* output buffer too small */
00245             av_log(avctx, AV_LOG_ERROR,
00246                    "lame: output buffer too small (buffer index: %d, free bytes: %d)\n",
00247                    s->buffer_index, BUFFER_SIZE - s->buffer_index);
00248         }
00249         return -1;
00250     }
00251 
00252     s->buffer_index += lame_result;
00253 
00254     if (s->buffer_index < 4)
00255         return 0;
00256 
00257     len = mp3len(s->buffer, NULL, NULL);
00258     //av_log(avctx, AV_LOG_DEBUG, "in:%d packet-len:%d index:%d\n",
00259     //       avctx->frame_size, len, s->buffer_index);
00260     if (len <= s->buffer_index) {
00261         memcpy(frame, s->buffer, len);
00262         s->buffer_index -= len;
00263 
00264         memmove(s->buffer, s->buffer + len, s->buffer_index);
00265         // FIXME fix the audio codec API, so we do not need the memcpy()
00266         /*for(i=0; i<len; i++) {
00267             av_log(avctx, AV_LOG_DEBUG, "%2X ", frame[i]);
00268         }*/
00269         return len;
00270     } else
00271         return 0;
00272 }
00273 
00274 static av_cold int MP3lame_encode_close(AVCodecContext *avctx)
00275 {
00276     Mp3AudioContext *s = avctx->priv_data;
00277 
00278     av_freep(&s->s32_data.left);
00279     av_freep(&avctx->coded_frame);
00280 
00281     lame_close(s->gfp);
00282     return 0;
00283 }
00284 
00285 #define OFFSET(x) offsetof(Mp3AudioContext, x)
00286 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
00287 static const AVOption options[] = {
00288     { "reservoir", "Use bit reservoir.", OFFSET(reservoir), AV_OPT_TYPE_INT, { 1 }, 0, 1, AE },
00289     { NULL },
00290 };
00291 
00292 static const AVClass libmp3lame_class = {
00293     .class_name = "libmp3lame encoder",
00294     .item_name  = av_default_item_name,
00295     .option     = options,
00296     .version    = LIBAVUTIL_VERSION_INT,
00297 };
00298 
00299 AVCodec ff_libmp3lame_encoder = {
00300     .name                  = "libmp3lame",
00301     .type                  = AVMEDIA_TYPE_AUDIO,
00302     .id                    = CODEC_ID_MP3,
00303     .priv_data_size        = sizeof(Mp3AudioContext),
00304     .init                  = MP3lame_encode_init,
00305     .encode                = MP3lame_encode_frame,
00306     .close                 = MP3lame_encode_close,
00307     .capabilities          = CODEC_CAP_DELAY,
00308     .sample_fmts           = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16,
00309 #if 2147483647 == INT_MAX
00310     AV_SAMPLE_FMT_S32,
00311 #endif
00312                                                              AV_SAMPLE_FMT_NONE },
00313     .supported_samplerates = sSampleRates,
00314     .long_name             = NULL_IF_CONFIG_SMALL("libmp3lame MP3 (MPEG audio layer 3)"),
00315     .priv_class            = &libmp3lame_class,
00316 };
Generated on Fri Feb 1 2013 14:34:37 for FFmpeg by doxygen 1.7.1