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.

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