Go to the documentation of this file.
66 int jobnr,
int nb_jobs);
70 #define OFFSET(x) offsetof(SSIMContext, x)
71 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
95 const uint8_t *ref8, ptrdiff_t ref_stride,
96 int64_t (*sums)[4],
int width)
98 const uint16_t *main16 = (
const uint16_t *)main8;
99 const uint16_t *ref16 = (
const uint16_t *)ref8;
105 for (z = 0; z <
width; z++) {
106 uint64_t
s1 = 0,
s2 = 0,
ss = 0, s12 = 0;
108 for (y = 0; y < 4; y++) {
109 for (x = 0; x < 4; x++) {
110 unsigned a = main16[x + y * main_stride];
111 unsigned b = ref16[x + y * ref_stride];
131 const uint8_t *
ref, ptrdiff_t ref_stride,
132 int (*sums)[4],
int width)
136 for (z = 0; z <
width; z++) {
137 uint32_t
s1 = 0,
s2 = 0,
ss = 0, s12 = 0;
139 for (y = 0; y < 4; y++) {
140 for (x = 0; x < 4; x++) {
141 int a =
main[x + y * main_stride];
142 int b =
ref[x + y * ref_stride];
163 int64_t ssim_c1 = (int64_t)(.01*.01*
max*
max*64 + .5);
164 int64_t ssim_c2 = (int64_t)(.03*.03*
max*
max*64*63 + .5);
170 int64_t
vars = fss * 64 - fs1 * fs1 - fs2 * fs2;
171 int64_t covar = fs12 * 64 - fs1 * fs2;
173 return (
float)(2 * fs1 * fs2 + ssim_c1) * (
float)(2 * covar + ssim_c2)
174 / ((
float)(fs1 * fs1 + fs2 * fs2 + ssim_c1) * (
float)(
vars + ssim_c2));
179 static const int ssim_c1 = (
int)(.01*.01*255*255*64 + .5);
180 static const int ssim_c2 = (
int)(.03*.03*255*255*64*63 + .5);
186 int vars = fss * 64 - fs1 * fs1 - fs2 * fs2;
187 int covar = fs12 * 64 - fs1 * fs2;
189 return (
float)(2 * fs1 * fs2 + ssim_c1) * (
float)(2 * covar + ssim_c2)
190 / ((
float)(fs1 * fs1 + fs2 * fs2 + ssim_c1) * (
float)(
vars + ssim_c2));
199 ssim +=
ssim_end1x(sum0[
i][0] + sum0[
i + 1][0] + sum1[
i][0] + sum1[
i + 1][0],
200 sum0[
i][1] + sum0[
i + 1][1] + sum1[
i][1] + sum1[
i + 1][1],
201 sum0[
i][2] + sum0[
i + 1][2] + sum1[
i][2] + sum1[
i + 1][2],
202 sum0[
i][3] + sum0[
i + 1][3] + sum1[
i][3] + sum1[
i + 1][3],
213 ssim +=
ssim_end1(sum0[
i][0] + sum0[
i + 1][0] + sum1[
i][0] + sum1[
i + 1][0],
214 sum0[
i][1] + sum0[
i + 1][1] + sum1[
i][1] + sum1[
i + 1][1],
215 sum0[
i][2] + sum0[
i + 1][2] + sum1[
i][2] + sum1[
i + 1][2],
216 sum0[
i][3] + sum0[
i + 1][3] + sum1[
i][3] + sum1[
i + 1][3]);
220 #define SUM_LEN(w) (((w) >> 2) + 3)
237 int jobnr,
int nb_jobs)
240 double *score =
td->score[jobnr];
241 void *
temp =
td->temp[jobnr];
242 const int max =
td->max;
244 for (
int c = 0;
c <
td->nb_components;
c++) {
245 const uint8_t *main_data =
td->main_data[
c];
246 const uint8_t *ref_data =
td->ref_data[
c];
247 const int main_stride =
td->main_linesize[
c];
248 const int ref_stride =
td->ref_linesize[
c];
251 const int slice_start = ((
height >> 2) * jobnr) / nb_jobs;
253 const int ystart =
FFMAX(1, slice_start);
256 int64_t (*sum0)[4] =
temp;
262 for (
int y = ystart; y <
slice_end; y++) {
263 for (; z <= y; z++) {
264 FFSWAP(
void*, sum0, sum1);
266 &ref_data[4 * z * ref_stride], ref_stride,
280 int jobnr,
int nb_jobs)
283 double *score =
td->score[jobnr];
284 void *
temp =
td->temp[jobnr];
287 for (
int c = 0;
c <
td->nb_components;
c++) {
288 const uint8_t *main_data =
td->main_data[
c];
289 const uint8_t *ref_data =
td->ref_data[
c];
290 const int main_stride =
td->main_linesize[
c];
291 const int ref_stride =
td->ref_linesize[
c];
294 const int slice_start = ((
height >> 2) * jobnr) / nb_jobs;
296 const int ystart =
FFMAX(1, slice_start);
305 for (
int y = ystart; y <
slice_end; y++) {
306 for (; z <= y; z++) {
307 FFSWAP(
void*, sum0, sum1);
308 dsp->
ssim_4x4_line(&main_data[4 * z * main_stride], main_stride,
309 &ref_data[4 * z * ref_stride], ref_stride,
313 ssim += dsp->
ssim_end_line((
const int (*)[4])sum0, (
const int (*)[4])sum1,
width - 1);
333 double c[4] = {0}, ssimv = 0.0;
340 if (
ctx->is_disabled || !
ref)
342 metadata = &
master->metadata;
346 td.nb_components =
s->nb_components;
352 for (
int n = 0; n <
s->nb_components; n++) {
354 td.ref_data[n] =
ref->data[n];
355 td.main_linesize[n] =
master->linesize[n];
356 td.ref_linesize[n] =
ref->linesize[n];
357 td.planewidth[n] =
s->planewidth[n];
358 td.planeheight[n] =
s->planeheight[n];
361 if (
master->color_range !=
ref->color_range) {
363 "frames use different color ranges (%s != %s)\n",
369 FFMIN((
s->planeheight[1] + 3) >> 2,
s->nb_threads));
371 for (
i = 0;
i <
s->nb_components;
i++) {
372 for (
int j = 0; j <
s->nb_threads; j++)
373 c[
i] +=
s->score[j][
i];
374 c[
i] =
c[
i] / (((
s->planewidth[
i] >> 2) - 1) * ((
s->planeheight[
i] >> 2) - 1));
377 for (
i = 0;
i <
s->nb_components;
i++) {
378 ssimv +=
s->coefs[
i] *
c[
i];
382 for (
i = 0;
i <
s->nb_components;
i++) {
383 int cidx =
s->is_rgb ?
s->rgba_map[
i] :
i;
384 set_meta(metadata,
"lavfi.ssim.",
s->comps[
i],
c[cidx]);
386 s->ssim_total += ssimv;
388 set_meta(metadata,
"lavfi.ssim.All", 0, ssimv);
392 fprintf(
s->stats_file,
"n:%"PRId64
" ",
s->nb_frames);
394 for (
i = 0;
i <
s->nb_components;
i++) {
395 int cidx =
s->is_rgb ?
s->rgba_map[
i] :
i;
396 fprintf(
s->stats_file,
"%c:%f ",
s->comps[
i],
c[cidx]);
399 fprintf(
s->stats_file,
"All:%f (%f)\n", ssimv,
ssim_db(ssimv, 1.0));
409 if (
s->stats_file_str) {
410 if (!strcmp(
s->stats_file_str,
"-")) {
411 s->stats_file = stdout;
414 if (!
s->stats_file) {
419 s->stats_file_str, buf);
437 #define PF(suf) AV_PIX_FMT_YUV420##suf, AV_PIX_FMT_YUV422##suf, AV_PIX_FMT_YUV444##suf, AV_PIX_FMT_GBR##suf
450 s->nb_components =
desc->nb_components;
452 if (
ctx->inputs[0]->w !=
ctx->inputs[1]->w ||
453 ctx->inputs[0]->h !=
ctx->inputs[1]->h) {
459 s->comps[0] =
s->is_rgb ?
'R' :
'Y';
460 s->comps[1] =
s->is_rgb ?
'G' :
'U';
461 s->comps[2] =
s->is_rgb ?
'B' :
'V';
465 s->planeheight[0] =
s->planeheight[3] =
inlink->h;
467 s->planewidth[0] =
s->planewidth[3] =
inlink->w;
468 for (
i = 0;
i <
s->nb_components;
i++)
469 sum +=
s->planeheight[
i] *
s->planewidth[
i];
470 for (
i = 0;
i <
s->nb_components;
i++)
471 s->coefs[
i] = (
double)
s->planeheight[
i] *
s->planewidth[
i] / sum;
477 for (
int t = 0; t <
s->nb_threads; t++) {
482 s->max = (1 <<
desc->comp[0].depth) - 1;
495 for (
int t = 0; t <
s->nb_threads; t++) {
496 s->score[t] =
av_calloc(
s->nb_components,
sizeof(*
s->score[0]));
514 outlink->
w = mainlink->
w;
515 outlink->
h = mainlink->
h;
527 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",
529 ctx->inputs[1]->time_base.num,
ctx->inputs[1]->time_base.den);
544 if (
s->nb_frames > 0) {
548 for (
i = 0;
i <
s->nb_components;
i++) {
549 int c =
s->is_rgb ?
s->rgba_map[
i] :
i;
550 av_strlcatf(buf,
sizeof(buf),
" %c:%f (%f)",
s->comps[
i],
s->ssim[
c] /
s->nb_frames,
554 s->ssim_total /
s->nb_frames,
ssim_db(
s->ssim_total,
s->nb_frames));
559 if (
s->stats_file &&
s->stats_file != stdout)
560 fclose(
s->stats_file);
562 for (
int t = 0; t <
s->nb_threads &&
s->score; t++)
566 for (
int t = 0; t <
s->nb_threads &&
s->temp; t++)
593 .preinit = ssim_framesync_preinit,
598 .priv_class = &ssim_class,
int ff_framesync_configure(FFFrameSync *fs)
Configure a frame sync structure.
void ff_ssim_init_x86(SSIMDSPContext *dsp)
#define AV_LOG_WARNING
Something somehow does not look correct.
AVPixelFormat
Pixel format.
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
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)
static float ssim_end1x(int64_t s1, int64_t s2, int64_t ss, int64_t s12, int max)
#define FILTER_PIXFMTS_ARRAY(array)
static int activate(AVFilterContext *ctx)
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
static int config_input_ref(AVFilterLink *inlink)
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.
A link between two filters.
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
static const AVOption ssim_options[]
int av_strerror(int errnum, char *errbuf, size_t errbuf_size)
Put a description of the AVERROR code errnum in errbuf.
#define AV_PIX_FMT_GRAY16
#define ss(width, name, subs,...)
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...
static float ssim_endn_16bit(const int64_t(*sum0)[4], const int64_t(*sum1)[4], int width, int max)
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)
static double ssim_endn_8bit(const int(*sum0)[4], const int(*sum1)[4], int width)
#define AV_CEIL_RSHIFT(a, b)
static const AVFilterPad ssim_inputs[]
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
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,...
#define AV_PIX_FMT_GRAY14
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
#define FILTER_INPUTS(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...
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)
#define AV_PIX_FMT_GRAY10
const char * av_color_range_name(enum AVColorRange range)
Describe the class of an AVClass context structure.
static __device__ float fabs(float a)
#define fs(width, name, subs,...)
static const uint8_t vars[2][12]
static int ssim_plane(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
FRAMESYNC_DEFINE_CLASS(ssim, SSIMContext, fs)
@ 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 int weight(int i, int blen, int offset)
int main(int argc, char **argv)
#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 double ssim_db(double ssim, double weight)
const AVFilter ff_vf_ssim
static av_cold int init(AVFilterContext *ctx)
AVFilterContext * src
source filter
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
#define AV_LOG_INFO
Standard information.
static av_cold void uninit(AVFilterContext *ctx)
static const AVFilterPad ssim_outputs[]
static int do_ssim(FFFrameSync *fs)
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 float ssim_end1(int s1, int s2, int ss, int s12)
#define i(width, name, range_min, range_max)
int w
agreed upon image width
double(* ssim_end_line)(const int(*sum0)[4], const int(*sum1)[4], int w)
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Used for passing data between threads.
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 av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
static int config_output(AVFilterLink *outlink)
#define FFSWAP(type, a, b)
int(* ssim_plane)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
static enum AVPixelFormat pix_fmts[]
int h
agreed upon image height
#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]
static void set_meta(AVDictionary **metadata, const char *key, char comp, float d)
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link.
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
@ 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...
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
#define FILTER_OUTPUTS(array)
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
@ 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)
static int ssim_plane_16bit(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
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)
#define AV_PIX_FMT_GRAY12
static av_always_inline int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
const uint8_t * ref_data[4]
const uint8_t * main_data[4]