]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_neighbor.c
Merge commit 'b93026777aada7742583d8c5ab079e9f4dfe9a5d'
[ffmpeg] / libavfilter / vf_neighbor.c
1 /*
2  * Copyright (c) 2012-2013 Oka Motofumi (chikuzen.mo at gmail dot com)
3  * Copyright (c) 2015 Paul B Mahol
4  *
5  * This file is part of FFmpeg.
6  *
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.
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 GNU
15  * Lesser General Public License for more details.
16  *
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
20  */
21
22 #include "libavutil/imgutils.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/pixdesc.h"
25 #include "libavutil/opt.h"
26 #include "avfilter.h"
27 #include "formats.h"
28 #include "internal.h"
29 #include "video.h"
30
31 typedef struct ThreadData {
32     AVFrame *in, *out;
33 } ThreadData;
34
35 typedef struct NContext {
36     const AVClass *class;
37     int planeheight[4];
38     int planewidth[4];
39     int nb_planes;
40     int threshold[4];
41     int coordinates;
42
43     int depth;
44     int bpc;
45
46     void (*filter)(uint8_t *dst, const uint8_t *p1, int width,
47                    int threshold, const uint8_t *coordinates[], int coord);
48 } NContext;
49
50 static int query_formats(AVFilterContext *ctx)
51 {
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_GRAY16,
70         AV_PIX_FMT_NONE
71     };
72
73     return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
74 }
75
76 static void erosion(uint8_t *dst, const uint8_t *p1, int width,
77                     int threshold, const uint8_t *coordinates[], int coord)
78 {
79     int x, i;
80
81     for (x = 0; x < width; x++) {
82         int min = p1[x];
83         int limit = FFMAX(min - threshold, 0);
84
85         for (i = 0; i < 8; i++) {
86             if (coord & (1 << i)) {
87                 min = FFMIN(min, *(coordinates[i] + x));
88             }
89             min = FFMAX(min, limit);
90         }
91
92         dst[x] = min;
93     }
94 }
95
96 static void erosion16(uint8_t *dstp, const uint8_t *p1, int width,
97                       int threshold, const uint8_t *coordinates[], int coord)
98 {
99     uint16_t *dst = (uint16_t *)dstp;
100     int x, i;
101
102     for (x = 0; x < width; x++) {
103         int min = AV_RN16A(&p1[2 * x]);
104         int limit = FFMAX(min - threshold, 0);
105
106         for (i = 0; i < 8; i++) {
107             if (coord & (1 << i)) {
108                 min = FFMIN(min, AV_RN16A(coordinates[i] + x * 2));
109             }
110             min = FFMAX(min, limit);
111         }
112
113         dst[x] = min;
114     }
115 }
116
117 static void dilation(uint8_t *dst, const uint8_t *p1, int width,
118                      int threshold, const uint8_t *coordinates[], int coord)
119 {
120     int x, i;
121
122     for (x = 0; x < width; x++) {
123         int max = p1[x];
124         int limit = FFMIN(max + threshold, 255);
125
126         for (i = 0; i < 8; i++) {
127             if (coord & (1 << i)) {
128                 max = FFMAX(max, *(coordinates[i] + x));
129             }
130             max = FFMIN(max, limit);
131         }
132
133         dst[x] = max;
134     }
135 }
136
137 static void dilation16(uint8_t *dstp, const uint8_t *p1, int width,
138                        int threshold, const uint8_t *coordinates[], int coord)
139 {
140     uint16_t *dst = (uint16_t *)dstp;
141     int x, i;
142
143     for (x = 0; x < width; x++) {
144         int max = AV_RN16A(&p1[x * 2]);
145         int limit = FFMIN(max + threshold, 255);
146
147         for (i = 0; i < 8; i++) {
148             if (coord & (1 << i)) {
149                 max = FFMAX(max, AV_RN16A(coordinates[i] + x * 2));
150             }
151             max = FFMIN(max, limit);
152         }
153
154         dst[x] = max;
155     }
156 }
157
158 static void deflate(uint8_t *dst, const uint8_t *p1, int width,
159                     int threshold, const uint8_t *coordinates[], int coord)
160 {
161     int x, i;
162
163     for (x = 0; x < width; x++) {
164         int sum = 0;
165         int limit = FFMAX(p1[x] - threshold, 0);
166
167         for (i = 0; i < 8; sum += *(coordinates[i++] + x));
168
169         dst[x] = FFMAX(FFMIN(sum / 8, p1[x]), limit);
170     }
171 }
172
173 static void deflate16(uint8_t *dstp, const uint8_t *p1, int width,
174                       int threshold, const uint8_t *coordinates[], int coord)
175 {
176     uint16_t *dst = (uint16_t *)dstp;
177     int x, i;
178
179     for (x = 0; x < width; x++) {
180         int sum = 0;
181         int limit = FFMAX(AV_RN16A(&p1[2 * x]) - threshold, 0);
182
183         for (i = 0; i < 8; sum += AV_RN16A(coordinates[i++] + x * 2));
184
185         dst[x] = FFMAX(FFMIN(sum / 8, p1[x]), limit);
186     }
187 }
188
189 static void inflate(uint8_t *dst, const uint8_t *p1, int width,
190                     int threshold, const uint8_t *coordinates[], int coord)
191 {
192     int x, i;
193
194     for (x = 0; x < width; x++) {
195         int sum = 0;
196         int limit = FFMIN(p1[x] + threshold, 255);
197
198         for (i = 0; i < 8; sum += *(coordinates[i++] + x));
199
200         dst[x] = FFMIN(FFMAX(sum / 8, p1[x]), limit);
201     }
202 }
203
204 static void inflate16(uint8_t *dstp, const uint8_t *p1, int width,
205                       int threshold, const uint8_t *coordinates[], int coord)
206 {
207     uint16_t *dst = (uint16_t *)dstp;
208     int x, i;
209
210     for (x = 0; x < width; x++) {
211         int sum = 0;
212         int limit = FFMIN(AV_RN16A(&p1[2 * x]) + threshold, 255);
213
214         for (i = 0; i < 8; sum += AV_RN16A(coordinates[i++] + x * 2));
215
216         dst[x] = FFMIN(FFMAX(sum / 8, p1[x]), limit);
217     }
218 }
219
220 static int config_input(AVFilterLink *inlink)
221 {
222     AVFilterContext *ctx = inlink->dst;
223     NContext *s = ctx->priv;
224     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
225
226     s->depth = desc->comp[0].depth;
227     s->bpc = (s->depth + 7) / 8;
228
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;
233
234     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
235
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;
244
245     return 0;
246 }
247
248 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
249 {
250     NContext *s = ctx->priv;
251     ThreadData *td = arg;
252     AVFrame *out = td->out;
253     AVFrame *in = td->in;
254     int plane, y;
255
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;
267
268         if (!threshold) {
269             av_image_copy_plane(dst, dstride, src, stride, width * bpc, slice_end - slice_start);
270             continue;
271         }
272
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,
277                                              src,                                            src + 2 * bpc,
278                                              src + ph * stride, src + 1 * bpc + ph * stride, src + 2 * bpc + ph * stride};
279
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};
283
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};
287
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);
291
292             src += stride;
293             dst += dstride;
294         }
295     }
296
297     return 0;
298 }
299
300 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
301 {
302     AVFilterContext *ctx = inlink->dst;
303     AVFilterLink *outlink = ctx->outputs[0];
304     NContext *s = ctx->priv;
305     ThreadData td;
306     AVFrame *out;
307
308     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
309     if (!out) {
310         av_frame_free(&in);
311         return AVERROR(ENOMEM);
312     }
313     av_frame_copy_props(out, in);
314
315     td.in = in;
316     td.out = out;
317     ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(s->planeheight[1], ff_filter_get_nb_threads(ctx)));
318
319     av_frame_free(&in);
320     return ff_filter_frame(outlink, out);
321 }
322
323 static const AVFilterPad neighbor_inputs[] = {
324     {
325         .name         = "default",
326         .type         = AVMEDIA_TYPE_VIDEO,
327         .filter_frame = filter_frame,
328         .config_props = config_input,
329     },
330     { NULL }
331 };
332
333 static const AVFilterPad neighbor_outputs[] = {
334     {
335         .name = "default",
336         .type = AVMEDIA_TYPE_VIDEO,
337     },
338     { NULL }
339 };
340
341 #define OFFSET(x) offsetof(NContext, x)
342 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
343
344 #define DEFINE_NEIGHBOR_FILTER(name_, description_)          \
345 AVFILTER_DEFINE_CLASS(name_);                                \
346                                                              \
347 AVFilter ff_vf_##name_ = {                                   \
348     .name          = #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,            \
357 }
358
359 #if CONFIG_EROSION_FILTER
360
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 },
367     { NULL }
368 };
369
370 DEFINE_NEIGHBOR_FILTER(erosion, "Apply erosion effect.");
371
372 #endif /* CONFIG_EROSION_FILTER */
373
374 #if CONFIG_DILATION_FILTER
375
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 },
382     { NULL }
383 };
384
385 DEFINE_NEIGHBOR_FILTER(dilation, "Apply dilation effect.");
386
387 #endif /* CONFIG_DILATION_FILTER */
388
389 #if CONFIG_DEFLATE_FILTER
390
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 },
396     { NULL }
397 };
398
399 DEFINE_NEIGHBOR_FILTER(deflate, "Apply deflate effect.");
400
401 #endif /* CONFIG_DEFLATE_FILTER */
402
403 #if CONFIG_INFLATE_FILTER
404
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 },
410     { NULL }
411 };
412
413 DEFINE_NEIGHBOR_FILTER(inflate, "Apply inflate effect.");
414
415 #endif /* CONFIG_INFLATE_FILTER */