FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_ssim.c
Date: 2022-12-05 03:11:11
Exec Total Coverage
Lines: 194 290 66.9%
Functions: 13 18 72.2%
Branches: 73 120 60.8%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2003-2013 Loren Merritt
3 * Copyright (c) 2015 Paul B Mahol
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /* Computes the Structural Similarity Metric between two video streams.
23 * original algorithm:
24 * Z. Wang, A. C. Bovik, H. R. Sheikh and E. P. Simoncelli,
25 * "Image quality assessment: From error visibility to structural similarity,"
26 * IEEE Transactions on Image Processing, vol. 13, no. 4, pp. 600-612, Apr. 2004.
27 *
28 * To improve speed, this implementation uses the standard approximation of
29 * overlapped 8x8 block sums, rather than the original gaussian weights.
30 */
31
32 /*
33 * @file
34 * Calculate the SSIM between two input videos.
35 */
36
37 #include "libavutil/avstring.h"
38 #include "libavutil/file_open.h"
39 #include "libavutil/opt.h"
40 #include "libavutil/pixdesc.h"
41 #include "avfilter.h"
42 #include "drawutils.h"
43 #include "framesync.h"
44 #include "internal.h"
45 #include "ssim.h"
46
47 typedef struct SSIMContext {
48 const AVClass *class;
49 FFFrameSync fs;
50 FILE *stats_file;
51 char *stats_file_str;
52 int nb_components;
53 int nb_threads;
54 int max;
55 uint64_t nb_frames;
56 double ssim[4], ssim_total;
57 char comps[4];
58 double coefs[4];
59 uint8_t rgba_map[4];
60 int planewidth[4];
61 int planeheight[4];
62 int **temp;
63 int is_rgb;
64 double **score;
65 int (*ssim_plane)(AVFilterContext *ctx, void *arg,
66 int jobnr, int nb_jobs);
67 SSIMDSPContext dsp;
68 } SSIMContext;
69
70 #define OFFSET(x) offsetof(SSIMContext, x)
71 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
72
73 static const AVOption ssim_options[] = {
74 {"stats_file", "Set file where to store per-frame difference information", OFFSET(stats_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
75 {"f", "Set file where to store per-frame difference information", OFFSET(stats_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
76 { NULL }
77 };
78
79
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
8 FRAMESYNC_DEFINE_CLASS(ssim, SSIMContext, fs);
80
81 50 static void set_meta(AVDictionary **metadata, const char *key, char comp, float d)
82 {
83 char value[128];
84 50 snprintf(value, sizeof(value), "%f", d);
85
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 20 times.
50 if (comp) {
86 char key2[128];
87 30 snprintf(key2, sizeof(key2), "%s%c", key, comp);
88 30 av_dict_set(metadata, key2, value, 0);
89 } else {
90 20 av_dict_set(metadata, key, value, 0);
91 }
92 50 }
93
94 static void ssim_4x4xn_16bit(const uint8_t *main8, ptrdiff_t main_stride,
95 const uint8_t *ref8, ptrdiff_t ref_stride,
96 int64_t (*sums)[4], int width)
97 {
98 const uint16_t *main16 = (const uint16_t *)main8;
99 const uint16_t *ref16 = (const uint16_t *)ref8;
100 int x, y, z;
101
102 main_stride >>= 1;
103 ref_stride >>= 1;
104
105 for (z = 0; z < width; z++) {
106 uint64_t s1 = 0, s2 = 0, ss = 0, s12 = 0;
107
108 for (y = 0; y < 4; y++) {
109 for (x = 0; x < 4; x++) {
110 unsigned a = main16[x + y * main_stride];
111 unsigned b = ref16[x + y * ref_stride];
112
113 s1 += a;
114 s2 += b;
115 ss += a*a;
116 ss += b*b;
117 s12 += a*b;
118 }
119 }
120
121 sums[z][0] = s1;
122 sums[z][1] = s2;
123 sums[z][2] = ss;
124 sums[z][3] = s12;
125 main16 += 4;
126 ref16 += 4;
127 }
128 }
129
130 1740 static void ssim_4x4xn_8bit(const uint8_t *main, ptrdiff_t main_stride,
131 const uint8_t *ref, ptrdiff_t ref_stride,
132 int (*sums)[4], int width)
133 {
134 int x, y, z;
135
136
2/2
✓ Branch 0 taken 108460 times.
✓ Branch 1 taken 1740 times.
110200 for (z = 0; z < width; z++) {
137 108460 uint32_t s1 = 0, s2 = 0, ss = 0, s12 = 0;
138
139
2/2
✓ Branch 0 taken 433840 times.
✓ Branch 1 taken 108460 times.
542300 for (y = 0; y < 4; y++) {
140
2/2
✓ Branch 0 taken 1735360 times.
✓ Branch 1 taken 433840 times.
2169200 for (x = 0; x < 4; x++) {
141 1735360 int a = main[x + y * main_stride];
142 1735360 int b = ref[x + y * ref_stride];
143
144 1735360 s1 += a;
145 1735360 s2 += b;
146 1735360 ss += a*a;
147 1735360 ss += b*b;
148 1735360 s12 += a*b;
149 }
150 }
151
152 108460 sums[z][0] = s1;
153 108460 sums[z][1] = s2;
154 108460 sums[z][2] = ss;
155 108460 sums[z][3] = s12;
156 108460 main += 4;
157 108460 ref += 4;
158 }
159 1740 }
160
161 static float ssim_end1x(int64_t s1, int64_t s2, int64_t ss, int64_t s12, int max)
162 {
163 int64_t ssim_c1 = (int64_t)(.01*.01*max*max*64 + .5);
164 int64_t ssim_c2 = (int64_t)(.03*.03*max*max*64*63 + .5);
165
166 int64_t fs1 = s1;
167 int64_t fs2 = s2;
168 int64_t fss = ss;
169 int64_t fs12 = s12;
170 int64_t vars = fss * 64 - fs1 * fs1 - fs2 * fs2;
171 int64_t covar = fs12 * 64 - fs1 * fs2;
172
173 return (float)(2 * fs1 * fs2 + ssim_c1) * (float)(2 * covar + ssim_c2)
174 / ((float)(fs1 * fs1 + fs2 * fs2 + ssim_c1) * (float)(vars + ssim_c2));
175 }
176
177 90160 static float ssim_end1(int s1, int s2, int ss, int s12)
178 {
179 static const int ssim_c1 = (int)(.01*.01*255*255*64 + .5);
180 static const int ssim_c2 = (int)(.03*.03*255*255*64*63 + .5);
181
182 90160 int fs1 = s1;
183 90160 int fs2 = s2;
184 90160 int fss = ss;
185 90160 int fs12 = s12;
186 90160 int vars = fss * 64 - fs1 * fs1 - fs2 * fs2;
187 90160 int covar = fs12 * 64 - fs1 * fs2;
188
189 90160 return (float)(2 * fs1 * fs2 + ssim_c1) * (float)(2 * covar + ssim_c2)
190 90160 / ((float)(fs1 * fs1 + fs2 * fs2 + ssim_c1) * (float)(vars + ssim_c2));
191 }
192
193 static float ssim_endn_16bit(const int64_t (*sum0)[4], const int64_t (*sum1)[4], int width, int max)
194 {
195 float ssim = 0.0;
196 int i;
197
198 for (i = 0; i < width; i++)
199 ssim += ssim_end1x(sum0[i][0] + sum0[i + 1][0] + sum1[i][0] + sum1[i + 1][0],
200 sum0[i][1] + sum0[i + 1][1] + sum1[i][1] + sum1[i + 1][1],
201 sum0[i][2] + sum0[i + 1][2] + sum1[i][2] + sum1[i + 1][2],
202 sum0[i][3] + sum0[i + 1][3] + sum1[i][3] + sum1[i + 1][3],
203 max);
204 return ssim;
205 }
206
207 1470 static double ssim_endn_8bit(const int (*sum0)[4], const int (*sum1)[4], int width)
208 {
209 1470 double ssim = 0.0;
210 int i;
211
212
2/2
✓ Branch 0 taken 90160 times.
✓ Branch 1 taken 1470 times.
91630 for (i = 0; i < width; i++)
213 90160 ssim += ssim_end1(sum0[i][0] + sum0[i + 1][0] + sum1[i][0] + sum1[i + 1][0],
214 90160 sum0[i][1] + sum0[i + 1][1] + sum1[i][1] + sum1[i + 1][1],
215 90160 sum0[i][2] + sum0[i + 1][2] + sum1[i][2] + sum1[i + 1][2],
216 90160 sum0[i][3] + sum0[i + 1][3] + sum1[i][3] + sum1[i + 1][3]);
217 1470 return ssim;
218 }
219
220 #define SUM_LEN(w) (((w) >> 2) + 3)
221
222 typedef struct ThreadData {
223 const uint8_t *main_data[4];
224 const uint8_t *ref_data[4];
225 int main_linesize[4];
226 int ref_linesize[4];
227 int planewidth[4];
228 int planeheight[4];
229 double **score;
230 int **temp;
231 int nb_components;
232 int max;
233 SSIMDSPContext *dsp;
234 } ThreadData;
235
236 static int ssim_plane_16bit(AVFilterContext *ctx, void *arg,
237 int jobnr, int nb_jobs)
238 {
239 ThreadData *td = arg;
240 double *score = td->score[jobnr];
241 void *temp = td->temp[jobnr];
242 const int max = td->max;
243
244 for (int c = 0; c < td->nb_components; c++) {
245 const uint8_t *main_data = td->main_data[c];
246 const uint8_t *ref_data = td->ref_data[c];
247 const int main_stride = td->main_linesize[c];
248 const int ref_stride = td->ref_linesize[c];
249 int width = td->planewidth[c];
250 int height = td->planeheight[c];
251 const int slice_start = ((height >> 2) * jobnr) / nb_jobs;
252 const int slice_end = ((height >> 2) * (jobnr+1)) / nb_jobs;
253 const int ystart = FFMAX(1, slice_start);
254 int z = ystart - 1;
255 double ssim = 0.0;
256 int64_t (*sum0)[4] = temp;
257 int64_t (*sum1)[4] = sum0 + SUM_LEN(width);
258
259 width >>= 2;
260 height >>= 2;
261
262 for (int y = ystart; y < slice_end; y++) {
263 for (; z <= y; z++) {
264 FFSWAP(void*, sum0, sum1);
265 ssim_4x4xn_16bit(&main_data[4 * z * main_stride], main_stride,
266 &ref_data[4 * z * ref_stride], ref_stride,
267 sum0, width);
268 }
269
270 ssim += ssim_endn_16bit((const int64_t (*)[4])sum0, (const int64_t (*)[4])sum1, width - 1, max);
271 }
272
273 score[c] = ssim;
274 }
275
276 return 0;
277 }
278
279 90 static int ssim_plane(AVFilterContext *ctx, void *arg,
280 int jobnr, int nb_jobs)
281 {
282 90 ThreadData *td = arg;
283 90 double *score = td->score[jobnr];
284 90 void *temp = td->temp[jobnr];
285 90 SSIMDSPContext *dsp = td->dsp;
286
287
2/2
✓ Branch 0 taken 270 times.
✓ Branch 1 taken 90 times.
360 for (int c = 0; c < td->nb_components; c++) {
288 270 const uint8_t *main_data = td->main_data[c];
289 270 const uint8_t *ref_data = td->ref_data[c];
290 270 const int main_stride = td->main_linesize[c];
291 270 const int ref_stride = td->ref_linesize[c];
292 270 int width = td->planewidth[c];
293 270 int height = td->planeheight[c];
294 270 const int slice_start = ((height >> 2) * jobnr) / nb_jobs;
295 270 const int slice_end = ((height >> 2) * (jobnr+1)) / nb_jobs;
296 270 const int ystart = FFMAX(1, slice_start);
297 270 int z = ystart - 1;
298 270 double ssim = 0.0;
299 270 int (*sum0)[4] = temp;
300 270 int (*sum1)[4] = sum0 + SUM_LEN(width);
301
302 270 width >>= 2;
303 270 height >>= 2;
304
305
2/2
✓ Branch 0 taken 1470 times.
✓ Branch 1 taken 270 times.
1740 for (int y = ystart; y < slice_end; y++) {
306
2/2
✓ Branch 0 taken 1740 times.
✓ Branch 1 taken 1470 times.
3210 for (; z <= y; z++) {
307 1740 FFSWAP(void*, sum0, sum1);
308 1740 dsp->ssim_4x4_line(&main_data[4 * z * main_stride], main_stride,
309 1740 &ref_data[4 * z * ref_stride], ref_stride,
310 sum0, width);
311 }
312
313 1470 ssim += dsp->ssim_end_line((const int (*)[4])sum0, (const int (*)[4])sum1, width - 1);
314 }
315
316 270 score[c] = ssim;
317 }
318
319 90 return 0;
320 }
321
322 18 static double ssim_db(double ssim, double weight)
323 {
324
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 return (fabs(weight - ssim) > 1e-9) ? 10.0 * log10(weight / (weight - ssim)) : INFINITY;
325 }
326
327 10 static int do_ssim(FFFrameSync *fs)
328 {
329 10 AVFilterContext *ctx = fs->parent;
330 10 SSIMContext *s = ctx->priv;
331 AVFrame *master, *ref;
332 AVDictionary **metadata;
333 10 double c[4] = {0}, ssimv = 0.0;
334 ThreadData td;
335 int ret, i;
336
337 10 ret = ff_framesync_dualinput_get(fs, &master, &ref);
338
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (ret < 0)
339 return ret;
340
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
10 if (ctx->is_disabled || !ref)
341 return ff_filter_frame(ctx->outputs[0], master);
342 10 metadata = &master->metadata;
343
344 10 s->nb_frames++;
345
346 10 td.nb_components = s->nb_components;
347 10 td.dsp = &s->dsp;
348 10 td.score = s->score;
349 10 td.temp = s->temp;
350 10 td.max = s->max;
351
352
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 10 times.
40 for (int n = 0; n < s->nb_components; n++) {
353 30 td.main_data[n] = master->data[n];
354 30 td.ref_data[n] = ref->data[n];
355 30 td.main_linesize[n] = master->linesize[n];
356 30 td.ref_linesize[n] = ref->linesize[n];
357 30 td.planewidth[n] = s->planewidth[n];
358 30 td.planeheight[n] = s->planeheight[n];
359 }
360
361 10 ff_filter_execute(ctx, s->ssim_plane, &td, NULL,
362 10 FFMIN((s->planeheight[1] + 3) >> 2, s->nb_threads));
363
364
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 10 times.
40 for (i = 0; i < s->nb_components; i++) {
365
2/2
✓ Branch 0 taken 270 times.
✓ Branch 1 taken 30 times.
300 for (int j = 0; j < s->nb_threads; j++)
366 270 c[i] += s->score[j][i];
367 30 c[i] = c[i] / (((s->planewidth[i] >> 2) - 1) * ((s->planeheight[i] >> 2) - 1));
368 }
369
370
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 10 times.
40 for (i = 0; i < s->nb_components; i++) {
371 30 ssimv += s->coefs[i] * c[i];
372 30 s->ssim[i] += c[i];
373 }
374
375
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 10 times.
40 for (i = 0; i < s->nb_components; i++) {
376
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
30 int cidx = s->is_rgb ? s->rgba_map[i] : i;
377 30 set_meta(metadata, "lavfi.ssim.", s->comps[i], c[cidx]);
378 }
379 10 s->ssim_total += ssimv;
380
381 10 set_meta(metadata, "lavfi.ssim.All", 0, ssimv);
382 10 set_meta(metadata, "lavfi.ssim.dB", 0, ssim_db(ssimv, 1.0));
383
384
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (s->stats_file) {
385 fprintf(s->stats_file, "n:%"PRId64" ", s->nb_frames);
386
387 for (i = 0; i < s->nb_components; i++) {
388 int cidx = s->is_rgb ? s->rgba_map[i] : i;
389 fprintf(s->stats_file, "%c:%f ", s->comps[i], c[cidx]);
390 }
391
392 fprintf(s->stats_file, "All:%f (%f)\n", ssimv, ssim_db(ssimv, 1.0));
393 }
394
395 10 return ff_filter_frame(ctx->outputs[0], master);
396 }
397
398 4 static av_cold int init(AVFilterContext *ctx)
399 {
400 4 SSIMContext *s = ctx->priv;
401
402
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (s->stats_file_str) {
403 if (!strcmp(s->stats_file_str, "-")) {
404 s->stats_file = stdout;
405 } else {
406 s->stats_file = avpriv_fopen_utf8(s->stats_file_str, "w");
407 if (!s->stats_file) {
408 int err = AVERROR(errno);
409 char buf[128];
410 av_strerror(err, buf, sizeof(buf));
411 av_log(ctx, AV_LOG_ERROR, "Could not open stats file %s: %s\n",
412 s->stats_file_str, buf);
413 return err;
414 }
415 }
416 }
417
418 4 s->fs.on_event = do_ssim;
419 4 return 0;
420 }
421
422 static const enum AVPixelFormat pix_fmts[] = {
423 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10,
424 AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
425 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
426 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
427 AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
428 AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
429 AV_PIX_FMT_GBRP,
430 #define PF(suf) AV_PIX_FMT_YUV420##suf, AV_PIX_FMT_YUV422##suf, AV_PIX_FMT_YUV444##suf, AV_PIX_FMT_GBR##suf
431 PF(P9), PF(P10), PF(P12), PF(P14), PF(P16),
432 AV_PIX_FMT_NONE
433 };
434
435 2 static int config_input_ref(AVFilterLink *inlink)
436 {
437 2 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
438 2 AVFilterContext *ctx = inlink->dst;
439 2 SSIMContext *s = ctx->priv;
440 2 int sum = 0, i;
441
442 2 s->nb_threads = ff_filter_get_nb_threads(ctx);
443 2 s->nb_components = desc->nb_components;
444
445
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (ctx->inputs[0]->w != ctx->inputs[1]->w ||
446
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 ctx->inputs[0]->h != ctx->inputs[1]->h) {
447 av_log(ctx, AV_LOG_ERROR, "Width and height of input videos must be same.\n");
448 return AVERROR(EINVAL);
449 }
450
451 2 s->is_rgb = ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0;
452
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 s->comps[0] = s->is_rgb ? 'R' : 'Y';
453
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 s->comps[1] = s->is_rgb ? 'G' : 'U';
454
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 s->comps[2] = s->is_rgb ? 'B' : 'V';
455 2 s->comps[3] = 'A';
456
457 2 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
458 2 s->planeheight[0] = s->planeheight[3] = inlink->h;
459 2 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
460 2 s->planewidth[0] = s->planewidth[3] = inlink->w;
461
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (i = 0; i < s->nb_components; i++)
462 6 sum += s->planeheight[i] * s->planewidth[i];
463
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (i = 0; i < s->nb_components; i++)
464 6 s->coefs[i] = (double) s->planeheight[i] * s->planewidth[i] / sum;
465
466 2 s->temp = av_calloc(s->nb_threads, sizeof(*s->temp));
467
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->temp)
468 return AVERROR(ENOMEM);
469
470
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
20 for (int t = 0; t < s->nb_threads; t++) {
471
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 s->temp[t] = av_calloc(2 * SUM_LEN(inlink->w), (desc->comp[0].depth > 8) ? sizeof(int64_t[4]) : sizeof(int[4]));
472
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (!s->temp[t])
473 return AVERROR(ENOMEM);
474 }
475 2 s->max = (1 << desc->comp[0].depth) - 1;
476
477
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 s->ssim_plane = desc->comp[0].depth > 8 ? ssim_plane_16bit : ssim_plane;
478 2 s->dsp.ssim_4x4_line = ssim_4x4xn_8bit;
479 2 s->dsp.ssim_end_line = ssim_endn_8bit;
480 #if ARCH_X86
481 2 ff_ssim_init_x86(&s->dsp);
482 #endif
483
484 2 s->score = av_calloc(s->nb_threads, sizeof(*s->score));
485
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->score)
486 return AVERROR(ENOMEM);
487
488
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
20 for (int t = 0; t < s->nb_threads; t++) {
489 18 s->score[t] = av_calloc(s->nb_components, sizeof(*s->score[0]));
490
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (!s->score[t])
491 return AVERROR(ENOMEM);
492 }
493
494 2 return 0;
495 }
496
497 2 static int config_output(AVFilterLink *outlink)
498 {
499 2 AVFilterContext *ctx = outlink->src;
500 2 SSIMContext *s = ctx->priv;
501 2 AVFilterLink *mainlink = ctx->inputs[0];
502 int ret;
503
504 2 ret = ff_framesync_init_dualinput(&s->fs, ctx);
505
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
506 return ret;
507 2 outlink->w = mainlink->w;
508 2 outlink->h = mainlink->h;
509 2 outlink->time_base = mainlink->time_base;
510 2 outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
511 2 outlink->frame_rate = mainlink->frame_rate;
512
513
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = ff_framesync_configure(&s->fs)) < 0)
514 return ret;
515
516 2 outlink->time_base = s->fs.time_base;
517
518
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
4 if (av_cmp_q(mainlink->time_base, outlink->time_base) ||
519 2 av_cmp_q(ctx->inputs[1]->time_base, outlink->time_base))
520 av_log(ctx, AV_LOG_WARNING, "not matching timebases found between first input: %d/%d and second input %d/%d, results may be incorrect!\n",
521 mainlink->time_base.num, mainlink->time_base.den,
522 ctx->inputs[1]->time_base.num, ctx->inputs[1]->time_base.den);
523
524 2 return 0;
525 }
526
527 30 static int activate(AVFilterContext *ctx)
528 {
529 30 SSIMContext *s = ctx->priv;
530 30 return ff_framesync_activate(&s->fs);
531 }
532
533 4 static av_cold void uninit(AVFilterContext *ctx)
534 {
535 4 SSIMContext *s = ctx->priv;
536
537
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (s->nb_frames > 0) {
538 char buf[256];
539 int i;
540 2 buf[0] = 0;
541
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (i = 0; i < s->nb_components; i++) {
542
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 int c = s->is_rgb ? s->rgba_map[i] : i;
543 6 av_strlcatf(buf, sizeof(buf), " %c:%f (%f)", s->comps[i], s->ssim[c] / s->nb_frames,
544 6 ssim_db(s->ssim[c], s->nb_frames));
545 }
546 4 av_log(ctx, AV_LOG_INFO, "SSIM%s All:%f (%f)\n", buf,
547 2 s->ssim_total / s->nb_frames, ssim_db(s->ssim_total, s->nb_frames));
548 }
549
550 4 ff_framesync_uninit(&s->fs);
551
552
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4 if (s->stats_file && s->stats_file != stdout)
553 fclose(s->stats_file);
554
555
3/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
22 for (int t = 0; t < s->nb_threads && s->score; t++)
556 18 av_freep(&s->score[t]);
557 4 av_freep(&s->score);
558
559
3/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
22 for (int t = 0; t < s->nb_threads && s->temp; t++)
560 18 av_freep(&s->temp[t]);
561 4 av_freep(&s->temp);
562 4 }
563
564 static const AVFilterPad ssim_inputs[] = {
565 {
566 .name = "main",
567 .type = AVMEDIA_TYPE_VIDEO,
568 },{
569 .name = "reference",
570 .type = AVMEDIA_TYPE_VIDEO,
571 .config_props = config_input_ref,
572 },
573 };
574
575 static const AVFilterPad ssim_outputs[] = {
576 {
577 .name = "default",
578 .type = AVMEDIA_TYPE_VIDEO,
579 .config_props = config_output,
580 },
581 };
582
583 const AVFilter ff_vf_ssim = {
584 .name = "ssim",
585 .description = NULL_IF_CONFIG_SMALL("Calculate the SSIM between two video streams."),
586 .preinit = ssim_framesync_preinit,
587 .init = init,
588 .uninit = uninit,
589 .activate = activate,
590 .priv_size = sizeof(SSIMContext),
591 .priv_class = &ssim_class,
592 FILTER_INPUTS(ssim_inputs),
593 FILTER_OUTPUTS(ssim_outputs),
594 FILTER_PIXFMTS_ARRAY(pix_fmts),
595 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
596 AVFILTER_FLAG_SLICE_THREADS |
597 AVFILTER_FLAG_METADATA_ONLY,
598 };
599