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-2001 *
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.49 2001/12/12 09:45:25 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){
44 static void _v_writestring(oggpack_buffer *o,char *s, int bytes){
47 oggpack_write(o,*s++,8);
51 static void _v_readstring(oggpack_buffer *o,char *buf,int bytes){
53 *buf++=oggpack_read(o,8);
57 void vorbis_comment_init(vorbis_comment *vc){
58 memset(vc,0,sizeof(*vc));
61 void vorbis_comment_add(vorbis_comment *vc,char *comment){
62 vc->user_comments=_ogg_realloc(vc->user_comments,
63 (vc->comments+2)*sizeof(*vc->user_comments));
64 vc->comment_lengths=_ogg_realloc(vc->comment_lengths,
65 (vc->comments+2)*sizeof(*vc->comment_lengths));
66 vc->comment_lengths[vc->comments]=strlen(comment);
67 vc->user_comments[vc->comments]=_ogg_malloc(vc->comment_lengths[vc->comments]+1);
68 strcpy(vc->user_comments[vc->comments], comment);
70 vc->user_comments[vc->comments]=NULL;
73 void vorbis_comment_add_tag(vorbis_comment *vc, char *tag, char *contents){
74 char *comment=alloca(strlen(tag)+strlen(contents)+2); /* +2 for = and \0 */
77 strcat(comment, contents);
78 vorbis_comment_add(vc, comment);
81 /* This is more or less the same as strncasecmp - but that doesn't exist
82 * everywhere, and this is a fairly trivial function, so we include it */
83 static int tagcompare(const char *s1, const char *s2, int n){
86 if(toupper(s1[c]) != toupper(s2[c]))
93 char *vorbis_comment_query(vorbis_comment *vc, char *tag, int count){
96 int taglen = strlen(tag)+1; /* +1 for the = we append */
97 char *fulltag = alloca(taglen+ 1);
100 strcat(fulltag, "=");
102 for(i=0;i<vc->comments;i++){
103 if(!tagcompare(vc->user_comments[i], fulltag, taglen)){
105 /* We return a pointer to the data, not a copy */
106 return vc->user_comments[i] + taglen;
111 return NULL; /* didn't find anything */
114 int vorbis_comment_query_count(vorbis_comment *vc, char *tag){
116 int taglen = strlen(tag)+1; /* +1 for the = we append */
117 char *fulltag = alloca(taglen+1);
119 strcat(fulltag, "=");
121 for(i=0;i<vc->comments;i++){
122 if(!tagcompare(vc->user_comments[i], fulltag, taglen))
129 void vorbis_comment_clear(vorbis_comment *vc){
132 for(i=0;i<vc->comments;i++)
133 if(vc->user_comments[i])_ogg_free(vc->user_comments[i]);
134 if(vc->user_comments)_ogg_free(vc->user_comments);
135 if(vc->comment_lengths)_ogg_free(vc->comment_lengths);
136 if(vc->vendor)_ogg_free(vc->vendor);
138 memset(vc,0,sizeof(*vc));
141 /* blocksize 0 is guaranteed to be short, 1 is guarantted to be long.
142 They may be equal, but short will never ge greater than long */
143 int vorbis_info_blocksize(vorbis_info *vi,int zo){
144 codec_setup_info *ci = vi->codec_setup;
145 return ci ? ci->blocksizes[zo] : -1;
148 /* used by synthesis, which has a full, alloced vi */
149 void vorbis_info_init(vorbis_info *vi){
150 memset(vi,0,sizeof(*vi));
151 vi->codec_setup=_ogg_calloc(1,sizeof(codec_setup_info));
154 void vorbis_info_clear(vorbis_info *vi){
155 codec_setup_info *ci=vi->codec_setup;
160 for(i=0;i<ci->modes;i++)
161 if(ci->mode_param[i])_ogg_free(ci->mode_param[i]);
163 for(i=0;i<ci->maps;i++) /* unpack does the range checking */
164 _mapping_P[ci->map_type[i]]->free_info(ci->map_param[i]);
166 for(i=0;i<ci->times;i++) /* unpack does the range checking */
167 _time_P[ci->time_type[i]]->free_info(ci->time_param[i]);
169 for(i=0;i<ci->floors;i++) /* unpack does the range checking */
170 _floor_P[ci->floor_type[i]]->free_info(ci->floor_param[i]);
172 for(i=0;i<ci->residues;i++) /* unpack does the range checking */
173 _residue_P[ci->residue_type[i]]->free_info(ci->residue_param[i]);
175 for(i=0;i<ci->books;i++){
176 if(ci->book_param[i]){
177 /* knows if the book was not alloced */
178 vorbis_staticbook_destroy(ci->book_param[i]);
182 for(i=0;i<ci->psys;i++)
183 _vi_psy_free(ci->psy_param[i]);
188 memset(vi,0,sizeof(*vi));
191 /* Header packing/unpacking ********************************************/
193 static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
194 codec_setup_info *ci=vi->codec_setup;
195 if(!ci)return(OV_EFAULT);
197 vi->version=oggpack_read(opb,32);
198 if(vi->version!=0)return(OV_EVERSION);
200 vi->channels=oggpack_read(opb,8);
201 vi->rate=oggpack_read(opb,32);
203 vi->bitrate_upper=oggpack_read(opb,32);
204 vi->bitrate_nominal=oggpack_read(opb,32);
205 vi->bitrate_lower=oggpack_read(opb,32);
207 ci->blocksizes[0]=1<<oggpack_read(opb,4);
208 ci->blocksizes[1]=1<<oggpack_read(opb,4);
210 if(vi->rate<1)goto err_out;
211 if(vi->channels<1)goto err_out;
212 if(ci->blocksizes[0]<8)goto err_out;
213 if(ci->blocksizes[1]<ci->blocksizes[0])goto err_out;
215 if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
219 vorbis_info_clear(vi);
220 return(OV_EBADHEADER);
223 static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
225 int vendorlen=oggpack_read(opb,32);
226 if(vendorlen<0)goto err_out;
227 vc->vendor=_ogg_calloc(vendorlen+1,1);
228 _v_readstring(opb,vc->vendor,vendorlen);
229 vc->comments=oggpack_read(opb,32);
230 if(vc->comments<0)goto err_out;
231 vc->user_comments=_ogg_calloc(vc->comments+1,sizeof(*vc->user_comments));
232 vc->comment_lengths=_ogg_calloc(vc->comments+1, sizeof(*vc->comment_lengths));
234 for(i=0;i<vc->comments;i++){
235 int len=oggpack_read(opb,32);
236 if(len<0)goto err_out;
237 vc->comment_lengths[i]=len;
238 vc->user_comments[i]=_ogg_calloc(len+1,1);
239 _v_readstring(opb,vc->user_comments[i],len);
241 if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
245 vorbis_comment_clear(vc);
246 return(OV_EBADHEADER);
249 /* all of the real encoding details are here. The modes, books,
251 static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
252 codec_setup_info *ci=vi->codec_setup;
254 if(!ci)return(OV_EFAULT);
257 ci->books=oggpack_read(opb,8)+1;
258 /*ci->book_param=_ogg_calloc(ci->books,sizeof(*ci->book_param));*/
259 for(i=0;i<ci->books;i++){
260 ci->book_param[i]=_ogg_calloc(1,sizeof(*ci->book_param[i]));
261 if(vorbis_staticbook_unpack(opb,ci->book_param[i]))goto err_out;
264 /* time backend settings */
265 ci->times=oggpack_read(opb,6)+1;
266 /*ci->time_type=_ogg_malloc(ci->times*sizeof(*ci->time_type));*/
267 /*ci->time_param=_ogg_calloc(ci->times,sizeof(void *));*/
268 for(i=0;i<ci->times;i++){
269 ci->time_type[i]=oggpack_read(opb,16);
270 if(ci->time_type[i]<0 || ci->time_type[i]>=VI_TIMEB)goto err_out;
271 ci->time_param[i]=_time_P[ci->time_type[i]]->unpack(vi,opb);
272 if(!ci->time_param[i])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[]="Xiphophorus libVorbis I 20011212";
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;
463 oggpack_write(opb,ci->times-1,6);
464 for(i=0;i<ci->times;i++){
465 oggpack_write(opb,ci->time_type[i],16);
466 _time_P[ci->time_type[i]]->pack(ci->time_param[i],opb);
470 oggpack_write(opb,ci->floors-1,6);
471 for(i=0;i<ci->floors;i++){
472 oggpack_write(opb,ci->floor_type[i],16);
473 _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 backend_lookup_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);