FFmpeg
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 "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;
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);
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 
67 {
68  PreMultiplyContext *s = ctx->priv;
69 
70  static const enum AVPixelFormat no_alpha_pix_fmts[] = {
79  };
80 
81  static const enum AVPixelFormat alpha_pix_fmts[] = {
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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:
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:
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:
579  break;
580  }
581  } else {
582  switch (outlink->format) {
583  case AV_PIX_FMT_YUV444P:
584  case AV_PIX_FMT_YUVA444P:
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:
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],
640  }
641 
642  return 0;
643 }
644 
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 
666 {
667  AVFilterContext *ctx = inlink->dst;
668  PreMultiplyContext *s = ctx->priv;
670  int vsub, hsub, ret;
671 
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 
692 static int config_output(AVFilterLink *outlink)
693 {
694  AVFilterContext *ctx = outlink->src;
695  PreMultiplyContext *s = ctx->priv;
696  AVFilterLink *base = ctx->inputs[0];
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;
742 
743  return ff_framesync_configure(&s->fs);
744 }
745 
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 
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]))
773  return 0;
774  }
775  } else {
776  return ff_framesync_activate(&s->fs);
777  }
778 }
779 
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";
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 
809 {
810  PreMultiplyContext *s = ctx->priv;
811 
812  if (!s->inplace)
813  ff_framesync_uninit(&s->fs);
814 }
815 
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 
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,
834  .activate = activate,
835  .inputs = NULL,
836  .outputs = premultiply_outputs,
837  .priv_class = &premultiply_class,
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 
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,
857  .activate = activate,
858  .inputs = NULL,
859  .outputs = premultiply_outputs,
860  .priv_class = &unpremultiply_class,
864 };
865 
866 #endif /* CONFIG_UNPREMULTIPLY_FILTER */
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link&#39;s FIFO and update the link&#39;s stats.
Definition: avfilter.c:1489
#define NULL
Definition: coverity.c:32
AVFrame * out
Definition: af_adeclick.c:494
static int shift(int a, int b)
Definition: sonic.c:82
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
static int activate(AVFilterContext *ctx)
This structure describes decoded (raw) audio or video data.
Definition: frame.h:308
AVOption.
Definition: opt.h:248
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)
#define AV_PIX_FMT_YUV444P14
Definition: pixfmt.h:409
#define AV_PIX_FMT_GBRAP10
Definition: pixfmt.h:419
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
misc image utilities
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2613
#define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter)
Forward the status on an output link to all input links.
Definition: filters.h:212
AVFrame * frame
Definition: dsddec.c:68
Main libavfilter public API header.
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)
#define AVFILTER_FLAG_DYNAMIC_INPUTS
The number of the filter inputs is not determined just by AVFilter.inputs.
Definition: avfilter.h:106
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)
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:168
static const AVOption options[]
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:415
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 AV_PIX_FMT_GRAY9
Definition: pixfmt.h:379
enum AVMediaType type
AVFilterPad type.
Definition: internal.h:65
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)
int ff_framesync_configure(FFFrameSync *fs)
Configure a frame sync structure.
Definition: framesync.c:124
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:99
int is_disabled
the enabled state from the last expression evaluation
Definition: avfilter.h:386
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
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:189
AVFILTER_DEFINE_CLASS(premultiply)
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Definition: avfilter.c:1615
enum FFFrameSyncExtMode before
Extrapolation mode for timestamps before the first frame.
Definition: framesync.h:86
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:287
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:380
const char * name
Pad name.
Definition: internal.h:60
AVFilterContext * parent
Parent filter context.
Definition: framesync.h:152
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:381
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:347
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1091
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
uint8_t
#define av_cold
Definition: attributes.h:88
#define fs(width, name, subs,...)
Definition: cbs_vp9.c:259
AVOptions.
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
#define f(width, name)
Definition: cbs_vp9.c:255
#define AV_PIX_FMT_FLAG_FLOAT
The pixel format contains IEEE-754 floating point values.
Definition: pixdesc.h:188
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:401
FFFrameSyncIn * in
Pointer to array of inputs.
Definition: framesync.h:203
#define AV_PIX_FMT_GBRP9
Definition: pixfmt.h:414
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)
int height
Definition: vf_avgblur.c:61
AVFrame * dst
Definition: vf_blend.c:56
#define FLAGS
int linesize
Definition: vf_avgblur.c:64
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:412
enum FFFrameSyncExtMode after
Extrapolation mode for timestamps after the last frame.
Definition: framesync.h:91
Input stream structure.
Definition: framesync.h:81
the definition of that something depends on the semantic of the filter The callback must examine the status of the filter s links and proceed accordingly The status of output links is stored in the status_in and status_out fields and tested by the ff_outlink_frame_wanted() function.If this function returns true
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:54
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
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:1444
AVFilterPad * input_pads
array of input pads
Definition: avfilter.h:346
static const struct @323 planes[]
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:588
#define td
Definition: regdef.h:70
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
void ff_framesync_uninit(FFFrameSync *fs)
Free all memory currently allocated.
Definition: framesync.c:290
Frame sync structure.
Definition: framesync.h:146
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:153
void(* premultiply[4])(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)
void * priv
private data for use by the filter
Definition: avfilter.h:354
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: frame.h:552
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:117
#define AV_PIX_FMT_YUVA444P16
Definition: pixfmt.h:443
const char * arg
Definition: jacosubdec.c:66
AVFilter ff_vf_premultiply
#define AV_PIX_FMT_GBRAP12
Definition: pixfmt.h:420
AVRational time_base
Time base for the incoming frames.
Definition: framesync.h:96
int ff_framesync_activate(FFFrameSync *fs)
Examine the frames in the filter&#39;s input and try to produce output.
Definition: framesync.c:341
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:402
int(* on_event)(struct FFFrameSync *fs)
Callback called when a frame event is ready.
Definition: framesync.h:172
#define FFMAX(a, b)
Definition: common.h:94
#define AV_PIX_FMT_GBRAP16
Definition: pixfmt.h:421
uint64_t flags
Combination of AV_PIX_FMT_FLAG_...
Definition: pixdesc.h:106
int(* config_props)(AVFilterLink *link)
Link configuration callback.
Definition: internal.h:118
#define AV_PIX_FMT_GBRP16
Definition: pixfmt.h:418
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:800
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:383
#define FFMIN(a, b)
Definition: common.h:96
#define AV_PIX_FMT_YUVA444P12
Definition: pixfmt.h:440
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
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)
AVFrame * m
AVFormatContext * ctx
Definition: movenc.c:48
AVRational time_base
Time base for the output events.
Definition: framesync.h:162
static const AVFilterPad premultiply_outputs[]
#define AV_PIX_FMT_YUVA444P10
Definition: pixfmt.h:438
static av_cold void uninit(AVFilterContext *ctx)
void * opaque
Opaque pointer, not used by the API.
Definition: framesync.h:177
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:398
#define AV_PIX_FMT_GBRP14
Definition: pixfmt.h:417
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:553
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
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)
Full range content.
Definition: pixfmt.h:586
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)
Extend the frame to infinity.
Definition: framesync.h:75
static int process_frame(FFFrameSync *fs)
Used for passing data between threads.
Definition: dsddec.c:67
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:339
int ff_framesync_init(FFFrameSync *fs, AVFilterContext *parent, unsigned nb_in)
Initialize a frame sync structure.
Definition: framesync.c:84
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:177
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
static const int16_t alpha[]
Definition: ilbcdata.h:55
#define AV_PIX_FMT_GRAY14
Definition: pixfmt.h:382
AVFilter ff_vf_unpremultiply
static int query_formats(AVFilterContext *ctx)
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
unsigned sync
Synchronization level: frames on input at the highest sync level will generate output frame events...
Definition: framesync.h:139
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:145
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 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)
they must not be accessed directly The fifo field contains the frames that are queued in the input for processing by the filter The status_in and status_out fields contains the queued status(EOF or error) of the link
static int config_output(AVFilterLink *outlink)
const char * name
Filter name.
Definition: avfilter.h:149
#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:134
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:351
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 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)
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:416
static int64_t pts
#define flags(name, subs,...)
Definition: cbs_av1.c:560
AVFilterInternal * internal
An opaque struct for libavfilter internal use.
Definition: avfilter.h:379
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:406
static int config_input(AVFilterLink *inlink)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:322
Narrow or limited range content.
Definition: pixfmt.h:569
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)
#define AV_PIX_FMT_GBRPF32
Definition: pixfmt.h:428
AVFrame * base
Y , 8bpp.
Definition: pixfmt.h:74
#define AV_PIX_FMT_GBRAPF32
Definition: pixfmt.h:429
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:215
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 int premultiply_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
static int filter_frame(AVFilterContext *ctx, AVFrame **out, AVFrame *base, AVFrame *alpha)
#define AV_PIX_FMT_YUVA444P9
Definition: pixfmt.h:435
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:80
avfilter_execute_func * execute
Definition: internal.h:136
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:2039
Completely stop all streams with this one.
Definition: framesync.h:65
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)
const AVPixFmtDescriptor * desc
Definition: vf_tonemap.c:196
static uint8_t half(int a, int b)
Definition: mobiclip.c:605
An instance of a filter.
Definition: avfilter.h:339
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)
const void ** s
AVFrame * in
Definition: af_afftdn.c:1083
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:373
AVFilterLink * inlink
Definition: vf_blend.c:57
internal API functions
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
int ff_framesync_get_frame(FFFrameSync *fs, unsigned in, AVFrame **rframe, unsigned get)
Get the current frame in an input.
Definition: framesync.c:253
int depth
Number of bits in the component.
Definition: pixdesc.h:58
AVFrame * a
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
const AVFilter * filter
the AVFilter of which this is an instance
Definition: avfilter.h:342
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:671
static av_cold int init(AVFilterContext *ctx)
static enum AVPixelFormat alpha_pix_fmts[]
Definition: vf_overlay.c:155
AVFrame * max
Definition: vf_threshold.c:75
#define OFFSET(x)
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
static int ff_insert_inpad(AVFilterContext *f, unsigned index, AVFilterPad *p)
Insert a new input pad for the filter.
Definition: internal.h:240