FFmpeg
vf_phase.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004 Ville Saari
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "libavutil/avassert.h"
22 #include "libavutil/imgutils.h"
23 #include "libavutil/pixdesc.h"
24 #include "libavutil/opt.h"
25 #include "avfilter.h"
26 #include "formats.h"
27 #include "internal.h"
28 #include "video.h"
29 
30 enum PhaseMode {
40 };
41 
42 #define DEPTH 8
43 #include "phase_template.c"
44 
45 #undef DEPTH
46 #define DEPTH 9
47 #include "phase_template.c"
48 
49 #undef DEPTH
50 #define DEPTH 10
51 #include "phase_template.c"
52 
53 #undef DEPTH
54 #define DEPTH 12
55 #include "phase_template.c"
56 
57 #undef DEPTH
58 #define DEPTH 14
59 #include "phase_template.c"
60 
61 #undef DEPTH
62 #define DEPTH 16
63 #include "phase_template.c"
64 
65 typedef struct PhaseContext {
66  const AVClass *class;
67  int mode; ///<PhaseMode
68  AVFrame *frame; /* previous frame */
69  int nb_planes;
70  int planeheight[4];
71  int linesize[4];
72 
73  enum PhaseMode (*analyze_plane)(void *ctx, enum PhaseMode mode, AVFrame *old, AVFrame *new);
74 } PhaseContext;
75 
76 #define OFFSET(x) offsetof(PhaseContext, x)
77 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
78 #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, unit }
79 
80 static const AVOption phase_options[] = {
81  { "mode", "set phase mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=AUTO_ANALYZE}, PROGRESSIVE, AUTO_ANALYZE, FLAGS, "mode" },
82  CONST("p", "progressive", PROGRESSIVE, "mode"),
83  CONST("t", "top first", TOP_FIRST, "mode"),
84  CONST("b", "bottom first", BOTTOM_FIRST, "mode"),
85  CONST("T", "top first analyze", TOP_FIRST_ANALYZE, "mode"),
86  CONST("B", "bottom first analyze", BOTTOM_FIRST_ANALYZE, "mode"),
87  CONST("u", "analyze", ANALYZE, "mode"),
88  CONST("U", "full analyze", FULL_ANALYZE, "mode"),
89  CONST("a", "auto", AUTO, "mode"),
90  CONST("A", "auto analyze", AUTO_ANALYZE, "mode"),
91  { NULL }
92 };
93 
95 
96 static const enum AVPixelFormat pix_fmts[] = {
124 };
125 
127 {
128  PhaseContext *s = inlink->dst->priv;
130  int ret;
131 
132  switch (desc->comp[0].depth) {
133  case 8: s->analyze_plane = analyze_plane_8; break;
134  case 9: s->analyze_plane = analyze_plane_9; break;
135  case 10: s->analyze_plane = analyze_plane_10; break;
136  case 12: s->analyze_plane = analyze_plane_12; break;
137  case 14: s->analyze_plane = analyze_plane_14; break;
138  case 16: s->analyze_plane = analyze_plane_16; break;
139  default: av_assert0(0);
140  };
141 
142  if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
143  return ret;
144 
145  s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
146  s->planeheight[0] = s->planeheight[3] = inlink->h;
147 
148  s->nb_planes = av_pix_fmt_count_planes(inlink->format);
149 
150  return 0;
151 }
152 
154 {
155  AVFilterContext *ctx = inlink->dst;
156  AVFilterLink *outlink = ctx->outputs[0];
157  PhaseContext *s = ctx->priv;
158  enum PhaseMode mode;
159  int plane, top, y;
160  AVFrame *out;
161 
162  if (ctx->is_disabled) {
163  av_frame_free(&s->frame);
164  /* we keep a reference to the previous frame so the filter can start
165  * being useful as soon as it's not disabled, avoiding the 1-frame
166  * delay. */
167  s->frame = av_frame_clone(in);
168  return ff_filter_frame(outlink, in);
169  }
170 
171  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
172  if (!out) {
173  av_frame_free(&in);
174  return AVERROR(ENOMEM);
175  }
177 
178  if (!s->frame) {
179  s->frame = in;
180  mode = PROGRESSIVE;
181  } else {
182  mode = s->analyze_plane(ctx, s->mode, s->frame, in);
183  }
184 
185  for (plane = 0; plane < s->nb_planes; plane++) {
186  const uint8_t *buf = s->frame->data[plane];
187  const uint8_t *from = in->data[plane];
188  uint8_t *to = out->data[plane];
189 
190  for (y = 0, top = 1; y < s->planeheight[plane]; y++, top ^= 1) {
191  memcpy(to, mode == (top ? BOTTOM_FIRST : TOP_FIRST) ? buf : from, s->linesize[plane]);
192 
193  buf += s->frame->linesize[plane];
194  from += in->linesize[plane];
195  to += out->linesize[plane];
196  }
197  }
198 
199  if (in != s->frame)
200  av_frame_free(&s->frame);
201  s->frame = in;
202  return ff_filter_frame(outlink, out);
203 }
204 
206 {
207  PhaseContext *s = ctx->priv;
208 
209  av_frame_free(&s->frame);
210 }
211 
212 static const AVFilterPad phase_inputs[] = {
213  {
214  .name = "default",
215  .type = AVMEDIA_TYPE_VIDEO,
216  .filter_frame = filter_frame,
217  .config_props = config_input,
218  },
219 };
220 
221 static const AVFilterPad phase_outputs[] = {
222  {
223  .name = "default",
224  .type = AVMEDIA_TYPE_VIDEO,
225  },
226 };
227 
229  .name = "phase",
230  .description = NULL_IF_CONFIG_SMALL("Phase shift fields."),
231  .priv_size = sizeof(PhaseContext),
232  .priv_class = &phase_class,
233  .uninit = uninit,
238  .process_command = ff_filter_process_command,
239 };
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
AV_PIX_FMT_YUVA422P16
#define AV_PIX_FMT_YUVA422P16
Definition: pixfmt.h:502
AV_PIX_FMT_GBRAP16
#define AV_PIX_FMT_GBRAP16
Definition: pixfmt.h:481
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
PhaseContext::mode
int mode
PhaseMode.
Definition: vf_phase.c:67
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
BOTTOM_FIRST
@ BOTTOM_FIRST
Definition: vf_phase.c:33
opt.h
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_phase.c:153
out
FILE * out
Definition: movenc.c:54
CONST
#define CONST(name, help, val, unit)
Definition: vf_phase.c:78
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:969
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2888
FILTER_PIXFMTS_ARRAY
#define FILTER_PIXFMTS_ARRAY(array)
Definition: internal.h:174
phase_template.c
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:99
AV_PIX_FMT_YUVA422P9
#define AV_PIX_FMT_YUVA422P9
Definition: pixfmt.h:494
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:330
pixdesc.h
AV_PIX_FMT_YUVA420P16
#define AV_PIX_FMT_YUVA420P16
Definition: pixfmt.h:501
AV_PIX_FMT_YUVA420P10
#define AV_PIX_FMT_YUVA420P10
Definition: pixfmt.h:496
AVOption
AVOption.
Definition: opt.h:251
AV_PIX_FMT_YUV420P10
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:459
config_input
static int config_input(AVFilterLink *inlink)
Definition: vf_phase.c:126
AV_PIX_FMT_YUV440P
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:99
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:165
video.h
AV_PIX_FMT_YUVA422P10
#define AV_PIX_FMT_YUVA422P10
Definition: pixfmt.h:497
AV_PIX_FMT_GRAY9
#define AV_PIX_FMT_GRAY9
Definition: pixfmt.h:439
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:351
formats.h
av_pix_fmt_count_planes
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2928
AV_PIX_FMT_YUVA420P9
#define AV_PIX_FMT_YUVA420P9
Definition: pixfmt.h:493
AV_PIX_FMT_GBRP14
#define AV_PIX_FMT_GBRP14
Definition: pixfmt.h:477
AV_PIX_FMT_GBRAP
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:205
AV_PIX_FMT_GBRP10
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:475
AV_PIX_FMT_YUVA444P16
#define AV_PIX_FMT_YUVA444P16
Definition: pixfmt.h:503
AV_PIX_FMT_YUV422P9
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:457
AV_PIX_FMT_GRAY16
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:443
PhaseContext::nb_planes
int nb_planes
Definition: vf_phase.c:69
ANALYZE
@ ANALYZE
Definition: vf_phase.c:36
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:49
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(phase)
AV_PIX_FMT_YUV444P10
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:462
AV_PIX_FMT_YUVJ411P
@ AV_PIX_FMT_YUVJ411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
Definition: pixfmt.h:276
avassert.h
av_cold
#define av_cold
Definition: attributes.h:90
AV_PIX_FMT_YUV422P16
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:471
AUTO
@ AUTO
Definition: vf_phase.c:38
AV_PIX_FMT_YUVJ422P
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:79
AV_PIX_FMT_GBRAP10
#define AV_PIX_FMT_GBRAP10
Definition: pixfmt.h:479
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
TOP_FIRST
@ TOP_FIRST
Definition: vf_phase.c:32
phase_inputs
static const AVFilterPad phase_inputs[]
Definition: vf_phase.c:212
s
#define s(width, name)
Definition: cbs_vp9.c:256
AV_PIX_FMT_GBRAP12
#define AV_PIX_FMT_GBRAP12
Definition: pixfmt.h:480
AV_PIX_FMT_YUVA420P
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:101
AV_PIX_FMT_YUV444P16
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:472
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:50
from
const char * from
Definition: jacosubdec.c:66
to
const char * to
Definition: webvttdec.c:35
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
AV_PIX_FMT_YUVA444P12
#define AV_PIX_FMT_YUVA444P12
Definition: pixfmt.h:500
AV_PIX_FMT_YUV420P9
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:456
AV_PIX_FMT_YUV420P16
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:470
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AV_PIX_FMT_GRAY14
#define AV_PIX_FMT_GRAY14
Definition: pixfmt.h:442
av_frame_clone
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:465
TOP_FIRST_ANALYZE
@ TOP_FIRST_ANALYZE
Definition: vf_phase.c:34
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:194
AV_PIX_FMT_YUVJ444P
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:80
AV_PIX_FMT_GRAY10
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:440
AV_PIX_FMT_GBRP16
#define AV_PIX_FMT_GBRP16
Definition: pixfmt.h:478
FLAGS
#define FLAGS
Definition: vf_phase.c:77
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:594
PhaseContext::planeheight
int planeheight[4]
Definition: vf_phase.c:70
AV_PIX_FMT_YUVJ420P
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:78
AV_PIX_FMT_YUV440P10
#define AV_PIX_FMT_YUV440P10
Definition: pixfmt.h:461
AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:460
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
OFFSET
#define OFFSET(x)
Definition: vf_phase.c:76
AV_PIX_FMT_GBRP9
#define AV_PIX_FMT_GBRP9
Definition: pixfmt.h:474
phase_outputs
static const AVFilterPad phase_outputs[]
Definition: vf_phase.c:221
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:115
AV_PIX_FMT_YUV422P12
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:464
AV_PIX_FMT_YUV444P12
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:466
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:842
PhaseContext::analyze_plane
enum PhaseMode(* analyze_plane)(void *ctx, enum PhaseMode mode, AVFrame *old, AVFrame *new)
Definition: vf_phase.c:73
AV_PIX_FMT_YUVA444P
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:167
AV_PIX_FMT_YUVA444P10
#define AV_PIX_FMT_YUVA444P10
Definition: pixfmt.h:498
FULL_ANALYZE
@ FULL_ANALYZE
Definition: vf_phase.c:37
internal.h
AV_PIX_FMT_GBRP12
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:476
PhaseMode
PhaseMode
Definition: vf_phase.c:30
PhaseContext
Definition: vf_phase.c:65
AV_PIX_FMT_YUVJ440P
@ AV_PIX_FMT_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
Definition: pixfmt.h:100
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:55
AV_PIX_FMT_YUV444P9
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:458
AVFilter
Filter definition.
Definition: avfilter.h:161
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_phase.c:205
ret
ret
Definition: filter_design.txt:187
PROGRESSIVE
@ PROGRESSIVE
Definition: vf_phase.c:31
AV_PIX_FMT_YUVA444P9
#define AV_PIX_FMT_YUVA444P9
Definition: pixfmt.h:495
PhaseContext::linesize
int linesize[4]
Definition: vf_phase.c:71
AV_PIX_FMT_YUV420P12
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:463
AUTO_ANALYZE
@ AUTO_ANALYZE
Definition: vf_phase.c:39
AV_PIX_FMT_YUV422P14
#define AV_PIX_FMT_YUV422P14
Definition: pixfmt.h:468
mode
mode
Definition: ebur128.h:83
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AV_PIX_FMT_YUVA422P12
#define AV_PIX_FMT_YUVA422P12
Definition: pixfmt.h:499
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
avfilter.h
phase_options
static const AVOption phase_options[]
Definition: vf_phase.c:80
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
AVFilterContext
An instance of a filter.
Definition: avfilter.h:392
AV_PIX_FMT_GBRP
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:158
desc
const char * desc
Definition: libsvtav1.c:83
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
ff_vf_phase
const AVFilter ff_vf_phase
Definition: vf_phase.c:228
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: vf_phase.c:96
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:195
AV_PIX_FMT_YUV411P
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
Definition: avfilter.h:150
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:375
AV_PIX_FMT_YUV410P
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:72
AV_PIX_FMT_YUV440P12
#define AV_PIX_FMT_YUV440P12
Definition: pixfmt.h:465
AV_PIX_FMT_YUV444P14
#define AV_PIX_FMT_YUV444P14
Definition: pixfmt.h:469
AV_PIX_FMT_GRAY12
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:441
BOTTOM_FIRST_ANALYZE
@ BOTTOM_FIRST_ANALYZE
Definition: vf_phase.c:35
AV_PIX_FMT_YUVA422P
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:166
AV_PIX_FMT_YUV420P14
#define AV_PIX_FMT_YUV420P14
Definition: pixfmt.h:467
PhaseContext::frame
AVFrame * frame
Definition: vf_phase.c:68