annotate stream.c @ 18:531d58007138

Initial revision
author arpi_esp
date Tue, 27 Feb 2001 02:04:08 +0000
parents 3b5f5d1c5041
children afa2c05cfe43
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
2 #include "vcd_read.c"
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
3
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
4 //=================== STREAMER =========================
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
5
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
6 #define STREAM_BUFFER_SIZE 4096
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
7
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
8 #define STREAMTYPE_FILE 0
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
9 #define STREAMTYPE_VCD 1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
10
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
11 typedef struct {
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
12 int fd;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
13 long pos;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
14 int eof;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
15 int type; // 0=file 1=VCD
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
16 unsigned int buf_pos,buf_len;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
17 unsigned char buffer[STREAM_BUFFER_SIZE];
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
18 } stream_t;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
19
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
20 int stream_fill_buffer(stream_t *s){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
21 int len;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
22 if(s->eof){ s->buf_pos=s->buf_len=0; return 0; }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
23 switch(s->type){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
24 case STREAMTYPE_FILE:
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
25 len=read(s->fd,s->buffer,STREAM_BUFFER_SIZE);break;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
26 case STREAMTYPE_VCD:
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
27 #ifdef VCD_CACHE
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
28 len=vcd_cache_read(s->fd,s->buffer);break;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
29 #else
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
30 len=vcd_read(s->fd,s->buffer);break;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
31 #endif
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
32 default: len=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
33 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
34 if(len<=0){ s->eof=1; s->buf_pos=s->buf_len=0; return 0; }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
35 s->buf_pos=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
36 s->buf_len=len;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
37 s->pos+=len;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
38 // printf("[%d]",len);fflush(stdout);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
39 return len;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
40 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
41
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
42 inline unsigned int stream_read_char(stream_t *s){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
43 return (s->buf_pos<s->buf_len)?s->buffer[s->buf_pos++]:
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
44 (stream_fill_buffer(s)?s->buffer[s->buf_pos++]:0);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
45 // if(s->buf_pos<s->buf_len) return s->buffer[s->buf_pos++];
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
46 // stream_fill_buffer(s);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
47 // if(s->buf_pos<s->buf_len) return s->buffer[s->buf_pos++];
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
48 // return 0; // EOF
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
49 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
50
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
51 inline unsigned int stream_read_word(stream_t *s){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
52 int x,y;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
53 x=stream_read_char(s);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
54 y=stream_read_char(s);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
55 return (x<<8)|y;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
56 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
57
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
58 inline unsigned int stream_read_dword(stream_t *s){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
59 unsigned int y;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
60 y=stream_read_char(s);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
61 y=(y<<8)|stream_read_char(s);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
62 y=(y<<8)|stream_read_char(s);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
63 y=(y<<8)|stream_read_char(s);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
64 return y;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
65 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
66
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
67 inline unsigned int stream_read_word_le(stream_t *s){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
68 int x,y;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
69 x=stream_read_char(s);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
70 y=stream_read_char(s);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
71 return (y<<8)|x;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
72 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
73
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
74 inline unsigned int stream_read_dword_le(stream_t *s){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
75 unsigned int y;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
76 y=stream_read_char(s);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
77 y|=stream_read_char(s)<<8;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
78 y|=stream_read_char(s)<<16;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
79 y|=stream_read_char(s)<<24;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
80 return y;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
81 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
82
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
83 inline void stream_read(stream_t *s,char* mem,int len){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
84 while(len>0){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
85 int x;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
86 x=s->buf_len-s->buf_pos;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
87 if(x==0){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
88 if(!stream_fill_buffer(s)) return; // EOF
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
89 x=s->buf_len-s->buf_pos;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
90 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
91 if(s->buf_pos>s->buf_len) printf("stream_read: WARNING! s->buf_pos>s->buf_len\n");
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
92 if(x>len) x=len;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
93 memcpy(mem,&s->buffer[s->buf_pos],x);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
94 s->buf_pos+=x; mem+=x; len-=x;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
95 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
96 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
97
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
98 inline int stream_eof(stream_t *s){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
99 return s->eof;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
100 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
101
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
102 inline int stream_tell(stream_t *s){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
103 return s->pos+s->buf_pos-s->buf_len;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
104 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
105
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
106 inline int stream_seek(stream_t *s,unsigned int pos){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
107 unsigned int newpos;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
108
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
109 if(verbose>=3) printf("seek to 0x%X\n",pos);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
110
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
111 if(pos<s->pos){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
112 int x=pos-(s->pos-s->buf_len);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
113 if(x>=0){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
114 s->buf_pos=x;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
115 // putchar('*');fflush(stdout);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
116 return 1;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
117 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
118 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
119
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
120 if(verbose>=3){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
121 printf("s->pos=%X newpos=%X new_bufpos=%X buflen=%X \n",
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
122 s->pos,newpos,pos,s->buf_len);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
123 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
124
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
125 s->buf_pos=s->buf_len=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
126
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
127 switch(s->type){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
128 case STREAMTYPE_FILE:
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
129 newpos=pos&(~4095);break;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
130 case STREAMTYPE_VCD:
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
131 newpos=(pos/VCD_SECTOR_DATA)*VCD_SECTOR_DATA;break;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
132 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
133
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
134 pos-=newpos;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
135
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
136 if(newpos==0 || newpos!=s->pos){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
137 s->pos=newpos; // real seek
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
138 switch(s->type){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
139 case STREAMTYPE_FILE:
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
140 if(lseek(s->fd,s->pos,SEEK_SET)<0) s->eof=1;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
141 break;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
142 case STREAMTYPE_VCD:
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
143 #ifdef VCD_CACHE
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
144 vcd_cache_seek(s->pos/VCD_SECTOR_DATA);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
145 #else
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
146 vcd_set_msf(s->pos/VCD_SECTOR_DATA);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
147 #endif
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
148 break;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
149 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
150 // putchar('.');fflush(stdout);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
151 //} else {
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
152 // putchar('%');fflush(stdout);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
153 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
154
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
155 stream_fill_buffer(s);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
156 if(pos>=0 && pos<s->buf_len){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
157 s->buf_pos=pos; // byte position in sector
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
158 return 1;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
159 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
160 printf("stream_seek: WARNING! Can't seek to 0x%X !\n",pos+newpos);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
161 return 0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
162 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
163
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
164 inline void stream_skip(stream_t *s,int len){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
165 if(len<0 || len>2*STREAM_BUFFER_SIZE){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
166 // negative or big skip!
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
167 stream_seek(s,stream_tell(s)+len);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
168 return;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
169 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
170 while(len>0){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
171 int x=s->buf_len-s->buf_pos;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
172 if(x==0){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
173 if(!stream_fill_buffer(s)) return; // EOF
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
174 x=s->buf_len-s->buf_pos;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
175 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
176 if(x>len) x=len;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
177 //memcpy(mem,&s->buf[s->buf_pos],x);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
178 s->buf_pos+=x; len-=x;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
179 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
180 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
181
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
182 void stream_reset(stream_t *s){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
183 if(s->eof){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
184 s->pos=0; //ftell(f);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
185 // s->buf_pos=s->buf_len=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
186 s->eof=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
187 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
188 //stream_seek(s,0);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
189 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
190
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
191 stream_t* new_stream(int fd,int type){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
192 stream_t *s=malloc(sizeof(stream_t));
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
193 s->fd=fd;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
194 s->type=type;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
195 stream_reset(s);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
196 return s;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
197 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
198
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
199 void free_stream(stream_t *s){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
200 free(s);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
201 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
202
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
203