media/libtremor/lib/tremor_mapping0.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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: channel mapping 0 implementation
michael@0 15
michael@0 16 ********************************************************************/
michael@0 17
michael@0 18 #include <stdlib.h>
michael@0 19 #include <stdio.h>
michael@0 20 #include <string.h>
michael@0 21 #include <math.h>
michael@0 22 #include <ogg/ogg.h>
michael@0 23 #include "ivorbiscodec.h"
michael@0 24 #include "mdct.h"
michael@0 25 #include "codec_internal.h"
michael@0 26 #include "codebook.h"
michael@0 27 #include "window.h"
michael@0 28 #include "registry.h"
michael@0 29 #include "misc.h"
michael@0 30
michael@0 31 /* simplistic, wasteful way of doing this (unique lookup for each
michael@0 32 mode/submapping); there should be a central repository for
michael@0 33 identical lookups. That will require minor work, so I'm putting it
michael@0 34 off as low priority.
michael@0 35
michael@0 36 Why a lookup for each backend in a given mode? Because the
michael@0 37 blocksize is set by the mode, and low backend lookups may require
michael@0 38 parameters from other areas of the mode/mapping */
michael@0 39
michael@0 40 typedef struct {
michael@0 41 vorbis_info_mode *mode;
michael@0 42 vorbis_info_mapping0 *map;
michael@0 43
michael@0 44 vorbis_look_floor **floor_look;
michael@0 45
michael@0 46 vorbis_look_residue **residue_look;
michael@0 47
michael@0 48 vorbis_func_floor **floor_func;
michael@0 49 vorbis_func_residue **residue_func;
michael@0 50
michael@0 51 int ch;
michael@0 52 long lastframe; /* if a different mode is called, we need to
michael@0 53 invalidate decay */
michael@0 54 } vorbis_look_mapping0;
michael@0 55
michael@0 56 static void mapping0_free_info(vorbis_info_mapping *i){
michael@0 57 vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)i;
michael@0 58 if(info){
michael@0 59 memset(info,0,sizeof(*info));
michael@0 60 _ogg_free(info);
michael@0 61 }
michael@0 62 }
michael@0 63
michael@0 64 static void mapping0_free_look(vorbis_look_mapping *look){
michael@0 65 int i;
michael@0 66 vorbis_look_mapping0 *l=(vorbis_look_mapping0 *)look;
michael@0 67 if(l){
michael@0 68
michael@0 69 for(i=0;i<l->map->submaps;i++){
michael@0 70 l->floor_func[i]->free_look(l->floor_look[i]);
michael@0 71 l->residue_func[i]->free_look(l->residue_look[i]);
michael@0 72 }
michael@0 73
michael@0 74 _ogg_free(l->floor_func);
michael@0 75 _ogg_free(l->residue_func);
michael@0 76 _ogg_free(l->floor_look);
michael@0 77 _ogg_free(l->residue_look);
michael@0 78 memset(l,0,sizeof(*l));
michael@0 79 _ogg_free(l);
michael@0 80 }
michael@0 81 }
michael@0 82
michael@0 83 static vorbis_look_mapping *mapping0_look(vorbis_dsp_state *vd,vorbis_info_mode *vm,
michael@0 84 vorbis_info_mapping *m){
michael@0 85 int i;
michael@0 86 vorbis_info *vi=vd->vi;
michael@0 87 codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
michael@0 88 vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)_ogg_calloc(1,sizeof(*look));
michael@0 89 vorbis_info_mapping0 *info=look->map=(vorbis_info_mapping0 *)m;
michael@0 90 look->mode=vm;
michael@0 91
michael@0 92 look->floor_look=(vorbis_look_floor **)_ogg_calloc(info->submaps,sizeof(*look->floor_look));
michael@0 93
michael@0 94 look->residue_look=(vorbis_look_residue **)_ogg_calloc(info->submaps,sizeof(*look->residue_look));
michael@0 95
michael@0 96 look->floor_func=(vorbis_func_floor **)_ogg_calloc(info->submaps,sizeof(*look->floor_func));
michael@0 97 look->residue_func=(vorbis_func_residue **)_ogg_calloc(info->submaps,sizeof(*look->residue_func));
michael@0 98
michael@0 99 for(i=0;i<info->submaps;i++){
michael@0 100 int floornum=info->floorsubmap[i];
michael@0 101 int resnum=info->residuesubmap[i];
michael@0 102
michael@0 103 look->floor_func[i]=_floor_P[ci->floor_type[floornum]];
michael@0 104 look->floor_look[i]=look->floor_func[i]->
michael@0 105 look(vd,vm,ci->floor_param[floornum]);
michael@0 106 look->residue_func[i]=_residue_P[ci->residue_type[resnum]];
michael@0 107 look->residue_look[i]=look->residue_func[i]->
michael@0 108 look(vd,vm,ci->residue_param[resnum]);
michael@0 109
michael@0 110 }
michael@0 111
michael@0 112 look->ch=vi->channels;
michael@0 113
michael@0 114 return(look);
michael@0 115 }
michael@0 116
michael@0 117 static int ilog(unsigned int v){
michael@0 118 int ret=0;
michael@0 119 if(v)--v;
michael@0 120 while(v){
michael@0 121 ret++;
michael@0 122 v>>=1;
michael@0 123 }
michael@0 124 return(ret);
michael@0 125 }
michael@0 126
michael@0 127 /* also responsible for range checking */
michael@0 128 static vorbis_info_mapping *mapping0_unpack(vorbis_info *vi,oggpack_buffer *opb){
michael@0 129 int i,b;
michael@0 130 vorbis_info_mapping0 *info=(vorbis_info_mapping0 *)_ogg_calloc(1,sizeof(*info));
michael@0 131 codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
michael@0 132 memset(info,0,sizeof(*info));
michael@0 133
michael@0 134 b=oggpack_read(opb,1);
michael@0 135 if(b<0)goto err_out;
michael@0 136 if(b){
michael@0 137 info->submaps=oggpack_read(opb,4)+1;
michael@0 138 if(info->submaps<=0)goto err_out;
michael@0 139 }else
michael@0 140 info->submaps=1;
michael@0 141
michael@0 142 b=oggpack_read(opb,1);
michael@0 143 if(b<0)goto err_out;
michael@0 144 if(b){
michael@0 145 info->coupling_steps=oggpack_read(opb,8)+1;
michael@0 146 if(info->coupling_steps<=0)goto err_out;
michael@0 147 for(i=0;i<info->coupling_steps;i++){
michael@0 148 int testM=info->coupling_mag[i]=oggpack_read(opb,ilog(vi->channels));
michael@0 149 int testA=info->coupling_ang[i]=oggpack_read(opb,ilog(vi->channels));
michael@0 150
michael@0 151 if(testM<0 ||
michael@0 152 testA<0 ||
michael@0 153 testM==testA ||
michael@0 154 testM>=vi->channels ||
michael@0 155 testA>=vi->channels) goto err_out;
michael@0 156 }
michael@0 157
michael@0 158 }
michael@0 159
michael@0 160 if(oggpack_read(opb,2)!=0)goto err_out; /* 2,3:reserved */
michael@0 161
michael@0 162 if(info->submaps>1){
michael@0 163 for(i=0;i<vi->channels;i++){
michael@0 164 info->chmuxlist[i]=oggpack_read(opb,4);
michael@0 165 if(info->chmuxlist[i]>=info->submaps || info->chmuxlist[i]<0)goto err_out;
michael@0 166 }
michael@0 167 }
michael@0 168 for(i=0;i<info->submaps;i++){
michael@0 169 int temp=oggpack_read(opb,8);
michael@0 170 if(temp>=ci->times)goto err_out;
michael@0 171 info->floorsubmap[i]=oggpack_read(opb,8);
michael@0 172 if(info->floorsubmap[i]>=ci->floors || info->floorsubmap[i]<0)goto err_out;
michael@0 173 info->residuesubmap[i]=oggpack_read(opb,8);
michael@0 174 if(info->residuesubmap[i]>=ci->residues || info->residuesubmap[i]<0)
michael@0 175 goto err_out;
michael@0 176 }
michael@0 177
michael@0 178 return info;
michael@0 179
michael@0 180 err_out:
michael@0 181 mapping0_free_info(info);
michael@0 182 return(NULL);
michael@0 183 }
michael@0 184
michael@0 185 static int seq=0;
michael@0 186 static int mapping0_inverse(vorbis_block *vb,vorbis_look_mapping *l){
michael@0 187 vorbis_dsp_state *vd=vb->vd;
michael@0 188 vorbis_info *vi=vd->vi;
michael@0 189 codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
michael@0 190 private_state *b=(private_state *)vd->backend_state;
michael@0 191 vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l;
michael@0 192 vorbis_info_mapping0 *info=look->map;
michael@0 193
michael@0 194 int i,j;
michael@0 195 long n=vb->pcmend=ci->blocksizes[vb->W];
michael@0 196
michael@0 197 ogg_int32_t **pcmbundle=(ogg_int32_t **)alloca(sizeof(*pcmbundle)*vi->channels);
michael@0 198 int *zerobundle=(int *)alloca(sizeof(*zerobundle)*vi->channels);
michael@0 199
michael@0 200 int *nonzero =(int *)alloca(sizeof(*nonzero)*vi->channels);
michael@0 201 void **floormemo=(void **)alloca(sizeof(*floormemo)*vi->channels);
michael@0 202
michael@0 203 /* time domain information decode (note that applying the
michael@0 204 information would have to happen later; we'll probably add a
michael@0 205 function entry to the harness for that later */
michael@0 206 /* NOT IMPLEMENTED */
michael@0 207
michael@0 208 /* recover the spectral envelope; store it in the PCM vector for now */
michael@0 209 for(i=0;i<vi->channels;i++){
michael@0 210 int submap=info->chmuxlist[i];
michael@0 211 floormemo[i]=look->floor_func[submap]->
michael@0 212 inverse1(vb,look->floor_look[submap]);
michael@0 213 if(floormemo[i])
michael@0 214 nonzero[i]=1;
michael@0 215 else
michael@0 216 nonzero[i]=0;
michael@0 217 memset(vb->pcm[i],0,sizeof(*vb->pcm[i])*n/2);
michael@0 218 }
michael@0 219
michael@0 220 /* channel coupling can 'dirty' the nonzero listing */
michael@0 221 for(i=0;i<info->coupling_steps;i++){
michael@0 222 if(nonzero[info->coupling_mag[i]] ||
michael@0 223 nonzero[info->coupling_ang[i]]){
michael@0 224 nonzero[info->coupling_mag[i]]=1;
michael@0 225 nonzero[info->coupling_ang[i]]=1;
michael@0 226 }
michael@0 227 }
michael@0 228
michael@0 229 /* recover the residue into our working vectors */
michael@0 230 for(i=0;i<info->submaps;i++){
michael@0 231 int ch_in_bundle=0;
michael@0 232 for(j=0;j<vi->channels;j++){
michael@0 233 if(info->chmuxlist[j]==i){
michael@0 234 if(nonzero[j])
michael@0 235 zerobundle[ch_in_bundle]=1;
michael@0 236 else
michael@0 237 zerobundle[ch_in_bundle]=0;
michael@0 238 pcmbundle[ch_in_bundle++]=vb->pcm[j];
michael@0 239 }
michael@0 240 }
michael@0 241
michael@0 242 look->residue_func[i]->inverse(vb,look->residue_look[i],
michael@0 243 pcmbundle,zerobundle,ch_in_bundle);
michael@0 244 }
michael@0 245
michael@0 246 //for(j=0;j<vi->channels;j++)
michael@0 247 //_analysis_output("coupled",seq+j,vb->pcm[j],-8,n/2,0,0);
michael@0 248
michael@0 249
michael@0 250 /* channel coupling */
michael@0 251 for(i=info->coupling_steps-1;i>=0;i--){
michael@0 252 ogg_int32_t *pcmM=vb->pcm[info->coupling_mag[i]];
michael@0 253 ogg_int32_t *pcmA=vb->pcm[info->coupling_ang[i]];
michael@0 254
michael@0 255 for(j=0;j<n/2;j++){
michael@0 256 ogg_int32_t mag=pcmM[j];
michael@0 257 ogg_int32_t ang=pcmA[j];
michael@0 258
michael@0 259 if(mag>0)
michael@0 260 if(ang>0){
michael@0 261 pcmM[j]=mag;
michael@0 262 pcmA[j]=mag-ang;
michael@0 263 }else{
michael@0 264 pcmA[j]=mag;
michael@0 265 pcmM[j]=mag+ang;
michael@0 266 }
michael@0 267 else
michael@0 268 if(ang>0){
michael@0 269 pcmM[j]=mag;
michael@0 270 pcmA[j]=mag+ang;
michael@0 271 }else{
michael@0 272 pcmA[j]=mag;
michael@0 273 pcmM[j]=mag-ang;
michael@0 274 }
michael@0 275 }
michael@0 276 }
michael@0 277
michael@0 278 //for(j=0;j<vi->channels;j++)
michael@0 279 //_analysis_output("residue",seq+j,vb->pcm[j],-8,n/2,0,0);
michael@0 280
michael@0 281 /* compute and apply spectral envelope */
michael@0 282 for(i=0;i<vi->channels;i++){
michael@0 283 ogg_int32_t *pcm=vb->pcm[i];
michael@0 284 int submap=info->chmuxlist[i];
michael@0 285 look->floor_func[submap]->
michael@0 286 inverse2(vb,look->floor_look[submap],floormemo[i],pcm);
michael@0 287 }
michael@0 288
michael@0 289 //for(j=0;j<vi->channels;j++)
michael@0 290 //_analysis_output("mdct",seq+j,vb->pcm[j],-24,n/2,0,1);
michael@0 291
michael@0 292 /* transform the PCM data; takes PCM vector, vb; modifies PCM vector */
michael@0 293 /* only MDCT right now.... */
michael@0 294 for(i=0;i<vi->channels;i++){
michael@0 295 ogg_int32_t *pcm=vb->pcm[i];
michael@0 296 mdct_backward(n,pcm,pcm);
michael@0 297 }
michael@0 298
michael@0 299 //for(j=0;j<vi->channels;j++)
michael@0 300 //_analysis_output("imdct",seq+j,vb->pcm[j],-24,n,0,0);
michael@0 301
michael@0 302 /* window the data */
michael@0 303 for(i=0;i<vi->channels;i++){
michael@0 304 ogg_int32_t *pcm=vb->pcm[i];
michael@0 305 if(nonzero[i])
michael@0 306 _vorbis_apply_window(pcm,b->window,ci->blocksizes,vb->lW,vb->W,vb->nW);
michael@0 307 else
michael@0 308 for(j=0;j<n;j++)
michael@0 309 pcm[j]=0;
michael@0 310
michael@0 311 }
michael@0 312
michael@0 313 //for(j=0;j<vi->channels;j++)
michael@0 314 //_analysis_output("window",seq+j,vb->pcm[j],-24,n,0,0);
michael@0 315
michael@0 316 seq+=vi->channels;
michael@0 317 /* all done! */
michael@0 318 return(0);
michael@0 319 }
michael@0 320
michael@0 321 /* export hooks */
michael@0 322 vorbis_func_mapping mapping0_exportbundle={
michael@0 323 &mapping0_unpack,
michael@0 324 &mapping0_look,
michael@0 325 &mapping0_free_info,
michael@0 326 &mapping0_free_look,
michael@0 327 &mapping0_inverse
michael@0 328 };

mercurial