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

Generated on Wed Apr 11 2012 07:31:36 for FFmpeg by  doxygen 1.7.1