home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Multimed / Multimed.zip / mpegplay.zip / UTIL.C < prev    next >
C/C++ Source or Header  |  1994-02-16  |  11KB  |  461 lines

  1. /*
  2.  * Copyright (c) 1992 The Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  * 
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  * 
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21.  
  22. #include <stdlib.h>
  23. #include "video.h"
  24. #include "proto.h"
  25. #include "util.h"
  26.  
  27. /* Declarations of global variables used. */
  28.  
  29. unsigned int curBits;
  30. int bitOffset;
  31. int bufLength;
  32. unsigned int *bitBuffer;
  33.  
  34. /* Bit masks used by bit i/o operations. */
  35.  
  36. unsigned int nBitMask[] = { 0x00000000, 0x80000000, 0xc0000000, 0xe0000000, 
  37.                 0xf0000000, 0xf8000000, 0xfc000000, 0xfe000000, 
  38.                 0xff000000, 0xff800000, 0xffc00000, 0xffe00000, 
  39.                 0xfff00000, 0xfff80000, 0xfffc0000, 0xfffe0000, 
  40.                 0xffff0000, 0xffff8000, 0xffffc000, 0xffffe000, 
  41.                 0xfffff000, 0xfffff800, 0xfffffc00, 0xfffffe00, 
  42.                 0xffffff00, 0xffffff80, 0xffffffc0, 0xffffffe0, 
  43.                 0xfffffff0, 0xfffffff8, 0xfffffffc, 0xfffffffe};
  44.  
  45. unsigned int bitMask[] = {  0xffffffff, 0x7fffffff, 0x3fffffff, 0x1fffffff, 
  46.                 0x0fffffff, 0x07ffffff, 0x03ffffff, 0x01ffffff,
  47.                 0x00ffffff, 0x007fffff, 0x003fffff, 0x001fffff,
  48.                 0x000fffff, 0x0007ffff, 0x0003ffff, 0x0001ffff,
  49.                 0x0000ffff, 0x00007fff, 0x00003fff, 0x00001fff,
  50.                 0x00000fff, 0x000007ff, 0x000003ff, 0x000001ff,
  51.                 0x000000ff, 0x0000007f, 0x0000003f, 0x0000001f,
  52.                 0x0000000f, 0x00000007, 0x00000003, 0x00000001};
  53.  
  54. unsigned int rBitMask[] = { 0xffffffff, 0xfffffffe, 0xfffffffc, 0xfffffff8, 
  55.                 0xfffffff0, 0xffffffe0, 0xffffffc0, 0xffffff80, 
  56.                 0xffffff00, 0xfffffe00, 0xfffffc00, 0xfffff800, 
  57.                 0xfffff000, 0xffffe000, 0xffffc000, 0xffff8000, 
  58.                 0xffff0000, 0xfffe0000, 0xfffc0000, 0xfff80000, 
  59.                 0xfff00000, 0xffe00000, 0xffc00000, 0xff800000, 
  60.                 0xff000000, 0xfe000000, 0xfc000000, 0xf8000000, 
  61.                 0xf0000000, 0xe0000000, 0xc0000000, 0x80000000};
  62.  
  63. unsigned int bitTest[] = {  0x80000000, 0x40000000, 0x20000000, 0x10000000, 
  64.                 0x08000000, 0x04000000, 0x02000000, 0x01000000,
  65.                 0x00800000, 0x00400000, 0x00200000, 0x00100000,
  66.                 0x00080000, 0x00040000, 0x00020000, 0x00010000,
  67.                 0x00008000, 0x00004000, 0x00002000, 0x00001000,
  68.                 0x00000800, 0x00000400, 0x00000200, 0x00000100,
  69.                 0x00000080, 0x00000040, 0x00000020, 0x00000010,
  70.                 0x00000008, 0x00000004, 0x00000002, 0x00000001};
  71.  
  72.  
  73. /*
  74.  *--------------------------------------------------------------
  75.  *
  76.  * correct_underflow --
  77.  *
  78.  *    Called when buffer does not have sufficient data to 
  79.  *      satisfy request for bits.
  80.  *      Calls get_more_data, an application specific routine
  81.  *      required to fill the buffer with more data.
  82.  *
  83.  * Results:
  84.  *      None really.
  85.  *  
  86.  * Side effects:
  87.  *    buf_length and buffer fields in curVidStream structure
  88.  *      may be changed.
  89.  *
  90.  *--------------------------------------------------------------
  91.  */
  92.  
  93. void 
  94. correct_underflow() {
  95.  
  96.   int status;
  97.  
  98.   status = get_more_data(curVidStream->buf_start,
  99.              curVidStream->max_buf_length,
  100.              &bufLength, &bitBuffer);
  101.  
  102.   if (status  < 0) {
  103.     if (!quietFlag) {
  104.       fprintf (stderr, "\n");
  105.       perror("Unexpected read error.");
  106.     }
  107.     exit(1);
  108.   }
  109.   else if ((status == 0) && (bufLength < 1)) {
  110.     if (!quietFlag) {
  111.       fprintf(stderr, "\nImproper or missing sequence end code.\n");
  112.     }
  113. #ifdef ANALYSIS
  114.     PrintAllStats();
  115. #endif
  116.     if (!quietFlag) {
  117.       PrintTimeInfo();
  118.     }
  119.  
  120.     if (loopFlag) longjmp(env, 1);
  121.     DestroyVidStream(curVidStream);
  122.     exit(0);
  123.   }
  124. #ifdef UTIL2
  125.   curBits = *bitBuffer << bitOffset;
  126. #else
  127.   curBits = *bitBuffer;
  128. #endif
  129.  
  130. }
  131.  
  132.  
  133. /*
  134.  *--------------------------------------------------------------
  135.  *
  136.  * next_bits --
  137.  *
  138.  *    Compares next num bits to low order position in mask.
  139.  *      Buffer pointer is NOT advanced.
  140.  *
  141.  * Results:
  142.  *    TRUE, FALSE, or error code.
  143.  *
  144.  * Side effects:
  145.  *    None.
  146.  *
  147.  *--------------------------------------------------------------
  148.  */
  149.  
  150. int next_bits(num, mask)
  151. int num;
  152. unsigned int mask;
  153. {
  154.   unsigned int stream;
  155.   int ret_value;
  156.  
  157.   /* If no current stream, return error. */
  158.  
  159.   if (curVidStream == NULL)
  160.     return NO_VID_STREAM;
  161.  
  162.   /* Get next num bits, no buffer pointer advance. */
  163.  
  164.   show_bitsn(num, stream);
  165.  
  166.   /* Compare bit stream and mask. Set return value toTRUE if equal, FALSE if
  167.      differs. 
  168.   */
  169.  
  170.   if (mask == stream) {
  171.     ret_value = TRUE;
  172.   } else ret_value = FALSE;
  173.  
  174.   /* Return return value. */
  175.  
  176.   return ret_value;
  177. }
  178.  
  179.  
  180. /*
  181.  *--------------------------------------------------------------
  182.  *
  183.  * get_ext_data --
  184.  *
  185.  *    Assumes that bit stream is at begining of extension
  186.  *      data. Parses off extension data into dynamically 
  187.  *      allocated space until start code is hit. 
  188.  *
  189.  * Results:
  190.  *    Pointer to dynamically allocated memory containing
  191.  *      extension data.
  192.  *
  193.  * Side effects:
  194.  *    Bit stream irreversibly parsed.
  195.  *
  196.  *--------------------------------------------------------------
  197.  */
  198.  
  199. char *get_ext_data ()
  200. {
  201.   int size, marker;
  202.   char *dataPtr;
  203.   unsigned int data;
  204.  
  205.   /* Set initial ext data buffer size. */
  206.  
  207.   size = EXT_BUF_SIZE;
  208.  
  209.   /* Allocate ext data buffer. */
  210.  
  211.   dataPtr = (char *) malloc(size);
  212.  
  213.   /* Initialize marker to keep place in ext data buffer. */
  214.  
  215.   marker = 0;
  216.  
  217.   /* While next data is not start code... */
  218.   while (!next_bits(24, 0x000001)) {
  219.  
  220.     /* Get next byte of ext data. */
  221.  
  222.     get_bits8(data);
  223.  
  224.     /* Put ext data into ext data buffer. Advance marker. */
  225.  
  226.     dataPtr[marker] = (char) data;
  227.     marker++;
  228.  
  229.     /* If end of ext data buffer reached, resize data buffer. */
  230.  
  231.     if (marker == size) {
  232.       size += EXT_BUF_SIZE;
  233.       dataPtr = (char *) realloc(dataPtr, size);
  234.     }
  235.   }
  236.  
  237.   /* Realloc data buffer to free any extra space. */
  238.  
  239.   dataPtr = (char *) realloc(dataPtr, marker);
  240.  
  241.   /* Return pointer to ext data buffer. */
  242.  
  243.   return dataPtr;
  244. }
  245.  
  246.  
  247. /*
  248.  *--------------------------------------------------------------
  249.  *
  250.  * next_start_code --
  251.  *
  252.  *    Parses off bitstream until start code reached. When done
  253.  *      next 4 bytes of bitstream will be start code. Bit offset
  254.  *      reset to 0.
  255.  *
  256.  * Results:
  257.  *    Status code.
  258.  *
  259.  * Side effects:
  260.  *    Bit stream irreversibly parsed.
  261.  *
  262.  *--------------------------------------------------------------
  263.  */
  264.  
  265. int next_start_code()
  266. {
  267.   int state;
  268.   int byteoff;
  269.   unsigned int data;
  270.  
  271.   /* If no current stream, return error. */
  272.  
  273.   if (curVidStream == NULL)
  274.     return NO_VID_STREAM;
  275.  
  276.   /* If insufficient buffer length, correct underflow. */
  277.  
  278.   if (bufLength < 2) {
  279.     correct_underflow();
  280.   }
  281.  
  282.   /* If bit offset not zero, reset and advance buffer pointer. */
  283.  
  284.   byteoff = bitOffset % 8;
  285.  
  286.   if (byteoff != 0) {
  287.     flush_bits((8-byteoff));
  288.   }
  289.  
  290.   /* Set state = 0. */
  291.  
  292.   state = 0;
  293.  
  294.   /* While buffer has data ... */
  295.  
  296.   while(bufLength > 0) {
  297.  
  298.     /* If insufficient data exists, correct underflow. */
  299.  
  300.     if (bufLength < 2) {
  301.       correct_underflow();
  302.     }
  303.  
  304.     /* If next byte is zero... */
  305.  
  306.     get_bits8(data);
  307.  
  308.     if (data == 0) {
  309.  
  310.       /* If state < 2, advance state. */
  311.  
  312.       if (state < 2) state++;
  313.     }
  314.  
  315.     /* If next byte is one... */
  316.  
  317.     else if (data == 1) {
  318.  
  319.       /* If state == 2, advance state (i.e. start code found). */
  320.  
  321.       if (state == 2) state++;
  322.  
  323.       /* Otherwise, reset state to zero. */
  324.  
  325.       else state = 0;
  326.     }
  327.  
  328.     /* Otherwise byte is neither 1 or 0, reset state to 0. */
  329.  
  330.     else {
  331.       state = 0;
  332.     }
  333.  
  334.     /* If state == 3 (i.e. start code found)... */
  335.  
  336.     if (state == 3) {
  337.  
  338.       /* Set buffer pointer back and reset length & bit offsets so
  339.      next bytes will be beginning of start code. 
  340.       */
  341.  
  342.       bitOffset = bitOffset - 24;
  343.  
  344. #ifdef ANALYSIS
  345.       bitCount -= 24;
  346. #endif
  347.  
  348.       if (bitOffset < 0) {
  349.     bitOffset = 32 + bitOffset;
  350.     bufLength++;
  351.     bitBuffer--;
  352. #ifdef UTIL2
  353.     curBits = *bitBuffer << bitOffset;
  354. #else
  355.     curBits = *bitBuffer;
  356. #endif
  357.       }
  358.       else {
  359. #ifdef UTIL2
  360.     curBits = *bitBuffer << bitOffset;
  361. #else
  362.     curBits = *bitBuffer;
  363. #endif
  364.       }
  365.  
  366.       /* Return success. */
  367.  
  368.       return OK;
  369.     }
  370.   }
  371.  
  372.   /* Return underflow error. */
  373.  
  374.   return UNDERFLOW;
  375. }
  376.  
  377.  
  378. /*
  379.  *--------------------------------------------------------------
  380.  *
  381.  * get_extra_bit_info --
  382.  *
  383.  *    Parses off extra bit info stream into dynamically 
  384.  *      allocated memory. Extra bit info is indicated by
  385.  *      a flag bit set to 1, followed by 8 bits of data.
  386.  *      This continues until the flag bit is zero. Assumes
  387.  *      that bit stream set to first flag bit in extra
  388.  *      bit info stream.
  389.  *
  390.  * Results:
  391.  *    Pointer to dynamically allocated memory with extra
  392.  *      bit info in it. Flag bits are NOT included.
  393.  *
  394.  * Side effects:
  395.  *    Bit stream irreversibly parsed.
  396.  *
  397.  *--------------------------------------------------------------
  398.  */
  399.  
  400. char *get_extra_bit_info ()
  401. {
  402.   int size, marker;
  403.   char *dataPtr;
  404.   unsigned int data;
  405.  
  406.   /* Get first flag bit. */
  407.   get_bits1(data);
  408.  
  409.   /* If flag is false, return NULL pointer (i.e. no extra bit info). */
  410.  
  411.   if (!data) return NULL;
  412.  
  413.   /* Initialize size of extra bit info buffer and allocate. */
  414.  
  415.   size = EXT_BUF_SIZE;
  416.   dataPtr = (char *) malloc(size);
  417.  
  418.   /* Reset marker to hold place in buffer. */
  419.  
  420.   marker = 0;
  421.  
  422.   /* While flag bit is true. */
  423.  
  424.   while (data) {
  425.  
  426.     /* Get next 8 bits of data. */
  427.     get_bits8(data);
  428.  
  429.     /* Place in extra bit info buffer. */
  430.  
  431.     dataPtr[marker] = (char) data;
  432.     marker++;
  433.  
  434.     /* If buffer is full, reallocate. */
  435.  
  436.     if (marker == size) {
  437.       size += EXT_BUF_SIZE;
  438.       dataPtr = (char *) realloc(dataPtr, size);
  439.     }
  440.  
  441.     /* Get next flag bit. */
  442.     get_bits1(data);
  443.   }
  444.  
  445.   /* Reallocate buffer to free extra space. */
  446.  
  447.   dataPtr = (char *) realloc(dataPtr, marker);
  448.  
  449.   /* Return pointer to extra bit info buffer. */
  450.  
  451.   return dataPtr;
  452. }
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.