diff mbox series

[FFmpeg-devel,v4] avcodec/libfdk-accenc: Add option to set frame length when encoding with libfdk_aac

Message ID 20230330122741.56060-1-info@raphael.schlarb.one
State New
Headers show
Series [FFmpeg-devel,v4] avcodec/libfdk-accenc: Add option to set frame length when encoding with libfdk_aac | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Raphael Schlarb March 30, 2023, 12:27 p.m. UTC
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 frame_length option to solve this problem.

Signed-off-by: Raphael Schlarb <info@raphael.schlarb.one>
---
Adjusted according to Martin's suggestion
 libavcodec/libfdk-aacenc.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

Comments

Martin Storsjö March 31, 2023, 6:51 a.m. UTC | #1
On Thu, 30 Mar 2023, Raphael Schlarb wrote:

> 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 frame_length option to solve this problem.
>
> Signed-off-by: Raphael Schlarb <info@raphael.schlarb.one>
> ---
> Adjusted according to Martin's suggestion
> libavcodec/libfdk-aacenc.c | 11 +++++++++++
> 1 file changed, 11 insertions(+)
>
> diff --git a/libavcodec/libfdk-aacenc.c b/libavcodec/libfdk-aacenc.c
> index eb97e0fb41..954b8e06d0 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 frame_length;
>
>     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 },
> +    { "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 },
>     FF_AAC_PROFILE_OPTS
>     { NULL }
> };
> @@ -166,6 +168,15 @@ static av_cold int aac_encode_init(AVCodecContext *avctx)
>         }
>     }
>
> +    if (s->frame_length != -1) {

I would kinda have used > 0 or >= 0 as comparison here, but that's just a 
nitpick (no need to resubmit just for that).

> +        if ((err = aacEncoder_SetParam(s->handle, AACENC_GRANULE_LENGTH,
> +                                       s->frame_length)) != AACENC_OK) {
> +            av_log(avctx, AV_LOG_ERROR, "Unable to set granule length: %s\n",
> +                   aac_get_error(err));
> +            goto error;
> +        }
> +    }
> +

Overall this looks fine to me, thanks!

// Martin
James Almer March 31, 2023, 11:54 a.m. UTC | #2
On 3/31/2023 3:51 AM, Martin Storsjö wrote:
> On Thu, 30 Mar 2023, Raphael Schlarb wrote:
> 
>> 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 frame_length option to solve this problem.
>>
>> Signed-off-by: Raphael Schlarb <info@raphael.schlarb.one>
>> ---
>> Adjusted according to Martin's suggestion
>> libavcodec/libfdk-aacenc.c | 11 +++++++++++
>> 1 file changed, 11 insertions(+)
>>
>> diff --git a/libavcodec/libfdk-aacenc.c b/libavcodec/libfdk-aacenc.c
>> index eb97e0fb41..954b8e06d0 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 frame_length;
>>
>>     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 },
>> +    { "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 },
>>     FF_AAC_PROFILE_OPTS
>>     { NULL }
>> };
>> @@ -166,6 +168,15 @@ static av_cold int aac_encode_init(AVCodecContext 
>> *avctx)
>>         }
>>     }
>>
>> +    if (s->frame_length != -1) {
> 
> I would kinda have used > 0 or >= 0 as comparison here, but that's just 
> a nitpick (no need to resubmit just for that).

Made that change, bumped micro version, and applied.

> 
>> +        if ((err = aacEncoder_SetParam(s->handle, AACENC_GRANULE_LENGTH,
>> +                                       s->frame_length)) != AACENC_OK) {
>> +            av_log(avctx, AV_LOG_ERROR, "Unable to set granule 
>> length: %s\n",
>> +                   aac_get_error(err));
>> +            goto error;
>> +        }
>> +    }
>> +
> 
> Overall this looks fine to me, thanks!
> 
> // Martin
> 
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
diff mbox series

Patch

diff --git a/libavcodec/libfdk-aacenc.c b/libavcodec/libfdk-aacenc.c
index eb97e0fb41..954b8e06d0 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 frame_length;
 
     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 },
+    { "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 },
     FF_AAC_PROFILE_OPTS
     { NULL }
 };
@@ -166,6 +168,15 @@  static av_cold int aac_encode_init(AVCodecContext *avctx)
         }
     }
 
+    if (s->frame_length != -1) {
+        if ((err = aacEncoder_SetParam(s->handle, AACENC_GRANULE_LENGTH,
+                                       s->frame_length)) != 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",