FFmpeg
Loading...
Searching...
No Matches
vf_premultiply.c
Go to the documentation of this file.
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 "config_components.h"
22
23#include "libavutil/imgutils.h"
24#include "libavutil/pixdesc.h"
25#include "libavutil/opt.h"
26#include "avfilter.h"
27#include "filters.h"
28#include "formats.h"
29#include "framesync.h"
30#include "video.h"
31
32typedef struct ThreadData {
33 AVFrame *m, *a, *d;
35
36typedef struct PreMultiplyContext {
37 const AVClass *class;
41 int planes;
47
48 void (*premultiply[AV_VIDEO_MAX_PLANES])(const uint8_t *msrc, const uint8_t *asrc,
49 uint8_t *dst,
50 ptrdiff_t mlinesize, ptrdiff_t alinesize,
51 ptrdiff_t dlinesize,
52 int w, int h,
53 int half, int shift, int offset);
55
56#define OFFSET(x) offsetof(PreMultiplyContext, x)
57#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
58
59static const AVOption options[] = {
60 { "planes", "set planes", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
61 { "inplace","enable inplace mode", OFFSET(inplace), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
62 { NULL }
63};
64
65AVFILTER_DEFINE_CLASS_EXT(premultiply, "(un)premultiply", options);
66
68 AVFilterFormatsConfig **cfg_in,
69 AVFilterFormatsConfig **cfg_out)
70{
71 const PreMultiplyContext *s = ctx->priv;
73 int ret;
74
75 static const enum AVPixelFormat no_alpha_pix_fmts[] = {
84 };
85
86 static const enum AVPixelFormat alpha_pix_fmts[] = {
92 };
93
94 ret = ff_set_pixel_formats_from_list2(ctx, cfg_in, cfg_out,
95 s->inplace ? alpha_pix_fmts : no_alpha_pix_fmts);
96 if (ret < 0)
97 return ret;
98
99 if (s->dynamic) {
100 ret = ff_formats_ref(ff_all_alpha_modes(), &cfg_in[0]->alpha_modes);
101 if (ret < 0)
102 return ret;
103 return ff_formats_ref(ff_all_alpha_modes(), &cfg_out[0]->alpha_modes);
104 } else {
105 /* Configure alpha mode corresponding to the chosen direction */
106 if (s->inplace) {
109 ret = ff_formats_ref(formats, &cfg_in[0]->alpha_modes);
110 if (ret < 0)
111 return ret;
112 }
113
116 return ff_formats_ref(formats, &cfg_out[0]->alpha_modes);
117 }
118}
119
120static void premultiply8(const uint8_t *msrc, const uint8_t *asrc,
121 uint8_t *dst,
122 ptrdiff_t mlinesize, ptrdiff_t alinesize,
123 ptrdiff_t dlinesize,
124 int w, int h,
125 int half, int shift, int offset)
126{
127 int x, y;
128
129 for (y = 0; y < h; y++) {
130 for (x = 0; x < w; x++) {
131 dst[x] = (msrc[x] * asrc[x] + 128) >> 8;
132 }
133
134 dst += dlinesize;
135 msrc += mlinesize;
136 asrc += alinesize;
137 }
138}
139
140static void premultiply8yuv(const uint8_t *msrc, const uint8_t *asrc,
141 uint8_t *dst,
142 ptrdiff_t mlinesize, ptrdiff_t alinesize,
143 ptrdiff_t dlinesize,
144 int w, int h,
145 int half, int shift, int offset)
146{
147 int x, y;
148
149 for (y = 0; y < h; y++) {
150 for (x = 0; x < w; x++) {
151 dst[x] = (((msrc[x] - 128) * asrc[x]) >> 8) + 128;
152 }
153
154 dst += dlinesize;
155 msrc += mlinesize;
156 asrc += alinesize;
157 }
158}
159
160static void premultiply8offset(const uint8_t *msrc, const uint8_t *asrc,
161 uint8_t *dst,
162 ptrdiff_t mlinesize, ptrdiff_t alinesize,
163 ptrdiff_t dlinesize,
164 int w, int h,
165 int half, int shift, int offset)
166{
167 int x, y;
168
169 for (y = 0; y < h; y++) {
170 for (x = 0; x < w; x++) {
171 dst[x] = ((((msrc[x] - offset) * asrc[x]) + 128) >> 8) + offset;
172 }
173
174 dst += dlinesize;
175 msrc += mlinesize;
176 asrc += alinesize;
177 }
178}
179
180static void premultiply16(const uint8_t *mmsrc, const uint8_t *aasrc,
181 uint8_t *ddst,
182 ptrdiff_t mlinesize, ptrdiff_t alinesize,
183 ptrdiff_t dlinesize,
184 int w, int h,
185 int half, int shift, int offset)
186{
187 const uint16_t *msrc = (const uint16_t *)mmsrc;
188 const uint16_t *asrc = (const uint16_t *)aasrc;
189 uint16_t *dst = (uint16_t *)ddst;
190 int x, y;
191
192 for (y = 0; y < h; y++) {
193 for (x = 0; x < w; x++) {
194 dst[x] = (msrc[x] * asrc[x] + half) >> shift;
195 }
196
197 dst += dlinesize / 2;
198 msrc += mlinesize / 2;
199 asrc += alinesize / 2;
200 }
201}
202
203static void premultiply16yuv(const uint8_t *mmsrc, const uint8_t *aasrc,
204 uint8_t *ddst,
205 ptrdiff_t mlinesize, ptrdiff_t alinesize,
206 ptrdiff_t dlinesize,
207 int w, int h,
208 int half, int shift, int offset)
209{
210 const uint16_t *msrc = (const uint16_t *)mmsrc;
211 const uint16_t *asrc = (const uint16_t *)aasrc;
212 uint16_t *dst = (uint16_t *)ddst;
213 int x, y;
214
215 for (y = 0; y < h; y++) {
216 for (x = 0; x < w; x++) {
217 dst[x] = (((msrc[x] - half) * (int64_t)asrc[x]) >> shift) + half;
218 }
219
220 dst += dlinesize / 2;
221 msrc += mlinesize / 2;
222 asrc += alinesize / 2;
223 }
224}
225
226static void premultiply16offset(const uint8_t *mmsrc, const uint8_t *aasrc,
227 uint8_t *ddst,
228 ptrdiff_t mlinesize, ptrdiff_t alinesize,
229 ptrdiff_t dlinesize,
230 int w, int h,
231 int half, int shift, int offset)
232{
233 const uint16_t *msrc = (const uint16_t *)mmsrc;
234 const uint16_t *asrc = (const uint16_t *)aasrc;
235 uint16_t *dst = (uint16_t *)ddst;
236 int x, y;
237
238 for (y = 0; y < h; y++) {
239 for (x = 0; x < w; x++) {
240 dst[x] = ((((msrc[x] - offset) * (int64_t)asrc[x]) + half) >> shift) + offset;
241 }
242
243 dst += dlinesize / 2;
244 msrc += mlinesize / 2;
245 asrc += alinesize / 2;
246 }
247}
248
249static void premultiplyf32(const uint8_t *mmsrc, const uint8_t *aasrc,
250 uint8_t *ddst,
251 ptrdiff_t mlinesize, ptrdiff_t alinesize,
252 ptrdiff_t dlinesize,
253 int w, int h,
254 int half, int shift, int offset)
255{
256 const float *msrc = (const float *)mmsrc;
257 const float *asrc = (const float *)aasrc;
258 float *dst = (float *)ddst;
259 int x, y;
260
261 for (y = 0; y < h; y++) {
262 for (x = 0; x < w; x++) {
263 dst[x] = msrc[x] * asrc[x];
264 }
265
266 dst += dlinesize / 4;
267 msrc += mlinesize / 4;
268 asrc += alinesize / 4;
269 }
270}
271
272static void premultiplyf32offset(const uint8_t *mmsrc, const uint8_t *aasrc,
273 uint8_t *ddst,
274 ptrdiff_t mlinesize, ptrdiff_t alinesize,
275 ptrdiff_t dlinesize,
276 int w, int h,
277 int half, int shift, int offset)
278{
279 const float *msrc = (const float *)mmsrc;
280 const float *asrc = (const float *)aasrc;
281 float *dst = (float *)ddst;
282 int x, y;
283
284 float offsetf = offset / 65535.0f;
285
286 for (y = 0; y < h; y++) {
287 for (x = 0; x < w; x++) {
288 dst[x] = ((msrc[x] - offsetf) * asrc[x]) + offsetf;
289 }
290
291 dst += dlinesize / 4;
292 msrc += mlinesize / 4;
293 asrc += alinesize / 4;
294 }
295}
296
297static void unpremultiply8(const uint8_t *msrc, const uint8_t *asrc,
298 uint8_t *dst,
299 ptrdiff_t mlinesize, ptrdiff_t alinesize,
300 ptrdiff_t dlinesize,
301 int w, int h,
302 int half, int max, int offset)
303{
304 int x, y;
305
306 for (y = 0; y < h; y++) {
307 for (x = 0; x < w; x++) {
308 if (asrc[x] > 0 && asrc[x] < 255)
309 dst[x] = FFMIN(msrc[x] * 255 / asrc[x], 255);
310 else
311 dst[x] = msrc[x];
312 }
313
314 dst += dlinesize;
315 msrc += mlinesize;
316 asrc += alinesize;
317 }
318}
319
320static void unpremultiply8yuv(const uint8_t *msrc, const uint8_t *asrc,
321 uint8_t *dst,
322 ptrdiff_t mlinesize, ptrdiff_t alinesize,
323 ptrdiff_t dlinesize,
324 int w, int h,
325 int half, int max, int offset)
326{
327 int x, y;
328
329 for (y = 0; y < h; y++) {
330 for (x = 0; x < w; x++) {
331 if (asrc[x] > 0 && asrc[x] < 255)
332 dst[x] = FFMIN((msrc[x] - 128) * 255 / asrc[x] + 128, 255);
333 else
334 dst[x] = msrc[x];
335 }
336
337 dst += dlinesize;
338 msrc += mlinesize;
339 asrc += alinesize;
340 }
341}
342
343static void unpremultiply8offset(const uint8_t *msrc, const uint8_t *asrc,
344 uint8_t *dst,
345 ptrdiff_t mlinesize, ptrdiff_t alinesize,
346 ptrdiff_t dlinesize,
347 int w, int h,
348 int half, int max, int offset)
349{
350 int x, y;
351
352 for (y = 0; y < h; y++) {
353 for (x = 0; x < w; x++) {
354 if (asrc[x] > 0 && asrc[x] < 255)
355 dst[x] = FFMIN(FFMAX(msrc[x] - offset, 0) * 255 / asrc[x] + offset, 255);
356 else
357 dst[x] = msrc[x];
358 }
359
360 dst += dlinesize;
361 msrc += mlinesize;
362 asrc += alinesize;
363 }
364}
365
366static void unpremultiply16(const uint8_t *mmsrc, const uint8_t *aasrc,
367 uint8_t *ddst,
368 ptrdiff_t mlinesize, ptrdiff_t alinesize,
369 ptrdiff_t dlinesize,
370 int w, int h,
371 int half, int max, int offset)
372{
373 const uint16_t *msrc = (const uint16_t *)mmsrc;
374 const uint16_t *asrc = (const uint16_t *)aasrc;
375 uint16_t *dst = (uint16_t *)ddst;
376 int x, y;
377
378 for (y = 0; y < h; y++) {
379 for (x = 0; x < w; x++) {
380 if (asrc[x] > 0 && asrc[x] < max)
381 dst[x] = FFMIN(msrc[x] * (unsigned)max / asrc[x], max);
382 else
383 dst[x] = msrc[x];
384 }
385
386 dst += dlinesize / 2;
387 msrc += mlinesize / 2;
388 asrc += alinesize / 2;
389 }
390}
391
392static void unpremultiply16yuv(const uint8_t *mmsrc, const uint8_t *aasrc,
393 uint8_t *ddst,
394 ptrdiff_t mlinesize, ptrdiff_t alinesize,
395 ptrdiff_t dlinesize,
396 int w, int h,
397 int half, int max, int offset)
398{
399 const uint16_t *msrc = (const uint16_t *)mmsrc;
400 const uint16_t *asrc = (const uint16_t *)aasrc;
401 uint16_t *dst = (uint16_t *)ddst;
402 int x, y;
403
404 for (y = 0; y < h; y++) {
405 for (x = 0; x < w; x++) {
406 if (asrc[x] > 0 && asrc[x] < max)
407 dst[x] = FFMAX(FFMIN((msrc[x] - half) * max / asrc[x], half - 1), -half) + half;
408 else
409 dst[x] = msrc[x];
410 }
411
412 dst += dlinesize / 2;
413 msrc += mlinesize / 2;
414 asrc += alinesize / 2;
415 }
416}
417
418static void unpremultiply16offset(const uint8_t *mmsrc, const uint8_t *aasrc,
419 uint8_t *ddst,
420 ptrdiff_t mlinesize, ptrdiff_t alinesize,
421 ptrdiff_t dlinesize,
422 int w, int h,
423 int half, int max, int offset)
424{
425 const uint16_t *msrc = (const uint16_t *)mmsrc;
426 const uint16_t *asrc = (const uint16_t *)aasrc;
427 uint16_t *dst = (uint16_t *)ddst;
428 int x, y;
429
430 for (y = 0; y < h; y++) {
431 for (x = 0; x < w; x++) {
432 if (asrc[x] > 0 && asrc[x] < max)
433 dst[x] = FFMAX(FFMIN(FFMAX(msrc[x] - offset, 0) * (unsigned)max / asrc[x] + offset, max), 0);
434 else
435 dst[x] = msrc[x];
436 }
437
438 dst += dlinesize / 2;
439 msrc += mlinesize / 2;
440 asrc += alinesize / 2;
441 }
442}
443
444static void unpremultiplyf32(const uint8_t *mmsrc, const uint8_t *aasrc,
445 uint8_t *ddst,
446 ptrdiff_t mlinesize, ptrdiff_t alinesize,
447 ptrdiff_t dlinesize,
448 int w, int h,
449 int half, int max, int offset)
450{
451 const float *msrc = (const float *)mmsrc;
452 const float *asrc = (const float *)aasrc;
453
454 float *dst = (float *)ddst;
455 int x, y;
456
457 for (y = 0; y < h; y++) {
458 for (x = 0; x < w; x++) {
459 if (asrc[x] > 0.0f)
460 dst[x] = msrc[x] / asrc[x];
461 else
462 dst[x] = msrc[x];
463 }
464
465 dst += dlinesize / 4;
466 msrc += mlinesize / 4;
467 asrc += alinesize / 4;
468 }
469}
470
471static void unpremultiplyf32offset(const uint8_t *mmsrc, const uint8_t *aasrc,
472 uint8_t *ddst,
473 ptrdiff_t mlinesize, ptrdiff_t alinesize,
474 ptrdiff_t dlinesize,
475 int w, int h,
476 int half, int max, int offset)
477{
478 const float *msrc = (const float *)mmsrc;
479 const float *asrc = (const float *)aasrc;
480
481 float *dst = (float *)ddst;
482 int x, y;
483
484 float offsetf = offset / 65535.0f;
485
486 for (y = 0; y < h; y++) {
487 for (x = 0; x < w; x++) {
488 if (asrc[x] > 0.0f)
489 dst[x] = (msrc[x] - offsetf) / asrc[x] + offsetf;
490 else
491 dst[x] = msrc[x];
492 }
493
494 dst += dlinesize / 4;
495 msrc += mlinesize / 4;
496 asrc += alinesize / 4;
497 }
498}
499
500static int premultiply_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
501{
502 PreMultiplyContext *s = ctx->priv;
503 ThreadData *td = arg;
504 AVFrame *out = td->d;
505 AVFrame *alpha = td->a;
506 AVFrame *base = td->m;
507 int p;
508
509 for (p = 0; p < s->nb_planes; p++) {
510 const int slice_start = ff_slice_pos(s->height[p], jobnr, nb_jobs);
511 const int slice_end = ff_slice_pos(s->height[p], jobnr + 1, nb_jobs);
512
513 if (!((1 << p) & s->planes) || p == 3) {
514 av_image_copy_plane(out->data[p] + slice_start * out->linesize[p],
515 out->linesize[p],
516 base->data[p] + slice_start * base->linesize[p],
517 base->linesize[p],
518 s->linesize[p], slice_end - slice_start);
519 continue;
520 }
521
522 s->premultiply[p](base->data[p] + slice_start * base->linesize[p],
523 s->inplace ? alpha->data[3] + slice_start * alpha->linesize[3] :
524 alpha->data[0] + slice_start * alpha->linesize[0],
525 out->data[p] + slice_start * out->linesize[p],
526 base->linesize[p], s->inplace ? alpha->linesize[3] : alpha->linesize[0],
527 out->linesize[p],
528 s->width[p], slice_end - slice_start,
529 s->half, s->inverse ? s->max : s->depth, s->offset);
530 }
531
532 return 0;
533}
534
537{
538 PreMultiplyContext *s = ctx->priv;
539 AVFilterLink *outlink = ctx->outputs[0];
540
541 if (ctx->is_disabled || outlink->alpha_mode == base->alpha_mode) {
543 if (!*out)
544 return AVERROR(ENOMEM);
545 } else {
546 ThreadData td;
547 int full, limited;
548
549 *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
550 if (!*out)
551 return AVERROR(ENOMEM);
553 (*out)->alpha_mode = outlink->alpha_mode;
554
555 full = base->color_range == AVCOL_RANGE_JPEG;
556 limited = base->color_range == AVCOL_RANGE_MPEG;
557
558 if (s->inverse) {
559 switch (outlink->format) {
562 s->premultiply[0] = full ? unpremultiply8 : unpremultiply8offset;
563 s->premultiply[1] = s->premultiply[2] = unpremultiply8yuv;
564 break;
566 s->premultiply[0] = unpremultiply8;
567 s->premultiply[1] = s->premultiply[2] = unpremultiply8yuv;
568 break;
569 case AV_PIX_FMT_GBRP:
570 case AV_PIX_FMT_GBRAP:
571 s->premultiply[0] = s->premultiply[1] = s->premultiply[2] = limited ? unpremultiply8offset : unpremultiply8;
572 break;
582 s->premultiply[0] = full ? unpremultiply16 : unpremultiply16offset;
583 s->premultiply[1] = s->premultiply[2] = unpremultiply16yuv;
584 break;
585 case AV_PIX_FMT_GBRP9:
593 s->premultiply[0] = s->premultiply[1] = s->premultiply[2] = limited ? unpremultiply16offset : unpremultiply16;
594 break;
597 s->premultiply[0] = s->premultiply[1] = s->premultiply[2] = limited ? unpremultiplyf32offset : unpremultiplyf32;
598 break;
599 case AV_PIX_FMT_GRAY8:
600 s->premultiply[0] = limited ? unpremultiply8offset : unpremultiply8;
601 break;
602 case AV_PIX_FMT_GRAY9:
607 s->premultiply[0] = limited ? unpremultiply16offset : unpremultiply16;
608 break;
609 }
610 } else {
611 switch (outlink->format) {
614 s->premultiply[0] = full ? premultiply8 : premultiply8offset;
615 s->premultiply[1] = s->premultiply[2] = premultiply8yuv;
616 break;
618 s->premultiply[0] = premultiply8;
619 s->premultiply[1] = s->premultiply[2] = premultiply8yuv;
620 break;
621 case AV_PIX_FMT_GBRP:
622 case AV_PIX_FMT_GBRAP:
623 s->premultiply[0] = s->premultiply[1] = s->premultiply[2] = limited ? premultiply8offset : premultiply8;
624 break;
634 s->premultiply[0] = full ? premultiply16 : premultiply16offset;
635 s->premultiply[1] = s->premultiply[2] = premultiply16yuv;
636 break;
637 case AV_PIX_FMT_GBRP9:
645 s->premultiply[0] = s->premultiply[1] = s->premultiply[2] = limited ? premultiply16offset : premultiply16;
646 break;
649 s->premultiply[0] = s->premultiply[1] = s->premultiply[2] = limited ? premultiplyf32offset: premultiplyf32;
650 break;
651 case AV_PIX_FMT_GRAY8:
652 s->premultiply[0] = limited ? premultiply8offset : premultiply8;
653 break;
654 case AV_PIX_FMT_GRAY9:
659 s->premultiply[0] = limited ? premultiply16offset : premultiply16;
660 break;
661 }
662 }
663
664 td.d = *out;
665 td.a = alpha;
666 td.m = base;
668 FFMIN(s->height[0], ff_filter_get_nb_threads(ctx)));
669 }
670
671 return 0;
672}
673
675{
676 AVFilterContext *ctx = fs->parent;
677 PreMultiplyContext *s = fs->opaque;
678 AVFilterLink *outlink = ctx->outputs[0];
679 AVFrame *out = NULL, *base, *alpha;
680 int ret;
681
682 if ((ret = ff_framesync_get_frame(&s->fs, 0, &base, 0)) < 0 ||
683 (ret = ff_framesync_get_frame(&s->fs, 1, &alpha, 0)) < 0)
684 return ret;
685
686 if ((ret = filter_frame(ctx, &out, base, alpha)) < 0)
687 return ret;
688
689 out->pts = av_rescale_q(base->pts, s->fs.time_base, outlink->time_base);
690
691 return ff_filter_frame(outlink, out);
692}
693
694static int config_input(AVFilterLink *inlink)
695{
696 AVFilterContext *ctx = inlink->dst;
697 PreMultiplyContext *s = ctx->priv;
699 int vsub, hsub, ret;
700
701 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
702
703 if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
704 return ret;
705
706 hsub = desc->log2_chroma_w;
707 vsub = desc->log2_chroma_h;
708 s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
709 s->height[0] = s->height[3] = inlink->h;
710 s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
711 s->width[0] = s->width[3] = inlink->w;
712
713 s->depth = desc->flags & AV_PIX_FMT_FLAG_FLOAT ? 16 : desc->comp[0].depth;
714 s->max = (1 << s->depth) - 1;
715 s->half = (1 << s->depth) / 2;
716 s->offset = 16 << (s->depth - 8);
717
718 return 0;
719}
720
721static int config_output(AVFilterLink *outlink)
722{
723 AVFilterContext *ctx = outlink->src;
724 PreMultiplyContext *s = ctx->priv;
725 AVFilterLink *base = ctx->inputs[0];
728 FilterLink *ol = ff_filter_link(outlink);
729 FFFrameSyncIn *in;
730 int ret;
731
732 if (!s->inplace) {
733 alpha = ctx->inputs[1];
734
735 if (base->w != alpha->w ||
736 base->h != alpha->h) {
737 av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
738 "(size %dx%d) do not match the corresponding "
739 "second input link %s parameters (%dx%d) ",
740 ctx->input_pads[0].name, base->w, base->h,
741 ctx->input_pads[1].name, alpha->w, alpha->h);
742 return AVERROR(EINVAL);
743 }
744 }
745
746 outlink->w = base->w;
747 outlink->h = base->h;
748 outlink->time_base = base->time_base;
749 outlink->sample_aspect_ratio = base->sample_aspect_ratio;
750 ol->frame_rate = il->frame_rate;
751
752 if (s->dynamic)
753 s->inverse = base->alpha_mode == AVALPHA_MODE_PREMULTIPLIED;
754
755 if (s->inplace)
756 return 0;
757
758 if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0)
759 return ret;
760
761 in = s->fs.in;
762 in[0].time_base = base->time_base;
763 in[1].time_base = alpha->time_base;
764 in[0].sync = 1;
765 in[0].before = EXT_STOP;
766 in[0].after = EXT_INFINITY;
767 in[1].sync = 1;
768 in[1].before = EXT_STOP;
769 in[1].after = EXT_INFINITY;
770 s->fs.opaque = s;
771 s->fs.on_event = process_frame;
772
773 return ff_framesync_configure(&s->fs);
774}
775
777{
778 PreMultiplyContext *s = ctx->priv;
779
780 if (s->inplace) {
781 AVFrame *frame = NULL;
782 AVFrame *out = NULL;
783 int ret, status;
784 int64_t pts;
785
787
788 if ((ret = ff_inlink_consume_frame(ctx->inputs[0], &frame)) > 0) {
789 ret = filter_frame(ctx, &out, frame, frame);
791 if (ret < 0)
792 return ret;
793 ret = ff_filter_frame(ctx->outputs[0], out);
794 }
795 if (ret < 0) {
796 return ret;
797 } else if (ff_inlink_acknowledge_status(ctx->inputs[0], &status, &pts)) {
798 ff_outlink_set_status(ctx->outputs[0], status, pts);
799 return 0;
800 } else {
801 if (ff_outlink_frame_wanted(ctx->outputs[0]))
802 ff_inlink_request_frame(ctx->inputs[0]);
803 return 0;
804 }
805 } else {
806 return ff_framesync_activate(&s->fs);
807 }
808}
809
811{
812 PreMultiplyContext *s = ctx->priv;
813 AVFilterPad pad = { 0 };
814 int ret;
815
816 if (!strcmp(ctx->filter->name, "premultiply_dynamic")) {
817 s->dynamic = s->inplace = 1;
818 return 0;
819 }
820
821 if (!strcmp(ctx->filter->name, "unpremultiply"))
822 s->inverse = 1;
823
825 pad.name = "main";
827
828 if ((ret = ff_append_inpad(ctx, &pad)) < 0)
829 return ret;
830
831 if (!s->inplace) {
833 pad.name = "alpha";
834 pad.config_props = NULL;
835
836 if ((ret = ff_append_inpad(ctx, &pad)) < 0)
837 return ret;
838 }
839
840 return 0;
841}
842
844{
845 PreMultiplyContext *s = ctx->priv;
846
847 if (!s->inplace)
849}
850
852 {
853 .name = "default",
854 .type = AVMEDIA_TYPE_VIDEO,
855 .config_props = config_output,
856 },
857};
858
859#if CONFIG_PREMULTIPLY_FILTER
860
862 .p.name = "premultiply",
863 .p.description = NULL_IF_CONFIG_SMALL("PreMultiply first stream with first plane of second stream."),
864 .p.priv_class = &premultiply_class,
868 .priv_size = sizeof(PreMultiplyContext),
869 .init = init,
870 .uninit = uninit,
874};
875
876#endif /* CONFIG_PREMULTIPLY_FILTER */
877
878#if CONFIG_UNPREMULTIPLY_FILTER
879
881 .p.name = "unpremultiply",
882 .p.description = NULL_IF_CONFIG_SMALL("UnPreMultiply first stream with first plane of second stream."),
883 .p.priv_class = &premultiply_class,
887 .priv_size = sizeof(PreMultiplyContext),
888 .init = init,
889 .uninit = uninit,
893};
894
895#endif /* CONFIG_UNPREMULTIPLY_FILTER */
896
897#if CONFIG_PREMULTIPLY_DYNAMIC_FILTER
898
899static const AVFilterPad premultiply_input[] = {
900 {
901 .name = "default",
902 .type = AVMEDIA_TYPE_VIDEO,
903 .config_props = config_input,
904 },
905};
906
908 .p.name = "premultiply_dynamic",
909 .p.description = NULL_IF_CONFIG_SMALL("Premultiply or unpremultiply an image in-place, as needed."),
910 .p.priv_class = &premultiply_class,
912 .priv_size = sizeof(PreMultiplyContext),
913 .init = init,
914 .uninit = uninit,
916 FILTER_INPUTS(premultiply_input),
919};
920
921#endif /* CONFIG_UNPREMULTIPLY_DYNAMIC_FILTER */
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition aeval.c:246
static int config_input(AVFilterLink *inlink)
const FFFilter ff_vf_premultiply_dynamic
const FFFilter ff_vf_unpremultiply
const FFFilter ff_vf_premultiply
static FILE * out
static AVFormatContext * ctx
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
Definition avfilter.c:1467
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_append_inpad(AVFilterContext *f, AVFilterPad *p)
Append a new input/output pad to the filter's list of such pads.
Definition avfilter.c:127
int ff_outlink_frame_wanted(AVFilterLink *link)
Test if a frame is wanted on an output link.
Definition avfilter.c:1690
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition avfilter.c:1696
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition avfilter.c:846
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
Definition avfilter.c:1520
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Definition avfilter.c:1623
Main libavfilter public API header.
#define s(width, name)
Definition cbs_vp9.c:198
#define fs(width, name, subs,...)
Definition cbs_vp9.c:200
#define FLAGS
Definition cmdutils.c:598
#define AV_CEIL_RSHIFT(a, b)
Definition common.h:60
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define max(a, b)
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
static av_always_inline int process_frame(AVTextFormatContext *tfc, InputFile *ifile, AVFrame *frame, const AVPacket *pkt, int *packet_new)
Definition ffprobe.c:1596
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add ref as a new reference to formats.
Definition formats.c:756
AVFilterFormats * ff_make_formats_list_singleton(int fmt)
Equivalent to ff_make_format_list({const int[]}{ fmt, -1 })
Definition formats.c:596
AVFilterFormats * ff_all_alpha_modes(void)
Construct an AVFilterFormats representing all possible alpha modes.
Definition formats.c:724
int ff_set_pixel_formats_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const enum AVPixelFormat *fmts)
Definition formats.c:1162
int ff_framesync_configure(FFFrameSync *fs)
Configure a frame sync structure.
Definition framesync.c:137
int ff_framesync_activate(FFFrameSync *fs)
Examine the frames in the filter's input and try to produce output.
Definition framesync.c:352
int ff_framesync_get_frame(FFFrameSync *fs, unsigned in, AVFrame **rframe, unsigned get)
Get the current frame in an input.
Definition framesync.c:269
void ff_framesync_uninit(FFFrameSync *fs)
Free all memory currently allocated.
Definition framesync.c:301
int ff_framesync_init(FFFrameSync *fs, AVFilterContext *parent, unsigned nb_in)
Initialize a frame sync structure.
Definition framesync.c:86
@ EXT_INFINITY
Extend the frame to infinity.
Definition framesync.h:75
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition avfilter.h:166
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
Definition avfilter.h:204
#define AVFILTER_FLAG_DYNAMIC_INPUTS
The number of the filter inputs is not determined just by AVFilter.inputs.
Definition avfilter.h:155
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition frame.c:483
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition imgutils.c:374
int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
Fill plane linesizes for an image with pixel format pix_fmt and width width.
Definition imgutils.c:89
static const int16_t alpha[]
Definition ilbcdata.h:55
misc image utilities
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
unsigned offset
Definition libaomenc.c:763
static int shift(int a, int b)
Definition bonk.c:261
const char * arg
Definition jacosubdec.c:65
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
static int ff_slice_pos(int total, int jobnr, int nb_jobs)
Compute the boundary index for a slice when work of size total is split into nb_jobs slices.
Definition filters.h:763
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition filters.h:629
#define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter)
Forward the status on an output link to all input links.
Definition filters.h:652
#define AVFILTER_DEFINE_CLASS_EXT(name, desc, options)
Definition filters.h:470
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:241
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
const char * desc
Definition libsvtav1.c:83
static const struct @257111027162314367033347246032313251342043035002 planes[]
uint8_t w
Definition llvidencdsp.c:39
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
static uint8_t half(int a, int b)
Definition mobiclip.c:540
static int slice_end(AVCodecContext *avctx, AVFrame *pict, int *got_output)
Handle slice ends.
Definition mpeg12dec.c:1697
AVOptions.
@ EXT_STOP
Completely stop all streams with this one.
Definition packetsync.h:62
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3500
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_FLAG_FLOAT
The pixel format contains IEEE-754 floating point values.
Definition pixdesc.h:158
#define AV_PIX_FMT_GBRAP12
Definition pixfmt.h:569
#define AV_PIX_FMT_GBRPF32
Definition pixfmt.h:584
#define AV_PIX_FMT_YUV444P12
Definition pixfmt.h:552
#define AV_PIX_FMT_YUV444P9
Definition pixfmt.h:544
#define AV_PIX_FMT_GRAY9
Definition pixfmt.h:524
#define AV_PIX_FMT_GBRAP16
Definition pixfmt.h:571
#define AV_PIX_FMT_GBRP9
Definition pixfmt.h:563
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition pixfmt.h:766
@ AVCOL_RANGE_JPEG
Full range content.
Definition pixfmt.h:783
#define AV_PIX_FMT_YUVA444P10
Definition pixfmt.h:598
@ AVALPHA_MODE_STRAIGHT
Alpha channel is independent of color values.
Definition pixfmt.h:819
@ AVALPHA_MODE_PREMULTIPLIED
Alpha channel is multiplied into color values.
Definition pixfmt.h:818
#define AV_VIDEO_MAX_PLANES
Maximum number of planes in any pixel format.
Definition pixfmt.h:40
#define AV_PIX_FMT_GBRP10
Definition pixfmt.h:564
#define AV_PIX_FMT_GRAY12
Definition pixfmt.h:526
#define AV_PIX_FMT_GBRP12
Definition pixfmt.h:565
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition pixfmt.h:174
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition pixfmt.h:212
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition pixfmt.h:165
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition pixfmt.h:87
#define AV_PIX_FMT_GRAY10
Definition pixfmt.h:525
#define AV_PIX_FMT_GRAY14
Definition pixfmt.h:527
#define AV_PIX_FMT_GRAY16
Definition pixfmt.h:528
#define AV_PIX_FMT_GBRAP10
Definition pixfmt.h:568
#define AV_PIX_FMT_YUVA444P16
Definition pixfmt.h:603
#define AV_PIX_FMT_GBRP16
Definition pixfmt.h:567
#define AV_PIX_FMT_YUV444P14
Definition pixfmt.h:555
#define AV_PIX_FMT_YUVA444P9
Definition pixfmt.h:595
#define AV_PIX_FMT_GBRP14
Definition pixfmt.h:566
#define AV_PIX_FMT_YUVA444P12
Definition pixfmt.h:600
#define AV_PIX_FMT_GBRAPF32
Definition pixfmt.h:585
#define AV_PIX_FMT_YUV444P16
Definition pixfmt.h:558
#define AV_PIX_FMT_YUV444P10
Definition pixfmt.h:548
formats
Definition signature.h:47
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
Lists of formats / etc.
Definition avfilter.h:120
A list of supported formats for one end of a filter link.
Definition formats.h:64
A filter pad used for either input or output.
Definition filters.h:40
int(* config_props)(AVFilterLink *link)
Link configuration callback.
Definition filters.h:120
enum AVMediaType type
AVFilterPad type.
Definition filters.h:51
const char * name
Pad name.
Definition filters.h:46
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVOption.
Definition opt.h:428
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
Input stream structure.
Definition framesync.h:102
enum FFFrameSyncExtMode after
Extrapolation mode for timestamps after the last frame.
Definition framesync.h:112
enum FFFrameSyncExtMode before
Extrapolation mode for timestamps before the first frame.
Definition framesync.h:107
AVRational time_base
Time base for the incoming frames.
Definition framesync.h:117
unsigned sync
Synchronization level: frames on input at the highest sync level will generate output frame events.
Definition framesync.h:160
Frame sync structure.
Definition framesync.h:168
void(* premultiply[AV_VIDEO_MAX_PLANES])(const uint8_t *msrc, const uint8_t *asrc, uint8_t *dst, ptrdiff_t mlinesize, ptrdiff_t alinesize, ptrdiff_t dlinesize, int w, int h, int half, int shift, int offset)
int height[AV_VIDEO_MAX_PLANES]
int width[AV_VIDEO_MAX_PLANES]
int linesize[AV_VIDEO_MAX_PLANES]
Used for passing data between threads.
Definition dsddec.c:71
AVFrame * m
AVFrame * a
#define av_log(a,...)
static int64_t pts
char full[32]
static void hsub(htype *dst, const htype *src, int bins)
Definition vf_median.c:74
static enum AVPixelFormat alpha_pix_fmts[]
Definition vf_overlay.c:154
static void unpremultiply16offset(const uint8_t *mmsrc, const uint8_t *aasrc, uint8_t *ddst, ptrdiff_t mlinesize, ptrdiff_t alinesize, ptrdiff_t dlinesize, int w, int h, int half, int max, int offset)
static void unpremultiply16(const uint8_t *mmsrc, const uint8_t *aasrc, uint8_t *ddst, ptrdiff_t mlinesize, ptrdiff_t alinesize, ptrdiff_t dlinesize, int w, int h, int half, int max, int offset)
static void unpremultiply16yuv(const uint8_t *mmsrc, const uint8_t *aasrc, uint8_t *ddst, ptrdiff_t mlinesize, ptrdiff_t alinesize, ptrdiff_t dlinesize, int w, int h, int half, int max, int offset)
static void premultiply8offset(const uint8_t *msrc, const uint8_t *asrc, uint8_t *dst, ptrdiff_t mlinesize, ptrdiff_t alinesize, ptrdiff_t dlinesize, int w, int h, int half, int shift, int offset)
static const AVFilterPad premultiply_outputs[]
static int config_input(AVFilterLink *inlink)
static void premultiply8yuv(const uint8_t *msrc, const uint8_t *asrc, uint8_t *dst, ptrdiff_t mlinesize, ptrdiff_t alinesize, ptrdiff_t dlinesize, int w, int h, int half, int shift, int offset)
static void unpremultiply8(const uint8_t *msrc, const uint8_t *asrc, uint8_t *dst, ptrdiff_t mlinesize, ptrdiff_t alinesize, ptrdiff_t dlinesize, int w, int h, int half, int max, int offset)
static void unpremultiply8offset(const uint8_t *msrc, const uint8_t *asrc, uint8_t *dst, ptrdiff_t mlinesize, ptrdiff_t alinesize, ptrdiff_t dlinesize, int w, int h, int half, int max, int offset)
static void unpremultiply8yuv(const uint8_t *msrc, const uint8_t *asrc, uint8_t *dst, ptrdiff_t mlinesize, ptrdiff_t alinesize, ptrdiff_t dlinesize, int w, int h, int half, int max, int offset)
static void unpremultiplyf32offset(const uint8_t *mmsrc, const uint8_t *aasrc, uint8_t *ddst, ptrdiff_t mlinesize, ptrdiff_t alinesize, ptrdiff_t dlinesize, int w, int h, int half, int max, int offset)
static void premultiply16yuv(const uint8_t *mmsrc, const uint8_t *aasrc, uint8_t *ddst, ptrdiff_t mlinesize, ptrdiff_t alinesize, ptrdiff_t dlinesize, int w, int h, int half, int shift, int offset)
static void premultiplyf32(const uint8_t *mmsrc, const uint8_t *aasrc, uint8_t *ddst, ptrdiff_t mlinesize, ptrdiff_t alinesize, ptrdiff_t dlinesize, int w, int h, int half, int shift, int offset)
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
static int activate(AVFilterContext *ctx)
static void premultiply16offset(const uint8_t *mmsrc, const uint8_t *aasrc, uint8_t *ddst, ptrdiff_t mlinesize, ptrdiff_t alinesize, ptrdiff_t dlinesize, int w, int h, int half, int shift, int offset)
static av_cold void uninit(AVFilterContext *ctx)
static void premultiply16(const uint8_t *mmsrc, const uint8_t *aasrc, uint8_t *ddst, ptrdiff_t mlinesize, ptrdiff_t alinesize, ptrdiff_t dlinesize, int w, int h, int half, int shift, int offset)
#define OFFSET(x)
static int config_output(AVFilterLink *outlink)
static void premultiply8(const uint8_t *msrc, const uint8_t *asrc, uint8_t *dst, ptrdiff_t mlinesize, ptrdiff_t alinesize, ptrdiff_t dlinesize, int w, int h, int half, int shift, int offset)
static int premultiply_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
static int process_frame(FFFrameSync *fs)
static int filter_frame(AVFilterContext *ctx, AVFrame **out, AVFrame *base, AVFrame *alpha)
static void unpremultiplyf32(const uint8_t *mmsrc, const uint8_t *aasrc, uint8_t *ddst, ptrdiff_t mlinesize, ptrdiff_t alinesize, ptrdiff_t dlinesize, int w, int h, int half, int max, int offset)
static void premultiplyf32offset(const uint8_t *mmsrc, const uint8_t *aasrc, uint8_t *ddst, ptrdiff_t mlinesize, ptrdiff_t alinesize, ptrdiff_t dlinesize, int w, int h, int half, int shift, int offset)
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:89
uint8_t base
Definition vp3data.h:128
static int slice_start(SliceContext *sc, VVCContext *s, VVCFrameContext *fc, const CodedBitstreamUnit *unit, const int is_first_slice)
Definition dec.c:844