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"
42 typedef struct PhaseContext {
45 AVFrame *frame; /* previous frame */
51 #define OFFSET(x) offsetof(PhaseContext, x)
52 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
53 #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, unit }
55 static const AVOption phase_options[] = {
56 { "mode", "set phase mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=AUTO_ANALYZE}, PROGRESSIVE, AUTO_ANALYZE, FLAGS, "mode" },
57 CONST("p", "progressive", PROGRESSIVE, "mode"),
58 CONST("t", "top first", TOP_FIRST, "mode"),
59 CONST("b", "bottom first", BOTTOM_FIRST, "mode"),
60 CONST("T", "top first analyze", TOP_FIRST_ANALYZE, "mode"),
61 CONST("B", "bottom first analyze", BOTTOM_FIRST_ANALYZE, "mode"),
62 CONST("u", "analyze", ANALYZE, "mode"),
63 CONST("U", "full analyze", FULL_ANALYZE, "mode"),
64 CONST("a", "auto", AUTO, "mode"),
65 CONST("A", "auto analyze", AUTO_ANALYZE, "mode"),
69 AVFILTER_DEFINE_CLASS(phase);
71 static int query_formats(AVFilterContext *ctx)
73 static const enum AVPixelFormat pix_fmts[] = {
74 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
75 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ422P,AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P,
76 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
77 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
80 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
84 static int config_input(AVFilterLink *inlink)
86 PhaseContext *s = inlink->dst->priv;
87 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
90 if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
93 s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
94 s->planeheight[0] = s->planeheight[3] = inlink->h;
96 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
102 * This macro interpolates the value of both fields at a point halfway
103 * between lines and takes the squared difference. In field resolution
104 * the point is a quarter pixel below a line in one field and a quarter
105 * pixel above a line in other.
107 * (The result is actually multiplied by 25)
109 #define DIFF(a, as, b, bs) (t = ((*a - b[bs]) << 2) + a[as << 1] - b[-bs], t * t)
112 * Find which field combination has the smallest average squared difference
113 * between the fields.
115 static enum PhaseMode analyze_plane(void *ctx, enum PhaseMode mode, AVFrame *old, AVFrame *new)
117 double bdiff, tdiff, pdiff, scale;
118 const int ns = new->linesize[0];
119 const int os = old->linesize[0];
120 uint8_t *nptr = new->data[0];
121 uint8_t *optr = old->data[0];
122 const int h = new->height;
123 const int w = new->width;
124 int bdif, tdif, pdif;
129 mode = new->interlaced_frame ? new->top_field_first ?
130 TOP_FIRST : BOTTOM_FIRST : PROGRESSIVE;
131 } else if (mode == AUTO_ANALYZE) {
132 mode = new->interlaced_frame ? new->top_field_first ?
133 TOP_FIRST_ANALYZE : BOTTOM_FIRST_ANALYZE : FULL_ANALYZE;
136 if (mode <= BOTTOM_FIRST) {
137 bdiff = pdiff = tdiff = 65536.0;
139 bdiff = pdiff = tdiff = 0.0;
141 for (end = nptr + (h - 2) * ns, nptr += ns, optr += os, top = 0;
142 nptr < end; nptr += ns - w, optr += os - w, top ^= 1) {
143 pdif = tdif = bdif = 0;
146 case TOP_FIRST_ANALYZE:
148 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
149 pdif += DIFF(nptr, ns, nptr, ns);
150 tdif += DIFF(nptr, ns, optr, os);
153 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
154 pdif += DIFF(nptr, ns, nptr, ns);
155 tdif += DIFF(optr, os, nptr, ns);
159 case BOTTOM_FIRST_ANALYZE:
161 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
162 pdif += DIFF(nptr, ns, nptr, ns);
163 bdif += DIFF(optr, os, nptr, ns);
166 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
167 pdif += DIFF(nptr, ns, nptr, ns);
168 bdif += DIFF(nptr, ns, optr, os);
174 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
175 tdif += DIFF(nptr, ns, optr, os);
176 bdif += DIFF(optr, os, nptr, ns);
179 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
180 bdif += DIFF(nptr, ns, optr, os);
181 tdif += DIFF(optr, os, nptr, ns);
187 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
188 pdif += DIFF(nptr, ns, nptr, ns);
189 tdif += DIFF(nptr, ns, optr, os);
190 bdif += DIFF(optr, os, nptr, ns);
193 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
194 pdif += DIFF(nptr, ns, nptr, ns);
195 bdif += DIFF(nptr, ns, optr, os);
196 tdif += DIFF(optr, os, nptr, ns);
204 pdiff += (double)pdif;
205 tdiff += (double)tdif;
206 bdiff += (double)bdif;
209 scale = 1.0 / (w * (h - 3)) / 25.0;
214 if (mode == TOP_FIRST_ANALYZE) {
216 } else if (mode == BOTTOM_FIRST_ANALYZE) {
218 } else if (mode == ANALYZE) {
222 if (bdiff < pdiff && bdiff < tdiff) {
224 } else if (tdiff < pdiff && tdiff < bdiff) {
231 av_log(ctx, AV_LOG_DEBUG, "mode=%c tdiff=%f bdiff=%f pdiff=%f\n",
232 mode == BOTTOM_FIRST ? 'b' : mode == TOP_FIRST ? 't' : 'p',
233 tdiff, bdiff, pdiff);
237 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
239 AVFilterContext *ctx = inlink->dst;
240 AVFilterLink *outlink = ctx->outputs[0];
241 PhaseContext *s = ctx->priv;
246 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
249 return AVERROR(ENOMEM);
251 av_frame_copy_props(out, in);
257 mode = analyze_plane(ctx, s->mode, s->frame, in);
260 for (plane = 0; plane < s->nb_planes; plane++) {
261 uint8_t *buf = s->frame->data[plane];
262 uint8_t *from = in->data[plane];
263 uint8_t *to = out->data[plane];
265 for (y = 0, top = 1; y < s->planeheight[plane]; y++, top ^= 1) {
266 memcpy(to, mode == (top ? BOTTOM_FIRST : TOP_FIRST) ? buf : from, s->linesize[plane]);
268 buf += s->frame->linesize[plane];
269 from += in->linesize[plane];
270 to += out->linesize[plane];
275 av_frame_free(&s->frame);
277 return ff_filter_frame(outlink, out);
280 static av_cold void uninit(AVFilterContext *ctx)
282 PhaseContext *s = ctx->priv;
284 av_frame_free(&s->frame);
287 static const AVFilterPad phase_inputs[] = {
290 .type = AVMEDIA_TYPE_VIDEO,
291 .filter_frame = filter_frame,
292 .config_props = config_input,
297 static const AVFilterPad phase_outputs[] = {
300 .type = AVMEDIA_TYPE_VIDEO,
305 AVFilter ff_vf_phase = {
307 .description = NULL_IF_CONFIG_SMALL("Phase shift fields."),
308 .priv_size = sizeof(PhaseContext),
309 .priv_class = &phase_class,
311 .query_formats = query_formats,
312 .inputs = phase_inputs,
313 .outputs = phase_outputs,