libmp3lame.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 <lame/lame.h>
  26. #include "libavutil/channel_layout.h"
  27. #include "libavutil/common.h"
  28. #include "libavutil/float_dsp.h"
  29. #include "libavutil/intreadwrite.h"
  30. #include "libavutil/log.h"
  31. #include "libavutil/opt.h"
  32. #include "avcodec.h"
  33. #include "audio_frame_queue.h"
  34. #include "internal.h"
  35. #include "mpegaudio.h"
  36. #include "mpegaudiodecheader.h"
  37. #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.
  38. typedef struct LAMEContext {
  39. AVClass *class;
  40. AVCodecContext *avctx;
  41. lame_global_flags *gfp;
  42. uint8_t *buffer;
  43. int buffer_index;
  44. int buffer_size;
  45. int reservoir;
  46. float *samples_flt[2];
  47. AudioFrameQueue afq;
  48. AVFloatDSPContext fdsp;
  49. } LAMEContext;
  50. static int realloc_buffer(LAMEContext *s)
  51. {
  52. if (!s->buffer || s->buffer_size - s->buffer_index < BUFFER_SIZE) {
  53. uint8_t *tmp;
  54. int new_size = s->buffer_index + 2 * BUFFER_SIZE;
  55. av_dlog(s->avctx, "resizing output buffer: %d -> %d\n", s->buffer_size,
  56. new_size);
  57. tmp = av_realloc(s->buffer, new_size);
  58. if (!tmp) {
  59. av_freep(&s->buffer);
  60. s->buffer_size = s->buffer_index = 0;
  61. return AVERROR(ENOMEM);
  62. }
  63. s->buffer = tmp;
  64. s->buffer_size = new_size;
  65. }
  66. return 0;
  67. }
  68. static av_cold int mp3lame_encode_close(AVCodecContext *avctx)
  69. {
  70. LAMEContext *s = avctx->priv_data;
  71. #if FF_API_OLD_ENCODE_AUDIO
  72. av_freep(&avctx->coded_frame);
  73. #endif
  74. av_freep(&s->samples_flt[0]);
  75. av_freep(&s->samples_flt[1]);
  76. av_freep(&s->buffer);
  77. ff_af_queue_close(&s->afq);
  78. lame_close(s->gfp);
  79. return 0;
  80. }
  81. static av_cold int mp3lame_encode_init(AVCodecContext *avctx)
  82. {
  83. LAMEContext *s = avctx->priv_data;
  84. int ret;
  85. s->avctx = avctx;
  86. /* initialize LAME and get defaults */
  87. if ((s->gfp = lame_init()) == NULL)
  88. return AVERROR(ENOMEM);
  89. lame_set_num_channels(s->gfp, avctx->channels);
  90. lame_set_mode(s->gfp, avctx->channels > 1 ? JOINT_STEREO : MONO);
  91. /* sample rate */
  92. lame_set_in_samplerate (s->gfp, avctx->sample_rate);
  93. lame_set_out_samplerate(s->gfp, avctx->sample_rate);
  94. /* algorithmic quality */
  95. if (avctx->compression_level == FF_COMPRESSION_DEFAULT)
  96. lame_set_quality(s->gfp, 5);
  97. else
  98. lame_set_quality(s->gfp, avctx->compression_level);
  99. /* rate control */
  100. if (avctx->flags & CODEC_FLAG_QSCALE) {
  101. lame_set_VBR(s->gfp, vbr_default);
  102. lame_set_VBR_quality(s->gfp, avctx->global_quality / (float)FF_QP2LAMBDA);
  103. } else {
  104. if (avctx->bit_rate)
  105. lame_set_brate(s->gfp, avctx->bit_rate / 1000);
  106. }
  107. /* do not get a Xing VBR header frame from LAME */
  108. lame_set_bWriteVbrTag(s->gfp,0);
  109. /* bit reservoir usage */
  110. lame_set_disable_reservoir(s->gfp, !s->reservoir);
  111. /* set specified parameters */
  112. if (lame_init_params(s->gfp) < 0) {
  113. ret = -1;
  114. goto error;
  115. }
  116. /* get encoder delay */
  117. avctx->delay = lame_get_encoder_delay(s->gfp) + 528 + 1;
  118. ff_af_queue_init(avctx, &s->afq);
  119. avctx->frame_size = lame_get_framesize(s->gfp);
  120. #if FF_API_OLD_ENCODE_AUDIO
  121. avctx->coded_frame = avcodec_alloc_frame();
  122. if (!avctx->coded_frame) {
  123. ret = AVERROR(ENOMEM);
  124. goto error;
  125. }
  126. #endif
  127. /* allocate float sample buffers */
  128. if (avctx->sample_fmt == AV_SAMPLE_FMT_FLTP) {
  129. int ch;
  130. for (ch = 0; ch < avctx->channels; ch++) {
  131. s->samples_flt[ch] = av_malloc(avctx->frame_size *
  132. sizeof(*s->samples_flt[ch]));
  133. if (!s->samples_flt[ch]) {
  134. ret = AVERROR(ENOMEM);
  135. goto error;
  136. }
  137. }
  138. }
  139. ret = realloc_buffer(s);
  140. if (ret < 0)
  141. goto error;
  142. avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
  143. return 0;
  144. error:
  145. mp3lame_encode_close(avctx);
  146. return ret;
  147. }
  148. #define ENCODE_BUFFER(func, buf_type, buf_name) do { \
  149. lame_result = func(s->gfp, \
  150. (const buf_type *)buf_name[0], \
  151. (const buf_type *)buf_name[1], frame->nb_samples, \
  152. s->buffer + s->buffer_index, \
  153. s->buffer_size - s->buffer_index); \
  154. } while (0)
  155. static int mp3lame_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  156. const AVFrame *frame, int *got_packet_ptr)
  157. {
  158. LAMEContext *s = avctx->priv_data;
  159. MPADecodeHeader hdr;
  160. int len, ret, ch;
  161. int lame_result;
  162. if (frame) {
  163. switch (avctx->sample_fmt) {
  164. case AV_SAMPLE_FMT_S16P:
  165. ENCODE_BUFFER(lame_encode_buffer, int16_t, frame->data);
  166. break;
  167. case AV_SAMPLE_FMT_S32P:
  168. ENCODE_BUFFER(lame_encode_buffer_int, int32_t, frame->data);
  169. break;
  170. case AV_SAMPLE_FMT_FLTP:
  171. if (frame->linesize[0] < 4 * FFALIGN(frame->nb_samples, 8)) {
  172. av_log(avctx, AV_LOG_ERROR, "inadequate AVFrame plane padding\n");
  173. return AVERROR(EINVAL);
  174. }
  175. for (ch = 0; ch < avctx->channels; ch++) {
  176. s->fdsp.vector_fmul_scalar(s->samples_flt[ch],
  177. (const float *)frame->data[ch],
  178. 32768.0f,
  179. FFALIGN(frame->nb_samples, 8));
  180. }
  181. ENCODE_BUFFER(lame_encode_buffer_float, float, s->samples_flt);
  182. break;
  183. default:
  184. return AVERROR_BUG;
  185. }
  186. } else {
  187. lame_result = lame_encode_flush(s->gfp, s->buffer + s->buffer_index,
  188. s->buffer_size - s->buffer_index);
  189. }
  190. if (lame_result < 0) {
  191. if (lame_result == -1) {
  192. av_log(avctx, AV_LOG_ERROR,
  193. "lame: output buffer too small (buffer index: %d, free bytes: %d)\n",
  194. s->buffer_index, s->buffer_size - s->buffer_index);
  195. }
  196. return -1;
  197. }
  198. s->buffer_index += lame_result;
  199. ret = realloc_buffer(s);
  200. if (ret < 0) {
  201. av_log(avctx, AV_LOG_ERROR, "error reallocating output buffer\n");
  202. return ret;
  203. }
  204. /* add current frame to the queue */
  205. if (frame) {
  206. if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
  207. return ret;
  208. }
  209. /* Move 1 frame from the LAME buffer to the output packet, if available.
  210. We have to parse the first frame header in the output buffer to
  211. determine the frame size. */
  212. if (s->buffer_index < 4)
  213. return 0;
  214. if (avpriv_mpegaudio_decode_header(&hdr, AV_RB32(s->buffer))) {
  215. av_log(avctx, AV_LOG_ERROR, "free format output not supported\n");
  216. return -1;
  217. }
  218. len = hdr.frame_size;
  219. av_dlog(avctx, "in:%d packet-len:%d index:%d\n", avctx->frame_size, len,
  220. s->buffer_index);
  221. if (len <= s->buffer_index) {
  222. if ((ret = ff_alloc_packet2(avctx, avpkt, len)) < 0)
  223. return ret;
  224. memcpy(avpkt->data, s->buffer, len);
  225. s->buffer_index -= len;
  226. memmove(s->buffer, s->buffer + len, s->buffer_index);
  227. /* Get the next frame pts/duration */
  228. ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
  229. &avpkt->duration);
  230. avpkt->size = len;
  231. *got_packet_ptr = 1;
  232. }
  233. return 0;
  234. }
  235. #define OFFSET(x) offsetof(LAMEContext, x)
  236. #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  237. static const AVOption options[] = {
  238. { "reservoir", "Use bit reservoir.", OFFSET(reservoir), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AE },
  239. { NULL },
  240. };
  241. static const AVClass libmp3lame_class = {
  242. .class_name = "libmp3lame encoder",
  243. .item_name = av_default_item_name,
  244. .option = options,
  245. .version = LIBAVUTIL_VERSION_INT,
  246. };
  247. static const AVCodecDefault libmp3lame_defaults[] = {
  248. { "b", "0" },
  249. { NULL },
  250. };
  251. static const int libmp3lame_sample_rates[] = {
  252. 44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000, 0
  253. };
  254. AVCodec ff_libmp3lame_encoder = {
  255. .name = "libmp3lame",
  256. .type = AVMEDIA_TYPE_AUDIO,
  257. .id = AV_CODEC_ID_MP3,
  258. .priv_data_size = sizeof(LAMEContext),
  259. .init = mp3lame_encode_init,
  260. .encode2 = mp3lame_encode_frame,
  261. .close = mp3lame_encode_close,
  262. .capabilities = CODEC_CAP_DELAY | CODEC_CAP_SMALL_LAST_FRAME,
  263. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S32P,
  264. AV_SAMPLE_FMT_FLTP,
  265. AV_SAMPLE_FMT_S16P,
  266. AV_SAMPLE_FMT_NONE },
  267. .supported_samplerates = libmp3lame_sample_rates,
  268. .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
  269. AV_CH_LAYOUT_STEREO,
  270. 0 },
  271. .long_name = NULL_IF_CONFIG_SMALL("libmp3lame MP3 (MPEG audio layer 3)"),
  272. .priv_class = &libmp3lame_class,
  273. .defaults = libmp3lame_defaults,
  274. };