michael@0: /******************************************************************** michael@0: * * michael@0: * THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE. * michael@0: * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * michael@0: * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * michael@0: * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * michael@0: * * michael@0: * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009 * michael@0: * by the Xiph.Org Foundation and contributors http://www.xiph.org/ * michael@0: * * michael@0: ******************************************************************** michael@0: michael@0: function: michael@0: last mod: $Id: quant.c 17307 2010-06-27 06:02:15Z tterribe $ michael@0: michael@0: ********************************************************************/ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include "quant.h" michael@0: #include "decint.h" michael@0: michael@0: /*The maximum output of the DCT with +/- 255 inputs is +/- 8157. michael@0: These minimum quantizers ensure the result after quantization (and after michael@0: prediction for DC) will be no more than +/- 510. michael@0: The tokenization system can handle values up to +/- 580, so there is no need michael@0: to do any coefficient clamping. michael@0: I would rather have allowed smaller quantizers and had to clamp, but these michael@0: minimums were required when constructing the original VP3 matrices and have michael@0: been formalized in the spec.*/ michael@0: static const unsigned OC_DC_QUANT_MIN[2]={4<<2,8<<2}; michael@0: static const unsigned OC_AC_QUANT_MIN[2]={2<<2,4<<2}; michael@0: michael@0: /*Initializes the dequantization tables from a set of quantizer info. michael@0: Currently the dequantizer (and elsewhere enquantizer) tables are expected to michael@0: be initialized as pointing to the storage reserved for them in the michael@0: oc_theora_state (resp. oc_enc_ctx) structure. michael@0: If some tables are duplicates of others, the pointers will be adjusted to michael@0: point to a single copy of the tables, but the storage for them will not be michael@0: freed. michael@0: If you're concerned about the memory footprint, the obvious thing to do is michael@0: to move the storage out of its fixed place in the structures and allocate michael@0: it on demand. michael@0: However, a much, much better option is to only store the quantization michael@0: matrices being used for the current frame, and to recalculate these as the michael@0: qi values change between frames (this is what VP3 did).*/ michael@0: void oc_dequant_tables_init(ogg_uint16_t *_dequant[64][3][2], michael@0: int _pp_dc_scale[64],const th_quant_info *_qinfo){ michael@0: /*Coding mode: intra or inter.*/ michael@0: int qti; michael@0: /*Y', C_b, C_r*/ michael@0: int pli; michael@0: for(qti=0;qti<2;qti++)for(pli=0;pli<3;pli++){ michael@0: /*Quality index.*/ michael@0: int qi; michael@0: /*Range iterator.*/ michael@0: int qri; michael@0: for(qi=0,qri=0;qri<=_qinfo->qi_ranges[qti][pli].nranges;qri++){ michael@0: th_quant_base base; michael@0: ogg_uint32_t q; michael@0: int qi_start; michael@0: int qi_end; michael@0: memcpy(base,_qinfo->qi_ranges[qti][pli].base_matrices[qri], michael@0: sizeof(base)); michael@0: qi_start=qi; michael@0: if(qri==_qinfo->qi_ranges[qti][pli].nranges)qi_end=qi+1; michael@0: else qi_end=qi+_qinfo->qi_ranges[qti][pli].sizes[qri]; michael@0: /*Iterate over quality indicies in this range.*/ michael@0: for(;;){ michael@0: ogg_uint32_t qfac; michael@0: int zzi; michael@0: int ci; michael@0: /*In the original VP3.2 code, the rounding offset and the size of the michael@0: dead zone around 0 were controlled by a "sharpness" parameter. michael@0: The size of our dead zone is now controlled by the per-coefficient michael@0: quality thresholds returned by our HVS module. michael@0: We round down from a more accurate value when the quality of the michael@0: reconstruction does not fall below our threshold and it saves bits. michael@0: Hence, all of that VP3.2 code is gone from here, and the remaining michael@0: floating point code has been implemented as equivalent integer code michael@0: with exact precision.*/ michael@0: qfac=(ogg_uint32_t)_qinfo->dc_scale[qi]*base[0]; michael@0: /*For postprocessing, not dequantization.*/ michael@0: if(_pp_dc_scale!=NULL)_pp_dc_scale[qi]=(int)(qfac/160); michael@0: /*Scale DC the coefficient from the proper table.*/ michael@0: q=(qfac/100)<<2; michael@0: q=OC_CLAMPI(OC_DC_QUANT_MIN[qti],q,OC_QUANT_MAX); michael@0: _dequant[qi][pli][qti][0]=(ogg_uint16_t)q; michael@0: /*Now scale AC coefficients from the proper table.*/ michael@0: for(zzi=1;zzi<64;zzi++){ michael@0: q=((ogg_uint32_t)_qinfo->ac_scale[qi]*base[OC_FZIG_ZAG[zzi]]/100)<<2; michael@0: q=OC_CLAMPI(OC_AC_QUANT_MIN[qti],q,OC_QUANT_MAX); michael@0: _dequant[qi][pli][qti][zzi]=(ogg_uint16_t)q; michael@0: } michael@0: /*If this is a duplicate of a previous matrix, use that instead. michael@0: This simple check helps us improve cache coherency later.*/ michael@0: { michael@0: int dupe; michael@0: int qtj; michael@0: int plj; michael@0: dupe=0; michael@0: for(qtj=0;qtj<=qti;qtj++){ michael@0: for(plj=0;plj<(qtj=qi_end)break; michael@0: /*Interpolate the next base matrix.*/ michael@0: for(ci=0;ci<64;ci++){ michael@0: base[ci]=(unsigned char)( michael@0: (2*((qi_end-qi)*_qinfo->qi_ranges[qti][pli].base_matrices[qri][ci]+ michael@0: (qi-qi_start)*_qinfo->qi_ranges[qti][pli].base_matrices[qri+1][ci]) michael@0: +_qinfo->qi_ranges[qti][pli].sizes[qri])/ michael@0: (2*_qinfo->qi_ranges[qti][pli].sizes[qri])); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: }