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 typedef struct PhaseContext {
43     const AVClass *class;
44     enum PhaseMode mode;
45     AVFrame *frame; /* previous frame */
46     int nb_planes;
47     int planeheight[4];
48     int linesize[4];
49 } PhaseContext;
50 
51 #define OFFSET(x) offsetof(PhaseContext, x)
52 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
53 
54 #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, unit }
55 
56 static const AVOption phase_options[] = {
57 	{ "mode", "set phase mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=AUTO_ANALYZE}, PROGRESSIVE, AUTO_ANALYZE, FLAGS, "mode" },
58 	CONST("p", "progressive",          PROGRESSIVE,          "mode"),
59     CONST("t", "top first",            TOP_FIRST,            "mode"),
60     CONST("b", "bottom first",         BOTTOM_FIRST,         "mode"),
61     CONST("T", "top first analyze",    TOP_FIRST_ANALYZE,    "mode"),
62     CONST("B", "bottom first analyze", BOTTOM_FIRST_ANALYZE, "mode"),
63     CONST("u", "analyze",              ANALYZE,              "mode"),
64     CONST("U", "full analyze",         FULL_ANALYZE,         "mode"),
65     CONST("a", "auto",                 AUTO,                 "mode"),
66     CONST("A", "auto analyze",         AUTO_ANALYZE,         "mode"),
67 	{ NULL }
68 };
69 
70 AVFILTER_DEFINE_CLASS(phase);
71 
query_formats(AVFilterContext * ctx)72 static int query_formats(AVFilterContext *ctx)
73 {
74     static const enum AVPixelFormat pix_fmts[] = {
75         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
76         AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ422P,AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P,
77         AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
78         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
79     };
80 
81     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
82     return 0;
83 }
84 
config_input(AVFilterLink * inlink)85 static int config_input(AVFilterLink *inlink)
86 {
87     PhaseContext *s = inlink->dst->priv;
88     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
89     int ret;
90 
91     if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
92         return ret;
93 
94     s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
95     s->planeheight[0] = s->planeheight[3] = inlink->h;
96 
97     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
98 
99     return 0;
100 }
101 
102 /*
103  * This macro interpolates the value of both fields at a point halfway
104  * between lines and takes the squared difference. In field resolution
105  * the point is a quarter pixel below a line in one field and a quarter
106  * pixel above a line in other.
107  *
108  * (The result is actually multiplied by 25)
109  */
110 #define DIFF(a, as, b, bs) (t = ((*a - b[bs]) << 2) + a[as << 1] - b[-bs], t * t)
111 
112 /*
113  * Find which field combination has the smallest average squared difference
114  * between the fields.
115  */
analyze_plane(void * ctx,enum PhaseMode mode,AVFrame * old,AVFrame * new)116 static enum PhaseMode analyze_plane(void *ctx, enum PhaseMode mode, AVFrame *old, AVFrame *new)
117 {
118     double bdiff, tdiff, pdiff, scale;
119     const int ns = new->linesize[0];
120     const int os = old->linesize[0];
121     const uint8_t *nptr = new->data[0];
122     const uint8_t *optr = old->data[0];
123     const int h = new->height;
124     const int w = new->width;
125     int bdif, tdif, pdif;
126 
127     if (mode == AUTO) {
128         mode = new->interlaced_frame ? new->top_field_first ?
129                TOP_FIRST : BOTTOM_FIRST : PROGRESSIVE;
130     } else if (mode == AUTO_ANALYZE) {
131         mode = new->interlaced_frame ? new->top_field_first ?
132                TOP_FIRST_ANALYZE : BOTTOM_FIRST_ANALYZE : FULL_ANALYZE;
133     }
134 
135     if (mode <= BOTTOM_FIRST) {
136         bdiff = pdiff = tdiff = 65536.0;
137     } else {
138         int top = 0, t;
139         const uint8_t *rend, *end = nptr + (h - 2) * ns;
140 
141         bdiff = pdiff = tdiff = 0.0;
142 
143         nptr += ns;
144         optr += os;
145         while (nptr < end) {
146             pdif = tdif = bdif = 0;
147 
148             switch (mode) {
149             case TOP_FIRST_ANALYZE:
150                 if (top) {
151                     for (rend = nptr + w; nptr < rend; nptr++, optr++) {
152                         pdif += DIFF(nptr, ns, nptr, ns);
153                         tdif += DIFF(nptr, ns, optr, os);
154                     }
155                 } else {
156                     for (rend = nptr + w; nptr < rend; nptr++, optr++) {
157                         pdif += DIFF(nptr, ns, nptr, ns);
158                         tdif += DIFF(optr, os, nptr, ns);
159                     }
160                 }
161                 break;
162             case BOTTOM_FIRST_ANALYZE:
163                 if (top) {
164                     for (rend = nptr + w; nptr < rend; nptr++, optr++) {
165                         pdif += DIFF(nptr, ns, nptr, ns);
166                         bdif += DIFF(optr, os, nptr, ns);
167                     }
168                 } else {
169                     for (rend = nptr + w; nptr < rend; nptr++, optr++) {
170                         pdif += DIFF(nptr, ns, nptr, ns);
171                         bdif += DIFF(nptr, ns, optr, os);
172                     }
173                 }
174                 break;
175             case ANALYZE:
176                 if (top) {
177                     for (rend = nptr + w; nptr < rend; nptr++, optr++) {
178                         tdif += DIFF(nptr, ns, optr, os);
179                         bdif += DIFF(optr, os, nptr, ns);
180                     }
181                 } else {
182                     for (rend = nptr + w; nptr < rend; nptr++, optr++) {
183                         bdif += DIFF(nptr, ns, optr, os);
184                         tdif += DIFF(optr, os, nptr, ns);
185                     }
186                 }
187                 break;
188             case FULL_ANALYZE:
189                 if (top) {
190                     for (rend = nptr + w; nptr < rend; nptr++, optr++) {
191                         pdif += DIFF(nptr, ns, nptr, ns);
192                         tdif += DIFF(nptr, ns, optr, os);
193                         bdif += DIFF(optr, os, nptr, ns);
194                     }
195                 } else {
196                     for (rend = nptr + w; nptr < rend; nptr++, optr++) {
197                         pdif += DIFF(nptr, ns, nptr, ns);
198                         bdif += DIFF(nptr, ns, optr, os);
199                         tdif += DIFF(optr, os, nptr, ns);
200                     }
201                 }
202                 break;
203             default:
204                 av_assert0(0);
205             }
206 
207             pdiff += (double)pdif;
208             tdiff += (double)tdif;
209             bdiff += (double)bdif;
210             nptr += ns - w;
211             optr += os - w;
212             top ^= 1;
213         }
214 
215         scale = 1.0 / (w * (h - 3)) / 25.0;
216         pdiff *= scale;
217         tdiff *= scale;
218         bdiff *= scale;
219 
220         if (mode == TOP_FIRST_ANALYZE) {
221             bdiff = 65536.0;
222         } else if (mode == BOTTOM_FIRST_ANALYZE) {
223             tdiff = 65536.0;
224         } else if (mode == ANALYZE) {
225             pdiff = 65536.0;
226         }
227 
228         if (bdiff < pdiff && bdiff < tdiff) {
229             mode = BOTTOM_FIRST;
230         } else if (tdiff < pdiff && tdiff < bdiff) {
231             mode = TOP_FIRST;
232         } else {
233             mode = PROGRESSIVE;
234         }
235     }
236 
237     av_log(ctx, AV_LOG_DEBUG, "mode=%c tdiff=%f bdiff=%f pdiff=%f\n",
238            mode == BOTTOM_FIRST ? 'b' : mode == TOP_FIRST ? 't' : 'p',
239            tdiff, bdiff, pdiff);
240     return mode;
241 }
242 
filter_frame(AVFilterLink * inlink,AVFrame * in)243 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
244 {
245     AVFilterContext *ctx = inlink->dst;
246     AVFilterLink *outlink = ctx->outputs[0];
247     PhaseContext *s = ctx->priv;
248     enum PhaseMode mode;
249     int plane, top, y;
250     AVFrame *out;
251 
252     if (ctx->is_disabled) {
253         av_frame_free(&s->frame);
254         /* we keep a reference to the previous frame so the filter can start
255          * being useful as soon as it's not disabled, avoiding the 1-frame
256          * delay. */
257         s->frame = av_frame_clone(in);
258         return ff_filter_frame(outlink, in);
259     }
260 
261     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
262     if (!out) {
263         av_frame_free(&in);
264         return AVERROR(ENOMEM);
265     }
266     av_frame_copy_props(out, in);
267 
268     if (!s->frame) {
269         s->frame = in;
270         mode = PROGRESSIVE;
271     } else {
272         mode = analyze_plane(ctx, s->mode, s->frame, in);
273     }
274 
275     for (plane = 0; plane < s->nb_planes; plane++) {
276         const uint8_t *buf = s->frame->data[plane];
277         const uint8_t *from = in->data[plane];
278         uint8_t *to = out->data[plane];
279 
280         for (y = 0, top = 1; y < s->planeheight[plane]; y++, top ^= 1) {
281             memcpy(to, mode == (top ? BOTTOM_FIRST : TOP_FIRST) ? buf : from, s->linesize[plane]);
282 
283             buf += s->frame->linesize[plane];
284             from += in->linesize[plane];
285             to += out->linesize[plane];
286         }
287     }
288 
289     if (in != s->frame)
290         av_frame_free(&s->frame);
291     s->frame = in;
292     return ff_filter_frame(outlink, out);
293 }
294 
uninit(AVFilterContext * ctx)295 static av_cold void uninit(AVFilterContext *ctx)
296 {
297     PhaseContext *s = ctx->priv;
298 
299     av_frame_free(&s->frame);
300 }
301 
302 static const AVFilterPad phase_inputs[] = {
303     {
304 		.name         = "default",
305         .type         = AVMEDIA_TYPE_VIDEO,
306         .filter_frame = filter_frame,
307         .config_props = config_input,
308 	},
309     { NULL }
310 };
311 
312 static const AVFilterPad phase_outputs[] = {
313     {
314 		.name = "default",
315         .type = AVMEDIA_TYPE_VIDEO,
316 	},
317     { NULL }
318 };
319 
320 AVFilter ff_vf_phase = {
321 	.name          = "phase",
322     .description   = NULL_IF_CONFIG_SMALL("Phase shift fields."),
323     .priv_size     = sizeof(PhaseContext),
324     .priv_class    = &phase_class,
325     .uninit        = uninit,
326     .query_formats = query_formats,
327     .inputs        = phase_inputs,
328     .outputs       = phase_outputs,
329     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
330 };
331