Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / media / libtheora / lib / dec / quant.c
blob8008600076571941fd732d6daafe921c6d25b4dc
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-2007 *
9 * by the Xiph.Org Foundation http://www.xiph.org/ *
10 * *
11 ********************************************************************
13 function:
14 last mod: $Id: quant.c 14375 2008-01-06 05:37:33Z tterribe $
16 ********************************************************************/
18 #include <stdlib.h>
19 #include <string.h>
20 #include <ogg/ogg.h>
21 #include "quant.h"
22 #include "decint.h"
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
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).*/
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 */
43 int pli; /* Y U V */
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++){
52 th_quant_base base;
54 ogg_uint32_t q;
55 int qi_start;
56 int qi_end;
57 int ci;
58 memcpy(base,_qinfo->qi_ranges[qti][pli].base_matrices[qri],
59 sizeof(base));
61 qi_start=qi;
62 if(qri==_qinfo->qi_ranges[qti][pli].nranges)
63 qi_end=qi+1;
64 else
65 qi_end=qi+_qinfo->qi_ranges[qti][pli].sizes[qri];
67 /* Iterate over quality indicies in this range */
68 for(;;){
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.*/
90 for(ci=1;ci<64;ci++){
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.*/
99 for(ci=0;ci<64;ci++){
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.*/
113 int dupe = 0;
114 int i,j;
115 for(i=0;i<=qti;i++){
116 for(j=0;j<(i<qti?3:pli);j++){
117 if(!memcmp(stage,_dequant[i][j],sizeof(stage))){
118 dupe = 1;
119 break;
122 if(dupe)break;
124 if(dupe){
125 _dequant[qti][pli]=_dequant[i][j];
126 }else{
127 memcpy(_dequant[qti][pli],stage,sizeof(stage));
133 #ifdef _TH_DEBUG_
134 int i, j, k, l;
135 /* dump the calculated quantizer tables */
136 for(i=0;i<2;i++){
137 for(j=0;j<3;j++){
138 for(k=0;k<64;k++){
139 TH_DEBUG("quantizer table [%s][%s][Q%d] = {",
140 (i==0?"intra":"inter"),(j==0?"Y":(j==1?"U":"V")),k);
141 for(l=0;l<64;l++){
142 if((l&7)==0)
143 TH_DEBUG("\n ");
144 TH_DEBUG("%4d ",_dequant[i][j][k][l]);
146 TH_DEBUG("}\n");
150 #endif