info.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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. function: maintain the info structure, info <-> header packets
  13. last mod: $Id$
  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. memset(vc,0,sizeof(*vc));
  120. }
  121. }
  122. /* blocksize 0 is guaranteed to be short, 1 is guaranteed 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. if(ci->map_param[i]) /* this may be cleaning up an aborted
  141. unpack, in which case the below type
  142. cannot be trusted */
  143. _mapping_P[ci->map_type[i]]->free_info(ci->map_param[i]);
  144. for(i=0;i<ci->floors;i++) /* unpack does the range checking */
  145. if(ci->floor_param[i]) /* this may be cleaning up an aborted
  146. unpack, in which case the below type
  147. cannot be trusted */
  148. _floor_P[ci->floor_type[i]]->free_info(ci->floor_param[i]);
  149. for(i=0;i<ci->residues;i++) /* unpack does the range checking */
  150. if(ci->residue_param[i]) /* this may be cleaning up an aborted
  151. unpack, in which case the below type
  152. cannot be trusted */
  153. _residue_P[ci->residue_type[i]]->free_info(ci->residue_param[i]);
  154. for(i=0;i<ci->books;i++){
  155. if(ci->book_param[i]){
  156. /* knows if the book was not alloced */
  157. vorbis_staticbook_destroy(ci->book_param[i]);
  158. }
  159. if(ci->fullbooks)
  160. vorbis_book_clear(ci->fullbooks+i);
  161. }
  162. if(ci->fullbooks)
  163. _ogg_free(ci->fullbooks);
  164. for(i=0;i<ci->psys;i++)
  165. _vi_psy_free(ci->psy_param[i]);
  166. _ogg_free(ci);
  167. }
  168. memset(vi,0,sizeof(*vi));
  169. }
  170. /* Header packing/unpacking ********************************************/
  171. static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
  172. codec_setup_info *ci=vi->codec_setup;
  173. if(!ci)return(OV_EFAULT);
  174. vi->version=oggpack_read(opb,32);
  175. if(vi->version!=0)return(OV_EVERSION);
  176. vi->channels=oggpack_read(opb,8);
  177. vi->rate=oggpack_read(opb,32);
  178. vi->bitrate_upper=oggpack_read(opb,32);
  179. vi->bitrate_nominal=oggpack_read(opb,32);
  180. vi->bitrate_lower=oggpack_read(opb,32);
  181. ci->blocksizes[0]=1<<oggpack_read(opb,4);
  182. ci->blocksizes[1]=1<<oggpack_read(opb,4);
  183. if(vi->rate<1)goto err_out;
  184. if(vi->channels<1)goto err_out;
  185. if(ci->blocksizes[0]<64)goto err_out;
  186. if(ci->blocksizes[1]<ci->blocksizes[0])goto err_out;
  187. if(ci->blocksizes[1]>8192)goto err_out;
  188. if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
  189. return(0);
  190. err_out:
  191. vorbis_info_clear(vi);
  192. return(OV_EBADHEADER);
  193. }
  194. static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
  195. int i;
  196. int vendorlen=oggpack_read(opb,32);
  197. if(vendorlen<0)goto err_out;
  198. vc->vendor=_ogg_calloc(vendorlen+1,1);
  199. _v_readstring(opb,vc->vendor,vendorlen);
  200. vc->comments=oggpack_read(opb,32);
  201. if(vc->comments<0)goto err_out;
  202. vc->user_comments=_ogg_calloc(vc->comments+1,sizeof(*vc->user_comments));
  203. vc->comment_lengths=_ogg_calloc(vc->comments+1, sizeof(*vc->comment_lengths));
  204. for(i=0;i<vc->comments;i++){
  205. int len=oggpack_read(opb,32);
  206. if(len<0)goto err_out;
  207. vc->comment_lengths[i]=len;
  208. vc->user_comments[i]=_ogg_calloc(len+1,1);
  209. _v_readstring(opb,vc->user_comments[i],len);
  210. }
  211. if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
  212. return(0);
  213. err_out:
  214. vorbis_comment_clear(vc);
  215. return(OV_EBADHEADER);
  216. }
  217. /* all of the real encoding details are here. The modes, books,
  218. everything */
  219. static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
  220. codec_setup_info *ci=vi->codec_setup;
  221. int i;
  222. if(!ci)return(OV_EFAULT);
  223. /* codebooks */
  224. ci->books=oggpack_read(opb,8)+1;
  225. /*ci->book_param=_ogg_calloc(ci->books,sizeof(*ci->book_param));*/
  226. for(i=0;i<ci->books;i++){
  227. ci->book_param[i]=_ogg_calloc(1,sizeof(*ci->book_param[i]));
  228. if(vorbis_staticbook_unpack(opb,ci->book_param[i]))goto err_out;
  229. }
  230. /* time backend settings; hooks are unused */
  231. {
  232. int times=oggpack_read(opb,6)+1;
  233. for(i=0;i<times;i++){
  234. int test=oggpack_read(opb,16);
  235. if(test<0 || test>=VI_TIMEB)goto err_out;
  236. }
  237. }
  238. /* floor backend settings */
  239. ci->floors=oggpack_read(opb,6)+1;
  240. /*ci->floor_type=_ogg_malloc(ci->floors*sizeof(*ci->floor_type));*/
  241. /*ci->floor_param=_ogg_calloc(ci->floors,sizeof(void *));*/
  242. for(i=0;i<ci->floors;i++){
  243. ci->floor_type[i]=oggpack_read(opb,16);
  244. if(ci->floor_type[i]<0 || ci->floor_type[i]>=VI_FLOORB)goto err_out;
  245. ci->floor_param[i]=_floor_P[ci->floor_type[i]]->unpack(vi,opb);
  246. if(!ci->floor_param[i])goto err_out;
  247. }
  248. /* residue backend settings */
  249. ci->residues=oggpack_read(opb,6)+1;
  250. /*ci->residue_type=_ogg_malloc(ci->residues*sizeof(*ci->residue_type));*/
  251. /*ci->residue_param=_ogg_calloc(ci->residues,sizeof(void *));*/
  252. for(i=0;i<ci->residues;i++){
  253. ci->residue_type[i]=oggpack_read(opb,16);
  254. if(ci->residue_type[i]<0 || ci->residue_type[i]>=VI_RESB)goto err_out;
  255. ci->residue_param[i]=_residue_P[ci->residue_type[i]]->unpack(vi,opb);
  256. if(!ci->residue_param[i])goto err_out;
  257. }
  258. /* map backend settings */
  259. ci->maps=oggpack_read(opb,6)+1;
  260. /*ci->map_type=_ogg_malloc(ci->maps*sizeof(*ci->map_type));*/
  261. /*ci->map_param=_ogg_calloc(ci->maps,sizeof(void *));*/
  262. for(i=0;i<ci->maps;i++){
  263. ci->map_type[i]=oggpack_read(opb,16);
  264. if(ci->map_type[i]<0 || ci->map_type[i]>=VI_MAPB)goto err_out;
  265. ci->map_param[i]=_mapping_P[ci->map_type[i]]->unpack(vi,opb);
  266. if(!ci->map_param[i])goto err_out;
  267. }
  268. /* mode settings */
  269. ci->modes=oggpack_read(opb,6)+1;
  270. /*vi->mode_param=_ogg_calloc(vi->modes,sizeof(void *));*/
  271. for(i=0;i<ci->modes;i++){
  272. ci->mode_param[i]=_ogg_calloc(1,sizeof(*ci->mode_param[i]));
  273. ci->mode_param[i]->blockflag=oggpack_read(opb,1);
  274. ci->mode_param[i]->windowtype=oggpack_read(opb,16);
  275. ci->mode_param[i]->transformtype=oggpack_read(opb,16);
  276. ci->mode_param[i]->mapping=oggpack_read(opb,8);
  277. if(ci->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
  278. if(ci->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
  279. if(ci->mode_param[i]->mapping>=ci->maps)goto err_out;
  280. }
  281. if(oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
  282. return(0);
  283. err_out:
  284. vorbis_info_clear(vi);
  285. return(OV_EBADHEADER);
  286. }
  287. /* Is this packet a vorbis ID header? */
  288. int vorbis_synthesis_idheader(ogg_packet *op){
  289. oggpack_buffer opb;
  290. char buffer[6];
  291. if(op){
  292. oggpack_readinit(&opb,op->packet,op->bytes);
  293. if(!op->b_o_s)
  294. return(0); /* Not the initial packet */
  295. if(oggpack_read(&opb,8) != 1)
  296. return 0; /* not an ID header */
  297. memset(buffer,0,6);
  298. _v_readstring(&opb,buffer,6);
  299. if(memcmp(buffer,"vorbis",6))
  300. return 0; /* not vorbis */
  301. return 1;
  302. }
  303. return 0;
  304. }
  305. /* The Vorbis header is in three packets; the initial small packet in
  306. the first page that identifies basic parameters, a second packet
  307. with bitstream comments and a third packet that holds the
  308. codebook. */
  309. int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
  310. oggpack_buffer opb;
  311. if(op){
  312. oggpack_readinit(&opb,op->packet,op->bytes);
  313. /* Which of the three types of header is this? */
  314. /* Also verify header-ness, vorbis */
  315. {
  316. char buffer[6];
  317. int packtype=oggpack_read(&opb,8);
  318. memset(buffer,0,6);
  319. _v_readstring(&opb,buffer,6);
  320. if(memcmp(buffer,"vorbis",6)){
  321. /* not a vorbis header */
  322. return(OV_ENOTVORBIS);
  323. }
  324. switch(packtype){
  325. case 0x01: /* least significant *bit* is read first */
  326. if(!op->b_o_s){
  327. /* Not the initial packet */
  328. return(OV_EBADHEADER);
  329. }
  330. if(vi->rate!=0){
  331. /* previously initialized info header */
  332. return(OV_EBADHEADER);
  333. }
  334. return(_vorbis_unpack_info(vi,&opb));
  335. case 0x03: /* least significant *bit* is read first */
  336. if(vi->rate==0){
  337. /* um... we didn't get the initial header */
  338. return(OV_EBADHEADER);
  339. }
  340. return(_vorbis_unpack_comment(vc,&opb));
  341. case 0x05: /* least significant *bit* is read first */
  342. if(vi->rate==0 || vc->vendor==NULL){
  343. /* um... we didn;t get the initial header or comments yet */
  344. return(OV_EBADHEADER);
  345. }
  346. return(_vorbis_unpack_books(vi,&opb));
  347. default:
  348. /* Not a valid vorbis header type */
  349. return(OV_EBADHEADER);
  350. break;
  351. }
  352. }
  353. }
  354. return(OV_EBADHEADER);
  355. }
  356. /* pack side **********************************************************/
  357. static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){
  358. codec_setup_info *ci=vi->codec_setup;
  359. if(!ci)return(OV_EFAULT);
  360. /* preamble */
  361. oggpack_write(opb,0x01,8);
  362. _v_writestring(opb,"vorbis", 6);
  363. /* basic information about the stream */
  364. oggpack_write(opb,0x00,32);
  365. oggpack_write(opb,vi->channels,8);
  366. oggpack_write(opb,vi->rate,32);
  367. oggpack_write(opb,vi->bitrate_upper,32);
  368. oggpack_write(opb,vi->bitrate_nominal,32);
  369. oggpack_write(opb,vi->bitrate_lower,32);
  370. oggpack_write(opb,ilog2(ci->blocksizes[0]),4);
  371. oggpack_write(opb,ilog2(ci->blocksizes[1]),4);
  372. oggpack_write(opb,1,1);
  373. return(0);
  374. }
  375. static int _vorbis_pack_comment(oggpack_buffer *opb,vorbis_comment *vc){
  376. char temp[]="AO; aoTuV b5a [20080330] (based on Xiph.Org's libVorbis)";
  377. int bytes = strlen(temp);
  378. /* preamble */
  379. oggpack_write(opb,0x03,8);
  380. _v_writestring(opb,"vorbis", 6);
  381. /* vendor */
  382. oggpack_write(opb,bytes,32);
  383. _v_writestring(opb,temp, bytes);
  384. /* comments */
  385. oggpack_write(opb,vc->comments,32);
  386. if(vc->comments){
  387. int i;
  388. for(i=0;i<vc->comments;i++){
  389. if(vc->user_comments[i]){
  390. oggpack_write(opb,vc->comment_lengths[i],32);
  391. _v_writestring(opb,vc->user_comments[i], vc->comment_lengths[i]);
  392. }else{
  393. oggpack_write(opb,0,32);
  394. }
  395. }
  396. }
  397. oggpack_write(opb,1,1);
  398. return(0);
  399. }
  400. static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){
  401. codec_setup_info *ci=vi->codec_setup;
  402. int i;
  403. if(!ci)return(OV_EFAULT);
  404. oggpack_write(opb,0x05,8);
  405. _v_writestring(opb,"vorbis", 6);
  406. /* books */
  407. oggpack_write(opb,ci->books-1,8);
  408. for(i=0;i<ci->books;i++)
  409. if(vorbis_staticbook_pack(ci->book_param[i],opb))goto err_out;
  410. /* times; hook placeholders */
  411. oggpack_write(opb,0,6);
  412. oggpack_write(opb,0,16);
  413. /* floors */
  414. oggpack_write(opb,ci->floors-1,6);
  415. for(i=0;i<ci->floors;i++){
  416. oggpack_write(opb,ci->floor_type[i],16);
  417. if(_floor_P[ci->floor_type[i]]->pack)
  418. _floor_P[ci->floor_type[i]]->pack(ci->floor_param[i],opb);
  419. else
  420. goto err_out;
  421. }
  422. /* residues */
  423. oggpack_write(opb,ci->residues-1,6);
  424. for(i=0;i<ci->residues;i++){
  425. oggpack_write(opb,ci->residue_type[i],16);
  426. _residue_P[ci->residue_type[i]]->pack(ci->residue_param[i],opb);
  427. }
  428. /* maps */
  429. oggpack_write(opb,ci->maps-1,6);
  430. for(i=0;i<ci->maps;i++){
  431. oggpack_write(opb,ci->map_type[i],16);
  432. _mapping_P[ci->map_type[i]]->pack(vi,ci->map_param[i],opb);
  433. }
  434. /* modes */
  435. oggpack_write(opb,ci->modes-1,6);
  436. for(i=0;i<ci->modes;i++){
  437. oggpack_write(opb,ci->mode_param[i]->blockflag,1);
  438. oggpack_write(opb,ci->mode_param[i]->windowtype,16);
  439. oggpack_write(opb,ci->mode_param[i]->transformtype,16);
  440. oggpack_write(opb,ci->mode_param[i]->mapping,8);
  441. }
  442. oggpack_write(opb,1,1);
  443. return(0);
  444. err_out:
  445. return(-1);
  446. }
  447. int vorbis_commentheader_out(vorbis_comment *vc,
  448. ogg_packet *op){
  449. oggpack_buffer opb;
  450. oggpack_writeinit(&opb);
  451. if(_vorbis_pack_comment(&opb,vc)) return OV_EIMPL;
  452. op->packet = _ogg_malloc(oggpack_bytes(&opb));
  453. memcpy(op->packet, opb.buffer, oggpack_bytes(&opb));
  454. op->bytes=oggpack_bytes(&opb);
  455. op->b_o_s=0;
  456. op->e_o_s=0;
  457. op->granulepos=0;
  458. op->packetno=1;
  459. return 0;
  460. }
  461. int vorbis_analysis_headerout(vorbis_dsp_state *v,
  462. vorbis_comment *vc,
  463. ogg_packet *op,
  464. ogg_packet *op_comm,
  465. ogg_packet *op_code){
  466. int ret=OV_EIMPL;
  467. vorbis_info *vi=v->vi;
  468. oggpack_buffer opb;
  469. private_state *b=v->backend_state;
  470. if(!b){
  471. ret=OV_EFAULT;
  472. goto err_out;
  473. }
  474. /* first header packet **********************************************/
  475. oggpack_writeinit(&opb);
  476. if(_vorbis_pack_info(&opb,vi))goto err_out;
  477. /* build the packet */
  478. if(b->header)_ogg_free(b->header);
  479. b->header=_ogg_malloc(oggpack_bytes(&opb));
  480. memcpy(b->header,opb.buffer,oggpack_bytes(&opb));
  481. op->packet=b->header;
  482. op->bytes=oggpack_bytes(&opb);
  483. op->b_o_s=1;
  484. op->e_o_s=0;
  485. op->granulepos=0;
  486. op->packetno=0;
  487. /* second header packet (comments) **********************************/
  488. oggpack_reset(&opb);
  489. if(_vorbis_pack_comment(&opb,vc))goto err_out;
  490. if(b->header1)_ogg_free(b->header1);
  491. b->header1=_ogg_malloc(oggpack_bytes(&opb));
  492. memcpy(b->header1,opb.buffer,oggpack_bytes(&opb));
  493. op_comm->packet=b->header1;
  494. op_comm->bytes=oggpack_bytes(&opb);
  495. op_comm->b_o_s=0;
  496. op_comm->e_o_s=0;
  497. op_comm->granulepos=0;
  498. op_comm->packetno=1;
  499. /* third header packet (modes/codebooks) ****************************/
  500. oggpack_reset(&opb);
  501. if(_vorbis_pack_books(&opb,vi))goto err_out;
  502. if(b->header2)_ogg_free(b->header2);
  503. b->header2=_ogg_malloc(oggpack_bytes(&opb));
  504. memcpy(b->header2,opb.buffer,oggpack_bytes(&opb));
  505. op_code->packet=b->header2;
  506. op_code->bytes=oggpack_bytes(&opb);
  507. op_code->b_o_s=0;
  508. op_code->e_o_s=0;
  509. op_code->granulepos=0;
  510. op_code->packetno=2;
  511. oggpack_writeclear(&opb);
  512. return(0);
  513. err_out:
  514. oggpack_writeclear(&opb);
  515. memset(op,0,sizeof(*op));
  516. memset(op_comm,0,sizeof(*op_comm));
  517. memset(op_code,0,sizeof(*op_code));
  518. if(b){
  519. if(b->header)_ogg_free(b->header);
  520. if(b->header1)_ogg_free(b->header1);
  521. if(b->header2)_ogg_free(b->header2);
  522. b->header=NULL;
  523. b->header1=NULL;
  524. b->header2=NULL;
  525. }
  526. return(ret);
  527. }
  528. double vorbis_granule_time(vorbis_dsp_state *v,ogg_int64_t granulepos){
  529. if(granulepos>=0)
  530. return((double)granulepos/v->vi->rate);
  531. return(-1);
  532. }