FFmpeg  4.3
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 = NULL, *alpha_formats = NULL;
59  int ret;
60 
61  if (!(main_formats = ff_make_format_list(main_fmts)) ||
62  !(alpha_formats = ff_make_format_list(alpha_fmts))) {
63  ret = AVERROR(ENOMEM);
64  goto fail;
65  }
66  if ((ret = ff_formats_ref(main_formats , &ctx->inputs[0]->out_formats)) < 0 ||
67  (ret = ff_formats_ref(alpha_formats, &ctx->inputs[1]->out_formats)) < 0 ||
68  (ret = ff_formats_ref(main_formats , &ctx->outputs[0]->in_formats)) < 0)
69  goto fail;
70  return 0;
71 fail:
72  if (main_formats)
73  av_freep(&main_formats->formats);
74  av_freep(&main_formats);
75  if (alpha_formats)
76  av_freep(&alpha_formats->formats);
77  av_freep(&alpha_formats);
78  return ret;
79 }
80 
82 {
83  AlphaMergeContext *s = inlink->dst->priv;
84  s->is_packed_rgb =
85  ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0 &&
86  inlink->format != AV_PIX_FMT_GBRAP;
87  return 0;
88 }
89 
90 static int config_output(AVFilterLink *outlink)
91 {
92  AVFilterContext *ctx = outlink->src;
93  AVFilterLink *mainlink = ctx->inputs[0];
94  AVFilterLink *alphalink = ctx->inputs[1];
95  if (mainlink->w != alphalink->w || mainlink->h != alphalink->h) {
97  "Input frame sizes do not match (%dx%d vs %dx%d).\n",
98  mainlink->w, mainlink->h,
99  alphalink->w, alphalink->h);
100  return AVERROR(EINVAL);
101  }
102 
103  outlink->w = mainlink->w;
104  outlink->h = mainlink->h;
105  outlink->time_base = mainlink->time_base;
106  outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
107  outlink->frame_rate = mainlink->frame_rate;
108  return 0;
109 }
110 
112  AVFrame *main_buf,
113  AVFrame *alpha_buf)
114 {
115  AlphaMergeContext *s = ctx->priv;
116  int h = main_buf->height;
117 
118  if (s->is_packed_rgb) {
119  int x, y;
120  uint8_t *pin, *pout;
121  for (y = 0; y < h; y++) {
122  pin = alpha_buf->data[0] + y * alpha_buf->linesize[0];
123  pout = main_buf->data[0] + y * main_buf->linesize[0] + s->rgba_map[A];
124  for (x = 0; x < main_buf->width; x++) {
125  *pout = *pin;
126  pin += 1;
127  pout += 4;
128  }
129  }
130  } else {
131  const int main_linesize = main_buf->linesize[A];
132  const int alpha_linesize = alpha_buf->linesize[Y];
133  av_image_copy_plane(main_buf->data[A], main_linesize,
134  alpha_buf->data[Y], alpha_linesize,
135  FFMIN(main_linesize, alpha_linesize), alpha_buf->height);
136  }
137 }
138 
140 {
141  AVFilterLink *outlink = ctx->outputs[0];
142  AlphaMergeContext *s = ctx->priv;
143  int ret;
144 
146 
147  if (!s->main_frame) {
148  ret = ff_inlink_consume_frame(ctx->inputs[0], &s->main_frame);
149  if (ret < 0)
150  return ret;
151  }
152 
153  if (!s->alpha_frame) {
154  ret = ff_inlink_consume_frame(ctx->inputs[1], &s->alpha_frame);
155  if (ret < 0)
156  return ret;
157  }
158 
159  if (s->main_frame && s->alpha_frame) {
160  if (!ctx->is_disabled)
161  draw_frame(ctx, s->main_frame, s->alpha_frame);
162  ret = ff_filter_frame(outlink, s->main_frame);
163  av_frame_free(&s->alpha_frame);
164  s->main_frame = NULL;
165  return ret;
166  }
167 
168  FF_FILTER_FORWARD_STATUS(ctx->inputs[0], outlink);
169  FF_FILTER_FORWARD_STATUS(ctx->inputs[1], outlink);
170 
171  if (ff_outlink_frame_wanted(ctx->outputs[0]) &&
172  !ff_outlink_get_status(ctx->inputs[0]) &&
173  !s->main_frame) {
174  ff_inlink_request_frame(ctx->inputs[0]);
175  return 0;
176  }
177 
178  if (ff_outlink_frame_wanted(ctx->outputs[0]) &&
179  !ff_outlink_get_status(ctx->inputs[1]) &&
180  !s->alpha_frame) {
181  ff_inlink_request_frame(ctx->inputs[1]);
182  return 0;
183  }
184 
185  return FFERROR_NOT_READY;
186 }
187 
188 static const AVFilterPad alphamerge_inputs[] = {
189  {
190  .name = "main",
191  .type = AVMEDIA_TYPE_VIDEO,
192  .config_props = config_input_main,
193  .needs_writable = 1,
194  },{
195  .name = "alpha",
196  .type = AVMEDIA_TYPE_VIDEO,
197  },
198  { NULL }
199 };
200 
201 static const AVFilterPad alphamerge_outputs[] = {
202  {
203  .name = "default",
204  .type = AVMEDIA_TYPE_VIDEO,
205  .config_props = config_output,
206  },
207  { NULL }
208 };
209 
210 static const AVOption alphamerge_options[] = {
211  { NULL }
212 };
213 
214 AVFILTER_DEFINE_CLASS(alphamerge);
215 
217  .name = "alphamerge",
218  .description = NULL_IF_CONFIG_SMALL("Copy the luma value of the second "
219  "input into the alpha channel of the first input."),
220  .priv_size = sizeof(AlphaMergeContext),
221  .priv_class = &alphamerge_class,
225  .activate = activate,
227 };
AlphaMergeContext::main_frame
AVFrame * main_frame
Definition: vf_alphamerge.c:45
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
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:283
alphamerge_inputs
static const AVFilterPad alphamerge_inputs[]
Definition: vf_alphamerge.c:188
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1075
FFERROR_NOT_READY
return FFERROR_NOT_READY
Definition: filter_design.txt:204
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
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
AVFrame::width
int width
Definition: frame.h:358
alphamerge_outputs
static const AVFilterPad alphamerge_outputs[]
Definition: vf_alphamerge.c:201
AVOption
AVOption.
Definition: opt.h:246
config_output
static int config_output(AVFilterLink *outlink)
Definition: vf_alphamerge.c:90
AV_PIX_FMT_BGRA
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:95
AVFilterFormats::formats
int * formats
list of media formats
Definition: formats.h:66
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:148
ff_vf_alphamerge
AVFilter ff_vf_alphamerge
Definition: vf_alphamerge.c:216
video.h
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: vf_alphamerge.c:49
config_input_main
static int config_input_main(AVFilterLink *inlink)
Definition: vf_alphamerge.c:81
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:314
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:338
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:64
formats.h
ff_inlink_consume_frame
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
Definition: avfilter.c:1476
FF_FILTER_FORWARD_STATUS_BACK_ALL
#define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter)
Forward the status on an output link to all input links.
Definition: filters.h:212
AV_PIX_FMT_GBRAP
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:215
fail
#define fail()
Definition: checkasm.h:123
x
FFmpeg Automated Testing Environment ************************************Introduction Using FATE from your FFmpeg source directory Submitting the results to the FFmpeg result aggregation server Uploading new samples to the fate suite FATE makefile targets and variables Makefile targets Makefile variables Examples Introduction **************FATE is an extended regression suite on the client side and a means for results aggregation and presentation on the server side The first part of this document explains how you can use FATE from your FFmpeg source directory to test your ffmpeg binary The second part describes how you can run FATE to submit the results to FFmpeg’s FATE server In any way you can have a look at the publicly viewable FATE results by visiting this as it can be seen if some test on some platform broke with their recent contribution This usually happens on the platforms the developers could not test on The second part of this document describes how you can run FATE to submit your results to FFmpeg’s FATE server If you want to submit your results be sure to check that your combination of OS and compiler is not already listed on the above mentioned website In the third part you can find a comprehensive listing of FATE makefile targets and variables Using FATE from your FFmpeg source directory **********************************************If you want to run FATE on your machine you need to have the samples in place You can get the samples via the build target fate rsync Use this command from the top level source this will cause FATE to fail NOTE To use a custom wrapper to run the pass ‘ target exec’ to ‘configure’ or set the TARGET_EXEC Make variable Submitting the results to the FFmpeg result aggregation server ****************************************************************To submit your results to the server you should run fate through the shell script ‘tests fate sh’ from the FFmpeg sources This script needs to be invoked with a configuration file as its first argument tests fate sh path to fate_config A configuration file template with comments describing the individual configuration variables can be found at ‘doc fate_config sh template’ Create a configuration that suits your based on the configuration template The ‘slot’ configuration variable can be any string that is not yet but it is suggested that you name it adhering to the following pattern ‘ARCH OS COMPILER COMPILER VERSION’ The configuration file itself will be sourced in a shell therefore all shell features may be used This enables you to setup the environment as you need it for your build For your first test runs the ‘fate_recv’ variable should be empty or commented out This will run everything as normal except that it will omit the submission of the results to the server The following files should be present in $workdir as specified in the configuration it may help to try out the ‘ssh’ command with one or more ‘ v’ options You should get detailed output concerning your SSH configuration and the authentication process The only thing left is to automate the execution of the fate sh script and the synchronisation of the samples directory Uploading new samples to the fate suite *****************************************If you need a sample uploaded send a mail to samples request This is for developers who have an account on the fate suite server If you upload new please make sure they are as small as space on each network bandwidth and so on benefit from smaller test cases Also keep in mind older checkouts use existing sample that means in practice generally do not remove or overwrite files as it likely would break older checkouts or releases Also all needed samples for a commit should be ideally before the push If you need an account for frequently uploading samples or you wish to help others by doing that send a mail to ffmpeg devel rsync vauL Duo x
Definition: fate.txt:150
A
@ A
Definition: vf_alphamerge.c:38
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:54
U
@ U
Definition: vf_alphamerge.c:38
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
ff_inlink_request_frame
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Definition: avfilter.c:1602
s
#define s(width, name)
Definition: cbs_vp9.c:257
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:101
ff_formats_ref
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:470
outputs
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
Y
@ Y
Definition: vf_alphamerge.c:38
AlphaMergeContext::rgba_map
uint8_t rgba_map[4]
Definition: vf_alphamerge.c:44
V
@ V
Definition: vf_alphamerge.c:38
inputs
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
Definition: filter_design.txt:243
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
AV_PIX_FMT_ABGR
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:94
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:186
FFMIN
#define FFMIN(a, b)
Definition: common.h:96
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:177
internal.h
AV_PIX_FMT_ARGB
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:92
uint8_t
uint8_t
Definition: audio_convert.c:194
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:60
draw_frame
static void draw_frame(AVFilterContext *ctx, AVFrame *main_buf, AVFrame *alpha_buf)
Definition: vf_alphamerge.c:111
AVFilter
Filter definition.
Definition: avfilter.h:144
ret
ret
Definition: filter_design.txt:187
pixfmt.h
AlphaMergeContext::alpha_frame
AVFrame * alpha_frame
Definition: vf_alphamerge.c:46
AVFrame::height
int height
Definition: frame.h:358
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
avfilter.h
alphamerge_options
static const AVOption alphamerge_options[]
Definition: vf_alphamerge.c:210
ff_outlink_get_status
int ff_outlink_get_status(AVFilterLink *link)
Get the status on an output link.
Definition: avfilter.c:1625
AVFilterContext
An instance of a filter.
Definition: avfilter.h:338
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(alphamerge)
FF_FILTER_FORWARD_STATUS
FF_FILTER_FORWARD_STATUS(inlink, outlink)
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
ff_fill_rgba_map
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
Definition: drawutils.c:35
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:133
imgutils.h
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:564
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:331
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
h
h
Definition: vp9dsp_template.c:2038
ff_outlink_frame_wanted
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
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:176
activate
static int activate(AVFilterContext *ctx)
Definition: vf_alphamerge.c:139
AlphaMergeContext::is_packed_rgb
int is_packed_rgb
Definition: vf_alphamerge.c:43