info.c 21 KB

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