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.29 2000/08/13 13:55:39 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)+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))
130 void vorbis_comment_clear(vorbis_comment *vc){
133 for(i=0;i<vc->comments;i++)
134 if(vc->user_comments[i])free(vc->user_comments[i]);
135 if(vc->user_comments)free(vc->user_comments);
136 if(vc->comment_lengths)free(vc->comment_lengths);
137 if(vc->vendor)free(vc->vendor);
139 memset(vc,0,sizeof(vorbis_comment));
142 /* used by synthesis, which has a full, alloced vi */
143 void vorbis_info_init(vorbis_info *vi){
144 memset(vi,0,sizeof(vorbis_info));
147 void vorbis_info_clear(vorbis_info *vi){
150 for(i=0;i<vi->modes;i++)
151 if(vi->mode_param[i])free(vi->mode_param[i]);
152 /*if(vi->mode_param)free(vi->mode_param);*/
154 for(i=0;i<vi->maps;i++) /* unpack does the range checking */
155 _mapping_P[vi->map_type[i]]->free_info(vi->map_param[i]);
156 /*if(vi->map_param)free(vi->map_param);*/
158 for(i=0;i<vi->times;i++) /* unpack does the range checking */
159 _time_P[vi->time_type[i]]->free_info(vi->time_param[i]);
160 /*if(vi->time_param)free(vi->time_param);*/
162 for(i=0;i<vi->floors;i++) /* unpack does the range checking */
163 _floor_P[vi->floor_type[i]]->free_info(vi->floor_param[i]);
164 /*if(vi->floor_param)free(vi->floor_param);*/
166 for(i=0;i<vi->residues;i++) /* unpack does the range checking */
167 _residue_P[vi->residue_type[i]]->free_info(vi->residue_param[i]);
168 /*if(vi->residue_param)free(vi->residue_param);*/
170 /* the static codebooks *are* freed if you call info_clear, because
171 decode side does alloc a 'static' codebook. Calling clear on the
172 full codebook does not clear the static codebook (that's our
174 for(i=0;i<vi->books;i++){
175 /* just in case the decoder pre-cleared to save space */
176 if(vi->book_param[i]){
177 vorbis_staticbook_clear(vi->book_param[i]);
178 free(vi->book_param[i]);
181 /*if(vi->book_param)free(vi->book_param);*/
183 for(i=0;i<vi->psys;i++)
184 _vi_psy_free(vi->psy_param[i]);
185 /*if(vi->psy_param)free(vi->psy_param);*/
187 memset(vi,0,sizeof(vorbis_info));
190 /* Header packing/unpacking ********************************************/
192 static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
193 vi->version=_oggpack_read(opb,32);
194 if(vi->version!=0)return(-1);
196 vi->channels=_oggpack_read(opb,8);
197 vi->rate=_oggpack_read(opb,32);
199 vi->bitrate_upper=_oggpack_read(opb,32);
200 vi->bitrate_nominal=_oggpack_read(opb,32);
201 vi->bitrate_lower=_oggpack_read(opb,32);
203 vi->blocksizes[0]=1<<_oggpack_read(opb,4);
204 vi->blocksizes[1]=1<<_oggpack_read(opb,4);
206 if(vi->rate<1)goto err_out;
207 if(vi->channels<1)goto err_out;
208 if(vi->blocksizes[0]<8)goto err_out;
209 if(vi->blocksizes[1]<vi->blocksizes[0])goto err_out;
211 if(_oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
215 vorbis_info_clear(vi);
219 static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
221 int vendorlen=_oggpack_read(opb,32);
222 if(vendorlen<0)goto err_out;
223 vc->vendor=calloc(vendorlen+1,1);
224 _v_readstring(opb,vc->vendor,vendorlen);
225 vc->comments=_oggpack_read(opb,32);
226 if(vc->comments<0)goto err_out;
227 vc->user_comments=calloc(vc->comments+1,sizeof(char **));
228 vc->comment_lengths=calloc(vc->comments+1, sizeof(int));
230 for(i=0;i<vc->comments;i++){
231 int len=_oggpack_read(opb,32);
232 if(len<0)goto err_out;
233 vc->comment_lengths[i]=len;
234 vc->user_comments[i]=calloc(len+1,1);
235 _v_readstring(opb,vc->user_comments[i],len);
237 if(_oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
241 vorbis_comment_clear(vc);
245 /* all of the real encoding details are here. The modes, books,
247 static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
251 vi->books=_oggpack_read(opb,8)+1;
252 /*vi->book_param=calloc(vi->books,sizeof(static_codebook *));*/
253 for(i=0;i<vi->books;i++){
254 vi->book_param[i]=calloc(1,sizeof(static_codebook));
255 if(vorbis_staticbook_unpack(opb,vi->book_param[i]))goto err_out;
258 /* time backend settings */
259 vi->times=_oggpack_read(opb,6)+1;
260 /*vi->time_type=malloc(vi->times*sizeof(int));*/
261 /*vi->time_param=calloc(vi->times,sizeof(void *));*/
262 for(i=0;i<vi->times;i++){
263 vi->time_type[i]=_oggpack_read(opb,16);
264 if(vi->time_type[i]<0 || vi->time_type[i]>=VI_TIMEB)goto err_out;
265 vi->time_param[i]=_time_P[vi->time_type[i]]->unpack(vi,opb);
266 if(!vi->time_param[i])goto err_out;
269 /* floor backend settings */
270 vi->floors=_oggpack_read(opb,6)+1;
271 /*vi->floor_type=malloc(vi->floors*sizeof(int));*/
272 /*vi->floor_param=calloc(vi->floors,sizeof(void *));*/
273 for(i=0;i<vi->floors;i++){
274 vi->floor_type[i]=_oggpack_read(opb,16);
275 if(vi->floor_type[i]<0 || vi->floor_type[i]>=VI_FLOORB)goto err_out;
276 vi->floor_param[i]=_floor_P[vi->floor_type[i]]->unpack(vi,opb);
277 if(!vi->floor_param[i])goto err_out;
280 /* residue backend settings */
281 vi->residues=_oggpack_read(opb,6)+1;
282 /*vi->residue_type=malloc(vi->residues*sizeof(int));*/
283 /*vi->residue_param=calloc(vi->residues,sizeof(void *));*/
284 for(i=0;i<vi->residues;i++){
285 vi->residue_type[i]=_oggpack_read(opb,16);
286 if(vi->residue_type[i]<0 || vi->residue_type[i]>=VI_RESB)goto err_out;
287 vi->residue_param[i]=_residue_P[vi->residue_type[i]]->unpack(vi,opb);
288 if(!vi->residue_param[i])goto err_out;
291 /* map backend settings */
292 vi->maps=_oggpack_read(opb,6)+1;
293 /*vi->map_type=malloc(vi->maps*sizeof(int));*/
294 /*vi->map_param=calloc(vi->maps,sizeof(void *));*/
295 for(i=0;i<vi->maps;i++){
296 vi->map_type[i]=_oggpack_read(opb,16);
297 if(vi->map_type[i]<0 || vi->map_type[i]>=VI_MAPB)goto err_out;
298 vi->map_param[i]=_mapping_P[vi->map_type[i]]->unpack(vi,opb);
299 if(!vi->map_param[i])goto err_out;
303 vi->modes=_oggpack_read(opb,6)+1;
304 /*vi->mode_param=calloc(vi->modes,sizeof(void *));*/
305 for(i=0;i<vi->modes;i++){
306 vi->mode_param[i]=calloc(1,sizeof(vorbis_info_mode));
307 vi->mode_param[i]->blockflag=_oggpack_read(opb,1);
308 vi->mode_param[i]->windowtype=_oggpack_read(opb,16);
309 vi->mode_param[i]->transformtype=_oggpack_read(opb,16);
310 vi->mode_param[i]->mapping=_oggpack_read(opb,8);
312 if(vi->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
313 if(vi->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
314 if(vi->mode_param[i]->mapping>=vi->maps)goto err_out;
317 if(_oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
321 vorbis_info_clear(vi);
325 /* The Vorbis header is in three packets; the initial small packet in
326 the first page that identifies basic parameters, a second packet
327 with bitstream comments and a third packet that holds the
330 int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
334 _oggpack_readinit(&opb,op->packet,op->bytes);
336 /* Which of the three types of header is this? */
337 /* Also verify header-ness, vorbis */
340 int packtype=_oggpack_read(&opb,8);
342 _v_readstring(&opb,buffer,6);
343 if(memcmp(buffer,"vorbis",6)){
344 /* not a vorbis header */
348 case 0x01: /* least significant *bit* is read first */
350 /* Not the initial packet */
354 /* previously initialized info header */
358 return(_vorbis_unpack_info(vi,&opb));
360 case 0x03: /* least significant *bit* is read first */
362 /* um... we didn't get the initial header */
366 return(_vorbis_unpack_comment(vc,&opb));
368 case 0x05: /* least significant *bit* is read first */
369 if(vi->rate==0 || vc->vendor==NULL){
370 /* um... we didn;t get the initial header or comments yet */
374 return(_vorbis_unpack_books(vi,&opb));
377 /* Not a valid vorbis header type */
386 /* pack side **********************************************************/
388 static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){
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(vi->blocksizes[0]),4);
403 _oggpack_write(opb,ilog2(vi->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 20000508";
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){
441 _oggpack_write(opb,0x05,8);
442 _v_writestring(opb,"vorbis");
445 _oggpack_write(opb,vi->books-1,8);
446 for(i=0;i<vi->books;i++)
447 if(vorbis_staticbook_pack(vi->book_param[i],opb))goto err_out;
450 _oggpack_write(opb,vi->times-1,6);
451 for(i=0;i<vi->times;i++){
452 _oggpack_write(opb,vi->time_type[i],16);
453 _time_P[vi->time_type[i]]->pack(vi->time_param[i],opb);
457 _oggpack_write(opb,vi->floors-1,6);
458 for(i=0;i<vi->floors;i++){
459 _oggpack_write(opb,vi->floor_type[i],16);
460 _floor_P[vi->floor_type[i]]->pack(vi->floor_param[i],opb);
464 _oggpack_write(opb,vi->residues-1,6);
465 for(i=0;i<vi->residues;i++){
466 _oggpack_write(opb,vi->residue_type[i],16);
467 _residue_P[vi->residue_type[i]]->pack(vi->residue_param[i],opb);
471 _oggpack_write(opb,vi->maps-1,6);
472 for(i=0;i<vi->maps;i++){
473 _oggpack_write(opb,vi->map_type[i],16);
474 _mapping_P[vi->map_type[i]]->pack(vi,vi->map_param[i],opb);
478 _oggpack_write(opb,vi->modes-1,6);
479 for(i=0;i<vi->modes;i++){
480 _oggpack_write(opb,vi->mode_param[i]->blockflag,1);
481 _oggpack_write(opb,vi->mode_param[i]->windowtype,16);
482 _oggpack_write(opb,vi->mode_param[i]->transformtype,16);
483 _oggpack_write(opb,vi->mode_param[i]->mapping,8);
485 _oggpack_write(opb,1,1);
492 int vorbis_analysis_headerout(vorbis_dsp_state *v,
496 ogg_packet *op_code){
497 vorbis_info *vi=v->vi;
500 /* first header packet **********************************************/
502 _oggpack_writeinit(&opb);
503 if(_vorbis_pack_info(&opb,vi))goto err_out;
505 /* build the packet */
506 if(v->header)free(v->header);
507 v->header=malloc(_oggpack_bytes(&opb));
508 memcpy(v->header,opb.buffer,_oggpack_bytes(&opb));
509 op->packet=v->header;
510 op->bytes=_oggpack_bytes(&opb);
515 /* second header packet (comments) **********************************/
517 _oggpack_reset(&opb);
518 if(_vorbis_pack_comment(&opb,vc))goto err_out;
520 if(v->header1)free(v->header1);
521 v->header1=malloc(_oggpack_bytes(&opb));
522 memcpy(v->header1,opb.buffer,_oggpack_bytes(&opb));
523 op_comm->packet=v->header1;
524 op_comm->bytes=_oggpack_bytes(&opb);
529 /* third header packet (modes/codebooks) ****************************/
531 _oggpack_reset(&opb);
532 if(_vorbis_pack_books(&opb,vi))goto err_out;
534 if(v->header2)free(v->header2);
535 v->header2=malloc(_oggpack_bytes(&opb));
536 memcpy(v->header2,opb.buffer,_oggpack_bytes(&opb));
537 op_code->packet=v->header2;
538 op_code->bytes=_oggpack_bytes(&opb);
543 _oggpack_writeclear(&opb);
546 _oggpack_writeclear(&opb);
547 memset(op,0,sizeof(ogg_packet));
548 memset(op_comm,0,sizeof(ogg_packet));
549 memset(op_code,0,sizeof(ogg_packet));
551 if(v->header)free(v->header);
552 if(v->header1)free(v->header1);
553 if(v->header2)free(v->header2);