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