Fix type in sizeof().
[platform/upstream/libvorbis.git] / lib / info.c
1 /********************************************************************
2  *                                                                  *
3  * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
4  * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
5  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
7  *                                                                  *
8  * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001             *
9  * by the XIPHOPHORUS Company http://www.xiph.org/                  *
10
11  ********************************************************************
12
13  function: maintain the info structure, info <-> header packets
14  last mod: $Id: info.c,v 1.45 2001/09/30 14:02:31 msmith Exp $
15
16  ********************************************************************/
17
18 /* general handling of the header and the vorbis_info structure (and
19    substructures) */
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <ctype.h>
24 #include <ogg/ogg.h>
25 #include "vorbis/codec.h"
26 #include "codec_internal.h"
27 #include "codebook.h"
28 #include "registry.h"
29 #include "window.h"
30 #include "psy.h"
31 #include "misc.h"
32 #include "os.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=_ogg_realloc(vc->user_comments,
62                             (vc->comments+2)*sizeof(char *));
63   vc->comment_lengths=_ogg_realloc(vc->comment_lengths,
64                             (vc->comments+2)*sizeof(int));
65   vc->user_comments[vc->comments]=strdup(comment);
66   vc->comment_lengths[vc->comments]=strlen(comment);
67   vc->comments++;
68   vc->user_comments[vc->comments]=NULL;
69 }
70
71 void vorbis_comment_add_tag(vorbis_comment *vc, char *tag, char *contents){
72   char *comment=alloca(strlen(tag)+strlen(contents)+2); /* +2 for = and \0 */
73   strcpy(comment, tag);
74   strcat(comment, "=");
75   strcat(comment, contents);
76   vorbis_comment_add(vc, comment);
77 }
78
79 /* This is more or less the same as strncasecmp - but that doesn't exist
80  * everywhere, and this is a fairly trivial function, so we include it */
81 static int tagcompare(const char *s1, const char *s2, int n){
82   int c=0;
83   while(c < n){
84     if(toupper(s1[c]) != toupper(s2[c]))
85       return !0;
86     c++;
87   }
88   return 0;
89 }
90
91 char *vorbis_comment_query(vorbis_comment *vc, char *tag, int count){
92   long i;
93   int found = 0;
94   int taglen = strlen(tag)+1; /* +1 for the = we append */
95   char *fulltag = alloca(taglen+ 1);
96
97   strcpy(fulltag, tag);
98   strcat(fulltag, "=");
99   
100   for(i=0;i<vc->comments;i++){
101     if(!tagcompare(vc->user_comments[i], fulltag, taglen)){
102       if(count == found)
103         /* We return a pointer to the data, not a copy */
104         return vc->user_comments[i] + taglen;
105       else
106         found++;
107     }
108   }
109   return NULL; /* didn't find anything */
110 }
111
112 int vorbis_comment_query_count(vorbis_comment *vc, char *tag){
113   int i,count=0;
114   int taglen = strlen(tag)+1; /* +1 for the = we append */
115   char *fulltag = alloca(taglen+1);
116   strcpy(fulltag,tag);
117   strcat(fulltag, "=");
118
119   for(i=0;i<vc->comments;i++){
120     if(!tagcompare(vc->user_comments[i], fulltag, taglen))
121       count++;
122   }
123
124   return count;
125 }
126
127 void vorbis_comment_clear(vorbis_comment *vc){
128   if(vc){
129     long i;
130     for(i=0;i<vc->comments;i++)
131       if(vc->user_comments[i])_ogg_free(vc->user_comments[i]);
132     if(vc->user_comments)_ogg_free(vc->user_comments);
133         if(vc->comment_lengths)_ogg_free(vc->comment_lengths);
134     if(vc->vendor)_ogg_free(vc->vendor);
135   }
136   memset(vc,0,sizeof(vorbis_comment));
137 }
138
139 /* used by synthesis, which has a full, alloced vi */
140 void vorbis_info_init(vorbis_info *vi){
141   memset(vi,0,sizeof(vorbis_info));
142   vi->codec_setup=_ogg_calloc(1,sizeof(codec_setup_info));
143 }
144
145 void vorbis_info_clear(vorbis_info *vi){
146   codec_setup_info     *ci=vi->codec_setup;
147   int i;
148
149   if(ci){
150
151     for(i=0;i<ci->modes;i++)
152       if(ci->mode_param[i])_ogg_free(ci->mode_param[i]);
153
154     for(i=0;i<ci->maps;i++) /* unpack does the range checking */
155       _mapping_P[ci->map_type[i]]->free_info(ci->map_param[i]);
156
157     for(i=0;i<ci->times;i++) /* unpack does the range checking */
158       _time_P[ci->time_type[i]]->free_info(ci->time_param[i]);
159
160     for(i=0;i<ci->floors;i++) /* unpack does the range checking */
161       _floor_P[ci->floor_type[i]]->free_info(ci->floor_param[i]);
162     
163     for(i=0;i<ci->residues;i++) /* unpack does the range checking */
164       _residue_P[ci->residue_type[i]]->free_info(ci->residue_param[i]);
165
166     for(i=0;i<ci->books;i++){
167       if(ci->book_param[i]){
168         /* knows if the book was not alloced */
169         vorbis_staticbook_destroy(ci->book_param[i]);
170       }
171     }
172     
173     for(i=0;i<ci->psys;i++)
174       _vi_psy_free(ci->psy_param[i]);
175
176     _ogg_free(ci);
177   }
178
179   memset(vi,0,sizeof(vorbis_info));
180 }
181
182 /* Header packing/unpacking ********************************************/
183
184 static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
185   codec_setup_info     *ci=vi->codec_setup;
186   if(!ci)return(OV_EFAULT);
187
188   vi->version=oggpack_read(opb,32);
189   if(vi->version!=0)return(OV_EVERSION);
190
191   vi->channels=oggpack_read(opb,8);
192   vi->rate=oggpack_read(opb,32);
193
194   vi->bitrate_upper=oggpack_read(opb,32);
195   vi->bitrate_nominal=oggpack_read(opb,32);
196   vi->bitrate_lower=oggpack_read(opb,32);
197
198   ci->blocksizes[0]=1<<oggpack_read(opb,4);
199   ci->blocksizes[1]=1<<oggpack_read(opb,4);
200   
201   if(vi->rate<1)goto err_out;
202   if(vi->channels<1)goto err_out;
203   if(ci->blocksizes[0]<8)goto err_out; 
204   if(ci->blocksizes[1]<ci->blocksizes[0])goto err_out;
205   
206   if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
207
208   return(0);
209  err_out:
210   vorbis_info_clear(vi);
211   return(OV_EBADHEADER);
212 }
213
214 static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
215   int i;
216   int vendorlen=oggpack_read(opb,32);
217   if(vendorlen<0)goto err_out;
218   vc->vendor=_ogg_calloc(vendorlen+1,1);
219   _v_readstring(opb,vc->vendor,vendorlen);
220   vc->comments=oggpack_read(opb,32);
221   if(vc->comments<0)goto err_out;
222   vc->user_comments=_ogg_calloc(vc->comments+1,sizeof(char *));
223   vc->comment_lengths=_ogg_calloc(vc->comments+1, sizeof(int));
224             
225   for(i=0;i<vc->comments;i++){
226     int len=oggpack_read(opb,32);
227     if(len<0)goto err_out;
228         vc->comment_lengths[i]=len;
229     vc->user_comments[i]=_ogg_calloc(len+1,1);
230     _v_readstring(opb,vc->user_comments[i],len);
231   }       
232   if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
233
234   return(0);
235  err_out:
236   vorbis_comment_clear(vc);
237   return(OV_EBADHEADER);
238 }
239
240 /* all of the real encoding details are here.  The modes, books,
241    everything */
242 static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
243   codec_setup_info     *ci=vi->codec_setup;
244   int i;
245   if(!ci)return(OV_EFAULT);
246
247   /* codebooks */
248   ci->books=oggpack_read(opb,8)+1;
249   /*ci->book_param=_ogg_calloc(ci->books,sizeof(static_codebook *));*/
250   for(i=0;i<ci->books;i++){
251     ci->book_param[i]=_ogg_calloc(1,sizeof(static_codebook));
252     if(vorbis_staticbook_unpack(opb,ci->book_param[i]))goto err_out;
253   }
254
255   /* time backend settings */
256   ci->times=oggpack_read(opb,6)+1;
257   /*ci->time_type=_ogg_malloc(ci->times*sizeof(int));*/
258   /*ci->time_param=_ogg_calloc(ci->times,sizeof(void *));*/
259   for(i=0;i<ci->times;i++){
260     ci->time_type[i]=oggpack_read(opb,16);
261     if(ci->time_type[i]<0 || ci->time_type[i]>=VI_TIMEB)goto err_out;
262     ci->time_param[i]=_time_P[ci->time_type[i]]->unpack(vi,opb);
263     if(!ci->time_param[i])goto err_out;
264   }
265
266   /* floor backend settings */
267   ci->floors=oggpack_read(opb,6)+1;
268   /*ci->floor_type=_ogg_malloc(ci->floors*sizeof(int));*/
269   /*ci->floor_param=_ogg_calloc(ci->floors,sizeof(void *));*/
270   for(i=0;i<ci->floors;i++){
271     ci->floor_type[i]=oggpack_read(opb,16);
272     if(ci->floor_type[i]<0 || ci->floor_type[i]>=VI_FLOORB)goto err_out;
273     ci->floor_param[i]=_floor_P[ci->floor_type[i]]->unpack(vi,opb);
274     if(!ci->floor_param[i])goto err_out;
275   }
276
277   /* residue backend settings */
278   ci->residues=oggpack_read(opb,6)+1;
279   /*ci->residue_type=_ogg_malloc(ci->residues*sizeof(int));*/
280   /*ci->residue_param=_ogg_calloc(ci->residues,sizeof(void *));*/
281   for(i=0;i<ci->residues;i++){
282     ci->residue_type[i]=oggpack_read(opb,16);
283     if(ci->residue_type[i]<0 || ci->residue_type[i]>=VI_RESB)goto err_out;
284     ci->residue_param[i]=_residue_P[ci->residue_type[i]]->unpack(vi,opb);
285     if(!ci->residue_param[i])goto err_out;
286   }
287
288   /* map backend settings */
289   ci->maps=oggpack_read(opb,6)+1;
290   /*ci->map_type=_ogg_malloc(ci->maps*sizeof(int));*/
291   /*ci->map_param=_ogg_calloc(ci->maps,sizeof(void *));*/
292   for(i=0;i<ci->maps;i++){
293     ci->map_type[i]=oggpack_read(opb,16);
294     if(ci->map_type[i]<0 || ci->map_type[i]>=VI_MAPB)goto err_out;
295     ci->map_param[i]=_mapping_P[ci->map_type[i]]->unpack(vi,opb);
296     if(!ci->map_param[i])goto err_out;
297   }
298   
299   /* mode settings */
300   ci->modes=oggpack_read(opb,6)+1;
301   /*vi->mode_param=_ogg_calloc(vi->modes,sizeof(void *));*/
302   for(i=0;i<ci->modes;i++){
303     ci->mode_param[i]=_ogg_calloc(1,sizeof(vorbis_info_mode));
304     ci->mode_param[i]->blockflag=oggpack_read(opb,1);
305     ci->mode_param[i]->windowtype=oggpack_read(opb,16);
306     ci->mode_param[i]->transformtype=oggpack_read(opb,16);
307     ci->mode_param[i]->mapping=oggpack_read(opb,8);
308
309     if(ci->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
310     if(ci->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
311     if(ci->mode_param[i]->mapping>=ci->maps)goto err_out;
312   }
313   
314   if(oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
315
316   return(0);
317  err_out:
318   vorbis_info_clear(vi);
319   return(OV_EBADHEADER);
320 }
321
322 /* The Vorbis header is in three packets; the initial small packet in
323    the first page that identifies basic parameters, a second packet
324    with bitstream comments and a third packet that holds the
325    codebook. */
326
327 int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
328   oggpack_buffer opb;
329   
330   if(op){
331     oggpack_readinit(&opb,op->packet,op->bytes);
332
333     /* Which of the three types of header is this? */
334     /* Also verify header-ness, vorbis */
335     {
336       char buffer[6];
337       int packtype=oggpack_read(&opb,8);
338       memset(buffer,0,6);
339       _v_readstring(&opb,buffer,6);
340       if(memcmp(buffer,"vorbis",6)){
341         /* not a vorbis header */
342         return(OV_ENOTVORBIS);
343       }
344       switch(packtype){
345       case 0x01: /* least significant *bit* is read first */
346         if(!op->b_o_s){
347           /* Not the initial packet */
348           return(OV_EBADHEADER);
349         }
350         if(vi->rate!=0){
351           /* previously initialized info header */
352           return(OV_EBADHEADER);
353         }
354
355         return(_vorbis_unpack_info(vi,&opb));
356
357       case 0x03: /* least significant *bit* is read first */
358         if(vi->rate==0){
359           /* um... we didn't get the initial header */
360           return(OV_EBADHEADER);
361         }
362
363         return(_vorbis_unpack_comment(vc,&opb));
364
365       case 0x05: /* least significant *bit* is read first */
366         if(vi->rate==0 || vc->vendor==NULL){
367           /* um... we didn;t get the initial header or comments yet */
368           return(OV_EBADHEADER);
369         }
370
371         return(_vorbis_unpack_books(vi,&opb));
372
373       default:
374         /* Not a valid vorbis header type */
375         return(OV_EBADHEADER);
376         break;
377       }
378     }
379   }
380   return(OV_EBADHEADER);
381 }
382
383 /* pack side **********************************************************/
384
385 static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){
386   codec_setup_info     *ci=vi->codec_setup;
387   if(!ci)return(OV_EFAULT);
388
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(ci->blocksizes[0]),4);
403   oggpack_write(opb,ilog2(ci->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 20010910";
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   codec_setup_info     *ci=vi->codec_setup;
441   int i;
442   if(!ci)return(OV_EFAULT);
443
444   oggpack_write(opb,0x05,8);
445   _v_writestring(opb,"vorbis");
446
447   /* books */
448   oggpack_write(opb,ci->books-1,8);
449   for(i=0;i<ci->books;i++)
450     if(vorbis_staticbook_pack(ci->book_param[i],opb))goto err_out;
451
452   /* times */
453   oggpack_write(opb,ci->times-1,6);
454   for(i=0;i<ci->times;i++){
455     oggpack_write(opb,ci->time_type[i],16);
456     _time_P[ci->time_type[i]]->pack(ci->time_param[i],opb);
457   }
458
459   /* floors */
460   oggpack_write(opb,ci->floors-1,6);
461   for(i=0;i<ci->floors;i++){
462     oggpack_write(opb,ci->floor_type[i],16);
463     _floor_P[ci->floor_type[i]]->pack(ci->floor_param[i],opb);
464   }
465
466   /* residues */
467   oggpack_write(opb,ci->residues-1,6);
468   for(i=0;i<ci->residues;i++){
469     oggpack_write(opb,ci->residue_type[i],16);
470     _residue_P[ci->residue_type[i]]->pack(ci->residue_param[i],opb);
471   }
472
473   /* maps */
474   oggpack_write(opb,ci->maps-1,6);
475   for(i=0;i<ci->maps;i++){
476     oggpack_write(opb,ci->map_type[i],16);
477     _mapping_P[ci->map_type[i]]->pack(vi,ci->map_param[i],opb);
478   }
479
480   /* modes */
481   oggpack_write(opb,ci->modes-1,6);
482   for(i=0;i<ci->modes;i++){
483     oggpack_write(opb,ci->mode_param[i]->blockflag,1);
484     oggpack_write(opb,ci->mode_param[i]->windowtype,16);
485     oggpack_write(opb,ci->mode_param[i]->transformtype,16);
486     oggpack_write(opb,ci->mode_param[i]->mapping,8);
487   }
488   oggpack_write(opb,1,1);
489
490   return(0);
491 err_out:
492   return(-1);
493
494
495 int vorbis_commentheader_out(vorbis_comment *vc,
496                                       ogg_packet *op){
497
498   oggpack_buffer opb;
499
500   oggpack_writeinit(&opb);
501   if(_vorbis_pack_comment(&opb,vc)) return OV_EIMPL;
502
503   op->packet = _ogg_malloc(oggpack_bytes(&opb));
504   memcpy(op->packet, opb.buffer, oggpack_bytes(&opb));
505
506   op->bytes=oggpack_bytes(&opb);
507   op->b_o_s=0;
508   op->e_o_s=0;
509   op->granulepos=0;
510
511   return 0;
512 }
513
514 int vorbis_analysis_headerout(vorbis_dsp_state *v,
515                               vorbis_comment *vc,
516                               ogg_packet *op,
517                               ogg_packet *op_comm,
518                               ogg_packet *op_code){
519   int ret=OV_EIMPL;
520   vorbis_info *vi=v->vi;
521   oggpack_buffer opb;
522   backend_lookup_state *b=v->backend_state;
523
524   if(!b){
525     ret=OV_EFAULT;
526     goto err_out;
527   }
528
529   /* first header packet **********************************************/
530
531   oggpack_writeinit(&opb);
532   if(_vorbis_pack_info(&opb,vi))goto err_out;
533
534   /* build the packet */
535   if(b->header)_ogg_free(b->header);
536   b->header=_ogg_malloc(oggpack_bytes(&opb));
537   memcpy(b->header,opb.buffer,oggpack_bytes(&opb));
538   op->packet=b->header;
539   op->bytes=oggpack_bytes(&opb);
540   op->b_o_s=1;
541   op->e_o_s=0;
542   op->granulepos=0;
543
544   /* second header packet (comments) **********************************/
545
546   oggpack_reset(&opb);
547   if(_vorbis_pack_comment(&opb,vc))goto err_out;
548
549   if(b->header1)_ogg_free(b->header1);
550   b->header1=_ogg_malloc(oggpack_bytes(&opb));
551   memcpy(b->header1,opb.buffer,oggpack_bytes(&opb));
552   op_comm->packet=b->header1;
553   op_comm->bytes=oggpack_bytes(&opb);
554   op_comm->b_o_s=0;
555   op_comm->e_o_s=0;
556   op_comm->granulepos=0;
557
558   /* third header packet (modes/codebooks) ****************************/
559
560   oggpack_reset(&opb);
561   if(_vorbis_pack_books(&opb,vi))goto err_out;
562
563   if(b->header2)_ogg_free(b->header2);
564   b->header2=_ogg_malloc(oggpack_bytes(&opb));
565   memcpy(b->header2,opb.buffer,oggpack_bytes(&opb));
566   op_code->packet=b->header2;
567   op_code->bytes=oggpack_bytes(&opb);
568   op_code->b_o_s=0;
569   op_code->e_o_s=0;
570   op_code->granulepos=0;
571
572   oggpack_writeclear(&opb);
573   return(0);
574  err_out:
575   oggpack_writeclear(&opb);
576   memset(op,0,sizeof(ogg_packet));
577   memset(op_comm,0,sizeof(ogg_packet));
578   memset(op_code,0,sizeof(ogg_packet));
579
580   if(b->header)_ogg_free(b->header);
581   if(b->header1)_ogg_free(b->header1);
582   if(b->header2)_ogg_free(b->header2);
583   b->header=NULL;
584   b->header1=NULL;
585   b->header2=NULL;
586   return(ret);
587 }
588