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.

226 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 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 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->initial_padding = 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->flags & AV_CODEC_FLAG_QSCALE || !avctx->bit_rate) {
  69. twolame_set_VBR(s->glopts, TRUE);
  70. twolame_set_VBR_level(s->glopts,
  71. avctx->global_quality / (float) FF_QP2LAMBDA);
  72. av_log(avctx, AV_LOG_WARNING,
  73. "VBR in MP2 is a hack, use another codec that supports it.\n");
  74. } else {
  75. twolame_set_bitrate(s->glopts, avctx->bit_rate / 1000);
  76. }
  77. ret = twolame_init_params(s->glopts);
  78. if (ret) {
  79. twolame_encode_close(avctx);
  80. return AVERROR_UNKNOWN;
  81. }
  82. return 0;
  83. }
  84. static int twolame_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  85. const AVFrame *frame, int *got_packet_ptr)
  86. {
  87. TWOLAMEContext *s = avctx->priv_data;
  88. int ret;
  89. if ((ret = ff_alloc_packet(avpkt, MPA_MAX_CODED_FRAME_SIZE)) < 0)
  90. return ret;
  91. if (frame) {
  92. switch (avctx->sample_fmt) {
  93. case AV_SAMPLE_FMT_FLT:
  94. ret = twolame_encode_buffer_float32_interleaved(s->glopts,
  95. (const float *)frame->data[0],
  96. frame->nb_samples,
  97. avpkt->data,
  98. avpkt->size);
  99. break;
  100. case AV_SAMPLE_FMT_FLTP:
  101. ret = twolame_encode_buffer_float32(s->glopts,
  102. (const float *)frame->data[0],
  103. (const float *)frame->data[1],
  104. frame->nb_samples,
  105. avpkt->data, avpkt->size);
  106. break;
  107. case AV_SAMPLE_FMT_S16:
  108. ret = twolame_encode_buffer_interleaved(s->glopts,
  109. (const short int *)frame->data[0],
  110. frame->nb_samples,
  111. avpkt->data, avpkt->size);
  112. break;
  113. case AV_SAMPLE_FMT_S16P:
  114. ret = twolame_encode_buffer(s->glopts,
  115. (const short int *)frame->data[0],
  116. (const short int *)frame->data[1],
  117. frame->nb_samples,
  118. avpkt->data, avpkt->size);
  119. break;
  120. default:
  121. av_log(avctx, AV_LOG_ERROR,
  122. "Unsupported sample format %d.\n", avctx->sample_fmt);
  123. return AVERROR_BUG;
  124. }
  125. } else {
  126. ret = twolame_encode_flush(s->glopts, avpkt->data, avpkt->size);
  127. }
  128. if (!ret) // no bytes written
  129. return 0;
  130. if (ret < 0) // twolame error
  131. return AVERROR_UNKNOWN;
  132. if (frame) {
  133. avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
  134. if (frame->pts != AV_NOPTS_VALUE)
  135. avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->initial_padding);
  136. } else {
  137. avpkt->pts = s->next_pts;
  138. }
  139. // this is for setting pts for flushed packet(s).
  140. if (avpkt->pts != AV_NOPTS_VALUE)
  141. s->next_pts = avpkt->pts + avpkt->duration;
  142. av_shrink_packet(avpkt, ret);
  143. *got_packet_ptr = 1;
  144. return 0;
  145. }
  146. #define OFFSET(x) offsetof(TWOLAMEContext, x)
  147. #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  148. static const AVOption options[] = {
  149. { "mode", "Mpeg Mode", OFFSET(mode), AV_OPT_TYPE_INT, { .i64 = TWOLAME_AUTO_MODE }, TWOLAME_AUTO_MODE, TWOLAME_MONO, AE, "mode"},
  150. { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_AUTO_MODE }, 0, 0, AE, "mode" },
  151. { "stereo", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_STEREO }, 0, 0, AE, "mode" },
  152. { "joint_stereo", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_JOINT_STEREO }, 0, 0, AE, "mode" },
  153. { "dual_channel", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_DUAL_CHANNEL }, 0, 0, AE, "mode" },
  154. { "mono", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TWOLAME_MONO }, 0, 0, AE, "mode" },
  155. { "psymodel", "Psychoacoustic Model", OFFSET(psymodel), AV_OPT_TYPE_INT, { .i64 = 3 }, -1, 4, AE},
  156. { "energy_levels","enable energy levels", OFFSET(energy), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE},
  157. { "error_protection","enable CRC error protection", OFFSET(error_protection), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE},
  158. { "copyright", "set MPEG Audio Copyright flag", OFFSET(copyright), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE},
  159. { "original", "set MPEG Audio Original flag", OFFSET(original), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AE},
  160. { "verbosity", "set library optput level (0-10)", OFFSET(verbosity), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 10, AE},
  161. { NULL },
  162. };
  163. static const AVClass twolame_class = {
  164. .class_name = "libtwolame encoder",
  165. .item_name = av_default_item_name,
  166. .option = options,
  167. .version = LIBAVUTIL_VERSION_INT,
  168. };
  169. static const AVCodecDefault twolame_defaults[] = {
  170. { "b", "384000" },
  171. { NULL },
  172. };
  173. static const int twolame_samplerates[] = {
  174. 16000, 22050, 24000, 32000, 44100, 48000, 0
  175. };
  176. AVCodec ff_libtwolame_encoder = {
  177. .name = "libtwolame",
  178. .long_name = NULL_IF_CONFIG_SMALL("libtwolame MP2 (MPEG audio layer 2)"),
  179. .type = AVMEDIA_TYPE_AUDIO,
  180. .id = AV_CODEC_ID_MP2,
  181. .priv_data_size = sizeof(TWOLAMEContext),
  182. .init = twolame_encode_init,
  183. .encode2 = twolame_encode_frame,
  184. .close = twolame_encode_close,
  185. .capabilities = AV_CODEC_CAP_DELAY,
  186. .defaults = twolame_defaults,
  187. .priv_class = &twolame_class,
  188. .sample_fmts = (const enum AVSampleFormat[]) {
  189. AV_SAMPLE_FMT_FLT,
  190. AV_SAMPLE_FMT_FLTP,
  191. AV_SAMPLE_FMT_S16,
  192. AV_SAMPLE_FMT_S16P,
  193. AV_SAMPLE_FMT_NONE
  194. },
  195. .channel_layouts = (const uint64_t[]) {
  196. AV_CH_LAYOUT_MONO,
  197. AV_CH_LAYOUT_STEREO,
  198. 0 },
  199. .supported_samplerates = twolame_samplerates,
  200. };