FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/tests/samplefmt.c
Date: 2026-08-15 14:54:27
Exec Total Coverage
Lines: 101 101 100.0%
Functions: 1 1 100.0%
Branches: 36 56 64.3%

Line Branch Exec Source
1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <errno.h>
20 #include <limits.h>
21 #include <stdio.h>
22 #include <string.h>
23
24 #include "libavutil/error.h"
25 #include "libavutil/mem.h"
26 #include "libavutil/samplefmt.h"
27
28 1 int main(void)
29 {
30 char buf[64];
31
32 /* av_get_sample_fmt_name and av_get_sample_fmt round-trip */
33 1 printf("Testing name/format round-trip\n");
34
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1 times.
13 for (int i = 0; i < AV_SAMPLE_FMT_NB; i++) {
35 12 const char *name = av_get_sample_fmt_name(i);
36 12 enum AVSampleFormat fmt = av_get_sample_fmt(name);
37
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 printf("%2d: name=%-5s roundtrip=%s\n", i, name, fmt == i ? "OK" : "FAIL");
38 }
39
40 /* boundary: NONE and out-of-range */
41
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 printf("NONE name: %s\n", av_get_sample_fmt_name(AV_SAMPLE_FMT_NONE) == NULL ? "(null)" : "?");
42
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 printf("NB name: %s\n", av_get_sample_fmt_name(AV_SAMPLE_FMT_NB) == NULL ? "(null)" : "?");
43 1 printf("unknown: %d\n", av_get_sample_fmt("nonexistent"));
44
45 /* av_get_bytes_per_sample */
46 1 printf("\nTesting av_get_bytes_per_sample()\n");
47
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1 times.
13 for (int i = 0; i < AV_SAMPLE_FMT_NB; i++)
48 12 printf("%s: %d\n", av_get_sample_fmt_name(i), av_get_bytes_per_sample(i));
49 1 printf("NONE: %d\n", av_get_bytes_per_sample(AV_SAMPLE_FMT_NONE));
50
51 /* av_sample_fmt_is_planar */
52 1 printf("\nTesting av_sample_fmt_is_planar()\n");
53
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1 times.
13 for (int i = 0; i < AV_SAMPLE_FMT_NB; i++)
54 12 printf("%s: %d\n", av_get_sample_fmt_name(i), av_sample_fmt_is_planar(i));
55 1 printf("NONE: %d\n", av_sample_fmt_is_planar(AV_SAMPLE_FMT_NONE));
56
57 /* av_get_packed_sample_fmt and av_get_planar_sample_fmt */
58 1 printf("\nTesting packed/planar conversions\n");
59
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1 times.
13 for (int i = 0; i < AV_SAMPLE_FMT_NB; i++) {
60 12 enum AVSampleFormat packed = av_get_packed_sample_fmt(i);
61 12 enum AVSampleFormat planar = av_get_planar_sample_fmt(i);
62 12 printf("%s: packed=%-4s planar=%s\n",
63 av_get_sample_fmt_name(i),
64 av_get_sample_fmt_name(packed),
65 av_get_sample_fmt_name(planar));
66 }
67
68 /* av_get_alt_sample_fmt */
69 1 printf("\nTesting av_get_alt_sample_fmt()\n");
70
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1 times.
13 for (int i = 0; i < AV_SAMPLE_FMT_NB; i++) {
71 12 enum AVSampleFormat alt_packed = av_get_alt_sample_fmt(i, 0);
72 12 enum AVSampleFormat alt_planar = av_get_alt_sample_fmt(i, 1);
73 12 printf("%s: alt_packed=%-4s alt_planar=%s\n",
74 av_get_sample_fmt_name(i),
75 av_get_sample_fmt_name(alt_packed),
76 av_get_sample_fmt_name(alt_planar));
77 }
78
79 /* av_get_sample_fmt_string */
80 1 printf("\nTesting av_get_sample_fmt_string()\n");
81 1 av_get_sample_fmt_string(buf, sizeof(buf), -1);
82 1 printf("header: %s\n", buf);
83
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1 times.
13 for (int i = 0; i < AV_SAMPLE_FMT_NB; i++) {
84 12 av_get_sample_fmt_string(buf, sizeof(buf), i);
85 12 printf("%s\n", buf);
86 }
87
88 /* av_samples_get_buffer_size */
89 1 printf("\nTesting av_samples_get_buffer_size()\n");
90 {
91 int linesize;
92 1 printf("2ch 1024smp s16: %d\n",
93 av_samples_get_buffer_size(NULL, 2, 1024, AV_SAMPLE_FMT_S16, 1));
94 1 printf("2ch 1024smp s16p: %d\n",
95 av_samples_get_buffer_size(NULL, 2, 1024, AV_SAMPLE_FMT_S16P, 1));
96 1 printf("6ch 512smp s32: %d\n",
97 av_samples_get_buffer_size(NULL, 6, 512, AV_SAMPLE_FMT_S32, 1));
98 1 av_samples_get_buffer_size(&linesize, 2, 1024, AV_SAMPLE_FMT_S16, 0);
99 1 printf("linesize (2ch 1024smp s16 align=0): %d\n", linesize);
100 1 printf("0ch error: %d\n",
101 1 av_samples_get_buffer_size(NULL, 0, 1024, AV_SAMPLE_FMT_S16, 1) < 0);
102 }
103
104 /* av_samples_alloc and av_samples_fill_arrays */
105 1 printf("\nTesting av_samples_alloc()\n");
106 {
107 1 uint8_t *data[8] = { 0 };
108 int linesize, ret;
109
110 1 ret = av_samples_alloc(data, &linesize, 2, 1024, AV_SAMPLE_FMT_S16, 0);
111 1 printf("alloc 2ch s16: ret=%d linesize=%d data[0]=%s\n",
112
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 ret > 0, linesize, data[0] ? "set" : "null");
113 1 av_freep(&data[0]);
114
115 1 ret = av_samples_alloc(data, &linesize, 2, 1024, AV_SAMPLE_FMT_S16P, 0);
116 2 printf("alloc 2ch s16p: ret=%d linesize=%d data[0]=%s data[1]=%s\n",
117 ret > 0, linesize,
118
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 data[0] ? "set" : "null", data[1] ? "set" : "null");
119 1 av_freep(&data[0]);
120 }
121
122 /* av_samples_alloc_array_and_samples */
123 1 printf("\nTesting av_samples_alloc_array_and_samples()\n");
124 {
125 1 uint8_t **data = NULL;
126 int linesize, ret;
127
128 1 ret = av_samples_alloc_array_and_samples(&data, &linesize, 2, 1024,
129 AV_SAMPLE_FMT_S16P, 0);
130 2 printf("alloc_array 2ch s16p: ret=%d linesize=%d data[0]=%s data[1]=%s\n",
131 ret > 0, linesize,
132
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 data[0] ? "set" : "null", data[1] ? "set" : "null");
133
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (data)
134 1 av_freep(&data[0]);
135 1 av_freep(&data);
136 }
137
138 /* av_samples_copy */
139 1 printf("\nTesting av_samples_copy()\n");
140 {
141 1 uint8_t *src[1] = { 0 }, *dst[1] = { 0 };
142 int linesize;
143
144 1 av_samples_alloc(src, &linesize, 1, 4, AV_SAMPLE_FMT_S16, 1);
145 1 av_samples_alloc(dst, &linesize, 1, 4, AV_SAMPLE_FMT_S16, 1);
146
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (src[0] && dst[0]) {
147 1 memset(src[0], 0xAB, 8);
148 1 av_samples_copy(dst, src, 0, 0, 4, 1, AV_SAMPLE_FMT_S16);
149
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 printf("copy: %s\n", memcmp(src[0], dst[0], 8) == 0 ? "OK" : "FAIL");
150 }
151 1 av_freep(&src[0]);
152 1 av_freep(&dst[0]);
153 }
154
155 /* OOM paths via av_max_alloc */
156 1 printf("\nTesting OOM paths\n");
157 {
158 1 uint8_t *data[8] = { 0 };
159 1 uint8_t **array_data = NULL;
160 int linesize, ret;
161
162 1 av_max_alloc(1);
163
164 1 ret = av_samples_alloc(data, &linesize, 2, 1024, AV_SAMPLE_FMT_S16, 0);
165 1 printf("alloc OOM: ret=%d data[0]=%s\n",
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 ret == AVERROR(ENOMEM), data[0] ? "set" : "null");
167
168 1 ret = av_samples_alloc_array_and_samples(&array_data, &linesize, 2, 1024,
169 AV_SAMPLE_FMT_S16P, 0);
170 1 printf("alloc_array OOM: ret=%d data=%s\n",
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 ret == AVERROR(ENOMEM), array_data ? "set" : "null");
172
173 1 av_max_alloc(INT_MAX);
174 }
175
176 /* av_samples_set_silence */
177 1 printf("\nTesting av_samples_set_silence()\n");
178 {
179 1 uint8_t *data[1] = { 0 };
180 int linesize;
181
182 1 av_samples_alloc(data, &linesize, 1, 4, AV_SAMPLE_FMT_S16, 1);
183
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (data[0]) {
184 1 memset(data[0], 0xFF, 8);
185 1 av_samples_set_silence(data, 0, 4, 1, AV_SAMPLE_FMT_S16);
186 /* silence for s16 is zero */
187 1 int silent = 1;
188
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
9 for (int i = 0; i < 8; i++)
189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (data[0][i] != 0) silent = 0;
190
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 printf("silence s16: %s\n", silent ? "OK" : "FAIL");
191 }
192 1 av_freep(&data[0]);
193
194 1 av_samples_alloc(data, &linesize, 1, 4, AV_SAMPLE_FMT_U8, 1);
195
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (data[0]) {
196 1 memset(data[0], 0xFF, 4);
197 1 av_samples_set_silence(data, 0, 4, 1, AV_SAMPLE_FMT_U8);
198 /* silence for u8 is 0x80 */
199 1 int silent = 1;
200
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 for (int i = 0; i < 4; i++)
201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (data[0][i] != 0x80) silent = 0;
202
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 printf("silence u8: %s\n", silent ? "OK" : "FAIL");
203 }
204 1 av_freep(&data[0]);
205 }
206
207 1 return 0;
208 }
209