/********************************************************************1* *2* THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE. *3* USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *4* GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *5* IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *6* *7* THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009 *8* by the Xiph.Org Foundation and contributors *9* https://www.xiph.org/ *10* *11********************************************************************1213function:1415********************************************************************/1617#include <stdlib.h>18#include <string.h>19#include <ogg/ogg.h>20#include "quant.h"21#include "decint.h"2223/*The maximum output of the DCT with +/- 255 inputs is +/- 8157.24These minimum quantizers ensure the result after quantization (and after25prediction for DC) will be no more than +/- 510.26The tokenization system can handle values up to +/- 580, so there is no need27to do any coefficient clamping.28I would rather have allowed smaller quantizers and had to clamp, but these29minimums were required when constructing the original VP3 matrices and have30been formalized in the spec.*/31static const unsigned OC_DC_QUANT_MIN[2]={4<<2,8<<2};32static const unsigned OC_AC_QUANT_MIN[2]={2<<2,4<<2};3334/*Initializes the dequantization tables from a set of quantizer info.35Currently the dequantizer (and elsewhere enquantizer) tables are expected to36be initialized as pointing to the storage reserved for them in the37oc_theora_state (resp. oc_enc_ctx) structure.38If some tables are duplicates of others, the pointers will be adjusted to39point to a single copy of the tables, but the storage for them will not be40freed.41If you're concerned about the memory footprint, the obvious thing to do is42to move the storage out of its fixed place in the structures and allocate43it on demand.44However, a much, much better option is to only store the quantization45matrices being used for the current frame, and to recalculate these as the46qi values change between frames (this is what VP3 did).*/47void oc_dequant_tables_init(ogg_uint16_t *_dequant[64][3][2],48int _pp_dc_scale[64],const th_quant_info *_qinfo){49/*Coding mode: intra or inter.*/50int qti;51/*Y', C_b, C_r*/52int pli;53for(qti=0;qti<2;qti++)for(pli=0;pli<3;pli++){54/*Quality index.*/55int qi;56/*Range iterator.*/57int qri;58for(qi=0,qri=0;qri<=_qinfo->qi_ranges[qti][pli].nranges;qri++){59th_quant_base base;60ogg_uint32_t q;61int qi_start;62int qi_end;63memcpy(base,_qinfo->qi_ranges[qti][pli].base_matrices[qri],64sizeof(base));65qi_start=qi;66if(qri==_qinfo->qi_ranges[qti][pli].nranges)qi_end=qi+1;67else qi_end=qi+_qinfo->qi_ranges[qti][pli].sizes[qri];68/*Iterate over quality indices in this range.*/69for(;;){70ogg_uint32_t qfac;71int zzi;72int ci;73/*In the original VP3.2 code, the rounding offset and the size of the74dead zone around 0 were controlled by a "sharpness" parameter.75The size of our dead zone is now controlled by the per-coefficient76quality thresholds returned by our HVS module.77We round down from a more accurate value when the quality of the78reconstruction does not fall below our threshold and it saves bits.79Hence, all of that VP3.2 code is gone from here, and the remaining80floating point code has been implemented as equivalent integer code81with exact precision.*/82qfac=(ogg_uint32_t)_qinfo->dc_scale[qi]*base[0];83/*For postprocessing, not dequantization.*/84if(_pp_dc_scale!=NULL)_pp_dc_scale[qi]=(int)(qfac/160);85/*Scale DC the coefficient from the proper table.*/86q=(qfac/100)<<2;87q=OC_CLAMPI(OC_DC_QUANT_MIN[qti],q,OC_QUANT_MAX);88_dequant[qi][pli][qti][0]=(ogg_uint16_t)q;89/*Now scale AC coefficients from the proper table.*/90for(zzi=1;zzi<64;zzi++){91q=((ogg_uint32_t)_qinfo->ac_scale[qi]*base[OC_FZIG_ZAG[zzi]]/100)<<2;92q=OC_CLAMPI(OC_AC_QUANT_MIN[qti],q,OC_QUANT_MAX);93_dequant[qi][pli][qti][zzi]=(ogg_uint16_t)q;94}95/*If this is a duplicate of a previous matrix, use that instead.96This simple check helps us improve cache coherency later.*/97{98int dupe;99int qtj;100int plj;101dupe=0;102for(qtj=0;qtj<=qti;qtj++){103for(plj=0;plj<(qtj<qti?3:pli);plj++){104if(!memcmp(_dequant[qi][pli][qti],_dequant[qi][plj][qtj],105sizeof(oc_quant_table))){106dupe=1;107break;108}109}110if(dupe)break;111}112if(dupe)_dequant[qi][pli][qti]=_dequant[qi][plj][qtj];113}114if(++qi>=qi_end)break;115/*Interpolate the next base matrix.*/116for(ci=0;ci<64;ci++){117base[ci]=(unsigned char)(118(2*((qi_end-qi)*_qinfo->qi_ranges[qti][pli].base_matrices[qri][ci]+119(qi-qi_start)*_qinfo->qi_ranges[qti][pli].base_matrices[qri+1][ci])120+_qinfo->qi_ranges[qti][pli].sizes[qri])/121(2*_qinfo->qi_ranges[qti][pli].sizes[qri]));122}123}124}125}126}127128129