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-2003 *
9 * by the XIPHOPHORUS Company http://www.xiph.org/ *
11 ********************************************************************
13 function: maintain the info structure, info <-> header packets
14 last mod: $Id: info.c,v 1.62 2003/09/10 01:10:18 xiphmont Exp $
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,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,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, char *tag, 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, 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, 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 for(i=0;i<vc->comments;i++)
134 if(vc->user_comments[i])_ogg_free(vc->user_comments[i]);
135 if(vc->user_comments)_ogg_free(vc->user_comments);
136 if(vc->comment_lengths)_ogg_free(vc->comment_lengths);
137 if(vc->vendor)_ogg_free(vc->vendor);
139 memset(vc,0,sizeof(*vc));
142 /* blocksize 0 is guaranteed to be short, 1 is guarantted to be long.
143 They may be equal, but short will never ge greater than long */
144 int vorbis_info_blocksize(vorbis_info *vi,int zo){
145 codec_setup_info *ci = vi->codec_setup;
146 return ci ? ci->blocksizes[zo] : -1;
149 /* used by synthesis, which has a full, alloced vi */
150 void vorbis_info_init(vorbis_info *vi){
151 memset(vi,0,sizeof(*vi));
152 vi->codec_setup=_ogg_calloc(1,sizeof(codec_setup_info));
155 void vorbis_info_clear(vorbis_info *vi){
156 codec_setup_info *ci=vi->codec_setup;
161 for(i=0;i<ci->modes;i++)
162 if(ci->mode_param[i])_ogg_free(ci->mode_param[i]);
164 for(i=0;i<ci->maps;i++) /* unpack does the range checking */
165 _mapping_P[ci->map_type[i]]->free_info(ci->map_param[i]);
167 for(i=0;i<ci->floors;i++) /* unpack does the range checking */
168 _floor_P[ci->floor_type[i]]->free_info(ci->floor_param[i]);
170 for(i=0;i<ci->residues;i++) /* unpack does the range checking */
171 _residue_P[ci->residue_type[i]]->free_info(ci->residue_param[i]);
173 for(i=0;i<ci->books;i++){
174 if(ci->book_param[i]){
175 /* knows if the book was not alloced */
176 vorbis_staticbook_destroy(ci->book_param[i]);
179 vorbis_book_clear(ci->fullbooks+i);
182 _ogg_free(ci->fullbooks);
184 for(i=0;i<ci->psys;i++)
185 _vi_psy_free(ci->psy_param[i]);
190 memset(vi,0,sizeof(*vi));
193 /* Header packing/unpacking ********************************************/
195 static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
196 codec_setup_info *ci=vi->codec_setup;
197 if(!ci)return(OV_EFAULT);
199 vi->version=oggpack_read(opb,32);
200 if(vi->version!=0)return(OV_EVERSION);
202 vi->channels=oggpack_read(opb,8);
203 vi->rate=oggpack_read(opb,32);
205 vi->bitrate_upper=oggpack_read(opb,32);
206 vi->bitrate_nominal=oggpack_read(opb,32);
207 vi->bitrate_lower=oggpack_read(opb,32);
209 ci->blocksizes[0]=1<<oggpack_read(opb,4);
210 ci->blocksizes[1]=1<<oggpack_read(opb,4);
212 if(vi->rate<1)goto err_out;
213 if(vi->channels<1)goto err_out;
214 if(ci->blocksizes[0]<8)goto err_out;
215 if(ci->blocksizes[1]<ci->blocksizes[0])goto err_out;
217 if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
221 vorbis_info_clear(vi);
222 return(OV_EBADHEADER);
225 static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
227 int vendorlen=oggpack_read(opb,32);
228 if(vendorlen<0)goto err_out;
229 vc->vendor=_ogg_calloc(vendorlen+1,1);
230 _v_readstring(opb,vc->vendor,vendorlen);
231 vc->comments=oggpack_read(opb,32);
232 if(vc->comments<0)goto err_out;
233 vc->user_comments=_ogg_calloc(vc->comments+1,sizeof(*vc->user_comments));
234 vc->comment_lengths=_ogg_calloc(vc->comments+1, sizeof(*vc->comment_lengths));
236 for(i=0;i<vc->comments;i++){
237 int len=oggpack_read(opb,32);
238 if(len<0)goto err_out;
239 vc->comment_lengths[i]=len;
240 vc->user_comments[i]=_ogg_calloc(len+1,1);
241 _v_readstring(opb,vc->user_comments[i],len);
243 if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
247 vorbis_comment_clear(vc);
248 return(OV_EBADHEADER);
251 /* all of the real encoding details are here. The modes, books,
253 static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
254 codec_setup_info *ci=vi->codec_setup;
256 if(!ci)return(OV_EFAULT);
259 ci->books=oggpack_read(opb,8)+1;
260 /*ci->book_param=_ogg_calloc(ci->books,sizeof(*ci->book_param));*/
261 for(i=0;i<ci->books;i++){
262 ci->book_param[i]=_ogg_calloc(1,sizeof(*ci->book_param[i]));
263 if(vorbis_staticbook_unpack(opb,ci->book_param[i]))goto err_out;
266 /* time backend settings; hooks are unused */
268 int times=oggpack_read(opb,6)+1;
269 for(i=0;i<times;i++){
270 int test=oggpack_read(opb,16);
271 if(test<0 || test>=VI_TIMEB)goto err_out;
275 /* floor backend settings */
276 ci->floors=oggpack_read(opb,6)+1;
277 /*ci->floor_type=_ogg_malloc(ci->floors*sizeof(*ci->floor_type));*/
278 /*ci->floor_param=_ogg_calloc(ci->floors,sizeof(void *));*/
279 for(i=0;i<ci->floors;i++){
280 ci->floor_type[i]=oggpack_read(opb,16);
281 if(ci->floor_type[i]<0 || ci->floor_type[i]>=VI_FLOORB)goto err_out;
282 ci->floor_param[i]=_floor_P[ci->floor_type[i]]->unpack(vi,opb);
283 if(!ci->floor_param[i])goto err_out;
286 /* residue backend settings */
287 ci->residues=oggpack_read(opb,6)+1;
288 /*ci->residue_type=_ogg_malloc(ci->residues*sizeof(*ci->residue_type));*/
289 /*ci->residue_param=_ogg_calloc(ci->residues,sizeof(void *));*/
290 for(i=0;i<ci->residues;i++){
291 ci->residue_type[i]=oggpack_read(opb,16);
292 if(ci->residue_type[i]<0 || ci->residue_type[i]>=VI_RESB)goto err_out;
293 ci->residue_param[i]=_residue_P[ci->residue_type[i]]->unpack(vi,opb);
294 if(!ci->residue_param[i])goto err_out;
297 /* map backend settings */
298 ci->maps=oggpack_read(opb,6)+1;
299 /*ci->map_type=_ogg_malloc(ci->maps*sizeof(*ci->map_type));*/
300 /*ci->map_param=_ogg_calloc(ci->maps,sizeof(void *));*/
301 for(i=0;i<ci->maps;i++){
302 ci->map_type[i]=oggpack_read(opb,16);
303 if(ci->map_type[i]<0 || ci->map_type[i]>=VI_MAPB)goto err_out;
304 ci->map_param[i]=_mapping_P[ci->map_type[i]]->unpack(vi,opb);
305 if(!ci->map_param[i])goto err_out;
309 ci->modes=oggpack_read(opb,6)+1;
310 /*vi->mode_param=_ogg_calloc(vi->modes,sizeof(void *));*/
311 for(i=0;i<ci->modes;i++){
312 ci->mode_param[i]=_ogg_calloc(1,sizeof(*ci->mode_param[i]));
313 ci->mode_param[i]->blockflag=oggpack_read(opb,1);
314 ci->mode_param[i]->windowtype=oggpack_read(opb,16);
315 ci->mode_param[i]->transformtype=oggpack_read(opb,16);
316 ci->mode_param[i]->mapping=oggpack_read(opb,8);
318 if(ci->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
319 if(ci->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
320 if(ci->mode_param[i]->mapping>=ci->maps)goto err_out;
323 if(oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
327 vorbis_info_clear(vi);
328 return(OV_EBADHEADER);
331 /* The Vorbis header is in three packets; the initial small packet in
332 the first page that identifies basic parameters, a second packet
333 with bitstream comments and a third packet that holds the
336 int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
340 oggpack_readinit(&opb,op->packet,op->bytes);
342 /* Which of the three types of header is this? */
343 /* Also verify header-ness, vorbis */
346 int packtype=oggpack_read(&opb,8);
348 _v_readstring(&opb,buffer,6);
349 if(memcmp(buffer,"vorbis",6)){
350 /* not a vorbis header */
351 return(OV_ENOTVORBIS);
354 case 0x01: /* least significant *bit* is read first */
356 /* Not the initial packet */
357 return(OV_EBADHEADER);
360 /* previously initialized info header */
361 return(OV_EBADHEADER);
364 return(_vorbis_unpack_info(vi,&opb));
366 case 0x03: /* least significant *bit* is read first */
368 /* um... we didn't get the initial header */
369 return(OV_EBADHEADER);
372 return(_vorbis_unpack_comment(vc,&opb));
374 case 0x05: /* least significant *bit* is read first */
375 if(vi->rate==0 || vc->vendor==NULL){
376 /* um... we didn;t get the initial header or comments yet */
377 return(OV_EBADHEADER);
380 return(_vorbis_unpack_books(vi,&opb));
383 /* Not a valid vorbis header type */
384 return(OV_EBADHEADER);
389 return(OV_EBADHEADER);
392 /* pack side **********************************************************/
394 static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){
395 codec_setup_info *ci=vi->codec_setup;
396 if(!ci)return(OV_EFAULT);
399 oggpack_write(opb,0x01,8);
400 _v_writestring(opb,"vorbis", 6);
402 /* basic information about the stream */
403 oggpack_write(opb,0x00,32);
404 oggpack_write(opb,vi->channels,8);
405 oggpack_write(opb,vi->rate,32);
407 oggpack_write(opb,vi->bitrate_upper,32);
408 oggpack_write(opb,vi->bitrate_nominal,32);
409 oggpack_write(opb,vi->bitrate_lower,32);
411 oggpack_write(opb,ilog2(ci->blocksizes[0]),4);
412 oggpack_write(opb,ilog2(ci->blocksizes[1]),4);
413 oggpack_write(opb,1,1);
418 static int _vorbis_pack_comment(oggpack_buffer *opb,vorbis_comment *vc){
419 char temp[]="Xiph.Org libVorbis I 20030909";
420 int bytes = strlen(temp);
423 oggpack_write(opb,0x03,8);
424 _v_writestring(opb,"vorbis", 6);
427 oggpack_write(opb,bytes,32);
428 _v_writestring(opb,temp, bytes);
432 oggpack_write(opb,vc->comments,32);
435 for(i=0;i<vc->comments;i++){
436 if(vc->user_comments[i]){
437 oggpack_write(opb,vc->comment_lengths[i],32);
438 _v_writestring(opb,vc->user_comments[i], vc->comment_lengths[i]);
440 oggpack_write(opb,0,32);
444 oggpack_write(opb,1,1);
449 static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){
450 codec_setup_info *ci=vi->codec_setup;
452 if(!ci)return(OV_EFAULT);
454 oggpack_write(opb,0x05,8);
455 _v_writestring(opb,"vorbis", 6);
458 oggpack_write(opb,ci->books-1,8);
459 for(i=0;i<ci->books;i++)
460 if(vorbis_staticbook_pack(ci->book_param[i],opb))goto err_out;
462 /* times; hook placeholders */
463 oggpack_write(opb,0,6);
464 oggpack_write(opb,0,16);
467 oggpack_write(opb,ci->floors-1,6);
468 for(i=0;i<ci->floors;i++){
469 oggpack_write(opb,ci->floor_type[i],16);
470 if(_floor_P[ci->floor_type[i]]->pack)
471 _floor_P[ci->floor_type[i]]->pack(ci->floor_param[i],opb);
477 oggpack_write(opb,ci->residues-1,6);
478 for(i=0;i<ci->residues;i++){
479 oggpack_write(opb,ci->residue_type[i],16);
480 _residue_P[ci->residue_type[i]]->pack(ci->residue_param[i],opb);
484 oggpack_write(opb,ci->maps-1,6);
485 for(i=0;i<ci->maps;i++){
486 oggpack_write(opb,ci->map_type[i],16);
487 _mapping_P[ci->map_type[i]]->pack(vi,ci->map_param[i],opb);
491 oggpack_write(opb,ci->modes-1,6);
492 for(i=0;i<ci->modes;i++){
493 oggpack_write(opb,ci->mode_param[i]->blockflag,1);
494 oggpack_write(opb,ci->mode_param[i]->windowtype,16);
495 oggpack_write(opb,ci->mode_param[i]->transformtype,16);
496 oggpack_write(opb,ci->mode_param[i]->mapping,8);
498 oggpack_write(opb,1,1);
505 int vorbis_commentheader_out(vorbis_comment *vc,
510 oggpack_writeinit(&opb);
511 if(_vorbis_pack_comment(&opb,vc)) return OV_EIMPL;
513 op->packet = _ogg_malloc(oggpack_bytes(&opb));
514 memcpy(op->packet, opb.buffer, oggpack_bytes(&opb));
516 op->bytes=oggpack_bytes(&opb);
524 int vorbis_analysis_headerout(vorbis_dsp_state *v,
528 ogg_packet *op_code){
530 vorbis_info *vi=v->vi;
532 private_state *b=v->backend_state;
539 /* first header packet **********************************************/
541 oggpack_writeinit(&opb);
542 if(_vorbis_pack_info(&opb,vi))goto err_out;
544 /* build the packet */
545 if(b->header)_ogg_free(b->header);
546 b->header=_ogg_malloc(oggpack_bytes(&opb));
547 memcpy(b->header,opb.buffer,oggpack_bytes(&opb));
548 op->packet=b->header;
549 op->bytes=oggpack_bytes(&opb);
554 /* second header packet (comments) **********************************/
557 if(_vorbis_pack_comment(&opb,vc))goto err_out;
559 if(b->header1)_ogg_free(b->header1);
560 b->header1=_ogg_malloc(oggpack_bytes(&opb));
561 memcpy(b->header1,opb.buffer,oggpack_bytes(&opb));
562 op_comm->packet=b->header1;
563 op_comm->bytes=oggpack_bytes(&opb);
566 op_comm->granulepos=0;
568 /* third header packet (modes/codebooks) ****************************/
571 if(_vorbis_pack_books(&opb,vi))goto err_out;
573 if(b->header2)_ogg_free(b->header2);
574 b->header2=_ogg_malloc(oggpack_bytes(&opb));
575 memcpy(b->header2,opb.buffer,oggpack_bytes(&opb));
576 op_code->packet=b->header2;
577 op_code->bytes=oggpack_bytes(&opb);
580 op_code->granulepos=0;
582 oggpack_writeclear(&opb);
585 oggpack_writeclear(&opb);
586 memset(op,0,sizeof(*op));
587 memset(op_comm,0,sizeof(*op_comm));
588 memset(op_code,0,sizeof(*op_code));
590 if(b->header)_ogg_free(b->header);
591 if(b->header1)_ogg_free(b->header1);
592 if(b->header2)_ogg_free(b->header2);
599 double vorbis_granule_time(vorbis_dsp_state *v,ogg_int64_t granulepos){
601 return((double)granulepos/v->vi->rate);