]> git.sesse.net Git - ffmpeg/blob - libavfilter/vsrc_color.c
swr: MMX2 & SSSE3 int16 resample core
[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 "formats.h"
28 #include "internal.h"
29 #include "video.h"
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"
37
38 typedef struct {
39     const AVClass *class;
40     int w, h;
41     char *rate_str;
42     char *color_str;
43     uint8_t color_rgba[4];
44     AVRational time_base;
45     uint64_t pts;
46     FFDrawContext draw;
47     FFDrawColor color;
48 } ColorContext;
49
50 #define OFFSET(x) offsetof(ColorContext, x)
51
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 },
59     { NULL },
60 };
61
62 AVFILTER_DEFINE_CLASS(color);
63
64 static av_cold int color_init(AVFilterContext *ctx, const char *args)
65 {
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;
72     int ret = 0;
73
74     color->class = &color_class;
75
76     if (args) {
77         colon = strchr(args, ':');
78         equal = strchr(args, '=');
79     }
80
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);
85             goto end;
86         }
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);
90             goto end;
91         }
92         if (av_parse_color(color->color_rgba, color->color_str, -1, ctx) < 0) {
93             ret = AVERROR(EINVAL);
94             goto end;
95         }
96     } else {
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);
102         }
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);
106         }
107         if (av_parse_color(color->color_rgba, color_string, -1, ctx) < 0)
108             return AVERROR(EINVAL);
109     }
110
111     color->time_base.num = frame_rate_q.den;
112     color->time_base.den = frame_rate_q.num;
113
114 end:
115     av_opt_free(color);
116     return ret;
117 }
118
119 static int query_formats(AVFilterContext *ctx)
120 {
121     ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
122     return 0;
123 }
124
125 static int color_config_props(AVFilterLink *inlink)
126 {
127     AVFilterContext *ctx = inlink->src;
128     ColorContext *color = ctx->priv;
129
130     ff_draw_init(&color->draw, inlink->format, 0);
131     ff_draw_color(&color->draw, &color->color, color->color_rgba);
132
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);
137
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;
144
145     return 0;
146 }
147
148 static int color_request_frame(AVFilterLink *link)
149 {
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++;
154     picref->pos = -1;
155
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);
160     ff_end_frame(link);
161     avfilter_unref_buffer(picref);
162
163     return 0;
164 }
165
166 AVFilter avfilter_vsrc_color = {
167     .name        = "color",
168     .description = NULL_IF_CONFIG_SMALL("Provide an uniformly colored input."),
169
170     .priv_size = sizeof(ColorContext),
171     .init      = color_init,
172
173     .query_formats = query_formats,
174
175     .inputs    = (const AVFilterPad[]) {{ .name = NULL}},
176
177     .outputs   = (const AVFilterPad[]) {{ .name      = "default",
178                                     .type            = AVMEDIA_TYPE_VIDEO,
179                                     .request_frame   = color_request_frame,
180                                     .config_props    = color_config_props },
181                                   { .name = NULL}},
182 };