Libav
|
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 "avcodec.h" 00028 #include "mpegaudio.h" 00029 #include <lame/lame.h> 00030 00031 #define BUFFER_SIZE (7200 + 2*MPA_FRAME_SIZE + MPA_FRAME_SIZE/4) 00032 typedef struct Mp3AudioContext { 00033 lame_global_flags *gfp; 00034 int stereo; 00035 uint8_t buffer[BUFFER_SIZE]; 00036 int buffer_index; 00037 } Mp3AudioContext; 00038 00039 static av_cold int MP3lame_encode_init(AVCodecContext *avctx) 00040 { 00041 Mp3AudioContext *s = avctx->priv_data; 00042 00043 if (avctx->channels > 2) 00044 return -1; 00045 00046 s->stereo = avctx->channels > 1 ? 1 : 0; 00047 00048 if ((s->gfp = lame_init()) == NULL) 00049 goto err; 00050 lame_set_in_samplerate(s->gfp, avctx->sample_rate); 00051 lame_set_out_samplerate(s->gfp, avctx->sample_rate); 00052 lame_set_num_channels(s->gfp, avctx->channels); 00053 if(avctx->compression_level == FF_COMPRESSION_DEFAULT) { 00054 lame_set_quality(s->gfp, 5); 00055 } else { 00056 lame_set_quality(s->gfp, avctx->compression_level); 00057 } 00058 /* lame 3.91 doesn't work in mono */ 00059 lame_set_mode(s->gfp, JOINT_STEREO); 00060 lame_set_brate(s->gfp, avctx->bit_rate/1000); 00061 if(avctx->flags & CODEC_FLAG_QSCALE) { 00062 lame_set_brate(s->gfp, 0); 00063 lame_set_VBR(s->gfp, vbr_default); 00064 lame_set_VBR_q(s->gfp, avctx->global_quality / (float)FF_QP2LAMBDA); 00065 } 00066 lame_set_bWriteVbrTag(s->gfp,0); 00067 lame_set_disable_reservoir(s->gfp, avctx->flags2 & CODEC_FLAG2_BIT_RESERVOIR ? 0 : 1); 00068 if (lame_init_params(s->gfp) < 0) 00069 goto err_close; 00070 00071 avctx->frame_size = lame_get_framesize(s->gfp); 00072 00073 avctx->coded_frame= avcodec_alloc_frame(); 00074 avctx->coded_frame->key_frame= 1; 00075 00076 return 0; 00077 00078 err_close: 00079 lame_close(s->gfp); 00080 err: 00081 return -1; 00082 } 00083 00084 static const int sSampleRates[] = { 00085 44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000, 0 00086 }; 00087 00088 static const int sBitRates[2][3][15] = { 00089 { { 0, 32, 64, 96,128,160,192,224,256,288,320,352,384,416,448}, 00090 { 0, 32, 48, 56, 64, 80, 96,112,128,160,192,224,256,320,384}, 00091 { 0, 32, 40, 48, 56, 64, 80, 96,112,128,160,192,224,256,320} 00092 }, 00093 { { 0, 32, 48, 56, 64, 80, 96,112,128,144,160,176,192,224,256}, 00094 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160}, 00095 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160} 00096 }, 00097 }; 00098 00099 static const int sSamplesPerFrame[2][3] = 00100 { 00101 { 384, 1152, 1152 }, 00102 { 384, 1152, 576 } 00103 }; 00104 00105 static const int sBitsPerSlot[3] = { 00106 32, 00107 8, 00108 8 00109 }; 00110 00111 static int mp3len(void *data, int *samplesPerFrame, int *sampleRate) 00112 { 00113 uint32_t header = AV_RB32(data); 00114 int layerID = 3 - ((header >> 17) & 0x03); 00115 int bitRateID = ((header >> 12) & 0x0f); 00116 int sampleRateID = ((header >> 10) & 0x03); 00117 int bitsPerSlot = sBitsPerSlot[layerID]; 00118 int isPadded = ((header >> 9) & 0x01); 00119 static int const mode_tab[4]= {2,3,1,0}; 00120 int mode= mode_tab[(header >> 19) & 0x03]; 00121 int mpeg_id= mode>0; 00122 int temp0, temp1, bitRate; 00123 00124 if ( (( header >> 21 ) & 0x7ff) != 0x7ff || mode == 3 || layerID==3 || sampleRateID==3) { 00125 return -1; 00126 } 00127 00128 if(!samplesPerFrame) samplesPerFrame= &temp0; 00129 if(!sampleRate ) sampleRate = &temp1; 00130 00131 // *isMono = ((header >> 6) & 0x03) == 0x03; 00132 00133 *sampleRate = sSampleRates[sampleRateID]>>mode; 00134 bitRate = sBitRates[mpeg_id][layerID][bitRateID] * 1000; 00135 *samplesPerFrame = sSamplesPerFrame[mpeg_id][layerID]; 00136 //av_log(NULL, AV_LOG_DEBUG, "sr:%d br:%d spf:%d l:%d m:%d\n", *sampleRate, bitRate, *samplesPerFrame, layerID, mode); 00137 00138 return *samplesPerFrame * bitRate / (bitsPerSlot * *sampleRate) + isPadded; 00139 } 00140 00141 static int MP3lame_encode_frame(AVCodecContext *avctx, 00142 unsigned char *frame, int buf_size, void *data) 00143 { 00144 Mp3AudioContext *s = avctx->priv_data; 00145 int len; 00146 int lame_result; 00147 00148 /* lame 3.91 dies on '1-channel interleaved' data */ 00149 00150 if(data){ 00151 if (s->stereo) { 00152 lame_result = lame_encode_buffer_interleaved( 00153 s->gfp, 00154 data, 00155 avctx->frame_size, 00156 s->buffer + s->buffer_index, 00157 BUFFER_SIZE - s->buffer_index 00158 ); 00159 } else { 00160 lame_result = lame_encode_buffer( 00161 s->gfp, 00162 data, 00163 data, 00164 avctx->frame_size, 00165 s->buffer + s->buffer_index, 00166 BUFFER_SIZE - s->buffer_index 00167 ); 00168 } 00169 }else{ 00170 lame_result= lame_encode_flush( 00171 s->gfp, 00172 s->buffer + s->buffer_index, 00173 BUFFER_SIZE - s->buffer_index 00174 ); 00175 } 00176 00177 if(lame_result < 0){ 00178 if(lame_result==-1) { 00179 /* output buffer too small */ 00180 av_log(avctx, AV_LOG_ERROR, "lame: output buffer too small (buffer index: %d, free bytes: %d)\n", s->buffer_index, BUFFER_SIZE - s->buffer_index); 00181 } 00182 return -1; 00183 } 00184 00185 s->buffer_index += lame_result; 00186 00187 if(s->buffer_index<4) 00188 return 0; 00189 00190 len= mp3len(s->buffer, NULL, NULL); 00191 //av_log(avctx, AV_LOG_DEBUG, "in:%d packet-len:%d index:%d\n", avctx->frame_size, len, s->buffer_index); 00192 if(len <= s->buffer_index){ 00193 memcpy(frame, s->buffer, len); 00194 s->buffer_index -= len; 00195 00196 memmove(s->buffer, s->buffer+len, s->buffer_index); 00197 //FIXME fix the audio codec API, so we do not need the memcpy() 00198 /*for(i=0; i<len; i++){ 00199 av_log(avctx, AV_LOG_DEBUG, "%2X ", frame[i]); 00200 }*/ 00201 return len; 00202 }else 00203 return 0; 00204 } 00205 00206 static av_cold int MP3lame_encode_close(AVCodecContext *avctx) 00207 { 00208 Mp3AudioContext *s = avctx->priv_data; 00209 00210 av_freep(&avctx->coded_frame); 00211 00212 lame_close(s->gfp); 00213 return 0; 00214 } 00215 00216 00217 AVCodec libmp3lame_encoder = { 00218 "libmp3lame", 00219 AVMEDIA_TYPE_AUDIO, 00220 CODEC_ID_MP3, 00221 sizeof(Mp3AudioContext), 00222 MP3lame_encode_init, 00223 MP3lame_encode_frame, 00224 MP3lame_encode_close, 00225 .capabilities= CODEC_CAP_DELAY, 00226 .sample_fmts = (const enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE}, 00227 .supported_samplerates= sSampleRates, 00228 .long_name= NULL_IF_CONFIG_SMALL("libmp3lame MP3 (MPEG audio layer 3)"), 00229 };