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.

443 lines
17KB

  1. /*
  2. * AAC encoder wrapper
  3. * Copyright (c) 2012 Martin Storsjo
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * * Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <fdk-aac/aacenc_lib.h>
  32. #include "libavutil/channel_layout.h"
  33. #include "libavutil/common.h"
  34. #include "libavutil/opt.h"
  35. #include "avcodec.h"
  36. #include "audio_frame_queue.h"
  37. #include "internal.h"
  38. typedef struct AACContext {
  39. const AVClass *class;
  40. HANDLE_AACENCODER handle;
  41. int afterburner;
  42. int eld_sbr;
  43. int signaling;
  44. int latm;
  45. int header_period;
  46. int vbr;
  47. AudioFrameQueue afq;
  48. } AACContext;
  49. static const AVOption aac_enc_options[] = {
  50. { "afterburner", "Afterburner (improved quality)", offsetof(AACContext, afterburner), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
  51. { "eld_sbr", "Enable SBR for ELD (for SBR in other configurations, use the -profile parameter)", offsetof(AACContext, eld_sbr), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
  52. { "signaling", "SBR/PS signaling style", offsetof(AACContext, signaling), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 2, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, "signaling" },
  53. { "default", "Choose signaling implicitly (explicit hierarchical by default, implicit if global header is disabled)", 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, 0, 0, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, "signaling" },
  54. { "implicit", "Implicit backwards compatible signaling", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, "signaling" },
  55. { "explicit_sbr", "Explicit SBR, implicit PS signaling", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, "signaling" },
  56. { "explicit_hierarchical", "Explicit hierarchical signaling", 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, 0, 0, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, "signaling" },
  57. { "latm", "Output LATM/LOAS encapsulated data", offsetof(AACContext, latm), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
  58. { "header_period", "StreamMuxConfig and PCE repetition period (in frames)", offsetof(AACContext, header_period), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 0xffff, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
  59. { "vbr", "VBR mode (1-5)", offsetof(AACContext, vbr), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 5, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
  60. { NULL }
  61. };
  62. static const AVClass aac_enc_class = {
  63. "libfdk_aac", av_default_item_name, aac_enc_options, LIBAVUTIL_VERSION_INT
  64. };
  65. static const char *aac_get_error(AACENC_ERROR err)
  66. {
  67. switch (err) {
  68. case AACENC_OK:
  69. return "No error";
  70. case AACENC_INVALID_HANDLE:
  71. return "Invalid handle";
  72. case AACENC_MEMORY_ERROR:
  73. return "Memory allocation error";
  74. case AACENC_UNSUPPORTED_PARAMETER:
  75. return "Unsupported parameter";
  76. case AACENC_INVALID_CONFIG:
  77. return "Invalid config";
  78. case AACENC_INIT_ERROR:
  79. return "Initialization error";
  80. case AACENC_INIT_AAC_ERROR:
  81. return "AAC library initialization error";
  82. case AACENC_INIT_SBR_ERROR:
  83. return "SBR library initialization error";
  84. case AACENC_INIT_TP_ERROR:
  85. return "Transport library initialization error";
  86. case AACENC_INIT_META_ERROR:
  87. return "Metadata library initialization error";
  88. case AACENC_ENCODE_ERROR:
  89. return "Encoding error";
  90. case AACENC_ENCODE_EOF:
  91. return "End of file";
  92. default:
  93. return "Unknown error";
  94. }
  95. }
  96. static int aac_encode_close(AVCodecContext *avctx)
  97. {
  98. AACContext *s = avctx->priv_data;
  99. if (s->handle)
  100. aacEncClose(&s->handle);
  101. av_freep(&avctx->extradata);
  102. ff_af_queue_close(&s->afq);
  103. return 0;
  104. }
  105. static av_cold int aac_encode_init(AVCodecContext *avctx)
  106. {
  107. AACContext *s = avctx->priv_data;
  108. int ret = AVERROR(EINVAL);
  109. AACENC_InfoStruct info = { 0 };
  110. CHANNEL_MODE mode;
  111. AACENC_ERROR err;
  112. int aot = FF_PROFILE_AAC_LOW + 1;
  113. int sce = 0, cpe = 0;
  114. if ((err = aacEncOpen(&s->handle, 0, avctx->channels)) != AACENC_OK) {
  115. av_log(avctx, AV_LOG_ERROR, "Unable to open the encoder: %s\n",
  116. aac_get_error(err));
  117. goto error;
  118. }
  119. if (avctx->profile != FF_PROFILE_UNKNOWN)
  120. aot = avctx->profile + 1;
  121. if ((err = aacEncoder_SetParam(s->handle, AACENC_AOT, aot)) != AACENC_OK) {
  122. av_log(avctx, AV_LOG_ERROR, "Unable to set the AOT %d: %s\n",
  123. aot, aac_get_error(err));
  124. goto error;
  125. }
  126. if (aot == FF_PROFILE_AAC_ELD + 1 && s->eld_sbr) {
  127. if ((err = aacEncoder_SetParam(s->handle, AACENC_SBR_MODE,
  128. 1)) != AACENC_OK) {
  129. av_log(avctx, AV_LOG_ERROR, "Unable to enable SBR for ELD: %s\n",
  130. aac_get_error(err));
  131. goto error;
  132. }
  133. }
  134. if ((err = aacEncoder_SetParam(s->handle, AACENC_SAMPLERATE,
  135. avctx->sample_rate)) != AACENC_OK) {
  136. av_log(avctx, AV_LOG_ERROR, "Unable to set the sample rate %d: %s\n",
  137. avctx->sample_rate, aac_get_error(err));
  138. goto error;
  139. }
  140. switch (avctx->channels) {
  141. case 1: mode = MODE_1; sce = 1; cpe = 0; break;
  142. case 2: mode = MODE_2; sce = 0; cpe = 1; break;
  143. case 3: mode = MODE_1_2; sce = 1; cpe = 1; break;
  144. case 4: mode = MODE_1_2_1; sce = 2; cpe = 1; break;
  145. case 5: mode = MODE_1_2_2; sce = 1; cpe = 2; break;
  146. case 6: mode = MODE_1_2_2_1; sce = 2; cpe = 2; break;
  147. /* The version macro is introduced the same time as the 7.1 support, so this
  148. should suffice. */
  149. #ifdef AACENCODER_LIB_VL0
  150. case 8:
  151. sce = 2;
  152. cpe = 3;
  153. if (avctx->channel_layout == AV_CH_LAYOUT_7POINT1) {
  154. mode = MODE_7_1_REAR_SURROUND;
  155. } else {
  156. // MODE_1_2_2_2_1 and MODE_7_1_FRONT_CENTER use the same channel layout
  157. mode = MODE_7_1_FRONT_CENTER;
  158. }
  159. break;
  160. #endif
  161. default:
  162. av_log(avctx, AV_LOG_ERROR,
  163. "Unsupported number of channels %d\n", avctx->channels);
  164. goto error;
  165. }
  166. if ((err = aacEncoder_SetParam(s->handle, AACENC_CHANNELMODE,
  167. mode)) != AACENC_OK) {
  168. av_log(avctx, AV_LOG_ERROR,
  169. "Unable to set channel mode %d: %s\n", mode, aac_get_error(err));
  170. goto error;
  171. }
  172. if ((err = aacEncoder_SetParam(s->handle, AACENC_CHANNELORDER,
  173. 1)) != AACENC_OK) {
  174. av_log(avctx, AV_LOG_ERROR,
  175. "Unable to set wav channel order %d: %s\n",
  176. mode, aac_get_error(err));
  177. goto error;
  178. }
  179. if (avctx->flags & CODEC_FLAG_QSCALE || s->vbr) {
  180. int mode = s->vbr ? s->vbr : avctx->global_quality;
  181. if (mode < 1 || mode > 5) {
  182. av_log(avctx, AV_LOG_WARNING,
  183. "VBR quality %d out of range, should be 1-5\n", mode);
  184. mode = av_clip(mode, 1, 5);
  185. }
  186. av_log(avctx, AV_LOG_WARNING,
  187. "Note, the VBR setting is unsupported and only works with "
  188. "some parameter combinations\n");
  189. if ((err = aacEncoder_SetParam(s->handle, AACENC_BITRATEMODE,
  190. mode)) != AACENC_OK) {
  191. av_log(avctx, AV_LOG_ERROR, "Unable to set the VBR bitrate mode %d: %s\n",
  192. mode, aac_get_error(err));
  193. goto error;
  194. }
  195. } else {
  196. if (avctx->bit_rate <= 0) {
  197. if (avctx->profile == FF_PROFILE_AAC_HE_V2) {
  198. sce = 1;
  199. cpe = 0;
  200. }
  201. avctx->bit_rate = (96*sce + 128*cpe) * avctx->sample_rate / 44;
  202. if (avctx->profile == FF_PROFILE_AAC_HE ||
  203. avctx->profile == FF_PROFILE_AAC_HE_V2 ||
  204. avctx->profile == FF_PROFILE_MPEG2_AAC_HE ||
  205. s->eld_sbr)
  206. avctx->bit_rate /= 2;
  207. }
  208. if ((err = aacEncoder_SetParam(s->handle, AACENC_BITRATE,
  209. avctx->bit_rate)) != AACENC_OK) {
  210. av_log(avctx, AV_LOG_ERROR, "Unable to set the bitrate %d: %s\n",
  211. avctx->bit_rate, aac_get_error(err));
  212. goto error;
  213. }
  214. }
  215. /* Choose bitstream format - if global header is requested, use
  216. * raw access units, otherwise use ADTS. */
  217. if ((err = aacEncoder_SetParam(s->handle, AACENC_TRANSMUX,
  218. avctx->flags & CODEC_FLAG_GLOBAL_HEADER ? 0 : s->latm ? 10 : 2)) != AACENC_OK) {
  219. av_log(avctx, AV_LOG_ERROR, "Unable to set the transmux format: %s\n",
  220. aac_get_error(err));
  221. goto error;
  222. }
  223. if (s->latm && s->header_period) {
  224. if ((err = aacEncoder_SetParam(s->handle, AACENC_HEADER_PERIOD,
  225. s->header_period)) != AACENC_OK) {
  226. av_log(avctx, AV_LOG_ERROR, "Unable to set header period: %s\n",
  227. aac_get_error(err));
  228. goto error;
  229. }
  230. }
  231. /* If no signaling mode is chosen, use explicit hierarchical signaling
  232. * if using mp4 mode (raw access units, with global header) and
  233. * implicit signaling if using ADTS. */
  234. if (s->signaling < 0)
  235. s->signaling = avctx->flags & CODEC_FLAG_GLOBAL_HEADER ? 2 : 0;
  236. if ((err = aacEncoder_SetParam(s->handle, AACENC_SIGNALING_MODE,
  237. s->signaling)) != AACENC_OK) {
  238. av_log(avctx, AV_LOG_ERROR, "Unable to set signaling mode %d: %s\n",
  239. s->signaling, aac_get_error(err));
  240. goto error;
  241. }
  242. if ((err = aacEncoder_SetParam(s->handle, AACENC_AFTERBURNER,
  243. s->afterburner)) != AACENC_OK) {
  244. av_log(avctx, AV_LOG_ERROR, "Unable to set afterburner to %d: %s\n",
  245. s->afterburner, aac_get_error(err));
  246. goto error;
  247. }
  248. if (avctx->cutoff > 0) {
  249. if (avctx->cutoff < (avctx->sample_rate + 255) >> 8 || avctx->cutoff > 20000) {
  250. av_log(avctx, AV_LOG_ERROR, "cutoff valid range is %d-20000\n",
  251. (avctx->sample_rate + 255) >> 8);
  252. goto error;
  253. }
  254. if ((err = aacEncoder_SetParam(s->handle, AACENC_BANDWIDTH,
  255. avctx->cutoff)) != AACENC_OK) {
  256. av_log(avctx, AV_LOG_ERROR, "Unable to set the encoder bandwidth to %d: %s\n",
  257. avctx->cutoff, aac_get_error(err));
  258. goto error;
  259. }
  260. }
  261. if ((err = aacEncEncode(s->handle, NULL, NULL, NULL, NULL)) != AACENC_OK) {
  262. av_log(avctx, AV_LOG_ERROR, "Unable to initialize the encoder: %s\n",
  263. aac_get_error(err));
  264. return AVERROR(EINVAL);
  265. }
  266. if ((err = aacEncInfo(s->handle, &info)) != AACENC_OK) {
  267. av_log(avctx, AV_LOG_ERROR, "Unable to get encoder info: %s\n",
  268. aac_get_error(err));
  269. goto error;
  270. }
  271. avctx->frame_size = info.frameLength;
  272. avctx->delay = info.encoderDelay;
  273. ff_af_queue_init(avctx, &s->afq);
  274. if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
  275. avctx->extradata_size = info.confSize;
  276. avctx->extradata = av_mallocz(avctx->extradata_size +
  277. FF_INPUT_BUFFER_PADDING_SIZE);
  278. if (!avctx->extradata) {
  279. ret = AVERROR(ENOMEM);
  280. goto error;
  281. }
  282. memcpy(avctx->extradata, info.confBuf, info.confSize);
  283. }
  284. return 0;
  285. error:
  286. aac_encode_close(avctx);
  287. return ret;
  288. }
  289. static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  290. const AVFrame *frame, int *got_packet_ptr)
  291. {
  292. AACContext *s = avctx->priv_data;
  293. AACENC_BufDesc in_buf = { 0 }, out_buf = { 0 };
  294. AACENC_InArgs in_args = { 0 };
  295. AACENC_OutArgs out_args = { 0 };
  296. int in_buffer_identifier = IN_AUDIO_DATA;
  297. int in_buffer_size, in_buffer_element_size;
  298. int out_buffer_identifier = OUT_BITSTREAM_DATA;
  299. int out_buffer_size, out_buffer_element_size;
  300. void *in_ptr, *out_ptr;
  301. int ret;
  302. AACENC_ERROR err;
  303. /* handle end-of-stream small frame and flushing */
  304. if (!frame) {
  305. in_args.numInSamples = -1;
  306. } else {
  307. in_ptr = frame->data[0];
  308. in_buffer_size = 2 * avctx->channels * frame->nb_samples;
  309. in_buffer_element_size = 2;
  310. in_args.numInSamples = avctx->channels * frame->nb_samples;
  311. in_buf.numBufs = 1;
  312. in_buf.bufs = &in_ptr;
  313. in_buf.bufferIdentifiers = &in_buffer_identifier;
  314. in_buf.bufSizes = &in_buffer_size;
  315. in_buf.bufElSizes = &in_buffer_element_size;
  316. /* add current frame to the queue */
  317. if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
  318. return ret;
  319. }
  320. /* The maximum packet size is 6144 bits aka 768 bytes per channel. */
  321. if ((ret = ff_alloc_packet(avpkt, FFMAX(8192, 768 * avctx->channels)))) {
  322. av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
  323. return ret;
  324. }
  325. out_ptr = avpkt->data;
  326. out_buffer_size = avpkt->size;
  327. out_buffer_element_size = 1;
  328. out_buf.numBufs = 1;
  329. out_buf.bufs = &out_ptr;
  330. out_buf.bufferIdentifiers = &out_buffer_identifier;
  331. out_buf.bufSizes = &out_buffer_size;
  332. out_buf.bufElSizes = &out_buffer_element_size;
  333. if ((err = aacEncEncode(s->handle, &in_buf, &out_buf, &in_args,
  334. &out_args)) != AACENC_OK) {
  335. if (!frame && err == AACENC_ENCODE_EOF)
  336. return 0;
  337. av_log(avctx, AV_LOG_ERROR, "Unable to encode frame: %s\n",
  338. aac_get_error(err));
  339. return AVERROR(EINVAL);
  340. }
  341. if (!out_args.numOutBytes)
  342. return 0;
  343. /* Get the next frame pts & duration */
  344. ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
  345. &avpkt->duration);
  346. avpkt->size = out_args.numOutBytes;
  347. *got_packet_ptr = 1;
  348. return 0;
  349. }
  350. static const AVProfile profiles[] = {
  351. { FF_PROFILE_AAC_LOW, "LC" },
  352. { FF_PROFILE_AAC_HE, "HE-AAC" },
  353. { FF_PROFILE_AAC_HE_V2, "HE-AACv2" },
  354. { FF_PROFILE_AAC_LD, "LD" },
  355. { FF_PROFILE_AAC_ELD, "ELD" },
  356. { FF_PROFILE_UNKNOWN },
  357. };
  358. static const AVCodecDefault aac_encode_defaults[] = {
  359. { "b", "0" },
  360. { NULL }
  361. };
  362. static const uint64_t aac_channel_layout[] = {
  363. AV_CH_LAYOUT_MONO,
  364. AV_CH_LAYOUT_STEREO,
  365. AV_CH_LAYOUT_SURROUND,
  366. AV_CH_LAYOUT_4POINT0,
  367. AV_CH_LAYOUT_5POINT0_BACK,
  368. AV_CH_LAYOUT_5POINT1_BACK,
  369. #ifdef AACENCODER_LIB_VL0
  370. AV_CH_LAYOUT_7POINT1_WIDE_BACK,
  371. AV_CH_LAYOUT_7POINT1,
  372. #endif
  373. 0,
  374. };
  375. static const int aac_sample_rates[] = {
  376. 96000, 88200, 64000, 48000, 44100, 32000,
  377. 24000, 22050, 16000, 12000, 11025, 8000, 0
  378. };
  379. AVCodec ff_libfdk_aac_encoder = {
  380. .name = "libfdk_aac",
  381. .long_name = NULL_IF_CONFIG_SMALL("Fraunhofer FDK AAC"),
  382. .type = AVMEDIA_TYPE_AUDIO,
  383. .id = AV_CODEC_ID_AAC,
  384. .priv_data_size = sizeof(AACContext),
  385. .init = aac_encode_init,
  386. .encode2 = aac_encode_frame,
  387. .close = aac_encode_close,
  388. .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
  389. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
  390. AV_SAMPLE_FMT_NONE },
  391. .priv_class = &aac_enc_class,
  392. .defaults = aac_encode_defaults,
  393. .profiles = profiles,
  394. .supported_samplerates = aac_sample_rates,
  395. .channel_layouts = aac_channel_layout,
  396. };