rev |
line source |
nuclear@1
|
1 /********************************************************************
|
nuclear@1
|
2 * *
|
nuclear@1
|
3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
|
nuclear@1
|
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
|
nuclear@1
|
5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
|
nuclear@1
|
6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
|
nuclear@1
|
7 * *
|
nuclear@1
|
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2010 *
|
nuclear@1
|
9 * by the Xiph.Org Foundation http://www.xiph.org/ *
|
nuclear@1
|
10 * *
|
nuclear@1
|
11 ********************************************************************
|
nuclear@1
|
12
|
nuclear@1
|
13 function: maintain the info structure, info <-> header packets
|
nuclear@1
|
14 last mod: $Id: info.c 18186 2012-02-03 22:08:44Z xiphmont $
|
nuclear@1
|
15
|
nuclear@1
|
16 ********************************************************************/
|
nuclear@1
|
17
|
nuclear@1
|
18 /* general handling of the header and the vorbis_info structure (and
|
nuclear@1
|
19 substructures) */
|
nuclear@1
|
20
|
nuclear@1
|
21 #include <stdlib.h>
|
nuclear@1
|
22 #include <string.h>
|
nuclear@1
|
23 #include <ctype.h>
|
nuclear@1
|
24 #include <ogg/ogg.h>
|
nuclear@1
|
25 #include "vorbis/codec.h"
|
nuclear@1
|
26 #include "codec_internal.h"
|
nuclear@1
|
27 #include "codebook.h"
|
nuclear@1
|
28 #include "registry.h"
|
nuclear@1
|
29 #include "window.h"
|
nuclear@1
|
30 #include "psy.h"
|
nuclear@1
|
31 #include "misc.h"
|
nuclear@1
|
32 #include "os.h"
|
nuclear@1
|
33
|
nuclear@1
|
34 #define GENERAL_VENDOR_STRING "Xiph.Org libVorbis 1.3.3"
|
nuclear@1
|
35 #define ENCODE_VENDOR_STRING "Xiph.Org libVorbis I 20120203 (Omnipresent)"
|
nuclear@1
|
36
|
nuclear@1
|
37 /* helpers */
|
nuclear@1
|
38 static int ilog2(unsigned int v){
|
nuclear@1
|
39 int ret=0;
|
nuclear@1
|
40 if(v)--v;
|
nuclear@1
|
41 while(v){
|
nuclear@1
|
42 ret++;
|
nuclear@1
|
43 v>>=1;
|
nuclear@1
|
44 }
|
nuclear@1
|
45 return(ret);
|
nuclear@1
|
46 }
|
nuclear@1
|
47
|
nuclear@1
|
48 static void _v_writestring(oggpack_buffer *o,const char *s, int bytes){
|
nuclear@1
|
49
|
nuclear@1
|
50 while(bytes--){
|
nuclear@1
|
51 oggpack_write(o,*s++,8);
|
nuclear@1
|
52 }
|
nuclear@1
|
53 }
|
nuclear@1
|
54
|
nuclear@1
|
55 static void _v_readstring(oggpack_buffer *o,char *buf,int bytes){
|
nuclear@1
|
56 while(bytes--){
|
nuclear@1
|
57 *buf++=oggpack_read(o,8);
|
nuclear@1
|
58 }
|
nuclear@1
|
59 }
|
nuclear@1
|
60
|
nuclear@1
|
61 void vorbis_comment_init(vorbis_comment *vc){
|
nuclear@1
|
62 memset(vc,0,sizeof(*vc));
|
nuclear@1
|
63 }
|
nuclear@1
|
64
|
nuclear@1
|
65 void vorbis_comment_add(vorbis_comment *vc,const char *comment){
|
nuclear@1
|
66 vc->user_comments=_ogg_realloc(vc->user_comments,
|
nuclear@1
|
67 (vc->comments+2)*sizeof(*vc->user_comments));
|
nuclear@1
|
68 vc->comment_lengths=_ogg_realloc(vc->comment_lengths,
|
nuclear@1
|
69 (vc->comments+2)*sizeof(*vc->comment_lengths));
|
nuclear@1
|
70 vc->comment_lengths[vc->comments]=strlen(comment);
|
nuclear@1
|
71 vc->user_comments[vc->comments]=_ogg_malloc(vc->comment_lengths[vc->comments]+1);
|
nuclear@1
|
72 strcpy(vc->user_comments[vc->comments], comment);
|
nuclear@1
|
73 vc->comments++;
|
nuclear@1
|
74 vc->user_comments[vc->comments]=NULL;
|
nuclear@1
|
75 }
|
nuclear@1
|
76
|
nuclear@1
|
77 void vorbis_comment_add_tag(vorbis_comment *vc, const char *tag, const char *contents){
|
nuclear@1
|
78 char *comment=alloca(strlen(tag)+strlen(contents)+2); /* +2 for = and \0 */
|
nuclear@1
|
79 strcpy(comment, tag);
|
nuclear@1
|
80 strcat(comment, "=");
|
nuclear@1
|
81 strcat(comment, contents);
|
nuclear@1
|
82 vorbis_comment_add(vc, comment);
|
nuclear@1
|
83 }
|
nuclear@1
|
84
|
nuclear@1
|
85 /* This is more or less the same as strncasecmp - but that doesn't exist
|
nuclear@1
|
86 * everywhere, and this is a fairly trivial function, so we include it */
|
nuclear@1
|
87 static int tagcompare(const char *s1, const char *s2, int n){
|
nuclear@1
|
88 int c=0;
|
nuclear@1
|
89 while(c < n){
|
nuclear@1
|
90 if(toupper(s1[c]) != toupper(s2[c]))
|
nuclear@1
|
91 return !0;
|
nuclear@1
|
92 c++;
|
nuclear@1
|
93 }
|
nuclear@1
|
94 return 0;
|
nuclear@1
|
95 }
|
nuclear@1
|
96
|
nuclear@1
|
97 char *vorbis_comment_query(vorbis_comment *vc, const char *tag, int count){
|
nuclear@1
|
98 long i;
|
nuclear@1
|
99 int found = 0;
|
nuclear@1
|
100 int taglen = strlen(tag)+1; /* +1 for the = we append */
|
nuclear@1
|
101 char *fulltag = alloca(taglen+ 1);
|
nuclear@1
|
102
|
nuclear@1
|
103 strcpy(fulltag, tag);
|
nuclear@1
|
104 strcat(fulltag, "=");
|
nuclear@1
|
105
|
nuclear@1
|
106 for(i=0;i<vc->comments;i++){
|
nuclear@1
|
107 if(!tagcompare(vc->user_comments[i], fulltag, taglen)){
|
nuclear@1
|
108 if(count == found)
|
nuclear@1
|
109 /* We return a pointer to the data, not a copy */
|
nuclear@1
|
110 return vc->user_comments[i] + taglen;
|
nuclear@1
|
111 else
|
nuclear@1
|
112 found++;
|
nuclear@1
|
113 }
|
nuclear@1
|
114 }
|
nuclear@1
|
115 return NULL; /* didn't find anything */
|
nuclear@1
|
116 }
|
nuclear@1
|
117
|
nuclear@1
|
118 int vorbis_comment_query_count(vorbis_comment *vc, const char *tag){
|
nuclear@1
|
119 int i,count=0;
|
nuclear@1
|
120 int taglen = strlen(tag)+1; /* +1 for the = we append */
|
nuclear@1
|
121 char *fulltag = alloca(taglen+1);
|
nuclear@1
|
122 strcpy(fulltag,tag);
|
nuclear@1
|
123 strcat(fulltag, "=");
|
nuclear@1
|
124
|
nuclear@1
|
125 for(i=0;i<vc->comments;i++){
|
nuclear@1
|
126 if(!tagcompare(vc->user_comments[i], fulltag, taglen))
|
nuclear@1
|
127 count++;
|
nuclear@1
|
128 }
|
nuclear@1
|
129
|
nuclear@1
|
130 return count;
|
nuclear@1
|
131 }
|
nuclear@1
|
132
|
nuclear@1
|
133 void vorbis_comment_clear(vorbis_comment *vc){
|
nuclear@1
|
134 if(vc){
|
nuclear@1
|
135 long i;
|
nuclear@1
|
136 if(vc->user_comments){
|
nuclear@1
|
137 for(i=0;i<vc->comments;i++)
|
nuclear@1
|
138 if(vc->user_comments[i])_ogg_free(vc->user_comments[i]);
|
nuclear@1
|
139 _ogg_free(vc->user_comments);
|
nuclear@1
|
140 }
|
nuclear@1
|
141 if(vc->comment_lengths)_ogg_free(vc->comment_lengths);
|
nuclear@1
|
142 if(vc->vendor)_ogg_free(vc->vendor);
|
nuclear@1
|
143 memset(vc,0,sizeof(*vc));
|
nuclear@1
|
144 }
|
nuclear@1
|
145 }
|
nuclear@1
|
146
|
nuclear@1
|
147 /* blocksize 0 is guaranteed to be short, 1 is guaranteed to be long.
|
nuclear@1
|
148 They may be equal, but short will never ge greater than long */
|
nuclear@1
|
149 int vorbis_info_blocksize(vorbis_info *vi,int zo){
|
nuclear@1
|
150 codec_setup_info *ci = vi->codec_setup;
|
nuclear@1
|
151 return ci ? ci->blocksizes[zo] : -1;
|
nuclear@1
|
152 }
|
nuclear@1
|
153
|
nuclear@1
|
154 /* used by synthesis, which has a full, alloced vi */
|
nuclear@1
|
155 void vorbis_info_init(vorbis_info *vi){
|
nuclear@1
|
156 memset(vi,0,sizeof(*vi));
|
nuclear@1
|
157 vi->codec_setup=_ogg_calloc(1,sizeof(codec_setup_info));
|
nuclear@1
|
158 }
|
nuclear@1
|
159
|
nuclear@1
|
160 void vorbis_info_clear(vorbis_info *vi){
|
nuclear@1
|
161 codec_setup_info *ci=vi->codec_setup;
|
nuclear@1
|
162 int i;
|
nuclear@1
|
163
|
nuclear@1
|
164 if(ci){
|
nuclear@1
|
165
|
nuclear@1
|
166 for(i=0;i<ci->modes;i++)
|
nuclear@1
|
167 if(ci->mode_param[i])_ogg_free(ci->mode_param[i]);
|
nuclear@1
|
168
|
nuclear@1
|
169 for(i=0;i<ci->maps;i++) /* unpack does the range checking */
|
nuclear@1
|
170 if(ci->map_param[i]) /* this may be cleaning up an aborted
|
nuclear@1
|
171 unpack, in which case the below type
|
nuclear@1
|
172 cannot be trusted */
|
nuclear@1
|
173 _mapping_P[ci->map_type[i]]->free_info(ci->map_param[i]);
|
nuclear@1
|
174
|
nuclear@1
|
175 for(i=0;i<ci->floors;i++) /* unpack does the range checking */
|
nuclear@1
|
176 if(ci->floor_param[i]) /* this may be cleaning up an aborted
|
nuclear@1
|
177 unpack, in which case the below type
|
nuclear@1
|
178 cannot be trusted */
|
nuclear@1
|
179 _floor_P[ci->floor_type[i]]->free_info(ci->floor_param[i]);
|
nuclear@1
|
180
|
nuclear@1
|
181 for(i=0;i<ci->residues;i++) /* unpack does the range checking */
|
nuclear@1
|
182 if(ci->residue_param[i]) /* this may be cleaning up an aborted
|
nuclear@1
|
183 unpack, in which case the below type
|
nuclear@1
|
184 cannot be trusted */
|
nuclear@1
|
185 _residue_P[ci->residue_type[i]]->free_info(ci->residue_param[i]);
|
nuclear@1
|
186
|
nuclear@1
|
187 for(i=0;i<ci->books;i++){
|
nuclear@1
|
188 if(ci->book_param[i]){
|
nuclear@1
|
189 /* knows if the book was not alloced */
|
nuclear@1
|
190 vorbis_staticbook_destroy(ci->book_param[i]);
|
nuclear@1
|
191 }
|
nuclear@1
|
192 if(ci->fullbooks)
|
nuclear@1
|
193 vorbis_book_clear(ci->fullbooks+i);
|
nuclear@1
|
194 }
|
nuclear@1
|
195 if(ci->fullbooks)
|
nuclear@1
|
196 _ogg_free(ci->fullbooks);
|
nuclear@1
|
197
|
nuclear@1
|
198 for(i=0;i<ci->psys;i++)
|
nuclear@1
|
199 _vi_psy_free(ci->psy_param[i]);
|
nuclear@1
|
200
|
nuclear@1
|
201 _ogg_free(ci);
|
nuclear@1
|
202 }
|
nuclear@1
|
203
|
nuclear@1
|
204 memset(vi,0,sizeof(*vi));
|
nuclear@1
|
205 }
|
nuclear@1
|
206
|
nuclear@1
|
207 /* Header packing/unpacking ********************************************/
|
nuclear@1
|
208
|
nuclear@1
|
209 static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
|
nuclear@1
|
210 codec_setup_info *ci=vi->codec_setup;
|
nuclear@1
|
211 if(!ci)return(OV_EFAULT);
|
nuclear@1
|
212
|
nuclear@1
|
213 vi->version=oggpack_read(opb,32);
|
nuclear@1
|
214 if(vi->version!=0)return(OV_EVERSION);
|
nuclear@1
|
215
|
nuclear@1
|
216 vi->channels=oggpack_read(opb,8);
|
nuclear@1
|
217 vi->rate=oggpack_read(opb,32);
|
nuclear@1
|
218
|
nuclear@1
|
219 vi->bitrate_upper=oggpack_read(opb,32);
|
nuclear@1
|
220 vi->bitrate_nominal=oggpack_read(opb,32);
|
nuclear@1
|
221 vi->bitrate_lower=oggpack_read(opb,32);
|
nuclear@1
|
222
|
nuclear@1
|
223 ci->blocksizes[0]=1<<oggpack_read(opb,4);
|
nuclear@1
|
224 ci->blocksizes[1]=1<<oggpack_read(opb,4);
|
nuclear@1
|
225
|
nuclear@1
|
226 if(vi->rate<1)goto err_out;
|
nuclear@1
|
227 if(vi->channels<1)goto err_out;
|
nuclear@1
|
228 if(ci->blocksizes[0]<64)goto err_out;
|
nuclear@1
|
229 if(ci->blocksizes[1]<ci->blocksizes[0])goto err_out;
|
nuclear@1
|
230 if(ci->blocksizes[1]>8192)goto err_out;
|
nuclear@1
|
231
|
nuclear@1
|
232 if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
|
nuclear@1
|
233
|
nuclear@1
|
234 return(0);
|
nuclear@1
|
235 err_out:
|
nuclear@1
|
236 vorbis_info_clear(vi);
|
nuclear@1
|
237 return(OV_EBADHEADER);
|
nuclear@1
|
238 }
|
nuclear@1
|
239
|
nuclear@1
|
240 static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
|
nuclear@1
|
241 int i;
|
nuclear@1
|
242 int vendorlen=oggpack_read(opb,32);
|
nuclear@1
|
243 if(vendorlen<0)goto err_out;
|
nuclear@1
|
244 if(vendorlen>opb->storage-8)goto err_out;
|
nuclear@1
|
245 vc->vendor=_ogg_calloc(vendorlen+1,1);
|
nuclear@1
|
246 _v_readstring(opb,vc->vendor,vendorlen);
|
nuclear@1
|
247 i=oggpack_read(opb,32);
|
nuclear@1
|
248 if(i<0)goto err_out;
|
nuclear@1
|
249 if(i>((opb->storage-oggpack_bytes(opb))>>2))goto err_out;
|
nuclear@1
|
250 vc->comments=i;
|
nuclear@1
|
251 vc->user_comments=_ogg_calloc(vc->comments+1,sizeof(*vc->user_comments));
|
nuclear@1
|
252 vc->comment_lengths=_ogg_calloc(vc->comments+1, sizeof(*vc->comment_lengths));
|
nuclear@1
|
253
|
nuclear@1
|
254 for(i=0;i<vc->comments;i++){
|
nuclear@1
|
255 int len=oggpack_read(opb,32);
|
nuclear@1
|
256 if(len<0)goto err_out;
|
nuclear@1
|
257 if(len>opb->storage-oggpack_bytes(opb))goto err_out;
|
nuclear@1
|
258 vc->comment_lengths[i]=len;
|
nuclear@1
|
259 vc->user_comments[i]=_ogg_calloc(len+1,1);
|
nuclear@1
|
260 _v_readstring(opb,vc->user_comments[i],len);
|
nuclear@1
|
261 }
|
nuclear@1
|
262 if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
|
nuclear@1
|
263
|
nuclear@1
|
264 return(0);
|
nuclear@1
|
265 err_out:
|
nuclear@1
|
266 vorbis_comment_clear(vc);
|
nuclear@1
|
267 return(OV_EBADHEADER);
|
nuclear@1
|
268 }
|
nuclear@1
|
269
|
nuclear@1
|
270 /* all of the real encoding details are here. The modes, books,
|
nuclear@1
|
271 everything */
|
nuclear@1
|
272 static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
|
nuclear@1
|
273 codec_setup_info *ci=vi->codec_setup;
|
nuclear@1
|
274 int i;
|
nuclear@1
|
275 if(!ci)return(OV_EFAULT);
|
nuclear@1
|
276
|
nuclear@1
|
277 /* codebooks */
|
nuclear@1
|
278 ci->books=oggpack_read(opb,8)+1;
|
nuclear@1
|
279 if(ci->books<=0)goto err_out;
|
nuclear@1
|
280 for(i=0;i<ci->books;i++){
|
nuclear@1
|
281 ci->book_param[i]=vorbis_staticbook_unpack(opb);
|
nuclear@1
|
282 if(!ci->book_param[i])goto err_out;
|
nuclear@1
|
283 }
|
nuclear@1
|
284
|
nuclear@1
|
285 /* time backend settings; hooks are unused */
|
nuclear@1
|
286 {
|
nuclear@1
|
287 int times=oggpack_read(opb,6)+1;
|
nuclear@1
|
288 if(times<=0)goto err_out;
|
nuclear@1
|
289 for(i=0;i<times;i++){
|
nuclear@1
|
290 int test=oggpack_read(opb,16);
|
nuclear@1
|
291 if(test<0 || test>=VI_TIMEB)goto err_out;
|
nuclear@1
|
292 }
|
nuclear@1
|
293 }
|
nuclear@1
|
294
|
nuclear@1
|
295 /* floor backend settings */
|
nuclear@1
|
296 ci->floors=oggpack_read(opb,6)+1;
|
nuclear@1
|
297 if(ci->floors<=0)goto err_out;
|
nuclear@1
|
298 for(i=0;i<ci->floors;i++){
|
nuclear@1
|
299 ci->floor_type[i]=oggpack_read(opb,16);
|
nuclear@1
|
300 if(ci->floor_type[i]<0 || ci->floor_type[i]>=VI_FLOORB)goto err_out;
|
nuclear@1
|
301 ci->floor_param[i]=_floor_P[ci->floor_type[i]]->unpack(vi,opb);
|
nuclear@1
|
302 if(!ci->floor_param[i])goto err_out;
|
nuclear@1
|
303 }
|
nuclear@1
|
304
|
nuclear@1
|
305 /* residue backend settings */
|
nuclear@1
|
306 ci->residues=oggpack_read(opb,6)+1;
|
nuclear@1
|
307 if(ci->residues<=0)goto err_out;
|
nuclear@1
|
308 for(i=0;i<ci->residues;i++){
|
nuclear@1
|
309 ci->residue_type[i]=oggpack_read(opb,16);
|
nuclear@1
|
310 if(ci->residue_type[i]<0 || ci->residue_type[i]>=VI_RESB)goto err_out;
|
nuclear@1
|
311 ci->residue_param[i]=_residue_P[ci->residue_type[i]]->unpack(vi,opb);
|
nuclear@1
|
312 if(!ci->residue_param[i])goto err_out;
|
nuclear@1
|
313 }
|
nuclear@1
|
314
|
nuclear@1
|
315 /* map backend settings */
|
nuclear@1
|
316 ci->maps=oggpack_read(opb,6)+1;
|
nuclear@1
|
317 if(ci->maps<=0)goto err_out;
|
nuclear@1
|
318 for(i=0;i<ci->maps;i++){
|
nuclear@1
|
319 ci->map_type[i]=oggpack_read(opb,16);
|
nuclear@1
|
320 if(ci->map_type[i]<0 || ci->map_type[i]>=VI_MAPB)goto err_out;
|
nuclear@1
|
321 ci->map_param[i]=_mapping_P[ci->map_type[i]]->unpack(vi,opb);
|
nuclear@1
|
322 if(!ci->map_param[i])goto err_out;
|
nuclear@1
|
323 }
|
nuclear@1
|
324
|
nuclear@1
|
325 /* mode settings */
|
nuclear@1
|
326 ci->modes=oggpack_read(opb,6)+1;
|
nuclear@1
|
327 if(ci->modes<=0)goto err_out;
|
nuclear@1
|
328 for(i=0;i<ci->modes;i++){
|
nuclear@1
|
329 ci->mode_param[i]=_ogg_calloc(1,sizeof(*ci->mode_param[i]));
|
nuclear@1
|
330 ci->mode_param[i]->blockflag=oggpack_read(opb,1);
|
nuclear@1
|
331 ci->mode_param[i]->windowtype=oggpack_read(opb,16);
|
nuclear@1
|
332 ci->mode_param[i]->transformtype=oggpack_read(opb,16);
|
nuclear@1
|
333 ci->mode_param[i]->mapping=oggpack_read(opb,8);
|
nuclear@1
|
334
|
nuclear@1
|
335 if(ci->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
|
nuclear@1
|
336 if(ci->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
|
nuclear@1
|
337 if(ci->mode_param[i]->mapping>=ci->maps)goto err_out;
|
nuclear@1
|
338 if(ci->mode_param[i]->mapping<0)goto err_out;
|
nuclear@1
|
339 }
|
nuclear@1
|
340
|
nuclear@1
|
341 if(oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
|
nuclear@1
|
342
|
nuclear@1
|
343 return(0);
|
nuclear@1
|
344 err_out:
|
nuclear@1
|
345 vorbis_info_clear(vi);
|
nuclear@1
|
346 return(OV_EBADHEADER);
|
nuclear@1
|
347 }
|
nuclear@1
|
348
|
nuclear@1
|
349 /* Is this packet a vorbis ID header? */
|
nuclear@1
|
350 int vorbis_synthesis_idheader(ogg_packet *op){
|
nuclear@1
|
351 oggpack_buffer opb;
|
nuclear@1
|
352 char buffer[6];
|
nuclear@1
|
353
|
nuclear@1
|
354 if(op){
|
nuclear@1
|
355 oggpack_readinit(&opb,op->packet,op->bytes);
|
nuclear@1
|
356
|
nuclear@1
|
357 if(!op->b_o_s)
|
nuclear@1
|
358 return(0); /* Not the initial packet */
|
nuclear@1
|
359
|
nuclear@1
|
360 if(oggpack_read(&opb,8) != 1)
|
nuclear@1
|
361 return 0; /* not an ID header */
|
nuclear@1
|
362
|
nuclear@1
|
363 memset(buffer,0,6);
|
nuclear@1
|
364 _v_readstring(&opb,buffer,6);
|
nuclear@1
|
365 if(memcmp(buffer,"vorbis",6))
|
nuclear@1
|
366 return 0; /* not vorbis */
|
nuclear@1
|
367
|
nuclear@1
|
368 return 1;
|
nuclear@1
|
369 }
|
nuclear@1
|
370
|
nuclear@1
|
371 return 0;
|
nuclear@1
|
372 }
|
nuclear@1
|
373
|
nuclear@1
|
374 /* The Vorbis header is in three packets; the initial small packet in
|
nuclear@1
|
375 the first page that identifies basic parameters, a second packet
|
nuclear@1
|
376 with bitstream comments and a third packet that holds the
|
nuclear@1
|
377 codebook. */
|
nuclear@1
|
378
|
nuclear@1
|
379 int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
|
nuclear@1
|
380 oggpack_buffer opb;
|
nuclear@1
|
381
|
nuclear@1
|
382 if(op){
|
nuclear@1
|
383 oggpack_readinit(&opb,op->packet,op->bytes);
|
nuclear@1
|
384
|
nuclear@1
|
385 /* Which of the three types of header is this? */
|
nuclear@1
|
386 /* Also verify header-ness, vorbis */
|
nuclear@1
|
387 {
|
nuclear@1
|
388 char buffer[6];
|
nuclear@1
|
389 int packtype=oggpack_read(&opb,8);
|
nuclear@1
|
390 memset(buffer,0,6);
|
nuclear@1
|
391 _v_readstring(&opb,buffer,6);
|
nuclear@1
|
392 if(memcmp(buffer,"vorbis",6)){
|
nuclear@1
|
393 /* not a vorbis header */
|
nuclear@1
|
394 return(OV_ENOTVORBIS);
|
nuclear@1
|
395 }
|
nuclear@1
|
396 switch(packtype){
|
nuclear@1
|
397 case 0x01: /* least significant *bit* is read first */
|
nuclear@1
|
398 if(!op->b_o_s){
|
nuclear@1
|
399 /* Not the initial packet */
|
nuclear@1
|
400 return(OV_EBADHEADER);
|
nuclear@1
|
401 }
|
nuclear@1
|
402 if(vi->rate!=0){
|
nuclear@1
|
403 /* previously initialized info header */
|
nuclear@1
|
404 return(OV_EBADHEADER);
|
nuclear@1
|
405 }
|
nuclear@1
|
406
|
nuclear@1
|
407 return(_vorbis_unpack_info(vi,&opb));
|
nuclear@1
|
408
|
nuclear@1
|
409 case 0x03: /* least significant *bit* is read first */
|
nuclear@1
|
410 if(vi->rate==0){
|
nuclear@1
|
411 /* um... we didn't get the initial header */
|
nuclear@1
|
412 return(OV_EBADHEADER);
|
nuclear@1
|
413 }
|
nuclear@1
|
414
|
nuclear@1
|
415 return(_vorbis_unpack_comment(vc,&opb));
|
nuclear@1
|
416
|
nuclear@1
|
417 case 0x05: /* least significant *bit* is read first */
|
nuclear@1
|
418 if(vi->rate==0 || vc->vendor==NULL){
|
nuclear@1
|
419 /* um... we didn;t get the initial header or comments yet */
|
nuclear@1
|
420 return(OV_EBADHEADER);
|
nuclear@1
|
421 }
|
nuclear@1
|
422
|
nuclear@1
|
423 return(_vorbis_unpack_books(vi,&opb));
|
nuclear@1
|
424
|
nuclear@1
|
425 default:
|
nuclear@1
|
426 /* Not a valid vorbis header type */
|
nuclear@1
|
427 return(OV_EBADHEADER);
|
nuclear@1
|
428 break;
|
nuclear@1
|
429 }
|
nuclear@1
|
430 }
|
nuclear@1
|
431 }
|
nuclear@1
|
432 return(OV_EBADHEADER);
|
nuclear@1
|
433 }
|
nuclear@1
|
434
|
nuclear@1
|
435 /* pack side **********************************************************/
|
nuclear@1
|
436
|
nuclear@1
|
437 static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){
|
nuclear@1
|
438 codec_setup_info *ci=vi->codec_setup;
|
nuclear@1
|
439 if(!ci)return(OV_EFAULT);
|
nuclear@1
|
440
|
nuclear@1
|
441 /* preamble */
|
nuclear@1
|
442 oggpack_write(opb,0x01,8);
|
nuclear@1
|
443 _v_writestring(opb,"vorbis", 6);
|
nuclear@1
|
444
|
nuclear@1
|
445 /* basic information about the stream */
|
nuclear@1
|
446 oggpack_write(opb,0x00,32);
|
nuclear@1
|
447 oggpack_write(opb,vi->channels,8);
|
nuclear@1
|
448 oggpack_write(opb,vi->rate,32);
|
nuclear@1
|
449
|
nuclear@1
|
450 oggpack_write(opb,vi->bitrate_upper,32);
|
nuclear@1
|
451 oggpack_write(opb,vi->bitrate_nominal,32);
|
nuclear@1
|
452 oggpack_write(opb,vi->bitrate_lower,32);
|
nuclear@1
|
453
|
nuclear@1
|
454 oggpack_write(opb,ilog2(ci->blocksizes[0]),4);
|
nuclear@1
|
455 oggpack_write(opb,ilog2(ci->blocksizes[1]),4);
|
nuclear@1
|
456 oggpack_write(opb,1,1);
|
nuclear@1
|
457
|
nuclear@1
|
458 return(0);
|
nuclear@1
|
459 }
|
nuclear@1
|
460
|
nuclear@1
|
461 static int _vorbis_pack_comment(oggpack_buffer *opb,vorbis_comment *vc){
|
nuclear@1
|
462 int bytes = strlen(ENCODE_VENDOR_STRING);
|
nuclear@1
|
463
|
nuclear@1
|
464 /* preamble */
|
nuclear@1
|
465 oggpack_write(opb,0x03,8);
|
nuclear@1
|
466 _v_writestring(opb,"vorbis", 6);
|
nuclear@1
|
467
|
nuclear@1
|
468 /* vendor */
|
nuclear@1
|
469 oggpack_write(opb,bytes,32);
|
nuclear@1
|
470 _v_writestring(opb,ENCODE_VENDOR_STRING, bytes);
|
nuclear@1
|
471
|
nuclear@1
|
472 /* comments */
|
nuclear@1
|
473
|
nuclear@1
|
474 oggpack_write(opb,vc->comments,32);
|
nuclear@1
|
475 if(vc->comments){
|
nuclear@1
|
476 int i;
|
nuclear@1
|
477 for(i=0;i<vc->comments;i++){
|
nuclear@1
|
478 if(vc->user_comments[i]){
|
nuclear@1
|
479 oggpack_write(opb,vc->comment_lengths[i],32);
|
nuclear@1
|
480 _v_writestring(opb,vc->user_comments[i], vc->comment_lengths[i]);
|
nuclear@1
|
481 }else{
|
nuclear@1
|
482 oggpack_write(opb,0,32);
|
nuclear@1
|
483 }
|
nuclear@1
|
484 }
|
nuclear@1
|
485 }
|
nuclear@1
|
486 oggpack_write(opb,1,1);
|
nuclear@1
|
487
|
nuclear@1
|
488 return(0);
|
nuclear@1
|
489 }
|
nuclear@1
|
490
|
nuclear@1
|
491 static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){
|
nuclear@1
|
492 codec_setup_info *ci=vi->codec_setup;
|
nuclear@1
|
493 int i;
|
nuclear@1
|
494 if(!ci)return(OV_EFAULT);
|
nuclear@1
|
495
|
nuclear@1
|
496 oggpack_write(opb,0x05,8);
|
nuclear@1
|
497 _v_writestring(opb,"vorbis", 6);
|
nuclear@1
|
498
|
nuclear@1
|
499 /* books */
|
nuclear@1
|
500 oggpack_write(opb,ci->books-1,8);
|
nuclear@1
|
501 for(i=0;i<ci->books;i++)
|
nuclear@1
|
502 if(vorbis_staticbook_pack(ci->book_param[i],opb))goto err_out;
|
nuclear@1
|
503
|
nuclear@1
|
504 /* times; hook placeholders */
|
nuclear@1
|
505 oggpack_write(opb,0,6);
|
nuclear@1
|
506 oggpack_write(opb,0,16);
|
nuclear@1
|
507
|
nuclear@1
|
508 /* floors */
|
nuclear@1
|
509 oggpack_write(opb,ci->floors-1,6);
|
nuclear@1
|
510 for(i=0;i<ci->floors;i++){
|
nuclear@1
|
511 oggpack_write(opb,ci->floor_type[i],16);
|
nuclear@1
|
512 if(_floor_P[ci->floor_type[i]]->pack)
|
nuclear@1
|
513 _floor_P[ci->floor_type[i]]->pack(ci->floor_param[i],opb);
|
nuclear@1
|
514 else
|
nuclear@1
|
515 goto err_out;
|
nuclear@1
|
516 }
|
nuclear@1
|
517
|
nuclear@1
|
518 /* residues */
|
nuclear@1
|
519 oggpack_write(opb,ci->residues-1,6);
|
nuclear@1
|
520 for(i=0;i<ci->residues;i++){
|
nuclear@1
|
521 oggpack_write(opb,ci->residue_type[i],16);
|
nuclear@1
|
522 _residue_P[ci->residue_type[i]]->pack(ci->residue_param[i],opb);
|
nuclear@1
|
523 }
|
nuclear@1
|
524
|
nuclear@1
|
525 /* maps */
|
nuclear@1
|
526 oggpack_write(opb,ci->maps-1,6);
|
nuclear@1
|
527 for(i=0;i<ci->maps;i++){
|
nuclear@1
|
528 oggpack_write(opb,ci->map_type[i],16);
|
nuclear@1
|
529 _mapping_P[ci->map_type[i]]->pack(vi,ci->map_param[i],opb);
|
nuclear@1
|
530 }
|
nuclear@1
|
531
|
nuclear@1
|
532 /* modes */
|
nuclear@1
|
533 oggpack_write(opb,ci->modes-1,6);
|
nuclear@1
|
534 for(i=0;i<ci->modes;i++){
|
nuclear@1
|
535 oggpack_write(opb,ci->mode_param[i]->blockflag,1);
|
nuclear@1
|
536 oggpack_write(opb,ci->mode_param[i]->windowtype,16);
|
nuclear@1
|
537 oggpack_write(opb,ci->mode_param[i]->transformtype,16);
|
nuclear@1
|
538 oggpack_write(opb,ci->mode_param[i]->mapping,8);
|
nuclear@1
|
539 }
|
nuclear@1
|
540 oggpack_write(opb,1,1);
|
nuclear@1
|
541
|
nuclear@1
|
542 return(0);
|
nuclear@1
|
543 err_out:
|
nuclear@1
|
544 return(-1);
|
nuclear@1
|
545 }
|
nuclear@1
|
546
|
nuclear@1
|
547 int vorbis_commentheader_out(vorbis_comment *vc,
|
nuclear@1
|
548 ogg_packet *op){
|
nuclear@1
|
549
|
nuclear@1
|
550 oggpack_buffer opb;
|
nuclear@1
|
551
|
nuclear@1
|
552 oggpack_writeinit(&opb);
|
nuclear@1
|
553 if(_vorbis_pack_comment(&opb,vc)){
|
nuclear@1
|
554 oggpack_writeclear(&opb);
|
nuclear@1
|
555 return OV_EIMPL;
|
nuclear@1
|
556 }
|
nuclear@1
|
557
|
nuclear@1
|
558 op->packet = _ogg_malloc(oggpack_bytes(&opb));
|
nuclear@1
|
559 memcpy(op->packet, opb.buffer, oggpack_bytes(&opb));
|
nuclear@1
|
560
|
nuclear@1
|
561 op->bytes=oggpack_bytes(&opb);
|
nuclear@1
|
562 op->b_o_s=0;
|
nuclear@1
|
563 op->e_o_s=0;
|
nuclear@1
|
564 op->granulepos=0;
|
nuclear@1
|
565 op->packetno=1;
|
nuclear@1
|
566
|
nuclear@1
|
567 oggpack_writeclear(&opb);
|
nuclear@1
|
568 return 0;
|
nuclear@1
|
569 }
|
nuclear@1
|
570
|
nuclear@1
|
571 int vorbis_analysis_headerout(vorbis_dsp_state *v,
|
nuclear@1
|
572 vorbis_comment *vc,
|
nuclear@1
|
573 ogg_packet *op,
|
nuclear@1
|
574 ogg_packet *op_comm,
|
nuclear@1
|
575 ogg_packet *op_code){
|
nuclear@1
|
576 int ret=OV_EIMPL;
|
nuclear@1
|
577 vorbis_info *vi=v->vi;
|
nuclear@1
|
578 oggpack_buffer opb;
|
nuclear@1
|
579 private_state *b=v->backend_state;
|
nuclear@1
|
580
|
nuclear@1
|
581 if(!b){
|
nuclear@1
|
582 ret=OV_EFAULT;
|
nuclear@1
|
583 goto err_out;
|
nuclear@1
|
584 }
|
nuclear@1
|
585
|
nuclear@1
|
586 /* first header packet **********************************************/
|
nuclear@1
|
587
|
nuclear@1
|
588 oggpack_writeinit(&opb);
|
nuclear@1
|
589 if(_vorbis_pack_info(&opb,vi))goto err_out;
|
nuclear@1
|
590
|
nuclear@1
|
591 /* build the packet */
|
nuclear@1
|
592 if(b->header)_ogg_free(b->header);
|
nuclear@1
|
593 b->header=_ogg_malloc(oggpack_bytes(&opb));
|
nuclear@1
|
594 memcpy(b->header,opb.buffer,oggpack_bytes(&opb));
|
nuclear@1
|
595 op->packet=b->header;
|
nuclear@1
|
596 op->bytes=oggpack_bytes(&opb);
|
nuclear@1
|
597 op->b_o_s=1;
|
nuclear@1
|
598 op->e_o_s=0;
|
nuclear@1
|
599 op->granulepos=0;
|
nuclear@1
|
600 op->packetno=0;
|
nuclear@1
|
601
|
nuclear@1
|
602 /* second header packet (comments) **********************************/
|
nuclear@1
|
603
|
nuclear@1
|
604 oggpack_reset(&opb);
|
nuclear@1
|
605 if(_vorbis_pack_comment(&opb,vc))goto err_out;
|
nuclear@1
|
606
|
nuclear@1
|
607 if(b->header1)_ogg_free(b->header1);
|
nuclear@1
|
608 b->header1=_ogg_malloc(oggpack_bytes(&opb));
|
nuclear@1
|
609 memcpy(b->header1,opb.buffer,oggpack_bytes(&opb));
|
nuclear@1
|
610 op_comm->packet=b->header1;
|
nuclear@1
|
611 op_comm->bytes=oggpack_bytes(&opb);
|
nuclear@1
|
612 op_comm->b_o_s=0;
|
nuclear@1
|
613 op_comm->e_o_s=0;
|
nuclear@1
|
614 op_comm->granulepos=0;
|
nuclear@1
|
615 op_comm->packetno=1;
|
nuclear@1
|
616
|
nuclear@1
|
617 /* third header packet (modes/codebooks) ****************************/
|
nuclear@1
|
618
|
nuclear@1
|
619 oggpack_reset(&opb);
|
nuclear@1
|
620 if(_vorbis_pack_books(&opb,vi))goto err_out;
|
nuclear@1
|
621
|
nuclear@1
|
622 if(b->header2)_ogg_free(b->header2);
|
nuclear@1
|
623 b->header2=_ogg_malloc(oggpack_bytes(&opb));
|
nuclear@1
|
624 memcpy(b->header2,opb.buffer,oggpack_bytes(&opb));
|
nuclear@1
|
625 op_code->packet=b->header2;
|
nuclear@1
|
626 op_code->bytes=oggpack_bytes(&opb);
|
nuclear@1
|
627 op_code->b_o_s=0;
|
nuclear@1
|
628 op_code->e_o_s=0;
|
nuclear@1
|
629 op_code->granulepos=0;
|
nuclear@1
|
630 op_code->packetno=2;
|
nuclear@1
|
631
|
nuclear@1
|
632 oggpack_writeclear(&opb);
|
nuclear@1
|
633 return(0);
|
nuclear@1
|
634 err_out:
|
nuclear@1
|
635 memset(op,0,sizeof(*op));
|
nuclear@1
|
636 memset(op_comm,0,sizeof(*op_comm));
|
nuclear@1
|
637 memset(op_code,0,sizeof(*op_code));
|
nuclear@1
|
638
|
nuclear@1
|
639 if(b){
|
nuclear@1
|
640 oggpack_writeclear(&opb);
|
nuclear@1
|
641 if(b->header)_ogg_free(b->header);
|
nuclear@1
|
642 if(b->header1)_ogg_free(b->header1);
|
nuclear@1
|
643 if(b->header2)_ogg_free(b->header2);
|
nuclear@1
|
644 b->header=NULL;
|
nuclear@1
|
645 b->header1=NULL;
|
nuclear@1
|
646 b->header2=NULL;
|
nuclear@1
|
647 }
|
nuclear@1
|
648 return(ret);
|
nuclear@1
|
649 }
|
nuclear@1
|
650
|
nuclear@1
|
651 double vorbis_granule_time(vorbis_dsp_state *v,ogg_int64_t granulepos){
|
nuclear@1
|
652 if(granulepos == -1) return -1;
|
nuclear@1
|
653
|
nuclear@1
|
654 /* We're not guaranteed a 64 bit unsigned type everywhere, so we
|
nuclear@1
|
655 have to put the unsigned granpo in a signed type. */
|
nuclear@1
|
656 if(granulepos>=0){
|
nuclear@1
|
657 return((double)granulepos/v->vi->rate);
|
nuclear@1
|
658 }else{
|
nuclear@1
|
659 ogg_int64_t granuleoff=0xffffffff;
|
nuclear@1
|
660 granuleoff<<=31;
|
nuclear@1
|
661 granuleoff|=0x7ffffffff;
|
nuclear@1
|
662 return(((double)granulepos+2+granuleoff+granuleoff)/v->vi->rate);
|
nuclear@1
|
663 }
|
nuclear@1
|
664 }
|
nuclear@1
|
665
|
nuclear@1
|
666 const char *vorbis_version_string(void){
|
nuclear@1
|
667 return GENERAL_VENDOR_STRING;
|
nuclear@1
|
668 }
|