1 /********************************************************************
3 * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE. *
5 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
6 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
7 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
9 * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2003 *
10 * BY THE Xiph.Org FOUNDATION http://www.xiph.org/ *
12 ********************************************************************
14 function: maintain the info structure, info <-> header packets
16 ********************************************************************/
18 /* general handling of the header and the vorbis_info structure (and
21 #include "config-tremor.h"
25 #include "ivorbiscodec.h"
26 #include "codec_internal.h"
34 static void _v_readstring(oggpack_buffer
*o
,char *buf
,int bytes
){
36 *buf
++=oggpack_read(o
,8);
40 void vorbis_comment_init(vorbis_comment
*vc
){
41 memset(vc
,0,sizeof(*vc
));
44 void vorbis_comment_clear(vorbis_comment
*vc
){
47 for(i
=0;i
<vc
->comments
;i
++)
48 if(vc
->user_comments
[i
])_ogg_free(vc
->user_comments
[i
]);
49 if(vc
->user_comments
)_ogg_free(vc
->user_comments
);
50 if(vc
->comment_lengths
)_ogg_free(vc
->comment_lengths
);
51 if(vc
->vendor
)_ogg_free(vc
->vendor
);
52 memset(vc
,0,sizeof(*vc
));
56 /* blocksize 0 is guaranteed to be short, 1 is guarantted to be long.
57 They may be equal, but short will never ge greater than long */
58 int vorbis_info_blocksize(vorbis_info
*vi
,int zo
){
59 codec_setup_info
*ci
= (codec_setup_info
*)vi
->codec_setup
;
60 return ci
? ci
->blocksizes
[zo
] : -1;
63 /* used by synthesis, which has a full, alloced vi */
64 void vorbis_info_init(vorbis_info
*vi
){
65 memset(vi
,0,sizeof(*vi
));
66 vi
->codec_setup
=(codec_setup_info
*)_ogg_calloc(1,sizeof(codec_setup_info
));
69 void vorbis_info_clear(vorbis_info
*vi
){
70 codec_setup_info
*ci
=(codec_setup_info
*)vi
->codec_setup
;
75 for(i
=0;i
<ci
->modes
;i
++)
76 if(ci
->mode_param
[i
])_ogg_free(ci
->mode_param
[i
]);
78 for(i
=0;i
<ci
->maps
;i
++) /* unpack does the range checking */
80 _mapping_P
[ci
->map_type
[i
]]->free_info(ci
->map_param
[i
]);
82 for(i
=0;i
<ci
->floors
;i
++) /* unpack does the range checking */
83 if(ci
->floor_param
[i
])
84 _floor_P
[ci
->floor_type
[i
]]->free_info(ci
->floor_param
[i
]);
86 for(i
=0;i
<ci
->residues
;i
++) /* unpack does the range checking */
87 if(ci
->residue_param
[i
])
88 _residue_P
[ci
->residue_type
[i
]]->free_info(ci
->residue_param
[i
]);
90 for(i
=0;i
<ci
->books
;i
++){
91 if(ci
->book_param
[i
]){
92 /* knows if the book was not alloced */
93 vorbis_staticbook_destroy(ci
->book_param
[i
]);
96 vorbis_book_clear(ci
->fullbooks
+i
);
99 _ogg_free(ci
->fullbooks
);
104 memset(vi
,0,sizeof(*vi
));
107 /* Header packing/unpacking ********************************************/
109 static int _vorbis_unpack_info(vorbis_info
*vi
,oggpack_buffer
*opb
){
110 codec_setup_info
*ci
=(codec_setup_info
*)vi
->codec_setup
;
111 if(!ci
)return(OV_EFAULT
);
113 vi
->version
=oggpack_read(opb
,32);
114 if(vi
->version
!=0)return(OV_EVERSION
);
116 vi
->channels
=oggpack_read(opb
,8);
117 vi
->rate
=oggpack_read(opb
,32);
119 vi
->bitrate_upper
=oggpack_read(opb
,32);
120 vi
->bitrate_nominal
=oggpack_read(opb
,32);
121 vi
->bitrate_lower
=oggpack_read(opb
,32);
123 ci
->blocksizes_nbits
[0]=oggpack_read(opb
,4);
124 ci
->blocksizes_nbits
[1]=oggpack_read(opb
,4);
125 ci
->blocksizes
[0]=1<<(ci
->blocksizes_nbits
[0]);
126 ci
->blocksizes
[1]=1<<(ci
->blocksizes_nbits
[1]);
128 if(vi
->rate
<1)goto err_out
;
129 if(vi
->channels
<1)goto err_out
;
130 if(ci
->blocksizes
[0]<64)goto err_out
;
131 if(ci
->blocksizes
[1]<ci
->blocksizes
[0])goto err_out
;
132 if(ci
->blocksizes
[1]>8192)goto err_out
;
134 if(oggpack_read(opb
,1)!=1)goto err_out
; /* EOP check */
138 vorbis_info_clear(vi
);
139 return(OV_EBADHEADER
);
142 static int _vorbis_unpack_comment(vorbis_comment
*vc
,oggpack_buffer
*opb
){
143 int vendorlen
=oggpack_read(opb
,32);
144 if(vendorlen
<0)goto err_out
;
145 vc
->vendor
=(char *)_ogg_calloc(vendorlen
+1,1);
146 _v_readstring(opb
,vc
->vendor
,vendorlen
);
150 vorbis_comment_clear(vc
);
151 return(OV_EBADHEADER
);
154 /* all of the real encoding details are here. The modes, books,
156 static int _vorbis_unpack_books(vorbis_info
*vi
,oggpack_buffer
*opb
){
157 codec_setup_info
*ci
=(codec_setup_info
*)vi
->codec_setup
;
159 if(!ci
)return(OV_EFAULT
);
162 ci
->books
=oggpack_read(opb
,8)+1;
163 /*ci->book_param=_ogg_calloc(ci->books,sizeof(*ci->book_param));*/
164 for(i
=0;i
<ci
->books
;i
++){
165 ci
->book_param
[i
]=(static_codebook
*)_ogg_calloc(1,sizeof(*ci
->book_param
[i
]));
166 if(vorbis_staticbook_unpack(opb
,ci
->book_param
[i
]))goto err_out
;
169 /* time backend settings */
170 ci
->times
=oggpack_read(opb
,6)+1;
171 /*ci->time_type=_ogg_malloc(ci->times*sizeof(*ci->time_type));*/
172 /*ci->time_param=_ogg_calloc(ci->times,sizeof(void *));*/
173 for(i
=0;i
<ci
->times
;i
++){
174 ci
->time_type
[i
]=oggpack_read(opb
,16);
175 if(ci
->time_type
[i
]<0 || ci
->time_type
[i
]>=VI_TIMEB
)goto err_out
;
176 /* ci->time_param[i]=_time_P[ci->time_type[i]]->unpack(vi,opb);
177 Vorbis I has no time backend */
178 /*if(!ci->time_param[i])goto err_out;*/
181 /* floor backend settings */
182 ci
->floors
=oggpack_read(opb
,6)+1;
183 /*ci->floor_type=_ogg_malloc(ci->floors*sizeof(*ci->floor_type));*/
184 /*ci->floor_param=_ogg_calloc(ci->floors,sizeof(void *));*/
185 for(i
=0;i
<ci
->floors
;i
++){
186 ci
->floor_type
[i
]=oggpack_read(opb
,16);
187 if(ci
->floor_type
[i
]<0 || ci
->floor_type
[i
]>=VI_FLOORB
)goto err_out
;
188 ci
->floor_param
[i
]=_floor_P
[ci
->floor_type
[i
]]->unpack(vi
,opb
);
189 if(!ci
->floor_param
[i
])goto err_out
;
192 /* residue backend settings */
193 ci
->residues
=oggpack_read(opb
,6)+1;
194 /*ci->residue_type=_ogg_malloc(ci->residues*sizeof(*ci->residue_type));*/
195 /*ci->residue_param=_ogg_calloc(ci->residues,sizeof(void *));*/
196 for(i
=0;i
<ci
->residues
;i
++){
197 ci
->residue_type
[i
]=oggpack_read(opb
,16);
198 if(ci
->residue_type
[i
]<0 || ci
->residue_type
[i
]>=VI_RESB
)goto err_out
;
199 ci
->residue_param
[i
]=_residue_P
[ci
->residue_type
[i
]]->unpack(vi
,opb
);
200 if(!ci
->residue_param
[i
])goto err_out
;
203 /* map backend settings */
204 ci
->maps
=oggpack_read(opb
,6)+1;
205 /*ci->map_type=_ogg_malloc(ci->maps*sizeof(*ci->map_type));*/
206 /*ci->map_param=_ogg_calloc(ci->maps,sizeof(void *));*/
207 for(i
=0;i
<ci
->maps
;i
++){
208 ci
->map_type
[i
]=oggpack_read(opb
,16);
209 if(ci
->map_type
[i
]<0 || ci
->map_type
[i
]>=VI_MAPB
)goto err_out
;
210 ci
->map_param
[i
]=_mapping_P
[ci
->map_type
[i
]]->unpack(vi
,opb
);
211 if(!ci
->map_param
[i
])goto err_out
;
215 ci
->modes
=oggpack_read(opb
,6)+1;
216 /*vi->mode_param=_ogg_calloc(vi->modes,sizeof(void *));*/
217 for(i
=0;i
<ci
->modes
;i
++){
218 ci
->mode_param
[i
]=(vorbis_info_mode
*)_ogg_calloc(1,sizeof(*ci
->mode_param
[i
]));
219 ci
->mode_param
[i
]->blockflag
=oggpack_read(opb
,1);
220 ci
->mode_param
[i
]->windowtype
=oggpack_read(opb
,16);
221 ci
->mode_param
[i
]->transformtype
=oggpack_read(opb
,16);
222 ci
->mode_param
[i
]->mapping
=oggpack_read(opb
,8);
224 if(ci
->mode_param
[i
]->windowtype
>=VI_WINDOWB
)goto err_out
;
225 if(ci
->mode_param
[i
]->transformtype
>=VI_WINDOWB
)goto err_out
;
226 if(ci
->mode_param
[i
]->mapping
>=ci
->maps
)goto err_out
;
229 if(oggpack_read(opb
,1)!=1)goto err_out
; /* top level EOP check */
233 vorbis_info_clear(vi
);
234 return(OV_EBADHEADER
);
237 /* Is this packet a vorbis ID header? */
238 int vorbis_synthesis_idheader(ogg_packet
*op
){
243 oggpack_readinit(&opb
,op
->packet
);
246 return(0); /* Not the initial packet */
248 if(oggpack_read(&opb
,8) != 1)
249 return 0; /* not an ID header */
252 _v_readstring(&opb
,buffer
,6);
253 if(memcmp(buffer
,"vorbis",6))
254 return 0; /* not vorbis */
262 /* The Vorbis header is in three packets; the initial small packet in
263 the first page that identifies basic parameters, a second packet
264 with bitstream comments and a third packet that holds the
267 int vorbis_synthesis_headerin(vorbis_info
*vi
,vorbis_comment
*vc
,ogg_packet
*op
){
271 oggpack_readinit(&opb
,op
->packet
);
273 /* Which of the three types of header is this? */
274 /* Also verify header-ness, vorbis */
277 int packtype
=oggpack_read(&opb
,8);
279 _v_readstring(&opb
,buffer
,6);
280 if(memcmp(buffer
,"vorbis",6)){
281 /* not a vorbis header */
282 return(OV_ENOTVORBIS
);
285 case 0x01: /* least significant *bit* is read first */
287 /* Not the initial packet */
288 return(OV_EBADHEADER
);
291 /* previously initialized info header */
292 return(OV_EBADHEADER
);
295 return(_vorbis_unpack_info(vi
,&opb
));
297 case 0x03: /* least significant *bit* is read first */
299 /* um... we didn't get the initial header */
300 return(OV_EBADHEADER
);
303 return(_vorbis_unpack_comment(vc
,&opb
));
305 case 0x05: /* least significant *bit* is read first */
306 if(vi
->rate
==0 || vc
->vendor
==NULL
){
307 /* um... we didn;t get the initial header or comments yet */
308 return(OV_EBADHEADER
);
311 return(_vorbis_unpack_books(vi
,&opb
));
314 /* Not a valid vorbis header type */
315 return(OV_EBADHEADER
);
320 return(OV_EBADHEADER
);