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