Change some filename-handling code to use mp_basename()
[mplayer/greg.git] / TOOLS / movinfo.c
blobdf9c0e541d2a89d053997927b719c30011e50b2a
1 /* show QuickTime .mov file structure (C) 2001. by A'rpi/ESP-team
2 * various hacks by alex@naxine.org
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 Blocks: 4bytes atom_size
21 4bytes atom_type (name)
22 ...
24 By older files, mdat is at the beginning, and moov follows it later,
25 by newer files, moov is at the begininng.
27 Fontosabb typeok:
29 trak: track: ezeken belul van egy-egy stream (video/audio)
30 tkhd: track header: fps (video esten picture size is itt van)
31 vmhd: video media handler (video stream informaciok)
32 smhd: sound media handler (audio stream informaciok)
35 #include <stdio.h>
36 #include <stdlib.h>
38 #undef NO_SPECIAL
40 static char *atom2human_type(int type)
42 switch (type)
44 case 0x766F6F6D: return "Information sections"; /* moov */
45 case 0x6468766D: return "Movie header"; /* mvhd */
46 case 0x6169646D: return "Media stream"; /* mdia */
47 case 0x64686D76: return "Video media header"; /* vmhd */
48 case 0x64686D73: return "Sound media header"; /* smhd */
49 case 0x6468646D: return "Media header"; /* mdhd */
50 case 0x666E696D: return "Media information"; /* minf */
51 case 0x726C6468: return "Handler reference"; /* hdlr */
52 case 0x6B617274: return "New track (stream)"; /* trak */
53 case 0x75716D72: return "rmqu";
54 case 0x65657266: return "free";
55 case 0x64686B74: return "Track header"; /* tkhd */
56 case 0x61746475: return "User data"; /* udta */
57 case 0x7461646D: return "Movie data"; /* mdat */
58 case 0x6C627473: return "Sample information table"; /* stbl */
59 case 0x64737473: return "Sample description"; /* stsd */
60 case 0x6F637473: return "Chunk offset table"; /* stco */
61 case 0x73747473: return "Sample time table"; /* stts */
62 case 0x63737473: return "Sample->Chunk mapping table"; /* stsc */
63 case 0x7A737473: return "Sample size table"; /* stsz */
65 return "unknown";
68 #define S_NONE 0
69 #define S_AUDIO 1
70 #define S_VIDEO 2
71 int stream = S_NONE;
72 int v_stream = 0;
73 int a_stream = 0;
75 static unsigned int read_dword(FILE *f){
76 unsigned char atom_size_b[4];
77 if(fread(&atom_size_b,4,1,f)<=0) return -1;
78 return (atom_size_b[0]<<24)|(atom_size_b[1]<<16)|(atom_size_b[2]<<8)|atom_size_b[3];
81 static void video_stream_info(FILE *f, int len)
83 int orig_pos = ftell(f);
84 unsigned char data[len-8];
85 int i;
86 // char codec[len-8];
88 len -= 8;
89 for (i=0; i<len; i++)
90 fread(&data[i], 1, 1, f);
92 // strncpy(codec, &data[43], len-43);
93 // printf(" [codec: %s]\n", &codec);
94 fseek(f,orig_pos,SEEK_SET);
97 static void audio_stream_info(FILE *f, int len)
99 int orig_pos = ftell(f);
100 unsigned char data[len-8];
101 int i;
103 len -= 8;
104 for (i=0; i<len; i++)
105 fread(&data[i], 1, 1, f);
107 printf(" [%d bit", data[19]);
108 if (data[17] == 1)
109 printf(" mono");
110 else
111 printf(" %d channels", data[17]);
112 printf("]\n");
113 fseek(f,orig_pos,SEEK_SET);
116 #if 0
117 static void userdata_info(FILE *f, int len, int pos, int level)
119 int orig_pos = pos; /*ftell(f);*/
120 unsigned int atom_size = 1;
121 unsigned int atom_type;
123 // printf("userdata @ %d:%d (%d)\n", pos, pos+len, len);
125 // fseek(f, pos+3, SEEK_SET);
127 while (atom_size != 0)
129 atom_size=read_dword(f);// if(fread(&atom_size_b,4,1,f)<=0) break;
130 if(fread(&atom_type,4,1,f)<=0) break;
132 if(atom_size<8) break; // error
134 // printf("%08X: %*s %.4s (%08X) %05d (begin: %08X)\n",pos,level*2,"",
135 // &atom_type,atom_type,atom_size,pos+8);
137 switch(atom_type)
139 case 0x797063A9: /* cpy (copyright) */
141 char *data = malloc(atom_size-8);
143 fseek(f, pos+6, SEEK_SET);
144 fread(data, atom_size-8, 1, f);
145 printf(" Copyright: %s\n", data);
146 free(data);
148 break;
149 case 0x666E69A9: /* inf (information) */
151 char data[atom_size-8];
153 fread(&data, 1, atom_size-8, f);
154 printf(" Owner: %s\n", &data);
156 break;
157 case 0x6D616EA9: /* nam (name) */
159 char data[atom_size-8];
161 fread(&data, 1, atom_size-8, f);
162 printf(" Name: %s\n", &data);
164 break;
167 fseek(f,orig_pos,SEEK_SET);
169 #endif
171 int time_scale = 0;
173 static void lschunks(FILE *f,int level,unsigned int endpos){
174 unsigned int atom_size;
175 unsigned int atom_type;
176 int pos;
178 while(endpos==0 || ftell(f)<endpos){
179 pos=ftell(f);
180 atom_size=read_dword(f);// if(fread(&atom_size_b,4,1,f)<=0) break;
181 if(fread(&atom_type,4,1,f)<=0) break;
183 if(atom_size<8) break; // error
185 printf("%08X: %*s %.4s (%08X) %05d [%s] (begin: %08X)\n",pos,level*2,"",&atom_type,atom_type,atom_size,
186 atom2human_type(atom_type), pos+8); // 8: atom_size fields (4) + atom_type fields (4)
188 #ifndef NO_SPECIAL
189 // if (atom_type == 0x61746475)
190 // userdata_info(f, atom_size, pos, level);
192 if (atom_type == 0x6468646D)
194 char data[4];
196 fread(&data, 1, 1, f); // char
197 printf("mdhd version %d\n", data[0]);
198 fread(&data, 3, 1, f); // int24
199 fread(&data, 4, 1, f); // int32
200 fread(&data, 4, 1, f); // int32
201 fread(&data, 4, 1, f); // int32
202 time_scale = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
203 printf("timescale: %d\n", time_scale);
204 fread(&data, 4, 1, f); // int32
205 fread(&data, 2, 1, f); // int16
206 fread(&data, 2, 1, f); // int16
209 if (atom_type == 0x64686D76)
211 stream = S_VIDEO;
212 printf(" Found VIDEO Stream #%d\n", v_stream++);
215 if (atom_type == 0x64686D73)
217 stream = S_AUDIO;
218 printf(" Found AUDIO Stream #%d\n", a_stream++);
221 if (atom_type == 0x64686B74) // tkhd - track header
223 int i;
224 unsigned char data[atom_size];
225 int x, y;
227 for (i=0; i<atom_size; i++)
228 fread(&data[i], 1, 1, f);
230 x = data[77];
231 y = data[81];
232 printf(" Flags: %d\n", data[3]);
233 printf(" Picture size: %dx%d\n", x, y);
234 if (x == 0 && y == 0)
235 printf(" Possible audio stream!\n");
238 if(atom_type==0x64737473) { // stsd
239 unsigned int tmp;
240 unsigned int count;
241 int i;
242 fread(&tmp,4,1,f);
243 count=read_dword(f);// fread(&count,4,1,f);
244 printf("desc count = %d\n",count);
245 for(i=0;i<count;i++){
246 unsigned int len;
247 unsigned int format;
248 len=read_dword(f); // fread(&len,4,1,f);
249 fread(&format,4,1,f);
250 printf(" desc #%d: %.4s (%d)\n",i+1,&format,len);
251 if (stream == S_VIDEO)
252 video_stream_info(f, len);
253 if (stream == S_AUDIO)
254 audio_stream_info(f, len);
255 fseek(f,len-8,SEEK_CUR);
259 if(atom_type==0x6F637473) { // stco
260 int len,i;
261 read_dword(f);
262 len=read_dword(f);
263 printf("Chunk table size :%d\n",len);
264 for(i=0;i<len;i++) printf(" chunk #%d: 0x%X\n",i+1,read_dword(f));
268 if(atom_type==0x73747473) { // stts
269 int len,i;
270 read_dword(f);
271 len=read_dword(f);
272 printf("T->S table size :%d\n",len);
273 for(i=0;i<len;i++){
274 int num=read_dword(f);
275 int dur=read_dword(f);
276 printf("%5d samples: %d duration", num, dur);
277 if (stream == S_AUDIO)
278 printf("(rate: %f Hz)\n", (float)time_scale/dur);
279 else
280 printf("(fps: %f)\n", (float)time_scale/dur);
284 if(atom_type==0x63737473) { // stsc
285 int len,i;
286 read_dword(f);
287 len=read_dword(f);
288 printf("S->C table size :%d\n",len);
289 for(i=0;i<len;i++){
290 int first=read_dword(f);
291 int spc=read_dword(f);
292 int sdid=read_dword(f);
293 printf(" chunk %d... %d s/c desc: %d\n",first,spc,sdid);
297 if(atom_type==0x7A737473) { // stsz
298 int len,i,ss;
299 read_dword(f);
300 ss=read_dword(f);
301 len=read_dword(f);
302 printf("Sample size table len: %d\n",len);
303 if(ss){
304 printf(" common sample size: %d bytes\n",ss);
305 } else {
306 for(i=0;i<len;i++) printf(" sample #%d: %d bytes\n",i+1,read_dword(f));
309 #endif
311 #if 1
312 switch(atom_type){
313 case 0x7461646D: // mdat Movie data
314 case 0x75716D72: // rmqu
315 case 0x65657266: // free JUNK
316 case 0x64686B74: // tkhd Track header
317 case 0x61746475: // udta User data
318 case 0x64737473: // stsd Sample description
319 case 0x6F637473: // stco Chunk offset table
320 case 0x73747473: // stts Sample time table
321 case 0x63737473: // stsc Sample->Chunk mapping table
322 case 0x7A737473: // stsz Sample size table
323 case 0x746f6e70: // pnot
324 case 0x54434950: // PICT
325 case 0x70797466:
326 break;
327 default: lschunks(f,level+1,pos+atom_size);
329 #else
330 switch(atom_type){
331 case 0x766F6F6D: // moov
332 case 0x61726D72: // rmra
333 case 0x61646D72: // rmda
334 lschunks(f,level+1,pos+atom_size);
336 #endif
337 fseek(f,pos+atom_size,SEEK_SET);
341 int main(int argc,char* argv[])
343 FILE *f;
345 if ((f = fopen(argc>1?argv[1]:"Akira.mov","rb")) == NULL)
346 return 1;
348 printf("%.8s %.4s (%.8s) %5s [%s]\n\n",
349 "position", "atom", "atomtype", "len", "human readable atom name");
351 lschunks(f, 0, 0);
353 printf("\nSummary: streams: %d video/%d audio\n", v_stream, a_stream);
355 return 0;