home *** CD-ROM | disk | FTP | other *** search
/ MPEG Toolkit / MPEG Toolkit.iso / dos / mpegstat / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-01  |  7.2 KB  |  336 lines

  1. /* MPEGSTAT - analyzing tool for MPEG-I video streams
  2.  * 
  3.  * Technical University of Berlin, Germany, Dept. of Computer Science
  4.  * Tom Pfeifer - Multimedia systems project - pfeifer@fokus.gmd.de
  5.  *
  6.  * Jens Brettin, Harald Masche, Alexander Schulze, Dirk Schubert
  7.  *
  8.  * This program uses parts of the source code of the Berkeley MPEG player
  9.  *
  10.  * ---------------------------
  11.  *
  12.  * Copyright (c) 1993 Technical University of Berlin, Germany
  13.  *
  14.  * for the parts of the Berkeley player used:
  15.  *
  16.  * Copyright (c) 1992 The Regents of the University of California.
  17.  * All rights reserved.
  18.  *
  19.  * ---------------------------
  20.  *
  21.  * Permission to use, copy, modify, and distribute this software and its
  22.  * documentation for any purpose, without fee, and without written agreement is
  23.  * hereby granted, provided that the above copyright notices and the following
  24.  * two paragraphs appear in all copies of this software.
  25.  * 
  26.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA 
  27.  * or the Technical University of Berlin BE LIABLE TO ANY PARTY FOR
  28.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  29.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  30.  * CALIFORNIA or the Technical University of Berlin HAS BEEN ADVISED OF THE 
  31.  * POSSIBILITY OF SUCH DAMAGE.
  32.  * 
  33.  * THE UNIVERSITY OF CALIFORNIA and the Technical University of Berlin 
  34.  * SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
  35.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  36.  * PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE 
  37.  * UNIVERSITY OF CALIFORNIA and the Technical University of Berlin HAVE NO 
  38.  * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, 
  39.  * OR MODIFICATIONS.
  40.  */
  41.  
  42. /* MAIN.C CHANGED FOR MPEG ANALYZER, 1993 */
  43.  
  44. #include "video.h"
  45. #include "proto.h"
  46. #include <sys/types.h>
  47. #include <signal.h>
  48. #ifndef MIPS
  49. #ifndef DOS
  50. #include <netinet/in.h>
  51. #endif
  52. #else
  53. #include <bsd/netinet/in.h>
  54. #endif
  55.  
  56. #include "util.h"
  57.  
  58. /* Define buffer length. */
  59.  
  60. #define BUF_LENGTH 80000
  61.  
  62. /* Function return type declarations */
  63. void usage();
  64.  
  65. /* External declaration of main decoding call. */
  66.  
  67. extern VidStream *mpegVidRsrc();
  68. extern VidStream *NewVidStream();
  69.  
  70. /* Declaration of global variable to hold dither info. */
  71.  
  72. int ditherType;
  73.  
  74. /* Global file pointer to incoming data. */
  75. FILE *input;
  76.  
  77. /* more global objects */
  78.  
  79. int LUM_RANGE;
  80. int CR_RANGE;
  81. int CB_RANGE;
  82.  
  83. int *lum_values;
  84. int *cr_values;
  85. int *cb_values;
  86.  
  87. /* End of File flag. */
  88. static int EOF_flag = 0;
  89.  
  90. /* Loop flag. */
  91. int loopFlag = 0;
  92.  
  93. /* Shared memory flag. */
  94. int shmemFlag = 0;
  95.  
  96. /* Quiet flag. */
  97. int quietFlag = 0;
  98.  
  99. /* Display image on screen? */
  100. int noDisplayFlag = 0;
  101.  
  102. /* Setjmp/Longjmp env. */
  103. jmp_buf env;
  104.  
  105.  
  106.     
  107. /*
  108.  *--------------------------------------------------------------
  109.  *
  110.  * get_more_data --
  111.  *
  112.  *    Called by correct_underflow in bit parsing utilities to
  113.  *      read in more data.
  114.  *
  115.  * Results:
  116.  *    Input buffer updated, buffer length updated.
  117.  *      Returns 1 if data read, 0 if EOF, -1 if error.
  118.  *
  119.  * Side effects:
  120.  *      None.
  121.  *
  122.  *--------------------------------------------------------------
  123.  */
  124.  
  125. int 
  126. get_more_data(buf_start, max_length, length_ptr, buf_ptr)
  127.      unsigned int *buf_start;
  128.      int max_length;
  129.      int *length_ptr;
  130.      unsigned int **buf_ptr;
  131. {
  132.   
  133.   int length, num_read, i, request;
  134.   unsigned char *buffer, *mark;
  135.   unsigned int *lmark;
  136.  
  137.   if (EOF_flag) return 0;
  138.  
  139.   length = *length_ptr;
  140.   buffer = (unsigned char *) *buf_ptr;
  141.  
  142.   if (length > 0) {
  143.     memcpy((unsigned char *) buf_start, buffer, (length*4));
  144.     mark = ((unsigned char *) (buf_start + length));
  145.   }
  146.   else {
  147.     mark = (unsigned char *) buf_start;
  148.     length = 0;
  149.   }
  150.  
  151.   request = (max_length-length)*4;
  152.   
  153.  
  154.   num_read = fread( mark, 1, request, input);
  155.  
  156.   byte_count(num_read);
  157.  
  158.   /* Paulo Villegas - 26/1/1993: Correction for 4-byte alignment */
  159.   {
  160.     int num_read_rounded;
  161.     unsigned char *index;
  162.  
  163.     num_read_rounded = 4*(num_read/4);
  164.  
  165.     /* this can happen only if num_read<request; i.e. end of file reached */
  166.     if( num_read_rounded < num_read )
  167.       { 
  168.      num_read_rounded = 4*( num_read/4+1 );
  169.      /* fill in with zeros */
  170.      for( index=mark+num_read; index<mark+num_read_rounded; *(index++)=0 );
  171.      /* advance to the next 4-byte boundary */
  172.      num_read = num_read_rounded;
  173.       }
  174.   }
  175.   
  176.   if   (num_read < 0) {
  177.     return -1;
  178.   }
  179.   else if (num_read == 0) {
  180.     *buf_ptr = buf_start;
  181.     
  182.     /* Make 32 bits after end equal to 0 and 32
  183.        bits after that equal to seq end code
  184.        in order to prevent messy data from infinite
  185.        recursion.
  186.     */
  187.  
  188.     *(buf_start + length) = 0x0;
  189.     *(buf_start + length+1) = SEQ_END_CODE;
  190.  
  191.     EOF_flag = 1;
  192.     return 0;
  193.   }
  194.  
  195.   lmark = (unsigned int *) mark;
  196.  
  197.   num_read = num_read/4;
  198.  
  199.   for (i=0; i<num_read; i++) {
  200.     *lmark = htonl(*lmark);
  201.     lmark++;
  202.   }
  203.  
  204.   *buf_ptr = buf_start;
  205.   *length_ptr = length + num_read;
  206.  
  207.   return 1;
  208. }
  209.  
  210. /*
  211.  *--------------------------------------------------------------
  212.  *
  213.  * int_handler --
  214.  *
  215.  *    Handles Cntl-C interupts..
  216.  *
  217.  * Results:
  218.  *    None.
  219.  *
  220.  * Side effects:
  221.  *    None.
  222.  *
  223.  *--------------------------------------------------------------
  224.  */
  225. void
  226. int_handler()
  227. {
  228.   if (!quietFlag) {
  229.     fprintf(stderr, "Interrupted!\n");
  230.   }
  231.   if (curVidStream != NULL)
  232.     DestroyVidStream(curVidStream);
  233.   exit(1);
  234. }
  235.  
  236.  
  237. /*
  238.  *--------------------------------------------------------------
  239.  *
  240.  * main --
  241.  *
  242.  *    Parses command line, starts decoding and displaying.
  243.  *
  244.  * Results:
  245.  *    None.
  246.  *
  247.  * Side effects:
  248.  *    None.
  249.  *
  250.  *--------------------------------------------------------------
  251.  */
  252.  
  253. void
  254. main(argc, argv)
  255.      int argc;
  256.      char **argv;
  257. {
  258.  
  259.   char *name;
  260.   static VidStream *theStream;
  261.   int mark;
  262.   int i;
  263.  
  264.   mark = 1;
  265.  
  266.   name = "";
  267.   input = stdin;
  268.   ditherType = ORDERED2_DITHER;
  269.   LUM_RANGE = 8;
  270.   CR_RANGE = CB_RANGE = 4;
  271.   noDisplayFlag = 0;
  272.  
  273. #ifdef SH_MEM
  274.   shmemFlag = 1;
  275. #endif
  276.  
  277.   printf("\n%s -- MPEG Analyzer for MPEG I video streams\n", argv[0]);
  278.   if(argc > 2 ) {
  279.     printf("Usage: %s [mpeg stream]\n", argv[0]);
  280.     exit(1);
  281.   }
  282.  
  283.   if(argc == 1) 
  284.     input = stdin;
  285.   else {
  286. #ifdef DOS
  287.      input = fopen(argv[1], "rb");
  288. #else
  289.      input = fopen(argv[1], "r");
  290. #endif
  291.      if (input == NULL) {
  292.         fprintf(stderr, "Could not open file %s\n", argv[1]);
  293.     exit (-1);
  294.      }
  295.   } 
  296.  
  297.   printf("\nReading %s\n\n", argc == 1 ? "STDIN" : argv[1]);
  298.  
  299.   lum_values = (int *) malloc(LUM_RANGE*sizeof(int));
  300.   cr_values = (int *) malloc(CR_RANGE*sizeof(int));
  301.   cb_values = (int *) malloc(CB_RANGE*sizeof(int));
  302.  
  303.   signal(SIGINT, int_handler);
  304.  
  305.   init_tables();
  306.   
  307.   if (setjmp(env) != 0) {
  308.  
  309.     DestroyVidStream(theStream);
  310.  
  311.     rewind(input);
  312.  
  313.     EOF_flag = 0;
  314.     curBits = 0;
  315.     bitOffset = 0;
  316.     bufLength = 0;
  317.     bitBuffer = NULL;
  318.     totNumFrames = 0;
  319.     init_stats();
  320.  
  321.   }
  322.  
  323.   theStream = NewVidStream(BUF_LENGTH);
  324.  
  325.  
  326.   mpegVidRsrc(0, theStream);
  327.  
  328.   if (ditherType == Twox2_DITHER) i = 2;
  329.   else i = 1;  
  330.  
  331.   realTimeStart = ReadSysClock();
  332.  
  333.   while (1) mpegVidRsrc(0, theStream);
  334. }
  335.  
  336.