Fix:
[mplayer/glamo.git] / libmpdemux / demux_mov.c
blob9835f80b0d1b1aed773e3ac9a6ff81202c5f4f03
1 // QuickTime MOV file parser by A'rpi
2 // additional work by Atmos
3 // based on TOOLS/movinfo.c by A'rpi & Al3x
4 // compressed header support from moov.c of the openquicktime lib.
5 // References: http://openquicktime.sf.net/, http://www.heroinewarrior.com/
6 // http://www.geocities.com/SiliconValley/Lakes/2160/fformats/files/mov.pdf
7 // (above url no longer works, file mirrored somewhere? ::atmos)
8 // The QuickTime File Format PDF from Apple:
9 // http://developer.apple.com/techpubs/quicktime/qtdevdocs/PDF/QTFileFormat.pdf
10 // (Complete list of documentation at http://developer.apple.com/quicktime/)
11 // MP4-Lib sources from http://mpeg4ip.sf.net/ might be usefull fot .mp4
12 // aswell as .mov specific stuff.
13 // All sort of Stuff about MPEG4:
14 // http://www.cmlab.csie.ntu.edu.tw/~pkhsiao/thesis.html
15 // I really recommend N4270-1.doc and N4270-2.doc which are exact specs
16 // of the MP4-File Format and the MPEG4 Specific extensions. ::atmos
17 // TSGS#15(02)0088
18 // http://www.3gpp.org/ftp/tsg_sa/TSG_SA/TSGS_15/Docs/pdf/SP-020088.pdf
19 // http://www.3gpp2.org/Public_html/specs/C.S0050-0_v1.0_121503.pdf
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
25 #include "config.h"
26 #include "mp_msg.h"
27 #include "help_mp.h"
29 #include "stream.h"
30 #include "demuxer.h"
31 #include "stheader.h"
33 #include "bswap.h"
35 #include "qtpalette.h"
36 #include "parse_mp4.h" // .MP4 specific stuff
38 #ifdef MACOSX
39 #include <QuickTime/QuickTime.h>
40 #include <QuickTime/ImageCompression.h>
41 #include <QuickTime/ImageCodec.h>
42 #else
43 #include "loader/qtx/qtxsdk/components.h"
44 #endif
46 #ifdef HAVE_ZLIB
47 #include <zlib.h>
48 #endif
50 #ifndef _FCNTL_H
51 #include <fcntl.h>
52 #endif
54 #define BE_16(x) (((unsigned char *)(x))[0] << 8 | \
55 ((unsigned char *)(x))[1])
56 #define BE_32(x) (((unsigned char *)(x))[0] << 24 | \
57 ((unsigned char *)(x))[1] << 16 | \
58 ((unsigned char *)(x))[2] << 8 | \
59 ((unsigned char *)(x))[3])
61 #define char2short(x,y) BE_16(&(x)[(y)])
62 #define char2int(x,y) BE_32(&(x)[(y)])
64 typedef struct {
65 unsigned int pts; // duration
66 unsigned int size;
67 off_t pos;
68 } mov_sample_t;
70 typedef struct {
71 unsigned int sample; // number of the first sample in the chunk
72 unsigned int size; // number of samples in the chunk
73 int desc; // for multiple codecs mode - not used
74 off_t pos;
75 } mov_chunk_t;
77 typedef struct {
78 unsigned int first;
79 unsigned int spc;
80 unsigned int sdid;
81 } mov_chunkmap_t;
83 typedef struct {
84 unsigned int num;
85 unsigned int dur;
86 } mov_durmap_t;
88 typedef struct {
89 unsigned int dur;
90 unsigned int pos;
91 int speed;
93 int frames;
94 int start_sample;
95 int start_frame;
96 int pts_offset;
97 } mov_editlist_t;
99 #define MOV_TRAK_UNKNOWN 0
100 #define MOV_TRAK_VIDEO 1
101 #define MOV_TRAK_AUDIO 2
102 #define MOV_TRAK_FLASH 3
103 #define MOV_TRAK_GENERIC 4
104 #define MOV_TRAK_CODE 5
106 typedef struct {
107 int id;
108 int type;
109 off_t pos;
111 unsigned int media_handler;
112 unsigned int data_handler;
114 int timescale;
115 unsigned int length;
116 int samplesize; // 0 = variable
117 int duration; // 0 = variable
118 int width,height; // for video
119 unsigned int fourcc;
120 unsigned int nchannels;
121 unsigned int samplebytes;
123 int tkdata_len; // track data
124 unsigned char* tkdata;
125 int stdata_len; // stream data
126 unsigned char* stdata;
128 unsigned char* stream_header;
129 int stream_header_len; // if >0, this header should be sent before the 1st frame
131 int samples_size;
132 mov_sample_t* samples;
133 int chunks_size;
134 mov_chunk_t* chunks;
135 int chunkmap_size;
136 mov_chunkmap_t* chunkmap;
137 int durmap_size;
138 mov_durmap_t* durmap;
139 int keyframes_size;
140 unsigned int* keyframes;
141 int editlist_size;
142 mov_editlist_t* editlist;
143 int editlist_pos;
145 void* desc; // image/sound/etc description (pointer to ImageDescription etc)
146 } mov_track_t;
148 #ifndef SIZE_MAX
149 #define SIZE_MAX ((size_t)-1)
150 #endif
152 void *realloc_struct(void *ptr, size_t nmemb, size_t size) {
153 if (nmemb > SIZE_MAX / size)
154 return NULL;
155 return realloc(ptr, nmemb * size);
158 void mov_build_index(mov_track_t* trak,int timescale){
159 int i,j,s;
160 int last=trak->chunks_size;
161 unsigned int pts=0;
163 #if 0
164 if (trak->chunks_size <= 0)
166 mp_msg(MSGT_DEMUX, MSGL_WARN, "No chunk offset table, trying to build one!\n");
168 trak->chunks_size = trak->samples_size; /* XXX: FIXME ! */
169 trak->chunks = realloc(trak->chunks, sizeof(mov_chunk_t)*trak->chunks_size);
171 for (i=0; i < trak->chunks_size; i++)
172 trak->chunks[i].pos = -1;
174 #endif
176 mp_msg(MSGT_DEMUX, MSGL_V, "MOV track #%d: %d chunks, %d samples\n",trak->id,trak->chunks_size,trak->samples_size);
177 mp_msg(MSGT_DEMUX, MSGL_V, "pts=%d scale=%d time=%5.3f\n",trak->length,trak->timescale,(float)trak->length/(float)trak->timescale);
179 // process chunkmap:
180 i=trak->chunkmap_size;
181 while(i>0){
182 --i;
183 for(j=trak->chunkmap[i].first;j<last;j++){
184 trak->chunks[j].desc=trak->chunkmap[i].sdid;
185 trak->chunks[j].size=trak->chunkmap[i].spc;
187 last=trak->chunkmap[i].first;
190 #if 0
191 for (i=0; i < trak->chunks_size; i++)
193 /* fixup position */
194 if (trak->chunks[i].pos == -1)
195 if (i > 0)
196 trak->chunks[i].pos = trak->chunks[i-1].pos + trak->chunks[i-1].size;
197 else
198 trak->chunks[i].pos = 0; /* FIXME: set initial pos */
199 #endif
201 // calc pts of chunks:
202 s=0;
203 for(j=0;j<trak->chunks_size;j++){
204 trak->chunks[j].sample=s;
205 s+=trak->chunks[j].size;
207 i = 0;
208 for (j = 0; j < trak->durmap_size; j++)
209 i += trak->durmap[j].num;
210 if (i != s) {
211 mp_msg(MSGT_DEMUX, MSGL_WARN,
212 "MOV: durmap and chunkmap sample count differ (%i vs %i)\n", i, s);
213 if (i > s) s = i;
216 // workaround for fixed-size video frames (dv and uncompressed)
217 if(!trak->samples_size && trak->type!=MOV_TRAK_AUDIO){
218 trak->samples_size=s;
219 trak->samples=calloc(s, sizeof(mov_sample_t));
220 for(i=0;i<s;i++)
221 trak->samples[i].size=trak->samplesize;
222 trak->samplesize=0;
225 if(!trak->samples_size){
226 // constant sampesize
227 if(trak->durmap_size==1 || (trak->durmap_size==2 && trak->durmap[1].num==1)){
228 trak->duration=trak->durmap[0].dur;
229 } else mp_msg(MSGT_DEMUX, MSGL_ERR, "*** constant samplesize & variable duration not yet supported! ***\nContact the author if you have such sample file!\n");
230 return;
233 if (trak->samples_size < s) {
234 mp_msg(MSGT_DEMUX, MSGL_WARN,
235 "MOV: durmap or chunkmap bigger than sample count (%i vs %i)\n",
236 s, trak->samples_size);
237 trak->samples_size = s;
238 trak->samples = realloc_struct(trak->samples, s, sizeof(mov_sample_t));
241 // calc pts:
242 s=0;
243 for(j=0;j<trak->durmap_size;j++){
244 for(i=0;i<trak->durmap[j].num;i++){
245 trak->samples[s].pts=pts;
246 ++s;
247 pts+=trak->durmap[j].dur;
251 // calc sample offsets
252 s=0;
253 for(j=0;j<trak->chunks_size;j++){
254 off_t pos=trak->chunks[j].pos;
255 for(i=0;i<trak->chunks[j].size;i++){
256 trak->samples[s].pos=pos;
257 mp_msg(MSGT_DEMUX, MSGL_DBG3, "Sample %5d: pts=%8d off=0x%08X size=%d\n",s,
258 trak->samples[s].pts,
259 (int)trak->samples[s].pos,
260 trak->samples[s].size);
261 pos+=trak->samples[s].size;
262 ++s;
266 // precalc editlist entries
267 if(trak->editlist_size>0){
268 int frame=0;
269 int e_pts=0;
270 for(i=0;i<trak->editlist_size;i++){
271 mov_editlist_t* el=&trak->editlist[i];
272 int sample=0;
273 int pts=el->pos;
274 el->start_frame=frame;
275 if(pts<0){
276 // skip!
277 el->frames=0; continue;
279 // find start sample
280 for(;sample<trak->samples_size;sample++){
281 if(pts<=trak->samples[sample].pts) break;
283 el->start_sample=sample;
284 el->pts_offset=((long long)e_pts*(long long)trak->timescale)/(long long)timescale-trak->samples[sample].pts;
285 pts+=((long long)el->dur*(long long)trak->timescale)/(long long)timescale;
286 e_pts+=el->dur;
287 // find end sample
288 for(;sample<trak->samples_size;sample++){
289 if(pts<trak->samples[sample].pts) break;
291 el->frames=sample-el->start_sample;
292 frame+=el->frames;
293 mp_msg(MSGT_DEMUX,MSGL_V,"EL#%d: pts=%d 1st_sample=%d frames=%d (%5.3fs) pts_offs=%d\n",i,
294 el->pos,el->start_sample, el->frames,
295 (float)(el->dur)/(float)timescale, el->pts_offset);
301 #define MOV_MAX_TRACKS 256
303 typedef struct {
304 off_t moov_start;
305 off_t moov_end;
306 off_t mdat_start;
307 off_t mdat_end;
308 int track_db;
309 mov_track_t* tracks[MOV_MAX_TRACKS];
310 int timescale; // movie timescale
311 int duration; // movie duration (in movie timescale units)
312 } mov_priv_t;
314 #define MOV_FOURCC(a,b,c,d) ((a<<24)|(b<<16)|(c<<8)|(d))
316 static int mov_check_file(demuxer_t* demuxer){
317 int flags=0;
318 int no=0;
319 mov_priv_t* priv=malloc(sizeof(mov_priv_t));
321 mp_msg(MSGT_DEMUX,MSGL_V,"Checking for MOV\n");
323 memset(priv,0,sizeof(mov_priv_t));
325 while(1){
326 int i;
327 int skipped=8;
328 off_t len=stream_read_dword(demuxer->stream);
329 unsigned int id=stream_read_dword(demuxer->stream);
330 if(stream_eof(demuxer->stream)) break; // EOF
331 if (len == 1) /* real size is 64bits - cjb */
333 #ifndef _LARGEFILE_SOURCE
334 if (stream_read_dword(demuxer->stream) != 0)
335 mp_msg(MSGT_DEMUX, MSGL_WARN, "64bit file, but you've compiled MPlayer without LARGEFILE support!\n");
336 len = stream_read_dword(demuxer->stream);
337 #else
338 len = stream_read_qword(demuxer->stream);
339 #endif
340 skipped += 8;
342 #if 0
343 else if (len == 0) /* deleted chunk */
345 /* XXX: CJB! is this right? - alex */
346 goto skip_chunk;
348 #endif
349 else if(len<8) break; // invalid chunk
351 switch(id){
352 case MOV_FOURCC('f','t','y','p'): {
353 unsigned int tmp;
354 // File Type Box (ftyp):
355 // char[4] major_brand (eg. 'isom')
356 // int minor_version (eg. 0x00000000)
357 // char[4] compatible_brands[] (eg. 'mp41')
358 // compatible_brands list spans to the end of box
359 #if 1
360 tmp = stream_read_dword(demuxer->stream);
361 switch(tmp) {
362 case MOV_FOURCC('i','s','o','m'):
363 mp_msg(MSGT_DEMUX,MSGL_V,"ISO: File Type Major Brand: ISO Base Media\n");
364 break;
365 case MOV_FOURCC('m','p','4','1'):
366 mp_msg(MSGT_DEMUX,MSGL_INFO,"ISO: File Type Major Brand: ISO/IEC 14496-1 (MPEG-4 system) v1\n");
367 break;
368 case MOV_FOURCC('m','p','4','2'):
369 mp_msg(MSGT_DEMUX,MSGL_INFO,"ISO: File Type Major Brand: ISO/IEC 14496-1 (MPEG-4 system) v2\n");
370 break;
371 case MOV_FOURCC('M','4','A',' '):
372 mp_msg(MSGT_DEMUX,MSGL_INFO,"ISO: File Type Major Brand: Apple iTunes AAC-LC Audio\n");
373 break;
374 case MOV_FOURCC('M','4','P',' '):
375 mp_msg(MSGT_DEMUX,MSGL_INFO,"ISO: File Type Major Brand: Apple iTunes AAC-LC Protected Audio\n");
376 break;
377 case MOV_FOURCC('q','t',' ',' '):
378 mp_msg(MSGT_DEMUX,MSGL_INFO,"ISO: File Type Major Brand: Original QuickTime\n");
379 break;
380 case MOV_FOURCC('3','g','p','1'):
381 mp_msg(MSGT_DEMUX,MSGL_INFO,"ISO: File Type Major Brand: 3GPP Profile 1\n");
382 break;
383 case MOV_FOURCC('3','g','p','2'):
384 case MOV_FOURCC('3','g','2','a'):
385 mp_msg(MSGT_DEMUX,MSGL_INFO,"ISO: File Type Major Brand: 3GPP Profile 2\n");
386 break;
387 case MOV_FOURCC('3','g','p','3'):
388 mp_msg(MSGT_DEMUX,MSGL_INFO,"ISO: File Type Major Brand: 3GPP Profile 3\n");
389 break;
390 case MOV_FOURCC('3','g','p','4'):
391 mp_msg(MSGT_DEMUX,MSGL_INFO,"ISO: File Type Major Brand: 3GPP Profile 4\n");
392 break;
393 case MOV_FOURCC('3','g','p','5'):
394 mp_msg(MSGT_DEMUX,MSGL_INFO,"ISO: File Type Major Brand: 3GPP Profile 5\n");
395 break;
396 case MOV_FOURCC('m','m','p','4'):
397 mp_msg(MSGT_DEMUX,MSGL_INFO,"ISO: File Type Major Brand: Mobile ISO/IEC 14496-1 (MPEG-4 system)\n");
398 break;
399 default:
400 tmp = be2me_32(tmp);
401 mp_msg(MSGT_DEMUX,MSGL_WARN,"ISO: Unknown File Type Major Brand: %.4s\n",(char *)&tmp);
403 mp_msg(MSGT_DEMUX,MSGL_V,"ISO: File Type Minor Version: %d\n",
404 stream_read_dword(demuxer->stream));
405 skipped += 8;
406 // List all compatible brands
407 for(i = 0; i < ((len-16)/4); i++) {
408 tmp = be2me_32(stream_read_dword(demuxer->stream));
409 mp_msg(MSGT_DEMUX,MSGL_V,"ISO: File Type Compatible Brand #%d: %.4s\n",i,(char *)&tmp);
410 skipped += 4;
412 #endif
413 } break;
414 case MOV_FOURCC('m','o','o','v'):
415 // case MOV_FOURCC('c','m','o','v'):
416 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: Movie header found!\n");
417 priv->moov_start=(off_t)stream_tell(demuxer->stream);
418 priv->moov_end=(off_t)priv->moov_start+len-skipped;
419 mp_msg(MSGT_DEMUX,MSGL_DBG2,"MOV: Movie header: start: %"PRIx64" end: %"PRIx64"\n",
420 (int64_t)priv->moov_start, (int64_t)priv->moov_end);
421 skipped+=8;
422 i = stream_read_dword(demuxer->stream)-8;
423 if(stream_read_dword(demuxer->stream)==MOV_FOURCC('r','m','r','a')){
424 int ref=0;
425 skipped+=i;
426 mp_msg(MSGT_DEMUX,MSGL_INFO,"MOV: Reference Media file!!!\n");
427 //set demuxer type to playlist ...
428 demuxer->type=DEMUXER_TYPE_PLAYLIST;
429 while(i>0){
430 int len=stream_read_dword(demuxer->stream)-8;
431 int fcc=stream_read_dword(demuxer->stream);
432 if(len<0) break; // EOF!?
433 i-=8;
434 // printf("i=%d len=%d\n",i,len);
435 switch(fcc){
436 case MOV_FOURCC('r','m','d','a'):
437 continue;
438 case MOV_FOURCC('r','d','r','f'): {
439 int tmp=stream_read_dword(demuxer->stream);
440 int type=stream_read_dword_le(demuxer->stream);
441 int slen=stream_read_dword(demuxer->stream);
442 //char* s=malloc(slen+1);
443 //stream_read(demuxer->stream,s,slen);
445 //FIXME: also store type & data_rate ?
446 ds_read_packet(demuxer->video,
447 demuxer->stream,
448 slen,
450 stream_tell(demuxer->stream),
451 0 // no flags
453 flags|=4;
454 mp_msg(MSGT_DEMUX,MSGL_V,"Added reference to playlist\n");
455 //s[slen]=0;
456 //mp_msg(MSGT_DEMUX,MSGL_INFO,"REF: [%.4s] %s\n",&type,s);
457 len-=12+slen;i-=12+slen; break;
459 case MOV_FOURCC('r','m','d','r'): {
460 int flags=stream_read_dword(demuxer->stream);
461 int rate=stream_read_dword(demuxer->stream);
462 mp_msg(MSGT_DEMUX,MSGL_V," min. data rate: %d bits/sec\n",rate);
463 len-=8; i-=8; break;
465 case MOV_FOURCC('r','m','q','u'): {
466 int q=stream_read_dword(demuxer->stream);
467 mp_msg(MSGT_DEMUX,MSGL_V," quality index: %d\n",q);
468 len-=4; i-=4; break;
471 i-=len;stream_skip(demuxer->stream,len);
474 flags|=1;
475 break;
476 case MOV_FOURCC('w','i','d','e'):
477 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: 'WIDE' chunk found!\n");
478 if(flags&2) break;
479 case MOV_FOURCC('m','d','a','t'):
480 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: Movie DATA found!\n");
481 priv->mdat_start=stream_tell(demuxer->stream);
482 priv->mdat_end=priv->mdat_start+len-skipped;
483 mp_msg(MSGT_DEMUX,MSGL_DBG2,"MOV: Movie data: start: %"PRIx64" end: %"PRIx64"\n",
484 (int64_t)priv->mdat_start, (int64_t)priv->mdat_end);
485 flags|=2;
486 if(flags==3){
487 // if we're over the headers, then we can stop parsing here!
488 demuxer->priv=priv;
489 return DEMUXER_TYPE_MOV;
491 break;
492 case MOV_FOURCC('f','r','e','e'):
493 case MOV_FOURCC('s','k','i','p'):
494 case MOV_FOURCC('j','u','n','k'):
495 mp_msg(MSGT_DEMUX,MSGL_DBG2,"MOV: free space (len: %"PRId64")\n", (int64_t)len);
496 /* unused, if you edit a mov, you can use space provided by free atoms (redefining it) */
497 break;
498 case MOV_FOURCC('p','n','o','t'):
499 case MOV_FOURCC('P','I','C','T'):
500 /* dunno what, but we shoudl ignore it */
501 break;
502 default:
503 if(no==0){ free(priv); return 0;} // first chunk is bad!
504 id = be2me_32(id);
505 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: unknown chunk: %.4s %d\n",(char *)&id,(int)len);
507 skip_chunk:
508 if(!stream_skip(demuxer->stream,len-skipped)) break;
509 ++no;
512 if(flags==3){
513 demuxer->priv=priv;
514 return DEMUXER_TYPE_MOV;
516 free(priv);
518 if ((flags==5) || (flags==7)) // reference & header sent
519 return DEMUXER_TYPE_PLAYLIST;
521 if(flags==1)
522 mp_msg(MSGT_DEMUX,MSGL_WARN,"MOV: missing data (mdat) chunk! Maybe broken file...\n");
523 else if(flags==2)
524 mp_msg(MSGT_DEMUX,MSGL_WARN,"MOV: missing header (moov/cmov) chunk! Maybe broken file...\n");
526 return 0;
529 static void demux_close_mov(demuxer_t *demuxer) {
530 mov_priv_t* priv = demuxer->priv;
531 int i;
532 if (!priv)
533 return;
534 for (i = 0; i < MOV_MAX_TRACKS; i++) {
535 mov_track_t *track = priv->tracks[i];
536 if (track) {
537 free(track->tkdata);
538 free(track->stdata);
539 free(track->stream_header);
540 free(track->samples);
541 free(track->chunks);
542 free(track->chunkmap);
543 free(track->durmap);
544 free(track->keyframes);
545 free(track->editlist);
546 free(track->desc);
547 free(track);
550 free(priv);
553 static int lschunks_intrak(demuxer_t* demuxer, int level, unsigned int id,
554 off_t pos, off_t len, mov_track_t* trak);
556 static void lschunks(demuxer_t* demuxer,int level,off_t endpos,mov_track_t* trak){
557 mov_priv_t* priv=demuxer->priv;
558 // printf("lschunks (level=%d,endpos=%x)\n", level, endpos);
559 while(1){
560 off_t pos;
561 off_t len;
562 unsigned int id;
564 pos=stream_tell(demuxer->stream);
565 // printf("stream_tell==%d\n",pos);
566 if(pos>=endpos) return; // END
567 len=stream_read_dword(demuxer->stream);
568 // printf("len==%d\n",len);
569 if(len<8) return; // error
570 len-=8;
571 id=stream_read_dword(demuxer->stream);
573 mp_msg(MSGT_DEMUX,MSGL_DBG2,"lschunks %.4s %d\n",(char *)&id,(int)len);
575 if(trak){
576 if (lschunks_intrak(demuxer, level, id, pos, len, trak) < 0)
577 return;
578 } else { /* not in track */
579 switch(id) {
580 case MOV_FOURCC('m','v','h','d'): {
581 stream_skip(demuxer->stream,12);
582 priv->timescale=stream_read_dword(demuxer->stream);
583 priv->duration=stream_read_dword(demuxer->stream);
584 mp_msg(MSGT_DEMUX, MSGL_V,"MOV: %*sMovie header (%d bytes): tscale=%d dur=%d\n",level,"",(int)len,
585 (int)priv->timescale,(int)priv->duration);
586 break;
588 case MOV_FOURCC('t','r','a','k'): {
589 // if(trak) printf("MOV: Warning! trak in trak?\n");
590 if(priv->track_db>=MOV_MAX_TRACKS){
591 mp_msg(MSGT_DEMUX,MSGL_WARN,MSGTR_MOVtooManyTrk);
592 return;
594 if(!priv->track_db) mp_msg(MSGT_DEMUX, MSGL_V, "--------------\n");
595 trak=malloc(sizeof(mov_track_t));
596 memset(trak,0,sizeof(mov_track_t));
597 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: Track #%d:\n",priv->track_db);
598 trak->id=priv->track_db;
599 priv->tracks[priv->track_db]=trak;
600 lschunks(demuxer,level+1,pos+len,trak);
601 mov_build_index(trak,priv->timescale);
602 switch(trak->type){
603 case MOV_TRAK_AUDIO: {
604 #if 0
605 struct {
606 int16_t version; // 0 or 1 (version 1 is qt3.0+)
607 int16_t revision; // 0
608 int32_t vendor_id; // 0
609 int16_t channels; // 1 or 2 (Mono/Stereo)
610 int16_t samplesize; // 8 or 16 (8Bit/16Bit)
611 int16_t compression_id; // if version 0 then 0
612 // if version 1 and vbr then -2 else 0
613 int16_t packet_size; // 0
614 uint16_t sample_rate; // samplerate (Hz)
615 // qt3.0+ (version == 1)
616 uint32_t samples_per_packet; // 0 or num uncompressed samples in a packet
617 // if 0 below three values are also 0
618 uint32_t bytes_per_packet; // 0 or num compressed bytes for one channel
619 uint32_t bytes_per_frame; // 0 or num compressed bytes for all channels
620 // (channels * bytes_per_packet)
621 uint32_t bytes_per_sample; // 0 or size of uncompressed sample
622 // if samples_per_packet and bytes_per_packet are constant (CBR)
623 // then bytes_per_frame and bytes_per_sample must be 0 (else is VBR)
624 // ---
625 // optional additional atom-based fields
626 // ([int32_t size,int32_t type,some data ],repeat)
627 } my_stdata;
628 #endif
629 int version, adjust;
630 sh_audio_t* sh=new_sh_audio(demuxer,priv->track_db);
631 sh->format=trak->fourcc;
633 switch( sh->format ) {
634 case 0x726D6173: /* samr */
635 /* amr narrowband */
636 trak->samplebytes=sh->samplesize=1;
637 trak->nchannels=sh->channels=1;
638 sh->samplerate=8000;
639 break;
641 case 0x62776173: /* sawb */
642 /* amr wideband */
643 trak->samplebytes=sh->samplesize=1;
644 trak->nchannels=sh->channels=1;
645 sh->samplerate=16000;
646 break;
648 default:
650 // assumptions for below table: short is 16bit, int is 32bit, intfp is 16bit
651 // XXX: 32bit fixed point numbers (intfp) are only 2 Byte!
652 // short values are usually one byte leftpadded by zero
653 // int values are usually two byte leftpadded by zero
654 // stdata[]:
655 // 8 short version
656 // 10 short revision
657 // 12 int vendor_id
658 // 16 short channels
659 // 18 short samplesize
660 // 20 short compression_id
661 // 22 short packet_size (==0)
662 // 24 intfp sample_rate
663 // (26 short) unknown (==0)
664 // ---- qt3.0+ (version>=1)
665 // 28 int samples_per_packet
666 // 32 int bytes_per_packet
667 // 36 int bytes_per_frame
668 // 40 int bytes_per_sample
669 // there may be additional atoms following at 28 (version 0)
670 // or 44 (version 1), eg. esds atom of .MP4 files
671 // esds atom:
672 // 28 int atom size (bytes of int size, int type and data)
673 // 32 char[4] atom type (fourc charater code -> esds)
674 // 36 char[] atom data (len=size-8)
676 // TODO: fix parsing for files using version 2.
677 version=char2short(trak->stdata,8);
678 if (version > 1)
679 mp_msg(MSGT_DEMUX, MSGL_WARN, "MOV: version %d sound atom may not parse correctly!\n", version);
680 trak->samplebytes=sh->samplesize=char2short(trak->stdata,18)/8;
682 /* I can't find documentation, but so far this is the case. -Corey */
683 switch (char2short(trak->stdata,16)) {
684 case 1:
685 trak->nchannels = 1; break;
686 case 2:
687 trak->nchannels = 2; break;
688 case 3:
689 trak->nchannels = 6; break;
690 default:
691 mp_msg(MSGT_DEMUX, MSGL_WARN,
692 "MOV: unable to determine audio channels, assuming 2 (got %d)\n",
693 char2short(trak->stdata,16));
694 trak->nchannels = 2;
696 sh->channels = trak->nchannels;
698 /*printf("MOV: timescale: %d samplerate: %d durmap: %d (%d) -> %d (%d)\n",
699 trak->timescale, char2short(trak->stdata,24), trak->durmap[0].dur,
700 trak->durmap[0].num, trak->timescale/trak->durmap[0].dur,
701 char2short(trak->stdata,24)/trak->durmap[0].dur);*/
702 sh->samplerate=char2short(trak->stdata,24);
703 if((sh->samplerate < 7000) && trak->durmap) {
704 switch(char2short(trak->stdata,24)/trak->durmap[0].dur) {
705 // TODO: add more cases.
706 case 31:
707 sh->samplerate = 32000; break;
708 case 43:
709 sh->samplerate = 44100; break;
710 case 47:
711 sh->samplerate = 48000; break;
712 default:
713 mp_msg(MSGT_DEMUX, MSGL_WARN,
714 "MOV: unable to determine audio samplerate, "
715 "assuming 44.1kHz (got %d)\n",
716 char2short(trak->stdata,24)/trak->durmap[0].dur);
717 sh->samplerate = 44100;
721 mp_msg(MSGT_DEMUX, MSGL_V, "Audio bits: %d chans: %d rate: %d\n",
722 sh->samplesize*8,sh->channels,sh->samplerate);
724 if(trak->stdata_len >= 44 && trak->stdata[9]>=1){
725 mp_msg(MSGT_DEMUX,MSGL_V,"Audio header: samp/pack=%d bytes/pack=%d bytes/frame=%d bytes/samp=%d \n",
726 char2int(trak->stdata,28),
727 char2int(trak->stdata,32),
728 char2int(trak->stdata,36),
729 char2int(trak->stdata,40));
730 if(trak->stdata_len>=44+8){
731 int len=char2int(trak->stdata,44);
732 int fcc=char2int(trak->stdata,48);
733 // we have extra audio headers!!!
734 mp_msg(MSGT_DEMUX,MSGL_V,"Audio extra header: len=%d fcc=0x%X\n",len,fcc);
735 if((len >= 4) &&
736 (char2int(trak->stdata,52) >= 12) &&
737 (char2int(trak->stdata,52+4) == MOV_FOURCC('f','r','m','a'))) {
738 switch(char2int(trak->stdata,52+8)) {
739 case MOV_FOURCC('a','l','a','c'):
740 if (len >= 36 + char2int(trak->stdata,52)) {
741 sh->codecdata_len = char2int(trak->stdata,52+char2int(trak->stdata,52));
742 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: Found alac atom (%d)!\n", sh->codecdata_len);
743 sh->codecdata = (unsigned char *)malloc(sh->codecdata_len);
744 memcpy(sh->codecdata, &trak->stdata[52+char2int(trak->stdata,52)], sh->codecdata_len);
746 break;
747 case MOV_FOURCC('i','n','2','4'):
748 case MOV_FOURCC('i','n','3','2'):
749 case MOV_FOURCC('f','l','3','2'):
750 case MOV_FOURCC('f','l','6','4'):
751 if ((len >= 22) &&
752 (char2int(trak->stdata,52+16)==MOV_FOURCC('e','n','d','a')) &&
753 (char2short(trak->stdata,52+20))) {
754 sh->format=char2int(trak->stdata,52+8);
755 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: Found little endian PCM data, reversed fourcc:%04x\n", sh->format);
757 break;
758 default:
759 if (len > 8 && len + 44 <= trak->stdata_len) {
760 sh->codecdata_len = len-8;
761 sh->codecdata = trak->stdata+44+8;
764 } else {
765 if (len > 8 && len + 44 <= trak->stdata_len) {
766 sh->codecdata_len = len-8;
767 sh->codecdata = trak->stdata+44+8;
773 switch (version) {
774 case 0:
775 adjust = 0; break;
776 case 1:
777 adjust = 48; break;
778 case 2:
779 adjust = 68; break;
780 default:
781 mp_msg(MSGT_DEMUX, MSGL_WARN, "MOV: unknown sound atom version (%d); may not work!\n", version);
782 adjust = 68;
784 if (trak->stdata_len >= 36 + adjust) {
785 int atom_len = char2int(trak->stdata,28+adjust);
786 switch(char2int(trak->stdata,32+adjust)) { // atom type
787 case MOV_FOURCC('e','s','d','s'): {
788 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: Found MPEG4 audio Elementary Stream Descriptor atom (%d)!\n", atom_len);
789 if(atom_len > 8) {
790 esds_t esds;
791 if(!mp4_parse_esds(&trak->stdata[36+adjust], atom_len-8, &esds)) {
793 sh->i_bps = esds.avgBitrate/8;
795 // printf("######## audio format = %d ########\n",esds.objectTypeId);
796 if(esds.objectTypeId==MP4OTI_MPEG1Audio || esds.objectTypeId==MP4OTI_MPEG2AudioPart3)
797 sh->format=0x55; // .mp3
799 // dump away the codec specific configuration for the AAC decoder
800 if(esds.decoderConfigLen){
801 if( (esds.decoderConfig[0]>>3) == 29 )
802 sh->format = 0x1d61346d; // request multi-channel mp3 decoder
803 sh->codecdata_len = esds.decoderConfigLen;
804 sh->codecdata = (unsigned char *)malloc(sh->codecdata_len);
805 memcpy(sh->codecdata, esds.decoderConfig, sh->codecdata_len);
808 mp4_free_esds(&esds); // freeup esds mem
809 #if 0
810 { FILE* f=fopen("esds.dat","wb");
811 fwrite(&trak->stdata[36],atom_len-8,1,f);
812 fclose(f); }
813 #endif
815 } break;
816 case MOV_FOURCC('a','l','a','c'): {
817 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: Found alac atom (%d)!\n", atom_len);
818 if(atom_len > 8) {
819 // copy all the atom (not only payload) for lavc alac decoder
820 sh->codecdata_len = atom_len;
821 sh->codecdata = (unsigned char *)malloc(sh->codecdata_len);
822 memcpy(sh->codecdata, &trak->stdata[28], sh->codecdata_len);
824 } break;
825 case MOV_FOURCC('d','a','m','r'):
826 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: Found AMR audio atom %c%c%c%c (%d)!\n", trak->stdata[32+adjust],trak->stdata[33+adjust],trak->stdata[34+adjust],trak->stdata[35+adjust], atom_len);
827 if (atom_len>14) {
828 mp_msg(MSGT_DEMUX, MSGL_V, "mov: vendor: %c%c%c%c Version: %d\n",trak->stdata[36+adjust],trak->stdata[37+adjust],trak->stdata[38+adjust], trak->stdata[39+adjust],trak->stdata[40+adjust]);
829 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: Modes set: %02x%02x\n",trak->stdata[41+adjust],trak->stdata[42+adjust]);
830 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: Mode change period: %d Frames per sample: %d\n",trak->stdata[43+adjust],trak->stdata[44+adjust]);
832 break;
833 default:
834 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: Found unknown audio atom %c%c%c%c (%d)!\n",
835 trak->stdata[32+adjust],trak->stdata[33+adjust],trak->stdata[34+adjust],trak->stdata[35+adjust],
836 atom_len);
839 mp_msg(MSGT_DEMUX, MSGL_V, "Fourcc: %.4s\n",(char *)&trak->fourcc);
840 #if 0
841 { FILE* f=fopen("stdata.dat","wb");
842 fwrite(trak->stdata,trak->stdata_len,1,f);
843 fclose(f); }
844 { FILE* f=fopen("tkdata.dat","wb");
845 fwrite(trak->tkdata,trak->tkdata_len,1,f);
846 fclose(f); }
847 #endif
848 // Emulate WAVEFORMATEX struct:
849 sh->wf=malloc(sizeof(WAVEFORMATEX));
850 memset(sh->wf,0,sizeof(WAVEFORMATEX));
851 sh->wf->nChannels=sh->channels;
852 sh->wf->wBitsPerSample=(trak->stdata[18]<<8)+trak->stdata[19];
853 // sh->wf->nSamplesPerSec=trak->timescale;
854 sh->wf->nSamplesPerSec=sh->samplerate;
855 if(trak->stdata_len >= 44 && trak->stdata[9]>=1 && char2int(trak->stdata,28)>0){
856 //Audio header: samp/pack=4096 bytes/pack=743 bytes/frame=1486 bytes/samp=2
857 sh->wf->nAvgBytesPerSec=(sh->wf->nChannels*sh->wf->nSamplesPerSec*
858 char2int(trak->stdata,32)+char2int(trak->stdata,28)/2)
859 /char2int(trak->stdata,28);
860 sh->wf->nBlockAlign=char2int(trak->stdata,36);
861 } else {
862 sh->wf->nAvgBytesPerSec=sh->wf->nChannels*sh->wf->wBitsPerSample*sh->wf->nSamplesPerSec/8;
863 // workaround for ms11 ima4
864 if (sh->format == 0x1100736d && trak->stdata_len >= 36)
865 sh->wf->nBlockAlign=char2int(trak->stdata,36);
867 // Selection:
868 // if(demuxer->audio->id==-1 || demuxer->audio->id==priv->track_db){
869 // // (auto)selected audio track:
870 // demuxer->audio->id=priv->track_db;
871 // demuxer->audio->sh=sh; sh->ds=demuxer->audio;
872 // }
873 break;
875 case MOV_TRAK_VIDEO: {
876 int i, entry;
877 int flag, start, count_flag, end, palette_count, gray;
878 int hdr_ptr = 76; // the byte just after depth
879 unsigned char *palette_map;
880 sh_video_t* sh=new_sh_video(demuxer,priv->track_db);
881 int depth;
882 sh->format=trak->fourcc;
884 if (trak->stdata_len < 78) {
885 mp_msg(MSGT_DEMUXER, MSGL_WARN,
886 "MOV: Invalid (%d bytes instead of >= 78) video trak desc\n",
887 trak->stdata_len);
888 break;
890 depth = trak->stdata[75] | (trak->stdata[74] << 8);
891 // stdata[]:
892 // 8 short version
893 // 10 short revision
894 // 12 int vendor_id
895 // 16 int temporal_quality
896 // 20 int spatial_quality
897 // 24 short width
898 // 26 short height
899 // 28 int h_dpi
900 // 32 int v_dpi
901 // 36 int 0
902 // 40 short frames_per_sample
903 // 42 char[4] compressor_name
904 // 74 short depth
905 // 76 short color_table_id
906 // additional atoms may follow,
907 // eg esds atom from .MP4 files
908 // 78 int atom size
909 // 82 char[4] atom type
910 // 86 ... atom data
912 { ImageDescription* id=malloc(8+trak->stdata_len); // safe
913 trak->desc=id;
914 id->idSize=8+trak->stdata_len;
915 // id->cType=bswap_32(trak->fourcc);
916 id->cType=le2me_32(trak->fourcc);
917 id->version=char2short(trak->stdata,8);
918 id->revisionLevel=char2short(trak->stdata,10);
919 id->vendor=char2int(trak->stdata,12);
920 id->temporalQuality=char2int(trak->stdata,16);
921 id->spatialQuality=char2int(trak->stdata,20);
922 id->width=char2short(trak->stdata,24);
923 id->height=char2short(trak->stdata,26);
924 id->hRes=char2int(trak->stdata,28);
925 id->vRes=char2int(trak->stdata,32);
926 id->dataSize=char2int(trak->stdata,36);
927 id->frameCount=char2short(trak->stdata,40);
928 memcpy(&id->name,trak->stdata+42,32);
929 id->depth=char2short(trak->stdata,74);
930 id->clutID=char2short(trak->stdata,76);
931 if(trak->stdata_len>78) memcpy(((char*)&id->clutID)+2,trak->stdata+78,trak->stdata_len-78);
932 sh->ImageDesc=id;
933 #if 0
934 { FILE *f=fopen("ImageDescription","wb");
935 fwrite(id,id->idSize,1,f);
936 fclose(f);
938 #endif
941 if(trak->stdata_len >= 86) { // extra atoms found
942 int pos=78;
943 int atom_len;
944 while(pos+8<=trak->stdata_len &&
945 (pos+(atom_len=char2int(trak->stdata,pos)))<=trak->stdata_len){
946 switch(char2int(trak->stdata,pos+4)) { // switch atom type
947 case MOV_FOURCC('g','a','m','a'):
948 // intfp with gamma value at which movie was captured
949 // can be used to gamma correct movie display
950 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: Found unsupported Gamma-Correction movie atom (%d)!\n",
951 atom_len);
952 break;
953 case MOV_FOURCC('f','i','e','l'):
954 // 2 char-values (8bit int) that specify field handling
955 // see the Apple's QuickTime Fileformat PDF for more info
956 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: Found unsupported Field-Handling movie atom (%d)!\n",
957 atom_len);
958 break;
959 case MOV_FOURCC('m','j','q','t'):
960 // Motion-JPEG default quantization table
961 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: Found unsupported MJPEG-Quantization movie atom (%d)!\n",
962 atom_len);
963 break;
964 case MOV_FOURCC('m','j','h','t'):
965 // Motion-JPEG default huffman table
966 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: Found unsupported MJPEG-Huffman movie atom (%d)!\n",
967 atom_len);
968 break;
969 case MOV_FOURCC('e','s','d','s'):
970 // MPEG4 Elementary Stream Descriptor header
971 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: Found MPEG4 movie Elementary Stream Descriptor atom (%d)!\n", atom_len);
972 // add code here to save esds header of length atom_len-8
973 // beginning at stdata[86] to some variable to pass it
974 // on to the decoder ::atmos
975 if(atom_len > 8) {
976 esds_t esds;
977 if(!mp4_parse_esds(trak->stdata+pos+8, atom_len-8, &esds)) {
979 if(esds.objectTypeId==MP4OTI_MPEG2VisualSimple || esds.objectTypeId==MP4OTI_MPEG2VisualMain ||
980 esds.objectTypeId==MP4OTI_MPEG2VisualSNR || esds.objectTypeId==MP4OTI_MPEG2VisualSpatial ||
981 esds.objectTypeId==MP4OTI_MPEG2VisualHigh || esds.objectTypeId==MP4OTI_MPEG2Visual422)
982 sh->format=mmioFOURCC('m', 'p', 'g', '2');
983 else if(esds.objectTypeId==MP4OTI_MPEG1Visual)
984 sh->format=mmioFOURCC('m', 'p', 'g', '1');
986 // dump away the codec specific configuration for the AAC decoder
987 trak->stream_header_len = esds.decoderConfigLen;
988 trak->stream_header = (unsigned char *)malloc(trak->stream_header_len);
989 memcpy(trak->stream_header, esds.decoderConfig, trak->stream_header_len);
991 mp4_free_esds(&esds); // freeup esds mem
993 break;
994 case MOV_FOURCC('a','v','c','C'):
995 // AVC decoder configuration record
996 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: AVC decoder configuration record atom (%d)!\n", atom_len);
997 if(atom_len > 8) {
998 int i, poffs, cnt;
999 // Parse some parts of avcC, just for fun :)
1000 // real parsing is done by avc1 decoder
1001 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: avcC version: %d\n", *(trak->stdata+pos+8));
1002 if (*(trak->stdata+pos+8) != 1)
1003 mp_msg(MSGT_DEMUX, MSGL_ERR, "MOV: unknown avcC version (%d). Expexct problems.\n", *(trak->stdata+pos+9));
1004 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: avcC profile: %d\n", *(trak->stdata+pos+9));
1005 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: avcC profile compatibility: %d\n", *(trak->stdata+pos+10));
1006 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: avcC level: %d\n", *(trak->stdata+pos+11));
1007 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: avcC nal length size: %d\n", ((*(trak->stdata+pos+12))&0x03)+1);
1008 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: avcC number of sequence param sets: %d\n", cnt = (*(trak->stdata+pos+13) & 0x1f));
1009 poffs = pos + 14;
1010 for (i = 0; i < cnt; i++) {
1011 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: avcC sps %d have length %d\n", i, BE_16(trak->stdata+poffs));
1012 poffs += BE_16(trak->stdata+poffs) + 2;
1014 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: avcC number of picture param sets: %d\n", *(trak->stdata+poffs));
1015 poffs++;
1016 for (i = 0; i < cnt; i++) {
1017 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: avcC pps %d have length %d\n", i, BE_16(trak->stdata+poffs));
1018 poffs += BE_16(trak->stdata+poffs) + 2;
1020 // Copy avcC for the AVC decoder
1021 // This data will be put in extradata below, where BITMAPINFOHEADER is created
1022 trak->stream_header_len = atom_len-8;
1023 trak->stream_header = (unsigned char *)malloc(trak->stream_header_len);
1024 memcpy(trak->stream_header, trak->stdata+pos+8, trak->stream_header_len);
1026 break;
1027 case MOV_FOURCC('d','2','6','3'):
1028 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: Found H.263 decoder atom %c%c%c%c (%d)!\n", trak->stdata[pos+4],trak->stdata[pos+5],trak->stdata[pos+6],trak->stdata[pos+7],atom_len);
1029 if (atom_len>10)
1030 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: Vendor: %c%c%c%c H.263 level: %d H.263 profile: %d \n", trak->stdata[pos+8],trak->stdata[pos+9],trak->stdata[pos+10],trak->stdata[pos+11],trak->stdata[pos+12],trak->stdata[pos+13]);
1031 break;
1032 case 0:
1033 break;
1034 default:
1035 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: Found unknown movie atom %c%c%c%c (%d)!\n",
1036 trak->stdata[pos+4],trak->stdata[pos+5],trak->stdata[pos+6],trak->stdata[pos+7],
1037 atom_len);
1039 if(atom_len<8) break;
1040 pos+=atom_len;
1041 // printf("pos=%d max=%d\n",pos,trak->stdata_len);
1044 sh->fps=trak->timescale/
1045 ((trak->durmap_size>=1)?(float)trak->durmap[0].dur:1);
1046 sh->frametime=1.0f/sh->fps;
1048 sh->disp_w=trak->stdata[25]|(trak->stdata[24]<<8);
1049 sh->disp_h=trak->stdata[27]|(trak->stdata[26]<<8);
1050 // if image size is zero, fallback to display size
1051 if(!sh->disp_w && !sh->disp_h) {
1052 sh->disp_w=trak->tkdata[77]|(trak->tkdata[76]<<8);
1053 sh->disp_h=trak->tkdata[81]|(trak->tkdata[80]<<8);
1054 } else if(sh->disp_w!=(trak->tkdata[77]|(trak->tkdata[76]<<8))){
1055 // codec and display width differ... use display one for aspect
1056 sh->aspect=trak->tkdata[77]|(trak->tkdata[76]<<8);
1057 sh->aspect/=trak->tkdata[81]|(trak->tkdata[80]<<8);
1060 if(depth>32+8) mp_msg(MSGT_DEMUX, MSGL_INFO,"*** depth = 0x%X\n",depth);
1062 // palettized?
1063 gray = 0;
1064 if (depth > 32) { depth&=31; gray = 1; } // depth > 32 means grayscale
1065 if ((depth == 2) || (depth == 4) || (depth == 8))
1066 palette_count = (1 << depth);
1067 else
1068 palette_count = 0;
1070 // emulate BITMAPINFOHEADER:
1071 if (palette_count)
1073 sh->bih=malloc(sizeof(BITMAPINFOHEADER) + palette_count * 4);
1074 memset(sh->bih,0,sizeof(BITMAPINFOHEADER) + palette_count * 4);
1075 sh->bih->biSize=40 + palette_count * 4;
1076 // fetch the relevant fields
1077 flag = BE_16(&trak->stdata[hdr_ptr]);
1078 hdr_ptr += 2;
1079 start = BE_32(&trak->stdata[hdr_ptr]);
1080 hdr_ptr += 4;
1081 count_flag = BE_16(&trak->stdata[hdr_ptr]);
1082 hdr_ptr += 2;
1083 end = BE_16(&trak->stdata[hdr_ptr]);
1084 hdr_ptr += 2;
1085 palette_map = (unsigned char *)sh->bih + 40;
1086 mp_msg(MSGT_DEMUX, MSGL_V, "Allocated %d entries for palette\n",
1087 palette_count);
1088 mp_msg(MSGT_DEMUX, MSGL_DBG2, "QT palette: start: %x, end: %x, count flag: %d, flags: %x\n",
1089 start, end, count_flag, flag);
1091 /* XXX: problems with sample (statunit6.mov) with flag&0x4 set! - alex*/
1093 // load default palette
1094 if (flag & 0x08)
1096 if (gray)
1098 mp_msg(MSGT_DEMUX, MSGL_V, "Using default QT grayscale palette\n");
1099 if (palette_count == 16)
1100 memcpy(palette_map, qt_default_grayscale_palette_16, 16 * 4);
1101 else if (palette_count == 256) {
1102 memcpy(palette_map, qt_default_grayscale_palette_256, 256 * 4);
1103 if (trak->fourcc == mmioFOURCC('c','v','i','d')) {
1104 int i;
1105 // Hack for grayscale CVID, negative palette
1106 // If you have samples where this is not required contact me (rxt)
1107 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: greyscale cvid with default palette,"
1108 " enabling negative palette hack.\n");
1109 for (i = 0; i < 256 * 4; i++)
1110 palette_map[i] = palette_map[i] ^ 0xff;
1114 else
1116 mp_msg(MSGT_DEMUX, MSGL_V, "Using default QT colour palette\n");
1117 if (palette_count == 4)
1118 memcpy(palette_map, qt_default_palette_4, 4 * 4);
1119 else if (palette_count == 16)
1120 memcpy(palette_map, qt_default_palette_16, 16 * 4);
1121 else if (palette_count == 256)
1122 memcpy(palette_map, qt_default_palette_256, 256 * 4);
1125 // load palette from file
1126 else
1128 mp_msg(MSGT_DEMUX, MSGL_V, "Loading palette from file\n");
1129 for (i = start; i <= end; i++)
1131 entry = BE_16(&trak->stdata[hdr_ptr]);
1132 hdr_ptr += 2;
1133 // apparently, if count_flag is set, entry is same as i
1134 if (count_flag & 0x8000)
1135 entry = i;
1136 // only care about top 8 bits of 16-bit R, G, or B value
1137 if (entry <= palette_count && entry >= 0)
1139 palette_map[entry * 4 + 2] = trak->stdata[hdr_ptr + 0];
1140 palette_map[entry * 4 + 1] = trak->stdata[hdr_ptr + 2];
1141 palette_map[entry * 4 + 0] = trak->stdata[hdr_ptr + 4];
1142 mp_dbg(MSGT_DEMUX, MSGL_DBG2, "QT palette: added entry: %d of %d (colors: R:%x G:%x B:%x)\n",
1143 entry, palette_count,
1144 palette_map[entry * 4 + 2],
1145 palette_map[entry * 4 + 1],
1146 palette_map[entry * 4 + 0]);
1148 else
1149 mp_msg(MSGT_DEMUX, MSGL_V, "QT palette: skipped entry (out of count): %d of %d\n",
1150 entry, palette_count);
1151 hdr_ptr += 6;
1155 else
1157 if (trak->fourcc == mmioFOURCC('a','v','c','1')) {
1158 if (trak->stream_header_len > 0xffffffff - sizeof(BITMAPINFOHEADER)) {
1159 mp_msg(MSGT_DEMUXER, MSGL_ERR, "Invalid extradata size %d, skipping\n",trak->stream_header_len);
1160 trak->stream_header_len = 0;
1162 sh->bih=malloc(sizeof(BITMAPINFOHEADER) + trak->stream_header_len);
1163 memset(sh->bih,0,sizeof(BITMAPINFOHEADER) + trak->stream_header_len);
1164 sh->bih->biSize=40 + trak->stream_header_len;
1165 memcpy(((unsigned char *)sh->bih)+40, trak->stream_header, trak->stream_header_len);
1166 free (trak->stream_header);
1167 trak->stream_header_len = 0;
1168 trak->stream_header = NULL;
1169 } else {
1170 sh->bih=malloc(sizeof(BITMAPINFOHEADER));
1171 memset(sh->bih,0,sizeof(BITMAPINFOHEADER));
1172 sh->bih->biSize=40;
1175 sh->bih->biWidth=sh->disp_w;
1176 sh->bih->biHeight=sh->disp_h;
1177 sh->bih->biPlanes=0;
1178 sh->bih->biBitCount=depth;
1179 sh->bih->biCompression=trak->fourcc;
1180 sh->bih->biSizeImage=sh->bih->biWidth*sh->bih->biHeight;
1182 mp_msg(MSGT_DEMUX, MSGL_V, "Image size: %d x %d (%d bpp)\n",sh->disp_w,sh->disp_h,sh->bih->biBitCount);
1183 if(trak->tkdata_len>81)
1184 mp_msg(MSGT_DEMUX, MSGL_V, "Display size: %d x %d\n",
1185 trak->tkdata[77]|(trak->tkdata[76]<<8),
1186 trak->tkdata[81]|(trak->tkdata[80]<<8));
1187 mp_msg(MSGT_DEMUX, MSGL_V, "Fourcc: %.4s Codec: '%.*s'\n",(char *)&trak->fourcc,trak->stdata[42]&31,trak->stdata+43);
1189 // if(demuxer->video->id==-1 || demuxer->video->id==priv->track_db){
1190 // // (auto)selected video track:
1191 // demuxer->video->id=priv->track_db;
1192 // demuxer->video->sh=sh; sh->ds=demuxer->video;
1193 // }
1194 break;
1196 case MOV_TRAK_GENERIC:
1197 mp_msg(MSGT_DEMUX, MSGL_V, "Generic track - not completely understood! (id: %d)\n",
1198 trak->id);
1199 /* XXX: Also this contains the FLASH data */
1201 #if 0
1203 int pos = stream_tell(demuxer->stream);
1204 int i;
1205 int fd;
1206 char name[20];
1208 for (i=0; i<trak->samples_size; i++)
1210 char buf[trak->samples[i].size];
1211 stream_seek(demuxer->stream, trak->samples[i].pos);
1212 snprintf((char *)&name[0], 20, "samp%d", i);
1213 fd = open((char *)&name[0], O_CREAT|O_WRONLY);
1214 stream_read(demuxer->stream, &buf[0], trak->samples[i].size);
1215 write(fd, &buf[0], trak->samples[i].size);
1216 close(fd);
1218 for (i=0; i<trak->chunks_size; i++)
1220 char buf[trak->length];
1221 stream_seek(demuxer->stream, trak->chunks[i].pos);
1222 snprintf((char *)&name[0], 20, "chunk%d", i);
1223 fd = open((char *)&name[0], O_CREAT|O_WRONLY);
1224 stream_read(demuxer->stream, &buf[0], trak->length);
1225 write(fd, &buf[0], trak->length);
1226 close(fd);
1228 if (trak->samplesize > 0)
1230 char *buf;
1232 buf = malloc(trak->samplesize);
1233 stream_seek(demuxer->stream, trak->chunks[0].pos);
1234 snprintf((char *)&name[0], 20, "trak%d", trak->id);
1235 fd = open((char *)&name[0], O_CREAT|O_WRONLY);
1236 stream_read(demuxer->stream, buf, trak->samplesize);
1237 write(fd, buf, trak->samplesize);
1238 close(fd);
1240 stream_seek(demuxer->stream, pos);
1242 #endif
1243 break;
1244 default:
1245 mp_msg(MSGT_DEMUX, MSGL_V, "Unknown track type found (type: %d)\n", trak->type);
1246 break;
1248 mp_msg(MSGT_DEMUX, MSGL_V, "--------------\n");
1249 priv->track_db++;
1250 trak=NULL;
1251 break;
1253 #ifndef HAVE_ZLIB
1254 case MOV_FOURCC('c','m','o','v'): {
1255 mp_msg(MSGT_DEMUX,MSGL_ERR,MSGTR_MOVcomprhdr);
1256 return;
1258 #else
1259 case MOV_FOURCC('m','o','o','v'):
1260 case MOV_FOURCC('c','m','o','v'): {
1261 // mp_msg(MSGT_DEMUX,MSGL_ERR,MSGTR_MOVcomprhdr);
1262 lschunks(demuxer,level+1,pos+len,NULL);
1263 break;
1265 case MOV_FOURCC('d','c','o','m'): {
1266 // int temp=stream_read_dword(demuxer->stream);
1267 unsigned int algo=be2me_32(stream_read_dword(demuxer->stream));
1268 mp_msg(MSGT_DEMUX, MSGL_V, "Compressed header uses %.4s algo!\n",(char *)&algo);
1269 break;
1271 case MOV_FOURCC('c','m','v','d'): {
1272 // int temp=stream_read_dword(demuxer->stream);
1273 unsigned int moov_sz=stream_read_dword(demuxer->stream);
1274 unsigned int cmov_sz=len-4;
1275 unsigned char* cmov_buf;
1276 unsigned char* moov_buf;
1277 int zret;
1278 z_stream zstrm;
1279 stream_t* backup;
1281 if (moov_sz > 0xffffffff - 16) {
1282 mp_msg(MSGT_DEMUX, MSGL_ERR, "Invalid cmvd atom size %d\n", moov_sz);
1283 break;
1285 cmov_buf=malloc(cmov_sz);
1286 moov_buf=malloc(moov_sz+16);
1287 mp_msg(MSGT_DEMUX, MSGL_V, "Compressed header size: %d / %d\n",cmov_sz,moov_sz);
1289 stream_read(demuxer->stream,cmov_buf,cmov_sz);
1291 zstrm.zalloc = (alloc_func)0;
1292 zstrm.zfree = (free_func)0;
1293 zstrm.opaque = (voidpf)0;
1294 zstrm.next_in = cmov_buf;
1295 zstrm.avail_in = cmov_sz;
1296 zstrm.next_out = moov_buf;
1297 zstrm.avail_out = moov_sz;
1299 zret = inflateInit(&zstrm);
1300 if (zret != Z_OK)
1301 { mp_msg(MSGT_DEMUX, MSGL_ERR, "QT cmov: inflateInit err %d\n",zret);
1302 return;
1304 zret = inflate(&zstrm, Z_NO_FLUSH);
1305 if ((zret != Z_OK) && (zret != Z_STREAM_END))
1306 { mp_msg(MSGT_DEMUX, MSGL_ERR, "QT cmov inflate: ERR %d\n",zret);
1307 return;
1309 #if 0
1310 else {
1311 FILE *DecOut;
1312 DecOut = fopen("Out.bin", "w");
1313 fwrite(moov_buf, 1, moov_sz, DecOut);
1314 fclose(DecOut);
1316 #endif
1317 if(moov_sz != zstrm.total_out)
1318 mp_msg(MSGT_DEMUX, MSGL_WARN, "Warning! moov size differs cmov: %d zlib: %ld\n",moov_sz,zstrm.total_out);
1319 zret = inflateEnd(&zstrm);
1321 backup=demuxer->stream;
1322 demuxer->stream=new_memory_stream(moov_buf,moov_sz);
1323 stream_skip(demuxer->stream,8);
1324 lschunks(demuxer,level+1,moov_sz,NULL); // parse uncompr. 'moov'
1325 //free_stream(demuxer->stream);
1326 demuxer->stream=backup;
1327 free(cmov_buf);
1328 free(moov_buf);
1329 break;
1331 #endif
1332 case MOV_FOURCC('u','d','t','a'):
1334 unsigned int udta_id;
1335 off_t udta_len;
1336 off_t udta_size = len;
1338 mp_msg(MSGT_DEMUX, MSGL_DBG2, "mov: user data record found\n");
1339 mp_msg(MSGT_DEMUX, MSGL_V, "Quicktime Clip Info:\n");
1341 while((len > 8) && (udta_size > 8))
1343 udta_len = stream_read_dword(demuxer->stream);
1344 udta_id = stream_read_dword(demuxer->stream);
1345 udta_size -= 8;
1346 mp_msg(MSGT_DEMUX, MSGL_DBG2, "udta_id: %.4s (len: %"PRId64")\n", (char *)&udta_id, (int64_t)udta_len);
1347 switch (udta_id)
1349 case MOV_FOURCC(0xa9,'c','p','y'):
1350 case MOV_FOURCC(0xa9,'d','a','y'):
1351 case MOV_FOURCC(0xa9,'d','i','r'):
1352 /* 0xa9,'e','d','1' - '9' : edit timestamps */
1353 case MOV_FOURCC(0xa9,'f','m','t'):
1354 case MOV_FOURCC(0xa9,'i','n','f'):
1355 case MOV_FOURCC(0xa9,'p','r','d'):
1356 case MOV_FOURCC(0xa9,'p','r','f'):
1357 case MOV_FOURCC(0xa9,'r','e','q'):
1358 case MOV_FOURCC(0xa9,'s','r','c'):
1359 case MOV_FOURCC('n','a','m','e'):
1360 case MOV_FOURCC(0xa9,'n','a','m'):
1361 case MOV_FOURCC(0xa9,'A','R','T'):
1362 case MOV_FOURCC(0xa9,'c','m','t'):
1363 case MOV_FOURCC(0xa9,'a','u','t'):
1364 case MOV_FOURCC(0xa9,'s','w','r'):
1366 off_t text_len = stream_read_word(demuxer->stream);
1367 char text[text_len+2+1];
1368 stream_read(demuxer->stream, (char *)&text, text_len+2);
1369 text[text_len+2] = 0x0;
1370 switch(udta_id)
1372 case MOV_FOURCC(0xa9,'a','u','t'):
1373 demux_info_add(demuxer, "author", &text[2]);
1374 mp_msg(MSGT_DEMUX, MSGL_V, " Author: %s\n", &text[2]);
1375 break;
1376 case MOV_FOURCC(0xa9,'c','p','y'):
1377 demux_info_add(demuxer, "copyright", &text[2]);
1378 mp_msg(MSGT_DEMUX, MSGL_V, " Copyright: %s\n", &text[2]);
1379 break;
1380 case MOV_FOURCC(0xa9,'i','n','f'):
1381 mp_msg(MSGT_DEMUX, MSGL_V, " Info: %s\n", &text[2]);
1382 break;
1383 case MOV_FOURCC('n','a','m','e'):
1384 case MOV_FOURCC(0xa9,'n','a','m'):
1385 demux_info_add(demuxer, "name", &text[2]);
1386 mp_msg(MSGT_DEMUX, MSGL_V, " Name: %s\n", &text[2]);
1387 break;
1388 case MOV_FOURCC(0xa9,'A','R','T'):
1389 mp_msg(MSGT_DEMUX, MSGL_V, " Artist: %s\n", &text[2]);
1390 break;
1391 case MOV_FOURCC(0xa9,'d','i','r'):
1392 mp_msg(MSGT_DEMUX, MSGL_V, " Director: %s\n", &text[2]);
1393 break;
1394 case MOV_FOURCC(0xa9,'c','m','t'):
1395 demux_info_add(demuxer, "comments", &text[2]);
1396 mp_msg(MSGT_DEMUX, MSGL_V, " Comment: %s\n", &text[2]);
1397 break;
1398 case MOV_FOURCC(0xa9,'r','e','q'):
1399 mp_msg(MSGT_DEMUX, MSGL_V, " Requirements: %s\n", &text[2]);
1400 break;
1401 case MOV_FOURCC(0xa9,'s','w','r'):
1402 demux_info_add(demuxer, "encoder", &text[2]);
1403 mp_msg(MSGT_DEMUX, MSGL_V, " Software: %s\n", &text[2]);
1404 break;
1405 case MOV_FOURCC(0xa9,'d','a','y'):
1406 mp_msg(MSGT_DEMUX, MSGL_V, " Creation timestamp: %s\n", &text[2]);
1407 break;
1408 case MOV_FOURCC(0xa9,'f','m','t'):
1409 mp_msg(MSGT_DEMUX, MSGL_V, " Format: %s\n", &text[2]);
1410 break;
1411 case MOV_FOURCC(0xa9,'p','r','d'):
1412 mp_msg(MSGT_DEMUX, MSGL_V, " Producer: %s\n", &text[2]);
1413 break;
1414 case MOV_FOURCC(0xa9,'p','r','f'):
1415 mp_msg(MSGT_DEMUX, MSGL_V, " Performer(s): %s\n", &text[2]);
1416 break;
1417 case MOV_FOURCC(0xa9,'s','r','c'):
1418 mp_msg(MSGT_DEMUX, MSGL_V, " Source providers: %s\n", &text[2]);
1419 break;
1421 udta_size -= 4+text_len;
1422 break;
1424 /* some other shits: WLOC - window location,
1425 LOOP - looping style,
1426 SelO - play only selected frames
1427 AllF - play all frames
1429 case MOV_FOURCC('W','L','O','C'):
1430 case MOV_FOURCC('L','O','O','P'):
1431 case MOV_FOURCC('S','e','l','O'):
1432 case MOV_FOURCC('A','l','l','F'):
1433 default:
1435 if( udta_len>udta_size)
1436 udta_len=udta_size;
1438 char dump[udta_len-4];
1439 stream_read(demuxer->stream, (char *)&dump, udta_len-4-4);
1440 udta_size -= udta_len;
1445 break;
1446 } /* eof udta */
1447 default:
1448 id = be2me_32(id);
1449 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: unknown chunk: %.4s %d\n",(char *)&id,(int)len);
1450 } /* endof switch */
1451 } /* endof else */
1453 pos+=len+8;
1454 if(pos>=endpos) break;
1455 if(!stream_seek(demuxer->stream,pos)) break;
1459 static int lschunks_intrak(demuxer_t* demuxer, int level, unsigned int id,
1460 off_t pos, off_t len, mov_track_t* trak)
1462 switch(id) {
1463 case MOV_FOURCC('m','d','a','t'): {
1464 mp_msg(MSGT_DEMUX,MSGL_WARN,"Hmm, strange MOV, parsing mdat in lschunks?\n");
1465 return -1;
1467 case MOV_FOURCC('f','r','e','e'):
1468 case MOV_FOURCC('u','d','t','a'):
1469 /* here not supported :p */
1470 break;
1471 case MOV_FOURCC('t','k','h','d'): {
1472 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*sTrack header!\n", level, "");
1473 // read codec data
1474 trak->tkdata_len = len;
1475 trak->tkdata = malloc(trak->tkdata_len);
1476 stream_read(demuxer->stream, trak->tkdata, trak->tkdata_len);
1478 0 1 Version
1479 1 3 Flags
1480 4 4 Creation time
1481 8 4 Modification time
1482 12 4 Track ID
1483 16 4 Reserved
1484 20 4 Duration
1485 24 8 Reserved
1486 32 2 Layer
1487 34 2 Alternate group
1488 36 2 Volume
1489 38 2 Reserved
1490 40 36 Matrix structure
1491 76 4 Track width
1492 80 4 Track height
1494 mp_msg(MSGT_DEMUX, MSGL_V,
1495 "tkhd len=%d ver=%d flags=0x%X id=%d dur=%d lay=%d vol=%d\n",
1496 trak->tkdata_len, trak->tkdata[0], trak->tkdata[1],
1497 char2int(trak->tkdata, 12), // id
1498 char2int(trak->tkdata, 20), // duration
1499 char2short(trak->tkdata, 32), // layer
1500 char2short(trak->tkdata, 36)); // volume
1501 break;
1503 case MOV_FOURCC('m','d','h','d'): {
1504 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: %*sMedia header!\n", level, "");
1505 stream_skip(demuxer->stream, 12);
1506 // read timescale
1507 trak->timescale = stream_read_dword(demuxer->stream);
1508 // read length
1509 trak->length = stream_read_dword(demuxer->stream);
1510 break;
1512 case MOV_FOURCC('h','d','l','r'): {
1513 unsigned int tmp = stream_read_dword(demuxer->stream);
1514 unsigned int type = stream_read_dword_le(demuxer->stream);
1515 unsigned int subtype = stream_read_dword_le(demuxer->stream);
1516 unsigned int manufact = stream_read_dword_le(demuxer->stream);
1517 unsigned int comp_flags = stream_read_dword(demuxer->stream);
1518 unsigned int comp_mask = stream_read_dword(demuxer->stream);
1519 int len = stream_read_char(demuxer->stream);
1520 char* str = malloc(len + 1);
1521 stream_read(demuxer->stream, str, len);
1522 str[len] = 0;
1523 mp_msg(MSGT_DEMUX, MSGL_V,
1524 "MOV: %*sHandler header: %.4s/%.4s (%.4s) %s\n", level, "",
1525 (char *)&type, (char *)&subtype, (char *)&manufact, str);
1526 free(str);
1527 switch(bswap_32(type)) {
1528 case MOV_FOURCC('m','h','l','r'):
1529 trak->media_handler = bswap_32(subtype);
1530 break;
1531 case MOV_FOURCC('d','h','l','r'):
1532 trak->data_handler = bswap_32(subtype);
1533 break;
1534 default:
1535 mp_msg(MSGT_DEMUX, MSGL_V,
1536 "MOV: unknown handler class: 0x%X (%.4s)\n",
1537 bswap_32(type), (char *)&type);
1539 break;
1541 case MOV_FOURCC('v','m','h','d'): {
1542 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: %*sVideo header!\n", level, "");
1543 trak->type = MOV_TRAK_VIDEO;
1544 // read video data
1545 break;
1547 case MOV_FOURCC('s','m','h','d'): {
1548 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: %*sSound header!\n", level, "");
1549 trak->type = MOV_TRAK_AUDIO;
1550 // read audio data
1551 break;
1553 case MOV_FOURCC('g','m','h','d'): {
1554 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: %*sGeneric header!\n", level, "");
1555 trak->type = MOV_TRAK_GENERIC;
1556 break;
1558 case MOV_FOURCC('n','m','h','d'): {
1559 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: %*sGeneric header!\n", level, "");
1560 trak->type = MOV_TRAK_GENERIC;
1561 break;
1563 case MOV_FOURCC('s','t','s','d'): {
1564 int i = stream_read_dword(demuxer->stream); // temp!
1565 int count = stream_read_dword(demuxer->stream);
1566 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: %*sDescription list! (cnt:%d)\n",
1567 level, "", count);
1568 for (i = 0; i < count; i++) {
1569 off_t pos = stream_tell(demuxer->stream);
1570 off_t len = stream_read_dword(demuxer->stream);
1571 unsigned int fourcc = stream_read_dword_le(demuxer->stream);
1572 /* some files created with Broadcast 2000 (e.g. ilacetest.mov)
1573 contain raw I420 video but have a yv12 fourcc */
1574 if (fourcc == mmioFOURCC('y','v','1','2'))
1575 fourcc = mmioFOURCC('I','4','2','0');
1576 if (len < 8)
1577 break; // error
1578 mp_msg(MSGT_DEMUX, MSGL_V,
1579 "MOV: %*s desc #%d: %.4s (%"PRId64" bytes)\n", level, "",
1580 i, (char *)&fourcc, (int64_t)len - 16);
1581 if (fourcc != trak->fourcc && i)
1582 mp_msg(MSGT_DEMUX, MSGL_WARN, MSGTR_MOVvariableFourCC);
1583 // if(!i)
1585 trak->fourcc = fourcc;
1586 // read type specific (audio/video/time/text etc) header
1587 // NOTE: trak type is not yet known at this point :(((
1588 trak->stdata_len = len - 8;
1589 trak->stdata = malloc(trak->stdata_len);
1590 stream_read(demuxer->stream, trak->stdata, trak->stdata_len);
1592 if (!stream_seek(demuxer->stream, pos + len))
1593 break;
1595 break;
1597 case MOV_FOURCC('s','t','t','s'): {
1598 int temp = stream_read_dword(demuxer->stream);
1599 int len = stream_read_dword(demuxer->stream);
1600 int i;
1601 unsigned int pts = 0;
1602 mp_msg(MSGT_DEMUX, MSGL_V,
1603 "MOV: %*sSample duration table! (%d blocks)\n", level, "",
1604 len);
1605 trak->durmap = calloc(len, sizeof(mov_durmap_t));
1606 trak->durmap_size = len;
1607 for (i = 0; i < len; i++) {
1608 trak->durmap[i].num = stream_read_dword(demuxer->stream);
1609 trak->durmap[i].dur = stream_read_dword(demuxer->stream);
1610 pts += trak->durmap[i].num * trak->durmap[i].dur;
1612 if (trak->length != pts)
1613 mp_msg(MSGT_DEMUX, MSGL_WARN, "Warning! pts=%d length=%d\n",
1614 pts, trak->length);
1615 break;
1617 case MOV_FOURCC('s','t','s','c'): {
1618 int temp = stream_read_dword(demuxer->stream);
1619 int len = stream_read_dword(demuxer->stream);
1620 int ver = (temp << 24);
1621 int flags = (temp << 16) | (temp << 8) | temp;
1622 int i;
1623 mp_msg(MSGT_DEMUX, MSGL_V,
1624 "MOV: %*sSample->Chunk mapping table! (%d blocks) (ver:%d,flags:%d)\n", level, "",
1625 len, ver, flags);
1626 // read data:
1627 trak->chunkmap_size = len;
1628 trak->chunkmap = calloc(len, sizeof(mov_chunkmap_t));
1629 for (i = 0; i < len; i++) {
1630 trak->chunkmap[i].first = stream_read_dword(demuxer->stream) - 1;
1631 trak->chunkmap[i].spc = stream_read_dword(demuxer->stream);
1632 trak->chunkmap[i].sdid = stream_read_dword(demuxer->stream);
1634 break;
1636 case MOV_FOURCC('s','t','s','z'): {
1637 int temp = stream_read_dword(demuxer->stream);
1638 int ss=stream_read_dword(demuxer->stream);
1639 int ver = (temp << 24);
1640 int flags = (temp << 16) | (temp << 8) | temp;
1641 int entries = stream_read_dword(demuxer->stream);
1642 int i;
1643 mp_msg(MSGT_DEMUX, MSGL_V,
1644 "MOV: %*sSample size table! (entries=%d ss=%d) (ver:%d,flags:%d)\n", level, "",
1645 entries, ss, ver, flags);
1646 trak->samplesize = ss;
1647 if (!ss) {
1648 // variable samplesize
1649 trak->samples = realloc_struct(trak->samples, entries, sizeof(mov_sample_t));
1650 trak->samples_size = entries;
1651 for (i = 0; i < entries; i++)
1652 trak->samples[i].size = stream_read_dword(demuxer->stream);
1654 break;
1656 case MOV_FOURCC('s','t','c','o'): {
1657 int temp = stream_read_dword(demuxer->stream);
1658 int len = stream_read_dword(demuxer->stream);
1659 int i;
1660 mp_msg(MSGT_DEMUX, MSGL_V,
1661 "MOV: %*sChunk offset table! (%d chunks)\n", level, "",
1662 len);
1663 // extend array if needed:
1664 if (len > trak->chunks_size) {
1665 trak->chunks = realloc_struct(trak->chunks, len, sizeof(mov_chunk_t));
1666 trak->chunks_size = len;
1668 // read elements:
1669 for(i = 0; i < len; i++)
1670 trak->chunks[i].pos = stream_read_dword(demuxer->stream);
1671 break;
1673 case MOV_FOURCC('c','o','6','4'): {
1674 int temp = stream_read_dword(demuxer->stream);
1675 int len = stream_read_dword(demuxer->stream);
1676 int i;
1677 mp_msg(MSGT_DEMUX, MSGL_V,
1678 "MOV: %*s64bit chunk offset table! (%d chunks)\n", level, "",
1679 len);
1680 // extend array if needed:
1681 if (len > trak->chunks_size) {
1682 trak->chunks = realloc_struct(trak->chunks, len, sizeof(mov_chunk_t));
1683 trak->chunks_size = len;
1685 // read elements:
1686 for (i = 0; i < len; i++) {
1687 #ifndef _LARGEFILE_SOURCE
1688 if (stream_read_dword(demuxer->stream) != 0)
1689 mp_msg(MSGT_DEMUX, MSGL_WARN, "Chunk %d has got 64bit address, but you've MPlayer compiled without LARGEFILE support!\n", i);
1690 trak->chunks[i].pos = stream_read_dword(demuxer->stream);
1691 #else
1692 trak->chunks[i].pos = stream_read_qword(demuxer->stream);
1693 #endif
1695 break;
1697 case MOV_FOURCC('s','t','s','s'): {
1698 int temp = stream_read_dword(demuxer->stream);
1699 int entries = stream_read_dword(demuxer->stream);
1700 int ver = (temp << 24);
1701 int flags = (temp << 16) | (temp<<8) | temp;
1702 int i;
1703 mp_msg(MSGT_DEMUX, MSGL_V,
1704 "MOV: %*sSyncing samples (keyframes) table! (%d entries) (ver:%d,flags:%d)\n", level, "",
1705 entries, ver, flags);
1706 trak->keyframes_size = entries;
1707 trak->keyframes = calloc(entries, sizeof(unsigned int));
1708 for (i = 0; i < entries; i++)
1709 trak->keyframes[i] = stream_read_dword(demuxer->stream) - 1;
1710 break;
1712 case MOV_FOURCC('m','d','i','a'): {
1713 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: %*sMedia stream!\n", level, "");
1714 lschunks(demuxer, level + 1, pos + len, trak);
1715 break;
1717 case MOV_FOURCC('m','i','n','f'): {
1718 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: %*sMedia info!\n", level, "");
1719 lschunks(demuxer, level + 1 ,pos + len, trak);
1720 break;
1722 case MOV_FOURCC('s','t','b','l'): {
1723 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: %*sSample info!\n", level, "");
1724 lschunks(demuxer, level + 1, pos + len, trak);
1725 break;
1727 case MOV_FOURCC('e','d','t','s'): {
1728 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: %*sEdit atom!\n", level, "");
1729 lschunks(demuxer, level + 1, pos + len, trak);
1730 break;
1732 case MOV_FOURCC('e','l','s','t'): {
1733 int temp = stream_read_dword(demuxer->stream);
1734 int entries = stream_read_dword(demuxer->stream);
1735 int ver = (temp << 24);
1736 int flags = (temp << 16) | (temp << 8) | temp;
1737 int i;
1738 mp_msg(MSGT_DEMUX, MSGL_V,
1739 "MOV: %*sEdit list table (%d entries) (ver:%d,flags:%d)\n", level, "",
1740 entries, ver, flags);
1741 #if 1
1742 trak->editlist_size = entries;
1743 trak->editlist = calloc(trak->editlist_size, sizeof(mov_editlist_t));
1744 for (i = 0; i < entries; i++) {
1745 int dur = stream_read_dword(demuxer->stream);
1746 int mt = stream_read_dword(demuxer->stream);
1747 int mr = stream_read_dword(demuxer->stream); // 16.16fp
1748 trak->editlist[i].dur = dur;
1749 trak->editlist[i].pos = mt;
1750 trak->editlist[i].speed = mr;
1751 mp_msg(MSGT_DEMUX, MSGL_V,
1752 "MOV: %*s entry#%d: duration: %d start time: %d speed: %3.1fx\n", level, "",
1753 i, dur, mt, (float)mr/65536.0f);
1755 #endif
1756 break;
1758 case MOV_FOURCC('c','o','d','e'): {
1759 /* XXX: Implement atom 'code' for FLASH support */
1760 break;
1762 default:
1763 id = be2me_32(id);
1764 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: unknown chunk: %.4s %d\n",(char *)&id,(int)len);
1765 break;
1766 }//switch(id)
1767 return 0;
1770 static demuxer_t* mov_read_header(demuxer_t* demuxer){
1771 mov_priv_t* priv=demuxer->priv;
1772 int t_no;
1773 int best_a_id=-1, best_a_len=0;
1774 int best_v_id=-1, best_v_len=0;
1776 mp_msg(MSGT_DEMUX, MSGL_DBG3, "mov_read_header!\n");
1778 // Parse header:
1779 stream_reset(demuxer->stream);
1780 if(!stream_seek(demuxer->stream,priv->moov_start))
1782 mp_msg(MSGT_DEMUX,MSGL_ERR,"MOV: Cannot seek to the beginning of the Movie header (0x%"PRIx64")\n",
1783 (int64_t)priv->moov_start);
1784 return 0;
1786 lschunks(demuxer, 0, priv->moov_end, NULL);
1787 // just in case we have hit eof while parsing...
1788 demuxer->stream->eof = 0;
1789 // mp_msg(MSGT_DEMUX, MSGL_INFO, "--------------\n");
1791 // find the best (longest) streams:
1792 for(t_no=0;t_no<priv->track_db;t_no++){
1793 mov_track_t* trak=priv->tracks[t_no];
1794 int len=(trak->samplesize) ? trak->chunks_size : trak->samples_size;
1795 if(demuxer->a_streams[t_no]){ // need audio
1796 if(len>best_a_len){ best_a_len=len; best_a_id=t_no; }
1798 if(demuxer->v_streams[t_no]){ // need video
1799 if(len>best_v_len){ best_v_len=len; best_v_id=t_no; }
1802 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: longest streams: A: #%d (%d samples) V: #%d (%d samples)\n",
1803 best_a_id,best_a_len,best_v_id,best_v_len);
1804 if(demuxer->audio->id==-1 && best_a_id>=0) demuxer->audio->id=best_a_id;
1805 if(demuxer->video->id==-1 && best_v_id>=0) demuxer->video->id=best_v_id;
1807 // setup sh pointers:
1808 if(demuxer->audio->id>=0){
1809 sh_audio_t* sh=demuxer->a_streams[demuxer->audio->id];
1810 if(sh){
1811 demuxer->audio->sh=sh; sh->ds=demuxer->audio;
1812 } else {
1813 mp_msg(MSGT_DEMUX, MSGL_ERR, "MOV: selected audio stream (%d) does not exists\n",demuxer->audio->id);
1814 demuxer->audio->id=-2;
1817 if(demuxer->video->id>=0){
1818 sh_video_t* sh=demuxer->v_streams[demuxer->video->id];
1819 if(sh){
1820 demuxer->video->sh=sh; sh->ds=demuxer->video;
1821 } else {
1822 mp_msg(MSGT_DEMUX, MSGL_ERR, "MOV: selected video stream (%d) does not exists\n",demuxer->video->id);
1823 demuxer->video->id=-2;
1827 #if 1
1828 if( mp_msg_test(MSGT_DEMUX,MSGL_DBG3) ){
1829 for(t_no=0;t_no<priv->track_db;t_no++){
1830 mov_track_t* trak=priv->tracks[t_no];
1831 if(trak->type==MOV_TRAK_GENERIC){
1832 int i;
1833 int fd;
1834 char name[20];
1835 mp_msg(MSGT_DEMUX, MSGL_INFO, "MOV: Track #%d: Extracting %d data chunks to files\n",t_no,trak->samples_size);
1836 for (i=0; i<trak->samples_size; i++)
1838 int len=trak->samples[i].size;
1839 char buf[len];
1840 stream_seek(demuxer->stream, trak->samples[i].pos);
1841 snprintf(name, 20, "t%02d-s%03d.%s", t_no,i,
1842 (trak->media_handler==MOV_FOURCC('f','l','s','h')) ?
1843 "swf":"dump");
1844 fd = open(name, O_CREAT|O_WRONLY);
1845 // { int j;
1846 // for(j=0;j<trak->stdata_len-3; j++)
1847 // printf("stdata[%d]=0x%X ize=0x%X\n",j,char2int(trak->stdata,j),MOV_FOURCC('z','l','i','b'));
1848 // }
1849 if( //trak->media_handler==MOV_FOURCC('s','p','r','t') &&
1850 trak->stdata_len>=16 &&
1851 char2int(trak->stdata,12)==MOV_FOURCC('z','l','i','b')
1853 int newlen=stream_read_dword(demuxer->stream);
1854 #ifdef HAVE_ZLIB
1855 // unzip:
1856 z_stream zstrm;
1857 int zret;
1858 char buf2[newlen];
1860 len-=4;
1861 stream_read(demuxer->stream, buf, len);
1863 zstrm.zalloc = (alloc_func)0;
1864 zstrm.zfree = (free_func)0;
1865 zstrm.opaque = (voidpf)0;
1866 zstrm.next_in = buf;
1867 zstrm.avail_in = len;
1868 zstrm.next_out = buf2;
1869 zstrm.avail_out = newlen;
1871 zret = inflateInit(&zstrm);
1872 zret = inflate(&zstrm, Z_NO_FLUSH);
1873 if(newlen != zstrm.total_out)
1874 mp_msg(MSGT_DEMUX, MSGL_WARN, "Warning! unzipped frame size differs hdr: %d zlib: %ld\n",newlen,zstrm.total_out);
1876 write(fd, buf2, newlen);
1877 } else {
1878 #else
1879 len-=4;
1880 mp_msg(MSGT_DEMUX, MSGL_INFO, "******* ZLIB COMPRESSED SAMPLE!!!!! (%d->%d bytes) *******\n",len,newlen);
1883 #endif
1884 stream_read(demuxer->stream, buf, len);
1885 write(fd, buf, len);
1887 close(fd);
1892 demuxer->stream->eof = 0;
1893 #endif
1895 return demuxer;
1899 * \brief return the mov track that belongs to a demuxer stream
1900 * \param ds the demuxer stream, may be NULL
1901 * \return the mov track info structure belonging to the stream,
1902 * NULL if not found
1904 static mov_track_t *stream_track(mov_priv_t *priv, demux_stream_t *ds) {
1905 if (ds && (ds->id >= 0) && (ds->id < priv->track_db))
1906 return priv->tracks[ds->id];
1907 return NULL;
1910 // return value:
1911 // 0 = EOF or no stream found
1912 // 1 = successfully read a packet
1913 static int demux_mov_fill_buffer(demuxer_t *demuxer,demux_stream_t* ds){
1914 mov_priv_t* priv=demuxer->priv;
1915 mov_track_t* trak=NULL;
1916 float pts;
1917 int x;
1918 off_t pos;
1920 if (ds->eof) return 0;
1921 trak = stream_track(priv, ds);
1922 if (!trak) return 0;
1924 if(trak->samplesize){
1925 // read chunk:
1926 if(trak->pos>=trak->chunks_size) return 0; // EOF
1927 stream_seek(demuxer->stream,trak->chunks[trak->pos].pos);
1928 pts=(float)(trak->chunks[trak->pos].sample*trak->duration)/(float)trak->timescale;
1929 if(trak->samplesize!=1)
1931 mp_msg(MSGT_DEMUX, MSGL_DBG2, "WARNING! Samplesize(%d) != 1\n",
1932 trak->samplesize);
1933 if((trak->fourcc != MOV_FOURCC('t','w','o','s')) && (trak->fourcc != MOV_FOURCC('s','o','w','t')))
1934 x=trak->chunks[trak->pos].size*trak->samplesize;
1935 else
1936 x=trak->chunks[trak->pos].size;
1938 else
1939 x=trak->chunks[trak->pos].size;
1940 // printf("X = %d\n", x);
1941 /* the following stuff is audio related */
1942 if (trak->type == MOV_TRAK_AUDIO){
1943 if(trak->stdata_len>=44 && trak->stdata[9]>=1 && char2int(trak->stdata,28)>0){
1944 // stsd version 1 - we have audio compression ratio info:
1945 x/=char2int(trak->stdata,28); // samples/packet
1946 // x*=char2int(trak->stdata,32); // bytes/packet
1947 x*=char2int(trak->stdata,36); // bytes/frame
1948 } else {
1949 if(ds->ss_div && ds->ss_mul){
1950 // workaround for buggy files like 7up-high-traffic-areas.mov,
1951 // with missing stsd v1 header containing compression rate
1952 x/=ds->ss_div; x*=ds->ss_mul; // compression ratio fix ! HACK !
1953 } else {
1954 x*=trak->nchannels;
1955 x*=trak->samplebytes;
1958 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Audio sample %d bytes pts %5.3f\n",trak->chunks[trak->pos].size*trak->samplesize,pts);
1959 } /* MOV_TRAK_AUDIO */
1960 pos=trak->chunks[trak->pos].pos;
1961 } else {
1962 int frame=trak->pos;
1963 // editlist support:
1964 if(trak->type == MOV_TRAK_VIDEO && trak->editlist_size>=1){
1965 // find the right editlist entry:
1966 if(frame<trak->editlist[trak->editlist_pos].start_frame)
1967 trak->editlist_pos=0;
1968 while(trak->editlist_pos<trak->editlist_size-1 &&
1969 frame>=trak->editlist[trak->editlist_pos+1].start_frame)
1970 ++trak->editlist_pos;
1971 if(frame>=trak->editlist[trak->editlist_pos].start_frame+
1972 trak->editlist[trak->editlist_pos].frames) return 0; // EOF
1973 // calc real frame index:
1974 frame-=trak->editlist[trak->editlist_pos].start_frame;
1975 frame+=trak->editlist[trak->editlist_pos].start_sample;
1976 // calc pts:
1977 pts=(float)(trak->samples[frame].pts+
1978 trak->editlist[trak->editlist_pos].pts_offset)/(float)trak->timescale;
1979 } else {
1980 if(frame>=trak->samples_size) return 0; // EOF
1981 pts=(float)trak->samples[frame].pts/(float)trak->timescale;
1983 // read sample:
1984 stream_seek(demuxer->stream,trak->samples[frame].pos);
1985 x=trak->samples[frame].size;
1986 pos=trak->samples[frame].pos;
1988 if(trak->pos==0 && trak->stream_header_len>0){
1989 // we have to append the stream header...
1990 demux_packet_t* dp=new_demux_packet(x+trak->stream_header_len);
1991 memcpy(dp->buffer,trak->stream_header,trak->stream_header_len);
1992 stream_read(demuxer->stream,dp->buffer+trak->stream_header_len,x);
1993 free(trak->stream_header);
1994 trak->stream_header = NULL;
1995 trak->stream_header_len = 0;
1996 dp->pts=pts;
1997 dp->flags=0;
1998 dp->pos=pos; // FIXME?
1999 ds_add_packet(ds,dp);
2000 } else
2001 ds_read_packet(ds,demuxer->stream,x,pts,pos,0);
2003 ++trak->pos;
2005 return 1;
2009 static float mov_seek_track(mov_track_t* trak,float pts,int flags){
2011 // printf("MOV track seek called %5.3f \n",pts);
2012 if(flags&2) pts*=trak->length; else pts*=(float)trak->timescale;
2014 if(trak->samplesize){
2015 int sample=pts/trak->duration;
2016 // printf("MOV track seek - chunk: %d (pts: %5.3f dur=%d) \n",sample,pts,trak->duration);
2017 if(!(flags&1)) sample+=trak->chunks[trak->pos].sample; // relative
2018 trak->pos=0;
2019 while(trak->pos<trak->chunks_size && trak->chunks[trak->pos].sample<sample) ++trak->pos;
2020 if (trak->pos == trak->chunks_size) return -1;
2021 pts=(float)(trak->chunks[trak->pos].sample*trak->duration)/(float)trak->timescale;
2022 } else {
2023 unsigned int ipts;
2024 if(!(flags&1)) pts+=trak->samples[trak->pos].pts;
2025 if(pts<0) pts=0;
2026 ipts=pts;
2027 //printf("MOV track seek - sample: %d \n",ipts);
2028 for(trak->pos=0;trak->pos<trak->samples_size;++trak->pos){
2029 if(trak->samples[trak->pos].pts>=ipts) break; // found it!
2031 if (trak->pos == trak->samples_size) return -1;
2032 if(trak->keyframes_size){
2033 // find nearest keyframe
2034 int i;
2035 for(i=0;i<trak->keyframes_size;i++){
2036 if(trak->keyframes[i]>=trak->pos) break;
2038 if (i == trak->keyframes_size) return -1;
2039 if(i>0 && (trak->keyframes[i]-trak->pos) > (trak->pos-trak->keyframes[i-1]))
2040 --i;
2041 trak->pos=trak->keyframes[i];
2042 // printf("nearest keyframe: %d \n",trak->pos);
2044 pts=(float)trak->samples[trak->pos].pts/(float)trak->timescale;
2047 // printf("MOV track seek done: %5.3f \n",pts);
2049 return pts;
2052 static void demux_seek_mov(demuxer_t *demuxer,float pts,float audio_delay,int flags){
2053 mov_priv_t* priv=demuxer->priv;
2054 demux_stream_t* ds;
2055 mov_track_t* trak;
2057 // printf("MOV seek called %5.3f flag=%d \n",pts,flags);
2059 ds=demuxer->video;
2060 trak = stream_track(priv, ds);
2061 if (trak) {
2062 //if(flags&2) pts*=(float)trak->length/(float)trak->timescale;
2063 //if(!(flags&1)) pts+=ds->pts;
2064 ds->pts=mov_seek_track(trak,pts,flags);
2065 if (ds->pts < 0) ds->eof = 1;
2066 else pts = ds->pts;
2067 flags=1; // absolute seconds
2070 ds=demuxer->audio;
2071 trak = stream_track(priv, ds);
2072 if (trak) {
2073 //if(flags&2) pts*=(float)trak->length/(float)trak->timescale;
2074 //if(!(flags&1)) pts+=ds->pts;
2075 ds->pts=mov_seek_track(trak,pts,flags);
2076 if (ds->pts < 0) ds->eof = 1;
2077 if (demuxer->video->id < 0)
2078 ((sh_audio_t*)ds->sh)->delay = ds->pts;
2083 static int demux_mov_control(demuxer_t *demuxer, int cmd, void *arg){
2084 mov_track_t* track;
2086 // try the video track
2087 track = stream_track(demuxer->priv, demuxer->video);
2088 if (!track || !track->length)
2089 // otherwise try to get the info from the audio track
2090 track = stream_track(demuxer->priv, demuxer->audio);
2092 if (!track || !track->length)
2093 return DEMUXER_CTRL_DONTKNOW;
2095 switch(cmd) {
2096 case DEMUXER_CTRL_GET_TIME_LENGTH:
2097 if (!track->timescale)
2098 return DEMUXER_CTRL_DONTKNOW;
2099 *((double *)arg) = (double)track->length / track->timescale;
2100 return DEMUXER_CTRL_OK;
2102 case DEMUXER_CTRL_GET_PERCENT_POS:
2104 off_t pos = track->pos;
2105 if (track->durmap_size >= 1)
2106 pos *= track->durmap[0].dur;
2107 *((int *)arg) = (int)(100 * pos / track->length);
2108 return DEMUXER_CTRL_OK;
2111 return DEMUXER_CTRL_NOTIMPL;
2115 demuxer_desc_t demuxer_desc_mov = {
2116 "Quicktime/MP4 demuxer",
2117 "mov",
2118 "Quicktime/MOV",
2119 "Arpi, Al3x, Atmos, others",
2120 "Handles Quicktime, MP4, 3GP",
2121 DEMUXER_TYPE_MOV,
2122 0, // slow autodetect
2123 mov_check_file,
2124 demux_mov_fill_buffer,
2125 mov_read_header,
2126 demux_close_mov,
2127 demux_seek_mov,
2128 demux_mov_control