comparison libmpdemux/stream.h @ 2310:9e059416eea6

libdemuxer...
author arpi
date Sat, 20 Oct 2001 18:49:08 +0000
parents stream.h@99f079ff9374
children e22ec6fce385
comparison
equal deleted inserted replaced
2309:3128b9d8b4ea 2310:9e059416eea6
1 #ifndef __STREAM_H
2 #define __STREAM_H
3
4 #define STREAM_BUFFER_SIZE 2048
5
6 #define STREAMTYPE_FILE 0
7 #define STREAMTYPE_VCD 1
8 #define STREAMTYPE_STREAM 2 // same as FILE but no seeking (for stdin)
9 #define STREAMTYPE_DVD 3
10 #define STREAMTYPE_MEMORY 4
11
12 #define VCD_SECTOR_SIZE 2352
13 #define VCD_SECTOR_OFFS 24
14 #define VCD_SECTOR_DATA 2324
15
16 int vcd_seek_to_track(int fd,int track);
17 void vcd_read_toc(int fd);
18
19 #ifdef VCD_CACHE
20 void vcd_cache_init(int s);
21 #endif
22
23 typedef struct {
24 int fd;
25 off_t pos;
26 int eof;
27 int type; // 0=file 1=VCD
28 unsigned int buf_pos,buf_len;
29 off_t start_pos,end_pos;
30 void* priv; // used for DVD
31 unsigned char buffer[STREAM_BUFFER_SIZE>VCD_SECTOR_SIZE?STREAM_BUFFER_SIZE:VCD_SECTOR_SIZE];
32 } stream_t;
33
34 int stream_fill_buffer(stream_t *s);
35
36 int stream_seek_long(stream_t *s,off_t pos);
37
38 inline static int stream_read_char(stream_t *s){
39 return (s->buf_pos<s->buf_len)?s->buffer[s->buf_pos++]:
40 (stream_fill_buffer(s)?s->buffer[s->buf_pos++]:-256);
41 // if(s->buf_pos<s->buf_len) return s->buffer[s->buf_pos++];
42 // stream_fill_buffer(s);
43 // if(s->buf_pos<s->buf_len) return s->buffer[s->buf_pos++];
44 // return 0; // EOF
45 }
46
47 inline static unsigned int stream_read_word(stream_t *s){
48 int x,y;
49 x=stream_read_char(s);
50 y=stream_read_char(s);
51 return (x<<8)|y;
52 }
53
54 inline static unsigned int stream_read_dword(stream_t *s){
55 unsigned int y;
56 y=stream_read_char(s);
57 y=(y<<8)|stream_read_char(s);
58 y=(y<<8)|stream_read_char(s);
59 y=(y<<8)|stream_read_char(s);
60 return y;
61 }
62
63 inline static unsigned int stream_read_word_le(stream_t *s){
64 int x,y;
65 x=stream_read_char(s);
66 y=stream_read_char(s);
67 return (y<<8)|x;
68 }
69
70 inline static unsigned int stream_read_dword_le(stream_t *s){
71 unsigned int y;
72 y=stream_read_char(s);
73 y|=stream_read_char(s)<<8;
74 y|=stream_read_char(s)<<16;
75 y|=stream_read_char(s)<<24;
76 return y;
77 }
78
79 inline static void stream_read(stream_t *s,char* mem,int len){
80 while(len>0){
81 int x;
82 x=s->buf_len-s->buf_pos;
83 if(x==0){
84 if(!stream_fill_buffer(s)) return; // EOF
85 x=s->buf_len-s->buf_pos;
86 }
87 if(s->buf_pos>s->buf_len) printf("stream_read: WARNING! s->buf_pos>s->buf_len\n");
88 if(x>len) x=len;
89 memcpy(mem,&s->buffer[s->buf_pos],x);
90 s->buf_pos+=x; mem+=x; len-=x;
91 }
92 }
93
94 inline static int stream_eof(stream_t *s){
95 return s->eof;
96 }
97
98 inline static off_t stream_tell(stream_t *s){
99 return s->pos+s->buf_pos-s->buf_len;
100 }
101
102 inline static int stream_seek(stream_t *s,off_t pos){
103
104 // if(verbose>=3) printf("seek to 0x%qX\n",(long long)pos);
105
106 if(pos<s->pos){
107 off_t x=pos-(s->pos-s->buf_len);
108 if(x>=0){
109 s->buf_pos=x;
110 // putchar('*');fflush(stdout);
111 return 1;
112 }
113 }
114
115 return stream_seek_long(s,pos);
116 }
117
118 inline static int stream_skip(stream_t *s,int len){
119 if(len<0 || (len>2*STREAM_BUFFER_SIZE && s->type!=STREAMTYPE_STREAM)){
120 // negative or big skip!
121 return stream_seek(s,stream_tell(s)+len);
122 }
123 while(len>0){
124 int x=s->buf_len-s->buf_pos;
125 if(x==0){
126 if(!stream_fill_buffer(s)) return 0; // EOF
127 x=s->buf_len-s->buf_pos;
128 }
129 if(x>len) x=len;
130 //memcpy(mem,&s->buf[s->buf_pos],x);
131 s->buf_pos+=x; len-=x;
132 }
133 return 1;
134 }
135
136 void stream_reset(stream_t *s);
137 stream_t* new_stream(int fd,int type);
138 void free_stream(stream_t *s);
139 stream_t* new_memory_stream(unsigned char* data,int len);
140
141 #endif // __STREAM_H