decinfo.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggTheora 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 Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009 *
  9. * by the Xiph.Org Foundation and contributors http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function:
  13. last mod: $Id$
  14. ********************************************************************/
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <limits.h>
  18. #include "decint.h"
  19. /*Unpacks a series of octets from a given byte array into the pack buffer.
  20. No checking is done to ensure the buffer contains enough data.
  21. _opb: The pack buffer to read the octets from.
  22. _buf: The byte array to store the unpacked bytes in.
  23. _len: The number of octets to unpack.*/
  24. static void oc_unpack_octets(oc_pack_buf *_opb,char *_buf,size_t _len){
  25. while(_len-->0){
  26. long val;
  27. val=oc_pack_read(_opb,8);
  28. *_buf++=(char)val;
  29. }
  30. }
  31. /*Unpacks a 32-bit integer encoded by octets in little-endian form.*/
  32. static long oc_unpack_length(oc_pack_buf *_opb){
  33. long ret[4];
  34. int i;
  35. for(i=0;i<4;i++)ret[i]=oc_pack_read(_opb,8);
  36. return ret[0]|ret[1]<<8|ret[2]<<16|ret[3]<<24;
  37. }
  38. static int oc_info_unpack(oc_pack_buf *_opb,th_info *_info){
  39. long val;
  40. /*Check the codec bitstream version.*/
  41. val=oc_pack_read(_opb,8);
  42. _info->version_major=(unsigned char)val;
  43. val=oc_pack_read(_opb,8);
  44. _info->version_minor=(unsigned char)val;
  45. val=oc_pack_read(_opb,8);
  46. _info->version_subminor=(unsigned char)val;
  47. /*verify we can parse this bitstream version.
  48. We accept earlier minors and all subminors, by spec*/
  49. if(_info->version_major>TH_VERSION_MAJOR||
  50. _info->version_major==TH_VERSION_MAJOR&&
  51. _info->version_minor>TH_VERSION_MINOR){
  52. return TH_EVERSION;
  53. }
  54. /*Read the encoded frame description.*/
  55. val=oc_pack_read(_opb,16);
  56. _info->frame_width=(ogg_uint32_t)val<<4;
  57. val=oc_pack_read(_opb,16);
  58. _info->frame_height=(ogg_uint32_t)val<<4;
  59. val=oc_pack_read(_opb,24);
  60. _info->pic_width=(ogg_uint32_t)val;
  61. val=oc_pack_read(_opb,24);
  62. _info->pic_height=(ogg_uint32_t)val;
  63. val=oc_pack_read(_opb,8);
  64. _info->pic_x=(ogg_uint32_t)val;
  65. val=oc_pack_read(_opb,8);
  66. _info->pic_y=(ogg_uint32_t)val;
  67. val=oc_pack_read(_opb,32);
  68. _info->fps_numerator=(ogg_uint32_t)val;
  69. val=oc_pack_read(_opb,32);
  70. _info->fps_denominator=(ogg_uint32_t)val;
  71. if(_info->frame_width==0||_info->frame_height==0||
  72. _info->pic_width+_info->pic_x>_info->frame_width||
  73. _info->pic_height+_info->pic_y>_info->frame_height||
  74. _info->fps_numerator==0||_info->fps_denominator==0){
  75. return TH_EBADHEADER;
  76. }
  77. /*Note: The sense of pic_y is inverted in what we pass back to the
  78. application compared to how it is stored in the bitstream.
  79. This is because the bitstream uses a right-handed coordinate system, while
  80. applications expect a left-handed one.*/
  81. _info->pic_y=_info->frame_height-_info->pic_height-_info->pic_y;
  82. val=oc_pack_read(_opb,24);
  83. _info->aspect_numerator=(ogg_uint32_t)val;
  84. val=oc_pack_read(_opb,24);
  85. _info->aspect_denominator=(ogg_uint32_t)val;
  86. val=oc_pack_read(_opb,8);
  87. _info->colorspace=(th_colorspace)val;
  88. val=oc_pack_read(_opb,24);
  89. _info->target_bitrate=(int)val;
  90. val=oc_pack_read(_opb,6);
  91. _info->quality=(int)val;
  92. val=oc_pack_read(_opb,5);
  93. _info->keyframe_granule_shift=(int)val;
  94. val=oc_pack_read(_opb,2);
  95. _info->pixel_fmt=(th_pixel_fmt)val;
  96. if(_info->pixel_fmt==TH_PF_RSVD)return TH_EBADHEADER;
  97. val=oc_pack_read(_opb,3);
  98. if(val!=0||oc_pack_bytes_left(_opb)<0)return TH_EBADHEADER;
  99. return 0;
  100. }
  101. static int oc_comment_unpack(oc_pack_buf *_opb,th_comment *_tc){
  102. long len;
  103. int i;
  104. /*Read the vendor string.*/
  105. len=oc_unpack_length(_opb);
  106. if(len<0||len>oc_pack_bytes_left(_opb))return TH_EBADHEADER;
  107. _tc->vendor=_ogg_malloc((size_t)len+1);
  108. if(_tc->vendor==NULL)return TH_EFAULT;
  109. oc_unpack_octets(_opb,_tc->vendor,len);
  110. _tc->vendor[len]='\0';
  111. /*Read the user comments.*/
  112. _tc->comments=(int)oc_unpack_length(_opb);
  113. len=_tc->comments;
  114. if(len<0||len>(LONG_MAX>>2)||len<<2>oc_pack_bytes_left(_opb)){
  115. _tc->comments=0;
  116. return TH_EBADHEADER;
  117. }
  118. _tc->comment_lengths=(int *)_ogg_malloc(
  119. _tc->comments*sizeof(_tc->comment_lengths[0]));
  120. _tc->user_comments=(char **)_ogg_malloc(
  121. _tc->comments*sizeof(_tc->user_comments[0]));
  122. if(_tc->comment_lengths==NULL||_tc->user_comments==NULL){
  123. _tc->comments=0;
  124. return TH_EFAULT;
  125. }
  126. for(i=0;i<_tc->comments;i++){
  127. len=oc_unpack_length(_opb);
  128. if(len<0||len>oc_pack_bytes_left(_opb)){
  129. _tc->comments=i;
  130. return TH_EBADHEADER;
  131. }
  132. _tc->comment_lengths[i]=len;
  133. _tc->user_comments[i]=_ogg_malloc((size_t)len+1);
  134. if(_tc->user_comments[i]==NULL){
  135. _tc->comments=i;
  136. return TH_EFAULT;
  137. }
  138. oc_unpack_octets(_opb,_tc->user_comments[i],len);
  139. _tc->user_comments[i][len]='\0';
  140. }
  141. return oc_pack_bytes_left(_opb)<0?TH_EBADHEADER:0;
  142. }
  143. static int oc_setup_unpack(oc_pack_buf *_opb,th_setup_info *_setup){
  144. int ret;
  145. /*Read the quantizer tables.*/
  146. ret=oc_quant_params_unpack(_opb,&_setup->qinfo);
  147. if(ret<0)return ret;
  148. /*Read the Huffman trees.*/
  149. return oc_huff_trees_unpack(_opb,_setup->huff_tables);
  150. }
  151. static void oc_setup_clear(th_setup_info *_setup){
  152. oc_quant_params_clear(&_setup->qinfo);
  153. oc_huff_trees_clear(_setup->huff_tables);
  154. }
  155. static int oc_dec_headerin(oc_pack_buf *_opb,th_info *_info,
  156. th_comment *_tc,th_setup_info **_setup,ogg_packet *_op){
  157. char buffer[6];
  158. long val;
  159. int packtype;
  160. int ret;
  161. val=oc_pack_read(_opb,8);
  162. packtype=(int)val;
  163. /*If we're at a data packet...*/
  164. if(!(packtype&0x80)){
  165. /*Check to make sure we received all three headers...
  166. If we haven't seen any valid headers, assume this is not actually
  167. Theora.*/
  168. if(_info->frame_width<=0)return TH_ENOTFORMAT;
  169. /*Follow our documentation, which says we'll return TH_EFAULT if this
  170. are NULL (_info was checked by our caller).*/
  171. if(_tc==NULL)return TH_EFAULT;
  172. /*And if any other headers were missing, declare this packet "out of
  173. sequence" instead.*/
  174. if(_tc->vendor==NULL)return TH_EBADHEADER;
  175. /*Don't check this until it's needed, since we allow passing NULL for the
  176. arguments that we're not expecting the next header to fill in yet.*/
  177. if(_setup==NULL)return TH_EFAULT;
  178. if(*_setup==NULL)return TH_EBADHEADER;
  179. /*If we got everything, we're done.*/
  180. return 0;
  181. }
  182. /*Check the codec string.*/
  183. oc_unpack_octets(_opb,buffer,6);
  184. if(memcmp(buffer,"theora",6)!=0)return TH_ENOTFORMAT;
  185. switch(packtype){
  186. /*Codec info header.*/
  187. case 0x80:{
  188. /*This should be the first packet, and we should not already be
  189. initialized.*/
  190. if(!_op->b_o_s||_info->frame_width>0)return TH_EBADHEADER;
  191. ret=oc_info_unpack(_opb,_info);
  192. if(ret<0)th_info_clear(_info);
  193. else ret=3;
  194. }break;
  195. /*Comment header.*/
  196. case 0x81:{
  197. if(_tc==NULL)return TH_EFAULT;
  198. /*We shoud have already decoded the info header, and should not yet have
  199. decoded the comment header.*/
  200. if(_info->frame_width==0||_tc->vendor!=NULL)return TH_EBADHEADER;
  201. ret=oc_comment_unpack(_opb,_tc);
  202. if(ret<0)th_comment_clear(_tc);
  203. else ret=2;
  204. }break;
  205. /*Codec setup header.*/
  206. case 0x82:{
  207. oc_setup_info *setup;
  208. if(_tc==NULL||_setup==NULL)return TH_EFAULT;
  209. /*We should have already decoded the info header and the comment header,
  210. and should not yet have decoded the setup header.*/
  211. if(_info->frame_width==0||_tc->vendor==NULL||*_setup!=NULL){
  212. return TH_EBADHEADER;
  213. }
  214. setup=(oc_setup_info *)_ogg_calloc(1,sizeof(*setup));
  215. if(setup==NULL)return TH_EFAULT;
  216. ret=oc_setup_unpack(_opb,setup);
  217. if(ret<0){
  218. oc_setup_clear(setup);
  219. _ogg_free(setup);
  220. }
  221. else{
  222. *_setup=setup;
  223. ret=1;
  224. }
  225. }break;
  226. default:{
  227. /*We don't know what this header is.*/
  228. return TH_EBADHEADER;
  229. }break;
  230. }
  231. return ret;
  232. }
  233. /*Decodes one header packet.
  234. This should be called repeatedly with the packets at the beginning of the
  235. stream until it returns 0.*/
  236. int th_decode_headerin(th_info *_info,th_comment *_tc,
  237. th_setup_info **_setup,ogg_packet *_op){
  238. oc_pack_buf opb;
  239. if(_op==NULL)return TH_EBADHEADER;
  240. if(_info==NULL)return TH_EFAULT;
  241. oc_pack_readinit(&opb,_op->packet,_op->bytes);
  242. return oc_dec_headerin(&opb,_info,_tc,_setup,_op);
  243. }
  244. void th_setup_free(th_setup_info *_setup){
  245. if(_setup!=NULL){
  246. oc_setup_clear(_setup);
  247. _ogg_free(_setup);
  248. }
  249. }