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