Follow-on fix for bug 457825. Use sheet principal for agent and user sheets. r=dbaron...
[wine-gecko.git] / media / libtheora / lib / dec / dequant.c
blobe538931f1666f8c223341ddb7196fc69063c2572
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: dequant.c 14369 2008-01-05 23:15:32Z tterribe $
16 ********************************************************************/
18 #include <stdlib.h>
19 #include <string.h>
20 #include <ogg/ogg.h>
21 #include "dequant.h"
22 #include "decint.h"
24 int oc_quant_params_unpack(oggpack_buffer *_opb,
25 th_quant_info *_qinfo){
26 th_quant_base *base_mats;
27 long val;
28 int nbase_mats;
29 int sizes[64];
30 int indices[64];
31 int nbits;
32 int bmi;
33 int ci;
34 int qti;
35 int pli;
36 int qri;
37 int qi;
38 int i;
39 theorapackB_read(_opb,3,&val);
40 nbits=(int)val;
41 for(qi=0;qi<64;qi++){
42 theorapackB_read(_opb,nbits,&val);
43 _qinfo->loop_filter_limits[qi]=(unsigned char)val;
45 theorapackB_read(_opb,4,&val);
46 nbits=(int)val+1;
47 for(qi=0;qi<64;qi++){
48 theorapackB_read(_opb,nbits,&val);
49 _qinfo->ac_scale[qi]=(ogg_uint16_t)val;
51 theorapackB_read(_opb,4,&val);
52 nbits=(int)val+1;
53 for(qi=0;qi<64;qi++){
54 theorapackB_read(_opb,nbits,&val);
55 _qinfo->dc_scale[qi]=(ogg_uint16_t)val;
57 theorapackB_read(_opb,9,&val);
58 nbase_mats=(int)val+1;
59 base_mats=_ogg_malloc(nbase_mats*sizeof(base_mats[0]));
60 for(bmi=0;bmi<nbase_mats;bmi++){
61 for(ci=0;ci<64;ci++){
62 theorapackB_read(_opb,8,&val);
63 base_mats[bmi][ci]=(unsigned char)val;
66 nbits=oc_ilog(nbase_mats-1);
67 for(i=0;i<6;i++){
68 th_quant_ranges *qranges;
69 th_quant_base *qrbms;
70 int *qrsizes;
71 qti=i/3;
72 pli=i%3;
73 qranges=_qinfo->qi_ranges[qti]+pli;
74 if(i>0){
75 theorapackB_read1(_opb,&val);
76 if(!val){
77 int qtj;
78 int plj;
79 if(qti>0){
80 theorapackB_read1(_opb,&val);
81 if(val){
82 qtj=qti-1;
83 plj=pli;
85 else{
86 qtj=(i-1)/3;
87 plj=(i-1)%3;
90 else{
91 qtj=(i-1)/3;
92 plj=(i-1)%3;
94 *qranges=*(_qinfo->qi_ranges[qtj]+plj);
95 continue;
98 theorapackB_read(_opb,nbits,&val);
99 indices[0]=(int)val;
100 for(qi=qri=0;qi<63;){
101 theorapackB_read(_opb,oc_ilog(62-qi),&val);
102 sizes[qri]=(int)val+1;
103 qi+=(int)val+1;
104 theorapackB_read(_opb,nbits,&val);
105 indices[++qri]=(int)val;
107 /*Note: The caller is responsible for cleaning up any partially
108 constructed qinfo.*/
109 if(qi>63){
110 _ogg_free(base_mats);
111 return TH_EBADHEADER;
113 qranges->nranges=qri;
114 qranges->sizes=qrsizes=(int *)_ogg_malloc(qri*sizeof(qrsizes[0]));
115 memcpy(qrsizes,sizes,qri*sizeof(qrsizes[0]));
116 qrbms=(th_quant_base *)_ogg_malloc((qri+1)*sizeof(qrbms[0]));
117 qranges->base_matrices=(const th_quant_base *)qrbms;
119 bmi=indices[qri];
120 /*Note: The caller is responsible for cleaning up any partially
121 constructed qinfo.*/
122 if(bmi>=nbase_mats){
123 _ogg_free(base_mats);
124 return TH_EBADHEADER;
126 memcpy(qrbms[qri],base_mats[bmi],sizeof(qrbms[qri]));
128 while(qri-->0);
131 #ifdef _TH_DEBUG_
132 /* dump the tables */
134 int i, j, k, l, m;
135 TH_DEBUG("loop filter limits = {");
136 for(i=0;i<64;){
137 TH_DEBUG("\n ");
138 for(j=0;j<16;i++,j++)
139 TH_DEBUG("%3d ",_qinfo->loop_filter_limits[i]);
141 TH_DEBUG("\n}\n\n");
143 TH_DEBUG("ac scale = {");
144 for(i=0;i<64;){
145 TH_DEBUG("\n ");
146 for(j=0;j<16;i++,j++)
147 TH_DEBUG("%3d ",_qinfo->ac_scale[i]);
149 TH_DEBUG("\n}\n\n");
151 TH_DEBUG("dc scale = {");
152 for(i=0;i<64;){
153 TH_DEBUG("\n ");
154 for(j=0;j<16;i++,j++)
155 TH_DEBUG("%3d ",_qinfo->dc_scale[i]);
157 TH_DEBUG("\n}\n\n");
159 for(k=0;k<2;k++)
160 for(l=0;l<3;l++){
161 char *name[2][3]={
162 {"intra Y bases","intra U bases", "intra V bases"},
163 {"inter Y bases","inter U bases", "inter V bases"}
166 th_quant_ranges *r = &_qinfo->qi_ranges[k][l];
167 TH_DEBUG("%s = {\n",name[k][l]);
168 TH_DEBUG(" ranges = %d\n",r->nranges);
169 TH_DEBUG(" intervals = { ");
170 for(i=0;i<r->nranges;i++)
171 TH_DEBUG("%3d ",r->sizes[i]);
172 TH_DEBUG("}\n");
173 TH_DEBUG("\n matricies = { ");
174 for(m=0;m<r->nranges+1;m++){
175 TH_DEBUG("\n { ");
176 for(i=0;i<64;){
177 TH_DEBUG("\n ");
178 for(j=0;j<8;i++,j++)
179 TH_DEBUG("%3d ",r->base_matrices[m][i]);
181 TH_DEBUG("\n }");
183 TH_DEBUG("\n }\n");
187 #endif
189 _ogg_free(base_mats);
190 return 0;
193 void oc_quant_params_clear(th_quant_info *_qinfo){
194 int i;
195 for(i=6;i-->0;){
196 int qti;
197 int pli;
198 qti=i/3;
199 pli=i%3;
200 /*Clear any duplicate pointer references.*/
201 if(i>0){
202 int qtj;
203 int plj;
204 qtj=(i-1)/3;
205 plj=(i-1)%3;
206 if(_qinfo->qi_ranges[qti][pli].sizes==
207 _qinfo->qi_ranges[qtj][plj].sizes){
208 _qinfo->qi_ranges[qti][pli].sizes=NULL;
210 if(_qinfo->qi_ranges[qti][pli].base_matrices==
211 _qinfo->qi_ranges[qtj][plj].base_matrices){
212 _qinfo->qi_ranges[qti][pli].base_matrices=NULL;
215 if(qti>0){
216 if(_qinfo->qi_ranges[1][pli].sizes==
217 _qinfo->qi_ranges[0][pli].sizes){
218 _qinfo->qi_ranges[1][pli].sizes=NULL;
220 if(_qinfo->qi_ranges[1][pli].base_matrices==
221 _qinfo->qi_ranges[0][pli].base_matrices){
222 _qinfo->qi_ranges[1][pli].base_matrices=NULL;
225 /*Now free all the non-duplicate storage.*/
226 _ogg_free((void *)_qinfo->qi_ranges[qti][pli].sizes);
227 _ogg_free((void *)_qinfo->qi_ranges[qti][pli].base_matrices);