-
Notifications
You must be signed in to change notification settings - Fork 6
/
descriptors.c
1855 lines (1736 loc) · 94.7 KB
/
descriptors.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
/*
* Simple MPEG/DVB parser to achieve network/service information without initial tuning data
*
* Copyright (C) 2006-2014 Winfried Koehler
* Copyright (C) 2017 - 2020 mighty-p
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* Or, point your browser to http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
* The project's page is https://github.com/mighty-p/t2scan
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "extended_frontend.h"
#include "si_types.h"
#include "scan.h"
#include "descriptors.h"
#include "atsc_psip_section.h"
#include "char-coding.h"
#define hd(d) hexdump(__FUNCTION__, d + 2, d[1])
/******************************************************************************
* returns minimum repetition rates as specified in ETR211 4.4.1 and 4.4.2
* and 13818-1 C.9 Bandwidth Utilization and Signal Acquisition Time
*****************************************************************************/
int repetition_rate(scantype_t scan_type, enum table_id table) {
switch(scan_type) {
case SCAN_CABLE:
case SCAN_SATELLITE:
// ETR211 4.4.1 Satellite and cable delivery systems
switch(table) {
case TABLE_PAT:
case TABLE_CAT:
case TABLE_PMT:
case TABLE_TSDT:
// see 13818-1 C.9 Bandwidth Utilization and Signal Acquisition Time
// FIXME: i did not understand fully
// but i seems to be (1/1 .. [1/25] .. 1/100) sec
// no hard spec.. :-(
return 1;
case TABLE_SDT_ACT:
case TABLE_EIT_ACT:
case TABLE_EIT_SCHEDULE_ACT_50 ... TABLE_EIT_SCHEDULE_ACT_5F:
return 2;
case TABLE_NIT_ACT:
case TABLE_NIT_OTH:
case TABLE_BAT:
case TABLE_SDT_OTH:
case TABLE_EIT_OTH:
return 10;
case TABLE_EIT_SCHEDULE_OTH_60 ... TABLE_EIT_SCHEDULE_OTH_60:
case TABLE_TDT:
case TABLE_TOT:
return 30;
default:
debug("table id 0x%.02X no repetition rate defined.\n", table);
return 30;
}
break;
case SCAN_TERRESTRIAL:
// ETR211 4.4.2 Terrestrial delivery systems
switch(table) {
case TABLE_PAT:
case TABLE_CAT:
case TABLE_PMT:
case TABLE_TSDT:
// see 13818-1 C.9 Bandwidth Utilization and Signal Acquisition Time
// FIXME: i did not understand fully
// but i seems to be (1/1 .. [1/25] .. 1/100) sec
// no hard spec.. :-(
return 3; // changed 20190120 since the old value was too slow for the "Connect" channels. This can make the scan slower.
case TABLE_NIT_ACT:
case TABLE_NIT_OTH:
case TABLE_BAT:
case TABLE_SDT_OTH:
case TABLE_EIT_SCHEDULE_ACT_50 ... TABLE_EIT_SCHEDULE_ACT_5F:
return 12;
case TABLE_SDT_ACT:
case TABLE_EIT_ACT:
return 2;
case TABLE_EIT_OTH:
return 20;
case TABLE_EIT_SCHEDULE_OTH_60 ... TABLE_EIT_SCHEDULE_OTH_60:
return 60;
case TABLE_TDT:
case TABLE_TOT:
return 30;
default:
debug("table id 0x%.02X no repetition rate defined.\n", table);
return 30;
}
break;
case SCAN_TERRCABLE_ATSC:
switch(table) {
case TABLE_PAT:
case TABLE_CAT:
case TABLE_PMT:
case TABLE_TSDT:
// see 13818-1 C.9 Bandwidth Utilization and Signal Acquisition Time
// FIXME: i did not understand fully
// but i seems to be (1/1 .. [1/25] .. 1/100) sec
// no hard spec.. :-(
return 1;
default:
/* FIXME: i dont have *any* information about atsc
* repetition rates. This should not break anything,
* but may be we will loose performance or services..
* these are the values mkrufky put in.
* Probably the same values as above..?
*/
debug("table id 0x%.02X no repetition rate defined.\n",
table);
return 5;
}
return 5;
default:
fatal("undefined frontend type.\n");
}
};
/******************************************************************************
* 300468 v181 6.2.32 Service descriptor
*****************************************************************************/
void parse_service_descriptor (const unsigned char *buf, struct service *s, unsigned user_charset_id) {
unsigned char len;
uint i, full_len, short_len, isUtf8;
uint emphasis_on = 0;
char * provider_name = NULL;
char * provider_short_name = NULL;
char * service_name = NULL;
char * service_short_name = NULL;
size_t inbytesleft, outbytesleft;
char * inbuf = NULL;
char * outbuf = NULL;
int default_charset_reset = 0;
hd(buf);
s->type = buf[2];
buf += 3;
len = *buf;
buf++;
if (s->provider_name) {
free (s->provider_name);
s->provider_name = 0;
}
if (s->provider_short_name) {
free (s->provider_short_name);
s->provider_name = 0;
}
full_len = short_len = emphasis_on = 0;
isUtf8 = (*buf == 0x15);
/* count length for short provider name
* and long provider name
*/
for(i=0; i < len; i++) {
switch(*(buf + i)) {
case sb_cc_reserved_80 ... sb_cc_reserved_85:
case sb_cc_reserved_88 ... sb_cc_reserved_89:
case sb_cc_user_8B ... sb_cc_user_9F:
// ETR211 4.6.1 Use of control codes in names
case character_cr_lf:
continue;
case character_emphasis_on:
emphasis_on = 1;
continue;
case character_emphasis_off:
emphasis_on = 0;
continue;
case utf8_cc_start:
if (isUtf8 && (i+1 < len)) {
uint16_t utf8_cc;
utf8_cc = *(buf + i) << 8;
utf8_cc += *(buf + i + 1);
switch(utf8_cc) {
case utf8_character_emphasis_on:
emphasis_on = 1;
i++;
continue;
case utf8_character_emphasis_off:
emphasis_on = 0;
i++;
continue;
default:;
}
}
default:
if (emphasis_on)
short_len++;
full_len++;
continue;
}
}
/* allocating memory and zero-terminating */
provider_name = calloc (full_len + 1, 1);
provider_short_name = calloc (short_len + 1, 1);
full_len = short_len = emphasis_on = 0;
/* copy data */
for(i=0; i < len; i++) {
switch(*(buf + i)) {
case sb_cc_reserved_80 ... sb_cc_reserved_85:
case sb_cc_reserved_88 ... sb_cc_reserved_89:
case sb_cc_user_8B ... sb_cc_user_9F:
// ETR211 4.6.1 Use of control codes in names
case character_cr_lf:
continue;
case character_emphasis_on:
emphasis_on = 1;
continue;
case character_emphasis_off:
emphasis_on = 0;
continue;
case utf8_cc_start:
if (isUtf8 && (i+1 < len)) {
uint16_t utf8_cc;
utf8_cc = *(buf + i) << 8;
utf8_cc += *(buf + i + 1);
switch(utf8_cc) {
case utf8_character_emphasis_on:
emphasis_on = 1;
i++;
continue;
case utf8_character_emphasis_off:
emphasis_on = 0;
i++;
continue;
default:;
}
}
default:
if (emphasis_on)
provider_short_name[short_len++] = *(buf + i);
provider_name[full_len++] = *(buf + i);
continue;
}
}
if (provider_name[0]) {
inbytesleft = full_len;
outbytesleft = 4 * full_len + 1;
s->provider_name = (char *) calloc(outbytesleft, 1);
inbuf = provider_name;
outbuf = s->provider_name;
char_coding(&inbuf, &inbytesleft, &outbuf, &outbytesleft, user_charset_id);
if(strcmp(s->provider_name,"ORF")==0) { default_charset_reset++; set_char_coding_default_charset("ISO885915"); } // special handling for ORF 20200517
}
free(provider_name);
if (provider_short_name[0]) {
inbytesleft = short_len;
outbytesleft = 4 * short_len + 1;
s->provider_short_name = (char *) calloc(outbytesleft, 1);
inbuf = provider_short_name;
outbuf = s->provider_short_name;
char_coding(&inbuf, &inbytesleft, &outbuf, &outbytesleft, user_charset_id);
if(strcmp(s->provider_short_name,"ORF")==0) { default_charset_reset++; set_char_coding_default_charset("ISO885915"); } // special handling for ORF 20200517
}
free(provider_short_name);
buf += len;
len = *buf;
buf++;
if (s->service_name)
free (s->service_name);
if (s->service_short_name)
free (s->service_short_name);
isUtf8 = (*buf == 0x15);
/* count length for short service name
* and long service name
*/
full_len = short_len = emphasis_on = 0;
for(i=0; i < len; i++) {
switch(*(buf + i)) {
case sb_cc_reserved_80 ... sb_cc_reserved_85:
case sb_cc_reserved_88 ... sb_cc_reserved_89:
case sb_cc_user_8B ... sb_cc_user_9F:
// ETR211 4.6.1 Use of control codes in names
case character_cr_lf:
continue;
case character_emphasis_on:
emphasis_on = 1;
continue;
case character_emphasis_off:
emphasis_on = 0;
continue;
case utf8_cc_start:
if (isUtf8 && (i+1 < len)) {
uint16_t utf8_cc;
utf8_cc = *(buf + i) << 8;
utf8_cc += *(buf + i + 1);
switch(utf8_cc) {
case utf8_character_emphasis_on:
emphasis_on = 1;
i++;
continue;
case utf8_character_emphasis_off:
emphasis_on = 0;
i++;
continue;
default:;
}
}
default:
if (emphasis_on)
short_len++;
full_len++;
continue;
}
}
/* allocating memory and zero-terminating */
service_name = calloc (full_len + 1, 1);
service_short_name = calloc (short_len + 1, 1);
full_len = short_len = emphasis_on = 0;
/* copy data */
for(i=0; i < len; i++) {
switch(*(buf + i)) {
case sb_cc_reserved_80 ... sb_cc_reserved_85:
case sb_cc_reserved_88 ... sb_cc_reserved_89:
case sb_cc_user_8B ... sb_cc_user_9F:
// ETR211 4.6.1 Use of control codes in names
case character_cr_lf:
continue;
case character_emphasis_on:
emphasis_on = 1;
continue;
case character_emphasis_off:
emphasis_on = 0;
continue;
case utf8_cc_start:
if (isUtf8 && (i+1 < len)) {
uint16_t utf8_cc;
utf8_cc = *(buf + i) << 8;
utf8_cc += *(buf + i + 1);
switch(utf8_cc) {
case utf8_character_emphasis_on:
emphasis_on = 1;
i++;
continue;
case utf8_character_emphasis_off:
emphasis_on = 0;
i++;
continue;
default:;
}
}
default:
if (emphasis_on)
service_short_name[short_len++] = *(buf + i);
service_name[full_len++] = *(buf + i);
continue;
}
}
if (service_name[0]) {
inbytesleft = full_len;
outbytesleft = 4 * full_len + 1;
s->service_name = (char *) calloc(outbytesleft, 1);
inbuf = service_name;
outbuf = s->service_name;
char_coding(&inbuf, &inbytesleft, &outbuf, &outbytesleft, user_charset_id);
}
free(service_name);
if (service_short_name[0]) {
inbytesleft = short_len;
outbytesleft = 4 * short_len + 1;
s->service_short_name = (char *) calloc(outbytesleft, 1);
inbuf = service_short_name;
outbuf = s->service_short_name;
char_coding(&inbuf, &inbytesleft, &outbuf, &outbytesleft, user_charset_id);
}
free(service_short_name);
info("\tservice = %s (%s)\n", s->service_name, s->provider_name);
if (default_charset_reset) reset_char_coding_default_charset();
}
void parse_ca_identifier_descriptor (const unsigned char *buf, struct service *s) {
unsigned char len = buf [1];
unsigned int i;
buf += 2;
if (len > sizeof(s->ca_id)) {
len = sizeof(s->ca_id);
warning("too many CA system ids\n");
}
memcpy(s->ca_id, buf, len);
s->ca_num=0;
for(i = 0; i < len / sizeof(s->ca_id[0]); i++) {
int id = ((s->ca_id[i] & 0x00FF) << 8) + ((s->ca_id[i] & 0xFF00) >> 8);
s->ca_id[i] = id;
moreverbose("\tCA ID\t: PID 0x%04x\n", s->ca_id[i]);
s->ca_num++;
}
}
void parse_ca_descriptor (const unsigned char *buf, struct service *s) {
unsigned char descriptor_length = buf [1];
int CA_system_ID;
int found=0;
int i;
buf += 2;
if (descriptor_length < 4)
return;
CA_system_ID = (buf[0] << 8) | buf[1];
for(i=0; i<s->ca_num; i++)
if (s->ca_id[i] == CA_system_ID)
found++;
if (!found) {
if (s->ca_num + 1 >= CA_SYSTEM_ID_MAX)
warning("TOO MANY CA SYSTEM IDs.\n");
else {
moreverbose("\tCA ID\t: PID 0x%04x\n", CA_system_ID);
s->ca_id[s->ca_num]=CA_system_ID;
s->ca_num++;
}
}
}
void parse_iso639_language_descriptor (const unsigned char *buf, struct service *s) {
unsigned int lang_count = buf[1] / 4;
unsigned int i;
buf += 2;
if (s->audio_num < 1) return;
for(i = 0; i < lang_count; i++) {
// ISO_639_language_code 24 bslbf
memcpy(s->audio_lang[s->audio_num-1], buf, 3);
/* switch(buf[3]) { // audio_type 8 bslbf, seems to be wrong all over the place
case 1: // clean effects, program element has no language
break;
case 2: // hearing impaired, program element is prepared for the hearing impaired
break;
case 3: // visual_impaired_commentary, program element is prepared for the visually impaired viewer
break;
default:
info("unhandled audio_type.\n");
}*/
buf += 4;
}
}
void parse_subtitling_descriptor (const unsigned char *buf, struct service *s) {
unsigned int N = buf[1] / 8; // descriptor_length divided by 8_bytes per subtitle
unsigned int i;
buf += 2;
if (N > SUBTITLES_MAX)
N = SUBTITLES_MAX;
for(i = 0; i < N; i++) {
memcpy(s->subtitling_lang[i], buf, 3);
buf += 3;
s->subtitling_type[i] = buf[0];
buf++;
s->composition_page_id[i] = buf[0] << 8 | buf[1];
buf += 2;
s->ancillary_page_id[i] = buf[0] << 8 | buf[1];
buf += 2;
}
}
void parse_network_name_descriptor (const unsigned char *buf, struct transponder *t) {
unsigned char len = buf[1];
//hd(buf);
if (t == NULL) {
info("%s: transponder == NULL\n", __FUNCTION__);
return;
}
if (t->network_name)
free (t->network_name);
t->network_name = (char *) malloc(len + 1);
memcpy(t->network_name, buf + 2, len);
t->network_name[len] = '\0';
if (!t->network_name[0]) {
free (t->network_name);
t->network_name = 0;
}
}
static long bcd32_to_cpu (const int b0, const int b1, const int b2, const int b3) {
return ((b0 >> 4) & 0x0f) * 10000000 + (b0 & 0x0f) * 1000000 +
((b1 >> 4) & 0x0f) * 100000 + (b1 & 0x0f) * 10000 +
((b2 >> 4) & 0x0f) * 1000 + (b2 & 0x0f) * 100 +
((b3 >> 4) & 0x0f) * 10 + (b3 & 0x0f);
}
time_t bcdtime(const unsigned char *t) {
return ((t[0] >> 4)*10 + (t[0] & 0xF)) * 3600 +
((t[1] >> 4)*10 + (t[1] & 0xF)) * 60 +
((t[2] >> 4)*10 + (t[2] & 0xF));
}
__u32 get_u32(const unsigned char *p) {
return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
}
__u32 get_u24(const unsigned char *p) {
return (p[0] << 16) | (p[1] << 8) | p[2];
}
__u16 get_u16(const unsigned char *p) {
return (p[0] << 8) | p[1];
}
void parse_S2_satellite_delivery_system_descriptor(const unsigned char *buf, void * dummy) {
hd(buf);
/* FIXME: finding that descriptor means that we're dealing with two
* transponders on the same freq. I'm not shure now what to do this case.
*/
//scrambling_sequence_selector 1 bslbf
//scrambling_sequence_selector = (buf[2] & 0x80) >> 7;
//multiple_input_stream_flag 1 bslbf
//multiple_input_stream_flag = (buf[2] & 0x40) >> 6;
//backwards_compatibility_indicator 1 bslbf
//backwards_compatibility_indicator = (buf[2] & 0x20) >> 5;
//reserved_future_use 5 bslbf
//buf += 3;
//if (scrambling_sequence_selector == 1) {
// Reserved 6 bslbf
// scrambling_sequence_index 18 uimsbf
// scrambling_sequence_index = (*buf++ & 0x03) << 16;
// scrambling_sequence_index |= *buf++ << 8;
// scrambling_sequence_index |= *buf++;
// }
//if (multiple_input_stream_flag == 1) {
// input_stream_identifier 8 uimsbf
// input_stream_identifier = *buf++;
//}
verbose("S2_satellite_delivery_system_descriptor(skipped.)\n");
}
void parse_satellite_delivery_system_descriptor(const unsigned char *buf,
struct transponder *t, fe_spectral_inversion_t inversion) {
if (t == NULL)
return;
hd(buf);
t->type = SCAN_SATELLITE;
t->source = 0x43;
t->inversion = inversion;
/* frequency is coded in GHz, where the decimal point occurs after the
* third character (e.g. 011,75725 GHz).
*/
t->frequency = 10 * bcd32_to_cpu (buf[2], buf[3], buf[4], buf[5]);
//orbital_position 16 bslbf
t->orbital_position = (buf[6] << 8) | buf[7];
//west_east_flag 1 bslbf
t->west_east_flag = (buf[8] & 0x80) >> 7;
//polarization 2 bslbf
switch((buf[8] & 0x60) >> 5) {
case 0: t->polarization = POLARIZATION_HORIZONTAL; break;
case 1: t->polarization = POLARIZATION_VERTICAL; break;
case 2: t->polarization = POLARIZATION_CIRCULAR_LEFT; break;
case 3: t->polarization = POLARIZATION_CIRCULAR_RIGHT; break;
default:
fatal("polarization decoding failed: %d\n", (buf[8] & 0x60) >> 5);
}
switch((buf[8] & 0x18) >> 3) {
case 0: t->rolloff = ROLLOFF_35; break;
case 1: t->rolloff = ROLLOFF_25; break;
case 2: t->rolloff = ROLLOFF_20; break;
case 3:
warning("reserved rolloff value 3 found\n");
t->rolloff = ROLLOFF_AUTO;
break;
default:
fatal("rolloff decoding failed: %d\n", (buf[8] & 0x18) >> 3);
}
switch((buf[8] & 0x04) >> 2) {
case 0: t->delsys = SYS_DVBS; break;
case 1: t->delsys = SYS_DVBS2; break;
default:
t->delsys = SYS_DVBS;
}
//modulation_type 2 bslbf
switch(buf[8] & 0x03) {
case 1: t->modulation = QPSK; break;
case 2: t->modulation = PSK_8; break;
case 3: t->modulation = QAM_16; break;
default:
t->modulation = QAM_AUTO;
}
if (t->modulation == PSK_8)
t->delsys = SYS_DVBS2;
//symbol_rate 28 bslbf
t->symbolrate = 10 * bcd32_to_cpu(buf[9], buf[10], buf[11], buf[12] & 0xF0);
//FEC_inner 4 bslbf
switch (buf[12] & 0x0F) {
case 1: t->coderate = FEC_1_2; break;
case 2: t->coderate = FEC_2_3; break;
case 3: t->coderate = FEC_3_4; break;
case 4: t->coderate = FEC_5_6; break;
case 5: t->coderate = FEC_7_8; break;
case 6: t->coderate = FEC_8_9; break;
case 7: t->coderate = FEC_3_5; break;
case 8: t->coderate = FEC_4_5; break;
case 9: t->coderate = FEC_9_10; break;
case 15:t->coderate = FEC_NONE; break;
default:
verbose("\t%s: undefined inner fec %u\n",
__FUNCTION__, buf[12] & 0x0F);
t->coderate = FEC_AUTO;
}
/* some NIT's are broken. */
if ((t->modulation == PSK_8) ||
(t->rolloff == ROLLOFF_25) ||
(t->rolloff == ROLLOFF_20) ||
(t->coderate == FEC_9_10) ||
(t->coderate == FEC_3_5)) {
verbose("\t%s: fixing broken NIT, setting modulation_system to DVB-S2.\n",
__FUNCTION__);
t->delsys = SYS_DVBS2;
}
}
#ifndef FEC_RS_204_208 //FIXME: as soon as defined in Linux DVB API, insert correct name here.
#define FEC_RS_204_208 FEC_AUTO
#endif
void parse_cable_delivery_system_descriptor (const unsigned char * buf, struct transponder * t,
fe_spectral_inversion_t inversion) {
if (t == NULL)
return;
hd(buf);
t->type = SCAN_CABLE;
t->source = 0x44;
t->delsys = SYS_DVBC_ANNEX_AC;
t->inversion = inversion;
/*frequency is coded in MHz, where the decimal occurs after the fourth
character (e.g. 0312,0000 MHz).
*/
t->frequency = 100 * bcd32_to_cpu (buf[2], buf[3], buf[4], buf[5]);
//t->reserved_future_use = (buf[6] << 4) | ((buf[7] & 0xf0) >> 4);
//FEC_outer 4 bslbf -> not used by linuxtv dvb api. WHY?
// switch (buf[7] & 0x0f) {
// case 1: t->fec_outer = FEC_NONE; break;
// case 2: t->fec_outer = FEC_RS_204_208; break;
// default:
// info("undefined outer fec\n");
// t->fec_outer = FEC_AUTO;
// }
//modulation 8 bslbf
switch (buf[8]) {
case 1: t->modulation = QAM_16; break;
case 2: t->modulation = QAM_32; break;
case 3: t->modulation = QAM_64; break;
case 4: t->modulation = QAM_128; break;
case 5: t->modulation = QAM_256; break;
default:
verbose("undefined modulation, using QAM_AUTO\n");
t->modulation = QAM_AUTO;
}
//symbol_rate 28 bslbf
t->symbolrate = 10 * bcd32_to_cpu(buf[9], buf[10], buf[11], buf[12] & 0xf0);
//FEC_inner 4 bslbf
switch (buf[12] & 0x0f) {
case 1: t->coderate = FEC_1_2; break;
case 2: t->coderate = FEC_2_3; break;
case 3: t->coderate = FEC_3_4; break;
case 4: t->coderate = FEC_5_6; break;
case 5: t->coderate = FEC_7_8; break;
case 6: t->coderate = FEC_8_9; break;
case 7: t->coderate = FEC_3_5; break;
case 8: t->coderate = FEC_4_5; break;
case 9: t->coderate = FEC_9_10; break;
case 15: t->coderate = FEC_NONE; break;
default:
verbose("undefined inner fec, using FEC_AUTO\n");
t->coderate = FEC_AUTO;
}
}
/* DVB-C2: PRELIMINARY && UNTESTED CODE ONLY. I NEED SOMEBODY WITH ACCESS TO
* DVB-C2. IF YOU WANT TO HELP PLS CONTACT ME. --20111204, wirbel--
*/
// 300468 v011201_final_draft; 09/2011
void parse_C2_delivery_system_descriptor (const unsigned char *buf,
struct transponder *t, fe_spectral_inversion_t inversion) {
__u8 descriptor_length;
//__u8 descriptor_tag_extension;
unsigned char * bp;
hd(buf);
if (t == NULL) return;
t->type = SCAN_CABLE;
t->source = 0x0D;
t->delsys = SYS_DVBC2;
t->inversion = inversion;
// descriptor_tag 8 uimsbf
descriptor_length = buf[1]; // descriptor_length 8 uimsbf
//descriptor_tag_extension = buf[2]; // descriptor_tag_extension 8 uimsbf
bp = (unsigned char *) &buf[3]; descriptor_length--;
t->plp_id = *bp; // plp_id 8 uimsbf; uniquely identifies a data PLP within the C2 System
bp++; descriptor_length--;
t->data_slice_id = *bp; // data_slice_id 8 uimsbf; uniquely identifies a data slice within the C2 system
bp++; descriptor_length--;
t->frequency = get_u32(bp); // C2_tuning_frequency 32 bslbf; see C2_tuning_frequency_type
bp+=4; descriptor_length-=4;
switch((*bp & 0xC0) >> 6) { // C2_tuning_frequency_type 2 uimsbf
case 0: t->C2_tuning_frequency_type = DATA_SLICE_TUNING_FREQUENCY; break;
case 1: t->C2_tuning_frequency_type = C2_SYSTEM_CENTER_FREQUENCY; break;
case 2: t->C2_tuning_frequency_type = INITIAL_TUNING_FOR_STATIC_DATA_SLICE; break;
//case 3: reserved_for_future_use
default:t->C2_tuning_frequency_type = DATA_SLICE_TUNING_FREQUENCY; // This is the default option for C2 systems
}
switch((*bp & 0x38) >> 3) { // active_OFDM_symbol_duration 3 uimsbf
case 0: t->active_OFDM_symbol_duration = FFT_4K_8MHZ; break; // 448\B5sec (4k FFT mode for 8MHz CATV systems)
case 1: t->active_OFDM_symbol_duration = FFT_4K_6MHZ; break; // 597,33\B5sec (4k FFT mode for 6MHz CATV systems)
//case 2 ... 7: reserved_for_future_use //
default:t->active_OFDM_symbol_duration = FFT_4K_8MHZ; // defaulting to here to 8MHz CATV systems, as nothing better found so far.
}
switch(*bp & 0x07) { // guard_interval 3 bslbf
case 0: t->guard = GUARD_INTERVAL_1_128; break; //
case 1: t->guard = GUARD_INTERVAL_1_64; break; // not defined in linux dvb api, see extended_frontend.h
//case 2 ... 7: reserved_for_future_use //
default:t->guard = GUARD_INTERVAL_1_128; // defaulting to here to 1/128, as nothing better found so far.
}
bp++; descriptor_length--;
}
/*
* 20140626:
* - if center_frequency = 0 and other_frequency_flag not set -> set this flag explictly.
*/
void parse_terrestrial_delivery_system_descriptor(const unsigned char * buf,
struct transponder * t, fe_spectral_inversion_t inversion) {
uint32_t center_frequency;
struct cell* p;
bool known;
int i;
hd(buf);
if (t == NULL) return;
t->type = SCAN_TERRESTRIAL;
t->source = 0x5A;
t->delsys = SYS_DVBT;
t->inversion = inversion;
center_frequency = 10 * get_u32(buf + 2); // center_frequency 32 bslbf, 10Hz steps
if ((center_frequency < 50000000) || (center_frequency > 1000000000))
center_frequency = 0;
switch(buf[6] >> 5) { // bandwidth 3 bslbf
case 0: t->bandwidth = 8000000; break;
case 1: t->bandwidth = 7000000; break;
case 2: t->bandwidth = 6000000; break;
case 3: t->bandwidth = 5000000; break;
default:
moreverbose("undefined bandwidth value found, using 8000000.\n");
t->bandwidth = 8000000;
}
t->priority = (buf[6] >> 4) & 0x1; // priority 1 bslbf, 20140705: convert to bool.
t->time_slicing = ((buf[6] >> 3) & 0x1) == 0; // Time_Slicing_indicator 1 bslbf 20140705: convert to bool.
t->mpe_fec = ((buf[6] >> 2) & 0x1) == 0; // MPE-FEC_indicator 1 bslbf 20140705: convert to bool.
// reserved_future_use 2 bslbf
switch(buf[7] >> 6) { // constellation 2 bslbf
case 0: t->modulation = QPSK; break;
case 1: t->modulation = QAM_16; break;
case 2: t->modulation = QAM_64; break;
default:
moreverbose("undefined modulation value found, using QAM_AUTO.\n");
t->modulation = QAM_AUTO;
}
switch((buf[7] >> 3) & 0x7) { // hierarchy_information 3 bslbf
// what about alpha here?
case 0: t->hierarchy = HIERARCHY_NONE; break; //non-hierarchical, native interleaver
case 1: t->hierarchy = HIERARCHY_1; break; //alpha = 1, native interleaver
case 2: t->hierarchy = HIERARCHY_2; break; //alpha = 2, native interleaver
case 3: t->hierarchy = HIERARCHY_4; break; //alpha = 4, native interleaver
case 4: t->hierarchy = HIERARCHY_NONE; break; //non-hierarchical, in-depth interleaver
case 5: t->hierarchy = HIERARCHY_1; break; //alpha = 1, in-depth interleaver
case 6: t->hierarchy = HIERARCHY_2; break; //alpha = 2, in-depth interleaver
case 7: t->hierarchy = HIERARCHY_4; break; //alpha = 4, in-depth interleaver
default:t->hierarchy = HIERARCHY_NONE;
}
switch(buf[7] & 0x7) { // code_rate-HP_stream 3 bslbf
case 0: t->coderate = FEC_1_2; break;
case 1: t->coderate = FEC_2_3; break;
case 2: t->coderate = FEC_3_4; break;
case 3: t->coderate = FEC_5_6; break;
case 4: t->coderate = FEC_7_8; break;
default:
moreverbose("undefined coderate HP, using FEC_AUTO\n");
t->coderate = FEC_AUTO;
}
switch((buf[8] >> 5) & 0x7) { // code_rate-LP_stream 3 bslbf
case 0: t->coderate_LP = FEC_1_2; break;
case 1: t->coderate_LP = FEC_2_3; break;
case 2: t->coderate_LP = FEC_3_4; break;
case 3: t->coderate_LP = FEC_5_6; break;
case 4: t->coderate_LP = FEC_7_8; break;
default:
moreverbose("undefined coderate LP, using FEC_AUTO\n");
t->coderate_LP = FEC_AUTO;
}
if (t->hierarchy == HIERARCHY_NONE)
t->coderate_LP = FEC_NONE;
switch((buf[8] >> 3) & 0x3) { // guard_interval 2 bslbf
case 0: t->guard = GUARD_INTERVAL_1_32; break;
case 1: t->guard = GUARD_INTERVAL_1_16; break;
case 2: t->guard = GUARD_INTERVAL_1_8; break;
case 3: t->guard = GUARD_INTERVAL_1_4; break;
default:;
}
switch((buf[8] >> 1) & 0x3) { // transmission_mode 2 bslbf
case 0: t->transmission = TRANSMISSION_MODE_2K; break;
case 1: t->transmission = TRANSMISSION_MODE_8K; break;
case 2: t->transmission = TRANSMISSION_MODE_4K; break;
default:
moreverbose("undefined transmission mode, using TRANSMISSION_MODE_AUTO\n");
t->transmission = TRANSMISSION_MODE_AUTO;
}
t->other_frequency_flag = ((buf[8] & 0x01) != 0); // other_frequency_flag 1 bslbf
// reserved_future_use 32 bslbf
// ----------------------------------------------------------------------------
if (center_frequency > 0) { // now: add center freq.
if (! t->other_frequency_flag)
t->frequency = center_frequency;
else {
known = false;
for(p = (t->cells)->first; p; p = p->next) {
for(i = 0; i < p->num_center_frequencies; i++)
if (p->center_frequencies[i] == center_frequency) {
known = true;
break;
}
for(i = 0; i < p->num_transposers; i++) {
if (p->transposers[i].transposer_frequency == center_frequency) {
known = true;
break;
}
}
}
if (! known) {
p = calloc(1, sizeof(*p));
p->num_center_frequencies = 1;
p->center_frequencies[0] = center_frequency;
AddItem(t->cells, p);
}
} // end other_frequency_flag
} // end if center_frequency > 0
if ((t->frequency == 0) && (t->other_frequency_flag == 0)) {
moreverbose("%s: center_freq = 0 && other_frequency_flag = 0 -> set other_frequency_flag = 1\n", __FUNCTION__);
t->other_frequency_flag = 1;
}
if (verbosity >= 4) {
verbose(" F%u B%u %s C%d D%d G%d T%d other_frequency=%d (%u)\n",
freq_scale(t->frequency, 1e-3),
freq_scale(t->bandwidth, 1e-6),
(t->modulation == QPSK)?"QPSK":
(t->modulation == QAM_16)?"M16":"M64",
(t->coderate == FEC_1_2)?12:
(t->coderate == FEC_2_3)?23:
(t->coderate == FEC_3_4)?34:
(t->coderate == FEC_5_6)?56:
(t->coderate == FEC_7_8)?78:999,
(t->coderate_LP == FEC_1_2)?12:
(t->coderate_LP == FEC_2_3)?23:
(t->coderate_LP == FEC_3_4)?34:
(t->coderate_LP == FEC_5_6)?56:
(t->coderate_LP == FEC_7_8)?78:999,
(t->guard==GUARD_INTERVAL_1_32 )? 32:
(t->guard==GUARD_INTERVAL_1_16 )? 16:
(t->guard==GUARD_INTERVAL_1_8 )? 8:4,
(t->transmission == TRANSMISSION_MODE_2K )?2:
(t->transmission == TRANSMISSION_MODE_8K )?8:4,
t->other_frequency_flag,t->other_frequency_flag?center_frequency:0
);
verbose(" %u cells\n", (t->cells)->count);
i = 0;
for(p = (t->cells)->first; p; p = p->next, ++i) {
int n;
for(n = 0; n < p->num_center_frequencies; n++)
verbose(" cell %u: center_frequency %7.3f\n",
p->cell_id, p->center_frequencies[n]/1000000.0);
for(n = 0; n < p->num_transposers; n++) {
verbose(" transposer %u transposer_frequency %7.3f\n",
p->transposers[n].cell_id_extension,
p->transposers[n].transposer_frequency/1000000.0);
}
}
}
} //end parse_terrestrial_delivery_system_descriptor
void parse_frequency_list_descriptor(const unsigned char * buf, struct transponder * t) {
uint8_t i, j, coding_type = (buf[2] & 0x03);
uint8_t num_frequencies = (buf[1] - 1) / 4;
uint32_t f;
bool known;
struct cell* p;
if (t == NULL) return;
hd(buf);
buf += 3;
for(i = 0; i < num_frequencies; ++i) {
switch(coding_type) {
case 1:
f = 10 * bcd32_to_cpu (buf[0], buf[1], buf[2], buf[3]);
break;
case 2:
f = 100 * bcd32_to_cpu (buf[0], buf[1], buf[2], buf[3]);
break;
case 3:
f = 10 * ((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
break;
default:
f = 0;
}
buf += 4;
if (f == 0) continue;
known = false;
for(p = (t->cells)->first; p; p = p->next) {
for(j = 0; j < p->num_center_frequencies; j++)
if (p->center_frequencies[j] == f) {
known = true;
break;
}
for(j = 0; j < p->num_transposers; j++) {
if (p->transposers[j].transposer_frequency == f) {
known = true;
break;
}
}
}
if (! known) {
p = calloc(1, sizeof(*p));
p->num_center_frequencies = 1;
p->center_frequencies[0] = f;
AddItem(t->cells, p);
}
} // end freq loop
if (verbosity >= 4) {
verbose(" %u cells\n", (t->cells)->count);
i = 0;
for(p = (t->cells)->first; p; p = p->next, ++i) {
int n;
for(n = 0; n < p->num_center_frequencies; n++)
verbose(" cell%d: center_frequency%u\n", i, p->center_frequencies[n]);
for(n = 0; n < p->num_transposers; n++) {
verbose(" transposer%d transposer_frequency%u\n", n, p->transposers[n].transposer_frequency);
}
}
}
}
/* DVB-T2: PRELIMINARY && UNTESTED CODE ONLY. I NEED SOMEBODY WITH ACCESS TO
* DVB-T2. IF YOU WANT TO HELP PLS CONTACT ME. --20111204, wirbel--
* 20140626:
* - if center_frequency = 0 and other_frequency_flag not set -> set this flag explictly.
*/
void parse_T2_delivery_system_descriptor(const unsigned char * buf,
struct transponder * t, fe_spectral_inversion_t inversion) {
unsigned char * bp;
__u8 descriptor_length;
__u8 frequency_loop_length = 0;
__u8 subcell_info_loop_length = 0;
__u32 center_frequency = 0;
struct cell* p;
if (t == NULL) return;
hd(buf);