Statistics
| Branch: | Revision:

ffmpeg / libavfilter / vf_overlay.c @ master

History | View | Annotate | Download (13.8 KB)

1 58935b25 Stefano Sabatini
/*
2
 * Copyright (c) 2010 Stefano Sabatini
3
 * Copyright (c) 2010 Baptiste Coudurier
4
 * Copyright (c) 2007 Bobby Bingham
5
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22
23
/**
24
 * @file
25
 * overlay one video on top of another
26
 */
27
28
#include "avfilter.h"
29
#include "libavutil/eval.h"
30
#include "libavutil/avstring.h"
31
#include "libavutil/pixdesc.h"
32 7ffe76e5 Reinhard Tartler
#include "libavutil/imgutils.h"
33 58935b25 Stefano Sabatini
#include "internal.h"
34
35
static const char *var_names[] = {
36
    "E",
37
    "PHI",
38
    "PI",
39
    "main_w",    "W", ///< width  of the main    video
40
    "main_h",    "H", ///< height of the main    video
41
    "overlay_w", "w", ///< width  of the overlay video
42
    "overlay_h", "h", ///< height of the overlay video
43
    NULL
44
};
45
46
enum var_name {
47
    VAR_E,
48
    VAR_PHI,
49
    VAR_PI,
50
    VAR_MAIN_W,    VAR_MW,
51
    VAR_MAIN_H,    VAR_MH,
52
    VAR_OVERLAY_W, VAR_OW,
53
    VAR_OVERLAY_H, VAR_OH,
54
    VAR_VARS_NB
55
};
56
57
#define MAIN    0
58
#define OVERLAY 1
59
60
typedef struct {
61
    int x, y;                   ///< position of overlayed picture
62
63
    AVFilterBufferRef *overpicref;
64
65
    int max_plane_step[4];      ///< steps per pixel for each plane
66
    int hsub, vsub;             ///< chroma subsampling values
67
68
    char x_expr[256], y_expr[256];
69
} OverlayContext;
70
71
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
72
{
73
    OverlayContext *over = ctx->priv;
74
75
    av_strlcpy(over->x_expr, "0", sizeof(over->x_expr));
76
    av_strlcpy(over->y_expr, "0", sizeof(over->y_expr));
77
78
    if (args)
79
        sscanf(args, "%255[^:]:%255[^:]", over->x_expr, over->y_expr);
80
81
    return 0;
82
}
83
84
static av_cold void uninit(AVFilterContext *ctx)
85
{
86
    OverlayContext *over = ctx->priv;
87
88
    if (over->overpicref)
89
        avfilter_unref_buffer(over->overpicref);
90
}
91
92
static int query_formats(AVFilterContext *ctx)
93
{
94
    const enum PixelFormat inout_pix_fmts[] = { PIX_FMT_YUV420P,  PIX_FMT_NONE };
95
    const enum PixelFormat blend_pix_fmts[] = { PIX_FMT_YUVA420P, PIX_FMT_NONE };
96
    AVFilterFormats *inout_formats = avfilter_make_format_list(inout_pix_fmts);
97
    AVFilterFormats *blend_formats = avfilter_make_format_list(blend_pix_fmts);
98
99
    avfilter_formats_ref(inout_formats, &ctx->inputs [MAIN   ]->out_formats);
100
    avfilter_formats_ref(blend_formats, &ctx->inputs [OVERLAY]->out_formats);
101
    avfilter_formats_ref(inout_formats, &ctx->outputs[MAIN   ]->in_formats );
102
103
    return 0;
104
}
105
106
static int config_input_main(AVFilterLink *inlink)
107
{
108
    OverlayContext *over = inlink->dst->priv;
109
    const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
110
111
    av_image_fill_max_pixsteps(over->max_plane_step, NULL, pix_desc);
112
    over->hsub = pix_desc->log2_chroma_w;
113
    over->vsub = pix_desc->log2_chroma_h;
114
115
    return 0;
116
}
117
118
static int config_input_overlay(AVFilterLink *inlink)
119
{
120
    AVFilterContext *ctx  = inlink->dst;
121
    OverlayContext  *over = inlink->dst->priv;
122
    char *expr;
123
    double var_values[VAR_VARS_NB], res;
124
    int ret;
125
126
    /* Finish the configuration by evaluating the expressions
127
       now when both inputs are configured. */
128
    var_values[VAR_E  ] = M_E;
129
    var_values[VAR_PHI] = M_PHI;
130
    var_values[VAR_PI ] = M_PI;
131
132
    var_values[VAR_MAIN_W   ] = var_values[VAR_MW] = ctx->inputs[MAIN   ]->w;
133
    var_values[VAR_MAIN_H   ] = var_values[VAR_MH] = ctx->inputs[MAIN   ]->h;
134
    var_values[VAR_OVERLAY_W] = var_values[VAR_OW] = ctx->inputs[OVERLAY]->w;
135
    var_values[VAR_OVERLAY_H] = var_values[VAR_OH] = ctx->inputs[OVERLAY]->h;
136
137
    if ((ret = av_expr_parse_and_eval(&res, (expr = over->x_expr), var_names, var_values,
138
                                      NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
139
        goto fail;
140
    over->x = res;
141
    if ((ret = av_expr_parse_and_eval(&res, (expr = over->y_expr), var_names, var_values,
142
                                      NULL, NULL, NULL, NULL, NULL, 0, ctx)))
143
        goto fail;
144
    over->y = res;
145
    /* x may depend on y */
146
    if ((ret = av_expr_parse_and_eval(&res, (expr = over->x_expr), var_names, var_values,
147
                                      NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
148
        goto fail;
149
    over->x = res;
150
151
    av_log(ctx, AV_LOG_INFO,
152
           "main w:%d h:%d fmt:%s overlay x:%d y:%d w:%d h:%d fmt:%s\n",
153
           ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
154
           av_pix_fmt_descriptors[ctx->inputs[MAIN]->format].name,
155
           over->x, over->y,
156
           ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
157
           av_pix_fmt_descriptors[ctx->inputs[OVERLAY]->format].name);
158
159
    if (over->x < 0 || over->y < 0 ||
160
        over->x + var_values[VAR_OVERLAY_W] > var_values[VAR_MAIN_W] ||
161
        over->y + var_values[VAR_OVERLAY_H] > var_values[VAR_MAIN_H]) {
162
        av_log(ctx, AV_LOG_ERROR,
163
               "Overlay area (%d,%d)<->(%d,%d) not within the main area (0,0)<->(%d,%d) or zero-sized\n",
164
               over->x, over->y,
165
               (int)(over->x + var_values[VAR_OVERLAY_W]),
166
               (int)(over->y + var_values[VAR_OVERLAY_H]),
167
               (int)var_values[VAR_MAIN_W], (int)var_values[VAR_MAIN_H]);
168
        return AVERROR(EINVAL);
169
    }
170
    return 0;
171
172
fail:
173
    av_log(NULL, AV_LOG_ERROR,
174
           "Error when evaluating the expression '%s'\n", expr);
175
    return ret;
176
}
177
178
static int config_output(AVFilterLink *outlink)
179
{
180
    AVFilterContext *ctx = outlink->src;
181
    int exact;
182
    // common timebase computation:
183
    AVRational tb1 = ctx->inputs[MAIN   ]->time_base;
184
    AVRational tb2 = ctx->inputs[OVERLAY]->time_base;
185
    AVRational *tb = &ctx->outputs[0]->time_base;
186
    exact = av_reduce(&tb->num, &tb->den,
187
                      av_gcd((int64_t)tb1.num * tb2.den,
188
                             (int64_t)tb2.num * tb1.den),
189
                      (int64_t)tb1.den * tb2.den, INT_MAX);
190
    av_log(ctx, AV_LOG_INFO,
191
           "main_tb:%d/%d overlay_tb:%d/%d -> tb:%d/%d exact:%d\n",
192
           tb1.num, tb1.den, tb2.num, tb2.den, tb->num, tb->den, exact);
193
    if (!exact)
194
        av_log(ctx, AV_LOG_WARNING,
195
               "Timestamp conversion inexact, timestamp information loss may occurr\n");
196
197
    outlink->w = ctx->inputs[MAIN]->w;
198
    outlink->h = ctx->inputs[MAIN]->h;
199
200
    return 0;
201
}
202
203
static AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms, int w, int h)
204
{
205
    return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
206
}
207
208
static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
209
{
210
    AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
211
    AVFilterContext *ctx = inlink->dst;
212
    OverlayContext *over = ctx->priv;
213
214
    inlink->dst->outputs[0]->out_buf = outpicref;
215 9b195435 Stefano Sabatini
    outpicref->pts = av_rescale_q(outpicref->pts, ctx->inputs[MAIN]->time_base,
216
                                  ctx->outputs[0]->time_base);
217 58935b25 Stefano Sabatini
218
    if (!over->overpicref || over->overpicref->pts < outpicref->pts) {
219
        AVFilterBufferRef *old = over->overpicref;
220
        over->overpicref = NULL;
221
        avfilter_request_frame(ctx->inputs[OVERLAY]);
222
        if (over->overpicref) {
223
            if (old)
224
                avfilter_unref_buffer(old);
225
        } else
226
            over->overpicref = old;
227
    }
228
229
    avfilter_start_frame(inlink->dst->outputs[0], outpicref);
230
}
231
232
static void start_frame_overlay(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
233
{
234
    AVFilterContext *ctx = inlink->dst;
235
    OverlayContext *over = ctx->priv;
236
237
    over->overpicref = inpicref;
238
    over->overpicref->pts = av_rescale_q(inpicref->pts, ctx->inputs[OVERLAY]->time_base,
239
                                         ctx->outputs[0]->time_base);
240
}
241
242
static void blend_slice(AVFilterContext *ctx,
243
                        AVFilterBufferRef *dst, AVFilterBufferRef *src,
244
                        int x, int y, int w, int h,
245
                        int slice_y, int slice_w, int slice_h)
246
{
247
    OverlayContext *over = ctx->priv;
248
    int i, j, k;
249
    int width, height;
250
    int overlay_end_y = y+h;
251
    int slice_end_y = slice_y+slice_h;
252
    int end_y, start_y;
253
254
    width = FFMIN(slice_w - x, w);
255
    end_y = FFMIN(slice_end_y, overlay_end_y);
256
    start_y = FFMAX(y, slice_y);
257
    height = end_y - start_y;
258
259
    if (dst->format == PIX_FMT_BGR24 || dst->format == PIX_FMT_RGB24) {
260
        uint8_t *dp = dst->data[0] + x * 3 + start_y * dst->linesize[0];
261
        uint8_t *sp = src->data[0];
262
        int b = dst->format == PIX_FMT_BGR24 ? 2 : 0;
263
        int r = dst->format == PIX_FMT_BGR24 ? 0 : 2;
264
        if (slice_y > y)
265
            sp += (slice_y - y) * src->linesize[0];
266
        for (i = 0; i < height; i++) {
267
            uint8_t *d = dp, *s = sp;
268
            for (j = 0; j < width; j++) {
269
                d[r] = (d[r] * (0xff - s[3]) + s[0] * s[3] + 128) >> 8;
270
                d[1] = (d[1] * (0xff - s[3]) + s[1] * s[3] + 128) >> 8;
271
                d[b] = (d[b] * (0xff - s[3]) + s[2] * s[3] + 128) >> 8;
272
                d += 3;
273
                s += 4;
274
            }
275
            dp += dst->linesize[0];
276
            sp += src->linesize[0];
277
        }
278
    } else {
279
        for (i = 0; i < 3; i++) {
280
            int hsub = i ? over->hsub : 0;
281
            int vsub = i ? over->vsub : 0;
282
            uint8_t *dp = dst->data[i] + (x >> hsub) +
283
                (start_y >> vsub) * dst->linesize[i];
284
            uint8_t *sp = src->data[i];
285
            uint8_t *ap = src->data[3];
286
            int wp = FFALIGN(width, 1<<hsub) >> hsub;
287
            int hp = FFALIGN(height, 1<<vsub) >> vsub;
288
            if (slice_y > y) {
289
                sp += ((slice_y - y) >> vsub) * src->linesize[i];
290
                ap += (slice_y - y) * src->linesize[3];
291
            }
292
            for (j = 0; j < hp; j++) {
293
                uint8_t *d = dp, *s = sp, *a = ap;
294
                for (k = 0; k < wp; k++) {
295
                    // average alpha for color components, improve quality
296
                    int alpha_v, alpha_h, alpha;
297
                    if (hsub && vsub && j+1 < hp && k+1 < wp) {
298
                        alpha = (a[0] + a[src->linesize[3]] +
299
                                 a[1] + a[src->linesize[3]+1]) >> 2;
300
                    } else if (hsub || vsub) {
301
                        alpha_h = hsub && k+1 < wp ?
302
                            (a[0] + a[1]) >> 1 : a[0];
303
                        alpha_v = vsub && j+1 < hp ?
304
                            (a[0] + a[src->linesize[3]]) >> 1 : a[0];
305
                        alpha = (alpha_v + alpha_h) >> 1;
306
                    } else
307
                        alpha = a[0];
308
                    *d = (*d * (0xff - alpha) + *s++ * alpha + 128) >> 8;
309
                    d++;
310
                    a += 1 << hsub;
311
                }
312
                dp += dst->linesize[i];
313
                sp += src->linesize[i];
314
                ap += (1 << vsub) * src->linesize[3];
315
            }
316
        }
317
    }
318
}
319
320
static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
321
{
322
    AVFilterContext *ctx = inlink->dst;
323
    AVFilterLink *outlink = ctx->outputs[0];
324
    AVFilterBufferRef *outpicref = outlink->out_buf;
325
    OverlayContext *over = ctx->priv;
326
327
    if (over->overpicref &&
328
        !(over->x >= outpicref->video->w || over->y >= outpicref->video->h ||
329
          y+h < over->y || y >= over->y + over->overpicref->video->h)) {
330
        blend_slice(ctx, outpicref, over->overpicref, over->x, over->y,
331
                    over->overpicref->video->w, over->overpicref->video->h,
332
                    y, outpicref->video->w, h);
333
    }
334
    avfilter_draw_slice(outlink, y, h, slice_dir);
335
}
336
337
static void end_frame(AVFilterLink *inlink)
338
{
339
    avfilter_end_frame(inlink->dst->outputs[0]);
340
    avfilter_unref_buffer(inlink->cur_buf);
341
}
342
343
static void null_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) { }
344
345
static void null_end_frame(AVFilterLink *inlink) { }
346
347
AVFilter avfilter_vf_overlay = {
348
    .name      = "overlay",
349
    .description = NULL_IF_CONFIG_SMALL("Overlay a video source on top of the input."),
350
351
    .init      = init,
352
    .uninit    = uninit,
353
354
    .priv_size = sizeof(OverlayContext),
355
356
    .query_formats = query_formats,
357
358
    .inputs    = (AVFilterPad[]) {{ .name            = "main",
359
                                    .type            = AVMEDIA_TYPE_VIDEO,
360
                                    .start_frame     = start_frame,
361
                                    .get_video_buffer= get_video_buffer,
362
                                    .config_props    = config_input_main,
363
                                    .draw_slice      = draw_slice,
364
                                    .end_frame       = end_frame,
365
                                    .min_perms       = AV_PERM_READ,
366
                                    .rej_perms       = AV_PERM_REUSE2|AV_PERM_PRESERVE, },
367
                                  { .name            = "overlay",
368
                                    .type            = AVMEDIA_TYPE_VIDEO,
369
                                    .start_frame     = start_frame_overlay,
370
                                    .config_props    = config_input_overlay,
371
                                    .draw_slice      = null_draw_slice,
372
                                    .end_frame       = null_end_frame,
373
                                    .min_perms       = AV_PERM_READ,
374
                                    .rej_perms       = AV_PERM_REUSE2, },
375
                                  { .name = NULL}},
376
    .outputs   = (AVFilterPad[]) {{ .name            = "default",
377
                                    .type            = AVMEDIA_TYPE_VIDEO,
378
                                    .config_props    = config_output, },
379
                                  { .name = NULL}},
380
};