FFmpeg
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 "internal.h"
32 
33 enum FilterMode {
37 };
38 
39 typedef struct IlContext {
40  const AVClass *class;
41  int luma_mode, chroma_mode, alpha_mode; ///<FilterMode
43  int nb_planes;
45  int has_alpha;
46 } IlContext;
47 
48 #define OFFSET(x) offsetof(IlContext, x)
49 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
50 
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_BOOL, {.i64=0}, 0, 1, FLAGS},
74  {"ls", "swap luma fields", OFFSET(luma_swap), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
75  {"chroma_swap", "swap chroma fields", OFFSET(chroma_swap), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
76  {"cs", "swap chroma fields", OFFSET(chroma_swap), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
77  {"alpha_swap", "swap alpha fields", OFFSET(alpha_swap), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
78  {"as", "swap alpha fields", OFFSET(alpha_swap), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
79  {NULL}
80 };
81 
83 
85 {
86  int reject_flags = AV_PIX_FMT_FLAG_PAL | AV_PIX_FMT_FLAG_HWACCEL;
87 
88  return ff_set_common_formats(ctx, ff_formats_pixdesc_filter(0, reject_flags));
89 }
90 
92 {
93  IlContext *s = inlink->dst->priv;
95  int ret;
96 
97  s->nb_planes = av_pix_fmt_count_planes(inlink->format);
98 
99  s->has_alpha = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
100  if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
101  return ret;
102 
103  s->chroma_height = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
104 
105  return 0;
106 }
107 
108 static void interleave(uint8_t *dst, uint8_t *src, int w, int h,
109  int dst_linesize, int src_linesize,
110  enum FilterMode mode, int swap)
111 {
112  const int a = swap;
113  const int b = 1 - a;
114  const int m = h >> 1;
115  int y;
116 
117  switch (mode) {
118  case MODE_DEINTERLEAVE:
119  for (y = 0; y < m; y++) {
120  memcpy(dst + dst_linesize * y , src + src_linesize * (y * 2 + a), w);
121  memcpy(dst + dst_linesize * (y + m), src + src_linesize * (y * 2 + b), w);
122  }
123  break;
124  case MODE_NONE:
125  for (y = 0; y < m; y++) {
126  memcpy(dst + dst_linesize * y * 2 , src + src_linesize * (y * 2 + a), w);
127  memcpy(dst + dst_linesize * (y * 2 + 1), src + src_linesize * (y * 2 + b), w);
128  }
129  break;
130  case MODE_INTERLEAVE:
131  for (y = 0; y < m; y++) {
132  memcpy(dst + dst_linesize * (y * 2 + a), src + src_linesize * y , w);
133  memcpy(dst + dst_linesize * (y * 2 + b), src + src_linesize * (y + m), w);
134  }
135  break;
136  }
137 }
138 
139 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
140 {
141  IlContext *s = inlink->dst->priv;
142  AVFilterLink *outlink = inlink->dst->outputs[0];
143  AVFrame *out;
144  int comp;
145 
146  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
147  if (!out) {
148  av_frame_free(&inpicref);
149  return AVERROR(ENOMEM);
150  }
151  av_frame_copy_props(out, inpicref);
152 
153  interleave(out->data[0], inpicref->data[0],
154  s->linesize[0], inlink->h,
155  out->linesize[0], inpicref->linesize[0],
156  s->luma_mode, s->luma_swap);
157 
158  for (comp = 1; comp < (s->nb_planes - s->has_alpha); comp++) {
159  interleave(out->data[comp], inpicref->data[comp],
160  s->linesize[comp], s->chroma_height,
161  out->linesize[comp], inpicref->linesize[comp],
162  s->chroma_mode, s->chroma_swap);
163  }
164 
165  if (s->has_alpha) {
166  comp = s->nb_planes - 1;
167  interleave(out->data[comp], inpicref->data[comp],
168  s->linesize[comp], inlink->h,
169  out->linesize[comp], inpicref->linesize[comp],
170  s->alpha_mode, s->alpha_swap);
171  }
172 
173  av_frame_free(&inpicref);
174  return ff_filter_frame(outlink, out);
175 }
176 
177 static const AVFilterPad inputs[] = {
178  {
179  .name = "default",
180  .type = AVMEDIA_TYPE_VIDEO,
181  .filter_frame = filter_frame,
182  .config_props = config_input,
183  },
184 };
185 
186 static const AVFilterPad outputs[] = {
187  {
188  .name = "default",
189  .type = AVMEDIA_TYPE_VIDEO,
190  },
191 };
192 
194  .name = "il",
195  .description = NULL_IF_CONFIG_SMALL("Deinterleave or interleave fields."),
196  .priv_size = sizeof(IlContext),
200  .priv_class = &il_class,
202  .process_command = ff_filter_process_command,
203 };
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:101
IlContext::luma_mode
int luma_mode
Definition: vf_il.c:41
IlContext::linesize
int linesize[4]
Definition: vf_il.c:44
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
out
FILE * out
Definition: movenc.c:54
comp
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
Definition: eamad.c:86
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:999
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2662
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:111
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
pixdesc.h
w
uint8_t w
Definition: llviddspenc.c:38
AVOption
AVOption.
Definition: opt.h:251
b
#define b
Definition: input.c:34
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:167
MODE_INTERLEAVE
@ MODE_INTERLEAVE
Definition: vf_il.c:35
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:175
ff_vf_il
const AVFilter ff_vf_il
Definition: vf_il.c:193
IlContext::alpha_swap
int alpha_swap
Definition: vf_il.c:42
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:346
av_pix_fmt_count_planes
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2702
FilterMode
FilterMode
Definition: vp9.h:64
IlContext
Definition: vf_il.c:39
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(il)
AV_PIX_FMT_FLAG_HWACCEL
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:128
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
Definition: vf_il.c:139
inputs
static const AVFilterPad inputs[]
Definition: vf_il.c:177
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:49
ff_set_common_formats
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:749
IlContext::chroma_height
int chroma_height
Definition: vf_il.c:44
av_image_fill_linesizes
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
s
#define s(width, name)
Definition: cbs_vp9.c:256
MODE_DEINTERLEAVE
@ MODE_DEINTERLEAVE
Definition: vf_il.c:36
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:50
config_input
static int config_input(AVFilterLink *inlink)
Definition: vf_il.c:91
AV_PIX_FMT_FLAG_ALPHA
#define AV_PIX_FMT_FLAG_ALPHA
The pixel format has an alpha channel.
Definition: pixdesc.h:147
ctx
AVFormatContext * ctx
Definition: movenc.c:48
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:190
MODE_NONE
@ MODE_NONE
Definition: vf_il.c:34
IlContext::has_alpha
int has_alpha
Definition: vf_il.c:45
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:596
IlContext::alpha_mode
int alpha_mode
FilterMode.
Definition: vf_il.c:41
interleave
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:108
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
ff_filter_process_command
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:863
il_options
static const AVOption il_options[]
Definition: vf_il.c:51
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
internal.h
AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
#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:152
ff_formats_pixdesc_filter
AVFilterFormats * ff_formats_pixdesc_filter(unsigned want, unsigned rej)
Construct a formats list containing all pixel formats with certain properties.
Definition: formats.c:498
IlContext::chroma_swap
int chroma_swap
Definition: vf_il.c:42
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:55
AVFilter
Filter definition.
Definition: avfilter.h:171
FLAGS
#define FLAGS
Definition: vf_il.c:49
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: vf_il.c:84
ret
ret
Definition: filter_design.txt:187
mode
mode
Definition: ebur128.h:83
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
avfilter.h
AVFilterContext
An instance of a filter.
Definition: avfilter.h:408
desc
const char * desc
Definition: libsvtav1.c:83
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
IlContext::luma_swap
int luma_swap
Definition: vf_il.c:42
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
OFFSET
#define OFFSET(x)
Definition: vf_il.c:48
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:244
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:191
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
IlContext::chroma_mode
int chroma_mode
Definition: vf_il.c:41
imgutils.h
AVFrame::linesize
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:370
h
h
Definition: vp9dsp_template.c:2038
IlContext::nb_planes
int nb_planes
Definition: vf_il.c:43
AV_PIX_FMT_FLAG_PAL
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition: pixdesc.h:120
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
outputs
static const AVFilterPad outputs[]
Definition: vf_il.c:186