File: caption_decoder.c

package info (click to toggle)
zapping 0.10~cvs6-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 9,856 kB
  • ctags: 11,841
  • sloc: ansic: 111,154; asm: 11,770; sh: 9,812; xml: 2,742; makefile: 1,283; perl: 488
file content (2227 lines) | stat: -rw-r--r-- 50,316 bytes parent folder | download | duplicates (6)
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
/*
 *  libzvbi - Closed Caption decoder
 *
 *  Copyright (C) 2000-2005 Michael H. Schimek
 *
 *  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 of the License, 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* $Id: caption_decoder.c,v 1.2 2006/05/06 09:11:09 mschimek Exp $ */

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

#include "hamm.h"
#include "page-priv.h"
#include "caption_decoder-priv.h"

#define XDS_EVENTS 0 /* TODO */
#define CAPTION_EVENTS (VBI3_EVENT_CC_PAGE |				\
			VBI3_EVENT_CC_RAW |				\
			VBI3_EVENT_PAGE_TYPE)

/*
    Resources:

    Code of Federal Regulations, Title 47 Telecommunication,
    Section 15.119 "Closed caption decoder requirements for analog
    television receivers".

    EIA 608-B "Recommended Practice for Line 21 Data Service"
    http://global.ihs.com for those with money to burn.

    Video Demystified
    http://www.video-demystified.com

    Related documents:

    47 CFR Section 15.120 "Program blocking technology requirements
    for television receivers".

    "Analog television receivers will receive program ratings
    transmitted pursuant to industry standard EIA-744 "Transport of
    Content Advisory Information Using Extended Data Service (XDS)"
    and EIA-608 "Recommended Practice for Line 21 Data Service". This
    incorporation by reference was approved by the Director of the
    Federal Register in accordance with 5 U.S.C. 522(a) and 1 CFR
    Part 51. [...] Copies of EIA-744 and EIA-608 may be obtained from:
    Global Engineering Documents, 15 Inverness Way East, Englewood,
    CO 80112-5704. Copies of EIA-744 may be inspected during normal
    business hours at the following locations: Federal Communications
    Commission, 2000 M Street, NW, Technical Information Center (Suite
    230), Washington, DC, or the Office of the Federal Register, 800
    North Capitol Street, NW, Suite 700, Washington, DC."

    47 CFR Section 15.122 "Closed caption decoder requirements for
    digital television receivers and converter boxes".

    "Digital television receivers and tuners must be capable of
    decoding closed captioning information that is delivered pursuant
    to the industry standard EIA-708-B, "Digital Television (DTV)
    Closed Captioning," Electronics Industries Association (1999). This
    incorporation by reference was approved by the Director of the
    Federal Register in accordance with 5 U.S.C. 552(a) and 1 CFR
    part 51. Digital television manufacturers may wish to view EIA-708-B
    in its entirety. Copies of EIA-708-B may be obtained from: Global
    Engineering Documents, 15 Inverness Way East, Englewood, CO
    80112-5704, http://www.global.ihs.com/. Copies of EIA-708-B may be
    inspected during regular business hours at the following locations:
    Federal Communications Commission, 445 12th Street, S.W., Washington,
    D.C. 20554, or the Office of the Federal Register, 800 N. Capitol
    Street, N.W., Washington, D.C."
*/

/* Closed Caption decoder ****************************************************/

#define ALL_DIRTY ((1 << MAX_ROWS) - 1)

#define WORD_UPDATE (VBI3_CHAR_UPDATE | VBI3_WORD_UPDATE)
#define ROW_UPDATE (WORD_UPDATE | VBI3_ROW_UPDATE)
#define PAGE_UPDATE (ROW_UPDATE | VBI3_PAGE_UPDATE)

static const vbi3_char
transparent_space [2] = {
	{
		/* Caption channels. */
		.attr			= 0,
		.size			= VBI3_NORMAL_SIZE,
		.opacity		= VBI3_TRANSPARENT_SPACE,
		.foreground		= VBI3_WHITE,
		.background		= VBI3_BLACK,
		.drcs_clut_offs		= 0,
		.unicode		= 0x0020
	}, {
		/* Text channels. */
		.attr			= 0,
		.size			= VBI3_NORMAL_SIZE,
		.opacity		= VBI3_OPAQUE,
		.foreground		= VBI3_WHITE,
		.background		= VBI3_BLACK,
		.drcs_clut_offs		= 0,
		.unicode		= 0x0020
	}
};

#define TRANSPARENT_SPACE(cd, ch)					\
	transparent_space[((ch)						\
	  >= &(cd)->cc.channel[VBI3_CAPTION_T1 - VBI3_CAPTION_CC1])]

static const vbi3_color
color_mapping [8] = {
	VBI3_WHITE, VBI3_GREEN, VBI3_BLUE, VBI3_CYAN,
	VBI3_RED, VBI3_YELLOW, VBI3_MAGENTA, VBI3_BLACK
};

#define IS_OPAQUE(c)							\
	(VBI3_TRANSPARENT_SPACE != (c).opacity && 0x0020 != (c).unicode)

/**
 */
void
vbi3_cc_channel_stat_destroy	(vbi3_cc_channel_stat *	cs)
{
	assert (NULL != cs);

	CLEAR (*cs);
}

/**
 */
void
vbi3_cc_channel_stat_init	(vbi3_cc_channel_stat *	cs)
{
	assert (NULL != cs);

	CLEAR (*cs);

	cs->page_type		= VBI3_UNKNOWN_PAGE;
	cs->last_received	= 0.0;
}

vbi3_bool
vbi3_caption_decoder_get_cc_channel_stat
				(vbi3_caption_decoder *	cd,
				 vbi3_cc_channel_stat *	cs,
				 vbi3_pgno		channel)
{
	caption_channel *ch;

	assert (NULL != cd);
	assert (NULL != cs);

	if (channel < VBI3_CAPTION_CC1 || channel > VBI3_CAPTION_T4)
		return FALSE;

	ch = &cd->cc.channel[channel - VBI3_CAPTION_CC1];

	CLEAR (*cs);

	cs->channel = channel;

	cs->page_type = (channel >= VBI3_CAPTION_T1) ?
		VBI3_NORMAL_PAGE : VBI3_SUBTITLE_PAGE;

	cs->caption_mode = ch->mode;

	/* TODO language code */

	cs->last_received = ch->last_timestamp;

	return TRUE;
}

/* Add solid spaces for legibility, according to Sec. 15.119 (d)(1),
   (f)(1)(vii), (f)(1)(viii), (f)(2)(iii), (f)(3)(ii). */
static __inline__ void
copy_with_padding		(vbi3_char *		dcp,
				 const vbi3_char *	scp,
				 vbi3_char		ts,
				 unsigned int		dirty)
{
	unsigned int rowct;

	for (rowct = MAX_ROWS; rowct > 0; --rowct) {
		if (unlikely (dirty & 1)) {
			unsigned int colct;

			dcp[0] = ts;
			dcp[1] = scp[0];
			dcp[2] = scp[1];

			if (IS_OPAQUE (scp[0])) {
				dcp[0].opacity = VBI3_OPAQUE;
				dcp[0].background = scp[0].background;
				
				if (VBI3_OPAQUE != scp[1].opacity)
					dcp[2].opacity = VBI3_OPAQUE;
			}
			
			for (colct = MAX_COLUMNS - 2; colct > 0; --colct) {
				dcp[3] = scp[2];

				if (IS_OPAQUE (scp[1])) {
					if (VBI3_OPAQUE != scp[0].opacity) {
						dcp[1].opacity = VBI3_OPAQUE;
					}

					if (VBI3_OPAQUE != scp[2].opacity) {
						dcp[3].opacity = VBI3_OPAQUE;
						dcp[3].background =
							scp[1].background;
					}
				}

				++scp;
				++dcp;
			}

			dcp[3] = ts;

			if (IS_OPAQUE (scp[1])) {
				if (VBI3_OPAQUE != scp[0].opacity)
					dcp[1].opacity = VBI3_OPAQUE;

				dcp[3].opacity = VBI3_OPAQUE;
				dcp[3].background = scp[1].background;
			}

			scp += 2;
			dcp += 4;
		} else {
			vbi3_char *end;

			end = dcp + MAX_COLUMNS + 2;

			while (dcp < end)
				*dcp++ = ts;

			scp += MAX_COLUMNS;
		}

		dirty >>= 1;
	}
}

/**
 * DOCUMENT ME
 */
vbi3_page *
vbi3_caption_decoder_get_page_va_list
				(vbi3_caption_decoder *	cd,
				 vbi3_pgno		channel,
				 va_list		format_options)
{
	/* This map corresponds to enum vbi3_color names, NOT closed
	   caption colors. We could do without color_mapping[] but it's
	   better to use the same color indices everywhere. */
	static const vbi3_rgba default_color_map [8] = {
		VBI3_RGBA (0x00, 0x00, 0x00), VBI3_RGBA (0xFF, 0x00, 0x00),
		VBI3_RGBA (0x00, 0xFF, 0x00), VBI3_RGBA (0xFF, 0xFF, 0x00),
		VBI3_RGBA (0x00, 0x00, 0xFF), VBI3_RGBA (0xFF, 0x00, 0xFF),
		VBI3_RGBA (0x00, 0xFF, 0xFF), VBI3_RGBA (0xFF, 0xFF, 0xFF)
	};
	vbi3_color option_foreground;
	vbi3_color option_background;
	vbi3_bool option_row_change;
	vbi3_page *pg;
	vbi3_page_priv *pgp;
	const vbi3_character_set *cs;
	caption_channel *ch;
	unsigned int n;
	vbi3_char ts;

	assert (NULL != cd);

	if (channel < VBI3_CAPTION_CC1 || channel > VBI3_CAPTION_T4)
		return NULL;

	ch = &cd->cc.channel[channel - VBI3_CAPTION_CC1];

	/* This clears pgp and initializes pg->priv and pg->ref_count. */
	if (!(pg = vbi3_page_new ()))
		return NULL;

	pgp = pg->priv;

	pgp->cn = cache_network_ref (cd->network);

	pgp->pg.cache = cd->cache;
	pgp->pg.network = &cd->network->network;

	pgp->pg.pgno = channel;

	pgp->pg.rows = MAX_ROWS;
	pgp->pg.columns = MAX_COLUMNS;

	assert (N_ELEMENTS (pgp->pg.text)
		>= MAX_ROWS * (MAX_COLUMNS + 2));

	/* Must be initialized for export. */
	cs = vbi3_character_set_from_code (0);
	pgp->char_set[0] = cs;
	pgp->char_set[1] = cs;

	option_foreground = -1;
	option_background = -1;
	option_row_change = FALSE;

	for (;;) {
		vbi3_format_option option;

		option = va_arg (format_options, vbi3_format_option);

		switch (option) {
		case VBI3_PADDING:
			/* Turns the text  [foo]   [bar baz]
			   into           [ foo ] [ bar baz ] */
			pgp->pg.columns =
				va_arg (format_options, vbi3_bool) ?
				MAX_COLUMNS + 2 : MAX_COLUMNS;
			break;

		case VBI3_ROW_CHANGE:
			option_row_change = va_arg (format_options, vbi3_bool);
			break;

		case VBI3_DEFAULT_FOREGROUND:
			pgp->pg.color_map[8] =
				va_arg (format_options, vbi3_rgba);

			if (VBI3_RGBA (0xFF, 0xFF, 0xFF)
			    & (default_color_map[VBI3_WHITE]
			       ^ pgp->pg.color_map[8]))
				option_foreground = VBI3_WHITE;
			break;

		case VBI3_DEFAULT_BACKGROUND:
			pgp->pg.color_map[9] =
				va_arg (format_options, vbi3_rgba);

			if (VBI3_RGBA (0xFF, 0xFF, 0xFF)
			    & (default_color_map[VBI3_BLACK]
			       ^ pgp->pg.color_map[9]))
				option_background = VBI3_BLACK;
			break;

		default:
			option = 0;
			break;
		}

		if (0 == option)
			break;
	}

	ts = TRANSPARENT_SPACE (cd, ch);

	if (option_background == ts.background)
		ts.background = 9;

	n = ch->displayed_buffer;
	if (option_row_change && VBI3_CAPTION_MODE_POP_ON != ch->mode)
		n = 2;

	if (ch->dirty[n] <= 0) {
		vbi3_char *cp;
		vbi3_char *end;

		cp = pgp->pg.text;
		end = pgp->pg.text + pgp->pg.rows * pgp->pg.columns;

		while (cp < end)
			*cp++ = ts;
	} else {
		if (pgp->pg.columns > MAX_COLUMNS) {
			copy_with_padding (pgp->pg.text,
					   ch->buffer[n][FIRST_ROW],
					   ts, ch->dirty[n]);
		} else {
			assert (sizeof (pgp->pg.text)
				>= sizeof (ch->buffer[0]));
			memcpy (pgp->pg.text,
				ch->buffer[n][FIRST_ROW],
				sizeof (ch->buffer[0]));
		}

		if ((int) option_foreground >= 0
		    || (int) option_background >= 0) {
			vbi3_char *cp;
			vbi3_char *end;

			cp = pgp->pg.text;
			end = pgp->pg.text + pgp->pg.rows * pgp->pg.columns;

			while (cp < end) {
				if (option_foreground == cp->foreground)
					cp->foreground = 8;

				if (option_background == cp->background)
					cp->background = 9;

				++cp;
			}
		}
	}

	pgp->pg.screen_color = ts.background;
	pgp->pg.screen_opacity = ts.opacity;

	assert (sizeof (pgp->pg.color_map) >= sizeof (default_color_map));
	memcpy (pgp->pg.color_map, default_color_map,
		sizeof (default_color_map));

	return pg;
}

/**
 * DOCUMENT ME
 */
vbi3_page *
vbi3_caption_decoder_get_page	(vbi3_caption_decoder *	cd,
				 vbi3_pgno		channel,
				 ...)
{
	vbi3_page *pg;
	va_list format_options;

	va_start (format_options, channel);

	pg = vbi3_caption_decoder_get_page_va_list
		(cd, channel, format_options);

	va_end (format_options);

	return pg;
}

static void
send_event			(vbi3_caption_decoder *	cd,
				 caption_channel *	ch,
				 unsigned int		event_type,
				 vbi3_cc_page_flags	flags)
{
	vbi3_event e;

	e.type = event_type;
	e.network = &cd->network->network;
	e.timestamp = cd->timestamp;

	e.ev.caption.channel = (ch - cd->cc.channel) + VBI3_CAPTION_CC1;
	e.ev.caption.flags = flags;

	_vbi3_event_handler_list_send (&cd->handlers, &e);

	cd->cc.event_pending = NULL;
}

static void
row_change_snapshot		(caption_channel *	ch)
{
	unsigned int n;

	/* Copy displayed buffer to row-change-snapshot buffer
	   (when a row is complete) for VBI3_ROW_CHANGE formatting option. */

	n = ch->displayed_buffer;

	if (ch->dirty[n] > 0)
		memcpy (ch->buffer[2], ch->buffer[n], sizeof (ch->buffer[2]));

	ch->dirty[2] = ch->dirty[n];
}

static void
send_raw_event			(vbi3_caption_decoder *	cd,
				 caption_channel *	ch,
				 unsigned int		first_row,
				 unsigned int		last_row)
{
	vbi3_char buffer[MAX_COLUMNS + 1];
	vbi3_event e;
	unsigned int row;

	e.type = VBI3_EVENT_CC_RAW;
	e.network = &cd->network->network;
	e.timestamp = cd->timestamp;
	e.ev.cc_raw.channel = (ch - cd->cc.channel) + VBI3_CAPTION_CC1;
	e.ev.cc_raw.text = buffer;
	e.ev.cc_raw.length = MAX_COLUMNS;

	CLEAR (buffer[MAX_COLUMNS]);

	for (row = first_row; row <= last_row; ++row) {
		const vbi3_char *text;
		unsigned int column;

		text = ch->buffer[ch->displayed_buffer][row];

		for (column = FIRST_COLUMN; column <= LAST_COLUMN; ++column)
			if (VBI3_TRANSPARENT_SPACE != text[column].opacity)
				break;

		if (column > LAST_COLUMN)
			continue;

		memcpy (buffer, text, MAX_COLUMNS * sizeof (*text));

		e.ev.cc_raw.row = row;

		_vbi3_event_handler_list_send (&cd->handlers, &e);
	}
}

/* NOTE ch is invalid if CHANNEL_UNKNOWN == cd->cc.curr_ch_num. */
static caption_channel *
switch_channel			(vbi3_caption_decoder *	cd,
				 caption_channel *	ch,
				 vbi3_pgno		new_ch_num)
{
	if (CHANNEL_UNKNOWN != cd->cc.curr_ch_num
	    && VBI3_CAPTION_MODE_UNKNOWN != ch->mode) {
		/* Force a display update if we do not send events
		   on every display change. */
	}

	cd->cc.curr_ch_num = new_ch_num;

	return &cd->cc.channel[new_ch_num - VBI3_CAPTION_CC1];
}

static void
reset_curr_attr			(vbi3_caption_decoder *	cd,
				 caption_channel *	ch)
{
	ch->curr_attr = TRANSPARENT_SPACE (cd, ch);
	ch->curr_attr.opacity = VBI3_OPAQUE;
}

static void
set_cursor			(caption_channel *	ch,
				 unsigned int		column,
				 unsigned int		row)
{
	ch->curr_row = row;
	ch->curr_column = column;
}

static void
erase_memory			(vbi3_caption_decoder *	cd,
				 caption_channel *	ch,
				 unsigned int		n)
{
	vbi3_char ts;
	unsigned int i;

	ts = TRANSPARENT_SPACE (cd, ch);

	for (i = 0; i < MAX_ROWS * MAX_COLUMNS; ++i)
		ch->buffer[n][0][i] = ts;

	ch->dirty[n] = 0;
}

static void
put_char			(vbi3_caption_decoder *	cd,
				 caption_channel *	ch,
				 unsigned int		unicode)
{
	unsigned int n;
	unsigned int row;
	unsigned int column;

	n = ch->displayed_buffer ^ (VBI3_CAPTION_MODE_POP_ON == ch->mode);

	/* Execute lazy erase. */
	if (ch->dirty[n] < 0)
		erase_memory (cd, ch, n);

	/* Sec. 15.119 (f)(1)(v), (1)(vi), (2)(ii), (3)(i). */

	row = ch->curr_row;
	column = ch->curr_column;

	if (column < LAST_COLUMN)
		ch->curr_column = column + 1;

	ch->curr_attr.unicode = unicode;
	ch->buffer[n][row][column] = ch->curr_attr;

	assert (sizeof (ch->dirty[0]) * 8 - 1 >= MAX_ROWS);
	ch->dirty[n] |= 1 << row;

	/* Send a display update event when the displayed buffer of
	   the current channel changed, but no more than once for
	   each pair of Closed Caption bytes. */
	if (VBI3_CAPTION_MODE_POP_ON != ch->mode) {
		cd->cc.event_pending = ch;
	}
}

static void
put_transparent_space		(vbi3_caption_decoder *	cd,
				 caption_channel *	ch)
{
	unsigned int n;
	unsigned int row;
	unsigned int column;

	/* Sec. 15.119 (f)(1)(v), (1)(vi), (2)(ii), (3)(i). */

	row = ch->curr_row;
	column = ch->curr_column;

	if (column < LAST_COLUMN)
		ch->curr_column = column + 1;

	n = ch->displayed_buffer ^ (VBI3_CAPTION_MODE_POP_ON == ch->mode);

	/* No event if the buffer will be erased to TRANSPARENT_SPACEs
	   (dirty < 0) or the row still contains only TRANSPARENT_SPACEs. */
	if (ch->dirty[n] > 0 && (ch->dirty[n] & (1 << row))) {
		ch->buffer[n][row][column] = TRANSPARENT_SPACE (cd, ch);

		if (VBI3_CAPTION_MODE_POP_ON != ch->mode) {
			cd->cc.event_pending = ch;
		}
	}
}

static void
move_window			(vbi3_caption_decoder *	cd,
				 caption_channel *	ch,
				 unsigned int		new_base_row)
{
	vbi3_char *text;
	vbi3_char ts;
	unsigned int old_max_rows;
	unsigned int new_max_rows;
	unsigned int copy_rows;
	unsigned int copy_chars;
	unsigned int erase_begin;
	unsigned int erase_end;
	unsigned int n;

	n = ch->displayed_buffer;

	/* No event if the buffer contains all TRANSPARENT_SPACEs. */
	if (ch->dirty[n] <= 0)
		return;

	old_max_rows = ch->curr_row + 1 - FIRST_ROW;
	new_max_rows = new_base_row + 1 - FIRST_ROW;
	copy_rows = MIN (MIN (old_max_rows, new_max_rows), ch->window_rows);
	copy_chars = copy_rows * MAX_COLUMNS;

	text = ch->buffer[n][FIRST_ROW];
	ts = TRANSPARENT_SPACE (cd, ch);

	if (new_base_row < ch->curr_row) {
		erase_begin = (new_base_row + 1) * MAX_COLUMNS;
		erase_end = (ch->curr_row + 1) * MAX_COLUMNS;

		memmove (text + erase_begin - copy_chars,
			 text + erase_end - copy_chars,
			 copy_chars * sizeof (*text));

		ch->dirty[n] >>= ch->curr_row - new_base_row;
	} else {
		erase_begin = FIRST_ROW * MAX_COLUMNS;
		erase_end = (new_base_row + 1) * MAX_COLUMNS - copy_chars;

		memmove (text + erase_end,
			 text + (ch->curr_row + 1) * MAX_COLUMNS - copy_chars,
			 copy_chars * sizeof (*text));

		ch->dirty[n] = (ch->dirty[n] << (new_base_row - ch->curr_row))
			& ALL_DIRTY;
	}

	while (erase_begin < erase_end) {
		text[erase_begin++] = ts;
	}

	send_event (cd, ch, VBI3_EVENT_CC_PAGE, PAGE_UPDATE);
}

static void
preamble_address_code		(vbi3_caption_decoder *	cd,
				 caption_channel *	ch,
				 unsigned int		c1,
				 unsigned int		c2)
{
	static const int row_mapping [16] = {
		/* 0 */ 10,			/* 0x1040 */
		/* 1 */ -1,			/* unassigned */
		/* 2 */ 0, 1, 2, 3,		/* 0x1140 ... 0x1260 */
		/* 6 */ 11, 12, 13, 14,		/* 0x1340 ... 0x1460 */
		/* 10 */ 4, 5, 6, 7, 8, 9	/* 0x1540 ... 0x1760 */
	};
	int row;

	/* Preamble Address Codes	001 crrr  1ri xxxu */

	row = row_mapping[(c1 & 7) * 2 + ((c2 >> 5) & 1)];

	/* Sec. 15.119 (j): No function, reject. */
	if (row < 0)
		return;

	if (c2 & 1)
		ch->curr_attr.attr |= VBI3_UNDERLINE;
	else
		ch->curr_attr.attr &= ~VBI3_UNDERLINE;

	ch->curr_attr.background = VBI3_BLACK;
	ch->curr_attr.opacity = VBI3_OPAQUE;

	if (VBI3_CAPTION_MODE_ROLL_UP == ch->mode) {
		/* Sec. 15.119 (f)(1)(ii). */
		if (row != (int) ch->curr_row) {
			move_window (cd, ch, /* new_base_row */ row);
		}
	}

	/* Sec. 15.119 (d)(1)(i) */
	set_cursor (ch, FIRST_COLUMN, row);

	if (c2 & 0x10) {
		/* Indent. */
		/* Sec. 15.119 (d)(1)(i) */

		ch->curr_column = FIRST_COLUMN + (c2 & 0x0E) * 2;
	} else {
		unsigned int color;

		color = (c2 >> 1) & 7;

		if (7 == color) {
			ch->curr_attr.attr |= VBI3_ITALIC;
			ch->curr_attr.foreground = VBI3_WHITE;
		} else {
	       		ch->curr_attr.attr &= ~VBI3_ITALIC;
			ch->curr_attr.foreground = color_mapping[color];
		}
	}
}

static void
mid_row_code			(vbi3_caption_decoder *	cd,
				 caption_channel *	ch,
				 unsigned int		c2)
{
	unsigned int color;

	/* Mid-Row Codes		001 c001  010 xxxu */

	if (c2 & 1) {
		/* Underline on. XXX correct to underline the space
		   between underlined words? */
		ch->curr_attr.attr &= ~VBI3_FLASH;
	} else {
		/* Sec. 15.119 (h)(1)(iii). */
		ch->curr_attr.attr &= ~(VBI3_FLASH | VBI3_UNDERLINE);
	}

	/* Sec. 15.119 (h)(1)(i): Is a spacing attribute. */
	put_char (cd, ch, 0x0020);

	color = (c2 >> 1) & 7;

	if (7 == color) {
		ch->curr_attr.attr |= VBI3_ITALIC;

		/* Sec. 15.119 (h)(1)(ii): No color change. */
	} else {
		/* Sec. 15.119 (h)(1)(ii). */
		ch->curr_attr.attr &= ~VBI3_ITALIC;

		ch->curr_attr.foreground = color_mapping[color];
	}

	if (c2 & 1)
		ch->curr_attr.attr |= VBI3_UNDERLINE;
}

static void
backspace			(vbi3_caption_decoder *	cd,
				 caption_channel *	ch)
{
	vbi3_char ts;
	unsigned int n;
	unsigned int row;
	unsigned int column;

	/* Backspace			001 c10f  010 0001 */
	/* Sec. 15.119 (f)(1)(vi). */

	/* XXX how should we delete spacing attributes? A simple
	   backspace or undo the attribute change (i.e. store
	   spacing attributes in the buffer[] similar to raw Teletext
	   pages)? */

	column = ch->curr_column;

	if (column <= FIRST_COLUMN)
		return;

	row = ch->curr_row;

	ch->curr_column = --column;

	n = ch->displayed_buffer ^ (VBI3_CAPTION_MODE_POP_ON == ch->mode);

	/* No event if the buffer will be erased to TRANSPARENT_SPACEs
	   (dirty < 0) or the row still contains only TRANSPARENT_SPACEs. */
	if (ch->dirty[n] > 0 && (ch->dirty[n] & (1 << row))) {
		ts = TRANSPARENT_SPACE (cd, ch);

		/* Transparent space has no attributes but background
		   color (when opaque, or when rendered opaque). */
		ts.foreground = ch->curr_attr.foreground;
		ts.background = ch->curr_attr.background;

		ch->buffer[n][row][column] = ts;

		if (VBI3_CAPTION_MODE_POP_ON != ch->mode) {
			send_event (cd, ch, VBI3_EVENT_CC_PAGE,
				    VBI3_CHAR_UPDATE);
		}
	}
}

static void
delete_to_end_of_row		(vbi3_caption_decoder *	cd,
				 caption_channel *	ch)
{
	unsigned int n;
	unsigned int row;

	/* Delete To End Of Row		001 c10f  010 0100 */
	/* Sec. 15.119 (f)(1)(vii). */

	n = ch->displayed_buffer ^ (VBI3_CAPTION_MODE_POP_ON == ch->mode);

	row = ch->curr_row;

	/* No event if the buffer will be erased to TRANSPARENT_SPACEs
	   (dirty < 0) or the row still contains only TRANSPARENT_SPACEs. */
	if (ch->dirty[n] > 0 && (ch->dirty[n] & (1 << row))) {
		vbi3_char ts;
		unsigned int column;

		ts = TRANSPARENT_SPACE (cd, ch);

		for (column = ch->curr_column;
		     column <= LAST_COLUMN; ++column) {
			/* Transparent space with default colors
			   for this row. */
			ch->buffer[n][row][column] = ts;
		}

		if (VBI3_CAPTION_MODE_POP_ON != ch->mode) {
			/* No ROW_UPDATE because row is not complete yet. */
			send_event (cd, ch, VBI3_EVENT_CC_PAGE, WORD_UPDATE);
		}
	}
}

static void
resize_window			(vbi3_caption_decoder *	cd,
				 caption_channel *	ch,
				 unsigned int		n_rows)
{
	vbi3_char *cp;
	vbi3_char *end;
	unsigned int n;

	n = ch->displayed_buffer;

	/* No event if the buffer contains all TRANSPARENT_SPACEs. */
	if (ch->dirty[n] <= 0)
		return;

	/* Nothing to do unless the window shrinks. */
	if (n_rows >= ch->window_rows)
		return;

	n_rows = MIN (ch->curr_row - FIRST_ROW + 1, n_rows);

	cp = ch->buffer[n][FIRST_ROW];
	end = cp + (ch->curr_row + 1 - n_rows) * MAX_COLUMNS;

	if (cp < end) {
		vbi3_char ts;

		ts = TRANSPARENT_SPACE (cd, ch);

		while (cp < end)
			*cp++ = ts;

		send_event (cd, ch, VBI3_EVENT_CC_PAGE, PAGE_UPDATE);
	}
}

static void
roll_up_captions		(vbi3_caption_decoder *	cd,
				 caption_channel *	ch,
				 unsigned int		c2)
{
	unsigned int n_rows;

	/* Roll-Up Captions		001 c10f  010 01xx */
	/* Sec. 15.119 (f)(1). */

	n_rows = (c2 & 7) - 3; /* 2, 3, 4 */

	if (VBI3_CAPTION_MODE_ROLL_UP == ch->mode) {
		/* Sec. 15.119 (f)(1)(iv). */
		resize_window (cd, ch, n_rows);

		ch->window_rows = n_rows;

		/* XXX NBC (sample s4) sends resume-text and roll-up commands
		   to switch between channels T2 and C1 *without* a PAC after
		   roll-up to place the cursor back at the previous column.
		   That makes sense to me but it contradicts Sec. 15.119
		   (f)(1)(ii): "The Roll-up command, in normal practice,
		   will be followed (not necessarily immediately) by a
		   Preamble Address Code indicating the base row and
		   the horizontal indent position. If no Preamble Address
		   Code is received, the base row will default to Row 15 or,
		   if a roll-up caption is currently displayed, to the same
		   base row last received, and the cursor will be placed at
		   Column 1." */
		/* set_cursor (ch, FIRST_COLUMN, ch->curr_row); */
	} else {
		int was_dirty;

		ch->mode = VBI3_CAPTION_MODE_ROLL_UP;
		ch->window_rows = n_rows;

		set_cursor (ch, FIRST_COLUMN, LAST_ROW);

		was_dirty = ch->dirty[ch->displayed_buffer];

		/* Sec. 15.119 (f)(1)(x). */

		/* Lazy erase of displayed and non-displayed buffer. */
		ch->dirty[0] = -1;
		ch->dirty[1] = -1;

		/* Copy displayed buffer to row-change-snapshot buffer. */
		ch->dirty[2] = -1;

		if (was_dirty > 0)
			send_event (cd, ch, VBI3_EVENT_CC_PAGE, PAGE_UPDATE);
	}
}

static void
erase_displayed_memory		(vbi3_caption_decoder *	cd,
				 caption_channel *	ch)
{
	unsigned int n;
	int was_dirty;

	/* Erase Displayed Memory	001 c10f  010 1100 */
	/* Sec. 15.119 (f). */

	n = ch->displayed_buffer;

	was_dirty = ch->dirty[n];

	/* Lazy erase. */
	ch->dirty[n] = -1;

	/* Copy displayed buffer to row-change-snapshot buffer. */
	ch->dirty[2] = -1;

	if (was_dirty > 0) {
		/* We don't send empty rows.
		   send_raw_event (cd, ch, FIRST_ROW, LAST_ROW); */

		send_event (cd, ch, VBI3_EVENT_CC_PAGE, PAGE_UPDATE);
	}
}

static void
carriage_return			(vbi3_caption_decoder *	cd,
				 caption_channel *	ch)
{
	vbi3_char ts;
	unsigned int n_rows;
	unsigned int first_row;
	unsigned int row;
	unsigned int n;
	vbi3_cc_page_flags flags;

	/* Carriage Return		001 c10f  010 1101 */
	/* Sec. 15.119 (f)(1)(iii). */

	n = ch->displayed_buffer;
	row = ch->curr_row;

	if (unlikely (VBI3_CAPTION_MODE_TEXT == ch->mode)) {
		if (LAST_ROW != row) {
			/* No event if the buffer will be erased to
			   TRANSPARENT_SPACEs (dirty < 0) or the row still
			   contains only TRANSPARENT_SPACEs. */
			if (ch->dirty[n] > 0 && (ch->dirty[n] & (1 << row))) {
				row_change_snapshot (ch);
				send_raw_event (cd, ch, row, row);
			}

			set_cursor (ch, FIRST_COLUMN, row + 1);

			return;
		}

		/* No event if the buffer contains all TRANSPARENT_SPACEs. */
		if (ch->dirty[n] <= 0) {
			ch->curr_column = FIRST_COLUMN;
			return;
		}

		n_rows = MAX_ROWS;
	} else {
		/* VBI3_CAPTION_MODE_ROLL_UP == ch->mode. */

		/* No event if the buffer contains all TRANSPARENT_SPACEs. */
		if (ch->dirty[n] <= 0) {
			ch->curr_column = FIRST_COLUMN;
			return;
		}

		n_rows = row - FIRST_ROW + 1;
		n_rows = MIN (n_rows, ch->window_rows);
	}

	row_change_snapshot (ch);

	/* No event if the row contains only TRANSPARENT_SPACEs. */
	if (likely (ch->dirty[n] & (1 << row))) {
		unsigned int column;

		send_raw_event (cd, ch, row, row);

		first_row = row + 1 - n_rows;

		memmove (ch->buffer[n][first_row],
			 ch->buffer[n][first_row + 1],
			 (n_rows - 1) * sizeof (ch->buffer[0][0]));

		ch->dirty[n] >>= 1;

		ts = TRANSPARENT_SPACE (cd, ch);

		for (column = FIRST_COLUMN; column <= LAST_COLUMN; ++column)
			ch->buffer[n][row][column] = ts;
	} else {
		first_row = row + 1 - n_rows;

		memmove (ch->buffer[n][first_row],
			 ch->buffer[n][first_row + 1],
			 (n_rows - 1) * sizeof (ch->buffer[0][0]));

		ch->dirty[n] >>= 1;
	}

	ch->curr_column = FIRST_COLUMN;

	flags = PAGE_UPDATE;
	if (VBI3_CAPTION_MODE_ROLL_UP == ch->mode)
		flags = PAGE_UPDATE | VBI3_START_ROLLING;

	send_event (cd, ch, VBI3_EVENT_CC_PAGE, flags);
}

static void
optional_attributes		(vbi3_caption_decoder *	cd,
				 caption_channel *	ch,
				 unsigned int		c2)
{
	vbi3_char attr;
	unsigned int column;

	attr = ch->curr_attr;

	switch (c2) {
	case 0x21 ... 0x23:
		/* Misc Control Codes, Tab Offset  001 c111  010 00xx */

		column = ch->curr_column + (c2 & 3);
		ch->curr_column = MIN (column, (unsigned int) LAST_COLUMN);

		return;

	case 0x2D:
		/* Optional Attributes		001 c111  010 1101 */
		/* From Video Demystified, not verified. */

		attr.opacity = VBI3_TRANSPARENT_FULL;

		break;

	case 0x2E:
	case 0x2F:
		/* Optional Attributes		001 c111  010 111u */
		/* From Video Demystified, not verified. */


		attr.foreground = VBI3_BLACK;

		if (c2 & 1)
			attr.attr |= VBI3_UNDERLINE;
		else
			attr.attr &= ~VBI3_UNDERLINE;
		
		break;

	default: /* ? */
		/* Sec. 15.119 (i)(1): Ignore. */
		return;
	}

	/* "Background and foreground attribute codes have
	   an automatic backspace for backward compatibility with
	   current decoders. Thus, an attribute must be preceded
	   by a standard space character. Standard decoders display
	   the space and ignore the attribute. Extended decoders
	   display the space, and on receiving the attribute,
	   backspace, then display a space that changes the color
	   and opacity." XXX what if there is no "standard space
	   character" or we're in first column? */

	column = ch->curr_column;

	if (column > FIRST_COLUMN)
		ch->curr_column = column - 1;

	/* This is a spacing attribute. */
	put_char (cd, ch, 0x0020);

	/* XXX I *think* attributes should set-after, not set-at. */
	ch->curr_attr = attr;
}

/* NOTE ch is invalid if CHANNEL_UNKNOWN == cd->cc.curr_ch_num. */
static void
misc_control_code		(vbi3_caption_decoder *	cd,
				 caption_channel *	ch,
				 unsigned int		c2,
				 unsigned int		ch_num0)
{
	/* Misc Control Codes		001 c10f  010 xxxx */

	/* XXX according to Video Demystified f = field. Purpose?
	   Sec. 15.119 does not mention this bit or caption on
	   field 2 at all. */

	switch (c2 & 15) {
	case 0:	
		/* Resume Caption Loading	001 c10f  010 0000 */

		ch = switch_channel (cd, ch, VBI3_CAPTION_CC1 + (ch_num0 & 3));

		ch->mode = VBI3_CAPTION_MODE_POP_ON;

		/* Sec. 15.119 (f)(1)(x): Memory not erased.
		   Sec. 15.119 (f)(2)(iv): Cursor position unchanged. */

		break;

	case 1:
		/* Backspace			001 c10f  010 0001 */

		if (CHANNEL_UNKNOWN == cd->cc.curr_ch_num
		    || VBI3_CAPTION_MODE_UNKNOWN == ch->mode)
			break;

		backspace (cd, ch);

		break;

	case 2: /* reserved (formerly Alarm Off) */
	case 3: /* reserved (formerly Alarm On) */
		/* Sec. 15.119 (i)(1): Ignore. */
		break;

	case 4:
		/* Delete To End Of Row		001 c10f  010 0100 */

		if (CHANNEL_UNKNOWN == cd->cc.curr_ch_num
		    || VBI3_CAPTION_MODE_UNKNOWN == ch->mode)
			break;

		delete_to_end_of_row (cd, ch);

		break;

	case 5:
	case 6:
	case 7:
		/* Roll-Up Captions		001 c10f  010 01xx */

		ch = switch_channel (cd, ch, VBI3_CAPTION_CC1 + (ch_num0 & 3));

		roll_up_captions (cd, ch, c2);

		break;

	case 8:
		/* Flash On			001 c10f  010 1000 */
		/* Sec. 15.119 (h)(1)(i). */

		if (CHANNEL_UNKNOWN == cd->cc.curr_ch_num
		    || VBI3_CAPTION_MODE_UNKNOWN == ch->mode)
			break;

		/* This is a spacing attribute. */
		put_char (cd, ch, 0x0020);

		ch->curr_attr.attr |= VBI3_FLASH;

		break;

	case 9:
		/* Resume Direct Captioning	001 c10f  010 1001 */

		ch = switch_channel (cd, ch, VBI3_CAPTION_CC1 + (ch_num0 & 3));

		/* Sec. 15.119 (f)(1)(x), (2)(vi): Memory not erased. */

		if (VBI3_CAPTION_MODE_UNKNOWN == ch->mode
		    || VBI3_CAPTION_MODE_POP_ON == ch->mode)
			row_change_snapshot (ch);

		ch->mode = VBI3_CAPTION_MODE_PAINT_ON;

		break;

	case 10:
		/* Text Restart			001 c10f  010 1010 */

		ch = switch_channel (cd, ch, VBI3_CAPTION_T1 + (ch_num0 & 3));

		/* Mode is invariably VBI3_CAPTION_MODE_TEXT. */

		set_cursor (ch, FIRST_COLUMN, FIRST_ROW);

		break;

	case 11:
		/* Resume Text Display		001 c10f  010 1011 */

		ch = switch_channel (cd, ch, VBI3_CAPTION_T1 + (ch_num0 & 3));

		/* Mode is invariably VBI3_CAPTION_MODE_TEXT. */

		break;

	case 12:
		/* Erase Displayed Memory	001 c10f  010 1100 */
		/* Sec. 15.119 (f). */

		if (CHANNEL_UNKNOWN == cd->cc.curr_ch_num
		    || VBI3_CAPTION_MODE_UNKNOWN == ch->mode)
			break;

		erase_displayed_memory (cd, ch);

		break;

	case 13:
		/* Carriage Return		001 c10f  010 1101 */

		if (CHANNEL_UNKNOWN == cd->cc.curr_ch_num)
			break;

		switch (ch->mode) {
		case VBI3_CAPTION_MODE_UNKNOWN:
			break;

		case VBI3_CAPTION_MODE_POP_ON:
		case VBI3_CAPTION_MODE_PAINT_ON:
			/* Sec. 15.119 (f)(2)(i), (3)(i): No effect. */
			break;

		case VBI3_CAPTION_MODE_ROLL_UP:
		case VBI3_CAPTION_MODE_TEXT:
			carriage_return (cd, ch);
			break;
		}

		break;

	case 14:
		/* Erase Non-Displayed Memory	001 c10f  010 1110 */
		/* Sec. 15.119 (f)(2)(v). */

		if (CHANNEL_UNKNOWN == cd->cc.curr_ch_num)
			break;

		/* Lazy erase. */
		ch->dirty[ch->displayed_buffer ^ 1] = -1;

		break;

	case 15:
		/* End Of Caption		001 c10f  010 1111 */
		/* Sec. 15.119 (f)(2). */

		ch = switch_channel (cd, ch, VBI3_CAPTION_CC1 + (ch_num0 & 3));

		/* Sec. 15.119 (f)(2). */
		ch->mode = VBI3_CAPTION_MODE_POP_ON;

		/* Swap displayed and non-displayed caption. */
		ch->displayed_buffer ^= 1;

		/* Sec. 15.119 (f)(3)(iv):
		   Does not erase non-displayed memory. */

		/* No event if both buffers are empty. */
		if (ch->dirty[0] > 0 || ch->dirty[1] > 0) {
			row_change_snapshot (ch);
			send_raw_event (cd, ch, FIRST_ROW, LAST_ROW);
			send_event (cd, ch, VBI3_EVENT_CC_PAGE, PAGE_UPDATE);
		}

		break;
	}
}

static void
caption_control_code		(vbi3_caption_decoder *	cd,
				 unsigned int		c1,
				 unsigned int		c2,
				 field_num		f)
{
	unsigned int ch_num0;
	caption_channel *ch;

	if (0)
		fprintf (stderr, "caption_control_code %02x %02x %d\n",
			 c1, c2, f);

	/* Caption / text, field 1 / 2, primary / secondary channel. */
	ch_num0 = (((cd->cc.curr_ch_num - VBI3_CAPTION_CC1) & 4)
		   + f * 2
		   + ((c1 >> 3) & 1));

	ch = &cd->cc.channel[ch_num0];

	if (c2 >= 0x40) {
		/* Preamble Address Codes	001 crrr  1ri xxxu */

		if (CHANNEL_UNKNOWN == cd->cc.curr_ch_num
		    || VBI3_CAPTION_MODE_UNKNOWN == ch->mode)
			return;

		preamble_address_code (cd, ch, c1, c2);

		return;
	}

	switch (c1 & 7) {
	case 0:
	{
		unsigned int color;

		if (c2 & 0x10) {
			/* Sec. 15.119 (i)(1): Ignore. */
		} else {
			/* Optional Attributes		001 c000  010 xxxt */
			/* From Video Demystified; not verified. */

			if (CHANNEL_UNKNOWN == cd->cc.curr_ch_num
			    || VBI3_CAPTION_MODE_UNKNOWN == ch->mode)
				break;

			color = (c2 >> 1) & 7;

			ch->curr_attr.opacity = (c2 & 1) ?
				VBI3_TRANSLUCENT : VBI3_OPAQUE;
			ch->curr_attr.background = color_mapping[color];
		}

		break;
	}

	case 1:
		if (CHANNEL_UNKNOWN == cd->cc.curr_ch_num
		    || VBI3_CAPTION_MODE_UNKNOWN == ch->mode)
			break;

		if (c2 & 0x10) {
			/* Special Characters		001 c001  011 xxxx */

			c2 &= 15;

			if (9 == c2) {
				put_transparent_space (cd, ch);
			} else {
				put_char (cd, ch, vbi3_caption_unicode (c2));
			}
		} else {
			/* Mid-Row Codes		001 c001  010 xxxu */

			mid_row_code (cd, ch, c2);
		}

		break;

	case 2: /* ? */
	case 3: /* ? */
		/* Sec. 15.119 (i)(1): Ignore. */
		break;

	case 4:
	case 5:
		if (c2 & 0x10) {
			/* Sec. 15.119 (i)(1): Ignore. */
		} else {
			misc_control_code (cd, ch, c2, ch_num0);
		}

		break;

	case 6: /* reserved */
		/* Sec. 15.119 (i)(1): Ignore. */
		break;

	case 7:
		if (CHANNEL_UNKNOWN == cd->cc.curr_ch_num
		    || VBI3_CAPTION_MODE_UNKNOWN == ch->mode)
			break;

		optional_attributes (cd, ch, c2);

		break;
	}
}

static vbi3_bool
caption_text			(vbi3_caption_decoder *	cd,
				 caption_channel *	ch,
				 int			c,
				 double			timestamp)
{
	double last_timestamp;

	if (0)
		fprintf (stderr, "caption_text %02x '%c' %f\n",
			 c, c, timestamp);

	if (0 == c) {
		if (VBI3_CAPTION_MODE_UNKNOWN == ch->mode)
			return TRUE;

		/* After x NUL characters (presumably a caption pause),
		   force a display update if we do not send events
		   on every display change. */

		return TRUE;
	}

	if (c < 0x20) {
		/* Parity error or invalid data. */

		if (c < 0 && VBI3_CAPTION_MODE_UNKNOWN != ch->mode) {
			/* Sec. 15.119 (j)(1). */
			put_char (cd, ch, vbi3_caption_unicode (0x7F));
		}

		return FALSE;
	}

	last_timestamp = ch->last_timestamp + 10.0 /* seconds */;

	/* Last activity on this channel. */
	ch->last_timestamp = timestamp;

	if ((cd->handlers.event_mask & VBI3_EVENT_PAGE_TYPE)
	    && timestamp > last_timestamp) {
		vbi3_event e;

		e.type = VBI3_EVENT_PAGE_TYPE;
		e.network = &cd->network->network;
		e.timestamp = timestamp;

		_vbi3_event_handler_list_send (&cd->handlers, &e);
	}

	if (VBI3_CAPTION_MODE_UNKNOWN != ch->mode)
		put_char (cd, ch, vbi3_caption_unicode (c));

	return TRUE;
}

/* ITV decoder ***************************************************************/

static vbi3_bool
itv_text			(vbi3_caption_decoder *	cd,
				 int			c)
{
	if (c >= 0x20) {
		/* Sample s4-NBC omitted CR. */
		if ('<' == c)
			itv_text (cd, 0);

		if (cd->itv.size > sizeof (cd->itv.buffer) - 2)
			cd->itv.size = 0;

		cd->itv.buffer[cd->itv.size++] = c;

		return TRUE;
	} else if (0 != c) {
		/* Parity error or invalid character. */
		cd->itv.size = 0;
		return FALSE;
	}

	cd->itv.buffer[cd->itv.size] = 0;
	cd->itv.size = 0;

#ifndef ZAPPING8
#warning TODO
	/* vbi3_atvef_trigger(vbi, cc->itv_buf);
	   event */
#endif

	return TRUE;
}

static void
itv_control_code		(vbi3_caption_decoder *	cd,
				 unsigned int		c1,
				 unsigned int		c2)
{
	if (c2 >= 0x40) {
		/* Preamble Address Codes	001 crrr  1ri xxxu */

		return;
	}

	switch (c1 & 7) {
	case 4:
	case 5:
		if (c2 & 0x10) {
			break;
		}

		/* Misc Control Codes		001 c10f  010 xxxx */
		/* f ("field"): purpose? */

		switch (c2 & 15) {
		case 0:
			/* Resume Caption Loading	001 c10f  010 0000 */
		case 5:
		case 6:
		case 7:
			/* Roll-Up Captions		001 c10f  010 01xx */
		case 9:
			/* Resume Direct Captioning	001 c10f  010 1001 */
		case 15:
			/* End Of Caption		001 c10f  010 1111 */

			cd->in_itv = FALSE;
			break;

		case 10:
			/* Text Restart		001 c10f  010 1010 */

			cd->itv.size = 0;

			/* Fall through. */

		case 11:
			/* Resume Text Display		001 c10f  010 1011 */

			/* First field, text mode, secondary channel ->
			   VBI3_CAPTION_T2. */
			cd->in_itv = !!(c1 & 0x08);
			break;

		case 13:
			/* Carriage Return		001 c10f  010 1101 */

			if (cd->in_itv)
				itv_text (cd, 0);
			break;

		default:
			break;
		}

		break;

	default:
		break;
	}
}

/* Demultiplexer *************************************************************/

/**
 * @internal
 * @param cd
 * @param buffer Two bytes.
 * @param line ITU-R line number this data originated from.
 * @param timestamp
 *
 * Decode two bytes of Closed Caption data (Caption, XDS, ITV),
 * updating the decoder state accordingly. May send events.
 */
vbi3_bool
vbi3_caption_decoder_feed	(vbi3_caption_decoder *	cd,
				 const uint8_t		buffer[2],
				 unsigned int		line,
				 double			timestamp)
{
	int c1;
	int c2;
	field_num f;
	vbi3_bool all_successful;

	assert (NULL != cd);
	assert (NULL != buffer);

	if (0)
		fprintf (stderr, "caption feed %02x %02x '%c%c' %3d %f\n",
			 buffer[0] & 0x7F, buffer[1] & 0x7F,
			 buffer[0] & 0x7F, buffer[1] & 0x7F, line, timestamp);

	f = FIELD_1;

	switch (line) {
	case 21: /* NTSC */
	case 22: /* PAL/SECAM */
		break;

	case 284: /* NTSC */
		f = FIELD_2;
		break;

	default:
		return FALSE;
	}

	cd->timestamp = timestamp;

// FIXME deferred reset here

	c1 = vbi3_unpar8 (buffer[0]);
	c2 = vbi3_unpar8 (buffer[1]);

	all_successful = TRUE;

	if (FIELD_1 == f) {
		/* First field. Control codes may repeat
		   to insure correct reception. */

		/* According to Sec. 15.119 (2)(i)(4). */

		if (c1 == cd->expect_ctrl[FIELD_1][0]
		    && c2 == cd->expect_ctrl[FIELD_1][1]) {
			/* Already acted upon. */
			goto finish;
		} else if (c1 < 0 && cd->expect_ctrl[FIELD_1][0]
			   && c2 == cd->expect_ctrl[FIELD_1][1]) {
			/* Parity error, probably in repeat control code. */
			goto parity_error;
		}
	} else {
		/* Second field. */

		if (cd->handlers.event_mask & XDS_EVENTS) {
/* TODO			all_successful &=
   				vbi3_xds_demux_feed (&cd->xds.demux, buffer);
*/
		}

		/* XDS bytes are in range 0x01 ... 0x0F (control codes)
		   and 0x40 ... 0x7F (data), must be filtered. */

		switch (c1) {
		case 0x01 ... 0x0E:
			/* XDS packet start or continuation. */
			cd->in_xds = TRUE;
			goto finish;

		case 0x0F:
			/* XDS packet terminator. */
			cd->in_xds = FALSE;
			goto finish;

		case 0x10 ... 0x1F:
			/* Caption control code. */
			cd->in_xds = FALSE;
			break;

		default:
			if (c1 < 0) {
				goto parity_error;
			}

			break;
		}
	}

	switch (c1) {
	case 0x10 ... 0x1F:
		/* Sec. 15.119 (i)(1), (i)(2). */
		if (c2 < 0x20) {
			/* Parity error or invalid control code.
			   Let's hope it repeats. */
			goto parity_error;
		}

		if (cd->handlers.event_mask & VBI3_EVENT_TRIGGER) {
			if (FIELD_1 == f)
				itv_control_code (cd, c1, c2);
		}

		if (cd->handlers.event_mask & CAPTION_EVENTS) {
			caption_control_code (cd, c1, c2, f);

			if (cd->cc.event_pending) {
				send_event (cd, cd->cc.event_pending,
					    VBI3_EVENT_CC_PAGE,
					    VBI3_CHAR_UPDATE);
			}
		}

		cd->expect_ctrl[f][0] = c1;
		cd->expect_ctrl[f][1] = c2;

		break;

	default:
		if (FIELD_1 != f && cd->in_xds)
			break;

		cd->expect_ctrl[f][0] = 0;

		/* Sec. 15.119 (i)(1). */
		if (c1 > 0x00 && c1 < 0x10) {
			c1 = 0;
		}

		if (cd->in_itv) {
			all_successful &= itv_text (cd, c1);
			all_successful &= itv_text (cd, c2);
		}

		if (cd->handlers.event_mask & CAPTION_EVENTS) {
			vbi3_pgno ch_num;
			caption_channel *ch;

			ch_num = cd->cc.curr_ch_num;
			if (CHANNEL_UNKNOWN == ch_num)
				break;

			ch_num = ((ch_num - VBI3_CAPTION_CC1) & 5) + f * 2;
			ch = &cd->cc.channel[ch_num];

			all_successful &= caption_text (cd, ch, c1, timestamp);
			all_successful &= caption_text (cd, ch, c2, timestamp);

			if (cd->cc.event_pending) {
				send_event (cd, cd->cc.event_pending,
					    VBI3_EVENT_CC_PAGE,
					    VBI3_CHAR_UPDATE); 
			}
		}

		break;
	}

 finish:
	cd->error_history = cd->error_history * 2 + all_successful;

	return all_successful;

 parity_error:
	cd->expect_ctrl[f][0] = 0;

	cd->error_history *= 2;

	return FALSE;
}

void
_vbi3_caption_decoder_resync	(vbi3_caption_decoder *	cd)
{
	unsigned int ch_num;

	assert (NULL != cd);

	if (0)
		fprintf (stderr, "caption resync\n");

	for (ch_num = 0; ch_num < MAX_CHANNELS; ++ch_num) {
		caption_channel *ch;

		ch = &cd->cc.channel[ch_num];

		if (ch_num <= 3)
			ch->mode = VBI3_CAPTION_MODE_UNKNOWN;
		else
			ch->mode = VBI3_CAPTION_MODE_TEXT; /* invariable */

		ch->displayed_buffer = 0;

		/* Lazy erase of all buffers. */
		memset (ch->dirty, -1, sizeof (ch->dirty));

		set_cursor (ch, FIRST_COLUMN, LAST_ROW);

		ch->window_rows = 3;

		reset_curr_attr (cd, ch);

		ch->last_timestamp = 0.0;
	}

	cd->cc.curr_ch_num = CHANNEL_UNKNOWN;

	cd->in_xds = FALSE;

	CLEAR (cd->expect_ctrl);

	cd->error_history = 0; /* all failed */
}

/** @internal */
void
cache_network_destroy_caption	(cache_network *	cn)
{
	cn = cn;

	/* TODO */
}

/**
 * @internal
 * @param cn cache_network structure to be initialized.
 *
 * Initializes the Caption fields of a cache_network structure.
 */
void
cache_network_init_caption	(cache_network *	cn)
{
	cn = cn;

	/* TODO */
}

/**
 * @internal
 * @param cd Caption decoder allocated with vbi3_caption_decoder_new().
 * @param cn New network, can be @c NULL if 0.0 != time.
 * @param time Deferred reset when time is greater than
 *   vbi3_caption_decoder_feed() timestamp. Pass a negative number to
 *   cancel a deferred reset, 0.0 to reset immediately.
 *
 * Internal reset function, called via cd->virtual_reset().
 */
static void
internal_reset			(vbi3_caption_decoder *	cd,
				 cache_network *	cn,
				 double			time)
{
	assert (NULL != cd);

	if (0)
		fprintf (stderr, "caption reset %f: %f -> %f\n",
			 cd->timestamp, cd->reset_time, time);

	if (time <= 0.0 /* reset now or cancel deferred reset */
	    || time > cd->reset_time)
		cd->reset_time = time;

	if (0.0 != time) {
		/* Don't reset now. */
		return;
	}

	assert (NULL != cn);

	cache_network_unref (cd->network);
	cd->network = cache_network_ref (cn);

	_vbi3_caption_decoder_resync (cd);

	if (internal_reset == cd->virtual_reset) {
		vbi3_event e;

		e.type		= VBI3_EVENT_RESET;
		e.network	= &cd->network->network;
		e.timestamp	= cd->timestamp;

		_vbi3_event_handler_list_send (&cd->handlers, &e);
	}
}

/**
 * @param cd Caption decoder allocated with vbi3_caption_decoder_new().
 * @param callback Function to be called on events.
 * @param user_data User pointer passed through to the @a callback function.
 * 
 * Removes an event handler from the Caption decoder, if a handler with
 * this @a callback and @a user_data has been registered. You can
 * safely call this function from a handler removing itself or another
 * handler.
 */
void
vbi3_caption_decoder_remove_event_handler
				(vbi3_caption_decoder *	cd,
				 vbi3_event_cb *	callback,
				 void *			user_data)
{
/* TODO
	vbi3_cache_remove_event_handler	(cd->cache,
					 callback,
					 user_data);
*/
	_vbi3_event_handler_list_remove_by_callback (&cd->handlers,
						     callback,
						     user_data);
}

/**
 * @param cd Caption decoder allocated with vbi3_caption_decoder_new().
 * @param event_mask Set of events (@c VBI3_EVENT_) the handler is waiting
 *   for, can be -1 for all and 0 for none.
 * @param callback Function to be called on events by
 *   vbi3_caption_decoder_feed().
 * @param user_data User pointer passed through to the @a callback function.
 * 
 * Adds a new event handler to the Caption decoder. When the @a callback
 * with @a user_data is already registered the function merely changes the
 * set of events it will receive in the future. When the @a event_mask is
 * empty the function does nothing or removes an already registered event
 * handler. You can safely call this function from an event handler.
 *
 * Any number of handlers can be added, also different handlers for the
 * same event which will be called in registration order.
 *
 * @returns
 * @c FALSE of failure (out of memory).
 */
vbi3_bool
vbi3_caption_decoder_add_event_handler
				(vbi3_caption_decoder *	cd,
				 unsigned int		event_mask,
				 vbi3_event_cb *	callback,
				 void *			user_data)
{
	unsigned int cc_mask;
	unsigned int add_mask;
	unsigned int rem_mask;

/* TODO
	if (!vbi3_cache_add_event_handler (cd->cache,
					   event_mask,
					   callback,
					   user_data))
		return FALSE;
*/
	cc_mask = event_mask & (VBI3_EVENT_CLOSE |
				VBI3_EVENT_RESET |
				VBI3_EVENT_CC_PAGE |
				VBI3_EVENT_CC_RAW |
				VBI3_EVENT_NETWORK |
				VBI3_EVENT_TRIGGER |
				VBI3_EVENT_PROG_INFO |
				VBI3_EVENT_LOCAL_TIME |
				VBI3_EVENT_PROG_ID |
				VBI3_EVENT_PAGE_TYPE);

	add_mask = cc_mask & ~cd->handlers.event_mask;
	rem_mask = cd->handlers.event_mask & ~event_mask;

	if (0 == cc_mask) {
		return TRUE;
	}

	if (NULL != _vbi3_event_handler_list_add (&cd->handlers,
						  cc_mask,
						  callback,
						  user_data)) {
		if (add_mask & (VBI3_EVENT_CC_PAGE |
				VBI3_EVENT_CC_RAW |
				VBI3_EVENT_TRIGGER)) {
			/* XXX is this really necessary? */
			_vbi3_caption_decoder_resync (cd);
		}

		return TRUE;
	} else {
/* TODO
		vbi3_cache_remove_event_handler	(cd->cache,
						 callback,
						 user_data);
*/
	}

	return FALSE;
}

/**
 * DOCUMENT ME
 */
vbi3_bool
vbi3_caption_decoder_get_network
				(vbi3_caption_decoder *	cd,
				 vbi3_network *		nk)
{
	assert (NULL != cd);
	assert (NULL != nk);

	if (!cd->network)
		return FALSE;

	return vbi3_network_copy (nk, &cd->network->network);
}

/**
 * DOCUMENT ME
 */
vbi3_cache *
vbi3_caption_decoder_get_cache	(vbi3_caption_decoder *	cd)
{
	assert (NULL != cd);

	if (!cd->cache)
		return NULL;

	return vbi3_cache_ref (cd->cache);
}

/**
 * @param cd Caption decoder allocated with vbi3_caption_decoder_new().
 * @param nk Identifies the new network, can be @c NULL.
 *
 * Resets the caption decoder, useful for example after a channel change.
 * This function sends a @c VBI3_EVENT_RESET.
 *
 * You can pass a vbi3_network structure to identify the new network in
 * advance, before the decoder receives a network ID, if ever.
 */
void
vbi3_caption_decoder_reset	(vbi3_caption_decoder *	cd,
				 const vbi3_network *	nk,
				 vbi3_videostd_set	videostd_set)
{
	cache_network *cn;

	assert (NULL != cd);

	cd->videostd_set = videostd_set;

	cn = _vbi3_cache_add_network (cd->cache, nk, videostd_set);

	cd->virtual_reset (cd, cn, 0.0 /* now */);

	cache_network_unref (cn);
}

/**
 * @internal
 * @param cd Caption decoder to be destroyed.
 *
 * Frees all resources associated with @a cd, except the structure itself.
 * This function sends a @c VBI3_EVENT_CLOSE.
 */
void
_vbi3_caption_decoder_destroy	(vbi3_caption_decoder *	cd)
{
	vbi3_event e;

	assert (NULL != cd);

	e.type		= VBI3_EVENT_CLOSE;
	e.network	= &cd->network->network;
	e.timestamp	= cd->timestamp;

	_vbi3_event_handler_list_send (&cd->handlers, &e);

	_vbi3_event_handler_list_destroy (&cd->handlers);

	cache_network_unref (cd->network);

	/* Delete if we hold the last reference. */

	vbi3_cache_unref (cd->cache);

	CLEAR (*cd);
}

/**
 * @internal
 * @param cd Caption decoder to be initialized.
 * @param ca Cache to be used by this Caption decoder, can be @c NULL.
 *   To allocate a cache call vbi3_cache_new(). Caches have a reference
 *   counter, you can vbi3_cache_unref() after calling this function.
 * @param nk Initial network (see vbi3_caption_decoder_reset()),
 *   can be @c NULL.
 *
 * Initializes a Caption decoder structure.
 *
 * @returns
 * @c FALSE on failure (out of memory).
 */
vbi3_bool
_vbi3_caption_decoder_init	(vbi3_caption_decoder *	cd,
				 vbi3_cache *		ca,
				 const vbi3_network *	nk,
				 vbi3_videostd_set	videostd_set)
{
	cache_network *cn;

	assert (NULL != cd);

	CLEAR (*cd);

	if (ca)
		cd->cache = vbi3_cache_ref (ca);
	else
		cd->cache = vbi3_cache_new ();

	if (!cd->cache)
		return FALSE;

	cd->virtual_reset = internal_reset;

	_vbi3_event_handler_list_init (&cd->handlers);

	cd->videostd_set = videostd_set;

	cn = _vbi3_cache_add_network (cd->cache, nk, videostd_set);

	internal_reset (cd, cn, 0.0 /* now */);

	cache_network_unref (cn);

	return TRUE;
}

static void
internal_delete			(vbi3_caption_decoder *	cd)
{
	assert (NULL != cd);

	_vbi3_caption_decoder_destroy (cd);

	vbi3_free (cd);		
}

/**
 * @param cd Caption decoder context allocated with
 *   vbi3_caption_decoder_new(), can be @c NULL.
 *
 * Frees all resources associated with @a cd.
 * This function sends a @c VBI3_EVENT_CLOSE.
 */
void
vbi3_caption_decoder_delete	(vbi3_caption_decoder *	cd)
{
	if (NULL == cd)
		return;

	assert (NULL != cd->virtual_delete);

	cd->virtual_delete (cd);
}

/**
 * @param ca Cache to be used by this Caption decoder. If @a ca is @c NULL
 *  the function allocates a new cache. To allocate a cache yourself call
 *  vbi3_cache_new(). Caches have a reference counter, you can
 *  vbi3_cache_unref() after calling this function.
 *
 * Allocates a new Close Caption (EIA 608) decoder. Decoded data is
 * available through the following functions:
 * - vbi3_caption_decoder_get_page()
 *
 * To be notified when new data is available call
 * vbi3_caption_decoder_add_event_handler().
 *
 * @returns
 * Pointer to newly allocated Caption decoder which must be
 * freed with vbi3_caption_decoder_delete() when done.
 * @c NULL on failure (out of memory).
 */
vbi3_caption_decoder *
vbi3_caption_decoder_new	(vbi3_cache *		ca,
				 const vbi3_network *	nk,
				 vbi3_videostd_set	videostd_set)
{
	vbi3_caption_decoder *cd;

	if (!(cd = malloc (sizeof (*cd)))) {
		return NULL;
	}

	_vbi3_caption_decoder_init (cd, ca, nk, videostd_set);

	cd->virtual_delete = internal_delete;

	return cd;
}