home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource1 / mpegplay / decoders.h < prev    next >
C/C++ Source or Header  |  1993-02-02  |  16KB  |  475 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.  * decoders.h
  23.  *
  24.  * This file contains the declarations of structures required for Huffman
  25.  * decoding
  26.  *
  27.  */
  28.  
  29. /* Include util.h for bit i/o parsing macros. */
  30.  
  31. #include "util.h"
  32.  
  33. /* Code for unbound values in decoding tables */
  34. #define ERROR -1
  35. #define DCT_ERROR 63
  36.  
  37. #define MACRO_BLOCK_STUFFING 34
  38. #define MACRO_BLOCK_ESCAPE 35
  39.  
  40. /* Two types of DCT Coefficients */
  41. #define DCT_COEFF_FIRST 0
  42. #define DCT_COEFF_NEXT 1
  43.  
  44. /* Special values for DCT Coefficients */
  45. #define END_OF_BLOCK 62
  46. #define ESCAPE 61
  47.  
  48. /* Structure for an entry in the decoding table of 
  49.  * macroblock_address_increment */
  50. typedef struct {
  51.   unsigned int value;       /* value for macroblock_address_increment */
  52.   int num_bits;             /* length of the Huffman code */
  53. } mb_addr_inc_entry;
  54.  
  55. /* Decoding table for macroblock_address_increment */
  56. extern mb_addr_inc_entry mb_addr_inc[2048];
  57.  
  58.  
  59. /* Structure for an entry in the decoding table of macroblock_type */
  60. typedef struct {
  61.   unsigned int mb_quant;              /* macroblock_quant */
  62.   unsigned int mb_motion_forward;     /* macroblock_motion_forward */
  63.   unsigned int mb_motion_backward;    /* macroblock_motion_backward */
  64.   unsigned int mb_pattern;            /* macroblock_pattern */
  65.   unsigned int mb_intra;              /* macroblock_intra */
  66.   int num_bits;                       /* length of the Huffman code */
  67. } mb_type_entry;
  68.  
  69. /* Decoding table for macroblock_type in predictive-coded pictures */
  70. extern mb_type_entry mb_type_P[64];
  71.  
  72. /* Decoding table for macroblock_type in bidirectionally-coded pictures */
  73. extern mb_type_entry mb_type_B[64];
  74.  
  75.  
  76. /* Structures for an entry in the decoding table of coded_block_pattern */
  77. typedef struct {
  78.   unsigned int cbp;            /* coded_block_pattern */
  79.   int num_bits;                /* length of the Huffman code */
  80. } coded_block_pattern_entry;
  81.  
  82. /* External declaration of coded block pattern table. */
  83.  
  84. extern coded_block_pattern_entry coded_block_pattern[512];
  85.  
  86.  
  87.  
  88. /* Structure for an entry in the decoding table of motion vectors */
  89. typedef struct {
  90.   int code;              /* value for motion_horizontal_forward_code,
  91.               * motion_vertical_forward_code, 
  92.               * motion_horizontal_backward_code, or
  93.               * motion_vertical_backward_code.
  94.               */
  95.   int num_bits;          /* length of the Huffman code */
  96. } motion_vectors_entry;
  97.  
  98.  
  99. /* Decoding table for motion vectors */
  100. extern motion_vectors_entry motion_vectors[2048];
  101.  
  102.  
  103. /* Structure for an entry in the decoding table of dct_dc_size */
  104. typedef struct {
  105.   unsigned int value;    /* value of dct_dc_size (luminance or chrominance) */
  106.   int num_bits;          /* length of the Huffman code */
  107. } dct_dc_size_entry;
  108.  
  109. /* External declaration of dct dc size lumiance table. */
  110.  
  111. extern dct_dc_size_entry dct_dc_size_luminance[128];
  112.  
  113. /* External declaration of dct dc size chrom table. */
  114.  
  115. extern dct_dc_size_entry dct_dc_size_chrominance[256];
  116.  
  117.  
  118. /* DCT coeff tables. */
  119.  
  120. #define RUN_MASK 0xfc00
  121. #define LEVEL_MASK 0x03f0
  122. #define NUM_MASK 0x000f
  123. #define RUN_SHIFT 10
  124. #define LEVEL_SHIFT 4
  125.  
  126. /* External declaration of dct coeff tables. */
  127.  
  128. extern unsigned short int dct_coeff_tbl_0[256];
  129. extern unsigned short int dct_coeff_tbl_1[16];
  130. extern unsigned short int dct_coeff_tbl_2[4];
  131. extern unsigned short int dct_coeff_tbl_3[4];
  132. extern unsigned short int dct_coeff_next[256];
  133. extern unsigned short int dct_coeff_first[256];
  134.  
  135. #define DecodeDCTDCSizeLum(macro_val)                    \
  136. {                                                    \
  137.   unsigned int index;                                \
  138.                                                      \
  139.   show_bits7(index);                              \
  140.                                                      \
  141.   macro_val = dct_dc_size_luminance[index].value;       \
  142.                                                      \
  143.   flush_bits(dct_dc_size_luminance[index].num_bits); \
  144. }
  145.  
  146. #define DecodeDCTDCSizeChrom(macro_val)                      \
  147. {                                                        \
  148.   unsigned int index;                                    \
  149.                                                          \
  150.   show_bits8(index);                                  \
  151.                                                          \
  152.   macro_val = dct_dc_size_chrominance[index].value;         \
  153.                                                          \
  154.   flush_bits(dct_dc_size_chrominance[index].num_bits);   \
  155. }
  156.  
  157. #define DecodeDCTCoeff(dct_coeff_tbl, run, level)            \
  158. {                                    \
  159.   unsigned int temp, index;                        \
  160.   unsigned int value, next32bits, flushed;                \
  161.                                     \
  162.   /*                                    \
  163.    * Grab the next 32 bits and use it to improve performance of        \
  164.    * getting the bits to parse. Thus, calls are translated as:        \
  165.    *                                    \
  166.    *    show_bitsX  <-->   next32bits >> (32-X)                \
  167.    *    get_bitsX   <-->   val = next32bits >> (32-flushed-X);        \
  168.    *               flushed += X;                \
  169.    *               next32bits &= bitMask[flushed];        \
  170.    *    flush_bitsX <-->   flushed += X;                \
  171.    *               next32bits &= bitMask[flushed];        \
  172.    *                                    \
  173.    * I've streamlined the code a lot, so that we don't have to mask    \
  174.    * out the low order bits and a few of the extra adds are removed.    \
  175.    */                                    \
  176.   show_bits32(next32bits);                        \
  177.                                     \
  178.   /* show_bits8(index); */                        \
  179.   index = next32bits >> 24;                        \
  180.                                     \
  181.   if (index > 3) {                            \
  182.     value = dct_coeff_tbl[index];                    \
  183.     run = value >> RUN_SHIFT;                        \
  184.     if (run != END_OF_BLOCK) {                        \
  185.       /* num_bits = (value & NUM_MASK) + 1; */                \
  186.       /* flush_bits(num_bits); */                    \
  187.       if (run != ESCAPE) {                        \
  188.      /* get_bits1(value); */                    \
  189.      /* if (value) level = -level; */                \
  190.      flushed = (value & NUM_MASK) + 2;                \
  191.          level = (value & LEVEL_MASK) >> LEVEL_SHIFT;            \
  192.      value = next32bits >> (32-flushed);                \
  193.      value &= 0x1;                            \
  194.      if (value) level = -level;                    \
  195.      /* next32bits &= ((~0) >> flushed);  last op before update */    \
  196.        }                                \
  197.        else {    /* run == ESCAPE */                    \
  198.      /* Get the next six into run, and next 8 into temp */        \
  199.          /* get_bits14(temp); */                    \
  200.      flushed = (value & NUM_MASK) + 1;                \
  201.      temp = next32bits >> (18-flushed);                \
  202.      /* Normally, we'd ad 14 to flushed, but I've saved a few    \
  203.       * instr by moving the add below */                \
  204.      temp &= 0x3fff;                        \
  205.      run = temp >> 8;                        \
  206.      temp &= 0xff;                            \
  207.      if (temp == 0) {                        \
  208.             /* get_bits8(level); */                    \
  209.         level = next32bits >> (10-flushed);                \
  210.         level &= 0xff;                        \
  211.         flushed += 22;                        \
  212.          assert(level >= 128);                    \
  213.      } else if (temp != 128) {                    \
  214.         /* Grab sign bit */                        \
  215.         flushed += 14;                        \
  216.         level = ((int) (temp << 24)) >> 24;                \
  217.      } else {                            \
  218.             /* get_bits8(level); */                    \
  219.         level = next32bits >> (10-flushed);                \
  220.         level &= 0xff;                        \
  221.         flushed += 22;                        \
  222.         level = level - 256;                    \
  223.         assert(level <= -128 && level >= -255);            \
  224.      }                                \
  225.        }                                \
  226.        /* Update bitstream... */                    \
  227.        flush_bits(flushed);                        \
  228.        assert (flushed <= 32);                        \
  229.     }                                    \
  230.   }                                    \
  231.   else {                                \
  232.     if (index == 2) {