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 {
31 PROGRESSIVE,
32 TOP_FIRST,
33 BOTTOM_FIRST,
34 TOP_FIRST_ANALYZE,
35 BOTTOM_FIRST_ANALYZE,
36 ANALYZE,
37 FULL_ANALYZE,
38 AUTO,
39 AUTO_ANALYZE
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
94 AVFILTER_DEFINE_CLASS(phase);
95
query_formats(AVFilterContext * ctx)96 static int query_formats(AVFilterContext *ctx)
97 {
98 static const enum AVPixelFormat pix_fmts[] = {
99 AV_PIX_FMT_GRAY8,
100 AV_PIX_FMT_GRAY9,
101 AV_PIX_FMT_GRAY10,
102 AV_PIX_FMT_GRAY12,
103 AV_PIX_FMT_GRAY14,
104 AV_PIX_FMT_GRAY16,
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,
110 AV_PIX_FMT_YUVJ411P,
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,
125 AV_PIX_FMT_NONE
126 };
127
128 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
129 if (!fmts_list)
130 return AVERROR(ENOMEM);
131 return ff_set_common_formats(ctx, fmts_list);
132 }
133
config_input(AVFilterLink * inlink)134 static int config_input(AVFilterLink *inlink)
135 {
136 PhaseContext *s = inlink->dst->priv;
137 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
138 int ret;
139
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);
148 };
149
150 if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
151 return ret;
152
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;
155
156 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
157
158 return 0;
159 }
160
filter_frame(AVFilterLink * inlink,AVFrame * in)161 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
162 {
163 AVFilterContext *ctx = inlink->dst;
164 AVFilterLink *outlink = ctx->outputs[0];
165 PhaseContext *s = ctx->priv;
166 enum PhaseMode mode;
167 int plane, top, y;
168 AVFrame *out;
169
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
174 * delay. */
175 s->frame = av_frame_clone(in);
176 return ff_filter_frame(outlink, in);
177 }
178
179 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
180 if (!out) {
181 av_frame_free(&in);
182 return AVERROR(ENOMEM);
183 }
184 av_frame_copy_props(out, in);
185
186 if (!s->frame) {
187 s->frame = in;
188 mode = PROGRESSIVE;
189 } else {
190 mode = s->analyze_plane(ctx, s->mode, s->frame, in);
191 }
192
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];
197
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]);
200
201 buf += s->frame->linesize[plane];
202 from += in->linesize[plane];
203 to += out->linesize[plane];
204 }
205 }
206
207 if (in != s->frame)
208 av_frame_free(&s->frame);
209 s->frame = in;
210 return ff_filter_frame(outlink, out);
211 }
212
uninit(AVFilterContext * ctx)213 static av_cold void uninit(AVFilterContext *ctx)
214 {
215 PhaseContext *s = ctx->priv;
216
217 av_frame_free(&s->frame);
218 }
219
220 static const AVFilterPad phase_inputs[] = {
221 {
222 .name = "default",
223 .type = AVMEDIA_TYPE_VIDEO,
224 .filter_frame = filter_frame,
225 .config_props = config_input,
226 },
227 { NULL }
228 };
229
230 static const AVFilterPad phase_outputs[] = {
231 {
232 .name = "default",
233 .type = AVMEDIA_TYPE_VIDEO,
234 },
235 { NULL }
236 };
237
238 AVFilter ff_vf_phase = {
239 .name = "phase",
240 .description = NULL_IF_CONFIG_SMALL("Phase shift fields."),
241 .priv_size = sizeof(PhaseContext),
242 .priv_class = &phase_class,
243 .uninit = uninit,
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,
249 };
250