media/libtheora/lib/quant.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libtheora/lib/quant.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,127 @@
     1.4 +/********************************************************************
     1.5 + *                                                                  *
     1.6 + * THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE.   *
     1.7 + * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
     1.8 + * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
     1.9 + * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
    1.10 + *                                                                  *
    1.11 + * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009                *
    1.12 + * by the Xiph.Org Foundation and contributors http://www.xiph.org/ *
    1.13 + *                                                                  *
    1.14 + ********************************************************************
    1.15 +
    1.16 +  function:
    1.17 +    last mod: $Id: quant.c 17307 2010-06-27 06:02:15Z tterribe $
    1.18 +
    1.19 + ********************************************************************/
    1.20 +
    1.21 +#include <stdlib.h>
    1.22 +#include <string.h>
    1.23 +#include <ogg/ogg.h>
    1.24 +#include "quant.h"
    1.25 +#include "decint.h"
    1.26 +
    1.27 +/*The maximum output of the DCT with +/- 255 inputs is +/- 8157.
    1.28 +  These minimum quantizers ensure the result after quantization (and after
    1.29 +   prediction for DC) will be no more than +/- 510.
    1.30 +  The tokenization system can handle values up to +/- 580, so there is no need
    1.31 +   to do any coefficient clamping.
    1.32 +  I would rather have allowed smaller quantizers and had to clamp, but these
    1.33 +   minimums were required when constructing the original VP3 matrices and have
    1.34 +   been formalized in the spec.*/
    1.35 +static const unsigned OC_DC_QUANT_MIN[2]={4<<2,8<<2};
    1.36 +static const unsigned OC_AC_QUANT_MIN[2]={2<<2,4<<2};
    1.37 +
    1.38 +/*Initializes the dequantization tables from a set of quantizer info.
    1.39 +  Currently the dequantizer (and elsewhere enquantizer) tables are expected to
    1.40 +   be initialized as pointing to the storage reserved for them in the
    1.41 +   oc_theora_state (resp. oc_enc_ctx) structure.
    1.42 +  If some tables are duplicates of others, the pointers will be adjusted to
    1.43 +   point to a single copy of the tables, but the storage for them will not be
    1.44 +   freed.
    1.45 +  If you're concerned about the memory footprint, the obvious thing to do is
    1.46 +   to move the storage out of its fixed place in the structures and allocate
    1.47 +   it on demand.
    1.48 +  However, a much, much better option is to only store the quantization
    1.49 +   matrices being used for the current frame, and to recalculate these as the
    1.50 +   qi values change between frames (this is what VP3 did).*/
    1.51 +void oc_dequant_tables_init(ogg_uint16_t *_dequant[64][3][2],
    1.52 + int _pp_dc_scale[64],const th_quant_info *_qinfo){
    1.53 +  /*Coding mode: intra or inter.*/
    1.54 +  int          qti;
    1.55 +  /*Y', C_b, C_r*/
    1.56 +  int          pli;
    1.57 +  for(qti=0;qti<2;qti++)for(pli=0;pli<3;pli++){
    1.58 +    /*Quality index.*/
    1.59 +    int qi;
    1.60 +    /*Range iterator.*/
    1.61 +    int qri;
    1.62 +    for(qi=0,qri=0;qri<=_qinfo->qi_ranges[qti][pli].nranges;qri++){
    1.63 +      th_quant_base base;
    1.64 +      ogg_uint32_t  q;
    1.65 +      int           qi_start;
    1.66 +      int           qi_end;
    1.67 +      memcpy(base,_qinfo->qi_ranges[qti][pli].base_matrices[qri],
    1.68 +       sizeof(base));
    1.69 +      qi_start=qi;
    1.70 +      if(qri==_qinfo->qi_ranges[qti][pli].nranges)qi_end=qi+1;
    1.71 +      else qi_end=qi+_qinfo->qi_ranges[qti][pli].sizes[qri];
    1.72 +      /*Iterate over quality indicies in this range.*/
    1.73 +      for(;;){
    1.74 +        ogg_uint32_t qfac;
    1.75 +        int          zzi;
    1.76 +        int          ci;
    1.77 +        /*In the original VP3.2 code, the rounding offset and the size of the
    1.78 +           dead zone around 0 were controlled by a "sharpness" parameter.
    1.79 +          The size of our dead zone is now controlled by the per-coefficient
    1.80 +           quality thresholds returned by our HVS module.
    1.81 +          We round down from a more accurate value when the quality of the
    1.82 +           reconstruction does not fall below our threshold and it saves bits.
    1.83 +          Hence, all of that VP3.2 code is gone from here, and the remaining
    1.84 +           floating point code has been implemented as equivalent integer code
    1.85 +           with exact precision.*/
    1.86 +        qfac=(ogg_uint32_t)_qinfo->dc_scale[qi]*base[0];
    1.87 +        /*For postprocessing, not dequantization.*/
    1.88 +        if(_pp_dc_scale!=NULL)_pp_dc_scale[qi]=(int)(qfac/160);
    1.89 +        /*Scale DC the coefficient from the proper table.*/
    1.90 +        q=(qfac/100)<<2;
    1.91 +        q=OC_CLAMPI(OC_DC_QUANT_MIN[qti],q,OC_QUANT_MAX);
    1.92 +        _dequant[qi][pli][qti][0]=(ogg_uint16_t)q;
    1.93 +        /*Now scale AC coefficients from the proper table.*/
    1.94 +        for(zzi=1;zzi<64;zzi++){
    1.95 +          q=((ogg_uint32_t)_qinfo->ac_scale[qi]*base[OC_FZIG_ZAG[zzi]]/100)<<2;
    1.96 +          q=OC_CLAMPI(OC_AC_QUANT_MIN[qti],q,OC_QUANT_MAX);
    1.97 +          _dequant[qi][pli][qti][zzi]=(ogg_uint16_t)q;
    1.98 +        }
    1.99 +        /*If this is a duplicate of a previous matrix, use that instead.
   1.100 +          This simple check helps us improve cache coherency later.*/
   1.101 +        {
   1.102 +          int dupe;
   1.103 +          int qtj;
   1.104 +          int plj;
   1.105 +          dupe=0;
   1.106 +          for(qtj=0;qtj<=qti;qtj++){
   1.107 +            for(plj=0;plj<(qtj<qti?3:pli);plj++){
   1.108 +              if(!memcmp(_dequant[qi][pli][qti],_dequant[qi][plj][qtj],
   1.109 +               sizeof(oc_quant_table))){
   1.110 +                dupe=1;
   1.111 +                break;
   1.112 +              }
   1.113 +            }
   1.114 +            if(dupe)break;
   1.115 +          }
   1.116 +          if(dupe)_dequant[qi][pli][qti]=_dequant[qi][plj][qtj];
   1.117 +        }
   1.118 +        if(++qi>=qi_end)break;
   1.119 +        /*Interpolate the next base matrix.*/
   1.120 +        for(ci=0;ci<64;ci++){
   1.121 +          base[ci]=(unsigned char)(
   1.122 +           (2*((qi_end-qi)*_qinfo->qi_ranges[qti][pli].base_matrices[qri][ci]+
   1.123 +           (qi-qi_start)*_qinfo->qi_ranges[qti][pli].base_matrices[qri+1][ci])
   1.124 +           +_qinfo->qi_ranges[qti][pli].sizes[qri])/
   1.125 +           (2*_qinfo->qi_ranges[qti][pli].sizes[qri]));
   1.126 +        }
   1.127 +      }
   1.128 +    }
   1.129 +  }
   1.130 +}

mercurial