Browse code

lavfi: add psnr filter

Signed-off-by: Paul B Mahol <onemda@gmail.com>

Paul B Mahol authored on 2013/07/06 09:21:12
Showing 6 changed files
... ...
@@ -69,6 +69,7 @@ version <next>:
69 69
 - rotate filter
70 70
 - spp filter ported from libmpcodecs
71 71
 - libgme support
72
+- psnr filter
72 73
 
73 74
 
74 75
 version 1.2:
... ...
@@ -5837,6 +5837,79 @@ pp=hb|y/vb|a
5837 5837
 @end example
5838 5838
 @end itemize
5839 5839
 
5840
+@section psnr
5841
+
5842
+Obtain the average, maximum and minimum PSNR (Peak Signal to Noise
5843
+Ratio) between two input videos.
5844
+
5845
+This filter takes in input two input videos, the first input is
5846
+considered the "main" source and is passed unchanged to the
5847
+output. The second input is used as a "reference" video for computing
5848
+the PSNR.
5849
+
5850
+Both video inputs must have the same resolution and pixel format for
5851
+this filter to work correctly. Also it assumes that both inputs
5852
+have the same number of frames, which are compared one by one.
5853
+
5854
+The obtained average PSNR is printed through the logging system.
5855
+
5856
+The filter stores the accumulated MSE (mean squared error) of each
5857
+frame, and at the end of the processing it is averaged across all frames
5858
+equally, and the following formula is applied to obtain the PSNR:
5859
+
5860
+@example
5861
+PSNR = 10*log10(MAX^2/MSE)
5862
+@end example
5863
+
5864
+Where MAX is the average of the maximum values of each component of the
5865
+image.
5866
+
5867
+The filter accepts parameters as a list of @var{key}=@var{value} pairs,
5868
+separated by ":".
5869
+
5870
+The description of the accepted parameters follows.
5871
+
5872
+@table @option
5873
+@item stats_file, f
5874
+If specified the filter will use the named file to save the PSNR of
5875
+each individual frame.
5876
+@end table
5877
+
5878
+The file printed if @var{stats_file} is selected, contains a sequence of
5879
+key/value pairs of the form @var{key}:@var{value} for each compared
5880
+couple of frames.
5881
+
5882
+The shown line contains .
5883
+
5884
+A description of each shown parameter follows:
5885
+
5886
+@table @option
5887
+@item n
5888
+sequential number of the input frame, starting from 1
5889
+
5890
+@item mse_average
5891
+Mean Square Error pixel-by-pixel average difference of the compared
5892
+frames, averaged over all the image components.
5893
+
5894
+@item mse_y, mse_u, mse_v, mse_r, mse_g, mse_g, mse_a
5895
+Mean Square Error pixel-by-pixel average difference of the compared
5896
+frames for the component specified by the suffix.
5897
+
5898
+@item psnr_y, psnr_u, psnr_v, psnr_r, psnr_g, psnr_g, psnr_a
5899
+Peak Signal to Noise ratio of the compared frames for the component
5900
+specified by the suffix.
5901
+@end table
5902
+
5903
+For example:
5904
+@example
5905
+movie=ref_movie.mpg, setpts=PTS-STARTPTS [main];
5906
+[main][ref] psnr="stats_file=stats.log" [out]
5907
+@end example
5908
+
5909
+On this example the input file being processed is compared with the
5910
+reference file @file{ref_movie.mpg}. The PSNR of each individual frame
5911
+is stored in @file{stats.log}.
5912
+
5840 5913
 @section removelogo
5841 5914
 
5842 5915
 Suppress a TV station logo, using an image file to determine which
... ...
@@ -167,6 +167,7 @@ OBJS-$(CONFIG_PAD_FILTER)                    += vf_pad.o
167 167
 OBJS-$(CONFIG_PERMS_FILTER)                  += f_perms.o
168 168
 OBJS-$(CONFIG_PIXDESCTEST_FILTER)            += vf_pixdesctest.o
169 169
 OBJS-$(CONFIG_PP_FILTER)                     += vf_pp.o
170
+OBJS-$(CONFIG_PSNR_FILTER)                   += vf_psnr.o dualinput.o
170 171
 OBJS-$(CONFIG_REMOVELOGO_FILTER)             += bbox.o lswsutils.o lavfutils.o vf_removelogo.o
171 172
 OBJS-$(CONFIG_ROTATE_FILTER)                 += vf_rotate.o
172 173
 OBJS-$(CONFIG_SEPARATEFIELDS_FILTER)         += vf_separatefields.o
... ...
@@ -162,6 +162,7 @@ void avfilter_register_all(void)
162 162
     REGISTER_FILTER(PERMS,          perms,          vf);
163 163
     REGISTER_FILTER(PIXDESCTEST,    pixdesctest,    vf);
164 164
     REGISTER_FILTER(PP,             pp,             vf);
165
+    REGISTER_FILTER(PSNR,           psnr,           vf);
165 166
     REGISTER_FILTER(REMOVELOGO,     removelogo,     vf);
166 167
     REGISTER_FILTER(ROTATE,         rotate,         vf);
167 168
     REGISTER_FILTER(SAB,            sab,            vf);
... ...
@@ -30,8 +30,8 @@
30 30
 #include "libavutil/avutil.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR  3
33
-#define LIBAVFILTER_VERSION_MINOR  78
34
-#define LIBAVFILTER_VERSION_MICRO 103
33
+#define LIBAVFILTER_VERSION_MINOR  79
34
+#define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
37 37
                                                LIBAVFILTER_VERSION_MINOR, \
38 38
new file mode 100644
... ...
@@ -0,0 +1,324 @@
0
+/*
1

                
2
+ * Copyright (c) 2011 Stefano Sabatini
3
+ * Copyright (c) 2013 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
+/**
23
+ * @file
24
+ * Caculate the PSNR between two input videos.
25
+ */
26
+
27
+#include "libavutil/opt.h"
28
+#include "libavutil/pixdesc.h"
29
+#include "avfilter.h"
30
+#include "dualinput.h"
31
+#include "drawutils.h"
32
+#include "formats.h"
33
+#include "internal.h"
34
+#include "video.h"
35
+
36
+typedef struct PSNRContext {
37
+    const AVClass *class;
38
+    FFDualInputContext dinput;
39
+    double mse, min_mse, max_mse;
40
+    uint64_t nb_frames;
41
+    FILE *stats_file;
42
+    char *stats_file_str;
43
+    int max[4], average_max;
44
+    int is_rgb;
45
+    uint8_t rgba_map[4];
46
+    char comps[4];
47
+    const AVPixFmtDescriptor *desc;
48
+} PSNRContext;
49
+
50
+#define OFFSET(x) offsetof(PSNRContext, x)
51
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
52
+
53
+static const AVOption psnr_options[] = {
54
+    {"stats_file", "Set file where to store per-frame difference information", OFFSET(stats_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
55
+    {"f",          "Set file where to store per-frame difference information", OFFSET(stats_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
56
+    { NULL },
57
+};
58
+
59
+AVFILTER_DEFINE_CLASS(psnr);
60
+
61
+static inline int pow2(int base)
62
+{
63
+    return base*base;
64
+}
65
+
66
+static inline double get_psnr(double mse, uint64_t nb_frames, int max)
67
+{
68
+    return 10.0 * log(pow2(max) / (mse / nb_frames)) / log(10.0);
69
+}
70
+
71
+static inline
72
+void compute_images_mse(const uint8_t *main_data[4], const int main_linesizes[4],
73
+                        const uint8_t *ref_data[4], const int ref_linesizes[4],
74
+                        int w, int h, const AVPixFmtDescriptor *desc,
75
+                        double mse[4])
76
+{
77
+    int i, c, j;
78
+
79
+    for (c = 0; c < desc->nb_components; c++) {
80
+        int hsub = c == 1 || c == 2 ? desc->log2_chroma_w : 0;
81
+        int vsub = c == 1 || c == 2 ? desc->log2_chroma_h : 0;
82
+        const int outw = FF_CEIL_RSHIFT(w, hsub);
83
+        const int outh = FF_CEIL_RSHIFT(h, vsub);
84
+        const uint8_t *main_line = main_data[c];
85
+        const uint8_t *ref_line = ref_data[c];
86
+        const int ref_linesize = ref_linesizes[c];
87
+        const int main_linesize = main_linesizes[c];
88
+        int m = 0;
89
+
90
+        for (i = 0; i < outh; i++) {
91
+            for (j = 0; j < outw; j++)
92
+                m += pow2(main_line[j] - ref_line[j]);
93
+            ref_line += ref_linesize;
94
+            main_line += main_linesize;
95
+        }
96
+        mse[c] = m / (outw * outh);
97
+    }
98
+}
99
+
100
+#define SET_META(key, comp, value) \
101
+    snprintf(buf, sizeof(buf), "%0.2f", value);  \
102
+    av_dict_set(metadata, #key #comp, buf, 0); \
103
+
104
+static AVFrame *do_psnr(AVFilterContext *ctx, AVFrame *main,
105
+                        const AVFrame *ref)
106
+{
107
+    PSNRContext *s = ctx->priv;
108
+    double comp_mse[4], mse = 0;
109
+    char buf[32];
110
+    int j, c;
111
+    AVDictionary **metadata = avpriv_frame_get_metadatap(main);
112
+
113
+    compute_images_mse((const uint8_t **)main->data, main->linesize,
114
+                       (const uint8_t **)ref->data, ref->linesize,
115
+                       main->width, main->height, s->desc, comp_mse);
116
+
117
+    for (j = 0; j < s->desc->nb_components; j++)
118
+        mse += comp_mse[j];
119
+    mse /= s->desc->nb_components;
120
+
121
+    s->min_mse = FFMIN(s->min_mse, mse);
122
+    s->max_mse = FFMAX(s->max_mse, mse);
123
+
124
+    s->mse += mse;
125
+    s->nb_frames++;
126
+
127
+    for (j = 0; j < s->desc->nb_components; j++) {
128
+        c = s->is_rgb ? s->rgba_map[j] : j;
129
+        SET_META("lavfi.psnr.mse.", s->comps[j], comp_mse[c]);
130
+        SET_META("lavfi.psnr.mse_avg", "", mse);
131
+        SET_META("lavfi.psnr.s.", s->comps[j], get_psnr(comp_mse[c], 1, s->max[c]));
132
+        SET_META("lavfi.psnr.s_avg", "", get_psnr(mse, 1, s->average_max));
133
+    }
134
+
135
+    if (s->stats_file) {
136
+        fprintf(s->stats_file, "n:%"PRId64" mse_avg:%0.2f ", s->nb_frames, mse);
137
+        for (j = 0; j < s->desc->nb_components; j++) {
138
+            c = s->is_rgb ? s->rgba_map[j] : j;
139
+            fprintf(s->stats_file, "mse_%c:%0.2f ", s->comps[j], comp_mse[c]);
140
+        }
141
+        for (j = 0; j < s->desc->nb_components; j++) {
142
+            c = s->is_rgb ? s->rgba_map[j] : j;
143
+            fprintf(s->stats_file, "s%c:%0.2f ", s->comps[j],
144
+                    get_psnr(comp_mse[c], 1, s->max[c]));
145
+        }
146
+        fprintf(s->stats_file, "\n");
147
+    }
148
+
149
+    return main;
150
+}
151
+
152
+static av_cold int init(AVFilterContext *ctx)
153
+{
154
+    PSNRContext *s = ctx->priv;
155
+
156
+    s->min_mse = +INFINITY;
157
+    s->max_mse = -INFINITY;
158
+
159
+    if (s->stats_file_str) {
160
+        s->stats_file = fopen(s->stats_file_str, "w");
161
+        if (!s->stats_file) {
162
+            int err = AVERROR(errno);
163
+            char buf[128];
164
+            av_strerror(err, buf, sizeof(buf));
165
+            av_log(ctx, AV_LOG_ERROR, "Could not open stats file %s: %s\n",
166
+                   s->stats_file_str, buf);
167
+            return err;
168
+        }
169
+    }
170
+
171
+    s->dinput.process = do_psnr;
172
+    return 0;
173
+}
174
+
175
+static int query_formats(AVFilterContext *ctx)
176
+{
177
+    static const enum PixelFormat pix_fmts[] = {
178
+        AV_PIX_FMT_GRAY8,
179
+        AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
180
+        AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P,
181
+        AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
182
+        AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ422P,
183
+        AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P,
184
+        AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
185
+        AV_PIX_FMT_NONE
186
+    };
187
+
188
+    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
189
+    return 0;
190
+}
191
+
192
+static int config_input_ref(AVFilterLink *inlink)
193
+{
194
+    AVFilterContext *ctx  = inlink->dst;
195
+    PSNRContext *s = ctx->priv;
196
+    int j;
197
+
198
+    s->desc = av_pix_fmt_desc_get(inlink->format);
199
+    if (ctx->inputs[0]->w != ctx->inputs[1]->w ||
200
+        ctx->inputs[0]->h != ctx->inputs[1]->h) {
201
+        av_log(ctx, AV_LOG_ERROR, "Width and heigth of input videos must be same.\n");
202
+        return AVERROR(EINVAL);
203
+    }
204
+    if (ctx->inputs[0]->format != ctx->inputs[1]->format) {
205
+        av_log(ctx, AV_LOG_ERROR, "Inputs must be of same pixel format.\n");
206
+        return AVERROR(EINVAL);
207
+    }
208
+
209
+    switch (inlink->format) {
210
+    case AV_PIX_FMT_YUV410P:
211
+    case AV_PIX_FMT_YUV411P:
212
+    case AV_PIX_FMT_YUV420P:
213
+    case AV_PIX_FMT_YUV422P:
214
+    case AV_PIX_FMT_YUV440P:
215
+    case AV_PIX_FMT_YUV444P:
216
+    case AV_PIX_FMT_YUVA420P:
217
+    case AV_PIX_FMT_YUVA422P:
218
+    case AV_PIX_FMT_YUVA444P:
219
+        s->max[0] = 235;
220
+        s->max[3] = 255;
221
+        s->max[1] = s->max[2] = 240;
222
+        break;
223
+    default:
224
+        s->max[0] = s->max[1] = s->max[2] = s->max[3] = 255;
225
+    }
226
+
227
+    s->is_rgb = ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0;
228
+    s->comps[0] = s->is_rgb ? 'r' : 'y' ;
229
+    s->comps[1] = s->is_rgb ? 'g' : 'u' ;
230
+    s->comps[2] = s->is_rgb ? 'b' : 'v' ;
231
+    s->comps[3] = 'a';
232
+
233
+    for (j = 0; j < s->desc->nb_components; j++)
234
+        s->average_max += s->max[j];
235
+    s->average_max /= s->desc->nb_components;
236
+
237
+    return 0;
238
+}
239
+
240
+static int config_output(AVFilterLink *outlink)
241
+{
242
+    AVFilterContext *ctx = outlink->src;
243
+    AVFilterLink *mainlink = ctx->inputs[0];
244
+
245
+    outlink->w = mainlink->w;
246
+    outlink->h = mainlink->h;
247
+    outlink->time_base = mainlink->time_base;
248
+    outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
249
+    outlink->frame_rate = mainlink->frame_rate;
250
+
251
+    return 0;
252
+}
253
+
254
+static int filter_frame_main(AVFilterLink *inlink, AVFrame *inpicref)
255
+{
256
+    PSNRContext *s = inlink->dst->priv;
257
+    return ff_dualinput_filter_frame_main(&s->dinput, inlink, inpicref);
258
+}
259
+
260
+static int filter_frame_ref(AVFilterLink *inlink, AVFrame *inpicref)
261
+{
262
+    PSNRContext *s = inlink->dst->priv;
263
+    return ff_dualinput_filter_frame_second(&s->dinput, inlink, inpicref);
264
+}
265
+
266
+static int request_frame(AVFilterLink *outlink)
267
+{
268
+    PSNRContext *s = outlink->src->priv;
269
+    return ff_dualinput_request_frame(&s->dinput, outlink);
270
+}
271
+
272
+static av_cold void uninit(AVFilterContext *ctx)
273
+{
274
+    PSNRContext *s = ctx->priv;
275
+
276
+    if (s->nb_frames > 0) {
277
+        av_log(ctx, AV_LOG_INFO, "PSNR average:%0.2f min:%0.2f max:%0.2f\n",
278
+               get_psnr(s->mse, s->nb_frames, s->average_max),
279
+               get_psnr(s->max_mse, 1, s->average_max),
280
+               get_psnr(s->min_mse, 1, s->average_max));
281
+    }
282
+
283
+    ff_dualinput_uninit(&s->dinput);
284
+
285
+    if (s->stats_file)
286
+        fclose(s->stats_file);
287
+}
288
+
289
+static const AVFilterPad psnr_inputs[] = {
290
+    {
291
+        .name             = "main",
292
+        .type             = AVMEDIA_TYPE_VIDEO,
293
+        .filter_frame     = filter_frame_main,
294
+    },{
295
+        .name             = "reference",
296
+        .type             = AVMEDIA_TYPE_VIDEO,
297
+        .filter_frame     = filter_frame_ref,
298
+        .config_props     = config_input_ref,
299
+    },
300
+    { NULL }
301
+};
302
+
303
+static const AVFilterPad psnr_outputs[] = {
304
+    {
305
+        .name          = "default",
306
+        .type          = AVMEDIA_TYPE_VIDEO,
307
+        .config_props  = config_output,
308
+        .request_frame = request_frame,
309
+    },
310
+    { NULL }
311
+};
312
+
313
+AVFilter avfilter_vf_psnr = {
314
+    .name           = "psnr",
315
+    .description    = NULL_IF_CONFIG_SMALL("Calculate the PSNR between two video streams."),
316
+    .init           = init,
317
+    .uninit         = uninit,
318
+    .query_formats  = query_formats,
319
+    .priv_size      = sizeof(PSNRContext),
320
+    .priv_class     = &psnr_class,
321
+    .inputs         = psnr_inputs,
322
+    .outputs        = psnr_outputs,
323
+};