Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

tsfix.c with mod

theshadow27 May 10th, 2014 261 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <inttypes.h>
  4.  
  5. #define SIZE 5000*1000
  6. uint8_t buf[SIZE];
  7.  
  8. // continuity counter
  9. uint8_t cctr[2048];
  10.  
  11. typedef struct Header {
  12.     uint8_t data[188];
  13.     uint8_t mask[188];
  14.     double threshold;
  15.     int flags;
  16. } Header;
  17.  
  18. // Note here that any 0x00 masks MUST have non-zero data, since {0x00, 0x00} is taken as the termination condition.
  19. Header headers[] = {
  20.     {    // video packet
  21.         { 0x47, 0x43, 0xE8, 0x30,   0x07, 0x10, 0x01, 0x2D, 0xF1, 0xBB, 0x7E, 0x01, 0x00, 0x00, 0x01, 0xE0, 0x00, 0xC0, 0x81, 0x80, 0x07, 0x21, 0x01, 0x6F, 0x78, 0xCD, 0xFF, 0xFF, 0x00, 0x00, 0x01, 0xB0, 0x03, 0x00, 0x00, 0x01, 0xB5, 0x0D, 0x0F, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x20, 0x00, 0xC4, 0x8D, 0xC0, 0x00, 0x46, 0x56, 0xC0, 0x1E, 0xC0, 0x01, 0xF6, 0xEA, 0x9A, 0xFC, 0x7C, 0x2E, 0xEE, 0x2C, 0x08, 0x78, 0x28, 0xC7, 0x00, 0x00, 0x01, 0xB2, 0x65, 0x6D, 0x34, 0x76, 0x20, 0x34, 0x2E, 0x33, 0x2E, 0x30, 0x2E, 0x30, 0x00, 0xC3, 0xFF, 0x00, 0x00, 0x01, 0xB6, 0x18},
  22.         { 0xFF, 0xFF, 0xFF, 0xF0,   0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x7E, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF1, 0x00, 0x01, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x80, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC0},
  23.          0.30,
  24.          1
  25.     },
  26.     {  // video packet2
  27.         { 0x47, 0x43, 0xE8, 0x32,   0x07, 0x10, 0x01, 0x33, 0x42, 0x77, 0x7E, 0x01, 0x00, 0x00, 0x01, 0xE0, 0x00, 0x00, 0x81, 0x80, 0x07, 0x21, 0x01, 0x99, 0xBB, 0xBD, 0xFF, 0xFF, 0x00, 0x00, 0x01, 0xB6, 0x40},
  28.         { 0xFF, 0xFF, 0xFF, 0xF0,   0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x7E, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF1, 0x00, 0x01, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC0},
  29.         0.4,
  30.         1
  31.     },
  32.     { // null packet
  33.         { 0x47, 0x1F, 0xFF, 0x10,   0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  34.         { 0xFF, 0xFF, 0xFF, 0xF0,   0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  35.         0.70,
  36.         0
  37.     },
  38.     {   // from gospacex's fix_4740001x_packets
  39.         { 0x47, 0x40, 0x00, 0x10,   0x00, 0x00, 0xB0, 0x11, 0x00, 0x00, 0xC1, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x10, 0x00, 0x01, 0xE0, 0x20, 0xD3, 0x6A, 0xF0, 0xAC },  
  40.         { 0xFF, 0xFF, 0xFF, 0xF0,   0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
  41.         0.90,
  42.         0
  43.     },
  44.     {  // from gospacex's fix_
  45.         { 0x47, 0x40, 0x20, 0x10,   0x00, 0x02, 0xb0, 0x1f, 0x00, 0x01, 0xC1, 0x00, 0x00, 0xe3, 0xe8, 0xf0, 0x00, 0x10, 0xE3, 0xE8, 0xF0, 0x03, 0x1B, 0x01, 0xF5, 0x80, 0xE3, 0xE9, 0xF0, 0x00, 0x81, 0xE3, 0xF3, 0xF0, 0x00, 0x3F},
  46.         { 0xFF, 0xFF, 0xFF, 0xF0,   0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},  
  47.     },
  48.     {   // just add 47
  49.         {0x47},
  50.         {0xFF},
  51.         0,
  52.     }
  53. };
  54.  
  55. //         {0x47, 0x03, 0xE8, 0x13},
  56. //         {0xFF, 0xFF, 0xFF, 0xF0},
  57. #define GETBIT(p, i) (((p)[(i)>>3] << (((i)&7))>>7)&1)
  58. #define PUTBIT(p, i, v) (p)[(i)>>3] = ((p)[(i)>>3] | ((v)<<7>>((i)&7))) & (((((v)<<7) + 0xFF7F) >>((i)&7)))
  59.  
  60.  
  61.  
  62.  
  63. uint8_t _popc8lut_[] = {
  64.         0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
  65.         1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  66.         1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  67.         2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  68.         1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  69.         2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  70.         2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  71.         3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  72.         1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  73.         2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  74.         2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  75.         3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  76.         2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  77.         3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  78.         3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  79.         4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8};
  80.  
  81. #define popc8(x) (_popc8lut_[(uint8_t)x])
  82.  
  83.  
  84.  
  85. int main(int argc, char **argv) {
  86.     int i=0, j, k, len, pid, afc, is_start;
  87.     int in_video = 0;
  88.  
  89.     while(!feof(stdin)) {
  90.         buf[i++] = getchar();
  91.     }
  92.     len = i-1;
  93.  
  94.     for (i=0; i<len; i+=188) {
  95. #if 0
  96.         for(j=0; j<188; j++)
  97.             fprintf(stderr, "%02X ", buf[i+j]);
  98.         fprintf(stderr, "\n");
  99. #endif
  100.  
  101.         for(j=0; 1; j++) {
  102.             int score = 0;
  103.             for (k=1; k<188; k++) {
  104.                 if (headers[j].mask[k]) // count bits different
  105.                     score += 8-popc8(headers[j].mask[k] & (buf[i+k] ^ headers[j].data[k]));
  106.                 else if (!headers[j].data[k])
  107.                     break;  // zero header and mask, termination condition
  108.             }
  109.            
  110.             double prob = score/(8.0 * k);        
  111.             if (prob > headers[j].threshold) {
  112.                 for(k=0; k<188; k++) {
  113.                     if (!(headers[j].mask[k] | headers[j].data[k]))  break;  // zero header and mask, termination condition
  114.                     buf[i+k] = (buf[i+k] &~headers[j].mask[k]) | (headers[j].data[k] & headers[j].mask[k]);
  115.  
  116.                 }
  117.            
  118. //                 for (k=0; k<188*8; k++) {
  119. //                     if (GETBIT(headers[j].mask, k))
  120. //                         PUTBIT(buf, 8*i + k, GETBIT(headers[j].data, k));
  121. //                 }
  122.                 in_video = headers[j].flags;
  123. //                 if (score < 15)
  124.                 fprintf(stderr, "SCORED %d %-4d (%f)\n", j, score, prob);
  125.                 break;
  126.             }
  127.             if(!headers[j].threshold)
  128.                 break;
  129.         }
  130.  
  131.         is_start = buf[i+1] & 0x40;
  132.         pid = ((buf[i+1]<<8) + buf[i+2]) & 0x1FFF;
  133.         afc = (buf[i+3] >> 4) & 3;
  134.  
  135.         if (pid == 1000)
  136.             in_video = 1;
  137.        
  138.         if (buf[i] != 0x47) {
  139.             fprintf(stderr, "Fixing 0x%02X->0x47\n", buf[i]);
  140.             buf[i] = 0x47;
  141.         }
  142.  
  143.         // 1008, 4072 1001 1011
  144.         if (in_video && pid != 0 && pid != 8191 && pid != 1000 && pid != 32 && pid != 64) {
  145.             fprintf(stderr, "PID %d -> 1000\n", pid);
  146.             pid = 1000;
  147.         }
  148.         if (pid == 1000 && !(afc&1)) {
  149.             fprintf(stderr, "AFC %d -> %d\n", afc, afc|1);
  150.             afc |= 1;
  151.         }
  152.         if (is_start && pid == 1000) {
  153.             int j;
  154.             for (j=0; j<188-3; j++) {
  155.                 if (buf[i+j] == 0 && buf[i+j+1] == 0 && buf[i+j+2] == 1)
  156.                     break;
  157.             }
  158.             if (j == 188-3) {
  159.                 fprintf(stderr, "discarding is start\n");
  160.                 is_start = 0;
  161.             }
  162.         }
  163. //without 0x47 fixing 3372
  164. //without pid=1000 3356
  165. //without afc|=1 3624
  166. //ref 3728
  167. //without is_start=0 2532
  168.  
  169.         buf[i+1] = (buf[i+1] & ~0x5F) + (pid>>8) + is_start;
  170.         buf[i+2] = pid;
  171.         buf[i+3] = (buf[i+3] & ~0x30) + (afc << 4);
  172.     }
  173.     for (i=0; i<len; i++)
  174.         putchar(buf[i]);
  175.  
  176.     return 0;
  177. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top