vf_alphaextract.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  * simple channel-swapping filter to get at the alpha component
24  */
25 
26 #include <string.h>
27 
28 #include "libavutil/pixfmt.h"
29 #include "avfilter.h"
30 #include "drawutils.h"
31 #include "internal.h"
32 #include "formats.h"
33 #include "video.h"
34 
35 enum { Y, U, V, A };
36 
37 typedef struct {
39  uint8_t rgba_map[4];
41 
43 {
44  static const enum AVPixelFormat in_fmts[] = {
48  };
49  static const enum AVPixelFormat out_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
52  return 0;
53 }
54 
55 static int config_input(AVFilterLink *inlink)
56 {
57  AlphaExtractContext *extract = inlink->dst->priv;
58  extract->is_packed_rgb =
59  ff_fill_rgba_map(extract->rgba_map, inlink->format) >= 0;
60  return 0;
61 }
62 
63 static int filter_frame(AVFilterLink *inlink, AVFrame *cur_buf)
64 {
65  AlphaExtractContext *extract = inlink->dst->priv;
66  AVFilterLink *outlink = inlink->dst->outputs[0];
67  AVFrame *out_buf = ff_get_video_buffer(outlink, outlink->w, outlink->h);
68  int ret;
69 
70  if (!out_buf) {
71  ret = AVERROR(ENOMEM);
72  goto end;
73  }
74  av_frame_copy_props(out_buf, cur_buf);
75 
76  if (extract->is_packed_rgb) {
77  int x, y;
78  uint8_t *pcur, *pout;
79  for (y = 0; y < outlink->h; y++) {
80  pcur = cur_buf->data[0] + y * cur_buf->linesize[0] + extract->rgba_map[A];
81  pout = out_buf->data[0] + y * out_buf->linesize[0];
82  for (x = 0; x < outlink->w; x++) {
83  *pout = *pcur;
84  pout += 1;
85  pcur += 4;
86  }
87  }
88  } else {
89  const int linesize = abs(FFMIN(out_buf->linesize[Y], cur_buf->linesize[A]));
90  int y;
91  for (y = 0; y < outlink->h; y++) {
92  memcpy(out_buf->data[Y] + y * out_buf->linesize[Y],
93  cur_buf->data[A] + y * cur_buf->linesize[A],
94  linesize);
95  }
96  }
97 
98  ret = ff_filter_frame(outlink, out_buf);
99 
100 end:
101  av_frame_free(&cur_buf);
102  return ret;
103 }
104 
106  {
107  .name = "default",
108  .type = AVMEDIA_TYPE_VIDEO,
109  .config_props = config_input,
110  .filter_frame = filter_frame,
111  },
112  { NULL }
113 };
114 
116  {
117  .name = "default",
118  .type = AVMEDIA_TYPE_VIDEO,
119  },
120  { NULL }
121 };
122 
124  .name = "alphaextract",
125  .description = NULL_IF_CONFIG_SMALL("Extract an alpha channel as a "
126  "grayscale image component."),
127  .priv_size = sizeof(AlphaExtractContext),
129  .inputs = alphaextract_inputs,
130  .outputs = alphaextract_outputs,
131 };
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:424
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:117
external API header
static int config_input(AVFilterLink *inlink)
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:143
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:308
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
end end
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
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:219
#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
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:96
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:97
static int query_formats(AVFilterContext *ctx)
#define FFMIN(a, b)
Definition: common.h:58
ret
Definition: avfilter.c:821
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
Definition: drawutils.c:33
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
Filter definition.
Definition: avfilter.h:436
static const AVFilterPad alphaextract_outputs[]
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
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
Y , 8bpp.
Definition: pixfmt.h:76
static const AVFilterPad alphaextract_inputs[]
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
static int filter_frame(AVFilterLink *inlink, AVFrame *cur_buf)
An instance of a filter.
Definition: avfilter.h:524
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_alphaextract