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.

431 lines
16KB

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