Commit 98f40e11d8 for ffmpeg

commit 98f40e11d83bf8fdd21c07f1d15b5d473a4b36db
Author: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Date:   Wed Dec 2 10:37:44 2026 -0800

    avcodec/qdm2: decode shortest FFT tones

    Decode duration group 4 and queue each FFT coefficient by its
    parsed duration and synthesis subpacket, preserving insertion order
    within each queue. The old FATE PCM matches the decoder that omits
    these tones. Compare against an independent QuickTime 7 extraction.

    Assisted-by: Fairy
    Signed-off-by: Baptiste Coudurier <baptiste.coudurier@gmail.com>

diff --git a/libavcodec/qdm2.c b/libavcodec/qdm2.c
index 846a2e4ea5..06e8088004 100644
--- a/libavcodec/qdm2.c
+++ b/libavcodec/qdm2.c
@@ -109,7 +109,7 @@ typedef struct FFTTone {
 } FFTTone;

 typedef struct FFTCoefficient {
-    int16_t sub_packet;
+    uint16_t next;
     uint8_t channel;
     int16_t offset;
     int16_t exp;
@@ -153,10 +153,11 @@ typedef struct QDM2Context {
     FFTTone fft_tones[1000];
     int fft_tone_start;
     int fft_tone_end;
-    FFTCoefficient fft_coefs[1000];
+    FFTCoefficient fft_coefs[1 + 1000]; // index 0 marks the end of a list
     int fft_coefs_index;
-    int fft_coefs_min_index[5];
-    int fft_coefs_max_index[5];
+    struct {
+        uint16_t first, last;
+    } fft_coef_list[5][16]; // indexed by duration and synthesis subpacket
     int fft_level_exp[6];
     AVTXContext *rdft_ctx;
     av_tx_fn rdft_fn;
@@ -1303,16 +1304,28 @@ static void qdm2_fft_init_coefficient(QDM2Context *q, int sub_packet,
                                       int offset, int duration, int channel,
                                       int exp, int phase)
 {
-    if (q->fft_coefs_min_index[duration] < 0)
-        q->fft_coefs_min_index[duration] = q->fft_coefs_index;
-
-    q->fft_coefs[q->fft_coefs_index].sub_packet =
-        ((sub_packet >= 16) ? (sub_packet - 16) : sub_packet);
-    q->fft_coefs[q->fft_coefs_index].channel = channel;
-    q->fft_coefs[q->fft_coefs_index].offset  = offset;
-    q->fft_coefs[q->fft_coefs_index].exp     = exp;
-    q->fft_coefs[q->fft_coefs_index].phase   = phase;
-    q->fft_coefs_index++;
+    FFTCoefficient *coef;
+    int index, last;
+
+    if (sub_packet >= 16)
+        sub_packet -= 16;
+    if ((unsigned)sub_packet >= 16)
+        return;
+
+    index = ++q->fft_coefs_index;
+    coef = &q->fft_coefs[index];
+    coef->next    = 0;
+    coef->channel = channel;
+    coef->offset  = offset;
+    coef->exp     = exp;
+    coef->phase   = phase;
+
+    last = q->fft_coef_list[duration][sub_packet].last;
+    if (last)
+        q->fft_coefs[last].next = index;
+    else
+        q->fft_coef_list[duration][sub_packet].first = index;
+    q->fft_coef_list[duration][sub_packet].last = index;
 }

 static int qdm2_fft_decode_tones(QDM2Context *q, int duration,
@@ -1394,7 +1407,7 @@ static int qdm2_fft_decode_tones(QDM2Context *q, int duration,
         if (q->frequency_range > (local_int_14 + 1)) {
             int sub_packet = (local_int_20 + local_int_28);

-            if (q->fft_coefs_index + stereo >= FF_ARRAY_ELEMS(q->fft_coefs))
+            if (q->fft_coefs_index + 1 + stereo >= FF_ARRAY_ELEMS(q->fft_coefs))
                 return AVERROR_INVALIDDATA;

             qdm2_fft_init_coefficient(q, sub_packet, offset, duration,
@@ -1418,10 +1431,8 @@ static int qdm2_decode_fft_packets(QDM2Context *q)
     if (!q->sub_packet_list_B[0].packet)
         return AVERROR_INVALIDDATA;

-    /* reset minimum indexes for FFT coefficients */
     q->fft_coefs_index = 0;
-    for (i = 0; i < 5; i++)
-        q->fft_coefs_min_index[i] = -1;
+    memset(q->fft_coef_list, 0, sizeof(q->fft_coef_list));

     /* process subpackets ordered by type, largest type first */
     for (i = 0, max = 256; i < q->sub_packets_B; i++) {
@@ -1462,29 +1473,19 @@ static int qdm2_decode_fft_packets(QDM2Context *q)
         if ((type >= 17 && type < 24) || (type >= 33 && type < 40)) {
             int duration = q->sub_sampling + 5 - (type & 15);

-            if (duration >= 0 && duration < 4)
+            if (duration >= 0 && duration < 5)
                 qdm2_fft_decode_tones(q, duration, &gb, unknown_flag);
         } else if (type == 31) {
-            for (j = 0; j < 4; j++)
+            for (j = 0; j < 5; j++)
                 qdm2_fft_decode_tones(q, j, &gb, unknown_flag);
         } else if (type == 46) {
             for (j = 0; j < 6; j++)
                 q->fft_level_exp[j] = get_bits(&gb, 6);
-            for (j = 0; j < 4; j++)
+            for (j = 0; j < 5; j++)
                 qdm2_fft_decode_tones(q, j, &gb, unknown_flag);
         }
     } // Loop on B packets

-    /* calculate maximum indexes for FFT coefficients */
-    for (i = 0, j = -1; i < 5; i++)
-        if (q->fft_coefs_min_index[i] >= 0) {
-            if (j >= 0)
-                q->fft_coefs_max_index[j] = q->fft_coefs_min_index[i];
-            j = i;
-        }
-    if (j >= 0)
-        q->fft_coefs_max_index[j] = q->fft_coefs_index;
-
     return 0;
 }

@@ -1545,24 +1546,21 @@ static void qdm2_fft_tone_synthesizer(QDM2Context *q, int sub_packet)


     /* apply FFT tones with duration 4 (1 FFT period) */
-    if (q->fft_coefs_min_index[4] >= 0)
-        for (i = q->fft_coefs_min_index[4]; i < q->fft_coefs_max_index[4]; i++) {
-            float level;
-            AVComplexFloat c;
-
-            if (q->fft_coefs[i].sub_packet != sub_packet)
-                break;
-
-            ch = (q->channels == 1) ? 0 : q->fft_coefs[i].channel;
-            level = (q->fft_coefs[i].exp < 0) ? 0.0 : fft_tone_level_table[q->superblocktype_2_3 ? 0 : 1][q->fft_coefs[i].exp & 63];
-
-            c.re = level * cos(q->fft_coefs[i].phase * iscale);
-            c.im = level * sin(q->fft_coefs[i].phase * iscale);
-            q->fft.complex[ch][q->fft_coefs[i].offset + 0].re += c.re;
-            q->fft.complex[ch][q->fft_coefs[i].offset + 0].im += c.im;
-            q->fft.complex[ch][q->fft_coefs[i].offset + 1].re -= c.re;
-            q->fft.complex[ch][q->fft_coefs[i].offset + 1].im -= c.im;
-        }
+    for (i = q->fft_coef_list[4][sub_packet].first; i; i = q->fft_coefs[i].next) {
+        float level;
+        AVComplexFloat c;
+
+        ch = (q->channels == 1) ? 0 : q->fft_coefs[i].channel;
+        level = (q->fft_coefs[i].exp < 0) ? 0.0 : fft_tone_level_table[q->superblocktype_2_3 ? 0 : 1][q->fft_coefs[i].exp & 63];
+
+        c.re = level * cos(q->fft_coefs[i].phase * iscale);
+        c.im = level * sin(q->fft_coefs[i].phase * iscale);
+        q->fft.complex[ch][q->fft_coefs[i].offset + 0].re += c.re;
+        q->fft.complex[ch][q->fft_coefs[i].offset + 0].im += c.im;
+        q->fft.complex[ch][q->fft_coefs[i].offset + 1].re -= c.re;
+        q->fft.complex[ch][q->fft_coefs[i].offset + 1].im -= c.im;
+    }
+    q->fft_coef_list[4][sub_packet].first = 0;

     /* generate existing FFT tones */
     for (i = q->fft_tone_end; i != q->fft_tone_start; ) {
@@ -1571,38 +1569,34 @@ static void qdm2_fft_tone_synthesizer(QDM2Context *q, int sub_packet)
     }

     /* create and generate new FFT tones with duration 0 (long) to 3 (short) */
-    for (i = 0; i < 4; i++)
-        if (q->fft_coefs_min_index[i] >= 0) {
-            for (j = q->fft_coefs_min_index[i]; j < q->fft_coefs_max_index[i]; j++) {
-                int offset, four_i;
-                FFTTone tone;
-
-                if (q->fft_coefs[j].sub_packet != sub_packet)
-                    break;
-
-                four_i = (4 - i);
-                offset = q->fft_coefs[j].offset >> four_i;
-                ch = (q->channels == 1) ? 0 : q->fft_coefs[j].channel;
-
-                if (offset < q->frequency_range) {
-                    if (offset < 2)
-                        tone.cutoff = offset;
-                    else
-                        tone.cutoff = (offset >= 60) ? 3 : 2;
+    for (i = 0; i < 4; i++) {
+        for (j = q->fft_coef_list[i][sub_packet].first; j; j = q->fft_coefs[j].next) {
+            int offset, four_i;
+            FFTTone tone;
+
+            four_i = (4 - i);
+            offset = q->fft_coefs[j].offset >> four_i;
+            ch = (q->channels == 1) ? 0 : q->fft_coefs[j].channel;
+
+            if (offset < q->frequency_range) {
+                if (offset < 2)
+                    tone.cutoff = offset;
+                else
+                    tone.cutoff = (offset >= 60) ? 3 : 2;

-                    tone.level = (q->fft_coefs[j].exp < 0) ? 0.0 : fft_tone_level_table[q->superblocktype_2_3 ? 0 : 1][q->fft_coefs[j].exp & 63];
-                    tone.complex = &q->fft.complex[ch][offset];
-                    tone.table = fft_tone_sample_table[i][q->fft_coefs[j].offset - (offset << four_i)];
-                    tone.phase = 64 * q->fft_coefs[j].phase - (offset << 8) - 128;
-                    tone.phase_shift = (2 * q->fft_coefs[j].offset + 1) << (7 - four_i);
-                    tone.duration = i;
-                    tone.time_index = 0;
+                tone.level = (q->fft_coefs[j].exp < 0) ? 0.0 : fft_tone_level_table[q->superblocktype_2_3 ? 0 : 1][q->fft_coefs[j].exp & 63];
+                tone.complex = &q->fft.complex[ch][offset];
+                tone.table = fft_tone_sample_table[i][q->fft_coefs[j].offset - (offset << four_i)];
+                tone.phase = 64 * q->fft_coefs[j].phase - (offset << 8) - 128;
+                tone.phase_shift = (2 * q->fft_coefs[j].offset + 1) << (7 - four_i);
+                tone.duration = i;
+                tone.time_index = 0;

-                    qdm2_fft_generate_tone(q, &tone);
-                }
+                qdm2_fft_generate_tone(q, &tone);
             }
-            q->fft_coefs_min_index[i] = j;
         }
+        q->fft_coef_list[i][sub_packet].first = 0;
+    }
 }

 static void qdm2_calculate_fft(QDM2Context *q, int channel, int sub_packet)
diff --git a/tests/fate/qt.mak b/tests/fate/qt.mak
index 043b7b302b..56754cc146 100644
--- a/tests/fate/qt.mak
+++ b/tests/fate/qt.mak
@@ -4,7 +4,7 @@ fate-8bps: CMD = framecrc -i $(TARGET_SAMPLES)/8bps/full9iron-partial.mov -pix_f
 FATE_QT-$(call ENCDEC, PCM_S16LE QDM2, PCM_S16LE MOV, PIPE_PROTOCOL) += fate-qdm2
 fate-qdm2: CMD = pcm -i $(TARGET_SAMPLES)/qt-surge-suite/surge-2-16-B-QDM2.mov
 fate-qdm2: CMP = oneoff
-fate-qdm2: REF = $(SAMPLES)/qt-surge-suite/surge-2-16-B-QDM2-trimmed.pcm
+fate-qdm2: REF = $(SAMPLES)/qt-surge-suite/surge-2-16-B-QDM2-qt7.pcm
 fate-qdm2: FUZZ = 2

 FATE_QT-$(call ENCDEC, PCM_S16LE QDMC, PCM_S16LE MOV, PIPE_PROTOCOL) += fate-qdmc