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/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 "framesync.h"
36 #include "video.h"
37 
38 enum { Y, U, V, A };
39 
40 typedef struct AlphaMergeContext {
41  const AVClass *class;
42 
44  uint8_t rgba_map[4];
45 
48 
50 {
51  AVFilterContext *ctx = fs->parent;
52  AlphaMergeContext *s = ctx->priv;
53  AVFrame *main_buf, *alpha_buf;
54  int ret;
55 
56  ret = ff_framesync_dualinput_get_writable(fs, &main_buf, &alpha_buf);
57  if (ret < 0)
58  return ret;
59  if (!alpha_buf)
60  return ff_filter_frame(ctx->outputs[0], main_buf);
61 
62  if (alpha_buf->color_range == AVCOL_RANGE_MPEG) {
63  av_log(ctx, AV_LOG_WARNING, "alpha plane color range tagged as %s, "
64  "output will be wrong!\n",
65  av_color_range_name(alpha_buf->color_range));
66  }
67 
68  if (s->is_packed_rgb) {
69  int x, y;
70  uint8_t *pin, *pout;
71  for (y = 0; y < main_buf->height; y++) {
72  pin = alpha_buf->data[0] + y * alpha_buf->linesize[0];
73  pout = main_buf->data[0] + y * main_buf->linesize[0] + s->rgba_map[A];
74  for (x = 0; x < main_buf->width; x++) {
75  *pout = *pin;
76  pin += 1;
77  pout += 4;
78  }
79  }
80  } else {
81  const int main_linesize = main_buf->linesize[A];
82  const int alpha_linesize = alpha_buf->linesize[Y];
83  av_image_copy_plane(main_buf->data[A], main_linesize,
84  alpha_buf->data[Y], alpha_linesize,
85  FFMIN(main_linesize, alpha_linesize), alpha_buf->height);
86  }
87 
88  return ff_filter_frame(ctx->outputs[0], main_buf);
89 }
90 
92 {
93  AlphaMergeContext *s = ctx->priv;
94 
95  s->fs.on_event = do_alphamerge;
96  return 0;
97 }
98 
99 static int query_formats(const AVFilterContext *ctx,
100  AVFilterFormatsConfig **cfg_in,
101  AVFilterFormatsConfig **cfg_out)
102 {
103  static const enum AVPixelFormat main_fmts[] = {
108  };
109  static const enum AVPixelFormat alpha_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
110  int ret;
111 
112  ret = ff_formats_ref(ff_make_format_list(alpha_fmts),
113  &cfg_in[1]->formats);
114  if (ret < 0)
115  return ret;
116 
117  ret = ff_set_common_formats_from_list2(ctx, cfg_in, cfg_out, main_fmts);
118  if (ret < 0)
119  return ret;
120 
121  return 0;
122 }
123 
125 {
126  AlphaMergeContext *s = inlink->dst->priv;
127  s->is_packed_rgb =
128  ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0 &&
129  inlink->format != AV_PIX_FMT_GBRAP;
130  return 0;
131 }
132 
133 static int config_output(AVFilterLink *outlink)
134 {
135  FilterLink *outl = ff_filter_link(outlink);
136  AVFilterContext *ctx = outlink->src;
137  AlphaMergeContext *s = ctx->priv;
138  AVFilterLink *mainlink = ctx->inputs[0];
139  FilterLink *ml = ff_filter_link(mainlink);
140  AVFilterLink *alphalink = ctx->inputs[1];
141  int ret;
142 
143  if (mainlink->w != alphalink->w || mainlink->h != alphalink->h) {
145  "Input frame sizes do not match (%dx%d vs %dx%d).\n",
146  mainlink->w, mainlink->h,
147  alphalink->w, alphalink->h);
148  return AVERROR(EINVAL);
149  }
150 
151  if ((ret = ff_framesync_init_dualinput(&s->fs, ctx)) < 0)
152  return ret;
153 
154  outlink->w = mainlink->w;
155  outlink->h = mainlink->h;
156  outlink->time_base = mainlink->time_base;
157  outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
158  outl->frame_rate = ml->frame_rate;
159 
160  return ff_framesync_configure(&s->fs);
161 }
162 
164 {
165  AlphaMergeContext *s = ctx->priv;
166  return ff_framesync_activate(&s->fs);
167 }
168 
170 {
171  AlphaMergeContext *s = ctx->priv;
172 
173  ff_framesync_uninit(&s->fs);
174 }
175 
176 static const AVFilterPad alphamerge_inputs[] = {
177  {
178  .name = "main",
179  .type = AVMEDIA_TYPE_VIDEO,
180  .config_props = config_input_main,
181  },{
182  .name = "alpha",
183  .type = AVMEDIA_TYPE_VIDEO,
184  },
185 };
186 
187 static const AVFilterPad alphamerge_outputs[] = {
188  {
189  .name = "default",
190  .type = AVMEDIA_TYPE_VIDEO,
191  .config_props = config_output,
192  },
193 };
194 
195 static const AVOption alphamerge_options[] = {
196  { NULL }
197 };
198 
200 
202  .name = "alphamerge",
203  .description = NULL_IF_CONFIG_SMALL("Copy the luma value of the second "
204  "input into the alpha channel of the first input."),
205  .preinit = alphamerge_framesync_preinit,
206  .priv_size = sizeof(AlphaMergeContext),
207  .priv_class = &alphamerge_class,
208  .init = init,
212  .uninit = uninit,
213  .activate = activate,
215 };
formats
formats
Definition: signature.h:47
ff_framesync_configure
int ff_framesync_configure(FFFrameSync *fs)
Configure a frame sync structure.
Definition: framesync.c:137
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
AVFrame::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: frame.h:668
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
AVERROR
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
opt.h
AlphaMergeContext
Definition: vf_alphamerge.c:40
ff_make_format_list
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:435
ff_framesync_uninit
void ff_framesync_uninit(FFFrameSync *fs)
Free all memory currently allocated.
Definition: framesync.c:301
alphamerge_inputs
static const AVFilterPad alphamerge_inputs[]
Definition: vf_alphamerge.c:176
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1062
inlink
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
Definition: filter_design.txt:212
Y
@ Y
Definition: vf_alphamerge.c:38
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
AVFrame::width
int width
Definition: frame.h:461
alphamerge_outputs
static const AVFilterPad alphamerge_outputs[]
Definition: vf_alphamerge.c:187
AVOption
AVOption.
Definition: opt.h:429
config_output
static int config_output(AVFilterLink *outlink)
Definition: vf_alphamerge.c:133
AV_PIX_FMT_BGRA
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:102
AlphaMergeContext::fs
FFFrameSync fs
Definition: vf_alphamerge.c:46
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
FFFrameSync
Frame sync structure.
Definition: framesync.h:168
video.h
config_input_main
static int config_input_main(AVFilterLink *inlink)
Definition: vf_alphamerge.c:124
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:410
av_image_copy_plane
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:374
formats.h
AV_PIX_FMT_GBRAP
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:212
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_alphamerge.c:91
do_alphamerge
static int do_alphamerge(FFFrameSync *fs)
Definition: vf_alphamerge.c:49
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
av_cold
#define av_cold
Definition: attributes.h:90
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_PIX_FMT_YUVA420P
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:108
ff_formats_ref
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:678
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:49
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:100
av_color_range_name
const char * av_color_range_name(enum AVColorRange range)
Definition: pixdesc.c:3486
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
NULL
#define NULL
Definition: coverity.c:32
fs
#define fs(width, name, subs,...)
Definition: cbs_vp9.c:200
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_alphamerge.c:169
AlphaMergeContext::rgba_map
uint8_t rgba_map[4]
Definition: vf_alphamerge.c:44
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:81
AV_PIX_FMT_ABGR
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:101
AVFilterFormatsConfig
Lists of formats / etc.
Definition: avfilter.h:111
ff_filter_link
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition: filters.h:197
A
@ A
Definition: vf_alphamerge.c:38
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
ff_framesync_init_dualinput
int ff_framesync_init_dualinput(FFFrameSync *fs, AVFilterContext *parent)
Initialize a frame sync structure for dualinput.
Definition: framesync.c:372
FRAMESYNC_DEFINE_CLASS
FRAMESYNC_DEFINE_CLASS(alphamerge, AlphaMergeContext, fs)
AV_PIX_FMT_YUVA444P
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:174
AV_PIX_FMT_ARGB
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:99
FILTER_QUERY_FUNC2
#define FILTER_QUERY_FUNC2(func)
Definition: filters.h:239
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
V
@ V
Definition: vf_alphamerge.c:38
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:700
AVFilter
Filter definition.
Definition: avfilter.h:201
ret
ret
Definition: filter_design.txt:187
pixfmt.h
ff_vf_alphamerge
const AVFilter ff_vf_alphamerge
Definition: vf_alphamerge.c:201
AVFrame::height
int height
Definition: frame.h:461
ff_set_common_formats_from_list2
int ff_set_common_formats_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const int *fmts)
Definition: formats.c:1016
framesync.h
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
avfilter.h
alphamerge_options
static const AVOption alphamerge_options[]
Definition: vf_alphamerge.c:195
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
ff_fill_rgba_map
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
Definition: drawutils.c:79
AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
#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:190
imgutils.h
query_formats
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition: vf_alphamerge.c:99
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:434
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
ff_framesync_activate
int ff_framesync_activate(FFFrameSync *fs)
Examine the frames in the filter's input and try to produce output.
Definition: framesync.c:352
U
@ U
Definition: vf_alphamerge.c:38
ff_framesync_dualinput_get_writable
int ff_framesync_dualinput_get_writable(FFFrameSync *fs, AVFrame **f0, AVFrame **f1)
Same as ff_framesync_dualinput_get(), but make sure that f0 is writable.
Definition: framesync.c:410
drawutils.h
AV_PIX_FMT_YUVA422P
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:173
activate
static int activate(AVFilterContext *ctx)
Definition: vf_alphamerge.c:163
AlphaMergeContext::is_packed_rgb
int is_packed_rgb
Definition: vf_alphamerge.c:43