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