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.23 2000/03/10 13:21:18 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"
34 static int ilog2(unsigned int v){
43 static void _v_writestring(oggpack_buffer *o,char *s){
45 _oggpack_write(o,*s++,8);
49 static void _v_readstring(oggpack_buffer *o,char *buf,int bytes){
51 *buf++=_oggpack_read(o,8);
55 void vorbis_comment_init(vorbis_comment *vc){
56 memset(vc,0,sizeof(vorbis_comment));
59 void vorbis_comment_add(vorbis_comment *vc,char *comment){
60 vc->user_comments=realloc(vc->user_comments,
61 (vc->comments+2)*sizeof(char *));
62 vc->user_comments[vc->comments]=strdup(comment);
64 vc->user_comments[vc->comments]=NULL;
67 void vorbis_comment_clear(vorbis_comment *vc){
70 for(i=0;i<vc->comments;i++)
71 if(vc->user_comments[i])free(vc->user_comments[i]);
72 if(vc->user_comments)free(vc->user_comments);
73 if(vc->vendor)free(vc->vendor);
75 memset(vc,0,sizeof(vorbis_comment));
78 /* used by synthesis, which has a full, alloced vi */
79 void vorbis_info_init(vorbis_info *vi){
80 memset(vi,0,sizeof(vorbis_info));
83 void vorbis_info_clear(vorbis_info *vi){
86 for(i=0;i<vi->modes;i++)
87 if(vi->mode_param[i])free(vi->mode_param[i]);
88 /*if(vi->mode_param)free(vi->mode_param);*/
90 for(i=0;i<vi->maps;i++) /* unpack does the range checking */
91 _mapping_P[vi->map_type[i]]->free_info(vi->map_param[i]);
92 /*if(vi->map_param)free(vi->map_param);*/
94 for(i=0;i<vi->times;i++) /* unpack does the range checking */
95 _time_P[vi->time_type[i]]->free_info(vi->time_param[i]);
96 /*if(vi->time_param)free(vi->time_param);*/
98 for(i=0;i<vi->floors;i++) /* unpack does the range checking */
99 _floor_P[vi->floor_type[i]]->free_info(vi->floor_param[i]);
100 /*if(vi->floor_param)free(vi->floor_param);*/
102 for(i=0;i<vi->residues;i++) /* unpack does the range checking */
103 _residue_P[vi->residue_type[i]]->free_info(vi->residue_param[i]);
104 /*if(vi->residue_param)free(vi->residue_param);*/
106 /* the static codebooks *are* freed if you call info_clear, because
107 decode side does alloc a 'static' codebook. Calling clear on the
108 full codebook does not clear the static codebook (that's our
110 for(i=0;i<vi->books;i++){
111 /* just in case the decoder pre-cleared to save space */
112 if(vi->book_param[i]){
113 vorbis_staticbook_clear(vi->book_param[i]);
114 free(vi->book_param[i]);
117 /*if(vi->book_param)free(vi->book_param);*/
119 for(i=0;i<vi->psys;i++)
120 _vi_psy_free(vi->psy_param[i]);
121 /*if(vi->psy_param)free(vi->psy_param);*/
123 memset(vi,0,sizeof(vorbis_info));
126 /* Header packing/unpacking ********************************************/
128 static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
129 vi->version=_oggpack_read(opb,32);
130 if(vi->version!=0)return(-1);
132 vi->channels=_oggpack_read(opb,8);
133 vi->rate=_oggpack_read(opb,32);
135 vi->bitrate_upper=_oggpack_read(opb,32);
136 vi->bitrate_nominal=_oggpack_read(opb,32);
137 vi->bitrate_lower=_oggpack_read(opb,32);
139 vi->blocksizes[0]=1<<_oggpack_read(opb,4);
140 vi->blocksizes[1]=1<<_oggpack_read(opb,4);
142 if(vi->rate<1)goto err_out;
143 if(vi->channels<1)goto err_out;
144 if(vi->blocksizes[0]<8)goto err_out;
145 if(vi->blocksizes[1]<vi->blocksizes[0])goto err_out;
147 if(_oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
151 vorbis_info_clear(vi);
155 static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
157 int vendorlen=_oggpack_read(opb,32);
158 if(vendorlen<0)goto err_out;
159 vc->vendor=calloc(vendorlen+1,1);
160 _v_readstring(opb,vc->vendor,vendorlen);
161 vc->comments=_oggpack_read(opb,32);
162 if(vc->comments<0)goto err_out;
163 vc->user_comments=calloc(vc->comments+1,sizeof(char **));
165 for(i=0;i<vc->comments;i++){
166 int len=_oggpack_read(opb,32);
167 if(len<0)goto err_out;
168 vc->user_comments[i]=calloc(len+1,1);
169 _v_readstring(opb,vc->user_comments[i],len);
171 if(_oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
175 vorbis_comment_clear(vc);
179 /* all of the real encoding details are here. The modes, books,
181 static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
185 vi->books=_oggpack_read(opb,8)+1;
186 /*vi->book_param=calloc(vi->books,sizeof(static_codebook *));*/
187 for(i=0;i<vi->books;i++){
188 vi->book_param[i]=calloc(1,sizeof(static_codebook));
189 if(vorbis_staticbook_unpack(opb,vi->book_param[i]))goto err_out;
192 /* time backend settings */
193 vi->times=_oggpack_read(opb,6)+1;
194 /*vi->time_type=malloc(vi->times*sizeof(int));*/
195 /*vi->time_param=calloc(vi->times,sizeof(void *));*/
196 for(i=0;i<vi->times;i++){
197 vi->time_type[i]=_oggpack_read(opb,16);
198 if(vi->time_type[i]<0 || vi->time_type[i]>=VI_TIMEB)goto err_out;
199 vi->time_param[i]=_time_P[vi->time_type[i]]->unpack(vi,opb);
200 if(!vi->time_param[i])goto err_out;
203 /* floor backend settings */
204 vi->floors=_oggpack_read(opb,6)+1;
205 /*vi->floor_type=malloc(vi->floors*sizeof(int));*/
206 /*vi->floor_param=calloc(vi->floors,sizeof(void *));*/
207 for(i=0;i<vi->floors;i++){
208 vi->floor_type[i]=_oggpack_read(opb,16);
209 if(vi->floor_type[i]<0 || vi->floor_type[i]>=VI_FLOORB)goto err_out;
210 vi->floor_param[i]=_floor_P[vi->floor_type[i]]->unpack(vi,opb);
211 if(!vi->floor_param[i])goto err_out;
214 /* residue backend settings */
215 vi->residues=_oggpack_read(opb,6)+1;
216 /*vi->residue_type=malloc(vi->residues*sizeof(int));*/
217 /*vi->residue_param=calloc(vi->residues,sizeof(void *));*/
218 for(i=0;i<vi->residues;i++){
219 vi->residue_type[i]=_oggpack_read(opb,16);
220 if(vi->residue_type[i]<0 || vi->residue_type[i]>=VI_RESB)goto err_out;
221 vi->residue_param[i]=_residue_P[vi->residue_type[i]]->unpack(vi,opb);
222 if(!vi->residue_param[i])goto err_out;
225 /* map backend settings */
226 vi->maps=_oggpack_read(opb,6)+1;
227 /*vi->map_type=malloc(vi->maps*sizeof(int));*/
228 /*vi->map_param=calloc(vi->maps,sizeof(void *));*/
229 for(i=0;i<vi->maps;i++){
230 vi->map_type[i]=_oggpack_read(opb,16);
231 if(vi->map_type[i]<0 || vi->map_type[i]>=VI_MAPB)goto err_out;
232 vi->map_param[i]=_mapping_P[vi->map_type[i]]->unpack(vi,opb);
233 if(!vi->map_param[i])goto err_out;
237 vi->modes=_oggpack_read(opb,6)+1;
238 /*vi->mode_param=calloc(vi->modes,sizeof(void *));*/
239 for(i=0;i<vi->modes;i++){
240 vi->mode_param[i]=calloc(1,sizeof(vorbis_info_mode));
241 vi->mode_param[i]->blockflag=_oggpack_read(opb,1);
242 vi->mode_param[i]->windowtype=_oggpack_read(opb,16);
243 vi->mode_param[i]->transformtype=_oggpack_read(opb,16);
244 vi->mode_param[i]->mapping=_oggpack_read(opb,8);
246 if(vi->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
247 if(vi->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
248 if(vi->mode_param[i]->mapping>=vi->maps)goto err_out;
251 if(_oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
255 vorbis_info_clear(vi);
259 /* The Vorbis header is in three packets; the initial small packet in
260 the first page that identifies basic parameters, a second packet
261 with bitstream comments and a third packet that holds the
264 int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
268 _oggpack_readinit(&opb,op->packet,op->bytes);
270 /* Which of the three types of header is this? */
271 /* Also verify header-ness, vorbis */
274 int packtype=_oggpack_read(&opb,8);
276 _v_readstring(&opb,buffer,6);
277 if(memcmp(buffer,"vorbis",6)){
278 /* not a vorbis header */
282 case 0x01: /* least significant *bit* is read first */
284 /* Not the initial packet */
288 /* previously initialized info header */
292 return(_vorbis_unpack_info(vi,&opb));
294 case 0x03: /* least significant *bit* is read first */
296 /* um... we didn't get the initial header */
300 return(_vorbis_unpack_comment(vc,&opb));
302 case 0x05: /* least significant *bit* is read first */
303 if(vi->rate==0 || vc->vendor==NULL){
304 /* um... we didn;t get the initial header or comments yet */
308 return(_vorbis_unpack_books(vi,&opb));
311 /* Not a valid vorbis header type */
320 /* pack side **********************************************************/
322 static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){
324 _oggpack_write(opb,0x01,8);
325 _v_writestring(opb,"vorbis");
327 /* basic information about the stream */
328 _oggpack_write(opb,0x00,32);
329 _oggpack_write(opb,vi->channels,8);
330 _oggpack_write(opb,vi->rate,32);
332 _oggpack_write(opb,vi->bitrate_upper,32);
333 _oggpack_write(opb,vi->bitrate_nominal,32);
334 _oggpack_write(opb,vi->bitrate_lower,32);
336 _oggpack_write(opb,ilog2(vi->blocksizes[0]),4);
337 _oggpack_write(opb,ilog2(vi->blocksizes[1]),4);
338 _oggpack_write(opb,1,1);
343 static int _vorbis_pack_comment(oggpack_buffer *opb,vorbis_comment *vc){
344 char temp[]="Xiphophorus libVorbis I 20000223";
347 _oggpack_write(opb,0x03,8);
348 _v_writestring(opb,"vorbis");
351 _oggpack_write(opb,strlen(temp),32);
352 _v_writestring(opb,temp);
356 _oggpack_write(opb,vc->comments,32);
359 for(i=0;i<vc->comments;i++){
360 if(vc->user_comments[i]){
361 _oggpack_write(opb,strlen(vc->user_comments[i]),32);
362 _v_writestring(opb,vc->user_comments[i]);
364 _oggpack_write(opb,0,32);
368 _oggpack_write(opb,1,1);
373 static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){
375 _oggpack_write(opb,0x05,8);
376 _v_writestring(opb,"vorbis");
379 _oggpack_write(opb,vi->books-1,8);
380 for(i=0;i<vi->books;i++)
381 if(vorbis_staticbook_pack(vi->book_param[i],opb))goto err_out;
384 _oggpack_write(opb,vi->times-1,6);
385 for(i=0;i<vi->times;i++){
386 _oggpack_write(opb,vi->time_type[i],16);
387 _time_P[vi->time_type[i]]->pack(vi->time_param[i],opb);
391 _oggpack_write(opb,vi->floors-1,6);
392 for(i=0;i<vi->floors;i++){
393 _oggpack_write(opb,vi->floor_type[i],16);
394 _floor_P[vi->floor_type[i]]->pack(vi->floor_param[i],opb);
398 _oggpack_write(opb,vi->residues-1,6);
399 for(i=0;i<vi->residues;i++){
400 _oggpack_write(opb,vi->residue_type[i],16);
401 _residue_P[vi->residue_type[i]]->pack(vi->residue_param[i],opb);
405 _oggpack_write(opb,vi->maps-1,6);
406 for(i=0;i<vi->maps;i++){
407 _oggpack_write(opb,vi->map_type[i],16);
408 _mapping_P[vi->map_type[i]]->pack(vi,vi->map_param[i],opb);
412 _oggpack_write(opb,vi->modes-1,6);
413 for(i=0;i<vi->modes;i++){
414 _oggpack_write(opb,vi->mode_param[i]->blockflag,1);
415 _oggpack_write(opb,vi->mode_param[i]->windowtype,16);
416 _oggpack_write(opb,vi->mode_param[i]->transformtype,16);
417 _oggpack_write(opb,vi->mode_param[i]->mapping,8);
419 _oggpack_write(opb,1,1);
426 int vorbis_analysis_headerout(vorbis_dsp_state *v,
430 ogg_packet *op_code){
431 vorbis_info *vi=v->vi;
434 /* first header packet **********************************************/
436 _oggpack_writeinit(&opb);
437 if(_vorbis_pack_info(&opb,vi))goto err_out;
439 /* build the packet */
440 if(v->header)free(v->header);
441 v->header=malloc(_oggpack_bytes(&opb));
442 memcpy(v->header,opb.buffer,_oggpack_bytes(&opb));
443 op->packet=v->header;
444 op->bytes=_oggpack_bytes(&opb);
449 /* second header packet (comments) **********************************/
451 _oggpack_reset(&opb);
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 if(_vorbis_pack_books(&opb,vi))goto err_out;
468 if(v->header2)free(v->header2);
469 v->header2=malloc(_oggpack_bytes(&opb));
470 memcpy(v->header2,opb.buffer,_oggpack_bytes(&opb));
471 op_code->packet=v->header2;
472 op_code->bytes=_oggpack_bytes(&opb);
477 _oggpack_writeclear(&opb);
480 _oggpack_writeclear(&opb);
481 memset(op,0,sizeof(ogg_packet));
482 memset(op_comm,0,sizeof(ogg_packet));
483 memset(op_code,0,sizeof(ogg_packet));
485 if(v->header)free(v->header);
486 if(v->header1)free(v->header1);
487 if(v->header2)free(v->header2);