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