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"
|
1490
|
10
|
|
11 #include "stream.h"
|
|
12 #include "demuxer.h"
|
|
13
|
|
14 #include "wine/mmreg.h"
|
|
15 #include "wine/avifmt.h"
|
|
16 #include "wine/vfw.h"
|
|
17
|
|
18 #include "codec-cfg.h"
|
|
19 #include "stheader.h"
|
|
20
|
|
21 typedef struct {
|
|
22 int id;
|
|
23 int type;
|
|
24 int timescale;
|
|
25 int width,height; // for video
|
|
26 unsigned int fourcc;
|
|
27 int data_len;
|
|
28 unsigned char* data;
|
|
29 } mov_track_t;
|
|
30
|
|
31 #define MOV_MAX_TRACKS 256
|
|
32
|
|
33 #define MOV_TRAK_UNKNOWN 0
|
|
34 #define MOV_TRAK_VIDEO 1
|
|
35 #define MOV_TRAK_AUDIO 2
|
|
36
|
|
37 typedef struct {
|
|
38 off_t moov_start;
|
|
39 off_t moov_end;
|
|
40 off_t mdat_start;
|
|
41 off_t mdat_end;
|
|
42 int track_db;
|
|
43 mov_track_t* tracks[MOV_MAX_TRACKS];
|
|
44 } mov_priv_t;
|
|
45
|
|
46 #define MOV_FOURCC(a,b,c,d) ((a<<24)|(b<<16)|(c<<8)|(d))
|
|
47
|
|
48 int mov_check_file(demuxer_t* demuxer){
|
|
49 int flags=0;
|
|
50 mov_priv_t* priv=malloc(sizeof(mov_priv_t));
|
|
51
|
1567
|
52 mp_msg(MSGT_DEMUX,MSGL_V,"Checking for MOV\n");
|
1490
|
53
|
|
54 memset(priv,0,sizeof(mov_priv_t));
|
|
55 demuxer->priv=priv;
|
|
56
|
|
57 while(1){
|
|
58 off_t len=stream_read_dword(demuxer->stream);
|
|
59 unsigned int id=stream_read_dword(demuxer->stream);
|
|
60 if(stream_eof(demuxer->stream)) break; // EOF
|
|
61 if(len<8) break; // invalid chunk
|
|
62 switch(id){
|
|
63 case MOV_FOURCC('m','o','o','v'):
|
1567
|
64 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: Movie header found!\n");
|
1490
|
65 priv->moov_start=stream_tell(demuxer->stream);
|
|
66 priv->moov_end=priv->moov_start+len-8;
|
|
67 flags|=1;
|
|
68 break;
|
|
69 case MOV_FOURCC('m','d','a','t'):
|
1567
|
70 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: Movie DATA found!\n");
|
1490
|
71 priv->mdat_start=stream_tell(demuxer->stream);
|
|
72 priv->mdat_end=priv->mdat_start+len-8;
|
|
73 flags|=2;
|
|
74 break;
|
|
75 default:
|
1567
|
76 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: unknown chunk: %.4s %d\n",&id,(int)len);
|
1490
|
77 }
|
|
78 if(!stream_skip(demuxer->stream,len-8)) break;
|
|
79 }
|
|
80
|
|
81 return (flags==3);
|
|
82 }
|
|
83
|
|
84 static void lschunks(demuxer_t* demuxer,int level,off_t endpos,mov_track_t* trak){
|
|
85 mov_priv_t* priv=demuxer->priv;
|
|
86 while(1){
|
|
87 off_t pos;
|
|
88 off_t len;
|
|
89 unsigned int id;
|
|
90 //
|
|
91 pos=stream_tell(demuxer->stream);
|
|
92 if(pos>=endpos) return; // END
|
|
93 len=stream_read_dword(demuxer->stream);
|
|
94 if(len<8) return; // error
|
|
95 len-=8;
|
|
96 id=stream_read_dword(demuxer->stream);
|
|
97 //
|
1567
|
98 mp_msg(MSGT_DEMUX,MSGL_DBG2,"lschunks %.4s %d\n",&id,(int)len);
|
1490
|
99 //
|
|
100 if(trak){
|
|
101 switch(id){
|
|
102 case MOV_FOURCC('t','k','h','d'): {
|
1567
|
103 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*sTrack header!\n",level,"");
|
1490
|
104 // read width x height
|
|
105 break;
|
|
106 }
|
|
107 case MOV_FOURCC('m','d','h','d'): {
|
1567
|
108 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*sMedia header!\n",level,"");
|
1490
|
109 // read timescale
|
|
110 break;
|
|
111 }
|
|
112 case MOV_FOURCC('v','m','h','d'): {
|
1567
|
113 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*sVideo header!\n",level,"");
|
1490
|
114 trak->type=MOV_TRAK_VIDEO;
|
|
115 // read video data
|
|
116 break;
|
|
117 }
|
|
118 case MOV_FOURCC('s','m','h','d'): {
|
1567
|
119 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*sSound header!\n",level,"");
|
1490
|
120 trak->type=MOV_TRAK_AUDIO;
|
|
121 // read audio data
|
|
122 break;
|
|
123 }
|
|
124 case MOV_FOURCC('s','t','s','d'): {
|
|
125 int i=stream_read_dword(demuxer->stream); // temp!
|
|
126 int count=stream_read_dword(demuxer->stream);
|
1567
|
127 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*sDescription list! (cnt:%d)\n",level,"",count);
|
1490
|
128 for(i=0;i<count;i++){
|
|
129 off_t pos=stream_tell(demuxer->stream);
|
|
130 off_t len=stream_read_dword(demuxer->stream);
|
|
131 unsigned int fourcc=stream_read_dword_le(demuxer->stream);
|
|
132 if(len<8) break; // error
|
1567
|
133 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*s desc #%d: %.4s",level,"",i,&fourcc);
|
1490
|
134 if(!i){
|
|
135 trak->fourcc=fourcc;
|
|
136 // read codec data
|
|
137 trak->data_len=len-8;
|
|
138 trak->data=malloc(trak->data_len);
|
|
139 stream_read(demuxer->stream,trak->data,trak->data_len);
|
|
140 if(trak->type==MOV_TRAK_VIDEO && trak->data_len>43){
|
1567
|
141 mp_msg(MSGT_DEMUX,MSGL_V," '%.*s'",trak->data_len-43,trak->data+43);
|
1490
|
142 }
|
|
143 }
|
1567
|
144 mp_msg(MSGT_DEMUX,MSGL_V,"\n");
|
1490
|
145 if(fourcc!=trak->fourcc && i)
|
1567
|
146 mp_msg(MSGT_DEMUX,MSGL_WARN,"MOV: Warning! variable FOURCC detected!?\n");
|
1490
|
147 if(!stream_seek(demuxer->stream,pos+len)) break;
|
|
148 }
|
|
149 break;
|
|
150 }
|
|
151 case MOV_FOURCC('m','d','i','a'): {
|
1567
|
152 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*sMedia stream!\n",level,"");
|
1490
|
153 lschunks(demuxer,level+1,pos+len,trak);
|
|
154 break;
|
|
155 }
|
|
156 case MOV_FOURCC('m','i','n','f'): {
|
1567
|
157 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*sMedia info!\n",level,"");
|
1490
|
158 lschunks(demuxer,level+1,pos+len,trak);
|
|
159 break;
|
|
160 }
|
|
161 case MOV_FOURCC('s','t','b','l'): {
|
1567
|
162 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*sSample info!\n",level,"");
|
1490
|
163 lschunks(demuxer,level+1,pos+len,trak);
|
|
164 break;
|
|
165 }
|
|
166 }//switch(id)
|
|
167 } else
|
|
168 if(id==MOV_FOURCC('t','r','a','k')){
|
|
169 // if(trak) printf("MOV: Warning! trak in trak?\n");
|
|
170 if(priv->track_db>=MOV_MAX_TRACKS){
|
1567
|
171 mp_msg(MSGT_DEMUX,MSGL_WARN,"MOV: Warning! too many tracks!");
|
1490
|
172 return;
|
|
173 }
|
|
174 trak=malloc(sizeof(mov_track_t));
|
|
175 memset(trak,0,sizeof(mov_track_t));
|
1567
|
176 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: Track #%d:\n",priv->track_db);
|
1490
|
177 trak->id=priv->track_db;
|
|
178 priv->tracks[priv->track_db++]=trak;
|
|
179 lschunks(demuxer,level+1,pos+len,trak);
|
|
180 trak=NULL;
|
|
181 } else
|
|
182 if(id==MOV_FOURCC('c','m','o','v')){
|
1567
|
183 mp_msg(MSGT_DEMUX,MSGL_ERR,"MOV: Compressed headers not (yet) supported!\n");
|
1490
|
184 return;
|
|
185 }
|
|
186
|
|
187 pos+=len+8;
|
|
188 if(pos>=endpos) break;
|
|
189 if(!stream_seek(demuxer->stream,pos)) break;
|
|
190 }
|
|
191 }
|
|
192
|
|
193 int mov_read_header(demuxer_t* demuxer){
|
|
194 mov_priv_t* priv=demuxer->priv;
|
|
195
|
|
196 // printf("mov_read_header!\n");
|
|
197
|
|
198 // Parse header:
|
|
199 if(!stream_seek(demuxer->stream,priv->moov_start)) return 0; // ???
|
|
200 lschunks(demuxer, 0, priv->moov_end, NULL);
|
|
201
|
|
202 // Build tables:
|
|
203 // ...
|
|
204
|
1567
|
205 mp_msg(MSGT_DEMUX,MSGL_ERR,"\n****** Quicktime MOV format not yet supported!!!!!!! *******\n");
|
1490
|
206
|
|
207 return 1;
|
|
208 }
|