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 vc
->user_comments
[i
]=(char *)_ogg_calloc(len
+1,1);
156 _v_readstring(opb
,vc
->user_comments
[i
],len
);
158 if(oggpack_read(opb
,1)!=1)goto err_out
; /* EOP check */
162 vorbis_comment_clear(vc
);
163 return(OV_EBADHEADER
);
166 /* all of the real encoding details are here. The modes, books,
168 static int _vorbis_unpack_books(vorbis_info
*vi
,oggpack_buffer
*opb
){
169 codec_setup_info
*ci
=(codec_setup_info
*)vi
->codec_setup
;
171 if(!ci
)return(OV_EFAULT
);
174 ci
->books
=oggpack_read(opb
,8)+1;
175 /*ci->book_param=_ogg_calloc(ci->books,sizeof(*ci->book_param));*/
176 for(i
=0;i
<ci
->books
;i
++){
177 ci
->book_param
[i
]=(static_codebook
*)_ogg_calloc(1,sizeof(*ci
->book_param
[i
]));
178 if(vorbis_staticbook_unpack(opb
,ci
->book_param
[i
]))goto err_out
;
181 /* time backend settings */
182 ci
->times
=oggpack_read(opb
,6)+1;
183 /*ci->time_type=_ogg_malloc(ci->times*sizeof(*ci->time_type));*/
184 /*ci->time_param=_ogg_calloc(ci->times,sizeof(void *));*/
185 for(i
=0;i
<ci
->times
;i
++){
186 ci
->time_type
[i
]=oggpack_read(opb
,16);
187 if(ci
->time_type
[i
]<0 || ci
->time_type
[i
]>=VI_TIMEB
)goto err_out
;
188 /* ci->time_param[i]=_time_P[ci->time_type[i]]->unpack(vi,opb);
189 Vorbis I has no time backend */
190 /*if(!ci->time_param[i])goto err_out;*/
193 /* floor backend settings */
194 ci
->floors
=oggpack_read(opb
,6)+1;
195 /*ci->floor_type=_ogg_malloc(ci->floors*sizeof(*ci->floor_type));*/
196 /*ci->floor_param=_ogg_calloc(ci->floors,sizeof(void *));*/
197 for(i
=0;i
<ci
->floors
;i
++){
198 ci
->floor_type
[i
]=oggpack_read(opb
,16);
199 if(ci
->floor_type
[i
]<0 || ci
->floor_type
[i
]>=VI_FLOORB
)goto err_out
;
200 ci
->floor_param
[i
]=_floor_P
[ci
->floor_type
[i
]]->unpack(vi
,opb
);
201 if(!ci
->floor_param
[i
])goto err_out
;
204 /* residue backend settings */
205 ci
->residues
=oggpack_read(opb
,6)+1;
206 /*ci->residue_type=_ogg_malloc(ci->residues*sizeof(*ci->residue_type));*/
207 /*ci->residue_param=_ogg_calloc(ci->residues,sizeof(void *));*/
208 for(i
=0;i
<ci
->residues
;i
++){
209 ci
->residue_type
[i
]=oggpack_read(opb
,16);
210 if(ci
->residue_type
[i
]<0 || ci
->residue_type
[i
]>=VI_RESB
)goto err_out
;
211 ci
->residue_param
[i
]=_residue_P
[ci
->residue_type
[i
]]->unpack(vi
,opb
);
212 if(!ci
->residue_param
[i
])goto err_out
;
215 /* map backend settings */
216 ci
->maps
=oggpack_read(opb
,6)+1;
217 /*ci->map_type=_ogg_malloc(ci->maps*sizeof(*ci->map_type));*/
218 /*ci->map_param=_ogg_calloc(ci->maps,sizeof(void *));*/
219 for(i
=0;i
<ci
->maps
;i
++){
220 ci
->map_type
[i
]=oggpack_read(opb
,16);
221 if(ci
->map_type
[i
]<0 || ci
->map_type
[i
]>=VI_MAPB
)goto err_out
;
222 ci
->map_param
[i
]=_mapping_P
[ci
->map_type
[i
]]->unpack(vi
,opb
);
223 if(!ci
->map_param
[i
])goto err_out
;
227 ci
->modes
=oggpack_read(opb
,6)+1;
228 /*vi->mode_param=_ogg_calloc(vi->modes,sizeof(void *));*/
229 for(i
=0;i
<ci
->modes
;i
++){
230 ci
->mode_param
[i
]=(vorbis_info_mode
*)_ogg_calloc(1,sizeof(*ci
->mode_param
[i
]));
231 ci
->mode_param
[i
]->blockflag
=oggpack_read(opb
,1);
232 ci
->mode_param
[i
]->windowtype
=oggpack_read(opb
,16);
233 ci
->mode_param
[i
]->transformtype
=oggpack_read(opb
,16);
234 ci
->mode_param
[i
]->mapping
=oggpack_read(opb
,8);
236 if(ci
->mode_param
[i
]->windowtype
>=VI_WINDOWB
)goto err_out
;
237 if(ci
->mode_param
[i
]->transformtype
>=VI_WINDOWB
)goto err_out
;
238 if(ci
->mode_param
[i
]->mapping
>=ci
->maps
)goto err_out
;
241 if(oggpack_read(opb
,1)!=1)goto err_out
; /* top level EOP check */
245 vorbis_info_clear(vi
);
246 return(OV_EBADHEADER
);
249 /* The Vorbis header is in three packets; the initial small packet in
250 the first page that identifies basic parameters, a second packet
251 with bitstream comments and a third packet that holds the
254 int vorbis_synthesis_headerin(vorbis_info
*vi
,vorbis_comment
*vc
,ogg_packet
*op
){
258 oggpack_readinit(&opb
,op
->packet
);
260 /* Which of the three types of header is this? */
261 /* Also verify header-ness, vorbis */
264 int packtype
=oggpack_read(&opb
,8);
266 _v_readstring(&opb
,buffer
,6);
267 if(memcmp(buffer
,"vorbis",6)){
268 /* not a vorbis header */
269 return(OV_ENOTVORBIS
);
272 case 0x01: /* least significant *bit* is read first */
274 /* Not the initial packet */
275 return(OV_EBADHEADER
);
278 /* previously initialized info header */
279 return(OV_EBADHEADER
);
282 return(_vorbis_unpack_info(vi
,&opb
));
284 case 0x03: /* least significant *bit* is read first */
286 /* um... we didn't get the initial header */
287 return(OV_EBADHEADER
);
290 return(_vorbis_unpack_comment(vc
,&opb
));
292 case 0x05: /* least significant *bit* is read first */
293 if(vi
->rate
==0 || vc
->vendor
==NULL
){
294 /* um... we didn;t get the initial header or comments yet */
295 return(OV_EBADHEADER
);
298 return(_vorbis_unpack_books(vi
,&opb
));
301 /* Not a valid vorbis header type */
302 return(OV_EBADHEADER
);
307 return(OV_EBADHEADER
);