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