michael@0: /******************************************************************** michael@0: * * michael@0: * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE. * michael@0: * * michael@0: * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * michael@0: * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * michael@0: * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * michael@0: * * michael@0: * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2003 * michael@0: * BY THE Xiph.Org FOUNDATION http://www.xiph.org/ * michael@0: * * michael@0: ******************************************************************** michael@0: michael@0: function: maintain the info structure, info <-> header packets michael@0: michael@0: ********************************************************************/ michael@0: michael@0: /* general handling of the header and the vorbis_info structure (and michael@0: substructures) */ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include "ivorbiscodec.h" michael@0: #include "codec_internal.h" michael@0: #include "codebook.h" michael@0: #include "registry.h" michael@0: #include "window.h" michael@0: #include "misc.h" michael@0: michael@0: /* helpers */ michael@0: static void _v_readstring(oggpack_buffer *o,char *buf,int bytes){ michael@0: while(bytes--){ michael@0: *buf++=oggpack_read(o,8); michael@0: } michael@0: } michael@0: michael@0: void vorbis_comment_init(vorbis_comment *vc){ michael@0: memset(vc,0,sizeof(*vc)); michael@0: } michael@0: michael@0: /* This is more or less the same as strncasecmp - but that doesn't exist michael@0: * everywhere, and this is a fairly trivial function, so we include it */ michael@0: static int tagcompare(const char *s1, const char *s2, int n){ michael@0: int c=0; michael@0: while(c < n){ michael@0: if(toupper(s1[c]) != toupper(s2[c])) michael@0: return !0; michael@0: c++; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: char *vorbis_comment_query(vorbis_comment *vc, char *tag, int count){ michael@0: long i; michael@0: int found = 0; michael@0: int taglen = strlen(tag)+1; /* +1 for the = we append */ michael@0: char *fulltag = (char *)alloca(taglen+ 1); michael@0: michael@0: strcpy(fulltag, tag); michael@0: strcat(fulltag, "="); michael@0: michael@0: for(i=0;icomments;i++){ michael@0: if(!tagcompare(vc->user_comments[i], fulltag, taglen)){ michael@0: if(count == found) michael@0: /* We return a pointer to the data, not a copy */ michael@0: return vc->user_comments[i] + taglen; michael@0: else michael@0: found++; michael@0: } michael@0: } michael@0: return NULL; /* didn't find anything */ michael@0: } michael@0: michael@0: int vorbis_comment_query_count(vorbis_comment *vc, char *tag){ michael@0: int i,count=0; michael@0: int taglen = strlen(tag)+1; /* +1 for the = we append */ michael@0: char *fulltag = (char *)alloca(taglen+1); michael@0: strcpy(fulltag,tag); michael@0: strcat(fulltag, "="); michael@0: michael@0: for(i=0;icomments;i++){ michael@0: if(!tagcompare(vc->user_comments[i], fulltag, taglen)) michael@0: count++; michael@0: } michael@0: michael@0: return count; michael@0: } michael@0: michael@0: void vorbis_comment_clear(vorbis_comment *vc){ michael@0: if(vc){ michael@0: long i; michael@0: if(vc->user_comments){ michael@0: for(i=0;icomments;i++) michael@0: if(vc->user_comments[i])_ogg_free(vc->user_comments[i]); michael@0: _ogg_free(vc->user_comments); michael@0: } michael@0: if(vc->comment_lengths)_ogg_free(vc->comment_lengths); michael@0: if(vc->vendor)_ogg_free(vc->vendor); michael@0: memset(vc,0,sizeof(*vc)); michael@0: } michael@0: } michael@0: michael@0: /* blocksize 0 is guaranteed to be short, 1 is guarantted to be long. michael@0: They may be equal, but short will never ge greater than long */ michael@0: int vorbis_info_blocksize(vorbis_info *vi,int zo){ michael@0: codec_setup_info *ci = (codec_setup_info *)vi->codec_setup; michael@0: return ci ? ci->blocksizes[zo] : -1; michael@0: } michael@0: michael@0: /* used by synthesis, which has a full, alloced vi */ michael@0: void vorbis_info_init(vorbis_info *vi){ michael@0: memset(vi,0,sizeof(*vi)); michael@0: vi->codec_setup=(codec_setup_info *)_ogg_calloc(1,sizeof(codec_setup_info)); michael@0: } michael@0: michael@0: void vorbis_info_clear(vorbis_info *vi){ michael@0: codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; michael@0: int i; michael@0: michael@0: if(ci){ michael@0: michael@0: for(i=0;imodes;i++) michael@0: if(ci->mode_param[i])_ogg_free(ci->mode_param[i]); michael@0: michael@0: for(i=0;imaps;i++) /* unpack does the range checking */ michael@0: if(ci->map_param[i]) michael@0: _mapping_P[ci->map_type[i]]->free_info(ci->map_param[i]); michael@0: michael@0: for(i=0;ifloors;i++) /* unpack does the range checking */ michael@0: if(ci->floor_param[i]) michael@0: _floor_P[ci->floor_type[i]]->free_info(ci->floor_param[i]); michael@0: michael@0: for(i=0;iresidues;i++) /* unpack does the range checking */ michael@0: if(ci->residue_param[i]) michael@0: _residue_P[ci->residue_type[i]]->free_info(ci->residue_param[i]); michael@0: michael@0: for(i=0;ibooks;i++){ michael@0: if(ci->book_param[i]){ michael@0: /* knows if the book was not alloced */ michael@0: vorbis_staticbook_destroy(ci->book_param[i]); michael@0: } michael@0: if(ci->fullbooks) michael@0: vorbis_book_clear(ci->fullbooks+i); michael@0: } michael@0: if(ci->fullbooks) michael@0: _ogg_free(ci->fullbooks); michael@0: michael@0: _ogg_free(ci); michael@0: } michael@0: michael@0: memset(vi,0,sizeof(*vi)); michael@0: } michael@0: michael@0: /* Header packing/unpacking ********************************************/ michael@0: michael@0: static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){ michael@0: codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; michael@0: if(!ci)return(OV_EFAULT); michael@0: michael@0: vi->version=oggpack_read(opb,32); michael@0: if(vi->version!=0)return(OV_EVERSION); michael@0: michael@0: vi->channels=oggpack_read(opb,8); michael@0: vi->rate=oggpack_read(opb,32); michael@0: michael@0: vi->bitrate_upper=oggpack_read(opb,32); michael@0: vi->bitrate_nominal=oggpack_read(opb,32); michael@0: vi->bitrate_lower=oggpack_read(opb,32); michael@0: michael@0: ci->blocksizes[0]=1<blocksizes[1]=1<rate<1)goto err_out; michael@0: if(vi->channels<1)goto err_out; michael@0: if(ci->blocksizes[0]<64)goto err_out; michael@0: if(ci->blocksizes[1]blocksizes[0])goto err_out; michael@0: if(ci->blocksizes[1]>8192)goto err_out; michael@0: michael@0: if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */ michael@0: michael@0: return(0); michael@0: err_out: michael@0: vorbis_info_clear(vi); michael@0: return(OV_EBADHEADER); michael@0: } michael@0: michael@0: static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){ michael@0: int i; michael@0: int vendorlen; michael@0: vendorlen=oggpack_read(opb,32); michael@0: if(vendorlen<0)goto err_out; michael@0: if(vendorlen>opb->storage-oggpack_bytes(opb))goto err_out; michael@0: vc->vendor=(char *)_ogg_calloc(vendorlen+1,1); michael@0: if(vc->vendor==NULL)goto err_out; michael@0: _v_readstring(opb,vc->vendor,vendorlen); michael@0: i=oggpack_read(opb,32); michael@0: if(i<0||i>=INT_MAX||i>(opb->storage-oggpack_bytes(opb))>>2)goto err_out; michael@0: vc->user_comments=(char **)_ogg_calloc(i+1,sizeof(*vc->user_comments)); michael@0: vc->comment_lengths=(int *)_ogg_calloc(i+1, sizeof(*vc->comment_lengths)); michael@0: if(vc->user_comments==NULL||vc->comment_lengths==NULL)goto err_out; michael@0: vc->comments=i; michael@0: michael@0: for(i=0;icomments;i++){ michael@0: int len=oggpack_read(opb,32); michael@0: if(len<0||len>opb->storage-oggpack_bytes(opb))goto err_out; michael@0: vc->comment_lengths[i]=len; michael@0: vc->user_comments[i]=(char *)_ogg_calloc(len+1,1); michael@0: if(vc->user_comments[i]==NULL){ michael@0: vc->comments=i; michael@0: goto err_out; michael@0: } michael@0: _v_readstring(opb,vc->user_comments[i],len); michael@0: } michael@0: if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */ michael@0: michael@0: return(0); michael@0: err_out: michael@0: vorbis_comment_clear(vc); michael@0: return(OV_EBADHEADER); michael@0: } michael@0: michael@0: /* all of the real encoding details are here. The modes, books, michael@0: everything */ michael@0: static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){ michael@0: codec_setup_info *ci=(codec_setup_info *)vi->codec_setup; michael@0: int i; michael@0: if(!ci)return(OV_EFAULT); michael@0: michael@0: /* codebooks */ michael@0: ci->books=oggpack_read(opb,8)+1; michael@0: if(ci->books<=0)goto err_out; michael@0: for(i=0;ibooks;i++){ michael@0: ci->book_param[i]=vorbis_staticbook_unpack(opb); michael@0: if(!ci->book_param[i])goto err_out; michael@0: } michael@0: michael@0: /* time backend settings */ michael@0: ci->times=oggpack_read(opb,6)+1; michael@0: if(ci->times<=0)goto err_out; michael@0: for(i=0;itimes;i++){ michael@0: ci->time_type[i]=oggpack_read(opb,16); michael@0: if(ci->time_type[i]<0 || ci->time_type[i]>=VI_TIMEB)goto err_out; michael@0: /* ci->time_param[i]=_time_P[ci->time_type[i]]->unpack(vi,opb); michael@0: Vorbis I has no time backend */ michael@0: /*if(!ci->time_param[i])goto err_out;*/ michael@0: } michael@0: michael@0: /* floor backend settings */ michael@0: ci->floors=oggpack_read(opb,6)+1; michael@0: if(ci->floors<=0)goto err_out; michael@0: for(i=0;ifloors;i++){ michael@0: ci->floor_type[i]=oggpack_read(opb,16); michael@0: if(ci->floor_type[i]<0 || ci->floor_type[i]>=VI_FLOORB)goto err_out; michael@0: ci->floor_param[i]=_floor_P[ci->floor_type[i]]->unpack(vi,opb); michael@0: if(!ci->floor_param[i])goto err_out; michael@0: } michael@0: michael@0: /* residue backend settings */ michael@0: ci->residues=oggpack_read(opb,6)+1; michael@0: if(ci->residues<=0)goto err_out; michael@0: for(i=0;iresidues;i++){ michael@0: ci->residue_type[i]=oggpack_read(opb,16); michael@0: if(ci->residue_type[i]<0 || ci->residue_type[i]>=VI_RESB)goto err_out; michael@0: ci->residue_param[i]=_residue_P[ci->residue_type[i]]->unpack(vi,opb); michael@0: if(!ci->residue_param[i])goto err_out; michael@0: } michael@0: michael@0: /* map backend settings */ michael@0: ci->maps=oggpack_read(opb,6)+1; michael@0: if(ci->maps<=0)goto err_out; michael@0: for(i=0;imaps;i++){ michael@0: ci->map_type[i]=oggpack_read(opb,16); michael@0: if(ci->map_type[i]<0 || ci->map_type[i]>=VI_MAPB)goto err_out; michael@0: ci->map_param[i]=_mapping_P[ci->map_type[i]]->unpack(vi,opb); michael@0: if(!ci->map_param[i])goto err_out; michael@0: } michael@0: michael@0: /* mode settings */ michael@0: ci->modes=oggpack_read(opb,6)+1; michael@0: if(ci->modes<=0)goto err_out; michael@0: for(i=0;imodes;i++){ michael@0: ci->mode_param[i]=(vorbis_info_mode *)_ogg_calloc(1,sizeof(*ci->mode_param[i])); michael@0: ci->mode_param[i]->blockflag=oggpack_read(opb,1); michael@0: ci->mode_param[i]->windowtype=oggpack_read(opb,16); michael@0: ci->mode_param[i]->transformtype=oggpack_read(opb,16); michael@0: ci->mode_param[i]->mapping=oggpack_read(opb,8); michael@0: michael@0: if(ci->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out; michael@0: if(ci->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out; michael@0: if(ci->mode_param[i]->mapping>=ci->maps)goto err_out; michael@0: if(ci->mode_param[i]->mapping<0)goto err_out; michael@0: } michael@0: michael@0: if(oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */ michael@0: michael@0: return(0); michael@0: err_out: michael@0: vorbis_info_clear(vi); michael@0: return(OV_EBADHEADER); michael@0: } michael@0: michael@0: /* Is this packet a vorbis ID header? */ michael@0: int vorbis_synthesis_idheader(ogg_packet *op){ michael@0: oggpack_buffer opb; michael@0: char buffer[6]; michael@0: michael@0: if(op){ michael@0: oggpack_readinit(&opb,op->packet,op->bytes); michael@0: michael@0: if(!op->b_o_s) michael@0: return(0); /* Not the initial packet */ michael@0: michael@0: if(oggpack_read(&opb,8) != 1) michael@0: return 0; /* not an ID header */ michael@0: michael@0: memset(buffer,0,6); michael@0: _v_readstring(&opb,buffer,6); michael@0: if(memcmp(buffer,"vorbis",6)) michael@0: return 0; /* not vorbis */ michael@0: michael@0: return 1; michael@0: } michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: /* The Vorbis header is in three packets; the initial small packet in michael@0: the first page that identifies basic parameters, a second packet michael@0: with bitstream comments and a third packet that holds the michael@0: codebook. */ michael@0: michael@0: int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){ michael@0: oggpack_buffer opb; michael@0: michael@0: if(op){ michael@0: oggpack_readinit(&opb,op->packet,op->bytes); michael@0: michael@0: /* Which of the three types of header is this? */ michael@0: /* Also verify header-ness, vorbis */ michael@0: { michael@0: char buffer[6]; michael@0: int packtype=oggpack_read(&opb,8); michael@0: memset(buffer,0,6); michael@0: _v_readstring(&opb,buffer,6); michael@0: if(memcmp(buffer,"vorbis",6)){ michael@0: /* not a vorbis header */ michael@0: return(OV_ENOTVORBIS); michael@0: } michael@0: switch(packtype){ michael@0: case 0x01: /* least significant *bit* is read first */ michael@0: if(!op->b_o_s){ michael@0: /* Not the initial packet */ michael@0: return(OV_EBADHEADER); michael@0: } michael@0: if(vi->rate!=0){ michael@0: /* previously initialized info header */ michael@0: return(OV_EBADHEADER); michael@0: } michael@0: michael@0: return(_vorbis_unpack_info(vi,&opb)); michael@0: michael@0: case 0x03: /* least significant *bit* is read first */ michael@0: if(vi->rate==0){ michael@0: /* um... we didn't get the initial header */ michael@0: return(OV_EBADHEADER); michael@0: } michael@0: michael@0: return(_vorbis_unpack_comment(vc,&opb)); michael@0: michael@0: case 0x05: /* least significant *bit* is read first */ michael@0: if(vi->rate==0 || vc->vendor==NULL){ michael@0: /* um... we didn;t get the initial header or comments yet */ michael@0: return(OV_EBADHEADER); michael@0: } michael@0: michael@0: return(_vorbis_unpack_books(vi,&opb)); michael@0: michael@0: default: michael@0: /* Not a valid vorbis header type */ michael@0: return(OV_EBADHEADER); michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: return(OV_EBADHEADER); michael@0: } michael@0: