📄 vorbisfile.java.new
字号:
while(true){ // process a packet if we can. If the machine isn't loaded, // neither is a page if(decode_ready){ Packet op=new Packet(); int result=os.packetout(op); long granulepos; // if(result==-1)return(-1); // hole in the data. For now, swallow // and go. We'll need to add a real // error code in a bit. if(result>0){ // got a packet. process it granulepos=op.granulepos; if(vb.synthesis(op)==0){ // lazy check for lazy // header handling. The // header packets aren't // audio, so if/when we // submit them, // vorbis_synthesis will // reject them // suck in the synthesis data and track bitrate { int oldsamples=vd.synthesis_pcmout(null, null); vd.synthesis_blockin(vb); samptrack+=vd.synthesis_pcmout(null, null)-oldsamples; bittrack+=op.bytes*8; } // update the pcm offset. if(granulepos!=-1 && op.e_o_s==0){ int link=(seekable?current_link:0); int samples; // this packet has a pcm_offset on it (the last packet // completed on a page carries the offset) After processing // (above), we know the pcm position of the *last* sample // ready to be returned. Find the offset of the *first* // // As an aside, this trick is inaccurate if we begin // reading anew right at the last page; the end-of-stream // granulepos declares the last frame in the stream, and the // last packet of the last page may be a partial frame. // So, we need a previous granulepos from an in-sequence page // to have a reference point. Thus the !op.e_o_s clause above samples=vd.synthesis_pcmout(null, null); granulepos-=samples; for(int i=0;i<link;i++){ granulepos+=pcmlengths[i]; } pcm_offset=granulepos; } return(1); } } } if(readp==0)return(0); if(get_next_page(og,-1)<0)return(0); // eof. leave unitialized // bitrate tracking; add the header's bytes here, the body bytes // are done by packet above bittrack+=og.header_len*8; // has our decoding just traversed a bitstream boundary? if(decode_ready){ if(current_serialno!=og.serialno()){ decode_clear(); } } // Do we need to load a new machine before submitting the page? // This is different in the seekable and non-seekable cases. // // In the seekable case, we already have all the header // information loaded and cached; we just initialize the machine // with it and continue on our merry way. // // In the non-seekable (streaming) case, we'll only be at a // boundary if we just left the previous logical bitstream and // we're now nominally at the header of the next bitstream if(!decode_ready){ int i; if(seekable){ current_serialno=og.serialno(); // match the serialno to bitstream section. We use this rather than // offset positions to avoid problems near logical bitstream // boundaries for(i=0;i<links;i++){ if(serialnos[i]==current_serialno)break; } if(i==links)return(-1); // sign of a bogus stream. error out, // leave machine uninitialized current_link=i; os.init(current_serialno); os.reset(); } else{ // we're streaming // fetch the three header packets, build the info struct int foo[]=new int[1]; fetch_headers(vi[0], vc[0], foo); current_serialno=foo[0]; current_link++; i=0; } make_decode_ready(); } os.pagein(og); } } //The helpers are over; it's all toplevel interface from here on out // clear out the OggVorbis_File struct int clear(){ vb.clear(); vd.clear(); os.clear(); if(vi!=null && links!=0){ for(int i=0;i<links;i++){ vi[i].clear(); vc[i].clear(); } vi=null; vc=null; } if(dataoffsets!=null)dataoffsets=null; if(pcmlengths!=null)pcmlengths=null; if(serialnos!=null)serialnos=null; if(offsets!=null)offsets=null; oy.clear(); //if(datasource!=null)(vf->callbacks.close_func)(vf->datasource); //memset(vf,0,sizeof(OggVorbis_File)); return(0); } static int fseek64_wrap(InputStream fis, //int64_t off, int off, int whence){ if(!fis.markSupported()){ return -1; } try{ try{if(whence==0){ fis.reset(); }} catch(Exception ee){System.out.println(ee);} fis.skip(off); } catch(Exception e){ System.out.println(e); //return -1; } return 0; } // inspects the OggVorbis file and finds/documents all the logical // bitstreams contained in it. Tries to be tolerant of logical // bitstream sections that are truncated/woogie. // // return: -1) error // 0) OK int open(InputStream is, byte[] initial, int ibytes){ return open_callbacks(is, initial, ibytes//, callbacks ); } int open_callbacks(InputStream is, byte[] initial, int ibytes//, callbacks callbacks ){// int offset=callbacks.seek_func(f,0,SEEK_CUR); int _offset=fseek64_wrap(is, (int)offset, SEEK_SET); int ret; // memset(vf,0,sizeof(OggVorbis_File)); datasource=is; //callbacks = _callbacks; // init the framing state oy.init(); // perhaps some data was previously read into a buffer for testing // against other stream types. Allow initialization from this // previously read data (as we may be reading from a non-seekable // stream) if(initial!=null){ int index=oy.buffer(ibytes); System.arraycopy(initial, 0, oy.data, index, ibytes); oy.wrote(ibytes); }System.out.println("open_callbacks="+_offset); // can we seek? Stevens suggests the seek test was portable if(_offset!=-1){ ret=open_seekable(); } else{ ret=open_nonseekable(); }System.out.println("ret="+ret); if(ret!=0){ datasource=null; clear(); } return(ret); } // How many logical bitstreams in this physical bitstream? public int streams(){ return links; } // Is the FILE * associated with vf seekable? public boolean seekable(){ return seekable; } // returns the bitrate for a given logical bitstream or the entire // physical bitstream. If the file is open for random access, it will // find the *actual* average bitrate. If the file is streaming, it // returns the nominal bitrate (if set) else the average of the // upper/lower bounds (if set) else -1 (unset). // // If you want the actual bitrate field settings, get them from the // vorbis_info structs public int bitrate(int i){ if(i>=links)return(-1); if(!seekable && i!=0)return(bitrate(0)); if(i<0){ long bits=0; for(int j=0;j<links;j++){ bits+=(offsets[j+1]-dataoffsets[j])*8; } return((int)Math.rint(bits/time_total(-1))); } else{ if(seekable){ // return the actual bitrate return((int)Math.rint((offsets[i+1]-dataoffsets[i])*8/time_total(i))); } else{ // return nominal if set if(vi[i].bitrate_nominal>0){ return vi[i].bitrate_nominal; } else{ if(vi[i].bitrate_upper>0){ if(vi[i].bitrate_lower>0){ return (vi[i].bitrate_upper+vi[i].bitrate_lower)/2; }else{ return vi[i].bitrate_upper; } } return(-1); } } } } // returns the actual bitrate since last call. returns -1 if no // additional data to offer since last call (or at beginning of stream) public int bitrate_instant(){ int _link=(seekable?current_link:0); if(samptrack==0)return(-1); int ret=(int)(bittrack/samptrack*vi[_link].rate+.5); bittrack=0.f; samptrack=0.f; return(ret); } public int serialnumber(int i){ if(i>=links)return(-1); if(!seekable && i>=0)return(serialnumber(-1)); if(i<0){ return(current_serialno); } else{ return(serialnos[i]); } } // returns: total raw (compressed) length of content if i==-1 // raw (compressed) length of that logical bitstream for i==0 to n // -1 if the stream is not seekable (we can't know the length) public long raw_total(int i){System.out.println("raw_total: "+seekable); if(!seekable || i>=links)return(-1); if(i<0){ long acc=0; // bug? for(int j=0;j<links;j++){ acc+=raw_total(j); } return(acc); } else{ return(offsets[i+1]-offsets[i]); } } // returns: total PCM length (samples) of content if i==-1 // PCM length (samples) of that logical bitstream for i==0 to n // -1 if the stream is not seekable (we can't know the length) public long pcm_total(int i){ if(!seekable || i>=links)return(-1); if(i<0){ long acc=0; for(int j=0;j<links;j++){ acc+=pcm_total(j); } return(acc); } else{ return(pcmlengths[i]); } } // returns: total seconds of content if i==-1 // seconds in that logical bitstream for i==0 to n // -1 if the stream is not seekable (we can't know the length) public float time_total(int i){ if(!seekable || i>=links)return(-1); if(i<0){ float acc=0; for(int j=0;j<links;j++){ acc+=time_total(j); } return(acc); } else{ return((float)(pcmlengths[i])/vi[i].rate); } } // seek to an offset relative to the *compressed* data. This also // immediately sucks in and decodes pages to update the PCM cursor. It // will cross a logical bitstream boundary, but only if it can't get // any packets out of the tail of the bitstream we seek to (so no // surprises). // // returns zero on success, nonzero on failure public int raw_seek(int pos){System.out.println("raw_seek: "+pos); if(!seekable)return(-1); // don't dump machine if we can't seek if(pos<0 || pos>offsets[links]){ //goto seek_error; pcm_offset=-1; decode_clear(); return -1; }System.out.println("#1"); // clear out decoding machine state pcm_offset=-1;System.out.println("#2"); decode_clear();System.out.println("#3"); // seek seek_helper(pos); // we need to make sure the pcm_offset is set. We use the // _fetch_packet helper to process one packet with readp set, then // call it until it returns '0' with readp not set (the last packet // from a page has the 'granulepos' field set, and that's how the // helper updates the offsetSystem.out.println("#4"); switch(process_packet(1)){ case 0:System.out.println("?0"); // oh, eof. There are no packets remaining. Set the pcm offset to // the end of file pcm_offset=pcm_total(-1); return(0); case -1:System.out.println("?-1"); // error! missing data or invalid bitstream structure //goto seek_error; pcm_offset=-1; decode_clear(); return -1; default:System.out.println("?break"); // all OK break; }System.out.println("pcm_offset="+pcm_offset); while(true){ switch(process_packet(0)){ case 0: // the offset is set. If it's a bogus bitstream with no offset // information, it's not but that's not our fault. We still run // gracefully, we're just missing the offset return(0); case -1: // error! missing data or invalid bitstream structure //goto seek_error; pcm_offset=-1; decode_clear(); return -1; default: // continue processing packets break; } } // seek_error: // dump the machine so we're in a known state //pcm_offset=-1; //decode_clear(); //return -1; } // seek to a sample offset relative to the decompressed pcm stream
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -