2
0

info.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 "ogg.h"
  21. #include "ivorbiscodec.h"
  22. #include "codec_internal.h"
  23. #include "codebook.h"
  24. #include "misc.h"
  25. #include "os.h"
  26. /* helpers */
  27. static void _v_readstring(oggpack_buffer *o,char *buf,int bytes){
  28. while(bytes--){
  29. *buf++=oggpack_read(o,8);
  30. }
  31. }
  32. void vorbis_comment_init(vorbis_comment *vc){
  33. memset(vc,0,sizeof(*vc));
  34. }
  35. /* This is more or less the same as strncasecmp - but that doesn't exist
  36. * everywhere, and this is a fairly trivial function, so we include it */
  37. static int tagcompare(const char *s1, const char *s2, int n){
  38. int c=0;
  39. while(c < n){
  40. if(toupper(s1[c]) != toupper(s2[c]))
  41. return !0;
  42. c++;
  43. }
  44. return 0;
  45. }
  46. char *vorbis_comment_query(vorbis_comment *vc, char *tag, int count){
  47. long i;
  48. int found = 0;
  49. int taglen = strlen(tag)+1; /* +1 for the = we append */
  50. char *fulltag = (char *)alloca(taglen+ 1);
  51. strcpy(fulltag, tag);
  52. strcat(fulltag, "=");
  53. for(i=0;i<vc->comments;i++){
  54. if(!tagcompare(vc->user_comments[i], fulltag, taglen)){
  55. if(count == found)
  56. /* We return a pointer to the data, not a copy */
  57. return vc->user_comments[i] + taglen;
  58. else
  59. found++;
  60. }
  61. }
  62. return NULL; /* didn't find anything */
  63. }
  64. int vorbis_comment_query_count(vorbis_comment *vc, char *tag){
  65. int i,count=0;
  66. int taglen = strlen(tag)+1; /* +1 for the = we append */
  67. char *fulltag = (char *)alloca(taglen+1);
  68. strcpy(fulltag,tag);
  69. strcat(fulltag, "=");
  70. for(i=0;i<vc->comments;i++){
  71. if(!tagcompare(vc->user_comments[i], fulltag, taglen))
  72. count++;
  73. }
  74. return count;
  75. }
  76. void vorbis_comment_clear(vorbis_comment *vc){
  77. if(vc){
  78. long i;
  79. for(i=0;i<vc->comments;i++)
  80. if(vc->user_comments[i])_ogg_free(vc->user_comments[i]);
  81. if(vc->user_comments)_ogg_free(vc->user_comments);
  82. if(vc->comment_lengths)_ogg_free(vc->comment_lengths);
  83. if(vc->vendor)_ogg_free(vc->vendor);
  84. }
  85. memset(vc,0,sizeof(*vc));
  86. }
  87. /* blocksize 0 is guaranteed to be short, 1 is guarantted to be long.
  88. They may be equal, but short will never ge greater than long */
  89. int vorbis_info_blocksize(vorbis_info *vi,int zo){
  90. codec_setup_info *ci = (codec_setup_info *)vi->codec_setup;
  91. return ci ? ci->blocksizes[zo] : -1;
  92. }
  93. /* used by synthesis, which has a full, alloced vi */
  94. void vorbis_info_init(vorbis_info *vi){
  95. memset(vi,0,sizeof(*vi));
  96. vi->codec_setup=(codec_setup_info *)_ogg_calloc(1,sizeof(codec_setup_info));
  97. }
  98. void vorbis_info_clear(vorbis_info *vi){
  99. codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
  100. int i;
  101. if(ci){
  102. if(ci->mode_param)_ogg_free(ci->mode_param);
  103. if(ci->map_param){
  104. for(i=0;i<ci->maps;i++) /* unpack does the range checking */
  105. mapping_clear_info(ci->map_param+i);
  106. _ogg_free(ci->map_param);
  107. }
  108. if(ci->floor_param){
  109. for(i=0;i<ci->floors;i++) /* unpack does the range checking */
  110. if(ci->floor_type[i])
  111. floor1_free_info(ci->floor_param[i]);
  112. else
  113. floor0_free_info(ci->floor_param[i]);
  114. _ogg_free(ci->floor_param);
  115. _ogg_free(ci->floor_type);
  116. }
  117. if(ci->residue_param){
  118. for(i=0;i<ci->residues;i++) /* unpack does the range checking */
  119. res_clear_info(ci->residue_param+i);
  120. _ogg_free(ci->residue_param);
  121. }
  122. if(ci->book_param){
  123. for(i=0;i<ci->books;i++)
  124. vorbis_book_clear(ci->book_param+i);
  125. _ogg_free(ci->book_param);
  126. }
  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. #ifdef LIMIT_TO_64kHz
  145. if(vi->rate>=64000 || ci->blocksizes[1]>4096)goto err_out;
  146. #else
  147. if(vi->rate<64000 && ci->blocksizes[1]>4096)goto err_out;
  148. #endif
  149. if(vi->rate<1)goto err_out;
  150. if(vi->channels<1)goto err_out;
  151. if(ci->blocksizes[0]<64)goto err_out;
  152. if(ci->blocksizes[1]<ci->blocksizes[0])goto err_out;
  153. if(ci->blocksizes[1]>8192)goto err_out;
  154. if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
  155. return(0);
  156. err_out:
  157. vorbis_info_clear(vi);
  158. return(OV_EBADHEADER);
  159. }
  160. static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
  161. int i;
  162. int vendorlen=oggpack_read(opb,32);
  163. if(vendorlen<0)goto err_out;
  164. vc->vendor=(char *)_ogg_calloc(vendorlen+1,1);
  165. _v_readstring(opb,vc->vendor,vendorlen);
  166. vc->comments=oggpack_read(opb,32);
  167. if(vc->comments<0)goto err_out;
  168. vc->user_comments=(char **)_ogg_calloc(vc->comments+1,sizeof(*vc->user_comments));
  169. vc->comment_lengths=(int *)_ogg_calloc(vc->comments+1, sizeof(*vc->comment_lengths));
  170. for(i=0;i<vc->comments;i++){
  171. int len=oggpack_read(opb,32);
  172. if(len<0)goto err_out;
  173. vc->comment_lengths[i]=len;
  174. vc->user_comments[i]=(char *)_ogg_calloc(len+1,1);
  175. _v_readstring(opb,vc->user_comments[i],len);
  176. }
  177. if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
  178. return(0);
  179. err_out:
  180. vorbis_comment_clear(vc);
  181. return(OV_EBADHEADER);
  182. }
  183. /* all of the real encoding details are here. The modes, books,
  184. everything */
  185. static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
  186. codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
  187. int i;
  188. if(!ci)return(OV_EFAULT);
  189. /* codebooks */
  190. ci->books=oggpack_read(opb,8)+1;
  191. ci->book_param=(codebook *)_ogg_calloc(ci->books,sizeof(*ci->book_param));
  192. for(i=0;i<ci->books;i++)
  193. if(vorbis_book_unpack(opb,ci->book_param+i))goto err_out;
  194. /* time backend settings, not actually used */
  195. i=oggpack_read(opb,6);
  196. for(;i>=0;i--)
  197. if(oggpack_read(opb,16)!=0)goto err_out;
  198. /* floor backend settings */
  199. ci->floors=oggpack_read(opb,6)+1;
  200. ci->floor_param=_ogg_malloc(sizeof(*ci->floor_param)*ci->floors);
  201. ci->floor_type=_ogg_malloc(sizeof(*ci->floor_type)*ci->floors);
  202. for(i=0;i<ci->floors;i++){
  203. ci->floor_type[i]=oggpack_read(opb,16);
  204. if(ci->floor_type[i]<0 || ci->floor_type[i]>=VI_FLOORB)goto err_out;
  205. if(ci->floor_type[i])
  206. ci->floor_param[i]=floor1_info_unpack(vi,opb);
  207. else
  208. ci->floor_param[i]=floor0_info_unpack(vi,opb);
  209. if(!ci->floor_param[i])goto err_out;
  210. }
  211. /* residue backend settings */
  212. ci->residues=oggpack_read(opb,6)+1;
  213. ci->residue_param=_ogg_malloc(sizeof(*ci->residue_param)*ci->residues);
  214. for(i=0;i<ci->residues;i++)
  215. if(res_unpack(ci->residue_param+i,vi,opb))goto err_out;
  216. /* map backend settings */
  217. ci->maps=oggpack_read(opb,6)+1;
  218. ci->map_param=_ogg_malloc(sizeof(*ci->map_param)*ci->maps);
  219. for(i=0;i<ci->maps;i++){
  220. if(oggpack_read(opb,16)!=0)goto err_out;
  221. if(mapping_info_unpack(ci->map_param+i,vi,opb))goto err_out;
  222. }
  223. /* mode settings */
  224. ci->modes=oggpack_read(opb,6)+1;
  225. ci->mode_param=
  226. (vorbis_info_mode *)_ogg_malloc(ci->modes*sizeof(*ci->mode_param));
  227. for(i=0;i<ci->modes;i++){
  228. ci->mode_param[i].blockflag=oggpack_read(opb,1);
  229. if(oggpack_read(opb,16))goto err_out;
  230. if(oggpack_read(opb,16))goto err_out;
  231. ci->mode_param[i].mapping=oggpack_read(opb,8);
  232. if(ci->mode_param[i].mapping>=ci->maps)goto err_out;
  233. }
  234. if(oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
  235. return(0);
  236. err_out:
  237. vorbis_info_clear(vi);
  238. return(OV_EBADHEADER);
  239. }
  240. /* The Vorbis header is in three packets; the initial small packet in
  241. the first page that identifies basic parameters, a second packet
  242. with bitstream comments and a third packet that holds the
  243. codebook. */
  244. int vorbis_dsp_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
  245. oggpack_buffer opb;
  246. if(op){
  247. oggpack_readinit(&opb,op->packet);
  248. /* Which of the three types of header is this? */
  249. /* Also verify header-ness, vorbis */
  250. {
  251. char buffer[6];
  252. int packtype=oggpack_read(&opb,8);
  253. memset(buffer,0,6);
  254. _v_readstring(&opb,buffer,6);
  255. if(memcmp(buffer,"vorbis",6)){
  256. /* not a vorbis header */
  257. return(OV_ENOTVORBIS);
  258. }
  259. switch(packtype){
  260. case 0x01: /* least significant *bit* is read first */
  261. if(!op->b_o_s){
  262. /* Not the initial packet */
  263. return(OV_EBADHEADER);
  264. }
  265. if(vi->rate!=0){
  266. /* previously initialized info header */
  267. return(OV_EBADHEADER);
  268. }
  269. return(_vorbis_unpack_info(vi,&opb));
  270. case 0x03: /* least significant *bit* is read first */
  271. if(vi->rate==0){
  272. /* um... we didn't get the initial header */
  273. return(OV_EBADHEADER);
  274. }
  275. return(_vorbis_unpack_comment(vc,&opb));
  276. case 0x05: /* least significant *bit* is read first */
  277. if(vi->rate==0 || vc->vendor==NULL){
  278. /* um... we didn;t get the initial header or comments yet */
  279. return(OV_EBADHEADER);
  280. }
  281. return(_vorbis_unpack_books(vi,&opb));
  282. default:
  283. /* Not a valid vorbis header type */
  284. return(OV_EBADHEADER);
  285. break;
  286. }
  287. }
  288. }
  289. return(OV_EBADHEADER);
  290. }