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