summaryrefslogtreecommitdiff
path: root/dvbsubtitle.c
blob: c1dfef4d86c4c46a6e33ffd0472f81195e8987dd (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
/*
 * dvbsubtitle.c: DVB subtitles
 *
 * See the main source file 'vdr.c' for copyright information and
 * how to reach the author.
 *
 * Original author: Marco Schluessler <marco@lordzodiac.de>
 * With some input from the "subtitles plugin" by Pekka Virtanen <pekka.virtanen@sci.fi>
 *
 * $Id: dvbsubtitle.c 5.1 2021/03/17 15:24:34 kls Exp $
 */

#include "dvbsubtitle.h"
#define __STDC_FORMAT_MACROS // Required for format specifiers
#include <inttypes.h>
#include "device.h"
#include "libsi/si.h"

#define PAGE_COMPOSITION_SEGMENT    0x10
#define REGION_COMPOSITION_SEGMENT  0x11
#define CLUT_DEFINITION_SEGMENT     0x12
#define OBJECT_DATA_SEGMENT         0x13
#define DISPLAY_DEFINITION_SEGMENT  0x14
#define DISPARITY_SIGNALING_SEGMENT 0x15 // DVB BlueBook A156
#define END_OF_DISPLAY_SET_SEGMENT  0x80
#define STUFFING_SEGMENT            0xFF

#define PGS_PALETTE_SEGMENT         0x14
#define PGS_OBJECT_SEGMENT          0x15
#define PGS_PRESENTATION_SEGMENT    0x16
#define PGS_WINDOW_SEGMENT          0x17
#define PGS_DISPLAY_SEGMENT         0x80

// Set these to 'true' for debug output, which is written into the file dbg-log.htm
// in the current working directory. The HTML file shows the actual bitmaps (dbg-nnn.jpg)
// used to display the subtitles.
static bool DebugNormal    = false; // shows pages, regions and objects
static bool DebugVerbose   = false; // shows everything
static bool DebugDisplay   = DebugVerbose || DebugNormal;
static bool DebugPages     = DebugVerbose || DebugNormal;
static bool DebugRegions   = DebugVerbose || DebugNormal;
static bool DebugObjects   = DebugVerbose || DebugNormal;
static bool DebugConverter = DebugVerbose;
static bool DebugSegments  = DebugVerbose;
static bool DebugPixel     = DebugVerbose;
static bool DebugCluts     = DebugVerbose;
static bool DebugOutput    = DebugVerbose;

#define dbgdisplay(a...)   if (DebugDisplay)   SD.WriteHtml(a)
#define dbgpages(a...)     if (DebugPages)     SD.WriteHtml(a)
#define dbgregions(a...)   if (DebugRegions)   SD.WriteHtml(a)
#define dbgobjects(a...)   if (DebugObjects)   SD.WriteHtml(a)
#define dbgconverter(a...) if (DebugConverter) SD.WriteHtml(a)
#define dbgsegments(a...)  if (DebugSegments)  SD.WriteHtml(a)
#define dbgpixel(a...)     if (DebugPixel)     SD.WriteHtml(a)
#define dbgcluts(a...)     if (DebugCluts)     SD.WriteHtml(a)
#define dbgoutput(a...)    if (DebugOutput)    SD.WriteHtml(a)

#define DBGMAXBITMAPS  100 // debug output will be stopped after this many bitmaps
#define DBGBITMAPWIDTH 400

#define FIX_SUBTITLE_VERSION_BROADCASTER_STUPIDITY // some don't properly handle version numbers, which renders them useless because subtitles are not displayed

// --- cSubtitleDebug --------------------------------------------------------

class cSubtitleDebug {
private:
  cMutex mutex;
  int imgCnt;
  int64_t firstPts;
  bool newFile;
  double factor;
public:
  cSubtitleDebug(void) { Reset(); }
  void Reset(void);
  bool Active(void) { return imgCnt < DBGMAXBITMAPS; }
  int64_t FirstPts(void) { return firstPts; }
  void SetFirstPts(int64_t FirstPts) { if (firstPts < 0) firstPts = FirstPts; }
  void SetFactor(double Factor) { factor = Factor; }
  cString WriteJpeg(const cBitmap *Bitmap, int MaxX = 0, int MaxY = 0);
  void WriteHtml(const char *Format, ...);
  };

void cSubtitleDebug::Reset(void)
{
  imgCnt = 0;
  firstPts = -1;
  newFile = true;
  factor = 1.0;
}

cString cSubtitleDebug::WriteJpeg(const cBitmap *Bitmap, int MaxX, int MaxY)
{
  if (!Active())
     return NULL;
  cMutexLock MutexLock(&mutex);
  cBitmap *Scaled = Bitmap->Scaled(factor, factor, true);
  int w = MaxX ? int(round(MaxX * factor)) : Scaled->Width();
  int h = MaxY ? int(round(MaxY * factor)) : Scaled->Height();
  uchar mem[w * h * 3];
  for (int x = 0; x < w; x++) {
      for (int y = 0; y < h; y++) {
          tColor c = Scaled->GetColor(x, y);
          int o = (y * w + x) * 3;
          mem[o++] = (c & 0x00FF0000) >> 16;
          mem[o++] = (c & 0x0000FF00) >> 8;
          mem[o]   = (c & 0x000000FF);
          }
      }
  delete Scaled;
  int Size = 0;
  uchar *Jpeg = RgbToJpeg(mem, w, h, Size);
  cString ImgName = cString::sprintf("dbg-%03d.jpg", imgCnt++);
  int f = open(ImgName, O_WRONLY | O_CREAT, DEFFILEMODE);
  if (f >= 0) {
     if (write(f, Jpeg, Size) < 0)
        LOG_ERROR_STR(*ImgName);
     close(f);
     }
  free(Jpeg);
  return ImgName;
}

void cSubtitleDebug::WriteHtml(const char *Format, ...)
{
  if (!Active())
     return;
  cMutexLock MutexLock(&mutex);
  if (FILE *f = fopen("dbg-log.htm", newFile ? "w" : "a")) {
     va_list ap;
     va_start(ap, Format);
     vfprintf(f, Format, ap);
     va_end(ap);
     fclose(f);
     newFile = false;
     }
}

static cSubtitleDebug SD;

// --- cSubtitleClut ---------------------------------------------------------

class cSubtitleClut : public cListObject {
private:
  int clutId;
  int clutVersionNumber;
  cPalette palette2;
  cPalette palette4;
  cPalette palette8;
  tColor yuv2rgb(int Y, int Cb, int Cr);
  void SetColor(int Bpp, int Index, tColor Color);
public:
  cSubtitleClut(int ClutId);
  void Parse(cBitStream &bs);
  void ParsePgs(cBitStream &bs);
  int ClutId(void) { return clutId; }
  int ClutVersionNumber(void) { return clutVersionNumber; }
  const cPalette *GetPalette(int Bpp);
  };

cSubtitleClut::cSubtitleClut(int ClutId)
:palette2(2)
,palette4(4)
,palette8(8)
{
  int a = 0, r = 0, g = 0, b = 0;
  clutId = ClutId;
  clutVersionNumber = -1;
  // ETSI EN 300 743 10.3: 4-entry CLUT default contents
  palette2.SetColor(0, ArgbToColor(  0,   0,   0,   0));
  palette2.SetColor(1, ArgbToColor(255, 255, 255, 255));
  palette2.SetColor(2, ArgbToColor(255,   0,   0,   0));
  palette2.SetColor(3, ArgbToColor(255, 127, 127, 127));
  // ETSI EN 300 743 10.2: 16-entry CLUT default contents
  palette4.SetColor(0, ArgbToColor(0, 0, 0, 0));
  for (int i = 1; i < 16; ++i) {
      if (i < 8) {
         r = (i & 1) ? 255 : 0;
         g = (i & 2) ? 255 : 0;
         b = (i & 4) ? 255 : 0;
         }
      else {
         r = (i & 1) ? 127 : 0;
         g = (i & 2) ? 127 : 0;
         b = (i & 4) ? 127 : 0;
         }
      palette4.SetColor(i, ArgbToColor(255, r, g, b));
      }
  // ETSI EN 300 743 10.1: 256-entry CLUT default contents
  palette8.SetColor(0, ArgbToColor(0, 0, 0, 0));
  for (int i = 1; i < 256; ++i) {
      if (i < 8) {
         r = (i & 1) ? 255 : 0;
         g = (i & 2) ? 255 : 0;
         b = (i & 4) ? 255 : 0;
         a = 63;
         }
      else {
         switch (i & 0x88) {
           case 0x00:
                r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
                g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
                b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
                a = 255;
                break;
           case 0x08:
                r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
                g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
                b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
                a = 127;
                break;
           case 0x80:
                r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
                g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
                b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
                a = 255;
                break;
           case 0x88:
                r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
                g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
                b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
                a = 255;
                break;
            }
         }
      palette8.SetColor(i, ArgbToColor(a, r, g, b));
      }
}

void cSubtitleClut::Parse(cBitStream &bs)
{
  int Version = bs.GetBits(4);
#ifndef FIX_SUBTITLE_VERSION_BROADCASTER_STUPIDITY
  if (clutVersionNumber == Version)
     return; // no update
#endif
  clutVersionNumber = Version;
  bs.SkipBits(4); // reserved
  dbgcluts("<b>clut</b> id %d version %d<br>\n", clutId, clutVersionNumber);
  while (!bs.IsEOF()) {
        uchar clutEntryId = bs.GetBits(8);
        bool entryClut2Flag = bs.GetBit();
        bool entryClut4Flag = bs.GetBit();
        bool entryClut8Flag = bs.GetBit();
        bs.SkipBits(4); // reserved
        uchar yval;
        uchar crval;
        uchar cbval;
        uchar tval;
        if (bs.GetBit()) { // full_range_flag
           yval  = bs.GetBits(8);
           crval = bs.GetBits(8);
           cbval = bs.GetBits(8);
           tval  = bs.GetBits(8);
           }
        else {
           yval  = bs.GetBits(6) << 2;
           crval = bs.GetBits(4) << 4;
           cbval = bs.GetBits(4) << 4;
           tval  = bs.GetBits(2) << 6;
           }
        tColor value = 0;
        if (yval) {
           value = yuv2rgb(yval, cbval, crval);
           value |= ((10 - (clutEntryId ? Setup.SubtitleFgTransparency : Setup.SubtitleBgTransparency)) * (255 - tval) / 10) << 24;
           }
        dbgcluts("%2d %d %d %d %08X<br>\n", clutEntryId, entryClut2Flag ? 2 : 0, entryClut4Flag ? 4 : 0, entryClut8Flag ? 8 : 0, value);
        if (entryClut2Flag)
           SetColor(2, clutEntryId, value);
        if (entryClut4Flag)
           SetColor(4, clutEntryId, value);
        if (entryClut8Flag)
           SetColor(8, clutEntryId, value);
        }
}

void cSubtitleClut::ParsePgs(cBitStream &bs)
{
  int Version = bs.GetBits(8);
  if (clutVersionNumber == Version)
     return; // no update
  clutVersionNumber = Version;
  dbgcluts("<b>clut</b> id %d version %d<br>\n", clutId, clutVersionNumber);
  for (int i = 0; i < 256; ++i)
      SetColor(8, i, ArgbToColor(0, 0, 0, 0));
  while (!bs.IsEOF()) {
        uchar clutEntryId = bs.GetBits(8);
        uchar yval  = bs.GetBits(8);
        uchar crval = bs.GetBits(8);
        uchar cbval = bs.GetBits(8);
        uchar tval  = bs.GetBits(8);
        tColor value = 0;
        if (yval) {
           value = yuv2rgb(yval, cbval, crval);
           value |= ((10 - (clutEntryId ? Setup.SubtitleFgTransparency : Setup.SubtitleBgTransparency)) * tval / 10) << 24;
           }
        dbgcluts("%2d %08X<br>\n", clutEntryId, value);
        SetColor(8, clutEntryId, value);
        }
}

tColor cSubtitleClut::yuv2rgb(int Y, int Cb, int Cr)
{
  int Ey, Epb, Epr;
  int Eg, Eb, Er;

  Ey = (Y - 16);
  Epb = (Cb - 128);
  Epr = (Cr - 128);
  /* ITU-R 709 */
  Er = constrain((298 * Ey             + 460 * Epr) / 256, 0, 255);
  Eg = constrain((298 * Ey -  55 * Epb - 137 * Epr) / 256, 0, 255);
  Eb = constrain((298 * Ey + 543 * Epb            ) / 256, 0, 255);

  return (Er << 16) | (Eg << 8) | Eb;
}

void cSubtitleClut::SetColor(int Bpp, int Index, tColor Color)
{
  switch (Bpp) {
    case 2: palette2.SetColor(Index, Color); break;
    case 4: palette4.SetColor(Index, Color); break;
    case 8: palette8.SetColor(Index, Color); break;
    default: esyslog("ERROR: wrong Bpp in cSubtitleClut::SetColor(%d, %d, %08X)", Bpp, Index, Color);
    }
}

const cPalette *cSubtitleClut::GetPalette(int Bpp)
{
  switch (Bpp) {
    case 2: return &palette2;
    case 4: return &palette4;
    case 8: return &palette8;
    default: esyslog("ERROR: wrong Bpp in cSubtitleClut::GetPalette(%d)", Bpp);
    }
  return &palette8;
}

// --- cSubtitleObject -------------------------------------------------------

class cSubtitleObject : public cListObject {
private:
  int objectId;
  int objectVersionNumber;
  int objectCodingMethod;
  bool nonModifyingColorFlag;
  int topLength;
  int botLength;
  int topIndex;
  uchar *topData;
  uchar *botData;
  char *txtData;
  int lineHeight;
  void DrawLine(cBitmap *Bitmap, int x, int y, tIndex Index, int Length);
  bool Decode2BppCodeString(cBitmap *Bitmap, int px, int py, cBitStream *bs, int&x, int y, const uint8_t *MapTable);
  bool Decode4BppCodeString(cBitmap *Bitmap, int px, int py, cBitStream *bs, int&x, int y, const uint8_t *MapTable);
  bool Decode8BppCodeString(cBitmap *Bitmap, int px, int py, cBitStream *bs, int&x, int y);
  bool DecodePgsCodeString(cBitmap *Bitmap, int px, int py, cBitStream *bs, int&x, int y);
  void DecodeSubBlock(cBitmap *Bitmap, int px, int py, const uchar *Data, int Length, bool Even);
  void DecodeCharacterString(const uchar *Data, int NumberOfCodes);
public:
  cSubtitleObject(int ObjectId);
  ~cSubtitleObject();
  void Parse(cBitStream &bs);
  void ParsePgs(cBitStream &bs);
  int ObjectId(void) { return objectId; }
  int ObjectVersionNumber(void) { return objectVersionNumber; }
  int ObjectCodingMethod(void) { return objectCodingMethod; }
  bool NonModifyingColorFlag(void) { return nonModifyingColorFlag; }
  void Render(cBitmap *Bitmap, int px, int py, tIndex IndexFg, tIndex IndexBg);
  };

cSubtitleObject::cSubtitleObject(int ObjectId)
{
  objectId = ObjectId;
  objectVersionNumber = -1;
  objectCodingMethod = -1;
  nonModifyingColorFlag = false;
  topLength = 0;
  botLength = 0;
  topIndex = 0;
  topData = NULL;
  botData = NULL;
  txtData = NULL;
  lineHeight = 26; // configurable subtitling font size?
}

cSubtitleObject::~cSubtitleObject()
{
  free(topData);
  free(botData);
  free(txtData);
}

void cSubtitleObject::Parse(cBitStream &bs)
{
  int Version = bs.GetBits(4);
#ifndef FIX_SUBTITLE_VERSION_BROADCASTER_STUPIDITY
  if (objectVersionNumber == Version)
     return; // no update
#endif
  objectVersionNumber = Version;
  objectCodingMethod = bs.GetBits(2);
  nonModifyingColorFlag = bs.GetBit();
  bs.SkipBit(); // reserved
  dbgobjects("<b>object</b> id %d version %d method %d modify %d", objectId, objectVersionNumber, objectCodingMethod, nonModifyingColorFlag); // no "<br>\n" here, DecodeCharacterString() may add data
  if (objectCodingMethod == 0) { // coding of pixels
     topLength = bs.GetBits(16);
     botLength = bs.GetBits(16);
     free(topData);
     if ((topData = MALLOC(uchar, topLength)) != NULL)
        memcpy(topData, bs.GetData(), topLength);
     else
        topLength = 0;
     free(botData);
     if ((botData = MALLOC(uchar, botLength)) != NULL)
        memcpy(botData, bs.GetData() + topLength, botLength);
     else
        botLength = 0;
     bs.WordAlign();
     }
  else if (objectCodingMethod == 1) { // coded as a string of characters
     int numberOfCodes = bs.GetBits(8);
     DecodeCharacterString(bs.GetData(), numberOfCodes);
     }
  dbgobjects("<br>\n");
  if (DebugObjects) {
     // We can't get the actual clut here, so we use a default one. This may lead to
     // funny colors, but we just want to get a rough idea of what's in the object, anyway.
     cSubtitleClut Clut(0);
     cBitmap b(1920, 1080, 8);
     b.Replace(*Clut.GetPalette(b.Bpp()));
     b.Clean();
     Render(&b, 0, 0, 0, 1);
     int x1, y1, x2, y2;
     if (b.Dirty(x1, y1, x2, y2)) {
        cString ImgName = SD.WriteJpeg(&b, x2, y2);
        dbgobjects("<img src=\"%s\"><br>\n", *ImgName);
        }
     }
}

void cSubtitleObject::ParsePgs(cBitStream &bs)
{
  int Version = bs.GetBits(8);
  if (objectVersionNumber == Version)
     return; // no update
  objectVersionNumber = Version;
  objectCodingMethod = 0;
  int sequenceDescriptor = bs.GetBits(8);
  if (!(sequenceDescriptor & 0x80) && topData != NULL) {
     memcpy(topData + topIndex, bs.GetData(), (bs.Length() - bs.Index()) / 8);
     topIndex += (bs.Length() - bs.Index()) / 8;
     return;
     }
  topLength = bs.GetBits(24) - 4 + 1; // exclude width / height, add sub block type
  bs.SkipBits(32);
  if ((topData = MALLOC(uchar, topLength)) != NULL) {
     topData[topIndex++] = 0xFF; // PGS end of line
     memcpy(topData + 1, bs.GetData(), (bs.Length() - bs.Index()) / 8);
     topIndex += (bs.Length() - bs.Index()) / 8 + 1;
     }
  dbgobjects("<b>object</b> id %d version %d method %d modify %d", objectId, objectVersionNumber, objectCodingMethod, nonModifyingColorFlag);
}

void cSubtitleObject::DecodeCharacterString(const uchar *Data, int NumberOfCodes)
{
  // "ETSI EN 300 743 V1.3.1 (2006-11)", chapter 7.2.5 "Object data segment" specifies
  // character_code to be a 16-bit index number into the character table identified
  // in the subtitle_descriptor. However, the "subtitling_descriptor" <sic> according to
  // "ETSI EN 300 468 V1.13.1 (2012-04)" doesn't contain a "character table identifier".
  // It only contains a three letter language code, without any specification as to how
  // this is related to a specific character table.
  // Apparently the first "code" in textual subtitles contains the character table
  // identifier, and all codes are 8-bit only. So let's first make Data a string of
  // 8-bit characters:
  if (NumberOfCodes > 0) {
     char txt[NumberOfCodes + 1];
     for (int i = 0; i < NumberOfCodes; i++)
         txt[i] = Data[i * 2 + 1];
     txt[NumberOfCodes] = 0;
     const uchar *from = (uchar *)txt;
     int len = NumberOfCodes;
     const char *CharacterTable = SI::getCharacterTable(from, len);
     dbgobjects(" table %s raw '%s'", CharacterTable, from);
     cCharSetConv conv(CharacterTable, cCharSetConv::SystemCharacterTable());
     const char *s = conv.Convert((const char *)from);
     dbgobjects(" conv '%s'", s);
     free(txtData);
     txtData = strdup(s);
     }
}

void cSubtitleObject::DecodeSubBlock(cBitmap *Bitmap, int px, int py, const uchar *Data, int Length, bool Even)
{
  int x = 0;
  int y = Even ? 0 : 1;
  uint8_t map2to4[ 4] = { 0x00, 0x07, 0x08, 0x0F };
  uint8_t map2to8[ 4] = { 0x00, 0x77, 0x88, 0xFF };
  uint8_t map4to8[16] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };
  const uint8_t *mapTable = NULL;
  cBitStream bs(Data, Length * 8);
  while (!bs.IsEOF()) {
        switch (bs.GetBits(8)) {
          case 0x10:
               dbgpixel("2-bit / pixel code string<br>\n");
               switch (Bitmap->Bpp()) {
                 case 8:  mapTable = map2to8; break;
                 case 4:  mapTable = map2to4; break;
                 default: mapTable = NULL;    break;
                 }
               while (Decode2BppCodeString(Bitmap, px, py, &bs, x, y, mapTable) && !bs.IsEOF())
                     ;
               bs.ByteAlign();
               break;
          case 0x11:
               dbgpixel("4-bit / pixel code string<br>\n");
               switch (Bitmap->Bpp()) {
                 case 8:  mapTable = map4to8; break;
                 default: mapTable = NULL;    break;
                 }
               while (Decode4BppCodeString(Bitmap, px, py, &bs, x, y, mapTable) && !bs.IsEOF())
                     ;
               bs.ByteAlign();
               break;
          case 0x12:
               dbgpixel("8-bit / pixel code string<br>\n");
               while (Decode8BppCodeString(Bitmap, px, py, &bs, x, y) && !bs.IsEOF())
                     ;
               break;
          case 0x20:
               dbgpixel("sub block 2 to 4 map<br>\n");
               for (int i = 0; i < 4; ++i)
                   map2to4[i] = bs.GetBits(4);
               break;
          case 0x21:
               dbgpixel("sub block 2 to 8 map<br>\n");
               for (int i = 0; i < 4; ++i)
                   map2to8[i] = bs.GetBits(8);
               break;
          case 0x22:
               dbgpixel("sub block 4 to 8 map<br>\n");
               for (int i = 0; i < 16; ++i)
                   map4to8[i] = bs.GetBits(8);
               break;
          case 0xF0:
               dbgpixel("end of object line<br>\n");
               x = 0;
               y += 2;
               break;
          case 0xFF:
               dbgpixel("PGS code string, including EOLs<br>\n");
               while (DecodePgsCodeString(Bitmap, px, py, &bs, x, y) && !bs.IsEOF()) {
                     x = 0;
                     y++;
                     }
               break;
          default: dbgpixel("unknown sub block %s %d<br>\n", __FUNCTION__, __LINE__);
          }
        }
}

void cSubtitleObject::DrawLine(cBitmap *Bitmap, int x, int y, tIndex Index, int Length)
{
  if (nonModifyingColorFlag && Index == 1)
     return;
  for (int pos = x; pos < x + Length; pos++)
      Bitmap->SetIndex(pos, y, Index);
}

bool cSubtitleObject::Decode2BppCodeString(cBitmap *Bitmap, int px, int py, cBitStream *bs, int &x, int y, const uint8_t *MapTable)
{
  int rl = 0;
  int color = 0;
  uchar code = bs->GetBits(2);
  if (code) {
     color = code;
     rl = 1;
     }
  else if (bs->GetBit()) { // switch_1
     rl = bs->GetBits(3) + 3;
     color = bs->GetBits(2);
     }
  else if (bs->GetBit()) // switch_2
     rl = 1; //color 0
  else {
     switch (bs->GetBits(2)) { // switch_3
       case 0:
            return false;
       case 1:
            rl = 2; //color 0
            break;
       case 2:
            rl = bs->GetBits(4) + 12;
            color = bs->GetBits(2);
            break;
       case 3:
            rl = bs->GetBits(8) + 29;
            color = bs->GetBits(2);
            break;
       default: ;
       }
     }
  if (MapTable)
     color = MapTable[color];
  DrawLine(Bitmap, px + x, py + y, color, rl);
  x += rl;
  return true;
}

bool cSubtitleObject::Decode4BppCodeString(cBitmap *Bitmap, int px, int py, cBitStream *bs, int &x, int y, const uint8_t *MapTable)
{
  int rl = 0;
  int color = 0;
  uchar code = bs->GetBits(4);
  if (code) {
     color = code;
     rl = 1;
     }
  else if (bs->GetBit() == 0) { // switch_1
     code = bs->GetBits(3);
     if (code)
        rl = code + 2; //color 0
     else
        return false;
     }
  else if (bs->GetBit() == 0) { // switch_2
     rl = bs->GetBits(2) + 4;
     color = bs->GetBits(4);
     }
  else {
     switch (bs->GetBits(2)) { // switch_3
       case 0: // color 0
            rl = 1;
            break;
       case 1: // color 0
            rl = 2;
            break;
       case 2:
            rl = bs->GetBits(4) + 9;
            color = bs->GetBits(4);
            break;
       case 3:
            rl = bs->GetBits(8) + 25;
            color = bs->GetBits(4);
            break;
       }
     }
  if (MapTable)
     color = MapTable[color];
  DrawLine(Bitmap, px + x, py + y, color, rl);
  x += rl;
  return true;
}

bool cSubtitleObject::Decode8BppCodeString(cBitmap *Bitmap, int px, int py, cBitStream *bs, int &x, int y)
{
  int rl = 0;
  int color = 0;
  uchar code = bs->GetBits(8);
  if (code) {
     color = code;
     rl = 1;
     }
  else if (bs->GetBit()) {
     rl = bs->GetBits(7);
     color = bs->GetBits(8);
     }
  else {
     code = bs->GetBits(7);
     if (code)
        rl = code; // color 0
     else
        return false;
     }
  DrawLine(Bitmap, px + x, py + y, color, rl);
  x += rl;
  return true;
}

bool cSubtitleObject::DecodePgsCodeString(cBitmap *Bitmap, int px, int py, cBitStream *bs, int &x, int y)
{
  while (!bs->IsEOF()) {
        int color = bs->GetBits(8);
        int rl = 1;
        if (!color) {
           int flags = bs->GetBits(8);
           rl = flags & 0x3f;
           if (flags & 0x40)
              rl = (rl << 8) + bs->GetBits(8);
           color = flags & 0x80 ? bs->GetBits(8) : 0;
           }
        if (rl > 0) {
           DrawLine(Bitmap, px + x, py + y, color, rl);
           x += rl;
           }
        else if (!rl)
           return true;
        }
  return false;
}

void cSubtitleObject::Render(cBitmap *Bitmap, int px, int py, tIndex IndexFg, tIndex IndexBg)
{
  if (objectCodingMethod == 0) { // coding of pixels
     DecodeSubBlock(Bitmap, px, py, topData, topLength, true);
     if (botLength)
        DecodeSubBlock(Bitmap, px, py, botData, botLength, false);
     else
        DecodeSubBlock(Bitmap, px, py, topData, topLength, false);
     }
  else if (objectCodingMethod == 1) { // coded as a string of characters
     if (txtData) {
        //TODO couldn't we draw the text directly into Bitmap?
        cFont *font = cFont::CreateFont(Setup.FontOsd, Setup.FontOsdSize);
        cBitmap tmp(font->Width(txtData), font->Height(), Bitmap->Bpp());
        double factor = (double)lineHeight / font->Height();
        tmp.DrawText(0, 0, txtData, Bitmap->Color(IndexFg), Bitmap->Color(IndexBg), font);
        cBitmap *scaled = tmp.Scaled(factor, factor, true);
        Bitmap->DrawBitmap(px, py, *scaled);
        delete scaled;
        delete font;
        }
     }
}

// --- cSubtitleObjects ------------------------------------------------------

class cSubtitleObjects : public cList<cSubtitleObject> {
public:
  cSubtitleObject *GetObjectById(int ObjectId, bool New = false);
  };

cSubtitleObject *cSubtitleObjects::GetObjectById(int ObjectId, bool New)
{
  for (cSubtitleObject *so = First(); so; so = Next(so)) {
      if (so->ObjectId() == ObjectId)
         return so;
      }
  if (!New)
     return NULL;
  cSubtitleObject *Object = new cSubtitleObject(ObjectId);
  Add(Object);
  return Object;
}

// --- cSubtitleObjectRef ----------------------------------------------------

class cSubtitleObjectRef : public cListObject {
protected:
  int objectId;
  int objectType;
  int objectProviderFlag;
  int objectHorizontalPosition;
  int objectVerticalPosition;
  int foregroundPixelCode;
  int backgroundPixelCode;
public:
  cSubtitleObjectRef(void);
  cSubtitleObjectRef(cBitStream &bs);
  int ObjectId(void) { return objectId; }
  int ObjectType(void) { return objectType; }
  int ObjectProviderFlag(void) { return objectProviderFlag; }
  int ObjectHorizontalPosition(void) { return objectHorizontalPosition; }
  int ObjectVerticalPosition(void) { return objectVerticalPosition; }
  int ForegroundPixelCode(void) { return foregroundPixelCode; }
  int BackgroundPixelCode(void) { return backgroundPixelCode; }
  };

cSubtitleObjectRef::cSubtitleObjectRef(void)
{
  objectId = 0;
  objectType = 0;
  objectProviderFlag = 0;
  objectHorizontalPosition = 0;
  objectVerticalPosition = 0;
  foregroundPixelCode = 0;
  backgroundPixelCode = 0;
}

cSubtitleObjectRef::cSubtitleObjectRef(cBitStream &bs)
{
  objectId = bs.GetBits(16);
  objectType = bs.GetBits(2);
  objectProviderFlag = bs.GetBits(2);
  objectHorizontalPosition = bs.GetBits(12);
  bs.SkipBits(4); // reserved
  objectVerticalPosition = bs.GetBits(12);
  if (objectType == 0x01 || objectType == 0x02) {
     foregroundPixelCode = bs.GetBits(8);
     backgroundPixelCode = bs.GetBits(8);
     }
  else {
     foregroundPixelCode = 0;
     backgroundPixelCode = 0;
     }
  dbgregions("<b>objectref</b> id %d type %d flag %d x %d y %d fg %d bg %d<br>\n", objectId, objectType, objectProviderFlag, objectHorizontalPosition, objectVerticalPosition, foregroundPixelCode, backgroundPixelCode);
}

// --- cSubtitleObjectRefPgs - PGS variant of cSubtitleObjectRef -------------

class cSubtitleObjectRefPgs : public cSubtitleObjectRef {
private:
  int windowId;
  int compositionFlag;
  int cropX;
  int cropY;
  int cropW;
  int cropH;
public:
  cSubtitleObjectRefPgs(cBitStream &bs);
};

cSubtitleObjectRefPgs::cSubtitleObjectRefPgs(cBitStream &bs)
:cSubtitleObjectRef()
{
  objectId = bs.GetBits(16);
  windowId = bs.GetBits(8);
  compositionFlag = bs.GetBits(8);
  bs.SkipBits(32); // skip absolute position, object is aligned to region
  if ((compositionFlag & 0x80) != 0) {
     cropX = bs.GetBits(16);
     cropY = bs.GetBits(16);
     cropW = bs.GetBits(16);
     cropH = bs.GetBits(16);
     }
  else
     cropX = cropY = cropW = cropH = 0;
  dbgregions("<b>objectrefPgs</b> id %d flag %d x %d y %d cropX %d cropY %d cropW %d cropH %d<br>\n", objectId, compositionFlag, objectHorizontalPosition, objectVerticalPosition, cropX, cropY, cropW, cropH);
}

// --- cSubtitleRegion -------------------------------------------------------

class cSubtitleRegion : public cListObject {
private:
  int regionId;
  int regionVersionNumber;
  bool regionFillFlag;
  int regionWidth;
  int regionHeight;
  int regionLevelOfCompatibility;
  int regionDepth;
  int clutId;
  int region8bitPixelCode;
  int region4bitPixelCode;
  int region2bitPixelCode;
  cList<cSubtitleObjectRef> objectRefs;
public:
  cSubtitleRegion(int RegionId);
  void Parse(cBitStream &bs);
  void ParsePgs(cBitStream &bs);
  void SetDimensions(int Width, int Height);
  int RegionId(void) { return regionId; }
  int RegionVersionNumber(void) { return regionVersionNumber; }
  bool RegionFillFlag(void) { return regionFillFlag; }
  int RegionWidth(void) { return regionWidth; }
  int RegionHeight(void) { return regionHeight; }
  int RegionLevelOfCompatibility(void) { return regionLevelOfCompatibility; }
  int RegionDepth(void) { return regionDepth; }
  int ClutId(void) { return clutId; }
  void Render(cBitmap *Bitmap, cSubtitleObjects *Objects);
  };

cSubtitleRegion::cSubtitleRegion(int RegionId)
{
  regionId = RegionId;
  regionVersionNumber = -1;
  regionFillFlag = false;
  regionWidth = 0;
  regionHeight = 0;
  regionLevelOfCompatibility = 0;
  regionDepth = 0;
  clutId = -1;
  region8bitPixelCode = 0;
  region4bitPixelCode = 0;
  region2bitPixelCode = 0;
}

void cSubtitleRegion::Parse(cBitStream &bs)
{
  int Version = bs.GetBits(4);
#ifndef FIX_SUBTITLE_VERSION_BROADCASTER_STUPIDITY
  if (regionVersionNumber == Version)
     return; // no update
#endif
  regionVersionNumber = Version;
  regionFillFlag = bs.GetBit();
  bs.SkipBits(3); // reserved
  regionWidth = bs.GetBits(16);
  regionHeight = bs.GetBits(16);
  regionLevelOfCompatibility = 1 << bs.GetBits(3); // stored as "number of bits per pixel"
  regionDepth = 1 << bs.GetBits(3); // stored as "number of bits per pixel"
  bs.SkipBits(2); // reserved
  clutId = bs.GetBits(8);
  region8bitPixelCode = bs.GetBits(8);
  region4bitPixelCode = bs.GetBits(4);
  region2bitPixelCode = bs.GetBits(2);
  bs.SkipBits(2); // reserved
  dbgregions("<b>region</b> id %d version %d fill %d width %d height %d level %d depth %d clutId %d<br>\n", regionId, regionVersionNumber, regionFillFlag, regionWidth, regionHeight, regionLevelOfCompatibility, regionDepth, clutId);
  // no objectRefs.Clear() here!
  while (!bs.IsEOF())
        objectRefs.Add(new cSubtitleObjectRef(bs));
}

void cSubtitleRegion::ParsePgs(cBitStream &bs)
{
  regionDepth = 8;
  bs.SkipBits(8); // skip palette update flag
  clutId = bs.GetBits(8);
  dbgregions("<b>region</b> id %d version %d clutId %d<br>\n", regionId, regionVersionNumber, clutId);
  int objects = bs.GetBits(8);
  while (objects--)
        objectRefs.Add(new cSubtitleObjectRefPgs(bs));
}

void cSubtitleRegion::SetDimensions(int Width, int Height)
{
  regionWidth = Width;
  regionHeight = Height;
  dbgregions("<b>region</b> id %d width %d height %d<br>\n", regionId, regionWidth, regionHeight);
}

void cSubtitleRegion::Render(cBitmap *Bitmap, cSubtitleObjects *Objects)
{
  if (regionFillFlag) {
     switch (Bitmap->Bpp()) {
       case 2: Bitmap->Fill(region2bitPixelCode); break;
       case 4: Bitmap->Fill(region4bitPixelCode); break;
       case 8: Bitmap->Fill(region8bitPixelCode); break;
       default: dbgregions("unknown bpp %d (%s %d)<br>\n", Bitmap->Bpp(), __FUNCTION__, __LINE__);
       }
     }
  for (cSubtitleObjectRef *sor = objectRefs.First(); sor; sor = objectRefs.Next(sor)) {
      if (cSubtitleObject *so = Objects->GetObjectById(sor->ObjectId())) {
         so->Render(Bitmap, sor->ObjectHorizontalPosition(), sor->ObjectVerticalPosition(), sor->ForegroundPixelCode(), sor->BackgroundPixelCode());
         }
      }
}

// --- cSubtitleRegionRef ----------------------------------------------------

class cSubtitleRegionRef : public cListObject {
private:
  int regionId;
  int regionHorizontalAddress;
  int regionVerticalAddress;
public:
  cSubtitleRegionRef(int id, int x, int y);
  cSubtitleRegionRef(cBitStream &bs);
  int RegionId(void) { return regionId; }
  int RegionHorizontalAddress(void) { return regionHorizontalAddress; }
  int RegionVerticalAddress(void) { return regionVerticalAddress; }
  };

cSubtitleRegionRef::cSubtitleRegionRef(int id, int x, int y)
{
  regionId = id;
  regionHorizontalAddress = x;
  regionVerticalAddress = y;
  dbgpages("<b>regionref</b> id %d tx %d y %d<br>\n", regionId, regionHorizontalAddress, regionVerticalAddress);
}
cSubtitleRegionRef::cSubtitleRegionRef(cBitStream &bs)
{
  regionId = bs.GetBits(8);
  bs.SkipBits(8); // reserved
  regionHorizontalAddress = bs.GetBits(16);
  regionVerticalAddress = bs.GetBits(16);
  dbgpages("<b>regionref</b> id %d tx %d y %d<br>\n", regionId, regionHorizontalAddress, regionVerticalAddress);
}

// --- cDvbSubtitlePage ------------------------------------------------------

class cDvbSubtitlePage : public cListObject {
private:
  int pageId;
  int pageTimeout;
  int pageVersionNumber;
  int pageState;
  int64_t pts;
  bool pending;
  cSubtitleObjects objects;
  cList<cSubtitleClut> cluts;
  cList<cSubtitleRegion> regions;
  cList<cSubtitleRegionRef> regionRefs;
public:
  cDvbSubtitlePage(int PageId);
  void Parse(int64_t Pts, cBitStream &bs);
  void ParsePgs(int64_t Pts, cBitStream &bs);
  int PageId(void) { return pageId; }
  int PageTimeout(void) { return pageTimeout; }
  int PageVersionNumber(void) { return pageVersionNumber; }
  int PageState(void) { return pageState; }
  int64_t Pts(void) const { return pts; }
  bool Pending(void) { return pending; }
  cSubtitleObjects *Objects(void) { return &objects; }
  tArea *GetAreas(int &NumAreas);
  tArea CombineAreas(int NumAreas, const tArea *Areas);
  tArea ScaleArea(const tArea &Area, double FactorX, double FactorY);
  cSubtitleObject *GetObjectById(int ObjectId, bool New = false);
  cSubtitleClut *GetClutById(int ClutId, bool New = false);
  cSubtitleRegion *GetRegionById(int RegionId, bool New = false);
  cSubtitleRegionRef *GetRegionRefByIndex(int RegionRefIndex) { return regionRefs.Get(RegionRefIndex); }
  void AddRegionRef(cSubtitleRegionRef *rf) { regionRefs.Add(rf); }
  void SetPending(bool Pending) { pending = Pending; }
  };

cDvbSubtitlePage::cDvbSubtitlePage(int PageId)
{
  pageId = PageId;
  pageTimeout = 0;
  pageVersionNumber = -1;
  pageState = -1;
  pts = -1;
  pending = false;
}

void cDvbSubtitlePage::Parse(int64_t Pts, cBitStream &bs)
{
  if (Pts >= 0)
     pts = Pts;
  pageTimeout = bs.GetBits(8);
  int Version = bs.GetBits(4);
#ifndef FIX_SUBTITLE_VERSION_BROADCASTER_STUPIDITY
  if (pageVersionNumber == Version)
     return; // no update
#endif
  pageVersionNumber = Version;
  pageState = bs.GetBits(2);
  switch (pageState) {
    case 0: // normal case - page update
         break;
    case 1: // acquisition point - page refresh
         regions.Clear();
         objects.Clear();
         break;
    case 2: // mode change - new page
         regions.Clear();
         cluts.Clear();
         objects.Clear();
         break;
    case 3: // reserved
         break;
    default: dbgpages("unknown page state: %d<br>\n", pageState);
    }
  bs.SkipBits(2); // reserved
  dbgpages("<hr>\n<b>page</b> id %d version %d pts %" PRId64 " timeout %d state %d<br>\n", pageId, pageVersionNumber, pts, pageTimeout, pageState);
  regionRefs.Clear();
  while (!bs.IsEOF())
        regionRefs.Add(new cSubtitleRegionRef(bs));
  pending = true;
}

void cDvbSubtitlePage::ParsePgs(int64_t Pts, cBitStream &bs)
{
  if (Pts >= 0)
     pts = Pts;
  pageTimeout = 60000;
  int Version = bs.GetBits(16);
  if (pageVersionNumber == Version)
     return;
  pageVersionNumber = Version;
  pageState = bs.GetBits(2);
  switch (pageState) {
    case 0: // normal case - page update
         regions.Clear();
         break;
    case 1: // acquisition point - page refresh
    case 2: // epoch start - new page
    case 3: // epoch continue - new page
         regions.Clear();
         cluts.Clear();
         objects.Clear();
         break;
    default: dbgpages("unknown page state: %d<br>\n", pageState);
    }
  bs.SkipBits(6);
  dbgpages("<hr>\n<b>page</b> id %d version %d pts %" PRId64 " timeout %d state %d<br>\n", pageId, pageVersionNumber, pts, pageTimeout, pageState);
  regionRefs.Clear();
  pending = true;
}

tArea *cDvbSubtitlePage::GetAreas(int &NumAreas)
{
  if (regions.Count() > 0) {
     NumAreas = regionRefs.Count();
     tArea *Areas = new tArea[NumAreas];
     tArea *a = Areas;
     for (cSubtitleRegionRef *srr = regionRefs.First(); srr; srr = regionRefs.Next(srr)) {
         if (cSubtitleRegion *sr = GetRegionById(srr->RegionId())) {
            a->x1 = srr->RegionHorizontalAddress();
            a->y1 = srr->RegionVerticalAddress();
            a->x2 = srr->RegionHorizontalAddress() + sr->RegionWidth() - 1;
            a->y2 = srr->RegionVerticalAddress() + sr->RegionHeight() - 1;
            a->bpp = sr->RegionDepth();
            }
         else
            a->x1 = a->y1 = a->x2 = a->y2 = a->bpp = 0;
         a++;
         }
     return Areas;
     }
  NumAreas = 0;
  return NULL;
}

tArea cDvbSubtitlePage::CombineAreas(int NumAreas, const tArea *Areas)
{
  tArea a;
  a.x1 = INT_MAX;
  a.x2 = INT_MIN;
  a.y1 = INT_MAX;
  a.y2 = INT_MIN;
  a.bpp = 1;
  for (int i = 0; i < NumAreas; i++) {
      a.x1 = min(a.x1, Areas[i].x1);
      a.x2 = max(a.x2, Areas[i].x2);
      a.y1 = min(a.y1, Areas[i].y1);
      a.y2 = max(a.y2, Areas[i].y2);
      a.bpp = max(a.bpp, Areas[i].bpp);
      }
  return a;
}

tArea cDvbSubtitlePage::ScaleArea(const tArea &Area, double FactorX, double FactorY)
{
  tArea a;
  a.x1  = int(round(FactorX * Area.x1)    );
  a.x2  = int(round(FactorX * Area.x2) - 1);
  a.y1  = int(round(FactorY * Area.y1)    );
  a.y2  = int(round(FactorY * Area.y2) - 1);
  a.bpp = Area.bpp;
  while ((a.Width() & 3) != 0)
        a.x2++; // aligns width to a multiple of 4, so 2, 4 and 8 bpp will work
  return a;
}

cSubtitleClut *cDvbSubtitlePage::GetClutById(int ClutId, bool New)
{
  for (cSubtitleClut *sc = cluts.First(); sc; sc = cluts.Next(sc)) {
      if (sc->ClutId() == ClutId)
         return sc;
      }
  if (!New)
     return NULL;
  cSubtitleClut *Clut = new cSubtitleClut(ClutId);
  cluts.Add(Clut);
  return Clut;
}

cSubtitleRegion *cDvbSubtitlePage::GetRegionById(int RegionId, bool New)
{
  for (cSubtitleRegion *sr = regions.First(); sr; sr = regions.Next(sr)) {
      if (sr->RegionId() == RegionId)
         return sr;
      }
  if (!New)
     return NULL;
  cSubtitleRegion *Region = new cSubtitleRegion(RegionId);
  regions.Add(Region);
  return Region;
}

cSubtitleObject *cDvbSubtitlePage::GetObjectById(int ObjectId, bool New)
{
  return objects.GetObjectById(ObjectId, New);
}

// --- cDvbSubtitleAssembler -------------------------------------------------

class cDvbSubtitleAssembler {
private:
  uchar *data;
  int length;
  int pos;
  int size;
  bool Realloc(int Size);
public:
  cDvbSubtitleAssembler(void);
  virtual ~cDvbSubtitleAssembler();
  void Reset(void);
  unsigned char *Get(int &Length);
  void Put(const uchar *Data, int Length);
  };

cDvbSubtitleAssembler::cDvbSubtitleAssembler(void)
{
  data = NULL;
  size = 0;
  Reset();
}

cDvbSubtitleAssembler::~cDvbSubtitleAssembler()
{
  free(data);
}

void cDvbSubtitleAssembler::Reset(void)
{
  length = 0;
  pos = 0;
}

bool cDvbSubtitleAssembler::Realloc(int Size)
{
  if (Size > size) {
     Size = max(Size, 2048);
     if (uchar *NewBuffer = (uchar *)realloc(data, Size)) {
        size = Size;
        data = NewBuffer;
        }
     else {
        esyslog("ERROR: can't allocate memory for subtitle assembler");
        length = 0;
        size = 0;
        free(data);
        data = NULL;
        return false;
        }
     }
  return true;
}

unsigned char *cDvbSubtitleAssembler::Get(int &Length)
{
  if (length > pos + 5) {
     Length = (data[pos + 4] << 8) + data[pos + 5] + 6;
     if (length >= pos + Length) {
        unsigned char *result = data + pos;
        pos += Length;
        return result;
        }
     }
  return NULL;
}

void cDvbSubtitleAssembler::Put(const uchar *Data, int Length)
{
  if (Length && Realloc(length + Length)) {
     memcpy(data + length, Data, Length);
     length += Length;
     }
}

// --- cDvbSubtitleBitmaps ---------------------------------------------------

class cDvbSubtitleBitmaps : public cListObject {
private:
  int state;
  int64_t pts;
  int timeout;
  tArea *areas;
  int numAreas;
  tArea areaCombined;
  tArea areaOsd;
  double osdFactorX;
  double osdFactorY;
  cVector<cBitmap *> bitmaps;
public:
  cDvbSubtitleBitmaps(int State, int64_t Pts, int Timeout, tArea *Areas, int NumAreas, double OsdFactorX, double OsdFactorY, tArea &AreaCombined, tArea &AreaOsd);
  ~cDvbSubtitleBitmaps();
  int State(void) { return state; }
  int64_t Pts(void) { return pts; }
  int Timeout(void) { return timeout; }
  void AddBitmap(cBitmap *Bitmap);
  bool HasBitmaps(void) { return bitmaps.Size(); }
  void Draw(cOsd *Osd);
  void DbgDump(int WindowWidth, int WindowHeight);
  };

cDvbSubtitleBitmaps::cDvbSubtitleBitmaps(int State, int64_t Pts, int Timeout, tArea *Areas, int NumAreas, double OsdFactorX, double OsdFactorY, tArea &AreaCombined, tArea &AreaOsd)
{
  state = State;
  pts = Pts;
  timeout = Timeout;
  areas = Areas;
  numAreas = NumAreas;
  areaCombined = AreaCombined;
  areaOsd = AreaOsd;
  osdFactorX = OsdFactorX;
  osdFactorY = OsdFactorY;
}

cDvbSubtitleBitmaps::~cDvbSubtitleBitmaps()
{
  delete[] areas;
  for (int i = 0; i < bitmaps.Size(); i++)
      delete bitmaps[i];
}

void cDvbSubtitleBitmaps::AddBitmap(cBitmap *Bitmap)
{
  bitmaps.Append(Bitmap);
}

void cDvbSubtitleBitmaps::Draw(cOsd *Osd)
{
  bool Scale = !(DoubleEqual(osdFactorX, 1.0) && DoubleEqual(osdFactorY, 1.0));
  bool AntiAlias = Setup.AntiAlias;
  if (Scale && osdFactorX > 1.0 || osdFactorY > 1.0) {
     // Upscaling requires 8bpp:
     int Bpp = areaOsd.bpp;
     areaOsd.bpp = 8;
     if (Osd->CanHandleAreas(&areaOsd, 1) != oeOk) {
        areaOsd.bpp = Bpp;
        AntiAlias = false;
        }
     }
  if (State() == 0 || Osd->SetAreas(&areaOsd, 1) == oeOk) {
     cBitmap combined(areaCombined.Width(), areaCombined.Height(), areaCombined.bpp);
     combined.SetOffset(areaCombined.x1, areaCombined.y1);
     for (int i = 0; i < bitmaps.Size(); i++) {
         // merge bitmaps into combined
         cBitmap *b = bitmaps[i];
         combined.DrawBitmap(b->X0(), b->Y0(), *b);
         }
     Osd->DrawScaledBitmap(int(round(combined.X0() * osdFactorX)), int(round(combined.Y0() * osdFactorY)), combined, osdFactorX, osdFactorY, AntiAlias);
     Osd->Flush();
     }
}

void cDvbSubtitleBitmaps::DbgDump(int WindowWidth, int WindowHeight)
{
  if (!SD.Active())
     return;
  SD.SetFirstPts(Pts());
  double STC = double(cDevice::PrimaryDevice()->GetSTC() - SD.FirstPts()) / 90000;
  double Start = double(Pts() - SD.FirstPts()) / 90000;
  double Duration = Timeout();
  double End = Start + Duration;
  cBitmap Bitmap(WindowWidth, WindowHeight, 8);
#define DBGBACKGROUND 0xA0A0A0
  Bitmap.DrawRectangle(0, 0, WindowWidth - 1, WindowHeight - 1, DBGBACKGROUND);
  for (int i = 0; i < bitmaps.Size(); i++) {
      cBitmap *b = bitmaps[i];
      Bitmap.DrawBitmap(b->X0(), b->Y0(), *b);
      }
  cString ImgName = SD.WriteJpeg(&Bitmap);
#define BORDER //" border=1"
  SD.WriteHtml("<p>%s<br>", State() == 0 ? "page update" : State() == 1 ? "page refresh" : State() == 2 ? "new page" : "???");
  SD.WriteHtml("<table" BORDER "><tr><td>");
  SD.WriteHtml("%.2f", STC);
  SD.WriteHtml("</td><td>");
  SD.WriteHtml("<img src=\"%s\">", *ImgName);
  SD.WriteHtml("</td><td style=\"height:100%%\"><table" BORDER " style=\"height:100%%\">");
  SD.WriteHtml("<tr><td valign=top><b>%.2f</b></td></tr>", Start);
  SD.WriteHtml("<tr><td valign=middle>%.2f</td></tr>", Duration);
  SD.WriteHtml("<tr><td valign=bottom>%.2f</td></tr>", End);
  SD.WriteHtml("</table></td>");
  SD.WriteHtml("</tr></table>\n");
}

// --- cDvbSubtitleConverter -------------------------------------------------

int cDvbSubtitleConverter::setupLevel = 0;

cDvbSubtitleConverter::cDvbSubtitleConverter(void)
:cThread("subtitle converter")
{
  dvbSubtitleAssembler = new cDvbSubtitleAssembler;
  osd = NULL;
  frozen = false;
  ddsVersionNumber = -1;
  displayWidth = windowWidth = 720;
  displayHeight = windowHeight = 576;
  windowHorizontalOffset = 0;
  windowVerticalOffset = 0;
  pages = new cList<cDvbSubtitlePage>;
  bitmaps = new cList<cDvbSubtitleBitmaps>;
  SD.Reset();
  Start();
}

cDvbSubtitleConverter::~cDvbSubtitleConverter()
{
  Cancel(3);
  delete dvbSubtitleAssembler;
  delete osd;
  delete bitmaps;
  delete pages;
}

void cDvbSubtitleConverter::SetupChanged(void)
{
  setupLevel++;
}

void cDvbSubtitleConverter::Reset(void)
{
  dbgconverter("converter reset -----------------------<br>\n");
  dvbSubtitleAssembler->Reset();
  Lock();
  pages->Clear();
  bitmaps->Clear();
  DELETENULL(osd);
  frozen = false;
  ddsVersionNumber = -1;
  displayWidth = windowWidth = 720;
  displayHeight = windowHeight = 576;
  windowHorizontalOffset = 0;
  windowVerticalOffset = 0;
  Unlock();
}

int cDvbSubtitleConverter::ConvertFragments(const uchar *Data, int Length)
{
  if (Data && Length > 8) {
     int PayloadOffset = PesPayloadOffset(Data);
     int SubstreamHeaderLength = 4;
     bool ResetSubtitleAssembler = Data[PayloadOffset + 3] == 0x00;

     // Compatibility mode for old subtitles plugin:
     if ((Data[7] & 0x01) && (Data[PayloadOffset - 3] & 0x81) == 0x01 && Data[PayloadOffset - 2] == 0x81) {
        PayloadOffset--;
        SubstreamHeaderLength = 1;
        ResetSubtitleAssembler = Data[8] >= 5;
        }

     if (Length > PayloadOffset + SubstreamHeaderLength) {
        int64_t pts = PesHasPts(Data) ? PesGetPts(Data) : -1;
        if (pts >= 0)
           dbgconverter("converter PTS: %" PRId64 "<br>\n", pts);
        const uchar *data = Data + PayloadOffset + SubstreamHeaderLength; // skip substream header
        int length = Length - PayloadOffset - SubstreamHeaderLength; // skip substream header
        if (ResetSubtitleAssembler)
           dvbSubtitleAssembler->Reset();

        if (length > 3) {
           if (data[0] == 0x20 && data[1] == 0x00 && data[2] == 0x0F)
              dvbSubtitleAssembler->Put(data + 2, length - 2);
           else
              dvbSubtitleAssembler->Put(data, length);

           int Count;
           while (true) {
                 unsigned char *b = dvbSubtitleAssembler->Get(Count);
                 if (b && b[0] == 0x0F) {
                    if (ExtractSegment(b, Count, pts) == -1)
                       break;
                    }
                 else
                    break;
                 }
           }
        }
     return Length;
     }
  return 0;
}

int cDvbSubtitleConverter::Convert(const uchar *Data, int Length)
{
  if (Data && Length > 8) {
     int PayloadOffset = PesPayloadOffset(Data);
     if (Length > PayloadOffset) {
        int64_t pts = PesHasPts(Data) ? PesGetPts(Data) : -1;
        if (pts >= 0)
           dbgconverter("converter PTS: %" PRId64 "<br>\n", pts);
        const uchar *data = Data + PayloadOffset;
        int length = Length - PayloadOffset;
        if (length > 0) {
           if (length > 2 && data[0] == 0x20 && data[1] == 0x00 && data[2] == 0x0F) {
              data += 2;
              length -= 2;
              }
           const uchar *b = data;
           while (length > 0) {
                 if (b[0] == STUFFING_SEGMENT)
                    break;
                 int n;
                 if (b[0] == 0x0F)
                    n = ExtractSegment(b, length, pts);
                 else
                    n = ExtractPgsSegment(b, length, pts);
                 if (n < 0)
                    break;
                 b += n;
                 length -= n;
                 }
           }
        }
     return Length;
     }
  return 0;
}

#define LimitTo32Bit(n) ((n) & 0x00000000FFFFFFFFL)

void cDvbSubtitleConverter::Action(void)
{
  int LastSetupLevel = setupLevel;
  cTimeMs Timeout;
  while (Running()) {
        int WaitMs = 100;
        if (!frozen) {
           LOCK_THREAD;
           if (osd) {
              int NewSetupLevel = setupLevel;
              if (Timeout.TimedOut() || LastSetupLevel != NewSetupLevel) {
                 dbgoutput("closing osd<br>\n");
                 DELETENULL(osd);
                 }
              LastSetupLevel = NewSetupLevel;
              }
           for (cDvbSubtitleBitmaps *sb = bitmaps->First(); sb; sb = bitmaps->Next(sb)) {
               // Calculate the Delta between the STC (the current timestamp of the video)
               // and the bitmap's PTS (the timestamp when the bitmap shall be presented).
               // A negative Delta means that the bitmap will be presented in the future:
               int64_t STC = cDevice::PrimaryDevice()->GetSTC();
               int64_t Delta = LimitTo32Bit(STC) - LimitTo32Bit(sb->Pts()); // some devices only deliver 32 bits
               if (Delta > (int64_t(1) << 31))
                  Delta -= (int64_t(1) << 32);
               else if (Delta < -((int64_t(1) << 31) - 1))
                  Delta += (int64_t(1) << 32);
               Delta /= 90; // STC and PTS are in 1/90000s
               if (Delta >= 0) { // found a bitmap that shall be displayed...
                  if (Delta < sb->Timeout() * 1000) { // ...and has not timed out yet
                     if (!sb->HasBitmaps()) {
                        Timeout.Set();
                        WaitMs = 0;
                        }
                     else if (AssertOsd()) {
                        dbgoutput("showing bitmap #%d of %d<br>\n", sb->Index() + 1, bitmaps->Count());
                        sb->Draw(osd);
                        Timeout.Set(sb->Timeout() * 1000);
                        dbgconverter("PTS: %" PRId64 "  STC: %" PRId64 " (%" PRId64 ") timeout: %d<br>\n", sb->Pts(), STC, Delta, sb->Timeout());
                        }
                     }
                  else
                     WaitMs = 0; // bitmap already timed out, so try next one immediately
                  dbgoutput("deleting bitmap #%d of %d<br>\n", sb->Index() + 1, bitmaps->Count());
                  bitmaps->Del(sb);
                  break;
                  }
               }
           }
        cCondWait::SleepMs(WaitMs);
        }
}

void cDvbSubtitleConverter::SetOsdData(void)
{
  int OsdWidth, OsdHeight;
  double OsdAspect;
  int VideoWidth, VideoHeight;
  double VideoAspect;
  cDevice::PrimaryDevice()->GetOsdSize(OsdWidth, OsdHeight, OsdAspect);
  cDevice::PrimaryDevice()->GetVideoSize(VideoWidth, VideoHeight, VideoAspect);
  if (OsdWidth == displayWidth && OsdHeight == displayHeight) {
     osdFactorX = osdFactorY = 1.0;
     osdDeltaX = osdDeltaY = 0;
     }
  else {
     osdFactorX = osdFactorY = min(double(OsdWidth) / displayWidth, double(OsdHeight) / displayHeight);
     osdDeltaX = (OsdWidth - displayWidth * osdFactorX) / 2;
     osdDeltaY = (OsdHeight - displayHeight * osdFactorY) / 2;
     }
}

bool cDvbSubtitleConverter::AssertOsd(void)
{
  LOCK_THREAD;
  if (!osd) {
     SetOsdData();
     osd = cOsdProvider::NewOsd(int(round(osdFactorX * windowHorizontalOffset + osdDeltaX)), int(round(osdFactorY * windowVerticalOffset + osdDeltaY)) + Setup.SubtitleOffset, OSD_LEVEL_SUBTITLES);
     }
  return osd != NULL;
}

cDvbSubtitlePage *cDvbSubtitleConverter::GetPageById(int PageId, bool New)
{
  for (cDvbSubtitlePage *sp = pages->First(); sp; sp = pages->Next(sp)) {
      if (sp->PageId() == PageId)
         return sp;
      }
  if (!New)
     return NULL;
  cDvbSubtitlePage *Page = new cDvbSubtitlePage(PageId);
  pages->Add(Page);
  return Page;
}

int cDvbSubtitleConverter::ExtractSegment(const uchar *Data, int Length, int64_t Pts)
{
  cBitStream bs(Data, Length * 8);
  if (Length > 5 && bs.GetBits(8) == 0x0F) { // sync byte
     int segmentType = bs.GetBits(8);
     if (segmentType == STUFFING_SEGMENT)
        return -1;
     LOCK_THREAD;
     cDvbSubtitlePage *page = GetPageById(bs.GetBits(16), true);
     int segmentLength = bs.GetBits(16);
     if (!bs.SetLength(bs.Index() + segmentLength * 8))
        return -1;
     switch (segmentType) {
       case PAGE_COMPOSITION_SEGMENT: {
            if (page->Pending()) {
               dbgsegments("END_OF_DISPLAY_SET_SEGMENT (simulated)<br>\n");
               FinishPage(page);
               }
            dbgsegments("PAGE_COMPOSITION_SEGMENT<br>\n");
            page->Parse(Pts, bs);
            SD.SetFactor(double(DBGBITMAPWIDTH) / windowWidth);
            break;
            }
       case REGION_COMPOSITION_SEGMENT: {
            dbgsegments("REGION_COMPOSITION_SEGMENT<br>\n");
            cSubtitleRegion *region = page->GetRegionById(bs.GetBits(8), true);
            region->Parse(bs);
            break;
            }
       case CLUT_DEFINITION_SEGMENT: {
            dbgsegments("CLUT_DEFINITION_SEGMENT<br>\n");
            cSubtitleClut *clut = page->GetClutById(bs.GetBits(8), true);
            clut->Parse(bs);
            break;
            }
       case OBJECT_DATA_SEGMENT: {
            dbgsegments("OBJECT_DATA_SEGMENT<br>\n");
            cSubtitleObject *object = page->GetObjectById(bs.GetBits(16), true);
            object->Parse(bs);
            break;
            }
       case DISPLAY_DEFINITION_SEGMENT: {
            dbgsegments("DISPLAY_DEFINITION_SEGMENT<br>\n");
            int version = bs.GetBits(4);
#ifndef FIX_SUBTITLE_VERSION_BROADCASTER_STUPIDITY
            if (version == ddsVersionNumber)
               break; // no update
#endif
            bool displayWindowFlag = bs.GetBit();
            windowHorizontalOffset = 0;
            windowVerticalOffset   = 0;
            bs.SkipBits(3); // reserved
            displayWidth  = windowWidth  = bs.GetBits(16) + 1;
            displayHeight = windowHeight = bs.GetBits(16) + 1;
            if (displayWindowFlag) {
               windowHorizontalOffset = bs.GetBits(16);                              // displayWindowHorizontalPositionMinimum
               windowWidth            = bs.GetBits(16) - windowHorizontalOffset + 1; // displayWindowHorizontalPositionMaximum
               windowVerticalOffset   = bs.GetBits(16);                              // displayWindowVerticalPositionMinimum
               windowHeight           = bs.GetBits(16) - windowVerticalOffset + 1;   // displayWindowVerticalPositionMaximum
               }
            SetOsdData();
            ddsVersionNumber = version;
            dbgdisplay("<b>display</b> version %d flag %d width %d height %d ofshor %d ofsver %d<br>\n", ddsVersionNumber, displayWindowFlag, windowWidth, windowHeight, windowHorizontalOffset, windowVerticalOffset);
            break;
            }
       case DISPARITY_SIGNALING_SEGMENT: {
            dbgsegments("DISPARITY_SIGNALING_SEGMENT<br>\n");
            bs.SkipBits(4); // dss_version_number
            bool disparity_shift_update_sequence_page_flag = bs.GetBit();
            bs.SkipBits(3); // reserved
            bs.SkipBits(8); // page_default_disparity_shift
            if (disparity_shift_update_sequence_page_flag) {
               bs.SkipBits(8); // disparity_shift_update_sequence_length
               bs.SkipBits(24); // interval_duration[23..0]
               int division_period_count = bs.GetBits(8);
               for (int i = 0; i < division_period_count; ++i) {
                   bs.SkipBits(8); // interval_count
                   bs.SkipBits(8); // disparity_shift_update_integer_part
                   }
               }
            while (!bs.IsEOF()) {
                  bs.SkipBits(8); // region_id
                  bool disparity_shift_update_sequence_region_flag = bs.GetBit();
                  bs.SkipBits(5); // reserved
                  int number_of_subregions_minus_1 = bs.GetBits(2);
                  for (int i = 0; i <= number_of_subregions_minus_1; ++i) {
                      if (number_of_subregions_minus_1 > 0) {
                         bs.SkipBits(16); // subregion_horizontal_position
                         bs.SkipBits(16); // subregion_width
                         }
                      bs.SkipBits(8); // subregion_disparity_shift_integer_part
                      bs.SkipBits(4); // subregion_disparity_shift_fractional_part
                      bs.SkipBits(4); // reserved
                      if (disparity_shift_update_sequence_region_flag) {
                         bs.SkipBits(8); // disparity_shift_update_sequence_length
                         bs.SkipBits(24); // interval_duration[23..0]
                         int division_period_count = bs.GetBits(8);
                         for (int i = 0; i < division_period_count; ++i) {
                             bs.SkipBits(8); // interval_count
                             bs.SkipBits(8); // disparity_shift_update_integer_part
                             }
                         }
                      }
                  }
            break;
            }
       case END_OF_DISPLAY_SET_SEGMENT: {
            dbgsegments("END_OF_DISPLAY_SET_SEGMENT<br>\n");
            FinishPage(page);
            page->SetPending(false);
            break;
            }
       default:
            dbgsegments("*** unknown segment type: %02X<br>\n", segmentType);
       }
     return bs.Length() / 8;
     }
  return -1;
}

int cDvbSubtitleConverter::ExtractPgsSegment(const uchar *Data, int Length, int64_t Pts)
{
  cBitStream bs(Data, Length * 8);
  if (Length >= 3) {
     int segmentType = bs.GetBits(8);
     int segmentLength = bs.GetBits(16);
     if (!bs.SetLength(bs.Index() + segmentLength * 8))
        return -1;
     LOCK_THREAD;
     cDvbSubtitlePage *page = GetPageById(0, true);
     switch (segmentType) {
       case PGS_PRESENTATION_SEGMENT: {
            if (page->Pending()) {
               dbgsegments("PGS_DISPLAY_SEGMENT (simulated)<br>\n");
               FinishPage(page);
               }
            dbgsegments("PGS_PRESENTATION_SEGMENT<br>\n");
            displayWidth  = windowWidth  = bs.GetBits(16);
            displayHeight = windowHeight = bs.GetBits(16);
            bs.SkipBits(8);
            page->ParsePgs(Pts, bs);
            SD.SetFactor(double(DBGBITMAPWIDTH) / windowWidth);
            cSubtitleRegion *region = page->GetRegionById(0, true);
            region->ParsePgs(bs);
            break;
            }
       case PGS_WINDOW_SEGMENT: {
            bs.SkipBits(16);
            int regionHorizontalAddress = bs.GetBits(16);
            int regionVerticalAddress   = bs.GetBits(16);
            int regionWidth  = bs.GetBits(16);
            int regionHeight = bs.GetBits(16);
            cSubtitleRegion *region = page->GetRegionById(0, true);
            region->SetDimensions(regionWidth, regionHeight);
            page->AddRegionRef(new cSubtitleRegionRef(0, regionHorizontalAddress, regionVerticalAddress));
            dbgsegments("PGS_WINDOW_SEGMENT<br>\n");
            break;
            }
       case PGS_PALETTE_SEGMENT: {
            dbgsegments("PGS_PALETTE_SEGMENT<br>\n");
            cSubtitleClut *clut = page->GetClutById(bs.GetBits(8), true);
            clut->ParsePgs(bs);
            break;
            }
       case PGS_OBJECT_SEGMENT: {
            dbgsegments("PGS_OBJECT_SEGMENT<br>\n");
            cSubtitleObject *object = page->GetObjectById(bs.GetBits(16), true);
            object->ParsePgs(bs);
            break;
            }
       case PGS_DISPLAY_SEGMENT: {
            dbgsegments("PGS_DISPLAY_SEGMENT<br>\n");
            FinishPage(page);
            page->SetPending(false);
            break;
            }
       default:
            dbgsegments("*** unknown segment type: %02X<br>\n", segmentType);
            return -1;
       }
     return bs.Length() / 8;
     }
  return -1;
}

void cDvbSubtitleConverter::FinishPage(cDvbSubtitlePage *Page)
{
  if (!AssertOsd())
     return;
  int NumAreas;
  tArea *Areas = Page->GetAreas(NumAreas);
  tArea AreaCombined = Page->CombineAreas(NumAreas, Areas);
  tArea AreaOsd = Page->ScaleArea(AreaCombined, osdFactorX, osdFactorY);
  int Bpp = 8;
  bool Reduced = false;
  if (osd && NumAreas > 0) {
     while (osd->CanHandleAreas(&AreaOsd, 1) != oeOk) {
           dbgoutput("CanHandleAreas: %d<br>\n", osd->CanHandleAreas(&AreaOsd, 1));
           int HalfBpp = Bpp / 2;
           if (HalfBpp >= 2) {
              if (AreaOsd.bpp >= Bpp) {
                  AreaOsd.bpp = HalfBpp;
                  Reduced = true;
                  }
              Bpp = HalfBpp;
              }
           else
              return; // unable to draw bitmaps
           }
     }
  cDvbSubtitleBitmaps *Bitmaps = new cDvbSubtitleBitmaps(Page->PageState(), Page->Pts(), Page->PageTimeout(), Areas, NumAreas, osdFactorX, osdFactorY, AreaCombined, AreaOsd);
  bitmaps->Add(Bitmaps);
  for (int i = 0; i < NumAreas; i++) {
      if (cSubtitleRegionRef *srr = Page->GetRegionRefByIndex(i)) {
         if (cSubtitleRegion *sr = Page->GetRegionById(srr->RegionId())) {
            if (cSubtitleClut *clut = Page->GetClutById(sr->ClutId())) {
               cBitmap *bm = new cBitmap(sr->RegionWidth(), sr->RegionHeight(), sr->RegionDepth());
               bm->Replace(*clut->GetPalette(sr->RegionDepth()));
               sr->Render(bm, Page->Objects());
               if (Reduced) {
                  if (sr->RegionDepth() != Areas[i].bpp) {
                     if (sr->RegionLevelOfCompatibility() <= Areas[i].bpp) {
                        //TODO this is untested - didn't have any such subtitle stream
                        cSubtitleClut *Clut = Page->GetClutById(sr->ClutId());
                        dbgregions("reduce region %d bpp %d level %d area bpp %d<br>\n", sr->RegionId(), sr->RegionDepth(), sr->RegionLevelOfCompatibility(), Areas[i].bpp);
                        bm->ReduceBpp(*Clut->GetPalette(sr->RegionDepth()));
                        }
                     else {
                        dbgregions("condense region %d bpp %d level %d area bpp %d<br>\n", sr->RegionId(), sr->RegionDepth(), sr->RegionLevelOfCompatibility(), Areas[i].bpp);
                        bm->ShrinkBpp(Areas[i].bpp);
                        }
                     }
                  }
               bm->SetOffset(srr->RegionHorizontalAddress(), srr->RegionVerticalAddress());
               Bitmaps->AddBitmap(bm);
               }
            }
         }
      }
  if (DebugPages)
     Bitmaps->DbgDump(windowWidth, windowHeight);
}