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.22 2000/02/23 11:50:29 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[vi->map_type[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[vi->time_type[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[vi->floor_type[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[vi->residue_type[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 */
273 int packtype=_oggpack_read(&opb,8);
275 _v_readstring(&opb,buffer,6);
276 if(memcmp(buffer,"vorbis",6)){
277 /* not a vorbis header */
281 case 0x01: /* least significant *bit* is read first */
283 /* Not the initial packet */
287 /* previously initialized info header */
291 return(_vorbis_unpack_info(vi,&opb));
293 case 0x03: /* least significant *bit* is read first */
295 /* um... we didn't get the initial header */
299 return(_vorbis_unpack_comment(vc,&opb));
301 case 0x05: /* least significant *bit* is read first */
302 if(vi->rate==0 || vc->vendor==NULL){
303 /* um... we didn;t get the initial header or comments yet */
307 return(_vorbis_unpack_books(vi,&opb));
310 /* Not a valid vorbis header type */
319 /* pack side **********************************************************/
321 static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){
323 _oggpack_write(opb,0x01,8);
324 _v_writestring(opb,"vorbis");
326 /* basic information about the stream */
327 _oggpack_write(opb,0x00,32);
328 _oggpack_write(opb,vi->channels,8);
329 _oggpack_write(opb,vi->rate,32);
331 _oggpack_write(opb,vi->bitrate_upper,32);
332 _oggpack_write(opb,vi->bitrate_nominal,32);
333 _oggpack_write(opb,vi->bitrate_lower,32);
335 _oggpack_write(opb,ilog2(vi->blocksizes[0]),4);
336 _oggpack_write(opb,ilog2(vi->blocksizes[1]),4);
337 _oggpack_write(opb,1,1);
342 static int _vorbis_pack_comment(oggpack_buffer *opb,vorbis_comment *vc){
343 char temp[]="Xiphophorus libVorbis I 20000223";
346 _oggpack_write(opb,0x03,8);
347 _v_writestring(opb,"vorbis");
350 _oggpack_write(opb,strlen(temp),32);
351 _v_writestring(opb,temp);
355 _oggpack_write(opb,vc->comments,32);
358 for(i=0;i<vc->comments;i++){
359 if(vc->user_comments[i]){
360 _oggpack_write(opb,strlen(vc->user_comments[i]),32);
361 _v_writestring(opb,vc->user_comments[i]);
363 _oggpack_write(opb,0,32);
367 _oggpack_write(opb,1,1);
372 static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){
374 _oggpack_write(opb,0x05,8);
375 _v_writestring(opb,"vorbis");
378 _oggpack_write(opb,vi->books-1,8);
379 for(i=0;i<vi->books;i++)
380 if(vorbis_staticbook_pack(vi->book_param[i],opb))goto err_out;
383 _oggpack_write(opb,vi->times-1,6);
384 for(i=0;i<vi->times;i++){
385 _oggpack_write(opb,vi->time_type[i],16);
386 _time_P[vi->time_type[i]]->pack(vi->time_param[i],opb);
390 _oggpack_write(opb,vi->floors-1,6);
391 for(i=0;i<vi->floors;i++){
392 _oggpack_write(opb,vi->floor_type[i],16);
393 _floor_P[vi->floor_type[i]]->pack(vi->floor_param[i],opb);
397 _oggpack_write(opb,vi->residues-1,6);
398 for(i=0;i<vi->residues;i++){
399 _oggpack_write(opb,vi->residue_type[i],16);
400 _residue_P[vi->residue_type[i]]->pack(vi->residue_param[i],opb);
404 _oggpack_write(opb,vi->maps-1,6);
405 for(i=0;i<vi->maps;i++){
406 _oggpack_write(opb,vi->map_type[i],16);
407 _mapping_P[vi->map_type[i]]->pack(vi,vi->map_param[i],opb);
411 _oggpack_write(opb,vi->modes-1,6);
412 for(i=0;i<vi->modes;i++){
413 _oggpack_write(opb,vi->mode_param[i]->blockflag,1);
414 _oggpack_write(opb,vi->mode_param[i]->windowtype,16);
415 _oggpack_write(opb,vi->mode_param[i]->transformtype,16);
416 _oggpack_write(opb,vi->mode_param[i]->mapping,8);
418 _oggpack_write(opb,1,1);
425 int vorbis_analysis_headerout(vorbis_dsp_state *v,
429 ogg_packet *op_code){
430 vorbis_info *vi=v->vi;
433 /* first header packet **********************************************/
435 _oggpack_writeinit(&opb);
436 if(_vorbis_pack_info(&opb,vi))goto err_out;
438 /* build the packet */
439 if(v->header)free(v->header);
440 v->header=malloc(_oggpack_bytes(&opb));
441 memcpy(v->header,opb.buffer,_oggpack_bytes(&opb));
442 op->packet=v->header;
443 op->bytes=_oggpack_bytes(&opb);
448 /* second header packet (comments) **********************************/
450 _oggpack_reset(&opb);
451 if(_vorbis_pack_comment(&opb,vc))goto err_out;
453 if(v->header1)free(v->header1);
454 v->header1=malloc(_oggpack_bytes(&opb));
455 memcpy(v->header1,opb.buffer,_oggpack_bytes(&opb));
456 op_comm->packet=v->header1;
457 op_comm->bytes=_oggpack_bytes(&opb);
462 /* third header packet (modes/codebooks) ****************************/
464 _oggpack_reset(&opb);
465 if(_vorbis_pack_books(&opb,vi))goto err_out;
467 if(v->header2)free(v->header2);
468 v->header2=malloc(_oggpack_bytes(&opb));
469 memcpy(v->header2,opb.buffer,_oggpack_bytes(&opb));
470 op_code->packet=v->header2;
471 op_code->bytes=_oggpack_bytes(&opb);
476 _oggpack_writeclear(&opb);
479 _oggpack_writeclear(&opb);
480 memset(op,0,sizeof(ogg_packet));
481 memset(op_comm,0,sizeof(ogg_packet));
482 memset(op_code,0,sizeof(ogg_packet));
484 if(v->header)free(v->header);
485 if(v->header1)free(v->header1);
486 if(v->header2)free(v->header2);