FFmpeg
Loading...
Searching...
No Matches
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 "filters.h"
27#include "video.h"
28
40
41#define DEPTH 8
42#include "phase_template.c"
43
44#undef DEPTH
45#define DEPTH 9
46#include "phase_template.c"
47
48#undef DEPTH
49#define DEPTH 10
50#include "phase_template.c"
51
52#undef DEPTH
53#define DEPTH 12
54#include "phase_template.c"
55
56#undef DEPTH
57#define DEPTH 14
58#include "phase_template.c"
59
60#undef DEPTH
61#define DEPTH 16
62#include "phase_template.c"
63
64typedef struct PhaseContext {
65 const AVClass *class;
66 int mode; ///<PhaseMode
67 AVFrame *frame; /* previous frame */
70 int linesize[4];
71
72 enum PhaseMode (*analyze_plane)(void *ctx, enum PhaseMode mode, AVFrame *old, AVFrame *new);
74
75#define OFFSET(x) offsetof(PhaseContext, x)
76#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
77#define CONST(name, help, val, u) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, .unit = u }
78
79static const AVOption phase_options[] = {
80 { "mode", "set phase mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=AUTO_ANALYZE}, PROGRESSIVE, AUTO_ANALYZE, FLAGS, .unit = "mode" },
81 CONST("p", "progressive", PROGRESSIVE, "mode"),
82 CONST("t", "top first", TOP_FIRST, "mode"),
83 CONST("b", "bottom first", BOTTOM_FIRST, "mode"),
84 CONST("T", "top first analyze", TOP_FIRST_ANALYZE, "mode"),
85 CONST("B", "bottom first analyze", BOTTOM_FIRST_ANALYZE, "mode"),
86 CONST("u", "analyze", ANALYZE, "mode"),
87 CONST("U", "full analyze", FULL_ANALYZE, "mode"),
88 CONST("a", "auto", AUTO, "mode"),
89 CONST("A", "auto analyze", AUTO_ANALYZE, "mode"),
90 { NULL }
91};
92
94
124
125static int config_input(AVFilterLink *inlink)
126{
127 PhaseContext *s = inlink->dst->priv;
129 int ret;
130
131 switch (desc->comp[0].depth) {
132 case 8: s->analyze_plane = analyze_plane_8; break;
133 case 9: s->analyze_plane = analyze_plane_9; break;
134 case 10: s->analyze_plane = analyze_plane_10; break;
135 case 12: s->analyze_plane = analyze_plane_12; break;
136 case 14: s->analyze_plane = analyze_plane_14; break;
137 case 16: s->analyze_plane = analyze_plane_16; break;
138 default: av_assert0(0);
139 };
140
141 if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
142 return ret;
143
144 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
145 s->planeheight[0] = s->planeheight[3] = inlink->h;
146
147 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
148
149 return 0;
150}
151
152static int filter_frame(AVFilterLink *inlink, AVFrame *in)
153{
154 AVFilterContext *ctx = inlink->dst;
155 AVFilterLink *outlink = ctx->outputs[0];
156 PhaseContext *s = ctx->priv;
157 enum PhaseMode mode;
158 int plane, top, y;
159 AVFrame *out;
160
161 if (ctx->is_disabled) {
162 av_frame_free(&s->frame);
163 /* we keep a reference to the previous frame so the filter can start
164 * being useful as soon as it's not disabled, avoiding the 1-frame
165 * delay. */
166 s->frame = av_frame_clone(in);
167 return ff_filter_frame(outlink, in);
168 }
169
170 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
171 if (!out) {
172 av_frame_free(&in);
173 return AVERROR(ENOMEM);
174 }
176
177 if (!s->frame) {
178 s->frame = in;
180 } else {
181 mode = s->analyze_plane(ctx, s->mode, s->frame, in);
182 }
183
184 for (plane = 0; plane < s->nb_planes; plane++) {
185 const uint8_t *buf = s->frame->data[plane];
186 const uint8_t *from = in->data[plane];
187 uint8_t *to = out->data[plane];
188
189 for (y = 0, top = 1; y < s->planeheight[plane]; y++, top ^= 1) {
190 memcpy(to, mode == (top ? BOTTOM_FIRST : TOP_FIRST) ? buf : from, s->linesize[plane]);
191
192 buf += s->frame->linesize[plane];
193 from += in->linesize[plane];
194 to += out->linesize[plane];
195 }
196 }
197
198 if (in != s->frame)
199 av_frame_free(&s->frame);
200 s->frame = in;
201 return ff_filter_frame(outlink, out);
202}
203
205{
206 PhaseContext *s = ctx->priv;
207
208 av_frame_free(&s->frame);
209}
210
211static const AVFilterPad phase_inputs[] = {
212 {
213 .name = "default",
214 .type = AVMEDIA_TYPE_VIDEO,
215 .filter_frame = filter_frame,
216 .config_props = config_input,
217 },
218};
219
221 .p.name = "phase",
222 .p.description = NULL_IF_CONFIG_SMALL("Phase shift fields."),
223 .p.priv_class = &phase_class,
225 .priv_size = sizeof(PhaseContext),
226 .uninit = uninit,
230 .process_command = ff_filter_process_command,
231};
static int config_input(AVFilterLink *inlink)
const FFFilter ff_vf_phase
Definition vf_phase.c:220
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
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
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
#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:204
#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
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition frame.c:483
@ 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
misc image utilities
static av_cold void uninit(AVBitStreamFilterContext *ctx)
const char * from
Definition jacosubdec.c:64
const char * to
Definition webvttdec.c:36
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FILTER_PIXFMTS_ARRAY(array)
Definition filters.h:244
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static enum AVPixelFormat pix_fmts[]
Definition libkvazaar.c:296
const char * desc
Definition libsvtav1.c:83
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_GBRAP12
Definition pixfmt.h:569
#define AV_PIX_FMT_YUV420P16
Definition pixfmt.h:556
#define AV_PIX_FMT_YUV444P12
Definition pixfmt.h:552
#define AV_PIX_FMT_YUV444P9
Definition pixfmt.h:544
#define AV_PIX_FMT_YUV420P10
Definition pixfmt.h:545
#define AV_PIX_FMT_YUV440P12
Definition pixfmt.h:551
#define AV_PIX_FMT_GRAY9
Definition pixfmt.h:524
#define AV_PIX_FMT_GBRAP16
Definition pixfmt.h:571
#define AV_PIX_FMT_GBRP9
Definition pixfmt.h:563
#define AV_PIX_FMT_YUV422P9
Definition pixfmt.h:543
#define AV_PIX_FMT_YUVA444P10
Definition pixfmt.h:598
#define AV_PIX_FMT_YUVA420P16
Definition pixfmt.h:601
#define AV_PIX_FMT_YUV420P12
Definition pixfmt.h:549
#define AV_PIX_FMT_YUVA420P10
Definition pixfmt.h:596
#define AV_PIX_FMT_YUVA422P9
Definition pixfmt.h:594
#define AV_PIX_FMT_YUV422P12
Definition pixfmt.h:550
#define AV_PIX_FMT_GBRP10
Definition pixfmt.h:564
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:546
#define AV_PIX_FMT_GRAY12
Definition pixfmt.h:526
#define AV_PIX_FMT_GBRP12
Definition pixfmt.h:565
#define AV_PIX_FMT_YUV420P9
Definition pixfmt.h:542
#define AV_PIX_FMT_YUVA420P9
Definition pixfmt.h:593
#define AV_PIX_FMT_YUVA422P10
Definition pixfmt.h:597
#define AV_PIX_FMT_YUV420P14
Definition pixfmt.h:553
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition pixfmt.h:106
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition pixfmt.h:108
@ 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:107
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition pixfmt.h:79
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition pixfmt.h:80
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition pixfmt.h:174
@ 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:283
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition pixfmt.h:212
@ 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:86
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition pixfmt.h:173
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition pixfmt.h:165
@ 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:87
@ 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:85
#define AV_PIX_FMT_YUVA422P12
Definition pixfmt.h:599
#define AV_PIX_FMT_YUV422P14
Definition pixfmt.h:554
#define AV_PIX_FMT_GRAY10
Definition pixfmt.h:525
#define AV_PIX_FMT_GRAY14
Definition pixfmt.h:527
#define AV_PIX_FMT_YUV422P16
Definition pixfmt.h:557
#define AV_PIX_FMT_YUV440P10
Definition pixfmt.h:547
#define AV_PIX_FMT_GRAY16
Definition pixfmt.h:528
#define AV_PIX_FMT_GBRAP10
Definition pixfmt.h:568
#define AV_PIX_FMT_YUVA444P16
Definition pixfmt.h:603
#define AV_PIX_FMT_YUVA422P16
Definition pixfmt.h:602
#define AV_PIX_FMT_GBRP16
Definition pixfmt.h:567
#define AV_PIX_FMT_YUV444P14
Definition pixfmt.h:555
#define AV_PIX_FMT_YUVA444P9
Definition pixfmt.h:595
#define AV_PIX_FMT_GBRP14
Definition pixfmt.h:566
#define AV_PIX_FMT_YUVA444P12
Definition pixfmt.h:600
#define AV_PIX_FMT_YUV444P16
Definition pixfmt.h:558
#define AV_PIX_FMT_YUV444P10
Definition pixfmt.h:548
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
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 planeheight[4]
Definition vf_phase.c:69
enum PhaseMode(* analyze_plane)(void *ctx, enum PhaseMode mode, AVFrame *old, AVFrame *new)
Definition vf_phase.c:72
int mode
PhaseMode.
Definition vf_phase.c:66
int nb_planes
Definition vf_phase.c:68
AVFrame * frame
Definition vf_phase.c:67
int linesize[4]
Definition vf_phase.c:70
Definition swscale.c:71
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
@ PROGRESSIVE
in the bitstream is reported as 00b
Definition vc1.h:152
static int config_input(AVFilterLink *inlink)
Definition vf_phase.c:125
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition vf_phase.c:152
#define CONST(name, help, val, u)
Definition vf_phase.c:77
static av_cold void uninit(AVFilterContext *ctx)
Definition vf_phase.c:204
static const AVOption phase_options[]
Definition vf_phase.c:79
#define OFFSET(x)
Definition vf_phase.c:75
PhaseMode
Definition vf_phase.c:29
@ TOP_FIRST_ANALYZE
Definition vf_phase.c:33
@ TOP_FIRST
Definition vf_phase.c:31
@ ANALYZE
Definition vf_phase.c:35
@ BOTTOM_FIRST_ANALYZE
Definition vf_phase.c:34
@ BOTTOM_FIRST
Definition vf_phase.c:32
@ AUTO
Definition vf_phase.c:37
@ FULL_ANALYZE
Definition vf_phase.c:36
@ AUTO_ANALYZE
Definition vf_phase.c:38
static const AVFilterPad phase_inputs[]
Definition vf_phase.c:211
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