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.30 2000/08/15 09:09:43 xiphmont 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))
129 void vorbis_comment_clear(vorbis_comment *vc){
132 for(i=0;i<vc->comments;i++)
133 if(vc->user_comments[i])free(vc->user_comments[i]);
134 if(vc->user_comments)free(vc->user_comments);
135 if(vc->comment_lengths)free(vc->comment_lengths);
136 if(vc->vendor)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));
146 void vorbis_info_clear(vorbis_info *vi){
149 for(i=0;i<vi->modes;i++)
150 if(vi->mode_param[i])free(vi->mode_param[i]);
151 /*if(vi->mode_param)free(vi->mode_param);*/
153 for(i=0;i<vi->maps;i++) /* unpack does the range checking */
154 _mapping_P[vi->map_type[i]]->free_info(vi->map_param[i]);
155 /*if(vi->map_param)free(vi->map_param);*/
157 for(i=0;i<vi->times;i++) /* unpack does the range checking */
158 _time_P[vi->time_type[i]]->free_info(vi->time_param[i]);
159 /*if(vi->time_param)free(vi->time_param);*/
161 for(i=0;i<vi->floors;i++) /* unpack does the range checking */
162 _floor_P[vi->floor_type[i]]->free_info(vi->floor_param[i]);
163 /*if(vi->floor_param)free(vi->floor_param);*/
165 for(i=0;i<vi->residues;i++) /* unpack does the range checking */
166 _residue_P[vi->residue_type[i]]->free_info(vi->residue_param[i]);
167 /*if(vi->residue_param)free(vi->residue_param);*/
169 /* the static codebooks *are* freed if you call info_clear, because
170 decode side does alloc a 'static' codebook. Calling clear on the
171 full codebook does not clear the static codebook (that's our
173 for(i=0;i<vi->books;i++){
174 /* just in case the decoder pre-cleared to save space */
175 if(vi->book_param[i]){
176 vorbis_staticbook_clear(vi->book_param[i]);
177 free(vi->book_param[i]);
180 /*if(vi->book_param)free(vi->book_param);*/
182 for(i=0;i<vi->psys;i++)
183 _vi_psy_free(vi->psy_param[i]);
184 /*if(vi->psy_param)free(vi->psy_param);*/
186 memset(vi,0,sizeof(vorbis_info));
189 /* Header packing/unpacking ********************************************/
191 static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
192 vi->version=_oggpack_read(opb,32);
193 if(vi->version!=0)return(-1);
195 vi->channels=_oggpack_read(opb,8);
196 vi->rate=_oggpack_read(opb,32);
198 vi->bitrate_upper=_oggpack_read(opb,32);
199 vi->bitrate_nominal=_oggpack_read(opb,32);
200 vi->bitrate_lower=_oggpack_read(opb,32);
202 vi->blocksizes[0]=1<<_oggpack_read(opb,4);
203 vi->blocksizes[1]=1<<_oggpack_read(opb,4);
205 if(vi->rate<1)goto err_out;
206 if(vi->channels<1)goto err_out;
207 if(vi->blocksizes[0]<8)goto err_out;
208 if(vi->blocksizes[1]<vi->blocksizes[0])goto err_out;
210 if(_oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
214 vorbis_info_clear(vi);
218 static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
220 int vendorlen=_oggpack_read(opb,32);
221 if(vendorlen<0)goto err_out;
222 vc->vendor=calloc(vendorlen+1,1);
223 _v_readstring(opb,vc->vendor,vendorlen);
224 vc->comments=_oggpack_read(opb,32);
225 if(vc->comments<0)goto err_out;
226 vc->user_comments=calloc(vc->comments+1,sizeof(char **));
227 vc->comment_lengths=calloc(vc->comments+1, sizeof(int));
229 for(i=0;i<vc->comments;i++){
230 int len=_oggpack_read(opb,32);
231 if(len<0)goto err_out;
232 vc->comment_lengths[i]=len;
233 vc->user_comments[i]=calloc(len+1,1);
234 _v_readstring(opb,vc->user_comments[i],len);
236 if(_oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
240 vorbis_comment_clear(vc);
244 /* all of the real encoding details are here. The modes, books,
246 static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
250 vi->books=_oggpack_read(opb,8)+1;
251 /*vi->book_param=calloc(vi->books,sizeof(static_codebook *));*/
252 for(i=0;i<vi->books;i++){
253 vi->book_param[i]=calloc(1,sizeof(static_codebook));
254 if(vorbis_staticbook_unpack(opb,vi->book_param[i]))goto err_out;
257 /* time backend settings */
258 vi->times=_oggpack_read(opb,6)+1;
259 /*vi->time_type=malloc(vi->times*sizeof(int));*/
260 /*vi->time_param=calloc(vi->times,sizeof(void *));*/
261 for(i=0;i<vi->times;i++){
262 vi->time_type[i]=_oggpack_read(opb,16);
263 if(vi->time_type[i]<0 || vi->time_type[i]>=VI_TIMEB)goto err_out;
264 vi->time_param[i]=_time_P[vi->time_type[i]]->unpack(vi,opb);
265 if(!vi->time_param[i])goto err_out;
268 /* floor backend settings */
269 vi->floors=_oggpack_read(opb,6)+1;
270 /*vi->floor_type=malloc(vi->floors*sizeof(int));*/
271 /*vi->floor_param=calloc(vi->floors,sizeof(void *));*/
272 for(i=0;i<vi->floors;i++){
273 vi->floor_type[i]=_oggpack_read(opb,16);
274 if(vi->floor_type[i]<0 || vi->floor_type[i]>=VI_FLOORB)goto err_out;
275 vi->floor_param[i]=_floor_P[vi->floor_type[i]]->unpack(vi,opb);
276 if(!vi->floor_param[i])goto err_out;
279 /* residue backend settings */
280 vi->residues=_oggpack_read(opb,6)+1;
281 /*vi->residue_type=malloc(vi->residues*sizeof(int));*/
282 /*vi->residue_param=calloc(vi->residues,sizeof(void *));*/
283 for(i=0;i<vi->residues;i++){
284 vi->residue_type[i]=_oggpack_read(opb,16);
285 if(vi->residue_type[i]<0 || vi->residue_type[i]>=VI_RESB)goto err_out;
286 vi->residue_param[i]=_residue_P[vi->residue_type[i]]->unpack(vi,opb);
287 if(!vi->residue_param[i])goto err_out;
290 /* map backend settings */
291 vi->maps=_oggpack_read(opb,6)+1;
292 /*vi->map_type=malloc(vi->maps*sizeof(int));*/
293 /*vi->map_param=calloc(vi->maps,sizeof(void *));*/
294 for(i=0;i<vi->maps;i++){
295 vi->map_type[i]=_oggpack_read(opb,16);
296 if(vi->map_type[i]<0 || vi->map_type[i]>=VI_MAPB)goto err_out;
297 vi->map_param[i]=_mapping_P[vi->map_type[i]]->unpack(vi,opb);
298 if(!vi->map_param[i])goto err_out;
302 vi->modes=_oggpack_read(opb,6)+1;
303 /*vi->mode_param=calloc(vi->modes,sizeof(void *));*/
304 for(i=0;i<vi->modes;i++){
305 vi->mode_param[i]=calloc(1,sizeof(vorbis_info_mode));
306 vi->mode_param[i]->blockflag=_oggpack_read(opb,1);
307 vi->mode_param[i]->windowtype=_oggpack_read(opb,16);
308 vi->mode_param[i]->transformtype=_oggpack_read(opb,16);
309 vi->mode_param[i]->mapping=_oggpack_read(opb,8);
311 if(vi->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
312 if(vi->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
313 if(vi->mode_param[i]->mapping>=vi->maps)goto err_out;
316 if(_oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
320 vorbis_info_clear(vi);
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 */
347 case 0x01: /* least significant *bit* is read first */
349 /* Not the initial packet */
353 /* previously initialized info header */
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 */
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 */
373 return(_vorbis_unpack_books(vi,&opb));
376 /* Not a valid vorbis header type */
385 /* pack side **********************************************************/
387 static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){
389 _oggpack_write(opb,0x01,8);
390 _v_writestring(opb,"vorbis");
392 /* basic information about the stream */
393 _oggpack_write(opb,0x00,32);
394 _oggpack_write(opb,vi->channels,8);
395 _oggpack_write(opb,vi->rate,32);
397 _oggpack_write(opb,vi->bitrate_upper,32);
398 _oggpack_write(opb,vi->bitrate_nominal,32);
399 _oggpack_write(opb,vi->bitrate_lower,32);
401 _oggpack_write(opb,ilog2(vi->blocksizes[0]),4);
402 _oggpack_write(opb,ilog2(vi->blocksizes[1]),4);
403 _oggpack_write(opb,1,1);
408 static int _vorbis_pack_comment(oggpack_buffer *opb,vorbis_comment *vc){
409 char temp[]="Xiphophorus libVorbis I 20000508";
412 _oggpack_write(opb,0x03,8);
413 _v_writestring(opb,"vorbis");
416 _oggpack_write(opb,strlen(temp),32);
417 _v_writestring(opb,temp);
421 _oggpack_write(opb,vc->comments,32);
424 for(i=0;i<vc->comments;i++){
425 if(vc->user_comments[i]){
426 _oggpack_write(opb,vc->comment_lengths[i],32);
427 _v_writestring(opb,vc->user_comments[i]);
429 _oggpack_write(opb,0,32);
433 _oggpack_write(opb,1,1);
438 static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){
440 _oggpack_write(opb,0x05,8);
441 _v_writestring(opb,"vorbis");
444 _oggpack_write(opb,vi->books-1,8);
445 for(i=0;i<vi->books;i++)
446 if(vorbis_staticbook_pack(vi->book_param[i],opb))goto err_out;
449 _oggpack_write(opb,vi->times-1,6);
450 for(i=0;i<vi->times;i++){
451 _oggpack_write(opb,vi->time_type[i],16);
452 _time_P[vi->time_type[i]]->pack(vi->time_param[i],opb);
456 _oggpack_write(opb,vi->floors-1,6);
457 for(i=0;i<vi->floors;i++){
458 _oggpack_write(opb,vi->floor_type[i],16);
459 _floor_P[vi->floor_type[i]]->pack(vi->floor_param[i],opb);
463 _oggpack_write(opb,vi->residues-1,6);
464 for(i=0;i<vi->residues;i++){
465 _oggpack_write(opb,vi->residue_type[i],16);
466 _residue_P[vi->residue_type[i]]->pack(vi->residue_param[i],opb);
470 _oggpack_write(opb,vi->maps-1,6);
471 for(i=0;i<vi->maps;i++){
472 _oggpack_write(opb,vi->map_type[i],16);
473 _mapping_P[vi->map_type[i]]->pack(vi,vi->map_param[i],opb);
477 _oggpack_write(opb,vi->modes-1,6);
478 for(i=0;i<vi->modes;i++){
479 _oggpack_write(opb,vi->mode_param[i]->blockflag,1);
480 _oggpack_write(opb,vi->mode_param[i]->windowtype,16);
481 _oggpack_write(opb,vi->mode_param[i]->transformtype,16);
482 _oggpack_write(opb,vi->mode_param[i]->mapping,8);
484 _oggpack_write(opb,1,1);
491 int vorbis_analysis_headerout(vorbis_dsp_state *v,
495 ogg_packet *op_code){
496 vorbis_info *vi=v->vi;
499 /* first header packet **********************************************/
501 _oggpack_writeinit(&opb);
502 if(_vorbis_pack_info(&opb,vi))goto err_out;
504 /* build the packet */
505 if(v->header)free(v->header);
506 v->header=malloc(_oggpack_bytes(&opb));
507 memcpy(v->header,opb.buffer,_oggpack_bytes(&opb));
508 op->packet=v->header;
509 op->bytes=_oggpack_bytes(&opb);
514 /* second header packet (comments) **********************************/
516 _oggpack_reset(&opb);
517 if(_vorbis_pack_comment(&opb,vc))goto err_out;
519 if(v->header1)free(v->header1);
520 v->header1=malloc(_oggpack_bytes(&opb));
521 memcpy(v->header1,opb.buffer,_oggpack_bytes(&opb));
522 op_comm->packet=v->header1;
523 op_comm->bytes=_oggpack_bytes(&opb);
528 /* third header packet (modes/codebooks) ****************************/
530 _oggpack_reset(&opb);
531 if(_vorbis_pack_books(&opb,vi))goto err_out;
533 if(v->header2)free(v->header2);
534 v->header2=malloc(_oggpack_bytes(&opb));
535 memcpy(v->header2,opb.buffer,_oggpack_bytes(&opb));
536 op_code->packet=v->header2;
537 op_code->bytes=_oggpack_bytes(&opb);
542 _oggpack_writeclear(&opb);
545 _oggpack_writeclear(&opb);
546 memset(op,0,sizeof(ogg_packet));
547 memset(op_comm,0,sizeof(ogg_packet));
548 memset(op_code,0,sizeof(ogg_packet));
550 if(v->header)free(v->header);
551 if(v->header1)free(v->header1);
552 if(v->header2)free(v->header2);