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.

230 lines
8.3KB

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