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