static inline define hack for GCC where it really helps the mdct
[platform/upstream/libvorbis.git] / lib / bitbuffer.c
1 /********************************************************************
2  *                                                                  *
3  * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
4  * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
5  * THE GNU LESSER/LIBRARY PUBLIC LICENSE, WHICH IS INCLUDED WITH    *
6  * THIS SOURCE. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.        *
7  *                                                                  *
8  * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2000             *
9  * by Monty <monty@xiph.org> and the XIPHOPHORUS Company            *
10  * http://www.xiph.org/                                             *
11  *                                                                  *
12  ********************************************************************
13
14  function: flexible, delayed bitpacking abstraction
15  last mod: $Id: bitbuffer.c,v 1.1 2000/11/17 11:57:49 xiphmont Exp $
16
17  ********************************************************************/
18
19 #include <ogg/ogg.h>
20 #include <vorbis/codec.h>
21 #include "misc.h"
22 #include "bitbuffer.h"
23
24 /* done carefully to do two things:
25    1) no realloc
26    2) draws from our exact-size vorbis_block pool
27 */
28
29 void bitbuf_init(vorbis_bitbuffer *vbb,vorbis_block *vb){
30   memset(vbb,0,sizeof(vorbis_bitbuffer));
31   vbb->vb=vb;
32   vbb->first=vbb->last=_vorbis_block_alloc(vb,sizeof(vorbis_bitbuffer_chain));
33   vbb->first->next=0; /* overengineering */
34 }
35
36 void bitbuf_write(vorbis_bitbuffer *vbb,unsigned long word,int length){
37   vorbis_block *vb=vbb->vb;
38   if(vbb->ptr>=_VBB_ALLOCSIZE){
39     vbb->last->next=_vorbis_block_alloc(vb,sizeof(vorbis_bitbuffer_chain));
40     vbb->last=vbb->last->next;
41     vbb->last->next=0; /* overengineering */
42     vbb->ptr=0;
43   }
44   vbb->last->words[vbb->ptr]=word;
45   vbb->last->bits[vbb->ptr++]=length;
46 }
47
48 void bitbuf_pack(oggpack_buffer *dest,vorbis_bitbuffer *vbb){
49   vorbis_block *vb=vbb->vb;
50   vorbis_bitbuffer_chain *vbc=vbb->first;
51   int i;
52   
53   while(vbc->next){
54     for(i=0;i<_VBB_ALLOCSIZE;i++)
55       oggpack_write(dest,vbc->words[i],vbc->bits[i]);
56     vbc=vbc->next;
57   }
58   for(i=0;i<vbb->ptr;i++)
59     oggpack_write(dest,vbc->words[i],vbc->bits[i]);
60 }
61
62 /* codebook variants for encoding to the bitbuffer */
63
64 int vorbis_book_bufencode(codebook *book, int a, vorbis_bitbuffer *b){
65   bitbuf_write(b,book->codelist[a],book->c->lengthlist[a]);
66   return(book->c->lengthlist[a]);
67 }
68