FFmpeg
vf_bm3d.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2016 mawen1250
3  * Copyright (c) 2018 Paul B Mahol
4  *
5  * This file is part of FFmpeg.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25 
26 /**
27  * @todo
28  * - non-power of 2 DCT
29  * - opponent color space
30  * - temporal support
31  */
32 
33 #include <float.h>
34 
35 #include "libavutil/imgutils.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/pixdesc.h"
38 #include "libavcodec/avfft.h"
39 #include "avfilter.h"
40 #include "filters.h"
41 #include "formats.h"
42 #include "framesync.h"
43 #include "internal.h"
44 #include "video.h"
45 
46 #define MAX_NB_THREADS 32
47 
52 };
53 
54 typedef struct ThreadData {
55  const uint8_t *src;
57  const uint8_t *ref;
59  int plane;
60 } ThreadData;
61 
62 typedef struct PosCode {
63  int x, y;
64 } PosCode;
65 
66 typedef struct PosPairCode {
67  double score;
68  int x, y;
69 } PosPairCode;
70 
71 typedef struct SliceContext {
82  float *num, *den;
86 } SliceContext;
87 
88 typedef struct BM3DContext {
89  const AVClass *class;
90 
91  float sigma;
95  int bm_range;
96  int bm_step;
97  float th_mse;
99  int mode;
100  int ref;
101  int planes;
102 
103  int depth;
104  int max;
106  int planewidth[4];
107  int planeheight[4];
110 
112 
115 
116  void (*get_block_row)(const uint8_t *srcp, int src_linesize,
117  int y, int x, int block_size, float *dst);
118  double (*do_block_ssd)(struct BM3DContext *s, PosCode *pos,
119  const uint8_t *src, int src_stride,
120  int r_y, int r_x);
121  void (*do_output)(struct BM3DContext *s, uint8_t *dst, int dst_linesize,
122  int plane, int nb_jobs);
123  void (*block_filtering)(struct BM3DContext *s,
124  const uint8_t *src, int src_linesize,
125  const uint8_t *ref, int ref_linesize,
126  int y, int x, int plane, int jobnr);
127 } BM3DContext;
128 
129 #define OFFSET(x) offsetof(BM3DContext, x)
130 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
131 static const AVOption bm3d_options[] = {
132  { "sigma", "set denoising strength",
133  OFFSET(sigma), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 99999.9, FLAGS },
134  { "block", "set log2(size) of local patch",
135  OFFSET(block_size), AV_OPT_TYPE_INT, {.i64=4}, 4, 6, FLAGS },
136  { "bstep", "set sliding step for processing blocks",
137  OFFSET(block_step), AV_OPT_TYPE_INT, {.i64=4}, 1, 64, FLAGS },
138  { "group", "set maximal number of similar blocks",
139  OFFSET(group_size), AV_OPT_TYPE_INT, {.i64=1}, 1, 256, FLAGS },
140  { "range", "set block matching range",
141  OFFSET(bm_range), AV_OPT_TYPE_INT, {.i64=9}, 1, INT32_MAX, FLAGS },
142  { "mstep", "set step for block matching",
143  OFFSET(bm_step), AV_OPT_TYPE_INT, {.i64=1}, 1, 64, FLAGS },
144  { "thmse", "set threshold of mean square error for block matching",
145  OFFSET(th_mse), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, INT32_MAX, FLAGS },
146  { "hdthr", "set hard threshold for 3D transfer domain",
147  OFFSET(hard_threshold), AV_OPT_TYPE_FLOAT, {.dbl=2.7}, 0, INT32_MAX, FLAGS },
148  { "estim", "set filtering estimation mode",
149  OFFSET(mode), AV_OPT_TYPE_INT, {.i64=BASIC}, 0, NB_MODES-1, FLAGS, "mode" },
150  { "basic", "basic estimate",
151  0, AV_OPT_TYPE_CONST, {.i64=BASIC}, 0, 0, FLAGS, "mode" },
152  { "final", "final estimate",
153  0, AV_OPT_TYPE_CONST, {.i64=FINAL}, 0, 0, FLAGS, "mode" },
154  { "ref", "have reference stream",
155  OFFSET(ref), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
156  { "planes", "set planes to filter",
157  OFFSET(planes), AV_OPT_TYPE_INT, {.i64=7}, 0, 15, FLAGS },
158  { NULL }
159 };
160 
162 
164 {
165  static const enum AVPixelFormat pix_fmts[] = {
189  };
190 
192  if (!fmts_list)
193  return AVERROR(ENOMEM);
194  return ff_set_common_formats(ctx, fmts_list);
195 }
196 
197 static int do_search_boundary(int pos, int plane_boundary, int search_range, int search_step)
198 {
199  int search_boundary;
200 
201  search_range = search_range / search_step * search_step;
202 
203  if (pos == plane_boundary) {
204  search_boundary = plane_boundary;
205  } else if (pos > plane_boundary) {
206  search_boundary = pos - search_range;
207 
208  while (search_boundary < plane_boundary) {
209  search_boundary += search_step;
210  }
211  } else {
212  search_boundary = pos + search_range;
213 
214  while (search_boundary > plane_boundary) {
215  search_boundary -= search_step;
216  }
217  }
218 
219  return search_boundary;
220 }
221 
222 static int search_boundary(int plane_boundary, int search_range, int search_step, int vertical, int y, int x)
223 {
224  return do_search_boundary(vertical ? y : x, plane_boundary, search_range, search_step);
225 }
226 
227 static int cmp_scores(const void *a, const void *b)
228 {
229  const struct PosPairCode *pair1 = a;
230  const struct PosPairCode *pair2 = b;
231  return FFDIFFSIGN(pair1->score, pair2->score);
232 }
233 
234 static double do_block_ssd(BM3DContext *s, PosCode *pos, const uint8_t *src, int src_stride, int r_y, int r_x)
235 {
236  const uint8_t *srcp = src + pos->y * src_stride + pos->x;
237  const uint8_t *refp = src + r_y * src_stride + r_x;
238  const int block_size = s->block_size;
239  double dist = 0.;
240  int x, y;
241 
242  for (y = 0; y < block_size; y++) {
243  for (x = 0; x < block_size; x++) {
244  double temp = refp[x] - srcp[x];
245  dist += temp * temp;
246  }
247 
248  srcp += src_stride;
249  refp += src_stride;
250  }
251 
252  return dist;
253 }
254 
255 static double do_block_ssd16(BM3DContext *s, PosCode *pos, const uint8_t *src, int src_stride, int r_y, int r_x)
256 {
257  const uint16_t *srcp = (uint16_t *)src + pos->y * src_stride / 2 + pos->x;
258  const uint16_t *refp = (uint16_t *)src + r_y * src_stride / 2 + r_x;
259  const int block_size = s->block_size;
260  double dist = 0.;
261  int x, y;
262 
263  for (y = 0; y < block_size; y++) {
264  for (x = 0; x < block_size; x++) {
265  double temp = refp[x] - srcp[x];
266  dist += temp * temp;
267  }
268 
269  srcp += src_stride / 2;
270  refp += src_stride / 2;
271  }
272 
273  return dist;
274 }
275 
276 static void do_block_matching_multi(BM3DContext *s, const uint8_t *src, int src_stride, int src_range,
277  const PosCode *search_pos, int search_size, float th_mse,
278  int r_y, int r_x, int plane, int jobnr)
279 {
280  SliceContext *sc = &s->slices[jobnr];
281  double MSE2SSE = s->group_size * s->block_size * s->block_size * src_range * src_range / (s->max * s->max);
282  double distMul = 1. / MSE2SSE;
283  double th_sse = th_mse * MSE2SSE;
284  int i, index = sc->nb_match_blocks;
285 
286  for (i = 0; i < search_size; i++) {
287  PosCode pos = search_pos[i];
288  double dist;
289 
290  dist = s->do_block_ssd(s, &pos, src, src_stride, r_y, r_x);
291 
292  // Only match similar blocks but not identical blocks
293  if (dist <= th_sse && dist != 0) {
294  const double score = dist * distMul;
295 
296  if (index >= s->group_size && score >= sc->match_blocks[index - 1].score) {
297  continue;
298  }
299 
300  if (index >= s->group_size)
301  index = s->group_size - 1;
302 
303  sc->match_blocks[index].score = score;
304  sc->match_blocks[index].y = pos.y;
305  sc->match_blocks[index].x = pos.x;
306  index++;
307  qsort(sc->match_blocks, index, sizeof(PosPairCode), cmp_scores);
308  }
309  }
310 
311  sc->nb_match_blocks = index;
312 }
313 
314 static void block_matching_multi(BM3DContext *s, const uint8_t *ref, int ref_linesize, int y, int x,
315  int exclude_cur_pos, int plane, int jobnr)
316 {
317  SliceContext *sc = &s->slices[jobnr];
318  const int width = s->planewidth[plane];
319  const int height = s->planeheight[plane];
320  const int block_size = s->block_size;
321  const int step = s->bm_step;
322  const int range = s->bm_range / step * step;
323  int l = search_boundary(0, range, step, 0, y, x);
324  int r = search_boundary(width - block_size, range, step, 0, y, x);
325  int t = search_boundary(0, range, step, 1, y, x);
326  int b = search_boundary(height - block_size, range, step, 1, y, x);
327  int j, i, index = 0;
328 
329  for (j = t; j <= b; j += step) {
330  for (i = l; i <= r; i += step) {
331  PosCode pos;
332 
333  if (exclude_cur_pos > 0 && j == y && i == x) {
334  continue;
335  }
336 
337  pos.y = j;
338  pos.x = i;
339  sc->search_positions[index++] = pos;
340  }
341  }
342 
343  if (exclude_cur_pos == 1) {
344  sc->match_blocks[0].score = 0;
345  sc->match_blocks[0].y = y;
346  sc->match_blocks[0].x = x;
347  sc->nb_match_blocks = 1;
348  }
349 
350  do_block_matching_multi(s, ref, ref_linesize, s->bm_range,
351  sc->search_positions, index, s->th_mse, y, x, plane, jobnr);
352 }
353 
354 static void block_matching(BM3DContext *s, const uint8_t *ref, int ref_linesize,
355  int j, int i, int plane, int jobnr)
356 {
357  SliceContext *sc = &s->slices[jobnr];
358 
359  if (s->group_size == 1 || s->th_mse <= 0.f) {
360  sc->match_blocks[0].score = 1;
361  sc->match_blocks[0].x = i;
362  sc->match_blocks[0].y = j;
363  sc->nb_match_blocks = 1;
364  return;
365  }
366 
367  sc->nb_match_blocks = 0;
368  block_matching_multi(s, ref, ref_linesize, j, i, 1, plane, jobnr);
369 }
370 
371 static void get_block_row(const uint8_t *srcp, int src_linesize,
372  int y, int x, int block_size, float *dst)
373 {
374  const uint8_t *src = srcp + y * src_linesize + x;
375  int j;
376 
377  for (j = 0; j < block_size; j++) {
378  dst[j] = src[j];
379  }
380 }
381 
382 static void get_block_row16(const uint8_t *srcp, int src_linesize,
383  int y, int x, int block_size, float *dst)
384 {
385  const uint16_t *src = (uint16_t *)srcp + y * src_linesize / 2 + x;
386  int j;
387 
388  for (j = 0; j < block_size; j++) {
389  dst[j] = src[j];
390  }
391 }
392 
393 static void basic_block_filtering(BM3DContext *s, const uint8_t *src, int src_linesize,
394  const uint8_t *ref, int ref_linesize,
395  int y, int x, int plane, int jobnr)
396 {
397  SliceContext *sc = &s->slices[jobnr];
398  const int buffer_linesize = s->block_size * s->block_size;
399  const int nb_match_blocks = sc->nb_match_blocks;
400  const int block_size = s->block_size;
401  const int width = s->planewidth[plane];
402  const int pgroup_size = s->pgroup_size;
403  const int group_size = s->group_size;
404  float *buffer = sc->buffer;
405  float *bufferh = sc->bufferh;
406  float *bufferv = sc->bufferv;
407  float *bufferz = sc->bufferz;
408  float threshold[4];
409  float den_weight, num_weight;
410  int retained = 0;
411  int i, j, k;
412 
413  for (k = 0; k < nb_match_blocks; k++) {
414  const int y = sc->match_blocks[k].y;
415  const int x = sc->match_blocks[k].x;
416 
417  for (i = 0; i < block_size; i++) {
418  s->get_block_row(src, src_linesize, y + i, x, block_size, bufferh + block_size * i);
419  av_dct_calc(sc->dctf, bufferh + block_size * i);
420  }
421 
422  for (i = 0; i < block_size; i++) {
423  for (j = 0; j < block_size; j++) {
424  bufferv[i * block_size + j] = bufferh[j * block_size + i];
425  }
426  av_dct_calc(sc->dctf, bufferv + i * block_size);
427  }
428 
429  for (i = 0; i < block_size; i++) {
430  memcpy(buffer + k * buffer_linesize + i * block_size,
431  bufferv + i * block_size, block_size * 4);
432  }
433  }
434 
435  for (i = 0; i < block_size; i++) {
436  for (j = 0; j < block_size; j++) {
437  for (k = 0; k < nb_match_blocks; k++)
438  bufferz[k] = buffer[buffer_linesize * k + i * block_size + j];
439  if (group_size > 1)
440  av_dct_calc(sc->gdctf, bufferz);
441  bufferz += pgroup_size;
442  }
443  }
444 
445  threshold[0] = s->hard_threshold * s->sigma * M_SQRT2 * block_size * block_size * (1 << (s->depth - 8)) / 255.f;
446  threshold[1] = threshold[0] * sqrtf(2.f);
447  threshold[2] = threshold[0] * 2.f;
448  threshold[3] = threshold[0] * sqrtf(8.f);
449  bufferz = sc->bufferz;
450 
451  for (i = 0; i < block_size; i++) {
452  for (j = 0; j < block_size; j++) {
453  for (k = 0; k < nb_match_blocks; k++) {
454  const float thresh = threshold[(j == 0) + (i == 0) + (k == 0)];
455 
456  if (bufferz[k] > thresh || bufferz[k] < -thresh) {
457  retained++;
458  } else {
459  bufferz[k] = 0;
460  }
461  }
462  bufferz += pgroup_size;
463  }
464  }
465 
466  bufferz = sc->bufferz;
467  buffer = sc->buffer;
468  for (i = 0; i < block_size; i++) {
469  for (j = 0; j < block_size; j++) {
470  if (group_size > 1)
471  av_dct_calc(sc->gdcti, bufferz);
472  for (k = 0; k < nb_match_blocks; k++) {
473  buffer[buffer_linesize * k + i * block_size + j] = bufferz[k];
474  }
475  bufferz += pgroup_size;
476  }
477  }
478 
479  den_weight = retained < 1 ? 1.f : 1.f / retained;
480  num_weight = den_weight;
481 
482  buffer = sc->buffer;
483  for (k = 0; k < nb_match_blocks; k++) {
484  float *num = sc->num + y * width + x;
485  float *den = sc->den + y * width + x;
486 
487  for (i = 0; i < block_size; i++) {
488  memcpy(bufferv + i * block_size,
489  buffer + k * buffer_linesize + i * block_size,
490  block_size * 4);
491  }
492 
493  for (i = 0; i < block_size; i++) {
494  av_dct_calc(sc->dcti, bufferv + block_size * i);
495  for (j = 0; j < block_size; j++) {
496  bufferh[j * block_size + i] = bufferv[i * block_size + j];
497  }
498  }
499 
500  for (i = 0; i < block_size; i++) {
501  av_dct_calc(sc->dcti, bufferh + block_size * i);
502  for (j = 0; j < block_size; j++) {
503  num[j] += bufferh[i * block_size + j] * num_weight;
504  den[j] += den_weight;
505  }
506  num += width;
507  den += width;
508  }
509  }
510 }
511 
512 static void final_block_filtering(BM3DContext *s, const uint8_t *src, int src_linesize,
513  const uint8_t *ref, int ref_linesize,
514  int y, int x, int plane, int jobnr)
515 {
516  SliceContext *sc = &s->slices[jobnr];
517  const int buffer_linesize = s->block_size * s->block_size;
518  const int nb_match_blocks = sc->nb_match_blocks;
519  const int block_size = s->block_size;
520  const int width = s->planewidth[plane];
521  const int pgroup_size = s->pgroup_size;
522  const int group_size = s->group_size;
523  const float sigma_sqr = s->sigma * s->sigma;
524  float *buffer = sc->buffer;
525  float *bufferh = sc->bufferh;
526  float *bufferv = sc->bufferv;
527  float *bufferz = sc->bufferz;
528  float *rbuffer = sc->rbuffer;
529  float *rbufferh = sc->rbufferh;
530  float *rbufferv = sc->rbufferv;
531  float *rbufferz = sc->rbufferz;
532  float den_weight, num_weight;
533  float l2_wiener = 0;
534  int i, j, k;
535 
536  for (k = 0; k < nb_match_blocks; k++) {
537  const int y = sc->match_blocks[k].y;
538  const int x = sc->match_blocks[k].x;
539 
540  for (i = 0; i < block_size; i++) {
541  s->get_block_row(src, src_linesize, y + i, x, block_size, bufferh + block_size * i);
542  s->get_block_row(ref, ref_linesize, y + i, x, block_size, rbufferh + block_size * i);
543  av_dct_calc(sc->dctf, bufferh + block_size * i);
544  av_dct_calc(sc->dctf, rbufferh + block_size * i);
545  }
546 
547  for (i = 0; i < block_size; i++) {
548  for (j = 0; j < block_size; j++) {
549  bufferv[i * block_size + j] = bufferh[j * block_size + i];
550  rbufferv[i * block_size + j] = rbufferh[j * block_size + i];
551  }
552  av_dct_calc(sc->dctf, bufferv + i * block_size);
553  av_dct_calc(sc->dctf, rbufferv + i * block_size);
554  }
555 
556  for (i = 0; i < block_size; i++) {
557  memcpy(buffer + k * buffer_linesize + i * block_size,
558  bufferv + i * block_size, block_size * 4);
559  memcpy(rbuffer + k * buffer_linesize + i * block_size,
560  rbufferv + i * block_size, block_size * 4);
561  }
562  }
563 
564  for (i = 0; i < block_size; i++) {
565  for (j = 0; j < block_size; j++) {
566  for (k = 0; k < nb_match_blocks; k++) {
567  bufferz[k] = buffer[buffer_linesize * k + i * block_size + j];
568  rbufferz[k] = rbuffer[buffer_linesize * k + i * block_size + j];
569  }
570  if (group_size > 1) {
571  av_dct_calc(sc->gdctf, bufferz);
572  av_dct_calc(sc->gdctf, rbufferz);
573  }
574  bufferz += pgroup_size;
575  rbufferz += pgroup_size;
576  }
577  }
578 
579  bufferz = sc->bufferz;
580  rbufferz = sc->rbufferz;
581 
582  for (i = 0; i < block_size; i++) {
583  for (j = 0; j < block_size; j++) {
584  for (k = 0; k < nb_match_blocks; k++) {
585  const float ref_sqr = rbufferz[k] * rbufferz[k];
586  float wiener_coef = ref_sqr / (ref_sqr + sigma_sqr);
587 
588  if (isnan(wiener_coef))
589  wiener_coef = 1;
590  bufferz[k] *= wiener_coef;
591  l2_wiener += wiener_coef * wiener_coef;
592  }
593  bufferz += pgroup_size;
594  rbufferz += pgroup_size;
595  }
596  }
597 
598  bufferz = sc->bufferz;
599  buffer = sc->buffer;
600  for (i = 0; i < block_size; i++) {
601  for (j = 0; j < block_size; j++) {
602  if (group_size > 1)
603  av_dct_calc(sc->gdcti, bufferz);
604  for (k = 0; k < nb_match_blocks; k++) {
605  buffer[buffer_linesize * k + i * block_size + j] = bufferz[k];
606  }
607  bufferz += pgroup_size;
608  }
609  }
610 
611  l2_wiener = FFMAX(l2_wiener, 1e-15f);
612  den_weight = 1.f / l2_wiener;
613  num_weight = den_weight;
614 
615  for (k = 0; k < nb_match_blocks; k++) {
616  float *num = sc->num + y * width + x;
617  float *den = sc->den + y * width + x;
618 
619  for (i = 0; i < block_size; i++) {
620  memcpy(bufferv + i * block_size,
621  buffer + k * buffer_linesize + i * block_size,
622  block_size * 4);
623  }
624 
625  for (i = 0; i < block_size; i++) {
626  av_dct_calc(sc->dcti, bufferv + block_size * i);
627  for (j = 0; j < block_size; j++) {
628  bufferh[j * block_size + i] = bufferv[i * block_size + j];
629  }
630  }
631 
632  for (i = 0; i < block_size; i++) {
633  av_dct_calc(sc->dcti, bufferh + block_size * i);
634  for (j = 0; j < block_size; j++) {
635  num[j] += bufferh[i * block_size + j] * num_weight;
636  den[j] += den_weight;
637  }
638  num += width;
639  den += width;
640  }
641  }
642 }
643 
644 static void do_output(BM3DContext *s, uint8_t *dst, int dst_linesize,
645  int plane, int nb_jobs)
646 {
647  const int height = s->planeheight[plane];
648  const int width = s->planewidth[plane];
649  int i, j, k;
650 
651  for (i = 0; i < height; i++) {
652  for (j = 0; j < width; j++) {
653  uint8_t *dstp = dst + i * dst_linesize;
654  float sum_den = 0.f;
655  float sum_num = 0.f;
656 
657  for (k = 0; k < nb_jobs; k++) {
658  SliceContext *sc = &s->slices[k];
659  float num = sc->num[i * width + j];
660  float den = sc->den[i * width + j];
661 
662  sum_num += num;
663  sum_den += den;
664  }
665 
666  dstp[j] = av_clip_uint8(lrintf(sum_num / sum_den));
667  }
668  }
669 }
670 
671 static void do_output16(BM3DContext *s, uint8_t *dst, int dst_linesize,
672  int plane, int nb_jobs)
673 {
674  const int height = s->planeheight[plane];
675  const int width = s->planewidth[plane];
676  const int depth = s->depth;
677  int i, j, k;
678 
679  for (i = 0; i < height; i++) {
680  for (j = 0; j < width; j++) {
681  uint16_t *dstp = (uint16_t *)dst + i * dst_linesize / 2;
682  float sum_den = 0.f;
683  float sum_num = 0.f;
684 
685  for (k = 0; k < nb_jobs; k++) {
686  SliceContext *sc = &s->slices[k];
687  float num = sc->num[i * width + j];
688  float den = sc->den[i * width + j];
689 
690  sum_num += num;
691  sum_den += den;
692  }
693 
694  dstp[j] = av_clip_uintp2_c(lrintf(sum_num / sum_den), depth);
695  }
696  }
697 }
698 
699 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
700 {
701  BM3DContext *s = ctx->priv;
702  SliceContext *sc = &s->slices[jobnr];
703  const int block_step = s->block_step;
704  ThreadData *td = arg;
705  const uint8_t *src = td->src;
706  const uint8_t *ref = td->ref;
707  const int src_linesize = td->src_linesize;
708  const int ref_linesize = td->ref_linesize;
709  const int plane = td->plane;
710  const int width = s->planewidth[plane];
711  const int height = s->planeheight[plane];
712  const int block_pos_bottom = FFMAX(0, height - s->block_size);
713  const int block_pos_right = FFMAX(0, width - s->block_size);
714  const int slice_start = (((height + block_step - 1) / block_step) * jobnr / nb_jobs) * block_step;
715  const int slice_end = (jobnr == nb_jobs - 1) ? block_pos_bottom + block_step :
716  (((height + block_step - 1) / block_step) * (jobnr + 1) / nb_jobs) * block_step;
717  int i, j;
718 
719  memset(sc->num, 0, width * height * sizeof(FFTSample));
720  memset(sc->den, 0, width * height * sizeof(FFTSample));
721 
722  for (j = slice_start; j < slice_end; j += block_step) {
723  if (j > block_pos_bottom) {
724  j = block_pos_bottom;
725  }
726 
727  for (i = 0; i < block_pos_right + block_step; i += block_step) {
728  if (i > block_pos_right) {
729  i = block_pos_right;
730  }
731 
732  block_matching(s, ref, ref_linesize, j, i, plane, jobnr);
733 
734  s->block_filtering(s, src, src_linesize,
735  ref, ref_linesize, j, i, plane, jobnr);
736  }
737  }
738 
739  return 0;
740 }
741 
743 {
744  BM3DContext *s = ctx->priv;
745  AVFilterLink *outlink = ctx->outputs[0];
746  int p;
747 
748  *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
749  if (!*out)
750  return AVERROR(ENOMEM);
751  av_frame_copy_props(*out, in);
752 
753  for (p = 0; p < s->nb_planes; p++) {
754  const int nb_jobs = FFMAX(1, FFMIN(s->nb_threads, s->planeheight[p] / s->block_size));
755  ThreadData td;
756 
757  if (!((1 << p) & s->planes) || ctx->is_disabled) {
758  av_image_copy_plane((*out)->data[p], (*out)->linesize[p],
759  in->data[p], in->linesize[p],
760  s->planewidth[p], s->planeheight[p]);
761  continue;
762  }
763 
764  td.src = in->data[p];
765  td.src_linesize = in->linesize[p];
766  td.ref = ref->data[p];
767  td.ref_linesize = ref->linesize[p];
768  td.plane = p;
769  ctx->internal->execute(ctx, filter_slice, &td, NULL, nb_jobs);
770 
771  s->do_output(s, (*out)->data[p], (*out)->linesize[p], p, nb_jobs);
772  }
773 
774  return 0;
775 }
776 
777 #define SQR(x) ((x) * (x))
778 
780 {
782  AVFilterContext *ctx = inlink->dst;
783  BM3DContext *s = ctx->priv;
784  int i, group_bits;
785 
787  s->nb_planes = av_pix_fmt_count_planes(inlink->format);
788  s->depth = desc->comp[0].depth;
789  s->max = (1 << s->depth) - 1;
790  s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
791  s->planeheight[0] = s->planeheight[3] = inlink->h;
792  s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
793  s->planewidth[0] = s->planewidth[3] = inlink->w;
794 
795  for (group_bits = 4; 1 << group_bits < s->group_size; group_bits++);
796  s->group_bits = group_bits;
797  s->pgroup_size = 1 << group_bits;
798 
799  for (i = 0; i < s->nb_threads; i++) {
800  SliceContext *sc = &s->slices[i];
801 
802  sc->num = av_calloc(FFALIGN(s->planewidth[0], s->block_size) * FFALIGN(s->planeheight[0], s->block_size), sizeof(FFTSample));
803  sc->den = av_calloc(FFALIGN(s->planewidth[0], s->block_size) * FFALIGN(s->planeheight[0], s->block_size), sizeof(FFTSample));
804  if (!sc->num || !sc->den)
805  return AVERROR(ENOMEM);
806 
807  sc->dctf = av_dct_init(av_log2(s->block_size), DCT_II);
808  sc->dcti = av_dct_init(av_log2(s->block_size), DCT_III);
809  if (!sc->dctf || !sc->dcti)
810  return AVERROR(ENOMEM);
811 
812  if (s->group_bits > 1) {
813  sc->gdctf = av_dct_init(s->group_bits, DCT_II);
814  sc->gdcti = av_dct_init(s->group_bits, DCT_III);
815  if (!sc->gdctf || !sc->gdcti)
816  return AVERROR(ENOMEM);
817  }
818 
819  sc->buffer = av_calloc(s->block_size * s->block_size * s->pgroup_size, sizeof(*sc->buffer));
820  sc->bufferz = av_calloc(s->block_size * s->block_size * s->pgroup_size, sizeof(*sc->bufferz));
821  sc->bufferh = av_calloc(s->block_size * s->block_size, sizeof(*sc->bufferh));
822  sc->bufferv = av_calloc(s->block_size * s->block_size, sizeof(*sc->bufferv));
823  if (!sc->bufferh || !sc->bufferv || !sc->buffer || !sc->bufferz)
824  return AVERROR(ENOMEM);
825 
826  if (s->mode == FINAL) {
827  sc->rbuffer = av_calloc(s->block_size * s->block_size * s->pgroup_size, sizeof(*sc->rbuffer));
828  sc->rbufferz = av_calloc(s->block_size * s->block_size * s->pgroup_size, sizeof(*sc->rbufferz));
829  sc->rbufferh = av_calloc(s->block_size * s->block_size, sizeof(*sc->rbufferh));
830  sc->rbufferv = av_calloc(s->block_size * s->block_size, sizeof(*sc->rbufferv));
831  if (!sc->rbufferh || !sc->rbufferv || !sc->rbuffer || !sc->rbufferz)
832  return AVERROR(ENOMEM);
833  }
834 
835  sc->search_positions = av_calloc(SQR(2 * s->bm_range / s->bm_step + 1), sizeof(*sc->search_positions));
836  if (!sc->search_positions)
837  return AVERROR(ENOMEM);
838  }
839 
840  s->do_output = do_output;
841  s->do_block_ssd = do_block_ssd;
842  s->get_block_row = get_block_row;
843 
844  if (s->depth > 8) {
845  s->do_output = do_output16;
846  s->do_block_ssd = do_block_ssd16;
847  s->get_block_row = get_block_row16;
848  }
849 
850  return 0;
851 }
852 
854 {
855  BM3DContext *s = ctx->priv;
856 
857  if (!s->ref) {
858  AVFrame *frame = NULL;
859  AVFrame *out = NULL;
860  int ret, status;
861  int64_t pts;
862 
863  FF_FILTER_FORWARD_STATUS_BACK(ctx->outputs[0], ctx->inputs[0]);
864 
865  if ((ret = ff_inlink_consume_frame(ctx->inputs[0], &frame)) > 0) {
868  if (ret < 0)
869  return ret;
870  ret = ff_filter_frame(ctx->outputs[0], out);
871  }
872  if (ret < 0) {
873  return ret;
874  } else if (ff_inlink_acknowledge_status(ctx->inputs[0], &status, &pts)) {
875  ff_outlink_set_status(ctx->outputs[0], status, pts);
876  return 0;
877  } else {
878  if (ff_outlink_frame_wanted(ctx->outputs[0]))
879  ff_inlink_request_frame(ctx->inputs[0]);
880  return 0;
881  }
882  } else {
883  return ff_framesync_activate(&s->fs);
884  }
885 }
886 
888 {
889  AVFilterContext *ctx = fs->parent;
890  BM3DContext *s = fs->opaque;
891  AVFilterLink *outlink = ctx->outputs[0];
892  AVFrame *out = NULL, *src, *ref;
893  int ret;
894 
895  if ((ret = ff_framesync_get_frame(&s->fs, 0, &src, 0)) < 0 ||
896  (ret = ff_framesync_get_frame(&s->fs, 1, &ref, 0)) < 0)
897  return ret;
898 
899  if ((ret = filter_frame(ctx, &out, src, ref)) < 0)
900  return ret;
901 
902  out->pts = av_rescale_q(src->pts, s->fs.time_base, outlink->time_base);
903 
904  return ff_filter_frame(outlink, out);
905 }
906 
908 {
909  BM3DContext *s = ctx->priv;
910  AVFilterPad pad = { 0 };
911  int ret;
912 
913  if (s->mode == BASIC) {
914  if (s->th_mse == 0.f)
915  s->th_mse = 400.f + s->sigma * 80.f;
916  s->block_filtering = basic_block_filtering;
917  } else if (s->mode == FINAL) {
918  if (!s->ref) {
919  av_log(ctx, AV_LOG_WARNING, "Reference stream is mandatory in final estimation mode.\n");
920  s->ref = 1;
921  }
922  if (s->th_mse == 0.f)
923  s->th_mse = 200.f + s->sigma * 10.f;
924 
925  s->block_filtering = final_block_filtering;
926  } else {
927  return AVERROR_BUG;
928  }
929 
930  s->block_size = 1 << s->block_size;
931 
932  if (s->block_step > s->block_size) {
933  av_log(ctx, AV_LOG_WARNING, "bstep: %d can't be bigger than block size. Changing to %d.\n",
934  s->block_step, s->block_size);
935  s->block_step = s->block_size;
936  }
937  if (s->bm_step > s->bm_range) {
938  av_log(ctx, AV_LOG_WARNING, "mstep: %d can't be bigger than block matching range. Changing to %d.\n",
939  s->bm_step, s->bm_range);
940  s->bm_step = s->bm_range;
941  }
942 
943  pad.type = AVMEDIA_TYPE_VIDEO;
944  pad.name = "source";
946 
947  if ((ret = ff_insert_inpad(ctx, 0, &pad)) < 0)
948  return ret;
949 
950  if (s->ref) {
951  pad.type = AVMEDIA_TYPE_VIDEO;
952  pad.name = "reference";
953  pad.config_props = NULL;
954 
955  if ((ret = ff_insert_inpad(ctx, 1, &pad)) < 0)
956  return ret;
957  }
958 
959  return 0;
960 }
961 
962 static int config_output(AVFilterLink *outlink)
963 {
964  AVFilterContext *ctx = outlink->src;
965  BM3DContext *s = ctx->priv;
966  AVFilterLink *src = ctx->inputs[0];
967  AVFilterLink *ref;
968  FFFrameSyncIn *in;
969  int ret;
970 
971  if (s->ref) {
972  ref = ctx->inputs[1];
973 
974  if (src->format != ref->format) {
975  av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
976  return AVERROR(EINVAL);
977  }
978  if (src->w != ref->w ||
979  src->h != ref->h) {
980  av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
981  "(size %dx%d) do not match the corresponding "
982  "second input link %s parameters (%dx%d) ",
983  ctx->input_pads[0].name, src->w, src->h,
984  ctx->input_pads[1].name, ref->w, ref->h);
985  return AVERROR(EINVAL);
986  }
987  }
988 
989  outlink->w = src->w;
990  outlink->h = src->h;
991  outlink->time_base = src->time_base;
992  outlink->sample_aspect_ratio = src->sample_aspect_ratio;
993  outlink->frame_rate = src->frame_rate;
994 
995  if (!s->ref)
996  return 0;
997 
998  if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0)
999  return ret;
1000 
1001  in = s->fs.in;
1002  in[0].time_base = src->time_base;
1003  in[1].time_base = ref->time_base;
1004  in[0].sync = 1;
1005  in[0].before = EXT_STOP;
1006  in[0].after = EXT_STOP;
1007  in[1].sync = 1;
1008  in[1].before = EXT_STOP;
1009  in[1].after = EXT_STOP;
1010  s->fs.opaque = s;
1011  s->fs.on_event = process_frame;
1012 
1013  return ff_framesync_configure(&s->fs);
1014 }
1015 
1017 {
1018  BM3DContext *s = ctx->priv;
1019  int i;
1020 
1021  if (s->ref)
1022  ff_framesync_uninit(&s->fs);
1023 
1024  for (i = 0; i < s->nb_threads; i++) {
1025  SliceContext *sc = &s->slices[i];
1026 
1027  av_freep(&sc->num);
1028  av_freep(&sc->den);
1029 
1030  av_dct_end(sc->gdctf);
1031  av_dct_end(sc->gdcti);
1032  av_dct_end(sc->dctf);
1033  av_dct_end(sc->dcti);
1034 
1035  av_freep(&sc->buffer);
1036  av_freep(&sc->bufferh);
1037  av_freep(&sc->bufferv);
1038  av_freep(&sc->bufferz);
1039  av_freep(&sc->rbuffer);
1040  av_freep(&sc->rbufferh);
1041  av_freep(&sc->rbufferv);
1042  av_freep(&sc->rbufferz);
1043 
1044  av_freep(&sc->search_positions);
1045  }
1046 }
1047 
1048 static const AVFilterPad bm3d_outputs[] = {
1049  {
1050  .name = "default",
1051  .type = AVMEDIA_TYPE_VIDEO,
1052  .config_props = config_output,
1053  },
1054  { NULL }
1055 };
1056 
1058  .name = "bm3d",
1059  .description = NULL_IF_CONFIG_SMALL("Block-Matching 3D denoiser."),
1060  .priv_size = sizeof(BM3DContext),
1061  .init = init,
1062  .uninit = uninit,
1063  .activate = activate,
1065  .inputs = NULL,
1066  .outputs = bm3d_outputs,
1067  .priv_class = &bm3d_class,
1071 };
SliceContext::num
float * num
Definition: vf_bm3d.c:82
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:98
AV_PIX_FMT_YUVA422P16
#define AV_PIX_FMT_YUVA422P16
Definition: pixfmt.h:432
AV_PIX_FMT_GBRAP16
#define AV_PIX_FMT_GBRAP16
Definition: pixfmt.h:411
FFFrameSyncIn::time_base
AVRational time_base
Time base for the incoming frames.
Definition: framesync.h:96
ff_framesync_configure
int ff_framesync_configure(FFFrameSync *fs)
Configure a frame sync structure.
Definition: framesync.c:124
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
BM3DContext::slices
SliceContext slices[MAX_NB_THREADS]
Definition: vf_bm3d.c:111
td
#define td
Definition: regdef.h:70
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
status
they must not be accessed directly The fifo field contains the frames that are queued in the input for processing by the filter The status_in and status_out fields contains the queued status(EOF or error) of the link
r
const char * r
Definition: vf_curves.c:116
AVERROR
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
opt.h
ff_make_format_list
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:286
ff_framesync_uninit
void ff_framesync_uninit(FFFrameSync *fs)
Free all memory currently allocated.
Definition: framesync.c:290
out
FILE * out
Definition: movenc.c:54
FLAGS
#define FLAGS
Definition: vf_bm3d.c:130
SliceContext::dcti
DCTContext * dcti
Definition: vf_bm3d.c:73
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:979
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2540
SliceContext::bufferz
FFTSample * bufferz
Definition: vf_bm3d.c:76
ff_framesync_get_frame
int ff_framesync_get_frame(FFFrameSync *fs, unsigned in, AVFrame **rframe, unsigned get)
Get the current frame in an input.
Definition: framesync.c:253
inlink
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
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:112
BM3DContext::do_block_ssd
double(* do_block_ssd)(struct BM3DContext *s, PosCode *pos, const uint8_t *src, int src_stride, int r_y, int r_x)
Definition: vf_bm3d.c:118
BM3DContext::th_mse
float th_mse
Definition: vf_bm3d.c:97
AV_PIX_FMT_YUVA422P9
#define AV_PIX_FMT_YUVA422P9
Definition: pixfmt.h:424
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
pixdesc.h
av_clip_uintp2_c
static av_always_inline av_const unsigned av_clip_uintp2_c(int a, int p)
Clip a signed integer to an unsigned power of two range.
Definition: common.h:276
bm3d_options
static const AVOption bm3d_options[]
Definition: vf_bm3d.c:131
step
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about which is also called distortion Distortion can be quantified by almost any quality measurement one chooses the sum of squared differences is used but more complex methods that consider psychovisual effects can be used as well It makes no difference in this discussion First step
Definition: rate_distortion.txt:58
index
fg index
Definition: ffmpeg_filter.c:168
AV_PIX_FMT_YUVA420P16
#define AV_PIX_FMT_YUVA420P16
Definition: pixfmt.h:431
ThreadData::ref_linesize
int ref_linesize
Definition: vf_bm3d.c:58
AV_PIX_FMT_YUVA420P10
#define AV_PIX_FMT_YUVA420P10
Definition: pixfmt.h:426
AVOption
AVOption.
Definition: opt.h:247
BASIC
@ BASIC
Definition: vf_bm3d.c:49
b
#define b
Definition: input.c:40
NB_MODES
@ NB_MODES
Definition: vf_bm3d.c:51
AV_PIX_FMT_YUV420P10
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:389
av_dct_init
DCTContext * av_dct_init(int nbits, enum DCTTransformType type)
Set up DCT.
float.h
SliceContext::rbufferz
FFTSample * rbufferz
Definition: vf_bm3d.c:80
AV_PIX_FMT_YUV440P
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:99
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:149
FFFrameSync
Frame sync structure.
Definition: framesync.h:146
block_matching
static void block_matching(BM3DContext *s, const uint8_t *ref, int ref_linesize, int j, int i, int plane, int jobnr)
Definition: vf_bm3d.c:354
BM3DContext::block_step
int block_step
Definition: vf_bm3d.c:93
video.h
AVFormatContext::internal
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1565
AV_PIX_FMT_YUVA422P10
#define AV_PIX_FMT_YUVA422P10
Definition: pixfmt.h:427
SliceContext::nb_match_blocks
int nb_match_blocks
Definition: vf_bm3d.c:84
FF_FILTER_FORWARD_STATUS_BACK
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition: filters.h:199
BM3DContext::planes
int planes
Definition: vf_bm3d.c:101
BM3DContext::mode
int mode
Definition: vf_bm3d.c:99
AV_PIX_FMT_GRAY9
#define AV_PIX_FMT_GRAY9
Definition: pixfmt.h:369
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:317
av_image_copy_plane
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition: imgutils.c:374
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:64
do_block_ssd
static double do_block_ssd(BM3DContext *s, PosCode *pos, const uint8_t *src, int src_stride, int r_y, int r_x)
Definition: vf_bm3d.c:234
formats.h
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(bm3d)
ff_insert_inpad
static int ff_insert_inpad(AVFilterContext *f, unsigned index, AVFilterPad *p)
Insert a new input pad for the filter.
Definition: internal.h:238
ff_inlink_consume_frame
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
Definition: avfilter.c:1379
av_pix_fmt_count_planes
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2580
AV_PIX_FMT_YUVA420P9
#define AV_PIX_FMT_YUVA420P9
Definition: pixfmt.h:423
EXT_STOP
@ EXT_STOP
Completely stop all streams with this one.
Definition: framesync.h:65
AV_PIX_FMT_GBRP14
#define AV_PIX_FMT_GBRP14
Definition: pixfmt.h:407
AV_PIX_FMT_GBRAP
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:205
DCT_III
@ DCT_III
Definition: avfft.h:95
BM3DContext::ref
int ref
Definition: vf_bm3d.c:100
AV_PIX_FMT_GBRP10
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:405
AV_PIX_FMT_YUVA444P16
#define AV_PIX_FMT_YUVA444P16
Definition: pixfmt.h:433
FFFrameSyncIn
Input stream structure.
Definition: framesync.h:81
BM3DContext::hard_threshold
float hard_threshold
Definition: vf_bm3d.c:98
AV_PIX_FMT_YUV422P9
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:387
PosPairCode::x
int x
Definition: vf_bm3d.c:68
SliceContext::den
float * den
Definition: vf_bm3d.c:82
pts
static int64_t pts
Definition: transcode_aac.c:653
AV_PIX_FMT_GRAY16
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:373
FFFrameSyncIn::sync
unsigned sync
Synchronization level: frames on input at the highest sync level will generate output frame events.
Definition: framesync.h:139
AVFILTER_FLAG_DYNAMIC_INPUTS
#define AVFILTER_FLAG_DYNAMIC_INPUTS
The number of the filter inputs is not determined just by AVFilter.inputs.
Definition: avfilter.h:106
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:52
FFDIFFSIGN
#define FFDIFFSIGN(x, y)
Comparator.
Definition: macros.h:45
AV_PIX_FMT_YUV444P10
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:392
AV_PIX_FMT_YUVJ411P
@ 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:248
planes
static const struct @318 planes[]
BM3DContext::group_bits
int group_bits
Definition: vf_bm3d.c:108
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
inputs
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
Definition: filter_design.txt:243
AV_PIX_FMT_YUV422P16
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:401
ff_set_common_formats
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:580
SliceContext::match_blocks
PosPairCode match_blocks[256]
Definition: vf_bm3d.c:83
BM3DContext::fs
FFFrameSync fs
Definition: vf_bm3d.c:113
av_dct_end
void av_dct_end(DCTContext *s)
BM3DContext::do_output
void(* do_output)(struct BM3DContext *s, uint8_t *dst, int dst_linesize, int plane, int nb_jobs)
Definition: vf_bm3d.c:121
SliceContext::dctf
DCTContext * dctf
Definition: vf_bm3d.c:73
AV_PIX_FMT_YUVJ422P
@ 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_GBRAP10
#define AV_PIX_FMT_GBRAP10
Definition: pixfmt.h:409
ThreadData::plane
int plane
Definition: vf_blend.c:58
ff_outlink_set_status
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition: filters.h:189
ff_inlink_request_frame
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Definition: avfilter.c:1496
width
#define width
s
#define s(width, name)
Definition: cbs_vp9.c:257
AV_PIX_FMT_GBRAP12
#define AV_PIX_FMT_GBRAP12
Definition: pixfmt.h:410
AV_PIX_FMT_YUVA420P
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:101
search_boundary
static int search_boundary(int plane_boundary, int search_range, int search_step, int vertical, int y, int x)
Definition: vf_bm3d.c:222
AV_PIX_FMT_YUV444P16
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:402
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:51
BM3DContext::bm_range
int bm_range
Definition: vf_bm3d.c:95
PosCode::x
int x
Definition: vf_bm3d.c:63
slice_end
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:2033
PosCode
Definition: vf_bm3d.c:62
outputs
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
filters.h
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:290
AV_PIX_FMT_YUVA444P12
#define AV_PIX_FMT_YUVA444P12
Definition: pixfmt.h:430
AV_PIX_FMT_YUV420P9
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:386
AV_PIX_FMT_YUV420P16
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:400
ctx
AVFormatContext * ctx
Definition: movenc.c:48
final_block_filtering
static void final_block_filtering(BM3DContext *s, const uint8_t *src, int src_linesize, const uint8_t *ref, int ref_linesize, int y, int x, int plane, int jobnr)
Definition: vf_bm3d.c:512
AV_PIX_FMT_GRAY14
#define AV_PIX_FMT_GRAY14
Definition: pixfmt.h:372
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:141
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
BM3DContext::max
int max
Definition: vf_bm3d.c:104
filter_slice
static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_bm3d.c:699
f
#define f(width, name)
Definition: cbs_vp9.c:255
AV_PIX_FMT_YUVJ444P
@ 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
arg
const char * arg
Definition: jacosubdec.c:67
AV_PIX_FMT_GRAY10
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:370
AV_PIX_FMT_GBRP16
#define AV_PIX_FMT_GBRP16
Definition: pixfmt.h:408
SQR
#define SQR(x)
Definition: vf_bm3d.c:777
av_dct_calc
void av_dct_calc(DCTContext *s, FFTSample *data)
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
process_frame
static int process_frame(FFFrameSync *fs)
Definition: vf_bm3d.c:887
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:537
fs
#define fs(width, name, subs,...)
Definition: cbs_vp9.c:259
ThreadData::src
const uint8_t * src
Definition: vf_bm3d.c:55
basic_block_filtering
static void basic_block_filtering(BM3DContext *s, const uint8_t *src, int src_linesize, const uint8_t *ref, int ref_linesize, int y, int x, int plane, int jobnr)
Definition: vf_bm3d.c:393
isnan
#define isnan(x)
Definition: libm.h:340
AV_PIX_FMT_YUVJ420P
@ 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
do_output
static void do_output(BM3DContext *s, uint8_t *dst, int dst_linesize, int plane, int nb_jobs)
Definition: vf_bm3d.c:644
src
#define src
Definition: vp8dsp.c:255
AV_PIX_FMT_YUV440P10
#define AV_PIX_FMT_YUV440P10
Definition: pixfmt.h:391
MAX_NB_THREADS
#define MAX_NB_THREADS
Definition: vf_bm3d.c:46
FFTSample
float FFTSample
Definition: avfft.h:35
avfft.h
AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:390
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
AV_PIX_FMT_GBRP9
#define AV_PIX_FMT_GBRP9
Definition: pixfmt.h:404
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_bm3d.c:1016
BM3DContext::planewidth
int planewidth[4]
Definition: vf_bm3d.c:106
ff_inlink_acknowledge_status
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
Definition: avfilter.c:1333
SliceContext::gdcti
DCTContext * gdcti
Definition: vf_bm3d.c:72
SliceContext
Definition: mss12.h:70
FINAL
@ FINAL
Definition: vf_bm3d.c:50
AVFilterPad::config_props
int(* config_props)(AVFilterLink *link)
Link configuration callback.
Definition: internal.h:116
cmp_scores
static int cmp_scores(const void *a, const void *b)
Definition: vf_bm3d.c:227
SliceContext::rbufferv
FFTSample * rbufferv
Definition: vf_bm3d.c:79
OFFSET
#define OFFSET(x)
Definition: vf_bm3d.c:129
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:116
BM3DContext::pgroup_size
int pgroup_size
Definition: vf_bm3d.c:109
PosPairCode
Definition: vf_bm3d.c:66
AV_PIX_FMT_YUV422P12
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:394
bm3d_outputs
static const AVFilterPad bm3d_outputs[]
Definition: vf_bm3d.c:1048
SliceContext::buffer
FFTSample * buffer
Definition: vf_bm3d.c:77
AV_PIX_FMT_YUV444P12
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:396
BM3DContext::bm_step
int bm_step
Definition: vf_bm3d.c:96
height
#define height
a
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
Definition: undefined.txt:41
AV_PIX_FMT_YUVA444P
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:167
AV_PIX_FMT_YUVA444P10
#define AV_PIX_FMT_YUVA444P10
Definition: pixfmt.h:428
BM3DContext::group_size
int group_size
Definition: vf_bm3d.c:94
SliceContext::search_positions
PosCode * search_positions
Definition: vf_bm3d.c:85
BM3DContext::planeheight
int planeheight[4]
Definition: vf_bm3d.c:107
internal.h
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:227
i
int i
Definition: input.c:406
lrintf
#define lrintf(x)
Definition: libm_mips.h:72
BM3DContext::get_block_row
void(* get_block_row)(const uint8_t *srcp, int src_linesize, int y, int x, int block_size, float *dst)
Definition: vf_bm3d.c:116
AV_PIX_FMT_GBRP12
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:406
do_output16
static void do_output16(BM3DContext *s, uint8_t *dst, int dst_linesize, int plane, int nb_jobs)
Definition: vf_bm3d.c:671
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:763
ThreadData
Used for passing data between threads.
Definition: dsddec.c:67
do_search_boundary
static int do_search_boundary(int pos, int plane_boundary, int search_range, int search_step)
Definition: vf_bm3d.c:197
config_input
static int config_input(AVFilterLink *inlink)
Definition: vf_bm3d.c:779
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
PosPairCode::y
int y
Definition: vf_bm3d.c:68
block_matching_multi
static void block_matching_multi(BM3DContext *s, const uint8_t *ref, int ref_linesize, int y, int x, int exclude_cur_pos, int plane, int jobnr)
Definition: vf_bm3d.c:314
AV_PIX_FMT_YUVJ440P
@ 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
DCTContext
Definition: dct.h:32
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:58
AV_PIX_FMT_YUV444P9
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:388
filter_frame
static int filter_frame(AVFilterContext *ctx, AVFrame **out, AVFrame *in, AVFrame *ref)
Definition: vf_bm3d.c:742
do_block_matching_multi
static void do_block_matching_multi(BM3DContext *s, const uint8_t *src, int src_stride, int src_range, const PosCode *search_pos, int search_size, float th_mse, int r_y, int r_x, int plane, int jobnr)
Definition: vf_bm3d.c:276
AVFilter
Filter definition.
Definition: avfilter.h:145
ret
ret
Definition: filter_design.txt:187
AVFilterPad::type
enum AVMediaType type
AVFilterPad type.
Definition: internal.h:63
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AV_PIX_FMT_YUVA444P9
#define AV_PIX_FMT_YUVA444P9
Definition: pixfmt.h:425
ff_framesync_init
int ff_framesync_init(FFFrameSync *fs, AVFilterContext *parent, unsigned nb_in)
Initialize a frame sync structure.
Definition: framesync.c:84
pos
unsigned int pos
Definition: spdifenc.c:412
SliceContext::rbufferh
FFTSample * rbufferh
Definition: vf_bm3d.c:78
AV_PIX_FMT_YUV420P12
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:393
AV_PIX_FMT_YUV422P14
#define AV_PIX_FMT_YUV422P14
Definition: pixfmt.h:398
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_bm3d.c:907
ThreadData::src_linesize
int src_linesize
Definition: vf_bm3d.c:56
FFFrameSyncIn::before
enum FFFrameSyncExtMode before
Extrapolation mode for timestamps before the first frame.
Definition: framesync.h:86
BM3DContext::sigma
float sigma
Definition: vf_bm3d.c:91
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:251
framesync.h
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
mode
mode
Definition: ebur128.h:83
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
get_block_row
static void get_block_row(const uint8_t *srcp, int src_linesize, int y, int x, int block_size, float *dst)
Definition: vf_bm3d.c:371
AV_PIX_FMT_YUVA422P12
#define AV_PIX_FMT_YUVA422P12
Definition: pixfmt.h:429
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
avfilter.h
BM3DContext::block_size
int block_size
Definition: vf_bm3d.c:92
config_output
static int config_output(AVFilterLink *outlink)
Definition: vf_bm3d.c:962
SliceContext::bufferv
FFTSample * bufferv
Definition: vf_bm3d.c:75
PosCode::y
int y
Definition: vf_bm3d.c:63
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
temp
else temp
Definition: vf_mcdeint.c:259
SliceContext::rbuffer
FFTSample * rbuffer
Definition: vf_bm3d.c:81
do_block_ssd16
static double do_block_ssd16(BM3DContext *s, PosCode *pos, const uint8_t *src, int src_stride, int r_y, int r_x)
Definition: vf_bm3d.c:255
PosPairCode::score
double score
Definition: vf_bm3d.c:67
av_clip_uint8
#define av_clip_uint8
Definition: common.h:102
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
AVFilterContext
An instance of a filter.
Definition: avfilter.h:333
AV_PIX_FMT_GBRP
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:158
AVFILTER_FLAG_SLICE_THREADS
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:117
desc
const char * desc
Definition: libsvtav1.c:79
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
BM3DContext::nb_planes
int nb_planes
Definition: vf_bm3d.c:105
M_SQRT2
#define M_SQRT2
Definition: mathematics.h:61
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
SliceContext::bufferh
FFTSample * bufferh
Definition: vf_bm3d.c:74
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:241
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
BM3DContext::nb_threads
int nb_threads
Definition: vf_bm3d.c:114
AV_PIX_FMT_YUV411P
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
DCT_II
@ DCT_II
Definition: avfft.h:94
AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
#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
imgutils.h
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:334
AV_PIX_FMT_YUV410P
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:72
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
FFFrameSyncIn::after
enum FFFrameSyncExtMode after
Extrapolation mode for timestamps after the last frame.
Definition: framesync.h:91
BM3DContext::depth
int depth
Definition: vf_bm3d.c:103
AV_PIX_FMT_YUV440P12
#define AV_PIX_FMT_YUV440P12
Definition: pixfmt.h:395
SliceContext::gdctf
DCTContext * gdctf
Definition: vf_bm3d.c:72
ff_outlink_frame_wanted
the definition of that something depends on the semantic of the filter The callback must examine the status of the filter s links and proceed accordingly The status of output links is stored in the status_in and status_out fields and tested by the ff_outlink_frame_wanted() function. If this function returns true
AV_PIX_FMT_YUV444P14
#define AV_PIX_FMT_YUV444P14
Definition: pixfmt.h:399
BM3DContext::block_filtering
void(* block_filtering)(struct BM3DContext *s, const uint8_t *src, int src_linesize, const uint8_t *ref, int ref_linesize, int y, int x, int plane, int jobnr)
Definition: vf_bm3d.c:123
get_block_row16
static void get_block_row16(const uint8_t *srcp, int src_linesize, int y, int x, int block_size, float *dst)
Definition: vf_bm3d.c:382
ff_framesync_activate
int ff_framesync_activate(FFFrameSync *fs)
Examine the frames in the filter's input and try to produce output.
Definition: framesync.c:341
AV_PIX_FMT_GRAY12
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:371
BM3DContext
Definition: vf_bm3d.c:88
ThreadData::ref
const uint8_t * ref
Definition: vf_bm3d.c:57
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:233
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
ff_vf_bm3d
const AVFilter ff_vf_bm3d
Definition: vf_bm3d.c:1057
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: vf_bm3d.c:163
AV_PIX_FMT_YUVA422P
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:166
AV_PIX_FMT_YUV420P14
#define AV_PIX_FMT_YUV420P14
Definition: pixfmt.h:397
activate
static int activate(AVFilterContext *ctx)
Definition: vf_bm3d.c:853
FilterModes
FilterModes
Definition: vf_bm3d.c:48