info.c 19 KB

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