19 typedef struct _ContentTYPE{
25 static CONTENT_TYPE ContentCatList[CAT_COUNT] = {
26 { "ニュース・報道", "news" },
28 { "情報", "information" },
31 { "バラエティ", "variety" },
33 { "アニメ・特撮", "anime" },
34 { "ドキュメンタリー・教養", "documentary" },
41 { "その他", "etc" } //その他
45 SVT_CONTROL *svttop = NULL;
46 DSM_CONTROL dsmctl[1024];
48 static unsigned char *base64 = (unsigned char *)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
50 static void base64_char(unsigned long bb, int srclen, unsigned char *dest, int j)
55 for ( i = srclen; i < 2; i++ )
59 for ( base = 18, x = 0; x < srclen + 2; x++, base -= 6) {
60 dest[j++] = base64[ (unsigned long)((bb>>base) & 0x3F) ];
64 for ( i = x; i < 4; i++ ) {
65 dest[j++] = (unsigned char)'='; /* 端数 */
70 static void base64_encode(unsigned char *dest, const unsigned char *src, int len)
72 unsigned char *p = src;
73 unsigned long bb = (unsigned long)0;
79 bb |= (unsigned long)*p;
83 base64_char(bb, i, dest, j);
95 if (i) base64_char(bb, i - 1, dest, j);
99 void xmlspecialchars(char *str)
101 strrep(str, "&", "&");
102 strrep(str, "'", "'");
103 strrep(str, "\"", """);
104 strrep(str, "<", "<");
105 strrep(str, ">", ">");
110 void GetSDT(FILE *infile, SVT_CONTROL *svttop, SECcache *secs,
111 int count, STATION **station, int * station_count, char *header, int is_logo)
114 int pmtpids[SECCOUNT];
115 memset(pmtpids, 0, sizeof(pmtpids));
116 int dsmccpids[SECCOUNT];
117 memset(dsmccpids, 0, sizeof(dsmccpids));
118 int i = 0 , downloadDataId = 0;
120 while((bsecs = readTS(infile, secs, count)) != NULL) {
122 if((bsecs->pid & 0xFF) == 0x11) {
123 dumpSDT(bsecs->buf, svttop, station, station_count, header);
126 else if((bsecs->pid & 0xFF) == 0x14) {
130 //else if((bsecs->pid & 0xFF) == 0x23) {
131 // dumpSDTT(bsecs->buf, *station, *station_count);
134 else if((bsecs->pid & 0xFF) == 0x29) {
135 dumpCDT(bsecs->buf, *station, *station_count);
137 else if ( is_logo ) {
139 if((bsecs->pid & 0xFF) == 0x00) {
140 dumpPAT(bsecs->buf, secs, count, pmtpids);
143 for ( i = 1; i < SECCOUNT; i++ ) {
144 if ( pmtpids[i] == 0 ) {
147 /* PMT specified by PAT */
148 if ( bsecs->pid == pmtpids[i] ) {
149 dumpPMT(bsecs->buf, secs, count, dsmccpids);
153 for ( i = 0; i < SECCOUNT; i++ ) {
154 if ( dsmccpids[i] == 0 ) {
157 /* DSM-CC specified by PMT */
158 if ( bsecs->pid == dsmccpids[i] ) {
159 dumpDSMCC(bsecs->buf, &downloadDataId, &dsmctl);
166 void GetEIT(FILE *infile, FILE *outfile, STATION *psta, SECcache *secs)
169 EIT_CONTROL *eitcur ;
170 EIT_CONTROL *eitnext ;
171 EIT_CONTROL *eittop = NULL;
181 char desc[102400] = {0};
183 char VideoType[1024];
184 char AudioType[1024];
186 memset(secs, 0, sizeof(SECcache) * SECCOUNT);
187 secs[0].pid = 0x12; /* EIT */
188 secs[1].pid = 0x26; /* EIT */
189 secs[2].pid = 0x27; /* EIT */
191 eittop = calloc(1, sizeof(EIT_CONTROL));
193 fseek(infile, 0, SEEK_SET);
194 while((bsecs = readTS(infile, secs, SECCOUNT)) != NULL) {
196 if((bsecs->pid & 0xFF) == 0x12) {
197 dumpEIT(bsecs->buf, psta->svId, psta->onId, psta->tsId, eittop);
198 }else if((bsecs->pid & 0xFF) == 0x26) {
199 dumpEIT(bsecs->buf, psta->svId, psta->onId, psta->tsId, eittop);
200 }else if((bsecs->pid & 0xFF) == 0x27) {
201 dumpEIT(bsecs->buf, psta->svId, psta->onId, psta->tsId, eittop);
205 while(eitcur != NULL){
207 eitcur = eitcur->next ;
210 if(eitcur->content_type > CAT_COUNT){
211 eitcur->content_type = CAT_COUNT -1 ;
213 memset(title, '\0', sizeof(title));
214 strcpy(title, eitcur->title);
215 xmlspecialchars(title);
217 memset(subtitle, '\0', sizeof(subtitle));
218 strcpy(subtitle, eitcur->subtitle);
219 xmlspecialchars(subtitle);
221 memset(desc, '\0', sizeof(desc));
222 if ( eitcur->desc ) {
223 strcpy(desc, eitcur->desc);
224 xmlspecialchars(desc);
227 memset(Category, '\0', sizeof(Category));
228 strcpy(Category, ContentCatList[eitcur->content_type].japanese);
229 xmlspecialchars(Category);
231 memset(VideoType, '\0', sizeof(VideoType));
232 strcpy(VideoType, parseComponentDescType(eitcur->video_type));
233 xmlspecialchars(VideoType);
235 memset(AudioType, '\0', sizeof(AudioType));
236 strcpy(AudioType, parseAudioComponentDescType(eitcur->audio_type));
237 xmlspecialchars(AudioType);
239 tl.tm_sec = eitcur->ss ;
240 tl.tm_min = eitcur->hm ;
241 tl.tm_hour = eitcur->hh ;
242 tl.tm_mday = eitcur->dd ;
243 tl.tm_mon = (eitcur->mm - 1);
244 tl.tm_year = (eitcur->yy - 1900);
248 l_time = mktime(&tl);
249 if((eitcur->ehh == 0) && (eitcur->emm == 0) && (eitcur->ess == 0)){
251 end_time = l_time + (60 * 5); // 5分後に設定
252 endtl = localtime(&end_time);
254 end_time = l_time + eitcur->ehh * 3600 + eitcur->emm * 60 + eitcur->ess;
255 endtl = localtime(&end_time);
257 memset(cendtime, '\0', sizeof(cendtime));
258 memset(cstarttime, '\0', sizeof(cstarttime));
259 strftime(cendtime, (sizeof(cendtime) - 1), "%Y%m%d%H%M%S", endtl);
260 strftime(cstarttime, (sizeof(cstarttime) - 1), "%Y%m%d%H%M%S", &tl);
262 fprintf(outfile, " <programme start=\"%s +0900\" stop=\"%s +0900\" channel=\"%s\" event=\"%d\">\n",
263 cstarttime, cendtime, psta->ontv, eitcur->event_id);
264 fprintf(outfile, " <title lang=\"ja_JP\">%s</title>\n", title);
265 fprintf(outfile, " <desc lang=\"ja_JP\">%s</desc>\n", subtitle);
266 fprintf(outfile, " <longdesc lang=\"ja_JP\">%s</longdesc>\n", desc);
267 fprintf(outfile, " <category lang=\"ja_JP\">%s</category>\n", Category);
268 fprintf(outfile, " <video type=\"%d\">%s</video>\n", eitcur->video_type, VideoType);
269 fprintf(outfile, " <audio type=\"%d\" multi=\"%d\">%s</audio>\n", eitcur->audio_type, eitcur->multi_type, AudioType);
270 //fprintf(outfile, " <category lang=\"en\">%s</category>\n", ContentCatList[eitcur->content_type].english);
271 fprintf(outfile, " </programme>\n");
273 fprintf(outfile, "(%x:%x:%x)%s,%s,%s,%s,%s,%s\n",
274 eitcur->servid, eitcur->table_id, eitcur->event_id,
275 cstarttime, cendtime,
278 ContentCatList[eitcur->content_type].english);
280 fprintf(outfile, "(%x:%x)%04d/%02d/%02d,%02d:%02d:%02d,%02d:%02d:%02d,%s,%s,%s,%s\n",
281 eitcur->table_id, eitcur->event_id,
282 eitcur->yy, eitcur->mm, eitcur->dd,
283 eitcur->hh, eitcur->hm, eitcur->ss,
284 eitcur->ehh, eitcur->emm, eitcur->ess,
285 eitcur->title, eitcur->subtitle,
286 ContentCatList[eitcur->content_type].japanese,
287 ContentCatList[eitcur->content_type].english);
289 eitnext = eitcur->next ;
291 free(eitcur->subtitle);
299 void checkSta(STATION **station,int *stalength){
304 STATION * statin = *station;
305 statmp = malloc( sizeof(STATION) * 2 );
306 for (int i = 0 ; i < *stalength ; i++){
308 for (int j = 0 ; j < chlt ; j++){
309 if ( chl[j] == statin[i].svId ) {
313 if ( noidinchl == 1 ) {
314 statmp = realloc(statmp, (stal+1) * sizeof(STATION));
315 statmp[stal] = statin[i];
316 chl[chlt] = statin[i].svId;
322 *stalength = stal;//ここいらが怪しい
323 //memcpy(statin,statmp,chlt*sizeof(STATION));
328 int main(int argc, char *argv[])
331 FILE *infile = stdin;
332 FILE *outfile = stdout;
337 SVT_CONTROL *svtcur ;
338 SVT_CONTROL *svtsave ;
339 SECcache secs[SECCOUNT];
347 SDTTdataService *service;
349 memset(dsmctl, 0, sizeof(dsmctl));
351 if(argc == 5 && strcmp(argv[1], "/LOGO") == 0){
360 if(strcmp(argv[2], "-")) {
361 infile = fopen(argv[2], "r");
363 printf( "tsFile not found (Can't open file: %s)\n", argv[2] );
371 if(strcmp(argv[3], "-")) {
372 outfile = fopen(argv[3], "w+");
374 printf( "xmlFile not found (Can't open file: %s)\n", argv[3] );
383 fprintf(stdout, "Usage : %s (/LOGO) {/BS|/CS|<id>} <tsFile> <outfile>\n", argv[0]);
384 fprintf(stdout, "\n");
385 fprintf(stdout, "/LOGO ロゴ取得モード。独立して指定し、番組表の出力を行ないません。\n");
386 fprintf(stdout, "id チャンネル識別子。地上波の物理チャンネルを与えます。\n");
387 fprintf(stdout, "/BS BSモード。一つのTSからBS全局のデータを読み込みます。\n");
388 fprintf(stdout, "/CS CSモード。一つのTSから複数局のデータを読み込みます。\n");
389 fprintf(stdout, "/TIME 時刻合わせモード。TSからTOT(Time Offset Table)を読み込みます。\n");
390 fprintf(stdout, " recpt1 <任意> 10(秒以上) - | epgdump /TIME - <任意>の形で使用してください。\n");
391 fprintf(stdout, " TOTは5秒に1回しか来ないため、recpt1に与える時間をある程度長くしてください。\n");
393 fprintf(stdout, " ontvcode Channel identifier (ex. ****.ontvjapan.com)\n");
394 fprintf(stdout, " /BS BS mode\n");
395 fprintf(stdout, " This mode reads the data of all BS TV stations\n");
396 fprintf(stdout, " from one TS data.\n");
397 fprintf(stdout, " /CS CS mode\n");
398 fprintf(stdout, " This mode reads the data of two or more CS TV stations\n");
399 fprintf(stdout, " from one TS data.\n");
406 svttop = calloc(1, sizeof(SVT_CONTROL));
411 memset(secs, 0, sizeof(SECcache) * SECCOUNT);
412 secs[0].pid = 0x00; /* PAT */
413 secs[1].pid = 0x11; /* SDT */
414 secs[2].pid = 0x29; /* CDT */
417 memset(secs, 0, sizeof(SECcache) * SECCOUNT);
418 secs[0].pid = 0x00; /* PAT */
419 secs[1].pid = 0x11; /* SDT */
420 secs[2].pid = 0x12; /* EIT */
421 secs[3].pid = 0x23; /* SDTT */
422 secs[4].pid = 0x26; /* EIT */
423 secs[5].pid = 0x27; /* EIT */
424 secs[6].pid = 0x29; /* CDT */
427 if(strcmp(arg_onTV, "/TIME") == 0){
428 printf("TSに載っている時刻データは2秒ほど早めてあるのかもしれません。\n");
429 memset(secs, 0, sizeof(SECcache) * SECCOUNT);
430 secs[0].pid = 0x14; /* TOT */
432 GetSDT(infile, NULL, secs, SECCOUNT,NULL, NULL,NULL, 0);
435 }else if(strcmp(arg_onTV, "/BS") == 0){
437 GetSDT(infile, svttop, secs, SECCOUNT, &pStas, &staCount, head, is_logo);
438 }else if(strcmp(arg_onTV, "/CS") == 0){
440 GetSDT(infile, svttop, secs, SECCOUNT, &pStas, &staCount, head, is_logo);
441 }else if(strcmp(arg_onTV, "/TEST") == 0){
443 GetSDT(infile, svttop, secs, SECCOUNT, &pStas, &staCount, head, 0);
445 // printf("Station count: %d\n1st ontv=%s,name=%s\n",staCount, pStas[0].ontv, pStas[0].name);
447 GetSDT(infile, svttop, secs, SECCOUNT, &pStas, &staCount, arg_onTV, 0);
449 // 地上波のマルチチャンネル対応のためコメントアウト
452 svttop = calloc(1, sizeof(SVT_CONTROL));
453 GetSDT(infile, svttop, secs, SECCOUNT);
454 svtcur = svttop->next ; //先頭
460 pStas = calloc(1, sizeof(STATION));
461 pStas->tsId = svtcur->transport_stream_id ;
462 pStas->onId = svtcur->original_network_id ;
463 pStas->svId = svtcur->event_id ;
464 pStas->ontv = arg_onTV ;
465 pStas->name = svtcur->servicename ;
469 checkSta(&pStas, &staCount);
471 fprintf(outfile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
472 fprintf(outfile, "<!DOCTYPE tv SYSTEM \"xmltv.dtd\">\n\n");
473 fprintf(outfile, "<tv generator-info-name=\"tsEPG2xml\" generator-info-url=\"http://localhost/\">\n");
475 char ServiceName[1024];
479 memset(Logo, '\0', sizeof(Logo));
480 for(lp = 0 ; lp < staCount ; lp++){
481 for ( i = 0 ; i < 6 ; i++) {
482 if (pStas[lp].logo_array[i].logo) {
483 base64_encode(Logo, pStas[lp].logo_array[i].logo, pStas[lp].logo_array[i].logo_size);
484 xmlspecialchars(Logo);
485 fprintf(outfile, " <logo ts=\"%d\" on=\"%d\" sv=\"%d\" type=\"%d\">%s</logo>\n",
495 for ( i = 0; i < 1024; i++) {
496 if ( dsmctl[i].isUsed == 0 ) break;
497 parseSDTTdata(dsmctl[i].blockData, &sdtd);
499 for (j = 0; j < sdtd.number_of_loop; j++) {
500 loop = sdtd.loop + sizeof(SDTTdataLoop) * j;
502 for ( k = 0; k < loop->number_of_services; k++) {
503 service = loop->services + sizeof(SDTTdataService) * k;
506 for(lp = 0 ; lp < staCount ; lp++){
508 pStas[lp].tsId == service->transport_stream_id &&
509 pStas[lp].onId == service->original_network_id &&
510 pStas[lp].svId == service->service_id
513 &pStas[lp].logo_array[sdtd.logo_type].logo,
514 &pStas[lp].logo_array[sdtd.logo_type].logo_size);
520 printf( "SDTTdataLoop (%d:%d) %d:%d[%d:%d:%d]%d\n",
524 service->transport_stream_id,
525 service->original_network_id,
535 clt2png(loop->data, &logo, &logo_size);
536 memset(Logo, '\0', sizeof(Logo));
537 base64_encode(Logo, logo, logo_size);
538 xmlspecialchars(Logo);
540 fprintf(outfile, " <logo ts=\"%d\" on=\"%d\" sv=\"%d\" type=\"%d\" dlid=\"%d\">%s</logo>\n",
541 service->transport_stream_id,
542 service->original_network_id,
552 for(lp = 0 ; lp < staCount ; lp++){
553 memset(ServiceName, '\0', sizeof(ServiceName));
554 strcpy(ServiceName, pStas[lp].name);
555 xmlspecialchars(ServiceName);
557 fprintf(outfile, " <channel id=\"%s\">\n", pStas[lp].ontv);
558 fprintf(outfile, " <display-name lang=\"ja_JP\">%s</display-name>\n", ServiceName);
559 fprintf(outfile, " <id ts=\"%d\" on=\"%d\" sv=\"%d\"/>\n", pStas[lp].tsId, pStas[lp].onId, pStas[lp].svId);
561 fprintf(outfile, " </channel>\n");
564 fprintf(outfile, "</tv>\n");
567 for(lp = 0 ; lp < staCount ; lp++){
568 GetEIT(infile, outfile, &pStas[lp], secs);
570 fprintf(outfile, "</tv>\n");
580 svtcur = svttop ; //先頭
581 while(svtcur != NULL){
582 svtsave = svtcur->next ;