annotate tremor/framing.c @ 15229:03ed7e622585

Start changelog for next release
author rtognimp
date Wed, 20 Apr 2005 21:24:46 +0000
parents 8631a3803289
children e891ff7a7b6c
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 * *
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
3 * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE. *
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
4 * *
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
5 * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2002 *
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
6 * BY THE Xiph.Org FOUNDATION http://www.xiph.org/ *
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
7 * ALL REDISTRIBUTION RIGHTS RESERVED. *
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
8 * *
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
9 ********************************************************************
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
10
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
11 function: code raw [Vorbis] packets into framed OggSquish stream and
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
12 decode Ogg streams back into raw packets
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
13
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
14 note: The CRC code is directly derived from public domain code by
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
15 Ross Williams (ross@guest.adelaide.edu.au). See docs/framing.html
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
16 for details.
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
17
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
18 ********************************************************************/
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
19
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
20 #include <stdlib.h>
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 "ogg.h"
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
23
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
24 /* A complete description of Ogg framing exists in docs/framing.html */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
25
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
26 int ogg_page_version(ogg_page *og){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
27 return((int)(og->header[4]));
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
28 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
29
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
30 int ogg_page_continued(ogg_page *og){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
31 return((int)(og->header[5]&0x01));
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
32 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
33
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
34 int ogg_page_bos(ogg_page *og){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
35 return((int)(og->header[5]&0x02));
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
36 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
37
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
38 int ogg_page_eos(ogg_page *og){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
39 return((int)(og->header[5]&0x04));
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
40 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
41
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
42 ogg_int64_t ogg_page_granulepos(ogg_page *og){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
43 unsigned char *page=og->header;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
44 ogg_int64_t granulepos=page[13]&(0xff);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
45 granulepos= (granulepos<<8)|(page[12]&0xff);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
46 granulepos= (granulepos<<8)|(page[11]&0xff);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
47 granulepos= (granulepos<<8)|(page[10]&0xff);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
48 granulepos= (granulepos<<8)|(page[9]&0xff);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
49 granulepos= (granulepos<<8)|(page[8]&0xff);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
50 granulepos= (granulepos<<8)|(page[7]&0xff);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
51 granulepos= (granulepos<<8)|(page[6]&0xff);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
52 return(granulepos);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
53 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
54
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
55 int ogg_page_serialno(ogg_page *og){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
56 return(og->header[14] |
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
57 (og->header[15]<<8) |
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
58 (og->header[16]<<16) |
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
59 (og->header[17]<<24));
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
60 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
61
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
62 long ogg_page_pageno(ogg_page *og){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
63 return(og->header[18] |
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
64 (og->header[19]<<8) |
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
65 (og->header[20]<<16) |
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
66 (og->header[21]<<24));
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
67 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
68
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
69
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
70
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
71 /* returns the number of packets that are completed on this page (if
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
72 the leading packet is begun on a previous page, but ends on this
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
73 page, it's counted */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
74
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
75 /* NOTE:
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
76 If a page consists of a packet begun on a previous page, and a new
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
77 packet begun (but not completed) on this page, the return will be:
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
78 ogg_page_packets(page) ==1,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
79 ogg_page_continued(page) !=0
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
80
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
81 If a page happens to be a single packet that was begun on a
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
82 previous page, and spans to the next page (in the case of a three or
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
83 more page packet), the return will be:
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
84 ogg_page_packets(page) ==0,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
85 ogg_page_continued(page) !=0
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
86 */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
87
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
88 int ogg_page_packets(ogg_page *og){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
89 int i,n=og->header[26],count=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
90 for(i=0;i<n;i++)
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
91 if(og->header[27+i]<255)count++;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
92 return(count);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
93 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
94
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
95 static ogg_uint32_t crc_lookup[256]={
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
96 0x00000000,0x04c11db7,0x09823b6e,0x0d4326d9,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
97 0x130476dc,0x17c56b6b,0x1a864db2,0x1e475005,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
98 0x2608edb8,0x22c9f00f,0x2f8ad6d6,0x2b4bcb61,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
99 0x350c9b64,0x31cd86d3,0x3c8ea00a,0x384fbdbd,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
100 0x4c11db70,0x48d0c6c7,0x4593e01e,0x4152fda9,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
101 0x5f15adac,0x5bd4b01b,0x569796c2,0x52568b75,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
102 0x6a1936c8,0x6ed82b7f,0x639b0da6,0x675a1011,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
103 0x791d4014,0x7ddc5da3,0x709f7b7a,0x745e66cd,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
104 0x9823b6e0,0x9ce2ab57,0x91a18d8e,0x95609039,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
105 0x8b27c03c,0x8fe6dd8b,0x82a5fb52,0x8664e6e5,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
106 0xbe2b5b58,0xbaea46ef,0xb7a96036,0xb3687d81,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
107 0xad2f2d84,0xa9ee3033,0xa4ad16ea,0xa06c0b5d,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
108 0xd4326d90,0xd0f37027,0xddb056fe,0xd9714b49,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
109 0xc7361b4c,0xc3f706fb,0xceb42022,0xca753d95,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
110 0xf23a8028,0xf6fb9d9f,0xfbb8bb46,0xff79a6f1,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
111 0xe13ef6f4,0xe5ffeb43,0xe8bccd9a,0xec7dd02d,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
112 0x34867077,0x30476dc0,0x3d044b19,0x39c556ae,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
113 0x278206ab,0x23431b1c,0x2e003dc5,0x2ac12072,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
114 0x128e9dcf,0x164f8078,0x1b0ca6a1,0x1fcdbb16,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
115 0x018aeb13,0x054bf6a4,0x0808d07d,0x0cc9cdca,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
116 0x7897ab07,0x7c56b6b0,0x71159069,0x75d48dde,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
117 0x6b93dddb,0x6f52c06c,0x6211e6b5,0x66d0fb02,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
118 0x5e9f46bf,0x5a5e5b08,0x571d7dd1,0x53dc6066,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
119 0x4d9b3063,0x495a2dd4,0x44190b0d,0x40d816ba,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
120 0xaca5c697,0xa864db20,0xa527fdf9,0xa1e6e04e,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
121 0xbfa1b04b,0xbb60adfc,0xb6238b25,0xb2e29692,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
122 0x8aad2b2f,0x8e6c3698,0x832f1041,0x87ee0df6,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
123 0x99a95df3,0x9d684044,0x902b669d,0x94ea7b2a,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
124 0xe0b41de7,0xe4750050,0xe9362689,0xedf73b3e,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
125 0xf3b06b3b,0xf771768c,0xfa325055,0xfef34de2,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
126 0xc6bcf05f,0xc27dede8,0xcf3ecb31,0xcbffd686,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
127 0xd5b88683,0xd1799b34,0xdc3abded,0xd8fba05a,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
128 0x690ce0ee,0x6dcdfd59,0x608edb80,0x644fc637,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
129 0x7a089632,0x7ec98b85,0x738aad5c,0x774bb0eb,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
130 0x4f040d56,0x4bc510e1,0x46863638,0x42472b8f,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
131 0x5c007b8a,0x58c1663d,0x558240e4,0x51435d53,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
132 0x251d3b9e,0x21dc2629,0x2c9f00f0,0x285e1d47,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
133 0x36194d42,0x32d850f5,0x3f9b762c,0x3b5a6b9b,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
134 0x0315d626,0x07d4cb91,0x0a97ed48,0x0e56f0ff,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
135 0x1011a0fa,0x14d0bd4d,0x19939b94,0x1d528623,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
136 0xf12f560e,0xf5ee4bb9,0xf8ad6d60,0xfc6c70d7,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
137 0xe22b20d2,0xe6ea3d65,0xeba91bbc,0xef68060b,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
138 0xd727bbb6,0xd3e6a601,0xdea580d8,0xda649d6f,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
139 0xc423cd6a,0xc0e2d0dd,0xcda1f604,0xc960ebb3,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
140 0xbd3e8d7e,0xb9ff90c9,0xb4bcb610,0xb07daba7,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
141 0xae3afba2,0xaafbe615,0xa7b8c0cc,0xa379dd7b,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
142 0x9b3660c6,0x9ff77d71,0x92b45ba8,0x9675461f,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
143 0x8832161a,0x8cf30bad,0x81b02d74,0x857130c3,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
144 0x5d8a9099,0x594b8d2e,0x5408abf7,0x50c9b640,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
145 0x4e8ee645,0x4a4ffbf2,0x470cdd2b,0x43cdc09c,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
146 0x7b827d21,0x7f436096,0x7200464f,0x76c15bf8,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
147 0x68860bfd,0x6c47164a,0x61043093,0x65c52d24,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
148 0x119b4be9,0x155a565e,0x18197087,0x1cd86d30,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
149 0x029f3d35,0x065e2082,0x0b1d065b,0x0fdc1bec,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
150 0x3793a651,0x3352bbe6,0x3e119d3f,0x3ad08088,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
151 0x2497d08d,0x2056cd3a,0x2d15ebe3,0x29d4f654,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
152 0xc5a92679,0xc1683bce,0xcc2b1d17,0xc8ea00a0,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
153 0xd6ad50a5,0xd26c4d12,0xdf2f6bcb,0xdbee767c,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
154 0xe3a1cbc1,0xe760d676,0xea23f0af,0xeee2ed18,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
155 0xf0a5bd1d,0xf464a0aa,0xf9278673,0xfde69bc4,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
156 0x89b8fd09,0x8d79e0be,0x803ac667,0x84fbdbd0,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
157 0x9abc8bd5,0x9e7d9662,0x933eb0bb,0x97ffad0c,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
158 0xafb010b1,0xab710d06,0xa6322bdf,0xa2f33668,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
159 0xbcb4666d,0xb8757bda,0xb5365d03,0xb1f740b4};
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
160
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
161 /* init the encode/decode logical stream state */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
162
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
163 int ogg_stream_init(ogg_stream_state *os,int serialno){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
164 if(os){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
165 memset(os,0,sizeof(*os));
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
166 os->body_storage=16*1024;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
167 os->body_data=(unsigned char *)_ogg_malloc(os->body_storage*sizeof(*os->body_data));
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
168
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
169 os->lacing_storage=1024;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
170 os->lacing_vals=(int *)_ogg_malloc(os->lacing_storage*sizeof(*os->lacing_vals));
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
171 os->granule_vals=(ogg_int64_t *)_ogg_malloc(os->lacing_storage*sizeof(*os->granule_vals));
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
172
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
173 os->serialno=serialno;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
174
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
175 return(0);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
176 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
177 return(-1);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
178 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
179
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
180 /* _clear does not free os, only the non-flat storage within */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
181 int ogg_stream_clear(ogg_stream_state *os){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
182 if(os){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
183 if(os->body_data)_ogg_free(os->body_data);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
184 if(os->lacing_vals)_ogg_free(os->lacing_vals);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
185 if(os->granule_vals)_ogg_free(os->granule_vals);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
186
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
187 memset(os,0,sizeof(*os));
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
188 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
189 return(0);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
190 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
191
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
192 int ogg_stream_destroy(ogg_stream_state *os){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
193 if(os){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
194 ogg_stream_clear(os);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
195 _ogg_free(os);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
196 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
197 return(0);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
198 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
199
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
200 /* Helpers for ogg_stream_encode; this keeps the structure and
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
201 what's happening fairly clear */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
202
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
203 static void _os_body_expand(ogg_stream_state *os,int needed){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
204 if(os->body_storage<=os->body_fill+needed){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
205 os->body_storage+=(needed+1024);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
206 os->body_data=(unsigned char *)_ogg_realloc(os->body_data,os->body_storage*sizeof(*os->body_data));
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
207 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
208 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
209
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
210 static void _os_lacing_expand(ogg_stream_state *os,int needed){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
211 if(os->lacing_storage<=os->lacing_fill+needed){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
212 os->lacing_storage+=(needed+32);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
213 os->lacing_vals=(int *)_ogg_realloc(os->lacing_vals,os->lacing_storage*sizeof(*os->lacing_vals));
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
214 os->granule_vals=(ogg_int64_t *)_ogg_realloc(os->granule_vals,os->lacing_storage*sizeof(*os->granule_vals));
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
215 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
216 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
217
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
218 /* checksum the page */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
219 /* Direct table CRC; note that this will be faster in the future if we
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
220 perform the checksum silmultaneously with other copies */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
221
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
222 void ogg_page_checksum_set(ogg_page *og){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
223 if(og){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
224 ogg_uint32_t crc_reg=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
225 int i;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
226
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
227 /* safety; needed for API behavior, but not framing code */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
228 og->header[22]=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
229 og->header[23]=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
230 og->header[24]=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
231 og->header[25]=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
232
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
233 for(i=0;i<og->header_len;i++)
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
234 crc_reg=(crc_reg<<8)^crc_lookup[((crc_reg >> 24)&0xff)^og->header[i]];
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
235 for(i=0;i<og->body_len;i++)
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
236 crc_reg=(crc_reg<<8)^crc_lookup[((crc_reg >> 24)&0xff)^og->body[i]];
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
237
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
238 og->header[22]=crc_reg&0xff;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
239 og->header[23]=(crc_reg>>8)&0xff;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
240 og->header[24]=(crc_reg>>16)&0xff;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
241 og->header[25]=(crc_reg>>24)&0xff;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
242 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
243 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
244
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
245 /* DECODING PRIMITIVES: packet streaming layer **********************/
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
246
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
247 /* This has two layers to place more of the multi-serialno and paging
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
248 control in the application's hands. First, we expose a data buffer
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
249 using ogg_sync_buffer(). The app either copies into the
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
250 buffer, or passes it directly to read(), etc. We then call
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
251 ogg_sync_wrote() to tell how many bytes we just added.
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
252
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
253 Pages are returned (pointers into the buffer in ogg_sync_state)
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
254 by ogg_sync_pageout(). The page is then submitted to
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
255 ogg_stream_pagein() along with the appropriate
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
256 ogg_stream_state* (ie, matching serialno). We then get raw
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
257 packets out calling ogg_stream_packetout() with a
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
258 ogg_stream_state. See the 'frame-prog.txt' docs for details and
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
259 example code. */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
260
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
261 /* initialize the struct to a known state */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
262 int ogg_sync_init(ogg_sync_state *oy){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
263 if(oy){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
264 memset(oy,0,sizeof(*oy));
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
265 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
266 return(0);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
267 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
268
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
269 /* clear non-flat storage within */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
270 int ogg_sync_clear(ogg_sync_state *oy){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
271 if(oy){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
272 if(oy->data)_ogg_free(oy->data);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
273 ogg_sync_init(oy);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
274 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
275 return(0);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
276 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
277
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
278 int ogg_sync_destroy(ogg_sync_state *oy){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
279 if(oy){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
280 ogg_sync_clear(oy);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
281 _ogg_free(oy);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
282 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
283 return(0);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
284 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
285
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
286 char *ogg_sync_buffer(ogg_sync_state *oy, long size){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
287
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
288 /* first, clear out any space that has been previously returned */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
289 if(oy->returned>8192){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
290 oy->fill-=oy->returned;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
291 if(oy->fill>0)
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
292 memmove(oy->data,oy->data+oy->returned,oy->fill);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
293 oy->returned=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
294 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
295
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
296 if(size>oy->storage-oy->fill){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
297 /* We need to extend the internal buffer */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
298 long newsize=size+oy->fill+4096; /* an extra page to be nice */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
299
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
300 if(oy->data)
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
301 oy->data=(unsigned char *)_ogg_realloc(oy->data,newsize);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
302 else
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
303 oy->data=(unsigned char *)_ogg_malloc(newsize);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
304 oy->storage=newsize;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
305 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
306
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
307 /* expose a segment at least as large as requested at the fill mark */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
308 return((char *)oy->data+oy->fill);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
309 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
310
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
311 int ogg_sync_wrote(ogg_sync_state *oy, long bytes){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
312 if(oy->fill+bytes>oy->storage)return(-1);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
313 oy->fill+=bytes;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
314 return(0);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
315 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
316
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
317 /* sync the stream. This is meant to be useful for finding page
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
318 boundaries.
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
319
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
320 return values for this:
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
321 -n) skipped n bytes
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
322 0) page not ready; more data (no bytes skipped)
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
323 n) page synced at current location; page length n bytes
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
324
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
325 */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
326
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
327 long ogg_sync_pageseek(ogg_sync_state *oy,ogg_page *og){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
328 unsigned char *page=oy->data+oy->returned;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
329 unsigned char *next;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
330 long bytes=oy->fill-oy->returned;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
331
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
332 if(oy->headerbytes==0){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
333 int headerbytes,i;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
334 if(bytes<27)return(0); /* not enough for a header */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
335
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
336 /* verify capture pattern */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
337 if(memcmp(page,"OggS",4))goto sync_fail;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
338
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
339 headerbytes=page[26]+27;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
340 if(bytes<headerbytes)return(0); /* not enough for header + seg table */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
341
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
342 /* count up body length in the segment table */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
343
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
344 for(i=0;i<page[26];i++)
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
345 oy->bodybytes+=page[27+i];
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
346 oy->headerbytes=headerbytes;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
347 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
348
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
349 if(oy->bodybytes+oy->headerbytes>bytes)return(0);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
350
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
351 /* The whole test page is buffered. Verify the checksum */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
352 {
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
353 /* Grab the checksum bytes, set the header field to zero */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
354 char chksum[4];
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
355 ogg_page log;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
356
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
357 memcpy(chksum,page+22,4);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
358 memset(page+22,0,4);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
359
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
360 /* set up a temp page struct and recompute the checksum */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
361 log.header=page;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
362 log.header_len=oy->headerbytes;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
363 log.body=page+oy->headerbytes;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
364 log.body_len=oy->bodybytes;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
365 ogg_page_checksum_set(&log);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
366
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
367 /* Compare */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
368 if(memcmp(chksum,page+22,4)){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
369 /* D'oh. Mismatch! Corrupt page (or miscapture and not a page
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
370 at all) */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
371 /* replace the computed checksum with the one actually read in */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
372 memcpy(page+22,chksum,4);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
373
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
374 /* Bad checksum. Lose sync */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
375 goto sync_fail;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
376 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
377 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
378
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
379 /* yes, have a whole page all ready to go */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
380 {
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
381 unsigned char *page=oy->data+oy->returned;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
382 long bytes;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
383
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
384 if(og){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
385 og->header=page;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
386 og->header_len=oy->headerbytes;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
387 og->body=page+oy->headerbytes;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
388 og->body_len=oy->bodybytes;
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 oy->unsynced=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
392 oy->returned+=(bytes=oy->headerbytes+oy->bodybytes);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
393 oy->headerbytes=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
394 oy->bodybytes=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
395 return(bytes);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
396 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
397
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
398 sync_fail:
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
399
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
400 oy->headerbytes=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
401 oy->bodybytes=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
402
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
403 /* search for possible capture */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
404 next=(unsigned char *)memchr(page+1,'O',bytes-1);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
405 if(!next)
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
406 next=oy->data+oy->fill;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
407
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
408 oy->returned=next-oy->data;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
409 return(-(next-page));
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
410 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
411
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
412 /* sync the stream and get a page. Keep trying until we find a page.
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
413 Supress 'sync errors' after reporting the first.
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
414
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
415 return values:
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
416 -1) recapture (hole in data)
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
417 0) need more data
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
418 1) page returned
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
419
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
420 Returns pointers into buffered data; invalidated by next call to
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
421 _stream, _clear, _init, or _buffer */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
422
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
423 int ogg_sync_pageout(ogg_sync_state *oy, ogg_page *og){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
424
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
425 /* all we need to do is verify a page at the head of the stream
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
426 buffer. If it doesn't verify, we look for the next potential
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
427 frame */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
428
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
429 while(1){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
430 long ret=ogg_sync_pageseek(oy,og);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
431 if(ret>0){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
432 /* have a page */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
433 return(1);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
434 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
435 if(ret==0){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
436 /* need more data */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
437 return(0);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
438 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
439
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
440 /* head did not start a synced page... skipped some bytes */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
441 if(!oy->unsynced){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
442 oy->unsynced=1;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
443 return(-1);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
444 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
445
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
446 /* loop. keep looking */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
447
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
448 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
449 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
450
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
451 /* add the incoming page to the stream state; we decompose the page
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
452 into packet segments here as well. */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
453
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
454 int ogg_stream_pagein(ogg_stream_state *os, ogg_page *og){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
455 unsigned char *header=og->header;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
456 unsigned char *body=og->body;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
457 long bodysize=og->body_len;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
458 int segptr=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
459
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
460 int version=ogg_page_version(og);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
461 int continued=ogg_page_continued(og);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
462 int bos=ogg_page_bos(og);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
463 int eos=ogg_page_eos(og);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
464 ogg_int64_t granulepos=ogg_page_granulepos(og);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
465 int serialno=ogg_page_serialno(og);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
466 long pageno=ogg_page_pageno(og);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
467 int segments=header[26];
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
468
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
469 /* clean up 'returned data' */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
470 {
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
471 long lr=os->lacing_returned;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
472 long br=os->body_returned;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
473
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
474 /* body data */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
475 if(br>8192){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
476 os->body_fill-=br;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
477 if(os->body_fill)
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
478 memmove(os->body_data,os->body_data+br,os->body_fill);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
479 os->body_returned=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
480 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
481
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
482 if(lr>8192){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
483 /* segment table */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
484 if(os->lacing_fill-lr){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
485 memmove(os->lacing_vals,os->lacing_vals+lr,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
486 (os->lacing_fill-lr)*sizeof(*os->lacing_vals));
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
487 memmove(os->granule_vals,os->granule_vals+lr,
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
488 (os->lacing_fill-lr)*sizeof(*os->granule_vals));
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
489 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
490 os->lacing_fill-=lr;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
491 os->lacing_packet-=lr;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
492 os->lacing_returned=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
493 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
494 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
495
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
496 /* check the serial number */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
497 if(serialno!=os->serialno)return(-1);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
498 if(version>0)return(-1);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
499
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
500 _os_lacing_expand(os,segments+1);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
501
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
502 /* are we in sequence? */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
503 if(pageno!=os->pageno){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
504 int i;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
505
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
506 /* unroll previous partial packet (if any) */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
507 for(i=os->lacing_packet;i<os->lacing_fill;i++)
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
508 os->body_fill-=os->lacing_vals[i]&0xff;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
509 os->lacing_fill=os->lacing_packet;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
510
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
511 /* make a note of dropped data in segment table */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
512 if(os->pageno!=-1){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
513 os->lacing_vals[os->lacing_fill++]=0x400;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
514 os->lacing_packet++;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
515 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
516
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
517 /* are we a 'continued packet' page? If so, we'll need to skip
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
518 some segments */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
519 if(continued){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
520 bos=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
521 for(;segptr<segments;segptr++){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
522 int val=header[27+segptr];
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
523 body+=val;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
524 bodysize-=val;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
525 if(val<255){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
526 segptr++;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
527 break;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
528 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
529 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
530 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
531 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
532
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
533 if(bodysize){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
534 _os_body_expand(os,bodysize);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
535 memcpy(os->body_data+os->body_fill,body,bodysize);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
536 os->body_fill+=bodysize;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
537 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
538
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
539 {
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
540 int saved=-1;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
541 while(segptr<segments){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
542 int val=header[27+segptr];
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
543 os->lacing_vals[os->lacing_fill]=val;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
544 os->granule_vals[os->lacing_fill]=-1;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
545
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
546 if(bos){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
547 os->lacing_vals[os->lacing_fill]|=0x100;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
548 bos=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
549 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
550
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
551 if(val<255)saved=os->lacing_fill;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
552
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
553 os->lacing_fill++;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
554 segptr++;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
555
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
556 if(val<255)os->lacing_packet=os->lacing_fill;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
557 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
558
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
559 /* set the granulepos on the last granuleval of the last full packet */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
560 if(saved!=-1){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
561 os->granule_vals[saved]=granulepos;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
562 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
563
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
564 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
565
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
566 if(eos){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
567 os->e_o_s=1;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
568 if(os->lacing_fill>0)
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
569 os->lacing_vals[os->lacing_fill-1]|=0x200;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
570 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
571
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
572 os->pageno=pageno+1;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
573
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
574 return(0);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
575 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
576
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
577 /* clear things to an initial state. Good to call, eg, before seeking */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
578 int ogg_sync_reset(ogg_sync_state *oy){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
579 oy->fill=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
580 oy->returned=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
581 oy->unsynced=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
582 oy->headerbytes=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
583 oy->bodybytes=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
584 return(0);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
585 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
586
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
587 int ogg_stream_reset(ogg_stream_state *os){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
588 os->body_fill=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
589 os->body_returned=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
590
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
591 os->lacing_fill=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
592 os->lacing_packet=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
593 os->lacing_returned=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
594
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
595 os->header_fill=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
596
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
597 os->e_o_s=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
598 os->b_o_s=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
599 os->pageno=-1;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
600 os->packetno=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
601 os->granulepos=0;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
602
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
603 return(0);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
604 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
605
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
606 static int _packetout(ogg_stream_state *os,ogg_packet *op,int adv){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
607
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
608 /* The last part of decode. We have the stream broken into packet
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
609 segments. Now we need to group them into packets (or return the
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
610 out of sync markers) */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
611
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
612 int ptr=os->lacing_returned;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
613
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
614 if(os->lacing_packet<=ptr)return(0);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
615
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
616 if(os->lacing_vals[ptr]&0x400){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
617 /* we need to tell the codec there's a gap; it might need to
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
618 handle previous packet dependencies. */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
619 os->lacing_returned++;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
620 os->packetno++;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
621 return(-1);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
622 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
623
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
624 if(!op && !adv)return(1); /* just using peek as an inexpensive way
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
625 to ask if there's a whole packet
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
626 waiting */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
627
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
628 /* Gather the whole packet. We'll have no holes or a partial packet */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
629 {
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
630 int size=os->lacing_vals[ptr]&0xff;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
631 int bytes=size;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
632 int eos=os->lacing_vals[ptr]&0x200; /* last packet of the stream? */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
633 int bos=os->lacing_vals[ptr]&0x100; /* first packet of the stream? */
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
634
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
635 while(size==255){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
636 int val=os->lacing_vals[++ptr];
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
637 size=val&0xff;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
638 if(val&0x200)eos=0x200;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
639 bytes+=size;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
640 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
641
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
642 if(op){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
643 op->e_o_s=eos;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
644 op->b_o_s=bos;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
645 op->packet=os->body_data+os->body_returned;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
646 op->packetno=os->packetno;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
647 op->granulepos=os->granule_vals[ptr];
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
648 op->bytes=bytes;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
649 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
650
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
651 if(adv){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
652 os->body_returned+=bytes;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
653 os->lacing_returned=ptr+1;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
654 os->packetno++;
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
655 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
656 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
657 return(1);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
658 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
659
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
660 int ogg_stream_packetout(ogg_stream_state *os,ogg_packet *op){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
661 return _packetout(os,op,1);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
662 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
663
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
664 int ogg_stream_packetpeek(ogg_stream_state *os,ogg_packet *op){
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
665 return _packetout(os,op,0);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
666 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
667
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
668 void ogg_packet_clear(ogg_packet *op) {
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
669 _ogg_free(op->packet);
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
670 memset(op, 0, sizeof(*op));
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
671 }
8631a3803289 internal Tremor decoder for Ogg/Vorbis
henry
parents:
diff changeset
672