FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/phase_template.c
Date: 2024-07-16 12:46:59
Exec Total Coverage
Lines: 51 88 58.0%
Functions: 0 0 -%
Branches: 0 0 -%

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