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.39 2001/02/26 03:50:41 xiphmont Exp $
16 ********************************************************************/
18 /* general handling of the header and the vorbis_info structure (and
25 #include "vorbis/codec.h"
27 #include "codec_internal.h"
36 static int ilog2(unsigned int v){
45 static void _v_writestring(oggpack_buffer *o,char *s){
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(vorbis_comment));
61 void vorbis_comment_add(vorbis_comment *vc,char *comment){
62 vc->user_comments=_ogg_realloc(vc->user_comments,
63 (vc->comments+2)*sizeof(char *));
64 vc->comment_lengths=_ogg_realloc(vc->comment_lengths,
65 (vc->comments+2)*sizeof(int));
66 vc->user_comments[vc->comments]=strdup(comment);
67 vc->comment_lengths[vc->comments]=strlen(comment);
69 vc->user_comments[vc->comments]=NULL;
72 void vorbis_comment_add_tag(vorbis_comment *vc, char *tag, char *contents){
73 char *comment=alloca(strlen(tag)+strlen(contents)+2); /* +2 for = and \0 */
76 strcat(comment, contents);
77 vorbis_comment_add(vc, comment);
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 */
82 static int tagcompare(const char *s1, const char *s2, int n){
85 if(toupper(s1[c]) != toupper(s2[c]))
92 char *vorbis_comment_query(vorbis_comment *vc, char *tag, int count){
95 int taglen = strlen(tag)+1; /* +1 for the = we append */
96 char *fulltag = alloca(taglen+ 1);
101 for(i=0;i<vc->comments;i++){
102 if(!tagcompare(vc->user_comments[i], fulltag, taglen)){
104 /* We return a pointer to the data, not a copy */
105 return vc->user_comments[i] + taglen;
110 return NULL; /* didn't find anything */
113 int vorbis_comment_query_count(vorbis_comment *vc, char *tag){
115 int taglen = strlen(tag)+1; /* +1 for the = we append */
116 char *fulltag = alloca(taglen+1);
118 strcat(fulltag, "=");
120 for(i=0;i<vc->comments;i++){
121 if(!tagcompare(vc->user_comments[i], fulltag, taglen))
128 void vorbis_comment_clear(vorbis_comment *vc){
131 for(i=0;i<vc->comments;i++)
132 if(vc->user_comments[i])_ogg_free(vc->user_comments[i]);
133 if(vc->user_comments)_ogg_free(vc->user_comments);
134 if(vc->comment_lengths)_ogg_free(vc->comment_lengths);
135 if(vc->vendor)_ogg_free(vc->vendor);
137 memset(vc,0,sizeof(vorbis_comment));
140 /* used by synthesis, which has a full, alloced vi */
141 void vorbis_info_init(vorbis_info *vi){
142 memset(vi,0,sizeof(vorbis_info));
143 vi->codec_setup=_ogg_calloc(1,sizeof(codec_setup_info));
146 void vorbis_info_clear(vorbis_info *vi){
147 codec_setup_info *ci=vi->codec_setup;
152 for(i=0;i<ci->modes;i++)
153 if(ci->mode_param[i])_ogg_free(ci->mode_param[i]);
155 for(i=0;i<ci->maps;i++) /* unpack does the range checking */
156 _mapping_P[ci->map_type[i]]->free_info(ci->map_param[i]);
158 for(i=0;i<ci->times;i++) /* unpack does the range checking */
159 _time_P[ci->time_type[i]]->free_info(ci->time_param[i]);
161 for(i=0;i<ci->floors;i++) /* unpack does the range checking */
162 _floor_P[ci->floor_type[i]]->free_info(ci->floor_param[i]);
164 for(i=0;i<ci->residues;i++) /* unpack does the range checking */
165 _residue_P[ci->residue_type[i]]->free_info(ci->residue_param[i]);
167 for(i=0;i<ci->books;i++){
168 if(ci->book_param[i]){
169 /* knows if the book was not alloced */
170 vorbis_staticbook_destroy(ci->book_param[i]);
174 for(i=0;i<ci->psys;i++)
175 _vi_psy_free(ci->psy_param[i]);
180 memset(vi,0,sizeof(vorbis_info));
183 /* Header packing/unpacking ********************************************/
185 static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
186 codec_setup_info *ci=vi->codec_setup;
187 if(!ci)return(OV_EFAULT);
189 vi->version=oggpack_read(opb,32);
190 if(vi->version!=0)return(OV_EVERSION);
192 vi->channels=oggpack_read(opb,8);
193 vi->rate=oggpack_read(opb,32);
195 vi->bitrate_upper=oggpack_read(opb,32);
196 vi->bitrate_nominal=oggpack_read(opb,32);
197 vi->bitrate_lower=oggpack_read(opb,32);
199 ci->blocksizes[0]=1<<oggpack_read(opb,4);
200 ci->blocksizes[1]=1<<oggpack_read(opb,4);
202 if(vi->rate<1)goto err_out;
203 if(vi->channels<1)goto err_out;
204 if(ci->blocksizes[0]<8)goto err_out;
205 if(ci->blocksizes[1]<ci->blocksizes[0])goto err_out;
207 if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
211 vorbis_info_clear(vi);
212 return(OV_EBADHEADER);
215 static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
217 int vendorlen=oggpack_read(opb,32);
218 if(vendorlen<0)goto err_out;
219 vc->vendor=_ogg_calloc(vendorlen+1,1);
220 _v_readstring(opb,vc->vendor,vendorlen);
221 vc->comments=oggpack_read(opb,32);
222 if(vc->comments<0)goto err_out;
223 vc->user_comments=_ogg_calloc(vc->comments+1,sizeof(char **));
224 vc->comment_lengths=_ogg_calloc(vc->comments+1, sizeof(int));
226 for(i=0;i<vc->comments;i++){
227 int len=oggpack_read(opb,32);
228 if(len<0)goto err_out;
229 vc->comment_lengths[i]=len;
230 vc->user_comments[i]=_ogg_calloc(len+1,1);
231 _v_readstring(opb,vc->user_comments[i],len);
233 if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
237 vorbis_comment_clear(vc);
238 return(OV_EBADHEADER);
241 /* all of the real encoding details are here. The modes, books,
243 static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
244 codec_setup_info *ci=vi->codec_setup;
246 if(!ci)return(OV_EFAULT);
249 ci->books=oggpack_read(opb,8)+1;
250 /*ci->book_param=_ogg_calloc(ci->books,sizeof(static_codebook *));*/
251 for(i=0;i<ci->books;i++){
252 ci->book_param[i]=_ogg_calloc(1,sizeof(static_codebook));
253 if(vorbis_staticbook_unpack(opb,ci->book_param[i]))goto err_out;
256 /* time backend settings */
257 ci->times=oggpack_read(opb,6)+1;
258 /*ci->time_type=_ogg_malloc(ci->times*sizeof(int));*/
259 /*ci->time_param=_ogg_calloc(ci->times,sizeof(void *));*/
260 for(i=0;i<ci->times;i++){
261 ci->time_type[i]=oggpack_read(opb,16);
262 if(ci->time_type[i]<0 || ci->time_type[i]>=VI_TIMEB)goto err_out;
263 ci->time_param[i]=_time_P[ci->time_type[i]]->unpack(vi,opb);
264 if(!ci->time_param[i])goto err_out;
267 /* floor backend settings */
268 ci->floors=oggpack_read(opb,6)+1;
269 /*ci->floor_type=_ogg_malloc(ci->floors*sizeof(int));*/
270 /*ci->floor_param=_ogg_calloc(ci->floors,sizeof(void *));*/
271 for(i=0;i<ci->floors;i++){
272 ci->floor_type[i]=oggpack_read(opb,16);
273 if(ci->floor_type[i]<0 || ci->floor_type[i]>=VI_FLOORB)goto err_out;
274 ci->floor_param[i]=_floor_P[ci->floor_type[i]]->unpack(vi,opb);
275 if(!ci->floor_param[i])goto err_out;
278 /* residue backend settings */
279 ci->residues=oggpack_read(opb,6)+1;
280 /*ci->residue_type=_ogg_malloc(ci->residues*sizeof(int));*/
281 /*ci->residue_param=_ogg_calloc(ci->residues,sizeof(void *));*/
282 for(i=0;i<ci->residues;i++){
283 ci->residue_type[i]=oggpack_read(opb,16);
284 if(ci->residue_type[i]<0 || ci->residue_type[i]>=VI_RESB)goto err_out;
285 ci->residue_param[i]=_residue_P[ci->residue_type[i]]->unpack(vi,opb);
286 if(!ci->residue_param[i])goto err_out;
289 /* map backend settings */
290 ci->maps=oggpack_read(opb,6)+1;
291 /*ci->map_type=_ogg_malloc(ci->maps*sizeof(int));*/
292 /*ci->map_param=_ogg_calloc(ci->maps,sizeof(void *));*/
293 for(i=0;i<ci->maps;i++){
294 ci->map_type[i]=oggpack_read(opb,16);
295 if(ci->map_type[i]<0 || ci->map_type[i]>=VI_MAPB)goto err_out;
296 ci->map_param[i]=_mapping_P[ci->map_type[i]]->unpack(vi,opb);
297 if(!ci->map_param[i])goto err_out;
301 ci->modes=oggpack_read(opb,6)+1;
302 /*vi->mode_param=_ogg_calloc(vi->modes,sizeof(void *));*/
303 for(i=0;i<ci->modes;i++){
304 ci->mode_param[i]=_ogg_calloc(1,sizeof(vorbis_info_mode));
305 ci->mode_param[i]->blockflag=oggpack_read(opb,1);
306 ci->mode_param[i]->windowtype=oggpack_read(opb,16);
307 ci->mode_param[i]->transformtype=oggpack_read(opb,16);
308 ci->mode_param[i]->mapping=oggpack_read(opb,8);
310 if(ci->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
311 if(ci->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
312 if(ci->mode_param[i]->mapping>=ci->maps)goto err_out;
315 if(oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
319 vorbis_info_clear(vi);
320 return(OV_EBADHEADER);
323 /* The Vorbis header is in three packets; the initial small packet in
324 the first page that identifies basic parameters, a second packet
325 with bitstream comments and a third packet that holds the
328 int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
332 oggpack_readinit(&opb,op->packet,op->bytes);
334 /* Which of the three types of header is this? */
335 /* Also verify header-ness, vorbis */
338 int packtype=oggpack_read(&opb,8);
340 _v_readstring(&opb,buffer,6);
341 if(memcmp(buffer,"vorbis",6)){
342 /* not a vorbis header */
343 return(OV_ENOTVORBIS);
346 case 0x01: /* least significant *bit* is read first */
348 /* Not the initial packet */
349 return(OV_EBADHEADER);
352 /* previously initialized info header */
353 return(OV_EBADHEADER);
356 return(_vorbis_unpack_info(vi,&opb));
358 case 0x03: /* least significant *bit* is read first */
360 /* um... we didn't get the initial header */
361 return(OV_EBADHEADER);
364 return(_vorbis_unpack_comment(vc,&opb));
366 case 0x05: /* least significant *bit* is read first */
367 if(vi->rate==0 || vc->vendor==NULL){
368 /* um... we didn;t get the initial header or comments yet */
369 return(OV_EBADHEADER);
372 return(_vorbis_unpack_books(vi,&opb));
375 /* Not a valid vorbis header type */
376 return(OV_EBADHEADER);
381 return(OV_EBADHEADER);
384 /* pack side **********************************************************/
386 static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){
387 codec_setup_info *ci=vi->codec_setup;
388 if(!ci)return(OV_EFAULT);
391 oggpack_write(opb,0x01,8);
392 _v_writestring(opb,"vorbis");
394 /* basic information about the stream */
395 oggpack_write(opb,0x00,32);
396 oggpack_write(opb,vi->channels,8);
397 oggpack_write(opb,vi->rate,32);
399 oggpack_write(opb,vi->bitrate_upper,32);
400 oggpack_write(opb,vi->bitrate_nominal,32);
401 oggpack_write(opb,vi->bitrate_lower,32);
403 oggpack_write(opb,ilog2(ci->blocksizes[0]),4);
404 oggpack_write(opb,ilog2(ci->blocksizes[1]),4);
405 oggpack_write(opb,1,1);
410 static int _vorbis_pack_comment(oggpack_buffer *opb,vorbis_comment *vc){
411 char temp[]="Xiphophorus libVorbis I 20010225";
414 oggpack_write(opb,0x03,8);
415 _v_writestring(opb,"vorbis");
418 oggpack_write(opb,strlen(temp),32);
419 _v_writestring(opb,temp);
423 oggpack_write(opb,vc->comments,32);
426 for(i=0;i<vc->comments;i++){
427 if(vc->user_comments[i]){
428 oggpack_write(opb,vc->comment_lengths[i],32);
429 _v_writestring(opb,vc->user_comments[i]);
431 oggpack_write(opb,0,32);
435 oggpack_write(opb,1,1);
440 static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){
441 codec_setup_info *ci=vi->codec_setup;
443 if(!ci)return(OV_EFAULT);
445 oggpack_write(opb,0x05,8);
446 _v_writestring(opb,"vorbis");
449 oggpack_write(opb,ci->books-1,8);
450 for(i=0;i<ci->books;i++)
451 if(vorbis_staticbook_pack(ci->book_param[i],opb))goto err_out;
454 oggpack_write(opb,ci->times-1,6);
455 for(i=0;i<ci->times;i++){
456 oggpack_write(opb,ci->time_type[i],16);
457 _time_P[ci->time_type[i]]->pack(ci->time_param[i],opb);
461 oggpack_write(opb,ci->floors-1,6);
462 for(i=0;i<ci->floors;i++){
463 oggpack_write(opb,ci->floor_type[i],16);
464 _floor_P[ci->floor_type[i]]->pack(ci->floor_param[i],opb);
468 oggpack_write(opb,ci->residues-1,6);
469 for(i=0;i<ci->residues;i++){
470 oggpack_write(opb,ci->residue_type[i],16);
471 _residue_P[ci->residue_type[i]]->pack(ci->residue_param[i],opb);
475 oggpack_write(opb,ci->maps-1,6);
476 for(i=0;i<ci->maps;i++){
477 oggpack_write(opb,ci->map_type[i],16);
478 _mapping_P[ci->map_type[i]]->pack(vi,ci->map_param[i],opb);
482 oggpack_write(opb,ci->modes-1,6);
483 for(i=0;i<ci->modes;i++){
484 oggpack_write(opb,ci->mode_param[i]->blockflag,1);
485 oggpack_write(opb,ci->mode_param[i]->windowtype,16);
486 oggpack_write(opb,ci->mode_param[i]->transformtype,16);
487 oggpack_write(opb,ci->mode_param[i]->mapping,8);
489 oggpack_write(opb,1,1);
496 int vorbis_commentheader_out(vorbis_comment *vc,
501 oggpack_writeinit(&opb);
502 if(_vorbis_pack_comment(&opb,vc)) return OV_EIMPL;
504 op->packet = _ogg_malloc(oggpack_bytes(&opb));
505 memcpy(op->packet, opb.buffer, oggpack_bytes(&opb));
507 op->bytes=oggpack_bytes(&opb);
515 int vorbis_analysis_headerout(vorbis_dsp_state *v,
519 ogg_packet *op_code){
521 vorbis_info *vi=v->vi;
523 backend_lookup_state *b=v->backend_state;
530 /* first header packet **********************************************/
532 oggpack_writeinit(&opb);
533 if(_vorbis_pack_info(&opb,vi))goto err_out;
535 /* build the packet */
536 if(b->header)_ogg_free(b->header);
537 b->header=_ogg_malloc(oggpack_bytes(&opb));
538 memcpy(b->header,opb.buffer,oggpack_bytes(&opb));
539 op->packet=b->header;
540 op->bytes=oggpack_bytes(&opb);
545 /* second header packet (comments) **********************************/
548 if(_vorbis_pack_comment(&opb,vc))goto err_out;
550 if(b->header1)_ogg_free(b->header1);
551 b->header1=_ogg_malloc(oggpack_bytes(&opb));
552 memcpy(b->header1,opb.buffer,oggpack_bytes(&opb));
553 op_comm->packet=b->header1;
554 op_comm->bytes=oggpack_bytes(&opb);
557 op_comm->granulepos=0;
559 /* third header packet (modes/codebooks) ****************************/
562 if(_vorbis_pack_books(&opb,vi))goto err_out;
564 if(b->header2)_ogg_free(b->header2);
565 b->header2=_ogg_malloc(oggpack_bytes(&opb));
566 memcpy(b->header2,opb.buffer,oggpack_bytes(&opb));
567 op_code->packet=b->header2;
568 op_code->bytes=oggpack_bytes(&opb);
571 op_code->granulepos=0;
573 oggpack_writeclear(&opb);
576 oggpack_writeclear(&opb);
577 memset(op,0,sizeof(ogg_packet));
578 memset(op_comm,0,sizeof(ogg_packet));
579 memset(op_code,0,sizeof(ogg_packet));
581 if(b->header)_ogg_free(b->header);
582 if(b->header1)_ogg_free(b->header1);
583 if(b->header2)_ogg_free(b->header2);