Merge branch_beta3 onto the mainline.
[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-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.32 2000/11/06 00:07:00 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 "backends.h"
28 #include "codec_internal.h"
29 #include "codebook.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=_ogg_realloc(vc->user_comments,
64                             (vc->comments+2)*sizeof(char *));
65   vc->comment_lengths=_ogg_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   vi->codec_setup=_ogg_calloc(1,sizeof(codec_setup_info));
145 }
146
147 void vorbis_info_clear(vorbis_info *vi){
148   codec_setup_info     *ci=vi->codec_setup;
149   int i;
150
151   if(ci){
152
153     for(i=0;i<ci->modes;i++)
154       if(ci->mode_param[i])free(ci->mode_param[i]);
155
156     for(i=0;i<ci->maps;i++) /* unpack does the range checking */
157       _mapping_P[ci->map_type[i]]->free_info(ci->map_param[i]);
158
159     for(i=0;i<ci->times;i++) /* unpack does the range checking */
160       _time_P[ci->time_type[i]]->free_info(ci->time_param[i]);
161
162     for(i=0;i<ci->floors;i++) /* unpack does the range checking */
163       _floor_P[ci->floor_type[i]]->free_info(ci->floor_param[i]);
164     
165     for(i=0;i<ci->residues;i++) /* unpack does the range checking */
166       _residue_P[ci->residue_type[i]]->free_info(ci->residue_param[i]);
167
168     for(i=0;i<ci->books;i++){
169       if(ci->book_param[i]){
170         /* knows if the book was not alloced */
171         vorbis_staticbook_destroy(ci->book_param[i]);
172       }
173     }
174     
175     for(i=0;i<ci->psys;i++)
176       _vi_psy_free(ci->psy_param[i]);
177
178     free(ci);
179   }
180
181   memset(vi,0,sizeof(vorbis_info));
182 }
183
184 /* Header packing/unpacking ********************************************/
185
186 static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
187   codec_setup_info     *ci=vi->codec_setup;
188   if(!ci)return(OV_EFAULT);
189
190   vi->version=oggpack_read(opb,32);
191   if(vi->version!=0)return(OV_EVERSION);
192
193   vi->channels=oggpack_read(opb,8);
194   vi->rate=oggpack_read(opb,32);
195
196   vi->bitrate_upper=oggpack_read(opb,32);
197   vi->bitrate_nominal=oggpack_read(opb,32);
198   vi->bitrate_lower=oggpack_read(opb,32);
199
200   ci->blocksizes[0]=1<<oggpack_read(opb,4);
201   ci->blocksizes[1]=1<<oggpack_read(opb,4);
202   
203   if(vi->rate<1)goto err_out;
204   if(vi->channels<1)goto err_out;
205   if(ci->blocksizes[0]<8)goto err_out; 
206   if(ci->blocksizes[1]<ci->blocksizes[0])goto err_out;
207   
208   if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
209
210   return(0);
211  err_out:
212   vorbis_info_clear(vi);
213   return(OV_EBADHEADER);
214 }
215
216 static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
217   int i;
218   int vendorlen=oggpack_read(opb,32);
219   if(vendorlen<0)goto err_out;
220   vc->vendor=_ogg_calloc(vendorlen+1,1);
221   _v_readstring(opb,vc->vendor,vendorlen);
222   vc->comments=oggpack_read(opb,32);
223   if(vc->comments<0)goto err_out;
224   vc->user_comments=_ogg_calloc(vc->comments+1,sizeof(char **));
225   vc->comment_lengths=_ogg_calloc(vc->comments+1, sizeof(int));
226             
227   for(i=0;i<vc->comments;i++){
228     int len=oggpack_read(opb,32);
229     if(len<0)goto err_out;
230         vc->comment_lengths[i]=len;
231     vc->user_comments[i]=_ogg_calloc(len+1,1);
232     _v_readstring(opb,vc->user_comments[i],len);
233   }       
234   if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
235
236   return(0);
237  err_out:
238   vorbis_comment_clear(vc);
239   return(OV_EBADHEADER);
240 }
241
242 /* all of the real encoding details are here.  The modes, books,
243    everything */
244 static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
245   codec_setup_info     *ci=vi->codec_setup;
246   int i;
247   if(!ci)return(OV_EFAULT);
248
249   /* codebooks */
250   ci->books=oggpack_read(opb,8)+1;
251   /*ci->book_param=_ogg_calloc(ci->books,sizeof(static_codebook *));*/
252   for(i=0;i<ci->books;i++){
253     ci->book_param[i]=_ogg_calloc(1,sizeof(static_codebook));
254     if(vorbis_staticbook_unpack(opb,ci->book_param[i]))goto err_out;
255   }
256
257   /* time backend settings */
258   ci->times=oggpack_read(opb,6)+1;
259   /*ci->time_type=_ogg_malloc(ci->times*sizeof(int));*/
260   /*ci->time_param=_ogg_calloc(ci->times,sizeof(void *));*/
261   for(i=0;i<ci->times;i++){
262     ci->time_type[i]=oggpack_read(opb,16);
263     if(ci->time_type[i]<0 || ci->time_type[i]>=VI_TIMEB)goto err_out;
264     ci->time_param[i]=_time_P[ci->time_type[i]]->unpack(vi,opb);
265     if(!ci->time_param[i])goto err_out;
266   }
267
268   /* floor backend settings */
269   ci->floors=oggpack_read(opb,6)+1;
270   /*ci->floor_type=_ogg_malloc(ci->floors*sizeof(int));*/
271   /*ci->floor_param=_ogg_calloc(ci->floors,sizeof(void *));*/
272   for(i=0;i<ci->floors;i++){
273     ci->floor_type[i]=oggpack_read(opb,16);
274     if(ci->floor_type[i]<0 || ci->floor_type[i]>=VI_FLOORB)goto err_out;
275     ci->floor_param[i]=_floor_P[ci->floor_type[i]]->unpack(vi,opb);
276     if(!ci->floor_param[i])goto err_out;
277   }
278
279   /* residue backend settings */
280   ci->residues=oggpack_read(opb,6)+1;
281   /*ci->residue_type=_ogg_malloc(ci->residues*sizeof(int));*/
282   /*ci->residue_param=_ogg_calloc(ci->residues,sizeof(void *));*/
283   for(i=0;i<ci->residues;i++){
284     ci->residue_type[i]=oggpack_read(opb,16);
285     if(ci->residue_type[i]<0 || ci->residue_type[i]>=VI_RESB)goto err_out;
286     ci->residue_param[i]=_residue_P[ci->residue_type[i]]->unpack(vi,opb);
287     if(!ci->residue_param[i])goto err_out;
288   }
289
290   /* map backend settings */
291   ci->maps=oggpack_read(opb,6)+1;
292   /*ci->map_type=_ogg_malloc(ci->maps*sizeof(int));*/
293   /*ci->map_param=_ogg_calloc(ci->maps,sizeof(void *));*/
294   for(i=0;i<ci->maps;i++){
295     ci->map_type[i]=oggpack_read(opb,16);
296     if(ci->map_type[i]<0 || ci->map_type[i]>=VI_MAPB)goto err_out;
297     ci->map_param[i]=_mapping_P[ci->map_type[i]]->unpack(vi,opb);
298     if(!ci->map_param[i])goto err_out;
299   }
300   
301   /* mode settings */
302   ci->modes=oggpack_read(opb,6)+1;
303   /*vi->mode_param=_ogg_calloc(vi->modes,sizeof(void *));*/
304   for(i=0;i<ci->modes;i++){
305     ci->mode_param[i]=_ogg_calloc(1,sizeof(vorbis_info_mode));
306     ci->mode_param[i]->blockflag=oggpack_read(opb,1);
307     ci->mode_param[i]->windowtype=oggpack_read(opb,16);
308     ci->mode_param[i]->transformtype=oggpack_read(opb,16);
309     ci->mode_param[i]->mapping=oggpack_read(opb,8);
310
311     if(ci->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
312     if(ci->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
313     if(ci->mode_param[i]->mapping>=ci->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(OV_EBADHEADER);
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(OV_ENOTVORBIS);
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(OV_EBADHEADER);
351         }
352         if(vi->rate!=0){
353           /* previously initialized info header */
354           return(OV_EBADHEADER);
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(OV_EBADHEADER);
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(OV_EBADHEADER);
371         }
372
373         return(_vorbis_unpack_books(vi,&opb));
374
375       default:
376         /* Not a valid vorbis header type */
377         return(OV_EBADHEADER);
378         break;
379       }
380     }
381   }
382   return(OV_EBADHEADER);
383 }
384
385 /* pack side **********************************************************/
386
387 static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){
388   codec_setup_info     *ci=vi->codec_setup;
389   if(!ci)return(OV_EFAULT);
390
391   /* preamble */  
392   oggpack_write(opb,0x01,8);
393   _v_writestring(opb,"vorbis");
394
395   /* basic information about the stream */
396   oggpack_write(opb,0x00,32);
397   oggpack_write(opb,vi->channels,8);
398   oggpack_write(opb,vi->rate,32);
399
400   oggpack_write(opb,vi->bitrate_upper,32);
401   oggpack_write(opb,vi->bitrate_nominal,32);
402   oggpack_write(opb,vi->bitrate_lower,32);
403
404   oggpack_write(opb,ilog2(ci->blocksizes[0]),4);
405   oggpack_write(opb,ilog2(ci->blocksizes[1]),4);
406   oggpack_write(opb,1,1);
407
408   return(0);
409 }
410
411 static int _vorbis_pack_comment(oggpack_buffer *opb,vorbis_comment *vc){
412   char temp[]="Xiphophorus libVorbis I 20001031";
413
414   /* preamble */  
415   oggpack_write(opb,0x03,8);
416   _v_writestring(opb,"vorbis");
417
418   /* vendor */
419   oggpack_write(opb,strlen(temp),32);
420   _v_writestring(opb,temp);
421   
422   /* comments */
423
424   oggpack_write(opb,vc->comments,32);
425   if(vc->comments){
426     int i;
427     for(i=0;i<vc->comments;i++){
428       if(vc->user_comments[i]){
429         oggpack_write(opb,vc->comment_lengths[i],32);
430         _v_writestring(opb,vc->user_comments[i]);
431       }else{
432         oggpack_write(opb,0,32);
433       }
434     }
435   }
436   oggpack_write(opb,1,1);
437
438   return(0);
439 }
440  
441 static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){
442   codec_setup_info     *ci=vi->codec_setup;
443   int i;
444   if(!ci)return(OV_EFAULT);
445
446   oggpack_write(opb,0x05,8);
447   _v_writestring(opb,"vorbis");
448
449   /* books */
450   oggpack_write(opb,ci->books-1,8);
451   for(i=0;i<ci->books;i++)
452     if(vorbis_staticbook_pack(ci->book_param[i],opb))goto err_out;
453
454   /* times */
455   oggpack_write(opb,ci->times-1,6);
456   for(i=0;i<ci->times;i++){
457     oggpack_write(opb,ci->time_type[i],16);
458     _time_P[ci->time_type[i]]->pack(ci->time_param[i],opb);
459   }
460
461   /* floors */
462   oggpack_write(opb,ci->floors-1,6);
463   for(i=0;i<ci->floors;i++){
464     oggpack_write(opb,ci->floor_type[i],16);
465     _floor_P[ci->floor_type[i]]->pack(ci->floor_param[i],opb);
466   }
467
468   /* residues */
469   oggpack_write(opb,ci->residues-1,6);
470   for(i=0;i<ci->residues;i++){
471     oggpack_write(opb,ci->residue_type[i],16);
472     _residue_P[ci->residue_type[i]]->pack(ci->residue_param[i],opb);
473   }
474
475   /* maps */
476   oggpack_write(opb,ci->maps-1,6);
477   for(i=0;i<ci->maps;i++){
478     oggpack_write(opb,ci->map_type[i],16);
479     _mapping_P[ci->map_type[i]]->pack(vi,ci->map_param[i],opb);
480   }
481
482   /* modes */
483   oggpack_write(opb,ci->modes-1,6);
484   for(i=0;i<ci->modes;i++){
485     oggpack_write(opb,ci->mode_param[i]->blockflag,1);
486     oggpack_write(opb,ci->mode_param[i]->windowtype,16);
487     oggpack_write(opb,ci->mode_param[i]->transformtype,16);
488     oggpack_write(opb,ci->mode_param[i]->mapping,8);
489   }
490   oggpack_write(opb,1,1);
491
492   return(0);
493 err_out:
494   return(-1);
495
496
497 int vorbis_analysis_headerout(vorbis_dsp_state *v,
498                               vorbis_comment *vc,
499                               ogg_packet *op,
500                               ogg_packet *op_comm,
501                               ogg_packet *op_code){
502   int ret=OV_EIMPL;
503   vorbis_info *vi=v->vi;
504   oggpack_buffer opb;
505   backend_lookup_state *b=v->backend_state;
506
507   if(!b){
508     ret=OV_EFAULT;
509     goto err_out;
510   }
511
512   /* first header packet **********************************************/
513
514   oggpack_writeinit(&opb);
515   if(_vorbis_pack_info(&opb,vi))goto err_out;
516
517   /* build the packet */
518   if(b->header)free(b->header);
519   b->header=_ogg_malloc(oggpack_bytes(&opb));
520   memcpy(b->header,opb.buffer,oggpack_bytes(&opb));
521   op->packet=b->header;
522   op->bytes=oggpack_bytes(&opb);
523   op->b_o_s=1;
524   op->e_o_s=0;
525   op->granulepos=0;
526
527   /* second header packet (comments) **********************************/
528
529   oggpack_reset(&opb);
530   if(_vorbis_pack_comment(&opb,vc))goto err_out;
531
532   if(b->header1)free(b->header1);
533   b->header1=_ogg_malloc(oggpack_bytes(&opb));
534   memcpy(b->header1,opb.buffer,oggpack_bytes(&opb));
535   op_comm->packet=b->header1;
536   op_comm->bytes=oggpack_bytes(&opb);
537   op_comm->b_o_s=0;
538   op_comm->e_o_s=0;
539   op_comm->granulepos=0;
540
541   /* third header packet (modes/codebooks) ****************************/
542
543   oggpack_reset(&opb);
544   if(_vorbis_pack_books(&opb,vi))goto err_out;
545
546   if(b->header2)free(b->header2);
547   b->header2=_ogg_malloc(oggpack_bytes(&opb));
548   memcpy(b->header2,opb.buffer,oggpack_bytes(&opb));
549   op_code->packet=b->header2;
550   op_code->bytes=oggpack_bytes(&opb);
551   op_code->b_o_s=0;
552   op_code->e_o_s=0;
553   op_code->granulepos=0;
554
555   oggpack_writeclear(&opb);
556   return(0);
557  err_out:
558   oggpack_writeclear(&opb);
559   memset(op,0,sizeof(ogg_packet));
560   memset(op_comm,0,sizeof(ogg_packet));
561   memset(op_code,0,sizeof(ogg_packet));
562
563   if(b->header)free(b->header);
564   if(b->header1)free(b->header1);
565   if(b->header2)free(b->header2);
566   b->header=NULL;
567   b->header1=NULL;
568   b->header2=NULL;
569   return(ret);
570 }
571