62#define OFFSET(x) offsetof(PSNRContext, x)
63#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
68 {
"stats_version",
"Set the format version for the stats file.",
OFFSET(stats_version),
AV_OPT_TYPE_INT, {.i64=1}, 1, 2,
FLAGS },
80static inline double get_psnr(
double mse, uint64_t nb_frames,
int max)
82 return 10.0 * log10(
pow_2(
max) / (mse / nb_frames));
99 int jobnr,
int nb_jobs)
102 uint64_t *score = td->
score[jobnr];
116 ref_line += ref_linesize;
117 main_line += main_linesize;
143 double comp_mse[4], mse = 0.;
144 uint64_t comp_sum[4] = { 0 };
152 if (
ctx->is_disabled || !
ref)
159 for (
int c = 0;
c <
s->nb_components;
c++) {
168 if (
master->color_range !=
ref->color_range) {
170 "frames use different color ranges (%s != %s)\n",
176 FFMIN(
s->planeheight[1],
s->nb_threads));
178 for (
int j = 0; j <
s->nb_threads; j++) {
179 for (
int c = 0;
c <
s->nb_components;
c++)
180 comp_sum[
c] +=
s->score[j][
c];
183 for (
int c = 0;
c <
s->nb_components;
c++)
184 comp_mse[
c] = comp_sum[
c] / ((
double)
s->planewidth[
c] *
s->planeheight[
c]);
186 for (
int c = 0;
c <
s->nb_components;
c++)
187 mse += comp_mse[
c] *
s->planeweight[
c];
189 s->min_mse =
FFMIN(
s->min_mse, mse);
190 s->max_mse =
FFMAX(
s->max_mse, mse);
194 for (
int j = 0; j <
s->nb_components; j++)
195 s->mse_comp[j] += comp_mse[j];
198 for (
int j = 0; j <
s->nb_components; j++) {
199 int c =
s->is_rgb ?
s->rgba_map[j] : j;
207 if (
s->stats_version == 2 && !
s->stats_header_written) {
208 fprintf(
s->stats_file,
"psnr_log_version:2 fields:n");
209 fprintf(
s->stats_file,
",mse_avg");
210 for (
int j = 0; j <
s->nb_components; j++) {
211 fprintf(
s->stats_file,
",mse_%c",
s->comps[j]);
213 fprintf(
s->stats_file,
",psnr_avg");
214 for (
int j = 0; j <
s->nb_components; j++) {
215 fprintf(
s->stats_file,
",psnr_%c",
s->comps[j]);
217 if (
s->stats_add_max) {
218 fprintf(
s->stats_file,
",max_avg");
219 for (
int j = 0; j <
s->nb_components; j++) {
220 fprintf(
s->stats_file,
",max_%c",
s->comps[j]);
223 fprintf(
s->stats_file,
"\n");
224 s->stats_header_written = 1;
226 fprintf(
s->stats_file,
"n:%"PRId64
" mse_avg:%0.2f ",
s->nb_frames, mse);
227 for (
int j = 0; j <
s->nb_components; j++) {
228 int c =
s->is_rgb ?
s->rgba_map[j] : j;
229 fprintf(
s->stats_file,
"mse_%c:%0.2f ",
s->comps[j], comp_mse[
c]);
231 fprintf(
s->stats_file,
"psnr_avg:%0.2f ",
get_psnr(mse, 1,
s->average_max));
232 for (
int j = 0; j <
s->nb_components; j++) {
233 int c =
s->is_rgb ?
s->rgba_map[j] : j;
234 fprintf(
s->stats_file,
"psnr_%c:%0.2f ",
s->comps[j],
237 if (
s->stats_version == 2 &&
s->stats_add_max) {
238 fprintf(
s->stats_file,
"max_avg:%d ",
s->average_max);
239 for (
int j = 0; j <
s->nb_components; j++) {
240 int c =
s->is_rgb ?
s->rgba_map[j] : j;
241 fprintf(
s->stats_file,
"max_%c:%d ",
s->comps[j],
s->max[
c]);
244 fprintf(
s->stats_file,
"\n");
257 if (
s->stats_file_str) {
258 if (
s->stats_version < 2 &&
s->stats_add_max) {
260 "stats_add_max was specified but stats_version < 2.\n" );
263 if (!strcmp(
s->stats_file_str,
"-")) {
264 s->stats_file = stdout;
267 if (!
s->stats_file) {
282#define PF_NOALPHA(suf) AV_PIX_FMT_YUV420##suf, AV_PIX_FMT_YUV422##suf, AV_PIX_FMT_YUV444##suf
283#define PF_ALPHA(suf) AV_PIX_FMT_YUVA420##suf, AV_PIX_FMT_YUVA422##suf, AV_PIX_FMT_YUVA444##suf
284#define PF(suf) PF_NOALPHA(suf), PF_ALPHA(suf)
305 s->nb_components =
desc->nb_components;
306 if (
ctx->inputs[0]->w !=
ctx->inputs[1]->w ||
307 ctx->inputs[0]->h !=
ctx->inputs[1]->h) {
312 s->max[0] = (1 <<
desc->comp[0].depth) - 1;
313 s->max[1] = (1 <<
desc->comp[1].depth) - 1;
314 s->max[2] = (1 <<
desc->comp[2].depth) - 1;
315 s->max[3] = (1 <<
desc->comp[3].depth) - 1;
318 s->comps[0] =
s->is_rgb ?
'r' :
'y' ;
319 s->comps[1] =
s->is_rgb ?
'g' :
'u' ;
320 s->comps[2] =
s->is_rgb ?
'b' :
'v' ;
324 s->planeheight[0] =
s->planeheight[3] = inlink->
h;
326 s->planewidth[0] =
s->planewidth[3] = inlink->
w;
328 for (j = 0; j <
s->nb_components; j++)
329 sum +=
s->planeheight[j] *
s->planewidth[j];
331 for (j = 0; j <
s->nb_components; j++) {
332 s->planeweight[j] = (
double)
s->planeheight[j] *
s->planewidth[j] / sum;
333 average_max +=
s->max[j] *
s->planeweight[j];
335 s->average_max =
lrint(average_max);
343 for (
int t = 0; t <
s->nb_threads; t++) {
344 s->score[t] =
av_calloc(
s->nb_components,
sizeof(*
s->score[0]));
364 outlink->
w = mainlink->
w;
365 outlink->
h = mainlink->
h;
376 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",
378 ctx->inputs[1]->time_base.num,
ctx->inputs[1]->time_base.den);
393 if (
s->nb_frames > 0) {
398 for (j = 0; j <
s->nb_components; j++) {
399 int c =
s->is_rgb ?
s->rgba_map[j] : j;
411 for (
int t = 0; t <
s->nb_threads &&
s->score; t++)
415 if (
s->stats_file &&
s->stats_file != stdout)
416 fclose(
s->stats_file);
441 .p.priv_class = &psnr_class,
445 .preinit = psnr_framesync_preinit,
const FFFilter ff_vf_psnr
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Main libavfilter public API header.
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
static int FUNC metadata(CodedBitstreamContext *ctx, RWContext *rw, APVRawMetadata *current)
#define i(width, name, range_min, range_max)
#define fs(width, name, subs,...)
#define AV_CEIL_RSHIFT(a, b)
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
int(* init)(AVBSFContext *ctx)
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
static double psnr(double d)
int ff_framesync_configure(FFFrameSync *fs)
Configure a frame sync structure.
int ff_framesync_dualinput_get(FFFrameSync *fs, AVFrame **f0, AVFrame **f1)
int ff_framesync_activate(FFFrameSync *fs)
Examine the frames in the filter's input and try to produce output.
int ff_framesync_init_dualinput(FFFrameSync *fs, AVFilterContext *parent)
Initialize a frame sync structure for dualinput.
void ff_framesync_uninit(FFFrameSync *fs)
Free all memory currently allocated.
#define FRAMESYNC_DEFINE_CLASS(name, context, field)
@ AV_OPT_TYPE_INT
Underlying C type is int.
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
@ 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 AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_INFO
Standard information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
#define FILTER_INPUTS(array)
#define FILTER_OUTPUTS(array)
static int ff_slice_pos(int total, int jobnr, int nb_jobs)
Compute the boundary index for a slice when work of size total is split into nb_jobs slices.
#define FILTER_PIXFMTS_ARRAY(array)
static FilterLink * ff_filter_link(AVFilterLink *link)
FILE * avpriv_fopen_utf8(const char *path, const char *mode)
Open a file using a UTF-8 filename.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static enum AVPixelFormat pix_fmts[]
void * av_calloc(size_t nmemb, size_t size)
Memory handling functions.
static int slice_end(AVCodecContext *avctx, AVFrame *pict, int *got_output)
Handle slice ends.
const char * av_color_range_name(enum AVColorRange range)
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
#define AV_PIX_FMT_GBRAP12
#define AV_PIX_FMT_GBRAP16
#define AV_PIX_FMT_GBRP10
#define AV_PIX_FMT_GRAY12
#define AV_PIX_FMT_GBRP12
AVPixelFormat
Pixel format.
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
@ AV_PIX_FMT_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
@ AV_PIX_FMT_YUVJ411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
#define AV_PIX_FMT_GRAY10
#define AV_PIX_FMT_GRAY14
#define AV_PIX_FMT_GRAY16
#define AV_PIX_FMT_GBRAP10
#define AV_PIX_FMT_GBRP16
#define AV_PIX_FMT_GBRP14
void ff_psnr_init(PSNRDSPContext *dsp, int bpp)
Describe the class of an AVClass context structure.
A link between two filters.
int w
agreed upon image width
int h
agreed upon image height
AVFilterContext * src
source filter
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link.
AVRational sample_aspect_ratio
agreed upon sample aspect ratio
AVFilterContext * dst
dest filter
int format
agreed upon media format
A filter pad used for either input or output.
This structure describes decoded (raw) audio or video data.
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Link properties exposed to filter code, but not external callers.
AVRational frame_rate
Frame rate of the stream on the link, or 1/0 if unknown or variable.
uint64_t(* sse_line)(const uint8_t *buf, const uint8_t *ref, int w)
Used for passing data between threads.
const uint8_t * ref_data[4]
const uint8_t * main_data[4]
static int ref[MAX_W *MAX_W]
static AVFormatContext * ctx
static int config_input_ref(AVFilterLink *inlink)
static const AVOption psnr_options[]
static unsigned pow_2(unsigned base)
static int compute_images_mse(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
static const AVFilterPad psnr_outputs[]
static int config_input_ref(AVFilterLink *inlink)
static void set_meta(AVDictionary **metadata, const char *key, char comp, float d)
static double get_psnr(double mse, uint64_t nb_frames, int max)
static const AVFilterPad psnr_inputs[]
static int activate(AVFilterContext *ctx)
static av_cold void uninit(AVFilterContext *ctx)
static int config_output(AVFilterLink *outlink)
static int do_psnr(FFFrameSync *fs)
static int slice_start(SliceContext *sc, VVCContext *s, VVCFrameContext *fc, const CodedBitstreamUnit *unit, const int is_first_slice)