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/avstring.h"
23 #include "libavutil/imgutils.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/pixdesc.h"
31 typedef struct ConvolutionContext {
47 void (*filter[4])(struct ConvolutionContext *s, AVFrame *in, AVFrame *out, int plane);
50 #define OFFSET(x) offsetof(ConvolutionContext, x)
51 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
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},
69 AVFILTER_DEFINE_CLASS(convolution);
71 static const int same3x3[9] = {0, 0, 0,
75 static const int same5x5[25] = {0, 0, 0, 0, 0,
81 static int query_formats(AVFilterContext *ctx)
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,
94 return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
97 static int config_input(AVFilterLink *inlink)
99 ConvolutionContext *s = inlink->dst->priv;
100 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
103 if ((ret = av_image_fill_linesizes(s->planewidth, inlink->format, inlink->w)) < 0)
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;
109 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
111 s->bstride = s->planewidth[0] + 32;
112 s->buffer = av_malloc(5 * s->bstride);
114 return AVERROR(ENOMEM);
119 static inline void line_copy8(uint8_t *line, const uint8_t *srcp, int width, int mergin)
123 memcpy(line, srcp, width);
125 for (i = mergin; i > 0; i--) {
127 line[width - 1 + i] = line[width - 1 - i];
131 static void filter_3x3(ConvolutionContext *s, AVFrame *in, AVFrame *out, int plane)
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];
148 line_copy8(p0, src + stride, width, 1);
149 line_copy8(p1, src, width, 1);
151 for (y = 0; y < height; y++) {
152 src += stride * (y < height - 1 ? 1 : -1);
153 line_copy8(p2, src, width, 1);
155 for (x = 0; x < width; x++) {
156 int sum = p0[x - 1] * matrix[0] +
158 p0[x + 1] * matrix[2] +
159 p1[x - 1] * matrix[3] +
161 p1[x + 1] * matrix[5] +
162 p2[x - 1] * matrix[6] +
164 p2[x + 1] * matrix[8];
165 sum = (int)(sum * rdiv + bias + 0.5f);
166 dst[x] = av_clip_uint8(sum);
171 p2 = (p2 == end) ? orig: p2 + bstride;
172 dst += out->linesize[plane];
176 static void filter_5x5(ConvolutionContext *s, AVFrame *in, AVFrame *out, int plane)
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];
195 line_copy8(p0, src + 2 * stride, width, 2);
196 line_copy8(p1, src + stride, width, 2);
197 line_copy8(p2, src, width, 2);
199 line_copy8(p3, src, width, 2);
202 for (y = 0; y < height; y++) {
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
211 src += stride * (y < height - 2 ? 1 : -1);
212 line_copy8(p4, src, width, 2);
214 for (x = 0; x < width; x++) {
217 for (i = 0; i < 25; i++) {
218 sum += *(array[i] + x) * matrix[i];
220 sum = (int)(sum * rdiv + bias + 0.5f);
221 dst[x] = av_clip_uint8(sum);
228 p4 = (p4 == end) ? orig: p4 + bstride;
229 dst += out->linesize[plane];
233 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
235 ConvolutionContext *s = inlink->dst->priv;
236 AVFilterLink *outlink = inlink->dst->outputs[0];
240 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
243 return AVERROR(ENOMEM);
245 av_frame_copy_props(out, in);
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]);
256 s->filter[plane](s, in, out, plane);
260 return ff_filter_frame(outlink, out);
263 static av_cold int init(AVFilterContext *ctx)
265 ConvolutionContext *s = ctx->priv;
268 for (i = 0; i < 4; i++) {
269 int *matrix = (int *)s->matrix[i];
270 char *p, *arg, *saveptr = NULL;
272 p = s->matrix_str[i];
273 while (s->matrix_length[i] < 25) {
274 if (!(arg = av_strtok(p, " ", &saveptr)))
278 sscanf(arg, "%d", &matrix[s->matrix_length[i]]);
279 s->matrix_length[i]++;
282 if (s->matrix_length[i] == 9) {
283 if (!memcmp(matrix, same3x3, sizeof(same3x3)))
286 s->filter[i] = filter_3x3;
287 } else if (s->matrix_length[i] == 25) {
288 if (!memcmp(matrix, same5x5, sizeof(same5x5)))
291 s->filter[i] = filter_5x5;
293 return AVERROR(EINVAL);
300 static av_cold void uninit(AVFilterContext *ctx)
302 ConvolutionContext *s = ctx->priv;
304 av_freep(&s->buffer);
307 static const AVFilterPad convolution_inputs[] = {
310 .type = AVMEDIA_TYPE_VIDEO,
311 .config_props = config_input,
312 .filter_frame = filter_frame,
317 static const AVFilterPad convolution_outputs[] = {
320 .type = AVMEDIA_TYPE_VIDEO,
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,
332 .query_formats = query_formats,
333 .inputs = convolution_inputs,
334 .outputs = convolution_outputs,
335 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,