68 int jobnr,
int nb_jobs);
72 #define OFFSET(x) offsetof(SSIMContext, x)
73 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
97 const uint8_t *ref8, ptrdiff_t ref_stride,
100 const uint16_t *main16 = (
const uint16_t *)main8;
101 const uint16_t *ref16 = (
const uint16_t *)ref8;
107 for (z = 0; z <
width; z++) {
108 uint64_t
s1 = 0,
s2 = 0,
ss = 0, s12 = 0;
110 for (y = 0; y < 4; y++) {
111 for (x = 0; x < 4; x++) {
112 unsigned a = main16[x + y * main_stride];
113 unsigned b = ref16[x + y * ref_stride];
134 int (*sums)[4],
int width)
138 for (z = 0; z <
width; z++) {
139 uint32_t
s1 = 0,
s2 = 0,
ss = 0, s12 = 0;
141 for (y = 0; y < 4; y++) {
142 for (x = 0; x < 4; x++) {
143 int a =
main[x + y * main_stride];
144 int b =
ref[x + y * ref_stride];
173 int64_t covar = fs12 * 64 - fs1 * fs2;
175 return (
float)(2 * fs1 * fs2 + ssim_c1) * (
float)(2 * covar + ssim_c2)
176 / ((
float)(fs1 * fs1 + fs2 * fs2 + ssim_c1) * (
float)(
vars + ssim_c2));
181 static const int ssim_c1 = (
int)(.01*.01*255*255*64 + .5);
182 static const int ssim_c2 = (
int)(.03*.03*255*255*64*63 + .5);
188 int vars = fss * 64 - fs1 * fs1 - fs2 * fs2;
189 int covar = fs12 * 64 - fs1 * fs2;
191 return (
float)(2 * fs1 * fs2 + ssim_c1) * (
float)(2 * covar + ssim_c2)
192 / ((
float)(fs1 * fs1 + fs2 * fs2 + ssim_c1) * (
float)(
vars + ssim_c2));
201 ssim +=
ssim_end1x(sum0[
i][0] + sum0[
i + 1][0] + sum1[
i][0] + sum1[
i + 1][0],
202 sum0[
i][1] + sum0[
i + 1][1] + sum1[
i][1] + sum1[
i + 1][1],
203 sum0[
i][2] + sum0[
i + 1][2] + sum1[
i][2] + sum1[
i + 1][2],
204 sum0[
i][3] + sum0[
i + 1][3] + sum1[
i][3] + sum1[
i + 1][3],
215 ssim +=
ssim_end1(sum0[
i][0] + sum0[
i + 1][0] + sum1[
i][0] + sum1[
i + 1][0],
216 sum0[
i][1] + sum0[
i + 1][1] + sum1[
i][1] + sum1[
i + 1][1],
217 sum0[
i][2] + sum0[
i + 1][2] + sum1[
i][2] + sum1[
i + 1][2],
218 sum0[
i][3] + sum0[
i + 1][3] + sum1[
i][3] + sum1[
i + 1][3]);
222 #define SUM_LEN(w) (((w) >> 2) + 3)
239 int jobnr,
int nb_jobs)
242 double *score =
td->score[jobnr];
243 void *
temp =
td->temp[jobnr];
244 const int max =
td->max;
246 for (
int c = 0;
c <
td->nb_components;
c++) {
249 const int main_stride =
td->main_linesize[
c];
250 const int ref_stride =
td->ref_linesize[
c];
255 const int ystart =
FFMAX(1, slice_start);
264 for (
int y = ystart; y <
slice_end; y++) {
265 for (; z <= y; z++) {
266 FFSWAP(
void*, sum0, sum1);
268 &ref_data[4 * z * ref_stride], ref_stride,
282 int jobnr,
int nb_jobs)
285 double *score =
td->score[jobnr];
286 void *
temp =
td->temp[jobnr];
289 for (
int c = 0;
c <
td->nb_components;
c++) {
292 const int main_stride =
td->main_linesize[
c];
293 const int ref_stride =
td->ref_linesize[
c];
298 const int ystart =
FFMAX(1, slice_start);
307 for (
int y = ystart; y <
slice_end; y++) {
308 for (; z <= y; z++) {
309 FFSWAP(
void*, sum0, sum1);
310 dsp->
ssim_4x4_line(&main_data[4 * z * main_stride], main_stride,
311 &ref_data[4 * z * ref_stride], ref_stride,
315 ssim += dsp->
ssim_end_line((
const int (*)[4])sum0, (
const int (*)[4])sum1,
width - 1);
335 double c[4] = {0}, ssimv = 0.0;
342 if (
ctx->is_disabled || !
ref)
344 metadata = &
master->metadata;
348 td.nb_components =
s->nb_components;
354 for (
int n = 0; n <
s->nb_components; n++) {
356 td.ref_data[n] =
ref->data[n];
357 td.main_linesize[n] =
master->linesize[n];
358 td.ref_linesize[n] =
ref->linesize[n];
359 td.planewidth[n] =
s->planewidth[n];
360 td.planeheight[n] =
s->planeheight[n];
365 for (
i = 0;
i <
s->nb_components;
i++) {
366 for (
int j = 0; j <
s->nb_threads; j++)
367 c[
i] +=
s->score[j][
i];
368 c[
i] =
c[
i] / (((
s->planewidth[
i] >> 2) - 1) * ((
s->planeheight[
i] >> 2) - 1));
371 for (
i = 0;
i <
s->nb_components;
i++) {
372 ssimv +=
s->coefs[
i] *
c[
i];
376 for (
i = 0;
i <
s->nb_components;
i++) {
377 int cidx =
s->is_rgb ?
s->rgba_map[
i] :
i;
378 set_meta(metadata,
"lavfi.ssim.",
s->comps[
i],
c[cidx]);
380 s->ssim_total += ssimv;
382 set_meta(metadata,
"lavfi.ssim.All", 0, ssimv);
386 fprintf(
s->stats_file,
"n:%"PRId64
" ",
s->nb_frames);
388 for (
i = 0;
i <
s->nb_components;
i++) {
389 int cidx =
s->is_rgb ?
s->rgba_map[
i] :
i;
390 fprintf(
s->stats_file,
"%c:%f ",
s->comps[
i],
c[cidx]);
393 fprintf(
s->stats_file,
"All:%f (%f)\n", ssimv,
ssim_db(ssimv, 1.0));
403 if (
s->stats_file_str) {
404 if (!strcmp(
s->stats_file_str,
"-")) {
405 s->stats_file = stdout;
407 s->stats_file = fopen(
s->stats_file_str,
"w");
408 if (!
s->stats_file) {
413 s->stats_file_str, buf);
433 #define PF(suf) AV_PIX_FMT_YUV420##suf, AV_PIX_FMT_YUV422##suf, AV_PIX_FMT_YUV444##suf, AV_PIX_FMT_GBR##suf
452 s->nb_components =
desc->nb_components;
454 if (
ctx->inputs[0]->w !=
ctx->inputs[1]->w ||
455 ctx->inputs[0]->h !=
ctx->inputs[1]->h) {
459 if (
ctx->inputs[0]->format !=
ctx->inputs[1]->format) {
465 s->comps[0] =
s->is_rgb ?
'R' :
'Y';
466 s->comps[1] =
s->is_rgb ?
'G' :
'U';
467 s->comps[2] =
s->is_rgb ?
'B' :
'V';
471 s->planeheight[0] =
s->planeheight[3] = inlink->
h;
473 s->planewidth[0] =
s->planewidth[3] = inlink->
w;
474 for (
i = 0;
i <
s->nb_components;
i++)
475 sum +=
s->planeheight[
i] *
s->planewidth[
i];
476 for (
i = 0;
i <
s->nb_components;
i++)
477 s->coefs[
i] = (
double)
s->planeheight[
i] *
s->planewidth[
i] / sum;
483 for (
int t = 0; t <
s->nb_threads; t++) {
488 s->max = (1 <<
desc->comp[0].depth) - 1;
500 for (
int t = 0; t <
s->nb_threads &&
s->score; t++) {
501 s->score[t] =
av_calloc(
s->nb_components,
sizeof(*
s->score[0]));
519 outlink->
w = mainlink->
w;
520 outlink->
h = mainlink->
h;
532 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",
534 ctx->inputs[1]->time_base.num,
ctx->inputs[1]->time_base.den);
549 if (
s->nb_frames > 0) {
553 for (
i = 0;
i <
s->nb_components;
i++) {
554 int c =
s->is_rgb ?
s->rgba_map[
i] :
i;
555 av_strlcatf(buf,
sizeof(buf),
" %c:%f (%f)",
s->comps[
i],
s->ssim[
c] /
s->nb_frames,
559 s->ssim_total /
s->nb_frames,
ssim_db(
s->ssim_total,
s->nb_frames));
564 if (
s->stats_file &&
s->stats_file != stdout)
565 fclose(
s->stats_file);
567 for (
int t = 0; t <
s->nb_threads &&
s->score; t++)
571 for (
int t = 0; t <
s->nb_threads &&
s->temp; t++)
600 .preinit = ssim_framesync_preinit,
606 .priv_class = &ssim_class,
static const AVFilterPad inputs[]
static const AVFilterPad outputs[]
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Main libavfilter public API header.
int main(int argc, char *argv[])
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
static const uint8_t vars[2][12]
#define flags(name, subs,...)
#define ss(width, name, subs,...)
#define fs(width, name, subs,...)
#define FFSWAP(type, a, b)
#define AV_CEIL_RSHIFT(a, b)
static __device__ float fabs(float a)
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
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.
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 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...
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
int av_strerror(int errnum, char *errbuf, size_t errbuf_size)
Put a description of the AVERROR code errnum in errbuf.
#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.
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
void * av_mallocz_array(size_t nmemb, size_t size)
Allocate a memory block for an array with av_mallocz().
static int weight(int i, int blen, int offset)
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static enum AVPixelFormat pix_fmts[]
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
#define AV_PIX_FMT_GRAY12
AVPixelFormat
Pixel format.
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 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_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 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_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
void ff_ssim_init_x86(SSIMDSPContext *dsp)
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
AVRational frame_rate
Frame rate of the stream on the link, or 1/0 if unknown or variable; if left to 0/0,...
AVFilterContext * dst
dest filter
int format
agreed upon media format
A filter pad used for either input or output.
const char * name
Pad name.
const char * name
Filter name.
AVFormatInternal * internal
An opaque field for libavformat internal usage.
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...
int(* ssim_plane)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
double(* ssim_end_line)(const int(*sum0)[4], const int(*sum1)[4], int w)
void(* ssim_4x4_line)(const uint8_t *buf, ptrdiff_t buf_stride, const uint8_t *ref, ptrdiff_t ref_stride, int(*sums)[4], 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 int ssim_plane(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
static int do_ssim(FFFrameSync *fs)
FRAMESYNC_DEFINE_CLASS(ssim, SSIMContext, fs)
static float ssim_end1(int s1, int s2, int ss, int s12)
static int query_formats(AVFilterContext *ctx)
static double ssim_db(double ssim, double weight)
static void ssim_4x4xn_8bit(const uint8_t *main, ptrdiff_t main_stride, const uint8_t *ref, ptrdiff_t ref_stride, int(*sums)[4], int width)
static const AVOption ssim_options[]
static float ssim_endn_16bit(const int64_t(*sum0)[4], const int64_t(*sum1)[4], int width, int max)
static const AVFilterPad ssim_outputs[]
static int config_input_ref(AVFilterLink *inlink)
static void set_meta(AVDictionary **metadata, const char *key, char comp, float d)
static int activate(AVFilterContext *ctx)
static av_cold int init(AVFilterContext *ctx)
static av_cold void uninit(AVFilterContext *ctx)
static void ssim_4x4xn_16bit(const uint8_t *main8, ptrdiff_t main_stride, const uint8_t *ref8, ptrdiff_t ref_stride, int64_t(*sums)[4], int width)
static int config_output(AVFilterLink *outlink)
static double ssim_endn_8bit(const int(*sum0)[4], const int(*sum1)[4], int width)
static float ssim_end1x(int64_t s1, int64_t s2, int64_t ss, int64_t s12, int max)
static int ssim_plane_16bit(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
static const AVFilterPad ssim_inputs[]