FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_premultiply.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 0 385 0.0%
Functions: 0 25 0.0%
Branches: 0 210 0.0%

Line Branch Exec Source
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
32 typedef struct ThreadData {
33 AVFrame *m, *a, *d;
34 } ThreadData;
35
36 typedef struct PreMultiplyContext {
37 const AVClass *class;
38 int width[AV_VIDEO_MAX_PLANES], height[AV_VIDEO_MAX_PLANES];
39 int linesize[AV_VIDEO_MAX_PLANES];
40 int nb_planes;
41 int planes;
42 int inverse;
43 int inplace;
44 int half, depth, offset, max;
45 FFFrameSync fs;
46
47 void (*premultiply[AV_VIDEO_MAX_PLANES])(const uint8_t *msrc, const uint8_t *asrc,
48 uint8_t *dst,
49 ptrdiff_t mlinesize, ptrdiff_t alinesize,
50 ptrdiff_t dlinesize,
51 int w, int h,
52 int half, int shift, int offset);
53 } PreMultiplyContext;
54
55 #define OFFSET(x) offsetof(PreMultiplyContext, x)
56 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
57
58 static const AVOption options[] = {
59 { "planes", "set planes", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
60 { "inplace","enable inplace mode", OFFSET(inplace), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
61 { NULL }
62 };
63
64 AVFILTER_DEFINE_CLASS_EXT(premultiply, "(un)premultiply", options);
65
66 static int query_formats(const AVFilterContext *ctx,
67 AVFilterFormatsConfig **cfg_in,
68 AVFilterFormatsConfig **cfg_out)
69 {
70 const PreMultiplyContext *s = ctx->priv;
71
72 static const enum AVPixelFormat no_alpha_pix_fmts[] = {
73 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
74 AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV444P10,
75 AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV444P14,
76 AV_PIX_FMT_YUV444P16,
77 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
78 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRPF32,
79 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
80 AV_PIX_FMT_NONE
81 };
82
83 static const enum AVPixelFormat alpha_pix_fmts[] = {
84 AV_PIX_FMT_YUVA444P,
85 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
86 AV_PIX_FMT_GBRAP,
87 AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16, AV_PIX_FMT_GBRAPF32,
88 AV_PIX_FMT_NONE
89 };
90
91 return ff_set_common_formats_from_list2(ctx, cfg_in, cfg_out,
92 s->inplace ? alpha_pix_fmts : no_alpha_pix_fmts);
93 }
94
95 static void premultiply8(const uint8_t *msrc, const uint8_t *asrc,
96 uint8_t *dst,
97 ptrdiff_t mlinesize, ptrdiff_t alinesize,
98 ptrdiff_t dlinesize,
99 int w, int h,
100 int half, int shift, int offset)
101 {
102 int x, y;
103
104 for (y = 0; y < h; y++) {
105 for (x = 0; x < w; x++) {
106 dst[x] = ((msrc[x] * (((asrc[x] >> 1) & 1) + asrc[x])) + 128) >> 8;
107 }
108
109 dst += dlinesize;
110 msrc += mlinesize;
111 asrc += alinesize;
112 }
113 }
114
115 static void premultiply8yuv(const uint8_t *msrc, const uint8_t *asrc,
116 uint8_t *dst,
117 ptrdiff_t mlinesize, ptrdiff_t alinesize,
118 ptrdiff_t dlinesize,
119 int w, int h,
120 int half, int shift, int offset)
121 {
122 int x, y;
123
124 for (y = 0; y < h; y++) {
125 for (x = 0; x < w; x++) {
126 dst[x] = ((((msrc[x] - 128) * (((asrc[x] >> 1) & 1) + asrc[x]))) >> 8) + 128;
127 }
128
129 dst += dlinesize;
130 msrc += mlinesize;
131 asrc += alinesize;
132 }
133 }
134
135 static void premultiply8offset(const uint8_t *msrc, const uint8_t *asrc,
136 uint8_t *dst,
137 ptrdiff_t mlinesize, ptrdiff_t alinesize,
138 ptrdiff_t dlinesize,
139 int w, int h,
140 int half, int shift, int offset)
141 {
142 int x, y;
143
144 for (y = 0; y < h; y++) {
145 for (x = 0; x < w; x++) {
146 dst[x] = ((((msrc[x] - offset) * (((asrc[x] >> 1) & 1) + asrc[x])) + 128) >> 8) + offset;
147 }
148
149 dst += dlinesize;
150 msrc += mlinesize;
151 asrc += alinesize;
152 }
153 }
154
155 static void premultiply16(const uint8_t *mmsrc, const uint8_t *aasrc,
156 uint8_t *ddst,
157 ptrdiff_t mlinesize, ptrdiff_t alinesize,
158 ptrdiff_t dlinesize,
159 int w, int h,
160 int half, int shift, int offset)
161 {
162 const uint16_t *msrc = (const uint16_t *)mmsrc;
163 const uint16_t *asrc = (const uint16_t *)aasrc;
164 uint16_t *dst = (uint16_t *)ddst;
165 int x, y;
166
167 for (y = 0; y < h; y++) {
168 for (x = 0; x < w; x++) {
169 dst[x] = ((msrc[x] * (((asrc[x] >> 1) & 1) + asrc[x])) + half) >> shift;
170 }
171
172 dst += dlinesize / 2;
173 msrc += mlinesize / 2;
174 asrc += alinesize / 2;
175 }
176 }
177
178 static void premultiply16yuv(const uint8_t *mmsrc, const uint8_t *aasrc,
179 uint8_t *ddst,
180 ptrdiff_t mlinesize, ptrdiff_t alinesize,
181 ptrdiff_t dlinesize,
182 int w, int h,
183 int half, int shift, int offset)
184 {
185 const uint16_t *msrc = (const uint16_t *)mmsrc;
186 const uint16_t *asrc = (const uint16_t *)aasrc;
187 uint16_t *dst = (uint16_t *)ddst;
188 int x, y;
189
190 for (y = 0; y < h; y++) {
191 for (x = 0; x < w; x++) {
192 dst[x] = ((((msrc[x] - half) * (int64_t)(((asrc[x] >> 1) & 1) + asrc[x]))) >> shift) + half;
193 }
194
195 dst += dlinesize / 2;
196 msrc += mlinesize / 2;
197 asrc += alinesize / 2;
198 }
199 }
200
201 static void premultiply16offset(const uint8_t *mmsrc, const uint8_t *aasrc,
202 uint8_t *ddst,
203 ptrdiff_t mlinesize, ptrdiff_t alinesize,
204 ptrdiff_t dlinesize,
205 int w, int h,
206 int half, int shift, int offset)
207 {
208 const uint16_t *msrc = (const uint16_t *)mmsrc;
209 const uint16_t *asrc = (const uint16_t *)aasrc;
210 uint16_t *dst = (uint16_t *)ddst;
211 int x, y;
212
213 for (y = 0; y < h; y++) {
214 for (x = 0; x < w; x++) {
215 dst[x] = ((((msrc[x] - offset) * (int64_t)(((asrc[x] >> 1) & 1) + asrc[x])) + half) >> shift) + offset;
216 }
217
218 dst += dlinesize / 2;
219 msrc += mlinesize / 2;
220 asrc += alinesize / 2;
221 }
222 }
223
224 static void premultiplyf32(const uint8_t *mmsrc, const uint8_t *aasrc,
225 uint8_t *ddst,
226 ptrdiff_t mlinesize, ptrdiff_t alinesize,
227 ptrdiff_t dlinesize,
228 int w, int h,
229 int half, int shift, int offset)
230 {
231 const float *msrc = (const float *)mmsrc;
232 const float *asrc = (const float *)aasrc;
233 float *dst = (float *)ddst;
234 int x, y;
235
236 for (y = 0; y < h; y++) {
237 for (x = 0; x < w; x++) {
238 dst[x] = msrc[x] * asrc[x];
239 }
240
241 dst += dlinesize / 4;
242 msrc += mlinesize / 4;
243 asrc += alinesize / 4;
244 }
245 }
246
247 static void premultiplyf32offset(const uint8_t *mmsrc, const uint8_t *aasrc,
248 uint8_t *ddst,
249 ptrdiff_t mlinesize, ptrdiff_t alinesize,
250 ptrdiff_t dlinesize,
251 int w, int h,
252 int half, int shift, int offset)
253 {
254 const float *msrc = (const float *)mmsrc;
255 const float *asrc = (const float *)aasrc;
256 float *dst = (float *)ddst;
257 int x, y;
258
259 float offsetf = offset / 65535.0f;
260
261 for (y = 0; y < h; y++) {
262 for (x = 0; x < w; x++) {
263 dst[x] = ((msrc[x] - offsetf) * asrc[x]) + offsetf;
264 }
265
266 dst += dlinesize / 4;
267 msrc += mlinesize / 4;
268 asrc += alinesize / 4;
269 }
270 }
271
272 static void unpremultiply8(const uint8_t *msrc, const uint8_t *asrc,
273 uint8_t *dst,
274 ptrdiff_t mlinesize, ptrdiff_t alinesize,
275 ptrdiff_t dlinesize,
276 int w, int h,
277 int half, int max, int offset)
278 {
279 int x, y;
280
281 for (y = 0; y < h; y++) {
282 for (x = 0; x < w; x++) {
283 if (asrc[x] > 0 && asrc[x] < 255)
284 dst[x] = FFMIN(msrc[x] * 255 / asrc[x], 255);
285 else
286 dst[x] = msrc[x];
287 }
288
289 dst += dlinesize;
290 msrc += mlinesize;
291 asrc += alinesize;
292 }
293 }
294
295 static void unpremultiply8yuv(const uint8_t *msrc, const uint8_t *asrc,
296 uint8_t *dst,
297 ptrdiff_t mlinesize, ptrdiff_t alinesize,
298 ptrdiff_t dlinesize,
299 int w, int h,
300 int half, int max, int offset)
301 {
302 int x, y;
303
304 for (y = 0; y < h; y++) {
305 for (x = 0; x < w; x++) {
306 if (asrc[x] > 0 && asrc[x] < 255)
307 dst[x] = FFMIN((msrc[x] - 128) * 255 / asrc[x] + 128, 255);
308 else
309 dst[x] = msrc[x];
310 }
311
312 dst += dlinesize;
313 msrc += mlinesize;
314 asrc += alinesize;
315 }
316 }
317
318 static void unpremultiply8offset(const uint8_t *msrc, const uint8_t *asrc,
319 uint8_t *dst,
320 ptrdiff_t mlinesize, ptrdiff_t alinesize,
321 ptrdiff_t dlinesize,
322 int w, int h,
323 int half, int max, int offset)
324 {
325 int x, y;
326
327 for (y = 0; y < h; y++) {
328 for (x = 0; x < w; x++) {
329 if (asrc[x] > 0 && asrc[x] < 255)
330 dst[x] = FFMIN(FFMAX(msrc[x] - offset, 0) * 255 / asrc[x] + offset, 255);
331 else
332 dst[x] = msrc[x];
333 }
334
335 dst += dlinesize;
336 msrc += mlinesize;
337 asrc += alinesize;
338 }
339 }
340
341 static void unpremultiply16(const uint8_t *mmsrc, const uint8_t *aasrc,
342 uint8_t *ddst,
343 ptrdiff_t mlinesize, ptrdiff_t alinesize,
344 ptrdiff_t dlinesize,
345 int w, int h,
346 int half, int max, int offset)
347 {
348 const uint16_t *msrc = (const uint16_t *)mmsrc;
349 const uint16_t *asrc = (const uint16_t *)aasrc;
350 uint16_t *dst = (uint16_t *)ddst;
351 int x, y;
352
353 for (y = 0; y < h; y++) {
354 for (x = 0; x < w; x++) {
355 if (asrc[x] > 0 && asrc[x] < max)
356 dst[x] = FFMIN(msrc[x] * (unsigned)max / asrc[x], max);
357 else
358 dst[x] = msrc[x];
359 }
360
361 dst += dlinesize / 2;
362 msrc += mlinesize / 2;
363 asrc += alinesize / 2;
364 }
365 }
366
367 static void unpremultiply16yuv(const uint8_t *mmsrc, const uint8_t *aasrc,
368 uint8_t *ddst,
369 ptrdiff_t mlinesize, ptrdiff_t alinesize,
370 ptrdiff_t dlinesize,
371 int w, int h,
372 int half, int max, int offset)
373 {
374 const uint16_t *msrc = (const uint16_t *)mmsrc;
375 const uint16_t *asrc = (const uint16_t *)aasrc;
376 uint16_t *dst = (uint16_t *)ddst;
377 int x, y;
378
379 for (y = 0; y < h; y++) {
380 for (x = 0; x < w; x++) {
381 if (asrc[x] > 0 && asrc[x] < max)
382 dst[x] = FFMAX(FFMIN((msrc[x] - half) * max / asrc[x], half - 1), -half) + half;
383 else
384 dst[x] = msrc[x];
385 }
386
387 dst += dlinesize / 2;
388 msrc += mlinesize / 2;
389 asrc += alinesize / 2;
390 }
391 }
392
393 static void unpremultiply16offset(const uint8_t *mmsrc, const uint8_t *aasrc,
394 uint8_t *ddst,
395 ptrdiff_t mlinesize, ptrdiff_t alinesize,
396 ptrdiff_t dlinesize,
397 int w, int h,
398 int half, int max, int offset)
399 {
400 const uint16_t *msrc = (const uint16_t *)mmsrc;
401 const uint16_t *asrc = (const uint16_t *)aasrc;
402 uint16_t *dst = (uint16_t *)ddst;
403 int x, y;
404
405 for (y = 0; y < h; y++) {
406 for (x = 0; x < w; x++) {
407 if (asrc[x] > 0 && asrc[x] < max)
408 dst[x] = FFMAX(FFMIN(FFMAX(msrc[x] - offset, 0) * (unsigned)max / asrc[x] + offset, max), 0);
409 else
410 dst[x] = msrc[x];
411 }
412
413 dst += dlinesize / 2;
414 msrc += mlinesize / 2;
415 asrc += alinesize / 2;
416 }
417 }
418
419 static void unpremultiplyf32(const uint8_t *mmsrc, const uint8_t *aasrc,
420 uint8_t *ddst,
421 ptrdiff_t mlinesize, ptrdiff_t alinesize,
422 ptrdiff_t dlinesize,
423 int w, int h,
424 int half, int max, int offset)
425 {
426 const float *msrc = (const float *)mmsrc;
427 const float *asrc = (const float *)aasrc;
428
429 float *dst = (float *)ddst;
430 int x, y;
431
432 for (y = 0; y < h; y++) {
433 for (x = 0; x < w; x++) {
434 if (asrc[x] > 0.0f)
435 dst[x] = msrc[x] / asrc[x];
436 else
437 dst[x] = msrc[x];
438 }
439
440 dst += dlinesize / 4;
441 msrc += mlinesize / 4;
442 asrc += alinesize / 4;
443 }
444 }
445
446 static void unpremultiplyf32offset(const uint8_t *mmsrc, const uint8_t *aasrc,
447 uint8_t *ddst,
448 ptrdiff_t mlinesize, ptrdiff_t alinesize,
449 ptrdiff_t dlinesize,
450 int w, int h,
451 int half, int max, int offset)
452 {
453 const float *msrc = (const float *)mmsrc;
454 const float *asrc = (const float *)aasrc;
455
456 float *dst = (float *)ddst;
457 int x, y;
458
459 float offsetf = offset / 65535.0f;
460
461 for (y = 0; y < h; y++) {
462 for (x = 0; x < w; x++) {
463 if (asrc[x] > 0.0f)
464 dst[x] = (msrc[x] - offsetf) / asrc[x] + offsetf;
465 else
466 dst[x] = msrc[x];
467 }
468
469 dst += dlinesize / 4;
470 msrc += mlinesize / 4;
471 asrc += alinesize / 4;
472 }
473 }
474
475 static int premultiply_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
476 {
477 PreMultiplyContext *s = ctx->priv;
478 ThreadData *td = arg;
479 AVFrame *out = td->d;
480 AVFrame *alpha = td->a;
481 AVFrame *base = td->m;
482 int p;
483
484 for (p = 0; p < s->nb_planes; p++) {
485 const int slice_start = (s->height[p] * jobnr) / nb_jobs;
486 const int slice_end = (s->height[p] * (jobnr+1)) / nb_jobs;
487
488 if (!((1 << p) & s->planes) || p == 3) {
489 av_image_copy_plane(out->data[p] + slice_start * out->linesize[p],
490 out->linesize[p],
491 base->data[p] + slice_start * base->linesize[p],
492 base->linesize[p],
493 s->linesize[p], slice_end - slice_start);
494 continue;
495 }
496
497 s->premultiply[p](base->data[p] + slice_start * base->linesize[p],
498 s->inplace ? alpha->data[3] + slice_start * alpha->linesize[3] :
499 alpha->data[0] + slice_start * alpha->linesize[0],
500 out->data[p] + slice_start * out->linesize[p],
501 base->linesize[p], s->inplace ? alpha->linesize[3] : alpha->linesize[0],
502 out->linesize[p],
503 s->width[p], slice_end - slice_start,
504 s->half, s->inverse ? s->max : s->depth, s->offset);
505 }
506
507 return 0;
508 }
509
510 static int filter_frame(AVFilterContext *ctx,
511 AVFrame **out, AVFrame *base, AVFrame *alpha)
512 {
513 PreMultiplyContext *s = ctx->priv;
514 AVFilterLink *outlink = ctx->outputs[0];
515
516 if (ctx->is_disabled) {
517 *out = av_frame_clone(base);
518 if (!*out)
519 return AVERROR(ENOMEM);
520 } else {
521 ThreadData td;
522 int full, limited;
523
524 *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
525 if (!*out)
526 return AVERROR(ENOMEM);
527 av_frame_copy_props(*out, base);
528
529 full = base->color_range == AVCOL_RANGE_JPEG;
530 limited = base->color_range == AVCOL_RANGE_MPEG;
531
532 if (s->inverse) {
533 switch (outlink->format) {
534 case AV_PIX_FMT_YUV444P:
535 case AV_PIX_FMT_YUVA444P:
536 s->premultiply[0] = full ? unpremultiply8 : unpremultiply8offset;
537 s->premultiply[1] = s->premultiply[2] = unpremultiply8yuv;
538 break;
539 case AV_PIX_FMT_YUVJ444P:
540 s->premultiply[0] = unpremultiply8;
541 s->premultiply[1] = s->premultiply[2] = unpremultiply8yuv;
542 break;
543 case AV_PIX_FMT_GBRP:
544 case AV_PIX_FMT_GBRAP:
545 s->premultiply[0] = s->premultiply[1] = s->premultiply[2] = limited ? unpremultiply8offset : unpremultiply8;
546 break;
547 case AV_PIX_FMT_YUV444P9:
548 case AV_PIX_FMT_YUVA444P9:
549 case AV_PIX_FMT_YUV444P10:
550 case AV_PIX_FMT_YUVA444P10:
551 case AV_PIX_FMT_YUV444P12:
552 case AV_PIX_FMT_YUVA444P12:
553 case AV_PIX_FMT_YUV444P14:
554 case AV_PIX_FMT_YUV444P16:
555 case AV_PIX_FMT_YUVA444P16:
556 s->premultiply[0] = full ? unpremultiply16 : unpremultiply16offset;
557 s->premultiply[1] = s->premultiply[2] = unpremultiply16yuv;
558 break;
559 case AV_PIX_FMT_GBRP9:
560 case AV_PIX_FMT_GBRP10:
561 case AV_PIX_FMT_GBRAP10:
562 case AV_PIX_FMT_GBRP12:
563 case AV_PIX_FMT_GBRAP12:
564 case AV_PIX_FMT_GBRP14:
565 case AV_PIX_FMT_GBRP16:
566 case AV_PIX_FMT_GBRAP16:
567 s->premultiply[0] = s->premultiply[1] = s->premultiply[2] = limited ? unpremultiply16offset : unpremultiply16;
568 break;
569 case AV_PIX_FMT_GBRPF32:
570 case AV_PIX_FMT_GBRAPF32:
571 s->premultiply[0] = s->premultiply[1] = s->premultiply[2] = limited ? unpremultiplyf32offset : unpremultiplyf32;
572 break;
573 case AV_PIX_FMT_GRAY8:
574 s->premultiply[0] = limited ? unpremultiply8offset : unpremultiply8;
575 break;
576 case AV_PIX_FMT_GRAY9:
577 case AV_PIX_FMT_GRAY10:
578 case AV_PIX_FMT_GRAY12:
579 case AV_PIX_FMT_GRAY14:
580 case AV_PIX_FMT_GRAY16:
581 s->premultiply[0] = limited ? unpremultiply16offset : unpremultiply16;
582 break;
583 }
584 } else {
585 switch (outlink->format) {
586 case AV_PIX_FMT_YUV444P:
587 case AV_PIX_FMT_YUVA444P:
588 s->premultiply[0] = full ? premultiply8 : premultiply8offset;
589 s->premultiply[1] = s->premultiply[2] = premultiply8yuv;
590 break;
591 case AV_PIX_FMT_YUVJ444P:
592 s->premultiply[0] = premultiply8;
593 s->premultiply[1] = s->premultiply[2] = premultiply8yuv;
594 break;
595 case AV_PIX_FMT_GBRP:
596 case AV_PIX_FMT_GBRAP:
597 s->premultiply[0] = s->premultiply[1] = s->premultiply[2] = limited ? premultiply8offset : premultiply8;
598 break;
599 case AV_PIX_FMT_YUV444P9:
600 case AV_PIX_FMT_YUVA444P9:
601 case AV_PIX_FMT_YUV444P10:
602 case AV_PIX_FMT_YUVA444P10:
603 case AV_PIX_FMT_YUV444P12:
604 case AV_PIX_FMT_YUVA444P12:
605 case AV_PIX_FMT_YUV444P14:
606 case AV_PIX_FMT_YUV444P16:
607 case AV_PIX_FMT_YUVA444P16:
608 s->premultiply[0] = full ? premultiply16 : premultiply16offset;
609 s->premultiply[1] = s->premultiply[2] = premultiply16yuv;
610 break;
611 case AV_PIX_FMT_GBRP9:
612 case AV_PIX_FMT_GBRP10:
613 case AV_PIX_FMT_GBRAP10:
614 case AV_PIX_FMT_GBRP12:
615 case AV_PIX_FMT_GBRAP12:
616 case AV_PIX_FMT_GBRP14:
617 case AV_PIX_FMT_GBRP16:
618 case AV_PIX_FMT_GBRAP16:
619 s->premultiply[0] = s->premultiply[1] = s->premultiply[2] = limited ? premultiply16offset : premultiply16;
620 break;
621 case AV_PIX_FMT_GBRPF32:
622 case AV_PIX_FMT_GBRAPF32:
623 s->premultiply[0] = s->premultiply[1] = s->premultiply[2] = limited ? premultiplyf32offset: premultiplyf32;
624 break;
625 case AV_PIX_FMT_GRAY8:
626 s->premultiply[0] = limited ? premultiply8offset : premultiply8;
627 break;
628 case AV_PIX_FMT_GRAY9:
629 case AV_PIX_FMT_GRAY10:
630 case AV_PIX_FMT_GRAY12:
631 case AV_PIX_FMT_GRAY14:
632 case AV_PIX_FMT_GRAY16:
633 s->premultiply[0] = limited ? premultiply16offset : premultiply16;
634 break;
635 }
636 }
637
638 td.d = *out;
639 td.a = alpha;
640 td.m = base;
641 ff_filter_execute(ctx, premultiply_slice, &td, NULL,
642 FFMIN(s->height[0], ff_filter_get_nb_threads(ctx)));
643 }
644
645 return 0;
646 }
647
648 static int process_frame(FFFrameSync *fs)
649 {
650 AVFilterContext *ctx = fs->parent;
651 PreMultiplyContext *s = fs->opaque;
652 AVFilterLink *outlink = ctx->outputs[0];
653 AVFrame *out = NULL, *base, *alpha;
654 int ret;
655
656 if ((ret = ff_framesync_get_frame(&s->fs, 0, &base, 0)) < 0 ||
657 (ret = ff_framesync_get_frame(&s->fs, 1, &alpha, 0)) < 0)
658 return ret;
659
660 if ((ret = filter_frame(ctx, &out, base, alpha)) < 0)
661 return ret;
662
663 out->pts = av_rescale_q(base->pts, s->fs.time_base, outlink->time_base);
664
665 return ff_filter_frame(outlink, out);
666 }
667
668 static int config_input(AVFilterLink *inlink)
669 {
670 AVFilterContext *ctx = inlink->dst;
671 PreMultiplyContext *s = ctx->priv;
672 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
673 int vsub, hsub, ret;
674
675 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
676
677 if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
678 return ret;
679
680 hsub = desc->log2_chroma_w;
681 vsub = desc->log2_chroma_h;
682 s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
683 s->height[0] = s->height[3] = inlink->h;
684 s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
685 s->width[0] = s->width[3] = inlink->w;
686
687 s->depth = desc->flags & AV_PIX_FMT_FLAG_FLOAT ? 16 : desc->comp[0].depth;
688 s->max = (1 << s->depth) - 1;
689 s->half = (1 << s->depth) / 2;
690 s->offset = 16 << (s->depth - 8);
691
692 return 0;
693 }
694
695 static int config_output(AVFilterLink *outlink)
696 {
697 AVFilterContext *ctx = outlink->src;
698 PreMultiplyContext *s = ctx->priv;
699 AVFilterLink *base = ctx->inputs[0];
700 AVFilterLink *alpha;
701 FilterLink *il = ff_filter_link(base);
702 FilterLink *ol = ff_filter_link(outlink);
703 FFFrameSyncIn *in;
704 int ret;
705
706 if (!s->inplace) {
707 alpha = ctx->inputs[1];
708
709 if (base->w != alpha->w ||
710 base->h != alpha->h) {
711 av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
712 "(size %dx%d) do not match the corresponding "
713 "second input link %s parameters (%dx%d) ",
714 ctx->input_pads[0].name, base->w, base->h,
715 ctx->input_pads[1].name, alpha->w, alpha->h);
716 return AVERROR(EINVAL);
717 }
718 }
719
720 outlink->w = base->w;
721 outlink->h = base->h;
722 outlink->time_base = base->time_base;
723 outlink->sample_aspect_ratio = base->sample_aspect_ratio;
724 ol->frame_rate = il->frame_rate;
725
726 if (s->inplace)
727 return 0;
728
729 if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0)
730 return ret;
731
732 in = s->fs.in;
733 in[0].time_base = base->time_base;
734 in[1].time_base = alpha->time_base;
735 in[0].sync = 1;
736 in[0].before = EXT_STOP;
737 in[0].after = EXT_INFINITY;
738 in[1].sync = 1;
739 in[1].before = EXT_STOP;
740 in[1].after = EXT_INFINITY;
741 s->fs.opaque = s;
742 s->fs.on_event = process_frame;
743
744 return ff_framesync_configure(&s->fs);
745 }
746
747 static int activate(AVFilterContext *ctx)
748 {
749 PreMultiplyContext *s = ctx->priv;
750
751 if (s->inplace) {
752 AVFrame *frame = NULL;
753 AVFrame *out = NULL;
754 int ret, status;
755 int64_t pts;
756
757 FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx);
758
759 if ((ret = ff_inlink_consume_frame(ctx->inputs[0], &frame)) > 0) {
760 ret = filter_frame(ctx, &out, frame, frame);
761 av_frame_free(&frame);
762 if (ret < 0)
763 return ret;
764 ret = ff_filter_frame(ctx->outputs[0], out);
765 }
766 if (ret < 0) {
767 return ret;
768 } else if (ff_inlink_acknowledge_status(ctx->inputs[0], &status, &pts)) {
769 ff_outlink_set_status(ctx->outputs[0], status, pts);
770 return 0;
771 } else {
772 if (ff_outlink_frame_wanted(ctx->outputs[0]))
773 ff_inlink_request_frame(ctx->inputs[0]);
774 return 0;
775 }
776 } else {
777 return ff_framesync_activate(&s->fs);
778 }
779 }
780
781 static av_cold int init(AVFilterContext *ctx)
782 {
783 PreMultiplyContext *s = ctx->priv;
784 AVFilterPad pad = { 0 };
785 int ret;
786
787 if (!strcmp(ctx->filter->name, "unpremultiply"))
788 s->inverse = 1;
789
790 pad.type = AVMEDIA_TYPE_VIDEO;
791 pad.name = "main";
792 pad.config_props = config_input;
793
794 if ((ret = ff_append_inpad(ctx, &pad)) < 0)
795 return ret;
796
797 if (!s->inplace) {
798 pad.type = AVMEDIA_TYPE_VIDEO;
799 pad.name = "alpha";
800 pad.config_props = NULL;
801
802 if ((ret = ff_append_inpad(ctx, &pad)) < 0)
803 return ret;
804 }
805
806 return 0;
807 }
808
809 static av_cold void uninit(AVFilterContext *ctx)
810 {
811 PreMultiplyContext *s = ctx->priv;
812
813 if (!s->inplace)
814 ff_framesync_uninit(&s->fs);
815 }
816
817 static const AVFilterPad premultiply_outputs[] = {
818 {
819 .name = "default",
820 .type = AVMEDIA_TYPE_VIDEO,
821 .config_props = config_output,
822 },
823 };
824
825 #if CONFIG_PREMULTIPLY_FILTER
826
827 const AVFilter ff_vf_premultiply = {
828 .name = "premultiply",
829 .description = NULL_IF_CONFIG_SMALL("PreMultiply first stream with first plane of second stream."),
830 .priv_size = sizeof(PreMultiplyContext),
831 .init = init,
832 .uninit = uninit,
833 .activate = activate,
834 .inputs = NULL,
835 FILTER_OUTPUTS(premultiply_outputs),
836 FILTER_QUERY_FUNC2(query_formats),
837 .priv_class = &premultiply_class,
838 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
839 AVFILTER_FLAG_DYNAMIC_INPUTS |
840 AVFILTER_FLAG_SLICE_THREADS,
841 };
842
843 #endif /* CONFIG_PREMULTIPLY_FILTER */
844
845 #if CONFIG_UNPREMULTIPLY_FILTER
846
847 const AVFilter ff_vf_unpremultiply = {
848 .name = "unpremultiply",
849 .description = NULL_IF_CONFIG_SMALL("UnPreMultiply first stream with first plane of second stream."),
850 .priv_class = &premultiply_class,
851 .priv_size = sizeof(PreMultiplyContext),
852 .init = init,
853 .uninit = uninit,
854 .activate = activate,
855 .inputs = NULL,
856 FILTER_OUTPUTS(premultiply_outputs),
857 FILTER_QUERY_FUNC2(query_formats),
858 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
859 AVFILTER_FLAG_DYNAMIC_INPUTS |
860 AVFILTER_FLAG_SLICE_THREADS,
861 };
862
863 #endif /* CONFIG_UNPREMULTIPLY_FILTER */
864