📄 block.c
字号:
/********************************************************************
* *
* THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
* USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
* GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
* IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
* *
* THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2002 *
* by the XIPHOPHORUS Company http://www.xiph.org/ *
* *
********************************************************************
function: PCM data vector blocking, windowing and dis/reassembly
last mod: $Id: block.c,v 1.72 2003/03/06 22:05:26 xiphmont Exp $
Handle windowing, overlap-add, etc of the PCM vectors. This is made
more amusing by Vorbis' current two allowed block sizes.
********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ogg/ogg.h>
#include "vorbis/codec.h"
#include "codec_internal.h"
#include "window.h"
#include "mdct.h"
#include "lpc.h"
#include "registry.h"
#include "misc.h"
static int ilog2(unsigned int v){
int ret=0;
if(v)--v;
while(v){
ret++;
v>>=1;
}
return(ret);
}
/* pcm accumulator examples (not exhaustive):
<-------------- lW ---------------->
<--------------- W ---------------->
: .....|..... _______________ |
: .''' | '''_--- | |\ |
:.....''' |_____--- '''......| | \_______|
:.................|__________________|_______|__|______|
|<------ Sl ------>| > Sr < |endW
|beginSl |endSl | |endSr
|beginW |endlW |beginSr
|< lW >|
<--------------- W ---------------->
| | .. ______________ |
| | ' `/ | ---_ |
|___.'___/`. | ---_____|
|_______|__|_______|_________________|
| >|Sl|< |<------ Sr ----->|endW
| | |endSl |beginSr |endSr
|beginW | |endlW
mult[0] |beginSl mult[n]
<-------------- lW ----------------->
|<--W-->|
: .............. ___ | |
: .''' |`/ \ | |
:.....''' |/`....\|...|
:.........................|___|___|___|
|Sl |Sr |endW
| | |endSr
| |beginSr
| |endSl
|beginSl
|beginW
*/
/* block abstraction setup *********************************************/
#ifndef WORD_ALIGN
#define WORD_ALIGN 8
#endif
int vorbis_block_init(vorbis_dsp_state *v, vorbis_block *vb){
memset(vb,0,sizeof(*vb));
vb->vd=v;
vb->localalloc=0;
vb->localstore=NULL;
return(0);
}
void *_vorbis_block_alloc(vorbis_block *vb,ogg_int32_t bytes){
bytes=(bytes+(WORD_ALIGN-1)) & ~(WORD_ALIGN-1);
if(bytes+vb->localtop>vb->localalloc){
/* can't just _ogg_realloc... there are outstanding pointers */
if(vb->localstore){
struct alloc_chain *link=_ogg_malloc(sizeof(*link));
vb->totaluse+=vb->localtop;
link->next=vb->reap;
link->ptr=vb->localstore;
vb->reap=link;
}
/* highly conservative */
vb->localalloc=bytes;
vb->localstore=_ogg_malloc(vb->localalloc);
vb->localtop=0;
}
{
void *ret=(void *)(((char *)vb->localstore)+vb->localtop);
vb->localtop+=bytes;
return ret;
}
}
/* reap the chain, pull the ripcord */
void _vorbis_block_ripcord(vorbis_block *vb){
/* reap the chain */
struct alloc_chain *reap=vb->reap;
while(reap){
struct alloc_chain *next=reap->next;
_ogg_free(reap->ptr);
memset(reap,0,sizeof(*reap));
_ogg_free(reap);
reap=next;
}
/* consolidate storage */
if(vb->totaluse){
vb->localstore=_ogg_realloc(vb->localstore,vb->totaluse+vb->localalloc);
vb->localalloc+=vb->totaluse;
vb->totaluse=0;
}
/* pull the ripcord */
vb->localtop=0;
vb->reap=NULL;
}
int vorbis_block_clear(vorbis_block *vb){
_vorbis_block_ripcord(vb);
if(vb->localstore)_ogg_free(vb->localstore);
if(vb->internal)
_ogg_free(vb->internal);
memset(vb,0,sizeof(*vb));
return(0);
}
/* Analysis side code, but directly related to blocking. Thus it's
here and not in analysis.c (which is for analysis transforms only).
The init is here because some of it is shared */
static int _vds_shared_init(vorbis_dsp_state *v,vorbis_info *vi){
int i;
codec_setup_info *ci=vi->codec_setup;
private_state *b=NULL;
memset(v,0,sizeof(*v));
b=v->backend_state=_ogg_calloc(1,sizeof(*b));
v->vi=vi;
b->modebits=ilog2(ci->modes);
b->transform[0]=_ogg_calloc(VI_TRANSFORMB,sizeof(*b->transform[0]));
b->transform[1]=_ogg_calloc(VI_TRANSFORMB,sizeof(*b->transform[1]));
/* MDCT is tranform 0 */
b->transform[0][0]=_ogg_calloc(1,sizeof(mdct_lookup));
b->transform[1][0]=_ogg_calloc(1,sizeof(mdct_lookup));
mdct_init(b->transform[0][0],ci->blocksizes[0]);
mdct_init(b->transform[1][0],ci->blocksizes[1]);
/* Vorbis I uses only window type 0 */
b->window[0]=_vorbis_window_get(0,ci->blocksizes[0]/2);
b->window[1]=_vorbis_window_get(0,ci->blocksizes[1]/2);
{
/* finish the codebooks */
if(!ci->fullbooks){
ci->fullbooks=_ogg_calloc(ci->books,sizeof(*ci->fullbooks));
for(i=0;i<ci->books;i++){
vorbis_book_init_decode(ci->fullbooks+i,ci->book_param[i]);
/* decode codebooks are now standalone after init */
vorbis_staticbook_destroy(ci->book_param[i]);
ci->book_param[i]=NULL;
}
}
}
/* initialize the storage vectors. blocksize[1] is small for encode,
but the correct size for decode */
v->pcm_storage=ci->blocksizes[1];
v->pcm=_ogg_malloc(vi->channels*sizeof(*v->pcm));
v->pcmret=_ogg_malloc(vi->channels*sizeof(*v->pcmret));
{
int i;
for(i=0;i<vi->channels;i++)
v->pcm[i]=_ogg_calloc(v->pcm_storage,sizeof(*v->pcm[i]));
}
/* all 1 (large block) or 0 (small block) */
/* explicitly set for the sake of clarity */
v->lW=0; /* previous window size */
v->W=0; /* current window size */
/* all vector indexes */
v->centerW=ci->blocksizes[1]/2;
v->pcm_current=v->centerW;
/* initialize all the backend lookups */
b->flr=_ogg_calloc(ci->floors,sizeof(*b->flr));
b->residue=_ogg_calloc(ci->residues,sizeof(*b->residue));
for(i=0;i<ci->floors;i++)
b->flr[i]=_floor_P[ci->floor_type[i]]->
look(v,ci->floor_param[i]);
for(i=0;i<ci->residues;i++)
b->residue[i]=_residue_P[ci->residue_type[i]]->
look(v,ci->residue_param[i]);
return(0);
}
void vorbis_dsp_clear(vorbis_dsp_state *v){
int i;
if(v){
vorbis_info *vi=v->vi;
codec_setup_info *ci=(vi?vi->codec_setup:NULL);
private_state *b=v->backend_state;
if(b){
if(b->ve){
_ve_envelope_clear(b->ve);
_ogg_free(b->ve);
}
if(b->transform[0]){
mdct_clear(b->transform[0][0]);
_ogg_free(b->transform[0][0]);
_ogg_free(b->transform[0]);
}
if(b->transform[1]){
mdct_clear(b->transform[1][0]);
_ogg_free(b->transform[1][0]);
_ogg_free(b->transform[1]);
}
if(b->flr){
for(i=0;i<ci->floors;i++)
_floor_P[ci->floor_type[i]]->
free_look(b->flr[i]);
_ogg_free(b->flr);
}
if(b->residue){
for(i=0;i<ci->residues;i++)
_residue_P[ci->residue_type[i]]->
free_look(b->residue[i]);
_ogg_free(b->residue);
}
if(b->psy){
for(i=0;i<ci->psys;i++)
_vp_psy_clear(b->psy+i);
_ogg_free(b->psy);
}
if(b->psy_g_look)_vp_global_free(b->psy_g_look);
// vorbis_bitrate_clear(&b->bms);
drft_clear(&b->fft_look[0]);
drft_clear(&b->fft_look[1]);
}
if(v->pcm){
for(i=0;i<vi->channels;i++)
if(v->pcm[i])_ogg_free(v->pcm[i]);
_ogg_free(v->pcm);
if(v->pcmret)_ogg_free(v->pcmret);
}
if(b){
/* free header, header1, header2 */
if(b->header)_ogg_free(b->header);
if(b->header1)_ogg_free(b->header1);
if(b->header2)_ogg_free(b->header2);
_ogg_free(b);
}
memset(v,0,sizeof(*v));
}
}
float **vorbis_analysis_buffer(vorbis_dsp_state *v, int vals){
int i;
vorbis_info *vi=v->vi;
private_state *b=v->backend_state;
/* free header, header1, header2 */
if(b->header)_ogg_free(b->header);b->header=NULL;
if(b->header1)_ogg_free(b->header1);b->header1=NULL;
if(b->header2)_ogg_free(b->header2);b->header2=NULL;
/* Do we have enough storage space for the requested buffer? If not,
expand the PCM (and envelope) storage */
if(v->pcm_current+vals>=v->pcm_storage){
v->pcm_storage=v->pcm_current+vals*2;
for(i=0;i<vi->channels;i++){
v->pcm[i]=_ogg_realloc(v->pcm[i],v->pcm_storage*sizeof(*v->pcm[i]));
}
}
for(i=0;i<vi->channels;i++)
v->pcmret[i]=v->pcm[i]+v->pcm_current;
return(v->pcmret);
}
static void _preextrapolate_helper(vorbis_dsp_state *v){
int i;
int order=32;
float *lpc=alloca(order*sizeof(*lpc));
float *work=alloca(v->pcm_current*sizeof(*work));
ogg_int32_t j;
v->preextrapolate=1;
if(v->pcm_current-v->centerW>order*2){ /* safety */
for(i=0;i<v->vi->channels;i++){
/* need to run the extrapolation in reverse! */
for(j=0;j<v->pcm_current;j++)
work[j]=v->pcm[i][v->pcm_current-j-1];
/* prime as above */
vorbis_lpc_from_data(work,lpc,v->pcm_current-v->centerW,order);
/* run the predictor filter */
vorbis_lpc_predict(lpc,work+v->pcm_current-v->centerW-order,
order,
work+v->pcm_current-v->centerW,
v->centerW);
for(j=0;j<v->pcm_current;j++)
v->pcm[i][v->pcm_current-j-1]=work[j];
}
}
}
/* call with val<=0 to set eof */
int vorbis_analysis_wrote(vorbis_dsp_state *v, int vals){
vorbis_info *vi=v->vi;
codec_setup_info *ci=vi->codec_setup;
if(vals<=0){
int order=32;
int i;
float *lpc=alloca(order*sizeof(*lpc));
/* if it wasn't done earlier (very short sample) */
if(!v->preextrapolate)
_preextrapolate_helper(v);
/* We're encoding the end of the stream. Just make sure we have
[at least] a few full blocks of zeroes at the end. */
/* actually, we don't want zeroes; that could drop a large
amplitude off a cliff, creating spread spectrum noise that will
suck to encode. Extrapolate for the sake of cleanliness. */
vorbis_analysis_buffer(v,ci->blocksizes[1]*3);
v->eofflag=v->pcm_current;
v->pcm_current+=ci->blocksizes[1]*3;
for(i=0;i<vi->channels;i++){
if(v->eofflag>order*2){
/* extrapolate with LPC to fill in */
ogg_int32_t n;
/* make a predictor filter */
n=v->eofflag;
if(n>ci->blocksizes[1])n=ci->blocksizes[1];
vorbis_lpc_from_data(v->pcm[i]+v->eofflag-n,lpc,n,order);
/* run the predictor filter */
vorbis_lpc_predict(lpc,v->pcm[i]+v->eofflag-order,order,
v->pcm[i]+v->eofflag,v->pcm_current-v->eofflag);
}else{
/* not enough data to extrapolate (unlikely to happen due to
guarding the overlap, but bulletproof in case that
assumtion goes away). zeroes will do. */
memset(v->pcm[i]+v->eofflag,0,
(v->pcm_current-v->eofflag)*sizeof(*v->pcm[i]));
}
}
}else{
if(v->pcm_current+vals>v->pcm_storage)
return(OV_EINVAL);
v->pcm_current+=vals;
/* we may want to reverse extrapolate the beginning of a stream
too... in case we're beginning on a cliff! */
/* clumsy, but simple. It only runs once, so simple is good. */
if(!v->preextrapolate && v->pcm_current-v->centerW>ci->blocksizes[1])
_preextrapolate_helper(v);
}
return(0);
}
/* do the deltas, envelope shaping, pre-echo and determine the size of
the next block on which to continue analysis */
int vorbis_analysis_blockout(vorbis_dsp_state *v,vorbis_block *vb){
int i;
vorbis_info *vi=v->vi;
codec_setup_info *ci=vi->codec_setup;
private_state *b=v->backend_state;
vorbis_look_psy_global *g=b->psy_g_look;
ogg_int32_t beginW=v->centerW-ci->blocksizes[v->W]/2,centerNext;
vorbis_block_internal *vbi=(vorbis_block_internal *)vb->internal;
/* check to see if we're started... */
if(!v->preextrapolate)return(0);
/* check to see if we're done... */
if(v->eofflag==-1)return(0);
/* By our invariant, we have lW, W and centerW set. Search for
the next boundary so we can determine nW (the next window size)
which lets us compute the shape of the current block's window */
/* we do an envelope search even on a single blocksize; we may still
be throwing more bits at impulses, and envelope search handles
marking impulses too. */
{
ogg_int32_t bp=_ve_envelope_search(v);
if(bp==-1){
if(v->eofflag==0)return(0); /* not enough data currently to search for a
full ogg_int32_t block */
v->nW=0;
}else{
if(ci->blocksizes[0]==ci->blocksizes[1])
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -