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.25 2000/07/07 00:53:10 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 "sharedbook.h"
28 #include "bookinternal.h"
36 static int ilog2(unsigned int v){
45 static void _v_writestring(oggpack_buffer *o,char *s){
47 _oggpack_write(o,*s++,8);
51 static void _v_readstring(oggpack_buffer *o,char *buf,int bytes){
53 *buf++=_oggpack_read(o,8);
57 void vorbis_comment_init(vorbis_comment *vc){
58 memset(vc,0,sizeof(vorbis_comment));
61 void vorbis_comment_add(vorbis_comment *vc,char *comment){
62 vc->user_comments=realloc(vc->user_comments,
63 (vc->comments+2)*sizeof(char *));
64 vc->user_comments[vc->comments]=strdup(comment);
66 vc->user_comments[vc->comments]=NULL;
69 void vorbis_comment_clear(vorbis_comment *vc){
72 for(i=0;i<vc->comments;i++)
73 if(vc->user_comments[i])free(vc->user_comments[i]);
74 if(vc->user_comments)free(vc->user_comments);
75 if(vc->vendor)free(vc->vendor);
77 memset(vc,0,sizeof(vorbis_comment));
80 /* used by synthesis, which has a full, alloced vi */
81 void vorbis_info_init(vorbis_info *vi){
82 memset(vi,0,sizeof(vorbis_info));
85 void vorbis_info_clear(vorbis_info *vi){
88 for(i=0;i<vi->modes;i++)
89 if(vi->mode_param[i])free(vi->mode_param[i]);
90 /*if(vi->mode_param)free(vi->mode_param);*/
92 for(i=0;i<vi->maps;i++) /* unpack does the range checking */
93 _mapping_P[vi->map_type[i]]->free_info(vi->map_param[i]);
94 /*if(vi->map_param)free(vi->map_param);*/
96 for(i=0;i<vi->times;i++) /* unpack does the range checking */
97 _time_P[vi->time_type[i]]->free_info(vi->time_param[i]);
98 /*if(vi->time_param)free(vi->time_param);*/
100 for(i=0;i<vi->floors;i++) /* unpack does the range checking */
101 _floor_P[vi->floor_type[i]]->free_info(vi->floor_param[i]);
102 /*if(vi->floor_param)free(vi->floor_param);*/
104 for(i=0;i<vi->residues;i++) /* unpack does the range checking */
105 _residue_P[vi->residue_type[i]]->free_info(vi->residue_param[i]);
106 /*if(vi->residue_param)free(vi->residue_param);*/
108 /* the static codebooks *are* freed if you call info_clear, because
109 decode side does alloc a 'static' codebook. Calling clear on the
110 full codebook does not clear the static codebook (that's our
112 for(i=0;i<vi->books;i++){
113 /* just in case the decoder pre-cleared to save space */
114 if(vi->book_param[i]){
115 vorbis_staticbook_clear(vi->book_param[i]);
116 free(vi->book_param[i]);
119 /*if(vi->book_param)free(vi->book_param);*/
121 for(i=0;i<vi->psys;i++)
122 _vi_psy_free(vi->psy_param[i]);
123 /*if(vi->psy_param)free(vi->psy_param);*/
125 memset(vi,0,sizeof(vorbis_info));
128 /* Header packing/unpacking ********************************************/
130 static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
131 vi->version=_oggpack_read(opb,32);
132 if(vi->version!=0)return(-1);
134 vi->channels=_oggpack_read(opb,8);
135 vi->rate=_oggpack_read(opb,32);
137 vi->bitrate_upper=_oggpack_read(opb,32);
138 vi->bitrate_nominal=_oggpack_read(opb,32);
139 vi->bitrate_lower=_oggpack_read(opb,32);
141 vi->blocksizes[0]=1<<_oggpack_read(opb,4);
142 vi->blocksizes[1]=1<<_oggpack_read(opb,4);
144 if(vi->rate<1)goto err_out;
145 if(vi->channels<1)goto err_out;
146 if(vi->blocksizes[0]<8)goto err_out;
147 if(vi->blocksizes[1]<vi->blocksizes[0])goto err_out;
149 if(_oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
153 vorbis_info_clear(vi);
157 static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
159 int vendorlen=_oggpack_read(opb,32);
160 if(vendorlen<0)goto err_out;
161 vc->vendor=calloc(vendorlen+1,1);
162 _v_readstring(opb,vc->vendor,vendorlen);
163 vc->comments=_oggpack_read(opb,32);
164 if(vc->comments<0)goto err_out;
165 vc->user_comments=calloc(vc->comments+1,sizeof(char **));
167 for(i=0;i<vc->comments;i++){
168 int len=_oggpack_read(opb,32);
169 if(len<0)goto err_out;
170 vc->user_comments[i]=calloc(len+1,1);
171 _v_readstring(opb,vc->user_comments[i],len);
173 if(_oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
177 vorbis_comment_clear(vc);
181 /* all of the real encoding details are here. The modes, books,
183 static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
187 vi->books=_oggpack_read(opb,8)+1;
188 /*vi->book_param=calloc(vi->books,sizeof(static_codebook *));*/
189 for(i=0;i<vi->books;i++){
190 vi->book_param[i]=calloc(1,sizeof(static_codebook));
191 if(vorbis_staticbook_unpack(opb,vi->book_param[i]))goto err_out;
194 /* time backend settings */
195 vi->times=_oggpack_read(opb,6)+1;
196 /*vi->time_type=malloc(vi->times*sizeof(int));*/
197 /*vi->time_param=calloc(vi->times,sizeof(void *));*/
198 for(i=0;i<vi->times;i++){
199 vi->time_type[i]=_oggpack_read(opb,16);
200 if(vi->time_type[i]<0 || vi->time_type[i]>=VI_TIMEB)goto err_out;
201 vi->time_param[i]=_time_P[vi->time_type[i]]->unpack(vi,opb);
202 if(!vi->time_param[i])goto err_out;
205 /* floor backend settings */
206 vi->floors=_oggpack_read(opb,6)+1;
207 /*vi->floor_type=malloc(vi->floors*sizeof(int));*/
208 /*vi->floor_param=calloc(vi->floors,sizeof(void *));*/
209 for(i=0;i<vi->floors;i++){
210 vi->floor_type[i]=_oggpack_read(opb,16);
211 if(vi->floor_type[i]<0 || vi->floor_type[i]>=VI_FLOORB)goto err_out;
212 vi->floor_param[i]=_floor_P[vi->floor_type[i]]->unpack(vi,opb);
213 if(!vi->floor_param[i])goto err_out;
216 /* residue backend settings */
217 vi->residues=_oggpack_read(opb,6)+1;
218 /*vi->residue_type=malloc(vi->residues*sizeof(int));*/
219 /*vi->residue_param=calloc(vi->residues,sizeof(void *));*/
220 for(i=0;i<vi->residues;i++){
221 vi->residue_type[i]=_oggpack_read(opb,16);
222 if(vi->residue_type[i]<0 || vi->residue_type[i]>=VI_RESB)goto err_out;
223 vi->residue_param[i]=_residue_P[vi->residue_type[i]]->unpack(vi,opb);
224 if(!vi->residue_param[i])goto err_out;
227 /* map backend settings */
228 vi->maps=_oggpack_read(opb,6)+1;
229 /*vi->map_type=malloc(vi->maps*sizeof(int));*/
230 /*vi->map_param=calloc(vi->maps,sizeof(void *));*/
231 for(i=0;i<vi->maps;i++){
232 vi->map_type[i]=_oggpack_read(opb,16);
233 if(vi->map_type[i]<0 || vi->map_type[i]>=VI_MAPB)goto err_out;
234 vi->map_param[i]=_mapping_P[vi->map_type[i]]->unpack(vi,opb);
235 if(!vi->map_param[i])goto err_out;
239 vi->modes=_oggpack_read(opb,6)+1;
240 /*vi->mode_param=calloc(vi->modes,sizeof(void *));*/
241 for(i=0;i<vi->modes;i++){
242 vi->mode_param[i]=calloc(1,sizeof(vorbis_info_mode));
243 vi->mode_param[i]->blockflag=_oggpack_read(opb,1);
244 vi->mode_param[i]->windowtype=_oggpack_read(opb,16);
245 vi->mode_param[i]->transformtype=_oggpack_read(opb,16);
246 vi->mode_param[i]->mapping=_oggpack_read(opb,8);
248 if(vi->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
249 if(vi->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
250 if(vi->mode_param[i]->mapping>=vi->maps)goto err_out;
253 if(_oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
257 vorbis_info_clear(vi);
261 /* The Vorbis header is in three packets; the initial small packet in
262 the first page that identifies basic parameters, a second packet
263 with bitstream comments and a third packet that holds the
266 int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
270 _oggpack_readinit(&opb,op->packet,op->bytes);
272 /* Which of the three types of header is this? */
273 /* Also verify header-ness, vorbis */
276 int packtype=_oggpack_read(&opb,8);
278 _v_readstring(&opb,buffer,6);
279 if(memcmp(buffer,"vorbis",6)){
280 /* not a vorbis header */
284 case 0x01: /* least significant *bit* is read first */
286 /* Not the initial packet */
290 /* previously initialized info header */
294 return(_vorbis_unpack_info(vi,&opb));
296 case 0x03: /* least significant *bit* is read first */
298 /* um... we didn't get the initial header */
302 return(_vorbis_unpack_comment(vc,&opb));
304 case 0x05: /* least significant *bit* is read first */
305 if(vi->rate==0 || vc->vendor==NULL){
306 /* um... we didn;t get the initial header or comments yet */
310 return(_vorbis_unpack_books(vi,&opb));
313 /* Not a valid vorbis header type */
322 /* pack side **********************************************************/
324 static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){
326 _oggpack_write(opb,0x01,8);
327 _v_writestring(opb,"vorbis");
329 /* basic information about the stream */
330 _oggpack_write(opb,0x00,32);
331 _oggpack_write(opb,vi->channels,8);
332 _oggpack_write(opb,vi->rate,32);
334 _oggpack_write(opb,vi->bitrate_upper,32);
335 _oggpack_write(opb,vi->bitrate_nominal,32);
336 _oggpack_write(opb,vi->bitrate_lower,32);
338 _oggpack_write(opb,ilog2(vi->blocksizes[0]),4);
339 _oggpack_write(opb,ilog2(vi->blocksizes[1]),4);
340 _oggpack_write(opb,1,1);
345 static int _vorbis_pack_comment(oggpack_buffer *opb,vorbis_comment *vc){
346 char temp[]="Xiphophorus libVorbis I 20000508";
349 _oggpack_write(opb,0x03,8);
350 _v_writestring(opb,"vorbis");
353 _oggpack_write(opb,strlen(temp),32);
354 _v_writestring(opb,temp);
358 _oggpack_write(opb,vc->comments,32);
361 for(i=0;i<vc->comments;i++){
362 if(vc->user_comments[i]){
363 _oggpack_write(opb,strlen(vc->user_comments[i]),32);
364 _v_writestring(opb,vc->user_comments[i]);
366 _oggpack_write(opb,0,32);
370 _oggpack_write(opb,1,1);
375 static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){
377 _oggpack_write(opb,0x05,8);
378 _v_writestring(opb,"vorbis");
381 _oggpack_write(opb,vi->books-1,8);
382 for(i=0;i<vi->books;i++)
383 if(vorbis_staticbook_pack(vi->book_param[i],opb))goto err_out;
386 _oggpack_write(opb,vi->times-1,6);
387 for(i=0;i<vi->times;i++){
388 _oggpack_write(opb,vi->time_type[i],16);
389 _time_P[vi->time_type[i]]->pack(vi->time_param[i],opb);
393 _oggpack_write(opb,vi->floors-1,6);
394 for(i=0;i<vi->floors;i++){
395 _oggpack_write(opb,vi->floor_type[i],16);
396 _floor_P[vi->floor_type[i]]->pack(vi->floor_param[i],opb);
400 _oggpack_write(opb,vi->residues-1,6);
401 for(i=0;i<vi->residues;i++){
402 _oggpack_write(opb,vi->residue_type[i],16);
403 _residue_P[vi->residue_type[i]]->pack(vi->residue_param[i],opb);
407 _oggpack_write(opb,vi->maps-1,6);
408 for(i=0;i<vi->maps;i++){
409 _oggpack_write(opb,vi->map_type[i],16);
410 _mapping_P[vi->map_type[i]]->pack(vi,vi->map_param[i],opb);
414 _oggpack_write(opb,vi->modes-1,6);
415 for(i=0;i<vi->modes;i++){
416 _oggpack_write(opb,vi->mode_param[i]->blockflag,1);
417 _oggpack_write(opb,vi->mode_param[i]->windowtype,16);
418 _oggpack_write(opb,vi->mode_param[i]->transformtype,16);
419 _oggpack_write(opb,vi->mode_param[i]->mapping,8);
421 _oggpack_write(opb,1,1);
428 int vorbis_analysis_headerout(vorbis_dsp_state *v,
432 ogg_packet *op_code){
433 vorbis_info *vi=v->vi;
436 /* first header packet **********************************************/
438 _oggpack_writeinit(&opb);
439 if(_vorbis_pack_info(&opb,vi))goto err_out;
441 /* build the packet */
442 if(v->header)free(v->header);
443 v->header=malloc(_oggpack_bytes(&opb));
444 memcpy(v->header,opb.buffer,_oggpack_bytes(&opb));
445 op->packet=v->header;
446 op->bytes=_oggpack_bytes(&opb);
451 /* second header packet (comments) **********************************/
453 _oggpack_reset(&opb);
454 if(_vorbis_pack_comment(&opb,vc))goto err_out;
456 if(v->header1)free(v->header1);
457 v->header1=malloc(_oggpack_bytes(&opb));
458 memcpy(v->header1,opb.buffer,_oggpack_bytes(&opb));
459 op_comm->packet=v->header1;
460 op_comm->bytes=_oggpack_bytes(&opb);
465 /* third header packet (modes/codebooks) ****************************/
467 _oggpack_reset(&opb);
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);