nuclear@0: /******************************************************************** nuclear@0: * * nuclear@0: * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * nuclear@0: * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * nuclear@0: * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * nuclear@0: * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * nuclear@0: * * nuclear@0: * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2010 * nuclear@0: * by the Xiph.Org Foundation http://www.xiph.org/ * nuclear@0: * * nuclear@0: ******************************************************************** nuclear@0: nuclear@0: function: maintain the info structure, info <-> header packets nuclear@0: last mod: $Id: info.c 18186 2012-02-03 22:08:44Z xiphmont $ nuclear@0: nuclear@0: ********************************************************************/ nuclear@0: nuclear@0: /* general handling of the header and the vorbis_info structure (and nuclear@0: substructures) */ nuclear@0: nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include "vorbis/codec.h" nuclear@0: #include "codec_internal.h" nuclear@0: #include "codebook.h" nuclear@0: #include "registry.h" nuclear@0: #include "window.h" nuclear@0: #include "psy.h" nuclear@0: #include "misc.h" nuclear@0: #include "os.h" nuclear@0: nuclear@0: #define GENERAL_VENDOR_STRING "Xiph.Org libVorbis 1.3.3" nuclear@0: #define ENCODE_VENDOR_STRING "Xiph.Org libVorbis I 20120203 (Omnipresent)" nuclear@0: nuclear@0: /* helpers */ nuclear@0: static int ilog2(unsigned int v){ nuclear@0: int ret=0; nuclear@0: if(v)--v; nuclear@0: while(v){ nuclear@0: ret++; nuclear@0: v>>=1; nuclear@0: } nuclear@0: return(ret); nuclear@0: } nuclear@0: nuclear@0: static void _v_writestring(oggpack_buffer *o,const char *s, int bytes){ nuclear@0: nuclear@0: while(bytes--){ nuclear@0: oggpack_write(o,*s++,8); nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: static void _v_readstring(oggpack_buffer *o,char *buf,int bytes){ nuclear@0: while(bytes--){ nuclear@0: *buf++=oggpack_read(o,8); nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: void vorbis_comment_init(vorbis_comment *vc){ nuclear@0: memset(vc,0,sizeof(*vc)); nuclear@0: } nuclear@0: nuclear@0: void vorbis_comment_add(vorbis_comment *vc,const char *comment){ nuclear@0: vc->user_comments=_ogg_realloc(vc->user_comments, nuclear@0: (vc->comments+2)*sizeof(*vc->user_comments)); nuclear@0: vc->comment_lengths=_ogg_realloc(vc->comment_lengths, nuclear@0: (vc->comments+2)*sizeof(*vc->comment_lengths)); nuclear@0: vc->comment_lengths[vc->comments]=strlen(comment); nuclear@0: vc->user_comments[vc->comments]=_ogg_malloc(vc->comment_lengths[vc->comments]+1); nuclear@0: strcpy(vc->user_comments[vc->comments], comment); nuclear@0: vc->comments++; nuclear@0: vc->user_comments[vc->comments]=NULL; nuclear@0: } nuclear@0: nuclear@0: void vorbis_comment_add_tag(vorbis_comment *vc, const char *tag, const char *contents){ nuclear@0: char *comment=alloca(strlen(tag)+strlen(contents)+2); /* +2 for = and \0 */ nuclear@0: strcpy(comment, tag); nuclear@0: strcat(comment, "="); nuclear@0: strcat(comment, contents); nuclear@0: vorbis_comment_add(vc, comment); nuclear@0: } nuclear@0: nuclear@0: /* This is more or less the same as strncasecmp - but that doesn't exist nuclear@0: * everywhere, and this is a fairly trivial function, so we include it */ nuclear@0: static int tagcompare(const char *s1, const char *s2, int n){ nuclear@0: int c=0; nuclear@0: while(c < n){ nuclear@0: if(toupper(s1[c]) != toupper(s2[c])) nuclear@0: return !0; nuclear@0: c++; nuclear@0: } nuclear@0: return 0; nuclear@0: } nuclear@0: nuclear@0: char *vorbis_comment_query(vorbis_comment *vc, const char *tag, int count){ nuclear@0: long i; nuclear@0: int found = 0; nuclear@0: int taglen = strlen(tag)+1; /* +1 for the = we append */ nuclear@0: char *fulltag = alloca(taglen+ 1); nuclear@0: nuclear@0: strcpy(fulltag, tag); nuclear@0: strcat(fulltag, "="); nuclear@0: nuclear@0: for(i=0;icomments;i++){ nuclear@0: if(!tagcompare(vc->user_comments[i], fulltag, taglen)){ nuclear@0: if(count == found) nuclear@0: /* We return a pointer to the data, not a copy */ nuclear@0: return vc->user_comments[i] + taglen; nuclear@0: else nuclear@0: found++; nuclear@0: } nuclear@0: } nuclear@0: return NULL; /* didn't find anything */ nuclear@0: } nuclear@0: nuclear@0: int vorbis_comment_query_count(vorbis_comment *vc, const char *tag){ nuclear@0: int i,count=0; nuclear@0: int taglen = strlen(tag)+1; /* +1 for the = we append */ nuclear@0: char *fulltag = alloca(taglen+1); nuclear@0: strcpy(fulltag,tag); nuclear@0: strcat(fulltag, "="); nuclear@0: nuclear@0: for(i=0;icomments;i++){ nuclear@0: if(!tagcompare(vc->user_comments[i], fulltag, taglen)) nuclear@0: count++; nuclear@0: } nuclear@0: nuclear@0: return count; nuclear@0: } nuclear@0: nuclear@0: void vorbis_comment_clear(vorbis_comment *vc){ nuclear@0: if(vc){ nuclear@0: long i; nuclear@0: if(vc->user_comments){ nuclear@0: for(i=0;icomments;i++) nuclear@0: if(vc->user_comments[i])_ogg_free(vc->user_comments[i]); nuclear@0: _ogg_free(vc->user_comments); nuclear@0: } nuclear@0: if(vc->comment_lengths)_ogg_free(vc->comment_lengths); nuclear@0: if(vc->vendor)_ogg_free(vc->vendor); nuclear@0: memset(vc,0,sizeof(*vc)); nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: /* blocksize 0 is guaranteed to be short, 1 is guaranteed to be long. nuclear@0: They may be equal, but short will never ge greater than long */ nuclear@0: int vorbis_info_blocksize(vorbis_info *vi,int zo){ nuclear@0: codec_setup_info *ci = vi->codec_setup; nuclear@0: return ci ? ci->blocksizes[zo] : -1; nuclear@0: } nuclear@0: nuclear@0: /* used by synthesis, which has a full, alloced vi */ nuclear@0: void vorbis_info_init(vorbis_info *vi){ nuclear@0: memset(vi,0,sizeof(*vi)); nuclear@0: vi->codec_setup=_ogg_calloc(1,sizeof(codec_setup_info)); nuclear@0: } nuclear@0: nuclear@0: void vorbis_info_clear(vorbis_info *vi){ nuclear@0: codec_setup_info *ci=vi->codec_setup; nuclear@0: int i; nuclear@0: nuclear@0: if(ci){ nuclear@0: nuclear@0: for(i=0;imodes;i++) nuclear@0: if(ci->mode_param[i])_ogg_free(ci->mode_param[i]); nuclear@0: nuclear@0: for(i=0;imaps;i++) /* unpack does the range checking */ nuclear@0: if(ci->map_param[i]) /* this may be cleaning up an aborted nuclear@0: unpack, in which case the below type nuclear@0: cannot be trusted */ nuclear@0: _mapping_P[ci->map_type[i]]->free_info(ci->map_param[i]); nuclear@0: nuclear@0: for(i=0;ifloors;i++) /* unpack does the range checking */ nuclear@0: if(ci->floor_param[i]) /* this may be cleaning up an aborted nuclear@0: unpack, in which case the below type nuclear@0: cannot be trusted */ nuclear@0: _floor_P[ci->floor_type[i]]->free_info(ci->floor_param[i]); nuclear@0: nuclear@0: for(i=0;iresidues;i++) /* unpack does the range checking */ nuclear@0: if(ci->residue_param[i]) /* this may be cleaning up an aborted nuclear@0: unpack, in which case the below type nuclear@0: cannot be trusted */ nuclear@0: _residue_P[ci->residue_type[i]]->free_info(ci->residue_param[i]); nuclear@0: nuclear@0: for(i=0;ibooks;i++){ nuclear@0: if(ci->book_param[i]){ nuclear@0: /* knows if the book was not alloced */ nuclear@0: vorbis_staticbook_destroy(ci->book_param[i]); nuclear@0: } nuclear@0: if(ci->fullbooks) nuclear@0: vorbis_book_clear(ci->fullbooks+i); nuclear@0: } nuclear@0: if(ci->fullbooks) nuclear@0: _ogg_free(ci->fullbooks); nuclear@0: nuclear@0: for(i=0;ipsys;i++) nuclear@0: _vi_psy_free(ci->psy_param[i]); nuclear@0: nuclear@0: _ogg_free(ci); nuclear@0: } nuclear@0: nuclear@0: memset(vi,0,sizeof(*vi)); nuclear@0: } nuclear@0: nuclear@0: /* Header packing/unpacking ********************************************/ nuclear@0: nuclear@0: static int _vorbis_unpack_info(vorbis_info *vi,oggpack_buffer *opb){ nuclear@0: codec_setup_info *ci=vi->codec_setup; nuclear@0: if(!ci)return(OV_EFAULT); nuclear@0: nuclear@0: vi->version=oggpack_read(opb,32); nuclear@0: if(vi->version!=0)return(OV_EVERSION); nuclear@0: nuclear@0: vi->channels=oggpack_read(opb,8); nuclear@0: vi->rate=oggpack_read(opb,32); nuclear@0: nuclear@0: vi->bitrate_upper=oggpack_read(opb,32); nuclear@0: vi->bitrate_nominal=oggpack_read(opb,32); nuclear@0: vi->bitrate_lower=oggpack_read(opb,32); nuclear@0: nuclear@0: ci->blocksizes[0]=1<blocksizes[1]=1<rate<1)goto err_out; nuclear@0: if(vi->channels<1)goto err_out; nuclear@0: if(ci->blocksizes[0]<64)goto err_out; nuclear@0: if(ci->blocksizes[1]blocksizes[0])goto err_out; nuclear@0: if(ci->blocksizes[1]>8192)goto err_out; nuclear@0: nuclear@0: if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */ nuclear@0: nuclear@0: return(0); nuclear@0: err_out: nuclear@0: vorbis_info_clear(vi); nuclear@0: return(OV_EBADHEADER); nuclear@0: } nuclear@0: nuclear@0: static int _vorbis_unpack_comment(vorbis_comment *vc,oggpack_buffer *opb){ nuclear@0: int i; nuclear@0: int vendorlen=oggpack_read(opb,32); nuclear@0: if(vendorlen<0)goto err_out; nuclear@0: if(vendorlen>opb->storage-8)goto err_out; nuclear@0: vc->vendor=_ogg_calloc(vendorlen+1,1); nuclear@0: _v_readstring(opb,vc->vendor,vendorlen); nuclear@0: i=oggpack_read(opb,32); nuclear@0: if(i<0)goto err_out; nuclear@0: if(i>((opb->storage-oggpack_bytes(opb))>>2))goto err_out; nuclear@0: vc->comments=i; nuclear@0: vc->user_comments=_ogg_calloc(vc->comments+1,sizeof(*vc->user_comments)); nuclear@0: vc->comment_lengths=_ogg_calloc(vc->comments+1, sizeof(*vc->comment_lengths)); nuclear@0: nuclear@0: for(i=0;icomments;i++){ nuclear@0: int len=oggpack_read(opb,32); nuclear@0: if(len<0)goto err_out; nuclear@0: if(len>opb->storage-oggpack_bytes(opb))goto err_out; nuclear@0: vc->comment_lengths[i]=len; nuclear@0: vc->user_comments[i]=_ogg_calloc(len+1,1); nuclear@0: _v_readstring(opb,vc->user_comments[i],len); nuclear@0: } nuclear@0: if(oggpack_read(opb,1)!=1)goto err_out; /* EOP check */ nuclear@0: nuclear@0: return(0); nuclear@0: err_out: nuclear@0: vorbis_comment_clear(vc); nuclear@0: return(OV_EBADHEADER); nuclear@0: } nuclear@0: nuclear@0: /* all of the real encoding details are here. The modes, books, nuclear@0: everything */ nuclear@0: static int _vorbis_unpack_books(vorbis_info *vi,oggpack_buffer *opb){ nuclear@0: codec_setup_info *ci=vi->codec_setup; nuclear@0: int i; nuclear@0: if(!ci)return(OV_EFAULT); nuclear@0: nuclear@0: /* codebooks */ nuclear@0: ci->books=oggpack_read(opb,8)+1; nuclear@0: if(ci->books<=0)goto err_out; nuclear@0: for(i=0;ibooks;i++){ nuclear@0: ci->book_param[i]=vorbis_staticbook_unpack(opb); nuclear@0: if(!ci->book_param[i])goto err_out; nuclear@0: } nuclear@0: nuclear@0: /* time backend settings; hooks are unused */ nuclear@0: { nuclear@0: int times=oggpack_read(opb,6)+1; nuclear@0: if(times<=0)goto err_out; nuclear@0: for(i=0;i=VI_TIMEB)goto err_out; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: /* floor backend settings */ nuclear@0: ci->floors=oggpack_read(opb,6)+1; nuclear@0: if(ci->floors<=0)goto err_out; nuclear@0: for(i=0;ifloors;i++){ nuclear@0: ci->floor_type[i]=oggpack_read(opb,16); nuclear@0: if(ci->floor_type[i]<0 || ci->floor_type[i]>=VI_FLOORB)goto err_out; nuclear@0: ci->floor_param[i]=_floor_P[ci->floor_type[i]]->unpack(vi,opb); nuclear@0: if(!ci->floor_param[i])goto err_out; nuclear@0: } nuclear@0: nuclear@0: /* residue backend settings */ nuclear@0: ci->residues=oggpack_read(opb,6)+1; nuclear@0: if(ci->residues<=0)goto err_out; nuclear@0: for(i=0;iresidues;i++){ nuclear@0: ci->residue_type[i]=oggpack_read(opb,16); nuclear@0: if(ci->residue_type[i]<0 || ci->residue_type[i]>=VI_RESB)goto err_out; nuclear@0: ci->residue_param[i]=_residue_P[ci->residue_type[i]]->unpack(vi,opb); nuclear@0: if(!ci->residue_param[i])goto err_out; nuclear@0: } nuclear@0: nuclear@0: /* map backend settings */ nuclear@0: ci->maps=oggpack_read(opb,6)+1; nuclear@0: if(ci->maps<=0)goto err_out; nuclear@0: for(i=0;imaps;i++){ nuclear@0: ci->map_type[i]=oggpack_read(opb,16); nuclear@0: if(ci->map_type[i]<0 || ci->map_type[i]>=VI_MAPB)goto err_out; nuclear@0: ci->map_param[i]=_mapping_P[ci->map_type[i]]->unpack(vi,opb); nuclear@0: if(!ci->map_param[i])goto err_out; nuclear@0: } nuclear@0: nuclear@0: /* mode settings */ nuclear@0: ci->modes=oggpack_read(opb,6)+1; nuclear@0: if(ci->modes<=0)goto err_out; nuclear@0: for(i=0;imodes;i++){ nuclear@0: ci->mode_param[i]=_ogg_calloc(1,sizeof(*ci->mode_param[i])); nuclear@0: ci->mode_param[i]->blockflag=oggpack_read(opb,1); nuclear@0: ci->mode_param[i]->windowtype=oggpack_read(opb,16); nuclear@0: ci->mode_param[i]->transformtype=oggpack_read(opb,16); nuclear@0: ci->mode_param[i]->mapping=oggpack_read(opb,8); nuclear@0: nuclear@0: if(ci->mode_param[i]->windowtype>=VI_WINDOWB)goto err_out; nuclear@0: if(ci->mode_param[i]->transformtype>=VI_WINDOWB)goto err_out; nuclear@0: if(ci->mode_param[i]->mapping>=ci->maps)goto err_out; nuclear@0: if(ci->mode_param[i]->mapping<0)goto err_out; nuclear@0: } nuclear@0: nuclear@0: if(oggpack_read(opb,1)!=1)goto err_out; /* top level EOP check */ nuclear@0: nuclear@0: return(0); nuclear@0: err_out: nuclear@0: vorbis_info_clear(vi); nuclear@0: return(OV_EBADHEADER); nuclear@0: } nuclear@0: nuclear@0: /* Is this packet a vorbis ID header? */ nuclear@0: int vorbis_synthesis_idheader(ogg_packet *op){ nuclear@0: oggpack_buffer opb; nuclear@0: char buffer[6]; nuclear@0: nuclear@0: if(op){ nuclear@0: oggpack_readinit(&opb,op->packet,op->bytes); nuclear@0: nuclear@0: if(!op->b_o_s) nuclear@0: return(0); /* Not the initial packet */ nuclear@0: nuclear@0: if(oggpack_read(&opb,8) != 1) nuclear@0: return 0; /* not an ID header */ nuclear@0: nuclear@0: memset(buffer,0,6); nuclear@0: _v_readstring(&opb,buffer,6); nuclear@0: if(memcmp(buffer,"vorbis",6)) nuclear@0: return 0; /* not vorbis */ nuclear@0: nuclear@0: return 1; nuclear@0: } nuclear@0: nuclear@0: return 0; nuclear@0: } nuclear@0: nuclear@0: /* The Vorbis header is in three packets; the initial small packet in nuclear@0: the first page that identifies basic parameters, a second packet nuclear@0: with bitstream comments and a third packet that holds the nuclear@0: codebook. */ nuclear@0: nuclear@0: int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,ogg_packet *op){ nuclear@0: oggpack_buffer opb; nuclear@0: nuclear@0: if(op){ nuclear@0: oggpack_readinit(&opb,op->packet,op->bytes); nuclear@0: nuclear@0: /* Which of the three types of header is this? */ nuclear@0: /* Also verify header-ness, vorbis */ nuclear@0: { nuclear@0: char buffer[6]; nuclear@0: int packtype=oggpack_read(&opb,8); nuclear@0: memset(buffer,0,6); nuclear@0: _v_readstring(&opb,buffer,6); nuclear@0: if(memcmp(buffer,"vorbis",6)){ nuclear@0: /* not a vorbis header */ nuclear@0: return(OV_ENOTVORBIS); nuclear@0: } nuclear@0: switch(packtype){ nuclear@0: case 0x01: /* least significant *bit* is read first */ nuclear@0: if(!op->b_o_s){ nuclear@0: /* Not the initial packet */ nuclear@0: return(OV_EBADHEADER); nuclear@0: } nuclear@0: if(vi->rate!=0){ nuclear@0: /* previously initialized info header */ nuclear@0: return(OV_EBADHEADER); nuclear@0: } nuclear@0: nuclear@0: return(_vorbis_unpack_info(vi,&opb)); nuclear@0: nuclear@0: case 0x03: /* least significant *bit* is read first */ nuclear@0: if(vi->rate==0){ nuclear@0: /* um... we didn't get the initial header */ nuclear@0: return(OV_EBADHEADER); nuclear@0: } nuclear@0: nuclear@0: return(_vorbis_unpack_comment(vc,&opb)); nuclear@0: nuclear@0: case 0x05: /* least significant *bit* is read first */ nuclear@0: if(vi->rate==0 || vc->vendor==NULL){ nuclear@0: /* um... we didn;t get the initial header or comments yet */ nuclear@0: return(OV_EBADHEADER); nuclear@0: } nuclear@0: nuclear@0: return(_vorbis_unpack_books(vi,&opb)); nuclear@0: nuclear@0: default: nuclear@0: /* Not a valid vorbis header type */ nuclear@0: return(OV_EBADHEADER); nuclear@0: break; nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: return(OV_EBADHEADER); nuclear@0: } nuclear@0: nuclear@0: /* pack side **********************************************************/ nuclear@0: nuclear@0: static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){ nuclear@0: codec_setup_info *ci=vi->codec_setup; nuclear@0: if(!ci)return(OV_EFAULT); nuclear@0: nuclear@0: /* preamble */ nuclear@0: oggpack_write(opb,0x01,8); nuclear@0: _v_writestring(opb,"vorbis", 6); nuclear@0: nuclear@0: /* basic information about the stream */ nuclear@0: oggpack_write(opb,0x00,32); nuclear@0: oggpack_write(opb,vi->channels,8); nuclear@0: oggpack_write(opb,vi->rate,32); nuclear@0: nuclear@0: oggpack_write(opb,vi->bitrate_upper,32); nuclear@0: oggpack_write(opb,vi->bitrate_nominal,32); nuclear@0: oggpack_write(opb,vi->bitrate_lower,32); nuclear@0: nuclear@0: oggpack_write(opb,ilog2(ci->blocksizes[0]),4); nuclear@0: oggpack_write(opb,ilog2(ci->blocksizes[1]),4); nuclear@0: oggpack_write(opb,1,1); nuclear@0: nuclear@0: return(0); nuclear@0: } nuclear@0: nuclear@0: static int _vorbis_pack_comment(oggpack_buffer *opb,vorbis_comment *vc){ nuclear@0: int bytes = strlen(ENCODE_VENDOR_STRING); nuclear@0: nuclear@0: /* preamble */ nuclear@0: oggpack_write(opb,0x03,8); nuclear@0: _v_writestring(opb,"vorbis", 6); nuclear@0: nuclear@0: /* vendor */ nuclear@0: oggpack_write(opb,bytes,32); nuclear@0: _v_writestring(opb,ENCODE_VENDOR_STRING, bytes); nuclear@0: nuclear@0: /* comments */ nuclear@0: nuclear@0: oggpack_write(opb,vc->comments,32); nuclear@0: if(vc->comments){ nuclear@0: int i; nuclear@0: for(i=0;icomments;i++){ nuclear@0: if(vc->user_comments[i]){ nuclear@0: oggpack_write(opb,vc->comment_lengths[i],32); nuclear@0: _v_writestring(opb,vc->user_comments[i], vc->comment_lengths[i]); nuclear@0: }else{ nuclear@0: oggpack_write(opb,0,32); nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: oggpack_write(opb,1,1); nuclear@0: nuclear@0: return(0); nuclear@0: } nuclear@0: nuclear@0: static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){ nuclear@0: codec_setup_info *ci=vi->codec_setup; nuclear@0: int i; nuclear@0: if(!ci)return(OV_EFAULT); nuclear@0: nuclear@0: oggpack_write(opb,0x05,8); nuclear@0: _v_writestring(opb,"vorbis", 6); nuclear@0: nuclear@0: /* books */ nuclear@0: oggpack_write(opb,ci->books-1,8); nuclear@0: for(i=0;ibooks;i++) nuclear@0: if(vorbis_staticbook_pack(ci->book_param[i],opb))goto err_out; nuclear@0: nuclear@0: /* times; hook placeholders */ nuclear@0: oggpack_write(opb,0,6); nuclear@0: oggpack_write(opb,0,16); nuclear@0: nuclear@0: /* floors */ nuclear@0: oggpack_write(opb,ci->floors-1,6); nuclear@0: for(i=0;ifloors;i++){ nuclear@0: oggpack_write(opb,ci->floor_type[i],16); nuclear@0: if(_floor_P[ci->floor_type[i]]->pack) nuclear@0: _floor_P[ci->floor_type[i]]->pack(ci->floor_param[i],opb); nuclear@0: else nuclear@0: goto err_out; nuclear@0: } nuclear@0: nuclear@0: /* residues */ nuclear@0: oggpack_write(opb,ci->residues-1,6); nuclear@0: for(i=0;iresidues;i++){ nuclear@0: oggpack_write(opb,ci->residue_type[i],16); nuclear@0: _residue_P[ci->residue_type[i]]->pack(ci->residue_param[i],opb); nuclear@0: } nuclear@0: nuclear@0: /* maps */ nuclear@0: oggpack_write(opb,ci->maps-1,6); nuclear@0: for(i=0;imaps;i++){ nuclear@0: oggpack_write(opb,ci->map_type[i],16); nuclear@0: _mapping_P[ci->map_type[i]]->pack(vi,ci->map_param[i],opb); nuclear@0: } nuclear@0: nuclear@0: /* modes */ nuclear@0: oggpack_write(opb,ci->modes-1,6); nuclear@0: for(i=0;imodes;i++){ nuclear@0: oggpack_write(opb,ci->mode_param[i]->blockflag,1); nuclear@0: oggpack_write(opb,ci->mode_param[i]->windowtype,16); nuclear@0: oggpack_write(opb,ci->mode_param[i]->transformtype,16); nuclear@0: oggpack_write(opb,ci->mode_param[i]->mapping,8); nuclear@0: } nuclear@0: oggpack_write(opb,1,1); nuclear@0: nuclear@0: return(0); nuclear@0: err_out: nuclear@0: return(-1); nuclear@0: } nuclear@0: nuclear@0: int vorbis_commentheader_out(vorbis_comment *vc, nuclear@0: ogg_packet *op){ nuclear@0: nuclear@0: oggpack_buffer opb; nuclear@0: nuclear@0: oggpack_writeinit(&opb); nuclear@0: if(_vorbis_pack_comment(&opb,vc)){ nuclear@0: oggpack_writeclear(&opb); nuclear@0: return OV_EIMPL; nuclear@0: } nuclear@0: nuclear@0: op->packet = _ogg_malloc(oggpack_bytes(&opb)); nuclear@0: memcpy(op->packet, opb.buffer, oggpack_bytes(&opb)); nuclear@0: nuclear@0: op->bytes=oggpack_bytes(&opb); nuclear@0: op->b_o_s=0; nuclear@0: op->e_o_s=0; nuclear@0: op->granulepos=0; nuclear@0: op->packetno=1; nuclear@0: nuclear@0: oggpack_writeclear(&opb); nuclear@0: return 0; nuclear@0: } nuclear@0: nuclear@0: int vorbis_analysis_headerout(vorbis_dsp_state *v, nuclear@0: vorbis_comment *vc, nuclear@0: ogg_packet *op, nuclear@0: ogg_packet *op_comm, nuclear@0: ogg_packet *op_code){ nuclear@0: int ret=OV_EIMPL; nuclear@0: vorbis_info *vi=v->vi; nuclear@0: oggpack_buffer opb; nuclear@0: private_state *b=v->backend_state; nuclear@0: nuclear@0: if(!b){ nuclear@0: ret=OV_EFAULT; nuclear@0: goto err_out; nuclear@0: } nuclear@0: nuclear@0: /* first header packet **********************************************/ nuclear@0: nuclear@0: oggpack_writeinit(&opb); nuclear@0: if(_vorbis_pack_info(&opb,vi))goto err_out; nuclear@0: nuclear@0: /* build the packet */ nuclear@0: if(b->header)_ogg_free(b->header); nuclear@0: b->header=_ogg_malloc(oggpack_bytes(&opb)); nuclear@0: memcpy(b->header,opb.buffer,oggpack_bytes(&opb)); nuclear@0: op->packet=b->header; nuclear@0: op->bytes=oggpack_bytes(&opb); nuclear@0: op->b_o_s=1; nuclear@0: op->e_o_s=0; nuclear@0: op->granulepos=0; nuclear@0: op->packetno=0; nuclear@0: nuclear@0: /* second header packet (comments) **********************************/ nuclear@0: nuclear@0: oggpack_reset(&opb); nuclear@0: if(_vorbis_pack_comment(&opb,vc))goto err_out; nuclear@0: nuclear@0: if(b->header1)_ogg_free(b->header1); nuclear@0: b->header1=_ogg_malloc(oggpack_bytes(&opb)); nuclear@0: memcpy(b->header1,opb.buffer,oggpack_bytes(&opb)); nuclear@0: op_comm->packet=b->header1; nuclear@0: op_comm->bytes=oggpack_bytes(&opb); nuclear@0: op_comm->b_o_s=0; nuclear@0: op_comm->e_o_s=0; nuclear@0: op_comm->granulepos=0; nuclear@0: op_comm->packetno=1; nuclear@0: nuclear@0: /* third header packet (modes/codebooks) ****************************/ nuclear@0: nuclear@0: oggpack_reset(&opb); nuclear@0: if(_vorbis_pack_books(&opb,vi))goto err_out; nuclear@0: nuclear@0: if(b->header2)_ogg_free(b->header2); nuclear@0: b->header2=_ogg_malloc(oggpack_bytes(&opb)); nuclear@0: memcpy(b->header2,opb.buffer,oggpack_bytes(&opb)); nuclear@0: op_code->packet=b->header2; nuclear@0: op_code->bytes=oggpack_bytes(&opb); nuclear@0: op_code->b_o_s=0; nuclear@0: op_code->e_o_s=0; nuclear@0: op_code->granulepos=0; nuclear@0: op_code->packetno=2; nuclear@0: nuclear@0: oggpack_writeclear(&opb); nuclear@0: return(0); nuclear@0: err_out: nuclear@0: memset(op,0,sizeof(*op)); nuclear@0: memset(op_comm,0,sizeof(*op_comm)); nuclear@0: memset(op_code,0,sizeof(*op_code)); nuclear@0: nuclear@0: if(b){ nuclear@0: oggpack_writeclear(&opb); nuclear@0: if(b->header)_ogg_free(b->header); nuclear@0: if(b->header1)_ogg_free(b->header1); nuclear@0: if(b->header2)_ogg_free(b->header2); nuclear@0: b->header=NULL; nuclear@0: b->header1=NULL; nuclear@0: b->header2=NULL; nuclear@0: } nuclear@0: return(ret); nuclear@0: } nuclear@0: nuclear@0: double vorbis_granule_time(vorbis_dsp_state *v,ogg_int64_t granulepos){ nuclear@0: if(granulepos == -1) return -1; nuclear@0: nuclear@0: /* We're not guaranteed a 64 bit unsigned type everywhere, so we nuclear@0: have to put the unsigned granpo in a signed type. */ nuclear@0: if(granulepos>=0){ nuclear@0: return((double)granulepos/v->vi->rate); nuclear@0: }else{ nuclear@0: ogg_int64_t granuleoff=0xffffffff; nuclear@0: granuleoff<<=31; nuclear@0: granuleoff|=0x7ffffffff; nuclear@0: return(((double)granulepos+2+granuleoff+granuleoff)/v->vi->rate); nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: const char *vorbis_version_string(void){ nuclear@0: return GENERAL_VENDOR_STRING; nuclear@0: }