1
|
1 // AVI file parser for DEMUXER v2.6 by A'rpi/ESP-team
|
|
2
|
|
3 //static float avi_pts_frametime=1.0f/25.0f;
|
|
4 static float avi_audio_pts=0;
|
|
5 static float avi_video_pts=0;
|
|
6
|
|
7 static int skip_video_frames=0;
|
|
8
|
|
9 static inline int avi_stream_id(unsigned int id){
|
|
10 unsigned char *p=(unsigned char *)&id;
|
|
11 unsigned char a,b;
|
|
12 a=p[0]-'0'; b=p[1]-'0';
|
|
13 if(a>9 || b>9) return 100; // invalid ID
|
|
14 return a*10+b;
|
|
15 }
|
|
16
|
|
17 // Select ds from ID
|
|
18 static inline demux_stream_t* demux_avi_select_stream(demuxer_t *demux,unsigned int id){
|
|
19 int stream_id=avi_stream_id(id);
|
426
|
20
|
|
21 // printf("demux_avi_select_stream(%d) {a:%d/v:%d}\n",stream_id,
|
|
22 // demux->audio->id,demux->video->id);
|
|
23
|
|
24 if(demux->video->id==-1)
|
|
25 if(avi_header.v_streams[stream_id])
|
|
26 demux->video->id=stream_id;
|
|
27
|
|
28 if(demux->audio->id==-1)
|
|
29 if(avi_header.a_streams[stream_id])
|
|
30 demux->audio->id=stream_id;
|
|
31
|
|
32 if(stream_id==demux->audio->id){
|
|
33 if(!demux->audio->sh){
|
|
34 demux->audio->sh=avi_header.a_streams[stream_id];
|
|
35 if(verbose) printf("Auto-selected AVI audio ID = %d\n",demux->audio->id);
|
|
36 }
|
|
37 return demux->audio;
|
|
38 }
|
|
39 if(stream_id==demux->video->id){
|
|
40 if(!demux->video->sh){
|
|
41 demux->video->sh=avi_header.v_streams[stream_id];
|
|
42 if(verbose) printf("Auto-selected AVI video ID = %d\n",demux->video->id);
|
|
43 }
|
|
44 return demux->video;
|
|
45 }
|
1
|
46 if(id!=mmioFOURCC('J','U','N','K')){
|
|
47 // unknown
|
|
48 if(verbose>=2) printf("Unknown chunk: %.4s (%X)\n",&id,id);
|
|
49 }
|
|
50 return NULL;
|
|
51 }
|
|
52
|
|
53 static int demux_avi_read_packet(demuxer_t *demux,unsigned int id,unsigned int len,int idxpos){
|
|
54 int skip;
|
|
55 float pts=0;
|
|
56 demux_stream_t *ds=demux_avi_select_stream(demux,id);
|
|
57
|
|
58 if(verbose>=3) printf("demux_avi.read_packet: %X\n",id);
|
|
59
|
|
60 if(ds==demux->audio){
|
|
61 pts=avi_audio_pts;
|
|
62 avi_audio_pts=0;
|
|
63 } else
|
|
64 if(ds==demux->video){
|
|
65 // video
|
|
66 if(skip_video_frames>0){
|
|
67 // drop frame (seeking)
|
|
68 --skip_video_frames;
|
|
69 ds=NULL;
|
|
70 } else {
|
|
71 pts=avi_video_pts;
|
|
72 }
|
|
73 // ezt a 2 sort lehet hogy fell kell majd cserelni:
|
|
74 //avi_video_pts+=avi_pts_frametime;
|
291
|
75 //avi_video_pts+=(float)avi_header.video.dwScale/(float)avi_header.video.dwRate;
|
|
76 //avi_video_pts+=((sh_video_t*)ds->sh)->frametime;
|
|
77 avi_video_pts+=(float)((sh_video_t*)(demux->video->sh))->video.dwScale/(float)((sh_video_t*)(demux->video->sh))->video.dwRate;
|
1
|
78 avi_audio_pts=avi_video_pts;
|
|
79 }
|
|
80
|
|
81 // len=stream_read_dword_le(demux->stream);
|
|
82 skip=(len+1)&(~1); // total bytes in this chunk
|
|
83
|
|
84 if(ds){
|
|
85 if(verbose>=2) printf("DEMUX_AVI: Read %d data bytes from packet %04X\n",len,id);
|
|
86 ds_read_packet(ds,demux->stream,len,pts,idxpos);
|
|
87 skip-=len;
|
|
88 }
|
|
89 if(skip){
|
|
90 if(verbose>=2) printf("DEMUX_AVI: Skipping %d bytes from packet %04X\n",skip,id);
|
|
91 stream_skip(demux->stream,skip);
|
|
92 }
|
|
93 return ds?1:0;
|
|
94 }
|
|
95
|
|
96 //static int num_elementary_packets100=0;
|
|
97 //static int num_elementary_packets101=0;
|
|
98
|
|
99 // return value:
|
|
100 // 0 = EOF or no stream found
|
|
101 // 1 = successfully read a packet
|
|
102 int demux_avi_fill_buffer(demuxer_t *demux){
|
|
103 unsigned int id=0;
|
|
104 unsigned int len;
|
|
105 int max_packs=128;
|
|
106 int ret=0;
|
|
107
|
|
108 do{
|
|
109 AVIINDEXENTRY *idx=NULL;
|
|
110 demux->filepos=stream_tell(demux->stream);
|
|
111 if(demux->filepos>=demux->endpos){
|
|
112 demux->stream->eof=1;
|
|
113 return 0;
|
|
114 }
|
|
115 if(stream_eof(demux->stream)) return 0;
|
|
116 if(avi_header.idx_size>0 && avi_header.idx_pos<avi_header.idx_size){
|
|
117 unsigned int pos;
|
|
118
|
|
119 //if(avi_header.idx_pos<0) printf("Fatal! idx_pos=%d\n",avi_header.idx_pos);
|
|
120
|
|
121 idx=&avi_header.idx[avi_header.idx_pos++];
|
|
122
|
|
123 //printf("[%d]",avi_header.idx_pos);fflush(stdout);
|
|
124
|
|
125 //stream_seek(demux->stream,idx.dwChunkOffset);
|
|
126 //printf("IDX pos=%X idx.pos=%X idx.size=%X idx.flags=%X\n",demux->filepos,
|
|
127 // pos-4,idx->dwChunkLength,idx->dwFlags);
|
|
128 if(idx->dwFlags&AVIIF_LIST){
|
|
129 // LIST
|
|
130 continue;
|
|
131 }
|
|
132 if(!demux_avi_select_stream(demux,idx->ckid)){
|
|
133 if(verbose>2) printf("Skip chunk %.4s (0x%X) \n",&idx->ckid,idx->ckid);
|
|
134 continue; // skip this chunk
|
|
135 }
|
|
136
|
|
137 pos=idx->dwChunkOffset+avi_header.idx_offset;
|
|
138 if(pos<avi_header.movi_start || pos>=avi_header.movi_end){
|
|
139 printf("ChunkOffset out of range! current=0x%X idx=0x%X \n",demux->filepos,pos);
|
|
140 continue;
|
|
141 }
|
|
142 #if 0
|
|
143 if(pos!=demux->filepos){
|
|
144 printf("Warning! pos=0x%X idx.pos=0x%X diff=%d \n",demux->filepos,pos,pos-demux->filepos);
|
|
145 }
|
|
146 #endif
|
|
147 stream_seek(demux->stream,pos);
|
|
148 id=stream_read_dword_le(demux->stream);
|
|
149 if(id!=idx->ckid){
|
|
150 printf("ChunkID mismatch! raw=%.4s idx=%.4s \n",&id,&idx->ckid);
|
|
151 continue;
|
|
152 }
|
|
153 len=stream_read_dword_le(demux->stream);
|
|
154 // if((len&(~1))!=(idx->dwChunkLength&(~1))){
|
|
155 // if((len)!=(idx->dwChunkLength)){
|
|
156 if((len!=idx->dwChunkLength)&&((len+1)!=idx->dwChunkLength)){
|
|
157 printf("ChunkSize mismatch! raw=%d idx=%d \n",len,idx->dwChunkLength);
|
|
158 continue;
|
|
159 }
|
|
160 } else {
|
|
161 id=stream_read_dword_le(demux->stream);
|
|
162 len=stream_read_dword_le(demux->stream);
|
|
163 if(id==mmioFOURCC('L','I','S','T')){
|
|
164 id=stream_read_dword_le(demux->stream); // list type
|
|
165 continue;
|
|
166 }
|
|
167 }
|
|
168 ret=demux_avi_read_packet(demux,id,len,avi_header.idx_pos-1);
|
|
169 if(!ret && skip_video_frames<=0)
|
|
170 if(--max_packs==0){
|
|
171 demux->stream->eof=1;
|
|
172 printf("demux: file doesn't contain the selected audio or video stream\n");
|
|
173 return 0;
|
|
174 }
|
|
175 } while(ret!=1);
|
|
176 return 1;
|
|
177 }
|
|
178
|
|
179
|
|
180 // return value:
|
|
181 // 0 = EOF or no stream found
|
|
182 // 1 = successfully read a packet
|
|
183 int demux_avi_fill_buffer_ni(demuxer_t *demux,demux_stream_t* ds){
|
|
184 unsigned int id=0;
|
|
185 unsigned int len;
|
|
186 int max_packs=128;
|
|
187 int ret=0;
|
|
188
|
|
189 do{
|
|
190 AVIINDEXENTRY *idx=NULL;
|
|
191 int idx_pos=0;
|
|
192 demux->filepos=stream_tell(demux->stream);
|
|
193
|
|
194 if(ds==demux->video) idx_pos=avi_header.idx_pos_a++; else
|
|
195 if(ds==demux->audio) idx_pos=avi_header.idx_pos_v++; else
|
|
196 idx_pos=avi_header.idx_pos++;
|
|
197
|
|
198 if(avi_header.idx_size>0 && idx_pos<avi_header.idx_size){
|
|
199 unsigned int pos;
|
|
200 idx=&avi_header.idx[idx_pos];
|
|
201
|
|
202 if(idx->dwFlags&AVIIF_LIST){
|
|
203 // LIST
|
|
204 continue;
|
|
205 }
|
|
206 if(ds && demux_avi_select_stream(demux,idx->ckid)!=ds){
|
|
207 if(verbose>2) printf("Skip chunk %.4s (0x%X) \n",&idx->ckid,idx->ckid);
|
|
208 continue; // skip this chunk
|
|
209 }
|
|
210
|
|
211 pos=idx->dwChunkOffset+avi_header.idx_offset;
|
|
212 if(pos<avi_header.movi_start || pos>=avi_header.movi_end){
|
|
213 printf("ChunkOffset out of range! current=0x%X idx=0x%X \n",demux->filepos,pos);
|
|
214 continue;
|
|
215 }
|
|
216 #if 0
|
|
217 if(pos!=demux->filepos){
|
|
218 printf("Warning! pos=0x%X idx.pos=0x%X diff=%d \n",demux->filepos,pos,pos-demux->filepos);
|
|
219 }
|
|
220 #endif
|
|
221 stream_seek(demux->stream,pos);
|
|
222
|
|
223 id=stream_read_dword_le(demux->stream);
|
|
224
|
|
225 if(stream_eof(demux->stream)) return 0;
|
|
226
|
|
227 if(id!=idx->ckid){
|
|
228 printf("ChunkID mismatch! raw=%.4s idx=%.4s \n",&id,&idx->ckid);
|
|
229 continue;
|
|
230 }
|
|
231 len=stream_read_dword_le(demux->stream);
|
|
232 // if((len&(~1))!=(idx->dwChunkLength&(~1))){
|
|
233 // if((len)!=(idx->dwChunkLength)){
|
|
234 if((len!=idx->dwChunkLength)&&((len+1)!=idx->dwChunkLength)){
|
|
235 printf("ChunkSize mismatch! raw=%d idx=%d \n",len,idx->dwChunkLength);
|
|
236 continue;
|
|
237 }
|
|
238 } else return 0;
|
|
239 ret=demux_avi_read_packet(demux,id,len,idx_pos);
|
|
240 if(!ret && skip_video_frames<=0)
|
|
241 if(--max_packs==0){
|
|
242 demux->stream->eof=1;
|
|
243 printf("demux: file doesn't contain the selected audio or video stream\n");
|
|
244 return 0;
|
|
245 }
|
|
246 } while(ret!=1);
|
|
247 return 1;
|
|
248 }
|
|
249
|
|
250
|
|
251 // return value:
|
|
252 // 0 = EOF or no stream found
|
|
253 // 1 = successfully read a packet
|
|
254 int demux_avi_fill_buffer_nini(demuxer_t *demux,demux_stream_t* ds){
|
|
255 unsigned int id=0;
|
|
256 unsigned int len;
|
|
257 int ret=0;
|
|
258 int *fpos=NULL;
|
|
259
|
|
260 if(ds==demux->video) fpos=&avi_header.idx_pos_a; else
|
|
261 if(ds==demux->audio) fpos=&avi_header.idx_pos_v; else
|
|
262 return 0;
|
|
263
|
|
264 stream_seek(demux->stream,fpos[0]);
|
|
265
|
|
266 do{
|
|
267
|
|
268 demux->filepos=stream_tell(demux->stream);
|
|
269 if(demux->filepos>=demux->endpos){
|
|
270 demux->stream->eof=1;
|
|
271 return 0;
|
|
272 }
|
|
273 if(stream_eof(demux->stream)) return 0;
|
|
274
|
|
275 id=stream_read_dword_le(demux->stream);
|
|
276 len=stream_read_dword_le(demux->stream);
|
|
277 if(id==mmioFOURCC('L','I','S','T')){
|
|
278 id=stream_read_dword_le(demux->stream); // list type
|
|
279 continue;
|
|
280 }
|
|
281
|
|
282 if(ds==demux_avi_select_stream(demux,id)){
|
|
283 // read it!
|
|
284 ret=demux_avi_read_packet(demux,id,len,avi_header.idx_pos-1);
|
|
285 } else {
|
|
286 // skip it!
|
|
287 int skip=(len+1)&(~1); // total bytes in this chunk
|
|
288 stream_skip(demux->stream,skip);
|
|
289 }
|
|
290
|
|
291 } while(ret!=1);
|
|
292 fpos[0]=stream_tell(demux->stream);
|
|
293 return 1;
|
|
294 }
|
|
295
|
|
296
|