• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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 
00026 #include "avfilter.h"
00027 #include "libavutil/pixdesc.h"
00028 #include "libavutil/colorspace.h"
00029 #include "libavutil/imgutils.h"
00030 #include "libavutil/mathematics.h"
00031 #include "libavutil/parseutils.h"
00032 #include "drawutils.h"
00033 
00034 typedef struct {
00035     int w, h;
00036     uint8_t color[4];
00037     AVRational time_base;
00038     uint8_t *line[4];
00039     int      line_step[4];
00040     int hsub, vsub;         
00041     uint64_t pts;
00042 } ColorContext;
00043 
00044 static av_cold int color_init(AVFilterContext *ctx, const char *args, void *opaque)
00045 {
00046     ColorContext *color = ctx->priv;
00047     char color_string[128] = "black";
00048     char frame_size  [128] = "320x240";
00049     char frame_rate  [128] = "25";
00050     AVRational frame_rate_q;
00051     int ret;
00052 
00053     if (args)
00054         sscanf(args, "%127[^:]:%127[^:]:%127s", color_string, frame_size, frame_rate);
00055 
00056     if (av_parse_video_size(&color->w, &color->h, frame_size) < 0) {
00057         av_log(ctx, AV_LOG_ERROR, "Invalid frame size: %s\n", frame_size);
00058         return AVERROR(EINVAL);
00059     }
00060 
00061     if (av_parse_video_rate(&frame_rate_q, frame_rate) < 0 ||
00062         frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
00063         av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: %s\n", frame_rate);
00064         return AVERROR(EINVAL);
00065     }
00066     color->time_base.num = frame_rate_q.den;
00067     color->time_base.den = frame_rate_q.num;
00068 
00069     if ((ret = av_parse_color(color->color, color_string, -1, ctx)) < 0)
00070         return ret;
00071 
00072     return 0;
00073 }
00074 
00075 static av_cold void color_uninit(AVFilterContext *ctx)
00076 {
00077     ColorContext *color = ctx->priv;
00078     int i;
00079 
00080     for (i = 0; i < 4; i++) {
00081         av_freep(&color->line[i]);
00082         color->line_step[i] = 0;
00083     }
00084 }
00085 
00086 static int query_formats(AVFilterContext *ctx)
00087 {
00088     static const enum PixelFormat pix_fmts[] = {
00089         PIX_FMT_ARGB,         PIX_FMT_RGBA,
00090         PIX_FMT_ABGR,         PIX_FMT_BGRA,
00091         PIX_FMT_RGB24,        PIX_FMT_BGR24,
00092 
00093         PIX_FMT_YUV444P,      PIX_FMT_YUV422P,
00094         PIX_FMT_YUV420P,      PIX_FMT_YUV411P,
00095         PIX_FMT_YUV410P,      PIX_FMT_YUV440P,
00096         PIX_FMT_YUVJ444P,     PIX_FMT_YUVJ422P,
00097         PIX_FMT_YUVJ420P,     PIX_FMT_YUVJ440P,
00098         PIX_FMT_YUVA420P,
00099 
00100         PIX_FMT_NONE
00101     };
00102 
00103     avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
00104     return 0;
00105 }
00106 
00107 static int color_config_props(AVFilterLink *inlink)
00108 {
00109     AVFilterContext *ctx = inlink->src;
00110     ColorContext *color = ctx->priv;
00111     uint8_t rgba_color[4];
00112     int is_packed_rgba;
00113     const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
00114 
00115     color->hsub = pix_desc->log2_chroma_w;
00116     color->vsub = pix_desc->log2_chroma_h;
00117 
00118     color->w &= ~((1 << color->hsub) - 1);
00119     color->h &= ~((1 << color->vsub) - 1);
00120     if (av_image_check_size(color->w, color->h, 0, ctx) < 0)
00121         return AVERROR(EINVAL);
00122 
00123     memcpy(rgba_color, color->color, sizeof(rgba_color));
00124     ff_fill_line_with_color(color->line, color->line_step, color->w, color->color,
00125                             inlink->format, rgba_color, &is_packed_rgba, NULL);
00126 
00127     av_log(ctx, AV_LOG_INFO, "w:%d h:%d r:%d/%d color:0x%02x%02x%02x%02x[%s]\n",
00128            color->w, color->h, color->time_base.den, color->time_base.num,
00129            color->color[0], color->color[1], color->color[2], color->color[3],
00130            is_packed_rgba ? "rgba" : "yuva");
00131     inlink->w = color->w;
00132     inlink->h = color->h;
00133     inlink->time_base = color->time_base;
00134 
00135     return 0;
00136 }
00137 
00138 static int color_request_frame(AVFilterLink *link)
00139 {
00140     ColorContext *color = link->src->priv;
00141     AVFilterBufferRef *picref = avfilter_get_video_buffer(link, AV_PERM_WRITE, color->w, color->h);
00142     picref->video->pixel_aspect = (AVRational) {1, 1};
00143     picref->pts                 = color->pts++;
00144     picref->pos                 = -1;
00145 
00146     avfilter_start_frame(link, avfilter_ref_buffer(picref, ~0));
00147     ff_draw_rectangle(picref->data, picref->linesize,
00148                       color->line, color->line_step, color->hsub, color->vsub,
00149                       0, 0, color->w, color->h);
00150     avfilter_draw_slice(link, 0, color->h, 1);
00151     avfilter_end_frame(link);
00152     avfilter_unref_buffer(picref);
00153 
00154     return 0;
00155 }
00156 
00157 AVFilter avfilter_vsrc_color = {
00158     .name        = "color",
00159     .description = NULL_IF_CONFIG_SMALL("Provide an uniformly colored input, syntax is: [color[:size[:rate]]]"),
00160 
00161     .priv_size = sizeof(ColorContext),
00162     .init      = color_init,
00163     .uninit    = color_uninit,
00164 
00165     .query_formats = query_formats,
00166 
00167     .inputs    = (AVFilterPad[]) {{ .name = NULL}},
00168 
00169     .outputs   = (AVFilterPad[]) {{ .name            = "default",
00170                                     .type            = AVMEDIA_TYPE_VIDEO,
00171                                     .request_frame   = color_request_frame,
00172                                     .config_props    = color_config_props },
00173                                   { .name = NULL}},
00174 };
Generated on Sat Mar 17 2012 12:57:52 for Libav by doxygen 1.7.1