Commit 37f5b5defe for ffmpeg

commit 37f5b5defed77c742ce053b7037b72f5eeb383e3
Author: Romain Beauxis <romain.beauxis@gmail.com>
Date:   Wed Nov 4 15:14:22 2026 -0600

    avcodec/aac_ac3_parser: derive the duration of spliced ADTS frames

    The parser sets the duration of an ADTS frame that does not match the probed stream from the sample count and sample rate in its header, including frames completed from buffered input.

    It warns when this happens, since the stream changed its frame duration mid-stream.

    The splice test reference now gives each section its own frame duration.

diff --git a/libavcodec/aac_ac3_parser.c b/libavcodec/aac_ac3_parser.c
index 51c801214c..0cfb0bc075 100644
--- a/libavcodec/aac_ac3_parser.c
+++ b/libavcodec/aac_ac3_parser.c
@@ -29,6 +29,46 @@
 #include "ac3_parser_internal.h"
 #include "adts_header.h"

+#if CONFIG_AAC_PARSER
+/// divisible by every ADTS sampling rate
+#define ADTS_RATES_LCM 28224000
+
+/**
+ * Set the duration of an ADTS frame that does not match the probed stream,
+ * as when two streams are spliced together.
+ *
+ * The ratio of the ADTS sample count to the ADTS sample rate is the frame
+ * duration even for HE-AAC, so it is expressed in units of
+ * avctx->sample_rate and the remainder carried to the next frame.
+ */
+static void set_spliced_adts_duration(AVCodecParserContext *s1,
+                                      AVCodecContext *avctx,
+                                      const AACADTSHeaderInfo *hdr)
+{
+    AACAC3ParseContext *s = s1->priv_data;
+
+    if (avctx->frame_size <= 0 || avctx->sample_rate <= 0)
+        return;
+
+    int64_t num  = (int64_t)hdr->samples * avctx->sample_rate;
+    int mismatch = num / hdr->num_aac_frames !=
+                   (int64_t)avctx->frame_size * hdr->sample_rate;
+
+    if (mismatch) {
+        if (!s->spliced)
+            av_log(avctx, AV_LOG_WARNING,
+                   "ADTS frame duration changed mid-stream to "
+                   "%d samples at %d Hz\n", hdr->samples, hdr->sample_rate);
+        num = num * (ADTS_RATES_LCM / hdr->sample_rate) + s->duration_rem;
+        s1->duration    = num / ADTS_RATES_LCM;
+        s->duration_rem = num % ADTS_RATES_LCM;
+    } else {
+        s1->duration = 0;
+    }
+    s->spliced = mismatch;
+}
+#endif
+
 int ff_aac_ac3_parse(AVCodecParserContext *s1,
                      AVCodecContext *avctx,
                      const uint8_t **poutbuf, int *poutbuf_size,
@@ -52,6 +92,7 @@ get_next:
             if(s->remaining_size && !s->need_next_header){
                 i= s->remaining_size;
                 s->remaining_size = 0;
+                got_frame = avctx->codec_id == AV_CODEC_ID_AAC;
             }else{ //we need a header first
                 len=0;
                 for(i=s->remaining_size; i<buf_size; i++){
@@ -156,6 +197,7 @@ get_next:
                 avctx->profile = hdr.object_type - 1;
             /* ADTS does not support USAC */
             s1->key_frame = 1;
+            set_spliced_adts_duration(s1, avctx, &hdr);
             bit_rate = hdr.bit_rate;
 #endif
         }
diff --git a/libavcodec/aac_ac3_parser.h b/libavcodec/aac_ac3_parser.h
index e3259d1841..027180129b 100644
--- a/libavcodec/aac_ac3_parser.h
+++ b/libavcodec/aac_ac3_parser.h
@@ -39,6 +39,11 @@ typedef struct AACAC3ParseContext {

     int need_next_header;
     int frame_number;
+    /// remainder of the spliced ADTS frame durations, in units of
+    /// 1 / 28224000 of an avctx->sample_rate sample
+    int64_t duration_rem;
+    /// whether the last ADTS frame did not match the probed stream
+    int spliced;
 } AACAC3ParseContext;

 int ff_aac_ac3_parse(AVCodecParserContext *s1,
diff --git a/tests/ref/fate/aac-adts-splice b/tests/ref/fate/aac-adts-splice
index 6bc6752fda..f566b92ec7 100644
--- a/tests/ref/fate/aac-adts-splice
+++ b/tests/ref/fate/aac-adts-splice
@@ -92,138 +92,138 @@
 0,   56360960,   56360960,   655360,      339, 0xeeb3a879
 0,   57016320,   57016320,   655360,      263, 0xde7081c0
 0,   57671680,   57671680,   655360,      102, 0x4e1c2de2
-0,   58327040,   58327040,   655360,       32, 0xb2a5083d
-0,   58982400,   58982400,   655360,      250, 0xa3c47436
-0,   59637760,   59637760,   655360,      164, 0xb3a1472d
-0,   60293120,   60293120,   655360,      127, 0x22a94206
-0,   60948480,   60948480,   655360,      121, 0x1f1437d1
-0,   61603840,   61603840,   655360,      291, 0xc1ce79c1
-0,   62259200,   62259200,   655360,      218, 0x765164b4
-0,   62914560,   62914560,   655360,      213, 0x8606661a
-0,   63569920,   63569920,   655360,      209, 0x0ff95aff
-0,   64225280,   64225280,   655360,      329, 0xf338a37b
-0,   64880640,   64880640,   655360,      174, 0x5e3751b9
-0,   65536000,   65536000,   655360,      155, 0x90434d85
-0,   66191360,   66191360,   655360,      157, 0x29cb47d8
-0,   66846720,   66846720,   655360,      417, 0xccf5d3c4
-0,   67502080,   67502080,   655360,      286, 0xeb837e03
-0,   68157440,   68157440,   655360,      258, 0xab947983
-0,   68812800,   68812800,   655360,      188, 0xd7c44bb9
-0,   69468160,   69468160,   655360,      371, 0x5330af51
-0,   70123520,   70123520,   655360,      253, 0xab09751d
-0,   70778880,   70778880,   655360,      190, 0x0cbf5062
-0,   71434240,   71434240,   655360,      228, 0xba71619e
-0,   72089600,   72089600,   655360,      356, 0x0a1db3dd
-0,   72744960,   72744960,   655360,      286, 0x8aa48ae0
-0,   73400320,   73400320,   655360,      282, 0x612d8310
-0,   74055680,   74055680,   655360,      310, 0xb9fc92d6
-0,   74711040,   74711040,   655360,      252, 0xaace78d6
-0,   75366400,   75366400,   655360,      225, 0xf2936ae1
-0,   76021760,   76021760,   655360,      218, 0x1a496cb8
-0,   76677120,   76677120,   655360,      230, 0x4d1f6f11
-0,   77332480,   77332480,   655360,      348, 0x4780a1fa
-0,   77987840,   77987840,   655360,      289, 0x5399817b
-0,   78643200,   78643200,   655360,      288, 0xc43483f2
-0,   79298560,   79298560,   655360,      378, 0x65c2b3f3
-0,   79953920,   79953920,   655360,      248, 0x5d047119
-0,   80609280,   80609280,   655360,      197, 0xb8c25412
-0,   81264640,   81264640,   655360,      205, 0xf8106336
-0,   81920000,   81920000,   655360,      251, 0x5805768f
-0,   82575360,   82575360,   655360,      364, 0x08f3b150
-0,   83230720,   83230720,   655360,      287, 0x855e825a
-0,   83886080,   83886080,   655360,      283, 0xd1d682f7
-0,   84541440,   84541440,   655360,      404, 0x88fcc0b8
-0,   85196800,   85196800,   655360,      290, 0x78468752
-0,   85852160,   85852160,   655360,      274, 0x1f5e7d1a
-0,   86507520,   86507520,   655360,      228, 0x525069de
-0,   87162880,   87162880,   655360,      288, 0x245b8192
-0,   87818240,   87818240,   655360,      123, 0x8396352b
-0,   88473600,   88473600,   655360,       13, 0x2a760491
-0,   89128960,   89128960,   655360,      224, 0xda527385
-0,   89784320,   89784320,   655360,      155, 0xd0b04d7c
-0,   90439680,   90439680,   655360,      180, 0xcd6e56d8
-0,   91095040,   91095040,   655360,      163, 0xd86649bc
-0,   91750400,   91750400,   655360,      185, 0xbe51558b
-0,   92405760,   92405760,   655360,      170, 0xce135016
-0,   93061120,   93061120,   655360,      150, 0x08c646de
-0,   93716480,   93716480,   655360,      167, 0x4e78542c
-0,   94371840,   94371840,   655360,      188, 0xe02a5fb8
-0,   95027200,   95027200,   655360,      264, 0x67717fc5
-0,   95682560,   95682560,   655360,      215, 0x3a376a73
-0,   96337920,   96337920,   655360,      212, 0x72fc6470
-0,   96993280,   96993280,   655360,      219, 0x0908638c
-0,   97648640,   97648640,   655360,      220, 0x02616a55
-0,   98304000,   98304000,   655360,      235, 0x2e8d72f3
-0,   98959360,   98959360,   655360,      161, 0x292f495e
-0,   99614720,   99614720,   655360,      362, 0xd7ecabb5
-0,  100270080,  100270080,   655360,      167, 0xf9f14f81
-0,  100925440,  100925440,   655360,      212, 0x0f9962a3
-0,  101580800,  101580800,   655360,      202, 0x33546313
-0,  102236160,  102236160,   655360,      222, 0xa849661e
-0,  102891520,  102891520,   655360,      296, 0x78c78bb0
-0,  103546880,  103546880,   655360,      249, 0x4ea27a68
-0,  104202240,  104202240,   655360,      273, 0x8bfa83c9
-0,  104857600,  104857600,   655360,      417, 0xd22fc8c4
-0,  105512960,  105512960,   655360,      291, 0x45098fea
-0,  106168320,  106168320,   655360,      275, 0x2ffe8800
-0,  106823680,  106823680,   655360,      283, 0x2de58c97
-0,  107479040,  107479040,   655360,      292, 0xc6898be1
-0,  108134400,  108134400,   655360,      271, 0x1b4680ae
-0,  108789760,  108789760,   655360,      301, 0xbb1d8cac
-0,  109445120,  109445120,   655360,      301, 0x52fd94f6
-0,  110100480,  110100480,   655360,      294, 0x73eb9af0
-0,  110755840,  110755840,   655360,      298, 0xd2009a01
-0,  111411200,  111411200,   655360,      298, 0xebde946c
-0,  112066560,  112066560,   655360,      329, 0xf7989b97
-0,  112721920,  112721920,   655360,      288, 0xa28e8e9f
-0,  113377280,  113377280,   655360,      326, 0xe03fac7d
-0,  114032640,  114032640,   655360,      303, 0xad469175
-0,  114688000,  114688000,   655360,      308, 0x27768a57
-0,  115343360,  115343360,   655360,      400, 0xc27ec70c
-0,  115998720,  115998720,   655360,      322, 0xdf3e9c47
-0,  116654080,  116654080,   655360,      289, 0x5ecd8fab
-0,  117309440,  117309440,   655360,      309, 0x35d39dbe
-0,  117964800,  117964800,   655360,      337, 0x54bba5c7
-0,  118620160,  118620160,   655360,      320, 0x51cba076
-0,  119275520,  119275520,   655360,      297, 0x960887b8
-0,  119930880,  119930880,   655360,      399, 0xf214c51d
-0,  120586240,  120586240,   655360,      338, 0x7c38a5da
-0,  121241600,  121241600,   655360,      323, 0x84289bd4
-0,  121896960,  121896960,   655360,      324, 0xbee693a1
-0,  122552320,  122552320,   655360,      315, 0x663d9c1d
-0,  123207680,  123207680,   655360,      326, 0xcd979e4c
-0,  123863040,  123863040,   655360,      309, 0x9d769cbf
-0,  124518400,  124518400,   655360,      325, 0x0ca7a67c
-0,  125173760,  125173760,   655360,      333, 0x8ac79e64
-0,  125829120,  125829120,   655360,      384, 0xf6bfb642
-0,  126484480,  126484480,   655360,      307, 0x98e490bd
-0,  127139840,  127139840,   655360,      312, 0xcdde9437
-0,  127795200,  127795200,   655360,      341, 0x26f89e97
-0,  128450560,  128450560,   655360,      311, 0x6351929c
-0,  129105920,  129105920,   655360,      308, 0x41bb950c
-0,  129761280,  129761280,   655360,      336, 0x7440a7d4
-0,  130416640,  130416640,   655360,      282, 0xdaad83c0
-0,  131072000,  131072000,   655360,      317, 0xc4ff9d47
-0,  131727360,  131727360,   655360,      319, 0xab98a250
-0,  132382720,  132382720,   655360,      287, 0x01c1842e
-0,  133038080,  133038080,   655360,      307, 0xf1c1930a
-0,  133693440,  133693440,   655360,      309, 0x14e89fc2
-0,  134348800,  134348800,   655360,      272, 0xdebd7eb0
-0,  135004160,  135004160,   655360,      287, 0x4f77861e
-0,  135659520,  135659520,   655360,      386, 0x9bfabb71
-0,  136314880,  136314880,   655360,      336, 0xb7749e9e
-0,  136970240,  136970240,   655360,      306, 0xc1368ed3
-0,  137625600,  137625600,   655360,      316, 0xf4739a34
-0,  138280960,  138280960,   655360,      343, 0xfc12a2ea
-0,  138936320,  138936320,   655360,      317, 0x3e8797c0
-0,  139591680,  139591680,   655360,      309, 0x4cf89c82
-0,  140247040,  140247040,   655360,      333, 0x64819d40
-0,  140902400,  140902400,   655360,      326, 0x38ad9a7c
-0,  141557760,  141557760,   655360,      325, 0xbe1c95a7
-0,  142213120,  142213120,   655360,      296, 0xca1d960b
-0,  142868480,  142868480,   655360,      308, 0xa29c9715
-0,  143523840,  143523840,   655360,      343, 0xd24da6cc
-0,  144179200,  144179200,   655360,      318, 0xb0fb9ba3
-0,  144834560,  144834560,   655360,      339, 0xeeb3a879
-0,  145489920,  145489920,   655360,      263, 0xde7081c0
-0,  146145280,  146145280,   655360,      102, 0x4e1c2de2
+0,   58327040,   58327040,  1310720,       32, 0xb2a5083d
+0,   59637760,   59637760,  1310720,      250, 0xa3c47436
+0,   60948480,   60948480,  1310720,      164, 0xb3a1472d
+0,   62259200,   62259200,  1310720,      127, 0x22a94206
+0,   63569920,   63569920,  1310720,      121, 0x1f1437d1
+0,   64880640,   64880640,  1310720,      291, 0xc1ce79c1
+0,   66191360,   66191360,  1310720,      218, 0x765164b4
+0,   67502080,   67502080,  1310720,      213, 0x8606661a
+0,   68812800,   68812800,  1310720,      209, 0x0ff95aff
+0,   70123520,   70123520,  1310720,      329, 0xf338a37b
+0,   71434240,   71434240,  1310720,      174, 0x5e3751b9
+0,   72744960,   72744960,  1310720,      155, 0x90434d85
+0,   74055680,   74055680,  1310720,      157, 0x29cb47d8
+0,   75366400,   75366400,  1310720,      417, 0xccf5d3c4
+0,   76677120,   76677120,  1310720,      286, 0xeb837e03
+0,   77987840,   77987840,  1310720,      258, 0xab947983
+0,   79298560,   79298560,  1310720,      188, 0xd7c44bb9
+0,   80609280,   80609280,  1310720,      371, 0x5330af51
+0,   81920000,   81920000,  1310720,      253, 0xab09751d
+0,   83230720,   83230720,  1310720,      190, 0x0cbf5062
+0,   84541440,   84541440,  1310720,      228, 0xba71619e
+0,   85852160,   85852160,  1310720,      356, 0x0a1db3dd
+0,   87162880,   87162880,  1310720,      286, 0x8aa48ae0
+0,   88473600,   88473600,  1310720,      282, 0x612d8310
+0,   89784320,   89784320,  1310720,      310, 0xb9fc92d6
+0,   91095040,   91095040,  1310720,      252, 0xaace78d6
+0,   92405760,   92405760,  1310720,      225, 0xf2936ae1
+0,   93716480,   93716480,  1310720,      218, 0x1a496cb8
+0,   95027200,   95027200,  1310720,      230, 0x4d1f6f11
+0,   96337920,   96337920,  1310720,      348, 0x4780a1fa
+0,   97648640,   97648640,  1310720,      289, 0x5399817b
+0,   98959360,   98959360,  1310720,      288, 0xc43483f2
+0,  100270080,  100270080,  1310720,      378, 0x65c2b3f3
+0,  101580800,  101580800,  1310720,      248, 0x5d047119
+0,  102891520,  102891520,  1310720,      197, 0xb8c25412
+0,  104202240,  104202240,  1310720,      205, 0xf8106336
+0,  105512960,  105512960,  1310720,      251, 0x5805768f
+0,  106823680,  106823680,  1310720,      364, 0x08f3b150
+0,  108134400,  108134400,  1310720,      287, 0x855e825a
+0,  109445120,  109445120,  1310720,      283, 0xd1d682f7
+0,  110755840,  110755840,  1310720,      404, 0x88fcc0b8
+0,  112066560,  112066560,  1310720,      290, 0x78468752
+0,  113377280,  113377280,  1310720,      274, 0x1f5e7d1a
+0,  114688000,  114688000,  1310720,      228, 0x525069de
+0,  115998720,  115998720,  1310720,      288, 0x245b8192
+0,  117309440,  117309440,  1310720,      123, 0x8396352b
+0,  118620160,  118620160,   655360,       13, 0x2a760491
+0,  119275520,  119275520,   655360,      224, 0xda527385
+0,  119930880,  119930880,   655360,      155, 0xd0b04d7c
+0,  120586240,  120586240,   655360,      180, 0xcd6e56d8
+0,  121241600,  121241600,   655360,      163, 0xd86649bc
+0,  121896960,  121896960,   655360,      185, 0xbe51558b
+0,  122552320,  122552320,   655360,      170, 0xce135016
+0,  123207680,  123207680,   655360,      150, 0x08c646de
+0,  123863040,  123863040,   655360,      167, 0x4e78542c
+0,  124518400,  124518400,   655360,      188, 0xe02a5fb8
+0,  125173760,  125173760,   655360,      264, 0x67717fc5
+0,  125829120,  125829120,   655360,      215, 0x3a376a73
+0,  126484480,  126484480,   655360,      212, 0x72fc6470
+0,  127139840,  127139840,   655360,      219, 0x0908638c
+0,  127795200,  127795200,   655360,      220, 0x02616a55
+0,  128450560,  128450560,   655360,      235, 0x2e8d72f3
+0,  129105920,  129105920,   655360,      161, 0x292f495e
+0,  129761280,  129761280,   655360,      362, 0xd7ecabb5
+0,  130416640,  130416640,   655360,      167, 0xf9f14f81
+0,  131072000,  131072000,   655360,      212, 0x0f9962a3
+0,  131727360,  131727360,   655360,      202, 0x33546313
+0,  132382720,  132382720,   655360,      222, 0xa849661e
+0,  133038080,  133038080,   655360,      296, 0x78c78bb0
+0,  133693440,  133693440,   655360,      249, 0x4ea27a68
+0,  134348800,  134348800,   655360,      273, 0x8bfa83c9
+0,  135004160,  135004160,   655360,      417, 0xd22fc8c4
+0,  135659520,  135659520,   655360,      291, 0x45098fea
+0,  136314880,  136314880,   655360,      275, 0x2ffe8800
+0,  136970240,  136970240,   655360,      283, 0x2de58c97
+0,  137625600,  137625600,   655360,      292, 0xc6898be1
+0,  138280960,  138280960,   655360,      271, 0x1b4680ae
+0,  138936320,  138936320,   655360,      301, 0xbb1d8cac
+0,  139591680,  139591680,   655360,      301, 0x52fd94f6
+0,  140247040,  140247040,   655360,      294, 0x73eb9af0
+0,  140902400,  140902400,   655360,      298, 0xd2009a01
+0,  141557760,  141557760,   655360,      298, 0xebde946c
+0,  142213120,  142213120,   655360,      329, 0xf7989b97
+0,  142868480,  142868480,   655360,      288, 0xa28e8e9f
+0,  143523840,  143523840,   655360,      326, 0xe03fac7d
+0,  144179200,  144179200,   655360,      303, 0xad469175
+0,  144834560,  144834560,   655360,      308, 0x27768a57
+0,  145489920,  145489920,   655360,      400, 0xc27ec70c
+0,  146145280,  146145280,   655360,      322, 0xdf3e9c47
+0,  146800640,  146800640,   655360,      289, 0x5ecd8fab
+0,  147456000,  147456000,   655360,      309, 0x35d39dbe
+0,  148111360,  148111360,   655360,      337, 0x54bba5c7
+0,  148766720,  148766720,   655360,      320, 0x51cba076
+0,  149422080,  149422080,   655360,      297, 0x960887b8
+0,  150077440,  150077440,   655360,      399, 0xf214c51d
+0,  150732800,  150732800,   655360,      338, 0x7c38a5da
+0,  151388160,  151388160,   655360,      323, 0x84289bd4
+0,  152043520,  152043520,   655360,      324, 0xbee693a1
+0,  152698880,  152698880,   655360,      315, 0x663d9c1d
+0,  153354240,  153354240,   655360,      326, 0xcd979e4c
+0,  154009600,  154009600,   655360,      309, 0x9d769cbf
+0,  154664960,  154664960,   655360,      325, 0x0ca7a67c
+0,  155320320,  155320320,   655360,      333, 0x8ac79e64
+0,  155975680,  155975680,   655360,      384, 0xf6bfb642
+0,  156631040,  156631040,   655360,      307, 0x98e490bd
+0,  157286400,  157286400,   655360,      312, 0xcdde9437
+0,  157941760,  157941760,   655360,      341, 0x26f89e97
+0,  158597120,  158597120,   655360,      311, 0x6351929c
+0,  159252480,  159252480,   655360,      308, 0x41bb950c
+0,  159907840,  159907840,   655360,      336, 0x7440a7d4
+0,  160563200,  160563200,   655360,      282, 0xdaad83c0
+0,  161218560,  161218560,   655360,      317, 0xc4ff9d47
+0,  161873920,  161873920,   655360,      319, 0xab98a250
+0,  162529280,  162529280,   655360,      287, 0x01c1842e
+0,  163184640,  163184640,   655360,      307, 0xf1c1930a
+0,  163840000,  163840000,   655360,      309, 0x14e89fc2
+0,  164495360,  164495360,   655360,      272, 0xdebd7eb0
+0,  165150720,  165150720,   655360,      287, 0x4f77861e
+0,  165806080,  165806080,   655360,      386, 0x9bfabb71
+0,  166461440,  166461440,   655360,      336, 0xb7749e9e
+0,  167116800,  167116800,   655360,      306, 0xc1368ed3
+0,  167772160,  167772160,   655360,      316, 0xf4739a34
+0,  168427520,  168427520,   655360,      343, 0xfc12a2ea
+0,  169082880,  169082880,   655360,      317, 0x3e8797c0
+0,  169738240,  169738240,   655360,      309, 0x4cf89c82
+0,  170393600,  170393600,   655360,      333, 0x64819d40
+0,  171048960,  171048960,   655360,      326, 0x38ad9a7c
+0,  171704320,  171704320,   655360,      325, 0xbe1c95a7
+0,  172359680,  172359680,   655360,      296, 0xca1d960b
+0,  173015040,  173015040,   655360,      308, 0xa29c9715
+0,  173670400,  173670400,   655360,      343, 0xd24da6cc
+0,  174325760,  174325760,   655360,      318, 0xb0fb9ba3
+0,  174981120,  174981120,   655360,      339, 0xeeb3a879
+0,  175636480,  175636480,   655360,      263, 0xde7081c0
+0,  176291840,  176291840,   655360,      102, 0x4e1c2de2