FFmpeg  4.0
vf_alphamerge.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Steven Robertson
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 /**
22  * @file
23  * copy an alpha component from another video's luma
24  */
25 
26 #include <string.h>
27 
28 #include "libavutil/imgutils.h"
29 #include "libavutil/pixfmt.h"
30 #include "avfilter.h"
31 #include "bufferqueue.h"
32 #include "drawutils.h"
33 #include "formats.h"
34 #include "internal.h"
35 #include "video.h"
36 
37 enum { Y, U, V, A };
38 
39 typedef struct AlphaMergeContext {
45 
47 {
51 }
52 
54 {
55  static const enum AVPixelFormat main_fmts[] = {
60  };
61  static const enum AVPixelFormat alpha_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
62  AVFilterFormats *main_formats = NULL, *alpha_formats = NULL;
63  int ret;
64 
65  if (!(main_formats = ff_make_format_list(main_fmts)) ||
66  !(alpha_formats = ff_make_format_list(alpha_fmts))) {
67  ret = AVERROR(ENOMEM);
68  goto fail;
69  }
70  if ((ret = ff_formats_ref(main_formats , &ctx->inputs[0]->out_formats)) < 0 ||
71  (ret = ff_formats_ref(alpha_formats, &ctx->inputs[1]->out_formats)) < 0 ||
72  (ret = ff_formats_ref(main_formats , &ctx->outputs[0]->in_formats)) < 0)
73  goto fail;
74  return 0;
75 fail:
76  if (main_formats)
77  av_freep(&main_formats->formats);
78  av_freep(&main_formats);
79  if (alpha_formats)
80  av_freep(&alpha_formats->formats);
81  av_freep(&alpha_formats);
82  return ret;
83 }
84 
85 static int config_input_main(AVFilterLink *inlink)
86 {
87  AlphaMergeContext *merge = inlink->dst->priv;
88  merge->is_packed_rgb =
89  ff_fill_rgba_map(merge->rgba_map, inlink->format) >= 0 &&
90  inlink->format != AV_PIX_FMT_GBRAP;
91  return 0;
92 }
93 
94 static int config_output(AVFilterLink *outlink)
95 {
96  AVFilterContext *ctx = outlink->src;
97  AVFilterLink *mainlink = ctx->inputs[0];
98  AVFilterLink *alphalink = ctx->inputs[1];
99  if (mainlink->w != alphalink->w || mainlink->h != alphalink->h) {
100  av_log(ctx, AV_LOG_ERROR,
101  "Input frame sizes do not match (%dx%d vs %dx%d).\n",
102  mainlink->w, mainlink->h,
103  alphalink->w, alphalink->h);
104  return AVERROR(EINVAL);
105  }
106 
107  outlink->w = mainlink->w;
108  outlink->h = mainlink->h;
109  outlink->time_base = mainlink->time_base;
110  outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
111  outlink->frame_rate = mainlink->frame_rate;
112  return 0;
113 }
114 
116  AVFrame *main_buf,
117  AVFrame *alpha_buf)
118 {
119  AlphaMergeContext *merge = ctx->priv;
120  int h = main_buf->height;
121 
122  if (merge->is_packed_rgb) {
123  int x, y;
124  uint8_t *pin, *pout;
125  for (y = 0; y < h; y++) {
126  pin = alpha_buf->data[0] + y * alpha_buf->linesize[0];
127  pout = main_buf->data[0] + y * main_buf->linesize[0] + merge->rgba_map[A];
128  for (x = 0; x < main_buf->width; x++) {
129  *pout = *pin;
130  pin += 1;
131  pout += 4;
132  }
133  }
134  } else {
135  const int main_linesize = main_buf->linesize[A];
136  const int alpha_linesize = alpha_buf->linesize[Y];
137  av_image_copy_plane(main_buf->data[A], main_linesize,
138  alpha_buf->data[Y], alpha_linesize,
139  FFMIN(main_linesize, alpha_linesize), alpha_buf->height);
140  }
141 }
142 
143 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
144 {
145  AVFilterContext *ctx = inlink->dst;
146  AlphaMergeContext *merge = ctx->priv;
147 
148  int ret = 0;
149  int is_alpha = (inlink == ctx->inputs[1]);
150  struct FFBufQueue *queue =
151  (is_alpha ? &merge->queue_alpha : &merge->queue_main);
152  ff_bufqueue_add(ctx, queue, buf);
153 
154  do {
155  AVFrame *main_buf, *alpha_buf;
156 
157  if (!ff_bufqueue_peek(&merge->queue_main, 0) ||
158  !ff_bufqueue_peek(&merge->queue_alpha, 0)) break;
159 
160  main_buf = ff_bufqueue_get(&merge->queue_main);
161  alpha_buf = ff_bufqueue_get(&merge->queue_alpha);
162 
163  draw_frame(ctx, main_buf, alpha_buf);
164  ret = ff_filter_frame(ctx->outputs[0], main_buf);
165  av_frame_free(&alpha_buf);
166  } while (ret >= 0);
167  return ret;
168 }
169 
170 static int request_frame(AVFilterLink *outlink)
171 {
172  AVFilterContext *ctx = outlink->src;
173  AlphaMergeContext *merge = ctx->priv;
174  int in, ret;
175 
176  in = ff_bufqueue_peek(&merge->queue_main, 0) ? 1 : 0;
177  ret = ff_request_frame(ctx->inputs[in]);
178  if (ret < 0)
179  return ret;
180  return 0;
181 }
182 
183 static const AVFilterPad alphamerge_inputs[] = {
184  {
185  .name = "main",
186  .type = AVMEDIA_TYPE_VIDEO,
187  .config_props = config_input_main,
188  .filter_frame = filter_frame,
189  .needs_writable = 1,
190  },{
191  .name = "alpha",
192  .type = AVMEDIA_TYPE_VIDEO,
193  .filter_frame = filter_frame,
194  },
195  { NULL }
196 };
197 
198 static const AVFilterPad alphamerge_outputs[] = {
199  {
200  .name = "default",
201  .type = AVMEDIA_TYPE_VIDEO,
202  .config_props = config_output,
203  .request_frame = request_frame,
204  },
205  { NULL }
206 };
207 
209  .name = "alphamerge",
210  .description = NULL_IF_CONFIG_SMALL("Copy the luma value of the second "
211  "input into the alpha channel of the first input."),
212  .uninit = uninit,
213  .priv_size = sizeof(AlphaMergeContext),
215  .inputs = alphamerge_inputs,
216  .outputs = alphamerge_outputs,
217 };
static AVFrame * ff_bufqueue_get(struct FFBufQueue *queue)
Get the first buffer from the queue and remove it.
Definition: bufferqueue.h:98
#define NULL
Definition: coverity.c:32
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
struct FFBufQueue queue_alpha
Definition: vf_alphamerge.c:43
misc image utilities
struct FFBufQueue queue_main
Definition: vf_alphamerge.c:42
Main libavfilter public API header.
static const AVFilterPad alphamerge_inputs[]
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
Structure holding the queue.
Definition: bufferqueue.h:49
const char * name
Pad name.
Definition: internal.h:60
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:1080
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:97
uint8_t
#define av_cold
Definition: attributes.h:82
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:90
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:54
static int config_input_main(AVFilterLink *inlink)
Definition: vf_alphamerge.c:85
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:172
int width
Definition: frame.h:276
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static void draw_frame(AVFilterContext *ctx, AVFrame *main_buf, AVFrame *alpha_buf)
#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:202
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:91
void * priv
private data for use by the filter
Definition: avfilter.h:353
static int query_formats(AVFilterContext *ctx)
Definition: vf_alphamerge.c:53
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:88
#define fail()
Definition: checkasm.h:116
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:89
AVFrame * queue[FF_BUFQUEUE_SIZE]
Definition: bufferqueue.h:50
#define FFMIN(a, b)
Definition: common.h:96
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:440
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_alphamerge.c:46
static const AVFilterPad alphamerge_outputs[]
AVFormatContext * ctx
Definition: movenc.c:48
static void ff_bufqueue_discard_all(struct FFBufQueue *queue)
Unref and remove all buffers from the queue.
Definition: bufferqueue.h:111
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
Definition: drawutils.c:35
static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
misc drawing utilities
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:249
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:173
void * buf
Definition: avisynth_c.h:690
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;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);return NULL;} return ac;} 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;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->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);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
Filter definition.
Definition: avfilter.h:144
const char * name
Filter name.
Definition: avfilter.h:148
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
static void merge(GetBitContext *gb, uint8_t *dst, uint8_t *src, int size)
Merge two consequent lists of equal size depending on bits read.
Definition: bink.c:217
static int config_output(AVFilterLink *outlink)
Definition: vf_alphamerge.c:94
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:232
Y , 8bpp.
Definition: pixfmt.h:70
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:211
AVFilter ff_vf_alphamerge
pixel format definitions
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:338
int height
Definition: frame.h:276
#define av_freep(p)
static int request_frame(AVFilterLink *outlink)
uint8_t rgba_map[4]
Definition: vf_alphamerge.c:41
static void ff_bufqueue_add(void *log, struct FFBufQueue *queue, AVFrame *buf)
Add a buffer to the queue.
Definition: bufferqueue.h:71
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:407
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:338
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
int * formats
list of media formats
Definition: formats.h:66
static AVFrame * ff_bufqueue_peek(struct FFBufQueue *queue, unsigned index)
Get a buffer from the queue without altering it.
Definition: bufferqueue.h:87