avformat/mxfdec: Check edit unit for overflow in mxf_set_current_edit_unit()
[FFMpeg-mirror.git] / libavfilter / phase_template.c
blob971cb4e34f92357dafe95dcf83d3b5c582ccf0b8
1 /*
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 "avfilter.h"
23 #include "video.h"
25 #undef pixel
26 #undef accumulator
27 #if DEPTH == 8
28 #define pixel uint8_t
29 #define accumulator int
30 #else
31 #define pixel uint16_t
32 #define accumulator int64_t
33 #endif
35 #define fn3(a,b) a##_##b
36 #define fn2(a,b) fn3(a,b)
37 #define fn(a) fn2(a, DEPTH)
40 * This macro interpolates the value of both fields at a point halfway
41 * between lines and takes the squared difference. In field resolution
42 * the point is a quarter pixel below a line in one field and a quarter
43 * pixel above a line in other.
45 * (The result is actually multiplied by 25)
47 #define DIFF(a, as, b, bs) ((t) = ((*(a) - (b)[bs]) * 4) + (a)[(as) * 2] - (b)[-(bs)], (t) * (t))
50 * Find which field combination has the smallest average squared difference
51 * between the fields.
53 static enum PhaseMode fn(analyze_plane)(void *ctx, enum PhaseMode mode, AVFrame *old, AVFrame *new)
55 double bdiff, tdiff, pdiff;
57 if (mode == AUTO) {
58 mode = (new->flags & AV_FRAME_FLAG_INTERLACED) ? (new->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST) ?
59 TOP_FIRST : BOTTOM_FIRST : PROGRESSIVE;
60 } else if (mode == AUTO_ANALYZE) {
61 mode = (new->flags & AV_FRAME_FLAG_INTERLACED) ? (new->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST) ?
62 TOP_FIRST_ANALYZE : BOTTOM_FIRST_ANALYZE : FULL_ANALYZE;
65 if (mode <= BOTTOM_FIRST) {
66 bdiff = pdiff = tdiff = 65536.0;
67 } else {
68 const double factor = 1. / (25. * (1 << (DEPTH - 8)) * (1 << (DEPTH - 8)));
69 const int ns = new->linesize[0] / sizeof(pixel);
70 const int os = old->linesize[0] / sizeof(pixel);
71 const pixel *nptr = (pixel *)new->data[0];
72 const pixel *optr = (pixel *)old->data[0];
73 const int h = new->height;
74 const int w = new->width;
75 accumulator bdif, tdif, pdif;
76 double scale;
78 int top = 0, t;
79 const pixel *rend, *end = nptr + (h - 2) * ns;
81 bdiff = pdiff = tdiff = 0.0;
83 nptr += ns;
84 optr += os;
85 while (nptr < end) {
86 pdif = tdif = bdif = 0;
88 switch (mode) {
89 case TOP_FIRST_ANALYZE:
90 if (top) {
91 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
92 pdif += DIFF(nptr, ns, nptr, ns);
93 tdif += DIFF(nptr, ns, optr, os);
95 } else {
96 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
97 pdif += DIFF(nptr, ns, nptr, ns);
98 tdif += DIFF(optr, os, nptr, ns);
101 break;
102 case BOTTOM_FIRST_ANALYZE:
103 if (top) {
104 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
105 pdif += DIFF(nptr, ns, nptr, ns);
106 bdif += DIFF(optr, os, nptr, ns);
108 } else {
109 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
110 pdif += DIFF(nptr, ns, nptr, ns);
111 bdif += DIFF(nptr, ns, optr, os);
114 break;
115 case ANALYZE:
116 if (top) {
117 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
118 tdif += DIFF(nptr, ns, optr, os);
119 bdif += DIFF(optr, os, nptr, ns);
121 } else {
122 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
123 bdif += DIFF(nptr, ns, optr, os);
124 tdif += DIFF(optr, os, nptr, ns);
127 break;
128 case FULL_ANALYZE:
129 if (top) {
130 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
131 pdif += DIFF(nptr, ns, nptr, ns);
132 tdif += DIFF(nptr, ns, optr, os);
133 bdif += DIFF(optr, os, nptr, ns);
135 } else {
136 for (rend = nptr + w; nptr < rend; nptr++, optr++) {
137 pdif += DIFF(nptr, ns, nptr, ns);
138 bdif += DIFF(nptr, ns, optr, os);
139 tdif += DIFF(optr, os, nptr, ns);
142 break;
143 default:
144 av_assert0(0);
147 pdiff += (double)pdif;
148 tdiff += (double)tdif;
149 bdiff += (double)bdif;
150 nptr += ns - w;
151 optr += os - w;
152 top ^= 1;
155 scale = 1.0 / (w * (h - 3)) * factor;
156 pdiff *= scale;
157 tdiff *= scale;
158 bdiff *= scale;
160 if (mode == TOP_FIRST_ANALYZE) {
161 bdiff = 65536.0;
162 } else if (mode == BOTTOM_FIRST_ANALYZE) {
163 tdiff = 65536.0;
164 } else if (mode == ANALYZE) {
165 pdiff = 65536.0;
168 if (bdiff < pdiff && bdiff < tdiff) {
169 mode = BOTTOM_FIRST;
170 } else if (tdiff < pdiff && tdiff < bdiff) {
171 mode = TOP_FIRST;
172 } else {
173 mode = PROGRESSIVE;
177 av_log(ctx, AV_LOG_DEBUG, "mode=%c tdiff=%f bdiff=%f pdiff=%f\n",
178 mode == BOTTOM_FIRST ? 'b' : mode == TOP_FIRST ? 't' : 'p',
179 tdiff, bdiff, pdiff);
180 return mode;