annotate demuxer.c @ 508:05da4b0e3bb8

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