You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

287 lines
8.4KB

  1. /*
  2. * Interface to libmp3lame for mp3 encoding
  3. * Copyright (c) 2002 Lennert Buytenhek <buytenh@gnu.org>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Interface to libmp3lame for mp3 encoding.
  24. */
  25. #include "avcodec.h"
  26. #include "mpegaudio.h"
  27. #include <lame/lame.h>
  28. #define BUFFER_SIZE (7200 + 2*MPA_FRAME_SIZE + MPA_FRAME_SIZE/4)
  29. typedef struct Mp3AudioContext {
  30. lame_global_flags *gfp;
  31. int stereo;
  32. uint8_t buffer[BUFFER_SIZE];
  33. int buffer_index;
  34. struct {
  35. int *left;
  36. int *right;
  37. } s32_data;
  38. } Mp3AudioContext;
  39. static av_cold int MP3lame_encode_init(AVCodecContext *avctx)
  40. {
  41. Mp3AudioContext *s = avctx->priv_data;
  42. if (avctx->channels > 2)
  43. return -1;
  44. s->stereo = avctx->channels > 1 ? 1 : 0;
  45. if ((s->gfp = lame_init()) == NULL)
  46. goto err;
  47. lame_set_in_samplerate(s->gfp, avctx->sample_rate);
  48. lame_set_out_samplerate(s->gfp, avctx->sample_rate);
  49. lame_set_num_channels(s->gfp, avctx->channels);
  50. if(avctx->compression_level == FF_COMPRESSION_DEFAULT) {
  51. lame_set_quality(s->gfp, 5);
  52. } else {
  53. lame_set_quality(s->gfp, avctx->compression_level);
  54. }
  55. lame_set_mode(s->gfp, s->stereo ? JOINT_STEREO : MONO);
  56. lame_set_brate(s->gfp, avctx->bit_rate/1000);
  57. if(avctx->flags & CODEC_FLAG_QSCALE) {
  58. lame_set_brate(s->gfp, 0);
  59. lame_set_VBR(s->gfp, vbr_default);
  60. lame_set_VBR_quality(s->gfp, avctx->global_quality/(float)FF_QP2LAMBDA);
  61. }
  62. lame_set_bWriteVbrTag(s->gfp,0);
  63. lame_set_disable_reservoir(s->gfp, avctx->flags2 & CODEC_FLAG2_BIT_RESERVOIR ? 0 : 1);
  64. if (lame_init_params(s->gfp) < 0)
  65. goto err_close;
  66. avctx->frame_size = lame_get_framesize(s->gfp);
  67. if(!(avctx->coded_frame= avcodec_alloc_frame())) {
  68. lame_close(s->gfp);
  69. return AVERROR(ENOMEM);
  70. }
  71. avctx->coded_frame->key_frame= 1;
  72. if(AV_SAMPLE_FMT_S32 == avctx->sample_fmt && s->stereo) {
  73. int nelem = 2 * avctx->frame_size;
  74. if(! (s->s32_data.left = av_malloc(nelem * sizeof(int)))) {
  75. av_freep(&avctx->coded_frame);
  76. lame_close(s->gfp);
  77. return AVERROR(ENOMEM);
  78. }
  79. s->s32_data.right = s->s32_data.left + avctx->frame_size;
  80. }
  81. return 0;
  82. err_close:
  83. lame_close(s->gfp);
  84. err:
  85. return -1;
  86. }
  87. static const int sSampleRates[] = {
  88. 44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000, 0
  89. };
  90. static const int sBitRates[2][3][15] = {
  91. { { 0, 32, 64, 96,128,160,192,224,256,288,320,352,384,416,448},
  92. { 0, 32, 48, 56, 64, 80, 96,112,128,160,192,224,256,320,384},
  93. { 0, 32, 40, 48, 56, 64, 80, 96,112,128,160,192,224,256,320}
  94. },
  95. { { 0, 32, 48, 56, 64, 80, 96,112,128,144,160,176,192,224,256},
  96. { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160},
  97. { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160}
  98. },
  99. };
  100. static const int sSamplesPerFrame[2][3] =
  101. {
  102. { 384, 1152, 1152 },
  103. { 384, 1152, 576 }
  104. };
  105. static const int sBitsPerSlot[3] = {
  106. 32,
  107. 8,
  108. 8
  109. };
  110. static int mp3len(void *data, int *samplesPerFrame, int *sampleRate)
  111. {
  112. uint32_t header = AV_RB32(data);
  113. int layerID = 3 - ((header >> 17) & 0x03);
  114. int bitRateID = ((header >> 12) & 0x0f);
  115. int sampleRateID = ((header >> 10) & 0x03);
  116. int bitsPerSlot = sBitsPerSlot[layerID];
  117. int isPadded = ((header >> 9) & 0x01);
  118. static int const mode_tab[4]= {2,3,1,0};
  119. int mode= mode_tab[(header >> 19) & 0x03];
  120. int mpeg_id= mode>0;
  121. int temp0, temp1, bitRate;
  122. if ( (( header >> 21 ) & 0x7ff) != 0x7ff || mode == 3 || layerID==3 || sampleRateID==3) {
  123. return -1;
  124. }
  125. if(!samplesPerFrame) samplesPerFrame= &temp0;
  126. if(!sampleRate ) sampleRate = &temp1;
  127. // *isMono = ((header >> 6) & 0x03) == 0x03;
  128. *sampleRate = sSampleRates[sampleRateID]>>mode;
  129. bitRate = sBitRates[mpeg_id][layerID][bitRateID] * 1000;
  130. *samplesPerFrame = sSamplesPerFrame[mpeg_id][layerID];
  131. //av_log(NULL, AV_LOG_DEBUG, "sr:%d br:%d spf:%d l:%d m:%d\n", *sampleRate, bitRate, *samplesPerFrame, layerID, mode);
  132. return *samplesPerFrame * bitRate / (bitsPerSlot * *sampleRate) + isPadded;
  133. }
  134. static int MP3lame_encode_frame(AVCodecContext *avctx,
  135. unsigned char *frame, int buf_size, void *data)
  136. {
  137. Mp3AudioContext *s = avctx->priv_data;
  138. int len;
  139. int lame_result;
  140. /* lame 3.91 dies on '1-channel interleaved' data */
  141. if(!data){
  142. lame_result= lame_encode_flush(
  143. s->gfp,
  144. s->buffer + s->buffer_index,
  145. BUFFER_SIZE - s->buffer_index
  146. );
  147. #if 2147483647 == INT_MAX
  148. }else if(AV_SAMPLE_FMT_S32 == avctx->sample_fmt){
  149. if (s->stereo) {
  150. int32_t *rp = data;
  151. int32_t *mp = rp + 2*avctx->frame_size;
  152. int *wpl = s->s32_data.left;
  153. int *wpr = s->s32_data.right;
  154. while (rp < mp) {
  155. *wpl++ = *rp++;
  156. *wpr++ = *rp++;
  157. }
  158. lame_result = lame_encode_buffer_int(
  159. s->gfp,
  160. s->s32_data.left,
  161. s->s32_data.right,
  162. avctx->frame_size,
  163. s->buffer + s->buffer_index,
  164. BUFFER_SIZE - s->buffer_index
  165. );
  166. } else {
  167. lame_result = lame_encode_buffer_int(
  168. s->gfp,
  169. data,
  170. data,
  171. avctx->frame_size,
  172. s->buffer + s->buffer_index,
  173. BUFFER_SIZE - s->buffer_index
  174. );
  175. }
  176. #endif
  177. }else{
  178. if (s->stereo) {
  179. lame_result = lame_encode_buffer_interleaved(
  180. s->gfp,
  181. data,
  182. avctx->frame_size,
  183. s->buffer + s->buffer_index,
  184. BUFFER_SIZE - s->buffer_index
  185. );
  186. } else {
  187. lame_result = lame_encode_buffer(
  188. s->gfp,
  189. data,
  190. data,
  191. avctx->frame_size,
  192. s->buffer + s->buffer_index,
  193. BUFFER_SIZE - s->buffer_index
  194. );
  195. }
  196. }
  197. if(lame_result < 0){
  198. if(lame_result==-1) {
  199. /* output buffer too small */
  200. 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);
  201. }
  202. return -1;
  203. }
  204. s->buffer_index += lame_result;
  205. if(s->buffer_index<4)
  206. return 0;
  207. len= mp3len(s->buffer, NULL, NULL);
  208. //av_log(avctx, AV_LOG_DEBUG, "in:%d packet-len:%d index:%d\n", avctx->frame_size, len, s->buffer_index);
  209. if(len <= s->buffer_index){
  210. memcpy(frame, s->buffer, len);
  211. s->buffer_index -= len;
  212. memmove(s->buffer, s->buffer+len, s->buffer_index);
  213. //FIXME fix the audio codec API, so we do not need the memcpy()
  214. /*for(i=0; i<len; i++){
  215. av_log(avctx, AV_LOG_DEBUG, "%2X ", frame[i]);
  216. }*/
  217. return len;
  218. }else
  219. return 0;
  220. }
  221. static av_cold int MP3lame_encode_close(AVCodecContext *avctx)
  222. {
  223. Mp3AudioContext *s = avctx->priv_data;
  224. av_freep(&s->s32_data.left);
  225. av_freep(&avctx->coded_frame);
  226. lame_close(s->gfp);
  227. return 0;
  228. }
  229. AVCodec ff_libmp3lame_encoder = {
  230. "libmp3lame",
  231. AVMEDIA_TYPE_AUDIO,
  232. CODEC_ID_MP3,
  233. sizeof(Mp3AudioContext),
  234. MP3lame_encode_init,
  235. MP3lame_encode_frame,
  236. MP3lame_encode_close,
  237. .capabilities= CODEC_CAP_DELAY,
  238. .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,
  239. #if 2147483647 == INT_MAX
  240. AV_SAMPLE_FMT_S32,
  241. #endif
  242. AV_SAMPLE_FMT_NONE},
  243. .supported_samplerates= sSampleRates,
  244. .long_name= NULL_IF_CONFIG_SMALL("libmp3lame MP3 (MPEG audio layer 3)"),
  245. };