FFmpeg  4.3.9
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/opt.h"
30 #include "libavutil/pixfmt.h"
31 #include "avfilter.h"
32 #include "drawutils.h"
33 #include "formats.h"
34 #include "filters.h"
35 #include "internal.h"
36 #include "video.h"
37 
38 enum { Y, U, V, A };
39 
40 typedef struct AlphaMergeContext {
41  const AVClass *class;
42 
48 
50 {
51  static const enum AVPixelFormat main_fmts[] = {
56  };
57  static const enum AVPixelFormat alpha_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
58  AVFilterFormats *main_formats = ff_make_format_list(main_fmts);
59  int ret;
60 
61  if ((ret = ff_formats_ref(main_formats, &ctx->inputs[0]->out_formats)) < 0 ||
62  (ret = ff_formats_ref(main_formats, &ctx->outputs[0]->in_formats)) < 0)
63  return ret;
64 
65  return ff_formats_ref(ff_make_format_list(alpha_fmts),
66  &ctx->inputs[1]->out_formats);
67 }
68 
69 static int config_input_main(AVFilterLink *inlink)
70 {
71  AlphaMergeContext *s = inlink->dst->priv;
72  s->is_packed_rgb =
73  ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0 &&
74  inlink->format != AV_PIX_FMT_GBRAP;
75  return 0;
76 }
77 
78 static int config_output(AVFilterLink *outlink)
79 {
80  AVFilterContext *ctx = outlink->src;
81  AVFilterLink *mainlink = ctx->inputs[0];
82  AVFilterLink *alphalink = ctx->inputs[1];
83  if (mainlink->w != alphalink->w || mainlink->h != alphalink->h) {
84  av_log(ctx, AV_LOG_ERROR,
85  "Input frame sizes do not match (%dx%d vs %dx%d).\n",
86  mainlink->w, mainlink->h,
87  alphalink->w, alphalink->h);
88  return AVERROR(EINVAL);
89  }
90 
91  outlink->w = mainlink->w;
92  outlink->h = mainlink->h;
93  outlink->time_base = mainlink->time_base;
94  outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
95  outlink->frame_rate = mainlink->frame_rate;
96  return 0;
97 }
98 
100  AVFrame *main_buf,
101  AVFrame *alpha_buf)
102 {
103  AlphaMergeContext *s = ctx->priv;
104  int h = main_buf->height;
105 
106  if (s->is_packed_rgb) {
107  int x, y;
108  uint8_t *pin, *pout;
109  for (y = 0; y < h; y++) {
110  pin = alpha_buf->data[0] + y * alpha_buf->linesize[0];
111  pout = main_buf->data[0] + y * main_buf->linesize[0] + s->rgba_map[A];
112  for (x = 0; x < main_buf->width; x++) {
113  *pout = *pin;
114  pin += 1;
115  pout += 4;
116  }
117  }
118  } else {
119  const int main_linesize = main_buf->linesize[A];
120  const int alpha_linesize = alpha_buf->linesize[Y];
121  av_image_copy_plane(main_buf->data[A], main_linesize,
122  alpha_buf->data[Y], alpha_linesize,
123  FFMIN(main_linesize, alpha_linesize), alpha_buf->height);
124  }
125 }
126 
128 {
129  AVFilterLink *outlink = ctx->outputs[0];
130  AlphaMergeContext *s = ctx->priv;
131  int ret;
132 
133  FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
134 
135  if (!s->main_frame) {
136  ret = ff_inlink_consume_frame(ctx->inputs[0], &s->main_frame);
137  if (ret < 0)
138  return ret;
139  }
140 
141  if (!s->alpha_frame) {
142  ret = ff_inlink_consume_frame(ctx->inputs[1], &s->alpha_frame);
143  if (ret < 0)
144  return ret;
145  }
146 
147  if (s->main_frame && s->alpha_frame) {
148  if (!ctx->is_disabled)
149  draw_frame(ctx, s->main_frame, s->alpha_frame);
150  ret = ff_filter_frame(outlink, s->main_frame);
152  s->main_frame = NULL;
153  return ret;
154  }
155 
156  FF_FILTER_FORWARD_STATUS(ctx->inputs[0], outlink);
157  FF_FILTER_FORWARD_STATUS(ctx->inputs[1], outlink);
158 
159  if (ff_outlink_frame_wanted(ctx->outputs[0]) &&
160  !ff_outlink_get_status(ctx->inputs[0]) &&
161  !s->main_frame) {
163  return 0;
164  }
165 
166  if (ff_outlink_frame_wanted(ctx->outputs[0]) &&
167  !ff_outlink_get_status(ctx->inputs[1]) &&
168  !s->alpha_frame) {
170  return 0;
171  }
172 
173  return FFERROR_NOT_READY;
174 }
175 
176 static const AVFilterPad alphamerge_inputs[] = {
177  {
178  .name = "main",
179  .type = AVMEDIA_TYPE_VIDEO,
180  .config_props = config_input_main,
181  .needs_writable = 1,
182  },{
183  .name = "alpha",
184  .type = AVMEDIA_TYPE_VIDEO,
185  },
186  { NULL }
187 };
188 
189 static const AVFilterPad alphamerge_outputs[] = {
190  {
191  .name = "default",
192  .type = AVMEDIA_TYPE_VIDEO,
193  .config_props = config_output,
194  },
195  { NULL }
196 };
197 
198 static const AVOption alphamerge_options[] = {
199  { NULL }
200 };
201 
202 AVFILTER_DEFINE_CLASS(alphamerge);
203 
205  .name = "alphamerge",
206  .description = NULL_IF_CONFIG_SMALL("Copy the luma value of the second "
207  "input into the alpha channel of the first input."),
208  .priv_size = sizeof(AlphaMergeContext),
209  .priv_class = &alphamerge_class,
211  .inputs = alphamerge_inputs,
212  .outputs = alphamerge_outputs,
213  .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:1476
#define NULL
Definition: coverity.c:32
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
AVOption.
Definition: opt.h:246
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[]
#define FFERROR_NOT_READY
Filters implementation helper functions.
Definition: filters.h:34
static int activate(AVFilterContext *ctx)
int is_disabled
the enabled state from the last expression evaluation
Definition: avfilter.h:385
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Definition: avfilter.c:1602
static int ff_outlink_frame_wanted(AVFilterLink *link)
Test if a frame is wanted on an output link.
Definition: filters.h:172
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:300
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:1075
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:101
AVFILTER_DEFINE_CLASS(alphamerge)
uint8_t
AVOptions.
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:94
AVFrame * main_frame
Definition: vf_alphamerge.c:45
#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:69
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:176
int width
Definition: frame.h:358
#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)
Definition: vf_alphamerge.c:99
#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:203
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:188
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 const AVOption alphamerge_options[]
static int query_formats(AVFilterContext *ctx)
Definition: vf_alphamerge.c:49
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:92
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:484
static const AVFilterPad alphamerge_outputs[]
AVFormatContext * ctx
Definition: movenc.c:48
#define s(width, name)
Definition: cbs_vp9.c:257
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
misc drawing utilities
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:331
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:177
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
int ff_outlink_get_status(AVFilterLink *link)
Get the status on an output link.
Definition: avfilter.c:1625
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
#define FF_FILTER_FORWARD_STATUS(inlink, outlink)
Acknowledge the status on an input link and forward it to an output link.
Definition: filters.h:226
#define flags(name, subs,...)
Definition: cbs_av1.c:576
static int config_output(AVFilterLink *outlink)
Definition: vf_alphamerge.c:78
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:314
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:46
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:358
uint8_t rgba_map[4]
Definition: vf_alphamerge.c:44
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:64