00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <stdlib.h>
00019 #include <stdio.h>
00020 #include <string.h>
00021 #include <math.h>
00022 #include "ogg.h"
00023 #include "ivorbiscodec.h"
00024 #include "mdct.h"
00025 #include "codec_internal.h"
00026 #include "codebook.h"
00027 #include "window.h"
00028 #include "registry.h"
00029 #include "misc.h"
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 typedef struct {
00041 vorbis_info_mode *mode;
00042 vorbis_info_mapping0 *map;
00043
00044 vorbis_look_floor **floor_look;
00045
00046 vorbis_look_residue **residue_look;
00047
00048 vorbis_func_floor **floor_func;
00049 vorbis_func_residue **residue_func;
00050
00051 int ch;
00052 long lastframe;
00053
00054 } vorbis_look_mapping0;
00055
00056 static void mapping0_free_info(vorbis_info_mapping *i){
00057 vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)i;
00058 if(info){
00059 memset(info,0,sizeof(*info));
00060 _ogg_free(info);
00061 }
00062 }
00063
00064 static void mapping0_free_look(vorbis_look_mapping *look){
00065 int i;
00066 vorbis_look_mapping0 *l=(vorbis_look_mapping0 *)look;
00067 if(l){
00068
00069 for(i=0;i<l->map->submaps;i++){
00070 l->floor_func[i]->free_look(l->floor_look[i]);
00071 l->residue_func[i]->free_look(l->residue_look[i]);
00072 }
00073
00074 _ogg_free(l->floor_func);
00075 _ogg_free(l->residue_func);
00076 _ogg_free(l->floor_look);
00077 _ogg_free(l->residue_look);
00078 memset(l,0,sizeof(*l));
00079 _ogg_free(l);
00080 }
00081 }
00082
00083 static vorbis_look_mapping *mapping0_look(vorbis_dsp_state *vd,vorbis_info_mode *vm,
00084 vorbis_info_mapping *m){
00085 int i;
00086 vorbis_info *vi=vd->vi;
00087 codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
00088 vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)_ogg_calloc(1,sizeof(*look));
00089 vorbis_info_mapping0 *info=look->map=(vorbis_info_mapping0 *)m;
00090 look->mode=vm;
00091
00092 look->floor_look=(vorbis_look_floor **)_ogg_calloc(info->submaps,sizeof(*look->floor_look));
00093
00094 look->residue_look=(vorbis_look_residue **)_ogg_calloc(info->submaps,sizeof(*look->residue_look));
00095
00096 look->floor_func=(vorbis_func_floor **)_ogg_calloc(info->submaps,sizeof(*look->floor_func));
00097 look->residue_func=(vorbis_func_residue **)_ogg_calloc(info->submaps,sizeof(*look->residue_func));
00098
00099 for(i=0;i<info->submaps;i++){
00100 int floornum=info->floorsubmap[i];
00101 int resnum=info->residuesubmap[i];
00102
00103 look->floor_func[i]=_floor_P[ci->floor_type[floornum]];
00104 look->floor_look[i]=look->floor_func[i]->
00105 look(vd,vm,ci->floor_param[floornum]);
00106 look->residue_func[i]=_residue_P[ci->residue_type[resnum]];
00107 look->residue_look[i]=look->residue_func[i]->
00108 look(vd,vm,ci->residue_param[resnum]);
00109
00110 }
00111
00112 look->ch=vi->channels;
00113
00114 return(look);
00115 }
00116
00117 static int ilog(unsigned int v){
00118 int ret=0;
00119 if(v)--v;
00120 while(v){
00121 ret++;
00122 v>>=1;
00123 }
00124 return(ret);
00125 }
00126
00127
00128 static vorbis_info_mapping *mapping0_unpack(vorbis_info *vi,oggpack_buffer *opb){
00129 int i;
00130 vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)_ogg_calloc(1,sizeof(*info));
00131 codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
00132 memset(info,0,sizeof(*info));
00133
00134 if(oggpack_read(opb,1))
00135 info->submaps=oggpack_read(opb,4)+1;
00136 else
00137 info->submaps=1;
00138
00139 if(oggpack_read(opb,1)){
00140 info->coupling_steps=oggpack_read(opb,8)+1;
00141
00142 for(i=0;i<info->coupling_steps;i++){
00143 int testM=info->coupling_mag[i]=oggpack_read(opb,ilog(vi->channels));
00144 int testA=info->coupling_ang[i]=oggpack_read(opb,ilog(vi->channels));
00145
00146 if(testM<0 ||
00147 testA<0 ||
00148 testM==testA ||
00149 testM>=vi->channels ||
00150 testA>=vi->channels) goto err_out;
00151 }
00152
00153 }
00154
00155 if(oggpack_read(opb,2)>0)goto err_out;
00156
00157 if(info->submaps>1){
00158 for(i=0;i<vi->channels;i++){
00159 info->chmuxlist[i]=oggpack_read(opb,4);
00160 if(info->chmuxlist[i]>=info->submaps)goto err_out;
00161 }
00162 }
00163 for(i=0;i<info->submaps;i++){
00164 int temp=oggpack_read(opb,8);
00165 if(temp>=ci->times)goto err_out;
00166 info->floorsubmap[i]=oggpack_read(opb,8);
00167 if(info->floorsubmap[i]>=ci->floors)goto err_out;
00168 info->residuesubmap[i]=oggpack_read(opb,8);
00169 if(info->residuesubmap[i]>=ci->residues)goto err_out;
00170 }
00171
00172 return info;
00173
00174 err_out:
00175 mapping0_free_info(info);
00176 return(NULL);
00177 }
00178
00179
00180 static int mapping0_inverse(vorbis_block *vb,vorbis_look_mapping *l){
00181 vorbis_dsp_state *vd=vb->vd;
00182 vorbis_info *vi=vd->vi;
00183 codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
00184 private_state *b=(private_state *)vd->backend_state;
00185 vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
00186 vorbis_info_mapping0 *info=look->map;
00187
00188 int i,j;
00189 long n=vb->pcmend=ci->blocksizes[vb->W];
00190
00191 ogg_int32_t **pcmbundle=(ogg_int32_t **)_ogg_malloc(sizeof(*pcmbundle)*vi->channels);
00192 int *zerobundle=(int *)_ogg_malloc(sizeof(*zerobundle)*vi->channels);
00193
00194 int *nonzero =(int *)_ogg_malloc(sizeof(*nonzero)*vi->channels);
00195 void **floormemo=(void **)_ogg_malloc(sizeof(*floormemo)*vi->channels);
00196
00197
00198
00199
00200
00201
00202
00203 for(i=0;i<vi->channels;i++){
00204 int submap=info->chmuxlist[i];
00205 floormemo[i]=look->floor_func[submap]->
00206 inverse1(vb,look->floor_look[submap]);
00207 if(floormemo[i])
00208 nonzero[i]=1;
00209 else
00210 nonzero[i]=0;
00211 memset(vb->pcm[i],0,sizeof(*vb->pcm[i])*n/2);
00212 }
00213
00214
00215 for(i=0;i<info->coupling_steps;i++){
00216 if(nonzero[info->coupling_mag[i]] ||
00217 nonzero[info->coupling_ang[i]]){
00218 nonzero[info->coupling_mag[i]]=1;
00219 nonzero[info->coupling_ang[i]]=1;
00220 }
00221 }
00222
00223
00224 for(i=0;i<info->submaps;i++){
00225 int ch_in_bundle=0;
00226 for(j=0;j<vi->channels;j++){
00227 if(info->chmuxlist[j]==i){
00228 if(nonzero[j])
00229 zerobundle[ch_in_bundle]=1;
00230 else
00231 zerobundle[ch_in_bundle]=0;
00232 pcmbundle[ch_in_bundle++]=vb->pcm[j];
00233 }
00234 }
00235
00236 look->residue_func[i]->inverse(vb,look->residue_look[i],
00237 pcmbundle,zerobundle,ch_in_bundle);
00238 }
00239
00240
00241
00242
00243
00244
00245 for(i=info->coupling_steps-1;i>=0;i--){
00246 ogg_int32_t *pcmM=vb->pcm[info->coupling_mag[i]];
00247 ogg_int32_t *pcmA=vb->pcm[info->coupling_ang[i]];
00248
00249 for(j=0;j<n/2;j++){
00250 ogg_int32_t mag=pcmM[j];
00251 ogg_int32_t ang=pcmA[j];
00252
00253 if(mag>0)
00254 if(ang>0){
00255 pcmM[j]=mag;
00256 pcmA[j]=mag-ang;
00257 }else{
00258 pcmA[j]=mag;
00259 pcmM[j]=mag+ang;
00260 }
00261 else
00262 if(ang>0){
00263 pcmM[j]=mag;
00264 pcmA[j]=mag+ang;
00265 }else{
00266 pcmA[j]=mag;
00267 pcmM[j]=mag-ang;
00268 }
00269 }
00270 }
00271
00272
00273
00274
00275
00276 for(i=0;i<vi->channels;i++){
00277 ogg_int32_t *pcm=vb->pcm[i];
00278 int submap=info->chmuxlist[i];
00279 look->floor_func[submap]->
00280 inverse2(vb,look->floor_look[submap],floormemo[i],pcm);
00281 }
00282
00283
00284
00285
00286
00287
00288 for(i=0;i<vi->channels;i++){
00289 ogg_int32_t *pcm=vb->pcm[i];
00290 mdct_backward(n,pcm,pcm);
00291 }
00292
00293
00294
00295
00296
00297 for(i=0;i<vi->channels;i++){
00298 ogg_int32_t *pcm=vb->pcm[i];
00299 if(nonzero[i])
00300 _vorbis_apply_window(pcm,b->window,ci->blocksizes,vb->lW,vb->W,vb->nW);
00301 else
00302 for(j=0;j<n;j++)
00303 pcm[j]=0;
00304
00305 }
00306
00307
00308
00309
00310
00311
00312 _ogg_free(pcmbundle);
00313 _ogg_free(zerobundle);
00314 _ogg_free(nonzero);
00315 _ogg_free(floormemo);
00316
00317 return(0);
00318 }
00319
00320
00321 const vorbis_func_mapping mapping0_exportbundle={
00322 &mapping0_unpack,
00323 &mapping0_look,
00324 &mapping0_free_info,
00325 &mapping0_free_look,
00326 &mapping0_inverse
00327 };