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/pixfmt.h"
29 #include "avfilter.h"
30 #include "bufferqueue.h"
31 #include "drawutils.h"
32 #include "formats.h"
33 #include "internal.h"
34 #include "video.h"
35 
36 enum { Y, U, V, A };
37 
38 typedef struct {
41  uint8_t rgba_map[4];
42  struct FFBufQueue queue_main;
43  struct FFBufQueue queue_alpha;
45 
46 static av_cold void uninit(AVFilterContext *ctx)
47 {
51 }
52 
54 {
55  static const enum AVPixelFormat main_fmts[] = {
59  };
60  static const enum AVPixelFormat alpha_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
61  AVFilterFormats *main_formats = ff_make_format_list(main_fmts);
62  AVFilterFormats *alpha_formats = ff_make_format_list(alpha_fmts);
63  ff_formats_ref(main_formats, &ctx->inputs[0]->out_formats);
64  ff_formats_ref(alpha_formats, &ctx->inputs[1]->out_formats);
65  ff_formats_ref(main_formats, &ctx->outputs[0]->in_formats);
66  return 0;
67 }
68 
69 static int config_input_main(AVFilterLink *inlink)
70 {
71  AlphaMergeContext *merge = inlink->dst->priv;
72  merge->is_packed_rgb =
73  ff_fill_rgba_map(merge->rgba_map, inlink->format) >= 0;
74  return 0;
75 }
76 
77 static int config_output(AVFilterLink *outlink)
78 {
79  AVFilterContext *ctx = outlink->src;
80  AVFilterLink *mainlink = ctx->inputs[0];
81  AVFilterLink *alphalink = ctx->inputs[1];
82  if (mainlink->w != alphalink->w || mainlink->h != alphalink->h) {
83  av_log(ctx, AV_LOG_ERROR,
84  "Input frame sizes do not match (%dx%d vs %dx%d).\n",
85  mainlink->w, mainlink->h,
86  alphalink->w, alphalink->h);
87  return AVERROR(EINVAL);
88  }
89 
90  outlink->w = mainlink->w;
91  outlink->h = mainlink->h;
92  outlink->time_base = mainlink->time_base;
93  outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
94  outlink->frame_rate = mainlink->frame_rate;
95  return 0;
96 }
97 
98 static void draw_frame(AVFilterContext *ctx,
99  AVFrame *main_buf,
100  AVFrame *alpha_buf)
101 {
102  AlphaMergeContext *merge = ctx->priv;
103  int h = main_buf->height;
104 
105  if (merge->is_packed_rgb) {
106  int x, y;
107  uint8_t *pin, *pout;
108  for (y = 0; y < h; y++) {
109  pin = alpha_buf->data[0] + y * alpha_buf->linesize[0];
110  pout = main_buf->data[0] + y * main_buf->linesize[0] + merge->rgba_map[A];
111  for (x = 0; x < main_buf->width; x++) {
112  *pout = *pin;
113  pin += 1;
114  pout += 4;
115  }
116  }
117  } else {
118  int y;
119  const int main_linesize = main_buf->linesize[A];
120  const int alpha_linesize = alpha_buf->linesize[Y];
121  for (y = 0; y < h && y < alpha_buf->height; y++) {
122  memcpy(main_buf->data[A] + y * main_linesize,
123  alpha_buf->data[Y] + y * alpha_linesize,
124  FFMIN(main_linesize, alpha_linesize));
125  }
126  }
127 }
128 
129 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
130 {
131  AVFilterContext *ctx = inlink->dst;
132  AlphaMergeContext *merge = ctx->priv;
133 
134  int ret = 0;
135  int is_alpha = (inlink == ctx->inputs[1]);
136  struct FFBufQueue *queue =
137  (is_alpha ? &merge->queue_alpha : &merge->queue_main);
138  ff_bufqueue_add(ctx, queue, buf);
139 
140  do {
141  AVFrame *main_buf, *alpha_buf;
142 
143  if (!ff_bufqueue_peek(&merge->queue_main, 0) ||
144  !ff_bufqueue_peek(&merge->queue_alpha, 0)) break;
145 
146  main_buf = ff_bufqueue_get(&merge->queue_main);
147  alpha_buf = ff_bufqueue_get(&merge->queue_alpha);
148 
149  merge->frame_requested = 0;
150  draw_frame(ctx, main_buf, alpha_buf);
151  ret = ff_filter_frame(ctx->outputs[0], main_buf);
152  av_frame_free(&alpha_buf);
153  } while (ret >= 0);
154  return ret;
155 }
156 
157 static int request_frame(AVFilterLink *outlink)
158 {
159  AVFilterContext *ctx = outlink->src;
160  AlphaMergeContext *merge = ctx->priv;
161  int in, ret;
162 
163  merge->frame_requested = 1;
164  while (merge->frame_requested) {
165  in = ff_bufqueue_peek(&merge->queue_main, 0) ? 1 : 0;
166  ret = ff_request_frame(ctx->inputs[in]);
167  if (ret < 0)
168  return ret;
169  }
170  return 0;
171 }
172 
173 static const AVFilterPad alphamerge_inputs[] = {
174  {
175  .name = "main",
176  .type = AVMEDIA_TYPE_VIDEO,
177  .config_props = config_input_main,
178  .get_video_buffer = ff_null_get_video_buffer,
179  .filter_frame = filter_frame,
180  .needs_writable = 1,
181  },{
182  .name = "alpha",
183  .type = AVMEDIA_TYPE_VIDEO,
184  .filter_frame = filter_frame,
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  .request_frame = request_frame,
195  },
196  { NULL }
197 };
198 
200  .name = "alphamerge",
201  .description = NULL_IF_CONFIG_SMALL("Copy the luma value of the second "
202  "input into the alpha channel of the first input."),
203  .uninit = uninit,
204  .priv_size = sizeof(AlphaMergeContext),
206  .inputs = alphamerge_inputs,
207  .outputs = alphamerge_outputs,
208 };
static AVFrame * ff_bufqueue_get(struct FFBufQueue *queue)
Get the first buffer from the queue and remove it.
Definition: bufferqueue.h:98
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
struct FFBufQueue queue_alpha
Definition: vf_alphamerge.c:43
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:117
struct FFBufQueue queue_main
Definition: vf_alphamerge.c:42
external API header
static const AVFilterPad alphamerge_inputs[]
About Git write you should know how to use GIT properly Luckily Git comes with excellent documentation git help man git shows you the available git< command > help man git< command > shows information about the subcommand< command > The most comprehensive manual is the website Git Reference visit they are quite exhaustive You do not need a special username or password All you need is to provide a ssh public key to the Git server admin What follows now is a basic introduction to Git and some FFmpeg specific guidelines Read it at least if you are granted commit privileges to the FFmpeg project you are expected to be familiar with these rules I if not You can get git from etc no matter how small Every one of them has been saved from looking like a fool by this many times It s very easy for stray debug output or cosmetic modifications to slip in
Definition: git-howto.txt:5
AVFrame * ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
Definition: video.c:35
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:308
Structure holding the queue.
Definition: bufferqueue.h:49
const char * name
Pad name.
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:532
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:105
uint8_t
it can be given away to ff_start_frame *A reference passed to ff_filter_frame(or the deprecated ff_start_frame) is given away and must no longer be used.*A reference created with avfilter_ref_buffer belongs to the code that created it.*A reference obtained with ff_get_video_buffer or ff_get_audio_buffer belongs to the code that requested it.*A reference given as return value by the get_video_buffer or get_audio_buffer method is given away and must no longer be used.Link reference fields---------------------The AVFilterLink structure has a few AVFilterBufferRef fields.The cur_buf and out_buf were used with the deprecated start_frame/draw_slice/end_frame API and should no longer be used.src_buf
#define av_cold
Definition: attributes.h:78
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:98
A filter pad used for either input or output.
Discrete Time axis x
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:219
int width
width and height of the video frame
Definition: frame.h:122
static void draw_frame(AVFilterContext *ctx, AVFrame *main_buf, AVFrame *alpha_buf)
Definition: vf_alphamerge.c:98
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:99
void * priv
private data for use by the filter
Definition: avfilter.h:545
static int query_formats(AVFilterContext *ctx)
Definition: vf_alphamerge.c:53
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:96
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:97
AVFrame * queue[FF_BUFQUEUE_SIZE]
Definition: bufferqueue.h:50
#define FFMIN(a, b)
Definition: common.h:58
ret
Definition: avfilter.c:821
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_alphamerge.c:46
static const AVFilterPad alphamerge_outputs[]
static void ff_bufqueue_discard_all(struct FFBufQueue *queue)
Unref and remove all buffers from the queue.
Definition: bufferqueue.h:111
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
Definition: drawutils.c:33
static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
NULL
Definition: eval.c:55
void ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:432
misc drawing utilities
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:218
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
void * buf
Definition: avisynth_c.h:594
Filter definition.
Definition: avfilter.h:436
const char * name
filter name
Definition: avfilter.h:437
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:539
static void merge(GetBitContext *gb, uint8_t *dst, uint8_t *src, int size)
Merge two consequent lists of equal size depending on bits read.
static int config_output(AVFilterLink *outlink)
Definition: vf_alphamerge.c:77
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
Y , 8bpp.
Definition: pixfmt.h:76
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:108
function y
Definition: D.m:1
pixel format definitions
half analysis window size pin
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:524
int height
Definition: frame.h:122
static int request_frame(AVFilterLink *outlink)
uint8_t rgba_map[4]
Definition: vf_alphamerge.c:41
static void ff_bufqueue_add(void *log, struct FFBufQueue *queue, AVFrame *buf)
Add a buffer to the queue.
Definition: bufferqueue.h:71
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:319
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
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
AVFilter avfilter_vf_alphamerge
static AVFrame * ff_bufqueue_peek(struct FFBufQueue *queue, unsigned index)
Get a buffer from the queue without altering it.
Definition: bufferqueue.h:87