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.

315 lines
10KB

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