FFmpeg
Loading...
Searching...
No Matches
vf_owdenoise.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (c) 2013 Clément Bœsch <u pkh me>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22// The code written by Michael Niedermayer in 70024b6b47b9eacfe01e8f92349ca9bf1ccd7d5a:libavfilter/vf_owdenoise.c
23// can also be used under the LGPL due to:
24// <michaelni> durandal_1707, if you do all the "todo" points from vf_owdenoise.c that are in that file since 2013 then sure i would be more than happy to relicense my part of it to LGPL
25// <durandal_1707> michaelni: first relicense than work
26
27/**
28 * @todo try to change to int
29 * @todo try lifting based implementation
30 * @todo optimize optimize optimize
31 * @todo hard thresholding
32 * @todo use QP to decide filter strength
33 * @todo wavelet normalization / least squares optimal signal vs. noise thresholds
34 */
35
36#include "libavutil/imgutils.h"
37#include "libavutil/mem.h"
38#include "libavutil/opt.h"
40#include "libavutil/pixdesc.h"
41#include "avfilter.h"
42#include "filters.h"
43#include "video.h"
44#include "wavelet97.h"
45
46typedef struct OWDenoiseContext {
47 const AVClass *class;
50 int depth;
51 float *plane[16+1][4];
53 int hsub, vsub;
56
57#define OFFSET(x) offsetof(OWDenoiseContext, x)
58#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
59static const AVOption owdenoise_options[] = {
60 { "depth", "set depth", OFFSET(depth), AV_OPT_TYPE_INT, {.i64 = 8}, 8, 16, FLAGS },
61 { "luma_strength", "set luma strength", OFFSET(luma_strength), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0}, 0, 1000, FLAGS },
62 { "ls", "set luma strength", OFFSET(luma_strength), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0}, 0, 1000, FLAGS },
63 { "chroma_strength", "set chroma strength", OFFSET(chroma_strength), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0}, 0, 1000, FLAGS },
64 { "cs", "set chroma strength", OFFSET(chroma_strength), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0}, 0, 1000, FLAGS },
65 { NULL }
66};
67
69
70DECLARE_ALIGNED(8, static const uint8_t, dither)[8][8] = {
71 { 0, 48, 12, 60, 3, 51, 15, 63 },
72 { 32, 16, 44, 28, 35, 19, 47, 31 },
73 { 8, 56, 4, 52, 11, 59, 7, 55 },
74 { 40, 24, 36, 20, 43, 27, 39, 23 },
75 { 2, 50, 14, 62, 1, 49, 13, 61 },
76 { 34, 18, 46, 30, 33, 17, 45, 29 },
77 { 10, 58, 6, 54, 9, 57, 5, 53 },
78 { 42, 26, 38, 22, 41, 25, 37, 21 },
79};
80
82 uint8_t *dst, int dst_linesize,
83 const uint8_t *src, int src_linesize,
84 int width, int height, double strength)
85{
86 int x, y, i, j, depth = s->depth;
87
88 while (1<<depth > width || 1<<depth > height)
89 depth--;
90
91 if (s->pixel_depth <= 8) {
92 for (y = 0; y < height; y++)
93 for(x = 0; x < width; x++)
94 s->plane[0][0][y*s->linesize + x] = src[y*src_linesize + x];
95 } else {
96 const uint16_t *src16 = (const uint16_t *)src;
97
98 src_linesize /= 2;
99 for (y = 0; y < height; y++)
100 for(x = 0; x < width; x++)
101 s->plane[0][0][y*s->linesize + x] = src16[y*src_linesize + x];
102 }
103
104 for (i = 0; i < depth; i++)
105 ff_wavelet97_decompose(s->plane[i + 1], s->plane[i][0], s->plane[0] + 1,
106 ff_wavelet97_coeff, s->linesize, 1<<i, width, height);
107
108 for (i = 0; i < depth; i++) {
109 for (j = 1; j < 4; j++) {
110 for (y = 0; y < height; y++) {
111 for (x = 0; x < width; x++) {
112 double v = s->plane[i + 1][j][y*s->linesize + x];
113 if (v > strength) v -= strength;
114 else if (v < -strength) v += strength;
115 else v = 0;
116 s->plane[i + 1][j][x + y*s->linesize] = v;
117 }
118 }
119 }
120 }
121 for (i = depth-1; i >= 0; i--)
122 ff_wavelet97_compose(s->plane[i][0], s->plane[i + 1], s->plane[0] + 1,
123 s->linesize, 1<<i, width, height);
124
125 if (s->pixel_depth <= 8) {
126 for (y = 0; y < height; y++) {
127 for (x = 0; x < width; x++) {
128 i = s->plane[0][0][y*s->linesize + x] + dither[x&7][y&7]*(1.0/64) + 1.0/128; // yes the rounding is insane but optimal :)
129 if ((unsigned)i > 255U) i = ~(i >> 31);
130 dst[y*dst_linesize + x] = i;
131 }
132 }
133 } else {
134 uint16_t *dst16 = (uint16_t *)dst;
135
136 dst_linesize /= 2;
137 for (y = 0; y < height; y++) {
138 for (x = 0; x < width; x++) {
139 i = s->plane[0][0][y*s->linesize + x];
140 dst16[y*dst_linesize + x] = i;
141 }
142 }
143 }
144}
145
146static int filter_frame(AVFilterLink *inlink, AVFrame *in)
147{
148 AVFilterContext *ctx = inlink->dst;
149 OWDenoiseContext *s = ctx->priv;
150 AVFilterLink *outlink = ctx->outputs[0];
151 AVFrame *out;
152 const int cw = AV_CEIL_RSHIFT(inlink->w, s->hsub);
153 const int ch = AV_CEIL_RSHIFT(inlink->h, s->vsub);
154 const int bytes_per_sample = (s->pixel_depth + 7) / 8;
155
156 if (av_frame_is_writable(in)) {
157 out = in;
158
159 if (s->luma_strength > 0)
160 filter(s, out->data[0], out->linesize[0], in->data[0], in->linesize[0], inlink->w, inlink->h, s->luma_strength);
161 if (s->chroma_strength > 0) {
162 filter(s, out->data[1], out->linesize[1], in->data[1], in->linesize[1], cw, ch, s->chroma_strength);
163 filter(s, out->data[2], out->linesize[2], in->data[2], in->linesize[2], cw, ch, s->chroma_strength);
164 }
165 } else {
166 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
167 if (!out) {
168 av_frame_free(&in);
169 return AVERROR(ENOMEM);
170 }
172
173 if (s->luma_strength > 0) {
174 filter(s, out->data[0], out->linesize[0], in->data[0], in->linesize[0], inlink->w, inlink->h, s->luma_strength);
175 } else {
176 av_image_copy_plane(out->data[0], out->linesize[0], in ->data[0], in ->linesize[0], inlink->w * bytes_per_sample, inlink->h);
177 }
178 if (s->chroma_strength > 0) {
179 filter(s, out->data[1], out->linesize[1], in->data[1], in->linesize[1], cw, ch, s->chroma_strength);
180 filter(s, out->data[2], out->linesize[2], in->data[2], in->linesize[2], cw, ch, s->chroma_strength);
181 } else {
182 av_image_copy_plane(out->data[1], out->linesize[1], in ->data[1], in ->linesize[1], cw * bytes_per_sample, ch);
183 av_image_copy_plane(out->data[2], out->linesize[2], in ->data[2], in ->linesize[2], cw * bytes_per_sample, ch);
184 }
185
186 if (in->data[3])
187 av_image_copy_plane(out->data[3], out->linesize[3],
188 in ->data[3], in ->linesize[3],
189 inlink->w, inlink->h);
190 av_frame_free(&in);
191 }
192
193 return ff_filter_frame(outlink, out);
194}
195
211
212static int config_input(AVFilterLink *inlink)
213{
214 int i, j;
215 OWDenoiseContext *s = inlink->dst->priv;
217 const int h = FFALIGN(inlink->h, 16);
218
219 s->hsub = desc->log2_chroma_w;
220 s->vsub = desc->log2_chroma_h;
221 s->pixel_depth = desc->comp[0].depth;
222
223 s->linesize = FFALIGN(inlink->w, 16);
224 for (j = 0; j < 4; j++) {
225 for (i = 0; i <= s->depth; i++) {
226 s->plane[i][j] = av_malloc_array(s->linesize, h * sizeof(s->plane[0][0][0]));
227 if (!s->plane[i][j])
228 return AVERROR(ENOMEM);
229 }
230 }
231 return 0;
232}
233
235{
236 int i, j;
237 OWDenoiseContext *s = ctx->priv;
238
239 for (j = 0; j < 4; j++)
240 for (i = 0; i <= s->depth; i++)
241 av_freep(&s->plane[i][j]);
242}
243
245 {
246 .name = "default",
247 .type = AVMEDIA_TYPE_VIDEO,
248 .filter_frame = filter_frame,
249 .config_props = config_input,
250 },
251};
252
254 .p.name = "owdenoise",
255 .p.description = NULL_IF_CONFIG_SMALL("Denoise using wavelets."),
256 .p.priv_class = &owdenoise_class,
258 .priv_size = sizeof(OWDenoiseContext),
259 .uninit = uninit,
263};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
static int config_input(AVFilterLink *inlink)
const FFFilter ff_vf_owdenoise
static FILE * out
static AVFormatContext * ctx
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1079
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:596
#define AV_CEIL_RSHIFT(a, b)
Definition common.h:60
#define NULL
Definition coverity.c:32
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition opt.h:266
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition avfilter.h:202
#define AVERROR(e)
Definition error.h:45
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition frame.c:538
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:65
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:602
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
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
misc image utilities
static av_cold void uninit(AVBitStreamFilterContext *ctx)
#define FILTER_INPUTS(array)
Definition filters.h:279
#define FILTER_OUTPUTS(array)
Definition filters.h:280
#define FILTER_PIXFMTS_ARRAY(array)
Definition filters.h:259
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:493
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
static enum AVPixelFormat pix_fmts[]
Definition libkvazaar.c:296
const char * desc
Definition libsvtav1.c:83
#define FFALIGN(x, a)
Definition macros.h:78
Memory handling functions.
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
AVOptions.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3484
#define AV_PIX_FMT_YUV420P16
Definition pixfmt.h:559
#define AV_PIX_FMT_YUV444P12
Definition pixfmt.h:555
#define AV_PIX_FMT_YUV444P9
Definition pixfmt.h:547
#define AV_PIX_FMT_YUV420P10
Definition pixfmt.h:548
#define AV_PIX_FMT_YUV440P12
Definition pixfmt.h:554
#define AV_PIX_FMT_YUV422P9
Definition pixfmt.h:546
#define AV_PIX_FMT_YUV420P12
Definition pixfmt.h:552
#define AV_PIX_FMT_YUV422P12
Definition pixfmt.h:553
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:549
#define AV_PIX_FMT_YUV420P9
Definition pixfmt.h:545
#define AV_PIX_FMT_YUV420P14
Definition pixfmt.h:556
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition pixfmt.h:106
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition pixfmt.h:108
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition pixfmt.h:79
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition pixfmt.h:80
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition pixfmt.h:174
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition pixfmt.h:173
#define AV_PIX_FMT_YUV422P14
Definition pixfmt.h:557
#define AV_PIX_FMT_YUV422P16
Definition pixfmt.h:560
#define AV_PIX_FMT_YUV440P10
Definition pixfmt.h:550
#define AV_PIX_FMT_YUV444P14
Definition pixfmt.h:558
#define AV_PIX_FMT_YUV444P16
Definition pixfmt.h:561
#define AV_PIX_FMT_YUV444P10
Definition pixfmt.h:551
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:279
void * priv
private data for use by the filter
Definition avfilter.h:294
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:500
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:524
AVOption.
Definition opt.h:428
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
float * plane[16+1][4]
#define av_malloc_array(a, b)
#define av_freep(p)
void(* filter)(uint8_t *src, ptrdiff_t stride, int qscale)
Definition h263dsp.c:29
#define src
Definition vp8dsp.c:248
#define height
Definition dsp.h:99
#define width
Definition dsp.h:99
static const uint16_t dither[8][8]
Definition vf_gradfun.c:46
static const AVFilterPad owdenoise_inputs[]
static int config_input(AVFilterLink *inlink)
static const AVOption owdenoise_options[]
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
static av_cold void uninit(AVFilterContext *ctx)
#define OFFSET(x)
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition video.c:37
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:120
const double ff_wavelet97_coeff[2][5]
Undecimated 9/7 wavelet transform.
Definition wavelet97.c:26
void ff_wavelet97_decompose(float *dst[4], const float *src, float *temp[2], const double coeff[2][5], int linesize, int step, int w, int h)
One level of undecimated 2D analysis, filters are mirrored at the plane borders.
Definition wavelet97.c:164
void ff_wavelet97_compose(float *dst, float *const src[4], float *temp[2], int linesize, int step, int w, int h)
One level of 2D synthesis, the inverse of ff_wavelet97_decompose() with ff_wavelet97_coeff.
Definition wavelet97.c:173