2 * Copyright (c) 2016 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/imgutils.h"
22 #include "libavutil/pixdesc.h"
23 #include "libavutil/opt.h"
26 #include "framesync.h"
30 typedef struct PreMultiplyContext {
32 int width[4], height[4];
36 int half, depth, offset;
39 void (*premultiply[4])(const uint8_t *msrc, const uint8_t *asrc,
41 ptrdiff_t mlinesize, ptrdiff_t alinesize,
44 int half, int shift, int offset);
47 #define OFFSET(x) offsetof(PreMultiplyContext, x)
48 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
50 static const AVOption premultiply_options[] = {
51 { "planes", "set planes", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
55 AVFILTER_DEFINE_CLASS(premultiply);
57 static int query_formats(AVFilterContext *ctx)
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,
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,
70 return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
73 static void premultiply8(const uint8_t *msrc, const uint8_t *asrc,
75 ptrdiff_t mlinesize, ptrdiff_t alinesize,
78 int half, int shift, int offset)
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;
93 static void premultiply8yuv(const uint8_t *msrc, const uint8_t *asrc,
95 ptrdiff_t mlinesize, ptrdiff_t alinesize,
98 int half, int shift, int offset)
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;
113 static void premultiply8offset(const uint8_t *msrc, const uint8_t *asrc,
115 ptrdiff_t mlinesize, ptrdiff_t alinesize,
118 int half, int shift, int offset)
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;
133 static void premultiply16(const uint8_t *mmsrc, const uint8_t *aasrc,
135 ptrdiff_t mlinesize, ptrdiff_t alinesize,
138 int half, int shift, int offset)
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;
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;
150 dst += dlinesize / 2;
151 msrc += mlinesize / 2;
152 asrc += alinesize / 2;
156 static void premultiply16yuv(const uint8_t *mmsrc, const uint8_t *aasrc,
158 ptrdiff_t mlinesize, ptrdiff_t alinesize,
161 int half, int shift, int offset)
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;
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;
173 dst += dlinesize / 2;
174 msrc += mlinesize / 2;
175 asrc += alinesize / 2;
179 static void premultiply16offset(const uint8_t *mmsrc, const uint8_t *aasrc,
181 ptrdiff_t mlinesize, ptrdiff_t alinesize,
184 int half, int shift, int offset)
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;
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;
196 dst += dlinesize / 2;
197 msrc += mlinesize / 2;
198 asrc += alinesize / 2;
202 static int process_frame(FFFrameSync *fs)
204 AVFilterContext *ctx = fs->parent;
205 PreMultiplyContext *s = fs->opaque;
206 AVFilterLink *outlink = ctx->outputs[0];
207 AVFrame *out, *base, *alpha;
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)
214 if (ctx->is_disabled) {
215 out = av_frame_clone(base);
217 return AVERROR(ENOMEM);
219 int p, full, limited;
221 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
223 return AVERROR(ENOMEM);
224 av_frame_copy_props(out, base);
226 full = base->color_range == AVCOL_RANGE_JPEG;
227 limited = base->color_range == AVCOL_RANGE_MPEG;
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;
235 case AV_PIX_FMT_YUVJ444P:
236 s->premultiply[0] = premultiply8;
237 s->premultiply[1] = premultiply8yuv;
238 s->premultiply[2] = premultiply8yuv;
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;
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;
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;
263 case AV_PIX_FMT_GRAY8:
264 s->premultiply[0] = limited ? premultiply8offset : premultiply8;
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;
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]);
280 s->premultiply[p](base->data[p], alpha->data[0],
282 base->linesize[p], alpha->linesize[0],
284 s->width[p], s->height[p],
285 s->half, s->depth, s->offset);
288 out->pts = av_rescale_q(base->pts, s->fs.time_base, outlink->time_base);
290 return ff_filter_frame(outlink, out);
293 static int config_input(AVFilterLink *inlink)
295 AVFilterContext *ctx = inlink->dst;
296 PreMultiplyContext *s = ctx->priv;
297 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
300 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
302 if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
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;
312 s->depth = desc->comp[0].depth;
313 s->half = (1 << s->depth) / 2;
314 s->offset = 16 << (s->depth - 8);
319 static int config_output(AVFilterLink *outlink)
321 AVFilterContext *ctx = outlink->src;
322 PreMultiplyContext *s = ctx->priv;
323 AVFilterLink *base = ctx->inputs[0];
324 AVFilterLink *alpha = ctx->inputs[1];
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);
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);
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;
348 if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0)
352 in[0].time_base = base->time_base;
353 in[1].time_base = alpha->time_base;
355 in[0].before = EXT_STOP;
356 in[0].after = EXT_INFINITY;
358 in[1].before = EXT_STOP;
359 in[1].after = EXT_INFINITY;
361 s->fs.on_event = process_frame;
363 return ff_framesync_configure(&s->fs);
366 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
368 PreMultiplyContext *s = inlink->dst->priv;
369 return ff_framesync_filter_frame(&s->fs, inlink, buf);
372 static int request_frame(AVFilterLink *outlink)
374 PreMultiplyContext *s = outlink->src->priv;
375 return ff_framesync_request_frame(&s->fs, outlink);
378 static av_cold void uninit(AVFilterContext *ctx)
380 PreMultiplyContext *s = ctx->priv;
382 ff_framesync_uninit(&s->fs);
385 static const AVFilterPad premultiply_inputs[] = {
388 .type = AVMEDIA_TYPE_VIDEO,
389 .filter_frame = filter_frame,
390 .config_props = config_input,
394 .type = AVMEDIA_TYPE_VIDEO,
395 .filter_frame = filter_frame,
400 static const AVFilterPad premultiply_outputs[] = {
403 .type = AVMEDIA_TYPE_VIDEO,
404 .config_props = config_output,
405 .request_frame = request_frame,
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),
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,