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.46 2001/10/02 00:14:31 segher 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){
46 oggpack_write(o,*s++,8);
50 static void _v_readstring(oggpack_buffer *o,char *buf,int bytes){
52 *buf++=oggpack_read(o,8);
56 void vorbis_comment_init(vorbis_comment *vc){
57 memset(vc,0,sizeof(*vc));
60 void vorbis_comment_add(vorbis_comment *vc,char *comment){
61 vc->user_comments=_ogg_realloc(vc->user_comments,
62 (vc->comments+2)*sizeof(*vc->user_comments));
63 vc->comment_lengths=_ogg_realloc(vc->comment_lengths,
64 (vc->comments+2)*sizeof(*vc->comment_lengths));
65 vc->user_comments[vc->comments]=strdup(comment);
66 vc->comment_lengths[vc->comments]=strlen(comment);
68 vc->user_comments[vc->comments]=NULL;
71 void vorbis_comment_add_tag(vorbis_comment *vc, char *tag, char *contents){
72 char *comment=alloca(strlen(tag)+strlen(contents)+2); /* +2 for = and \0 */
75 strcat(comment, contents);
76 vorbis_comment_add(vc, comment);
79 /* This is more or less the same as strncasecmp - but that doesn't exist
80 * everywhere, and this is a fairly trivial function, so we include it */
81 static int tagcompare(const char *s1, const char *s2, int n){
84 if(toupper(s1[c]) != toupper(s2[c]))
91 char *vorbis_comment_query(vorbis_comment *vc, char *tag, int count){
94 int taglen = strlen(tag)+1; /* +1 for the = we append */
95 char *fulltag = alloca(taglen+ 1);
100 for(i=0;i<vc->comments;i++){
101 if(!tagcompare(vc->user_comments[i], fulltag, taglen)){
103 /* We return a pointer to the data, not a copy */
104 return vc->user_comments[i] + taglen;
109 return NULL; /* didn't find anything */
112 int vorbis_comment_query_count(vorbis_comment *vc, char *tag){
114 int taglen = strlen(tag)+1; /* +1 for the = we append */
115 char *fulltag = alloca(taglen+1);
117 strcat(fulltag, "=");
119 for(i=0;i<vc->comments;i++){
120 if(!tagcompare(vc->user_comments[i], fulltag, taglen))
127 void vorbis_comment_clear(vorbis_comment *vc){
130 for(i=0;i<vc->comments;i++)
131 if(vc->user_comments[i])_ogg_free(vc->user_comments[i]);
132 if(vc->user_comments)_ogg_free(vc->user_comments);
133 if(vc->comment_lengths)_ogg_free(vc->comment_lengths);
134 if(vc->vendor)_ogg_free(vc->vendor);
136 memset(vc,0,sizeof(*vc));
139 /* used by synthesis, which has a full, alloced vi */
140 void vorbis_info_init(vorbis_info *vi){
141 memset(vi,0,sizeof(*vi));
142 vi->codec_setup=_ogg_calloc(1,sizeof(codec_setup_info));
145 void vorbis_info_clear(vorbis_info *vi){
146 codec_setup_info *ci=vi->codec_setup;
151 for(i=0;i<ci->modes;i++)
152 if(ci->mode_param[i])_ogg_free(ci->mode_param[i]);
154 for(i=0;i<ci->maps;i++) /* unpack does the range checking */
155 _mapping_P[ci->map_type[i]]->free_info(ci->map_param[i]);
157 for(i=0;i<ci->times;i++) /* unpack does the range checking */
158 _time_P[ci->time_type[i]]->free_info(ci->time_param[i]);
160 for(i=0;i<ci->floors;i++) /* unpack does the range checking */
161 _floor_P[ci->floor_type[i]]->free_info(ci->floor_param[i]);
163 for(i=0;i<ci->residues;i++) /* unpack does the range checking */
164 _residue_P[ci->residue_type[i]]->free_info(ci->residue_param[i]);
166 for(i=0;i<ci->books;i++){
167 if(ci->book_param[i]){
168 /* knows if the book was not alloced */
169 vorbis_staticbook_destroy(ci->book_param[i]);
173 for(i=0;i<ci->psys;i++)
174 _vi_psy_free(ci->psy_param[i]);
179 memset(vi,0,sizeof(*vi));
182 /* Header packing/unpacking ********************************************/
184 static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
185 codec_setup_info *ci=vi->codec_setup;
186 if(!ci)return(OV_EFAULT);
188 vi->version=oggpack_read(opb,32);
189 if(vi->version!=0)return(OV_EVERSION);
191 vi->channels=oggpack_read(opb,8);
192 vi->rate=oggpack_read(opb,32);
194 vi->bitrate_upper=oggpack_read(opb,32);
195 vi->bitrate_nominal=oggpack_read(opb,32);
196 vi->bitrate_lower=oggpack_read(opb,32);
198 ci->blocksizes[0]=1<<oggpack_read(opb,4);
199 ci->blocksizes[1]=1<<oggpack_read(opb,4);
201 if(vi->rate<1)goto err_out;
202 if(vi->channels<1)goto err_out;
203 if(ci->blocksizes[0]<8)goto err_out;
204 if(ci->blocksizes[1]<ci->blocksizes[0])goto err_out;
206 if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
210 vorbis_info_clear(vi);
211 return(OV_EBADHEADER);
214 static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
216 int vendorlen=oggpack_read(opb,32);
217 if(vendorlen<0)goto err_out;
218 vc->vendor=_ogg_calloc(vendorlen+1,1);
219 _v_readstring(opb,vc->vendor,vendorlen);
220 vc->comments=oggpack_read(opb,32);
221 if(vc->comments<0)goto err_out;
222 vc->user_comments=_ogg_calloc(vc->comments+1,sizeof(*vc->user_comments));
223 vc->comment_lengths=_ogg_calloc(vc->comments+1, sizeof(*vc->comment_lengths));
225 for(i=0;i<vc->comments;i++){
226 int len=oggpack_read(opb,32);
227 if(len<0)goto err_out;
228 vc->comment_lengths[i]=len;
229 vc->user_comments[i]=_ogg_calloc(len+1,1);
230 _v_readstring(opb,vc->user_comments[i],len);
232 if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
236 vorbis_comment_clear(vc);
237 return(OV_EBADHEADER);
240 /* all of the real encoding details are here. The modes, books,
242 static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
243 codec_setup_info *ci=vi->codec_setup;
245 if(!ci)return(OV_EFAULT);
248 ci->books=oggpack_read(opb,8)+1;
249 /*ci->book_param=_ogg_calloc(ci->books,sizeof(*ci->book_param));*/
250 for(i=0;i<ci->books;i++){
251 ci->book_param[i]=_ogg_calloc(1,sizeof(*ci->book_param[i]));
252 if(vorbis_staticbook_unpack(opb,ci->book_param[i]))goto err_out;
255 /* time backend settings */
256 ci->times=oggpack_read(opb,6)+1;
257 /*ci->time_type=_ogg_malloc(ci->times*sizeof(*ci->time_type));*/
258 /*ci->time_param=_ogg_calloc(ci->times,sizeof(void *));*/
259 for(i=0;i<ci->times;i++){
260 ci->time_type[i]=oggpack_read(opb,16);
261 if(ci->time_type[i]<0 || ci->time_type[i]>=VI_TIMEB)goto err_out;
262 ci->time_param[i]=_time_P[ci->time_type[i]]->unpack(vi,opb);
263 if(!ci->time_param[i])goto err_out;
266 /* floor backend settings */
267 ci->floors=oggpack_read(opb,6)+1;
268 /*ci->floor_type=_ogg_malloc(ci->floors*sizeof(*ci->floor_type));*/
269 /*ci->floor_param=_ogg_calloc(ci->floors,sizeof(void *));*/
270 for(i=0;i<ci->floors;i++){
271 ci->floor_type[i]=oggpack_read(opb,16);
272 if(ci->floor_type[i]<0 || ci->floor_type[i]>=VI_FLOORB)goto err_out;
273 ci->floor_param[i]=_floor_P[ci->floor_type[i]]->unpack(vi,opb);
274 if(!ci->floor_param[i])goto err_out;
277 /* residue backend settings */
278 ci->residues=oggpack_read(opb,6)+1;
279 /*ci->residue_type=_ogg_malloc(ci->residues*sizeof(*ci->residue_type));*/
280 /*ci->residue_param=_ogg_calloc(ci->residues,sizeof(void *));*/
281 for(i=0;i<ci->residues;i++){
282 ci->residue_type[i]=oggpack_read(opb,16);
283 if(ci->residue_type[i]<0 || ci->residue_type[i]>=VI_RESB)goto err_out;
284 ci->residue_param[i]=_residue_P[ci->residue_type[i]]->unpack(vi,opb);
285 if(!ci->residue_param[i])goto err_out;
288 /* map backend settings */
289 ci->maps=oggpack_read(opb,6)+1;
290 /*ci->map_type=_ogg_malloc(ci->maps*sizeof(*ci->map_type));*/
291 /*ci->map_param=_ogg_calloc(ci->maps,sizeof(void *));*/
292 for(i=0;i<ci->maps;i++){
293 ci->map_type[i]=oggpack_read(opb,16);
294 if(ci->map_type[i]<0 || ci->map_type[i]>=VI_MAPB)goto err_out;
295 ci->map_param[i]=_mapping_P[ci->map_type[i]]->unpack(vi,opb);
296 if(!ci->map_param[i])goto err_out;
300 ci->modes=oggpack_read(opb,6)+1;
301 /*vi->mode_param=_ogg_calloc(vi->modes,sizeof(void *));*/
302 for(i=0;i<ci->modes;i++){
303 ci->mode_param[i]=_ogg_calloc(1,sizeof(*ci->mode_param[i]));
304 ci->mode_param[i]->blockflag=oggpack_read(opb,1);
305 ci->mode_param[i]->windowtype=oggpack_read(opb,16);
306 ci->mode_param[i]->transformtype=oggpack_read(opb,16);
307 ci->mode_param[i]->mapping=oggpack_read(opb,8);
309 if(ci->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
310 if(ci->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
311 if(ci->mode_param[i]->mapping>=ci->maps)goto err_out;
314 if(oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
318 vorbis_info_clear(vi);
319 return(OV_EBADHEADER);
322 /* The Vorbis header is in three packets; the initial small packet in
323 the first page that identifies basic parameters, a second packet
324 with bitstream comments and a third packet that holds the
327 int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
331 oggpack_readinit(&opb,op->packet,op->bytes);
333 /* Which of the three types of header is this? */
334 /* Also verify header-ness, vorbis */
337 int packtype=oggpack_read(&opb,8);
339 _v_readstring(&opb,buffer,6);
340 if(memcmp(buffer,"vorbis",6)){
341 /* not a vorbis header */
342 return(OV_ENOTVORBIS);
345 case 0x01: /* least significant *bit* is read first */
347 /* Not the initial packet */
348 return(OV_EBADHEADER);
351 /* previously initialized info header */
352 return(OV_EBADHEADER);
355 return(_vorbis_unpack_info(vi,&opb));
357 case 0x03: /* least significant *bit* is read first */
359 /* um... we didn't get the initial header */
360 return(OV_EBADHEADER);
363 return(_vorbis_unpack_comment(vc,&opb));
365 case 0x05: /* least significant *bit* is read first */
366 if(vi->rate==0 || vc->vendor==NULL){
367 /* um... we didn;t get the initial header or comments yet */
368 return(OV_EBADHEADER);
371 return(_vorbis_unpack_books(vi,&opb));
374 /* Not a valid vorbis header type */
375 return(OV_EBADHEADER);
380 return(OV_EBADHEADER);
383 /* pack side **********************************************************/
385 static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){
386 codec_setup_info *ci=vi->codec_setup;
387 if(!ci)return(OV_EFAULT);
390 oggpack_write(opb,0x01,8);
391 _v_writestring(opb,"vorbis");
393 /* basic information about the stream */
394 oggpack_write(opb,0x00,32);
395 oggpack_write(opb,vi->channels,8);
396 oggpack_write(opb,vi->rate,32);
398 oggpack_write(opb,vi->bitrate_upper,32);
399 oggpack_write(opb,vi->bitrate_nominal,32);
400 oggpack_write(opb,vi->bitrate_lower,32);
402 oggpack_write(opb,ilog2(ci->blocksizes[0]),4);
403 oggpack_write(opb,ilog2(ci->blocksizes[1]),4);
404 oggpack_write(opb,1,1);
409 static int _vorbis_pack_comment(oggpack_buffer *opb,vorbis_comment *vc){
410 char temp[]="Xiphophorus libVorbis I 20010910";
413 oggpack_write(opb,0x03,8);
414 _v_writestring(opb,"vorbis");
417 oggpack_write(opb,strlen(temp),32);
418 _v_writestring(opb,temp);
422 oggpack_write(opb,vc->comments,32);
425 for(i=0;i<vc->comments;i++){
426 if(vc->user_comments[i]){
427 oggpack_write(opb,vc->comment_lengths[i],32);
428 _v_writestring(opb,vc->user_comments[i]);
430 oggpack_write(opb,0,32);
434 oggpack_write(opb,1,1);
439 static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){
440 codec_setup_info *ci=vi->codec_setup;
442 if(!ci)return(OV_EFAULT);
444 oggpack_write(opb,0x05,8);
445 _v_writestring(opb,"vorbis");
448 oggpack_write(opb,ci->books-1,8);
449 for(i=0;i<ci->books;i++)
450 if(vorbis_staticbook_pack(ci->book_param[i],opb))goto err_out;
453 oggpack_write(opb,ci->times-1,6);
454 for(i=0;i<ci->times;i++){
455 oggpack_write(opb,ci->time_type[i],16);
456 _time_P[ci->time_type[i]]->pack(ci->time_param[i],opb);
460 oggpack_write(opb,ci->floors-1,6);
461 for(i=0;i<ci->floors;i++){
462 oggpack_write(opb,ci->floor_type[i],16);
463 _floor_P[ci->floor_type[i]]->pack(ci->floor_param[i],opb);
467 oggpack_write(opb,ci->residues-1,6);
468 for(i=0;i<ci->residues;i++){
469 oggpack_write(opb,ci->residue_type[i],16);
470 _residue_P[ci->residue_type[i]]->pack(ci->residue_param[i],opb);
474 oggpack_write(opb,ci->maps-1,6);
475 for(i=0;i<ci->maps;i++){
476 oggpack_write(opb,ci->map_type[i],16);
477 _mapping_P[ci->map_type[i]]->pack(vi,ci->map_param[i],opb);
481 oggpack_write(opb,ci->modes-1,6);
482 for(i=0;i<ci->modes;i++){
483 oggpack_write(opb,ci->mode_param[i]->blockflag,1);
484 oggpack_write(opb,ci->mode_param[i]->windowtype,16);
485 oggpack_write(opb,ci->mode_param[i]->transformtype,16);
486 oggpack_write(opb,ci->mode_param[i]->mapping,8);
488 oggpack_write(opb,1,1);
495 int vorbis_commentheader_out(vorbis_comment *vc,
500 oggpack_writeinit(&opb);
501 if(_vorbis_pack_comment(&opb,vc)) return OV_EIMPL;
503 op->packet = _ogg_malloc(oggpack_bytes(&opb));
504 memcpy(op->packet, opb.buffer, oggpack_bytes(&opb));
506 op->bytes=oggpack_bytes(&opb);
514 int vorbis_analysis_headerout(vorbis_dsp_state *v,
518 ogg_packet *op_code){
520 vorbis_info *vi=v->vi;
522 backend_lookup_state *b=v->backend_state;
529 /* first header packet **********************************************/
531 oggpack_writeinit(&opb);
532 if(_vorbis_pack_info(&opb,vi))goto err_out;
534 /* build the packet */
535 if(b->header)_ogg_free(b->header);
536 b->header=_ogg_malloc(oggpack_bytes(&opb));
537 memcpy(b->header,opb.buffer,oggpack_bytes(&opb));
538 op->packet=b->header;
539 op->bytes=oggpack_bytes(&opb);
544 /* second header packet (comments) **********************************/
547 if(_vorbis_pack_comment(&opb,vc))goto err_out;
549 if(b->header1)_ogg_free(b->header1);
550 b->header1=_ogg_malloc(oggpack_bytes(&opb));
551 memcpy(b->header1,opb.buffer,oggpack_bytes(&opb));
552 op_comm->packet=b->header1;
553 op_comm->bytes=oggpack_bytes(&opb);
556 op_comm->granulepos=0;
558 /* third header packet (modes/codebooks) ****************************/
561 if(_vorbis_pack_books(&opb,vi))goto err_out;
563 if(b->header2)_ogg_free(b->header2);
564 b->header2=_ogg_malloc(oggpack_bytes(&opb));
565 memcpy(b->header2,opb.buffer,oggpack_bytes(&opb));
566 op_code->packet=b->header2;
567 op_code->bytes=oggpack_bytes(&opb);
570 op_code->granulepos=0;
572 oggpack_writeclear(&opb);
575 oggpack_writeclear(&opb);
576 memset(op,0,sizeof(*op));
577 memset(op_comm,0,sizeof(*op_comm));
578 memset(op_code,0,sizeof(*op_code));
580 if(b->header)_ogg_free(b->header);
581 if(b->header1)_ogg_free(b->header1);
582 if(b->header2)_ogg_free(b->header2);