FFmpeg  4.4
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 {
87  int ret;
88 
92  if (ret < 0)
93  return ret;
95 }
96 
97 static int config_input(AVFilterLink *inlink)
98 {
99  IlContext *s = inlink->dst->priv;
101  int ret;
102 
103  s->nb_planes = av_pix_fmt_count_planes(inlink->format);
104 
105  s->has_alpha = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
106  if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
107  return ret;
108 
109  s->chroma_height = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
110 
111  return 0;
112 }
113 
114 static void interleave(uint8_t *dst, uint8_t *src, int w, int h,
115  int dst_linesize, int src_linesize,
116  enum FilterMode mode, int swap)
117 {
118  const int a = swap;
119  const int b = 1 - a;
120  const int m = h >> 1;
121  int y;
122 
123  switch (mode) {
124  case MODE_DEINTERLEAVE:
125  for (y = 0; y < m; y++) {
126  memcpy(dst + dst_linesize * y , src + src_linesize * (y * 2 + a), w);
127  memcpy(dst + dst_linesize * (y + m), src + src_linesize * (y * 2 + b), w);
128  }
129  break;
130  case MODE_NONE:
131  for (y = 0; y < m; y++) {
132  memcpy(dst + dst_linesize * y * 2 , src + src_linesize * (y * 2 + a), w);
133  memcpy(dst + dst_linesize * (y * 2 + 1), src + src_linesize * (y * 2 + b), w);
134  }
135  break;
136  case MODE_INTERLEAVE:
137  for (y = 0; y < m; y++) {
138  memcpy(dst + dst_linesize * (y * 2 + a), src + src_linesize * y , w);
139  memcpy(dst + dst_linesize * (y * 2 + b), src + src_linesize * (y + m), w);
140  }
141  break;
142  }
143 }
144 
145 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
146 {
147  IlContext *s = inlink->dst->priv;
148  AVFilterLink *outlink = inlink->dst->outputs[0];
149  AVFrame *out;
150  int comp;
151 
152  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
153  if (!out) {
154  av_frame_free(&inpicref);
155  return AVERROR(ENOMEM);
156  }
157  av_frame_copy_props(out, inpicref);
158 
159  interleave(out->data[0], inpicref->data[0],
160  s->linesize[0], inlink->h,
161  out->linesize[0], inpicref->linesize[0],
162  s->luma_mode, s->luma_swap);
163 
164  for (comp = 1; comp < (s->nb_planes - s->has_alpha); comp++) {
165  interleave(out->data[comp], inpicref->data[comp],
166  s->linesize[comp], s->chroma_height,
167  out->linesize[comp], inpicref->linesize[comp],
168  s->chroma_mode, s->chroma_swap);
169  }
170 
171  if (s->has_alpha) {
172  comp = s->nb_planes - 1;
173  interleave(out->data[comp], inpicref->data[comp],
174  s->linesize[comp], inlink->h,
175  out->linesize[comp], inpicref->linesize[comp],
176  s->alpha_mode, s->alpha_swap);
177  }
178 
179  av_frame_free(&inpicref);
180  return ff_filter_frame(outlink, out);
181 }
182 
183 static const AVFilterPad inputs[] = {
184  {
185  .name = "default",
186  .type = AVMEDIA_TYPE_VIDEO,
187  .filter_frame = filter_frame,
188  .config_props = config_input,
189  },
190  { NULL }
191 };
192 
193 static const AVFilterPad outputs[] = {
194  {
195  .name = "default",
196  .type = AVMEDIA_TYPE_VIDEO,
197  },
198  { NULL }
199 };
200 
202  .name = "il",
203  .description = NULL_IF_CONFIG_SMALL("Deinterleave or interleave fields."),
204  .priv_size = sizeof(IlContext),
206  .inputs = inputs,
207  .outputs = outputs,
208  .priv_class = &il_class,
211 };
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: af_acrusher.c:336
uint8_t
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1094
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:882
Main libavfilter public API header.
#define flags(name, subs,...)
Definition: cbs_av1.c:561
#define s(width, name)
Definition: cbs_vp9.c:257
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
#define NULL
Definition: coverity.c:32
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
Definition: eamad.c:85
mode
Use these values in ebur128_init (or'ed).
Definition: ebur128.h:83
int ff_formats_pixdesc_filter(AVFilterFormats **rfmts, unsigned want, unsigned rej)
Construct a formats list containing all pixel formats with certain properties.
Definition: formats.c:367
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:587
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
@ AV_OPT_TYPE_INT
Definition: opt.h:225
@ AV_OPT_TYPE_BOOL
Definition: opt.h:242
#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:126
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:658
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
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
misc image utilities
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:117
const char * desc
Definition: libsvtav1.c:79
uint8_t w
Definition: llviddspenc.c:39
AVOptions.
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2613
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
#define AV_PIX_FMT_FLAG_ALPHA
The pixel format has an alpha channel.
Definition: pixdesc.h:179
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:140
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition: pixdesc.h:132
formats
Definition: signature.h:48
Describe the class of an AVClass context structure.
Definition: log.h:67
An instance of a filter.
Definition: avfilter.h:341
void * priv
private data for use by the filter
Definition: avfilter.h:356
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:353
A list of supported formats for one end of a filter link.
Definition: formats.h:65
A filter pad used for either input or output.
Definition: internal.h:54
const char * name
Pad name.
Definition: internal.h:60
Filter definition.
Definition: avfilter.h:145
const char * name
Filter name.
Definition: avfilter.h:149
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
AVOption.
Definition: opt.h:248
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
int alpha_mode
FilterMode.
Definition: vf_il.c:41
int linesize[4]
Definition: vf_il.c:44
int nb_planes
Definition: vf_il.c:43
int alpha_swap
Definition: vf_il.c:42
int has_alpha
Definition: vf_il.c:45
int chroma_swap
Definition: vf_il.c:42
int luma_swap
Definition: vf_il.c:42
int chroma_height
Definition: vf_il.c:44
int chroma_mode
Definition: vf_il.c:41
int luma_mode
Definition: vf_il.c:41
#define src
Definition: vp8dsp.c:255
FILE * out
Definition: movenc.c:54
AVFormatContext * ctx
Definition: movenc.c:48
const char * b
Definition: vf_curves.c:118
AVFILTER_DEFINE_CLASS(il)
@ MODE_NONE
Definition: vf_il.c:34
@ MODE_INTERLEAVE
Definition: vf_il.c:35
@ MODE_DEINTERLEAVE
Definition: vf_il.c:36
static const AVOption il_options[]
Definition: vf_il.c:51
AVFilter ff_vf_il
Definition: vf_il.c:201
static int query_formats(AVFilterContext *ctx)
Definition: vf_il.c:84
static int config_input(AVFilterLink *inlink)
Definition: vf_il.c:97
static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
Definition: vf_il.c:145
#define FLAGS
Definition: vf_il.c:49
static const AVFilterPad inputs[]
Definition: vf_il.c:183
static const AVFilterPad outputs[]
Definition: vf_il.c:193
#define OFFSET(x)
Definition: vf_il.c:48
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:114
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:99
FilterMode
Definition: vp9.h:64