1 /********************************************************************
3 * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE. *
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. *
9 * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2003 *
10 * BY THE Xiph.Org FOUNDATION http://www.xiph.org/ *
12 ********************************************************************
14 function: maintain the info structure, info <-> header packets
16 ********************************************************************/
18 /* general handling of the header and the vorbis_info structure (and
21 #include "config-tremor.h"
25 #include "ivorbiscodec.h"
26 #include "codec_internal.h"
34 static void _v_readstring(oggpack_buffer
*o
,char *buf
,int 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
){
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
;
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 */
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
]);
96 vorbis_book_clear(ci
->fullbooks
+i
);
99 _ogg_free(ci
->fullbooks
);
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_nbits
[0]=oggpack_read(opb
,4);
124 ci
->blocksizes_nbits
[1]=oggpack_read(opb
,4);
125 ci
->blocksizes
[0]=1<<(ci
->blocksizes_nbits
[0]);
126 ci
->blocksizes
[1]=1<<(ci
->blocksizes_nbits
[1]);
128 if(vi
->rate
<1)goto err_out
;
129 if(vi
->channels
<1)goto err_out
;
130 if(ci
->blocksizes
[0]<64)goto err_out
;
131 if(ci
->blocksizes
[1]<ci
->blocksizes
[0])goto err_out
;
132 if(ci
->blocksizes
[1]>8192)goto err_out
;
134 if(oggpack_read(opb
,1)!=1)goto err_out
; /* EOP check */
138 vorbis_info_clear(vi
);
139 return(OV_EBADHEADER
);
142 static int _vorbis_unpack_comment(vorbis_comment
*vc
,oggpack_buffer
*opb
){
144 vendorlen
=oggpack_read(opb
,32);
145 if(vendorlen
<0)goto err_out
;
146 if(vendorlen
>opb
->storage
-oggpack_bytes(opb
))goto err_out
;
147 vc
->vendor
=(char *)_ogg_calloc(vendorlen
+1,1);
148 if(vc
->vendor
==NULL
)goto err_out
;
149 _v_readstring(opb
,vc
->vendor
,vendorlen
);
151 /* ROCKBOX: the meat of this function was deleted as we don't need it */
154 vorbis_comment_clear(vc
);
155 return(OV_EBADHEADER
);
158 /* all of the real encoding details are here. The modes, books,
160 static int _vorbis_unpack_books(vorbis_info
*vi
,oggpack_buffer
*opb
){
161 codec_setup_info
*ci
=(codec_setup_info
*)vi
->codec_setup
;
163 if(!ci
)return(OV_EFAULT
);
166 ci
->books
=oggpack_read(opb
,8)+1;
167 /*ci->book_param=_ogg_calloc(ci->books,sizeof(*ci->book_param));*/
168 for(i
=0;i
<ci
->books
;i
++){
169 ci
->book_param
[i
]=(static_codebook
*)_ogg_calloc(1,sizeof(*ci
->book_param
[i
]));
170 if(vorbis_staticbook_unpack(opb
,ci
->book_param
[i
]))goto err_out
;
173 /* time backend settings */
174 ci
->times
=oggpack_read(opb
,6)+1;
175 /*ci->time_type=_ogg_malloc(ci->times*sizeof(*ci->time_type));*/
176 /*ci->time_param=_ogg_calloc(ci->times,sizeof(void *));*/
177 for(i
=0;i
<ci
->times
;i
++){
178 ci
->time_type
[i
]=oggpack_read(opb
,16);
179 if(ci
->time_type
[i
]<0 || ci
->time_type
[i
]>=VI_TIMEB
)goto err_out
;
180 /* ci->time_param[i]=_time_P[ci->time_type[i]]->unpack(vi,opb);
181 Vorbis I has no time backend */
182 /*if(!ci->time_param[i])goto err_out;*/
185 /* floor backend settings */
186 ci
->floors
=oggpack_read(opb
,6)+1;
187 /*ci->floor_type=_ogg_malloc(ci->floors*sizeof(*ci->floor_type));*/
188 /*ci->floor_param=_ogg_calloc(ci->floors,sizeof(void *));*/
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 /*ci->residue_type=_ogg_malloc(ci->residues*sizeof(*ci->residue_type));*/
199 /*ci->residue_param=_ogg_calloc(ci->residues,sizeof(void *));*/
200 for(i
=0;i
<ci
->residues
;i
++){
201 ci
->residue_type
[i
]=oggpack_read(opb
,16);
202 if(ci
->residue_type
[i
]<0 || ci
->residue_type
[i
]>=VI_RESB
)goto err_out
;
203 ci
->residue_param
[i
]=_residue_P
[ci
->residue_type
[i
]]->unpack(vi
,opb
);
204 if(!ci
->residue_param
[i
])goto err_out
;
207 /* map backend settings */
208 ci
->maps
=oggpack_read(opb
,6)+1;
209 /*ci->map_type=_ogg_malloc(ci->maps*sizeof(*ci->map_type));*/
210 /*ci->map_param=_ogg_calloc(ci->maps,sizeof(void *));*/
211 for(i
=0;i
<ci
->maps
;i
++){
212 ci
->map_type
[i
]=oggpack_read(opb
,16);
213 if(ci
->map_type
[i
]<0 || ci
->map_type
[i
]>=VI_MAPB
)goto err_out
;
214 ci
->map_param
[i
]=_mapping_P
[ci
->map_type
[i
]]->unpack(vi
,opb
);
215 if(!ci
->map_param
[i
])goto err_out
;
219 ci
->modes
=oggpack_read(opb
,6)+1;
220 /*vi->mode_param=_ogg_calloc(vi->modes,sizeof(void *));*/
221 for(i
=0;i
<ci
->modes
;i
++){
222 ci
->mode_param
[i
]=(vorbis_info_mode
*)_ogg_calloc(1,sizeof(*ci
->mode_param
[i
]));
223 ci
->mode_param
[i
]->blockflag
=oggpack_read(opb
,1);
224 ci
->mode_param
[i
]->windowtype
=oggpack_read(opb
,16);
225 ci
->mode_param
[i
]->transformtype
=oggpack_read(opb
,16);
226 ci
->mode_param
[i
]->mapping
=oggpack_read(opb
,8);
228 if(ci
->mode_param
[i
]->windowtype
>=VI_WINDOWB
)goto err_out
;
229 if(ci
->mode_param
[i
]->transformtype
>=VI_WINDOWB
)goto err_out
;
230 if(ci
->mode_param
[i
]->mapping
>=ci
->maps
)goto err_out
;
233 if(oggpack_read(opb
,1)!=1)goto err_out
; /* top level EOP check */
237 vorbis_info_clear(vi
);
238 return(OV_EBADHEADER
);
241 /* Is this packet a vorbis ID header? */
242 int vorbis_synthesis_idheader(ogg_packet
*op
){
247 oggpack_readinit(&opb
,op
->packet
,op
->bytes
);
250 return(0); /* Not the initial packet */
252 if(oggpack_read(&opb
,8) != 1)
253 return 0; /* not an ID header */
256 _v_readstring(&opb
,buffer
,6);
257 if(memcmp(buffer
,"vorbis",6))
258 return 0; /* not vorbis */
266 /* The Vorbis header is in three packets; the initial small packet in
267 the first page that identifies basic parameters, a second packet
268 with bitstream comments and a third packet that holds the
271 int vorbis_synthesis_headerin(vorbis_info
*vi
,vorbis_comment
*vc
,ogg_packet
*op
){
275 oggpack_readinit(&opb
,op
->packet
,op
->bytes
);
277 /* Which of the three types of header is this? */
278 /* Also verify header-ness, vorbis */
281 int packtype
=oggpack_read(&opb
,8);
283 _v_readstring(&opb
,buffer
,6);
284 if(memcmp(buffer
,"vorbis",6)){
285 /* not a vorbis header */
286 return(OV_ENOTVORBIS
);
289 case 0x01: /* least significant *bit* is read first */
291 /* Not the initial packet */
292 return(OV_EBADHEADER
);
295 /* previously initialized info header */
296 return(OV_EBADHEADER
);
299 return(_vorbis_unpack_info(vi
,&opb
));
301 case 0x03: /* least significant *bit* is read first */
303 /* um... we didn't get the initial header */
304 return(OV_EBADHEADER
);
307 return(_vorbis_unpack_comment(vc
,&opb
));
309 case 0x05: /* least significant *bit* is read first */
310 if(vi
->rate
==0 || vc
->vendor
==NULL
){
311 /* um... we didn;t get the initial header or comments yet */
312 return(OV_EBADHEADER
);
315 return(_vorbis_unpack_books(vi
,&opb
));
318 /* Not a valid vorbis header type */
319 return(OV_EBADHEADER
);
324 return(OV_EBADHEADER
);