--- multiplexer/en300468ts.c 2010/09/29 21:36:23 1.20 +++ multiplexer/en300468ts.c 2010/09/29 21:37:12 1.21 @@ -57,11 +57,13 @@ static int ipfd; #define TABBUF_SIZE (1<<15) #define MAX_PSI_SIZE (4096+1) -static unsigned int tabin[TABLE_PID_LAST-TABLE_PID_FIRST+1]; -static unsigned int tabinold[TABLE_PID_LAST-TABLE_PID_FIRST+1]; -static unsigned int tabout[TABLE_PID_LAST-TABLE_PID_FIRST+1]; -static unsigned char conticnt[TABLE_PID_LAST-TABLE_PID_FIRST+1]; -static unsigned char *tabbuf[TABLE_PID_LAST-TABLE_PID_FIRST+1]; +static struct { + unsigned char *tabbuf; + unsigned int tabin; + unsigned int tabout; + unsigned char tabinold; + unsigned char conticnt; +} perpid[TABLE_PID_LAST-TABLE_PID_FIRST+1]; #define TABLEID_FIRST 0x40 #define TABLEID_LAST 0x7F @@ -1469,7 +1471,7 @@ static void gentab(struct sitab *st, str unsigned char *b; memset(&descrcnt[0], 0, sizeof(descrcnt)); i = st->pid - TABLE_PID_FIRST; - b = &tabbuf[i][tabin[i]]; + b = &perpid[i].tabbuf[perpid[i].tabin]; *b++ = st->pid >> 8; *b++ = st->pid; switch (st->esi) { @@ -1486,7 +1488,7 @@ static void gentab(struct sitab *st, str fprintf(stderr, "internal error (gentab, %d)\n", st->esi); exit(1); } - tabin[i] += l+2; + perpid[i].tabin += l+2; } static enum enumsi alloctab(long pid, long tid) @@ -2027,18 +2029,19 @@ static void argloop(int f0) } i = 0; while (tmo != 0 && i <= TABLE_PID_LAST-TABLE_PID_FIRST) { - if ((tabin[i] > tabout[i]) - && (r >= (((((tabbuf[i][tabout[i]+3] & 0x0F) << 8) - + tabbuf[i][tabout[i]+4] + TS_HEADSLEN + TS_PFIELDLEN - + TS_PACKET_SIZE - TS_PACKET_HEADSIZE) - * 131) / 128))) { + if ((perpid[i].tabin > perpid[i].tabout) + && (r >= (((((perpid[i].tabbuf[perpid[i].tabout+3] & 0x0F) << 8) + + perpid[i].tabbuf[perpid[i].tabout+4] + TS_HEADSLEN + + TS_PFIELDLEN + TS_PACKET_SIZE - TS_PACKET_HEADSIZE) + * 131) / 128))) { tmo = 0; #ifdef DEBUG { int x; - fprintf(stderr, "tabbuf[%d..%d-1]:\n", tabout[i], tabin[i]); - for (x = tabout[i]; x < tabin[i]; x++) { - fprintf(stderr, "%02x ", tabbuf[i][x]); + fprintf(stderr, "tabbuf[%d..%d-1]:\n", + perpid[i].tabout, perpid[i].tabin); + for (x = perpid[i].tabout; x < perpid[i].tabin; x++) { + fprintf(stderr, "%02x ", perpid[i].tabbuf[x]); } fprintf(stderr, "\n"); } @@ -2054,9 +2057,9 @@ static void argloop(int f0) fprintf(stderr, "internal error (pid)\n"); exit(1); } - tabinold[i] = tabin[i]; + perpid[i].tabinold = perpid[i].tabin ? 1 : 0; if (tmo != 0) { - if (tabin[i] == 0) { + if (perpid[i].tabin == 0) { i = (st->soon.tv_sec - tv.tv_sec) * 1000 + (st->soon.tv_usec - tv.tv_usec) / 1000; if (i <= 0) { @@ -2071,19 +2074,20 @@ static void argloop(int f0) n = pollfd_poll(tmo); gettimeofday(&tv, NULL); for (i = 0; i <= TABLE_PID_LAST-TABLE_PID_FIRST; i++) { - while ((tabin[i] > tabout[i]) - && (r >= (((((tabbuf[i][tabout[i]+3] & 0x0F) << 8) - + tabbuf[i][tabout[i]+4] + TS_HEADSLEN + TS_PFIELDLEN - + TS_PACKET_SIZE - TS_PACKET_HEADSIZE) - * 131) / 128))) { - tabout[i] += tab2ts(&tabbuf[i][tabout[i]], &conticnt[i]); + while ((perpid[i].tabin > perpid[i].tabout) + && (r >= (((((perpid[i].tabbuf[perpid[i].tabout+3] & 0x0F) << 8) + + perpid[i].tabbuf[perpid[i].tabout+4] + TS_HEADSLEN + + TS_PFIELDLEN + TS_PACKET_SIZE - TS_PACKET_HEADSIZE) + * 131) / 128))) { + perpid[i].tabout += + tab2ts(&perpid[i].tabbuf[perpid[i].tabout], &perpid[i].conticnt); r = outout - outin; if (r < 0) { r += OUTBUF_SIZE; } } - if (tabin[i] <= tabout[i]) { - tabin[i] = tabout[i] = 0; + if (perpid[i].tabin <= perpid[i].tabout) { + perpid[i].tabin = perpid[i].tabout = 0; } } if ((n > 0) && (n1 >= 0) && (r = pollfd_rev(n1))) { @@ -2111,8 +2115,8 @@ static void argloop(int f0) pst = &runtab; while ((st = *pst) != NULL) { i = st->pid - TABLE_PID_FIRST; - if ((tabinold[i] == 0) - && (tabin[i] < (TABBUF_SIZE-MAX_PSI_SIZE+1-2)) + if ((perpid[i].tabinold == 0) + && (perpid[i].tabin < (TABBUF_SIZE-MAX_PSI_SIZE+1-2)) && ((st->soon.tv_sec < tv.tv_sec) || ((st->soon.tv_sec == tv.tv_sec) && (st->soon.tv_usec <= tv.tv_usec)))) { @@ -2181,8 +2185,8 @@ static void argloop(int f0) fprintf(stderr, "\n"); #endif i = newtab->pid - TABLE_PID_FIRST; - if ((tabbuf[i] == NULL) - && ((tabbuf[i] = malloc(TABBUF_SIZE)) == NULL)) { + if ((perpid[i].tabbuf == NULL) + && ((perpid[i].tabbuf = malloc(TABBUF_SIZE)) == NULL)) { fprintf(stderr, "malloc failed for table buffer pid=%02lx\n", newtab->pid); free(newtab->tab); @@ -2245,13 +2249,8 @@ int main(int argc, char *argv[]) system_init(); unblockf(STDIN_FILENO); unblockf(STDOUT_FILENO); - memset(&tabin[0], 0, sizeof(tabin)); - memset(&tabout[0], 0, sizeof(tabout)); - memset(&conticnt[0], 0, sizeof(conticnt)); memset(&nextversion[0], 0, sizeof(nextversion)); - for (a = TABLE_PID_LAST-TABLE_PID_FIRST; a >= 0; a--) { - tabbuf[a] = NULL; - } + memset(&perpid[0], 0, sizeof(perpid)); a = 1; do { if ((a < argc) && (strcmp(argv[a], "-"))) {