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