michael@0: /******************************************************************** michael@0: * * michael@0: * THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE. * michael@0: * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * michael@0: * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * michael@0: * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * michael@0: * * michael@0: * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009 * michael@0: * by the Xiph.Org Foundation and contributors http://www.xiph.org/ * michael@0: * * michael@0: ******************************************************************** michael@0: michael@0: function: michael@0: last mod: $Id: decinfo.c 17276 2010-06-05 05:57:05Z tterribe $ michael@0: michael@0: ********************************************************************/ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include "decint.h" michael@0: michael@0: michael@0: michael@0: /*Unpacks a series of octets from a given byte array into the pack buffer. michael@0: No checking is done to ensure the buffer contains enough data. michael@0: _opb: The pack buffer to read the octets from. michael@0: _buf: The byte array to store the unpacked bytes in. michael@0: _len: The number of octets to unpack.*/ michael@0: static void oc_unpack_octets(oc_pack_buf *_opb,char *_buf,size_t _len){ michael@0: while(_len-->0){ michael@0: long val; michael@0: val=oc_pack_read(_opb,8); michael@0: *_buf++=(char)val; michael@0: } michael@0: } michael@0: michael@0: /*Unpacks a 32-bit integer encoded by octets in little-endian form.*/ michael@0: static long oc_unpack_length(oc_pack_buf *_opb){ michael@0: long ret[4]; michael@0: int i; michael@0: for(i=0;i<4;i++)ret[i]=oc_pack_read(_opb,8); michael@0: return ret[0]|ret[1]<<8|ret[2]<<16|ret[3]<<24; michael@0: } michael@0: michael@0: static int oc_info_unpack(oc_pack_buf *_opb,th_info *_info){ michael@0: long val; michael@0: /*Check the codec bitstream version.*/ michael@0: val=oc_pack_read(_opb,8); michael@0: _info->version_major=(unsigned char)val; michael@0: val=oc_pack_read(_opb,8); michael@0: _info->version_minor=(unsigned char)val; michael@0: val=oc_pack_read(_opb,8); michael@0: _info->version_subminor=(unsigned char)val; michael@0: /*verify we can parse this bitstream version. michael@0: We accept earlier minors and all subminors, by spec*/ michael@0: if(_info->version_major>TH_VERSION_MAJOR|| michael@0: _info->version_major==TH_VERSION_MAJOR&& michael@0: _info->version_minor>TH_VERSION_MINOR){ michael@0: return TH_EVERSION; michael@0: } michael@0: /*Read the encoded frame description.*/ michael@0: val=oc_pack_read(_opb,16); michael@0: _info->frame_width=(ogg_uint32_t)val<<4; michael@0: val=oc_pack_read(_opb,16); michael@0: _info->frame_height=(ogg_uint32_t)val<<4; michael@0: val=oc_pack_read(_opb,24); michael@0: _info->pic_width=(ogg_uint32_t)val; michael@0: val=oc_pack_read(_opb,24); michael@0: _info->pic_height=(ogg_uint32_t)val; michael@0: val=oc_pack_read(_opb,8); michael@0: _info->pic_x=(ogg_uint32_t)val; michael@0: val=oc_pack_read(_opb,8); michael@0: _info->pic_y=(ogg_uint32_t)val; michael@0: val=oc_pack_read(_opb,32); michael@0: _info->fps_numerator=(ogg_uint32_t)val; michael@0: val=oc_pack_read(_opb,32); michael@0: _info->fps_denominator=(ogg_uint32_t)val; michael@0: if(_info->frame_width==0||_info->frame_height==0|| michael@0: _info->pic_width+_info->pic_x>_info->frame_width|| michael@0: _info->pic_height+_info->pic_y>_info->frame_height|| michael@0: _info->fps_numerator==0||_info->fps_denominator==0){ michael@0: return TH_EBADHEADER; michael@0: } michael@0: /*Note: The sense of pic_y is inverted in what we pass back to the michael@0: application compared to how it is stored in the bitstream. michael@0: This is because the bitstream uses a right-handed coordinate system, while michael@0: applications expect a left-handed one.*/ michael@0: _info->pic_y=_info->frame_height-_info->pic_height-_info->pic_y; michael@0: val=oc_pack_read(_opb,24); michael@0: _info->aspect_numerator=(ogg_uint32_t)val; michael@0: val=oc_pack_read(_opb,24); michael@0: _info->aspect_denominator=(ogg_uint32_t)val; michael@0: val=oc_pack_read(_opb,8); michael@0: _info->colorspace=(th_colorspace)val; michael@0: val=oc_pack_read(_opb,24); michael@0: _info->target_bitrate=(int)val; michael@0: val=oc_pack_read(_opb,6); michael@0: _info->quality=(int)val; michael@0: val=oc_pack_read(_opb,5); michael@0: _info->keyframe_granule_shift=(int)val; michael@0: val=oc_pack_read(_opb,2); michael@0: _info->pixel_fmt=(th_pixel_fmt)val; michael@0: if(_info->pixel_fmt==TH_PF_RSVD)return TH_EBADHEADER; michael@0: val=oc_pack_read(_opb,3); michael@0: if(val!=0||oc_pack_bytes_left(_opb)<0)return TH_EBADHEADER; michael@0: return 0; michael@0: } michael@0: michael@0: static int oc_comment_unpack(oc_pack_buf *_opb,th_comment *_tc){ michael@0: long len; michael@0: int i; michael@0: /*Read the vendor string.*/ michael@0: len=oc_unpack_length(_opb); michael@0: if(len<0||len>oc_pack_bytes_left(_opb))return TH_EBADHEADER; michael@0: _tc->vendor=_ogg_malloc((size_t)len+1); michael@0: if(_tc->vendor==NULL)return TH_EFAULT; michael@0: oc_unpack_octets(_opb,_tc->vendor,len); michael@0: _tc->vendor[len]='\0'; michael@0: /*Read the user comments.*/ michael@0: _tc->comments=(int)oc_unpack_length(_opb); michael@0: len=_tc->comments; michael@0: if(len<0||len>(LONG_MAX>>2)||len<<2>oc_pack_bytes_left(_opb)){ michael@0: _tc->comments=0; michael@0: return TH_EBADHEADER; michael@0: } michael@0: _tc->comment_lengths=(int *)_ogg_malloc( michael@0: _tc->comments*sizeof(_tc->comment_lengths[0])); michael@0: _tc->user_comments=(char **)_ogg_malloc( michael@0: _tc->comments*sizeof(_tc->user_comments[0])); michael@0: if(_tc->comment_lengths==NULL||_tc->user_comments==NULL){ michael@0: _tc->comments=0; michael@0: return TH_EFAULT; michael@0: } michael@0: for(i=0;i<_tc->comments;i++){ michael@0: len=oc_unpack_length(_opb); michael@0: if(len<0||len>oc_pack_bytes_left(_opb)){ michael@0: _tc->comments=i; michael@0: return TH_EBADHEADER; michael@0: } michael@0: _tc->comment_lengths[i]=len; michael@0: _tc->user_comments[i]=_ogg_malloc((size_t)len+1); michael@0: if(_tc->user_comments[i]==NULL){ michael@0: _tc->comments=i; michael@0: return TH_EFAULT; michael@0: } michael@0: oc_unpack_octets(_opb,_tc->user_comments[i],len); michael@0: _tc->user_comments[i][len]='\0'; michael@0: } michael@0: return oc_pack_bytes_left(_opb)<0?TH_EBADHEADER:0; michael@0: } michael@0: michael@0: static int oc_setup_unpack(oc_pack_buf *_opb,th_setup_info *_setup){ michael@0: int ret; michael@0: /*Read the quantizer tables.*/ michael@0: ret=oc_quant_params_unpack(_opb,&_setup->qinfo); michael@0: if(ret<0)return ret; michael@0: /*Read the Huffman trees.*/ michael@0: return oc_huff_trees_unpack(_opb,_setup->huff_tables); michael@0: } michael@0: michael@0: static void oc_setup_clear(th_setup_info *_setup){ michael@0: oc_quant_params_clear(&_setup->qinfo); michael@0: oc_huff_trees_clear(_setup->huff_tables); michael@0: } michael@0: michael@0: static int oc_dec_headerin(oc_pack_buf *_opb,th_info *_info, michael@0: th_comment *_tc,th_setup_info **_setup,ogg_packet *_op){ michael@0: char buffer[6]; michael@0: long val; michael@0: int packtype; michael@0: int ret; michael@0: val=oc_pack_read(_opb,8); michael@0: packtype=(int)val; michael@0: /*If we're at a data packet and we have received all three headers, we're michael@0: done.*/ michael@0: if(!(packtype&0x80)&&_info->frame_width>0&&_tc->vendor!=NULL&&*_setup!=NULL){ michael@0: return 0; michael@0: } michael@0: /*Check the codec string.*/ michael@0: oc_unpack_octets(_opb,buffer,6); michael@0: if(memcmp(buffer,"theora",6)!=0)return TH_ENOTFORMAT; michael@0: switch(packtype){ michael@0: /*Codec info header.*/ michael@0: case 0x80:{ michael@0: /*This should be the first packet, and we should not already be michael@0: initialized.*/ michael@0: if(!_op->b_o_s||_info->frame_width>0)return TH_EBADHEADER; michael@0: ret=oc_info_unpack(_opb,_info); michael@0: if(ret<0)th_info_clear(_info); michael@0: else ret=3; michael@0: }break; michael@0: /*Comment header.*/ michael@0: case 0x81:{ michael@0: if(_tc==NULL)return TH_EFAULT; michael@0: /*We shoud have already decoded the info header, and should not yet have michael@0: decoded the comment header.*/ michael@0: if(_info->frame_width==0||_tc->vendor!=NULL)return TH_EBADHEADER; michael@0: ret=oc_comment_unpack(_opb,_tc); michael@0: if(ret<0)th_comment_clear(_tc); michael@0: else ret=2; michael@0: }break; michael@0: /*Codec setup header.*/ michael@0: case 0x82:{ michael@0: oc_setup_info *setup; michael@0: if(_tc==NULL||_setup==NULL)return TH_EFAULT; michael@0: /*We should have already decoded the info header and the comment header, michael@0: and should not yet have decoded the setup header.*/ michael@0: if(_info->frame_width==0||_tc->vendor==NULL||*_setup!=NULL){ michael@0: return TH_EBADHEADER; michael@0: } michael@0: setup=(oc_setup_info *)_ogg_calloc(1,sizeof(*setup)); michael@0: if(setup==NULL)return TH_EFAULT; michael@0: ret=oc_setup_unpack(_opb,setup); michael@0: if(ret<0){ michael@0: oc_setup_clear(setup); michael@0: _ogg_free(setup); michael@0: } michael@0: else{ michael@0: *_setup=setup; michael@0: ret=1; michael@0: } michael@0: }break; michael@0: default:{ michael@0: /*We don't know what this header is.*/ michael@0: return TH_EBADHEADER; michael@0: }break; michael@0: } michael@0: return ret; michael@0: } michael@0: michael@0: michael@0: /*Decodes one header packet. michael@0: This should be called repeatedly with the packets at the beginning of the michael@0: stream until it returns 0.*/ michael@0: int th_decode_headerin(th_info *_info,th_comment *_tc, michael@0: th_setup_info **_setup,ogg_packet *_op){ michael@0: oc_pack_buf opb; michael@0: if(_op==NULL)return TH_EBADHEADER; michael@0: if(_info==NULL)return TH_EFAULT; michael@0: oc_pack_readinit(&opb,_op->packet,_op->bytes); michael@0: return oc_dec_headerin(&opb,_info,_tc,_setup,_op); michael@0: } michael@0: michael@0: void th_setup_free(th_setup_info *_setup){ michael@0: if(_setup!=NULL){ michael@0: oc_setup_clear(_setup); michael@0: _ogg_free(_setup); michael@0: } michael@0: }