Oops. Couple of significant (though not usually fatal) bugs in the new comment API...
[platform/upstream/libvorbis.git] / lib / info.c
1 /********************************************************************
2  *                                                                  *
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.                            *
7  *                                                                  *
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/                                             *
11  *                                                                  *
12  ********************************************************************
13
14  function: maintain the info structure, info <-> header packets
15  last mod: $Id: info.c,v 1.27 2000/07/30 13:10:10 msmith Exp $
16
17  ********************************************************************/
18
19 /* general handling of the header and the vorbis_info structure (and
20    substructures) */
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include "vorbis/codec.h"
26 #include "vorbis/backends.h"
27 #include "bitwise.h"
28 #include "sharedbook.h"
29 #include "bookinternal.h"
30 #include "registry.h"
31 #include "window.h"
32 #include "psy.h"
33 #include "misc.h"
34 #include "os.h"
35
36 /* helpers */
37 static int ilog2(unsigned int v){
38   int ret=0;
39   while(v>1){
40     ret++;
41     v>>=1;
42   }
43   return(ret);
44 }
45
46 static void _v_writestring(oggpack_buffer *o,char *s){
47   while(*s){
48     _oggpack_write(o,*s++,8);
49   }
50 }
51
52 static void _v_readstring(oggpack_buffer *o,char *buf,int bytes){
53   while(bytes--){
54     *buf++=_oggpack_read(o,8);
55   }
56 }
57
58 void vorbis_comment_init(vorbis_comment *vc){
59   memset(vc,0,sizeof(vorbis_comment));
60 }
61
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);
69   vc->comments++;
70   vc->user_comments[vc->comments]=NULL;
71 }
72
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 */
75   strcpy(comment, tag);
76   strcat(comment, "=");
77   strcat(comment, contents);
78   vorbis_comment_add(vc, comment);
79 }
80
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){
84   int c=0;
85   while(c < n){
86     if(toupper(s1[c]) != toupper(s2[c]))
87       return !0;
88     c++;
89   }
90   return 0;
91 }
92
93 char *vorbis_comment_query(vorbis_comment *vc, char *tag, int count){
94   long i;
95   int found = 0;
96   int taglen = strlen(tag);
97   char *fulltag = alloca(taglen+ 2);
98
99   strcpy(fulltag, tag);
100   strcat(fulltag, "=");
101   
102   for(i=0;i<vc->comments;i++){
103     if(!tagcompare(vc->user_comments[i], fulltag, taglen)){
104       if(count == found)
105         /* We return a pointer to the data, not a copy */
106         return vc->user_comments[i] + taglen + 1;
107       else
108         found++;
109     }
110   }
111   return NULL; /* didn't find anything */
112 }
113
114 void vorbis_comment_clear(vorbis_comment *vc){
115   if(vc){
116     long i;
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->vendor)free(vc->vendor);
121   }
122   memset(vc,0,sizeof(vorbis_comment));
123 }
124
125 /* used by synthesis, which has a full, alloced vi */
126 void vorbis_info_init(vorbis_info *vi){
127   memset(vi,0,sizeof(vorbis_info));
128 }
129
130 void vorbis_info_clear(vorbis_info *vi){
131   int i;
132
133   for(i=0;i<vi->modes;i++)
134     if(vi->mode_param[i])free(vi->mode_param[i]);
135   /*if(vi->mode_param)free(vi->mode_param);*/
136  
137   for(i=0;i<vi->maps;i++) /* unpack does the range checking */
138     _mapping_P[vi->map_type[i]]->free_info(vi->map_param[i]);
139   /*if(vi->map_param)free(vi->map_param);*/
140     
141   for(i=0;i<vi->times;i++) /* unpack does the range checking */
142     _time_P[vi->time_type[i]]->free_info(vi->time_param[i]);
143   /*if(vi->time_param)free(vi->time_param);*/
144     
145   for(i=0;i<vi->floors;i++) /* unpack does the range checking */
146     _floor_P[vi->floor_type[i]]->free_info(vi->floor_param[i]);
147   /*if(vi->floor_param)free(vi->floor_param);*/
148     
149   for(i=0;i<vi->residues;i++) /* unpack does the range checking */
150     _residue_P[vi->residue_type[i]]->free_info(vi->residue_param[i]);
151   /*if(vi->residue_param)free(vi->residue_param);*/
152
153   /* the static codebooks *are* freed if you call info_clear, because
154      decode side does alloc a 'static' codebook. Calling clear on the
155      full codebook does not clear the static codebook (that's our
156      responsibility) */
157   for(i=0;i<vi->books;i++){
158     /* just in case the decoder pre-cleared to save space */
159     if(vi->book_param[i]){
160       vorbis_staticbook_clear(vi->book_param[i]);
161       free(vi->book_param[i]);
162     }
163   }
164   /*if(vi->book_param)free(vi->book_param);*/
165
166   for(i=0;i<vi->psys;i++)
167     _vi_psy_free(vi->psy_param[i]);
168   /*if(vi->psy_param)free(vi->psy_param);*/
169   
170   memset(vi,0,sizeof(vorbis_info));
171 }
172
173 /* Header packing/unpacking ********************************************/
174
175 static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
176   vi->version=_oggpack_read(opb,32);
177   if(vi->version!=0)return(-1);
178
179   vi->channels=_oggpack_read(opb,8);
180   vi->rate=_oggpack_read(opb,32);
181
182   vi->bitrate_upper=_oggpack_read(opb,32);
183   vi->bitrate_nominal=_oggpack_read(opb,32);
184   vi->bitrate_lower=_oggpack_read(opb,32);
185
186   vi->blocksizes[0]=1<<_oggpack_read(opb,4);
187   vi->blocksizes[1]=1<<_oggpack_read(opb,4);
188   
189   if(vi->rate<1)goto err_out;
190   if(vi->channels<1)goto err_out;
191   if(vi->blocksizes[0]<8)goto err_out; 
192   if(vi->blocksizes[1]<vi->blocksizes[0])goto err_out;
193   
194   if(_oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
195
196   return(0);
197  err_out:
198   vorbis_info_clear(vi);
199   return(-1);
200 }
201
202 static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
203   int i;
204   int vendorlen=_oggpack_read(opb,32);
205   if(vendorlen<0)goto err_out;
206   vc->vendor=calloc(vendorlen+1,1);
207   _v_readstring(opb,vc->vendor,vendorlen);
208   vc->comments=_oggpack_read(opb,32);
209   if(vc->comments<0)goto err_out;
210   vc->user_comments=calloc(vc->comments+1,sizeof(char **));
211   vc->comment_lengths=calloc(vc->comments+1, sizeof(int));
212             
213   for(i=0;i<vc->comments;i++){
214     int len=_oggpack_read(opb,32);
215     if(len<0)goto err_out;
216         vc->comment_lengths[i]=len;
217     vc->user_comments[i]=calloc(len+1,1);
218     _v_readstring(opb,vc->user_comments[i],len);
219   }       
220   if(_oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
221
222   return(0);
223  err_out:
224   vorbis_comment_clear(vc);
225   return(-1);
226 }
227
228 /* all of the real encoding details are here.  The modes, books,
229    everything */
230 static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
231   int i;
232
233   /* codebooks */
234   vi->books=_oggpack_read(opb,8)+1;
235   /*vi->book_param=calloc(vi->books,sizeof(static_codebook *));*/
236   for(i=0;i<vi->books;i++){
237     vi->book_param[i]=calloc(1,sizeof(static_codebook));
238     if(vorbis_staticbook_unpack(opb,vi->book_param[i]))goto err_out;
239   }
240
241   /* time backend settings */
242   vi->times=_oggpack_read(opb,6)+1;
243   /*vi->time_type=malloc(vi->times*sizeof(int));*/
244   /*vi->time_param=calloc(vi->times,sizeof(void *));*/
245   for(i=0;i<vi->times;i++){
246     vi->time_type[i]=_oggpack_read(opb,16);
247     if(vi->time_type[i]<0 || vi->time_type[i]>=VI_TIMEB)goto err_out;
248     vi->time_param[i]=_time_P[vi->time_type[i]]->unpack(vi,opb);
249     if(!vi->time_param[i])goto err_out;
250   }
251
252   /* floor backend settings */
253   vi->floors=_oggpack_read(opb,6)+1;
254   /*vi->floor_type=malloc(vi->floors*sizeof(int));*/
255   /*vi->floor_param=calloc(vi->floors,sizeof(void *));*/
256   for(i=0;i<vi->floors;i++){
257     vi->floor_type[i]=_oggpack_read(opb,16);
258     if(vi->floor_type[i]<0 || vi->floor_type[i]>=VI_FLOORB)goto err_out;
259     vi->floor_param[i]=_floor_P[vi->floor_type[i]]->unpack(vi,opb);
260     if(!vi->floor_param[i])goto err_out;
261   }
262
263   /* residue backend settings */
264   vi->residues=_oggpack_read(opb,6)+1;
265   /*vi->residue_type=malloc(vi->residues*sizeof(int));*/
266   /*vi->residue_param=calloc(vi->residues,sizeof(void *));*/
267   for(i=0;i<vi->residues;i++){
268     vi->residue_type[i]=_oggpack_read(opb,16);
269     if(vi->residue_type[i]<0 || vi->residue_type[i]>=VI_RESB)goto err_out;
270     vi->residue_param[i]=_residue_P[vi->residue_type[i]]->unpack(vi,opb);
271     if(!vi->residue_param[i])goto err_out;
272   }
273
274   /* map backend settings */
275   vi->maps=_oggpack_read(opb,6)+1;
276   /*vi->map_type=malloc(vi->maps*sizeof(int));*/
277   /*vi->map_param=calloc(vi->maps,sizeof(void *));*/
278   for(i=0;i<vi->maps;i++){
279     vi->map_type[i]=_oggpack_read(opb,16);
280     if(vi->map_type[i]<0 || vi->map_type[i]>=VI_MAPB)goto err_out;
281     vi->map_param[i]=_mapping_P[vi->map_type[i]]->unpack(vi,opb);
282     if(!vi->map_param[i])goto err_out;
283   }
284   
285   /* mode settings */
286   vi->modes=_oggpack_read(opb,6)+1;
287   /*vi->mode_param=calloc(vi->modes,sizeof(void *));*/
288   for(i=0;i<vi->modes;i++){
289     vi->mode_param[i]=calloc(1,sizeof(vorbis_info_mode));
290     vi->mode_param[i]->blockflag=_oggpack_read(opb,1);
291     vi->mode_param[i]->windowtype=_oggpack_read(opb,16);
292     vi->mode_param[i]->transformtype=_oggpack_read(opb,16);
293     vi->mode_param[i]->mapping=_oggpack_read(opb,8);
294
295     if(vi->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
296     if(vi->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
297     if(vi->mode_param[i]->mapping>=vi->maps)goto err_out;
298   }
299   
300   if(_oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
301
302   return(0);
303  err_out:
304   vorbis_info_clear(vi);
305   return(-1);
306 }
307
308 /* The Vorbis header is in three packets; the initial small packet in
309    the first page that identifies basic parameters, a second packet
310    with bitstream comments and a third packet that holds the
311    codebook. */
312
313 int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
314   oggpack_buffer opb;
315   
316   if(op){
317     _oggpack_readinit(&opb,op->packet,op->bytes);
318
319     /* Which of the three types of header is this? */
320     /* Also verify header-ness, vorbis */
321     {
322       char buffer[6];
323       int packtype=_oggpack_read(&opb,8);
324       memset(buffer,0,6);
325       _v_readstring(&opb,buffer,6);
326       if(memcmp(buffer,"vorbis",6)){
327         /* not a vorbis header */
328         return(-1);
329       }
330       switch(packtype){
331       case 0x01: /* least significant *bit* is read first */
332         if(!op->b_o_s){
333           /* Not the initial packet */
334           return(-1);
335         }
336         if(vi->rate!=0){
337           /* previously initialized info header */
338           return(-1);
339         }
340
341         return(_vorbis_unpack_info(vi,&opb));
342
343       case 0x03: /* least significant *bit* is read first */
344         if(vi->rate==0){
345           /* um... we didn't get the initial header */
346           return(-1);
347         }
348
349         return(_vorbis_unpack_comment(vc,&opb));
350
351       case 0x05: /* least significant *bit* is read first */
352         if(vi->rate==0 || vc->vendor==NULL){
353           /* um... we didn;t get the initial header or comments yet */
354           return(-1);
355         }
356
357         return(_vorbis_unpack_books(vi,&opb));
358
359       default:
360         /* Not a valid vorbis header type */
361         return(-1);
362         break;
363       }
364     }
365   }
366   return(-1);
367 }
368
369 /* pack side **********************************************************/
370
371 static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){
372   /* preamble */  
373   _oggpack_write(opb,0x01,8);
374   _v_writestring(opb,"vorbis");
375
376   /* basic information about the stream */
377   _oggpack_write(opb,0x00,32);
378   _oggpack_write(opb,vi->channels,8);
379   _oggpack_write(opb,vi->rate,32);
380
381   _oggpack_write(opb,vi->bitrate_upper,32);
382   _oggpack_write(opb,vi->bitrate_nominal,32);
383   _oggpack_write(opb,vi->bitrate_lower,32);
384
385   _oggpack_write(opb,ilog2(vi->blocksizes[0]),4);
386   _oggpack_write(opb,ilog2(vi->blocksizes[1]),4);
387   _oggpack_write(opb,1,1);
388
389   return(0);
390 }
391
392 static int _vorbis_pack_comment(oggpack_buffer *opb,vorbis_comment *vc){
393   char temp[]="Xiphophorus libVorbis I 20000508";
394
395   /* preamble */  
396   _oggpack_write(opb,0x03,8);
397   _v_writestring(opb,"vorbis");
398
399   /* vendor */
400   _oggpack_write(opb,strlen(temp),32);
401   _v_writestring(opb,temp);
402   
403   /* comments */
404
405   _oggpack_write(opb,vc->comments,32);
406   if(vc->comments){
407     int i;
408     for(i=0;i<vc->comments;i++){
409       if(vc->user_comments[i]){
410         _oggpack_write(opb,vc->comment_lengths[i],32);
411         _v_writestring(opb,vc->user_comments[i]);
412       }else{
413         _oggpack_write(opb,0,32);
414       }
415     }
416   }
417   _oggpack_write(opb,1,1);
418
419   return(0);
420 }
421  
422 static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){
423   int i;
424   _oggpack_write(opb,0x05,8);
425   _v_writestring(opb,"vorbis");
426
427   /* books */
428   _oggpack_write(opb,vi->books-1,8);
429   for(i=0;i<vi->books;i++)
430     if(vorbis_staticbook_pack(vi->book_param[i],opb))goto err_out;
431
432   /* times */
433   _oggpack_write(opb,vi->times-1,6);
434   for(i=0;i<vi->times;i++){
435     _oggpack_write(opb,vi->time_type[i],16);
436     _time_P[vi->time_type[i]]->pack(vi->time_param[i],opb);
437   }
438
439   /* floors */
440   _oggpack_write(opb,vi->floors-1,6);
441   for(i=0;i<vi->floors;i++){
442     _oggpack_write(opb,vi->floor_type[i],16);
443     _floor_P[vi->floor_type[i]]->pack(vi->floor_param[i],opb);
444   }
445
446   /* residues */
447   _oggpack_write(opb,vi->residues-1,6);
448   for(i=0;i<vi->residues;i++){
449     _oggpack_write(opb,vi->residue_type[i],16);
450     _residue_P[vi->residue_type[i]]->pack(vi->residue_param[i],opb);
451   }
452
453   /* maps */
454   _oggpack_write(opb,vi->maps-1,6);
455   for(i=0;i<vi->maps;i++){
456     _oggpack_write(opb,vi->map_type[i],16);
457     _mapping_P[vi->map_type[i]]->pack(vi,vi->map_param[i],opb);
458   }
459
460   /* modes */
461   _oggpack_write(opb,vi->modes-1,6);
462   for(i=0;i<vi->modes;i++){
463     _oggpack_write(opb,vi->mode_param[i]->blockflag,1);
464     _oggpack_write(opb,vi->mode_param[i]->windowtype,16);
465     _oggpack_write(opb,vi->mode_param[i]->transformtype,16);
466     _oggpack_write(opb,vi->mode_param[i]->mapping,8);
467   }
468   _oggpack_write(opb,1,1);
469
470   return(0);
471 err_out:
472   return(-1);
473
474
475 int vorbis_analysis_headerout(vorbis_dsp_state *v,
476                               vorbis_comment *vc,
477                               ogg_packet *op,
478                               ogg_packet *op_comm,
479                               ogg_packet *op_code){
480   vorbis_info *vi=v->vi;
481   oggpack_buffer opb;
482
483   /* first header packet **********************************************/
484
485   _oggpack_writeinit(&opb);
486   if(_vorbis_pack_info(&opb,vi))goto err_out;
487
488   /* build the packet */
489   if(v->header)free(v->header);
490   v->header=malloc(_oggpack_bytes(&opb));
491   memcpy(v->header,opb.buffer,_oggpack_bytes(&opb));
492   op->packet=v->header;
493   op->bytes=_oggpack_bytes(&opb);
494   op->b_o_s=1;
495   op->e_o_s=0;
496   op->frameno=0;
497
498   /* second header packet (comments) **********************************/
499
500   _oggpack_reset(&opb);
501   if(_vorbis_pack_comment(&opb,vc))goto err_out;
502
503   if(v->header1)free(v->header1);
504   v->header1=malloc(_oggpack_bytes(&opb));
505   memcpy(v->header1,opb.buffer,_oggpack_bytes(&opb));
506   op_comm->packet=v->header1;
507   op_comm->bytes=_oggpack_bytes(&opb);
508   op_comm->b_o_s=0;
509   op_comm->e_o_s=0;
510   op_comm->frameno=0;
511
512   /* third header packet (modes/codebooks) ****************************/
513
514   _oggpack_reset(&opb);
515   if(_vorbis_pack_books(&opb,vi))goto err_out;
516
517   if(v->header2)free(v->header2);
518   v->header2=malloc(_oggpack_bytes(&opb));
519   memcpy(v->header2,opb.buffer,_oggpack_bytes(&opb));
520   op_code->packet=v->header2;
521   op_code->bytes=_oggpack_bytes(&opb);
522   op_code->b_o_s=0;
523   op_code->e_o_s=0;
524   op_code->frameno=0;
525
526   _oggpack_writeclear(&opb);
527   return(0);
528  err_out:
529   _oggpack_writeclear(&opb);
530   memset(op,0,sizeof(ogg_packet));
531   memset(op_comm,0,sizeof(ogg_packet));
532   memset(op_code,0,sizeof(ogg_packet));
533
534   if(v->header)free(v->header);
535   if(v->header1)free(v->header1);
536   if(v->header2)free(v->header2);
537   v->header=NULL;
538   v->header1=NULL;
539   v->header2=NULL;
540   return(-1);
541 }
542