Fix style: "if(" -> "if (".
[libav.git] / libavfilter / vf_pad.c
CommitLineData
3c940173
MN
1/*
2 * copyright (c) 2008 vmrsss
3 * copyright (c) 2009 Stefano Sabatini
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/**
aeaa9630 23 * @file
23ccf3c7 24 * video padding filter and color source
3c940173
MN
25 */
26
27#include "avfilter.h"
28#include "parseutils.h"
29#include "libavutil/pixdesc.h"
2b4abbd6 30#include "libavutil/colorspace.h"
23ccf3c7 31#include "libavcodec/avcodec.h"
3c940173 32
039baa78
SS
33enum { RED = 0, GREEN, BLUE, ALPHA };
34
35static int fill_line_with_color(uint8_t *line[4], int line_step[4], int w, uint8_t color[4],
36 enum PixelFormat pix_fmt, uint8_t rgba_color[4], int *is_packed_rgba)
37{
38 uint8_t rgba_map[4] = {0};
39 int i;
40 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[pix_fmt];
41 int hsub = pix_desc->log2_chroma_w;
42
43 *is_packed_rgba = 1;
44 switch (pix_fmt) {
45 case PIX_FMT_ARGB: rgba_map[ALPHA] = 0; rgba_map[RED ] = 1; rgba_map[GREEN] = 2; rgba_map[BLUE ] = 3; break;
46 case PIX_FMT_ABGR: rgba_map[ALPHA] = 0; rgba_map[BLUE ] = 1; rgba_map[GREEN] = 2; rgba_map[RED ] = 3; break;
47 case PIX_FMT_RGBA:
48 case PIX_FMT_RGB24: rgba_map[RED ] = 0; rgba_map[GREEN] = 1; rgba_map[BLUE ] = 2; rgba_map[ALPHA] = 3; break;
49 case PIX_FMT_BGRA:
50 case PIX_FMT_BGR24: rgba_map[BLUE ] = 0; rgba_map[GREEN] = 1; rgba_map[RED ] = 2; rgba_map[ALPHA] = 3; break;
51 default:
52 *is_packed_rgba = 0;
53 }
54
55 if (*is_packed_rgba) {
56 line_step[0] = (av_get_bits_per_pixel(pix_desc))>>3;
57 for (i = 0; i < 4; i++)
58 color[rgba_map[i]] = rgba_color[i];
59
60 line[0] = av_malloc(w * line_step[0]);
61 for (i = 0; i < w; i++)
62 memcpy(line[0] + i * line_step[0], color, line_step[0]);
63 } else {
64 int plane;
65
d0b9b91b
MR
66 color[RED ] = RGB_TO_Y_CCIR(rgba_color[0], rgba_color[1], rgba_color[2]);
67 color[GREEN] = RGB_TO_U_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
68 color[BLUE ] = RGB_TO_V_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
039baa78
SS
69 color[ALPHA] = rgba_color[3];
70
71 for (plane = 0; plane < 4; plane++) {
72 int line_size;
73 int hsub1 = (plane == 1 || plane == 2) ? hsub : 0;
74
75 line_step[plane] = 1;
76 line_size = (w >> hsub1) * line_step[plane];
77 line[plane] = av_malloc(line_size);
78 memset(line[plane], color[plane], line_size);
79 }
80 }
81
82 return 0;
83}
84
1b364fd2
SS
85static void draw_rectangle(AVFilterPicRef *outpic, uint8_t *line[4], int line_step[4],
86 int hsub, int vsub, int x, int y, int w, int h)
87{
88 int i, plane;
89 uint8_t *p;
90
91 for (plane = 0; plane < 4 && outpic->data[plane]; plane++) {
92 int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
93 int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
94
95 p = outpic->data[plane] + (y >> vsub1) * outpic->linesize[plane];
96 for (i = 0; i < (h >> vsub1); i++) {
97 memcpy(p + (x >> hsub1) * line_step[plane], line[plane], (w >> hsub1) * line_step[plane]);
98 p += outpic->linesize[plane];
99 }
100 }
101}
102
103static int query_formats(AVFilterContext *ctx)
104{
105 static const enum PixelFormat pix_fmts[] = {
106 PIX_FMT_ARGB, PIX_FMT_RGBA,
107 PIX_FMT_ABGR, PIX_FMT_BGRA,
108 PIX_FMT_RGB24, PIX_FMT_BGR24,
109
110 PIX_FMT_YUV444P, PIX_FMT_YUV422P,
111 PIX_FMT_YUV420P, PIX_FMT_YUV411P,
112 PIX_FMT_YUV410P, PIX_FMT_YUV440P,
113 PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P,
114 PIX_FMT_YUVJ420P, PIX_FMT_YUVJ440P,
115 PIX_FMT_YUVA420P,
116
117 PIX_FMT_NONE
118 };
119
120 avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
121 return 0;
122}
123
124#if CONFIG_PAD_FILTER
125
3c940173
MN
126typedef struct {
127 int w, h; ///< output dimensions, a value of 0 will result in the input size
128 int x, y; ///< offsets of the input area with respect to the padded area
129 int in_w, in_h; ///< width and height for the padded input video, which has to be aligned to the chroma values in order to avoid chroma issues
130
131 uint8_t color[4]; ///< color expressed either in YUVA or RGBA colorspace for the padding area
132 uint8_t *line[4];
133 int line_step[4];
134 int hsub, vsub; ///< chroma subsampling values
135} PadContext;
136
137static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
138{
139 PadContext *pad = ctx->priv;
140 char color_string[128] = "black";
141
142 if (args)
143 sscanf(args, "%d:%d:%d:%d:%s", &pad->w, &pad->h, &pad->x, &pad->y, color_string);
144
145 if (av_parse_color(pad->color, color_string, ctx) < 0)
622e6ce5 146 return AVERROR(EINVAL);
3c940173
MN
147
148 /* sanity check params */
149 if (pad->w < 0 || pad->h < 0) {
150 av_log(ctx, AV_LOG_ERROR, "Negative size values are not acceptable.\n");
622e6ce5 151 return AVERROR(EINVAL);
3c940173
MN
152 }
153
154 return 0;
155}
156
157static av_cold void uninit(AVFilterContext *ctx)
158{
159 PadContext *pad = ctx->priv;
160 int i;
161
162 for (i = 0; i < 4; i++) {
163 av_freep(&pad->line[i]);
164 pad->line_step[i] = 0;
165 }
166}
167
3c940173
MN
168static int config_input(AVFilterLink *inlink)
169{
170 AVFilterContext *ctx = inlink->dst;
171 PadContext *pad = ctx->priv;
172 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
173 uint8_t rgba_color[4];
039baa78 174 int is_packed_rgba;
3c940173
MN
175
176 pad->hsub = pix_desc->log2_chroma_w;
177 pad->vsub = pix_desc->log2_chroma_h;
178
179 if (!pad->w)
180 pad->w = inlink->w;
181 if (!pad->h)
182 pad->h = inlink->h;
183
184 pad->w &= ~((1 << pad->hsub) - 1);
185 pad->h &= ~((1 << pad->vsub) - 1);
186 pad->x &= ~((1 << pad->hsub) - 1);
187 pad->y &= ~((1 << pad->vsub) - 1);
188
189 pad->in_w = inlink->w & ~((1 << pad->hsub) - 1);
190 pad->in_h = inlink->h & ~((1 << pad->vsub) - 1);
191
192 memcpy(rgba_color, pad->color, sizeof(rgba_color));
039baa78
SS
193 fill_line_with_color(pad->line, pad->line_step, pad->w, pad->color,
194 inlink->format, rgba_color, &is_packed_rgba);
3c940173
MN
195
196 av_log(ctx, AV_LOG_INFO, "w:%d h:%d x:%d y:%d color:0x%02X%02X%02X%02X[%s]\n",
197 pad->w, pad->h, pad->x, pad->y,
198 pad->color[0], pad->color[1], pad->color[2], pad->color[3],
039baa78 199 is_packed_rgba ? "rgba" : "yuva");
3c940173
MN
200
201 if (pad->x < 0 || pad->y < 0 ||
202 pad->w <= 0 || pad->h <= 0 ||
203 (unsigned)pad->x + (unsigned)inlink->w > pad->w ||
204 (unsigned)pad->y + (unsigned)inlink->h > pad->h) {
205 av_log(ctx, AV_LOG_ERROR,
206 "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
207 pad->x, pad->y, pad->x + inlink->w, pad->y + inlink->h, pad->w, pad->h);
622e6ce5 208 return AVERROR(EINVAL);
3c940173
MN
209 }
210
211 return 0;
212}
213
214static int config_output(AVFilterLink *outlink)
215{
216 PadContext *pad = outlink->src->priv;
217
218 outlink->w = pad->w;
219 outlink->h = pad->h;
220 return 0;
221}
222
223static AVFilterPicRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
224{
225 PadContext *pad = inlink->dst->priv;
226
227 AVFilterPicRef *picref = avfilter_get_video_buffer(inlink->dst->outputs[0], perms,
228 w + (pad->w - pad->in_w),
229 h + (pad->h - pad->in_h));
f66b3905 230 int plane;
3c940173 231
f66b3905 232 for (plane = 0; plane < 4 && picref->data[plane]; plane++) {
3c940173
MN
233 int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
234 int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0;
235
236 picref->data[plane] += (pad->x >> hsub) * pad->line_step[plane] +
237 (pad->y >> vsub) * picref->linesize[plane];
238 }
239
240 return picref;
241}
242
243static void start_frame(AVFilterLink *inlink, AVFilterPicRef *inpicref)
244{
245 PadContext *pad = inlink->dst->priv;
246 AVFilterPicRef *outpicref = avfilter_ref_pic(inpicref, ~0);
f66b3905
MR
247 int plane;
248
3c940173
MN
249 inlink->dst->outputs[0]->outpic = outpicref;
250
f66b3905 251 for (plane = 0; plane < 4 && outpicref->data[plane]; plane++) {
3c940173
MN
252 int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
253 int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0;
254
255 outpicref->data[plane] -= (pad->x >> hsub) * pad->line_step[plane] +
256 (pad->y >> vsub) * outpicref->linesize[plane];
257 }
258
259 avfilter_start_frame(inlink->dst->outputs[0], outpicref);
260}
261
262static void end_frame(AVFilterLink *link)
263{
264 avfilter_end_frame(link->dst->outputs[0]);
265 avfilter_unref_pic(link->cur_pic);
266}
267
3c940173
MN
268static void draw_send_bar_slice(AVFilterLink *link, int y, int h, int slice_dir, int before_slice)
269{
270 PadContext *pad = link->dst->priv;
271 int bar_y, bar_h = 0;
272
273 if (slice_dir * before_slice == 1 && y == pad->y) {
274 /* top bar */
275 bar_y = 0;
276 bar_h = pad->y;
277 } else if (slice_dir * before_slice == -1 && (y + h) == (pad->y + pad->in_h)) {
278 /* bottom bar */
279 bar_y = pad->y + pad->in_h;
280 bar_h = pad->h - pad->in_h - pad->y;
281 }
282
283 if (bar_h) {
284 draw_rectangle(link->dst->outputs[0]->outpic,
285 pad->line, pad->line_step, pad->hsub, pad->vsub,
286 0, bar_y, pad->w, bar_h);
287 avfilter_draw_slice(link->dst->outputs[0], bar_y, bar_h, slice_dir);
288 }
289}
290
291static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
292{
293 PadContext *pad = link->dst->priv;
294 AVFilterPicRef *outpic = link->dst->outputs[0]->outpic;
295
296 y += pad->y;
297
298 y &= ~((1 << pad->vsub) - 1);
299 h &= ~((1 << pad->vsub) - 1);
300
301 if (!h)
302 return;
303 draw_send_bar_slice(link, y, h, slice_dir, 1);
304
305 /* left border */
306 draw_rectangle(outpic, pad->line, pad->line_step, pad->hsub, pad->vsub,
307 0, y, pad->x, h);
308 /* right border */
309 draw_rectangle(outpic, pad->line, pad->line_step, pad->hsub, pad->vsub,
310 pad->x + pad->in_w, y, pad->w - pad->x - pad->in_w, h);
311 avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
312
313 draw_send_bar_slice(link, y, h, slice_dir, -1);
314}
315
316AVFilter avfilter_vf_pad = {
317 .name = "pad",
478b9d74 318 .description = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
3c940173
MN
319
320 .priv_size = sizeof(PadContext),
321 .init = init,
322 .uninit = uninit,
323 .query_formats = query_formats,
324
325 .inputs = (AVFilterPad[]) {{ .name = "default",
326 .type = AVMEDIA_TYPE_VIDEO,
327 .config_props = config_input,
328 .get_video_buffer = get_video_buffer,
329 .start_frame = start_frame,
330 .draw_slice = draw_slice,
331 .end_frame = end_frame, },
332 { .name = NULL}},
333
334 .outputs = (AVFilterPad[]) {{ .name = "default",
335 .type = AVMEDIA_TYPE_VIDEO,
336 .config_props = config_output, },
337 { .name = NULL}},
338};
1b364fd2
SS
339
340#endif /* CONFIG_PAD_FILTER */
23ccf3c7
SS
341
342#if CONFIG_COLOR_FILTER
343
344typedef struct {
345 int w, h;
346 uint8_t color[4];
347 AVRational time_base;
348 uint8_t *line[4];
349 int line_step[4];
350 int hsub, vsub; ///< chroma subsampling values
351 uint64_t pts;
352} ColorContext;
353
354static av_cold int color_init(AVFilterContext *ctx, const char *args, void *opaque)
355{
356 ColorContext *color = ctx->priv;
357 char color_string[128] = "black";
358 char frame_size [128] = "320x240";
359 char frame_rate [128] = "25";
360 AVRational frame_rate_q;
361 int ret;
362
363 if (args)
364 sscanf(args, "%127[^:]:%127[^:]:%127s", color_string, frame_size, frame_rate);
365
366 if (av_parse_video_frame_size(&color->w, &color->h, frame_size) < 0) {
367 av_log(ctx, AV_LOG_ERROR, "Invalid frame size: %s\n", frame_size);
368 return AVERROR(EINVAL);
369 }
370
371 if (av_parse_video_frame_rate(&frame_rate_q, frame_rate) < 0 ||
372 frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
373 av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: %s\n", frame_rate);
374 return AVERROR(EINVAL);
375 }
376 color->time_base.num = frame_rate_q.den;
377 color->time_base.den = frame_rate_q.num;
378
379 if ((ret = av_parse_color(color->color, color_string, ctx)) < 0)
380 return ret;
381
382 return 0;
383}
384
385static av_cold void color_uninit(AVFilterContext *ctx)
386{
387 ColorContext *color = ctx->priv;
388 int i;
389
390 for (i = 0; i < 4; i++) {
391 av_freep(&color->line[i]);
392 color->line_step[i] = 0;
393 }
394}
395
396static int color_config_props(AVFilterLink *inlink)
397{
398 AVFilterContext *ctx = inlink->src;
399 ColorContext *color = ctx->priv;
400 uint8_t rgba_color[4];
401 int is_packed_rgba;
402 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
403
404 color->hsub = pix_desc->log2_chroma_w;
405 color->vsub = pix_desc->log2_chroma_h;
406
407 color->w &= ~((1 << color->hsub) - 1);
408 color->h &= ~((1 << color->vsub) - 1);
409 if (avcodec_check_dimensions(ctx, color->w, color->h) < 0)
410 return AVERROR(EINVAL);
411
412 memcpy(rgba_color, color->color, sizeof(rgba_color));
413 fill_line_with_color(color->line, color->line_step, color->w, color->color,
414 inlink->format, rgba_color, &is_packed_rgba);
415
416 av_log(ctx, AV_LOG_INFO, "w:%d h:%d r:%d/%d color:0x%02x%02x%02x%02x[%s]\n",
417 color->w, color->h, color->time_base.den, color->time_base.num,
418 color->color[0], color->color[1], color->color[2], color->color[3],
419 is_packed_rgba ? "rgba" : "yuva");
420 inlink->w = color->w;
421 inlink->h = color->h;
422
423 return 0;
424}
425
426static int color_request_frame(AVFilterLink *link)
427{
428 ColorContext *color = link->src->priv;
429 AVFilterPicRef *picref = avfilter_get_video_buffer(link, AV_PERM_WRITE, color->w, color->h);
430 picref->pixel_aspect = (AVRational) {1, 1};
431 picref->pts = av_rescale_q(color->pts++, color->time_base, AV_TIME_BASE_Q);
432 picref->pos = 0;
433
434 avfilter_start_frame(link, avfilter_ref_pic(picref, ~0));
435 draw_rectangle(picref,
436 color->line, color->line_step, color->hsub, color->vsub,
437 0, 0, color->w, color->h);
438 avfilter_draw_slice(link, 0, color->h, 1);
439 avfilter_end_frame(link);
440 avfilter_unref_pic(picref);
441
442 return 0;
443}
444
445AVFilter avfilter_vsrc_color = {
446 .name = "color",
447 .description = NULL_IF_CONFIG_SMALL("Provide an uniformly colored input, syntax is: [color[:size[:rate]]]"),
448
449 .priv_size = sizeof(ColorContext),
450 .init = color_init,
451 .uninit = color_uninit,
452
453 .query_formats = query_formats,
454
455 .inputs = (AVFilterPad[]) {{ .name = NULL}},
456
457 .outputs = (AVFilterPad[]) {{ .name = "default",
458 .type = CODEC_TYPE_VIDEO,
459 .request_frame = color_request_frame,
460 .config_props = color_config_props },
461 { .name = NULL}},
462};
463
464#endif /* CONFIG_COLOR_FILTER */