Upstream won't take a testless parser. New api-ac4-parser-test builds
its vectors in code (a minimal version-2 stereo TOC plus payload
fill, CRCs solved per frame), so the suite needs no uploaded samples:
split/resync counts over noise, lookalike, CRC-corrupt, truncated,
ext-len, rate-gated, fs-flipped, ig-cleared, AC40-chain and
sequence-jump shapes, single-frame-at-EOF emits, the bounded
false-lock recovery, bed-bail and hostile-p1 param-untouched cases,
and COMPLETE_FRAMES passthrough, each in whole, 13-byte and 1-byte
feed modes, asserting byte-exact slices, sizes, keyframes, params and
the frame-end-offset return contract. Plus five synth sub-variants:
noise-suffix, mixed40-41, false-embed, ext-split5, rate14-f0.
Vectors also pin the shared-walk reconciliation: fixed-rate rows
0/3/5/8/11 (factor/fractions shapes, output-domain counts), cm
14/252 escapes, and the IMS pres_ver-2 presented counts.
Wired as fate-ac4-parser behind CONFIG_AC4_PARSER.

Stacks on Series E: appends to the stack tests/fate/ac4.mak
(and extends the fate-ac4 aggregate) instead of creating it.

Signed-off-by: Michael Rowlinson <[email protected]>
---
 tests/api/Makefile              |    1 +
 tests/api/api-ac4-parser-test.c | 1007 +++++++++++++++++++++++++++++++
 tests/fate/ac4.mak              |   13 +-
 3 files changed, 1020 insertions(+), 1 deletion(-)
 create mode 100644 tests/api/api-ac4-parser-test.c

diff --git a/tests/api/Makefile b/tests/api/Makefile
index e51dc6c..751c84e 100644
--- a/tests/api/Makefile
+++ b/tests/api/Makefile
@@ -10,6 +10,7 @@ APITESTPROGS-$(call DEMDEC, H263, H263) += api-band
 APITESTPROGS-$(HAVE_THREADS) += api-threadmessage
 APITESTPROGS-$(CONFIG_AVCODEC) += api-enc-info
 APITESTPROGS-$(call ALLYES, H261_ENCODER H261_PARSER) += api-enc-parser
+APITESTPROGS-$(CONFIG_AC4_PARSER) += api-ac4-parser
 APITESTPROGS += $(APITESTPROGS-yes)
 
 APITESTOBJS  := $(APITESTOBJS:%=$(APITESTSDIR)%) 
$(APITESTPROGS:%=$(APITESTSDIR)/%-test.o)
diff --git a/tests/api/api-ac4-parser-test.c b/tests/api/api-ac4-parser-test.c
new file mode 100644
index 0000000..74d4494
--- /dev/null
+++ b/tests/api/api-ac4-parser-test.c
@@ -0,0 +1,1007 @@
+/*
+ * AC-4 parser test.
+ * Copyright (c) 2026 Michael Rowlinson
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * Self-contained tests for ff_ac4_parser. All vectors are synthesized
+ * in code (a minimal bitstream-version-2 stereo TOC plus payload
+ * fill), so the test needs no fate-suite samples: split/resync
+ * counts, byte-exact slices, per-frame sizes, keyframes and params,
+ * the frame-end-offset return contract, and COMPLETE_FRAMES
+ * passthrough, each in whole, 13-byte and 1-byte feed modes.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+
+#include "libavcodec/avcodec.h"
+#include "libavutil/channel_layout.h"
+#include "libavutil/mem.h"
+
+#define PAD AV_INPUT_BUFFER_PADDING_SIZE
+
+static int failures = 0;
+#define CHECK(cond, ...) do { \
+    if (!(cond)) { \
+        printf("FAIL: " __VA_ARGS__); printf("\n"); failures++; \
+    } \
+} while (0)
+
+/* Deterministic noise without stray sync bytes. */
+static uint32_t lcg_state;
+static void noise(uint8_t *d, int n, uint32_t seed)
+{
+    lcg_state = seed;
+    for (int i = 0; i < n; i++) {
+        int b;
+        lcg_state = lcg_state * 1103515245u + 12345u;
+        b = (lcg_state >> 16) & 0xFF;
+        d[i] = (b == 0xAC) ? 0xAD : b;
+    }
+}
+
+/* CRC-16, MSB-first, poly 0x8005, init 0 (test-local copy of the
+ * sync-layer rule: accept on zero remainder over the span). */
+static unsigned int crc16_update(unsigned int crc, const uint8_t *p, int len)
+{
+    for (int i = 0; i < len; i++) {
+        crc ^= (unsigned int) p[i] << 8;
+        for (int k = 0; k < 8; k++)
+            crc = (crc & 0x8000) ? ((crc << 1) ^ 0x8005) : (crc << 1);
+        crc &= 0xFFFF;
+    }
+    return crc;
+}
+
+/* Stored word making (span plus word) check to zero. The map from the
+ * 16 stored bits to the final remainder is bijective, so the search
+ * always terminates. */
+static unsigned int crc16_fix(const uint8_t *span, int len)
+{
+    unsigned int r = crc16_update(0, span, len);
+
+    for (int v = 0; v < 65536; v++) {
+        uint8_t tail[2] = { v >> 8, v & 0xFF };
+        if (crc16_update(r, tail, 2) == 0)
+            return v;
+    }
+    return 0;
+}
+
+typedef struct BitW {
+    uint8_t *d;
+    int bit;
+} BitW;
+
+static void put_bits(BitW *w, int n, unsigned int v)
+{
+    for (int i = n - 1; i >= 0; i--) {
+        if ((v >> i) & 1)
+            w->d[w->bit / 8] |= 1 << (7 - w->bit % 8);
+        w->bit++;
+    }
+}
+
+/* Minimal TOC: presentation 0, group 0, one channel-coded bed element.
+ * mult selects the frame_rate_multiply factor (0 = factor 1); pver is
+ * the presentation_version; cm is the bed channel_mode (2, 12-14,
+ * 120, 121, or 252). All other fields take their zero/absent shape, so every
+ * rate row emits its true bit shape including factor/fractions. */
+static int build_toc2(uint8_t *raw, int bv, int seq, int fs, int rate,
+                      int ig, int n_pres, int mult, int pver, int cm)
+{
+    BitW w = { raw, 0 };
+
+    put_bits(&w, 2, bv);
+    put_bits(&w, 10, seq);
+    put_bits(&w, 1, 0);
+    put_bits(&w, 1, fs);
+    put_bits(&w, 4, rate);
+    put_bits(&w, 1, ig);
+    if (n_pres) {
+        put_bits(&w, 1, 1);
+    } else {
+        put_bits(&w, 1, 0);
+        put_bits(&w, 1, 0);
+    }
+    put_bits(&w, 1, 0);
+    if (bv == 2)
+        put_bits(&w, 1, 0);
+    if (!n_pres)
+        return (w.bit + 7) / 8;
+    /* One v1 presentation record, group index 0. */
+    put_bits(&w, 1, 1);
+    for (int i = 0; i < pver; i++)
+        put_bits(&w, 1, 1);
+    put_bits(&w, 1, 0);       /* presentation_version */
+    put_bits(&w, 3, 0);
+    put_bits(&w, 1, 0);
+    /* frame_rate_multiply_info + fractions, rate-row shaped. */
+    if (rate == 2 || rate == 3 || rate == 4) {
+        put_bits(&w, 1, mult ? 1 : 0);
+        if (mult)
+            put_bits(&w, 1, 0);
+    } else if (rate <= 1 || (rate >= 7 && rate <= 9)) {
+        put_bits(&w, 1, mult ? 1 : 0);
+    }
+    if (rate >= 5 && rate <= 9 && !mult) {
+        put_bits(&w, 1, 0);
+    } else if (rate >= 10 && rate <= 12) {
+        put_bits(&w, 1, 0);
+    }
+    put_bits(&w, 2, 0);       /* emdf substream/version/shape: absent */
+    put_bits(&w, 3, 0);
+    put_bits(&w, 1, 0);
+    put_bits(&w, 2, 0);
+    put_bits(&w, 2, 0);
+    put_bits(&w, 1, 0);
+    put_bits(&w, 3, 0);       /* group index */
+    put_bits(&w, 1, 0);
+    put_bits(&w, 1, 0);
+    put_bits(&w, 1, 0);
+    put_bits(&w, 1, 0);
+    put_bits(&w, 2, 0);
+    /* Group 0 head: bed present, then the channel_mode prefix. */
+    put_bits(&w, 1, 0);
+    put_bits(&w, 1, 0);
+    put_bits(&w, 1, 1);
+    put_bits(&w, 1, 1);
+    if (cm == 2) {
+        put_bits(&w, 1, 1);
+        put_bits(&w, 1, 0);
+    } else if (cm >= 12 && cm <= 14) {
+        put_bits(&w, 2, 3);
+        put_bits(&w, 2, cm - 12);
+    } else if (cm == 120 || cm == 121) {
+        put_bits(&w, 2, 3);
+        put_bits(&w, 2, 3);
+        put_bits(&w, 3, cm - 120);
+    } else if (cm == 252) {
+        put_bits(&w, 7, 126);
+        put_bits(&w, 1, 0);
+        put_bits(&w, 4, 0);   /* immersive presence bits */
+    } else {
+        CHECK(0, "builder: bad cm %d", cm);
+        put_bits(&w, 1, 1);
+        put_bits(&w, 1, 0);
+    }
+    if (fs)
+        put_bits(&w, 1, 0);
+    return (w.bit + 7) / 8;
+}
+
+#define RAW_LEN 64
+#define F41_LEN (4 + RAW_LEN + 2)
+#define F40_LEN (4 + RAW_LEN)
+
+/* One synthetic AC41 frame. Mutations by parameter; the CRC always
+ * covers the stored bytes unless corrupt_crc asks to keep a stale one. */
+static int build_f412(uint8_t *d, int bv, int seq, int fs, int rate, int ig,
+                      int n_pres, uint32_t fill_seed, int corrupt_at,
+                      int mult, int pver, int cm)
+{
+    uint8_t raw[RAW_LEN];
+    int head_bytes;
+
+    memset(raw, 0, sizeof(raw));
+    head_bytes = build_toc2(raw, bv, seq, fs, rate, ig, n_pres,
+                            mult, pver, cm);
+    noise(raw + head_bytes, RAW_LEN - head_bytes, fill_seed);
+    d[0] = 0xAC;
+    d[1] = 0x41;
+    d[2] = RAW_LEN >> 8;
+    d[3] = RAW_LEN & 0xFF;
+    memcpy(d + 4, raw, RAW_LEN);
+    {
+        /* CRC over the length bytes plus raw, stored big-endian. */
+        unsigned int crc = crc16_fix(d + 2, 2 + RAW_LEN);
+        d[4 + RAW_LEN]     = crc >> 8;
+        d[4 + RAW_LEN + 1] = crc & 0xFF;
+    }
+    if (corrupt_at >= 0)
+        d[4 + corrupt_at] ^= 0xFF;
+    else if (corrupt_at == -2)
+        d[4 + RAW_LEN] ^= 0x01;
+    return F41_LEN;
+}
+
+static int build_f41(uint8_t *d, int bv, int seq, int fs, int rate, int ig,
+                     int n_pres, uint32_t fill_seed, int corrupt_at)
+{
+    return build_f412(d, bv, seq, fs, rate, ig, n_pres, fill_seed,
+                     corrupt_at, 0, 0, 2);
+}
+
+typedef struct {
+    uint8_t *bytes;
+    int nbytes, cap;
+    int nsizes;
+    int sizes[8];
+    int kf[8];
+    int sr[8], dur[8], ch[8];
+    uint64_t mask[8];
+    int first_len;   /* first parse return in whole mode */
+    int consumed;
+} Got;
+
+static void run_stream(const uint8_t *data, int size, int chunk, Got *g)
+{
+    AVCodecParserContext *pc;
+    AVCodecContext *avctx;
+    int off = 0, iters = 0, first = 1;
+    int cap_iters = size * 4 + 100;
+
+    memset(g, 0, sizeof(*g));
+    g->first_len = -1;
+    avctx = avcodec_alloc_context3(NULL);
+    avctx->codec_id = AV_CODEC_ID_AC4;
+    pc = av_parser_init(AV_CODEC_ID_AC4);
+    for (;;) {
+        uint8_t *out = NULL;
+        int out_size = 0, n, len;
+
+        if (off < size)
+            n = chunk > 0 ? (chunk < size - off ? chunk : size - off)
+                          : size - off;
+        else
+            n = 0;
+        len = av_parser_parse2(pc, avctx, &out, &out_size,
+                               data + off, n, AV_NOPTS_VALUE,
+                               AV_NOPTS_VALUE, off);
+        if (first && chunk == 0) {
+            g->first_len = len;
+            first = 0;
+        }
+        if (len < 0 || len > n) {
+            printf("  parser returned %d for %d input bytes\n", len, n);
+            failures++;
+            break;
+        }
+        off += len;
+        if (out_size > 0) {
+            int i = g->nsizes;
+            if (g->nbytes + out_size > g->cap) {
+                g->cap = (g->nbytes + out_size) * 2 + 64;
+                g->bytes = realloc(g->bytes, g->cap);
+            }
+            memcpy(g->bytes + g->nbytes, out, out_size);
+            g->nbytes += out_size;
+            if (i < 8) {
+                g->sizes[i] = out_size;
+                g->kf[i]    = pc->key_frame;
+                g->sr[i]    = avctx->sample_rate;
+                g->dur[i]   = (int) pc->duration;
+                g->ch[i]    = avctx->ch_layout.nb_channels;
+                g->mask[i]  = avctx->ch_layout.order ==
+                              AV_CHANNEL_ORDER_NATIVE
+                              ? avctx->ch_layout.u.mask : 0;
+            }
+            g->nsizes++;
+        }
+        if (n == 0 && out_size == 0)
+            break;
+        if (++iters > cap_iters) {
+            printf("  TOO MANY ITERS (no progress)\n");
+            failures++;
+            break;
+        }
+    }
+    g->consumed = off;
+    av_parser_close(pc);
+    avcodec_free_context(&avctx);
+}
+
+typedef struct {
+    const char *name;
+    int nframes;
+    int sizes[4];
+    int kf[4];
+    int sr[4];       /* -1 = skip (bed bail keeps params untouched) */
+    int firstend;    /* expected first parse return in whole mode */
+} Expect;
+
+static void check_vec(const char *tag, const uint8_t *data, int size,
+                      const Expect *e, int chunk,
+                      const int (*slices)[2], int nslices)
+{
+    Got g;
+    int exp_bytes = 0;
+
+    run_stream(data, size, chunk, &g);
+    for (int i = 0; i < nslices; i++)
+        exp_bytes += slices[i][1] - slices[i][0];
+    CHECK(g.consumed == size, "%s c%d: consumed %d != %d",
+          tag, chunk, g.consumed, size);
+    CHECK(g.nsizes == e->nframes, "%s c%d: frames %d != %d",
+          tag, chunk, g.nsizes, e->nframes);
+    CHECK(g.nbytes == exp_bytes, "%s c%d: bytes %d != %d",
+          tag, chunk, g.nbytes, exp_bytes);
+    if (chunk == 0 && e->firstend >= 0)
+        CHECK(g.first_len == e->firstend, "%s: first return %d != %d",
+              tag, g.first_len, e->firstend);
+    for (int i = 0; i < e->nframes && i < g.nsizes; i++)
+        CHECK(g.sizes[i] == e->sizes[i], "%s c%d: f%d size %d != %d",
+              tag, chunk, i, g.sizes[i], e->sizes[i]);
+    {
+        int p = 0;
+        for (int i = 0; i < nslices; i++) {
+            int a = slices[i][0], b = slices[i][1];
+            if (p + (b - a) <= g.nbytes && b <= size)
+                CHECK(memcmp(g.bytes + p, data + a, b - a) == 0,
+                      "%s c%d: slice [%d,%d) mismatch", tag, chunk, a, b);
+            p += b - a;
+        }
+    }
+    for (int i = 0; i < e->nframes && i < g.nsizes; i++) {
+        CHECK(g.kf[i] == e->kf[i], "%s c%d: f%d kf %d != %d",
+              tag, chunk, i, g.kf[i], e->kf[i]);
+        if (e->sr[i] < 0)
+            continue;
+        CHECK(g.sr[i] == e->sr[i], "%s c%d: f%d sr %d != %d",
+              tag, chunk, i, g.sr[i], e->sr[i]);
+        CHECK(g.dur[i] == 2048, "%s c%d: f%d dur %d != 2048",
+              tag, chunk, i, g.dur[i]);
+        CHECK(g.ch[i] == 2, "%s c%d: f%d ch %d != 2",
+              tag, chunk, i, g.ch[i]);
+        CHECK(g.mask[i] == (uint64_t) AV_CH_LAYOUT_STEREO,
+              "%s c%d: f%d not stereo", tag, chunk, i);
+    }
+    free(g.bytes);
+}
+
+static void run_case(const char *tag, uint8_t *data, int size,
+                     const Expect *e, const int (*slices)[2], int nslices)
+{
+    static const int chunks[] = { 0, 13, 1 };
+    for (unsigned i = 0; i < sizeof(chunks) / sizeof(chunks[0]); i++)
+        check_vec(tag, data, size, e, chunks[i], slices, nslices);
+}
+
+/* Single-frame param pin with explicit rate/duration/channels/mask. */
+static void run_param_case(const char *tag, uint8_t *data, int size,
+                           int exp_sr, int exp_dur, int exp_ch,
+                           uint64_t exp_mask, int exp_kf)
+{
+    static const int chunks[] = { 0, 13, 1 };
+
+    for (unsigned ci = 0; ci < sizeof(chunks) / sizeof(chunks[0]); ci++) {
+        Got g;
+        int chunk = chunks[ci];
+
+        run_stream(data, size, chunk, &g);
+        CHECK(g.consumed == size, "%s c%d: consumed %d != %d",
+              tag, chunk, g.consumed, size);
+        CHECK(g.nsizes == 1, "%s c%d: frames %d != 1",
+              tag, chunk, g.nsizes);
+        if (g.nsizes < 1) {
+            free(g.bytes);
+            continue;
+        }
+        CHECK(g.sizes[0] == size, "%s c%d: size %d != %d",
+              tag, chunk, g.sizes[0], size);
+        CHECK(g.kf[0] == exp_kf, "%s c%d: kf %d != %d",
+              tag, chunk, g.kf[0], exp_kf);
+        CHECK(g.sr[0] == exp_sr, "%s c%d: sr %d != %d",
+              tag, chunk, g.sr[0], exp_sr);
+        CHECK(g.dur[0] == exp_dur, "%s c%d: dur %d != %d",
+              tag, chunk, g.dur[0], exp_dur);
+        CHECK(g.ch[0] == exp_ch, "%s c%d: ch %d != %d",
+              tag, chunk, g.ch[0], exp_ch);
+        CHECK(g.mask[0] == exp_mask, "%s c%d: mask 0x%llx != 0x%llx",
+              tag, chunk, (unsigned long long) g.mask[0],
+              (unsigned long long) exp_mask);
+        free(g.bytes);
+    }
+}
+
+static void run_complete(const uint8_t *data, int size, const char *tag,
+                         int exp_kf, int exp_sr)
+{
+    AVCodecParserContext *pc;
+    AVCodecContext *avctx;
+    uint8_t *out = NULL;
+    int out_size = 0, len;
+
+    avctx = avcodec_alloc_context3(NULL);
+    avctx->codec_id = AV_CODEC_ID_AC4;
+    pc = av_parser_init(AV_CODEC_ID_AC4);
+    pc->flags |= PARSER_FLAG_COMPLETE_FRAMES;
+    len = av_parser_parse2(pc, avctx, &out, &out_size, data, size,
+                           AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
+    CHECK(len == size, "%s: consumed %d != %d", tag, len, size);
+    CHECK(out_size == size, "%s: passthrough %d != %d", tag, out_size, size);
+    if (out_size == size && size > 0)
+        CHECK(memcmp(out, data, size) == 0, "%s: bytes mutated", tag);
+    CHECK(pc->key_frame == exp_kf, "%s: kf %d != %d",
+          tag, pc->key_frame, exp_kf);
+    if (exp_sr > 0) {
+        CHECK(avctx->sample_rate == exp_sr, "%s: sr %d != %d",
+              tag, avctx->sample_rate, exp_sr);
+        CHECK(pc->duration == 2048, "%s: dur %d != 2048", tag,
+              (int) pc->duration);
+        CHECK(avctx->ch_layout.nb_channels == 2, "%s: ch %d != 2",
+              tag, avctx->ch_layout.nb_channels);
+    } else if (size > 0) {
+        CHECK(avctx->sample_rate == 0, "%s: sr touched %d", tag,
+              avctx->sample_rate);
+    }
+    av_parser_close(pc);
+    avcodec_free_context(&avctx);
+}
+
+/* Standard stereo frame: bv2, rate 13 (48 kHz), iframe. */
+static int std_frame(uint8_t *d, int seq, uint32_t seed)
+{
+    return build_f41(d, 2, seq, 1, 13, 1, 1, seed, -1);
+}
+
+int main(void)
+{
+    static uint8_t data[4096 + PAD];
+    uint8_t *pad = data + 4096;
+
+    memset(pad, 0, PAD);
+
+    /* Three-frame chain, the base of most vectors. */
+    {
+        static const Expect e =
+            { "chain3", 3, { 70, 70, 70 }, { 1, 1, 1 },
+              { 48000, 48000, 48000 }, 70 };
+        static const int slices[][2] = { { 0, 210 } };
+        int p = 0;
+        p += std_frame(data + p, 0, 101);
+        p += std_frame(data + p, 1, 102);
+        p += std_frame(data + p, 2, 103);
+        run_case("chain3", data, p, &e, slices, 1);
+    }
+
+    /* Noise around and between frames still yields the chain. */
+    {
+        static const Expect e =
+            { "noise", 3, { 70, 70, 70 }, { 1, 1, 1 },
+              { 48000, 48000, 48000 }, 64 + 70 };
+        static const int slices[][2] = { { 64, 134 }, { 171, 311 } };
+        int p = 0;
+        noise(data + p, 64, 11);
+        p += 64;
+        p += std_frame(data + p, 0, 101);
+        noise(data + p, 37, 22);
+        p += 37;
+        p += std_frame(data + p, 1, 102);
+        p += std_frame(data + p, 2, 103);
+        run_case("noise", data, p, &e, slices, 2);
+    }
+
+    /* Trailing noise after the last emit drops (t03 shape). */
+    {
+        static const Expect e =
+            { "noise-suffix", 3, { 70, 70, 70 }, { 1, 1, 1 },
+              { 48000, 48000, 48000 }, 70 };
+        static const int slices[][2] = { { 0, 210 } };
+        int p = 0;
+        p += std_frame(data + p, 0, 101);
+        p += std_frame(data + p, 1, 102);
+        p += std_frame(data + p, 2, 103);
+        noise(data + p, 100, 33);
+        p += 100;
+        run_case("noise-suffix", data, p, &e, slices, 1);
+    }
+
+    /* Byte patterns that look sync-adjacent never match the marker. */
+    {
+        static const Expect e =
+            { "lookalike", 3, { 70, 70, 70 }, { 1, 1, 1 },
+              { 48000, 48000, 48000 }, 274 };
+        static const int slices[][2] = { { 64, 274 } };
+        int p = 0;
+        for (int i = 0; i < 16; i++) {
+            data[p++] = 0x40;
+            data[p++] = 0xAC;
+            data[p++] = 0x41;
+            data[p++] = 0xAC;
+        }
+        p += std_frame(data + p, 0, 101);
+        p += std_frame(data + p, 1, 102);
+        p += std_frame(data + p, 2, 103);
+        run_case("lookalike", data, p, &e, slices, 1);
+    }
+
+    /* Corrupt payload with the original CRC: the frame drops. */
+    {
+        static const Expect e =
+            { "crc-mid", 2, { 70, 70 }, { 1, 1 },
+              { 48000, 48000 }, 70 };
+        static const int slices[][2] = { { 0, 70 }, { 140, 210 } };
+        int p = 0;
+        p += std_frame(data + p, 0, 101);
+        p += build_f41(data + p, 2, 1, 1, 13, 1, 1, 102, 10);
+        p += std_frame(data + p, 2, 103);
+        run_case("crc-mid", data, p, &e, slices, 2);
+    }
+
+    /* All CRCs bad: nothing, clean EOF, no hang. */
+    {
+        static const Expect e = { "crc-all", 0, { 0 }, { 0 }, { 0 }, 210 };
+        int p = 0;
+        p += build_f41(data + p, 2, 0, 1, 13, 1, 1, 101, 50);
+        p += build_f41(data + p, 2, 1, 1, 13, 1, 1, 102, 50);
+        p += build_f41(data + p, 2, 2, 1, 13, 1, 1, 103, 50);
+        run_case("crc-all", data, p, &e, NULL, 0);
+    }
+
+    /* A corrupt stored word drops only its own frame. */
+    {
+        static const Expect e =
+            { "crc-field", 2, { 70, 70 }, { 1, 1 },
+              { 48000, 48000 }, 140 };
+        static const int slices[][2] = { { 70, 210 } };
+        int p = 0;
+        p += build_f41(data + p, 2, 0, 1, 13, 1, 1, 101, -2);
+        p += std_frame(data + p, 1, 102);
+        p += std_frame(data + p, 2, 103);
+        run_case("crc-field", data, p, &e, slices, 1);
+    }
+
+    /* Truncations: complete frames emit, stubs vanish. */
+    {
+        static const Expect e =
+            { "trunc-tail", 2, { 70, 70 }, { 1, 1 },
+              { 48000, 48000 }, 70 };
+        static const int slices[][2] = { { 0, 140 } };
+        int p = 0;
+        p += std_frame(data + p, 0, 101);
+        p += std_frame(data + p, 1, 102);
+        p += std_frame(data + p, 2, 103);
+        run_case("trunc-tail", data, 170, &e, slices, 1);
+    }
+    {
+        static const Expect e =
+            { "trunc-head", 2, { 70, 70 }, { 1, 1 },
+              { 48000, 48000 }, 70 };
+        static const int slices[][2] = { { 0, 140 } };
+        int p = 0;
+        p += std_frame(data + p, 0, 101);
+        p += std_frame(data + p, 1, 102);
+        p += std_frame(data + p, 2, 103);
+        run_case("trunc-head", data, 143, &e, slices, 1);
+    }
+    {
+        static const Expect e = { "stub", 0, { 0 }, { 0 }, { 0 }, 40 };
+        int p = 0;
+        p += std_frame(data + p, 0, 101);
+        run_case("stub", data, 40, &e, NULL, 0);
+    }
+    {
+        /* Extended-length header with no payload behind it. */
+        static const Expect e = { "ext-stub", 0, { 0 }, { 0 }, { 0 }, 7 };
+        static const uint8_t stub[7] =
+            { 0xAC, 0x41, 0xFF, 0xFF, 0x00, 0x00, 0x40 };
+        memcpy(data, stub, sizeof(stub));
+        run_case("ext-stub", data, 7, &e, NULL, 0);
+    }
+
+    /* Extended length carrying a real frame splits wider. */
+    {
+        static const Expect e =
+            { "extlen", 3, { 73, 70, 70 }, { 1, 1, 1 },
+              { 48000, 48000, 48000 }, 73 };
+        static const int slices[][2] = { { 0, 213 } };
+        uint8_t f0[F41_LEN];
+        int p = 0, raw_len = RAW_LEN;
+        std_frame(f0, 0, 101);
+        data[p++] = 0xAC;
+        data[p++] = 0x41;
+        data[p++] = 0xFF;
+        data[p++] = 0xFF;
+        data[p++] = (raw_len >> 16) & 0xFF;
+        data[p++] = (raw_len >> 8) & 0xFF;
+        data[p++] = raw_len & 0xFF;
+        memcpy(data + p, f0 + 4, raw_len);
+        p += raw_len;
+        {
+            /* CRC over the 5 length bytes plus raw. */
+            unsigned int crc = crc16_fix(data + 2, 5 + raw_len);
+            data[p++] = crc >> 8;
+            data[p++] = crc & 0xFF;
+        }
+        p += std_frame(data + p, 1, 102);
+        p += std_frame(data + p, 2, 103);
+        run_case("extlen", data, p, &e, slices, 1);
+    }
+
+    /* A length claim past EOF drops the head and resyncs behind it.
+     * The absurd claim rejects before buffering (n1), so the first
+     * call already emits f1: 73 skipped + 70 emitted. */
+    {
+        static const Expect e =
+            { "extlen-huge", 2, { 70, 70 }, { 1, 1 },
+              { 48000, 48000 }, 143 };
+        static const int slices[][2] = { { 73, 213 } };
+        uint8_t f0[F41_LEN];
+        int p = 0;
+        std_frame(f0, 0, 101);
+        data[p++] = 0xAC;
+        data[p++] = 0x41;
+        data[p++] = 0xFF;
+        data[p++] = 0xFF;
+        data[p++] = 0xFF;
+        data[p++] = 0xFF;
+        data[p++] = 0xFF;
+        memcpy(data + p, f0 + 4, RAW_LEN);
+        p += RAW_LEN;
+        data[p++] = f0[4 + RAW_LEN];
+        data[p++] = f0[4 + RAW_LEN + 1];
+        p += std_frame(data + p, 1, 102);
+        p += std_frame(data + p, 2, 103);
+        run_case("extlen-huge", data, p, &e, slices, 1);
+    }
+
+    /* A 5-byte split extended header (t13 shape): want-more-data,
+     * then nothing at EOF. */
+    {
+        static const Expect e = { "ext-split5", 0, { 0 }, { 0 }, { 0 }, 5 };
+        static const uint8_t stub[5] = { 0xAC, 0x41, 0xFF, 0xFF, 0x00 };
+        memcpy(data, stub, sizeof(stub));
+        run_case("ext-split5", data, 5, &e, NULL, 0);
+    }
+
+    /* Reserved rate rows fail the TOC gate; the chain survives. */
+    for (int rate = 14; rate <= 15; rate++) {
+        char tag[16];
+        static const int slices[][2] = { { 0, 70 }, { 140, 210 } };
+        Expect e = { tag, 2, { 70, 70 }, { 1, 1 },
+                     { 48000, 48000 }, 70 };
+        int p = 0;
+        snprintf(tag, sizeof(tag), "rate%d", rate);
+        p += std_frame(data + p, 0, 101);
+        p += build_f41(data + p, 2, 1, 1, rate, 1, 1, 102, -1);
+        p += std_frame(data + p, 2, 103);
+        run_case(tag, data, p, &e, slices, 2);
+    }
+
+    /* A rate reject on the first frame still resyncs (t17b shape). */
+    {
+        static const Expect e =
+            { "rate14-f0", 2, { 70, 70 }, { 1, 1 },
+              { 48000, 48000 }, 140 };
+        static const int slices[][2] = { { 70, 210 } };
+        int p = 0;
+        p += build_f41(data + p, 2, 0, 1, 14, 1, 1, 101, -1);
+        p += std_frame(data + p, 1, 102);
+        p += std_frame(data + p, 2, 103);
+        run_case("rate14-f0", data, p, &e, slices, 1);
+    }
+
+    /* The 44.1 kHz grid flag switches the reported rate. */
+    {
+        static const Expect e =
+            { "fs-flip", 3, { 70, 70, 70 }, { 1, 1, 1 },
+              { 48000, 44100, 48000 }, 70 };
+        static const int slices[][2] = { { 0, 210 } };
+        int p = 0;
+        p += std_frame(data + p, 0, 101);
+        p += build_f41(data + p, 2, 1, 0, 13, 1, 1, 102, -1);
+        p += std_frame(data + p, 2, 103);
+        run_case("fs-flip", data, p, &e, slices, 1);
+    }
+
+    /* A cleared iframe bit clears the keyframe flag only. */
+    {
+        static const Expect e =
+            { "ig-clear", 3, { 70, 70, 70 }, { 0, 1, 1 },
+              { 48000, 48000, 48000 }, 70 };
+        static const int slices[][2] = { { 0, 210 } };
+        int p = 0;
+        p += build_f41(data + p, 2, 0, 1, 13, 0, 1, 101, -1);
+        p += std_frame(data + p, 1, 102);
+        p += std_frame(data + p, 2, 103);
+        run_case("ig-clear", data, p, &e, slices, 1);
+    }
+
+    /* Bare chains anchor head-to-head with no checksums. */
+    {
+        static const Expect e =
+            { "ac40-chain", 3, { 68, 68, 68 }, { 1, 1, 1 },
+              { 48000, 48000, 48000 }, 68 };
+        static const int slices[][2] = { { 0, 204 } };
+        uint8_t f[F41_LEN];
+        int p = 0;
+        for (int i = 0; i < 3; i++) {
+            std_frame(f, i, 101 + i);
+            data[p++] = 0xAC;
+            data[p++] = 0x40;
+            memcpy(data + p, f + 2, 2 + RAW_LEN);
+            p += 2 + RAW_LEN;
+        }
+        run_case("ac40-chain", data, p, &e, slices, 1);
+    }
+
+    /* A mixed bare/checksummed chain anchors across kinds (t26). */
+    {
+        static const Expect e =
+            { "mixed40-41", 3, { 68, 70, 70 }, { 1, 1, 1 },
+              { 48000, 48000, 48000 }, 68 };
+        static const int slices[][2] = { { 0, 208 } };
+        uint8_t f[F41_LEN];
+        int p = 0;
+        std_frame(f, 0, 101);
+        data[p++] = 0xAC;
+        data[p++] = 0x40;
+        memcpy(data + p, f + 2, 2 + RAW_LEN);
+        p += 2 + RAW_LEN;
+        p += std_frame(data + p, 1, 102);
+        p += std_frame(data + p, 2, 103);
+        run_case("mixed40-41", data, p, &e, slices, 1);
+    }
+
+    /* A lone frame ending exactly at EOF still emits (TOR-1). */
+    {
+        static const Expect e =
+            { "single41", 1, { 70 }, { 1 }, { 48000 }, 70 };
+        static const int slices[][2] = { { 0, 70 } };
+        uint8_t f[F41_LEN];
+        std_frame(f, 1, 102);
+        run_case("single41", f, 70, &e, slices, 1);
+    }
+    {
+        static const Expect e =
+            { "single40", 1, { 68 }, { 1 }, { 48000 }, 68 };
+        static const int slices[][2] = { { 0, 68 } };
+        uint8_t f[F41_LEN], b[F40_LEN];
+        std_frame(f, 1, 102);
+        b[0] = 0xAC;
+        b[1] = 0x40;
+        memcpy(b + 2, f + 2, 2 + RAW_LEN);
+        run_case("single40", b, 68, &e, slices, 1);
+    }
+
+    /* A bare frame with neither anchor nor EOF behind it drops. */
+    {
+        static const Expect e = { "naked40", 0, { 0 }, { 0 }, { 0 }, 128 };
+        uint8_t f[F41_LEN];
+        int p = 0;
+        std_frame(f, 1, 102);
+        data[p++] = 0xAC;
+        data[p++] = 0x40;
+        memcpy(data + p, f + 2, 2 + RAW_LEN);
+        p += 2 + RAW_LEN;
+        noise(data + p, 60, 55);
+        p += 60;
+        run_case("naked40", data, p, &e, NULL, 0);
+    }
+
+    /* A bare head embedded in noise with no anchor drops (t24). */
+    {
+        static const Expect e = { "false-embed", 0, { 0 }, { 0 }, { 0 }, 104 };
+        int p = 0;
+        noise(data + p, 20, 66);
+        p += 20;
+        data[p++] = 0xAC;
+        data[p++] = 0x40;
+        data[p++] = 0x00;
+        data[p++] = 0x32;
+        noise(data + p, 80, 77);
+        p += 80;
+        run_case("false-embed", data, p, &e, NULL, 0);
+    }
+
+    /* The pathological bare embed false-locks but recovers bounded
+     * (TOR-2): the bogus frame emits, then the chain re-locks. */
+    {
+        static const Expect e =
+            { "falsechain", 3, { 100, 70, 70 }, { 1, 1, 1 },
+              { -1, 48000, 48000 }, 110 };
+        static const int slices[][2] = { { 10, 250 } };
+        uint8_t f[F41_LEN];
+        int p = 0;
+        std_frame(f, 1, 102);
+        noise(data + p, 10, 88);
+        p += 10;
+        data[p++] = 0xAC;
+        data[p++] = 0x40;
+        data[p++] = 0;
+        data[p++] = 96;
+        memcpy(data + p, f + 4, 7);   /* plausible head, garbage tail */
+        p += 7;
+        noise(data + p, 89, 89);
+        p += 89;
+        memcpy(data + p, f, 70);
+        p += 70;
+        std_frame(data + p, 2, 103);
+        p += 70;
+        run_case("falsechain", data, p, &e, slices, 1);
+    }
+
+    /* The sequence counter never rejects. */
+    {
+        static const Expect e =
+            { "seq", 3, { 70, 70, 70 }, { 1, 1, 1 },
+              { 48000, 48000, 48000 }, 70 };
+        static const int slices[][2] = { { 0, 210 } };
+        int p = 0;
+        p += build_f41(data + p, 2, 0, 1, 13, 1, 1, 101, -1);
+        p += build_f41(data + p, 2, 500, 1, 13, 1, 1, 102, -1);
+        p += std_frame(data + p, 2, 103);
+        run_case("seq", data, p, &e, slices, 1);
+    }
+
+    /* A bed walk that bails leaves params untouched past key_frame. */
+    {
+        static const Expect e =
+            { "bed-bail", 1, { 70 }, { 1 }, { -1 }, 70 };
+        static const int slices[][2] = { { 0, 70 } };
+        uint8_t f[F41_LEN];
+        build_f41(f, 2, 0, 1, 13, 1, 0, 101, -1);
+        run_case("bed-bail", f, 70, &e, slices, 1);
+        /* sr/dur/ch must read back zero on the emitted frame. */
+        {
+            AVCodecParserContext *pc;
+            AVCodecContext *avctx;
+            uint8_t *out = NULL;
+            int out_size = 0;
+            avctx = avcodec_alloc_context3(NULL);
+            avctx->codec_id = AV_CODEC_ID_AC4;
+            pc = av_parser_init(AV_CODEC_ID_AC4);
+            av_parser_parse2(pc, avctx, &out, &out_size, f, 70,
+                             AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
+            CHECK(out_size == 70, "bed-bail: out %d", out_size);
+            CHECK(pc->key_frame == 1, "bed-bail: kf %d", pc->key_frame);
+            CHECK(avctx->sample_rate == 0, "bed-bail: sr %d",
+                  avctx->sample_rate);
+            CHECK(pc->duration == 0, "bed-bail: dur %d",
+                  (int) pc->duration);
+            CHECK(avctx->ch_layout.nb_channels == 0, "bed-bail: ch %d",
+                  avctx->ch_layout.nb_channels);
+            av_parser_close(pc);
+            avcodec_free_context(&avctx);
+        }
+    }
+
+    /* A version-1 frame whose presentation record truncates after
+     * the head: the bed walk bails on budget, the frame still emits,
+     * and params stay untouched past key_frame. */
+    {
+        static const Expect e =
+            { "p1hostile", 1, { 9 }, { 1 }, { -1 }, 9 };
+        static const int slices[][2] = { { 0, 9 } };
+        uint8_t f[9] = { 0xAC, 0x41, 0, 3, 0, 0, 0, 0, 0 };
+        BitW w = { f + 4, 0 };
+        put_bits(&w, 2, 1);
+        put_bits(&w, 10, 7);
+        put_bits(&w, 1, 0);
+        put_bits(&w, 1, 1);
+        put_bits(&w, 4, 5);
+        put_bits(&w, 1, 1);
+        put_bits(&w, 1, 1);
+        put_bits(&w, 1, 0);
+        {
+            unsigned int crc = crc16_fix(f + 2, 5);
+            f[7] = crc >> 8;
+            f[8] = crc & 0xFF;
+        }
+        run_case("p1hostile", f, 9, &e, slices, 1);
+        {
+            AVCodecParserContext *pc;
+            AVCodecContext *avctx;
+            uint8_t *out = NULL;
+            int out_size = 0;
+            avctx = avcodec_alloc_context3(NULL);
+            avctx->codec_id = AV_CODEC_ID_AC4;
+            pc = av_parser_init(AV_CODEC_ID_AC4);
+            av_parser_parse2(pc, avctx, &out, &out_size, f, 9,
+                             AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
+            CHECK(out_size == 9, "p1hostile: out %d", out_size);
+            CHECK(pc->key_frame == 1, "p1hostile: kf %d", pc->key_frame);
+            CHECK(avctx->sample_rate == 0, "p1hostile: sr %d",
+                  avctx->sample_rate);
+            CHECK(pc->duration == 0, "p1hostile: dur %d",
+                  (int) pc->duration);
+            CHECK(avctx->ch_layout.nb_channels == 0, "p1hostile: ch %d",
+                  avctx->ch_layout.nb_channels);
+            av_parser_close(pc);
+            avcodec_free_context(&avctx);
+        }
+    }
+
+    /* Noise-only and empty inputs: nothing, clean EOF, no hang. */
+    {
+        static const Expect e0 = { "empty", 0, { 0 }, { 0 }, { 0 }, 0 };
+        static const Expect e7 = { "noise-only", 0, { 0 }, { 0 }, { 0 }, 256 };
+        noise(data, 256, 44);
+        run_case("noise-only", data, 256, &e7, NULL, 0);
+        run_case("empty", data, 0, &e0, NULL, 0);
+    }
+
+    /* COMPLETE_FRAMES passes raw frames through and only probes. */
+    {
+        uint8_t f[F41_LEN];
+        std_frame(f, 0, 101);
+        run_complete(f + 4, RAW_LEN, "complete/raw", 1, 48000);
+        /* All-ones fail the head parse (version escape overflows). */
+        memset(data, 0xFF, 40);
+        run_complete(data, 40, "complete/garbage", -1, 0);
+        run_complete(NULL, 0, "complete/empty", -1, 0);
+    }
+
+    /* Fixed-rate rows report output-domain rate/counts, not the grid. */
+    {
+        uint8_t f[F41_LEN];
+        /* Row 3 (29.97 fps): 2-bit factor shape, 1601.6 nominal. */
+        build_f412(f, 2, 0, 1, 3, 1, 1, 111, -1, 0, 0, 2);
+        run_param_case("rate3", f, F41_LEN, 48000, 1602, 2,
+                       AV_CH_LAYOUT_STEREO, 1);
+        /* Same row with factor 2: value unused, alignment identical. */
+        build_f412(f, 2, 0, 1, 3, 1, 1, 112, -1, 1, 0, 2);
+        run_param_case("rate3-mult", f, F41_LEN, 48000, 1602, 2,
+                       AV_CH_LAYOUT_STEREO, 1);
+        /* Row 8 (59.94 fps): 1-bit factor plus the fractions bit. */
+        build_f412(f, 2, 0, 1, 8, 1, 1, 113, -1, 0, 0, 2);
+        run_param_case("rate8", f, F41_LEN, 48000, 801, 2,
+                       AV_CH_LAYOUT_STEREO, 1);
+        /* Same row with factor 2: fractions absent, walk skips them. */
+        build_f412(f, 2, 0, 1, 8, 1, 1, 114, -1, 1, 0, 2);
+        run_param_case("rate8-mult", f, F41_LEN, 48000, 801, 2,
+                       AV_CH_LAYOUT_STEREO, 1);
+        /* Row 11 (119.88 fps): fractions only, no factor bits. */
+        build_f412(f, 2, 0, 1, 11, 1, 1, 115, -1, 0, 0, 2);
+        run_param_case("rate11", f, F41_LEN, 48000, 400, 2,
+                       AV_CH_LAYOUT_STEREO, 1);
+        /* Row 0 (23.976 fps): 1-bit factor, no fractions. */
+        build_f412(f, 2, 0, 1, 0, 1, 1, 116, -1, 0, 0, 2);
+        run_param_case("rate0", f, F41_LEN, 48000, 2002, 2,
+                       AV_CH_LAYOUT_STEREO, 1);
+        /* Row 5 (48 fps): no factor bits, one fractions bit. */
+        build_f412(f, 2, 0, 1, 5, 1, 1, 117, -1, 0, 0, 2);
+        run_param_case("rate5", f, F41_LEN, 48000, 1001, 2,
+                       AV_CH_LAYOUT_STEREO, 1);
+    }
+
+    /* Wide channel modes pin the shared bed map. */
+    {
+        uint8_t f[F41_LEN];
+        /* cm 252: 8-bit escape, 11 channels, unspecified layout. */
+        build_f412(f, 2, 0, 1, 13, 1, 1, 121, -1, 0, 0, 252);
+        run_param_case("cm252", f, F41_LEN, 48000, 2048, 11, 0, 1);
+        /* IMS pres_ver 2: cm 120 presents 5.1, cm 121 stereo. */
+        build_f412(f, 2, 0, 1, 13, 1, 1, 122, -1, 0, 2, 120);
+        run_param_case("ims120", f, F41_LEN, 48000, 2048, 6,
+                       AV_CH_LAYOUT_5POINT1, 1);
+        build_f412(f, 2, 0, 1, 13, 1, 1, 123, -1, 0, 2, 121);
+        run_param_case("ims121", f, F41_LEN, 48000, 2048, 2,
+                       AV_CH_LAYOUT_STEREO, 1);
+        /* Non-IMS cm 121: plain 8-channel bed, 7.1 layout. */
+        build_f412(f, 2, 0, 1, 13, 1, 1, 124, -1, 0, 0, 121);
+        run_param_case("cm121", f, F41_LEN, 48000, 2048, 8,
+                       AV_CH_LAYOUT_7POINT1, 1);
+        /* cm 14: 4-bit prefix, 6 channels, 5.1 layout. */
+        build_f412(f, 2, 0, 1, 13, 1, 1, 125, -1, 0, 0, 14);
+        run_param_case("cm14", f, F41_LEN, 48000, 2048, 6,
+                       AV_CH_LAYOUT_5POINT1, 1);
+    }
+
+    if (failures == 0)
+        printf("ALL AC-4 PARSER TESTS PASSED\n");
+    else
+        printf("%d FAILURES\n", failures);
+    return failures != 0;
+}
diff --git a/tests/fate/ac4.mak b/tests/fate/ac4.mak
index 81db40d..41330bc 100644
--- a/tests/fate/ac4.mak
+++ b/tests/fate/ac4.mak
@@ -264,4 +264,15 @@ fate-ac4-mpegts-probe: CMD = run 
ffprobe$(PROGSSUF)$(EXESUF) -v 0 -strict -2 -bi
 
 FATE_FFMPEG += $(FATE_AC4_MPEGTS_PMT-yes)
 FATE_FFPROBE += $(FATE_AC4_MPEGTS_PROBE-yes)
-fate-ac4: $(FATE_AC4-yes) $(FATE_AC4_MD5-yes) $(FATE_AC4_REMUX-yes) 
$(FATE_AC4_ENCNEG-yes) $(FATE_AC4_ENC-yes) $(FATE_AC4_MPEGTS_PMT-yes) 
$(FATE_AC4_MPEGTS_PROBE-yes)
+# AC-4 parser tests. The vectors are synthesized inside the test tool,
+# so this needs no fate-suite samples.
+FATE_AC4_PARSE-$(CONFIG_AC4_PARSER) += fate-ac4-parser
+fate-ac4-parser: $(APITESTSDIR)/api-ac4-parser-test$(EXESUF)
+fate-ac4-parser: CMD = run $(APITESTSDIR)/api-ac4-parser-test$(EXESUF)
+fate-ac4-parser: CMP = null
+
+
+fate-ac4-parse: $(FATE_AC4_PARSE-yes)
+
+FATE-yes += $(FATE_AC4_PARSE-yes)
+fate-ac4: $(FATE_AC4-yes) $(FATE_AC4_MD5-yes) $(FATE_AC4_REMUX-yes) 
$(FATE_AC4_ENCNEG-yes) $(FATE_AC4_ENC-yes) $(FATE_AC4_MPEGTS_PMT-yes) 
$(FATE_AC4_MPEGTS_PROBE-yes) $(FATE_AC4_PARSE-yes)
-- 
2.56.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to