Line |
Branch |
Exec |
Source |
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 |
|
|
|
25 |
|
|
typedef struct CASContext { |
26 |
|
|
const AVClass *class; |
27 |
|
|
|
28 |
|
|
float strength; |
29 |
|
|
int planes; |
30 |
|
|
int nb_planes; |
31 |
|
|
|
32 |
|
|
int depth; |
33 |
|
|
int planeheight[4]; |
34 |
|
|
int planewidth[4]; |
35 |
|
|
|
36 |
|
|
AVFrame *in; |
37 |
|
|
|
38 |
|
|
int (*do_slice)(AVFilterContext *s, void *arg, |
39 |
|
|
int jobnr, int nb_jobs); |
40 |
|
|
} CASContext; |
41 |
|
|
|
42 |
|
✗ |
static inline float lerpf(float v0, float v1, float f) |
43 |
|
|
{ |
44 |
|
✗ |
return v0 + (v1 - v0) * f; |
45 |
|
|
} |
46 |
|
|
|
47 |
|
✗ |
static 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 = (s->planeheight[p] * jobnr) / nb_jobs; |
56 |
|
✗ |
const int slice_end = (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, |
68 |
|
|
w, slice_end - slice_start); |
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 |
|
|
|
113 |
|
✗ |
static 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 = (s->planeheight[p] * jobnr) / nb_jobs; |
123 |
|
✗ |
const int slice_end = (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 |
|
|
|
180 |
|
✗ |
static 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 |
|
|
} |
192 |
|
✗ |
av_frame_copy_props(out, in); |
193 |
|
|
|
194 |
|
✗ |
s->in = in; |
195 |
|
✗ |
ff_filter_execute(ctx, s->do_slice, out, NULL, |
196 |
|
✗ |
FFMIN(in->height, ff_filter_get_nb_threads(ctx))); |
197 |
|
✗ |
av_frame_free(&in); |
198 |
|
✗ |
s->in = NULL; |
199 |
|
|
|
200 |
|
✗ |
return ff_filter_frame(ctx->outputs[0], out); |
201 |
|
|
} |
202 |
|
|
|
203 |
|
|
static const enum AVPixelFormat pixel_fmts[] = { |
204 |
|
|
AV_PIX_FMT_GRAY8, |
205 |
|
|
AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, |
206 |
|
|
AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16, |
207 |
|
|
AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, |
208 |
|
|
AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, |
209 |
|
|
AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P, |
210 |
|
|
AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, |
211 |
|
|
AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P, |
212 |
|
|
AV_PIX_FMT_YUVJ411P, |
213 |
|
|
AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, |
214 |
|
|
AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, |
215 |
|
|
AV_PIX_FMT_YUV440P10, |
216 |
|
|
AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12, |
217 |
|
|
AV_PIX_FMT_YUV440P12, |
218 |
|
|
AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14, |
219 |
|
|
AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, |
220 |
|
|
AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, |
221 |
|
|
AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16, |
222 |
|
|
AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P, |
223 |
|
|
AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16, |
224 |
|
|
AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16, |
225 |
|
|
AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16, |
226 |
|
|
AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16, |
227 |
|
|
AV_PIX_FMT_NONE |
228 |
|
|
}; |
229 |
|
|
|
230 |
|
✗ |
static av_cold int config_input(AVFilterLink *inlink) |
231 |
|
|
{ |
232 |
|
✗ |
AVFilterContext *avctx = inlink->dst; |
233 |
|
✗ |
CASContext *s = avctx->priv; |
234 |
|
✗ |
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); |
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 |
|
|
|
248 |
|
|
static 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 |
|
|
|
260 |
|
|
static 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 |
|
|
|
266 |
|
|
AVFILTER_DEFINE_CLASS(cas); |
267 |
|
|
|
268 |
|
|
const AVFilter ff_vf_cas = { |
269 |
|
|
.name = "cas", |
270 |
|
|
.description = NULL_IF_CONFIG_SMALL("Contrast Adaptive Sharpen."), |
271 |
|
|
.priv_size = sizeof(CASContext), |
272 |
|
|
.priv_class = &cas_class, |
273 |
|
|
FILTER_INPUTS(cas_inputs), |
274 |
|
|
FILTER_OUTPUTS(ff_video_default_filterpad), |
275 |
|
|
FILTER_PIXFMTS_ARRAY(pixel_fmts), |
276 |
|
|
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS, |
277 |
|
|
.process_command = ff_filter_process_command, |
278 |
|
|
}; |
279 |
|
|
|