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