[FFmpeg-devel] [PATCH] lavfi/overlay: add support for partial overlaying
Stefano Sabatini
stefasab at gmail.com
Tue Feb 19 20:10:42 CET 2013
TODO: bump micro
---
libavfilter/vf_overlay.c | 60 ++++++++++++++++++++++++++--------------------
1 file changed, 34 insertions(+), 26 deletions(-)
diff --git a/libavfilter/vf_overlay.c b/libavfilter/vf_overlay.c
index 7e7ec00..bcc095b 100644
--- a/libavfilter/vf_overlay.c
+++ b/libavfilter/vf_overlay.c
@@ -236,13 +236,12 @@ static int config_input_overlay(AVFilterLink *inlink)
if (over->x < 0 || over->y < 0 ||
over->x + var_values[VAR_OVERLAY_W] > var_values[VAR_MAIN_W] ||
over->y + var_values[VAR_OVERLAY_H] > var_values[VAR_MAIN_H]) {
- av_log(ctx, AV_LOG_ERROR,
+ av_log(ctx, AV_LOG_WARNING,
"Overlay area (%d,%d)<->(%d,%d) not within the main area (0,0)<->(%d,%d) or zero-sized\n",
over->x, over->y,
(int)(over->x + var_values[VAR_OVERLAY_W]),
(int)(over->y + var_values[VAR_OVERLAY_H]),
(int)var_values[VAR_MAIN_W], (int)var_values[VAR_MAIN_H]);
- return AVERROR(EINVAL);
}
return 0;
@@ -275,9 +274,6 @@ static int config_output(AVFilterLink *outlink)
/**
* Blend image in src to destination buffer dst at position (x, y).
- *
- * It is assumed that the src image at position (x, y) is contained in
- * dst.
*/
static void blend_image(AVFilterContext *ctx,
AVFilterBufferRef *dst, AVFilterBufferRef *src,
@@ -287,11 +283,10 @@ static void blend_image(AVFilterContext *ctx,
int i, j, k;
int width = src->video->w;
int height = src->video->h;
+ const int dst_width = dst->video->w;
+ const int dst_height = dst->video->h;
if (over->main_is_packed_rgb) {
- uint8_t *dp = dst->data[0] + x * over->main_pix_step[0] +
- y * dst->linesize[0];
- uint8_t *sp = src->data[0];
uint8_t alpha; ///< the amount of overlay to blend on to main
const int dr = over->main_rgba_map[R];
const int dg = over->main_rgba_map[G];
@@ -305,8 +300,17 @@ static void blend_image(AVFilterContext *ctx,
const int sstep = over->overlay_pix_step[0];
const int main_has_alpha = over->main_has_alpha;
for (i = 0; i < height; i++) {
- uint8_t *d = dp, *s = sp;
+ uint8_t *d, *s;
+
+ if (y+i < 0 || y+i >= dst_height)
+ continue;
+
+ d = dst->data[0] + x * over->main_pix_step[0] + y * dst->linesize[0];
+ s = dst->data[0] + i * dst->linesize[0];
+
for (j = 0; j < width; j++) {
+ if (x+j < 0 || x+j >= dst_width)
+ continue;
alpha = s[sa];
// if the main channel has an alpha channel, alpha has to be calculated
@@ -346,19 +350,20 @@ static void blend_image(AVFilterContext *ctx,
d += dstep;
s += sstep;
}
- dp += dst->linesize[0];
- sp += src->linesize[0];
}
} else {
const int main_has_alpha = over->main_has_alpha;
if (main_has_alpha) {
- uint8_t *da = dst->data[3] + x * over->main_pix_step[3] +
- y * dst->linesize[3];
- uint8_t *sa = src->data[3];
uint8_t alpha; ///< the amount of overlay to blend on to main
for (i = 0; i < height; i++) {
- uint8_t *d = da, *s = sa;
+ uint8_t *d, *s;
+ if (y+i < 0 || y+i >= dst_height)
+ continue;
+ d = dst->data[3] + y * dst->linesize[3] + x;
+ s = src->data[3] + i * src->linesize[3];
+
for (j = 0; j < width; j++) {
+ if (x+j >= 0 && x+j < dst_width) {
alpha = *s;
if (alpha != 0 && alpha != 255) {
uint8_t alpha_d = *d;
@@ -374,27 +379,32 @@ static void blend_image(AVFilterContext *ctx,
// apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha
*d += FAST_DIV255((255 - *d) * *s);
}
+ }
d += 1;
s += 1;
}
- da += dst->linesize[3];
- sa += src->linesize[3];
}
}
for (i = 0; i < 3; i++) {
int hsub = i ? over->hsub : 0;
int vsub = i ? over->vsub : 0;
- uint8_t *dp = dst->data[i] + (x >> hsub) +
- (y >> vsub) * dst->linesize[i];
- uint8_t *sp = src->data[i];
- uint8_t *ap = src->data[3];
int wp = FFALIGN(width, 1<<hsub) >> hsub;
int hp = FFALIGN(height, 1<<vsub) >> vsub;
+ const int dst_wp = FFALIGN(dst_width, 1<<hsub) >> hsub;
+ const int dst_hp = FFALIGN(dst_height, 1<<vsub) >> vsub;
+
for (j = 0; j < hp; j++) {
- uint8_t *d = dp, *s = sp, *a = ap;
+ uint8_t *d, *s, *a;
+ if ((y>>vsub)+j < 0 || (y>>vsub)+j >= dst_hp)
+ continue;
+
+ d = dst->data[i] + ((y>>vsub)+j) * dst->linesize[i] + (x>>hsub);
+ s = src->data[i] + j * src->linesize[i];
+ a = src->data[3] + (j<<vsub) * src->linesize[3];
for (k = 0; k < wp; k++) {
- // average alpha for color components, improve quality
int alpha_v, alpha_h, alpha;
+ if ((x>>hsub)+k >= 0 && (x>>hsub)+k < dst_wp) {
+ // average alpha for color components, improve quality
if (hsub && vsub && j+1 < hp && k+1 < wp) {
alpha = (a[0] + a[src->linesize[3]] +
a[1] + a[src->linesize[3]+1]) >> 2;
@@ -425,13 +435,11 @@ static void blend_image(AVFilterContext *ctx,
alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d);
}
*d = FAST_DIV255(*d * (255 - alpha) + *s * alpha);
+ }
s++;
d++;
a += 1 << hsub;
}
- dp += dst->linesize[i];
- sp += src->linesize[i];
- ap += (1 << vsub) * src->linesize[3];
}
}
}
--
1.7.9.5
More information about the ffmpeg-devel
mailing list