annotate demux_avi.c @ 1479:fc0fece9e83d

*** empty log message ***
author gabucino
date Fri, 10 Aug 2001 16:21:46 +0000
parents 8b7ede5849f4
children db2a6ae7e296
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
1 // AVI file parser for DEMUXER v2.6 by A'rpi/ESP-team
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
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
1728d249c783 missing unistd.h (requires for off_t under freebsd)
arpi
parents: 1421
diff changeset
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
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
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
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
24
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
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
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
27 int stream_id=avi_stream_id(id);
426
26e513f392b2 new stream selection code
arpi_esp
parents: 291
diff changeset
28
26e513f392b2 new stream selection code
arpi_esp
parents: 291
diff changeset
29 // printf("demux_avi_select_stream(%d) {a:%d/v:%d}\n",stream_id,
26e513f392b2 new stream selection code
arpi_esp
parents: 291
diff changeset
30 // demux->audio->id,demux->video->id);
26e513f392b2 new stream selection code
arpi_esp
parents: 291
diff changeset
31
26e513f392b2 new stream selection code
arpi_esp
parents: 291
diff changeset
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
26e513f392b2 new stream selection code
arpi_esp
parents: 291
diff changeset
34 demux->video->id=stream_id;
26e513f392b2 new stream selection code
arpi_esp
parents: 291
diff changeset
35
26e513f392b2 new stream selection code
arpi_esp
parents: 291
diff changeset
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
26e513f392b2 new stream selection code
arpi_esp
parents: 291
diff changeset
38 demux->audio->id=stream_id;
26e513f392b2 new stream selection code
arpi_esp
parents: 291
diff changeset
39
26e513f392b2 new stream selection code
arpi_esp
parents: 291
diff changeset
40 if(stream_id==demux->audio->id){
26e513f392b2 new stream selection code
arpi_esp
parents: 291
diff changeset
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
26e513f392b2 new stream selection code
arpi_esp
parents: 291
diff changeset
43 if(verbose) printf("Auto-selected AVI audio ID = %d\n",demux->audio->id);
26e513f392b2 new stream selection code
arpi_esp
parents: 291
diff changeset
44 }
26e513f392b2 new stream selection code
arpi_esp
parents: 291
diff changeset
45 return demux->audio;
26e513f392b2 new stream selection code
arpi_esp
parents: 291
diff changeset
46 }
26e513f392b2 new stream selection code
arpi_esp
parents: 291
diff changeset
47 if(stream_id==demux->video->id){
26e513f392b2 new stream selection code
arpi_esp
parents: 291
diff changeset
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
26e513f392b2 new stream selection code
arpi_esp
parents: 291
diff changeset
50 if(verbose) printf("Auto-selected AVI video ID = %d\n",demux->video->id);
26e513f392b2 new stream selection code
arpi_esp
parents: 291
diff changeset
51 }
26e513f392b2 new stream selection code
arpi_esp
parents: 291
diff changeset
52 return demux->video;
26e513f392b2 new stream selection code
arpi_esp
parents: 291
diff changeset
53 }
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
54 if(id!=mmioFOURCC('J','U','N','K')){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
55 // unknown
600
ef932c18cb14 some warnings have gone
szabii
parents: 587
diff changeset
56 if(verbose>=2) printf("Unknown chunk: %.4s (%X)\n",(char *) &id,id);
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
57 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
58 return NULL;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
59 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
60
1455
b2f3962d0e0e interleaved AVI: audio preload PTS compensation
arpi
parents: 1430
diff changeset
61 static float pts_correction=0.0;
b2f3962d0e0e interleaved AVI: audio preload PTS compensation
arpi
parents: 1430
diff changeset
62 static int pts_corrected=0;
b2f3962d0e0e interleaved AVI: audio preload PTS compensation
arpi
parents: 1430
diff changeset
63 static int pts_has_video=0;
b2f3962d0e0e interleaved AVI: audio preload PTS compensation
arpi
parents: 1430
diff changeset
64 static unsigned int pts_corr_bytes=0;
b2f3962d0e0e interleaved AVI: audio preload PTS compensation
arpi
parents: 1430
diff changeset
65
979
3bcc435cd5a2 keyframe flag added
arpi_esp
parents: 600
diff changeset
66 static int demux_avi_read_packet(demuxer_t *demux,unsigned int id,unsigned int len,int idxpos,int flags){
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
67 int skip;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
68 float pts=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
69 demux_stream_t *ds=demux_avi_select_stream(demux,id);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
70
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
71 if(verbose>=3) printf("demux_avi.read_packet: %X\n",id);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
72
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
73 if(ds==demux->audio){
1455
b2f3962d0e0e interleaved AVI: audio preload PTS compensation
arpi
parents: 1430
diff changeset
74
b2f3962d0e0e interleaved AVI: audio preload PTS compensation
arpi
parents: 1430
diff changeset
75 if(pts_corrected==0){
b2f3962d0e0e interleaved AVI: audio preload PTS compensation
arpi
parents: 1430
diff changeset
76 // printf("\rYYY-A A: %5.3f V: %5.3f \n",avi_audio_pts,avi_video_pts);
b2f3962d0e0e interleaved AVI: audio preload PTS compensation
arpi
parents: 1430
diff changeset
77 if(pts_has_video){
b2f3962d0e0e interleaved AVI: audio preload PTS compensation
arpi
parents: 1430
diff changeset
78 // we have video pts now
b2f3962d0e0e interleaved AVI: audio preload PTS compensation
arpi
parents: 1430
diff changeset
79 float delay=(float)pts_corr_bytes/((sh_audio_t*)(ds->sh))->wf->nAvgBytesPerSec;
b2f3962d0e0e interleaved AVI: audio preload PTS compensation
arpi
parents: 1430
diff changeset
80 printf("XXX initial v_pts=%5.3f a_pos=%d (%5.3f) \n",avi_audio_pts,pts_corr_bytes,delay);
b2f3962d0e0e interleaved AVI: audio preload PTS compensation
arpi
parents: 1430
diff changeset
81 //pts_correction=-avi_audio_pts+delay;
b2f3962d0e0e interleaved AVI: audio preload PTS compensation
arpi
parents: 1430
diff changeset
82 pts_correction=delay-avi_audio_pts;
1477
8b7ede5849f4 ehh. yet anouther silly a_pts bug
arpi
parents: 1476
diff changeset
83 avi_audio_pts+=pts_correction;
1455
b2f3962d0e0e interleaved AVI: audio preload PTS compensation
arpi
parents: 1430
diff changeset
84 pts_corrected=1;
b2f3962d0e0e interleaved AVI: audio preload PTS compensation
arpi
parents: 1430
diff changeset
85 } else
b2f3962d0e0e interleaved AVI: audio preload PTS compensation
arpi
parents: 1430
diff changeset
86 pts_corr_bytes+=len;
b2f3962d0e0e interleaved AVI: audio preload PTS compensation
arpi
parents: 1430
diff changeset
87 }
1476
e17940b2cbb2 silly a_pts bug
arpi
parents: 1466
diff changeset
88 pts=avi_audio_pts; //+pts_correction;
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
89 avi_audio_pts=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
90 } else
1409
65058a94df3b video pts calculation fixed (Desperado-trailer.avi)
arpi
parents: 1368
diff changeset
91 if(ds==demux->video){
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
92 // video
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
93 if(skip_video_frames>0){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
94 // drop frame (seeking)
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
95 --skip_video_frames;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
96 ds=NULL;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
97 } else {
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
98 pts=avi_video_pts;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
99 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
100 // ezt a 2 sort lehet hogy fell kell majd cserelni:
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
101 //avi_video_pts+=avi_pts_frametime;
291
da98e96499bb sh_audio/sh_video added, general codec cleanup
arpi_esp
parents: 109
diff changeset
102 //avi_video_pts+=(float)avi_header.video.dwScale/(float)avi_header.video.dwRate;
da98e96499bb sh_audio/sh_video added, general codec cleanup
arpi_esp
parents: 109
diff changeset
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
faee296c7c9a video pts calculation precision fixed
arpi
parents: 979
diff changeset
105 #if 1
faee296c7c9a video pts calculation precision fixed
arpi
parents: 979
diff changeset
106 // printf("ds=0x%X\n",ds);
faee296c7c9a video pts calculation precision fixed
arpi
parents: 979
diff changeset
107 // printf("packno=%d\n",ds->pack_no);
1409
65058a94df3b video pts calculation fixed (Desperado-trailer.avi)
arpi
parents: 1368
diff changeset
108 // printf("### pack_no=%d\n",demux->video->pack_no+demux->video->packs);
65058a94df3b video pts calculation fixed (Desperado-trailer.avi)
arpi
parents: 1368
diff changeset
109 avi_video_pts = (demux->video->pack_no+demux->video->packs) *
1368
faee296c7c9a video pts calculation precision fixed
arpi
parents: 979
diff changeset
110 (float)((sh_video_t*)demux->video->sh)->video.dwScale /
faee296c7c9a video pts calculation precision fixed
arpi
parents: 979
diff changeset
111 (float)((sh_video_t*)demux->video->sh)->video.dwRate;
faee296c7c9a video pts calculation precision fixed
arpi
parents: 979
diff changeset
112 #else
291
da98e96499bb sh_audio/sh_video added, general codec cleanup
arpi_esp
parents: 109
diff changeset
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
faee296c7c9a video pts calculation precision fixed
arpi
parents: 979
diff changeset
115 #endif
1455
b2f3962d0e0e interleaved AVI: audio preload PTS compensation
arpi
parents: 1430
diff changeset
116 // printf("\rYYY-V A: %5.3f V: %5.3f \n",avi_audio_pts,avi_video_pts);
1476
e17940b2cbb2 silly a_pts bug
arpi
parents: 1466
diff changeset
117 avi_audio_pts=avi_video_pts+pts_correction;
1455
b2f3962d0e0e interleaved AVI: audio preload PTS compensation
arpi
parents: 1430
diff changeset
118 pts_has_video=1;
b2f3962d0e0e interleaved AVI: audio preload PTS compensation
arpi
parents: 1430
diff changeset
119
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
120 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
121
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
122 // len=stream_read_dword_le(demux->stream);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
123 skip=(len+1)&(~1); // total bytes in this chunk
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
124
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
125 if(ds){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
126 if(verbose>=2) printf("DEMUX_AVI: Read %d data bytes from packet %04X\n",len,id);
979
3bcc435cd5a2 keyframe flag added
arpi_esp
parents: 600
diff changeset
127 ds_read_packet(ds,demux->stream,len,pts,idxpos,flags);
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
128 skip-=len;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
129 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
130 if(skip){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
131 if(verbose>=2) printf("DEMUX_AVI: Skipping %d bytes from packet %04X\n",skip,id);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
132 stream_skip(demux->stream,skip);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
133 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
134 return ds?1:0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
135 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
136
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
137 //static int num_elementary_packets100=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
138 //static int num_elementary_packets101=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
139
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
140 // return value:
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
141 // 0 = EOF or no stream found
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
142 // 1 = successfully read a packet
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
143 int demux_avi_fill_buffer(demuxer_t *demux){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
144 unsigned int id=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
145 unsigned int len;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
146 int max_packs=128;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
147 int ret=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
148
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
149 do{
979
3bcc435cd5a2 keyframe flag added
arpi_esp
parents: 600
diff changeset
150 int flags=0;
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
151 AVIINDEXENTRY *idx=NULL;
1421
ea0a0b9e6fbc nosound avi seeking fixed
arpi
parents: 1409
diff changeset
152 #if 0
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
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
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
155 demux->stream->eof=1;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
156 return 0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
157 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
158 if(stream_eof(demux->stream)) return 0;
1421
ea0a0b9e6fbc nosound avi seeking fixed
arpi
parents: 1409
diff changeset
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
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
161 unsigned int pos;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
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
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
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
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
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
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
168
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
169 //stream_seek(demux->stream,idx.dwChunkOffset);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
170 //printf("IDX pos=%X idx.pos=%X idx.size=%X idx.flags=%X\n",demux->filepos,
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
171 // pos-4,idx->dwChunkLength,idx->dwFlags);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
172 if(idx->dwFlags&AVIIF_LIST){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
173 // LIST
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
174 continue;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
175 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
176 if(!demux_avi_select_stream(demux,idx->ckid)){
600
ef932c18cb14 some warnings have gone
szabii
parents: 587
diff changeset
177 if(verbose>2) printf("Skip chunk %.4s (0x%X) \n",(char *)&idx->ckid,(unsigned int)idx->ckid);
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
178 continue; // skip this chunk
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
179 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
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
ea0a0b9e6fbc nosound avi seeking fixed
arpi
parents: 1409
diff changeset
183 printf("ChunkOffset out of range! idx=0x%X \n",pos);
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
184 continue;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
185 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
186 #if 0
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
187 if(pos!=demux->filepos){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
188 printf("Warning! pos=0x%X idx.pos=0x%X diff=%d \n",demux->filepos,pos,pos-demux->filepos);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
189 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
190 #endif
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
191 stream_seek(demux->stream,pos);
1421
ea0a0b9e6fbc nosound avi seeking fixed
arpi
parents: 1409
diff changeset
192 demux->filepos=stream_tell(demux->stream);
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
193 id=stream_read_dword_le(demux->stream);
1421
ea0a0b9e6fbc nosound avi seeking fixed
arpi
parents: 1409
diff changeset
194 if(stream_eof(demux->stream)) return 0; // EOF!
ea0a0b9e6fbc nosound avi seeking fixed
arpi
parents: 1409
diff changeset
195
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
196 if(id!=idx->ckid){
1464
891d8b59f2f1 changed chunkid/size check
arpi
parents: 1455
diff changeset
197 if(verbose) printf("ChunkID mismatch! raw=%.4s idx=%.4s \n",(char *)&id,(char *)&idx->ckid);
891d8b59f2f1 changed chunkid/size check
arpi
parents: 1455
diff changeset
198 id=idx->ckid;
891d8b59f2f1 changed chunkid/size check
arpi
parents: 1455
diff changeset
199 // continue;
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
200 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
201 len=stream_read_dword_le(demux->stream);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
202 // if((len&(~1))!=(idx->dwChunkLength&(~1))){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
203 // if((len)!=(idx->dwChunkLength)){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
204 if((len!=idx->dwChunkLength)&&((len+1)!=idx->dwChunkLength)){
1464
891d8b59f2f1 changed chunkid/size check
arpi
parents: 1455
diff changeset
205 if(verbose) printf("ChunkSize mismatch! raw=%d idx=%ld \n",len,idx->dwChunkLength);
891d8b59f2f1 changed chunkid/size check
arpi
parents: 1455
diff changeset
206 len=idx->dwChunkLength;
891d8b59f2f1 changed chunkid/size check
arpi
parents: 1455
diff changeset
207 // continue;
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
208 }
979
3bcc435cd5a2 keyframe flag added
arpi_esp
parents: 600
diff changeset
209 if(idx->dwFlags&AVIIF_KEYFRAME) flags=1;
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
210 } else {
1421
ea0a0b9e6fbc nosound avi seeking fixed
arpi
parents: 1409
diff changeset
211 demux->filepos=stream_tell(demux->stream);
ea0a0b9e6fbc nosound avi seeking fixed
arpi
parents: 1409
diff changeset
212 if(demux->filepos>=demux->movi_end){
ea0a0b9e6fbc nosound avi seeking fixed
arpi
parents: 1409
diff changeset
213 demux->stream->eof=1;
ea0a0b9e6fbc nosound avi seeking fixed
arpi
parents: 1409
diff changeset
214 return 0;
ea0a0b9e6fbc nosound avi seeking fixed
arpi
parents: 1409
diff changeset
215 }
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
216 id=stream_read_dword_le(demux->stream);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
217 len=stream_read_dword_le(demux->stream);
1421
ea0a0b9e6fbc nosound avi seeking fixed
arpi
parents: 1409
diff changeset
218 if(stream_eof(demux->stream)) return 0; // EOF!
ea0a0b9e6fbc nosound avi seeking fixed
arpi
parents: 1409
diff changeset
219
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
220 if(id==mmioFOURCC('L','I','S','T')){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
221 id=stream_read_dword_le(demux->stream); // list type
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
222 continue;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
223 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
224 }
979
3bcc435cd5a2 keyframe flag added
arpi_esp
parents: 600
diff changeset
225 ret=demux_avi_read_packet(demux,id,len,demux->idx_pos-1,flags);
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
226 if(!ret && skip_video_frames<=0)
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
227 if(--max_packs==0){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
228 demux->stream->eof=1;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
229 printf("demux: file doesn't contain the selected audio or video stream\n");
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
230 return 0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
231 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
232 } while(ret!=1);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
233 return 1;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
234 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
235
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
236
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
237 // return value:
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
238 // 0 = EOF or no stream found
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
239 // 1 = successfully read a packet
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
240 int demux_avi_fill_buffer_ni(demuxer_t *demux,demux_stream_t* ds){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
241 unsigned int id=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
242 unsigned int len;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
243 int max_packs=128;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
244 int ret=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
245
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
246 do{
979
3bcc435cd5a2 keyframe flag added
arpi_esp
parents: 600
diff changeset
247 int flags=0;
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
248 AVIINDEXENTRY *idx=NULL;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
249 int idx_pos=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
250 demux->filepos=stream_tell(demux->stream);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
251
587
8511095c5283 stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents: 426
diff changeset
252 if(ds==demux->video) idx_pos=demux->idx_pos_a++; else
8511095c5283 stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents: 426
diff changeset
253 if(ds==demux->audio) idx_pos=demux->idx_pos_v++; else
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
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
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
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
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
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
260
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
261 if(idx->dwFlags&AVIIF_LIST){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
262 // LIST
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
263 continue;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
264 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
265 if(ds && demux_avi_select_stream(demux,idx->ckid)!=ds){
600
ef932c18cb14 some warnings have gone
szabii
parents: 587
diff changeset
266 if(verbose>2) printf("Skip chunk %.4s (0x%X) \n",(char *)&idx->ckid,(unsigned int)idx->ckid);
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
267 continue; // skip this chunk
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
268 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
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
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
272 printf("ChunkOffset out of range! current=0x%X idx=0x%X \n",demux->filepos,pos);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
273 continue;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
274 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
275 #if 0
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
276 if(pos!=demux->filepos){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
277 printf("Warning! pos=0x%X idx.pos=0x%X diff=%d \n",demux->filepos,pos,pos-demux->filepos);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
278 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
279 #endif
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
280 stream_seek(demux->stream,pos);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
281
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
282 id=stream_read_dword_le(demux->stream);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
283
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
284 if(stream_eof(demux->stream)) return 0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
285
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
286 if(id!=idx->ckid){
1464
891d8b59f2f1 changed chunkid/size check
arpi
parents: 1455
diff changeset
287 if(verbose) printf("ChunkID mismatch! raw=%.4s idx=%.4s \n",(char *)&id,(char *)&idx->ckid);
891d8b59f2f1 changed chunkid/size check
arpi
parents: 1455
diff changeset
288 id=idx->ckid;
891d8b59f2f1 changed chunkid/size check
arpi
parents: 1455
diff changeset
289 // continue;
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
290 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
291 len=stream_read_dword_le(demux->stream);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
292 // if((len&(~1))!=(idx->dwChunkLength&(~1))){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
293 // if((len)!=(idx->dwChunkLength)){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
294 if((len!=idx->dwChunkLength)&&((len+1)!=idx->dwChunkLength)){
1464
891d8b59f2f1 changed chunkid/size check
arpi
parents: 1455
diff changeset
295 if(verbose) printf("ChunkSize mismatch! raw=%d idx=%ld \n",len,idx->dwChunkLength);
891d8b59f2f1 changed chunkid/size check
arpi
parents: 1455
diff changeset
296 len=idx->dwChunkLength;
891d8b59f2f1 changed chunkid/size check
arpi
parents: 1455
diff changeset
297 // continue;
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
298 }
979
3bcc435cd5a2 keyframe flag added
arpi_esp
parents: 600
diff changeset
299 if(idx->dwFlags&AVIIF_KEYFRAME) flags=1;
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
300 } else return 0;
979
3bcc435cd5a2 keyframe flag added
arpi_esp
parents: 600
diff changeset
301 ret=demux_avi_read_packet(demux,id,len,idx_pos,flags);
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
302 if(!ret && skip_video_frames<=0)
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
303 if(--max_packs==0){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
304 demux->stream->eof=1;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
305 printf("demux: file doesn't contain the selected audio or video stream\n");
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
306 return 0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
307 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
308 } while(ret!=1);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
309 return 1;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
310 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
311
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
312
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
313 // return value:
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
314 // 0 = EOF or no stream found
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
315 // 1 = successfully read a packet
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
316 int demux_avi_fill_buffer_nini(demuxer_t *demux,demux_stream_t* ds){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
317 unsigned int id=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
318 unsigned int len;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
319 int ret=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
320 int *fpos=NULL;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
321
587
8511095c5283 stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents: 426
diff changeset
322 if(ds==demux->video) fpos=&demux->idx_pos_a; else
8511095c5283 stage#1 completed: c files no more included from mplayer.c
arpi_esp
parents: 426
diff changeset
323 if(ds==demux->audio) fpos=&demux->idx_pos_v; else
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
324 return 0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
325
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
326 stream_seek(demux->stream,fpos[0]);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
327
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
328 do{
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
329
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
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
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
332 demux->stream->eof=1;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
333 return 0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
334 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
335 if(stream_eof(demux->stream)) return 0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
336
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
337 id=stream_read_dword_le(demux->stream);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
338 len=stream_read_dword_le(demux->stream);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
339 if(id==mmioFOURCC('L','I','S','T')){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
340 id=stream_read_dword_le(demux->stream); // list type
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
341 continue;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
342 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
343
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
344 if(ds==demux_avi_select_stream(demux,id)){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
345 // read it!
979
3bcc435cd5a2 keyframe flag added
arpi_esp
parents: 600
diff changeset
346 ret=demux_avi_read_packet(demux,id,len,demux->idx_pos-1,0);
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
347 } else {
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
348 // skip it!
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
349 int skip=(len+1)&(~1); // total bytes in this chunk
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
350 stream_skip(demux->stream,skip);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
351 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
352
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
353 } while(ret!=1);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
354 fpos[0]=stream_tell(demux->stream);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
355 return 1;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
356 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
357
1466
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
358 extern float initial_pts_delay;
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
359
1466
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
360 void demux_seek_avi(demuxer_t *demuxer,float rel_seek_secs,int flags){
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
361 demux_stream_t *d_audio=demuxer->audio;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
362 demux_stream_t *d_video=demuxer->video;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
363 sh_audio_t *sh_audio=d_audio->sh;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
364 sh_video_t *sh_video=d_video->sh;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
365 float skip_audio_secs=0;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
366
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
367 //FIXME: OFF_T - Didn't check AVI case yet (avi files can't be >2G anyway?)
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
368 //================= seek in AVI ==========================
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
369 int rel_seek_frames=rel_seek_secs*sh_video->fps;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
370 int curr_audio_pos=0;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
371 int audio_chunk_pos=-1;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
372 int video_chunk_pos=d_video->pos;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
373 int i;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
374
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
375 skip_video_frames=0;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
376 avi_audio_pts=0;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
377
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
378 // find nearest video keyframe chunk pos:
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
379 if(rel_seek_frames>0){
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
380 // seek forward
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
381 while(video_chunk_pos<demuxer->idx_size){
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
382 int id=((AVIINDEXENTRY *)demuxer->idx)[video_chunk_pos].ckid;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
383 if(avi_stream_id(id)==d_video->id){ // video frame
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
384 if((--rel_seek_frames)<0 && ((AVIINDEXENTRY *)demuxer->idx)[video_chunk_pos].dwFlags&AVIIF_KEYFRAME) break;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
385 // ++skip_audio_bytes;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
386 }
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
387 ++video_chunk_pos;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
388 }
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
389 } else {
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
390 // seek backward
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
391 while(video_chunk_pos>=0){
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
392 int id=((AVIINDEXENTRY *)demuxer->idx)[video_chunk_pos].ckid;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
393 if(avi_stream_id(id)==d_video->id){ // video frame
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
394 if((++rel_seek_frames)>0 && ((AVIINDEXENTRY *)demuxer->idx)[video_chunk_pos].dwFlags&AVIIF_KEYFRAME) break;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
395 // --skip_audio_bytes;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
396 }
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
397 --video_chunk_pos;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
398 }
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
399 }
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
400 demuxer->idx_pos_a=demuxer->idx_pos_v=demuxer->idx_pos=video_chunk_pos;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
401 // printf("%d frames skipped\n",skip_audio_bytes);
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
402
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
403 // re-calc video pts:
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
404 d_video->pack_no=0;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
405 for(i=0;i<video_chunk_pos;i++){
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
406 int id=((AVIINDEXENTRY *)demuxer->idx)[i].ckid;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
407 if(avi_stream_id(id)==d_video->id) ++d_video->pack_no;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
408 }
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
409 sh_video->num_frames=d_video->pack_no;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
410 avi_video_pts=d_video->pack_no*(float)sh_video->video.dwScale/(float)sh_video->video.dwRate;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
411
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
412 if(sh_audio){
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
413 int i;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
414 int apos=0;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
415 int last=0;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
416 int len=0;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
417 int skip_audio_bytes=0;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
418
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
419 // calc new audio position in audio stream: (using avg.bps value)
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
420 curr_audio_pos=(avi_video_pts) * sh_audio->wf->nAvgBytesPerSec;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
421 if(curr_audio_pos<0)curr_audio_pos=0;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
422 #if 1
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
423 curr_audio_pos&=~15; // requires for PCM formats!!!
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
424 #else
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
425 curr_audio_pos/=sh_audio->wf->nBlockAlign;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
426 curr_audio_pos*=sh_audio->wf->nBlockAlign;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
427 demuxer->audio_seekable=1;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
428 #endif
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
429
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
430 // find audio chunk pos:
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
431 for(i=0;i<video_chunk_pos;i++){
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
432 int id=((AVIINDEXENTRY *)demuxer->idx)[i].ckid;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
433 if(avi_stream_id(id)==d_audio->id){
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
434 len=((AVIINDEXENTRY *)demuxer->idx)[i].dwChunkLength;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
435 last=i;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
436 if(apos<=curr_audio_pos && curr_audio_pos<(apos+len)){
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
437 if(verbose)printf("break;\n");
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
438 break;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
439 }
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
440 apos+=len;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
441 }
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
442 }
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
443 if(verbose)printf("XXX i=%d last=%d apos=%d curr_audio_pos=%d \n",
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
444 i,last,apos,curr_audio_pos);
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
445 // audio_chunk_pos=last; // maybe wrong (if not break; )
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
446 audio_chunk_pos=i; // maybe wrong (if not break; )
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
447 skip_audio_bytes=curr_audio_pos-apos;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
448
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
449 // update stream position:
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
450 d_audio->pos=audio_chunk_pos;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
451 d_audio->dpos=apos;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
452 d_audio->pts=initial_pts_delay+(float)apos/(float)sh_audio->wf->nAvgBytesPerSec;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
453 demuxer->idx_pos_a=demuxer->idx_pos_v=demuxer->idx_pos=audio_chunk_pos;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
454
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
455 if(!(sh_audio->codec->flags&CODECS_FLAG_SEEKABLE)){
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
456 #if 0
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
457 // curr_audio_pos=apos; // selected audio codec can't seek in chunk
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
458 skip_audio_secs=(float)skip_audio_bytes/(float)sh_audio->wf->nAvgBytesPerSec;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
459 //printf("Seek_AUDIO: %d bytes --> %5.3f secs\n",skip_audio_bytes,skip_audio_secs);
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
460 skip_audio_bytes=0;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
461 #else
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
462 int d=skip_audio_bytes % sh_audio->wf->nBlockAlign;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
463 skip_audio_bytes-=d;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
464 // curr_audio_pos-=d;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
465 skip_audio_secs=(float)d/(float)sh_audio->wf->nAvgBytesPerSec;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
466 //printf("Seek_AUDIO: %d bytes --> %5.3f secs\n",d,skip_audio_secs);
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
467 #endif
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
468 }
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
469 // now: audio_chunk_pos=pos in index
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
470 // skip_audio_bytes=bytes to skip from that chunk
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
471 // skip_audio_secs=time to play audio before video (if can't skip)
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
472
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
473 // calc skip_video_frames & adjust video pts counter:
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
474 // i=last;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
475 for(i=demuxer->idx_pos;i<video_chunk_pos;i++){
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
476 int id=((AVIINDEXENTRY *)demuxer->idx)[i].ckid;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
477 if(avi_stream_id(id)==d_video->id) ++skip_video_frames;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
478 }
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
479 // requires for correct audio pts calculation (demuxer):
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
480 avi_video_pts-=skip_video_frames*(float)sh_video->video.dwScale/(float)sh_video->video.dwRate;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
481
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
482 if(verbose) printf("SEEK: idx=%d (a:%d v:%d) v.skip=%d a.skip=%d/%4.3f \n",
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
483 demuxer->idx_pos,audio_chunk_pos,video_chunk_pos,
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
484 skip_video_frames,skip_audio_bytes,skip_audio_secs);
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
485
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
486 if(skip_audio_bytes){
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
487 demux_read_data(d_audio,NULL,skip_audio_bytes);
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
488 //d_audio->pts=0; // PTS is outdated because of the raw data skipping
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
489 }
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
490 resync_audio_stream(sh_audio);
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
491
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
492 sh_audio->timer=-skip_audio_secs;
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
493
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
494 }
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
495
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
496
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
497 }
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
498
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
499
7845f6d7c4ba format-specific seeking code moved to demuxer_ stuff
arpi
parents: 1464
diff changeset
500