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