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