libtremor: merge upstream revision 17528-17530, more error checking and bug fixes
[kugel-rb.git] / apps / codecs / libtremor / info.c
blobb21d08de49e9b62aac9164c4b047f4c371d42561
1 /********************************************************************
2 * *
3 * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE. *
4 * *
5 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
6 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
7 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
8 * *
9 * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2003 *
10 * BY THE Xiph.Org FOUNDATION http://www.xiph.org/ *
11 * *
12 ********************************************************************
14 function: maintain the info structure, info <-> header packets
16 ********************************************************************/
18 /* general handling of the header and the vorbis_info structure (and
19 substructures) */
21 #include "config-tremor.h"
22 #include <string.h>
23 #include <ctype.h>
24 #include "ogg.h"
25 #include "ivorbiscodec.h"
26 #include "codec_internal.h"
27 #include "codebook.h"
28 #include "registry.h"
29 #include "window.h"
30 #include "misc.h"
31 #include "os.h"
33 /* helpers */
34 static void _v_readstring(oggpack_buffer *o,char *buf,int bytes){
35 while(bytes--){
36 *buf++=oggpack_read(o,8);
40 void vorbis_comment_init(vorbis_comment *vc){
41 memset(vc,0,sizeof(*vc));
44 void vorbis_comment_clear(vorbis_comment *vc){
45 if(vc){
46 long i;
47 if(vc->user_comments){
48 for(i=0;i<vc->comments;i++)
49 if(vc->user_comments[i])_ogg_free(vc->user_comments[i]);
50 _ogg_free(vc->user_comments);
52 if(vc->comment_lengths)_ogg_free(vc->comment_lengths);
53 if(vc->vendor)_ogg_free(vc->vendor);
54 memset(vc,0,sizeof(*vc));
58 /* blocksize 0 is guaranteed to be short, 1 is guarantted to be long.
59 They may be equal, but short will never ge greater than long */
60 int vorbis_info_blocksize(vorbis_info *vi,int zo){
61 codec_setup_info *ci = (codec_setup_info *)vi->codec_setup;
62 return ci ? ci->blocksizes[zo] : -1;
65 /* used by synthesis, which has a full, alloced vi */
66 void vorbis_info_init(vorbis_info *vi){
67 memset(vi,0,sizeof(*vi));
68 vi->codec_setup=(codec_setup_info *)_ogg_calloc(1,sizeof(codec_setup_info));
71 void vorbis_info_clear(vorbis_info *vi){
72 codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
73 int i;
75 if(ci){
77 for(i=0;i<ci->modes;i++)
78 if(ci->mode_param[i])_ogg_free(ci->mode_param[i]);
80 for(i=0;i<ci->maps;i++) /* unpack does the range checking */
81 if(ci->map_param[i])
82 _mapping_P[ci->map_type[i]]->free_info(ci->map_param[i]);
84 for(i=0;i<ci->floors;i++) /* unpack does the range checking */
85 if(ci->floor_param[i])
86 _floor_P[ci->floor_type[i]]->free_info(ci->floor_param[i]);
88 for(i=0;i<ci->residues;i++) /* unpack does the range checking */
89 if(ci->residue_param[i])
90 _residue_P[ci->residue_type[i]]->free_info(ci->residue_param[i]);
92 for(i=0;i<ci->books;i++){
93 if(ci->book_param[i]){
94 /* knows if the book was not alloced */
95 vorbis_staticbook_destroy(ci->book_param[i]);
97 if(ci->fullbooks)
98 vorbis_book_clear(ci->fullbooks+i);
100 if(ci->fullbooks)
101 _ogg_free(ci->fullbooks);
103 _ogg_free(ci);
106 memset(vi,0,sizeof(*vi));
109 /* Header packing/unpacking ********************************************/
111 static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
112 codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
113 if(!ci)return(OV_EFAULT);
115 vi->version=oggpack_read(opb,32);
116 if(vi->version!=0)return(OV_EVERSION);
118 vi->channels=oggpack_read(opb,8);
119 vi->rate=oggpack_read(opb,32);
121 vi->bitrate_upper=oggpack_read(opb,32);
122 vi->bitrate_nominal=oggpack_read(opb,32);
123 vi->bitrate_lower=oggpack_read(opb,32);
125 ci->blocksizes_nbits[0]=oggpack_read(opb,4);
126 ci->blocksizes_nbits[1]=oggpack_read(opb,4);
127 ci->blocksizes[0]=1<<(ci->blocksizes_nbits[0]);
128 ci->blocksizes[1]=1<<(ci->blocksizes_nbits[1]);
130 if(vi->rate<1)goto err_out;
131 if(vi->channels<1)goto err_out;
132 if(ci->blocksizes[0]<64)goto err_out;
133 if(ci->blocksizes[1]<ci->blocksizes[0])goto err_out;
134 if(ci->blocksizes[1]>8192)goto err_out;
136 if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
138 return(0);
139 err_out:
140 vorbis_info_clear(vi);
141 return(OV_EBADHEADER);
144 static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
145 int vendorlen;
146 vendorlen=oggpack_read(opb,32);
147 if(vendorlen<0)goto err_out;
148 if(vendorlen>opb->storage-oggpack_bytes(opb))goto err_out;
149 vc->vendor=(char *)_ogg_calloc(vendorlen+1,1);
150 if(vc->vendor==NULL)goto err_out;
151 _v_readstring(opb,vc->vendor,vendorlen);
152 vc->comments=0;
153 /* ROCKBOX: the meat of this function was deleted as we don't need it */
154 return(0);
155 err_out:
156 vorbis_comment_clear(vc);
157 return(OV_EBADHEADER);
160 /* all of the real encoding details are here. The modes, books,
161 everything */
162 static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
163 codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
164 int i;
165 if(!ci)return(OV_EFAULT);
167 /* codebooks */
168 ci->books=oggpack_read(opb,8)+1;
169 if(ci->books<=0)goto err_out;
170 for(i=0;i<ci->books;i++){
171 ci->book_param[i]=vorbis_staticbook_unpack(opb);
172 if(!ci->book_param[i])goto err_out;
175 /* time backend settings */
176 ci->times=oggpack_read(opb,6)+1;
177 if(ci->times<=0)goto err_out;
178 for(i=0;i<ci->times;i++){
179 ci->time_type[i]=oggpack_read(opb,16);
180 if(ci->time_type[i]<0 || ci->time_type[i]>=VI_TIMEB)goto err_out;
181 /* ci->time_param[i]=_time_P[ci->time_type[i]]->unpack(vi,opb);
182 Vorbis I has no time backend */
183 /*if(!ci->time_param[i])goto err_out;*/
186 /* floor backend settings */
187 ci->floors=oggpack_read(opb,6)+1;
188 if(ci->floors<=0)goto err_out;
189 for(i=0;i<ci->floors;i++){
190 ci->floor_type[i]=oggpack_read(opb,16);
191 if(ci->floor_type[i]<0 || ci->floor_type[i]>=VI_FLOORB)goto err_out;
192 ci->floor_param[i]=_floor_P[ci->floor_type[i]]->unpack(vi,opb);
193 if(!ci->floor_param[i])goto err_out;
196 /* residue backend settings */
197 ci->residues=oggpack_read(opb,6)+1;
198 if(ci->residues<=0)goto err_out;
199 for(i=0;i<ci->residues;i++){
200 ci->residue_type[i]=oggpack_read(opb,16);
201 if(ci->residue_type[i]<0 || ci->residue_type[i]>=VI_RESB)goto err_out;
202 ci->residue_param[i]=_residue_P[ci->residue_type[i]]->unpack(vi,opb);
203 if(!ci->residue_param[i])goto err_out;
206 /* map backend settings */
207 ci->maps=oggpack_read(opb,6)+1;
208 if(ci->maps<=0)goto err_out;
209 for(i=0;i<ci->maps;i++){
210 ci->map_type[i]=oggpack_read(opb,16);
211 if(ci->map_type[i]<0 || ci->map_type[i]>=VI_MAPB)goto err_out;
212 ci->map_param[i]=_mapping_P[ci->map_type[i]]->unpack(vi,opb);
213 if(!ci->map_param[i])goto err_out;
216 /* mode settings */
217 ci->modes=oggpack_read(opb,6)+1;
218 if(ci->modes<=0)goto err_out;
219 for(i=0;i<ci->modes;i++){
220 ci->mode_param[i]=(vorbis_info_mode *)_ogg_calloc(1,sizeof(*ci->mode_param[i]));
221 ci->mode_param[i]->blockflag=oggpack_read(opb,1);
222 ci->mode_param[i]->windowtype=oggpack_read(opb,16);
223 ci->mode_param[i]->transformtype=oggpack_read(opb,16);
224 ci->mode_param[i]->mapping=oggpack_read(opb,8);
226 if(ci->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
227 if(ci->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
228 if(ci->mode_param[i]->mapping>=ci->maps)goto err_out;
229 if(ci->mode_param[i]->mapping<0)goto err_out;
232 if(oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
234 return(0);
235 err_out:
236 vorbis_info_clear(vi);
237 return(OV_EBADHEADER);
240 /* Is this packet a vorbis ID header? */
241 int vorbis_synthesis_idheader(ogg_packet *op){
242 oggpack_buffer opb;
243 char buffer[6];
245 if(op){
246 oggpack_readinit(&opb,op->packet,op->bytes);
248 if(!op->b_o_s)
249 return(0); /* Not the initial packet */
251 if(oggpack_read(&opb,8) != 1)
252 return 0; /* not an ID header */
254 memset(buffer,0,6);
255 _v_readstring(&opb,buffer,6);
256 if(memcmp(buffer,"vorbis",6))
257 return 0; /* not vorbis */
259 return 1;
262 return 0;
265 /* The Vorbis header is in three packets; the initial small packet in
266 the first page that identifies basic parameters, a second packet
267 with bitstream comments and a third packet that holds the
268 codebook. */
270 int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
271 oggpack_buffer opb;
273 if(op){
274 oggpack_readinit(&opb,op->packet,op->bytes);
276 /* Which of the three types of header is this? */
277 /* Also verify header-ness, vorbis */
279 char buffer[6];
280 int packtype=oggpack_read(&opb,8);
281 memset(buffer,0,6);
282 _v_readstring(&opb,buffer,6);
283 if(memcmp(buffer,"vorbis",6)){
284 /* not a vorbis header */
285 return(OV_ENOTVORBIS);
287 switch(packtype){
288 case 0x01: /* least significant *bit* is read first */
289 if(!op->b_o_s){
290 /* Not the initial packet */
291 return(OV_EBADHEADER);
293 if(vi->rate!=0){
294 /* previously initialized info header */
295 return(OV_EBADHEADER);
298 return(_vorbis_unpack_info(vi,&opb));
300 case 0x03: /* least significant *bit* is read first */
301 if(vi->rate==0){
302 /* um... we didn't get the initial header */
303 return(OV_EBADHEADER);
306 return(_vorbis_unpack_comment(vc,&opb));
308 case 0x05: /* least significant *bit* is read first */
309 if(vi->rate==0 || vc->vendor==NULL){
310 /* um... we didn;t get the initial header or comments yet */
311 return(OV_EBADHEADER);
314 return(_vorbis_unpack_books(vi,&opb));
316 default:
317 /* Not a valid vorbis header type */
318 return(OV_EBADHEADER);
319 break;
323 return(OV_EBADHEADER);