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.26 2000/07/29 08:45:33 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 */
76 comment[strlen(tag)] = '=';
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);
98 for(i=0;i<vc->comments;i++){
99 if(!tagcompare(vc->user_comments[i], tag, taglen)){
101 /* We return a pointer to the data, not a copy */
102 return vc->user_comments[i] + taglen + 1;
107 return NULL; /* didn't find anything */
110 void vorbis_comment_clear(vorbis_comment *vc){
113 for(i=0;i<vc->comments;i++)
114 if(vc->user_comments[i])free(vc->user_comments[i]);
115 if(vc->user_comments)free(vc->user_comments);
116 if(vc->vendor)free(vc->vendor);
118 memset(vc,0,sizeof(vorbis_comment));
121 /* used by synthesis, which has a full, alloced vi */
122 void vorbis_info_init(vorbis_info *vi){
123 memset(vi,0,sizeof(vorbis_info));
126 void vorbis_info_clear(vorbis_info *vi){
129 for(i=0;i<vi->modes;i++)
130 if(vi->mode_param[i])free(vi->mode_param[i]);
131 /*if(vi->mode_param)free(vi->mode_param);*/
133 for(i=0;i<vi->maps;i++) /* unpack does the range checking */
134 _mapping_P[vi->map_type[i]]->free_info(vi->map_param[i]);
135 /*if(vi->map_param)free(vi->map_param);*/
137 for(i=0;i<vi->times;i++) /* unpack does the range checking */
138 _time_P[vi->time_type[i]]->free_info(vi->time_param[i]);
139 /*if(vi->time_param)free(vi->time_param);*/
141 for(i=0;i<vi->floors;i++) /* unpack does the range checking */
142 _floor_P[vi->floor_type[i]]->free_info(vi->floor_param[i]);
143 /*if(vi->floor_param)free(vi->floor_param);*/
145 for(i=0;i<vi->residues;i++) /* unpack does the range checking */
146 _residue_P[vi->residue_type[i]]->free_info(vi->residue_param[i]);
147 /*if(vi->residue_param)free(vi->residue_param);*/
149 /* the static codebooks *are* freed if you call info_clear, because
150 decode side does alloc a 'static' codebook. Calling clear on the
151 full codebook does not clear the static codebook (that's our
153 for(i=0;i<vi->books;i++){
154 /* just in case the decoder pre-cleared to save space */
155 if(vi->book_param[i]){
156 vorbis_staticbook_clear(vi->book_param[i]);
157 free(vi->book_param[i]);
160 /*if(vi->book_param)free(vi->book_param);*/
162 for(i=0;i<vi->psys;i++)
163 _vi_psy_free(vi->psy_param[i]);
164 /*if(vi->psy_param)free(vi->psy_param);*/
166 memset(vi,0,sizeof(vorbis_info));
169 /* Header packing/unpacking ********************************************/
171 static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
172 vi->version=_oggpack_read(opb,32);
173 if(vi->version!=0)return(-1);
175 vi->channels=_oggpack_read(opb,8);
176 vi->rate=_oggpack_read(opb,32);
178 vi->bitrate_upper=_oggpack_read(opb,32);
179 vi->bitrate_nominal=_oggpack_read(opb,32);
180 vi->bitrate_lower=_oggpack_read(opb,32);
182 vi->blocksizes[0]=1<<_oggpack_read(opb,4);
183 vi->blocksizes[1]=1<<_oggpack_read(opb,4);
185 if(vi->rate<1)goto err_out;
186 if(vi->channels<1)goto err_out;
187 if(vi->blocksizes[0]<8)goto err_out;
188 if(vi->blocksizes[1]<vi->blocksizes[0])goto err_out;
190 if(_oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
194 vorbis_info_clear(vi);
198 static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
200 int vendorlen=_oggpack_read(opb,32);
201 if(vendorlen<0)goto err_out;
202 vc->vendor=calloc(vendorlen+1,1);
203 _v_readstring(opb,vc->vendor,vendorlen);
204 vc->comments=_oggpack_read(opb,32);
205 if(vc->comments<0)goto err_out;
206 vc->user_comments=calloc(vc->comments+1,sizeof(char **));
207 vc->comment_lengths=calloc(vc->comments+1, sizeof(int));
209 for(i=0;i<vc->comments;i++){
210 int len=_oggpack_read(opb,32);
211 if(len<0)goto err_out;
212 vc->comment_lengths[i]=len;
213 vc->user_comments[i]=calloc(len+1,1);
214 _v_readstring(opb,vc->user_comments[i],len);
216 if(_oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
220 vorbis_comment_clear(vc);
224 /* all of the real encoding details are here. The modes, books,
226 static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
230 vi->books=_oggpack_read(opb,8)+1;
231 /*vi->book_param=calloc(vi->books,sizeof(static_codebook *));*/
232 for(i=0;i<vi->books;i++){
233 vi->book_param[i]=calloc(1,sizeof(static_codebook));
234 if(vorbis_staticbook_unpack(opb,vi->book_param[i]))goto err_out;
237 /* time backend settings */
238 vi->times=_oggpack_read(opb,6)+1;
239 /*vi->time_type=malloc(vi->times*sizeof(int));*/
240 /*vi->time_param=calloc(vi->times,sizeof(void *));*/
241 for(i=0;i<vi->times;i++){
242 vi->time_type[i]=_oggpack_read(opb,16);
243 if(vi->time_type[i]<0 || vi->time_type[i]>=VI_TIMEB)goto err_out;
244 vi->time_param[i]=_time_P[vi->time_type[i]]->unpack(vi,opb);
245 if(!vi->time_param[i])goto err_out;
248 /* floor backend settings */
249 vi->floors=_oggpack_read(opb,6)+1;
250 /*vi->floor_type=malloc(vi->floors*sizeof(int));*/
251 /*vi->floor_param=calloc(vi->floors,sizeof(void *));*/
252 for(i=0;i<vi->floors;i++){
253 vi->floor_type[i]=_oggpack_read(opb,16);
254 if(vi->floor_type[i]<0 || vi->floor_type[i]>=VI_FLOORB)goto err_out;
255 vi->floor_param[i]=_floor_P[vi->floor_type[i]]->unpack(vi,opb);
256 if(!vi->floor_param[i])goto err_out;
259 /* residue backend settings */
260 vi->residues=_oggpack_read(opb,6)+1;
261 /*vi->residue_type=malloc(vi->residues*sizeof(int));*/
262 /*vi->residue_param=calloc(vi->residues,sizeof(void *));*/
263 for(i=0;i<vi->residues;i++){
264 vi->residue_type[i]=_oggpack_read(opb,16);
265 if(vi->residue_type[i]<0 || vi->residue_type[i]>=VI_RESB)goto err_out;
266 vi->residue_param[i]=_residue_P[vi->residue_type[i]]->unpack(vi,opb);
267 if(!vi->residue_param[i])goto err_out;
270 /* map backend settings */
271 vi->maps=_oggpack_read(opb,6)+1;
272 /*vi->map_type=malloc(vi->maps*sizeof(int));*/
273 /*vi->map_param=calloc(vi->maps,sizeof(void *));*/
274 for(i=0;i<vi->maps;i++){
275 vi->map_type[i]=_oggpack_read(opb,16);
276 if(vi->map_type[i]<0 || vi->map_type[i]>=VI_MAPB)goto err_out;
277 vi->map_param[i]=_mapping_P[vi->map_type[i]]->unpack(vi,opb);
278 if(!vi->map_param[i])goto err_out;
282 vi->modes=_oggpack_read(opb,6)+1;
283 /*vi->mode_param=calloc(vi->modes,sizeof(void *));*/
284 for(i=0;i<vi->modes;i++){
285 vi->mode_param[i]=calloc(1,sizeof(vorbis_info_mode));
286 vi->mode_param[i]->blockflag=_oggpack_read(opb,1);
287 vi->mode_param[i]->windowtype=_oggpack_read(opb,16);
288 vi->mode_param[i]->transformtype=_oggpack_read(opb,16);
289 vi->mode_param[i]->mapping=_oggpack_read(opb,8);
291 if(vi->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
292 if(vi->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
293 if(vi->mode_param[i]->mapping>=vi->maps)goto err_out;
296 if(_oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
300 vorbis_info_clear(vi);
304 /* The Vorbis header is in three packets; the initial small packet in
305 the first page that identifies basic parameters, a second packet
306 with bitstream comments and a third packet that holds the
309 int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
313 _oggpack_readinit(&opb,op->packet,op->bytes);
315 /* Which of the three types of header is this? */
316 /* Also verify header-ness, vorbis */
319 int packtype=_oggpack_read(&opb,8);
321 _v_readstring(&opb,buffer,6);
322 if(memcmp(buffer,"vorbis",6)){
323 /* not a vorbis header */
327 case 0x01: /* least significant *bit* is read first */
329 /* Not the initial packet */
333 /* previously initialized info header */
337 return(_vorbis_unpack_info(vi,&opb));
339 case 0x03: /* least significant *bit* is read first */
341 /* um... we didn't get the initial header */
345 return(_vorbis_unpack_comment(vc,&opb));
347 case 0x05: /* least significant *bit* is read first */
348 if(vi->rate==0 || vc->vendor==NULL){
349 /* um... we didn;t get the initial header or comments yet */
353 return(_vorbis_unpack_books(vi,&opb));
356 /* Not a valid vorbis header type */
365 /* pack side **********************************************************/
367 static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){
369 _oggpack_write(opb,0x01,8);
370 _v_writestring(opb,"vorbis");
372 /* basic information about the stream */
373 _oggpack_write(opb,0x00,32);
374 _oggpack_write(opb,vi->channels,8);
375 _oggpack_write(opb,vi->rate,32);
377 _oggpack_write(opb,vi->bitrate_upper,32);
378 _oggpack_write(opb,vi->bitrate_nominal,32);
379 _oggpack_write(opb,vi->bitrate_lower,32);
381 _oggpack_write(opb,ilog2(vi->blocksizes[0]),4);
382 _oggpack_write(opb,ilog2(vi->blocksizes[1]),4);
383 _oggpack_write(opb,1,1);
388 static int _vorbis_pack_comment(oggpack_buffer *opb,vorbis_comment *vc){
389 char temp[]="Xiphophorus libVorbis I 20000508";
392 _oggpack_write(opb,0x03,8);
393 _v_writestring(opb,"vorbis");
396 _oggpack_write(opb,strlen(temp),32);
397 _v_writestring(opb,temp);
401 _oggpack_write(opb,vc->comments,32);
404 for(i=0;i<vc->comments;i++){
405 if(vc->user_comments[i]){
406 _oggpack_write(opb,vc->comment_lengths[i],32);
407 _v_writestring(opb,vc->user_comments[i]);
409 _oggpack_write(opb,0,32);
413 _oggpack_write(opb,1,1);
418 static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){
420 _oggpack_write(opb,0x05,8);
421 _v_writestring(opb,"vorbis");
424 _oggpack_write(opb,vi->books-1,8);
425 for(i=0;i<vi->books;i++)
426 if(vorbis_staticbook_pack(vi->book_param[i],opb))goto err_out;
429 _oggpack_write(opb,vi->times-1,6);
430 for(i=0;i<vi->times;i++){
431 _oggpack_write(opb,vi->time_type[i],16);
432 _time_P[vi->time_type[i]]->pack(vi->time_param[i],opb);
436 _oggpack_write(opb,vi->floors-1,6);
437 for(i=0;i<vi->floors;i++){
438 _oggpack_write(opb,vi->floor_type[i],16);
439 _floor_P[vi->floor_type[i]]->pack(vi->floor_param[i],opb);
443 _oggpack_write(opb,vi->residues-1,6);
444 for(i=0;i<vi->residues;i++){
445 _oggpack_write(opb,vi->residue_type[i],16);
446 _residue_P[vi->residue_type[i]]->pack(vi->residue_param[i],opb);
450 _oggpack_write(opb,vi->maps-1,6);
451 for(i=0;i<vi->maps;i++){
452 _oggpack_write(opb,vi->map_type[i],16);
453 _mapping_P[vi->map_type[i]]->pack(vi,vi->map_param[i],opb);
457 _oggpack_write(opb,vi->modes-1,6);
458 for(i=0;i<vi->modes;i++){
459 _oggpack_write(opb,vi->mode_param[i]->blockflag,1);
460 _oggpack_write(opb,vi->mode_param[i]->windowtype,16);
461 _oggpack_write(opb,vi->mode_param[i]->transformtype,16);
462 _oggpack_write(opb,vi->mode_param[i]->mapping,8);
464 _oggpack_write(opb,1,1);
471 int vorbis_analysis_headerout(vorbis_dsp_state *v,
475 ogg_packet *op_code){
476 vorbis_info *vi=v->vi;
479 /* first header packet **********************************************/
481 _oggpack_writeinit(&opb);
482 if(_vorbis_pack_info(&opb,vi))goto err_out;
484 /* build the packet */
485 if(v->header)free(v->header);
486 v->header=malloc(_oggpack_bytes(&opb));
487 memcpy(v->header,opb.buffer,_oggpack_bytes(&opb));
488 op->packet=v->header;
489 op->bytes=_oggpack_bytes(&opb);
494 /* second header packet (comments) **********************************/
496 _oggpack_reset(&opb);
497 if(_vorbis_pack_comment(&opb,vc))goto err_out;
499 if(v->header1)free(v->header1);
500 v->header1=malloc(_oggpack_bytes(&opb));
501 memcpy(v->header1,opb.buffer,_oggpack_bytes(&opb));
502 op_comm->packet=v->header1;
503 op_comm->bytes=_oggpack_bytes(&opb);
508 /* third header packet (modes/codebooks) ****************************/
510 _oggpack_reset(&opb);
511 if(_vorbis_pack_books(&opb,vi))goto err_out;
513 if(v->header2)free(v->header2);
514 v->header2=malloc(_oggpack_bytes(&opb));
515 memcpy(v->header2,opb.buffer,_oggpack_bytes(&opb));
516 op_code->packet=v->header2;
517 op_code->bytes=_oggpack_bytes(&opb);
522 _oggpack_writeclear(&opb);
525 _oggpack_writeclear(&opb);
526 memset(op,0,sizeof(ogg_packet));
527 memset(op_comm,0,sizeof(ogg_packet));
528 memset(op_code,0,sizeof(ogg_packet));
530 if(v->header)free(v->header);
531 if(v->header1)free(v->header1);
532 if(v->header2)free(v->header2);