Skip to content
This repository
Browse code

cannot read from anything but stdin

  • Loading branch information...
commit 8eb5c1133d390cb3eeff4a8641026782bdcd7167 1 parent f2a065c
authored April 04, 2012

Showing 1 changed file with 62 additions and 53 deletions. Show diff stats Hide diff stats

  1. 115  src/mpeg2desc.c
115  src/mpeg2desc.c
@@ -39,8 +39,8 @@
39 39
 // #define SHOWDATA
40 40
 
41 41
 static int
42  
-    inputpos = 0,
43  
-    queuedlen = 0;
  42
+    inputpos = 0, /* position in stdin */
  43
+    queuedlen = 0; /* total nr bytes awaiting writing to output files */
44 44
 
45 45
 const char * const frametype = "0IPB4567";
46 46
 
@@ -94,6 +94,9 @@ static int64_t readpts(const unsigned char *buf)
94 94
   } /*readpts*/
95 95
 
96 96
 static bool hasbecomevalid(int stream, const struct ofd *o)
  97
+  /* checks if there is a valid packet header at the start of the data to be
  98
+    written to the specified output stream. Assumes there is at least 4 bytes
  99
+    of data waiting to be written. */
97 100
   {
98 101
     unsigned char quad[4];
99 102
     const struct fdbuf * const f1 = o->firstbuf;
@@ -127,7 +130,12 @@ static bool hasbecomevalid(int stream, const struct ofd *o)
127 130
     return false;
128 131
   } /*hasbecomevalid*/
129 132
 
130  
-static bool dowork(bool checkin)
  133
+static bool dowork
  134
+  (
  135
+    bool checkin /* whether to check if bytes are available to be read from stdin */
  136
+  )
  137
+  /* writes pending packets to output streams. This is done concurrently to allow
  138
+    for use with pipes. Returns true iff stdin has input available. */
131 139
   {
132 140
     int highestfd = -1;
133 141
     struct timeval tv;
@@ -144,10 +152,10 @@ static bool dowork(bool checkin)
144 152
       } /*if*/
145 153
     while (true)
146 154
       {
147  
-        int i ,minq=-1;
  155
+        int i, minq = -1;
148 156
         for (i = 0; i < numofd; i++)
149 157
           {
150  
-            struct ofd *o=&outputfds[ofdlist[i]];
  158
+            struct ofd *o = &outputfds[ofdlist[i]];
151 159
             if (o->fd != -1)
152 160
               {
153 161
                 if (o->fd == -2)
@@ -168,7 +176,7 @@ static bool dowork(bool checkin)
168 176
                 // at this point, fd >= 0 
169 177
                 if (minq == -1 || o->len < minq)
170 178
                   {
171  
-                    minq=o->len;
  179
+                    minq = o->len;
172 180
                   } /*if*/
173 181
                 if ((o->len > 0 && o->isvalid) || o->len >= 4)
174 182
                   {
@@ -212,12 +220,12 @@ static bool dowork(bool checkin)
212 220
               {
213 221
                 struct fdbuf * const f = o->firstbuf;
214 222
                 int written;
215  
-                if (!o->isvalid && hasbecomevalid(ofdlist[i],o))
  223
+                if (!o->isvalid && hasbecomevalid(ofdlist[i], o))
216 224
                     o->isvalid = true;
217 225
                 if (o->isvalid)
218  
-                    written = write(o->fd,f->buf+f->pos,f->len-f->pos);
  226
+                    written = write(o->fd, f->buf + f->pos, f->len - f->pos);
219 227
                 else if (f->len - f->pos > 0)
220  
-                    written = 1;
  228
+                    written = 1; /* discard one byte while waiting for valid packet */
221 229
                 else
222 230
                     written = 0;
223 231
                 if (written == -1)
@@ -229,6 +237,7 @@ static bool dowork(bool checkin)
229 237
                 f->pos += written;
230 238
                 if (f->pos == f->len)
231 239
                   {
  240
+                  /* finished writing buffer at head of queue */
232 241
                     o->firstbuf = f->next;
233 242
                     if (o->lastbufptr == &f->next)
234 243
                         o->lastbufptr = &o->firstbuf;
@@ -251,36 +260,36 @@ static void flushwork(void)
251 260
         dowork(false);
252 261
   } /*flushwork*/
253 262
 
254  
-static int forceread(void *ptr,int len,FILE *h)
  263
+static void forceread(void *ptr, int len)
  264
+  /* reads the specified number of bytes from standard input, finishing processing
  265
+    if EOF is reached. */
255 266
   {
256  
-    while (!dowork(true));
257  
-    if (fread(ptr,1,len,h) != len)
  267
+    while (!dowork(true))
  268
+      /* flush output queues while waiting for more input */;
  269
+    if (fread(ptr, 1, len, stdin) != len)
258 270
       {
259 271
         fprintf(stderr,"Could not read\n");
260 272
         flushwork();
261 273
         exit(1);
262 274
       } /*if*/
263 275
     inputpos += len;
264  
-    return len;
265 276
   } /*forceread*/
266 277
 
267  
-static void forceread1(void *ptr,FILE *h)
  278
+static unsigned char forceread1(void)
  279
+  /* reads one  byte from standard input, finishing processing
  280
+    if EOF is reached. */
268 281
   {
269  
-    int v = fgetc(h);
270  
-    if (v < 0)
271  
-      {
272  
-        fprintf(stderr,"Could not read\n");
273  
-        flushwork();
274  
-        exit(1);
275  
-      } /*if*/
276  
-    ((unsigned char *)ptr)[0] = v;
277  
-    inputpos += 1;
  282
+    unsigned char c;
  283
+    forceread(&c, 1);
  284
+    return c;
278 285
   } /*forceread1*/
279 286
 
280  
-static void writetostream(int stream,unsigned char *buf,int len)
  287
+static void writetostream(int stream, unsigned char *buf, int len)
  288
+  /* queues more data to be written to the output file for the specified stream id,
  289
+    if I am writing it. */
281 290
   {
282 291
     struct ofd * const o = &outputfds[stream];
283  
-    if (o->fd == -1)
  292
+    if (o->fd == -1) /* not extracting this stream */
284 293
         return;
285 294
     while (len > 0)
286 295
       {
@@ -303,7 +312,7 @@ static void writetostream(int stream,unsigned char *buf,int len)
303 312
         if (thislen > len)
304 313
             thislen = len;
305 314
         o->len += thislen;
306  
-        memcpy(fb->buf+fb->len,buf,thislen);
  315
+        memcpy(fb->buf + fb->len, buf, thislen);
307 316
         fb->len += thislen;
308 317
         len -= thislen;
309 318
         buf += thislen;
@@ -402,7 +411,7 @@ int main(int argc,char **argv)
402 411
       {
403 412
         firstpts[i] = -1;
404 413
       } /*for*/
405  
-    forceread(&hdr,4,stdin);
  414
+    forceread(&hdr,4);
406 415
     while (true)
407 416
       {
408 417
         const int disppos = inputpos - 4; /* where packet actually started */
@@ -410,14 +419,14 @@ int main(int argc,char **argv)
410 419
           {
411 420
       // start codes:
412 421
         case 0x100 + MPID_PICTURE: // picture header
413  
-            forceread(buf,4,stdin);
  422
+            forceread(buf,4);
414 423
             if (outputenglish)
415 424
                 printf("%08x: picture hdr, frametype=%c, temporal=%d\n",disppos,frametype[(buf[1]>>3)&7],(buf[0]<<2)|(buf[1]>>6));
416  
-            forceread(&hdr,4,stdin);
  425
+            forceread(&hdr,4);
417 426
         break;
418 427
 
419 428
         case 0x100 + MPID_SEQUENCE: // sequence header
420  
-            forceread(buf,8,stdin);
  429
+            forceread(buf,8);
421 430
             if (outputenglish)
422 431
                 printf("%08x: sequence hdr: %dx%d, a/f:%02x, bitrate=%d\n"
423 432
                        ,disppos
@@ -427,34 +436,34 @@ int main(int argc,char **argv)
427 436
                        ,(buf[4]<<10)|(buf[5]<<2)|(buf[6]>>6)
428 437
                     );
429 438
             if (buf[7] & 2)
430  
-                forceread(buf+8,64,stdin);
  439
+                forceread(buf+8,64);
431 440
             if (buf[7] & 1)
432  
-                forceread(buf+8,64,stdin);
433  
-            forceread(&hdr,4,stdin);
  441
+                forceread(buf+8,64);
  442
+            forceread(&hdr,4);
434 443
         break;
435 444
 
436 445
         case 0x100 + MPID_EXTENSION: // extension header
437  
-            forceread(buf,1,stdin);
  446
+            forceread(buf,1);
438 447
             switch (buf[0] >> 4)
439 448
               {
440 449
             case 1:
441 450
                 if (outputenglish)
442 451
                     printf("%08x: sequence extension hdr\n",disppos);
443  
-                forceread(buf+1,5,stdin);
  452
+                forceread(buf+1,5);
444 453
             break;
445 454
             case 2:
446 455
                 if (outputenglish)
447 456
                     printf("%08x: sequence display extension hdr\n",disppos);
448  
-                forceread(buf+1,(buf[0]&1)?7:3,stdin);
  457
+                forceread(buf+1,(buf[0]&1)?7:3);
449 458
             break;
450 459
             case 7:
451 460
                 if (outputenglish)
452 461
                     printf("%08x: picture display extension hdr\n",disppos);
453 462
             break;
454 463
             case 8:
455  
-                forceread(buf+1,4,stdin);
  464
+                forceread(buf+1,4);
456 465
                 if (buf[4] & 64)
457  
-                    forceread(buf+5,2,stdin);
  466
+                    forceread(buf+5,2);
458 467
                 if (outputenglish)
459 468
                   {
460 469
                     printf("%08x: picture coding extension hdr%s%s\n",
@@ -468,17 +477,17 @@ int main(int argc,char **argv)
468 477
                     printf("%08x: extension hdr %x\n",disppos,buf[0]>>4);
469 478
             break;
470 479
               } /*switch*/
471  
-            forceread(&hdr,4,stdin);
  480
+            forceread(&hdr,4);
472 481
         break;
473 482
 
474 483
         case 0x100 + MPID_SEQUENCE_END: // end of sequence
475 484
             if (outputenglish)
476 485
                 printf("%08x: end of sequence\n",disppos);
477  
-            forceread(&hdr,4,stdin);
  486
+            forceread(&hdr,4);
478 487
         break;
479 488
 
480 489
         case 0x100 + MPID_GOP: // group of pictures
481  
-            forceread(buf,4,stdin);
  490
+            forceread(buf,4);
482 491
             if (outputenglish)
483 492
               {
484 493
                 printf("%08x: GOP: %s%d:%02d:%02d.%02d, %s%s\n"
@@ -492,23 +501,23 @@ int main(int argc,char **argv)
492 501
                        ,buf[3]&32?", broken":""
493 502
                     );
494 503
               } /*if*/
495  
-            forceread(&hdr,4,stdin);
  504
+            forceread(&hdr,4);
496 505
         break;
497 506
 
498 507
         case 0x100 + MPID_PROGRAM_END: // end of program stream
499 508
             if (outputenglish)
500 509
                 printf("%08x: end of program stream\n",disppos);
501  
-            forceread(&hdr,4,stdin);
  510
+            forceread(&hdr,4);
502 511
         break;
503 512
 
504 513
         case 0x100 + MPID_PACK: // mpeg_pack_header
505 514
           {
506 515
             uint32_t scr,scrhi,scrext;
507 516
             int64_t fulltime;
508  
-            forceread(buf,8,stdin);
  517
+            forceread(buf,8);
509 518
             if ((buf[0] & 0xC0) == 0x40)
510 519
               {
511  
-                forceread(buf+8,2,stdin);
  520
+                forceread(buf+8,2);
512 521
                 scrhi = (buf[0] & 0x20) >> 5;
513 522
                 scr =
514 523
                         (buf[0] & 0x18) << 27
@@ -551,7 +560,7 @@ int main(int argc,char **argv)
551 560
               } /*if*/
552 561
             if (outputenglish)
553 562
                 printf("%08x: mpeg%c pack hdr, %" PRId64 ".%03" PRId64 " sec\n",disppos,mpeg2?'2':'1',fulltime/SCRTIME,(fulltime%SCRTIME)/(SCRTIME/1000));
554  
-            forceread(&hdr,4,stdin);
  563
+            forceread(&hdr,4);
555 564
           }
556 565
         break;
557 566
 
@@ -652,9 +661,10 @@ int main(int argc,char **argv)
652 661
                     printf("pes video %d", packetid - MPID_VIDEO_FIRST);
653 662
                 has_extension = true;
654 663
               } /*if*/
655  
-            forceread(buf,2,stdin); // pes packet length
  664
+            forceread(buf,2); // pes packet length
656 665
             extra = buf[0] << 8 | buf[1];
657  
-            readlen = forceread(buf, extra > sizeof buf ? sizeof buf : extra, stdin);
  666
+            readlen = extra > sizeof buf ? sizeof buf : extra;
  667
+            forceread(buf, readlen);
658 668
             extra -= readlen;
659 669
             if (outputenglish)
660 670
               {
@@ -839,24 +849,23 @@ int main(int argc,char **argv)
839 849
 
840 850
             while (extra)
841 851
               {
842  
-                readlen = forceread(buf,(extra>sizeof(buf))?sizeof(buf):extra,stdin);
  852
+                readlen = extra > sizeof buf ? sizeof(buf) : extra;
  853
+                forceread(buf, readlen);
843 854
                 if (dowrite)
844 855
                     writetostream(ntohl(hdr)&255,buf,readlen);
845 856
                 extra -= readlen;
846 857
               } /*while*/
847  
-            forceread(&hdr,4,stdin);
  858
+            forceread(&hdr,4);
848 859
           }
849 860
         break;
850 861
 
851 862
         default:
852 863
             do
853 864
               {
854  
-                unsigned char c;
855 865
                 if (outputenglish && !nounknown)
856 866
                     printf("%08x: unknown hdr: %08x\n",disppos,ntohl(hdr));
857 867
                 hdr >>= 8;
858  
-                forceread1(&c,stdin);
859  
-                hdr |= c << 24;
  868
+                hdr |= forceread1() << 24;
860 869
               }
861 870
             while ((ntohl(hdr) & 0xffffff00) != 0x100);
862 871
         break;

0 notes on commit 8eb5c11

Please sign in to comment.
Something went wrong with that request. Please try again.