1 /********************************************************************
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. *
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/ *
12 ********************************************************************
14 function: flexible, delayed bitpacking abstraction
15 last mod: $Id: bitbuffer.c,v 1.1 2000/11/17 11:57:49 xiphmont Exp $
17 ********************************************************************/
20 #include <vorbis/codec.h>
22 #include "bitbuffer.h"
24 /* done carefully to do two things:
26 2) draws from our exact-size vorbis_block pool
29 void bitbuf_init(vorbis_bitbuffer *vbb,vorbis_block *vb){
30 memset(vbb,0,sizeof(vorbis_bitbuffer));
32 vbb->first=vbb->last=_vorbis_block_alloc(vb,sizeof(vorbis_bitbuffer_chain));
33 vbb->first->next=0; /* overengineering */
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 */
44 vbb->last->words[vbb->ptr]=word;
45 vbb->last->bits[vbb->ptr++]=length;
48 void bitbuf_pack(oggpack_buffer *dest,vorbis_bitbuffer *vbb){
49 vorbis_block *vb=vbb->vb;
50 vorbis_bitbuffer_chain *vbc=vbb->first;
54 for(i=0;i<_VBB_ALLOCSIZE;i++)
55 oggpack_write(dest,vbc->words[i],vbc->bits[i]);
58 for(i=0;i<vbb->ptr;i++)
59 oggpack_write(dest,vbc->words[i],vbc->bits[i]);
62 /* codebook variants for encoding to the bitbuffer */
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]);