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