1 /********************************************************************
2 * *
3 * THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
7 * *
8 * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009 *
9 * by the Xiph.Org Foundation and contributors http://www.xiph.org/ *
10 * *
11 ********************************************************************
12
13 function:
14 last mod: $Id$
15
16 ********************************************************************/
17
18 #include <stdlib.h>
19 #include <string.h>
20 #include <ogg/ogg.h>
21 #include "quant.h"
22 #include "decint.h"
23
24 static const unsigned OC_DC_QUANT_MIN[2]={4<<2,8<<2};
25 static const unsigned OC_AC_QUANT_MIN[2]={2<<2,4<<2};
26
27 /*Initializes the dequantization tables from a set of quantizer info.
28 Currently the dequantizer (and elsewhere enquantizer) tables are expected to
29 be initialized as pointing to the storage reserved for them in the
30 oc_theora_state (resp. oc_enc_ctx) structure.
31 If some tables are duplicates of others, the pointers will be adjusted to
32 point to a single copy of the tables, but the storage for them will not be
33 freed.
34 If you're concerned about the memory footprint, the obvious thing to do is
35 to move the storage out of its fixed place in the structures and allocate
36 it on demand.
37 However, a much, much better option is to only store the quantization
38 matrices being used for the current frame, and to recalculate these as the
39 qi values change between frames (this is what VP3 did).*/
oc_dequant_tables_init(ogg_uint16_t * _dequant[64][3][2],int _pp_dc_scale[64],const th_quant_info * _qinfo)40 void oc_dequant_tables_init(ogg_uint16_t *_dequant[64][3][2],
41 int _pp_dc_scale[64],const th_quant_info *_qinfo){
42 /*Coding mode: intra or inter.*/
43 int qti;
44 /*Y', C_b, C_r*/
45 int pli;
46 for(qti=0;qti<2;qti++)for(pli=0;pli<3;pli++){
47 /*Quality index.*/
48 int qi;
49 /*Range iterator.*/
50 int qri;
51 for(qi=0,qri=0;qri<=_qinfo->qi_ranges[qti][pli].nranges;qri++){
52 th_quant_base base;
53 ogg_uint32_t q;
54 int qi_start;
55 int qi_end;
56 memcpy(base,_qinfo->qi_ranges[qti][pli].base_matrices[qri],
57 sizeof(base));
58 qi_start=qi;
59 if(qri==_qinfo->qi_ranges[qti][pli].nranges)qi_end=qi+1;
60 else qi_end=qi+_qinfo->qi_ranges[qti][pli].sizes[qri];
61 /*Iterate over quality indicies in this range.*/
62 for(;;){
63 ogg_uint32_t qfac;
64 int zzi;
65 int ci;
66 /*In the original VP3.2 code, the rounding offset and the size of the
67 dead zone around 0 were controlled by a "sharpness" parameter.
68 The size of our dead zone is now controlled by the per-coefficient
69 quality thresholds returned by our HVS module.
70 We round down from a more accurate value when the quality of the
71 reconstruction does not fall below our threshold and it saves bits.
72 Hence, all of that VP3.2 code is gone from here, and the remaining
73 floating point code has been implemented as equivalent integer code
74 with exact precision.*/
75 qfac=(ogg_uint32_t)_qinfo->dc_scale[qi]*base[0];
76 /*For postprocessing, not dequantization.*/
77 if(_pp_dc_scale!=NULL)_pp_dc_scale[qi]=(int)(qfac/160);
78 /*Scale DC the coefficient from the proper table.*/
79 q=(qfac/100)<<2;
80 q=OC_CLAMPI(OC_DC_QUANT_MIN[qti],q,OC_QUANT_MAX);
81 _dequant[qi][pli][qti][0]=(ogg_uint16_t)q;
82 /*Now scale AC coefficients from the proper table.*/
83 for(zzi=1;zzi<64;zzi++){
84 q=((ogg_uint32_t)_qinfo->ac_scale[qi]*base[OC_FZIG_ZAG[zzi]]/100)<<2;
85 q=OC_CLAMPI(OC_AC_QUANT_MIN[qti],q,OC_QUANT_MAX);
86 _dequant[qi][pli][qti][zzi]=(ogg_uint16_t)q;
87 }
88 /*If this is a duplicate of a previous matrix, use that instead.
89 This simple check helps us improve cache coherency later.*/
90 {
91 int dupe;
92 int qtj;
93 int plj;
94 dupe=0;
95 for(qtj=0;qtj<=qti;qtj++){
96 for(plj=0;plj<(qtj<qti?3:pli);plj++){
97 if(!memcmp(_dequant[qi][pli][qti],_dequant[qi][plj][qtj],
98 sizeof(oc_quant_table))){
99 dupe=1;
100 break;
101 }
102 }
103 if(dupe)break;
104 }
105 if(dupe)_dequant[qi][pli][qti]=_dequant[qi][plj][qtj];
106 }
107 if(++qi>=qi_end)break;
108 /*Interpolate the next base matrix.*/
109 for(ci=0;ci<64;ci++){
110 base[ci]=(unsigned char)(
111 (2*((qi_end-qi)*_qinfo->qi_ranges[qti][pli].base_matrices[qri][ci]+
112 (qi-qi_start)*_qinfo->qi_ranges[qti][pli].base_matrices[qri+1][ci])
113 +_qinfo->qi_ranges[qti][pli].sizes[qri])/
114 (2*_qinfo->qi_ranges[qti][pli].sizes[qri]));
115 }
116 }
117 }
118 }
119 }
120