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/pixdesc.h"
24 #include "libavutil/opt.h"
30 typedef struct NContext {
39 void (*filter)(uint8_t *dst, const uint8_t *p1, int width,
40 int threshold, const uint8_t *coordinates[], int coord);
43 static int query_formats(AVFilterContext *ctx)
45 static const enum AVPixelFormat pix_fmts[] = {
46 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
47 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ422P,AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P,
48 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
49 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
52 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
56 static av_cold void uninit(AVFilterContext *ctx)
58 NContext *s = ctx->priv;
63 static inline void line_copy8(uint8_t *line, const uint8_t *srcp, int width, int mergin)
67 memcpy(line, srcp, width);
69 for (i = mergin; i > 0; i--) {
71 line[width - 1 + i] = line[width - 1 - i];
75 static void erosion(uint8_t *dst, const uint8_t *p1, int width,
76 int threshold, const uint8_t *coordinates[], int coord)
80 for (x = 0; x < width; x++) {
82 int limit = FFMAX(min - threshold, 0);
84 for (i = 0; i < 8; i++) {
85 if (coord & (1 << i)) {
86 min = FFMIN(min, *(coordinates[i] + x));
88 min = FFMAX(min, limit);
95 static void dilation(uint8_t *dst, const uint8_t *p1, int width,
96 int threshold, const uint8_t *coordinates[], int coord)
100 for (x = 0; x < width; x++) {
102 int limit = FFMIN(max + threshold, 255);
104 for (i = 0; i < 8; i++) {
105 if (coord & (1 << i)) {
106 max = FFMAX(max, *(coordinates[i] + x));
108 max = FFMIN(max, limit);
115 static void deflate(uint8_t *dst, const uint8_t *p1, int width,
116 int threshold, const uint8_t *coordinates[], int coord)
120 for (x = 0; x < width; x++) {
122 int limit = FFMAX(p1[x] - threshold, 0);
124 for (i = 0; i < 8; sum += *(coordinates[i++] + x));
126 dst[x] = FFMAX(FFMIN(sum / 8, p1[x]), limit);
130 static void inflate(uint8_t *dst, const uint8_t *p1, int width,
131 int threshold, const uint8_t *coordinates[], int coord)
135 for (x = 0; x < width; x++) {
137 int limit = FFMIN(p1[x] + threshold, 255);
139 for (i = 0; i < 8; sum += *(coordinates[i++] + x));
141 dst[x] = FFMIN(FFMAX(sum / 8, p1[x]), limit);
145 static int config_input(AVFilterLink *inlink)
147 AVFilterContext *ctx = inlink->dst;
148 NContext *s = ctx->priv;
149 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
152 if ((ret = av_image_fill_linesizes(s->planewidth, inlink->format, inlink->w)) < 0)
155 s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
156 s->planeheight[0] = s->planeheight[3] = inlink->h;
158 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
159 s->buffer = av_malloc(3 * (s->planewidth[0] + 32));
161 return AVERROR(ENOMEM);
163 if (!strcmp(ctx->filter->name, "erosion"))
165 else if (!strcmp(ctx->filter->name, "dilation"))
166 s->filter = dilation;
167 else if (!strcmp(ctx->filter->name, "deflate"))
169 else if (!strcmp(ctx->filter->name, "inflate"))
175 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
177 AVFilterContext *ctx = inlink->dst;
178 AVFilterLink *outlink = ctx->outputs[0];
179 NContext *s = ctx->priv;
183 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
186 return AVERROR(ENOMEM);
188 av_frame_copy_props(out, in);
190 for (plane = 0; plane < s->nb_planes; plane++) {
191 const int threshold = s->threshold[plane];
194 const uint8_t *src = in->data[plane];
195 uint8_t *dst = out->data[plane];
196 int stride = in->linesize[plane];
197 int height = s->planeheight[plane];
198 int width = s->planewidth[plane];
199 uint8_t *p0 = s->buffer + 16;
200 uint8_t *p1 = p0 + s->planewidth[0];
201 uint8_t *p2 = p1 + s->planewidth[0];
202 uint8_t *orig = p0, *end = p2;
204 line_copy8(p0, src + stride, width, 1);
205 line_copy8(p1, src, width, 1);
207 for (y = 0; y < height; y++) {
208 const uint8_t *coordinates[] = { p0 - 1, p0, p0 + 1,
211 src += stride * (y < height - 1 ? 1 : -1);
212 line_copy8(p2, src, width, 1);
214 s->filter(dst, p1, width, threshold, coordinates, s->coordinates);
218 p2 = (p2 == end) ? orig: p2 + s->planewidth[0];
219 dst += out->linesize[plane];
222 av_image_copy_plane(out->data[plane], out->linesize[plane],
223 in->data[plane], in->linesize[plane],
224 s->planewidth[plane], s->planeheight[plane]);
229 return ff_filter_frame(outlink, out);
232 static const AVFilterPad neighbor_inputs[] = {
235 .type = AVMEDIA_TYPE_VIDEO,
236 .filter_frame = filter_frame,
237 .config_props = config_input,
242 static const AVFilterPad neighbor_outputs[] = {
245 .type = AVMEDIA_TYPE_VIDEO,
250 #define OFFSET(x) offsetof(NContext, x)
251 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
253 #define DEFINE_NEIGHBOR_FILTER(name_, description_) \
254 AVFILTER_DEFINE_CLASS(name_); \
256 AVFilter ff_vf_##name_ = { \
258 .description = NULL_IF_CONFIG_SMALL(description_), \
259 .priv_size = sizeof(NContext), \
260 .priv_class = &name_##_class, \
262 .query_formats = query_formats, \
263 .inputs = neighbor_inputs, \
264 .outputs = neighbor_outputs, \
265 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, \
268 #if CONFIG_EROSION_FILTER
270 static const AVOption erosion_options[] = {
271 { "threshold0", "set threshold for 1st plane", OFFSET(threshold[0]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
272 { "threshold1", "set threshold for 2nd plane", OFFSET(threshold[1]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
273 { "threshold2", "set threshold for 3rd plane", OFFSET(threshold[2]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
274 { "threshold3", "set threshold for 4th plane", OFFSET(threshold[3]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
275 { "coordinates", "set coordinates", OFFSET(coordinates), AV_OPT_TYPE_INT, {.i64=255}, 0, 255, FLAGS },
279 DEFINE_NEIGHBOR_FILTER(erosion, "Apply erosion effect");
281 #endif /* CONFIG_EROSION_FILTER */
283 #if CONFIG_DILATION_FILTER
285 static const AVOption dilation_options[] = {
286 { "threshold0", "set threshold for 1st plane", OFFSET(threshold[0]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
287 { "threshold1", "set threshold for 2nd plane", OFFSET(threshold[1]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
288 { "threshold2", "set threshold for 3rd plane", OFFSET(threshold[2]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
289 { "threshold3", "set threshold for 4th plane", OFFSET(threshold[3]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
290 { "coordinates", "set coordinates", OFFSET(coordinates), AV_OPT_TYPE_INT, {.i64=255}, 0, 255, FLAGS },
294 DEFINE_NEIGHBOR_FILTER(dilation, "Apply dilation effect");
296 #endif /* CONFIG_DILATION_FILTER */
298 #if CONFIG_DEFLATE_FILTER
300 static const AVOption deflate_options[] = {
301 { "threshold0", "set threshold for 1st plane", OFFSET(threshold[0]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
302 { "threshold1", "set threshold for 2nd plane", OFFSET(threshold[1]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
303 { "threshold2", "set threshold for 3rd plane", OFFSET(threshold[2]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
304 { "threshold3", "set threshold for 4th plane", OFFSET(threshold[3]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
308 DEFINE_NEIGHBOR_FILTER(deflate, "Apply deflate effect");
310 #endif /* CONFIG_DEFLATE_FILTER */
312 #if CONFIG_INFLATE_FILTER
314 static const AVOption inflate_options[] = {
315 { "threshold0", "set threshold for 1st plane", OFFSET(threshold[0]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
316 { "threshold1", "set threshold for 2nd plane", OFFSET(threshold[1]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
317 { "threshold2", "set threshold for 3rd plane", OFFSET(threshold[2]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
318 { "threshold3", "set threshold for 4th plane", OFFSET(threshold[3]), AV_OPT_TYPE_INT, {.i64=65535}, 0, 65535, FLAGS },
322 DEFINE_NEIGHBOR_FILTER(inflate, "Apply inflate effect");
324 #endif /* CONFIG_INFLATE_FILTER */