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);
179 if ( logd.logo_transmission_type != 0x01 ) continue;
180 for (i=0; i<stationi; i++) {
181 if ( pStation[i].svId == sdtb.service_id ) {
182 pStation[i].logo_download_data_id = logd.download_data_id;
188 else if ( *ptr != 0x48 ) {
189 len = parseOTHERdesc(ptr);
194 len = parseSVCdesc(ptr, &desc);
195 //printf("desc %d - %d = %d\n",desc_len,len,desc_len - len);
199 rc = serachid(top, sdtb.service_id);
201 svtptr = calloc(1, sizeof(SVT_CONTROL));
202 svtptr->original_network_id = sdth.original_network_id;
203 svtptr->transport_stream_id = sdth.transport_stream_id;
204 svtptr->event_id = sdtb.service_id;
205 memcpy(svtptr->servicename, desc.service_name, strlen(desc.service_name));
206 if (desc.service_type == 1){
207 enqueue_sdt(top, svtptr);
209 sprintf(sid, "%s_%d", ontvheader, sdtb.service_id );
210 for (i=0; i<stationi; i++) {
211 if ( !strcmp( pStation[i].ontv, sid ) ) {
217 pStation = realloc(pStation,(stationi+1)*sizeof(STATION));
218 if (!pStation) printf( "Realloc returned NULL!!! stationi = %d\n", stationi);
219 pStation[stationi].name = malloc( strlen(desc.service_name) + 1 );
220 pStation[stationi].ontv = malloc( strlen(sid) + 1 );
221 pStation[stationi].tsId = sdth.transport_stream_id;
222 pStation[stationi].onId = sdth.original_network_id;
223 pStation[stationi].svId = sdtb.service_id;
224 memset(pStation[stationi].logo_array, 0, sizeof(pStation[stationi].logo_array));
226 strcpy(pStation[stationi].name, desc.service_name);
227 strcpy(pStation[stationi].ontv, sid);
233 printf("STATION=%s,%d,%d,%d,%d,%d\n",
234 desc.service_name,sdtb.service_id,sdth.transport_stream_id,
235 sdth.original_network_id,sdtb.service_id,desc.service_type);
237 printf("SDT=%s,%d,%x,%x,%x,%x,%x,%x,%x\n",
238 desc.service_name, sdtb.service_id, sdtb.reserved_future_use1,
239 sdtb.EIT_user_defined_flags, sdtb.EIT_schedule_flag, sdtb.EIT_present_following_flag,
240 sdtb.running_status, sdtb.free_CA_mode, sdtb.descriptors_loop_length);
246 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",
247 sdth.table_id, desc.service_type,
248 desc.service_name, sdtb.service_id,
249 desc.descriptor_tag, desc.descriptor_length, desc.service_type,
250 desc.service_provider_name_length, desc.service_name_length,
251 sdth.table_id, sdth.section_syntax_indicator, sdth.reserved_future_use1,
252 sdth.reserved1, sdth.section_length, sdth.transport_stream_id,
253 sdth.reserved2, sdth.version_number, sdth.current_next_indicator,
254 sdth.section_number, sdth.last_section_number, sdth.original_network_id,
255 sdth.reserved_future_use2);
260 //ptr += sdtb.descriptors_loop_length;
261 loop_len -= sdtb.descriptors_loop_length;
264 ptr += sdtb.descriptors_loop_length;
269 *station_count = stationi;
270 //printf("stationi %d -",stationi);//stationi==294で落ちる