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];
50 char desc[102400] = {0};
52 char ServiceName[1024];
54 static unsigned char *base64 = (unsigned char *)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
56 static void base64_char(unsigned long bb, int srclen, unsigned char *dest, int j)
61 for ( i = srclen; i < 2; i++ )
65 for ( base = 18, x = 0; x < srclen + 2; x++, base -= 6) {
66 dest[j++] = base64[ (unsigned long)((bb>>base) & 0x3F) ];
70 for ( i = x; i < 4; i++ ) {
71 dest[j++] = (unsigned char)'='; /* 端数 */
76 static void base64_encode(unsigned char *dest, const unsigned char *src, int len)
78 unsigned char *p = src;
79 unsigned long bb = (unsigned long)0;
85 bb |= (unsigned long)*p;
89 base64_char(bb, i, dest, j);
101 if (i) base64_char(bb, i - 1, dest, j);
105 void xmlspecialchars(char *str)
107 strrep(str, "&", "&");
108 strrep(str, "'", "'");
109 strrep(str, "\"", """);
110 strrep(str, "<", "<");
111 strrep(str, ">", ">");
116 void GetSDT(FILE *infile, SVT_CONTROL *svttop, SECcache *secs,
117 int count, STATION **station, int * station_count, char *header, int is_logo)
120 int pmtpids[SECCOUNT];
121 memset(pmtpids, 0, sizeof(pmtpids));
122 int dsmccpids[SECCOUNT];
123 memset(dsmccpids, 0, sizeof(dsmccpids));
124 int i = 0 , downloadDataId = 0;
126 while((bsecs = readTS(infile, secs, count)) != NULL) {
128 if((bsecs->pid & 0xFF) == 0x11) {
129 dumpSDT(bsecs->buf, svttop, station, station_count, header);
132 else if((bsecs->pid & 0xFF) == 0x14) {
136 //else if((bsecs->pid & 0xFF) == 0x23) {
137 // dumpSDTT(bsecs->buf, *station, *station_count);
140 else if((bsecs->pid & 0xFF) == 0x29) {
141 dumpCDT(bsecs->buf, *station, *station_count);
143 else if ( is_logo ) {
145 if((bsecs->pid & 0xFF) == 0x00) {
146 dumpPAT(bsecs->buf, secs, count, pmtpids);
149 for ( i = 1; i < SECCOUNT; i++ ) {
150 if ( pmtpids[i] == 0 ) {
153 /* PMT specified by PAT */
154 if ( bsecs->pid == pmtpids[i] ) {
155 dumpPMT(bsecs->buf, secs, count, dsmccpids);
159 for ( i = 0; i < SECCOUNT; i++ ) {
160 if ( dsmccpids[i] == 0 ) {
163 /* DSM-CC specified by PMT */
164 if ( bsecs->pid == dsmccpids[i] ) {
165 dumpDSMCC(bsecs->buf, &downloadDataId, &dsmctl);
172 void GetEIT(FILE *infile, FILE *outfile, STATION *psta, SECcache *secs)
175 EIT_CONTROL *eitcur ;
176 EIT_CONTROL *eitnext ;
177 EIT_CONTROL *eittop = NULL;
185 memset(secs, 0, sizeof(SECcache) * SECCOUNT);
186 secs[0].pid = 0x12; /* EIT */
187 secs[1].pid = 0x26; /* EIT */
188 secs[2].pid = 0x27; /* EIT */
190 eittop = calloc(1, sizeof(EIT_CONTROL));
192 fseek(infile, 0, SEEK_SET);
193 while((bsecs = readTS(infile, secs, SECCOUNT)) != NULL) {
195 if((bsecs->pid & 0xFF) == 0x12) {
196 dumpEIT(bsecs->buf, psta->svId, psta->onId, psta->tsId, eittop);
197 }else if((bsecs->pid & 0xFF) == 0x26) {
198 dumpEIT(bsecs->buf, psta->svId, psta->onId, psta->tsId, eittop);
199 }else if((bsecs->pid & 0xFF) == 0x27) {
200 dumpEIT(bsecs->buf, psta->svId, psta->onId, psta->tsId, eittop);
204 while(eitcur != NULL){
206 eitcur = eitcur->next ;
209 if(eitcur->content_type > CAT_COUNT){
210 eitcur->content_type = CAT_COUNT -1 ;
212 memset(title, '\0', sizeof(title));
213 strcpy(title, eitcur->title);
214 xmlspecialchars(title);
216 memset(subtitle, '\0', sizeof(subtitle));
217 strcpy(subtitle, eitcur->subtitle);
218 xmlspecialchars(subtitle);
220 memset(desc, '\0', sizeof(desc));
221 if ( eitcur->desc ) {
222 strcpy(desc, eitcur->desc);
223 xmlspecialchars(desc);
226 memset(Category, '\0', sizeof(Category));
227 strcpy(Category, ContentCatList[eitcur->content_type].japanese);
228 xmlspecialchars(Category);
230 tl.tm_sec = eitcur->ss ;
231 tl.tm_min = eitcur->hm ;
232 tl.tm_hour = eitcur->hh ;
233 tl.tm_mday = eitcur->dd ;
234 tl.tm_mon = (eitcur->mm - 1);
235 tl.tm_year = (eitcur->yy - 1900);
239 l_time = mktime(&tl);
240 if((eitcur->ehh == 0) && (eitcur->emm == 0) && (eitcur->ess == 0)){
242 end_time = l_time + (60 * 5); // 5分後に設定
243 endtl = localtime(&end_time);
245 end_time = l_time + eitcur->ehh * 3600 + eitcur->emm * 60 + eitcur->ess;
246 endtl = localtime(&end_time);
248 memset(cendtime, '\0', sizeof(cendtime));
249 memset(cstarttime, '\0', sizeof(cstarttime));
250 strftime(cendtime, (sizeof(cendtime) - 1), "%Y%m%d%H%M%S", endtl);
251 strftime(cstarttime, (sizeof(cstarttime) - 1), "%Y%m%d%H%M%S", &tl);
252 fprintf(outfile, " <programme start=\"%s +0900\" stop=\"%s +0900\" channel=\"%s\" event=\"%d\">\n",
253 cstarttime, cendtime, psta->ontv, eitcur->event_id);
254 fprintf(outfile, " <title lang=\"ja_JP\">%s</title>\n", title);
255 fprintf(outfile, " <desc lang=\"ja_JP\">%s</desc>\n", subtitle);
256 fprintf(outfile, " <longdesc lang=\"ja_JP\">%s</longdesc>\n", desc);
257 fprintf(outfile, " <category lang=\"ja_JP\">%s</category>\n", Category);
258 //fprintf(outfile, " <category lang=\"en\">%s</category>\n", ContentCatList[eitcur->content_type].english);
259 fprintf(outfile, " </programme>\n");
261 fprintf(outfile, "(%x:%x:%x)%s,%s,%s,%s,%s,%s\n",
262 eitcur->servid, eitcur->table_id, eitcur->event_id,
263 cstarttime, cendtime,
266 ContentCatList[eitcur->content_type].english);
268 fprintf(outfile, "(%x:%x)%04d/%02d/%02d,%02d:%02d:%02d,%02d:%02d:%02d,%s,%s,%s,%s\n",
269 eitcur->table_id, eitcur->event_id,
270 eitcur->yy, eitcur->mm, eitcur->dd,
271 eitcur->hh, eitcur->hm, eitcur->ss,
272 eitcur->ehh, eitcur->emm, eitcur->ess,
273 eitcur->title, eitcur->subtitle,
274 ContentCatList[eitcur->content_type].japanese,
275 ContentCatList[eitcur->content_type].english);
277 eitnext = eitcur->next ;
279 free(eitcur->subtitle);
287 void checkSta(STATION **station,int *stalength){
292 STATION * statin = *station;
293 statmp = malloc( sizeof(STATION) * 2 );
294 for (int i = 0 ; i < *stalength ; i++){
296 for (int j = 0 ; j < chlt ; j++){
297 if ( chl[j] == statin[i].svId ) {
301 if ( noidinchl == 1 ) {
302 statmp = realloc(statmp, (stal+1) * sizeof(STATION));
303 statmp[stal] = statin[i];
304 chl[chlt] = statin[i].svId;
310 *stalength = stal;//ここいらが怪しい
311 //memcpy(statin,statmp,chlt*sizeof(STATION));
316 int main(int argc, char *argv[])
319 FILE *infile = stdin;
320 FILE *outfile = stdout;
325 SVT_CONTROL *svtcur ;
326 SVT_CONTROL *svtsave ;
327 SECcache secs[SECCOUNT];
335 SDTTdataService *service;
337 memset(dsmctl, 0, sizeof(dsmctl));
339 if(argc == 5 && strcmp(argv[1], "/LOGO") == 0){
348 if(strcmp(argv[2], "-")) {
349 infile = fopen(argv[2], "r");
351 printf( "tsFile not found (Can't open file: %s)\n", argv[2] );
359 if(strcmp(argv[3], "-")) {
360 outfile = fopen(argv[3], "w+");
362 printf( "xmlFile not found (Can't open file: %s)\n", argv[3] );
371 fprintf(stdout, "Usage : %s (/LOGO) {/BS|/CS|<id>} <tsFile> <outfile>\n", argv[0]);
372 fprintf(stdout, "\n");
373 fprintf(stdout, "/LOGO ロゴ取得モード。独立して指定し、番組表の出力を行ないません。\n");
374 fprintf(stdout, "id チャンネル識別子。地上波の物理チャンネルを与えます。\n");
375 fprintf(stdout, "/BS BSモード。一つのTSからBS全局のデータを読み込みます。\n");
376 fprintf(stdout, "/CS CSモード。一つのTSから複数局のデータを読み込みます。\n");
377 fprintf(stdout, "/TIME 時刻合わせモード。TSからTOT(Time Offset Table)を読み込みます。\n");
378 fprintf(stdout, " recpt1 <任意> 10(秒以上) - | epgdump /TIME - <任意>の形で使用してください。\n");
379 fprintf(stdout, " TOTは5秒に1回しか来ないため、recpt1に与える時間をある程度長くしてください。\n");
381 fprintf(stdout, " ontvcode Channel identifier (ex. ****.ontvjapan.com)\n");
382 fprintf(stdout, " /BS BS mode\n");
383 fprintf(stdout, " This mode reads the data of all BS TV stations\n");
384 fprintf(stdout, " from one TS data.\n");
385 fprintf(stdout, " /CS CS mode\n");
386 fprintf(stdout, " This mode reads the data of two or more CS TV stations\n");
387 fprintf(stdout, " from one TS data.\n");
394 svttop = calloc(1, sizeof(SVT_CONTROL));
399 memset(secs, 0, sizeof(SECcache) * SECCOUNT);
400 secs[0].pid = 0x00; /* PAT */
401 secs[1].pid = 0x11; /* SDT */
402 secs[2].pid = 0x29; /* CDT */
405 memset(secs, 0, sizeof(SECcache) * SECCOUNT);
406 secs[0].pid = 0x00; /* PAT */
407 secs[1].pid = 0x11; /* SDT */
408 secs[2].pid = 0x12; /* EIT */
409 secs[3].pid = 0x23; /* SDTT */
410 secs[4].pid = 0x26; /* EIT */
411 secs[5].pid = 0x27; /* EIT */
412 secs[6].pid = 0x29; /* CDT */
415 if(strcmp(arg_onTV, "/TIME") == 0){
416 printf("TSに載っている時刻データは2秒ほど早めてあるのかもしれません。\n");
417 memset(secs, 0, sizeof(SECcache) * SECCOUNT);
418 secs[0].pid = 0x14; /* TOT */
420 GetSDT(infile, NULL, secs, SECCOUNT,NULL, NULL,NULL, 0);
423 }else if(strcmp(arg_onTV, "/BS") == 0){
425 GetSDT(infile, svttop, secs, SECCOUNT, &pStas, &staCount, head, 0);
426 }else if(strcmp(arg_onTV, "/CS") == 0){
428 GetSDT(infile, svttop, secs, SECCOUNT, &pStas, &staCount, head, 0);
429 }else if(strcmp(arg_onTV, "/TEST") == 0){
431 GetSDT(infile, svttop, secs, SECCOUNT, &pStas, &staCount, head, 0);
433 // printf("Station count: %d\n1st ontv=%s,name=%s\n",staCount, pStas[0].ontv, pStas[0].name);
435 GetSDT(infile, svttop, secs, SECCOUNT, &pStas, &staCount, arg_onTV, 0);
437 // 地上波のマルチチャンネル対応のためコメントアウト
440 svttop = calloc(1, sizeof(SVT_CONTROL));
441 GetSDT(infile, svttop, secs, SECCOUNT);
442 svtcur = svttop->next ; //先頭
448 pStas = calloc(1, sizeof(STATION));
449 pStas->tsId = svtcur->transport_stream_id ;
450 pStas->onId = svtcur->original_network_id ;
451 pStas->svId = svtcur->event_id ;
452 pStas->ontv = arg_onTV ;
453 pStas->name = svtcur->servicename ;
457 checkSta(&pStas, &staCount);
459 fprintf(outfile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
460 fprintf(outfile, "<!DOCTYPE tv SYSTEM \"xmltv.dtd\">\n\n");
461 fprintf(outfile, "<tv generator-info-name=\"tsEPG2xml\" generator-info-url=\"http://localhost/\">\n");
464 memset(Logo, '\0', sizeof(Logo));
465 for(lp = 0 ; lp < staCount ; lp++){
466 for ( i = 0 ; i < 6 ; i++) {
467 if (pStas[lp].logo_array[i].logo) {
468 base64_encode(Logo, pStas[lp].logo_array[i].logo, pStas[lp].logo_array[i].logo_size);
469 xmlspecialchars(Logo);
470 fprintf(outfile, " <logo ts=\"%d\" on=\"%d\" sv=\"%d\" type=\"%d\">%s</logo>\n",
480 for ( i = 0; i < 1024; i++) {
481 if ( dsmctl[i].isUsed == 0 ) break;
482 parseSDTTdata(dsmctl[i].blockData, &sdtd);
484 for (j = 0; j < sdtd.number_of_loop; j++) {
485 loop = sdtd.loop + sizeof(SDTTdataLoop) * j;
487 for ( k = 0; k < loop->number_of_services; k++) {
488 service = loop->services + sizeof(SDTTdataService) * k;
491 for(lp = 0 ; lp < staCount ; lp++){
493 pStas[lp].tsId == service->transport_stream_id &&
494 pStas[lp].onId == service->original_network_id &&
495 pStas[lp].svId == service->service_id
498 &pStas[lp].logo_array[sdtd.logo_type].logo,
499 &pStas[lp].logo_array[sdtd.logo_type].logo_size);
505 printf( "SDTTdataLoop (%d:%d) %d:%d[%d:%d:%d]%d\n",
509 service->transport_stream_id,
510 service->original_network_id,
520 clt2png(loop->data, &logo, &logo_size);
521 memset(Logo, '\0', sizeof(Logo));
522 base64_encode(Logo, logo, logo_size);
523 xmlspecialchars(Logo);
525 fprintf(outfile, " <logo ts=\"%d\" on=\"%d\" sv=\"%d\" type=\"%d\" dlid=\"%d\">%s</logo>\n",
526 service->transport_stream_id,
527 service->original_network_id,
537 for(lp = 0 ; lp < staCount ; lp++){
538 memset(ServiceName, '\0', sizeof(ServiceName));
539 strcpy(ServiceName, pStas[lp].name);
540 xmlspecialchars(ServiceName);
542 fprintf(outfile, " <channel id=\"%s\">\n", pStas[lp].ontv);
543 fprintf(outfile, " <display-name lang=\"ja_JP\">%s</display-name>\n", ServiceName);
544 fprintf(outfile, " <id ts=\"%d\" on=\"%d\" sv=\"%d\"/>\n", pStas[lp].tsId, pStas[lp].onId, pStas[lp].svId);
546 fprintf(outfile, " </channel>\n");
549 fprintf(outfile, "</tv>\n");
552 for(lp = 0 ; lp < staCount ; lp++){
553 GetEIT(infile, outfile, &pStas[lp], secs);
555 fprintf(outfile, "</tv>\n");
565 svtcur = svttop ; //先頭
566 while(svtcur != NULL){
567 svtsave = svtcur->next ;