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.

199 lines
7.6KB

  1. /*
  2. * Interface to libtwolame for mp2 encoding
  3. * Copyright (c) 2012 Paul B Mahol
  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 libtwolame for mp2 encoding.
  24. */
  25. #include <twolame.h>
  26. #include "libavutil/opt.h"
  27. #include "avcodec.h"
  28. #include "internal.h"
  29. #include "mpegaudio.h"
  30. typedef struct TWOLAMEContext {
  31. AVClass *class;
  32. int mode;
  33. int psymodel;
  34. int energy;
  35. int error_protection;
  36. int copyright;
  37. int original;
  38. twolame_options *glopts;
  39. int64_t next_pts;
  40. } TWOLAMEContext;
  41. static av_cold int twolame_encode_close(AVCodecContext *avctx)
  42. {
  43. TWOLAMEContext *s = avctx->priv_data;
  44. twolame_close(&s->glopts);
  45. return 0;
  46. }
  47. static av_cold int twolame_encode_init(AVCodecContext *avctx)
  48. {
  49. TWOLAMEContext *s = avctx->priv_data;
  50. int ret;
  51. avctx->frame_size = TWOLAME_SAMPLES_PER_FRAME;
  52. s->glopts = twolame_init();
  53. if (!s->glopts)
  54. return AVERROR(ENOMEM);
  55. twolame_set_verbosity(s->glopts, 0);
  56. twolame_set_mode(s->glopts, s->mode);
  57. twolame_set_psymodel(s->glopts, s->psymodel);
  58. twolame_set_energy_levels(s->glopts, s->energy);
  59. twolame_set_error_protection(s->glopts, s->error_protection);
  60. twolame_set_copyright(s->glopts, s->copyright);
  61. twolame_set_original(s->glopts, s->original);
  62. twolame_set_num_channels(s->glopts, avctx->channels);
  63. twolame_set_in_samplerate(s->glopts, avctx->sample_rate);
  64. twolame_set_out_samplerate(s->glopts, avctx->sample_rate);
  65. if (avctx->flags & CODEC_FLAG_QSCALE || !avctx->bit_rate) {
  66. twolame_set_VBR(s->glopts, TRUE);
  67. twolame_set_VBR_level(s->glopts, avctx->global_quality);
  68. av_log(avctx, AV_LOG_WARNING, "VBR mode is experimental!\n");
  69. } else {
  70. twolame_set_bitrate(s->glopts, avctx->bit_rate / 1000);
  71. }
  72. if ((ret = twolame_init_params(s->glopts)))
  73. goto error;
  74. return 0;
  75. error:
  76. twolame_encode_close(avctx);
  77. return ret;
  78. }
  79. static int twolame_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  80. const AVFrame *frame, int *got_packet_ptr)
  81. {
  82. TWOLAMEContext *s = avctx->priv_data;
  83. int ret;
  84. if ((ret = ff_alloc_packet2(avctx, avpkt, MPA_MAX_CODED_FRAME_SIZE)) < 0)
  85. return ret;
  86. if (frame) {
  87. switch (avctx->sample_fmt) {
  88. case AV_SAMPLE_FMT_FLT:
  89. ret = twolame_encode_buffer_float32_interleaved(s->glopts,
  90. frame->data[0],
  91. frame->nb_samples,
  92. avpkt->data, avpkt->size);
  93. break;
  94. case AV_SAMPLE_FMT_FLTP:
  95. ret = twolame_encode_buffer_float32(s->glopts,
  96. frame->data[0], frame->data[1],
  97. frame->nb_samples,
  98. avpkt->data, avpkt->size);
  99. break;
  100. case AV_SAMPLE_FMT_S16:
  101. ret = twolame_encode_buffer_interleaved(s->glopts,
  102. frame->data[0],
  103. frame->nb_samples,
  104. avpkt->data, avpkt->size);
  105. break;
  106. case AV_SAMPLE_FMT_S16P:
  107. ret = twolame_encode_buffer(s->glopts,
  108. frame->data[0], frame->data[1],
  109. frame->nb_samples,
  110. avpkt->data, avpkt->size);
  111. break;
  112. default:
  113. return AVERROR_BUG;
  114. }
  115. } else {
  116. ret = twolame_encode_flush(s->glopts, avpkt->data, avpkt->size);
  117. }
  118. if (ret > 0) {
  119. avpkt->duration = ff_samples_to_time_base(avctx, avctx->frame_size);
  120. if (frame) {
  121. if (frame->pts != AV_NOPTS_VALUE)
  122. avpkt->pts = frame->pts;
  123. } else {
  124. avpkt->pts = s->next_pts;
  125. }
  126. if (avpkt->pts != AV_NOPTS_VALUE)
  127. s->next_pts = avpkt->pts + avpkt->duration;
  128. avpkt->size = ret;
  129. *got_packet_ptr = 1;
  130. return 0;
  131. }
  132. return ret;
  133. }
  134. #define OFFSET(x) offsetof(TWOLAMEContext, x)
  135. #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  136. static const AVOption options[] = {
  137. { "mode", "Mpeg Mode", OFFSET(mode), AV_OPT_TYPE_INT, { .i64 = TWOLAME_AUTO_MODE }, TWOLAME_AUTO_MODE, TWOLAME_MONO, AE, "mode"},
  138. { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_AUTO_MODE }, 0, 0, AE, "mode" },
  139. { "stereo", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_STEREO }, 0, 0, AE, "mode" },
  140. { "joint_stereo", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_JOINT_STEREO }, 0, 0, AE, "mode" },
  141. { "dual_channel", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_DUAL_CHANNEL }, 0, 0, AE, "mode" },
  142. { "mono", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_MONO }, 0, 0, AE, "mode" },
  143. { "psymodel", "Psychoacoustic Model", OFFSET(psymodel), AV_OPT_TYPE_INT, { .i64 = 3 }, -1, 4, AE},
  144. { "energy_levels","enable energy levels", OFFSET(energy), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE},
  145. { "error_protection","enable CRC error protection", OFFSET(error_protection), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE},
  146. { "copyright", "set MPEG Audio Copyright flag", OFFSET(copyright), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE},
  147. { "original", "set MPEG Audio Original flag", OFFSET(original), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE},
  148. { NULL },
  149. };
  150. static const AVClass libtwolame_class = {
  151. .class_name = "libtwolame encoder",
  152. .item_name = av_default_item_name,
  153. .option = options,
  154. .version = LIBAVUTIL_VERSION_INT,
  155. };
  156. AVCodec ff_libtwolame_encoder = {
  157. .name = "libtwolame",
  158. .type = AVMEDIA_TYPE_AUDIO,
  159. .id = AV_CODEC_ID_MP2,
  160. .priv_data_size = sizeof(TWOLAMEContext),
  161. .init = twolame_encode_init,
  162. .encode2 = twolame_encode_frame,
  163. .close = twolame_encode_close,
  164. .capabilities = CODEC_CAP_DELAY,
  165. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLT,
  166. AV_SAMPLE_FMT_FLTP,
  167. AV_SAMPLE_FMT_S16,
  168. AV_SAMPLE_FMT_S16P,
  169. AV_SAMPLE_FMT_NONE },
  170. .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
  171. AV_CH_LAYOUT_STEREO,
  172. 0 },
  173. .supported_samplerates = (const int[]){ 16000, 22050, 24000, 32000, 44100, 48000, 0 },
  174. .long_name = NULL_IF_CONFIG_SMALL("libtwolame MP2 (MPEG audio layer 2)"),
  175. .priv_class = &libtwolame_class,
  176. };