[FFmpeg-devel] [PATCH] Add option to encode short aac ld/eld frames with libfdk_aac

Raphael Schlarb info at raphael.schlarb.one
Mon Mar 27 15:28:13 EEST 2023


Some specifications require the size of ld/eld frames to be 480 samples
instead of the default 512. libfdk_aac provides an option to set an alternative
frame size, but it's not exposed via the ffmpeg interface.
This patch adds a boolean short_frame option to encode ld/eld frames of
size 480.

Signed-off-by: Raphael Schlarb <info at raphael.schlarb.one>
---
libavcodec/libfdk-aacenc.c | 13 +++++++++++++
1 file changed, 13 insertions(+)

diff --git a/libavcodec/libfdk-aacenc.c b/libavcodec/libfdk-aacenc.c
index eb97e0fb41..bd719850dd 100644
--- a/libavcodec/libfdk-aacenc.c
+++ b/libavcodec/libfdk-aacenc.c
@@ -55,6 +55,7 @@ typedef struct AACContext {
    int metadata_mode;
    AACENC_MetaData metaDataSetup;
    int delay_sent;
+    int short_frame;

    AudioFrameQueue afq;
} AACContext;
@@ -78,6 +79,7 @@ static const AVOption aac_enc_options[] = {
    { "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 },
    { "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 },
    { "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 },
+    { "short_frame", "Encode short LD/ELD frames, using 480 instead of 512 samples per frame", offsetof(AACContext, short_frame), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
    FF_AAC_PROFILE_OPTS
    { NULL }
};
@@ -166,6 +168,17 @@ static av_cold int aac_encode_init(AVCodecContext *avctx)
        }
    }

+    if (s->short_frame) {
+        if (aot == FF_PROFILE_AAC_LD + 1 || aot == FF_PROFILE_AAC_ELD + 1) {
+            if ((err = aacEncoder_SetParam(s->handle, AACENC_GRANULE_LENGTH,
+                                           480)) != AACENC_OK) {
+                av_log(avctx, AV_LOG_ERROR, "Unable to set granule length: %s\n",
+                       aac_get_error(err));
+                goto error;
+            }
+        }
+    }
+
    if ((err = aacEncoder_SetParam(s->handle, AACENC_SAMPLERATE,
                                   avctx->sample_rate)) != AACENC_OK) {
        av_log(avctx, AV_LOG_ERROR, "Unable to set the sample rate %d: %s\n",
-- 
2.40.0



More information about the ffmpeg-devel mailing list