2 * Copyright (c) 2008 vmrsss
3 * Copyright (c) 2009 Stefano Sabatini
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
24 * video padding filter and color source
28 #include "libavutil/pixdesc.h"
29 #include "libavutil/colorspace.h"
30 #include "libavutil/avassert.h"
31 #include "libavcore/imgutils.h"
32 #include "libavcore/parseutils.h"
34 enum { RED = 0, GREEN, BLUE, ALPHA };
36 static int fill_line_with_color(uint8_t *line[4], int line_step[4], int w, uint8_t color[4],
37 enum PixelFormat pix_fmt, uint8_t rgba_color[4], int *is_packed_rgba)
39 uint8_t rgba_map[4] = {0};
41 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[pix_fmt];
42 int hsub = pix_desc->log2_chroma_w;
46 case PIX_FMT_ARGB: rgba_map[ALPHA] = 0; rgba_map[RED ] = 1; rgba_map[GREEN] = 2; rgba_map[BLUE ] = 3; break;
47 case PIX_FMT_ABGR: rgba_map[ALPHA] = 0; rgba_map[BLUE ] = 1; rgba_map[GREEN] = 2; rgba_map[RED ] = 3; break;
49 case PIX_FMT_RGB24: rgba_map[RED ] = 0; rgba_map[GREEN] = 1; rgba_map[BLUE ] = 2; rgba_map[ALPHA] = 3; break;
51 case PIX_FMT_BGR24: rgba_map[BLUE ] = 0; rgba_map[GREEN] = 1; rgba_map[RED ] = 2; rgba_map[ALPHA] = 3; break;
56 if (*is_packed_rgba) {
57 line_step[0] = (av_get_bits_per_pixel(pix_desc))>>3;
58 for (i = 0; i < 4; i++)
59 color[rgba_map[i]] = rgba_color[i];
61 line[0] = av_malloc(w * line_step[0]);
62 for (i = 0; i < w; i++)
63 memcpy(line[0] + i * line_step[0], color, line_step[0]);
67 color[RED ] = RGB_TO_Y_CCIR(rgba_color[0], rgba_color[1], rgba_color[2]);
68 color[GREEN] = RGB_TO_U_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
69 color[BLUE ] = RGB_TO_V_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
70 color[ALPHA] = rgba_color[3];
72 for (plane = 0; plane < 4; plane++) {
74 int hsub1 = (plane == 1 || plane == 2) ? hsub : 0;
77 line_size = (w >> hsub1) * line_step[plane];
78 line[plane] = av_malloc(line_size);
79 memset(line[plane], color[plane], line_size);
86 static void draw_rectangle(AVFilterBufferRef *outpic, uint8_t *line[4], int line_step[4],
87 int hsub, int vsub, int x, int y, int w, int h)
92 for (plane = 0; plane < 4 && outpic->data[plane]; plane++) {
93 int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
94 int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
96 p = outpic->data[plane] + (y >> vsub1) * outpic->linesize[plane];
97 for (i = 0; i < (h >> vsub1); i++) {
98 memcpy(p + (x >> hsub1) * line_step[plane], line[plane], (w >> hsub1) * line_step[plane]);
99 p += outpic->linesize[plane];
104 static void copy_rectangle(AVFilterBufferRef *outpic,uint8_t *line[4], int line_step[4], int linesize[4],
105 int hsub, int vsub, int x, int y, int y2, int w, int h)
110 for (plane = 0; plane < 4 && outpic->data[plane]; plane++) {
111 int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
112 int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
114 p = outpic->data[plane] + (y >> vsub1) * outpic->linesize[plane];
115 for (i = 0; i < (h >> vsub1); i++) {
116 memcpy(p + (x >> hsub1) * line_step[plane], line[plane] + linesize[plane]*(i+(y2>>vsub1)), (w >> hsub1) * line_step[plane]);
117 p += outpic->linesize[plane];
122 static int query_formats(AVFilterContext *ctx)
124 static const enum PixelFormat pix_fmts[] = {
125 PIX_FMT_ARGB, PIX_FMT_RGBA,
126 PIX_FMT_ABGR, PIX_FMT_BGRA,
127 PIX_FMT_RGB24, PIX_FMT_BGR24,
129 PIX_FMT_YUV444P, PIX_FMT_YUV422P,
130 PIX_FMT_YUV420P, PIX_FMT_YUV411P,
131 PIX_FMT_YUV410P, PIX_FMT_YUV440P,
132 PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P,
133 PIX_FMT_YUVJ420P, PIX_FMT_YUVJ440P,
139 avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
143 #if CONFIG_PAD_FILTER
146 int w, h; ///< output dimensions, a value of 0 will result in the input size
147 int x, y; ///< offsets of the input area with respect to the padded area
148 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
150 uint8_t color[4]; ///< color expressed either in YUVA or RGBA colorspace for the padding area
153 int hsub, vsub; ///< chroma subsampling values
157 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
159 PadContext *pad = ctx->priv;
160 char color_string[128] = "black";
163 sscanf(args, "%d:%d:%d:%d:%s", &pad->w, &pad->h, &pad->x, &pad->y, color_string);
165 if (av_parse_color(pad->color, color_string, -1, ctx) < 0)
166 return AVERROR(EINVAL);
168 /* sanity check params */
169 if (pad->w < 0 || pad->h < 0) {
170 av_log(ctx, AV_LOG_ERROR, "Negative size values are not acceptable.\n");
171 return AVERROR(EINVAL);
177 static av_cold void uninit(AVFilterContext *ctx)
179 PadContext *pad = ctx->priv;
182 for (i = 0; i < 4; i++) {
183 av_freep(&pad->line[i]);
184 pad->line_step[i] = 0;
188 static int config_input(AVFilterLink *inlink)
190 AVFilterContext *ctx = inlink->dst;
191 PadContext *pad = ctx->priv;
192 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
193 uint8_t rgba_color[4];
196 pad->hsub = pix_desc->log2_chroma_w;
197 pad->vsub = pix_desc->log2_chroma_h;
204 pad->w &= ~((1 << pad->hsub) - 1);
205 pad->h &= ~((1 << pad->vsub) - 1);
206 pad->x &= ~((1 << pad->hsub) - 1);
207 pad->y &= ~((1 << pad->vsub) - 1);
209 pad->in_w = inlink->w & ~((1 << pad->hsub) - 1);
210 pad->in_h = inlink->h & ~((1 << pad->vsub) - 1);
212 memcpy(rgba_color, pad->color, sizeof(rgba_color));
213 fill_line_with_color(pad->line, pad->line_step, pad->w, pad->color,
214 inlink->format, rgba_color, &is_packed_rgba);
216 av_log(ctx, AV_LOG_INFO, "w:%d h:%d -> w:%d h:%d x:%d y:%d color:0x%02X%02X%02X%02X[%s]\n",
217 inlink->w, inlink->h, pad->w, pad->h, pad->x, pad->y,
218 pad->color[0], pad->color[1], pad->color[2], pad->color[3],
219 is_packed_rgba ? "rgba" : "yuva");
221 if (pad->x < 0 || pad->y < 0 ||
222 pad->w <= 0 || pad->h <= 0 ||
223 (unsigned)pad->x + (unsigned)inlink->w > pad->w ||
224 (unsigned)pad->y + (unsigned)inlink->h > pad->h) {
225 av_log(ctx, AV_LOG_ERROR,
226 "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
227 pad->x, pad->y, pad->x + inlink->w, pad->y + inlink->h, pad->w, pad->h);
228 return AVERROR(EINVAL);
234 static int config_output(AVFilterLink *outlink)
236 PadContext *pad = outlink->src->priv;
243 static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
245 PadContext *pad = inlink->dst->priv;
247 AVFilterBufferRef *picref = avfilter_get_video_buffer(inlink->dst->outputs[0], perms,
248 w + (pad->w - pad->in_w),
249 h + (pad->h - pad->in_h));
252 picref->video->w = w;
253 picref->video->h = h;
255 for (plane = 0; plane < 4 && picref->data[plane]; plane++) {
256 int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
257 int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0;
259 picref->data[plane] += (pad->x >> hsub) * pad->line_step[plane] +
260 (pad->y >> vsub) * picref->linesize[plane];
266 static int does_clip(PadContext *pad, AVFilterBufferRef *outpicref, int plane, int hsub, int vsub, int x, int y)
268 int64_t x_in_buf, y_in_buf;
270 x_in_buf = outpicref->data[plane] - outpicref->buf->data[plane]
271 + (x >> hsub) * pad ->line_step[plane]
272 + (y >> vsub) * outpicref->linesize [plane];
274 if(x_in_buf < 0 || x_in_buf % pad->line_step[plane])
276 x_in_buf /= pad->line_step[plane];
278 av_assert0(outpicref->buf->linesize[plane]>0); //while reference can use negative linesize the main buffer should not
280 y_in_buf = x_in_buf / outpicref->buf->linesize[plane];
281 x_in_buf %= outpicref->buf->linesize[plane];
283 if( y_in_buf<<vsub >= outpicref->buf->h
284 || x_in_buf<<hsub >= outpicref->buf->w)
289 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
291 PadContext *pad = inlink->dst->priv;
292 AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
295 for (plane = 0; plane < 4 && outpicref->data[plane]; plane++) {
296 int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
297 int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0;
299 av_assert0(outpicref->buf->w>0 && outpicref->buf->h>0);
301 if(outpicref->format != outpicref->buf->format) //unsupported currently
304 outpicref->data[plane] -= (pad->x >> hsub) * pad ->line_step[plane]
305 + (pad->y >> vsub) * outpicref->linesize [plane];
307 if( does_clip(pad, outpicref, plane, hsub, vsub, 0, 0)
308 || does_clip(pad, outpicref, plane, hsub, vsub, 0, pad->h-1)
309 || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, 0)
310 || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, pad->h-1)
314 pad->needs_copy= plane < 4 && outpicref->data[plane];
316 av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
317 avfilter_unref_buffer(outpicref);
318 outpicref = avfilter_get_video_buffer(inlink->dst->outputs[0], AV_PERM_WRITE | AV_PERM_NEG_LINESIZES,
319 FFMAX(inlink->w, pad->w),
320 FFMAX(inlink->h, pad->h));
321 avfilter_copy_buffer_ref_props(outpicref, inpicref);
324 inlink->dst->outputs[0]->out_buf = outpicref;
326 outpicref->video->w = pad->w;
327 outpicref->video->h = pad->h;
329 avfilter_start_frame(inlink->dst->outputs[0], outpicref);
332 static void end_frame(AVFilterLink *link)
334 avfilter_end_frame(link->dst->outputs[0]);
335 avfilter_unref_buffer(link->cur_buf);
338 static void draw_send_bar_slice(AVFilterLink *link, int y, int h, int slice_dir, int before_slice)
340 PadContext *pad = link->dst->priv;
341 int bar_y, bar_h = 0;
343 if (slice_dir * before_slice == 1 && y == pad->y) {
347 } else if (slice_dir * before_slice == -1 && (y + h) == (pad->y + pad->in_h)) {
349 bar_y = pad->y + pad->in_h;
350 bar_h = pad->h - pad->in_h - pad->y;
354 draw_rectangle(link->dst->outputs[0]->out_buf,
355 pad->line, pad->line_step, pad->hsub, pad->vsub,
356 0, bar_y, pad->w, bar_h);
357 avfilter_draw_slice(link->dst->outputs[0], bar_y, bar_h, slice_dir);
361 static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
363 PadContext *pad = link->dst->priv;
364 AVFilterBufferRef *outpic = link->dst->outputs[0]->out_buf;
365 AVFilterBufferRef *inpic = link->cur_buf;
369 y &= ~((1 << pad->vsub) - 1);
370 h &= ~((1 << pad->vsub) - 1);
374 draw_send_bar_slice(link, y, h, slice_dir, 1);
377 draw_rectangle(outpic, pad->line, pad->line_step, pad->hsub, pad->vsub,
381 copy_rectangle(outpic,
382 inpic->data, pad->line_step, inpic->linesize, pad->hsub, pad->vsub,
383 pad->x, y, y-pad->y, inpic->video->w, h);
387 draw_rectangle(outpic, pad->line, pad->line_step, pad->hsub, pad->vsub,
388 pad->x + pad->in_w, y, pad->w - pad->x - pad->in_w, h);
389 avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
391 draw_send_bar_slice(link, y, h, slice_dir, -1);
394 AVFilter avfilter_vf_pad = {
396 .description = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
398 .priv_size = sizeof(PadContext),
401 .query_formats = query_formats,
403 .inputs = (AVFilterPad[]) {{ .name = "default",
404 .type = AVMEDIA_TYPE_VIDEO,
405 .config_props = config_input,
406 .get_video_buffer = get_video_buffer,
407 .start_frame = start_frame,
408 .draw_slice = draw_slice,
409 .end_frame = end_frame, },
412 .outputs = (AVFilterPad[]) {{ .name = "default",
413 .type = AVMEDIA_TYPE_VIDEO,
414 .config_props = config_output, },
418 #endif /* CONFIG_PAD_FILTER */
420 #if CONFIG_COLOR_FILTER
425 AVRational time_base;
428 int hsub, vsub; ///< chroma subsampling values
432 static av_cold int color_init(AVFilterContext *ctx, const char *args, void *opaque)
434 ColorContext *color = ctx->priv;
435 char color_string[128] = "black";
436 char frame_size [128] = "320x240";
437 char frame_rate [128] = "25";
438 AVRational frame_rate_q;
442 sscanf(args, "%127[^:]:%127[^:]:%127s", color_string, frame_size, frame_rate);
444 if (av_parse_video_size(&color->w, &color->h, frame_size) < 0) {
445 av_log(ctx, AV_LOG_ERROR, "Invalid frame size: %s\n", frame_size);
446 return AVERROR(EINVAL);
449 if (av_parse_video_rate(&frame_rate_q, frame_rate) < 0 ||
450 frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
451 av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: %s\n", frame_rate);
452 return AVERROR(EINVAL);
454 color->time_base.num = frame_rate_q.den;
455 color->time_base.den = frame_rate_q.num;
457 if ((ret = av_parse_color(color->color, color_string, -1, ctx)) < 0)
463 static av_cold void color_uninit(AVFilterContext *ctx)
465 ColorContext *color = ctx->priv;
468 for (i = 0; i < 4; i++) {
469 av_freep(&color->line[i]);
470 color->line_step[i] = 0;
474 static int color_config_props(AVFilterLink *inlink)
476 AVFilterContext *ctx = inlink->src;
477 ColorContext *color = ctx->priv;
478 uint8_t rgba_color[4];
480 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
482 color->hsub = pix_desc->log2_chroma_w;
483 color->vsub = pix_desc->log2_chroma_h;
485 color->w &= ~((1 << color->hsub) - 1);
486 color->h &= ~((1 << color->vsub) - 1);
487 if (av_image_check_size(color->w, color->h, 0, ctx) < 0)
488 return AVERROR(EINVAL);
490 memcpy(rgba_color, color->color, sizeof(rgba_color));
491 fill_line_with_color(color->line, color->line_step, color->w, color->color,
492 inlink->format, rgba_color, &is_packed_rgba);
494 av_log(ctx, AV_LOG_INFO, "w:%d h:%d r:%d/%d color:0x%02x%02x%02x%02x[%s]\n",
495 color->w, color->h, color->time_base.den, color->time_base.num,
496 color->color[0], color->color[1], color->color[2], color->color[3],
497 is_packed_rgba ? "rgba" : "yuva");
498 inlink->w = color->w;
499 inlink->h = color->h;
504 static int color_request_frame(AVFilterLink *link)
506 ColorContext *color = link->src->priv;
507 AVFilterBufferRef *picref = avfilter_get_video_buffer(link, AV_PERM_WRITE, color->w, color->h);
508 picref->video->pixel_aspect = (AVRational) {1, 1};
509 picref->pts = av_rescale_q(color->pts++, color->time_base, AV_TIME_BASE_Q);
512 avfilter_start_frame(link, avfilter_ref_buffer(picref, ~0));
513 draw_rectangle(picref,
514 color->line, color->line_step, color->hsub, color->vsub,
515 0, 0, color->w, color->h);
516 avfilter_draw_slice(link, 0, color->h, 1);
517 avfilter_end_frame(link);
518 avfilter_unref_buffer(picref);
523 AVFilter avfilter_vsrc_color = {
525 .description = NULL_IF_CONFIG_SMALL("Provide an uniformly colored input, syntax is: [color[:size[:rate]]]"),
527 .priv_size = sizeof(ColorContext),
529 .uninit = color_uninit,
531 .query_formats = query_formats,
533 .inputs = (AVFilterPad[]) {{ .name = NULL}},
535 .outputs = (AVFilterPad[]) {{ .name = "default",
536 .type = AVMEDIA_TYPE_VIDEO,
537 .request_frame = color_request_frame,
538 .config_props = color_config_props },
542 #endif /* CONFIG_COLOR_FILTER */