GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavfilter/vf_alphamerge.c Lines: 71 86 82.6 %
Date: 2020-08-14 10:39:37 Branches: 42 62 67.7 %

Line Branch Exec Source
1
/*
2
 * Copyright (c) 2012 Steven Robertson
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
/**
22
 * @file
23
 * copy an alpha component from another video's luma
24
 */
25
26
#include <string.h>
27
28
#include "libavutil/imgutils.h"
29
#include "libavutil/opt.h"
30
#include "libavutil/pixfmt.h"
31
#include "avfilter.h"
32
#include "drawutils.h"
33
#include "formats.h"
34
#include "filters.h"
35
#include "internal.h"
36
#include "video.h"
37
38
enum { Y, U, V, A };
39
40
typedef struct AlphaMergeContext {
41
    const AVClass *class;
42
43
    int is_packed_rgb;
44
    uint8_t rgba_map[4];
45
    AVFrame *main_frame;
46
    AVFrame *alpha_frame;
47
} AlphaMergeContext;
48
49
4
static int query_formats(AVFilterContext *ctx)
50
{
51
    static const enum AVPixelFormat main_fmts[] = {
52
        AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
53
        AV_PIX_FMT_GBRAP,
54
        AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
55
        AV_PIX_FMT_NONE
56
    };
57
    static const enum AVPixelFormat alpha_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
58
4
    AVFilterFormats *main_formats = NULL, *alpha_formats = NULL;
59
    int ret;
60
61
4
    if (!(main_formats = ff_make_format_list(main_fmts)) ||
62
4
        !(alpha_formats = ff_make_format_list(alpha_fmts))) {
63
            ret = AVERROR(ENOMEM);
64
            goto fail;
65
        }
66

8
    if ((ret = ff_formats_ref(main_formats , &ctx->inputs[0]->out_formats)) < 0 ||
67
8
        (ret = ff_formats_ref(alpha_formats, &ctx->inputs[1]->out_formats)) < 0 ||
68
4
        (ret = ff_formats_ref(main_formats , &ctx->outputs[0]->in_formats)) < 0)
69
            goto fail;
70
4
    return 0;
71
fail:
72
    if (main_formats)
73
        av_freep(&main_formats->formats);
74
    av_freep(&main_formats);
75
    if (alpha_formats)
76
        av_freep(&alpha_formats->formats);
77
    av_freep(&alpha_formats);
78
    return ret;
79
}
80
81
4
static int config_input_main(AVFilterLink *inlink)
82
{
83
4
    AlphaMergeContext *s = inlink->dst->priv;
84
4
    s->is_packed_rgb =
85
6
        ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0 &&
86
2
        inlink->format != AV_PIX_FMT_GBRAP;
87
4
    return 0;
88
}
89
90
4
static int config_output(AVFilterLink *outlink)
91
{
92
4
    AVFilterContext *ctx = outlink->src;
93
4
    AVFilterLink *mainlink = ctx->inputs[0];
94
4
    AVFilterLink *alphalink = ctx->inputs[1];
95

4
    if (mainlink->w != alphalink->w || mainlink->h != alphalink->h) {
96
        av_log(ctx, AV_LOG_ERROR,
97
               "Input frame sizes do not match (%dx%d vs %dx%d).\n",
98
               mainlink->w, mainlink->h,
99
               alphalink->w, alphalink->h);
100
        return AVERROR(EINVAL);
101
    }
102
103
4
    outlink->w = mainlink->w;
104
4
    outlink->h = mainlink->h;
105
4
    outlink->time_base = mainlink->time_base;
106
4
    outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
107
4
    outlink->frame_rate = mainlink->frame_rate;
108
4
    return 0;
109
}
110
111
200
static void draw_frame(AVFilterContext *ctx,
112
                       AVFrame *main_buf,
113
                       AVFrame *alpha_buf)
114
{
115
200
    AlphaMergeContext *s = ctx->priv;
116
200
    int h = main_buf->height;
117
118
200
    if (s->is_packed_rgb) {
119
        int x, y;
120
        uint8_t *pin, *pout;
121
28900
        for (y = 0; y < h; y++) {
122
28800
            pin = alpha_buf->data[0] + y * alpha_buf->linesize[0];
123
28800
            pout = main_buf->data[0] + y * main_buf->linesize[0] + s->rgba_map[A];
124
10166400
            for (x = 0; x < main_buf->width; x++) {
125
10137600
                *pout = *pin;
126
10137600
                pin += 1;
127
10137600
                pout += 4;
128
            }
129
        }
130
    } else {
131
100
        const int main_linesize = main_buf->linesize[A];
132
100
        const int alpha_linesize = alpha_buf->linesize[Y];
133
100
        av_image_copy_plane(main_buf->data[A], main_linesize,
134
100
                            alpha_buf->data[Y], alpha_linesize,
135
                            FFMIN(main_linesize, alpha_linesize), alpha_buf->height);
136
    }
137
200
}
138
139
506
static int activate(AVFilterContext *ctx)
140
{
141
506
    AVFilterLink *outlink = ctx->outputs[0];
142
506
    AlphaMergeContext *s = ctx->priv;
143
    int ret;
144
145

510
    FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
146
147
504
    if (!s->main_frame) {
148
404
        ret = ff_inlink_consume_frame(ctx->inputs[0], &s->main_frame);
149
404
        if (ret < 0)
150
            return ret;
151
    }
152
153
504
    if (!s->alpha_frame) {
154
504
        ret = ff_inlink_consume_frame(ctx->inputs[1], &s->alpha_frame);
155
504
        if (ret < 0)
156
            return ret;
157
    }
158
159

504
    if (s->main_frame && s->alpha_frame) {
160
200
        if (!ctx->is_disabled)
161
200
            draw_frame(ctx, s->main_frame, s->alpha_frame);
162
200
        ret = ff_filter_frame(outlink, s->main_frame);
163
200
        av_frame_free(&s->alpha_frame);
164
200
        s->main_frame = NULL;
165
200
        return ret;
166
    }
167
168
304
    FF_FILTER_FORWARD_STATUS(ctx->inputs[0], outlink);
169
300
    FF_FILTER_FORWARD_STATUS(ctx->inputs[1], outlink);
170
171

598
    if (ff_outlink_frame_wanted(ctx->outputs[0]) &&
172
298
        !ff_outlink_get_status(ctx->inputs[0]) &&
173
298
        !s->main_frame) {
174
200
        ff_inlink_request_frame(ctx->inputs[0]);
175
200
        return 0;
176
    }
177
178

198
    if (ff_outlink_frame_wanted(ctx->outputs[0]) &&
179
98
        !ff_outlink_get_status(ctx->inputs[1]) &&
180
98
        !s->alpha_frame) {
181
98
        ff_inlink_request_frame(ctx->inputs[1]);
182
98
        return 0;
183
    }
184
185
2
    return FFERROR_NOT_READY;
186
}
187
188
static const AVFilterPad alphamerge_inputs[] = {
189
    {
190
        .name             = "main",
191
        .type             = AVMEDIA_TYPE_VIDEO,
192
        .config_props     = config_input_main,
193
        .needs_writable   = 1,
194
    },{
195
        .name             = "alpha",
196
        .type             = AVMEDIA_TYPE_VIDEO,
197
    },
198
    { NULL }
199
};
200
201
static const AVFilterPad alphamerge_outputs[] = {
202
    {
203
        .name          = "default",
204
        .type          = AVMEDIA_TYPE_VIDEO,
205
        .config_props  = config_output,
206
    },
207
    { NULL }
208
};
209
210
static const AVOption alphamerge_options[] = {
211
    { NULL }
212
};
213
214
AVFILTER_DEFINE_CLASS(alphamerge);
215
216
AVFilter ff_vf_alphamerge = {
217
    .name           = "alphamerge",
218
    .description    = NULL_IF_CONFIG_SMALL("Copy the luma value of the second "
219
                      "input into the alpha channel of the first input."),
220
    .priv_size      = sizeof(AlphaMergeContext),
221
    .priv_class     = &alphamerge_class,
222
    .query_formats  = query_formats,
223
    .inputs         = alphamerge_inputs,
224
    .outputs        = alphamerge_outputs,
225
    .activate       = activate,
226
    .flags          = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
227
};