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