2 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (c) 2013 Paul B Mahol
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * (de)interleave fields filter
27 #include "libavutil/opt.h"
28 #include "libavutil/imgutils.h"
29 #include "libavutil/pixdesc.h"
41 enum FilterMode luma_mode, chroma_mode, alpha_mode;
42 int luma_swap, chroma_swap, alpha_swap;
44 int linesize[4], chroma_height;
48 #define OFFSET(x) offsetof(IlContext, x)
49 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
51 static const AVOption il_options[] = {
52 {"luma_mode", "select luma mode", OFFSET(luma_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, "luma_mode"},
53 {"l", "select luma mode", OFFSET(luma_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, "luma_mode"},
54 {"none", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_NONE}, 0, 0, FLAGS, "luma_mode"},
55 {"interleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE}, 0, 0, FLAGS, "luma_mode"},
56 {"i", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE}, 0, 0, FLAGS, "luma_mode"},
57 {"deinterleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, "luma_mode"},
58 {"d", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, "luma_mode"},
59 {"chroma_mode", "select chroma mode", OFFSET(chroma_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, "chroma_mode"},
60 {"c", "select chroma mode", OFFSET(chroma_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, "chroma_mode"},
61 {"none", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_NONE}, 0, 0, FLAGS, "chroma_mode"},
62 {"interleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE}, 0, 0, FLAGS, "chroma_mode"},
63 {"i", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE}, 0, 0, FLAGS, "chroma_mode"},
64 {"deinterleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, "chroma_mode"},
65 {"d", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, "chroma_mode"},
66 {"alpha_mode", "select alpha mode", OFFSET(alpha_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, "alpha_mode"},
67 {"a", "select alpha mode", OFFSET(alpha_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, "alpha_mode"},
68 {"none", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_NONE}, 0, 0, FLAGS, "alpha_mode"},
69 {"interleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE}, 0, 0, FLAGS, "alpha_mode"},
70 {"i", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE}, 0, 0, FLAGS, "alpha_mode"},
71 {"deinterleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, "alpha_mode"},
72 {"d", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, "alpha_mode"},
73 {"luma_swap", "swap luma fields", OFFSET(luma_swap), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS},
74 {"ls", "swap luma fields", OFFSET(luma_swap), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS},
75 {"chroma_swap", "swap chroma fields", OFFSET(chroma_swap), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS},
76 {"cs", "swap chroma fields", OFFSET(chroma_swap), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS},
77 {"alpha_swap", "swap alpha fields", OFFSET(alpha_swap), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS},
78 {"as", "swap alpha fields", OFFSET(alpha_swap), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS},
82 AVFILTER_DEFINE_CLASS(il);
84 static av_cold int init(AVFilterContext *ctx, const char *args)
86 IlContext *il = ctx->priv;
89 il->class = &il_class;
90 av_opt_set_defaults(il);
92 if ((ret = av_set_options_string(il, args, "=", ":")) < 0)
98 static int query_formats(AVFilterContext *ctx)
100 AVFilterFormats *formats = NULL;
103 for (fmt = 0; fmt < AV_PIX_FMT_NB; fmt++) {
104 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
105 if (!(desc->flags & PIX_FMT_PAL))
106 ff_add_format(&formats, fmt);
109 ff_set_common_formats(ctx, formats);
113 static int config_input(AVFilterLink *inlink)
115 IlContext *il = inlink->dst->priv;
116 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
119 for (i = 0; i < desc->nb_components; i++)
120 il->nb_planes = FFMAX(il->nb_planes, desc->comp[i].plane);
123 il->has_alpha = !!(desc->flags & PIX_FMT_ALPHA);
124 if ((ret = av_image_fill_linesizes(il->linesize, inlink->format, inlink->w)) < 0)
127 il->chroma_height = inlink->h >> desc->log2_chroma_h;
132 static void interleave(uint8_t *dst, uint8_t *src, int w, int h,
133 int dst_linesize, int src_linesize,
134 enum FilterMode mode, int swap)
138 const int m = h >> 1;
142 case MODE_DEINTERLEAVE:
143 for (y = 0; y < m; y++) {
144 memcpy(dst + dst_linesize * y , src + src_linesize * (y * 2 + a), w);
145 memcpy(dst + dst_linesize * (y + m), src + src_linesize * (y * 2 + b), w);
149 for (y = 0; y < m; y++) {
150 memcpy(dst + dst_linesize * y * 2 , src + src_linesize * (y * 2 + a), w);
151 memcpy(dst + dst_linesize * (y * 2 + 1), src + src_linesize * (y * 2 + b), w);
154 case MODE_INTERLEAVE:
155 for (y = 0; y < m; y++) {
156 memcpy(dst + dst_linesize * (y * 2 + a), src + src_linesize * y , w);
157 memcpy(dst + dst_linesize * (y * 2 + b), src + src_linesize * (y + m), w);
163 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
165 IlContext *il = inlink->dst->priv;
166 AVFilterLink *outlink = inlink->dst->outputs[0];
167 AVFilterBufferRef *out;
170 out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
172 avfilter_unref_bufferp(&inpicref);
173 return AVERROR(ENOMEM);
175 avfilter_copy_buffer_ref_props(out, inpicref);
177 interleave(out->data[0], inpicref->data[0],
178 il->linesize[0], inlink->h,
179 out->linesize[0], inpicref->linesize[0],
180 il->luma_mode, il->luma_swap);
182 for (comp = 1; comp < (il->nb_planes - il->has_alpha); comp++) {
183 interleave(out->data[comp], inpicref->data[comp],
184 il->linesize[comp], il->chroma_height,
185 out->linesize[comp], inpicref->linesize[comp],
186 il->chroma_mode, il->chroma_swap);
190 int comp = il->nb_planes - 1;
191 interleave(out->data[comp], inpicref->data[comp],
192 il->linesize[comp], inlink->h,
193 out->linesize[comp], inpicref->linesize[comp],
194 il->alpha_mode, il->alpha_swap);
197 ret = ff_filter_frame(outlink, out);
198 avfilter_unref_bufferp(&inpicref);
202 static const AVFilterPad inputs[] = {
205 .type = AVMEDIA_TYPE_VIDEO,
206 .get_video_buffer = ff_null_get_video_buffer,
207 .filter_frame = filter_frame,
208 .config_props = config_input,
213 static const AVFilterPad outputs[] = {
216 .type = AVMEDIA_TYPE_VIDEO,
221 AVFilter avfilter_vf_il = {
223 .description = NULL_IF_CONFIG_SMALL("Deinterleave or interleave fields."),
224 .priv_size = sizeof(IlContext),
226 .query_formats = query_formats,
229 .priv_class = &il_class,