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