OSDN Git Service

libavfilter/scale2ref: Add constants for the primary input
[android-x86/external-ffmpeg.git] / libavfilter / vf_maskedmerge.c
1 /*
2  * Copyright (c) 2015 Paul B Mahol
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 #include "libavutil/imgutils.h"
22 #include "libavutil/pixdesc.h"
23 #include "libavutil/opt.h"
24 #include "avfilter.h"
25 #include "formats.h"
26 #include "internal.h"
27 #include "video.h"
28 #include "maskedmerge.h"
29
30 #define OFFSET(x) offsetof(MaskedMergeContext, x)
31 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
32
33 static const AVOption maskedmerge_options[] = {
34     { "planes", "set planes", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
35     { NULL }
36 };
37
38 AVFILTER_DEFINE_CLASS(maskedmerge);
39
40 static int query_formats(AVFilterContext *ctx)
41 {
42     static const enum AVPixelFormat pix_fmts[] = {
43         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
44         AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
45         AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
46         AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
47         AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
48         AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
49         AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
50         AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
51         AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
52         AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
53         AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
54         AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
55         AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
56         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
57         AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
58         AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
59         AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY16,
60         AV_PIX_FMT_NONE
61     };
62
63     return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
64 }
65
66 static int process_frame(FFFrameSync *fs)
67 {
68     AVFilterContext *ctx = fs->parent;
69     MaskedMergeContext *s = fs->opaque;
70     AVFilterLink *outlink = ctx->outputs[0];
71     AVFrame *out, *base, *overlay, *mask;
72     int ret;
73
74     if ((ret = ff_framesync_get_frame(&s->fs, 0, &base,    0)) < 0 ||
75         (ret = ff_framesync_get_frame(&s->fs, 1, &overlay, 0)) < 0 ||
76         (ret = ff_framesync_get_frame(&s->fs, 2, &mask,    0)) < 0)
77         return ret;
78
79     if (ctx->is_disabled) {
80         out = av_frame_clone(base);
81         if (!out)
82             return AVERROR(ENOMEM);
83     } else {
84         int p;
85
86         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
87         if (!out)
88             return AVERROR(ENOMEM);
89         av_frame_copy_props(out, base);
90
91         for (p = 0; p < s->nb_planes; p++) {
92             if (!((1 << p) & s->planes)) {
93                 av_image_copy_plane(out->data[p], out->linesize[p], base->data[p], base->linesize[p],
94                                     s->linesize[p], s->height[p]);
95                 continue;
96             }
97
98             s->maskedmerge(base->data[p], overlay->data[p],
99                            mask->data[p], out->data[p],
100                            base->linesize[p], overlay->linesize[p],
101                            mask->linesize[p], out->linesize[p],
102                            s->width[p], s->height[p],
103                            s->half, s->depth);
104         }
105     }
106     out->pts = av_rescale_q(base->pts, s->fs.time_base, outlink->time_base);
107
108     return ff_filter_frame(outlink, out);
109 }
110
111 static void maskedmerge8(const uint8_t *bsrc, const uint8_t *osrc,
112                          const uint8_t *msrc, uint8_t *dst,
113                          ptrdiff_t blinesize, ptrdiff_t olinesize,
114                          ptrdiff_t mlinesize, ptrdiff_t dlinesize,
115                          int w, int h,
116                          int half, int shift)
117 {
118     int x, y;
119
120     for (y = 0; y < h; y++) {
121         for (x = 0; x < w; x++) {
122             dst[x] = bsrc[x] + ((msrc[x] * (osrc[x] - bsrc[x]) + 128) >> 8);
123         }
124
125         dst  += dlinesize;
126         bsrc += blinesize;
127         osrc += olinesize;
128         msrc += mlinesize;
129     }
130 }
131
132 static void maskedmerge16(const uint8_t *bbsrc, const uint8_t *oosrc,
133                           const uint8_t *mmsrc, uint8_t *ddst,
134                           ptrdiff_t blinesize, ptrdiff_t olinesize,
135                           ptrdiff_t mlinesize, ptrdiff_t dlinesize,
136                           int w, int h,
137                           int half, int shift)
138 {
139     const uint16_t *bsrc = (const uint16_t *)bbsrc;
140     const uint16_t *osrc = (const uint16_t *)oosrc;
141     const uint16_t *msrc = (const uint16_t *)mmsrc;
142     uint16_t *dst = (uint16_t *)ddst;
143     int x, y;
144
145     for (y = 0; y < h; y++) {
146         for (x = 0; x < w; x++) {
147             dst[x] = bsrc[x] + ((msrc[x] * (osrc[x] - bsrc[x]) + half) >> shift);
148         }
149
150         dst  += dlinesize / 2;
151         bsrc += blinesize / 2;
152         osrc += olinesize / 2;
153         msrc += mlinesize / 2;
154     }
155 }
156
157 static int config_input(AVFilterLink *inlink)
158 {
159     AVFilterContext *ctx = inlink->dst;
160     MaskedMergeContext *s = ctx->priv;
161     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
162     int vsub, hsub;
163
164     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
165
166     hsub = desc->log2_chroma_w;
167     vsub = desc->log2_chroma_h;
168     s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
169     s->height[0] = s->height[3] = inlink->h;
170     s->width[1]  = s->width[2]  = AV_CEIL_RSHIFT(inlink->w, hsub);
171     s->width[0]  = s->width[3]  = inlink->w;
172
173     s->depth = desc->comp[0].depth;
174     s->half = (1 << s->depth) / 2;
175
176     if (desc->comp[0].depth == 8)
177         s->maskedmerge = maskedmerge8;
178     else
179         s->maskedmerge = maskedmerge16;
180
181     if (ARCH_X86)
182         ff_maskedmerge_init_x86(s);
183
184     return 0;
185 }
186
187 static int config_output(AVFilterLink *outlink)
188 {
189     AVFilterContext *ctx = outlink->src;
190     MaskedMergeContext *s = ctx->priv;
191     AVFilterLink *base = ctx->inputs[0];
192     AVFilterLink *overlay = ctx->inputs[1];
193     AVFilterLink *mask = ctx->inputs[2];
194     FFFrameSyncIn *in;
195     int ret;
196
197     if (base->format != overlay->format ||
198         base->format != mask->format) {
199         av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
200         return AVERROR(EINVAL);
201     }
202     if (base->w                       != overlay->w ||
203         base->h                       != overlay->h ||
204         base->sample_aspect_ratio.num != overlay->sample_aspect_ratio.num ||
205         base->sample_aspect_ratio.den != overlay->sample_aspect_ratio.den ||
206         base->w                       != mask->w ||
207         base->h                       != mask->h ||
208         base->sample_aspect_ratio.num != mask->sample_aspect_ratio.num ||
209         base->sample_aspect_ratio.den != mask->sample_aspect_ratio.den) {
210         av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
211                "(size %dx%d, SAR %d:%d) do not match the corresponding "
212                "second input link %s parameters (%dx%d, SAR %d:%d) "
213                "and/or third input link %s parameters (%dx%d, SAR %d:%d)\n",
214                ctx->input_pads[0].name, base->w, base->h,
215                base->sample_aspect_ratio.num,
216                base->sample_aspect_ratio.den,
217                ctx->input_pads[1].name, overlay->w, overlay->h,
218                overlay->sample_aspect_ratio.num,
219                overlay->sample_aspect_ratio.den,
220                ctx->input_pads[2].name, mask->w, mask->h,
221                mask->sample_aspect_ratio.num,
222                mask->sample_aspect_ratio.den);
223         return AVERROR(EINVAL);
224     }
225
226     outlink->w = base->w;
227     outlink->h = base->h;
228     outlink->time_base = base->time_base;
229     outlink->sample_aspect_ratio = base->sample_aspect_ratio;
230     outlink->frame_rate = base->frame_rate;
231
232     if ((ret = av_image_fill_linesizes(s->linesize, outlink->format, outlink->w)) < 0)
233         return ret;
234
235     if ((ret = ff_framesync_init(&s->fs, ctx, 3)) < 0)
236         return ret;
237
238     in = s->fs.in;
239     in[0].time_base = base->time_base;
240     in[1].time_base = overlay->time_base;
241     in[2].time_base = mask->time_base;
242     in[0].sync   = 1;
243     in[0].before = EXT_STOP;
244     in[0].after  = EXT_INFINITY;
245     in[1].sync   = 1;
246     in[1].before = EXT_STOP;
247     in[1].after  = EXT_INFINITY;
248     in[2].sync   = 1;
249     in[2].before = EXT_STOP;
250     in[2].after  = EXT_INFINITY;
251     s->fs.opaque   = s;
252     s->fs.on_event = process_frame;
253
254     return ff_framesync_configure(&s->fs);
255 }
256
257 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
258 {
259     MaskedMergeContext *s = inlink->dst->priv;
260     return ff_framesync_filter_frame(&s->fs, inlink, buf);
261 }
262
263 static int request_frame(AVFilterLink *outlink)
264 {
265     MaskedMergeContext *s = outlink->src->priv;
266     return ff_framesync_request_frame(&s->fs, outlink);
267 }
268
269 static av_cold void uninit(AVFilterContext *ctx)
270 {
271     MaskedMergeContext *s = ctx->priv;
272
273     ff_framesync_uninit(&s->fs);
274 }
275
276 static const AVFilterPad maskedmerge_inputs[] = {
277     {
278         .name         = "base",
279         .type         = AVMEDIA_TYPE_VIDEO,
280         .filter_frame = filter_frame,
281         .config_props = config_input,
282     },
283     {
284         .name         = "overlay",
285         .type         = AVMEDIA_TYPE_VIDEO,
286         .filter_frame = filter_frame,
287     },
288     {
289         .name         = "mask",
290         .type         = AVMEDIA_TYPE_VIDEO,
291         .filter_frame = filter_frame,
292     },
293     { NULL }
294 };
295
296 static const AVFilterPad maskedmerge_outputs[] = {
297     {
298         .name          = "default",
299         .type          = AVMEDIA_TYPE_VIDEO,
300         .config_props  = config_output,
301         .request_frame = request_frame,
302     },
303     { NULL }
304 };
305
306 AVFilter ff_vf_maskedmerge = {
307     .name          = "maskedmerge",
308     .description   = NULL_IF_CONFIG_SMALL("Merge first stream with second stream using third stream as mask."),
309     .priv_size     = sizeof(MaskedMergeContext),
310     .uninit        = uninit,
311     .query_formats = query_formats,
312     .inputs        = maskedmerge_inputs,
313     .outputs       = maskedmerge_outputs,
314     .priv_class    = &maskedmerge_class,
315     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
316 };