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 Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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)
  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. ret = AVERROR(EINVAL);
  68. goto error;
  69. }
  70. lame_set_num_channels(s->gfp, avctx->channels);
  71. lame_set_mode(s->gfp, avctx->channels > 1 ? JOINT_STEREO : MONO);
  72. /* sample rate */
  73. lame_set_in_samplerate (s->gfp, avctx->sample_rate);
  74. lame_set_out_samplerate(s->gfp, avctx->sample_rate);
  75. /* algorithmic quality */
  76. if (avctx->compression_level == FF_COMPRESSION_DEFAULT)
  77. lame_set_quality(s->gfp, 5);
  78. else
  79. lame_set_quality(s->gfp, avctx->compression_level);
  80. /* rate control */
  81. if (avctx->flags & CODEC_FLAG_QSCALE) {
  82. lame_set_VBR(s->gfp, vbr_default);
  83. lame_set_VBR_quality(s->gfp, avctx->global_quality / (float)FF_QP2LAMBDA);
  84. } else {
  85. if (avctx->bit_rate)
  86. lame_set_brate(s->gfp, avctx->bit_rate / 1000);
  87. }
  88. /* do not get a Xing VBR header frame from LAME */
  89. lame_set_bWriteVbrTag(s->gfp,0);
  90. /* bit reservoir usage */
  91. lame_set_disable_reservoir(s->gfp, !s->reservoir);
  92. /* set specified parameters */
  93. if (lame_init_params(s->gfp) < 0) {
  94. ret = -1;
  95. goto error;
  96. }
  97. /* get encoder delay */
  98. avctx->delay = lame_get_encoder_delay(s->gfp) + 528 + 1;
  99. ff_af_queue_init(avctx, &s->afq);
  100. avctx->frame_size = lame_get_framesize(s->gfp);
  101. #if FF_API_OLD_ENCODE_AUDIO
  102. avctx->coded_frame = avcodec_alloc_frame();
  103. if (!avctx->coded_frame) {
  104. ret = AVERROR(ENOMEM);
  105. goto error;
  106. }
  107. #endif
  108. /* sample format */
  109. if (avctx->sample_fmt == AV_SAMPLE_FMT_S32 ||
  110. avctx->sample_fmt == AV_SAMPLE_FMT_FLT) {
  111. int ch;
  112. for (ch = 0; ch < avctx->channels; ch++) {
  113. s->planar_samples[ch] = av_malloc(avctx->frame_size *
  114. av_get_bytes_per_sample(avctx->sample_fmt));
  115. if (!s->planar_samples[ch]) {
  116. ret = AVERROR(ENOMEM);
  117. goto error;
  118. }
  119. }
  120. }
  121. return 0;
  122. error:
  123. mp3lame_encode_close(avctx);
  124. return ret;
  125. }
  126. #define DEINTERLEAVE(type, scale) do { \
  127. int ch, i; \
  128. for (ch = 0; ch < s->avctx->channels; ch++) { \
  129. const type *input = samples; \
  130. type *output = s->planar_samples[ch]; \
  131. input += ch; \
  132. for (i = 0; i < nb_samples; i++) { \
  133. output[i] = *input * scale; \
  134. input += s->avctx->channels; \
  135. } \
  136. } \
  137. } while (0)
  138. static int encode_frame_int16(LAMEContext *s, void *samples, int nb_samples)
  139. {
  140. if (s->avctx->channels > 1) {
  141. return lame_encode_buffer_interleaved(s->gfp, samples,
  142. nb_samples,
  143. s->buffer + s->buffer_index,
  144. BUFFER_SIZE - s->buffer_index);
  145. } else {
  146. return lame_encode_buffer(s->gfp, samples, NULL, nb_samples,
  147. s->buffer + s->buffer_index,
  148. BUFFER_SIZE - s->buffer_index);
  149. }
  150. }
  151. static int encode_frame_int32(LAMEContext *s, void *samples, int nb_samples)
  152. {
  153. DEINTERLEAVE(int32_t, 1);
  154. return lame_encode_buffer_int(s->gfp,
  155. s->planar_samples[0], s->planar_samples[1],
  156. nb_samples,
  157. s->buffer + s->buffer_index,
  158. BUFFER_SIZE - s->buffer_index);
  159. }
  160. static int encode_frame_float(LAMEContext *s, void *samples, int nb_samples)
  161. {
  162. DEINTERLEAVE(float, 32768.0f);
  163. return lame_encode_buffer_float(s->gfp,
  164. s->planar_samples[0], s->planar_samples[1],
  165. nb_samples,
  166. s->buffer + s->buffer_index,
  167. BUFFER_SIZE - s->buffer_index);
  168. }
  169. static int mp3lame_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  170. const AVFrame *frame, int *got_packet_ptr)
  171. {
  172. LAMEContext *s = avctx->priv_data;
  173. MPADecodeHeader hdr;
  174. int len, ret;
  175. int lame_result;
  176. if (frame) {
  177. switch (avctx->sample_fmt) {
  178. case AV_SAMPLE_FMT_S16:
  179. lame_result = encode_frame_int16(s, frame->data[0], frame->nb_samples);
  180. break;
  181. case AV_SAMPLE_FMT_S32:
  182. lame_result = encode_frame_int32(s, frame->data[0], frame->nb_samples);
  183. break;
  184. case AV_SAMPLE_FMT_FLT:
  185. lame_result = encode_frame_float(s, frame->data[0], frame->nb_samples);
  186. break;
  187. default:
  188. return AVERROR_BUG;
  189. }
  190. } else {
  191. lame_result = lame_encode_flush(s->gfp, s->buffer + s->buffer_index,
  192. BUFFER_SIZE - s->buffer_index);
  193. }
  194. if (lame_result < 0) {
  195. if (lame_result == -1) {
  196. av_log(avctx, AV_LOG_ERROR,
  197. "lame: output buffer too small (buffer index: %d, free bytes: %d)\n",
  198. s->buffer_index, BUFFER_SIZE - s->buffer_index);
  199. }
  200. return -1;
  201. }
  202. s->buffer_index += lame_result;
  203. /* add current frame to the queue */
  204. if (frame) {
  205. if ((ret = ff_af_queue_add(&s->afq, frame) < 0))
  206. return ret;
  207. }
  208. /* Move 1 frame from the LAME buffer to the output packet, if available.
  209. We have to parse the first frame header in the output buffer to
  210. determine the frame size. */
  211. if (s->buffer_index < 4)
  212. return 0;
  213. if (avpriv_mpegaudio_decode_header(&hdr, AV_RB32(s->buffer))) {
  214. av_log(avctx, AV_LOG_ERROR, "free format output not supported\n");
  215. return -1;
  216. }
  217. len = hdr.frame_size;
  218. av_dlog(avctx, "in:%d packet-len:%d index:%d\n", avctx->frame_size, len,
  219. s->buffer_index);
  220. if (len <= s->buffer_index) {
  221. if ((ret = ff_alloc_packet(avpkt, len))) {
  222. av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
  223. return ret;
  224. }
  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. };