FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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  int y;
90  for (y = 0; y < outlink->h; y++) {
91  memcpy(out_buf->data[Y] + y * out_buf->linesize[Y],
92  cur_buf->data[A] + y * cur_buf->linesize[A],
93  outlink->w);
94  }
95  }
96 
97  ret = ff_filter_frame(outlink, out_buf);
98 
99 end:
100  av_frame_free(&cur_buf);
101  return ret;
102 }
103 
105  {
106  .name = "default",
107  .type = AVMEDIA_TYPE_VIDEO,
108  .config_props = config_input,
109  .filter_frame = filter_frame,
110  },
111  { NULL }
112 };
113 
115  {
116  .name = "default",
117  .type = AVMEDIA_TYPE_VIDEO,
118  },
119  { NULL }
120 };
121 
123  .name = "alphaextract",
124  .description = NULL_IF_CONFIG_SMALL("Extract an alpha channel as a "
125  "grayscale image component."),
126  .priv_size = sizeof(AlphaExtractContext),
128  .inputs = alphaextract_inputs,
129  .outputs = alphaextract_outputs,
130 };