2 * Copyright (c) 2012-2013 Oka Motofumi (chikuzen.mo at gmail dot com)
3 * Copyright (c) 2015 Paul B Mahol
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
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 GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/imgutils.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/pixdesc.h"
25 #include "libavutil/opt.h"
31 typedef struct ThreadData {
35 typedef struct NContext {
46 void (*filter)(uint8_t *dst, const uint8_t *p1, int width,
47 int threshold, const uint8_t *coordinates[], int coord);
50 static int query_formats(AVFilterContext *ctx)
52 static const enum AVPixelFormat pix_fmts[] = {
53 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
54 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
55 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
56 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
57 AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
58 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
59 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
60 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
61 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
62 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
63 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
64 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
65 AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
66 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
67 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
68 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
69 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
73 return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
76 static void erosion(uint8_t *dst, const uint8_t *p1, int width,
77 int threshold, const uint8_t *coordinates[], int coord)
81 for (x = 0; x < width; x++) {
83 int limit = FFMAX(min - threshold, 0);
85 for (i = 0; i < 8; i++) {
86 if (coord & (1 << i)) {
87 min = FFMIN(min, *(coordinates[i] + x));
89 min = FFMAX(min, limit);
96 static void erosion16(uint8_t *dstp, const uint8_t *p1, int width,
97 int threshold, const uint8_t *coordinates[], int coord)
99 uint16_t *dst = (uint16_t *)dstp;
102 for (x = 0; x < width; x++) {
103 int min = AV_RN16A(&p1[2 * x]);
104 int limit = FFMAX(min - threshold, 0);
106 for (i = 0; i < 8; i++) {
107 if (coord & (1 << i)) {
108 min = FFMIN(min, AV_RN16A(coordinates[i] + x * 2));
110 min = FFMAX(min, limit);
117 static void dilation(uint8_t *dst, const uint8_t *p1, int width,
118 int threshold, const uint8_t *coordinates[], int coord)
122 for (x = 0; x < width; x++) {
124 int limit = FFMIN(max + threshold, 255);
126 for (i = 0; i < 8; i++) {
127 if (coord & (1 << i)) {
128 max = FFMAX(max, *(coordinates[i] + x));
130 max = FFMIN(max, limit);
137 static void dilation16(uint8_t *dstp, const uint8_t *p1, int width,
138 int threshold, const uint8_t *coordinates[], int coord)
140 uint16_t *dst = (uint16_t *)dstp;
143 for (x = 0; x < width; x++) {
144 int max = AV_RN16A(&p1[x * 2]);
145 int limit = FFMIN(max + threshold, 255);
147 for (i = 0; i < 8; i++) {
148 if (coord & (1 << i)) {
149 max = FFMAX(max, AV_RN16A(coordinates[i] + x * 2));
151 max = FFMIN(max, limit);
158 static void deflate(uint8_t *dst, const uint8_t *p1, int width,
159 int threshold, const uint8_t *coordinates[], int coord)
163 for (x = 0; x < width; x++) {
165 int limit = FFMAX(p1[x] - threshold, 0);
167 for (i = 0; i < 8; sum += *(coordinates[i++] + x));
169 dst[x] = FFMAX(FFMIN(sum / 8, p1[x]), limit);
173 static void deflate16(uint8_t *dstp, const uint8_t *p1, int width,
174 int threshold, const uint8_t *coordinates[], int coord)
176 uint16_t *dst = (uint16_t *)dstp;
179 for (x = 0; x < width; x++) {
181 int limit = FFMAX(AV_RN16A(&p1[2 * x]) - threshold, 0);
183 for (i = 0; i < 8; sum += AV_RN16A(coordinates[i++] + x * 2));
185 dst[x] = FFMAX(FFMIN(sum / 8, p1[x]), limit);
189 static void inflate(uint8_t *dst, const uint8_t *p1, int width,
190 int threshold, const uint8_t *coordinates[], int coord)
194 for (x = 0; x < width; x++) {
196 int limit = FFMIN(p1[x] + threshold, 255);
198 for (i = 0; i < 8; sum += *(coordinates[i++] + x));
200 dst[x] = FFMIN(FFMAX(sum / 8, p1[x]), limit);
204 static void inflate16(uint8_t *dstp, const uint8_t *p1, int width,
205 int threshold, const uint8_t *coordinates[], int coord)
207 uint16_t *dst = (uint16_t *)dstp;
210 for (x = 0; x < width; x++) {
212 int limit = FFMIN(AV_RN16A(&p1[2 * x]) + threshold, 255);
214 for (i = 0; i < 8; sum += AV_RN16A(coordinates[i++] + x * 2));
216 dst[x] = FFMIN(FFMAX(sum / 8, p1[x]), limit);
220 static int config_input(AVFilterLink *inlink)
222 AVFilterContext *ctx = inlink->dst;
223 NContext *s = ctx->priv;
224 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
226 s->depth = desc->comp[0].depth;
227 s->bpc = (s->depth + 7) / 8;
229 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
230 s->planewidth[0] = s->planewidth[3] = inlink->w;
231 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
232 s->planeheight[0] = s->planeheight[3] = inlink->h;
234 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
236 if (!strcmp(ctx->filter->name, "erosion"))
237 s->filter = s->depth > 8 ? erosion16 : erosion;
238 else if (!strcmp(ctx->filter->name, "dilation"))
239 s->filter = s->depth > 8 ? dilation16 : dilation;
240 else if (!strcmp(ctx->filter->name, "deflate"))
241 s->filter = s->depth > 8 ? deflate16 : deflate;
242 else if (!strcmp(ctx->filter->name, "inflate"))
243 s->filter = s->depth > 8 ? inflate16 : inflate;
248 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
250 NContext *s = ctx->priv;
251 ThreadData *td = arg;
252 AVFrame *out = td->out;
253 AVFrame *in = td->in;
256 for (plane = 0; plane < s->nb_planes; plane++) {
257 const int bpc = s->bpc;
258 const int threshold = s->threshold[plane];
259 const int stride = in->linesize[plane];
260 const int dstride = out->linesize[plane];
261 const int height = s->planeheight[plane];
262 const int width = s->planewidth[plane];
263 const int slice_start = (height * jobnr) / nb_jobs;
264 const int slice_end = (height * (jobnr+1)) / nb_jobs;
265 const uint8_t *src = (const uint8_t *)in->data[plane] + slice_start * stride;
266 uint8_t *dst = out->data[plane] + slice_start * dstride;
269 av_image_copy_plane(dst, dstride, src, stride, width * bpc, slice_end - slice_start);
273 for (y = slice_start; y < slice_end; y++) {
274 const int nh = y > 0;
275 const int ph = y < height - 1;
276 const uint8_t *coordinates[] = { src - nh * stride, src + 1 * bpc - nh * stride, src + 2 * bpc - nh * stride,
278 src + ph * stride, src + 1 * bpc + ph * stride, src + 2 * bpc + ph * stride};
280 const uint8_t *coordinateslb[] = { src + 1 * bpc - nh * stride, src - nh * stride, src + 1 * bpc - nh * stride,
281 src + 1 * bpc, src + 1 * bpc,
282 src + 1 * bpc + ph * stride, src + ph * stride, src + 1 * bpc + ph * stride};
284 const uint8_t *coordinatesrb[] = { src + (width - 2) * bpc - nh * stride, src + (width - 1) * bpc - nh * stride, src + (width - 2) * bpc - nh * stride,
285 src + (width - 2) * bpc, src + (width - 2) * bpc,
286 src + (width - 2) * bpc + ph * stride, src + (width - 1) * bpc + ph * stride, src + (width - 2) * bpc + ph * stride};
288 s->filter(dst, src, 1, threshold, coordinateslb, s->coordinates);
289 s->filter(dst + 1 * bpc, src + 1 * bpc, width - 2, threshold, coordinates, s->coordinates);
290 s->filter(dst + (width - 1) * bpc, src + (width - 1) * bpc, 1, threshold, coordinatesrb, s->coordinates);
300 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
302 AVFilterContext *ctx = inlink->dst;
303 AVFilterLink *outlink = ctx->outputs[0];
304 NContext *s = ctx->priv;
308 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
311 return AVERROR(ENOMEM);
313 av_frame_copy_props(out, in);
317 ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(s->planeheight[1], ff_filter_get_nb_threads(ctx)));
320 return ff_filter_frame(outlink, out);
323 static const AVFilterPad neighbor_inputs[] = {
326 .type = AVMEDIA_TYPE_VIDEO,
327 .filter_frame = filter_frame,
328 .config_props = config_input,
333 static const AVFilterPad neighbor_outputs[] = {
336 .type = AVMEDIA_TYPE_VIDEO,
341 #define OFFSET(x) offsetof(NContext, x)
342 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
344 #define DEFINE_NEIGHBOR_FILTER(name_, description_) \
345 AVFILTER_DEFINE_CLASS(name_); \
347 AVFilter ff_vf_##name_ = { \
349 .description = NULL_IF_CONFIG_SMALL(description_), \
350 .priv_size = sizeof(NContext), \
351 .priv_class = &name_##_class, \
352 .query_formats = query_formats, \
353 .inputs = neighbor_inputs, \
354 .outputs = neighbor_outputs, \
355 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC| \
356 AVFILTER_FLAG_SLICE_THREADS, \
359 #if CONFIG_EROSION_FILTER
361 static const AVOption erosion_options[] = {
362 { "threshold0", "set threshold for 1st plane", OFFSET(threshold[0]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
363 { "threshold1", "set threshold for 2nd plane", OFFSET(threshold[1]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
364 { "threshold2", "set threshold for 3rd plane", OFFSET(threshold[2]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
365 { "threshold3", "set threshold for 4th plane", OFFSET(threshold[3]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
366 { "coordinates", "set coordinates", OFFSET(coordinates), AV_OPT_TYPE_INT, {.i64=255}, 0, 255, FLAGS },
370 DEFINE_NEIGHBOR_FILTER(erosion, "Apply erosion effect.");
372 #endif /* CONFIG_EROSION_FILTER */
374 #if CONFIG_DILATION_FILTER
376 static const AVOption dilation_options[] = {
377 { "threshold0", "set threshold for 1st plane", OFFSET(threshold[0]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
378 { "threshold1", "set threshold for 2nd plane", OFFSET(threshold[1]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
379 { "threshold2", "set threshold for 3rd plane", OFFSET(threshold[2]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
380 { "threshold3", "set threshold for 4th plane", OFFSET(threshold[3]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
381 { "coordinates", "set coordinates", OFFSET(coordinates), AV_OPT_TYPE_INT, {.i64=255}, 0, 255, FLAGS },
385 DEFINE_NEIGHBOR_FILTER(dilation, "Apply dilation effect.");
387 #endif /* CONFIG_DILATION_FILTER */
389 #if CONFIG_DEFLATE_FILTER
391 static const AVOption deflate_options[] = {
392 { "threshold0", "set threshold for 1st plane", OFFSET(threshold[0]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
393 { "threshold1", "set threshold for 2nd plane", OFFSET(threshold[1]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
394 { "threshold2", "set threshold for 3rd plane", OFFSET(threshold[2]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
395 { "threshold3", "set threshold for 4th plane", OFFSET(threshold[3]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
399 DEFINE_NEIGHBOR_FILTER(deflate, "Apply deflate effect.");
401 #endif /* CONFIG_DEFLATE_FILTER */
403 #if CONFIG_INFLATE_FILTER
405 static const AVOption inflate_options[] = {
406 { "threshold0", "set threshold for 1st plane", OFFSET(threshold[0]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
407 { "threshold1", "set threshold for 2nd plane", OFFSET(threshold[1]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
408 { "threshold2", "set threshold for 3rd plane", OFFSET(threshold[2]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
409 { "threshold3", "set threshold for 4th plane", OFFSET(threshold[3]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
413 DEFINE_NEIGHBOR_FILTER(inflate, "Apply inflate effect.");
415 #endif /* CONFIG_INFLATE_FILTER */