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"
31 #define accumulator int
33 #define pixel uint16_t
34 #define accumulator int64_t
37 #define fn3(a,b) a##_##b
38 #define fn2(a,b) fn3(a,b)
39 #define fn(a) fn2(a, DEPTH)
42 * This macro interpolates the value of both fields at a point halfway
43 * between lines and takes the squared difference. In field resolution
44 * the point is a quarter pixel below a line in one field and a quarter
45 * pixel above a line in other.
47 * (The result is actually multiplied by 25)
49 #define DIFF(a, as, b, bs) ((t) = ((*(a) - (b)[bs]) << 2) + (a)[(as) << 1] - (b)[-(bs)], (t) * (t))
52 * Find which field combination has the smallest average squared difference
55 static enum PhaseMode fn(analyze_plane)(void *ctx, enum PhaseMode mode, AVFrame *old, AVFrame *new)
57 double bdiff, tdiff, pdiff;
60 mode = new->interlaced_frame ? new->top_field_first ?
61 TOP_FIRST : BOTTOM_FIRST : PROGRESSIVE;
62 } else if (mode == AUTO_ANALYZE) {
63 mode = new->interlaced_frame ? new->top_field_first ?
64 TOP_FIRST_ANALYZE : BOTTOM_FIRST_ANALYZE : FULL_ANALYZE;
67 if (mode <= BOTTOM_FIRST) {
68 bdiff = pdiff = tdiff = 65536.0;
70 const double factor = 1. / (25. * (1 << (DEPTH - 8)) * (1 << (DEPTH - 8)));
71 const int ns = new->linesize[0] / sizeof(pixel);
72 const int os = old->linesize[0] / sizeof(pixel);
73 const pixel *nptr = (pixel *)new->data[0];
74 const pixel *optr = (pixel *)old->data[0];
75 const int h = new->height;
76 const int w = new->width;
77 accumulator bdif, tdif, pdif;
81 const pixel *rend, *end = nptr + (h - 2) * ns;
83 bdiff = pdiff = tdiff = 0.0;
88 pdif = tdif = bdif = 0;
91 case TOP_FIRST_ANALYZE:
93 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
94 pdif += DIFF(nptr, ns, nptr, ns);
95 tdif += DIFF(nptr, ns, optr, os);
98 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
99 pdif += DIFF(nptr, ns, nptr, ns);
100 tdif += DIFF(optr, os, nptr, ns);
104 case BOTTOM_FIRST_ANALYZE:
106 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
107 pdif += DIFF(nptr, ns, nptr, ns);
108 bdif += DIFF(optr, os, nptr, ns);
111 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
112 pdif += DIFF(nptr, ns, nptr, ns);
113 bdif += DIFF(nptr, ns, optr, os);
119 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
120 tdif += DIFF(nptr, ns, optr, os);
121 bdif += DIFF(optr, os, nptr, ns);
124 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
125 bdif += DIFF(nptr, ns, optr, os);
126 tdif += DIFF(optr, os, nptr, ns);
132 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
133 pdif += DIFF(nptr, ns, nptr, ns);
134 tdif += DIFF(nptr, ns, optr, os);
135 bdif += DIFF(optr, os, nptr, ns);
138 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
139 pdif += DIFF(nptr, ns, nptr, ns);
140 bdif += DIFF(nptr, ns, optr, os);
141 tdif += DIFF(optr, os, nptr, ns);
149 pdiff += (double)pdif;
150 tdiff += (double)tdif;
151 bdiff += (double)bdif;
157 scale = 1.0 / (w * (h - 3)) * factor;
162 if (mode == TOP_FIRST_ANALYZE) {
164 } else if (mode == BOTTOM_FIRST_ANALYZE) {
166 } else if (mode == ANALYZE) {
170 if (bdiff < pdiff && bdiff < tdiff) {
172 } else if (tdiff < pdiff && tdiff < bdiff) {
179 av_log(ctx, AV_LOG_DEBUG, "mode=%c tdiff=%f bdiff=%f pdiff=%f\n",
180 mode == BOTTOM_FIRST ? 'b' : mode == TOP_FIRST ? 't' : 'p',
181 tdiff, bdiff, pdiff);