block.c 29.9 KB
Newer Older
Mike Whitson's avatar
Mike Whitson committed
1 2
/********************************************************************
 *                                                                  *
Monty's avatar
 
Monty committed
3
 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
Monty's avatar
 
Monty committed
4 5 6
 * 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.       *
Mike Whitson's avatar
Mike Whitson committed
7
 *                                                                  *
Ralph Giles's avatar
Ralph Giles committed
8
 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2015             *
9
 * by the Xiph.Org Foundation http://www.xiph.org/                  *
10
 *                                                                  *
Mike Whitson's avatar
Mike Whitson committed
11 12 13 14
 ********************************************************************

 function: PCM data vector blocking, windowing and dis/reassembly

Monty's avatar
 
Monty committed
15
 Handle windowing, overlap-add, etc of the PCM vectors.  This is made
Monty's avatar
 
Monty committed
16
 more amusing by Vorbis' current two allowed block sizes.
Monty's avatar
Monty committed
17

Mike Whitson's avatar
Mike Whitson committed
18 19
 ********************************************************************/

Monty's avatar
 
Monty committed
20
#include <stdio.h>
Mike Whitson's avatar
Mike Whitson committed
21
#include <stdlib.h>
Monty's avatar
 
Monty committed
22
#include <string.h>
Monty's avatar
 
Monty committed
23
#include <ogg/ogg.h>
Monty's avatar
 
Monty committed
24
#include "vorbis/codec.h"
Monty's avatar
 
Monty committed
25
#include "codec_internal.h"
Monty's avatar
 
Monty committed
26

Monty's avatar
 
Monty committed
27
#include "window.h"
Monty's avatar
 
Monty committed
28
#include "mdct.h"
Monty's avatar
 
Monty committed
29
#include "lpc.h"
Monty's avatar
 
Monty committed
30
#include "registry.h"
Monty's avatar
 
Monty committed
31
#include "misc.h"
Mike Whitson's avatar
Mike Whitson committed
32

Monty's avatar
 
Monty committed
33
/* pcm accumulator examples (not exhaustive):
Mike Whitson's avatar
Mike Whitson committed
34

Monty's avatar
 
Monty committed
35
 <-------------- lW ---------------->
Mike Whitson's avatar
Mike Whitson committed
36 37 38 39 40 41
                   <--------------- W ---------------->
:            .....|.....       _______________         |
:        .'''     |     '''_---      |       |\        |
:.....'''         |_____--- '''......|       | \_______|
:.................|__________________|_______|__|______|
                  |<------ Sl ------>|      > Sr <     |endW
Monty's avatar
Monty committed
42
                  |beginSl           |endSl  |  |endSr
Mike Whitson's avatar
Mike Whitson committed
43 44 45
                  |beginW            |endlW  |beginSr


Monty's avatar
Monty committed
46
                      |< lW >|
Mike Whitson's avatar
Mike Whitson committed
47 48 49
                   <--------------- W ---------------->
                  |   |  ..  ______________            |
                  |   | '  `/        |     ---_        |
Monty's avatar
Monty committed
50
                  |___.'___/`.       |         ---_____|
Mike Whitson's avatar
Mike Whitson committed
51 52 53
                  |_______|__|_______|_________________|
                  |      >|Sl|<      |<------ Sr ----->|endW
                  |       |  |endSl  |beginSr          |endSr
Monty's avatar
Monty committed
54
                  |beginW |  |endlW
Mike Whitson's avatar
Mike Whitson committed
55 56
                  mult[0] |beginSl                     mult[n]

Monty's avatar
 
Monty committed
57
 <-------------- lW ----------------->
Monty's avatar
Monty committed
58 59 60 61 62 63
                          |<--W-->|
:            ..............  ___  |   |
:        .'''             |`/   \ |   |
:.....'''                 |/`....\|...|
:.........................|___|___|___|
                          |Sl |Sr |endW
Monty's avatar
 
Monty committed
64 65 66
                          |   |   |endSr
                          |   |beginSr
                          |   |endSl
67 68
                          |beginSl
                          |beginW
Mike Whitson's avatar
Mike Whitson committed
69 70
*/

Monty's avatar
 
Monty committed
71 72
/* block abstraction setup *********************************************/

Monty's avatar
 
Monty committed
73 74 75 76
#ifndef WORD_ALIGN
#define WORD_ALIGN 8
#endif

Monty's avatar
 
Monty committed
77
int vorbis_block_init(vorbis_dsp_state *v, vorbis_block *vb){
Monty's avatar
 
Monty committed
78
  int i;
79
  memset(vb,0,sizeof(*vb));
Monty's avatar
 
Monty committed
80
  vb->vd=v;
Monty's avatar
 
Monty committed
81 82
  vb->localalloc=0;
  vb->localstore=NULL;
Monty's avatar
 
Monty committed
83
  if(v->analysisp){
Monty's avatar
 
Monty committed
84 85 86
    vorbis_block_internal *vbi=
      vb->internal=_ogg_calloc(1,sizeof(vorbis_block_internal));
    vbi->ampmax=-9999;
Monty's avatar
 
Monty committed
87 88 89

    for(i=0;i<PACKETBLOBS;i++){
      if(i==PACKETBLOBS/2){
90
        vbi->packetblob[i]=&vb->opb;
Monty's avatar
 
Monty committed
91
      }else{
92 93
        vbi->packetblob[i]=
          _ogg_calloc(1,sizeof(oggpack_buffer));
Monty's avatar
 
Monty committed
94 95
      }
      oggpack_writeinit(vbi->packetblob[i]);
Monty's avatar
Monty committed
96
    }
Monty's avatar
 
Monty committed
97
  }
98

Monty's avatar
 
Monty committed
99 100 101
  return(0);
}

Monty's avatar
 
Monty committed
102 103 104
void *_vorbis_block_alloc(vorbis_block *vb,long bytes){
  bytes=(bytes+(WORD_ALIGN-1)) & ~(WORD_ALIGN-1);
  if(bytes+vb->localtop>vb->localalloc){
Monty's avatar
 
Monty committed
105
    /* can't just _ogg_realloc... there are outstanding pointers */
Monty's avatar
 
Monty committed
106
    if(vb->localstore){
107
      struct alloc_chain *link=_ogg_malloc(sizeof(*link));
Monty's avatar
 
Monty committed
108 109 110 111 112 113 114
      vb->totaluse+=vb->localtop;
      link->next=vb->reap;
      link->ptr=vb->localstore;
      vb->reap=link;
    }
    /* highly conservative */
    vb->localalloc=bytes;
Monty's avatar
 
Monty committed
115
    vb->localstore=_ogg_malloc(vb->localalloc);
Monty's avatar
 
Monty committed
116
    vb->localtop=0;
Monty's avatar
 
Monty committed
117
  }
Monty's avatar
 
Monty committed
118
  {
Monty's avatar
 
Monty committed
119
    void *ret=(void *)(((char *)vb->localstore)+vb->localtop);
Monty's avatar
 
Monty committed
120 121 122
    vb->localtop+=bytes;
    return ret;
  }
Monty's avatar
 
Monty committed
123 124
}

Monty's avatar
 
Monty committed
125
/* reap the chain, pull the ripcord */
Monty's avatar
 
Monty committed
126
void _vorbis_block_ripcord(vorbis_block *vb){
Monty's avatar
 
Monty committed
127 128 129 130
  /* reap the chain */
  struct alloc_chain *reap=vb->reap;
  while(reap){
    struct alloc_chain *next=reap->next;
Monty's avatar
 
Monty committed
131
    _ogg_free(reap->ptr);
132
    memset(reap,0,sizeof(*reap));
Monty's avatar
 
Monty committed
133
    _ogg_free(reap);
Monty's avatar
 
Monty committed
134 135 136 137
    reap=next;
  }
  /* consolidate storage */
  if(vb->totaluse){
Monty's avatar
 
Monty committed
138
    vb->localstore=_ogg_realloc(vb->localstore,vb->totaluse+vb->localalloc);
Monty's avatar
 
Monty committed
139 140 141 142 143
    vb->localalloc+=vb->totaluse;
    vb->totaluse=0;
  }

  /* pull the ripcord */
Monty's avatar
 
Monty committed
144
  vb->localtop=0;
Monty's avatar
 
Monty committed
145
  vb->reap=NULL;
Monty's avatar
 
Monty committed
146 147 148
}

int vorbis_block_clear(vorbis_block *vb){
Monty's avatar
 
Monty committed
149 150 151
  int i;
  vorbis_block_internal *vbi=vb->internal;

Monty's avatar
 
Monty committed
152
  _vorbis_block_ripcord(vb);
Monty's avatar
 
Monty committed
153
  if(vb->localstore)_ogg_free(vb->localstore);
Monty's avatar
 
Monty committed
154

Monty's avatar
 
Monty committed
155 156 157 158 159 160 161
  if(vbi){
    for(i=0;i<PACKETBLOBS;i++){
      oggpack_writeclear(vbi->packetblob[i]);
      if(i!=PACKETBLOBS/2)_ogg_free(vbi->packetblob[i]);
    }
    _ogg_free(vbi);
  }
162
  memset(vb,0,sizeof(*vb));
Monty's avatar
 
Monty committed
163 164 165 166 167 168 169
  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 */

Monty's avatar
 
Monty committed
170
static int _vds_shared_init(vorbis_dsp_state *v,vorbis_info *vi,int encp){
Monty's avatar
 
Monty committed
171
  int i;
Monty's avatar
 
Monty committed
172
  codec_setup_info *ci=vi->codec_setup;
Monty's avatar
 
Monty committed
173
  private_state *b=NULL;
Monty's avatar
 
Monty committed
174 175
  int hs;

176 177 178 179 180 181
  if(ci==NULL||
     ci->modes<=0||
     ci->blocksizes[0]<64||
     ci->blocksizes[1]<ci->blocksizes[0]){
    return 1;
  }
Monty's avatar
Monty committed
182
  hs=ci->halfrate_flag;
Monty's avatar
 
Monty committed
183

184 185
  memset(v,0,sizeof(*v));
  b=v->backend_state=_ogg_calloc(1,sizeof(*b));
Monty's avatar
 
Monty committed
186

Monty's avatar
 
Monty committed
187
  v->vi=vi;
188
  b->modebits=ov_ilog(ci->modes-1);
Monty's avatar
 
Monty committed
189

190 191
  b->transform[0]=_ogg_calloc(VI_TRANSFORMB,sizeof(*b->transform[0]));
  b->transform[1]=_ogg_calloc(VI_TRANSFORMB,sizeof(*b->transform[1]));
Monty's avatar
 
Monty committed
192

Monty's avatar
 
Monty committed
193 194
  /* MDCT is tranform 0 */

Monty's avatar
 
Monty committed
195 196
  b->transform[0][0]=_ogg_calloc(1,sizeof(mdct_lookup));
  b->transform[1][0]=_ogg_calloc(1,sizeof(mdct_lookup));
Monty's avatar
 
Monty committed
197 198
  mdct_init(b->transform[0][0],ci->blocksizes[0]>>hs);
  mdct_init(b->transform[1][0],ci->blocksizes[1]>>hs);
Monty's avatar
 
Monty committed
199

Monty's avatar
 
Monty committed
200
  /* Vorbis I uses only window type 0 */
201 202 203 204 205 206 207 208
  /* note that the correct computation below is technically:
       b->window[0]=ov_ilog(ci->blocksizes[0]-1)-6;
       b->window[1]=ov_ilog(ci->blocksizes[1]-1)-6;
    but since blocksizes are always powers of two,
    the below is equivalent.
   */
  b->window[0]=ov_ilog(ci->blocksizes[0])-7;
  b->window[1]=ov_ilog(ci->blocksizes[1])-7;
Monty's avatar
 
Monty committed
209

Monty's avatar
 
Monty committed
210
  if(encp){ /* encode/decode differ here */
Monty's avatar
 
Monty committed
211 212 213 214 215

    /* analysis always needs an fft */
    drft_init(&b->fft_look[0],ci->blocksizes[0]);
    drft_init(&b->fft_look[1],ci->blocksizes[1]);

Monty's avatar
 
Monty committed
216
    /* finish the codebooks */
Monty's avatar
 
Monty committed
217 218 219
    if(!ci->fullbooks){
      ci->fullbooks=_ogg_calloc(ci->books,sizeof(*ci->fullbooks));
      for(i=0;i<ci->books;i++)
220
        vorbis_book_init_encode(ci->fullbooks+i,ci->book_param[i]);
Monty's avatar
 
Monty committed
221
    }
Monty's avatar
 
Monty committed
222 223 224 225

    b->psy=_ogg_calloc(ci->psys,sizeof(*b->psy));
    for(i=0;i<ci->psys;i++){
      _vp_psy_init(b->psy+i,
226 227 228 229
                   ci->psy_param[i],
                   &ci->psy_g_param,
                   ci->blocksizes[ci->psy_param[i]->blockflag]/2,
                   vi->rate);
Monty's avatar
 
Monty committed
230 231
    }

Monty's avatar
 
Monty committed
232 233 234
    v->analysisp=1;
  }else{
    /* finish the codebooks */
235
    if(!ci->fullbooks){
Monty's avatar
 
Monty committed
236
      ci->fullbooks=_ogg_calloc(ci->books,sizeof(*ci->fullbooks));
237 238 239 240 241
      for(i=0;i<ci->books;i++){
        if(ci->book_param[i]==NULL)
          goto abort_books;
        if(vorbis_book_init_decode(ci->fullbooks+i,ci->book_param[i]))
          goto abort_books;
242
        /* decode codebooks are now standalone after init */
243 244 245
        vorbis_staticbook_destroy(ci->book_param[i]);
        ci->book_param[i]=NULL;
      }
Monty's avatar
 
Monty committed
246
    }
Monty's avatar
 
Monty committed
247 248
  }

Monty's avatar
 
Monty committed
249 250 251
  /* initialize the storage vectors. blocksize[1] is small for encode,
     but the correct size for decode */
  v->pcm_storage=ci->blocksizes[1];
252 253
  v->pcm=_ogg_malloc(vi->channels*sizeof(*v->pcm));
  v->pcmret=_ogg_malloc(vi->channels*sizeof(*v->pcmret));
Mike Whitson's avatar
Mike Whitson committed
254 255
  {
    int i;
Monty's avatar
 
Monty committed
256
    for(i=0;i<vi->channels;i++)
257
      v->pcm[i]=_ogg_calloc(v->pcm_storage,sizeof(*v->pcm[i]));
Mike Whitson's avatar
Mike Whitson committed
258 259 260
  }

  /* all 1 (large block) or 0 (small block) */
Monty's avatar
 
Monty committed
261 262 263
  /* explicitly set for the sake of clarity */
  v->lW=0; /* previous window size */
  v->W=0;  /* current window size */
Mike Whitson's avatar
Mike Whitson committed
264

Monty's avatar
 
Monty committed
265
  /* all vector indexes */
Monty's avatar
 
Monty committed
266
  v->centerW=ci->blocksizes[1]/2;
Mike Whitson's avatar
Mike Whitson committed
267

Monty's avatar
 
Monty committed
268
  v->pcm_current=v->centerW;
Monty's avatar
 
Monty committed
269

Monty's avatar
 
Monty committed
270 271 272 273 274 275 276 277 278 279
  /* 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]]->
Monty's avatar
Monty committed
280
      look(v,ci->residue_param[i]);
Monty's avatar
 
Monty committed
281

Monty's avatar
 
Monty committed
282
  return 0;
283 284 285 286 287 288 289 290 291
 abort_books:
  for(i=0;i<ci->books;i++){
    if(ci->book_param[i]!=NULL){
      vorbis_staticbook_destroy(ci->book_param[i]);
      ci->book_param[i]=NULL;
    }
  }
  vorbis_dsp_clear(v);
  return -1;
Mike Whitson's avatar
Mike Whitson committed
292 293
}

Monty's avatar
 
Monty committed
294 295
/* arbitrary settings and spec-mandated numbers get filled in here */
int vorbis_analysis_init(vorbis_dsp_state *v,vorbis_info *vi){
Monty's avatar
 
Monty committed
296
  private_state *b=NULL;
Monty's avatar
 
Monty committed
297

Monty's avatar
 
Monty committed
298
  if(_vds_shared_init(v,vi,1))return 1;
Monty's avatar
 
Monty committed
299
  b=v->backend_state;
Monty's avatar
 
Monty committed
300
  b->psy_g_look=_vp_global_look(vi);
Monty's avatar
 
Monty committed
301

Monty's avatar
 
Monty committed
302
  /* Initialize the envelope state storage */
303
  b->ve=_ogg_calloc(1,sizeof(*b->ve));
Monty's avatar
 
Monty committed
304
  _ve_envelope_init(b->ve,vi);
Monty's avatar
 
Monty committed
305 306 307

  vorbis_bitrate_init(vi,&b->bms);

308 309 310 311
  /* compressed audio packets start after the headers
     with sequence number 3 */
  v->sequence=3;

Monty's avatar
 
Monty committed
312 313 314
  return(0);
}

Monty's avatar
 
Monty committed
315
void vorbis_dsp_clear(vorbis_dsp_state *v){
Monty's avatar
 
Monty committed
316
  int i;
Mike Whitson's avatar
Mike Whitson committed
317
  if(v){
Monty's avatar
 
Monty committed
318
    vorbis_info *vi=v->vi;
Monty's avatar
 
Monty committed
319
    codec_setup_info *ci=(vi?vi->codec_setup:NULL);
Monty's avatar
 
Monty committed
320
    private_state *b=v->backend_state;
Monty's avatar
 
Monty committed
321 322

    if(b){
Monty's avatar
Monty committed
323

Monty's avatar
 
Monty committed
324
      if(b->ve){
325 326
        _ve_envelope_clear(b->ve);
        _ogg_free(b->ve);
Monty's avatar
 
Monty committed
327 328 329
      }

      if(b->transform[0]){
330 331 332
        mdct_clear(b->transform[0][0]);
        _ogg_free(b->transform[0][0]);
        _ogg_free(b->transform[0]);
Monty's avatar
 
Monty committed
333 334
      }
      if(b->transform[1]){
335 336 337
        mdct_clear(b->transform[1][0]);
        _ogg_free(b->transform[1][0]);
        _ogg_free(b->transform[1]);
Monty's avatar
 
Monty committed
338
      }
Monty's avatar
 
Monty committed
339 340

      if(b->flr){
341 342 343 344 345
        if(ci)
          for(i=0;i<ci->floors;i++)
            _floor_P[ci->floor_type[i]]->
              free_look(b->flr[i]);
        _ogg_free(b->flr);
Monty's avatar
 
Monty committed
346 347
      }
      if(b->residue){
348 349 350 351 352
        if(ci)
          for(i=0;i<ci->residues;i++)
            _residue_P[ci->residue_type[i]]->
              free_look(b->residue[i]);
        _ogg_free(b->residue);
Monty's avatar
 
Monty committed
353 354
      }
      if(b->psy){
355 356 357 358
        if(ci)
          for(i=0;i<ci->psys;i++)
            _vp_psy_clear(b->psy+i);
        _ogg_free(b->psy);
Monty's avatar
 
Monty committed
359 360
      }

Monty's avatar
 
Monty committed
361
      if(b->psy_g_look)_vp_global_free(b->psy_g_look);
Monty's avatar
 
Monty committed
362
      vorbis_bitrate_clear(&b->bms);
Monty's avatar
 
Monty committed
363 364 365 366

      drft_clear(&b->fft_look[0]);
      drft_clear(&b->fft_look[1]);

Monty's avatar
 
Monty committed
367
    }
Monty's avatar
Monty committed
368

Mike Whitson's avatar
Mike Whitson committed
369
    if(v->pcm){
370
      if(vi)
371 372
        for(i=0;i<vi->channels;i++)
          if(v->pcm[i])_ogg_free(v->pcm[i]);
Monty's avatar
 
Monty committed
373 374
      _ogg_free(v->pcm);
      if(v->pcmret)_ogg_free(v->pcmret);
Mike Whitson's avatar
Mike Whitson committed
375
    }
Monty's avatar
 
Monty committed
376

Monty's avatar
 
Monty committed
377 378
    if(b){
      /* free header, header1, header2 */
Monty's avatar
 
Monty committed
379 380 381 382
      if(b->header)_ogg_free(b->header);
      if(b->header1)_ogg_free(b->header1);
      if(b->header2)_ogg_free(b->header2);
      _ogg_free(b);
Monty's avatar
 
Monty committed
383
    }
Monty's avatar
Monty committed
384

385
    memset(v,0,sizeof(*v));
Mike Whitson's avatar
Mike Whitson committed
386 387 388
  }
}

Monty's avatar
 
Monty committed
389
float **vorbis_analysis_buffer(vorbis_dsp_state *v, int vals){
Mike Whitson's avatar
Mike Whitson committed
390
  int i;
Monty's avatar
 
Monty committed
391
  vorbis_info *vi=v->vi;
Monty's avatar
 
Monty committed
392
  private_state *b=v->backend_state;
Mike Whitson's avatar
Mike Whitson committed
393

Monty's avatar
 
Monty committed
394
  /* free header, header1, header2 */
Monty's avatar
 
Monty committed
395 396 397
  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;
Monty's avatar
 
Monty committed
398

Monty's avatar
 
Monty committed
399 400
  /* Do we have enough storage space for the requested buffer? If not,
     expand the PCM (and envelope) storage */
Monty's avatar
Monty committed
401

Monty's avatar
 
Monty committed
402 403
  if(v->pcm_current+vals>=v->pcm_storage){
    v->pcm_storage=v->pcm_current+vals*2;
Monty's avatar
Monty committed
404

Monty's avatar
 
Monty committed
405
    for(i=0;i<vi->channels;i++){
406
      v->pcm[i]=_ogg_realloc(v->pcm[i],v->pcm_storage*sizeof(*v->pcm[i]));
Monty's avatar
 
Monty committed
407
    }
Monty's avatar
 
Monty committed
408
  }
Mike Whitson's avatar
Mike Whitson committed
409

Monty's avatar
 
Monty committed
410
  for(i=0;i<vi->channels;i++)
Monty's avatar
 
Monty committed
411
    v->pcmret[i]=v->pcm[i]+v->pcm_current;
Monty's avatar
Monty committed
412

Monty's avatar
 
Monty committed
413 414
  return(v->pcmret);
}
Mike Whitson's avatar
Mike Whitson committed
415

Monty's avatar
 
Monty committed
416
static void _preextrapolate_helper(vorbis_dsp_state *v){
Monty's avatar
 
Monty committed
417
  int i;
Monty's avatar
Monty committed
418
  int order=16;
419 420
  float *lpc=alloca(order*sizeof(*lpc));
  float *work=alloca(v->pcm_current*sizeof(*work));
Monty's avatar
 
Monty committed
421
  long j;
Monty's avatar
 
Monty committed
422
  v->preextrapolate=1;
Monty's avatar
 
Monty committed
423 424 425

  if(v->pcm_current-v->centerW>order*2){ /* safety */
    for(i=0;i<v->vi->channels;i++){
Monty's avatar
 
Monty committed
426
      /* need to run the extrapolation in reverse! */
Monty's avatar
 
Monty committed
427
      for(j=0;j<v->pcm_current;j++)
428
        work[j]=v->pcm[i][v->pcm_current-j-1];
Monty's avatar
Monty committed
429

Monty's avatar
 
Monty committed
430 431
      /* prime as above */
      vorbis_lpc_from_data(work,lpc,v->pcm_current-v->centerW,order);
Monty's avatar
Monty committed
432 433 434

#if 0
      if(v->vi->channels==2){
435 436 437 438
        if(i==0)
          _analysis_output("predataL",0,work,v->pcm_current-v->centerW,0,0,0);
        else
          _analysis_output("predataR",0,work,v->pcm_current-v->centerW,0,0,0);
Monty's avatar
Monty committed
439
      }else{
440
        _analysis_output("predata",0,work,v->pcm_current-v->centerW,0,0,0);
Monty's avatar
Monty committed
441
      }
Monty's avatar
Monty committed
442 443
#endif

Monty's avatar
 
Monty committed
444 445
      /* run the predictor filter */
      vorbis_lpc_predict(lpc,work+v->pcm_current-v->centerW-order,
446 447 448
                         order,
                         work+v->pcm_current-v->centerW,
                         v->centerW);
Monty's avatar
 
Monty committed
449

Monty's avatar
 
Monty committed
450
      for(j=0;j<v->pcm_current;j++)
451
        v->pcm[i][v->pcm_current-j-1]=work[j];
Monty's avatar
 
Monty committed
452

Monty's avatar
 
Monty committed
453 454 455 456 457
    }
  }
}


Monty's avatar
 
Monty committed
458 459
/* call with val<=0 to set eof */

Monty's avatar
 
Monty committed
460
int vorbis_analysis_wrote(vorbis_dsp_state *v, int vals){
Monty's avatar
 
Monty committed
461
  vorbis_info *vi=v->vi;
Monty's avatar
 
Monty committed
462 463
  codec_setup_info *ci=vi->codec_setup;

Monty's avatar
 
Monty committed
464
  if(vals<=0){
Monty's avatar
 
Monty committed
465 466
    int order=32;
    int i;
467
    float *lpc=alloca(order*sizeof(*lpc));
Monty's avatar
 
Monty committed
468 469

    /* if it wasn't done earlier (very short sample) */
Monty's avatar
 
Monty committed
470 471
    if(!v->preextrapolate)
      _preextrapolate_helper(v);
Monty's avatar
 
Monty committed
472

Monty's avatar
 
Monty committed
473
    /* We're encoding the end of the stream.  Just make sure we have
Monty's avatar
 
Monty committed
474
       [at least] a few full blocks of zeroes at the end. */
Monty's avatar
 
Monty committed
475 476 477
    /* 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. */
Monty's avatar
 
Monty committed
478

Monty's avatar
Monty committed
479
    vorbis_analysis_buffer(v,ci->blocksizes[1]*3);
Monty's avatar
 
Monty committed
480
    v->eofflag=v->pcm_current;
Monty's avatar
 
Monty committed
481
    v->pcm_current+=ci->blocksizes[1]*3;
Monty's avatar
 
Monty committed
482 483 484

    for(i=0;i<vi->channels;i++){
      if(v->eofflag>order*2){
485 486
        /* extrapolate with LPC to fill in */
        long n;
Monty's avatar
 
Monty committed
487

488 489 490 491
        /* 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);
Monty's avatar
 
Monty committed
492

493 494 495
        /* 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);
Monty's avatar
 
Monty committed
496
      }else{
497
        /* not enough data to extrapolate (unlikely to happen due to
Monty's avatar
 
Monty committed
498 499
           guarding the overlap, but bulletproof in case that
           assumtion goes away). zeroes will do. */
500 501
        memset(v->pcm[i]+v->eofflag,0,
               (v->pcm_current-v->eofflag)*sizeof(*v->pcm[i]));
Monty's avatar
 
Monty committed
502 503 504

      }
    }
Monty's avatar
 
Monty committed
505
  }else{
Monty's avatar
 
Monty committed
506

Monty's avatar
 
Monty committed
507
    if(v->pcm_current+vals>v->pcm_storage)
Monty's avatar
 
Monty committed
508
      return(OV_EINVAL);
Mike Whitson's avatar
Mike Whitson committed
509

Monty's avatar
 
Monty committed
510
    v->pcm_current+=vals;
Monty's avatar
 
Monty committed
511

Monty's avatar
 
Monty committed
512
    /* we may want to reverse extrapolate the beginning of a stream
Monty's avatar
 
Monty committed
513 514
       too... in case we're beginning on a cliff! */
    /* clumsy, but simple.  It only runs once, so simple is good. */
Monty's avatar
 
Monty committed
515
    if(!v->preextrapolate && v->pcm_current-v->centerW>ci->blocksizes[1])
Monty's avatar
 
Monty committed
516
      _preextrapolate_helper(v);
Monty's avatar
 
Monty committed
517

Monty's avatar
 
Monty committed
518
  }
Monty's avatar
 
Monty committed
519 520
  return(0);
}
Mike Whitson's avatar
Mike Whitson committed
521

Monty's avatar
 
Monty committed
522 523
/* do the deltas, envelope shaping, pre-echo and determine the size of
   the next block on which to continue analysis */
Monty's avatar
 
Monty committed
524
int vorbis_analysis_blockout(vorbis_dsp_state *v,vorbis_block *vb){
Monty's avatar
 
Monty committed
525
  int i;
Monty's avatar
 
Monty committed
526
  vorbis_info *vi=v->vi;
Monty's avatar
 
Monty committed
527
  codec_setup_info *ci=vi->codec_setup;
Monty's avatar
 
Monty committed
528
  private_state *b=v->backend_state;
Monty's avatar
 
Monty committed
529
  vorbis_look_psy_global *g=b->psy_g_look;
Monty's avatar
 
Monty committed
530
  long beginW=v->centerW-ci->blocksizes[v->W]/2,centerNext;
Monty's avatar
 
Monty committed
531
  vorbis_block_internal *vbi=(vorbis_block_internal *)vb->internal;
Mike Whitson's avatar
Mike Whitson committed
532

Monty's avatar
 
Monty committed
533
  /* check to see if we're started... */
Monty's avatar
 
Monty committed
534
  if(!v->preextrapolate)return(0);
Monty's avatar
 
Monty committed
535

Monty's avatar
 
Monty committed
536 537
  /* check to see if we're done... */
  if(v->eofflag==-1)return(0);
Mike Whitson's avatar
Mike Whitson committed
538

Monty's avatar
 
Monty committed
539 540 541
  /* 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 */
Monty's avatar
 
Monty committed
542 543 544 545

  /* 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. */
Monty's avatar
Monty committed
546
  {
Monty's avatar
 
Monty committed
547
    long bp=_ve_envelope_search(v);
Monty's avatar
 
Monty committed
548
    if(bp==-1){
Monty's avatar
 
Monty committed
549

Monty's avatar
 
Monty committed
550
      if(v->eofflag==0)return(0); /* not enough data currently to search for a
551
                                     full long block */
Monty's avatar
 
Monty committed
552 553
      v->nW=0;
    }else{
Monty's avatar
 
Monty committed
554 555

      if(ci->blocksizes[0]==ci->blocksizes[1])
556
        v->nW=0;
Monty's avatar
 
Monty committed
557
      else
558
        v->nW=bp;
Monty's avatar
 
Monty committed
559
    }
Monty's avatar
 
Monty committed
560
  }
Monty's avatar
 
Monty committed
561

Monty's avatar
 
Monty committed
562
  centerNext=v->centerW+ci->blocksizes[v->W]/4+ci->blocksizes[v->nW]/4;
Monty's avatar
 
Monty committed
563

Monty's avatar
 
Monty committed
564
  {
Monty's avatar
 
Monty committed
565
    /* center of next block + next block maximum right side. */
Monty's avatar
 
Monty committed
566

Monty's avatar
 
Monty committed
567
    long blockbound=centerNext+ci->blocksizes[v->nW]/2;
Monty's avatar
 
Monty committed
568 569 570 571 572 573 574
    if(v->pcm_current<blockbound)return(0); /* not enough data yet;
                                               although this check is
                                               less strict that the
                                               _ve_envelope_search,
                                               the search is not run
                                               if we only use one
                                               block size */
Monty's avatar
 
Monty committed
575 576


Monty's avatar
 
Monty committed
577
  }
Monty's avatar
Monty committed
578

Monty's avatar
 
Monty committed
579 580
  /* fill in the block.  Note that for a short window, lW and nW are *short*
     regardless of actual settings in the stream */
Monty's avatar
 
Monty committed
581

Monty's avatar
 
Monty committed
582
  _vorbis_block_ripcord(vb);
Monty's avatar
 
Monty committed
583 584 585
  vb->lW=v->lW;
  vb->W=v->W;
  vb->nW=v->nW;
Monty's avatar
 
Monty committed
586 587

  if(v->W){
Monty's avatar
 
Monty committed
588
    if(!v->lW || !v->nW){
Monty's avatar
 
Monty committed
589
      vbi->blocktype=BLOCKTYPE_TRANSITION;
Monty's avatar
 
Monty committed
590
      /*fprintf(stderr,"-");*/
Monty's avatar
 
Monty committed
591
    }else{
Monty's avatar
 
Monty committed
592
      vbi->blocktype=BLOCKTYPE_LONG;
Monty's avatar
 
Monty committed
593
      /*fprintf(stderr,"_");*/
Monty's avatar
 
Monty committed
594
    }
Monty's avatar
 
Monty committed
595
  }else{
Monty's avatar
 
Monty committed
596
    if(_ve_envelope_mark(v)){
Monty's avatar
 
Monty committed
597
      vbi->blocktype=BLOCKTYPE_IMPULSE;
Monty's avatar
 
Monty committed
598
      /*fprintf(stderr,"|");*/
Monty's avatar
 
Monty committed
599 600

    }else{
Monty's avatar
 
Monty committed
601
      vbi->blocktype=BLOCKTYPE_PADDING;
Monty's avatar
 
Monty committed
602
      /*fprintf(stderr,".");*/
Monty's avatar
 
Monty committed
603 604

    }
Monty's avatar
 
Monty committed
605
  }
Monty's avatar
Monty committed
606

Monty's avatar
 
Monty committed
607
  vb->vd=v;
Monty's avatar
 
Monty committed
608
  vb->sequence=v->sequence++;
Monty's avatar
 
Monty committed
609
  vb->granulepos=v->granulepos;
Monty's avatar
 
Monty committed
610
  vb->pcmend=ci->blocksizes[v->W];
Monty's avatar
Monty committed
611

Monty's avatar
 
Monty committed
612
  /* copy the vectors; this uses the local storage in vb */
Monty's avatar
 
Monty committed
613

Monty's avatar
 
Monty committed
614 615 616 617 618
  /* this tracks 'strongest peak' for later psychoacoustics */
  /* moved to the global psy state; clean this mess up */
  if(vbi->ampmax>g->ampmax)g->ampmax=vbi->ampmax;
  g->ampmax=_vp_ampmax_decay(g->ampmax,v);
  vbi->ampmax=g->ampmax;
Monty's avatar
Monty committed
619

Monty's avatar
 
Monty committed
620 621 622 623 624 625 626
  vb->pcm=_vorbis_block_alloc(vb,sizeof(*vb->pcm)*vi->channels);
  vbi->pcmdelay=_vorbis_block_alloc(vb,sizeof(*vbi->pcmdelay)*vi->channels);
  for(i=0;i<vi->channels;i++){
    vbi->pcmdelay[i]=
      _vorbis_block_alloc(vb,(vb->pcmend+beginW)*sizeof(*vbi->pcmdelay[i]));
    memcpy(vbi->pcmdelay[i],v->pcm[i],(vb->pcmend+beginW)*sizeof(*vbi->pcmdelay[i]));
    vb->pcm[i]=vbi->pcmdelay[i]+beginW;
Monty's avatar
Monty committed
627 628

    /* before we added the delay
Monty's avatar
 
Monty committed
629 630 631
       vb->pcm[i]=_vorbis_block_alloc(vb,vb->pcmend*sizeof(*vb->pcm[i]));
       memcpy(vb->pcm[i],v->pcm[i]+beginW,ci->blocksizes[v->W]*sizeof(*vb->pcm[i]));
    */
Monty's avatar
Monty committed
632

Monty's avatar
 
Monty committed
633
  }
Monty's avatar
Monty committed
634

Monty's avatar
 
Monty committed
635 636 637 638 639
  /* handle eof detection: eof==0 means that we've not yet received EOF
                           eof>0  marks the last 'real' sample in pcm[]
                           eof<0  'no more to do'; doesn't get here */

  if(v->eofflag){
Monty's avatar
 
Monty committed
640
    if(v->centerW>=v->eofflag){
Monty's avatar
 
Monty committed
641 642
      v->eofflag=-1;
      vb->eofflag=1;
Monty's avatar
 
Monty committed
643
      return(1);
Monty's avatar
 
Monty committed
644 645
    }
  }
Mike Whitson's avatar
Mike Whitson committed
646 647 648

  /* advance storage vectors and clean up */
  {
Monty's avatar
 
Monty committed
649
    int new_centerNext=ci->blocksizes[1]/2;
Monty's avatar
 
Monty committed
650
    int movementW=centerNext-new_centerNext;
Monty's avatar
 
Monty committed
651

Monty's avatar
 
Monty committed
652
    if(movementW>0){
Monty's avatar
 
Monty committed
653

Monty's avatar
 
Monty committed
654 655
      _ve_envelope_shift(b->ve,movementW);
      v->pcm_current-=movementW;
Monty's avatar
Monty committed
656

Monty's avatar
 
Monty committed
657
      for(i=0;i<vi->channels;i++)
658 659
        memmove(v->pcm[i],v->pcm[i]+movementW,
                v->pcm_current*sizeof(*v->pcm[i]));
Monty's avatar
Monty committed
660 661


Monty's avatar
 
Monty committed
662 663 664
      v->lW=v->W;
      v->W=v->nW;
      v->centerW=new_centerNext;
Monty's avatar
Monty committed
665

Monty's avatar
 
Monty committed
666
      if(v->eofflag){
667 668 669 670 671 672 673 674
        v->eofflag-=movementW;
        if(v->eofflag<=0)v->eofflag=-1;
        /* do not add padding to end of stream! */
        if(v->centerW>=v->eofflag){
          v->granulepos+=movementW-(v->centerW-v->eofflag);
        }else{
          v->granulepos+=movementW;
        }
Monty's avatar
 
Monty committed
675
      }else{
676
        v->granulepos+=movementW;
Monty's avatar
 
Monty committed
677
      }
Monty's avatar
 
Monty committed
678
    }
Mike Whitson's avatar
Mike Whitson committed
679 680 681 682 683
  }

  /* done */
  return(1);
}
Monty's avatar
 
Monty committed
684

Monty's avatar
 
Monty committed
685 686 687
int vorbis_synthesis_restart(vorbis_dsp_state *v){
  vorbis_info *vi=v->vi;
  codec_setup_info *ci;
Monty's avatar
 
Monty committed
688
  int hs;
Monty's avatar
 
Monty committed
689 690 691 692 693

  if(!v->backend_state)return -1;
  if(!vi)return -1;
  ci=vi->codec_setup;
  if(!ci)return -1;
Monty's avatar
Monty committed
694
  hs=ci->halfrate_flag;
Monty's avatar
 
Monty committed
695

Monty's avatar
 
Monty committed
696 697
  v->centerW=ci->blocksizes[1]>>(hs+1);
  v->pcm_current=v->centerW>>hs;
Monty's avatar
Monty committed
698

Monty's avatar
 
Monty committed
699
  v->pcm_returned=-1;
Monty's avatar
 
Monty committed
700
  v->granulepos=-1;
Monty's avatar
 
Monty committed
701
  v->sequence=-1;
Monty's avatar
 
Monty committed
702
  v->eofflag=0;
Monty's avatar
 
Monty committed
703
  ((private_state *)(v->backend_state))->sample_count=-1;
Monty's avatar
 
Monty committed
704

Monty's avatar
 
Monty committed
705 706
  return(0);
}
Monty's avatar
 
Monty committed
707

Monty's avatar
 
Monty committed
708
int vorbis_synthesis_init(vorbis_dsp_state *v,vorbis_info *vi){
709 710 711 712
  if(_vds_shared_init(v,vi,0)){
    vorbis_dsp_clear(v);
    return 1;
  }
Monty's avatar
 
Monty committed
713
  vorbis_synthesis_restart(v);
Monty's avatar
 
Monty committed
714
  return 0;
Monty's avatar
 
Monty committed
715 716
}

Monty's avatar
 
Monty committed
717
/* Unlike in analysis, the window is only partially applied for each
Monty's avatar
 
Monty committed
718 719
   block.  The time domain envelope is not yet handled at the point of
   calling (as it relies on the previous block). */
Monty's avatar
 
Monty committed
720

Monty's avatar
 
Monty committed
721
int vorbis_synthesis_blockin(vorbis_dsp_state *v,vorbis_block *vb){
Monty's avatar
 
Monty committed
722
  vorbis_info *vi=v->vi;
Monty's avatar
 
Monty committed
723
  codec_setup_info *ci=vi->codec_setup;
Monty's avatar
 
Monty committed
724
  private_state *b=v->backend_state;
Monty's avatar
Monty committed
725
  int hs=ci->halfrate_flag;
Monty's avatar
 
Monty committed
726
  int i,j;
Monty's avatar
 
Monty committed
727

Monty's avatar
 
Monty committed
728
  if(!vb)return(OV_EINVAL);
Monty's avatar
 
Monty committed
729
  if(v->pcm_current>v->pcm_returned  && v->pcm_returned!=-1)return(OV_EINVAL);
Monty's avatar
Monty committed
730

Monty's avatar
 
Monty committed
731 732
  v->lW=v->W;
  v->W=vb->W;
Monty's avatar
 
Monty committed
733
  v->nW=-1;
Monty's avatar
Monty committed
734

Monty's avatar
 
Monty committed
735 736 737 738 739 740
  if((v->sequence==-1)||
     (v->sequence+1 != vb->sequence)){
    v->granulepos=-1; /* out of sequence; lose count */
    b->sample_count=-1;
  }

Monty's avatar
 
Monty committed
741
  v->sequence=vb->sequence;
Monty's avatar
Monty committed
742 743

  if(vb->pcm){  /* no pcm to process if vorbis_synthesis_trackonly
744
                   was called on block */
Monty's avatar
 
Monty committed
745 746 747 748
    int n=ci->blocksizes[v->W]>>(hs+1);
    int n0=ci->blocksizes[0]>>(hs+1);
    int n1=ci->blocksizes[1]>>(hs+1);

Monty's avatar
 
Monty committed
749 750
    int thisCenter;
    int prevCenter;
Monty's avatar
Monty committed
751

Monty's avatar
 
Monty committed
752 753 754 755
    v->glue_bits+=vb->glue_bits;
    v->time_bits+=vb->time_bits;
    v->floor_bits+=vb->floor_bits;
    v->res_bits+=vb->res_bits;
Monty's avatar
Monty committed
756

Monty's avatar
 
Monty committed
757 758 759 760
    if(v->centerW){
      thisCenter=n1;
      prevCenter=0;
    }else{
Monty's avatar
 
Monty committed
761 762
      thisCenter=0;
      prevCenter=n1;
Monty's avatar
 
Monty committed
763
    }
Monty's avatar
Monty committed
764

Monty's avatar
 
Monty committed
765 766 767
    /* v->pcm is now used like a two-stage double buffer.  We don't want
       to have to constantly shift *or* adjust memory usage.  Don't
       accept a new block until the old is shifted out */
Monty's avatar
Monty committed
768

Monty's avatar
 
Monty committed
769
    for(j=0;j<vi->channels;j++){
Monty's avatar
 
Monty committed
770
      /* the overlap/add section */
Monty's avatar
 
Monty committed
771
      if(v->lW){
772 773
        if