blob: 1201b2cd7c3a84abdddad31238162da95659be81
1 | /* |
2 | * Copyright (c) 2011 Roger Pau Monné <roger.pau@entel.upc.edu> |
3 | * Copyright (c) 2011 Stefano Sabatini |
4 | * Copyright (c) 2013 Paul B Mahol |
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 | * Caculate the PSNR between two input videos. |
26 | */ |
27 | |
28 | #include "libavutil/avstring.h" |
29 | #include "libavutil/opt.h" |
30 | #include "libavutil/pixdesc.h" |
31 | #include "avfilter.h" |
32 | #include "dualinput.h" |
33 | #include "drawutils.h" |
34 | #include "formats.h" |
35 | #include "internal.h" |
36 | #include "psnr.h" |
37 | #include "video.h" |
38 | |
39 | typedef struct PSNRContext { |
40 | const AVClass *class; |
41 | FFDualInputContext dinput; |
42 | double mse, min_mse, max_mse, mse_comp[4]; |
43 | uint64_t nb_frames; |
44 | FILE *stats_file; |
45 | char *stats_file_str; |
46 | int stats_version; |
47 | int stats_header_written; |
48 | int stats_add_max; |
49 | int max[4], average_max; |
50 | int is_rgb; |
51 | uint8_t rgba_map[4]; |
52 | char comps[4]; |
53 | int nb_components; |
54 | int planewidth[4]; |
55 | int planeheight[4]; |
56 | double planeweight[4]; |
57 | PSNRDSPContext dsp; |
58 | } PSNRContext; |
59 | |
60 | #define OFFSET(x) offsetof(PSNRContext, x) |
61 | #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM |
62 | |
63 | static const AVOption psnr_options[] = { |
64 | {"stats_file", "Set file where to store per-frame difference information", OFFSET(stats_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS }, |
65 | {"f", "Set file where to store per-frame difference information", OFFSET(stats_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS }, |
66 | {"stats_version", "Set the format version for the stats file.", OFFSET(stats_version), AV_OPT_TYPE_INT, {.i64=1}, 1, 2, FLAGS }, |
67 | {"output_max", "Add raw stats (max values) to the output log.", OFFSET(stats_add_max), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS}, |
68 | { NULL } |
69 | }; |
70 | |
71 | AVFILTER_DEFINE_CLASS(psnr); |
72 | |
73 | static inline unsigned pow_2(unsigned base) |
74 | { |
75 | return base*base; |
76 | } |
77 | |
78 | static inline double get_psnr(double mse, uint64_t nb_frames, int max) |
79 | { |
80 | return 10.0 * log10(pow_2(max) / (mse / nb_frames)); |
81 | } |
82 | |
83 | static uint64_t sse_line_8bit(const uint8_t *main_line, const uint8_t *ref_line, int outw) |
84 | { |
85 | int j; |
86 | unsigned m2 = 0; |
87 | |
88 | for (j = 0; j < outw; j++) |
89 | m2 += pow_2(main_line[j] - ref_line[j]); |
90 | |
91 | return m2; |
92 | } |
93 | |
94 | static uint64_t sse_line_16bit(const uint8_t *_main_line, const uint8_t *_ref_line, int outw) |
95 | { |
96 | int j; |
97 | uint64_t m2 = 0; |
98 | const uint16_t *main_line = (const uint16_t *) _main_line; |
99 | const uint16_t *ref_line = (const uint16_t *) _ref_line; |
100 | |
101 | for (j = 0; j < outw; j++) |
102 | m2 += pow_2(main_line[j] - ref_line[j]); |
103 | |
104 | return m2; |
105 | } |
106 | |
107 | static inline |
108 | void compute_images_mse(PSNRContext *s, |
109 | const uint8_t *main_data[4], const int main_linesizes[4], |
110 | const uint8_t *ref_data[4], const int ref_linesizes[4], |
111 | int w, int h, double mse[4]) |
112 | { |
113 | int i, c; |
114 | |
115 | for (c = 0; c < s->nb_components; c++) { |
116 | const int outw = s->planewidth[c]; |
117 | const int outh = s->planeheight[c]; |
118 | const uint8_t *main_line = main_data[c]; |
119 | const uint8_t *ref_line = ref_data[c]; |
120 | const int ref_linesize = ref_linesizes[c]; |
121 | const int main_linesize = main_linesizes[c]; |
122 | uint64_t m = 0; |
123 | for (i = 0; i < outh; i++) { |
124 | m += s->dsp.sse_line(main_line, ref_line, outw); |
125 | ref_line += ref_linesize; |
126 | main_line += main_linesize; |
127 | } |
128 | mse[c] = m / (double)(outw * outh); |
129 | } |
130 | } |
131 | |
132 | static void set_meta(AVDictionary **metadata, const char *key, char comp, float d) |
133 | { |
134 | char value[128]; |
135 | snprintf(value, sizeof(value), "%0.2f", d); |
136 | if (comp) { |
137 | char key2[128]; |
138 | snprintf(key2, sizeof(key2), "%s%c", key, comp); |
139 | av_dict_set(metadata, key2, value, 0); |
140 | } else { |
141 | av_dict_set(metadata, key, value, 0); |
142 | } |
143 | } |
144 | |
145 | static AVFrame *do_psnr(AVFilterContext *ctx, AVFrame *main, |
146 | const AVFrame *ref) |
147 | { |
148 | PSNRContext *s = ctx->priv; |
149 | double comp_mse[4], mse = 0; |
150 | int j, c; |
151 | AVDictionary **metadata = avpriv_frame_get_metadatap(main); |
152 | |
153 | compute_images_mse(s, (const uint8_t **)main->data, main->linesize, |
154 | (const uint8_t **)ref->data, ref->linesize, |
155 | main->width, main->height, comp_mse); |
156 | |
157 | for (j = 0; j < s->nb_components; j++) |
158 | mse += comp_mse[j] * s->planeweight[j]; |
159 | |
160 | s->min_mse = FFMIN(s->min_mse, mse); |
161 | s->max_mse = FFMAX(s->max_mse, mse); |
162 | |
163 | s->mse += mse; |
164 | for (j = 0; j < s->nb_components; j++) |
165 | s->mse_comp[j] += comp_mse[j]; |
166 | s->nb_frames++; |
167 | |
168 | for (j = 0; j < s->nb_components; j++) { |
169 | c = s->is_rgb ? s->rgba_map[j] : j; |
170 | set_meta(metadata, "lavfi.psnr.mse.", s->comps[j], comp_mse[c]); |
171 | set_meta(metadata, "lavfi.psnr.psnr.", s->comps[j], get_psnr(comp_mse[c], 1, s->max[c])); |
172 | } |
173 | set_meta(metadata, "lavfi.psnr.mse_avg", 0, mse); |
174 | set_meta(metadata, "lavfi.psnr.psnr_avg", 0, get_psnr(mse, 1, s->average_max)); |
175 | |
176 | if (s->stats_file) { |
177 | if (s->stats_version == 2 && !s->stats_header_written) { |
178 | fprintf(s->stats_file, "psnr_log_version:2 fields:n"); |
179 | fprintf(s->stats_file, ",mse_avg"); |
180 | for (j = 0; j < s->nb_components; j++) { |
181 | fprintf(s->stats_file, ",mse_%c", s->comps[j]); |
182 | } |
183 | fprintf(s->stats_file, ",psnr_avg"); |
184 | for (j = 0; j < s->nb_components; j++) { |
185 | fprintf(s->stats_file, ",psnr_%c", s->comps[j]); |
186 | } |
187 | if (s->stats_add_max) { |
188 | fprintf(s->stats_file, ",max_avg"); |
189 | for (j = 0; j < s->nb_components; j++) { |
190 | fprintf(s->stats_file, ",max_%c", s->comps[j]); |
191 | } |
192 | } |
193 | fprintf(s->stats_file, "\n"); |
194 | s->stats_header_written = 1; |
195 | } |
196 | fprintf(s->stats_file, "n:%"PRId64" mse_avg:%0.2f ", s->nb_frames, mse); |
197 | for (j = 0; j < s->nb_components; j++) { |
198 | c = s->is_rgb ? s->rgba_map[j] : j; |
199 | fprintf(s->stats_file, "mse_%c:%0.2f ", s->comps[j], comp_mse[c]); |
200 | } |
201 | fprintf(s->stats_file, "psnr_avg:%0.2f ", get_psnr(mse, 1, s->average_max)); |
202 | for (j = 0; j < s->nb_components; j++) { |
203 | c = s->is_rgb ? s->rgba_map[j] : j; |
204 | fprintf(s->stats_file, "psnr_%c:%0.2f ", s->comps[j], |
205 | get_psnr(comp_mse[c], 1, s->max[c])); |
206 | } |
207 | if (s->stats_version == 2 && s->stats_add_max) { |
208 | fprintf(s->stats_file, "max_avg:%d ", s->average_max); |
209 | for (j = 0; j < s->nb_components; j++) { |
210 | c = s->is_rgb ? s->rgba_map[j] : j; |
211 | fprintf(s->stats_file, "max_%c:%d ", s->comps[j], s->max[c]); |
212 | } |
213 | } |
214 | fprintf(s->stats_file, "\n"); |
215 | } |
216 | |
217 | return main; |
218 | } |
219 | |
220 | static av_cold int init(AVFilterContext *ctx) |
221 | { |
222 | PSNRContext *s = ctx->priv; |
223 | |
224 | s->min_mse = +INFINITY; |
225 | s->max_mse = -INFINITY; |
226 | |
227 | if (s->stats_file_str) { |
228 | if (s->stats_version < 2 && s->stats_add_max) { |
229 | av_log(ctx, AV_LOG_ERROR, |
230 | "stats_add_max was specified but stats_version < 2.\n" ); |
231 | return AVERROR(EINVAL); |
232 | } |
233 | if (!strcmp(s->stats_file_str, "-")) { |
234 | s->stats_file = stdout; |
235 | } else { |
236 | s->stats_file = fopen(s->stats_file_str, "w"); |
237 | if (!s->stats_file) { |
238 | int err = AVERROR(errno); |
239 | char buf[128]; |
240 | av_strerror(err, buf, sizeof(buf)); |
241 | av_log(ctx, AV_LOG_ERROR, "Could not open stats file %s: %s\n", |
242 | s->stats_file_str, buf); |
243 | return err; |
244 | } |
245 | } |
246 | } |
247 | |
248 | s->dinput.process = do_psnr; |
249 | return 0; |
250 | } |
251 | |
252 | static int query_formats(AVFilterContext *ctx) |
253 | { |
254 | static const enum AVPixelFormat pix_fmts[] = { |
255 | AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY16, |
256 | #define PF_NOALPHA(suf) AV_PIX_FMT_YUV420##suf, AV_PIX_FMT_YUV422##suf, AV_PIX_FMT_YUV444##suf |
257 | #define PF_ALPHA(suf) AV_PIX_FMT_YUVA420##suf, AV_PIX_FMT_YUVA422##suf, AV_PIX_FMT_YUVA444##suf |
258 | #define PF(suf) PF_NOALPHA(suf), PF_ALPHA(suf) |
259 | PF(P), PF(P9), PF(P10), PF_NOALPHA(P12), PF_NOALPHA(P14), PF(P16), |
260 | AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, |
261 | AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, |
262 | AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P, |
263 | AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, |
264 | AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16, |
265 | AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP16, |
266 | AV_PIX_FMT_NONE |
267 | }; |
268 | |
269 | AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts); |
270 | if (!fmts_list) |
271 | return AVERROR(ENOMEM); |
272 | return ff_set_common_formats(ctx, fmts_list); |
273 | } |
274 | |
275 | static int config_input_ref(AVFilterLink *inlink) |
276 | { |
277 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); |
278 | AVFilterContext *ctx = inlink->dst; |
279 | PSNRContext *s = ctx->priv; |
280 | double average_max; |
281 | unsigned sum; |
282 | int j; |
283 | |
284 | s->nb_components = desc->nb_components; |
285 | if (ctx->inputs[0]->w != ctx->inputs[1]->w || |
286 | ctx->inputs[0]->h != ctx->inputs[1]->h) { |
287 | av_log(ctx, AV_LOG_ERROR, "Width and height of input videos must be same.\n"); |
288 | return AVERROR(EINVAL); |
289 | } |
290 | if (ctx->inputs[0]->format != ctx->inputs[1]->format) { |
291 | av_log(ctx, AV_LOG_ERROR, "Inputs must be of same pixel format.\n"); |
292 | return AVERROR(EINVAL); |
293 | } |
294 | |
295 | s->max[0] = (1 << desc->comp[0].depth) - 1; |
296 | s->max[1] = (1 << desc->comp[1].depth) - 1; |
297 | s->max[2] = (1 << desc->comp[2].depth) - 1; |
298 | s->max[3] = (1 << desc->comp[3].depth) - 1; |
299 | |
300 | s->is_rgb = ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0; |
301 | s->comps[0] = s->is_rgb ? 'r' : 'y' ; |
302 | s->comps[1] = s->is_rgb ? 'g' : 'u' ; |
303 | s->comps[2] = s->is_rgb ? 'b' : 'v' ; |
304 | s->comps[3] = 'a'; |
305 | |
306 | s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); |
307 | s->planeheight[0] = s->planeheight[3] = inlink->h; |
308 | s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w); |
309 | s->planewidth[0] = s->planewidth[3] = inlink->w; |
310 | sum = 0; |
311 | for (j = 0; j < s->nb_components; j++) |
312 | sum += s->planeheight[j] * s->planewidth[j]; |
313 | average_max = 0; |
314 | for (j = 0; j < s->nb_components; j++) { |
315 | s->planeweight[j] = (double) s->planeheight[j] * s->planewidth[j] / sum; |
316 | average_max += s->max[j] * s->planeweight[j]; |
317 | } |
318 | s->average_max = lrint(average_max); |
319 | |
320 | s->dsp.sse_line = desc->comp[0].depth > 8 ? sse_line_16bit : sse_line_8bit; |
321 | if (ARCH_X86) |
322 | ff_psnr_init_x86(&s->dsp, desc->comp[0].depth); |
323 | |
324 | return 0; |
325 | } |
326 | |
327 | static int config_output(AVFilterLink *outlink) |
328 | { |
329 | AVFilterContext *ctx = outlink->src; |
330 | PSNRContext *s = ctx->priv; |
331 | AVFilterLink *mainlink = ctx->inputs[0]; |
332 | int ret; |
333 | |
334 | outlink->w = mainlink->w; |
335 | outlink->h = mainlink->h; |
336 | outlink->time_base = mainlink->time_base; |
337 | outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio; |
338 | outlink->frame_rate = mainlink->frame_rate; |
339 | if ((ret = ff_dualinput_init(ctx, &s->dinput)) < 0) |
340 | return ret; |
341 | |
342 | return 0; |
343 | } |
344 | |
345 | static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref) |
346 | { |
347 | PSNRContext *s = inlink->dst->priv; |
348 | return ff_dualinput_filter_frame(&s->dinput, inlink, inpicref); |
349 | } |
350 | |
351 | static int request_frame(AVFilterLink *outlink) |
352 | { |
353 | PSNRContext *s = outlink->src->priv; |
354 | return ff_dualinput_request_frame(&s->dinput, outlink); |
355 | } |
356 | |
357 | static av_cold void uninit(AVFilterContext *ctx) |
358 | { |
359 | PSNRContext *s = ctx->priv; |
360 | |
361 | if (s->nb_frames > 0) { |
362 | int j; |
363 | char buf[256]; |
364 | |
365 | buf[0] = 0; |
366 | for (j = 0; j < s->nb_components; j++) { |
367 | int c = s->is_rgb ? s->rgba_map[j] : j; |
368 | av_strlcatf(buf, sizeof(buf), " %c:%f", s->comps[j], |
369 | get_psnr(s->mse_comp[c], s->nb_frames, s->max[c])); |
370 | } |
371 | av_log(ctx, AV_LOG_INFO, "PSNR%s average:%f min:%f max:%f\n", |
372 | buf, |
373 | get_psnr(s->mse, s->nb_frames, s->average_max), |
374 | get_psnr(s->max_mse, 1, s->average_max), |
375 | get_psnr(s->min_mse, 1, s->average_max)); |
376 | } |
377 | |
378 | ff_dualinput_uninit(&s->dinput); |
379 | |
380 | if (s->stats_file && s->stats_file != stdout) |
381 | fclose(s->stats_file); |
382 | } |
383 | |
384 | static const AVFilterPad psnr_inputs[] = { |
385 | { |
386 | .name = "main", |
387 | .type = AVMEDIA_TYPE_VIDEO, |
388 | .filter_frame = filter_frame, |
389 | },{ |
390 | .name = "reference", |
391 | .type = AVMEDIA_TYPE_VIDEO, |
392 | .filter_frame = filter_frame, |
393 | .config_props = config_input_ref, |
394 | }, |
395 | { NULL } |
396 | }; |
397 | |
398 | static const AVFilterPad psnr_outputs[] = { |
399 | { |
400 | .name = "default", |
401 | .type = AVMEDIA_TYPE_VIDEO, |
402 | .config_props = config_output, |
403 | .request_frame = request_frame, |
404 | }, |
405 | { NULL } |
406 | }; |
407 | |
408 | AVFilter ff_vf_psnr = { |
409 | .name = "psnr", |
410 | .description = NULL_IF_CONFIG_SMALL("Calculate the PSNR between two video streams."), |
411 | .init = init, |
412 | .uninit = uninit, |
413 | .query_formats = query_formats, |
414 | .priv_size = sizeof(PSNRContext), |
415 | .priv_class = &psnr_class, |
416 | .inputs = psnr_inputs, |
417 | .outputs = psnr_outputs, |
418 | }; |
419 |