info.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE. *
  4. * *
  5. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  6. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  7. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  8. * *
  9. * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2003 *
  10. * BY THE Xiph.Org FOUNDATION http://www.xiph.org/ *
  11. * *
  12. ********************************************************************
  13. function: maintain the info structure, info <-> header packets
  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 <limits.h>
  21. #include "ogg/ogg.h"
  22. #include "ivorbiscodec.h"
  23. #include "codec_internal.h"
  24. #include "codebook.h"
  25. #include "registry.h"
  26. #include "window.h"
  27. #include "misc.h"
  28. /* helpers */
  29. static void _v_readstring(oggpack_buffer *o,char *buf,int bytes){
  30. while(bytes--){
  31. *buf++=oggpack_read(o,8);
  32. }
  33. }
  34. void vorbis_comment_init(vorbis_comment *vc){
  35. memset(vc,0,sizeof(*vc));
  36. }
  37. /* This is more or less the same as strncasecmp - but that doesn't exist
  38. * everywhere, and this is a fairly trivial function, so we include it */
  39. static int tagcompare(const char *s1, const char *s2, int n){
  40. int c=0;
  41. while(c < n){
  42. if(toupper(s1[c]) != toupper(s2[c]))
  43. return !0;
  44. c++;
  45. }
  46. return 0;
  47. }
  48. char *vorbis_comment_query(vorbis_comment *vc, char *tag, int count){
  49. long i;
  50. int found = 0;
  51. int taglen = strlen(tag)+1; /* +1 for the = we append */
  52. char *fulltag = (char *)alloca(taglen+ 1);
  53. strcpy(fulltag, tag);
  54. strcat(fulltag, "=");
  55. for(i=0;i<vc->comments;i++){
  56. if(!tagcompare(vc->user_comments[i], fulltag, taglen)){
  57. if(count == found)
  58. /* We return a pointer to the data, not a copy */
  59. return vc->user_comments[i] + taglen;
  60. else
  61. found++;
  62. }
  63. }
  64. return NULL; /* didn't find anything */
  65. }
  66. int vorbis_comment_query_count(vorbis_comment *vc, char *tag){
  67. int i,count=0;
  68. int taglen = strlen(tag)+1; /* +1 for the = we append */
  69. char *fulltag = (char *)alloca(taglen+1);
  70. strcpy(fulltag,tag);
  71. strcat(fulltag, "=");
  72. for(i=0;i<vc->comments;i++){
  73. if(!tagcompare(vc->user_comments[i], fulltag, taglen))
  74. count++;
  75. }
  76. return count;
  77. }
  78. void vorbis_comment_clear(vorbis_comment *vc){
  79. if(vc){
  80. long i;
  81. if(vc->user_comments){
  82. for(i=0;i<vc->comments;i++)
  83. if(vc->user_comments[i])_ogg_free(vc->user_comments[i]);
  84. _ogg_free(vc->user_comments);
  85. }
  86. if(vc->comment_lengths)_ogg_free(vc->comment_lengths);
  87. if(vc->vendor)_ogg_free(vc->vendor);
  88. memset(vc,0,sizeof(*vc));
  89. }
  90. }
  91. /* blocksize 0 is guaranteed to be short, 1 is guarantted to be long.
  92. They may be equal, but short will never ge greater than long */
  93. int vorbis_info_blocksize(vorbis_info *vi,int zo){
  94. codec_setup_info *ci = (codec_setup_info *)vi->codec_setup;
  95. return ci ? ci->blocksizes[zo] : -1;
  96. }
  97. /* used by synthesis, which has a full, alloced vi */
  98. void vorbis_info_init(vorbis_info *vi){
  99. memset(vi,0,sizeof(*vi));
  100. vi->codec_setup=(codec_setup_info *)_ogg_calloc(1,sizeof(codec_setup_info));
  101. }
  102. void vorbis_info_clear(vorbis_info *vi){
  103. codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
  104. int i;
  105. if(ci){
  106. for(i=0;i<ci->modes;i++)
  107. if(ci->mode_param[i])_ogg_free(ci->mode_param[i]);
  108. for(i=0;i<ci->maps;i++) /* unpack does the range checking */
  109. if(ci->map_param[i])
  110. _mapping_P[ci->map_type[i]]->free_info(ci->map_param[i]);
  111. for(i=0;i<ci->floors;i++) /* unpack does the range checking */
  112. if(ci->floor_param[i])
  113. _floor_P[ci->floor_type[i]]->free_info(ci->floor_param[i]);
  114. for(i=0;i<ci->residues;i++) /* unpack does the range checking */
  115. if(ci->residue_param[i])
  116. _residue_P[ci->residue_type[i]]->free_info(ci->residue_param[i]);
  117. for(i=0;i<ci->books;i++){
  118. if(ci->book_param[i]){
  119. /* knows if the book was not alloced */
  120. vorbis_staticbook_destroy(ci->book_param[i]);
  121. }
  122. if(ci->fullbooks)
  123. vorbis_book_clear(ci->fullbooks+i);
  124. }
  125. if(ci->fullbooks)
  126. _ogg_free(ci->fullbooks);
  127. _ogg_free(ci);
  128. }
  129. memset(vi,0,sizeof(*vi));
  130. }
  131. /* Header packing/unpacking ********************************************/
  132. static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
  133. codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
  134. if(!ci)return(OV_EFAULT);
  135. vi->version=oggpack_read(opb,32);
  136. if(vi->version!=0)return(OV_EVERSION);
  137. vi->channels=oggpack_read(opb,8);
  138. vi->rate=oggpack_read(opb,32);
  139. vi->bitrate_upper=oggpack_read(opb,32);
  140. vi->bitrate_nominal=oggpack_read(opb,32);
  141. vi->bitrate_lower=oggpack_read(opb,32);
  142. ci->blocksizes[0]=1<<oggpack_read(opb,4);
  143. ci->blocksizes[1]=1<<oggpack_read(opb,4);
  144. if(vi->rate<1)goto err_out;
  145. if(vi->channels<1)goto err_out;
  146. if(ci->blocksizes[0]<64)goto err_out;
  147. if(ci->blocksizes[1]<ci->blocksizes[0])goto err_out;
  148. if(ci->blocksizes[1]>8192)goto err_out;
  149. if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
  150. return(0);
  151. err_out:
  152. vorbis_info_clear(vi);
  153. return(OV_EBADHEADER);
  154. }
  155. static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
  156. int i;
  157. int vendorlen;
  158. vendorlen=oggpack_read(opb,32);
  159. if(vendorlen<0)goto err_out;
  160. if(vendorlen>opb->storage-oggpack_bytes(opb))goto err_out;
  161. vc->vendor=(char *)_ogg_calloc(vendorlen+1,1);
  162. if(vc->vendor==NULL)goto err_out;
  163. _v_readstring(opb,vc->vendor,vendorlen);
  164. i=oggpack_read(opb,32);
  165. if(i<0||i>=INT_MAX||i>(opb->storage-oggpack_bytes(opb))>>2)goto err_out;
  166. vc->user_comments=(char **)_ogg_calloc(i+1,sizeof(*vc->user_comments));
  167. vc->comment_lengths=(int *)_ogg_calloc(i+1, sizeof(*vc->comment_lengths));
  168. if(vc->user_comments==NULL||vc->comment_lengths==NULL)goto err_out;
  169. vc->comments=i;
  170. for(i=0;i<vc->comments;i++){
  171. int len=oggpack_read(opb,32);
  172. if(len<0||len>opb->storage-oggpack_bytes(opb))goto err_out;
  173. vc->comment_lengths[i]=len;
  174. vc->user_comments[i]=(char *)_ogg_calloc(len+1,1);
  175. if(vc->user_comments[i]==NULL){
  176. vc->comments=i;
  177. goto err_out;
  178. }
  179. _v_readstring(opb,vc->user_comments[i],len);
  180. }
  181. if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
  182. return(0);
  183. err_out:
  184. vorbis_comment_clear(vc);
  185. return(OV_EBADHEADER);
  186. }
  187. /* all of the real encoding details are here. The modes, books,
  188. everything */
  189. static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
  190. codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
  191. int i;
  192. if(!ci)return(OV_EFAULT);
  193. /* codebooks */
  194. ci->books=oggpack_read(opb,8)+1;
  195. if(ci->books<=0)goto err_out;
  196. for(i=0;i<ci->books;i++){
  197. ci->book_param[i]=vorbis_staticbook_unpack(opb);
  198. if(!ci->book_param[i])goto err_out;
  199. }
  200. /* time backend settings */
  201. ci->times=oggpack_read(opb,6)+1;
  202. if(ci->times<=0)goto err_out;
  203. for(i=0;i<ci->times;i++){
  204. ci->time_type[i]=oggpack_read(opb,16);
  205. if(ci->time_type[i]<0 || ci->time_type[i]>=VI_TIMEB)goto err_out;
  206. /* ci->time_param[i]=_time_P[ci->time_type[i]]->unpack(vi,opb);
  207. Vorbis I has no time backend */
  208. /*if(!ci->time_param[i])goto err_out;*/
  209. }
  210. /* floor backend settings */
  211. ci->floors=oggpack_read(opb,6)+1;
  212. if(ci->floors<=0)goto err_out;
  213. for(i=0;i<ci->floors;i++){
  214. ci->floor_type[i]=oggpack_read(opb,16);
  215. if(ci->floor_type[i]<0 || ci->floor_type[i]>=VI_FLOORB)goto err_out;
  216. ci->floor_param[i]=_floor_P[ci->floor_type[i]]->unpack(vi,opb);
  217. if(!ci->floor_param[i])goto err_out;
  218. }
  219. /* residue backend settings */
  220. ci->residues=oggpack_read(opb,6)+1;
  221. if(ci->residues<=0)goto err_out;
  222. for(i=0;i<ci->residues;i++){
  223. ci->residue_type[i]=oggpack_read(opb,16);
  224. if(ci->residue_type[i]<0 || ci->residue_type[i]>=VI_RESB)goto err_out;
  225. ci->residue_param[i]=_residue_P[ci->residue_type[i]]->unpack(vi,opb);
  226. if(!ci->residue_param[i])goto err_out;
  227. }
  228. /* map backend settings */
  229. ci->maps=oggpack_read(opb,6)+1;
  230. if(ci->maps<=0)goto err_out;
  231. for(i=0;i<ci->maps;i++){
  232. ci->map_type[i]=oggpack_read(opb,16);
  233. if(ci->map_type[i]<0 || ci->map_type[i]>=VI_MAPB)goto err_out;
  234. ci->map_param[i]=_mapping_P[ci->map_type[i]]->unpack(vi,opb);
  235. if(!ci->map_param[i])goto err_out;
  236. }
  237. /* mode settings */
  238. ci->modes=oggpack_read(opb,6)+1;
  239. if(ci->modes<=0)goto err_out;
  240. for(i=0;i<ci->modes;i++){
  241. ci->mode_param[i]=(vorbis_info_mode *)_ogg_calloc(1,sizeof(*ci->mode_param[i]));
  242. ci->mode_param[i]->blockflag=oggpack_read(opb,1);
  243. ci->mode_param[i]->windowtype=oggpack_read(opb,16);
  244. ci->mode_param[i]->transformtype=oggpack_read(opb,16);
  245. ci->mode_param[i]->mapping=oggpack_read(opb,8);
  246. if(ci->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
  247. if(ci->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
  248. if(ci->mode_param[i]->mapping>=ci->maps)goto err_out;
  249. if(ci->mode_param[i]->mapping<0)goto err_out;
  250. }
  251. if(oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
  252. return(0);
  253. err_out:
  254. vorbis_info_clear(vi);
  255. return(OV_EBADHEADER);
  256. }
  257. /* Is this packet a vorbis ID header? */
  258. int vorbis_synthesis_idheader(ogg_packet *op){
  259. oggpack_buffer opb;
  260. char buffer[6];
  261. if(op){
  262. oggpack_readinit(&opb,op->packet,op->bytes);
  263. if(!op->b_o_s)
  264. return(0); /* Not the initial packet */
  265. if(oggpack_read(&opb,8) != 1)
  266. return 0; /* not an ID header */
  267. memset(buffer,0,6);
  268. _v_readstring(&opb,buffer,6);
  269. if(memcmp(buffer,"vorbis",6))
  270. return 0; /* not vorbis */
  271. return 1;
  272. }
  273. return 0;
  274. }
  275. /* The Vorbis header is in three packets; the initial small packet in
  276. the first page that identifies basic parameters, a second packet
  277. with bitstream comments and a third packet that holds the
  278. codebook. */
  279. int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
  280. oggpack_buffer opb;
  281. if(op){
  282. oggpack_readinit(&opb,op->packet,op->bytes);
  283. /* Which of the three types of header is this? */
  284. /* Also verify header-ness, vorbis */
  285. {
  286. char buffer[6];
  287. int packtype=oggpack_read(&opb,8);
  288. memset(buffer,0,6);
  289. _v_readstring(&opb,buffer,6);
  290. if(memcmp(buffer,"vorbis",6)){
  291. /* not a vorbis header */
  292. return(OV_ENOTVORBIS);
  293. }
  294. switch(packtype){
  295. case 0x01: /* least significant *bit* is read first */
  296. if(!op->b_o_s){
  297. /* Not the initial packet */
  298. return(OV_EBADHEADER);
  299. }
  300. if(vi->rate!=0){
  301. /* previously initialized info header */
  302. return(OV_EBADHEADER);
  303. }
  304. return(_vorbis_unpack_info(vi,&opb));
  305. case 0x03: /* least significant *bit* is read first */
  306. if(vi->rate==0){
  307. /* um... we didn't get the initial header */
  308. return(OV_EBADHEADER);
  309. }
  310. return(_vorbis_unpack_comment(vc,&opb));
  311. case 0x05: /* least significant *bit* is read first */
  312. if(vi->rate==0 || vc->vendor==NULL){
  313. /* um... we didn;t get the initial header or comments yet */
  314. return(OV_EBADHEADER);
  315. }
  316. return(_vorbis_unpack_books(vi,&opb));
  317. default:
  318. /* Not a valid vorbis header type */
  319. return(OV_EBADHEADER);
  320. break;
  321. }
  322. }
  323. }
  324. return(OV_EBADHEADER);
  325. }