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