History

vf_overlay: enable RGB path

Add option rgb which forces the RGB path.

Stefano Sabatini committed 15 years ago

Commit 3013bfa1 parent c0dbab92 files · +116 −20Browse files

Unified · Split

2 files
  1. doc/filters.texi +13 −2
  2. libavfilter/vf_overlay.c +103 −18
doc/filters.texi+13−2
@@ -1631,10 +1631,10 @@ Overlay one video on top of another. · 1630 unmodified lines
16311631 It takes two inputs and one output, the first input is the "main"
16321632 video on which the second input is overlayed.
16331633
1634−It accepts the parameters: @var{x}:@var{y}.
1634+It accepts the parameters: @var{x}:@var{y}[:@var{options}].
16351635
16361636 @var{x} is the x coordinate of the overlayed video on the main video,
1637−@var{y} is the y coordinate. The parameters are expressions containing
1637+@var{y} is the y coordinate. @var{x} and @var{y} are expressions containing
16381638 the following parameters:
16391639
16401640 @table @option
@@ -1651,6 +1651,17 @@ overlay input width and height · 10 unmodified lines
16511651 same as @var{overlay_w} and @var{overlay_h}
16521652 @end table
16531653
1654+@var{options} is an optional list of @var{key}=@var{value} pairs,
1655+separated by ":".
1656+
1657+The description of the accepted options follows.
1658+
1659+@table @option
1660+@item rgb
1661+If set to 1, force the filter to accept inputs in the RGB
1662+colorspace. Default value is 0.
1663+@end table
1664+
16541665 Be aware that frames are taken from each input video in timestamp
16551666 order, hence, if their initial timestamps differ, it is a a good idea
16561667 to pass the two inputs through a @var{setpts=PTS-STARTPTS} filter to
libavfilter/vf_overlay.c+103−18
@@ -33,6 +33,7 @@ · 32 unmodified lines
3333 #include "libavutil/imgutils.h"
3434 #include "libavutil/mathematics.h"
3535 #include "internal.h"
36+#include "drawutils.h"
3637
3738 static const char *var_names[] = {
3839 "main_w", "W", ///< width of the main video
@@ -53,13 +54,31 @@ enum var_name { · 14 unmodified lines
5354 #define MAIN 0
5455 #define OVERLAY 1
5556
57+#define R 0
58+#define G 1
59+#define B 2
60+#define A 3
61+
62+#define Y 0
63+#define U 1
64+#define V 2
65+
5666 typedef struct {
5767 const AVClass *class;
5868 int x, y; ///< position of overlayed picture
5969
70+ int allow_packed_rgb;
71+ uint8_t main_is_packed_rgb;
72+ uint8_t main_rgba_map[4];
73+ uint8_t main_has_alpha;
74+ uint8_t overlay_is_packed_rgb;
75+ uint8_t overlay_rgba_map[4];
76+ uint8_t overlay_has_alpha;
77+
6078 AVFilterBufferRef *overpicref;
6179
62− int max_plane_step[4]; ///< steps per pixel for each plane
80+ int main_pix_step[4]; ///< steps per pixel for each plane of the main output
81+ int overlay_pix_step[4]; ///< steps per pixel for each plane of the overlay
6382 int hsub, vsub; ///< chroma subsampling values
6483
6584 char *x_expr, *y_expr;
@@ -70,6 +89,7 @@ typedef struct { · 4 unmodified lines
7089 static const AVOption overlay_options[] = {
7190 { "x", "set the x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX },
7291 { "y", "set the y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX },
92+ {"rgb", "force packed RGB in input and output", OFFSET(allow_packed_rgb), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1 },
7393 {NULL},
7494 };
7595
@@ -128,27 +148,59 @@ static av_cold void uninit(AVFilterContext *ctx) · 52 unmodified lines
128148
129149 static int query_formats(AVFilterContext *ctx)
130150 {
131− const enum PixelFormat inout_pix_fmts[] = { PIX_FMT_YUV420P, PIX_FMT_NONE };
132− const enum PixelFormat blend_pix_fmts[] = { PIX_FMT_YUVA420P, PIX_FMT_NONE };
133− AVFilterFormats *inout_formats = avfilter_make_format_list(inout_pix_fmts);
134− AVFilterFormats *blend_formats = avfilter_make_format_list(blend_pix_fmts);
151+ OverlayContext *over = ctx->priv;
135152
136− avfilter_formats_ref(inout_formats, &ctx->inputs [MAIN ]->out_formats);
137− avfilter_formats_ref(blend_formats, &ctx->inputs [OVERLAY]->out_formats);
138− avfilter_formats_ref(inout_formats, &ctx->outputs[MAIN ]->in_formats );
153+ /* overlay formats contains alpha, for avoiding conversion with alpha information loss */
154+ const enum PixelFormat main_pix_fmts_yuv[] = { PIX_FMT_YUV420P, PIX_FMT_NONE };
155+ const enum PixelFormat overlay_pix_fmts_yuv[] = { PIX_FMT_YUVA420P, PIX_FMT_NONE };
156+ const enum PixelFormat main_pix_fmts_rgb[] = {
157+ PIX_FMT_ARGB, PIX_FMT_RGBA,
158+ PIX_FMT_ABGR, PIX_FMT_BGRA,
159+ PIX_FMT_RGB24, PIX_FMT_BGR24,
160+ PIX_FMT_NONE
161+ };
162+ const enum PixelFormat overlay_pix_fmts_rgb[] = {
163+ PIX_FMT_ARGB, PIX_FMT_RGBA,
164+ PIX_FMT_ABGR, PIX_FMT_BGRA,
165+ PIX_FMT_NONE
166+ };
167+
168+ AVFilterFormats *main_formats;
169+ AVFilterFormats *overlay_formats;
170+
171+ if (over->allow_packed_rgb) {
172+ main_formats = avfilter_make_format_list(main_pix_fmts_rgb);
173+ overlay_formats = avfilter_make_format_list(overlay_pix_fmts_rgb);
174+ } else {
175+ main_formats = avfilter_make_format_list(main_pix_fmts_yuv);
176+ overlay_formats = avfilter_make_format_list(overlay_pix_fmts_yuv);
177+ }
178+
179+ avfilter_formats_ref(main_formats, &ctx->inputs [MAIN ]->out_formats);
180+ avfilter_formats_ref(overlay_formats, &ctx->inputs [OVERLAY]->out_formats);
181+ avfilter_formats_ref(main_formats, &ctx->outputs[MAIN ]->in_formats );
139182
140183 return 0;
141184 }
142185
186+static enum PixelFormat alpha_pix_fmts[] = {
187+ PIX_FMT_YUVA420P, PIX_FMT_ARGB, PIX_FMT_ABGR, PIX_FMT_RGBA,
188+ PIX_FMT_BGRA, PIX_FMT_NONE
189+};
190+
143191 static int config_input_main(AVFilterLink *inlink)
144192 {
145193 OverlayContext *over = inlink->dst->priv;
146194 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
147195
148− av_image_fill_max_pixsteps(over->max_plane_step, NULL, pix_desc);
196+ av_image_fill_max_pixsteps(over->main_pix_step, NULL, pix_desc);
197+
149198 over->hsub = pix_desc->log2_chroma_w;
150199 over->vsub = pix_desc->log2_chroma_h;
151200
201+ over->main_is_packed_rgb =
202+ ff_fill_rgba_map(over->main_rgba_map, inlink->format) >= 0;
203+ over->main_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
152204 return 0;
153205 }
154206
@@ -159,6 +211,9 @@ static int config_input_overlay(AVFilterLink *inlink) · 4 unmodified lines
159211 char *expr;
160212 double var_values[VAR_VARS_NB], res;
161213 int ret;
214+ const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
215+
216+ av_image_fill_max_pixsteps(over->overlay_pix_step, NULL, pix_desc);
162217
163218 /* Finish the configuration by evaluating the expressions
164219 now when both inputs are configured. */
@@ -181,6 +236,10 @@ static int config_input_overlay(AVFilterLink *inlink) · 16 unmodified lines
181236 goto fail;
182237 over->x = res;
183238
239+ over->overlay_is_packed_rgb =
240+ ff_fill_rgba_map(over->overlay_rgba_map, inlink->format) >= 0;
241+ over->overlay_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
242+
184243 av_log(ctx, AV_LOG_INFO,
185244 "main w:%d h:%d fmt:%s overlay x:%d y:%d w:%d h:%d fmt:%s\n",
186245 ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
@@ -272,6 +331,10 @@ static void start_frame_overlay(AVFilterLink *inlink, AVFilterBufferRef *inpicre · 85 unmodified lines
272331 ctx->outputs[0]->time_base);
273332 }
274333
334+// divide by 255 and round to nearest
335+// apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
336+#define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
337+
275338 static void blend_slice(AVFilterContext *ctx,
276339 AVFilterBufferRef *dst, AVFilterBufferRef *src,
277340 int x, int y, int w, int h,
@@ -289,21 +352,43 @@ static void blend_slice(AVFilterContext *ctx, · 11 unmodified lines
289352 start_y = FFMAX(y, slice_y);
290353 height = end_y - start_y;
291354
292− if (dst->format == PIX_FMT_BGR24 || dst->format == PIX_FMT_RGB24) {
293− uint8_t *dp = dst->data[0] + x * 3 + start_y * dst->linesize[0];
355+ if (over->main_is_packed_rgb) {
356+ uint8_t *dp = dst->data[0] + x * over->main_pix_step[0] +
357+ start_y * dst->linesize[0];
294358 uint8_t *sp = src->data[0];
295− int b = dst->format == PIX_FMT_BGR24 ? 2 : 0;
296− int r = dst->format == PIX_FMT_BGR24 ? 0 : 2;
359+ uint8_t alpha; ///< the amount of overlay to blend on to main
360+ const int dr = over->main_rgba_map[R];
361+ const int dg = over->main_rgba_map[G];
362+ const int db = over->main_rgba_map[B];
363+ const int dstep = over->main_pix_step[0];
364+ const int sr = over->overlay_rgba_map[R];
365+ const int sg = over->overlay_rgba_map[G];
366+ const int sb = over->overlay_rgba_map[B];
367+ const int sa = over->overlay_rgba_map[A];
368+ const int sstep = over->overlay_pix_step[0];
297369 if (slice_y > y)
298370 sp += (slice_y - y) * src->linesize[0];
299371 for (i = 0; i < height; i++) {
300372 uint8_t *d = dp, *s = sp;
301373 for (j = 0; j < width; j++) {
302− d[r] = (d[r] * (0xff - s[3]) + s[0] * s[3] + 128) >> 8;
303− d[1] = (d[1] * (0xff - s[3]) + s[1] * s[3] + 128) >> 8;
304− d[b] = (d[b] * (0xff - s[3]) + s[2] * s[3] + 128) >> 8;
305− d += 3;
306− s += 4;
374+ alpha = s[sa];
375+ switch (alpha) {
376+ case 0:
377+ break;
378+ case 255:
379+ d[dr] = s[sr];
380+ d[dg] = s[sg];
381+ d[db] = s[sb];
382+ break;
383+ default:
384+ // main_value = main_value * (1 - alpha) + overlay_value * alpha
385+ // since alpha is in the range 0-255, the result must divided by 255
386+ d[dr] = FAST_DIV255(d[dr] * (255 - alpha) + s[sr] * alpha);
387+ d[dg] = FAST_DIV255(d[dg] * (255 - alpha) + s[sg] * alpha);
388+ d[db] = FAST_DIV255(d[db] * (255 - alpha) + s[sb] * alpha);
389+ }
390+ d += dstep;
391+ s += sstep;
307392 }
308393 dp += dst->linesize[0];
309394 sp += src->linesize[0];

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t