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
42
44 #define INPUT_CLEANSRC 1
45
50 };
51
60 };
61
67 };
68
74 };
75
78
81 int got_frame[2];
///< frame request flag for each input stream
82 int hsub[2],
vsub[2];
///< chroma subsampling values
83 int bpc;
///< bytes per component
84 uint32_t
eof;
///< bitmask for end of stream
87
88 /* options */
91 int mode;
///< matching_mode
103
104 /* misc buffers */
113
114 #define OFFSET(x) offsetof(FieldMatchContext, x)
115 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
116
126 {
"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,
"mode" },
129 {
"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 },
134 {
"mchroma",
"set whether or not chroma is included during the match comparisons",
OFFSET(mchroma),
AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1,
FLAGS },
135 {
"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 },
136 {
"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 },
146 {
"cthresh",
"set the area combing threshold used for combed frame detection",
OFFSET(cthresh),
AV_OPT_TYPE_INT, {.i64= 9}, -1, 0xff,
FLAGS },
148 {
"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 },
149 {
"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 },
150 {
"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 },
152 };
153
155
157 {
159 }
160
162 {
164 }
165
167 {
168 int x, y;
169 const uint8_t *srcp1 = f1->
data[0];
170 const uint8_t *srcp2 = f2->
data[0];
171 const int src1_linesize = f1->
linesize[0];
172 const int src2_linesize = f2->
linesize[0];
176
177 for (y = 0; y <
height; y++) {
178 for (x = 0; x <
width; x++)
179 acc +=
abs(srcp1[x] - srcp2[x]);
180 srcp1 += src1_linesize;
181 srcp2 += src2_linesize;
182 }
184 }
185
187 {
188 int y;
189
190 for (y = 0; y <
h; y++) {
193 }
194 }
195
197 {
198 int x, y, plane, max_v = 0;
199 const int cthresh = fm->
cthresh;
200 const int cthresh6 = cthresh * 6;
201
202 for (plane = 0; plane < (fm->
chroma ? 3 : 1); plane++) {
203 const uint8_t *srcp =
src->data[plane];
204 const int src_linesize =
src->linesize[plane];
209
210 if (cthresh < 0) {
212 continue;
213 }
215
216 /* [1 -3 4 -3 1] vertical filter */
217 #define FILTER(xm2, xm1, xp1, xp2) \
218 abs( 4 * srcp[x] \
219 -3 * (srcp[x + (xm1)*src_linesize] + srcp[x + (xp1)*src_linesize]) \
220 + (srcp[x + (xm2)*src_linesize] + srcp[x + (xp2)*src_linesize])) > cthresh6
221
222 /* first line */
223 for (x = 0; x <
width; x++) {
224 const int s1 =
abs(srcp[x] - srcp[x + src_linesize]);
225 if (
s1 > cthresh &&
FILTER(2, 1, 1, 2))
226 cmkp[x] = 0xff;
227 }
228 srcp += src_linesize;
229 cmkp += cmk_linesize;
230
231 /* second line */
232 for (x = 0; x <
width; x++) {
233 const int s1 =
abs(srcp[x] - srcp[x - src_linesize]);
234 const int s2 =
abs(srcp[x] - srcp[x + src_linesize]);
235 if (
s1 > cthresh &&
s2 > cthresh &&
FILTER(2, -1, 1, 2))
236 cmkp[x] = 0xff;
237 }
238 srcp += src_linesize;
239 cmkp += cmk_linesize;
240
241 /* all lines minus first two and last two */
242 for (y = 2; y <
height-2; y++) {
243 for (x = 0; x <
width; x++) {
244 const int s1 =
abs(srcp[x] - srcp[x - src_linesize]);
245 const int s2 =
abs(srcp[x] - srcp[x + src_linesize]);
246 if (
s1 > cthresh &&
s2 > cthresh &&
FILTER(-2, -1, 1, 2))
247 cmkp[x] = 0xff;
248 }
249 srcp += src_linesize;
250 cmkp += cmk_linesize;
251 }
252
253 /* before-last line */
254 for (x = 0; x <
width; x++) {
255 const int s1 =
abs(srcp[x] - srcp[x - src_linesize]);
256 const int s2 =
abs(srcp[x] - srcp[x + src_linesize]);
257 if (
s1 > cthresh &&
s2 > cthresh &&
FILTER(-2, -1, 1, -2))
258 cmkp[x] = 0xff;
259 }
260 srcp += src_linesize;
261 cmkp += cmk_linesize;
262
263 /* last line */
264 for (x = 0; x <
width; x++) {
265 const int s1 =
abs(srcp[x] - srcp[x - src_linesize]);
266 if (
s1 > cthresh &&
FILTER(-2, -1, -1, -2))
267 cmkp[x] = 0xff;
268 }
269 }
270
279 uint8_t *cmkpp = cmkp - (cmk_linesize>>1);
280 uint8_t *cmkpn = cmkp + (cmk_linesize>>1);
281 uint8_t *cmkpnn = cmkp + cmk_linesize;
282 for (y = 1; y <
height - 1; y++) {
283 cmkpp += cmk_linesize;
284 cmkp += cmk_linesize;
285 cmkpn += cmk_linesize;
286 cmkpnn += cmk_linesize;
287 cmkpV += cmk_linesizeUV;
288 cmkpU += cmk_linesizeUV;
289 for (x = 1; x <
width - 1; x++) {
290 #define HAS_FF_AROUND(p, lz) (p[(x)-1 - (lz)] == 0xff || p[(x) - (lz)] == 0xff || p[(x)+1 - (lz)] == 0xff || \
291 p[(x)-1 ] == 0xff || p[(x)+1 ] == 0xff || \
292 p[(x)-1 + (lz)] == 0xff || p[(x) + (lz)] == 0xff || p[(x)+1 + (lz)] == 0xff)
293 if ((cmkpV[x] == 0xff &&
HAS_FF_AROUND(cmkpV, cmk_linesizeUV)) ||
294 (cmkpU[x] == 0xff &&
HAS_FF_AROUND(cmkpU, cmk_linesizeUV))) {
295 ((uint16_t*)cmkp)[x] = 0xffff;
296 ((uint16_t*)cmkpn)[x] = 0xffff;
297 if (y&1) ((uint16_t*)cmkpp)[x] = 0xffff;
298 else ((uint16_t*)cmkpnn)[x] = 0xffff;
299 }
300 }
301 }
302 }
303
304 {
305 const int blockx = fm->
blockx;
306 const int blocky = fm->
blocky;
307 const int xhalf = blockx/2;
308 const int yhalf = blocky/2;
310 const uint8_t *cmkp = fm->
cmask_data[0] + cmk_linesize;
313 const int xblocks = ((
width+xhalf)/blockx) + 1;
314 const int xblocks4 = xblocks<<2;
315 const int yblocks = ((
height+yhalf)/blocky) + 1;
317 const int arraysize = (xblocks*yblocks)<<2;
318 int heighta = (
height/(blocky/2))*(blocky/2);
319 const int widtha = (
width /(blockx/2))*(blockx/2);
322 memset(c_array, 0, arraysize * sizeof(*c_array));
323
324 #define C_ARRAY_ADD(v) do { \
325 const int box1 = (x / blockx) * 4; \
326 const int box2 = ((x + xhalf) / blockx) * 4; \
327 c_array[temp1 + box1 ] += v; \
328 c_array[temp1 + box2 + 1] += v; \
329 c_array[temp2 + box1 + 2] += v; \
330 c_array[temp2 + box2 + 3] += v; \
331 } while (0)
332
333 #define VERTICAL_HALF(y_start, y_end) do { \
334 for (y = y_start; y < y_end; y++) { \
335 const int temp1 = (y / blocky) * xblocks4; \
336 const int temp2 = ((y + yhalf) / blocky) * xblocks4; \
337 for (x = 0; x < width; x++) \
338 if (cmkp[x - cmk_linesize] == 0xff && \
339 cmkp[x ] == 0xff && \
340 cmkp[x + cmk_linesize] == 0xff) \
341 C_ARRAY_ADD(1); \
342 cmkp += cmk_linesize; \
343 } \
344 } while (0)
345
347
348 for (y = yhalf; y < heighta; y += yhalf) {
349 const int temp1 = (y / blocky) * xblocks4;
350 const int temp2 = ((y + yhalf) / blocky) * xblocks4;
351
352 for (x = 0; x < widtha; x += xhalf) {
353 const uint8_t *cmkp_tmp = cmkp + x;
355 for (
u = 0;
u < yhalf;
u++) {
356 for (v = 0; v < xhalf; v++)
357 if (cmkp_tmp[v - cmk_linesize] == 0xff &&
358 cmkp_tmp[v ] == 0xff &&
359 cmkp_tmp[v + cmk_linesize] == 0xff)
360 sum++;
361 cmkp_tmp += cmk_linesize;
362 }
363 if (sum)
365 }
366
367 for (x = widtha; x <
width; x++) {
368 const uint8_t *cmkp_tmp = cmkp + x;
370 for (
u = 0;
u < yhalf;
u++) {
371 if (cmkp_tmp[-cmk_linesize] == 0xff &&
372 cmkp_tmp[ 0] == 0xff &&
373 cmkp_tmp[ cmk_linesize] == 0xff)
374 sum++;
375 cmkp_tmp += cmk_linesize;
376 }
377 if (sum)
379 }
380
381 cmkp += cmk_linesize * yhalf;
382 }
383
385
386 for (x = 0; x < arraysize; x++)
387 if (c_array[x] > max_v)
388 max_v = c_array[x];
389 }
390 return max_v;
391 }
392
393 // the secret is that tbuffer is an interlaced, offset subset of all the lines
395 const uint8_t *nxtp, int nxt_linesize,
396 uint8_t *tbuffer, int tbuf_linesize,
398 {
399 int y, x;
400
401 prvp -= prv_linesize;
402 nxtp -= nxt_linesize;
403 for (y = 0; y <
height; y++) {
404 for (x = 0; x <
width; x++)
405 tbuffer[x] =
FFABS(prvp[x] - nxtp[x]);
406 prvp += prv_linesize;
407 nxtp += nxt_linesize;
408 tbuffer += tbuf_linesize;
409 }
410 }
411
412 /**
413 * Build a map over which pixels differ a lot/a little
414 */
416 const uint8_t *prvp, int prv_linesize,
417 const uint8_t *nxtp, int nxt_linesize,
418 uint8_t *dstp,
int dst_linesize,
int height,
419 int width,
int plane)
420 {
421 int x, y,
u,
diff, count;
423 const uint8_t *dp = fm->
tbuffer + tpitch;
424
427
428 for (y = 2; y <
height - 2; y += 2) {
429 for (x = 1; x <
width - 1; x++) {
432 for (count = 0,
u = x-1;
u < x+2 && count < 2;
u++) {
433 count += dp[
u-tpitch] > 3;
435 count += dp[
u+tpitch] > 3;
436 }
437 if (count > 1) {
438 dstp[x] = 1;
440 int upper = 0, lower = 0;
441 for (count = 0,
u = x-1;
u < x+2 && count < 6;
u++) {
442 if (dp[
u-tpitch] > 19) { count++; upper = 1; }
443 if (dp[
u ] > 19) count++;
444 if (dp[
u+tpitch] > 19) { count++; lower = 1; }
445 }
446 if (count > 3) {
447 if (upper && lower) {
448 dstp[x] |= 1<<1;
449 } else {
450 int upper2 = 0, lower2 = 0;
452 if (y != 2 && dp[
u-2*tpitch] > 19) upper2 = 1;
453 if ( dp[
u- tpitch] > 19) upper = 1;
454 if ( dp[
u+ tpitch] > 19) lower = 1;
455 if (y !=
height-4 && dp[
u+2*tpitch] > 19) lower2 = 1;
456 }
457 if ((upper && (lower || upper2)) ||
458 (lower && (upper || lower2)))
459 dstp[x] |= 1<<1;
460 else if (count > 5)
461 dstp[x] |= 1<<2;
462 }
463 }
464 }
465 }
466 }
467 }
468 dp += tpitch;
469 dstp += dst_linesize;
470 }
471 }
472
474
476 {
478 }
479
481 {
482 if (match ==
mP || match ==
mB)
return fm->
prv;
483 else if (match ==
mN || match ==
mU)
return fm->
nxt;
484 else /* match == mC */ return fm->
src;
485 }
486
488 {
490 uint64_t accumPc = 0, accumPm = 0, accumPml = 0;
491 uint64_t accumNc = 0, accumNm = 0, accumNml = 0;
492 int norm1, norm2, mtn1, mtn2;
495
496 for (plane = 0; plane < (fm->
mchroma ? 3 : 1); plane++) {
497 int x, y, temp1, temp2, fbase;
499 uint8_t *mapp = fm->
map_data[plane];
501 const uint8_t *srcp =
src->data[plane];
502 const int src_linesize =
src->linesize[plane];
503 const int srcf_linesize = src_linesize << 1;
504 int prv_linesize, nxt_linesize;
505 int prvf_linesize, nxtf_linesize;
511 const int stopx =
width - startx;
512 const uint8_t *srcpf, *srcf, *srcnf;
513 const uint8_t *prvpf, *prvnf, *nxtpf, *nxtnf;
514
516
517 /* match1 */
519 srcf = srcp + (fbase + 1) * src_linesize;
520 srcpf = srcf - srcf_linesize;
521 srcnf = srcf + srcf_linesize;
522 mapp = mapp + fbase * map_linesize;
524 prv_linesize = prev->
linesize[plane];
525 prvf_linesize = prv_linesize << 1;
526 prvpf = prev->
data[plane] + fbase * prv_linesize;
// previous frame, previous field
527 prvnf = prvpf + prvf_linesize; // previous frame, next field
528
529 /* match2 */
532 nxt_linesize = next->
linesize[plane];
533 nxtf_linesize = nxt_linesize << 1;
534 nxtpf = next->
data[plane] + fbase * nxt_linesize;
// next frame, previous field
535 nxtnf = nxtpf + nxtf_linesize; // next frame, next field
536
537 map_linesize <<= 1;
538 if ((match1 >= 3 &&
field == 1) || (match1 < 3 &&
field != 1))
541 else
543 mapp + map_linesize, map_linesize,
height,
width, plane);
544
545 for (y = 2; y <
height - 2; y += 2) {
546 if (y0a == y1a || y < y0a || y > y1a) {
547 for (x = startx; x < stopx; x++) {
548 if (mapp[x] > 0 || mapp[x + map_linesize] > 0) {
549 temp1 = srcpf[x] + (srcf[x] << 2) + srcnf[x]; // [1 4 1]
550
551 temp2 =
abs(3 * (prvpf[x] + prvnf[x]) - temp1);
552 if (temp2 > 23 && ((mapp[x]&1) || (mapp[x + map_linesize]&1)))
553 accumPc += temp2;
554 if (temp2 > 42) {
555 if ((mapp[x]&2) || (mapp[x + map_linesize]&2))
556 accumPm += temp2;
557 if ((mapp[x]&4) || (mapp[x + map_linesize]&4))
558 accumPml += temp2;
559 }
560
561 temp2 =
abs(3 * (nxtpf[x] + nxtnf[x]) - temp1);
562 if (temp2 > 23 && ((mapp[x]&1) || (mapp[x + map_linesize]&1)))
563 accumNc += temp2;
564 if (temp2 > 42) {
565 if ((mapp[x]&2) || (mapp[x + map_linesize]&2))
566 accumNm += temp2;
567 if ((mapp[x]&4) || (mapp[x + map_linesize]&4))
568 accumNml += temp2;
569 }
570 }
571 }
572 }
573 prvpf += prvf_linesize;
574 prvnf += prvf_linesize;
575 srcpf += srcf_linesize;
576 srcf += srcf_linesize;
577 srcnf += srcf_linesize;
578 nxtpf += nxtf_linesize;
579 nxtnf += nxtf_linesize;
580 mapp += map_linesize;
581 }
582 }
583
584 if (accumPm < 500 && accumNm < 500 && (accumPml >= 500 || accumNml >= 500) &&
585 FFMAX(accumPml,accumNml) > 3*
FFMIN(accumPml,accumNml)) {
586 accumPm = accumPml;
587 accumNm = accumNml;
588 }
589
590 norm1 = (
int)((accumPc / 6.0
f) + 0.5f);
591 norm2 = (
int)((accumNc / 6.0
f) + 0.5f);
592 mtn1 = (
int)((accumPm / 6.0
f) + 0.5f);
593 mtn2 = (
int)((accumNm / 6.0
f) + 0.5f);
597 if (((mtn1 >= 500 || mtn2 >= 500) && (mtn1*2 < mtn2*1 || mtn2*2 < mtn1*1)) ||
598 ((mtn1 >= 1000 || mtn2 >= 1000) && (mtn1*3 < mtn2*2 || mtn2*3 < mtn1*2)) ||
599 ((mtn1 >= 2000 || mtn2 >= 2000) && (mtn1*5 < mtn2*4 || mtn2*5 < mtn1*4)) ||
600 ((mtn1 >= 4000 || mtn2 >= 4000) &&
c2 >
c1))
601 ret = mtn1 > mtn2 ? match2 : match1;
602 else if (mr > 0.005 &&
FFMAX(mtn1, mtn2) > 150 && (mtn1*2 < mtn2*1 || mtn2*2 < mtn1*1))
603 ret = mtn1 > mtn2 ? match2 : match1;
604 else
605 ret = norm1 > norm2 ? match2 : match1;
607 }
608
611 {
612 int plane;
613 for (plane = 0; plane < 4 &&
src->data[plane] &&
src->linesize[plane]; plane++) {
615 const int nb_copy_fields = (plane_h >> 1) + (
field ? 0 : (plane_h & 1));
617 src->data[plane] +
field*
src->linesize[plane],
src->linesize[plane] << 1,
619 }
620 }
621
624 {
627
630 } else {
632
634 if (!dst)
637
638 switch (match) {
644 }
645 }
646 return dst;
647 }
648
651 {
653
654 #define LOAD_COMB(mid) do { \
655 if (combs[mid] < 0) { \
656 if (!gen_frames[mid]) \
657 gen_frames[mid] = create_weave_frame(ctx, mid, field, \
658 fm->prv, fm->src, fm->nxt, \
659 INPUT_MAIN); \
660 combs[mid] = calc_combed_score(fm, gen_frames[mid]); \
661 } \
662 } while (0)
663
666
667 if ((combs[m2] * 3 < combs[m1] || (combs[m2] * 2 < combs[m1] && combs[m1] > fm->
combpel)) &&
668 abs(combs[m2] - combs[m1]) >= 30 && combs[m2] < fm->
combpel)
669 return m2;
670 else
671 return m1;
672 }
673
676
678 {
682 int combs[] = { -1, -1, -1, -1, -1 };
683 int order,
field,
i, match, interlaced_frame, sc = 0,
ret = 0;
684 const int *fxo;
687
688 /* update frames queue(s) */
689 #define SLIDING_FRAME_WINDOW(prv, src, nxt) do { \
690 if (prv != src) /* 2nd loop exception (1st has prv==src and we don't want to loose src) */ \
691 av_frame_free(&prv); \
692 prv = src; \
693 src = nxt; \
694 if (in) \
695 nxt = in; \
696 if (!prv) \
697 prv = src; \
698 if (!prv) /* received only one frame at that point */ \
699 return 0; \
700 av_assert0(prv && src && nxt); \
701 } while (0)
706 } else {
710 }
712 return 0;
715
716 /* parity */
721
722 /* debug mode: we generate all the fields combinations and their associated
723 * combed score. XXX: inject as frame metadata? */
727 break;
729 if (!gen_frames[
i]) {
732 }
734 }
736 combs[0], combs[1], combs[2], combs[3], combs[4]);
737 } else {
739 if (!gen_frames[
mC]) {
742 }
743 }
744
745 /* p/c selection and optional 3-way p/c/n matches */
749
750 /* scene change check */
754 sc = 1;
756 sc = 1;
757 }
758
759 if (!sc) {
763 }
764 }
765
768 /* 2-way p/c matches */
771 break;
774 break;
777 break;
782 break;
783 /* 3-way p/c/n matches */
786 break;
790 break;
791 default:
793 }
794 }
795
796 /* keep fields as-is if not matched properly */
797 interlaced_frame = combs[match] >= fm->
combpel;
800 }
801
802 /* get output frame and drop the others */
804 /* field matching was based on a filtered/post-processed input, we now
805 * pick the untouched fields from the clean source */
807 } else {
808 if (!gen_frames[match]) { // XXX: is that possible?
810 } else {
811 dst = gen_frames[match];
812 gen_frames[match] =
NULL;
813 }
814 }
815 if (!dst) {
818 }
819
820 /* mark the frame we are unable to match properly as interlaced so a proper
821 * de-interlacer can take the relay */
827 }
828
830 " match=%d combed=%s\n", sc, combs[0], combs[1], combs[2], combs[3], combs[4],
832
836
840 }
841
843 {
848
850
856 }
865 }
872 }
879 }
882 } else {
888 }
889 return 0;
890 }
891 }
892
894 {
896
901 };
917 };
919
921 if (!fmts_list)
925 }
926
930 if (!fmts_list)
936 return 0;
937 }
938
940 {
947
949
953
960 }
961
964
971
972 return 0;
973 }
974
976 {
982 };
984
987
989 pad.
name =
"clean_src";
993 }
994
999 }
1000
1004 }
1005
1006 return 0;
1007 }
1008
1010 {
1012
1027 }
1028
1030 {
1036
1037 fm->
bpc = (
desc->comp[0].depth + 7) / 8;
1043 return 0;
1044 }
1045
1047 {
1051 },
1052 };
1053
1055 .
name =
"fieldmatch",
1064 .priv_class = &fieldmatch_class,
1066 };