FS#8961 - Anti-Aliased Fonts.
[kugel-rb.git] / apps / codecs / libtremor / info.c
blob4273f97dc1c96312a81d669e7ece0e3d8e86e3ef
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 for(i=0;i<vc->comments;i++)
48 if(vc->user_comments[i])_ogg_free(vc->user_comments[i]);
49 if(vc->user_comments)_ogg_free(vc->user_comments);
50 if(vc->comment_lengths)_ogg_free(vc->comment_lengths);
51 if(vc->vendor)_ogg_free(vc->vendor);
52 memset(vc,0,sizeof(*vc));
56 /* blocksize 0 is guaranteed to be short, 1 is guarantted to be long.
57 They may be equal, but short will never ge greater than long */
58 int vorbis_info_blocksize(vorbis_info *vi,int zo){
59 codec_setup_info *ci = (codec_setup_info *)vi->codec_setup;
60 return ci ? ci->blocksizes[zo] : -1;
63 /* used by synthesis, which has a full, alloced vi */
64 void vorbis_info_init(vorbis_info *vi){
65 memset(vi,0,sizeof(*vi));
66 vi->codec_setup=(codec_setup_info *)_ogg_calloc(1,sizeof(codec_setup_info));
69 void vorbis_info_clear(vorbis_info *vi){
70 codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
71 int i;
73 if(ci){
75 for(i=0;i<ci->modes;i++)
76 if(ci->mode_param[i])_ogg_free(ci->mode_param[i]);
78 for(i=0;i<ci->maps;i++) /* unpack does the range checking */
79 if(ci->map_param[i])
80 _mapping_P[ci->map_type[i]]->free_info(ci->map_param[i]);
82 for(i=0;i<ci->floors;i++) /* unpack does the range checking */
83 if(ci->floor_param[i])
84 _floor_P[ci->floor_type[i]]->free_info(ci->floor_param[i]);
86 for(i=0;i<ci->residues;i++) /* unpack does the range checking */
87 if(ci->residue_param[i])
88 _residue_P[ci->residue_type[i]]->free_info(ci->residue_param[i]);
90 for(i=0;i<ci->books;i++){
91 if(ci->book_param[i]){
92 /* knows if the book was not alloced */
93 vorbis_staticbook_destroy(ci->book_param[i]);
95 if(ci->fullbooks)
96 vorbis_book_clear(ci->fullbooks+i);
98 if(ci->fullbooks)
99 _ogg_free(ci->fullbooks);
101 _ogg_free(ci);
104 memset(vi,0,sizeof(*vi));
107 /* Header packing/unpacking ********************************************/
109 static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
110 codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
111 if(!ci)return(OV_EFAULT);
113 vi->version=oggpack_read(opb,32);
114 if(vi->version!=0)return(OV_EVERSION);
116 vi->channels=oggpack_read(opb,8);
117 vi->rate=oggpack_read(opb,32);
119 vi->bitrate_upper=oggpack_read(opb,32);
120 vi->bitrate_nominal=oggpack_read(opb,32);
121 vi->bitrate_lower=oggpack_read(opb,32);
123 ci->blocksizes[0]=1<<oggpack_read(opb,4);
124 ci->blocksizes[1]=1<<oggpack_read(opb,4);
126 if(vi->rate<1)goto err_out;
127 if(vi->channels<1)goto err_out;
128 if(ci->blocksizes[0]<64)goto err_out;
129 if(ci->blocksizes[1]<ci->blocksizes[0])goto err_out;
130 if(ci->blocksizes[1]>8192)goto err_out;
132 if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
134 return(0);
135 err_out:
136 vorbis_info_clear(vi);
137 return(OV_EBADHEADER);
140 static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
141 int vendorlen=oggpack_read(opb,32);
142 if(vendorlen<0)goto err_out;
143 vc->vendor=(char *)_ogg_calloc(vendorlen+1,1);
144 _v_readstring(opb,vc->vendor,vendorlen);
145 vc->comments=0;
146 return(0);
147 err_out:
148 vorbis_comment_clear(vc);
149 return(OV_EBADHEADER);
152 /* all of the real encoding details are here. The modes, books,
153 everything */
154 static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
155 codec_setup_info *ci=(codec_setup_info *)vi->codec_setup;
156 int i;
157 if(!ci)return(OV_EFAULT);
159 /* codebooks */
160 ci->books=oggpack_read(opb,8)+1;
161 /*ci->book_param=_ogg_calloc(ci->books,sizeof(*ci->book_param));*/
162 for(i=0;i<ci->books;i++){
163 ci->book_param[i]=(static_codebook *)_ogg_calloc(1,sizeof(*ci->book_param[i]));
164 if(vorbis_staticbook_unpack(opb,ci->book_param[i]))goto err_out;
167 /* time backend settings */
168 ci->times=oggpack_read(opb,6)+1;
169 /*ci->time_type=_ogg_malloc(ci->times*sizeof(*ci->time_type));*/
170 /*ci->time_param=_ogg_calloc(ci->times,sizeof(void *));*/
171 for(i=0;i<ci->times;i++){
172 ci->time_type[i]=oggpack_read(opb,16);
173 if(ci->time_type[i]<0 || ci->time_type[i]>=VI_TIMEB)goto err_out;
174 /* ci->time_param[i]=_time_P[ci->time_type[i]]->unpack(vi,opb);
175 Vorbis I has no time backend */
176 /*if(!ci->time_param[i])goto err_out;*/
179 /* floor backend settings */
180 ci->floors=oggpack_read(opb,6)+1;
181 /*ci->floor_type=_ogg_malloc(ci->floors*sizeof(*ci->floor_type));*/
182 /*ci->floor_param=_ogg_calloc(ci->floors,sizeof(void *));*/
183 for(i=0;i<ci->floors;i++){
184 ci->floor_type[i]=oggpack_read(opb,16);
185 if(ci->floor_type[i]<0 || ci->floor_type[i]>=VI_FLOORB)goto err_out;
186 ci->floor_param[i]=_floor_P[ci->floor_type[i]]->unpack(vi,opb);
187 if(!ci->floor_param[i])goto err_out;
190 /* residue backend settings */
191 ci->residues=oggpack_read(opb,6)+1;
192 /*ci->residue_type=_ogg_malloc(ci->residues*sizeof(*ci->residue_type));*/
193 /*ci->residue_param=_ogg_calloc(ci->residues,sizeof(void *));*/
194 for(i=0;i<ci->residues;i++){
195 ci->residue_type[i]=oggpack_read(opb,16);
196 if(ci->residue_type[i]<0 || ci->residue_type[i]>=VI_RESB)goto err_out;
197 ci->residue_param[i]=_residue_P[ci->residue_type[i]]->unpack(vi,opb);
198 if(!ci->residue_param[i])goto err_out;
201 /* map backend settings */
202 ci->maps=oggpack_read(opb,6)+1;
203 /*ci->map_type=_ogg_malloc(ci->maps*sizeof(*ci->map_type));*/
204 /*ci->map_param=_ogg_calloc(ci->maps,sizeof(void *));*/
205 for(i=0;i<ci->maps;i++){
206 ci->map_type[i]=oggpack_read(opb,16);
207 if(ci->map_type[i]<0 || ci->map_type[i]>=VI_MAPB)goto err_out;
208 ci->map_param[i]=_mapping_P[ci->map_type[i]]->unpack(vi,opb);
209 if(!ci->map_param[i])goto err_out;
212 /* mode settings */
213 ci->modes=oggpack_read(opb,6)+1;
214 /*vi->mode_param=_ogg_calloc(vi->modes,sizeof(void *));*/
215 for(i=0;i<ci->modes;i++){
216 ci->mode_param[i]=(vorbis_info_mode *)_ogg_calloc(1,sizeof(*ci->mode_param[i]));
217 ci->mode_param[i]->blockflag=oggpack_read(opb,1);
218 ci->mode_param[i]->windowtype=oggpack_read(opb,16);
219 ci->mode_param[i]->transformtype=oggpack_read(opb,16);
220 ci->mode_param[i]->mapping=oggpack_read(opb,8);
222 if(ci->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
223 if(ci->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
224 if(ci->mode_param[i]->mapping>=ci->maps)goto err_out;
227 if(oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
229 return(0);
230 err_out:
231 vorbis_info_clear(vi);
232 return(OV_EBADHEADER);
235 /* Is this packet a vorbis ID header? */
236 int vorbis_synthesis_idheader(ogg_packet *op){
237 oggpack_buffer opb;
238 char buffer[6];
240 if(op){
241 oggpack_readinit(&opb,op->packet);
243 if(!op->b_o_s)
244 return(0); /* Not the initial packet */
246 if(oggpack_read(&opb,8) != 1)
247 return 0; /* not an ID header */
249 memset(buffer,0,6);
250 _v_readstring(&opb,buffer,6);
251 if(memcmp(buffer,"vorbis",6))
252 return 0; /* not vorbis */
254 return 1;
257 return 0;
260 /* The Vorbis header is in three packets; the initial small packet in
261 the first page that identifies basic parameters, a second packet
262 with bitstream comments and a third packet that holds the
263 codebook. */
265 int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
266 oggpack_buffer opb;
268 if(op){
269 oggpack_readinit(&opb,op->packet);
271 /* Which of the three types of header is this? */
272 /* Also verify header-ness, vorbis */
274 char buffer[6];
275 int packtype=oggpack_read(&opb,8);
276 memset(buffer,0,6);
277 _v_readstring(&opb,buffer,6);
278 if(memcmp(buffer,"vorbis",6)){
279 /* not a vorbis header */
280 return(OV_ENOTVORBIS);
282 switch(packtype){
283 case 0x01: /* least significant *bit* is read first */
284 if(!op->b_o_s){
285 /* Not the initial packet */
286 return(OV_EBADHEADER);
288 if(vi->rate!=0){
289 /* previously initialized info header */
290 return(OV_EBADHEADER);
293 return(_vorbis_unpack_info(vi,&opb));
295 case 0x03: /* least significant *bit* is read first */
296 if(vi->rate==0){
297 /* um... we didn't get the initial header */
298 return(OV_EBADHEADER);
301 return(_vorbis_unpack_comment(vc,&opb));
303 case 0x05: /* least significant *bit* is read first */
304 if(vi->rate==0 || vc->vendor==NULL){
305 /* um... we didn;t get the initial header or comments yet */
306 return(OV_EBADHEADER);
309 return(_vorbis_unpack_books(vi,&opb));
311 default:
312 /* Not a valid vorbis header type */
313 return(OV_EBADHEADER);
314 break;
318 return(OV_EBADHEADER);