annotate demuxer.c @ 196:9b82a3ef38ee

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