rev |
line source |
nuclear@0
|
1 /********************************************************************
|
nuclear@0
|
2 * *
|
nuclear@0
|
3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
|
nuclear@0
|
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
|
nuclear@0
|
5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
|
nuclear@0
|
6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
|
nuclear@0
|
7 * *
|
nuclear@0
|
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2010 *
|
nuclear@0
|
9 * by the Xiph.Org Foundation http://www.xiph.org/ *
|
nuclear@0
|
10 * *
|
nuclear@0
|
11 ********************************************************************
|
nuclear@0
|
12
|
nuclear@0
|
13 function: maintain the info structure, info <-> header packets
|
nuclear@0
|
14 last mod: $Id: info.c 18186 2012-02-03 22:08:44Z xiphmont $
|
nuclear@0
|
15
|
nuclear@0
|
16 ********************************************************************/
|
nuclear@0
|
17
|
nuclear@0
|
18 /* general handling of the header and the vorbis_info structure (and
|
nuclear@0
|
19 substructures) */
|
nuclear@0
|
20
|
nuclear@0
|
21 #include <stdlib.h>
|
nuclear@0
|
22 #include <string.h>
|
nuclear@0
|
23 #include <ctype.h>
|
nuclear@0
|
24 #include <ogg/ogg.h>
|
nuclear@0
|
25 #include "vorbis/codec.h"
|
nuclear@0
|
26 #include "codec_internal.h"
|
nuclear@0
|
27 #include "codebook.h"
|
nuclear@0
|
28 #include "registry.h"
|
nuclear@0
|
29 #include "window.h"
|
nuclear@0
|
30 #include "psy.h"
|
nuclear@0
|
31 #include "misc.h"
|
nuclear@0
|
32 #include "os.h"
|
nuclear@0
|
33
|
nuclear@0
|
34 #define GENERAL_VENDOR_STRING "Xiph.Org libVorbis 1.3.3"
|
nuclear@0
|
35 #define ENCODE_VENDOR_STRING "Xiph.Org libVorbis I 20120203 (Omnipresent)"
|
nuclear@0
|
36
|
nuclear@0
|
37 /* helpers */
|
nuclear@0
|
38 static int ilog2(unsigned int v){
|
nuclear@0
|
39 int ret=0;
|
nuclear@0
|
40 if(v)--v;
|
nuclear@0
|
41 while(v){
|
nuclear@0
|
42 ret++;
|
nuclear@0
|
43 v>>=1;
|
nuclear@0
|
44 }
|
nuclear@0
|
45 return(ret);
|
nuclear@0
|
46 }
|
nuclear@0
|
47
|
nuclear@0
|
48 static void _v_writestring(oggpack_buffer *o,const char *s, int bytes){
|
nuclear@0
|
49
|
nuclear@0
|
50 while(bytes--){
|
nuclear@0
|
51 oggpack_write(o,*s++,8);
|
nuclear@0
|
52 }
|
nuclear@0
|
53 }
|
nuclear@0
|
54
|
nuclear@0
|
55 static void _v_readstring(oggpack_buffer *o,char *buf,int bytes){
|
nuclear@0
|
56 while(bytes--){
|
nuclear@0
|
57 *buf++=oggpack_read(o,8);
|
nuclear@0
|
58 }
|
nuclear@0
|
59 }
|
nuclear@0
|
60
|
nuclear@0
|
61 void vorbis_comment_init(vorbis_comment *vc){
|
nuclear@0
|
62 memset(vc,0,sizeof(*vc));
|
nuclear@0
|
63 }
|
nuclear@0
|
64
|
nuclear@0
|
65 void vorbis_comment_add(vorbis_comment *vc,const char *comment){
|
nuclear@0
|
66 vc->user_comments=_ogg_realloc(vc->user_comments,
|
nuclear@0
|
67 (vc->comments+2)*sizeof(*vc->user_comments));
|
nuclear@0
|
68 vc->comment_lengths=_ogg_realloc(vc->comment_lengths,
|
nuclear@0
|
69 (vc->comments+2)*sizeof(*vc->comment_lengths));
|
nuclear@0
|
70 vc->comment_lengths[vc->comments]=strlen(comment);
|
nuclear@0
|
71 vc->user_comments[vc->comments]=_ogg_malloc(vc->comment_lengths[vc->comments]+1);
|
nuclear@0
|
72 strcpy(vc->user_comments[vc->comments], comment);
|
nuclear@0
|
73 vc->comments++;
|
nuclear@0
|
74 vc->user_comments[vc->comments]=NULL;
|
nuclear@0
|
75 }
|
nuclear@0
|
76
|
nuclear@0
|
77 void vorbis_comment_add_tag(vorbis_comment *vc, const char *tag, const char *contents){
|
nuclear@0
|
78 char *comment=alloca(strlen(tag)+strlen(contents)+2); /* +2 for = and \0 */
|
nuclear@0
|
79 strcpy(comment, tag);
|
nuclear@0
|
80 strcat(comment, "=");
|
nuclear@0
|
81 strcat(comment, contents);
|
nuclear@0
|
82 vorbis_comment_add(vc, comment);
|
nuclear@0
|
83 }
|
nuclear@0
|
84
|
nuclear@0
|
85 /* This is more or less the same as strncasecmp - but that doesn't exist
|
nuclear@0
|
86 * everywhere, and this is a fairly trivial function, so we include it */
|
nuclear@0
|
87 static int tagcompare(const char *s1, const char *s2, int n){
|
nuclear@0
|
88 int c=0;
|
nuclear@0
|
89 while(c < n){
|
nuclear@0
|
90 if(toupper(s1[c]) != toupper(s2[c]))
|
nuclear@0
|
91 return !0;
|
nuclear@0
|
92 c++;
|
nuclear@0
|
93 }
|
nuclear@0
|
94 return 0;
|
nuclear@0
|
95 }
|
nuclear@0
|
96
|
nuclear@0
|
97 char *vorbis_comment_query(vorbis_comment *vc, const char *tag, int count){
|
nuclear@0
|
98 long i;
|
nuclear@0
|
99 int found = 0;
|
nuclear@0
|
100 int taglen = strlen(tag)+1; /* +1 for the = we append */
|
nuclear@0
|
101 char *fulltag = alloca(taglen+ 1);
|
nuclear@0
|
102
|
nuclear@0
|
103 strcpy(fulltag, tag);
|
nuclear@0
|
104 strcat(fulltag, "=");
|
nuclear@0
|
105
|
nuclear@0
|
106 for(i=0;i<vc->comments;i++){
|
nuclear@0
|
107 if(!tagcompare(vc->user_comments[i], fulltag, taglen)){
|
nuclear@0
|
108 if(count == found)
|
nuclear@0
|
109 /* We return a pointer to the data, not a copy */
|
nuclear@0
|
110 return vc->user_comments[i] + taglen;
|
nuclear@0
|
111 else
|
nuclear@0
|
112 found++;
|
nuclear@0
|
113 }
|
nuclear@0
|
114 }
|
nuclear@0
|
115 return NULL; /* didn't find anything */
|
nuclear@0
|
116 }
|
nuclear@0
|
117
|
nuclear@0
|
118 int vorbis_comment_query_count(vorbis_comment *vc, const char *tag){
|
nuclear@0
|
119 int i,count=0;
|
nuclear@0
|
120 int taglen = strlen(tag)+1; /* +1 for the = we append */
|
nuclear@0
|
121 char *fulltag = alloca(taglen+1);
|
nuclear@0
|
122 strcpy(fulltag,tag);
|
nuclear@0
|
123 strcat(fulltag, "=");
|
nuclear@0
|
124
|
nuclear@0
|
125 for(i=0;i<vc->comments;i++){
|
nuclear@0
|
126 if(!tagcompare(vc->user_comments[i], fulltag, taglen))
|
nuclear@0
|
127 count++;
|
nuclear@0
|
128 }
|
nuclear@0
|
129
|
nuclear@0
|
130 return count;
|
nuclear@0
|
131 }
|
nuclear@0
|
132
|
nuclear@0
|
133 void vorbis_comment_clear(vorbis_comment *vc){
|
nuclear@0
|
134 if(vc){
|
nuclear@0
|
135 long i;
|
nuclear@0
|
136 if(vc->user_comments){
|
nuclear@0
|
137 for(i=0;i<vc->comments;i++)
|
nuclear@0
|
138 if(vc->user_comments[i])_ogg_free(vc->user_comments[i]);
|
nuclear@0
|
139 _ogg_free(vc->user_comments);
|
nuclear@0
|
140 }
|
nuclear@0
|
141 if(vc->comment_lengths)_ogg_free(vc->comment_lengths);
|
nuclear@0
|
142 if(vc->vendor)_ogg_free(vc->vendor);
|
nuclear@0
|
143 memset(vc,0,sizeof(*vc));
|
nuclear@0
|
144 }
|
nuclear@0
|
145 }
|
nuclear@0
|
146
|
nuclear@0
|
147 /* blocksize 0 is guaranteed to be short, 1 is guaranteed to be long.
|
nuclear@0
|
148 They may be equal, but short will never ge greater than long */
|
nuclear@0
|
149 int vorbis_info_blocksize(vorbis_info *vi,int zo){
|
nuclear@0
|
150 codec_setup_info *ci = vi->codec_setup;
|
nuclear@0
|
151 return ci ? ci->blocksizes[zo] : -1;
|
nuclear@0
|
152 }
|
nuclear@0
|
153
|
nuclear@0
|
154 /* used by synthesis, which has a full, alloced vi */
|
nuclear@0
|
155 void vorbis_info_init(vorbis_info *vi){
|
nuclear@0
|
156 memset(vi,0,sizeof(*vi));
|
nuclear@0
|
157 vi->codec_setup=_ogg_calloc(1,sizeof(codec_setup_info));
|
nuclear@0
|
158 }
|
nuclear@0
|
159
|
nuclear@0
|
160 void vorbis_info_clear(vorbis_info *vi){
|
nuclear@0
|
161 codec_setup_info *ci=vi->codec_setup;
|
nuclear@0
|
162 int i;
|
nuclear@0
|
163
|
nuclear@0
|
164 if(ci){
|
nuclear@0
|
165
|
nuclear@0
|
166 for(i=0;i<ci->modes;i++)
|
nuclear@0
|
167 if(ci->mode_param[i])_ogg_free(ci->mode_param[i]);
|
nuclear@0
|
168
|
nuclear@0
|
169 for(i=0;i<ci->maps;i++) /* unpack does the range checking */
|
nuclear@0
|
170 if(ci->map_param[i]) /* this may be cleaning up an aborted
|
nuclear@0
|
171 unpack, in which case the below type
|
nuclear@0
|
172 cannot be trusted */
|
nuclear@0
|
173 _mapping_P[ci->map_type[i]]->free_info(ci->map_param[i]);
|
nuclear@0
|
174
|
nuclear@0
|
175 for(i=0;i<ci->floors;i++) /* unpack does the range checking */
|
nuclear@0
|
176 if(ci->floor_param[i]) /* this may be cleaning up an aborted
|
nuclear@0
|
177 unpack, in which case the below type
|
nuclear@0
|
178 cannot be trusted */
|
nuclear@0
|
179 _floor_P[ci->floor_type[i]]->free_info(ci->floor_param[i]);
|
nuclear@0
|
180
|
nuclear@0
|
181 for(i=0;i<ci->residues;i++) /* unpack does the range checking */
|
nuclear@0
|
182 if(ci->residue_param[i]) /* this may be cleaning up an aborted
|
nuclear@0
|
183 unpack, in which case the below type
|
nuclear@0
|
184 cannot be trusted */
|
nuclear@0
|
185 _residue_P[ci->residue_type[i]]->free_info(ci->residue_param[i]);
|
nuclear@0
|
186
|
nuclear@0
|
187 for(i=0;i<ci->books;i++){
|
nuclear@0
|
188 if(ci->book_param[i]){
|
nuclear@0
|
189 /* knows if the book was not alloced */
|
nuclear@0
|
190 vorbis_staticbook_destroy(ci->book_param[i]);
|
nuclear@0
|
191 }
|
nuclear@0
|
192 if(ci->fullbooks)
|
nuclear@0
|
193 vorbis_book_clear(ci->fullbooks+i);
|
nuclear@0
|
194 }
|
nuclear@0
|
195 if(ci->fullbooks)
|
nuclear@0
|
196 _ogg_free(ci->fullbooks);
|
nuclear@0
|
197
|
nuclear@0
|
198 for(i=0;i<ci->psys;i++)
|
nuclear@0
|
199 _vi_psy_free(ci->psy_param[i]);
|
nuclear@0
|
200
|
nuclear@0
|
201 _ogg_free(ci);
|
nuclear@0
|
202 }
|
nuclear@0
|
203
|
nuclear@0
|
204 memset(vi,0,sizeof(*vi));
|
nuclear@0
|
205 }
|
nuclear@0
|
206
|
nuclear@0
|
207 /* Header packing/unpacking ********************************************/
|
nuclear@0
|
208
|
nuclear@0
|
209 static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
|
nuclear@0
|
210 codec_setup_info *ci=vi->codec_setup;
|
nuclear@0
|
211 if(!ci)return(OV_EFAULT);
|
nuclear@0
|
212
|
nuclear@0
|
213 vi->version=oggpack_read(opb,32);
|
nuclear@0
|
214 if(vi->version!=0)return(OV_EVERSION);
|
nuclear@0
|
215
|
nuclear@0
|
216 vi->channels=oggpack_read(opb,8);
|
nuclear@0
|
217 vi->rate=oggpack_read(opb,32);
|
nuclear@0
|
218
|
nuclear@0
|
219 vi->bitrate_upper=oggpack_read(opb,32);
|
nuclear@0
|
220 vi->bitrate_nominal=oggpack_read(opb,32);
|
nuclear@0
|
221 vi->bitrate_lower=oggpack_read(opb,32);
|
nuclear@0
|
222
|
nuclear@0
|
223 ci->blocksizes[0]=1<<oggpack_read(opb,4);
|
nuclear@0
|
224 ci->blocksizes[1]=1<<oggpack_read(opb,4);
|
nuclear@0
|
225
|
nuclear@0
|
226 if(vi->rate<1)goto err_out;
|
nuclear@0
|
227 if(vi->channels<1)goto err_out;
|
nuclear@0
|
228 if(ci->blocksizes[0]<64)goto err_out;
|
nuclear@0
|
229 if(ci->blocksizes[1]<ci->blocksizes[0])goto err_out;
|
nuclear@0
|
230 if(ci->blocksizes[1]>8192)goto err_out;
|
nuclear@0
|
231
|
nuclear@0
|
232 if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
|
nuclear@0
|
233
|
nuclear@0
|
234 return(0);
|
nuclear@0
|
235 err_out:
|
nuclear@0
|
236 vorbis_info_clear(vi);
|
nuclear@0
|
237 return(OV_EBADHEADER);
|
nuclear@0
|
238 }
|
nuclear@0
|
239
|
nuclear@0
|
240 static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
|
nuclear@0
|
241 int i;
|
nuclear@0
|
242 int vendorlen=oggpack_read(opb,32);
|
nuclear@0
|
243 if(vendorlen<0)goto err_out;
|
nuclear@0
|
244 if(vendorlen>opb->storage-8)goto err_out;
|
nuclear@0
|
245 vc->vendor=_ogg_calloc(vendorlen+1,1);
|
nuclear@0
|
246 _v_readstring(opb,vc->vendor,vendorlen);
|
nuclear@0
|
247 i=oggpack_read(opb,32);
|
nuclear@0
|
248 if(i<0)goto err_out;
|
nuclear@0
|
249 if(i>((opb->storage-oggpack_bytes(opb))>>2))goto err_out;
|
nuclear@0
|
250 vc->comments=i;
|
nuclear@0
|
251 vc->user_comments=_ogg_calloc(vc->comments+1,sizeof(*vc->user_comments));
|
nuclear@0
|
252 vc->comment_lengths=_ogg_calloc(vc->comments+1, sizeof(*vc->comment_lengths));
|
nuclear@0
|
253
|
nuclear@0
|
254 for(i=0;i<vc->comments;i++){
|
nuclear@0
|
255 int len=oggpack_read(opb,32);
|
nuclear@0
|
256 if(len<0)goto err_out;
|
nuclear@0
|
257 if(len>opb->storage-oggpack_bytes(opb))goto err_out;
|
nuclear@0
|
258 vc->comment_lengths[i]=len;
|
nuclear@0
|
259 vc->user_comments[i]=_ogg_calloc(len+1,1);
|
nuclear@0
|
260 _v_readstring(opb,vc->user_comments[i],len);
|
nuclear@0
|
261 }
|
nuclear@0
|
262 if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
|
nuclear@0
|
263
|
nuclear@0
|
264 return(0);
|
nuclear@0
|
265 err_out:
|
nuclear@0
|
266 vorbis_comment_clear(vc);
|
nuclear@0
|
267 return(OV_EBADHEADER);
|
nuclear@0
|
268 }
|
nuclear@0
|
269
|
nuclear@0
|
270 /* all of the real encoding details are here. The modes, books,
|
nuclear@0
|
271 everything */
|
nuclear@0
|
272 static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
|
nuclear@0
|
273 codec_setup_info *ci=vi->codec_setup;
|
nuclear@0
|
274 int i;
|
nuclear@0
|
275 if(!ci)return(OV_EFAULT);
|
nuclear@0
|
276
|
nuclear@0
|
277 /* codebooks */
|
nuclear@0
|
278 ci->books=oggpack_read(opb,8)+1;
|
nuclear@0
|
279 if(ci->books<=0)goto err_out;
|
nuclear@0
|
280 for(i=0;i<ci->books;i++){
|
nuclear@0
|
281 ci->book_param[i]=vorbis_staticbook_unpack(opb);
|
nuclear@0
|
282 if(!ci->book_param[i])goto err_out;
|
nuclear@0
|
283 }
|
nuclear@0
|
284
|
nuclear@0
|
285 /* time backend settings; hooks are unused */
|
nuclear@0
|
286 {
|
nuclear@0
|
287 int times=oggpack_read(opb,6)+1;
|
nuclear@0
|
288 if(times<=0)goto err_out;
|
nuclear@0
|
289 for(i=0;i<times;i++){
|
nuclear@0
|
290 int test=oggpack_read(opb,16);
|
nuclear@0
|
291 if(test<0 || test>=VI_TIMEB)goto err_out;
|
nuclear@0
|
292 }
|
nuclear@0
|
293 }
|
nuclear@0
|
294
|
nuclear@0
|
295 /* floor backend settings */
|
nuclear@0
|
296 ci->floors=oggpack_read(opb,6)+1;
|
nuclear@0
|
297 if(ci->floors<=0)goto err_out;
|
nuclear@0
|
298 for(i=0;i<ci->floors;i++){
|
nuclear@0
|
299 ci->floor_type[i]=oggpack_read(opb,16);
|
nuclear@0
|
300 if(ci->floor_type[i]<0 || ci->floor_type[i]>=VI_FLOORB)goto err_out;
|
nuclear@0
|
301 ci->floor_param[i]=_floor_P[ci->floor_type[i]]->unpack(vi,opb);
|
nuclear@0
|
302 if(!ci->floor_param[i])goto err_out;
|
nuclear@0
|
303 }
|
nuclear@0
|
304
|
nuclear@0
|
305 /* residue backend settings */
|
nuclear@0
|
306 ci->residues=oggpack_read(opb,6)+1;
|
nuclear@0
|
307 if(ci->residues<=0)goto err_out;
|
nuclear@0
|
308 for(i=0;i<ci->residues;i++){
|
nuclear@0
|
309 ci->residue_type[i]=oggpack_read(opb,16);
|
nuclear@0
|
310 if(ci->residue_type[i]<0 || ci->residue_type[i]>=VI_RESB)goto err_out;
|
nuclear@0
|
311 ci->residue_param[i]=_residue_P[ci->residue_type[i]]->unpack(vi,opb);
|
nuclear@0
|
312 if(!ci->residue_param[i])goto err_out;
|
nuclear@0
|
313 }
|
nuclear@0
|
314
|
nuclear@0
|
315 /* map backend settings */
|
nuclear@0
|
316 ci->maps=oggpack_read(opb,6)+1;
|
nuclear@0
|
317 if(ci->maps<=0)goto err_out;
|
nuclear@0
|
318 for(i=0;i<ci->maps;i++){
|
nuclear@0
|
319 ci->map_type[i]=oggpack_read(opb,16);
|
nuclear@0
|
320 if(ci->map_type[i]<0 || ci->map_type[i]>=VI_MAPB)goto err_out;
|
nuclear@0
|
321 ci->map_param[i]=_mapping_P[ci->map_type[i]]->unpack(vi,opb);
|
nuclear@0
|
322 if(!ci->map_param[i])goto err_out;
|
nuclear@0
|
323 }
|
nuclear@0
|
324
|
nuclear@0
|
325 /* mode settings */
|
nuclear@0
|
326 ci->modes=oggpack_read(opb,6)+1;
|
nuclear@0
|
327 if(ci->modes<=0)goto err_out;
|
nuclear@0
|
328 for(i=0;i<ci->modes;i++){
|
nuclear@0
|
329 ci->mode_param[i]=_ogg_calloc(1,sizeof(*ci->mode_param[i]));
|
nuclear@0
|
330 ci->mode_param[i]->blockflag=oggpack_read(opb,1);
|
nuclear@0
|
331 ci->mode_param[i]->windowtype=oggpack_read(opb,16);
|
nuclear@0
|
332 ci->mode_param[i]->transformtype=oggpack_read(opb,16);
|
nuclear@0
|
333 ci->mode_param[i]->mapping=oggpack_read(opb,8);
|
nuclear@0
|
334
|
nuclear@0
|
335 if(ci->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
|
nuclear@0
|
336 if(ci->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
|
nuclear@0
|
337 if(ci->mode_param[i]->mapping>=ci->maps)goto err_out;
|
nuclear@0
|
338 if(ci->mode_param[i]->mapping<0)goto err_out;
|
nuclear@0
|
339 }
|
nuclear@0
|
340
|
nuclear@0
|
341 if(oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
|
nuclear@0
|
342
|
nuclear@0
|
343 return(0);
|
nuclear@0
|
344 err_out:
|
nuclear@0
|
345 vorbis_info_clear(vi);
|
nuclear@0
|
346 return(OV_EBADHEADER);
|
nuclear@0
|
347 }
|
nuclear@0
|
348
|
nuclear@0
|
349 /* Is this packet a vorbis ID header? */
|
nuclear@0
|
350 int vorbis_synthesis_idheader(ogg_packet *op){
|
nuclear@0
|
351 oggpack_buffer opb;
|
nuclear@0
|
352 char buffer[6];
|
nuclear@0
|
353
|
nuclear@0
|
354 if(op){
|
nuclear@0
|
355 oggpack_readinit(&opb,op->packet,op->bytes);
|
nuclear@0
|
356
|
nuclear@0
|
357 if(!op->b_o_s)
|
nuclear@0
|
358 return(0); /* Not the initial packet */
|
nuclear@0
|
359
|
nuclear@0
|
360 if(oggpack_read(&opb,8) != 1)
|
nuclear@0
|
361 return 0; /* not an ID header */
|
nuclear@0
|
362
|
nuclear@0
|
363 memset(buffer,0,6);
|
nuclear@0
|
364 _v_readstring(&opb,buffer,6);
|
nuclear@0
|
365 if(memcmp(buffer,"vorbis",6))
|
nuclear@0
|
366 return 0; /* not vorbis */
|
nuclear@0
|
367
|
nuclear@0
|
368 return 1;
|
nuclear@0
|
369 }
|
nuclear@0
|
370
|
nuclear@0
|
371 return 0;
|
nuclear@0
|
372 }
|
nuclear@0
|
373
|
nuclear@0
|
374 /* The Vorbis header is in three packets; the initial small packet in
|
nuclear@0
|
375 the first page that identifies basic parameters, a second packet
|
nuclear@0
|
376 with bitstream comments and a third packet that holds the
|
nuclear@0
|
377 codebook. */
|
nuclear@0
|
378
|
nuclear@0
|
379 int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
|
nuclear@0
|
380 oggpack_buffer opb;
|
nuclear@0
|
381
|
nuclear@0
|
382 if(op){
|
nuclear@0
|
383 oggpack_readinit(&opb,op->packet,op->bytes);
|
nuclear@0
|
384
|
nuclear@0
|
385 /* Which of the three types of header is this? */
|
nuclear@0
|
386 /* Also verify header-ness, vorbis */
|
nuclear@0
|
387 {
|
nuclear@0
|
388 char buffer[6];
|
nuclear@0
|
389 int packtype=oggpack_read(&opb,8);
|
nuclear@0
|
390 memset(buffer,0,6);
|
nuclear@0
|
391 _v_readstring(&opb,buffer,6);
|
nuclear@0
|
392 if(memcmp(buffer,"vorbis",6)){
|
nuclear@0
|
393 /* not a vorbis header */
|
nuclear@0
|
394 return(OV_ENOTVORBIS);
|
nuclear@0
|
395 }
|
nuclear@0
|
396 switch(packtype){
|
nuclear@0
|
397 case 0x01: /* least significant *bit* is read first */
|
nuclear@0
|
398 if(!op->b_o_s){
|
nuclear@0
|
399 /* Not the initial packet */
|
nuclear@0
|
400 return(OV_EBADHEADER);
|
nuclear@0
|
401 }
|
nuclear@0
|
402 if(vi->rate!=0){
|
nuclear@0
|
403 /* previously initialized info header */
|
nuclear@0
|
404 return(OV_EBADHEADER);
|
nuclear@0
|
405 }
|
nuclear@0
|
406
|
nuclear@0
|
407 return(_vorbis_unpack_info(vi,&opb));
|
nuclear@0
|
408
|
nuclear@0
|
409 case 0x03: /* least significant *bit* is read first */
|
nuclear@0
|
410 if(vi->rate==0){
|
nuclear@0
|
411 /* um... we didn't get the initial header */
|
nuclear@0
|
412 return(OV_EBADHEADER);
|
nuclear@0
|
413 }
|
nuclear@0
|
414
|
nuclear@0
|
415 return(_vorbis_unpack_comment(vc,&opb));
|
nuclear@0
|
416
|
nuclear@0
|
417 case 0x05: /* least significant *bit* is read first */
|
nuclear@0
|
418 if(vi->rate==0 || vc->vendor==NULL){
|
nuclear@0
|
419 /* um... we didn;t get the initial header or comments yet */
|
nuclear@0
|
420 return(OV_EBADHEADER);
|
nuclear@0
|
421 }
|
nuclear@0
|
422
|
nuclear@0
|
423 return(_vorbis_unpack_books(vi,&opb));
|
nuclear@0
|
424
|
nuclear@0
|
425 default:
|
nuclear@0
|
426 /* Not a valid vorbis header type */
|
nuclear@0
|
427 return(OV_EBADHEADER);
|
nuclear@0
|
428 break;
|
nuclear@0
|
429 }
|
nuclear@0
|
430 }
|
nuclear@0
|
431 }
|
nuclear@0
|
432 return(OV_EBADHEADER);
|
nuclear@0
|
433 }
|
nuclear@0
|
434
|
nuclear@0
|
435 /* pack side **********************************************************/
|
nuclear@0
|
436
|
nuclear@0
|
437 static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){
|
nuclear@0
|
438 codec_setup_info *ci=vi->codec_setup;
|
nuclear@0
|
439 if(!ci)return(OV_EFAULT);
|
nuclear@0
|
440
|
nuclear@0
|
441 /* preamble */
|
nuclear@0
|
442 oggpack_write(opb,0x01,8);
|
nuclear@0
|
443 _v_writestring(opb,"vorbis", 6);
|
nuclear@0
|
444
|
nuclear@0
|
445 /* basic information about the stream */
|
nuclear@0
|
446 oggpack_write(opb,0x00,32);
|
nuclear@0
|
447 oggpack_write(opb,vi->channels,8);
|
nuclear@0
|
448 oggpack_write(opb,vi->rate,32);
|
nuclear@0
|
449
|
nuclear@0
|
450 oggpack_write(opb,vi->bitrate_upper,32);
|
nuclear@0
|
451 oggpack_write(opb,vi->bitrate_nominal,32);
|
nuclear@0
|
452 oggpack_write(opb,vi->bitrate_lower,32);
|
nuclear@0
|
453
|
nuclear@0
|
454 oggpack_write(opb,ilog2(ci->blocksizes[0]),4);
|
nuclear@0
|
455 oggpack_write(opb,ilog2(ci->blocksizes[1]),4);
|
nuclear@0
|
456 oggpack_write(opb,1,1);
|
nuclear@0
|
457
|
nuclear@0
|
458 return(0);
|
nuclear@0
|
459 }
|
nuclear@0
|
460
|
nuclear@0
|
461 static int _vorbis_pack_comment(oggpack_buffer *opb,vorbis_comment *vc){
|
nuclear@0
|
462 int bytes = strlen(ENCODE_VENDOR_STRING);
|
nuclear@0
|
463
|
nuclear@0
|
464 /* preamble */
|
nuclear@0
|
465 oggpack_write(opb,0x03,8);
|
nuclear@0
|
466 _v_writestring(opb,"vorbis", 6);
|
nuclear@0
|
467
|
nuclear@0
|
468 /* vendor */
|
nuclear@0
|
469 oggpack_write(opb,bytes,32);
|
nuclear@0
|
470 _v_writestring(opb,ENCODE_VENDOR_STRING, bytes);
|
nuclear@0
|
471
|
nuclear@0
|
472 /* comments */
|
nuclear@0
|
473
|
nuclear@0
|
474 oggpack_write(opb,vc->comments,32);
|
nuclear@0
|
475 if(vc->comments){
|
nuclear@0
|
476 int i;
|
nuclear@0
|
477 for(i=0;i<vc->comments;i++){
|
nuclear@0
|
478 if(vc->user_comments[i]){
|
nuclear@0
|
479 oggpack_write(opb,vc->comment_lengths[i],32);
|
nuclear@0
|
480 _v_writestring(opb,vc->user_comments[i], vc->comment_lengths[i]);
|
nuclear@0
|
481 }else{
|
nuclear@0
|
482 oggpack_write(opb,0,32);
|
nuclear@0
|
483 }
|
nuclear@0
|
484 }
|
nuclear@0
|
485 }
|
nuclear@0
|
486 oggpack_write(opb,1,1);
|
nuclear@0
|
487
|
nuclear@0
|
488 return(0);
|
nuclear@0
|
489 }
|
nuclear@0
|
490
|
nuclear@0
|
491 static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){
|
nuclear@0
|
492 codec_setup_info *ci=vi->codec_setup;
|
nuclear@0
|
493 int i;
|
nuclear@0
|
494 if(!ci)return(OV_EFAULT);
|
nuclear@0
|
495
|
nuclear@0
|
496 oggpack_write(opb,0x05,8);
|
nuclear@0
|
497 _v_writestring(opb,"vorbis", 6);
|
nuclear@0
|
498
|
nuclear@0
|
499 /* books */
|
nuclear@0
|
500 oggpack_write(opb,ci->books-1,8);
|
nuclear@0
|
501 for(i=0;i<ci->books;i++)
|
nuclear@0
|
502 if(vorbis_staticbook_pack(ci->book_param[i],opb))goto err_out;
|
nuclear@0
|
503
|
nuclear@0
|
504 /* times; hook placeholders */
|
nuclear@0
|
505 oggpack_write(opb,0,6);
|
nuclear@0
|
506 oggpack_write(opb,0,16);
|
nuclear@0
|
507
|
nuclear@0
|
508 /* floors */
|
nuclear@0
|
509 oggpack_write(opb,ci->floors-1,6);
|
nuclear@0
|
510 for(i=0;i<ci->floors;i++){
|
nuclear@0
|
511 oggpack_write(opb,ci->floor_type[i],16);
|
nuclear@0
|
512 if(_floor_P[ci->floor_type[i]]->pack)
|
nuclear@0
|
513 _floor_P[ci->floor_type[i]]->pack(ci->floor_param[i],opb);
|
nuclear@0
|
514 else
|
nuclear@0
|
515 goto err_out;
|
nuclear@0
|
516 }
|
nuclear@0
|
517
|
nuclear@0
|
518 /* residues */
|
nuclear@0
|
519 oggpack_write(opb,ci->residues-1,6);
|
nuclear@0
|
520 for(i=0;i<ci->residues;i++){
|
nuclear@0
|
521 oggpack_write(opb,ci->residue_type[i],16);
|
nuclear@0
|
522 _residue_P[ci->residue_type[i]]->pack(ci->residue_param[i],opb);
|
nuclear@0
|
523 }
|
nuclear@0
|
524
|
nuclear@0
|
525 /* maps */
|
nuclear@0
|
526 oggpack_write(opb,ci->maps-1,6);
|
nuclear@0
|
527 for(i=0;i<ci->maps;i++){
|
nuclear@0
|
528 oggpack_write(opb,ci->map_type[i],16);
|
nuclear@0
|
529 _mapping_P[ci->map_type[i]]->pack(vi,ci->map_param[i],opb);
|
nuclear@0
|
530 }
|
nuclear@0
|
531
|
nuclear@0
|
532 /* modes */
|
nuclear@0
|
533 oggpack_write(opb,ci->modes-1,6);
|
nuclear@0
|
534 for(i=0;i<ci->modes;i++){
|
nuclear@0
|
535 oggpack_write(opb,ci->mode_param[i]->blockflag,1);
|
nuclear@0
|
536 oggpack_write(opb,ci->mode_param[i]->windowtype,16);
|
nuclear@0
|
537 oggpack_write(opb,ci->mode_param[i]->transformtype,16);
|
nuclear@0
|
538 oggpack_write(opb,ci->mode_param[i]->mapping,8);
|
nuclear@0
|
539 }
|
nuclear@0
|
540 oggpack_write(opb,1,1);
|
nuclear@0
|
541
|
nuclear@0
|
542 return(0);
|
nuclear@0
|
543 err_out:
|
nuclear@0
|
544 return(-1);
|
nuclear@0
|
545 }
|
nuclear@0
|
546
|
nuclear@0
|
547 int vorbis_commentheader_out(vorbis_comment *vc,
|
nuclear@0
|
548 ogg_packet *op){
|
nuclear@0
|
549
|
nuclear@0
|
550 oggpack_buffer opb;
|
nuclear@0
|
551
|
nuclear@0
|
552 oggpack_writeinit(&opb);
|
nuclear@0
|
553 if(_vorbis_pack_comment(&opb,vc)){
|
nuclear@0
|
554 oggpack_writeclear(&opb);
|
nuclear@0
|
555 return OV_EIMPL;
|
nuclear@0
|
556 }
|
nuclear@0
|
557
|
nuclear@0
|
558 op->packet = _ogg_malloc(oggpack_bytes(&opb));
|
nuclear@0
|
559 memcpy(op->packet, opb.buffer, oggpack_bytes(&opb));
|
nuclear@0
|
560
|
nuclear@0
|
561 op->bytes=oggpack_bytes(&opb);
|
nuclear@0
|
562 op->b_o_s=0;
|
nuclear@0
|
563 op->e_o_s=0;
|
nuclear@0
|
564 op->granulepos=0;
|
nuclear@0
|
565 op->packetno=1;
|
nuclear@0
|
566
|
nuclear@0
|
567 oggpack_writeclear(&opb);
|
nuclear@0
|
568 return 0;
|
nuclear@0
|
569 }
|
nuclear@0
|
570
|
nuclear@0
|
571 int vorbis_analysis_headerout(vorbis_dsp_state *v,
|
nuclear@0
|
572 vorbis_comment *vc,
|
nuclear@0
|
573 ogg_packet *op,
|
nuclear@0
|
574 ogg_packet *op_comm,
|
nuclear@0
|
575 ogg_packet *op_code){
|
nuclear@0
|
576 int ret=OV_EIMPL;
|
nuclear@0
|
577 vorbis_info *vi=v->vi;
|
nuclear@0
|
578 oggpack_buffer opb;
|
nuclear@0
|
579 private_state *b=v->backend_state;
|
nuclear@0
|
580
|
nuclear@0
|
581 if(!b){
|
nuclear@0
|
582 ret=OV_EFAULT;
|
nuclear@0
|
583 goto err_out;
|
nuclear@0
|
584 }
|
nuclear@0
|
585
|
nuclear@0
|
586 /* first header packet **********************************************/
|
nuclear@0
|
587
|
nuclear@0
|
588 oggpack_writeinit(&opb);
|
nuclear@0
|
589 if(_vorbis_pack_info(&opb,vi))goto err_out;
|
nuclear@0
|
590
|
nuclear@0
|
591 /* build the packet */
|
nuclear@0
|
592 if(b->header)_ogg_free(b->header);
|
nuclear@0
|
593 b->header=_ogg_malloc(oggpack_bytes(&opb));
|
nuclear@0
|
594 memcpy(b->header,opb.buffer,oggpack_bytes(&opb));
|
nuclear@0
|
595 op->packet=b->header;
|
nuclear@0
|
596 op->bytes=oggpack_bytes(&opb);
|
nuclear@0
|
597 op->b_o_s=1;
|
nuclear@0
|
598 op->e_o_s=0;
|
nuclear@0
|
599 op->granulepos=0;
|
nuclear@0
|
600 op->packetno=0;
|
nuclear@0
|
601
|
nuclear@0
|
602 /* second header packet (comments) **********************************/
|
nuclear@0
|
603
|
nuclear@0
|
604 oggpack_reset(&opb);
|
nuclear@0
|
605 if(_vorbis_pack_comment(&opb,vc))goto err_out;
|
nuclear@0
|
606
|
nuclear@0
|
607 if(b->header1)_ogg_free(b->header1);
|
nuclear@0
|
608 b->header1=_ogg_malloc(oggpack_bytes(&opb));
|
nuclear@0
|
609 memcpy(b->header1,opb.buffer,oggpack_bytes(&opb));
|
nuclear@0
|
610 op_comm->packet=b->header1;
|
nuclear@0
|
611 op_comm->bytes=oggpack_bytes(&opb);
|
nuclear@0
|
612 op_comm->b_o_s=0;
|
nuclear@0
|
613 op_comm->e_o_s=0;
|
nuclear@0
|
614 op_comm->granulepos=0;
|
nuclear@0
|
615 op_comm->packetno=1;
|
nuclear@0
|
616
|
nuclear@0
|
617 /* third header packet (modes/codebooks) ****************************/
|
nuclear@0
|
618
|
nuclear@0
|
619 oggpack_reset(&opb);
|
nuclear@0
|
620 if(_vorbis_pack_books(&opb,vi))goto err_out;
|
nuclear@0
|
621
|
nuclear@0
|
622 if(b->header2)_ogg_free(b->header2);
|
nuclear@0
|
623 b->header2=_ogg_malloc(oggpack_bytes(&opb));
|
nuclear@0
|
624 memcpy(b->header2,opb.buffer,oggpack_bytes(&opb));
|
nuclear@0
|
625 op_code->packet=b->header2;
|
nuclear@0
|
626 op_code->bytes=oggpack_bytes(&opb);
|
nuclear@0
|
627 op_code->b_o_s=0;
|
nuclear@0
|
628 op_code->e_o_s=0;
|
nuclear@0
|
629 op_code->granulepos=0;
|
nuclear@0
|
630 op_code->packetno=2;
|
nuclear@0
|
631
|
nuclear@0
|
632 oggpack_writeclear(&opb);
|
nuclear@0
|
633 return(0);
|
nuclear@0
|
634 err_out:
|
nuclear@0
|
635 memset(op,0,sizeof(*op));
|
nuclear@0
|
636 memset(op_comm,0,sizeof(*op_comm));
|
nuclear@0
|
637 memset(op_code,0,sizeof(*op_code));
|
nuclear@0
|
638
|
nuclear@0
|
639 if(b){
|
nuclear@0
|
640 oggpack_writeclear(&opb);
|
nuclear@0
|
641 if(b->header)_ogg_free(b->header);
|
nuclear@0
|
642 if(b->header1)_ogg_free(b->header1);
|
nuclear@0
|
643 if(b->header2)_ogg_free(b->header2);
|
nuclear@0
|
644 b->header=NULL;
|
nuclear@0
|
645 b->header1=NULL;
|
nuclear@0
|
646 b->header2=NULL;
|
nuclear@0
|
647 }
|
nuclear@0
|
648 return(ret);
|
nuclear@0
|
649 }
|
nuclear@0
|
650
|
nuclear@0
|
651 double vorbis_granule_time(vorbis_dsp_state *v,ogg_int64_t granulepos){
|
nuclear@0
|
652 if(granulepos == -1) return -1;
|
nuclear@0
|
653
|
nuclear@0
|
654 /* We're not guaranteed a 64 bit unsigned type everywhere, so we
|
nuclear@0
|
655 have to put the unsigned granpo in a signed type. */
|
nuclear@0
|
656 if(granulepos>=0){
|
nuclear@0
|
657 return((double)granulepos/v->vi->rate);
|
nuclear@0
|
658 }else{
|
nuclear@0
|
659 ogg_int64_t granuleoff=0xffffffff;
|
nuclear@0
|
660 granuleoff<<=31;
|
nuclear@0
|
661 granuleoff|=0x7ffffffff;
|
nuclear@0
|
662 return(((double)granulepos+2+granuleoff+granuleoff)/v->vi->rate);
|
nuclear@0
|
663 }
|
nuclear@0
|
664 }
|
nuclear@0
|
665
|
nuclear@0
|
666 const char *vorbis_version_string(void){
|
nuclear@0
|
667 return GENERAL_VENDOR_STRING;
|
nuclear@0
|
668 }
|