info.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
  5. * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *
  6. * PLEASE READ THESE TERMS DISTRIBUTING. *
  7. * *
  8. * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-2000 *
  9. * by Monty <monty@xiph.org> and The XIPHOPHORUS Company *
  10. * http://www.xiph.org/ *
  11. * *
  12. ********************************************************************
  13. function: maintain the info structure, info <-> header packets
  14. last mod: $Id: info.c,v 1.22.4.2 2000/04/21 16:35:39 xiphmont Exp $
  15. ********************************************************************/
  16. /* general handling of the header and the vorbis_info structure (and
  17. substructures) */
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include "vorbis/codec.h"
  21. #include "vorbis/backends.h"
  22. #include "bitwise.h"
  23. #include "sharedbook.h"
  24. #include "bookinternal.h"
  25. #include "registry.h"
  26. #include "window.h"
  27. #include "psy.h"
  28. /* helpers */
  29. static int ilog2(unsigned int v){
  30. int ret=0;
  31. while(v>1){
  32. ret++;
  33. v>>=1;
  34. }
  35. return(ret);
  36. }
  37. static void _v_writestring(oggpack_buffer *o,char *s){
  38. while(*s){
  39. _oggpack_write(o,*s++,8);
  40. }
  41. }
  42. static void _v_readstring(oggpack_buffer *o,char *buf,int bytes){
  43. while(bytes--){
  44. *buf++=_oggpack_read(o,8);
  45. }
  46. }
  47. void vorbis_comment_init(vorbis_comment *vc){
  48. memset(vc,0,sizeof(vorbis_comment));
  49. }
  50. void vorbis_comment_add(vorbis_comment *vc,char *comment){
  51. vc->user_comments=realloc(vc->user_comments,
  52. (vc->comments+2)*sizeof(char *));
  53. vc->user_comments[vc->comments]=strdup(comment);
  54. vc->comments++;
  55. vc->user_comments[vc->comments]=NULL;
  56. }
  57. void vorbis_comment_clear(vorbis_comment *vc){
  58. if(vc){
  59. long i;
  60. for(i=0;i<vc->comments;i++)
  61. if(vc->user_comments[i])free(vc->user_comments[i]);
  62. if(vc->user_comments)free(vc->user_comments);
  63. if(vc->vendor)free(vc->vendor);
  64. }
  65. memset(vc,0,sizeof(vorbis_comment));
  66. }
  67. /* used by synthesis, which has a full, alloced vi */
  68. void vorbis_info_init(vorbis_info *vi){
  69. memset(vi,0,sizeof(vorbis_info));
  70. }
  71. void vorbis_info_clear(vorbis_info *vi){
  72. int i;
  73. for(i=0;i<vi->modes;i++)
  74. if(vi->mode_param[i])free(vi->mode_param[i]);
  75. /*if(vi->mode_param)free(vi->mode_param);*/
  76. for(i=0;i<vi->maps;i++) /* unpack does the range checking */
  77. _mapping_P[vi->map_type[i]]->free_info(vi->map_param[i]);
  78. /*if(vi->map_param)free(vi->map_param);*/
  79. for(i=0;i<vi->times;i++) /* unpack does the range checking */
  80. _time_P[vi->time_type[i]]->free_info(vi->time_param[i]);
  81. /*if(vi->time_param)free(vi->time_param);*/
  82. for(i=0;i<vi->floors;i++) /* unpack does the range checking */
  83. _floor_P[vi->floor_type[i]]->free_info(vi->floor_param[i]);
  84. /*if(vi->floor_param)free(vi->floor_param);*/
  85. for(i=0;i<vi->residues;i++) /* unpack does the range checking */
  86. _residue_P[vi->residue_type[i]]->free_info(vi->residue_param[i]);
  87. /*if(vi->residue_param)free(vi->residue_param);*/
  88. /* the static codebooks *are* freed if you call info_clear, because
  89. decode side does alloc a 'static' codebook. Calling clear on the
  90. full codebook does not clear the static codebook (that's our
  91. responsibility) */
  92. for(i=0;i<vi->books;i++){
  93. /* just in case the decoder pre-cleared to save space */
  94. if(vi->book_param[i]){
  95. vorbis_staticbook_clear(vi->book_param[i]);
  96. free(vi->book_param[i]);
  97. }
  98. }
  99. /*if(vi->book_param)free(vi->book_param);*/
  100. for(i=0;i<vi->psys;i++)
  101. _vi_psy_free(vi->psy_param[i]);
  102. /*if(vi->psy_param)free(vi->psy_param);*/
  103. memset(vi,0,sizeof(vorbis_info));
  104. }
  105. /* Header packing/unpacking ********************************************/
  106. static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
  107. vi->version=_oggpack_read(opb,32);
  108. if(vi->version!=0)return(-1);
  109. vi->channels=_oggpack_read(opb,8);
  110. vi->rate=_oggpack_read(opb,32);
  111. vi->bitrate_upper=_oggpack_read(opb,32);
  112. vi->bitrate_nominal=_oggpack_read(opb,32);
  113. vi->bitrate_lower=_oggpack_read(opb,32);
  114. vi->blocksizes[0]=1<<_oggpack_read(opb,4);
  115. vi->blocksizes[1]=1<<_oggpack_read(opb,4);
  116. if(vi->rate<1)goto err_out;
  117. if(vi->channels<1)goto err_out;
  118. if(vi->blocksizes[0]<8)goto err_out;
  119. if(vi->blocksizes[1]<vi->blocksizes[0])goto err_out;
  120. if(_oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
  121. return(0);
  122. err_out:
  123. vorbis_info_clear(vi);
  124. return(-1);
  125. }
  126. static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
  127. int i;
  128. int vendorlen=_oggpack_read(opb,32);
  129. if(vendorlen<0)goto err_out;
  130. vc->vendor=calloc(vendorlen+1,1);
  131. _v_readstring(opb,vc->vendor,vendorlen);
  132. vc->comments=_oggpack_read(opb,32);
  133. if(vc->comments<0)goto err_out;
  134. vc->user_comments=calloc(vc->comments+1,sizeof(char **));
  135. for(i=0;i<vc->comments;i++){
  136. int len=_oggpack_read(opb,32);
  137. if(len<0)goto err_out;
  138. vc->user_comments[i]=calloc(len+1,1);
  139. _v_readstring(opb,vc->user_comments[i],len);
  140. }
  141. if(_oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
  142. return(0);
  143. err_out:
  144. vorbis_comment_clear(vc);
  145. return(-1);
  146. }
  147. /* all of the real encoding details are here. The modes, books,
  148. everything */
  149. static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
  150. int i;
  151. /* codebooks */
  152. vi->books=_oggpack_read(opb,8)+1;
  153. /*vi->book_param=calloc(vi->books,sizeof(static_codebook *));*/
  154. for(i=0;i<vi->books;i++){
  155. vi->book_param[i]=calloc(1,sizeof(static_codebook));
  156. if(vorbis_staticbook_unpack(opb,vi->book_param[i]))goto err_out;
  157. }
  158. /* time backend settings */
  159. vi->times=_oggpack_read(opb,6)+1;
  160. /*vi->time_type=malloc(vi->times*sizeof(int));*/
  161. /*vi->time_param=calloc(vi->times,sizeof(void *));*/
  162. for(i=0;i<vi->times;i++){
  163. vi->time_type[i]=_oggpack_read(opb,16);
  164. if(vi->time_type[i]<0 || vi->time_type[i]>=VI_TIMEB)goto err_out;
  165. vi->time_param[i]=_time_P[vi->time_type[i]]->unpack(vi,opb);
  166. if(!vi->time_param[i])goto err_out;
  167. }
  168. /* floor backend settings */
  169. vi->floors=_oggpack_read(opb,6)+1;
  170. /*vi->floor_type=malloc(vi->floors*sizeof(int));*/
  171. /*vi->floor_param=calloc(vi->floors,sizeof(void *));*/
  172. for(i=0;i<vi->floors;i++){
  173. vi->floor_type[i]=_oggpack_read(opb,16);
  174. if(vi->floor_type[i]<0 || vi->floor_type[i]>=VI_FLOORB)goto err_out;
  175. vi->floor_param[i]=_floor_P[vi->floor_type[i]]->unpack(vi,opb);
  176. if(!vi->floor_param[i])goto err_out;
  177. }
  178. /* residue backend settings */
  179. vi->residues=_oggpack_read(opb,6)+1;
  180. /*vi->residue_type=malloc(vi->residues*sizeof(int));*/
  181. /*vi->residue_param=calloc(vi->residues,sizeof(void *));*/
  182. for(i=0;i<vi->residues;i++){
  183. vi->residue_type[i]=_oggpack_read(opb,16);
  184. if(vi->residue_type[i]<0 || vi->residue_type[i]>=VI_RESB)goto err_out;
  185. vi->residue_param[i]=_residue_P[vi->residue_type[i]]->unpack(vi,opb);
  186. if(!vi->residue_param[i])goto err_out;
  187. }
  188. /* map backend settings */
  189. vi->maps=_oggpack_read(opb,6)+1;
  190. /*vi->map_type=malloc(vi->maps*sizeof(int));*/
  191. /*vi->map_param=calloc(vi->maps,sizeof(void *));*/
  192. for(i=0;i<vi->maps;i++){
  193. vi->map_type[i]=_oggpack_read(opb,16);
  194. if(vi->map_type[i]<0 || vi->map_type[i]>=VI_MAPB)goto err_out;
  195. vi->map_param[i]=_mapping_P[vi->map_type[i]]->unpack(vi,opb);
  196. if(!vi->map_param[i])goto err_out;
  197. }
  198. /* mode settings */
  199. vi->modes=_oggpack_read(opb,6)+1;
  200. /*vi->mode_param=calloc(vi->modes,sizeof(void *));*/
  201. for(i=0;i<vi->modes;i++){
  202. vi->mode_param[i]=calloc(1,sizeof(vorbis_info_mode));
  203. vi->mode_param[i]->blockflag=_oggpack_read(opb,1);
  204. vi->mode_param[i]->windowtype=_oggpack_read(opb,16);
  205. vi->mode_param[i]->transformtype=_oggpack_read(opb,16);
  206. vi->mode_param[i]->mapping=_oggpack_read(opb,8);
  207. if(vi->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
  208. if(vi->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
  209. if(vi->mode_param[i]->mapping>=vi->maps)goto err_out;
  210. }
  211. if(_oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
  212. return(0);
  213. err_out:
  214. vorbis_info_clear(vi);
  215. return(-1);
  216. }
  217. /* The Vorbis header is in three packets; the initial small packet in
  218. the first page that identifies basic parameters, a second packet
  219. with bitstream comments and a third packet that holds the
  220. codebook. */
  221. int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
  222. oggpack_buffer opb;
  223. if(op){
  224. _oggpack_readinit(&opb,op->packet,op->bytes);
  225. /* Which of the three types of header is this? */
  226. /* Also verify header-ness, vorbis */
  227. {
  228. char buffer[6];
  229. int packtype=_oggpack_read(&opb,8);
  230. memset(buffer,0,6);
  231. _v_readstring(&opb,buffer,6);
  232. if(memcmp(buffer,"vorbis",6)){
  233. /* not a vorbis header */
  234. return(-1);
  235. }
  236. switch(packtype){
  237. case 0x01: /* least significant *bit* is read first */
  238. if(!op->b_o_s){
  239. /* Not the initial packet */
  240. return(-1);
  241. }
  242. if(vi->rate!=0){
  243. /* previously initialized info header */
  244. return(-1);
  245. }
  246. return(_vorbis_unpack_info(vi,&opb));
  247. case 0x03: /* least significant *bit* is read first */
  248. if(vi->rate==0){
  249. /* um... we didn't get the initial header */
  250. return(-1);
  251. }
  252. return(_vorbis_unpack_comment(vc,&opb));
  253. case 0x05: /* least significant *bit* is read first */
  254. if(vi->rate==0 || vc->vendor==NULL){
  255. /* um... we didn;t get the initial header or comments yet */
  256. return(-1);
  257. }
  258. return(_vorbis_unpack_books(vi,&opb));
  259. default:
  260. /* Not a valid vorbis header type */
  261. return(-1);
  262. break;
  263. }
  264. }
  265. }
  266. return(-1);
  267. }
  268. /* pack side **********************************************************/
  269. static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){
  270. /* preamble */
  271. _oggpack_write(opb,0x01,8);
  272. _v_writestring(opb,"vorbis");
  273. /* basic information about the stream */
  274. _oggpack_write(opb,0x00,32);
  275. _oggpack_write(opb,vi->channels,8);
  276. _oggpack_write(opb,vi->rate,32);
  277. _oggpack_write(opb,vi->bitrate_upper,32);
  278. _oggpack_write(opb,vi->bitrate_nominal,32);
  279. _oggpack_write(opb,vi->bitrate_lower,32);
  280. _oggpack_write(opb,ilog2(vi->blocksizes[0]),4);
  281. _oggpack_write(opb,ilog2(vi->blocksizes[1]),4);
  282. _oggpack_write(opb,1,1);
  283. return(0);
  284. }
  285. static int _vorbis_pack_comment(oggpack_buffer *opb,vorbis_comment *vc){
  286. char temp[]="Xiphophorus libVorbis I 20000415";
  287. /* preamble */
  288. _oggpack_write(opb,0x03,8);
  289. _v_writestring(opb,"vorbis");
  290. /* vendor */
  291. _oggpack_write(opb,strlen(temp),32);
  292. _v_writestring(opb,temp);
  293. /* comments */
  294. _oggpack_write(opb,vc->comments,32);
  295. if(vc->comments){
  296. int i;
  297. for(i=0;i<vc->comments;i++){
  298. if(vc->user_comments[i]){
  299. _oggpack_write(opb,strlen(vc->user_comments[i]),32);
  300. _v_writestring(opb,vc->user_comments[i]);
  301. }else{
  302. _oggpack_write(opb,0,32);
  303. }
  304. }
  305. }
  306. _oggpack_write(opb,1,1);
  307. return(0);
  308. }
  309. static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){
  310. int i;
  311. _oggpack_write(opb,0x05,8);
  312. _v_writestring(opb,"vorbis");
  313. /* books */
  314. _oggpack_write(opb,vi->books-1,8);
  315. for(i=0;i<vi->books;i++)
  316. if(vorbis_staticbook_pack(vi->book_param[i],opb))goto err_out;
  317. /* times */
  318. _oggpack_write(opb,vi->times-1,6);
  319. for(i=0;i<vi->times;i++){
  320. _oggpack_write(opb,vi->time_type[i],16);
  321. _time_P[vi->time_type[i]]->pack(vi->time_param[i],opb);
  322. }
  323. /* floors */
  324. _oggpack_write(opb,vi->floors-1,6);
  325. for(i=0;i<vi->floors;i++){
  326. _oggpack_write(opb,vi->floor_type[i],16);
  327. _floor_P[vi->floor_type[i]]->pack(vi->floor_param[i],opb);
  328. }
  329. /* residues */
  330. _oggpack_write(opb,vi->residues-1,6);
  331. for(i=0;i<vi->residues;i++){
  332. _oggpack_write(opb,vi->residue_type[i],16);
  333. _residue_P[vi->residue_type[i]]->pack(vi->residue_param[i],opb);
  334. }
  335. /* maps */
  336. _oggpack_write(opb,vi->maps-1,6);
  337. for(i=0;i<vi->maps;i++){
  338. _oggpack_write(opb,vi->map_type[i],16);
  339. _mapping_P[vi->map_type[i]]->pack(vi,vi->map_param[i],opb);
  340. }
  341. /* modes */
  342. _oggpack_write(opb,vi->modes-1,6);
  343. for(i=0;i<vi->modes;i++){
  344. _oggpack_write(opb,vi->mode_param[i]->blockflag,1);
  345. _oggpack_write(opb,vi->mode_param[i]->windowtype,16);
  346. _oggpack_write(opb,vi->mode_param[i]->transformtype,16);
  347. _oggpack_write(opb,vi->mode_param[i]->mapping,8);
  348. }
  349. _oggpack_write(opb,1,1);
  350. return(0);
  351. err_out:
  352. return(-1);
  353. }
  354. int vorbis_analysis_headerout(vorbis_dsp_state *v,
  355. vorbis_comment *vc,
  356. ogg_packet *op,
  357. ogg_packet *op_comm,
  358. ogg_packet *op_code){
  359. vorbis_info *vi=v->vi;
  360. oggpack_buffer opb;
  361. /* first header packet **********************************************/
  362. _oggpack_writeinit(&opb);
  363. if(_vorbis_pack_info(&opb,vi))goto err_out;
  364. /* build the packet */
  365. if(v->header)free(v->header);
  366. v->header=malloc(_oggpack_bytes(&opb));
  367. memcpy(v->header,opb.buffer,_oggpack_bytes(&opb));
  368. op->packet=v->header;
  369. op->bytes=_oggpack_bytes(&opb);
  370. op->b_o_s=1;
  371. op->e_o_s=0;
  372. op->frameno=0;
  373. /* second header packet (comments) **********************************/
  374. _oggpack_reset(&opb);
  375. if(_vorbis_pack_comment(&opb,vc))goto err_out;
  376. if(v->header1)free(v->header1);
  377. v->header1=malloc(_oggpack_bytes(&opb));
  378. memcpy(v->header1,opb.buffer,_oggpack_bytes(&opb));
  379. op_comm->packet=v->header1;
  380. op_comm->bytes=_oggpack_bytes(&opb);
  381. op_comm->b_o_s=0;
  382. op_comm->e_o_s=0;
  383. op_comm->frameno=0;
  384. /* third header packet (modes/codebooks) ****************************/
  385. _oggpack_reset(&opb);
  386. if(_vorbis_pack_books(&opb,vi))goto err_out;
  387. if(v->header2)free(v->header2);
  388. v->header2=malloc(_oggpack_bytes(&opb));
  389. memcpy(v->header2,opb.buffer,_oggpack_bytes(&opb));
  390. op_code->packet=v->header2;
  391. op_code->bytes=_oggpack_bytes(&opb);
  392. op_code->b_o_s=0;
  393. op_code->e_o_s=0;
  394. op_code->frameno=0;
  395. _oggpack_writeclear(&opb);
  396. return(0);
  397. err_out:
  398. _oggpack_writeclear(&opb);
  399. memset(op,0,sizeof(ogg_packet));
  400. memset(op_comm,0,sizeof(ogg_packet));
  401. memset(op_code,0,sizeof(ogg_packet));
  402. if(v->header)free(v->header);
  403. if(v->header1)free(v->header1);
  404. if(v->header2)free(v->header2);
  405. v->header=NULL;
  406. v->header1=NULL;
  407. v->header2=NULL;
  408. return(-1);
  409. }