2 * Copyright (c) 2017 Paul B Mahol
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/colorspace.h"
22 #include "libavutil/common.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/pixdesc.h"
26 #include "drawutils.h"
34 enum FillMode { FM_SMEAR, FM_MIRROR, FM_FIXED, FM_NB_MODES };
36 typedef struct Borders {
37 int left, right, top, bottom;
40 typedef struct FillBordersContext {
42 int left, right, top, bottom;
52 uint8_t rgba_color[4];
54 void (*fillborders)(struct FillBordersContext *s, AVFrame *frame);
57 static int query_formats(AVFilterContext *ctx)
59 static const enum AVPixelFormat pix_fmts[] = {
60 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
61 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
62 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
63 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
64 AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
65 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
66 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
67 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
68 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
69 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
70 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
71 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
72 AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
73 AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
74 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
75 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
76 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
77 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
80 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
82 return AVERROR(ENOMEM);
83 return ff_set_common_formats(ctx, fmts_list);
86 static void smear_borders8(FillBordersContext *s, AVFrame *frame)
90 for (p = 0; p < s->nb_planes; p++) {
91 uint8_t *ptr = frame->data[p];
92 int linesize = frame->linesize[p];
94 for (y = s->borders[p].top; y < s->planeheight[p] - s->borders[p].bottom; y++) {
95 memset(ptr + y * linesize,
96 *(ptr + y * linesize + s->borders[p].left),
98 memset(ptr + y * linesize + s->planewidth[p] - s->borders[p].right,
99 *(ptr + y * linesize + s->planewidth[p] - s->borders[p].right - 1),
100 s->borders[p].right);
103 for (y = 0; y < s->borders[p].top; y++) {
104 memcpy(ptr + y * linesize,
105 ptr + s->borders[p].top * linesize, s->planewidth[p]);
108 for (y = s->planeheight[p] - s->borders[p].bottom; y < s->planeheight[p]; y++) {
109 memcpy(ptr + y * linesize,
110 ptr + (s->planeheight[p] - s->borders[p].bottom - 1) * linesize,
116 static void smear_borders16(FillBordersContext *s, AVFrame *frame)
120 for (p = 0; p < s->nb_planes; p++) {
121 uint16_t *ptr = (uint16_t *)frame->data[p];
122 int linesize = frame->linesize[p] / 2;
124 for (y = s->borders[p].top; y < s->planeheight[p] - s->borders[p].bottom; y++) {
125 for (x = 0; x < s->borders[p].left; x++) {
126 ptr[y * linesize + x] = *(ptr + y * linesize + s->borders[p].left);
129 for (x = 0; x < s->borders[p].right; x++) {
130 ptr[y * linesize + s->planewidth[p] - s->borders[p].right + x] =
131 *(ptr + y * linesize + s->planewidth[p] - s->borders[p].right - 1);
135 for (y = 0; y < s->borders[p].top; y++) {
136 memcpy(ptr + y * linesize,
137 ptr + s->borders[p].top * linesize, s->planewidth[p] * 2);
140 for (y = s->planeheight[p] - s->borders[p].bottom; y < s->planeheight[p]; y++) {
141 memcpy(ptr + y * linesize,
142 ptr + (s->planeheight[p] - s->borders[p].bottom - 1) * linesize,
143 s->planewidth[p] * 2);
148 static void mirror_borders8(FillBordersContext *s, AVFrame *frame)
152 for (p = 0; p < s->nb_planes; p++) {
153 uint8_t *ptr = frame->data[p];
154 int linesize = frame->linesize[p];
156 for (y = s->borders[p].top; y < s->planeheight[p] - s->borders[p].bottom; y++) {
157 for (x = 0; x < s->borders[p].left; x++) {
158 ptr[y * linesize + x] = ptr[y * linesize + s->borders[p].left * 2 - 1 - x];
161 for (x = 0; x < s->borders[p].right; x++) {
162 ptr[y * linesize + s->planewidth[p] - s->borders[p].right + x] =
163 ptr[y * linesize + s->planewidth[p] - s->borders[p].right - 1 - x];
167 for (y = 0; y < s->borders[p].top; y++) {
168 memcpy(ptr + y * linesize,
169 ptr + (s->borders[p].top * 2 - 1 - y) * linesize,
173 for (y = 0; y < s->borders[p].bottom; y++) {
174 memcpy(ptr + (s->planeheight[p] - s->borders[p].bottom + y) * linesize,
175 ptr + (s->planeheight[p] - s->borders[p].bottom - 1 - y) * linesize,
181 static void mirror_borders16(FillBordersContext *s, AVFrame *frame)
185 for (p = 0; p < s->nb_planes; p++) {
186 uint16_t *ptr = (uint16_t *)frame->data[p];
187 int linesize = frame->linesize[p] / 2;
189 for (y = s->borders[p].top; y < s->planeheight[p] - s->borders[p].bottom; y++) {
190 for (x = 0; x < s->borders[p].left; x++) {
191 ptr[y * linesize + x] = ptr[y * linesize + s->borders[p].left * 2 - 1 - x];
194 for (x = 0; x < s->borders[p].right; x++) {
195 ptr[y * linesize + s->planewidth[p] - s->borders[p].right + x] =
196 ptr[y * linesize + s->planewidth[p] - s->borders[p].right - 1 - x];
200 for (y = 0; y < s->borders[p].top; y++) {
201 memcpy(ptr + y * linesize,
202 ptr + (s->borders[p].top * 2 - 1 - y) * linesize,
203 s->planewidth[p] * 2);
206 for (y = 0; y < s->borders[p].bottom; y++) {
207 memcpy(ptr + (s->planeheight[p] - s->borders[p].bottom + y) * linesize,
208 ptr + (s->planeheight[p] - s->borders[p].bottom - 1 - y) * linesize,
209 s->planewidth[p] * 2);
214 static void fixed_borders8(FillBordersContext *s, AVFrame *frame)
218 for (p = 0; p < s->nb_planes; p++) {
219 uint8_t *ptr = frame->data[p];
220 uint8_t fill = s->fill[p];
221 int linesize = frame->linesize[p];
223 for (y = s->borders[p].top; y < s->planeheight[p] - s->borders[p].bottom; y++) {
224 memset(ptr + y * linesize, fill, s->borders[p].left);
225 memset(ptr + y * linesize + s->planewidth[p] - s->borders[p].right, fill,
226 s->borders[p].right);
229 for (y = 0; y < s->borders[p].top; y++) {
230 memset(ptr + y * linesize, fill, s->planewidth[p]);
233 for (y = s->planeheight[p] - s->borders[p].bottom; y < s->planeheight[p]; y++) {
234 memset(ptr + y * linesize, fill, s->planewidth[p]);
239 static void fixed_borders16(FillBordersContext *s, AVFrame *frame)
243 for (p = 0; p < s->nb_planes; p++) {
244 uint16_t *ptr = (uint16_t *)frame->data[p];
245 uint16_t fill = s->fill[p] << (s->depth - 8);
246 int linesize = frame->linesize[p] / 2;
248 for (y = s->borders[p].top; y < s->planeheight[p] - s->borders[p].bottom; y++) {
249 for (x = 0; x < s->borders[p].left; x++) {
250 ptr[y * linesize + x] = fill;
253 for (x = 0; x < s->borders[p].right; x++) {
254 ptr[y * linesize + s->planewidth[p] - s->borders[p].right + x] = fill;
258 for (y = 0; y < s->borders[p].top; y++) {
259 for (x = 0; x < s->planewidth[p]; x++) {
260 ptr[y * linesize + x] = fill;
264 for (y = s->planeheight[p] - s->borders[p].bottom; y < s->planeheight[p]; y++) {
265 for (x = 0; x < s->planewidth[p]; x++) {
266 ptr[y * linesize + x] = fill;
272 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
274 FillBordersContext *s = inlink->dst->priv;
276 s->fillborders(s, frame);
278 return ff_filter_frame(inlink->dst->outputs[0], frame);
281 static int config_input(AVFilterLink *inlink)
283 AVFilterContext *ctx = inlink->dst;
284 FillBordersContext *s = ctx->priv;
285 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
287 s->nb_planes = desc->nb_components;
288 s->depth = desc->comp[0].depth;
290 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
291 s->planeheight[0] = s->planeheight[3] = inlink->h;
292 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
293 s->planewidth[0] = s->planewidth[3] = inlink->w;
295 if (inlink->w < s->left + s->right ||
296 inlink->w <= s->left ||
297 inlink->w <= s->right ||
298 inlink->h < s->top + s->bottom ||
299 inlink->h <= s->top ||
300 inlink->h <= s->bottom ||
301 inlink->w < s->left * 2 ||
302 inlink->w < s->right * 2 ||
303 inlink->h < s->top * 2 ||
304 inlink->h < s->bottom * 2) {
305 av_log(ctx, AV_LOG_ERROR, "Borders are bigger than input frame size.\n");
306 return AVERROR(EINVAL);
309 s->borders[0].left = s->borders[3].left = s->left;
310 s->borders[0].right = s->borders[3].right = s->right;
311 s->borders[0].top = s->borders[3].top = s->top;
312 s->borders[0].bottom = s->borders[3].bottom = s->bottom;
314 s->borders[1].left = s->left >> desc->log2_chroma_w;
315 s->borders[1].right = s->right >> desc->log2_chroma_w;
316 s->borders[1].top = s->top >> desc->log2_chroma_h;
317 s->borders[1].bottom = s->bottom >> desc->log2_chroma_h;
319 s->borders[2].left = s->left >> desc->log2_chroma_w;
320 s->borders[2].right = s->right >> desc->log2_chroma_w;
321 s->borders[2].top = s->top >> desc->log2_chroma_h;
322 s->borders[2].bottom = s->bottom >> desc->log2_chroma_h;
325 case FM_SMEAR: s->fillborders = s->depth <= 8 ? smear_borders8 : smear_borders16; break;
326 case FM_MIRROR: s->fillborders = s->depth <= 8 ? mirror_borders8 : mirror_borders16; break;
327 case FM_FIXED: s->fillborders = s->depth <= 8 ? fixed_borders8 : fixed_borders16; break;
330 s->yuv_color[Y] = RGB_TO_Y_CCIR(s->rgba_color[R], s->rgba_color[G], s->rgba_color[B]);
331 s->yuv_color[U] = RGB_TO_U_CCIR(s->rgba_color[R], s->rgba_color[G], s->rgba_color[B], 0);
332 s->yuv_color[V] = RGB_TO_V_CCIR(s->rgba_color[R], s->rgba_color[G], s->rgba_color[B], 0);
333 s->yuv_color[A] = s->rgba_color[A];
335 if (desc->flags & AV_PIX_FMT_FLAG_RGB) {
339 ff_fill_rgba_map(rgba_map, inlink->format);
340 for (i = 0; i < 4; i++)
341 s->fill[rgba_map[i]] = s->rgba_color[i];
343 memcpy(s->fill, s->yuv_color, sizeof(s->yuv_color));
349 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
350 char *res, int res_len, int flags)
354 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
358 return config_input(ctx->inputs[0]);
361 #define OFFSET(x) offsetof(FillBordersContext, x)
362 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
364 static const AVOption fillborders_options[] = {
365 { "left", "set the left fill border", OFFSET(left), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
366 { "right", "set the right fill border", OFFSET(right), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
367 { "top", "set the top fill border", OFFSET(top), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
368 { "bottom", "set the bottom fill border", OFFSET(bottom), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
369 { "mode", "set the fill borders mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=FM_SMEAR}, 0, FM_NB_MODES-1, FLAGS, "mode" },
370 { "smear", NULL, 0, AV_OPT_TYPE_CONST, {.i64=FM_SMEAR}, 0, 0, FLAGS, "mode" },
371 { "mirror", NULL, 0, AV_OPT_TYPE_CONST, {.i64=FM_MIRROR}, 0, 0, FLAGS, "mode" },
372 { "fixed", NULL, 0, AV_OPT_TYPE_CONST, {.i64=FM_FIXED}, 0, 0, FLAGS, "mode" },
373 { "color", "set the color for the fixed mode", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str = "black"}, .flags = FLAGS },
377 AVFILTER_DEFINE_CLASS(fillborders);
379 static const AVFilterPad fillborders_inputs[] = {
382 .type = AVMEDIA_TYPE_VIDEO,
383 .config_props = config_input,
384 .filter_frame = filter_frame,
390 static const AVFilterPad fillborders_outputs[] = {
393 .type = AVMEDIA_TYPE_VIDEO,
398 AVFilter ff_vf_fillborders = {
399 .name = "fillborders",
400 .description = NULL_IF_CONFIG_SMALL("Fill borders of the input video."),
401 .priv_size = sizeof(FillBordersContext),
402 .priv_class = &fillborders_class,
403 .query_formats = query_formats,
404 .inputs = fillborders_inputs,
405 .outputs = fillborders_outputs,
406 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
407 .process_command = process_command,