10 int parseSDThead(unsigned char *data, SDThead *h) {
13 memset(h, 0, sizeof(SDThead));
16 h->table_id = getBit(data, &boff, 8);
17 h->section_syntax_indicator = getBit(data, &boff, 1);
18 h->reserved_future_use1 = getBit(data, &boff, 1);
19 h->reserved1 = getBit(data, &boff, 2);
20 h->section_length = getBit(data, &boff, 12);
21 h->transport_stream_id = getBit(data, &boff, 16);
22 h->reserved2 = getBit(data, &boff, 2);
23 h->version_number = getBit(data, &boff, 5);
24 h->current_next_indicator = getBit(data, &boff, 1);
25 h->section_number = getBit(data, &boff, 8);
26 h->last_section_number = getBit(data, &boff, 8);
27 h->original_network_id = getBit(data, &boff, 16);
28 h->reserved_future_use2 = getBit(data, &boff, 8);
33 int parseSDTbody(unsigned char *data, SDTbody *b) {
36 memset(b, 0, sizeof(SDTbody));
38 b->service_id = getBit(data, &boff, 16);
39 b->reserved_future_use1 = getBit(data, &boff, 3);
40 b->EIT_user_defined_flags = getBit(data, &boff, 3);
41 b->EIT_schedule_flag = getBit(data, &boff, 1);
42 b->EIT_present_following_flag = getBit(data, &boff, 1);
43 b->running_status = getBit(data, &boff, 3);
44 b->free_CA_mode = getBit(data, &boff, 1);
45 b->descriptors_loop_length = getBit(data, &boff, 12);
50 int parseSVCdesc(unsigned char *data, SVCdesc *desc) {
51 //0x48のサービス記述子、放送局の名前などが入っているよう
54 memset(desc, 0, sizeof(SVCdesc));
55 desc->descriptor_tag = getBit(data, &boff, 8);
56 desc->descriptor_length = getBit(data, &boff, 8);
57 desc->service_type = getBit(data, &boff, 8);
58 desc->service_provider_name_length = getBit(data, &boff, 8);
59 getStr(desc->service_provider_name, data, &boff, desc->service_provider_name_length);
60 desc->service_name_length = getBit(data, &boff, 8);
61 getStr(desc->service_name, data, &boff, desc->service_name_length);
63 return desc->descriptor_length + 2;
66 int parseLOGdesc(unsigned char *data, LOGdesc *desc) {
71 memset(desc, 0, sizeof(LOGdesc));
72 desc->descriptor_tag = getBit(data, &boff, 8);
73 desc->descriptor_length = getBit(data, &boff, 8);
74 desc->logo_transmission_type = getBit(data, &boff, 8);
75 if ( desc->logo_transmission_type == 0x01 ) {
76 desc->reserved_future_use1 = getBit(data, &boff, 7);
77 desc->logo_id = getBit(data, &boff, 9);
78 desc->reserved_future_use2 = getBit(data, &boff, 4);
79 desc->logo_version = getBit(data, &boff, 12);
80 desc->download_data_id = getBit(data, &boff, 16);
82 else if ( desc->logo_transmission_type == 0x02 ) {
83 desc->reserved_future_use1 = getBit(data, &boff, 7);
84 desc->logo_id = getBit(data, &boff, 9);
86 else if ( desc->logo_transmission_type == 0x03 ) {
87 memcpy(desc->logo_char, data + boff / 8, desc->descriptor_length);
88 // getStr(desc->logo_char, data, &boff, desc->descriptor_length);
91 return desc->descriptor_length + 2;
94 int serachid(SVT_CONTROL *top, int service_id)
96 SVT_CONTROL *cur = top ;
98 if(cur->event_id == service_id){
106 void enqueue_sdt(SVT_CONTROL *top, SVT_CONTROL *sdtptr)
110 if(top->next == NULL){
117 if(sdtptr->event_id < cur->event_id){
118 if(cur->prev != NULL){
119 cur->prev->next = sdtptr ;
120 sdtptr->prev = cur->prev ;
126 if(cur->next == NULL){
137 void dumpSDT(unsigned char *ptr, SVT_CONTROL *top,STATION **station, int * station_count,char *ontvheader)
144 SVT_CONTROL *svtptr ;
145 STATION * pStation = *station;
152 int stationi = *station_count;
156 len = parseSDThead(ptr, &sdth);
158 loop_len = sdth.section_length - (len - 3 + 4); // 3は共通ヘッダ長 4はCRC
159 while(loop_len > 0) {
160 len = parseSDTbody(ptr, &sdtb);
161 //printf("body %d - %d = %d\n",loop_len,len,loop_len - len);
165 desc_len = sdtb.descriptors_loop_length;
166 loop_len -= desc_len;
167 while(desc_len > 0) {
168 if ( *ptr == 0xCF ) {
169 len = parseLOGdesc(ptr, &logd);
174 printf("LOG=%d,%d,%d,%d\n",
175 logd.logo_transmission_type, logd.logo_id,
176 logd.logo_version, logd.download_data_id);
180 logd.logo_transmission_type
181 0x01 CDT 伝送方式1:CDT をダウンロードデータ識別で直接参照する場合
182 0x02 CDT 伝送方式2:CDT をロゴ識別を用いてダウンロードデータ識別を間接的に参照する場合
185 if ( logd.logo_transmission_type != 0x01 ) continue;
186 for (i=0; i<stationi; i++) {
187 if ( pStation[i].svId == sdtb.service_id && pStation[i].logo_version < logd.logo_version ) {
188 pStation[i].logo_download_data_id = logd.download_data_id;
189 pStation[i].logo_version = logd.logo_version;
195 else if ( *ptr != 0x48 ) {
196 len = parseOTHERdesc(ptr);
201 len = parseSVCdesc(ptr, &desc);
202 //printf("desc %d - %d = %d\n",desc_len,len,desc_len - len);
206 rc = serachid(top, sdtb.service_id);
208 svtptr = calloc(1, sizeof(SVT_CONTROL));
209 svtptr->original_network_id = sdth.original_network_id;
210 svtptr->transport_stream_id = sdth.transport_stream_id;
211 svtptr->event_id = sdtb.service_id;
212 memcpy(svtptr->servicename, desc.service_name, strlen(desc.service_name));
213 if (desc.service_type == 1){
214 enqueue_sdt(top, svtptr);
216 sprintf(sid, "%s_%d", ontvheader, sdtb.service_id );
217 for (i=0; i<stationi; i++) {
218 if ( !strcmp( pStation[i].ontv, sid ) ) {
224 pStation = realloc(pStation, (stationi + 1) * sizeof(STATION));
225 memset(&pStation[stationi], 0, sizeof(STATION));
227 pStation[stationi].name = malloc( strlen(desc.service_name) + 1 );
228 pStation[stationi].ontv = malloc( strlen(sid) + 1 );
229 pStation[stationi].tsId = sdth.transport_stream_id;
230 pStation[stationi].onId = sdth.original_network_id;
231 pStation[stationi].svId = sdtb.service_id;
233 strcpy(pStation[stationi].name, desc.service_name);
234 strcpy(pStation[stationi].ontv, sid);
240 printf("STATION=%s,%d,%d,%d,%d,%d\n",
241 desc.service_name,sdtb.service_id,sdth.transport_stream_id,
242 sdth.original_network_id,sdtb.service_id,desc.service_type);
244 printf("SDT=%s,%d,%x,%x,%x,%x,%x,%x,%x\n",
245 desc.service_name, sdtb.service_id, sdtb.reserved_future_use1,
246 sdtb.EIT_user_defined_flags, sdtb.EIT_schedule_flag, sdtb.EIT_present_following_flag,
247 sdtb.running_status, sdtb.free_CA_mode, sdtb.descriptors_loop_length);
253 printf("SDT=(%x:%x)%s,%d,%d,%d,%d,%d(%d,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x)\n",
254 sdth.table_id, desc.service_type,
255 desc.service_name, sdtb.service_id,
256 desc.descriptor_tag, desc.descriptor_length, desc.service_type,
257 desc.service_provider_name_length, desc.service_name_length,
258 sdth.table_id, sdth.section_syntax_indicator, sdth.reserved_future_use1,
259 sdth.reserved1, sdth.section_length, sdth.transport_stream_id,
260 sdth.reserved2, sdth.version_number, sdth.current_next_indicator,
261 sdth.section_number, sdth.last_section_number, sdth.original_network_id,
262 sdth.reserved_future_use2);
267 //ptr += sdtb.descriptors_loop_length;
268 loop_len -= sdtb.descriptors_loop_length;
271 ptr += sdtb.descriptors_loop_length;
276 *station_count = stationi;
277 //printf("stationi %d -",stationi);//stationi==294で落ちる