1 /********************************************************************
3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2015 *
9 * by the Xiph.Org Foundation http://www.xiph.org/ *
11 ********************************************************************
13 function: maintain the info structure, info <-> header packets
16 ********************************************************************/
18 /* general handling of the header and the vorbis_info structure (and
25 #include "vorbis/codec.h"
26 #include "codec_internal.h"
34 #define GENERAL_VENDOR_STRING "Xiph.Org libVorbis 1.3.5"
35 #define ENCODE_VENDOR_STRING "Xiph.Org libVorbis I 20150105 (⛄⛄⛄⛄)"
38 static void _v_writestring(oggpack_buffer *o,const char *s, int bytes){
41 oggpack_write(o,*s++,8);
45 static void _v_readstring(oggpack_buffer *o,char *buf,int bytes){
47 *buf++=oggpack_read(o,8);
51 void vorbis_comment_init(vorbis_comment *vc){
52 memset(vc,0,sizeof(*vc));
55 void vorbis_comment_add(vorbis_comment *vc,const char *comment){
56 vc->user_comments=_ogg_realloc(vc->user_comments,
57 (vc->comments+2)*sizeof(*vc->user_comments));
58 vc->comment_lengths=_ogg_realloc(vc->comment_lengths,
59 (vc->comments+2)*sizeof(*vc->comment_lengths));
60 vc->comment_lengths[vc->comments]=strlen(comment);
61 vc->user_comments[vc->comments]=_ogg_malloc(vc->comment_lengths[vc->comments]+1);
62 strcpy(vc->user_comments[vc->comments], comment);
64 vc->user_comments[vc->comments]=NULL;
67 void vorbis_comment_add_tag(vorbis_comment *vc, const char *tag, const char *contents){
68 char *comment=alloca(strlen(tag)+strlen(contents)+2); /* +2 for = and \0 */
71 strcat(comment, contents);
72 vorbis_comment_add(vc, comment);
75 /* This is more or less the same as strncasecmp - but that doesn't exist
76 * everywhere, and this is a fairly trivial function, so we include it */
77 static int tagcompare(const char *s1, const char *s2, int n){
80 if(toupper(s1[c]) != toupper(s2[c]))
87 char *vorbis_comment_query(vorbis_comment *vc, const char *tag, int count){
90 int taglen = strlen(tag)+1; /* +1 for the = we append */
91 char *fulltag = alloca(taglen+ 1);
96 for(i=0;i<vc->comments;i++){
97 if(!tagcompare(vc->user_comments[i], fulltag, taglen)){
99 /* We return a pointer to the data, not a copy */
100 return vc->user_comments[i] + taglen;
105 return NULL; /* didn't find anything */
108 int vorbis_comment_query_count(vorbis_comment *vc, const char *tag){
110 int taglen = strlen(tag)+1; /* +1 for the = we append */
111 char *fulltag = alloca(taglen+1);
113 strcat(fulltag, "=");
115 for(i=0;i<vc->comments;i++){
116 if(!tagcompare(vc->user_comments[i], fulltag, taglen))
123 void vorbis_comment_clear(vorbis_comment *vc){
126 if(vc->user_comments){
127 for(i=0;i<vc->comments;i++)
128 if(vc->user_comments[i])_ogg_free(vc->user_comments[i]);
129 _ogg_free(vc->user_comments);
131 if(vc->comment_lengths)_ogg_free(vc->comment_lengths);
132 if(vc->vendor)_ogg_free(vc->vendor);
133 memset(vc,0,sizeof(*vc));
137 /* blocksize 0 is guaranteed to be short, 1 is guaranteed to be long.
138 They may be equal, but short will never ge greater than long */
139 int vorbis_info_blocksize(vorbis_info *vi,int zo){
140 codec_setup_info *ci = vi->codec_setup;
141 return ci ? ci->blocksizes[zo] : -1;
144 /* used by synthesis, which has a full, alloced vi */
145 void vorbis_info_init(vorbis_info *vi){
146 memset(vi,0,sizeof(*vi));
147 vi->codec_setup=_ogg_calloc(1,sizeof(codec_setup_info));
150 void vorbis_info_clear(vorbis_info *vi){
151 codec_setup_info *ci=vi->codec_setup;
156 for(i=0;i<ci->modes;i++)
157 if(ci->mode_param[i])_ogg_free(ci->mode_param[i]);
159 for(i=0;i<ci->maps;i++) /* unpack does the range checking */
160 if(ci->map_param[i]) /* this may be cleaning up an aborted
161 unpack, in which case the below type
163 _mapping_P[ci->map_type[i]]->free_info(ci->map_param[i]);
165 for(i=0;i<ci->floors;i++) /* unpack does the range checking */
166 if(ci->floor_param[i]) /* this may be cleaning up an aborted
167 unpack, in which case the below type
169 _floor_P[ci->floor_type[i]]->free_info(ci->floor_param[i]);
171 for(i=0;i<ci->residues;i++) /* unpack does the range checking */
172 if(ci->residue_param[i]) /* this may be cleaning up an aborted
173 unpack, in which case the below type
175 _residue_P[ci->residue_type[i]]->free_info(ci->residue_param[i]);
177 for(i=0;i<ci->books;i++){
178 if(ci->book_param[i]){
179 /* knows if the book was not alloced */
180 vorbis_staticbook_destroy(ci->book_param[i]);
183 vorbis_book_clear(ci->fullbooks+i);
186 _ogg_free(ci->fullbooks);
188 for(i=0;i<ci->psys;i++)
189 _vi_psy_free(ci->psy_param[i]);
194 memset(vi,0,sizeof(*vi));
197 /* Header packing/unpacking ********************************************/
199 static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
200 codec_setup_info *ci=vi->codec_setup;
201 if(!ci)return(OV_EFAULT);
203 vi->version=oggpack_read(opb,32);
204 if(vi->version!=0)return(OV_EVERSION);
206 vi->channels=oggpack_read(opb,8);
207 vi->rate=oggpack_read(opb,32);
209 vi->bitrate_upper=oggpack_read(opb,32);
210 vi->bitrate_nominal=oggpack_read(opb,32);
211 vi->bitrate_lower=oggpack_read(opb,32);
213 ci->blocksizes[0]=1<<oggpack_read(opb,4);
214 ci->blocksizes[1]=1<<oggpack_read(opb,4);
216 if(vi->rate<1)goto err_out;
217 if(vi->channels<1)goto err_out;
218 if(ci->blocksizes[0]<64)goto err_out;
219 if(ci->blocksizes[1]<ci->blocksizes[0])goto err_out;
220 if(ci->blocksizes[1]>8192)goto err_out;
222 if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
226 vorbis_info_clear(vi);
227 return(OV_EBADHEADER);
230 static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
232 int vendorlen=oggpack_read(opb,32);
233 if(vendorlen<0)goto err_out;
234 if(vendorlen>opb->storage-8)goto err_out;
235 vc->vendor=_ogg_calloc(vendorlen+1,1);
236 _v_readstring(opb,vc->vendor,vendorlen);
237 i=oggpack_read(opb,32);
239 if(i>((opb->storage-oggpack_bytes(opb))>>2))goto err_out;
241 vc->user_comments=_ogg_calloc(vc->comments+1,sizeof(*vc->user_comments));
242 vc->comment_lengths=_ogg_calloc(vc->comments+1, sizeof(*vc->comment_lengths));
244 for(i=0;i<vc->comments;i++){
245 int len=oggpack_read(opb,32);
246 if(len<0)goto err_out;
247 if(len>opb->storage-oggpack_bytes(opb))goto err_out;
248 vc->comment_lengths[i]=len;
249 vc->user_comments[i]=_ogg_calloc(len+1,1);
250 _v_readstring(opb,vc->user_comments[i],len);
252 if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
256 vorbis_comment_clear(vc);
257 return(OV_EBADHEADER);
260 /* all of the real encoding details are here. The modes, books,
262 static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
263 codec_setup_info *ci=vi->codec_setup;
267 ci->books=oggpack_read(opb,8)+1;
268 if(ci->books<=0)goto err_out;
269 for(i=0;i<ci->books;i++){
270 ci->book_param[i]=vorbis_staticbook_unpack(opb);
271 if(!ci->book_param[i])goto err_out;
274 /* time backend settings; hooks are unused */
276 int times=oggpack_read(opb,6)+1;
277 if(times<=0)goto err_out;
278 for(i=0;i<times;i++){
279 int test=oggpack_read(opb,16);
280 if(test<0 || test>=VI_TIMEB)goto err_out;
284 /* floor backend settings */
285 ci->floors=oggpack_read(opb,6)+1;
286 if(ci->floors<=0)goto err_out;
287 for(i=0;i<ci->floors;i++){
288 ci->floor_type[i]=oggpack_read(opb,16);
289 if(ci->floor_type[i]<0 || ci->floor_type[i]>=VI_FLOORB)goto err_out;
290 ci->floor_param[i]=_floor_P[ci->floor_type[i]]->unpack(vi,opb);
291 if(!ci->floor_param[i])goto err_out;
294 /* residue backend settings */
295 ci->residues=oggpack_read(opb,6)+1;
296 if(ci->residues<=0)goto err_out;
297 for(i=0;i<ci->residues;i++){
298 ci->residue_type[i]=oggpack_read(opb,16);
299 if(ci->residue_type[i]<0 || ci->residue_type[i]>=VI_RESB)goto err_out;
300 ci->residue_param[i]=_residue_P[ci->residue_type[i]]->unpack(vi,opb);
301 if(!ci->residue_param[i])goto err_out;
304 /* map backend settings */
305 ci->maps=oggpack_read(opb,6)+1;
306 if(ci->maps<=0)goto err_out;
307 for(i=0;i<ci->maps;i++){
308 ci->map_type[i]=oggpack_read(opb,16);
309 if(ci->map_type[i]<0 || ci->map_type[i]>=VI_MAPB)goto err_out;
310 ci->map_param[i]=_mapping_P[ci->map_type[i]]->unpack(vi,opb);
311 if(!ci->map_param[i])goto err_out;
315 ci->modes=oggpack_read(opb,6)+1;
316 if(ci->modes<=0)goto err_out;
317 for(i=0;i<ci->modes;i++){
318 ci->mode_param[i]=_ogg_calloc(1,sizeof(*ci->mode_param[i]));
319 ci->mode_param[i]->blockflag=oggpack_read(opb,1);
320 ci->mode_param[i]->windowtype=oggpack_read(opb,16);
321 ci->mode_param[i]->transformtype=oggpack_read(opb,16);
322 ci->mode_param[i]->mapping=oggpack_read(opb,8);
324 if(ci->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
325 if(ci->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
326 if(ci->mode_param[i]->mapping>=ci->maps)goto err_out;
327 if(ci->mode_param[i]->mapping<0)goto err_out;
330 if(oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
334 vorbis_info_clear(vi);
335 return(OV_EBADHEADER);
338 /* Is this packet a vorbis ID header? */
339 int vorbis_synthesis_idheader(ogg_packet *op){
344 oggpack_readinit(&opb,op->packet,op->bytes);
347 return(0); /* Not the initial packet */
349 if(oggpack_read(&opb,8) != 1)
350 return 0; /* not an ID header */
353 _v_readstring(&opb,buffer,6);
354 if(memcmp(buffer,"vorbis",6))
355 return 0; /* not vorbis */
363 /* The Vorbis header is in three packets; the initial small packet in
364 the first page that identifies basic parameters, a second packet
365 with bitstream comments and a third packet that holds the
368 int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
372 oggpack_readinit(&opb,op->packet,op->bytes);
374 /* Which of the three types of header is this? */
375 /* Also verify header-ness, vorbis */
378 int packtype=oggpack_read(&opb,8);
380 _v_readstring(&opb,buffer,6);
381 if(memcmp(buffer,"vorbis",6)){
382 /* not a vorbis header */
383 return(OV_ENOTVORBIS);
386 case 0x01: /* least significant *bit* is read first */
388 /* Not the initial packet */
389 return(OV_EBADHEADER);
392 /* previously initialized info header */
393 return(OV_EBADHEADER);
396 return(_vorbis_unpack_info(vi,&opb));
398 case 0x03: /* least significant *bit* is read first */
400 /* um... we didn't get the initial header */
401 return(OV_EBADHEADER);
403 if(vc->vendor!=NULL){
404 /* previously initialized comment header */
405 return(OV_EBADHEADER);
408 return(_vorbis_unpack_comment(vc,&opb));
410 case 0x05: /* least significant *bit* is read first */
411 if(vi->rate==0 || vc->vendor==NULL){
412 /* um... we didn;t get the initial header or comments yet */
413 return(OV_EBADHEADER);
415 if(vi->codec_setup==NULL){
416 /* improperly initialized vorbis_info */
419 if(((codec_setup_info *)vi->codec_setup)->books>0){
420 /* previously initialized setup header */
421 return(OV_EBADHEADER);
424 return(_vorbis_unpack_books(vi,&opb));
427 /* Not a valid vorbis header type */
428 return(OV_EBADHEADER);
433 return(OV_EBADHEADER);
436 /* pack side **********************************************************/
438 static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){
439 codec_setup_info *ci=vi->codec_setup;
441 ci->blocksizes[0]<64||
442 ci->blocksizes[1]<ci->blocksizes[0]){
447 oggpack_write(opb,0x01,8);
448 _v_writestring(opb,"vorbis", 6);
450 /* basic information about the stream */
451 oggpack_write(opb,0x00,32);
452 oggpack_write(opb,vi->channels,8);
453 oggpack_write(opb,vi->rate,32);
455 oggpack_write(opb,vi->bitrate_upper,32);
456 oggpack_write(opb,vi->bitrate_nominal,32);
457 oggpack_write(opb,vi->bitrate_lower,32);
459 oggpack_write(opb,ov_ilog(ci->blocksizes[0]-1),4);
460 oggpack_write(opb,ov_ilog(ci->blocksizes[1]-1),4);
461 oggpack_write(opb,1,1);
466 static int _vorbis_pack_comment(oggpack_buffer *opb,vorbis_comment *vc){
467 int bytes = strlen(ENCODE_VENDOR_STRING);
470 oggpack_write(opb,0x03,8);
471 _v_writestring(opb,"vorbis", 6);
474 oggpack_write(opb,bytes,32);
475 _v_writestring(opb,ENCODE_VENDOR_STRING, bytes);
479 oggpack_write(opb,vc->comments,32);
482 for(i=0;i<vc->comments;i++){
483 if(vc->user_comments[i]){
484 oggpack_write(opb,vc->comment_lengths[i],32);
485 _v_writestring(opb,vc->user_comments[i], vc->comment_lengths[i]);
487 oggpack_write(opb,0,32);
491 oggpack_write(opb,1,1);
496 static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){
497 codec_setup_info *ci=vi->codec_setup;
499 if(!ci)return(OV_EFAULT);
501 oggpack_write(opb,0x05,8);
502 _v_writestring(opb,"vorbis", 6);
505 oggpack_write(opb,ci->books-1,8);
506 for(i=0;i<ci->books;i++)
507 if(vorbis_staticbook_pack(ci->book_param[i],opb))goto err_out;
509 /* times; hook placeholders */
510 oggpack_write(opb,0,6);
511 oggpack_write(opb,0,16);
514 oggpack_write(opb,ci->floors-1,6);
515 for(i=0;i<ci->floors;i++){
516 oggpack_write(opb,ci->floor_type[i],16);
517 if(_floor_P[ci->floor_type[i]]->pack)
518 _floor_P[ci->floor_type[i]]->pack(ci->floor_param[i],opb);
524 oggpack_write(opb,ci->residues-1,6);
525 for(i=0;i<ci->residues;i++){
526 oggpack_write(opb,ci->residue_type[i],16);
527 _residue_P[ci->residue_type[i]]->pack(ci->residue_param[i],opb);
531 oggpack_write(opb,ci->maps-1,6);
532 for(i=0;i<ci->maps;i++){
533 oggpack_write(opb,ci->map_type[i],16);
534 _mapping_P[ci->map_type[i]]->pack(vi,ci->map_param[i],opb);
538 oggpack_write(opb,ci->modes-1,6);
539 for(i=0;i<ci->modes;i++){
540 oggpack_write(opb,ci->mode_param[i]->blockflag,1);
541 oggpack_write(opb,ci->mode_param[i]->windowtype,16);
542 oggpack_write(opb,ci->mode_param[i]->transformtype,16);
543 oggpack_write(opb,ci->mode_param[i]->mapping,8);
545 oggpack_write(opb,1,1);
552 int vorbis_commentheader_out(vorbis_comment *vc,
557 oggpack_writeinit(&opb);
558 if(_vorbis_pack_comment(&opb,vc)){
559 oggpack_writeclear(&opb);
563 op->packet = _ogg_malloc(oggpack_bytes(&opb));
564 memcpy(op->packet, opb.buffer, oggpack_bytes(&opb));
566 op->bytes=oggpack_bytes(&opb);
572 oggpack_writeclear(&opb);
576 int vorbis_analysis_headerout(vorbis_dsp_state *v,
580 ogg_packet *op_code){
582 vorbis_info *vi=v->vi;
584 private_state *b=v->backend_state;
586 if(!b||vi->channels<=0){
591 /* first header packet **********************************************/
593 oggpack_writeinit(&opb);
594 if(_vorbis_pack_info(&opb,vi))goto err_out;
596 /* build the packet */
597 if(b->header)_ogg_free(b->header);
598 b->header=_ogg_malloc(oggpack_bytes(&opb));
599 memcpy(b->header,opb.buffer,oggpack_bytes(&opb));
600 op->packet=b->header;
601 op->bytes=oggpack_bytes(&opb);
607 /* second header packet (comments) **********************************/
610 if(_vorbis_pack_comment(&opb,vc))goto err_out;
612 if(b->header1)_ogg_free(b->header1);
613 b->header1=_ogg_malloc(oggpack_bytes(&opb));
614 memcpy(b->header1,opb.buffer,oggpack_bytes(&opb));
615 op_comm->packet=b->header1;
616 op_comm->bytes=oggpack_bytes(&opb);
619 op_comm->granulepos=0;
622 /* third header packet (modes/codebooks) ****************************/
625 if(_vorbis_pack_books(&opb,vi))goto err_out;
627 if(b->header2)_ogg_free(b->header2);
628 b->header2=_ogg_malloc(oggpack_bytes(&opb));
629 memcpy(b->header2,opb.buffer,oggpack_bytes(&opb));
630 op_code->packet=b->header2;
631 op_code->bytes=oggpack_bytes(&opb);
634 op_code->granulepos=0;
637 oggpack_writeclear(&opb);
640 memset(op,0,sizeof(*op));
641 memset(op_comm,0,sizeof(*op_comm));
642 memset(op_code,0,sizeof(*op_code));
645 oggpack_writeclear(&opb);
646 if(b->header)_ogg_free(b->header);
647 if(b->header1)_ogg_free(b->header1);
648 if(b->header2)_ogg_free(b->header2);
656 double vorbis_granule_time(vorbis_dsp_state *v,ogg_int64_t granulepos){
657 if(granulepos == -1) return -1;
659 /* We're not guaranteed a 64 bit unsigned type everywhere, so we
660 have to put the unsigned granpo in a signed type. */
662 return((double)granulepos/v->vi->rate);
664 ogg_int64_t granuleoff=0xffffffff;
666 granuleoff|=0x7ffffffff;
667 return(((double)granulepos+2+granuleoff+granuleoff)/v->vi->rate);
671 const char *vorbis_version_string(void){
672 return GENERAL_VENDOR_STRING;