michael@0: /******************************************************************** michael@0: * * michael@0: * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * 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 SOURCE CODE IS (C) COPYRIGHT 1994-2010 * 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: last mod: $Id: info.c 19058 2014-01-22 18:03:15Z xiphmont $ 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 "vorbis/codec.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 "psy.h" michael@0: #include "misc.h" michael@0: #include "os.h" michael@0: michael@0: #define GENERAL_VENDOR_STRING "Xiph.Org libVorbis 1.3.4" michael@0: #define ENCODE_VENDOR_STRING "Xiph.Org libVorbis I 20140122 (Turpakäräjiin)" michael@0: michael@0: /* helpers */ michael@0: static int ilog2(unsigned int v){ michael@0: int ret=0; michael@0: if(v)--v; michael@0: while(v){ michael@0: ret++; michael@0: v>>=1; michael@0: } michael@0: return(ret); michael@0: } michael@0: michael@0: static void _v_writestring(oggpack_buffer *o,const char *s, int bytes){ michael@0: michael@0: while(bytes--){ michael@0: oggpack_write(o,*s++,8); michael@0: } michael@0: } michael@0: 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: void vorbis_comment_add(vorbis_comment *vc,const char *comment){ michael@0: vc->user_comments=_ogg_realloc(vc->user_comments, michael@0: (vc->comments+2)*sizeof(*vc->user_comments)); michael@0: vc->comment_lengths=_ogg_realloc(vc->comment_lengths, michael@0: (vc->comments+2)*sizeof(*vc->comment_lengths)); michael@0: vc->comment_lengths[vc->comments]=strlen(comment); michael@0: vc->user_comments[vc->comments]=_ogg_malloc(vc->comment_lengths[vc->comments]+1); michael@0: strcpy(vc->user_comments[vc->comments], comment); michael@0: vc->comments++; michael@0: vc->user_comments[vc->comments]=NULL; michael@0: } michael@0: michael@0: void vorbis_comment_add_tag(vorbis_comment *vc, const char *tag, const char *contents){ michael@0: char *comment=alloca(strlen(tag)+strlen(contents)+2); /* +2 for = and \0 */ michael@0: strcpy(comment, tag); michael@0: strcat(comment, "="); michael@0: strcat(comment, contents); michael@0: vorbis_comment_add(vc, comment); 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, const 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 = 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, const char *tag){ michael@0: int i,count=0; michael@0: int taglen = strlen(tag)+1; /* +1 for the = we append */ michael@0: char *fulltag = 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 guaranteed 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 = 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=_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=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]) /* this may be cleaning up an aborted michael@0: unpack, in which case the below type michael@0: cannot be trusted */ 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]) /* this may be cleaning up an aborted michael@0: unpack, in which case the below type michael@0: cannot be trusted */ 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]) /* this may be cleaning up an aborted michael@0: unpack, in which case the below type michael@0: cannot be trusted */ 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: for(i=0;ipsys;i++) michael@0: _vi_psy_free(ci->psy_param[i]); 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=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=oggpack_read(opb,32); michael@0: if(vendorlen<0)goto err_out; michael@0: if(vendorlen>opb->storage-8)goto err_out; michael@0: vc->vendor=_ogg_calloc(vendorlen+1,1); michael@0: _v_readstring(opb,vc->vendor,vendorlen); michael@0: i=oggpack_read(opb,32); michael@0: if(i<0)goto err_out; michael@0: if(i>((opb->storage-oggpack_bytes(opb))>>2))goto err_out; michael@0: vc->comments=i; michael@0: vc->user_comments=_ogg_calloc(vc->comments+1,sizeof(*vc->user_comments)); michael@0: vc->comment_lengths=_ogg_calloc(vc->comments+1, sizeof(*vc->comment_lengths)); michael@0: michael@0: for(i=0;icomments;i++){ michael@0: int len=oggpack_read(opb,32); michael@0: if(len<0)goto err_out; michael@0: if(len>opb->storage-oggpack_bytes(opb))goto err_out; michael@0: vc->comment_lengths[i]=len; michael@0: vc->user_comments[i]=_ogg_calloc(len+1,1); 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=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; hooks are unused */ michael@0: { michael@0: int times=oggpack_read(opb,6)+1; michael@0: if(times<=0)goto err_out; michael@0: for(i=0;i=VI_TIMEB)goto err_out; michael@0: } 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]=_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: michael@0: /* pack side **********************************************************/ michael@0: michael@0: static int _vorbis_pack_info(oggpack_buffer *opb,vorbis_info *vi){ michael@0: codec_setup_info *ci=vi->codec_setup; michael@0: if(!ci)return(OV_EFAULT); michael@0: michael@0: /* preamble */ michael@0: oggpack_write(opb,0x01,8); michael@0: _v_writestring(opb,"vorbis", 6); michael@0: michael@0: /* basic information about the stream */ michael@0: oggpack_write(opb,0x00,32); michael@0: oggpack_write(opb,vi->channels,8); michael@0: oggpack_write(opb,vi->rate,32); michael@0: michael@0: oggpack_write(opb,vi->bitrate_upper,32); michael@0: oggpack_write(opb,vi->bitrate_nominal,32); michael@0: oggpack_write(opb,vi->bitrate_lower,32); michael@0: michael@0: oggpack_write(opb,ilog2(ci->blocksizes[0]),4); michael@0: oggpack_write(opb,ilog2(ci->blocksizes[1]),4); michael@0: oggpack_write(opb,1,1); michael@0: michael@0: return(0); michael@0: } michael@0: michael@0: static int _vorbis_pack_comment(oggpack_buffer *opb,vorbis_comment *vc){ michael@0: int bytes = strlen(ENCODE_VENDOR_STRING); michael@0: michael@0: /* preamble */ michael@0: oggpack_write(opb,0x03,8); michael@0: _v_writestring(opb,"vorbis", 6); michael@0: michael@0: /* vendor */ michael@0: oggpack_write(opb,bytes,32); michael@0: _v_writestring(opb,ENCODE_VENDOR_STRING, bytes); michael@0: michael@0: /* comments */ michael@0: michael@0: oggpack_write(opb,vc->comments,32); michael@0: if(vc->comments){ michael@0: int i; michael@0: for(i=0;icomments;i++){ michael@0: if(vc->user_comments[i]){ michael@0: oggpack_write(opb,vc->comment_lengths[i],32); michael@0: _v_writestring(opb,vc->user_comments[i], vc->comment_lengths[i]); michael@0: }else{ michael@0: oggpack_write(opb,0,32); michael@0: } michael@0: } michael@0: } michael@0: oggpack_write(opb,1,1); michael@0: michael@0: return(0); michael@0: } michael@0: michael@0: static int _vorbis_pack_books(oggpack_buffer *opb,vorbis_info *vi){ michael@0: codec_setup_info *ci=vi->codec_setup; michael@0: int i; michael@0: if(!ci)return(OV_EFAULT); michael@0: michael@0: oggpack_write(opb,0x05,8); michael@0: _v_writestring(opb,"vorbis", 6); michael@0: michael@0: /* books */ michael@0: oggpack_write(opb,ci->books-1,8); michael@0: for(i=0;ibooks;i++) michael@0: if(vorbis_staticbook_pack(ci->book_param[i],opb))goto err_out; michael@0: michael@0: /* times; hook placeholders */ michael@0: oggpack_write(opb,0,6); michael@0: oggpack_write(opb,0,16); michael@0: michael@0: /* floors */ michael@0: oggpack_write(opb,ci->floors-1,6); michael@0: for(i=0;ifloors;i++){ michael@0: oggpack_write(opb,ci->floor_type[i],16); michael@0: if(_floor_P[ci->floor_type[i]]->pack) michael@0: _floor_P[ci->floor_type[i]]->pack(ci->floor_param[i],opb); michael@0: else michael@0: goto err_out; michael@0: } michael@0: michael@0: /* residues */ michael@0: oggpack_write(opb,ci->residues-1,6); michael@0: for(i=0;iresidues;i++){ michael@0: oggpack_write(opb,ci->residue_type[i],16); michael@0: _residue_P[ci->residue_type[i]]->pack(ci->residue_param[i],opb); michael@0: } michael@0: michael@0: /* maps */ michael@0: oggpack_write(opb,ci->maps-1,6); michael@0: for(i=0;imaps;i++){ michael@0: oggpack_write(opb,ci->map_type[i],16); michael@0: _mapping_P[ci->map_type[i]]->pack(vi,ci->map_param[i],opb); michael@0: } michael@0: michael@0: /* modes */ michael@0: oggpack_write(opb,ci->modes-1,6); michael@0: for(i=0;imodes;i++){ michael@0: oggpack_write(opb,ci->mode_param[i]->blockflag,1); michael@0: oggpack_write(opb,ci->mode_param[i]->windowtype,16); michael@0: oggpack_write(opb,ci->mode_param[i]->transformtype,16); michael@0: oggpack_write(opb,ci->mode_param[i]->mapping,8); michael@0: } michael@0: oggpack_write(opb,1,1); michael@0: michael@0: return(0); michael@0: err_out: michael@0: return(-1); michael@0: } michael@0: michael@0: int vorbis_commentheader_out(vorbis_comment *vc, michael@0: ogg_packet *op){ michael@0: michael@0: oggpack_buffer opb; michael@0: michael@0: oggpack_writeinit(&opb); michael@0: if(_vorbis_pack_comment(&opb,vc)){ michael@0: oggpack_writeclear(&opb); michael@0: return OV_EIMPL; michael@0: } michael@0: michael@0: op->packet = _ogg_malloc(oggpack_bytes(&opb)); michael@0: memcpy(op->packet, opb.buffer, oggpack_bytes(&opb)); michael@0: michael@0: op->bytes=oggpack_bytes(&opb); michael@0: op->b_o_s=0; michael@0: op->e_o_s=0; michael@0: op->granulepos=0; michael@0: op->packetno=1; michael@0: michael@0: oggpack_writeclear(&opb); michael@0: return 0; michael@0: } michael@0: michael@0: int vorbis_analysis_headerout(vorbis_dsp_state *v, michael@0: vorbis_comment *vc, michael@0: ogg_packet *op, michael@0: ogg_packet *op_comm, michael@0: ogg_packet *op_code){ michael@0: int ret=OV_EIMPL; michael@0: vorbis_info *vi=v->vi; michael@0: oggpack_buffer opb; michael@0: private_state *b=v->backend_state; michael@0: michael@0: if(!b){ michael@0: ret=OV_EFAULT; michael@0: goto err_out; michael@0: } michael@0: michael@0: /* first header packet **********************************************/ michael@0: michael@0: oggpack_writeinit(&opb); michael@0: if(_vorbis_pack_info(&opb,vi))goto err_out; michael@0: michael@0: /* build the packet */ michael@0: if(b->header)_ogg_free(b->header); michael@0: b->header=_ogg_malloc(oggpack_bytes(&opb)); michael@0: memcpy(b->header,opb.buffer,oggpack_bytes(&opb)); michael@0: op->packet=b->header; michael@0: op->bytes=oggpack_bytes(&opb); michael@0: op->b_o_s=1; michael@0: op->e_o_s=0; michael@0: op->granulepos=0; michael@0: op->packetno=0; michael@0: michael@0: /* second header packet (comments) **********************************/ michael@0: michael@0: oggpack_reset(&opb); michael@0: if(_vorbis_pack_comment(&opb,vc))goto err_out; michael@0: michael@0: if(b->header1)_ogg_free(b->header1); michael@0: b->header1=_ogg_malloc(oggpack_bytes(&opb)); michael@0: memcpy(b->header1,opb.buffer,oggpack_bytes(&opb)); michael@0: op_comm->packet=b->header1; michael@0: op_comm->bytes=oggpack_bytes(&opb); michael@0: op_comm->b_o_s=0; michael@0: op_comm->e_o_s=0; michael@0: op_comm->granulepos=0; michael@0: op_comm->packetno=1; michael@0: michael@0: /* third header packet (modes/codebooks) ****************************/ michael@0: michael@0: oggpack_reset(&opb); michael@0: if(_vorbis_pack_books(&opb,vi))goto err_out; michael@0: michael@0: if(b->header2)_ogg_free(b->header2); michael@0: b->header2=_ogg_malloc(oggpack_bytes(&opb)); michael@0: memcpy(b->header2,opb.buffer,oggpack_bytes(&opb)); michael@0: op_code->packet=b->header2; michael@0: op_code->bytes=oggpack_bytes(&opb); michael@0: op_code->b_o_s=0; michael@0: op_code->e_o_s=0; michael@0: op_code->granulepos=0; michael@0: op_code->packetno=2; michael@0: michael@0: oggpack_writeclear(&opb); michael@0: return(0); michael@0: err_out: michael@0: memset(op,0,sizeof(*op)); michael@0: memset(op_comm,0,sizeof(*op_comm)); michael@0: memset(op_code,0,sizeof(*op_code)); michael@0: michael@0: if(b){ michael@0: oggpack_writeclear(&opb); michael@0: if(b->header)_ogg_free(b->header); michael@0: if(b->header1)_ogg_free(b->header1); michael@0: if(b->header2)_ogg_free(b->header2); michael@0: b->header=NULL; michael@0: b->header1=NULL; michael@0: b->header2=NULL; michael@0: } michael@0: return(ret); michael@0: } michael@0: michael@0: double vorbis_granule_time(vorbis_dsp_state *v,ogg_int64_t granulepos){ michael@0: if(granulepos == -1) return -1; michael@0: michael@0: /* We're not guaranteed a 64 bit unsigned type everywhere, so we michael@0: have to put the unsigned granpo in a signed type. */ michael@0: if(granulepos>=0){ michael@0: return((double)granulepos/v->vi->rate); michael@0: }else{ michael@0: ogg_int64_t granuleoff=0xffffffff; michael@0: granuleoff<<=31; michael@0: granuleoff|=0x7ffffffff; michael@0: return(((double)granulepos+2+granuleoff+granuleoff)/v->vi->rate); michael@0: } michael@0: } michael@0: michael@0: const char *vorbis_version_string(void){ michael@0: return GENERAL_VENDOR_STRING; michael@0: }