root/trunk/epgdumpr2/eit.c

リビジョン 136, 14.8 kB (コミッタ: sorshi, コミット時期: 13 年 前)

foltiaで利用しているepgdumpを追加

Line 
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "eit.h"
6
7 char            *subtitle_cnv_str[] = {
8         NULL
9 };
10 static void timecmp(int *,int *,int *,
11                                         int, int, int);
12
13 int parseEIThead(unsigned char *data, EIThead *h) {
14         int boff = 0;
15
16         memset(h, 0, sizeof(EIThead));
17
18         h->table_id = getBit(data, &boff, 8);
19         h->section_syntax_indicator = getBit(data, &boff, 1);
20         h->reserved_future_use = getBit(data, &boff, 1);
21         h->reserved1 = getBit(data, &boff, 2);
22         h->section_length =getBit(data, &boff,12);
23         h->service_id = getBit(data, &boff, 16);
24         h->reserved2 = getBit(data, &boff, 2);
25         h->version_number = getBit(data, &boff, 5);
26         h->current_next_indicator = getBit(data, &boff, 1);
27         h->section_number = getBit(data, &boff, 8);
28         h->last_section_number = getBit(data, &boff, 8);
29         h->transport_stream_id = getBit(data, &boff, 16);
30         h->original_network_id = getBit(data, &boff, 16);
31         h->segment_last_section_number = getBit(data, &boff, 8);
32         h->last_table_id = getBit(data, &boff, 8);
33  
34         return 14;
35 }
36
37 int parseEITbody(unsigned char *data, EITbody *b)
38 {
39         int boff = 0;
40         int tnum;
41         char buf[4];
42
43         memset(b, 0, sizeof(EITbody));
44
45         b->event_id = getBit(data, &boff, 16);
46
47         memcpy(b->start_time, data + boff / 8, 5);
48         /* b->start_time = getBit(data, &boff, 40); */
49         boff += 40;
50         memcpy(b->duration, data + boff / 8, 3);
51         /* b->duration = getBit(data, &boff, 24); */
52         boff += 24;
53         b->running_status = getBit(data, &boff, 3);
54         b->free_CA_mode = getBit(data, &boff, 1);
55         b->descriptors_loop_length = getBit(data, &boff, 12);
56
57         /* �ヤ�紊��*/
58         tnum = (b->start_time[0] & 0xFF) << 8 | (b->start_time[1] & 0xFF);
59  
60         b->yy = (tnum - 15078.2) / 365.25;
61         b->mm = ((tnum - 14956.1) - (int)(b->yy * 365.25)) / 30.6001;
62         b->dd = (tnum - 14956) - (int)(b->yy * 365.25) - (int)(b->mm * 30.6001);
63
64         if(b->dd == 0) {
65                 printf("aa");
66         }
67
68         if(b->mm == 14 || b->mm == 15) {
69                 b->yy += 1;
70                 b->mm = b->mm - 1 - (1 * 12);
71         } else {
72                 b->mm = b->mm - 1;
73         }
74
75         b->yy += 1900;
76  
77         memset(buf, '\0', sizeof(buf));
78         sprintf(buf, "%x", b->start_time[2]);
79         b->hh = atoi(buf);
80         memset(buf, '\0', sizeof(buf));
81         sprintf(buf, "%x", b->start_time[3]);
82         b->hm = atoi(buf);
83         memset(buf, '\0', sizeof(buf));
84         sprintf(buf, "%x", b->start_time[4]);
85         b->ss = atoi(buf);
86
87         if((b->duration[0] == 0xFF) && (b->duration[1] == 0xFF) && (b->duration[2] == 0xFF)){
88                 b->dhh = b->dhm = b->dss = 0;
89         }else{
90                 memset(buf, '\0', sizeof(buf));
91         sprintf(buf, "%x", b->duration[0]);
92         b->dhh = atoi(buf);
93                 memset(buf, '\0', sizeof(buf));
94         sprintf(buf, "%x", b->duration[1]);
95         b->dhm = atoi(buf);
96                 memset(buf, '\0', sizeof(buf));
97         sprintf(buf, "%x", b->duration[2]);
98         b->dss = atoi(buf);
99         }
100         return 12;
101 }
102
103 int parseSEVTdesc(unsigned char *data, SEVTdesc *desc) {
104         int boff = 0;
105  
106         memset(desc, 0, sizeof(SEVTdesc));
107
108         desc->descriptor_tag = getBit(data, &boff, 8);
109         if((desc->descriptor_tag & 0xFF) != 0x4D) {
110                 return -1;
111         }
112         desc->descriptor_length = getBit(data, &boff, 8);
113         memcpy(desc->ISO_639_language_code, data + boff / 8, 3);
114         /* desc->ISO_639_language_code = getBit(data, &boff, 24); */
115         boff += 24;
116         desc->event_name_length = getBit(data, &boff, 8);
117         getStr(desc->event_name, data, &boff, desc->event_name_length);
118         desc->text_length = getBit(data, &boff, 8);
119         getStr(desc->text, data, &boff, desc->text_length);
120
121         return desc->descriptor_length + 2;
122 }
123
124 int parseContentDesc(unsigned char *data, ContentDesc *desc) {
125         int boff = 0;
126  
127         memset(desc, 0, sizeof(ContentDesc));
128
129         desc->descriptor_tag = getBit(data, &boff, 8);
130         if((desc->descriptor_tag & 0xFF) != 0x54) {
131                 return -1;
132         }
133         desc->descriptor_length = getBit(data, &boff, 8);
134         memcpy(desc->content, data+(boff/8), desc->descriptor_length);
135         //getStr(desc->content, data, &boff, desc->descriptor_length);
136         return desc->descriptor_length + 2;
137 }
138
139 int parseSeriesDesc(unsigned char *data, SeriesDesc *desc) {
140         int boff = 0;
141  
142         memset(desc, 0, sizeof(SeriesDesc));
143
144         desc->descriptor_tag = getBit(data, &boff, 8);
145         if((desc->descriptor_tag & 0xFF) != 0xD5) {
146                 return -1;
147         }
148         desc->descriptor_length = getBit(data, &boff, 8);
149         desc->series_id = getBit(data, &boff, 16);
150         desc->repeat_label = getBit(data, &boff, 4);
151         desc->program_pattern = getBit(data, &boff, 3);
152         desc->expire_date_valid_flag = getBit(data, &boff, 1);
153
154         desc->expire_date = getBit(data, &boff, 16);
155         //memcpy(desc->expire_date, data + boff / 8, 2);
156         //boff += 16;
157
158         desc->episode_number = getBit(data, &boff, 12);
159         desc->last_episode_number = getBit(data, &boff, 12);
160
161         getStr(desc->series_name_char, data, &boff, desc->descriptor_length - 8);
162         return desc->descriptor_length + 2;
163 }
164
165 int parseEEVTDhead(unsigned char *data, EEVTDhead *desc) {
166         int boff = 0;
167  
168         memset(desc, 0, sizeof(EEVTDhead));
169
170         desc->descriptor_tag = getBit(data, &boff, 8);
171         if((desc->descriptor_tag & 0xFF) != 0x4E) {
172                 return -1;
173         }
174         desc->descriptor_length = getBit(data, &boff, 8);
175         desc->descriptor_number = getBit(data, &boff, 4);
176         desc->last_descriptor_number = getBit(data, &boff, 4);
177         memcpy(desc->ISO_639_language_code, data + boff / 8, 3);
178         /* desc->ISO_639_language_code = getBit(data, &boff, 24); */
179         boff += 24;
180
181         desc->length_of_items = getBit(data, &boff, 8);
182
183         return 7;
184 }
185
186 int parseEEVTDitem(unsigned char *data, EEVTDitem *desc) {
187         int boff = 0;
188  
189         memset(desc, 0, sizeof(EEVTDitem));
190
191         desc->item_description_length = getBit(data, &boff, 8);
192         getStr(desc->item_description, data, &boff, desc->item_description_length);
193
194         desc->item_length = getBit(data, &boff, 8);
195         memcpy(desc->item, data + (boff / 8), desc->item_length);
196         /* getStr(desc->item, data, &boff, desc->item_length); */
197
198         return desc->item_description_length + desc->item_length + 2;
199 }
200
201 int parseEEVTDtail(unsigned char *data, EEVTDtail *desc) {
202         int boff = 0;
203  
204         memset(desc, 0, sizeof(EEVTDtail));
205
206         desc->text_length = getBit(data, &boff, 8);
207         getStr(desc->text, data, &boff, desc->text_length);
208
209         return desc->text_length + 1;
210 }
211
212 int checkEEVTDitem(EEVTDitem *save, EEVTDitem *new, int descriptor_number) {
213
214         EEVTDitem swap;
215         int boff = 0;
216
217         if(new == NULL) {
218                 if(save->item_length != 0) {
219                         swap = *save;
220                         getStr(save->item, (unsigned char*)swap.item, &boff, swap.item_length);
221                         return 1;
222                 } else {
223                         return 0;
224                 }
225         }
226
227         if(new->item_description_length == 0) {
228                 /* 膓�� 篆�� */
229                 memcpy(save->item + save->item_length, new->item, new->item_length);
230                 save->item_length += new->item_length;
231                 return 0;
232         } else {
233                 /* ����若���ave����桁�莟<������ave������? */
234                 if(save->item_length != 0) {
235                         /* ���羝�������*/
236                         swap = *save;
237                         getStr(save->item, (unsigned char*)swap.item, &boff, swap.item_length);
238                         swap = *new;
239                         *new = *save;
240                         *save = swap;
241                         save->descriptor_number = descriptor_number;
242                 } else {
243                         *save = *new;
244                         save->descriptor_number = descriptor_number;
245                         return 0;
246                 }
247         }
248
249         return 1;
250 }
251 EIT_CONTROL     *searcheit(EIT_CONTROL *top, int servid, int eventid)
252 {
253         EIT_CONTROL     *cur ;
254         cur = top ;
255
256         while(cur != NULL){
257                 if((cur->event_id == eventid) && (cur->servid == servid)){
258                         return cur ;
259                 }
260
261                 cur = cur->next ;
262         }
263         return NULL ;
264 }
265 char    *strstr_eucjp(const char *str, const char *search)
266 {
267         char *pos ;
268         pos = (char *)str ;
269
270         while (*pos != '\0') {
271                 if (*pos == *search) {
272                         if (strncmp(pos, search, strlen(search)) == 0) {
273                                 return pos ;
274                         }
275                 }
276                 if ((unsigned char)*pos == 0x8Fu) {
277                         pos += 3 ;
278                 } else if ((unsigned char)*pos >= 0x80u) {
279                         pos += 2 ;
280                 } else {
281                         pos += 1 ;
282                 }
283         }
284
285         return NULL ;
286 }
287 void    conv_title_subtitle(EIT_CONTROL *eitptr)
288 {
289         int             lp = 0 ;
290 //      size_t  addsize ;
291         char    *ptr ;
292         char    *ptr2 ;
293         char    *newsubtitle ;
294
295         for(lp = 0 ; subtitle_cnv_str[lp] != NULL ; lp++){
296                 ptr = strstr(eitptr->title, subtitle_cnv_str[lp]);
297                 if(ptr == NULL){
298                         continue ;
299                 }
300                 // �帥�������������������                if(ptr == eitptr->title){
301                         continue ;
302                 }
303                 ptr2 = ptr ;
304                 for( ; (unsigned char)*ptr2 == 0x20u ; ptr2++ );
305                 for( ; (unsigned char)*ptr2 == 0xA1u && (unsigned char)*(ptr2+1) == 0xA1u ; ptr2 += 2);
306                 for( ; (unsigned char)*ptr2 == 0x20u ; ptr2++ );
307                 newsubtitle = calloc(1, ((strlen(ptr2) + 2) + (strlen(eitptr->subtitle) + 1)));
308                 memcpy(newsubtitle, ptr2, strlen(ptr2));
309 //              *(newsubtitle+strlen(ptr)) = ' ';
310                 strcat(newsubtitle, "��);
311                 *ptr = '\0';
312                 strcat(newsubtitle, eitptr->subtitle);
313                 free(eitptr->subtitle);
314                 eitptr->subtitle = newsubtitle ;
315                 return ;
316         }
317 }
318 void    enqueue(EIT_CONTROL *top, EIT_CONTROL *eitptr)
319 {
320         EIT_CONTROL     *cur ;
321         cur = top ;
322         int             rc ;
323
324         if(top->next == NULL){
325                 top->next = eitptr ;
326                 eitptr->prev = top ;
327                 return ;
328         }
329         cur = top->next ;
330         while(cur != NULL){
331                 rc = memcmp(&cur->yy, &eitptr->yy, (sizeof(int) * 3));
332                 if(rc == 0){
333                         rc = memcmp(&cur->hh, &eitptr->hh, (sizeof(int) * 3));
334                         if(rc == 0){
335                                 free(eitptr->title);
336                                 free(eitptr->subtitle);
337                                 free(eitptr);
338                                 return ;
339                         }
340                         if(rc > 0){
341                                 if(cur->prev != 0){
342                                         cur->prev->next = eitptr ;
343                                         eitptr->prev = cur->prev ;
344                                 }
345                                 cur->prev = eitptr ;
346                                 eitptr->next = cur ;
347                                 conv_title_subtitle(eitptr);
348                                 return ;
349                         }
350                 }
351                 if(rc > 0){
352                         if(cur->prev != 0){
353                                 cur->prev->next = eitptr ;
354                                 eitptr->prev = cur->prev ;
355                         }
356                         cur->prev = eitptr ;
357                         eitptr->next = cur ;
358                         conv_title_subtitle(eitptr);
359                         return ;
360                 }
361                 if(cur->next == NULL){
362                         cur->next = eitptr ;
363                         eitptr->prev = cur ;
364                         conv_title_subtitle(eitptr);
365                         return ;
366                 }
367                 cur = cur->next ;
368         }
369         return ;
370
371 }
372
373 void dumpEIT(unsigned char *ptr, int serv_id, int original_network_id, int transport_stream_id, EIT_CONTROL *eittop)
374 {
375
376         EIThead  eith;
377         EITbody  eitb;
378         SEVTdesc sevtd;
379
380         EEVTDhead eevthead;
381         EEVTDitem eevtitem;
382         EEVTDtail eevttail;
383
384         EEVTDitem save_eevtitem;
385
386         EIT_CONTROL     *cur ;
387
388         int len = 0;
389         int loop_len = 0;
390         int loop_blen = 0;
391         int loop_elen = 0;
392
393         int ehh, emm, ess;
394
395         /* EIT */
396         len = parseEIThead(ptr, &eith);
397
398         ptr += len;
399         loop_len = eith.section_length - (len - 3 + 4); // 3�����������4��RC
400         while(loop_len > 0) {
401                 /* �g�����≦宍�ゃ��潟����羲√��潟����羈c��ャ����
402                    ���������������膓����������tem_description_length��              荐������������������ゆ��с���������*/
403                 memset(&save_eevtitem, 0, sizeof(EEVTDitem));
404
405                 len = parseEITbody(ptr, &eitb);
406                 ptr += len;
407                 loop_len -= len;
408     
409                 /* printf("evtid:%d\n", eitb.event_id); */
410     
411                 loop_blen = eitb.descriptors_loop_length;
412                 loop_len -= loop_blen;
413                 while(loop_blen > 0) {
414
415                         len = parseSEVTdesc(ptr, &sevtd);
416                         if(len > 0) {
417
418                                 /*
419                                   if(eith.service_id == 19304 &&
420                                   eitb.event_id == 46564) {
421                                   printf("aa");
422                                   }
423                                 */
424
425                                 ehh = eitb.hh;
426                                 emm = eitb.hm;
427                                 ess = eitb.ss;
428                                 if(eith.service_id != serv_id){
429                                         ptr += len;
430                                         loop_blen -= len;
431                                         continue ;
432                                 }
433
434                                 timecmp(&ehh, &emm, &ess,
435                                                 eitb.dhh, eitb.dhm, eitb.dss);
436                                 cur = searcheit(eittop, eith.service_id, eitb.event_id);
437                                 if(cur == NULL){
438                                         cur = calloc(1, sizeof(EIT_CONTROL));
439                                         cur->event_id = eitb.event_id ;
440                                         cur->servid = eith.service_id ;
441                                         cur->title = calloc(1, (strlen(sevtd.event_name) + 1));
442
443                                         memcpy(cur->title, sevtd.event_name, strlen(sevtd.event_name));
444                                         cur->subtitle = calloc(1, (strlen(sevtd.text) + 1));
445                                         memcpy(cur->subtitle, sevtd.text, strlen(sevtd.text));
446                                         cur->yy = eitb.yy;
447                                         cur->mm = eitb.mm;
448                                         cur->dd = eitb.dd;
449                                         cur->hh = eitb.hh;
450                                         cur->hm = eitb.hm;
451                                         cur->ss = eitb.ss;
452                                         cur->ehh = eitb.dhh;
453                                         cur->emm = eitb.dhm;
454                                         cur->ess = eitb.dss ;
455                                         cur->table_id = eith.table_id ;
456                                         enqueue(eittop, cur);
457                                 }
458                         } else {
459                                 len = parseEEVTDhead(ptr, &eevthead);
460
461                                 /*
462                                   if(eith.service_id == 19304 &&
463                                   eitb.event_id == 46564) {
464                                   printf("aa");
465                                   }
466                                 */
467
468
469                                 if(len > 0) {
470                                         ptr += len;
471                                         loop_blen -= len;
472
473                                         loop_elen = eevthead.length_of_items;
474                                         loop_len -= loop_elen;
475                                         while(loop_elen > 0) {
476                                                 len = parseEEVTDitem(ptr, &eevtitem);
477
478                                                 ptr += len;
479                                                 loop_elen -= len;
480                                                 loop_blen -= len;
481
482                                                 if(checkEEVTDitem(&save_eevtitem, &eevtitem,
483                                                                                   eevthead.descriptor_number)) {
484 #if 0
485                                                         if(mode == 1) { /* long format */
486                                                                 fprintf(out, "EEVT,%d,%d,%d,%s,%s\n",
487                                                                                 eith.service_id,
488                                                                                 eitb.event_id,
489                                                                                 eevtitem.descriptor_number, /* ������ */
490                                                                                 eevtitem.item_description,
491                                                                                 eevtitem.item);
492                                                         }
493 #endif
494                                                 }
495                                         }
496
497                                         len = parseEEVTDtail(ptr, &eevttail);
498 #if 0
499                                         if(mode == 1) { /* long format */
500                                                 fprintf(out, "EEVTt,%d,%d,%d,%s\n",
501                                                                 eith.service_id,
502                                                                 eitb.event_id,
503                                                                 eevthead.descriptor_number,
504                                                                 eevttail.text);
505                                         }
506 #endif
507                                 } else {
508                                         ContentDesc contentDesc;
509                                         len = parseContentDesc(ptr, &contentDesc);
510                                         if (len > 0) {
511 //                                              int header_printed = 0;
512                                                 for (int i = 0; i < contentDesc.descriptor_length - 1; i+=2) {
513                                                         /*
514                                                         if (0xff == (unsigned char)contentDesc.content[i])
515                                                                 continue;
516                                                         */
517 #if 0
518                                                         if (!header_printed) {
519                                                                 fprintf(out, "Content,%d,%d",
520                                                                         eith.service_id,
521                                                                         eitb.event_id);
522                                                                 header_printed = 1;
523                                                         }
524 #endif
525 #if 0
526                                                         fprintf(out, ",%02x%02x", (unsigned char)contentDesc.content[i], (unsigned char)contentDesc.content[i+1]);
527 #endif
528                                                 }
529                                                 if((eith.original_network_id == original_network_id) && (eith.transport_stream_id == transport_stream_id)){
530                                                         cur = searcheit(eittop, eith.service_id, eitb.event_id);
531                                                         if(cur != NULL){
532                                                                 cur->content_type = (unsigned char)(contentDesc.content[0] >> 4);
533 #if 0
534                                                                 fprintf(stdout, "%s:", cur->title);
535                                                                 fprintf(stdout, ",%02x%02x", (unsigned char)contentDesc.content[0], (unsigned char)contentDesc.content[1]);
536                                                                 fprintf(stdout, ",%02x%02x\n", (unsigned char)contentDesc.content[2], (unsigned char)contentDesc.content[3]);
537 #endif
538
539                                                         }
540 #if 0
541                                                         if (header_printed) {
542                                                                 fprintf(out, "\n");
543                                                         }
544 #endif
545                                                 }
546                                         } else {
547                                                 SeriesDesc seriesDesc;
548                                                 len = parseSeriesDesc(ptr, &seriesDesc);
549                                                 if (len > 0) {
550 #if 0
551                                                         fprintf(out, "Series,%d,%d,series=%d,repeat=%01x,pattern=%d,expire_valid=%d,expire=%04x,epinum=%d,lastepinum=%d,%s\n",
552                                                                 eith.service_id,
553                                                                 eitb.event_id,
554                                                                 seriesDesc.series_id,
555                                                                 seriesDesc.repeat_label,
556                                                                 seriesDesc.program_pattern,
557                                                                 seriesDesc.expire_date_valid_flag,
558                                                                 seriesDesc.expire_date,
559                                                                 seriesDesc.episode_number,
560                                                                 seriesDesc.last_episode_number,
561                                                                 seriesDesc.series_name_char);
562 #endif
563                                                 } else {
564                                                         len = parseOTHERdesc(ptr);
565                                                 }
566                                         }
567                                 }
568                         }
569                         ptr += len;
570                         loop_blen -= len;
571                 }
572                 /* �������������с���*/
573     
574                 if(checkEEVTDitem(&save_eevtitem, NULL, 0)) {
575 #if 0
576                         if(mode == 1) { /* long format */
577                                 fprintf(out, "EEVT,%d,%d,%d,%s,%s\n",
578                                                 eith.service_id,
579                                                 eitb.event_id,
580                                                 save_eevtitem.descriptor_number,
581                                                 save_eevtitem.item_description,
582                                                 save_eevtitem.item);
583                         }
584 #endif
585                 }
586         }
587
588         return;
589 }
590
591 void timecmp(int *thh, int *tmm, int *tss,
592                          int dhh, int dmm, int dss) {
593
594         int ama;
595
596         *tss += dss;
597         ama = *tss % 60;
598         *tmm += (*tss / 60);
599         *tss = ama;
600
601         *tmm += dmm;
602         ama   = *tmm % 60;
603         *thh += (*tmm / 60);
604         *tmm  = ama;
605
606         *thh += dhh;
607
608 }
609
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。
track feed