libfdk-aacenc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * AAC encoder wrapper
  3. * Copyright (c) 2012 Martin Storsjo
  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. #include <fdk-aac/aacenc_lib.h>
  22. #include "avcodec.h"
  23. #include "audio_frame_queue.h"
  24. #include "internal.h"
  25. #include "libavutil/audioconvert.h"
  26. #include "libavutil/common.h"
  27. #include "libavutil/opt.h"
  28. typedef struct AACContext {
  29. const AVClass *class;
  30. HANDLE_AACENCODER handle;
  31. int afterburner;
  32. int eld_sbr;
  33. int signaling;
  34. int latm;
  35. int header_period;
  36. AudioFrameQueue afq;
  37. } AACContext;
  38. static const AVOption aac_enc_options[] = {
  39. { "afterburner", "Afterburner (improved quality)", offsetof(AACContext, afterburner), AV_OPT_TYPE_INT, { 1 }, 0, 1, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
  40. { "eld_sbr", "Enable SBR for ELD (for SBR in other configurations, use the -profile parameter)", offsetof(AACContext, eld_sbr), AV_OPT_TYPE_INT, { 0 }, 0, 1, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
  41. { "signaling", "SBR/PS signaling style", offsetof(AACContext, signaling), AV_OPT_TYPE_INT, { -1 }, -1, 2, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, "signaling" },
  42. { "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" },
  43. { "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" },
  44. { "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" },
  45. { "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" },
  46. { "latm", "Output LATM/LOAS encapsulated data", offsetof(AACContext, latm), AV_OPT_TYPE_INT, { 0 }, 0, 1, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
  47. { "header_period", "StreamMuxConfig and PCE repetition period (in frames)", offsetof(AACContext, header_period), AV_OPT_TYPE_INT, { 0 }, 0, 0xffff, 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. #if FF_API_OLD_ENCODE_AUDIO
  90. av_freep(&avctx->coded_frame);
  91. #endif
  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. default:
  139. av_log(avctx, AV_LOG_ERROR,
  140. "Unsupported number of channels %d\n", avctx->channels);
  141. goto error;
  142. }
  143. if ((err = aacEncoder_SetParam(s->handle, AACENC_CHANNELMODE,
  144. mode)) != AACENC_OK) {
  145. av_log(avctx, AV_LOG_ERROR,
  146. "Unable to set channel mode %d: %s\n", mode, aac_get_error(err));
  147. goto error;
  148. }
  149. if ((err = aacEncoder_SetParam(s->handle, AACENC_CHANNELORDER,
  150. 1)) != AACENC_OK) {
  151. av_log(avctx, AV_LOG_ERROR,
  152. "Unable to set wav channel order %d: %s\n",
  153. mode, aac_get_error(err));
  154. goto error;
  155. }
  156. if (avctx->flags & CODEC_FLAG_QSCALE) {
  157. int mode = avctx->global_quality;
  158. if (mode < 1 || mode > 5) {
  159. av_log(avctx, AV_LOG_WARNING,
  160. "VBR quality %d out of range, should be 1-5\n", mode);
  161. mode = av_clip(mode, 1, 5);
  162. }
  163. if ((err = aacEncoder_SetParam(s->handle, AACENC_BITRATEMODE,
  164. mode)) != AACENC_OK) {
  165. av_log(avctx, AV_LOG_ERROR, "Unable to set the VBR bitrate mode %d: %s\n",
  166. mode, aac_get_error(err));
  167. goto error;
  168. }
  169. } else {
  170. if (avctx->bit_rate <= 0) {
  171. if (avctx->profile == FF_PROFILE_AAC_HE_V2) {
  172. sce = 1;
  173. cpe = 0;
  174. }
  175. avctx->bit_rate = (96*sce + 128*cpe) * avctx->sample_rate / 44;
  176. if (avctx->profile == FF_PROFILE_AAC_HE ||
  177. avctx->profile == FF_PROFILE_AAC_HE_V2 ||
  178. s->eld_sbr)
  179. avctx->bit_rate /= 2;
  180. }
  181. if ((err = aacEncoder_SetParam(s->handle, AACENC_BITRATE,
  182. avctx->bit_rate)) != AACENC_OK) {
  183. av_log(avctx, AV_LOG_ERROR, "Unable to set the bitrate %d: %s\n",
  184. avctx->bit_rate, aac_get_error(err));
  185. goto error;
  186. }
  187. }
  188. /* Choose bitstream format - if global header is requested, use
  189. * raw access units, otherwise use ADTS. */
  190. if ((err = aacEncoder_SetParam(s->handle, AACENC_TRANSMUX,
  191. avctx->flags & CODEC_FLAG_GLOBAL_HEADER ? 0 : s->latm ? 10 : 2)) != AACENC_OK) {
  192. av_log(avctx, AV_LOG_ERROR, "Unable to set the transmux format: %s\n",
  193. aac_get_error(err));
  194. goto error;
  195. }
  196. if (s->latm && s->header_period) {
  197. if ((err = aacEncoder_SetParam(s->handle, AACENC_HEADER_PERIOD,
  198. s->header_period)) != AACENC_OK) {
  199. av_log(avctx, AV_LOG_ERROR, "Unable to set header period: %s\n",
  200. aac_get_error(err));
  201. goto error;
  202. }
  203. }
  204. /* If no signaling mode is chosen, use explicit hierarchical signaling
  205. * if using mp4 mode (raw access units, with global header) and
  206. * implicit signaling if using ADTS. */
  207. if (s->signaling < 0)
  208. s->signaling = avctx->flags & CODEC_FLAG_GLOBAL_HEADER ? 2 : 0;
  209. if ((err = aacEncoder_SetParam(s->handle, AACENC_SIGNALING_MODE,
  210. s->signaling)) != AACENC_OK) {
  211. av_log(avctx, AV_LOG_ERROR, "Unable to set signaling mode %d: %s\n",
  212. s->signaling, aac_get_error(err));
  213. goto error;
  214. }
  215. if ((err = aacEncoder_SetParam(s->handle, AACENC_AFTERBURNER,
  216. s->afterburner)) != AACENC_OK) {
  217. av_log(avctx, AV_LOG_ERROR, "Unable to set afterburner to %d: %s\n",
  218. s->afterburner, aac_get_error(err));
  219. goto error;
  220. }
  221. if (avctx->cutoff > 0) {
  222. if (avctx->cutoff < (avctx->sample_rate + 255) >> 8) {
  223. av_log(avctx, AV_LOG_ERROR, "cutoff valid range is %d-20000\n",
  224. (avctx->sample_rate + 255) >> 8);
  225. goto error;
  226. }
  227. if ((err = aacEncoder_SetParam(s->handle, AACENC_BANDWIDTH,
  228. avctx->cutoff)) != AACENC_OK) {
  229. av_log(avctx, AV_LOG_ERROR, "Unable to set the encoder bandwith to %d: %s\n",
  230. avctx->cutoff, aac_get_error(err));
  231. goto error;
  232. }
  233. }
  234. if ((err = aacEncEncode(s->handle, NULL, NULL, NULL, NULL)) != AACENC_OK) {
  235. av_log(avctx, AV_LOG_ERROR, "Unable to initialize the encoder: %s\n",
  236. aac_get_error(err));
  237. return AVERROR(EINVAL);
  238. }
  239. if ((err = aacEncInfo(s->handle, &info)) != AACENC_OK) {
  240. av_log(avctx, AV_LOG_ERROR, "Unable to get encoder info: %s\n",
  241. aac_get_error(err));
  242. goto error;
  243. }
  244. #if FF_API_OLD_ENCODE_AUDIO
  245. avctx->coded_frame = avcodec_alloc_frame();
  246. if (!avctx->coded_frame) {
  247. ret = AVERROR(ENOMEM);
  248. goto error;
  249. }
  250. #endif
  251. avctx->frame_size = info.frameLength;
  252. avctx->delay = info.encoderDelay;
  253. ff_af_queue_init(avctx, &s->afq);
  254. if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
  255. avctx->extradata_size = info.confSize;
  256. avctx->extradata = av_mallocz(avctx->extradata_size +
  257. FF_INPUT_BUFFER_PADDING_SIZE);
  258. if (!avctx->extradata) {
  259. ret = AVERROR(ENOMEM);
  260. goto error;
  261. }
  262. memcpy(avctx->extradata, info.confBuf, info.confSize);
  263. }
  264. return 0;
  265. error:
  266. aac_encode_close(avctx);
  267. return ret;
  268. }
  269. static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  270. const AVFrame *frame, int *got_packet_ptr)
  271. {
  272. AACContext *s = avctx->priv_data;
  273. AACENC_BufDesc in_buf = { 0 }, out_buf = { 0 };
  274. AACENC_InArgs in_args = { 0 };
  275. AACENC_OutArgs out_args = { 0 };
  276. int in_buffer_identifier = IN_AUDIO_DATA;
  277. int in_buffer_size, in_buffer_element_size;
  278. int out_buffer_identifier = OUT_BITSTREAM_DATA;
  279. int out_buffer_size, out_buffer_element_size;
  280. void *in_ptr, *out_ptr;
  281. int ret;
  282. AACENC_ERROR err;
  283. /* handle end-of-stream small frame and flushing */
  284. if (!frame) {
  285. in_args.numInSamples = -1;
  286. } else {
  287. in_ptr = frame->data[0];
  288. in_buffer_size = 2 * avctx->channels * frame->nb_samples;
  289. in_buffer_element_size = 2;
  290. in_args.numInSamples = avctx->channels * frame->nb_samples;
  291. in_buf.numBufs = 1;
  292. in_buf.bufs = &in_ptr;
  293. in_buf.bufferIdentifiers = &in_buffer_identifier;
  294. in_buf.bufSizes = &in_buffer_size;
  295. in_buf.bufElSizes = &in_buffer_element_size;
  296. /* add current frame to the queue */
  297. if ((ret = ff_af_queue_add(&s->afq, frame) < 0))
  298. return ret;
  299. }
  300. /* The maximum packet size is 6144 bits aka 768 bytes per channel. */
  301. if ((ret = ff_alloc_packet2(avctx, avpkt, FFMAX(8192, 768 * avctx->channels))))
  302. return ret;
  303. out_ptr = avpkt->data;
  304. out_buffer_size = avpkt->size;
  305. out_buffer_element_size = 1;
  306. out_buf.numBufs = 1;
  307. out_buf.bufs = &out_ptr;
  308. out_buf.bufferIdentifiers = &out_buffer_identifier;
  309. out_buf.bufSizes = &out_buffer_size;
  310. out_buf.bufElSizes = &out_buffer_element_size;
  311. if ((err = aacEncEncode(s->handle, &in_buf, &out_buf, &in_args,
  312. &out_args)) != AACENC_OK) {
  313. if (!frame && err == AACENC_ENCODE_EOF)
  314. return 0;
  315. av_log(avctx, AV_LOG_ERROR, "Unable to encode frame: %s\n",
  316. aac_get_error(err));
  317. return AVERROR(EINVAL);
  318. }
  319. if (!out_args.numOutBytes)
  320. return 0;
  321. /* Get the next frame pts & duration */
  322. ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
  323. &avpkt->duration);
  324. avpkt->size = out_args.numOutBytes;
  325. *got_packet_ptr = 1;
  326. return 0;
  327. }
  328. static const AVProfile profiles[] = {
  329. { FF_PROFILE_AAC_LOW, "LC" },
  330. { FF_PROFILE_AAC_HE, "HE-AAC" },
  331. { FF_PROFILE_AAC_HE_V2, "HE-AACv2" },
  332. { FF_PROFILE_AAC_LD, "LD" },
  333. { FF_PROFILE_AAC_ELD, "ELD" },
  334. { FF_PROFILE_UNKNOWN },
  335. };
  336. static const AVCodecDefault aac_encode_defaults[] = {
  337. { "b", "0" },
  338. { NULL }
  339. };
  340. static const uint64_t aac_channel_layout[] = {
  341. AV_CH_LAYOUT_MONO,
  342. AV_CH_LAYOUT_STEREO,
  343. AV_CH_LAYOUT_SURROUND,
  344. AV_CH_LAYOUT_4POINT0,
  345. AV_CH_LAYOUT_5POINT0_BACK,
  346. AV_CH_LAYOUT_5POINT1_BACK,
  347. 0,
  348. };
  349. AVCodec ff_libfdk_aac_encoder = {
  350. .name = "libfdk_aac",
  351. .type = AVMEDIA_TYPE_AUDIO,
  352. .id = AV_CODEC_ID_AAC,
  353. .priv_data_size = sizeof(AACContext),
  354. .init = aac_encode_init,
  355. .encode2 = aac_encode_frame,
  356. .close = aac_encode_close,
  357. .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
  358. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
  359. AV_SAMPLE_FMT_NONE },
  360. .long_name = NULL_IF_CONFIG_SMALL("Fraunhofer FDK AAC"),
  361. .priv_class = &aac_enc_class,
  362. .defaults = aac_encode_defaults,
  363. .profiles = profiles,
  364. .channel_layouts = aac_channel_layout,
  365. };