FFmpeg  4.4.9
vf_vif.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Ronald S. Bultje <rsbultje@gmail.com>
3  * Copyright (c) 2017 Ashish Pratap Singh <ashk43712@gmail.com>
4  * Copyright (c) 2021 Paul B Mahol
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 /**
24  * @file
25  * Calculate VIF between two input videos.
26  */
27 
28 #include <float.h>
29 
30 #include "libavutil/avstring.h"
31 #include "libavutil/internal.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/pixdesc.h"
34 #include "avfilter.h"
35 #include "filters.h"
36 #include "framesync.h"
37 #include "drawutils.h"
38 #include "formats.h"
39 #include "internal.h"
40 #include "vif.h"
41 #include "video.h"
42 
43 typedef struct VIFContext {
44  const AVClass *class;
47  int width;
48  int height;
50  float factor;
51  float *data_buf[13];
52  float **temp;
53  float *ref_data;
54  float *main_data;
55  double vif_sum[4];
56  double vif_min[4];
57  double vif_max[4];
58  uint64_t nb_frames;
59 } VIFContext;
60 
61 #define OFFSET(x) offsetof(VIFContext, x)
62 
63 static const AVOption vif_options[] = {
64  { NULL }
65 };
66 
68 
69 static const uint8_t vif_filter1d_width1[4] = { 17, 9, 5, 3 };
70 
71 static const float vif_filter1d_table[4][17] =
72 {
73  {
74  0.00745626912, 0.0142655009, 0.0250313189, 0.0402820669, 0.0594526194,
75  0.0804751068, 0.0999041125, 0.113746084, 0.118773937, 0.113746084,
76  0.0999041125, 0.0804751068, 0.0594526194, 0.0402820669, 0.0250313189,
77  0.0142655009, 0.00745626912
78  },
79  {
80  0.0189780835, 0.0558981746, 0.120920904, 0.192116052, 0.224173605,
81  0.192116052, 0.120920904, 0.0558981746, 0.0189780835
82  },
83  {
84  0.054488685, 0.244201347, 0.402619958, 0.244201347, 0.054488685
85  },
86  {
87  0.166378498, 0.667243004, 0.166378498
88  }
89 };
90 
91 typedef struct ThreadData {
92  const float *filter;
93  const float *src;
94  float *dst;
95  int w, h;
96  int src_stride;
97  int dst_stride;
99  float **temp;
100 } ThreadData;
101 
102 static void vif_dec2(const float *src, float *dst, int w, int h,
103  int src_stride, int dst_stride)
104 {
105  const int dst_px_stride = dst_stride / 2;
106 
107  for (int i = 0; i < h / 2; i++) {
108  for (int j = 0; j < w / 2; j++)
109  dst[i * dst_px_stride + j] = src[(i * 2) * src_stride + (j * 2)];
110  }
111 }
112 
113 static void vif_statistic(const float *mu1_sq, const float *mu2_sq,
114  const float *mu1_mu2, const float *xx_filt,
115  const float *yy_filt, const float *xy_filt,
116  float *num, float *den, int w, int h)
117 {
118  static const float sigma_nsq = 2;
119  float mu1_sq_val, mu2_sq_val, mu1_mu2_val, xx_filt_val, yy_filt_val, xy_filt_val;
120  float sigma1_sq, sigma2_sq, sigma12, g, sv_sq, eps = 1.0e-10f;
121  float gain_limit = 100.f;
122  float num_val, den_val;
123  float accum_num = 0.0f;
124  float accum_den = 0.0f;
125 
126  for (int i = 0; i < h; i++) {
127  float accum_inner_num = 0.f;
128  float accum_inner_den = 0.f;
129 
130  for (int j = 0; j < w; j++) {
131  mu1_sq_val = mu1_sq[i * w + j];
132  mu2_sq_val = mu2_sq[i * w + j];
133  mu1_mu2_val = mu1_mu2[i * w + j];
134  xx_filt_val = xx_filt[i * w + j];
135  yy_filt_val = yy_filt[i * w + j];
136  xy_filt_val = xy_filt[i * w + j];
137 
138  sigma1_sq = xx_filt_val - mu1_sq_val;
139  sigma2_sq = yy_filt_val - mu2_sq_val;
140  sigma12 = xy_filt_val - mu1_mu2_val;
141 
142  sigma1_sq = FFMAX(sigma1_sq, 0.0f);
143  sigma2_sq = FFMAX(sigma2_sq, 0.0f);
144  sigma12 = FFMAX(sigma12, 0.0f);
145 
146  g = sigma12 / (sigma1_sq + eps);
147  sv_sq = sigma2_sq - g * sigma12;
148 
149  if (sigma1_sq < eps) {
150  g = 0.0f;
151  sv_sq = sigma2_sq;
152  sigma1_sq = 0.0f;
153  }
154 
155  if (sigma2_sq < eps) {
156  g = 0.0f;
157  sv_sq = 0.0f;
158  }
159 
160  if (g < 0.0f) {
161  sv_sq = sigma2_sq;
162  g = 0.0f;
163  }
164  sv_sq = FFMAX(sv_sq, eps);
165 
166  g = FFMIN(g, gain_limit);
167 
168  num_val = log2f(1.0f + g * g * sigma1_sq / (sv_sq + sigma_nsq));
169  den_val = log2f(1.0f + sigma1_sq / sigma_nsq);
170 
171  if (isnan(den_val))
172  num_val = den_val = 1.f;
173 
174  accum_inner_num += num_val;
175  accum_inner_den += den_val;
176  }
177 
178  accum_num += accum_inner_num;
179  accum_den += accum_inner_den;
180  }
181 
182  num[0] = accum_num;
183  den[0] = accum_den;
184 }
185 
186 static void vif_xx_yy_xy(const float *x, const float *y, float *xx, float *yy,
187  float *xy, int w, int h)
188 {
189  for (int i = 0; i < h; i++) {
190  for (int j = 0; j < w; j++) {
191  float xval = x[j];
192  float yval = y[j];
193  float xxval = xval * xval;
194  float yyval = yval * yval;
195  float xyval = xval * yval;
196 
197  xx[j] = xxval;
198  yy[j] = yyval;
199  xy[j] = xyval;
200  }
201 
202  xx += w;
203  yy += w;
204  xy += w;
205  x += w;
206  y += w;
207  }
208 }
209 
210 static int vif_filter1d(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
211 {
212  ThreadData *td = arg;
213  const float *filter = td->filter;
214  const float *src = td->src;
215  float *dst = td->dst;
216  int w = td->w;
217  int h = td->h;
218  int src_stride = td->src_stride;
219  int dst_stride = td->dst_stride;
220  int filt_w = td->filter_width;
221  float *temp = td->temp[jobnr];
222  const int slice_start = ff_slice_pos(h, jobnr, nb_jobs);
223  const int slice_end = ff_slice_pos(h, jobnr + 1, nb_jobs);
224 
225  for (int i = slice_start; i < slice_end; i++) {
226  /** Vertical pass. */
227  for (int j = 0; j < w; j++) {
228  float sum = 0.f;
229 
230  if (i >= filt_w / 2 && i < h - filt_w / 2 - 1) {
231  for (int filt_i = 0; filt_i < filt_w; filt_i++) {
232  const float filt_coeff = filter[filt_i];
233  float img_coeff;
234  int ii = i - filt_w / 2 + filt_i;
235 
236  img_coeff = src[ii * src_stride + j];
237  sum += filt_coeff * img_coeff;
238  }
239  } else {
240  for (int filt_i = 0; filt_i < filt_w; filt_i++) {
241  const float filt_coeff = filter[filt_i];
242  int ii = i - filt_w / 2 + filt_i;
243  float img_coeff;
244 
245  ii = avpriv_mirror(ii, h - 1);
246 
247  img_coeff = src[ii * src_stride + j];
248  sum += filt_coeff * img_coeff;
249  }
250  }
251 
252  temp[j] = sum;
253  }
254 
255  /** Horizontal pass. */
256  for (int j = 0; j < w; j++) {
257  float sum = 0.f;
258 
259  if (j >= filt_w / 2 && j < w - filt_w / 2 - 1) {
260  for (int filt_j = 0; filt_j < filt_w; filt_j++) {
261  const float filt_coeff = filter[filt_j];
262  int jj = j - filt_w / 2 + filt_j;
263  float img_coeff;
264 
265  img_coeff = temp[jj];
266  sum += filt_coeff * img_coeff;
267  }
268  } else {
269  for (int filt_j = 0; filt_j < filt_w; filt_j++) {
270  const float filt_coeff = filter[filt_j];
271  int jj = j - filt_w / 2 + filt_j;
272  float img_coeff;
273 
274  jj = avpriv_mirror(jj, w - 1);
275 
276  img_coeff = temp[jj];
277  sum += filt_coeff * img_coeff;
278  }
279  }
280 
281  dst[i * dst_stride + j] = sum;
282  }
283  }
284 
285  return 0;
286 }
287 
289  const float *ref, const float *main, int w, int h,
290  int ref_stride, int main_stride, float *score,
291  float *data_buf[14], float **temp,
292  int gnb_threads)
293 {
294  ThreadData td;
295  float *ref_scale = data_buf[0];
296  float *main_scale = data_buf[1];
297  float *ref_sq = data_buf[2];
298  float *main_sq = data_buf[3];
299  float *ref_main = data_buf[4];
300  float *mu1 = data_buf[5];
301  float *mu2 = data_buf[6];
302  float *mu1_sq = data_buf[7];
303  float *mu2_sq = data_buf[8];
304  float *mu1_mu2 = data_buf[9];
305  float *ref_sq_filt = data_buf[10];
306  float *main_sq_filt = data_buf[11];
307  float *ref_main_filt = data_buf[12];
308 
309  float *curr_ref_scale = (float *)ref;
310  float *curr_main_scale = (float *)main;
311  int curr_ref_stride = ref_stride;
312  int curr_main_stride = main_stride;
313 
314  float num = 0.f;
315  float den = 0.f;
316 
317  for (int scale = 0; scale < 4; scale++) {
318  const float *filter = vif_filter1d_table[scale];
319  int filter_width = vif_filter1d_width1[scale];
320  const int nb_threads = FFMIN(h, gnb_threads);
321  int buf_valid_w = w;
322  int buf_valid_h = h;
323 
324  td.filter = filter;
325  td.filter_width = filter_width;
326 
327  if (scale > 0) {
328  td.src = curr_ref_scale;
329  td.dst = mu1;
330  td.w = w;
331  td.h = h;
332  td.src_stride = curr_ref_stride;
333  td.dst_stride = w;
334  td.temp = temp;
335  ctx->internal->execute(ctx, vif_filter1d, &td, NULL, nb_threads);
336 
337  td.src = curr_main_scale;
338  td.dst = mu2;
339  td.src_stride = curr_main_stride;
340  ctx->internal->execute(ctx, vif_filter1d, &td, NULL, nb_threads);
341 
342  vif_dec2(mu1, ref_scale, buf_valid_w, buf_valid_h, w, w);
343  vif_dec2(mu2, main_scale, buf_valid_w, buf_valid_h, w, w);
344 
345  w = buf_valid_w / 2;
346  h = buf_valid_h / 2;
347 
348  buf_valid_w = w;
349  buf_valid_h = h;
350 
351  curr_ref_scale = ref_scale;
352  curr_main_scale = main_scale;
353 
354  curr_ref_stride = w;
355  curr_main_stride = w;
356  }
357 
358  td.src = curr_ref_scale;
359  td.dst = mu1;
360  td.w = w;
361  td.h = h;
362  td.src_stride = curr_ref_stride;
363  td.dst_stride = w;
364  td.temp = temp;
365  ctx->internal->execute(ctx, vif_filter1d, &td, NULL, nb_threads);
366 
367  td.src = curr_main_scale;
368  td.dst = mu2;
369  td.src_stride = curr_main_stride;
370  ctx->internal->execute(ctx, vif_filter1d, &td, NULL, nb_threads);
371 
372  vif_xx_yy_xy(mu1, mu2, mu1_sq, mu2_sq, mu1_mu2, w, h);
373 
374  vif_xx_yy_xy(curr_ref_scale, curr_main_scale, ref_sq, main_sq, ref_main, w, h);
375 
376  td.src = ref_sq;
377  td.dst = ref_sq_filt;
378  td.src_stride = w;
379  ctx->internal->execute(ctx, vif_filter1d, &td, NULL, nb_threads);
380 
381  td.src = main_sq;
382  td.dst = main_sq_filt;
383  td.src_stride = w;
384  ctx->internal->execute(ctx, vif_filter1d, &td, NULL, nb_threads);
385 
386  td.src = ref_main;
387  td.dst = ref_main_filt;
388  ctx->internal->execute(ctx, vif_filter1d, &td, NULL, nb_threads);
389 
390  vif_statistic(mu1_sq, mu2_sq, mu1_mu2, ref_sq_filt, main_sq_filt,
391  ref_main_filt, &num, &den, w, h);
392 
393  score[scale] = den <= FLT_EPSILON ? 1.f : num / den;
394  }
395 
396  return 0;
397 }
398 
399 #define offset_fn(type, bits) \
400 static void offset_##bits##bit(VIFContext *s, \
401  const AVFrame *ref, \
402  AVFrame *main, int stride)\
403 { \
404  int w = s->width; \
405  int h = s->height; \
406  \
407  int ref_stride = ref->linesize[0]; \
408  int main_stride = main->linesize[0]; \
409  \
410  const type *ref_ptr = (const type *) ref->data[0]; \
411  const type *main_ptr = (const type *) main->data[0]; \
412  \
413  const float factor = s->factor; \
414  \
415  float *ref_ptr_data = s->ref_data; \
416  float *main_ptr_data = s->main_data; \
417  \
418  for (int i = 0; i < h; i++) { \
419  for (int j = 0; j < w; j++) { \
420  ref_ptr_data[j] = ref_ptr[j] * factor - 128.f; \
421  main_ptr_data[j] = main_ptr[j] * factor - 128.f; \
422  } \
423  ref_ptr += ref_stride / sizeof(type); \
424  ref_ptr_data += w; \
425  main_ptr += main_stride / sizeof(type); \
426  main_ptr_data += w; \
427  } \
428 }
429 
431 offset_fn(uint16_t, 16)
432 
433 static void set_meta(AVDictionary **metadata, const char *key, float d)
434 {
435  char value[257];
436  snprintf(value, sizeof(value), "%f", d);
437  av_dict_set(metadata, key, value, 0);
438 }
439 
441 {
442  VIFContext *s = ctx->priv;
443  AVDictionary **metadata = &main->metadata;
444  float score[4];
445 
446  s->factor = 1.f / (1 << (s->desc->comp[0].depth - 8));
447  if (s->desc->comp[0].depth <= 8) {
448  offset_8bit(s, ref, main, s->width);
449  } else {
450  offset_16bit(s, ref, main, s->width);
451  }
452 
454  s->ref_data, s->main_data, s->width,
455  s->height, s->width, s->width,
456  score, s->data_buf, s->temp,
457  s->nb_threads);
458 
459  set_meta(metadata, "lavfi.vif.scale.0", score[0]);
460  set_meta(metadata, "lavfi.vif.scale.1", score[1]);
461  set_meta(metadata, "lavfi.vif.scale.2", score[2]);
462  set_meta(metadata, "lavfi.vif.scale.3", score[3]);
463 
464  for (int i = 0; i < 4; i++) {
465  s->vif_min[i] = FFMIN(s->vif_min[i], score[i]);
466  s->vif_max[i] = FFMAX(s->vif_max[i], score[i]);
467  s->vif_sum[i] += score[i];
468  }
469 
470  s->nb_frames++;
471 
472  return main;
473 }
474 
476 {
477  static const enum AVPixelFormat pix_fmts[] = {
484 #define PF(suf) AV_PIX_FMT_YUV420##suf, AV_PIX_FMT_YUV422##suf, AV_PIX_FMT_YUV444##suf
485  PF(P9), PF(P10), PF(P12), PF(P14), PF(P16),
487  };
488 
490  if (!fmts_list)
491  return AVERROR(ENOMEM);
492  return ff_set_common_formats(ctx, fmts_list);
493 }
494 
495 static int config_input_ref(AVFilterLink *inlink)
496 {
497  AVFilterContext *ctx = inlink->dst;
498  VIFContext *s = ctx->priv;
499 
500  if (ctx->inputs[0]->w != ctx->inputs[1]->w ||
501  ctx->inputs[0]->h != ctx->inputs[1]->h) {
502  av_log(ctx, AV_LOG_ERROR, "Width and height of input videos must be same.\n");
503  return AVERROR(EINVAL);
504  }
505  if (ctx->inputs[0]->format != ctx->inputs[1]->format) {
506  av_log(ctx, AV_LOG_ERROR, "Inputs must be of same pixel format.\n");
507  return AVERROR(EINVAL);
508  }
509 
510  s->desc = av_pix_fmt_desc_get(inlink->format);
511  s->width = ctx->inputs[0]->w;
512  s->height = ctx->inputs[0]->h;
513  s->nb_threads = ff_filter_get_nb_threads(ctx);
514 
515  for (int i = 0; i < 4; i++) {
516  s->vif_min[i] = DBL_MAX;
517  s->vif_max[i] = -DBL_MAX;
518  }
519 
520  for (int i = 0; i < 13; i++) {
521  if (!(s->data_buf[i] = av_calloc(s->width, s->height * sizeof(float))))
522  return AVERROR(ENOMEM);
523  }
524 
525  if (!(s->ref_data = av_calloc(s->width, s->height * sizeof(float))))
526  return AVERROR(ENOMEM);
527 
528  if (!(s->main_data = av_calloc(s->width, s->height * sizeof(float))))
529  return AVERROR(ENOMEM);
530 
531  if (!(s->temp = av_calloc(s->nb_threads, sizeof(s->temp[0]))))
532  return AVERROR(ENOMEM);
533 
534  for (int i = 0; i < s->nb_threads; i++) {
535  if (!(s->temp[i] = av_calloc(s->width, sizeof(float))))
536  return AVERROR(ENOMEM);
537  }
538 
539  return 0;
540 }
541 
543 {
544  AVFilterContext *ctx = fs->parent;
545  VIFContext *s = fs->opaque;
546  AVFilterLink *outlink = ctx->outputs[0];
547  AVFrame *out_frame, *main_frame = NULL, *ref_frame = NULL;
548  int ret;
549 
550  ret = ff_framesync_dualinput_get(fs, &main_frame, &ref_frame);
551  if (ret < 0)
552  return ret;
553 
554  if (ctx->is_disabled || !ref_frame) {
555  out_frame = main_frame;
556  } else {
557  out_frame = do_vif(ctx, main_frame, ref_frame);
558  }
559 
560  out_frame->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
561 
562  return ff_filter_frame(outlink, out_frame);
563 }
564 
565 
566 static int config_output(AVFilterLink *outlink)
567 {
568  AVFilterContext *ctx = outlink->src;
569  VIFContext *s = ctx->priv;
570  AVFilterLink *mainlink = ctx->inputs[0];
571  FFFrameSyncIn *in;
572  int ret;
573 
574  outlink->w = mainlink->w;
575  outlink->h = mainlink->h;
576  outlink->time_base = mainlink->time_base;
577  outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
578  outlink->frame_rate = mainlink->frame_rate;
579  if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0)
580  return ret;
581 
582  in = s->fs.in;
583  in[0].time_base = mainlink->time_base;
584  in[1].time_base = ctx->inputs[1]->time_base;
585  in[0].sync = 2;
586  in[0].before = EXT_STOP;
587  in[0].after = EXT_STOP;
588  in[1].sync = 1;
589  in[1].before = EXT_STOP;
590  in[1].after = EXT_STOP;
591  s->fs.opaque = s;
592  s->fs.on_event = process_frame;
593 
594  return ff_framesync_configure(&s->fs);
595 }
596 
598 {
599  VIFContext *s = ctx->priv;
600  return ff_framesync_activate(&s->fs);
601 }
602 
604 {
605  VIFContext *s = ctx->priv;
606 
607  if (s->nb_frames > 0) {
608  for (int i = 0; i < 4; i++)
609  av_log(ctx, AV_LOG_INFO, "VIF scale=%d average:%f min:%f: max:%f\n",
610  i, s->vif_sum[i] / s->nb_frames, s->vif_min[i], s->vif_max[i]);
611  }
612 
613  for (int i = 0; i < 13; i++)
614  av_freep(&s->data_buf[i]);
615 
616  av_freep(&s->ref_data);
617  av_freep(&s->main_data);
618 
619  for (int i = 0; i < s->nb_threads && s->temp; i++)
620  av_freep(&s->temp[i]);
621 
622  av_freep(&s->temp);
623 
624  ff_framesync_uninit(&s->fs);
625 }
626 
627 static const AVFilterPad vif_inputs[] = {
628  {
629  .name = "main",
630  .type = AVMEDIA_TYPE_VIDEO,
631  },{
632  .name = "reference",
633  .type = AVMEDIA_TYPE_VIDEO,
634  .config_props = config_input_ref,
635  },
636  { NULL }
637 };
638 
639 static const AVFilterPad vif_outputs[] = {
640  {
641  .name = "default",
642  .type = AVMEDIA_TYPE_VIDEO,
643  .config_props = config_output,
644  },
645  { NULL }
646 };
647 
649  .name = "vif",
650  .description = NULL_IF_CONFIG_SMALL("Calculate the VIF between two video streams."),
651  .uninit = uninit,
652  .query_formats = query_formats,
653  .priv_size = sizeof(VIFContext),
654  .priv_class = &vif_class,
655  .activate = activate,
656  .inputs = vif_inputs,
657  .outputs = vif_outputs,
659 };
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
static void set_meta(AVDictionary **metadata, int chan, const char *key, const char *fmt, double val)
Definition: af_astats.c:393
#define av_cold
Definition: attributes.h:88
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
uint8_t
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1099
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:805
Main libavfilter public API header.
int main(int argc, char *argv[])
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define s(width, name)
Definition: cbs_vp9.c:257
#define fs(width, name, subs,...)
Definition: cbs_vp9.c:259
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)
Definition: cfhddsp.c:27
#define FFMIN(a, b)
Definition: common.h:105
#define FFMAX(a, b)
Definition: common.h:103
#define NULL
Definition: coverity.c:32
misc drawing utilities
double value
Definition: eval.c:100
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.
Definition: filters.h:282
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:587
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:286
int ff_framesync_configure(FFFrameSync *fs)
Configure a frame sync structure.
Definition: framesync.c:124
int ff_framesync_dualinput_get(FFFrameSync *fs, AVFrame **f0, AVFrame **f1)
Definition: framesync.c:379
int ff_framesync_activate(FFFrameSync *fs)
Examine the frames in the filter's input and try to produce output.
Definition: framesync.c:341
void ff_framesync_uninit(FFFrameSync *fs)
Free all memory currently allocated.
Definition: framesync.c:290
int ff_framesync_init(FFFrameSync *fs, AVFilterContext *parent, unsigned nb_in)
Initialize a frame sync structure.
Definition: framesync.c:84
@ EXT_STOP
Completely stop all streams with this one.
Definition: framesync.h:65
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:117
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
Definition: avfilter.h:134
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:70
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_INFO
Standard information.
Definition: log.h:205
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:245
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
const char * key
int i
Definition: input.c:407
const char * arg
Definition: jacosubdec.c:67
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
static av_always_inline av_const int avpriv_mirror(int x, int w)
Definition: internal.h:269
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:309
#define isnan(x)
Definition: libm.h:340
#define log2f(x)
Definition: libm.h:409
uint8_t w
Definition: llviddspenc.c:39
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:2035
AVOptions.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
#define AV_PIX_FMT_GRAY9
Definition: pixfmt.h:379
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:381
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:99
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
@ AV_PIX_FMT_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
Definition: pixfmt.h:100
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:72
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
@ AV_PIX_FMT_YUVJ411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
Definition: pixfmt.h:258
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:79
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:80
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:78
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:380
#define AV_PIX_FMT_GRAY14
Definition: pixfmt.h:382
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:383
#define td
Definition: regdef.h:70
#define snprintf
Definition: snprintf.h:34
Describe the class of an AVClass context structure.
Definition: log.h:67
An instance of a filter.
Definition: avfilter.h:341
A list of supported formats for one end of a filter link.
Definition: formats.h:65
A filter pad used for either input or output.
Definition: internal.h:54
const char * name
Pad name.
Definition: internal.h:60
Filter definition.
Definition: avfilter.h:145
const char * name
Filter name.
Definition: avfilter.h:149
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1699
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:411
AVOption.
Definition: opt.h:248
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
Input stream structure.
Definition: framesync.h:81
Frame sync structure.
Definition: framesync.h:146
Used for passing data between threads.
Definition: dsddec.c:67
int filter_width
Definition: vf_vif.c:98
int src_stride
Definition: vf_unsharp.c:56
const float * filter
Definition: vf_vif.c:92
float ** temp
Definition: vf_vif.c:99
const float * src
Definition: vf_vif.c:93
int dst_stride
Definition: vf_unsharp.c:55
AVFrame * dst
Definition: vf_blend.c:57
float factor
Definition: vf_vif.c:50
int width
Definition: vf_vif.c:47
int nb_threads
Definition: vf_vif.c:49
uint64_t nb_frames
Definition: vf_vif.c:58
double vif_sum[4]
Definition: vf_vif.c:55
double vif_max[4]
Definition: vf_vif.c:57
float * data_buf[13]
Definition: vf_vif.c:51
FFFrameSync fs
Definition: vf_vif.c:45
int height
Definition: vf_vif.c:48
float ** temp
Definition: vf_vif.c:52
double vif_min[4]
Definition: vf_vif.c:56
const AVPixFmtDescriptor * desc
Definition: vf_vif.c:46
float * main_data
Definition: vf_vif.c:54
float * ref_data
Definition: vf_vif.c:53
#define av_freep(p)
#define av_log(a,...)
#define src
Definition: vp8dsp.c:255
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
AVFormatContext * ctx
Definition: movenc.c:48
const char * g
Definition: vf_curves.c:118
else temp
Definition: vf_mcdeint.c:259
AVFilter ff_vf_vif
Definition: vf_vif.c:648
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)
Definition: vf_vif.c:113
static const AVOption vif_options[]
Definition: vf_vif.c:63
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)
Definition: vf_vif.c:288
static const AVFilterPad vif_inputs[]
Definition: vf_vif.c:627
static int query_formats(AVFilterContext *ctx)
Definition: vf_vif.c:475
static int config_input_ref(AVFilterLink *inlink)
Definition: vf_vif.c:495
#define PF(suf)
static const float vif_filter1d_table[4][17]
Definition: vf_vif.c:71
static int activate(AVFilterContext *ctx)
Definition: vf_vif.c:597
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_vif.c:603
AVFILTER_DEFINE_CLASS(vif)
static int config_output(AVFilterLink *outlink)
Definition: vf_vif.c:566
static void vif_dec2(const float *src, float *dst, int w, int h, int src_stride, int dst_stride)
Definition: vf_vif.c:102
#define offset_fn(type, bits)
Definition: vf_vif.c:399
static const uint8_t vif_filter1d_width1[4]
Definition: vf_vif.c:69
static const AVFilterPad vif_outputs[]
Definition: vf_vif.c:639
static void vif_xx_yy_xy(const float *x, const float *y, float *xx, float *yy, float *xy, int w, int h)
Definition: vf_vif.c:186
static int process_frame(FFFrameSync *fs)
Definition: vf_vif.c:542
static int vif_filter1d(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_vif.c:210
static AVFrame * do_vif(AVFilterContext *ctx, AVFrame *main, const AVFrame *ref)
Definition: vf_vif.c:440