2 * Copyright (c) 2010 Stefano Sabatini
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
27 #include "libavutil/pixdesc.h"
28 #include "libavutil/colorspace.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/mathematics.h"
31 #include "libavutil/parseutils.h"
32 #include "drawutils.h"
36 uint8_t color_rgba[4];
43 static av_cold int color_init(AVFilterContext *ctx, const char *args, void *opaque)
45 ColorContext *color = ctx->priv;
46 char color_string[128] = "black";
47 char frame_size [128] = "320x240";
48 char frame_rate [128] = "25";
49 AVRational frame_rate_q;
53 sscanf(args, "%127[^:]:%127[^:]:%127s", color_string, frame_size, frame_rate);
55 if (av_parse_video_size(&color->w, &color->h, frame_size) < 0) {
56 av_log(ctx, AV_LOG_ERROR, "Invalid frame size: %s\n", frame_size);
57 return AVERROR(EINVAL);
60 if (av_parse_video_rate(&frame_rate_q, frame_rate) < 0 ||
61 frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
62 av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: %s\n", frame_rate);
63 return AVERROR(EINVAL);
65 color->time_base.num = frame_rate_q.den;
66 color->time_base.den = frame_rate_q.num;
68 if ((ret = av_parse_color(color->color_rgba, color_string, -1, ctx)) < 0)
74 static int query_formats(AVFilterContext *ctx)
76 avfilter_set_common_pixel_formats(ctx, ff_draw_supported_pixel_formats(0));
80 static int color_config_props(AVFilterLink *inlink)
82 AVFilterContext *ctx = inlink->src;
83 ColorContext *color = ctx->priv;
85 ff_draw_init(&color->draw, inlink->format, 0);
86 ff_draw_color(&color->draw, &color->color, color->color_rgba);
88 color->w = ff_draw_round_to_sub(&color->draw, 0, -1, color->w);
89 color->h = ff_draw_round_to_sub(&color->draw, 1, -1, color->h);
90 if (av_image_check_size(color->w, color->h, 0, ctx) < 0)
91 return AVERROR(EINVAL);
93 av_log(ctx, AV_LOG_INFO, "w:%d h:%d r:%d/%d color:0x%02x%02x%02x%02x\n",
94 color->w, color->h, color->time_base.den, color->time_base.num,
95 color->color_rgba[0], color->color_rgba[1], color->color_rgba[2], color->color_rgba[3]);
98 inlink->time_base = color->time_base;
103 static int color_request_frame(AVFilterLink *link)
105 ColorContext *color = link->src->priv;
106 AVFilterBufferRef *picref = avfilter_get_video_buffer(link, AV_PERM_WRITE, color->w, color->h);
107 picref->video->sample_aspect_ratio = (AVRational) {1, 1};
108 picref->pts = color->pts++;
111 avfilter_start_frame(link, avfilter_ref_buffer(picref, ~0));
112 ff_fill_rectangle(&color->draw, &color->color, picref->data, picref->linesize,
113 0, 0, color->w, color->h);
114 avfilter_draw_slice(link, 0, color->h, 1);
115 avfilter_end_frame(link);
116 avfilter_unref_buffer(picref);
121 AVFilter avfilter_vsrc_color = {
123 .description = NULL_IF_CONFIG_SMALL("Provide an uniformly colored input, syntax is: [color[:size[:rate]]]."),
125 .priv_size = sizeof(ColorContext),
128 .query_formats = query_formats,
130 .inputs = (const AVFilterPad[]) {{ .name = NULL}},
132 .outputs = (const AVFilterPad[]) {{ .name = "default",
133 .type = AVMEDIA_TYPE_VIDEO,
134 .request_frame = color_request_frame,
135 .config_props = color_config_props },