Commit 912b5d9502 for ffmpeg

commit 912b5d9502e958a190229af99915da93bc88b167
Author: marcos ashton <marcosashiglesias@gmail.com>
Date:   Tue Dec 1 15:27:59 2026 +0000

    tests/fate/libavutil: add FATE test for frame

    It replaces planar audio frames with more and fewer than 8 channels into one destination and applies crop fields that are out of range, and fails without either of the two avutil/frame fixes before it.

diff --git a/.forgejo/CODEOWNERS b/.forgejo/CODEOWNERS
index b07b4617be..0d5ddaae34 100644
--- a/.forgejo/CODEOWNERS
+++ b/.forgejo/CODEOWNERS
@@ -246,6 +246,7 @@ libavutil/tests/buffer.* @MarcosAsh
 libavutil/tests/csp.* @MarcosAsh
 libavutil/tests/dovi_meta.* @MarcosAsh
 libavutil/tests/downmix_info.* @MarcosAsh
+libavutil/tests/frame.* @MarcosAsh
 libavutil/tests/hdr_dynamic_metadata.* @MarcosAsh
 libavutil/tests/hdr_dynamic_vivid_metadata.* @MarcosAsh
 libavutil/tests/mastering_display_metadata.* @MarcosAsh
@@ -260,6 +261,7 @@ tests/ref/fate/buffer @MarcosAsh
 tests/ref/fate/csp @MarcosAsh
 tests/ref/fate/dovi_meta @MarcosAsh
 tests/ref/fate/downmix_info @MarcosAsh
+tests/ref/fate/frame @MarcosAsh
 tests/ref/fate/hdr_dynamic_metadata @MarcosAsh
 tests/ref/fate/hdr_dynamic_vivid_metadata @MarcosAsh
 tests/ref/fate/mastering_display_metadata @MarcosAsh
diff --git a/libavutil/Makefile b/libavutil/Makefile
index 1f91fd5d17..2277e2f7b4 100644
--- a/libavutil/Makefile
+++ b/libavutil/Makefile
@@ -288,6 +288,7 @@ TESTPROGS = adler32                                                     \
             file                                                        \
             fifo                                                        \
             film_grain_params                                           \
+            frame                                                       \
             hash                                                        \
             hdr_dynamic_metadata                                        \
             hdr_dynamic_vivid_metadata                                  \
diff --git a/libavutil/tests/.gitignore b/libavutil/tests/.gitignore
index ac30059249..1e5fb79b3e 100644
--- a/libavutil/tests/.gitignore
+++ b/libavutil/tests/.gitignore
@@ -29,6 +29,7 @@
 /fifo
 /file
 /film_grain_params
+/frame
 /hash
 /hdr_dynamic_metadata
 /hdr_dynamic_vivid_metadata
diff --git a/libavutil/tests/frame.c b/libavutil/tests/frame.c
new file mode 100644
index 0000000000..2a2cf91529
--- /dev/null
+++ b/libavutil/tests/frame.c
@@ -0,0 +1,190 @@
+/*
+ * 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
+ */
+
+#include <inttypes.h>
+#include <limits.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "libavutil/adler32.h"
+#include "libavutil/avassert.h"
+#include "libavutil/channel_layout.h"
+#include "libavutil/error.h"
+#include "libavutil/frame.h"
+#include "libavutil/macros.h"
+#include "libavutil/pixdesc.h"
+#include "libavutil/samplefmt.h"
+
+/* strerror() text differs between C libraries */
+static const char *err(int ret)
+{
+    return ret >= 0                ? "0"      :
+           ret == AVERROR(EINVAL)  ? "EINVAL" :
+           ret == AVERROR(ERANGE)  ? "ERANGE" :
+           ret == AVERROR(ENOMEM)  ? "ENOMEM" : "unexpected error";
+}
+
+static AVFrame *video(enum AVPixelFormat fmt, int w, int h)
+{
+    AVFrame *f = av_frame_alloc();
+    av_assert0(f);
+    f->format = fmt;
+    f->width  = w;
+    f->height = h;
+    av_assert0(av_frame_get_buffer(f, 0) >= 0);
+    return f;
+}
+
+static AVFrame *audio(enum AVSampleFormat fmt, int channels, int nb_samples)
+{
+    AVFrame *f = av_frame_alloc();
+    int planes = av_sample_fmt_is_planar(fmt) ? channels : 1;
+    int size   = nb_samples * av_get_bytes_per_sample(fmt) * (channels / planes);
+
+    av_assert0(f);
+    f->format     = fmt;
+    f->nb_samples = nb_samples;
+    av_channel_layout_default(&f->ch_layout, channels);
+    av_assert0(av_frame_get_buffer(f, 0) >= 0);
+    for (int p = 0; p < planes; p++)
+        for (int i = 0; i < size; i++)
+            f->extended_data[p][i] = i * 5 + p * 13;
+    return f;
+}
+
+static uint32_t audio_sum(const AVFrame *f)
+{
+    int planar = av_sample_fmt_is_planar(f->format);
+    int planes = planar ? f->ch_layout.nb_channels : 1;
+    int size   = f->nb_samples * av_get_bytes_per_sample(f->format) *
+                 (planar ? 1 : f->ch_layout.nb_channels);
+    uint32_t sum = 1;
+
+    for (int p = 0; p < planes; p++)
+        sum = av_adler32_update(sum, f->extended_data[p], size);
+    return sum;
+}
+
+static void print_audio(const char *name, const AVFrame *f)
+{
+    printf("%s: %d ch, %d samples, nb_extended_buf %d, extended_data %s, sum %08"PRIx32"\n",
+           name, f->ch_layout.nb_channels, f->nb_samples, f->nb_extended_buf,
+           f->extended_data == f->data ? "data" : "own", audio_sum(f));
+}
+
+static void test_replace(void)
+{
+    AVFrame *stereo = audio(AV_SAMPLE_FMT_FLTP, 2, 64);
+    AVFrame *ten    = audio(AV_SAMPLE_FMT_FLTP, 10, 64);
+    AVFrame *twelve = audio(AV_SAMPLE_FMT_S16P, 12, 32);
+    AVFrame *packed = audio(AV_SAMPLE_FMT_S16, 10, 64);
+    AVFrame *dst    = av_frame_alloc();
+
+    av_assert0(dst);
+    print_audio("stereo", stereo);
+    print_audio("ten", ten);
+    print_audio("twelve", twelve);
+    print_audio("packed", packed);
+
+    /* grow and shrink extended_buf on one destination */
+    printf("replace twelve: %s\n", err(av_frame_replace(dst, twelve)));
+    print_audio("dst", dst);
+    printf("replace ten: %s\n", err(av_frame_replace(dst, ten)));
+    print_audio("dst", dst);
+    printf("replace stereo: %s\n", err(av_frame_replace(dst, stereo)));
+    print_audio("dst", dst);
+    printf("replace twelve: %s\n", err(av_frame_replace(dst, twelve)));
+    print_audio("dst", dst);
+    printf("replace packed: %s\n", err(av_frame_replace(dst, packed)));
+    print_audio("dst", dst);
+    av_frame_unref(dst);
+    printf("replace ten: %s\n", err(av_frame_replace(dst, ten)));
+    print_audio("dst", dst);
+
+    av_frame_free(&dst);
+    av_frame_free(&packed);
+    av_frame_free(&twelve);
+    av_frame_free(&ten);
+    av_frame_free(&stereo);
+}
+
+static void test_cropping(void)
+{
+    static const struct {
+        const char *name;
+        enum AVPixelFormat fmt;
+        size_t left, right, top, bottom;
+        int flags;
+    } tests[] = {
+        { "2/4/2/6",       AV_PIX_FMT_YUV420P,   2, 4, 2, 6, 0 },
+        { "2/4/2/6",       AV_PIX_FMT_YUV420P,   2, 4, 2, 6, AV_FRAME_CROP_UNALIGNED },
+        { "40/0/0/0",      AV_PIX_FMT_YUV420P,  40, 0, 0, 0, 0 },
+        { "3/1/1/1",       AV_PIX_FMT_RGB24,     3, 1, 1, 1, AV_FRAME_CROP_UNALIGNED },
+        { "5/0/3/0",       AV_PIX_FMT_PAL8,      5, 0, 3, 0, AV_FRAME_CROP_UNALIGNED },
+        { "0/8/0/4",       AV_PIX_FMT_MONOWHITE, 0, 8, 0, 4, 0 },
+        { "32/32/0/0",     AV_PIX_FMT_YUV420P,  32, 32, 0, 0, 0 },
+        { "0/0/24/24",     AV_PIX_FMT_YUV420P,   0, 0, 24, 24, 0 },
+        { "INT_MAX/1/0/0", AV_PIX_FMT_YUV420P, INT_MAX, 1, 0, 0, 0 },
+        { "1/SIZE_MAX/0/0", AV_PIX_FMT_YUV420P,  1, SIZE_MAX, 0, 0, AV_FRAME_CROP_UNALIGNED },
+        { "0/0/1/SIZE_MAX", AV_PIX_FMT_YUV420P,  0, 0, 1, SIZE_MAX, AV_FRAME_CROP_UNALIGNED },
+    };
+
+    for (int i = 0; i < FF_ARRAY_ELEMS(tests); i++) {
+        AVFrame *f = video(tests[i].fmt, 64, 48);
+        uint8_t *data[4];
+        int ret;
+
+        memcpy(data, f->data, sizeof(data));
+        f->crop_left   = tests[i].left;
+        f->crop_right  = tests[i].right;
+        f->crop_top    = tests[i].top;
+        f->crop_bottom = tests[i].bottom;
+        ret = av_frame_apply_cropping(f, tests[i].flags);
+        printf("crop %s %s%s: %s, %dx%d, crop fields %s, offsets",
+               av_get_pix_fmt_name(tests[i].fmt), tests[i].name,
+               tests[i].flags ? " unaligned" : "", err(ret), f->width, f->height,
+               !(f->crop_left | f->crop_right | f->crop_top | f->crop_bottom) ? "cleared" :
+               f->crop_left == tests[i].left && f->crop_right  == tests[i].right &&
+               f->crop_top  == tests[i].top  && f->crop_bottom == tests[i].bottom ? "kept" : "changed");
+        for (int p = 0; p < 4 && data[p]; p++) {
+            ptrdiff_t off = f->data[p] - data[p];
+            /* linesize depends on the build, so print rows and bytes */
+            if (f->linesize[p])
+                printf(" %td+%td", off / f->linesize[p], off % f->linesize[p]);
+            else
+                printf(" %td", off);
+        }
+        printf("\n");
+        av_frame_free(&f);
+    }
+
+    {
+        AVFrame *f = av_frame_alloc();
+        av_assert0(f);
+        printf("crop empty frame: %s\n", err(av_frame_apply_cropping(f, 0)));
+        av_frame_free(&f);
+    }
+}
+
+int main(void)
+{
+    test_replace();
+    test_cropping();
+    return 0;
+}
diff --git a/tests/fate/libavutil.mak b/tests/fate/libavutil.mak
index fa55ebdbe3..8d7f364b36 100644
--- a/tests/fate/libavutil.mak
+++ b/tests/fate/libavutil.mak
@@ -123,6 +123,10 @@ FATE_LIBAVUTIL += fate-film_grain_params
 fate-film_grain_params: libavutil/tests/film_grain_params$(EXESUF)
 fate-film_grain_params: CMD = run libavutil/tests/film_grain_params$(EXESUF)

+FATE_LIBAVUTIL += fate-frame
+fate-frame: libavutil/tests/frame$(EXESUF)
+fate-frame: CMD = run libavutil/tests/frame$(EXESUF)
+
 FATE_LIBAVUTIL += fate-hash
 fate-hash: libavutil/tests/hash$(EXESUF)
 fate-hash: CMD = run libavutil/tests/hash$(EXESUF)
diff --git a/tests/ref/fate/frame b/tests/ref/fate/frame
new file mode 100644
index 0000000000..57a8f44cf8
--- /dev/null
+++ b/tests/ref/fate/frame
@@ -0,0 +1,28 @@
+stereo: 2 ch, 64 samples, nb_extended_buf 0, extended_data data, sum 1508ff01
+ten: 10 ch, 64 samples, nb_extended_buf 2, extended_data own, sum e9fafb3d
+twelve: 12 ch, 32 samples, nb_extended_buf 4, extended_data own, sum 9bbf6f10
+packed: 10 ch, 64 samples, nb_extended_buf 0, extended_data data, sum 09c97d9f
+replace twelve: 0
+dst: 12 ch, 32 samples, nb_extended_buf 4, extended_data own, sum 9bbf6f10
+replace ten: 0
+dst: 10 ch, 64 samples, nb_extended_buf 2, extended_data own, sum e9fafb3d
+replace stereo: 0
+dst: 2 ch, 64 samples, nb_extended_buf 0, extended_data data, sum 1508ff01
+replace twelve: 0
+dst: 12 ch, 32 samples, nb_extended_buf 4, extended_data own, sum 9bbf6f10
+replace packed: 0
+dst: 10 ch, 64 samples, nb_extended_buf 0, extended_data data, sum 09c97d9f
+replace ten: 0
+dst: 10 ch, 64 samples, nb_extended_buf 2, extended_data own, sum e9fafb3d
+crop yuv420p 2/4/2/6: 0, 60x40, crop fields cleared, offsets 2+0 1+0 1+0
+crop yuv420p 2/4/2/6 unaligned: 0, 58x40, crop fields cleared, offsets 2+2 1+1 1+1
+crop yuv420p 40/0/0/0: 0, 64x48, crop fields cleared, offsets 0+0 0+0 0+0
+crop rgb24 3/1/1/1 unaligned: 0, 60x46, crop fields cleared, offsets 1+9
+crop pal8 5/0/3/0 unaligned: 0, 59x45, crop fields cleared, offsets 3+5 0
+crop monow 0/8/0/4: 0, 56x44, crop fields cleared, offsets 0+0
+crop yuv420p 32/32/0/0: ERANGE, 64x48, crop fields kept, offsets 0+0 0+0 0+0
+crop yuv420p 0/0/24/24: ERANGE, 64x48, crop fields kept, offsets 0+0 0+0 0+0
+crop yuv420p INT_MAX/1/0/0: ERANGE, 64x48, crop fields kept, offsets 0+0 0+0 0+0
+crop yuv420p 1/SIZE_MAX/0/0 unaligned: ERANGE, 64x48, crop fields kept, offsets 0+0 0+0 0+0
+crop yuv420p 0/0/1/SIZE_MAX unaligned: ERANGE, 64x48, crop fields kept, offsets 0+0 0+0 0+0
+crop empty frame: EINVAL