info.c 19 KB

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