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