Fix for a tiny memory leak.
[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.28 2000/08/01 13:45:13 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->comment_lengths)free(vc->comment_lengths);
121     if(vc->vendor)free(vc->vendor);
122   }
123   memset(vc,0,sizeof(vorbis_comment));
124 }
125
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));
129 }
130
131 void vorbis_info_clear(vorbis_info *vi){
132   int i;
133
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);*/
137  
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);*/
141     
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);*/
145     
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);*/
149     
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);*/
153
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
157      responsibility) */
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]);
163     }
164   }
165   /*if(vi->book_param)free(vi->book_param);*/
166
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);*/
170   
171   memset(vi,0,sizeof(vorbis_info));
172 }
173
174 /* Header packing/unpacking ********************************************/
175
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);
179
180   vi->channels=_oggpack_read(opb,8);
181   vi->rate=_oggpack_read(opb,32);
182
183   vi->bitrate_upper=_oggpack_read(opb,32);
184   vi->bitrate_nominal=_oggpack_read(opb,32);
185   vi->bitrate_lower=_oggpack_read(opb,32);
186
187   vi->blocksizes[0]=1<<_oggpack_read(opb,4);
188   vi->blocksizes[1]=1<<_oggpack_read(opb,4);
189   
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;
194   
195   if(_oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
196
197   return(0);
198  err_out:
199   vorbis_info_clear(vi);
200   return(-1);
201 }
202
203 static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
204   int i;
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));
213             
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);
220   }       
221   if(_oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
222
223   return(0);
224  err_out:
225   vorbis_comment_clear(vc);
226   return(-1);
227 }
228
229 /* all of the real encoding details are here.  The modes, books,
230    everything */
231 static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
232   int i;
233
234   /* codebooks */
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;
240   }
241
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;
251   }
252
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;
262   }
263
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;
273   }
274
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;
284   }
285   
286   /* mode settings */
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);
295
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;
299   }
300   
301   if(_oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
302
303   return(0);
304  err_out:
305   vorbis_info_clear(vi);
306   return(-1);
307 }
308
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
312    codebook. */
313
314 int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
315   oggpack_buffer opb;
316   
317   if(op){
318     _oggpack_readinit(&opb,op->packet,op->bytes);
319
320     /* Which of the three types of header is this? */
321     /* Also verify header-ness, vorbis */
322     {
323       char buffer[6];
324       int packtype=_oggpack_read(&opb,8);
325       memset(buffer,0,6);
326       _v_readstring(&opb,buffer,6);
327       if(memcmp(buffer,"vorbis",6)){
328         /* not a vorbis header */
329         return(-1);
330       }
331       switch(packtype){
332       case 0x01: /* least significant *bit* is read first */
333         if(!op->b_o_s){
334           /* Not the initial packet */
335           return(-1);
336         }
337         if(vi->rate!=0){
338           /* previously initialized info header */
339           return(-1);
340         }
341
342         return(_vorbis_unpack_info(vi,&opb));
343
344       case 0x03: /* least significant *bit* is read first */
345         if(vi->rate==0){
346           /* um... we didn't get the initial header */
347           return(-1);
348         }
349
350         return(_vorbis_unpack_comment(vc,&opb));
351
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 */
355           return(-1);
356         }
357
358         return(_vorbis_unpack_books(vi,&opb));
359
360       default:
361         /* Not a valid vorbis header type */
362         return(-1);
363         break;
364       }
365     }
366   }
367   return(-1);
368 }
369
370 /* pack side **********************************************************/
371
372 static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){
373   /* preamble */  
374   _oggpack_write(opb,0x01,8);
375   _v_writestring(opb,"vorbis");
376
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);
381
382   _oggpack_write(opb,vi->bitrate_upper,32);
383   _oggpack_write(opb,vi->bitrate_nominal,32);
384   _oggpack_write(opb,vi->bitrate_lower,32);
385
386   _oggpack_write(opb,ilog2(vi->blocksizes[0]),4);
387   _oggpack_write(opb,ilog2(vi->blocksizes[1]),4);
388   _oggpack_write(opb,1,1);
389
390   return(0);
391 }
392
393 static int _vorbis_pack_comment(oggpack_buffer *opb,vorbis_comment *vc){
394   char temp[]="Xiphophorus libVorbis I 20000508";
395
396   /* preamble */  
397   _oggpack_write(opb,0x03,8);
398   _v_writestring(opb,"vorbis");
399
400   /* vendor */
401   _oggpack_write(opb,strlen(temp),32);
402   _v_writestring(opb,temp);
403   
404   /* comments */
405
406   _oggpack_write(opb,vc->comments,32);
407   if(vc->comments){
408     int i;
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]);
413       }else{
414         _oggpack_write(opb,0,32);
415       }
416     }
417   }
418   _oggpack_write(opb,1,1);
419
420   return(0);
421 }
422  
423 static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){
424   int i;
425   _oggpack_write(opb,0x05,8);
426   _v_writestring(opb,"vorbis");
427
428   /* books */
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;
432
433   /* times */
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);
438   }
439
440   /* floors */
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);
445   }
446
447   /* residues */
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);
452   }
453
454   /* maps */
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);
459   }
460
461   /* modes */
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);
468   }
469   _oggpack_write(opb,1,1);
470
471   return(0);
472 err_out:
473   return(-1);
474
475
476 int vorbis_analysis_headerout(vorbis_dsp_state *v,
477                               vorbis_comment *vc,
478                               ogg_packet *op,
479                               ogg_packet *op_comm,
480                               ogg_packet *op_code){
481   vorbis_info *vi=v->vi;
482   oggpack_buffer opb;
483
484   /* first header packet **********************************************/
485
486   _oggpack_writeinit(&opb);
487   if(_vorbis_pack_info(&opb,vi))goto err_out;
488
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);
495   op->b_o_s=1;
496   op->e_o_s=0;
497   op->frameno=0;
498
499   /* second header packet (comments) **********************************/
500
501   _oggpack_reset(&opb);
502   if(_vorbis_pack_comment(&opb,vc))goto err_out;
503
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);
509   op_comm->b_o_s=0;
510   op_comm->e_o_s=0;
511   op_comm->frameno=0;
512
513   /* third header packet (modes/codebooks) ****************************/
514
515   _oggpack_reset(&opb);
516   if(_vorbis_pack_books(&opb,vi))goto err_out;
517
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);
523   op_code->b_o_s=0;
524   op_code->e_o_s=0;
525   op_code->frameno=0;
526
527   _oggpack_writeclear(&opb);
528   return(0);
529  err_out:
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));
534
535   if(v->header)free(v->header);
536   if(v->header1)free(v->header1);
537   if(v->header2)free(v->header2);
538   v->header=NULL;
539   v->header1=NULL;
540   v->header2=NULL;
541   return(-1);
542 }
543