info.c 19 KB

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