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
|
/*
* dvbsubtitle.c: DVB subtitles
*
* See the main source file 'vdr.c' for copyright information and
* how to reach the author.
*
* Original author: Marco Schlüßler <marco@lordzodiac.de>
* With some input from the "subtitle plugin" by Pekka Virtanen <pekka.virtanen@sci.fi>
*
* $Id: dvbsubtitle.c 1.1 2007/10/14 14:02:35 kls Exp $
*/
#include "dvbsubtitle.h"
#include "device.h"
#define PAGE_COMPOSITION_SEGMENT 0x10
#define REGION_COMPOSITION_SEGMENT 0x11
#define CLUT_DEFINITION_SEGMENT 0x12
#define OBJECT_DATA_SEGMENT 0x13
#define END_OF_DISPLAY_SET_SEGMENT 0x80
// Set these to 'true' for debug output:
static bool DebugConverter = false;
static bool DebugSegments = false;
static bool DebugPages = false;
static bool DebugRegions = false;
static bool DebugObjects = false;
static bool DebugCluts = false;
#define dbgconverter(a...) if (DebugConverter) fprintf(stderr, a)
#define dbgsegments(a...) if (DebugSegments) fprintf(stderr, a)
#define dbgpages(a...) if (DebugPages) fprintf(stderr, a)
#define dbgregions(a...) if (DebugRegions) fprintf(stderr, a)
#define dbgobjects(a...) if (DebugObjects) fprintf(stderr, a)
#define dbgcluts(a...) if (DebugCluts) fprintf(stderr, a)
// --- cSubtitleClut ---------------------------------------------------------
class cSubtitleClut : public cListObject {
private:
int clutId;
int version;
cPalette palette2;
cPalette palette4;
cPalette palette8;
public:
cSubtitleClut(int ClutId);
int ClutId(void) { return clutId; }
int Version(void) { return version; }
void SetVersion(int Version) { version = Version; }
void SetColor(int Bpp, int Index, tColor Color);
const cPalette *GetPalette(int Bpp);
};
cSubtitleClut::cSubtitleClut(int ClutId)
:palette2(2)
,palette4(4)
,palette8(8)
{
clutId = ClutId;
version = -1;
}
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 version;
int codingMethod;
bool nonModifyingColorFlag;
int nibblePos;
uchar backgroundColor;
uchar foregroundColor;
int providerFlag;
int px;
int py;
cBitmap *bitmap;
void DrawLine(int x, int y, tIndex Index, int Length);
uchar Get2Bits(const uchar *Data, int &Index);
uchar Get4Bits(const uchar *Data, int &Index);
bool Decode2BppCodeString(const uchar *Data, int &Index, int&x, int y);
bool Decode4BppCodeString(const uchar *Data, int &Index, int&x, int y);
bool Decode8BppCodeString(const uchar *Data, int &Index, int&y, int y);
public:
cSubtitleObject(int ObjectId, cBitmap *Bitmap);
int ObjectId(void) { return objectId; }
int Version(void) { return version; }
int CodingMethod(void) { return codingMethod; }
bool NonModifyingColorFlag(void) { return nonModifyingColorFlag; }
void DecodeSubBlock(const uchar *Data, int Length, bool Even);
void SetVersion(int Version) { version = Version; }
void SetBackgroundColor(uchar BackgroundColor) { backgroundColor = BackgroundColor; }
void SetForegroundColor(uchar ForegroundColor) { foregroundColor = ForegroundColor; }
void SetNonModifyingColorFlag(bool NonModifyingColorFlag) { nonModifyingColorFlag = NonModifyingColorFlag; }
void SetCodingMethod(int CodingMethod) { codingMethod = CodingMethod; }
void SetPosition(int x, int y) { px = x; py = y; }
void SetProviderFlag(int ProviderFlag) { providerFlag = ProviderFlag; }
};
cSubtitleObject::cSubtitleObject(int ObjectId, cBitmap *Bitmap)
{
objectId = ObjectId;
version = -1;
codingMethod = -1;
nonModifyingColorFlag = false;
nibblePos = 0;
backgroundColor = 0;
foregroundColor = 0;
providerFlag = -1;
px = py = 0;
bitmap = Bitmap;
}
void cSubtitleObject::DecodeSubBlock(const uchar *Data, int Length, bool Even)
{
int x = 0;
int y = Even ? 0 : 1;
for (int index = 0; index < Length; ) {
switch (Data[index++]) {
case 0x10: {
nibblePos = 8;
while (Decode2BppCodeString(Data, index, x, y) && index < Length)
;
if (!nibblePos)
index++;
break;
}
case 0x11: {
nibblePos = 4;
while (Decode4BppCodeString(Data, index, x, y) && index < Length)
;
if (!nibblePos)
index++;
break;
}
case 0x12:
while (Decode8BppCodeString(Data, index, x, y) && index < Length)
;
break;
case 0x20: //TODO
dbgobjects("sub block 2 to 4 map");
index += 4;
break;
case 0x21: //TODO
dbgobjects("sub block 2 to 8 map");
index += 4;
break;
case 0x22: //TODO
dbgobjects("sub block 4 to 8 map");
index += 16;
break;
case 0xF0:
x = 0;
y += 2;
break;
}
}
}
void cSubtitleObject::DrawLine(int x, int y, tIndex Index, int Length)
{
if (nonModifyingColorFlag && Index == 1)
return;
x += px;
y += py;
for (int pos = x; pos < x + Length; pos++)
bitmap->SetIndex(pos, y, Index);
}
uchar cSubtitleObject::Get2Bits(const uchar *Data, int &Index)
{
uchar result = Data[Index];
if (!nibblePos) {
Index++;
nibblePos = 8;
}
nibblePos -= 2;
return (result >> nibblePos) & 0x03;
}
uchar cSubtitleObject::Get4Bits(const uchar *Data, int &Index)
{
uchar result = Data[Index];
if (!nibblePos) {
Index++;
nibblePos = 4;
}
else {
result >>= 4;
nibblePos -= 4;
}
return result & 0x0F;
}
bool cSubtitleObject::Decode2BppCodeString(const uchar *Data, int &Index, int &x, int y)
{
int rl = 0;
int color = 0;
uchar code = Get2Bits(Data, Index);
if (code) {
color = code;
rl = 1;
}
else {
code = Get2Bits(Data, Index);
if (code & 2) { // switch_1
rl = ((code & 1) << 2) + Get2Bits(Data, Index) + 3;
color = Get2Bits(Data, Index);
}
else if (code & 1)
rl = 1; //color 0
else {
code = Get2Bits(Data, Index);
switch (code & 0x3) { //switch_3
case 0:
return false;
case 1:
rl = 2; //color 0
break;
case 2:
rl = (Get2Bits(Data, Index) << 2) + Get2Bits(Data, Index) + 12;
color = Get2Bits(Data, Index);
break;
case 3:
rl = (Get2Bits(Data, Index) << 6) + (Get2Bits(Data, Index) << 4) + (Get2Bits(Data, Index) << 2) + Get2Bits(Data, Index) + 29;
color = Get2Bits(Data, Index);
break;
}
}
}
DrawLine(x, y, color, rl);
x += rl;
return true;
}
bool cSubtitleObject::Decode4BppCodeString(const uchar *Data, int &Index, int &x, int y)
{
int rl = 0;
int color = 0;
uchar code = Get4Bits(Data, Index);
if (code) {
color = code;
rl = 1;
}
else {
code = Get4Bits(Data, Index);
if (code & 8) { // switch_1
if (code & 4) { //switch_2
switch (code & 3) { //switch_3
case 0: // color 0
rl = 1;
break;
case 1: // color 0
rl = 2;
break;
case 2:
rl = Get4Bits(Data, Index) + 9;
color = Get4Bits(Data, Index);
break;
case 3:
rl = (Get4Bits(Data, Index) << 4) + Get4Bits(Data, Index) + 25;
color = Get4Bits(Data, Index);
break;
}
}
else {
rl = (code & 3) + 4;
color = Get4Bits(Data, Index);
}
}
else { // color 0
if (!code)
return false;
rl = code + 2;
}
}
DrawLine(x, y, color, rl);
x += rl;
return true;
}
bool cSubtitleObject::Decode8BppCodeString(const uchar *Data, int &Index, int &x, int y)
{
int rl = 0;
int color = 0;
uchar code = Data[Index++];
if (code) {
color = code;
rl = 1;
}
else {
code = Data[Index++];
rl = code & 0x63;
if (code & 0x80)
color = Data[Index++];
else if (!rl)
return false; //else color 0
}
DrawLine(x, y, color, rl);
x += rl;
return true;
}
// --- cSubtitleRegion -------------------------------------------------------
class cSubtitleRegion : public cListObject, public cBitmap {
private:
int regionId;
int version;
int clutId;
int horizontalAddress;
int verticalAddress;
int level;
cList<cSubtitleObject> objects;
public:
cSubtitleRegion(int RegionId);
int RegionId(void) { return regionId; }
int Version(void) { return version; }
int ClutId(void) { return clutId; }
int Level(void) { return level; }
int Depth(void) { return Bpp(); }
void FillRegion(tIndex Index);
cSubtitleObject *GetObjectById(int ObjectId, bool New = false);
int HorizontalAddress(void) { return horizontalAddress; }
int VerticalAddress(void) { return verticalAddress; }
void SetVersion(int Version) { version = Version; }
void SetClutId(int ClutId) { clutId = ClutId; }
void SetLevel(int Level);
void SetDepth(int Depth);
void SetHorizontalAddress(int HorizontalAddress) { horizontalAddress = HorizontalAddress; }
void SetVerticalAddress(int VerticalAddress) { verticalAddress = VerticalAddress; }
};
cSubtitleRegion::cSubtitleRegion(int RegionId)
:cBitmap(1, 1, 4)
{
regionId = RegionId;
version = -1;
clutId = -1;
horizontalAddress = 0;
verticalAddress = 0;
level = 0;
}
void cSubtitleRegion::FillRegion(tIndex Index)
{
dbgregions("FillRegion %d\n", Index);
for (int y = 0; y < Height(); y++) {
for (int x = 0; x < Width(); x++)
SetIndex(x, y, Index);
}
}
cSubtitleObject *cSubtitleRegion::GetObjectById(int ObjectId, bool New)
{
cSubtitleObject *result = NULL;
for (cSubtitleObject *so = objects.First(); so; so = objects.Next(so)) {
if (so->ObjectId() == ObjectId)
result = so;
}
if (!result && New) {
result = new cSubtitleObject(ObjectId, this);
objects.Add(result);
}
return result;
}
void cSubtitleRegion::SetLevel(int Level)
{
if (Level > 0 && Level < 4)
level = 1 << Level;
}
void cSubtitleRegion::SetDepth(int Depth)
{
if (Depth > 0 && Depth < 4)
SetBpp(1 << Depth);
}
// --- cDvbSubtitlePage ------------------------------------------------------
class cDvbSubtitlePage : public cListObject {
private:
int pageId;
int version;
int state;
int64_t pts;
int timeout;
cList<cSubtitleClut> cluts;
public:
cList<cSubtitleRegion> regions;
cDvbSubtitlePage(int PageId);
virtual ~cDvbSubtitlePage();
int PageId(void) { return pageId; }
int Version(void) { return version; }
int State(void) { return state; }
tArea *GetAreas(void);
cSubtitleClut *GetClutById(int ClutId, bool New = false);
cSubtitleObject *GetObjectById(int ObjectId);
cSubtitleRegion *GetRegionById(int RegionId, bool New = false);
int64_t Pts(void) const { return pts; }
int Timeout(void) { return timeout; }
void SetVersion(int Version) { version = Version; }
void SetPts(int64_t Pts) { pts = Pts; }
void SetState(int State);
void SetTimeout(int Timeout) { timeout = Timeout; }
void UpdateRegionPalette(cSubtitleClut *Clut);
};
cDvbSubtitlePage::cDvbSubtitlePage(int PageId)
{
pageId = PageId;
version = -1;
state = -1;
pts = 0;
timeout = 0;
}
cDvbSubtitlePage::~cDvbSubtitlePage()
{
}
tArea *cDvbSubtitlePage::GetAreas(void)
{
if (regions.Count() > 0) {
tArea *Areas = new tArea[regions.Count()];
tArea *a = Areas;
for (cSubtitleRegion *sr = regions.First(); sr; sr = regions.Next(sr)) {
a->x1 = sr->HorizontalAddress();
a->y1 = sr->VerticalAddress();
a->x2 = sr->HorizontalAddress() + sr->Width() - 1;
a->y2 = sr->VerticalAddress() + sr->Height() - 1;
a->bpp = sr->Bpp();
while ((a->Width() & 3) != 0)
a->x2++; // aligns width to a multiple of 4, so 2, 4 and 8 bpp will work
a++;
}
return Areas;
}
return NULL;
}
cSubtitleClut *cDvbSubtitlePage::GetClutById(int ClutId, bool New)
{
cSubtitleClut *result = NULL;
for (cSubtitleClut *sc = cluts.First(); sc; sc = cluts.Next(sc)) {
if (sc->ClutId() == ClutId)
result = sc;
}
if (!result && New) {
result = new cSubtitleClut(ClutId);
cluts.Add(result);
}
return result;
}
cSubtitleRegion *cDvbSubtitlePage::GetRegionById(int RegionId, bool New)
{
cSubtitleRegion *result = NULL;
for (cSubtitleRegion *sr = regions.First(); sr; sr = regions.Next(sr)) {
if (sr->RegionId() == RegionId)
result = sr;
}
if (!result && New) {
result = new cSubtitleRegion(RegionId);
regions.Add(result);
}
return result;
}
cSubtitleObject *cDvbSubtitlePage::GetObjectById(int ObjectId)
{
cSubtitleObject *result = NULL;
for (cSubtitleRegion *sr = regions.First(); sr && !result; sr = regions.Next(sr))
result = sr->GetObjectById(ObjectId);
return result;
}
void cDvbSubtitlePage::SetState(int State)
{
state = State;
switch (state) {
case 0: // normal case - page update
dbgpages("page update\n");
break;
case 1: // aquisition point - page refresh
dbgpages("page refresh\n");
regions.Clear();
break;
case 2: // mode change - new page
dbgpages("new Page\n");
regions.Clear();
cluts.Clear();
break;
case 3: // reserved
break;
}
}
void cDvbSubtitlePage::UpdateRegionPalette(cSubtitleClut *Clut)
{
for (cSubtitleRegion *sr = regions.First(); sr; sr = regions.Next(sr)) {
if (sr->ClutId() == Clut->ClutId())
sr->Replace(*Clut->GetPalette(sr->Bpp()));
}
}
// --- 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);
data = (uchar *)realloc(data, size);
if (!data) {
esyslog("ERROR: can't allocate memory for subtitle assembler");
length = 0;
size = 0;
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:
int64_t pts;
int timeout;
tArea *areas;
int numAreas;
cVector<cBitmap *> bitmaps;
public:
cDvbSubtitleBitmaps(int64_t Pts, int Timeout, tArea *Areas, int NumAreas);
~cDvbSubtitleBitmaps();
int64_t Pts(void) { return pts; }
int Timeout(void) { return timeout; }
void AddBitmap(cBitmap *Bitmap);
void Draw(cOsd *Osd);
};
cDvbSubtitleBitmaps::cDvbSubtitleBitmaps(int64_t Pts, int Timeout, tArea *Areas, int NumAreas)
{
pts = Pts;
timeout = Timeout;
areas = Areas;
numAreas = NumAreas;
}
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)
{
if (Osd->SetAreas(areas, numAreas) == oeOk) {
for (int i = 0; i < bitmaps.Size(); i++)
Osd->DrawBitmap(bitmaps[i]->X0(), bitmaps[i]->Y0(), *bitmaps[i]);
Osd->Flush();
}
}
// --- cDvbSubtitleConverter -------------------------------------------------
int cDvbSubtitleConverter::setupLevel = 0;
cDvbSubtitleConverter::cDvbSubtitleConverter(void)
:cThread("subtitleConverter")
{
dvbSubtitleAssembler = new cDvbSubtitleAssembler;
osd = NULL;
pages = new cList<cDvbSubtitlePage>;
bitmaps = new cList<cDvbSubtitleBitmaps>;
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 -----------------------\n");
dvbSubtitleAssembler->Reset();
Lock();
pages->Clear();
bitmaps->Clear();
DELETENULL(osd);
Unlock();
}
int cDvbSubtitleConverter::Convert(const uchar *Data, int Length)
{
if (Data && Length > 8) {
int PayloadOffset = Data[8] + 9;
int SubstreamHeaderLength = 4;
bool ResetSubtitleAssembler = Data[PayloadOffset + 3] == 0x00;
// Compatibility mode for old subtitles plugin:
if ((Data[PayloadOffset - 3] & 0x81) == 1 && Data[PayloadOffset - 2] == 0x81) {
PayloadOffset--;
SubstreamHeaderLength = 1;
ResetSubtitleAssembler = Data[8] >= 5;
}
if (Length > PayloadOffset + SubstreamHeaderLength) {
int64_t pts = 0;
if ((Data[7] & 0x80) && Data[8] >= 5) {
pts = (((int64_t)Data[ 9]) & 0x0E) << 29;
pts |= ( (int64_t)Data[10]) << 22;
pts |= (((int64_t)Data[11]) & 0xFE) << 14;
pts |= ( (int64_t)Data[12]) << 7;
pts |= (((int64_t)Data[13]) & 0xFE) >> 1;
dbgconverter("Converter PTS: %lld\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;
}
#define LimitTo32Bit(n) (n & 0x00000000FFFFFFFFL)
#define MAXDELTA 40000 // max. reasonable PTS/STC delta in ms
void cDvbSubtitleConverter::Action(void)
{
int LastSetupLevel = setupLevel;
cTimeMs Timeout;
while (Running()) {
if (osd) {
int NewSetupLevel = setupLevel;
if (Timeout.TimedOut() || LastSetupLevel != NewSetupLevel) {
DELETENULL(osd);
}
LastSetupLevel = NewSetupLevel;
}
int WaitMs = 100;
Lock();
if (cDvbSubtitleBitmaps *sb = bitmaps->First()) {
int64_t STC = cDevice::PrimaryDevice()->GetSTC();
int64_t Delta = 0;
if (STC >= 0) {
Delta = LimitTo32Bit(sb->Pts()) - LimitTo32Bit(STC); // 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);
}
else {
//TODO sync on PTS? are there actually devices that don't deliver an STC?
}
Delta /= 90; // STC and PTS are in 1/90000s
if (abs(Delta) <= MAXDELTA) {
if (Delta <= 0) {
dbgconverter("Got %d bitmaps, showing #%d\n", bitmaps->Count(), sb->Index() + 1);
if (AssertOsd()) {
sb->Draw(osd);
Timeout.Set(sb->Timeout() * 1000);
dbgconverter("PTS: %lld STC: %lld (%lld) timeout: %d\n", sb->Pts(), cDevice::PrimaryDevice()->GetSTC(), Delta, sb->Timeout());
}
bitmaps->Del(sb);
}
else
WaitMs = min(max(Delta, int64_t(0)), int64_t(1000));
}
else
bitmaps->Del(sb);
}
Unlock();
cCondWait::SleepMs(WaitMs);
}
}
tColor cDvbSubtitleConverter::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 = max(min(((298 * Ey + 460 * Epr) / 256), 255), 0);
Eg = max(min(((298 * Ey - 55 * Epb - 137 * Epr) / 256), 255), 0);
Eb = max(min(((298 * Ey + 543 * Epb ) / 256), 255), 0);
return (Er << 16) | (Eg << 8) | Eb;
}
bool cDvbSubtitleConverter::AssertOsd(void)
{
return osd || (osd = cOsdProvider::NewOsd(0, Setup.SubtitleOffset, OSD_LEVEL_SUBTITLES));
}
int cDvbSubtitleConverter::ExtractSegment(const uchar *Data, int Length, int64_t Pts)
{
if (Length > 5 && Data[0] == 0x0F) {
int segmentLength = (Data[4] << 8) + Data[5] + 6;
if (segmentLength > Length)
return -1;
int segmentType = Data[1];
int pageId = (Data[2] << 8) + Data[3];
cDvbSubtitlePage *page = NULL;
LOCK_THREAD;
for (cDvbSubtitlePage *sp = pages->First(); sp; sp = pages->Next(sp)) {
if (sp->PageId() == pageId) {
page = sp;
break;
}
}
if (!page) {
page = new cDvbSubtitlePage(pageId);
pages->Add(page);
dbgpages("Create SubtitlePage %d (total pages = %d)\n", pageId, pages->Count());
}
switch (segmentType) {
case PAGE_COMPOSITION_SEGMENT: {
dbgsegments("PAGE_COMPOSITION_SEGMENT\n");
int pageVersion = (Data[6 + 1] & 0xF0) >> 4;
if (pageVersion == page->Version())
break; // no update
page->SetVersion(pageVersion);
if (Pts)
page->SetPts(Pts);
page->SetTimeout(Data[6]);
page->SetState((Data[6 + 1] & 0x0C) >> 2);
page->regions.Clear();
dbgpages("Update page id %d version %d pts %lld timeout %d state %d\n", pageId, page->Version(), page->Pts(), page->Timeout(), page->State());
for (int i = 6 + 2; i < segmentLength; i += 6) {
cSubtitleRegion *region = page->GetRegionById(Data[i], true);
region->SetHorizontalAddress((Data[i + 2] << 8) + Data[i + 3]);
region->SetVerticalAddress((Data[i + 4] << 8) + Data[i + 5]);
}
break;
}
case REGION_COMPOSITION_SEGMENT: {
dbgsegments("REGION_COMPOSITION_SEGMENT\n");
cSubtitleRegion *region = page->GetRegionById(Data[6]);
if (!region)
break;
int regionVersion = (Data[6 + 1] & 0xF0) >> 4;
if (regionVersion == region->Version())
break; // no update
region->SetVersion(regionVersion);
bool regionFillFlag = (Data[6 + 1] & 0x08) >> 3;
int regionWidth = (Data[6 + 2] << 8) | Data[6 + 3];
int regionHeight = (Data[6 + 4] << 8) | Data[6 + 5];
region->SetSize(regionWidth, regionHeight);
region->SetLevel((Data[6 + 6] & 0xE0) >> 5);
region->SetDepth((Data[6 + 6] & 0x1C) >> 2);
region->SetClutId(Data[6 + 7]);
dbgregions("Region pageId %d id %d version %d fill %d width %d height %d level %d depth %d clutId %d\n", pageId, region->RegionId(), region->Version(), regionFillFlag, regionWidth, regionHeight, region->Level(), region->Depth(), region->ClutId());
if (regionFillFlag) {
switch (region->Bpp()) {
case 2: region->FillRegion((Data[6 + 9] & 0x0C) >> 2); break;
case 4: region->FillRegion((Data[6 + 9] & 0xF0) >> 4); break;
case 8: region->FillRegion(Data[6 + 8]); break;
}
}
for (int i = 6 + 10; i < segmentLength; i += 6) {
cSubtitleObject *object = region->GetObjectById((Data[i] << 8) | Data[i + 1], true);
int objectType = (Data[i + 2] & 0xC0) >> 6;
object->SetCodingMethod(objectType);
object->SetProviderFlag((Data[i + 2] & 0x30) >> 4);
int objectHorizontalPosition = ((Data[i + 2] & 0x0F) << 8) | Data[i + 3];
int objectVerticalPosition = ((Data[i + 4] & 0x0F) << 8) | Data[i + 5];
object->SetPosition(objectHorizontalPosition, objectVerticalPosition);
if (objectType == 0x01 || objectType == 0x02) {
object->SetForegroundColor(Data[i + 6]);
object->SetBackgroundColor(Data[i + 7]);
i += 2;
}
}
break;
}
case CLUT_DEFINITION_SEGMENT: {
dbgsegments("CLUT_DEFINITION_SEGMENT\n");
cSubtitleClut *clut = page->GetClutById(Data[6], true);
int clutVersion = (Data[6 + 1] & 0xF0) >> 4;
if (clutVersion == clut->Version())
break; // no update
clut->SetVersion(clutVersion);
dbgcluts("Clut pageId %d id %d version %d\n", pageId, clut->ClutId(), clut->Version());
for (int i = 6 + 2; i < segmentLength; i += 2) {
uchar clutEntryId = Data[i];
bool fullRangeFlag = Data[i + 1] & 1;
uchar yval;
uchar crval;
uchar cbval;
uchar tval;
if (fullRangeFlag) {
yval = Data[i + 2];
crval = Data[i + 3];
cbval = Data[i + 4];
tval = Data[i + 5];
}
else {
yval = Data[i + 2] & 0xFC;
crval = (Data[i + 2] & 0x03) << 6;
crval |= (Data[i + 3] & 0xC0) >> 2;
cbval = (Data[i + 3] & 0x3C) << 2;
tval = (Data[i + 3] & 0x03) << 6;
}
tColor value = 0;
if (yval) {
value = yuv2rgb(yval, cbval, crval);
value |= ((10 - (clutEntryId ? Setup.SubtitleFgTransparency : Setup.SubtitleBgTransparency)) * (255 - tval) / 10) << 24;
}
int EntryFlags = Data[i + 1];
dbgcluts("%2d %d %d %d %08X\n", clutEntryId, (EntryFlags & 0x80) ? 2 : 0, (EntryFlags & 0x40) ? 4 : 0, (EntryFlags & 0x20) ? 8 : 0, value);
if ((EntryFlags & 0x80) != 0)
clut->SetColor(2, clutEntryId, value);
if ((EntryFlags & 0x40) != 0)
clut->SetColor(4, clutEntryId, value);
if ((EntryFlags & 0x20) != 0)
clut->SetColor(8, clutEntryId, value);
i += fullRangeFlag ? 4 : 2;
}
dbgcluts("\n");
page->UpdateRegionPalette(clut);
break;
}
case OBJECT_DATA_SEGMENT: {
dbgsegments("OBJECT_DATA_SEGMENT\n");
cSubtitleObject *object = page->GetObjectById((Data[6] << 8) | Data[6 + 1]);
if (!object)
break;
int objectVersion = (Data[6 + 2] & 0xF0) >> 4;
if (objectVersion == object->Version())
break; // no update
object->SetVersion(objectVersion);
int codingMethod = (Data[6 + 2] & 0x0C) >> 2;
object->SetNonModifyingColorFlag(Data[6 + 2] & 0x01);
dbgobjects("Object pageId %d id %d version %d method %d modify %d\n", pageId, object->ObjectId(), object->Version(), object->CodingMethod(), object->NonModifyingColorFlag());
if (codingMethod == 0) { // coding of pixels
int i = 6 + 3;
int topFieldLength = (Data[i] << 8) | Data[i + 1];
int bottomFieldLength = (Data[i + 2] << 8) | Data[i + 3];
object->DecodeSubBlock(Data + i + 4, topFieldLength, true);
if (bottomFieldLength)
object->DecodeSubBlock(Data + i + 4 + topFieldLength, bottomFieldLength, false);
else
object->DecodeSubBlock(Data + i + 4, topFieldLength, false);
}
else if (codingMethod == 1) { // coded as a string of characters
//TODO implement textual subtitles
}
break;
}
case END_OF_DISPLAY_SET_SEGMENT: {
dbgsegments("END_OF_DISPLAY_SET_SEGMENT\n");
FinishPage(page);
}
default:
dbgsegments("*** unknown segment type: %02X\n", segmentType);
}
return segmentLength;
}
return -1;
}
void cDvbSubtitleConverter::FinishPage(cDvbSubtitlePage *Page)
{
if (!AssertOsd())
return;
tArea *Areas = Page->GetAreas();
int NumAreas = Page->regions.Count();
int Bpp = 8;
bool Reduced = false;
while (osd->CanHandleAreas(Areas, NumAreas) != oeOk) {
int HalfBpp = Bpp / 2;
if (HalfBpp >= 2) {
for (int i = 0; i < NumAreas; i++) {
if (Areas[i].bpp >= Bpp) {
Areas[i].bpp = HalfBpp;
Reduced = true;
}
}
Bpp = HalfBpp;
}
else
return; // unable to draw bitmaps
}
if (Reduced) {
for (int i = 0; i < NumAreas; i++) {
cSubtitleRegion *sr = Page->regions.Get(i);
if (sr->Bpp() != Areas[i].bpp) {
if (sr->Level() <= Areas[i].bpp) {
//TODO this is untested - didn't have any such subtitle stream
cSubtitleClut *Clut = Page->GetClutById(sr->ClutId());
if (Clut) {
dbgregions("reduce region %d bpp %d level %d area bpp %d\n", sr->RegionId(), sr->Bpp(), sr->Level(), Areas[i].bpp);
sr->ReduceBpp(*Clut->GetPalette(sr->Bpp()));
}
}
else {
dbgregions("condense region %d bpp %d level %d area bpp %d\n", sr->RegionId(), sr->Bpp(), sr->Level(), Areas[i].bpp);
sr->ShrinkBpp(Areas[i].bpp);
}
}
}
}
cDvbSubtitleBitmaps *Bitmaps = new cDvbSubtitleBitmaps(Page->Pts(), Page->Timeout(), Areas, NumAreas);
bitmaps->Add(Bitmaps);
for (cSubtitleRegion *sr = Page->regions.First(); sr; sr = Page->regions.Next(sr)) {
int posX = sr->HorizontalAddress();
int posY = sr->VerticalAddress();
cBitmap *bm = new cBitmap(sr->Width(), sr->Height(), sr->Bpp(), posX, posY);
bm->DrawBitmap(posX, posY, *sr);
Bitmaps->AddBitmap(bm);
}
}
|