📄 framing.c
字号:
/* continued packet flag? */ os->header[5]=0x00; if((os->lacing_vals[0]&0x100)==0) os->header[5]|=0x01; /* first page flag? */ if(os->b_o_s==0)os->header[5]|=0x02; /* last page flag? */ if(os->e_o_s && os->lacing_fill==vals)os->header[5]|=0x04; os->b_o_s=1; /* 64 bits of PCM position */ for(i=6;i<14;i++){ os->header[i]=(unsigned char)(granule_pos&0xff); // granule_pos>>=8; granule_pos = granule_pos>>8; } /* 32 bits of stream serial number */ { long serialno=os->serialno; for(i=14;i<18;i++){ os->header[i]=(unsigned char)(serialno&0xff); serialno>>=8; } } /* 32 bits of page counter (we have both counter and page header because this val can roll over) */ if(os->pageno==-1)os->pageno=0; /* because someone called stream_reset; this would be a strange thing to do in an encode stream, but it has plausible uses */ { long pageno=os->pageno++; for(i=18;i<22;i++){ os->header[i]=(unsigned char)(pageno&0xff); pageno>>=8; } } /* zero for computation; filled in later */ os->header[22]=0; os->header[23]=0; os->header[24]=0; os->header[25]=0; /* segment table */ os->header[26]=(unsigned char)(vals&0xff); for(i=0;i<vals;i++) bytes+=os->header[i+27]=(unsigned char)(os->lacing_vals[i]&0xff); /* set pointers in the ogg_page struct */ og->header=os->header; og->header_len=os->header_fill=vals+27; og->body=os->body_data+os->body_returned; //添加地址偏移量 og->body_len=bytes; /* advance the lacing data and set the body_returned pointer */ os->lacing_fill-=vals; memmove(os->lacing_vals,os->lacing_vals+vals,os->lacing_fill*sizeof(*os->lacing_vals)); memmove(os->granule_vals,os->granule_vals+vals,os->lacing_fill*sizeof(*os->granule_vals)); os->body_returned+=bytes; /* calculate the checksum */ ogg_page_checksum_set(og);//对head[22-26]中数据值作校验,之后输出 /* done */ return(1);}/* This constructs pages from buffered packet segments. The pointersreturned are to static buffers; do not free. The returned buffers aregood only until the next call (using the same ogg_stream_state) */int ogg_stream_pageout(ogg_stream_state *os, ogg_page *og){ if((os->e_o_s&&os->lacing_fill) || /* 'were done, now flush' case */ os->body_fill-os->body_returned > 4096 ||/* 'page nominal size' case */ os->lacing_fill>=255 || /* 'segment table full' case */ (os->lacing_fill&&!os->b_o_s)){ /* 'initial header page' case */ return(ogg_stream_flush(os,og)); } /* not enough data to construct a page and not end of stream */ return(0);}int ogg_stream_eos(ogg_stream_state *os){ return os->e_o_s;}/* DECODING PRIMITIVES: packet streaming layer **********************//* This has two layers to place more of the multi-serialno and paging control in the application's hands. First, we expose a data buffer using ogg_sync_buffer(). The app either copies into the buffer, or passes it directly to read(), etc. We then call ogg_sync_wrote() to tell how many bytes we just added. Pages are returned (pointers into the buffer in ogg_sync_state) by ogg_sync_pageout(). The page is then submitted to ogg_stream_pagein() along with the appropriate ogg_stream_state* (ie, matching serialno). We then get raw packets out calling ogg_stream_packetout() with a ogg_stream_state. *//* initialize the struct to a known state */int ogg_sync_init(ogg_sync_state *oy){ if(oy){ memset(oy,0,sizeof(*oy)); } return(0);}/* clear non-flat storage within */int ogg_sync_clear(ogg_sync_state *oy){ if(oy){ if(oy->data)_ogg_free(oy->data); ogg_sync_init(oy); } return(0);}int ogg_sync_destroy(ogg_sync_state *oy){ if(oy){ ogg_sync_clear(oy); _ogg_free(oy); } return(0);}char *ogg_sync_buffer(ogg_sync_state *oy, long size){ /* first, clear out any space that has been previously returned */ if(oy->returned){//清除缓存区空间内值 oy->fill-=oy->returned; if(oy->fill>0) memmove(oy->data,oy->data+oy->returned,oy->fill); oy->returned=0; } if(size>oy->storage-oy->fill){ /* We need to extend the internal buffer */ long newsize=size+oy->fill+4096; /* an extra page to be nice */ //扩展内部缓存 if(oy->data) oy->data=_ogg_realloc(oy->data,newsize); else oy->data=_ogg_malloc(newsize); oy->storage=newsize; } /* expose a segment at least as large as requested at the fill mark */ return((char *)oy->data+oy->fill);}int ogg_sync_wrote(ogg_sync_state *oy, long bytes){ if(oy->fill+bytes>oy->storage)return(-1); oy->fill+=bytes; return(0);}/* sync the stream. This is meant to be useful for finding page boundaries. return values for this: -n) skipped n bytes 0) page not ready; more data (no bytes skipped) n) page synced at current location; page length n bytes */long ogg_sync_pageseek(ogg_sync_state *oy,ogg_page *og){ unsigned char *page=oy->data+oy->returned; unsigned char *next; long bytes=oy->fill-oy->returned; if(oy->headerbytes==0){ int headerbytes,i; if(bytes<27)return(0); /* not enough for a header */ /* verify capture pattern */ if(memcmp(page,"OggS",4))goto sync_fail; headerbytes=page[26]+27; if(bytes<headerbytes)return(0); /* not enough for header + seg table */ /* count up body length in the segment table */ for(i=0;i<page[26];i++) oy->bodybytes+=page[27+i]; oy->headerbytes=headerbytes; } if(oy->bodybytes+oy->headerbytes>bytes)return(0); /* The whole test page is buffered. Verify the checksum */ { /* Grab the checksum bytes, set the header field to zero */ char chksum[4]; ogg_page log; memcpy(chksum,page+22,4); memset(page+22,0,4); /* set up a temp page struct and recompute the checksum */ log.header=page; log.header_len=oy->headerbytes; log.body=page+oy->headerbytes; log.body_len=oy->bodybytes; ogg_page_checksum_set(&log); /* Compare */ if(memcmp(chksum,page+22,4)){ /* D'oh. Mismatch! Corrupt page (or miscapture and not a page at all) */ /* replace the computed checksum with the one actually read in */ memcpy(page+22,chksum,4); /* Bad checksum. Lose sync */ goto sync_fail; } } /* yes, have a whole page all ready to go */ { unsigned char *page=oy->data+oy->returned; long bytes; if(og){ og->header=page; og->header_len=oy->headerbytes; og->body=page+oy->headerbytes; og->body_len=oy->bodybytes; } oy->unsynced=0; oy->returned+=(bytes=oy->headerbytes+oy->bodybytes); oy->headerbytes=0; oy->bodybytes=0; return(bytes); } sync_fail: oy->headerbytes=0; oy->bodybytes=0; /* search for possible capture */ next=memchr(page+1,'O',bytes-1); if(!next) next=oy->data+oy->fill; oy->returned=next-oy->data; return(-(next-page));}/* sync the stream and get a page. Keep trying until we find a page. Supress 'sync errors' after reporting the first. return values: -1) recapture (hole in data) 0) need more data 1) page returned Returns pointers into buffered data; invalidated by next call to _stream, _clear, _init, or _buffer */int ogg_sync_pageout(ogg_sync_state *oy, ogg_page *og){ /* all we need to do is verify a page at the head of the stream buffer. If it doesn't verify, we look for the next potential frame */ for(;;){ long ret=ogg_sync_pageseek(oy,og);//从oy中读取数据头和数据到og中去 if(ret>0){ /* have a page */ return(1); } if(ret==0){ /* need more data */ return(0); } /* head did not start a synced page... skipped some bytes */ if(!oy->unsynced){ oy->unsynced=1; return(-1); } /* loop. keep looking */ }}/* add the incoming page to the stream state; we decompose the page into packet segments here as well. */int ogg_stream_pagein(ogg_stream_state *os, ogg_page *og){ unsigned char *header=og->header; unsigned char *body=og->body; long bodysize=og->body_len; int segptr=0; int version=ogg_page_version(og); int continued=ogg_page_continued(og); int bos=ogg_page_bos(og); int eos=ogg_page_eos(og); ogg_int64_t granulepos=ogg_page_granulepos(og); int serialno=ogg_page_serialno(og); long pageno=ogg_page_pageno(og); int segments=header[26]; /* clean up 'returned data' */ { long lr=os->lacing_returned; long br=os->body_returned; /* body data */ if(br){ os->body_fill-=br; if(os->body_fill) memmove(os->body_data,os->body_data+br,os->body_fill); os->body_returned=0; } if(lr){ /* segment table */ if(os->lacing_fill-lr){ memmove(os->lacing_vals,os->lacing_vals+lr, (os->lacing_fill-lr)*sizeof(*os->lacing_vals)); memmove(os->granule_vals,os->granule_vals+lr, (os->lacing_fill-lr)*sizeof(*os->granule_vals)); } os->lacing_fill-=lr; os->lacing_packet-=lr; os->lacing_returned=0; } } /* check the serial number */ if(serialno!=os->serialno)return(-1); if(version>0)return(-1); _os_lacing_expand(os,segments+1); /* are we in sequence? */ if(pageno!=os->pageno){ int i; /* unroll previous partial packet (if any) */ for(i=os->lacing_packet;i<os->lacing_fill;i++) os->body_fill-=os->lacing_vals[i]&0xff; os->lacing_fill=os->lacing_packet; /* make a note of dropped data in segment table */ if(os->pageno!=-1){ os->lacing_vals[os->lacing_fill++]=0x400; os->lacing_packet++; } } /* are we a 'continued packet' page? If so, we may need to skip some segments */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -