Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /******************************************************************** |
michael@0 | 2 | * * |
michael@0 | 3 | * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE. * |
michael@0 | 4 | * * |
michael@0 | 5 | * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * |
michael@0 | 6 | * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * |
michael@0 | 7 | * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * |
michael@0 | 8 | * * |
michael@0 | 9 | * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2002 * |
michael@0 | 10 | * BY THE Xiph.Org FOUNDATION http://www.xiph.org/ * |
michael@0 | 11 | * * |
michael@0 | 12 | ******************************************************************** |
michael@0 | 13 | |
michael@0 | 14 | function: residue backend 0, 1 and 2 implementation |
michael@0 | 15 | |
michael@0 | 16 | ********************************************************************/ |
michael@0 | 17 | |
michael@0 | 18 | #include <stdlib.h> |
michael@0 | 19 | #include <string.h> |
michael@0 | 20 | #include <math.h> |
michael@0 | 21 | #include <ogg/ogg.h> |
michael@0 | 22 | #include "ivorbiscodec.h" |
michael@0 | 23 | #include "codec_internal.h" |
michael@0 | 24 | #include "registry.h" |
michael@0 | 25 | #include "codebook.h" |
michael@0 | 26 | #include "misc.h" |
michael@0 | 27 | #include "os.h" |
michael@0 | 28 | #include "block.h" |
michael@0 | 29 | |
michael@0 | 30 | typedef struct { |
michael@0 | 31 | vorbis_info_residue0 *info; |
michael@0 | 32 | int map; |
michael@0 | 33 | |
michael@0 | 34 | int parts; |
michael@0 | 35 | int stages; |
michael@0 | 36 | codebook *fullbooks; |
michael@0 | 37 | codebook *phrasebook; |
michael@0 | 38 | codebook ***partbooks; |
michael@0 | 39 | |
michael@0 | 40 | int partvals; |
michael@0 | 41 | int **decodemap; |
michael@0 | 42 | |
michael@0 | 43 | } vorbis_look_residue0; |
michael@0 | 44 | |
michael@0 | 45 | void res0_free_info(vorbis_info_residue *i){ |
michael@0 | 46 | vorbis_info_residue0 *info=(vorbis_info_residue0 *)i; |
michael@0 | 47 | if(info){ |
michael@0 | 48 | memset(info,0,sizeof(*info)); |
michael@0 | 49 | _ogg_free(info); |
michael@0 | 50 | } |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | void res0_free_look(vorbis_look_residue *i){ |
michael@0 | 54 | int j; |
michael@0 | 55 | if(i){ |
michael@0 | 56 | |
michael@0 | 57 | vorbis_look_residue0 *look=(vorbis_look_residue0 *)i; |
michael@0 | 58 | |
michael@0 | 59 | for(j=0;j<look->parts;j++) |
michael@0 | 60 | if(look->partbooks[j])_ogg_free(look->partbooks[j]); |
michael@0 | 61 | _ogg_free(look->partbooks); |
michael@0 | 62 | for(j=0;j<look->partvals;j++) |
michael@0 | 63 | _ogg_free(look->decodemap[j]); |
michael@0 | 64 | _ogg_free(look->decodemap); |
michael@0 | 65 | |
michael@0 | 66 | memset(look,0,sizeof(*look)); |
michael@0 | 67 | _ogg_free(look); |
michael@0 | 68 | } |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | static int ilog(unsigned int v){ |
michael@0 | 72 | int ret=0; |
michael@0 | 73 | while(v){ |
michael@0 | 74 | ret++; |
michael@0 | 75 | v>>=1; |
michael@0 | 76 | } |
michael@0 | 77 | return(ret); |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | static int icount(unsigned int v){ |
michael@0 | 81 | int ret=0; |
michael@0 | 82 | while(v){ |
michael@0 | 83 | ret+=v&1; |
michael@0 | 84 | v>>=1; |
michael@0 | 85 | } |
michael@0 | 86 | return(ret); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | /* vorbis_info is for range checking */ |
michael@0 | 90 | vorbis_info_residue *res0_unpack(vorbis_info *vi,oggpack_buffer *opb){ |
michael@0 | 91 | int j,acc=0; |
michael@0 | 92 | vorbis_info_residue0 *info=(vorbis_info_residue0 *)_ogg_calloc(1,sizeof(*info)); |
michael@0 | 93 | codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; |
michael@0 | 94 | |
michael@0 | 95 | info->begin=oggpack_read(opb,24); |
michael@0 | 96 | info->end=oggpack_read(opb,24); |
michael@0 | 97 | info->grouping=oggpack_read(opb,24)+1; |
michael@0 | 98 | info->partitions=oggpack_read(opb,6)+1; |
michael@0 | 99 | info->groupbook=oggpack_read(opb,8); |
michael@0 | 100 | |
michael@0 | 101 | /* check for premature EOP */ |
michael@0 | 102 | if(info->groupbook<0)goto errout; |
michael@0 | 103 | |
michael@0 | 104 | for(j=0;j<info->partitions;j++){ |
michael@0 | 105 | int cascade=oggpack_read(opb,3); |
michael@0 | 106 | int cflag=oggpack_read(opb,1); |
michael@0 | 107 | if(cflag<0) goto errout; |
michael@0 | 108 | if(cflag){ |
michael@0 | 109 | int c=oggpack_read(opb,5); |
michael@0 | 110 | if(c<0) goto errout; |
michael@0 | 111 | cascade|=(c<<3); |
michael@0 | 112 | } |
michael@0 | 113 | info->secondstages[j]=cascade; |
michael@0 | 114 | |
michael@0 | 115 | acc+=icount(cascade); |
michael@0 | 116 | } |
michael@0 | 117 | for(j=0;j<acc;j++){ |
michael@0 | 118 | int book=oggpack_read(opb,8); |
michael@0 | 119 | if(book<0) goto errout; |
michael@0 | 120 | info->booklist[j]=book; |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | if(info->groupbook>=ci->books)goto errout; |
michael@0 | 124 | for(j=0;j<acc;j++){ |
michael@0 | 125 | if(info->booklist[j]>=ci->books)goto errout; |
michael@0 | 126 | if(ci->book_param[info->booklist[j]]->maptype==0)goto errout; |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | /* verify the phrasebook is not specifying an impossible or |
michael@0 | 130 | inconsistent partitioning scheme. */ |
michael@0 | 131 | /* modify the phrasebook ranging check from r16327; an early beta |
michael@0 | 132 | encoder had a bug where it used an oversized phrasebook by |
michael@0 | 133 | accident. These files should continue to be playable, but don't |
michael@0 | 134 | allow an exploit */ |
michael@0 | 135 | { |
michael@0 | 136 | int entries = ci->book_param[info->groupbook]->entries; |
michael@0 | 137 | int dim = ci->book_param[info->groupbook]->dim; |
michael@0 | 138 | int partvals = 1; |
michael@0 | 139 | if (dim<1) goto errout; |
michael@0 | 140 | while(dim>0){ |
michael@0 | 141 | partvals *= info->partitions; |
michael@0 | 142 | if(partvals > entries) goto errout; |
michael@0 | 143 | dim--; |
michael@0 | 144 | } |
michael@0 | 145 | info->partvals = partvals; |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | return(info); |
michael@0 | 149 | errout: |
michael@0 | 150 | res0_free_info(info); |
michael@0 | 151 | return(NULL); |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | vorbis_look_residue *res0_look(vorbis_dsp_state *vd,vorbis_info_mode *vm, |
michael@0 | 155 | vorbis_info_residue *vr){ |
michael@0 | 156 | vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr; |
michael@0 | 157 | vorbis_look_residue0 *look=(vorbis_look_residue0 *)_ogg_calloc(1,sizeof(*look)); |
michael@0 | 158 | codec_setup_info *ci=(codec_setup_info *)vd->vi->codec_setup; |
michael@0 | 159 | |
michael@0 | 160 | int j,k,acc=0; |
michael@0 | 161 | int dim; |
michael@0 | 162 | int maxstage=0; |
michael@0 | 163 | look->info=info; |
michael@0 | 164 | look->map=vm->mapping; |
michael@0 | 165 | |
michael@0 | 166 | look->parts=info->partitions; |
michael@0 | 167 | look->fullbooks=ci->fullbooks; |
michael@0 | 168 | look->phrasebook=ci->fullbooks+info->groupbook; |
michael@0 | 169 | dim=look->phrasebook->dim; |
michael@0 | 170 | |
michael@0 | 171 | look->partbooks=(codebook ***)_ogg_calloc(look->parts,sizeof(*look->partbooks)); |
michael@0 | 172 | |
michael@0 | 173 | for(j=0;j<look->parts;j++){ |
michael@0 | 174 | int stages=ilog(info->secondstages[j]); |
michael@0 | 175 | if(stages){ |
michael@0 | 176 | if(stages>maxstage)maxstage=stages; |
michael@0 | 177 | look->partbooks[j]=(codebook **)_ogg_calloc(stages,sizeof(*look->partbooks[j])); |
michael@0 | 178 | for(k=0;k<stages;k++) |
michael@0 | 179 | if(info->secondstages[j]&(1<<k)){ |
michael@0 | 180 | look->partbooks[j][k]=ci->fullbooks+info->booklist[acc++]; |
michael@0 | 181 | #ifdef TRAIN_RES |
michael@0 | 182 | look->training_data[k][j]=calloc(look->partbooks[j][k]->entries, |
michael@0 | 183 | sizeof(***look->training_data)); |
michael@0 | 184 | #endif |
michael@0 | 185 | } |
michael@0 | 186 | } |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | look->partvals=look->parts; |
michael@0 | 190 | for(j=1;j<dim;j++)look->partvals*=look->parts; |
michael@0 | 191 | look->stages=maxstage; |
michael@0 | 192 | look->decodemap=(int **)_ogg_malloc(look->partvals*sizeof(*look->decodemap)); |
michael@0 | 193 | for(j=0;j<look->partvals;j++){ |
michael@0 | 194 | long val=j; |
michael@0 | 195 | long mult=look->partvals/look->parts; |
michael@0 | 196 | look->decodemap[j]=(int *)_ogg_malloc(dim*sizeof(*look->decodemap[j])); |
michael@0 | 197 | for(k=0;k<dim;k++){ |
michael@0 | 198 | long deco=val/mult; |
michael@0 | 199 | val-=deco*mult; |
michael@0 | 200 | mult/=look->parts; |
michael@0 | 201 | look->decodemap[j][k]=deco; |
michael@0 | 202 | } |
michael@0 | 203 | } |
michael@0 | 204 | |
michael@0 | 205 | return(look); |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | |
michael@0 | 209 | /* a truncated packet here just means 'stop working'; it's not an error */ |
michael@0 | 210 | static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl, |
michael@0 | 211 | ogg_int32_t **in,int ch, |
michael@0 | 212 | long (*decodepart)(codebook *, ogg_int32_t *, |
michael@0 | 213 | oggpack_buffer *,int,int)){ |
michael@0 | 214 | |
michael@0 | 215 | long i,j,k,l,s; |
michael@0 | 216 | vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl; |
michael@0 | 217 | vorbis_info_residue0 *info=look->info; |
michael@0 | 218 | |
michael@0 | 219 | /* move all this setup out later */ |
michael@0 | 220 | int samples_per_partition=info->grouping; |
michael@0 | 221 | int partitions_per_word=look->phrasebook->dim; |
michael@0 | 222 | int max=vb->pcmend>>1; |
michael@0 | 223 | int end=(info->end<max?info->end:max); |
michael@0 | 224 | int n=end-info->begin; |
michael@0 | 225 | |
michael@0 | 226 | if(n>0){ |
michael@0 | 227 | int partvals=n/samples_per_partition; |
michael@0 | 228 | int partwords=(partvals+partitions_per_word-1)/partitions_per_word; |
michael@0 | 229 | int ***partword=(int ***)alloca(ch*sizeof(*partword)); |
michael@0 | 230 | |
michael@0 | 231 | for(j=0;j<ch;j++) |
michael@0 | 232 | partword[j]=(int **)_vorbis_block_alloc(vb,partwords*sizeof(*partword[j])); |
michael@0 | 233 | |
michael@0 | 234 | for(s=0;s<look->stages;s++){ |
michael@0 | 235 | |
michael@0 | 236 | /* each loop decodes on partition codeword containing |
michael@0 | 237 | partitions_pre_word partitions */ |
michael@0 | 238 | for(i=0,l=0;i<partvals;l++){ |
michael@0 | 239 | if(s==0){ |
michael@0 | 240 | /* fetch the partition word for each channel */ |
michael@0 | 241 | for(j=0;j<ch;j++){ |
michael@0 | 242 | int temp=vorbis_book_decode(look->phrasebook,&vb->opb); |
michael@0 | 243 | if(temp==-1 || temp>=info->partvals)goto eopbreak; |
michael@0 | 244 | partword[j][l]=look->decodemap[temp]; |
michael@0 | 245 | if(partword[j][l]==NULL)goto errout; |
michael@0 | 246 | } |
michael@0 | 247 | } |
michael@0 | 248 | |
michael@0 | 249 | /* now we decode residual values for the partitions */ |
michael@0 | 250 | for(k=0;k<partitions_per_word && i<partvals;k++,i++) |
michael@0 | 251 | for(j=0;j<ch;j++){ |
michael@0 | 252 | long offset=info->begin+i*samples_per_partition; |
michael@0 | 253 | if(info->secondstages[partword[j][l][k]]&(1<<s)){ |
michael@0 | 254 | codebook *stagebook=look->partbooks[partword[j][l][k]][s]; |
michael@0 | 255 | if(stagebook){ |
michael@0 | 256 | if(decodepart(stagebook,in[j]+offset,&vb->opb, |
michael@0 | 257 | samples_per_partition,-8)==-1)goto eopbreak; |
michael@0 | 258 | } |
michael@0 | 259 | } |
michael@0 | 260 | } |
michael@0 | 261 | } |
michael@0 | 262 | } |
michael@0 | 263 | } |
michael@0 | 264 | errout: |
michael@0 | 265 | eopbreak: |
michael@0 | 266 | return(0); |
michael@0 | 267 | } |
michael@0 | 268 | |
michael@0 | 269 | int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl, |
michael@0 | 270 | ogg_int32_t **in,int *nonzero,int ch){ |
michael@0 | 271 | int i,used=0; |
michael@0 | 272 | for(i=0;i<ch;i++) |
michael@0 | 273 | if(nonzero[i]) |
michael@0 | 274 | in[used++]=in[i]; |
michael@0 | 275 | if(used) |
michael@0 | 276 | return(_01inverse(vb,vl,in,used,vorbis_book_decodevs_add)); |
michael@0 | 277 | else |
michael@0 | 278 | return(0); |
michael@0 | 279 | } |
michael@0 | 280 | |
michael@0 | 281 | int res1_inverse(vorbis_block *vb,vorbis_look_residue *vl, |
michael@0 | 282 | ogg_int32_t **in,int *nonzero,int ch){ |
michael@0 | 283 | int i,used=0; |
michael@0 | 284 | for(i=0;i<ch;i++) |
michael@0 | 285 | if(nonzero[i]) |
michael@0 | 286 | in[used++]=in[i]; |
michael@0 | 287 | if(used) |
michael@0 | 288 | return(_01inverse(vb,vl,in,used,vorbis_book_decodev_add)); |
michael@0 | 289 | else |
michael@0 | 290 | return(0); |
michael@0 | 291 | } |
michael@0 | 292 | |
michael@0 | 293 | /* duplicate code here as speed is somewhat more important */ |
michael@0 | 294 | int res2_inverse(vorbis_block *vb,vorbis_look_residue *vl, |
michael@0 | 295 | ogg_int32_t **in,int *nonzero,int ch){ |
michael@0 | 296 | long i,k,l,s; |
michael@0 | 297 | vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl; |
michael@0 | 298 | vorbis_info_residue0 *info=look->info; |
michael@0 | 299 | |
michael@0 | 300 | /* move all this setup out later */ |
michael@0 | 301 | int samples_per_partition=info->grouping; |
michael@0 | 302 | int partitions_per_word=look->phrasebook->dim; |
michael@0 | 303 | int max=(vb->pcmend*ch)>>1; |
michael@0 | 304 | int end=(info->end<max?info->end:max); |
michael@0 | 305 | int n=end-info->begin; |
michael@0 | 306 | |
michael@0 | 307 | if(n>0){ |
michael@0 | 308 | |
michael@0 | 309 | int partvals=n/samples_per_partition; |
michael@0 | 310 | int partwords=(partvals+partitions_per_word-1)/partitions_per_word; |
michael@0 | 311 | int **partword=(int **)_vorbis_block_alloc(vb,partwords*sizeof(*partword)); |
michael@0 | 312 | int beginoff=info->begin/ch; |
michael@0 | 313 | |
michael@0 | 314 | for(i=0;i<ch;i++)if(nonzero[i])break; |
michael@0 | 315 | if(i==ch)return(0); /* no nonzero vectors */ |
michael@0 | 316 | |
michael@0 | 317 | samples_per_partition/=ch; |
michael@0 | 318 | |
michael@0 | 319 | for(s=0;s<look->stages;s++){ |
michael@0 | 320 | for(i=0,l=0;i<partvals;l++){ |
michael@0 | 321 | |
michael@0 | 322 | if(s==0){ |
michael@0 | 323 | /* fetch the partition word */ |
michael@0 | 324 | int temp=vorbis_book_decode(look->phrasebook,&vb->opb); |
michael@0 | 325 | if(temp==-1 || temp>=info->partvals)goto eopbreak; |
michael@0 | 326 | partword[l]=look->decodemap[temp]; |
michael@0 | 327 | if(partword[l]==NULL)goto errout; |
michael@0 | 328 | } |
michael@0 | 329 | |
michael@0 | 330 | /* now we decode residual values for the partitions */ |
michael@0 | 331 | for(k=0;k<partitions_per_word && i<partvals;k++,i++) |
michael@0 | 332 | if(info->secondstages[partword[l][k]]&(1<<s)){ |
michael@0 | 333 | codebook *stagebook=look->partbooks[partword[l][k]][s]; |
michael@0 | 334 | |
michael@0 | 335 | if(stagebook){ |
michael@0 | 336 | if(vorbis_book_decodevv_add(stagebook,in, |
michael@0 | 337 | i*samples_per_partition+beginoff,ch, |
michael@0 | 338 | &vb->opb, |
michael@0 | 339 | samples_per_partition,-8)==-1) |
michael@0 | 340 | goto eopbreak; |
michael@0 | 341 | } |
michael@0 | 342 | } |
michael@0 | 343 | } |
michael@0 | 344 | } |
michael@0 | 345 | } |
michael@0 | 346 | errout: |
michael@0 | 347 | eopbreak: |
michael@0 | 348 | return(0); |
michael@0 | 349 | } |
michael@0 | 350 | |
michael@0 | 351 | |
michael@0 | 352 | vorbis_func_residue residue0_exportbundle={ |
michael@0 | 353 | &res0_unpack, |
michael@0 | 354 | &res0_look, |
michael@0 | 355 | &res0_free_info, |
michael@0 | 356 | &res0_free_look, |
michael@0 | 357 | &res0_inverse |
michael@0 | 358 | }; |
michael@0 | 359 | |
michael@0 | 360 | vorbis_func_residue residue1_exportbundle={ |
michael@0 | 361 | &res0_unpack, |
michael@0 | 362 | &res0_look, |
michael@0 | 363 | &res0_free_info, |
michael@0 | 364 | &res0_free_look, |
michael@0 | 365 | &res1_inverse |
michael@0 | 366 | }; |
michael@0 | 367 | |
michael@0 | 368 | vorbis_func_residue residue2_exportbundle={ |
michael@0 | 369 | &res0_unpack, |
michael@0 | 370 | &res0_look, |
michael@0 | 371 | &res0_free_info, |
michael@0 | 372 | &res0_free_look, |
michael@0 | 373 | &res2_inverse |
michael@0 | 374 | }; |