decinfo.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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: decinfo.c 17276 2010-06-05 05:57:05Z tterribe $
  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 and we have received all three headers, we're
  164. done.*/
  165. if(!(packtype&0x80)&&_info->frame_width>0&&_tc->vendor!=NULL&&*_setup!=NULL){
  166. return 0;
  167. }
  168. /*Check the codec string.*/
  169. oc_unpack_octets(_opb,buffer,6);
  170. if(memcmp(buffer,"theora",6)!=0)return TH_ENOTFORMAT;
  171. switch(packtype){
  172. /*Codec info header.*/
  173. case 0x80:{
  174. /*This should be the first packet, and we should not already be
  175. initialized.*/
  176. if(!_op->b_o_s||_info->frame_width>0)return TH_EBADHEADER;
  177. ret=oc_info_unpack(_opb,_info);
  178. if(ret<0)th_info_clear(_info);
  179. else ret=3;
  180. }break;
  181. /*Comment header.*/
  182. case 0x81:{
  183. if(_tc==NULL)return TH_EFAULT;
  184. /*We shoud have already decoded the info header, and should not yet have
  185. decoded the comment header.*/
  186. if(_info->frame_width==0||_tc->vendor!=NULL)return TH_EBADHEADER;
  187. ret=oc_comment_unpack(_opb,_tc);
  188. if(ret<0)th_comment_clear(_tc);
  189. else ret=2;
  190. }break;
  191. /*Codec setup header.*/
  192. case 0x82:{
  193. oc_setup_info *setup;
  194. if(_tc==NULL||_setup==NULL)return TH_EFAULT;
  195. /*We should have already decoded the info header and the comment header,
  196. and should not yet have decoded the setup header.*/
  197. if(_info->frame_width==0||_tc->vendor==NULL||*_setup!=NULL){
  198. return TH_EBADHEADER;
  199. }
  200. setup=(oc_setup_info *)_ogg_calloc(1,sizeof(*setup));
  201. if(setup==NULL)return TH_EFAULT;
  202. ret=oc_setup_unpack(_opb,setup);
  203. if(ret<0){
  204. oc_setup_clear(setup);
  205. _ogg_free(setup);
  206. }
  207. else{
  208. *_setup=setup;
  209. ret=1;
  210. }
  211. }break;
  212. default:{
  213. /*We don't know what this header is.*/
  214. return TH_EBADHEADER;
  215. }break;
  216. }
  217. return ret;
  218. }
  219. /*Decodes one header packet.
  220. This should be called repeatedly with the packets at the beginning of the
  221. stream until it returns 0.*/
  222. int th_decode_headerin(th_info *_info,th_comment *_tc,
  223. th_setup_info **_setup,ogg_packet *_op){
  224. oc_pack_buf opb;
  225. if(_op==NULL)return TH_EBADHEADER;
  226. if(_info==NULL)return TH_EFAULT;
  227. oc_pack_readinit(&opb,_op->packet,_op->bytes);
  228. return oc_dec_headerin(&opb,_info,_tc,_setup,_op);
  229. }
  230. void th_setup_free(th_setup_info *_setup){
  231. if(_setup!=NULL){
  232. oc_setup_clear(_setup);
  233. _ogg_free(_setup);
  234. }
  235. }