FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_hqdn3d.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2003 Daniel Moreno <comac AT comac DOT darktech DOT org>
3  * Copyright (c) 2010 Baptiste Coudurier
4  * Copyright (c) 2012 Loren Merritt
5  *
6  * This file is part of FFmpeg, ported from MPlayer.
7  *
8  * FFmpeg is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (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
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 /**
24  * @file
25  * high quality 3d video denoiser, ported from MPlayer
26  * libmpcodecs/vf_hqdn3d.c.
27  */
28 
29 #include "libavutil/common.h"
30 #include "libavutil/pixdesc.h"
31 #include "libavutil/intreadwrite.h"
32 #include "avfilter.h"
33 #include "formats.h"
34 #include "internal.h"
35 #include "video.h"
36 
37 typedef struct {
38  int16_t *coefs[4];
39  uint16_t *line;
40  uint16_t *frame_prev[3];
41  double strength[4];
42  int hsub, vsub;
43  int depth;
44  void (*denoise_row[17])(uint8_t *src, uint8_t *dst, uint16_t *line_ant, uint16_t *frame_ant, ptrdiff_t w, int16_t *spatial, int16_t *temporal);
46 
47 void ff_hqdn3d_row_8_x86(uint8_t *src, uint8_t *dst, uint16_t *line_ant, uint16_t *frame_ant, ptrdiff_t w, int16_t *spatial, int16_t *temporal);
48 void ff_hqdn3d_row_9_x86(uint8_t *src, uint8_t *dst, uint16_t *line_ant, uint16_t *frame_ant, ptrdiff_t w, int16_t *spatial, int16_t *temporal);
49 void ff_hqdn3d_row_10_x86(uint8_t *src, uint8_t *dst, uint16_t *line_ant, uint16_t *frame_ant, ptrdiff_t w, int16_t *spatial, int16_t *temporal);
50 void ff_hqdn3d_row_16_x86(uint8_t *src, uint8_t *dst, uint16_t *line_ant, uint16_t *frame_ant, ptrdiff_t w, int16_t *spatial, int16_t *temporal);
51 
52 #define LUT_BITS (depth==16 ? 8 : 4)
53 #define LOAD(x) (((depth == 8 ? src[x] : AV_RN16A(src + (x) * 2)) << (16 - depth))\
54  + (((1 << (16 - depth)) - 1) >> 1))
55 #define STORE(x,val) (depth == 8 ? dst[x] = (val) >> (16 - depth) : \
56  AV_WN16A(dst + (x) * 2, (val) >> (16 - depth)))
57 
59 static uint32_t lowpass(int prev, int cur, int16_t *coef, int depth)
60 {
61  int d = (prev - cur) >> (8 - LUT_BITS);
62  return cur + coef[d];
63 }
64 
66 static void denoise_temporal(uint8_t *src, uint8_t *dst,
67  uint16_t *frame_ant,
68  int w, int h, int sstride, int dstride,
69  int16_t *temporal, int depth)
70 {
71  long x, y;
72  uint32_t tmp;
73 
74  temporal += 256 << LUT_BITS;
75 
76  for (y = 0; y < h; y++) {
77  for (x = 0; x < w; x++) {
78  frame_ant[x] = tmp = lowpass(frame_ant[x], LOAD(x), temporal, depth);
79  STORE(x, tmp);
80  }
81  src += sstride;
82  dst += dstride;
83  frame_ant += w;
84  }
85 }
86 
88 static void denoise_spatial(HQDN3DContext *hqdn3d,
89  uint8_t *src, uint8_t *dst,
90  uint16_t *line_ant, uint16_t *frame_ant,
91  int w, int h, int sstride, int dstride,
92  int16_t *spatial, int16_t *temporal, int depth)
93 {
94  long x, y;
95  uint32_t pixel_ant;
96  uint32_t tmp;
97 
98  spatial += 256 << LUT_BITS;
99  temporal += 256 << LUT_BITS;
100 
101  /* First line has no top neighbor. Only left one for each tmp and
102  * last frame */
103  pixel_ant = LOAD(0);
104  for (x = 0; x < w; x++) {
105  line_ant[x] = tmp = pixel_ant = lowpass(pixel_ant, LOAD(x), spatial, depth);
106  frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
107  STORE(x, tmp);
108  }
109 
110  for (y = 1; y < h; y++) {
111  src += sstride;
112  dst += dstride;
113  frame_ant += w;
114  if (hqdn3d->denoise_row[depth]) {
115  hqdn3d->denoise_row[depth](src, dst, line_ant, frame_ant, w, spatial, temporal);
116  continue;
117  }
118  pixel_ant = LOAD(0);
119  for (x = 0; x < w-1; x++) {
120  line_ant[x] = tmp = lowpass(line_ant[x], pixel_ant, spatial, depth);
121  pixel_ant = lowpass(pixel_ant, LOAD(x+1), spatial, depth);
122  frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
123  STORE(x, tmp);
124  }
125  line_ant[x] = tmp = lowpass(line_ant[x], pixel_ant, spatial, depth);
126  frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
127  STORE(x, tmp);
128  }
129 }
130 
132 static void denoise_depth(HQDN3DContext *hqdn3d,
133  uint8_t *src, uint8_t *dst,
134  uint16_t *line_ant, uint16_t **frame_ant_ptr,
135  int w, int h, int sstride, int dstride,
136  int16_t *spatial, int16_t *temporal, int depth)
137 {
138  // FIXME: For 16bit depth, frame_ant could be a pointer to the previous
139  // filtered frame rather than a separate buffer.
140  long x, y;
141  uint16_t *frame_ant = *frame_ant_ptr;
142  if (!frame_ant) {
143  uint8_t *frame_src = src;
144  *frame_ant_ptr = frame_ant = av_malloc(w*h*sizeof(uint16_t));
145  for (y = 0; y < h; y++, src += sstride, frame_ant += w)
146  for (x = 0; x < w; x++)
147  frame_ant[x] = LOAD(x);
148  src = frame_src;
149  frame_ant = *frame_ant_ptr;
150  }
151 
152  if (spatial[0])
153  denoise_spatial(hqdn3d, src, dst, line_ant, frame_ant,
154  w, h, sstride, dstride, spatial, temporal, depth);
155  else
156  denoise_temporal(src, dst, frame_ant,
157  w, h, sstride, dstride, temporal, depth);
158 }
159 
160 #define denoise(...) \
161  switch (hqdn3d->depth) {\
162  case 8: denoise_depth(__VA_ARGS__, 8); break;\
163  case 9: denoise_depth(__VA_ARGS__, 9); break;\
164  case 10: denoise_depth(__VA_ARGS__, 10); break;\
165  case 16: denoise_depth(__VA_ARGS__, 16); break;\
166  }
167 
168 static int16_t *precalc_coefs(double dist25, int depth)
169 {
170  int i;
171  double gamma, simil, C;
172  int16_t *ct = av_malloc((512<<LUT_BITS)*sizeof(int16_t));
173  if (!ct)
174  return NULL;
175 
176  gamma = log(0.25) / log(1.0 - FFMIN(dist25,252.0)/255.0 - 0.00001);
177 
178  for (i = -255<<LUT_BITS; i <= 255<<LUT_BITS; i++) {
179  double f = ((i<<(9-LUT_BITS)) + (1<<(8-LUT_BITS)) - 1) / 512.0; // midpoint of the bin
180  simil = 1.0 - FFABS(f) / 255.0;
181  C = pow(simil, gamma) * 256.0 * f;
182  ct[(256<<LUT_BITS)+i] = lrint(C);
183  }
184 
185  ct[0] = !!dist25;
186  return ct;
187 }
188 
189 #define PARAM1_DEFAULT 4.0
190 #define PARAM2_DEFAULT 3.0
191 #define PARAM3_DEFAULT 6.0
192 
193 static int init(AVFilterContext *ctx, const char *args)
194 {
195  HQDN3DContext *hqdn3d = ctx->priv;
196  double lum_spac, lum_tmp, chrom_spac, chrom_tmp;
197  double param1, param2, param3, param4;
198 
199  lum_spac = PARAM1_DEFAULT;
200  chrom_spac = PARAM2_DEFAULT;
201  lum_tmp = PARAM3_DEFAULT;
202  chrom_tmp = lum_tmp * chrom_spac / lum_spac;
203 
204  if (args) {
205  switch (sscanf(args, "%lf:%lf:%lf:%lf",
206  &param1, &param2, &param3, &param4)) {
207  case 1:
208  lum_spac = param1;
209  chrom_spac = PARAM2_DEFAULT * param1 / PARAM1_DEFAULT;
210  lum_tmp = PARAM3_DEFAULT * param1 / PARAM1_DEFAULT;
211  chrom_tmp = lum_tmp * chrom_spac / lum_spac;
212  break;
213  case 2:
214  lum_spac = param1;
215  chrom_spac = param2;
216  lum_tmp = PARAM3_DEFAULT * param1 / PARAM1_DEFAULT;
217  chrom_tmp = lum_tmp * chrom_spac / lum_spac;
218  break;
219  case 3:
220  lum_spac = param1;
221  chrom_spac = param2;
222  lum_tmp = param3;
223  chrom_tmp = lum_tmp * chrom_spac / lum_spac;
224  break;
225  case 4:
226  lum_spac = param1;
227  chrom_spac = param2;
228  lum_tmp = param3;
229  chrom_tmp = param4;
230  break;
231  }
232  }
233 
234  hqdn3d->strength[0] = lum_spac;
235  hqdn3d->strength[1] = lum_tmp;
236  hqdn3d->strength[2] = chrom_spac;
237  hqdn3d->strength[3] = chrom_tmp;
238 
239  av_log(ctx, AV_LOG_VERBOSE, "ls:%f cs:%f lt:%f ct:%f\n",
240  lum_spac, chrom_spac, lum_tmp, chrom_tmp);
241  if (lum_spac < 0 || chrom_spac < 0 || isnan(chrom_tmp)) {
242  av_log(ctx, AV_LOG_ERROR,
243  "Invalid negative value for luma or chroma spatial strength, "
244  "or resulting value for chroma temporal strength is nan.\n");
245  return AVERROR(EINVAL);
246  }
247 
248  return 0;
249 }
250 
251 static void uninit(AVFilterContext *ctx)
252 {
253  HQDN3DContext *hqdn3d = ctx->priv;
254 
255  av_freep(&hqdn3d->coefs[0]);
256  av_freep(&hqdn3d->coefs[1]);
257  av_freep(&hqdn3d->coefs[2]);
258  av_freep(&hqdn3d->coefs[3]);
259  av_freep(&hqdn3d->line);
260  av_freep(&hqdn3d->frame_prev[0]);
261  av_freep(&hqdn3d->frame_prev[1]);
262  av_freep(&hqdn3d->frame_prev[2]);
263 }
264 
266 {
267  static const enum AVPixelFormat pix_fmts[] = {
288  };
289 
291 
292  return 0;
293 }
294 
295 static int config_input(AVFilterLink *inlink)
296 {
297  HQDN3DContext *hqdn3d = inlink->dst->priv;
298  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
299  int i;
300 
301  hqdn3d->hsub = desc->log2_chroma_w;
302  hqdn3d->vsub = desc->log2_chroma_h;
303  hqdn3d->depth = desc->comp[0].depth_minus1+1;
304 
305  hqdn3d->line = av_malloc(inlink->w * sizeof(*hqdn3d->line));
306  if (!hqdn3d->line)
307  return AVERROR(ENOMEM);
308 
309  for (i = 0; i < 4; i++) {
310  hqdn3d->coefs[i] = precalc_coefs(hqdn3d->strength[i], hqdn3d->depth);
311  if (!hqdn3d->coefs[i])
312  return AVERROR(ENOMEM);
313  }
314 
315 #if HAVE_YASM
316  hqdn3d->denoise_row[ 8] = ff_hqdn3d_row_8_x86;
317  hqdn3d->denoise_row[ 9] = ff_hqdn3d_row_9_x86;
318  hqdn3d->denoise_row[10] = ff_hqdn3d_row_10_x86;
319  hqdn3d->denoise_row[16] = ff_hqdn3d_row_16_x86;
320 #endif
321 
322  return 0;
323 }
324 
326 {
327  HQDN3DContext *hqdn3d = inlink->dst->priv;
328  AVFilterLink *outlink = inlink->dst->outputs[0];
329 
330  AVFilterBufferRef *out;
331  int direct, c;
332 
333  if (in->perms & AV_PERM_WRITE) {
334  direct = 1;
335  out = in;
336  } else {
337  direct = 0;
338  out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
339  if (!out) {
341  return AVERROR(ENOMEM);
342  }
344  }
345 
346  for (c = 0; c < 3; c++) {
347  denoise(hqdn3d, in->data[c], out->data[c],
348  hqdn3d->line, &hqdn3d->frame_prev[c],
349  in->video->w >> (!!c * hqdn3d->hsub),
350  in->video->h >> (!!c * hqdn3d->vsub),
351  in->linesize[c], out->linesize[c],
352  hqdn3d->coefs[c?2:0], hqdn3d->coefs[c?3:1]);
353  }
354 
355  if (!direct)
357 
358  return ff_filter_frame(outlink, out);
359 }
360 
362  {
363  .name = "default",
364  .type = AVMEDIA_TYPE_VIDEO,
365  .config_props = config_input,
366  .filter_frame = filter_frame,
367  },
368  { NULL }
369 };
370 
371 
373  {
374  .name = "default",
375  .type = AVMEDIA_TYPE_VIDEO
376  },
377  { NULL }
378 };
379 
381  .name = "hqdn3d",
382  .description = NULL_IF_CONFIG_SMALL("Apply a High Quality 3D Denoiser."),
383 
384  .priv_size = sizeof(HQDN3DContext),
385  .init = init,
386  .uninit = uninit,
388 
389  .inputs = avfilter_vf_hqdn3d_inputs,
390 
391  .outputs = avfilter_vf_hqdn3d_outputs,
392 };