1 /********************************************************************
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. *
8 * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2007 *
9 * by the Xiph.Org Foundation http://www.xiph.org/ *
11 ********************************************************************
14 last mod: $Id: quant.c 14375 2008-01-06 05:37:33Z tterribe $
16 ********************************************************************/
24 unsigned OC_DC_QUANT_MIN
[2]={4<<2,8<<2};
25 unsigned OC_AC_QUANT_MIN
[2]={2<<2,4<<2};
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
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
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).*/
40 void oc_dequant_tables_init(oc_quant_table
*_dequant
[2][3],
41 int _pp_dc_scale
[64],const th_quant_info
*_qinfo
){
42 int qti
; /* coding mode: intra or inter */
44 for(qti
=0;qti
<2;qti
++){
45 for(pli
=0;pli
<3;pli
++){
46 oc_quant_tables stage
;
48 int qi
; /* quality index */
49 int qri
; /* range iterator */
51 for(qi
=0,qri
=0; qri
<=_qinfo
->qi_ranges
[qti
][pli
].nranges
; qri
++){
58 memcpy(base
,_qinfo
->qi_ranges
[qti
][pli
].base_matrices
[qri
],
62 if(qri
==_qinfo
->qi_ranges
[qti
][pli
].nranges
)
65 qi_end
=qi
+_qinfo
->qi_ranges
[qti
][pli
].sizes
[qri
];
67 /* Iterate over quality indicies in this range */
70 /*In the original VP3.2 code, the rounding offset and the size of the
71 dead zone around 0 were controlled by a "sharpness" parameter.
72 The size of our dead zone is now controlled by the per-coefficient
73 quality thresholds returned by our HVS module.
74 We round down from a more accurate value when the quality of the
75 reconstruction does not fall below our threshold and it saves bits.
76 Hence, all of that VP3.2 code is gone from here, and the remaining
77 floating point code has been implemented as equivalent integer code
78 with exact precision.*/
80 /* for postprocess, not dequant */
81 if(_pp_dc_scale
!=NULL
)
82 _pp_dc_scale
[qi
]=(int)((ogg_uint32_t
)_qinfo
->dc_scale
[qi
]*base
[0]/160);
84 /*Scale DC the coefficient from the proper table.*/
85 q
=((ogg_uint32_t
)_qinfo
->dc_scale
[qi
]*base
[0]/100)<<2;
86 q
=OC_CLAMPI(OC_DC_QUANT_MIN
[qti
],q
,OC_QUANT_MAX
);
87 stage
[qi
][0]=(ogg_uint16_t
)q
;
89 /*Now scale AC coefficients from the proper table.*/
91 q
=((ogg_uint32_t
)_qinfo
->ac_scale
[qi
]*base
[ci
]/100)<<2;
92 q
=OC_CLAMPI(OC_AC_QUANT_MIN
[qti
],q
,OC_QUANT_MAX
);
93 stage
[qi
][ci
]=(ogg_uint16_t
)q
;
96 if(++qi
>=qi_end
)break;
98 /*Interpolate the next base matrix.*/
100 base
[ci
]=(unsigned char)
101 ((2*((qi_end
-qi
)*_qinfo
->qi_ranges
[qti
][pli
].base_matrices
[qri
][ci
]+
102 (qi
-qi_start
)*_qinfo
->qi_ranges
[qti
][pli
].base_matrices
[qri
+1][ci
])
103 +_qinfo
->qi_ranges
[qti
][pli
].sizes
[qri
])/
104 (2*_qinfo
->qi_ranges
[qti
][pli
].sizes
[qri
]));
109 /* Staging matricies complete; commit to memory only if this
110 isn't a duplicate of a preceeding plane. This simple check
111 helps us improve cache coherency later.*/
116 for(j
=0;j
<(i
<qti
?3:pli
);j
++){
117 if(!memcmp(stage
,_dequant
[i
][j
],sizeof(stage
))){
125 _dequant
[qti
][pli
]=_dequant
[i
][j
];
127 memcpy(_dequant
[qti
][pli
],stage
,sizeof(stage
));
135 /* dump the calculated quantizer tables */
139 TH_DEBUG("quantizer table [%s][%s][Q%d] = {",
140 (i
==0?"intra":"inter"),(j
==0?"Y":(j
==1?"U":"V")),k
);
144 TH_DEBUG("%4d ",_dequant
[i
][j
][k
][l
]);