Mercurial > mplayer.hg
annotate demuxer.c @ 1247:429aacbe23a7
typo fix
author | gabucino |
---|---|
date | Fri, 29 Jun 2001 22:46:21 +0000 |
parents | 3bcc435cd5a2 |
children | b4457de47804 |
rev | line source |
---|---|
1 | 1 //=================== DEMUXER v2.5 ========================= |
2 | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
3 #include <stdio.h> |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
4 #include <stdlib.h> |
1 | 5 |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
6 extern int verbose; // defined in mplayer.c |
1 | 7 |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
8 #include "stream.h" |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
9 #include "demuxer.h" |
1 | 10 |
11 demux_stream_t* new_demuxer_stream(struct demuxer_st *demuxer,int id){ | |
12 demux_stream_t* ds=malloc(sizeof(demux_stream_t)); | |
13 ds->buffer_pos=ds->buffer_size=0; | |
14 ds->buffer=NULL; | |
15 ds->pts=0; | |
746 | 16 ds->pts_bytes=0; |
1 | 17 ds->eof=0; |
18 ds->pos=0; | |
19 ds->dpos=0; | |
889 | 20 ds->pack_no=0; |
1 | 21 //--------------- |
22 ds->packs=0; | |
23 ds->bytes=0; | |
24 ds->first=ds->last=NULL; | |
25 ds->id=id; | |
26 ds->type=-1; | |
27 ds->demuxer=demuxer; | |
28 //---------------- | |
29 ds->asf_seq=-1; | |
30 ds->asf_packet=NULL; | |
291 | 31 //---------------- |
32 ds->sh=NULL; | |
1 | 33 return ds; |
34 } | |
35 | |
552 | 36 demuxer_t* new_demuxer(stream_t *stream,int type,int a_id,int v_id,int s_id){ |
1 | 37 demuxer_t *d=malloc(sizeof(demuxer_t)); |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
38 memset(d,0,sizeof(demuxer_t)); |
1 | 39 d->stream=stream; |
598 | 40 d->movi_start=stream->start_pos; |
41 d->movi_end=stream->end_pos; | |
1 | 42 d->synced=0; |
43 d->filepos=0; | |
44 d->audio=new_demuxer_stream(d,a_id); | |
45 d->video=new_demuxer_stream(d,v_id); | |
552 | 46 d->sub=new_demuxer_stream(d,s_id); |
1 | 47 d->type=type; |
48 return d; | |
49 } | |
50 | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
51 void ds_add_packet(demux_stream_t *ds,demux_packet_t* dp){ |
1 | 52 // demux_packet_t* dp=new_demux_packet(len); |
53 // stream_read(stream,dp->buffer,len); | |
54 // dp->pts=pts; //(float)pts/90000.0f; | |
55 // dp->pos=pos; | |
56 // append packet to DS stream: | |
57 ++ds->packs; | |
58 ds->bytes+=dp->len; | |
59 if(ds->last){ | |
60 // next packet in stream | |
61 ds->last->next=dp; | |
62 ds->last=dp; | |
63 } else { | |
64 // first packet in stream | |
65 ds->first=ds->last=dp; | |
66 } | |
67 if(verbose>=2) | |
68 printf("DEMUX: Append packet to %s, len=%d pts=%5.3f pos=%d [packs: A=%d V=%d]\n", | |
69 (ds==ds->demuxer->audio)?"d_audio":"d_video", | |
70 dp->len,dp->pts,dp->pos,ds->demuxer->audio->packs,ds->demuxer->video->packs); | |
71 } | |
72 | |
979 | 73 void ds_read_packet(demux_stream_t *ds,stream_t *stream,int len,float pts,int pos,int flags){ |
1 | 74 demux_packet_t* dp=new_demux_packet(len); |
75 stream_read(stream,dp->buffer,len); | |
76 dp->pts=pts; //(float)pts/90000.0f; | |
77 dp->pos=pos; | |
979 | 78 dp->flags=flags; |
1 | 79 // append packet to DS stream: |
80 ds_add_packet(ds,dp); | |
81 } | |
82 | |
83 // return value: | |
84 // 0 = EOF or no stream found or invalid type | |
85 // 1 = successfully read a packet | |
86 int demux_mpg_es_fill_buffer(demuxer_t *demux); | |
87 int demux_mpg_fill_buffer(demuxer_t *demux); | |
88 int demux_avi_fill_buffer(demuxer_t *demux); | |
89 int demux_avi_fill_buffer_ni(demuxer_t *demux,demux_stream_t *ds); | |
90 int demux_avi_fill_buffer_nini(demuxer_t *demux,demux_stream_t *ds); | |
91 int demux_asf_fill_buffer(demuxer_t *demux); | |
92 | |
93 int demux_fill_buffer(demuxer_t *demux,demux_stream_t *ds){ | |
94 // Note: parameter 'ds' can be NULL! | |
95 switch(demux->type){ | |
96 case DEMUXER_TYPE_MPEG_ES: return demux_mpg_es_fill_buffer(demux); | |
97 case DEMUXER_TYPE_MPEG_PS: return demux_mpg_fill_buffer(demux); | |
98 case DEMUXER_TYPE_AVI: return demux_avi_fill_buffer(demux); | |
99 case DEMUXER_TYPE_AVI_NI: return demux_avi_fill_buffer_ni(demux,ds); | |
100 case DEMUXER_TYPE_AVI_NINI: return demux_avi_fill_buffer_nini(demux,ds); | |
101 case DEMUXER_TYPE_ASF: return demux_asf_fill_buffer(demux); | |
102 } | |
103 return 0; | |
104 } | |
105 | |
106 // return value: | |
107 // 0 = EOF | |
108 // 1 = succesfull | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
109 int ds_fill_buffer(demux_stream_t *ds){ |
1 | 110 demuxer_t *demux=ds->demuxer; |
111 if(ds->buffer) free(ds->buffer); | |
112 if(verbose>2){ | |
113 if(ds==demux->audio) printf("ds_fill_buffer(d_audio) called\n");else | |
114 if(ds==demux->video) printf("ds_fill_buffer(d_video) called\n");else | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
115 printf("ds_fill_buffer(unknown 0x%X) called\n",(unsigned int)ds); |
1 | 116 } |
117 while(1){ | |
118 if(ds->packs){ | |
119 demux_packet_t *p=ds->first; | |
120 // copy useful data: | |
121 ds->buffer=p->buffer; | |
122 ds->buffer_pos=0; | |
123 ds->buffer_size=p->len; | |
124 ds->pos=p->pos; | |
125 ds->dpos+=p->len; // !!! | |
889 | 126 ++ds->pack_no; |
746 | 127 if(p->pts){ |
128 ds->pts=p->pts; | |
129 ds->pts_bytes=0; | |
130 } | |
131 ds->pts_bytes+=p->len; // !!! | |
979 | 132 ds->flags=p->flags; |
1 | 133 // free packet: |
134 ds->bytes-=p->len; | |
135 ds->first=p->next; | |
136 if(!ds->first) ds->last=NULL; | |
137 free(p); | |
138 --ds->packs; | |
139 return 1; //ds->buffer_size; | |
140 } | |
141 if(demux->audio->packs>=MAX_PACKS || demux->audio->bytes>=MAX_PACK_BYTES){ | |
142 printf("\nDEMUXER: Too many (%d in %d bytes) audio packets in the buffer!\n",demux->audio->packs,demux->audio->bytes); | |
143 printf("(maybe you play a non-interleaved stream/file or audio codec failed)\n"); | |
144 break; | |
145 } | |
146 if(demux->video->packs>=MAX_PACKS || demux->video->bytes>=MAX_PACK_BYTES){ | |
147 printf("\nDEMUXER: Too many (%d in %d bytes) video packets in the buffer!\n",demux->video->packs,demux->video->bytes); | |
148 printf("(maybe you play a non-interleaved stream/file or video codec failed)\n"); | |
149 break; | |
150 } | |
151 if(!demux_fill_buffer(demux,ds)) break; // EOF | |
152 } | |
153 ds->buffer_pos=ds->buffer_size=0; | |
154 ds->buffer=NULL; | |
155 if(verbose) printf("ds_fill_buffer: EOF reached (stream: %s) \n",ds==demux->audio?"audio":"video"); | |
156 ds->eof=1; | |
157 return 0; | |
158 } | |
159 | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
160 int demux_read_data(demux_stream_t *ds,unsigned char* mem,int len){ |
1 | 161 int x; |
162 int bytes=0; | |
163 while(len>0){ | |
164 x=ds->buffer_size-ds->buffer_pos; | |
165 if(x==0){ | |
166 if(!ds_fill_buffer(ds)) return bytes; | |
167 } else { | |
168 if(x>len) x=len; | |
169 if(mem) memcpy(mem+bytes,&ds->buffer[ds->buffer_pos],x); | |
170 bytes+=x;len-=x;ds->buffer_pos+=x; | |
171 } | |
172 } | |
173 return bytes; | |
174 } | |
175 | |
746 | 176 int demux_read_data_pack(demux_stream_t *ds,unsigned char* mem,int len){ |
177 int x; | |
178 int bytes=0; | |
179 while(len>0){ | |
180 x=ds->buffer_size-ds->buffer_pos; | |
181 if(x==0){ | |
182 if(!ds_fill_buffer(ds)) return bytes; | |
183 } else { | |
184 if(x>len) x=len; | |
185 if(mem) memcpy(mem+bytes,&ds->buffer[ds->buffer_pos],x); | |
186 bytes+=x;len-=x;ds->buffer_pos+=x; | |
187 return bytes; // stop at end of package! (for correct timestamping) | |
188 } | |
189 } | |
190 return bytes; | |
191 } | |
192 | |
193 | |
1 | 194 void ds_free_packs(demux_stream_t *ds){ |
195 demux_packet_t *dp=ds->first; | |
196 while(dp){ | |
197 demux_packet_t *dn=dp->next; | |
198 free(dp->buffer); | |
199 free(dp); | |
200 dp=dn; | |
201 } | |
202 if(ds->asf_packet){ | |
203 // free unfinished .asf fragments: | |
204 free(ds->asf_packet->buffer); | |
205 free(ds->asf_packet); | |
206 ds->asf_packet=NULL; | |
207 } | |
208 ds->first=ds->last=NULL; | |
209 ds->packs=0; // !!!!! | |
210 ds->bytes=0; | |
211 if(ds->buffer) free(ds->buffer); | |
212 ds->buffer=NULL; | |
213 ds->buffer_pos=ds->buffer_size; | |
828 | 214 ds->pts=0; ds->pts_bytes=0; |
1 | 215 } |
216 | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
217 int ds_get_packet(demux_stream_t *ds,unsigned char **start){ |
1 | 218 while(1){ |
219 int len; | |
220 if(ds->buffer_pos>=ds->buffer_size){ | |
221 if(!ds_fill_buffer(ds)){ | |
222 // EOF | |
223 *start = NULL; | |
224 return -1; | |
225 } | |
226 } | |
227 len=ds->buffer_size-ds->buffer_pos; | |
228 *start = &ds->buffer[ds->buffer_pos]; | |
229 ds->buffer_pos+=len; | |
230 return len; | |
231 } | |
232 } | |
554 | 233 |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
554
diff
changeset
|
234 int ds_get_packet_sub(demux_stream_t *ds,unsigned char **start){ |
554 | 235 while(1){ |
236 int len; | |
237 if(ds->buffer_pos>=ds->buffer_size){ | |
238 *start = NULL; | |
239 if(!ds->packs) return -1; // no sub | |
240 if(!ds_fill_buffer(ds)) return -1; // EOF | |
241 } | |
242 len=ds->buffer_size-ds->buffer_pos; | |
243 *start = &ds->buffer[ds->buffer_pos]; | |
244 ds->buffer_pos+=len; | |
245 return len; | |
246 } | |
247 } | |
248 |