info.c 19 KB

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