/
telxcc.c
1616 lines (1460 loc) · 57.2 KB
/
telxcc.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
/*!
(c) 2011-2013 Forers, s. r. o.: telxcc
telxcc conforms to ETSI 300 706 Presentation Level 1.5: Presentation Level 1 defines the basic Teletext page,
characterised by the use of spacing attributes only and a limited alphanumeric and mosaics repertoire.
Presentation Level 1.5 decoder responds as Level 1 but the character repertoire is extended via packets X/26.
Selection of national option sub-sets related features from Presentation Level 2.5 feature set have been implemented, too.
(X/28/0 Format 1, X/28/4, M/29/0 and M/29/4 packets)
Further documentation:
ETSI TS 101 154 V1.9.1 (2009-09), Technical Specification
Digital Video Broadcasting (DVB); Specification for the use of Video and Audio Coding in Broadcasting Applications based on the MPEG-2 Transport Stream
ETSI EN 300 231 V1.3.1 (2003-04), European Standard (Telecommunications series)
Television systems; Specification of the domestic video Programme Delivery Control system (PDC)
ETSI EN 300 472 V1.3.1 (2003-05), European Standard (Telecommunications series)
Digital Video Broadcasting (DVB); Specification for conveying ITU-R System B Teletext in DVB bitstreams
ETSI EN 301 775 V1.2.1 (2003-05), European Standard (Telecommunications series)
Digital Video Broadcasting (DVB); Specification for the carriage of Vertical Blanking Information (VBI) data in DVB bitstreams
ETS 300 706 (May 1997)
Enhanced Teletext Specification
ETS 300 708 (March 1997)
Television systems; Data transmission within Teletext
ISO/IEC STANDARD 13818-1 Second edition (2000-12-01)
Information technology — Generic coding of moving pictures and associated audio information: Systems
ISO/IEC STANDARD 6937 Third edition (2001-12-15)
Information technology — Coded graphic character set for text communication — Latin alphabet
Werner Brückner -- Teletext in digital television
*/
#include "lib_ccx.h"
#include "ccx_common_option.h"
#include "hamming.h"
#include "teletext.h"
#include <signal.h>
#include "activity.h"
#include "ccx_encoders_helpers.h"
#ifdef __MINGW32__
// switch stdin and all normal files into binary mode -- needed for Windows
#include <fcntl.h>
int _CRT_fmode = _O_BINARY;
// for better UX in Windows we want to detect that app is not run by "double-clicking" in Explorer
#define WIN32_LEAN_AND_MEAN
#define _WIN32_WINNT 0x0502
#define _WIN32_IE 0x0400
#include <windows.h>
#include <commctrl.h>
#endif
uint64_t last_pes_pts = 0; // PTS of last PES packet (debug purposes)
static int de_ctr = 0; // a keeps count of packets with flag subtitle ON and data packets
static const char* TTXT_COLOURS[8] = {
//black, red, green, yellow, blue, magenta, cyan, white
"#000000", "#ff0000", "#00ff00", "#ffff00", "#0000ff", "#ff00ff", "#00ffff", "#ffffff"
};
// 1-byte alignment; just to be sure, this struct is being used for explicit type conversion
// FIXME: remove explicit type conversion from buffer to structs
#pragma pack(push)
#pragma pack(1)
typedef struct {
uint8_t _clock_in; // clock run in
uint8_t _framing_code; // framing code, not needed, ETSI 300 706: const 0xe4
uint8_t address[2];
uint8_t data[40];
} teletext_packet_payload_t;
#pragma pack(pop)
// application config global variable
struct ccx_s_teletext_config tlt_config = { 0};
// macro -- output only when increased verbosity was turned on
#define VERBOSE_ONLY if (tlt_config.verbose == YES)
// current charset (charset can be -- and always is -- changed during transmission)
struct s_primary_charset {
uint8_t current;
uint8_t g0_m29;
uint8_t g0_x28;
} primary_charset = {
0x00, UNDEFINED, UNDEFINED
};
// entities, used in colour mode, to replace unsafe HTML tag chars
struct {
uint16_t character;
const char *entity;
} const ENTITIES[] = {
{ '<', "<" },
{ '>', ">" },
{ '&', "&" }
};
// Latin-Russian characters mapping, issue #1086
struct {
uint16_t lat_char;
const char * rus_char;
} const LAT_RUS[] = {
{65, "А"}, {66, "Б"}, {87, "В"}, {71, "Г"},
{68, "Д"}, {69, "Е"}, {86, "Ж"}, {90, "З"},
{73, "И"}, {74, "Й"}, {75, "К"}, {76, "Л"},
{77, "М"}, {78, "Н"}, {79, "О"}, {80, "П"},
{82, "Р"}, {83, "С"}, {84, "Т"}, {85, "У"},
{70, "Ф"}, {72, "Х"}, {67, "Ц"}, {238, "Ч"},
{235, "Ш"},{249, "Щ"},{35, "Ы"}, {88, "Ь"},
{234, "Э"},{224, "Ю"},{81, "Я"}, {97, "а"},
{98, "б"}, {119, "в"},{103, "г"},{100, "д"},
{101, "е"},{118, "ж"},{122, "з"},{105, "и"},
{106, "й"},{107, "к"},{108, "л"},{109, "м"},
{110, "н"},{111, "о"},{112, "п"},{114, "р"},
{115, "с"},{116, "т"},{117, "у"},{102, "ф"},
{104, "х"},{99, "ц"}, {231, "ч"},{226, "ш"},
{251, "щ"},{121, "ъ"},{38, "ы"}, {120, "ь"},
{244, "э"},{232, "ю"},{113, "я"}
};
#define array_length(a) (sizeof(a)/sizeof(a[0]))
// extracts magazine number from teletext page
#define MAGAZINE(p) ((p >> 8) & 0xf)
// extracts page number from teletext page
#define PAGE(p) (p & 0xff)
typedef enum
{
LATIN = 0,
CYRILLIC1,
CYRILLIC2,
CYRILLIC3,
GREEK,
ARABIC,
HEBREW
} g0_charsets_type;
g0_charsets_type default_g0_charset;
// Note: All characters are encoded in UCS-2
// --- G0 ----------------------------------------------------------------------
// G0 charsets
uint16_t G0[5][96] = {
{ // Latin G0 Primary Set
0x0020, 0x0021, 0x0022, 0x00a3, 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f,
0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f,
0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f,
0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059, 0x005a, 0x00ab, 0x00bd, 0x00bb, 0x005e, 0x0023,
0x002d, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 0x0068, 0x0069, 0x006a, 0x006b, 0x006c, 0x006d, 0x006e, 0x006f,
0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, 0x0078, 0x0079, 0x007a, 0x00bc, 0x00a6, 0x00be, 0x00f7, 0x007f
},
{ // Cyrillic G0 Primary Set - Option 1 - Serbian/Croatian
0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x044b, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f,
0x0030, 0x0031, 0x3200, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f,
0x0427, 0x0410, 0x0411, 0x0426, 0x0414, 0x0415, 0x0424, 0x0413, 0x0425, 0x0418, 0x0408, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e,
0x041f, 0x040c, 0x0420, 0x0421, 0x0422, 0x0423, 0x0412, 0x0403, 0x0409, 0x040a, 0x0417, 0x040b, 0x0416, 0x0402, 0x0428, 0x040f,
0x0447, 0x0430, 0x0431, 0x0446, 0x0434, 0x0435, 0x0444, 0x0433, 0x0445, 0x0438, 0x0428, 0x043a, 0x043b, 0x043c, 0x043d, 0x043e,
0x043f, 0x042c, 0x0440, 0x0441, 0x0442, 0x0443, 0x0432, 0x0423, 0x0429, 0x042a, 0x0437, 0x042b, 0x0436, 0x0422, 0x0448, 0x042f
},
{ // Cyrillic G0 Primary Set - Option 2 - Russian/Bulgarian
0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x044b, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f,
0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f,
0x042e, 0x0410, 0x0411, 0x0426, 0x0414, 0x0415, 0x0424, 0x0413, 0x0425, 0x0418, 0x0419, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e,
0x041f, 0x042f, 0x0420, 0x0421, 0x0422, 0x0423, 0x0416, 0x0412, 0x042c, 0x042a, 0x0417, 0x0428, 0x042d, 0x0429, 0x0427, 0x042b,
0x044e, 0x0430, 0x0431, 0x0446, 0x0434, 0x0435, 0x0444, 0x0433, 0x0445, 0x0438, 0x0439, 0x043a, 0x043b, 0x043c, 0x043d, 0x043e,
0x043f, 0x044f, 0x0440, 0x0441, 0x0442, 0x0443, 0x0436, 0x0432, 0x044c, 0x044a, 0x0437, 0x0448, 0x044d, 0x0449, 0x0447, 0x044b
},
{ // Cyrillic G0 Primary Set - Option 3 - Ukrainian
0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x00ef, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f,
0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f,
0x042e, 0x0410, 0x0411, 0x0426, 0x0414, 0x0415, 0x0424, 0x0413, 0x0425, 0x0418, 0x0419, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e,
0x041f, 0x042f, 0x0420, 0x0421, 0x0422, 0x0423, 0x0416, 0x0412, 0x042c, 0x0049, 0x0417, 0x0428, 0x042d, 0x0429, 0x0427, 0x00cf,
0x044e, 0x0430, 0x0431, 0x0446, 0x0434, 0x0435, 0x0444, 0x0433, 0x0445, 0x0438, 0x0439, 0x043a, 0x043b, 0x043c, 0x043d, 0x043e,
0x043f, 0x044f, 0x0440, 0x0441, 0x0442, 0x0443, 0x0436, 0x0432, 0x044c, 0x0069, 0x0437, 0x0448, 0x044d, 0x0449, 0x0447, 0x00ff
},
{ // Greek G0 Primary Set
0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f,
0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f,
0x0390, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, 0x0398, 0x0399, 0x039a, 0x039b, 0x039c, 0x039d, 0x039e, 0x039f,
0x03a0, 0x03a1, 0x03a2, 0x03a3, 0x03a4, 0x03a5, 0x03a6, 0x03a7, 0x03a8, 0x03a9, 0x03aa, 0x03ab, 0x03ac, 0x03ad, 0x03ae, 0x03af,
0x03b0, 0x03b1, 0x03b2, 0x03b3, 0x03b4, 0x03b5, 0x03b6, 0x03b7, 0x03b8, 0x03b9, 0x03ba, 0x03bb, 0x03bc, 0x03bd, 0x03be, 0x03bf,
0x03c0, 0x03c1, 0x03c2, 0x03c3, 0x03c4, 0x03c5, 0x03c6, 0x03c7, 0x03c8, 0x03c9, 0x03ca, 0x03cb, 0x03cc, 0x03cd, 0x03ce, 0x03cf
}
//{ // Arabic G0 Primary Set
//},
//{ // Hebrew G0 Primary Set
//}
};
// array positions where chars from G0_LATIN_NATIONAL_SUBSETS are injected into G0[LATIN]
const uint8_t G0_LATIN_NATIONAL_SUBSETS_POSITIONS[13] = {
0x03, 0x04, 0x20, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x5b, 0x5c, 0x5d, 0x5e
};
// ETS 300 706, chapter 15.2, table 32: Function of Default G0 and G2 Character Set Designation
// and National Option Selection bits in packets X/28/0 Format 1, X/28/4, M/29/0 and M/29/4
// Latin National Option Sub-sets
struct {
const char *language;
uint16_t characters[13];
} const G0_LATIN_NATIONAL_SUBSETS[14] = {
{ // 0
"English",
{ 0x00a3, 0x0024, 0x0040, 0x00ab, 0x00bd, 0x00bb, 0x005e, 0x0023, 0x002d, 0x00bc, 0x00a6, 0x00be, 0x00f7 }
},
{ // 1
"French",
{ 0x00e9, 0x00ef, 0x00e0, 0x00eb, 0x00ea, 0x00f9, 0x00ee, 0x0023, 0x00e8, 0x00e2, 0x00f4, 0x00fb, 0x00e7 }
},
{ // 2
"Swedish, Finnish, Hungarian",
{ 0x0023, 0x00a4, 0x00c9, 0x00c4, 0x00d6, 0x00c5, 0x00dc, 0x005f, 0x00e9, 0x00e4, 0x00f6, 0x00e5, 0x00fc }
},
{ // 3
"Czech, Slovak",
{ 0x0023, 0x016f, 0x010d, 0x0165, 0x017e, 0x00fd, 0x00ed, 0x0159, 0x00e9, 0x00e1, 0x011b, 0x00fa, 0x0161 }
},
{ // 4
"German",
{ 0x0023, 0x0024, 0x00a7, 0x00c4, 0x00d6, 0x00dc, 0x005e, 0x005f, 0x00b0, 0x00e4, 0x00f6, 0x00fc, 0x00df }
},
{ // 5
"Portuguese, Spanish",
{ 0x00e7, 0x0024, 0x00a1, 0x00e1, 0x00e9, 0x00ed, 0x00f3, 0x00fa, 0x00bf, 0x00fc, 0x00f1, 0x00e8, 0x00e0 }
},
{ // 6
"Italian",
{ 0x00a3, 0x0024, 0x00e9, 0x00b0, 0x00e7, 0x00bb, 0x005e, 0x0023, 0x00f9, 0x00e0, 0x00f2, 0x00e8, 0x00ec }
},
{ // 7
"Rumanian",
{ 0x0023, 0x00a4, 0x0162, 0x00c2, 0x015e, 0x0102, 0x00ce, 0x0131, 0x0163, 0x00e2, 0x015f, 0x0103, 0x00ee }
},
{ // 8
"Polish",
{ 0x0023, 0x0144, 0x0105, 0x017b, 0x015a, 0x0141, 0x0107, 0x00f3, 0x0119, 0x017c, 0x015b, 0x0142, 0x017a }
},
{ // 9
"Turkish",
{ 0x0054, 0x011f, 0x0130, 0x015e, 0x00d6, 0x00c7, 0x00dc, 0x011e, 0x0131, 0x015f, 0x00f6, 0x00e7, 0x00fc }
},
{ // a
"Serbian, Croatian, Slovenian",
{ 0x0023, 0x00cb, 0x010c, 0x0106, 0x017d, 0x0110, 0x0160, 0x00eb, 0x010d, 0x0107, 0x017e, 0x0111, 0x0161 }
},
{ // b
"Estonian",
{ 0x0023, 0x00f5, 0x0160, 0x00c4, 0x00d6, 0x017e, 0x00dc, 0x00d5, 0x0161, 0x00e4, 0x00f6, 0x017e, 0x00fc }
},
{ // c
"Lettish, Lithuanian",
{ 0x0023, 0x0024, 0x0160, 0x0117, 0x0119, 0x017d, 0x010d, 0x016b, 0x0161, 0x0105, 0x0173, 0x017e, 0x012f }
}
};
// References to the G0_LATIN_NATIONAL_SUBSETS array
const uint8_t G0_LATIN_NATIONAL_SUBSETS_MAP[56] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x01, 0x02, 0x03, 0x04, 0xff, 0x06, 0xff,
0x00, 0x01, 0x02, 0x09, 0x04, 0x05, 0x06, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0x0a, 0xff, 0x07,
0xff, 0xff, 0x0b, 0x03, 0x04, 0xff, 0x0c, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0x09, 0xff, 0xff, 0xff, 0xff
};
// --- G2 ----------------------------------------------------------------------
const uint16_t G2[1][96] = {
{ // Latin G2 Supplementary Set
0x0020, 0x00a1, 0x00a2, 0x00a3, 0x0024, 0x00a5, 0x0023, 0x00a7, 0x00a4, 0x2018, 0x201c, 0x00ab, 0x2190, 0x2191, 0x2192, 0x2193,
0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00d7, 0x00b5, 0x00b6, 0x00b7, 0x00f7, 0x2019, 0x201d, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00bf,
0x0020, 0x0300, 0x0301, 0x0302, 0x0303, 0x0304, 0x0306, 0x0307, 0x0308, 0x0000, 0x030a, 0x0327, 0x005f, 0x030b, 0x0328, 0x030c,
0x2015, 0x00b9, 0x00ae, 0x00a9, 0x2122, 0x266a, 0x20ac, 0x2030, 0x03B1, 0x0000, 0x0000, 0x0000, 0x215b, 0x215c, 0x215d, 0x215e,
0x03a9, 0x00c6, 0x0110, 0x00aa, 0x0126, 0x0000, 0x0132, 0x013f, 0x0141, 0x00d8, 0x0152, 0x00ba, 0x00de, 0x0166, 0x014a, 0x0149,
0x0138, 0x00e6, 0x0111, 0x00f0, 0x0127, 0x0131, 0x0133, 0x0140, 0x0142, 0x00f8, 0x0153, 0x00df, 0x00fe, 0x0167, 0x014b, 0x0020
}
// { // Cyrillic G2 Supplementary Set
// },
// { // Greek G2 Supplementary Set
// },
// { // Arabic G2 Supplementary Set
// }
};
const uint16_t G2_ACCENTS[15][52] = {
// A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z
{ // grave
0x00c0, 0x0000, 0x0000, 0x0000, 0x00c8, 0x0000, 0x0000, 0x0000, 0x00cc, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x00d2, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x00d9, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x00e0, 0x0000, 0x0000, 0x0000, 0x00e8, 0x0000,
0x0000, 0x0000, 0x00ec, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x00f2, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x00f9, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000
},
{ // acute
0x00c1, 0x0000, 0x0106, 0x0000, 0x00c9, 0x0000, 0x0000, 0x0000, 0x00cd, 0x0000, 0x0000, 0x0139, 0x0000, 0x0143, 0x00d3, 0x0000,
0x0000, 0x0154, 0x015a, 0x0000, 0x00da, 0x0000, 0x0000, 0x0000, 0x00dd, 0x0179, 0x00e1, 0x0000, 0x0107, 0x0000, 0x00e9, 0x0000,
0x0123, 0x0000, 0x00ed, 0x0000, 0x0000, 0x013a, 0x0000, 0x0144, 0x00f3, 0x0000, 0x0000, 0x0155, 0x015b, 0x0000, 0x00fa, 0x0000,
0x0000, 0x0000, 0x00fd, 0x017a
},
{ // circumflex
0x00c2, 0x0000, 0x0108, 0x0000, 0x00ca, 0x0000, 0x011c, 0x0124, 0x00ce, 0x0134, 0x0000, 0x0000, 0x0000, 0x0000, 0x00d4, 0x0000,
0x0000, 0x0000, 0x015c, 0x0000, 0x00db, 0x0000, 0x0174, 0x0000, 0x0176, 0x0000, 0x00e2, 0x0000, 0x0109, 0x0000, 0x00ea, 0x0000,
0x011d, 0x0125, 0x00ee, 0x0135, 0x0000, 0x0000, 0x0000, 0x0000, 0x00f4, 0x0000, 0x0000, 0x0000, 0x015d, 0x0000, 0x00fb, 0x0000,
0x0175, 0x0000, 0x0177, 0x0000
},
{ // tilde
0x00c3, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0128, 0x0000, 0x0000, 0x0000, 0x0000, 0x00d1, 0x00d5, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0168, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x00e3, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0129, 0x0000, 0x0000, 0x0000, 0x0000, 0x00f1, 0x00f5, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0169, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000
},
{ // macron
0x0100, 0x0000, 0x0000, 0x0000, 0x0112, 0x0000, 0x0000, 0x0000, 0x012a, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x014c, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x016a, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0101, 0x0000, 0x0000, 0x0000, 0x0113, 0x0000,
0x0000, 0x0000, 0x012b, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x014d, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x016b, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000
},
{ // breve
0x0102, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x011e, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x016c, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0103, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x011f, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x016d, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000
},
{ // dot
0x0000, 0x0000, 0x010a, 0x0000, 0x0116, 0x0000, 0x0120, 0x0000, 0x0130, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x017b, 0x0000, 0x0000, 0x010b, 0x0000, 0x0117, 0x0000,
0x0121, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x017c
},
{ // umlaut
0x00c4, 0x0000, 0x0000, 0x0000, 0x00cb, 0x0000, 0x0000, 0x0000, 0x00cf, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x00d6, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x00dc, 0x0000, 0x0000, 0x0000, 0x0178, 0x0000, 0x00e4, 0x0000, 0x0000, 0x0000, 0x00eb, 0x0000,
0x0000, 0x0000, 0x00ef, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x00f6, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x00fc, 0x0000,
0x0000, 0x0000, 0x00ff, 0x0000
},
{ 0 },
{ // ring
0x00c5, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x016e, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x00e5, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x016f, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000
},
{ // cedilla
0x0000, 0x0000, 0x00c7, 0x0000, 0x0000, 0x0000, 0x0122, 0x0000, 0x0000, 0x0000, 0x0136, 0x013b, 0x0000, 0x0145, 0x0000, 0x0000,
0x0000, 0x0156, 0x015e, 0x0162, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x00e7, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0137, 0x013c, 0x0000, 0x0146, 0x0000, 0x0000, 0x0000, 0x0157, 0x015f, 0x0163, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000
},
{ 0 },
{ // double acute
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0150, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0170, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0151, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0171, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000
},
{ // ogonek
0x0104, 0x0000, 0x0000, 0x0000, 0x0118, 0x0000, 0x0000, 0x0000, 0x012e, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0172, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0105, 0x0000, 0x0000, 0x0000, 0x0119, 0x0000,
0x0000, 0x0000, 0x012f, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0173, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000
},
{ // caron
0x0000, 0x0000, 0x010c, 0x010e, 0x011a, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x013d, 0x0000, 0x0147, 0x0000, 0x0000,
0x0000, 0x0158, 0x0160, 0x0164, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x017d, 0x0000, 0x0000, 0x010d, 0x010f, 0x011b, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x013e, 0x0000, 0x0148, 0x0000, 0x0000, 0x0000, 0x0159, 0x0161, 0x0165, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x017e
}
};
void page_buffer_add_string (struct TeletextCtx *ctx, const char *s)
{
if(ctx->page_buffer_cur_size < (ctx->page_buffer_cur_used + strlen (s)+1))
{
int add=strlen (s)+4096; // So we don't need to realloc often
ctx->page_buffer_cur_size=ctx->page_buffer_cur_size+add;
ctx->page_buffer_cur=(char *) realloc (ctx->page_buffer_cur,ctx->page_buffer_cur_size);
if (!ctx->page_buffer_cur)
fatal (EXIT_NOT_ENOUGH_MEMORY, "Not enough memory to process teletext page.\n");
}
memcpy (ctx->page_buffer_cur+ctx->page_buffer_cur_used, s, strlen (s));
ctx->page_buffer_cur_used+=strlen (s);
ctx->page_buffer_cur[ctx->page_buffer_cur_used]=0;
}
void ucs2_buffer_add_char (struct TeletextCtx *ctx, uint64_t c)
{
if (ctx->ucs2_buffer_cur_size<(ctx->ucs2_buffer_cur_used+2))
{
int add=4096; // So we don't need to realloc often
ctx->ucs2_buffer_cur_size=ctx->ucs2_buffer_cur_size+add;
ctx->ucs2_buffer_cur=(uint64_t *) realloc (ctx->ucs2_buffer_cur,ctx->ucs2_buffer_cur_size*sizeof (uint64_t));
if (!ctx->ucs2_buffer_cur)
fatal (EXIT_NOT_ENOUGH_MEMORY, "Not enough memory to process teletext page.\n");
}
ctx->ucs2_buffer_cur[ctx->ucs2_buffer_cur_used++]=c;
ctx->ucs2_buffer_cur[ctx->ucs2_buffer_cur_used]=0;
}
void page_buffer_add_char (struct TeletextCtx *ctx, char c)
{
char t[2];
t[0]=c;
t[1]=0;
page_buffer_add_string (ctx, t);
}
// ETS 300 706, chapter 8.2
uint8_t unham_8_4(uint8_t a)
{
uint8_t r = UNHAM_8_4[a];
if (r == 0xff) {
dbg_print (CCX_DMT_TELETEXT, "- Unrecoverable data error; UNHAM8/4(%02x)\n", a);
}
return (r & 0x0f);
}
// ETS 300 706, chapter 8.3
uint32_t unham_24_18(uint32_t a)
{
uint8_t test = 0;
//Tests A-F correspond to bits 0-6 respectively in 'test'.
for (uint8_t i = 0; i < 23; i++) test ^= ((a >> i) & 0x01) * (i + 33);
//Only parity bit is tested for bit 24
test ^= ((a >> 23) & 0x01) * 32;
if ((test & 0x1f) != 0x1f)
{
//Not all tests A-E correct
if ((test & 0x20) == 0x20)
{
//F correct: Double error
return 0xffffffff;
}
//Test F incorrect: Single error
a ^= 1 << (30 - test);
}
return (a & 0x000004) >> 2 | (a & 0x000070) >> 3 | (a & 0x007f00) >> 4 | (a & 0x7f0000) >> 5;
}
//Default G0 Character Set
void set_g0_charset(uint32_t triplet)
{
// ETS 300 706, Table 32
if((triplet & 0x3c00) == 0x1000)
{
if((triplet & 0x0380) == 0x0000)
default_g0_charset = CYRILLIC1;
else if((triplet & 0x0380) == 0x0200)
default_g0_charset = CYRILLIC2;
else if((triplet & 0x0380) == 0x0280)
default_g0_charset = CYRILLIC3;
else
default_g0_charset = LATIN;
}
else
default_g0_charset = LATIN;
}
// Latin National Subset Selection
void remap_g0_charset(uint8_t c)
{
if (c != primary_charset.current)
{
uint8_t m = G0_LATIN_NATIONAL_SUBSETS_MAP[c];
if (m == 0xff)
{
fprintf(stderr, "- G0 Latin National Subset ID 0x%1x.%1x is not implemented\n", (c >> 3), (c & 0x7));
}
else
{
for (uint8_t j = 0; j < 13; j++)
G0[LATIN][G0_LATIN_NATIONAL_SUBSETS_POSITIONS[j]] = G0_LATIN_NATIONAL_SUBSETS[m].characters[j];
VERBOSE_ONLY fprintf(stderr, "- Using G0 Latin National Subset ID 0x%1x.%1x (%s)\n", (c >> 3), (c & 0x7), G0_LATIN_NATIONAL_SUBSETS[m].language);
primary_charset.current = c;
}
}
}
// wide char (16 bits) to utf-8 conversion
void ucs2_to_utf8(char *r, uint16_t ch)
{
if (ch < 0x80)
{
r[0] = ch & 0x7f;
r[1] = 0;
r[2] = 0;
}
else if (ch < 0x800)
{
r[0] = (ch >> 6) | 0xc0;
r[1] = (ch & 0x3f) | 0x80;
r[2] = 0;
}
else
{
r[0] = (ch >> 12) | 0xe0;
r[1] = ((ch >> 6) & 0x3f) | 0x80;
r[2] = (ch & 0x3f) | 0x80;
}
r[3] = 0;
}
// check parity and translate any reasonable teletext character into ucs2
uint16_t telx_to_ucs2(uint8_t c)
{
if (PARITY_8[c] == 0)
{
dbg_print (CCX_DMT_TELETEXT, "- Unrecoverable data error; PARITY(%02x)\n", c);
return 0x20;
}
uint16_t r = c & 0x7f;
if (r >= 0x20)
r = G0[default_g0_charset][r - 0x20];
return r;
}
uint16_t bcd_page_to_int (uint16_t bcd)
{
return ((bcd&0xf00)>>8)*100 + ((bcd&0xf0)>>4)*10 + (bcd&0xf);
}
void telx_case_fix (struct TeletextCtx *context)
{
//Capitalizing first letter of every sentence
int line_len = strlen(context->page_buffer_cur);
for(int i = 0; i < line_len; i++)
{
switch(context->page_buffer_cur[i])
{
case ' ':
//case 0x89: // This is a transparent space
case '-':
break;
case '.': // Fallthrough
case '?': // Fallthrough
case '!':
case ':':
context->new_sentence = 1;
break;
default:
if (context->new_sentence && i!=0 && context->page_buffer_cur[i-1]!='\n')
context->page_buffer_cur[i] = cctoupper(context->page_buffer_cur[i]);
else if(!context->new_sentence && i!=0 && context->page_buffer_cur[i-1] != '\n')
context->page_buffer_cur[i] = cctolower(context->page_buffer_cur[i]);
context->new_sentence = 0;
break;
}
}
telx_correct_case(context->page_buffer_cur);
}
void telxcc_dump_prev_page (struct TeletextCtx *ctx, struct cc_subtitle *sub)
{
char info[4];
if (!ctx->page_buffer_prev)
return;
snprintf(info, 4, "%.3u", bcd_page_to_int(tlt_config.page));
add_cc_sub_text(sub, ctx->page_buffer_prev, ctx->prev_show_timestamp,
ctx->prev_hide_timestamp, info, "TLT", CCX_ENC_UTF_8);
if (ctx->page_buffer_prev)
free (ctx->page_buffer_prev);
if (ctx->ucs2_buffer_prev)
free (ctx->ucs2_buffer_prev);
// Switch "dump" buffers
ctx->page_buffer_prev_used=ctx->page_buffer_cur_used;
ctx->page_buffer_prev_size=ctx->page_buffer_cur_size;
ctx->page_buffer_prev=ctx->page_buffer_cur;
ctx->page_buffer_cur_size=0;
ctx->page_buffer_cur_used=0;
ctx->page_buffer_cur=NULL;
// Also switch compare buffers
ctx->ucs2_buffer_prev_used=ctx->ucs2_buffer_cur_used;
ctx->ucs2_buffer_prev_size=ctx->ucs2_buffer_cur_size;
ctx->ucs2_buffer_prev=ctx->ucs2_buffer_cur;
ctx->ucs2_buffer_cur_size=0;
ctx->ucs2_buffer_cur_used=0;
ctx->ucs2_buffer_cur=NULL;
}
// Note: c1 and c2 are just used for debug output, not for the actual comparison
int fuzzy_memcmp (const char *c1, const char *c2, const uint64_t *ucs2_buf1, unsigned ucs2_buf1_len,
const uint64_t *ucs2_buf2, unsigned ucs2_buf2_len)
{
size_t l;
size_t short_len=ucs2_buf1_len<ucs2_buf2_len?ucs2_buf1_len:ucs2_buf2_len;
size_t max=(short_len * tlt_config.levdistmaxpct)/100;
unsigned upto=(ucs2_buf1_len<ucs2_buf2_len)?ucs2_buf1_len:ucs2_buf2_len;
if (max < tlt_config.levdistmincnt)
max=tlt_config.levdistmincnt;
// For the second string, only take the first chars (up to the first string length, that's upto).
l = (size_t) levenshtein_dist (ucs2_buf1,ucs2_buf2,ucs2_buf1_len,upto);
int res=(l>max);
dbg_print(CCX_DMT_LEVENSHTEIN, "\rLEV | %s | %s | Max: %d | Calc: %d | Match: %d\n", c1,c2,max,l,!res);
return res;
}
void process_page(struct TeletextCtx *ctx, teletext_page_t *page, struct cc_subtitle *sub)
{
if ((tlt_config.extraction_start.set && page->hide_timestamp < tlt_config.extraction_start.time_in_ms) ||
(tlt_config.extraction_end.set && page->show_timestamp > tlt_config.extraction_end.time_in_ms) ||
page->hide_timestamp == 0)
{
return;
}
#ifdef DEBUG
for (uint8_t row = 1; row < 25; row++) {
fprintf(stdout, "DEBUG[%02u]: ", row);
for (uint8_t col = 0; col < 40; col++) fprintf(stdout, "%3x ", page->text[row][col]);
fprintf(stdout, "\n");
}
fprintf(stdout, "\n");
#endif
char u[4] = {0, 0, 0, 0};
// optimization: slicing column by column -- higher probability we could find boxed area start mark sooner
uint8_t page_is_empty = YES;
for (uint8_t col = 0; col < 40; col++)
{
for (uint8_t row = 1; row < 25; row++)
{
if (page->text[row][col] == 0x0b)
{
page_is_empty = NO;
goto page_is_empty;
}
}
}
page_is_empty:
if (page_is_empty == YES) return;
if (page->show_timestamp > page->hide_timestamp)
page->hide_timestamp = page->show_timestamp;
char timecode_show[24] = { 0 }, timecode_hide[24] = { 0 };
int time_reported=0;
char c_tempb[256]; // For buffering
uint8_t line_count = 0;
timestamp_to_srttime(page->show_timestamp, timecode_show);
timecode_show[12] = 0;
timestamp_to_srttime(page->hide_timestamp, timecode_hide);
timecode_hide[12] = 0;
// process data
for (uint8_t row = 1; row < 25; row++)
{
// anchors for string trimming purpose
uint8_t col_start = 40;
uint8_t col_stop = 40;
uint8_t box_open = NO;
for (int8_t col = 0; col < 40; col++)
{
// replace all 0/B and 0/A characters with 0/20, as specified in ETS 300 706:
// Unless operating in "Hold Mosaics" mode, each character space occupied by a
// spacing attribute is displayed as a SPACE
if (page->text[row][col] == 0xb) // open the box
{
if (col_start == 40)
{
col_start = col;
line_count++;
}
else
{
page->text[row][col] = 0x20;
}
box_open = YES;
}
else if (page->text[row][col] == 0xa) // close the box
{
page->text[row][col] = 0x20;
box_open = NO;
}
// characters between 0xA and 0xB shouldn't be displayed
// page->text[row][col] > 0x20 added to preserve color information
else if (!box_open && col_start < 40 && page->text[row][col] > 0x20)
{
page->text[row][col] = 0x20;
}
}
// line is empty
if (col_start > 39)
continue;
for (uint8_t col = col_start + 1; col <= 39; col++)
{
if (page->text[row][col] > 0x20)
{
if (col_stop > 39)
col_start = col;
col_stop = col;
}
}
// line is empty
if (col_stop > 39)
continue;
// ETS 300 706, chapter 12.2: Alpha White ("Set-After") - Start-of-row default condition.
// used for colour changes _before_ start box mark
// white is default as stated in ETS 300 706, chapter 12.2
// black(0), red(1), green(2), yellow(3), blue(4), magenta(5), cyan(6), white(7)
uint8_t foreground_color = 0x7;
uint8_t font_tag_opened = NO;
if (line_count > 1)
{
switch (tlt_config.write_format)
{
case CCX_OF_TRANSCRIPT:
page_buffer_add_string(ctx, " ");
break;
case CCX_OF_SMPTETT:
page_buffer_add_string(ctx, "<br/>");
break;
default:
page_buffer_add_string(ctx, "\r\n");
}
}
if (tlt_config.gui_mode_reports)
{
fprintf (stderr, "###SUBTITLE#");
if (!time_reported)
{
char timecode_show_mmss[6], timecode_hide_mmss[6];
memcpy (timecode_show_mmss, timecode_show+3, 5);
memcpy (timecode_hide_mmss, timecode_hide+3, 5);
timecode_show_mmss[5]=0;
timecode_hide_mmss[5]=0;
// Note, only MM:SS here as we need to save space in the preview window
fprintf (stderr, "%s#%s#",timecode_show_mmss, timecode_hide_mmss);
time_reported=1;
}
else
fprintf (stderr, "##");
}
for (uint8_t col = 0; col <= col_stop; col++)
{
// v is just a shortcut
uint16_t v = page->text[row][col];
if (col < col_start)
{
if (v <= 0x7) foreground_color = (uint8_t) v;
}
if (col == col_start)
{
if ((foreground_color != 0x7) && !tlt_config.nofontcolor)
{
sprintf (c_tempb, "<font color=\"%s\">", TTXT_COLOURS[foreground_color]);
page_buffer_add_string (ctx, c_tempb);
font_tag_opened = YES;
}
}
if (col >= col_start)
{
if (v <= 0x7)
{
// ETS 300 706, chapter 12.2: Unless operating in "Hold Mosaics" mode,
// each character space occupied by a spacing attribute is displayed as a SPACE.
if (!tlt_config.nofontcolor)
{
if (font_tag_opened == YES)
{
page_buffer_add_string (ctx, "</font>");
font_tag_opened = NO;
}
page_buffer_add_string(ctx, " ");
// black is considered as white for telxcc purpose
// telxcc writes <font/> tags only when needed
if ((v > 0x0) && (v < 0x7))
{
sprintf (c_tempb, "<font color=\"%s\">", TTXT_COLOURS[v]);
page_buffer_add_string (ctx, c_tempb);
font_tag_opened = YES;
}
}
else
v = 0x20;
}
if (v >= 0x20)
{
ucs2_to_utf8(u, v);
uint64_t ucs2_char=(u[0]<<24) | (u[1]<<16) | (u[2]<<8) | u[3];
ucs2_buffer_add_char(ctx, ucs2_char);
if (font_tag_opened == NO && tlt_config.latrusmap) {
for (uint8_t i = 0; i < array_length(LAT_RUS); i++) {
if (v == LAT_RUS[i].lat_char)
{
page_buffer_add_string (ctx, LAT_RUS[i].rus_char);
// already processed char
v = 0;
break;
}
}
}
// translate some chars into entities, if in colour mode
if (!tlt_config.nofontcolor && !tlt_config.nohtmlescape)
{
for (uint8_t i = 0; i < array_length(ENTITIES); i++)
if (v == ENTITIES[i].character)
{
page_buffer_add_string (ctx, ENTITIES[i].entity);
// v < 0x20 won't be printed in next block
v = 0;
break;
}
}
}
if (v >= 0x20)
{
page_buffer_add_string (ctx, u);
if (tlt_config.gui_mode_reports) // For now we just handle the easy stuff
fprintf (stderr,"%s",u);
}
}
}
// no tag will left opened!
if ((!tlt_config.nofontcolor) && (font_tag_opened == YES))
{
page_buffer_add_string (ctx, "</font>");
font_tag_opened = NO;
}
if (tlt_config.gui_mode_reports)
{
fprintf (stderr,"\n");
}
}
time_reported=0;
if (ctx->sentence_cap)
telx_case_fix(ctx);
switch (tlt_config.write_format)
{
case CCX_OF_TRANSCRIPT:
case CCX_OF_SRT:
if (ctx->page_buffer_prev_used == 0)
ctx->prev_show_timestamp = page->show_timestamp;
if (ctx->page_buffer_prev_used == 0 ||
(tlt_config.dolevdist &&
fuzzy_memcmp (ctx->page_buffer_prev, ctx->page_buffer_cur,
ctx->ucs2_buffer_prev, ctx->ucs2_buffer_prev_used,
ctx->ucs2_buffer_cur, ctx->ucs2_buffer_cur_used
) == 0))
{
// If empty previous buffer, we just start one with the
// current page and do nothing. Wait until we see more.
if (ctx->page_buffer_prev)
free (ctx->page_buffer_prev);
ctx->page_buffer_prev_used = ctx->page_buffer_cur_used;
ctx->page_buffer_prev_size = ctx->page_buffer_cur_size;
ctx->page_buffer_prev = ctx->page_buffer_cur;
ctx->page_buffer_cur_size = 0;
ctx->page_buffer_cur_used = 0;
ctx->page_buffer_cur = NULL;
if (ctx->ucs2_buffer_prev)
free (ctx->ucs2_buffer_prev);
ctx->ucs2_buffer_prev_used = ctx->ucs2_buffer_cur_used;
ctx->ucs2_buffer_prev_size = ctx->ucs2_buffer_cur_size;
ctx->ucs2_buffer_prev = ctx->ucs2_buffer_cur;
ctx->ucs2_buffer_cur_size = 0;
ctx->ucs2_buffer_cur_used = 0;
ctx->ucs2_buffer_cur = NULL;
ctx->prev_hide_timestamp = page->hide_timestamp;
break;
}
else
{
// OK, the old and new buffer don't match. So write the old
telxcc_dump_prev_page(ctx, sub);
ctx->prev_hide_timestamp = page->hide_timestamp;
ctx->prev_show_timestamp = page->show_timestamp;
}
break;
default:
add_cc_sub_text(sub, ctx->page_buffer_cur, page->show_timestamp,
page->hide_timestamp + 1, NULL, "TLT", CCX_ENC_UTF_8);
}
// Also update GUI...
ctx->page_buffer_cur_used=0;
if (ctx->page_buffer_cur)
ctx->page_buffer_cur[0]=0;
if (tlt_config.gui_mode_reports)
fflush (stderr);
}
void process_telx_packet(struct TeletextCtx *ctx, data_unit_t data_unit_id, teletext_packet_payload_t *packet, uint64_t timestamp, struct cc_subtitle *sub)
{
// variable names conform to ETS 300 706, chapter 7.1.2
uint8_t address, m, y, designation_code;
address = (unham_8_4(packet->address[1]) << 4) | unham_8_4(packet->address[0]);
m = address & 0x7;
if (m == 0) m = 8;
y = (address >> 3) & 0x1f;
designation_code = (y > 25) ? unham_8_4(packet->data[0]) : 0x00;
uint8_t flag_subtitle;
if (y == 0)
{
// CC map
uint8_t i = (unham_8_4(packet->data[1]) << 4) | unham_8_4(packet->data[0]);
uint8_t flag_subtitle = (unham_8_4(packet->data[5]) & 0x08) >> 3;
uint16_t page_number;
uint8_t charset;
uint8_t c;
ctx->cc_map[i] |= flag_subtitle << (m - 1);
if ((flag_subtitle == YES) && (i < 0xff))
{
int thisp= (m << 8) | (unham_8_4(packet->data[1]) << 4) | unham_8_4(packet->data[0]);
char t1[10];
sprintf (t1,"%x",thisp); // Example: 1928 -> 788
thisp=atoi (t1);
if (!ctx->seen_sub_page[thisp])
{
ctx->seen_sub_page[thisp]=1;
mprint ("\rNotice: Teletext page with possible subtitles detected: %03d\n",thisp);
}
}
if ((tlt_config.page == 0) && (flag_subtitle == YES) && (i < 0xff))
{
tlt_config.page = (m << 8) | (unham_8_4(packet->data[1]) << 4) | unham_8_4(packet->data[0]);
mprint ("- No teletext page specified, first received suitable page is %03x, not guaranteed\n", tlt_config.page);
}
// Page number and control bits
page_number = (m << 8) | (unham_8_4(packet->data[1]) << 4) | unham_8_4(packet->data[0]);
charset = ((unham_8_4(packet->data[7]) & 0x08) | (unham_8_4(packet->data[7]) & 0x04) | (unham_8_4(packet->data[7]) & 0x02)) >> 1;
//uint8_t flag_suppress_header = unham_8_4(packet->data[6]) & 0x01;
//uint8_t flag_inhibit_display = (unham_8_4(packet->data[6]) & 0x08) >> 3;
// ETS 300 706, chapter 9.3.1.3:
// When set to '1' the service is designated to be in Serial mode and the transmission of a page is terminated
// by the next page header with a different page number.
// When set to '0' the service is designated to be in Parallel mode and the transmission of a page is terminated
// by the next page header with a different page number but the same magazine number.
// The same setting shall be used for all page headers in the service.
// ETS 300 706, chapter 7.2.1: Page is terminated by and excludes the next page header packet
// having the same magazine address in parallel transmission mode, or any magazine address in serial transmission mode.
ctx->transmission_mode = (transmission_mode_t) (unham_8_4(packet->data[7]) & 0x01);
// FIXME: Well, this is not ETS 300 706 kosher, however we are interested in DATA_UNIT_EBU_TELETEXT_SUBTITLE only
if ((ctx->transmission_mode == TRANSMISSION_MODE_PARALLEL) && (data_unit_id != DATA_UNIT_EBU_TELETEXT_SUBTITLE) && !(de_ctr && flag_subtitle && ctx->receiving_data == YES)) return;
if ((ctx->receiving_data == YES) && (
((ctx->transmission_mode == TRANSMISSION_MODE_SERIAL) && (PAGE(page_number) != PAGE(tlt_config.page))) ||
((ctx->transmission_mode == TRANSMISSION_MODE_PARALLEL) && (PAGE(page_number) != PAGE(tlt_config.page)) && (m == MAGAZINE(tlt_config.page)))))
{
ctx->receiving_data = NO;
if(!(de_ctr && flag_subtitle))
return;
}
// Page transmission is terminated, however now we are waiting for our new page
if (page_number != tlt_config.page && !(de_ctr && flag_subtitle && ctx->receiving_data == YES))
return;
// Now we have the begining of page transmission; if there is page_buffer pending, process it
if (ctx->page_buffer.tainted == YES)
{
// Convert telx to UCS-2 before processing
for(uint8_t yt = 1; yt <= 23; ++yt)
{
for(uint8_t it = 0; it < 40; it++)
{
if (ctx->page_buffer.text[yt][it] != 0x00 && ctx->page_buffer.g2_char_present[yt][it] == 0)
ctx->page_buffer.text[yt][it] = telx_to_ucs2(ctx->page_buffer.text[yt][it]);
}
}
// it would be nice, if subtitle hides on previous video frame, so we contract 40 ms (1 frame @25 fps)