rev |
line source |
nuclear@1
|
1 /********************************************************************
|
nuclear@1
|
2 * *
|
nuclear@1
|
3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
|
nuclear@1
|
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
|
nuclear@1
|
5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
|
nuclear@1
|
6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
|
nuclear@1
|
7 * *
|
nuclear@1
|
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 *
|
nuclear@1
|
9 * by the Xiph.Org Foundation http://www.xiph.org/ *
|
nuclear@1
|
10 * *
|
nuclear@1
|
11 ********************************************************************
|
nuclear@1
|
12
|
nuclear@1
|
13 function: floor backend 0 implementation
|
nuclear@1
|
14 last mod: $Id: floor0.c 18184 2012-02-03 20:55:12Z xiphmont $
|
nuclear@1
|
15
|
nuclear@1
|
16 ********************************************************************/
|
nuclear@1
|
17
|
nuclear@1
|
18 #include <stdlib.h>
|
nuclear@1
|
19 #include <string.h>
|
nuclear@1
|
20 #include <math.h>
|
nuclear@1
|
21 #include <ogg/ogg.h>
|
nuclear@1
|
22 #include "vorbis/codec.h"
|
nuclear@1
|
23 #include "codec_internal.h"
|
nuclear@1
|
24 #include "registry.h"
|
nuclear@1
|
25 #include "lpc.h"
|
nuclear@1
|
26 #include "lsp.h"
|
nuclear@1
|
27 #include "codebook.h"
|
nuclear@1
|
28 #include "scales.h"
|
nuclear@1
|
29 #include "misc.h"
|
nuclear@1
|
30 #include "os.h"
|
nuclear@1
|
31
|
nuclear@1
|
32 #include "misc.h"
|
nuclear@1
|
33 #include <stdio.h>
|
nuclear@1
|
34
|
nuclear@1
|
35 typedef struct {
|
nuclear@1
|
36 int ln;
|
nuclear@1
|
37 int m;
|
nuclear@1
|
38 int **linearmap;
|
nuclear@1
|
39 int n[2];
|
nuclear@1
|
40
|
nuclear@1
|
41 vorbis_info_floor0 *vi;
|
nuclear@1
|
42
|
nuclear@1
|
43 long bits;
|
nuclear@1
|
44 long frames;
|
nuclear@1
|
45 } vorbis_look_floor0;
|
nuclear@1
|
46
|
nuclear@1
|
47
|
nuclear@1
|
48 /***********************************************/
|
nuclear@1
|
49
|
nuclear@1
|
50 static void floor0_free_info(vorbis_info_floor *i){
|
nuclear@1
|
51 vorbis_info_floor0 *info=(vorbis_info_floor0 *)i;
|
nuclear@1
|
52 if(info){
|
nuclear@1
|
53 memset(info,0,sizeof(*info));
|
nuclear@1
|
54 _ogg_free(info);
|
nuclear@1
|
55 }
|
nuclear@1
|
56 }
|
nuclear@1
|
57
|
nuclear@1
|
58 static void floor0_free_look(vorbis_look_floor *i){
|
nuclear@1
|
59 vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
|
nuclear@1
|
60 if(look){
|
nuclear@1
|
61
|
nuclear@1
|
62 if(look->linearmap){
|
nuclear@1
|
63
|
nuclear@1
|
64 if(look->linearmap[0])_ogg_free(look->linearmap[0]);
|
nuclear@1
|
65 if(look->linearmap[1])_ogg_free(look->linearmap[1]);
|
nuclear@1
|
66
|
nuclear@1
|
67 _ogg_free(look->linearmap);
|
nuclear@1
|
68 }
|
nuclear@1
|
69 memset(look,0,sizeof(*look));
|
nuclear@1
|
70 _ogg_free(look);
|
nuclear@1
|
71 }
|
nuclear@1
|
72 }
|
nuclear@1
|
73
|
nuclear@1
|
74 static vorbis_info_floor *floor0_unpack (vorbis_info *vi,oggpack_buffer *opb){
|
nuclear@1
|
75 codec_setup_info *ci=vi->codec_setup;
|
nuclear@1
|
76 int j;
|
nuclear@1
|
77
|
nuclear@1
|
78 vorbis_info_floor0 *info=_ogg_malloc(sizeof(*info));
|
nuclear@1
|
79 info->order=oggpack_read(opb,8);
|
nuclear@1
|
80 info->rate=oggpack_read(opb,16);
|
nuclear@1
|
81 info->barkmap=oggpack_read(opb,16);
|
nuclear@1
|
82 info->ampbits=oggpack_read(opb,6);
|
nuclear@1
|
83 info->ampdB=oggpack_read(opb,8);
|
nuclear@1
|
84 info->numbooks=oggpack_read(opb,4)+1;
|
nuclear@1
|
85
|
nuclear@1
|
86 if(info->order<1)goto err_out;
|
nuclear@1
|
87 if(info->rate<1)goto err_out;
|
nuclear@1
|
88 if(info->barkmap<1)goto err_out;
|
nuclear@1
|
89 if(info->numbooks<1)goto err_out;
|
nuclear@1
|
90
|
nuclear@1
|
91 for(j=0;j<info->numbooks;j++){
|
nuclear@1
|
92 info->books[j]=oggpack_read(opb,8);
|
nuclear@1
|
93 if(info->books[j]<0 || info->books[j]>=ci->books)goto err_out;
|
nuclear@1
|
94 if(ci->book_param[info->books[j]]->maptype==0)goto err_out;
|
nuclear@1
|
95 if(ci->book_param[info->books[j]]->dim<1)goto err_out;
|
nuclear@1
|
96 }
|
nuclear@1
|
97 return(info);
|
nuclear@1
|
98
|
nuclear@1
|
99 err_out:
|
nuclear@1
|
100 floor0_free_info(info);
|
nuclear@1
|
101 return(NULL);
|
nuclear@1
|
102 }
|
nuclear@1
|
103
|
nuclear@1
|
104 /* initialize Bark scale and normalization lookups. We could do this
|
nuclear@1
|
105 with static tables, but Vorbis allows a number of possible
|
nuclear@1
|
106 combinations, so it's best to do it computationally.
|
nuclear@1
|
107
|
nuclear@1
|
108 The below is authoritative in terms of defining scale mapping.
|
nuclear@1
|
109 Note that the scale depends on the sampling rate as well as the
|
nuclear@1
|
110 linear block and mapping sizes */
|
nuclear@1
|
111
|
nuclear@1
|
112 static void floor0_map_lazy_init(vorbis_block *vb,
|
nuclear@1
|
113 vorbis_info_floor *infoX,
|
nuclear@1
|
114 vorbis_look_floor0 *look){
|
nuclear@1
|
115 if(!look->linearmap[vb->W]){
|
nuclear@1
|
116 vorbis_dsp_state *vd=vb->vd;
|
nuclear@1
|
117 vorbis_info *vi=vd->vi;
|
nuclear@1
|
118 codec_setup_info *ci=vi->codec_setup;
|
nuclear@1
|
119 vorbis_info_floor0 *info=(vorbis_info_floor0 *)infoX;
|
nuclear@1
|
120 int W=vb->W;
|
nuclear@1
|
121 int n=ci->blocksizes[W]/2,j;
|
nuclear@1
|
122
|
nuclear@1
|
123 /* we choose a scaling constant so that:
|
nuclear@1
|
124 floor(bark(rate/2-1)*C)=mapped-1
|
nuclear@1
|
125 floor(bark(rate/2)*C)=mapped */
|
nuclear@1
|
126 float scale=look->ln/toBARK(info->rate/2.f);
|
nuclear@1
|
127
|
nuclear@1
|
128 /* the mapping from a linear scale to a smaller bark scale is
|
nuclear@1
|
129 straightforward. We do *not* make sure that the linear mapping
|
nuclear@1
|
130 does not skip bark-scale bins; the decoder simply skips them and
|
nuclear@1
|
131 the encoder may do what it wishes in filling them. They're
|
nuclear@1
|
132 necessary in some mapping combinations to keep the scale spacing
|
nuclear@1
|
133 accurate */
|
nuclear@1
|
134 look->linearmap[W]=_ogg_malloc((n+1)*sizeof(**look->linearmap));
|
nuclear@1
|
135 for(j=0;j<n;j++){
|
nuclear@1
|
136 int val=floor( toBARK((info->rate/2.f)/n*j)
|
nuclear@1
|
137 *scale); /* bark numbers represent band edges */
|
nuclear@1
|
138 if(val>=look->ln)val=look->ln-1; /* guard against the approximation */
|
nuclear@1
|
139 look->linearmap[W][j]=val;
|
nuclear@1
|
140 }
|
nuclear@1
|
141 look->linearmap[W][j]=-1;
|
nuclear@1
|
142 look->n[W]=n;
|
nuclear@1
|
143 }
|
nuclear@1
|
144 }
|
nuclear@1
|
145
|
nuclear@1
|
146 static vorbis_look_floor *floor0_look(vorbis_dsp_state *vd,
|
nuclear@1
|
147 vorbis_info_floor *i){
|
nuclear@1
|
148 vorbis_info_floor0 *info=(vorbis_info_floor0 *)i;
|
nuclear@1
|
149 vorbis_look_floor0 *look=_ogg_calloc(1,sizeof(*look));
|
nuclear@1
|
150 look->m=info->order;
|
nuclear@1
|
151 look->ln=info->barkmap;
|
nuclear@1
|
152 look->vi=info;
|
nuclear@1
|
153
|
nuclear@1
|
154 look->linearmap=_ogg_calloc(2,sizeof(*look->linearmap));
|
nuclear@1
|
155
|
nuclear@1
|
156 return look;
|
nuclear@1
|
157 }
|
nuclear@1
|
158
|
nuclear@1
|
159 static void *floor0_inverse1(vorbis_block *vb,vorbis_look_floor *i){
|
nuclear@1
|
160 vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
|
nuclear@1
|
161 vorbis_info_floor0 *info=look->vi;
|
nuclear@1
|
162 int j,k;
|
nuclear@1
|
163
|
nuclear@1
|
164 int ampraw=oggpack_read(&vb->opb,info->ampbits);
|
nuclear@1
|
165 if(ampraw>0){ /* also handles the -1 out of data case */
|
nuclear@1
|
166 long maxval=(1<<info->ampbits)-1;
|
nuclear@1
|
167 float amp=(float)ampraw/maxval*info->ampdB;
|
nuclear@1
|
168 int booknum=oggpack_read(&vb->opb,_ilog(info->numbooks));
|
nuclear@1
|
169
|
nuclear@1
|
170 if(booknum!=-1 && booknum<info->numbooks){ /* be paranoid */
|
nuclear@1
|
171 codec_setup_info *ci=vb->vd->vi->codec_setup;
|
nuclear@1
|
172 codebook *b=ci->fullbooks+info->books[booknum];
|
nuclear@1
|
173 float last=0.f;
|
nuclear@1
|
174
|
nuclear@1
|
175 /* the additional b->dim is a guard against any possible stack
|
nuclear@1
|
176 smash; b->dim is provably more than we can overflow the
|
nuclear@1
|
177 vector */
|
nuclear@1
|
178 float *lsp=_vorbis_block_alloc(vb,sizeof(*lsp)*(look->m+b->dim+1));
|
nuclear@1
|
179
|
nuclear@1
|
180 if(vorbis_book_decodev_set(b,lsp,&vb->opb,look->m)==-1)goto eop;
|
nuclear@1
|
181 for(j=0;j<look->m;){
|
nuclear@1
|
182 for(k=0;j<look->m && k<b->dim;k++,j++)lsp[j]+=last;
|
nuclear@1
|
183 last=lsp[j-1];
|
nuclear@1
|
184 }
|
nuclear@1
|
185
|
nuclear@1
|
186 lsp[look->m]=amp;
|
nuclear@1
|
187 return(lsp);
|
nuclear@1
|
188 }
|
nuclear@1
|
189 }
|
nuclear@1
|
190 eop:
|
nuclear@1
|
191 return(NULL);
|
nuclear@1
|
192 }
|
nuclear@1
|
193
|
nuclear@1
|
194 static int floor0_inverse2(vorbis_block *vb,vorbis_look_floor *i,
|
nuclear@1
|
195 void *memo,float *out){
|
nuclear@1
|
196 vorbis_look_floor0 *look=(vorbis_look_floor0 *)i;
|
nuclear@1
|
197 vorbis_info_floor0 *info=look->vi;
|
nuclear@1
|
198
|
nuclear@1
|
199 floor0_map_lazy_init(vb,info,look);
|
nuclear@1
|
200
|
nuclear@1
|
201 if(memo){
|
nuclear@1
|
202 float *lsp=(float *)memo;
|
nuclear@1
|
203 float amp=lsp[look->m];
|
nuclear@1
|
204
|
nuclear@1
|
205 /* take the coefficients back to a spectral envelope curve */
|
nuclear@1
|
206 vorbis_lsp_to_curve(out,
|
nuclear@1
|
207 look->linearmap[vb->W],
|
nuclear@1
|
208 look->n[vb->W],
|
nuclear@1
|
209 look->ln,
|
nuclear@1
|
210 lsp,look->m,amp,(float)info->ampdB);
|
nuclear@1
|
211 return(1);
|
nuclear@1
|
212 }
|
nuclear@1
|
213 memset(out,0,sizeof(*out)*look->n[vb->W]);
|
nuclear@1
|
214 return(0);
|
nuclear@1
|
215 }
|
nuclear@1
|
216
|
nuclear@1
|
217 /* export hooks */
|
nuclear@1
|
218 const vorbis_func_floor floor0_exportbundle={
|
nuclear@1
|
219 NULL,&floor0_unpack,&floor0_look,&floor0_free_info,
|
nuclear@1
|
220 &floor0_free_look,&floor0_inverse1,&floor0_inverse2
|
nuclear@1
|
221 };
|