OSDN Git Service

fix logo parse func
[rec10/rec10-git.git] / epgdump / epgdump.c
index 305b838..e4dd11e 100755 (executable)
 #include "sdt.h"
 #include "sdtt.h"
 #include "eit.h"
+#include "tot.h"
 #include "dsmcc.h"
 #include "ts_ctl.h"
+#include "util.h"
 
 typedef                struct  _ContentTYPE{
        char    *japanese ;
@@ -41,6 +43,7 @@ static  CONTENT_TYPE  ContentCatList[CAT_COUNT] = {
 
 
 SVT_CONTROL    *svttop = NULL;
+DSM_CONTROL    dsmctl[1024];
 #define                SECCOUNT        64
 char   title[1024];
 char   subtitle[1024];
@@ -111,22 +114,18 @@ void      xmlspecialchars(char *str)
 
 
 void   GetSDT(FILE *infile, SVT_CONTROL *svttop, SECcache *secs, 
-       int count, STATION **station, int * station_count, char *header, int is_bs_cs)
+       int count, STATION **station, int * station_count, char *header, int is_bs_cs_logo)
 {
        SECcache  *bsecs;
        int pmtpids[SECCOUNT];
        memset(pmtpids, 0, sizeof(pmtpids));
        int dsmccpids[SECCOUNT];
        memset(dsmccpids, 0, sizeof(dsmccpids));
-       int i = 0;
+       int i = 0 , downloadDataId = 0;
 
        while((bsecs = readTS(infile, secs, count)) != NULL) {
-               /* PAT */
-               if(is_bs_cs && (bsecs->pid & 0xFF) == 0x00) {
-                       dumpPAT(bsecs->buf, secs, count, pmtpids);
-               }
                /* SDT */
-               else if((bsecs->pid & 0xFF) == 0x11) {
+               if((bsecs->pid & 0xFF) == 0x11) {
                        dumpSDT(bsecs->buf, svttop, station, station_count, header);
                }
                /* TOT */
@@ -134,14 +133,19 @@ void      GetSDT(FILE *infile, SVT_CONTROL *svttop, SECcache *secs,
                        dumpTOT(bsecs->buf);
                }
                /* SDTT */
-               else if((bsecs->pid & 0xFF) == 0x23) {
-                       dumpSDTT(bsecs->buf, *station, *station_count);
-               }
+               //else if((bsecs->pid & 0xFF) == 0x23) {
+               //      dumpSDTT(bsecs->buf, *station, *station_count);
+               //}
                /* CDT */
                else if((bsecs->pid & 0xFF) == 0x29) {
                        dumpCDT(bsecs->buf, *station, *station_count);
                }
-               else if ( is_bs_cs ) {
+               else if ( is_bs_cs_logo ) {
+                       /* PAT */
+                       if((bsecs->pid & 0xFF) == 0x00) {
+                               dumpPAT(bsecs->buf, secs, count, pmtpids);
+                       }
+                       /* PMT */
                        for ( i = 1; i < SECCOUNT; i++ ) {
                                if ( pmtpids[i] == 0 ) {
                                        break;
@@ -151,14 +155,14 @@ void      GetSDT(FILE *infile, SVT_CONTROL *svttop, SECcache *secs,
                                        dumpPMT(bsecs->buf, secs, count, dsmccpids);
                                }
                        }
-
+                       /* DSM-CC */
                        for ( i = 0; i < SECCOUNT; i++ ) {
                                if ( dsmccpids[i] == 0 ) {
                                        break;
                                }
                                /* DSM-CC specified by PMT */
                                if ( bsecs->pid == dsmccpids[i] ) {
-                                       dumpDSMCC(bsecs->buf, *station, *station_count);
+                                       dumpDSMCC(bsecs->buf, &downloadDataId, &dsmctl);
                                }
                        }
                }
@@ -279,66 +283,36 @@ void      GetEIT(FILE *infile, FILE *outfile, STATION *psta, SECcache *secs)
        free(eittop);
        eittop = NULL;
 }
-void checkSta_BS(STATION **station,int *stalength){
-       STATION *statmp;
-       int chl[30];
-       int chlt=0;
-       int stal=0;
-       STATION * statin= *station;
-       statmp=malloc(sizeof(STATION)*2);
-       for (int i=0;i<*stalength;i++){
-               int noidinchl=1;
-               for (int j=0;j<chlt;j++){
-                       if (chl[j]==statin[i].svId || statin[i].svId >= 290){
-                               noidinchl=0;
-                       }
-               }
-               if (noidinchl==1){
-                       statmp=realloc(statmp,(stal+1)*sizeof(STATION));
-                       statmp[stal]=statin[i];
-                       //memcpy(statmp[stal-1],station[i],sizeof(STATION));
-                       chl[chlt]=statin[i].svId;
-                       chlt++;
-                       stal++;
-               }
-       }
-       //*station=realloc(station,(stal+1)*sizeof(STATION));
-       //memcpy(*station,statmp,(stal+1)*sizeof(STATION));
-       *station=statmp;
-       *stalength=stal;
-       //memcpy(statin,statmp,chlt*sizeof(STATION));
-       //free(statmp);
-}
 
 void checkSta(STATION **station,int *stalength){
        STATION *statmp;
        int chl[90];
-       int chlt=0;
-       int stal=0;
-       STATION * statin= *station;
-       statmp=malloc(sizeof(STATION)*2);
-       for (int i=0;i<*stalength;i++){
-               int noidinchl=1;
-               for (int j=0;j<chlt;j++){
-                       if (chl[j]==statin[i].svId){
-                               noidinchl=0;
+       int chlt = 0;
+       int stal = 0;
+       STATION * statin = *station;
+       statmp = malloc( sizeof(STATION) * 2 );
+       for (int i = 0 ; i < *stalength ; i++){
+               int noidinchl = 1;
+               for (int j = 0 ; j < chlt ; j++){
+                       if ( chl[j] == statin[i].svId ) {
+                               noidinchl = 0;
                        }
                }
-               if (noidinchl==1){
-                       statmp=realloc(statmp,(stal+1)*sizeof(STATION));
-                       statmp[stal]=statin[i];
-                       //memcpy(statmp[stal-1],station[i],sizeof(STATION));
-                       chl[chlt]=statin[i].svId;
+               if ( noidinchl == 1 ) {
+                       statmp = realloc(statmp, (stal+1) * sizeof(STATION));
+                       statmp[stal] = statin[i];
+                       chl[chlt] = statin[i].svId;
                        chlt++;
                        stal++;
                }
        }
-       *station=statmp;
-       *stalength=stal;//ここいらが怪しい
+       *station = statmp;
+       *stalength = stal;//ここいらが怪しい
        //memcpy(statin,statmp,chlt*sizeof(STATION));
        //free(statmp);
        return;
 }
+
 int main(int argc, char *argv[])
 {
 
@@ -354,24 +328,27 @@ int main(int argc, char *argv[])
        int             lp ;
        STATION *pStas ;
        int             act = 0;
-       int             i ;
+       int             i , j, k ;
+       int             is_bs_cs_logo = 0;
+       SDTTdata  sdtd;
+       SDTTdataLoop *loop;
+       SDTTdataService *service;
 
        /* 興味のあるpidを指定 */
        memset(secs, 0,  sizeof(SECcache) * SECCOUNT);
        secs[0].pid = 0x00; /* PAT  */
        secs[1].pid = 0x11; /* SDT  */
-       secs[2].pid = 0x12; /* EIT  */
-       secs[3].pid = 0x23; /* SDTT */
-       secs[4].pid = 0x26; /* EIT  */
-       secs[5].pid = 0x27; /* EIT  */
-       secs[6].pid = 0x29; /* CDT  */
+       secs[2].pid = 0x23; /* SDTT */
+       secs[3].pid = 0x29; /* CDT  */
+
+       memset(dsmctl, 0,  sizeof(dsmctl));
 
        if(argc == 4){
                arg_onTV = argv[1];
                if(strcmp(argv[2], "-")) {
                        infile = fopen(argv[2], "r");
                        if ( !infile) {
-                         printf( "tsFile not found.\n" );
+                         printf( "tsFile not found (Can't open file: %s)\n", argv[2] );
                          exit( -1 );
                        }
                        inclose = 1;
@@ -382,7 +359,7 @@ int main(int argc, char *argv[])
                if(strcmp(argv[3], "-")) {
                        outfile = fopen(argv[3], "w+");
                        if ( !outfile) {
-                         printf( "xmlfile not found.\n" );
+                         printf( "xmlFile not found (Can't open file: %s)\n", argv[3] );
                          exit( -1 );
                        }
                        outclose = 1;
@@ -391,8 +368,8 @@ int main(int argc, char *argv[])
                        outfile = stdout;
                }
        }else{
-               fprintf(stdout, "Usage : %s {/BS|/CS} <tsFile> <outfile>\n", argv[0]);
-               fprintf(stdout, "Usage : %s <id> <tsFile> <outfile>\n", argv[0]);
+               fprintf(stdout, "Usage : %s {/BS|/CS} <tsFile> <xmlFile>\n", argv[0]);
+               fprintf(stdout, "Usage : %s <id> <tsFile> <xmlFile>\n", argv[0]);
                fprintf(stdout, "\n");
                fprintf(stdout, "id       チャンネル識別子。地上波の物理チャンネルを与えます。\n");
                fprintf(stdout, "/BS      BSモード。一つのTSからBS全局のデータを読み込みます。\n");
@@ -412,54 +389,37 @@ int main(int argc, char *argv[])
                return 0;
        }
 
-       if(strcmp(arg_onTV, "/BS") == 0){
-               STATION *sta = NULL;
-               int sta_count = 0;
-               svttop = calloc(1, sizeof(SVT_CONTROL));
-               char *head="BS";
-               GetSDT(infile, svttop, secs, SECCOUNT,&sta, &sta_count, head, 1);
-               checkSta_BS(&sta,&sta_count);
-               pStas = sta;
-               staCount = sta_count;
-               //staCount = sizeof(pStas) / sizeof (STATION);
-               act = 0 ;
-       }else if(strcmp(arg_onTV, "/CS") == 0){
-               STATION *sta=NULL;
-               int sta_count = 0;
-               svttop = calloc(1, sizeof(SVT_CONTROL));
-               char *head="CS";
-               GetSDT(infile, svttop, secs, SECCOUNT,&sta, &sta_count, head, 1);
-               checkSta(&sta,&sta_count);
-               pStas = sta;
-               staCount = sta_count;
-               //staCount=sizeof(pStas) / sizeof (STATION);
-               act = 0 ;
-       }else if(strcmp(arg_onTV, "/TEST") == 0){
-               STATION *sta=NULL;
-               int sta_count = 0;
-               svttop = calloc(1, sizeof(SVT_CONTROL));
-               char *head="TEST";
-               GetSDT(infile, svttop, secs, SECCOUNT,&sta, &sta_count, head, 0);
-               checkSta(&sta,&sta_count);
-               //if (sta_count) 
-               printf("Station count: %d\n1st ontv=%s,name=%s\n",sta_count, sta[0].ontv, sta[0].name);
-               pStas = sta;
-               staCount = sta_count;// sizeof(pStas) / sizeof (STATION);
-               act = 0 ;
-       }else if(strcmp(arg_onTV, "/TIME") == 0){
+       pStas = NULL;
+       staCount = 0;
+       svttop = calloc(1, sizeof(SVT_CONTROL));
+       act = 0 ;
+
+       if(strcmp(arg_onTV, "/TIME") == 0){
                printf("TSに載っている時刻データは2秒ほど早めてあるのかもしれません。\n");
                memset(secs, 0,  sizeof(SECcache) * SECCOUNT);
                secs[0].pid = 0x14; /* TOT  */
 
                GetSDT(infile, NULL, secs, SECCOUNT,NULL, NULL,NULL, 0);
-               act = 0 ;
 
                goto cleanup;
+       }else if(strcmp(arg_onTV, "/BS") == 0){
+               char *head = "BS";
+               GetSDT(infile, svttop, secs, SECCOUNT, &pStas, &staCount, head, 0);
+       }else if(strcmp(arg_onTV, "/CS") == 0){
+               char *head = "CS";
+               GetSDT(infile, svttop, secs, SECCOUNT, &pStas, &staCount, head, 0);
+       }else if(strcmp(arg_onTV, "/BSCSLOGO") == 0){
+               is_bs_cs_logo = 1;
+               GetSDT(infile, svttop, secs, SECCOUNT, &pStas, &staCount, "", 1);
+       }else if(strcmp(arg_onTV, "/TEST") == 0){
+               char *head = "TEST";
+               GetSDT(infile, svttop, secs, SECCOUNT, &pStas, &staCount, head, 0);
+               //if (sta_count) 
+               //      printf("Station count: %d\n1st ontv=%s,name=%s\n",staCount, pStas[0].ontv, pStas[0].name);
        }else{
-               if(infile == NULL){
-                       fprintf(stderr, "Can't open file: %s\n", argv[2]);
-                       return 1;
-               }
+               GetSDT(infile, svttop, secs, SECCOUNT, &pStas, &staCount, arg_onTV, 0);
+
+               // 地上波のマルチチャンネル対応のためコメントアウト
                /*
                act = 1 ;
                svttop = calloc(1, sizeof(SVT_CONTROL));
@@ -478,24 +438,73 @@ int main(int argc, char *argv[])
                pStas->name = svtcur->servicename ;
                staCount = 1;
                */
-
-               // 地上波のマルチチャンネル対応のため
-               STATION *sta = NULL;
-               int sta_count = 0;
-               svttop = calloc(1, sizeof(SVT_CONTROL));
-               //char *head=arg_onTV;
-               GetSDT(infile, svttop, secs, SECCOUNT, &sta, &sta_count, arg_onTV, 0);
-               checkSta(&sta, &sta_count);
-               //if (sta_count) printf("Station count: %d\n1st ontv=%s,name=%s\n",sta_count, sta[0].ontv, sta[0].name);
-               pStas = sta;
-               staCount = sta_count;// sizeof(pStas) / sizeof (STATION);
-               act = 0 ;
        }
+       checkSta(&pStas, &staCount);
 
        fprintf(outfile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        fprintf(outfile, "<!DOCTYPE tv SYSTEM \"xmltv.dtd\">\n\n");
        fprintf(outfile, "<tv generator-info-name=\"tsEPG2xml\" generator-info-url=\"http://localhost/\">\n");
 
+       for ( i = 0; i < 1024; i++) {
+               if ( dsmctl[i].isUsed == 0 ) break;
+               parseSDTTdata(dsmctl[i].blockData, &sdtd);
+
+               for (j = 0; j < sdtd.number_of_loop; j++) {
+                       loop = sdtd.loop + sizeof(SDTTdataLoop) * j;
+
+                       for ( k = 0; k < loop->number_of_services; k++) {
+                               service = loop->services + sizeof(SDTTdataService) * k;
+
+                               /*
+                               for(lp = 0 ; lp < staCount ; lp++){
+                                       if ( 
+                                               pStas[lp].tsId == service->transport_stream_id && 
+                                               pStas[lp].onId == service->original_network_id && 
+                                               pStas[lp].svId == service->service_id
+                                       ) {
+                                               clt2png(loop->data, 
+                                                       &pStas[lp].logo_array[sdtd.logo_type].logo, 
+                                                       &pStas[lp].logo_array[sdtd.logo_type].logo_size);
+                                       }
+                               }
+                               */
+
+                               #if 0
+                               printf( "SDTTdataLoop (%d:%d) %d:%d[%d:%d:%d]%d\n", 
+                                       i, j, 
+                                       sdtd.logo_type, 
+                                       loop->logo_id, 
+                                       service->transport_stream_id, 
+                                       service->original_network_id, 
+                                       service->service_id, 
+                                       loop->data_size
+                               );
+                               #endif
+
+                               if ( is_bs_cs_logo ) {
+                                       fprintf(outfile, "  <logo ts=\"%d\" on=\"%d\" sv=\"%d\" type=\"%d\">%d</logo>\n", 
+                                               service->transport_stream_id, 
+                                               service->original_network_id, 
+                                               service->service_id, 
+                                               sdtd.logo_type, 
+                                               loop->logo_id);
+                               }
+                       }
+
+                       if ( is_bs_cs_logo ) {
+                               void* logo = NULL;
+                               int logo_size = 0;
+
+                               clt2png(loop->data, &logo, &logo_size);
+                               memset(Logo, '\0', sizeof(Logo));
+                               base64_encode(Logo, logo, logo_size);
+                               xmlspecialchars(Logo);
+                               fprintf(outfile, "  <logodata id=\"%d\">%s</logodata>\n", 
+                                       loop->logo_id, 
+                                       Logo);
+                       }
+               }
+       }
        for(lp = 0 ; lp < staCount ; lp++){
                memset(ServiceName, '\0', sizeof(ServiceName));
                strcpy(ServiceName, pStas[lp].name);
@@ -503,18 +512,23 @@ int main(int argc, char *argv[])
 
                fprintf(outfile, "  <channel id=\"%s\">\n", pStas[lp].ontv);
                fprintf(outfile, "    <display-name lang=\"ja_JP\">%s</display-name>\n", ServiceName);
+               fprintf(outfile, "    <id ts=\"%d\" on=\"%d\" sv=\"%d\"/>\n", pStas[lp].tsId, pStas[lp].onId, pStas[lp].svId);
 
                memset(Logo, '\0', sizeof(Logo));
                for ( i = 0 ; i < 6 ; i++) {
                        if (pStas[lp].logo_array[i].logo) {
                                base64_encode(Logo, pStas[lp].logo_array[i].logo, pStas[lp].logo_array[i].logo_size);
                                xmlspecialchars(Logo);
-                               fprintf(outfile, "    <logo id=\"%02d\">%s</logo>\n", i, Logo);
+                               fprintf(outfile, "    <logo type=\"%02d\">%s</logo>\n", i, Logo);
                        }
                }
 
                fprintf(outfile, "  </channel>\n");
        }
+       if ( is_bs_cs_logo ) {
+               fprintf(outfile, "</tv>\n");
+               goto cleanup;
+       }
        for(lp = 0 ; lp < staCount ; lp++){
                GetEIT(infile, outfile, &pStas[lp], secs);
        }
@@ -538,3 +552,4 @@ cleanup:
 
        return 0;
 }
+