FFmpeg
libfdk-aacenc.c
Go to the documentation of this file.
1 /*
2  * AAC encoder wrapper
3  * Copyright (c) 2012 Martin Storsjo
4  *
5  * This file is part of FFmpeg.
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 
20 #include <fdk-aac/aacenc_lib.h>
21 
23 #include "libavutil/common.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/opt.h"
26 #include "avcodec.h"
27 #include "audio_frame_queue.h"
28 #include "codec_internal.h"
29 #include "encode.h"
30 #include "profiles.h"
31 
32 #ifdef AACENCODER_LIB_VL0
33 #define FDKENC_VER_AT_LEAST(vl0, vl1) \
34  ((AACENCODER_LIB_VL0 > vl0) || \
35  (AACENCODER_LIB_VL0 == vl0 && AACENCODER_LIB_VL1 >= vl1))
36 #else
37 #define FDKENC_VER_AT_LEAST(vl0, vl1) 0
38 #endif
39 
40 typedef struct AACContext {
41  const AVClass *class;
42  HANDLE_AACENCODER handle;
44  int eld_sbr;
45  int eld_v2;
46  int signaling;
47  int latm;
49  int vbr;
54  int prog_ref;
56  AACENC_MetaData metaDataSetup;
58 
60 } AACContext;
61 
62 static const AVOption aac_enc_options[] = {
63  { "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 },
64  { "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 },
65 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
66  { "eld_v2", "Enable ELDv2 (LD-MPS extension for ELD stereo signals)", offsetof(AACContext, eld_v2), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
67 #endif
68  { "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" },
69  { "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" },
70  { "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" },
71  { "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" },
72  { "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" },
73  { "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 },
74  { "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 },
75  { "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 },
76  { "drc_profile", "The desired compression profile for AAC DRC", offsetof(AACContext, drc_profile), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 256, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
77  { "drc_target_ref", "Expected target reference level at decoder side in dB (for clipping prevention/limiter)", offsetof(AACContext, drc_target_ref), AV_OPT_TYPE_INT, { .i64 = 0.0 }, -31.75, 0, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
78  { "comp_profile", "The desired compression profile for AAC DRC", offsetof(AACContext, comp_profile), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 256, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
79  { "comp_target_ref", "Expected target reference level at decoder side in dB (for clipping prevention/limiter)", offsetof(AACContext, comp_target_ref), AV_OPT_TYPE_INT, { .i64 = 0.0 }, -31.75, 0, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
80  { "prog_ref", "The program reference level or dialog level in dB", offsetof(AACContext, prog_ref), AV_OPT_TYPE_INT, { .i64 = 0.0 }, -31.75, 0, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
82  { NULL }
83 };
84 
85 static const AVClass aac_enc_class = {
86  .class_name = "libfdk_aac",
87  .item_name = av_default_item_name,
88  .option = aac_enc_options,
89  .version = LIBAVUTIL_VERSION_INT,
90 };
91 
92 static const char *aac_get_error(AACENC_ERROR err)
93 {
94  switch (err) {
95  case AACENC_OK:
96  return "No error";
97  case AACENC_INVALID_HANDLE:
98  return "Invalid handle";
99  case AACENC_MEMORY_ERROR:
100  return "Memory allocation error";
101  case AACENC_UNSUPPORTED_PARAMETER:
102  return "Unsupported parameter";
103  case AACENC_INVALID_CONFIG:
104  return "Invalid config";
105  case AACENC_INIT_ERROR:
106  return "Initialization error";
107  case AACENC_INIT_AAC_ERROR:
108  return "AAC library initialization error";
109  case AACENC_INIT_SBR_ERROR:
110  return "SBR library initialization error";
111  case AACENC_INIT_TP_ERROR:
112  return "Transport library initialization error";
113  case AACENC_INIT_META_ERROR:
114  return "Metadata library initialization error";
115  case AACENC_ENCODE_ERROR:
116  return "Encoding error";
117  case AACENC_ENCODE_EOF:
118  return "End of file";
119  default:
120  return "Unknown error";
121  }
122 }
123 
125 {
126  AACContext *s = avctx->priv_data;
127 
128  if (s->handle)
129  aacEncClose(&s->handle);
130  ff_af_queue_close(&s->afq);
131 
132  return 0;
133 }
134 
136 {
137  AACContext *s = avctx->priv_data;
138  int ret = AVERROR(EINVAL);
139  AACENC_InfoStruct info = { 0 };
140  CHANNEL_MODE mode;
141  AACENC_ERROR err;
142  int aot = FF_PROFILE_AAC_LOW + 1;
143  int sce = 0, cpe = 0;
144 
145  if ((err = aacEncOpen(&s->handle, 0, avctx->ch_layout.nb_channels)) != AACENC_OK) {
146  av_log(avctx, AV_LOG_ERROR, "Unable to open the encoder: %s\n",
147  aac_get_error(err));
148  goto error;
149  }
150 
151  if (avctx->profile != FF_PROFILE_UNKNOWN)
152  aot = avctx->profile + 1;
153 
154  if ((err = aacEncoder_SetParam(s->handle, AACENC_AOT, aot)) != AACENC_OK) {
155  av_log(avctx, AV_LOG_ERROR, "Unable to set the AOT %d: %s\n",
156  aot, aac_get_error(err));
157  goto error;
158  }
159 
160  if (aot == FF_PROFILE_AAC_ELD + 1 && s->eld_sbr) {
161  if ((err = aacEncoder_SetParam(s->handle, AACENC_SBR_MODE,
162  1)) != AACENC_OK) {
163  av_log(avctx, AV_LOG_ERROR, "Unable to enable SBR for ELD: %s\n",
164  aac_get_error(err));
165  goto error;
166  }
167  }
168 
169  if ((err = aacEncoder_SetParam(s->handle, AACENC_SAMPLERATE,
170  avctx->sample_rate)) != AACENC_OK) {
171  av_log(avctx, AV_LOG_ERROR, "Unable to set the sample rate %d: %s\n",
172  avctx->sample_rate, aac_get_error(err));
173  goto error;
174  }
175 
176  switch (avctx->ch_layout.nb_channels) {
177  case 1: mode = MODE_1; sce = 1; cpe = 0; break;
178  case 2:
179 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
180  // (profile + 1) to map from profile range to AOT range
181  if (aot == FF_PROFILE_AAC_ELD + 1 && s->eld_v2) {
182  if ((err = aacEncoder_SetParam(s->handle, AACENC_CHANNELMODE,
183  128)) != AACENC_OK) {
184  av_log(avctx, AV_LOG_ERROR, "Unable to enable ELDv2: %s\n",
185  aac_get_error(err));
186  goto error;
187  } else {
188  mode = MODE_212;
189  sce = 1;
190  cpe = 0;
191  }
192  } else
193 #endif
194  {
195  mode = MODE_2;
196  sce = 0;
197  cpe = 1;
198  }
199  break;
200  case 3: mode = MODE_1_2; sce = 1; cpe = 1; break;
201  case 4: mode = MODE_1_2_1; sce = 2; cpe = 1; break;
202  case 5: mode = MODE_1_2_2; sce = 1; cpe = 2; break;
203  case 6: mode = MODE_1_2_2_1; sce = 2; cpe = 2; break;
204 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
205  case 7: mode = MODE_6_1; sce = 3; cpe = 2; break;
206 #endif
207 /* The version macro is introduced the same time as the 7.1 support, so this
208  should suffice. */
209 #if FDKENC_VER_AT_LEAST(3, 4) // 3.4.12
210  case 8:
211  sce = 2;
212  cpe = 3;
214  mode = MODE_7_1_REAR_SURROUND;
215 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
217  mode = MODE_7_1_TOP_FRONT;
218 #endif
219  } else {
220  // MODE_1_2_2_2_1 and MODE_7_1_FRONT_CENTER use the same channel layout
221  mode = MODE_7_1_FRONT_CENTER;
222  }
223  break;
224 #endif
225  default:
226  av_log(avctx, AV_LOG_ERROR,
227  "Unsupported number of channels %d\n", avctx->ch_layout.nb_channels);
228  goto error;
229  }
230 
231  if ((err = aacEncoder_SetParam(s->handle, AACENC_CHANNELMODE,
232  mode)) != AACENC_OK) {
233  av_log(avctx, AV_LOG_ERROR,
234  "Unable to set channel mode %d: %s\n", mode, aac_get_error(err));
235  goto error;
236  }
237 
238  if ((err = aacEncoder_SetParam(s->handle, AACENC_CHANNELORDER,
239  1)) != AACENC_OK) {
240  av_log(avctx, AV_LOG_ERROR,
241  "Unable to set wav channel order %d: %s\n",
242  mode, aac_get_error(err));
243  goto error;
244  }
245 
246  if (avctx->flags & AV_CODEC_FLAG_QSCALE || s->vbr) {
247  int mode = s->vbr ? s->vbr : avctx->global_quality;
248  if (mode < 1 || mode > 5) {
249  av_log(avctx, AV_LOG_WARNING,
250  "VBR quality %d out of range, should be 1-5\n", mode);
251  mode = av_clip(mode, 1, 5);
252  }
253  av_log(avctx, AV_LOG_WARNING,
254  "Note, the VBR setting is unsupported and only works with "
255  "some parameter combinations\n");
256  if ((err = aacEncoder_SetParam(s->handle, AACENC_BITRATEMODE,
257  mode)) != AACENC_OK) {
258  av_log(avctx, AV_LOG_ERROR, "Unable to set the VBR bitrate mode %d: %s\n",
259  mode, aac_get_error(err));
260  goto error;
261  }
262  } else {
263  if (avctx->bit_rate <= 0) {
264  if (avctx->profile == FF_PROFILE_AAC_HE_V2) {
265  sce = 1;
266  cpe = 0;
267  }
268  avctx->bit_rate = (96*sce + 128*cpe) * avctx->sample_rate / 44;
269  if (avctx->profile == FF_PROFILE_AAC_HE ||
270  avctx->profile == FF_PROFILE_AAC_HE_V2 ||
271  avctx->profile == FF_PROFILE_MPEG2_AAC_HE ||
272  s->eld_sbr)
273  avctx->bit_rate /= 2;
274  }
275  if ((err = aacEncoder_SetParam(s->handle, AACENC_BITRATE,
276  avctx->bit_rate)) != AACENC_OK) {
277  av_log(avctx, AV_LOG_ERROR, "Unable to set the bitrate %"PRId64": %s\n",
278  avctx->bit_rate, aac_get_error(err));
279  goto error;
280  }
281  }
282 
283  /* Choose bitstream format - if global header is requested, use
284  * raw access units, otherwise use ADTS. */
285  if ((err = aacEncoder_SetParam(s->handle, AACENC_TRANSMUX,
286  avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER ? TT_MP4_RAW :
287  s->latm ? TT_MP4_LOAS : TT_MP4_ADTS)) != AACENC_OK) {
288  av_log(avctx, AV_LOG_ERROR, "Unable to set the transmux format: %s\n",
289  aac_get_error(err));
290  goto error;
291  }
292 
293  if (s->latm && s->header_period) {
294  if ((err = aacEncoder_SetParam(s->handle, AACENC_HEADER_PERIOD,
295  s->header_period)) != AACENC_OK) {
296  av_log(avctx, AV_LOG_ERROR, "Unable to set header period: %s\n",
297  aac_get_error(err));
298  goto error;
299  }
300  }
301 
302  /* If no signaling mode is chosen, use explicit hierarchical signaling
303  * if using mp4 mode (raw access units, with global header) and
304  * implicit signaling if using ADTS. */
305  if (s->signaling < 0)
306  s->signaling = avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER ? 2 : 0;
307 
308  if ((err = aacEncoder_SetParam(s->handle, AACENC_SIGNALING_MODE,
309  s->signaling)) != AACENC_OK) {
310  av_log(avctx, AV_LOG_ERROR, "Unable to set signaling mode %d: %s\n",
311  s->signaling, aac_get_error(err));
312  goto error;
313  }
314 
315  if ((err = aacEncoder_SetParam(s->handle, AACENC_AFTERBURNER,
316  s->afterburner)) != AACENC_OK) {
317  av_log(avctx, AV_LOG_ERROR, "Unable to set afterburner to %d: %s\n",
318  s->afterburner, aac_get_error(err));
319  goto error;
320  }
321 
322  if (avctx->cutoff > 0) {
323  if (avctx->cutoff < (avctx->sample_rate + 255) >> 8 || avctx->cutoff > 20000) {
324  av_log(avctx, AV_LOG_ERROR, "cutoff valid range is %d-20000\n",
325  (avctx->sample_rate + 255) >> 8);
326  goto error;
327  }
328  if ((err = aacEncoder_SetParam(s->handle, AACENC_BANDWIDTH,
329  avctx->cutoff)) != AACENC_OK) {
330  av_log(avctx, AV_LOG_ERROR, "Unable to set the encoder bandwidth to %d: %s\n",
331  avctx->cutoff, aac_get_error(err));
332  goto error;
333  }
334  }
335 
336  s->metadata_mode = 0;
337  if (s->prog_ref) {
338  s->metadata_mode = 1;
339  s->metaDataSetup.prog_ref_level_present = 1;
340  s->metaDataSetup.prog_ref_level = s->prog_ref << 16;
341  }
342  if (s->drc_profile) {
343  s->metadata_mode = 1;
344  s->metaDataSetup.drc_profile = s->drc_profile;
345  s->metaDataSetup.drc_TargetRefLevel = s->drc_target_ref << 16;
346  if (s->comp_profile) {
347  /* Including the comp_profile means that we need to set the mode to ETSI */
348  s->metadata_mode = 2;
349  s->metaDataSetup.comp_profile = s->comp_profile;
350  s->metaDataSetup.comp_TargetRefLevel = s->comp_target_ref << 16;
351  }
352  }
353 
354  if ((err = aacEncoder_SetParam(s->handle, AACENC_METADATA_MODE, s->metadata_mode)) != AACENC_OK) {
355  av_log(avctx, AV_LOG_ERROR, "Unable to set metadata mode to %d: %s\n",
356  s->metadata_mode, aac_get_error(err));
357  goto error;
358  }
359 
360  if ((err = aacEncEncode(s->handle, NULL, NULL, NULL, NULL)) != AACENC_OK) {
361  av_log(avctx, AV_LOG_ERROR, "Unable to initialize the encoder: %s\n",
362  aac_get_error(err));
363  return AVERROR(EINVAL);
364  }
365 
366  if ((err = aacEncInfo(s->handle, &info)) != AACENC_OK) {
367  av_log(avctx, AV_LOG_ERROR, "Unable to get encoder info: %s\n",
368  aac_get_error(err));
369  goto error;
370  }
371 
372  avctx->frame_size = info.frameLength;
373 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
374  avctx->initial_padding = info.nDelay;
375 #else
376  avctx->initial_padding = info.encoderDelay;
377 #endif
378  ff_af_queue_init(avctx, &s->afq);
379 
380  if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
381  avctx->extradata_size = info.confSize;
382  avctx->extradata = av_mallocz(avctx->extradata_size +
384  if (!avctx->extradata) {
385  ret = AVERROR(ENOMEM);
386  goto error;
387  }
388 
389  memcpy(avctx->extradata, info.confBuf, info.confSize);
390  }
391  return 0;
392 error:
393  aac_encode_close(avctx);
394  return ret;
395 }
396 
397 static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
398  const AVFrame *frame, int *got_packet_ptr)
399 {
400  AACContext *s = avctx->priv_data;
401  AACENC_BufDesc in_buf = { 0 }, out_buf = { 0 };
402  AACENC_InArgs in_args = { 0 };
403  AACENC_OutArgs out_args = { 0 };
404  void* inBuffer[] = { 0, &s->metaDataSetup };
405  int in_buffer_identifiers[] = { IN_AUDIO_DATA, IN_METADATA_SETUP };
406  int in_buffer_element_sizes[] = { 2, sizeof(AACENC_MetaData) };
407  int in_buffer_sizes[] = { 0, sizeof(s->metaDataSetup) };
408  int out_buffer_identifier = OUT_BITSTREAM_DATA;
409  int out_buffer_size, out_buffer_element_size;
410  void *out_ptr;
411  int ret, discard_padding;
412  uint8_t dummy_buf[1];
413  AACENC_ERROR err;
414 
415  /* handle end-of-stream small frame and flushing */
416  if (!frame) {
417  /* Must be a non-null pointer, even if it's a dummy. We could use
418  * the address of anything else on the stack as well. */
419  inBuffer[0] = dummy_buf;
420 
421  in_args.numInSamples = -1;
422  } else {
423  inBuffer[0] = frame->data[0];
424  in_buffer_sizes[0] = 2 * avctx->ch_layout.nb_channels * frame->nb_samples;
425 
426  in_args.numInSamples = avctx->ch_layout.nb_channels * frame->nb_samples;
427 
428  /* add current frame to the queue */
429  if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
430  return ret;
431  }
432 
433  if (s->metadata_mode == 0) {
434  in_buf.numBufs = 1;
435  } else {
436  in_buf.numBufs = 2;
437  }
438 
439  in_buf.bufs = (void**)inBuffer;
440  in_buf.bufferIdentifiers = in_buffer_identifiers;
441  in_buf.bufSizes = in_buffer_sizes;
442  in_buf.bufElSizes = in_buffer_element_sizes;
443 
444  /* The maximum packet size is 6144 bits aka 768 bytes per channel. */
445  ret = ff_alloc_packet(avctx, avpkt, FFMAX(8192, 768 * avctx->ch_layout.nb_channels));
446  if (ret < 0)
447  return ret;
448 
449  out_ptr = avpkt->data;
450  out_buffer_size = avpkt->size;
451  out_buffer_element_size = 1;
452  out_buf.numBufs = 1;
453  out_buf.bufs = &out_ptr;
454  out_buf.bufferIdentifiers = &out_buffer_identifier;
455  out_buf.bufSizes = &out_buffer_size;
456  out_buf.bufElSizes = &out_buffer_element_size;
457 
458  if ((err = aacEncEncode(s->handle, &in_buf, &out_buf, &in_args,
459  &out_args)) != AACENC_OK) {
460  if (!frame && err == AACENC_ENCODE_EOF)
461  return 0;
462  av_log(avctx, AV_LOG_ERROR, "Unable to encode frame: %s\n",
463  aac_get_error(err));
464  return AVERROR(EINVAL);
465  }
466 
467  if (!out_args.numOutBytes)
468  return 0;
469 
470  /* Get the next frame pts & duration */
471  ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
472  &avpkt->duration);
473 
474  discard_padding = avctx->frame_size - avpkt->duration;
475  // Check if subtraction resulted in an overflow
476  if ((discard_padding < avctx->frame_size) != (avpkt->duration > 0)) {
477  av_log(avctx, AV_LOG_ERROR, "discard padding overflow\n");
478  return AVERROR(EINVAL);
479  }
480  if ((!s->delay_sent && avctx->initial_padding > 0) || discard_padding > 0) {
481  uint8_t *side_data =
483  if (!side_data)
484  return AVERROR(ENOMEM);
485  if (!s->delay_sent) {
486  AV_WL32(side_data, avctx->initial_padding);
487  s->delay_sent = 1;
488  }
489  AV_WL32(side_data + 4, discard_padding);
490  }
491 
492  avpkt->size = out_args.numOutBytes;
493  *got_packet_ptr = 1;
494  return 0;
495 }
496 
497 static const AVProfile profiles[] = {
498  { FF_PROFILE_AAC_LOW, "LC" },
499  { FF_PROFILE_AAC_HE, "HE-AAC" },
500  { FF_PROFILE_AAC_HE_V2, "HE-AACv2" },
501  { FF_PROFILE_AAC_LD, "LD" },
502  { FF_PROFILE_AAC_ELD, "ELD" },
503  { FF_PROFILE_UNKNOWN },
504 };
505 
507  { "b", "0" },
508  { NULL }
509 };
510 
511 #if FF_API_OLD_CHANNEL_LAYOUT
512 static const uint64_t aac_channel_layout[] = {
519 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
521 #endif
522 #if FDKENC_VER_AT_LEAST(3, 4) // 3.4.12
525 #endif
526 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
528 #endif
529  0,
530 };
531 #endif /* FF_API_OLD_CHANNEL_LAYOUT */
532 
533 static const AVChannelLayout aac_ch_layouts[16] = {
540 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
542 #endif
543 #if FDKENC_VER_AT_LEAST(3, 4) // 3.4.12
546 #endif
547 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
549 #endif
550  { 0 },
551 };
552 
553 static const int aac_sample_rates[] = {
554  96000, 88200, 64000, 48000, 44100, 32000,
555  24000, 22050, 16000, 12000, 11025, 8000, 0
556 };
557 
559  .p.name = "libfdk_aac",
560  CODEC_LONG_NAME("Fraunhofer FDK AAC"),
561  .p.type = AVMEDIA_TYPE_AUDIO,
562  .p.id = AV_CODEC_ID_AAC,
563  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
565  .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE,
566  .priv_data_size = sizeof(AACContext),
569  .close = aac_encode_close,
570  .p.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
572  .p.priv_class = &aac_enc_class,
573  .defaults = aac_encode_defaults,
574  .p.profiles = profiles,
575  .p.supported_samplerates = aac_sample_rates,
576  .p.wrapper_name = "libfdk",
577  CODEC_OLD_CHANNEL_LAYOUTS_ARRAY(aac_channel_layout)
578  .p.ch_layouts = aac_ch_layouts,
579 };
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:31
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1066
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AV_CH_LAYOUT_7POINT1_WIDE_BACK
#define AV_CH_LAYOUT_7POINT1_WIDE_BACK
Definition: channel_layout.h:234
AV_CH_LAYOUT_5POINT0_BACK
#define AV_CH_LAYOUT_5POINT0_BACK
Definition: channel_layout.h:222
av_clip
#define av_clip
Definition: common.h:95
aac_ch_layouts
static const AVChannelLayout aac_ch_layouts[16]
Definition: libfdk-aacenc.c:533
AACContext::metadata_mode
int metadata_mode
Definition: libfdk-aacenc.c:55
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
FF_PROFILE_MPEG2_AAC_HE
#define FF_PROFILE_MPEG2_AAC_HE
Definition: avcodec.h:1582
aac_enc_class
static const AVClass aac_enc_class
Definition: libfdk-aacenc.c:85
ff_libfdk_aac_encoder
const FFCodec ff_libfdk_aac_encoder
Definition: libfdk-aacenc.c:558
AV_WL32
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
ff_af_queue_remove
void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts, int64_t *duration)
Remove frame(s) from the queue.
Definition: audio_frame_queue.c:75
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:369
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1038
ff_af_queue_close
void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
Definition: audio_frame_queue.c:36
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:210
AV_CODEC_FLAG_QSCALE
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
Definition: avcodec.h:216
ff_af_queue_init
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
Definition: audio_frame_queue.c:28
aac_encode_frame
static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: libfdk-aacenc.c:397
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:330
aac_encode_init
static av_cold int aac_encode_init(AVCodecContext *avctx)
Definition: libfdk-aacenc.c:135
AVPacket::data
uint8_t * data
Definition: packet.h:374
AACContext::signaling
int signaling
Definition: libfdk-aacenc.c:46
AVOption
AVOption.
Definition: opt.h:251
encode.h
FF_CODEC_CAP_NOT_INIT_THREADSAFE
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
Definition: codec_internal.h:34
FFCodec
Definition: codec_internal.h:127
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:392
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:311
AV_CODEC_FLAG_GLOBAL_HEADER
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:317
AVProfile
AVProfile.
Definition: codec.h:176
FFCodecDefault
Definition: codec_internal.h:97
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2066
aac_get_error
static const char * aac_get_error(AACENC_ERROR err)
Definition: libfdk-aacenc.c:92
audio_frame_queue.h
AVCodecContext::initial_padding
int initial_padding
Audio only.
Definition: avcodec.h:1753
AV_CHANNEL_LAYOUT_7POINT1_TOP_BACK
#define AV_CHANNEL_LAYOUT_7POINT1_TOP_BACK
Definition: channel_layout.h:393
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:506
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:315
ff_af_queue_add
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
Definition: audio_frame_queue.c:44
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:211
AV_CHANNEL_LAYOUT_SURROUND
#define AV_CHANNEL_LAYOUT_SURROUND
Definition: channel_layout.h:372
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
FF_PROFILE_AAC_HE_V2
#define FF_PROFILE_AAC_HE_V2
Definition: avcodec.h:1578
AV_CHANNEL_LAYOUT_4POINT0
#define AV_CHANNEL_LAYOUT_4POINT0
Definition: channel_layout.h:374
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:528
AV_CHANNEL_LAYOUT_7POINT1
#define AV_CHANNEL_LAYOUT_7POINT1
Definition: channel_layout.h:390
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:256
AVCodecContext::global_quality
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:492
AV_OPT_FLAG_ENCODING_PARAM
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:281
frame_size
int frame_size
Definition: mxfenc.c:2205
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
info
MIPS optimizations info
Definition: mips.txt:2
AV_CHANNEL_LAYOUT_5POINT0_BACK
#define AV_CHANNEL_LAYOUT_5POINT0_BACK
Definition: channel_layout.h:380
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts_bsf.c:365
AACContext::prog_ref
int prog_ref
Definition: libfdk-aacenc.c:54
AACContext::eld_v2
int eld_v2
Definition: libfdk-aacenc.c:45
FF_PROFILE_UNKNOWN
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:1570
AudioFrameQueue
Definition: audio_frame_queue.h:32
AACContext::vbr
int vbr
Definition: libfdk-aacenc.c:49
AACContext::header_period
int header_period
Definition: libfdk-aacenc.c:48
AACContext::latm
int latm
Definition: libfdk-aacenc.c:47
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
AV_OPT_FLAG_AUDIO_PARAM
#define AV_OPT_FLAG_AUDIO_PARAM
Definition: opt.h:283
AACContext::comp_target_ref
int comp_target_ref
Definition: libfdk-aacenc.c:53
if
if(ret)
Definition: filter_design.txt:179
AV_CHANNEL_LAYOUT_7POINT1_WIDE_BACK
#define AV_CHANNEL_LAYOUT_7POINT1_WIDE_BACK
Definition: channel_layout.h:392
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
AACContext::comp_profile
int comp_profile
Definition: libfdk-aacenc.c:52
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:476
FF_PROFILE_AAC_LD
#define FF_PROFILE_AAC_LD
Definition: avcodec.h:1579
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
profiles.h
AACContext::metaDataSetup
AACENC_MetaData metaDataSetup
Definition: libfdk-aacenc.c:56
aac_encode_close
static int aac_encode_close(AVCodecContext *avctx)
Definition: libfdk-aacenc.c:124
AACContext::afterburner
int afterburner
Definition: libfdk-aacenc.c:43
FF_PROFILE_AAC_ELD
#define FF_PROFILE_AAC_ELD
Definition: avcodec.h:1580
aac_encode_defaults
static const FFCodecDefault aac_encode_defaults[]
Definition: libfdk-aacenc.c:506
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:440
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:375
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:301
codec_internal.h
FF_PROFILE_AAC_LOW
#define FF_PROFILE_AAC_LOW
Definition: avcodec.h:1574
AACContext::afq
AudioFrameQueue afq
Definition: libfdk-aacenc.c:59
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
AV_CH_LAYOUT_5POINT1_BACK
#define AV_CH_LAYOUT_5POINT1_BACK
Definition: channel_layout.h:223
av_channel_layout_compare
int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1)
Check whether two channel layouts are semantically the same, i.e.
Definition: channel_layout.c:932
AV_CHANNEL_LAYOUT_6POINT1_BACK
#define AV_CHANNEL_LAYOUT_6POINT1_BACK
Definition: channel_layout.h:386
AACContext::eld_sbr
int eld_sbr
Definition: libfdk-aacenc.c:44
CODEC_OLD_CHANNEL_LAYOUTS_ARRAY
#define CODEC_OLD_CHANNEL_LAYOUTS_ARRAY(array)
Definition: codec_internal.h:303
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:367
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:527
common.h
AVCodecContext::cutoff
int cutoff
Audio cutoff bandwidth (0 means "automatic")
Definition: avcodec.h:1094
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
AV_CH_LAYOUT_7POINT1
#define AV_CH_LAYOUT_7POINT1
Definition: channel_layout.h:232
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:254
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:191
AV_PKT_DATA_SKIP_SAMPLES
@ AV_PKT_DATA_SKIP_SAMPLES
Recommmends skipping the specified number of samples.
Definition: packet.h:157
avcodec.h
FF_PROFILE_AAC_HE
#define FF_PROFILE_AAC_HE
Definition: avcodec.h:1577
ret
ret
Definition: filter_design.txt:187
AACContext::drc_target_ref
int drc_target_ref
Definition: libfdk-aacenc.c:51
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AV_CH_LAYOUT_SURROUND
#define AV_CH_LAYOUT_SURROUND
Definition: channel_layout.h:214
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
AVCodecContext
main external API structure.
Definition: avcodec.h:426
channel_layout.h
av_packet_new_side_data
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
Definition: avpacket.c:230
mode
mode
Definition: ebur128.h:83
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1569
profiles
static const AVProfile profiles[]
Definition: libfdk-aacenc.c:497
AV_CODEC_CAP_DELAY
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:76
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:368
FF_AAC_PROFILE_OPTS
#define FF_AAC_PROFILE_OPTS
Definition: profiles.h:28
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:453
aac_sample_rates
static const int aac_sample_rates[]
Definition: libfdk-aacenc.c:553
AV_CH_LAYOUT_4POINT0
#define AV_CH_LAYOUT_4POINT0
Definition: channel_layout.h:216
AACContext
main AAC context
Definition: aac.h:296
AV_CHANNEL_LAYOUT_5POINT1_BACK
#define AV_CHANNEL_LAYOUT_5POINT1_BACK
Definition: channel_layout.h:381
AV_CH_LAYOUT_6POINT1_BACK
#define AV_CH_LAYOUT_6POINT1_BACK
Definition: channel_layout.h:228
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AV_CH_LAYOUT_7POINT1_TOP_BACK
#define AV_CH_LAYOUT_7POINT1_TOP_BACK
Definition: channel_layout.h:235
AACContext::handle
HANDLE_AACENCODER handle
Definition: libfdk-aacenc.c:42
AV_CODEC_CAP_SMALL_LAST_FRAME
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition: codec.h:81
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
AACContext::delay_sent
int delay_sent
Definition: libfdk-aacenc.c:57
ff_alloc_packet
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition: encode.c:35
aac_enc_options
static const AVOption aac_enc_options[]
Definition: libfdk-aacenc.c:62
AACContext::drc_profile
int drc_profile
Definition: libfdk-aacenc.c:50