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/opt.h"
00030 #include "libavutil/pixdesc.h"
00031 #include "libavutil/parseutils.h"
00032 #include "avfilter.h"
00033 #include "formats.h"
00034 #include "internal.h"
00035 #include "video.h"
00036
00037 enum { Y, U, V, A };
00038
00039 typedef struct {
00040 const AVClass *class;
00041 int x, y, w, h, thickness;
00042 char *color_str;
00043 unsigned char yuv_color[4];
00044 int invert_color;
00045 int vsub, hsub;
00046 } DrawBoxContext;
00047
00048 #define OFFSET(x) offsetof(DrawBoxContext, x)
00049 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
00050
00051 static const AVOption drawbox_options[] = {
00052 { "x", "set the box top-left corner x position", OFFSET(x), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX, FLAGS },
00053 { "y", "set the box top-left corner y position", OFFSET(y), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX, FLAGS },
00054 { "width", "set the box width", OFFSET(w), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
00055 { "w", "set the box width", OFFSET(w), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
00056 { "height", "set the box height", OFFSET(h), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
00057 { "h", "set the box height", OFFSET(h), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
00058 { "color", "set the box edge color", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str="black"}, CHAR_MIN, CHAR_MAX, FLAGS },
00059 { "c", "set the box edge color", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str="black"}, CHAR_MIN, CHAR_MAX, FLAGS },
00060 { "thickness", "set the box maximum thickness", OFFSET(thickness), AV_OPT_TYPE_INT, {.i64=4}, 0, INT_MAX, FLAGS },
00061 { "t", "set the box maximum thickness", OFFSET(thickness), AV_OPT_TYPE_INT, {.i64=4}, 0, INT_MAX, FLAGS },
00062 {NULL},
00063 };
00064
00065 AVFILTER_DEFINE_CLASS(drawbox);
00066
00067 static av_cold int init(AVFilterContext *ctx, const char *args)
00068 {
00069 DrawBoxContext *drawbox = ctx->priv;
00070 uint8_t rgba_color[4];
00071 static const char *shorthand[] = { "x", "y", "w", "h", "color", "thickness", NULL };
00072 int ret;
00073
00074 drawbox->class = &drawbox_class;
00075 av_opt_set_defaults(drawbox);
00076
00077 if ((ret = av_opt_set_from_string(drawbox, args, shorthand, "=", ":")) < 0)
00078 return ret;
00079
00080 if (!strcmp(drawbox->color_str, "invert"))
00081 drawbox->invert_color = 1;
00082 else if (av_parse_color(rgba_color, drawbox->color_str, -1, ctx) < 0)
00083 return AVERROR(EINVAL);
00084
00085 if (!drawbox->invert_color) {
00086 drawbox->yuv_color[Y] = RGB_TO_Y_CCIR(rgba_color[0], rgba_color[1], rgba_color[2]);
00087 drawbox->yuv_color[U] = RGB_TO_U_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
00088 drawbox->yuv_color[V] = RGB_TO_V_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
00089 drawbox->yuv_color[A] = rgba_color[3];
00090 }
00091
00092 return 0;
00093 }
00094
00095 static av_cold void uninit(AVFilterContext *ctx)
00096 {
00097 DrawBoxContext *drawbox = ctx->priv;
00098 av_opt_free(drawbox);
00099 }
00100
00101 static int query_formats(AVFilterContext *ctx)
00102 {
00103 enum AVPixelFormat pix_fmts[] = {
00104 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
00105 AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
00106 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
00107 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P,
00108 AV_PIX_FMT_NONE
00109 };
00110
00111 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
00112 return 0;
00113 }
00114
00115 static int config_input(AVFilterLink *inlink)
00116 {
00117 DrawBoxContext *drawbox = inlink->dst->priv;
00118 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
00119
00120 drawbox->hsub = desc->log2_chroma_w;
00121 drawbox->vsub = desc->log2_chroma_h;
00122
00123 if (drawbox->w == 0) drawbox->w = inlink->w;
00124 if (drawbox->h == 0) drawbox->h = inlink->h;
00125
00126 av_log(inlink->dst, AV_LOG_VERBOSE, "x:%d y:%d w:%d h:%d color:0x%02X%02X%02X%02X\n",
00127 drawbox->x, drawbox->y, drawbox->w, drawbox->h,
00128 drawbox->yuv_color[Y], drawbox->yuv_color[U], drawbox->yuv_color[V], drawbox->yuv_color[A]);
00129
00130 return 0;
00131 }
00132
00133 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
00134 {
00135 DrawBoxContext *drawbox = inlink->dst->priv;
00136 int plane, x, y, xb = drawbox->x, yb = drawbox->y;
00137 unsigned char *row[4];
00138
00139 for (y = FFMAX(yb, 0); y < frame->video->h && y < (yb + drawbox->h); y++) {
00140 row[0] = frame->data[0] + y * frame->linesize[0];
00141
00142 for (plane = 1; plane < 3; plane++)
00143 row[plane] = frame->data[plane] +
00144 frame->linesize[plane] * (y >> drawbox->vsub);
00145
00146 if (drawbox->invert_color) {
00147 for (x = FFMAX(xb, 0); x < xb + drawbox->w && x < frame->video->w; x++)
00148 if ((y - yb < drawbox->thickness-1) || (yb + drawbox->h - y < drawbox->thickness) ||
00149 (x - xb < drawbox->thickness-1) || (xb + drawbox->w - x < drawbox->thickness))
00150 row[0][x] = 0xff - row[0][x];
00151 } else {
00152 for (x = FFMAX(xb, 0); x < xb + drawbox->w && x < frame->video->w; x++) {
00153 double alpha = (double)drawbox->yuv_color[A] / 255;
00154
00155 if ((y - yb < drawbox->thickness-1) || (yb + drawbox->h - y < drawbox->thickness) ||
00156 (x - xb < drawbox->thickness-1) || (xb + drawbox->w - x < drawbox->thickness)) {
00157 row[0][x ] = (1 - alpha) * row[0][x ] + alpha * drawbox->yuv_color[Y];
00158 row[1][x >> drawbox->hsub] = (1 - alpha) * row[1][x >> drawbox->hsub] + alpha * drawbox->yuv_color[U];
00159 row[2][x >> drawbox->hsub] = (1 - alpha) * row[2][x >> drawbox->hsub] + alpha * drawbox->yuv_color[V];
00160 }
00161 }
00162 }
00163 }
00164
00165 return ff_filter_frame(inlink->dst->outputs[0], frame);
00166 }
00167
00168 static const AVFilterPad avfilter_vf_drawbox_inputs[] = {
00169 {
00170 .name = "default",
00171 .type = AVMEDIA_TYPE_VIDEO,
00172 .config_props = config_input,
00173 .get_video_buffer = ff_null_get_video_buffer,
00174 .filter_frame = filter_frame,
00175 .min_perms = AV_PERM_WRITE | AV_PERM_READ,
00176 },
00177 { NULL }
00178 };
00179
00180 static const AVFilterPad avfilter_vf_drawbox_outputs[] = {
00181 {
00182 .name = "default",
00183 .type = AVMEDIA_TYPE_VIDEO,
00184 },
00185 { NULL }
00186 };
00187
00188 AVFilter avfilter_vf_drawbox = {
00189 .name = "drawbox",
00190 .description = NULL_IF_CONFIG_SMALL("Draw a colored box on the input video."),
00191 .priv_size = sizeof(DrawBoxContext),
00192 .init = init,
00193 .uninit = uninit,
00194
00195 .query_formats = query_formats,
00196 .inputs = avfilter_vf_drawbox_inputs,
00197 .outputs = avfilter_vf_drawbox_outputs,
00198 .priv_class = &drawbox_class,
00199 };