1 /********************************************************************
3 * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE. *
5 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
6 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
7 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
9 * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2002 *
10 * BY THE Xiph.Org FOUNDATION http://www.xiph.org/ *
12 ********************************************************************
14 function: PCM data vector blocking, windowing and dis/reassembly
16 ********************************************************************/
18 #include "config-tremor.h"
22 #include "ivorbiscodec.h"
23 #include "codec_internal.h"
29 static int ilog(unsigned int v
){
39 /* pcm accumulator examples (not exhaustive):
41 <-------------- lW ---------------->
42 <--------------- W ---------------->
43 : .....|..... _______________ |
44 : .''' | '''_--- | |\ |
45 :.....''' |_____--- '''......| | \_______|
46 :.................|__________________|_______|__|______|
47 |<------ Sl ------>| > Sr < |endW
48 |beginSl |endSl | |endSr
49 |beginW |endlW |beginSr
53 <--------------- W ---------------->
54 | | .. ______________ |
56 |___.'___/`. | ---_____|
57 |_______|__|_______|_________________|
58 | >|Sl|< |<------ Sr ----->|endW
59 | | |endSl |beginSr |endSr
61 mult[0] |beginSl mult[n]
63 <-------------- lW ----------------->
65 : .............. ___ | |
67 :.....''' |/`....\|...|
68 :.........................|___|___|___|
77 /* block abstraction setup *********************************************/
83 int vorbis_block_init(vorbis_dsp_state
*v
, vorbis_block
*vb
){
84 memset(vb
,0,sizeof(*vb
));
92 void *_vorbis_block_alloc(vorbis_block
*vb
,long bytes
){
93 bytes
=(bytes
+(WORD_ALIGN
-1)) & ~(WORD_ALIGN
-1);
94 if(bytes
+vb
->localtop
>vb
->localalloc
){
95 /* can't just _ogg_realloc... there are outstanding pointers */
97 struct alloc_chain
*link
=(struct alloc_chain
*)_ogg_malloc(sizeof(*link
));
98 vb
->totaluse
+=vb
->localtop
;
100 link
->ptr
=vb
->localstore
;
103 /* highly conservative */
104 vb
->localalloc
=bytes
;
105 vb
->localstore
=_ogg_malloc(vb
->localalloc
);
109 void *ret
=(void *)(((char *)vb
->localstore
)+vb
->localtop
);
115 /* reap the chain, pull the ripcord */
116 void _vorbis_block_ripcord(vorbis_block
*vb
){
118 struct alloc_chain
*reap
=vb
->reap
;
120 struct alloc_chain
*next
=reap
->next
;
121 _ogg_free(reap
->ptr
);
122 memset(reap
,0,sizeof(*reap
));
126 /* consolidate storage */
128 vb
->localstore
=_ogg_realloc(vb
->localstore
,vb
->totaluse
+vb
->localalloc
);
129 vb
->localalloc
+=vb
->totaluse
;
133 /* pull the ripcord */
138 int vorbis_block_clear(vorbis_block
*vb
){
139 _vorbis_block_ripcord(vb
);
140 if(vb
->localstore
)_ogg_free(vb
->localstore
);
142 memset(vb
,0,sizeof(*vb
));
146 static int _vds_init(vorbis_dsp_state
*v
,vorbis_info
*vi
){
148 codec_setup_info
*ci
=(codec_setup_info
*)vi
->codec_setup
;
149 private_state
*b
=NULL
;
151 memset(v
,0,sizeof(*v
));
152 b
=(private_state
*)(v
->backend_state
=_ogg_calloc(1,sizeof(*b
)));
155 b
->modebits
=ilog(ci
->modes
);
157 /* Vorbis I uses only window type 0 */
158 b
->window
[0]=_vorbis_window(0,ci
->blocksizes
[0]/2);
159 b
->window
[1]=_vorbis_window(0,ci
->blocksizes
[1]/2);
161 /* finish the codebooks */
163 ci
->fullbooks
=(codebook
*)_ogg_calloc(ci
->books
,sizeof(*ci
->fullbooks
));
164 for(i
=0;i
<ci
->books
;i
++){
165 vorbis_book_init_decode(ci
->fullbooks
+i
,ci
->book_param
[i
]);
166 /* decode codebooks are now standalone after init */
167 vorbis_staticbook_destroy(ci
->book_param
[i
]);
168 ci
->book_param
[i
]=NULL
;
172 v
->pcm_storage
=ci
->blocksizes
[1];
173 v
->pcm
=(ogg_int32_t
**)_ogg_malloc(vi
->channels
*sizeof(*v
->pcm
));
174 v
->pcmb
=(ogg_int32_t
**)_ogg_malloc(vi
->channels
*sizeof(*v
->pcmb
));
175 v
->pcmret
=(ogg_int32_t
**)_ogg_malloc(vi
->channels
*sizeof(*v
->pcmret
));
177 for(i
=0;i
<vi
->channels
;i
++)
178 v
->pcm
[i
]=(ogg_int32_t
*)_ogg_calloc(v
->pcm_storage
,sizeof(*v
->pcm
[i
]));
181 /* all 1 (large block) or 0 (small block) */
182 /* explicitly set for the sake of clarity */
183 v
->lW
=0; /* previous window size */
184 v
->W
=0; /* current window size */
186 /* initialize all the mapping/backend lookups */
187 b
->mode
=(vorbis_look_mapping
**)_ogg_calloc(ci
->modes
,sizeof(*b
->mode
));
188 for(i
=0;i
<ci
->modes
;i
++){
189 int mapnum
=ci
->mode_param
[i
]->mapping
;
190 int maptype
=ci
->map_type
[mapnum
];
191 b
->mode
[i
]=_mapping_P
[maptype
]->look(v
,ci
->mode_param
[i
],
192 ci
->map_param
[mapnum
]);
197 int vorbis_synthesis_restart(vorbis_dsp_state
*v
){
198 vorbis_info
*vi
=v
->vi
;
199 codec_setup_info
*ci
;
201 if(!v
->backend_state
)return -1;
206 v
->centerW
=ci
->blocksizes
[1]/2;
207 v
->pcm_current
=v
->centerW
;
212 ((private_state
*)(v
->backend_state
))->sample_count
=-1;
217 int vorbis_synthesis_init(vorbis_dsp_state
*v
,vorbis_info
*vi
){
219 vorbis_synthesis_restart(v
);
224 void vorbis_dsp_clear(vorbis_dsp_state
*v
){
227 vorbis_info
*vi
=v
->vi
;
228 codec_setup_info
*ci
=(codec_setup_info
*)(vi
?vi
->codec_setup
:NULL
);
229 private_state
*b
=(private_state
*)v
->backend_state
;
232 for(i
=0;i
<vi
->channels
;i
++)
233 if(v
->pcm
[i
])_ogg_free(v
->pcm
[i
]);
235 if(v
->pcmret
)_ogg_free(v
->pcmret
);
238 /* free mode lookups; these are actually vorbis_look_mapping structs */
240 for(i
=0;i
<ci
->modes
;i
++){
241 int mapnum
=ci
->mode_param
[i
]->mapping
;
242 int maptype
=ci
->map_type
[mapnum
];
243 if(b
&& b
->mode
)_mapping_P
[maptype
]->free_look(b
->mode
[i
]);
248 if(b
->mode
)_ogg_free(b
->mode
);
252 memset(v
,0,sizeof(*v
));
256 /* Unlike in analysis, the window is only partially applied for each
257 block. The time domain envelope is not yet handled at the point of
258 calling (as it relies on the previous block). */
260 int vorbis_synthesis_blockin(vorbis_dsp_state
*v
,vorbis_block
*vb
)
261 ICODE_ATTR_TREMOR_NOT_MDCT
;
262 int vorbis_synthesis_blockin(vorbis_dsp_state
*v
,vorbis_block
*vb
){
263 vorbis_info
*vi
=v
->vi
;
264 codec_setup_info
*ci
=(codec_setup_info
*)vi
->codec_setup
;
265 private_state
*b
=v
->backend_state
;
268 if(v
->pcm_current
>v
->pcm_returned
&& v
->pcm_returned
!=-1)return(OV_EINVAL
);
274 if((v
->sequence
==-1)||
275 (v
->sequence
+1 != vb
->sequence
)){
276 v
->granulepos
=-1; /* out of sequence; lose count */
280 v
->sequence
=vb
->sequence
;
282 if(vb
->pcm
){ /* no pcm to process if vorbis_synthesis_trackonly
283 was called on block */
284 int n
=ci
->blocksizes
[v
->W
]/2;
285 int n0
=ci
->blocksizes
[0]/2;
286 int n1
=ci
->blocksizes
[1]/2;
299 /* v->pcm is now used like a two-stage double buffer. We don't want
300 to have to constantly shift *or* adjust memory usage. Don't
301 accept a new block until the old is shifted out */
303 /* overlap/add PCM */
305 for(j
=0;j
<vi
->channels
;j
++){
306 /* the overlap/add section */
310 ogg_int32_t
*pcm
=v
->pcm
[j
]+prevCenter
;
311 ogg_int32_t
*p
=vb
->pcm
[j
];
312 vect_add(p
, pcm
, n1
);
316 ogg_int32_t
*pcm
=v
->pcm
[j
]+prevCenter
+n1
/2-n0
/2;
317 ogg_int32_t
*p
=vb
->pcm
[j
];
318 vect_add(pcm
, p
, n0
);
319 v
->pcmb
[j
]=v
->pcm
[j
]+prevCenter
;
324 ogg_int32_t
*pcm
=v
->pcm
[j
]+prevCenter
;
325 ogg_int32_t
*p
=vb
->pcm
[j
]+n1
/2-n0
/2;
326 vect_add(p
, pcm
, n0
);
330 ogg_int32_t
*pcm
=v
->pcm
[j
]+prevCenter
;
331 ogg_int32_t
*p
=vb
->pcm
[j
];
332 vect_add(p
, pcm
, n0
);
337 /* the copy section */
339 ogg_int32_t
*pcm
=v
->pcm
[j
]+thisCenter
;
340 ogg_int32_t
*p
=vb
->pcm
[j
]+n
;
341 vect_copy(pcm
, p
, n
);
350 /* deal with initial packet state; we do this using the explicit
351 pcm_returned==-1 flag otherwise we're sensitive to first block
352 being short or long */
354 if(v
->pcm_returned
==-1){
355 v
->pcm_returned
=thisCenter
;
356 v
->pcm_current
=thisCenter
;
359 v
->pcm_current
=ci
->blocksizes
[v
->lW
]/4+ci
->blocksizes
[v
->W
]/4;
364 /* track the frame number... This is for convenience, but also
365 making sure our last packet doesn't end with added padding. If
366 the last packet is partial, the number of samples we'll have to
367 return will be past the vb->granulepos.
369 This is not foolproof! It will be confused if we begin
370 decoding at the last page after a seek or hole. In that case,
371 we don't have a starting point to judge where the last frame
372 is. For this reason, vorbisfile will always try to make sure
373 it reads the last two marked pages in proper sequence */
375 if(b
->sample_count
==-1){
378 b
->sample_count
+=ci
->blocksizes
[v
->lW
]/4+ci
->blocksizes
[v
->W
]/4;
381 if(v
->granulepos
==-1){
382 if(vb
->granulepos
!=-1){ /* only set if we have a position to set to */
384 v
->granulepos
=vb
->granulepos
;
386 /* is this a short page? */
387 if(b
->sample_count
>v
->granulepos
){
388 /* corner case; if this is both the first and last audio page,
389 then spec says the end is cut, not beginning */
392 /* no preceeding granulepos; assume we started at zero (we'd
393 have to in a short single-page stream) */
394 /* granulepos could be -1 due to a seek, but that would result
395 in a long coun`t, not short count */
397 v
->pcm_current
-=(b
->sample_count
-v
->granulepos
);
399 /* trim the beginning */
400 v
->pcm_returned
+=(b
->sample_count
-v
->granulepos
);
401 if(v
->pcm_returned
>v
->pcm_current
)
402 v
->pcm_returned
=v
->pcm_current
;
409 v
->granulepos
+=ci
->blocksizes
[v
->lW
]/4+ci
->blocksizes
[v
->W
]/4;
410 if(vb
->granulepos
!=-1 && v
->granulepos
!=vb
->granulepos
){
412 if(v
->granulepos
>vb
->granulepos
){
413 long extra
=v
->granulepos
-vb
->granulepos
;
417 /* partial last frame. Strip the extra samples off */
418 v
->pcm_current
-=extra
;
419 } /* else {Shouldn't happen *unless* the bitstream is out of
420 spec. Either way, believe the bitstream } */
421 } /* else {Shouldn't happen *unless* the bitstream is out of
422 spec. Either way, believe the bitstream } */
423 v
->granulepos
=vb
->granulepos
;
427 /* Update, cleanup */
429 if(vb
->eofflag
)v
->eofflag
=1;
433 /* pcm==NULL indicates we just want the pending samples, no more */
434 int vorbis_synthesis_pcmout(vorbis_dsp_state
*v
,ogg_int32_t
***pcm
) ICODE_ATTR
;
435 int vorbis_synthesis_pcmout(vorbis_dsp_state
*v
,ogg_int32_t
***pcm
){
436 vorbis_info
*vi
=v
->vi
;
437 if(v
->pcm_returned
>-1 && v
->pcm_returned
<v
->pcm_current
){
440 for(i
=0;i
<vi
->channels
;i
++)
441 v
->pcmret
[i
]=v
->pcmb
[i
]+v
->pcm_returned
;
444 return(v
->pcm_current
-v
->pcm_returned
);
449 int vorbis_synthesis_read(vorbis_dsp_state
*v
,int bytes
){
450 if(bytes
&& v
->pcm_returned
+bytes
>v
->pcm_current
)return(OV_EINVAL
);
451 v
->pcm_returned
+=bytes
;