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