From 5b9d971b108677d54bc3aac317669007462dbcc9 Mon Sep 17 00:00:00 2001
From: Lynne <dev@lynne.ee>
Date: Mon, 30 Oct 2023 05:40:22 +0100
Subject: [PATCH 2/2] aacdec: fix single-sample delay for SBR streams
Our SBR implementation introduces a single sample of latency into the
output, which is currently never skipped.
This delay is separate from any container skip value and always present,
as it is from an additional part of the decoding process.
Fixes Trac issue #9667
---
libavcodec/aac.h | 1 +
libavcodec/aacdec_template.c | 23 ++++++++++++++++++-----
2 files changed, 19 insertions(+), 5 deletions(-)
@@ -298,6 +298,7 @@ struct AACContext {
AVCodecContext *avctx;
AVFrame *frame;
+ int skip_samples;
int is_saved; ///< Set if elements have stored overlap from previous frame.
DynamicRangeControl che_drc;
@@ -529,6 +529,8 @@ static void flush(AVCodecContext *avctx)
}
}
}
+
+ ac->skip_samples = 1;
}
/**
@@ -1222,6 +1224,7 @@ static av_cold int aac_decode_init(AVCodecContext *avctx)
return AVERROR(ENOMEM);
}
+ ac->skip_samples = 1;
ac->random_state = 0x1f2e3d4c;
#define MDCT_INIT(s, fn, len, sval) \
@@ -2390,7 +2393,7 @@ static int decode_dynamic_range(DynamicRangeControl *che_drc,
static int decode_fill(AACContext *ac, GetBitContext *gb, int len) {
uint8_t buf[256];
- int i, major, minor;
+ int i;
if (len < 13+7*8)
goto unknown;
@@ -2404,10 +2407,6 @@ static int decode_fill(AACContext *ac, GetBitContext *gb, int len) {
if (ac->avctx->debug & FF_DEBUG_PICT_INFO)
av_log(ac->avctx, AV_LOG_DEBUG, "FILL:%s\n", buf);
- if (sscanf(buf, "libfaac %d.%d", &major, &minor) == 2){
- ac->avctx->internal->skip_samples = 1024;
- }
-
unknown:
skip_bits_long(gb, len);
@@ -3374,6 +3373,20 @@ static int aac_decode_frame(AVCodecContext *avctx, AVFrame *frame,
if (buf[buf_offset])
break;
+ if (ac->skip_samples) {
+ int skip = 0;
+
+ /* Our SBR implementation introduces a sample of delay into the output. */
+ if (ac->oc[1].m4ac.sbr)
+ skip = 1;
+
+ err = ff_decode_skip_samples(avctx, frame, skip);
+ if (err < 0)
+ return err;
+
+ ac->skip_samples = 0;
+ }
+
return buf_size > buf_offset ? buf_consumed : buf_size;
}
--
2.43.0