2013-07-06 03:21:12 +03:00
|
|
|
/*
|
2015-07-30 03:28:09 +02:00
|
|
|
* Copyright (c) 2011 Roger Pau Monné <roger.pau@entel.upc.edu>
|
2013-07-06 03:21:12 +03:00
|
|
|
* Copyright (c) 2011 Stefano Sabatini
|
|
|
|
* Copyright (c) 2013 Paul B Mahol
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Caculate the PSNR between two input videos.
|
|
|
|
*/
|
|
|
|
|
2015-07-11 03:42:26 +02:00
|
|
|
#include "libavutil/avstring.h"
|
2013-07-06 03:21:12 +03:00
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavutil/pixdesc.h"
|
|
|
|
#include "avfilter.h"
|
|
|
|
#include "drawutils.h"
|
|
|
|
#include "formats.h"
|
2017-08-31 19:47:37 +02:00
|
|
|
#include "framesync.h"
|
2013-07-06 03:21:12 +03:00
|
|
|
#include "internal.h"
|
2015-07-12 12:44:39 +02:00
|
|
|
#include "psnr.h"
|
2013-07-06 03:21:12 +03:00
|
|
|
#include "video.h"
|
|
|
|
|
|
|
|
typedef struct PSNRContext {
|
|
|
|
const AVClass *class;
|
2017-07-31 12:44:13 +02:00
|
|
|
FFFrameSync fs;
|
2015-07-11 03:42:26 +02:00
|
|
|
double mse, min_mse, max_mse, mse_comp[4];
|
2013-07-06 03:21:12 +03:00
|
|
|
uint64_t nb_frames;
|
|
|
|
FILE *stats_file;
|
|
|
|
char *stats_file_str;
|
2016-07-25 20:54:37 +02:00
|
|
|
int stats_version;
|
|
|
|
int stats_header_written;
|
2016-08-31 21:40:41 +02:00
|
|
|
int stats_add_max;
|
2013-07-06 03:21:12 +03:00
|
|
|
int max[4], average_max;
|
|
|
|
int is_rgb;
|
|
|
|
uint8_t rgba_map[4];
|
|
|
|
char comps[4];
|
2013-09-08 13:53:55 +03:00
|
|
|
int nb_components;
|
2021-02-13 01:53:16 +02:00
|
|
|
int nb_threads;
|
2013-09-08 13:53:55 +03:00
|
|
|
int planewidth[4];
|
|
|
|
int planeheight[4];
|
2015-07-11 03:42:26 +02:00
|
|
|
double planeweight[4];
|
2021-02-13 01:53:16 +02:00
|
|
|
uint64_t **score;
|
2015-07-12 12:44:39 +02:00
|
|
|
PSNRDSPContext dsp;
|
2013-07-06 03:21:12 +03:00
|
|
|
} PSNRContext;
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(PSNRContext, x)
|
|
|
|
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
|
|
|
|
|
|
|
|
static const AVOption psnr_options[] = {
|
|
|
|
{"stats_file", "Set file where to store per-frame difference information", OFFSET(stats_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
|
|
|
|
{"f", "Set file where to store per-frame difference information", OFFSET(stats_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
|
2016-07-25 20:54:37 +02:00
|
|
|
{"stats_version", "Set the format version for the stats file.", OFFSET(stats_version), AV_OPT_TYPE_INT, {.i64=1}, 1, 2, FLAGS },
|
2016-08-31 21:40:41 +02:00
|
|
|
{"output_max", "Add raw stats (max values) to the output log.", OFFSET(stats_add_max), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
|
2013-09-07 15:13:50 +03:00
|
|
|
{ NULL }
|
2013-07-06 03:21:12 +03:00
|
|
|
};
|
|
|
|
|
2017-07-31 12:44:13 +02:00
|
|
|
FRAMESYNC_DEFINE_CLASS(psnr, PSNRContext, fs);
|
2013-07-06 03:21:12 +03:00
|
|
|
|
2017-03-26 20:43:11 +02:00
|
|
|
static inline unsigned pow_2(unsigned base)
|
2013-07-06 03:21:12 +03:00
|
|
|
{
|
|
|
|
return base*base;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline double get_psnr(double mse, uint64_t nb_frames, int max)
|
|
|
|
{
|
2017-03-26 20:43:11 +02:00
|
|
|
return 10.0 * log10(pow_2(max) / (mse / nb_frames));
|
2013-07-06 03:21:12 +03:00
|
|
|
}
|
|
|
|
|
2015-07-12 12:44:39 +02:00
|
|
|
static uint64_t sse_line_8bit(const uint8_t *main_line, const uint8_t *ref_line, int outw)
|
2013-07-06 03:21:12 +03:00
|
|
|
{
|
2015-07-12 12:44:39 +02:00
|
|
|
int j;
|
|
|
|
unsigned m2 = 0;
|
2013-07-06 03:21:12 +03:00
|
|
|
|
2015-07-12 12:44:39 +02:00
|
|
|
for (j = 0; j < outw; j++)
|
2017-03-26 20:43:11 +02:00
|
|
|
m2 += pow_2(main_line[j] - ref_line[j]);
|
2013-07-06 03:21:12 +03:00
|
|
|
|
2015-07-12 12:44:39 +02:00
|
|
|
return m2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t sse_line_16bit(const uint8_t *_main_line, const uint8_t *_ref_line, int outw)
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
uint64_t m2 = 0;
|
|
|
|
const uint16_t *main_line = (const uint16_t *) _main_line;
|
|
|
|
const uint16_t *ref_line = (const uint16_t *) _ref_line;
|
|
|
|
|
|
|
|
for (j = 0; j < outw; j++)
|
2017-03-26 20:43:11 +02:00
|
|
|
m2 += pow_2(main_line[j] - ref_line[j]);
|
2015-07-12 12:44:39 +02:00
|
|
|
|
|
|
|
return m2;
|
2013-09-08 14:37:07 +03:00
|
|
|
}
|
|
|
|
|
2021-02-13 01:53:16 +02:00
|
|
|
typedef struct ThreadData {
|
|
|
|
const uint8_t *main_data[4];
|
|
|
|
const uint8_t *ref_data[4];
|
|
|
|
int main_linesize[4];
|
|
|
|
int ref_linesize[4];
|
|
|
|
int planewidth[4];
|
|
|
|
int planeheight[4];
|
|
|
|
uint64_t **score;
|
|
|
|
int nb_components;
|
|
|
|
PSNRDSPContext *dsp;
|
|
|
|
} ThreadData;
|
|
|
|
|
|
|
|
static
|
|
|
|
int compute_images_mse(AVFilterContext *ctx, void *arg,
|
|
|
|
int jobnr, int nb_jobs)
|
2013-09-08 14:37:07 +03:00
|
|
|
{
|
2021-02-13 01:53:16 +02:00
|
|
|
ThreadData *td = arg;
|
|
|
|
uint64_t *score = td->score[jobnr];
|
|
|
|
|
|
|
|
for (int c = 0; c < td->nb_components; c++) {
|
|
|
|
const int outw = td->planewidth[c];
|
|
|
|
const int outh = td->planeheight[c];
|
|
|
|
const int slice_start = (outh * jobnr) / nb_jobs;
|
|
|
|
const int slice_end = (outh * (jobnr+1)) / nb_jobs;
|
|
|
|
const int ref_linesize = td->ref_linesize[c];
|
|
|
|
const int main_linesize = td->main_linesize[c];
|
|
|
|
const uint8_t *main_line = td->main_data[c] + main_linesize * slice_start;
|
|
|
|
const uint8_t *ref_line = td->ref_data[c] + ref_linesize * slice_start;
|
2013-09-08 14:37:07 +03:00
|
|
|
uint64_t m = 0;
|
2021-02-13 01:53:16 +02:00
|
|
|
for (int i = slice_start; i < slice_end; i++) {
|
|
|
|
m += td->dsp->sse_line(main_line, ref_line, outw);
|
2013-09-08 14:37:07 +03:00
|
|
|
ref_line += ref_linesize;
|
|
|
|
main_line += main_linesize;
|
|
|
|
}
|
2021-02-13 01:53:16 +02:00
|
|
|
score[c] = m;
|
2013-07-06 03:21:12 +03:00
|
|
|
}
|
2021-02-13 01:53:16 +02:00
|
|
|
|
|
|
|
return 0;
|
2013-07-06 03:21:12 +03:00
|
|
|
}
|
|
|
|
|
2013-07-08 23:13:14 +03:00
|
|
|
static void set_meta(AVDictionary **metadata, const char *key, char comp, float d)
|
|
|
|
{
|
|
|
|
char value[128];
|
2021-02-13 00:16:50 +02:00
|
|
|
snprintf(value, sizeof(value), "%f", d);
|
2013-07-08 23:13:14 +03:00
|
|
|
if (comp) {
|
|
|
|
char key2[128];
|
|
|
|
snprintf(key2, sizeof(key2), "%s%c", key, comp);
|
|
|
|
av_dict_set(metadata, key2, value, 0);
|
|
|
|
} else {
|
|
|
|
av_dict_set(metadata, key, value, 0);
|
|
|
|
}
|
|
|
|
}
|
2013-07-06 03:21:12 +03:00
|
|
|
|
2017-07-31 12:44:13 +02:00
|
|
|
static int do_psnr(FFFrameSync *fs)
|
2013-07-06 03:21:12 +03:00
|
|
|
{
|
2017-07-31 12:44:13 +02:00
|
|
|
AVFilterContext *ctx = fs->parent;
|
2013-07-06 03:21:12 +03:00
|
|
|
PSNRContext *s = ctx->priv;
|
2017-10-07 15:59:22 +02:00
|
|
|
AVFrame *master, *ref;
|
2021-02-13 01:53:16 +02:00
|
|
|
double comp_mse[4], mse = 0.;
|
|
|
|
uint64_t comp_sum[4] = { 0 };
|
2017-07-31 12:44:13 +02:00
|
|
|
AVDictionary **metadata;
|
2021-02-13 01:53:16 +02:00
|
|
|
ThreadData td;
|
|
|
|
int ret;
|
2017-07-31 12:44:13 +02:00
|
|
|
|
2017-10-07 15:59:22 +02:00
|
|
|
ret = ff_framesync_dualinput_get(fs, &master, &ref);
|
2017-07-31 12:44:13 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2021-02-12 22:18:23 +02:00
|
|
|
if (ctx->is_disabled || !ref)
|
2017-10-07 15:59:22 +02:00
|
|
|
return ff_filter_frame(ctx->outputs[0], master);
|
|
|
|
metadata = &master->metadata;
|
2013-07-06 03:21:12 +03:00
|
|
|
|
2021-02-13 01:53:16 +02:00
|
|
|
td.nb_components = s->nb_components;
|
|
|
|
td.dsp = &s->dsp;
|
|
|
|
td.score = s->score;
|
|
|
|
for (int c = 0; c < s->nb_components; c++) {
|
|
|
|
td.main_data[c] = master->data[c];
|
|
|
|
td.ref_data[c] = ref->data[c];
|
|
|
|
td.main_linesize[c] = master->linesize[c];
|
|
|
|
td.ref_linesize[c] = ref->linesize[c];
|
|
|
|
td.planewidth[c] = s->planewidth[c];
|
|
|
|
td.planeheight[c] = s->planeheight[c];
|
|
|
|
}
|
2013-07-06 03:21:12 +03:00
|
|
|
|
2021-08-15 21:33:25 +02:00
|
|
|
ff_filter_execute(ctx, compute_images_mse, &td, NULL,
|
|
|
|
FFMIN(s->planeheight[1], s->nb_threads));
|
2021-02-13 01:53:16 +02:00
|
|
|
|
|
|
|
for (int j = 0; j < s->nb_threads; j++) {
|
|
|
|
for (int c = 0; c < s->nb_components; c++)
|
|
|
|
comp_sum[c] += s->score[j][c];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int c = 0; c < s->nb_components; c++)
|
|
|
|
comp_mse[c] = comp_sum[c] / ((double)s->planewidth[c] * s->planeheight[c]);
|
|
|
|
|
|
|
|
for (int c = 0; c < s->nb_components; c++)
|
|
|
|
mse += comp_mse[c] * s->planeweight[c];
|
2013-07-06 03:21:12 +03:00
|
|
|
|
|
|
|
s->min_mse = FFMIN(s->min_mse, mse);
|
|
|
|
s->max_mse = FFMAX(s->max_mse, mse);
|
|
|
|
|
|
|
|
s->mse += mse;
|
2021-02-13 01:53:16 +02:00
|
|
|
|
|
|
|
for (int j = 0; j < s->nb_components; j++)
|
2015-07-11 03:42:26 +02:00
|
|
|
s->mse_comp[j] += comp_mse[j];
|
2013-07-06 03:21:12 +03:00
|
|
|
s->nb_frames++;
|
|
|
|
|
2021-02-13 01:53:16 +02:00
|
|
|
for (int j = 0; j < s->nb_components; j++) {
|
|
|
|
int c = s->is_rgb ? s->rgba_map[j] : j;
|
2013-07-08 23:13:14 +03:00
|
|
|
set_meta(metadata, "lavfi.psnr.mse.", s->comps[j], comp_mse[c]);
|
2013-07-09 12:38:47 +03:00
|
|
|
set_meta(metadata, "lavfi.psnr.psnr.", s->comps[j], get_psnr(comp_mse[c], 1, s->max[c]));
|
2013-07-06 03:21:12 +03:00
|
|
|
}
|
2015-07-11 04:47:24 +02:00
|
|
|
set_meta(metadata, "lavfi.psnr.mse_avg", 0, mse);
|
|
|
|
set_meta(metadata, "lavfi.psnr.psnr_avg", 0, get_psnr(mse, 1, s->average_max));
|
2013-07-06 03:21:12 +03:00
|
|
|
|
|
|
|
if (s->stats_file) {
|
2016-07-25 20:54:37 +02:00
|
|
|
if (s->stats_version == 2 && !s->stats_header_written) {
|
|
|
|
fprintf(s->stats_file, "psnr_log_version:2 fields:n");
|
|
|
|
fprintf(s->stats_file, ",mse_avg");
|
2021-02-13 01:53:16 +02:00
|
|
|
for (int j = 0; j < s->nb_components; j++) {
|
2016-07-25 20:54:37 +02:00
|
|
|
fprintf(s->stats_file, ",mse_%c", s->comps[j]);
|
|
|
|
}
|
|
|
|
fprintf(s->stats_file, ",psnr_avg");
|
2021-02-13 01:53:16 +02:00
|
|
|
for (int j = 0; j < s->nb_components; j++) {
|
2016-07-25 20:54:37 +02:00
|
|
|
fprintf(s->stats_file, ",psnr_%c", s->comps[j]);
|
|
|
|
}
|
2016-08-31 21:40:41 +02:00
|
|
|
if (s->stats_add_max) {
|
|
|
|
fprintf(s->stats_file, ",max_avg");
|
2021-02-13 01:53:16 +02:00
|
|
|
for (int j = 0; j < s->nb_components; j++) {
|
2016-08-31 21:40:41 +02:00
|
|
|
fprintf(s->stats_file, ",max_%c", s->comps[j]);
|
|
|
|
}
|
|
|
|
}
|
2016-07-25 20:54:37 +02:00
|
|
|
fprintf(s->stats_file, "\n");
|
|
|
|
s->stats_header_written = 1;
|
|
|
|
}
|
2013-07-06 03:21:12 +03:00
|
|
|
fprintf(s->stats_file, "n:%"PRId64" mse_avg:%0.2f ", s->nb_frames, mse);
|
2021-02-13 01:53:16 +02:00
|
|
|
for (int j = 0; j < s->nb_components; j++) {
|
|
|
|
int c = s->is_rgb ? s->rgba_map[j] : j;
|
2013-07-06 03:21:12 +03:00
|
|
|
fprintf(s->stats_file, "mse_%c:%0.2f ", s->comps[j], comp_mse[c]);
|
|
|
|
}
|
2015-08-05 13:30:16 +02:00
|
|
|
fprintf(s->stats_file, "psnr_avg:%0.2f ", get_psnr(mse, 1, s->average_max));
|
2021-02-13 01:53:16 +02:00
|
|
|
for (int j = 0; j < s->nb_components; j++) {
|
|
|
|
int c = s->is_rgb ? s->rgba_map[j] : j;
|
2013-07-09 12:38:47 +03:00
|
|
|
fprintf(s->stats_file, "psnr_%c:%0.2f ", s->comps[j],
|
2013-07-06 03:21:12 +03:00
|
|
|
get_psnr(comp_mse[c], 1, s->max[c]));
|
|
|
|
}
|
2016-08-31 21:40:41 +02:00
|
|
|
if (s->stats_version == 2 && s->stats_add_max) {
|
|
|
|
fprintf(s->stats_file, "max_avg:%d ", s->average_max);
|
2021-02-13 01:53:16 +02:00
|
|
|
for (int j = 0; j < s->nb_components; j++) {
|
|
|
|
int c = s->is_rgb ? s->rgba_map[j] : j;
|
2016-08-31 21:40:41 +02:00
|
|
|
fprintf(s->stats_file, "max_%c:%d ", s->comps[j], s->max[c]);
|
|
|
|
}
|
|
|
|
}
|
2013-07-06 03:21:12 +03:00
|
|
|
fprintf(s->stats_file, "\n");
|
|
|
|
}
|
|
|
|
|
2017-10-07 15:59:22 +02:00
|
|
|
return ff_filter_frame(ctx->outputs[0], master);
|
2013-07-06 03:21:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
PSNRContext *s = ctx->priv;
|
|
|
|
|
|
|
|
s->min_mse = +INFINITY;
|
|
|
|
s->max_mse = -INFINITY;
|
|
|
|
|
2015-10-23 00:00:40 +02:00
|
|
|
if (s->stats_file_str) {
|
2016-08-31 21:40:41 +02:00
|
|
|
if (s->stats_version < 2 && s->stats_add_max) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"stats_add_max was specified but stats_version < 2.\n" );
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2015-10-23 00:00:40 +02:00
|
|
|
if (!strcmp(s->stats_file_str, "-")) {
|
|
|
|
s->stats_file = stdout;
|
|
|
|
} else {
|
|
|
|
s->stats_file = fopen(s->stats_file_str, "w");
|
|
|
|
if (!s->stats_file) {
|
|
|
|
int err = AVERROR(errno);
|
|
|
|
char buf[128];
|
|
|
|
av_strerror(err, buf, sizeof(buf));
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Could not open stats file %s: %s\n",
|
|
|
|
s->stats_file_str, buf);
|
|
|
|
return err;
|
|
|
|
}
|
2013-07-06 03:21:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-31 12:44:13 +02:00
|
|
|
s->fs.on_event = do_psnr;
|
2013-07-06 03:21:12 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
2015-03-18 02:18:40 +02:00
|
|
|
static const enum AVPixelFormat pix_fmts[] = {
|
2018-09-09 19:05:56 +02:00
|
|
|
AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
|
2013-09-08 14:37:07 +03:00
|
|
|
#define PF_NOALPHA(suf) AV_PIX_FMT_YUV420##suf, AV_PIX_FMT_YUV422##suf, AV_PIX_FMT_YUV444##suf
|
|
|
|
#define PF_ALPHA(suf) AV_PIX_FMT_YUVA420##suf, AV_PIX_FMT_YUVA422##suf, AV_PIX_FMT_YUVA444##suf
|
|
|
|
#define PF(suf) PF_NOALPHA(suf), PF_ALPHA(suf)
|
|
|
|
PF(P), PF(P9), PF(P10), PF_NOALPHA(P12), PF_NOALPHA(P14), PF(P16),
|
|
|
|
AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
|
|
|
|
AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
|
|
|
|
AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
|
|
|
|
AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
|
|
|
|
AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
|
2017-12-20 13:45:21 +02:00
|
|
|
AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
|
2013-07-06 03:21:12 +03:00
|
|
|
AV_PIX_FMT_NONE
|
|
|
|
};
|
|
|
|
|
2021-08-10 01:25:31 +02:00
|
|
|
return ff_set_common_formats_from_list(ctx, pix_fmts);
|
2013-07-06 03:21:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int config_input_ref(AVFilterLink *inlink)
|
|
|
|
{
|
2013-09-08 13:53:55 +03:00
|
|
|
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
|
2013-07-06 03:21:12 +03:00
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
PSNRContext *s = ctx->priv;
|
2016-07-21 02:04:03 +02:00
|
|
|
double average_max;
|
2015-07-11 03:42:26 +02:00
|
|
|
unsigned sum;
|
2013-07-06 03:21:12 +03:00
|
|
|
int j;
|
|
|
|
|
2021-02-13 01:53:16 +02:00
|
|
|
s->nb_threads = ff_filter_get_nb_threads(ctx);
|
2013-09-08 13:53:55 +03:00
|
|
|
s->nb_components = desc->nb_components;
|
2013-07-06 03:21:12 +03:00
|
|
|
if (ctx->inputs[0]->w != ctx->inputs[1]->w ||
|
|
|
|
ctx->inputs[0]->h != ctx->inputs[1]->h) {
|
2014-02-07 11:33:56 +03:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Width and height of input videos must be same.\n");
|
2013-07-06 03:21:12 +03:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2015-09-08 17:10:48 +02:00
|
|
|
s->max[0] = (1 << desc->comp[0].depth) - 1;
|
|
|
|
s->max[1] = (1 << desc->comp[1].depth) - 1;
|
|
|
|
s->max[2] = (1 << desc->comp[2].depth) - 1;
|
|
|
|
s->max[3] = (1 << desc->comp[3].depth) - 1;
|
2013-07-06 03:21:12 +03:00
|
|
|
|
|
|
|
s->is_rgb = ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0;
|
|
|
|
s->comps[0] = s->is_rgb ? 'r' : 'y' ;
|
|
|
|
s->comps[1] = s->is_rgb ? 'g' : 'u' ;
|
|
|
|
s->comps[2] = s->is_rgb ? 'b' : 'v' ;
|
|
|
|
s->comps[3] = 'a';
|
|
|
|
|
2016-01-27 17:19:38 +02:00
|
|
|
s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
|
2013-09-08 13:53:55 +03:00
|
|
|
s->planeheight[0] = s->planeheight[3] = inlink->h;
|
2016-01-27 17:19:38 +02:00
|
|
|
s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
|
2013-09-08 13:53:55 +03:00
|
|
|
s->planewidth[0] = s->planewidth[3] = inlink->w;
|
2015-07-11 03:42:26 +02:00
|
|
|
sum = 0;
|
|
|
|
for (j = 0; j < s->nb_components; j++)
|
|
|
|
sum += s->planeheight[j] * s->planewidth[j];
|
2016-07-21 02:04:03 +02:00
|
|
|
average_max = 0;
|
2015-07-11 03:42:26 +02:00
|
|
|
for (j = 0; j < s->nb_components; j++) {
|
|
|
|
s->planeweight[j] = (double) s->planeheight[j] * s->planewidth[j] / sum;
|
2016-07-21 02:04:03 +02:00
|
|
|
average_max += s->max[j] * s->planeweight[j];
|
2015-07-11 03:42:26 +02:00
|
|
|
}
|
2016-07-21 02:04:03 +02:00
|
|
|
s->average_max = lrint(average_max);
|
2013-07-06 03:21:12 +03:00
|
|
|
|
2015-09-08 17:10:48 +02:00
|
|
|
s->dsp.sse_line = desc->comp[0].depth > 8 ? sse_line_16bit : sse_line_8bit;
|
2015-07-12 12:44:39 +02:00
|
|
|
if (ARCH_X86)
|
2015-09-08 17:10:48 +02:00
|
|
|
ff_psnr_init_x86(&s->dsp, desc->comp[0].depth);
|
2013-09-08 14:37:07 +03:00
|
|
|
|
2021-02-13 01:53:16 +02:00
|
|
|
s->score = av_calloc(s->nb_threads, sizeof(*s->score));
|
|
|
|
if (!s->score)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2021-03-13 04:35:02 +02:00
|
|
|
for (int t = 0; t < s->nb_threads; t++) {
|
2021-02-13 01:53:16 +02:00
|
|
|
s->score[t] = av_calloc(s->nb_components, sizeof(*s->score[0]));
|
|
|
|
if (!s->score[t])
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2013-07-06 03:21:12 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int config_output(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
2013-08-28 01:07:22 +03:00
|
|
|
PSNRContext *s = ctx->priv;
|
2013-07-06 03:21:12 +03:00
|
|
|
AVFilterLink *mainlink = ctx->inputs[0];
|
2013-08-28 01:07:22 +03:00
|
|
|
int ret;
|
2013-07-06 03:21:12 +03:00
|
|
|
|
2017-08-31 19:47:37 +02:00
|
|
|
ret = ff_framesync_init_dualinput(&s->fs, ctx);
|
2017-07-31 12:44:13 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2013-07-06 03:21:12 +03:00
|
|
|
outlink->w = mainlink->w;
|
|
|
|
outlink->h = mainlink->h;
|
|
|
|
outlink->time_base = mainlink->time_base;
|
|
|
|
outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
|
|
|
|
outlink->frame_rate = mainlink->frame_rate;
|
2017-08-31 19:47:37 +02:00
|
|
|
if ((ret = ff_framesync_configure(&s->fs)) < 0)
|
2013-08-28 01:07:22 +03:00
|
|
|
return ret;
|
2013-07-06 03:21:12 +03:00
|
|
|
|
2019-10-25 22:02:41 +02:00
|
|
|
outlink->time_base = s->fs.time_base;
|
|
|
|
|
2020-02-01 22:10:25 +02:00
|
|
|
if (av_cmp_q(mainlink->time_base, outlink->time_base) ||
|
2019-10-25 22:02:41 +02:00
|
|
|
av_cmp_q(ctx->inputs[1]->time_base, outlink->time_base))
|
|
|
|
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",
|
|
|
|
mainlink->time_base.num, mainlink->time_base.den,
|
|
|
|
ctx->inputs[1]->time_base.num, ctx->inputs[1]->time_base.den);
|
|
|
|
|
2013-07-06 03:21:12 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-31 12:44:13 +02:00
|
|
|
static int activate(AVFilterContext *ctx)
|
2013-07-06 03:21:12 +03:00
|
|
|
{
|
2017-07-31 12:44:13 +02:00
|
|
|
PSNRContext *s = ctx->priv;
|
2017-08-31 19:47:37 +02:00
|
|
|
return ff_framesync_activate(&s->fs);
|
2013-07-06 03:21:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
PSNRContext *s = ctx->priv;
|
|
|
|
|
|
|
|
if (s->nb_frames > 0) {
|
2015-07-11 03:42:26 +02:00
|
|
|
int j;
|
|
|
|
char buf[256];
|
|
|
|
|
|
|
|
buf[0] = 0;
|
2015-07-11 17:21:17 +02:00
|
|
|
for (j = 0; j < s->nb_components; j++) {
|
|
|
|
int c = s->is_rgb ? s->rgba_map[j] : j;
|
2015-10-22 21:25:38 +02:00
|
|
|
av_strlcatf(buf, sizeof(buf), " %c:%f", s->comps[j],
|
2015-07-11 17:21:17 +02:00
|
|
|
get_psnr(s->mse_comp[c], s->nb_frames, s->max[c]));
|
|
|
|
}
|
2015-10-22 21:25:38 +02:00
|
|
|
av_log(ctx, AV_LOG_INFO, "PSNR%s average:%f min:%f max:%f\n",
|
2015-07-11 03:42:26 +02:00
|
|
|
buf,
|
2013-07-06 03:21:12 +03:00
|
|
|
get_psnr(s->mse, s->nb_frames, s->average_max),
|
|
|
|
get_psnr(s->max_mse, 1, s->average_max),
|
|
|
|
get_psnr(s->min_mse, 1, s->average_max));
|
|
|
|
}
|
|
|
|
|
2017-08-31 19:47:37 +02:00
|
|
|
ff_framesync_uninit(&s->fs);
|
2021-02-13 01:53:16 +02:00
|
|
|
for (int t = 0; t < s->nb_threads && s->score; t++)
|
|
|
|
av_freep(&s->score[t]);
|
|
|
|
av_freep(&s->score);
|
2013-07-06 03:21:12 +03:00
|
|
|
|
2015-10-20 15:02:21 +02:00
|
|
|
if (s->stats_file && s->stats_file != stdout)
|
2013-07-06 03:21:12 +03:00
|
|
|
fclose(s->stats_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const AVFilterPad psnr_inputs[] = {
|
|
|
|
{
|
2013-09-07 15:13:50 +03:00
|
|
|
.name = "main",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2013-07-06 03:21:12 +03:00
|
|
|
},{
|
2013-09-07 15:13:50 +03:00
|
|
|
.name = "reference",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.config_props = config_input_ref,
|
2013-07-06 03:21:12 +03:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad psnr_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.config_props = config_output,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-04-19 18:33:56 +02:00
|
|
|
const AVFilter ff_vf_psnr = {
|
2013-09-07 15:13:50 +03:00
|
|
|
.name = "psnr",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Calculate the PSNR between two video streams."),
|
2017-07-31 12:44:13 +02:00
|
|
|
.preinit = psnr_framesync_preinit,
|
2013-09-07 15:13:50 +03:00
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
2017-07-31 12:44:13 +02:00
|
|
|
.activate = activate,
|
2013-09-07 15:13:50 +03:00
|
|
|
.priv_size = sizeof(PSNRContext),
|
|
|
|
.priv_class = &psnr_class,
|
2021-08-12 13:05:31 +02:00
|
|
|
FILTER_INPUTS(psnr_inputs),
|
|
|
|
FILTER_OUTPUTS(psnr_outputs),
|
2021-09-27 12:07:35 +02:00
|
|
|
FILTER_QUERY_FUNC(query_formats),
|
2021-02-13 01:53:16 +02:00
|
|
|
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
|
2013-07-06 03:21:12 +03:00
|
|
|
};
|