FFmpeg
Loading...
Searching...
No Matches
vf_il.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (c) 2013 Paul B Mahol
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22/**
23 * @file
24 * (de)interleave fields filter
25 */
26
27#include "libavutil/opt.h"
28#include "libavutil/imgutils.h"
29#include "libavutil/pixdesc.h"
30#include "avfilter.h"
31#include "filters.h"
32#include "formats.h"
33#include "video.h"
34
40
41typedef struct IlContext {
42 const AVClass *class;
43 int luma_mode, chroma_mode, alpha_mode; ///<FilterMode
48} IlContext;
49
50#define OFFSET(x) offsetof(IlContext, x)
51#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
52
53static const AVOption il_options[] = {
54 {"luma_mode", "select luma mode", OFFSET(luma_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, .unit = "luma_mode"},
55 {"l", "select luma mode", OFFSET(luma_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, .unit = "luma_mode"},
56 {"none", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_NONE}, 0, 0, FLAGS, .unit = "luma_mode"},
57 {"interleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE}, 0, 0, FLAGS, .unit = "luma_mode"},
58 {"i", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE}, 0, 0, FLAGS, .unit = "luma_mode"},
59 {"deinterleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, .unit = "luma_mode"},
60 {"d", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, .unit = "luma_mode"},
61 {"chroma_mode", "select chroma mode", OFFSET(chroma_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, .unit = "chroma_mode"},
62 {"c", "select chroma mode", OFFSET(chroma_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, .unit = "chroma_mode"},
63 {"none", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_NONE}, 0, 0, FLAGS, .unit = "chroma_mode"},
64 {"interleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE}, 0, 0, FLAGS, .unit = "chroma_mode"},
65 {"i", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE}, 0, 0, FLAGS, .unit = "chroma_mode"},
66 {"deinterleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, .unit = "chroma_mode"},
67 {"d", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, .unit = "chroma_mode"},
68 {"alpha_mode", "select alpha mode", OFFSET(alpha_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, .unit = "alpha_mode"},
69 {"a", "select alpha mode", OFFSET(alpha_mode), AV_OPT_TYPE_INT, {.i64=MODE_NONE}, MODE_NONE, MODE_DEINTERLEAVE, FLAGS, .unit = "alpha_mode"},
70 {"none", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_NONE}, 0, 0, FLAGS, .unit = "alpha_mode"},
71 {"interleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE}, 0, 0, FLAGS, .unit = "alpha_mode"},
72 {"i", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_INTERLEAVE}, 0, 0, FLAGS, .unit = "alpha_mode"},
73 {"deinterleave", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, .unit = "alpha_mode"},
74 {"d", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MODE_DEINTERLEAVE}, 0, 0, FLAGS, .unit = "alpha_mode"},
75 {"luma_swap", "swap luma fields", OFFSET(luma_swap), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
76 {"ls", "swap luma fields", OFFSET(luma_swap), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
77 {"chroma_swap", "swap chroma fields", OFFSET(chroma_swap), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
78 {"cs", "swap chroma fields", OFFSET(chroma_swap), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
79 {"alpha_swap", "swap alpha fields", OFFSET(alpha_swap), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
80 {"as", "swap alpha fields", OFFSET(alpha_swap), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
81 {NULL}
82};
83
85
87 AVFilterFormatsConfig **cfg_in,
88 AVFilterFormatsConfig **cfg_out)
89{
91
92 return ff_set_common_formats2(ctx, cfg_in, cfg_out,
93 ff_formats_pixdesc_filter(0, reject_flags));
94}
95
96static int config_input(AVFilterLink *inlink)
97{
98 IlContext *s = inlink->dst->priv;
100 int ret;
101
102 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
103
104 s->has_alpha = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
105 if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
106 return ret;
107
108 s->chroma_height = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
109
110 return 0;
111}
112
113static void interleave(uint8_t *dst, uint8_t *src, int w, int h,
114 int dst_linesize, int src_linesize,
115 enum FilterMode mode, int swap)
116{
117 const int a = swap;
118 const int b = 1 - a;
119 const int m = h >> 1;
120 int y;
121
122 switch (mode) {
124 for (y = 0; y < m; y++) {
125 memcpy(dst + dst_linesize * y , src + src_linesize * (y * 2 + a), w);
126 memcpy(dst + dst_linesize * (y + m), src + src_linesize * (y * 2 + b), w);
127 }
128 break;
129 case MODE_NONE:
130 for (y = 0; y < m; y++) {
131 memcpy(dst + dst_linesize * y * 2 , src + src_linesize * (y * 2 + a), w);
132 memcpy(dst + dst_linesize * (y * 2 + 1), src + src_linesize * (y * 2 + b), w);
133 }
134 break;
135 case MODE_INTERLEAVE:
136 for (y = 0; y < m; y++) {
137 memcpy(dst + dst_linesize * (y * 2 + a), src + src_linesize * y , w);
138 memcpy(dst + dst_linesize * (y * 2 + b), src + src_linesize * (y + m), w);
139 }
140 break;
141 }
142}
143
144static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
145{
146 IlContext *s = inlink->dst->priv;
147 AVFilterLink *outlink = inlink->dst->outputs[0];
148 AVFrame *out;
149 int comp;
150
151 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
152 if (!out) {
153 av_frame_free(&inpicref);
154 return AVERROR(ENOMEM);
155 }
156 av_frame_copy_props(out, inpicref);
157
158 interleave(out->data[0], inpicref->data[0],
159 s->linesize[0], inlink->h,
160 out->linesize[0], inpicref->linesize[0],
161 s->luma_mode, s->luma_swap);
162
163 for (comp = 1; comp < (s->nb_planes - s->has_alpha); comp++) {
164 interleave(out->data[comp], inpicref->data[comp],
165 s->linesize[comp], s->chroma_height,
166 out->linesize[comp], inpicref->linesize[comp],
167 s->chroma_mode, s->chroma_swap);
168 }
169
170 if (s->has_alpha) {
171 comp = s->nb_planes - 1;
172 interleave(out->data[comp], inpicref->data[comp],
173 s->linesize[comp], inlink->h,
174 out->linesize[comp], inpicref->linesize[comp],
175 s->alpha_mode, s->alpha_swap);
176 }
177
178 av_frame_free(&inpicref);
179 return ff_filter_frame(outlink, out);
180}
181
182static const AVFilterPad inputs[] = {
183 {
184 .name = "default",
185 .type = AVMEDIA_TYPE_VIDEO,
186 .filter_frame = filter_frame,
187 .config_props = config_input,
188 },
189};
190
192 .p.name = "il",
193 .p.description = NULL_IF_CONFIG_SMALL("Deinterleave or interleave fields."),
194 .p.priv_class = &il_class,
196 .priv_size = sizeof(IlContext),
200 .process_command = ff_filter_process_command,
201};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition aeval.c:246
static const AVFilterPad inputs[]
Definition af_aap.c:299
static int config_input(AVFilterLink *inlink)
const FFFilter ff_vf_il
Definition vf_il.c:191
static FILE * out
static AVFormatContext * ctx
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
Definition avfilter.c:906
Main libavfilter public API header.
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
#define AV_CEIL_RSHIFT(a, b)
Definition common.h:60
#define NULL
Definition coverity.c:32
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
Definition eamad.c:79
@ MODE_NONE
Definition f_perms.c:31
int ff_set_common_formats2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *formats)
Definition formats.c:1137
AVFilterFormats * ff_formats_pixdesc_filter(unsigned want, unsigned rej)
Construct a formats list containing all pixel formats with certain properties.
Definition formats.c:620
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
#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
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
Fill plane linesizes for an image with pixel format pix_fmt and width width.
Definition imgutils.c:89
int a
misc image utilities
#define b
Definition input.c:43
#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 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
uint8_t w
Definition llvidencdsp.c:39
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_ALPHA
The pixel format has an alpha channel.
Definition pixdesc.h:147
#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
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
void * priv
private data for use by the filter
Definition avfilter.h:288
AVFilterLink ** outputs
array of pointers to output links
Definition avfilter.h:285
Lists of formats / etc.
Definition avfilter.h:120
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
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:517
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
int alpha_mode
FilterMode.
Definition vf_il.c:43
int linesize[4]
Definition vf_il.c:46
int nb_planes
Definition vf_il.c:45
int alpha_swap
Definition vf_il.c:44
int has_alpha
Definition vf_il.c:47
int chroma_swap
Definition vf_il.c:44
int luma_swap
Definition vf_il.c:44
int chroma_height
Definition vf_il.c:46
int chroma_mode
Definition vf_il.c:43
int luma_mode
Definition vf_il.c:43
Definition swscale.c:71
#define src
Definition vp8dsp.c:248
@ MODE_INTERLEAVE
Definition vf_il.c:37
@ MODE_DEINTERLEAVE
Definition vf_il.c:38
static const AVOption il_options[]
Definition vf_il.c:53
static int config_input(AVFilterLink *inlink)
Definition vf_il.c:96
static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
Definition vf_il.c:144
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition vf_il.c:86
#define OFFSET(x)
Definition vf_il.c:50
static void interleave(uint8_t *dst, uint8_t *src, int w, int h, int dst_linesize, int src_linesize, enum FilterMode mode, int swap)
Definition vf_il.c:113
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
FilterMode
Definition vp9.h:64