]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_convolution.c
avfilter/vf_waveform: add acolor filter
[ffmpeg] / libavfilter / vf_convolution.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/avstring.h"
23 #include "libavutil/imgutils.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/pixdesc.h"
26 #include "avfilter.h"
27 #include "formats.h"
28 #include "internal.h"
29 #include "video.h"
30
31 typedef struct ConvolutionContext {
32     const AVClass *class;
33
34     char *matrix_str[4];
35     float rdiv[4];
36     float bias[4];
37
38     int bstride;
39     uint8_t *buffer;
40     int nb_planes;
41     int planewidth[4];
42     int planeheight[4];
43     int matrix[4][25];
44     int matrix_length[4];
45     int copy[4];
46
47     void (*filter[4])(struct ConvolutionContext *s, AVFrame *in, AVFrame *out, int plane);
48 } ConvolutionContext;
49
50 #define OFFSET(x) offsetof(ConvolutionContext, x)
51 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
52
53 static const AVOption convolution_options[] = {
54     { "0m", "set matrix for 1st plane", OFFSET(matrix_str[0]), AV_OPT_TYPE_STRING, {.str="0 0 0 0 1 0 0 0 0"}, 0, 0, FLAGS },
55     { "1m", "set matrix for 2nd plane", OFFSET(matrix_str[1]), AV_OPT_TYPE_STRING, {.str="0 0 0 0 1 0 0 0 0"}, 0, 0, FLAGS },
56     { "2m", "set matrix for 3rd plane", OFFSET(matrix_str[2]), AV_OPT_TYPE_STRING, {.str="0 0 0 0 1 0 0 0 0"}, 0, 0, FLAGS },
57     { "3m", "set matrix for 4th plane", OFFSET(matrix_str[3]), AV_OPT_TYPE_STRING, {.str="0 0 0 0 1 0 0 0 0"}, 0, 0, FLAGS },
58     { "0rdiv", "set rdiv for 1st plane", OFFSET(rdiv[0]), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, INT_MAX, FLAGS},
59     { "1rdiv", "set rdiv for 2nd plane", OFFSET(rdiv[1]), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, INT_MAX, FLAGS},
60     { "2rdiv", "set rdiv for 3rd plane", OFFSET(rdiv[2]), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, INT_MAX, FLAGS},
61     { "3rdiv", "set rdiv for 4th plane", OFFSET(rdiv[3]), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, INT_MAX, FLAGS},
62     { "0bias", "set bias for 1st plane", OFFSET(bias[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
63     { "1bias", "set bias for 2nd plane", OFFSET(bias[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
64     { "2bias", "set bias for 3rd plane", OFFSET(bias[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
65     { "3bias", "set bias for 4th plane", OFFSET(bias[3]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
66     { NULL }
67 };
68
69 AVFILTER_DEFINE_CLASS(convolution);
70
71 static const int same3x3[9] = {0, 0, 0,
72                                0, 1, 0,
73                                0, 0, 0};
74
75 static const int same5x5[25] = {0, 0, 0, 0, 0,
76                                 0, 0, 0, 0, 0,
77                                 0, 0, 1, 0, 0,
78                                 0, 0, 0, 0, 0,
79                                 0, 0, 0, 0, 0};
80
81 static int query_formats(AVFilterContext *ctx)
82 {
83     static const enum AVPixelFormat pix_fmts[] = {
84         AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
85         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
86         AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
87         AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P,
88         AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
89         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
90         AV_PIX_FMT_GRAY8,
91         AV_PIX_FMT_NONE
92     };
93
94     return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
95 }
96
97 static int config_input(AVFilterLink *inlink)
98 {
99     ConvolutionContext *s = inlink->dst->priv;
100     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
101     int ret;
102
103     if ((ret = av_image_fill_linesizes(s->planewidth, inlink->format, inlink->w)) < 0)
104         return ret;
105
106     s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
107     s->planeheight[0] = s->planeheight[3] = inlink->h;
108
109     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
110
111     s->bstride = s->planewidth[0] + 32;
112     s->buffer = av_malloc(5 * s->bstride);
113     if (!s->buffer)
114         return AVERROR(ENOMEM);
115
116     return 0;
117 }
118
119 static inline void line_copy8(uint8_t *line, const uint8_t *srcp, int width, int mergin)
120 {
121     int i;
122
123     memcpy(line, srcp, width);
124
125     for (i = mergin; i > 0; i--) {
126         line[-i] = line[i];
127         line[width - 1 + i] = line[width - 1 - i];
128     }
129 }
130
131 static void filter_3x3(ConvolutionContext *s, AVFrame *in, AVFrame *out, int plane)
132 {
133     const uint8_t *src = in->data[plane];
134     uint8_t *dst = out->data[plane];
135     const int stride = in->linesize[plane];
136     const int bstride = s->bstride;
137     const int height = s->planeheight[plane];
138     const int width  = s->planewidth[plane];
139     uint8_t *p0 = s->buffer + 16;
140     uint8_t *p1 = p0 + bstride;
141     uint8_t *p2 = p1 + bstride;
142     uint8_t *orig = p0, *end = p2;
143     const int *matrix = s->matrix[plane];
144     const float rdiv = s->rdiv[plane];
145     const float bias = s->bias[plane];
146     int y, x;
147
148     line_copy8(p0, src + stride, width, 1);
149     line_copy8(p1, src, width, 1);
150
151     for (y = 0; y < height; y++) {
152         src += stride * (y < height - 1 ? 1 : -1);
153         line_copy8(p2, src, width, 1);
154
155         for (x = 0; x < width; x++) {
156             int sum = p0[x - 1] * matrix[0] +
157                       p0[x] *     matrix[1] +
158                       p0[x + 1] * matrix[2] +
159                       p1[x - 1] * matrix[3] +
160                       p1[x] *     matrix[4] +
161                       p1[x + 1] * matrix[5] +
162                       p2[x - 1] * matrix[6] +
163                       p2[x] *     matrix[7] +
164                       p2[x + 1] * matrix[8];
165             sum = (int)(sum * rdiv + bias + 0.5f);
166             dst[x] = av_clip_uint8(sum);
167         }
168
169         p0 = p1;
170         p1 = p2;
171         p2 = (p2 == end) ? orig: p2 + bstride;
172         dst += out->linesize[plane];
173     }
174 }
175
176 static void filter_5x5(ConvolutionContext *s, AVFrame *in, AVFrame *out, int plane)
177 {
178     const uint8_t *src = in->data[plane];
179     uint8_t *dst = out->data[plane];
180     const int stride = in->linesize[plane];
181     const int bstride = s->bstride;
182     const int height = s->planeheight[plane];
183     const int width  = s->planewidth[plane];
184     uint8_t *p0 = s->buffer + 16;
185     uint8_t *p1 = p0 + bstride;
186     uint8_t *p2 = p1 + bstride;
187     uint8_t *p3 = p2 + bstride;
188     uint8_t *p4 = p3 + bstride;
189     uint8_t *orig = p0, *end = p4;
190     const int *matrix = s->matrix[plane];
191     float rdiv = s->rdiv[plane];
192     float bias = s->bias[plane];
193     int y, x, i;
194
195     line_copy8(p0, src + 2 * stride, width, 2);
196     line_copy8(p1, src + stride, width, 2);
197     line_copy8(p2, src, width, 2);
198     src += stride;
199     line_copy8(p3, src, width, 2);
200
201
202     for (y = 0; y < height; y++) {
203         uint8_t *array[] = {
204             p0 - 2, p0 - 1, p0, p0 + 1, p0 + 2,
205             p1 - 2, p1 - 1, p1, p1 + 1, p1 + 2,
206             p2 - 2, p2 - 1, p2, p2 + 1, p2 + 2,
207             p3 - 2, p3 - 1, p3, p3 + 1, p3 + 2,
208             p4 - 2, p4 - 1, p4, p4 + 1, p4 + 2
209         };
210
211         src += stride * (y < height - 2 ? 1 : -1);
212         line_copy8(p4, src, width, 2);
213
214         for (x = 0; x < width; x++) {
215             int sum = 0;
216
217             for (i = 0; i < 25; i++) {
218                 sum += *(array[i] + x) * matrix[i];
219             }
220             sum = (int)(sum * rdiv + bias + 0.5f);
221             dst[x] = av_clip_uint8(sum);
222         }
223
224         p0 = p1;
225         p1 = p2;
226         p2 = p3;
227         p3 = p4;
228         p4 = (p4 == end) ? orig: p4 + bstride;
229         dst += out->linesize[plane];
230     }
231 }
232
233 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
234 {
235     ConvolutionContext *s = inlink->dst->priv;
236     AVFilterLink *outlink = inlink->dst->outputs[0];
237     AVFrame *out;
238     int plane;
239
240     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
241     if (!out) {
242         av_frame_free(&in);
243         return AVERROR(ENOMEM);
244     }
245     av_frame_copy_props(out, in);
246
247     for (plane = 0; plane < s->nb_planes; plane++) {
248         if (s->copy[plane]) {
249             av_image_copy_plane(out->data[plane], out->linesize[plane],
250                                 in->data[plane], in->linesize[plane],
251                                 s->planewidth[plane],
252                                 s->planeheight[plane]);
253             continue;
254         }
255
256         s->filter[plane](s, in, out, plane);
257     }
258
259     av_frame_free(&in);
260     return ff_filter_frame(outlink, out);
261 }
262
263 static av_cold int init(AVFilterContext *ctx)
264 {
265     ConvolutionContext *s = ctx->priv;
266     int i;
267
268     for (i = 0; i < 4; i++) {
269         int *matrix = (int *)s->matrix[i];
270         char *p, *arg, *saveptr = NULL;
271
272         p = s->matrix_str[i];
273         while (s->matrix_length[i] < 25) {
274             if (!(arg = av_strtok(p, " ", &saveptr)))
275                 break;
276
277             p = NULL;
278             sscanf(arg, "%d", &matrix[s->matrix_length[i]]);
279             s->matrix_length[i]++;
280         }
281
282         if (s->matrix_length[i] == 9) {
283             if (!memcmp(matrix, same3x3, sizeof(same3x3)))
284                 s->copy[i] = 1;
285             else
286                 s->filter[i] = filter_3x3;
287         } else if (s->matrix_length[i] == 25) {
288             if (!memcmp(matrix, same5x5, sizeof(same5x5)))
289                 s->copy[i] = 1;
290             else
291                 s->filter[i] = filter_5x5;
292         } else {
293             return AVERROR(EINVAL);
294         }
295     }
296
297     return 0;
298 }
299
300 static av_cold void uninit(AVFilterContext *ctx)
301 {
302     ConvolutionContext *s = ctx->priv;
303
304     av_freep(&s->buffer);
305 }
306
307 static const AVFilterPad convolution_inputs[] = {
308     {
309         .name         = "default",
310         .type         = AVMEDIA_TYPE_VIDEO,
311         .config_props = config_input,
312         .filter_frame = filter_frame,
313     },
314     { NULL }
315 };
316
317 static const AVFilterPad convolution_outputs[] = {
318     {
319         .name = "default",
320         .type = AVMEDIA_TYPE_VIDEO,
321     },
322     { NULL }
323 };
324
325 AVFilter ff_vf_convolution = {
326     .name          = "convolution",
327     .description   = NULL_IF_CONFIG_SMALL("Apply convolution filter."),
328     .priv_size     = sizeof(ConvolutionContext),
329     .priv_class    = &convolution_class,
330     .init          = init,
331     .uninit        = uninit,
332     .query_formats = query_formats,
333     .inputs        = convolution_inputs,
334     .outputs       = convolution_outputs,
335     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
336 };