FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_fieldmatch.c
Date: 2024-04-19 17:50:32
Exec Total Coverage
Lines: 413 550 75.1%
Functions: 20 20 100.0%
Branches: 381 606 62.9%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2012 Fredrik Mellbin
3 * Copyright (c) 2013 Clément Bœsch
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * Fieldmatching filter, ported from VFM filter (VapourSynth) by Clément.
25 * Fredrik Mellbin is the author of the VIVTC/VFM filter, which is itself a
26 * light clone of the TIVTC/TFM (AviSynth) filter written by Kevin Stone
27 * (tritical), the original author.
28 *
29 * @see http://bengal.missouri.edu/~kes25c/
30 * @see http://www.vapoursynth.com/about/
31 */
32
33 #include <inttypes.h>
34
35 #include "libavutil/avassert.h"
36 #include "libavutil/imgutils.h"
37 #include "libavutil/mem.h"
38 #include "libavutil/opt.h"
39 #include "libavutil/timestamp.h"
40 #include "avfilter.h"
41 #include "filters.h"
42 #include "formats.h"
43 #include "internal.h"
44 #include "video.h"
45
46 #define INPUT_MAIN 0
47 #define INPUT_CLEANSRC 1
48
49 enum fieldmatch_parity {
50 FM_PARITY_AUTO = -1,
51 FM_PARITY_BOTTOM = 0,
52 FM_PARITY_TOP = 1,
53 };
54
55 enum matching_mode {
56 MODE_PC,
57 MODE_PC_N,
58 MODE_PC_U,
59 MODE_PC_N_UB,
60 MODE_PCN,
61 MODE_PCN_UB,
62 NB_MODE
63 };
64
65 enum comb_matching_mode {
66 COMBMATCH_NONE,
67 COMBMATCH_SC,
68 COMBMATCH_FULL,
69 NB_COMBMATCH
70 };
71
72 enum comb_dbg {
73 COMBDBG_NONE,
74 COMBDBG_PCN,
75 COMBDBG_PCNUB,
76 NB_COMBDBG
77 };
78
79 typedef struct FieldMatchContext {
80 const AVClass *class;
81
82 AVFrame *prv, *src, *nxt; ///< main sliding window of 3 frames
83 AVFrame *prv2, *src2, *nxt2; ///< sliding window of the optional second stream
84 int got_frame[2]; ///< frame request flag for each input stream
85 int hsub[2], vsub[2]; ///< chroma subsampling values
86 int bpc; ///< bytes per component
87 uint32_t eof; ///< bitmask for end of stream
88 int64_t lastscdiff;
89 int64_t lastn;
90
91 /* options */
92 int order;
93 int ppsrc;
94 int mode; ///< matching_mode
95 int field;
96 int mchroma;
97 int y0, y1;
98 int64_t scthresh;
99 double scthresh_flt;
100 int combmatch; ///< comb_matching_mode
101 int combdbg;
102 int cthresh;
103 int chroma;
104 int blockx, blocky;
105 int combpel;
106
107 /* misc buffers */
108 uint8_t *map_data[4];
109 int map_linesize[4];
110 uint8_t *cmask_data[4];
111 int cmask_linesize[4];
112 int *c_array;
113 int tpitchy, tpitchuv;
114 uint8_t *tbuffer;
115 } FieldMatchContext;
116
117 #define OFFSET(x) offsetof(FieldMatchContext, x)
118 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
119
120 static const AVOption fieldmatch_options[] = {
121 { "order", "specify the assumed field order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=FM_PARITY_AUTO}, -1, 1, FLAGS, .unit = "order" },
122 { "auto", "auto detect parity", 0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_AUTO}, INT_MIN, INT_MAX, FLAGS, .unit = "order" },
123 { "bff", "assume bottom field first", 0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_BOTTOM}, INT_MIN, INT_MAX, FLAGS, .unit = "order" },
124 { "tff", "assume top field first", 0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_TOP}, INT_MIN, INT_MAX, FLAGS, .unit = "order" },
125 { "mode", "set the matching mode or strategy to use", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_PC_N}, MODE_PC, NB_MODE-1, FLAGS, .unit = "mode" },
126 { "pc", "2-way match (p/c)", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PC}, INT_MIN, INT_MAX, FLAGS, .unit = "mode" },
127 { "pc_n", "2-way match + 3rd match on combed (p/c + u)", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PC_N}, INT_MIN, INT_MAX, FLAGS, .unit = "mode" },
128 { "pc_u", "2-way match + 3rd match (same order) on combed (p/c + u)", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PC_U}, INT_MIN, INT_MAX, FLAGS, .unit = "mode" },
129 { "pc_n_ub", "2-way match + 3rd match on combed + 4th/5th matches if still combed (p/c + u + u/b)", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PC_N_UB}, INT_MIN, INT_MAX, FLAGS, .unit = "mode" },
130 { "pcn", "3-way match (p/c/n)", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PCN}, INT_MIN, INT_MAX, FLAGS, .unit = "mode" },
131 { "pcn_ub", "3-way match + 4th/5th matches on combed (p/c/n + u/b)", 0, AV_OPT_TYPE_CONST, {.i64=MODE_PCN_UB}, INT_MIN, INT_MAX, FLAGS, .unit = "mode" },
132 { "ppsrc", "mark main input as a pre-processed input and activate clean source input stream", OFFSET(ppsrc), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
133 { "field", "set the field to match from", OFFSET(field), AV_OPT_TYPE_INT, {.i64=FM_PARITY_AUTO}, -1, 1, FLAGS, .unit = "field" },
134 { "auto", "automatic (same value as 'order')", 0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_AUTO}, INT_MIN, INT_MAX, FLAGS, .unit = "field" },
135 { "bottom", "bottom field", 0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_BOTTOM}, INT_MIN, INT_MAX, FLAGS, .unit = "field" },
136 { "top", "top field", 0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_TOP}, INT_MIN, INT_MAX, FLAGS, .unit = "field" },
137 { "mchroma", "set whether or not chroma is included during the match comparisons", OFFSET(mchroma), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
138 { "y0", "define an exclusion band which excludes the lines between y0 and y1 from the field matching decision", OFFSET(y0), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
139 { "y1", "define an exclusion band which excludes the lines between y0 and y1 from the field matching decision", OFFSET(y1), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
140 { "scthresh", "set scene change detection threshold", OFFSET(scthresh_flt), AV_OPT_TYPE_DOUBLE, {.dbl=12}, 0, 100, FLAGS },
141 { "combmatch", "set combmatching mode", OFFSET(combmatch), AV_OPT_TYPE_INT, {.i64=COMBMATCH_SC}, COMBMATCH_NONE, NB_COMBMATCH-1, FLAGS, .unit = "combmatching" },
142 { "none", "disable combmatching", 0, AV_OPT_TYPE_CONST, {.i64=COMBMATCH_NONE}, INT_MIN, INT_MAX, FLAGS, .unit = "combmatching" },
143 { "sc", "enable combmatching only on scene change", 0, AV_OPT_TYPE_CONST, {.i64=COMBMATCH_SC}, INT_MIN, INT_MAX, FLAGS, .unit = "combmatching" },
144 { "full", "enable combmatching all the time", 0, AV_OPT_TYPE_CONST, {.i64=COMBMATCH_FULL}, INT_MIN, INT_MAX, FLAGS, .unit = "combmatching" },
145 { "combdbg", "enable comb debug", OFFSET(combdbg), AV_OPT_TYPE_INT, {.i64=COMBDBG_NONE}, COMBDBG_NONE, NB_COMBDBG-1, FLAGS, .unit = "dbglvl" },
146 { "none", "no forced calculation", 0, AV_OPT_TYPE_CONST, {.i64=COMBDBG_NONE}, INT_MIN, INT_MAX, FLAGS, .unit = "dbglvl" },
147 { "pcn", "calculate p/c/n", 0, AV_OPT_TYPE_CONST, {.i64=COMBDBG_PCN}, INT_MIN, INT_MAX, FLAGS, .unit = "dbglvl" },
148 { "pcnub", "calculate p/c/n/u/b", 0, AV_OPT_TYPE_CONST, {.i64=COMBDBG_PCNUB}, INT_MIN, INT_MAX, FLAGS, .unit = "dbglvl" },
149 { "cthresh", "set the area combing threshold used for combed frame detection", OFFSET(cthresh), AV_OPT_TYPE_INT, {.i64= 9}, -1, 0xff, FLAGS },
150 { "chroma", "set whether or not chroma is considered in the combed frame decision", OFFSET(chroma), AV_OPT_TYPE_BOOL,{.i64= 0}, 0, 1, FLAGS },
151 { "blockx", "set the x-axis size of the window used during combed frame detection", OFFSET(blockx), AV_OPT_TYPE_INT, {.i64=16}, 4, 1<<9, FLAGS },
152 { "blocky", "set the y-axis size of the window used during combed frame detection", OFFSET(blocky), AV_OPT_TYPE_INT, {.i64=16}, 4, 1<<9, FLAGS },
153 { "combpel", "set the number of combed pixels inside any of the blocky by blockx size blocks on the frame for the frame to be detected as combed", OFFSET(combpel), AV_OPT_TYPE_INT, {.i64=80}, 0, INT_MAX, FLAGS },
154 { NULL }
155 };
156
157 AVFILTER_DEFINE_CLASS(fieldmatch);
158
159 1549 static int get_width(const FieldMatchContext *fm, const AVFrame *f, int plane, int input)
160 {
161
2/2
✓ Branch 0 taken 886 times.
✓ Branch 1 taken 663 times.
1549 return plane ? AV_CEIL_RSHIFT(f->width, fm->hsub[input]) : f->width;
162 }
163
164 1549 static int get_height(const FieldMatchContext *fm, const AVFrame *f, int plane, int input)
165 {
166
2/2
✓ Branch 0 taken 886 times.
✓ Branch 1 taken 663 times.
1549 return plane ? AV_CEIL_RSHIFT(f->height, fm->vsub[input]) : f->height;
167 }
168
169 130 static int64_t luma_abs_diff(const AVFrame *f1, const AVFrame *f2)
170 {
171 int x, y;
172 130 const uint8_t *srcp1 = f1->data[0];
173 130 const uint8_t *srcp2 = f2->data[0];
174 130 const int src1_linesize = f1->linesize[0];
175 130 const int src2_linesize = f2->linesize[0];
176 130 const int width = f1->width;
177 130 const int height = f1->height;
178 130 int64_t acc = 0;
179
180
2/2
✓ Branch 0 taken 37440 times.
✓ Branch 1 taken 130 times.
37570 for (y = 0; y < height; y++) {
181
2/2
✓ Branch 0 taken 13178880 times.
✓ Branch 1 taken 37440 times.
13216320 for (x = 0; x < width; x++)
182 13178880 acc += abs(srcp1[x] - srcp2[x]);
183 37440 srcp1 += src1_linesize;
184 37440 srcp2 += src2_linesize;
185 }
186 130 return acc;
187 }
188
189 595 static void fill_buf(uint8_t *data, int w, int h, int linesize, uint8_t v)
190 {
191 int y;
192
193
2/2
✓ Branch 0 taken 153360 times.
✓ Branch 1 taken 595 times.
153955 for (y = 0; y < h; y++) {
194 153360 memset(data, v, w);
195 153360 data += linesize;
196 }
197 595 }
198
199 220 static int calc_combed_score(const FieldMatchContext *fm, const AVFrame *src)
200 {
201 220 int x, y, plane, max_v = 0;
202 220 const int cthresh = fm->cthresh;
203 220 const int cthresh6 = cthresh * 6;
204
205
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 440 times.
✓ Branch 2 taken 220 times.
✓ Branch 3 taken 220 times.
440 for (plane = 0; plane < (fm->chroma ? 3 : 1); plane++) {
206 220 const uint8_t *srcp = src->data[plane];
207 220 const int src_linesize = src->linesize[plane];
208 220 const int width = get_width (fm, src, plane, INPUT_MAIN);
209 220 const int height = get_height(fm, src, plane, INPUT_MAIN);
210 220 uint8_t *cmkp = fm->cmask_data[plane];
211 220 const int cmk_linesize = fm->cmask_linesize[plane];
212
213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 220 times.
220 if (cthresh < 0) {
214 fill_buf(cmkp, width, height, cmk_linesize, 0xff);
215 continue;
216 }
217 220 fill_buf(cmkp, width, height, cmk_linesize, 0);
218
219 /* [1 -3 4 -3 1] vertical filter */
220 #define FILTER(xm2, xm1, xp1, xp2) \
221 abs( 4 * srcp[x] \
222 -3 * (srcp[x + (xm1)*src_linesize] + srcp[x + (xp1)*src_linesize]) \
223 + (srcp[x + (xm2)*src_linesize] + srcp[x + (xp2)*src_linesize])) > cthresh6
224
225 /* first line */
226
2/2
✓ Branch 0 taken 77440 times.
✓ Branch 1 taken 220 times.
77660 for (x = 0; x < width; x++) {
227 77440 const int s1 = abs(srcp[x] - srcp[x + src_linesize]);
228
4/4
✓ Branch 0 taken 41535 times.
✓ Branch 1 taken 35905 times.
✓ Branch 2 taken 40180 times.
✓ Branch 3 taken 1355 times.
77440 if (s1 > cthresh && FILTER(2, 1, 1, 2))
229 40180 cmkp[x] = 0xff;
230 }
231 220 srcp += src_linesize;
232 220 cmkp += cmk_linesize;
233
234 /* second line */
235
2/2
✓ Branch 0 taken 77440 times.
✓ Branch 1 taken 220 times.
77660 for (x = 0; x < width; x++) {
236 77440 const int s1 = abs(srcp[x] - srcp[x - src_linesize]);
237 77440 const int s2 = abs(srcp[x] - srcp[x + src_linesize]);
238
6/6
✓ Branch 0 taken 41535 times.
✓ Branch 1 taken 35905 times.
✓ Branch 2 taken 37459 times.
✓ Branch 3 taken 4076 times.
✓ Branch 4 taken 37099 times.
✓ Branch 5 taken 360 times.
77440 if (s1 > cthresh && s2 > cthresh && FILTER(2, -1, 1, 2))
239 37099 cmkp[x] = 0xff;
240 }
241 220 srcp += src_linesize;
242 220 cmkp += cmk_linesize;
243
244 /* all lines minus first two and last two */
245
2/2
✓ Branch 0 taken 62480 times.
✓ Branch 1 taken 220 times.
62700 for (y = 2; y < height-2; y++) {
246
2/2
✓ Branch 0 taken 21992960 times.
✓ Branch 1 taken 62480 times.
22055440 for (x = 0; x < width; x++) {
247 21992960 const int s1 = abs(srcp[x] - srcp[x - src_linesize]);
248 21992960 const int s2 = abs(srcp[x] - srcp[x + src_linesize]);
249
6/6
✓ Branch 0 taken 12008290 times.
✓ Branch 1 taken 9984670 times.
✓ Branch 2 taken 10342405 times.
✓ Branch 3 taken 1665885 times.
✓ Branch 4 taken 9768879 times.
✓ Branch 5 taken 573526 times.
21992960 if (s1 > cthresh && s2 > cthresh && FILTER(-2, -1, 1, 2))
250 9768879 cmkp[x] = 0xff;
251 }
252 62480 srcp += src_linesize;
253 62480 cmkp += cmk_linesize;
254 }
255
256 /* before-last line */
257
2/2
✓ Branch 0 taken 77440 times.
✓ Branch 1 taken 220 times.
77660 for (x = 0; x < width; x++) {
258 77440 const int s1 = abs(srcp[x] - srcp[x - src_linesize]);
259 77440 const int s2 = abs(srcp[x] - srcp[x + src_linesize]);
260
6/6
✓ Branch 0 taken 41719 times.
✓ Branch 1 taken 35721 times.
✓ Branch 2 taken 33941 times.
✓ Branch 3 taken 7778 times.
✓ Branch 4 taken 30781 times.
✓ Branch 5 taken 3160 times.
77440 if (s1 > cthresh && s2 > cthresh && FILTER(-2, -1, 1, -2))
261 30781 cmkp[x] = 0xff;
262 }
263 220 srcp += src_linesize;
264 220 cmkp += cmk_linesize;
265
266 /* last line */
267
2/2
✓ Branch 0 taken 77440 times.
✓ Branch 1 taken 220 times.
77660 for (x = 0; x < width; x++) {
268 77440 const int s1 = abs(srcp[x] - srcp[x - src_linesize]);
269
4/4
✓ Branch 0 taken 40462 times.
✓ Branch 1 taken 36978 times.
✓ Branch 2 taken 35380 times.
✓ Branch 3 taken 5082 times.
77440 if (s1 > cthresh && FILTER(-2, -1, -1, -2))
270 35380 cmkp[x] = 0xff;
271 }
272 }
273
274
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 220 times.
220 if (fm->chroma) {
275 uint8_t *cmkp = fm->cmask_data[0];
276 uint8_t *cmkpU = fm->cmask_data[1];
277 uint8_t *cmkpV = fm->cmask_data[2];
278 const int width = AV_CEIL_RSHIFT(src->width, fm->hsub[INPUT_MAIN]);
279 const int height = AV_CEIL_RSHIFT(src->height, fm->vsub[INPUT_MAIN]);
280 const int cmk_linesize = fm->cmask_linesize[0] << 1;
281 const int cmk_linesizeUV = fm->cmask_linesize[2];
282 uint8_t *cmkpp = cmkp - (cmk_linesize>>1);
283 uint8_t *cmkpn = cmkp + (cmk_linesize>>1);
284 uint8_t *cmkpnn = cmkp + cmk_linesize;
285 for (y = 1; y < height - 1; y++) {
286 cmkpp += cmk_linesize;
287 cmkp += cmk_linesize;
288 cmkpn += cmk_linesize;
289 cmkpnn += cmk_linesize;
290 cmkpV += cmk_linesizeUV;
291 cmkpU += cmk_linesizeUV;
292 for (x = 1; x < width - 1; x++) {
293 #define HAS_FF_AROUND(p, lz) (p[(x)-1 - (lz)] == 0xff || p[(x) - (lz)] == 0xff || p[(x)+1 - (lz)] == 0xff || \
294 p[(x)-1 ] == 0xff || p[(x)+1 ] == 0xff || \
295 p[(x)-1 + (lz)] == 0xff || p[(x) + (lz)] == 0xff || p[(x)+1 + (lz)] == 0xff)
296 if ((cmkpV[x] == 0xff && HAS_FF_AROUND(cmkpV, cmk_linesizeUV)) ||
297 (cmkpU[x] == 0xff && HAS_FF_AROUND(cmkpU, cmk_linesizeUV))) {
298 ((uint16_t*)cmkp)[x] = 0xffff;
299 ((uint16_t*)cmkpn)[x] = 0xffff;
300 if (y&1) ((uint16_t*)cmkpp)[x] = 0xffff;
301 else ((uint16_t*)cmkpnn)[x] = 0xffff;
302 }
303 }
304 }
305 }
306
307 {
308 220 const int blockx = fm->blockx;
309 220 const int blocky = fm->blocky;
310 220 const int xhalf = blockx/2;
311 220 const int yhalf = blocky/2;
312 220 const int cmk_linesize = fm->cmask_linesize[0];
313 220 const uint8_t *cmkp = fm->cmask_data[0] + cmk_linesize;
314 220 const int width = src->width;
315 220 const int height = src->height;
316 220 const int xblocks = ((width+xhalf)/blockx) + 1;
317 220 const int xblocks4 = xblocks<<2;
318 220 const int yblocks = ((height+yhalf)/blocky) + 1;
319 220 int *c_array = fm->c_array;
320 220 const int arraysize = (xblocks*yblocks)<<2;
321 220 int heighta = (height/(blocky/2))*(blocky/2);
322 220 const int widtha = (width /(blockx/2))*(blockx/2);
323
1/2
✓ Branch 0 taken 220 times.
✗ Branch 1 not taken.
220 if (heighta == height)
324 220 heighta = height - yhalf;
325 220 memset(c_array, 0, arraysize * sizeof(*c_array));
326
327 #define C_ARRAY_ADD(v) do { \
328 const int box1 = (x / blockx) * 4; \
329 const int box2 = ((x + xhalf) / blockx) * 4; \
330 c_array[temp1 + box1 ] += v; \
331 c_array[temp1 + box2 + 1] += v; \
332 c_array[temp2 + box1 + 2] += v; \
333 c_array[temp2 + box2 + 3] += v; \
334 } while (0)
335
336 #define VERTICAL_HALF(y_start, y_end) do { \
337 for (y = y_start; y < y_end; y++) { \
338 const int temp1 = (y / blocky) * xblocks4; \
339 const int temp2 = ((y + yhalf) / blocky) * xblocks4; \
340 for (x = 0; x < width; x++) \
341 if (cmkp[x - cmk_linesize] == 0xff && \
342 cmkp[x ] == 0xff && \
343 cmkp[x + cmk_linesize] == 0xff) \
344 C_ARRAY_ADD(1); \
345 cmkp += cmk_linesize; \
346 } \
347 } while (0)
348
349
10/10
✓ Branch 0 taken 261066 times.
✓ Branch 1 taken 281014 times.
✓ Branch 2 taken 252355 times.
✓ Branch 3 taken 8711 times.
✓ Branch 4 taken 247367 times.
✓ Branch 5 taken 4988 times.
✓ Branch 6 taken 542080 times.
✓ Branch 7 taken 1540 times.
✓ Branch 8 taken 1540 times.
✓ Branch 9 taken 220 times.
543840 VERTICAL_HALF(1, yhalf);
350
351
2/2
✓ Branch 0 taken 7480 times.
✓ Branch 1 taken 220 times.
7700 for (y = yhalf; y < heighta; y += yhalf) {
352 7480 const int temp1 = (y / blocky) * xblocks4;
353 7480 const int temp2 = ((y + yhalf) / blocky) * xblocks4;
354
355
2/2
✓ Branch 0 taken 329120 times.
✓ Branch 1 taken 7480 times.
336600 for (x = 0; x < widtha; x += xhalf) {
356 329120 const uint8_t *cmkp_tmp = cmkp + x;
357 329120 int u, v, sum = 0;
358
2/2
✓ Branch 0 taken 2632960 times.
✓ Branch 1 taken 329120 times.
2962080 for (u = 0; u < yhalf; u++) {
359
2/2
✓ Branch 0 taken 21063680 times.
✓ Branch 1 taken 2632960 times.
23696640 for (v = 0; v < xhalf; v++)
360
2/2
✓ Branch 0 taken 9365808 times.
✓ Branch 1 taken 11697872 times.
21063680 if (cmkp_tmp[v - cmk_linesize] == 0xff &&
361
2/2
✓ Branch 0 taken 8453820 times.
✓ Branch 1 taken 911988 times.
9365808 cmkp_tmp[v ] == 0xff &&
362
2/2
✓ Branch 0 taken 7819534 times.
✓ Branch 1 taken 634286 times.
8453820 cmkp_tmp[v + cmk_linesize] == 0xff)
363 7819534 sum++;
364 2632960 cmkp_tmp += cmk_linesize;
365 }
366
2/2
✓ Branch 0 taken 200837 times.
✓ Branch 1 taken 128283 times.
329120 if (sum)
367 200837 C_ARRAY_ADD(sum);
368 }
369
370
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7480 times.
7480 for (x = widtha; x < width; x++) {
371 const uint8_t *cmkp_tmp = cmkp + x;
372 int u, sum = 0;
373 for (u = 0; u < yhalf; u++) {
374 if (cmkp_tmp[-cmk_linesize] == 0xff &&
375 cmkp_tmp[ 0] == 0xff &&
376 cmkp_tmp[ cmk_linesize] == 0xff)
377 sum++;
378 cmkp_tmp += cmk_linesize;
379 }
380 if (sum)
381 C_ARRAY_ADD(sum);
382 }
383
384 7480 cmkp += cmk_linesize * yhalf;
385 }
386
387
10/10
✓ Branch 0 taken 219284 times.
✓ Branch 1 taken 322796 times.
✓ Branch 2 taken 188226 times.
✓ Branch 3 taken 31058 times.
✓ Branch 4 taken 170775 times.
✓ Branch 5 taken 17451 times.
✓ Branch 6 taken 542080 times.
✓ Branch 7 taken 1540 times.
✓ Branch 8 taken 1540 times.
✓ Branch 9 taken 220 times.
543840 VERTICAL_HALF(heighta, height - 1);
388
389
2/2
✓ Branch 0 taken 384560 times.
✓ Branch 1 taken 220 times.
384780 for (x = 0; x < arraysize; x++)
390
2/2
✓ Branch 0 taken 971 times.
✓ Branch 1 taken 383589 times.
384560 if (c_array[x] > max_v)
391 971 max_v = c_array[x];
392 }
393 220 return max_v;
394 }
395
396 // the secret is that tbuffer is an interlaced, offset subset of all the lines
397 375 static void build_abs_diff_mask(const uint8_t *prvp, int prv_linesize,
398 const uint8_t *nxtp, int nxt_linesize,
399 uint8_t *tbuffer, int tbuf_linesize,
400 int width, int height)
401 {
402 int y, x;
403
404 375 prvp -= prv_linesize;
405 375 nxtp -= nxt_linesize;
406
2/2
✓ Branch 0 taken 45000 times.
✓ Branch 1 taken 375 times.
45375 for (y = 0; y < height; y++) {
407
2/2
✓ Branch 0 taken 11563200 times.
✓ Branch 1 taken 45000 times.
11608200 for (x = 0; x < width; x++)
408 11563200 tbuffer[x] = FFABS(prvp[x] - nxtp[x]);
409 45000 prvp += prv_linesize;
410 45000 nxtp += nxt_linesize;
411 45000 tbuffer += tbuf_linesize;
412 }
413 375 }
414
415 /**
416 * Build a map over which pixels differ a lot/a little
417 */
418 375 static void build_diff_map(FieldMatchContext *fm,
419 const uint8_t *prvp, int prv_linesize,
420 const uint8_t *nxtp, int nxt_linesize,
421 uint8_t *dstp, int dst_linesize, int height,
422 int width, int plane)
423 {
424 int x, y, u, diff, count;
425
2/2
✓ Branch 0 taken 250 times.
✓ Branch 1 taken 125 times.
375 int tpitch = plane ? fm->tpitchuv : fm->tpitchy;
426 375 const uint8_t *dp = fm->tbuffer + tpitch;
427
428 375 build_abs_diff_mask(prvp, prv_linesize, nxtp, nxt_linesize,
429 fm->tbuffer, tpitch, width, height>>1);
430
431
2/2
✓ Branch 0 taken 44250 times.
✓ Branch 1 taken 375 times.
44625 for (y = 2; y < height - 2; y += 2) {
432
2/2
✓ Branch 0 taken 11298700 times.
✓ Branch 1 taken 44250 times.
11342950 for (x = 1; x < width - 1; x++) {
433 11298700 diff = dp[x];
434
2/2
✓ Branch 0 taken 8349851 times.
✓ Branch 1 taken 2948849 times.
11298700 if (diff > 3) {
435
4/4
✓ Branch 0 taken 17173724 times.
✓ Branch 1 taken 29376 times.
✓ Branch 2 taken 8853249 times.
✓ Branch 3 taken 8320475 times.
17203100 for (count = 0, u = x-1; u < x+2 && count < 2; u++) {
436 8853249 count += dp[u-tpitch] > 3;
437 8853249 count += dp[u ] > 3;
438 8853249 count += dp[u+tpitch] > 3;
439 }
440
2/2
✓ Branch 0 taken 8341988 times.
✓ Branch 1 taken 7863 times.
8349851 if (count > 1) {
441 8341988 dstp[x] = 1;
442
2/2
✓ Branch 0 taken 5368701 times.
✓ Branch 1 taken 2973287 times.
8341988 if (diff > 19) {
443 5368701 int upper = 0, lower = 0;
444
4/4
✓ Branch 0 taken 16106103 times.
✓ Branch 1 taken 1749851 times.
✓ Branch 2 taken 12487253 times.
✓ Branch 3 taken 3618850 times.
17855954 for (count = 0, u = x-1; u < x+2 && count < 6; u++) {
445
2/2
✓ Branch 0 taken 9663768 times.
✓ Branch 1 taken 2823485 times.
12487253 if (dp[u-tpitch] > 19) { count++; upper = 1; }
446
2/2
✓ Branch 0 taken 11376572 times.
✓ Branch 1 taken 1110681 times.
12487253 if (dp[u ] > 19) count++;
447
2/2
✓ Branch 0 taken 9681749 times.
✓ Branch 1 taken 2805504 times.
12487253 if (dp[u+tpitch] > 19) { count++; lower = 1; }
448 }
449
2/2
✓ Branch 0 taken 4947837 times.
✓ Branch 1 taken 420864 times.
5368701 if (count > 3) {
450
4/4
✓ Branch 0 taken 4735927 times.
✓ Branch 1 taken 211910 times.
✓ Branch 2 taken 4503660 times.
✓ Branch 3 taken 232267 times.
4947837 if (upper && lower) {
451 4503660 dstp[x] |= 1<<1;
452 } else {
453 444177 int upper2 = 0, lower2 = 0;
454
4/4
✓ Branch 0 taken 4375447 times.
✓ Branch 1 taken 44284 times.
✓ Branch 2 taken 3975554 times.
✓ Branch 3 taken 444177 times.
4419731 for (u = FFMAX(x-4,0); u < FFMIN(x+5,width); u++) {
455
4/4
✓ Branch 0 taken 3949581 times.
✓ Branch 1 taken 25973 times.
✓ Branch 2 taken 1912449 times.
✓ Branch 3 taken 2037132 times.
3975554 if (y != 2 && dp[u-2*tpitch] > 19) upper2 = 1;
456
2/2
✓ Branch 0 taken 1761812 times.
✓ Branch 1 taken 2213742 times.
3975554 if ( dp[u- tpitch] > 19) upper = 1;
457
2/2
✓ Branch 0 taken 1724739 times.
✓ Branch 1 taken 2250815 times.
3975554 if ( dp[u+ tpitch] > 19) lower = 1;
458
4/4
✓ Branch 0 taken 3938974 times.
✓ Branch 1 taken 36580 times.
✓ Branch 2 taken 1911008 times.
✓ Branch 3 taken 2027966 times.
3975554 if (y != height-4 && dp[u+2*tpitch] > 19) lower2 = 1;
459 }
460
8/8
✓ Branch 0 taken 389308 times.
✓ Branch 1 taken 54869 times.
✓ Branch 2 taken 68776 times.
✓ Branch 3 taken 320532 times.
✓ Branch 4 taken 6218 times.
✓ Branch 5 taken 62558 times.
✓ Branch 6 taken 54869 times.
✓ Branch 7 taken 6218 times.
444177 if ((upper && (lower || upper2)) ||
461
3/4
✓ Branch 0 taken 54869 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48685 times.
✓ Branch 3 taken 6184 times.
54869 (lower && (upper || lower2)))
462 431775 dstp[x] |= 1<<1;
463
2/2
✓ Branch 0 taken 8927 times.
✓ Branch 1 taken 3475 times.
12402 else if (count > 5)
464 8927 dstp[x] |= 1<<2;
465 }
466 }
467 }
468 }
469 }
470 }
471 44250 dp += tpitch;
472 44250 dstp += dst_linesize;
473 }
474 375 }
475
476 enum { mP, mC, mN, mB, mU };
477
478 750 static int get_field_base(int match, int field)
479 {
480
1/2
✓ Branch 0 taken 750 times.
✗ Branch 1 not taken.
750 return match < 3 ? 2 - field : 1 + field;
481 }
482
483 750 static AVFrame *select_frame(FieldMatchContext *fm, int match)
484 {
485
3/4
✓ Branch 0 taken 375 times.
✓ Branch 1 taken 375 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 375 times.
750 if (match == mP || match == mB) return fm->prv;
486
2/4
✓ Branch 0 taken 375 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 375 times.
375 else if (match == mN || match == mU) return fm->nxt;
487 375 else /* match == mC */ return fm->src;
488 }
489
490 125 static int compare_fields(FieldMatchContext *fm, int match1, int match2, int field)
491 {
492 int plane, ret;
493 125 uint64_t accumPc = 0, accumPm = 0, accumPml = 0;
494 125 uint64_t accumNc = 0, accumNm = 0, accumNml = 0;
495 int norm1, norm2, mtn1, mtn2;
496 float c1, c2, mr;
497 125 const AVFrame *src = fm->src;
498
499
3/4
✓ Branch 0 taken 500 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 375 times.
✓ Branch 3 taken 125 times.
500 for (plane = 0; plane < (fm->mchroma ? 3 : 1); plane++) {
500 int x, y, temp1, temp2, fbase;
501 const AVFrame *prev, *next;
502 375 uint8_t *mapp = fm->map_data[plane];
503 375 int map_linesize = fm->map_linesize[plane];
504 375 const uint8_t *srcp = src->data[plane];
505 375 const int src_linesize = src->linesize[plane];
506 375 const int srcf_linesize = src_linesize << 1;
507 int prv_linesize, nxt_linesize;
508 int prvf_linesize, nxtf_linesize;
509 375 const int width = get_width (fm, src, plane, INPUT_MAIN);
510 375 const int height = get_height(fm, src, plane, INPUT_MAIN);
511
2/2
✓ Branch 0 taken 250 times.
✓ Branch 1 taken 125 times.
375 const int y0a = fm->y0 >> (plane ? fm->vsub[INPUT_MAIN] : 0);
512
2/2
✓ Branch 0 taken 250 times.
✓ Branch 1 taken 125 times.
375 const int y1a = fm->y1 >> (plane ? fm->vsub[INPUT_MAIN] : 0);
513
2/2
✓ Branch 0 taken 250 times.
✓ Branch 1 taken 125 times.
375 const int startx = (plane == 0 ? 8 : 8 >> fm->hsub[INPUT_MAIN]);
514 375 const int stopx = width - startx;
515 const uint8_t *srcpf, *srcf, *srcnf;
516 const uint8_t *prvpf, *prvnf, *nxtpf, *nxtnf;
517
518 375 fill_buf(mapp, width, height, map_linesize, 0);
519
520 /* match1 */
521 375 fbase = get_field_base(match1, field);
522 375 srcf = srcp + (fbase + 1) * src_linesize;
523 375 srcpf = srcf - srcf_linesize;
524 375 srcnf = srcf + srcf_linesize;
525 375 mapp = mapp + fbase * map_linesize;
526 375 prev = select_frame(fm, match1);
527 375 prv_linesize = prev->linesize[plane];
528 375 prvf_linesize = prv_linesize << 1;
529 375 prvpf = prev->data[plane] + fbase * prv_linesize; // previous frame, previous field
530 375 prvnf = prvpf + prvf_linesize; // previous frame, next field
531
532 /* match2 */
533 375 fbase = get_field_base(match2, field);
534 375 next = select_frame(fm, match2);
535 375 nxt_linesize = next->linesize[plane];
536 375 nxtf_linesize = nxt_linesize << 1;
537 375 nxtpf = next->data[plane] + fbase * nxt_linesize; // next frame, previous field
538 375 nxtnf = nxtpf + nxtf_linesize; // next frame, next field
539
540 375 map_linesize <<= 1;
541
3/8
✗ Branch 0 not taken.
✓ Branch 1 taken 375 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 375 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 375 times.
375 if ((match1 >= 3 && field == 1) || (match1 < 3 && field != 1))
542 build_diff_map(fm, prvpf, prvf_linesize, nxtpf, nxtf_linesize,
543 mapp, map_linesize, height, width, plane);
544 else
545 375 build_diff_map(fm, prvnf, prvf_linesize, nxtnf, nxtf_linesize,
546 mapp + map_linesize, map_linesize, height, width, plane);
547
548
2/2
✓ Branch 0 taken 44250 times.
✓ Branch 1 taken 375 times.
44625 for (y = 2; y < height - 2; y += 2) {
549
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 44250 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
44250 if (y0a == y1a || y < y0a || y > y1a) {
550
2/2
✓ Branch 0 taken 10869600 times.
✓ Branch 1 taken 44250 times.
10913850 for (x = startx; x < stopx; x++) {
551
4/4
✓ Branch 0 taken 2915754 times.
✓ Branch 1 taken 7953846 times.
✓ Branch 2 taken 784879 times.
✓ Branch 3 taken 2130875 times.
10869600 if (mapp[x] > 0 || mapp[x + map_linesize] > 0) {
552 8738725 temp1 = srcpf[x] + (srcf[x] << 2) + srcnf[x]; // [1 4 1]
553
554 8738725 temp2 = abs(3 * (prvpf[x] + prvnf[x]) - temp1);
555
5/6
✓ Branch 0 taken 3823099 times.
✓ Branch 1 taken 4915626 times.
✓ Branch 2 taken 188970 times.
✓ Branch 3 taken 3634129 times.
✓ Branch 4 taken 188970 times.
✗ Branch 5 not taken.
8738725 if (temp2 > 23 && ((mapp[x]&1) || (mapp[x + map_linesize]&1)))
556 3823099 accumPc += temp2;
557
2/2
✓ Branch 0 taken 3272461 times.
✓ Branch 1 taken 5466264 times.
8738725 if (temp2 > 42) {
558
4/4
✓ Branch 0 taken 797824 times.
✓ Branch 1 taken 2474637 times.
✓ Branch 2 taken 185193 times.
✓ Branch 3 taken 612631 times.
3272461 if ((mapp[x]&2) || (mapp[x + map_linesize]&2))
559 2659830 accumPm += temp2;
560
4/4
✓ Branch 0 taken 3271377 times.
✓ Branch 1 taken 1084 times.
✓ Branch 2 taken 355 times.
✓ Branch 3 taken 3271022 times.
3272461 if ((mapp[x]&4) || (mapp[x + map_linesize]&4))
561 1439 accumPml += temp2;
562 }
563
564 8738725 temp2 = abs(3 * (nxtpf[x] + nxtnf[x]) - temp1);
565
5/6
✓ Branch 0 taken 6363210 times.
✓ Branch 1 taken 2375515 times.
✓ Branch 2 taken 582549 times.
✓ Branch 3 taken 5780661 times.
✓ Branch 4 taken 582549 times.
✗ Branch 5 not taken.
8738725 if (temp2 > 23 && ((mapp[x]&1) || (mapp[x + map_linesize]&1)))
566 6363210 accumNc += temp2;
567
2/2
✓ Branch 0 taken 5577754 times.
✓ Branch 1 taken 3160971 times.
8738725 if (temp2 > 42) {
568
4/4
✓ Branch 0 taken 2477003 times.
✓ Branch 1 taken 3100751 times.
✓ Branch 2 taken 469157 times.
✓ Branch 3 taken 2007846 times.
5577754 if ((mapp[x]&2) || (mapp[x + map_linesize]&2))
569 3569908 accumNm += temp2;
570
4/4
✓ Branch 0 taken 5570593 times.
✓ Branch 1 taken 7161 times.
✓ Branch 2 taken 3202 times.
✓ Branch 3 taken 5567391 times.
5577754 if ((mapp[x]&4) || (mapp[x + map_linesize]&4))
571 10363 accumNml += temp2;
572 }
573 }
574 }
575 }
576 44250 prvpf += prvf_linesize;
577 44250 prvnf += prvf_linesize;
578 44250 srcpf += srcf_linesize;
579 44250 srcf += srcf_linesize;
580 44250 srcnf += srcf_linesize;
581 44250 nxtpf += nxtf_linesize;
582 44250 nxtnf += nxtf_linesize;
583 44250 mapp += map_linesize;
584 }
585 }
586
587
6/8
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 114 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 10 times.
125 if (accumPm < 500 && accumNm < 500 && (accumPml >= 500 || accumNml >= 500) &&
588 FFMAX(accumPml,accumNml) > 3*FFMIN(accumPml,accumNml)) {
589 accumPm = accumPml;
590 accumNm = accumNml;
591 }
592
593 125 norm1 = (int)((accumPc / 6.0f) + 0.5f);
594 125 norm2 = (int)((accumNc / 6.0f) + 0.5f);
595 125 mtn1 = (int)((accumPm / 6.0f) + 0.5f);
596 125 mtn2 = (int)((accumNm / 6.0f) + 0.5f);
597
6/6
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 80 times.
✓ Branch 2 taken 115 times.
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 45 times.
✓ Branch 5 taken 70 times.
125 c1 = ((float)FFMAX(norm1,norm2)) / ((float)FFMAX(FFMIN(norm1,norm2),1));
598
6/6
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 75 times.
✓ Branch 2 taken 115 times.
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 50 times.
✓ Branch 5 taken 65 times.
125 c2 = ((float)FFMAX(mtn1, mtn2)) / ((float)FFMAX(FFMIN(mtn1, mtn2), 1));
599
6/6
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 75 times.
✓ Branch 2 taken 115 times.
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 45 times.
✓ Branch 5 taken 70 times.
125 mr = ((float)FFMAX(mtn1, mtn2)) / ((float)FFMAX(FFMAX(norm1,norm2),1));
600
10/10
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 111 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 52 times.
✓ Branch 5 taken 63 times.
✓ Branch 6 taken 12 times.
✓ Branch 7 taken 40 times.
✓ Branch 8 taken 10 times.
✓ Branch 9 taken 12 times.
125 if (((mtn1 >= 500 || mtn2 >= 500) && (mtn1*2 < mtn2*1 || mtn2*2 < mtn1*1)) ||
601
7/8
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 7 times.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 10 times.
✓ Branch 7 taken 7 times.
22 ((mtn1 >= 1000 || mtn2 >= 1000) && (mtn1*3 < mtn2*2 || mtn2*3 < mtn1*2)) ||
602
7/8
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 10 times.
✓ Branch 7 taken 4 times.
17 ((mtn1 >= 2000 || mtn2 >= 2000) && (mtn1*5 < mtn2*4 || mtn2*5 < mtn1*4)) ||
603
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1 times.
14 ((mtn1 >= 4000 || mtn2 >= 4000) && c2 > c1))
604
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 65 times.
114 ret = mtn1 > mtn2 ? match2 : match1;
605
5/8
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
11 else if (mr > 0.005 && FFMAX(mtn1, mtn2) > 150 && (mtn1*2 < mtn2*1 || mtn2*2 < mtn1*1))
606 ret = mtn1 > mtn2 ? match2 : match1;
607 else
608
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 ret = norm1 > norm2 ? match2 : match1;
609 125 return ret;
610 }
611
612 318 static void copy_fields(const FieldMatchContext *fm, AVFrame *dst,
613 const AVFrame *src, int field, int input)
614 {
615 int plane;
616
4/6
✓ Branch 0 taken 1272 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 954 times.
✓ Branch 3 taken 318 times.
✓ Branch 4 taken 954 times.
✗ Branch 5 not taken.
1272 for (plane = 0; plane < 4 && src->data[plane] && src->linesize[plane]; plane++) {
617 954 const int plane_h = get_height(fm, src, plane, input);
618
2/2
✓ Branch 0 taken 477 times.
✓ Branch 1 taken 477 times.
954 const int nb_copy_fields = (plane_h >> 1) + (field ? 0 : (plane_h & 1));
619 954 av_image_copy_plane(dst->data[plane] + field*dst->linesize[plane], dst->linesize[plane] << 1,
620 954 src->data[plane] + field*src->linesize[plane], src->linesize[plane] << 1,
621 954 get_width(fm, src, plane, input) * fm->bpc, nb_copy_fields);
622 }
623 318 }
624
625 159 static AVFrame *create_weave_frame(AVFilterContext *ctx, int match, int field,
626 const AVFrame *prv, AVFrame *src, const AVFrame *nxt, int input)
627 {
628 AVFrame *dst;
629 159 FieldMatchContext *fm = ctx->priv;
630
631
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 159 times.
159 if (match == mC) {
632 dst = av_frame_clone(src);
633 } else {
634
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 159 times.
159 AVFilterLink *link = input == INPUT_CLEANSRC ? ctx->outputs[0] : ctx->inputs[INPUT_MAIN];
635
636 159 dst = ff_get_video_buffer(link, link->w, link->h);
637
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 159 times.
159 if (!dst)
638 return NULL;
639 159 av_frame_copy_props(dst, src);
640
641
2/5
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 110 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
159 switch (match) {
642 49 case mP: copy_fields(fm, dst, src, 1-field, input); copy_fields(fm, dst, prv, field, input); break;
643 110 case mN: copy_fields(fm, dst, src, 1-field, input); copy_fields(fm, dst, nxt, field, input); break;
644 case mB: copy_fields(fm, dst, src, field, input); copy_fields(fm, dst, prv, 1-field, input); break;
645 case mU: copy_fields(fm, dst, src, field, input); copy_fields(fm, dst, nxt, 1-field, input); break;
646 default: av_assert0(0);
647 }
648 }
649 159 return dst;
650 }
651
652 110 static int checkmm(AVFilterContext *ctx, int *combs, int m1, int m2,
653 AVFrame **gen_frames, int field)
654 {
655 110 const FieldMatchContext *fm = ctx->priv;
656
657 #define LOAD_COMB(mid) do { \
658 if (combs[mid] < 0) { \
659 if (!gen_frames[mid]) \
660 gen_frames[mid] = create_weave_frame(ctx, mid, field, \
661 fm->prv, fm->src, fm->nxt, \
662 INPUT_MAIN); \
663 combs[mid] = calc_combed_score(fm, gen_frames[mid]); \
664 } \
665 } while (0)
666
667
3/4
✓ Branch 0 taken 110 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
✓ Branch 3 taken 61 times.
110 LOAD_COMB(m1);
668
2/4
✓ Branch 0 taken 110 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 110 times.
✗ Branch 3 not taken.
110 LOAD_COMB(m2);
669
670
2/6
✓ Branch 0 taken 110 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 110 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
110 if ((combs[m2] * 3 < combs[m1] || (combs[m2] * 2 < combs[m1] && combs[m1] > fm->combpel)) &&
671 abs(combs[m2] - combs[m1]) >= 30 && combs[m2] < fm->combpel)
672 return m2;
673 else
674 110 return m1;
675 }
676
677 static const int fxo0m[] = { mP, mC, mN, mB, mU };
678 static const int fxo1m[] = { mN, mC, mP, mU, mB };
679
680 130 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
681 {
682 130 AVFilterContext *ctx = inlink->dst;
683 130 AVFilterLink *outlink = ctx->outputs[0];
684 130 FieldMatchContext *fm = ctx->priv;
685 130 int combs[] = { -1, -1, -1, -1, -1 };
686 130 int order, field, i, match, interlaced_frame, sc = 0, ret = 0;
687 const int *fxo;
688 130 AVFrame *gen_frames[] = { NULL, NULL, NULL, NULL, NULL };
689 130 AVFrame *dst = NULL;
690
691 /* update frames queue(s) */
692 #define SLIDING_FRAME_WINDOW(prv, src, nxt) do { \
693 if (prv != src) /* 2nd loop exception (1st has prv==src and we don't want to loose src) */ \
694 av_frame_free(&prv); \
695 prv = src; \
696 src = nxt; \
697 if (in) \
698 nxt = in; \
699 if (!prv) \
700 prv = src; \
701 if (!prv) /* received only one frame at that point */ \
702 return 0; \
703 av_assert0(prv && src && nxt); \
704 } while (0)
705
1/2
✓ Branch 0 taken 130 times.
✗ Branch 1 not taken.
130 if (FF_INLINK_IDX(inlink) == INPUT_MAIN) {
706
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
130 av_assert0(fm->got_frame[INPUT_MAIN] == 0);
707
10/14
✓ Branch 0 taken 115 times.
✓ Branch 1 taken 15 times.
✓ Branch 3 taken 130 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 10 times.
✓ Branch 6 taken 120 times.
✓ Branch 7 taken 5 times.
✓ Branch 8 taken 125 times.
✓ Branch 9 taken 125 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 125 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 14 taken 125 times.
130 SLIDING_FRAME_WINDOW(fm->prv, fm->src, fm->nxt);
708 125 fm->got_frame[INPUT_MAIN] = 1;
709 } else {
710 av_assert0(fm->got_frame[INPUT_CLEANSRC] == 0);
711 SLIDING_FRAME_WINDOW(fm->prv2, fm->src2, fm->nxt2);
712 fm->got_frame[INPUT_CLEANSRC] = 1;
713 }
714
2/6
✓ Branch 0 taken 125 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 125 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
125 if (!fm->got_frame[INPUT_MAIN] || (fm->ppsrc && !fm->got_frame[INPUT_CLEANSRC]))
715 return 0;
716 125 fm->got_frame[INPUT_MAIN] = fm->got_frame[INPUT_CLEANSRC] = 0;
717 125 in = fm->src;
718
719 /* parity */
720
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
250 order = fm->order != FM_PARITY_AUTO ? fm->order : ((in->flags & AV_FRAME_FLAG_INTERLACED) ?
721
3/4
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 90 times.
✗ Branch 3 not taken.
125 !!(in->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST) : 1);
722
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
125 field = fm->field != FM_PARITY_AUTO ? fm->field : order;
723
2/8
✓ Branch 0 taken 125 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 125 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
125 av_assert0(order == 0 || order == 1 || field == 0 || field == 1);
724
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
125 fxo = field ^ order ? fxo1m : fxo0m;
725
726 /* debug mode: we generate all the fields combinations and their associated
727 * combed score. XXX: inject as frame metadata? */
728
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
125 if (fm->combdbg) {
729 for (i = 0; i < FF_ARRAY_ELEMS(combs); i++) {
730 if (i > mN && fm->combdbg == COMBDBG_PCN)
731 break;
732 gen_frames[i] = create_weave_frame(ctx, i, field, fm->prv, fm->src, fm->nxt, INPUT_MAIN);
733 if (!gen_frames[i]) {
734 ret = AVERROR(ENOMEM);
735 goto fail;
736 }
737 combs[i] = calc_combed_score(fm, gen_frames[i]);
738 }
739 av_log(ctx, AV_LOG_INFO, "COMBS: %3d %3d %3d %3d %3d\n",
740 combs[0], combs[1], combs[2], combs[3], combs[4]);
741 } else {
742 125 gen_frames[mC] = av_frame_clone(fm->src);
743
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
125 if (!gen_frames[mC]) {
744 ret = AVERROR(ENOMEM);
745 goto fail;
746 }
747 }
748
749 /* p/c selection and optional 3-way p/c/n matches */
750 125 match = compare_fields(fm, fxo[mC], fxo[mP], field);
751
2/4
✓ Branch 0 taken 125 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 125 times.
125 if (fm->mode == MODE_PCN || fm->mode == MODE_PCN_UB)
752 match = compare_fields(fm, match, fxo[mN], field);
753
754 /* scene change check */
755
1/2
✓ Branch 0 taken 125 times.
✗ Branch 1 not taken.
125 if (fm->combmatch == COMBMATCH_SC) {
756
2/2
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 70 times.
125 if (fm->lastn == outlink->frame_count_in - 1) {
757
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 10 times.
55 if (fm->lastscdiff > fm->scthresh)
758 45 sc = 1;
759
2/2
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 50 times.
70 } else if (luma_abs_diff(fm->prv, fm->src) > fm->scthresh) {
760 20 sc = 1;
761 }
762
763
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 65 times.
125 if (!sc) {
764 60 fm->lastn = outlink->frame_count_in;
765 60 fm->lastscdiff = luma_abs_diff(fm->src, fm->nxt);
766 60 sc = fm->lastscdiff > fm->scthresh;
767 }
768 }
769
770
4/6
✓ Branch 0 taken 125 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 125 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 110 times.
✓ Branch 5 taken 15 times.
125 if (fm->combmatch == COMBMATCH_FULL || (fm->combmatch == COMBMATCH_SC && sc)) {
771
1/7
✗ Branch 0 not taken.
✓ Branch 1 taken 110 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
110 switch (fm->mode) {
772 /* 2-way p/c matches */
773 case MODE_PC:
774 match = checkmm(ctx, combs, match, match == fxo[mP] ? fxo[mC] : fxo[mP], gen_frames, field);
775 break;
776 110 case MODE_PC_N:
777 110 match = checkmm(ctx, combs, match, fxo[mN], gen_frames, field);
778 110 break;
779 case MODE_PC_U:
780 match = checkmm(ctx, combs, match, fxo[mU], gen_frames, field);
781 break;
782 case MODE_PC_N_UB:
783 match = checkmm(ctx, combs, match, fxo[mN], gen_frames, field);
784 match = checkmm(ctx, combs, match, fxo[mU], gen_frames, field);
785 match = checkmm(ctx, combs, match, fxo[mB], gen_frames, field);
786 break;
787 /* 3-way p/c/n matches */
788 case MODE_PCN:
789 match = checkmm(ctx, combs, match, match == fxo[mP] ? fxo[mC] : fxo[mP], gen_frames, field);
790 break;
791 case MODE_PCN_UB:
792 match = checkmm(ctx, combs, match, fxo[mU], gen_frames, field);
793 match = checkmm(ctx, combs, match, fxo[mB], gen_frames, field);
794 break;
795 default:
796 av_assert0(0);
797 }
798 }
799
800 /* keep fields as-is if not matched properly */
801 125 interlaced_frame = combs[match] >= fm->combpel;
802
3/4
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 110 times.
125 if (interlaced_frame && fm->combmatch == COMBMATCH_FULL) {
803 match = mC;
804 }
805
806 /* get output frame and drop the others */
807
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
125 if (fm->ppsrc) {
808 /* field matching was based on a filtered/post-processed input, we now
809 * pick the untouched fields from the clean source */
810 dst = create_weave_frame(ctx, match, field, fm->prv2, fm->src2, fm->nxt2, INPUT_CLEANSRC);
811 } else {
812
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
125 if (!gen_frames[match]) { // XXX: is that possible?
813 dst = create_weave_frame(ctx, match, field, fm->prv, fm->src, fm->nxt, INPUT_MAIN);
814 } else {
815 125 dst = gen_frames[match];
816 125 gen_frames[match] = NULL;
817 }
818 }
819
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
125 if (!dst) {
820 ret = AVERROR(ENOMEM);
821 goto fail;
822 }
823
824 /* mark the frame we are unable to match properly as interlaced so a proper
825 * de-interlacer can take the relay */
826 #if FF_API_INTERLACED_FRAME
827 FF_DISABLE_DEPRECATION_WARNINGS
828 125 dst->interlaced_frame = interlaced_frame;
829 FF_ENABLE_DEPRECATION_WARNINGS
830 #endif
831
2/2
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 15 times.
125 if (interlaced_frame) {
832 110 dst->flags |= AV_FRAME_FLAG_INTERLACED;
833 110 av_log(ctx, AV_LOG_WARNING, "Frame #%"PRId64" at %s is still interlaced\n",
834 110 outlink->frame_count_in, av_ts2timestr(in->pts, &inlink->time_base));
835 #if FF_API_INTERLACED_FRAME
836 FF_DISABLE_DEPRECATION_WARNINGS
837 110 dst->top_field_first = field;
838 FF_ENABLE_DEPRECATION_WARNINGS
839 #endif
840
1/2
✓ Branch 0 taken 110 times.
✗ Branch 1 not taken.
110 if (field)
841 110 dst->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
842 else
843 dst->flags &= ~AV_FRAME_FLAG_TOP_FIELD_FIRST;
844 } else
845 15 dst->flags &= ~AV_FRAME_FLAG_INTERLACED;
846
847 125 av_log(ctx, AV_LOG_DEBUG, "SC:%d | COMBS: %3d %3d %3d %3d %3d (combpel=%d)"
848 " match=%d combed=%s\n", sc, combs[0], combs[1], combs[2], combs[3], combs[4],
849
2/2
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 15 times.
125 fm->combpel, match, (dst->flags & AV_FRAME_FLAG_INTERLACED) ? "YES" : "NO");
850
851 125 fail:
852
2/2
✓ Branch 0 taken 625 times.
✓ Branch 1 taken 125 times.
750 for (i = 0; i < FF_ARRAY_ELEMS(gen_frames); i++)
853 625 av_frame_free(&gen_frames[i]);
854
855
1/2
✓ Branch 0 taken 125 times.
✗ Branch 1 not taken.
125 if (ret >= 0)
856 125 return ff_filter_frame(outlink, dst);
857 return ret;
858 }
859
860 230 static int activate(AVFilterContext *ctx)
861 {
862 230 FieldMatchContext *fm = ctx->priv;
863 230 AVFrame *frame = NULL;
864 230 int ret = 0, status;
865 int64_t pts;
866
867
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 230 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
230 FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx);
868
869
3/4
✓ Branch 0 taken 230 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 130 times.
✓ Branch 3 taken 100 times.
460 if ((fm->got_frame[INPUT_MAIN] == 0) &&
870 230 (ret = ff_inlink_consume_frame(ctx->inputs[INPUT_MAIN], &frame)) > 0) {
871 130 ret = filter_frame(ctx->inputs[INPUT_MAIN], frame);
872
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
130 if (ret < 0)
873 return ret;
874 }
875
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 230 times.
230 if (ret < 0)
876 return ret;
877
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 230 times.
230 if (fm->ppsrc &&
878 (fm->got_frame[INPUT_CLEANSRC] == 0) &&
879 (ret = ff_inlink_consume_frame(ctx->inputs[INPUT_CLEANSRC], &frame)) > 0) {
880 ret = filter_frame(ctx->inputs[INPUT_CLEANSRC], frame);
881 if (ret < 0)
882 return ret;
883 }
884
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 230 times.
230 if (ret < 0) {
885 return ret;
886
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 230 times.
230 } else if (ff_inlink_acknowledge_status(ctx->inputs[INPUT_MAIN], &status, &pts)) {
887 if (status == AVERROR_EOF) { // flushing
888 fm->eof |= 1 << INPUT_MAIN;
889 ret = filter_frame(ctx->inputs[INPUT_MAIN], NULL);
890 }
891 ff_outlink_set_status(ctx->outputs[0], status, pts);
892 return ret;
893
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 230 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
230 } else if (fm->ppsrc && ff_inlink_acknowledge_status(ctx->inputs[INPUT_CLEANSRC], &status, &pts)) {
894 if (status == AVERROR_EOF) { // flushing
895 fm->eof |= 1 << INPUT_CLEANSRC;
896 ret = filter_frame(ctx->inputs[INPUT_CLEANSRC], NULL);
897 }
898 ff_outlink_set_status(ctx->outputs[0], status, pts);
899 return ret;
900 } else {
901
2/2
✓ Branch 1 taken 100 times.
✓ Branch 2 taken 130 times.
230 if (ff_outlink_frame_wanted(ctx->outputs[0])) {
902
1/2
✓ Branch 0 taken 100 times.
✗ Branch 1 not taken.
100 if (fm->got_frame[INPUT_MAIN] == 0)
903 100 ff_inlink_request_frame(ctx->inputs[INPUT_MAIN]);
904
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 100 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
100 if (fm->ppsrc && (fm->got_frame[INPUT_CLEANSRC] == 0))
905 ff_inlink_request_frame(ctx->inputs[INPUT_CLEANSRC]);
906 }
907 230 return 0;
908 }
909 }
910
911 6 static int query_formats(AVFilterContext *ctx)
912 {
913 6 FieldMatchContext *fm = ctx->priv;
914
915 static const enum AVPixelFormat pix_fmts[] = {
916 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
917 AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
918 AV_PIX_FMT_NONE
919 };
920 static const enum AVPixelFormat unproc_pix_fmts[] = {
921 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
922 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
923 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
924 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
925 AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
926 AV_PIX_FMT_YUVJ411P,
927 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
928 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
929 AV_PIX_FMT_YUV440P10,
930 AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
931 AV_PIX_FMT_YUV440P12,
932 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
933 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
934 AV_PIX_FMT_NONE
935 };
936 int ret;
937
938 6 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
939
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!fmts_list)
940 return AVERROR(ENOMEM);
941
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (!fm->ppsrc) {
942 6 return ff_set_common_formats(ctx, fmts_list);
943 }
944
945 if ((ret = ff_formats_ref(fmts_list, &ctx->inputs[INPUT_MAIN]->outcfg.formats)) < 0)
946 return ret;
947 fmts_list = ff_make_format_list(unproc_pix_fmts);
948 if (!fmts_list)
949 return AVERROR(ENOMEM);
950 if ((ret = ff_formats_ref(fmts_list, &ctx->outputs[0]->incfg.formats)) < 0)
951 return ret;
952 if ((ret = ff_formats_ref(fmts_list, &ctx->inputs[INPUT_CLEANSRC]->outcfg.formats)) < 0)
953 return ret;
954 return 0;
955 }
956
957 5 static int config_input(AVFilterLink *inlink)
958 {
959 int ret;
960 5 AVFilterContext *ctx = inlink->dst;
961 5 FieldMatchContext *fm = ctx->priv;
962 5 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
963 5 const int w = inlink->w;
964 5 const int h = inlink->h;
965
966 5 fm->scthresh = (int64_t)((w * h * 255.0 * fm->scthresh_flt) / 100.0);
967
968
2/4
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
10 if ((ret = av_image_alloc(fm->map_data, fm->map_linesize, w, h, inlink->format, 32)) < 0 ||
969 5 (ret = av_image_alloc(fm->cmask_data, fm->cmask_linesize, w, h, inlink->format, 32)) < 0)
970 return ret;
971
972 5 fm->hsub[INPUT_MAIN] = pix_desc->log2_chroma_w;
973 5 fm->vsub[INPUT_MAIN] = pix_desc->log2_chroma_h;
974
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (fm->ppsrc) {
975 pix_desc = av_pix_fmt_desc_get(ctx->inputs[INPUT_CLEANSRC]->format);
976 fm->hsub[INPUT_CLEANSRC] = pix_desc->log2_chroma_w;
977 fm->vsub[INPUT_CLEANSRC] = pix_desc->log2_chroma_h;
978 }
979
980 5 fm->tpitchy = FFALIGN(w, 16);
981 5 fm->tpitchuv = FFALIGN(w >> 1, 16);
982
983 5 fm->tbuffer = av_calloc((h/2 + 4) * fm->tpitchy, sizeof(*fm->tbuffer));
984 10 fm->c_array = av_malloc_array((((w + fm->blockx/2)/fm->blockx)+1) *
985 5 (((h + fm->blocky/2)/fm->blocky)+1),
986 4 * sizeof(*fm->c_array));
987
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
5 if (!fm->tbuffer || !fm->c_array)
988 return AVERROR(ENOMEM);
989
990 5 return 0;
991 }
992
993 11 static av_cold int fieldmatch_init(AVFilterContext *ctx)
994 {
995 11 const FieldMatchContext *fm = ctx->priv;
996 11 AVFilterPad pad = {
997 .name = "main",
998 .type = AVMEDIA_TYPE_VIDEO,
999 .config_props = config_input,
1000 };
1001 int ret;
1002
1003
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
11 if ((ret = ff_append_inpad(ctx, &pad)) < 0)
1004 return ret;
1005
1006
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (fm->ppsrc) {
1007 pad.name = "clean_src";
1008 pad.config_props = NULL;
1009 if ((ret = ff_append_inpad(ctx, &pad)) < 0)
1010 return ret;
1011 }
1012
1013
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 if ((fm->blockx & (fm->blockx - 1)) ||
1014
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 (fm->blocky & (fm->blocky - 1))) {
1015 av_log(ctx, AV_LOG_ERROR, "blockx and blocky settings must be power of two\n");
1016 return AVERROR(EINVAL);
1017 }
1018
1019
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (fm->combpel > fm->blockx * fm->blocky) {
1020 av_log(ctx, AV_LOG_ERROR, "Combed pixel should not be larger than blockx x blocky\n");
1021 return AVERROR(EINVAL);
1022 }
1023
1024 11 return 0;
1025 }
1026
1027 11 static av_cold void fieldmatch_uninit(AVFilterContext *ctx)
1028 {
1029 11 FieldMatchContext *fm = ctx->priv;
1030
1031
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 6 times.
11 if (fm->prv != fm->src)
1032 5 av_frame_free(&fm->prv);
1033
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 6 times.
11 if (fm->nxt != fm->src)
1034 5 av_frame_free(&fm->nxt);
1035
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (fm->prv2 != fm->src2)
1036 av_frame_free(&fm->prv2);
1037
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (fm->nxt2 != fm->src2)
1038 av_frame_free(&fm->nxt2);
1039 11 av_frame_free(&fm->src);
1040 11 av_frame_free(&fm->src2);
1041 11 av_freep(&fm->map_data[0]);
1042 11 av_freep(&fm->cmask_data[0]);
1043 11 av_freep(&fm->tbuffer);
1044 11 av_freep(&fm->c_array);
1045 11 }
1046
1047 5 static int config_output(AVFilterLink *outlink)
1048 {
1049 5 AVFilterContext *ctx = outlink->src;
1050 5 FieldMatchContext *fm = ctx->priv;
1051 5 const AVFilterLink *inlink =
1052
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 ctx->inputs[fm->ppsrc ? INPUT_CLEANSRC : INPUT_MAIN];
1053 5 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
1054
1055 5 fm->bpc = (desc->comp[0].depth + 7) / 8;
1056 5 outlink->time_base = inlink->time_base;
1057 5 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
1058 5 outlink->frame_rate = inlink->frame_rate;
1059 5 outlink->w = inlink->w;
1060 5 outlink->h = inlink->h;
1061 5 return 0;
1062 }
1063
1064 static const AVFilterPad fieldmatch_outputs[] = {
1065 {
1066 .name = "default",
1067 .type = AVMEDIA_TYPE_VIDEO,
1068 .config_props = config_output,
1069 },
1070 };
1071
1072 const AVFilter ff_vf_fieldmatch = {
1073 .name = "fieldmatch",
1074 .description = NULL_IF_CONFIG_SMALL("Field matching for inverse telecine."),
1075 .priv_size = sizeof(FieldMatchContext),
1076 .init = fieldmatch_init,
1077 .activate = activate,
1078 .uninit = fieldmatch_uninit,
1079 .inputs = NULL,
1080 FILTER_OUTPUTS(fieldmatch_outputs),
1081 FILTER_QUERY_FUNC(query_formats),
1082 .priv_class = &fieldmatch_class,
1083 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
1084 };
1085