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
30 #include "libavutil/pixdesc.h"
31 #include "libavutil/colorspace.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/mathematics.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/parseutils.h"
36 #include "drawutils.h"
43 uint8_t color_rgba[4];
50 #define OFFSET(x) offsetof(ColorContext, x)
52 static const AVOption color_options[]= {
53 { "size", "set frame size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0 },
54 { "s", "set frame size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0 },
55 { "rate", "set frame rate", OFFSET(rate_str), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0 },
56 { "r", "set frame rate", OFFSET(rate_str), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0 },
57 { "color", "set color", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str = "black"}, CHAR_MIN, CHAR_MAX },
58 { "c", "set color", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str = "black"}, CHAR_MIN, CHAR_MAX },
62 AVFILTER_DEFINE_CLASS(color);
64 static av_cold int color_init(AVFilterContext *ctx, const char *args)
66 ColorContext *color = ctx->priv;
67 char color_string[128] = "black";
68 char frame_size [128] = "320x240";
69 char frame_rate [128] = "25";
70 AVRational frame_rate_q;
71 char *colon = 0, *equal = 0;
74 color->class = &color_class;
77 colon = strchr(args, ':');
78 equal = strchr(args, '=');
81 if (!args || (equal && (!colon || equal < colon))) {
82 av_opt_set_defaults(color);
83 if ((ret = av_set_options_string(color, args, "=", ":")) < 0) {
84 av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
87 if (av_parse_video_rate(&frame_rate_q, color->rate_str) < 0) {
88 av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: %s\n", color->rate_str);
89 ret = AVERROR(EINVAL);
92 if (av_parse_color(color->color_rgba, color->color_str, -1, ctx) < 0) {
93 ret = AVERROR(EINVAL);
97 av_log(ctx, AV_LOG_WARNING, "Flat options syntax is deprecated, use key=value pairs.\n");
98 sscanf(args, "%127[^:]:%127[^:]:%127s", color_string, frame_size, frame_rate);
99 if (av_parse_video_size(&color->w, &color->h, frame_size) < 0) {
100 av_log(ctx, AV_LOG_ERROR, "Invalid frame size: %s\n", frame_size);
101 return AVERROR(EINVAL);
103 if (av_parse_video_rate(&frame_rate_q, frame_rate) < 0) {
104 av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: %s\n", frame_rate);
105 return AVERROR(EINVAL);
107 if (av_parse_color(color->color_rgba, color_string, -1, ctx) < 0)
108 return AVERROR(EINVAL);
111 color->time_base.num = frame_rate_q.den;
112 color->time_base.den = frame_rate_q.num;
119 static int query_formats(AVFilterContext *ctx)
121 ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
125 static int color_config_props(AVFilterLink *inlink)
127 AVFilterContext *ctx = inlink->src;
128 ColorContext *color = ctx->priv;
130 ff_draw_init(&color->draw, inlink->format, 0);
131 ff_draw_color(&color->draw, &color->color, color->color_rgba);
133 color->w = ff_draw_round_to_sub(&color->draw, 0, -1, color->w);
134 color->h = ff_draw_round_to_sub(&color->draw, 1, -1, color->h);
135 if (av_image_check_size(color->w, color->h, 0, ctx) < 0)
136 return AVERROR(EINVAL);
138 av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d r:%d/%d color:0x%02x%02x%02x%02x\n",
139 color->w, color->h, color->time_base.den, color->time_base.num,
140 color->color_rgba[0], color->color_rgba[1], color->color_rgba[2], color->color_rgba[3]);
141 inlink->w = color->w;
142 inlink->h = color->h;
143 inlink->time_base = color->time_base;
148 static int color_request_frame(AVFilterLink *link)
150 ColorContext *color = link->src->priv;
151 AVFilterBufferRef *picref = ff_get_video_buffer(link, AV_PERM_WRITE, color->w, color->h);
152 picref->video->sample_aspect_ratio = (AVRational) {1, 1};
153 picref->pts = color->pts++;
156 ff_start_frame(link, avfilter_ref_buffer(picref, ~0));
157 ff_fill_rectangle(&color->draw, &color->color, picref->data, picref->linesize,
158 0, 0, color->w, color->h);
159 ff_draw_slice(link, 0, color->h, 1);
161 avfilter_unref_buffer(picref);
166 AVFilter avfilter_vsrc_color = {
168 .description = NULL_IF_CONFIG_SMALL("Provide an uniformly colored input."),
170 .priv_size = sizeof(ColorContext),
173 .query_formats = query_formats,
175 .inputs = (const AVFilterPad[]) {{ .name = NULL}},
177 .outputs = (const AVFilterPad[]) {{ .name = "default",
178 .type = AVMEDIA_TYPE_VIDEO,
179 .request_frame = color_request_frame,
180 .config_props = color_config_props },