2010-09-28 15:16:40 +02:00
|
|
|
/*
|
2010-11-28 11:22:58 +01:00
|
|
|
* Copyright (c) 2008 Affine Systems, Inc (Michael Sullivan, Bobby Impollonia)
|
2010-09-28 15:16:40 +02:00
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Box drawing filter. Also a nice template for a filter that needs to
|
|
|
|
* write in the input frame.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavutil/colorspace.h"
|
2012-08-06 15:49:32 +02:00
|
|
|
#include "libavutil/common.h"
|
2012-10-30 19:42:40 +01:00
|
|
|
#include "libavutil/opt.h"
|
2010-09-28 15:16:40 +02:00
|
|
|
#include "libavutil/pixdesc.h"
|
2011-02-07 14:37:08 +01:00
|
|
|
#include "libavutil/parseutils.h"
|
2010-09-28 15:16:40 +02:00
|
|
|
#include "avfilter.h"
|
2012-05-30 10:12:55 +02:00
|
|
|
#include "formats.h"
|
2012-06-12 20:12:42 +02:00
|
|
|
#include "internal.h"
|
2012-05-19 10:37:56 +02:00
|
|
|
#include "video.h"
|
2010-09-28 15:16:40 +02:00
|
|
|
|
|
|
|
enum { Y, U, V, A };
|
|
|
|
|
|
|
|
typedef struct {
|
2012-10-30 19:42:40 +01:00
|
|
|
const AVClass *class;
|
2012-10-30 20:40:19 +01:00
|
|
|
int x, y, w, h, thickness;
|
2012-10-30 19:42:40 +01:00
|
|
|
char *color_str;
|
2010-09-28 15:16:40 +02:00
|
|
|
unsigned char yuv_color[4];
|
2012-10-30 20:14:07 +01:00
|
|
|
int invert_color; ///< invert luma color
|
2011-08-05 13:15:04 +02:00
|
|
|
int vsub, hsub; ///< chroma subsampling
|
2010-09-28 15:16:40 +02:00
|
|
|
} DrawBoxContext;
|
|
|
|
|
2013-03-13 08:26:39 +01:00
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
2010-09-28 15:16:40 +02:00
|
|
|
{
|
2012-10-30 19:42:40 +01:00
|
|
|
DrawBoxContext *drawbox = ctx->priv;
|
2010-09-28 15:16:40 +02:00
|
|
|
uint8_t rgba_color[4];
|
|
|
|
|
2012-10-30 20:14:07 +01:00
|
|
|
if (!strcmp(drawbox->color_str, "invert"))
|
|
|
|
drawbox->invert_color = 1;
|
|
|
|
else if (av_parse_color(rgba_color, drawbox->color_str, -1, ctx) < 0)
|
2010-09-28 15:16:40 +02:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
2012-11-06 21:40:23 +01:00
|
|
|
if (!drawbox->invert_color) {
|
|
|
|
drawbox->yuv_color[Y] = RGB_TO_Y_CCIR(rgba_color[0], rgba_color[1], rgba_color[2]);
|
|
|
|
drawbox->yuv_color[U] = RGB_TO_U_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
|
|
|
|
drawbox->yuv_color[V] = RGB_TO_V_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
|
|
|
|
drawbox->yuv_color[A] = rgba_color[3];
|
|
|
|
}
|
2010-09-28 15:16:40 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
2012-12-21 23:38:59 +01:00
|
|
|
static const enum AVPixelFormat pix_fmts[] = {
|
2012-10-06 12:10:34 +02:00
|
|
|
AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
|
|
|
|
AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
|
|
|
|
AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
|
|
|
|
AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P,
|
|
|
|
AV_PIX_FMT_NONE
|
2010-09-28 15:16:40 +02:00
|
|
|
};
|
|
|
|
|
2012-05-30 10:12:55 +02:00
|
|
|
ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
|
2010-09-28 15:16:40 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int config_input(AVFilterLink *inlink)
|
|
|
|
{
|
|
|
|
DrawBoxContext *drawbox = inlink->dst->priv;
|
2012-10-06 13:29:37 +02:00
|
|
|
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
|
2010-09-28 15:16:40 +02:00
|
|
|
|
2012-10-06 13:29:37 +02:00
|
|
|
drawbox->hsub = desc->log2_chroma_w;
|
|
|
|
drawbox->vsub = desc->log2_chroma_h;
|
2010-09-28 15:16:40 +02:00
|
|
|
|
|
|
|
if (drawbox->w == 0) drawbox->w = inlink->w;
|
|
|
|
if (drawbox->h == 0) drawbox->h = inlink->h;
|
|
|
|
|
2012-06-25 06:31:38 +02:00
|
|
|
av_log(inlink->dst, AV_LOG_VERBOSE, "x:%d y:%d w:%d h:%d color:0x%02X%02X%02X%02X\n",
|
2012-04-13 23:11:39 +02:00
|
|
|
drawbox->x, drawbox->y, drawbox->w, drawbox->h,
|
2010-09-28 15:16:40 +02:00
|
|
|
drawbox->yuv_color[Y], drawbox->yuv_color[U], drawbox->yuv_color[V], drawbox->yuv_color[A]);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-28 08:41:07 +01:00
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
|
2010-09-28 15:16:40 +02:00
|
|
|
{
|
|
|
|
DrawBoxContext *drawbox = inlink->dst->priv;
|
|
|
|
int plane, x, y, xb = drawbox->x, yb = drawbox->y;
|
|
|
|
unsigned char *row[4];
|
|
|
|
|
2012-11-28 08:41:07 +01:00
|
|
|
for (y = FFMAX(yb, 0); y < frame->height && y < (yb + drawbox->h); y++) {
|
2012-11-29 00:09:53 +01:00
|
|
|
row[0] = frame->data[0] + y * frame->linesize[0];
|
2010-09-28 15:16:40 +02:00
|
|
|
|
|
|
|
for (plane = 1; plane < 3; plane++)
|
2012-11-29 00:09:53 +01:00
|
|
|
row[plane] = frame->data[plane] +
|
|
|
|
frame->linesize[plane] * (y >> drawbox->vsub);
|
2010-09-28 15:16:40 +02:00
|
|
|
|
2012-10-30 20:14:07 +01:00
|
|
|
if (drawbox->invert_color) {
|
2013-03-10 01:30:30 +01:00
|
|
|
for (x = FFMAX(xb, 0); x < xb + drawbox->w && x < frame->width; x++)
|
2012-10-30 20:40:19 +01:00
|
|
|
if ((y - yb < drawbox->thickness-1) || (yb + drawbox->h - y < drawbox->thickness) ||
|
|
|
|
(x - xb < drawbox->thickness-1) || (xb + drawbox->w - x < drawbox->thickness))
|
2012-10-30 20:14:07 +01:00
|
|
|
row[0][x] = 0xff - row[0][x];
|
|
|
|
} else {
|
2013-03-10 01:30:30 +01:00
|
|
|
for (x = FFMAX(xb, 0); x < xb + drawbox->w && x < frame->width; x++) {
|
2012-10-30 20:17:24 +01:00
|
|
|
double alpha = (double)drawbox->yuv_color[A] / 255;
|
|
|
|
|
2012-10-30 20:40:19 +01:00
|
|
|
if ((y - yb < drawbox->thickness-1) || (yb + drawbox->h - y < drawbox->thickness) ||
|
|
|
|
(x - xb < drawbox->thickness-1) || (xb + drawbox->w - x < drawbox->thickness)) {
|
2012-10-30 20:17:24 +01:00
|
|
|
row[0][x ] = (1 - alpha) * row[0][x ] + alpha * drawbox->yuv_color[Y];
|
|
|
|
row[1][x >> drawbox->hsub] = (1 - alpha) * row[1][x >> drawbox->hsub] + alpha * drawbox->yuv_color[U];
|
|
|
|
row[2][x >> drawbox->hsub] = (1 - alpha) * row[2][x >> drawbox->hsub] + alpha * drawbox->yuv_color[V];
|
|
|
|
}
|
2010-09-28 15:16:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-29 00:09:53 +01:00
|
|
|
return ff_filter_frame(inlink->dst->outputs[0], frame);
|
2010-09-28 15:16:40 +02:00
|
|
|
}
|
|
|
|
|
2013-02-25 21:21:29 +01:00
|
|
|
#define OFFSET(x) offsetof(DrawBoxContext, x)
|
2013-04-10 18:11:15 +02:00
|
|
|
#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
|
|
|
|
|
|
|
|
static const AVOption drawbox_options[] = {
|
2013-02-25 21:21:29 +01:00
|
|
|
{ "x", "Horizontal position of the left box edge", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS },
|
|
|
|
{ "y", "Vertical position of the top box edge", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS },
|
|
|
|
{ "width", "Width of the box", OFFSET(w), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
|
2013-04-10 18:11:15 +02:00
|
|
|
{ "w", "Width of the box", OFFSET(w), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
|
2013-02-25 21:21:29 +01:00
|
|
|
{ "height", "Height of the box", OFFSET(h), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
|
2013-04-10 18:11:15 +02:00
|
|
|
{ "h", "Height of the box", OFFSET(h), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
|
|
|
|
{ "color", "Color of the box", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, CHAR_MIN, CHAR_MAX, .flags = FLAGS },
|
|
|
|
{ "c", "Color of the box", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, CHAR_MIN, CHAR_MAX, .flags = FLAGS },
|
|
|
|
{ "thickness", "set the box maximum thickness", OFFSET(thickness), AV_OPT_TYPE_INT, {.i64=4}, 0, INT_MAX, FLAGS },
|
|
|
|
{ "t", "set the box maximum thickness", OFFSET(thickness), AV_OPT_TYPE_INT, {.i64=4}, 0, INT_MAX, FLAGS },
|
2013-02-25 21:21:29 +01:00
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
2013-04-10 18:11:15 +02:00
|
|
|
AVFILTER_DEFINE_CLASS(drawbox);
|
2013-02-25 21:21:29 +01:00
|
|
|
|
2012-07-24 15:14:01 +02:00
|
|
|
static const AVFilterPad avfilter_vf_drawbox_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.config_props = config_input,
|
|
|
|
.get_video_buffer = ff_null_get_video_buffer,
|
2012-11-29 00:09:53 +01:00
|
|
|
.filter_frame = filter_frame,
|
2012-11-28 08:41:07 +01:00
|
|
|
.needs_writable = 1,
|
2012-07-24 15:14:01 +02:00
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad avfilter_vf_drawbox_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2010-09-28 15:16:40 +02:00
|
|
|
AVFilter avfilter_vf_drawbox = {
|
|
|
|
.name = "drawbox",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Draw a colored box on the input video."),
|
|
|
|
.priv_size = sizeof(DrawBoxContext),
|
2013-02-25 21:21:29 +01:00
|
|
|
.priv_class = &drawbox_class,
|
2010-09-28 15:16:40 +02:00
|
|
|
.init = init,
|
|
|
|
|
|
|
|
.query_formats = query_formats,
|
2012-07-24 15:14:01 +02:00
|
|
|
.inputs = avfilter_vf_drawbox_inputs,
|
|
|
|
.outputs = avfilter_vf_drawbox_outputs,
|
2013-04-08 20:35:02 +02:00
|
|
|
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE,
|
2010-09-28 15:16:40 +02:00
|
|
|
};
|