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.

215 lines
6.2KB

  1. /*
  2. * Interface to libmp3lame for mp3 encoding
  3. * Copyright (c) 2002 Lennert Buytenhek <buytenh@gnu.org>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * @file mp3lameaudio.c
  21. * Interface to libmp3lame for mp3 encoding.
  22. */
  23. #include "avcodec.h"
  24. #include "mpegaudio.h"
  25. #include <lame/lame.h>
  26. #define BUFFER_SIZE (2*MPA_FRAME_SIZE)
  27. typedef struct Mp3AudioContext {
  28. lame_global_flags *gfp;
  29. int stereo;
  30. uint8_t buffer[BUFFER_SIZE];
  31. int buffer_index;
  32. } Mp3AudioContext;
  33. static int MP3lame_encode_init(AVCodecContext *avctx)
  34. {
  35. Mp3AudioContext *s = avctx->priv_data;
  36. if (avctx->channels > 2)
  37. return -1;
  38. s->stereo = avctx->channels > 1 ? 1 : 0;
  39. if ((s->gfp = lame_init()) == NULL)
  40. goto err;
  41. lame_set_in_samplerate(s->gfp, avctx->sample_rate);
  42. lame_set_out_samplerate(s->gfp, avctx->sample_rate);
  43. lame_set_num_channels(s->gfp, avctx->channels);
  44. /* lame 3.91 dies on quality != 5 */
  45. lame_set_quality(s->gfp, 5);
  46. /* lame 3.91 doesn't work in mono */
  47. lame_set_mode(s->gfp, JOINT_STEREO);
  48. lame_set_brate(s->gfp, avctx->bit_rate/1000);
  49. lame_set_bWriteVbrTag(s->gfp,0);
  50. if (lame_init_params(s->gfp) < 0)
  51. goto err_close;
  52. avctx->frame_size = lame_get_framesize(s->gfp);
  53. avctx->coded_frame= avcodec_alloc_frame();
  54. avctx->coded_frame->key_frame= 1;
  55. return 0;
  56. err_close:
  57. lame_close(s->gfp);
  58. err:
  59. return -1;
  60. }
  61. static const int sSampleRates[3] = {
  62. 44100, 48000, 32000
  63. };
  64. static const int sBitRates[2][3][15] = {
  65. { { 0, 32, 64, 96,128,160,192,224,256,288,320,352,384,416,448},
  66. { 0, 32, 48, 56, 64, 80, 96,112,128,160,192,224,256,320,384},
  67. { 0, 32, 40, 48, 56, 64, 80, 96,112,128,160,192,224,256,320}
  68. },
  69. { { 0, 32, 48, 56, 64, 80, 96,112,128,144,160,176,192,224,256},
  70. { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160},
  71. { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160}
  72. },
  73. };
  74. static const int sSamplesPerFrame[2][3] =
  75. {
  76. { 384, 1152, 1152 },
  77. { 384, 1152, 576 }
  78. };
  79. static const int sBitsPerSlot[3] = {
  80. 32,
  81. 8,
  82. 8
  83. };
  84. static int mp3len(void *data, int *samplesPerFrame, int *sampleRate)
  85. {
  86. uint8_t *dataTmp = (uint8_t *)data;
  87. uint32_t header = ( (uint32_t)dataTmp[0] << 24 ) | ( (uint32_t)dataTmp[1] << 16 ) | ( (uint32_t)dataTmp[2] << 8 ) | (uint32_t)dataTmp[3];
  88. int layerID = 3 - ((header >> 17) & 0x03);
  89. int bitRateID = ((header >> 12) & 0x0f);
  90. int sampleRateID = ((header >> 10) & 0x03);
  91. int bitsPerSlot = sBitsPerSlot[layerID];
  92. int isPadded = ((header >> 9) & 0x01);
  93. static int const mode_tab[4]= {2,3,1,0};
  94. int mode= mode_tab[(header >> 19) & 0x03];
  95. int mpeg_id= mode>0;
  96. int temp0, temp1, bitRate;
  97. if ( (( header >> 21 ) & 0x7ff) != 0x7ff || mode == 3 || layerID==3 || sampleRateID==3) {
  98. return -1;
  99. }
  100. if(!samplesPerFrame) samplesPerFrame= &temp0;
  101. if(!sampleRate ) sampleRate = &temp1;
  102. // *isMono = ((header >> 6) & 0x03) == 0x03;
  103. *sampleRate = sSampleRates[sampleRateID]>>mode;
  104. bitRate = sBitRates[mpeg_id][layerID][bitRateID] * 1000;
  105. *samplesPerFrame = sSamplesPerFrame[mpeg_id][layerID];
  106. //av_log(NULL, AV_LOG_DEBUG, "sr:%d br:%d spf:%d l:%d m:%d\n", *sampleRate, bitRate, *samplesPerFrame, layerID, mode);
  107. return *samplesPerFrame * bitRate / (bitsPerSlot * *sampleRate) + isPadded;
  108. }
  109. int MP3lame_encode_frame(AVCodecContext *avctx,
  110. unsigned char *frame, int buf_size, void *data)
  111. {
  112. Mp3AudioContext *s = avctx->priv_data;
  113. int len;
  114. int lame_result;
  115. /* lame 3.91 dies on '1-channel interleaved' data */
  116. if(data){
  117. if (s->stereo) {
  118. lame_result = lame_encode_buffer_interleaved(
  119. s->gfp,
  120. data,
  121. avctx->frame_size,
  122. s->buffer + s->buffer_index,
  123. BUFFER_SIZE - s->buffer_index
  124. );
  125. } else {
  126. lame_result = lame_encode_buffer(
  127. s->gfp,
  128. data,
  129. data,
  130. avctx->frame_size,
  131. s->buffer + s->buffer_index,
  132. BUFFER_SIZE - s->buffer_index
  133. );
  134. }
  135. }else{
  136. lame_result= lame_encode_flush(
  137. s->gfp,
  138. s->buffer + s->buffer_index,
  139. BUFFER_SIZE - s->buffer_index
  140. );
  141. }
  142. if(lame_result==-1) {
  143. /* output buffer too small */
  144. 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);
  145. return 0;
  146. }
  147. s->buffer_index += lame_result;
  148. if(s->buffer_index<4)
  149. return 0;
  150. len= mp3len(s->buffer, NULL, NULL);
  151. //av_log(avctx, AV_LOG_DEBUG, "in:%d packet-len:%d index:%d\n", avctx->frame_size, len, s->buffer_index);
  152. if(len <= s->buffer_index){
  153. memcpy(frame, s->buffer, len);
  154. s->buffer_index -= len;
  155. memmove(s->buffer, s->buffer+len, s->buffer_index);
  156. //FIXME fix the audio codec API, so we dont need the memcpy()
  157. /*for(i=0; i<len; i++){
  158. av_log(avctx, AV_LOG_DEBUG, "%2X ", frame[i]);
  159. }*/
  160. return len;
  161. }else
  162. return 0;
  163. }
  164. int MP3lame_encode_close(AVCodecContext *avctx)
  165. {
  166. Mp3AudioContext *s = avctx->priv_data;
  167. av_freep(&avctx->coded_frame);
  168. lame_close(s->gfp);
  169. return 0;
  170. }
  171. AVCodec mp3lame_encoder = {
  172. "mp3",
  173. CODEC_TYPE_AUDIO,
  174. CODEC_ID_MP3,
  175. sizeof(Mp3AudioContext),
  176. MP3lame_encode_init,
  177. MP3lame_encode_frame,
  178. MP3lame_encode_close,
  179. .capabilities= CODEC_CAP_DELAY,
  180. };