aboutsummaryrefslogtreecommitdiffstats
path: root/src/tvtime.c
blob: 43b23855103f606a4a43e6b6e8c981f5a1d3a38b (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
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
/**
 * Copyright (c) 2001, 2002, 2003 Billy Biggs <vektor@dumbterm.net>.
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <locale.h>
#include <stdlib.h>
#include <termio.h>
#include <ctype.h>
#if defined (__SVR4) && defined (__sun)
# include <sys/int_types.h>
#else
# include <stdint.h>
#endif
#include <math.h>
#include <time.h>
#include <errno.h>
#include <libxml/parser.h>
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef ENABLE_NLS
# define _(string) gettext (string)
# include "gettext.h"
#else
# define _(string) string
#endif
#include "pngoutput.h"
#include "pnginput.h"
#include "videoinput.h"
#include "rtctimer.h"
#include "videotools.h"
#include "mixer.h"
#include "tvtimeosd.h"
#include "tvtimeconf.h"
#include "input.h"
#include "speedy.h"
#include "copyfunctions.h"
#include "deinterlace.h"
#include "plugins.h"
#include "performance.h"
#include "taglines.h"
#include "xvoutput.h"
#include "vbidata.h"
#include "vbiscreen.h"
#include "videofilter.h"
#include "fifo.h"
#include "commands.h"
#include "station.h"
#include "pulldown.h"
#include "utils.h"
#include "cpuinfo.h"
#include "outputfilter.h"
#include "mm_accel.h"
#include "menu.h"
#include "tvtimeglyphs.h"
#include "alsa_stream.h"

/**
 * This is how many frames to wait until deciding if the pulldown phase
 * has changed or if we've really found a pulldown sequence.  This is
 * currently set to about 1 second, that is, we won't go into film mode
 * until we've seen a pulldown sequence successfully for 1 second.
 */
#define PULLDOWN_ERROR_WAIT     60

/**
 * This is how many predictions have to be incorrect before we fall back to
 * video mode.  Right now, if we mess up, we jump to video mode immediately.
 */
#define PULLDOWN_ERROR_THRESHOLD 2

/**
 * Michael Niedermayer's magic FIRs
 *
 * [-1 14 3]
 * [-9 111 29 -3]
 */

/**
 * Which pulldown algorithm we're using.
 */
enum {
    PULLDOWN_NONE = 0,
    PULLDOWN_VEKTOR = 1,
    PULLDOWN_MAX = 2
};

/**
 * Which output driver we're using.
 */
enum {
    OUTPUT_XV,
    OUTPUT_DIRECTFB,
    OUTPUT_DIRECTFB_MATROXTV,
    OUTPUT_MGA,
    OUTPUT_SDL
};

/**
 * Speed at which to fade to blue on channel changes.
 */
const int fadespeed = 65;

/**
 * Number of frames to wait before detecting a signal.
 */
const int scan_delay = 10;

typedef struct tvtime_deinterlacer_s
{
    int start;
    int end;
    int cur;
} tvtime_deinterlacer_t;

typedef struct tvtime_s
{
    deinterlace_method_t *curmethod;
    videofilter_t *inputfilter;
    outputfilter_t *outputfilter;
    int filtered_curframe;
    int last_topdiff;
    int last_botdiff;

    /* 2-3 pulldown detection. */
    int pulldown_alg;
    int pdoffset;
    int pderror;
    int pdlastbusted;
    int filmmode;
} tvtime_t;

tvtime_t *tvtime_new( void )
{
    tvtime_t *tvtime = malloc( sizeof( tvtime_t ) );
    if( !tvtime ) return 0;

    tvtime->curmethod = 0;
    tvtime->inputfilter = videofilter_new();
    if( !tvtime->inputfilter ) {
        free( tvtime );
        return 0;
    }
    tvtime->outputfilter = outputfilter_new();
    if( !tvtime->outputfilter ) {
        videofilter_delete( tvtime->inputfilter );
        free( tvtime );
        return 0;
    }
    tvtime->filtered_curframe = 0;
    tvtime->last_topdiff = 0;
    tvtime->last_botdiff = 0;

    tvtime->pulldown_alg = PULLDOWN_NONE;
    tvtime->pdoffset = PULLDOWN_SEQ_AA;
    tvtime->pderror = PULLDOWN_ERROR_WAIT;
    tvtime->pdlastbusted = 0;
    tvtime->filmmode = 0;

    return tvtime;
}

void tvtime_delete( tvtime_t *tvtime )
{
    videofilter_delete( tvtime->inputfilter );
    outputfilter_delete( tvtime->outputfilter );
    free( tvtime );
}

void tvtime_set_deinterlacer( tvtime_t *tvtime, deinterlace_method_t *method )
{
    tvtime->curmethod = method;
}

static void build_colourbars( uint8_t *output, int width, int height )
{
    uint8_t *cb444 = malloc( width * height * 3 );
    int i;
    if( !cb444 ) { memset( output, 255, width * height * 2 ); return; }

    create_colourbars_packed444( cb444, width, height, width*3 );
    for( i = 0; i < height; i++ ) {
        uint8_t *curout = output + (i * width * 2);
        uint8_t *curin = cb444 + (i * width * 3);
        packed444_to_packed422_scanline( curout, curin, width );
    }

    free( cb444 );
}

static void build_blue_frame( uint8_t *output, int width, int height )
{
    uint8_t bluergb[ 3 ];
    uint8_t blueycbcr[ 3 ];

    bluergb[ 0 ] = 0;
    bluergb[ 1 ] = 0;
    bluergb[ 2 ] = 0xff;
    rgb24_to_packed444_rec601_scanline( blueycbcr, bluergb, 1 );
    blit_colour_packed422_scanline( output, width * height, blueycbcr[ 0 ],
                                    blueycbcr[ 1 ], blueycbcr[ 2 ] );
}

static void save_last_frame( uint8_t *saveframe, uint8_t *curframe,
                             int width, int height, int savestride, int curstride )
{
    height /= 2;
    height--;
    while( height-- ) {
        blit_packed422_scanline( saveframe, curframe, width );
        saveframe += savestride;
        interpolate_packed422_scanline( saveframe, curframe, curframe + (curstride*2), width );
        saveframe += savestride;
        curframe += (curstride*2);
    }
    blit_packed422_scanline( saveframe, curframe, width );
    saveframe += savestride;
    blit_packed422_scanline( saveframe, curframe, width );
    saveframe += savestride;
}

static void pngscreenshot( const char *filename, uint8_t *frame422,
                           int width, int height, int stride )
{
    pngoutput_t *pngout = pngoutput_new( filename, width, height, 0, 0.45, 0 );
    uint8_t *tempscanline = malloc( width * 3 );
    int i;

    if( !tempscanline ) {
        if( pngout ) pngoutput_delete( pngout );
        return;
    }

    if( !pngout ) {
        free( tempscanline );
        return;
    }

    for( i = 0; i < height; i++ ) {
        uint8_t *input422 = frame422 + (i * stride);
        packed422_to_packed444_rec601_scanline( tempscanline, input422, width );
        packed444_to_rgb24_rec601_scanline( tempscanline, tempscanline, width );
        pngoutput_scanline( pngout, tempscanline );
    }

    pngoutput_delete( pngout );
    free( tempscanline );
}

/**
 * Explination of the loop:
 *
 * We want to build frames so that they look like this:
 *  Top field:      Bot field:
 *     Copy            Double
 *     Interp          Copy
 *     Copy            Interp
 *     Interp          Copy
 *     Copy            --
 *     --              --
 *     --              --
 *     Copy            Interp
 *     Interp          Copy
 *     Copy            Interp
 *     Double          Copy
 *
 *  So, say a frame is n high.
 *  For the bottom field, the first scanline is blank (special case).
 *  For the top field, the final scanline is blank (special case).
 *  For the rest of the scanlines, we alternate between Copy then Interpolate.
 *
 *  To do the loop, I go 'Interp then Copy', and handle the first copy
 *  outside the loop for both top and bottom.
 *  The top field therefore handles n-2 scanlines in the loop.
 *  The bot field handles n-2 scanlines in the loop.
 *
 * What we pass to the deinterlacing routines:
 *
 * Each deinterlacing routine can require data from up to four fields.
 * The current field is being output is Field 4:
 *
 * | Field 3 | Field 2 | Field 1 | Field 0 |
 * |         |   T2    |         |   T0    |
 * |   M3    |         |    M1   |         |
 * |         |   B2    |         |   B0    |
 * |  NX3    |         |   NX1   |         |
 *
 * So, since we currently get frames not individual fields from V4L, there
 * are two possibilities for where these come from:
 *
 * CASE 1: Deinterlacing the top field:
 * | Field 4 | Field 3 | Field 2 | Field 1 | Field 0 |
 * |   T4    |         |   T2    |         |   T0    |
 * |         |   M3    |         |    M1   |         |
 * |   B4    |         |   B2    |         |   B0    |
 *  [--  secondlast --] [--  lastframe  --] [--  curframe   --]
 *
 * CASE 2: Deinterlacing the bottom field:
 * | Field 4 | Field 3 | Field 2 | Field 1 | Field 0 |
 * |   T4    |         |   T2    |         |   T0    |
 * |         |   M3    |         |    M1   |         |
 * |   B4    |         |   B2    |         |   B0    |
 * ndlast --] [--  lastframe  --] [--  curframe   --]
 *
 * So, in case 1, we need the previous 2 frames as well as the current
 * frame, and in case 2, we only need the previous frame, since the
 * current frame contains both Field 3 and Field 4.
 */
static void pulldown_merge_fields( uint8_t *output,
                                   uint8_t *topfield,
                                   uint8_t *botfield,
                                   outputfilter_t *of,
                                   int width,
                                   int frame_height,
                                   int fieldstride,
                                   int outstride )
{
    int i;

    for( i = 0; i < frame_height; i++ ) {
        uint8_t *curoutput = output + (i * outstride);

        if( i & 1 ) {
            blit_packed422_scanline( curoutput, botfield + ((i / 2) * fieldstride), width );
        } else {
            blit_packed422_scanline( curoutput, topfield + ((i / 2) * fieldstride), width );
        }

        if( of ) outputfilter_composite_packed422_scanline( of, curoutput, width, 0, i );
    }
}

static void calculate_pulldown_score_vektor( tvtime_t *tvtime,
                                             uint8_t *curframe,
                                             uint8_t *lastframe,
                                             int instride,
                                             int frame_height,
                                             int width )
{
    int i;

    tvtime->last_topdiff = 0;
    tvtime->last_botdiff = 0;

    for( i = 0; i < frame_height; i++ ) {
        if( !tvtime->filtered_curframe && videofilter_active_on_scanline( tvtime->inputfilter, i ) ) {
            videofilter_packed422_scanline( tvtime->inputfilter, curframe + (i*instride), width, 0, i );
        }

        if( i > 40 && (i & 3) == 0 && i < frame_height - 40 ) {
            tvtime->last_topdiff += diff_factor_packed422_scanline( curframe + (i*instride),
                                                                    lastframe + (i*instride), width );
            tvtime->last_botdiff += diff_factor_packed422_scanline( curframe + (i*instride) + instride,
                                                                    lastframe + (i*instride) + instride,
                                                                    width );
        }
    }

    tvtime->filtered_curframe = 1;
}

static void tvtime_build_deinterlaced_frame( tvtime_t *tvtime,
                                             uint8_t *output,
                                             uint8_t *curframe,
                                             uint8_t *lastframe,
                                             uint8_t *secondlastframe,
                                             int bottom_field, int second_field,
                                             int width,
                                             int frame_height,
                                             int instride,
                                             int outstride )
{
    int i;

    if( tvtime->pulldown_alg != PULLDOWN_VEKTOR ) {
        /* If we leave vektor pulldown mode, lose our state. */
        tvtime->filmmode = 0;
    }

    if( tvtime->pulldown_alg == PULLDOWN_VEKTOR ) {
        /* Make pulldown phase decisions every top field. */
        if( !bottom_field ) {
            int predicted;

            predicted = tvtime->pdoffset << 1;
            if( predicted > PULLDOWN_SEQ_DD ) predicted = PULLDOWN_SEQ_AA;

            calculate_pulldown_score_vektor( tvtime, curframe, lastframe,
                                             instride, frame_height, width );
            tvtime->pdoffset = determine_pulldown_offset_short_history_new( tvtime->last_topdiff,
                                                                            tvtime->last_botdiff,
                                                                            1, predicted );

            /* 3:2 pulldown state machine. */
            if( !tvtime->pdoffset ) {
                /* No pulldown offset applies, drop out of pulldown immediately. */
                tvtime->pdlastbusted = 0;
                tvtime->pderror = PULLDOWN_ERROR_WAIT;
            } else if( tvtime->pdoffset != predicted ) {
                if( tvtime->pdlastbusted ) {
                    tvtime->pdlastbusted--;
                    tvtime->pdoffset = predicted;
                } else {
                    tvtime->pderror = PULLDOWN_ERROR_WAIT;
                }
            } else {
                if( tvtime->pderror ) {
                    tvtime->pderror--;
                }

                if( !tvtime->pderror ) {
                    tvtime->pdlastbusted = PULLDOWN_ERROR_THRESHOLD;
                }
            }


            if( !tvtime->pderror ) {
                /* We're in pulldown, reverse it. */
                if( !tvtime->filmmode ) {
                    tvtime->filmmode = 1;
                }

                if( pulldown_source( tvtime->pdoffset, 0 ) ) {
                    pulldown_merge_fields( output, lastframe, lastframe + instride,
                                           tvtime->outputfilter, width, frame_height, instride*2, outstride );
                } else {
                    pulldown_merge_fields( output, curframe, lastframe + instride,
                                           tvtime->outputfilter, width, frame_height, instride*2, outstride );
                }

                return;
            } else {
                if( tvtime->filmmode ) {
                    tvtime->filmmode = 0;
                }
            }
        } else if( !tvtime->pderror ) {
            if( pulldown_source( tvtime->pdoffset, 1 ) ) {
                pulldown_merge_fields( output, curframe, lastframe + instride,
                                       tvtime->outputfilter, width, frame_height, instride*2, outstride );
            } else {
                pulldown_merge_fields( output, curframe, curframe + instride,
                                       tvtime->outputfilter, width, frame_height, instride*2, outstride );
            }

            return;
        }
    }

    if( !tvtime->curmethod->scanlinemode ) {
        deinterlace_frame_data_t data;

        if( !tvtime->filtered_curframe ) {
            for( i = 0; i < frame_height; i++ ) {
                if( videofilter_active_on_scanline( tvtime->inputfilter, i ) ) {
                    videofilter_packed422_scanline( tvtime->inputfilter, curframe + (i*instride), width, 0, i );
                }
            }
        }

        data.f0 = curframe;
        data.f1 = lastframe;
        data.f2 = secondlastframe;

        tvtime->curmethod->deinterlace_frame( output, outstride, &data, bottom_field, bottom_field, width, frame_height );

        if( tvtime->outputfilter ) {
            for( i = 0; i < frame_height; i++ ) {
                uint8_t *curoutput = output + (i * outstride);
                outputfilter_composite_packed422_scanline( tvtime->outputfilter, curoutput, width, 0, i );
            }
        }
    } else {
        int loop_size;
        int scanline = 0;

        if( bottom_field ) {
            /* Advance frame pointers to the next input line. */
            curframe += instride;
            lastframe += instride;
            secondlastframe += instride;

            /* Double the top scanline a scanline. */
            blit_packed422_scanline( output, curframe, width );
            if( tvtime->outputfilter ) {
                outputfilter_composite_packed422_scanline( tvtime->outputfilter, output, width, 0, scanline );
            }

            output += outstride;
            scanline++;
        }

        /* Copy a scanline. */
        if( !tvtime->filtered_curframe && videofilter_active_on_scanline( tvtime->inputfilter, scanline ) ) {
            videofilter_packed422_scanline( tvtime->inputfilter, curframe, width, 0, scanline );
        }
        blit_packed422_scanline( output, curframe, width );
        if( tvtime->outputfilter ) {
            outputfilter_composite_packed422_scanline( tvtime->outputfilter, output, width, 0, scanline );
        }

        output += outstride;
        scanline++;

        /* Something is wrong here. -Billy */
        /* xine's suggestion:
        loop_size = ((frame_height - 2) / 2);
        */
        loop_size = ((frame_height - 2) / 2) - bottom_field;
        for( i = loop_size; i; --i ) {
            deinterlace_scanline_data_t data;

            data.bottom_field = bottom_field;

            data.t0 = curframe;
            data.b0 = curframe + (instride*2);

            if( !tvtime->filtered_curframe && videofilter_active_on_scanline( tvtime->inputfilter, scanline + 1 ) ) {
                videofilter_packed422_scanline( tvtime->inputfilter, curframe + instride, width, 0, scanline );
                videofilter_packed422_scanline( tvtime->inputfilter, curframe + (instride*2), width, 0, scanline + 1 );
            }

            if( second_field ) {
                data.tt1 = (i < loop_size) ? (curframe - instride) : (curframe + instride);
                data.m1  = curframe + instride;
                data.bb1 = (i > 1) ? (curframe + (instride*3)) : (curframe + instride);
            } else {
                data.tt1 = (i < loop_size) ? (lastframe - instride) : (lastframe + instride);
                data.m1  = lastframe + instride;
                data.bb1 = (i > 1) ? (lastframe + (instride*3)) : (lastframe + instride);
            }

            data.t2 = lastframe;
            data.b2 = lastframe + (instride*2);

            if( second_field ) {
                data.tt3 = (i < loop_size) ? (lastframe - instride) : (lastframe + instride);
                data.m3  = lastframe + instride;
                data.bb3 = (i > 1) ? (lastframe + (instride*3)) : (lastframe + instride);
            } else {
                data.tt3 = (i < loop_size) ? (secondlastframe - instride) : (secondlastframe + instride);
                data.m3  = secondlastframe + instride;
                data.bb3 = (i > 1) ? (secondlastframe + (instride*3)) : (secondlastframe + instride);
            }

            tvtime->curmethod->interpolate_scanline( output, &data, width );
            if( tvtime->outputfilter ) {
                outputfilter_composite_packed422_scanline( tvtime->outputfilter, output, width, 0, scanline );
            }

            output += outstride;
            scanline++;

            data.tt0 = curframe;
            data.m0  = curframe + (instride*2);
            data.bb0 = (i > 1) ? (curframe + (instride*4)) : (curframe + (instride*2));

            if( second_field ) {
                data.t1 = curframe + instride;
                data.b1 = (i > 1) ? (curframe + (instride*3)) : (curframe + instride);
            } else {
                data.t1 = lastframe + instride;
                data.b1 = (i > 1) ? (lastframe + (instride*3)) : (lastframe + instride);
            }

            data.tt2 = lastframe;
            data.m2  = lastframe + (instride*2);
            data.bb2 = (i > 1) ? (lastframe + (instride*4)) : (lastframe + (instride*2));

            if( second_field ) {
                data.t2 = lastframe + instride;
                /* xine's suggestion:
                data.b2 = (i > 1) ? (lastframe + (instride*3)) : (lastframe + instride);
                */
                data.b2 = lastframe + (instride*3);
            } else {
                data.t2 = secondlastframe + instride;
                /* xine's suggestion:
                data.b2 = (i > 1) ? (secondlastframe + (instride*3)) : (secondlastframe + instride);
                */
                data.b2 = secondlastframe + (instride*3);
            }

            /* Copy a scanline. */
            tvtime->curmethod->copy_scanline( output, &data, width );
            curframe += instride * 2;
            lastframe += instride * 2;
            secondlastframe += instride * 2;

            if( tvtime->outputfilter ) {
                outputfilter_composite_packed422_scanline( tvtime->outputfilter, output, width, 0, scanline );
            }

            output += outstride;
            scanline++;
        }

        if( !bottom_field ) {
            /* Double the bottom scanline. */
            blit_packed422_scanline( output, curframe, width );

            if( tvtime->outputfilter ) {
                outputfilter_composite_packed422_scanline( tvtime->outputfilter, output, width, 0, scanline );
            }

            output += outstride;
            scanline++;
        }
    }

    tvtime->filtered_curframe = 1;
}

static void tvtime_build_interlaced_frame( tvtime_t *tvtime,
                                           uint8_t *output,
                                           uint8_t *curframe,
                                           int bottom_field,
                                           int width,
                                           int frame_height,
                                           int instride,
                                           int outstride,
                                           int interleaved )
{
    /* uint8_t tempscanline[ 768*2 ]; */
    int scanline = 0;
    int i;

    if( !interleaved ) {
        if( bottom_field ) {
            /* Advance frame pointers to the next input line. */
            curframe += instride;
  
            /* Skip the top scanline. */
            output += outstride;
            scanline++;
        }

        /* Copy a scanline. */
        blit_packed422_scanline( output, curframe, width );
        if( tvtime->outputfilter ) {
            outputfilter_composite_packed422_scanline( tvtime->outputfilter, output, width, 0, scanline );
        }

        output += outstride;
        scanline++;

        for( i = ((frame_height - 2) / 2); i; --i ) {
            /* Skip a scanline. */
            output += outstride;
            scanline++;

            /* Copy a scanline. */
            curframe += instride * 2;
            blit_packed422_scanline( output, curframe, width );

            if( tvtime->outputfilter ) {
                outputfilter_composite_packed422_scanline( tvtime->outputfilter, output, width, 0, scanline );
            }
  
            output += outstride;
            scanline++;
        }
    } else {
        for( i = frame_height; i; --i ) {

            /* Copy a scanline. */
            blit_packed422_scanline( output, curframe, width );

            if( tvtime->outputfilter ) {
                outputfilter_composite_packed422_scanline( tvtime->outputfilter, output, width, 0, scanline );
            }
  
            curframe += instride;         
            output += outstride;
            scanline++;
        }
    }
}

static void tvtime_build_copied_field( tvtime_t *tvtime,
                                       uint8_t *output,
                                       uint8_t *curframe,
                                       int bottom_field,
                                       int width,
                                       int frame_height,
                                       int instride,
                                       int outstride )
{
    int scanline = 0;
    int i;

    if( bottom_field ) {
        /* Advance frame pointers to the next input line. */
        curframe += instride;
    }

    /* Interpolate the top scanline as a special case. */
    if( !tvtime->filtered_curframe && videofilter_active_on_scanline( tvtime->inputfilter, scanline + bottom_field ) ) {
        videofilter_packed422_scanline( tvtime->inputfilter, curframe, width, 0, 0 );
        videofilter_packed422_scanline( tvtime->inputfilter, curframe + instride, width, 0, 1 );
        videofilter_packed422_scanline( tvtime->inputfilter, curframe + (instride*2), width, 0, 2 );
    }
    quarter_blit_vertical_packed422_scanline( output, curframe + (instride*2), curframe, width );

    if( tvtime->outputfilter ) {
        outputfilter_composite_packed422_scanline( tvtime->outputfilter, output, width, 0, scanline );
    }
  
    curframe += instride * 2;
    output += outstride;
    scanline += 2;

    for( i = ((frame_height - 2) / 2); i; --i ) {

        if( !tvtime->filtered_curframe && 
                (videofilter_active_on_scanline( tvtime->inputfilter, scanline + 1 ) ||
                 videofilter_active_on_scanline( tvtime->inputfilter, scanline + 2 )) ) {
            videofilter_packed422_scanline( tvtime->inputfilter, curframe + instride, width, 0, scanline + 1 );
            if( i > 1 ) {
                videofilter_packed422_scanline( tvtime->inputfilter, curframe + (instride*2), width, 0, scanline + 2 );
            }
        }

        /* Interpolate scanline. */
        if( bottom_field ) {
            quarter_blit_vertical_packed422_scanline( output, curframe - (instride*2), curframe, width );
        } else {
            if( i > 1 ) {
                quarter_blit_vertical_packed422_scanline( output, curframe + (instride*2), curframe, width );
            } else {
                blit_packed422_scanline( output, curframe, width );
            }
        }
        curframe += instride * 2;

        if( tvtime->outputfilter ) {
            outputfilter_composite_packed422_scanline( tvtime->outputfilter, output, width, 0, scanline );
        }

        output += outstride;
        scanline += 2;
    }

    tvtime->filtered_curframe = 1;
}

static void osd_list_deinterlacer_info( tvtime_osd_t *osd, int curmethod )
{
    int i;

    if( get_deinterlace_method( curmethod ) ) {
        tvtime_osd_list_set_lines( osd, 11 );
        tvtime_osd_list_set_text( osd, 0, get_deinterlace_method( curmethod )->name );
        for( i = 0; i < 10; i++ ) {
            tvtime_osd_list_set_text( osd, i + 1, get_deinterlace_method( curmethod )->description[ i ] );
        }
        tvtime_osd_list_set_hilight( osd, -1 );
        tvtime_osd_show_list( osd, 1, 0 );
    }
}

static void osd_list_deinterlacers( tvtime_osd_t *osd, int curmethod )
{
    int nummethods = get_num_deinterlace_methods();
    int i;

    if( nummethods ) {
        tvtime_osd_list_set_lines( osd, get_num_deinterlace_methods() + 1 );
        tvtime_osd_list_set_text( osd, 0, _("Deinterlacer configuration") );
        for( i = 0; i < nummethods; i++ ) {
            tvtime_osd_list_set_text( osd, i + 1, get_deinterlace_method( i )->name );
        }
        tvtime_osd_list_set_hilight( osd, curmethod + 1 );
        tvtime_osd_show_list( osd, 1, 0 );
    }
}

static void build_deinterlacer_menu( menu_t *menu, int curmethod )
{
    int nummethods = get_num_deinterlace_methods();
    char string[ 128 ];
    int i;

    menu_set_back_command( menu, TVTIME_SHOW_MENU, "processing" );
    for( i = 0; i < nummethods; i++ ) {
        if( i == curmethod ) {
            snprintf( string, sizeof( string ), TVTIME_ICON_RADIOON "  %s",
                      get_deinterlace_method( i )->name );
        } else {
            snprintf( string, sizeof( string ), TVTIME_ICON_RADIOOFF "  %s",
                      get_deinterlace_method( i )->name );
        }
        menu_set_text( menu, i + 1, string );
        menu_set_enter_command( menu, i + 1, TVTIME_SET_DEINTERLACER,
                                get_deinterlace_method( i )->short_name );
    }

    snprintf( string, sizeof( string ), TVTIME_ICON_PLAINLEFTARROW "  %s", 
              _("Back") );
    menu_set_back_command( menu, TVTIME_SHOW_MENU, "processing" );
    menu_set_text( menu, nummethods + 1, string );
    menu_set_enter_command( menu, i + 1, TVTIME_SHOW_MENU, "processing" );
}

static void build_deinterlacer_description_menu( menu_t *menu, int curmethod )
{
    char string[ 128 ];
    int i;

    if( get_deinterlace_method( curmethod ) ) {
        menu_set_text( menu, 0, get_deinterlace_method( curmethod )->name );
        menu_set_back_command( menu, TVTIME_SHOW_MENU, "processing" );
        for( i = 0; i < 10; i++ ) {
            menu_set_text( menu, i + 1, get_deinterlace_method( curmethod )->description[ i ] );
            menu_set_enter_command( menu, i + 1, TVTIME_SHOW_MENU, "processing" );
        }
        snprintf( string, sizeof( string ), TVTIME_ICON_PLAINLEFTARROW "  %s",
                  _("Back") );
        menu_set_default_cursor( menu, i );
        menu_set_back_command( menu, TVTIME_SHOW_MENU, "processing" );
        menu_set_text( menu, i + 1, string );
        menu_set_enter_command( menu, i + 1, TVTIME_SHOW_MENU, "processing" );
    }
}

static void build_framerate_menu( menu_t *menu, double maxrate, int mode )
{
    char string[ 128 ];
    char text[ 128 ];

    snprintf( text, sizeof( text ), _("Full rate: %.2f fps"), maxrate );
    snprintf( string, sizeof( string ), mode == 0 ?
              TVTIME_ICON_RADIOON "  %s" : TVTIME_ICON_RADIOOFF "  %s", text );
    menu_set_text( menu, 1, string );
    menu_set_back_command( menu, TVTIME_SHOW_MENU, "processing" );
    menu_set_enter_command( menu, 1, TVTIME_SET_FRAMERATE, "full" );

    snprintf( text, sizeof( text ),
              _("Half rate, deinterlace top fields: %.2f fps"), maxrate / 2 );
    snprintf( string, sizeof( string ), mode == 1 ?
              TVTIME_ICON_RADIOON "  %s" : TVTIME_ICON_RADIOOFF "  %s", text );
    menu_set_text( menu, 2, string );
    menu_set_enter_command( menu, 2, TVTIME_SET_FRAMERATE, "top" );

    snprintf( text, sizeof( text ),
              _("Half rate, deinterlace bottom fields: %.2f fps"), maxrate / 2 );
    snprintf( string, sizeof( string ), mode == 2 ?
              TVTIME_ICON_RADIOON "  %s" : TVTIME_ICON_RADIOOFF "  %s", text );
    menu_set_text( menu, 3, string );
    menu_set_enter_command( menu, 3, TVTIME_SET_FRAMERATE, "bottom" );

    snprintf( string, sizeof( string ), TVTIME_ICON_PLAINLEFTARROW "  %s",
              _("Back") );
    menu_set_text( menu, 4, string );
    menu_set_enter_command( menu, 4, TVTIME_SHOW_MENU, "processing" );
}

static void build_output_menu( menu_t *menu, int widescreen,
                               int fullscreen, int alwaysontop,
                               int fullscreen_supported,
                               int alwaysontop_supported,
                               int overscan_supported,
                               int quiet_screenshots )
{
    char string[ 128 ];
    int cur = 1;

    menu_set_back_command( menu, TVTIME_SHOW_MENU, "root" );
    if( overscan_supported ) {
        snprintf( string, sizeof( string ), TVTIME_ICON_OVERSCANSETTING "  %s",
                  _("Overscan setting") );
        menu_set_text( menu, cur, string );
        menu_set_enter_command( menu, cur, TVTIME_SHOW_MENU, "overscan" );
        cur++;
    }

    snprintf( string, sizeof( string ), TVTIME_ICON_APPLYMATTE "  %s",
              _("Apply matte") );
    menu_set_text( menu, cur, string );
    menu_set_enter_command( menu, cur, TVTIME_SHOW_MENU, "matte" );
    cur++;

    snprintf( string, sizeof( string ), widescreen ?
              TVTIME_ICON_GENERALTOGGLEON "  %s" :
              TVTIME_ICON_GENERALTOGGLEOFF "  %s",
              _("16:9 output") );
    menu_set_text( menu, cur, string );
    menu_set_enter_command( menu, cur, TVTIME_TOGGLE_ASPECT, "" );
    cur++;

    snprintf( string, sizeof( string ), 
              TVTIME_ICON_TELEVISIONSTANDARD "  %s",
              _("Resize window to match contents") );
    menu_set_text( menu, cur, string );
    menu_set_enter_command( menu, cur, TVTIME_AUTO_ADJUST_WINDOW, "" );
    cur++;

    if( fullscreen_supported ) {
        snprintf( string, sizeof( string ), fullscreen ?
                  TVTIME_ICON_GENERALTOGGLEON "  %s" :
                  TVTIME_ICON_GENERALTOGGLEOFF "  %s",
                  _("Fullscreen") );

        menu_set_text( menu, cur, string );
        menu_set_enter_command( menu, cur, TVTIME_TOGGLE_FULLSCREEN, "" );
        cur++;

        snprintf( string, sizeof( string ), TVTIME_ICON_SETFSPOS "  %s",
                  _("Set fullscreen position") );
        menu_set_text( menu, cur, string );
        menu_set_enter_command( menu, cur, TVTIME_SHOW_MENU, "fspos" );
        cur++;
    }

    if( alwaysontop_supported ) {
        snprintf( string, sizeof( string ), alwaysontop ?
                  TVTIME_ICON_GENERALTOGGLEON "  %s" :
                  TVTIME_ICON_GENERALTOGGLEOFF "  %s",
                  _("Always-on-top") );
        menu_set_text( menu, cur, string );
        menu_set_enter_command( menu, cur, TVTIME_TOGGLE_ALWAYSONTOP, "" );
        cur++;
    }

    snprintf( string, sizeof( string ), quiet_screenshots ?
              TVTIME_ICON_GENERALTOGGLEON "  %s" :
              TVTIME_ICON_GENERALTOGGLEOFF "  %s",
              _("Quiet screenshots") );
    menu_set_text( menu, cur, string );
    menu_set_enter_command( menu, cur, TVTIME_TOGGLE_QUIET_SCREENSHOTS, "" );
    cur++;

    snprintf( string, sizeof( string ), TVTIME_ICON_PLAINLEFTARROW "  %s",
              _("Back") );
    menu_set_text( menu, cur, string );
    menu_set_enter_command( menu, cur, TVTIME_SHOW_MENU, "root" );
}

static void build_fspos_menu( menu_t *menu, int fspos )
{
    char string[ 128 ];

    snprintf( string, sizeof( string ), (fspos == 0) ?
              TVTIME_ICON_RADIOON "  %s" : TVTIME_ICON_RADIOOFF "  %s",
              _("Centre") );
    menu_set_text( menu, 1, string );
    menu_set_back_command( menu, TVTIME_SHOW_MENU, "output" );
    menu_set_enter_command( menu, 1, TVTIME_SET_FULLSCREEN_POSITION, "centre" );

    snprintf( string, sizeof( string ), (fspos == 1) ?
              TVTIME_ICON_RADIOON "  %s" : TVTIME_ICON_RADIOOFF "  %s",
              _("Top") );
    menu_set_text( menu, 2, string );
    menu_set_enter_command( menu, 2, TVTIME_SET_FULLSCREEN_POSITION, "top" );

    snprintf( string, sizeof( string ), (fspos == 2) ?
              TVTIME_ICON_RADIOON "  %s" : TVTIME_ICON_RADIOOFF "  %s",
              _("Bottom") );
    menu_set_text( menu, 3, string );
    menu_set_enter_command( menu, 3, TVTIME_SET_FULLSCREEN_POSITION, "bottom" );
    snprintf( string, sizeof( string ), TVTIME_ICON_PLAINLEFTARROW "  %s",
              _("Back") );
    menu_set_text( menu, 4, string );
    menu_set_enter_command( menu, 4, TVTIME_SHOW_MENU, "output" );
}

static void osd_list_framerates( tvtime_osd_t *osd, double maxrate, int mode )
{
    char text[ 200 ];

    tvtime_osd_list_set_lines( osd, 4 );
    tvtime_osd_list_set_text( osd, 0, _("Attempted framerate") );

    snprintf( text, sizeof( text ), _("Full rate: %.2f fps"), maxrate );
    tvtime_osd_list_set_text( osd, 1, text );

    snprintf( text, sizeof( text ),
              _("Half rate, deinterlace top fields: %.2f fps"), maxrate / 2 );
    tvtime_osd_list_set_text( osd, 2, text );

    snprintf( text, sizeof( text ),
              _("Half rate, deinterlace bottom fields: %.2f fps"),
              maxrate / 2 );
    tvtime_osd_list_set_text( osd, 3, text );

    tvtime_osd_list_set_hilight( osd, mode + 1 );
    tvtime_osd_show_list( osd, 1, 0 );
}

static void osd_list_statistics( tvtime_osd_t *osd, performance_t *perf,
                                 const char *deinterlacer, int width,
                                 int height, int framesize, int fieldtime,
                                 int frameratemode, int norm )
{
    double blit_time = performance_get_estimated_blit_time( perf );
    char text[ 200 ];

    tvtime_osd_list_set_lines( osd, frameratemode ? 7 : 8 );
    tvtime_osd_list_set_text( osd, 0, _("Performance estimates") );

    snprintf( text, sizeof( text ), "%s: %s", _("Deinterlacer"), deinterlacer );
    tvtime_osd_list_set_text( osd, 1, text );

    snprintf( text, sizeof( text ), _("Input: %s at %dx%d pixels"),
              videoinput_get_norm_name( norm ), width, height );
    tvtime_osd_list_set_text( osd, 2, text );

    snprintf( text, sizeof( text ), _("Attempted framerate: %.2f fps"),
             frameratemode ? 1000000.0 / ((double) (fieldtime*2)) : 1000000.0 / ((double) fieldtime) );
    tvtime_osd_list_set_text( osd, 3, text );

    snprintf( text, sizeof( text ), _("Average blit time: %.2f ms (%.0f MB/sec)"),
             blit_time, (((double) framesize) * 0.00095367431640625000) / blit_time );
    tvtime_osd_list_set_text( osd, 4, text );

    snprintf( text, sizeof( text ), _("Average render time: %5.2f ms"),
              performance_get_estimated_rendering_time( perf ) );
    tvtime_osd_list_set_text( osd, 5, text );

    snprintf( text, sizeof( text ), _("Dropped frames: %d"),
             performance_get_dropped_frames( perf ) );
    tvtime_osd_list_set_text( osd, 6, text );

    if( !frameratemode ) {
        snprintf( text, sizeof( text ), 
                  _("Blit spacing: %4.1f/%4.1f ms (want %4.1f ms)"),
                  performance_get_time_top_to_bot( perf ),
                  performance_get_time_bot_to_top( perf ),
                  ((double) fieldtime) * 0.001 );
        tvtime_osd_list_set_text( osd, 7, text );
    }

    tvtime_osd_list_set_hilight( osd, -1 );
    tvtime_osd_show_list( osd, 1, 0 );
}

static void build_matte_menu( menu_t *menu, int mode, int sixteennine )
{
    char string[ 128 ];

    menu_set_back_command( menu, TVTIME_SHOW_MENU, "output" );
    if( sixteennine ) {
        snprintf( string, sizeof( string ), (mode == 0) ?
                  TVTIME_ICON_RADIOON "  %s" : TVTIME_ICON_RADIOOFF "  %s",
                  _("16:9 + Overscan") );
        menu_set_text( menu, 1, string );
        menu_set_enter_command( menu, 1, TVTIME_SET_MATTE, "16:9" );
        snprintf( string, sizeof( string ), (mode == 1) ?
                  TVTIME_ICON_RADIOON "  %s" : TVTIME_ICON_RADIOOFF "  %s",
                  _("1.85:1") );
        menu_set_text( menu, 2, string );
        menu_set_enter_command( menu, 2, TVTIME_SET_MATTE, "1.85:1" );
        snprintf( string, sizeof( string ), (mode == 2) ?
                  TVTIME_ICON_RADIOON "  %s" : TVTIME_ICON_RADIOOFF "  %s",
                  _("2.35:1") );
        menu_set_text( menu, 3, string );
        menu_set_enter_command( menu, 3, TVTIME_SET_MATTE, "2.35:1" );
        snprintf( string, sizeof( string ), (mode == 3) ?
                  TVTIME_ICON_RADIOON "  %s" : TVTIME_ICON_RADIOOFF "  %s",
                  _("4:3 centre") );
        menu_set_text( menu, 4, string );
        menu_set_enter_command( menu, 4, TVTIME_SET_MATTE, "4:3" );
        snprintf( string, sizeof( string ), (mode == 4) ?
                  TVTIME_ICON_RADIOON "  %s" : TVTIME_ICON_RADIOOFF "  %s",
                  _("16:10") );
        menu_set_text( menu, 5, string );
        menu_set_enter_command( menu, 5, TVTIME_SET_MATTE, "16:10" );
    } else {
        snprintf( string, sizeof( string ), (mode == 0) ?
                  TVTIME_ICON_RADIOON "  %s" : TVTIME_ICON_RADIOOFF "  %s",
                  _("4:3 + Overscan") );
        menu_set_text( menu, 1, string );
        menu_set_enter_command( menu, 1, TVTIME_SET_MATTE, "4:3" );
        snprintf( string, sizeof( string ), (mode == 1) ?
                  TVTIME_ICON_RADIOON "  %s" : TVTIME_ICON_RADIOOFF "  %s",
                  _("16:9") );
        menu_set_text( menu, 2, string );
        menu_set_enter_command( menu, 2, TVTIME_SET_MATTE, "16:9" );
        snprintf( string, sizeof( string ), (mode == 2) ?
                  TVTIME_ICON_RADIOON "  %s" : TVTIME_ICON_RADIOOFF "  %s",
                  _("16:10") );
        menu_set_text( menu, 3, string );
        menu_set_enter_command( menu, 3, TVTIME_SET_MATTE, "16:10" );
        snprintf( string, sizeof( string ), (mode == 3) ?
                  TVTIME_ICON_RADIOON "  %s" : TVTIME_ICON_RADIOOFF "  %s",
                  _("1.85:1") );
        menu_set_text( menu, 4, string );
        menu_set_enter_command( menu, 4, TVTIME_SET_MATTE, "1.85:1" );
        snprintf( string, sizeof( string ), (mode == 4) ?
                  TVTIME_ICON_RADIOON "  %s" : TVTIME_ICON_RADIOOFF "  %s",
                  _("2.35:1") );
        menu_set_text( menu, 5, string );
        menu_set_enter_command( menu, 5, TVTIME_SET_MATTE, "2.35:1" );
    }

    snprintf( string, sizeof( string ), TVTIME_ICON_PLAINLEFTARROW "  %s",
              _("Back") );
    menu_set_text( menu, 6, string );
    menu_set_enter_command( menu, 6, TVTIME_SHOW_MENU, "output" );
}

static void osd_list_matte( tvtime_osd_t *osd, int mode, int sixteennine )
{
    tvtime_osd_list_set_lines( osd, 6 );
    if( sixteennine ) {
        tvtime_osd_list_set_text( osd, 0, _("Matte setting (Anamorphic input)") );
        tvtime_osd_list_set_text( osd, 1, _("16:9 + Overscan") );
        tvtime_osd_list_set_text( osd, 2, "1.85:1" );
        tvtime_osd_list_set_text( osd, 3, "2.35:1" );
        tvtime_osd_list_set_text( osd, 4, _("4:3 centre") );
        tvtime_osd_list_set_text( osd, 5, "16:10" );
    } else {
        tvtime_osd_list_set_text( osd, 0, _("Matte setting (4:3 input)") );
        tvtime_osd_list_set_text( osd, 1, _("4:3 + Overscan") );
        tvtime_osd_list_set_text( osd, 2, "16:9" );
        tvtime_osd_list_set_text( osd, 3, "16:10" );
        tvtime_osd_list_set_text( osd, 4, "1.85:1" );
        tvtime_osd_list_set_text( osd, 5, "2.35:1" );
    }
    tvtime_osd_list_set_hilight( osd, mode + 1 );
    tvtime_osd_show_list( osd, 1, 0 );
}

/*
 * setup_i18n() is run by main() before tvtime_main(),
 * so using gettext here is safe.
 */
int tvtime_main( rtctimer_t *rtctimer, int read_stdin, int realtime,
                 int argc, char **argv )
{
    videoinput_t *vidin = 0;
    station_mgr_t *stationmgr = 0;
    int width = 720;
    int height = 480;
    int norm = 0;
    int sixteennine = 0;
    int fieldtime;
    int safetytime;
    int fieldsavailable = 0;
    int verbose;
    int send_fields;
    tvtime_osd_t *osd = 0;
    uint8_t *colourbars;
    uint8_t *lastframe = 0;
    uint8_t *secondlastframe = 0;
    uint8_t *saveframe = 0;
    uint8_t *fadeframe = 0;
    uint8_t *blueframe = 0;
    uint8_t *curframe = 0;
    const char *tagline;
    int curframeid;
    int lastframeid;
    int secondlastframeid;
    config_t *ct = 0;
    input_t *in = 0;
    output_api_t *output = 0;
    performance_t *perf = 0;
    int has_signal = 0;
    vbidata_t *vbidata = 0;
    vbiscreen_t *vs = 0;
    fifo_t *fifo = 0;
    char *fifofile = 0;
    commands_t *commands = 0;
    int fadepos = 0;
    int scanwait = scan_delay;
    int scanning = 0;
    int numscanned = 0;
    int framerate_mode = -1;
    char kbd_cmd[ 1024 ];
    int kbd_pos = 0;
    int kbd_available;
    char *error_string = 0;
    char error_string2[4096];
    double pixel_aspect;
    char number[ 6 ];
    tvtime_t *tvtime;
    deinterlace_method_t *curmethod;
    int curmethodid;
    int matte_x = 0;
    int matte_w = 0;
    int matte_y = 0;
    int matte_h = 0;
    int matte_mode = 0;
    int restarttvtime = 0;
    int return_value = 0;
    int last_current_id = -1;
    int quiet_screenshots = 0;
    char prevloc[ 256 ];
    int i;

    ct = config_new();
    if( !ct ) {
        lfprintf( stderr, _("%s: Cannot allocate memory.\n"), argv[ 0 ] );
        return 1;
    }

    /* Parse command line arguments. */
    if( !config_parse_tvtime_command_line( ct, argc, argv ) ) {
        config_delete( ct );
        return 1;
    }

    verbose = config_get_verbose( ct );
    if( verbose ) {
        cpuinfo_print_info();
    }

    if( setpriority( PRIO_PROCESS, 0, config_get_priority( ct ) ) < 0 ) {
        if( verbose ) {
            fprintf( stderr, "%s: Cannot set priority to %d: %s.\n",
                     argv[ 0 ], config_get_priority( ct ), strerror( errno ) );
        }
    }

    send_fields = config_get_send_fields( ct );

    /* Setup the output. */
    output = get_xv_output();

    sixteennine = config_get_aspect( ct );

    if (!output) {
        fprintf (stderr, "%s: Broken tvtime install, "
                 "not compiled with X support?\n", argv[ 0 ]);
        return 1;
    }

    if( !output->init( config_get_geometry( ct ), sixteennine,
                       config_get_square_pixels( ct ), verbose ) ) {
        /* Error messages are driver specific. */
        return 1;
    }

    /* Setup the ALSA streaming device */
    tvtime_alsa_thread_startup(config_get_alsa_outputdev( ct ),
			       config_get_alsa_inputdev( ct ) );

    /* Setup the speedy calls. */
    setup_speedy_calls( mm_accel(), verbose );

    /* Setup the tvtime object. */
    tvtime = tvtime_new();
    if( !tvtime ) {
        lfprintf( stderr, _("%s: Cannot allocate memory.\n"), argv[ 0 ] );
    }

    if( !output->is_interlaced() ) {
        register_deinterlace_method( linear_get_method() );
        register_deinterlace_method( scalerbob_get_method() );

        register_deinterlace_method( linearblend_get_method() );
        register_deinterlace_method( vfir_get_method() );

#ifdef ARCH_X86
        register_deinterlace_method( dscaler_tomsmocomp_get_method() );
        register_deinterlace_method( dscaler_greedyh_get_method() );
#endif
        register_deinterlace_method( greedy_get_method() );

        register_deinterlace_method( weave_get_method() );
        register_deinterlace_method( weavetff_get_method() );
        register_deinterlace_method( weavebff_get_method() );
    }

    if( !strcasecmp( config_get_v4l_norm( ct ), "pal" ) ) {
        norm = VIDEOINPUT_PAL;
    } else if( !strcasecmp( config_get_v4l_norm( ct ), "secam" ) ) {
        norm = VIDEOINPUT_SECAM;
    } else if( !strcasecmp( config_get_v4l_norm( ct ), "pal-nc" ) ) {
        norm = VIDEOINPUT_PAL_NC;
    } else if( !strcasecmp( config_get_v4l_norm( ct ), "pal-m" ) ) {
        norm = VIDEOINPUT_PAL_M;
    } else if( !strcasecmp( config_get_v4l_norm( ct ), "pal-n" ) ) {
        norm = VIDEOINPUT_PAL_N;
    } else if( !strcasecmp( config_get_v4l_norm( ct ), "ntsc-jp" ) ) {
        norm = VIDEOINPUT_NTSC_JP;
    } else if( !strcasecmp( config_get_v4l_norm( ct ), "pal-60" ) ) {
        norm = VIDEOINPUT_PAL_60;
    } else {
        /* Only allow NTSC otherwise. */
        norm = VIDEOINPUT_NTSC;
    }

    /* Field display in microseconds. */
    fieldtime = videoinput_get_time_per_field( norm );
    safetytime = fieldtime - ((fieldtime*3)/4);
    perf = performance_new( fieldtime );
    if( !perf ) {
        lfprintf( stderr, _("%s: Cannot allocate memory.\n"), argv[ 0 ] );
        return 1;
    }

    stationmgr = station_new( videoinput_get_norm_name( norm ),
                              config_get_v4l_freq( ct ),
                              config_get_ntsc_cable_mode( ct ), verbose );
    if( !stationmgr ) {
        lfprintf( stderr, _("%s: Cannot allocate memory.\n"), argv[ 0 ] );
        return 1;
    }
    station_set( stationmgr, config_get_prev_channel( ct ) );
    station_set( stationmgr, config_get_start_channel( ct ) );


    /* Default to a width specified on the command line. */
    width = config_get_inputwidth( ct );

    vidin = videoinput_new( config_get_v4l_device( ct ), 
                            config_get_inputwidth( ct ), 
                            config_get_audio_boost( ct ), 
                            norm, verbose, error_string2 );
    if( !vidin ) {
        if( asprintf( &error_string,
                      _("Cannot open capture device %s."),
                      config_get_v4l_device( ct ) ) < 0 ) {
            error_string = 0;
        }
    } else if( videoinput_get_numframes( vidin ) < 2 ) {
        lfprintf( stderr, _("\n"
    "    Your capture card driver, %s, does not seem\n"
    "    to support full framerate capture.  Please check to see if it is\n"
    "    misconfigured, or if you have selected the wrong capture\n"
    "    device (%s).\n\n"), videoinput_get_driver_name( vidin ),
                             config_get_v4l_device( ct ) );
        videoinput_delete( vidin );
        vidin = 0;
     } else {
        const char *audiomode = config_get_audio_mode( ct );
        videoinput_set_input_num( vidin, config_get_inputnum( ct ) );

        if( audiomode ) {
            if( !strcasecmp( audiomode, "mono" ) ) {
                videoinput_set_audio_mode( vidin, VIDEOINPUT_MONO );
            } else if( !strcasecmp( audiomode, "stereo" ) ) {
                videoinput_set_audio_mode( vidin, VIDEOINPUT_STEREO );
            } else if( !strcasecmp( audiomode, "sap" )
                       || !strcasecmp( audiomode, "lang1" ) ) {
                videoinput_set_audio_mode( vidin, VIDEOINPUT_LANG1 );
            } else {
                videoinput_set_audio_mode( vidin, VIDEOINPUT_LANG2 );
            }
        }

        width = videoinput_get_width( vidin );
        height = videoinput_get_height( vidin );
        if( verbose ) {
            fprintf( stderr, "tvtime: Sampling input at %d pixels "
                               "per scanline.\n", width );
        }
    }

    /* Set input size. */
    if( !output->set_input_size( width, height ) ) {
        /* FIXME: Clean up. */
        return 1;
    }

    /**
     * 1 buffer : 0 fields available.  [t][b]
     * 2 buffers: 1 field  available.  [t][b][-][-]
     *                                 ^^^
     * 3 buffers: 3 fields available.  [t][b][t][b][-][-]
     *                                 ^^^^^^^^^
     * 4 buffers: 5 fields available.  [t][b][t][b][t][b][-][-]
     *                                 ^^^^^^^^^^^^^^^
     */
    if( vidin ) {
        if( videoinput_get_numframes( vidin ) == 2 ) {
            fieldsavailable = 1;
        } else if( videoinput_get_numframes( vidin ) == 3 ) {
            fieldsavailable = 2;
        } else {
            fieldsavailable = 4;
        }

        if( fieldsavailable < 4 ) {
            if( videoinput_is_bttv( vidin ) ) {
                lfprintf( stderr,
                      _("\n"
        "    You are using the bttv driver, but have not configured enough\n"
        "    buffers for tvtime to process the video optimally.  This is\n"
        "    true by default with bttv in kernels before 2.4.21.  Please\n"
        "    set the option gbuffers=4 when loading bttv.  For more\n"
        "    information see our support page at %s\n\n"), PACKAGE_BUGREPORT );
            } else {
                lfprintf( stderr, _("\n"
     "    Your capture card driver, %s, is not providing\n"
     "    enough buffers for tvtime to process the video.  Please check with\n"
     "    your driver documentation to see if you can increase the number\n"
     "    of buffers provided to applications, and report this to the tvtime\n"
     "    bug tracker at %s\n\n"), videoinput_get_driver_name( vidin ),
                                   PACKAGE_BUGREPORT );
            }
        }
    } else {
        fieldsavailable = 4;
    }

    if( output->is_interlaced() ) {
        curmethodid = 0;
        curmethod = 0;
    } else {
        filter_deinterlace_methods( speedy_get_accel(), fieldsavailable );
        curmethodid = 0;
        curmethod = get_deinterlace_method( 0 );
        while( strcasecmp( config_get_deinterlace_method( ct ),
                           curmethod->short_name ) ) {
            curmethodid = (curmethodid + 1) % get_num_deinterlace_methods();
            curmethod = get_deinterlace_method( curmethodid );
            if( !curmethodid ) break;
        }
    }
    tvtime_set_deinterlacer( tvtime, curmethod );

    /* Build colourbars. */
    colourbars = malloc( width * height * 2 );
    saveframe = malloc( width * height * 2 );
    fadeframe = malloc( width * height * 2 );
    blueframe = malloc( width * height * 2 );
    if( !colourbars || !saveframe || !fadeframe || !blueframe ) {
        lfprintf( stderr, _("%s: Cannot allocate memory.\n"), argv[ 0 ] );
        return 1;
    }
    build_colourbars( colourbars, width, height );
    build_blue_frame( blueframe, width, height );
    blit_packed422_scanline( saveframe, blueframe, width * height );
    secondlastframe = lastframe = blueframe;

    /* Set the mixer device. */
    mixer_set_device( config_get_mixer_device( ct ) );
    mixer_set_state( config_get_muted( ct ), config_get_unmute_volume( ct ) );

    /* Setup OSD stuff. */
    pixel_aspect = ( (double) width ) /
        ( ( (double) height ) * ( sixteennine ? (16.0 / 9.0) : (4.0 / 3.0) ) );
    osd = tvtime_osd_new( width, height, pixel_aspect, fieldtime,
                          config_get_channel_text_rgb( ct ),
                          config_get_other_text_rgb( ct ) );
    if( !osd ) {
        lfputs( _("On screen display failed to initialize, disabled.\n"),
                stderr );
    } else {
        tvtime_osd_set_timeformat( osd, config_get_timeformat( ct ) );
        if( curmethod ) {
            tvtime_osd_set_deinterlace_method( osd, curmethod->name );
        }
        if( vidin ) {
            tvtime_osd_set_input( osd, videoinput_get_input_name( vidin ) );
            tvtime_osd_set_norm( osd,
                  videoinput_get_norm_name( videoinput_get_norm( vidin ) ) );
        } else {
            tvtime_osd_set_input( osd, _("No video source") );
            tvtime_osd_set_norm( osd, "" );
        }

        if( error_string ) {
            tvtime_osd_set_hold_message( osd, error_string2, error_string );
            tvtime_osd_hold( osd, 1 );
            tvtime_osd_show_info( osd );
            free( error_string );
            error_string = 0;
        }
    }
    if( tvtime->outputfilter ) {
        outputfilter_set_osd( tvtime->outputfilter, osd );
        outputfilter_set_pixel_aspect( tvtime->outputfilter, pixel_aspect );
    }

    commands = commands_new( ct, vidin, stationmgr, osd, fieldtime );
    if( !commands ) {
        lfprintf( stderr, _("%s: Cannot allocate memory.\n"), argv[ 0 ] );
        return 1;
    }
    build_deinterlacer_menu( commands_get_menu( commands, "deinterlacer" ),
                             curmethodid );
    build_deinterlacer_description_menu( commands_get_menu( commands,
                                         "deintdescription" ), curmethodid );

    /* Setup the video correction tables. */
    videofilter_set_colour_invert( tvtime->inputfilter,
                                   commands_apply_colour_invert( commands ) );
    videofilter_set_mirror( tvtime->inputfilter,
                            commands_apply_mirror( commands ) );
    videofilter_set_chroma_kill( tvtime->inputfilter,
                                 commands_apply_chroma_kill( commands ) );
    if( vidin && videoinput_is_uyvy( vidin ) ) {
        videofilter_enable_uyvy_conversion( tvtime->inputfilter );
    }

    fifofile = get_tvtime_fifo_filename( config_get_uid( ct ) );
    if( !fifofile ) {
        lfprintf( stderr, _("%s: Cannot allocate memory.\n"), argv[ 0 ] );
    } else {
        fifo = fifo_new( fifofile );
        if( !fifo ) {
            lfputs( _("Cannot create FIFO, "
                      "remote control of tvtime disabled.\n"), stderr );
        }
    }

    /* We no longer need the fifo filename. */
    if( fifofile ) free( fifofile );

    in = input_new( ct, commands, verbose );
    if( !in ) {
        lfprintf( stderr, _("%s: Cannot allocate memory.\n"), argv[ 0 ] );
        return 1;
    }

    /* Setup VBI stuff for NTSC-like norms. */
    if( vidin && height == 480 ) {
        vs = vbiscreen_new( width, height, pixel_aspect, verbose );
        if( !vs ) {
            lfputs( _("Closed caption display failed to "
                      "initialize, disabled.\n"), stderr );
        }

        vbidata = vbidata_new( config_get_vbi_device( ct ), vs, verbose );
        if( !vbidata ) {
            lfprintf( stderr, _("%s: Cannot allocate memory.\n"), argv[ 0 ] );
        } else {
            vbidata_capture_mode( vbidata, config_get_cc( ct )
                                  ? CAPTURE_CC1 : CAPTURE_OFF );
            vbidata_capture_xds( vbidata, config_get_usexds( ct ) );
        }
        commands_set_vbidata( commands, vbidata );
    }
    if( tvtime->outputfilter ) {
        outputfilter_set_vbiscreen( tvtime->outputfilter, vs );
    }

    /* Randomly assign a tagline as the window caption. */
    srand( time( 0 ) );
    tagline = taglines[ rand() % numtaglines ];

    /* Set the fullscreen position. */
    output->set_fullscreen_position( config_get_fullscreen_position( ct ) );

    /* If we start fullscreen, go into fullscreen mode now. */
    if( config_get_fullscreen( ct ) ) {
        commands_handle( commands, TVTIME_TOGGLE_FULLSCREEN, 0 );
    }

    /* If we start always-on-top, enter this state now. */
    if( config_get_alwaysontop( ct ) ) {
        commands_handle( commands, TVTIME_TOGGLE_ALWAYSONTOP, 0 );
    }

    /* If we start half-framerate, toggle that now. */
    for( i = 0; i < config_get_framerate_mode( ct ); i++ ) {
        commands_handle( commands, TVTIME_TOGGLE_FRAMERATE, 0 );
    }

    /* If we want quiet screenshots, make sure we do that. */
    quiet_screenshots = config_get_quiet_screenshots( ct );

    /* If we are a new install, show the menu. */
    if( station_is_new_install( stationmgr ) ) {
        commands_handle( commands, TVTIME_SHOW_MENU, 0 );
    }

    /* Begin capturing frames. */
    if( vidin ) {
        if( fieldsavailable == 2 ) {
            lastframe = videoinput_next_frame( vidin, &lastframeid );
        } else if( fieldsavailable == 4 ) {
            secondlastframe = videoinput_next_frame( vidin,
                                                     &secondlastframeid );
            lastframe = videoinput_next_frame( vidin, &lastframeid );
        }
    }

    build_output_menu( commands_get_menu( commands, "output" ), sixteennine,
                       output->is_fullscreen(), output->is_alwaysontop(),
                       output->is_fullscreen_supported(),
                       output->is_alwaysontop_supported(),
                       output->is_overscan_supported(),
                       quiet_screenshots );
    build_matte_menu( commands_get_menu( commands, "matte" ),
                      matte_mode, sixteennine );
    build_fspos_menu( commands_get_menu( commands, "fspos" ),
                      config_get_fullscreen_position( ct ) );

    /* Initialize our timestamps. */
    for(;;) {
        const char *fifo_args = 0;
        int printdebug = 0;
        int showbars, screenshot;
        int acquired = 0;
        int tuner_state;
        int paused = 0;
        int output_x, output_y, output_w, output_h;
        int output_success = 1;
        int exposed = output->is_exposed();
        int current_id;

        if( vidin && videoinput_has_tuner( vidin ) ) {
            current_id = station_get_current_id( stationmgr );
        } else {
            current_id = 0;
        }

        if( matte_mode ) {
            output_x = matte_x;
            output_w = matte_w;
            output_y = matte_y;
            output_h = matte_h;
        } else {
            output_x = (int) ((((double) width) *
                               commands_get_overscan( commands )) + 0.5);
            output_w = (int) ((((double) width) -
                               (((double) width) *
                                commands_get_overscan( commands ) * 2.0)) +
                              0.5);
            output_y = (int) ((((double) height) *
                               commands_get_overscan( commands )) + 0.5);
            output_h = (int) ((((double) height) -
                               (((double) height) *
                                commands_get_overscan( commands ) * 2.0)) +
                              0.5);
        }

        if( fifo ) {
            int cmd;
            cmd = fifo_get_next_command( fifo );
            fifo_args = fifo_get_arguments( fifo );
            if( cmd != TVTIME_NOCOMMAND ) {
                output->update_server_time( 0 );
                commands_handle( commands, cmd, fifo_args );
            }
        }

        if( read_stdin ) {
            /* Read commands from standard input. */
            while( !ioctl( STDIN_FILENO, FIONREAD, &kbd_available ) ) {
                char c;

                if( (kbd_available > 0) && read( STDIN_FILENO, &c, 1 ) == 1 ) {
                    if( c == '\n' ) {
                        int cmd;

                        kbd_cmd[ kbd_pos ] = '\0';

                        cmd = tvtime_string_to_command( kbd_cmd );
                        if( cmd != TVTIME_NOCOMMAND ) {
                            commands_handle( commands, cmd, 0 );
                        }
                        memset( kbd_cmd, 0, sizeof( kbd_cmd ) );
                        kbd_pos = 0;
                    } else {
                        if( kbd_pos < sizeof( kbd_cmd ) - 1 ) {
                            kbd_cmd[ kbd_pos++ ] = c;
                        } else {
                            memset( kbd_cmd, 0, sizeof( kbd_cmd ) );
                            kbd_pos = 0;
                        }
                    }
                } else {
                    break;
                }
            }
        }

        if( osd ) {
            tvtime_osd_advance_frame( osd );
        }

        if( curmethod ) {
            commands_set_half_size( commands, curmethod->doscalerbob );
        }
        output->poll_events( in );

        if( commands_quit( commands ) ) break;
        if( commands_restart_tvtime( commands ) ) {
            restarttvtime = 1;
            break;
        }
        /* Check if it's time to sleep and dream well */
        if( commands_sleeptimer( commands ) &&
            commands_sleeptimer_do_shutdown( commands ) ) {
            /**
             * Return the special constant 3 if we've exited
             * from a sleep timer.
             */
            return_value = 3;
            break;
        }
        printdebug = commands_print_debug( commands );
        showbars = commands_show_bars( commands );
        screenshot = commands_take_screenshot( commands );
        if( screenshot ) {
            /**
             * If we're being asked to take a screenshot, force a draw by
             * marking us exposed (just for this frame).  This makes sure
             * that screenshots always happen regardless of whether the
             * window is or becomes obscured.
             */
            exposed = 1;
        }
        paused = commands_pause( commands );
        if( framerate_mode < 0
            || framerate_mode != commands_get_framerate( commands ) ) {
            int showlist = !(framerate_mode < 0 ) &&
                !commands_menu_active( commands );
            framerate_mode = commands_get_framerate( commands );

            if( osd ) {
                build_framerate_menu
                    ( commands_get_menu( commands, "framerate" ),
                      1000000.0 / ((double) fieldtime), framerate_mode );
                commands_refresh_menu( commands );
                if( showlist ) {
                    osd_list_framerates( osd, 1000000.0 / ((double) fieldtime),
                                         framerate_mode );
                }
            }
        }
        if( commands_toggle_fullscreen( commands ) ) {
            if( output->toggle_fullscreen( 0, 0 ) ) {
                config_save( ct, "FullScreen", "1" );
            } else {
                config_save( ct, "FullScreen", "0" );
            }
            build_output_menu( commands_get_menu( commands, "output" ),
                               sixteennine,
                               output->is_fullscreen(),
                               output->is_alwaysontop(),
                               output->is_fullscreen_supported(),
                               output->is_alwaysontop_supported(),
                               output->is_overscan_supported(),
                               quiet_screenshots );
            commands_refresh_menu( commands );
        }
        if( commands_toggle_alwaysontop( commands ) ) {
            if( output->toggle_alwaysontop() ) {
                if( osd ) {
                    tvtime_osd_show_message( osd,
                          _("Always-on-top enabled.") );
                }
            } else {
                if( osd ) {
                    tvtime_osd_show_message( osd,
                          _("Always-on-top disabled.") );
                }
            }
            build_output_menu( commands_get_menu( commands, "output" ),
                               sixteennine,
                               output->is_fullscreen(),
                               output->is_alwaysontop(),
                               output->is_fullscreen_supported(),
                               output->is_alwaysontop_supported(),
                               output->is_overscan_supported(),
                               quiet_screenshots );
            commands_refresh_menu( commands );
        }
        if( commands_toggle_aspect( commands ) ) {
            matte_mode = 0;
            output->set_matte( 0, 0 );
            if( output->toggle_aspect() ) {
                sixteennine = 1;
                if( osd ) {
                    tvtime_osd_show_message( osd,
                          _("16:9 display mode active.") );
                }
                config_save( ct, "WideScreen", "1" );
                pixel_aspect = ( (double) width ) /
                    ( ( (double) height ) * (16.0 / 9.0) );
            } else {
                sixteennine = 0;
                if( osd ) {
                    tvtime_osd_show_message( osd,
                          _("4:3 display mode active.") );
                }
                config_save( ct, "WideScreen", "0" );
                pixel_aspect =
                    ( (double) width ) / ( ( (double) height ) * (4.0 / 3.0) );
            }
            if( osd ) {
                tvtime_osd_show_list( osd, 0, 0 );
                tvtime_osd_set_pixel_aspect( osd, pixel_aspect );
            }
            build_output_menu( commands_get_menu( commands, "output" ),
                               sixteennine,
                               output->is_fullscreen(),
                               output->is_alwaysontop(),
                               output->is_fullscreen_supported(),
                               output->is_alwaysontop_supported(),
                               output->is_overscan_supported(),
                               quiet_screenshots );
            build_matte_menu( commands_get_menu( commands, "matte" ),
                              matte_mode, sixteennine );
            commands_refresh_menu( commands );
        }
        if( commands_get_fs_pos( commands ) ) {
            const char *fspos = commands_get_fs_pos( commands );
            int newpos;
            if( tolower( fspos[ 0 ] ) == 't' ) {
                newpos = 1;
            } else if( tolower( fspos[ 0 ] ) == 'b' ) {
                newpos = 2;
            } else {
                newpos = 0;
            }
            output->set_fullscreen_position( newpos );
            config_save( ct, "FullscreenPosition",
                         commands_get_fs_pos( commands ) );
            build_fspos_menu( commands_get_menu( commands, "fspos" ), newpos );
            commands_refresh_menu( commands );
        }
        if( commands_toggle_quiet_screenshots( commands ) ) {
            quiet_screenshots = !quiet_screenshots;
            build_output_menu( commands_get_menu( commands, "output" ),
                               sixteennine,
                               output->is_fullscreen(),
                               output->is_alwaysontop(),
                               output->is_fullscreen_supported(),
                               output->is_alwaysontop_supported(),
                               output->is_overscan_supported(),
                               quiet_screenshots );
            if( osd ) {
                if( quiet_screenshots ) {
                    tvtime_osd_show_message( osd, _("Screenshot messages disabled.") );
                } else {
                    tvtime_osd_show_message( osd, _("Screenshot messages enabled.") );
                }
            }
            commands_refresh_menu( commands );
        }
        if( commands_toggle_matte( commands ) ||
            commands_get_matte_mode( commands ) ) {
            double matte = 4.0 / 3.0;
            int sqwidth = sixteennine ?
                ((height * 16) / 9) : ((height * 4) / 3);
            int sqheight = sixteennine ?
                ((width * 9) / 16) : ((width * 3) / 4);

            matte_x = 0;
            matte_w = width;
            if( commands_toggle_matte( commands ) ) {
                matte_mode = (matte_mode + 1) % 5;
            } else {
                if( !strcmp( commands_get_matte_mode( commands ), "16:9" ) ) {
                    matte_mode = sixteennine ? 0 : 1;
                } else if( !strcmp( commands_get_matte_mode( commands ),
                                    "16:10" ) ) {
                    matte_mode = sixteennine ? 4 : 2;
                } else if( !strcmp( commands_get_matte_mode( commands ),
                                    "1.85:1" ) ) {
                    matte_mode = sixteennine ? 1 : 3;
                } else if( !strcmp( commands_get_matte_mode( commands ),
                                    "2.35:1" ) ) {
                    matte_mode = sixteennine ? 2 : 4;
                } else {
                    matte_mode = sixteennine ? 3 : 0;
                }
            }

            if( sixteennine ) {
                if( matte_mode == 0 ) {
                    matte = 16.0 / 9.0;
                } else if( matte_mode == 1 ) {
                    matte = 1.85;
                } else if( matte_mode == 2 ) {
                    matte = 2.35;
                } else if( matte_mode == 3 ) {
                    matte = 4.0 / 3.0;
                    matte_w = (int) (((double) sqheight * matte) + 0.5);
                    matte_x = (width - matte_w) / 2;
                    /* We're cropping the sides off so we add overscan to avoid mess
                     * at the top of the screen. */
                    matte_y = commands_get_overscan( commands ) * height / 2;
                    matte_h = height - matte_y;
                    output->set_matte( (matte_h * 4) / 3, matte_h );
                } else if( matte_mode == 4 ) {
                    matte = 1.6;
                    matte_w = (int) (((double) sqheight * matte) + 0.5);
                    matte_x = (width - matte_w) / 2;
                    /* We're cropping the sides off so we add overscan to avoid mess
                     * at the top of the screen. */
                    matte_y = commands_get_overscan( commands ) * height / 2;
                    matte_h = height - matte_y;
                    output->set_matte( (matte_h * 16) / 10, matte_h );
                }
            } else {
                if( matte_mode == 1 ) {
                    matte = 16.0 / 9.0;
                } else if( matte_mode == 2 ) {
                    matte = 1.6;
                } else if( matte_mode == 3 ) {
                    matte = 1.85;
                } else if( matte_mode == 4 ) {
                    matte = 2.35;
                }
            }
            if( !matte_x ) {
                matte_h = (int) ((((double) sqwidth)/matte) + 0.5);
                matte_y = (height - matte_h) / 2;
                output->set_matte( sqwidth, matte_h );
            }
            if( osd && !commands_menu_active( commands ) ) {
                osd_list_matte( osd, matte_mode, sixteennine );
            }
            build_matte_menu( commands_get_menu( commands, "matte" ),
                              matte_mode, sixteennine );
            commands_refresh_menu( commands );
        }
        if( commands_toggle_pulldown_detection( commands ) ) {
            if( height == 480 ) {
                tvtime->pulldown_alg =
                    (tvtime->pulldown_alg + 1) % PULLDOWN_MAX;
                commands_set_pulldown_alg( commands, tvtime->pulldown_alg );
                if( osd ) {
                    if( tvtime->pulldown_alg == PULLDOWN_NONE ) {
                        tvtime_osd_show_message( osd,
                              _("2-3 pulldown inversion disabled.") );
                    } else if( tvtime->pulldown_alg == PULLDOWN_VEKTOR ) {
                        tvtime_osd_show_message( osd,
                              _("2-3 pulldown inversion enabled.") );
                    }
                    commands_refresh_menu( commands );
                }
            } else if( osd ) {
                tvtime_osd_show_message( osd,
                    _("2-3 pulldown inversion is not"
                      " valid with your TV norm.") );
            }
        }
        if( commands_show_deinterlacer_info( commands ) ) {
            osd_list_deinterlacer_info( osd, curmethodid );
        }
        if( !output->is_interlaced() &&
            commands_toggle_deinterlacer( commands ) ) {
            curmethodid = (curmethodid + 1) % get_num_deinterlace_methods();
            curmethod = get_deinterlace_method( curmethodid );
            tvtime_set_deinterlacer( tvtime, curmethod );
            if( osd ) {
                build_deinterlacer_menu ( commands_get_menu( commands,
                                                             "deinterlacer" ),
                                          curmethodid );
                build_deinterlacer_description_menu
                    ( commands_get_menu ( commands, "deintdescription" ),
                      curmethodid );
                commands_refresh_menu( commands );
                if( !commands_menu_active( commands ) )
                    osd_list_deinterlacers( osd, curmethodid );
                tvtime_osd_set_deinterlace_method( osd, curmethod->name );
                tvtime_osd_show_info( osd );
            }
            config_save( ct, "DeinterlaceMethod", curmethod->short_name );
        }
        if( !output->is_interlaced() && commands_set_deinterlacer( commands ) ) {
            curmethodid = 0;
            curmethod = get_deinterlace_method( 0 );
            while( strcasecmp( commands_get_new_deinterlacer( commands ),
                               curmethod->short_name ) ) {
                curmethodid =
                    (curmethodid + 1) % get_num_deinterlace_methods();
                curmethod = get_deinterlace_method( curmethodid );
                if( !curmethodid ) break;
            }
            if( osd ) {
                build_deinterlacer_menu
                    ( commands_get_menu( commands, "deinterlacer" ),
                      curmethodid );
                build_deinterlacer_description_menu
                    ( commands_get_menu( commands, "deintdescription" ),
                      curmethodid );
                commands_refresh_menu( commands );
            }
            tvtime_set_deinterlacer( tvtime, curmethod );
            config_save( ct, "DeinterlaceMethod", curmethod->short_name );
        }
        if( commands_set_freq_table( commands ) ) {
            station_writeconfig( stationmgr );
            station_delete( stationmgr );
            stationmgr = station_new( videoinput_get_norm_name( norm ),
                                      commands_get_new_freq_table( commands ),
                                      config_get_ntsc_cable_mode( ct ),
                                      verbose );
            commands_set_station_mgr( commands, stationmgr );
            config_save( ct, "Frequencies",
                         commands_get_new_freq_table( commands ) );
        }
        if( commands_resize_window( commands ) ) {
            output->set_window_height( output->get_visible_height() );
        }
        if( current_id != last_current_id || commands_xmltv_updated( commands ) ) {
            const char *curline = commands_get_xmltv_title( commands );
            char caption[ 256 ];
            if( !curline && config_get_show_taglines( ct ) ) {
                curline = tagline;
            }
            if( curline ) {
                if( current_id ) {
                    snprintf( caption, sizeof( caption ), "tvtime [%d]: %s",
                              current_id, curline );
                } else {
                    snprintf( caption, sizeof( caption ), "tvtime: %s",
                              curline );
                }
            } else {
                if( current_id ) {
                    snprintf( caption, sizeof( caption ), "tvtime [%d]",
                              current_id );
                } else {
                    snprintf( caption, sizeof( caption ), "tvtime" );
                }
            }
            output->set_window_caption( caption );
            last_current_id = current_id;
        }
        if( commands_tuner_reset( commands ) && vidin ) {
            output->update_xawtv_station( videoinput_get_tuner_freq( vidin ),
                                          station_get_current_id( stationmgr ),
                                          station_get_current_channel_name( stationmgr ) );
        }

        commands_next_frame( commands );

        /* Notice this because it's cheap. */
        videofilter_set_colour_invert( tvtime->inputfilter,
                                commands_apply_colour_invert( commands ) );
        videofilter_set_mirror( tvtime->inputfilter,
                                commands_apply_mirror( commands ) );
        videofilter_set_chroma_kill( tvtime->inputfilter,
                                     commands_apply_chroma_kill( commands ) );

        if( osd ) {
            tvtime_osd_set_pulldown( osd, tvtime->pulldown_alg );
            if( tvtime->pulldown_alg == PULLDOWN_NONE ) {
                tvtime_osd_set_film_mode( osd, -1 );
            } else {
                tvtime_osd_set_film_mode( osd, tvtime->filmmode );
            }
        }


        /**
         * Acquire the next frame.
         */
        if( vidin ) {
            tuner_state = videoinput_check_for_signal
                ( vidin, commands_check_freq_present( commands ) );
        } else {
            tuner_state = TUNER_STATE_NO_SIGNAL;
        }

        if( has_signal && tuner_state != TUNER_STATE_HAS_SIGNAL ) {
            has_signal = 0;
            if( lastframe ) {
                save_last_frame( saveframe, lastframe, width, height,
                                 width * 2, width * 2 );
            }
            fadepos = 0;
        }

        if( tuner_state == TUNER_STATE_HAS_SIGNAL ) {
            has_signal = 1;
            if( osd ) tvtime_osd_signal_present( osd, 1 );
        } else if( tuner_state == TUNER_STATE_NO_SIGNAL ) {
            if( osd ) tvtime_osd_signal_present( osd, 0 );
            if( fadepos < 256 ) {
                crossfade_frame( fadeframe, saveframe, blueframe, width,
                                 height, width*2, width*2, width*2, fadepos );
                fadepos += fadespeed;
            }
        }

        if( !paused && (tuner_state == TUNER_STATE_HAS_SIGNAL ||
                        tuner_state == TUNER_STATE_SIGNAL_DETECTED) ) {
            acquired = 1;
            if( vidin ) {
                curframe = videoinput_next_frame( vidin, &curframeid );
                if( !curframe ) {
                    has_signal = 0;
                    if( lastframe ) {
                        save_last_frame( saveframe, lastframe, 
                                         width, height, width*2, width*2 );
                    }
                    fadepos = 0;
                    tuner_state = TUNER_STATE_NO_SIGNAL;
                    acquired = 0;
                } else {
                    /* Make sure our buffers are valid. */
                    if( fieldsavailable == 2 ) {
                        if( videoinput_buffer_invalid( vidin, lastframeid ) ) {
                            lastframe = curframe;
                            lastframeid = -1;
                        }
                    } else if( fieldsavailable == 4 ) {
                        if( videoinput_buffer_invalid( vidin,
                                                       secondlastframeid ) ) {
                            secondlastframe = curframe;
                            secondlastframeid = -1;
                        }
                        if( videoinput_buffer_invalid( vidin, lastframeid ) ) {
                            lastframe = curframe;
                            lastframeid = -1;
                        }
                    }
                }
            }
            tvtime->filtered_curframe = 0;
        } else {
            /**
             * Wait for the next field time.
             */
            if( rtctimer ) {
                while( performance_get_usecs_since_frame_acquired( perf )
                       < ( (fieldtime*2) -
                           (rtctimer_get_usecs( rtctimer ) / 2) ) ) {
                    rtctimer_next_tick( rtctimer );
                }
            } else {
                int timeleft = performance_get_usecs_since_frame_acquired( perf );
                if( timeleft < fieldtime ) {
                    usleep( fieldtime - timeleft );
                }
            }
        }

        if( tuner_state != TUNER_STATE_HAS_SIGNAL ) {
            if( fadepos == 0 ) {
                secondlastframe = lastframe = curframe = saveframe;
            } else if( fadepos >= 256 ) {
                secondlastframe = lastframe = curframe = blueframe;
            } else {
                secondlastframe = lastframe = curframe = fadeframe;
            }
        }

        if( !commands_scan_channels( commands ) && scanning ) {
            station_writeconfig( stationmgr );
            scanning = 0;
        }

        if( commands_scan_channels( commands ) && !scanning ) {
            scanning = 1;
            numscanned = 0;
            scanwait = scan_delay;
        }

        if( scanning ) {
            scanwait--;

            if( !scanwait ) {
                int before_pos;
                int after_pos;

                scanwait = scan_delay;
                if( tuner_state == TUNER_STATE_HAS_SIGNAL ) {
                    station_set_current_active( stationmgr, 1 );
                } else {
                    station_set_current_active( stationmgr, 0 );
                }
                before_pos = station_get_current_pos( stationmgr );
                commands_handle( commands, TVTIME_CHANNEL_INC, 0 );
                after_pos = station_get_current_pos( stationmgr );

                if( station_get_num_stations( stationmgr ) ) {
                    numscanned +=
                        (after_pos + station_get_num_stations( stationmgr ) -
                         before_pos) % station_get_num_stations( stationmgr );
                }

                /* Stop when we loop around. */
                if( numscanned > station_get_num_stations( stationmgr ) ||
                    !station_get_num_stations( stationmgr ) ) {
                    commands_handle( commands, TVTIME_CHANNEL_SCAN, 0 );
                }
            }
        }

        performance_checkpoint_acquired_input_frame( perf );


        /* Print statistics and check for missed frames. */
        if( printdebug ) {
            int framesize = width * height * 2;
            fprintf( stderr, "tvtime: Stats using '%s' at %dx%d%s%s.\n",
                      (curmethod) ? curmethod->name : "interlaced passthrough", 
                      width, height, realtime ? " [RT]" : "", rtctimer ? " [RTC]" : " [no RTC]" );
            if( curmethod && curmethod->doscalerbob ) {
                framesize = width * height;
            }
            performance_print_last_frame_stats( perf, framesize );
            if( osd ) {
                if( vidin ) {
                    osd_list_statistics( osd, perf,
                          (curmethod) ? curmethod->name : "interlaced passthrough",
                          width, height, framesize, fieldtime, framerate_mode,
                          videoinput_get_norm( vidin ) );
                } else {
                    osd_list_statistics( osd, perf,
                          (curmethod) ? curmethod->name : "interlaced passthrough",
                          width, height, framesize, fieldtime, framerate_mode,
                          (height == 576) );
                }
            }
        }
        if( config_get_debug( ct ) &&
            commands_check_freq_present( commands ) ) {
            if( curmethod )  {
                performance_print_frame_drops( perf, curmethod->doscalerbob ?
                                               (width * height) :
                                               (width * height * 2) );
            } else {
                performance_print_frame_drops( perf, width * height * 2 );
            }
        }

        /**
         * Show the bottom field.
         */
        if( exposed && (framerate_mode == FRAMERATE_FULL ||
                        framerate_mode == FRAMERATE_HALF_TFF) &&
            !output->is_interlaced() ) {
            if( curmethod->doscalerbob && !showbars ) {
                output_success = output->show_frame( output_x, output_y/2,
                                                     output_w, output_h/2 );
            } else {
                output_success = output->show_frame( output_x, output_y,
                                                     output_w, output_h );
            }
        }
        performance_checkpoint_show_bot_field( perf );
        if( !output_success ) break;


        /**
         * Deinterlace the top field.
         */
        if( exposed && (framerate_mode == FRAMERATE_FULL ||
                        framerate_mode == FRAMERATE_HALF_BFF) ) {
            if( output->is_interlaced() ) {
                if( send_fields ) {
                    /* Wait until we can draw the even field. */
                    output->wait_for_sync( 0 );

                    output->lock_output_buffer();
                    tvtime_build_interlaced_frame( tvtime,
                                                   output->get_output_buffer(),
                                                   curframe, 0, width, height,
                                                   width * 2, 
                                                   output->get_output_stride(),
                                                   0);
                    output->unlock_output_buffer();
                    output->show_frame( output_x, output_y/2, 
                                        output_w, output_h/2 );
                } else {
                    output->lock_output_buffer();
                    tvtime_build_interlaced_frame( tvtime,
                                                   output->get_output_buffer(),
                                                   curframe, 0, width, height,
                                                   width * 2,
                                                   output->get_output_stride(),
                                                   1 );
                    output->unlock_output_buffer();
                    output->show_frame( output_x, output_y/2, 
                                        output_w, output_h/2 );
                }
            } else {
                /* Build the output from the top field. */
                output->lock_output_buffer();
                if( showbars ) {
                    for( i = 0; i < height; i++ ) {
                        blit_packed422_scanline( output->get_output_buffer() +
                              i*output->get_output_stride(),
                              colourbars + (i*width*2), width );
                    }
                } else {
                    if( curmethod->doscalerbob ) {
                        tvtime_build_copied_field( tvtime,
                              output->get_output_buffer(),
                              curframe, 0, width, height, width * 2,
                              output->get_output_stride () );
                    } else {
                        tvtime_build_deinterlaced_frame( tvtime,
                              output->get_output_buffer(),
                              curframe, lastframe, secondlastframe,
                              0, 0, width, height, width * 2,
                              output->get_output_stride() );
                    }
                }

                /*
                 * For the screenshot, use the output
                 * after we build the top field.
                 */
                if( screenshot ) {
                    const char *basename;
                    const char *outfile;
                    char *filename;
                    char *pathfilename;
                    char *message;
                    char timestamp[ 256 ];
                    time_t tm = time( 0 );

                    if( *commands_screenshot_filename( commands ) ) {
                        basename = outfile = commands_screenshot_filename( commands );
                    } else {
                        strftime( timestamp, 
                                  sizeof( timestamp ),
                                  config_get_timeformat( ct ),
                                  localtime( &tm ) );
                        asprintf( &filename,
                                  "tvtime-output-%s.png",
                                  timestamp );
                        asprintf( &pathfilename,
                                  "%s/%s",
                                  config_get_screenshot_dir( ct ),
                                  filename );
                        outfile = pathfilename;
                        basename = filename;
                    }
                    if( curmethod->doscalerbob ) {
                        pngscreenshot( outfile, output->get_output_buffer(),
                                       width, height/2, width * 2 );
                    } else {
                        pngscreenshot( outfile, output->get_output_buffer(),
                                       width, height, width * 2 );
                    }
                    asprintf( &message, _("Screenshot: %s"), basename );
                    if( osd && !quiet_screenshots ) {
                        tvtime_osd_show_message( osd, message );
                    }
                    screenshot = 0;
                }
                output->unlock_output_buffer();
            }
        }

        performance_checkpoint_constructed_top_field( perf );


        /**
         * We're done with the secondlastframe now.
         */
        if( vidin && acquired ) {
            if( fieldsavailable == 4 ) {
                if( secondlastframeid >= 0 )
                    videoinput_free_frame( vidin, secondlastframeid );
                if( vbidata ) {
                    vbidata_process_frame( vbidata, printdebug );
		}
            } else if( fieldsavailable == 2 ) {
               if( lastframeid >= 0 )
                   videoinput_free_frame( vidin, lastframeid );
                if( vbidata ) vbidata_process_frame( vbidata, printdebug );
            }
        }


        /**
         * Wait for the next field time.
         */
        if( rtctimer ) {
            while( performance_get_usecs_since_frame_acquired( perf )
                   < ( fieldtime - (rtctimer_get_usecs( rtctimer ) / 2) ) ) {
                rtctimer_next_tick( rtctimer );
            }
        } else {
            int timeleft = performance_get_usecs_since_frame_acquired( perf );

            if( timeleft < fieldtime ) {
                usleep( fieldtime - timeleft );
            }
        }
        performance_checkpoint_wait_for_bot_field( perf );


        /**
         * Show the top field.
         */
        if( exposed && (framerate_mode == FRAMERATE_FULL ||
                        framerate_mode == FRAMERATE_HALF_BFF) &&
            !output->is_interlaced() ) {
            if( curmethod->doscalerbob && !showbars ) {
                output_success = output->show_frame( output_x, output_y/2,
                                                     output_w, output_h/2 );
            } else {
                output_success = output->show_frame( output_x, output_y,
                                                     output_w, output_h );
            }
        }
        performance_checkpoint_show_top_field( perf );
        if( !output_success ) break;


        /**
         * Deinterlace the bottom field.
         */
        if( exposed && (framerate_mode == FRAMERATE_FULL ||
                        framerate_mode == FRAMERATE_HALF_TFF) ) {
            if( output->is_interlaced() ) {
                if( send_fields ) {
                    /* Wait until we can draw the odd field. */
                    output->wait_for_sync( 1 );
                    
                    output->lock_output_buffer();
                    tvtime_build_interlaced_frame
                        ( tvtime, output->get_output_buffer(), curframe, 1,
                          width, height, width * 2, 
                          output->get_output_stride(), 0 );
                    output->unlock_output_buffer();
                    output->show_frame( output_x, output_y/2, 
                                        output_w, output_h/2 );
                }
            } else {
                /* Build the output from the bottom field. */
                output->lock_output_buffer();
                if( showbars ) {
                    for( i = 0; i < height; i++ ) {
                        blit_packed422_scanline
                            ( output->get_output_buffer() +
                              i*output->get_output_stride(),
                              colourbars + (i*width*2), width );
                    }
                } else {
                    if( curmethod->doscalerbob ) {
                        tvtime_build_copied_field( tvtime,
                              output->get_output_buffer(),
                              curframe, 1, width, height, width * 2,
                              output->get_output_stride() );
                    } else {
                        tvtime_build_deinterlaced_frame( tvtime,
                              output->get_output_buffer(),
                              curframe, lastframe, secondlastframe, 1, 1,
                              width, height, width * 2,
                              output->get_output_stride() );
                    }
                }

                /*
                 * For the screenshot, use may need to use
                 * the output after we build the bot field.
                 */
                if( screenshot ) {
                    const char *basename;
                    const char *outfile;
                    char filename[ 256 ];
                    char message[ 512 ];
                    char pathfilename[ 256 ];
                    char timestamp[ 50 ];
                    time_t tm = time( 0 );

                    if( *commands_screenshot_filename( commands ) ) {
                        basename = outfile = commands_screenshot_filename( commands );
                    } else {
                        strftime( timestamp, sizeof( timestamp ),
                                  config_get_timeformat( ct ),
                                  localtime( &tm ) );
                        snprintf( filename, sizeof( filename ),
                                  "tvtime-output-%s.png", timestamp );
                        snprintf( pathfilename, sizeof( pathfilename ),
                                  "%s/%s",
                                  config_get_screenshot_dir( ct ), filename );
                        outfile = pathfilename;
                        basename = filename;
                    }
                    if( curmethod->doscalerbob ) {
                        pngscreenshot( outfile, output->get_output_buffer(),
                                       width, height/2, width * 2 );
                    } else {
                        pngscreenshot( outfile, output->get_output_buffer(),
                                       width, height, width * 2 );
                    }
                    snprintf( message, sizeof( message ),
                              _("Screenshot: %s"), basename );
                    if( osd && !quiet_screenshots ) {
                        tvtime_osd_show_message( osd, message );
                    }
                    screenshot = 0;
                }
                output->unlock_output_buffer();
            }
        }
        performance_checkpoint_constructed_bot_field( perf );


        /* We're done with the input now. */
        if( vidin && acquired ) {
            if( fieldsavailable == 4 ) {
                secondlastframeid = lastframeid;
                secondlastframe = lastframe;
                lastframeid = curframeid;
                lastframe = curframe;
            } else if( fieldsavailable == 2 ) {
                lastframeid = curframeid;
                lastframe = curframe;
            } else {
                if( curframeid >= 0 )
                    videoinput_free_frame( vidin, curframeid );
                if( vbidata ) vbidata_process_frame( vbidata, printdebug );
            }
        }
    }

    /* Return to normal scheduling. */
    set_default_priority();

    /* Remember to save our settings if we were scanning. */
    if( scanning ) {
        station_writeconfig( stationmgr );
    }

    if( commands_get_new_input_width( commands ) ) {
        snprintf( number, 4, "%d", commands_get_new_input_width( commands ) );
        config_save( ct, "InputWidth", number );
    }

    snprintf( number, 4, "%d", commands_get_global_brightness( commands ) );
    config_save( ct, "DefaultBrightness", number );
    snprintf( number, 4, "%d", commands_get_global_contrast( commands ) );
    config_save( ct, "DefaultContrast", number );
    snprintf( number, 4, "%d", commands_get_global_saturation( commands ) );
    config_save( ct, "DefaultSaturation", number );
    snprintf( number, 4, "%d", commands_get_global_hue( commands ) );
    config_save( ct, "DefaultHue", number );

    if( commands_get_new_norm( commands ) ) {
        config_save( ct, "Norm", commands_get_new_norm( commands ) );
    }

    snprintf( number, 4, "%d", station_get_prev_id( stationmgr ) );
    config_save( ct, "PrevChannel", number );

    snprintf( number, 4, "%d", station_get_current_id( stationmgr ) );
    config_save( ct, "Channel", number );

    snprintf( number, 4, "%d", framerate_mode );
    config_save( ct, "FramerateMode", number );

    /* Make sure floating point numbers are always written to the config
     * file in "C" locale format.
     */
    strncpy( prevloc, setlocale( LC_NUMERIC, NULL ), sizeof( prevloc ) );
    setlocale( LC_NUMERIC, "C" );
    snprintf( number, 4, "%2.1f",
              commands_get_overscan( commands ) * 2.0 * 100.0 );
    config_save( ct, "OverScan", number );
    setlocale( LC_NUMERIC, prevloc );

    snprintf( number, 4, "%d", commands_check_freq_present( commands ) );
    config_save( ct, "CheckForSignal", number );

    snprintf( number, 4, "%d", commands_get_audio_boost( commands ) );
    config_save( ct, "AudioBoost", number );

    snprintf( number, 4, "%d", output->is_alwaysontop() );
    config_save( ct, "AlwaysOnTop", number );

    snprintf( number, 4, "%d", quiet_screenshots );
    config_save( ct, "QuietScreenshots", number );

    snprintf( number, 6, "%d", mixer_get_unmute_volume() );
    config_save( ct, "UnmuteVolume", number );

    snprintf( number, 4, "%d", mixer_ismute() );
    config_save( ct, "Muted", number );

    if( config_get_mute_on_exit( ct ) ) {
        mixer_mute( 1 );
    }

    if( vidin ) {
        snprintf( number, 4, "%d", videoinput_get_input_num( vidin ) );
        config_save( ct, "V4LInput", number );

        if( videoinput_get_audio_mode( vidin ) == VIDEOINPUT_MONO ) {
            config_save( ct, "AudioMode", "mono" );
        } else if( videoinput_get_audio_mode( vidin ) == VIDEOINPUT_LANG1 ) {
            config_save( ct, "AudioMode", "lang1" );
        } else if( videoinput_get_audio_mode( vidin ) == VIDEOINPUT_LANG2 ) {
            config_save( ct, "AudioMode", "lang2" );
        } else {
            config_save( ct, "AudioMode", "stereo" );
        }

        snprintf( number, 4, "%d", videoinput_get_pal_audio_mode( vidin ) );
        config_save( ct, "PalDKMode", number );
    }

    output->shutdown();

    if( verbose ) {
        fprintf( stderr, "tvtime: Cleaning up.\n" );
    }
    if( vidin ) {
        videoinput_delete( vidin );
    }
    config_delete( ct );
    input_delete( in );
    commands_delete( commands );
    performance_delete( perf );
    station_writeconfig( stationmgr );
    station_delete( stationmgr );
    if( fifo ) {
        fifo_delete( fifo );
    }
    if( vbidata ) {
        vbidata_delete( vbidata );
    }
    if( vs ) {
        vbiscreen_delete( vs );
    }
    if( osd ) {
        tvtime_osd_delete( osd );
    }
    mixer_close_device();

    /* Free temporary memory. */
    free( colourbars );
    free( saveframe );
    free( fadeframe );
    free( blueframe );

    /* Free state. */
    tvtime_delete( tvtime );

    xmlCleanupParser();

    if( restarttvtime ) {
        lfprintf( stderr, _("Restarting tvtime.\n") );
        return 2;
    }

    lfprintf( stderr, _("Thank you for using tvtime.\n") );
    return return_value;
}

int main( int argc, char **argv )
{
    rtctimer_t *rtctimer = 0;
    int read_stdin = 1;
    int result = 0;
    int realtime = 0;
    uid_t priv_uid = geteuid();
    uid_t user_uid = getuid();

    /*
     * Temporarily drop down to user-level access, so that files aren't
     * created setuid root.
     */
    if( seteuid( user_uid ) == -1 ) {
        lfprintf( stderr, _("\n"
    "    Failed to drop root privileges: %s.\n"
    "    tvtime will now exit to avoid security problems.\n\n"),
            strerror( errno ) );
        return 1;
    }

    /*
     * Setup i18n. This has to be done as early as possible in order
     * to show startup messages in the users preferred language.
     */
    setup_i18n();

    /*
     * Make gettext return UTF-8 strings, and setup lfprintf.
     */
    setup_utf8();

    lfprintf( stderr, _("Running %s.\n"), PACKAGE_STRING );

    /* Disable this code for a release. */
    /*
    fprintf( stderr, "\n"
             "*** WARNING: This is a DEVELOPMENT version of tvtime.\n"
             "*** Please do not redistribute snapshots, and please submit\n"
             "*** bug reports for released versions only!\n\n" );
    */

    /* Steal system resources in the name of performance. */
    /* Get maximum priority before dropping root privileges. We'll drop back */
    /* to the value specified in the config file (or the default) later. */
    seteuid( priv_uid );
    setpriority( PRIO_PROCESS, 0, -19 );
    if( set_realtime_priority( 0 ) ) {
        realtime = 1;
    }

    rtctimer = rtctimer_new( 0 );
    if( rtctimer ) {
        if( !rtctimer_set_interval( rtctimer, 1024 ) &&
            !rtctimer_set_interval( rtctimer, 64 ) ) {
            rtctimer_delete( rtctimer );
            rtctimer = 0;
        } else {
            rtctimer_start_clock( rtctimer );
            if( rtctimer_get_resolution( rtctimer ) < 1024 ) {
                rtctimer_delete( rtctimer );
                rtctimer = 0;
            }
        }
    }


    /* We've now stolen all our root-requiring resources, drop to a user. */
    if( setuid( user_uid ) == -1 ) {
        /*
         * This used to say "Unknown problems", but we're printing an
         * error string, so that didn't really make sense, did it?
         */
        lfprintf( stderr, _("\n"
    "    Failed to drop root privileges: %s.\n"
    "    tvtime will now exit to avoid security problems.\n\n"),
            strerror( errno ) );
        return 1;
    }


    /* Ditch stdin early. */
    if( isatty( STDIN_FILENO ) ) {
        read_stdin = 0;
        close( STDIN_FILENO );
    }

    /* Run tvtime. */
    for(;;) {
        if( result == 2 ) {
            char *new_argv[ 2 ];

            new_argv[ 0 ] = "tvtime";
            new_argv[ 1 ] = 0;

            result = tvtime_main( rtctimer, read_stdin, realtime, 0, new_argv );
        } else {
            result = tvtime_main( rtctimer, read_stdin, realtime, argc, argv );
        }

        if( result != 2 ) break;
    }

    if( rtctimer ) {
        rtctimer_delete( rtctimer );
    }

    return result;
}

Privacy Policy