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
[0]=1<<oggpack_read(opb
,4);
124 ci
->blocksizes
[1]=1<<oggpack_read(opb
,4);
126 if(vi
->rate
<1)goto err_out
;
127 if(vi
->channels
<1)goto err_out
;
128 if(ci
->blocksizes
[0]<64)goto err_out
;
129 if(ci
->blocksizes
[1]<ci
->blocksizes
[0])goto err_out
;
130 if(ci
->blocksizes
[1]>8192)goto err_out
;
132 if(oggpack_read(opb
,1)!=1)goto err_out
; /* EOP check */
136 vorbis_info_clear(vi
);
137 return(OV_EBADHEADER
);
140 static int _vorbis_unpack_comment(vorbis_comment
*vc
,oggpack_buffer
*opb
){
142 int vendorlen
=oggpack_read(opb
,32);
143 if(vendorlen
<0)goto err_out
;
144 vc
->vendor
=(char *)_ogg_calloc(vendorlen
+1,1);
145 _v_readstring(opb
,vc
->vendor
,vendorlen
);
146 vc
->comments
=oggpack_read(opb
,32);
147 if(vc
->comments
<0)goto err_out
;
148 vc
->user_comments
=(char **)_ogg_calloc(vc
->comments
+1,sizeof(*vc
->user_comments
));
149 vc
->comment_lengths
=(int *)_ogg_calloc(vc
->comments
+1, sizeof(*vc
->comment_lengths
));
151 for(i
=0;i
<vc
->comments
;i
++){
152 int len
=oggpack_read(opb
,32);
153 if(len
<0)goto err_out
;
154 vc
->comment_lengths
[i
]=len
;
155 if(len
>10000){ /*truncate long comments rather then seg faulting*/
156 vc
->user_comments
[i
]=(char *)_ogg_calloc(10001,1);
157 _v_readstring(opb
,vc
->user_comments
[i
],10000);
158 /*just to be neat, consumed and discard the rest of the comment*/
163 vc
->user_comments
[i
]=(char *)_ogg_calloc(len
+1,1);
164 _v_readstring(opb
,vc
->user_comments
[i
],len
);
167 if(oggpack_read(opb
,1)!=1)goto err_out
; /* EOP check */
171 vorbis_comment_clear(vc
);
172 return(OV_EBADHEADER
);
175 /* all of the real encoding details are here. The modes, books,
177 static int _vorbis_unpack_books(vorbis_info
*vi
,oggpack_buffer
*opb
){
178 codec_setup_info
*ci
=(codec_setup_info
*)vi
->codec_setup
;
180 if(!ci
)return(OV_EFAULT
);
183 ci
->books
=oggpack_read(opb
,8)+1;
184 /*ci->book_param=_ogg_calloc(ci->books,sizeof(*ci->book_param));*/
185 for(i
=0;i
<ci
->books
;i
++){
186 ci
->book_param
[i
]=(static_codebook
*)_ogg_calloc(1,sizeof(*ci
->book_param
[i
]));
187 if(vorbis_staticbook_unpack(opb
,ci
->book_param
[i
]))goto err_out
;
190 /* time backend settings */
191 ci
->times
=oggpack_read(opb
,6)+1;
192 /*ci->time_type=_ogg_malloc(ci->times*sizeof(*ci->time_type));*/
193 /*ci->time_param=_ogg_calloc(ci->times,sizeof(void *));*/
194 for(i
=0;i
<ci
->times
;i
++){
195 ci
->time_type
[i
]=oggpack_read(opb
,16);
196 if(ci
->time_type
[i
]<0 || ci
->time_type
[i
]>=VI_TIMEB
)goto err_out
;
197 /* ci->time_param[i]=_time_P[ci->time_type[i]]->unpack(vi,opb);
198 Vorbis I has no time backend */
199 /*if(!ci->time_param[i])goto err_out;*/
202 /* floor backend settings */
203 ci
->floors
=oggpack_read(opb
,6)+1;
204 /*ci->floor_type=_ogg_malloc(ci->floors*sizeof(*ci->floor_type));*/
205 /*ci->floor_param=_ogg_calloc(ci->floors,sizeof(void *));*/
206 for(i
=0;i
<ci
->floors
;i
++){
207 ci
->floor_type
[i
]=oggpack_read(opb
,16);
208 if(ci
->floor_type
[i
]<0 || ci
->floor_type
[i
]>=VI_FLOORB
)goto err_out
;
209 ci
->floor_param
[i
]=_floor_P
[ci
->floor_type
[i
]]->unpack(vi
,opb
);
210 if(!ci
->floor_param
[i
])goto err_out
;
213 /* residue backend settings */
214 ci
->residues
=oggpack_read(opb
,6)+1;
215 /*ci->residue_type=_ogg_malloc(ci->residues*sizeof(*ci->residue_type));*/
216 /*ci->residue_param=_ogg_calloc(ci->residues,sizeof(void *));*/
217 for(i
=0;i
<ci
->residues
;i
++){
218 ci
->residue_type
[i
]=oggpack_read(opb
,16);
219 if(ci
->residue_type
[i
]<0 || ci
->residue_type
[i
]>=VI_RESB
)goto err_out
;
220 ci
->residue_param
[i
]=_residue_P
[ci
->residue_type
[i
]]->unpack(vi
,opb
);
221 if(!ci
->residue_param
[i
])goto err_out
;
224 /* map backend settings */
225 ci
->maps
=oggpack_read(opb
,6)+1;
226 /*ci->map_type=_ogg_malloc(ci->maps*sizeof(*ci->map_type));*/
227 /*ci->map_param=_ogg_calloc(ci->maps,sizeof(void *));*/
228 for(i
=0;i
<ci
->maps
;i
++){
229 ci
->map_type
[i
]=oggpack_read(opb
,16);
230 if(ci
->map_type
[i
]<0 || ci
->map_type
[i
]>=VI_MAPB
)goto err_out
;
231 ci
->map_param
[i
]=_mapping_P
[ci
->map_type
[i
]]->unpack(vi
,opb
);
232 if(!ci
->map_param
[i
])goto err_out
;
236 ci
->modes
=oggpack_read(opb
,6)+1;
237 /*vi->mode_param=_ogg_calloc(vi->modes,sizeof(void *));*/
238 for(i
=0;i
<ci
->modes
;i
++){
239 ci
->mode_param
[i
]=(vorbis_info_mode
*)_ogg_calloc(1,sizeof(*ci
->mode_param
[i
]));
240 ci
->mode_param
[i
]->blockflag
=oggpack_read(opb
,1);
241 ci
->mode_param
[i
]->windowtype
=oggpack_read(opb
,16);
242 ci
->mode_param
[i
]->transformtype
=oggpack_read(opb
,16);
243 ci
->mode_param
[i
]->mapping
=oggpack_read(opb
,8);
245 if(ci
->mode_param
[i
]->windowtype
>=VI_WINDOWB
)goto err_out
;
246 if(ci
->mode_param
[i
]->transformtype
>=VI_WINDOWB
)goto err_out
;
247 if(ci
->mode_param
[i
]->mapping
>=ci
->maps
)goto err_out
;
250 if(oggpack_read(opb
,1)!=1)goto err_out
; /* top level EOP check */
254 vorbis_info_clear(vi
);
255 return(OV_EBADHEADER
);
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
);
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 */
278 return(OV_ENOTVORBIS
);
281 case 0x01: /* least significant *bit* is read first */
283 /* Not the initial packet */
284 return(OV_EBADHEADER
);
287 /* previously initialized info header */
288 return(OV_EBADHEADER
);
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 */
296 return(OV_EBADHEADER
);
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 */
304 return(OV_EBADHEADER
);
307 return(_vorbis_unpack_books(vi
,&opb
));
310 /* Not a valid vorbis header type */
311 return(OV_EBADHEADER
);
316 return(OV_EBADHEADER
);