Gloria Wang | 37fe158 | 2010-03-12 14:53:20 -0800 | [diff] [blame] | 1 | /************************************************************************ |
Gloria Wang | 2da723a | 2010-03-18 15:56:16 -0700 | [diff] [blame] | 2 | * Copyright (C) 2002-2009, Xiph.org Foundation |
| 3 | * Copyright (C) 2010, Robin Watts for Pinknoise Productions Ltd |
Gloria Wang | 37fe158 | 2010-03-12 14:53:20 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
Gloria Wang | 2da723a | 2010-03-18 15:56:16 -0700 | [diff] [blame] | 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
Gloria Wang | 37fe158 | 2010-03-12 14:53:20 -0800 | [diff] [blame] | 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
Gloria Wang | 2da723a | 2010-03-18 15:56:16 -0700 | [diff] [blame] | 16 | * * Neither the names of the Xiph.org Foundation nor Pinknoise |
| 17 | * Productions Ltd nor the names of its contributors may be used to |
| 18 | * endorse or promote products derived from this software without |
| 19 | * specific prior written permission. |
Gloria Wang | 37fe158 | 2010-03-12 14:53:20 -0800 | [diff] [blame] | 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | ************************************************************************ |
Gloria Wang | 7913073 | 2010-02-08 14:41:04 -0800 | [diff] [blame] | 33 | |
| 34 | function: residue backend 0, 1 and 2 implementation |
| 35 | |
Gloria Wang | 37fe158 | 2010-03-12 14:53:20 -0800 | [diff] [blame] | 36 | ************************************************************************/ |
Gloria Wang | 7913073 | 2010-02-08 14:41:04 -0800 | [diff] [blame] | 37 | |
| 38 | #include <stdlib.h> |
| 39 | #include <string.h> |
| 40 | #include <math.h> |
| 41 | #include "ogg.h" |
| 42 | #include "ivorbiscodec.h" |
| 43 | #include "codec_internal.h" |
| 44 | #include "codebook.h" |
| 45 | #include "misc.h" |
| 46 | #include "os.h" |
| 47 | |
| 48 | void res_clear_info(vorbis_info_residue *info){ |
| 49 | if(info){ |
| 50 | if(info->stagemasks)_ogg_free(info->stagemasks); |
| 51 | if(info->stagebooks)_ogg_free(info->stagebooks); |
| 52 | memset(info,0,sizeof(*info)); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | |
| 57 | /* vorbis_info is for range checking */ |
| 58 | int res_unpack(vorbis_info_residue *info, |
Marco Nelissen | cd32a5d | 2016-05-20 09:14:36 -0700 | [diff] [blame] | 59 | vorbis_info *vi,oggpack_buffer *opb){ |
Gloria Wang | 7913073 | 2010-02-08 14:41:04 -0800 | [diff] [blame] | 60 | int j,k; |
| 61 | codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; |
| 62 | memset(info,0,sizeof(*info)); |
| 63 | |
| 64 | info->type=oggpack_read(opb,16); |
| 65 | if(info->type>2 || info->type<0)goto errout; |
| 66 | info->begin=oggpack_read(opb,24); |
| 67 | info->end=oggpack_read(opb,24); |
Marco Nelissen | c738794 | 2017-07-17 15:54:28 -0700 | [diff] [blame] | 68 | info->grouping=oggpack_read(opb,24)+1; // "partition size" in spec |
| 69 | info->partitions=(char)(oggpack_read(opb,6)+1); // "classification" in spec |
| 70 | info->groupbook=(unsigned char)oggpack_read(opb,8); // "classbook" in spec |
Gloria Wang | 7913073 | 2010-02-08 14:41:04 -0800 | [diff] [blame] | 71 | if(info->groupbook>=ci->books)goto errout; |
| 72 | |
| 73 | info->stagemasks=_ogg_malloc(info->partitions*sizeof(*info->stagemasks)); |
| 74 | info->stagebooks=_ogg_malloc(info->partitions*8*sizeof(*info->stagebooks)); |
| 75 | |
| 76 | for(j=0;j<info->partitions;j++){ |
| 77 | int cascade=oggpack_read(opb,3); |
| 78 | if(oggpack_read(opb,1)) |
| 79 | cascade|=(oggpack_read(opb,5)<<3); |
| 80 | info->stagemasks[j]=cascade; |
| 81 | } |
| 82 | |
| 83 | for(j=0;j<info->partitions;j++){ |
| 84 | for(k=0;k<8;k++){ |
| 85 | if((info->stagemasks[j]>>k)&1){ |
Marco Nelissen | cd32a5d | 2016-05-20 09:14:36 -0700 | [diff] [blame] | 86 | unsigned char book=(unsigned char)oggpack_read(opb,8); |
| 87 | if(book>=ci->books)goto errout; |
| 88 | info->stagebooks[j*8+k]=book; |
| 89 | if(k+1>info->stages)info->stages=k+1; |
Gloria Wang | 7913073 | 2010-02-08 14:41:04 -0800 | [diff] [blame] | 90 | }else |
Marco Nelissen | cd32a5d | 2016-05-20 09:14:36 -0700 | [diff] [blame] | 91 | info->stagebooks[j*8+k]=0xff; |
Gloria Wang | 7913073 | 2010-02-08 14:41:04 -0800 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | if(oggpack_eop(opb))goto errout; |
| 96 | |
Marco Nelissen | c738794 | 2017-07-17 15:54:28 -0700 | [diff] [blame] | 97 | // According to the Vorbis spec (paragraph 8.6.2 "packet decode"), residue |
| 98 | // begin and end should be limited to the maximum possible vector size in |
| 99 | // case they exceed it. However doing that makes the decoder crash further |
| 100 | // down, so we return an error instead. |
| 101 | int limit = (info->type == 2 ? vi->channels : 1) * ci->blocksizes[1] / 2; |
| 102 | if (info->begin > info->end || |
| 103 | info->end > limit) { |
| 104 | goto errout; |
| 105 | } |
Gloria Wang | 7913073 | 2010-02-08 14:41:04 -0800 | [diff] [blame] | 106 | return 0; |
| 107 | errout: |
| 108 | res_clear_info(info); |
| 109 | return 1; |
| 110 | } |
| 111 | |
| 112 | int res_inverse(vorbis_dsp_state *vd,vorbis_info_residue *info, |
Marco Nelissen | cd32a5d | 2016-05-20 09:14:36 -0700 | [diff] [blame] | 113 | ogg_int32_t **in,int *nonzero,int ch){ |
Gloria Wang | 7913073 | 2010-02-08 14:41:04 -0800 | [diff] [blame] | 114 | |
| 115 | int i,j,k,s,used=0; |
| 116 | codec_setup_info *ci=(codec_setup_info *)vd->vi->codec_setup; |
| 117 | codebook *phrasebook=ci->book_param+info->groupbook; |
| 118 | int samples_per_partition=info->grouping; |
| 119 | int partitions_per_word=phrasebook->dim; |
| 120 | int pcmend=ci->blocksizes[vd->W]; |
| 121 | |
| 122 | if(info->type<2){ |
| 123 | int max=pcmend>>1; |
| 124 | int end=(info->end<max?info->end:max); |
| 125 | int n=end-info->begin; |
| 126 | |
| 127 | if(n>0){ |
| 128 | int partvals=n/samples_per_partition; |
| 129 | int partwords=(partvals+partitions_per_word-1)/partitions_per_word; |
| 130 | |
| 131 | for(i=0;i<ch;i++) |
Marco Nelissen | cd32a5d | 2016-05-20 09:14:36 -0700 | [diff] [blame] | 132 | if(nonzero[i]) |
| 133 | in[used++]=in[i]; |
Gloria Wang | 7913073 | 2010-02-08 14:41:04 -0800 | [diff] [blame] | 134 | ch=used; |
| 135 | |
| 136 | if(used){ |
| 137 | |
Dongwon Kang | 013a3b5 | 2017-07-13 18:41:31 -0700 | [diff] [blame] | 138 | char **partword=(char **)_ogg_calloc(ch,sizeof(*partword)); |
| 139 | if(partword==NULL)goto cleanup1; |
| 140 | for(j=0;j<ch;j++){ |
| 141 | partword[j]=(char *)_ogg_malloc(partwords*partitions_per_word* |
| 142 | sizeof(*partword[j])); |
| 143 | if(partword[j]==NULL)goto cleanup1; |
| 144 | } |
Gloria Wang | 7913073 | 2010-02-08 14:41:04 -0800 | [diff] [blame] | 145 | |
Marco Nelissen | cd32a5d | 2016-05-20 09:14:36 -0700 | [diff] [blame] | 146 | for(s=0;s<info->stages;s++){ |
Gloria Wang | 7913073 | 2010-02-08 14:41:04 -0800 | [diff] [blame] | 147 | |
Marco Nelissen | cd32a5d | 2016-05-20 09:14:36 -0700 | [diff] [blame] | 148 | for(i=0;i<partvals;){ |
| 149 | if(s==0){ |
| 150 | /* fetch the partition word for each channel */ |
Gloria Wang | 7913073 | 2010-02-08 14:41:04 -0800 | [diff] [blame] | 151 | |
Marco Nelissen | cd32a5d | 2016-05-20 09:14:36 -0700 | [diff] [blame] | 152 | partword[0][i+partitions_per_word-1]=1; |
| 153 | for(k=partitions_per_word-2;k>=0;k--) |
| 154 | partword[0][i+k]=partword[0][i+k+1]*info->partitions; |
Gloria Wang | 7913073 | 2010-02-08 14:41:04 -0800 | [diff] [blame] | 155 | |
Marco Nelissen | cd32a5d | 2016-05-20 09:14:36 -0700 | [diff] [blame] | 156 | for(j=1;j<ch;j++) |
| 157 | for(k=partitions_per_word-1;k>=0;k--) |
| 158 | partword[j][i+k]=partword[j-1][i+k]; |
Gloria Wang | 7913073 | 2010-02-08 14:41:04 -0800 | [diff] [blame] | 159 | |
Marco Nelissen | cd32a5d | 2016-05-20 09:14:36 -0700 | [diff] [blame] | 160 | for(j=0;j<ch;j++){ |
| 161 | int temp=vorbis_book_decode(phrasebook,&vd->opb); |
Dongwon Kang | 013a3b5 | 2017-07-13 18:41:31 -0700 | [diff] [blame] | 162 | if(temp==-1)goto cleanup1; |
Gloria Wang | 7913073 | 2010-02-08 14:41:04 -0800 | [diff] [blame] | 163 | |
Marco Nelissen | cd32a5d | 2016-05-20 09:14:36 -0700 | [diff] [blame] | 164 | /* this can be done quickly in assembly due to the quotient |
| 165 | always being at most six bits */ |
| 166 | for(k=0;k<partitions_per_word;k++){ |
| 167 | ogg_uint32_t div=partword[j][i+k]; |
Marco Nelissen | fd877e9 | 2017-02-01 15:59:53 -0800 | [diff] [blame] | 168 | partword[j][i+k]= (div == 0) ? 0 : (temp / div); |
Marco Nelissen | cd32a5d | 2016-05-20 09:14:36 -0700 | [diff] [blame] | 169 | temp-=partword[j][i+k]*div; |
| 170 | } |
Gloria Wang | 7913073 | 2010-02-08 14:41:04 -0800 | [diff] [blame] | 171 | |
Marco Nelissen | cd32a5d | 2016-05-20 09:14:36 -0700 | [diff] [blame] | 172 | } |
| 173 | } |
Gloria Wang | 7913073 | 2010-02-08 14:41:04 -0800 | [diff] [blame] | 174 | |
Marco Nelissen | cd32a5d | 2016-05-20 09:14:36 -0700 | [diff] [blame] | 175 | /* now we decode residual values for the partitions */ |
| 176 | for(k=0;k<partitions_per_word && i<partvals;k++,i++) |
| 177 | for(j=0;j<ch;j++){ |
| 178 | long offset=info->begin+i*samples_per_partition; |
| 179 | int idx = (int)partword[j][i]; |
| 180 | if(idx < info->partitions && info->stagemasks[idx]&(1<<s)){ |
| 181 | codebook *stagebook=ci->book_param+ |
| 182 | info->stagebooks[(partword[j][i]<<3)+s]; |
| 183 | if(info->type){ |
| 184 | if(vorbis_book_decodev_add(stagebook,in[j]+offset,&vd->opb, |
| 185 | samples_per_partition,-8)==-1) |
Dongwon Kang | 013a3b5 | 2017-07-13 18:41:31 -0700 | [diff] [blame] | 186 | goto cleanup1; |
Marco Nelissen | cd32a5d | 2016-05-20 09:14:36 -0700 | [diff] [blame] | 187 | }else{ |
| 188 | if(vorbis_book_decodevs_add(stagebook,in[j]+offset,&vd->opb, |
| 189 | samples_per_partition,-8)==-1) |
Dongwon Kang | 013a3b5 | 2017-07-13 18:41:31 -0700 | [diff] [blame] | 190 | goto cleanup1; |
Marco Nelissen | cd32a5d | 2016-05-20 09:14:36 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | } |
Dongwon Kang | 013a3b5 | 2017-07-13 18:41:31 -0700 | [diff] [blame] | 196 | cleanup1: |
| 197 | if(partword){ |
| 198 | for(j=0;j<ch;j++){ |
| 199 | if(partword[j])_ogg_free(partword[j]); |
| 200 | } |
| 201 | _ogg_free(partword); |
| 202 | } |
Gloria Wang | 7913073 | 2010-02-08 14:41:04 -0800 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | }else{ |
| 206 | int max=(pcmend*ch)>>1; |
| 207 | int end=(info->end<max?info->end:max); |
| 208 | int n=end-info->begin; |
| 209 | |
| 210 | if(n>0){ |
| 211 | int partvals=n/samples_per_partition; |
| 212 | int partwords=(partvals+partitions_per_word-1)/partitions_per_word; |
| 213 | |
| 214 | char *partword= |
Dongwon Kang | 013a3b5 | 2017-07-13 18:41:31 -0700 | [diff] [blame] | 215 | (char *)_ogg_malloc(partwords*partitions_per_word*sizeof(*partword)); |
| 216 | if(partword==NULL)goto cleanup2; |
Gloria Wang | 7913073 | 2010-02-08 14:41:04 -0800 | [diff] [blame] | 217 | int beginoff=info->begin/ch; |
| 218 | |
| 219 | for(i=0;i<ch;i++)if(nonzero[i])break; |
Dongwon Kang | 013a3b5 | 2017-07-13 18:41:31 -0700 | [diff] [blame] | 220 | if(i==ch)goto cleanup2; /* no nonzero vectors */ |
Gloria Wang | 7913073 | 2010-02-08 14:41:04 -0800 | [diff] [blame] | 221 | |
| 222 | samples_per_partition/=ch; |
| 223 | |
| 224 | for(s=0;s<info->stages;s++){ |
Marco Nelissen | fd877e9 | 2017-02-01 15:59:53 -0800 | [diff] [blame] | 225 | for(i=0;i<partvals;){ |
Gloria Wang | 7913073 | 2010-02-08 14:41:04 -0800 | [diff] [blame] | 226 | |
Marco Nelissen | fd877e9 | 2017-02-01 15:59:53 -0800 | [diff] [blame] | 227 | if(s==0){ |
| 228 | int temp; |
| 229 | partword[i+partitions_per_word-1]=1; |
| 230 | for(k=partitions_per_word-2;k>=0;k--) |
| 231 | partword[i+k]=partword[i+k+1]*info->partitions; |
Gloria Wang | 7913073 | 2010-02-08 14:41:04 -0800 | [diff] [blame] | 232 | |
Marco Nelissen | fd877e9 | 2017-02-01 15:59:53 -0800 | [diff] [blame] | 233 | /* fetch the partition word */ |
| 234 | temp=vorbis_book_decode(phrasebook,&vd->opb); |
Dongwon Kang | 013a3b5 | 2017-07-13 18:41:31 -0700 | [diff] [blame] | 235 | if(temp==-1)goto cleanup2; |
Gloria Wang | 7913073 | 2010-02-08 14:41:04 -0800 | [diff] [blame] | 236 | |
Marco Nelissen | fd877e9 | 2017-02-01 15:59:53 -0800 | [diff] [blame] | 237 | /* this can be done quickly in assembly due to the quotient |
| 238 | always being at most six bits */ |
| 239 | for(k=0;k<partitions_per_word;k++){ |
| 240 | ogg_uint32_t div=partword[i+k]; |
| 241 | partword[i+k]= (div == 0) ? 0 : (temp / div); |
| 242 | temp-=partword[i+k]*div; |
| 243 | } |
| 244 | } |
Gloria Wang | 7913073 | 2010-02-08 14:41:04 -0800 | [diff] [blame] | 245 | |
Marco Nelissen | fd877e9 | 2017-02-01 15:59:53 -0800 | [diff] [blame] | 246 | /* now we decode residual values for the partitions */ |
| 247 | for(k=0;k<partitions_per_word && i<partvals;k++,i++){ |
| 248 | if(partword[i] >= 0 && partword[i] < info->partitions && |
| 249 | (info->stagemasks[(int)partword[i]] & (1 << s))){ |
| 250 | codebook *stagebook=ci->book_param+ |
| 251 | info->stagebooks[(partword[i]<<3)+s]; |
| 252 | if(vorbis_book_decodevv_add(stagebook,in, |
| 253 | i*samples_per_partition+beginoff,ch, |
| 254 | &vd->opb, |
| 255 | samples_per_partition,-8)==-1) |
Dongwon Kang | 013a3b5 | 2017-07-13 18:41:31 -0700 | [diff] [blame] | 256 | goto cleanup2; |
Marco Nelissen | fd877e9 | 2017-02-01 15:59:53 -0800 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | } |
Gloria Wang | 7913073 | 2010-02-08 14:41:04 -0800 | [diff] [blame] | 260 | } |
Dongwon Kang | 013a3b5 | 2017-07-13 18:41:31 -0700 | [diff] [blame] | 261 | cleanup2: |
| 262 | if(partword)_ogg_free(partword); |
Gloria Wang | 7913073 | 2010-02-08 14:41:04 -0800 | [diff] [blame] | 263 | } |
| 264 | } |
Gloria Wang | 7913073 | 2010-02-08 14:41:04 -0800 | [diff] [blame] | 265 | |
| 266 | return 0; |
| 267 | } |