FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 PreMultiplyContext {
32  const AVClass *class;
33  int width[4], height[4];
34  int linesize[4];
35  int nb_planes;
36  int planes;
37  int inverse;
38  int inplace;
39  int half, depth, offset, max;
41 
42  void (*premultiply[4])(const uint8_t *msrc, const uint8_t *asrc,
43  uint8_t *dst,
44  ptrdiff_t mlinesize, ptrdiff_t alinesize,
45  ptrdiff_t dlinesize,
46  int w, int h,
47  int half, int shift, int offset);
49 
50 #define OFFSET(x) offsetof(PreMultiplyContext, x)
51 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
52 
53 static const AVOption options[] = {
54  { "planes", "set planes", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
55  { "inplace","enable inplace mode", OFFSET(inplace), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
56  { NULL }
57 };
58 
59 #define premultiply_options options
60 AVFILTER_DEFINE_CLASS(premultiply);
61 
63 {
64  PreMultiplyContext *s = ctx->priv;
65 
66  static const enum AVPixelFormat no_alpha_pix_fmts[] = {
75  };
76 
77  static const enum AVPixelFormat alpha_pix_fmts[] = {
83  };
84 
85  return ff_set_common_formats(ctx, ff_make_format_list(s->inplace ? alpha_pix_fmts : no_alpha_pix_fmts));
86 }
87 
88 static void premultiply8(const uint8_t *msrc, const uint8_t *asrc,
89  uint8_t *dst,
90  ptrdiff_t mlinesize, ptrdiff_t alinesize,
91  ptrdiff_t dlinesize,
92  int w, int h,
93  int half, int shift, int offset)
94 {
95  int x, y;
96 
97  for (y = 0; y < h; y++) {
98  for (x = 0; x < w; x++) {
99  dst[x] = ((msrc[x] * (((asrc[x] >> 1) & 1) + asrc[x])) + 128) >> 8;
100  }
101 
102  dst += dlinesize;
103  msrc += mlinesize;
104  asrc += alinesize;
105  }
106 }
107 
108 static void premultiply8yuv(const uint8_t *msrc, const uint8_t *asrc,
109  uint8_t *dst,
110  ptrdiff_t mlinesize, ptrdiff_t alinesize,
111  ptrdiff_t dlinesize,
112  int w, int h,
113  int half, int shift, int offset)
114 {
115  int x, y;
116 
117  for (y = 0; y < h; y++) {
118  for (x = 0; x < w; x++) {
119  dst[x] = ((((msrc[x] - 128) * (((asrc[x] >> 1) & 1) + asrc[x]))) >> 8) + 128;
120  }
121 
122  dst += dlinesize;
123  msrc += mlinesize;
124  asrc += alinesize;
125  }
126 }
127 
128 static void premultiply8offset(const uint8_t *msrc, const uint8_t *asrc,
129  uint8_t *dst,
130  ptrdiff_t mlinesize, ptrdiff_t alinesize,
131  ptrdiff_t dlinesize,
132  int w, int h,
133  int half, int shift, int offset)
134 {
135  int x, y;
136 
137  for (y = 0; y < h; y++) {
138  for (x = 0; x < w; x++) {
139  dst[x] = ((((msrc[x] - offset) * (((asrc[x] >> 1) & 1) + asrc[x])) + 128) >> 8) + offset;
140  }
141 
142  dst += dlinesize;
143  msrc += mlinesize;
144  asrc += alinesize;
145  }
146 }
147 
148 static void premultiply16(const uint8_t *mmsrc, const uint8_t *aasrc,
149  uint8_t *ddst,
150  ptrdiff_t mlinesize, ptrdiff_t alinesize,
151  ptrdiff_t dlinesize,
152  int w, int h,
153  int half, int shift, int offset)
154 {
155  const uint16_t *msrc = (const uint16_t *)mmsrc;
156  const uint16_t *asrc = (const uint16_t *)aasrc;
157  uint16_t *dst = (uint16_t *)ddst;
158  int x, y;
159 
160  for (y = 0; y < h; y++) {
161  for (x = 0; x < w; x++) {
162  dst[x] = ((msrc[x] * (((asrc[x] >> 1) & 1) + asrc[x])) + half) >> shift;
163  }
164 
165  dst += dlinesize / 2;
166  msrc += mlinesize / 2;
167  asrc += alinesize / 2;
168  }
169 }
170 
171 static void premultiply16yuv(const uint8_t *mmsrc, const uint8_t *aasrc,
172  uint8_t *ddst,
173  ptrdiff_t mlinesize, ptrdiff_t alinesize,
174  ptrdiff_t dlinesize,
175  int w, int h,
176  int half, int shift, int offset)
177 {
178  const uint16_t *msrc = (const uint16_t *)mmsrc;
179  const uint16_t *asrc = (const uint16_t *)aasrc;
180  uint16_t *dst = (uint16_t *)ddst;
181  int x, y;
182 
183  for (y = 0; y < h; y++) {
184  for (x = 0; x < w; x++) {
185  dst[x] = ((((msrc[x] - half) * (((asrc[x] >> 1) & 1) + asrc[x]))) >> shift) + half;
186  }
187 
188  dst += dlinesize / 2;
189  msrc += mlinesize / 2;
190  asrc += alinesize / 2;
191  }
192 }
193 
194 static void premultiply16offset(const uint8_t *mmsrc, const uint8_t *aasrc,
195  uint8_t *ddst,
196  ptrdiff_t mlinesize, ptrdiff_t alinesize,
197  ptrdiff_t dlinesize,
198  int w, int h,
199  int half, int shift, int offset)
200 {
201  const uint16_t *msrc = (const uint16_t *)mmsrc;
202  const uint16_t *asrc = (const uint16_t *)aasrc;
203  uint16_t *dst = (uint16_t *)ddst;
204  int x, y;
205 
206  for (y = 0; y < h; y++) {
207  for (x = 0; x < w; x++) {
208  dst[x] = ((((msrc[x] - offset) * (((asrc[x] >> 1) & 1) + asrc[x])) + half) >> shift) + offset;
209  }
210 
211  dst += dlinesize / 2;
212  msrc += mlinesize / 2;
213  asrc += alinesize / 2;
214  }
215 }
216 
217 static void unpremultiply8(const uint8_t *msrc, const uint8_t *asrc,
218  uint8_t *dst,
219  ptrdiff_t mlinesize, ptrdiff_t alinesize,
220  ptrdiff_t dlinesize,
221  int w, int h,
222  int half, int max, int offset)
223 {
224  int x, y;
225 
226  for (y = 0; y < h; y++) {
227  for (x = 0; x < w; x++) {
228  if (asrc[x] > 0 && asrc[x] < 255)
229  dst[x] = FFMIN(msrc[x] * 255 / asrc[x], 255);
230  else
231  dst[x] = msrc[x];
232  }
233 
234  dst += dlinesize;
235  msrc += mlinesize;
236  asrc += alinesize;
237  }
238 }
239 
240 static void unpremultiply8yuv(const uint8_t *msrc, const uint8_t *asrc,
241  uint8_t *dst,
242  ptrdiff_t mlinesize, ptrdiff_t alinesize,
243  ptrdiff_t dlinesize,
244  int w, int h,
245  int half, int max, int offset)
246 {
247  int x, y;
248 
249  for (y = 0; y < h; y++) {
250  for (x = 0; x < w; x++) {
251  if (asrc[x] > 0 && asrc[x] < 255)
252  dst[x] = FFMIN((msrc[x] - 128) * 255 / asrc[x] + 128, 255);
253  else
254  dst[x] = msrc[x];
255  }
256 
257  dst += dlinesize;
258  msrc += mlinesize;
259  asrc += alinesize;
260  }
261 }
262 
263 static void unpremultiply8offset(const uint8_t *msrc, const uint8_t *asrc,
264  uint8_t *dst,
265  ptrdiff_t mlinesize, ptrdiff_t alinesize,
266  ptrdiff_t dlinesize,
267  int w, int h,
268  int half, int max, int offset)
269 {
270  int x, y;
271 
272  for (y = 0; y < h; y++) {
273  for (x = 0; x < w; x++) {
274  if (asrc[x] > 0 && asrc[x] < 255)
275  dst[x] = FFMIN((msrc[x] - offset) * 255 / asrc[x] + offset, 255);
276  else
277  dst[x] = msrc[x];
278  }
279 
280  dst += dlinesize;
281  msrc += mlinesize;
282  asrc += alinesize;
283  }
284 }
285 
286 static void unpremultiply16(const uint8_t *mmsrc, const uint8_t *aasrc,
287  uint8_t *ddst,
288  ptrdiff_t mlinesize, ptrdiff_t alinesize,
289  ptrdiff_t dlinesize,
290  int w, int h,
291  int half, int max, int offset)
292 {
293  const uint16_t *msrc = (const uint16_t *)mmsrc;
294  const uint16_t *asrc = (const uint16_t *)aasrc;
295  uint16_t *dst = (uint16_t *)ddst;
296  int x, y;
297 
298  for (y = 0; y < h; y++) {
299  for (x = 0; x < w; x++) {
300  if (asrc[x] > 0 && asrc[x] < max)
301  dst[x] = FFMIN(msrc[x] * (unsigned)max / asrc[x], max);
302  else
303  dst[x] = msrc[x];
304  }
305 
306  dst += dlinesize / 2;
307  msrc += mlinesize / 2;
308  asrc += alinesize / 2;
309  }
310 }
311 
312 static void unpremultiply16yuv(const uint8_t *mmsrc, const uint8_t *aasrc,
313  uint8_t *ddst,
314  ptrdiff_t mlinesize, ptrdiff_t alinesize,
315  ptrdiff_t dlinesize,
316  int w, int h,
317  int half, int max, int offset)
318 {
319  const uint16_t *msrc = (const uint16_t *)mmsrc;
320  const uint16_t *asrc = (const uint16_t *)aasrc;
321  uint16_t *dst = (uint16_t *)ddst;
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] < max)
327  dst[x] = FFMAX(FFMIN((msrc[x] - half) * max / asrc[x], half - 1), -half) + half;
328  else
329  dst[x] = msrc[x];
330  }
331 
332  dst += dlinesize / 2;
333  msrc += mlinesize / 2;
334  asrc += alinesize / 2;
335  }
336 }
337 
338 static void unpremultiply16offset(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] = FFMAX(FFMIN((msrc[x] - offset) * (unsigned)max / asrc[x] + offset, max), 0);
354  else
355  dst[x] = msrc[x];
356  }
357 
358  dst += dlinesize / 2;
359  msrc += mlinesize / 2;
360  asrc += alinesize / 2;
361  }
362 }
363 
365  AVFrame **out, AVFrame *base, AVFrame *alpha)
366 {
367  PreMultiplyContext *s = ctx->priv;
368  AVFilterLink *outlink = ctx->outputs[0];
369 
370  if (ctx->is_disabled) {
371  *out = av_frame_clone(base);
372  if (!*out)
373  return AVERROR(ENOMEM);
374  } else {
375  int p, full, limited;
376 
377  *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
378  if (!*out)
379  return AVERROR(ENOMEM);
380  av_frame_copy_props(*out, base);
381 
382  full = base->color_range == AVCOL_RANGE_JPEG;
383  limited = base->color_range == AVCOL_RANGE_MPEG;
384 
385  if (s->inverse) {
386  switch (outlink->format) {
387  case AV_PIX_FMT_YUV444P:
388  case AV_PIX_FMT_YUVA444P:
390  s->premultiply[1] = s->premultiply[2] = unpremultiply8yuv;
391  break;
392  case AV_PIX_FMT_YUVJ444P:
393  s->premultiply[0] = unpremultiply8;
394  s->premultiply[1] = s->premultiply[2] = unpremultiply8yuv;
395  break;
396  case AV_PIX_FMT_GBRP:
397  case AV_PIX_FMT_GBRAP:
398  s->premultiply[0] = s->premultiply[1] = s->premultiply[2] = limited ? unpremultiply8offset : unpremultiply8;
399  break;
400  case AV_PIX_FMT_YUV444P9:
410  break;
411  case AV_PIX_FMT_GBRP9:
412  case AV_PIX_FMT_GBRP10:
413  case AV_PIX_FMT_GBRAP10:
414  case AV_PIX_FMT_GBRP12:
415  case AV_PIX_FMT_GBRAP12:
416  case AV_PIX_FMT_GBRP14:
417  case AV_PIX_FMT_GBRP16:
418  case AV_PIX_FMT_GBRAP16:
419  s->premultiply[0] = s->premultiply[1] = s->premultiply[2] = limited ? unpremultiply16offset : unpremultiply16;
420  break;
421  case AV_PIX_FMT_GRAY8:
422  s->premultiply[0] = limited ? unpremultiply8offset : unpremultiply8;
423  break;
424  case AV_PIX_FMT_GRAY9:
425  case AV_PIX_FMT_GRAY10:
426  case AV_PIX_FMT_GRAY12:
427  case AV_PIX_FMT_GRAY16:
429  break;
430  }
431  } else {
432  switch (outlink->format) {
433  case AV_PIX_FMT_YUV444P:
434  case AV_PIX_FMT_YUVA444P:
436  s->premultiply[1] = s->premultiply[2] = premultiply8yuv;
437  break;
438  case AV_PIX_FMT_YUVJ444P:
439  s->premultiply[0] = premultiply8;
440  s->premultiply[1] = s->premultiply[2] = premultiply8yuv;
441  break;
442  case AV_PIX_FMT_GBRP:
443  case AV_PIX_FMT_GBRAP:
444  s->premultiply[0] = s->premultiply[1] = s->premultiply[2] = limited ? premultiply8offset : premultiply8;
445  break;
446  case AV_PIX_FMT_YUV444P9:
455  s->premultiply[1] = s->premultiply[2] = premultiply16yuv;
456  break;
457  case AV_PIX_FMT_GBRP9:
458  case AV_PIX_FMT_GBRP10:
459  case AV_PIX_FMT_GBRAP10:
460  case AV_PIX_FMT_GBRP12:
461  case AV_PIX_FMT_GBRAP12:
462  case AV_PIX_FMT_GBRP14:
463  case AV_PIX_FMT_GBRP16:
464  case AV_PIX_FMT_GBRAP16:
465  s->premultiply[0] = s->premultiply[1] = s->premultiply[2] = limited ? premultiply16offset : premultiply16;
466  break;
467  case AV_PIX_FMT_GRAY8:
468  s->premultiply[0] = limited ? premultiply8offset : premultiply8;
469  break;
470  case AV_PIX_FMT_GRAY9:
471  case AV_PIX_FMT_GRAY10:
472  case AV_PIX_FMT_GRAY12:
473  case AV_PIX_FMT_GRAY16:
474  s->premultiply[0] = limited ? premultiply16offset : premultiply16;
475  break;
476  }
477  }
478 
479  for (p = 0; p < s->nb_planes; p++) {
480  if (!((1 << p) & s->planes) || p == 3) {
481  av_image_copy_plane((*out)->data[p], (*out)->linesize[p], base->data[p], base->linesize[p],
482  s->linesize[p], s->height[p]);
483  continue;
484  }
485 
486  s->premultiply[p](base->data[p], s->inplace ? alpha->data[3] : alpha->data[0],
487  (*out)->data[p],
488  base->linesize[p], s->inplace ? alpha->linesize[3] : alpha->linesize[0],
489  (*out)->linesize[p],
490  s->width[p], s->height[p],
491  s->half, s->inverse ? s->max : s->depth, s->offset);
492  }
493  }
494 
495  return 0;
496 }
497 
498 static int process_frame(FFFrameSync *fs)
499 {
500  AVFilterContext *ctx = fs->parent;
501  PreMultiplyContext *s = fs->opaque;
502  AVFilterLink *outlink = ctx->outputs[0];
503  AVFrame *out = NULL, *base, *alpha;
504  int ret;
505 
506  if ((ret = ff_framesync_get_frame(&s->fs, 0, &base, 0)) < 0 ||
507  (ret = ff_framesync_get_frame(&s->fs, 1, &alpha, 0)) < 0)
508  return ret;
509 
510  if ((ret = filter_frame(ctx, &out, base, alpha)) < 0)
511  return ret;
512 
513  out->pts = av_rescale_q(base->pts, s->fs.time_base, outlink->time_base);
514 
515  return ff_filter_frame(outlink, out);
516 }
517 
518 static int config_input(AVFilterLink *inlink)
519 {
520  AVFilterContext *ctx = inlink->dst;
521  PreMultiplyContext *s = ctx->priv;
523  int vsub, hsub, ret;
524 
526 
527  if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
528  return ret;
529 
530  hsub = desc->log2_chroma_w;
531  vsub = desc->log2_chroma_h;
532  s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
533  s->height[0] = s->height[3] = inlink->h;
534  s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
535  s->width[0] = s->width[3] = inlink->w;
536 
537  s->depth = desc->comp[0].depth;
538  s->max = (1 << s->depth) - 1;
539  s->half = (1 << s->depth) / 2;
540  s->offset = 16 << (s->depth - 8);
541 
542  return 0;
543 }
544 
545 static int config_output(AVFilterLink *outlink)
546 {
547  AVFilterContext *ctx = outlink->src;
548  PreMultiplyContext *s = ctx->priv;
549  AVFilterLink *base = ctx->inputs[0];
551  FFFrameSyncIn *in;
552  int ret;
553 
554  if (!s->inplace) {
555  alpha = ctx->inputs[1];
556 
557  if (base->format != alpha->format) {
558  av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
559  return AVERROR(EINVAL);
560  }
561  if (base->w != alpha->w ||
562  base->h != alpha->h) {
563  av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
564  "(size %dx%d) do not match the corresponding "
565  "second input link %s parameters (%dx%d) ",
566  ctx->input_pads[0].name, base->w, base->h,
567  ctx->input_pads[1].name, alpha->w, alpha->h);
568  return AVERROR(EINVAL);
569  }
570  }
571 
572  outlink->w = base->w;
573  outlink->h = base->h;
574  outlink->time_base = base->time_base;
575  outlink->sample_aspect_ratio = base->sample_aspect_ratio;
576  outlink->frame_rate = base->frame_rate;
577 
578  if (s->inplace)
579  return 0;
580 
581  if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0)
582  return ret;
583 
584  in = s->fs.in;
585  in[0].time_base = base->time_base;
586  in[1].time_base = alpha->time_base;
587  in[0].sync = 1;
588  in[0].before = EXT_STOP;
589  in[0].after = EXT_INFINITY;
590  in[1].sync = 1;
591  in[1].before = EXT_STOP;
592  in[1].after = EXT_INFINITY;
593  s->fs.opaque = s;
595 
596  return ff_framesync_configure(&s->fs);
597 }
598 
600 {
601  PreMultiplyContext *s = ctx->priv;
602 
603  if (s->inplace) {
604  AVFrame *frame = NULL;
605  AVFrame *out = NULL;
606  int ret, status;
607  int64_t pts;
608 
609  if ((ret = ff_inlink_consume_frame(ctx->inputs[0], &frame)) > 0) {
610  if ((ret = filter_frame(ctx, &out, frame, frame)) < 0)
611  return ret;
612  av_frame_free(&frame);
613  ret = ff_filter_frame(ctx->outputs[0], out);
614  }
615  if (ret < 0) {
616  return ret;
617  } else if (ff_inlink_acknowledge_status(ctx->inputs[0], &status, &pts)) {
618  ff_outlink_set_status(ctx->outputs[0], status, pts);
619  return 0;
620  } else {
621  if (ff_outlink_frame_wanted(ctx->outputs[0]))
623  return 0;
624  }
625  } else {
626  return ff_framesync_activate(&s->fs);
627  }
628 }
629 
631 {
632  PreMultiplyContext *s = ctx->priv;
633  AVFilterPad pad = { 0 };
634  int ret;
635 
636  if (!strcmp(ctx->filter->name, "unpremultiply"))
637  s->inverse = 1;
638 
639  pad.type = AVMEDIA_TYPE_VIDEO;
640  pad.name = av_strdup("main");
642  if (!pad.name)
643  return AVERROR(ENOMEM);
644 
645  if ((ret = ff_insert_inpad(ctx, 0, &pad)) < 0) {
646  av_freep(&pad.name);
647  return ret;
648  }
649 
650  if (!s->inplace) {
651  pad.type = AVMEDIA_TYPE_VIDEO;
652  pad.name = av_strdup("alpha");
653  pad.config_props = NULL;
654  if (!pad.name)
655  return AVERROR(ENOMEM);
656 
657  if ((ret = ff_insert_inpad(ctx, 1, &pad)) < 0) {
658  av_freep(&pad.name);
659  return ret;
660  }
661  }
662 
663  return 0;
664 }
665 
667 {
668  PreMultiplyContext *s = ctx->priv;
669 
670  if (!s->inplace)
671  ff_framesync_uninit(&s->fs);
672 }
673 
675  {
676  .name = "default",
677  .type = AVMEDIA_TYPE_VIDEO,
678  .config_props = config_output,
679  },
680  { NULL }
681 };
682 
683 #if CONFIG_PREMULTIPLY_FILTER
684 
685 AVFilter ff_vf_premultiply = {
686  .name = "premultiply",
687  .description = NULL_IF_CONFIG_SMALL("PreMultiply first stream with first plane of second stream."),
688  .priv_size = sizeof(PreMultiplyContext),
689  .init = init,
690  .uninit = uninit,
692  .activate = activate,
693  .inputs = NULL,
694  .outputs = premultiply_outputs,
695  .priv_class = &premultiply_class,
698 };
699 
700 #endif /* CONFIG_PREMULTIPLY_FILTER */
701 
702 #if CONFIG_UNPREMULTIPLY_FILTER
703 
704 #define unpremultiply_options options
705 AVFILTER_DEFINE_CLASS(unpremultiply);
706 
707 AVFilter ff_vf_unpremultiply = {
708  .name = "unpremultiply",
709  .description = NULL_IF_CONFIG_SMALL("UnPreMultiply first stream with first plane of second stream."),
710  .priv_size = sizeof(PreMultiplyContext),
711  .init = init,
712  .uninit = uninit,
714  .activate = activate,
715  .inputs = NULL,
716  .outputs = premultiply_outputs,
717  .priv_class = &unpremultiply_class,
720 };
721 
722 #endif /* CONFIG_UNPREMULTIPLY_FILTER */
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
Definition: avfilter.c:1542
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
static int shift(int a, int b)
Definition: sonic.c:82
static float alpha(float a)
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2419
static int activate(AVFilterContext *ctx)
This structure describes decoded (raw) audio or video data.
Definition: frame.h:201
AVOption.
Definition: opt.h:246
#define AV_PIX_FMT_YUV444P14
Definition: pixfmt.h:389
#define AV_PIX_FMT_GBRAP10
Definition: pixfmt.h:399
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:67
misc image utilities
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2459
Main libavfilter public API header.
const char * desc
Definition: nvenc.c:60
int(* on_event)(struct FFFrameSync *fs)
Callback called when a frame event is ready.
Definition: framesync.h:172
#define AVFILTER_FLAG_DYNAMIC_INPUTS
The number of the filter inputs is not determined just by AVFilter.inputs.
Definition: avfilter.h:105
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:180
static const AVOption options[]
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:395
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:360
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:117
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:92
int is_disabled
the enabled state from the last expression evaluation
Definition: avfilter.h:385
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:169
AVFILTER_DEFINE_CLASS(premultiply)
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Definition: avfilter.c:1663
static int ff_outlink_frame_wanted(AVFilterLink *link)
Test if a frame is wanted on an output link.
Definition: filters.h:152
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:283
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:361
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:362
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1151
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
uint8_t
#define av_cold
Definition: attributes.h:82
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:294
FFFrameSyncIn * in
Pointer to array of inputs.
Definition: framesync.h:203
#define AV_PIX_FMT_GBRP9
Definition: pixfmt.h:394
static void unpremultiply16offset(const uint8_t *mmsrc, const uint8_t *aasrc, uint8_t *ddst, ptrdiff_t mlinesize, ptrdiff_t alinesize, ptrdiff_t dlinesize, int w, int h, int half, int max, int offset)
static AVFrame * frame
#define FLAGS
static int flags
Definition: log.c:57
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:392
enum FFFrameSyncExtMode after
Extrapolation mode for timestamps after the last frame.
Definition: framesync.h:91
Input stream structure.
Definition: framesync.h:81
#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:1507
AVFilterPad * input_pads
array of input pads
Definition: avfilter.h:345
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
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:568
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:293
Frame sync structure.
Definition: framesync.h:146
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:163
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
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:353
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: frame.h:446
#define AV_PIX_FMT_YUVA444P16
Definition: pixfmt.h:419
#define AV_PIX_FMT_GBRAP12
Definition: pixfmt.h:400
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's input and try to produce output.
Definition: framesync.c:344
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:382
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
#define FFMAX(a, b)
Definition: common.h:94
#define AV_PIX_FMT_GBRAP16
Definition: pixfmt.h:401
#define AV_PIX_FMT_GBRP16
Definition: pixfmt.h:398
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:363
#define FFMIN(a, b)
Definition: common.h:96
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)
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:416
static av_cold void uninit(AVFilterContext *ctx)
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:389
void * opaque
Opaque pointer, not used by the API.
Definition: framesync.h:177
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:378
#define AV_PIX_FMT_GBRP14
Definition: pixfmt.h:397
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:492
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)
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:510
static void unpremultiply16(const uint8_t *mmsrc, const uint8_t *aasrc, uint8_t *ddst, ptrdiff_t mlinesize, ptrdiff_t alinesize, ptrdiff_t dlinesize, int w, int h, int half, int max, int offset)
static const AVFilterPad inputs[]
Definition: af_afftfilt.c:379
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:237
Extend the frame to infinity.
Definition: framesync.h:75
static int process_frame(FFFrameSync *fs)
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:232
int ff_framesync_init(FFFrameSync *fs, AVFilterContext *parent, unsigned nb_in)
Initialize a frame sync structure.
Definition: framesync.c:77
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:189
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
static int query_formats(AVFilterContext *ctx)
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:144
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)
static int config_output(AVFilterLink *outlink)
const char * name
Filter name.
Definition: avfilter.h:148
#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:133
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
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:396
static int64_t pts
Global timestamp for the audio frames.
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:386
static int config_input(AVFilterLink *inlink)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:215
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:509
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)
Y , 8bpp.
Definition: pixfmt.h:70
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:229
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 filter_frame(AVFilterContext *ctx, AVFrame **out, AVFrame *base, AVFrame *alpha)
#define AV_PIX_FMT_YUVA444P9
Definition: pixfmt.h:413
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:76
Completely stop all streams with this one.
Definition: framesync.h:65
An instance of a filter.
Definition: avfilter.h:338
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)
FILE * out
Definition: movenc.c:54
#define av_freep(p)
int(* config_props)(AVFilterLink *link)
Link configuration callback.
Definition: internal.h:129
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:337
internal API functions
int ff_framesync_get_frame(FFFrameSync *fs, unsigned in, AVFrame **rframe, unsigned get)
Get the current frame in an input.
Definition: framesync.c:256
int depth
Number of bits in the component.
Definition: pixdesc.h:58
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
const AVFilter * filter
the AVFilter of which this is an instance
Definition: avfilter.h:341
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:603
static av_cold int init(AVFilterContext *ctx)
static enum AVPixelFormat alpha_pix_fmts[]
Definition: vf_overlay.c:202
#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:277