OSDN Git Service

libavfilter/scale2ref: Add constants for the primary input
[android-x86/external-ffmpeg.git] / libavfilter / vf_premultiply.c
1 /*
2  * Copyright (c) 2016 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 "framesync.h"
27 #include "internal.h"
28 #include "video.h"
29
30 typedef struct PreMultiplyContext {
31     const AVClass *class;
32     int width[4], height[4];
33     int linesize[4];
34     int nb_planes;
35     int planes;
36     int half, depth, offset;
37     FFFrameSync fs;
38
39     void (*premultiply[4])(const uint8_t *msrc, const uint8_t *asrc,
40                            uint8_t *dst,
41                            ptrdiff_t mlinesize, ptrdiff_t alinesize,
42                            ptrdiff_t dlinesize,
43                            int w, int h,
44                            int half, int shift, int offset);
45 } PreMultiplyContext;
46
47 #define OFFSET(x) offsetof(PreMultiplyContext, x)
48 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
49
50 static const AVOption premultiply_options[] = {
51     { "planes", "set planes", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
52     { NULL }
53 };
54
55 AVFILTER_DEFINE_CLASS(premultiply);
56
57 static int query_formats(AVFilterContext *ctx)
58 {
59     static const enum AVPixelFormat pix_fmts[] = {
60         AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
61         AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV444P10,
62         AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV444P14,
63         AV_PIX_FMT_YUV444P16,
64         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
65         AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
66         AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY16,
67         AV_PIX_FMT_NONE
68     };
69
70     return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
71 }
72
73 static void premultiply8(const uint8_t *msrc, const uint8_t *asrc,
74                          uint8_t *dst,
75                          ptrdiff_t mlinesize, ptrdiff_t alinesize,
76                          ptrdiff_t dlinesize,
77                          int w, int h,
78                          int half, int shift, int offset)
79 {
80     int x, y;
81
82     for (y = 0; y < h; y++) {
83         for (x = 0; x < w; x++) {
84             dst[x] = ((msrc[x] * (((asrc[x] >> 1) & 1) + asrc[x])) + 128) >> 8;
85         }
86
87         dst  += dlinesize;
88         msrc += mlinesize;
89         asrc += alinesize;
90     }
91 }
92
93 static void premultiply8yuv(const uint8_t *msrc, const uint8_t *asrc,
94                             uint8_t *dst,
95                             ptrdiff_t mlinesize, ptrdiff_t alinesize,
96                             ptrdiff_t dlinesize,
97                             int w, int h,
98                             int half, int shift, int offset)
99 {
100     int x, y;
101
102     for (y = 0; y < h; y++) {
103         for (x = 0; x < w; x++) {
104             dst[x] = ((((msrc[x] - 128) * (((asrc[x] >> 1) & 1) + asrc[x]))) >> 8) + 128;
105         }
106
107         dst  += dlinesize;
108         msrc += mlinesize;
109         asrc += alinesize;
110     }
111 }
112
113 static void premultiply8offset(const uint8_t *msrc, const uint8_t *asrc,
114                                uint8_t *dst,
115                                ptrdiff_t mlinesize, ptrdiff_t alinesize,
116                                ptrdiff_t dlinesize,
117                                int w, int h,
118                                int half, int shift, int offset)
119 {
120     int x, y;
121
122     for (y = 0; y < h; y++) {
123         for (x = 0; x < w; x++) {
124             dst[x] = ((((msrc[x] - offset) * (((asrc[x] >> 1) & 1) + asrc[x])) + 128) >> 8) + offset;
125         }
126
127         dst  += dlinesize;
128         msrc += mlinesize;
129         asrc += alinesize;
130     }
131 }
132
133 static void premultiply16(const uint8_t *mmsrc, const uint8_t *aasrc,
134                           uint8_t *ddst,
135                           ptrdiff_t mlinesize, ptrdiff_t alinesize,
136                           ptrdiff_t dlinesize,
137                           int w, int h,
138                           int half, int shift, int offset)
139 {
140     const uint16_t *msrc = (const uint16_t *)mmsrc;
141     const uint16_t *asrc = (const uint16_t *)aasrc;
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] = ((msrc[x] * (((asrc[x] >> 1) & 1) + asrc[x])) + half) >> shift;
148         }
149
150         dst  += dlinesize / 2;
151         msrc += mlinesize / 2;
152         asrc += alinesize / 2;
153     }
154 }
155
156 static void premultiply16yuv(const uint8_t *mmsrc, const uint8_t *aasrc,
157                              uint8_t *ddst,
158                              ptrdiff_t mlinesize, ptrdiff_t alinesize,
159                              ptrdiff_t dlinesize,
160                              int w, int h,
161                              int half, int shift, int offset)
162 {
163     const uint16_t *msrc = (const uint16_t *)mmsrc;
164     const uint16_t *asrc = (const uint16_t *)aasrc;
165     uint16_t *dst = (uint16_t *)ddst;
166     int x, y;
167
168     for (y = 0; y < h; y++) {
169         for (x = 0; x < w; x++) {
170             dst[x] = ((((msrc[x] - half) * (((asrc[x] >> 1) & 1) + asrc[x]))) >> shift) + half;
171         }
172
173         dst  += dlinesize / 2;
174         msrc += mlinesize / 2;
175         asrc += alinesize / 2;
176     }
177 }
178
179 static void premultiply16offset(const uint8_t *mmsrc, const uint8_t *aasrc,
180                                 uint8_t *ddst,
181                                 ptrdiff_t mlinesize, ptrdiff_t alinesize,
182                                 ptrdiff_t dlinesize,
183                                 int w, int h,
184                                 int half, int shift, int offset)
185 {
186     const uint16_t *msrc = (const uint16_t *)mmsrc;
187     const uint16_t *asrc = (const uint16_t *)aasrc;
188     uint16_t *dst = (uint16_t *)ddst;
189     int x, y;
190
191     for (y = 0; y < h; y++) {
192         for (x = 0; x < w; x++) {
193             dst[x] = ((((msrc[x] - offset) * (((asrc[x] >> 1) & 1) + asrc[x])) + half) >> shift) + offset;
194         }
195
196         dst  += dlinesize / 2;
197         msrc += mlinesize / 2;
198         asrc += alinesize / 2;
199     }
200 }
201
202 static int process_frame(FFFrameSync *fs)
203 {
204     AVFilterContext *ctx = fs->parent;
205     PreMultiplyContext *s = fs->opaque;
206     AVFilterLink *outlink = ctx->outputs[0];
207     AVFrame *out, *base, *alpha;
208     int ret;
209
210     if ((ret = ff_framesync_get_frame(&s->fs, 0, &base,  0)) < 0 ||
211         (ret = ff_framesync_get_frame(&s->fs, 1, &alpha, 0)) < 0)
212         return ret;
213
214     if (ctx->is_disabled) {
215         out = av_frame_clone(base);
216         if (!out)
217             return AVERROR(ENOMEM);
218     } else {
219         int p, full, limited;
220
221         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
222         if (!out)
223             return AVERROR(ENOMEM);
224         av_frame_copy_props(out, base);
225
226         full = base->color_range == AVCOL_RANGE_JPEG;
227         limited = base->color_range == AVCOL_RANGE_MPEG;
228
229         switch (outlink->format) {
230         case AV_PIX_FMT_YUV444P:
231             s->premultiply[0] = full ? premultiply8 : premultiply8offset;
232             s->premultiply[1] = premultiply8yuv;
233             s->premultiply[2] = premultiply8yuv;
234             break;
235         case AV_PIX_FMT_YUVJ444P:
236             s->premultiply[0] = premultiply8;
237             s->premultiply[1] = premultiply8yuv;
238             s->premultiply[2] = premultiply8yuv;
239             break;
240         case AV_PIX_FMT_GBRP:
241             s->premultiply[0] = limited ? premultiply8offset : premultiply8;
242             s->premultiply[1] = limited ? premultiply8offset : premultiply8;
243             s->premultiply[2] = limited ? premultiply8offset : premultiply8;
244             break;
245         case AV_PIX_FMT_YUV444P9:
246         case AV_PIX_FMT_YUV444P10:
247         case AV_PIX_FMT_YUV444P12:
248         case AV_PIX_FMT_YUV444P14:
249         case AV_PIX_FMT_YUV444P16:
250             s->premultiply[0] = full ? premultiply16 : premultiply16offset;
251             s->premultiply[1] = premultiply16yuv;
252             s->premultiply[2] = premultiply16yuv;
253             break;
254         case AV_PIX_FMT_GBRP9:
255         case AV_PIX_FMT_GBRP10:
256         case AV_PIX_FMT_GBRP12:
257         case AV_PIX_FMT_GBRP14:
258         case AV_PIX_FMT_GBRP16:
259             s->premultiply[0] = limited ? premultiply16offset : premultiply16;
260             s->premultiply[1] = limited ? premultiply16offset : premultiply16;
261             s->premultiply[2] = limited ? premultiply16offset : premultiply16;
262             break;
263         case AV_PIX_FMT_GRAY8:
264             s->premultiply[0] = limited ? premultiply8offset : premultiply8;
265             break;
266         case AV_PIX_FMT_GRAY10:
267         case AV_PIX_FMT_GRAY12:
268         case AV_PIX_FMT_GRAY16:
269             s->premultiply[0] = limited ? premultiply16offset : premultiply16;
270             break;
271         }
272
273         for (p = 0; p < s->nb_planes; p++) {
274             if (!((1 << p) & s->planes)) {
275                 av_image_copy_plane(out->data[p], out->linesize[p], base->data[p], base->linesize[p],
276                                     s->linesize[p], s->height[p]);
277                 continue;
278             }
279
280             s->premultiply[p](base->data[p], alpha->data[0],
281                               out->data[p],
282                               base->linesize[p], alpha->linesize[0],
283                               out->linesize[p],
284                               s->width[p], s->height[p],
285                               s->half, s->depth, s->offset);
286         }
287     }
288     out->pts = av_rescale_q(base->pts, s->fs.time_base, outlink->time_base);
289
290     return ff_filter_frame(outlink, out);
291 }
292
293 static int config_input(AVFilterLink *inlink)
294 {
295     AVFilterContext *ctx = inlink->dst;
296     PreMultiplyContext *s = ctx->priv;
297     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
298     int vsub, hsub, ret;
299
300     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
301
302     if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
303         return ret;
304
305     hsub = desc->log2_chroma_w;
306     vsub = desc->log2_chroma_h;
307     s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
308     s->height[0] = s->height[3] = inlink->h;
309     s->width[1]  = s->width[2]  = AV_CEIL_RSHIFT(inlink->w, hsub);
310     s->width[0]  = s->width[3]  = inlink->w;
311
312     s->depth = desc->comp[0].depth;
313     s->half = (1 << s->depth) / 2;
314     s->offset = 16 << (s->depth - 8);
315
316     return 0;
317 }
318
319 static int config_output(AVFilterLink *outlink)
320 {
321     AVFilterContext *ctx = outlink->src;
322     PreMultiplyContext *s = ctx->priv;
323     AVFilterLink *base = ctx->inputs[0];
324     AVFilterLink *alpha = ctx->inputs[1];
325     FFFrameSyncIn *in;
326     int ret;
327
328     if (base->format != alpha->format) {
329         av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
330         return AVERROR(EINVAL);
331     }
332     if (base->w                       != alpha->w ||
333         base->h                       != alpha->h) {
334         av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
335                "(size %dx%d) do not match the corresponding "
336                "second input link %s parameters (%dx%d) ",
337                ctx->input_pads[0].name, base->w, base->h,
338                ctx->input_pads[1].name, alpha->w, alpha->h);
339         return AVERROR(EINVAL);
340     }
341
342     outlink->w = base->w;
343     outlink->h = base->h;
344     outlink->time_base = base->time_base;
345     outlink->sample_aspect_ratio = base->sample_aspect_ratio;
346     outlink->frame_rate = base->frame_rate;
347
348     if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0)
349         return ret;
350
351     in = s->fs.in;
352     in[0].time_base = base->time_base;
353     in[1].time_base = alpha->time_base;
354     in[0].sync   = 1;
355     in[0].before = EXT_STOP;
356     in[0].after  = EXT_INFINITY;
357     in[1].sync   = 1;
358     in[1].before = EXT_STOP;
359     in[1].after  = EXT_INFINITY;
360     s->fs.opaque   = s;
361     s->fs.on_event = process_frame;
362
363     return ff_framesync_configure(&s->fs);
364 }
365
366 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
367 {
368     PreMultiplyContext *s = inlink->dst->priv;
369     return ff_framesync_filter_frame(&s->fs, inlink, buf);
370 }
371
372 static int request_frame(AVFilterLink *outlink)
373 {
374     PreMultiplyContext *s = outlink->src->priv;
375     return ff_framesync_request_frame(&s->fs, outlink);
376 }
377
378 static av_cold void uninit(AVFilterContext *ctx)
379 {
380     PreMultiplyContext *s = ctx->priv;
381
382     ff_framesync_uninit(&s->fs);
383 }
384
385 static const AVFilterPad premultiply_inputs[] = {
386     {
387         .name         = "main",
388         .type         = AVMEDIA_TYPE_VIDEO,
389         .filter_frame = filter_frame,
390         .config_props = config_input,
391     },
392     {
393         .name         = "alpha",
394         .type         = AVMEDIA_TYPE_VIDEO,
395         .filter_frame = filter_frame,
396     },
397     { NULL }
398 };
399
400 static const AVFilterPad premultiply_outputs[] = {
401     {
402         .name          = "default",
403         .type          = AVMEDIA_TYPE_VIDEO,
404         .config_props  = config_output,
405         .request_frame = request_frame,
406     },
407     { NULL }
408 };
409
410 AVFilter ff_vf_premultiply = {
411     .name          = "premultiply",
412     .description   = NULL_IF_CONFIG_SMALL("PreMultiply first stream with first plane of second stream."),
413     .priv_size     = sizeof(PreMultiplyContext),
414     .uninit        = uninit,
415     .query_formats = query_formats,
416     .inputs        = premultiply_inputs,
417     .outputs       = premultiply_outputs,
418     .priv_class    = &premultiply_class,
419     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
420 };