]> git.sesse.net Git - ffmpeg/blob - libavfilter/vsrc_color.c
aacdec: fix "may be used uninitialized" warning
[ffmpeg] / libavfilter / vsrc_color.c
1 /*
2  * Copyright (c) 2010 Stefano Sabatini
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /**
22  * @file
23  * color source
24  */
25
26 #include "avfilter.h"
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"
33
34 typedef struct {
35     int w, h;
36     uint8_t color_rgba[4];
37     AVRational time_base;
38     uint64_t pts;
39     FFDrawContext draw;
40     FFDrawColor color;
41 } ColorContext;
42
43 static av_cold int color_init(AVFilterContext *ctx, const char *args, void *opaque)
44 {
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;
50     int ret;
51
52     if (args)
53         sscanf(args, "%127[^:]:%127[^:]:%127s", color_string, frame_size, frame_rate);
54
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);
58     }
59
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);
64     }
65     color->time_base.num = frame_rate_q.den;
66     color->time_base.den = frame_rate_q.num;
67
68     if ((ret = av_parse_color(color->color_rgba, color_string, -1, ctx)) < 0)
69         return ret;
70
71     return 0;
72 }
73
74 static int query_formats(AVFilterContext *ctx)
75 {
76     avfilter_set_common_pixel_formats(ctx, ff_draw_supported_pixel_formats(0));
77     return 0;
78 }
79
80 static int color_config_props(AVFilterLink *inlink)
81 {
82     AVFilterContext *ctx = inlink->src;
83     ColorContext *color = ctx->priv;
84
85     ff_draw_init(&color->draw, inlink->format, 0);
86     ff_draw_color(&color->draw, &color->color, color->color_rgba);
87
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);
92
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]);
96     inlink->w = color->w;
97     inlink->h = color->h;
98     inlink->time_base = color->time_base;
99
100     return 0;
101 }
102
103 static int color_request_frame(AVFilterLink *link)
104 {
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++;
109     picref->pos = -1;
110
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);
117
118     return 0;
119 }
120
121 AVFilter avfilter_vsrc_color = {
122     .name        = "color",
123     .description = NULL_IF_CONFIG_SMALL("Provide an uniformly colored input, syntax is: [color[:size[:rate]]]."),
124
125     .priv_size = sizeof(ColorContext),
126     .init      = color_init,
127
128     .query_formats = query_formats,
129
130     .inputs    = (const AVFilterPad[]) {{ .name = NULL}},
131
132     .outputs   = (const AVFilterPad[]) {{ .name      = "default",
133                                     .type            = AVMEDIA_TYPE_VIDEO,
134                                     .request_frame   = color_request_frame,
135                                     .config_props    = color_config_props },
136                                   { .name = NULL}},
137 };