1 /********************************************************************
3 * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
5 * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *
6 * PLEASE READ THESE TERMS DISTRIBUTING. *
8 * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-2000 *
9 * by Monty <monty@xiph.org> and The XIPHOPHORUS Company *
10 * http://www.xiph.org/ *
12 ********************************************************************
14 function: maintain the info structure, info <-> header packets
15 last mod: $Id: info.c,v 1.19 2000/01/28 14:31:26 xiphmont Exp $
17 ********************************************************************/
19 /* general handling of the header and the vorbis_info structure (and
24 #include "vorbis/codec.h"
25 #include "vorbis/backends.h"
27 #include "bookinternal.h"
33 static int ilog2(unsigned int v){
42 static void _v_writestring(oggpack_buffer *o,char *s){
44 _oggpack_write(o,*s++,8);
48 static void _v_readstring(oggpack_buffer *o,char *buf,int bytes){
50 *buf++=_oggpack_read(o,8);
54 void vorbis_comment_init(vorbis_comment *vc){
55 memset(vc,0,sizeof(vorbis_comment));
58 void vorbis_comment_add(vorbis_comment *vc,char *comment){
59 vc->user_comments=realloc(vc->user_comments,
60 (vc->comments+2)*sizeof(char *));
61 vc->user_comments[vc->comments]=strdup(comment);
63 vc->user_comments[vc->comments]=NULL;
66 void vorbis_comment_clear(vorbis_comment *vc){
69 for(i=0;i<vc->comments;i++)
70 if(vc->user_comments[i])free(vc->user_comments[i]);
71 if(vc->user_comments)free(vc->user_comments);
72 if(vc->vendor)free(vc->vendor);
74 memset(vc,0,sizeof(vorbis_comment));
77 /* used by synthesis, which has a full, alloced vi */
78 void vorbis_info_init(vorbis_info *vi){
79 memset(vi,0,sizeof(vorbis_info));
82 void vorbis_info_clear(vorbis_info *vi){
85 for(i=0;i<vi->modes;i++)
86 if(vi->mode_param[i])free(vi->mode_param[i]);
87 /*if(vi->mode_param)free(vi->mode_param);*/
89 for(i=0;i<vi->maps;i++) /* unpack does the range checking */
90 _mapping_P[i]->free_info(vi->map_param[i]);
91 /*if(vi->map_param)free(vi->map_param);*/
93 for(i=0;i<vi->times;i++) /* unpack does the range checking */
94 _time_P[i]->free_info(vi->time_param[i]);
95 /*if(vi->time_param)free(vi->time_param);*/
97 for(i=0;i<vi->floors;i++) /* unpack does the range checking */
98 _floor_P[i]->free_info(vi->floor_param[i]);
99 /*if(vi->floor_param)free(vi->floor_param);*/
101 for(i=0;i<vi->residues;i++) /* unpack does the range checking */
102 _residue_P[i]->free_info(vi->residue_param[i]);
103 /*if(vi->residue_param)free(vi->residue_param);*/
105 /* the static codebooks *are* freed if you call info_clear, because
106 decode side does alloc a 'static' codebook. Calling clear on the
107 full codebook does not clear the static codebook (that's our
109 for(i=0;i<vi->books;i++){
110 /* just in case the decoder pre-cleared to save space */
111 if(vi->book_param[i]){
112 vorbis_staticbook_clear(vi->book_param[i]);
113 free(vi->book_param[i]);
116 /*if(vi->book_param)free(vi->book_param);*/
118 for(i=0;i<vi->psys;i++)
119 _vi_psy_free(vi->psy_param[i]);
120 /*if(vi->psy_param)free(vi->psy_param);*/
122 memset(vi,0,sizeof(vorbis_info));
125 /* Header packing/unpacking ********************************************/
127 static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
128 vi->version=_oggpack_read(opb,32);
129 if(vi->version!=0)return(-1);
131 vi->channels=_oggpack_read(opb,8);
132 vi->rate=_oggpack_read(opb,32);
134 vi->bitrate_upper=_oggpack_read(opb,32);
135 vi->bitrate_nominal=_oggpack_read(opb,32);
136 vi->bitrate_lower=_oggpack_read(opb,32);
138 vi->blocksizes[0]=1<<_oggpack_read(opb,4);
139 vi->blocksizes[1]=1<<_oggpack_read(opb,4);
141 if(vi->rate<1)goto err_out;
142 if(vi->channels<1)goto err_out;
143 if(vi->blocksizes[0]<8)goto err_out;
144 if(vi->blocksizes[1]<vi->blocksizes[0])goto err_out;
146 if(_oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
150 vorbis_info_clear(vi);
154 static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
156 int vendorlen=_oggpack_read(opb,32);
157 if(vendorlen<0)goto err_out;
158 vc->vendor=calloc(vendorlen+1,1);
159 _v_readstring(opb,vc->vendor,vendorlen);
160 vc->comments=_oggpack_read(opb,32);
161 if(vc->comments<0)goto err_out;
162 vc->user_comments=calloc(vc->comments+1,sizeof(char **));
164 for(i=0;i<vc->comments;i++){
165 int len=_oggpack_read(opb,32);
166 if(len<0)goto err_out;
167 vc->user_comments[i]=calloc(len+1,1);
168 _v_readstring(opb,vc->user_comments[i],len);
170 if(_oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
174 vorbis_comment_clear(vc);
178 /* all of the real encoding details are here. The modes, books,
180 static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
184 vi->books=_oggpack_read(opb,8)+1;
185 /*vi->book_param=calloc(vi->books,sizeof(static_codebook *));*/
186 for(i=0;i<vi->books;i++){
187 vi->book_param[i]=calloc(1,sizeof(static_codebook));
188 if(vorbis_staticbook_unpack(opb,vi->book_param[i]))goto err_out;
191 /* time backend settings */
192 vi->times=_oggpack_read(opb,6)+1;
193 /*vi->time_type=malloc(vi->times*sizeof(int));*/
194 /*vi->time_param=calloc(vi->times,sizeof(void *));*/
195 for(i=0;i<vi->times;i++){
196 vi->time_type[i]=_oggpack_read(opb,16);
197 if(vi->time_type[i]<0 || vi->time_type[i]>=VI_TIMEB)goto err_out;
198 vi->time_param[i]=_time_P[vi->time_type[i]]->unpack(vi,opb);
199 if(!vi->time_param[i])goto err_out;
202 /* floor backend settings */
203 vi->floors=_oggpack_read(opb,6)+1;
204 /*vi->floor_type=malloc(vi->floors*sizeof(int));*/
205 /*vi->floor_param=calloc(vi->floors,sizeof(void *));*/
206 for(i=0;i<vi->floors;i++){
207 vi->floor_type[i]=_oggpack_read(opb,16);
208 if(vi->floor_type[i]<0 || vi->floor_type[i]>=VI_FLOORB)goto err_out;
209 vi->floor_param[i]=_floor_P[vi->floor_type[i]]->unpack(vi,opb);
210 if(!vi->floor_param[i])goto err_out;
213 /* residue backend settings */
214 vi->residues=_oggpack_read(opb,6)+1;
215 /*vi->residue_type=malloc(vi->residues*sizeof(int));*/
216 /*vi->residue_param=calloc(vi->residues,sizeof(void *));*/
217 for(i=0;i<vi->residues;i++){
218 vi->residue_type[i]=_oggpack_read(opb,16);
219 if(vi->residue_type[i]<0 || vi->residue_type[i]>=VI_RESB)goto err_out;
220 vi->residue_param[i]=_residue_P[vi->residue_type[i]]->unpack(vi,opb);
221 if(!vi->residue_param[i])goto err_out;
224 /* map backend settings */
225 vi->maps=_oggpack_read(opb,6)+1;
226 /*vi->map_type=malloc(vi->maps*sizeof(int));*/
227 /*vi->map_param=calloc(vi->maps,sizeof(void *));*/
228 for(i=0;i<vi->maps;i++){
229 vi->map_type[i]=_oggpack_read(opb,16);
230 if(vi->map_type[i]<0 || vi->map_type[i]>=VI_MAPB)goto err_out;
231 vi->map_param[i]=_mapping_P[vi->map_type[i]]->unpack(vi,opb);
232 if(!vi->map_param[i])goto err_out;
236 vi->modes=_oggpack_read(opb,6)+1;
237 /*vi->mode_param=calloc(vi->modes,sizeof(void *));*/
238 for(i=0;i<vi->modes;i++){
239 vi->mode_param[i]=calloc(1,sizeof(vorbis_info_mode));
240 vi->mode_param[i]->blockflag=_oggpack_read(opb,1);
241 vi->mode_param[i]->windowtype=_oggpack_read(opb,16);
242 vi->mode_param[i]->transformtype=_oggpack_read(opb,16);
243 vi->mode_param[i]->mapping=_oggpack_read(opb,8);
245 if(vi->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
246 if(vi->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
247 if(vi->mode_param[i]->mapping>=vi->maps)goto err_out;
250 if(_oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
254 vorbis_info_clear(vi);
258 /* The Vorbis header is in three packets; the initial small packet in
259 the first page that identifies basic parameters, a second packet
260 with bitstream comments and a third packet that holds the
263 int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
267 _oggpack_readinit(&opb,op->packet,op->bytes);
269 /* Which of the three types of header is this? */
270 /* Also verify header-ness, vorbis */
274 _v_readstring(&opb,buffer,6);
275 if(memcmp(buffer,"vorbis",6)){
276 /* not a vorbis header */
279 switch(_oggpack_read(&opb,8)){
282 /* Not the initial packet */
286 /* previously initialized info header */
290 return(_vorbis_unpack_info(vi,&opb));
294 /* um... we didn't get the initial header */
298 return(_vorbis_unpack_comment(vc,&opb));
301 if(vi->rate==0 || vc->vendor==NULL){
302 /* um... we didn;t get the initial header or comments yet */
306 return(_vorbis_unpack_books(vi,&opb));
309 /* Not a valid vorbis header type */
318 /* pack side **********************************************************/
320 static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){
322 _v_writestring(opb,"vorbis");
323 _oggpack_write(opb,0x80,8);
325 /* basic information about the stream */
326 _oggpack_write(opb,0x00,32);
327 _oggpack_write(opb,vi->channels,8);
328 _oggpack_write(opb,vi->rate,32);
330 _oggpack_write(opb,vi->bitrate_upper,32);
331 _oggpack_write(opb,vi->bitrate_nominal,32);
332 _oggpack_write(opb,vi->bitrate_lower,32);
334 _oggpack_write(opb,ilog2(vi->blocksizes[0]),4);
335 _oggpack_write(opb,ilog2(vi->blocksizes[1]),4);
336 _oggpack_write(opb,1,1);
341 static int _vorbis_pack_comment(oggpack_buffer *opb,vorbis_comment *vc){
342 char temp[]="Xiphophorus libVorbis I 20000121";
345 _v_writestring(opb,"vorbis");
346 _oggpack_write(opb,0x81,8);
349 _oggpack_write(opb,strlen(temp),32);
350 _v_writestring(opb,temp);
354 _oggpack_write(opb,vc->comments,32);
357 for(i=0;i<vc->comments;i++){
358 if(vc->user_comments[i]){
359 _oggpack_write(opb,strlen(vc->user_comments[i]),32);
360 _v_writestring(opb,vc->user_comments[i]);
362 _oggpack_write(opb,0,32);
366 _oggpack_write(opb,1,1);
371 static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){
373 _v_writestring(opb,"vorbis");
374 _oggpack_write(opb,0x82,8);
377 _oggpack_write(opb,vi->books-1,8);
378 for(i=0;i<vi->books;i++)
379 if(vorbis_staticbook_pack(vi->book_param[i],opb))goto err_out;
382 _oggpack_write(opb,vi->times-1,6);
383 for(i=0;i<vi->times;i++){
384 _oggpack_write(opb,vi->time_type[i],16);
385 _time_P[vi->time_type[i]]->pack(vi->time_param[i],opb);
389 _oggpack_write(opb,vi->floors-1,6);
390 for(i=0;i<vi->floors;i++){
391 _oggpack_write(opb,vi->floor_type[i],16);
392 _floor_P[vi->floor_type[i]]->pack(vi->floor_param[i],opb);
396 _oggpack_write(opb,vi->residues-1,6);
397 for(i=0;i<vi->residues;i++){
398 _oggpack_write(opb,vi->residue_type[i],16);
399 _residue_P[vi->residue_type[i]]->pack(vi->residue_param[i],opb);
403 _oggpack_write(opb,vi->maps-1,6);
404 for(i=0;i<vi->maps;i++){
405 _oggpack_write(opb,vi->map_type[i],16);
406 _mapping_P[vi->map_type[i]]->pack(vi,vi->map_param[i],opb);
410 _oggpack_write(opb,vi->modes-1,6);
411 for(i=0;i<vi->modes;i++){
412 _oggpack_write(opb,vi->mode_param[i]->blockflag,1);
413 _oggpack_write(opb,vi->mode_param[i]->windowtype,16);
414 _oggpack_write(opb,vi->mode_param[i]->transformtype,16);
415 _oggpack_write(opb,vi->mode_param[i]->mapping,8);
417 _oggpack_write(opb,1,1);
424 int vorbis_analysis_headerout(vorbis_dsp_state *v,
428 ogg_packet *op_code){
429 vorbis_info *vi=v->vi;
432 /* first header packet **********************************************/
434 _oggpack_writeinit(&opb);
435 if(_vorbis_pack_info(&opb,vi))goto err_out;
437 /* build the packet */
438 if(v->header)free(v->header);
439 v->header=malloc(_oggpack_bytes(&opb));
440 memcpy(v->header,opb.buffer,_oggpack_bytes(&opb));
441 op->packet=v->header;
442 op->bytes=_oggpack_bytes(&opb);
447 /* second header packet (comments) **********************************/
449 _oggpack_reset(&opb);
450 _v_writestring(&opb,"vorbis");
451 _oggpack_write(&opb,0x81,8);
452 if(_vorbis_pack_comment(&opb,vc))goto err_out;
454 if(v->header1)free(v->header1);
455 v->header1=malloc(_oggpack_bytes(&opb));
456 memcpy(v->header1,opb.buffer,_oggpack_bytes(&opb));
457 op_comm->packet=v->header1;
458 op_comm->bytes=_oggpack_bytes(&opb);
463 /* third header packet (modes/codebooks) ****************************/
465 _oggpack_reset(&opb);
466 _v_writestring(&opb,"vorbis");
467 _oggpack_write(&opb,0x82,8);
468 if(_vorbis_pack_books(&opb,vi))goto err_out;
470 if(v->header2)free(v->header2);
471 v->header2=malloc(_oggpack_bytes(&opb));
472 memcpy(v->header2,opb.buffer,_oggpack_bytes(&opb));
473 op_code->packet=v->header2;
474 op_code->bytes=_oggpack_bytes(&opb);
479 _oggpack_writeclear(&opb);
482 _oggpack_writeclear(&opb);
483 memset(op,0,sizeof(ogg_packet));
484 memset(op_comm,0,sizeof(ogg_packet));
485 memset(op_code,0,sizeof(ogg_packet));
487 if(v->header)free(v->header);
488 if(v->header1)free(v->header1);
489 if(v->header2)free(v->header2);