info.c 17 KB

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