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