1 /********************************************************************
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. *
8 * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009 *
9 * by the Xiph.Org Foundation and contributors http://www.xiph.org/ *
11 ********************************************************************
14 last mod: $Id: decinfo.c 16503 2009-08-22 18:14:02Z giles $
16 ********************************************************************/
25 /*Unpacks a series of octets from a given byte array into the pack buffer.
26 No checking is done to ensure the buffer contains enough data.
27 _opb: The pack buffer to read the octets from.
28 _buf: The byte array to store the unpacked bytes in.
29 _len: The number of octets to unpack.*/
30 static void oc_unpack_octets(oc_pack_buf *_opb,char *_buf,size_t _len){
33 val=oc_pack_read(_opb,8);
38 /*Unpacks a 32-bit integer encoded by octets in little-endian form.*/
39 static long oc_unpack_length(oc_pack_buf *_opb){
42 for(i=0;i<4;i++)ret[i]=oc_pack_read(_opb,8);
43 return ret[0]|ret[1]<<8|ret[2]<<16|ret[3]<<24;
46 static int oc_info_unpack(oc_pack_buf *_opb,th_info *_info){
48 /*Check the codec bitstream version.*/
49 val=oc_pack_read(_opb,8);
50 _info->version_major=(unsigned char)val;
51 val=oc_pack_read(_opb,8);
52 _info->version_minor=(unsigned char)val;
53 val=oc_pack_read(_opb,8);
54 _info->version_subminor=(unsigned char)val;
55 /*verify we can parse this bitstream version.
56 We accept earlier minors and all subminors, by spec*/
57 if(_info->version_major>TH_VERSION_MAJOR||
58 _info->version_major==TH_VERSION_MAJOR&&
59 _info->version_minor>TH_VERSION_MINOR){
62 /*Read the encoded frame description.*/
63 val=oc_pack_read(_opb,16);
64 _info->frame_width=(ogg_uint32_t)val<<4;
65 val=oc_pack_read(_opb,16);
66 _info->frame_height=(ogg_uint32_t)val<<4;
67 val=oc_pack_read(_opb,24);
68 _info->pic_width=(ogg_uint32_t)val;
69 val=oc_pack_read(_opb,24);
70 _info->pic_height=(ogg_uint32_t)val;
71 val=oc_pack_read(_opb,8);
72 _info->pic_x=(ogg_uint32_t)val;
73 val=oc_pack_read(_opb,8);
74 _info->pic_y=(ogg_uint32_t)val;
75 val=oc_pack_read(_opb,32);
76 _info->fps_numerator=(ogg_uint32_t)val;
77 val=oc_pack_read(_opb,32);
78 _info->fps_denominator=(ogg_uint32_t)val;
79 if(_info->frame_width==0||_info->frame_height==0||
80 _info->pic_width+_info->pic_x>_info->frame_width||
81 _info->pic_height+_info->pic_y>_info->frame_height||
82 _info->fps_numerator==0||_info->fps_denominator==0){
85 /*Note: The sense of pic_y is inverted in what we pass back to the
86 application compared to how it is stored in the bitstream.
87 This is because the bitstream uses a right-handed coordinate system, while
88 applications expect a left-handed one.*/
89 _info->pic_y=_info->frame_height-_info->pic_height-_info->pic_y;
90 val=oc_pack_read(_opb,24);
91 _info->aspect_numerator=(ogg_uint32_t)val;
92 val=oc_pack_read(_opb,24);
93 _info->aspect_denominator=(ogg_uint32_t)val;
94 val=oc_pack_read(_opb,8);
95 _info->colorspace=(th_colorspace)val;
96 val=oc_pack_read(_opb,24);
97 _info->target_bitrate=(int)val;
98 val=oc_pack_read(_opb,6);
99 _info->quality=(int)val;
100 val=oc_pack_read(_opb,5);
101 _info->keyframe_granule_shift=(int)val;
102 val=oc_pack_read(_opb,2);
103 _info->pixel_fmt=(th_pixel_fmt)val;
104 if(_info->pixel_fmt==TH_PF_RSVD)return TH_EBADHEADER;
105 val=oc_pack_read(_opb,3);
106 if(val!=0||oc_pack_bytes_left(_opb)<0)return TH_EBADHEADER;
110 static int oc_comment_unpack(oc_pack_buf *_opb,th_comment *_tc){
113 /*Read the vendor string.*/
114 len=oc_unpack_length(_opb);
115 if(len<0||len>oc_pack_bytes_left(_opb))return TH_EBADHEADER;
116 _tc->vendor=_ogg_malloc((size_t)len+1);
117 if(_tc->vendor==NULL)return TH_EFAULT;
118 oc_unpack_octets(_opb,_tc->vendor,len);
119 _tc->vendor[len]='\0';
120 /*Read the user comments.*/
121 _tc->comments=(int)oc_unpack_length(_opb);
123 if(len<0||len>(LONG_MAX>>2)||len<<2>oc_pack_bytes_left(_opb)){
125 return TH_EBADHEADER;
127 _tc->comment_lengths=(int *)_ogg_malloc(
128 _tc->comments*sizeof(_tc->comment_lengths[0]));
129 _tc->user_comments=(char **)_ogg_malloc(
130 _tc->comments*sizeof(_tc->user_comments[0]));
131 for(i=0;i<_tc->comments;i++){
132 len=oc_unpack_length(_opb);
133 if(len<0||len>oc_pack_bytes_left(_opb)){
135 return TH_EBADHEADER;
137 _tc->comment_lengths[i]=len;
138 _tc->user_comments[i]=_ogg_malloc((size_t)len+1);
139 if(_tc->user_comments[i]==NULL){
143 oc_unpack_octets(_opb,_tc->user_comments[i],len);
144 _tc->user_comments[i][len]='\0';
146 return oc_pack_bytes_left(_opb)<0?TH_EBADHEADER:0;
149 static int oc_setup_unpack(oc_pack_buf *_opb,th_setup_info *_setup){
151 /*Read the quantizer tables.*/
152 ret=oc_quant_params_unpack(_opb,&_setup->qinfo);
154 /*Read the Huffman trees.*/
155 return oc_huff_trees_unpack(_opb,_setup->huff_tables);
158 static void oc_setup_clear(th_setup_info *_setup){
159 oc_quant_params_clear(&_setup->qinfo);
160 oc_huff_trees_clear(_setup->huff_tables);
163 static int oc_dec_headerin(oc_pack_buf *_opb,th_info *_info,
164 th_comment *_tc,th_setup_info **_setup,ogg_packet *_op){
169 val=oc_pack_read(_opb,8);
171 /*If we're at a data packet and we have received all three headers, we're
173 if(!(packtype&0x80)&&_info->frame_width>0&&_tc->vendor!=NULL&&*_setup!=NULL){
176 /*Check the codec string.*/
177 oc_unpack_octets(_opb,buffer,6);
178 if(memcmp(buffer,"theora",6)!=0)return TH_ENOTFORMAT;
180 /*Codec info header.*/
182 /*This should be the first packet, and we should not already be
184 if(!_op->b_o_s||_info->frame_width>0)return TH_EBADHEADER;
185 ret=oc_info_unpack(_opb,_info);
186 if(ret<0)th_info_clear(_info);
191 if(_tc==NULL)return TH_EFAULT;
192 /*We shoud have already decoded the info header, and should not yet have
193 decoded the comment header.*/
194 if(_info->frame_width==0||_tc->vendor!=NULL)return TH_EBADHEADER;
195 ret=oc_comment_unpack(_opb,_tc);
196 if(ret<0)th_comment_clear(_tc);
199 /*Codec setup header.*/
201 oc_setup_info *setup;
202 if(_tc==NULL||_setup==NULL)return TH_EFAULT;
203 /*We should have already decoded the info header and the comment header,
204 and should not yet have decoded the setup header.*/
205 if(_info->frame_width==0||_tc->vendor==NULL||*_setup!=NULL){
206 return TH_EBADHEADER;
208 setup=(oc_setup_info *)_ogg_calloc(1,sizeof(*setup));
209 if(setup==NULL)return TH_EFAULT;
210 ret=oc_setup_unpack(_opb,setup);
212 oc_setup_clear(setup);
221 /*We don't know what this header is.*/
222 return TH_EBADHEADER;
229 /*Decodes one header packet.
230 This should be called repeatedly with the packets at the beginning of the
231 stream until it returns 0.*/
232 int th_decode_headerin(th_info *_info,th_comment *_tc,
233 th_setup_info **_setup,ogg_packet *_op){
235 if(_op==NULL)return TH_EBADHEADER;
236 if(_info==NULL)return TH_EFAULT;
237 oc_pack_readinit(&opb,_op->packet,_op->bytes);
238 return oc_dec_headerin(&opb,_info,_tc,_setup,_op);
241 void th_setup_free(th_setup_info *_setup){
243 oc_setup_clear(_setup);