FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libswscale/tests/floatimg_cmp.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 92 151 60.9%
Functions: 1 1 100.0%
Branches: 58 102 56.9%

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 <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <math.h>
23 #include <inttypes.h>
24 #include <float.h>
25
26 #include "libavutil/avutil.h"
27 #include "libavutil/imgutils.h"
28 #include "libavutil/intfloat.h"
29 #include "libavutil/intreadwrite.h"
30 #include "libavutil/lfg.h"
31 #include "libavutil/mem.h"
32 #include "libavutil/parseutils.h"
33 #include "libavutil/pixdesc.h"
34
35 #include "libswscale/swscale.h"
36
37 #define DEFAULT_W 96
38 #define DEFAULT_H 96
39
40 static const enum AVPixelFormat pix_fmts[] = {
41 AV_PIX_FMT_YUV444P16LE,
42 AV_PIX_FMT_YUV444P,
43 AV_PIX_FMT_YUV444P9LE, AV_PIX_FMT_YUV444P10LE,
44 AV_PIX_FMT_YUV444P12LE, AV_PIX_FMT_YUV444P14LE,
45 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
46 AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
47 AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
48 AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
49 AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
50 AV_PIX_FMT_RGB48LE, AV_PIX_FMT_BGR48LE,
51 AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_BGRA64LE,
52 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
53 AV_PIX_FMT_GBRP9LE,
54 AV_PIX_FMT_GBRP10LE, AV_PIX_FMT_GBRAP10LE,
55 AV_PIX_FMT_GBRP12LE, AV_PIX_FMT_GBRAP12LE,
56 AV_PIX_FMT_GBRP14LE,
57 AV_PIX_FMT_GBRP16LE, AV_PIX_FMT_GBRAP16LE
58 };
59
60 const char *usage = "floatimg_cmp -pixel_format <pix_fmt> -size <image_size> -ref <testfile>\n";
61
62 1 int main(int argc, char **argv)
63 {
64 1 enum AVPixelFormat inFormat = AV_PIX_FMT_NONE;
65 1 enum AVPixelFormat dstFormat = AV_PIX_FMT_NONE;
66 const AVPixFmtDescriptor *desc;
67 uint8_t *ptr;
68 uint32_t *in, *out;
69
70 1 uint8_t *rgbIn[4] = {NULL, NULL, NULL, NULL};
71 1 uint8_t *rgbOut[4] = {NULL, NULL, NULL, NULL};
72 int rgbStride[4];
73
74 1 uint8_t *dst[4] = {NULL, NULL, NULL, NULL};
75 int dstStride[4];
76
77 int i, x, y, p, size, count;
78 1 int res = -1;
79 1 int w = -1;
80 1 int h = -1;
81 union av_intfloat32 v0, v1;
82
83 double sum;
84 float minimum, maximum, diff;
85
86 1 SwsContext *sws = NULL;
87 AVLFG rand;
88 1 FILE *fp = NULL;
89
90
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 for (i = 1; i < argc; i += 2) {
91 if (argv[i][0] != '-' || i + 1 == argc)
92 goto bad_option;
93 if (!strcmp(argv[i], "-ref")) {
94 fp = fopen(argv[i + 1], "rb");
95 if (!fp) {
96 fprintf(stderr, "could not open '%s'\n", argv[i + 1]);
97 goto end;
98 }
99 } else if (!strcmp(argv[i], "-size")) {
100 res = av_parse_video_size(&w, &h, argv[i + 1]);
101 if (res < 0) {
102 fprintf(stderr, "invalid video size %s\n", argv[i + 1]);
103 goto end;
104 }
105 } else if (!strcmp(argv[i], "-pixel_format")) {
106 inFormat = av_get_pix_fmt(argv[i + 1]);
107 if (inFormat == AV_PIX_FMT_NONE) {
108 fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
109 goto end;
110 }
111 } else {
112 bad_option:
113 fprintf(stderr, "%s", usage);
114 fprintf(stderr, "bad option or argument missing (%s)\n", argv[i]);
115 goto end;
116 };
117 }
118
119
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!fp) {
120 1 inFormat = AV_PIX_FMT_GBRPF32LE;
121 1 w = DEFAULT_W;
122 1 h = DEFAULT_H;
123 }
124
125
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (w <= 0 || h <= 0) {
126 fprintf(stderr, "%s", usage);
127 fprintf(stderr, "invalid -video_size\n");
128 goto end;
129 }
130
131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (inFormat == AV_PIX_FMT_NONE) {
132 fprintf(stderr, "%s", usage);
133 fprintf(stderr, "invalid input pixel format\n");
134 goto end;
135 }
136
137 1 desc = av_pix_fmt_desc_get(inFormat);
138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!(desc->flags & AV_PIX_FMT_FLAG_FLOAT)) {
139 fprintf(stderr, "input pixel format not floating point.\n");
140 goto end;
141 }
142
143 1 res = av_image_fill_linesizes(rgbStride, inFormat, w);
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (res < 0) {
145 fprintf(stderr, "av_image_fill_linesizes failed\n");
146 goto end;
147 }
148
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 for (p = 0; p < 4; p++) {
149 4 rgbStride[p] = FFALIGN(rgbStride[p], 16);
150
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 if (rgbStride[p]) {
151 3 rgbIn[p] = av_mallocz(rgbStride[p] * h + 16);
152 3 rgbOut[p] = av_mallocz(rgbStride[p] * h + 16);
153 }
154
4/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
4 if (rgbStride[p] && (!rgbIn[p] || !rgbOut[p])) {
155 goto end;
156 }
157 }
158
159
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 1 times.
31 for (i = 0; i < FF_ARRAY_ELEMS(pix_fmts); i++) {
160 30 dstFormat = pix_fmts[i];
161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (fp) {
162 fseek(fp, 0, SEEK_SET);
163 for (p = 0; p < 4; p++) {
164 if (!rgbStride[p])
165 continue;
166
167 ptr = rgbIn[p];
168 for (y = 0; y < h; y++) {
169 size = fread(ptr, 1, w*4, fp);
170 if (size != w*4) {
171 fprintf(stderr, "read error: %d\n", size);
172 goto end;
173 }
174 ptr += rgbStride[p];
175 }
176 }
177 } else {
178 // fill src with random values between 0.0 - 1.0
179 30 av_lfg_init(&rand, 1);
180
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 30 times.
150 for (p = 0; p < 4; p++) {
181
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 90 times.
120 if (!rgbStride[p])
182 30 continue;
183
184
2/2
✓ Branch 0 taken 8640 times.
✓ Branch 1 taken 90 times.
8730 for (y = 0; y < h; y++) {
185 8640 in = (uint32_t*)(rgbIn[p] + y * rgbStride[p]);
186
2/2
✓ Branch 0 taken 829440 times.
✓ Branch 1 taken 8640 times.
838080 for (x = 0; x < w; x++) {
187 829440 v0.f = (float)av_lfg_get(&rand)/(float)(UINT32_MAX);
188 829440 *in++ = AV_RL32(&v0.i);
189 }
190 }
191 }
192 }
193
194 // setup intermediate image
195
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 30 times.
150 for (p = 0; p < 4; p++) {
196 120 av_freep(&dst[p]);
197 }
198
199 30 res = av_image_fill_linesizes(dstStride, dstFormat, w);
200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (res < 0) {
201 fprintf(stderr, "av_image_fill_linesizes failed\n");
202 goto end;
203 }
204
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 30 times.
150 for (p = 0; p < 4; p++) {
205 120 dstStride[p] = FFALIGN(dstStride[p], 16);
206
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 54 times.
120 if (dstStride[p]) {
207 66 dst[p] = av_mallocz(dstStride[p] * h + 16);
208 }
209
3/4
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 66 times.
120 if (dstStride[p] && !dst[p]) {
210 goto end;
211 }
212 }
213
214 // srcFormat -> dstFormat
215 30 sws = sws_getContext(w, h, inFormat, w, h,
216 dstFormat, SWS_BILINEAR, NULL, NULL, NULL);
217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (!sws) {
218 fprintf(stderr, "Failed to get %s -> %s\n", av_get_pix_fmt_name(inFormat), av_get_pix_fmt_name(dstFormat) );
219 goto end;
220 }
221
222 30 res = sws_scale(sws, (const uint8_t *const *)rgbIn, rgbStride, 0, h, dst, dstStride);
223
2/4
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 30 times.
30 if (res < 0 || res != h) {
224 fprintf(stderr, "sws_scale failed\n");
225 res = -1;
226 goto end;
227 }
228 30 sws_freeContext(sws);
229
230 // dstFormat -> srcFormat
231 30 sws = sws_getContext(w, h, dstFormat, w, h,
232 inFormat, SWS_BILINEAR, NULL, NULL, NULL);
233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if(!sws) {
234 fprintf(stderr, "Failed to get %s -> %s\n", av_get_pix_fmt_name(dstFormat), av_get_pix_fmt_name(inFormat) );
235 goto end;
236 }
237
238 30 res = sws_scale(sws, (const uint8_t *const *)dst, dstStride, 0, h, rgbOut, rgbStride);
239
2/4
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 30 times.
30 if (res < 0 || res != h) {
240 fprintf(stderr, "sws_scale failed\n");
241 res = -1;
242 goto end;
243 }
244 30 sws_freeContext(sws);
245 30 sws = NULL;
246
247 30 minimum = FLT_MAX;
248 30 maximum = -FLT_MAX;
249 30 count = 0;
250 30 sum = 0.0;
251
252
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 30 times.
150 for (p = 0; p < 4; p++) {
253
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 90 times.
120 if (!rgbStride[p])
254 30 continue;
255
256
2/2
✓ Branch 0 taken 8640 times.
✓ Branch 1 taken 90 times.
8730 for (y = 0; y < h; y++) {
257 8640 in = (uint32_t*)(rgbIn[p] + y * rgbStride[p]);
258 8640 out = (uint32_t*)(rgbOut[p] + y * rgbStride[p]);
259
2/2
✓ Branch 0 taken 829440 times.
✓ Branch 1 taken 8640 times.
838080 for (x = 0; x < w; x++) {
260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 829440 times.
829440 if (desc->flags & AV_PIX_FMT_FLAG_BE) {
261 v0.i = AV_RB32(in);
262 v1.i = AV_RB32(out);
263 } else {
264 829440 v0.i = AV_RL32(in);
265 829440 v1.i = AV_RL32(out);
266 }
267
268 829440 diff = fabsf(v0.f - v1.f);
269 829440 sum += diff;
270
2/2
✓ Branch 0 taken 320 times.
✓ Branch 1 taken 829120 times.
829440 minimum = FFMIN(minimum, diff);
271
2/2
✓ Branch 0 taken 828981 times.
✓ Branch 1 taken 459 times.
829440 maximum = FFMAX(maximum, diff);
272
273 829440 count++;
274 829440 in++;
275 829440 out++;
276 }
277 }
278 }
279
280 30 fprintf(stdout, "%s -> %s -> %s\n", av_get_pix_fmt_name(inFormat), av_get_pix_fmt_name(dstFormat), av_get_pix_fmt_name(inFormat) );
281 30 fprintf(stdout, "avg diff: %f\nmin diff: %f\nmax diff: %f\n", sum / count, minimum, maximum);
282 30 res = 0;
283 }
284
285 1 end:
286 1 sws_freeContext(sws);
287
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 for (p = 0; p < 4; p++) {
288 4 av_freep(&rgbIn[p]);
289 4 av_freep(&rgbOut[p]);
290 4 av_freep(&dst[p]);
291 }
292
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (fp)
293 fclose(fp);
294
295 1 return res;
296 }
297