home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / mpeg_play-2.1 / parseblock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-09  |  12.4 KB  |  504 lines

  1. /*
  2.  * parseblock.c --
  3.  *
  4.  *      Procedures to read in the values of a block and store them
  5.  *      in a place where the player can use them.
  6.  *
  7.  */
  8.  
  9. /*
  10.  * Copyright (c) 1995 The Regents of the University of California.
  11.  * All rights reserved.
  12.  * 
  13.  * Permission to use, copy, modify, and distribute this software and its
  14.  * documentation for any purpose, without fee, and without written agreement is
  15.  * hereby granted, provided that the above copyright notice and the following
  16.  * two paragraphs appear in all copies of this software.
  17.  * 
  18.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  19.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  20.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  21.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22.  * 
  23.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  24.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  25.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  26.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  27.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  28.  */
  29. #define NO_SANITY_CHECKS
  30. #include <assert.h>
  31. #include "video.h"
  32. #include "proto.h"
  33. #include "decoders.h"
  34.  
  35. /* External declarations. */
  36.  
  37. extern int zigzag_direct[];
  38. #ifdef DCPREC
  39. extern int dcprec;
  40. #endif
  41.  
  42. /* Macro for returning 1 if num is positive, -1 if negative, 0 if 0. */
  43.  
  44. #define Sign(num) ((num > 0) ? 1 : ((num == 0) ? 0 : -1))
  45.  
  46.  
  47. /*
  48.  *--------------------------------------------------------------
  49.  *
  50.  * ParseReconBlock --
  51.  *
  52.  *    Parse values for block structure from bitstream.
  53.  *      n is an indication of the position of the block within
  54.  *      the macroblock (i.e. 0-5) and indicates the type of 
  55.  *      block (i.e. luminance or chrominance). Reconstructs
  56.  *      coefficients from values parsed and puts in 
  57.  *      block.dct_recon array in vid stream structure.
  58.  *      sparseFlag is set when the block contains only one
  59.  *      coeffictient and is used by the IDCT.
  60.  *
  61.  * Results:
  62.  *    
  63.  *
  64.  * Side effects:
  65.  *      Bit stream irreversibly parsed.
  66.  *
  67.  *--------------------------------------------------------------
  68.  */
  69.  
  70. #define DCT_recon blockPtr->dct_recon
  71. #define DCT_dc_y_past blockPtr->dct_dc_y_past
  72. #define DCT_dc_cr_past blockPtr->dct_dc_cr_past
  73. #define DCT_dc_cb_past blockPtr->dct_dc_cb_past
  74.  
  75. #define DECODE_DCT_COEFF_FIRST DecodeDCTCoeffFirst
  76. #define DECODE_DCT_COEFF_NEXT DecodeDCTCoeffNext
  77.  
  78. void
  79. ParseReconBlock(n)
  80.      int n;
  81. {
  82. #ifdef RISC
  83.   unsigned int temp_curBits;
  84.   int temp_bitOffset;
  85.   int temp_bufLength;
  86.   unsigned int *temp_bitBuffer;
  87. #endif
  88.  
  89.   Block *blockPtr = &curVidStream->block;
  90.   int coeffCount=0;
  91.   
  92.   if (bufLength < 100)
  93.     correct_underflow();
  94.  
  95. #ifdef RISC
  96.   temp_curBits = curBits;
  97.   temp_bitOffset = bitOffset;
  98.   temp_bufLength = bufLength;
  99.   temp_bitBuffer = bitBuffer;
  100. #endif
  101.  
  102.   {
  103.     /*
  104.      * Copy the globals curBits, bitOffset, bufLength, and bitBuffer
  105.      * into local variables with the same names, so the macros use the
  106.      * local variables instead.  This allows register allocation and
  107.      * can provide 1-2 fps speedup.  On machines with not so many registers,
  108.      * don't do this.
  109.      */
  110. #ifdef RISC
  111.     register unsigned int curBits = temp_curBits;
  112.     register int bitOffset = temp_bitOffset;
  113.     register int bufLength = temp_bufLength;
  114.     register unsigned int *bitBuffer = temp_bitBuffer;
  115. #endif
  116.  
  117.     int diff;
  118.     int size, level=0, i, run, pos, coeff;
  119.     short int *reconptr;
  120.     unsigned char *iqmatrixptr, *niqmatrixptr;
  121.     int qscale;
  122.  
  123.     reconptr = DCT_recon[0];
  124.  
  125.     /* 
  126.      * Hand coded version of memset that's a little faster...
  127.      * Old call:
  128.      *    memset((char *) DCT_recon, 0, 64*sizeof(short int));
  129.      */
  130.     {
  131.       INT32 *p;
  132.       p = (INT32 *) reconptr;
  133.  
  134.       p[0] = p[1] = p[2] = p[3] = p[4] = p[5] = p[6] = p[7] = p[8] = p[9] = 
  135.       p[10] = p[11] = p[12] = p[13] = p[14] = p[15] = p[16] = p[17] = p[18] =
  136.       p[19] = p[20] = p[21] = p[22] = p[23] = p[24] = p[25] = p[26] = p[27] =
  137.       p[28] = p[29] = p[30] = p[31] = 0;
  138.  
  139.     }
  140.  
  141.     if (curVidStream->mblock.mb_intra) {
  142.  
  143.       if (n < 4) {
  144.  
  145.     /*
  146.      * Get the luminance bits.  This code has been hand optimized to
  147.      * get by the normal bit parsing routines.  We get some speedup
  148.      * by grabbing the next 16 bits and parsing things locally.
  149.      * Thus, calls are translated as:
  150.      *
  151.      *    show_bitsX  <-->   next16bits >> (16-X)
  152.      *    get_bitsX   <-->   val = next16bits >> (16-flushed-X);
  153.      *               flushed += X;
  154.      *               next16bits &= bitMask[flushed];
  155.      *    flush_bitsX <-->   flushed += X;
  156.      *               next16bits &= bitMask[flushed];
  157.      *
  158.      * I've streamlined the code a lot, so that we don't have to mask
  159.      * out the low order bits and a few of the extra adds are removed.
  160.      *    bsmith
  161.      */
  162.     unsigned int next16bits, index, flushed;
  163.  
  164.         show_bits16(next16bits);
  165.         index = next16bits >> (16-5);
  166.         if (index < 31) {
  167.           size = dct_dc_size_luminance[index].value;
  168.           flushed = dct_dc_size_luminance[index].num_bits;
  169.         } else {
  170.           index = next16bits >> (16-9);
  171.           index -= 0x1f0;
  172.           size = dct_dc_size_luminance1[index].value;
  173.           flushed = dct_dc_size_luminance1[index].num_bits;
  174.         }
  175.         next16bits &= bitMask[16+flushed];
  176.  
  177.         if (size != 0) {
  178.           flushed += size;
  179.           diff = next16bits >> (16-flushed);
  180.           if (!(diff & bitTest[32-size])) {
  181.             diff = rBitMask[size] | (diff + 1);
  182.           }
  183.         } else {
  184.           diff = 0;
  185.         }
  186.         flush_bits(flushed);
  187.  
  188.         if (n == 0) {
  189.           coeff = diff << 3;
  190.           if (curVidStream->mblock.mb_address -
  191.               curVidStream->mblock.past_intra_addr > 1) {
  192.             coeff += 1024;
  193.           } else {
  194.             coeff += DCT_dc_y_past;
  195.           }
  196.           DCT_dc_y_past = coeff;
  197.         } else {
  198.           coeff = DCT_dc_y_past + (diff << 3);
  199.           DCT_dc_y_past = coeff;
  200.         }
  201.  
  202.       } else { /* n = 4 or 5 */
  203.     
  204.     /*
  205.      * Get the chrominance bits.  This code has been hand optimized to
  206.      * as described above
  207.      */
  208.  
  209.     unsigned int next16bits, index, flushed;
  210.  
  211.         show_bits16(next16bits);
  212.         index = next16bits >> (16-5);
  213.         if (index < 31) {
  214.           size = dct_dc_size_chrominance[index].value;
  215.           flushed = dct_dc_size_chrominance[index].num_bits;
  216.         } else {
  217.           index = next16bits >> (16-10);
  218.           index -= 0x3e0;
  219.           size = dct_dc_size_chrominance1[index].value;
  220.           flushed = dct_dc_size_chrominance1[index].num_bits;
  221.         }
  222.         next16bits &= bitMask[16+flushed];
  223.     
  224.         if (size != 0) {
  225.           flushed += size;
  226.           diff = next16bits >> (16-flushed);
  227.           if (!(diff & bitTest[32-size])) {
  228.             diff = rBitMask[size] | (diff + 1);
  229.           }
  230.         } else {
  231.           diff = 0;
  232.         }
  233.         flush_bits(flushed);
  234.     
  235.       /* We test 5 first; a result of the mixup of Cr and Cb */
  236.         if (n == 5) {
  237.           coeff = diff << 3;
  238.  
  239.           if (curVidStream->mblock.mb_address -
  240.               curVidStream->mblock.past_intra_addr > 1) {
  241.             coeff += 1024;
  242.           } else {
  243.             coeff += DCT_dc_cr_past;
  244.           }
  245.           DCT_dc_cr_past = coeff;
  246.         } else {
  247.           coeff = diff << 3;
  248.           if (curVidStream->mblock.mb_address -
  249.               curVidStream->mblock.past_intra_addr > 1) {
  250.             coeff += 1024;
  251.           } else {
  252.             coeff += DCT_dc_cb_past;
  253.           }
  254.           DCT_dc_cb_past = coeff;
  255.         }
  256.       }
  257.       
  258.       *reconptr = coeff;
  259.       i = 0; 
  260.       pos = 0;
  261.       coeffCount = (coeff != 0);
  262.     
  263.       if (curVidStream->picture.code_type != 4) {
  264.     
  265.         qscale = curVidStream->slice.quant_scale;
  266.         iqmatrixptr = curVidStream->intra_quant_matrix[0];
  267.     
  268.         while(1) {
  269.       
  270.           DECODE_DCT_COEFF_NEXT(run, level);
  271.  
  272.           if (run >= END_OF_BLOCK) break;
  273.  
  274.           i = i + run + 1;
  275.           pos = zigzag_direct[i];
  276.  
  277.           /* quantizes and oddifies each coefficient */
  278.           if (level < 0) {
  279.             coeff = ((level<<1) * qscale * 
  280.                      ((int) (iqmatrixptr[pos]))) / 16; 
  281.             coeff += (1 - (coeff & 1));
  282.           } else {
  283.             coeff = ((level<<1) * qscale * 
  284.                      ((int) (*(iqmatrixptr+pos)))) >> 4; 
  285.             coeff -= (1 - (coeff & 1));
  286.           }
  287. #ifdef QUANT_CHECK
  288.           printf ("coeff: %d\n", coeff);
  289. #endif
  290.  
  291.           reconptr[pos] = coeff;
  292.           coeffCount++;
  293.  
  294.         }
  295.  
  296. #ifdef QUANT_CHECK
  297.         printf ("\n");
  298. #endif
  299.  
  300. #ifdef ANALYSIS 
  301.         {
  302.           extern unsigned int *mbCoeffPtr;
  303.           mbCoeffPtr[pos]++;
  304.         }
  305. #endif
  306.  
  307.         flush_bits(2);
  308.         goto end;
  309.       }
  310.     } else { /* non-intra-coded macroblock */
  311.       
  312.       niqmatrixptr = curVidStream->non_intra_quant_matrix[0];
  313.       qscale = curVidStream->slice.quant_scale;
  314.       
  315.       DECODE_DCT_COEFF_FIRST(run, level);
  316.       i = run;
  317.  
  318.       pos = zigzag_direct[i];
  319.  
  320.         /* quantizes and oddifies each coefficient */
  321.       if (level < 0) {
  322.         coeff = (((level<<1) - 1) * qscale * 
  323.                  ((int) (niqmatrixptr[pos]))) / 16; 
  324.         coeff += (1 - (coeff & 1));
  325.       } else {
  326.         coeff = (((level<<1) + 1) * qscale * 
  327.                  ((int) (*(niqmatrixptr+pos)))) >> 4; 
  328.         coeff -= (1 - (coeff & 1));
  329.       }
  330.  
  331.       reconptr[pos] = coeff;
  332.       if (coeff) {
  333.           coeffCount = 1;
  334.       }
  335.  
  336.       if (curVidStream->picture.code_type != 4) {
  337.     
  338.         while(1) {
  339.       
  340.           DECODE_DCT_COEFF_NEXT(run, level);
  341.  
  342.           if (run == END_OF_BLOCK) {
  343.                break;
  344.           }
  345.  
  346.           i = i+run+1;
  347.           pos = zigzag_direct[i];
  348.           if (level < 0) {
  349.             coeff = (((level<<1) - 1) * qscale * 
  350.                      ((int) (niqmatrixptr[pos]))) / 16; 
  351.             coeff += (coeff & 1);
  352.           } else {
  353.             coeff = (((level<<1) + 1) * qscale * 
  354.                      ((int) (*(niqmatrixptr+pos)))) >> 4; 
  355.             coeff -= (coeff & 1);
  356.           }
  357.           reconptr[pos] = coeff;
  358.           coeffCount++;
  359.         } /* end while */
  360.  
  361. #ifdef ANALYSIS
  362.         {
  363.           extern unsigned int *mbCoeffPtr;
  364.           mbCoeffPtr[pos]++;
  365.         }
  366. #endif
  367.  
  368.         flush_bits(2);
  369.  
  370.         goto end;
  371.       } /* end if (curVidStream->picture.code_type != 4) */
  372.     }
  373.     
  374.   end:
  375.  
  376.     if (coeffCount == 1) {
  377.       j_rev_dct_sparse (reconptr, pos);
  378.     }
  379.     else {
  380. #ifdef FLOATDCT
  381.       if (qualityFlag) {
  382.          float_idct(reconptr);
  383.       } else {
  384. #endif
  385.         j_rev_dct(reconptr);
  386. #ifdef FLOATDCT
  387.       }
  388. #endif
  389.     }
  390.  
  391. #ifdef RISC
  392.     temp_curBits = curBits;
  393.     temp_bitOffset = bitOffset;
  394.     temp_bufLength = bufLength;
  395.     temp_bitBuffer = bitBuffer;
  396. #endif
  397.  
  398.   }
  399.  
  400. #ifdef RISC
  401.   curBits = temp_curBits;
  402.   bitOffset = temp_bitOffset;
  403.   bufLength = temp_bufLength;
  404.   bitBuffer = temp_bitBuffer;
  405. #endif
  406. }
  407.     
  408. #undef DCT_recon 
  409. #undef DCT_dc_y_past 
  410. #undef DCT_dc_cr_past 
  411. #undef DCT_dc_cb_past 
  412.  
  413.  
  414. /*
  415.  *--------------------------------------------------------------
  416.  *
  417.  * ParseAwayBlock --
  418.  *
  419.  *    Parses off block values, throwing them away.
  420.  *      Used with grayscale dithering.
  421.  *
  422.  * Results:
  423.  *    None.
  424.  *
  425.  * Side effects:
  426.  *      None.
  427.  *
  428.  *--------------------------------------------------------------
  429.  */
  430.  
  431. void
  432. ParseAwayBlock(n)
  433.      int n;
  434. {
  435.   unsigned int diff;
  436.   unsigned int size, run;
  437.   int level;
  438.  
  439.   if (bufLength < 100)
  440.     correct_underflow();
  441.  
  442.   if (curVidStream->mblock.mb_intra) {
  443.  
  444.     /* If the block is a luminance block... */
  445.  
  446.     if (n < 4) {
  447.  
  448.       /* Parse and decode size of first coefficient. */
  449.  
  450.       DecodeDCTDCSizeLum(size);
  451.  
  452.       /* Parse first coefficient. */
  453.  
  454.       if (size != 0) {
  455.         get_bitsn(size, diff);
  456.       }
  457.     }
  458.  
  459.     /* Otherwise, block is chrominance block... */
  460.  
  461.     else {
  462.  
  463.       /* Parse and decode size of first coefficient. */
  464.  
  465.       DecodeDCTDCSizeChrom(size);
  466.  
  467.       /* Parse first coefficient. */
  468.  
  469.       if (size != 0) {
  470.         get_bitsn(size, diff);
  471.       }
  472.     }
  473.   }
  474.  
  475.   /* Otherwise, block is not intracoded... */
  476.  
  477.   else {
  478.  
  479.     /* Decode and set first coefficient. */
  480.  
  481.     DECODE_DCT_COEFF_FIRST(run, level);
  482.   }
  483.  
  484.   /* If picture is not D type (i.e. I, P, or B)... */
  485.  
  486.   if (curVidStream->picture.code_type != 4) {
  487.  
  488.     /* While end of macroblock has not been reached... */
  489.  
  490.     while (1) {
  491.  
  492.       /* Get the dct_coeff_next */
  493.  
  494.       DECODE_DCT_COEFF_NEXT(run, level);
  495.  
  496.       if (run == END_OF_BLOCK) break;
  497.     }
  498.  
  499.     /* End_of_block */
  500.  
  501.     flush_bits(2);
  502.   }
  503. }
  504.