info.c 21 KB

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