FFmpeg
Loading...
Searching...
No Matches
vf_cas.c
Go to the documentation of this file.
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include "libavutil/opt.h"
20#include "libavutil/imgutils.h"
21#include "avfilter.h"
22#include "filters.h"
23#include "video.h"
24
25typedef struct CASContext {
26 const AVClass *class;
27
28 float strength;
29 int planes;
31
32 int depth;
34 int planewidth[4];
35
37
38 int (*do_slice)(AVFilterContext *s, void *arg,
39 int jobnr, int nb_jobs);
41
42static inline float lerpf(float v0, float v1, float f)
43{
44 return v0 + (v1 - v0) * f;
45}
46
47static int cas_slice8(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
48{
49 CASContext *s = avctx->priv;
50 const float strength = -lerpf(16.f, 4.01f, s->strength);
51 AVFrame *out = arg;
52 AVFrame *in = s->in;
53
54 for (int p = 0; p < s->nb_planes; p++) {
55 const int slice_start = ff_slice_pos(s->planeheight[p], jobnr, nb_jobs);
56 const int slice_end = ff_slice_pos(s->planeheight[p], jobnr + 1, nb_jobs);
57 const int linesize = out->linesize[p];
58 const int in_linesize = in->linesize[p];
59 const int w = s->planewidth[p];
60 const int w1 = w - 1;
61 const int h = s->planeheight[p];
62 const int h1 = h - 1;
63 uint8_t *dst = out->data[p] + slice_start * linesize;
64 const uint8_t *src = in->data[p];
65
66 if (!((1 << p) & s->planes)) {
67 av_image_copy_plane(dst, linesize, src + slice_start * in_linesize, in_linesize,
69 continue;
70 }
71
72 for (int y = slice_start; y < slice_end; y++) {
73 const int y0 = FFMAX(y - 1, 0);
74 const int y1 = FFMIN(y + 1, h1);
75 for (int x = 0; x < w; x++) {
76 const int x0 = FFMAX(x - 1, 0);
77 const int x1 = FFMIN(x + 1, w1);
78 int a = src[y0 * in_linesize + x0];
79 int b = src[y0 * in_linesize + x];
80 int c = src[y0 * in_linesize + x1];
81 int d = src[y * in_linesize + x0];
82 int e = src[y * in_linesize + x];
83 int f = src[y * in_linesize + x1];
84 int g = src[y1 * in_linesize + x0];
85 int h = src[y1 * in_linesize + x];
86 int i = src[y1 * in_linesize + x1];
87 int mn, mn2, mx, mx2;
88 float amp, weight;
89
90 mn = FFMIN3(FFMIN3( d, e, f), b, h);
91 mn2 = FFMIN3(FFMIN3(mn, a, c), g, i);
92
93 mn = mn + mn2;
94
95 mx = FFMAX3(FFMAX3( d, e, f), b, h);
96 mx2 = FFMAX3(FFMAX3(mx, a, c), g, i);
97
98 mx = mx + mx2;
99
100 amp = sqrtf(av_clipf(FFMIN(mn, 511 - mx) / (float)mx, 0.f, 1.f));
101
102 weight = amp / strength;
103
104 dst[x] = av_clip_uint8(((b + d + f + h) * weight + e) / (1.f + 4.f * weight));
105 }
106 dst += linesize;
107 }
108 }
109
110 return 0;
111}
112
113static int cas_slice16(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
114{
115 CASContext *s = avctx->priv;
116 const float strength = -lerpf(16.f, 4.01f, s->strength);
117 const int max = 2 * (1 << s->depth) - 1;
118 AVFrame *out = arg;
119 AVFrame *in = s->in;
120
121 for (int p = 0; p < s->nb_planes; p++) {
122 const int slice_start = ff_slice_pos(s->planeheight[p], jobnr, nb_jobs);
123 const int slice_end = ff_slice_pos(s->planeheight[p], jobnr + 1, nb_jobs);
124 const int linesize = out->linesize[p] / 2;
125 const int in_linesize = in->linesize[p] / 2;
126 const int w = s->planewidth[p];
127 const int w1 = w - 1;
128 const int h = s->planeheight[p];
129 const int h1 = h - 1;
130 uint16_t *dst = ((uint16_t *)out->data[p]) + slice_start * linesize;
131 const uint16_t *src = (const uint16_t *)in->data[p];
132
133 if (!((1 << p) & s->planes)) {
134 av_image_copy_plane((uint8_t *)dst, linesize * 2, (uint8_t *)(src + slice_start * in_linesize),
135 in_linesize * 2, w * 2, slice_end - slice_start);
136 continue;
137 }
138
139 for (int y = slice_start; y < slice_end; y++) {
140 const int y0 = FFMAX(y - 1, 0);
141 const int y1 = FFMIN(y + 1, h1);
142 for (int x = 0; x < w; x++) {
143 const int x0 = FFMAX(x - 1, 0);
144 const int x1 = FFMIN(x + 1, w1);
145 int a = src[y0 * in_linesize + x0];
146 int b = src[y0 * in_linesize + x];
147 int c = src[y0 * in_linesize + x1];
148 int d = src[y * in_linesize + x0];
149 int e = src[y * in_linesize + x];
150 int f = src[y * in_linesize + x1];
151 int g = src[y1 * in_linesize + x0];
152 int h = src[y1 * in_linesize + x];
153 int i = src[y1 * in_linesize + x1];
154 int mn, mn2, mx, mx2;
155 float amp, weight;
156
157 mn = FFMIN3(FFMIN3( d, e, f), b, h);
158 mn2 = FFMIN3(FFMIN3(mn, a, c), g, i);
159
160 mn = mn + mn2;
161
162 mx = FFMAX3(FFMAX3( d, e, f), b, h);
163 mx2 = FFMAX3(FFMAX3(mx, a, c), g, i);
164
165 mx = mx + mx2;
166
167 amp = sqrtf(av_clipf(FFMIN(mn, max - mx) / (float)mx, 0.f, 1.f));
168
169 weight = amp / strength;
170
171 dst[x] = av_clip_uintp2_c(((b + d + f + h) * weight + e) / (1.f + 4.f * weight), s->depth);
172 }
173 dst += linesize;
174 }
175 }
176
177 return 0;
178}
179
180static int filter_frame(AVFilterLink *inlink, AVFrame *in)
181{
182 AVFilterContext *ctx = inlink->dst;
183 AVFilterLink *outlink = ctx->outputs[0];
184 CASContext *s = ctx->priv;
185 AVFrame *out;
186
187 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
188 if (!out) {
189 av_frame_free(&in);
190 return AVERROR(ENOMEM);
191 }
193
194 s->in = in;
195 ff_filter_execute(ctx, s->do_slice, out, NULL,
197 av_frame_free(&in);
198 s->in = NULL;
199
200 return ff_filter_frame(ctx->outputs[0], out);
201}
202
229
231{
232 AVFilterContext *avctx = inlink->dst;
233 CASContext *s = avctx->priv;
235
236 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
237 s->planeheight[0] = s->planeheight[3] = inlink->h;
238 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
239 s->planewidth[0] = s->planewidth[3] = inlink->w;
240
241 s->depth = desc->comp[0].depth;
242 s->nb_planes = desc->nb_components;
243 s->do_slice = s->depth <= 8 ? cas_slice8 : cas_slice16;
244
245 return 0;
246}
247
248static const AVFilterPad cas_inputs[] = {
249 {
250 .name = "default",
251 .type = AVMEDIA_TYPE_VIDEO,
252 .filter_frame = filter_frame,
253 .config_props = config_input,
254 },
255};
256
257#define OFFSET(x) offsetof(CASContext, x)
258#define VF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
259
260static const AVOption cas_options[] = {
261 { "strength", "set the sharpening strength", OFFSET(strength), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, VF },
262 { "planes", "set what planes to filter", OFFSET(planes), AV_OPT_TYPE_FLAGS, {.i64=7}, 0, 15, VF },
263 { NULL }
264};
265
267
269 .p.name = "cas",
270 .p.description = NULL_IF_CONFIG_SMALL("Contrast Adaptive Sharpen."),
271 .p.priv_class = &cas_class,
273 .priv_size = sizeof(CASContext),
277 .process_command = ff_filter_process_command,
278};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t mx
Definition dsp.h:57
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static int config_input(AVFilterLink *inlink)
const FFFilter ff_vf_cas
Definition vf_cas.c:268
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
Definition avfilter.c:906
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition avfilter.c:1696
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition avfilter.c:846
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
#define AV_CEIL_RSHIFT(a, b)
Definition common.h:60
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:280
#define av_clip_uint8
Definition common.h:106
#define av_clipf
Definition common.h:145
#define NULL
Definition coverity.c:32
static __device__ float sqrtf(float a)
#define max(a, b)
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
@ AV_OPT_TYPE_FLAGS
Underlying C type is unsigned int.
Definition opt.h:254
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition opt.h:270
#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:196
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition avfilter.h:166
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
@ 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
int a
misc image utilities
#define b
Definition input.c:43
const char * arg
Definition jacosubdec.c:65
#define VF
Definition af_afir.c:731
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
static int ff_slice_pos(int total, int jobnr, int nb_jobs)
Compute the boundary index for a slice when work of size total is split into nb_jobs slices.
Definition filters.h:763
#define FILTER_PIXFMTS_ARRAY(array)
Definition filters.h:244
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#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:88
const char * desc
Definition libsvtav1.c:83
static const struct @257111027162314367033347246032313251342043035002 planes[]
uint8_t w
Definition llvidencdsp.c:39
#define FFMAX3(a, b, c)
Definition macros.h:48
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
#define FFMIN3(a, b, c)
Definition macros.h:50
static int slice_end(AVCodecContext *avctx, AVFrame *pict, int *got_output)
Handle slice ends.
Definition mpeg12dec.c:1697
AVOptions.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_GBRAP12
Definition pixfmt.h:569
#define AV_PIX_FMT_YUV420P16
Definition pixfmt.h:556
#define AV_PIX_FMT_YUV444P12
Definition pixfmt.h:552
#define AV_PIX_FMT_YUV444P9
Definition pixfmt.h:544
#define AV_PIX_FMT_YUV420P10
Definition pixfmt.h:545
#define AV_PIX_FMT_YUV440P12
Definition pixfmt.h:551
#define AV_PIX_FMT_GRAY9
Definition pixfmt.h:524
#define AV_PIX_FMT_GBRAP16
Definition pixfmt.h:571
#define AV_PIX_FMT_GBRP9
Definition pixfmt.h:563
#define AV_PIX_FMT_YUV422P9
Definition pixfmt.h:543
#define AV_PIX_FMT_YUVA444P10
Definition pixfmt.h:598
#define AV_PIX_FMT_YUVA420P16
Definition pixfmt.h:601
#define AV_PIX_FMT_YUV420P12
Definition pixfmt.h:549
#define AV_PIX_FMT_YUVA420P10
Definition pixfmt.h:596
#define AV_PIX_FMT_YUVA422P9
Definition pixfmt.h:594
#define AV_PIX_FMT_YUV422P12
Definition pixfmt.h:550
#define AV_PIX_FMT_GBRP10
Definition pixfmt.h:564
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:546
#define AV_PIX_FMT_GRAY12
Definition pixfmt.h:526
#define AV_PIX_FMT_GBRP12
Definition pixfmt.h:565
#define AV_PIX_FMT_YUV420P9
Definition pixfmt.h:542
#define AV_PIX_FMT_YUVA420P9
Definition pixfmt.h:593
#define AV_PIX_FMT_YUVA422P10
Definition pixfmt.h:597
#define AV_PIX_FMT_YUV420P14
Definition pixfmt.h:553
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_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ 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_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
Definition pixfmt.h:107
@ 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_YUVJ411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
Definition pixfmt.h:283
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition pixfmt.h:212
@ 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:86
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition pixfmt.h:173
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition pixfmt.h:165
@ 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:87
@ 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:85
#define AV_PIX_FMT_YUVA422P12
Definition pixfmt.h:599
#define AV_PIX_FMT_YUV422P14
Definition pixfmt.h:554
#define AV_PIX_FMT_GRAY10
Definition pixfmt.h:525
#define AV_PIX_FMT_GRAY14
Definition pixfmt.h:527
#define AV_PIX_FMT_YUV422P16
Definition pixfmt.h:557
#define AV_PIX_FMT_YUV440P10
Definition pixfmt.h:547
#define AV_PIX_FMT_GRAY16
Definition pixfmt.h:528
#define AV_PIX_FMT_GBRAP10
Definition pixfmt.h:568
#define AV_PIX_FMT_YUVA444P16
Definition pixfmt.h:603
#define AV_PIX_FMT_YUVA422P16
Definition pixfmt.h:602
#define AV_PIX_FMT_GBRP16
Definition pixfmt.h:567
#define AV_PIX_FMT_YUV444P14
Definition pixfmt.h:555
#define AV_PIX_FMT_YUVA444P9
Definition pixfmt.h:595
#define AV_PIX_FMT_GBRP14
Definition pixfmt.h:566
#define AV_PIX_FMT_YUVA444P12
Definition pixfmt.h:600
#define AV_PIX_FMT_YUV444P16
Definition pixfmt.h:558
#define AV_PIX_FMT_YUV444P10
Definition pixfmt.h:548
const h264_weight_func weight
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
void * priv
private data for use by the filter
Definition avfilter.h:288
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:472
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int height
Definition frame.h:544
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:517
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 strength
Definition vf_cas.c:28
int planewidth[4]
Definition vf_cas.c:34
AVFrame * in
Definition vf_cas.c:36
int planes
Definition vf_cas.c:29
int depth
Definition vf_cas.c:32
int nb_planes
Definition vf_cas.c:30
int planeheight[4]
Definition vf_cas.c:33
int(* do_slice)(AVFilterContext *s, void *arg, int jobnr, int nb_jobs)
Definition vf_cas.c:38
#define src
Definition vp8dsp.c:248
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
static enum AVPixelFormat pixel_fmts[]
Definition vf_amplify.c:52
static const AVOption cas_options[]
Definition vf_cas.c:260
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition vf_cas.c:180
static int cas_slice8(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
Definition vf_cas.c:47
static av_cold int config_input(AVFilterLink *inlink)
Definition vf_cas.c:230
static float lerpf(float v0, float v1, float f)
Definition vf_cas.c:42
#define OFFSET(x)
Definition vf_cas.c:257
static int cas_slice16(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
Definition vf_cas.c:113
static const AVFilterPad cas_inputs[]
Definition vf_cas.c:248
const char * g
Definition vf_curves.c:128
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:89
static double c[64]
static int slice_start(SliceContext *sc, VVCContext *s, VVCFrameContext *fc, const CodedBitstreamUnit *unit, const int is_first_slice)
Definition dec.c:844