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