1 /********************************************************************
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. *
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2007 *
9 * by the Xiph.Org Foundation http://www.xiph.org/ *
11 ********************************************************************
13 function: maintain the info structure, info <-> header packets
16 ********************************************************************/
18 /* general handling of the header and the vorbis_info structure (and
25 #include "vorbis/codec.h"
26 #include "codec_internal.h"
35 static int ilog2(unsigned int v){
45 static void _v_writestring(oggpack_buffer *o,const char *s, int bytes){
48 oggpack_write(o,*s++,8);
52 static void _v_readstring(oggpack_buffer *o,char *buf,int bytes){
54 *buf++=oggpack_read(o,8);
58 void vorbis_comment_init(vorbis_comment *vc){
59 memset(vc,0,sizeof(*vc));
62 void vorbis_comment_add(vorbis_comment *vc,const char *comment){
63 vc->user_comments=_ogg_realloc(vc->user_comments,
64 (vc->comments+2)*sizeof(*vc->user_comments));
65 vc->comment_lengths=_ogg_realloc(vc->comment_lengths,
66 (vc->comments+2)*sizeof(*vc->comment_lengths));
67 vc->comment_lengths[vc->comments]=strlen(comment);
68 vc->user_comments[vc->comments]=_ogg_malloc(vc->comment_lengths[vc->comments]+1);
69 strcpy(vc->user_comments[vc->comments], comment);
71 vc->user_comments[vc->comments]=NULL;
74 void vorbis_comment_add_tag(vorbis_comment *vc, const char *tag, const char *contents){
75 char *comment=alloca(strlen(tag)+strlen(contents)+2); /* +2 for = and \0 */
78 strcat(comment, contents);
79 vorbis_comment_add(vc, comment);
82 /* This is more or less the same as strncasecmp - but that doesn't exist
83 * everywhere, and this is a fairly trivial function, so we include it */
84 static int tagcompare(const char *s1, const char *s2, int n){
87 if(toupper(s1[c]) != toupper(s2[c]))
94 char *vorbis_comment_query(vorbis_comment *vc, const char *tag, int count){
97 int taglen = strlen(tag)+1; /* +1 for the = we append */
98 char *fulltag = alloca(taglen+ 1);
100 strcpy(fulltag, tag);
101 strcat(fulltag, "=");
103 for(i=0;i<vc->comments;i++){
104 if(!tagcompare(vc->user_comments[i], fulltag, taglen)){
106 /* We return a pointer to the data, not a copy */
107 return vc->user_comments[i] + taglen;
112 return NULL; /* didn't find anything */
115 int vorbis_comment_query_count(vorbis_comment *vc, const char *tag){
117 int taglen = strlen(tag)+1; /* +1 for the = we append */
118 char *fulltag = alloca(taglen+1);
120 strcat(fulltag, "=");
122 for(i=0;i<vc->comments;i++){
123 if(!tagcompare(vc->user_comments[i], fulltag, taglen))
130 void vorbis_comment_clear(vorbis_comment *vc){
133 if(vc->user_comments){
134 for(i=0;i<vc->comments;i++)
135 if(vc->user_comments[i])_ogg_free(vc->user_comments[i]);
136 _ogg_free(vc->user_comments);
138 if(vc->comment_lengths)_ogg_free(vc->comment_lengths);
139 if(vc->vendor)_ogg_free(vc->vendor);
140 memset(vc,0,sizeof(*vc));
144 /* blocksize 0 is guaranteed to be short, 1 is guaranteed to be long.
145 They may be equal, but short will never ge greater than long */
146 int vorbis_info_blocksize(vorbis_info *vi,int zo){
147 codec_setup_info *ci = vi->codec_setup;
148 return ci ? ci->blocksizes[zo] : -1;
151 /* used by synthesis, which has a full, alloced vi */
152 void vorbis_info_init(vorbis_info *vi){
153 memset(vi,0,sizeof(*vi));
154 vi->codec_setup=_ogg_calloc(1,sizeof(codec_setup_info));
157 void vorbis_info_clear(vorbis_info *vi){
158 codec_setup_info *ci=vi->codec_setup;
163 for(i=0;i<ci->modes;i++)
164 if(ci->mode_param[i])_ogg_free(ci->mode_param[i]);
166 for(i=0;i<ci->maps;i++) /* unpack does the range checking */
167 if(ci->map_param[i]) /* this may be cleaning up an aborted
168 unpack, in which case the below type
170 _mapping_P[ci->map_type[i]]->free_info(ci->map_param[i]);
172 for(i=0;i<ci->floors;i++) /* unpack does the range checking */
173 if(ci->floor_param[i]) /* this may be cleaning up an aborted
174 unpack, in which case the below type
176 _floor_P[ci->floor_type[i]]->free_info(ci->floor_param[i]);
178 for(i=0;i<ci->residues;i++) /* unpack does the range checking */
179 if(ci->residue_param[i]) /* this may be cleaning up an aborted
180 unpack, in which case the below type
182 _residue_P[ci->residue_type[i]]->free_info(ci->residue_param[i]);
184 for(i=0;i<ci->books;i++){
185 if(ci->book_param[i]){
186 /* knows if the book was not alloced */
187 vorbis_staticbook_destroy(ci->book_param[i]);
190 vorbis_book_clear(ci->fullbooks+i);
193 _ogg_free(ci->fullbooks);
195 for(i=0;i<ci->psys;i++)
196 _vi_psy_free(ci->psy_param[i]);
201 memset(vi,0,sizeof(*vi));
204 /* Header packing/unpacking ********************************************/
206 static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
207 codec_setup_info *ci=vi->codec_setup;
208 if(!ci)return(OV_EFAULT);
210 vi->version=oggpack_read(opb,32);
211 if(vi->version!=0)return(OV_EVERSION);
213 vi->channels=oggpack_read(opb,8);
214 vi->rate=oggpack_read(opb,32);
216 vi->bitrate_upper=oggpack_read(opb,32);
217 vi->bitrate_nominal=oggpack_read(opb,32);
218 vi->bitrate_lower=oggpack_read(opb,32);
220 ci->blocksizes[0]=1<<oggpack_read(opb,4);
221 ci->blocksizes[1]=1<<oggpack_read(opb,4);
223 if(vi->rate<1)goto err_out;
224 if(vi->channels<1)goto err_out;
225 if(ci->blocksizes[0]<64)goto err_out;
226 if(ci->blocksizes[1]<ci->blocksizes[0])goto err_out;
227 if(ci->blocksizes[1]>8192)goto err_out;
229 if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
233 vorbis_info_clear(vi);
234 return(OV_EBADHEADER);
237 static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
239 int vendorlen=oggpack_read(opb,32);
240 if(vendorlen<0)goto err_out;
241 if(vendorlen+8>opb->storage)goto err_out;
242 vc->vendor=_ogg_calloc(vendorlen+1,1);
243 _v_readstring(opb,vc->vendor,vendorlen);
244 i=oggpack_read(opb,32);
246 if(4*i+oggpack_bytes(opb)>opb->storage)goto err_out;
248 vc->user_comments=_ogg_calloc(vc->comments+1,sizeof(*vc->user_comments));
249 vc->comment_lengths=_ogg_calloc(vc->comments+1, sizeof(*vc->comment_lengths));
251 for(i=0;i<vc->comments;i++){
252 int len=oggpack_read(opb,32);
253 if(len<0)goto err_out;
254 if(len+oggpack_bytes(opb)>opb->storage)goto err_out;
255 vc->comment_lengths[i]=len;
256 vc->user_comments[i]=_ogg_calloc(len+1,1);
257 _v_readstring(opb,vc->user_comments[i],len);
259 if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
263 vorbis_comment_clear(vc);
264 return(OV_EBADHEADER);
267 /* all of the real encoding details are here. The modes, books,
269 static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
270 codec_setup_info *ci=vi->codec_setup;
272 if(!ci)return(OV_EFAULT);
275 ci->books=oggpack_read(opb,8)+1;
276 /*ci->book_param=_ogg_calloc(ci->books,sizeof(*ci->book_param));*/
277 for(i=0;i<ci->books;i++){
278 ci->book_param[i]=_ogg_calloc(1,sizeof(*ci->book_param[i]));
279 if(vorbis_staticbook_unpack(opb,ci->book_param[i]))goto err_out;
282 /* time backend settings; hooks are unused */
284 int times=oggpack_read(opb,6)+1;
285 for(i=0;i<times;i++){
286 int test=oggpack_read(opb,16);
287 if(test<0 || test>=VI_TIMEB)goto err_out;
291 /* floor backend settings */
292 ci->floors=oggpack_read(opb,6)+1;
293 /*ci->floor_type=_ogg_malloc(ci->floors*sizeof(*ci->floor_type));*/
294 /*ci->floor_param=_ogg_calloc(ci->floors,sizeof(void *));*/
295 for(i=0;i<ci->floors;i++){
296 ci->floor_type[i]=oggpack_read(opb,16);
297 if(ci->floor_type[i]<0 || ci->floor_type[i]>=VI_FLOORB)goto err_out;
298 ci->floor_param[i]=_floor_P[ci->floor_type[i]]->unpack(vi,opb);
299 if(!ci->floor_param[i])goto err_out;
302 /* residue backend settings */
303 ci->residues=oggpack_read(opb,6)+1;
304 /*ci->residue_type=_ogg_malloc(ci->residues*sizeof(*ci->residue_type));*/
305 /*ci->residue_param=_ogg_calloc(ci->residues,sizeof(void *));*/
306 for(i=0;i<ci->residues;i++){
307 ci->residue_type[i]=oggpack_read(opb,16);
308 if(ci->residue_type[i]<0 || ci->residue_type[i]>=VI_RESB)goto err_out;
309 ci->residue_param[i]=_residue_P[ci->residue_type[i]]->unpack(vi,opb);
310 if(!ci->residue_param[i])goto err_out;
313 /* map backend settings */
314 ci->maps=oggpack_read(opb,6)+1;
315 /*ci->map_type=_ogg_malloc(ci->maps*sizeof(*ci->map_type));*/
316 /*ci->map_param=_ogg_calloc(ci->maps,sizeof(void *));*/
317 for(i=0;i<ci->maps;i++){
318 ci->map_type[i]=oggpack_read(opb,16);
319 if(ci->map_type[i]<0 || ci->map_type[i]>=VI_MAPB)goto err_out;
320 ci->map_param[i]=_mapping_P[ci->map_type[i]]->unpack(vi,opb);
321 if(!ci->map_param[i])goto err_out;
325 ci->modes=oggpack_read(opb,6)+1;
326 /*vi->mode_param=_ogg_calloc(vi->modes,sizeof(void *));*/
327 for(i=0;i<ci->modes;i++){
328 ci->mode_param[i]=_ogg_calloc(1,sizeof(*ci->mode_param[i]));
329 ci->mode_param[i]->blockflag=oggpack_read(opb,1);
330 ci->mode_param[i]->windowtype=oggpack_read(opb,16);
331 ci->mode_param[i]->transformtype=oggpack_read(opb,16);
332 ci->mode_param[i]->mapping=oggpack_read(opb,8);
334 if(ci->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
335 if(ci->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
336 if(ci->mode_param[i]->mapping>=ci->maps)goto err_out;
339 if(oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
343 vorbis_info_clear(vi);
344 return(OV_EBADHEADER);
347 /* Is this packet a vorbis ID header? */
348 int vorbis_synthesis_idheader(ogg_packet *op){
353 oggpack_readinit(&opb,op->packet,op->bytes);
356 return(0); /* Not the initial packet */
358 if(oggpack_read(&opb,8) != 1)
359 return 0; /* not an ID header */
362 _v_readstring(&opb,buffer,6);
363 if(memcmp(buffer,"vorbis",6))
364 return 0; /* not vorbis */
372 /* The Vorbis header is in three packets; the initial small packet in
373 the first page that identifies basic parameters, a second packet
374 with bitstream comments and a third packet that holds the
377 int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
381 oggpack_readinit(&opb,op->packet,op->bytes);
383 /* Which of the three types of header is this? */
384 /* Also verify header-ness, vorbis */
387 int packtype=oggpack_read(&opb,8);
389 _v_readstring(&opb,buffer,6);
390 if(memcmp(buffer,"vorbis",6)){
391 /* not a vorbis header */
392 return(OV_ENOTVORBIS);
395 case 0x01: /* least significant *bit* is read first */
397 /* Not the initial packet */
398 return(OV_EBADHEADER);
401 /* previously initialized info header */
402 return(OV_EBADHEADER);
405 return(_vorbis_unpack_info(vi,&opb));
407 case 0x03: /* least significant *bit* is read first */
409 /* um... we didn't get the initial header */
410 return(OV_EBADHEADER);
413 return(_vorbis_unpack_comment(vc,&opb));
415 case 0x05: /* least significant *bit* is read first */
416 if(vi->rate==0 || vc->vendor==NULL){
417 /* um... we didn;t get the initial header or comments yet */
418 return(OV_EBADHEADER);
421 return(_vorbis_unpack_books(vi,&opb));
424 /* Not a valid vorbis header type */
425 return(OV_EBADHEADER);
430 return(OV_EBADHEADER);
433 /* pack side **********************************************************/
435 static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){
436 codec_setup_info *ci=vi->codec_setup;
437 if(!ci)return(OV_EFAULT);
440 oggpack_write(opb,0x01,8);
441 _v_writestring(opb,"vorbis", 6);
443 /* basic information about the stream */
444 oggpack_write(opb,0x00,32);
445 oggpack_write(opb,vi->channels,8);
446 oggpack_write(opb,vi->rate,32);
448 oggpack_write(opb,vi->bitrate_upper,32);
449 oggpack_write(opb,vi->bitrate_nominal,32);
450 oggpack_write(opb,vi->bitrate_lower,32);
452 oggpack_write(opb,ilog2(ci->blocksizes[0]),4);
453 oggpack_write(opb,ilog2(ci->blocksizes[1]),4);
454 oggpack_write(opb,1,1);
459 static int _vorbis_pack_comment(oggpack_buffer *opb,vorbis_comment *vc){
460 char temp[]="Xiph.Org libVorbis I 20080501";
461 int bytes = strlen(temp);
464 oggpack_write(opb,0x03,8);
465 _v_writestring(opb,"vorbis", 6);
468 oggpack_write(opb,bytes,32);
469 _v_writestring(opb,temp, bytes);
473 oggpack_write(opb,vc->comments,32);
476 for(i=0;i<vc->comments;i++){
477 if(vc->user_comments[i]){
478 oggpack_write(opb,vc->comment_lengths[i],32);
479 _v_writestring(opb,vc->user_comments[i], vc->comment_lengths[i]);
481 oggpack_write(opb,0,32);
485 oggpack_write(opb,1,1);
490 static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){
491 codec_setup_info *ci=vi->codec_setup;
493 if(!ci)return(OV_EFAULT);
495 oggpack_write(opb,0x05,8);
496 _v_writestring(opb,"vorbis", 6);
499 oggpack_write(opb,ci->books-1,8);
500 for(i=0;i<ci->books;i++)
501 if(vorbis_staticbook_pack(ci->book_param[i],opb))goto err_out;
503 /* times; hook placeholders */
504 oggpack_write(opb,0,6);
505 oggpack_write(opb,0,16);
508 oggpack_write(opb,ci->floors-1,6);
509 for(i=0;i<ci->floors;i++){
510 oggpack_write(opb,ci->floor_type[i],16);
511 if(_floor_P[ci->floor_type[i]]->pack)
512 _floor_P[ci->floor_type[i]]->pack(ci->floor_param[i],opb);
518 oggpack_write(opb,ci->residues-1,6);
519 for(i=0;i<ci->residues;i++){
520 oggpack_write(opb,ci->residue_type[i],16);
521 _residue_P[ci->residue_type[i]]->pack(ci->residue_param[i],opb);
525 oggpack_write(opb,ci->maps-1,6);
526 for(i=0;i<ci->maps;i++){
527 oggpack_write(opb,ci->map_type[i],16);
528 _mapping_P[ci->map_type[i]]->pack(vi,ci->map_param[i],opb);
532 oggpack_write(opb,ci->modes-1,6);
533 for(i=0;i<ci->modes;i++){
534 oggpack_write(opb,ci->mode_param[i]->blockflag,1);
535 oggpack_write(opb,ci->mode_param[i]->windowtype,16);
536 oggpack_write(opb,ci->mode_param[i]->transformtype,16);
537 oggpack_write(opb,ci->mode_param[i]->mapping,8);
539 oggpack_write(opb,1,1);
546 int vorbis_commentheader_out(vorbis_comment *vc,
551 oggpack_writeinit(&opb);
552 if(_vorbis_pack_comment(&opb,vc)) return OV_EIMPL;
554 op->packet = _ogg_malloc(oggpack_bytes(&opb));
555 memcpy(op->packet, opb.buffer, oggpack_bytes(&opb));
557 op->bytes=oggpack_bytes(&opb);
566 int vorbis_analysis_headerout(vorbis_dsp_state *v,
570 ogg_packet *op_code){
572 vorbis_info *vi=v->vi;
574 private_state *b=v->backend_state;
581 /* first header packet **********************************************/
583 oggpack_writeinit(&opb);
584 if(_vorbis_pack_info(&opb,vi))goto err_out;
586 /* build the packet */
587 if(b->header)_ogg_free(b->header);
588 b->header=_ogg_malloc(oggpack_bytes(&opb));
589 memcpy(b->header,opb.buffer,oggpack_bytes(&opb));
590 op->packet=b->header;
591 op->bytes=oggpack_bytes(&opb);
597 /* second header packet (comments) **********************************/
600 if(_vorbis_pack_comment(&opb,vc))goto err_out;
602 if(b->header1)_ogg_free(b->header1);
603 b->header1=_ogg_malloc(oggpack_bytes(&opb));
604 memcpy(b->header1,opb.buffer,oggpack_bytes(&opb));
605 op_comm->packet=b->header1;
606 op_comm->bytes=oggpack_bytes(&opb);
609 op_comm->granulepos=0;
612 /* third header packet (modes/codebooks) ****************************/
615 if(_vorbis_pack_books(&opb,vi))goto err_out;
617 if(b->header2)_ogg_free(b->header2);
618 b->header2=_ogg_malloc(oggpack_bytes(&opb));
619 memcpy(b->header2,opb.buffer,oggpack_bytes(&opb));
620 op_code->packet=b->header2;
621 op_code->bytes=oggpack_bytes(&opb);
624 op_code->granulepos=0;
627 oggpack_writeclear(&opb);
630 oggpack_writeclear(&opb);
631 memset(op,0,sizeof(*op));
632 memset(op_comm,0,sizeof(*op_comm));
633 memset(op_code,0,sizeof(*op_code));
636 if(b->header)_ogg_free(b->header);
637 if(b->header1)_ogg_free(b->header1);
638 if(b->header2)_ogg_free(b->header2);
646 double vorbis_granule_time(vorbis_dsp_state *v,ogg_int64_t granulepos){
648 return((double)granulepos/v->vi->rate);