1 /********************************************************************
3 * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
5 * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *
6 * PLEASE READ THESE TERMS DISTRIBUTING. *
8 * THE OggSQUISH 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.28 2000/08/01 13:45:13 msmith Exp $
17 ********************************************************************/
19 /* general handling of the header and the vorbis_info structure (and
25 #include "vorbis/codec.h"
26 #include "vorbis/backends.h"
28 #include "sharedbook.h"
29 #include "bookinternal.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=realloc(vc->user_comments,
64 (vc->comments+2)*sizeof(char *));
65 vc->comment_lengths=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);
97 char *fulltag = alloca(taglen+ 2);
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 + 1;
111 return NULL; /* didn't find anything */
114 void vorbis_comment_clear(vorbis_comment *vc){
117 for(i=0;i<vc->comments;i++)
118 if(vc->user_comments[i])free(vc->user_comments[i]);
119 if(vc->user_comments)free(vc->user_comments);
120 if(vc->comment_lengths)free(vc->comment_lengths);
121 if(vc->vendor)free(vc->vendor);
123 memset(vc,0,sizeof(vorbis_comment));
126 /* used by synthesis, which has a full, alloced vi */
127 void vorbis_info_init(vorbis_info *vi){
128 memset(vi,0,sizeof(vorbis_info));
131 void vorbis_info_clear(vorbis_info *vi){
134 for(i=0;i<vi->modes;i++)
135 if(vi->mode_param[i])free(vi->mode_param[i]);
136 /*if(vi->mode_param)free(vi->mode_param);*/
138 for(i=0;i<vi->maps;i++) /* unpack does the range checking */
139 _mapping_P[vi->map_type[i]]->free_info(vi->map_param[i]);
140 /*if(vi->map_param)free(vi->map_param);*/
142 for(i=0;i<vi->times;i++) /* unpack does the range checking */
143 _time_P[vi->time_type[i]]->free_info(vi->time_param[i]);
144 /*if(vi->time_param)free(vi->time_param);*/
146 for(i=0;i<vi->floors;i++) /* unpack does the range checking */
147 _floor_P[vi->floor_type[i]]->free_info(vi->floor_param[i]);
148 /*if(vi->floor_param)free(vi->floor_param);*/
150 for(i=0;i<vi->residues;i++) /* unpack does the range checking */
151 _residue_P[vi->residue_type[i]]->free_info(vi->residue_param[i]);
152 /*if(vi->residue_param)free(vi->residue_param);*/
154 /* the static codebooks *are* freed if you call info_clear, because
155 decode side does alloc a 'static' codebook. Calling clear on the
156 full codebook does not clear the static codebook (that's our
158 for(i=0;i<vi->books;i++){
159 /* just in case the decoder pre-cleared to save space */
160 if(vi->book_param[i]){
161 vorbis_staticbook_clear(vi->book_param[i]);
162 free(vi->book_param[i]);
165 /*if(vi->book_param)free(vi->book_param);*/
167 for(i=0;i<vi->psys;i++)
168 _vi_psy_free(vi->psy_param[i]);
169 /*if(vi->psy_param)free(vi->psy_param);*/
171 memset(vi,0,sizeof(vorbis_info));
174 /* Header packing/unpacking ********************************************/
176 static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
177 vi->version=_oggpack_read(opb,32);
178 if(vi->version!=0)return(-1);
180 vi->channels=_oggpack_read(opb,8);
181 vi->rate=_oggpack_read(opb,32);
183 vi->bitrate_upper=_oggpack_read(opb,32);
184 vi->bitrate_nominal=_oggpack_read(opb,32);
185 vi->bitrate_lower=_oggpack_read(opb,32);
187 vi->blocksizes[0]=1<<_oggpack_read(opb,4);
188 vi->blocksizes[1]=1<<_oggpack_read(opb,4);
190 if(vi->rate<1)goto err_out;
191 if(vi->channels<1)goto err_out;
192 if(vi->blocksizes[0]<8)goto err_out;
193 if(vi->blocksizes[1]<vi->blocksizes[0])goto err_out;
195 if(_oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
199 vorbis_info_clear(vi);
203 static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
205 int vendorlen=_oggpack_read(opb,32);
206 if(vendorlen<0)goto err_out;
207 vc->vendor=calloc(vendorlen+1,1);
208 _v_readstring(opb,vc->vendor,vendorlen);
209 vc->comments=_oggpack_read(opb,32);
210 if(vc->comments<0)goto err_out;
211 vc->user_comments=calloc(vc->comments+1,sizeof(char **));
212 vc->comment_lengths=calloc(vc->comments+1, sizeof(int));
214 for(i=0;i<vc->comments;i++){
215 int len=_oggpack_read(opb,32);
216 if(len<0)goto err_out;
217 vc->comment_lengths[i]=len;
218 vc->user_comments[i]=calloc(len+1,1);
219 _v_readstring(opb,vc->user_comments[i],len);
221 if(_oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
225 vorbis_comment_clear(vc);
229 /* all of the real encoding details are here. The modes, books,
231 static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
235 vi->books=_oggpack_read(opb,8)+1;
236 /*vi->book_param=calloc(vi->books,sizeof(static_codebook *));*/
237 for(i=0;i<vi->books;i++){
238 vi->book_param[i]=calloc(1,sizeof(static_codebook));
239 if(vorbis_staticbook_unpack(opb,vi->book_param[i]))goto err_out;
242 /* time backend settings */
243 vi->times=_oggpack_read(opb,6)+1;
244 /*vi->time_type=malloc(vi->times*sizeof(int));*/
245 /*vi->time_param=calloc(vi->times,sizeof(void *));*/
246 for(i=0;i<vi->times;i++){
247 vi->time_type[i]=_oggpack_read(opb,16);
248 if(vi->time_type[i]<0 || vi->time_type[i]>=VI_TIMEB)goto err_out;
249 vi->time_param[i]=_time_P[vi->time_type[i]]->unpack(vi,opb);
250 if(!vi->time_param[i])goto err_out;
253 /* floor backend settings */
254 vi->floors=_oggpack_read(opb,6)+1;
255 /*vi->floor_type=malloc(vi->floors*sizeof(int));*/
256 /*vi->floor_param=calloc(vi->floors,sizeof(void *));*/
257 for(i=0;i<vi->floors;i++){
258 vi->floor_type[i]=_oggpack_read(opb,16);
259 if(vi->floor_type[i]<0 || vi->floor_type[i]>=VI_FLOORB)goto err_out;
260 vi->floor_param[i]=_floor_P[vi->floor_type[i]]->unpack(vi,opb);
261 if(!vi->floor_param[i])goto err_out;
264 /* residue backend settings */
265 vi->residues=_oggpack_read(opb,6)+1;
266 /*vi->residue_type=malloc(vi->residues*sizeof(int));*/
267 /*vi->residue_param=calloc(vi->residues,sizeof(void *));*/
268 for(i=0;i<vi->residues;i++){
269 vi->residue_type[i]=_oggpack_read(opb,16);
270 if(vi->residue_type[i]<0 || vi->residue_type[i]>=VI_RESB)goto err_out;
271 vi->residue_param[i]=_residue_P[vi->residue_type[i]]->unpack(vi,opb);
272 if(!vi->residue_param[i])goto err_out;
275 /* map backend settings */
276 vi->maps=_oggpack_read(opb,6)+1;
277 /*vi->map_type=malloc(vi->maps*sizeof(int));*/
278 /*vi->map_param=calloc(vi->maps,sizeof(void *));*/
279 for(i=0;i<vi->maps;i++){
280 vi->map_type[i]=_oggpack_read(opb,16);
281 if(vi->map_type[i]<0 || vi->map_type[i]>=VI_MAPB)goto err_out;
282 vi->map_param[i]=_mapping_P[vi->map_type[i]]->unpack(vi,opb);
283 if(!vi->map_param[i])goto err_out;
287 vi->modes=_oggpack_read(opb,6)+1;
288 /*vi->mode_param=calloc(vi->modes,sizeof(void *));*/
289 for(i=0;i<vi->modes;i++){
290 vi->mode_param[i]=calloc(1,sizeof(vorbis_info_mode));
291 vi->mode_param[i]->blockflag=_oggpack_read(opb,1);
292 vi->mode_param[i]->windowtype=_oggpack_read(opb,16);
293 vi->mode_param[i]->transformtype=_oggpack_read(opb,16);
294 vi->mode_param[i]->mapping=_oggpack_read(opb,8);
296 if(vi->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
297 if(vi->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
298 if(vi->mode_param[i]->mapping>=vi->maps)goto err_out;
301 if(_oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
305 vorbis_info_clear(vi);
309 /* The Vorbis header is in three packets; the initial small packet in
310 the first page that identifies basic parameters, a second packet
311 with bitstream comments and a third packet that holds the
314 int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
318 _oggpack_readinit(&opb,op->packet,op->bytes);
320 /* Which of the three types of header is this? */
321 /* Also verify header-ness, vorbis */
324 int packtype=_oggpack_read(&opb,8);
326 _v_readstring(&opb,buffer,6);
327 if(memcmp(buffer,"vorbis",6)){
328 /* not a vorbis header */
332 case 0x01: /* least significant *bit* is read first */
334 /* Not the initial packet */
338 /* previously initialized info header */
342 return(_vorbis_unpack_info(vi,&opb));
344 case 0x03: /* least significant *bit* is read first */
346 /* um... we didn't get the initial header */
350 return(_vorbis_unpack_comment(vc,&opb));
352 case 0x05: /* least significant *bit* is read first */
353 if(vi->rate==0 || vc->vendor==NULL){
354 /* um... we didn;t get the initial header or comments yet */
358 return(_vorbis_unpack_books(vi,&opb));
361 /* Not a valid vorbis header type */
370 /* pack side **********************************************************/
372 static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){
374 _oggpack_write(opb,0x01,8);
375 _v_writestring(opb,"vorbis");
377 /* basic information about the stream */
378 _oggpack_write(opb,0x00,32);
379 _oggpack_write(opb,vi->channels,8);
380 _oggpack_write(opb,vi->rate,32);
382 _oggpack_write(opb,vi->bitrate_upper,32);
383 _oggpack_write(opb,vi->bitrate_nominal,32);
384 _oggpack_write(opb,vi->bitrate_lower,32);
386 _oggpack_write(opb,ilog2(vi->blocksizes[0]),4);
387 _oggpack_write(opb,ilog2(vi->blocksizes[1]),4);
388 _oggpack_write(opb,1,1);
393 static int _vorbis_pack_comment(oggpack_buffer *opb,vorbis_comment *vc){
394 char temp[]="Xiphophorus libVorbis I 20000508";
397 _oggpack_write(opb,0x03,8);
398 _v_writestring(opb,"vorbis");
401 _oggpack_write(opb,strlen(temp),32);
402 _v_writestring(opb,temp);
406 _oggpack_write(opb,vc->comments,32);
409 for(i=0;i<vc->comments;i++){
410 if(vc->user_comments[i]){
411 _oggpack_write(opb,vc->comment_lengths[i],32);
412 _v_writestring(opb,vc->user_comments[i]);
414 _oggpack_write(opb,0,32);
418 _oggpack_write(opb,1,1);
423 static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){
425 _oggpack_write(opb,0x05,8);
426 _v_writestring(opb,"vorbis");
429 _oggpack_write(opb,vi->books-1,8);
430 for(i=0;i<vi->books;i++)
431 if(vorbis_staticbook_pack(vi->book_param[i],opb))goto err_out;
434 _oggpack_write(opb,vi->times-1,6);
435 for(i=0;i<vi->times;i++){
436 _oggpack_write(opb,vi->time_type[i],16);
437 _time_P[vi->time_type[i]]->pack(vi->time_param[i],opb);
441 _oggpack_write(opb,vi->floors-1,6);
442 for(i=0;i<vi->floors;i++){
443 _oggpack_write(opb,vi->floor_type[i],16);
444 _floor_P[vi->floor_type[i]]->pack(vi->floor_param[i],opb);
448 _oggpack_write(opb,vi->residues-1,6);
449 for(i=0;i<vi->residues;i++){
450 _oggpack_write(opb,vi->residue_type[i],16);
451 _residue_P[vi->residue_type[i]]->pack(vi->residue_param[i],opb);
455 _oggpack_write(opb,vi->maps-1,6);
456 for(i=0;i<vi->maps;i++){
457 _oggpack_write(opb,vi->map_type[i],16);
458 _mapping_P[vi->map_type[i]]->pack(vi,vi->map_param[i],opb);
462 _oggpack_write(opb,vi->modes-1,6);
463 for(i=0;i<vi->modes;i++){
464 _oggpack_write(opb,vi->mode_param[i]->blockflag,1);
465 _oggpack_write(opb,vi->mode_param[i]->windowtype,16);
466 _oggpack_write(opb,vi->mode_param[i]->transformtype,16);
467 _oggpack_write(opb,vi->mode_param[i]->mapping,8);
469 _oggpack_write(opb,1,1);
476 int vorbis_analysis_headerout(vorbis_dsp_state *v,
480 ogg_packet *op_code){
481 vorbis_info *vi=v->vi;
484 /* first header packet **********************************************/
486 _oggpack_writeinit(&opb);
487 if(_vorbis_pack_info(&opb,vi))goto err_out;
489 /* build the packet */
490 if(v->header)free(v->header);
491 v->header=malloc(_oggpack_bytes(&opb));
492 memcpy(v->header,opb.buffer,_oggpack_bytes(&opb));
493 op->packet=v->header;
494 op->bytes=_oggpack_bytes(&opb);
499 /* second header packet (comments) **********************************/
501 _oggpack_reset(&opb);
502 if(_vorbis_pack_comment(&opb,vc))goto err_out;
504 if(v->header1)free(v->header1);
505 v->header1=malloc(_oggpack_bytes(&opb));
506 memcpy(v->header1,opb.buffer,_oggpack_bytes(&opb));
507 op_comm->packet=v->header1;
508 op_comm->bytes=_oggpack_bytes(&opb);
513 /* third header packet (modes/codebooks) ****************************/
515 _oggpack_reset(&opb);
516 if(_vorbis_pack_books(&opb,vi))goto err_out;
518 if(v->header2)free(v->header2);
519 v->header2=malloc(_oggpack_bytes(&opb));
520 memcpy(v->header2,opb.buffer,_oggpack_bytes(&opb));
521 op_code->packet=v->header2;
522 op_code->bytes=_oggpack_bytes(&opb);
527 _oggpack_writeclear(&opb);
530 _oggpack_writeclear(&opb);
531 memset(op,0,sizeof(ogg_packet));
532 memset(op_comm,0,sizeof(ogg_packet));
533 memset(op_code,0,sizeof(ogg_packet));
535 if(v->header)free(v->header);
536 if(v->header1)free(v->header1);
537 if(v->header2)free(v->header2);