Mercurial > mplayer.hg
annotate demux_avi.c @ 1481:db2a6ae7e296
AVI pts/seek fixes, VBR support, faster NI seek
author | arpi |
---|---|
date | Sat, 11 Aug 2001 00:53:45 +0000 |
parents | 8b7ede5849f4 |
children | b895f95e7657 |
rev | line source |
---|---|
1 | 1 // AVI file parser for DEMUXER v2.6 by A'rpi/ESP-team |
2 | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
3 #include <stdio.h> |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
4 #include <stdlib.h> |
1430 | 5 #include <unistd.h> |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
6 |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
7 extern int verbose; // defined in mplayer.c |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
8 |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
9 #include "stream.h" |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
10 #include "demuxer.h" |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
11 |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
12 #include "wine/mmreg.h" |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
13 #include "wine/avifmt.h" |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
14 #include "wine/vfw.h" |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
15 |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
16 #include "codec-cfg.h" |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
17 #include "stheader.h" |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
18 |
1 | 19 //static float avi_pts_frametime=1.0f/25.0f; |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
20 float avi_audio_pts=0; |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
21 float avi_video_pts=0; |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
22 //float avi_video_ftime=0.04; |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
23 int skip_video_frames=0; |
1 | 24 |
25 // Select ds from ID | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
26 demux_stream_t* demux_avi_select_stream(demuxer_t *demux,unsigned int id){ |
1 | 27 int stream_id=avi_stream_id(id); |
426 | 28 |
29 // printf("demux_avi_select_stream(%d) {a:%d/v:%d}\n",stream_id, | |
30 // demux->audio->id,demux->video->id); | |
31 | |
32 if(demux->video->id==-1) | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
33 if(demux->v_streams[stream_id]) |
426 | 34 demux->video->id=stream_id; |
35 | |
36 if(demux->audio->id==-1) | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
37 if(demux->a_streams[stream_id]) |
426 | 38 demux->audio->id=stream_id; |
39 | |
40 if(stream_id==demux->audio->id){ | |
41 if(!demux->audio->sh){ | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
42 demux->audio->sh=demux->a_streams[stream_id]; |
426 | 43 if(verbose) printf("Auto-selected AVI audio ID = %d\n",demux->audio->id); |
44 } | |
45 return demux->audio; | |
46 } | |
47 if(stream_id==demux->video->id){ | |
48 if(!demux->video->sh){ | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
49 demux->video->sh=demux->v_streams[stream_id]; |
426 | 50 if(verbose) printf("Auto-selected AVI video ID = %d\n",demux->video->id); |
51 } | |
52 return demux->video; | |
53 } | |
1 | 54 if(id!=mmioFOURCC('J','U','N','K')){ |
55 // unknown | |
600 | 56 if(verbose>=2) printf("Unknown chunk: %.4s (%X)\n",(char *) &id,id); |
1 | 57 } |
58 return NULL; | |
59 } | |
60 | |
1455 | 61 static float pts_correction=0.0; |
62 static int pts_corrected=0; | |
63 static int pts_has_video=0; | |
64 static unsigned int pts_corr_bytes=0; | |
65 | |
979 | 66 static int demux_avi_read_packet(demuxer_t *demux,unsigned int id,unsigned int len,int idxpos,int flags){ |
1 | 67 int skip; |
68 float pts=0; | |
69 demux_stream_t *ds=demux_avi_select_stream(demux,id); | |
70 | |
71 if(verbose>=3) printf("demux_avi.read_packet: %X\n",id); | |
72 | |
73 if(ds==demux->audio){ | |
1455 | 74 |
75 if(pts_corrected==0){ | |
76 // printf("\rYYY-A A: %5.3f V: %5.3f \n",avi_audio_pts,avi_video_pts); | |
77 if(pts_has_video){ | |
78 // we have video pts now | |
79 float delay=(float)pts_corr_bytes/((sh_audio_t*)(ds->sh))->wf->nAvgBytesPerSec; | |
80 printf("XXX initial v_pts=%5.3f a_pos=%d (%5.3f) \n",avi_audio_pts,pts_corr_bytes,delay); | |
81 //pts_correction=-avi_audio_pts+delay; | |
82 pts_correction=delay-avi_audio_pts; | |
1477 | 83 avi_audio_pts+=pts_correction; |
1455 | 84 pts_corrected=1; |
85 } else | |
86 pts_corr_bytes+=len; | |
87 } | |
1476 | 88 pts=avi_audio_pts; //+pts_correction; |
1 | 89 avi_audio_pts=0; |
90 } else | |
1409 | 91 if(ds==demux->video){ |
1 | 92 // video |
93 if(skip_video_frames>0){ | |
94 // drop frame (seeking) | |
95 --skip_video_frames; | |
96 ds=NULL; | |
97 } else { | |
98 pts=avi_video_pts; | |
99 } | |
100 // ezt a 2 sort lehet hogy fell kell majd cserelni: | |
101 //avi_video_pts+=avi_pts_frametime; | |
291 | 102 //avi_video_pts+=(float)avi_header.video.dwScale/(float)avi_header.video.dwRate; |
103 //avi_video_pts+=((sh_video_t*)ds->sh)->frametime; | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
104 // FIXME!!! |
1368 | 105 #if 1 |
106 // printf("ds=0x%X\n",ds); | |
107 // printf("packno=%d\n",ds->pack_no); | |
1409 | 108 // printf("### pack_no=%d\n",demux->video->pack_no+demux->video->packs); |
109 avi_video_pts = (demux->video->pack_no+demux->video->packs) * | |
1368 | 110 (float)((sh_video_t*)demux->video->sh)->video.dwScale / |
111 (float)((sh_video_t*)demux->video->sh)->video.dwRate; | |
112 #else | |
291 | 113 avi_video_pts+=(float)((sh_video_t*)(demux->video->sh))->video.dwScale/(float)((sh_video_t*)(demux->video->sh))->video.dwRate; |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
114 // avi_video_pts+=avi_video_ftime; |
1368 | 115 #endif |
1455 | 116 // printf("\rYYY-V A: %5.3f V: %5.3f \n",avi_audio_pts,avi_video_pts); |
1476 | 117 avi_audio_pts=avi_video_pts+pts_correction; |
1455 | 118 pts_has_video=1; |
119 | |
1 | 120 } |
121 | |
122 // len=stream_read_dword_le(demux->stream); | |
123 skip=(len+1)&(~1); // total bytes in this chunk | |
124 | |
125 if(ds){ | |
126 if(verbose>=2) printf("DEMUX_AVI: Read %d data bytes from packet %04X\n",len,id); | |
979 | 127 ds_read_packet(ds,demux->stream,len,pts,idxpos,flags); |
1 | 128 skip-=len; |
129 } | |
130 if(skip){ | |
131 if(verbose>=2) printf("DEMUX_AVI: Skipping %d bytes from packet %04X\n",skip,id); | |
132 stream_skip(demux->stream,skip); | |
133 } | |
134 return ds?1:0; | |
135 } | |
136 | |
137 //static int num_elementary_packets100=0; | |
138 //static int num_elementary_packets101=0; | |
139 | |
140 // return value: | |
141 // 0 = EOF or no stream found | |
142 // 1 = successfully read a packet | |
143 int demux_avi_fill_buffer(demuxer_t *demux){ | |
144 unsigned int id=0; | |
145 unsigned int len; | |
146 int max_packs=128; | |
147 int ret=0; | |
148 | |
149 do{ | |
979 | 150 int flags=0; |
1 | 151 AVIINDEXENTRY *idx=NULL; |
1421 | 152 #if 0 |
1 | 153 demux->filepos=stream_tell(demux->stream); |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
154 if(demux->filepos>=demux->movi_end){ |
1 | 155 demux->stream->eof=1; |
156 return 0; | |
157 } | |
158 if(stream_eof(demux->stream)) return 0; | |
1421 | 159 #endif |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
160 if(demux->idx_size>0 && demux->idx_pos<demux->idx_size){ |
1 | 161 unsigned int pos; |
162 | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
163 //if(demux->idx_pos<0) printf("Fatal! idx_pos=%d\n",demux->idx_pos); |
1 | 164 |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
165 idx=&((AVIINDEXENTRY *)demux->idx)[demux->idx_pos++]; |
1 | 166 |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
167 //printf("[%d]",demux->idx_pos);fflush(stdout); |
1 | 168 |
169 //stream_seek(demux->stream,idx.dwChunkOffset); | |
170 //printf("IDX pos=%X idx.pos=%X idx.size=%X idx.flags=%X\n",demux->filepos, | |
171 // pos-4,idx->dwChunkLength,idx->dwFlags); | |
172 if(idx->dwFlags&AVIIF_LIST){ | |
173 // LIST | |
174 continue; | |
175 } | |
176 if(!demux_avi_select_stream(demux,idx->ckid)){ | |
600 | 177 if(verbose>2) printf("Skip chunk %.4s (0x%X) \n",(char *)&idx->ckid,(unsigned int)idx->ckid); |
1 | 178 continue; // skip this chunk |
179 } | |
180 | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
181 pos=idx->dwChunkOffset+demux->idx_offset; |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
182 if(pos<demux->movi_start || pos>=demux->movi_end){ |
1421 | 183 printf("ChunkOffset out of range! idx=0x%X \n",pos); |
1 | 184 continue; |
185 } | |
186 #if 0 | |
187 if(pos!=demux->filepos){ | |
188 printf("Warning! pos=0x%X idx.pos=0x%X diff=%d \n",demux->filepos,pos,pos-demux->filepos); | |
189 } | |
190 #endif | |
191 stream_seek(demux->stream,pos); | |
1421 | 192 demux->filepos=stream_tell(demux->stream); |
1 | 193 id=stream_read_dword_le(demux->stream); |
1421 | 194 if(stream_eof(demux->stream)) return 0; // EOF! |
195 | |
1 | 196 if(id!=idx->ckid){ |
1464 | 197 if(verbose) printf("ChunkID mismatch! raw=%.4s idx=%.4s \n",(char *)&id,(char *)&idx->ckid); |
198 id=idx->ckid; | |
199 // continue; | |
1 | 200 } |
201 len=stream_read_dword_le(demux->stream); | |
202 // if((len&(~1))!=(idx->dwChunkLength&(~1))){ | |
203 // if((len)!=(idx->dwChunkLength)){ | |
204 if((len!=idx->dwChunkLength)&&((len+1)!=idx->dwChunkLength)){ | |
1464 | 205 if(verbose) printf("ChunkSize mismatch! raw=%d idx=%ld \n",len,idx->dwChunkLength); |
206 len=idx->dwChunkLength; | |
207 // continue; | |
1 | 208 } |
979 | 209 if(idx->dwFlags&AVIIF_KEYFRAME) flags=1; |
1 | 210 } else { |
1421 | 211 demux->filepos=stream_tell(demux->stream); |
212 if(demux->filepos>=demux->movi_end){ | |
213 demux->stream->eof=1; | |
214 return 0; | |
215 } | |
1 | 216 id=stream_read_dword_le(demux->stream); |
217 len=stream_read_dword_le(demux->stream); | |
1421 | 218 if(stream_eof(demux->stream)) return 0; // EOF! |
219 | |
1 | 220 if(id==mmioFOURCC('L','I','S','T')){ |
221 id=stream_read_dword_le(demux->stream); // list type | |
222 continue; | |
223 } | |
224 } | |
979 | 225 ret=demux_avi_read_packet(demux,id,len,demux->idx_pos-1,flags); |
1 | 226 if(!ret && skip_video_frames<=0) |
227 if(--max_packs==0){ | |
228 demux->stream->eof=1; | |
229 printf("demux: file doesn't contain the selected audio or video stream\n"); | |
230 return 0; | |
231 } | |
232 } while(ret!=1); | |
233 return 1; | |
234 } | |
235 | |
236 | |
237 // return value: | |
238 // 0 = EOF or no stream found | |
239 // 1 = successfully read a packet | |
240 int demux_avi_fill_buffer_ni(demuxer_t *demux,demux_stream_t* ds){ | |
241 unsigned int id=0; | |
242 unsigned int len; | |
243 int max_packs=128; | |
244 int ret=0; | |
245 | |
246 do{ | |
979 | 247 int flags=0; |
1 | 248 AVIINDEXENTRY *idx=NULL; |
249 int idx_pos=0; | |
250 demux->filepos=stream_tell(demux->stream); | |
251 | |
1481 | 252 if(ds==demux->video) idx_pos=demux->idx_pos_v++; else |
253 if(ds==demux->audio) idx_pos=demux->idx_pos_a++; else | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
254 idx_pos=demux->idx_pos++; |
1 | 255 |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
256 if(demux->idx_size>0 && idx_pos<demux->idx_size){ |
1 | 257 unsigned int pos; |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
258 idx=&((AVIINDEXENTRY *)demux->idx)[idx_pos]; |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
259 // idx=&demux->idx[idx_pos]; |
1 | 260 |
261 if(idx->dwFlags&AVIIF_LIST){ | |
262 // LIST | |
263 continue; | |
264 } | |
265 if(ds && demux_avi_select_stream(demux,idx->ckid)!=ds){ | |
600 | 266 if(verbose>2) printf("Skip chunk %.4s (0x%X) \n",(char *)&idx->ckid,(unsigned int)idx->ckid); |
1 | 267 continue; // skip this chunk |
268 } | |
269 | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
270 pos=idx->dwChunkOffset+demux->idx_offset; |
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
271 if(pos<demux->movi_start || pos>=demux->movi_end){ |
1 | 272 printf("ChunkOffset out of range! current=0x%X idx=0x%X \n",demux->filepos,pos); |
273 continue; | |
274 } | |
275 #if 0 | |
276 if(pos!=demux->filepos){ | |
277 printf("Warning! pos=0x%X idx.pos=0x%X diff=%d \n",demux->filepos,pos,pos-demux->filepos); | |
278 } | |
279 #endif | |
280 stream_seek(demux->stream,pos); | |
281 | |
282 id=stream_read_dword_le(demux->stream); | |
283 | |
284 if(stream_eof(demux->stream)) return 0; | |
285 | |
286 if(id!=idx->ckid){ | |
1464 | 287 if(verbose) printf("ChunkID mismatch! raw=%.4s idx=%.4s \n",(char *)&id,(char *)&idx->ckid); |
288 id=idx->ckid; | |
289 // continue; | |
1 | 290 } |
291 len=stream_read_dword_le(demux->stream); | |
292 // if((len&(~1))!=(idx->dwChunkLength&(~1))){ | |
293 // if((len)!=(idx->dwChunkLength)){ | |
294 if((len!=idx->dwChunkLength)&&((len+1)!=idx->dwChunkLength)){ | |
1464 | 295 if(verbose) printf("ChunkSize mismatch! raw=%d idx=%ld \n",len,idx->dwChunkLength); |
296 len=idx->dwChunkLength; | |
297 // continue; | |
1 | 298 } |
979 | 299 if(idx->dwFlags&AVIIF_KEYFRAME) flags=1; |
1 | 300 } else return 0; |
979 | 301 ret=demux_avi_read_packet(demux,id,len,idx_pos,flags); |
1 | 302 if(!ret && skip_video_frames<=0) |
303 if(--max_packs==0){ | |
304 demux->stream->eof=1; | |
305 printf("demux: file doesn't contain the selected audio or video stream\n"); | |
306 return 0; | |
307 } | |
308 } while(ret!=1); | |
309 return 1; | |
310 } | |
311 | |
312 | |
313 // return value: | |
314 // 0 = EOF or no stream found | |
315 // 1 = successfully read a packet | |
316 int demux_avi_fill_buffer_nini(demuxer_t *demux,demux_stream_t* ds){ | |
317 unsigned int id=0; | |
318 unsigned int len; | |
319 int ret=0; | |
320 int *fpos=NULL; | |
321 | |
1481 | 322 if(ds==demux->video) fpos=&demux->idx_pos_v; else |
323 if(ds==demux->audio) fpos=&demux->idx_pos_a; else | |
1 | 324 return 0; |
325 | |
326 stream_seek(demux->stream,fpos[0]); | |
327 | |
328 do{ | |
329 | |
330 demux->filepos=stream_tell(demux->stream); | |
587
8511095c5283
stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents:
426
diff
changeset
|
331 if(demux->filepos>=demux->movi_end){ |
1 | 332 demux->stream->eof=1; |
333 return 0; | |
334 } | |
335 if(stream_eof(demux->stream)) return 0; | |
336 | |
337 id=stream_read_dword_le(demux->stream); | |
338 len=stream_read_dword_le(demux->stream); | |
339 if(id==mmioFOURCC('L','I','S','T')){ | |
340 id=stream_read_dword_le(demux->stream); // list type | |
341 continue; | |
342 } | |
343 | |
344 if(ds==demux_avi_select_stream(demux,id)){ | |
345 // read it! | |
979 | 346 ret=demux_avi_read_packet(demux,id,len,demux->idx_pos-1,0); |
1 | 347 } else { |
348 // skip it! | |
349 int skip=(len+1)&(~1); // total bytes in this chunk | |
350 stream_skip(demux->stream,skip); | |
351 } | |
352 | |
353 } while(ret!=1); | |
354 fpos[0]=stream_tell(demux->stream); | |
355 return 1; | |
356 } | |
357 | |
1466 | 358 extern float initial_pts_delay; |
1 | 359 |
1466 | 360 void demux_seek_avi(demuxer_t *demuxer,float rel_seek_secs,int flags){ |
361 demux_stream_t *d_audio=demuxer->audio; | |
362 demux_stream_t *d_video=demuxer->video; | |
363 sh_audio_t *sh_audio=d_audio->sh; | |
364 sh_video_t *sh_video=d_video->sh; | |
365 float skip_audio_secs=0; | |
366 | |
367 //FIXME: OFF_T - Didn't check AVI case yet (avi files can't be >2G anyway?) | |
368 //================= seek in AVI ========================== | |
369 int rel_seek_frames=rel_seek_secs*sh_video->fps; | |
370 int video_chunk_pos=d_video->pos; | |
371 int i; | |
372 | |
373 skip_video_frames=0; | |
374 avi_audio_pts=0; | |
375 | |
1481 | 376 // ------------ STEP 1: find nearest video keyframe chunk ------------ |
1466 | 377 // find nearest video keyframe chunk pos: |
378 if(rel_seek_frames>0){ | |
379 // seek forward | |
380 while(video_chunk_pos<demuxer->idx_size){ | |
381 int id=((AVIINDEXENTRY *)demuxer->idx)[video_chunk_pos].ckid; | |
382 if(avi_stream_id(id)==d_video->id){ // video frame | |
383 if((--rel_seek_frames)<0 && ((AVIINDEXENTRY *)demuxer->idx)[video_chunk_pos].dwFlags&AVIIF_KEYFRAME) break; | |
384 } | |
385 ++video_chunk_pos; | |
386 } | |
387 } else { | |
388 // seek backward | |
389 while(video_chunk_pos>=0){ | |
390 int id=((AVIINDEXENTRY *)demuxer->idx)[video_chunk_pos].ckid; | |
391 if(avi_stream_id(id)==d_video->id){ // video frame | |
392 if((++rel_seek_frames)>0 && ((AVIINDEXENTRY *)demuxer->idx)[video_chunk_pos].dwFlags&AVIIF_KEYFRAME) break; | |
393 } | |
394 --video_chunk_pos; | |
395 } | |
396 } | |
397 demuxer->idx_pos_a=demuxer->idx_pos_v=demuxer->idx_pos=video_chunk_pos; | |
398 | |
399 // re-calc video pts: | |
400 d_video->pack_no=0; | |
401 for(i=0;i<video_chunk_pos;i++){ | |
402 int id=((AVIINDEXENTRY *)demuxer->idx)[i].ckid; | |
403 if(avi_stream_id(id)==d_video->id) ++d_video->pack_no; | |
404 } | |
405 sh_video->num_frames=d_video->pack_no; | |
406 avi_video_pts=d_video->pack_no*(float)sh_video->video.dwScale/(float)sh_video->video.dwRate; | |
1481 | 407 d_video->pos=video_chunk_pos; |
408 | |
409 // ------------ STEP 2: seek audio, find the right chunk & pos ------------ | |
410 | |
411 d_audio->pack_no=0; | |
412 d_audio->dpos=0; | |
1466 | 413 |
414 if(sh_audio){ | |
415 int i; | |
1481 | 416 // int apos=0; |
1466 | 417 int last=0; |
418 int len=0; | |
419 int skip_audio_bytes=0; | |
1481 | 420 int curr_audio_pos=-1; |
421 int audio_chunk_pos=-1; | |
422 int chunk_max=(demuxer->type==DEMUXER_TYPE_AVI)?video_chunk_pos:demuxer->idx_size; | |
423 | |
424 if(sh_audio->audio.dwSampleSize){ | |
425 // constant rate audio stream | |
426 #if 0 | |
427 int align; | |
428 curr_audio_pos=(avi_video_pts) * sh_audio->wf->nAvgBytesPerSec; | |
429 if(curr_audio_pos<0)curr_audio_pos=0; | |
430 align=sh_audio->audio.dwSampleSize; | |
431 if(sh_audio->wf->nBlockAlign>align) align=sh_audio->wf->nBlockAlign; | |
432 curr_audio_pos/=align; | |
433 curr_audio_pos*=align; | |
1466 | 434 #else |
1481 | 435 curr_audio_pos=(avi_video_pts)*(float)sh_audio->audio.dwRate/(float)sh_audio->audio.dwScale; |
436 curr_audio_pos*=sh_audio->audio.dwSampleSize; | |
1466 | 437 #endif |
438 | |
439 // find audio chunk pos: | |
1481 | 440 for(i=0;i<chunk_max;i++){ |
1466 | 441 int id=((AVIINDEXENTRY *)demuxer->idx)[i].ckid; |
442 if(avi_stream_id(id)==d_audio->id){ | |
443 len=((AVIINDEXENTRY *)demuxer->idx)[i].dwChunkLength; | |
1481 | 444 audio_chunk_pos=i; ++d_audio->pack_no; |
445 if(d_audio->dpos<=curr_audio_pos && curr_audio_pos<(d_audio->dpos+len)){ | |
446 //if(verbose)printf("break;\n"); | |
1466 | 447 break; |
448 } | |
1481 | 449 d_audio->dpos+=len; |
1466 | 450 } |
451 } | |
1481 | 452 skip_audio_bytes=curr_audio_pos-d_audio->dpos; |
1466 | 453 |
1481 | 454 } else { |
455 // VBR audio | |
456 int chunks=(avi_video_pts)*(float)sh_audio->audio.dwRate/(float)sh_audio->audio.dwScale; | |
457 audio_chunk_pos=0; | |
458 | |
459 // find audio chunk pos: | |
460 for(i=0;i<demuxer->idx_size && chunks>0;i++){ | |
461 int id=((AVIINDEXENTRY *)demuxer->idx)[i].ckid; | |
462 if(avi_stream_id(id)==d_audio->id){ | |
463 len=((AVIINDEXENTRY *)demuxer->idx)[i].dwChunkLength; | |
464 if(i>chunk_max){ | |
465 skip_audio_bytes+=len; | |
466 } else { | |
467 ++d_audio->pack_no; | |
468 d_audio->dpos+=len; | |
469 audio_chunk_pos=i; | |
470 } | |
471 --chunks; | |
472 } | |
473 } | |
474 //if(audio_chunk_pos>chunk_max) audio_chunk_pos=chunk_max; | |
475 | |
476 // printf("VBR seek: %5.3f -> chunk_no %d -> chunk_idx %d + skip %d \n", | |
477 // avi_video_pts, audio_chunk_pos, ); | |
478 | |
479 } | |
480 | |
481 // Now we have: | |
482 // audio_chunk_pos = chunk no in index table (it's <=chunk_max) | |
483 // skip_audio_bytes = bytes to be skipped after chunk seek | |
484 // d-audio->pack_no = chunk_no in stream at audio_chunk_pos | |
485 // d_audio->dpos = bytepos in stream at audio_chunk_pos | |
486 // let's seek! | |
487 | |
1466 | 488 // update stream position: |
489 d_audio->pos=audio_chunk_pos; | |
1481 | 490 // d_audio->dpos=apos; |
491 // d_audio->pts=initial_pts_delay+(float)apos/(float)sh_audio->wf->nAvgBytesPerSec; | |
492 | |
493 if(demuxer->type==DEMUXER_TYPE_AVI){ | |
494 // interleaved stream: | |
495 if(audio_chunk_pos<video_chunk_pos){ | |
496 // calc skip_video_frames & adjust video pts counter: | |
497 for(i=audio_chunk_pos;i<video_chunk_pos;i++){ | |
498 int id=((AVIINDEXENTRY *)demuxer->idx)[i].ckid; | |
499 if(avi_stream_id(id)==d_video->id) ++skip_video_frames; | |
500 } | |
501 // requires for correct audio pts calculation (demuxer): | |
502 avi_video_pts-=skip_video_frames*(float)sh_video->video.dwScale/(float)sh_video->video.dwRate; | |
503 demuxer->idx_pos_a=demuxer->idx_pos_v=demuxer->idx_pos=audio_chunk_pos; | |
504 } | |
505 } else { | |
506 // non-interleaved stream: | |
507 demuxer->idx_pos_a=audio_chunk_pos; | |
508 demuxer->idx_pos_v=video_chunk_pos; | |
509 demuxer->idx_pos=(audio_chunk_pos<video_chunk_pos)?audio_chunk_pos:video_chunk_pos; | |
510 } | |
1466 | 511 |
512 if(verbose) printf("SEEK: idx=%d (a:%d v:%d) v.skip=%d a.skip=%d/%4.3f \n", | |
513 demuxer->idx_pos,audio_chunk_pos,video_chunk_pos, | |
514 skip_video_frames,skip_audio_bytes,skip_audio_secs); | |
515 | |
516 if(skip_audio_bytes){ | |
517 demux_read_data(d_audio,NULL,skip_audio_bytes); | |
518 //d_audio->pts=0; // PTS is outdated because of the raw data skipping | |
519 } | |
520 resync_audio_stream(sh_audio); | |
521 | |
1481 | 522 // sh_audio->timer=-skip_audio_secs; |
1466 | 523 |
524 } | |
525 | |
526 | |
527 } | |
528 | |
529 | |
530 |