h264_metadata: Add option to delete filler data
[libav.git] / libavfilter / vf_hqdn3d.c
CommitLineData
a4dc7aa5
BC
1/*
2 * Copyright (c) 2003 Daniel Moreno <comac AT comac DOT darktech DOT org>
3 * Copyright (c) 2010 Baptiste Coudurier
85e228c7 4 * Copyright (c) 2012 Loren Merritt
a4dc7aa5 5 *
2912e87a 6 * This file is part of Libav, ported from MPlayer.
a4dc7aa5 7 *
2912e87a 8 * Libav is free software; you can redistribute it and/or modify
a4dc7aa5
BC
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 *
2912e87a 13 * Libav is distributed in the hope that it will be useful,
a4dc7aa5
BC
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
2912e87a 19 * with Libav; if not, write to the Free Software Foundation, Inc.,
a4dc7aa5
BC
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
8c747d46
AK
29#include <float.h>
30
76d90125 31#include "config.h"
093804a9 32#include "libavutil/attributes.h"
1d9c2dc8 33#include "libavutil/common.h"
a4dc7aa5 34#include "libavutil/pixdesc.h"
1ad715db 35#include "libavutil/intreadwrite.h"
8c747d46
AK
36#include "libavutil/opt.h"
37
a4dc7aa5 38#include "avfilter.h"
b74a1da4 39#include "formats.h"
9d0bfc50 40#include "internal.h"
803391f7 41#include "video.h"
76d90125 42#include "vf_hqdn3d.h"
7a1944b9 43
566858a7 44#define LUT_BITS (depth==16 ? 8 : 4)
5b3c1aec
LM
45#define LOAD(x) (((depth == 8 ? src[x] : AV_RN16A(src + (x) * 2)) << (16 - depth))\
46 + (((1 << (16 - depth)) - 1) >> 1))
47#define STORE(x,val) (depth == 8 ? dst[x] = (val) >> (16 - depth) : \
48 AV_WN16A(dst + (x) * 2, (val) >> (16 - depth)))
1ad715db 49
566858a7 50av_always_inline
b441a451 51static uint32_t lowpass(int prev, int cur, int16_t *coef, int depth)
a4dc7aa5 52{
566858a7 53 int d = (prev - cur) >> (8 - LUT_BITS);
60b97855 54 return cur + coef[d];
a4dc7aa5
BC
55}
56
1ad715db 57av_always_inline
60b97855
LM
58static void denoise_temporal(uint8_t *src, uint8_t *dst,
59 uint16_t *frame_ant,
60 int w, int h, int sstride, int dstride,
1ad715db 61 int16_t *temporal, int depth)
a4dc7aa5 62{
60b97855 63 long x, y;
85e228c7 64 uint32_t tmp;
60b97855 65
566858a7 66 temporal += 256 << LUT_BITS;
0f583e6c 67
60b97855
LM
68 for (y = 0; y < h; y++) {
69 for (x = 0; x < w; x++) {
566858a7 70 frame_ant[x] = tmp = lowpass(frame_ant[x], LOAD(x), temporal, depth);
1ad715db 71 STORE(x, tmp);
a4dc7aa5 72 }
60b97855
LM
73 src += sstride;
74 dst += dstride;
75 frame_ant += w;
a4dc7aa5
BC
76 }
77}
78
1ad715db 79av_always_inline
56e4ce0d 80static void denoise_spatial(HQDN3DContext *s,
7a1944b9 81 uint8_t *src, uint8_t *dst,
0f583e6c 82 uint16_t *line_ant, uint16_t *frame_ant,
60b97855 83 int w, int h, int sstride, int dstride,
1ad715db 84 int16_t *spatial, int16_t *temporal, int depth)
a4dc7aa5 85{
60b97855 86 long x, y;
60b97855 87 uint32_t pixel_ant;
85e228c7 88 uint32_t tmp;
a4dc7aa5 89
566858a7
LM
90 spatial += 256 << LUT_BITS;
91 temporal += 256 << LUT_BITS;
0f583e6c 92
85e228c7
LM
93 /* First line has no top neighbor. Only left one for each tmp and
94 * last frame */
1ad715db 95 pixel_ant = LOAD(0);
85e228c7 96 for (x = 0; x < w; x++) {
566858a7
LM
97 line_ant[x] = tmp = pixel_ant = lowpass(pixel_ant, LOAD(x), spatial, depth);
98 frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
1ad715db 99 STORE(x, tmp);
a4dc7aa5
BC
100 }
101
60b97855 102 for (y = 1; y < h; y++) {
85e228c7
LM
103 src += sstride;
104 dst += dstride;
105 frame_ant += w;
56e4ce0d
AK
106 if (s->denoise_row[depth]) {
107 s->denoise_row[depth](src, dst, line_ant, frame_ant, w, spatial, temporal);
7a1944b9
LM
108 continue;
109 }
1ad715db 110 pixel_ant = LOAD(0);
85e228c7 111 for (x = 0; x < w-1; x++) {
566858a7
LM
112 line_ant[x] = tmp = lowpass(line_ant[x], pixel_ant, spatial, depth);
113 pixel_ant = lowpass(pixel_ant, LOAD(x+1), spatial, depth);
114 frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
1ad715db 115 STORE(x, tmp);
a4dc7aa5 116 }
566858a7
LM
117 line_ant[x] = tmp = lowpass(line_ant[x], pixel_ant, spatial, depth);
118 frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
1ad715db 119 STORE(x, tmp);
a4dc7aa5
BC
120 }
121}
122
1ad715db 123av_always_inline
22b985d5
VG
124static int denoise_depth(HQDN3DContext *s,
125 uint8_t *src, uint8_t *dst,
126 uint16_t *line_ant, uint16_t **frame_ant_ptr,
127 int w, int h, int sstride, int dstride,
128 int16_t *spatial, int16_t *temporal, int depth)
a4dc7aa5 129{
41ed7ab4 130 // FIXME: For 16-bit depth, frame_ant could be a pointer to the previous
566858a7 131 // filtered frame rather than a separate buffer.
60b97855 132 long x, y;
60b97855 133 uint16_t *frame_ant = *frame_ant_ptr;
60b97855 134 if (!frame_ant) {
85e228c7 135 uint8_t *frame_src = src;
60b97855 136 *frame_ant_ptr = frame_ant = av_malloc(w*h*sizeof(uint16_t));
22b985d5
VG
137 if (!frame_ant)
138 return AVERROR(ENOMEM);
85e228c7 139 for (y = 0; y < h; y++, src += sstride, frame_ant += w)
60b97855 140 for (x = 0; x < w; x++)
1ad715db 141 frame_ant[x] = LOAD(x);
85e228c7
LM
142 src = frame_src;
143 frame_ant = *frame_ant_ptr;
a4dc7aa5
BC
144 }
145
85e228c7 146 if (spatial[0])
56e4ce0d 147 denoise_spatial(s, src, dst, line_ant, frame_ant,
1ad715db 148 w, h, sstride, dstride, spatial, temporal, depth);
85e228c7 149 else
60b97855 150 denoise_temporal(src, dst, frame_ant,
1ad715db 151 w, h, sstride, dstride, temporal, depth);
e995cf1b 152 emms_c();
22b985d5 153 return 0;
a4dc7aa5
BC
154}
155
22b985d5
VG
156#define denoise(...) \
157 do { \
158 int ret = AVERROR_INVALIDDATA; \
159 switch (s->depth) { \
160 case 8: ret = denoise_depth(__VA_ARGS__, 8); break; \
161 case 9: ret = denoise_depth(__VA_ARGS__, 9); break; \
162 case 10: ret = denoise_depth(__VA_ARGS__, 10); break; \
163 case 16: ret = denoise_depth(__VA_ARGS__, 16); break; \
164 } \
165 if (ret < 0) { \
166 av_frame_free(&out); \
167 if (!direct) \
168 av_frame_free(&in); \
169 return ret; \
170 } \
171 } while (0)
1ad715db 172
566858a7 173static int16_t *precalc_coefs(double dist25, int depth)
a4dc7aa5
BC
174{
175 int i;
60b97855 176 double gamma, simil, C;
566858a7
LM
177 int16_t *ct = av_malloc((512<<LUT_BITS)*sizeof(int16_t));
178 if (!ct)
179 return NULL;
a4dc7aa5 180
0f583e6c 181 gamma = log(0.25) / log(1.0 - FFMIN(dist25,252.0)/255.0 - 0.00001);
a4dc7aa5 182
566858a7
LM
183 for (i = -255<<LUT_BITS; i <= 255<<LUT_BITS; i++) {
184 double f = ((i<<(9-LUT_BITS)) + (1<<(8-LUT_BITS)) - 1) / 512.0; // midpoint of the bin
0f583e6c
LM
185 simil = 1.0 - FFABS(f) / 255.0;
186 C = pow(simil, gamma) * 256.0 * f;
566858a7 187 ct[(256<<LUT_BITS)+i] = lrint(C);
a4dc7aa5
BC
188 }
189
60b97855 190 ct[0] = !!dist25;
566858a7 191 return ct;
a4dc7aa5
BC
192}
193
194#define PARAM1_DEFAULT 4.0
195#define PARAM2_DEFAULT 3.0
196#define PARAM3_DEFAULT 6.0
197
093804a9 198static av_cold int init(AVFilterContext *ctx)
a4dc7aa5 199{
56e4ce0d 200 HQDN3DContext *s = ctx->priv;
a4dc7aa5 201
56e4ce0d
AK
202 if (!s->strength[LUMA_SPATIAL])
203 s->strength[LUMA_SPATIAL] = PARAM1_DEFAULT;
204 if (!s->strength[CHROMA_SPATIAL])
205 s->strength[CHROMA_SPATIAL] = PARAM2_DEFAULT * s->strength[LUMA_SPATIAL] / PARAM1_DEFAULT;
206 if (!s->strength[LUMA_TMP])
207 s->strength[LUMA_TMP] = PARAM3_DEFAULT * s->strength[LUMA_SPATIAL] / PARAM1_DEFAULT;
208 if (!s->strength[CHROMA_TMP])
209 s->strength[CHROMA_TMP] = s->strength[LUMA_TMP] * s->strength[CHROMA_SPATIAL] / s->strength[LUMA_SPATIAL];
566858a7 210
bcb8d9eb 211 av_log(ctx, AV_LOG_VERBOSE, "ls:%f cs:%f lt:%f ct:%f\n",
56e4ce0d
AK
212 s->strength[LUMA_SPATIAL], s->strength[CHROMA_SPATIAL],
213 s->strength[LUMA_TMP], s->strength[CHROMA_TMP]);
a4dc7aa5 214
a4dc7aa5
BC
215 return 0;
216}
217
093804a9 218static av_cold void uninit(AVFilterContext *ctx)
a4dc7aa5 219{
56e4ce0d
AK
220 HQDN3DContext *s = ctx->priv;
221
222 av_freep(&s->coefs[0]);
223 av_freep(&s->coefs[1]);
224 av_freep(&s->coefs[2]);
225 av_freep(&s->coefs[3]);
226 av_freep(&s->line);
227 av_freep(&s->frame_prev[0]);
228 av_freep(&s->frame_prev[1]);
229 av_freep(&s->frame_prev[2]);
a4dc7aa5
BC
230}
231
232static int query_formats(AVFilterContext *ctx)
233{
716d413c
AK
234 static const enum AVPixelFormat pix_fmts[] = {
235 AV_PIX_FMT_YUV420P,
236 AV_PIX_FMT_YUV422P,
237 AV_PIX_FMT_YUV444P,
238 AV_PIX_FMT_YUV410P,
239 AV_PIX_FMT_YUV411P,
240 AV_PIX_FMT_YUV440P,
241 AV_PIX_FMT_YUVJ420P,
242 AV_PIX_FMT_YUVJ422P,
243 AV_PIX_FMT_YUVJ444P,
244 AV_PIX_FMT_YUVJ440P,
245 AV_NE( AV_PIX_FMT_YUV420P9BE, AV_PIX_FMT_YUV420P9LE ),
246 AV_NE( AV_PIX_FMT_YUV422P9BE, AV_PIX_FMT_YUV422P9LE ),
247 AV_NE( AV_PIX_FMT_YUV444P9BE, AV_PIX_FMT_YUV444P9LE ),
248 AV_NE( AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUV420P10LE ),
249 AV_NE( AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUV422P10LE ),
250 AV_NE( AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUV444P10LE ),
251 AV_NE( AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUV420P16LE ),
252 AV_NE( AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUV422P16LE ),
253 AV_NE( AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUV444P16LE ),
254 AV_PIX_FMT_NONE
a4dc7aa5
BC
255 };
256
b74a1da4 257 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
a4dc7aa5
BC
258
259 return 0;
260}
261
262static int config_input(AVFilterLink *inlink)
263{
56e4ce0d 264 HQDN3DContext *s = inlink->dst->priv;
59ee9f78 265 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
2bd67175 266 int i;
a4dc7aa5 267
3ba35a34
AK
268 uninit(inlink->dst);
269
56e4ce0d
AK
270 s->hsub = desc->log2_chroma_w;
271 s->vsub = desc->log2_chroma_h;
2268db2c 272 s->depth = desc->comp[0].depth;
a4dc7aa5 273
56e4ce0d
AK
274 s->line = av_malloc(inlink->w * sizeof(*s->line));
275 if (!s->line)
a4dc7aa5
BC
276 return AVERROR(ENOMEM);
277
2bd67175 278 for (i = 0; i < 4; i++) {
56e4ce0d
AK
279 s->coefs[i] = precalc_coefs(s->strength[i], s->depth);
280 if (!s->coefs[i])
566858a7
LM
281 return AVERROR(ENOMEM);
282 }
283
76d90125 284 if (ARCH_X86)
56e4ce0d 285 ff_hqdn3d_init_x86(s);
7a1944b9 286
a4dc7aa5
BC
287 return 0;
288}
289
7e350379 290static int filter_frame(AVFilterLink *inlink, AVFrame *in)
a4dc7aa5 291{
56e4ce0d 292 HQDN3DContext *s = inlink->dst->priv;
a4dc7aa5 293 AVFilterLink *outlink = inlink->dst->outputs[0];
7e350379 294 AVFrame *out;
22b985d5 295 int c, direct = av_frame_is_writable(in);
565e4993 296
22b985d5 297 if (direct) {
565e4993
AK
298 out = in;
299 } else {
7e350379 300 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
565e4993 301 if (!out) {
7e350379 302 av_frame_free(&in);
565e4993
AK
303 return AVERROR(ENOMEM);
304 }
305
7e350379
AK
306 av_frame_copy_props(out, in);
307 out->width = outlink->w;
308 out->height = outlink->h;
565e4993 309 }
85e228c7
LM
310
311 for (c = 0; c < 3; c++) {
56e4ce0d
AK
312 denoise(s, in->data[c], out->data[c],
313 s->line, &s->frame_prev[c],
314 in->width >> (!!c * s->hsub),
315 in->height >> (!!c * s->vsub),
565e4993 316 in->linesize[c], out->linesize[c],
56e4ce0d 317 s->coefs[c?2:0], s->coefs[c?3:1]);
85e228c7 318 }
a4dc7aa5 319
565e4993 320 if (!direct)
7e350379 321 av_frame_free(&in);
565e4993
AK
322
323 return ff_filter_frame(outlink, out);
a4dc7aa5
BC
324}
325
8c747d46
AK
326#define OFFSET(x) offsetof(HQDN3DContext, x)
327#define FLAGS AV_OPT_FLAG_VIDEO_PARAM
328static const AVOption options[] = {
329 { "luma_spatial", "spatial luma strength", OFFSET(strength[LUMA_SPATIAL]), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
330 { "chroma_spatial", "spatial chroma strength", OFFSET(strength[CHROMA_SPATIAL]), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
331 { "luma_tmp", "temporal luma strength", OFFSET(strength[LUMA_TMP]), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
332 { "chroma_tmp", "temporal chroma strength", OFFSET(strength[CHROMA_TMP]), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
333 { NULL },
334};
335
336static const AVClass hqdn3d_class = {
337 .class_name = "hqdn3d",
338 .item_name = av_default_item_name,
339 .option = options,
340 .version = LIBAVUTIL_VERSION_INT,
341};
342
568c70e7
MR
343static const AVFilterPad avfilter_vf_hqdn3d_inputs[] = {
344 {
345 .name = "default",
346 .type = AVMEDIA_TYPE_VIDEO,
568c70e7 347 .config_props = config_input,
565e4993 348 .filter_frame = filter_frame,
568c70e7
MR
349 },
350 { NULL }
351};
352
353static const AVFilterPad avfilter_vf_hqdn3d_outputs[] = {
354 {
355 .name = "default",
356 .type = AVMEDIA_TYPE_VIDEO
357 },
358 { NULL }
359};
360
cd43ca04 361AVFilter ff_vf_hqdn3d = {
a4dc7aa5
BC
362 .name = "hqdn3d",
363 .description = NULL_IF_CONFIG_SMALL("Apply a High Quality 3D Denoiser."),
364
365 .priv_size = sizeof(HQDN3DContext),
8c747d46 366 .priv_class = &hqdn3d_class,
a4dc7aa5
BC
367 .init = init,
368 .uninit = uninit,
369 .query_formats = query_formats,
370
568c70e7
MR
371 .inputs = avfilter_vf_hqdn3d_inputs,
372
373 .outputs = avfilter_vf_hqdn3d_outputs,
a4dc7aa5 374};