2 * This file is part of FFmpeg.
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.
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.
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
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"
30 typedef struct ShufflePlanesContext {
33 /* number of planes in the selected pixel format */
39 /* set to 1 if some plane is used more than once, so we need to make a copy */
41 } ShufflePlanesContext;
43 static int query_formats(AVFilterContext *ctx)
45 AVFilterFormats *formats = NULL;
46 ShufflePlanesContext *s = ctx->priv;
49 for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
50 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
51 int planes = av_pix_fmt_count_planes(fmt);
53 if (!(desc->flags & AV_PIX_FMT_FLAG_PAL) &&
54 !(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) {
55 for (i = 0; i < 4; i++) {
56 if (s->map[i] >= planes)
59 if ((desc->log2_chroma_h || desc->log2_chroma_w) &&
60 (i == 1 || i == 2) != (s->map[i] == 1 || s->map[i] == 2))
66 if ((ret = ff_add_format(&formats, fmt)) < 0) {
72 return ff_set_common_formats(ctx, formats);
75 static av_cold int shuffleplanes_config_input(AVFilterLink *inlink)
77 AVFilterContext *ctx = inlink->dst;
78 ShufflePlanesContext *s = ctx->priv;
83 s->planes = av_pix_fmt_count_planes(inlink->format);
85 for (i = 0; i < s->planes; i++) {
94 static int shuffleplanes_filter_frame(AVFilterLink *inlink, AVFrame *frame)
96 AVFilterContext *ctx = inlink->dst;
97 ShufflePlanesContext *s = ctx->priv;
98 uint8_t *shuffled_data[4] = { NULL };
99 int shuffled_linesize[4] = { 0 };
102 for (i = 0; i < s->planes; i++) {
103 shuffled_data[i] = frame->data[s->map[i]];
104 shuffled_linesize[i] = frame->linesize[s->map[i]];
106 memcpy(frame->data, shuffled_data, sizeof(shuffled_data));
107 memcpy(frame->linesize, shuffled_linesize, sizeof(shuffled_linesize));
110 AVFrame *copy = ff_get_video_buffer(ctx->outputs[0], frame->width, frame->height);
113 ret = AVERROR(ENOMEM);
117 av_frame_copy(copy, frame);
119 ret = av_frame_copy_props(copy, frame);
121 av_frame_free(©);
125 av_frame_free(&frame);
129 return ff_filter_frame(ctx->outputs[0], frame);
131 av_frame_free(&frame);
135 #define OFFSET(x) offsetof(ShufflePlanesContext, x)
136 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
137 static const AVOption shuffleplanes_options[] = {
138 { "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 },
139 { "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 },
140 { "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 },
141 { "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 AVFILTER_DEFINE_CLASS(shuffleplanes);
147 static const AVFilterPad shuffleplanes_inputs[] = {
150 .type = AVMEDIA_TYPE_VIDEO,
151 .config_props = shuffleplanes_config_input,
152 .filter_frame = shuffleplanes_filter_frame,
157 static const AVFilterPad shuffleplanes_outputs[] = {
160 .type = AVMEDIA_TYPE_VIDEO,
165 AVFilter ff_vf_shuffleplanes = {
166 .name = "shuffleplanes",
167 .description = NULL_IF_CONFIG_SMALL("Shuffle video planes."),
168 .priv_size = sizeof(ShufflePlanesContext),
169 .priv_class = &shuffleplanes_class,
170 .query_formats = query_formats,
171 .inputs = shuffleplanes_inputs,
172 .outputs = shuffleplanes_outputs,
173 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,