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 https://xiph.org/ *
11 ********************************************************************
13 function: maintain the info structure, info <-> header packets
15 ********************************************************************/
17 /* general handling of the header and the vorbis_info structure (and
23 #include "vorbis/codec.h"
24 #include "codec_internal.h"
32 #define GENERAL_VENDOR_STRING "Xiph.Org libVorbis 1.3.7"
33 #define ENCODE_VENDOR_STRING "Xiph.Org libVorbis I 20200704 (Reducing Environment)"
36 static void _v_writestring(oggpack_buffer *o,const char *s, int bytes){
39 oggpack_write(o,*s++,8);
43 static void _v_readstring(oggpack_buffer *o,char *buf,int bytes){
45 *buf++=oggpack_read(o,8);
49 static int _v_toupper(int c) {
50 return (c >= 'a' && c <= 'z') ? (c & ~('a' - 'A')) : c;
53 void vorbis_comment_init(vorbis_comment *vc){
54 memset(vc,0,sizeof(*vc));
57 void vorbis_comment_add(vorbis_comment *vc,const char *comment){
58 vc->user_comments=_ogg_realloc(vc->user_comments,
59 (vc->comments+2)*sizeof(*vc->user_comments));
60 vc->comment_lengths=_ogg_realloc(vc->comment_lengths,
61 (vc->comments+2)*sizeof(*vc->comment_lengths));
62 vc->comment_lengths[vc->comments]=strlen(comment);
63 vc->user_comments[vc->comments]=_ogg_malloc(vc->comment_lengths[vc->comments]+1);
64 strcpy(vc->user_comments[vc->comments], comment);
66 vc->user_comments[vc->comments]=NULL;
69 void vorbis_comment_add_tag(vorbis_comment *vc, const char *tag, const char *contents){
70 /* Length for key and value +2 for = and \0 */
71 char *comment=_ogg_malloc(strlen(tag)+strlen(contents)+2);
74 strcat(comment, contents);
75 vorbis_comment_add(vc, comment);
79 /* This is more or less the same as strncasecmp - but that doesn't exist
80 * everywhere, and this is a fairly trivial function, so we include it */
81 static int tagcompare(const char *s1, const char *s2, int n){
84 if(_v_toupper(s1[c]) != _v_toupper(s2[c]))
91 char *vorbis_comment_query(vorbis_comment *vc, const char *tag, int count){
94 int taglen = strlen(tag)+1; /* +1 for the = we append */
95 char *fulltag = _ogg_malloc(taglen+1);
100 for(i=0;i<vc->comments;i++){
101 if(!tagcompare(vc->user_comments[i], fulltag, taglen)){
103 /* We return a pointer to the data, not a copy */
105 return vc->user_comments[i] + taglen;
112 return NULL; /* didn't find anything */
115 int vorbis_comment_query_count(vorbis_comment *vc, const char *tag){
117 int taglen = strlen(tag)+1; /* +1 for the = we append */
118 char *fulltag = _ogg_malloc(taglen+1);
120 strcat(fulltag, "=");
122 for(i=0;i<vc->comments;i++){
123 if(!tagcompare(vc->user_comments[i], fulltag, taglen))
131 void vorbis_comment_clear(vorbis_comment *vc){
134 if(vc->user_comments){
135 for(i=0;i<vc->comments;i++)
136 if(vc->user_comments[i])_ogg_free(vc->user_comments[i]);
137 _ogg_free(vc->user_comments);
139 if(vc->comment_lengths)_ogg_free(vc->comment_lengths);
140 if(vc->vendor)_ogg_free(vc->vendor);
141 memset(vc,0,sizeof(*vc));
145 /* blocksize 0 is guaranteed to be short, 1 is guaranteed to be long.
146 They may be equal, but short will never ge greater than long */
147 int vorbis_info_blocksize(vorbis_info *vi,int zo){
148 codec_setup_info *ci = vi->codec_setup;
149 return ci ? ci->blocksizes[zo] : -1;
152 /* used by synthesis, which has a full, alloced vi */
153 void vorbis_info_init(vorbis_info *vi){
154 memset(vi,0,sizeof(*vi));
155 vi->codec_setup=_ogg_calloc(1,sizeof(codec_setup_info));
158 void vorbis_info_clear(vorbis_info *vi){
159 codec_setup_info *ci=vi->codec_setup;
164 for(i=0;i<ci->modes;i++)
165 if(ci->mode_param[i])_ogg_free(ci->mode_param[i]);
167 for(i=0;i<ci->maps;i++) /* unpack does the range checking */
168 if(ci->map_param[i]) /* this may be cleaning up an aborted
169 unpack, in which case the below type
171 _mapping_P[ci->map_type[i]]->free_info(ci->map_param[i]);
173 for(i=0;i<ci->floors;i++) /* unpack does the range checking */
174 if(ci->floor_param[i]) /* this may be cleaning up an aborted
175 unpack, in which case the below type
177 _floor_P[ci->floor_type[i]]->free_info(ci->floor_param[i]);
179 for(i=0;i<ci->residues;i++) /* unpack does the range checking */
180 if(ci->residue_param[i]) /* this may be cleaning up an aborted
181 unpack, in which case the below type
183 _residue_P[ci->residue_type[i]]->free_info(ci->residue_param[i]);
185 for(i=0;i<ci->books;i++){
186 if(ci->book_param[i]){
187 /* knows if the book was not alloced */
188 vorbis_staticbook_destroy(ci->book_param[i]);
191 vorbis_book_clear(ci->fullbooks+i);
194 _ogg_free(ci->fullbooks);
196 for(i=0;i<ci->psys;i++)
197 _vi_psy_free(ci->psy_param[i]);
202 memset(vi,0,sizeof(*vi));
205 /* Header packing/unpacking ********************************************/
207 static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){
208 codec_setup_info *ci=vi->codec_setup;
210 if(!ci)return(OV_EFAULT);
212 vi->version=oggpack_read(opb,32);
213 if(vi->version!=0)return(OV_EVERSION);
215 vi->channels=oggpack_read(opb,8);
216 vi->rate=oggpack_read(opb,32);
218 vi->bitrate_upper=(ogg_int32_t)oggpack_read(opb,32);
219 vi->bitrate_nominal=(ogg_int32_t)oggpack_read(opb,32);
220 vi->bitrate_lower=(ogg_int32_t)oggpack_read(opb,32);
222 bs = oggpack_read(opb,4);
223 if(bs<0)goto err_out;
224 ci->blocksizes[0]=1<<bs;
225 bs = oggpack_read(opb,4);
226 if(bs<0)goto err_out;
227 ci->blocksizes[1]=1<<bs;
229 if(vi->rate<1)goto err_out;
230 if(vi->channels<1)goto err_out;
231 if(ci->blocksizes[0]<64)goto err_out;
232 if(ci->blocksizes[1]<ci->blocksizes[0])goto err_out;
233 if(ci->blocksizes[1]>8192)goto err_out;
235 if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
239 vorbis_info_clear(vi);
240 return(OV_EBADHEADER);
243 static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){
245 int vendorlen=oggpack_read(opb,32);
246 if(vendorlen<0)goto err_out;
247 if(vendorlen>opb->storage-8)goto err_out;
248 vc->vendor=_ogg_calloc(vendorlen+1,1);
249 _v_readstring(opb,vc->vendor,vendorlen);
250 i=oggpack_read(opb,32);
252 if(i>((opb->storage-oggpack_bytes(opb))>>2))goto err_out;
254 vc->user_comments=_ogg_calloc(vc->comments+1,sizeof(*vc->user_comments));
255 vc->comment_lengths=_ogg_calloc(vc->comments+1, sizeof(*vc->comment_lengths));
257 for(i=0;i<vc->comments;i++){
258 int len=oggpack_read(opb,32);
259 if(len<0)goto err_out;
260 if(len>opb->storage-oggpack_bytes(opb))goto err_out;
261 vc->comment_lengths[i]=len;
262 vc->user_comments[i]=_ogg_calloc(len+1,1);
263 _v_readstring(opb,vc->user_comments[i],len);
265 if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */
269 vorbis_comment_clear(vc);
270 return(OV_EBADHEADER);
273 /* all of the real encoding details are here. The modes, books,
275 static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){
276 codec_setup_info *ci=vi->codec_setup;
280 ci->books=oggpack_read(opb,8)+1;
281 if(ci->books<=0)goto err_out;
282 for(i=0;i<ci->books;i++){
283 ci->book_param[i]=vorbis_staticbook_unpack(opb);
284 if(!ci->book_param[i])goto err_out;
287 /* time backend settings; hooks are unused */
289 int times=oggpack_read(opb,6)+1;
290 if(times<=0)goto err_out;
291 for(i=0;i<times;i++){
292 int test=oggpack_read(opb,16);
293 if(test<0 || test>=VI_TIMEB)goto err_out;
297 /* floor backend settings */
298 ci->floors=oggpack_read(opb,6)+1;
299 if(ci->floors<=0)goto err_out;
300 for(i=0;i<ci->floors;i++){
301 ci->floor_type[i]=oggpack_read(opb,16);
302 if(ci->floor_type[i]<0 || ci->floor_type[i]>=VI_FLOORB)goto err_out;
303 ci->floor_param[i]=_floor_P[ci->floor_type[i]]->unpack(vi,opb);
304 if(!ci->floor_param[i])goto err_out;
307 /* residue backend settings */
308 ci->residues=oggpack_read(opb,6)+1;
309 if(ci->residues<=0)goto err_out;
310 for(i=0;i<ci->residues;i++){
311 ci->residue_type[i]=oggpack_read(opb,16);
312 if(ci->residue_type[i]<0 || ci->residue_type[i]>=VI_RESB)goto err_out;
313 ci->residue_param[i]=_residue_P[ci->residue_type[i]]->unpack(vi,opb);
314 if(!ci->residue_param[i])goto err_out;
317 /* map backend settings */
318 ci->maps=oggpack_read(opb,6)+1;
319 if(ci->maps<=0)goto err_out;
320 for(i=0;i<ci->maps;i++){
321 ci->map_type[i]=oggpack_read(opb,16);
322 if(ci->map_type[i]<0 || ci->map_type[i]>=VI_MAPB)goto err_out;
323 ci->map_param[i]=_mapping_P[ci->map_type[i]]->unpack(vi,opb);
324 if(!ci->map_param[i])goto err_out;
328 ci->modes=oggpack_read(opb,6)+1;
329 if(ci->modes<=0)goto err_out;
330 for(i=0;i<ci->modes;i++){
331 ci->mode_param[i]=_ogg_calloc(1,sizeof(*ci->mode_param[i]));
332 ci->mode_param[i]->blockflag=oggpack_read(opb,1);
333 ci->mode_param[i]->windowtype=oggpack_read(opb,16);
334 ci->mode_param[i]->transformtype=oggpack_read(opb,16);
335 ci->mode_param[i]->mapping=oggpack_read(opb,8);
337 if(ci->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out;
338 if(ci->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out;
339 if(ci->mode_param[i]->mapping>=ci->maps)goto err_out;
340 if(ci->mode_param[i]->mapping<0)goto err_out;
343 if(oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */
347 vorbis_info_clear(vi);
348 return(OV_EBADHEADER);
351 /* Is this packet a vorbis ID header? */
352 int vorbis_synthesis_idheader(ogg_packet *op){
357 oggpack_readinit(&opb,op->packet,op->bytes);
360 return(0); /* Not the initial packet */
362 if(oggpack_read(&opb,8) != 1)
363 return 0; /* not an ID header */
366 _v_readstring(&opb,buffer,6);
367 if(memcmp(buffer,"vorbis",6))
368 return 0; /* not vorbis */
376 /* The Vorbis header is in three packets; the initial small packet in
377 the first page that identifies basic parameters, a second packet
378 with bitstream comments and a third packet that holds the
381 int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){
385 oggpack_readinit(&opb,op->packet,op->bytes);
387 /* Which of the three types of header is this? */
388 /* Also verify header-ness, vorbis */
391 int packtype=oggpack_read(&opb,8);
393 _v_readstring(&opb,buffer,6);
394 if(memcmp(buffer,"vorbis",6)){
395 /* not a vorbis header */
396 return(OV_ENOTVORBIS);
399 case 0x01: /* least significant *bit* is read first */
401 /* Not the initial packet */
402 return(OV_EBADHEADER);
405 /* previously initialized info header */
406 return(OV_EBADHEADER);
409 return(_vorbis_unpack_info(vi,&opb));
411 case 0x03: /* least significant *bit* is read first */
413 /* um... we didn't get the initial header */
414 return(OV_EBADHEADER);
416 if(vc->vendor!=NULL){
417 /* previously initialized comment header */
418 return(OV_EBADHEADER);
421 return(_vorbis_unpack_comment(vc,&opb));
423 case 0x05: /* least significant *bit* is read first */
424 if(vi->rate==0 || vc->vendor==NULL){
425 /* um... we didn;t get the initial header or comments yet */
426 return(OV_EBADHEADER);
428 if(vi->codec_setup==NULL){
429 /* improperly initialized vorbis_info */
432 if(((codec_setup_info *)vi->codec_setup)->books>0){
433 /* previously initialized setup header */
434 return(OV_EBADHEADER);
437 return(_vorbis_unpack_books(vi,&opb));
440 /* Not a valid vorbis header type */
441 return(OV_EBADHEADER);
446 return(OV_EBADHEADER);
449 /* pack side **********************************************************/
451 static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){
452 codec_setup_info *ci=vi->codec_setup;
454 ci->blocksizes[0]<64||
455 ci->blocksizes[1]<ci->blocksizes[0]){
460 oggpack_write(opb,0x01,8);
461 _v_writestring(opb,"vorbis", 6);
463 /* basic information about the stream */
464 oggpack_write(opb,0x00,32);
465 oggpack_write(opb,vi->channels,8);
466 oggpack_write(opb,vi->rate,32);
468 oggpack_write(opb,vi->bitrate_upper,32);
469 oggpack_write(opb,vi->bitrate_nominal,32);
470 oggpack_write(opb,vi->bitrate_lower,32);
472 oggpack_write(opb,ov_ilog(ci->blocksizes[0]-1),4);
473 oggpack_write(opb,ov_ilog(ci->blocksizes[1]-1),4);
474 oggpack_write(opb,1,1);
479 static int _vorbis_pack_comment(oggpack_buffer *opb,vorbis_comment *vc){
480 int bytes = strlen(ENCODE_VENDOR_STRING);
483 oggpack_write(opb,0x03,8);
484 _v_writestring(opb,"vorbis", 6);
487 oggpack_write(opb,bytes,32);
488 _v_writestring(opb,ENCODE_VENDOR_STRING, bytes);
492 oggpack_write(opb,vc->comments,32);
495 for(i=0;i<vc->comments;i++){
496 if(vc->user_comments[i]){
497 oggpack_write(opb,vc->comment_lengths[i],32);
498 _v_writestring(opb,vc->user_comments[i], vc->comment_lengths[i]);
500 oggpack_write(opb,0,32);
504 oggpack_write(opb,1,1);
509 static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){
510 codec_setup_info *ci=vi->codec_setup;
512 if(!ci)return(OV_EFAULT);
514 oggpack_write(opb,0x05,8);
515 _v_writestring(opb,"vorbis", 6);
518 oggpack_write(opb,ci->books-1,8);
519 for(i=0;i<ci->books;i++)
520 if(vorbis_staticbook_pack(ci->book_param[i],opb))goto err_out;
522 /* times; hook placeholders */
523 oggpack_write(opb,0,6);
524 oggpack_write(opb,0,16);
527 oggpack_write(opb,ci->floors-1,6);
528 for(i=0;i<ci->floors;i++){
529 oggpack_write(opb,ci->floor_type[i],16);
530 if(_floor_P[ci->floor_type[i]]->pack)
531 _floor_P[ci->floor_type[i]]->pack(ci->floor_param[i],opb);
537 oggpack_write(opb,ci->residues-1,6);
538 for(i=0;i<ci->residues;i++){
539 oggpack_write(opb,ci->residue_type[i],16);
540 _residue_P[ci->residue_type[i]]->pack(ci->residue_param[i],opb);
544 oggpack_write(opb,ci->maps-1,6);
545 for(i=0;i<ci->maps;i++){
546 oggpack_write(opb,ci->map_type[i],16);
547 _mapping_P[ci->map_type[i]]->pack(vi,ci->map_param[i],opb);
551 oggpack_write(opb,ci->modes-1,6);
552 for(i=0;i<ci->modes;i++){
553 oggpack_write(opb,ci->mode_param[i]->blockflag,1);
554 oggpack_write(opb,ci->mode_param[i]->windowtype,16);
555 oggpack_write(opb,ci->mode_param[i]->transformtype,16);
556 oggpack_write(opb,ci->mode_param[i]->mapping,8);
558 oggpack_write(opb,1,1);
565 int vorbis_commentheader_out(vorbis_comment *vc,
570 oggpack_writeinit(&opb);
571 if(_vorbis_pack_comment(&opb,vc)){
572 oggpack_writeclear(&opb);
576 op->packet = _ogg_malloc(oggpack_bytes(&opb));
577 memcpy(op->packet, opb.buffer, oggpack_bytes(&opb));
579 op->bytes=oggpack_bytes(&opb);
585 oggpack_writeclear(&opb);
589 int vorbis_analysis_headerout(vorbis_dsp_state *v,
593 ogg_packet *op_code){
595 vorbis_info *vi=v->vi;
597 private_state *b=v->backend_state;
599 if(!b||vi->channels<=0||vi->channels>256){
605 /* first header packet **********************************************/
607 oggpack_writeinit(&opb);
608 if(_vorbis_pack_info(&opb,vi))goto err_out;
610 /* build the packet */
611 if(b->header)_ogg_free(b->header);
612 b->header=_ogg_malloc(oggpack_bytes(&opb));
613 memcpy(b->header,opb.buffer,oggpack_bytes(&opb));
614 op->packet=b->header;
615 op->bytes=oggpack_bytes(&opb);
621 /* second header packet (comments) **********************************/
624 if(_vorbis_pack_comment(&opb,vc))goto err_out;
626 if(b->header1)_ogg_free(b->header1);
627 b->header1=_ogg_malloc(oggpack_bytes(&opb));
628 memcpy(b->header1,opb.buffer,oggpack_bytes(&opb));
629 op_comm->packet=b->header1;
630 op_comm->bytes=oggpack_bytes(&opb);
633 op_comm->granulepos=0;
636 /* third header packet (modes/codebooks) ****************************/
639 if(_vorbis_pack_books(&opb,vi))goto err_out;
641 if(b->header2)_ogg_free(b->header2);
642 b->header2=_ogg_malloc(oggpack_bytes(&opb));
643 memcpy(b->header2,opb.buffer,oggpack_bytes(&opb));
644 op_code->packet=b->header2;
645 op_code->bytes=oggpack_bytes(&opb);
648 op_code->granulepos=0;
651 oggpack_writeclear(&opb);
654 memset(op,0,sizeof(*op));
655 memset(op_comm,0,sizeof(*op_comm));
656 memset(op_code,0,sizeof(*op_code));
659 if(vi->channels>0)oggpack_writeclear(&opb);
660 if(b->header)_ogg_free(b->header);
661 if(b->header1)_ogg_free(b->header1);
662 if(b->header2)_ogg_free(b->header2);
670 double vorbis_granule_time(vorbis_dsp_state *v,ogg_int64_t granulepos){
671 if(granulepos == -1) return -1;
673 /* We're not guaranteed a 64 bit unsigned type everywhere, so we
674 have to put the unsigned granpo in a signed type. */
676 return((double)granulepos/v->vi->rate);
678 ogg_int64_t granuleoff=0xffffffff;
680 granuleoff|=0x7ffffffff;
681 return(((double)granulepos+2+granuleoff+granuleoff)/v->vi->rate);
685 const char *vorbis_version_string(void){
686 return GENERAL_VENDOR_STRING;