1 /********************************************************************
3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
5 * THE GNU LESSER/LIBRARY PUBLIC LICENSE, WHICH IS INCLUDED WITH *
6 * THIS SOURCE. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2000 *
9 * by Monty <monty@xiph.org> and the XIPHOPHORUS Company *
10 * http://www.xiph.org/ *
12 ********************************************************************
14 function: maintain the info structure, info <-> header packets
15 last mod: $Id: info.c,v 1.36 2001/02/01 01:03:29 xiphmont Exp $
17 ********************************************************************/
19 /* general handling of the header and the vorbis_info structure (and
26 #include "vorbis/codec.h"
28 #include "codec_internal.h"
37 static int ilog2(unsigned int v){
46 static void _v_writestring(oggpack_buffer *o,char *s){
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(vorbis_comment));
62 void vorbis_comment_add(vorbis_comment *vc,char *comment){
63 vc->user_comments=_ogg_realloc(vc->user_comments,
64 (vc->comments+2)*sizeof(char *));
65 vc->comment_lengths=_ogg_realloc(vc->comment_lengths,
66 (vc->comments+2)*sizeof(int));
67 vc->user_comments[vc->comments]=strdup(comment);
68 vc->comment_lengths[vc->comments]=strlen(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(vorbis_comment));
141 /* used by synthesis, which has a full, alloced vi */
142 void vorbis_info_init(vorbis_info *vi){
143 memset(vi,0,sizeof(vorbis_info));
144 vi->codec_setup=_ogg_calloc(1,sizeof(codec_setup_info));
147 void vorbis_info_clear(vorbis_info *vi){
148 codec_setup_info *ci=vi->codec_setup;
153 for(i=0;i<ci->modes;i++)
154 if(ci->mode_param[i])_ogg_free(ci->mode_param[i]);
156 for(i=0;i<ci->maps;i++) /* unpack does the range checking */
157 _mapping_P[ci->map_type[i]]->free_info(ci->map_param[i]);
159 for(i=0;i<ci->times;i++) /* unpack does the range checking */
160 _time_P[ci->time_type[i]]->free_info(ci->time_param[i]);
162 for(i=0;i<ci->floors;i++) /* unpack does the range checking */
163 _floor_P[ci->floor_type[i]]->free_info(ci->floor_param[i]);
165 for(i=0;i<ci->residues;i++) /* unpack does the range checking */
166 _residue_P[ci->residue_type[i]]->free_info(ci->residue_param[i]);
168 for(i=0;i<ci->books;i++){
169 if(ci->book_param[i]){
170 /* knows if the book was not alloced */
171 vorbis_staticbook_destroy(ci->book_param[i]);
175 for(i=0;i<ci->psys;i++)
176 _vi_psy_free(ci->psy_param[i]);
181 memset(vi,0,sizeof(vorbis_info));
184 /* Header packing/unpacking ********************************************/
186 static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
187 codec_setup_info *ci=vi->codec_setup;
188 if(!ci)return(OV_EFAULT);
190 vi->version=oggpack_read(opb,32);
191 if(vi->version!=0)return(OV_EVERSION);
193 vi->channels=oggpack_read(opb,8);
194 vi->rate=oggpack_read(opb,32);
196 vi->bitrate_upper=oggpack_read(opb,32);
197 vi->bitrate_nominal=oggpack_read(opb,32);
198 vi->bitrate_lower=oggpack_read(opb,32);
200 ci->blocksizes[0]=1<<oggpack_read(opb,4);
201 ci->blocksizes[1]=1<<oggpack_read(opb,4);
203 if(vi->rate<1)goto err_out;
204 if(vi->channels<1)goto err_out;
205 if(ci->blocksizes[0]<8)goto err_out;
206 if(ci->blocksizes[1]<ci->blocksizes[0])goto err_out;
208 if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
212 vorbis_info_clear(vi);
213 return(OV_EBADHEADER);
216 static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
218 int vendorlen=oggpack_read(opb,32);
219 if(vendorlen<0)goto err_out;
220 vc->vendor=_ogg_calloc(vendorlen+1,1);
221 _v_readstring(opb,vc->vendor,vendorlen);
222 vc->comments=oggpack_read(opb,32);
223 if(vc->comments<0)goto err_out;
224 vc->user_comments=_ogg_calloc(vc->comments+1,sizeof(char **));
225 vc->comment_lengths=_ogg_calloc(vc->comments+1, sizeof(int));
227 for(i=0;i<vc->comments;i++){
228 int len=oggpack_read(opb,32);
229 if(len<0)goto err_out;
230 vc->comment_lengths[i]=len;
231 vc->user_comments[i]=_ogg_calloc(len+1,1);
232 _v_readstring(opb,vc->user_comments[i],len);
234 if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
238 vorbis_comment_clear(vc);
239 return(OV_EBADHEADER);
242 /* all of the real encoding details are here. The modes, books,
244 static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
245 codec_setup_info *ci=vi->codec_setup;
247 if(!ci)return(OV_EFAULT);
250 ci->books=oggpack_read(opb,8)+1;
251 /*ci->book_param=_ogg_calloc(ci->books,sizeof(static_codebook *));*/
252 for(i=0;i<ci->books;i++){
253 ci->book_param[i]=_ogg_calloc(1,sizeof(static_codebook));
254 if(vorbis_staticbook_unpack(opb,ci->book_param[i]))goto err_out;
257 /* time backend settings */
258 ci->times=oggpack_read(opb,6)+1;
259 /*ci->time_type=_ogg_malloc(ci->times*sizeof(int));*/
260 /*ci->time_param=_ogg_calloc(ci->times,sizeof(void *));*/
261 for(i=0;i<ci->times;i++){
262 ci->time_type[i]=oggpack_read(opb,16);
263 if(ci->time_type[i]<0 || ci->time_type[i]>=VI_TIMEB)goto err_out;
264 ci->time_param[i]=_time_P[ci->time_type[i]]->unpack(vi,opb);
265 if(!ci->time_param[i])goto err_out;
268 /* floor backend settings */
269 ci->floors=oggpack_read(opb,6)+1;
270 /*ci->floor_type=_ogg_malloc(ci->floors*sizeof(int));*/
271 /*ci->floor_param=_ogg_calloc(ci->floors,sizeof(void *));*/
272 for(i=0;i<ci->floors;i++){
273 ci->floor_type[i]=oggpack_read(opb,16);
274 if(ci->floor_type[i]<0 || ci->floor_type[i]>=VI_FLOORB)goto err_out;
275 ci->floor_param[i]=_floor_P[ci->floor_type[i]]->unpack(vi,opb);
276 if(!ci->floor_param[i])goto err_out;
279 /* residue backend settings */
280 ci->residues=oggpack_read(opb,6)+1;
281 /*ci->residue_type=_ogg_malloc(ci->residues*sizeof(int));*/
282 /*ci->residue_param=_ogg_calloc(ci->residues,sizeof(void *));*/
283 for(i=0;i<ci->residues;i++){
284 ci->residue_type[i]=oggpack_read(opb,16);
285 if(ci->residue_type[i]<0 || ci->residue_type[i]>=VI_RESB)goto err_out;
286 ci->residue_param[i]=_residue_P[ci->residue_type[i]]->unpack(vi,opb);
287 if(!ci->residue_param[i])goto err_out;
290 /* map backend settings */
291 ci->maps=oggpack_read(opb,6)+1;
292 /*ci->map_type=_ogg_malloc(ci->maps*sizeof(int));*/
293 /*ci->map_param=_ogg_calloc(ci->maps,sizeof(void *));*/
294 for(i=0;i<ci->maps;i++){
295 ci->map_type[i]=oggpack_read(opb,16);
296 if(ci->map_type[i]<0 || ci->map_type[i]>=VI_MAPB)goto err_out;
297 ci->map_param[i]=_mapping_P[ci->map_type[i]]->unpack(vi,opb);
298 if(!ci->map_param[i])goto err_out;
302 ci->modes=oggpack_read(opb,6)+1;
303 /*vi->mode_param=_ogg_calloc(vi->modes,sizeof(void *));*/
304 for(i=0;i<ci->modes;i++){
305 ci->mode_param[i]=_ogg_calloc(1,sizeof(vorbis_info_mode));
306 ci->mode_param[i]->blockflag=oggpack_read(opb,1);
307 ci->mode_param[i]->windowtype=oggpack_read(opb,16);
308 ci->mode_param[i]->transformtype=oggpack_read(opb,16);
309 ci->mode_param[i]->mapping=oggpack_read(opb,8);
311 if(ci->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
312 if(ci->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
313 if(ci->mode_param[i]->mapping>=ci->maps)goto err_out;
316 if(oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
320 vorbis_info_clear(vi);
321 return(OV_EBADHEADER);
324 /* The Vorbis header is in three packets; the initial small packet in
325 the first page that identifies basic parameters, a second packet
326 with bitstream comments and a third packet that holds the
329 int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
333 oggpack_readinit(&opb,op->packet,op->bytes);
335 /* Which of the three types of header is this? */
336 /* Also verify header-ness, vorbis */
339 int packtype=oggpack_read(&opb,8);
341 _v_readstring(&opb,buffer,6);
342 if(memcmp(buffer,"vorbis",6)){
343 /* not a vorbis header */
344 return(OV_ENOTVORBIS);
347 case 0x01: /* least significant *bit* is read first */
349 /* Not the initial packet */
350 return(OV_EBADHEADER);
353 /* previously initialized info header */
354 return(OV_EBADHEADER);
357 return(_vorbis_unpack_info(vi,&opb));
359 case 0x03: /* least significant *bit* is read first */
361 /* um... we didn't get the initial header */
362 return(OV_EBADHEADER);
365 return(_vorbis_unpack_comment(vc,&opb));
367 case 0x05: /* least significant *bit* is read first */
368 if(vi->rate==0 || vc->vendor==NULL){
369 /* um... we didn;t get the initial header or comments yet */
370 return(OV_EBADHEADER);
373 return(_vorbis_unpack_books(vi,&opb));
376 /* Not a valid vorbis header type */
377 return(OV_EBADHEADER);
382 return(OV_EBADHEADER);
385 /* pack side **********************************************************/
387 static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){
388 codec_setup_info *ci=vi->codec_setup;
389 if(!ci)return(OV_EFAULT);
392 oggpack_write(opb,0x01,8);
393 _v_writestring(opb,"vorbis");
395 /* basic information about the stream */
396 oggpack_write(opb,0x00,32);
397 oggpack_write(opb,vi->channels,8);
398 oggpack_write(opb,vi->rate,32);
400 oggpack_write(opb,vi->bitrate_upper,32);
401 oggpack_write(opb,vi->bitrate_nominal,32);
402 oggpack_write(opb,vi->bitrate_lower,32);
404 oggpack_write(opb,ilog2(ci->blocksizes[0]),4);
405 oggpack_write(opb,ilog2(ci->blocksizes[1]),4);
406 oggpack_write(opb,1,1);
411 static int _vorbis_pack_comment(oggpack_buffer *opb,vorbis_comment *vc){
412 char temp[]="Xiphophorus libVorbis I 20010201";
415 oggpack_write(opb,0x03,8);
416 _v_writestring(opb,"vorbis");
419 oggpack_write(opb,strlen(temp),32);
420 _v_writestring(opb,temp);
424 oggpack_write(opb,vc->comments,32);
427 for(i=0;i<vc->comments;i++){
428 if(vc->user_comments[i]){
429 oggpack_write(opb,vc->comment_lengths[i],32);
430 _v_writestring(opb,vc->user_comments[i]);
432 oggpack_write(opb,0,32);
436 oggpack_write(opb,1,1);
441 static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){
442 codec_setup_info *ci=vi->codec_setup;
444 if(!ci)return(OV_EFAULT);
446 oggpack_write(opb,0x05,8);
447 _v_writestring(opb,"vorbis");
450 oggpack_write(opb,ci->books-1,8);
451 for(i=0;i<ci->books;i++)
452 if(vorbis_staticbook_pack(ci->book_param[i],opb))goto err_out;
455 oggpack_write(opb,ci->times-1,6);
456 for(i=0;i<ci->times;i++){
457 oggpack_write(opb,ci->time_type[i],16);
458 _time_P[ci->time_type[i]]->pack(ci->time_param[i],opb);
462 oggpack_write(opb,ci->floors-1,6);
463 for(i=0;i<ci->floors;i++){
464 oggpack_write(opb,ci->floor_type[i],16);
465 _floor_P[ci->floor_type[i]]->pack(ci->floor_param[i],opb);
469 oggpack_write(opb,ci->residues-1,6);
470 for(i=0;i<ci->residues;i++){
471 oggpack_write(opb,ci->residue_type[i],16);
472 _residue_P[ci->residue_type[i]]->pack(ci->residue_param[i],opb);
476 oggpack_write(opb,ci->maps-1,6);
477 for(i=0;i<ci->maps;i++){
478 oggpack_write(opb,ci->map_type[i],16);
479 _mapping_P[ci->map_type[i]]->pack(vi,ci->map_param[i],opb);
483 oggpack_write(opb,ci->modes-1,6);
484 for(i=0;i<ci->modes;i++){
485 oggpack_write(opb,ci->mode_param[i]->blockflag,1);
486 oggpack_write(opb,ci->mode_param[i]->windowtype,16);
487 oggpack_write(opb,ci->mode_param[i]->transformtype,16);
488 oggpack_write(opb,ci->mode_param[i]->mapping,8);
490 oggpack_write(opb,1,1);
497 int vorbis_commentheader_out(vorbis_comment *vc,
502 oggpack_writeinit(&opb);
503 if(_vorbis_pack_comment(&opb,vc)) return OV_EIMPL;
505 op->packet = _ogg_malloc(oggpack_bytes(&opb));
506 memcpy(op->packet, opb.buffer, oggpack_bytes(&opb));
508 op->bytes=oggpack_bytes(&opb);
516 int vorbis_analysis_headerout(vorbis_dsp_state *v,
520 ogg_packet *op_code){
522 vorbis_info *vi=v->vi;
524 backend_lookup_state *b=v->backend_state;
531 /* first header packet **********************************************/
533 oggpack_writeinit(&opb);
534 if(_vorbis_pack_info(&opb,vi))goto err_out;
536 /* build the packet */
537 if(b->header)_ogg_free(b->header);
538 b->header=_ogg_malloc(oggpack_bytes(&opb));
539 memcpy(b->header,opb.buffer,oggpack_bytes(&opb));
540 op->packet=b->header;
541 op->bytes=oggpack_bytes(&opb);
546 /* second header packet (comments) **********************************/
549 if(_vorbis_pack_comment(&opb,vc))goto err_out;
551 if(b->header1)_ogg_free(b->header1);
552 b->header1=_ogg_malloc(oggpack_bytes(&opb));
553 memcpy(b->header1,opb.buffer,oggpack_bytes(&opb));
554 op_comm->packet=b->header1;
555 op_comm->bytes=oggpack_bytes(&opb);
558 op_comm->granulepos=0;
560 /* third header packet (modes/codebooks) ****************************/
563 if(_vorbis_pack_books(&opb,vi))goto err_out;
565 if(b->header2)_ogg_free(b->header2);
566 b->header2=_ogg_malloc(oggpack_bytes(&opb));
567 memcpy(b->header2,opb.buffer,oggpack_bytes(&opb));
568 op_code->packet=b->header2;
569 op_code->bytes=oggpack_bytes(&opb);
572 op_code->granulepos=0;
574 oggpack_writeclear(&opb);
577 oggpack_writeclear(&opb);
578 memset(op,0,sizeof(ogg_packet));
579 memset(op_comm,0,sizeof(ogg_packet));
580 memset(op_code,0,sizeof(ogg_packet));
582 if(b->header)_ogg_free(b->header);
583 if(b->header1)_ogg_free(b->header1);
584 if(b->header2)_ogg_free(b->header2);