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