examples/sfexamples/oggvorbiscodec/src/tremor/mapping0.c

00001 /********************************************************************
00002  *                                                                  *
00003  * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE.   *
00004  *                                                                  *
00005  * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
00006  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
00007  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
00008  *                                                                  *
00009  * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2002    *
00010  * BY THE Xiph.Org FOUNDATION http://www.xiph.org/                  *
00011  *                                                                  *
00012  ********************************************************************
00013 
00014  function: channel mapping 0 implementation
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 /* simplistic, wasteful way of doing this (unique lookup for each
00032    mode/submapping); there should be a central repository for
00033    identical lookups.  That will require minor work, so I'm putting it
00034    off as low priority.
00035 
00036    Why a lookup for each backend in a given mode?  Because the
00037    blocksize is set by the mode, and low backend lookups may require
00038    parameters from other areas of the mode/mapping */
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; /* if a different mode is called, we need to 
00053                      invalidate decay */
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 /* also responsible for range checking */
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; /* 2,3:reserved */
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 //static int seq=0;
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   /* time domain information decode (note that applying the
00198      information would have to happen later; we'll probably add a
00199      function entry to the harness for that later */
00200   /* NOT IMPLEMENTED */
00201 
00202   /* recover the spectral envelope; store it in the PCM vector for now */
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   /* channel coupling can 'dirty' the nonzero listing */
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   /* recover the residue into our working vectors */
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   //for(j=0;j<vi->channels;j++)
00241   //_analysis_output("coupled",seq+j,vb->pcm[j],-8,n/2,0,0);
00242 
00243 
00244   /* channel coupling */
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   //for(j=0;j<vi->channels;j++)
00273   //_analysis_output("residue",seq+j,vb->pcm[j],-8,n/2,0,0);
00274 
00275   /* compute and apply spectral envelope */
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   //for(j=0;j<vi->channels;j++)
00284   //_analysis_output("mdct",seq+j,vb->pcm[j],-24,n/2,0,1);
00285 
00286   /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
00287   /* only MDCT right now.... */
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   //for(j=0;j<vi->channels;j++)
00294   //_analysis_output("imdct",seq+j,vb->pcm[j],-24,n,0,0);
00295 
00296   /* window the data */
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   //for(j=0;j<vi->channels;j++)
00308   //_analysis_output("window",seq+j,vb->pcm[j],-24,n,0,0);
00309 
00310 //  seq+=vi->channels;
00311   /* all done! */
00312   _ogg_free(pcmbundle);
00313   _ogg_free(zerobundle);
00314   _ogg_free(nonzero);
00315   _ogg_free(floormemo);
00316 
00317   return(0);
00318 }
00319 
00320 /* export hooks */
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 };

Generated by  doxygen 1.6.2