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