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