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
25 #include "ivorbiscodec.h"
26 #include "codec_internal.h"
33 static void _v_readstring(oggpack_buffer
*o
,char *buf
,int bytes
){
35 *buf
++=oggpack_read(o
,8);
39 void vorbis_comment_init(vorbis_comment
*vc
){
40 memset(vc
,0,sizeof(*vc
));
43 /* This is more or less the same as strncasecmp - but that doesn't exist
44 * everywhere, and this is a fairly trivial function, so we include it */
45 static int tagcompare(const char *s1
, const char *s2
, int n
){
48 if(toupper(s1
[c
]) != toupper(s2
[c
]))
55 char *vorbis_comment_query(vorbis_comment
*vc
, char *tag
, int count
){
58 int taglen
= strlen(tag
)+1; /* +1 for the = we append */
59 char *fulltag
= (char *)alloca(taglen
+ 1);
64 for(i
=0;i
<vc
->comments
;i
++){
65 if(!tagcompare(vc
->user_comments
[i
], fulltag
, taglen
)){
67 /* We return a pointer to the data, not a copy */
68 return vc
->user_comments
[i
] + taglen
;
73 return NULL
; /* didn't find anything */
76 int vorbis_comment_query_count(vorbis_comment
*vc
, char *tag
){
78 int taglen
= strlen(tag
)+1; /* +1 for the = we append */
79 char *fulltag
= (char *)alloca(taglen
+1);
83 for(i
=0;i
<vc
->comments
;i
++){
84 if(!tagcompare(vc
->user_comments
[i
], fulltag
, taglen
))
91 void vorbis_comment_clear(vorbis_comment
*vc
){
94 for(i
=0;i
<vc
->comments
;i
++)
95 if(vc
->user_comments
[i
])_ogg_free(vc
->user_comments
[i
]);
96 if(vc
->user_comments
)_ogg_free(vc
->user_comments
);
97 if(vc
->comment_lengths
)_ogg_free(vc
->comment_lengths
);
98 if(vc
->vendor
)_ogg_free(vc
->vendor
);
100 memset(vc
,0,sizeof(*vc
));
103 /* blocksize 0 is guaranteed to be short, 1 is guarantted to be long.
104 They may be equal, but short will never ge greater than long */
105 int vorbis_info_blocksize(vorbis_info
*vi
,int zo
){
106 codec_setup_info
*ci
= (codec_setup_info
*)vi
->codec_setup
;
107 return ci
? ci
->blocksizes
[zo
] : -1;
110 /* used by synthesis, which has a full, alloced vi */
111 void vorbis_info_init(vorbis_info
*vi
){
112 memset(vi
,0,sizeof(*vi
));
113 vi
->codec_setup
=(codec_setup_info
*)_ogg_calloc(1,sizeof(codec_setup_info
));
116 void vorbis_info_clear(vorbis_info
*vi
){
117 codec_setup_info
*ci
=(codec_setup_info
*)vi
->codec_setup
;
122 for(i
=0;i
<ci
->modes
;i
++)
123 if(ci
->mode_param
[i
])_ogg_free(ci
->mode_param
[i
]);
125 for(i
=0;i
<ci
->maps
;i
++) /* unpack does the range checking */
126 _mapping_P
[ci
->map_type
[i
]]->free_info(ci
->map_param
[i
]);
128 for(i
=0;i
<ci
->floors
;i
++) /* unpack does the range checking */
129 _floor_P
[ci
->floor_type
[i
]]->free_info(ci
->floor_param
[i
]);
131 for(i
=0;i
<ci
->residues
;i
++) /* unpack does the range checking */
132 _residue_P
[ci
->residue_type
[i
]]->free_info(ci
->residue_param
[i
]);
134 for(i
=0;i
<ci
->books
;i
++){
135 if(ci
->book_param
[i
]){
136 /* knows if the book was not alloced */
137 vorbis_staticbook_destroy(ci
->book_param
[i
]);
140 vorbis_book_clear(ci
->fullbooks
+i
);
143 _ogg_free(ci
->fullbooks
);
148 memset(vi
,0,sizeof(*vi
));
151 /* Header packing/unpacking ********************************************/
153 static int _vorbis_unpack_info(vorbis_info
*vi
,oggpack_buffer
*opb
){
154 codec_setup_info
*ci
=(codec_setup_info
*)vi
->codec_setup
;
155 if(!ci
)return(OV_EFAULT
);
157 vi
->version
=oggpack_read(opb
,32);
158 if(vi
->version
!=0)return(OV_EVERSION
);
160 vi
->channels
=oggpack_read(opb
,8);
161 vi
->rate
=oggpack_read(opb
,32);
163 vi
->bitrate_upper
=oggpack_read(opb
,32);
164 vi
->bitrate_nominal
=oggpack_read(opb
,32);
165 vi
->bitrate_lower
=oggpack_read(opb
,32);
167 ci
->blocksizes
[0]=1<<oggpack_read(opb
,4);
168 ci
->blocksizes
[1]=1<<oggpack_read(opb
,4);
170 if(vi
->rate
<1)goto err_out
;
171 if(vi
->channels
<1)goto err_out
;
172 if(ci
->blocksizes
[0]<64)goto err_out
;
173 if(ci
->blocksizes
[1]<ci
->blocksizes
[0])goto err_out
;
174 if(ci
->blocksizes
[1]>8192)goto err_out
;
176 if(oggpack_read(opb
,1)!=1)goto err_out
; /* EOP check */
180 vorbis_info_clear(vi
);
181 return(OV_EBADHEADER
);
184 static int _vorbis_unpack_comment(vorbis_comment
*vc
,oggpack_buffer
*opb
){
186 int vendorlen
=oggpack_read(opb
,32);
187 if(vendorlen
<0)goto err_out
;
188 vc
->vendor
=(char *)_ogg_calloc(vendorlen
+1,1);
189 _v_readstring(opb
,vc
->vendor
,vendorlen
);
190 vc
->comments
=oggpack_read(opb
,32);
191 if(vc
->comments
<0)goto err_out
;
192 vc
->user_comments
=(char **)_ogg_calloc(vc
->comments
+1,sizeof(*vc
->user_comments
));
193 vc
->comment_lengths
=(int *)_ogg_calloc(vc
->comments
+1, sizeof(*vc
->comment_lengths
));
195 for(i
=0;i
<vc
->comments
;i
++){
196 int len
=oggpack_read(opb
,32);
197 if(len
<0)goto err_out
;
198 vc
->comment_lengths
[i
]=len
;
199 vc
->user_comments
[i
]=(char *)_ogg_calloc(len
+1,1);
200 _v_readstring(opb
,vc
->user_comments
[i
],len
);
202 if(oggpack_read(opb
,1)!=1)goto err_out
; /* EOP check */
206 vorbis_comment_clear(vc
);
207 return(OV_EBADHEADER
);
210 /* all of the real encoding details are here. The modes, books,
212 static int _vorbis_unpack_books(vorbis_info
*vi
,oggpack_buffer
*opb
){
213 codec_setup_info
*ci
=(codec_setup_info
*)vi
->codec_setup
;
215 if(!ci
)return(OV_EFAULT
);
218 ci
->books
=oggpack_read(opb
,8)+1;
219 /*ci->book_param=_ogg_calloc(ci->books,sizeof(*ci->book_param));*/
220 for(i
=0;i
<ci
->books
;i
++){
221 ci
->book_param
[i
]=(static_codebook
*)_ogg_calloc(1,sizeof(*ci
->book_param
[i
]));
222 if(vorbis_staticbook_unpack(opb
,ci
->book_param
[i
]))goto err_out
;
225 /* time backend settings */
226 ci
->times
=oggpack_read(opb
,6)+1;
227 /*ci->time_type=_ogg_malloc(ci->times*sizeof(*ci->time_type));*/
228 /*ci->time_param=_ogg_calloc(ci->times,sizeof(void *));*/
229 for(i
=0;i
<ci
->times
;i
++){
230 ci
->time_type
[i
]=oggpack_read(opb
,16);
231 if(ci
->time_type
[i
]<0 || ci
->time_type
[i
]>=VI_TIMEB
)goto err_out
;
232 /* ci->time_param[i]=_time_P[ci->time_type[i]]->unpack(vi,opb);
233 Vorbis I has no time backend */
234 /*if(!ci->time_param[i])goto err_out;*/
237 /* floor backend settings */
238 ci
->floors
=oggpack_read(opb
,6)+1;
239 /*ci->floor_type=_ogg_malloc(ci->floors*sizeof(*ci->floor_type));*/
240 /*ci->floor_param=_ogg_calloc(ci->floors,sizeof(void *));*/
241 for(i
=0;i
<ci
->floors
;i
++){
242 ci
->floor_type
[i
]=oggpack_read(opb
,16);
243 if(ci
->floor_type
[i
]<0 || ci
->floor_type
[i
]>=VI_FLOORB
)goto err_out
;
244 ci
->floor_param
[i
]=_floor_P
[ci
->floor_type
[i
]]->unpack(vi
,opb
);
245 if(!ci
->floor_param
[i
])goto err_out
;
248 /* residue backend settings */
249 ci
->residues
=oggpack_read(opb
,6)+1;
250 /*ci->residue_type=_ogg_malloc(ci->residues*sizeof(*ci->residue_type));*/
251 /*ci->residue_param=_ogg_calloc(ci->residues,sizeof(void *));*/
252 for(i
=0;i
<ci
->residues
;i
++){
253 ci
->residue_type
[i
]=oggpack_read(opb
,16);
254 if(ci
->residue_type
[i
]<0 || ci
->residue_type
[i
]>=VI_RESB
)goto err_out
;
255 ci
->residue_param
[i
]=_residue_P
[ci
->residue_type
[i
]]->unpack(vi
,opb
);
256 if(!ci
->residue_param
[i
])goto err_out
;
259 /* map backend settings */
260 ci
->maps
=oggpack_read(opb
,6)+1;
261 /*ci->map_type=_ogg_malloc(ci->maps*sizeof(*ci->map_type));*/
262 /*ci->map_param=_ogg_calloc(ci->maps,sizeof(void *));*/
263 for(i
=0;i
<ci
->maps
;i
++){
264 ci
->map_type
[i
]=oggpack_read(opb
,16);
265 if(ci
->map_type
[i
]<0 || ci
->map_type
[i
]>=VI_MAPB
)goto err_out
;
266 ci
->map_param
[i
]=_mapping_P
[ci
->map_type
[i
]]->unpack(vi
,opb
);
267 if(!ci
->map_param
[i
])goto err_out
;
271 ci
->modes
=oggpack_read(opb
,6)+1;
272 /*vi->mode_param=_ogg_calloc(vi->modes,sizeof(void *));*/
273 for(i
=0;i
<ci
->modes
;i
++){
274 ci
->mode_param
[i
]=(vorbis_info_mode
*)_ogg_calloc(1,sizeof(*ci
->mode_param
[i
]));
275 ci
->mode_param
[i
]->blockflag
=oggpack_read(opb
,1);
276 ci
->mode_param
[i
]->windowtype
=oggpack_read(opb
,16);
277 ci
->mode_param
[i
]->transformtype
=oggpack_read(opb
,16);
278 ci
->mode_param
[i
]->mapping
=oggpack_read(opb
,8);
280 if(ci
->mode_param
[i
]->windowtype
>=VI_WINDOWB
)goto err_out
;
281 if(ci
->mode_param
[i
]->transformtype
>=VI_WINDOWB
)goto err_out
;
282 if(ci
->mode_param
[i
]->mapping
>=ci
->maps
)goto err_out
;
285 if(oggpack_read(opb
,1)!=1)goto err_out
; /* top level EOP check */
289 vorbis_info_clear(vi
);
290 return(OV_EBADHEADER
);
293 /* The Vorbis header is in three packets; the initial small packet in
294 the first page that identifies basic parameters, a second packet
295 with bitstream comments and a third packet that holds the
298 int vorbis_synthesis_headerin(vorbis_info
*vi
,vorbis_comment
*vc
,ogg_packet
*op
){
302 oggpack_readinit(&opb
,op
->packet
);
304 /* Which of the three types of header is this? */
305 /* Also verify header-ness, vorbis */
308 int packtype
=oggpack_read(&opb
,8);
310 _v_readstring(&opb
,buffer
,6);
311 if(memcmp(buffer
,"vorbis",6)){
312 /* not a vorbis header */
313 return(OV_ENOTVORBIS
);
316 case 0x01: /* least significant *bit* is read first */
318 /* Not the initial packet */
319 return(OV_EBADHEADER
);
322 /* previously initialized info header */
323 return(OV_EBADHEADER
);
326 return(_vorbis_unpack_info(vi
,&opb
));
328 case 0x03: /* least significant *bit* is read first */
330 /* um... we didn't get the initial header */
331 return(OV_EBADHEADER
);
334 return(_vorbis_unpack_comment(vc
,&opb
));
336 case 0x05: /* least significant *bit* is read first */
337 if(vi
->rate
==0 || vc
->vendor
==NULL
){
338 /* um... we didn;t get the initial header or comments yet */
339 return(OV_EBADHEADER
);
342 return(_vorbis_unpack_books(vi
,&opb
));
345 /* Not a valid vorbis header type */
346 return(OV_EBADHEADER
);
351 return(OV_EBADHEADER
);