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.

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