00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00027 #include "libavutil/colorspace.h"
00028 #include "libavutil/common.h"
00029 #include "libavutil/pixdesc.h"
00030 #include "libavutil/parseutils.h"
00031 #include "avfilter.h"
00032 #include "formats.h"
00033 #include "internal.h"
00034 #include "video.h"
00035
00036 enum { Y, U, V, A };
00037
00038 typedef struct {
00039 int x, y, w, h;
00040 unsigned char yuv_color[4];
00041 int vsub, hsub;
00042 } DrawBoxContext;
00043
00044 static av_cold int init(AVFilterContext *ctx, const char *args)
00045 {
00046 DrawBoxContext *drawbox= ctx->priv;
00047 char color_str[1024] = "black";
00048 uint8_t rgba_color[4];
00049
00050 drawbox->x = drawbox->y = drawbox->w = drawbox->h = 0;
00051
00052 if (args)
00053 sscanf(args, "%d:%d:%d:%d:%s",
00054 &drawbox->x, &drawbox->y, &drawbox->w, &drawbox->h, color_str);
00055
00056 if (av_parse_color(rgba_color, color_str, -1, ctx) < 0)
00057 return AVERROR(EINVAL);
00058
00059 drawbox->yuv_color[Y] = RGB_TO_Y_CCIR(rgba_color[0], rgba_color[1], rgba_color[2]);
00060 drawbox->yuv_color[U] = RGB_TO_U_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
00061 drawbox->yuv_color[V] = RGB_TO_V_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
00062 drawbox->yuv_color[A] = rgba_color[3];
00063
00064 return 0;
00065 }
00066
00067 static int query_formats(AVFilterContext *ctx)
00068 {
00069 enum PixelFormat pix_fmts[] = {
00070 PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV420P,
00071 PIX_FMT_YUV411P, PIX_FMT_YUV410P,
00072 PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ420P,
00073 PIX_FMT_YUV440P, PIX_FMT_YUVJ440P,
00074 PIX_FMT_NONE
00075 };
00076
00077 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
00078 return 0;
00079 }
00080
00081 static int config_input(AVFilterLink *inlink)
00082 {
00083 DrawBoxContext *drawbox = inlink->dst->priv;
00084
00085 drawbox->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
00086 drawbox->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
00087
00088 if (drawbox->w == 0) drawbox->w = inlink->w;
00089 if (drawbox->h == 0) drawbox->h = inlink->h;
00090
00091 av_log(inlink->dst, AV_LOG_VERBOSE, "x:%d y:%d w:%d h:%d color:0x%02X%02X%02X%02X\n",
00092 drawbox->x, drawbox->y, drawbox->w, drawbox->h,
00093 drawbox->yuv_color[Y], drawbox->yuv_color[U], drawbox->yuv_color[V], drawbox->yuv_color[A]);
00094
00095 return 0;
00096 }
00097
00098 static int draw_slice(AVFilterLink *inlink, int y0, int h, int slice_dir)
00099 {
00100 DrawBoxContext *drawbox = inlink->dst->priv;
00101 int plane, x, y, xb = drawbox->x, yb = drawbox->y;
00102 unsigned char *row[4];
00103 AVFilterBufferRef *picref = inlink->cur_buf;
00104
00105 for (y = FFMAX(yb, y0); y < (y0 + h) && y < (yb + drawbox->h); y++) {
00106 row[0] = picref->data[0] + y * picref->linesize[0];
00107
00108 for (plane = 1; plane < 3; plane++)
00109 row[plane] = picref->data[plane] +
00110 picref->linesize[plane] * (y >> drawbox->vsub);
00111
00112 for (x = FFMAX(xb, 0); x < (xb + drawbox->w) && x < picref->video->w; x++) {
00113 double alpha = (double)drawbox->yuv_color[A] / 255;
00114
00115 if ((y - yb < 3) || (yb + drawbox->h - y < 4) ||
00116 (x - xb < 3) || (xb + drawbox->w - x < 4)) {
00117 row[0][x ] = (1 - alpha) * row[0][x ] + alpha * drawbox->yuv_color[Y];
00118 row[1][x >> drawbox->hsub] = (1 - alpha) * row[1][x >> drawbox->hsub] + alpha * drawbox->yuv_color[U];
00119 row[2][x >> drawbox->hsub] = (1 - alpha) * row[2][x >> drawbox->hsub] + alpha * drawbox->yuv_color[V];
00120 }
00121 }
00122 }
00123
00124 return ff_draw_slice(inlink->dst->outputs[0], y0, h, 1);
00125 }
00126
00127 AVFilter avfilter_vf_drawbox = {
00128 .name = "drawbox",
00129 .description = NULL_IF_CONFIG_SMALL("Draw a colored box on the input video."),
00130 .priv_size = sizeof(DrawBoxContext),
00131 .init = init,
00132
00133 .query_formats = query_formats,
00134 .inputs = (const AVFilterPad[]) {{ .name = "default",
00135 .type = AVMEDIA_TYPE_VIDEO,
00136 .config_props = config_input,
00137 .get_video_buffer = ff_null_get_video_buffer,
00138 .start_frame = ff_null_start_frame,
00139 .draw_slice = draw_slice,
00140 .end_frame = ff_null_end_frame,
00141 .min_perms = AV_PERM_WRITE | AV_PERM_READ },
00142 { .name = NULL}},
00143 .outputs = (const AVFilterPad[]) {{ .name = "default",
00144 .type = AVMEDIA_TYPE_VIDEO, },
00145 { .name = NULL}},
00146 };