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