info.c 19 KB

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