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.24 2000/05/08 20:49:48 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"
35 static int ilog2(unsigned int v){
44 static void _v_writestring(oggpack_buffer *o,char *s){
46 _oggpack_write(o,*s++,8);
50 static void _v_readstring(oggpack_buffer *o,char *buf,int bytes){
52 *buf++=_oggpack_read(o,8);
56 void vorbis_comment_init(vorbis_comment *vc){
57 memset(vc,0,sizeof(vorbis_comment));
60 void vorbis_comment_add(vorbis_comment *vc,char *comment){
61 vc->user_comments=realloc(vc->user_comments,
62 (vc->comments+2)*sizeof(char *));
63 vc->user_comments[vc->comments]=strdup(comment);
65 vc->user_comments[vc->comments]=NULL;
68 void vorbis_comment_clear(vorbis_comment *vc){
71 for(i=0;i<vc->comments;i++)
72 if(vc->user_comments[i])free(vc->user_comments[i]);
73 if(vc->user_comments)free(vc->user_comments);
74 if(vc->vendor)free(vc->vendor);
76 memset(vc,0,sizeof(vorbis_comment));
79 /* used by synthesis, which has a full, alloced vi */
80 void vorbis_info_init(vorbis_info *vi){
81 memset(vi,0,sizeof(vorbis_info));
84 void vorbis_info_clear(vorbis_info *vi){
87 for(i=0;i<vi->modes;i++)
88 if(vi->mode_param[i])free(vi->mode_param[i]);
89 /*if(vi->mode_param)free(vi->mode_param);*/
91 for(i=0;i<vi->maps;i++) /* unpack does the range checking */
92 _mapping_P[vi->map_type[i]]->free_info(vi->map_param[i]);
93 /*if(vi->map_param)free(vi->map_param);*/
95 for(i=0;i<vi->times;i++) /* unpack does the range checking */
96 _time_P[vi->time_type[i]]->free_info(vi->time_param[i]);
97 /*if(vi->time_param)free(vi->time_param);*/
99 for(i=0;i<vi->floors;i++) /* unpack does the range checking */
100 _floor_P[vi->floor_type[i]]->free_info(vi->floor_param[i]);
101 /*if(vi->floor_param)free(vi->floor_param);*/
103 for(i=0;i<vi->residues;i++) /* unpack does the range checking */
104 _residue_P[vi->residue_type[i]]->free_info(vi->residue_param[i]);
105 /*if(vi->residue_param)free(vi->residue_param);*/
107 /* the static codebooks *are* freed if you call info_clear, because
108 decode side does alloc a 'static' codebook. Calling clear on the
109 full codebook does not clear the static codebook (that's our
111 for(i=0;i<vi->books;i++){
112 /* just in case the decoder pre-cleared to save space */
113 if(vi->book_param[i]){
114 vorbis_staticbook_clear(vi->book_param[i]);
115 free(vi->book_param[i]);
118 /*if(vi->book_param)free(vi->book_param);*/
120 for(i=0;i<vi->psys;i++)
121 _vi_psy_free(vi->psy_param[i]);
122 /*if(vi->psy_param)free(vi->psy_param);*/
124 memset(vi,0,sizeof(vorbis_info));
127 /* Header packing/unpacking ********************************************/
129 static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
130 vi->version=_oggpack_read(opb,32);
131 if(vi->version!=0)return(-1);
133 vi->channels=_oggpack_read(opb,8);
134 vi->rate=_oggpack_read(opb,32);
136 vi->bitrate_upper=_oggpack_read(opb,32);
137 vi->bitrate_nominal=_oggpack_read(opb,32);
138 vi->bitrate_lower=_oggpack_read(opb,32);
140 vi->blocksizes[0]=1<<_oggpack_read(opb,4);
141 vi->blocksizes[1]=1<<_oggpack_read(opb,4);
143 if(vi->rate<1)goto err_out;
144 if(vi->channels<1)goto err_out;
145 if(vi->blocksizes[0]<8)goto err_out;
146 if(vi->blocksizes[1]<vi->blocksizes[0])goto err_out;
148 if(_oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
152 vorbis_info_clear(vi);
156 static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
158 int vendorlen=_oggpack_read(opb,32);
159 if(vendorlen<0)goto err_out;
160 vc->vendor=calloc(vendorlen+1,1);
161 _v_readstring(opb,vc->vendor,vendorlen);
162 vc->comments=_oggpack_read(opb,32);
163 if(vc->comments<0)goto err_out;
164 vc->user_comments=calloc(vc->comments+1,sizeof(char **));
166 for(i=0;i<vc->comments;i++){
167 int len=_oggpack_read(opb,32);
168 if(len<0)goto err_out;
169 vc->user_comments[i]=calloc(len+1,1);
170 _v_readstring(opb,vc->user_comments[i],len);
172 if(_oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
176 vorbis_comment_clear(vc);
180 /* all of the real encoding details are here. The modes, books,
182 static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
186 vi->books=_oggpack_read(opb,8)+1;
187 /*vi->book_param=calloc(vi->books,sizeof(static_codebook *));*/
188 for(i=0;i<vi->books;i++){
189 vi->book_param[i]=calloc(1,sizeof(static_codebook));
190 if(vorbis_staticbook_unpack(opb,vi->book_param[i]))goto err_out;
193 /* time backend settings */
194 vi->times=_oggpack_read(opb,6)+1;
195 /*vi->time_type=malloc(vi->times*sizeof(int));*/
196 /*vi->time_param=calloc(vi->times,sizeof(void *));*/
197 for(i=0;i<vi->times;i++){
198 vi->time_type[i]=_oggpack_read(opb,16);
199 if(vi->time_type[i]<0 || vi->time_type[i]>=VI_TIMEB)goto err_out;
200 vi->time_param[i]=_time_P[vi->time_type[i]]->unpack(vi,opb);
201 if(!vi->time_param[i])goto err_out;
204 /* floor backend settings */
205 vi->floors=_oggpack_read(opb,6)+1;
206 /*vi->floor_type=malloc(vi->floors*sizeof(int));*/
207 /*vi->floor_param=calloc(vi->floors,sizeof(void *));*/
208 for(i=0;i<vi->floors;i++){
209 vi->floor_type[i]=_oggpack_read(opb,16);
210 if(vi->floor_type[i]<0 || vi->floor_type[i]>=VI_FLOORB)goto err_out;
211 vi->floor_param[i]=_floor_P[vi->floor_type[i]]->unpack(vi,opb);
212 if(!vi->floor_param[i])goto err_out;
215 /* residue backend settings */
216 vi->residues=_oggpack_read(opb,6)+1;
217 /*vi->residue_type=malloc(vi->residues*sizeof(int));*/
218 /*vi->residue_param=calloc(vi->residues,sizeof(void *));*/
219 for(i=0;i<vi->residues;i++){
220 vi->residue_type[i]=_oggpack_read(opb,16);
221 if(vi->residue_type[i]<0 || vi->residue_type[i]>=VI_RESB)goto err_out;
222 vi->residue_param[i]=_residue_P[vi->residue_type[i]]->unpack(vi,opb);
223 if(!vi->residue_param[i])goto err_out;
226 /* map backend settings */
227 vi->maps=_oggpack_read(opb,6)+1;
228 /*vi->map_type=malloc(vi->maps*sizeof(int));*/
229 /*vi->map_param=calloc(vi->maps,sizeof(void *));*/
230 for(i=0;i<vi->maps;i++){
231 vi->map_type[i]=_oggpack_read(opb,16);
232 if(vi->map_type[i]<0 || vi->map_type[i]>=VI_MAPB)goto err_out;
233 vi->map_param[i]=_mapping_P[vi->map_type[i]]->unpack(vi,opb);
234 if(!vi->map_param[i])goto err_out;
238 vi->modes=_oggpack_read(opb,6)+1;
239 /*vi->mode_param=calloc(vi->modes,sizeof(void *));*/
240 for(i=0;i<vi->modes;i++){
241 vi->mode_param[i]=calloc(1,sizeof(vorbis_info_mode));
242 vi->mode_param[i]->blockflag=_oggpack_read(opb,1);
243 vi->mode_param[i]->windowtype=_oggpack_read(opb,16);
244 vi->mode_param[i]->transformtype=_oggpack_read(opb,16);
245 vi->mode_param[i]->mapping=_oggpack_read(opb,8);
247 if(vi->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
248 if(vi->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
249 if(vi->mode_param[i]->mapping>=vi->maps)goto err_out;
252 if(_oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
256 vorbis_info_clear(vi);
260 /* The Vorbis header is in three packets; the initial small packet in
261 the first page that identifies basic parameters, a second packet
262 with bitstream comments and a third packet that holds the
265 int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
269 _oggpack_readinit(&opb,op->packet,op->bytes);
271 /* Which of the three types of header is this? */
272 /* Also verify header-ness, vorbis */
275 int packtype=_oggpack_read(&opb,8);
277 _v_readstring(&opb,buffer,6);
278 if(memcmp(buffer,"vorbis",6)){
279 /* not a vorbis header */
283 case 0x01: /* least significant *bit* is read first */
285 /* Not the initial packet */
289 /* previously initialized info header */
293 return(_vorbis_unpack_info(vi,&opb));
295 case 0x03: /* least significant *bit* is read first */
297 /* um... we didn't get the initial header */
301 return(_vorbis_unpack_comment(vc,&opb));
303 case 0x05: /* least significant *bit* is read first */
304 if(vi->rate==0 || vc->vendor==NULL){
305 /* um... we didn;t get the initial header or comments yet */
309 return(_vorbis_unpack_books(vi,&opb));
312 /* Not a valid vorbis header type */
321 /* pack side **********************************************************/
323 static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){
325 _oggpack_write(opb,0x01,8);
326 _v_writestring(opb,"vorbis");
328 /* basic information about the stream */
329 _oggpack_write(opb,0x00,32);
330 _oggpack_write(opb,vi->channels,8);
331 _oggpack_write(opb,vi->rate,32);
333 _oggpack_write(opb,vi->bitrate_upper,32);
334 _oggpack_write(opb,vi->bitrate_nominal,32);
335 _oggpack_write(opb,vi->bitrate_lower,32);
337 _oggpack_write(opb,ilog2(vi->blocksizes[0]),4);
338 _oggpack_write(opb,ilog2(vi->blocksizes[1]),4);
339 _oggpack_write(opb,1,1);
344 static int _vorbis_pack_comment(oggpack_buffer *opb,vorbis_comment *vc){
345 char temp[]="Xiphophorus libVorbis I 20000508";
348 _oggpack_write(opb,0x03,8);
349 _v_writestring(opb,"vorbis");
352 _oggpack_write(opb,strlen(temp),32);
353 _v_writestring(opb,temp);
357 _oggpack_write(opb,vc->comments,32);
360 for(i=0;i<vc->comments;i++){
361 if(vc->user_comments[i]){
362 _oggpack_write(opb,strlen(vc->user_comments[i]),32);
363 _v_writestring(opb,vc->user_comments[i]);
365 _oggpack_write(opb,0,32);
369 _oggpack_write(opb,1,1);
374 static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){
376 _oggpack_write(opb,0x05,8);
377 _v_writestring(opb,"vorbis");
380 _oggpack_write(opb,vi->books-1,8);
381 for(i=0;i<vi->books;i++)
382 if(vorbis_staticbook_pack(vi->book_param[i],opb))goto err_out;
385 _oggpack_write(opb,vi->times-1,6);
386 for(i=0;i<vi->times;i++){
387 _oggpack_write(opb,vi->time_type[i],16);
388 _time_P[vi->time_type[i]]->pack(vi->time_param[i],opb);
392 _oggpack_write(opb,vi->floors-1,6);
393 for(i=0;i<vi->floors;i++){
394 _oggpack_write(opb,vi->floor_type[i],16);
395 _floor_P[vi->floor_type[i]]->pack(vi->floor_param[i],opb);
399 _oggpack_write(opb,vi->residues-1,6);
400 for(i=0;i<vi->residues;i++){
401 _oggpack_write(opb,vi->residue_type[i],16);
402 _residue_P[vi->residue_type[i]]->pack(vi->residue_param[i],opb);
406 _oggpack_write(opb,vi->maps-1,6);
407 for(i=0;i<vi->maps;i++){
408 _oggpack_write(opb,vi->map_type[i],16);
409 _mapping_P[vi->map_type[i]]->pack(vi,vi->map_param[i],opb);
413 _oggpack_write(opb,vi->modes-1,6);
414 for(i=0;i<vi->modes;i++){
415 _oggpack_write(opb,vi->mode_param[i]->blockflag,1);
416 _oggpack_write(opb,vi->mode_param[i]->windowtype,16);
417 _oggpack_write(opb,vi->mode_param[i]->transformtype,16);
418 _oggpack_write(opb,vi->mode_param[i]->mapping,8);
420 _oggpack_write(opb,1,1);
427 int vorbis_analysis_headerout(vorbis_dsp_state *v,
431 ogg_packet *op_code){
432 vorbis_info *vi=v->vi;
435 /* first header packet **********************************************/
437 _oggpack_writeinit(&opb);
438 if(_vorbis_pack_info(&opb,vi))goto err_out;
440 /* build the packet */
441 if(v->header)free(v->header);
442 v->header=malloc(_oggpack_bytes(&opb));
443 memcpy(v->header,opb.buffer,_oggpack_bytes(&opb));
444 op->packet=v->header;
445 op->bytes=_oggpack_bytes(&opb);
450 /* second header packet (comments) **********************************/
452 _oggpack_reset(&opb);
453 if(_vorbis_pack_comment(&opb,vc))goto err_out;
455 if(v->header1)free(v->header1);
456 v->header1=malloc(_oggpack_bytes(&opb));
457 memcpy(v->header1,opb.buffer,_oggpack_bytes(&opb));
458 op_comm->packet=v->header1;
459 op_comm->bytes=_oggpack_bytes(&opb);
464 /* third header packet (modes/codebooks) ****************************/
466 _oggpack_reset(&opb);
467 if(_vorbis_pack_books(&opb,vi))goto err_out;
469 if(v->header2)free(v->header2);
470 v->header2=malloc(_oggpack_bytes(&opb));
471 memcpy(v->header2,opb.buffer,_oggpack_bytes(&opb));
472 op_code->packet=v->header2;
473 op_code->bytes=_oggpack_bytes(&opb);
478 _oggpack_writeclear(&opb);
481 _oggpack_writeclear(&opb);
482 memset(op,0,sizeof(ogg_packet));
483 memset(op_comm,0,sizeof(ogg_packet));
484 memset(op_code,0,sizeof(ogg_packet));
486 if(v->header)free(v->header);
487 if(v->header1)free(v->header1);
488 if(v->header2)free(v->header2);