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;
59 
61 } AACContext;
62 
63 static const AVOption aac_enc_options[] = {
64  { "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 },
65  { "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 },
66 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
67  { "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 },
68 #endif
69  { "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, .unit = "signaling" },
70  { "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, .unit = "signaling" },
71  { "implicit", "Implicit backwards compatible signaling", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, .unit = "signaling" },
72  { "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, .unit = "signaling" },
73  { "explicit_hierarchical", "Explicit hierarchical signaling", 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, 0, 0, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, .unit = "signaling" },
74  { "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 },
75  { "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 },
76  { "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 },
77  { "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 },
78  { "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 },
79  { "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 },
80  { "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 },
81  { "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  { "frame_length", "The desired frame length", offsetof(AACContext, frame_length), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1024, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
84  { NULL }
85 };
86 
87 static const AVClass aac_enc_class = {
88  .class_name = "libfdk_aac",
89  .item_name = av_default_item_name,
90  .option = aac_enc_options,
91  .version = LIBAVUTIL_VERSION_INT,
92 };
93 
94 static const char *aac_get_error(AACENC_ERROR err)
95 {
96  switch (err) {
97  case AACENC_OK:
98  return "No error";
99  case AACENC_INVALID_HANDLE:
100  return "Invalid handle";
101  case AACENC_MEMORY_ERROR:
102  return "Memory allocation error";
103  case AACENC_UNSUPPORTED_PARAMETER:
104  return "Unsupported parameter";
105  case AACENC_INVALID_CONFIG:
106  return "Invalid config";
107  case AACENC_INIT_ERROR:
108  return "Initialization error";
109  case AACENC_INIT_AAC_ERROR:
110  return "AAC library initialization error";
111  case AACENC_INIT_SBR_ERROR:
112  return "SBR library initialization error";
113  case AACENC_INIT_TP_ERROR:
114  return "Transport library initialization error";
115  case AACENC_INIT_META_ERROR:
116  return "Metadata library initialization error";
117  case AACENC_ENCODE_ERROR:
118  return "Encoding error";
119  case AACENC_ENCODE_EOF:
120  return "End of file";
121  default:
122  return "Unknown error";
123  }
124 }
125 
127 {
128  AACContext *s = avctx->priv_data;
129 
130  if (s->handle)
131  aacEncClose(&s->handle);
132  ff_af_queue_close(&s->afq);
133 
134  return 0;
135 }
136 
137 static void aac_encode_flush(AVCodecContext *avctx)
138 {
139  AACContext *s = avctx->priv_data;
140  AACENC_BufDesc in_buf = { 0 }, out_buf = { 0 };
141  AACENC_InArgs in_args = { 0 };
142  AACENC_OutArgs out_args;
143  int64_t pts, duration;
144  uint8_t dummy_in[1], dummy_out[1];
145  int in_buffer_identifiers[] = { IN_AUDIO_DATA, IN_METADATA_SETUP };
146  int in_buffer_element_sizes[] = { 2, sizeof(AACENC_MetaData) };
147  int in_buffer_sizes[] = { 0, sizeof(s->metaDataSetup) };
148  int out_buffer_identifier = OUT_BITSTREAM_DATA;
149  int out_buffer_size = sizeof(dummy_out), out_buffer_element_size = 1;
150  void* inBuffer[] = { dummy_in, &s->metaDataSetup };
151  void *out_ptr = dummy_out;
152  AACENC_ERROR err;
153 
154  ff_af_queue_remove(&s->afq, s->afq.frame_count, &pts, &duration);
155 
156  in_buf.bufs = (void **)inBuffer;
157  in_buf.numBufs = s->metadata_mode == 0 ? 1 : 2;
158  in_buf.bufferIdentifiers = in_buffer_identifiers;
159  in_buf.bufSizes = in_buffer_sizes;
160  in_buf.bufElSizes = in_buffer_element_sizes;
161 
162  out_buf.numBufs = 1;
163  out_buf.bufs = &out_ptr;
164  out_buf.bufferIdentifiers = &out_buffer_identifier;
165  out_buf.bufSizes = &out_buffer_size;
166  out_buf.bufElSizes = &out_buffer_element_size;
167 
168  err = aacEncEncode(s->handle, &in_buf, &out_buf, &in_args, &out_args);
169  if (err != AACENC_OK) {
170  av_log(avctx, AV_LOG_ERROR, "Unexpected error while flushing: %s\n",
171  aac_get_error(err));
172  }
173 }
174 
176 {
177  AACContext *s = avctx->priv_data;
178  int ret = AVERROR(EINVAL);
179  AACENC_InfoStruct info = { 0 };
180  CHANNEL_MODE mode;
181  AACENC_ERROR err;
182  int aot = AV_PROFILE_AAC_LOW + 1;
183  int sce = 0, cpe = 0;
184 
185  if ((err = aacEncOpen(&s->handle, 0, avctx->ch_layout.nb_channels)) != AACENC_OK) {
186  av_log(avctx, AV_LOG_ERROR, "Unable to open the encoder: %s\n",
187  aac_get_error(err));
188  goto error;
189  }
190 
191  if (avctx->profile != AV_PROFILE_UNKNOWN)
192  aot = avctx->profile + 1;
193 
194  if ((err = aacEncoder_SetParam(s->handle, AACENC_AOT, aot)) != AACENC_OK) {
195  av_log(avctx, AV_LOG_ERROR, "Unable to set the AOT %d: %s\n",
196  aot, aac_get_error(err));
197  goto error;
198  }
199 
200  if (aot == AV_PROFILE_AAC_ELD + 1 && s->eld_sbr) {
201  if ((err = aacEncoder_SetParam(s->handle, AACENC_SBR_MODE,
202  1)) != AACENC_OK) {
203  av_log(avctx, AV_LOG_ERROR, "Unable to enable SBR for ELD: %s\n",
204  aac_get_error(err));
205  goto error;
206  }
207  }
208 
209  if (s->frame_length >= 0) {
210  if ((err = aacEncoder_SetParam(s->handle, AACENC_GRANULE_LENGTH,
211  s->frame_length)) != AACENC_OK) {
212  av_log(avctx, AV_LOG_ERROR, "Unable to set granule length: %s\n",
213  aac_get_error(err));
214  goto error;
215  }
216  }
217 
218  if ((err = aacEncoder_SetParam(s->handle, AACENC_SAMPLERATE,
219  avctx->sample_rate)) != AACENC_OK) {
220  av_log(avctx, AV_LOG_ERROR, "Unable to set the sample rate %d: %s\n",
221  avctx->sample_rate, aac_get_error(err));
222  goto error;
223  }
224 
225  switch (avctx->ch_layout.nb_channels) {
226  case 1: mode = MODE_1; sce = 1; cpe = 0; break;
227  case 2:
228 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
229  // (profile + 1) to map from profile range to AOT range
230  if (aot == AV_PROFILE_AAC_ELD + 1 && s->eld_v2) {
231  if ((err = aacEncoder_SetParam(s->handle, AACENC_CHANNELMODE,
232  128)) != AACENC_OK) {
233  av_log(avctx, AV_LOG_ERROR, "Unable to enable ELDv2: %s\n",
234  aac_get_error(err));
235  goto error;
236  } else {
237  mode = MODE_212;
238  sce = 1;
239  cpe = 0;
240  }
241  } else
242 #endif
243  {
244  mode = MODE_2;
245  sce = 0;
246  cpe = 1;
247  }
248  break;
249  case 3: mode = MODE_1_2; sce = 1; cpe = 1; break;
250  case 4: mode = MODE_1_2_1; sce = 2; cpe = 1; break;
251  case 5: mode = MODE_1_2_2; sce = 1; cpe = 2; break;
252  case 6: mode = MODE_1_2_2_1; sce = 2; cpe = 2; break;
253 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
254  case 7: mode = MODE_6_1; sce = 3; cpe = 2; break;
255 #endif
256 /* The version macro is introduced the same time as the 7.1 support, so this
257  should suffice. */
258 #if FDKENC_VER_AT_LEAST(3, 4) // 3.4.12
259  case 8:
260  sce = 2;
261  cpe = 3;
263  mode = MODE_7_1_REAR_SURROUND;
264 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
266  mode = MODE_7_1_TOP_FRONT;
267 #endif
268  } else {
269  // MODE_1_2_2_2_1 and MODE_7_1_FRONT_CENTER use the same channel layout
270  mode = MODE_7_1_FRONT_CENTER;
271  }
272  break;
273 #endif
274  default:
275  av_log(avctx, AV_LOG_ERROR,
276  "Unsupported number of channels %d\n", avctx->ch_layout.nb_channels);
277  goto error;
278  }
279 
280  if ((err = aacEncoder_SetParam(s->handle, AACENC_CHANNELMODE,
281  mode)) != AACENC_OK) {
282  av_log(avctx, AV_LOG_ERROR,
283  "Unable to set channel mode %d: %s\n", mode, aac_get_error(err));
284  goto error;
285  }
286 
287  if ((err = aacEncoder_SetParam(s->handle, AACENC_CHANNELORDER,
288  1)) != AACENC_OK) {
289  av_log(avctx, AV_LOG_ERROR,
290  "Unable to set wav channel order %d: %s\n",
291  mode, aac_get_error(err));
292  goto error;
293  }
294 
295  if (avctx->flags & AV_CODEC_FLAG_QSCALE || s->vbr) {
296  int mode = s->vbr ? s->vbr : avctx->global_quality;
297  if (mode < 1 || mode > 5) {
298  av_log(avctx, AV_LOG_WARNING,
299  "VBR quality %d out of range, should be 1-5\n", mode);
300  mode = av_clip(mode, 1, 5);
301  }
302  av_log(avctx, AV_LOG_WARNING,
303  "Note, the VBR setting is unsupported and only works with "
304  "some parameter combinations\n");
305  if ((err = aacEncoder_SetParam(s->handle, AACENC_BITRATEMODE,
306  mode)) != AACENC_OK) {
307  av_log(avctx, AV_LOG_ERROR, "Unable to set the VBR bitrate mode %d: %s\n",
308  mode, aac_get_error(err));
309  goto error;
310  }
311  } else {
312  if (avctx->bit_rate <= 0) {
313  if (avctx->profile == AV_PROFILE_AAC_HE_V2) {
314  sce = 1;
315  cpe = 0;
316  }
317  avctx->bit_rate = (96*sce + 128*cpe) * avctx->sample_rate / 44;
318  if (avctx->profile == AV_PROFILE_AAC_HE ||
319  avctx->profile == AV_PROFILE_AAC_HE_V2 ||
320  avctx->profile == AV_PROFILE_MPEG2_AAC_HE ||
321  s->eld_sbr)
322  avctx->bit_rate /= 2;
323  }
324  if ((err = aacEncoder_SetParam(s->handle, AACENC_BITRATE,
325  avctx->bit_rate)) != AACENC_OK) {
326  av_log(avctx, AV_LOG_ERROR, "Unable to set the bitrate %"PRId64": %s\n",
327  avctx->bit_rate, aac_get_error(err));
328  goto error;
329  }
330  }
331 
332  /* Choose bitstream format - if global header is requested, use
333  * raw access units, otherwise use ADTS. */
334  if ((err = aacEncoder_SetParam(s->handle, AACENC_TRANSMUX,
335  avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER ? TT_MP4_RAW :
336  s->latm ? TT_MP4_LOAS : TT_MP4_ADTS)) != AACENC_OK) {
337  av_log(avctx, AV_LOG_ERROR, "Unable to set the transmux format: %s\n",
338  aac_get_error(err));
339  goto error;
340  }
341 
342  if (s->latm && s->header_period) {
343  if ((err = aacEncoder_SetParam(s->handle, AACENC_HEADER_PERIOD,
344  s->header_period)) != AACENC_OK) {
345  av_log(avctx, AV_LOG_ERROR, "Unable to set header period: %s\n",
346  aac_get_error(err));
347  goto error;
348  }
349  }
350 
351  /* If no signaling mode is chosen, use explicit hierarchical signaling
352  * if using mp4 mode (raw access units, with global header) and
353  * implicit signaling if using ADTS. */
354  if (s->signaling < 0)
355  s->signaling = avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER ? 2 : 0;
356 
357  if ((err = aacEncoder_SetParam(s->handle, AACENC_SIGNALING_MODE,
358  s->signaling)) != AACENC_OK) {
359  av_log(avctx, AV_LOG_ERROR, "Unable to set signaling mode %d: %s\n",
360  s->signaling, aac_get_error(err));
361  goto error;
362  }
363 
364  if ((err = aacEncoder_SetParam(s->handle, AACENC_AFTERBURNER,
365  s->afterburner)) != AACENC_OK) {
366  av_log(avctx, AV_LOG_ERROR, "Unable to set afterburner to %d: %s\n",
367  s->afterburner, aac_get_error(err));
368  goto error;
369  }
370 
371  if (avctx->cutoff > 0) {
372  if (avctx->cutoff < (avctx->sample_rate + 255) >> 8 || avctx->cutoff > 20000) {
373  av_log(avctx, AV_LOG_ERROR, "cutoff valid range is %d-20000\n",
374  (avctx->sample_rate + 255) >> 8);
375  goto error;
376  }
377  if ((err = aacEncoder_SetParam(s->handle, AACENC_BANDWIDTH,
378  avctx->cutoff)) != AACENC_OK) {
379  av_log(avctx, AV_LOG_ERROR, "Unable to set the encoder bandwidth to %d: %s\n",
380  avctx->cutoff, aac_get_error(err));
381  goto error;
382  }
383  }
384 
385  s->metadata_mode = 0;
386  if (s->prog_ref) {
387  s->metadata_mode = 1;
388  s->metaDataSetup.prog_ref_level_present = 1;
389  s->metaDataSetup.prog_ref_level = s->prog_ref << 16;
390  }
391  if (s->drc_profile) {
392  s->metadata_mode = 1;
393  s->metaDataSetup.drc_profile = s->drc_profile;
394  s->metaDataSetup.drc_TargetRefLevel = s->drc_target_ref << 16;
395  if (s->comp_profile) {
396  /* Including the comp_profile means that we need to set the mode to ETSI */
397  s->metadata_mode = 2;
398  s->metaDataSetup.comp_profile = s->comp_profile;
399  s->metaDataSetup.comp_TargetRefLevel = s->comp_target_ref << 16;
400  }
401  }
402 
403  if ((err = aacEncoder_SetParam(s->handle, AACENC_METADATA_MODE, s->metadata_mode)) != AACENC_OK) {
404  av_log(avctx, AV_LOG_ERROR, "Unable to set metadata mode to %d: %s\n",
405  s->metadata_mode, aac_get_error(err));
406  goto error;
407  }
408 
409  if ((err = aacEncEncode(s->handle, NULL, NULL, NULL, NULL)) != AACENC_OK) {
410  av_log(avctx, AV_LOG_ERROR, "Unable to initialize the encoder: %s\n",
411  aac_get_error(err));
412  return AVERROR(EINVAL);
413  }
414 
415  if ((err = aacEncInfo(s->handle, &info)) != AACENC_OK) {
416  av_log(avctx, AV_LOG_ERROR, "Unable to get encoder info: %s\n",
417  aac_get_error(err));
418  goto error;
419  }
420 
421  avctx->frame_size = info.frameLength;
422 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
423  avctx->initial_padding = info.nDelay;
424 #else
425  avctx->initial_padding = info.encoderDelay;
426 #endif
427  ff_af_queue_init(avctx, &s->afq);
428 
429  if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
430  avctx->extradata_size = info.confSize;
431  avctx->extradata = av_mallocz(avctx->extradata_size +
433  if (!avctx->extradata) {
434  ret = AVERROR(ENOMEM);
435  goto error;
436  }
437 
438  memcpy(avctx->extradata, info.confBuf, info.confSize);
439  }
440  return 0;
441 error:
442  aac_encode_close(avctx);
443  return ret;
444 }
445 
446 static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
447  const AVFrame *frame, int *got_packet_ptr)
448 {
449  AACContext *s = avctx->priv_data;
450  AACENC_BufDesc in_buf = { 0 }, out_buf = { 0 };
451  AACENC_InArgs in_args = { 0 };
452  AACENC_OutArgs out_args = { 0 };
453  void* inBuffer[] = { 0, &s->metaDataSetup };
454  int in_buffer_identifiers[] = { IN_AUDIO_DATA, IN_METADATA_SETUP };
455  int in_buffer_element_sizes[] = { 2, sizeof(AACENC_MetaData) };
456  int in_buffer_sizes[] = { 0, sizeof(s->metaDataSetup) };
457  int out_buffer_identifier = OUT_BITSTREAM_DATA;
458  int out_buffer_size, out_buffer_element_size;
459  void *out_ptr;
460  int ret, discard_padding;
461  uint8_t dummy_buf[1];
462  AACENC_ERROR err;
463 
464  /* handle end-of-stream small frame and flushing */
465  if (!frame) {
466  /* Must be a non-null pointer, even if it's a dummy. We could use
467  * the address of anything else on the stack as well. */
468  inBuffer[0] = dummy_buf;
469 
470  in_args.numInSamples = -1;
471  } else {
472  inBuffer[0] = frame->data[0];
473  in_buffer_sizes[0] = 2 * avctx->ch_layout.nb_channels * frame->nb_samples;
474 
475  in_args.numInSamples = avctx->ch_layout.nb_channels * frame->nb_samples;
476 
477  /* add current frame to the queue */
478  if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
479  return ret;
480  }
481 
482  if (s->metadata_mode == 0) {
483  in_buf.numBufs = 1;
484  } else {
485  in_buf.numBufs = 2;
486  }
487 
488  in_buf.bufs = (void**)inBuffer;
489  in_buf.bufferIdentifiers = in_buffer_identifiers;
490  in_buf.bufSizes = in_buffer_sizes;
491  in_buf.bufElSizes = in_buffer_element_sizes;
492 
493  /* The maximum packet size is 6144 bits aka 768 bytes per channel. */
494  ret = ff_alloc_packet(avctx, avpkt, FFMAX(8192, 768 * avctx->ch_layout.nb_channels));
495  if (ret < 0)
496  return ret;
497 
498  out_ptr = avpkt->data;
499  out_buffer_size = avpkt->size;
500  out_buffer_element_size = 1;
501  out_buf.numBufs = 1;
502  out_buf.bufs = &out_ptr;
503  out_buf.bufferIdentifiers = &out_buffer_identifier;
504  out_buf.bufSizes = &out_buffer_size;
505  out_buf.bufElSizes = &out_buffer_element_size;
506 
507  if ((err = aacEncEncode(s->handle, &in_buf, &out_buf, &in_args,
508  &out_args)) != AACENC_OK) {
509  if (!frame && err == AACENC_ENCODE_EOF)
510  return 0;
511  av_log(avctx, AV_LOG_ERROR, "Unable to encode frame: %s\n",
512  aac_get_error(err));
513  return AVERROR(EINVAL);
514  }
515 
516  if (!out_args.numOutBytes)
517  return 0;
518 
519  /* Get the next frame pts & duration */
520  ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
521  &avpkt->duration);
522 
523  discard_padding = avctx->frame_size - avpkt->duration;
524  // Check if subtraction resulted in an overflow
525  if ((discard_padding < avctx->frame_size) != (avpkt->duration > 0)) {
526  av_log(avctx, AV_LOG_ERROR, "discard padding overflow\n");
527  return AVERROR(EINVAL);
528  }
529  if ((!s->delay_sent && avctx->initial_padding > 0) || discard_padding > 0) {
530  uint8_t *side_data =
532  if (!side_data)
533  return AVERROR(ENOMEM);
534  if (!s->delay_sent) {
535  AV_WL32(side_data, avctx->initial_padding);
536  s->delay_sent = 1;
537  }
538  AV_WL32(side_data + 4, discard_padding);
539  }
540 
541  avpkt->size = out_args.numOutBytes;
542  *got_packet_ptr = 1;
543  return 0;
544 }
545 
546 static const AVProfile profiles[] = {
547  { AV_PROFILE_AAC_LOW, "LC" },
548  { AV_PROFILE_AAC_HE, "HE-AAC" },
549  { AV_PROFILE_AAC_HE_V2, "HE-AACv2" },
550  { AV_PROFILE_AAC_LD, "LD" },
551  { AV_PROFILE_AAC_ELD, "ELD" },
552  { AV_PROFILE_UNKNOWN },
553 };
554 
556  { "b", "0" },
557  { NULL }
558 };
559 
560 static const AVChannelLayout aac_ch_layouts[16] = {
567 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
569 #endif
570 #if FDKENC_VER_AT_LEAST(3, 4) // 3.4.12
573 #endif
574 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
576 #endif
577  { 0 },
578 };
579 
580 static const int aac_sample_rates[] = {
581  96000, 88200, 64000, 48000, 44100, 32000,
582  24000, 22050, 16000, 12000, 11025, 8000, 0
583 };
584 
586  .p.name = "libfdk_aac",
587  CODEC_LONG_NAME("Fraunhofer FDK AAC"),
588  .p.type = AVMEDIA_TYPE_AUDIO,
589  .p.id = AV_CODEC_ID_AAC,
590  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
593  .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE,
594  .priv_data_size = sizeof(AACContext),
597  .flush = aac_encode_flush,
598  .close = aac_encode_close,
599  .p.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
601  .p.priv_class = &aac_enc_class,
602  .defaults = aac_encode_defaults,
603  .p.profiles = profiles,
604  .p.supported_samplerates = aac_sample_rates,
605  .p.wrapper_name = "libfdk",
606  .p.ch_layouts = aac_ch_layouts,
607 };
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:1077
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
av_clip
#define av_clip
Definition: common.h:98
aac_ch_layouts
static const AVChannelLayout aac_ch_layouts[16]
Definition: libfdk-aacenc.c:560
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
aac_enc_class
static const AVClass aac_enc_class
Definition: libfdk-aacenc.c:87
ff_libfdk_aac_encoder
const FFCodec ff_libfdk_aac_encoder
Definition: libfdk-aacenc.c:585
AV_WL32
#define AV_WL32(p, v)
Definition: intreadwrite.h:424
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:379
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1050
ff_af_queue_close
void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
Definition: audio_frame_queue.c:36
AV_CODEC_FLAG_QSCALE
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
Definition: avcodec.h:224
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:446
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:344
aac_encode_init
static av_cold int aac_encode_init(AVCodecContext *avctx)
Definition: libfdk-aacenc.c:175
AVPacket::data
uint8_t * data
Definition: packet.h:522
AACContext::signaling
int signaling
Definition: libfdk-aacenc.c:46
AVOption
AVOption.
Definition: opt.h:346
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:540
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:313
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:365
AV_CODEC_FLAG_GLOBAL_HEADER
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:338
AVProfile
AVProfile.
Definition: codec.h:179
AACContext::frame_length
int frame_length
Definition: libfdk-aacenc.c:58
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:1065
aac_get_error
static const char * aac_get_error(AACENC_ERROR err)
Definition: libfdk-aacenc.c:94
audio_frame_queue.h
AVCodecContext::initial_padding
int initial_padding
Audio only.
Definition: avcodec.h:1122
AV_CHANNEL_LAYOUT_7POINT1_TOP_BACK
#define AV_CHANNEL_LAYOUT_7POINT1_TOP_BACK
Definition: channel_layout.h:416
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:502
pts
static int64_t pts
Definition: transcode_aac.c:643
AV_CODEC_CAP_ENCODER_FLUSH
#define AV_CODEC_CAP_ENCODER_FLUSH
This encoder can be flushed using avcodec_flush_buffers().
Definition: codec.h:166
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:296
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_CHANNEL_LAYOUT_SURROUND
#define AV_CHANNEL_LAYOUT_SURROUND
Definition: channel_layout.h:382
AV_OPT_FLAG_AUDIO_PARAM
#define AV_OPT_FLAG_AUDIO_PARAM
Definition: opt.h:274
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
AV_PROFILE_UNKNOWN
#define AV_PROFILE_UNKNOWN
Definition: defs.h:65
duration
int64_t duration
Definition: movenc.c:64
AV_CHANNEL_LAYOUT_4POINT0
#define AV_CHANNEL_LAYOUT_4POINT0
Definition: channel_layout.h:384
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:524
AV_CHANNEL_LAYOUT_7POINT1
#define AV_CHANNEL_LAYOUT_7POINT1
Definition: channel_layout.h:401
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVCodecContext::global_quality
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:1239
AV_PROFILE_MPEG2_AAC_HE
#define AV_PROFILE_MPEG2_AAC_HE
Definition: defs.h:77
frame_size
int frame_size
Definition: mxfenc.c:2422
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:390
AACContext::prog_ref
int prog_ref
Definition: libfdk-aacenc.c:54
AACContext::eld_v2
int eld_v2
Definition: libfdk-aacenc.c:45
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
AV_PROFILE_AAC_ELD
#define AV_PROFILE_AAC_ELD
Definition: defs.h:75
AACContext::latm
int latm
Definition: libfdk-aacenc.c:47
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
frame
static AVFrame * frame
Definition: demux_decode.c:54
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:403
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:495
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:126
AACContext::afterburner
int afterburner
Definition: libfdk-aacenc.c:43
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:269
aac_encode_defaults
static const FFCodecDefault aac_encode_defaults[]
Definition: libfdk-aacenc.c:555
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:442
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:365
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:523
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:303
codec_internal.h
AACContext::afq
AudioFrameQueue afq
Definition: libfdk-aacenc.c:60
AV_PROFILE_AAC_LD
#define AV_PROFILE_AAC_LD
Definition: defs.h:74
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
aac_encode_flush
static void aac_encode_flush(AVCodecContext *avctx)
Definition: libfdk-aacenc.c:137
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:800
AV_CHANNEL_LAYOUT_6POINT1_BACK
#define AV_CHANNEL_LAYOUT_6POINT1_BACK
Definition: channel_layout.h:397
AACContext::eld_sbr
int eld_sbr
Definition: libfdk-aacenc.c:44
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:424
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:515
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:523
common.h
AVCodecContext::cutoff
int cutoff
Audio cutoff bandwidth (0 means "automatic")
Definition: avcodec.h:1090
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
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:194
avcodec.h
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
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
AV_PROFILE_AAC_LOW
#define AV_PROFILE_AAC_LOW
Definition: defs.h:69
AVCodecContext
main external API structure.
Definition: avcodec.h:445
channel_layout.h
AV_PROFILE_AAC_HE_V2
#define AV_PROFILE_AAC_HE_V2
Definition: defs.h:73
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:231
mode
mode
Definition: ebur128.h:83
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:235
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1639
AV_PKT_DATA_SKIP_SAMPLES
@ AV_PKT_DATA_SKIP_SAMPLES
Recommmends skipping the specified number of samples.
Definition: packet.h:157
profiles
static const AVProfile profiles[]
Definition: libfdk-aacenc.c:546
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:378
AV_PROFILE_AAC_HE
#define AV_PROFILE_AAC_HE
Definition: defs.h:72
FF_AAC_PROFILE_OPTS
#define FF_AAC_PROFILE_OPTS
Definition: profiles.h:28
AVPacket
This structure stores compressed data.
Definition: packet.h:499
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
aac_sample_rates
static const int aac_sample_rates[]
Definition: libfdk-aacenc.c:580
AACContext
Definition: libfdk-aacenc.c:40
AV_CHANNEL_LAYOUT_5POINT1_BACK
#define AV_CHANNEL_LAYOUT_5POINT1_BACK
Definition: channel_layout.h:391
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
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:244
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:61
aac_enc_options
static const AVOption aac_enc_options[]
Definition: libfdk-aacenc.c:63
AACContext::drc_profile
int drc_profile
Definition: libfdk-aacenc.c:50