-
Notifications
You must be signed in to change notification settings - Fork 7
/
dvbsubtitle.c
1825 lines (1722 loc) · 60.6 KB
/
dvbsubtitle.c
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
/*
* dvbsubtitle.c: DVB subtitles
*
* See the main source file 'vdr.c' for copyright information and
* how to reach the author.
*
* Original author: Marco Schluessler <[email protected]>
* With some input from the "subtitles plugin" by Pekka Virtanen <[email protected]>
*
* $Id: dvbsubtitle.c 5.2 2022/12/06 16:57:01 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);