60 #define OFFSET(x) offsetof(VIFContext, x)
73 0.00745626912, 0.0142655009, 0.0250313189, 0.0402820669, 0.0594526194,
74 0.0804751068, 0.0999041125, 0.113746084, 0.118773937, 0.113746084,
75 0.0999041125, 0.0804751068, 0.0594526194, 0.0402820669, 0.0250313189,
76 0.0142655009, 0.00745626912
79 0.0189780835, 0.0558981746, 0.120920904, 0.192116052, 0.224173605,
80 0.192116052, 0.120920904, 0.0558981746, 0.0189780835
83 0.054488685, 0.244201347, 0.402619958, 0.244201347, 0.054488685
86 0.166378498, 0.667243004, 0.166378498
102 int src_stride,
int dst_stride)
104 const int dst_px_stride = dst_stride / 2;
106 for (
int i = 0;
i <
h / 2;
i++) {
107 for (
int j = 0; j <
w / 2; j++)
108 dst[
i * dst_px_stride + j] =
src[(
i * 2) * src_stride + (j * 2)];
113 const float *mu1_mu2,
const float *xx_filt,
114 const float *yy_filt,
const float *xy_filt,
115 float *num,
float *den,
int w,
int h)
117 static const float sigma_nsq = 2;
118 float mu1_sq_val, mu2_sq_val, mu1_mu2_val, xx_filt_val, yy_filt_val, xy_filt_val;
119 float sigma1_sq, sigma2_sq, sigma12,
g, sv_sq, eps = 1.0e-10f;
120 float gain_limit = 100.f;
121 float num_val, den_val;
122 float accum_num = 0.0f;
123 float accum_den = 0.0f;
125 for (
int i = 0;
i <
h;
i++) {
126 float accum_inner_num = 0.f;
127 float accum_inner_den = 0.f;
129 for (
int j = 0; j <
w; j++) {
130 mu1_sq_val = mu1_sq[
i *
w + j];
131 mu2_sq_val = mu2_sq[
i *
w + j];
132 mu1_mu2_val = mu1_mu2[
i *
w + j];
133 xx_filt_val = xx_filt[
i *
w + j];
134 yy_filt_val = yy_filt[
i *
w + j];
135 xy_filt_val = xy_filt[
i *
w + j];
137 sigma1_sq = xx_filt_val - mu1_sq_val;
138 sigma2_sq = yy_filt_val - mu2_sq_val;
139 sigma12 = xy_filt_val - mu1_mu2_val;
141 sigma1_sq =
FFMAX(sigma1_sq, 0.0f);
142 sigma2_sq =
FFMAX(sigma2_sq, 0.0f);
143 sigma12 =
FFMAX(sigma12, 0.0f);
145 g = sigma12 / (sigma1_sq + eps);
146 sv_sq = sigma2_sq -
g * sigma12;
148 if (sigma1_sq < eps) {
154 if (sigma2_sq < eps) {
163 sv_sq =
FFMAX(sv_sq, eps);
167 num_val =
log2f(1.0f +
g *
g * sigma1_sq / (sv_sq + sigma_nsq));
168 den_val =
log2f(1.0f + sigma1_sq / sigma_nsq);
171 num_val = den_val = 1.f;
173 accum_inner_num += num_val;
174 accum_inner_den += den_val;
177 accum_num += accum_inner_num;
178 accum_den += accum_inner_den;
185 static void vif_xx_yy_xy(
const float *x,
const float *y,
float *xx,
float *yy,
186 float *xy,
int w,
int h)
188 for (
int i = 0;
i <
h;
i++) {
189 for (
int j = 0; j <
w; j++) {
192 float xxval = xval * xval;
193 float yyval = yval * yval;
194 float xyval = xval * yval;
213 const float *
src =
td->src;
214 float *dst =
td->dst;
217 int src_stride =
td->src_stride;
218 int dst_stride =
td->dst_stride;
219 int filt_w =
td->filter_width;
220 float *
temp =
td->temp[jobnr];
226 for (
int j = 0; j <
w; j++) {
229 if (
i >= filt_w / 2 &&
i <
h - filt_w / 2 - 1) {
230 for (
int filt_i = 0; filt_i < filt_w; filt_i++) {
231 const float filt_coeff =
filter[filt_i];
233 int ii =
i - filt_w / 2 + filt_i;
235 img_coeff =
src[ii * src_stride + j];
236 sum += filt_coeff * img_coeff;
239 for (
int filt_i = 0; filt_i < filt_w; filt_i++) {
240 const float filt_coeff =
filter[filt_i];
241 int ii =
i - filt_w / 2 + filt_i;
244 ii = ii < 0 ? -ii : (ii >=
h ? 2 *
h - ii - 1 : ii);
246 img_coeff =
src[ii * src_stride + j];
247 sum += filt_coeff * img_coeff;
255 for (
int j = 0; j <
w; j++) {
258 if (j >= filt_w / 2 && j <
w - filt_w / 2 - 1) {
259 for (
int filt_j = 0; filt_j < filt_w; filt_j++) {
260 const float filt_coeff =
filter[filt_j];
261 int jj = j - filt_w / 2 + filt_j;
264 img_coeff =
temp[jj];
265 sum += filt_coeff * img_coeff;
268 for (
int filt_j = 0; filt_j < filt_w; filt_j++) {
269 const float filt_coeff =
filter[filt_j];
270 int jj = j - filt_w / 2 + filt_j;
273 jj = jj < 0 ? -jj : (jj >=
w ? 2 *
w - jj - 1 : jj);
275 img_coeff =
temp[jj];
276 sum += filt_coeff * img_coeff;
280 dst[
i * dst_stride + j] = sum;
288 const float *
ref,
const float *
main,
int w,
int h,
289 int ref_stride,
int main_stride,
float *score,
290 float *data_buf[14],
float **
temp,
294 float *ref_scale = data_buf[0];
295 float *main_scale = data_buf[1];
296 float *ref_sq = data_buf[2];
297 float *main_sq = data_buf[3];
298 float *ref_main = data_buf[4];
299 float *mu1 = data_buf[5];
300 float *mu2 = data_buf[6];
301 float *mu1_sq = data_buf[7];
302 float *mu2_sq = data_buf[8];
303 float *mu1_mu2 = data_buf[9];
304 float *ref_sq_filt = data_buf[10];
305 float *main_sq_filt = data_buf[11];
306 float *ref_main_filt = data_buf[12];
308 float *curr_ref_scale = (
float *)
ref;
309 float *curr_main_scale = (
float *)
main;
310 int curr_ref_stride = ref_stride;
311 int curr_main_stride = main_stride;
316 for (
int scale = 0; scale < 4; scale++) {
319 const int nb_threads =
FFMIN(
h, gnb_threads);
324 td.filter_width = filter_width;
327 td.src = curr_ref_scale;
331 td.src_stride = curr_ref_stride;
336 td.src = curr_main_scale;
338 td.src_stride = curr_main_stride;
341 vif_dec2(mu1, ref_scale, buf_valid_w, buf_valid_h,
w,
w);
342 vif_dec2(mu2, main_scale, buf_valid_w, buf_valid_h,
w,
w);
350 curr_ref_scale = ref_scale;
351 curr_main_scale = main_scale;
354 curr_main_stride =
w;
357 td.src = curr_ref_scale;
361 td.src_stride = curr_ref_stride;
366 td.src = curr_main_scale;
368 td.src_stride = curr_main_stride;
373 vif_xx_yy_xy(curr_ref_scale, curr_main_scale, ref_sq, main_sq, ref_main,
w,
h);
376 td.dst = ref_sq_filt;
381 td.dst = main_sq_filt;
386 td.dst = ref_main_filt;
389 vif_statistic(mu1_sq, mu2_sq, mu1_mu2, ref_sq_filt, main_sq_filt,
390 ref_main_filt, &num, &den,
w,
h);
392 score[scale] = den <= FLT_EPSILON ? 1.f : num / den;
398 #define offset_fn(type, bits) \
399 static void offset_##bits##bit(VIFContext *s, \
400 const AVFrame *ref, \
401 AVFrame *main, int stride)\
406 int ref_stride = ref->linesize[0]; \
407 int main_stride = main->linesize[0]; \
409 const type *ref_ptr = (const type *) ref->data[0]; \
410 const type *main_ptr = (const type *) main->data[0]; \
412 const float factor = s->factor; \
414 float *ref_ptr_data = s->ref_data; \
415 float *main_ptr_data = s->main_data; \
417 for (int i = 0; i < h; i++) { \
418 for (int j = 0; j < w; j++) { \
419 ref_ptr_data[j] = ref_ptr[j] * factor - 128.f; \
420 main_ptr_data[j] = main_ptr[j] * factor - 128.f; \
422 ref_ptr += ref_stride / sizeof(type); \
424 main_ptr += main_stride / sizeof(type); \
425 main_ptr_data += w; \
445 s->factor = 1.f / (1 << (
s->desc->comp[0].depth - 8));
446 if (
s->desc->comp[0].depth <= 8) {
453 s->ref_data,
s->main_data,
s->width,
454 s->height,
s->width,
s->width,
455 score,
s->data_buf,
s->temp,
458 set_meta(metadata,
"lavfi.vif.scale.0", score[0]);
459 set_meta(metadata,
"lavfi.vif.scale.1", score[1]);
460 set_meta(metadata,
"lavfi.vif.scale.2", score[2]);
461 set_meta(metadata,
"lavfi.vif.scale.3", score[3]);
463 for (
int i = 0;
i < 4;
i++) {
464 s->vif_min[
i] =
FFMIN(
s->vif_min[
i], score[
i]);
465 s->vif_max[
i] =
FFMAX(
s->vif_max[
i], score[
i]);
466 s->vif_sum[
i] += score[
i];
483 #define PF(suf) AV_PIX_FMT_YUV420##suf, AV_PIX_FMT_YUV422##suf, AV_PIX_FMT_YUV444##suf
499 if (
ctx->inputs[0]->w !=
ctx->inputs[1]->w ||
500 ctx->inputs[0]->h !=
ctx->inputs[1]->h) {
504 if (
ctx->inputs[0]->format !=
ctx->inputs[1]->format) {
510 s->width =
ctx->inputs[0]->w;
511 s->height =
ctx->inputs[0]->h;
514 for (
int i = 0;
i < 4;
i++) {
515 s->vif_min[
i] = DBL_MAX;
516 s->vif_max[
i] = -DBL_MAX;
519 for (
int i = 0;
i < 13;
i++) {
520 if (!(
s->data_buf[
i] =
av_calloc(
s->width,
s->height *
sizeof(
float))))
524 if (!(
s->ref_data =
av_calloc(
s->width,
s->height *
sizeof(
float))))
527 if (!(
s->main_data =
av_calloc(
s->width,
s->height *
sizeof(
float))))
530 if (!(
s->temp =
av_calloc(
s->nb_threads,
sizeof(
s->temp[0]))))
533 for (
int i = 0;
i <
s->nb_threads;
i++) {
553 if (
ctx->is_disabled || !ref_frame) {
554 out_frame = main_frame;
556 out_frame =
do_vif(
ctx, main_frame, ref_frame);
573 outlink->
w = mainlink->
w;
574 outlink->
h = mainlink->
h;
583 in[1].time_base =
ctx->inputs[1]->time_base;
606 if (
s->nb_frames > 0) {
607 for (
int i = 0;
i < 4;
i++)
609 i,
s->vif_sum[
i] /
s->nb_frames,
s->vif_min[
i],
s->vif_max[
i]);
612 for (
int i = 0;
i < 13;
i++)
618 for (
int i = 0;
i <
s->nb_threads &&
s->temp;
i++)
653 .priv_class = &vif_class,
static const AVFilterPad inputs[]
static const AVFilterPad outputs[]
static void set_meta(AVDictionary **metadata, int chan, const char *key, const char *fmt, double val)
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
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[])
#define flags(name, subs,...)
#define fs(width, name, subs,...)
static av_always_inline void filter(int16_t *output, ptrdiff_t out_stride, const int16_t *low, ptrdiff_t low_stride, const int16_t *high, ptrdiff_t high_stride, int len, int clip)
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.
void ff_framesync_uninit(FFFrameSync *fs)
Free all memory currently allocated.
int ff_framesync_init(FFFrameSync *fs, AVFilterContext *parent, unsigned nb_in)
Initialize a frame sync structure.
@ EXT_STOP
Completely stop all streams with this one.
#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.
#define AV_LOG_INFO
Standard information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
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_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
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.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Used for passing data between threads.
const AVPixFmtDescriptor * desc
static int ref[MAX_W *MAX_W]
static void vif_statistic(const float *mu1_sq, const float *mu2_sq, const float *mu1_mu2, const float *xx_filt, const float *yy_filt, const float *xy_filt, float *num, float *den, int w, int h)
static const AVOption vif_options[]
int ff_compute_vif2(AVFilterContext *ctx, const float *ref, const float *main, int w, int h, int ref_stride, int main_stride, float *score, float *data_buf[14], float **temp, int gnb_threads)
static const AVFilterPad vif_inputs[]
static int query_formats(AVFilterContext *ctx)
static int config_input_ref(AVFilterLink *inlink)
static const float vif_filter1d_table[4][17]
static int activate(AVFilterContext *ctx)
static av_cold void uninit(AVFilterContext *ctx)
AVFILTER_DEFINE_CLASS(vif)
static int config_output(AVFilterLink *outlink)
static void vif_dec2(const float *src, float *dst, int w, int h, int src_stride, int dst_stride)
#define offset_fn(type, bits)
static const uint8_t vif_filter1d_width1[4]
static const AVFilterPad vif_outputs[]
static void vif_xx_yy_xy(const float *x, const float *y, float *xx, float *yy, float *xy, int w, int h)
static int process_frame(FFFrameSync *fs)
static int vif_filter1d(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
static AVFrame * do_vif(AVFilterContext *ctx, AVFrame *main, const AVFrame *ref)