FFmpeg
Loading...
Searching...
No Matches
vf_shuffleplanes.c
Go to the documentation of this file.
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include "libavutil/avstring.h"
20#include "libavutil/common.h"
21#include "libavutil/internal.h"
22#include "libavutil/opt.h"
23#include "libavutil/pixdesc.h"
24#include "libavutil/pixfmt.h"
25
26#include "avfilter.h"
27#include "filters.h"
28#include "formats.h"
29#include "video.h"
30
31typedef struct ShufflePlanesContext {
32 const AVClass *class;
33
34 /* number of planes in the selected pixel format */
35 int planes;
36
37 /* mapping indices */
38 int map[4];
39
40 /* set to 1 if some plane is used more than once, so we need to make a copy */
41 int copy;
43
45 AVFilterFormatsConfig **cfg_in,
46 AVFilterFormatsConfig **cfg_out)
47{
49 const ShufflePlanesContext *s = ctx->priv;
50 int fmt, ret, i;
51
52 for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
55
56 if (!(desc->flags & AV_PIX_FMT_FLAG_PAL) &&
57 !(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) {
58 for (i = 0; i < 4; i++) {
59 if (s->map[i] >= planes)
60 break;
61
62 if ((desc->log2_chroma_h || desc->log2_chroma_w) &&
63 (i == 1 || i == 2) != (s->map[i] == 1 || s->map[i] == 2))
64 break;
65 }
66
67 if (i != 4)
68 continue;
69 if ((ret = ff_add_format(&formats, fmt)) < 0) {
70 return ret;
71 }
72 }
73 }
74
75 return ff_set_common_formats2(ctx, cfg_in, cfg_out, formats);
76}
77
79{
80 AVFilterContext *ctx = inlink->dst;
81 ShufflePlanesContext *s = ctx->priv;
82 int used[4] = { 0 };
83 int i;
84
85 s->copy = 0;
86 s->planes = av_pix_fmt_count_planes(inlink->format);
87
88 for (i = 0; i < s->planes; i++) {
89 if (used[s->map[i]])
90 s->copy = 1;
91 used[s->map[i]]++;
92 }
93
94 return 0;
95}
96
98{
99 AVFilterContext *ctx = inlink->dst;
100 ShufflePlanesContext *s = ctx->priv;
101 uint8_t *shuffled_data[4] = { NULL };
102 int shuffled_linesize[4] = { 0 };
103 int i, ret;
104
105 for (i = 0; i < s->planes; i++) {
106 shuffled_data[i] = frame->data[s->map[i]];
107 shuffled_linesize[i] = frame->linesize[s->map[i]];
108 }
109 memcpy(frame->data, shuffled_data, sizeof(shuffled_data));
110 memcpy(frame->linesize, shuffled_linesize, sizeof(shuffled_linesize));
111
112 if (s->copy) {
113 AVFrame *copy = ff_get_video_buffer(ctx->outputs[0], frame->width, frame->height);
114
115 if (!copy) {
116 ret = AVERROR(ENOMEM);
117 goto fail;
118 }
119
121
123 if (ret < 0) {
125 goto fail;
126 }
127
129 frame = copy;
130 }
131
132 return ff_filter_frame(ctx->outputs[0], frame);
133fail:
135 return ret;
136}
137
138#define OFFSET(x) offsetof(ShufflePlanesContext, x)
139#define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
141 { "map0", "Index of the input plane to be used as the first output plane ", OFFSET(map[0]), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 3, FLAGS },
142 { "map1", "Index of the input plane to be used as the second output plane ", OFFSET(map[1]), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 3, FLAGS },
143 { "map2", "Index of the input plane to be used as the third output plane ", OFFSET(map[2]), AV_OPT_TYPE_INT, { .i64 = 2 }, 0, 3, FLAGS },
144 { "map3", "Index of the input plane to be used as the fourth output plane ", OFFSET(map[3]), AV_OPT_TYPE_INT, { .i64 = 3 }, 0, 3, FLAGS },
145 { NULL },
146};
147
149
151 {
152 .name = "default",
153 .type = AVMEDIA_TYPE_VIDEO,
154 .config_props = shuffleplanes_config_input,
155 .filter_frame = shuffleplanes_filter_frame,
156 },
157};
158
160 .p.name = "shuffleplanes",
161 .p.description = NULL_IF_CONFIG_SMALL("Shuffle video planes."),
162 .p.priv_class = &shuffleplanes_class,
164 .priv_size = sizeof(ShufflePlanesContext),
168};
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition aeval.c:246
const FFFilter ff_vf_shuffleplanes
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 i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
common internal and external API header
#define NULL
Definition coverity.c:32
static AVFrame * frame
int ff_set_common_formats2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *formats)
Definition formats.c:1137
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition formats.c:571
#define fail
Definition test.h:479
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition avfilter.h:196
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
Definition frame.c:711
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
const VDPAUPixFmtMap * map
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:241
#define av_cold
Definition attributes.h:117
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
const char * desc
Definition libsvtav1.c:83
static const struct @257111027162314367033347246032313251342043035002 planes[]
AVOptions.
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3500
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition pixdesc.h:128
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition pixdesc.h:120
pixel format definitions
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
Lists of formats / etc.
Definition avfilter.h:120
A list of supported formats for one end of a filter link.
Definition formats.h:64
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
AVOption.
Definition opt.h:428
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
static AVFormatContext * ctx
Definition movenc.c:49
static const AVOption shuffleplanes_options[]
static int shuffleplanes_filter_frame(AVFilterLink *inlink, AVFrame *frame)
static av_cold int shuffleplanes_config_input(AVFilterLink *inlink)
static const AVFilterPad shuffleplanes_inputs[]
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
#define OFFSET(x)
static void copy(const float *p1, float *p2, const int length)
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition video.c:37
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:89