OSDN Git Service

0f8d12ace2da2546dbc2752c87af37387291d777
[rec10/rec10-git.git] / epgdump / sdt.c
1 // -*- tab-width:4 -*-
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 #include "sdt.h"
8 #include "ts_ctl.h"
9
10 int parseSDThead(unsigned char *data, SDThead *h) {
11         int boff = 0;
12
13         memset(h, 0, sizeof(SDThead));
14
15         boff = 0;
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);
29
30         return 11;
31 }
32
33 int parseSDTbody(unsigned char *data, SDTbody *b) {
34         int boff = 0;
35
36         memset(b, 0, sizeof(SDTbody));
37
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);
46
47         return 5;
48 }
49
50 int parseSVCdesc(unsigned char *data, SVCdesc *desc) {
51 //0x48のサービス記述子、放送局の名前などが入っているよう
52         int boff = 0;
53
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);
62
63         return desc->descriptor_length + 2;
64 }
65
66 int parseLOGdesc(unsigned char *data, LOGdesc *desc) {
67 //0xC1のデジタルコピー制御記述子
68 //0xCFのロゴ伝送記述子
69         int boff = 0;
70
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);
81         }
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);
85         }
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);
89         }
90
91         return desc->descriptor_length + 2;
92 }
93
94 int serachid(SVT_CONTROL *top, int service_id)
95 {
96         SVT_CONTROL     *cur = top ;
97         while(cur != NULL){
98                 if(cur->event_id == service_id){
99                         return 1 ;
100                 }
101                 cur = cur->next ;
102         }
103         return 0 ;
104 }
105
106 void enqueue_sdt(SVT_CONTROL *top, SVT_CONTROL *sdtptr)
107 {
108         SVT_CONTROL     *cur ;
109
110         if(top->next == NULL){
111                 top->next = sdtptr ;
112                 top->prev = top ;
113                 return ;
114         }
115         cur = top->next ;
116         while(cur != NULL){
117                 if(sdtptr->event_id < cur->event_id){
118                         if(cur->prev != NULL){
119                                 cur->prev->next = sdtptr ;
120                                 sdtptr->prev = cur->prev ;
121                         }
122                         cur->prev = sdtptr ;
123                         sdtptr->next = cur ;
124                         return ;
125                 }
126                 if(cur->next == NULL){
127                         cur->next = sdtptr ;
128                         sdtptr->prev = cur ;
129                         return ;
130                 }
131                 cur = cur->next ;
132         }
133         return ;
134
135 }
136
137 void dumpSDT(unsigned char *ptr, SVT_CONTROL *top,STATION **station, int * station_count,char *ontvheader)
138 {
139
140         SDThead  sdth;
141         SDTbody  sdtb;
142         SVCdesc  desc;
143         LOGdesc  logd;
144         SVT_CONTROL     *svtptr ;
145         STATION * pStation = *station;
146         int             rc ;
147
148         int len = 0;
149         int loop_len = 0;
150         int desc_len = 0;
151         char sid[80];
152         int stationi = *station_count;
153         int i = 0;
154
155         /* SDT */
156         len = parseSDThead(ptr, &sdth); 
157         ptr += len;
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);
162                 ptr += len;
163                 loop_len -= len;
164
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);
170                                 ptr += len;
171                                 desc_len -= len;
172
173                                 #if 0
174                                 printf("LOG=%d,%d,%d,%d\n",
175                                         logd.logo_transmission_type, logd.logo_id, 
176                                         logd.logo_version, logd.download_data_id);
177                                 #endif
178
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;
183                                         }
184                                 }
185
186                                 continue;
187                         }
188                         else if ( *ptr != 0x48 ) {
189                                 len = parseOTHERdesc(ptr);
190                                 ptr += len;
191                                 desc_len -= len;
192                                 continue;
193                         }
194                         len = parseSVCdesc(ptr, &desc);
195                         //printf("desc %d - %d = %d\n",desc_len,len,desc_len - len);
196                         ptr += len;
197                         desc_len -= len;
198
199                         rc = serachid(top, sdtb.service_id);
200                         if(rc == 0){
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);
208
209                                         sprintf(sid, "%s_%d", ontvheader, sdtb.service_id );
210                                         for (i=0; i<stationi; i++) {
211                                                 if ( !strcmp( pStation[i].ontv, sid ) ) {
212                                                         // 既に同じものが含まれるのでスキップ
213                                                         break;
214                                                 }
215                                         }
216
217                                         pStation = realloc(pStation, (stationi + 1) * sizeof(STATION));
218                                         pStation[stationi].name = malloc( strlen(desc.service_name) + 1 );
219                                         pStation[stationi].ontv = malloc( strlen(sid) + 1 );
220                                         pStation[stationi].tsId = sdth.transport_stream_id;
221                                         pStation[stationi].onId = sdth.original_network_id;
222                                         pStation[stationi].svId = sdtb.service_id;
223                                         memset(pStation[stationi].logo_array, 0, sizeof(pStation[stationi].logo_array));
224
225                                         strcpy(pStation[stationi].name, desc.service_name);
226                                         strcpy(pStation[stationi].ontv, sid);
227
228                                         stationi++;
229                                 }
230
231 #if 0
232                                 printf("STATION=%s,%d,%d,%d,%d,%d\n",
233                                         desc.service_name,sdtb.service_id,sdth.transport_stream_id,
234                                         sdth.original_network_id,sdtb.service_id,desc.service_type);
235
236                                 printf("SDT=%s,%d,%x,%x,%x,%x,%x,%x,%x\n",
237                                         desc.service_name, sdtb.service_id, sdtb.reserved_future_use1,
238                                         sdtb.EIT_user_defined_flags, sdtb.EIT_schedule_flag, sdtb.EIT_present_following_flag,
239                                         sdtb.running_status, sdtb.free_CA_mode, sdtb.descriptors_loop_length);
240 /*
241 0x01:デジタルTVサービス
242 0xA5:プロモーション映像サービス
243 0x0C:データサービス
244 */
245                                 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",
246                                                 sdth.table_id, desc.service_type, 
247                                                 desc.service_name, sdtb.service_id,
248                                                 desc.descriptor_tag, desc.descriptor_length, desc.service_type,
249                                                 desc.service_provider_name_length, desc.service_name_length,
250                                                 sdth.table_id, sdth.section_syntax_indicator, sdth.reserved_future_use1,
251                                                 sdth.reserved1, sdth.section_length, sdth.transport_stream_id,
252                                                 sdth.reserved2, sdth.version_number, sdth.current_next_indicator,
253                                                 sdth.section_number, sdth.last_section_number, sdth.original_network_id,
254                                                 sdth.reserved_future_use2);
255 #endif
256                         }
257                 }
258 /*
259                 //ptr += sdtb.descriptors_loop_length;
260                 loop_len -= sdtb.descriptors_loop_length;
261                 
262                 if (loop_len>0){
263                         ptr += sdtb.descriptors_loop_length;
264                 }
265 */
266         }
267         *station = pStation;
268         *station_count = stationi;
269         //printf("stationi %d -",stationi);//stationi==294で落ちる
270         return;
271 }
272