FFmpeg
Loading...
Searching...
No Matches
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
38enum { Y, U, V, A };
39
40typedef struct AlphaMergeContext {
41 const AVClass *class;
42
44 uint8_t rgba_map[4];
45
48
50{
51 AVFilterContext *ctx = fs->parent;
52 AVFilterLink *outlink = ctx->outputs[0];
53 AlphaMergeContext *s = ctx->priv;
54 AVFrame *main_buf, *alpha_buf;
55 int ret;
56
57 ret = ff_framesync_dualinput_get_writable(fs, &main_buf, &alpha_buf);
58 if (ret < 0)
59 return ret;
60 main_buf->alpha_mode = outlink->alpha_mode;
61 if (!alpha_buf)
62 return ff_filter_frame(ctx->outputs[0], main_buf);
63
64 if (alpha_buf->color_range == AVCOL_RANGE_MPEG) {
65 av_log(ctx, AV_LOG_WARNING, "alpha plane color range tagged as %s, "
66 "output will be wrong!\n",
68 }
69
70 if (s->is_packed_rgb) {
71 int x, y;
72 uint8_t *pin, *pout;
73 for (y = 0; y < main_buf->height; y++) {
74 pin = alpha_buf->data[0] + y * alpha_buf->linesize[0];
75 pout = main_buf->data[0] + y * main_buf->linesize[0] + s->rgba_map[A];
76 for (x = 0; x < main_buf->width; x++) {
77 *pout = *pin;
78 pin += 1;
79 pout += 4;
80 }
81 }
82 } else {
83 const int main_linesize = main_buf->linesize[A];
84 const int alpha_linesize = alpha_buf->linesize[Y];
85 av_image_copy_plane(main_buf->data[A], main_linesize,
86 alpha_buf->data[Y], alpha_linesize,
87 FFMIN(main_linesize, alpha_linesize), alpha_buf->height);
88 }
89
90 return ff_filter_frame(ctx->outputs[0], main_buf);
91}
92
94{
95 AlphaMergeContext *s = ctx->priv;
96
97 s->fs.on_event = do_alphamerge;
98 return 0;
99}
100
102 AVFilterFormatsConfig **cfg_in,
103 AVFilterFormatsConfig **cfg_out)
104{
105 static const enum AVPixelFormat main_fmts[] = {
110 };
111 static const enum AVPixelFormat alpha_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
112 int ret;
113
115 &cfg_in[1]->formats);
116 if (ret < 0)
117 return ret;
118
119 ret = ff_set_pixel_formats_from_list2(ctx, cfg_in, cfg_out, main_fmts);
120 if (ret < 0)
121 return ret;
122
124 &cfg_out[0]->alpha_modes);
125 if (ret < 0)
126 return ret;
127
128 return 0;
129}
130
132{
133 AlphaMergeContext *s = inlink->dst->priv;
134 s->is_packed_rgb =
135 ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0 &&
136 inlink->format != AV_PIX_FMT_GBRAP;
137 return 0;
138}
139
140static int config_output(AVFilterLink *outlink)
141{
142 FilterLink *outl = ff_filter_link(outlink);
143 AVFilterContext *ctx = outlink->src;
144 AlphaMergeContext *s = ctx->priv;
145 AVFilterLink *mainlink = ctx->inputs[0];
146 FilterLink *ml = ff_filter_link(mainlink);
147 AVFilterLink *alphalink = ctx->inputs[1];
148 int ret;
149
150 if (mainlink->w != alphalink->w || mainlink->h != alphalink->h) {
152 "Input frame sizes do not match (%dx%d vs %dx%d).\n",
153 mainlink->w, mainlink->h,
154 alphalink->w, alphalink->h);
155 return AVERROR(EINVAL);
156 }
157
158 if ((ret = ff_framesync_init_dualinput(&s->fs, ctx)) < 0)
159 return ret;
160
161 outlink->w = mainlink->w;
162 outlink->h = mainlink->h;
163 outlink->time_base = mainlink->time_base;
164 outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
165 outl->frame_rate = ml->frame_rate;
166
167 return ff_framesync_configure(&s->fs);
168}
169
171{
172 AlphaMergeContext *s = ctx->priv;
173 return ff_framesync_activate(&s->fs);
174}
175
177{
178 AlphaMergeContext *s = ctx->priv;
179
181}
182
184 {
185 .name = "main",
186 .type = AVMEDIA_TYPE_VIDEO,
187 .config_props = config_input_main,
188 },{
189 .name = "alpha",
190 .type = AVMEDIA_TYPE_VIDEO,
191 },
192};
193
195 {
196 .name = "default",
197 .type = AVMEDIA_TYPE_VIDEO,
198 .config_props = config_output,
199 },
200};
201
202static const AVOption alphamerge_options[] = {
203 { NULL }
204};
205
207
209 .p.name = "alphamerge",
210 .p.description = NULL_IF_CONFIG_SMALL("Copy the luma value of the second "
211 "input into the alpha channel of the first input."),
212 .p.priv_class = &alphamerge_class,
214 .preinit = alphamerge_framesync_preinit,
215 .priv_size = sizeof(AlphaMergeContext),
216 .init = init,
220 .uninit = uninit,
221 .activate = activate,
222};
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition aeval.c:246
const FFFilter ff_vf_alphamerge
static AVFormatContext * ctx
#define U(x)
Definition vpx_arith.h:37
#define A(x)
Definition vpx_arith.h:28
#define V
Definition avdct.c:32
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
Main libavfilter public API header.
#define Y
Definition boxblur.h:37
#define s(width, name)
Definition cbs_vp9.c:198
#define fs(width, name, subs,...)
Definition cbs_vp9.c:200
#define NULL
Definition coverity.c:32
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
Definition drawutils.c:80
misc drawing utilities
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add ref as a new reference to formats.
Definition formats.c:756
AVFilterFormats * ff_make_formats_list_singleton(int fmt)
Equivalent to ff_make_format_list({const int[]}{ fmt, -1 })
Definition formats.c:596
int ff_set_pixel_formats_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const enum AVPixelFormat *fmts)
Definition formats.c:1162
av_warn_unused_result AVFilterFormats * ff_make_pixel_format_list(const enum AVPixelFormat *fmts)
Create a list of supported pixel formats.
int ff_framesync_configure(FFFrameSync *fs)
Configure a frame sync structure.
Definition framesync.c:137
int ff_framesync_activate(FFFrameSync *fs)
Examine the frames in the filter's input and try to produce output.
Definition framesync.c:352
int ff_framesync_init_dualinput(FFFrameSync *fs, AVFilterContext *parent)
Initialize a frame sync structure for dualinput.
Definition framesync.c:372
void ff_framesync_uninit(FFFrameSync *fs)
Free all memory currently allocated.
Definition framesync.c:301
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
#define FRAMESYNC_DEFINE_CLASS(name, context, field)
Definition framesync.h:352
#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:204
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
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
misc image utilities
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:241
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define FFMIN(a, b)
Definition macros.h:49
AVOptions.
const char * av_color_range_name(enum AVColorRange range)
Definition pixdesc.c:3776
pixel format definitions
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition pixfmt.h:766
@ AVALPHA_MODE_STRAIGHT
Alpha channel is independent of color values.
Definition pixfmt.h:819
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition pixfmt.h:99
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition pixfmt.h:102
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition pixfmt.h:101
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition pixfmt.h:108
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition pixfmt.h:100
@ 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_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition pixfmt.h:212
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition pixfmt.h:173
formats
Definition signature.h:47
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
void * priv
private data for use by the filter
Definition avfilter.h:288
Lists of formats / etc.
Definition avfilter.h:120
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int width
Definition frame.h:544
int height
Definition frame.h:544
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition frame.h:723
enum AVAlphaMode alpha_mode
Indicates how the alpha channel of the video is to be handled.
Definition frame.h:827
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:517
AVOption.
Definition opt.h:428
uint8_t rgba_map[4]
Frame sync structure.
Definition framesync.h:168
#define av_log(a,...)
static const AVFilterPad alphamerge_inputs[]
static int do_alphamerge(FFFrameSync *fs)
static const AVOption alphamerge_options[]
static int config_input_main(AVFilterLink *inlink)
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
static int activate(AVFilterContext *ctx)
static av_cold void uninit(AVFilterContext *ctx)
static const AVFilterPad alphamerge_outputs[]
static int config_output(AVFilterLink *outlink)