1
|
1 //=================== DEMUXER v2.5 =========================
|
|
2
|
|
3 #define MAX_PACKS 2048
|
|
4 #define MAX_PACK_BYTES 0x400000
|
|
5
|
|
6 typedef struct demux_packet_st {
|
|
7 int len;
|
|
8 float pts;
|
|
9 int pos; // pozicio indexben (AVI) ill. fileban (MPG)
|
|
10 unsigned char* buffer;
|
|
11 struct demux_packet_st* next;
|
|
12 } demux_packet_t;
|
|
13
|
|
14 inline demux_packet_t* new_demux_packet(int len){
|
|
15 demux_packet_t* dp=malloc(sizeof(demux_packet_t));
|
|
16 dp->len=len;
|
|
17 dp->buffer=malloc(len);
|
|
18 dp->next=NULL;
|
|
19 dp->pts=0;
|
|
20 dp->pos=0;
|
|
21 return dp;
|
|
22 }
|
|
23
|
|
24 inline void free_demux_packet(demux_packet_t* dp){
|
|
25 free(dp->buffer);
|
|
26 free(dp);
|
|
27 }
|
|
28
|
|
29 typedef struct {
|
|
30 int buffer_pos; // current buffer position
|
|
31 int buffer_size; // current buffer size
|
|
32 unsigned char* buffer; // current buffer
|
|
33 float pts; // current buffer's pts
|
|
34 int eof; // end of demuxed stream? (true if all buffer empty)
|
|
35 int pos; // position in the input stream (file)
|
|
36 int dpos; // position in the demuxed stream
|
|
37 //---------------
|
|
38 int packs; // number of packets in buffer
|
|
39 int bytes; // total bytes of packets in buffer
|
|
40 demux_packet_t *first; // read to current buffer from here
|
|
41 demux_packet_t *last; // append new packets from input stream to here
|
|
42 int id; // stream ID (for multiple audio/video streams)
|
|
43 int type; // stream type (currently used only for audio)
|
|
44 struct demuxer_st *demuxer; // parent demuxer structure (stream handler)
|
|
45 // ---- asf -----
|
|
46 demux_packet_t *asf_packet; // read asf fragments here
|
|
47 int asf_seq;
|
291
|
48 // ---- stream header ----
|
|
49 void* sh;
|
1
|
50 } demux_stream_t;
|
|
51
|
|
52 demux_stream_t* new_demuxer_stream(struct demuxer_st *demuxer,int id){
|
|
53 demux_stream_t* ds=malloc(sizeof(demux_stream_t));
|
|
54 ds->buffer_pos=ds->buffer_size=0;
|
|
55 ds->buffer=NULL;
|
|
56 ds->pts=0;
|
|
57 ds->eof=0;
|
|
58 ds->pos=0;
|
|
59 ds->dpos=0;
|
|
60 //---------------
|
|
61 ds->packs=0;
|
|
62 ds->bytes=0;
|
|
63 ds->first=ds->last=NULL;
|
|
64 ds->id=id;
|
|
65 ds->type=-1;
|
|
66 ds->demuxer=demuxer;
|
|
67 //----------------
|
|
68 ds->asf_seq=-1;
|
|
69 ds->asf_packet=NULL;
|
291
|
70 //----------------
|
|
71 ds->sh=NULL;
|
1
|
72 return ds;
|
|
73 }
|
|
74
|
|
75 #define DEMUXER_TYPE_UNKNOWN 0
|
|
76 #define DEMUXER_TYPE_MPEG_ES 1
|
|
77 #define DEMUXER_TYPE_MPEG_PS 2
|
|
78 #define DEMUXER_TYPE_AVI 3
|
|
79 #define DEMUXER_TYPE_AVI_NI 4
|
|
80 #define DEMUXER_TYPE_AVI_NINI 5
|
|
81 #define DEMUXER_TYPE_ASF 6
|
|
82
|
|
83 #define DEMUXER_TIME_NONE 0
|
|
84 #define DEMUXER_TIME_PTS 1
|
|
85 #define DEMUXER_TIME_FILE 2
|
|
86 #define DEMUXER_TIME_BPS 3
|
|
87
|
|
88 typedef struct demuxer_st {
|
|
89 stream_t *stream;
|
|
90 int synced; // stream synced (used by mpeg)
|
|
91 int filepos; // input stream current pos.
|
|
92 int endpos; // input stream end pos. (return EOF fi filepos>endpos)
|
|
93 int type; // mpeg system stream, mpeg elementary s., avi raw, avi indexed
|
|
94 // int time_src;// time source (pts/file/bps)
|
|
95 demux_stream_t *audio;
|
|
96 demux_stream_t *video;
|
|
97 } demuxer_t;
|
|
98
|
|
99 demuxer_t* new_demuxer(stream_t *stream,int type,int a_id,int v_id){
|
|
100 demuxer_t *d=malloc(sizeof(demuxer_t));
|
|
101 d->stream=stream;
|
|
102 d->synced=0;
|
|
103 d->filepos=0;
|
|
104 d->audio=new_demuxer_stream(d,a_id);
|
|
105 d->video=new_demuxer_stream(d,v_id);
|
|
106 d->type=type;
|
|
107 return d;
|
|
108 }
|
|
109
|
|
110 static void ds_add_packet(demux_stream_t *ds,demux_packet_t* dp){
|
|
111 // demux_packet_t* dp=new_demux_packet(len);
|
|
112 // stream_read(stream,dp->buffer,len);
|
|
113 // dp->pts=pts; //(float)pts/90000.0f;
|
|
114 // dp->pos=pos;
|
|
115 // append packet to DS stream:
|
|
116 ++ds->packs;
|
|
117 ds->bytes+=dp->len;
|
|
118 if(ds->last){
|
|
119 // next packet in stream
|
|
120 ds->last->next=dp;
|
|
121 ds->last=dp;
|
|
122 } else {
|
|
123 // first packet in stream
|
|
124 ds->first=ds->last=dp;
|
|
125 }
|
|
126 if(verbose>=2)
|
|
127 printf("DEMUX: Append packet to %s, len=%d pts=%5.3f pos=%d [packs: A=%d V=%d]\n",
|
|
128 (ds==ds->demuxer->audio)?"d_audio":"d_video",
|
|
129 dp->len,dp->pts,dp->pos,ds->demuxer->audio->packs,ds->demuxer->video->packs);
|
|
130 }
|
|
131
|
|
132 static void ds_read_packet(demux_stream_t *ds,stream_t *stream,int len,float pts,int pos){
|
|
133 demux_packet_t* dp=new_demux_packet(len);
|
|
134 stream_read(stream,dp->buffer,len);
|
|
135 dp->pts=pts; //(float)pts/90000.0f;
|
|
136 dp->pos=pos;
|
|
137 // append packet to DS stream:
|
|
138 ds_add_packet(ds,dp);
|
|
139 }
|
|
140
|
|
141
|
|
142 // return value:
|
|
143 // 0 = EOF or no stream found or invalid type
|
|
144 // 1 = successfully read a packet
|
|
145 int demux_mpg_es_fill_buffer(demuxer_t *demux);
|
|
146 int demux_mpg_fill_buffer(demuxer_t *demux);
|
|
147 int demux_avi_fill_buffer(demuxer_t *demux);
|
|
148 int demux_avi_fill_buffer_ni(demuxer_t *demux,demux_stream_t *ds);
|
|
149 int demux_avi_fill_buffer_nini(demuxer_t *demux,demux_stream_t *ds);
|
|
150 int demux_asf_fill_buffer(demuxer_t *demux);
|
|
151
|
|
152 int demux_fill_buffer(demuxer_t *demux,demux_stream_t *ds){
|
|
153 // Note: parameter 'ds' can be NULL!
|
|
154 switch(demux->type){
|
|
155 case DEMUXER_TYPE_MPEG_ES: return demux_mpg_es_fill_buffer(demux);
|
|
156 case DEMUXER_TYPE_MPEG_PS: return demux_mpg_fill_buffer(demux);
|
|
157 case DEMUXER_TYPE_AVI: return demux_avi_fill_buffer(demux);
|
|
158 case DEMUXER_TYPE_AVI_NI: return demux_avi_fill_buffer_ni(demux,ds);
|
|
159 case DEMUXER_TYPE_AVI_NINI: return demux_avi_fill_buffer_nini(demux,ds);
|
|
160 case DEMUXER_TYPE_ASF: return demux_asf_fill_buffer(demux);
|
|
161 }
|
|
162 return 0;
|
|
163 }
|
|
164
|
|
165 // return value:
|
|
166 // 0 = EOF
|
|
167 // 1 = succesfull
|
|
168 inline static int ds_fill_buffer(demux_stream_t *ds){
|
|
169 demuxer_t *demux=ds->demuxer;
|
|
170 if(ds->buffer) free(ds->buffer);
|
|
171 if(verbose>2){
|
|
172 if(ds==demux->audio) printf("ds_fill_buffer(d_audio) called\n");else
|
|
173 if(ds==demux->video) printf("ds_fill_buffer(d_video) called\n");else
|
|
174 printf("ds_fill_buffer(unknown 0x%X) called\n",ds);
|
|
175 }
|
|
176 while(1){
|
|
177 if(ds->packs){
|
|
178 demux_packet_t *p=ds->first;
|
|
179 // copy useful data:
|
|
180 ds->buffer=p->buffer;
|
|
181 ds->buffer_pos=0;
|
|
182 ds->buffer_size=p->len;
|
|
183 ds->pos=p->pos;
|
|
184 ds->dpos+=p->len; // !!!
|
|
185 ds->pts=p->pts;
|
|
186 // free packet:
|
|
187 ds->bytes-=p->len;
|
|
188 ds->first=p->next;
|
|
189 if(!ds->first) ds->last=NULL;
|
|
190 free(p);
|
|
191 --ds->packs;
|
|
192 return 1; //ds->buffer_size;
|
|
193 }
|
|
194 if(demux->audio->packs>=MAX_PACKS || demux->audio->bytes>=MAX_PACK_BYTES){
|
|
195 printf("\nDEMUXER: Too many (%d in %d bytes) audio packets in the buffer!\n",demux->audio->packs,demux->audio->bytes);
|
|
196 printf("(maybe you play a non-interleaved stream/file or audio codec failed)\n");
|
|
197 break;
|
|
198 }
|
|
199 if(demux->video->packs>=MAX_PACKS || demux->video->bytes>=MAX_PACK_BYTES){
|
|
200 printf("\nDEMUXER: Too many (%d in %d bytes) video packets in the buffer!\n",demux->video->packs,demux->video->bytes);
|
|
201 printf("(maybe you play a non-interleaved stream/file or video codec failed)\n");
|
|
202 break;
|
|
203 }
|
|
204 if(!demux_fill_buffer(demux,ds)) break; // EOF
|
|
205 }
|
|
206 ds->buffer_pos=ds->buffer_size=0;
|
|
207 ds->buffer=NULL;
|
|
208 if(verbose) printf("ds_fill_buffer: EOF reached (stream: %s) \n",ds==demux->audio?"audio":"video");
|
|
209 ds->eof=1;
|
|
210 return 0;
|
|
211 }
|
|
212
|
|
213 inline int ds_tell(demux_stream_t *ds){
|
|
214 return (ds->dpos-ds->buffer_size)+ds->buffer_pos;
|
|
215 }
|
|
216
|
|
217 int demux_read_data(demux_stream_t *ds,char* mem,int len){
|
|
218 int x;
|
|
219 int bytes=0;
|
|
220 while(len>0){
|
|
221 x=ds->buffer_size-ds->buffer_pos;
|
|
222 if(x==0){
|
|
223 if(!ds_fill_buffer(ds)) return bytes;
|
|
224 } else {
|
|
225 if(x>len) x=len;
|
|
226 if(mem) memcpy(mem+bytes,&ds->buffer[ds->buffer_pos],x);
|
|
227 bytes+=x;len-=x;ds->buffer_pos+=x;
|
|
228 }
|
|
229 }
|
|
230 return bytes;
|
|
231 }
|
|
232
|
|
233
|
|
234 #if 1
|
|
235 #define demux_getc(ds) (\
|
|
236 (ds->buffer_pos<ds->buffer_size) ? ds->buffer[ds->buffer_pos++] \
|
|
237 :((!ds_fill_buffer(ds))? (-1) : ds->buffer[ds->buffer_pos++] ) )
|
|
238 #else
|
|
239 inline static int demux_getc(demux_stream_t *ds){
|
|
240 if(ds->buffer_pos>=ds->buffer_size){
|
|
241 if(!ds_fill_buffer(ds)){
|
|
242 // printf("DEMUX_GETC: EOF reached!\n");
|
|
243 return -1; // EOF
|
|
244 }
|
|
245 }
|
|
246 // printf("[%02X]",ds->buffer[ds->buffer_pos]);
|
|
247 return ds->buffer[ds->buffer_pos++];
|
|
248 }
|
|
249 #endif
|
|
250
|
|
251 void ds_free_packs(demux_stream_t *ds){
|
|
252 demux_packet_t *dp=ds->first;
|
|
253 while(dp){
|
|
254 demux_packet_t *dn=dp->next;
|
|
255 free(dp->buffer);
|
|
256 free(dp);
|
|
257 dp=dn;
|
|
258 }
|
|
259 if(ds->asf_packet){
|
|
260 // free unfinished .asf fragments:
|
|
261 free(ds->asf_packet->buffer);
|
|
262 free(ds->asf_packet);
|
|
263 ds->asf_packet=NULL;
|
|
264 }
|
|
265 ds->first=ds->last=NULL;
|
|
266 ds->packs=0; // !!!!!
|
|
267 ds->bytes=0;
|
|
268 if(ds->buffer) free(ds->buffer);
|
|
269 ds->buffer=NULL;
|
|
270 ds->buffer_pos=ds->buffer_size;
|
|
271 ds->pts=0;
|
|
272 }
|
|
273
|
|
274 int ds_get_packet(demux_stream_t *ds,char **start){
|
|
275 while(1){
|
|
276 int len;
|
|
277 if(ds->buffer_pos>=ds->buffer_size){
|
|
278 if(!ds_fill_buffer(ds)){
|
|
279 // EOF
|
|
280 *start = NULL;
|
|
281 return -1;
|
|
282 }
|
|
283 }
|
|
284 len=ds->buffer_size-ds->buffer_pos;
|
|
285 *start = &ds->buffer[ds->buffer_pos];
|
|
286 ds->buffer_pos+=len;
|
|
287 return len;
|
|
288 }
|
|
289 }
|