FFmpeg: libavfilter/vf_psnr.c Source File
Go to the documentation of this file. 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 * Calculate the PSNR between two input videos.
26 */
27
38
61
62 #define OFFSET(x) offsetof(PSNRContext, x)
63 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
64
68 {
"stats_version",
"Set the format version for the stats file.",
OFFSET(stats_version),
AV_OPT_TYPE_INT, {.i64=1}, 1, 2,
FLAGS },
71 };
72
74
76 {
78 }
79
80 static inline double get_psnr(
double mse, uint64_t nb_frames,
int max)
81 {
82 return 10.0 * log10(
pow_2(
max) / (mse / nb_frames));
83 }
84
85 static uint64_t
sse_line_8bit(
const uint8_t *main_line,
const uint8_t *ref_line,
int outw)
86 {
87 int j;
88 unsigned m2 = 0;
89
90 for (j = 0; j < outw; j++)
91 m2 +=
pow_2(main_line[j] - ref_line[j]);
92
93 return m2;
94 }
95
96 static uint64_t
sse_line_16bit(
const uint8_t *_main_line,
const uint8_t *_ref_line,
int outw)
97 {
98 int j;
99 uint64_t m2 = 0;
100 const uint16_t *main_line = (const uint16_t *) _main_line;
101 const uint16_t *ref_line = (const uint16_t *) _ref_line;
102
103 for (j = 0; j < outw; j++)
104 m2 +=
pow_2(main_line[j] - ref_line[j]);
105
106 return m2;
107 }
108
120
121 static
123 int jobnr, int nb_jobs)
124 {
126 uint64_t *score = td->
score[jobnr];
127
132 const int slice_end = (outh * (jobnr+1)) / nb_jobs;
137 uint64_t m = 0;
140 ref_line += ref_linesize;
141 main_line += main_linesize;
142 }
144 }
145
146 return 0;
147 }
148
150 {
154 char key2[128];
157 } else {
159 }
160 }
161
163 {
167 double comp_mse[4], mse = 0.;
168 uint64_t comp_sum[4] = { 0 };
172
176 if (
ctx->is_disabled || !
ref)
178 metadata = &
master->metadata;
179
183 for (
int c = 0;
c <
s->nb_components;
c++) {
190 }
191
192 if (
master->color_range !=
ref->color_range) {
194 "frames use different color ranges (%s != %s)\n",
197 }
198
200 FFMIN(
s->planeheight[1],
s->nb_threads));
201
202 for (
int j = 0; j <
s->nb_threads; j++) {
203 for (
int c = 0;
c <
s->nb_components;
c++)
204 comp_sum[
c] +=
s->score[j][
c];
205 }
206
207 for (
int c = 0;
c <
s->nb_components;
c++)
208 comp_mse[
c] = comp_sum[
c] / ((
double)
s->planewidth[
c] *
s->planeheight[
c]);
209
210 for (
int c = 0;
c <
s->nb_components;
c++)
211 mse += comp_mse[
c] *
s->planeweight[
c];
212
213 s->min_mse =
FFMIN(
s->min_mse, mse);
214 s->max_mse =
FFMAX(
s->max_mse, mse);
215
217
218 for (
int j = 0; j <
s->nb_components; j++)
219 s->mse_comp[j] += comp_mse[j];
221
222 for (
int j = 0; j <
s->nb_components; j++) {
223 int c =
s->is_rgb ?
s->rgba_map[j] : j;
224 set_meta(metadata,
"lavfi.psnr.mse.",
s->comps[j], comp_mse[
c]);
226 }
227 set_meta(metadata,
"lavfi.psnr.mse_avg", 0, mse);
229
231 if (
s->stats_version == 2 && !
s->stats_header_written) {
232 fprintf(
s->stats_file,
"psnr_log_version:2 fields:n");
233 fprintf(
s->stats_file,
",mse_avg");
234 for (
int j = 0; j <
s->nb_components; j++) {
235 fprintf(
s->stats_file,
",mse_%c",
s->comps[j]);
236 }
237 fprintf(
s->stats_file,
",psnr_avg");
238 for (
int j = 0; j <
s->nb_components; j++) {
239 fprintf(
s->stats_file,
",psnr_%c",
s->comps[j]);
240 }
241 if (
s->stats_add_max) {
242 fprintf(
s->stats_file,
",max_avg");
243 for (
int j = 0; j <
s->nb_components; j++) {
244 fprintf(
s->stats_file,
",max_%c",
s->comps[j]);
245 }
246 }
247 fprintf(
s->stats_file,
"\n");
248 s->stats_header_written = 1;
249 }
250 fprintf(
s->stats_file,
"n:%"PRId64
" mse_avg:%0.2f ",
s->nb_frames, mse);
251 for (
int j = 0; j <
s->nb_components; j++) {
252 int c =
s->is_rgb ?
s->rgba_map[j] : j;
253 fprintf(
s->stats_file,
"mse_%c:%0.2f ",
s->comps[j], comp_mse[
c]);
254 }
255 fprintf(
s->stats_file,
"psnr_avg:%0.2f ",
get_psnr(mse, 1,
s->average_max));
256 for (
int j = 0; j <
s->nb_components; j++) {
257 int c =
s->is_rgb ?
s->rgba_map[j] : j;
258 fprintf(
s->stats_file,
"psnr_%c:%0.2f ",
s->comps[j],
260 }
261 if (
s->stats_version == 2 &&
s->stats_add_max) {
262 fprintf(
s->stats_file,
"max_avg:%d ",
s->average_max);
263 for (
int j = 0; j <
s->nb_components; j++) {
264 int c =
s->is_rgb ?
s->rgba_map[j] : j;
265 fprintf(
s->stats_file,
"max_%c:%d ",
s->comps[j],
s->max[
c]);
266 }
267 }
268 fprintf(
s->stats_file,
"\n");
269 }
270
272 }
273
275 {
277
280
281 if (
s->stats_file_str) {
282 if (
s->stats_version < 2 &&
s->stats_add_max) {
284 "stats_add_max was specified but stats_version < 2.\n" );
286 }
287 if (!strcmp(
s->stats_file_str,
"-")) {
288 s->stats_file = stdout;
289 } else {
291 if (!
s->stats_file) {
295 return err;
296 }
297 }
298 }
299
301 return 0;
302 }
303
306 #define PF_NOALPHA(suf) AV_PIX_FMT_YUV420##suf, AV_PIX_FMT_YUV422##suf, AV_PIX_FMT_YUV444##suf
307 #define PF_ALPHA(suf) AV_PIX_FMT_YUVA420##suf, AV_PIX_FMT_YUVA422##suf, AV_PIX_FMT_YUVA444##suf
308 #define PF(suf) PF_NOALPHA(suf), PF_ALPHA(suf)
317 };
318
320 {
324 double average_max;
325 unsigned sum;
326 int j;
327
329 s->nb_components =
desc->nb_components;
330 if (
ctx->inputs[0]->w !=
ctx->inputs[1]->w ||
331 ctx->inputs[0]->h !=
ctx->inputs[1]->h) {
334 }
335
336 s->max[0] = (1 <<
desc->comp[0].depth) - 1;
337 s->max[1] = (1 <<
desc->comp[1].depth) - 1;
338 s->max[2] = (1 <<
desc->comp[2].depth) - 1;
339 s->max[3] = (1 <<
desc->comp[3].depth) - 1;
340
342 s->comps[0] =
s->is_rgb ?
'r' :
'y' ;
343 s->comps[1] =
s->is_rgb ?
'g' :
'u' ;
344 s->comps[2] =
s->is_rgb ?
'b' :
'v' ;
346
348 s->planeheight[0] =
s->planeheight[3] =
inlink->h;
350 s->planewidth[0] =
s->planewidth[3] =
inlink->w;
351 sum = 0;
352 for (j = 0; j <
s->nb_components; j++)
353 sum +=
s->planeheight[j] *
s->planewidth[j];
354 average_max = 0;
355 for (j = 0; j <
s->nb_components; j++) {
356 s->planeweight[j] = (
double)
s->planeheight[j] *
s->planewidth[j] / sum;
357 average_max +=
s->max[j] *
s->planeweight[j];
358 }
359 s->average_max =
lrint(average_max);
360
362 #if ARCH_X86
364 #endif
365
369
370 for (
int t = 0; t <
s->nb_threads; t++) {
371 s->score[t] =
av_calloc(
s->nb_components,
sizeof(*
s->score[0]));
374 }
375
376 return 0;
377 }
378
380 {
387
391 outlink->
w = mainlink->
w;
392 outlink->
h = mainlink->
h;
398
400
403 av_log(
ctx,
AV_LOG_WARNING,
"not matching timebases found between first input: %d/%d and second input %d/%d, results may be incorrect!\n",
405 ctx->inputs[1]->time_base.num,
ctx->inputs[1]->time_base.den);
406
407 return 0;
408 }
409
411 {
414 }
415
417 {
419
420 if (
s->nb_frames > 0) {
421 int j;
422 char buf[256];
423
424 buf[0] = 0;
425 for (j = 0; j <
s->nb_components; j++) {
426 int c =
s->is_rgb ?
s->rgba_map[j] : j;
429 }
431 buf,
435 }
436
438 for (
int t = 0; t <
s->nb_threads &&
s->score; t++)
441
442 if (
s->stats_file &&
s->stats_file != stdout)
443 fclose(
s->stats_file);
444 }
445
447 {
450 },{
451 .name = "reference",
454 },
455 };
456
458 {
462 },
463 };
464
468 .preinit = psnr_framesync_preinit,
473 .priv_class = &psnr_class,
480 };
#define AV_PIX_FMT_GBRAP16
int ff_framesync_configure(FFFrameSync *fs)
Configure a frame sync structure.
#define AV_LOG_WARNING
Something somehow does not look correct.
static int config_input_ref(AVFilterLink *inlink)
AVPixelFormat
Pixel format.
static int do_psnr(FFFrameSync *fs)
Filter the word "frame" indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
static void set_meta(AVDictionary **metadata, const char *key, char comp, float d)
#define FILTER_PIXFMTS_ARRAY(array)
void ff_framesync_uninit(FFFrameSync *fs)
Free all memory currently allocated.
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
#define FILTER_INPUTS(array)
This structure describes decoded (raw) audio or video data.
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
const char * name
Filter name.
static av_cold int init(AVFilterContext *ctx)
static double psnr(double d)
A link between two filters.
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
void ff_psnr_init_x86(PSNRDSPContext *dsp, int bpp)
Link properties exposed to filter code, but not external callers.
static const AVFilterPad psnr_inputs[]
#define AV_PIX_FMT_GBRP14
static int slice_end(AVCodecContext *avctx, AVFrame *pict, int *got_output)
Handle slice ends.
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
#define AV_PIX_FMT_GBRP10
#define AV_PIX_FMT_GRAY16
A filter pad used for either input or output.
@ AV_PIX_FMT_YUVJ411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
#define AV_PIX_FMT_GBRAP10
#define AV_PIX_FMT_GBRAP12
#define AV_CEIL_RSHIFT(a, b)
FRAMESYNC_DEFINE_CLASS(psnr, PSNRContext, fs)
AVRational sample_aspect_ratio
agreed upon sample aspect ratio
#define AV_PIX_FMT_GRAY14
#define FILTER_OUTPUTS(array)
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
#define AV_PIX_FMT_GRAY10
const char * av_color_range_name(enum AVColorRange range)
#define AV_PIX_FMT_GBRP16
Describe the class of an AVClass context structure.
#define fs(width, name, subs,...)
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
static const AVOption psnr_options[]
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
static uint64_t sse_line_8bit(const uint8_t *main_line, const uint8_t *ref_line, int outw)
static FilterLink * ff_filter_link(AVFilterLink *link)
static uint64_t sse_line_16bit(const uint8_t *_main_line, const uint8_t *_ref_line, int outw)
static const AVFilterPad psnr_outputs[]
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
int ff_framesync_init_dualinput(FFFrameSync *fs, AVFilterContext *parent)
Initialize a frame sync structure for dualinput.
static unsigned pow_2(unsigned base)
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
static av_cold void uninit(AVFilterContext *ctx)
AVFilterContext * src
source filter
static enum AVPixelFormat pix_fmts[]
#define AV_LOG_INFO
Standard information.
#define i(width, name, range_min, range_max)
int w
agreed upon image width
#define AV_PIX_FMT_GBRP12
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Used for passing data between threads.
static int config_output(AVFilterLink *outlink)
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
@ AV_PIX_FMT_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
const char * name
Pad name.
FILE * avpriv_fopen_utf8(const char *path, const char *mode)
Open a file using a UTF-8 filename.
void * av_calloc(size_t nmemb, size_t size)
static int slice_start(SliceContext *sc, VVCContext *s, VVCFrameContext *fc, const CodedBitstreamUnit *unit, const int is_first_slice)
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
static double get_psnr(double mse, uint64_t nb_frames, int max)
uint64_t(* sse_line)(const uint8_t *buf, const uint8_t *ref, int w)
int h
agreed upon image height
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
@ AV_OPT_TYPE_INT
Underlying C type is int.
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
static int ref[MAX_W *MAX_W]
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link.
static int activate(AVFilterContext *ctx)
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
static int compute_images_mse(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
AVRational frame_rate
Frame rate of the stream on the link, or 1/0 if unknown or variable.
int ff_framesync_activate(FFFrameSync *fs)
Examine the frames in the filter's input and try to produce output.
int ff_framesync_dualinput_get(FFFrameSync *fs, AVFrame **f0, AVFrame **f1)
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
#define AV_PIX_FMT_GRAY12
const uint8_t * ref_data[4]
const AVFilter ff_vf_psnr
const uint8_t * main_data[4]
Generated on Fri Aug 22 2025 13:59:19 for FFmpeg by
doxygen
1.8.17