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;
|
552
|
97 demux_stream_t *sub;
|
1
|
98 } demuxer_t;
|
|
99
|
552
|
100 demuxer_t* new_demuxer(stream_t *stream,int type,int a_id,int v_id,int s_id){
|
1
|
101 demuxer_t *d=malloc(sizeof(demuxer_t));
|
|
102 d->stream=stream;
|
|
103 d->synced=0;
|
|
104 d->filepos=0;
|
|
105 d->audio=new_demuxer_stream(d,a_id);
|
|
106 d->video=new_demuxer_stream(d,v_id);
|
552
|
107 d->sub=new_demuxer_stream(d,s_id);
|
1
|
108 d->type=type;
|
|
109 return d;
|
|
110 }
|
|
111
|
|
112 static void ds_add_packet(demux_stream_t *ds,demux_packet_t* dp){
|
|
113 // demux_packet_t* dp=new_demux_packet(len);
|
|
114 // stream_read(stream,dp->buffer,len);
|
|
115 // dp->pts=pts; //(float)pts/90000.0f;
|
|
116 // dp->pos=pos;
|
|
117 // append packet to DS stream:
|
|
118 ++ds->packs;
|
|
119 ds->bytes+=dp->len;
|
|
120 if(ds->last){
|
|
121 // next packet in stream
|
|
122 ds->last->next=dp;
|
|
123 ds->last=dp;
|
|
124 } else {
|
|
125 // first packet in stream
|
|
126 ds->first=ds->last=dp;
|
|
127 }
|
|
128 if(verbose>=2)
|
|
129 printf("DEMUX: Append packet to %s, len=%d pts=%5.3f pos=%d [packs: A=%d V=%d]\n",
|
|
130 (ds==ds->demuxer->audio)?"d_audio":"d_video",
|
|
131 dp->len,dp->pts,dp->pos,ds->demuxer->audio->packs,ds->demuxer->video->packs);
|
|
132 }
|
|
133
|
|
134 static void ds_read_packet(demux_stream_t *ds,stream_t *stream,int len,float pts,int pos){
|
|
135 demux_packet_t* dp=new_demux_packet(len);
|
|
136 stream_read(stream,dp->buffer,len);
|
|
137 dp->pts=pts; //(float)pts/90000.0f;
|
|
138 dp->pos=pos;
|
|
139 // append packet to DS stream:
|
|
140 ds_add_packet(ds,dp);
|
|
141 }
|
|
142
|
|
143
|
|
144 // return value:
|
|
145 // 0 = EOF or no stream found or invalid type
|
|
146 // 1 = successfully read a packet
|
|
147 int demux_mpg_es_fill_buffer(demuxer_t *demux);
|
|
148 int demux_mpg_fill_buffer(demuxer_t *demux);
|
|
149 int demux_avi_fill_buffer(demuxer_t *demux);
|
|
150 int demux_avi_fill_buffer_ni(demuxer_t *demux,demux_stream_t *ds);
|
|
151 int demux_avi_fill_buffer_nini(demuxer_t *demux,demux_stream_t *ds);
|
|
152 int demux_asf_fill_buffer(demuxer_t *demux);
|
|
153
|
|
154 int demux_fill_buffer(demuxer_t *demux,demux_stream_t *ds){
|
|
155 // Note: parameter 'ds' can be NULL!
|
|
156 switch(demux->type){
|
|
157 case DEMUXER_TYPE_MPEG_ES: return demux_mpg_es_fill_buffer(demux);
|
|
158 case DEMUXER_TYPE_MPEG_PS: return demux_mpg_fill_buffer(demux);
|
|
159 case DEMUXER_TYPE_AVI: return demux_avi_fill_buffer(demux);
|
|
160 case DEMUXER_TYPE_AVI_NI: return demux_avi_fill_buffer_ni(demux,ds);
|
|
161 case DEMUXER_TYPE_AVI_NINI: return demux_avi_fill_buffer_nini(demux,ds);
|
|
162 case DEMUXER_TYPE_ASF: return demux_asf_fill_buffer(demux);
|
|
163 }
|
|
164 return 0;
|
|
165 }
|
|
166
|
|
167 // return value:
|
|
168 // 0 = EOF
|
|
169 // 1 = succesfull
|
|
170 inline static int ds_fill_buffer(demux_stream_t *ds){
|
|
171 demuxer_t *demux=ds->demuxer;
|
|
172 if(ds->buffer) free(ds->buffer);
|
|
173 if(verbose>2){
|
|
174 if(ds==demux->audio) printf("ds_fill_buffer(d_audio) called\n");else
|
|
175 if(ds==demux->video) printf("ds_fill_buffer(d_video) called\n");else
|
|
176 printf("ds_fill_buffer(unknown 0x%X) called\n",ds);
|
|
177 }
|
|
178 while(1){
|
|
179 if(ds->packs){
|
|
180 demux_packet_t *p=ds->first;
|
|
181 // copy useful data:
|
|
182 ds->buffer=p->buffer;
|
|
183 ds->buffer_pos=0;
|
|
184 ds->buffer_size=p->len;
|
|
185 ds->pos=p->pos;
|
|
186 ds->dpos+=p->len; // !!!
|
|
187 ds->pts=p->pts;
|
|
188 // free packet:
|
|
189 ds->bytes-=p->len;
|
|
190 ds->first=p->next;
|
|
191 if(!ds->first) ds->last=NULL;
|
|
192 free(p);
|
|
193 --ds->packs;
|
|
194 return 1; //ds->buffer_size;
|
|
195 }
|
|
196 if(demux->audio->packs>=MAX_PACKS || demux->audio->bytes>=MAX_PACK_BYTES){
|
|
197 printf("\nDEMUXER: Too many (%d in %d bytes) audio packets in the buffer!\n",demux->audio->packs,demux->audio->bytes);
|
|
198 printf("(maybe you play a non-interleaved stream/file or audio codec failed)\n");
|
|
199 break;
|
|
200 }
|
|
201 if(demux->video->packs>=MAX_PACKS || demux->video->bytes>=MAX_PACK_BYTES){
|
|
202 printf("\nDEMUXER: Too many (%d in %d bytes) video packets in the buffer!\n",demux->video->packs,demux->video->bytes);
|
|
203 printf("(maybe you play a non-interleaved stream/file or video codec failed)\n");
|
|
204 break;
|
|
205 }
|
|
206 if(!demux_fill_buffer(demux,ds)) break; // EOF
|
|
207 }
|
|
208 ds->buffer_pos=ds->buffer_size=0;
|
|
209 ds->buffer=NULL;
|
|
210 if(verbose) printf("ds_fill_buffer: EOF reached (stream: %s) \n",ds==demux->audio?"audio":"video");
|
|
211 ds->eof=1;
|
|
212 return 0;
|
|
213 }
|
|
214
|
|
215 inline int ds_tell(demux_stream_t *ds){
|
|
216 return (ds->dpos-ds->buffer_size)+ds->buffer_pos;
|
|
217 }
|
|
218
|
|
219 int demux_read_data(demux_stream_t *ds,char* mem,int len){
|
|
220 int x;
|
|
221 int bytes=0;
|
|
222 while(len>0){
|
|
223 x=ds->buffer_size-ds->buffer_pos;
|
|
224 if(x==0){
|
|
225 if(!ds_fill_buffer(ds)) return bytes;
|
|
226 } else {
|
|
227 if(x>len) x=len;
|
|
228 if(mem) memcpy(mem+bytes,&ds->buffer[ds->buffer_pos],x);
|
|
229 bytes+=x;len-=x;ds->buffer_pos+=x;
|
|
230 }
|
|
231 }
|
|
232 return bytes;
|
|
233 }
|
|
234
|
|
235
|
|
236 #if 1
|
|
237 #define demux_getc(ds) (\
|
|
238 (ds->buffer_pos<ds->buffer_size) ? ds->buffer[ds->buffer_pos++] \
|
|
239 :((!ds_fill_buffer(ds))? (-1) : ds->buffer[ds->buffer_pos++] ) )
|
|
240 #else
|
|
241 inline static int demux_getc(demux_stream_t *ds){
|
|
242 if(ds->buffer_pos>=ds->buffer_size){
|
|
243 if(!ds_fill_buffer(ds)){
|
|
244 // printf("DEMUX_GETC: EOF reached!\n");
|
|
245 return -1; // EOF
|
|
246 }
|
|
247 }
|
|
248 // printf("[%02X]",ds->buffer[ds->buffer_pos]);
|
|
249 return ds->buffer[ds->buffer_pos++];
|
|
250 }
|
|
251 #endif
|
|
252
|
|
253 void ds_free_packs(demux_stream_t *ds){
|
|
254 demux_packet_t *dp=ds->first;
|
|
255 while(dp){
|
|
256 demux_packet_t *dn=dp->next;
|
|
257 free(dp->buffer);
|
|
258 free(dp);
|
|
259 dp=dn;
|
|
260 }
|
|
261 if(ds->asf_packet){
|
|
262 // free unfinished .asf fragments:
|
|
263 free(ds->asf_packet->buffer);
|
|
264 free(ds->asf_packet);
|
|
265 ds->asf_packet=NULL;
|
|
266 }
|
|
267 ds->first=ds->last=NULL;
|
|
268 ds->packs=0; // !!!!!
|
|
269 ds->bytes=0;
|
|
270 if(ds->buffer) free(ds->buffer);
|
|
271 ds->buffer=NULL;
|
|
272 ds->buffer_pos=ds->buffer_size;
|
|
273 ds->pts=0;
|
|
274 }
|
|
275
|
|
276 int ds_get_packet(demux_stream_t *ds,char **start){
|
|
277 while(1){
|
|
278 int len;
|
|
279 if(ds->buffer_pos>=ds->buffer_size){
|
|
280 if(!ds_fill_buffer(ds)){
|
|
281 // EOF
|
|
282 *start = NULL;
|
|
283 return -1;
|
|
284 }
|
|
285 }
|
|
286 len=ds->buffer_size-ds->buffer_pos;
|
|
287 *start = &ds->buffer[ds->buffer_pos];
|
|
288 ds->buffer_pos+=len;
|
|
289 return len;
|
|
290 }
|
|
291 }
|
554
|
292
|
|
293 int ds_get_packet_sub(demux_stream_t *ds,char **start){
|
|
294 while(1){
|
|
295 int len;
|
|
296 if(ds->buffer_pos>=ds->buffer_size){
|
|
297 *start = NULL;
|
|
298 if(!ds->packs) return -1; // no sub
|
|
299 if(!ds_fill_buffer(ds)) return -1; // EOF
|
|
300 }
|
|
301 len=ds->buffer_size-ds->buffer_pos;
|
|
302 *start = &ds->buffer[ds->buffer_pos];
|
|
303 ds->buffer_pos+=len;
|
|
304 return len;
|
|
305 }
|
|
306 }
|
|
307
|