treminfo.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /************************************************************************
  2. * Copyright (C) 2002-2009, Xiph.org Foundation
  3. * Copyright (C) 2010, Robin Watts for Pinknoise Productions Ltd
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the names of the Xiph.org Foundation nor Pinknoise
  17. * Productions Ltd nor the names of its contributors may be used to
  18. * endorse or promote products derived from this software without
  19. * specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ************************************************************************
  33. function: maintain the info structure, info <-> header packets
  34. ************************************************************************/
  35. /* general handling of the header and the vorbis_info structure (and
  36. substructures) */
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <ctype.h>
  40. #include "ogg.h"
  41. #include "ivorbiscodec.h"
  42. #include "codec_internal.h"
  43. #include "codebook.h"
  44. #include "misc.h"
  45. #include "os.h"
  46. /* helpers */
  47. static void _v_readstring(oggpack_buffer *o,char *buf,int bytes){
  48. while(bytes--){
  49. *buf++=(char)oggpack_read(o,8);
  50. }
  51. }
  52. void vorbis_comment_init(vorbis_comment *vc){
  53. memset(vc,0,sizeof(*vc));
  54. }
  55. /* This is more or less the same as strncasecmp - but that doesn't exist
  56. * everywhere, and this is a fairly trivial function, so we include it */
  57. static int tagcompare(const char *s1, const char *s2, int n){
  58. int c=0;
  59. while(c < n){
  60. if(toupper(s1[c]) != toupper(s2[c]))
  61. return !0;
  62. c++;
  63. }
  64. return 0;
  65. }
  66. char *vorbis_comment_query(vorbis_comment *vc, char *tag, int count){
  67. long i;
  68. int found = 0;
  69. int taglen = strlen(tag)+1; /* +1 for the = we append */
  70. char *fulltag = (char *)alloca(taglen+ 1);
  71. strcpy(fulltag, tag);
  72. strcat(fulltag, "=");
  73. for(i=0;i<vc->comments;i++){
  74. if(!tagcompare(vc->user_comments[i], fulltag, taglen)){
  75. if(count == found)
  76. /* We return a pointer to the data, not a copy */
  77. return vc->user_comments[i] + taglen;
  78. else
  79. found++;
  80. }
  81. }
  82. return NULL; /* didn't find anything */
  83. }
  84. int vorbis_comment_query_count(vorbis_comment *vc, char *tag){
  85. int i,count=0;
  86. int taglen = strlen(tag)+1; /* +1 for the = we append */
  87. char *fulltag = (char *)alloca(taglen+1);
  88. strcpy(fulltag,tag);
  89. strcat(fulltag, "=");
  90. for(i=0;i<vc->comments;i++){
  91. if(!tagcompare(vc->user_comments[i], fulltag, taglen))
  92. count++;
  93. }
  94. return count;
  95. }
  96. void vorbis_comment_clear(vorbis_comment *vc){
  97. if(vc){
  98. long i;
  99. for(i=0;i<vc->comments;i++)
  100. if(vc->user_comments[i])_ogg_free(vc->user_comments[i]);
  101. if(vc->user_comments)_ogg_free(vc->user_comments);
  102. if(vc->comment_lengths)_ogg_free(vc->comment_lengths);
  103. if(vc->vendor)_ogg_free(vc->vendor);
  104. }
  105. memset(vc,0,sizeof(*vc));
  106. }
  107. /* blocksize 0 is guaranteed to be short, 1 is guarantted to be long.
  108. They may be equal, but short will never ge greater than long */
  109. int vorbis_info_blocksize(vorbis_info *vi,int zo){
  110. codec_setup_info *ci = (codec_setup_info *)vi->codec_setup;
  111. return ci ? ci->blocksizes[zo] : -1;
  112. }
  113. /* used by synthesis, which has a full, alloced vi */
  114. void vorbis_info_init(vorbis_info *vi){
  115. memset(vi,0,sizeof(*vi));
  116. vi->codec_setup=(codec_setup_info *)_ogg_calloc(1,sizeof(codec_setup_info));
  117. }
  118. void vorbis_info_clear(vorbis_info *vi){
  119. codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
  120. int i;
  121. if(ci){
  122. if(ci->mode_param)_ogg_free(ci->mode_param);
  123. if(ci->map_param){
  124. for(i=0;i<ci->maps;i++) /* unpack does the range checking */
  125. mapping_clear_info(ci->map_param+i);
  126. _ogg_free(ci->map_param);
  127. }
  128. if(ci->floor_param){
  129. for(i=0;i<ci->floors;i++) /* unpack does the range checking */
  130. if(ci->floor_type[i])
  131. floor1_free_info(ci->floor_param[i]);
  132. else
  133. floor0_free_info(ci->floor_param[i]);
  134. _ogg_free(ci->floor_param);
  135. _ogg_free(ci->floor_type);
  136. }
  137. if(ci->residue_param){
  138. for(i=0;i<ci->residues;i++) /* unpack does the range checking */
  139. res_clear_info(ci->residue_param+i);
  140. _ogg_free(ci->residue_param);
  141. }
  142. if(ci->book_param){
  143. for(i=0;i<ci->books;i++)
  144. vorbis_book_clear(ci->book_param+i);
  145. _ogg_free(ci->book_param);
  146. }
  147. _ogg_free(ci);
  148. }
  149. memset(vi,0,sizeof(*vi));
  150. }
  151. /* Header packing/unpacking ********************************************/
  152. int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
  153. codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
  154. if(!ci)return(OV_EFAULT);
  155. vi->version=oggpack_read(opb,32);
  156. if(vi->version!=0)return(OV_EVERSION);
  157. vi->channels=oggpack_read(opb,8);
  158. vi->rate=oggpack_read(opb,32);
  159. vi->bitrate_upper=oggpack_read(opb,32);
  160. vi->bitrate_nominal=oggpack_read(opb,32);
  161. vi->bitrate_lower=oggpack_read(opb,32);
  162. ci->blocksizes[0]=1<<oggpack_read(opb,4);
  163. ci->blocksizes[1]=1<<oggpack_read(opb,4);
  164. #ifdef LIMIT_TO_64kHz
  165. if(vi->rate>=64000 || ci->blocksizes[1]>4096)goto err_out;
  166. #else
  167. if(vi->rate<64000 && ci->blocksizes[1]>4096)goto err_out;
  168. #endif
  169. if(vi->rate<1)goto err_out;
  170. if(vi->channels<1)goto err_out;
  171. if(ci->blocksizes[0]<64)goto err_out;
  172. if(ci->blocksizes[1]<ci->blocksizes[0])goto err_out;
  173. if(ci->blocksizes[1]>8192)goto err_out;
  174. if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
  175. return(0);
  176. err_out:
  177. vorbis_info_clear(vi);
  178. return(OV_EBADHEADER);
  179. }
  180. int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
  181. int i;
  182. int vendorlen=oggpack_read(opb,32);
  183. if(vendorlen<0)goto err_out;
  184. vc->vendor=(char *)_ogg_calloc(vendorlen+1,1);
  185. if(!vc->vendor)goto err_out;
  186. _v_readstring(opb,vc->vendor,vendorlen);
  187. vc->comments=oggpack_read(opb,32);
  188. if(vc->comments<0)goto err_out;
  189. vc->user_comments=(char **)_ogg_calloc(vc->comments+1,sizeof(*vc->user_comments));
  190. if (!vc->user_comments){
  191. vc->comments=0;
  192. goto err_out;
  193. }
  194. vc->comment_lengths=(int *)_ogg_calloc(vc->comments+1, sizeof(*vc->comment_lengths));
  195. if (!vc->comment_lengths)goto err_out;
  196. for(i=0;i<vc->comments;i++){
  197. int len=oggpack_read(opb,32);
  198. if(len<0)goto err_out;
  199. vc->comment_lengths[i]=len;
  200. vc->user_comments[i]=(char *)_ogg_calloc(len+1,1);
  201. if(!vc->user_comments[i])goto err_out;
  202. _v_readstring(opb,vc->user_comments[i],len);
  203. }
  204. if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
  205. return(0);
  206. err_out:
  207. vorbis_comment_clear(vc);
  208. return(OV_EBADHEADER);
  209. }
  210. /* all of the real encoding details are here. The modes, books,
  211. everything */
  212. int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
  213. codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
  214. int i;
  215. if(!ci)return(OV_EFAULT);
  216. /* codebooks */
  217. ci->books=oggpack_read(opb,8)+1;
  218. ci->book_param=(codebook *)_ogg_calloc(ci->books,sizeof(*ci->book_param));
  219. if(!ci->book_param){
  220. ci->books=0;
  221. goto err_out;
  222. }
  223. for(i=0;i<ci->books;i++)
  224. if(vorbis_book_unpack(opb,ci->book_param+i))goto err_out;
  225. /* time backend settings, not actually used */
  226. i=oggpack_read(opb,6);
  227. for(;i>=0;i--)
  228. if(oggpack_read(opb,16)!=0)goto err_out;
  229. /* floor backend settings */
  230. ci->floors=oggpack_read(opb,6)+1;
  231. ci->floor_param=_ogg_calloc(ci->floors, sizeof(*ci->floor_param));
  232. ci->floor_type=_ogg_calloc(ci->floors, sizeof(*ci->floor_type));
  233. if(!ci->floor_param || !ci->floor_type){
  234. ci->floors=0;
  235. goto err_out;
  236. }
  237. for(i=0;i<ci->floors;i++){
  238. ci->floor_type[i]=(char)oggpack_read(opb,16);
  239. if(ci->floor_type[i]<0 || ci->floor_type[i]>=VI_FLOORB)goto err_out;
  240. if(ci->floor_type[i])
  241. ci->floor_param[i]=floor1_info_unpack(vi,opb);
  242. else
  243. ci->floor_param[i]=floor0_info_unpack(vi,opb);
  244. if(!ci->floor_param[i])goto err_out;
  245. }
  246. /* residue backend settings */
  247. ci->residues=oggpack_read(opb,6)+1;
  248. ci->residue_param=_ogg_calloc(ci->residues, sizeof(*ci->residue_param));
  249. if (!ci->residue_param){
  250. ci->residues=0;
  251. goto err_out;
  252. }
  253. for(i=0;i<ci->residues;i++)
  254. if(res_unpack(ci->residue_param+i,vi,opb))goto err_out;
  255. /* map backend settings */
  256. ci->maps=oggpack_read(opb,6)+1;
  257. ci->map_param=_ogg_calloc(ci->maps, sizeof(*ci->map_param));
  258. if (!ci->map_param){
  259. ci->maps=0;
  260. goto err_out;
  261. }
  262. for(i=0;i<ci->maps;i++){
  263. if(oggpack_read(opb,16)!=0)goto err_out;
  264. if(mapping_info_unpack(ci->map_param+i,vi,opb))goto err_out;
  265. }
  266. /* mode settings */
  267. ci->modes=oggpack_read(opb,6)+1;
  268. ci->mode_param=
  269. (vorbis_info_mode *)_ogg_calloc(ci->modes, sizeof(*ci->mode_param));
  270. if (!ci->mode_param){
  271. ci->modes=0;
  272. goto err_out;
  273. }
  274. for(i=0;i<ci->modes;i++){
  275. ci->mode_param[i].blockflag=(unsigned char)oggpack_read(opb,1);
  276. if(oggpack_read(opb,16))goto err_out;
  277. if(oggpack_read(opb,16))goto err_out;
  278. ci->mode_param[i].mapping=(unsigned char)oggpack_read(opb,8);
  279. if(ci->mode_param[i].mapping>=ci->maps)goto err_out;
  280. }
  281. if(oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
  282. return(0);
  283. err_out:
  284. vorbis_info_clear(vi);
  285. return(OV_EBADHEADER);
  286. }
  287. /* The Vorbis header is in three packets; the initial small packet in
  288. the first page that identifies basic parameters, a second packet
  289. with bitstream comments and a third packet that holds the
  290. codebook. */
  291. int vorbis_dsp_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
  292. oggpack_buffer opb;
  293. if(op){
  294. oggpack_readinit(&opb,op->packet);
  295. /* Which of the three types of header is this? */
  296. /* Also verify header-ness, vorbis */
  297. {
  298. char buffer[6];
  299. int packtype=oggpack_read(&opb,8);
  300. memset(buffer,0,6);
  301. _v_readstring(&opb,buffer,6);
  302. if(memcmp(buffer,"vorbis",6)){
  303. /* not a vorbis header */
  304. return(OV_ENOTVORBIS);
  305. }
  306. switch(packtype){
  307. case 0x01: /* least significant *bit* is read first */
  308. if(!op->b_o_s){
  309. /* Not the initial packet */
  310. return(OV_EBADHEADER);
  311. }
  312. if(vi->rate!=0){
  313. /* previously initialized info header */
  314. return(OV_EBADHEADER);
  315. }
  316. return(_vorbis_unpack_info(vi,&opb));
  317. case 0x03: /* least significant *bit* is read first */
  318. if(vi->rate==0){
  319. /* um... we didn't get the initial header */
  320. return(OV_EBADHEADER);
  321. }
  322. return(_vorbis_unpack_comment(vc,&opb));
  323. case 0x05: /* least significant *bit* is read first */
  324. if(vi->rate==0 || vc->vendor==NULL){
  325. /* um... we didn;t get the initial header or comments yet */
  326. return(OV_EBADHEADER);
  327. }
  328. return(_vorbis_unpack_books(vi,&opb));
  329. default:
  330. /* Not a valid vorbis header type */
  331. return(OV_EBADHEADER);
  332. break;
  333. }
  334. }
  335. }
  336. return(OV_EBADHEADER);
  337. }