decinfo.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 16503 2009-08-22 18:14:02Z giles $
  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. for(i=0;i<_tc->comments;i++){
  123. len=oc_unpack_length(_opb);
  124. if(len<0||len>oc_pack_bytes_left(_opb)){
  125. _tc->comments=i;
  126. return TH_EBADHEADER;
  127. }
  128. _tc->comment_lengths[i]=len;
  129. _tc->user_comments[i]=_ogg_malloc((size_t)len+1);
  130. if(_tc->user_comments[i]==NULL){
  131. _tc->comments=i;
  132. return TH_EFAULT;
  133. }
  134. oc_unpack_octets(_opb,_tc->user_comments[i],len);
  135. _tc->user_comments[i][len]='\0';
  136. }
  137. return oc_pack_bytes_left(_opb)<0?TH_EBADHEADER:0;
  138. }
  139. static int oc_setup_unpack(oc_pack_buf *_opb,th_setup_info *_setup){
  140. int ret;
  141. /*Read the quantizer tables.*/
  142. ret=oc_quant_params_unpack(_opb,&_setup->qinfo);
  143. if(ret<0)return ret;
  144. /*Read the Huffman trees.*/
  145. return oc_huff_trees_unpack(_opb,_setup->huff_tables);
  146. }
  147. static void oc_setup_clear(th_setup_info *_setup){
  148. oc_quant_params_clear(&_setup->qinfo);
  149. oc_huff_trees_clear(_setup->huff_tables);
  150. }
  151. static int oc_dec_headerin(oc_pack_buf *_opb,th_info *_info,
  152. th_comment *_tc,th_setup_info **_setup,ogg_packet *_op){
  153. char buffer[6];
  154. long val;
  155. int packtype;
  156. int ret;
  157. val=oc_pack_read(_opb,8);
  158. packtype=(int)val;
  159. /*If we're at a data packet and we have received all three headers, we're
  160. done.*/
  161. if(!(packtype&0x80)&&_info->frame_width>0&&_tc->vendor!=NULL&&*_setup!=NULL){
  162. return 0;
  163. }
  164. /*Check the codec string.*/
  165. oc_unpack_octets(_opb,buffer,6);
  166. if(memcmp(buffer,"theora",6)!=0)return TH_ENOTFORMAT;
  167. switch(packtype){
  168. /*Codec info header.*/
  169. case 0x80:{
  170. /*This should be the first packet, and we should not already be
  171. initialized.*/
  172. if(!_op->b_o_s||_info->frame_width>0)return TH_EBADHEADER;
  173. ret=oc_info_unpack(_opb,_info);
  174. if(ret<0)th_info_clear(_info);
  175. else ret=3;
  176. }break;
  177. /*Comment header.*/
  178. case 0x81:{
  179. if(_tc==NULL)return TH_EFAULT;
  180. /*We shoud have already decoded the info header, and should not yet have
  181. decoded the comment header.*/
  182. if(_info->frame_width==0||_tc->vendor!=NULL)return TH_EBADHEADER;
  183. ret=oc_comment_unpack(_opb,_tc);
  184. if(ret<0)th_comment_clear(_tc);
  185. else ret=2;
  186. }break;
  187. /*Codec setup header.*/
  188. case 0x82:{
  189. oc_setup_info *setup;
  190. if(_tc==NULL||_setup==NULL)return TH_EFAULT;
  191. /*We should have already decoded the info header and the comment header,
  192. and should not yet have decoded the setup header.*/
  193. if(_info->frame_width==0||_tc->vendor==NULL||*_setup!=NULL){
  194. return TH_EBADHEADER;
  195. }
  196. setup=(oc_setup_info *)_ogg_calloc(1,sizeof(*setup));
  197. if(setup==NULL)return TH_EFAULT;
  198. ret=oc_setup_unpack(_opb,setup);
  199. if(ret<0){
  200. oc_setup_clear(setup);
  201. _ogg_free(setup);
  202. }
  203. else{
  204. *_setup=setup;
  205. ret=1;
  206. }
  207. }break;
  208. default:{
  209. /*We don't know what this header is.*/
  210. return TH_EBADHEADER;
  211. }break;
  212. }
  213. return ret;
  214. }
  215. /*Decodes one header packet.
  216. This should be called repeatedly with the packets at the beginning of the
  217. stream until it returns 0.*/
  218. int th_decode_headerin(th_info *_info,th_comment *_tc,
  219. th_setup_info **_setup,ogg_packet *_op){
  220. oc_pack_buf opb;
  221. if(_op==NULL)return TH_EBADHEADER;
  222. if(_info==NULL)return TH_EFAULT;
  223. oc_pack_readinit(&opb,_op->packet,_op->bytes);
  224. return oc_dec_headerin(&opb,_info,_tc,_setup,_op);
  225. }
  226. void th_setup_free(th_setup_info *_setup){
  227. if(_setup!=NULL){
  228. oc_setup_clear(_setup);
  229. _ogg_free(_setup);
  230. }
  231. }