Mercurial > mplayer.hg
annotate demux_mov.c @ 2113:42291b22243f
mp-iso-arial-ize
author | gabucino |
---|---|
date | Sat, 06 Oct 2001 19:28:04 +0000 |
parents | 3e55202cfdab |
children | fe1cd4a5109a |
rev | line source |
---|---|
1490 | 1 // QuickTime MOV file parser |
2 // based on TOOLS/movinfo.c by A'rpi & Al3x | |
3 | |
4 #include <stdio.h> | |
5 #include <stdlib.h> | |
6 #include <unistd.h> | |
7 | |
1567 | 8 #include "config.h" |
9 #include "mp_msg.h" | |
1973
5216f108cb4f
all error/warn/info messages moved to help_mp-en.h for translation
arpi
parents:
1567
diff
changeset
|
10 #include "help_mp.h" |
1490 | 11 |
12 #include "stream.h" | |
13 #include "demuxer.h" | |
14 | |
15 #include "wine/mmreg.h" | |
16 #include "wine/avifmt.h" | |
17 #include "wine/vfw.h" | |
18 | |
19 #include "codec-cfg.h" | |
20 #include "stheader.h" | |
21 | |
22 typedef struct { | |
2100 | 23 unsigned int pts; // duration |
24 unsigned int size; | |
25 off_t pos; | |
26 } mov_sample_t; | |
27 | |
28 typedef struct { | |
29 unsigned int size; // samples/chunk | |
30 int desc; // for multiple codecs mode - not used | |
31 off_t pos; | |
32 } mov_chunk_t; | |
33 | |
34 typedef struct { | |
35 unsigned int first; | |
36 unsigned int spc; | |
37 unsigned int sdid; | |
38 } mov_chunkmap_t; | |
39 | |
40 typedef struct { | |
1490 | 41 int id; |
42 int type; | |
2101 | 43 int pos; |
44 // | |
1490 | 45 int timescale; |
2100 | 46 unsigned int length; |
1490 | 47 int width,height; // for video |
48 unsigned int fourcc; | |
2101 | 49 int tkdata_len; // track data |
50 unsigned char* tkdata; | |
51 int stdata_len; // stream data | |
52 unsigned char* stdata; | |
2100 | 53 int samples_size; |
54 mov_sample_t* samples; | |
55 int chunks_size; | |
56 mov_chunk_t* chunks; | |
57 int chunkmap_size; | |
58 mov_chunkmap_t* chunkmap; | |
1490 | 59 } mov_track_t; |
60 | |
2100 | 61 void mov_build_index(mov_track_t* trak){ |
62 int i,j,s; | |
63 int last=trak->chunks_size; | |
64 printf("MOV track: %d chunks, %d samples\n",trak->chunks_size,trak->samples_size); | |
65 printf("pts=%d scale=%d time=%5.3f\n",trak->length,trak->timescale,(float)trak->length/(float)trak->timescale); | |
66 // process chunkmap: | |
67 i=trak->chunkmap_size; | |
68 while(i>0){ | |
69 --i; | |
70 for(j=trak->chunkmap[i].first;j<last;j++){ | |
71 trak->chunks[j].desc=trak->chunkmap[i].sdid; | |
72 trak->chunks[j].size=trak->chunkmap[i].spc; | |
73 } | |
74 last=trak->chunkmap[i].first; | |
75 } | |
76 | |
77 // calc sample offsets | |
78 s=0; | |
79 for(j=0;j<trak->chunks_size;j++){ | |
80 off_t pos=trak->chunks[j].pos; | |
81 for(i=0;i<trak->chunks[j].size;i++){ | |
82 trak->samples[s].pos=pos; | |
83 #if 0 | |
84 printf("Sample %5d: pts=%8d off=0x%08X size=%d\n",s, | |
85 trak->samples[s].pts, | |
86 (int)trak->samples[s].pos, | |
87 trak->samples[s].size); | |
88 #endif | |
89 pos+=trak->samples[s].size; | |
90 ++s; | |
91 } | |
92 } | |
93 | |
94 } | |
95 | |
1490 | 96 #define MOV_MAX_TRACKS 256 |
97 | |
98 #define MOV_TRAK_UNKNOWN 0 | |
99 #define MOV_TRAK_VIDEO 1 | |
100 #define MOV_TRAK_AUDIO 2 | |
101 | |
102 typedef struct { | |
103 off_t moov_start; | |
104 off_t moov_end; | |
105 off_t mdat_start; | |
106 off_t mdat_end; | |
107 int track_db; | |
108 mov_track_t* tracks[MOV_MAX_TRACKS]; | |
109 } mov_priv_t; | |
110 | |
111 #define MOV_FOURCC(a,b,c,d) ((a<<24)|(b<<16)|(c<<8)|(d)) | |
112 | |
113 int mov_check_file(demuxer_t* demuxer){ | |
114 int flags=0; | |
115 mov_priv_t* priv=malloc(sizeof(mov_priv_t)); | |
116 | |
1567 | 117 mp_msg(MSGT_DEMUX,MSGL_V,"Checking for MOV\n"); |
1490 | 118 |
119 memset(priv,0,sizeof(mov_priv_t)); | |
120 demuxer->priv=priv; | |
121 | |
122 while(1){ | |
123 off_t len=stream_read_dword(demuxer->stream); | |
124 unsigned int id=stream_read_dword(demuxer->stream); | |
125 if(stream_eof(demuxer->stream)) break; // EOF | |
126 if(len<8) break; // invalid chunk | |
127 switch(id){ | |
128 case MOV_FOURCC('m','o','o','v'): | |
1567 | 129 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: Movie header found!\n"); |
1490 | 130 priv->moov_start=stream_tell(demuxer->stream); |
131 priv->moov_end=priv->moov_start+len-8; | |
132 flags|=1; | |
133 break; | |
134 case MOV_FOURCC('m','d','a','t'): | |
1567 | 135 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: Movie DATA found!\n"); |
1490 | 136 priv->mdat_start=stream_tell(demuxer->stream); |
137 priv->mdat_end=priv->mdat_start+len-8; | |
138 flags|=2; | |
139 break; | |
140 default: | |
1567 | 141 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: unknown chunk: %.4s %d\n",&id,(int)len); |
1490 | 142 } |
143 if(!stream_skip(demuxer->stream,len-8)) break; | |
144 } | |
145 | |
146 return (flags==3); | |
147 } | |
148 | |
149 static void lschunks(demuxer_t* demuxer,int level,off_t endpos,mov_track_t* trak){ | |
150 mov_priv_t* priv=demuxer->priv; | |
151 while(1){ | |
152 off_t pos; | |
153 off_t len; | |
154 unsigned int id; | |
155 // | |
156 pos=stream_tell(demuxer->stream); | |
157 if(pos>=endpos) return; // END | |
158 len=stream_read_dword(demuxer->stream); | |
159 if(len<8) return; // error | |
160 len-=8; | |
161 id=stream_read_dword(demuxer->stream); | |
162 // | |
1567 | 163 mp_msg(MSGT_DEMUX,MSGL_DBG2,"lschunks %.4s %d\n",&id,(int)len); |
1490 | 164 // |
165 if(trak){ | |
166 switch(id){ | |
167 case MOV_FOURCC('t','k','h','d'): { | |
1567 | 168 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*sTrack header!\n",level,""); |
2101 | 169 // read codec data |
170 trak->tkdata_len=len; | |
171 trak->tkdata=malloc(trak->tkdata_len); | |
172 stream_read(demuxer->stream,trak->tkdata,trak->tkdata_len); | |
1490 | 173 break; |
174 } | |
175 case MOV_FOURCC('m','d','h','d'): { | |
2100 | 176 unsigned int tmp; |
1567 | 177 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*sMedia header!\n",level,""); |
2100 | 178 #if 0 |
179 tmp=stream_read_dword(demuxer->stream); | |
180 printf("dword1: 0x%08X (%d)\n",tmp,tmp); | |
181 tmp=stream_read_dword(demuxer->stream); | |
182 printf("dword2: 0x%08X (%d)\n",tmp,tmp); | |
183 tmp=stream_read_dword(demuxer->stream); | |
184 printf("dword3: 0x%08X (%d)\n",tmp,tmp); | |
185 tmp=stream_read_dword(demuxer->stream); | |
186 printf("dword4: 0x%08X (%d)\n",tmp,tmp); | |
187 tmp=stream_read_dword(demuxer->stream); | |
188 printf("dword5: 0x%08X (%d)\n",tmp,tmp); | |
189 tmp=stream_read_dword(demuxer->stream); | |
190 printf("dword6: 0x%08X (%d)\n",tmp,tmp); | |
191 #endif | |
192 stream_skip(demuxer->stream,12); | |
1490 | 193 // read timescale |
2100 | 194 trak->timescale=stream_read_dword(demuxer->stream); |
195 // read length | |
196 trak->length=stream_read_dword(demuxer->stream); | |
1490 | 197 break; |
198 } | |
199 case MOV_FOURCC('v','m','h','d'): { | |
1567 | 200 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*sVideo header!\n",level,""); |
1490 | 201 trak->type=MOV_TRAK_VIDEO; |
202 // read video data | |
203 break; | |
204 } | |
205 case MOV_FOURCC('s','m','h','d'): { | |
1567 | 206 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*sSound header!\n",level,""); |
1490 | 207 trak->type=MOV_TRAK_AUDIO; |
208 // read audio data | |
209 break; | |
210 } | |
211 case MOV_FOURCC('s','t','s','d'): { | |
212 int i=stream_read_dword(demuxer->stream); // temp! | |
213 int count=stream_read_dword(demuxer->stream); | |
1567 | 214 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*sDescription list! (cnt:%d)\n",level,"",count); |
1490 | 215 for(i=0;i<count;i++){ |
216 off_t pos=stream_tell(demuxer->stream); | |
217 off_t len=stream_read_dword(demuxer->stream); | |
218 unsigned int fourcc=stream_read_dword_le(demuxer->stream); | |
219 if(len<8) break; // error | |
1567 | 220 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*s desc #%d: %.4s",level,"",i,&fourcc); |
1490 | 221 if(!i){ |
222 trak->fourcc=fourcc; | |
223 // read codec data | |
2101 | 224 trak->stdata_len=len-8; |
225 trak->stdata=malloc(trak->stdata_len); | |
226 stream_read(demuxer->stream,trak->stdata,trak->stdata_len); | |
227 if(trak->type==MOV_TRAK_VIDEO && trak->stdata_len>43){ | |
228 mp_msg(MSGT_DEMUX,MSGL_V," '%.*s'",trak->stdata_len-43,trak->stdata+43); | |
1490 | 229 } |
230 } | |
1567 | 231 mp_msg(MSGT_DEMUX,MSGL_V,"\n"); |
1490 | 232 if(fourcc!=trak->fourcc && i) |
1973
5216f108cb4f
all error/warn/info messages moved to help_mp-en.h for translation
arpi
parents:
1567
diff
changeset
|
233 mp_msg(MSGT_DEMUX,MSGL_WARN,MSGTR_MOVvariableFourCC); |
1490 | 234 if(!stream_seek(demuxer->stream,pos+len)) break; |
235 } | |
236 break; | |
237 } | |
2100 | 238 case MOV_FOURCC('s','t','t','s'): { |
239 int temp=stream_read_dword(demuxer->stream); | |
240 int len=stream_read_dword(demuxer->stream); | |
241 int i; | |
242 int x=0; | |
243 unsigned int pts=0; | |
244 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*sSample duration table! (%d blocks)\n",level,"",len); | |
245 for(i=0;i<len;i++){ | |
246 unsigned int num=stream_read_dword(demuxer->stream); | |
247 unsigned int dur=stream_read_dword(demuxer->stream); | |
248 printf("num=%d dur=%d [%d] (%d)\n",num,dur,trak->samples_size,x); | |
249 num+=x; | |
250 // extend array if needed: | |
251 if(num>trak->samples_size){ | |
252 trak->samples=realloc(trak->samples,sizeof(mov_sample_t)*num); | |
253 trak->samples_size=num; | |
254 } | |
255 // fill array: | |
256 while(x<num){ | |
257 trak->samples[x].pts=pts; | |
258 pts+=dur; | |
259 ++x; | |
260 } | |
261 } | |
262 if(trak->length!=pts) printf("Warning! pts=%d length=%d\n",pts,trak->length); | |
263 break; | |
264 } | |
265 case MOV_FOURCC('s','t','s','c'): { | |
266 int temp=stream_read_dword(demuxer->stream); | |
267 int len=stream_read_dword(demuxer->stream); | |
268 int i; | |
269 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*sSample->Chunk mapping table! (%d blocks)\n",level,"",len); | |
270 // read data: | |
271 trak->chunkmap_size=len; | |
272 trak->chunkmap=malloc(sizeof(mov_chunkmap_t)*len); | |
273 for(i=0;i<len;i++){ | |
2103 | 274 trak->chunkmap[i].first=stream_read_dword(demuxer->stream)-1; |
2100 | 275 trak->chunkmap[i].spc=stream_read_dword(demuxer->stream); |
276 trak->chunkmap[i].sdid=stream_read_dword(demuxer->stream); | |
277 } | |
278 break; | |
279 } | |
280 case MOV_FOURCC('s','t','s','z'): { | |
281 int temp=stream_read_dword(demuxer->stream); | |
282 int ss=stream_read_dword(demuxer->stream); | |
283 int len=stream_read_dword(demuxer->stream); | |
284 int i; | |
285 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*sSample size table! len=%d ss=%d\n",level,"",len,ss); | |
286 // extend array if needed: | |
287 if(len>trak->samples_size){ | |
288 trak->samples=realloc(trak->samples,sizeof(mov_sample_t)*len); | |
289 trak->samples_size=len; | |
290 } | |
291 for(i=0;i<len;i++){ | |
292 trak->samples[i].size=ss?ss:stream_read_dword(demuxer->stream); | |
293 } | |
294 break; | |
295 } | |
296 case MOV_FOURCC('s','t','c','o'): { | |
297 int temp=stream_read_dword(demuxer->stream); | |
298 int len=stream_read_dword(demuxer->stream); | |
299 int i; | |
300 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*sChunk offset table! (%d chunks)\n",level,"",len); | |
301 // extend array if needed: | |
302 if(len>trak->chunks_size){ | |
303 trak->chunks=realloc(trak->chunks,sizeof(mov_chunk_t)*len); | |
304 trak->chunks_size=len; | |
305 } | |
306 // read elements: | |
307 for(i=0;i<len;i++) trak->chunks[i].pos=stream_read_dword(demuxer->stream); | |
308 break; | |
309 } | |
1490 | 310 case MOV_FOURCC('m','d','i','a'): { |
1567 | 311 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*sMedia stream!\n",level,""); |
1490 | 312 lschunks(demuxer,level+1,pos+len,trak); |
313 break; | |
314 } | |
315 case MOV_FOURCC('m','i','n','f'): { | |
1567 | 316 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*sMedia info!\n",level,""); |
1490 | 317 lschunks(demuxer,level+1,pos+len,trak); |
318 break; | |
319 } | |
320 case MOV_FOURCC('s','t','b','l'): { | |
1567 | 321 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*sSample info!\n",level,""); |
1490 | 322 lschunks(demuxer,level+1,pos+len,trak); |
323 break; | |
324 } | |
325 }//switch(id) | |
326 } else | |
327 if(id==MOV_FOURCC('t','r','a','k')){ | |
328 // if(trak) printf("MOV: Warning! trak in trak?\n"); | |
329 if(priv->track_db>=MOV_MAX_TRACKS){ | |
1973
5216f108cb4f
all error/warn/info messages moved to help_mp-en.h for translation
arpi
parents:
1567
diff
changeset
|
330 mp_msg(MSGT_DEMUX,MSGL_WARN,MSGTR_MOVtooManyTrk); |
1490 | 331 return; |
332 } | |
333 trak=malloc(sizeof(mov_track_t)); | |
334 memset(trak,0,sizeof(mov_track_t)); | |
1567 | 335 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: Track #%d:\n",priv->track_db); |
1490 | 336 trak->id=priv->track_db; |
2100 | 337 priv->tracks[priv->track_db]=trak; |
1490 | 338 lschunks(demuxer,level+1,pos+len,trak); |
2100 | 339 mov_build_index(trak); |
340 switch(trak->type){ | |
341 case MOV_TRAK_AUDIO: { | |
342 sh_audio_t* sh=new_sh_audio(demuxer,priv->track_db); | |
343 sh->format=trak->fourcc; | |
2101 | 344 printf("!!! audio bits: %d chans: %d\n",trak->stdata[19],trak->stdata[17]); |
345 // Emulate WAVEFORMATEX struct: | |
346 sh->wf=malloc(sizeof(WAVEFORMATEX)); | |
347 memset(sh->wf,0,sizeof(WAVEFORMATEX)); | |
348 sh->wf->nChannels=trak->stdata[17]; | |
349 sh->wf->wBitsPerSample=trak->stdata[19]; | |
350 sh->wf->nSamplesPerSec=trak->timescale; | |
351 sh->wf->nAvgBytesPerSec=sh->wf->nChannels*((sh->wf->wBitsPerSample+7)/8)*sh->wf->nSamplesPerSec; | |
352 // Selection: | |
353 if(demuxer->audio->id==-1 || demuxer->audio->id==priv->track_db){ | |
354 // (auto)selected audio track: | |
355 demuxer->audio->id=priv->track_db; | |
356 demuxer->audio->sh=sh; sh->ds=demuxer->audio; | |
357 } | |
2100 | 358 break; |
359 } | |
360 case MOV_TRAK_VIDEO: { | |
361 sh_video_t* sh=new_sh_video(demuxer,priv->track_db); | |
362 sh->format=trak->fourcc; | |
363 sh->fps=trak->timescale; | |
364 sh->frametime=1.0f/sh->fps; | |
2101 | 365 sh->disp_w=trak->tkdata[77]|(trak->tkdata[76]<<8); |
366 sh->disp_h=trak->tkdata[81]|(trak->tkdata[80]<<8); | |
367 | |
368 // emulate BITMAPINFOHEADER: | |
369 sh->bih=malloc(sizeof(BITMAPINFOHEADER)); | |
370 memset(sh->bih,0,sizeof(BITMAPINFOHEADER)); | |
371 sh->bih->biSize=40; | |
372 sh->bih->biWidth=sh->disp_w; | |
373 sh->bih->biHeight=sh->disp_h; | |
374 sh->bih->biPlanes=0; | |
375 sh->bih->biBitCount=16; | |
376 sh->bih->biCompression=trak->fourcc; | |
377 sh->bih->biSizeImage=sh->bih->biWidth*sh->bih->biHeight; | |
378 | |
379 printf("Image size: %d x %d\n",sh->disp_w,sh->disp_h); | |
380 if(demuxer->video->id==-1 || demuxer->video->id==priv->track_db){ | |
381 // (auto)selected video track: | |
382 demuxer->video->id=priv->track_db; | |
383 demuxer->video->sh=sh; sh->ds=demuxer->video; | |
384 } | |
2100 | 385 break; |
386 } | |
387 } | |
388 printf("--------------\n"); | |
389 priv->track_db++; | |
1490 | 390 trak=NULL; |
391 } else | |
392 if(id==MOV_FOURCC('c','m','o','v')){ | |
1973
5216f108cb4f
all error/warn/info messages moved to help_mp-en.h for translation
arpi
parents:
1567
diff
changeset
|
393 mp_msg(MSGT_DEMUX,MSGL_ERR,MSGTR_MOVcomprhdr); |
1490 | 394 return; |
395 } | |
396 | |
397 pos+=len+8; | |
398 if(pos>=endpos) break; | |
399 if(!stream_seek(demuxer->stream,pos)) break; | |
400 } | |
401 } | |
402 | |
403 int mov_read_header(demuxer_t* demuxer){ | |
404 mov_priv_t* priv=demuxer->priv; | |
405 | |
2100 | 406 printf("mov_read_header!\n"); |
1490 | 407 |
408 // Parse header: | |
2100 | 409 stream_reset(demuxer->stream); |
1490 | 410 if(!stream_seek(demuxer->stream,priv->moov_start)) return 0; // ??? |
411 lschunks(demuxer, 0, priv->moov_end, NULL); | |
412 | |
413 // Build tables: | |
414 // ... | |
415 | |
1973
5216f108cb4f
all error/warn/info messages moved to help_mp-en.h for translation
arpi
parents:
1567
diff
changeset
|
416 mp_msg(MSGT_DEMUX,MSGL_ERR,MSGTR_MOVnotyetsupp); |
1490 | 417 |
418 return 1; | |
419 } | |
2101 | 420 |
421 // return value: | |
422 // 0 = EOF or no stream found | |
423 // 1 = successfully read a packet | |
424 int demux_mov_fill_buffer(demuxer_t *demuxer,demux_stream_t* ds){ | |
425 mov_priv_t* priv=demuxer->priv; | |
426 mov_track_t* trak=NULL; | |
427 float pts; | |
428 | |
429 if(ds->id<0 || ds->id>=priv->track_db) return 0; | |
430 trak=priv->tracks[ds->id]; | |
431 | |
432 // read sample: | |
433 if(trak->pos>=trak->samples_size) return 0; // EOF | |
434 | |
435 stream_seek(demuxer->stream,trak->samples[trak->pos].pos); | |
436 pts=(float)trak->samples[trak->pos].pts/(float)trak->timescale; | |
437 ds_read_packet(ds,demuxer->stream,trak->samples[trak->pos].size,pts,trak->samples[trak->pos].pos,0); | |
438 ++trak->pos; | |
439 return 1; | |
440 | |
441 } |