Libav 0.7.1
libavfilter/vsrc_color.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010 Stefano Sabatini
00003  *
00004  * This file is part of Libav.
00005  *
00006  * Libav is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * Libav is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with Libav; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 
00021 #include "avfilter.h"
00022 #include "libavutil/pixdesc.h"
00023 #include "libavutil/colorspace.h"
00024 #include "libavutil/imgutils.h"
00025 #include "libavutil/parseutils.h"
00026 #include "drawutils.h"
00027 
00028 typedef struct {
00029     int w, h;
00030     uint8_t color[4];
00031     AVRational time_base;
00032     uint8_t *line[4];
00033     int      line_step[4];
00034     int hsub, vsub;         
00035     uint64_t pts;
00036 } ColorContext;
00037 
00038 static av_cold int color_init(AVFilterContext *ctx, const char *args, void *opaque)
00039 {
00040     ColorContext *color = ctx->priv;
00041     char color_string[128] = "black";
00042     char frame_size  [128] = "320x240";
00043     char frame_rate  [128] = "25";
00044     AVRational frame_rate_q;
00045     int ret;
00046 
00047     if (args)
00048         sscanf(args, "%127[^:]:%127[^:]:%127s", color_string, frame_size, frame_rate);
00049 
00050     if (av_parse_video_size(&color->w, &color->h, frame_size) < 0) {
00051         av_log(ctx, AV_LOG_ERROR, "Invalid frame size: %s\n", frame_size);
00052         return AVERROR(EINVAL);
00053     }
00054 
00055     if (av_parse_video_rate(&frame_rate_q, frame_rate) < 0 ||
00056         frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
00057         av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: %s\n", frame_rate);
00058         return AVERROR(EINVAL);
00059     }
00060     color->time_base.num = frame_rate_q.den;
00061     color->time_base.den = frame_rate_q.num;
00062 
00063     if ((ret = av_parse_color(color->color, color_string, -1, ctx)) < 0)
00064         return ret;
00065 
00066     return 0;
00067 }
00068 
00069 static av_cold void color_uninit(AVFilterContext *ctx)
00070 {
00071     ColorContext *color = ctx->priv;
00072     int i;
00073 
00074     for (i = 0; i < 4; i++) {
00075         av_freep(&color->line[i]);
00076         color->line_step[i] = 0;
00077     }
00078 }
00079 
00080 static int query_formats(AVFilterContext *ctx)
00081 {
00082     static const enum PixelFormat pix_fmts[] = {
00083         PIX_FMT_ARGB,         PIX_FMT_RGBA,
00084         PIX_FMT_ABGR,         PIX_FMT_BGRA,
00085         PIX_FMT_RGB24,        PIX_FMT_BGR24,
00086 
00087         PIX_FMT_YUV444P,      PIX_FMT_YUV422P,
00088         PIX_FMT_YUV420P,      PIX_FMT_YUV411P,
00089         PIX_FMT_YUV410P,      PIX_FMT_YUV440P,
00090         PIX_FMT_YUVJ444P,     PIX_FMT_YUVJ422P,
00091         PIX_FMT_YUVJ420P,     PIX_FMT_YUVJ440P,
00092         PIX_FMT_YUVA420P,
00093 
00094         PIX_FMT_NONE
00095     };
00096 
00097     avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
00098     return 0;
00099 }
00100 
00101 static int color_config_props(AVFilterLink *inlink)
00102 {
00103     AVFilterContext *ctx = inlink->src;
00104     ColorContext *color = ctx->priv;
00105     uint8_t rgba_color[4];
00106     int is_packed_rgba;
00107     const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
00108 
00109     color->hsub = pix_desc->log2_chroma_w;
00110     color->vsub = pix_desc->log2_chroma_h;
00111 
00112     color->w &= ~((1 << color->hsub) - 1);
00113     color->h &= ~((1 << color->vsub) - 1);
00114     if (av_image_check_size(color->w, color->h, 0, ctx) < 0)
00115         return AVERROR(EINVAL);
00116 
00117     memcpy(rgba_color, color->color, sizeof(rgba_color));
00118     ff_fill_line_with_color(color->line, color->line_step, color->w, color->color,
00119                             inlink->format, rgba_color, &is_packed_rgba, NULL);
00120 
00121     av_log(ctx, AV_LOG_INFO, "w:%d h:%d r:%d/%d color:0x%02x%02x%02x%02x[%s]\n",
00122            color->w, color->h, color->time_base.den, color->time_base.num,
00123            color->color[0], color->color[1], color->color[2], color->color[3],
00124            is_packed_rgba ? "rgba" : "yuva");
00125     inlink->w = color->w;
00126     inlink->h = color->h;
00127 
00128     return 0;
00129 }
00130 
00131 static int color_request_frame(AVFilterLink *link)
00132 {
00133     ColorContext *color = link->src->priv;
00134     AVFilterBufferRef *picref = avfilter_get_video_buffer(link, AV_PERM_WRITE, color->w, color->h);
00135     picref->video->pixel_aspect = (AVRational) {1, 1};
00136     picref->pts                 = av_rescale_q(color->pts++, color->time_base, AV_TIME_BASE_Q);
00137     picref->pos                 = 0;
00138 
00139     avfilter_start_frame(link, avfilter_ref_buffer(picref, ~0));
00140     ff_draw_rectangle(picref->data, picref->linesize,
00141                       color->line, color->line_step, color->hsub, color->vsub,
00142                       0, 0, color->w, color->h);
00143     avfilter_draw_slice(link, 0, color->h, 1);
00144     avfilter_end_frame(link);
00145     avfilter_unref_buffer(picref);
00146 
00147     return 0;
00148 }
00149 
00150 AVFilter avfilter_vsrc_color = {
00151     .name        = "color",
00152     .description = NULL_IF_CONFIG_SMALL("Provide an uniformly colored input, syntax is: [color[:size[:rate]]]"),
00153 
00154     .priv_size = sizeof(ColorContext),
00155     .init      = color_init,
00156     .uninit    = color_uninit,
00157 
00158     .query_formats = query_formats,
00159 
00160     .inputs    = (AVFilterPad[]) {{ .name = NULL}},
00161 
00162     .outputs   = (AVFilterPad[]) {{ .name            = "default",
00163                                     .type            = AVMEDIA_TYPE_VIDEO,
00164                                     .request_frame   = color_request_frame,
00165                                     .config_props    = color_config_props },
00166                                   { .name = NULL}},
00167 };