annotate tremor/bitwise.c @ 21682:67e1aa5951e9

synced with r21697
author gpoirier
date Wed, 20 Dec 2006 12:43:06 +0000
parents e891ff7a7b6c
children e83eef58b30a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14280
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
1 /********************************************************************
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
2 * *
15250
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
14280
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
7 * *
15250
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2002 *
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
9 * by the Xiph.Org Foundation http://www.xiph.org/ *
14280
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
10 * *
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
11 ********************************************************************
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
12
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
13 function: packing variable sized words into an octet stream
15250
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
14 last mod: $Id$
14280
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
15
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
16 ********************************************************************/
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
17
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
18 /* We're 'LSb' endian; if we write a word but read individual bits,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
19 then we'll read the lsb first */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
20
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
21 #include <string.h>
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
22 #include <stdlib.h>
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
23 #include "ogg.h"
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
24
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
25 #define BUFFER_INCREMENT 256
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
26
15250
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
27 static const unsigned long mask[]=
14280
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
28 {0x00000000,0x00000001,0x00000003,0x00000007,0x0000000f,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
29 0x0000001f,0x0000003f,0x0000007f,0x000000ff,0x000001ff,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
30 0x000003ff,0x000007ff,0x00000fff,0x00001fff,0x00003fff,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
31 0x00007fff,0x0000ffff,0x0001ffff,0x0003ffff,0x0007ffff,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
32 0x000fffff,0x001fffff,0x003fffff,0x007fffff,0x00ffffff,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
33 0x01ffffff,0x03ffffff,0x07ffffff,0x0fffffff,0x1fffffff,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
34 0x3fffffff,0x7fffffff,0xffffffff };
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
35
15250
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
36 static const unsigned int mask8B[]=
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
37 {0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff};
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
38
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
39 void oggpack_writeinit(oggpack_buffer *b){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
40 memset(b,0,sizeof(*b));
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
41 b->ptr=b->buffer=_ogg_malloc(BUFFER_INCREMENT);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
42 b->buffer[0]='\0';
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
43 b->storage=BUFFER_INCREMENT;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
44 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
45
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
46 void oggpackB_writeinit(oggpack_buffer *b){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
47 oggpack_writeinit(b);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
48 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
49
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
50 void oggpack_writetrunc(oggpack_buffer *b,long bits){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
51 long bytes=bits>>3;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
52 bits-=bytes*8;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
53 b->ptr=b->buffer+bytes;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
54 b->endbit=bits;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
55 b->endbyte=bytes;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
56 *b->ptr&=mask[bits];
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
57 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
58
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
59 void oggpackB_writetrunc(oggpack_buffer *b,long bits){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
60 long bytes=bits>>3;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
61 bits-=bytes*8;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
62 b->ptr=b->buffer+bytes;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
63 b->endbit=bits;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
64 b->endbyte=bytes;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
65 *b->ptr&=mask8B[bits];
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
66 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
67
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
68 /* Takes only up to 32 bits. */
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
69 void oggpack_write(oggpack_buffer *b,unsigned long value,int bits){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
70 if(b->endbyte+4>=b->storage){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
71 b->buffer=_ogg_realloc(b->buffer,b->storage+BUFFER_INCREMENT);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
72 b->storage+=BUFFER_INCREMENT;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
73 b->ptr=b->buffer+b->endbyte;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
74 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
75
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
76 value&=mask[bits];
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
77 bits+=b->endbit;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
78
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
79 b->ptr[0]|=value<<b->endbit;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
80
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
81 if(bits>=8){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
82 b->ptr[1]=(unsigned char)(value>>(8-b->endbit));
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
83 if(bits>=16){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
84 b->ptr[2]=(unsigned char)(value>>(16-b->endbit));
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
85 if(bits>=24){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
86 b->ptr[3]=(unsigned char)(value>>(24-b->endbit));
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
87 if(bits>=32){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
88 if(b->endbit)
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
89 b->ptr[4]=(unsigned char)(value>>(32-b->endbit));
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
90 else
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
91 b->ptr[4]=0;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
92 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
93 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
94 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
95 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
96
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
97 b->endbyte+=bits/8;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
98 b->ptr+=bits/8;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
99 b->endbit=bits&7;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
100 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
101
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
102 /* Takes only up to 32 bits. */
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
103 void oggpackB_write(oggpack_buffer *b,unsigned long value,int bits){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
104 if(b->endbyte+4>=b->storage){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
105 b->buffer=_ogg_realloc(b->buffer,b->storage+BUFFER_INCREMENT);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
106 b->storage+=BUFFER_INCREMENT;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
107 b->ptr=b->buffer+b->endbyte;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
108 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
109
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
110 value=(value&mask[bits])<<(32-bits);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
111 bits+=b->endbit;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
112
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
113 b->ptr[0]|=value>>(24+b->endbit);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
114
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
115 if(bits>=8){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
116 b->ptr[1]=(unsigned char)(value>>(16+b->endbit));
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
117 if(bits>=16){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
118 b->ptr[2]=(unsigned char)(value>>(8+b->endbit));
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
119 if(bits>=24){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
120 b->ptr[3]=(unsigned char)(value>>(b->endbit));
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
121 if(bits>=32){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
122 if(b->endbit)
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
123 b->ptr[4]=(unsigned char)(value<<(8-b->endbit));
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
124 else
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
125 b->ptr[4]=0;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
126 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
127 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
128 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
129 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
130
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
131 b->endbyte+=bits/8;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
132 b->ptr+=bits/8;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
133 b->endbit=bits&7;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
134 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
135
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
136 void oggpack_writealign(oggpack_buffer *b){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
137 int bits=8-b->endbit;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
138 if(bits<8)
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
139 oggpack_write(b,0,bits);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
140 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
141
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
142 void oggpackB_writealign(oggpack_buffer *b){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
143 int bits=8-b->endbit;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
144 if(bits<8)
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
145 oggpackB_write(b,0,bits);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
146 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
147
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
148 static void oggpack_writecopy_helper(oggpack_buffer *b,
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
149 void *source,
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
150 long bits,
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
151 void (*w)(oggpack_buffer *,
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
152 unsigned long,
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
153 int),
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
154 int msb){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
155 unsigned char *ptr=(unsigned char *)source;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
156
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
157 long bytes=bits/8;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
158 bits-=bytes*8;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
159
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
160 if(b->endbit){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
161 int i;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
162 /* unaligned copy. Do it the hard way. */
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
163 for(i=0;i<bytes;i++)
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
164 w(b,(unsigned long)(ptr[i]),8);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
165 }else{
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
166 /* aligned block copy */
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
167 if(b->endbyte+bytes+1>=b->storage){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
168 b->storage=b->endbyte+bytes+BUFFER_INCREMENT;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
169 b->buffer=_ogg_realloc(b->buffer,b->storage);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
170 b->ptr=b->buffer+b->endbyte;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
171 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
172
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
173 memmove(b->ptr,source,bytes);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
174 b->ptr+=bytes;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
175 b->endbyte+=bytes;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
176 *b->ptr=0;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
177
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
178 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
179 if(bits){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
180 if(msb)
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
181 w(b,(unsigned long)(ptr[bytes]>>(8-bits)),bits);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
182 else
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
183 w(b,(unsigned long)(ptr[bytes]),bits);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
184 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
185 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
186
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
187 void oggpack_writecopy(oggpack_buffer *b,void *source,long bits){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
188 oggpack_writecopy_helper(b,source,bits,oggpack_write,0);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
189 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
190
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
191 void oggpackB_writecopy(oggpack_buffer *b,void *source,long bits){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
192 oggpack_writecopy_helper(b,source,bits,oggpackB_write,1);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
193 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
194
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
195 void oggpack_reset(oggpack_buffer *b){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
196 b->ptr=b->buffer;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
197 b->buffer[0]=0;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
198 b->endbit=b->endbyte=0;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
199 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
200
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
201 void oggpackB_reset(oggpack_buffer *b){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
202 oggpack_reset(b);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
203 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
204
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
205 void oggpack_writeclear(oggpack_buffer *b){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
206 _ogg_free(b->buffer);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
207 memset(b,0,sizeof(*b));
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
208 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
209
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
210 void oggpackB_writeclear(oggpack_buffer *b){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
211 oggpack_writeclear(b);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
212 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
213
14280
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
214 void oggpack_readinit(oggpack_buffer *b,unsigned char *buf,int bytes){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
215 memset(b,0,sizeof(*b));
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
216 b->buffer=b->ptr=buf;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
217 b->storage=bytes;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
218 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
219
15250
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
220 void oggpackB_readinit(oggpack_buffer *b,unsigned char *buf,int bytes){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
221 oggpack_readinit(b,buf,bytes);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
222 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
223
14280
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
224 /* Read in bits without advancing the bitptr; bits <= 32 */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
225 long oggpack_look(oggpack_buffer *b,int bits){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
226 unsigned long ret;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
227 unsigned long m=mask[bits];
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
228
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
229 bits+=b->endbit;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
230
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
231 if(b->endbyte+4>=b->storage){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
232 /* not the main path */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
233 if(b->endbyte*8+bits>b->storage*8)return(-1);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
234 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
235
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
236 ret=b->ptr[0]>>b->endbit;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
237 if(bits>8){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
238 ret|=b->ptr[1]<<(8-b->endbit);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
239 if(bits>16){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
240 ret|=b->ptr[2]<<(16-b->endbit);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
241 if(bits>24){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
242 ret|=b->ptr[3]<<(24-b->endbit);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
243 if(bits>32 && b->endbit)
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
244 ret|=b->ptr[4]<<(32-b->endbit);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
245 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
246 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
247 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
248 return(m&ret);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
249 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
250
15250
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
251 /* Read in bits without advancing the bitptr; bits <= 32 */
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
252 long oggpackB_look(oggpack_buffer *b,int bits){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
253 unsigned long ret;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
254 int m=32-bits;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
255
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
256 bits+=b->endbit;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
257
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
258 if(b->endbyte+4>=b->storage){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
259 /* not the main path */
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
260 if(b->endbyte*8+bits>b->storage*8)return(-1);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
261 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
262
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
263 ret=b->ptr[0]<<(24+b->endbit);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
264 if(bits>8){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
265 ret|=b->ptr[1]<<(16+b->endbit);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
266 if(bits>16){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
267 ret|=b->ptr[2]<<(8+b->endbit);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
268 if(bits>24){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
269 ret|=b->ptr[3]<<(b->endbit);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
270 if(bits>32 && b->endbit)
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
271 ret|=b->ptr[4]>>(8-b->endbit);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
272 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
273 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
274 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
275 return ((ret&0xffffffff)>>(m>>1))>>((m+1)>>1);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
276 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
277
14280
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
278 long oggpack_look1(oggpack_buffer *b){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
279 if(b->endbyte>=b->storage)return(-1);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
280 return((b->ptr[0]>>b->endbit)&1);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
281 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
282
15250
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
283 long oggpackB_look1(oggpack_buffer *b){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
284 if(b->endbyte>=b->storage)return(-1);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
285 return((b->ptr[0]>>(7-b->endbit))&1);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
286 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
287
14280
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
288 void oggpack_adv(oggpack_buffer *b,int bits){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
289 bits+=b->endbit;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
290 b->ptr+=bits/8;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
291 b->endbyte+=bits/8;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
292 b->endbit=bits&7;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
293 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
294
15250
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
295 void oggpackB_adv(oggpack_buffer *b,int bits){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
296 oggpack_adv(b,bits);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
297 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
298
14280
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
299 void oggpack_adv1(oggpack_buffer *b){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
300 if(++(b->endbit)>7){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
301 b->endbit=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
302 b->ptr++;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
303 b->endbyte++;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
304 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
305 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
306
15250
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
307 void oggpackB_adv1(oggpack_buffer *b){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
308 oggpack_adv1(b);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
309 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
310
14280
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
311 /* bits <= 32 */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
312 long oggpack_read(oggpack_buffer *b,int bits){
15250
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
313 long ret;
14280
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
314 unsigned long m=mask[bits];
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
315
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
316 bits+=b->endbit;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
317
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
318 if(b->endbyte+4>=b->storage){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
319 /* not the main path */
15250
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
320 ret=-1L;
14280
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
321 if(b->endbyte*8+bits>b->storage*8)goto overflow;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
322 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
323
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
324 ret=b->ptr[0]>>b->endbit;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
325 if(bits>8){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
326 ret|=b->ptr[1]<<(8-b->endbit);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
327 if(bits>16){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
328 ret|=b->ptr[2]<<(16-b->endbit);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
329 if(bits>24){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
330 ret|=b->ptr[3]<<(24-b->endbit);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
331 if(bits>32 && b->endbit){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
332 ret|=b->ptr[4]<<(32-b->endbit);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
333 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
334 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
335 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
336 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
337 ret&=m;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
338
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
339 overflow:
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
340
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
341 b->ptr+=bits/8;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
342 b->endbyte+=bits/8;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
343 b->endbit=bits&7;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
344 return(ret);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
345 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
346
15250
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
347 /* bits <= 32 */
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
348 long oggpackB_read(oggpack_buffer *b,int bits){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
349 long ret;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
350 long m=32-bits;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
351
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
352 bits+=b->endbit;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
353
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
354 if(b->endbyte+4>=b->storage){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
355 /* not the main path */
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
356 ret=-1L;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
357 if(b->endbyte*8+bits>b->storage*8)goto overflow;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
358 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
359
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
360 ret=b->ptr[0]<<(24+b->endbit);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
361 if(bits>8){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
362 ret|=b->ptr[1]<<(16+b->endbit);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
363 if(bits>16){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
364 ret|=b->ptr[2]<<(8+b->endbit);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
365 if(bits>24){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
366 ret|=b->ptr[3]<<(b->endbit);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
367 if(bits>32 && b->endbit)
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
368 ret|=b->ptr[4]>>(8-b->endbit);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
369 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
370 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
371 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
372 ret=((ret&0xffffffffUL)>>(m>>1))>>((m+1)>>1);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
373
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
374 overflow:
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
375
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
376 b->ptr+=bits/8;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
377 b->endbyte+=bits/8;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
378 b->endbit=bits&7;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
379 return(ret);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
380 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
381
14280
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
382 long oggpack_read1(oggpack_buffer *b){
15250
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
383 long ret;
14280
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
384
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
385 if(b->endbyte>=b->storage){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
386 /* not the main path */
15250
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
387 ret=-1L;
14280
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
388 goto overflow;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
389 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
390
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
391 ret=(b->ptr[0]>>b->endbit)&1;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
392
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
393 overflow:
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
394
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
395 b->endbit++;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
396 if(b->endbit>7){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
397 b->endbit=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
398 b->ptr++;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
399 b->endbyte++;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
400 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
401 return(ret);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
402 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
403
15250
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
404 long oggpackB_read1(oggpack_buffer *b){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
405 long ret;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
406
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
407 if(b->endbyte>=b->storage){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
408 /* not the main path */
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
409 ret=-1L;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
410 goto overflow;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
411 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
412
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
413 ret=(b->ptr[0]>>(7-b->endbit))&1;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
414
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
415 overflow:
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
416
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
417 b->endbit++;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
418 if(b->endbit>7){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
419 b->endbit=0;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
420 b->ptr++;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
421 b->endbyte++;
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
422 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
423 return(ret);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
424 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
425
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
426 long oggpack_bytes(oggpack_buffer *b){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
427 return(b->endbyte+(b->endbit+7)/8);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
428 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
429
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
430 long oggpack_bits(oggpack_buffer *b){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
431 return(b->endbyte*8+b->endbit);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
432 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
433
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
434 long oggpackB_bytes(oggpack_buffer *b){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
435 return oggpack_bytes(b);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
436 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
437
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
438 long oggpackB_bits(oggpack_buffer *b){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
439 return oggpack_bits(b);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
440 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
441
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
442 unsigned char *oggpack_get_buffer(oggpack_buffer *b){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
443 return(b->buffer);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
444 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
445
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
446 unsigned char *oggpackB_get_buffer(oggpack_buffer *b){
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
447 return oggpack_get_buffer(b);
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
448 }
e891ff7a7b6c update to libogg 1.1.2 (needed for Theora)
henry
parents: 14280
diff changeset
449
14280
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
450 #undef BUFFER_INCREMENT