Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Copyright (c) 2024 Christian R. Helmrich | ||
3 | * Copyright (c) 2024 Christian Lehmann | ||
4 | * Copyright (c) 2024 Christian Stoffers | ||
5 | * | ||
6 | * This file is part of FFmpeg. | ||
7 | * | ||
8 | * FFmpeg is free software; you can redistribute it and/or | ||
9 | * modify it under the terms of the GNU Lesser General Public | ||
10 | * License as published by the Free Software Foundation; either | ||
11 | * version 2.1 of the License, or (at your option) any later version. | ||
12 | * | ||
13 | * FFmpeg is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | * Lesser General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU Lesser General Public | ||
19 | * License along with FFmpeg; if not, write to the Free Software | ||
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
21 | */ | ||
22 | |||
23 | /** | ||
24 | * @file | ||
25 | * Calculate the extended perceptually weighted PSNR (XPSNR) between two input videos. | ||
26 | * | ||
27 | * Authors: Christian Helmrich, Lehmann, and Stoffers, Fraunhofer HHI, Berlin, Germany | ||
28 | */ | ||
29 | |||
30 | #include "libavutil/avstring.h" | ||
31 | #include "libavutil/file_open.h" | ||
32 | #include "libavutil/mem.h" | ||
33 | #include "libavutil/opt.h" | ||
34 | #include "libavutil/pixdesc.h" | ||
35 | #include "avfilter.h" | ||
36 | #include "drawutils.h" | ||
37 | #include "filters.h" | ||
38 | #include "framesync.h" | ||
39 | #include "psnr.h" | ||
40 | #include "xpsnr.h" | ||
41 | |||
42 | /* XPSNR structure definition */ | ||
43 | |||
44 | typedef struct XPSNRContext { | ||
45 | /* required basic variables */ | ||
46 | const AVClass *class; | ||
47 | int bpp; /* unpacked */ | ||
48 | int depth; /* packed */ | ||
49 | char comps[4]; | ||
50 | int num_comps; | ||
51 | uint64_t num_frames_64; | ||
52 | unsigned frame_rate; | ||
53 | FFFrameSync fs; | ||
54 | int line_sizes[4]; | ||
55 | int plane_height[4]; | ||
56 | int plane_width[4]; | ||
57 | uint8_t rgba_map[4]; | ||
58 | FILE *stats_file; | ||
59 | char *stats_file_str; | ||
60 | /* XPSNR specific variables */ | ||
61 | double *sse_luma; | ||
62 | double *weights; | ||
63 | AVBufferRef *buf_org [3]; | ||
64 | AVBufferRef *buf_org_m1[3]; | ||
65 | AVBufferRef *buf_org_m2[3]; | ||
66 | AVBufferRef *buf_rec [3]; | ||
67 | uint64_t max_error_64; | ||
68 | double sum_wdist [3]; | ||
69 | double sum_xpsnr [3]; | ||
70 | int and_is_inf[3]; | ||
71 | int is_rgb; | ||
72 | XPSNRDSPContext dsp; | ||
73 | PSNRDSPContext pdsp; | ||
74 | } XPSNRContext; | ||
75 | |||
76 | /* required macro definitions */ | ||
77 | |||
78 | #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM | ||
79 | #define OFFSET(x) offsetof(XPSNRContext, x) | ||
80 | #define XPSNR_GAMMA 2 | ||
81 | |||
82 | static const AVOption xpsnr_options[] = { | ||
83 | {"stats_file", "Set file where to store per-frame XPSNR information", OFFSET(stats_file_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS}, | ||
84 | {"f", "Set file where to store per-frame XPSNR information", OFFSET(stats_file_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS}, | ||
85 | { NULL } | ||
86 | }; | ||
87 | |||
88 |
0/2✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
8 | FRAMESYNC_DEFINE_CLASS(xpsnr, XPSNRContext, fs); |
89 | |||
90 | /* XPSNR function definitions */ | ||
91 | |||
92 | ✗ | static uint64_t highds(const int x_act, const int y_act, const int w_act, const int h_act, const int16_t *o_m0, const int o) | |
93 | { | ||
94 | ✗ | uint64_t sa_act = 0; | |
95 | |||
96 | ✗ | for (int y = y_act; y < h_act; y += 2) { | |
97 | ✗ | for (int x = x_act; x < w_act; x += 2) { | |
98 | ✗ | const int f = 12 * ((int)o_m0[ y *o + x ] + (int)o_m0[ y *o + x+1] + (int)o_m0[(y+1)*o + x ] + (int)o_m0[(y+1)*o + x+1]) | |
99 | ✗ | - 3 * ((int)o_m0[(y-1)*o + x ] + (int)o_m0[(y-1)*o + x+1] + (int)o_m0[(y+2)*o + x ] + (int)o_m0[(y+2)*o + x+1]) | |
100 | ✗ | - 3 * ((int)o_m0[ y *o + x-1] + (int)o_m0[ y *o + x+2] + (int)o_m0[(y+1)*o + x-1] + (int)o_m0[(y+1)*o + x+2]) | |
101 | ✗ | - 2 * ((int)o_m0[(y-1)*o + x-1] + (int)o_m0[(y-1)*o + x+2] + (int)o_m0[(y+2)*o + x-1] + (int)o_m0[(y+2)*o + x+2]) | |
102 | ✗ | - ((int)o_m0[(y-2)*o + x-1] + (int)o_m0[(y-2)*o + x ] + (int)o_m0[(y-2)*o + x+1] + (int)o_m0[(y-2)*o + x+2] | |
103 | ✗ | + (int)o_m0[(y+3)*o + x-1] + (int)o_m0[(y+3)*o + x ] + (int)o_m0[(y+3)*o + x+1] + (int)o_m0[(y+3)*o + x+2] | |
104 | ✗ | + (int)o_m0[(y-1)*o + x-2] + (int)o_m0[ y *o + x-2] + (int)o_m0[(y+1)*o + x-2] + (int)o_m0[(y+2)*o + x-2] | |
105 | ✗ | + (int)o_m0[(y-1)*o + x+3] + (int)o_m0[ y *o + x+3] + (int)o_m0[(y+1)*o + x+3] + (int)o_m0[(y+2)*o + x+3]); | |
106 | ✗ | sa_act += (uint64_t) abs(f); | |
107 | } | ||
108 | } | ||
109 | ✗ | return sa_act; | |
110 | } | ||
111 | |||
112 | ✗ | static uint64_t diff1st(const uint32_t w_act, const uint32_t h_act, const int16_t *o_m0, int16_t *o_m1, const int o) | |
113 | { | ||
114 | ✗ | uint64_t ta_act = 0; | |
115 | |||
116 | ✗ | for (uint32_t y = 0; y < h_act; y += 2) { | |
117 | ✗ | for (uint32_t x = 0; x < w_act; x += 2) { | |
118 | ✗ | const int t = (int)o_m0[y*o + x] + (int)o_m0[y*o + x+1] + (int)o_m0[(y+1)*o + x] + (int)o_m0[(y+1)*o + x+1] | |
119 | ✗ | - ((int)o_m1[y*o + x] + (int)o_m1[y*o + x+1] + (int)o_m1[(y+1)*o + x] + (int)o_m1[(y+1)*o + x+1]); | |
120 | ✗ | ta_act += (uint64_t) abs(t); | |
121 | ✗ | o_m1[y*o + x ] = o_m0[y*o + x ]; o_m1[(y+1)*o + x ] = o_m0[(y+1)*o + x ]; | |
122 | ✗ | o_m1[y*o + x+1] = o_m0[y*o + x+1]; o_m1[(y+1)*o + x+1] = o_m0[(y+1)*o + x+1]; | |
123 | } | ||
124 | } | ||
125 | ✗ | return (ta_act * XPSNR_GAMMA); | |
126 | } | ||
127 | |||
128 | ✗ | static uint64_t diff2nd(const uint32_t w_act, const uint32_t h_act, const int16_t *o_m0, int16_t *o_m1, int16_t *o_m2, const int o) | |
129 | { | ||
130 | ✗ | uint64_t ta_act = 0; | |
131 | |||
132 | ✗ | for (uint32_t y = 0; y < h_act; y += 2) { | |
133 | ✗ | for (uint32_t x = 0; x < w_act; x += 2) { | |
134 | ✗ | const int t = (int)o_m0[y*o + x] + (int)o_m0[y*o + x+1] + (int)o_m0[(y+1)*o + x] + (int)o_m0[(y+1)*o + x+1] | |
135 | ✗ | - 2 * ((int)o_m1[y*o + x] + (int)o_m1[y*o + x+1] + (int)o_m1[(y+1)*o + x] + (int)o_m1[(y+1)*o + x+1]) | |
136 | ✗ | + (int)o_m2[y*o + x] + (int)o_m2[y*o + x+1] + (int)o_m2[(y+1)*o + x] + (int)o_m2[(y+1)*o + x+1]; | |
137 | ✗ | ta_act += (uint64_t) abs(t); | |
138 | ✗ | o_m2[y*o + x ] = o_m1[y*o + x ]; o_m2[(y+1)*o + x ] = o_m1[(y+1)*o + x ]; | |
139 | ✗ | o_m2[y*o + x+1] = o_m1[y*o + x+1]; o_m2[(y+1)*o + x+1] = o_m1[(y+1)*o + x+1]; | |
140 | ✗ | o_m1[y*o + x ] = o_m0[y*o + x ]; o_m1[(y+1)*o + x ] = o_m0[(y+1)*o + x ]; | |
141 | ✗ | o_m1[y*o + x+1] = o_m0[y*o + x+1]; o_m1[(y+1)*o + x+1] = o_m0[(y+1)*o + x+1]; | |
142 | } | ||
143 | } | ||
144 | ✗ | return (ta_act * XPSNR_GAMMA); | |
145 | } | ||
146 | |||
147 | 12750 | static inline uint64_t calc_squared_error(XPSNRContext const *s, | |
148 | const int16_t *blk_org, const uint32_t stride_org, | ||
149 | const int16_t *blk_rec, const uint32_t stride_rec, | ||
150 | const uint32_t block_width, const uint32_t block_height) | ||
151 | { | ||
152 | 12750 | uint64_t sse = 0; /* sum of squared errors */ | |
153 | |||
154 |
2/2✓ Branch 0 taken 150000 times.
✓ Branch 1 taken 12750 times.
|
162750 | for (uint32_t y = 0; y < block_height; y++) { |
155 | 150000 | sse += s->pdsp.sse_line((const uint8_t *) blk_org, (const uint8_t *) blk_rec, (int) block_width); | |
156 | 150000 | blk_org += stride_org; | |
157 | 150000 | blk_rec += stride_rec; | |
158 | } | ||
159 | |||
160 | /* return nonweighted sum of squared errors */ | ||
161 | 12750 | return sse; | |
162 | } | ||
163 | |||
164 | 4250 | static inline double calc_squared_error_and_weight (XPSNRContext const *s, | |
165 | const int16_t *pic_org, const uint32_t stride_org, | ||
166 | int16_t *pic_org_m1, int16_t *pic_org_m2, | ||
167 | const int16_t *pic_rec, const uint32_t stride_rec, | ||
168 | const uint32_t offset_x, const uint32_t offset_y, | ||
169 | const uint32_t block_width, const uint32_t block_height, | ||
170 | const uint32_t bit_depth, const uint32_t int_frame_rate, double *ms_act) | ||
171 | { | ||
172 | 4250 | const int o = (int) stride_org; | |
173 | 4250 | const int r = (int) stride_rec; | |
174 | 4250 | const int16_t *o_m0 = pic_org + offset_y * o + offset_x; | |
175 | 4250 | int16_t *o_m1 = pic_org_m1 + offset_y * o + offset_x; | |
176 | 4250 | int16_t *o_m2 = pic_org_m2 + offset_y * o + offset_x; | |
177 | 4250 | const int16_t *r_m0 = pic_rec + offset_y * r + offset_x; | |
178 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4250 times.
|
4250 | const int b_val = (s->plane_width[0] * s->plane_height[0] > 2048 * 1152 ? 2 : 1); /* threshold is a bit more than HD resolution */ |
179 |
2/2✓ Branch 0 taken 170 times.
✓ Branch 1 taken 4080 times.
|
4250 | const int x_act = (offset_x > 0 ? 0 : b_val); |
180 |
2/2✓ Branch 0 taken 250 times.
✓ Branch 1 taken 4000 times.
|
4250 | const int y_act = (offset_y > 0 ? 0 : b_val); |
181 |
2/2✓ Branch 0 taken 170 times.
✓ Branch 1 taken 4080 times.
|
4250 | const int w_act = (offset_x + block_width < (uint32_t) s->plane_width [0] ? (int) block_width : (int) block_width - b_val); |
182 |
2/2✓ Branch 0 taken 250 times.
✓ Branch 1 taken 4000 times.
|
4250 | const int h_act = (offset_y + block_height < (uint32_t) s->plane_height[0] ? (int) block_height : (int) block_height - b_val); |
183 | |||
184 | 4250 | const double sse = (double) calc_squared_error (s, o_m0, stride_org, | |
185 | r_m0, stride_rec, | ||
186 | block_width, block_height); | ||
187 | 4250 | uint64_t sa_act = 0; /* spatial abs. activity */ | |
188 | 4250 | uint64_t ta_act = 0; /* temporal abs. activity */ | |
189 | |||
190 |
2/4✓ Branch 0 taken 4250 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4250 times.
|
4250 | if (w_act <= x_act || h_act <= y_act) /* small */ |
191 | ✗ | return sse; | |
192 | |||
193 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4250 times.
|
4250 | if (b_val > 1) { /* highpass with downsampling */ |
194 | ✗ | if (w_act > 12) | |
195 | ✗ | sa_act = s->dsp.highds_func(x_act, y_act, w_act, h_act, o_m0, o); | |
196 | else | ||
197 | ✗ | highds(x_act, y_act, w_act, h_act, o_m0, o); | |
198 | } else { /* <=HD highpass without downsampling */ | ||
199 |
2/2✓ Branch 0 taken 49500 times.
✓ Branch 1 taken 4250 times.
|
53750 | for (int y = y_act; y < h_act; y++) { |
200 |
2/2✓ Branch 0 taken 590040 times.
✓ Branch 1 taken 49500 times.
|
639540 | for (int x = x_act; x < w_act; x++) { |
201 | 590040 | const int f = 12 * (int)o_m0[y*o + x] - 2 * ((int)o_m0[y*o + x-1] + (int)o_m0[y*o + x+1] + (int)o_m0[(y-1)*o + x] + (int)o_m0[(y+1)*o + x]) | |
202 | 590040 | - ((int)o_m0[(y-1)*o + x-1] + (int)o_m0[(y-1)*o + x+1] + (int)o_m0[(y+1)*o + x-1] + (int)o_m0[(y+1)*o + x+1]); | |
203 | 590040 | sa_act += (uint64_t) abs(f); | |
204 | } | ||
205 | } | ||
206 | } | ||
207 | |||
208 | /* calculate weight (average squared activity) */ | ||
209 | 4250 | *ms_act = (double) sa_act / ((double) (w_act - x_act) * (double) (h_act - y_act)); | |
210 | |||
211 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4250 times.
|
4250 | if (b_val > 1) { /* highpass with downsampling */ |
212 | ✗ | if (int_frame_rate < 32) /* 1st-order diff */ | |
213 | ✗ | ta_act = s->dsp.diff1st_func(block_width, block_height, o_m0, o_m1, o); | |
214 | else /* 2nd-order diff (diff of two diffs) */ | ||
215 | ✗ | ta_act = s->dsp.diff2nd_func(block_width, block_height, o_m0, o_m1, o_m2, o); | |
216 | } else { /* <=HD highpass without downsampling */ | ||
217 |
1/2✓ Branch 0 taken 4250 times.
✗ Branch 1 not taken.
|
4250 | if (int_frame_rate < 32) { /* 1st-order diff */ |
218 |
2/2✓ Branch 0 taken 50000 times.
✓ Branch 1 taken 4250 times.
|
54250 | for (uint32_t y = 0; y < block_height; y++) { |
219 |
2/2✓ Branch 0 taken 600000 times.
✓ Branch 1 taken 50000 times.
|
650000 | for (uint32_t x = 0; x < block_width; x++) { |
220 | 600000 | const int t = (int)o_m0[y * o + x] - (int)o_m1[y * o + x]; | |
221 | |||
222 | 600000 | ta_act += XPSNR_GAMMA * (uint64_t) abs(t); | |
223 | 600000 | o_m1[y * o + x] = o_m0[y * o + x]; | |
224 | } | ||
225 | } | ||
226 | } else { /* 2nd-order diff (diff of 2 diffs) */ | ||
227 | ✗ | for (uint32_t y = 0; y < block_height; y++) { | |
228 | ✗ | for (uint32_t x = 0; x < block_width; x++) { | |
229 | ✗ | const int t = (int)o_m0[y * o + x] - 2 * (int)o_m1[y * o + x] + (int)o_m2[y * o + x]; | |
230 | |||
231 | ✗ | ta_act += XPSNR_GAMMA * (uint64_t) abs(t); | |
232 | ✗ | o_m2[y * o + x] = o_m1[y * o + x]; | |
233 | ✗ | o_m1[y * o + x] = o_m0[y * o + x]; | |
234 | } | ||
235 | } | ||
236 | } | ||
237 | } | ||
238 | |||
239 | /* weight += mean squared temporal activity */ | ||
240 | 4250 | *ms_act += (double) ta_act / ((double) block_width * (double) block_height); | |
241 | |||
242 | /* lower limit, accounts for high-pass gain */ | ||
243 |
2/2✓ Branch 0 taken 1813 times.
✓ Branch 1 taken 2437 times.
|
4250 | if (*ms_act < (double) (1 << (bit_depth - 6))) |
244 | 1813 | *ms_act = (double) (1 << (bit_depth - 6)); | |
245 | |||
246 | 4250 | *ms_act *= *ms_act; /* since SSE is squared */ | |
247 | |||
248 | /* return nonweighted sum of squared errors */ | ||
249 | 4250 | return sse; | |
250 | } | ||
251 | |||
252 | 36 | static inline double get_avg_xpsnr (const double sqrt_wsse_val, const double sum_xpsnr_val, | |
253 | const uint32_t image_width, const uint32_t image_height, | ||
254 | const uint64_t max_error_64, const uint64_t num_frames_64) | ||
255 | { | ||
256 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
|
36 | if (num_frames_64 == 0) |
257 | ✗ | return INFINITY; | |
258 | |||
259 |
1/2✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
|
36 | if (sqrt_wsse_val >= (double) num_frames_64) { /* square-mean-root average */ |
260 | 36 | const double avg_dist = sqrt_wsse_val / (double) num_frames_64; | |
261 | 36 | const uint64_t num64 = (uint64_t) image_width * (uint64_t) image_height * max_error_64; | |
262 | |||
263 | 36 | return 10.0 * log10((double) num64 / ((double) avg_dist * (double) avg_dist)); | |
264 | } | ||
265 | |||
266 | ✗ | return sum_xpsnr_val / (double) num_frames_64; /* older log-domain average */ | |
267 | } | ||
268 | |||
269 | 10 | static int get_wsse(AVFilterContext *ctx, int16_t **org, int16_t **org_m1, int16_t **org_m2, int16_t **rec, | |
270 | uint64_t *const wsse64) | ||
271 | { | ||
272 | 10 | XPSNRContext *const s = ctx->priv; | |
273 | 10 | const uint32_t w = s->plane_width [0]; /* luma image width in pixels */ | |
274 | 10 | const uint32_t h = s->plane_height[0];/* luma image height in pixels */ | |
275 | 10 | const double r = (double)(w * h) / (3840.0 * 2160.0); /* UHD ratio */ | |
276 | 10 | const uint32_t b = FFMAX(0, 4 * (int32_t) (32.0 * sqrt(r) + | |
277 | 0.5)); /* block size, integer multiple of 4 for SIMD */ | ||
278 | 10 | const uint32_t w_blk = (w + b - 1) / b; /* luma width in units of blocks */ | |
279 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | const double avg_act = sqrt(16.0 * (double) (1 << (2 * s->depth - 9)) / sqrt(FFMAX(0.00001, |
280 | r))); /* the sqrt(a_pic) */ | ||
281 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | const int *stride_org = (s->bpp == 1 ? s->plane_width : s->line_sizes); |
282 | 10 | uint32_t x, y, idx_blk = 0; /* the "16.0" above is due to fixed-point code */ | |
283 | 10 | double *const sse_luma = s->sse_luma; | |
284 | 10 | double *const weights = s->weights; | |
285 | int c; | ||
286 | |||
287 |
4/8✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 10 times.
✗ Branch 7 not taken.
|
10 | if (!wsse64 || (s->depth < 6) || (s->depth > 16) || (s->num_comps <= 0) || |
288 |
3/6✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 10 times.
|
10 | (s->num_comps > 3) || (w == 0) || (h == 0)) { |
289 | ✗ | av_log(ctx, AV_LOG_ERROR, "Error in XPSNR routine: invalid argument(s).\n"); | |
290 | ✗ | return AVERROR(EINVAL); | |
291 | } | ||
292 |
3/6✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 10 times.
|
10 | if (!weights || (b >= 4 && !sse_luma)) { |
293 | ✗ | av_log(ctx, AV_LOG_ERROR, "Failed to allocate temporary block memory.\n"); | |
294 | ✗ | return AVERROR(ENOMEM); | |
295 | } | ||
296 | |||
297 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | if (b >= 4) { |
298 | 10 | const int16_t *p_org = org[0]; | |
299 | 10 | const uint32_t s_org = stride_org[0] / s->bpp; | |
300 | 10 | const int16_t *p_rec = rec[0]; | |
301 | 10 | const uint32_t s_rec = s->plane_width[0]; | |
302 | 10 | int16_t *p_org_m1 = org_m1[0]; /* pixel */ | |
303 | 10 | int16_t *p_org_m2 = org_m2[0]; /* memory */ | |
304 | 10 | double wsse_luma = 0.0; | |
305 | |||
306 |
2/2✓ Branch 0 taken 170 times.
✓ Branch 1 taken 10 times.
|
180 | for (y = 0; y < h; y += b) { /* calculate block SSE and perceptual weights */ |
307 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 160 times.
|
170 | const uint32_t block_height = (y + b > h ? h - y : b); |
308 | |||
309 |
2/2✓ Branch 0 taken 4250 times.
✓ Branch 1 taken 170 times.
|
4420 | for (x = 0; x < w; x += b, idx_blk++) { |
310 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4250 times.
|
4250 | const uint32_t block_width = (x + b > w ? w - x : b); |
311 | 4250 | double ms_act = 1.0, ms_act_prev = 0.0; | |
312 | |||
313 | 8500 | sse_luma[idx_blk] = calc_squared_error_and_weight(s, p_org, s_org, | |
314 | p_org_m1, p_org_m2, | ||
315 | p_rec, s_rec, | ||
316 | x, y, | ||
317 | block_width, block_height, | ||
318 | 4250 | s->depth, s->frame_rate, &ms_act); | |
319 | 4250 | weights[idx_blk] = 1.0 / sqrt(ms_act); | |
320 | |||
321 |
1/2✓ Branch 0 taken 4250 times.
✗ Branch 1 not taken.
|
4250 | if (w * h <= 640 * 480) { /* in-line "min-smoothing" as in paper */ |
322 |
2/2✓ Branch 0 taken 170 times.
✓ Branch 1 taken 4080 times.
|
4250 | if (x == 0) /* first column */ |
323 |
2/2✓ Branch 0 taken 160 times.
✓ Branch 1 taken 10 times.
|
170 | ms_act_prev = (idx_blk > 1 ? weights[idx_blk - 2] : 0); |
324 | else /* after first column */ | ||
325 |
4/4✓ Branch 0 taken 3910 times.
✓ Branch 1 taken 170 times.
✓ Branch 2 taken 1462 times.
✓ Branch 3 taken 2448 times.
|
4080 | ms_act_prev = (x > b ? FFMAX(weights[idx_blk - 2], weights[idx_blk]) : weights[idx_blk]); |
326 | |||
327 |
2/2✓ Branch 0 taken 3990 times.
✓ Branch 1 taken 260 times.
|
4250 | if (idx_blk > w_blk) /* after the first row and first column */ |
328 |
2/2✓ Branch 0 taken 1801 times.
✓ Branch 1 taken 2189 times.
|
3990 | ms_act_prev = FFMAX(ms_act_prev, weights[idx_blk - 1 - w_blk]); /* min (L, T) */ |
329 |
4/4✓ Branch 0 taken 4240 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 145 times.
✓ Branch 3 taken 4095 times.
|
4250 | if ((idx_blk > 0) && (weights[idx_blk - 1] > ms_act_prev)) |
330 | 145 | weights[idx_blk - 1] = ms_act_prev; | |
331 | |||
332 |
5/6✓ Branch 0 taken 170 times.
✓ Branch 1 taken 4080 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 160 times.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
|
4250 | if ((x + b >= w) && (y + b >= h) && (idx_blk > w_blk)) { /* last block in picture */ |
333 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | ms_act_prev = FFMAX(weights[idx_blk - 1], weights[idx_blk - w_blk]); |
334 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6 times.
|
10 | if (weights[idx_blk] > ms_act_prev) |
335 | 4 | weights[idx_blk] = ms_act_prev; | |
336 | } | ||
337 | } | ||
338 | } /* for x */ | ||
339 | } /* for y */ | ||
340 | |||
341 |
2/2✓ Branch 0 taken 170 times.
✓ Branch 1 taken 10 times.
|
180 | for (y = idx_blk = 0; y < h; y += b) { /* calculate sum for luma (Y) XPSNR */ |
342 |
2/2✓ Branch 0 taken 4250 times.
✓ Branch 1 taken 170 times.
|
4420 | for (x = 0; x < w; x += b, idx_blk++) { |
343 | 4250 | wsse_luma += sse_luma[idx_blk] * weights[idx_blk]; | |
344 | } | ||
345 | } | ||
346 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | wsse64[0] = (wsse_luma <= 0.0 ? 0 : (uint64_t) (wsse_luma * avg_act + 0.5)); |
347 | } /* b >= 4 */ | ||
348 | |||
349 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 10 times.
|
40 | for (c = 0; c < s->num_comps; c++) { /* finalize WSSE value for each component */ |
350 | 30 | const int16_t *p_org = org[c]; | |
351 | 30 | const uint32_t s_org = stride_org[c] / s->bpp; | |
352 | 30 | const int16_t *p_rec = rec[c]; | |
353 | 30 | const uint32_t s_rec = s->plane_width[c]; | |
354 | 30 | const uint32_t w_pln = s->plane_width[c]; | |
355 | 30 | const uint32_t h_pln = s->plane_height[c]; | |
356 | |||
357 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if (b < 4) /* picture is too small for XPSNR, calculate nonweighted PSNR */ |
358 | ✗ | wsse64[c] = calc_squared_error (s, p_org, s_org, | |
359 | p_rec, s_rec, | ||
360 | w_pln, h_pln); | ||
361 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 10 times.
|
30 | else if (c > 0) { /* b >= 4 so Y XPSNR has already been calculated above */ |
362 | 20 | const uint32_t bx = (b * w_pln) / w; | |
363 | 20 | const uint32_t by = (b * h_pln) / h; /* up to chroma downsampling by 4 */ | |
364 | 20 | double wsse_chroma = 0.0; | |
365 | |||
366 |
2/2✓ Branch 0 taken 340 times.
✓ Branch 1 taken 20 times.
|
360 | for (y = idx_blk = 0; y < h_pln; y += by) { /* calc chroma (Cb/Cr) XPSNR */ |
367 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 320 times.
|
340 | const uint32_t block_height = (y + by > h_pln ? h_pln - y : by); |
368 | |||
369 |
2/2✓ Branch 0 taken 8500 times.
✓ Branch 1 taken 340 times.
|
8840 | for (x = 0; x < w_pln; x += bx, idx_blk++) { |
370 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8500 times.
|
8500 | const uint32_t block_width = (x + bx > w_pln ? w_pln - x : bx); |
371 | |||
372 | 17000 | wsse_chroma += (double) calc_squared_error (s, p_org + y * s_org + x, s_org, | |
373 | 8500 | p_rec + y * s_rec + x, s_rec, | |
374 | 8500 | block_width, block_height) * weights[idx_blk]; | |
375 | } | ||
376 | } | ||
377 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
|
20 | wsse64[c] = (wsse_chroma <= 0.0 ? 0 : (uint64_t) (wsse_chroma * avg_act + 0.5)); |
378 | } | ||
379 | } /* for c */ | ||
380 | |||
381 | 10 | return 0; | |
382 | } | ||
383 | |||
384 | 30 | static void set_meta(AVDictionary **metadata, const char *key, char comp, float d) | |
385 | { | ||
386 | char value[128]; | ||
387 | 30 | snprintf(value, sizeof(value), "%f", d); | |
388 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 | if (comp) { |
389 | char key2[128]; | ||
390 | 30 | snprintf(key2, sizeof(key2), "%s%c", key, comp); | |
391 | 30 | av_dict_set(metadata, key2, value, 0); | |
392 | } else { | ||
393 | ✗ | av_dict_set(metadata, key, value, 0); | |
394 | } | ||
395 | 30 | } | |
396 | |||
397 | 10 | static int do_xpsnr(FFFrameSync *fs) | |
398 | { | ||
399 | 10 | AVFilterContext *ctx = fs->parent; | |
400 | 10 | XPSNRContext *const s = ctx->priv; | |
401 | 10 | const uint32_t w = s->plane_width [0]; /* luma image width in pixels */ | |
402 | 10 | const uint32_t h = s->plane_height[0]; /* luma image height in pixels */ | |
403 | 10 | const uint32_t b = FFMAX(0, 4 * (int32_t) (32.0 * sqrt((double) (w * h) / (3840.0 * 2160.0)) + 0.5)); /* block size */ | |
404 | 10 | const uint32_t w_blk = (w + b - 1) / b; /* luma width in units of blocks */ | |
405 | 10 | const uint32_t h_blk = (h + b - 1) / b; /* luma height in units of blocks */ | |
406 | 10 | AVFrame *master, *ref = NULL; | |
407 | int16_t *porg [3]; | ||
408 | int16_t *porg_m1[3]; | ||
409 | int16_t *porg_m2[3]; | ||
410 | int16_t *prec [3]; | ||
411 | 10 | uint64_t wsse64 [3] = {0, 0, 0}; | |
412 | 10 | double cur_xpsnr[3] = {INFINITY, INFINITY, INFINITY}; | |
413 | int c, ret_value; | ||
414 | AVDictionary **metadata; | ||
415 | |||
416 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
|
10 | if ((ret_value = ff_framesync_dualinput_get(fs, &master, &ref)) < 0) |
417 | ✗ | return ret_value; | |
418 |
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) |
419 | ✗ | return ff_filter_frame(ctx->outputs[0], master); | |
420 | 10 | metadata = &master->metadata; | |
421 | |||
422 | /* prepare XPSNR calculations: allocate temporary picture and block memory */ | ||
423 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
|
10 | if (!s->sse_luma) |
424 | 2 | s->sse_luma = av_malloc_array(w_blk * h_blk, sizeof(double)); | |
425 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
|
10 | if (!s->weights) |
426 | 2 | s->weights = av_malloc_array(w_blk * h_blk, sizeof(double)); | |
427 | |||
428 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 10 times.
|
40 | for (c = 0; c < s->num_comps; c++) { /* create temporal org buffer memory */ |
429 | 30 | s->line_sizes[c] = master->linesize[c]; | |
430 | |||
431 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 20 times.
|
30 | if (c == 0) { /* luma ch. */ |
432 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | const int stride_org_bpp = (s->bpp == 1 ? s->plane_width[c] : s->line_sizes[c] / s->bpp); |
433 | |||
434 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
|
10 | if (!s->buf_org_m1[c]) |
435 | 2 | s->buf_org_m1[c] = av_buffer_allocz(stride_org_bpp * s->plane_height[c] * sizeof(int16_t)); | |
436 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
|
10 | if (!s->buf_org_m2[c]) |
437 | 2 | s->buf_org_m2[c] = av_buffer_allocz(stride_org_bpp * s->plane_height[c] * sizeof(int16_t)); | |
438 | |||
439 | 10 | porg_m1[c] = (int16_t *) s->buf_org_m1[c]->data; | |
440 | 10 | porg_m2[c] = (int16_t *) s->buf_org_m2[c]->data; | |
441 | } | ||
442 | } | ||
443 | |||
444 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | if (s->bpp == 1) { /* 8 bit */ |
445 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 10 times.
|
40 | for (c = 0; c < s->num_comps; c++) { /* allocate org/rec buffer memory */ |
446 | 30 | const int m = s->line_sizes[c]; /* master stride */ | |
447 | 30 | const int r = ref->linesize[c]; /* ref/c stride */ | |
448 | 30 | const int o = s->plane_width[c]; /* XPSNR stride */ | |
449 | |||
450 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 24 times.
|
30 | if (!s->buf_org[c]) |
451 | 6 | s->buf_org[c] = av_buffer_allocz(s->plane_width[c] * s->plane_height[c] * sizeof(int16_t)); | |
452 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 24 times.
|
30 | if (!s->buf_rec[c]) |
453 | 6 | s->buf_rec[c] = av_buffer_allocz(s->plane_width[c] * s->plane_height[c] * sizeof(int16_t)); | |
454 | |||
455 | 30 | porg[c] = (int16_t *) s->buf_org[c]->data; | |
456 | 30 | prec[c] = (int16_t *) s->buf_rec[c]->data; | |
457 | |||
458 |
2/2✓ Branch 0 taken 6000 times.
✓ Branch 1 taken 30 times.
|
6030 | for (int y = 0; y < s->plane_height[c]; y++) { |
459 |
2/2✓ Branch 0 taken 1500000 times.
✓ Branch 1 taken 6000 times.
|
1506000 | for (int x = 0; x < s->plane_width[c]; x++) { |
460 | 1500000 | porg[c][y * o + x] = (int16_t) master->data[c][y * m + x]; | |
461 | 1500000 | prec[c][y * o + x] = (int16_t) ref->data[c][y * r + x]; | |
462 | } | ||
463 | } | ||
464 | } | ||
465 | } else { /* 10, 12, 14 bit */ | ||
466 | ✗ | for (c = 0; c < s->num_comps; c++) { | |
467 | ✗ | porg[c] = (int16_t *) master->data[c]; | |
468 | ✗ | prec[c] = (int16_t *) ref->data[c]; | |
469 | } | ||
470 | } | ||
471 | |||
472 | /* extended perceptually weighted peak signal-to-noise ratio (XPSNR) value */ | ||
473 | 10 | ret_value = get_wsse(ctx, (int16_t **) &porg, (int16_t **) &porg_m1, (int16_t **) &porg_m2, | |
474 | (int16_t **) &prec, wsse64); | ||
475 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if ( ret_value < 0 ) |
476 | ✗ | return ret_value; /* an error here means something went wrong earlier! */ | |
477 | |||
478 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 10 times.
|
40 | for (c = 0; c < s->num_comps; c++) { |
479 | 30 | const double sqrt_wsse = sqrt((double) wsse64[c]); | |
480 | |||
481 | 60 | cur_xpsnr[c] = get_avg_xpsnr (sqrt_wsse, INFINITY, | |
482 | 30 | s->plane_width[c], s->plane_height[c], | |
483 | s->max_error_64, 1 /* single frame */); | ||
484 | 30 | s->sum_wdist[c] += sqrt_wsse; | |
485 | 30 | s->sum_xpsnr[c] += cur_xpsnr[c]; | |
486 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
30 | s->and_is_inf[c] &= isinf(cur_xpsnr[c]); |
487 | } | ||
488 | 10 | s->num_frames_64++; | |
489 | |||
490 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 10 times.
|
40 | for (int j = 0; j < s->num_comps; j++) { |
491 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
|
30 | int c = s->is_rgb ? s->rgba_map[j] : j; |
492 | 30 | set_meta(metadata, "lavfi.xpsnr.xpsnr.", s->comps[j], cur_xpsnr[c]); | |
493 | } | ||
494 | |||
495 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (s->stats_file) { /* print out frame- and component-wise XPSNR averages */ |
496 | ✗ | fprintf(s->stats_file, "n: %4"PRId64"", s->num_frames_64); | |
497 | |||
498 | ✗ | for (c = 0; c < s->num_comps; c++) | |
499 | ✗ | fprintf(s->stats_file, " XPSNR %c: %3.4f", s->comps[c], cur_xpsnr[c]); | |
500 | ✗ | fprintf(s->stats_file, "\n"); | |
501 | } | ||
502 | |||
503 | 10 | return ff_filter_frame(ctx->outputs[0], master); | |
504 | } | ||
505 | |||
506 | 4 | static av_cold int init(AVFilterContext *ctx) | |
507 | { | ||
508 | 4 | XPSNRContext *const s = ctx->priv; | |
509 | int c; | ||
510 | |||
511 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (s->stats_file_str) { |
512 | ✗ | if (!strcmp(s->stats_file_str, "-")) /* no stats file, so use stdout */ | |
513 | ✗ | s->stats_file = stdout; | |
514 | else { | ||
515 | ✗ | s->stats_file = avpriv_fopen_utf8(s->stats_file_str, "w"); | |
516 | |||
517 | ✗ | if (!s->stats_file) { | |
518 | ✗ | const int err = AVERROR(errno); | |
519 | char buf[128]; | ||
520 | |||
521 | ✗ | av_strerror(err, buf, sizeof(buf)); | |
522 | ✗ | av_log(ctx, AV_LOG_ERROR, "Could not open statistics file %s: %s\n", s->stats_file_str, buf); | |
523 | ✗ | return err; | |
524 | } | ||
525 | } | ||
526 | } | ||
527 | |||
528 | 4 | s->sse_luma = NULL; | |
529 | 4 | s->weights = NULL; | |
530 | |||
531 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
|
16 | for (c = 0; c < 3; c++) { /* initialize XPSNR data of each color component */ |
532 | 12 | s->buf_org [c] = NULL; | |
533 | 12 | s->buf_org_m1[c] = NULL; | |
534 | 12 | s->buf_org_m2[c] = NULL; | |
535 | 12 | s->buf_rec [c] = NULL; | |
536 | 12 | s->sum_wdist [c] = 0.0; | |
537 | 12 | s->sum_xpsnr [c] = 0.0; | |
538 | 12 | s->and_is_inf[c] = 1; | |
539 | } | ||
540 | |||
541 | 4 | s->fs.on_event = do_xpsnr; | |
542 | |||
543 | 4 | return 0; | |
544 | } | ||
545 | |||
546 | static const enum AVPixelFormat xpsnr_formats[] = { | ||
547 | AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16, | ||
548 | #define PF_NOALPHA(suf) AV_PIX_FMT_YUV420##suf, AV_PIX_FMT_YUV422##suf, AV_PIX_FMT_YUV444##suf | ||
549 | #define PF_ALPHA(suf) AV_PIX_FMT_YUVA420##suf, AV_PIX_FMT_YUVA422##suf, AV_PIX_FMT_YUVA444##suf | ||
550 | #define PF(suf) PF_NOALPHA(suf), PF_ALPHA(suf) | ||
551 | PF(P), PF(P9), PF(P10), PF_NOALPHA(P12), PF_NOALPHA(P14), PF(P16), | ||
552 | AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, | ||
553 | AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, | ||
554 | AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P, | ||
555 | AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, | ||
556 | AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16, | ||
557 | AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16, | ||
558 | AV_PIX_FMT_NONE | ||
559 | }; | ||
560 | |||
561 | 2 | static int config_input_ref(AVFilterLink *inlink) | |
562 | { | ||
563 | 2 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); | |
564 | 2 | AVFilterContext *ctx = inlink->dst; | |
565 | 2 | XPSNRContext *const s = ctx->priv; | |
566 | 2 | FilterLink *il = ff_filter_link(inlink); | |
567 | |||
568 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if ((ctx->inputs[0]->w != ctx->inputs[1]->w) || |
569 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | (ctx->inputs[0]->h != ctx->inputs[1]->h)) { |
570 | ✗ | av_log(ctx, AV_LOG_ERROR, "Width and height of the input videos must match.\n"); | |
571 | ✗ | return AVERROR(EINVAL); | |
572 | } | ||
573 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ctx->inputs[0]->format != ctx->inputs[1]->format) { |
574 | ✗ | av_log(ctx, AV_LOG_ERROR, "The input videos must be of the same pixel format.\n"); | |
575 | ✗ | return AVERROR(EINVAL); | |
576 | } | ||
577 | |||
578 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | s->bpp = (desc->comp[0].depth <= 8 ? 1 : 2); |
579 | 2 | s->depth = desc->comp[0].depth; | |
580 | 2 | s->max_error_64 = (1 << s->depth) - 1; /* conventional limit */ | |
581 | 2 | s->max_error_64 *= s->max_error_64; | |
582 | |||
583 | 2 | s->frame_rate = il->frame_rate.num / il->frame_rate.den; | |
584 | |||
585 | 2 | s->num_comps = (desc->nb_components > 3 ? 3 : desc->nb_components); | |
586 | |||
587 | 2 | s->is_rgb = (ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0); | |
588 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | s->comps[0] = (s->is_rgb ? 'r' : 'y'); |
589 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | s->comps[1] = (s->is_rgb ? 'g' : 'u'); |
590 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | s->comps[2] = (s->is_rgb ? 'b' : 'v'); |
591 | 2 | s->comps[3] = 'a'; | |
592 | |||
593 | 2 | s->plane_width [1] = s->plane_width [2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w); | |
594 | 2 | s->plane_width [0] = s->plane_width [3] = inlink->w; | |
595 | 2 | s->plane_height[1] = s->plane_height[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); | |
596 | 2 | s->plane_height[0] = s->plane_height[3] = inlink->h; | |
597 | |||
598 | /* XPSNR always operates with 16-bit internal precision */ | ||
599 | 2 | ff_psnr_init(&s->pdsp, 15); | |
600 | 2 | s->dsp.highds_func = highds; /* initialize filtering methods */ | |
601 | 2 | s->dsp.diff1st_func = diff1st; | |
602 | 2 | s->dsp.diff2nd_func = diff2nd; | |
603 | |||
604 | 2 | return 0; | |
605 | } | ||
606 | |||
607 | 2 | static int config_output(AVFilterLink *outlink) | |
608 | { | ||
609 | 2 | AVFilterContext *ctx = outlink->src; | |
610 | 2 | XPSNRContext *s = ctx->priv; | |
611 | 2 | AVFilterLink *mainlink = ctx->inputs[0]; | |
612 | 2 | FilterLink *il = ff_filter_link(mainlink); | |
613 | 2 | FilterLink *ol = ff_filter_link(outlink); | |
614 | int ret; | ||
615 | |||
616 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if ((ret = ff_framesync_init_dualinput(&s->fs, ctx)) < 0) |
617 | ✗ | return ret; | |
618 | |||
619 | 2 | outlink->w = mainlink->w; | |
620 | 2 | outlink->h = mainlink->h; | |
621 | 2 | outlink->time_base = mainlink->time_base; | |
622 | 2 | outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio; | |
623 | 2 | ol->frame_rate = il->frame_rate; | |
624 | |||
625 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if ((ret = ff_framesync_configure(&s->fs)) < 0) |
626 | ✗ | return ret; | |
627 | |||
628 | 2 | outlink->time_base = s->fs.time_base; | |
629 | |||
630 |
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) || |
631 | 2 | av_cmp_q(ctx->inputs[1]->time_base, outlink->time_base)) | |
632 | ✗ | 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", | |
633 | mainlink->time_base.num, mainlink->time_base.den, | ||
634 | ✗ | ctx->inputs[1]->time_base.num, ctx->inputs[1]->time_base.den); | |
635 | |||
636 | 2 | return 0; | |
637 | } | ||
638 | |||
639 | 30 | static int activate(AVFilterContext *ctx) | |
640 | { | ||
641 | 30 | XPSNRContext *s = ctx->priv; | |
642 | |||
643 | 30 | return ff_framesync_activate(&s->fs); | |
644 | } | ||
645 | |||
646 | 4 | static av_cold void uninit(AVFilterContext *ctx) | |
647 | { | ||
648 | 4 | XPSNRContext *const s = ctx->priv; | |
649 | int c; | ||
650 | |||
651 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if (s->num_frames_64 > 0) { /* print out overall component-wise mean XPSNR */ |
652 | 2 | const double xpsnr_luma = get_avg_xpsnr(s->sum_wdist[0], s->sum_xpsnr[0], | |
653 | 2 | s->plane_width[0], s->plane_height[0], | |
654 | s->max_error_64, s->num_frames_64); | ||
655 | 2 | double xpsnr_min = xpsnr_luma; | |
656 | |||
657 | /* luma */ | ||
658 | 2 | av_log(ctx, AV_LOG_INFO, "XPSNR %c: %3.4f", s->comps[0], xpsnr_luma); | |
659 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (s->stats_file) { |
660 | ✗ | fprintf(s->stats_file, "\nXPSNR average, %"PRId64" frames", s->num_frames_64); | |
661 | ✗ | fprintf(s->stats_file, " %c: %3.4f", s->comps[0], xpsnr_luma); | |
662 | } | ||
663 | /* chroma */ | ||
664 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
|
6 | for (c = 1; c < s->num_comps; c++) { |
665 | 4 | const double xpsnr_chroma = get_avg_xpsnr(s->sum_wdist[c], s->sum_xpsnr[c], | |
666 | 4 | s->plane_width[c], s->plane_height[c], | |
667 | s->max_error_64, s->num_frames_64); | ||
668 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
|
4 | if (xpsnr_min > xpsnr_chroma) |
669 | 3 | xpsnr_min = xpsnr_chroma; | |
670 | |||
671 | 4 | av_log(ctx, AV_LOG_INFO, " %c: %3.4f", s->comps[c], xpsnr_chroma); | |
672 |
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) |
673 | ✗ | fprintf(s->stats_file, " %c: %3.4f", s->comps[c], xpsnr_chroma); | |
674 | } | ||
675 | /* print out line break, and minimum XPSNR across the color components */ | ||
676 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (s->num_comps > 1) { |
677 | 2 | av_log(ctx, AV_LOG_INFO, " (minimum: %3.4f)\n", xpsnr_min); | |
678 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
2 | if (s->stats_file && s->stats_file != stdout) |
679 | ✗ | fprintf(s->stats_file, " (minimum: %3.4f)\n", xpsnr_min); | |
680 | } else { | ||
681 | ✗ | av_log(ctx, AV_LOG_INFO, "\n"); | |
682 | ✗ | if (s->stats_file && s->stats_file != stdout) | |
683 | ✗ | fprintf(s->stats_file, "\n"); | |
684 | } | ||
685 | } | ||
686 | |||
687 | 4 | ff_framesync_uninit(&s->fs); /* free temporary picture or block buf memory */ | |
688 | |||
689 |
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) |
690 | ✗ | fclose(s->stats_file); | |
691 | |||
692 | 4 | av_freep(&s->sse_luma); | |
693 | 4 | av_freep(&s->weights ); | |
694 | |||
695 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4 times.
|
10 | for (c = 0; c < s->num_comps; c++) { /* free extra temporal org buf memory */ |
696 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
|
6 | if(s->buf_org_m1[c]) |
697 | 2 | av_freep(s->buf_org_m1[c]); | |
698 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
|
6 | if(s->buf_org_m2[c]) |
699 | 2 | av_freep(s->buf_org_m2[c]); | |
700 | } | ||
701 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if (s->bpp == 1) { /* 8 bit */ |
702 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
|
8 | for (c = 0; c < s->num_comps; c++) { /* and org/rec picture buf memory */ |
703 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
|
6 | if(s->buf_org_m2[c]) |
704 | 2 | av_freep(s->buf_org[c]); | |
705 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if(s->buf_rec[c]) |
706 | 6 | av_freep(s->buf_rec[c]); | |
707 | } | ||
708 | } | ||
709 | 4 | } | |
710 | |||
711 | static const AVFilterPad xpsnr_inputs[] = { | ||
712 | { | ||
713 | .name = "main", | ||
714 | .type = AVMEDIA_TYPE_VIDEO, | ||
715 | }, { | ||
716 | .name = "reference", | ||
717 | .type = AVMEDIA_TYPE_VIDEO, | ||
718 | .config_props = config_input_ref, | ||
719 | } | ||
720 | }; | ||
721 | |||
722 | static const AVFilterPad xpsnr_outputs[] = { | ||
723 | { | ||
724 | .name = "default", | ||
725 | .type = AVMEDIA_TYPE_VIDEO, | ||
726 | .config_props = config_output, | ||
727 | } | ||
728 | }; | ||
729 | |||
730 | const AVFilter ff_vf_xpsnr = { | ||
731 | .name = "xpsnr", | ||
732 | .description = NULL_IF_CONFIG_SMALL("Calculate the extended perceptually weighted peak signal-to-noise ratio (XPSNR) between two video streams."), | ||
733 | .preinit = xpsnr_framesync_preinit, | ||
734 | .init = init, | ||
735 | .uninit = uninit, | ||
736 | .activate = activate, | ||
737 | .priv_size = sizeof(XPSNRContext), | ||
738 | .priv_class = &xpsnr_class, | ||
739 | FILTER_INPUTS (xpsnr_inputs), | ||
740 | FILTER_OUTPUTS(xpsnr_outputs), | ||
741 | FILTER_PIXFMTS_ARRAY(xpsnr_formats), | ||
742 | .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_METADATA_ONLY | ||
743 | }; | ||
744 |