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"
27 #include "framesync.h"
31 typedef struct PreMultiplyContext {
33 int width[4], height[4];
39 int half, depth, offset, max;
42 void (*premultiply[4])(const uint8_t *msrc, const uint8_t *asrc,
44 ptrdiff_t mlinesize, ptrdiff_t alinesize,
47 int half, int shift, int offset);
50 #define OFFSET(x) offsetof(PreMultiplyContext, x)
51 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
53 static const AVOption options[] = {
54 { "planes", "set planes", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
55 { "inplace","enable inplace mode", OFFSET(inplace), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
59 #define premultiply_options options
60 AVFILTER_DEFINE_CLASS(premultiply);
62 static int query_formats(AVFilterContext *ctx)
64 PreMultiplyContext *s = ctx->priv;
66 static const enum AVPixelFormat no_alpha_pix_fmts[] = {
67 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
68 AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV444P10,
69 AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV444P14,
71 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
72 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
73 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY16,
77 static const enum AVPixelFormat alpha_pix_fmts[] = {
79 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P16,
81 AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
85 return ff_set_common_formats(ctx, ff_make_format_list(s->inplace ? alpha_pix_fmts : no_alpha_pix_fmts));
88 static void premultiply8(const uint8_t *msrc, const uint8_t *asrc,
90 ptrdiff_t mlinesize, ptrdiff_t alinesize,
93 int half, int shift, int offset)
97 for (y = 0; y < h; y++) {
98 for (x = 0; x < w; x++) {
99 dst[x] = ((msrc[x] * (((asrc[x] >> 1) & 1) + asrc[x])) + 128) >> 8;
108 static void premultiply8yuv(const uint8_t *msrc, const uint8_t *asrc,
110 ptrdiff_t mlinesize, ptrdiff_t alinesize,
113 int half, int shift, int offset)
117 for (y = 0; y < h; y++) {
118 for (x = 0; x < w; x++) {
119 dst[x] = ((((msrc[x] - 128) * (((asrc[x] >> 1) & 1) + asrc[x]))) >> 8) + 128;
128 static void premultiply8offset(const uint8_t *msrc, const uint8_t *asrc,
130 ptrdiff_t mlinesize, ptrdiff_t alinesize,
133 int half, int shift, int offset)
137 for (y = 0; y < h; y++) {
138 for (x = 0; x < w; x++) {
139 dst[x] = ((((msrc[x] - offset) * (((asrc[x] >> 1) & 1) + asrc[x])) + 128) >> 8) + offset;
148 static void premultiply16(const uint8_t *mmsrc, const uint8_t *aasrc,
150 ptrdiff_t mlinesize, ptrdiff_t alinesize,
153 int half, int shift, int offset)
155 const uint16_t *msrc = (const uint16_t *)mmsrc;
156 const uint16_t *asrc = (const uint16_t *)aasrc;
157 uint16_t *dst = (uint16_t *)ddst;
160 for (y = 0; y < h; y++) {
161 for (x = 0; x < w; x++) {
162 dst[x] = ((msrc[x] * (((asrc[x] >> 1) & 1) + asrc[x])) + half) >> shift;
165 dst += dlinesize / 2;
166 msrc += mlinesize / 2;
167 asrc += alinesize / 2;
171 static void premultiply16yuv(const uint8_t *mmsrc, const uint8_t *aasrc,
173 ptrdiff_t mlinesize, ptrdiff_t alinesize,
176 int half, int shift, int offset)
178 const uint16_t *msrc = (const uint16_t *)mmsrc;
179 const uint16_t *asrc = (const uint16_t *)aasrc;
180 uint16_t *dst = (uint16_t *)ddst;
183 for (y = 0; y < h; y++) {
184 for (x = 0; x < w; x++) {
185 dst[x] = ((((msrc[x] - half) * (((asrc[x] >> 1) & 1) + asrc[x]))) >> shift) + half;
188 dst += dlinesize / 2;
189 msrc += mlinesize / 2;
190 asrc += alinesize / 2;
194 static void premultiply16offset(const uint8_t *mmsrc, const uint8_t *aasrc,
196 ptrdiff_t mlinesize, ptrdiff_t alinesize,
199 int half, int shift, int offset)
201 const uint16_t *msrc = (const uint16_t *)mmsrc;
202 const uint16_t *asrc = (const uint16_t *)aasrc;
203 uint16_t *dst = (uint16_t *)ddst;
206 for (y = 0; y < h; y++) {
207 for (x = 0; x < w; x++) {
208 dst[x] = ((((msrc[x] - offset) * (((asrc[x] >> 1) & 1) + asrc[x])) + half) >> shift) + offset;
211 dst += dlinesize / 2;
212 msrc += mlinesize / 2;
213 asrc += alinesize / 2;
217 static void unpremultiply8(const uint8_t *msrc, const uint8_t *asrc,
219 ptrdiff_t mlinesize, ptrdiff_t alinesize,
222 int half, int max, int offset)
226 for (y = 0; y < h; y++) {
227 for (x = 0; x < w; x++) {
228 if (asrc[x] > 0 && asrc[x] < 255)
229 dst[x] = FFMIN(msrc[x] * 255 / asrc[x], 255);
240 static void unpremultiply8yuv(const uint8_t *msrc, const uint8_t *asrc,
242 ptrdiff_t mlinesize, ptrdiff_t alinesize,
245 int half, int max, int offset)
249 for (y = 0; y < h; y++) {
250 for (x = 0; x < w; x++) {
251 if (asrc[x] > 0 && asrc[x] < 255)
252 dst[x] = FFMIN((msrc[x] - 128) * 255 / asrc[x] + 128, 255);
263 static void unpremultiply8offset(const uint8_t *msrc, const uint8_t *asrc,
265 ptrdiff_t mlinesize, ptrdiff_t alinesize,
268 int half, int max, int offset)
272 for (y = 0; y < h; y++) {
273 for (x = 0; x < w; x++) {
274 if (asrc[x] > 0 && asrc[x] < 255)
275 dst[x] = FFMIN((msrc[x] - offset) * 255 / asrc[x] + offset, 255);
286 static void unpremultiply16(const uint8_t *mmsrc, const uint8_t *aasrc,
288 ptrdiff_t mlinesize, ptrdiff_t alinesize,
291 int half, int max, int offset)
293 const uint16_t *msrc = (const uint16_t *)mmsrc;
294 const uint16_t *asrc = (const uint16_t *)aasrc;
295 uint16_t *dst = (uint16_t *)ddst;
298 for (y = 0; y < h; y++) {
299 for (x = 0; x < w; x++) {
300 if (asrc[x] > 0 && asrc[x] < max)
301 dst[x] = FFMIN(msrc[x] * (unsigned)max / asrc[x], max);
306 dst += dlinesize / 2;
307 msrc += mlinesize / 2;
308 asrc += alinesize / 2;
312 static void unpremultiply16yuv(const uint8_t *mmsrc, const uint8_t *aasrc,
314 ptrdiff_t mlinesize, ptrdiff_t alinesize,
317 int half, int max, int offset)
319 const uint16_t *msrc = (const uint16_t *)mmsrc;
320 const uint16_t *asrc = (const uint16_t *)aasrc;
321 uint16_t *dst = (uint16_t *)ddst;
324 for (y = 0; y < h; y++) {
325 for (x = 0; x < w; x++) {
326 if (asrc[x] > 0 && asrc[x] < max)
327 dst[x] = FFMAX(FFMIN((msrc[x] - half) * max / asrc[x], half - 1), -half) + half;
332 dst += dlinesize / 2;
333 msrc += mlinesize / 2;
334 asrc += alinesize / 2;
338 static void unpremultiply16offset(const uint8_t *mmsrc, const uint8_t *aasrc,
340 ptrdiff_t mlinesize, ptrdiff_t alinesize,
343 int half, int max, int offset)
345 const uint16_t *msrc = (const uint16_t *)mmsrc;
346 const uint16_t *asrc = (const uint16_t *)aasrc;
347 uint16_t *dst = (uint16_t *)ddst;
350 for (y = 0; y < h; y++) {
351 for (x = 0; x < w; x++) {
352 if (asrc[x] > 0 && asrc[x] < max)
353 dst[x] = FFMAX(FFMIN((msrc[x] - offset) * (unsigned)max / asrc[x] + offset, max), 0);
358 dst += dlinesize / 2;
359 msrc += mlinesize / 2;
360 asrc += alinesize / 2;
364 static int filter_frame(AVFilterContext *ctx,
365 AVFrame **out, AVFrame *base, AVFrame *alpha)
367 PreMultiplyContext *s = ctx->priv;
368 AVFilterLink *outlink = ctx->outputs[0];
370 if (ctx->is_disabled) {
371 *out = av_frame_clone(base);
373 return AVERROR(ENOMEM);
375 int p, full, limited;
377 *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
379 return AVERROR(ENOMEM);
380 av_frame_copy_props(*out, base);
382 full = base->color_range == AVCOL_RANGE_JPEG;
383 limited = base->color_range == AVCOL_RANGE_MPEG;
386 switch (outlink->format) {
387 case AV_PIX_FMT_YUV444P:
388 case AV_PIX_FMT_YUVA444P:
389 s->premultiply[0] = full ? unpremultiply8 : unpremultiply8offset;
390 s->premultiply[1] = s->premultiply[2] = unpremultiply8yuv;
392 case AV_PIX_FMT_YUVJ444P:
393 s->premultiply[0] = unpremultiply8;
394 s->premultiply[1] = s->premultiply[2] = unpremultiply8yuv;
396 case AV_PIX_FMT_GBRP:
397 case AV_PIX_FMT_GBRAP:
398 s->premultiply[0] = s->premultiply[1] = s->premultiply[2] = limited ? unpremultiply8offset : unpremultiply8;
400 case AV_PIX_FMT_YUV444P9:
401 case AV_PIX_FMT_YUVA444P9:
402 case AV_PIX_FMT_YUV444P10:
403 case AV_PIX_FMT_YUVA444P10:
404 case AV_PIX_FMT_YUV444P12:
405 case AV_PIX_FMT_YUV444P14:
406 case AV_PIX_FMT_YUV444P16:
407 case AV_PIX_FMT_YUVA444P16:
408 s->premultiply[0] = full ? unpremultiply16 : unpremultiply16offset;
409 s->premultiply[1] = s->premultiply[2] = unpremultiply16yuv;
411 case AV_PIX_FMT_GBRP9:
412 case AV_PIX_FMT_GBRP10:
413 case AV_PIX_FMT_GBRAP10:
414 case AV_PIX_FMT_GBRP12:
415 case AV_PIX_FMT_GBRAP12:
416 case AV_PIX_FMT_GBRP14:
417 case AV_PIX_FMT_GBRP16:
418 case AV_PIX_FMT_GBRAP16:
419 s->premultiply[0] = s->premultiply[1] = s->premultiply[2] = limited ? unpremultiply16offset : unpremultiply16;
421 case AV_PIX_FMT_GRAY8:
422 s->premultiply[0] = limited ? unpremultiply8offset : unpremultiply8;
424 case AV_PIX_FMT_GRAY9:
425 case AV_PIX_FMT_GRAY10:
426 case AV_PIX_FMT_GRAY12:
427 case AV_PIX_FMT_GRAY16:
428 s->premultiply[0] = limited ? unpremultiply16offset : unpremultiply16;
432 switch (outlink->format) {
433 case AV_PIX_FMT_YUV444P:
434 case AV_PIX_FMT_YUVA444P:
435 s->premultiply[0] = full ? premultiply8 : premultiply8offset;
436 s->premultiply[1] = s->premultiply[2] = premultiply8yuv;
438 case AV_PIX_FMT_YUVJ444P:
439 s->premultiply[0] = premultiply8;
440 s->premultiply[1] = s->premultiply[2] = premultiply8yuv;
442 case AV_PIX_FMT_GBRP:
443 case AV_PIX_FMT_GBRAP:
444 s->premultiply[0] = s->premultiply[1] = s->premultiply[2] = limited ? premultiply8offset : premultiply8;
446 case AV_PIX_FMT_YUV444P9:
447 case AV_PIX_FMT_YUVA444P9:
448 case AV_PIX_FMT_YUV444P10:
449 case AV_PIX_FMT_YUVA444P10:
450 case AV_PIX_FMT_YUV444P12:
451 case AV_PIX_FMT_YUV444P14:
452 case AV_PIX_FMT_YUV444P16:
453 case AV_PIX_FMT_YUVA444P16:
454 s->premultiply[0] = full ? premultiply16 : premultiply16offset;
455 s->premultiply[1] = s->premultiply[2] = premultiply16yuv;
457 case AV_PIX_FMT_GBRP9:
458 case AV_PIX_FMT_GBRP10:
459 case AV_PIX_FMT_GBRAP10:
460 case AV_PIX_FMT_GBRP12:
461 case AV_PIX_FMT_GBRAP12:
462 case AV_PIX_FMT_GBRP14:
463 case AV_PIX_FMT_GBRP16:
464 case AV_PIX_FMT_GBRAP16:
465 s->premultiply[0] = s->premultiply[1] = s->premultiply[2] = limited ? premultiply16offset : premultiply16;
467 case AV_PIX_FMT_GRAY8:
468 s->premultiply[0] = limited ? premultiply8offset : premultiply8;
470 case AV_PIX_FMT_GRAY9:
471 case AV_PIX_FMT_GRAY10:
472 case AV_PIX_FMT_GRAY12:
473 case AV_PIX_FMT_GRAY16:
474 s->premultiply[0] = limited ? premultiply16offset : premultiply16;
479 for (p = 0; p < s->nb_planes; p++) {
480 if (!((1 << p) & s->planes) || p == 3) {
481 av_image_copy_plane((*out)->data[p], (*out)->linesize[p], base->data[p], base->linesize[p],
482 s->linesize[p], s->height[p]);
486 s->premultiply[p](base->data[p], s->inplace ? alpha->data[3] : alpha->data[0],
488 base->linesize[p], s->inplace ? alpha->linesize[3] : alpha->linesize[0],
490 s->width[p], s->height[p],
491 s->half, s->inverse ? s->max : s->depth, s->offset);
498 static int process_frame(FFFrameSync *fs)
500 AVFilterContext *ctx = fs->parent;
501 PreMultiplyContext *s = fs->opaque;
502 AVFilterLink *outlink = ctx->outputs[0];
503 AVFrame *out = NULL, *base, *alpha;
506 if ((ret = ff_framesync_get_frame(&s->fs, 0, &base, 0)) < 0 ||
507 (ret = ff_framesync_get_frame(&s->fs, 1, &alpha, 0)) < 0)
510 if ((ret = filter_frame(ctx, &out, base, alpha)) < 0)
513 out->pts = av_rescale_q(base->pts, s->fs.time_base, outlink->time_base);
515 return ff_filter_frame(outlink, out);
518 static int config_input(AVFilterLink *inlink)
520 AVFilterContext *ctx = inlink->dst;
521 PreMultiplyContext *s = ctx->priv;
522 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
525 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
527 if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
530 hsub = desc->log2_chroma_w;
531 vsub = desc->log2_chroma_h;
532 s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
533 s->height[0] = s->height[3] = inlink->h;
534 s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
535 s->width[0] = s->width[3] = inlink->w;
537 s->depth = desc->comp[0].depth;
538 s->max = (1 << s->depth) - 1;
539 s->half = (1 << s->depth) / 2;
540 s->offset = 16 << (s->depth - 8);
545 static int config_output(AVFilterLink *outlink)
547 AVFilterContext *ctx = outlink->src;
548 PreMultiplyContext *s = ctx->priv;
549 AVFilterLink *base = ctx->inputs[0];
555 alpha = ctx->inputs[1];
557 if (base->format != alpha->format) {
558 av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
559 return AVERROR(EINVAL);
561 if (base->w != alpha->w ||
562 base->h != alpha->h) {
563 av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
564 "(size %dx%d) do not match the corresponding "
565 "second input link %s parameters (%dx%d) ",
566 ctx->input_pads[0].name, base->w, base->h,
567 ctx->input_pads[1].name, alpha->w, alpha->h);
568 return AVERROR(EINVAL);
572 outlink->w = base->w;
573 outlink->h = base->h;
574 outlink->time_base = base->time_base;
575 outlink->sample_aspect_ratio = base->sample_aspect_ratio;
576 outlink->frame_rate = base->frame_rate;
581 if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0)
585 in[0].time_base = base->time_base;
586 in[1].time_base = alpha->time_base;
588 in[0].before = EXT_STOP;
589 in[0].after = EXT_INFINITY;
591 in[1].before = EXT_STOP;
592 in[1].after = EXT_INFINITY;
594 s->fs.on_event = process_frame;
596 return ff_framesync_configure(&s->fs);
599 static int activate(AVFilterContext *ctx)
601 PreMultiplyContext *s = ctx->priv;
604 AVFrame *frame = NULL;
609 if ((ret = ff_inlink_consume_frame(ctx->inputs[0], &frame)) > 0) {
610 if ((ret = filter_frame(ctx, &out, frame, frame)) < 0)
612 av_frame_free(&frame);
613 ret = ff_filter_frame(ctx->outputs[0], out);
617 } else if (ff_inlink_acknowledge_status(ctx->inputs[0], &status, &pts)) {
618 ff_outlink_set_status(ctx->outputs[0], status, pts);
621 if (ff_outlink_frame_wanted(ctx->outputs[0]))
622 ff_inlink_request_frame(ctx->inputs[0]);
626 return ff_framesync_activate(&s->fs);
630 static av_cold int init(AVFilterContext *ctx)
632 PreMultiplyContext *s = ctx->priv;
633 AVFilterPad pad = { 0 };
636 if (!strcmp(ctx->filter->name, "unpremultiply"))
639 pad.type = AVMEDIA_TYPE_VIDEO;
640 pad.name = av_strdup("main");
641 pad.config_props = config_input;
643 return AVERROR(ENOMEM);
645 if ((ret = ff_insert_inpad(ctx, 0, &pad)) < 0) {
651 pad.type = AVMEDIA_TYPE_VIDEO;
652 pad.name = av_strdup("alpha");
653 pad.config_props = NULL;
655 return AVERROR(ENOMEM);
657 if ((ret = ff_insert_inpad(ctx, 1, &pad)) < 0) {
666 static av_cold void uninit(AVFilterContext *ctx)
668 PreMultiplyContext *s = ctx->priv;
671 ff_framesync_uninit(&s->fs);
674 static const AVFilterPad premultiply_outputs[] = {
677 .type = AVMEDIA_TYPE_VIDEO,
678 .config_props = config_output,
683 #if CONFIG_PREMULTIPLY_FILTER
685 AVFilter ff_vf_premultiply = {
686 .name = "premultiply",
687 .description = NULL_IF_CONFIG_SMALL("PreMultiply first stream with first plane of second stream."),
688 .priv_size = sizeof(PreMultiplyContext),
691 .query_formats = query_formats,
692 .activate = activate,
694 .outputs = premultiply_outputs,
695 .priv_class = &premultiply_class,
696 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
697 AVFILTER_FLAG_DYNAMIC_INPUTS,
700 #endif /* CONFIG_PREMULTIPLY_FILTER */
702 #if CONFIG_UNPREMULTIPLY_FILTER
704 #define unpremultiply_options options
705 AVFILTER_DEFINE_CLASS(unpremultiply);
707 AVFilter ff_vf_unpremultiply = {
708 .name = "unpremultiply",
709 .description = NULL_IF_CONFIG_SMALL("UnPreMultiply first stream with first plane of second stream."),
710 .priv_size = sizeof(PreMultiplyContext),
713 .query_formats = query_formats,
714 .activate = activate,
716 .outputs = premultiply_outputs,
717 .priv_class = &unpremultiply_class,
718 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
719 AVFILTER_FLAG_DYNAMIC_INPUTS,
722 #endif /* CONFIG_UNPREMULTIPLY_FILTER */