PR #25894 opened by 06needhamt
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25894
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25894.patch

# Summary of changes

Add a decoder for AMAREC **AMV2**, the first generation of the capture codec 
AmaRecTV writes, together with a demuxer for its headerless `.amv2` streams and 
FATE tests.

This is **not** the Actions Media Video read by the `amv` decoder: an AMV2 
stream has no RIFF header and no `AMVF` chunk. It is a plain sequence of `AMDB` 
(keyframe) and `AMDC` (inter frame) records. The decoder reads AVI files with 
the fourcc `AMV2` and, through the new `amv2` demuxer, bare `.amv2` files.

## What is covered

- **Both picture layouts.** RGB is output as `rgb24` and YUY2 as `yuyv422`. 
This includes the YUY2 layout the codec uses for RGB captures at its shifts 3-5.
- **Every frame type:** raw keyframes and the three coded modes. Modes A and B 
are lossless token modes. Mode C is the lossy mode, coded through an auxiliary 
class map. It keeps a wrapping state and shows a clamped picture.
- **Any size the codec takes** (multiples of 8x4), and the stream format's 
interlace flag.
- **Layout and mode** come from the AVI stream format when present, otherwise 
from the data.
- **Decoder option:** `-interlace` (default: the stream format's flag).
- **Demuxer options:** `-video_size` (default 320x240) and `-framerate` 
(default 30).

Pictures are output exactly as the codec keeps them. The vendor's own 
YUY2-to-RGB conversion is not applied; swscale converts from `yuyv422`. 

```fate-samples
amv2/mix-r-fail.amv2
amv2/mix-r-neutral.amv2
amv2/mix-r-prime.amv2
amv2/mix-y-fail.amv2
amv2/mix-y-neutral.amv2
amv2/r1.amv2
amv2/r2-88x44-il.avi
amv2/r2.amv2
amv2/r3-328x244.amv2
amv2/r3-88x44-il.amv2
amv2/r3-88x44-il.avi
amv2/r3.amv2
amv2/rand-a-r.amv2
amv2/rand-a-y-8x4.amv2
amv2/rand-b-y.amv2
amv2/rand-c-dense-r.amv2
amv2/rand-c-dense-y-16x8.amv2
amv2/rand-c-r-8x4.amv2
amv2/rand-c-y.amv2
amv2/y1-88x44.avi
amv2/y1.amv2
amv2/y2-88x44-rgbin.avi
amv2/y2.amv2
amv2/y3-328x244.amv2
amv2/y3-88x44-rgbin-il.avi
amv2/y3.amv2
```


>From 4b46fce020c708383253ca73ea983f3d4546479a Mon Sep 17 00:00:00 2001
From: Tom Needham <[email protected]>
Date: Sat, 31 Oct 2026 20:05:26 +0000
Subject: [PATCH 1/5] avcodec: add AMAREC AMV2 decoder

Add a decoder for AMV2, the first generation of the AMAREC capture codec
written by AmaRecTV.  It is not the Actions Media Video decoded by the
amv decoder: the stream has no RIFF header and no AMVF chunk.

A record is "AMDB" (keyframe) or "AMDC" (inter frame), a size and a
payload, optionally followed by an auxiliary block.  Pictures are kept as
coding units in one of two layouts, RGB (24 byte units over 4x2 pixels)
or YUY2 (32 byte units over 8x2 pixels), and are output unchanged as
RGB24 and YUYV422.  The AVI stream format extradata, when present, gives
the layout and the interlace flag; both can also be told from the data.

This first step handles raw keyframes at any size the codec takes
(multiples of 8x4).  Coded frames follow.

Signed-off-by: Tom Needham <[email protected]>
---
 Changelog                 |   1 +
 doc/APIchanges            |   2 +
 doc/general_contents.texi |   2 +
 libavcodec/Makefile       |   1 +
 libavcodec/allcodecs.c    |   1 +
 libavcodec/amv2dec.c      | 317 ++++++++++++++++++++++++++++++++++++++
 libavcodec/codec_desc.c   |   7 +
 libavcodec/codec_id.h     |   1 +
 libavcodec/version.h      |   2 +-
 libavformat/riff.c        |   1 +
 10 files changed, 334 insertions(+), 1 deletion(-)
 create mode 100644 libavcodec/amv2dec.c

diff --git a/Changelog b/Changelog
index bce79b181f7..570d723aded 100644
--- a/Changelog
+++ b/Changelog
@@ -6,6 +6,7 @@ version <next>:
 - LCEVC raw demuxer and muxer
 - AVS+ (AVS1-P16, Broadcasting profile) support in the Chinese AVS decoder
 - PCM PAMF decoder
+- AMAREC AMV2 video decoder
 
 version 9.1:
 - ASTC image encoding and decoding via libastcenc, with .astc and KTX 1.0 
muxers and demuxers
diff --git a/doc/APIchanges b/doc/APIchanges
index c729b61a44c..53cc46b7cb2 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -2,6 +2,8 @@ The last version increases of all libraries were on 2026-06-23.
 
 API changes, most recent first:
 
+2026-11-xx - xxxxxxxxxx - lavc 63.29.100 - codec_id.h                         
Add AV_CODEC_ID_AMAREC_AMV2.
+
 2026-11-xx - xxxxxxxxxx - lavc 63.28.100 - packet.h
   Add AV_PKT_FLAG_EOS.
 
diff --git a/doc/general_contents.texi b/doc/general_contents.texi
index 0176cd08750..a43c0313809 100644
--- a/doc/general_contents.texi
+++ b/doc/general_contents.texi
@@ -1000,6 +1000,8 @@ following image formats are supported:
 @item American Laser Games MM  @tab    @tab X
     @tab Used in games like Mad Dog McCree.
 @item Amuse Graphics Movie   @tab     @tab  X
+@item AMAREC AMV2            @tab     @tab  X
+    @tab The AmaRecTV capture codec, fourcc AMV2.  Not the Actions Media Video 
decoded as @samp{AMV Video}.
 @item AMV Video              @tab  X  @tab  X
     @tab Used in Chinese MP3 players.
 @item ANSI/ASCII art         @tab     @tab  X
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 4bffc081184..a8f6ecf44af 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -229,6 +229,7 @@ OBJS-$(CONFIG_ALAC_ENCODER)            += alacenc.o 
alac_data.o
 OBJS-$(CONFIG_ALIAS_PIX_DECODER)       += aliaspixdec.o
 OBJS-$(CONFIG_ALIAS_PIX_ENCODER)       += aliaspixenc.o
 OBJS-$(CONFIG_ALS_DECODER)             += alsdec.o bgmc.o mlz.o
+OBJS-$(CONFIG_AMAREC_AMV2_DECODER)     += amv2dec.o
 OBJS-$(CONFIG_AMRNB_DECODER)           += amrnbdec.o celp_filters.o   \
                                           acelp_filters.o \
                                           acelp_vectors.o             \
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index edbe44ecc61..1664e0bdcbf 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -39,6 +39,7 @@ extern const FFCodec ff_aic_decoder;
 extern const FFCodec ff_alias_pix_encoder;
 extern const FFCodec ff_alias_pix_decoder;
 extern const FFCodec ff_agm_decoder;
+extern const FFCodec ff_amarec_amv2_decoder;
 extern const FFCodec ff_amv_encoder;
 extern const FFCodec ff_amv_decoder;
 extern const FFCodec ff_anm_decoder;
diff --git a/libavcodec/amv2dec.c b/libavcodec/amv2dec.c
new file mode 100644
index 00000000000..20b6c5047e3
--- /dev/null
+++ b/libavcodec/amv2dec.c
@@ -0,0 +1,317 @@
+/*
+ * AMAREC AMV2 decoder
+ * Copyright (c) 2026 Tom Needham
+ *
+ * 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
+ * AMAREC AMV2 video decoder.
+ *
+ * AMV2 is the first generation of the capture codec AmaRecTV writes; it is
+ * not the RIFF/MJPEG "Actions Media Video" decoded by the amv decoder.
+ *
+ * A packet is one record, optionally followed by an auxiliary block:
+ *
+ *   uint8  tag[4]      "AMDB" (keyframe) or "AMDC" (inter frame)
+ *   uint32 size        record size including these 8 bytes
+ *   uint8  payload[size - 8]
+ *   uint8  aux[...]    mode C only; begins with its own uint32 length
+ *
+ * Pictures are kept as a stream of coding units.  Two layouts exist, both
+ * 8 pixels wide at a time and bottom-up in the vendor's output buffer:
+ *
+ *   RGB   a 24 byte unit covers 4x2 pixels as B, G and R planes of 8 bytes
+ *   YUY2  a 32 byte unit covers 8x2 pixels as 16 luma, 8 Cb and 8 Cr bytes
+ *
+ * The decoder outputs the units as they are: RGB24 for the RGB layout and
+ * YUYV422 for the YUY2 one.
+ */
+
+#include "libavutil/imgutils.h"
+#include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
+#include "libavutil/opt.h"
+
+#include "avcodec.h"
+#include "codec_internal.h"
+#include "decode.h"
+
+#define AMV2_TAG_KEY   MKTAG('A', 'M', 'D', 'B')
+#define AMV2_TAG_INTER MKTAG('A', 'M', 'D', 'C')
+
+/** Largest side the codec takes. */
+#define AMV2_MAX_SIDE  4096
+
+/** A raw frame may be followed by this many zero bytes. */
+#define AMV2_TRAILER   8
+
+enum AMV2Profile {
+    AMV2_PROFILE_UNKNOWN = -1,
+    AMV2_PROFILE_RGB,       ///< 24 byte units, 4x2 pixels, RGB24 out
+    AMV2_PROFILE_YUY2,      ///< 32 byte units, 8x2 pixels, YUYV422 out
+};
+
+typedef struct AMV2Context {
+    AVClass *class;
+    int interlace_opt;      ///< user option: -1 from the stream format
+
+    int profile;            ///< enum AMV2Profile
+    int interlace;          ///< swap rows 1 and 2 of every four
+
+    size_t frame_bytes;     ///< bytes of one frame's units
+    uint8_t *units;         ///< the frame's units: the last decoded frame
+} AMV2Context;
+
+/** Frame bytes of a profile at the given size. */
+static size_t amv2_frame_bytes(int profile, int width, int height)
+{
+    return (size_t)width * height * (profile == AMV2_PROFILE_RGB ? 3 : 2);
+}
+
+/**
+ * Pin the profile: allocate its buffers and set the output format.
+ */
+static int amv2_set_profile(AVCodecContext *avctx, int profile)
+{
+    AMV2Context *s = avctx->priv_data;
+
+    s->profile     = profile;
+    s->frame_bytes = amv2_frame_bytes(profile, avctx->width, avctx->height);
+    avctx->pix_fmt = profile == AMV2_PROFILE_RGB ? AV_PIX_FMT_RGB24
+                                                 : AV_PIX_FMT_YUYV422;
+
+    s->units = av_mallocz(s->frame_bytes);
+    if (!s->units)
+        return AVERROR(ENOMEM);
+    return 0;
+}
+
+static av_cold int amv2_decode_init(AVCodecContext *avctx)
+{
+    AMV2Context *s = avctx->priv_data;
+    int ret;
+
+    if (avctx->width  < 8 || avctx->width  % 8 ||
+        avctx->height < 4 || avctx->height % 4 ||
+        avctx->width > AMV2_MAX_SIDE || avctx->height > AMV2_MAX_SIDE) {
+        av_log(avctx, AV_LOG_ERROR, "Unsupported size %dx%d\n",
+               avctx->width, avctx->height);
+        return AVERROR_PATCHWELCOME;
+    }
+    if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
+        return ret;
+
+    s->profile   = AMV2_PROFILE_UNKNOWN;
+    s->interlace = s->interlace_opt > 0;
+
+    /* The stream format an AVI carries after its BITMAPINFOHEADER:
+     * version, shift, input and coded fourcc, flags. */
+    if (avctx->extradata_size >= 20) {
+        const uint8_t *ed = avctx->extradata;
+        const uint32_t coded = AV_RL32(ed + 12);
+        const uint32_t flags = AV_RL32(ed + 16);
+
+        if (coded == MKTAG('Y', '4', '2', '2'))
+            ret = amv2_set_profile(avctx, AMV2_PROFILE_YUY2);
+        else if (coded == MKTAG('R', 'G', '3', '2') ||
+                 coded == MKTAG('Y', '4', '4', '4'))
+            ret = amv2_set_profile(avctx, AMV2_PROFILE_RGB);
+        else
+            ret = 0;
+        if (ret < 0)
+            return ret;
+        if (s->interlace_opt < 0)
+            s->interlace = !!(flags & 2);
+    }
+
+    return 0;
+}
+
+static av_cold int amv2_decode_close(AVCodecContext *avctx)
+{
+    AMV2Context *s = avctx->priv_data;
+
+    av_freep(&s->units);
+    return 0;
+}
+
+/**
+ * RGB units to a picture: a permutation, no arithmetic.
+ *
+ * A pair of 24 byte units carries a 4x2 pixel area as B, G and R planes of
+ * 8 bytes, each holding two 2x2 blocks side by side.  The unit rows run
+ * bottom-up.
+ */
+static void amv2_rgb_to_picture(const uint8_t *units, uint8_t *dst,
+                                ptrdiff_t linesize, int width, int height)
+{
+    for (int br = 0; br < height; br++) {
+        const uint8_t *row = units + (size_t)(br >> 1) * (width / 4) * 24 +
+                             2 * (br & 1);
+        uint8_t *out = dst + (ptrdiff_t)(height - 1 - br) * linesize;
+
+        for (int pc = 0; pc < width / 4; pc++) {
+            const uint8_t *pair = row + (size_t)pc * 24;
+
+            for (int q = 0; q < 2; q++) {
+                for (int k = 0; k < 2; k++) {
+                    const uint8_t *src = pair + 4 * q + k;
+
+                    *out++ = src[16];   /* R */
+                    *out++ = src[8];    /* G */
+                    *out++ = src[0];    /* B */
+                }
+            }
+        }
+    }
+}
+
+/**
+ * YUY2 units to a picture: unit row r covers the picture's rows 2r and
+ * 2r + 1, top-down.  Byte 2i + j of a unit's luma is column i of line j,
+ * and its chroma sits on the same lines, one sample per pixel pair.
+ */
+static void amv2_yuy2_to_picture(const uint8_t *units, uint8_t *dst,
+                                 ptrdiff_t linesize, int width, int height)
+{
+    const int upr = width / 8;
+
+    for (int ur = 0; ur < height / 2; ur++) {
+        for (int j = 0; j < 2; j++) {
+            const uint8_t *row = units + (size_t)ur * upr * 32;
+            uint8_t *out = dst + (ptrdiff_t)(2 * ur + j) * linesize;
+
+            for (int uc = 0; uc < upr; uc++) {
+                const uint8_t *u = row + (size_t)uc * 32;
+
+                for (int i = 0; i < 4; i++) {
+                    *out++ = u[2 * (2 * i)     + j];
+                    *out++ = u[16 + 2 * i      + j];
+                    *out++ = u[2 * (2 * i + 1) + j];
+                    *out++ = u[24 + 2 * i      + j];
+                }
+            }
+        }
+    }
+}
+
+/** Swap rows 1 and 2 of every four. */
+static void amv2_interlace(uint8_t *dst, ptrdiff_t linesize, int row_bytes,
+                           int height)
+{
+    for (int y = 1; y + 1 < height; y += 4) {
+        uint8_t *a = dst + (ptrdiff_t)y * linesize;
+        uint8_t *b = a + linesize;
+
+        for (int x = 0; x < row_bytes; x++)
+            FFSWAP(uint8_t, a[x], b[x]);
+    }
+}
+
+static int amv2_decode_frame(AVCodecContext *avctx, AVFrame *frame,
+                             int *got_frame, AVPacket *avpkt)
+{
+    AMV2Context *s = avctx->priv_data;
+    const uint8_t *data = avpkt->data;
+    size_t size, len, raw_bytes;
+    uint32_t tag;
+    int key, ret;
+
+    if (avpkt->size < 8)
+        return AVERROR_INVALIDDATA;
+    tag = AV_RL32(data);
+    if (tag != AMV2_TAG_KEY && tag != AMV2_TAG_INTER)
+        return AVERROR_INVALIDDATA;
+    key  = tag == AMV2_TAG_KEY;
+    size = AV_RL32(data + 4);
+    if (size < 8 || size > (size_t)avpkt->size)
+        return AVERROR_INVALIDDATA;
+    len  = size - 8;
+    data += 8;
+
+    if (s->profile == AMV2_PROFILE_UNKNOWN) {
+        /* a raw frame says which layout the stream has */
+        for (int p = AMV2_PROFILE_RGB; p <= AMV2_PROFILE_YUY2; p++) {
+            raw_bytes = amv2_frame_bytes(p, avctx->width, avctx->height);
+            if (key && (len == raw_bytes || len == raw_bytes + AMV2_TRAILER)) {
+                if ((ret = amv2_set_profile(avctx, p)) < 0)
+                    return ret;
+                break;
+            }
+        }
+        if (s->profile == AMV2_PROFILE_UNKNOWN)
+            return AVERROR_INVALIDDATA;
+    }
+
+    if (!key || (len != s->frame_bytes && len != s->frame_bytes + 
AMV2_TRAILER))
+        return AVERROR_PATCHWELCOME;
+    memcpy(s->units, data, s->frame_bytes);
+
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
+        return ret;
+
+    if (s->profile == AMV2_PROFILE_RGB)
+        amv2_rgb_to_picture(s->units, frame->data[0], frame->linesize[0],
+                            avctx->width, avctx->height);
+    else
+        amv2_yuy2_to_picture(s->units, frame->data[0], frame->linesize[0],
+                             avctx->width, avctx->height);
+    if (s->interlace)
+        amv2_interlace(frame->data[0], frame->linesize[0],
+                       avctx->width * (s->profile == AMV2_PROFILE_RGB ? 3 : 2),
+                       avctx->height);
+
+    if (key) {
+        frame->flags     |= AV_FRAME_FLAG_KEY;
+        frame->pict_type  = AV_PICTURE_TYPE_I;
+    } else {
+        frame->pict_type  = AV_PICTURE_TYPE_P;
+    }
+    *got_frame = 1;
+    return avpkt->size;
+}
+
+#define OFFSET(x) offsetof(AMV2Context, x)
+#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
+static const AVOption amv2_options[] = {
+    { "interlace", "swap rows 1 and 2 of every four (-1: from the stream 
format)",
+      OFFSET(interlace_opt), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VD },
+    { NULL }
+};
+
+static const AVClass amv2_class = {
+    .class_name = "amarec_amv2",
+    .item_name  = av_default_item_name,
+    .option     = amv2_options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+const FFCodec ff_amarec_amv2_decoder = {
+    .p.name         = "amarec_amv2",
+    CODEC_LONG_NAME("AMAREC AMV2"),
+    .p.type         = AVMEDIA_TYPE_VIDEO,
+    .p.id           = AV_CODEC_ID_AMAREC_AMV2,
+    .p.capabilities = AV_CODEC_CAP_DR1,
+    .p.priv_class   = &amv2_class,
+    .priv_data_size = sizeof(AMV2Context),
+    .init           = amv2_decode_init,
+    .close          = amv2_decode_close,
+    FF_CODEC_DECODE_CB(amv2_decode_frame),
+    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
+};
diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index dd63c2194df..f4f65767adb 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -2017,6 +2017,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
         .long_name = NULL_IF_CONFIG_SMALL("Infinity IMM6"),
         .props     = AV_CODEC_PROP_LOSSY | AV_CODEC_PROP_REORDER,
     },
+    {
+        .id        = AV_CODEC_ID_AMAREC_AMV2,
+        .type      = AVMEDIA_TYPE_VIDEO,
+        .name      = "amarec_amv2",
+        .long_name = NULL_IF_CONFIG_SMALL("AMAREC AMV2"),
+        .props     = AV_CODEC_PROP_LOSSY | AV_CODEC_PROP_LOSSLESS,
+    },
 
     /* various PCM "codecs" */
     {
diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h
index 8c78bbce379..59036610511 100644
--- a/libavcodec/codec_id.h
+++ b/libavcodec/codec_id.h
@@ -327,6 +327,7 @@ enum AVCodecID {
     AV_CODEC_ID_ASTC,
     AV_CODEC_ID_RW2,
     AV_CODEC_ID_IMM6,
+    AV_CODEC_ID_AMAREC_AMV2,
 
     /* various PCM "codecs" */
     AV_CODEC_ID_FIRST_AUDIO = 0x10000,     ///< A dummy id pointing at the 
start of audio codecs
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 87b7284a953..5183deb68bd 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -29,7 +29,7 @@
 
 #include "version_major.h"
 
-#define LIBAVCODEC_VERSION_MINOR  28
+#define LIBAVCODEC_VERSION_MINOR  29
 #define LIBAVCODEC_VERSION_MICRO 100
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
diff --git a/libavformat/riff.c b/libavformat/riff.c
index 50c14ecb661..b3993d17daa 100644
--- a/libavformat/riff.c
+++ b/libavformat/riff.c
@@ -503,6 +503,7 @@ const AVCodecTag ff_codec_bmp_tags[] = {
     { AV_CODEC_ID_LSCR,         MKTAG('L', 'S', 'C', 'R') },
     { AV_CODEC_ID_IMM5,         MKTAG('I', 'M', 'M', '5') },
     { AV_CODEC_ID_IMM6,         MKTAG('I', 'M', 'M', '6') },
+    { AV_CODEC_ID_AMAREC_AMV2,  MKTAG('A', 'M', 'V', '2') },
     { AV_CODEC_ID_MVDV,         MKTAG('M', 'V', 'D', 'V') },
     { AV_CODEC_ID_MVHA,         MKTAG('M', 'V', 'H', 'A') },
     { AV_CODEC_ID_MV30,         MKTAG('M', 'V', '3', '0') },
-- 
2.54.0


>From 2b0ce26c3d2565773373d14bed845c0e2d96d390 Mon Sep 17 00:00:00 2001
From: Tom Needham <[email protected]>
Date: Sat, 31 Oct 2026 20:05:43 +0000
Subject: [PATCH 2/5] avformat: add AMAREC AMV2 raw stream demuxer

Add a demuxer for the headerless record stream the AMV2 codec of AmaRecTV
writes to .amv2 files.  A packet is a record together with the auxiliary
block that follows it in mode C, which opens with its own length; failing
that length the block runs to the next record tag.

The stream records neither its picture size nor its frame rate, so both
are options (video_size, default 320x240, and framerate, default 30).

Signed-off-by: Tom Needham <[email protected]>
---
 Changelog                 |   1 +
 doc/demuxers.texi         |  18 +++
 doc/general_contents.texi |   2 +
 libavformat/Makefile      |   1 +
 libavformat/allformats.c  |   1 +
 libavformat/amv2dec.c     | 234 ++++++++++++++++++++++++++++++++++++++
 6 files changed, 257 insertions(+)
 create mode 100644 libavformat/amv2dec.c

diff --git a/Changelog b/Changelog
index 570d723aded..136db953f96 100644
--- a/Changelog
+++ b/Changelog
@@ -7,6 +7,7 @@ version <next>:
 - AVS+ (AVS1-P16, Broadcasting profile) support in the Chinese AVS decoder
 - PCM PAMF decoder
 - AMAREC AMV2 video decoder
+- AMAREC AMV2 raw stream demuxer
 
 version 9.1:
 - ASTC image encoding and decoding via libastcenc, with .astc and KTX 1.0 
muxers and demuxers
diff --git a/doc/demuxers.texi b/doc/demuxers.texi
index e3e2018471e..fc21a0a3adc 100644
--- a/doc/demuxers.texi
+++ b/doc/demuxers.texi
@@ -32,6 +32,24 @@ Raw Audio Data Transport Stream AAC demuxer.
 This demuxer is used to demux an ADTS input containing a single AAC stream
 alongwith any ID3v1/2 or APE tags in it.
 
+@section amv2
+
+AMAREC AMV2 raw stream demuxer.
+
+This demuxer reads the headerless record stream written by the AMV2 codec
+of AmaRecTV (.amv2 files).  The stream records neither its picture size nor
+its frame rate, so both are options.  AMV2 in an AVI file needs no
+demuxer of its own.
+
+This demuxer accepts the following options:
+@table @option
+@item video_size
+Set the frame size. The default is @code{320x240}.
+
+@item framerate
+Set the frame rate. The default is @code{30}.
+@end table
+
 @section apng
 
 Animated Portable Network Graphics demuxer.
diff --git a/doc/general_contents.texi b/doc/general_contents.texi
index a43c0313809..31bb37fc5e4 100644
--- a/doc/general_contents.texi
+++ b/doc/general_contents.texi
@@ -516,6 +516,8 @@ library:
 @item American Laser Games MM   @tab   @tab X
     @tab Multimedia format used in games like Mad Dog McCree.
 @item 3GPP AMR                  @tab X @tab X
+@item AMAREC AMV2 raw video    @tab   @tab X
+    @tab Headerless record stream (.amv2) of the AmaRecTV capture codec.
 @item Amazing Studio Packed Animation File  @tab   @tab X
     @tab Multimedia format used in game Heart Of Darkness.
 @item Apple HTTP Live Streaming @tab   @tab X
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 17da02eee7f..e117f5217a3 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -113,6 +113,7 @@ OBJS-$(CONFIG_AIFF_MUXER)                += aiffenc.o 
aiff.o id3v2enc.o
 OBJS-$(CONFIG_AIX_DEMUXER)               += aixdec.o
 OBJS-$(CONFIG_ALP_DEMUXER)               += alp.o
 OBJS-$(CONFIG_ALP_MUXER)                 += alp.o rawenc.o
+OBJS-$(CONFIG_AMV2_DEMUXER)              += amv2dec.o
 OBJS-$(CONFIG_AMR_DEMUXER)               += amr.o rawdec.o
 OBJS-$(CONFIG_AMR_MUXER)                 += amr.o rawenc.o
 OBJS-$(CONFIG_AMRNB_DEMUXER)             += amr.o rawdec.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 3306d71bf7b..c987379a7e8 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -59,6 +59,7 @@ extern const FFInputFormat  ff_amr_demuxer;
 extern const FFOutputFormat ff_amr_muxer;
 extern const FFInputFormat  ff_amrnb_demuxer;
 extern const FFInputFormat  ff_amrwb_demuxer;
+extern const FFInputFormat  ff_amv2_demuxer;
 extern const FFOutputFormat ff_amv_muxer;
 extern const FFInputFormat  ff_anm_demuxer;
 extern const FFInputFormat  ff_apac_demuxer;
diff --git a/libavformat/amv2dec.c b/libavformat/amv2dec.c
new file mode 100644
index 00000000000..95f25512a48
--- /dev/null
+++ b/libavformat/amv2dec.c
@@ -0,0 +1,234 @@
+/*
+ * AMAREC AMV2 raw stream demuxer
+ * Copyright (c) 2026 Tom Needham
+ *
+ * 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
+ * Demuxer for the headerless AMAREC AMV2 record stream: back to back
+ *
+ *   uint8  tag[4]      "AMDB" (keyframe) or "AMDC" (inter frame)
+ *   uint32 size        record size including these 8 bytes
+ *   uint8  payload[size - 8]
+ *   uint8  aux[...]    mode C only, begins with its own uint32 length
+ *
+ * The stream records neither a size nor a frame rate, so both are options.
+ * A packet is a record with its auxiliary block, as the decoder takes it.
+ */
+
+#include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
+#include "libavutil/opt.h"
+#include "libavutil/parseutils.h"
+
+#include "avformat.h"
+#include "avio_internal.h"
+#include "demux.h"
+#include "internal.h"
+
+#define AMV2_TAG_KEY   MKTAG('A', 'M', 'D', 'B')
+#define AMV2_TAG_INTER MKTAG('A', 'M', 'D', 'C')
+
+/** An auxiliary block is a small map: refuse a length beyond this. */
+#define AMV2_MAX_AUX   (1 << 26)
+
+typedef struct AMV2DemuxContext {
+    const AVClass *class;
+    int width, height;
+    AVRational framerate;
+    int64_t pts;
+} AMV2DemuxContext;
+
+static int amv2_is_tag(uint32_t tag)
+{
+    return tag == AMV2_TAG_KEY || tag == AMV2_TAG_INTER;
+}
+
+static int amv2_probe(const AVProbeData *p)
+{
+    const uint8_t *b = p->buf;
+    const int n = p->buf_size;
+    uint32_t size;
+
+    if (n < 8 || AV_RL32(b) != AMV2_TAG_KEY)
+        return 0;
+    size = AV_RL32(b + 4);
+    if (size < 8 || size > AMV2_MAX_AUX)
+        return 0;
+    /* the first record is a whole frame, usually longer than the buffer */
+    if (size + 8 > n)
+        return AVPROBE_SCORE_MAX / 4;
+    if (amv2_is_tag(AV_RL32(b + size)))
+        return AVPROBE_SCORE_MAX / 2;
+    /* an auxiliary block is length-prefixed, and a record follows it */
+    if (size + 12 <= n) {
+        uint32_t aux = AV_RL32(b + size);
+
+        if (aux >= 4 && aux <= AMV2_MAX_AUX && size + aux + 4 <= n &&
+            amv2_is_tag(AV_RL32(b + size + aux)))
+            return AVPROBE_SCORE_MAX / 2;
+    }
+    return AVPROBE_SCORE_MAX / 4;
+}
+
+static int amv2_read_header(AVFormatContext *s)
+{
+    AMV2DemuxContext *c = s->priv_data;
+    AVStream *st = avformat_new_stream(s, NULL);
+
+    if (!st)
+        return AVERROR(ENOMEM);
+
+    st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
+    st->codecpar->codec_id   = AV_CODEC_ID_AMAREC_AMV2;
+    st->codecpar->width      = c->width;
+    st->codecpar->height     = c->height;
+    st->avg_frame_rate       = c->framerate;
+    avpriv_set_pts_info(st, 64, c->framerate.den, c->framerate.num);
+    return 0;
+}
+
+/**
+ * Look at the next four bytes of the stream without consuming them.
+ * @return how many there were: 0 at the end of the stream
+ */
+static int amv2_peek(AVFormatContext *s, uint8_t head[4])
+{
+    int ret = ffio_ensure_seekback(s->pb, 4);
+
+    if (ret < 0)
+        return ret;
+    ret = avio_read(s->pb, head, 4);
+    if (ret > 0)
+        avio_seek(s->pb, -ret, SEEK_CUR);
+    return FFMAX(ret, 0);
+}
+
+/**
+ * Append the auxiliary block that follows a record, if there is one.
+ *
+ * AMV2's block opens with its own length, which is believed when a record or
+ * the end of the stream follows it.  Failing that the block runs to the next
+ * record tag, and a tail too short to hold one is auxiliary data too.
+ *
+ * @return 0 or a negative error code
+ */
+static int amv2_read_aux(AVFormatContext *s, AVPacket *pkt)
+{
+    uint8_t head[4];
+    int n = amv2_peek(s, head);
+
+    if (n <= 0 || (n == 4 && amv2_is_tag(AV_RL32(head))))
+        return n < 0 ? n : 0;
+
+    if (n == 4) {
+        uint32_t aux = AV_RL32(head);
+
+        if (aux >= 4 && aux <= AMV2_MAX_AUX) {
+            int ret = av_append_packet(s->pb, pkt, aux);
+
+            if (ret < 0)
+                return ret == AVERROR_EOF ? 0 : ret;
+            if (ret < aux)
+                return 0;
+            n = amv2_peek(s, head);
+            if (n <= 0 || (n == 4 && amv2_is_tag(AV_RL32(head))))
+                return n < 0 ? n : 0;
+        }
+    }
+
+    for (;;) {
+        int ret = av_append_packet(s->pb, pkt, 1);
+
+        if (ret < 0)
+            return ret == AVERROR_EOF ? 0 : ret;
+        n = amv2_peek(s, head);
+        if (n <= 0 || (n == 4 && amv2_is_tag(AV_RL32(head))))
+            return n < 0 ? n : 0;
+    }
+}
+
+static int amv2_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    AMV2DemuxContext *c = s->priv_data;
+    uint32_t tag, size;
+    int ret;
+
+    ret = av_get_packet(s->pb, pkt, 8);
+    if (ret < 0)
+        return ret;
+    if (ret < 8)
+        return AVERROR_INVALIDDATA;
+
+    tag  = AV_RL32(pkt->data);
+    size = AV_RL32(pkt->data + 4);
+    if (!amv2_is_tag(tag) || size < 8) {
+        av_log(s, AV_LOG_ERROR, "Invalid record at %"PRId64"\n",
+               avio_tell(s->pb) - 8);
+        return AVERROR_INVALIDDATA;
+    }
+
+    if (size > 8) {
+        ret = av_append_packet(s->pb, pkt, size - 8);
+        if (ret < 0)
+            return ret;
+        if (ret < size - 8)
+            return AVERROR_INVALIDDATA;
+    }
+
+    ret = amv2_read_aux(s, pkt);
+    if (ret < 0)
+        return ret;
+
+    if (tag == AMV2_TAG_KEY)
+        pkt->flags |= AV_PKT_FLAG_KEY;
+    pkt->stream_index = 0;
+    pkt->pts = pkt->dts = c->pts++;
+    pkt->duration = 1;
+    return 0;
+}
+
+#define OFFSET(x) offsetof(AMV2DemuxContext, x)
+#define DEC AV_OPT_FLAG_DECODING_PARAM
+static const AVOption amv2_options[] = {
+    { "video_size", "set frame size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE,
+      { .str = "320x240" }, 0, 0, DEC },
+    { "framerate", "set frame rate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE,
+      { .str = "30" }, 0, INT_MAX, DEC },
+    { NULL },
+};
+
+static const AVClass amv2_demuxer_class = {
+    .class_name = "amv2 demuxer",
+    .item_name  = av_default_item_name,
+    .option     = amv2_options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+const FFInputFormat ff_amv2_demuxer = {
+    .p.name         = "amv2",
+    .p.long_name    = NULL_IF_CONFIG_SMALL("AMAREC AMV2 raw video"),
+    .p.extensions   = "amv2",
+    .p.flags        = AVFMT_GENERIC_INDEX,
+    .p.priv_class   = &amv2_demuxer_class,
+    .priv_data_size = sizeof(AMV2DemuxContext),
+    .read_probe     = amv2_probe,
+    .read_header    = amv2_read_header,
+    .read_packet    = amv2_read_packet,
+};
-- 
2.54.0


>From 7c0f83b3ad3660e6d46425b2f5899db9d5b0d8cd Mon Sep 17 00:00:00 2001
From: Tom Needham <[email protected]>
Date: Sat, 31 Oct 2026 20:07:37 +0000
Subject: [PATCH 3/5] avcodec/amv2dec: add modes A and B

Decode the two token modes of AMV2.  Both walk a frame's coding units in
order, six (RGB) or eight (YUY2) groups of four bytes to a unit, with the
first group's code in the token's low bits.  Mode A has a presence bit per
group and mode B a two bit code; their deltas wrap modulo 256, and the
two byte form spreads its four nibbles over the lanes in the order 0, 2, 1, 3.

A keyframe predicts each group from the group before it in its plane block
and the first one from where the unit before left off, its three slots
starting at zero once per frame and running on across unit rows.  An inter
frame without a reference adds to black luma and grey chroma.

The layout and mode come from the stream format's shift when the stream
has one, and otherwise from the first payload that parses.

Signed-off-by: Tom Needham <[email protected]>
---
 libavcodec/amv2dec.c | 287 ++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 267 insertions(+), 20 deletions(-)

diff --git a/libavcodec/amv2dec.c b/libavcodec/amv2dec.c
index 20b6c5047e3..caf799342dd 100644
--- a/libavcodec/amv2dec.c
+++ b/libavcodec/amv2dec.c
@@ -41,8 +41,22 @@
  *
  * The decoder outputs the units as they are: RGB24 for the RGB layout and
  * YUYV422 for the YUY2 one.
+ *
+ * A frame is raw (a keyframe holding its units) or coded in one of three
+ * modes, told apart by the stream format's shift (0: A, 1: B, 2: C) or, in
+ * a bare stream, by which one parses:
+ *
+ *   A  a presence bit per group, then 4 byte literals
+ *   B  a two bit code per group: unchanged, literal, 1 byte or 2 byte deltas
+ *
+ * Both walk the units in order, six (RGB) or eight (YUY2) groups of four
+ * bytes to a unit.  An inter frame applies them to the previous frame.  A
+ * keyframe predicts each group from the group before it in its plane block,
+ * the first one from where the unit before left off.
  */
 
+#include <string.h>
+
 #include "libavutil/imgutils.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mem.h"
@@ -67,15 +81,54 @@ enum AMV2Profile {
     AMV2_PROFILE_YUY2,      ///< 32 byte units, 8x2 pixels, YUYV422 out
 };
 
+enum AMV2Mode {
+    AMV2_MODE_UNKNOWN = -1,
+    AMV2_MODE_A,
+    AMV2_MODE_B,
+    AMV2_NB_AB_MODES,
+};
+
+/** Token layout of modes A and B. */
+typedef struct AMV2Token {
+    uint8_t token_bytes;    ///< bytes of the unit's token
+    uint8_t code_bits;      ///< bits of each group's code in it
+    uint8_t cost[4];        ///< payload bytes each code consumes
+} AMV2Token;
+
+static const AMV2Token amv2_modes[AMV2_NB_AB_MODES] = {
+    [AMV2_MODE_A] = { 1, 1, { 0, 4, 0, 0 } },   ///< 1 = literal
+    [AMV2_MODE_B] = { 2, 2, { 0, 4, 1, 2 } },   ///< 1 = literal, 2 = 1, 3 = 2 
bytes
+};
+
+/** The plane block each group of a unit belongs to, and the block's groups. */
+static const struct {
+    uint8_t lane[8];
+    uint8_t begin[3], end[3];
+} amv2_lanes[2] = {
+    [AMV2_PROFILE_RGB]  = { { 0, 0, 1, 1, 2, 2 },
+                            { 0, 2, 4 }, { 2, 4, 6 } },
+    [AMV2_PROFILE_YUY2] = { { 0, 0, 0, 0, 1, 1, 2, 2 },
+                            { 0, 4, 6 }, { 4, 6, 8 } },
+};
+
+/** The order the two-byte form spreads its four nibbles over the lanes. */
+static const uint8_t amv2_nibble_order[4] = { 0, 2, 1, 3 };
+
 typedef struct AMV2Context {
     AVClass *class;
     int interlace_opt;      ///< user option: -1 from the stream format
 
     int profile;            ///< enum AMV2Profile
+    int mode;               ///< enum AMV2Mode, or unknown
     int interlace;          ///< swap rows 1 and 2 of every four
 
+    int groups;             ///< groups of four bytes to a unit
+    int unit_bytes;
+    int units_per_row;      ///< units across the picture
+    int n_rows;             ///< unit rows down it
     size_t frame_bytes;     ///< bytes of one frame's units
-    uint8_t *units;         ///< the frame's units: the last decoded frame
+    uint8_t *buf[2];        ///< frames' units, the last one decoded in 
buf[ref]
+    int ref;                ///< index into buf of the last frame, or -1
 } AMV2Context;
 
 /** Frame bytes of a profile at the given size. */
@@ -84,6 +137,17 @@ static size_t amv2_frame_bytes(int profile, int width, int 
height)
     return (size_t)width * height * (profile == AMV2_PROFILE_RGB ? 3 : 2);
 }
 
+/** Fill in the geometry of a profile at the stream's size. */
+static void amv2_geometry(AMV2Context *s, int profile, int width, int height)
+{
+    s->profile       = profile;
+    s->groups        = profile == AMV2_PROFILE_RGB ? 6 : 8;
+    s->unit_bytes    = 4 * s->groups;
+    s->units_per_row = width / (profile == AMV2_PROFILE_RGB ? 4 : 8);
+    s->n_rows        = height / 2;
+    s->frame_bytes   = amv2_frame_bytes(profile, width, height);
+}
+
 /**
  * Pin the profile: allocate its buffers and set the output format.
  */
@@ -91,14 +155,15 @@ static int amv2_set_profile(AVCodecContext *avctx, int 
profile)
 {
     AMV2Context *s = avctx->priv_data;
 
-    s->profile     = profile;
-    s->frame_bytes = amv2_frame_bytes(profile, avctx->width, avctx->height);
+    amv2_geometry(s, profile, avctx->width, avctx->height);
     avctx->pix_fmt = profile == AMV2_PROFILE_RGB ? AV_PIX_FMT_RGB24
                                                  : AV_PIX_FMT_YUYV422;
 
-    s->units = av_mallocz(s->frame_bytes);
-    if (!s->units)
-        return AVERROR(ENOMEM);
+    for (int i = 0; i < 2; i++) {
+        s->buf[i] = av_mallocz(s->frame_bytes);
+        if (!s->buf[i])
+            return AVERROR(ENOMEM);
+    }
     return 0;
 }
 
@@ -118,6 +183,8 @@ static av_cold int amv2_decode_init(AVCodecContext *avctx)
         return ret;
 
     s->profile   = AMV2_PROFILE_UNKNOWN;
+    s->mode      = AMV2_MODE_UNKNOWN;
+    s->ref       = -1;
     s->interlace = s->interlace_opt > 0;
 
     /* The stream format an AVI carries after its BITMAPINFOHEADER:
@@ -125,6 +192,7 @@ static av_cold int amv2_decode_init(AVCodecContext *avctx)
     if (avctx->extradata_size >= 20) {
         const uint8_t *ed = avctx->extradata;
         const uint32_t coded = AV_RL32(ed + 12);
+        const uint32_t shift = AV_RL32(ed + 4);
         const uint32_t flags = AV_RL32(ed + 16);
 
         if (coded == MKTAG('Y', '4', '2', '2'))
@@ -136,6 +204,9 @@ static av_cold int amv2_decode_init(AVCodecContext *avctx)
             ret = 0;
         if (ret < 0)
             return ret;
+        /* shifts 0-2 and 3-5 code modes A, B and C of the two layouts */
+        if (shift < 6 && shift % 3 < AMV2_NB_AB_MODES)
+            s->mode = shift % 3;
         if (s->interlace_opt < 0)
             s->interlace = !!(flags & 2);
     }
@@ -147,7 +218,8 @@ static av_cold int amv2_decode_close(AVCodecContext *avctx)
 {
     AMV2Context *s = avctx->priv_data;
 
-    av_freep(&s->units);
+    av_freep(&s->buf[0]);
+    av_freep(&s->buf[1]);
     return 0;
 }
 
@@ -224,14 +296,169 @@ static void amv2_interlace(uint8_t *dst, ptrdiff_t 
linesize, int row_bytes,
     }
 }
 
+/** Turn the bytes a code consumed into its group's four bytes. */
+static void amv2_expand(const uint8_t *src, int cost, const uint8_t pred[4],
+                        uint8_t dst[4])
+{
+    uint8_t v[4];
+
+    switch (cost) {
+    case 0:
+        memcpy(v, pred, 4);
+        break;
+    case 4:
+        memcpy(v, src, 4);
+        break;
+    case 1:
+        /* four 2 bit deltas, -2..+1, lane i in bits 2i and 2i + 1 */
+        for (int i = 0; i < 4; i++) {
+            int d = (src[0] >> (2 * i)) & 3;
+
+            v[i] = pred[i] + (d >= 2 ? d - 4 : d);
+        }
+        break;
+    default: {
+        /* four 4 bit deltas, -8..+7, in the order 0, 2, 1, 3 */
+        const unsigned w = AV_RL16(src);
+
+        for (int i = 0; i < 4; i++) {
+            int d = (w >> (4 * amv2_nibble_order[i])) & 15;
+
+            v[i] = pred[i] + (d >= 8 ? d - 16 : d);
+        }
+        break;
+    }
+    }
+    memcpy(dst, v, 4);
+}
+
+/**
+ * Walk the units of a mode A or B payload.
+ *
+ * Adds wrap modulo 256.  A keyframe predicts from its own previous groups,
+ * its three predictor slots starting at zero once per frame and running on
+ * from one unit row into the next.  Every group of an inter frame is a delta
+ * on, or a literal over, the group `out` already holds.
+ *
+ * @param out  the frame's units, holding the previous frame for an inter
+ *             frame, or NULL to only check that the payload parses
+ * @return 0 if the payload is made of whole units and no byte is left over
+ */
+static int amv2_walk(const AMV2Context *s, int mode, int intra,
+                     const uint8_t *payload, size_t len, uint8_t *out)
+{
+    const AMV2Token *m = &amv2_modes[mode];
+    const uint8_t *lane = amv2_lanes[s->profile].lane;
+    const unsigned mask = (1u << m->code_bits) - 1;
+    const unsigned used = m->code_bits * s->groups;
+    uint8_t pred[3][4] = { { 0 } };
+    uint8_t codes[8];
+    const uint8_t *src[8];
+    size_t pos = 0;
+
+    for (int u = 0; u < s->units_per_row * s->n_rows; u++) {
+        uint32_t token = 0;
+
+        if ((size_t)m->token_bytes > len - pos)
+            return AVERROR_INVALIDDATA;
+        for (int b = 0; b < m->token_bytes; b++)
+            token |= (uint32_t)payload[pos + b] << (8 * b);
+        pos += m->token_bytes;
+        if (token >> used)
+            return AVERROR_INVALIDDATA;     /* bits above the last group */
+
+        /* group 0 is in the token's low bits */
+        for (int g = 0; g < s->groups; g++) {
+            const int code = (token >> (m->code_bits * g)) & mask;
+            const int cost = m->cost[code];
+
+            if ((size_t)cost > len - pos)
+                return AVERROR_INVALIDDATA;
+            codes[g] = code;
+            src[g]   = payload + pos;
+            pos     += cost;
+        }
+        if (!out)
+            continue;
+
+        if (!intra) {
+            uint8_t *unit = out + (size_t)u * s->unit_bytes;
+
+            for (int g = 0; g < s->groups; g++)
+                if (codes[g])
+                    amv2_expand(src[g], m->cost[codes[g]], unit + 4 * g,
+                                unit + 4 * g);
+        } else {
+            uint8_t *unit = out + (size_t)u * s->unit_bytes;
+
+            for (int g = 0; g < s->groups; g++) {
+                const int l = lane[g];
+                uint8_t *grp = unit + 4 * g;
+                const uint8_t *base = g == amv2_lanes[s->profile].begin[l]
+                                      ? pred[l] : grp - 4;
+
+                amv2_expand(src[g], m->cost[codes[g]], base, grp);
+                if (g + 1 == amv2_lanes[s->profile].end[l])
+                    memcpy(pred[l], grp, 4);
+            }
+        }
+    }
+    return pos == len ? 0 : AVERROR_INVALIDDATA;
+}
+
+/** Black luma and grey chroma: what an inter frame without a reference adds 
to. */
+static void amv2_neutral_frame(const AMV2Context *s, uint8_t *out)
+{
+    if (s->profile == AMV2_PROFILE_RGB) {
+        memset(out, 0, s->frame_bytes);
+        return;
+    }
+    for (int u = 0; u < s->units_per_row * s->n_rows; u++) {
+        memset(out + (size_t)u * 32,      0x00, 16);
+        memset(out + (size_t)u * 32 + 16, 0x80, 16);
+    }
+}
+
+/**
+ * Tell the layout and the mode from a payload that is neither raw nor mode C:
+ * the first layout and mode, RGB before YUY2 and A before B, that it parses
+ * as.  A layout and a mode already known are not asked again.
+ */
+static int amv2_detect(AVCodecContext *avctx, int intra,
+                       const uint8_t *payload, size_t len)
+{
+    AMV2Context *s = avctx->priv_data;
+    const int pinned = s->profile;
+    int ret;
+
+    for (int p = AMV2_PROFILE_RGB; p <= AMV2_PROFILE_YUY2; p++) {
+        if (pinned != AMV2_PROFILE_UNKNOWN && pinned != p)
+            continue;
+        amv2_geometry(s, p, avctx->width, avctx->height);
+        for (int mode = 0; mode < AMV2_NB_AB_MODES; mode++) {
+            if (s->mode != AMV2_MODE_UNKNOWN && s->mode != mode)
+                continue;
+            if (amv2_walk(s, mode, intra, payload, len, NULL) < 0)
+                continue;
+            if (!s->buf[0] && (ret = amv2_set_profile(avctx, p)) < 0)
+                return ret;
+            s->mode = mode;
+            return 0;
+        }
+    }
+    s->profile = pinned;
+    return AVERROR_INVALIDDATA;
+}
+
 static int amv2_decode_frame(AVCodecContext *avctx, AVFrame *frame,
                              int *got_frame, AVPacket *avpkt)
 {
     AMV2Context *s = avctx->priv_data;
     const uint8_t *data = avpkt->data;
-    size_t size, len, raw_bytes;
+    size_t size, len;
     uint32_t tag;
-    int key, ret;
+    uint8_t *cur;
+    int key, raw, ret;
 
     if (avpkt->size < 8)
         return AVERROR_INVALIDDATA;
@@ -245,32 +472,52 @@ static int amv2_decode_frame(AVCodecContext *avctx, 
AVFrame *frame,
     len  = size - 8;
     data += 8;
 
-    if (s->profile == AMV2_PROFILE_UNKNOWN) {
-        /* a raw frame says which layout the stream has */
+    /* what follows the record is mode C's auxiliary block */
+    if (avpkt->size > size)
+        return AVERROR_PATCHWELCOME;
+
+    /* a raw frame says which layout the stream has */
+    if (s->profile == AMV2_PROFILE_UNKNOWN && key) {
         for (int p = AMV2_PROFILE_RGB; p <= AMV2_PROFILE_YUY2; p++) {
-            raw_bytes = amv2_frame_bytes(p, avctx->width, avctx->height);
-            if (key && (len == raw_bytes || len == raw_bytes + AMV2_TRAILER)) {
+            size_t raw_bytes = amv2_frame_bytes(p, avctx->width, 
avctx->height);
+
+            if (len == raw_bytes || len == raw_bytes + AMV2_TRAILER) {
                 if ((ret = amv2_set_profile(avctx, p)) < 0)
                     return ret;
                 break;
             }
         }
-        if (s->profile == AMV2_PROFILE_UNKNOWN)
-            return AVERROR_INVALIDDATA;
+    }
+    raw = key && s->profile != AMV2_PROFILE_UNKNOWN &&
+          (len == s->frame_bytes || len == s->frame_bytes + AMV2_TRAILER);
+
+    if (!raw && (s->profile == AMV2_PROFILE_UNKNOWN ||
+                 s->mode == AMV2_MODE_UNKNOWN)) {
+        if ((ret = amv2_detect(avctx, key, data, len)) < 0)
+            return ret;
     }
 
-    if (!key || (len != s->frame_bytes && len != s->frame_bytes + 
AMV2_TRAILER))
-        return AVERROR_PATCHWELCOME;
-    memcpy(s->units, data, s->frame_bytes);
+    cur = s->buf[s->ref == 0];
+    if (raw) {
+        memcpy(cur, data, s->frame_bytes);
+    } else {
+        if (s->ref < 0)
+            amv2_neutral_frame(s, cur);
+        else if (!key)
+            memcpy(cur, s->buf[s->ref], s->frame_bytes);
+        if ((ret = amv2_walk(s, s->mode, key, data, len, cur)) < 0)
+            return ret;
+    }
+    s->ref = s->ref == 0;
 
     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
 
     if (s->profile == AMV2_PROFILE_RGB)
-        amv2_rgb_to_picture(s->units, frame->data[0], frame->linesize[0],
+        amv2_rgb_to_picture(cur, frame->data[0], frame->linesize[0],
                             avctx->width, avctx->height);
     else
-        amv2_yuy2_to_picture(s->units, frame->data[0], frame->linesize[0],
+        amv2_yuy2_to_picture(cur, frame->data[0], frame->linesize[0],
                              avctx->width, avctx->height);
     if (s->interlace)
         amv2_interlace(frame->data[0], frame->linesize[0],
-- 
2.54.0


>From 704b75c5f1cd8e4f14287131af40521b4adf3bf0 Mon Sep 17 00:00:00 2001
From: Tom Needham <[email protected]>
Date: Sat, 31 Oct 2026 20:12:32 +0000
Subject: [PATCH 4/5] avcodec/amv2dec: add mode C

Decode the lossy mode of AMV2.  An auxiliary map gives each 8 byte chunk
of a frame two halves with a class each (runs and skips take a count of
1 to 64, 0 meaning 64, and trailing zero bytes are padding), and the
payload holds what the classes consume, unit by unit with the three map
segments interleaved.

The state wraps modulo 256 and the next frame predicts from it, while the
picture shows each coded half clamped to 0..255.  A keyframe codes its
halves as a ladder over four chain bytes or the half one unit row above;
an inter frame codes them over the previous state and a single chain byte,
which is 0 before the first chunk of a chain.  The RGB layout's state is
Y, Pb and Pr, converted to and from the units with the vendor's rounding,
and an inter frame after a raw or A/B one primes it from the units.  The
YUY2 layout's state is its units.

Signed-off-by: Tom Needham <[email protected]>
---
 libavcodec/amv2dec.c | 536 +++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 523 insertions(+), 13 deletions(-)

diff --git a/libavcodec/amv2dec.c b/libavcodec/amv2dec.c
index caf799342dd..37c0420d142 100644
--- a/libavcodec/amv2dec.c
+++ b/libavcodec/amv2dec.c
@@ -48,15 +48,24 @@
  *
  *   A  a presence bit per group, then 4 byte literals
  *   B  a two bit code per group: unchanged, literal, 1 byte or 2 byte deltas
+ *   C  lossy: an auxiliary map gives each 8 byte chunk of a frame two 4 byte
+ *      halves with a class each, and the payload holds what the classes
+ *      consume, unit by unit
  *
  * Both walk the units in order, six (RGB) or eight (YUY2) groups of four
  * bytes to a unit.  An inter frame applies them to the previous frame.  A
  * keyframe predicts each group from the group before it in its plane block,
  * the first one from where the unit before left off.
+ *
+ * Mode C keeps its state as the RGB layout's Y, Pb and Pr (the YUY2 layout's
+ * units are its own state), every add wrapping modulo 256, and shows a
+ * picture in which each half that was coded is clamped to 0..255 instead.
+ * The next frame predicts from the state, never from the picture.
  */
 
 #include <string.h>
 
+#include "libavutil/common.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mem.h"
@@ -114,6 +123,50 @@ static const struct {
 /** The order the two-byte form spreads its four nibbles over the lanes. */
 static const uint8_t amv2_nibble_order[4] = { 0, 2, 1, 3 };
 
+/** Mode C: one map segment per chain, in all of a frame's chunks. */
+#define AMV2_SEGMENTS 3
+/** Mode C: bytes of a chunk, two halves of four. */
+#define AMV2_CHUNK    8
+
+/** How a profile's units divide into 8 byte chunks, chains and map segments. 
*/
+static const struct {
+    uint8_t blocks;         ///< chunks to a unit
+    uint8_t seg_of[4];      ///< map segment each chunk of a unit is listed in
+    uint8_t chain_of[4];    ///< chain each chunk threads
+    uint8_t prev_off[4];    ///< chunks back to the previous one of the chain
+} amv2_cgeom[2] = {
+    [AMV2_PROFILE_RGB]  = { 3, { 0, 1, 2, 0 }, { 0, 1, 2, 0 }, { 3, 3, 3, 0 } 
},
+    [AMV2_PROFILE_YUY2] = { 4, { 0, 0, 1, 2 }, { 0, 0, 1, 2 }, { 3, 1, 4, 4 } 
},
+};
+
+/** Keyframe payload bytes per class, and the bits per lane of a class's code. 
*/
+static const uint8_t amv2_intra_cost[8] = { 0, 4, 1, 2, 3, 1, 2, 0 };
+static const uint8_t amv2_intra_bits[8] = { 0, 0, 2, 4, 6, 2, 4, 0 };
+/** Inter payload bytes per class, bits per lane, and delta scale as a shift. 
*/
+static const uint8_t amv2_inter_cost[8]  = { 0, 4, 1, 2, 1, 1, 2, 0 };
+static const uint8_t amv2_inter_bits[8]  = { 0, 0, 2, 4, 2, 2, 4, 0 };
+static const uint8_t amv2_inter_shift[8] = { 0, 0, 0, 3, 2, 1, 3, 0 };
+
+/** Lane orders a class's fields take. */
+static const uint8_t amv2_perm_id[4]  = { 0, 1, 2, 3 };
+static const uint8_t amv2_perm_col[4] = { 0, 2, 1, 3 };   ///< an involution
+static const uint8_t amv2_perm_rev[4] = { 3, 2, 1, 0 };
+static const uint8_t *const amv2_intra_perm[8] = {
+    amv2_perm_id, amv2_perm_id, amv2_perm_id, amv2_perm_col,
+    amv2_perm_rev, amv2_perm_id, amv2_perm_col, amv2_perm_id,
+};
+static const uint8_t *const amv2_inter_perm[8] = {
+    amv2_perm_id, amv2_perm_id, amv2_perm_id, amv2_perm_col,
+    amv2_perm_id, amv2_perm_id, amv2_perm_col, amv2_perm_id,
+};
+
+/** One map segment's cursor, walked in step with the chunks it lists. */
+typedef struct AMV2Cursor {
+    const uint8_t *p, *end;
+    size_t left;            ///< chunks still owed by the current byte
+    uint8_t cls[2];         ///< its two classes
+} AMV2Cursor;
+
 typedef struct AMV2Context {
     AVClass *class;
     int interlace_opt;      ///< user option: -1 from the stream format
@@ -126,9 +179,14 @@ typedef struct AMV2Context {
     int unit_bytes;
     int units_per_row;      ///< units across the picture
     int n_rows;             ///< unit rows down it
+    int blocks;             ///< chunks to a unit (mode C)
     size_t frame_bytes;     ///< bytes of one frame's units
     uint8_t *buf[2];        ///< frames' units, the last one decoded in 
buf[ref]
     int ref;                ///< index into buf of the last frame, or -1
+    uint8_t *mstate;        ///< mode C: the wrapped state its next frame 
predicts from
+    uint8_t *display;       ///< mode C: the clamped picture, as units
+    uint8_t *scratch;       ///< mode C: RGB state primed from units
+    int have_mstate;        ///< mstate follows from the last frame
 } AMV2Context;
 
 /** Frame bytes of a profile at the given size. */
@@ -145,6 +203,7 @@ static void amv2_geometry(AMV2Context *s, int profile, int 
width, int height)
     s->unit_bytes    = 4 * s->groups;
     s->units_per_row = width / (profile == AMV2_PROFILE_RGB ? 4 : 8);
     s->n_rows        = height / 2;
+    s->blocks        = amv2_cgeom[profile].blocks;
     s->frame_bytes   = amv2_frame_bytes(profile, width, height);
 }
 
@@ -164,6 +223,12 @@ static int amv2_set_profile(AVCodecContext *avctx, int 
profile)
         if (!s->buf[i])
             return AVERROR(ENOMEM);
     }
+    s->mstate  = av_mallocz(s->frame_bytes);
+    s->display = av_mallocz(s->frame_bytes);
+    if (profile == AMV2_PROFILE_RGB)
+        s->scratch = av_mallocz(s->frame_bytes);
+    if (!s->mstate || !s->display || (profile == AMV2_PROFILE_RGB && 
!s->scratch))
+        return AVERROR(ENOMEM);
     return 0;
 }
 
@@ -220,6 +285,9 @@ static av_cold int amv2_decode_close(AVCodecContext *avctx)
 
     av_freep(&s->buf[0]);
     av_freep(&s->buf[1]);
+    av_freep(&s->mstate);
+    av_freep(&s->display);
+    av_freep(&s->scratch);
     return 0;
 }
 
@@ -450,12 +518,424 @@ static int amv2_detect(AVCodecContext *avctx, int intra,
     return AVERROR_INVALIDDATA;
 }
 
+/**
+ * Decode one byte of mode C's map.
+ *
+ * A byte with the top bit set is a run of 1 to 64 chunks of class 1 (or class
+ * 3 with the next bit also set), 0x40 | n a skip of n chunks of class 0, and
+ * anything else one chunk with its two halves' classes in the low and next
+ * three bits.  A count of 0 is 64.
+ *
+ * @return chunks the byte covers
+ */
+static size_t amv2_map_token(uint8_t b, uint8_t cls[2])
+{
+    if (b & 0x80) {
+        cls[0] = cls[1] = (b & 0x40) ? 3 : 1;
+        return (b & 0x3F) ? (b & 0x3F) : 64;
+    }
+    if (b & 0x40) {
+        cls[0] = cls[1] = 0;
+        return (b & 0x3F) ? (b & 0x3F) : 64;
+    }
+    cls[0] = b & 7;
+    cls[1] = (b >> 3) & 7;
+    return 1;
+}
+
+/**
+ * The map's bytes: without its self-length prefix and without trailing zero
+ * bytes, which are padding and not descriptors.
+ */
+static const uint8_t *amv2_map_body(const uint8_t *aux, size_t aux_len,
+                                    const uint8_t **end)
+{
+    const uint8_t *p = aux, *e = aux + aux_len;
+
+    if (aux_len >= 4 && AV_RL32(aux) == aux_len)
+        p += 4;
+    while (e > p && e[-1] == 0)
+        e--;
+    *end = e;
+    return p;
+}
+
+/**
+ * Tally a map, and position a cursor at the start of each segment.
+ *
+ * @param seg_start  first chunk of each segment, or NULL for the tally alone
+ * @param chunks     receives the chunks the map covers
+ * @param intra      receives the payload bytes of a keyframe it implies
+ * @param inter      receives the payload bytes of an inter frame
+ */
+static void amv2_map_scan(const uint8_t *body, const uint8_t *end,
+                          const size_t seg_start[AMV2_SEGMENTS],
+                          size_t *chunks, size_t *intra, size_t *inter,
+                          AMV2Cursor cur[AMV2_SEGMENTS])
+{
+    *chunks = *intra = *inter = 0;
+    if (seg_start)
+        for (int i = 0; i < AMV2_SEGMENTS; i++)
+            cur[i] = (AMV2Cursor){ .p = end, .end = end };
+
+    for (const uint8_t *p = body; p < end; p++) {
+        uint8_t cls[2];
+        const size_t n   = amv2_map_token(*p, cls);
+        const size_t pos = *chunks;
+
+        *chunks += n;
+        *intra  += n * (amv2_intra_cost[cls[0]] + amv2_intra_cost[cls[1]]);
+        *inter  += n * (amv2_inter_cost[cls[0]] + amv2_inter_cost[cls[1]]);
+        if (!seg_start)
+            continue;
+        for (int i = 0; i < AMV2_SEGMENTS; i++) {
+            if (seg_start[i] >= pos && seg_start[i] < pos + n)
+                cur[i] = (AMV2Cursor){ .p = p + 1, .end = end,
+                                       .left = pos + n - seg_start[i],
+                                       .cls = { cls[0], cls[1] } };
+        }
+    }
+}
+
+/** The next chunk's classes: 0 once the map runs out, which a checked map 
never does. */
+static void amv2_cursor_next(AMV2Cursor *c, uint8_t cls[2])
+{
+    while (!c->left) {
+        if (c->p >= c->end) {
+            cls[0] = cls[1] = 0;
+            return;
+        }
+        c->left = amv2_map_token(*c->p++, c->cls);
+    }
+    c->left--;
+    cls[0] = c->cls[0];
+    cls[1] = c->cls[1];
+}
+
+/**
+ * Split a code word into four signed, scaled, permuted lane deltas: field i
+ * is bits `bits * i` of the little-endian word, and lane i takes field 
perm[i].
+ */
+static void amv2_deltas(const uint8_t *src, int nbytes, int bits, int shift,
+                        const uint8_t perm[4], int d[4])
+{
+    const unsigned mask = (1u << bits) - 1;
+    const unsigned sign = 1u << (bits - 1);
+    unsigned w = 0;
+    int f[4];
+
+    for (int k = 0; k < nbytes; k++)
+        w |= (unsigned)src[k] << (8 * k);
+    for (int i = 0; i < 4; i++) {
+        const unsigned v = (w >> (bits * i)) & mask;
+
+        f[i] = (int)(v & (sign - 1)) - (int)(v & sign);
+    }
+    for (int i = 0; i < 4; i++)
+        d[i] = f[perm[i]] * (1 << shift);
+}
+
+/**
+ * Add four deltas to a base: the sum wraps into dst, and is clamped to
+ * 0..255 into shown, which may be NULL.
+ */
+static void amv2_add_both(uint8_t dst[4], uint8_t *shown,
+                          const uint8_t base[4], const int d[4])
+{
+    for (int i = 0; i < 4; i++) {
+        const int v = base[i] + d[i];
+
+        if (shown)
+            shown[i] = av_clip_uint8(v);
+        dst[i] = v;
+    }
+}
+
+/**
+ * Decode a keyframe's chunks.  Each half is a ladder of classes over one of
+ * four chain bytes per segment, or over the half a unit row above.
+ */
+static void amv2_decode_intra(const AMV2Context *s, AMV2Cursor 
cur[AMV2_SEGMENTS],
+                              const uint8_t *payload)
+{
+    static const uint8_t zero[AMV2_CHUNK];
+    const size_t above = (size_t)s->units_per_row * s->blocks;
+    const size_t units = (size_t)s->units_per_row * s->n_rows;
+    const uint8_t *seg_of = amv2_cgeom[s->profile].seg_of;
+    const uint8_t *chain_of = amv2_cgeom[s->profile].chain_of;
+    uint8_t chain[AMV2_SEGMENTS][4] = { { 0 } };
+    uint8_t *state = s->mstate, *display = s->display;
+    size_t q = 0, b = 0;
+
+    for (size_t u = 0; u < units; u++) {
+        for (int j = 0; j < s->blocks; j++, b++) {
+            uint8_t *blk = state + b * AMV2_CHUNK;
+            const uint8_t *ab = b >= above ? state + (b - above) * AMV2_CHUNK 
: zero;
+            uint8_t *ch = chain[chain_of[j]];
+            uint8_t cls[2];
+
+            amv2_cursor_next(&cur[seg_of[j]], cls);
+            for (int h = 0; h < 2; h++) {
+                const int c = cls[h];
+                uint8_t *dst   = blk + 4 * h;
+                uint8_t *shown = display + b * AMV2_CHUNK + 4 * h;
+                const uint8_t *up = ab + 4 * h;
+                int d[4];
+
+                switch (c) {
+                case 0:
+                    memcpy(dst, ch, 4);
+                    break;
+                case 7:
+                    memcpy(dst, up, 4);
+                    break;
+                case 1:
+                    memcpy(dst, payload + q, 4);
+                    break;
+                default:
+                    amv2_deltas(payload + q, amv2_intra_cost[c],
+                                amv2_intra_bits[c], 0, amv2_intra_perm[c], d);
+                    amv2_add_both(dst, shown, c <= 4 ? ch : up, d);
+                    shown = NULL;
+                    break;
+                }
+                if (shown)
+                    memcpy(shown, dst, 4);
+                q += amv2_intra_cost[c];
+                memcpy(ch, dst, 4);
+            }
+        }
+    }
+}
+
+/**
+ * Decode an inter frame over the previous state, in place.
+ *
+ * The chain byte of a half is lane 0 of the half before it in chain order, as
+ * decoded this frame, and before the first chunk of a chain it is 0.
+ */
+static void amv2_decode_inter(const AMV2Context *s, AMV2Cursor 
cur[AMV2_SEGMENTS],
+                              const uint8_t *payload)
+{
+    const size_t units = (size_t)s->units_per_row * s->n_rows;
+    const uint8_t *seg_of = amv2_cgeom[s->profile].seg_of;
+    const uint8_t *prev_off = amv2_cgeom[s->profile].prev_off;
+    uint8_t *state = s->mstate, *display = s->display;
+    size_t q = 0, b = 0;
+
+    for (size_t u = 0; u < units; u++) {
+        for (int j = 0; j < s->blocks; j++, b++) {
+            uint8_t *blk = state + b * AMV2_CHUNK;
+            const int first = b < prev_off[j];
+            const size_t pb = first ? 0 : b - prev_off[j];
+            uint8_t cls[2];
+
+            amv2_cursor_next(&cur[seg_of[j]], cls);
+            for (int h = 0; h < 2; h++) {
+                const int c = cls[h];
+                uint8_t *dst   = blk + 4 * h;
+                uint8_t *shown = display + b * AMV2_CHUNK + 4 * h;
+                uint8_t cb;
+                int d[4];
+
+                if (!c)
+                    continue;
+                cb = h ? blk[0] : first ? 0 : state[pb * AMV2_CHUNK + 4];
+                switch (c) {
+                case 7:
+                    memset(dst, cb, 4);
+                    break;
+                case 1:
+                    memcpy(dst, payload + q, 4);
+                    break;
+                default: {
+                    const uint8_t chain4[4] = { cb, cb, cb, cb };
+
+                    amv2_deltas(payload + q, amv2_inter_cost[c],
+                                amv2_inter_bits[c], amv2_inter_shift[c],
+                                amv2_inter_perm[c], d);
+                    amv2_add_both(dst, shown, c == 6 ? chain4 : dst, d);
+                    shown = NULL;
+                    break;
+                }
+                }
+                if (shown)
+                    memcpy(shown, dst, 4);
+                q += amv2_inter_cost[c];
+            }
+        }
+    }
+}
+
+/**
+ * YPbPr to RGB as the vendor's decoder does it.  Green's two chroma terms
+ * each round before they are summed.
+ */
+static void amv2_ypbpr_to_bgr(uint8_t y, uint8_t pb, uint8_t pr, uint8_t 
bgr[3])
+{
+    const int y64 = y * 64;
+    const int b_t = (int)((pb * 58032u) >> 9) - 14508;
+    const int r_t = (int)((pr * 45947u) >> 9) - 11487;
+    const int gb  = (int)((pb * 11325u) >> 9) - 2831;
+    const int gr  = (int)((pr * 23413u) >> 9) - 5853;
+
+    bgr[0] = av_clip_uint8((y64 + b_t) >> 6);
+    bgr[1] = av_clip_uint8((y64 - gb - gr) >> 6);
+    bgr[2] = av_clip_uint8((y64 + r_t) >> 6);
+}
+
+static int amv2_term(uint8_t v, unsigned coef)
+{
+    return (v * coef) >> 9;
+}
+
+/** RGB to YPbPr as the vendor's encoder does it. */
+static void amv2_bgr_to_ypbpr(uint8_t b, uint8_t g, uint8_t r, uint8_t 
ypbpr[3])
+{
+    const int y  = (amv2_term(r, 19589) + amv2_term(g, 38443) + amv2_term(b, 
7504)) >> 7;
+    const int pb = amv2_term(b, 32768) - amv2_term(g, 21706) - amv2_term(r, 
11056) + 16384;
+    const int pr = amv2_term(r, 32768) - amv2_term(g, 27414) - amv2_term(b, 
5348) + 16384;
+
+    ypbpr[0] = y;
+    ypbpr[1] = ((unsigned)pb & 0xFFFF) >> 7;
+    ypbpr[2] = ((unsigned)pr & 0xFFFF) >> 7;
+}
+
+/**
+ * The RGB layout's state is three 8 byte blocks per unit of Pb, Pr and Y, its
+ * units three of B, G and R; each block holds a left and a right 2x2 pixel
+ * area, whose lanes the unit has in column order and the state in row order.
+ */
+static void amv2_state_to_units(const AMV2Context *s, const uint8_t *state,
+                                uint8_t *units)
+{
+    for (size_t u = 0; u < (size_t)s->units_per_row * s->n_rows; u++) {
+        uint8_t in[24];
+        uint8_t *o = units + u * 24;
+
+        memcpy(in, state + u * 24, 24);
+        for (int h = 0; h < 2; h++) {
+            for (int c = 0; c < 4; c++) {
+                const int i = 4 * h + c, l = 4 * h + amv2_perm_col[c];
+                uint8_t bgr[3];
+
+                amv2_ypbpr_to_bgr(in[16 + i], in[i], in[8 + i], bgr);
+                o[l]      = bgr[0];
+                o[8 + l]  = bgr[1];
+                o[16 + l] = bgr[2];
+            }
+        }
+    }
+}
+
+static void amv2_state_from_units(const AMV2Context *s, const uint8_t *units,
+                                  uint8_t *state)
+{
+    for (size_t u = 0; u < (size_t)s->units_per_row * s->n_rows; u++) {
+        uint8_t in[24];
+        uint8_t *o = state + u * 24;
+
+        memcpy(in, units + u * 24, 24);
+        for (int h = 0; h < 2; h++) {
+            for (int c = 0; c < 4; c++) {
+                const int i = 4 * h + c, l = 4 * h + amv2_perm_col[c];
+                uint8_t ypbpr[3];
+
+                amv2_bgr_to_ypbpr(in[l], in[8 + l], in[16 + l], ypbpr);
+                o[i]      = ypbpr[1];
+                o[8 + i]  = ypbpr[2];
+                o[16 + i] = ypbpr[0];
+            }
+        }
+    }
+}
+
+/**
+ * The chunks of a frame in a layout: what a map has to add up to.
+ */
+static size_t amv2_chunks(int profile, int width, int height)
+{
+    return (size_t)width * height * (profile == AMV2_PROFILE_RGB ? 3 : 4) /
+           (profile == AMV2_PROFILE_RGB ? 8 : 16);
+}
+
+/**
+ * Decode a mode C record.
+ *
+ * Nothing is changed unless the map and the payload fit the frame exactly.
+ *
+ * @param cur    receives the frame's units, which the next frame's other
+ *               modes predict from
+ * @param shown  receives the units of the picture to show
+ */
+static int amv2_decode_c(AMV2Context *s, int key,
+                         const uint8_t *payload, size_t len,
+                         const uint8_t *aux, size_t aux_len,
+                         uint8_t *cur, const uint8_t **shown)
+{
+    const size_t n = (size_t)s->units_per_row * s->n_rows;
+    const size_t seg_start[AMV2_SEGMENTS] = {
+        0,
+        s->profile == AMV2_PROFILE_RGB ? n : 2 * n,
+        s->profile == AMV2_PROFILE_RGB ? 2 * n : 3 * n,
+    };
+    AMV2Cursor cur_seg[AMV2_SEGMENTS];
+    const uint8_t *end, *body = amv2_map_body(aux, aux_len, &end);
+    size_t chunks, intra, inter;
+    const uint8_t *prev;
+
+    amv2_map_scan(body, end, seg_start, &chunks, &intra, &inter, cur_seg);
+    if (chunks != n * s->blocks || (key ? intra : inter) != len)
+        return AVERROR_INVALIDDATA;
+
+    if (key) {
+        amv2_decode_intra(s, cur_seg, payload);
+    } else {
+        prev = s->mstate;
+        if (!s->have_mstate) {
+            /* the first inter frame after a raw or A/B one: the RGB layout
+             * primes its state from the units, the YUY2 one cannot */
+            if (s->profile != AMV2_PROFILE_RGB || s->ref < 0)
+                return AVERROR_INVALIDDATA;
+            amv2_state_from_units(s, s->buf[s->ref], s->scratch);
+            prev = s->scratch;
+        }
+        if (prev != s->mstate)
+            memcpy(s->mstate, prev, s->frame_bytes);
+        memcpy(s->display, s->mstate, s->frame_bytes);
+        amv2_decode_inter(s, cur_seg, payload);
+    }
+
+    if (s->profile == AMV2_PROFILE_RGB) {
+        amv2_state_to_units(s, s->mstate, cur);
+        amv2_state_to_units(s, s->display, s->display);
+    } else {
+        memcpy(cur, s->mstate, s->frame_bytes);
+    }
+    s->have_mstate = 1;
+    *shown = s->display;
+    return 0;
+}
+
+/** Tell the layout from the chunks a mode C map adds up to. */
+static int amv2_detect_c(AVCodecContext *avctx, const uint8_t *aux, size_t 
aux_len)
+{
+    const uint8_t *end, *body = amv2_map_body(aux, aux_len, &end);
+    size_t chunks, intra, inter;
+
+    amv2_map_scan(body, end, NULL, &chunks, &intra, &inter, NULL);
+    for (int p = AMV2_PROFILE_RGB; p <= AMV2_PROFILE_YUY2; p++)
+        if (chunks == amv2_chunks(p, avctx->width, avctx->height))
+            return amv2_set_profile(avctx, p);
+    return AVERROR_INVALIDDATA;
+}
+
 static int amv2_decode_frame(AVCodecContext *avctx, AVFrame *frame,
                              int *got_frame, AVPacket *avpkt)
 {
     AMV2Context *s = avctx->priv_data;
-    const uint8_t *data = avpkt->data;
-    size_t size, len;
+    const uint8_t *data = avpkt->data, *aux = NULL, *shown;
+    size_t size, len, aux_len = 0;
     uint32_t tag;
     uint8_t *cur;
     int key, raw, ret;
@@ -472,12 +952,18 @@ static int amv2_decode_frame(AVCodecContext *avctx, 
AVFrame *frame,
     len  = size - 8;
     data += 8;
 
-    /* what follows the record is mode C's auxiliary block */
-    if (avpkt->size > size)
-        return AVERROR_PATCHWELCOME;
+    /* whatever follows the record is mode C's auxiliary block, which opens
+     * with its own length */
+    if (avpkt->size > size) {
+        const size_t rem = avpkt->size - size;
+        const uint32_t decl = rem >= 4 ? AV_RL32(avpkt->data + size) : 0;
+
+        aux     = avpkt->data + size;
+        aux_len = decl >= 4 && decl <= rem ? decl : rem;
+    }
 
     /* a raw frame says which layout the stream has */
-    if (s->profile == AMV2_PROFILE_UNKNOWN && key) {
+    if (s->profile == AMV2_PROFILE_UNKNOWN && key && !aux_len) {
         for (int p = AMV2_PROFILE_RGB; p <= AMV2_PROFILE_YUY2; p++) {
             size_t raw_bytes = amv2_frame_bytes(p, avctx->width, 
avctx->height);
 
@@ -488,36 +974,51 @@ static int amv2_decode_frame(AVCodecContext *avctx, 
AVFrame *frame,
             }
         }
     }
-    raw = key && s->profile != AMV2_PROFILE_UNKNOWN &&
+    raw = !aux_len && key && s->profile != AMV2_PROFILE_UNKNOWN &&
           (len == s->frame_bytes || len == s->frame_bytes + AMV2_TRAILER);
 
-    if (!raw && (s->profile == AMV2_PROFILE_UNKNOWN ||
-                 s->mode == AMV2_MODE_UNKNOWN)) {
+    if (aux_len) {
+        if (s->profile == AMV2_PROFILE_UNKNOWN &&
+            (ret = amv2_detect_c(avctx, aux, aux_len)) < 0)
+            return ret;
+    } else if (!raw && (s->profile == AMV2_PROFILE_UNKNOWN ||
+                        s->mode == AMV2_MODE_UNKNOWN)) {
         if ((ret = amv2_detect(avctx, key, data, len)) < 0)
             return ret;
     }
 
-    cur = s->buf[s->ref == 0];
+    cur   = s->buf[s->ref == 0];
+    shown = cur;
     if (raw) {
         memcpy(cur, data, s->frame_bytes);
-    } else {
+    } else if (!aux_len) {
         if (s->ref < 0)
             amv2_neutral_frame(s, cur);
         else if (!key)
             memcpy(cur, s->buf[s->ref], s->frame_bytes);
         if ((ret = amv2_walk(s, s->mode, key, data, len, cur)) < 0)
             return ret;
+    } else {
+        /* an inter frame needs the state of the frame before it, or units
+         * the RGB layout can make one from */
+        if (!key && !s->have_mstate &&
+            (s->profile != AMV2_PROFILE_RGB || s->ref < 0))
+            return AVERROR_INVALIDDATA;
+        if ((ret = amv2_decode_c(s, key, data, len, aux, aux_len, cur, 
&shown)) < 0)
+            return ret;
     }
+    if (!aux_len)
+        s->have_mstate = 0;     /* the state no longer follows the frames */
     s->ref = s->ref == 0;
 
     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
 
     if (s->profile == AMV2_PROFILE_RGB)
-        amv2_rgb_to_picture(cur, frame->data[0], frame->linesize[0],
+        amv2_rgb_to_picture(shown, frame->data[0], frame->linesize[0],
                             avctx->width, avctx->height);
     else
-        amv2_yuy2_to_picture(cur, frame->data[0], frame->linesize[0],
+        amv2_yuy2_to_picture(shown, frame->data[0], frame->linesize[0],
                              avctx->width, avctx->height);
     if (s->interlace)
         amv2_interlace(frame->data[0], frame->linesize[0],
@@ -534,6 +1035,14 @@ static int amv2_decode_frame(AVCodecContext *avctx, 
AVFrame *frame,
     return avpkt->size;
 }
 
+static av_cold void amv2_decode_flush(AVCodecContext *avctx)
+{
+    AMV2Context *s = avctx->priv_data;
+
+    s->ref         = -1;
+    s->have_mstate = 0;
+}
+
 #define OFFSET(x) offsetof(AMV2Context, x)
 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
 static const AVOption amv2_options[] = {
@@ -559,6 +1068,7 @@ const FFCodec ff_amarec_amv2_decoder = {
     .priv_data_size = sizeof(AMV2Context),
     .init           = amv2_decode_init,
     .close          = amv2_decode_close,
+    .flush          = amv2_decode_flush,
     FF_CODEC_DECODE_CB(amv2_decode_frame),
     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
 };
-- 
2.54.0


>From cbcf257bff7ae3dddcf98b96b1ed1705e6879831 Mon Sep 17 00:00:00 2001
From: Tom Needham <[email protected]>
Date: Sat, 31 Oct 2026 20:31:30 +0000
Subject: [PATCH 5/5] fate: add AMV2 tests

Add tests for the AMAREC AMV2 decoder and demuxer: excerpts of the six
AmaRecTV sample clips (both layouts, modes A, B and C: a keyframe, its
coded inter frames and one that codes nothing), the codec at other sizes
from 8x4 to 328x244, interlaced streams, AVI files whose stream format
gives the layout, mode and interlace flag, streams that switch between
raw, A, B and C records (which exercise the state mode C primes or
drops), streams that begin with an inter frame or contain records that
must be refused, and records of random content that reach every code of
every mode and every overflow of the wrapping state.

The references were checked against an independent decoder of the format
frame by frame.  The samples are to go into the fate-suite as amv2/.

Signed-off-by: Tom Needham <[email protected]>
---
 tests/Makefile                          |  1 +
 tests/fate/amv2.mak                     | 53 +++++++++++++++++++++++++
 tests/ref/fate/amv2-avi-r2-il           | 11 +++++
 tests/ref/fate/amv2-avi-r3-il           | 11 +++++
 tests/ref/fate/amv2-avi-y1              | 11 +++++
 tests/ref/fate/amv2-avi-y2-rgbin        | 11 +++++
 tests/ref/fate/amv2-avi-y3-rgbin-il     | 11 +++++
 tests/ref/fate/amv2-mix-r-fail          | 13 ++++++
 tests/ref/fate/amv2-mix-r-neutral       | 11 +++++
 tests/ref/fate/amv2-mix-r-prime         | 17 ++++++++
 tests/ref/fate/amv2-mix-y-fail          | 11 +++++
 tests/ref/fate/amv2-mix-y-neutral       | 11 +++++
 tests/ref/fate/amv2-r1                  |  8 ++++
 tests/ref/fate/amv2-r2                  | 10 +++++
 tests/ref/fate/amv2-r3                  | 11 +++++
 tests/ref/fate/amv2-r3-328x244          |  7 ++++
 tests/ref/fate/amv2-r3-88x44-il         | 11 +++++
 tests/ref/fate/amv2-rand-a-r            | 19 +++++++++
 tests/ref/fate/amv2-rand-a-y-8x4        | 19 +++++++++
 tests/ref/fate/amv2-rand-b-y            | 19 +++++++++
 tests/ref/fate/amv2-rand-c-dense-r      | 19 +++++++++
 tests/ref/fate/amv2-rand-c-dense-y-16x8 | 19 +++++++++
 tests/ref/fate/amv2-rand-c-r-8x4        | 19 +++++++++
 tests/ref/fate/amv2-rand-c-y            | 19 +++++++++
 tests/ref/fate/amv2-y1                  | 19 +++++++++
 tests/ref/fate/amv2-y2                  | 11 +++++
 tests/ref/fate/amv2-y3                  | 19 +++++++++
 tests/ref/fate/amv2-y3-328x244          |  7 ++++
 28 files changed, 408 insertions(+)
 create mode 100644 tests/fate/amv2.mak
 create mode 100644 tests/ref/fate/amv2-avi-r2-il
 create mode 100644 tests/ref/fate/amv2-avi-r3-il
 create mode 100644 tests/ref/fate/amv2-avi-y1
 create mode 100644 tests/ref/fate/amv2-avi-y2-rgbin
 create mode 100644 tests/ref/fate/amv2-avi-y3-rgbin-il
 create mode 100644 tests/ref/fate/amv2-mix-r-fail
 create mode 100644 tests/ref/fate/amv2-mix-r-neutral
 create mode 100644 tests/ref/fate/amv2-mix-r-prime
 create mode 100644 tests/ref/fate/amv2-mix-y-fail
 create mode 100644 tests/ref/fate/amv2-mix-y-neutral
 create mode 100644 tests/ref/fate/amv2-r1
 create mode 100644 tests/ref/fate/amv2-r2
 create mode 100644 tests/ref/fate/amv2-r3
 create mode 100644 tests/ref/fate/amv2-r3-328x244
 create mode 100644 tests/ref/fate/amv2-r3-88x44-il
 create mode 100644 tests/ref/fate/amv2-rand-a-r
 create mode 100644 tests/ref/fate/amv2-rand-a-y-8x4
 create mode 100644 tests/ref/fate/amv2-rand-b-y
 create mode 100644 tests/ref/fate/amv2-rand-c-dense-r
 create mode 100644 tests/ref/fate/amv2-rand-c-dense-y-16x8
 create mode 100644 tests/ref/fate/amv2-rand-c-r-8x4
 create mode 100644 tests/ref/fate/amv2-rand-c-y
 create mode 100644 tests/ref/fate/amv2-y1
 create mode 100644 tests/ref/fate/amv2-y2
 create mode 100644 tests/ref/fate/amv2-y3
 create mode 100644 tests/ref/fate/amv2-y3-328x244

diff --git a/tests/Makefile b/tests/Makefile
index 365c6e7c194..74a01ac8d32 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -171,6 +171,7 @@ include $(SRC_PATH)/tests/fate/alac.mak
 include $(SRC_PATH)/tests/fate/als.mak
 include $(SRC_PATH)/tests/fate/amrnb.mak
 include $(SRC_PATH)/tests/fate/amrwb.mak
+include $(SRC_PATH)/tests/fate/amv2.mak
 include $(SRC_PATH)/tests/fate/api.mak
 include $(SRC_PATH)/tests/fate/apng.mak
 include $(SRC_PATH)/tests/fate/apv.mak
diff --git a/tests/fate/amv2.mak b/tests/fate/amv2.mak
new file mode 100644
index 00000000000..e8a44035950
--- /dev/null
+++ b/tests/fate/amv2.mak
@@ -0,0 +1,53 @@
+# AMV2 streams: excerpts of the AmaRecTV sample clips R1-R3 and Y1-Y3, which
+# are the two layouts and the three modes at 320x240 (a keyframe, its coded
+# inter frames and one that codes nothing; dropping the others changes no
+# frame kept), the same codec at other sizes and fed by other captures,
+# interlaced, in an AVI or bare, and records of random content that reach
+# every code and every overflow.
+FATE_AMV2_FLAT = fate-amv2-r1 fate-amv2-r2 fate-amv2-r3                        
\
+                 fate-amv2-y1 fate-amv2-y2 fate-amv2-y3                        
\
+                 fate-amv2-r3-328x244 fate-amv2-y3-328x244                     
\
+                 fate-amv2-r3-88x44-il                                         
\
+                 fate-amv2-rand-c-dense-r fate-amv2-rand-c-y                   
\
+                 fate-amv2-rand-a-r fate-amv2-rand-b-y                         
\
+                 fate-amv2-rand-c-r-8x4 fate-amv2-rand-a-y-8x4                 
\
+                 fate-amv2-rand-c-dense-y-16x8                                 
\
+                 fate-amv2-mix-r-prime fate-amv2-mix-r-fail                    
\
+                 fate-amv2-mix-r-neutral fate-amv2-mix-y-neutral               
\
+                 fate-amv2-mix-y-fail
+
+FATE_AMV2_AVI = fate-amv2-avi-r3-il fate-amv2-avi-r2-il fate-amv2-avi-y1      \
+                fate-amv2-avi-y2-rgbin fate-amv2-avi-y3-rgbin-il
+
+FATE_SAMPLES_FFMPEG-$(call FRAMECRC, AMV2, AMAREC_AMV2) += $(FATE_AMV2_FLAT)
+FATE_SAMPLES_FFMPEG-$(call FRAMECRC, AVI, AMAREC_AMV2)  += $(FATE_AMV2_AVI)
+
+fate-amv2: $(FATE_AMV2_FLAT) $(FATE_AMV2_AVI)
+
+fate-amv2-r1:                 CMD = framecrc -i $(TARGET_SAMPLES)/amv2/r1.amv2
+fate-amv2-r2:                 CMD = framecrc -i $(TARGET_SAMPLES)/amv2/r2.amv2
+fate-amv2-r3:                 CMD = framecrc -i $(TARGET_SAMPLES)/amv2/r3.amv2
+fate-amv2-y1:                 CMD = framecrc -i $(TARGET_SAMPLES)/amv2/y1.amv2
+fate-amv2-y2:                 CMD = framecrc -i $(TARGET_SAMPLES)/amv2/y2.amv2
+fate-amv2-y3:                 CMD = framecrc -i $(TARGET_SAMPLES)/amv2/y3.amv2
+fate-amv2-r3-328x244:         CMD = framecrc -video_size 328x244 -i 
$(TARGET_SAMPLES)/amv2/r3-328x244.amv2
+fate-amv2-y3-328x244:         CMD = framecrc -video_size 328x244 -i 
$(TARGET_SAMPLES)/amv2/y3-328x244.amv2
+fate-amv2-r3-88x44-il:        CMD = framecrc -interlace 1 -video_size 88x44 -i 
$(TARGET_SAMPLES)/amv2/r3-88x44-il.amv2
+fate-amv2-rand-c-dense-r:     CMD = framecrc -video_size 88x44 -i 
$(TARGET_SAMPLES)/amv2/rand-c-dense-r.amv2
+fate-amv2-rand-c-y:           CMD = framecrc -video_size 88x44 -i 
$(TARGET_SAMPLES)/amv2/rand-c-y.amv2
+fate-amv2-rand-a-r:           CMD = framecrc -video_size 88x44 -i 
$(TARGET_SAMPLES)/amv2/rand-a-r.amv2
+fate-amv2-rand-b-y:           CMD = framecrc -video_size 88x44 -i 
$(TARGET_SAMPLES)/amv2/rand-b-y.amv2
+fate-amv2-rand-c-r-8x4:       CMD = framecrc -video_size 8x4 -i 
$(TARGET_SAMPLES)/amv2/rand-c-r-8x4.amv2
+fate-amv2-rand-a-y-8x4:       CMD = framecrc -video_size 8x4 -i 
$(TARGET_SAMPLES)/amv2/rand-a-y-8x4.amv2
+fate-amv2-rand-c-dense-y-16x8: CMD = framecrc -video_size 16x8 -i 
$(TARGET_SAMPLES)/amv2/rand-c-dense-y-16x8.amv2
+fate-amv2-mix-r-prime:        CMD = framecrc -video_size 88x44 -i 
$(TARGET_SAMPLES)/amv2/mix-r-prime.amv2
+fate-amv2-mix-r-fail:         CMD = framecrc -video_size 88x44 -i 
$(TARGET_SAMPLES)/amv2/mix-r-fail.amv2
+fate-amv2-mix-r-neutral:      CMD = framecrc -video_size 88x44 -i 
$(TARGET_SAMPLES)/amv2/mix-r-neutral.amv2
+fate-amv2-mix-y-neutral:      CMD = framecrc -video_size 88x44 -i 
$(TARGET_SAMPLES)/amv2/mix-y-neutral.amv2
+fate-amv2-mix-y-fail:         CMD = framecrc -video_size 88x44 -i 
$(TARGET_SAMPLES)/amv2/mix-y-fail.amv2
+
+fate-amv2-avi-r3-il:          CMD = framecrc -i 
$(TARGET_SAMPLES)/amv2/r3-88x44-il.avi
+fate-amv2-avi-r2-il:          CMD = framecrc -i 
$(TARGET_SAMPLES)/amv2/r2-88x44-il.avi
+fate-amv2-avi-y1:             CMD = framecrc -i 
$(TARGET_SAMPLES)/amv2/y1-88x44.avi
+fate-amv2-avi-y2-rgbin:       CMD = framecrc -i 
$(TARGET_SAMPLES)/amv2/y2-88x44-rgbin.avi
+fate-amv2-avi-y3-rgbin-il:    CMD = framecrc -i 
$(TARGET_SAMPLES)/amv2/y3-88x44-rgbin-il.avi
diff --git a/tests/ref/fate/amv2-avi-r2-il b/tests/ref/fate/amv2-avi-r2-il
new file mode 100644
index 00000000000..e739614f266
--- /dev/null
+++ b/tests/ref/fate/amv2-avi-r2-il
@@ -0,0 +1,11 @@
+#tb 0: 1/30
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 88x44
+#sar 0: 0/1
+0,          0,          0,        1,    11616, 0x2c6184e2
+0,          1,          1,        1,    11616, 0x0bba84ec
+0,          2,          2,        1,    11616, 0x347621be
+0,          3,          3,        1,    11616, 0xb76522f5
+0,          4,          4,        1,    11616, 0xeacc22ab
+0,          5,          5,        1,    11616, 0x24e92633
diff --git a/tests/ref/fate/amv2-avi-r3-il b/tests/ref/fate/amv2-avi-r3-il
new file mode 100644
index 00000000000..7f40db3ff7a
--- /dev/null
+++ b/tests/ref/fate/amv2-avi-r3-il
@@ -0,0 +1,11 @@
+#tb 0: 1/30
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 88x44
+#sar 0: 0/1
+0,          0,          0,        1,    11616, 0x5d62480c
+0,          1,          1,        1,    11616, 0x17cb4a56
+0,          2,          2,        1,    11616, 0x0798ed19
+0,          3,          3,        1,    11616, 0x2974e32c
+0,          4,          4,        1,    11616, 0x7ec8d182
+0,          5,          5,        1,    11616, 0xfb6caa92
diff --git a/tests/ref/fate/amv2-avi-y1 b/tests/ref/fate/amv2-avi-y1
new file mode 100644
index 00000000000..8e530b3cc97
--- /dev/null
+++ b/tests/ref/fate/amv2-avi-y1
@@ -0,0 +1,11 @@
+#tb 0: 1/30
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 88x44
+#sar 0: 0/1
+0,          0,          0,        1,     7744, 0x4fef010c
+0,          1,          1,        1,     7744, 0xaec9010f
+0,          2,          2,        1,     7744, 0x3437e22f
+0,          3,          3,        1,     7744, 0xb3c9e259
+0,          4,          4,        1,     7744, 0x821be316
+0,          5,          5,        1,     7744, 0xbc47e5e5
diff --git a/tests/ref/fate/amv2-avi-y2-rgbin b/tests/ref/fate/amv2-avi-y2-rgbin
new file mode 100644
index 00000000000..18a13bb9c09
--- /dev/null
+++ b/tests/ref/fate/amv2-avi-y2-rgbin
@@ -0,0 +1,11 @@
+#tb 0: 1/30
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 88x44
+#sar 0: 0/1
+0,          0,          0,        1,     7744, 0xb3240323
+0,          1,          1,        1,     7744, 0xae6f0338
+0,          2,          2,        1,     7744, 0xd1b2e3ed
+0,          3,          3,        1,     7744, 0x86e2e3a3
+0,          4,          4,        1,     7744, 0x44ade3be
+0,          5,          5,        1,     7744, 0x39a9e3c6
diff --git a/tests/ref/fate/amv2-avi-y3-rgbin-il 
b/tests/ref/fate/amv2-avi-y3-rgbin-il
new file mode 100644
index 00000000000..9d79e68a1a2
--- /dev/null
+++ b/tests/ref/fate/amv2-avi-y3-rgbin-il
@@ -0,0 +1,11 @@
+#tb 0: 1/30
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 88x44
+#sar 0: 0/1
+0,          0,          0,        1,     7744, 0xb3240323
+0,          1,          1,        1,     7744, 0xa9d303f3
+0,          2,          2,        1,     7744, 0xfce9e318
+0,          3,          3,        1,     7744, 0xda8ae197
+0,          4,          4,        1,     7744, 0x3ff5dd37
+0,          5,          5,        1,     7744, 0xdcbdd041
diff --git a/tests/ref/fate/amv2-mix-r-fail b/tests/ref/fate/amv2-mix-r-fail
new file mode 100644
index 00000000000..23993750a61
--- /dev/null
+++ b/tests/ref/fate/amv2-mix-r-fail
@@ -0,0 +1,13 @@
+#tb 0: 1/30
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 88x44
+#sar 0: 0/1
+0,          2,          2,        1,    11616, 0x2c6184e2
+0,          3,          3,        1,    11616, 0x21cc84ed
+0,          4,          4,        1,    11616, 0x60f1373a
+0,          5,          5,        1,    11616, 0x5d62480c
+0,          6,          6,        1,    11616, 0xfda7ddc3
+0,          7,          7,        1,    11616, 0x198ff544
+0,          8,          8,        1,    11616, 0x85e16d4c
+0,          9,          9,        1,    11616, 0x0bea4eee
diff --git a/tests/ref/fate/amv2-mix-r-neutral 
b/tests/ref/fate/amv2-mix-r-neutral
new file mode 100644
index 00000000000..baa4b68d0a7
--- /dev/null
+++ b/tests/ref/fate/amv2-mix-r-neutral
@@ -0,0 +1,11 @@
+#tb 0: 1/30
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 88x44
+#sar 0: 0/1
+0,          0,          0,        1,    11616, 0x608afd0c
+0,          1,          1,        1,    11616, 0xc7b31f4f
+0,          2,          2,        1,    11616, 0x832d2530
+0,          3,          3,        1,    11616, 0x5d62480c
+0,          4,          4,        1,    11616, 0x419e4a5e
+0,          5,          5,        1,    11616, 0x3d0d4756
diff --git a/tests/ref/fate/amv2-mix-r-prime b/tests/ref/fate/amv2-mix-r-prime
new file mode 100644
index 00000000000..db0de812a4d
--- /dev/null
+++ b/tests/ref/fate/amv2-mix-r-prime
@@ -0,0 +1,17 @@
+#tb 0: 1/30
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 88x44
+#sar 0: 0/1
+0,          0,          0,        1,    11616, 0x2c6184e2
+0,          1,          1,        1,    11616, 0x419e4a5e
+0,          2,          2,        1,    11616, 0xe5b8ecb9
+0,          3,          3,        1,    11616, 0x2dd7e243
+0,          4,          4,        1,    11616, 0x07317a18
+0,          5,          5,        1,    11616, 0x19164979
+0,          6,          6,        1,    11616, 0x5d62480c
+0,          7,          7,        1,    11616, 0x74a164d2
+0,          8,          8,        1,    11616, 0x2c6184e2
+0,          9,          9,        1,    11616, 0x86bb2079
+0,         10,         10,        1,    11616, 0xbfa32541
+0,         12,         12,        1,    11616, 0x3f6e3ba8
diff --git a/tests/ref/fate/amv2-mix-y-fail b/tests/ref/fate/amv2-mix-y-fail
new file mode 100644
index 00000000000..bfb20fee19b
--- /dev/null
+++ b/tests/ref/fate/amv2-mix-y-fail
@@ -0,0 +1,11 @@
+#tb 0: 1/30
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 88x44
+#sar 0: 0/1
+0,          0,          0,        1,     7744, 0x4fef010c
+0,          4,          4,        1,     7744, 0x1635ebcf
+0,          6,          6,        1,     7744, 0x4fef010c
+0,          7,          7,        1,     7744, 0xddd57fed
+0,          8,          8,        1,     7744, 0x4fef010c
+0,         11,         11,        1,     7744, 0x911fe901
diff --git a/tests/ref/fate/amv2-mix-y-neutral 
b/tests/ref/fate/amv2-mix-y-neutral
new file mode 100644
index 00000000000..e775007dcac
--- /dev/null
+++ b/tests/ref/fate/amv2-mix-y-neutral
@@ -0,0 +1,11 @@
+#tb 0: 1/30
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 88x44
+#sar 0: 0/1
+0,          0,          0,        1,     7744, 0x7870559c
+0,          1,          1,        1,     7744, 0x20d5ddf8
+0,          2,          2,        1,     7744, 0xbc47e5e5
+0,          3,          3,        1,     7744, 0x4fef010c
+0,          4,          4,        1,     7744, 0x87bf01d4
+0,          5,          5,        1,     7744, 0x1470f5ad
diff --git a/tests/ref/fate/amv2-r1 b/tests/ref/fate/amv2-r1
new file mode 100644
index 00000000000..3e31be6e9f1
--- /dev/null
+++ b/tests/ref/fate/amv2-r1
@@ -0,0 +1,8 @@
+#tb 0: 1/30
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 320x240
+#sar 0: 0/1
+0,          0,          0,        1,   230400, 0x0bdde8ef
+0,          1,          1,        1,   230400, 0xb6fa0b2a
+0,          2,          2,        1,   230400, 0xb6fa0b2a
diff --git a/tests/ref/fate/amv2-r2 b/tests/ref/fate/amv2-r2
new file mode 100644
index 00000000000..f2ff9f11422
--- /dev/null
+++ b/tests/ref/fate/amv2-r2
@@ -0,0 +1,10 @@
+#tb 0: 1/30
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 320x240
+#sar 0: 0/1
+0,          0,          0,        1,   230400, 0x9d2be128
+0,          1,          1,        1,   230400, 0x9d2be128
+0,          2,          2,        1,   230400, 0xed7de4bf
+0,          3,          3,        1,   230400, 0xc0e20667
+0,          4,          4,        1,   230400, 0x21b2fb0c
diff --git a/tests/ref/fate/amv2-r3 b/tests/ref/fate/amv2-r3
new file mode 100644
index 00000000000..15874c77317
--- /dev/null
+++ b/tests/ref/fate/amv2-r3
@@ -0,0 +1,11 @@
+#tb 0: 1/30
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 320x240
+#sar 0: 0/1
+0,          0,          0,        1,   230400, 0xe8c38395
+0,          1,          1,        1,   230400, 0x1a7d3460
+0,          2,          2,        1,   230400, 0xf5159119
+0,          3,          3,        1,   230400, 0x9367b7cd
+0,          4,          4,        1,   230400, 0x2789c1c1
+0,          5,          5,        1,   230400, 0x2789c1c1
diff --git a/tests/ref/fate/amv2-r3-328x244 b/tests/ref/fate/amv2-r3-328x244
new file mode 100644
index 00000000000..b25d6e96005
--- /dev/null
+++ b/tests/ref/fate/amv2-r3-328x244
@@ -0,0 +1,7 @@
+#tb 0: 1/30
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 328x244
+#sar 0: 0/1
+0,          0,          0,        1,   240096, 0xe8c97e96
+0,          1,          1,        1,   240096, 0x99d3af30
diff --git a/tests/ref/fate/amv2-r3-88x44-il b/tests/ref/fate/amv2-r3-88x44-il
new file mode 100644
index 00000000000..7f40db3ff7a
--- /dev/null
+++ b/tests/ref/fate/amv2-r3-88x44-il
@@ -0,0 +1,11 @@
+#tb 0: 1/30
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 88x44
+#sar 0: 0/1
+0,          0,          0,        1,    11616, 0x5d62480c
+0,          1,          1,        1,    11616, 0x17cb4a56
+0,          2,          2,        1,    11616, 0x0798ed19
+0,          3,          3,        1,    11616, 0x2974e32c
+0,          4,          4,        1,    11616, 0x7ec8d182
+0,          5,          5,        1,    11616, 0xfb6caa92
diff --git a/tests/ref/fate/amv2-rand-a-r b/tests/ref/fate/amv2-rand-a-r
new file mode 100644
index 00000000000..6c62b671a7e
--- /dev/null
+++ b/tests/ref/fate/amv2-rand-a-r
@@ -0,0 +1,19 @@
+#tb 0: 1/30
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 88x44
+#sar 0: 0/1
+0,          0,          0,        1,    11616, 0xb637bb54
+0,          1,          1,        1,    11616, 0x0525b030
+0,          2,          2,        1,    11616, 0xb472a5b6
+0,          3,          3,        1,    11616, 0xdbccb753
+0,          4,          4,        1,    11616, 0x7ab4b0d6
+0,          5,          5,        1,    11616, 0x7d51c298
+0,          6,          6,        1,    11616, 0xc1366fd9
+0,          7,          7,        1,    11616, 0x2c305e83
+0,          8,          8,        1,    11616, 0x3f3b575e
+0,          9,          9,        1,    11616, 0x284d5719
+0,         10,         10,        1,    11616, 0x2c5f4b7b
+0,         11,         11,        1,    11616, 0x286b7b8a
+0,         12,         12,        1,    11616, 0x33ef18cb
+0,         13,         13,        1,    11616, 0xf4883bd0
diff --git a/tests/ref/fate/amv2-rand-a-y-8x4 b/tests/ref/fate/amv2-rand-a-y-8x4
new file mode 100644
index 00000000000..3666c2452d1
--- /dev/null
+++ b/tests/ref/fate/amv2-rand-a-y-8x4
@@ -0,0 +1,19 @@
+#tb 0: 1/30
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 8x4
+#sar 0: 0/1
+0,          0,          0,        1,       64, 0x680a1c4d
+0,          1,          1,        1,       64, 0x680a1c4d
+0,          2,          2,        1,       64, 0x28832086
+0,          3,          3,        1,       64, 0x08fa2071
+0,          4,          4,        1,       64, 0x08fa2071
+0,          5,          5,        1,       64, 0xf1ae1f25
+0,          6,          6,        1,       64, 0x749606e4
+0,          7,          7,        1,       64, 0xb7620a57
+0,          8,          8,        1,       64, 0xb7620a57
+0,          9,          9,        1,       64, 0xb1960aab
+0,         10,         10,        1,       64, 0x5ad9135d
+0,         11,         11,        1,       64, 0xa98a1a35
+0,         12,         12,        1,       64, 0x00000000
+0,         13,         13,        1,       64, 0x9d200a33
diff --git a/tests/ref/fate/amv2-rand-b-y b/tests/ref/fate/amv2-rand-b-y
new file mode 100644
index 00000000000..3ee6d5a219e
--- /dev/null
+++ b/tests/ref/fate/amv2-rand-b-y
@@ -0,0 +1,19 @@
+#tb 0: 1/30
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 88x44
+#sar 0: 0/1
+0,          0,          0,        1,     7744, 0x1efa1761
+0,          1,          1,        1,     7744, 0xa5b71482
+0,          2,          2,        1,     7744, 0xa183235c
+0,          3,          3,        1,     7744, 0x03a71b3f
+0,          4,          4,        1,     7744, 0x9d081fc3
+0,          5,          5,        1,     7744, 0xcc0c237e
+0,          6,          6,        1,     7744, 0xbd9a7087
+0,          7,          7,        1,     7744, 0x442572b1
+0,          8,          8,        1,     7744, 0x808d5d1c
+0,          9,          9,        1,     7744, 0x26085b2d
+0,         10,         10,        1,     7744, 0xff7e32e1
+0,         11,         11,        1,     7744, 0xf5fd24f1
+0,         12,         12,        1,     7744, 0x39bd8447
+0,         13,         13,        1,     7744, 0x8246b8ba
diff --git a/tests/ref/fate/amv2-rand-c-dense-r 
b/tests/ref/fate/amv2-rand-c-dense-r
new file mode 100644
index 00000000000..fe8e006a688
--- /dev/null
+++ b/tests/ref/fate/amv2-rand-c-dense-r
@@ -0,0 +1,19 @@
+#tb 0: 1/30
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 88x44
+#sar 0: 0/1
+0,          0,          0,        1,    11616, 0x4a1aad53
+0,          1,          1,        1,    11616, 0xb4717d65
+0,          2,          2,        1,    11616, 0x28acf464
+0,          3,          3,        1,    11616, 0x23ff51bd
+0,          4,          4,        1,    11616, 0x163a4996
+0,          5,          5,        1,    11616, 0xf0124ad8
+0,          6,          6,        1,    11616, 0xcb3b3dba
+0,          7,          7,        1,    11616, 0xfe3b5bf1
+0,          8,          8,        1,    11616, 0xfa37067e
+0,          9,          9,        1,    11616, 0xb91fcc33
+0,         10,         10,        1,    11616, 0x9433676b
+0,         11,         11,        1,    11616, 0xeede1721
+0,         12,         12,        1,    11616, 0x6b6e8a1f
+0,         13,         13,        1,    11616, 0x17730862
diff --git a/tests/ref/fate/amv2-rand-c-dense-y-16x8 
b/tests/ref/fate/amv2-rand-c-dense-y-16x8
new file mode 100644
index 00000000000..abec1a6515b
--- /dev/null
+++ b/tests/ref/fate/amv2-rand-c-dense-y-16x8
@@ -0,0 +1,19 @@
+#tb 0: 1/30
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 16x8
+#sar 0: 0/1
+0,          0,          0,        1,      256, 0x0a3150a5
+0,          1,          1,        1,      256, 0xd20b6a9d
+0,          2,          2,        1,      256, 0x1df690cc
+0,          3,          3,        1,      256, 0xa55f9123
+0,          4,          4,        1,      256, 0x788d7f15
+0,          5,          5,        1,      256, 0x23c5832a
+0,          6,          6,        1,      256, 0xe76569ca
+0,          7,          7,        1,      256, 0xa81a73f6
+0,          8,          8,        1,      256, 0x8adf7b50
+0,          9,          9,        1,      256, 0x667586f8
+0,         10,         10,        1,      256, 0x5eb18b32
+0,         11,         11,        1,      256, 0x19da8bc5
+0,         12,         12,        1,      256, 0x0766822f
+0,         13,         13,        1,      256, 0x2066820a
diff --git a/tests/ref/fate/amv2-rand-c-r-8x4 b/tests/ref/fate/amv2-rand-c-r-8x4
new file mode 100644
index 00000000000..58f56699c82
--- /dev/null
+++ b/tests/ref/fate/amv2-rand-c-r-8x4
@@ -0,0 +1,19 @@
+#tb 0: 1/30
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 8x4
+#sar 0: 0/1
+0,          0,          0,        1,       96, 0x8c702e57
+0,          1,          1,        1,       96, 0x81c12e50
+0,          2,          2,        1,       96, 0xaf0c2a15
+0,          3,          3,        1,       96, 0xa52735da
+0,          4,          4,        1,       96, 0x088b2efd
+0,          5,          5,        1,       96, 0x088b2efd
+0,          6,          6,        1,       96, 0xc7dc2e4d
+0,          7,          7,        1,       96, 0xabf62be0
+0,          8,          8,        1,       96, 0x7b352b12
+0,          9,          9,        1,       96, 0x7b352b12
+0,         10,         10,        1,       96, 0x6028294d
+0,         11,         11,        1,       96, 0xb4f32c96
+0,         12,         12,        1,       96, 0x8e602b29
+0,         13,         13,        1,       96, 0x46442fb9
diff --git a/tests/ref/fate/amv2-rand-c-y b/tests/ref/fate/amv2-rand-c-y
new file mode 100644
index 00000000000..d8414ec94a3
--- /dev/null
+++ b/tests/ref/fate/amv2-rand-c-y
@@ -0,0 +1,19 @@
+#tb 0: 1/30
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 88x44
+#sar 0: 0/1
+0,          0,          0,        1,     7744, 0x7a7106ea
+0,          1,          1,        1,     7744, 0x63f31ce5
+0,          2,          2,        1,     7744, 0x48c85f3d
+0,          3,          3,        1,     7744, 0x6684e879
+0,          4,          4,        1,     7744, 0xf8629be8
+0,          5,          5,        1,     7744, 0xfee55f2f
+0,          6,          6,        1,     7744, 0xd00f917a
+0,          7,          7,        1,     7744, 0x28507ee4
+0,          8,          8,        1,     7744, 0x1317ab9e
+0,          9,          9,        1,     7744, 0x0830c8aa
+0,         10,         10,        1,     7744, 0x30370540
+0,         11,         11,        1,     7744, 0xb847f044
+0,         12,         12,        1,     7744, 0x6e357cd9
+0,         13,         13,        1,     7744, 0x41889676
diff --git a/tests/ref/fate/amv2-y1 b/tests/ref/fate/amv2-y1
new file mode 100644
index 00000000000..8be9759f1bf
--- /dev/null
+++ b/tests/ref/fate/amv2-y1
@@ -0,0 +1,19 @@
+#tb 0: 1/30
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 320x240
+#sar 0: 0/1
+0,          0,          0,        1,   153600, 0x9a0ba09c
+0,          1,          1,        1,   153600, 0x9a0ba09c
+0,          2,          2,        1,   153600, 0x352da09c
+0,          3,          3,        1,   153600, 0x3f7fa0ae
+0,          4,          4,        1,   153600, 0xb8caa26a
+0,          5,          5,        1,   153600, 0x87d9a984
+0,          6,          6,        1,   153600, 0x0edf624a
+0,          7,          7,        1,   153600, 0x6ec2698a
+0,          8,          8,        1,   153600, 0x032f86da
+0,          9,          9,        1,   153600, 0x1043e7ef
+0,         10,         10,        1,   153600, 0xd86ce3fd
+0,         11,         11,        1,   153600, 0x0ed035c7
+0,         12,         12,        1,   153600, 0x67f07e8d
+0,         13,         13,        1,   153600, 0x2477c5f6
diff --git a/tests/ref/fate/amv2-y2 b/tests/ref/fate/amv2-y2
new file mode 100644
index 00000000000..e96107b95a1
--- /dev/null
+++ b/tests/ref/fate/amv2-y2
@@ -0,0 +1,11 @@
+#tb 0: 1/30
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 320x240
+#sar 0: 0/1
+0,          0,          0,        1,   153600, 0xecd87e1a
+0,          1,          1,        1,   153600, 0xfb1d3b13
+0,          2,          2,        1,   153600, 0xfb1d3b13
+0,          3,          3,        1,   153600, 0xe70b3cca
+0,          4,          4,        1,   153600, 0x6a913991
+0,          5,          5,        1,   153600, 0x631a3833
diff --git a/tests/ref/fate/amv2-y3 b/tests/ref/fate/amv2-y3
new file mode 100644
index 00000000000..acd25da17f7
--- /dev/null
+++ b/tests/ref/fate/amv2-y3
@@ -0,0 +1,19 @@
+#tb 0: 1/30
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 320x240
+#sar 0: 0/1
+0,          0,          0,        1,   153600, 0xd87cfd95
+0,          1,          1,        1,   153600, 0xd9c85224
+0,          2,          2,        1,   153600, 0xf475578a
+0,          3,          3,        1,   153600, 0x33475cc9
+0,          4,          4,        1,   153600, 0xb9fb5ed2
+0,          5,          5,        1,   153600, 0xa5455efd
+0,          6,          6,        1,   153600, 0xa5455efd
+0,          7,          7,        1,   153600, 0xd3655efa
+0,          8,          8,        1,   153600, 0x8ed0a90f
+0,          9,          9,        1,   153600, 0xdc15bdaf
+0,         10,         10,        1,   153600, 0x7c8eea86
+0,         11,         11,        1,   153600, 0xed2ef940
+0,         12,         12,        1,   153600, 0x72b9fd28
+0,         13,         13,        1,   153600, 0x72b9fd28
diff --git a/tests/ref/fate/amv2-y3-328x244 b/tests/ref/fate/amv2-y3-328x244
new file mode 100644
index 00000000000..e34342e04ae
--- /dev/null
+++ b/tests/ref/fate/amv2-y3-328x244
@@ -0,0 +1,7 @@
+#tb 0: 1/30
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 328x244
+#sar 0: 0/1
+0,          0,          0,        1,   160064, 0x0829811f
+0,          1,          1,        1,   160064, 0xa6ad92e0
-- 
2.54.0

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

Reply via email to