tests/fate/libavutil: add FATE test for samplefmt

Test all public API functions: name/format round-trip lookups,
bytes_per_sample, is_planar, packed/planar conversions,
alt_sample_fmt, get_sample_fmt_string, samples_get_buffer_size,
samples_alloc, samples_alloc_array_and_samples, samples_copy,
and samples_set_silence. OOM error paths are exercised via
av_max_alloc().

Coverage for libavutil/samplefmt.c: 0.00% -> 95.28%

Remaining uncovered lines are the fill_arrays failure path
and the overlapping memmove branch in samples_copy.
This commit is contained in:
marcos ashton
2026-03-31 20:27:49 +01:00
committed by michaelni
parent 117897bcd0
commit 66b1dbfb98
4 changed files with 327 additions and 0 deletions

View File

@@ -301,6 +301,7 @@ TESTPROGS = adler32 \
ripemd \
sha \
sha512 \
samplefmt \
side_data_array \
softfloat \
spherical \

208
libavutil/tests/samplefmt.c Normal file
View File

@@ -0,0 +1,208 @@
/*
* 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 <errno.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include "libavutil/error.h"
#include "libavutil/mem.h"
#include "libavutil/samplefmt.h"
int main(void)
{
char buf[64];
/* av_get_sample_fmt_name and av_get_sample_fmt round-trip */
printf("Testing name/format round-trip\n");
for (int i = 0; i < AV_SAMPLE_FMT_NB; i++) {
const char *name = av_get_sample_fmt_name(i);
enum AVSampleFormat fmt = av_get_sample_fmt(name);
printf("%2d: name=%-5s roundtrip=%s\n", i, name, fmt == i ? "OK" : "FAIL");
}
/* boundary: NONE and out-of-range */
printf("NONE name: %s\n", av_get_sample_fmt_name(AV_SAMPLE_FMT_NONE) == NULL ? "(null)" : "?");
printf("NB name: %s\n", av_get_sample_fmt_name(AV_SAMPLE_FMT_NB) == NULL ? "(null)" : "?");
printf("unknown: %d\n", av_get_sample_fmt("nonexistent"));
/* av_get_bytes_per_sample */
printf("\nTesting av_get_bytes_per_sample()\n");
for (int i = 0; i < AV_SAMPLE_FMT_NB; i++)
printf("%s: %d\n", av_get_sample_fmt_name(i), av_get_bytes_per_sample(i));
printf("NONE: %d\n", av_get_bytes_per_sample(AV_SAMPLE_FMT_NONE));
/* av_sample_fmt_is_planar */
printf("\nTesting av_sample_fmt_is_planar()\n");
for (int i = 0; i < AV_SAMPLE_FMT_NB; i++)
printf("%s: %d\n", av_get_sample_fmt_name(i), av_sample_fmt_is_planar(i));
printf("NONE: %d\n", av_sample_fmt_is_planar(AV_SAMPLE_FMT_NONE));
/* av_get_packed_sample_fmt and av_get_planar_sample_fmt */
printf("\nTesting packed/planar conversions\n");
for (int i = 0; i < AV_SAMPLE_FMT_NB; i++) {
enum AVSampleFormat packed = av_get_packed_sample_fmt(i);
enum AVSampleFormat planar = av_get_planar_sample_fmt(i);
printf("%s: packed=%-4s planar=%s\n",
av_get_sample_fmt_name(i),
av_get_sample_fmt_name(packed),
av_get_sample_fmt_name(planar));
}
/* av_get_alt_sample_fmt */
printf("\nTesting av_get_alt_sample_fmt()\n");
for (int i = 0; i < AV_SAMPLE_FMT_NB; i++) {
enum AVSampleFormat alt_packed = av_get_alt_sample_fmt(i, 0);
enum AVSampleFormat alt_planar = av_get_alt_sample_fmt(i, 1);
printf("%s: alt_packed=%-4s alt_planar=%s\n",
av_get_sample_fmt_name(i),
av_get_sample_fmt_name(alt_packed),
av_get_sample_fmt_name(alt_planar));
}
/* av_get_sample_fmt_string */
printf("\nTesting av_get_sample_fmt_string()\n");
av_get_sample_fmt_string(buf, sizeof(buf), -1);
printf("header: %s\n", buf);
for (int i = 0; i < AV_SAMPLE_FMT_NB; i++) {
av_get_sample_fmt_string(buf, sizeof(buf), i);
printf("%s\n", buf);
}
/* av_samples_get_buffer_size */
printf("\nTesting av_samples_get_buffer_size()\n");
{
int linesize;
printf("2ch 1024smp s16: %d\n",
av_samples_get_buffer_size(NULL, 2, 1024, AV_SAMPLE_FMT_S16, 1));
printf("2ch 1024smp s16p: %d\n",
av_samples_get_buffer_size(NULL, 2, 1024, AV_SAMPLE_FMT_S16P, 1));
printf("6ch 512smp s32: %d\n",
av_samples_get_buffer_size(NULL, 6, 512, AV_SAMPLE_FMT_S32, 1));
av_samples_get_buffer_size(&linesize, 2, 1024, AV_SAMPLE_FMT_S16, 0);
printf("linesize (2ch 1024smp s16 align=0): %d\n", linesize);
printf("0ch error: %d\n",
av_samples_get_buffer_size(NULL, 0, 1024, AV_SAMPLE_FMT_S16, 1) < 0);
}
/* av_samples_alloc and av_samples_fill_arrays */
printf("\nTesting av_samples_alloc()\n");
{
uint8_t *data[8] = { 0 };
int linesize, ret;
ret = av_samples_alloc(data, &linesize, 2, 1024, AV_SAMPLE_FMT_S16, 0);
printf("alloc 2ch s16: ret=%d linesize=%d data[0]=%s\n",
ret > 0, linesize, data[0] ? "set" : "null");
av_freep(&data[0]);
ret = av_samples_alloc(data, &linesize, 2, 1024, AV_SAMPLE_FMT_S16P, 0);
printf("alloc 2ch s16p: ret=%d linesize=%d data[0]=%s data[1]=%s\n",
ret > 0, linesize,
data[0] ? "set" : "null", data[1] ? "set" : "null");
av_freep(&data[0]);
}
/* av_samples_alloc_array_and_samples */
printf("\nTesting av_samples_alloc_array_and_samples()\n");
{
uint8_t **data = NULL;
int linesize, ret;
ret = av_samples_alloc_array_and_samples(&data, &linesize, 2, 1024,
AV_SAMPLE_FMT_S16P, 0);
printf("alloc_array 2ch s16p: ret=%d linesize=%d data[0]=%s data[1]=%s\n",
ret > 0, linesize,
data[0] ? "set" : "null", data[1] ? "set" : "null");
if (data)
av_freep(&data[0]);
av_freep(&data);
}
/* av_samples_copy */
printf("\nTesting av_samples_copy()\n");
{
uint8_t *src[1] = { 0 }, *dst[1] = { 0 };
int linesize;
av_samples_alloc(src, &linesize, 1, 4, AV_SAMPLE_FMT_S16, 1);
av_samples_alloc(dst, &linesize, 1, 4, AV_SAMPLE_FMT_S16, 1);
if (src[0] && dst[0]) {
memset(src[0], 0xAB, 8);
av_samples_copy(dst, src, 0, 0, 4, 1, AV_SAMPLE_FMT_S16);
printf("copy: %s\n", memcmp(src[0], dst[0], 8) == 0 ? "OK" : "FAIL");
}
av_freep(&src[0]);
av_freep(&dst[0]);
}
/* OOM paths via av_max_alloc */
printf("\nTesting OOM paths\n");
{
uint8_t *data[8] = { 0 };
uint8_t **array_data = NULL;
int linesize, ret;
av_max_alloc(1);
ret = av_samples_alloc(data, &linesize, 2, 1024, AV_SAMPLE_FMT_S16, 0);
printf("alloc OOM: ret=%d data[0]=%s\n",
ret == AVERROR(ENOMEM), data[0] ? "set" : "null");
ret = av_samples_alloc_array_and_samples(&array_data, &linesize, 2, 1024,
AV_SAMPLE_FMT_S16P, 0);
printf("alloc_array OOM: ret=%d data=%s\n",
ret == AVERROR(ENOMEM), array_data ? "set" : "null");
av_max_alloc(INT_MAX);
}
/* av_samples_set_silence */
printf("\nTesting av_samples_set_silence()\n");
{
uint8_t *data[1] = { 0 };
int linesize;
av_samples_alloc(data, &linesize, 1, 4, AV_SAMPLE_FMT_S16, 1);
if (data[0]) {
memset(data[0], 0xFF, 8);
av_samples_set_silence(data, 0, 4, 1, AV_SAMPLE_FMT_S16);
/* silence for s16 is zero */
int silent = 1;
for (int i = 0; i < 8; i++)
if (data[0][i] != 0) silent = 0;
printf("silence s16: %s\n", silent ? "OK" : "FAIL");
}
av_freep(&data[0]);
av_samples_alloc(data, &linesize, 1, 4, AV_SAMPLE_FMT_U8, 1);
if (data[0]) {
memset(data[0], 0xFF, 4);
av_samples_set_silence(data, 0, 4, 1, AV_SAMPLE_FMT_U8);
/* silence for u8 is 0x80 */
int silent = 1;
for (int i = 0; i < 4; i++)
if (data[0][i] != 0x80) silent = 0;
printf("silence u8: %s\n", silent ? "OK" : "FAIL");
}
av_freep(&data[0]);
}
return 0;
}

View File

@@ -160,6 +160,10 @@ FATE_LIBAVUTIL += fate-sha512
fate-sha512: libavutil/tests/sha512$(EXESUF)
fate-sha512: CMD = run libavutil/tests/sha512$(EXESUF)
FATE_LIBAVUTIL += fate-samplefmt
fate-samplefmt: libavutil/tests/samplefmt$(EXESUF)
fate-samplefmt: CMD = run libavutil/tests/samplefmt$(EXESUF)
FATE_LIBAVUTIL += fate-side_data_array
fate-side_data_array: libavutil/tests/side_data_array$(EXESUF)
fate-side_data_array: CMD = run libavutil/tests/side_data_array$(EXESUF)

114
tests/ref/fate/samplefmt Normal file
View File

@@ -0,0 +1,114 @@
Testing name/format round-trip
0: name=u8 roundtrip=OK
1: name=s16 roundtrip=OK
2: name=s32 roundtrip=OK
3: name=flt roundtrip=OK
4: name=dbl roundtrip=OK
5: name=u8p roundtrip=OK
6: name=s16p roundtrip=OK
7: name=s32p roundtrip=OK
8: name=fltp roundtrip=OK
9: name=dblp roundtrip=OK
10: name=s64 roundtrip=OK
11: name=s64p roundtrip=OK
NONE name: (null)
NB name: (null)
unknown: -1
Testing av_get_bytes_per_sample()
u8: 1
s16: 2
s32: 4
flt: 4
dbl: 8
u8p: 1
s16p: 2
s32p: 4
fltp: 4
dblp: 8
s64: 8
s64p: 8
NONE: 0
Testing av_sample_fmt_is_planar()
u8: 0
s16: 0
s32: 0
flt: 0
dbl: 0
u8p: 1
s16p: 1
s32p: 1
fltp: 1
dblp: 1
s64: 0
s64p: 1
NONE: 0
Testing packed/planar conversions
u8: packed=u8 planar=u8p
s16: packed=s16 planar=s16p
s32: packed=s32 planar=s32p
flt: packed=flt planar=fltp
dbl: packed=dbl planar=dblp
u8p: packed=u8 planar=u8p
s16p: packed=s16 planar=s16p
s32p: packed=s32 planar=s32p
fltp: packed=flt planar=fltp
dblp: packed=dbl planar=dblp
s64: packed=s64 planar=s64p
s64p: packed=s64 planar=s64p
Testing av_get_alt_sample_fmt()
u8: alt_packed=u8 alt_planar=u8p
s16: alt_packed=s16 alt_planar=s16p
s32: alt_packed=s32 alt_planar=s32p
flt: alt_packed=flt alt_planar=fltp
dbl: alt_packed=dbl alt_planar=dblp
u8p: alt_packed=u8 alt_planar=u8p
s16p: alt_packed=s16 alt_planar=s16p
s32p: alt_packed=s32 alt_planar=s32p
fltp: alt_packed=flt alt_planar=fltp
dblp: alt_packed=dbl alt_planar=dblp
s64: alt_packed=s64 alt_planar=s64p
s64p: alt_packed=s64 alt_planar=s64p
Testing av_get_sample_fmt_string()
header: name depth
u8 8
s16 16
s32 32
flt 32
dbl 64
u8p 8
s16p 16
s32p 32
fltp 32
dblp 64
s64 64
s64p 64
Testing av_samples_get_buffer_size()
2ch 1024smp s16: 4096
2ch 1024smp s16p: 4096
6ch 512smp s32: 12288
linesize (2ch 1024smp s16 align=0): 4096
0ch error: 1
Testing av_samples_alloc()
alloc 2ch s16: ret=1 linesize=4096 data[0]=set
alloc 2ch s16p: ret=1 linesize=2048 data[0]=set data[1]=set
Testing av_samples_alloc_array_and_samples()
alloc_array 2ch s16p: ret=1 linesize=2048 data[0]=set data[1]=set
Testing av_samples_copy()
copy: OK
Testing OOM paths
alloc OOM: ret=1 data[0]=null
alloc_array OOM: ret=1 data=null
Testing av_samples_set_silence()
silence s16: OK
silence u8: OK