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