FFmpeg
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 "drawutils.h"
32 #include "formats.h"
33 #include "filters.h"
34 #include "internal.h"
35 #include "video.h"
36 
37 enum { Y, U, V, A };
38 
39 typedef struct AlphaMergeContext {
45 
47 {
48  static const enum AVPixelFormat main_fmts[] = {
53  };
54  static const enum AVPixelFormat alpha_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
55  AVFilterFormats *main_formats = NULL, *alpha_formats = NULL;
56  int ret;
57 
58  if (!(main_formats = ff_make_format_list(main_fmts)) ||
59  !(alpha_formats = ff_make_format_list(alpha_fmts))) {
60  ret = AVERROR(ENOMEM);
61  goto fail;
62  }
63  if ((ret = ff_formats_ref(main_formats , &ctx->inputs[0]->out_formats)) < 0 ||
64  (ret = ff_formats_ref(alpha_formats, &ctx->inputs[1]->out_formats)) < 0 ||
65  (ret = ff_formats_ref(main_formats , &ctx->outputs[0]->in_formats)) < 0)
66  goto fail;
67  return 0;
68 fail:
69  if (main_formats)
70  av_freep(&main_formats->formats);
71  av_freep(&main_formats);
72  if (alpha_formats)
73  av_freep(&alpha_formats->formats);
74  av_freep(&alpha_formats);
75  return ret;
76 }
77 
79 {
80  AlphaMergeContext *s = inlink->dst->priv;
81  s->is_packed_rgb =
82  ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0 &&
83  inlink->format != AV_PIX_FMT_GBRAP;
84  return 0;
85 }
86 
87 static int config_output(AVFilterLink *outlink)
88 {
89  AVFilterContext *ctx = outlink->src;
90  AVFilterLink *mainlink = ctx->inputs[0];
91  AVFilterLink *alphalink = ctx->inputs[1];
92  if (mainlink->w != alphalink->w || mainlink->h != alphalink->h) {
93  av_log(ctx, AV_LOG_ERROR,
94  "Input frame sizes do not match (%dx%d vs %dx%d).\n",
95  mainlink->w, mainlink->h,
96  alphalink->w, alphalink->h);
97  return AVERROR(EINVAL);
98  }
99 
100  outlink->w = mainlink->w;
101  outlink->h = mainlink->h;
102  outlink->time_base = mainlink->time_base;
103  outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
104  outlink->frame_rate = mainlink->frame_rate;
105  return 0;
106 }
107 
109  AVFrame *main_buf,
110  AVFrame *alpha_buf)
111 {
112  AlphaMergeContext *s = ctx->priv;
113  int h = main_buf->height;
114 
115  if (s->is_packed_rgb) {
116  int x, y;
117  uint8_t *pin, *pout;
118  for (y = 0; y < h; y++) {
119  pin = alpha_buf->data[0] + y * alpha_buf->linesize[0];
120  pout = main_buf->data[0] + y * main_buf->linesize[0] + s->rgba_map[A];
121  for (x = 0; x < main_buf->width; x++) {
122  *pout = *pin;
123  pin += 1;
124  pout += 4;
125  }
126  }
127  } else {
128  const int main_linesize = main_buf->linesize[A];
129  const int alpha_linesize = alpha_buf->linesize[Y];
130  av_image_copy_plane(main_buf->data[A], main_linesize,
131  alpha_buf->data[Y], alpha_linesize,
132  FFMIN(main_linesize, alpha_linesize), alpha_buf->height);
133  }
134 }
135 
137 {
138  AVFilterLink *outlink = ctx->outputs[0];
139  AlphaMergeContext *s = ctx->priv;
140  int ret;
141 
142  FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
143 
144  if (!s->main_frame) {
145  ret = ff_inlink_consume_frame(ctx->inputs[0], &s->main_frame);
146  if (ret < 0)
147  return ret;
148  }
149 
150  if (!s->alpha_frame) {
151  ret = ff_inlink_consume_frame(ctx->inputs[1], &s->alpha_frame);
152  if (ret < 0)
153  return ret;
154  }
155 
156  if (s->main_frame && s->alpha_frame) {
157  draw_frame(ctx, s->main_frame, s->alpha_frame);
158  ret = ff_filter_frame(outlink, s->main_frame);
160  s->main_frame = NULL;
161  return ret;
162  }
163 
164  FF_FILTER_FORWARD_STATUS(ctx->inputs[0], outlink);
165  FF_FILTER_FORWARD_STATUS(ctx->inputs[1], outlink);
166 
167  if (ff_outlink_frame_wanted(ctx->outputs[0]) &&
168  !ff_outlink_get_status(ctx->inputs[0]) &&
169  !s->main_frame) {
171  return 0;
172  }
173 
174  if (ff_outlink_frame_wanted(ctx->outputs[0]) &&
175  !ff_outlink_get_status(ctx->inputs[1]) &&
176  !s->alpha_frame) {
178  return 0;
179  }
180 
181  return FFERROR_NOT_READY;
182 }
183 
184 static const AVFilterPad alphamerge_inputs[] = {
185  {
186  .name = "main",
187  .type = AVMEDIA_TYPE_VIDEO,
188  .config_props = config_input_main,
189  .needs_writable = 1,
190  },{
191  .name = "alpha",
192  .type = AVMEDIA_TYPE_VIDEO,
193  },
194  { NULL }
195 };
196 
197 static const AVFilterPad alphamerge_outputs[] = {
198  {
199  .name = "default",
200  .type = AVMEDIA_TYPE_VIDEO,
201  .config_props = config_output,
202  },
203  { NULL }
204 };
205 
207  .name = "alphamerge",
208  .description = NULL_IF_CONFIG_SMALL("Copy the luma value of the second "
209  "input into the alpha channel of the first input."),
210  .priv_size = sizeof(AlphaMergeContext),
212  .inputs = alphamerge_inputs,
213  .outputs = alphamerge_outputs,
214  .activate = activate,
215 };
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:1494
#define NULL
Definition: coverity.c:32
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
misc image utilities
#define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter)
Forward the status on an output link to all input links.
Definition: filters.h:212
Main libavfilter public API header.
static const AVFilterPad alphamerge_inputs[]
FF_FILTER_FORWARD_STATUS(inlink, outlink)
static int activate(AVFilterContext *ctx)
return FFERROR_NOT_READY
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Definition: avfilter.c:1620
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
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:1093
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:101
uint8_t
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:94
AVFrame * main_frame
Definition: vf_alphamerge.c:42
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
static int config_input_main(AVFilterLink *inlink)
Definition: vf_alphamerge.c:78
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:176
int width
Definition: frame.h:353
#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)
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:95
void * priv
private data for use by the filter
Definition: avfilter.h:353
static int query_formats(AVFilterContext *ctx)
Definition: vf_alphamerge.c:46
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:92
#define fail()
Definition: checkasm.h:122
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
#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:439
static const AVFilterPad alphamerge_outputs[]
AVFormatContext * ctx
Definition: movenc.c:48
#define s(width, name)
Definition: cbs_vp9.c:257
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
misc drawing utilities
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:326
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:177
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
Filter definition.
Definition: avfilter.h:144
int ff_outlink_get_status(AVFilterLink *link)
Get the status on an output link.
Definition: avfilter.c:1643
const char * name
Filter name.
Definition: avfilter.h:148
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
static int config_output(AVFilterLink *outlink)
Definition: vf_alphamerge.c:87
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:309
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Y , 8bpp.
Definition: pixfmt.h:74
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:215
AVFilter ff_vf_alphamerge
AVFrame * alpha_frame
Definition: vf_alphamerge.c:43
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:353
#define av_freep(p)
uint8_t rgba_map[4]
Definition: vf_alphamerge.c:41
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
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
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
int * formats
list of media formats
Definition: formats.h:66