summaryrefslogtreecommitdiffstats
path: root/debian/transcode/transcode-1.1.7/tools/tcyait.c
blob: 84882fa626d09f37c81c61c012af4457b7837e1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
/*
 *  tcyait.c
 *
 *  Copyright (C) Allan Snider - February 2007
 *
 *  This file is part of transcode, a video stream processing tool
 *
 *  transcode is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  transcode is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

/*
 *	tcyait:
 *		Yet Another Inverse Telecine filter.
 *
 *	Usage:
 *		tcyait [-dnlotbwmEO] [arg...]
 *			-d		Print debug information to stdout
 *			-n		Do not drop frames, always de-interlace
 *			-k		No forced keep frames
 *			-l log		Input yait log file name
 *			-o ops		Output yait frame ops file name
 *			-t thresh	Interlace detection threshold (>1)
 *			-b blend	forced blend threshold (>thresh)
 *			-w size		Drop frame look ahead window (0-20)
 *			-m mode		Transcode blend method (0-5)
 *			-E thresh	Even pattern threhold [thresh]
 *			-O thresh	Odd pattern threhold [thresh]
 *			-N noise	minimum normalized delta, else considered noise
 *
 *		By default, reads "yait.log" and produces "yait.ops".
 *
 *	Description:
 *
 *		Read a yait log file (generated via -J yait=log), and analyze it to
 *	produce a yait frame operations file.  The frame operations file contains
 *	commands to the yait filter to drop, copy or save rows (to de-interlace),
 *	or blend frames.  This will convert from NTSC 29.97 to 23.976 fps.  The file
 *	generated is used as input for another transcode pass (-J yait=ops).
 */


#define	YAIT_VERSION		"v0.2"

#define	TRUE			1
#define	FALSE			0

/* program defaults */
#define	Y_LOG_FN		"yait.log"	/* log file read */
#define	Y_OPS_FN		"yait.ops"	/* frame operation file written */
#define	Y_THRESH		1.2		/* even/odd ratio to detect interlace */
#define	Y_DROPWIN_SIZE		15		/* drop frame look ahead window */
#define	Y_DEINT_MODE		3		/* default transcode de-interlace mode */

/* limits */
#define	Y_DEINT_MIN		0
#define	Y_DEINT_MAX		5
#define	Y_DROPWIN_MIN		0
#define	Y_DROPWIN_MAX		60

#define	Y_MTHRESH		1.02		/* minimum ratio allowing de-interlace */
#define	Y_NOISE			0.003		/* normalized delta too weak, noise */
#define	Y_SCENE_CHANGE		0.15		/* normalized delta > 15% of max delta */

/* force de-interlace */
#define	Y_FBLEND		1.6		/* if ratio is > this */
#define	Y_FWEIGHT		0.01		/* if over this weight */

/* frame operation flags */
#define	Y_OP_ODD		0x10
#define	Y_OP_EVEN		0x20
#define	Y_OP_PAT		0x30

#define	Y_OP_NOP		0x0
#define	Y_OP_SAVE		0x1
#define	Y_OP_COPY		0x2
#define	Y_OP_DROP		0x4
#define	Y_OP_DEINT		0x8

/* group flags */
#define	Y_HAS_DROP		1
#define	Y_BANK_DROP		2
#define	Y_WITHDRAW_DROP		3
#define	Y_BORROW_DROP		4
#define	Y_RETURN_DROP		5
#define	Y_FORCE_DEINT		6
#define	Y_FORCE_DROP		7
#define	Y_FORCE_KEEP		8

/* frame information */
typedef struct fi_t Fi;
struct fi_t
	{
	double	r;		/* even/odd delta ratio, filtered */
	double	ro;		/* ratio, original value */
	double	w;		/* statistical strength */
	double	nd;		/* normalized delta */
	int	fn;		/* frame number */
	int	ed;		/* even row delta */
	int	od;		/* odd row delta */
	int	gi;		/* group array index */
	int	ip;		/* telecine pattern */
	int	op;		/* frame operation, nop, save/copy row */
	int	drop;		/* frame is to be dropped */
	int	gf;		/* group flag */
	Fi	*next;
	};


/*
 *	globals:
 */

char *Prog;			/* argv[0] */
char *LogFn;			/* log file name, default "yait.log" */
char *OpsFn;			/* ops file name, default "yait.ops" */
double Thresh;			/* row delta ratio interlace detection threshold */
double EThresh;			/* even interlace detection threshold */
double OThresh;			/* odd interlace detection threshold */
double Blend;			/* force frame blending over this threshold */
double Noise;			/* minimum normalized delta */
int DropWin;			/* drop frame look ahead window */
int DeintMode;			/* transcode de-interlace mode, (1-5) */
int DebugFi;			/* dump debug frame info */
int NoDrops;			/* force de-interlace everywhere (non vfr) */
int NoKeeps;			/* Don't force keep frames (allows a/v sync drift) */

FILE *LogFp;			/* log file */
FILE *OpsFp;			/* ops file */

Fi *Fl;				/* frame list */
Fi **Fa;			/* frame array */
Fi **Ga;			/* group array */
int *Da;			/* drop count array */
int Nf;				/* number of frames */
int Ng;				/* number of elements in Ga */
int Nd;				/* number of elements in Da */
int Md;				/* max delta */

int Stat_nd;			/* total number of dropped frames */
int Stat_nb;			/* total number of blended frames */
int Stat_no;			/* total number of odd interlaced pairs */
int Stat_ne;			/* total number of even interlaced pairs */
int Stat_fd;			/* total number of forced drops */
int Stat_fk;			/* total number of forced keeps */


/*
 *	protos:
 */

static void yait_parse_args( int, char** );
static void yait_chkac( int* );
static void yait_usage( void );
static void yait_read_log( void );
static double yait_calc_ratio( int, int );
static void yait_find_ip( void );
static void yait_chk_ip( int );
static void yait_chk_pairs( int );
static void yait_chk_tuplets( int );
static int yait_find_odd( double, int, double*, int );
static int yait_find_even( double, int, double*, int );
static int yait_ffmin( int, int );
static int yait_ffmax( int, int );
static int yait_m5( int );
static void yait_mark_grp( int, int, double );
static void yait_find_drops( void );
static int yait_cnt_drops( int );
static int yait_extra_drop( int );
static int yait_missing_drop( int );
static void yait_keep_frame( int );
static int yait_get_hdrop( int, int* );
static void yait_ivtc_keep( int );
static void yait_ivtc_grps( void );
static int yait_scan_bk( int );
static int yait_scan_fw( int );
static void yait_drop_frame( int );
static int yait_ivtc_grp( int, int, int );
static double yait_tst_ip( int, int );
static void yait_deint( void );
static void yait_write_ops( void );
static char *yait_write_op( Fi* );
static void yait_fini( void );

static void yait_debug_fi( void );
static char *yait_op( int op );
static char *yait_drop( Fi *f );
static char *yait_grp( int flg );


/*
 *	main:
 */

int
main( int argc, char **argv )
	{
	/* parse args */
	yait_parse_args( argc, argv );

	LogFp = fopen( LogFn, "r" );
	if( !LogFp )
		{
		perror( "fopen" );
		fprintf( stderr, "Cannot open YAIT delta log file (%s)\n", LogFn );
		exit( 1 );
		}

	OpsFp = fopen( OpsFn, "w" );
	if( !OpsFp )
		{
		perror( "fopen" );
		fprintf( stderr, "Cannot create YAIT frame ops file (%s)\n", OpsFn );
		exit( 1 );
		}

	/* read the log */
	yait_read_log();

	/* find interleave patterns */
	yait_find_ip();

	/* find drop frames */
	yait_find_drops();

	/* complete groups missing an interleave pattern */
	yait_ivtc_grps();

	/* let transcode de-interlace frames we missed */
	yait_deint();

	/* print frame ops file */
	yait_write_ops();

	if( DebugFi )
		yait_debug_fi();

	/* graceful exit */
	yait_fini();

	return( 0 );
	}


/*
 *	yait_parse_args:
 */

static void
yait_parse_args( int argc, char **argv )
	{
	int opt;
	char *p;

	LogFn = Y_LOG_FN;
	OpsFn = Y_OPS_FN;
	Thresh = Y_THRESH;
	EThresh = 0;
	OThresh = 0;
	Blend = Y_FBLEND;
	Noise = Y_NOISE;
	DeintMode = Y_DEINT_MODE;
	DropWin = Y_DROPWIN_SIZE;

	--argc;
	Prog = *argv++;
	while( (p = *argv) )
		{
		if( *p++ != '-' )
			break;
		while( (opt = *p++) )
			switch( opt )
				{
				case 'd':
					DebugFi = TRUE;
					break;

				case 'n':
					NoDrops = TRUE;
					break;

				case 'k':
					NoKeeps = TRUE;
					break;

				case 'l':
					yait_chkac( &argc );
					LogFn = *++argv;
					break;

				case 'o':
					yait_chkac( &argc );
					OpsFn = *++argv;
					break;

				case 't':
					yait_chkac( &argc );
					Thresh = atof( *++argv );
					break;

				case 'E':
					yait_chkac( &argc );
					EThresh = atof( *++argv );
					break;

				case 'O':
					yait_chkac( &argc );
					OThresh = atof( *++argv );
					break;

				case 'b':
					yait_chkac( &argc );
					Blend = atof( *++argv );
					break;

				case 'N':
					yait_chkac( &argc );
					Noise = atof( *++argv );
					break;

				case 'w':
					yait_chkac( &argc );
					DropWin = atoi( *++argv );
					break;

				case 'm':
					yait_chkac( &argc );
					DeintMode = atoi( *++argv );
					break;

				default:
					yait_usage();
					break;
				}
		--argc;
		argv++;
		}

	if( Thresh <= 1 )
		{
		printf( "Invalid threshold specified (%g).\n\n", Thresh );
		yait_usage();
		}

	if( Blend <= Thresh )
		{
		printf( "Invalid blend threshold specified (%g).\n\n", Blend );
		yait_usage();
		}

	if( DropWin<Y_DROPWIN_MIN || DropWin>Y_DROPWIN_MAX )
		{
		printf( "Invalid drop window size specified (%d).\n\n", DropWin );
		yait_usage();
		}

	if( DeintMode<Y_DEINT_MIN || DeintMode>Y_DEINT_MAX )
		{
		printf( "Invalid de-interlace method specified (%d).\n\n", DeintMode );
		yait_usage();
		}

	if( !EThresh )
		EThresh = Thresh;
	if( !OThresh )
		OThresh = Thresh;

	if( argc )
		yait_usage();
	}


/*
 *	yait_chkac:
 */

static void
yait_chkac( int *ac )
	{
	if( *ac < 1 )
		yait_usage();
	--*ac;
	}


/*
 *	yait_usage:
 */

static void
yait_usage( void )
	{
	printf( "Usage: %s [-dnklotbwmEON] [arg...] \n", Prog );
	printf( "\t-d\t\tPrint debug information to stdout.\n" );
	printf( "\t-n\t\tDo not drop frames, always de-interlace.\n" );
	printf( "\t-k\t\tNo forced keep frames.\n" );
	printf( "\t-l log\t\tInput yait log file name [%s].\n", Y_LOG_FN );
	printf( "\t-o ops\t\tOutput yait frame ops file name [%s].\n", Y_OPS_FN );
	printf( "\t-t thresh\tInterlace detection threshold (>1) [%g].\n", Y_THRESH );
	printf( "\t-b blend\tforced blend threshold (>thresh) [%g].\n", Y_FBLEND );
	printf( "\t-w size\t\tDrop frame look ahead window (0-20) [%d].\n", Y_DROPWIN_SIZE );
	printf( "\t-m mode\t\tTranscode blend method (0-5) [%d].\n", Y_DEINT_MODE );
	printf( "\t-E thresh\tEven pattern threshold [%g].\n", Y_THRESH );
	printf( "\t-O thresh\tOdd pattern threshold [%g].\n", Y_THRESH );
	printf( "\t-N noise\tMinimum normalized delta, else noise [%g].\n", Y_NOISE );
	printf( "\n" );

	exit( 1 );
	}


/*
 *	yait_read_log:
 */

static void
yait_read_log( void )
	{
	Fi **fa, *pf, *f;
	int fn, ed, od;
	int s, n;

	s = 0;
	pf = NULL;
	for( Nf=0; ; Nf++ )
		{
		n = fscanf( LogFp, "%d: e: %d, o: %d\n", &fn, &ed, &od );
		if( n != 3 )
			break;

		/* starting frame number */
		if( !Nf )
			s = fn;

		if( (fn-s) != Nf )
			{
			printf( "Broken log file, line %d\n", Nf );
			exit( 1 );
			}

		f = (Fi*) malloc( sizeof(Fi) );
		if( !f )
			{
			perror( "malloc" );
			exit( 1 );
			}

		memset( (void*) f, 0, sizeof(Fi) );
		if( !Fl )
			Fl = f;
		if( pf )
			pf->next = f;
		pf = f;

		f->r = yait_calc_ratio( ed, od );
		f->ro = f->r;
		f->fn = fn;
		f->ed = ed;
		f->od = od;
		f->ip = -1;
		}

	if( !Fl )
		{
		fprintf( stderr, "Invalid log file.\n" );
		exit( 1 );
		}

	/* number of 5 frame groups */
	Nd = Nf / 5;

	Fa = (Fi**) malloc( (Nf+1) * sizeof(Fi*) );
	Ga = (Fi**) malloc( (Nf+1) * sizeof(Fi*) );
	Da = (int*) malloc( Nd * sizeof(int) );
	if( !Fa || !Ga || !Da )
		{
		perror( "malloc" );
		exit( 1 );
		}

	fa = Fa;
	for( f=Fl; f; f=f->next )
		*fa++ = f;
	*fa = NULL;
	}


/*
 *	yait_calc_ratio:
 *		Compute a ratio between even/odd row deltas.  A high ratio indicates an
 *	interlace present.  Use the sign of the ratio to indicate even row (<0), or odd
 *	row (>0) correlation.
 *
 *		If the magnitude of the ratio is > 1.1, this is usually enough to
 *	indicate interlacing.  A value around 1.0 indicates no row correlation at
 *	all.
 *
 * 		Assigning the ratios in this manner result in the following patterns
 * 	present for interlaced material.  Assume 0 for fabs(r)<thresh, else +/- 1:
 *
 * 	An odd interlace pattern (for a five frame group) would appear as:
 *
 *			frame:  1	2	3	4	5
 *			even:	a	a	b	c	d
 *			odd:	a	b	c	c	d
 *
 *			ratio:	0	-1	0	1	0
 *
 * 	If we detect this pattern, we assign the following frame operations:
 *
 *			frame:  1	2	3	4	5
 *			even:	a	a	b	c	d
 *			odd:	a	b	c	c	d
 *
 *			ratio:	0	-1	0	1	0
 *			op:		osd	oc
 *
 * 		osd = save odd rows and drop the frame
 * 		oc  = copy in saved odd rows
 *
 * 	This results with:
 *
 *			frame:  1	|2|	3	4	5
 *			even:	a	|a|	b	c	d
 *			odd:	a	|b|-->	b	c	d
 *                                     drop
 *
 *	For even interlace patterns, the signs are reversed, or simply:
 *
 *			ratio:	0	1	0	-1	0
 *					esd	ec
 *
 *	The entire approach of this tool depends on these specific ratio patterns
 *	to be present, and should be for 2:3 pulldown.  Lots of complications arise
 *	around still and abrupt scene changes.  Again, it might be useful for the
 *	filter to produce a combing co-efficient as well as the delta information.
 *
 *	Side note:
 *		For yuv, deltas based only on luminance yeilded much stronger
 *		interlace patterns, however, I suppose there are (rare) cases where
 *		chroma could be the only indicator, so chroma is included in the
 *		delta calculation, even though it results in weaker ratios.
 */

static double
yait_calc_ratio( int ed, int od )
	{
	double r;

	r = 1;

	/* compute ratio, >1 odd, <-1 even */
	if( !ed && !od )
		/* duplicate frame */
		r = 0;

	if( ed && !od )
		r = 100;

	if( !ed && od )
		r = -100;

	if( ed && od )
		{
		r = (double) ed / (double) od;

		if( r < 1 )
			r = -1.0 / r;

		if( r > 100 )
			r = 100;
		if( r < -100 )
			r = -100;
		}

	return( r );
	}


/*
 *	yait_find_ip:
 *		- Mark isolated duplicate frames to be hard dropped.
 *		- Create the group array which is used to processes interleave
 *		  patterns without duplicate frames present.
 *		- Find the maximum frame delta value.  This is used to normalize
 *		  frame deltas to filter out weak frames (noise which may cause
 *		  erroneous interleave patterns to be detected).
 *		- Detect local interleave patterns.
 */

static void
yait_find_ip( void )
	{
	Fi *f;
	double w;
	int m, p, i;

	/* mark obvious drop frames */
	if( !NoDrops )
		for( i=1; i<Nf-1; i++ )
			{
			f = Fa[i];
			if( f->r )
				continue;

			if( !Fa[i-1]->r && !Fa[i+1]->r )
				continue;

			f->drop = TRUE;
			}

	/* create group array, ommiting drops */
	Ng = 0;
	for( i=0; i<Nf; i++ )
		{
		f = Fa[i];
		if( f->drop )
			continue;

		f->gi = Ng;
		Ga[Ng++] = f;
		}
	Ga[Ng] = NULL;

	/* find max row delta */
	m = 0;
	for( i=0; i<Nf; i++ )
		{
		f = Fa[i];
		if( f->ed > m )
			m = f->ed;
		if( f->od > m )
			m = f->od;
		}

	Md = m;
	if( !Md )
		{
		fprintf( stderr, "All empty frames?\n" );
		exit( 1 );
		}

	/* compute normalized row deltas and */
	/* filter out weak r values (noise) */
	for( i=0; i<Ng; i++ )
		{
		f = Ga[i];
		f->nd = (f->ed + f->od) / (double) Md;
		if( f->nd < Noise )
			f->r = 1;
		}

	/* adjust for incomplete interleave patterns */
	/* (indexing Fa[0,..,i+6]) */
	for( i=0; i<Ng-6; i++ )
		yait_chk_ip( i );

	/* find interleave patterns */
	for( i=0; i<Ng; i++ )
		{
		f = Ga[i];
		if( f->op & Y_OP_COPY )
			{
			/* finish this group before looking for another pattern */
			i++;
			continue;
			}

		p = yait_find_odd( OThresh, i, &w, 4 );
		if( p != -1 )
			{
			yait_mark_grp( p, i, w );
			continue;
			}

		p = yait_find_even( EThresh, i, &w, 4 );
		if( p != -1 )
			yait_mark_grp( p+10, i, w );
		}
	}


/*
 *	yait_chk_ip:
 *		Two cases to look for.  An isolated pair of high r's, and an
 *	isolated tuplet of high r's.  These can be caused by interlacing over
 *	still and abrupt scene changes.
 */

static void
yait_chk_ip( int n )
	{
	if( !NoDrops )
		yait_chk_pairs( n );

	yait_chk_tuplets( n );
	}


/*
 *	yait_chk_pairs:
 *		Look for patterns of the type:
 *			i:      0  1  2  3  4  5
 *			odd:	0  0 -1  1  0  0
 *			even:	0  0  1 -1  0  0
 *
 *	If detected, force the drop of the (single) interlaced frame.
 *	De-interlacing would just incur a redundant copy operation.
 */

static void
yait_chk_pairs( int n )
	{
	Fi *fa[6];
	double ra[6];
	int i;

	for( i=0; i<6; i++ )
		{
		fa[i] = Ga[n+i];
		ra[i] = fabs( fa[i]->r );
		}

	for( i=2; i<4; i++ )
		if( ra[i] < Thresh )
			return;

	/* adjacent frames to the tuplet must be <thresh */
	if( ra[1]>Thresh || ra[4]>Thresh )
		return;

	/* we only need one edge frame to be <thresh */
	if( ra[0]>Thresh && ra[5]>Thresh )
		return;

	if( fa[2]->r>0 && fa[3]->r>0 )
		return;

	if( fa[2]->r<0 && fa[3]->r<0 )
		return;

	/* two isolated high r values of opposite sign */
	/* drop the interlaced frame, erase the pattern */
	fa[2]->r = 1;
	fa[3]->r = 1;

	fa[2]->drop = TRUE;
	}


/*
 *	yait_chk_tuplets:
 *		Look for patterns of the type:
 *			i:      0  1  2   3    4  5  6
 *			odd:	0  0 -1  +/-2  1  0  0
 *			even:	0  0  1  +/-2 -1  0  0
 *
 *	and complete to:
 *
 *			odd:	0  0 -1   0    1  0  0
 *			even:	0  0  1   0   -1  0  0
 */

static void
yait_chk_tuplets( int n )
	{
	Fi *fa[7];
	double ra[7];
	int i;

	for( i=0; i<7; i++ )
		{
		fa[i] = Ga[n+i];
		ra[i] = fabs( fa[i]->r );
		}

	for( i=2; i<5; i++ )
		if( ra[i] < Thresh )
			return;

	/* adjacent frames to the tuplet must be <thresh */
	if( ra[1]>Thresh || ra[5]>Thresh )
		return;

	/* we only need one edge frame to be <thresh */
	if( ra[0]>Thresh && ra[6]>Thresh )
		return;

	if( fa[2]->r>0 && fa[4]->r>0 )
		return;

	if( fa[2]->r<0 && fa[4]->r<0 )
		return;

	/* isolated tuplet of high r values of opposite sign */
	if( ra[3]>ra[2] || ra[3]>ra[4] )
		fa[3]->r = 1;
	}


/*
 *	yait_find_odd:
 */

static int
yait_find_odd( double thresh, int n, double *w, int win )
	{
	double re, ro;
	int me, mo;
	int p;

	/* find max even/odd correlations */
	/* (r<0 - even, r>0 - odd) */
	me = yait_ffmin( n, win );
	mo = yait_ffmax( n, win );

	p = -1;
	if( yait_m5(mo-2) == yait_m5(me) )
		{
		re = fabs( Ga[me]->r );
		ro = fabs( Ga[mo]->r );
		if( re>thresh && ro>thresh )
			{
			p = yait_m5( mo - 4 );
			if( w )
				*w = re + ro;
			}
		}

	return( p );
	}


/*
 *	yait_find_even:
 */

static int
yait_find_even( double thresh, int n, double *w, int win )
	{
	double re, ro;
	int me, mo;
	int p;

	me = yait_ffmin( n, win );
	mo = yait_ffmax( n, win );

	p = -1;
	if( yait_m5(me-2) == yait_m5(mo) )
		{
		re = fabs( Ga[me]->r );
		ro = fabs( Ga[mo]->r );
		if( re>thresh && ro>thresh )
			{
			p = yait_m5( me - 4 );
			if( w )
				*w = re + ro;
			}
		}

	return( p );
	}


/*
 *	yait_ffmin:
 */

static int
yait_ffmin( int n, int w )
	{
	Fi *f;
	int m, i;
	double r;

	r = 0;
	m = 0;
	for( i=n; i<n+w; i++ )
		{
		if( i < 0 )
			break;

		f = Ga[i];
		if( !f )
			break;

		if( f->r < r )
			{
			r = f->r;
			m = i;
			}
		}

	return( m );
	}


/*
 *	yait_ffmax:
 */

static int
yait_ffmax( int n, int w )
	{
	Fi *f;
	int m, i;
	double r;

	r = 0;
	m = 0;
	for( i=n; i<n+w; i++ )
		{
		if( i < 0 )
			break;

		f = Ga[i];
		if( !f )
			break;

		if( f->r > r )
			{
			r = f->r;
			m = i;
			}
		}

	return( m );
	}


/*
 *	yait_m5:
 */

static int
yait_m5( int n )
	{
	while( n < 0 )
		n += 5;
	return( n % 5 );
	}


/*
 *	yait_mark_grp:
 *		Try to catch the situation where a progressive frame is missing
 *	between interlace groups.  This will cause an erroneous (opposite) ip
 *	pattern to be detected.  The first sequence shown below is a normal (odd)
 *	telecine pattern.  The second shows what happens when a progressive frame
 *	is missing.  We want to reject the even pattern detected.  Therefore, if
 *	we find an identical pattern at n+4 we keep it.  If not, we reject if an
 *	opposite pattern follows at n+2 of greater weight.
 *
 *		n:  0   1   2   3   4   0   1   2   3   4
 *		r:  0  -1   0   1   0   0  -1   0   1   0
 *                     odd                 odd
 *
 *		n:  0   1   2   3   4   1   2   3   4
 *		r:  0  -1   0   1   0  -1   0   1   0
 *                     odd     even    odd
 */

static void
yait_mark_grp( int p, int n, double w )
	{
	Fi *f;
	double nw;
	int np, t, i;

	if( n%5 != (p+2)%5 )
		return;

	/* only overwrite an existing pattern if weight is greater */
	f = Ga[n];
	if( w <= f->w )
		return;

	/* check for same pattern at n+4 */
	if( p < 10 )
		np = yait_find_odd( OThresh, n+4, NULL, 5 );
	else
		np = yait_find_even( EThresh, n+4, NULL, 5 );
	if( np < 0 )
		{
		/* no pattern at n+4, reject if opposite ip at n+2 */
		if( p < 10 )
			np = yait_find_even( EThresh, n+2, &nw, 5 );
		else
			np = yait_find_odd( OThresh, n+2, &nw, 5 );

		if( np>=0 && nw>w )
			return;
		}

	/* erase previous pattern */
	if( n > 1 )
		{
		Ga[n-1]->op = 0;
		Ga[n-2]->op = 0;
		}

	/* this frame and next are interlaced */
	t = (p < 10) ? Y_OP_ODD : Y_OP_EVEN;
	f->op = t | Y_OP_SAVE | Y_OP_DROP;
	f = Ga[n+1];
	f->op = t | Y_OP_COPY;

	/* assume 1 progressive on either side of the tuplet */
	for( i=n-1; i<n+4; i++ )
		{
		if( i<0 || i>=Ng )
			continue;

		f = Ga[i];
		f->ip = p;
		f->w = w;
		}
	}


/*
 *	yait_find_drops:
 *		For every group of 5 frames, make sure we drop a frame.  Allow up to a
 *	DropWin (default 15) group lookahead to make up for extra or missing drops.  (The
 *	duplicated frames generated by --hard_fps can be quite early or late in the sequence).
 *	If a group requires a drop, but none exists, mark the group as requiring de-interlacing.
 *	Finally, consequetive marked groups inherit surrounding interleave patterns.
 *
 *	Each group will receive one of the following flags:
 *
 * 		Y_HAS_DROP		- group has a single drop frame
 * 		Y_BANK_DROP		- extra drop, can be used forward
 * 		Y_WITHDRAW_DROP		- missing drop, use banked drop from behind
 * 		Y_RETURN_DROP		- extra drop, can be used behind
 * 		Y_BORROW_DROP		- missing drop, use future extra drop
 * 		Y_FORCE_DEINT		- force de-interlacing, (produces a drop)
 * 		Y_FORCE_DROP		- missing drop, no extras and no interleave found
 * 		Y_FORCE_KEEP		- extra drop, no consumer so have to keep it
 *
 *	For any flags other than FORCE, no action is required.  Eeach group already has
 *	an available frame to drop, whether a marked duplicate, or a locally detected
 *	interleave pattern (which produces a drop).
 *
 *	For Y_FORCE_DEINT, assemble consecutive groups of this type and try to inherit
 *	adjacent interleave patterns.  If no pattern is available, mark them as
 *	Y_FORCE_DROP.
 */

static void
yait_find_drops( void )
	{
	Fi *f;
	int d, l;

	/* populate drop counts */
	for( d=0; d<Nd; d++ )
		Da[d] = yait_cnt_drops( d*5 );

	/* balance drop counts restricted by window size */
	for( d=0; d<Nd; d++ )
		{
		f = Fa[d*5];

		/* this is what we want to see */
		if( Da[d] == 1 )
			{
			if( !f->gf )
				f->gf = Y_HAS_DROP;
			continue;
			}

		/* group is missing a drop? */
		if( !Da[d] )
			{
			/* look ahead for an extra drop */
			l = yait_extra_drop( d );
			if( l )
				{
				/* found one */
				Da[d]++;
				f->gf = Y_BORROW_DROP;

				--Da[l];
				Fa[l*5]->gf = Y_RETURN_DROP;
				continue;
				}

			/* no extra drops exist, mark for de-interlacing */
			f->gf = Y_FORCE_DEINT;
			continue;
			}

		/* we have too many drops */
		while( Da[d] > 1 )
			{
			--Da[d];

			/* look ahead for a missing drop */
			l = yait_missing_drop( d );
			if( l )
				{
				/* found one */
				f->gf = Y_BANK_DROP;

				Da[l]++;
				Fa[l*5]->gf = Y_WITHDRAW_DROP;
				continue;
				}

			/* we have to keep a drop */
			if( !NoKeeps )
				{
				f->gf = Y_FORCE_KEEP;
				yait_keep_frame( d*5 );

				Stat_fk++;
				}
			}
		}
	}


/*
 *	yait_cnt_drops:
 */

static int
yait_cnt_drops( int n )
	{
	Fi *f;
	int d, i;

	d = 0;
	for( i=n; i<n+5 && i<Nf; i++ )
		{
		f = Fa[i];
		if( f->drop || f->op&Y_OP_DROP )
			d++;
		}

	return( d );
	}


/*
 *	yait_extra_drop:
 *		Scan DropWin groups ahead for an extra drop.
 */

static int
yait_extra_drop( int d )
	{
	int l, w;

	for( w=0; w<DropWin; w++ )
		{
		l = d + w + 1;
		if( l >= Nd )
			return( 0 );

		if( Da[l] > 1 )
			return( l );
		}

	return( 0 );
	}


/*
 *	yait_missing_drop:
 *		Scan DropWin groups ahead for a missing drop.
 */

static int
yait_missing_drop( int d )
	{
	int l, w;

	for( w=0; w<DropWin; w++ )
		{
		l = d + w + 1;
		if( l >= Nd )
			return( 0 );

		if( !Da[l] )
			return( l );
		}

	return( 0 );
	}


/*
 *	yait_keep_frame:
 *		Multiple drops exist.  Pick the best frame to keep.  This can be difficult,
 *	as we do not want to keep a duplicate of an interlaced frame.  First, try to find
 *	a hard dropped frame which does not follow an interlace.  If one can be found, then
 *	simply negate the drop flag.  If we are duplicating an interlace, alter the frame
 *	operations for the group to produce a non-interlaced duplicate.
 */

static void
yait_keep_frame( int n )
	{
	Fi *f;
	int da[6], bd, d, i;

	d = yait_get_hdrop( n, da );

	if( !d )
		{
		/* no hard drop frames were found, so ... */
		/* two interlace drops exist, keep one, but blend it */
		for( i=n; i<n+5 && i<Nf; i++ )
			{
			f = Fa[i];
			if( f->op & Y_OP_DROP )
				{
				f->op &= ~Y_OP_DROP;
				f->op |= Y_OP_DEINT;
				return;
				}
			}

		/* sanity check */
		f = Fa[n];
		fprintf( stderr, "No drop frame can be found, frame: %d\n", f->fn );
		exit( 1 );
		}

	/* try to use a drop frame that isn't an interlace duplicate */
	bd = -1;
	for( i=0; i<5; i++ )
		{
		d = da[i];
		if( !d )
			/* can't access before Fa[0] */
			continue;

		if( d < 0 )
			/* end of drop list */
			break;

		f = Fa[d-1];
		if( f->drop )
			/* two dups in a row */
			f = Fa[d-2];

		if( !f->op )
			{
			/* good */
			f = Fa[d];
			f->drop = FALSE;
			return;
			}

		if( f->op & Y_OP_COPY )
			bd = d;
		}

	/* keeping a duplicate of an interlace, try to use one which duplicates the */
	/* second of an interlace pair, as that is cleaner to deal with */
	/* bd (best drop) was set earlier in the loop if such a frame was found */
	if( bd < 0 )
		bd = da[0];

	yait_ivtc_keep( bd );
	}


/*
 *	yait_get_hdrop:
 *		Populate an index array of the hard dropped frames, and return
 *	the count of how many were found.
 */

static int
yait_get_hdrop( int n, int *da )
	{
	Fi *f;
	int d, i;

	d = 0;
	for( i=n; i<n+5 && i<Nf; i++ )
		{
		f = Fa[i];
		if( f->drop )
			{
			*da++ = i;
			d++;
			}
		}
	*da = -1;

	return( d );
	}


/*
 *	yait_ivtc_keep
 *		Depending upon the position of the DROP in the pattern, alter the
 *	frame ops to generate a non-interlaced frame, and keep it.
 *
 *	Case 1:
 *		If the duplicated frame is the second of the interlaced pair, then
 *		simply repeat the row copy operation and keep the frame.
 *
 *		Original (odd pattern):
 *				 	sd	c	 	 
 *			even:	2	2	3	3	4
 *			odd:	2	3	4	4	4
 *					drop		DROP
 *
 *		    yeilds (bad keep frame):
 *			even:	2		3	3	4
 *			odd:	2		3	4	4
 *							KEEP
 *		Revised:
 *				 	sd	c	c	 
 *			even:	2	2	3	3	4
 *			odd:	2	3	4	4	4
 *					drop		DROP
 *		    yeilds:
 *			even:	2		3	3	4
 *			odd:	2		3	3	4
 *							KEEP
 *
 *	Case 2:
 *		If the duplicated frame copies the first of the interlaced pair, more
 *		work must be done:
 *
 *		Original (odd pattern):
 *				 	sd		c	 
 *			even:	2	2	2	3	4
 *			odd:	2	3	3	4	4
 *					drop	DROP
 *
 *		    yeilds (bad keep frame):
 *			even:	2		2	3	4
 *			odd:	2		3	3	4
 *						KEEP
 *		Revised:
 *				s	c	sd	c	 
 *			even:	2	2	2	3	4
 *			odd:	2	3	3	4	4
 *						drop
 *		    yeilds:
 *			even:	2	2		3	4
 *			odd:	2	2		3	4
 *					(keep)
 */

static void
yait_ivtc_keep( int d )
	{
	Fi *fd, *fp;
	int t;

	fd = Fa[d];
	fp = Fa[d-1];
	if( fp->drop )
		fp = Fa[d-2];

	if( fp->op & Y_OP_COPY )
		{
		/* case 1 */
		fd->op = fp->op;
		fd->drop = FALSE;
		return;
		}

	/* case 2 */
	if( d < 2 )
		{
		/* can't access before Fa[0] */
		/* (unlikely we would see this the first two frames of a film) */
		fd->drop = FALSE;
		return;
		}

	fd->op = fp->op;
	fd->drop = FALSE;

	t = fp->op & Y_OP_PAT;
	fp->op = t | Y_OP_COPY;
	fp = Fa[d-2];
	fp->op = t | Y_OP_SAVE;
	}


/*
 *	yait_ivtc_grps:
 *		For each group missing an interleave pattern, scan backward and forward
 *	for an adjacent pattern.  Consider hard dropped frames as barriers.  If two
 *	different patterns exist, test the pattern against the original r values to find
 *	the best match.  For consecutive (forced) interleave groups, use the previously
 *	found pattern values, until the forward scan value is used, which is then
 *	propagated to the rest of the sequence.  (This avoids an O(n^2) search).
 *
 *		If no pattern can be found, force a drop of a frame in the group.
 *
 *	TODO:
 *		I should really be detecting scene changes as well, and consider them
 *		barriers.
 */

static void
yait_ivtc_grps( void )
	{
	Fi *f;
	int pb, pf, fg;
	int p, n;

	/* process by groups of 5 */
	fg = TRUE;
	pb = -1;
	pf = -1;
	for( n=0; n<Nf; n+=5 )
		{
		f = Fa[n];
		if( f->gf != Y_FORCE_DEINT )
			{
			fg = TRUE;
			continue;
			}

		if( fg )
			{
			/* this is the first group of a sequence, scan */
			fg = FALSE;
			pb = yait_scan_bk( n );
			pf = yait_scan_fw( n );
			}

		if( pb<0 && pf<0 )
			{
			/* no pattern exists */
			f->gf = Y_FORCE_DROP;
			yait_drop_frame( n );
			continue;
			}

		/* deinterlace the group with one of the given patterns */
		/* if the pattern used is forward, keep it from now on */
		p = yait_ivtc_grp( n, pb, pf );
		if( p < 0 )
			{
			/* no pattern will match */
			f->gf = Y_FORCE_DROP;
			yait_drop_frame( n );
			continue;
			}

		if( p == pf )
			pb = -1;
		}
	}


/*
 *	yait_scan_bk:
 */

static int
yait_scan_bk( int n )
	{
	Fi *f;
	int i;

	for( i=n-1; i>=0; --i )
		{
		f = Fa[i];
		if( !f )
			return( -1 );

		if( f->drop )
			return( -1 );

		if( f->ip != -1 )
			return( f->ip );
		}

	return( -1 );
	}


/*
 *	yait_scan_fw:
 */

static int
yait_scan_fw( int n )
	{
	Fi *f;
	int i;

	for( i=n+5; i<Nf; i++ )
		{
		f = Fa[i];

		if( f->drop )
			return( -1 );

		if( f->ip != -1 )
			return( f->ip );
		}

	return( -1 );
	}


/*
 *	yait_drop_frame:
 *		Choose a frame to drop.  We want the frame with the highest fabs(r) value,
 *	as it is likely an interlaced frame.  Do not use a frame which follows an assigned
 *	ip pattern, (it is the trailing element of a tuplet).  If no r values exceed the
 *	threshold, choose the frame with the minimum delta.
 *
 *		Frame:	0   1   2   3   4   |   5   6   7   8   9  
 *		Ratio:	0   0   0  -1   0   |	1   0   0   0   0
 *		Op:		   sd	c   |
 *				      group boundary
 *
 *	In the above example, the first frame of the second group (5) may have the highest
 *	ratio value, but is the worst choice because it is part of the detected pattern and
 *	is a unique progressive frame.
 */

static void
yait_drop_frame( int n )
	{
	Fi *f;
	double mr, r;
	int md, d;
	int fr, fd;
	int i;

	mr = 0;
	md = 0;
	fr = n;
	fd = n;

	for( i=n; i<n+5 && i<Nf-1; i++ )
		{
		if( !i )
			/* can't access before Fa[0] */
			continue;

		if( Fa[i-1]->drop || Fa[i+1]->drop )
			/* avoid two consequetive drops */
			continue;

		if( Fa[i-1]->op & Y_OP_PAT )
			/* trailing tuplet element */
			continue;

		f = Fa[i];

		r = fabs( f->ro );
		if( r > mr )
			{
			mr = r;
			fr = i;
			}

		d = f->ed + f->od;
		if( !md || d<md )
			{
			md = d;
			fd = i;
			}
		}

	Fa[ (mr>Thresh)?fr:fd ]->drop = TRUE;
	Stat_fd++;
	}


/*
 *	yait_ivtc_grp:
 *		We need to de-interlace this group.  Given are two potential patterns.
 *	If both are valid, test both and keep the one with the best r value matches.
 *	For the pattern used, mark the group, set the frame ops accordingly, and return
 *	it as the function value.
 */

static int
yait_ivtc_grp( int n, int p1, int p2 )
	{
	Fi *f;
	double thresh, m1, m2;
	int p, t, i;

	m1 = (p1 < 0) ? -1 : yait_tst_ip(n,p1);
	m2 = (p2 < 0) ? -1 : yait_tst_ip(n,p2);

	/* yait_tst_ip() returns the sum of two ratios */
	/* we want both ratios > Y_MTHRESH */
	thresh = Y_MTHRESH * 2;
	if( !NoDrops && m1<thresh && m2<thresh )
		/* neither pattern matches, force a drop instead */
		return( -1 );

	p = (m1 > m2) ? p1 : p2;

	/* sanity check */
	if( p < 0 )
		{
		f = Fa[n];
		fprintf( stderr, "Impossible interlace pattern computed (%d), frame: %d\n",
			p, f->fn );
		exit( 1 );
		}

	/* we have a pattern, mark group */
	for( i=n; i<n+5 && i<Nf; i++ )
		{
		f = Fa[i];
		if( f->drop )
			{
			fprintf( stderr,
				"De-interlace, horribly confused now, frame: %d.\n", f->fn );
			exit( 1 );
			}
		f->ip = p;
		}

	f = Fa[n];
	n = f->gi;

	/* sanity check */
	if( Ga[n] != f )
		{
		fprintf( stderr, "Lost our frame in the group array, frame: %d\n", f->fn );
		exit( 1 );
		}

	t = (p < 10) ? Y_OP_ODD : Y_OP_EVEN;
	for( i=n; i<n+5 && i<Ng-1; i++ )
		{
		if( i%5 == (p+2)%5 )
			{
			f = Ga[i];
			f->op = t | Y_OP_SAVE | Y_OP_DROP;

			/* don't overwrite an existing frame drop */
			f = Ga[i+1];
			if( !(f->op&Y_OP_DROP) )
				f->op = t | Y_OP_COPY;

			break;
			}
		}

	return( p );
	}


/*
 *	yait_tst_ip:
 */

static double
yait_tst_ip( int n, int p )
	{
	double rs, r;
	int s, i;

	s = (p < 10) ? 1 : -1;
	rs = 0;

	n = Fa[n]->gi;
	for( i=n; i<n+5 && i<Ng-2; i++ )
		{
		if( i%5 != (p+2)%5 )
			continue;

		/* strong pattern would have r[i]<-thresh and r[i+2]>thresh */
		r = s * Ga[i]->ro;
		if( r < 0 )
			rs += fabs( r );

		r = s * Ga[i+2]->ro;
		if( r > 0 )
			rs += r;

		break;
		}

	return( rs );
	}


/*
 *	yait_deint:
 *		For non 3/2 telecine patterns, we may have let interlaced frames
 *	through.  Tell transcode to de-interlace (blend) these.  This is the case for
 *	any frame having a high ratio with no interlace pattern detected.
 *
 *	TODO:
 *		This was an afterthought.  Perhaps we can avoid a 32detect pass on
 *	the video by performing this, although it is difficult to detect out of
 *	pattern interlace frames solely on row delta information.  Perhaps we should
 *	have built 32detect into the log generation, and added an extra flag field if
 *	we thought the frame was interlaced.  This also would help when trying to
 *	assign ambiguous ip patterns.
 */

static void
yait_deint( void )
	{
	Fi *fp, *fn, *f;
	int i;

	for( i=1; i<Ng-1; i++ )
		{
		fp = Ga[i-1];
		f = Ga[i];
		fn = Ga[i+1];

		if( f->op&Y_OP_PAT || f->drop )
			/* already being de-interlaced or dropped */
			continue;

		if( fp->op & Y_OP_PAT )
			/* trailing element of a tuplet */
			continue;

		if( fabs(f->r) < Blend )
			/* it isn't interlaced (we think) */
			continue;

		if( f->nd < Y_FWEIGHT )
			/* delta is too weak, interlace is likely not visible */
			continue;

		if( fp->nd>Y_SCENE_CHANGE || f->nd>Y_SCENE_CHANGE )
			/* can't make a decision over scene changes */
			continue;

		/* this frame is interlaced with no operation assigned */
		f->op = Y_OP_DEINT;

		/* if the next frame ratio < thresh, it is similar, unless */
		/* a scene change, therefore interlaced as well */
		if( fabs(fn->r)<Thresh && fn->nd<Y_SCENE_CHANGE )
			if( !(fn->op&Y_OP_PAT) && !fn->drop )
				fn->op = Y_OP_DEINT;

		/* if the next frame(s) are duplicates of this, mark them */
		/* for blending as well, as the may eventually be force kept */
		while( f->next && !f->next->gi )
			{
			f = f->next;
			f->op = Y_OP_DEINT;
			}
		}
	}


/*
 *	yait_write_ops:
 */

static void
yait_write_ops( void )
	{
	Fi *f;

	for( f=Fl; f; f=f->next )
		fprintf( OpsFp, "%d: %s\n", f->fn, yait_write_op(f) );
	}


/*
 *	yait_write_op:
 */

static char*
yait_write_op( Fi *f )
	{
	static char buf[10];
	char *p;
	int op;

	p = buf;
	if( f->drop )
		{
		*p++ = 'd';
		*p = 0;
		Stat_nd++;
		return( buf );
		}

	op = f->op;
	if( op & Y_OP_ODD )
		*p++ = 'o';
	if( op & Y_OP_EVEN )
		*p++ = 'e';
	if( op & Y_OP_SAVE )
		*p++ = 's';
	if( op & Y_OP_COPY )
		*p++ = 'c';
	if( op & Y_OP_DROP )
		{
		*p++ = 'd';
		Stat_nd++;
		if( op & Y_OP_ODD )
			Stat_no++;
		else
			Stat_ne++;
		}
	if( op & Y_OP_DEINT )
		{
		*p++ = '0' + DeintMode;
		Stat_nb++;
		}
	*p = 0;

	return( buf );
	}


/*
 *	yait_fini:
 *		Free up allocations.
 */

static void
yait_fini( void )
	{
	int i;

	for( i=0; i<Nf; i++ )
		free( Fa[i] );

	free( Fa );
	free( Ga );
	free( Da );
	}


/*
 *	Output debug information to stdout
 */

static void
yait_debug_fi( void )
	{
	Fi *f;
	int i;

	printf( "Options:\n" );
	printf( "\tLog file: %s\n", LogFn );
	printf( "\tOps file: %s\n", OpsFn );
	printf( "\tEven Threshold: %g\n", EThresh );
	printf( "\tOdd Threshold: %g\n", OThresh );
	printf( "\tBlend threshold: %g\n", Blend );
	printf( "\tDrop window size: %d\n", DropWin );
	printf( "\tDe-interlace mode: %d\n\n", DeintMode );

	printf( "Stats:\n" );
	printf( "\tTotal number of frames: %d\n", Nf );
	printf( "\tNumber of frames divided by 5: %d\n", Nf/5 );
	printf( "\tTotal dropped frames: %d\n", Stat_nd );
	printf( "\tTotal blended frames: %d\n", Stat_nb );
	printf( "\tTotal odd interlaced pairs: %d\n", Stat_no );
	printf( "\tTotal even interlaced pairs: %d\n", Stat_ne );
	printf( "\tNumber of forced frame drops: %d\n", Stat_fd );
	printf( "\tNumber of forced frame keeps: %d\n\n", Stat_fk );
	printf( "\tMax row delta: %d\n\n", Md );

	i = 0;
	for( f=Fl; f; f=f->next, i++ )
		{
		if( i && !(i%5) )
			printf( "\n" );

		printf( "Frame %6d: e: %8d, o: %8d, r: %7.3f, ro: %7.3f, w: %8.4f, "
			"ip: %2d, gi: %6d, op: %-4s d: %s   %s\n",
				f->fn, f->ed, f->od, f->r, f->ro, f->w, f->ip, f->gi,
				yait_op(f->op), yait_drop(f), yait_grp(f->gf) );
		}
	}

static char*
yait_op( int op )
	{
	static char buf[10];
	char *p;

	p = buf;
	*p = 0;
	if( !op )
		return( buf );

	if( op & Y_OP_ODD )
		*p++ = 'o';
	if( op & Y_OP_EVEN )
		*p++ = 'e';
	if( op & Y_OP_SAVE )
		*p++ = 's';
	if( op & Y_OP_COPY )
		*p++ = 'c';
	if( op & Y_OP_DROP )
		*p++ = 'd';
	if( op & Y_OP_DEINT )
		*p++ = '0' + DeintMode;
	*p = 0;

	return( buf );
	}

static char*
yait_drop( Fi *f )
	{
	if( f->drop )
		return( "DROP" );

	if( f->op&Y_OP_ODD && f->op&Y_OP_DROP )
		return( "odd " );

	if( f->op&Y_OP_EVEN && f->op&Y_OP_DROP )
		return( "even" );

	return( "    " );
	}

static char*
yait_grp( int flg )
	{
	switch( flg )
		{
		case Y_HAS_DROP:
			return( "has drop" );
		case Y_BANK_DROP:
			return( "bank" );
		case Y_WITHDRAW_DROP:
			return( "withdraw" );
		case Y_BORROW_DROP:
			return( "borrow" );
		case Y_RETURN_DROP:
			return( "return" );
		case Y_FORCE_DEINT:
			return( "force deint" );
		case Y_FORCE_DROP:
			return( "force drop" );
		case Y_FORCE_KEEP:
			return( "force keep" );
		}
	return( "" );
	}