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
6 * This file is part of FFmpeg.
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.
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.
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
25 * Caculate the PSNR between two input videos.
28 #include "libavutil/opt.h"
29 #include "libavutil/pixdesc.h"
31 #include "dualinput.h"
32 #include "drawutils.h"
37 typedef struct PSNRContext {
39 FFDualInputContext dinput;
40 double mse, min_mse, max_mse;
44 int max[4], average_max;
48 const AVPixFmtDescriptor *desc;
51 #define OFFSET(x) offsetof(PSNRContext, x)
52 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
54 static const AVOption psnr_options[] = {
55 {"stats_file", "Set file where to store per-frame difference information", OFFSET(stats_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
56 {"f", "Set file where to store per-frame difference information", OFFSET(stats_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
60 AVFILTER_DEFINE_CLASS(psnr);
62 static inline int pow2(int base)
67 static inline double get_psnr(double mse, uint64_t nb_frames, int max)
69 return 10.0 * log(pow2(max) / (mse / nb_frames)) / log(10.0);
73 void compute_images_mse(const uint8_t *main_data[4], const int main_linesizes[4],
74 const uint8_t *ref_data[4], const int ref_linesizes[4],
75 int w, int h, const AVPixFmtDescriptor *desc,
80 for (c = 0; c < desc->nb_components; c++) {
81 int hsub = c == 1 || c == 2 ? desc->log2_chroma_w : 0;
82 int vsub = c == 1 || c == 2 ? desc->log2_chroma_h : 0;
83 const int outw = FF_CEIL_RSHIFT(w, hsub);
84 const int outh = FF_CEIL_RSHIFT(h, vsub);
85 const uint8_t *main_line = main_data[c];
86 const uint8_t *ref_line = ref_data[c];
87 const int ref_linesize = ref_linesizes[c];
88 const int main_linesize = main_linesizes[c];
91 for (i = 0; i < outh; i++) {
92 for (j = 0; j < outw; j++)
93 m += pow2(main_line[j] - ref_line[j]);
94 ref_line += ref_linesize;
95 main_line += main_linesize;
97 mse[c] = m / (outw * outh);
101 static void set_meta(AVDictionary **metadata, const char *key, char comp, float d)
104 snprintf(value, sizeof(value), "%0.2f", d);
107 snprintf(key2, sizeof(key2), "%s%c", key, comp);
108 av_dict_set(metadata, key2, value, 0);
110 av_dict_set(metadata, key, value, 0);
114 static AVFrame *do_psnr(AVFilterContext *ctx, AVFrame *main,
117 PSNRContext *s = ctx->priv;
118 double comp_mse[4], mse = 0;
120 AVDictionary **metadata = avpriv_frame_get_metadatap(main);
122 compute_images_mse((const uint8_t **)main->data, main->linesize,
123 (const uint8_t **)ref->data, ref->linesize,
124 main->width, main->height, s->desc, comp_mse);
126 for (j = 0; j < s->desc->nb_components; j++)
128 mse /= s->desc->nb_components;
130 s->min_mse = FFMIN(s->min_mse, mse);
131 s->max_mse = FFMAX(s->max_mse, mse);
136 for (j = 0; j < s->desc->nb_components; j++) {
137 c = s->is_rgb ? s->rgba_map[j] : j;
138 set_meta(metadata, "lavfi.psnr.mse.", s->comps[j], comp_mse[c]);
139 set_meta(metadata, "lavfi.psnr.mse_avg", 0, mse);
140 set_meta(metadata, "lavfi.psnr.psnr.", s->comps[j], get_psnr(comp_mse[c], 1, s->max[c]));
141 set_meta(metadata, "lavfi.psnr.psnr_avg", 0, get_psnr(mse, 1, s->average_max));
145 fprintf(s->stats_file, "n:%"PRId64" mse_avg:%0.2f ", s->nb_frames, mse);
146 for (j = 0; j < s->desc->nb_components; j++) {
147 c = s->is_rgb ? s->rgba_map[j] : j;
148 fprintf(s->stats_file, "mse_%c:%0.2f ", s->comps[j], comp_mse[c]);
150 for (j = 0; j < s->desc->nb_components; j++) {
151 c = s->is_rgb ? s->rgba_map[j] : j;
152 fprintf(s->stats_file, "psnr_%c:%0.2f ", s->comps[j],
153 get_psnr(comp_mse[c], 1, s->max[c]));
155 fprintf(s->stats_file, "\n");
161 static av_cold int init(AVFilterContext *ctx)
163 PSNRContext *s = ctx->priv;
165 s->min_mse = +INFINITY;
166 s->max_mse = -INFINITY;
168 if (s->stats_file_str) {
169 s->stats_file = fopen(s->stats_file_str, "w");
170 if (!s->stats_file) {
171 int err = AVERROR(errno);
173 av_strerror(err, buf, sizeof(buf));
174 av_log(ctx, AV_LOG_ERROR, "Could not open stats file %s: %s\n",
175 s->stats_file_str, buf);
180 s->dinput.process = do_psnr;
184 static int query_formats(AVFilterContext *ctx)
186 static const enum PixelFormat pix_fmts[] = {
188 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
189 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P,
190 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
191 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ422P,
192 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P,
193 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
197 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
201 static int config_input_ref(AVFilterLink *inlink)
203 AVFilterContext *ctx = inlink->dst;
204 PSNRContext *s = ctx->priv;
207 s->desc = av_pix_fmt_desc_get(inlink->format);
208 if (ctx->inputs[0]->w != ctx->inputs[1]->w ||
209 ctx->inputs[0]->h != ctx->inputs[1]->h) {
210 av_log(ctx, AV_LOG_ERROR, "Width and heigth of input videos must be same.\n");
211 return AVERROR(EINVAL);
213 if (ctx->inputs[0]->format != ctx->inputs[1]->format) {
214 av_log(ctx, AV_LOG_ERROR, "Inputs must be of same pixel format.\n");
215 return AVERROR(EINVAL);
218 switch (inlink->format) {
219 case AV_PIX_FMT_YUV410P:
220 case AV_PIX_FMT_YUV411P:
221 case AV_PIX_FMT_YUV420P:
222 case AV_PIX_FMT_YUV422P:
223 case AV_PIX_FMT_YUV440P:
224 case AV_PIX_FMT_YUV444P:
225 case AV_PIX_FMT_YUVA420P:
226 case AV_PIX_FMT_YUVA422P:
227 case AV_PIX_FMT_YUVA444P:
230 s->max[1] = s->max[2] = 240;
233 s->max[0] = s->max[1] = s->max[2] = s->max[3] = 255;
236 s->is_rgb = ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0;
237 s->comps[0] = s->is_rgb ? 'r' : 'y' ;
238 s->comps[1] = s->is_rgb ? 'g' : 'u' ;
239 s->comps[2] = s->is_rgb ? 'b' : 'v' ;
242 for (j = 0; j < s->desc->nb_components; j++)
243 s->average_max += s->max[j];
244 s->average_max /= s->desc->nb_components;
249 static int config_output(AVFilterLink *outlink)
251 AVFilterContext *ctx = outlink->src;
252 AVFilterLink *mainlink = ctx->inputs[0];
254 outlink->w = mainlink->w;
255 outlink->h = mainlink->h;
256 outlink->time_base = mainlink->time_base;
257 outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
258 outlink->frame_rate = mainlink->frame_rate;
263 static int filter_frame_main(AVFilterLink *inlink, AVFrame *inpicref)
265 PSNRContext *s = inlink->dst->priv;
266 return ff_dualinput_filter_frame_main(&s->dinput, inlink, inpicref);
269 static int filter_frame_ref(AVFilterLink *inlink, AVFrame *inpicref)
271 PSNRContext *s = inlink->dst->priv;
272 return ff_dualinput_filter_frame_second(&s->dinput, inlink, inpicref);
275 static int request_frame(AVFilterLink *outlink)
277 PSNRContext *s = outlink->src->priv;
278 return ff_dualinput_request_frame(&s->dinput, outlink);
281 static av_cold void uninit(AVFilterContext *ctx)
283 PSNRContext *s = ctx->priv;
285 if (s->nb_frames > 0) {
286 av_log(ctx, AV_LOG_INFO, "PSNR average:%0.2f min:%0.2f max:%0.2f\n",
287 get_psnr(s->mse, s->nb_frames, s->average_max),
288 get_psnr(s->max_mse, 1, s->average_max),
289 get_psnr(s->min_mse, 1, s->average_max));
292 ff_dualinput_uninit(&s->dinput);
295 fclose(s->stats_file);
298 static const AVFilterPad psnr_inputs[] = {
301 .type = AVMEDIA_TYPE_VIDEO,
302 .filter_frame = filter_frame_main,
305 .type = AVMEDIA_TYPE_VIDEO,
306 .filter_frame = filter_frame_ref,
307 .config_props = config_input_ref,
312 static const AVFilterPad psnr_outputs[] = {
315 .type = AVMEDIA_TYPE_VIDEO,
316 .config_props = config_output,
317 .request_frame = request_frame,
322 AVFilter avfilter_vf_psnr = {
324 .description = NULL_IF_CONFIG_SMALL("Calculate the PSNR between two video streams."),
327 .query_formats = query_formats,
328 .priv_size = sizeof(PSNRContext),
329 .priv_class = &psnr_class,
330 .inputs = psnr_inputs,
331 .outputs = psnr_outputs,