7 char *subtitle_cnv_str[] = {
18 static void timecmp(int *,int *,int *,
21 int parseEIThead(unsigned char *data, EIThead *h) {
24 memset(h, 0, sizeof(EIThead));
26 h->table_id = getBit(data, &boff, 8);
27 h->section_syntax_indicator = getBit(data, &boff, 1);
28 h->reserved_future_use = getBit(data, &boff, 1);
29 h->reserved1 = getBit(data, &boff, 2);
30 h->section_length =getBit(data, &boff,12);
31 h->service_id = getBit(data, &boff, 16);
32 h->reserved2 = getBit(data, &boff, 2);
33 h->version_number = getBit(data, &boff, 5);
34 h->current_next_indicator = getBit(data, &boff, 1);
35 h->section_number = getBit(data, &boff, 8);
36 h->last_section_number = getBit(data, &boff, 8);
37 h->transport_stream_id = getBit(data, &boff, 16);
38 h->original_network_id = getBit(data, &boff, 16);
39 h->segment_last_section_number = getBit(data, &boff, 8);
40 h->last_table_id = getBit(data, &boff, 8);
45 int parseEITbody(unsigned char *data, EITbody *b)
51 memset(b, 0, sizeof(EITbody));
53 b->event_id = getBit(data, &boff, 16);
55 memcpy(b->start_time, data + boff / 8, 5);
56 /* b->start_time = getBit(data, &boff, 40); */
58 memcpy(b->duration, data + boff / 8, 3);
59 /* b->duration = getBit(data, &boff, 24); */
61 b->running_status = getBit(data, &boff, 3);
62 b->free_CA_mode = getBit(data, &boff, 1);
63 b->descriptors_loop_length = getBit(data, &boff, 12);
66 tnum = (b->start_time[0] & 0xFF) << 8 | (b->start_time[1] & 0xFF);
68 b->yy = (tnum - 15078.2) / 365.25;
69 b->mm = ((tnum - 14956.1) - (int)(b->yy * 365.25)) / 30.6001;
70 b->dd = (tnum - 14956) - (int)(b->yy * 365.25) - (int)(b->mm * 30.6001);
76 if(b->mm == 14 || b->mm == 15) {
78 b->mm = b->mm - 1 - (1 * 12);
85 memset(buf, '\0', sizeof(buf));
86 sprintf(buf, "%x", b->start_time[2]);
88 memset(buf, '\0', sizeof(buf));
89 sprintf(buf, "%x", b->start_time[3]);
91 memset(buf, '\0', sizeof(buf));
92 sprintf(buf, "%x", b->start_time[4]);
95 if((b->duration[0] == 0xFF) && (b->duration[1] == 0xFF) && (b->duration[2] == 0xFF)){
96 b->dhh = b->dhm = b->dss = 0;
98 memset(buf, '\0', sizeof(buf));
99 sprintf(buf, "%x", b->duration[0]);
101 memset(buf, '\0', sizeof(buf));
102 sprintf(buf, "%x", b->duration[1]);
104 memset(buf, '\0', sizeof(buf));
105 sprintf(buf, "%x", b->duration[2]);
111 int parseSEVTdesc(unsigned char *data, SEVTdesc *desc) {
114 memset(desc, 0, sizeof(SEVTdesc));
116 desc->descriptor_tag = getBit(data, &boff, 8);
117 if((desc->descriptor_tag & 0xFF) != 0x4D) {
120 desc->descriptor_length = getBit(data, &boff, 8);
121 memcpy(desc->ISO_639_language_code, data + boff / 8, 3);
122 /* desc->ISO_639_language_code = getBit(data, &boff, 24); */
124 desc->event_name_length = getBit(data, &boff, 8);
125 getStr(desc->event_name, data, &boff, desc->event_name_length);
126 desc->text_length = getBit(data, &boff, 8);
127 getStr(desc->text, data, &boff, desc->text_length);
129 return desc->descriptor_length + 2;
132 int parseContentDesc(unsigned char *data, ContentDesc *desc) {
135 memset(desc, 0, sizeof(ContentDesc));
137 desc->descriptor_tag = getBit(data, &boff, 8);
138 if((desc->descriptor_tag & 0xFF) != 0x54) {
141 desc->descriptor_length = getBit(data, &boff, 8);
142 memcpy(desc->content, data+(boff/8), desc->descriptor_length);
143 //getStr(desc->content, data, &boff, desc->descriptor_length);
144 return desc->descriptor_length + 2;
147 int parseSeriesDesc(unsigned char *data, SeriesDesc *desc) {
150 memset(desc, 0, sizeof(SeriesDesc));
152 desc->descriptor_tag = getBit(data, &boff, 8);
153 if((desc->descriptor_tag & 0xFF) != 0xD5) {
156 desc->descriptor_length = getBit(data, &boff, 8);
157 desc->series_id = getBit(data, &boff, 16);
158 desc->repeat_label = getBit(data, &boff, 4);
159 desc->program_pattern = getBit(data, &boff, 3);
160 desc->expire_date_valid_flag = getBit(data, &boff, 1);
162 desc->expire_date = getBit(data, &boff, 16);
163 //memcpy(desc->expire_date, data + boff / 8, 2);
166 desc->episode_number = getBit(data, &boff, 12);
167 desc->last_episode_number = getBit(data, &boff, 12);
169 getStr(desc->series_name_char, data, &boff, desc->descriptor_length - 8);
170 return desc->descriptor_length + 2;
173 int parseEEVTDhead(unsigned char *data, EEVTDhead *desc) {
176 memset(desc, 0, sizeof(EEVTDhead));
178 desc->descriptor_tag = getBit(data, &boff, 8);
179 if((desc->descriptor_tag & 0xFF) != 0x4E) {
182 desc->descriptor_length = getBit(data, &boff, 8);
183 desc->descriptor_number = getBit(data, &boff, 4);
184 desc->last_descriptor_number = getBit(data, &boff, 4);
185 memcpy(desc->ISO_639_language_code, data + boff / 8, 3);
186 /* desc->ISO_639_language_code = getBit(data, &boff, 24); */
189 desc->length_of_items = getBit(data, &boff, 8);
194 int parseEEVTDitem(unsigned char *data, EEVTDitem *desc) {
197 memset(desc, 0, sizeof(EEVTDitem));
199 desc->item_description_length = getBit(data, &boff, 8);
200 getStr(desc->item_description, data, &boff, desc->item_description_length);
202 desc->item_length = getBit(data, &boff, 8);
203 // memcpy(desc->item, data + (boff / 8), desc->item_length);
204 getStr(desc->item, data, &boff, desc->item_length);
206 return desc->item_description_length + desc->item_length + 2;
209 int parseEEVTDtail(unsigned char *data, EEVTDtail *desc) {
212 memset(desc, 0, sizeof(EEVTDtail));
214 desc->text_length = getBit(data, &boff, 8);
215 getStr(desc->text, data, &boff, desc->text_length);
217 return desc->text_length + 1;
220 int checkEEVTDitem(EEVTDitem *save, EEVTDitem *new, int descriptor_number) {
225 if(save->item_length != 0) {
227 getStr(save->item, (unsigned char*)swap.item, &boff, swap.item_length);
230 //save->descriptor_number = descriptor_number;
233 if (swap.descriptor_number==1){
234 // printf("descriptor_num %d typeB\n",swap.descriptor_number);
241 //printf("descriptor_num %d typeC\n",swap.descriptor_number);
249 if(new->item_description_length == 0) {
251 memcpy(save->item + save->item_length, new->item, new->item_length);
252 save->item_length += new->item_length;
255 if (swap.descriptor_number==1){
256 printf("descriptor_num %d typeD\n",swap.descriptor_number);
262 /* ¥Ö¥ì¡¼¥¯¡£save¤ò°õºþÂоݤˤ¹¤ë¡£save¤ò¥¯¥ê¥¢? */
263 if(save->item_length != 0) {
266 getStr(save->item, (unsigned char*)swap.item, &boff, swap.item_length);
270 save->descriptor_number = descriptor_number;
275 if (swap.descriptor_number==1){
276 printf("descriptor_num %d typeE\n",swap.descriptor_number);
282 save->descriptor_number = descriptor_number;
284 if (swap.descriptor_number==1){
285 printf("descriptor_num %d typeF\n",swap.descriptor_number);
295 EIT_CONTROL *searcheit(EIT_CONTROL *top, int servid, int eventid)
301 if((cur->event_id == eventid) && (cur->servid == servid)){
309 void conv_title_subtitle(EIT_CONTROL *eitptr)
316 for(lp = 0 ; subtitle_cnv_str[lp] != NULL ; lp++){
317 ptr = strstr(eitptr->title, subtitle_cnv_str[lp]);
321 // ¥¿¥¤¥È¥ë¤¬¤Ê¤¯¤Ê¤é¤Ê¤¤¤è¤¦¤Ë
322 if(ptr == eitptr->title){
325 newsubtitle = calloc(1, ((strlen(ptr) + 1) + (strlen(eitptr->subtitle) + 1)));
326 memcpy(newsubtitle, ptr, strlen(ptr));
327 newsubtitle[strlen(ptr)] = ' ';
329 strcat(newsubtitle, eitptr->subtitle);
330 free(eitptr->subtitle);
331 eitptr->subtitle = newsubtitle ;
335 void enqueue(EIT_CONTROL *top, EIT_CONTROL *eitptr)
341 if(top->next == NULL){
348 rc = memcmp(&cur->yy, &eitptr->yy, (sizeof(int) * 3));
350 rc = memcmp(&cur->hh, &eitptr->hh, (sizeof(int) * 3));
353 free(eitptr->subtitle);
359 cur->prev->next = eitptr ;
360 eitptr->prev = cur->prev ;
364 conv_title_subtitle(eitptr);
370 cur->prev->next = eitptr ;
371 eitptr->prev = cur->prev ;
375 conv_title_subtitle(eitptr);
378 if(cur->next == NULL){
381 conv_title_subtitle(eitptr);
390 void dumpEIT(unsigned char *ptr, int serv_id, int original_network_id, int transport_stream_id, EIT_CONTROL *eittop)
401 EEVTDitem save_eevtitem;
404 EIT_CONTROL *curtmp ;
415 len = parseEIThead(ptr, &eith);
418 loop_len = eith.section_length - (len - 3 + 4); // 3¤Ï¶¦Ḁ̈إåÀĹ 4¤ÏCRC
419 while(loop_len > 0) {
420 /* Ϣ³¤¹¤ë³ÈÄ¥¥¤¥Ù¥ó¥È¤Ï¡¢´Á»ú¥³¡¼¥É¤¬µã¤Ê̤줷¤Æ
421 ʬ³ä¤µ¤ì¤ë¤è¤¦¤À¡£Ï¢Â³¤«¤É¤¦¤«¤Ï¡¢item_description_length¤¬
422 ÀßÄꤵ¤ì¤Æ¤¤¤ë¤«¤É¤¦¤«¤ÇȽÃǤǤ¤ë¤è¤¦¤À¡£ */
423 memset(&save_eevtitem, 0, sizeof(EEVTDitem));
425 len = parseEITbody(ptr, &eitb);
429 /* printf("evtid:%d\n", eitb.event_id); */
431 loop_blen = eitb.descriptors_loop_length;
432 loop_len -= loop_blen;
433 while(loop_blen > 0) {
435 len = parseSEVTdesc(ptr, &sevtd);
439 if(eith.service_id == 19304 &&
440 eitb.event_id == 46564) {
448 if(eith.service_id != serv_id){
454 timecmp(&ehh, &emm, &ess,
455 eitb.dhh, eitb.dhm, eitb.dss);
456 cur = searcheit(eittop, eith.service_id, eitb.event_id);
459 cur = calloc(1, sizeof(EIT_CONTROL));
464 cur->event_id = eitb.event_id ;
465 cur->servid = eith.service_id ;
466 cur->title = calloc(1, (strlen(sevtd.event_name) + 1));
468 memcpy(cur->title, sevtd.event_name, strlen(sevtd.event_name));
469 cur->subtitle = calloc(1, (strlen(sevtd.text) + 1));
470 memcpy(cur->subtitle, sevtd.text, strlen(sevtd.text));
479 cur->ess = eitb.dss ;
480 cur->table_id = eith.table_id ;
481 if ( !curtmp ) enqueue(eittop, cur);
483 len = parseEEVTDhead(ptr, &eevthead);
486 if(eith.service_id == 19304 &&
487 eitb.event_id == 46564) {
496 loop_elen = eevthead.length_of_items;
497 loop_len -= loop_elen;
498 while(loop_elen > 0) {
499 len = parseEEVTDitem(ptr, &eevtitem);
505 if (1||checkEEVTDitem(&save_eevtitem, &eevtitem,
506 eevthead.descriptor_number)) {
509 if (eevtitem.descriptor_number>0){
510 //printf("descriptor_num %d\n",eevtitem.descriptor_number);
514 //printf("descriptor_num %d\n",eevtitem.descriptor_number);
516 printf("EEVT,%d,%d,%d,%s,%s\n",
519 eevtitem.descriptor_number, // ÂàÈò¹àÌÜ
520 eevtitem.item_description,
526 cur = searcheit(eittop, eith.service_id, eitb.event_id);
529 cur = calloc(1, sizeof(EIT_CONTROL));
537 str_alen = strlen( cur->desc );
542 eevtitem.item_description_length = strlen(eevtitem.item_description);
543 eevtitem.item_length = strlen(eevtitem.item);
544 cur->desc = realloc(cur->desc, str_alen + eevtitem.item_description_length + eevtitem.item_length + 1000);
545 if ( !str_alen ) *cur->desc = '\0';
547 if ( eevtitem.item_description_length && !strstr(cur->desc, eevtitem.item_description) ) {
548 strcat(cur->desc, eevtitem.item_description);
549 strcat(cur->desc, "\t");
552 if ( eevtitem.item_length && !strstr(cur->desc, eevtitem.item) ) {
553 strcat(cur->desc, eevtitem.item);
554 strcat(cur->desc, "\\n");
557 if ( !curtmp ) enqueue(eittop, cur);
561 len = parseEEVTDtail(ptr, &eevttail);
564 printf("EEVTt,%d,%d,%d,%s\n",
567 eevthead.descriptor_number,
572 ContentDesc contentDesc;
573 len = parseContentDesc(ptr, &contentDesc);
575 int header_printed = 0;
576 for (int i = 0; i < contentDesc.descriptor_length - 1; i+=2) {
578 if (0xff == (unsigned char)contentDesc.content[i])
582 if (!header_printed) {
583 fprintf(out, "Content,%d,%d",
591 fprintf(out, ",%02x%02x", (unsigned char)contentDesc.content[i], (unsigned char)contentDesc.content[i+1]);
594 if((eith.original_network_id == original_network_id) && (eith.transport_stream_id == transport_stream_id)){
595 cur = searcheit(eittop, eith.service_id, eitb.event_id);
597 cur->content_type = (unsigned char)(contentDesc.content[0] >> 4);
599 fprintf(stdout, "%s:", cur->title);
600 fprintf(stdout, ",%02x%02x", (unsigned char)contentDesc.content[0], (unsigned char)contentDesc.content[1]);
601 fprintf(stdout, ",%02x%02x\n", (unsigned char)contentDesc.content[2], (unsigned char)contentDesc.content[3]);
606 if (header_printed) {
612 SeriesDesc seriesDesc;
613 len = parseSeriesDesc(ptr, &seriesDesc);
616 printf("Series,%d,%d,series=%d,repeat=%01x,pattern=%d,expire_valid=%d,expire=%04x,epinum=%d,lastepinum=%d,%s\n",
619 seriesDesc.series_id,
620 seriesDesc.repeat_label,
621 seriesDesc.program_pattern,
622 seriesDesc.expire_date_valid_flag,
623 seriesDesc.expire_date,
624 seriesDesc.episode_number,
625 seriesDesc.last_episode_number,
626 seriesDesc.series_name_char);
629 len = parseOTHERdesc(ptr);
637 /* ºÇ¸å¤Î¥Ö¥ì¡¼¥¯¥Á¥§¥Ã¥¯ */
639 if(checkEEVTDitem(&save_eevtitem, NULL, 0)) {
641 if(mode == 1) { /* long format */
642 fprintf(out, "EEVT,%d,%d,%d,%s,%s\n",
645 save_eevtitem.descriptor_number,
646 save_eevtitem.item_description,
656 void timecmp(int *thh, int *tmm, int *tss,
657 int dhh, int dmm, int dss) {