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