2 * Copyright (c) 2004 Ville Saari
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/avassert.h"
22 #include "libavutil/imgutils.h"
23 #include "libavutil/pixdesc.h"
24 #include "libavutil/opt.h"
43 #include "phase_template.c"
47 #include "phase_template.c"
51 #include "phase_template.c"
55 #include "phase_template.c"
59 #include "phase_template.c"
63 #include "phase_template.c"
65 typedef struct PhaseContext {
67 int mode; ///<PhaseMode
68 AVFrame *frame; /* previous frame */
73 enum PhaseMode (*analyze_plane)(void *ctx, enum PhaseMode mode, AVFrame *old, AVFrame *new);
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 }
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"),
94 AVFILTER_DEFINE_CLASS(phase);
96 static int query_formats(AVFilterContext *ctx)
98 static const enum AVPixelFormat pix_fmts[] = {
105 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
106 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
107 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
108 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
109 AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
111 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
112 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
113 AV_PIX_FMT_YUV440P10,
114 AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
115 AV_PIX_FMT_YUV440P12,
116 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
117 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
118 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
119 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
120 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
121 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
122 AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
123 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
124 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
128 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
130 return AVERROR(ENOMEM);
131 return ff_set_common_formats(ctx, fmts_list);
134 static int config_input(AVFilterLink *inlink)
136 PhaseContext *s = inlink->dst->priv;
137 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
140 switch (desc->comp[0].depth) {
141 case 8: s->analyze_plane = analyze_plane_8; break;
142 case 9: s->analyze_plane = analyze_plane_9; break;
143 case 10: s->analyze_plane = analyze_plane_10; break;
144 case 12: s->analyze_plane = analyze_plane_12; break;
145 case 14: s->analyze_plane = analyze_plane_14; break;
146 case 16: s->analyze_plane = analyze_plane_16; break;
147 default: av_assert0(0);
150 if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
153 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
154 s->planeheight[0] = s->planeheight[3] = inlink->h;
156 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
161 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
163 AVFilterContext *ctx = inlink->dst;
164 AVFilterLink *outlink = ctx->outputs[0];
165 PhaseContext *s = ctx->priv;
170 if (ctx->is_disabled) {
171 av_frame_free(&s->frame);
172 /* we keep a reference to the previous frame so the filter can start
173 * being useful as soon as it's not disabled, avoiding the 1-frame
175 s->frame = av_frame_clone(in);
176 return ff_filter_frame(outlink, in);
179 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
182 return AVERROR(ENOMEM);
184 av_frame_copy_props(out, in);
190 mode = s->analyze_plane(ctx, s->mode, s->frame, in);
193 for (plane = 0; plane < s->nb_planes; plane++) {
194 const uint8_t *buf = s->frame->data[plane];
195 const uint8_t *from = in->data[plane];
196 uint8_t *to = out->data[plane];
198 for (y = 0, top = 1; y < s->planeheight[plane]; y++, top ^= 1) {
199 memcpy(to, mode == (top ? BOTTOM_FIRST : TOP_FIRST) ? buf : from, s->linesize[plane]);
201 buf += s->frame->linesize[plane];
202 from += in->linesize[plane];
203 to += out->linesize[plane];
208 av_frame_free(&s->frame);
210 return ff_filter_frame(outlink, out);
213 static av_cold void uninit(AVFilterContext *ctx)
215 PhaseContext *s = ctx->priv;
217 av_frame_free(&s->frame);
220 static const AVFilterPad phase_inputs[] = {
223 .type = AVMEDIA_TYPE_VIDEO,
224 .filter_frame = filter_frame,
225 .config_props = config_input,
230 static const AVFilterPad phase_outputs[] = {
233 .type = AVMEDIA_TYPE_VIDEO,
238 const AVFilter ff_vf_phase = {
240 .description = NULL_IF_CONFIG_SMALL("Phase shift fields."),
241 .priv_size = sizeof(PhaseContext),
242 .priv_class = &phase_class,
244 .query_formats = query_formats,
245 .inputs = phase_inputs,
246 .outputs = phase_outputs,
247 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
248 .process_command = ff_filter_process_command,