📄 framing.c
字号:
if(!oy->unsynced){
oy->unsynced=1;
return(-1);
}
}
}
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];
{
long lr=os->lacing_returned;
long br=os->body_returned;
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){
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;
}
}
if(serialno!=os->serialno)return(-1);
if(version>0)return(-1);
_os_lacing_expand(os,segments+1);
if(pageno!=os->pageno){
int i;
for(i=os->lacing_packet;i<os->lacing_fill;i++)
os->body_fill-=os->lacing_vals[i]&0xff;
os->lacing_fill=os->lacing_packet;
if(os->pageno!=-1){
os->lacing_vals[os->lacing_fill++]=0x400;
os->lacing_packet++;
}
if(continued){
bos=0;
for(;segptr<segments;segptr++){
int val=header[27+segptr];
body+=val;
bodysize-=val;
if(val<255){
segptr++;
break;
}
}
}
}
if(bodysize){
_os_body_expand(os,bodysize);
memcpy(os->body_data+os->body_fill,body,bodysize);
os->body_fill+=bodysize;
}
{
int saved=-1;
while(segptr<segments){
int val=header[27+segptr];
os->lacing_vals[os->lacing_fill]=val;
os->granule_vals[os->lacing_fill]=-1;
if(bos){
os->lacing_vals[os->lacing_fill]|=0x100;
bos=0;
}
if(val<255)saved=os->lacing_fill;
os->lacing_fill++;
segptr++;
if(val<255)os->lacing_packet=os->lacing_fill;
}
if(saved!=-1){
os->granule_vals[saved]=granulepos;
}
}
if(eos){
os->e_o_s=1;
if(os->lacing_fill>0)
os->lacing_vals[os->lacing_fill-1]|=0x200;
}
os->pageno=pageno+1;
return(0);
}
int ogg_sync_reset(ogg_sync_state *oy){
oy->fill=0;
oy->returned=0;
oy->unsynced=0;
oy->headerbytes=0;
oy->bodybytes=0;
return(0);
}
int ogg_stream_reset(ogg_stream_state *os){
os->body_fill=0;
os->body_returned=0;
os->lacing_fill=0;
os->lacing_packet=0;
os->lacing_returned=0;
os->header_fill=0;
os->e_o_s=0;
os->b_o_s=0;
os->pageno=-1;
os->packetno=0;
os->granulepos=0;
return(0);
}
int ogg_stream_reset_serialno(ogg_stream_state *os,int serialno){
ogg_stream_reset(os);
os->serialno=serialno;
return(0);
}
static int _packetout(ogg_stream_state *os,ogg_packet *op,int adv){
int ptr=os->lacing_returned;
if(os->lacing_packet<=ptr)return(0);
if(os->lacing_vals[ptr]&0x400){
os->lacing_returned++;
os->packetno++;
return(-1);
}
if(!op && !adv)return(1);
{
int size=os->lacing_vals[ptr]&0xff;
int bytes=size;
int eos=os->lacing_vals[ptr]&0x200;
int bos=os->lacing_vals[ptr]&0x100;
while(size==255){
int val=os->lacing_vals[++ptr];
size=val&0xff;
if(val&0x200)eos=0x200;
bytes+=size;
}
if(op){
op->e_o_s=eos;
op->b_o_s=bos;
op->packet=os->body_data+os->body_returned;
op->packetno=os->packetno;
op->granulepos=os->granule_vals[ptr];
op->bytes=bytes;
}
if(adv){
os->body_returned+=bytes;
os->lacing_returned=ptr+1;
os->packetno++;
}
}
return(1);
}
int ogg_stream_packetout(ogg_stream_state *os,ogg_packet *op){
return _packetout(os,op,1);
}
int ogg_stream_packetpeek(ogg_stream_state *os,ogg_packet *op){
return _packetout(os,op,0);
}
void ogg_packet_clear(ogg_packet *op) {
_ogg_free(op->packet);
memset(op, 0, sizeof(*op));
}
#ifdef _V_SELFTEST
#include <stdio.h>
ogg_stream_state os_en, os_de;
ogg_sync_state oy;
void checkpacket(ogg_packet *op,int len, int no, int pos){
long j;
static int sequence=0;
static int lastno=0;
if(op->bytes!=len){
fprintf(stderr,"incorrect packet length!\n");
exit(1);
}
if(op->granulepos!=pos){
fprintf(stderr,"incorrect packet position!\n");
exit(1);
}
if(no==0){
sequence=0;
}else{
sequence++;
if(no>lastno+1)
sequence++;
}
lastno=no;
if(op->packetno!=sequence){
fprintf(stderr,"incorrect packet sequence %ld != %d\n",
(long)(op->packetno),sequence);
exit(1);
}
for(j=0;j<op->bytes;j++)
if(op->packet[j]!=((j+no)&0xff)){
fprintf(stderr,"body data mismatch (1) at pos %ld: %x!=%lx!\n\n",
j,op->packet[j],(j+no)&0xff);
exit(1);
}
}
void check_page(unsigned char *data,const int *header,ogg_page *og){
long j;
for(j=0;j<og->body_len;j++)
if(og->body[j]!=data[j]){
fprintf(stderr,"body data mismatch (2) at pos %ld: %x!=%x!\n\n",
j,data[j],og->body[j]);
exit(1);
}
for(j=0;j<og->header_len;j++){
if(og->header[j]!=header[j]){
fprintf(stderr,"header content mismatch at pos %ld:\n",j);
for(j=0;j<header[26]+27;j++)
fprintf(stderr," (%ld)%02x:%02x",j,header[j],og->header[j]);
fprintf(stderr,"\n");
exit(1);
}
}
if(og->header_len!=header[26]+27){
fprintf(stderr,"header length incorrect! (%ld!=%d)\n",
og->header_len,header[26]+27);
exit(1);
}
}
void print_header(ogg_page *og){
int j;
fprintf(stderr,"\nHEADER:\n");
fprintf(stderr," capture: %c %c %c %c version: %d flags: %x\n",
og->header[0],og->header[1],og->header[2],og->header[3],
(int)og->header[4],(int)og->header[5]);
fprintf(stderr," granulepos: %d serialno: %d pageno: %ld\n",
(og->header[9]<<24)|(og->header[8]<<16)|
(og->header[7]<<8)|og->header[6],
(og->header[17]<<24)|(og->header[16]<<16)|
(og->header[15]<<8)|og->header[14],
((long)(og->header[21])<<24)|(og->header[20]<<16)|
(og->header[19]<<8)|og->header[18]);
fprintf(stderr," checksum: %02x:%02x:%02x:%02x\n segments: %d (",
(int)og->header[22],(int)og->header[23],
(int)og->header[24],(int)og->header[25],
(int)og->header[26]);
for(j=27;j<og->header_len;j++)
fprintf(stderr,"%d ",(int)og->header[j]);
fprintf(stderr,")\n\n");
}
void copy_page(ogg_page *og){
unsigned char *temp=_ogg_malloc(og->header_len);
memcpy(temp,og->header,og->header_len);
og->header=temp;
temp=_ogg_malloc(og->body_len);
memcpy(temp,og->body,og->body_len);
og->body=temp;
}
void error(void){
fprintf(stderr,"error!\n");
exit(1);
}
/* 17 only */
const int head1_0[] = {0x4f,0x67,0x67,0x53,0,0x06,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x02,0x03,0x04,0,0,0,0,
0x15,0xed,0xec,0x91,
1,
17};
/* 17, 254, 255, 256, 500, 510, 600 byte, pad */
const int head1_1[] = {0x4f,0x67,0x67,0x53,0,0x02,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x02,0x03,0x04,0,0,0,0,
0x59,0x10,0x6c,0x2c,
1,
17};
const int head2_1[] = {0x4f,0x67,0x67,0x53,0,0x04,
0x07,0x18,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x02,0x03,0x04,1,0,0,0,
0x89,0x33,0x85,0xce,
13,
254,255,0,255,1,255,245,255,255,0,
255,255,90};
const int head1_2[] = {0x4f,0x67,0x67,0x53,0,0x02,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x02,0x03,0x04,0,0,0,0,
0xff,0x7b,0x23,0x17,
1,
0};
const int head2_2[] = {0x4f,0x67,0x67,0x53,0,0x04,
0x07,0x28,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x02,0x03,0x04,1,0,0,0,
0x5c,0x3f,0x66,0xcb,
17,
17,254,255,0,0,255,1,0,255,245,255,255,0,
255,255,90,0};
const int head1_3[] = {0x4f,0x67,0x67,0x53,0,0x02,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x02,0x03,0x04,0,0,0,0,
0x01,0x27,0x31,0xaa,
18,
255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,10};
const int head2_3[] = {0x4f,0x67,0x67,0x53,0,0x04,
0x07,0x08,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x02,0x03,0x04,1,0,0,0,
0x7f,0x4e,0x8a,0xd2,
4,
255,4,255,0};
const int head1_4[] = {0x4f,0x67,0x67,0x53,0,0x02,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x02,0x03,0x04,0,0,0,0,
0xff,0x7b,0x23,0x17,
1,
0};
const int head2_4[] = {0x4f,0x67,0x67,0x53,0,0x00,
0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x02,0x03,0x04,1,0,0,0,
0x34,0x24,0xd5,0x29,
17,
255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255};
const int head3_4[] = {0x4f,0x67,0x67,0x53,0,0x05,
0x07,0x0c,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x02,0x03,0x04,2,0,0,0,
0xc8,0xc3,0xcb,0xed,
5,
10,255,4,255,0};
const int head1_5[] = {0x4f,0x67,0x67,0x53,0,0x02,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x02,0x03,0x04,0,0,0,0,
0xff,0x7b,0x23,0x17,
1,
0};
const int head2_5[] = {0x4f,0x67,0x67,0x53,0,0x00,
0x07,0xfc,0x03,0x00,0x00,0x00,0x00,0x00,
0x01,0x02,0x03,0x04,1,0,0,0,
0xed,0x2a,0x2e,0xa7,
255,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10};
const int head3_5[] = {0x4f,0x67,0x67,0x53,0,0x04,
0x07,0x00,0x04,0x00,0x00,0x00,0x00,0x00,
0x01,0x02,0x03,0x04,2,0,0,0,
0x6c,0x3b,0x82,0x3d,
1,
50};
const int head1_6[] = {0x4f,0x67,0x67,0x53,0,0x02,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x02,0x03,0x04,0,0,0,0,
0xff,0x7b,0x23,0x17,
1,
0};
const int head2_6[] = {0x4f,0x67,0x67,0x53,0,0x00,
0x07,0x04,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x02,0x03,0x04,1,0,0,0,
0x3c,0xd9,0x4d,0x3f,
17,
100,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255};
const int head3_6[] = {0x4f,0x67,0x67,0x53,0,0x01,
0x07,0x04,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x02,0x03,0x04,2,0,0,0,
0xbd,0xd5,0xb5,0x8b,
17,
255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255};
const int head4_6[] = {0x4f,0x67,0x67,0x53,0,0x05,
0x07,0x10,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x02,0x03,0x04,3,0,0,0,
0xef,0xdd,0x88,0xde,
7,
255,255,75,255,4,255,0};
const int head1_7[] = {0x4f,0x67,0x67,0x53,0,0x02,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x02,0x03,0x04,0,0,0,0,
0xff,0x7b,0x23,0x17,
1,
0};
const int head2_7[] = {0x4f,0x67,0x67,0x53,0,0x00,
0x07,0x04,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x02,0x03,0x04,1,0,0,0,
0x3c,0xd9,0x4d,0x3f,
17,
100,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255};
const int head3_7[] = {0x4f,0x67,0x67,0x53,0,0x05,
0x07,0x08,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x02,0x03,0x04,2,0,0,0,
0xd4,0xe0,0x60,0xe5,
1,0};
void test_pack(const int *pl, const int **headers){
unsigned char *data=_ogg_malloc(1024*1024);
long inptr=0;
long outptr=0;
long deptr=0;
long depacket=0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -