Fix segfault if an 'strf' chunk couldn't be found in avi
[mplayer/glamo.git] / libmpdemux / demux_mov.c
blobf34275259744c1838a70898870c6cb03d2d5a0fb
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 useful for .mp4
12 // as well 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>
24 #include <inttypes.h>
26 #include "config.h"
28 #ifdef MACOSX
29 #include <QuickTime/QuickTime.h>
30 #include <QuickTime/ImageCompression.h>
31 #include <QuickTime/ImageCodec.h>
32 #else
33 #include "loader/qtx/qtxsdk/components.h"
34 #endif
36 #include "mp_msg.h"
37 #include "help_mp.h"
39 #include "stream/stream.h"
40 #include "demuxer.h"
41 #include "stheader.h"
43 #include "libvo/sub.h"
44 extern subtitle *vo_sub;
46 #include "qtpalette.h"
47 #include "parse_mp4.h" // .MP4 specific stuff
49 #ifdef HAVE_ZLIB
50 #include <zlib.h>
51 #endif
53 #ifndef _FCNTL_H
54 #include <fcntl.h>
55 #endif
57 #define BE_16(x) (((unsigned char *)(x))[0] << 8 | \
58 ((unsigned char *)(x))[1])
59 #define BE_32(x) (((unsigned char *)(x))[0] << 24 | \
60 ((unsigned char *)(x))[1] << 16 | \
61 ((unsigned char *)(x))[2] << 8 | \
62 ((unsigned char *)(x))[3])
64 #define char2short(x,y) BE_16(&(x)[(y)])
65 #define char2int(x,y) BE_32(&(x)[(y)])
67 typedef struct {
68 unsigned int pts; // duration
69 unsigned int size;
70 off_t pos;
71 } mov_sample_t;
73 typedef struct {
74 unsigned int sample; // number of the first sample in the chunk
75 unsigned int size; // number of samples in the chunk
76 int desc; // for multiple codecs mode - not used
77 off_t pos;
78 } mov_chunk_t;
80 typedef struct {
81 unsigned int first;
82 unsigned int spc;
83 unsigned int sdid;
84 } mov_chunkmap_t;
86 typedef struct {
87 unsigned int num;
88 unsigned int dur;
89 } mov_durmap_t;
91 typedef struct {
92 unsigned int dur;
93 unsigned int pos;
94 int speed;
96 int frames;
97 int start_sample;
98 int start_frame;
99 int pts_offset;
100 } mov_editlist_t;
102 #define MOV_TRAK_UNKNOWN 0
103 #define MOV_TRAK_VIDEO 1
104 #define MOV_TRAK_AUDIO 2
105 #define MOV_TRAK_FLASH 3
106 #define MOV_TRAK_GENERIC 4
107 #define MOV_TRAK_CODE 5
109 typedef struct {
110 int id;
111 int type;
112 off_t pos;
114 unsigned int media_handler;
115 unsigned int data_handler;
117 int timescale;
118 unsigned int length;
119 int samplesize; // 0 = variable
120 int duration; // 0 = variable
121 int width,height; // for video
122 unsigned int fourcc;
123 unsigned int nchannels;
124 unsigned int samplebytes;
126 int tkdata_len; // track data
127 unsigned char* tkdata;
128 int stdata_len; // stream data
129 unsigned char* stdata;
131 unsigned char* stream_header;
132 int stream_header_len; // if >0, this header should be sent before the 1st frame
134 int samples_size;
135 mov_sample_t* samples;
136 int chunks_size;
137 mov_chunk_t* chunks;
138 int chunkmap_size;
139 mov_chunkmap_t* chunkmap;
140 int durmap_size;
141 mov_durmap_t* durmap;
142 int keyframes_size;
143 unsigned int* keyframes;
144 int editlist_size;
145 mov_editlist_t* editlist;
146 int editlist_pos;
148 void* desc; // image/sound/etc description (pointer to ImageDescription etc)
149 } mov_track_t;
151 void mov_build_index(mov_track_t* trak,int timescale){
152 int i,j,s;
153 int last=trak->chunks_size;
154 unsigned int pts=0;
156 #if 0
157 if (trak->chunks_size <= 0)
159 mp_msg(MSGT_DEMUX, MSGL_WARN, "No chunk offset table, trying to build one!\n");
161 trak->chunks_size = trak->samples_size; /* XXX: FIXME ! */
162 // audit: this code will be vulnerable if it is reenabled (currently #if 0)
163 trak->chunks = realloc(trak->chunks, sizeof(mov_chunk_t)*trak->chunks_size);
165 for (i=0; i < trak->chunks_size; i++)
166 trak->chunks[i].pos = -1;
168 #endif
170 mp_msg(MSGT_DEMUX, MSGL_V, "MOV track #%d: %d chunks, %d samples\n",trak->id,trak->chunks_size,trak->samples_size);
171 mp_msg(MSGT_DEMUX, MSGL_V, "pts=%d scale=%d time=%5.3f\n",trak->length,trak->timescale,(float)trak->length/(float)trak->timescale);
173 // process chunkmap:
174 i=trak->chunkmap_size;
175 while(i>0){
176 --i;
177 for(j=trak->chunkmap[i].first;j<last;j++){
178 trak->chunks[j].desc=trak->chunkmap[i].sdid;
179 trak->chunks[j].size=trak->chunkmap[i].spc;
181 last=trak->chunkmap[i].first;
184 #if 0
185 for (i=0; i < trak->chunks_size; i++)
187 /* fixup position */
188 if (trak->chunks[i].pos == -1)
189 if (i > 0)
190 trak->chunks[i].pos = trak->chunks[i-1].pos + trak->chunks[i-1].size;
191 else
192 trak->chunks[i].pos = 0; /* FIXME: set initial pos */
193 #endif
195 // calc pts of chunks:
196 s=0;
197 for(j=0;j<trak->chunks_size;j++){
198 trak->chunks[j].sample=s;
199 s+=trak->chunks[j].size;
201 i = 0;
202 for (j = 0; j < trak->durmap_size; j++)
203 i += trak->durmap[j].num;
204 if (i != s) {
205 mp_msg(MSGT_DEMUX, MSGL_WARN,
206 "MOV: durmap and chunkmap sample count differ (%i vs %i)\n", i, s);
207 if (i > s) s = i;
210 // workaround for fixed-size video frames (dv and uncompressed)
211 if(!trak->samples_size && trak->type!=MOV_TRAK_AUDIO){
212 trak->samples_size=s;
213 trak->samples=calloc(s, sizeof(mov_sample_t));
214 for(i=0;i<s;i++)
215 trak->samples[i].size=trak->samplesize;
216 trak->samplesize=0;
219 if(!trak->samples_size){
220 // constant sampesize
221 if(trak->durmap_size==1 || (trak->durmap_size==2 && trak->durmap[1].num==1)){
222 trak->duration=trak->durmap[0].dur;
223 } else mp_msg(MSGT_DEMUX, MSGL_ERR, "*** constant samplesize & variable duration not yet supported! ***\nContact the author if you have such sample file!\n");
224 return;
227 if (trak->samples_size < s) {
228 mp_msg(MSGT_DEMUX, MSGL_WARN,
229 "MOV: durmap or chunkmap bigger than sample count (%i vs %i)\n",
230 s, trak->samples_size);
231 trak->samples_size = s;
232 trak->samples = realloc_struct(trak->samples, s, sizeof(mov_sample_t));
235 // calc pts:
236 s=0;
237 for(j=0;j<trak->durmap_size;j++){
238 for(i=0;i<trak->durmap[j].num;i++){
239 trak->samples[s].pts=pts;
240 ++s;
241 pts+=trak->durmap[j].dur;
245 // calc sample offsets
246 s=0;
247 for(j=0;j<trak->chunks_size;j++){
248 off_t pos=trak->chunks[j].pos;
249 for(i=0;i<trak->chunks[j].size;i++){
250 trak->samples[s].pos=pos;
251 mp_msg(MSGT_DEMUX, MSGL_DBG3, "Sample %5d: pts=%8d off=0x%08X size=%d\n",s,
252 trak->samples[s].pts,
253 (int)trak->samples[s].pos,
254 trak->samples[s].size);
255 pos+=trak->samples[s].size;
256 ++s;
260 // precalc editlist entries
261 if(trak->editlist_size>0){
262 int frame=0;
263 int e_pts=0;
264 for(i=0;i<trak->editlist_size;i++){
265 mov_editlist_t* el=&trak->editlist[i];
266 int sample=0;
267 int pts=el->pos;
268 el->start_frame=frame;
269 if(pts<0){
270 // skip!
271 el->frames=0; continue;
273 // find start sample
274 for(;sample<trak->samples_size;sample++){
275 if(pts<=trak->samples[sample].pts) break;
277 el->start_sample=sample;
278 el->pts_offset=((long long)e_pts*(long long)trak->timescale)/(long long)timescale-trak->samples[sample].pts;
279 pts+=((long long)el->dur*(long long)trak->timescale)/(long long)timescale;
280 e_pts+=el->dur;
281 // find end sample
282 for(;sample<trak->samples_size;sample++){
283 if(pts<trak->samples[sample].pts) break;
285 el->frames=sample-el->start_sample;
286 frame+=el->frames;
287 mp_msg(MSGT_DEMUX,MSGL_V,"EL#%d: pts=%d 1st_sample=%d frames=%d (%5.3fs) pts_offs=%d\n",i,
288 el->pos,el->start_sample, el->frames,
289 (float)(el->dur)/(float)timescale, el->pts_offset);
295 #define MOV_MAX_TRACKS 256
296 #define MOV_MAX_SUBLEN 1024
298 typedef struct {
299 off_t moov_start;
300 off_t moov_end;
301 off_t mdat_start;
302 off_t mdat_end;
303 int track_db;
304 mov_track_t* tracks[MOV_MAX_TRACKS];
305 int timescale; // movie timescale
306 int duration; // movie duration (in movie timescale units)
307 subtitle subs;
308 char subtext[MOV_MAX_SUBLEN + 1];
309 int current_sub;
310 } mov_priv_t;
312 #define MOV_FOURCC(a,b,c,d) ((a<<24)|(b<<16)|(c<<8)|(d))
314 static int mov_check_file(demuxer_t* demuxer){
315 int flags=0;
316 int no=0;
317 mov_priv_t* priv=malloc(sizeof(mov_priv_t));
319 mp_msg(MSGT_DEMUX,MSGL_V,"Checking for MOV\n");
321 memset(priv,0,sizeof(mov_priv_t));
322 priv->current_sub = -1;
324 while(1){
325 int i;
326 int skipped=8;
327 off_t len=stream_read_dword(demuxer->stream);
328 unsigned int id=stream_read_dword(demuxer->stream);
329 if(stream_eof(demuxer->stream)) break; // EOF
330 if (len == 1) /* real size is 64bits - cjb */
332 #ifndef _LARGEFILE_SOURCE
333 if (stream_read_dword(demuxer->stream) != 0)
334 mp_msg(MSGT_DEMUX, MSGL_WARN, "64bit file, but you've compiled MPlayer without LARGEFILE support!\n");
335 len = stream_read_dword(demuxer->stream);
336 #else
337 len = stream_read_qword(demuxer->stream);
338 #endif
339 skipped += 8;
341 #if 0
342 else if (len == 0) /* deleted chunk */
344 /* XXX: CJB! is this right? - alex */
345 goto skip_chunk;
347 #endif
348 else if(len<8) break; // invalid chunk
350 switch(id){
351 case MOV_FOURCC('f','t','y','p'): {
352 unsigned int tmp;
353 // File Type Box (ftyp):
354 // char[4] major_brand (eg. 'isom')
355 // int minor_version (eg. 0x00000000)
356 // char[4] compatible_brands[] (eg. 'mp41')
357 // compatible_brands list spans to the end of box
358 #if 1
359 tmp = stream_read_dword(demuxer->stream);
360 switch(tmp) {
361 case MOV_FOURCC('i','s','o','m'):
362 mp_msg(MSGT_DEMUX,MSGL_V,"ISO: File Type Major Brand: ISO Base Media\n");
363 break;
364 case MOV_FOURCC('m','p','4','1'):
365 mp_msg(MSGT_DEMUX,MSGL_INFO,"ISO: File Type Major Brand: ISO/IEC 14496-1 (MPEG-4 system) v1\n");
366 break;
367 case MOV_FOURCC('m','p','4','2'):
368 mp_msg(MSGT_DEMUX,MSGL_INFO,"ISO: File Type Major Brand: ISO/IEC 14496-1 (MPEG-4 system) v2\n");
369 break;
370 case MOV_FOURCC('M','4','A',' '):
371 mp_msg(MSGT_DEMUX,MSGL_INFO,"ISO: File Type Major Brand: Apple iTunes AAC-LC Audio\n");
372 break;
373 case MOV_FOURCC('M','4','P',' '):
374 mp_msg(MSGT_DEMUX,MSGL_INFO,"ISO: File Type Major Brand: Apple iTunes AAC-LC Protected Audio\n");
375 break;
376 case MOV_FOURCC('q','t',' ',' '):
377 mp_msg(MSGT_DEMUX,MSGL_INFO,"ISO: File Type Major Brand: Original QuickTime\n");
378 break;
379 case MOV_FOURCC('3','g','p','1'):
380 mp_msg(MSGT_DEMUX,MSGL_INFO,"ISO: File Type Major Brand: 3GPP Profile 1\n");
381 break;
382 case MOV_FOURCC('3','g','p','2'):
383 case MOV_FOURCC('3','g','2','a'):
384 mp_msg(MSGT_DEMUX,MSGL_INFO,"ISO: File Type Major Brand: 3GPP Profile 2\n");
385 break;
386 case MOV_FOURCC('3','g','p','3'):
387 mp_msg(MSGT_DEMUX,MSGL_INFO,"ISO: File Type Major Brand: 3GPP Profile 3\n");
388 break;
389 case MOV_FOURCC('3','g','p','4'):
390 mp_msg(MSGT_DEMUX,MSGL_INFO,"ISO: File Type Major Brand: 3GPP Profile 4\n");
391 break;
392 case MOV_FOURCC('3','g','p','5'):
393 mp_msg(MSGT_DEMUX,MSGL_INFO,"ISO: File Type Major Brand: 3GPP Profile 5\n");
394 break;
395 case MOV_FOURCC('m','m','p','4'):
396 mp_msg(MSGT_DEMUX,MSGL_INFO,"ISO: File Type Major Brand: Mobile ISO/IEC 14496-1 (MPEG-4 system)\n");
397 break;
398 default:
399 tmp = be2me_32(tmp);
400 mp_msg(MSGT_DEMUX,MSGL_WARN,"ISO: Unknown File Type Major Brand: %.4s\n",(char *)&tmp);
402 mp_msg(MSGT_DEMUX,MSGL_V,"ISO: File Type Minor Version: %d\n",
403 stream_read_dword(demuxer->stream));
404 skipped += 8;
405 // List all compatible brands
406 for(i = 0; i < ((len-16)/4); i++) {
407 tmp = be2me_32(stream_read_dword(demuxer->stream));
408 mp_msg(MSGT_DEMUX,MSGL_V,"ISO: File Type Compatible Brand #%d: %.4s\n",i,(char *)&tmp);
409 skipped += 4;
411 #endif
412 } break;
413 case MOV_FOURCC('m','o','o','v'):
414 // case MOV_FOURCC('c','m','o','v'):
415 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: Movie header found!\n");
416 priv->moov_start=(off_t)stream_tell(demuxer->stream);
417 priv->moov_end=(off_t)priv->moov_start+len-skipped;
418 mp_msg(MSGT_DEMUX,MSGL_DBG2,"MOV: Movie header: start: %"PRIx64" end: %"PRIx64"\n",
419 (int64_t)priv->moov_start, (int64_t)priv->moov_end);
420 skipped+=8;
421 i = stream_read_dword(demuxer->stream)-8;
422 if(stream_read_dword(demuxer->stream)==MOV_FOURCC('r','m','r','a')){
423 skipped+=i;
424 mp_msg(MSGT_DEMUX,MSGL_INFO,"MOV: Reference Media file!!!\n");
425 //set demuxer type to playlist ...
426 demuxer->type=DEMUXER_TYPE_PLAYLIST;
427 while(i>0){
428 int len=stream_read_dword(demuxer->stream)-8;
429 int fcc=stream_read_dword(demuxer->stream);
430 if(len<0) break; // EOF!?
431 i-=8;
432 // printf("i=%d len=%d\n",i,len);
433 switch(fcc){
434 case MOV_FOURCC('r','m','d','a'):
435 continue;
436 case MOV_FOURCC('r','d','r','f'): {
437 int tmp=stream_read_dword(demuxer->stream);
438 int type=stream_read_dword_le(demuxer->stream);
439 int slen=stream_read_dword(demuxer->stream);
440 //char* s=malloc(slen+1);
441 //stream_read(demuxer->stream,s,slen);
443 //FIXME: also store type & data_rate ?
444 ds_read_packet(demuxer->video,
445 demuxer->stream,
446 slen,
448 stream_tell(demuxer->stream),
449 0 // no flags
451 flags|=4;
452 mp_msg(MSGT_DEMUX,MSGL_V,"Added reference to playlist\n");
453 //s[slen]=0;
454 //mp_msg(MSGT_DEMUX,MSGL_INFO,"REF: [%.4s] %s\n",&type,s);
455 len-=12+slen;i-=12+slen; break;
457 case MOV_FOURCC('r','m','d','r'): {
458 int flags=stream_read_dword(demuxer->stream);
459 int rate=stream_read_dword(demuxer->stream);
460 mp_msg(MSGT_DEMUX,MSGL_V," min. data rate: %d bits/sec\n",rate);
461 len-=8; i-=8; break;
463 case MOV_FOURCC('r','m','q','u'): {
464 int q=stream_read_dword(demuxer->stream);
465 mp_msg(MSGT_DEMUX,MSGL_V," quality index: %d\n",q);
466 len-=4; i-=4; break;
469 i-=len;stream_skip(demuxer->stream,len);
472 flags|=1;
473 break;
474 case MOV_FOURCC('w','i','d','e'):
475 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: 'WIDE' chunk found!\n");
476 if(flags&2) break;
477 case MOV_FOURCC('m','d','a','t'):
478 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: Movie DATA found!\n");
479 priv->mdat_start=stream_tell(demuxer->stream);
480 priv->mdat_end=priv->mdat_start+len-skipped;
481 mp_msg(MSGT_DEMUX,MSGL_DBG2,"MOV: Movie data: start: %"PRIx64" end: %"PRIx64"\n",
482 (int64_t)priv->mdat_start, (int64_t)priv->mdat_end);
483 flags|=2;
484 if(flags==3){
485 // if we're over the headers, then we can stop parsing here!
486 demuxer->priv=priv;
487 return DEMUXER_TYPE_MOV;
489 break;
490 case MOV_FOURCC('f','r','e','e'):
491 case MOV_FOURCC('s','k','i','p'):
492 case MOV_FOURCC('j','u','n','k'):
493 mp_msg(MSGT_DEMUX,MSGL_DBG2,"MOV: free space (len: %"PRId64")\n", (int64_t)len);
494 /* unused, if you edit a mov, you can use space provided by free atoms (redefining it) */
495 break;
496 case MOV_FOURCC('p','n','o','t'):
497 case MOV_FOURCC('P','I','C','T'):
498 /* dunno what, but we shoudl ignore it */
499 break;
500 default:
501 if(no==0){ free(priv); return 0;} // first chunk is bad!
502 id = be2me_32(id);
503 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: unknown chunk: %.4s %d\n",(char *)&id,(int)len);
505 skip_chunk:
506 if(!stream_skip(demuxer->stream,len-skipped)) break;
507 ++no;
510 if(flags==3){
511 demuxer->priv=priv;
512 return DEMUXER_TYPE_MOV;
514 free(priv);
516 if ((flags==5) || (flags==7)) // reference & header sent
517 return DEMUXER_TYPE_PLAYLIST;
519 if(flags==1)
520 mp_msg(MSGT_DEMUX,MSGL_WARN,"MOV: missing data (mdat) chunk! Maybe broken file...\n");
521 else if(flags==2)
522 mp_msg(MSGT_DEMUX,MSGL_WARN,"MOV: missing header (moov/cmov) chunk! Maybe broken file...\n");
524 return 0;
527 static void demux_close_mov(demuxer_t *demuxer) {
528 mov_priv_t* priv = demuxer->priv;
529 int i;
530 if (!priv)
531 return;
532 for (i = 0; i < MOV_MAX_TRACKS; i++) {
533 mov_track_t *track = priv->tracks[i];
534 if (track) {
535 free(track->tkdata);
536 free(track->stdata);
537 free(track->stream_header);
538 free(track->samples);
539 free(track->chunks);
540 free(track->chunkmap);
541 free(track->durmap);
542 free(track->keyframes);
543 free(track->editlist);
544 free(track->desc);
545 free(track);
548 free(priv);
551 unsigned int store_ughvlc(unsigned char *s, unsigned int v){
552 unsigned int n = 0;
554 while(v >= 0xff) {
555 *s++ = 0xff;
556 v -= 0xff;
557 n++;
559 *s = v;
560 n++;
562 return n;
565 static void init_vobsub(sh_sub_t *sh, mov_track_t *trak) {
566 int i;
567 uint8_t *pal = trak->stdata;
568 sh->type = 'v';
569 if (trak->stdata_len < 106)
570 return;
571 sh->has_palette = 1;
572 pal += 42;
573 for (i = 0; i < 16; i++) {
574 sh->palette[i] = BE_32(pal);
575 pal += 4;
579 static int lschunks_intrak(demuxer_t* demuxer, int level, unsigned int id,
580 off_t pos, off_t len, mov_track_t* trak);
582 static void lschunks(demuxer_t* demuxer,int level,off_t endpos,mov_track_t* trak){
583 mov_priv_t* priv=demuxer->priv;
584 // printf("lschunks (level=%d,endpos=%x)\n", level, endpos);
585 while(1){
586 off_t pos;
587 off_t len;
588 unsigned int id;
590 pos=stream_tell(demuxer->stream);
591 // printf("stream_tell==%d\n",pos);
592 if(pos>=endpos) return; // END
593 len=stream_read_dword(demuxer->stream);
594 // printf("len==%d\n",len);
595 if(len<8) return; // error
596 len-=8;
597 id=stream_read_dword(demuxer->stream);
599 mp_msg(MSGT_DEMUX,MSGL_DBG2,"lschunks %.4s %d\n",(char *)&id,(int)len);
601 if(trak){
602 if (lschunks_intrak(demuxer, level, id, pos, len, trak) < 0)
603 return;
604 } else { /* not in track */
605 switch(id) {
606 case MOV_FOURCC('m','v','h','d'): {
607 int version = stream_read_char(demuxer->stream);
608 stream_skip(demuxer->stream, (version == 1) ? 19 : 11);
609 priv->timescale=stream_read_dword(demuxer->stream);
610 if (version == 1)
611 priv->duration=stream_read_qword(demuxer->stream);
612 else
613 priv->duration=stream_read_dword(demuxer->stream);
614 mp_msg(MSGT_DEMUX, MSGL_V,"MOV: %*sMovie header (%d bytes): tscale=%d dur=%d\n",level,"",(int)len,
615 (int)priv->timescale,(int)priv->duration);
616 break;
618 case MOV_FOURCC('t','r','a','k'): {
619 // if(trak) printf("MOV: Warning! trak in trak?\n");
620 if(priv->track_db>=MOV_MAX_TRACKS){
621 mp_msg(MSGT_DEMUX,MSGL_WARN,MSGTR_MOVtooManyTrk);
622 return;
624 if(!priv->track_db) mp_msg(MSGT_DEMUX, MSGL_V, "--------------\n");
625 trak=malloc(sizeof(mov_track_t));
626 memset(trak,0,sizeof(mov_track_t));
627 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: Track #%d:\n",priv->track_db);
628 trak->id=priv->track_db;
629 priv->tracks[priv->track_db]=trak;
630 lschunks(demuxer,level+1,pos+len,trak);
631 mov_build_index(trak,priv->timescale);
632 switch(trak->type){
633 case MOV_TRAK_AUDIO: {
634 #if 0
635 struct {
636 int16_t version; // 0 or 1 (version 1 is qt3.0+)
637 int16_t revision; // 0
638 int32_t vendor_id; // 0
639 int16_t channels; // 1 or 2 (Mono/Stereo)
640 int16_t samplesize; // 8 or 16 (8Bit/16Bit)
641 int16_t compression_id; // if version 0 then 0
642 // if version 1 and vbr then -2 else 0
643 int16_t packet_size; // 0
644 uint16_t sample_rate; // samplerate (Hz)
645 // qt3.0+ (version == 1)
646 uint32_t samples_per_packet; // 0 or num uncompressed samples in a packet
647 // if 0 below three values are also 0
648 uint32_t bytes_per_packet; // 0 or num compressed bytes for one channel
649 uint32_t bytes_per_frame; // 0 or num compressed bytes for all channels
650 // (channels * bytes_per_packet)
651 uint32_t bytes_per_sample; // 0 or size of uncompressed sample
652 // if samples_per_packet and bytes_per_packet are constant (CBR)
653 // then bytes_per_frame and bytes_per_sample must be 0 (else is VBR)
654 // ---
655 // optional additional atom-based fields
656 // ([int32_t size,int32_t type,some data ],repeat)
657 } my_stdata;
658 #endif
659 int version, adjust;
660 int is_vorbis = 0;
661 sh_audio_t* sh=new_sh_audio(demuxer,priv->track_db);
662 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_AudioID, "mov", priv->track_db);
663 sh->format=trak->fourcc;
665 // crude audio delay from editlist0 hack ::atm
666 if(trak->editlist_size>=1) {
667 if(trak->editlist[0].pos == -1) {
668 sh->stream_delay = (float)trak->editlist[0].dur/(float)priv->timescale;
669 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: Initial Audio-Delay: %.3f sec\n", sh->stream_delay);
674 switch( sh->format ) {
675 case 0x726D6173: /* samr */
676 /* amr narrowband */
677 trak->samplebytes=sh->samplesize=1;
678 trak->nchannels=sh->channels=1;
679 sh->samplerate=8000;
680 break;
682 case 0x62776173: /* sawb */
683 /* amr wideband */
684 trak->samplebytes=sh->samplesize=1;
685 trak->nchannels=sh->channels=1;
686 sh->samplerate=16000;
687 break;
689 default:
691 // assumptions for below table: short is 16bit, int is 32bit, intfp is 16bit
692 // XXX: 32bit fixed point numbers (intfp) are only 2 Byte!
693 // short values are usually one byte leftpadded by zero
694 // int values are usually two byte leftpadded by zero
695 // stdata[]:
696 // 8 short version
697 // 10 short revision
698 // 12 int vendor_id
699 // 16 short channels
700 // 18 short samplesize
701 // 20 short compression_id
702 // 22 short packet_size (==0)
703 // 24 intfp sample_rate
704 // (26 short) unknown (==0)
705 // ---- qt3.0+ (version>=1)
706 // 28 int samples_per_packet
707 // 32 int bytes_per_packet
708 // 36 int bytes_per_frame
709 // 40 int bytes_per_sample
710 // there may be additional atoms following at 28 (version 0)
711 // or 44 (version 1), eg. esds atom of .MP4 files
712 // esds atom:
713 // 28 int atom size (bytes of int size, int type and data)
714 // 32 char[4] atom type (fourc charater code -> esds)
715 // 36 char[] atom data (len=size-8)
717 // TODO: fix parsing for files using version 2.
718 version=char2short(trak->stdata,8);
719 if (version > 1)
720 mp_msg(MSGT_DEMUX, MSGL_WARN, "MOV: version %d sound atom may not parse correctly!\n", version);
721 trak->samplebytes=sh->samplesize=char2short(trak->stdata,18)/8;
723 /* I can't find documentation, but so far this is the case. -Corey */
724 switch (char2short(trak->stdata,16)) {
725 case 1:
726 trak->nchannels = 1; break;
727 case 2:
728 trak->nchannels = 2; break;
729 case 3:
730 trak->nchannels = 6; break;
731 default:
732 mp_msg(MSGT_DEMUX, MSGL_WARN,
733 "MOV: unable to determine audio channels, assuming 2 (got %d)\n",
734 char2short(trak->stdata,16));
735 trak->nchannels = 2;
737 sh->channels = trak->nchannels;
739 /*printf("MOV: timescale: %d samplerate: %d durmap: %d (%d) -> %d (%d)\n",
740 trak->timescale, char2short(trak->stdata,24), trak->durmap[0].dur,
741 trak->durmap[0].num, trak->timescale/trak->durmap[0].dur,
742 char2short(trak->stdata,24)/trak->durmap[0].dur);*/
743 sh->samplerate=char2short(trak->stdata,24);
744 if((sh->samplerate < 7000) && trak->durmap && trak->durmap[0].dur > 1) {
745 switch(char2short(trak->stdata,24)/trak->durmap[0].dur) {
746 // TODO: add more cases.
747 case 31:
748 sh->samplerate = 32000; break;
749 case 43:
750 sh->samplerate = 44100; break;
751 case 47:
752 sh->samplerate = 48000; break;
753 default:
754 mp_msg(MSGT_DEMUX, MSGL_WARN,
755 "MOV: unable to determine audio samplerate, "
756 "assuming 44.1kHz (got %d)\n",
757 char2short(trak->stdata,24)/trak->durmap[0].dur);
758 sh->samplerate = 44100;
762 mp_msg(MSGT_DEMUX, MSGL_V, "Audio bits: %d chans: %d rate: %d\n",
763 sh->samplesize*8,sh->channels,sh->samplerate);
765 if(trak->stdata_len >= 44 && trak->stdata[9]>=1){
766 mp_msg(MSGT_DEMUX,MSGL_V,"Audio header: samp/pack=%d bytes/pack=%d bytes/frame=%d bytes/samp=%d \n",
767 char2int(trak->stdata,28),
768 char2int(trak->stdata,32),
769 char2int(trak->stdata,36),
770 char2int(trak->stdata,40));
771 if(trak->stdata_len>=44+8){
772 int len=char2int(trak->stdata,44);
773 int fcc=char2int(trak->stdata,48);
774 // we have extra audio headers!!!
775 mp_msg(MSGT_DEMUX,MSGL_V,"Audio extra header: len=%d fcc=0x%X\n",len,fcc);
776 if((len >= 4) &&
777 (char2int(trak->stdata,52) >= 12) &&
778 (char2int(trak->stdata,52+4) == MOV_FOURCC('f','r','m','a'))) {
779 switch(char2int(trak->stdata,52+8)) {
780 case MOV_FOURCC('a','l','a','c'):
781 if (len >= 36 + char2int(trak->stdata,52)) {
782 sh->codecdata_len = char2int(trak->stdata,52+char2int(trak->stdata,52));
783 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: Found alac atom (%d)!\n", sh->codecdata_len);
784 sh->codecdata = malloc(sh->codecdata_len);
785 memcpy(sh->codecdata, &trak->stdata[52+char2int(trak->stdata,52)], sh->codecdata_len);
787 break;
788 case MOV_FOURCC('i','n','2','4'):
789 case MOV_FOURCC('i','n','3','2'):
790 case MOV_FOURCC('f','l','3','2'):
791 case MOV_FOURCC('f','l','6','4'):
792 if ((len >= 22) &&
793 (char2int(trak->stdata,52+16)==MOV_FOURCC('e','n','d','a')) &&
794 (char2short(trak->stdata,52+20))) {
795 sh->format=char2int(trak->stdata,52+8);
796 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: Found little endian PCM data, reversed fourcc:%04x\n", sh->format);
798 break;
799 default:
800 if (len > 8 && len + 44 <= trak->stdata_len) {
801 sh->codecdata_len = len-8;
802 sh->codecdata = trak->stdata+44+8;
805 } else {
806 if (len > 8 && len + 44 <= trak->stdata_len) {
807 sh->codecdata_len = len-8;
808 sh->codecdata = trak->stdata+44+8;
814 switch (version) {
815 case 0:
816 adjust = 0; break;
817 case 1:
818 adjust = 48; break;
819 case 2:
820 adjust = 68; break;
821 default:
822 mp_msg(MSGT_DEMUX, MSGL_WARN, "MOV: unknown sound atom version (%d); may not work!\n", version);
823 adjust = 68;
825 if (trak->stdata_len >= 36 + adjust) {
826 int atom_len = char2int(trak->stdata,28+adjust);
827 switch(char2int(trak->stdata,32+adjust)) { // atom type
828 case MOV_FOURCC('e','s','d','s'): {
829 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: Found MPEG4 audio Elementary Stream Descriptor atom (%d)!\n", atom_len);
830 if(atom_len > 8) {
831 esds_t esds;
832 if(!mp4_parse_esds(&trak->stdata[36+adjust], atom_len-8, &esds)) {
833 /* 0xdd is a "user private" id, not an official allocated id (see http://www.mp4ra.org/object.html),
834 so perform some extra checks to be sure that this is really vorbis audio */
835 if(esds.objectTypeId==0xdd && esds.streamType==0x15 && sh->format==0x6134706D && esds.decoderConfigLen > 8)
837 //vorbis audio
838 unsigned char *buf[3];
839 unsigned short sizes[3];
840 int offset, len, k;
841 unsigned char *ptr = esds.decoderConfig;
843 if(ptr[0] != 0 || ptr[1] != 30) goto quit_vorbis_block; //wrong extradata layout
845 offset = len = 0;
846 for(k = 0; k < 3; k++)
848 sizes[k] = (ptr[offset]<<8) | ptr[offset+1];
849 len += sizes[k];
850 offset += 2;
851 if(offset + sizes[k] > esds.decoderConfigLen)
853 mp_msg(MSGT_DEMUX, MSGL_FATAL, "MOV: ERROR!, not enough vorbis extradata to read: offset = %d, k=%d, size=%d, len: %d\n", offset, k, sizes[k], esds.decoderConfigLen);
854 goto quit_vorbis_block;
856 buf[k] = malloc(sizes[k]);
857 if(!buf[k]) goto quit_vorbis_block;
858 memcpy(buf[k], &ptr[offset], sizes[k]);
859 offset += sizes[k];
862 sh->codecdata_len = len + len/255 + 64;
863 sh->codecdata = malloc(sh->codecdata_len);
864 ptr = sh->codecdata;
866 ptr[0] = 2;
867 offset = 1;
868 offset += store_ughvlc(&ptr[offset], sizes[0]);
869 offset += store_ughvlc(&ptr[offset], sizes[1]);
870 for(k = 0; k < 3; k++)
872 memcpy(&ptr[offset], buf[k], sizes[k]);
873 offset += sizes[k];
876 sh->codecdata_len = offset;
877 sh->codecdata = realloc(sh->codecdata, offset);
878 mp_msg(MSGT_DEMUX,MSGL_V, "demux_mov, vorbis extradata size: %d\n", offset);
879 is_vorbis = 1;
880 quit_vorbis_block:
881 sh->format = mmioFOURCC('v', 'r', 'b', 's');
883 sh->i_bps = esds.avgBitrate/8;
885 // printf("######## audio format = %d ########\n",esds.objectTypeId);
886 if(esds.objectTypeId==MP4OTI_MPEG1Audio || esds.objectTypeId==MP4OTI_MPEG2AudioPart3)
887 sh->format=0x55; // .mp3
889 if(esds.objectTypeId==MP4OTI_13kVoice) { // 13K Voice, defined by 3GPP2
890 sh->format=mmioFOURCC('Q', 'c', 'l', 'p');
891 trak->nchannels=sh->channels=1;
892 trak->samplebytes=sh->samplesize=1;
895 // dump away the codec specific configuration for the AAC decoder
896 if(esds.decoderConfigLen){
897 if( (esds.decoderConfig[0]>>3) == 29 )
898 sh->format = 0x1d61346d; // request multi-channel mp3 decoder
899 if(!is_vorbis)
901 sh->codecdata_len = esds.decoderConfigLen;
902 sh->codecdata = malloc(sh->codecdata_len);
903 memcpy(sh->codecdata, esds.decoderConfig, sh->codecdata_len);
907 mp4_free_esds(&esds); // freeup esds mem
908 #if 0
909 { FILE* f=fopen("esds.dat","wb");
910 fwrite(&trak->stdata[36],atom_len-8,1,f);
911 fclose(f); }
912 #endif
914 } break;
915 case MOV_FOURCC('a','l','a','c'): {
916 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: Found alac atom (%d)!\n", atom_len);
917 if(atom_len > 8) {
918 // copy all the atom (not only payload) for lavc alac decoder
919 sh->codecdata_len = atom_len;
920 sh->codecdata = malloc(sh->codecdata_len);
921 memcpy(sh->codecdata, &trak->stdata[28], sh->codecdata_len);
923 } break;
924 case MOV_FOURCC('d','a','m','r'):
925 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);
926 if (atom_len>14) {
927 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]);
928 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: Modes set: %02x%02x\n",trak->stdata[41+adjust],trak->stdata[42+adjust]);
929 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: Mode change period: %d Frames per sample: %d\n",trak->stdata[43+adjust],trak->stdata[44+adjust]);
931 break;
932 default:
933 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: Found unknown audio atom %c%c%c%c (%d)!\n",
934 trak->stdata[32+adjust],trak->stdata[33+adjust],trak->stdata[34+adjust],trak->stdata[35+adjust],
935 atom_len);
938 mp_msg(MSGT_DEMUX, MSGL_V, "Fourcc: %.4s\n",(char *)&trak->fourcc);
939 #if 0
940 { FILE* f=fopen("stdata.dat","wb");
941 fwrite(trak->stdata,trak->stdata_len,1,f);
942 fclose(f); }
943 { FILE* f=fopen("tkdata.dat","wb");
944 fwrite(trak->tkdata,trak->tkdata_len,1,f);
945 fclose(f); }
946 #endif
947 // Emulate WAVEFORMATEX struct:
948 sh->wf=malloc(sizeof(WAVEFORMATEX) + (is_vorbis ? sh->codecdata_len : 0));
949 memset(sh->wf,0,sizeof(WAVEFORMATEX));
950 sh->wf->nChannels=sh->channels;
951 sh->wf->wBitsPerSample=(trak->stdata[18]<<8)+trak->stdata[19];
952 // sh->wf->nSamplesPerSec=trak->timescale;
953 sh->wf->nSamplesPerSec=sh->samplerate;
954 if(trak->stdata_len >= 44 && trak->stdata[9]>=1 && char2int(trak->stdata,28)>0){
955 //Audio header: samp/pack=4096 bytes/pack=743 bytes/frame=1486 bytes/samp=2
956 sh->wf->nAvgBytesPerSec=(sh->wf->nChannels*sh->wf->nSamplesPerSec*
957 char2int(trak->stdata,32)+char2int(trak->stdata,28)/2)
958 /char2int(trak->stdata,28);
959 sh->wf->nBlockAlign=char2int(trak->stdata,36);
960 } else {
961 sh->wf->nAvgBytesPerSec=sh->wf->nChannels*sh->wf->wBitsPerSample*sh->wf->nSamplesPerSec/8;
962 // workaround for ms11 ima4
963 if (sh->format == 0x1100736d && trak->stdata_len >= 36)
964 sh->wf->nBlockAlign=char2int(trak->stdata,36);
967 if(is_vorbis && sh->codecdata_len)
969 memcpy(sh->wf+1, sh->codecdata, sh->codecdata_len);
970 sh->wf->cbSize = sh->codecdata_len;
972 // Selection:
973 // if(demuxer->audio->id==-1 || demuxer->audio->id==priv->track_db){
974 // // (auto)selected audio track:
975 // demuxer->audio->id=priv->track_db;
976 // demuxer->audio->sh=sh; sh->ds=demuxer->audio;
977 // }
978 break;
980 case MOV_TRAK_VIDEO: {
981 int depth, i, entry;
982 int flag, start, count_flag, end, palette_count, gray;
983 int hdr_ptr = 76; // the byte just after depth
984 unsigned char *palette_map;
985 sh_video_t* sh=new_sh_video(demuxer,priv->track_db);
986 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_VideoID, "mov", priv->track_db);
987 sh->format=trak->fourcc;
989 // crude video delay from editlist0 hack ::atm
990 if(trak->editlist_size>=1) {
991 if(trak->editlist[0].pos == -1) {
992 sh->stream_delay = (float)trak->editlist[0].dur/(float)priv->timescale;
993 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: Initial Video-Delay: %.3f sec\n", sh->stream_delay);
998 if (trak->stdata_len < 78) {
999 mp_msg(MSGT_DEMUXER, MSGL_WARN,
1000 "MOV: Invalid (%d bytes instead of >= 78) video trak desc\n",
1001 trak->stdata_len);
1002 break;
1004 depth = trak->stdata[75] | (trak->stdata[74] << 8);
1005 // stdata[]:
1006 // 8 short version
1007 // 10 short revision
1008 // 12 int vendor_id
1009 // 16 int temporal_quality
1010 // 20 int spatial_quality
1011 // 24 short width
1012 // 26 short height
1013 // 28 int h_dpi
1014 // 32 int v_dpi
1015 // 36 int 0
1016 // 40 short frames_per_sample
1017 // 42 char[4] compressor_name
1018 // 74 short depth
1019 // 76 short color_table_id
1020 // additional atoms may follow,
1021 // eg esds atom from .MP4 files
1022 // 78 int atom size
1023 // 82 char[4] atom type
1024 // 86 ... atom data
1026 { ImageDescription* id=malloc(8+trak->stdata_len); // safe
1027 trak->desc=id;
1028 id->idSize=8+trak->stdata_len;
1029 // id->cType=bswap_32(trak->fourcc);
1030 id->cType=le2me_32(trak->fourcc);
1031 id->version=char2short(trak->stdata,8);
1032 id->revisionLevel=char2short(trak->stdata,10);
1033 id->vendor=char2int(trak->stdata,12);
1034 id->temporalQuality=char2int(trak->stdata,16);
1035 id->spatialQuality=char2int(trak->stdata,20);
1036 id->width=char2short(trak->stdata,24);
1037 id->height=char2short(trak->stdata,26);
1038 id->hRes=char2int(trak->stdata,28);
1039 id->vRes=char2int(trak->stdata,32);
1040 id->dataSize=char2int(trak->stdata,36);
1041 id->frameCount=char2short(trak->stdata,40);
1042 memcpy(&id->name,trak->stdata+42,32);
1043 id->depth=char2short(trak->stdata,74);
1044 id->clutID=char2short(trak->stdata,76);
1045 if(trak->stdata_len>78) memcpy(((char*)&id->clutID)+2,trak->stdata+78,trak->stdata_len-78);
1046 sh->ImageDesc=id;
1047 #if 0
1048 { FILE *f=fopen("ImageDescription","wb");
1049 fwrite(id,id->idSize,1,f);
1050 fclose(f);
1052 #endif
1055 if(trak->stdata_len >= 86) { // extra atoms found
1056 int pos=78;
1057 int atom_len;
1058 while(pos+8<=trak->stdata_len &&
1059 (pos+(atom_len=char2int(trak->stdata,pos)))<=trak->stdata_len){
1060 switch(char2int(trak->stdata,pos+4)) { // switch atom type
1061 case MOV_FOURCC('g','a','m','a'):
1062 // intfp with gamma value at which movie was captured
1063 // can be used to gamma correct movie display
1064 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: Found unsupported Gamma-Correction movie atom (%d)!\n",
1065 atom_len);
1066 break;
1067 case MOV_FOURCC('f','i','e','l'):
1068 // 2 char-values (8bit int) that specify field handling
1069 // see the Apple's QuickTime Fileformat PDF for more info
1070 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: Found unsupported Field-Handling movie atom (%d)!\n",
1071 atom_len);
1072 break;
1073 case MOV_FOURCC('m','j','q','t'):
1074 // Motion-JPEG default quantization table
1075 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: Found unsupported MJPEG-Quantization movie atom (%d)!\n",
1076 atom_len);
1077 break;
1078 case MOV_FOURCC('m','j','h','t'):
1079 // Motion-JPEG default huffman table
1080 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: Found unsupported MJPEG-Huffman movie atom (%d)!\n",
1081 atom_len);
1082 break;
1083 case MOV_FOURCC('e','s','d','s'):
1084 // MPEG4 Elementary Stream Descriptor header
1085 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: Found MPEG4 movie Elementary Stream Descriptor atom (%d)!\n", atom_len);
1086 // add code here to save esds header of length atom_len-8
1087 // beginning at stdata[86] to some variable to pass it
1088 // on to the decoder ::atmos
1089 if(atom_len > 8) {
1090 esds_t esds;
1091 if(!mp4_parse_esds(trak->stdata+pos+8, atom_len-8, &esds)) {
1093 if(esds.objectTypeId==MP4OTI_MPEG2VisualSimple || esds.objectTypeId==MP4OTI_MPEG2VisualMain ||
1094 esds.objectTypeId==MP4OTI_MPEG2VisualSNR || esds.objectTypeId==MP4OTI_MPEG2VisualSpatial ||
1095 esds.objectTypeId==MP4OTI_MPEG2VisualHigh || esds.objectTypeId==MP4OTI_MPEG2Visual422)
1096 sh->format=mmioFOURCC('m', 'p', 'g', '2');
1097 else if(esds.objectTypeId==MP4OTI_MPEG1Visual)
1098 sh->format=mmioFOURCC('m', 'p', 'g', '1');
1100 // dump away the codec specific configuration for the AAC decoder
1101 trak->stream_header_len = esds.decoderConfigLen;
1102 trak->stream_header = malloc(trak->stream_header_len);
1103 memcpy(trak->stream_header, esds.decoderConfig, trak->stream_header_len);
1105 mp4_free_esds(&esds); // freeup esds mem
1107 break;
1108 case MOV_FOURCC('a','v','c','C'):
1109 // AVC decoder configuration record
1110 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: AVC decoder configuration record atom (%d)!\n", atom_len);
1111 if(atom_len > 8) {
1112 int i, poffs, cnt;
1113 // Parse some parts of avcC, just for fun :)
1114 // real parsing is done by avc1 decoder
1115 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: avcC version: %d\n", *(trak->stdata+pos+8));
1116 if (*(trak->stdata+pos+8) != 1)
1117 mp_msg(MSGT_DEMUX, MSGL_ERR, "MOV: unknown avcC version (%d). Expexct problems.\n", *(trak->stdata+pos+9));
1118 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: avcC profile: %d\n", *(trak->stdata+pos+9));
1119 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: avcC profile compatibility: %d\n", *(trak->stdata+pos+10));
1120 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: avcC level: %d\n", *(trak->stdata+pos+11));
1121 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: avcC nal length size: %d\n", ((*(trak->stdata+pos+12))&0x03)+1);
1122 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: avcC number of sequence param sets: %d\n", cnt = (*(trak->stdata+pos+13) & 0x1f));
1123 poffs = pos + 14;
1124 for (i = 0; i < cnt; i++) {
1125 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: avcC sps %d have length %d\n", i, BE_16(trak->stdata+poffs));
1126 poffs += BE_16(trak->stdata+poffs) + 2;
1128 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: avcC number of picture param sets: %d\n", *(trak->stdata+poffs));
1129 poffs++;
1130 for (i = 0; i < cnt; i++) {
1131 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: avcC pps %d have length %d\n", i, BE_16(trak->stdata+poffs));
1132 poffs += BE_16(trak->stdata+poffs) + 2;
1134 // Copy avcC for the AVC decoder
1135 // This data will be put in extradata below, where BITMAPINFOHEADER is created
1136 trak->stream_header_len = atom_len-8;
1137 trak->stream_header = malloc(trak->stream_header_len);
1138 memcpy(trak->stream_header, trak->stdata+pos+8, trak->stream_header_len);
1140 break;
1141 case MOV_FOURCC('d','2','6','3'):
1142 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);
1143 if (atom_len>10)
1144 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]);
1145 break;
1146 case 0:
1147 break;
1148 default:
1149 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: Found unknown movie atom %c%c%c%c (%d)!\n",
1150 trak->stdata[pos+4],trak->stdata[pos+5],trak->stdata[pos+6],trak->stdata[pos+7],
1151 atom_len);
1153 if(atom_len<8) break;
1154 pos+=atom_len;
1155 // printf("pos=%d max=%d\n",pos,trak->stdata_len);
1158 sh->fps=trak->timescale/
1159 ((trak->durmap_size>=1)?(float)trak->durmap[0].dur:1);
1160 sh->frametime=1.0f/sh->fps;
1162 sh->disp_w=trak->stdata[25]|(trak->stdata[24]<<8);
1163 sh->disp_h=trak->stdata[27]|(trak->stdata[26]<<8);
1164 // if image size is zero, fallback to display size
1165 if(!sh->disp_w && !sh->disp_h) {
1166 sh->disp_w=trak->tkdata[77]|(trak->tkdata[76]<<8);
1167 sh->disp_h=trak->tkdata[81]|(trak->tkdata[80]<<8);
1168 } else if(sh->disp_w!=(trak->tkdata[77]|(trak->tkdata[76]<<8))){
1169 // codec and display width differ... use display one for aspect
1170 sh->aspect=trak->tkdata[77]|(trak->tkdata[76]<<8);
1171 sh->aspect/=trak->tkdata[81]|(trak->tkdata[80]<<8);
1174 if(depth>32+8) mp_msg(MSGT_DEMUX, MSGL_INFO,"*** depth = 0x%X\n",depth);
1176 // palettized?
1177 gray = 0;
1178 if (depth > 32) { depth&=31; gray = 1; } // depth > 32 means grayscale
1179 if ((depth == 2) || (depth == 4) || (depth == 8))
1180 palette_count = (1 << depth);
1181 else
1182 palette_count = 0;
1184 // emulate BITMAPINFOHEADER:
1185 if (palette_count)
1187 sh->bih=malloc(sizeof(BITMAPINFOHEADER) + palette_count * 4);
1188 memset(sh->bih,0,sizeof(BITMAPINFOHEADER) + palette_count * 4);
1189 sh->bih->biSize=40 + palette_count * 4;
1190 // fetch the relevant fields
1191 flag = BE_16(&trak->stdata[hdr_ptr]);
1192 hdr_ptr += 2;
1193 start = BE_32(&trak->stdata[hdr_ptr]);
1194 hdr_ptr += 4;
1195 count_flag = BE_16(&trak->stdata[hdr_ptr]);
1196 hdr_ptr += 2;
1197 end = BE_16(&trak->stdata[hdr_ptr]);
1198 hdr_ptr += 2;
1199 palette_map = (unsigned char *)sh->bih + 40;
1200 mp_msg(MSGT_DEMUX, MSGL_V, "Allocated %d entries for palette\n",
1201 palette_count);
1202 mp_msg(MSGT_DEMUX, MSGL_DBG2, "QT palette: start: %x, end: %x, count flag: %d, flags: %x\n",
1203 start, end, count_flag, flag);
1205 /* XXX: problems with sample (statunit6.mov) with flag&0x4 set! - alex*/
1207 // load default palette
1208 if (flag & 0x08)
1210 if (gray)
1212 mp_msg(MSGT_DEMUX, MSGL_V, "Using default QT grayscale palette\n");
1213 if (palette_count == 16)
1214 memcpy(palette_map, qt_default_grayscale_palette_16, 16 * 4);
1215 else if (palette_count == 256) {
1216 memcpy(palette_map, qt_default_grayscale_palette_256, 256 * 4);
1217 if (trak->fourcc == mmioFOURCC('c','v','i','d')) {
1218 int i;
1219 // Hack for grayscale CVID, negative palette
1220 // If you have samples where this is not required contact me (rxt)
1221 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: greyscale cvid with default palette,"
1222 " enabling negative palette hack.\n");
1223 for (i = 0; i < 256 * 4; i++)
1224 palette_map[i] = palette_map[i] ^ 0xff;
1228 else
1230 mp_msg(MSGT_DEMUX, MSGL_V, "Using default QT colour palette\n");
1231 if (palette_count == 4)
1232 memcpy(palette_map, qt_default_palette_4, 4 * 4);
1233 else if (palette_count == 16)
1234 memcpy(palette_map, qt_default_palette_16, 16 * 4);
1235 else if (palette_count == 256)
1236 memcpy(palette_map, qt_default_palette_256, 256 * 4);
1239 // load palette from file
1240 else
1242 mp_msg(MSGT_DEMUX, MSGL_V, "Loading palette from file\n");
1243 for (i = start; i <= end; i++)
1245 entry = BE_16(&trak->stdata[hdr_ptr]);
1246 hdr_ptr += 2;
1247 // apparently, if count_flag is set, entry is same as i
1248 if (count_flag & 0x8000)
1249 entry = i;
1250 // only care about top 8 bits of 16-bit R, G, or B value
1251 if (entry <= palette_count && entry >= 0)
1253 palette_map[entry * 4 + 2] = trak->stdata[hdr_ptr + 0];
1254 palette_map[entry * 4 + 1] = trak->stdata[hdr_ptr + 2];
1255 palette_map[entry * 4 + 0] = trak->stdata[hdr_ptr + 4];
1256 mp_dbg(MSGT_DEMUX, MSGL_DBG2, "QT palette: added entry: %d of %d (colors: R:%x G:%x B:%x)\n",
1257 entry, palette_count,
1258 palette_map[entry * 4 + 2],
1259 palette_map[entry * 4 + 1],
1260 palette_map[entry * 4 + 0]);
1262 else
1263 mp_msg(MSGT_DEMUX, MSGL_V, "QT palette: skipped entry (out of count): %d of %d\n",
1264 entry, palette_count);
1265 hdr_ptr += 6;
1269 else
1271 if (trak->fourcc == mmioFOURCC('a','v','c','1')) {
1272 if (trak->stream_header_len > 0xffffffff - sizeof(BITMAPINFOHEADER)) {
1273 mp_msg(MSGT_DEMUXER, MSGL_ERR, "Invalid extradata size %d, skipping\n",trak->stream_header_len);
1274 trak->stream_header_len = 0;
1276 sh->bih=malloc(sizeof(BITMAPINFOHEADER) + trak->stream_header_len);
1277 memset(sh->bih,0,sizeof(BITMAPINFOHEADER) + trak->stream_header_len);
1278 sh->bih->biSize=40 + trak->stream_header_len;
1279 memcpy(((unsigned char *)sh->bih)+40, trak->stream_header, trak->stream_header_len);
1280 free (trak->stream_header);
1281 trak->stream_header_len = 0;
1282 trak->stream_header = NULL;
1283 } else {
1284 sh->bih=malloc(sizeof(BITMAPINFOHEADER));
1285 memset(sh->bih,0,sizeof(BITMAPINFOHEADER));
1286 sh->bih->biSize=40;
1289 sh->bih->biWidth=sh->disp_w;
1290 sh->bih->biHeight=sh->disp_h;
1291 sh->bih->biPlanes=0;
1292 sh->bih->biBitCount=depth;
1293 sh->bih->biCompression=trak->fourcc;
1294 sh->bih->biSizeImage=sh->bih->biWidth*sh->bih->biHeight;
1296 mp_msg(MSGT_DEMUX, MSGL_V, "Image size: %d x %d (%d bpp)\n",sh->disp_w,sh->disp_h,sh->bih->biBitCount);
1297 if(trak->tkdata_len>81)
1298 mp_msg(MSGT_DEMUX, MSGL_V, "Display size: %d x %d\n",
1299 trak->tkdata[77]|(trak->tkdata[76]<<8),
1300 trak->tkdata[81]|(trak->tkdata[80]<<8));
1301 mp_msg(MSGT_DEMUX, MSGL_V, "Fourcc: %.4s Codec: '%.*s'\n",(char *)&trak->fourcc,trak->stdata[42]&31,trak->stdata+43);
1303 // if(demuxer->video->id==-1 || demuxer->video->id==priv->track_db){
1304 // // (auto)selected video track:
1305 // demuxer->video->id=priv->track_db;
1306 // demuxer->video->sh=sh; sh->ds=demuxer->video;
1307 // }
1308 break;
1310 case MOV_TRAK_GENERIC:
1311 if (trak->fourcc == mmioFOURCC('m','p','4','s') ||
1312 trak->fourcc == mmioFOURCC('t','x','3','g') ||
1313 trak->fourcc == mmioFOURCC('t','e','x','t')) {
1314 sh_sub_t *sh = new_sh_sub(demuxer, priv->track_db);
1315 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_SubtitleID, "mov", priv->track_db);
1316 if (trak->fourcc == mmioFOURCC('m','p','4','s'))
1317 init_vobsub(sh, trak);
1318 else
1319 sh->type = 't';
1320 } else
1321 mp_msg(MSGT_DEMUX, MSGL_V, "Generic track - not completely understood! (id: %d)\n",
1322 trak->id);
1323 /* XXX: Also this contains the FLASH data */
1325 #if 0
1327 int pos = stream_tell(demuxer->stream);
1328 int i;
1329 int fd;
1330 char name[20];
1332 for (i=0; i<trak->samples_size; i++)
1334 char buf[trak->samples[i].size];
1335 stream_seek(demuxer->stream, trak->samples[i].pos);
1336 snprintf((char *)&name[0], 20, "samp%d", i);
1337 fd = open((char *)&name[0], O_CREAT|O_WRONLY);
1338 stream_read(demuxer->stream, &buf[0], trak->samples[i].size);
1339 write(fd, &buf[0], trak->samples[i].size);
1340 close(fd);
1342 for (i=0; i<trak->chunks_size; i++)
1344 char buf[trak->length];
1345 stream_seek(demuxer->stream, trak->chunks[i].pos);
1346 snprintf((char *)&name[0], 20, "chunk%d", i);
1347 fd = open((char *)&name[0], O_CREAT|O_WRONLY);
1348 stream_read(demuxer->stream, &buf[0], trak->length);
1349 write(fd, &buf[0], trak->length);
1350 close(fd);
1352 if (trak->samplesize > 0)
1354 char *buf;
1356 buf = malloc(trak->samplesize);
1357 stream_seek(demuxer->stream, trak->chunks[0].pos);
1358 snprintf((char *)&name[0], 20, "trak%d", trak->id);
1359 fd = open((char *)&name[0], O_CREAT|O_WRONLY);
1360 stream_read(demuxer->stream, buf, trak->samplesize);
1361 write(fd, buf, trak->samplesize);
1362 close(fd);
1364 stream_seek(demuxer->stream, pos);
1366 #endif
1367 break;
1368 default:
1369 mp_msg(MSGT_DEMUX, MSGL_V, "Unknown track type found (type: %d)\n", trak->type);
1370 break;
1372 mp_msg(MSGT_DEMUX, MSGL_V, "--------------\n");
1373 priv->track_db++;
1374 trak=NULL;
1375 break;
1377 #ifndef HAVE_ZLIB
1378 case MOV_FOURCC('c','m','o','v'): {
1379 mp_msg(MSGT_DEMUX,MSGL_ERR,MSGTR_MOVcomprhdr);
1380 return;
1382 #else
1383 case MOV_FOURCC('m','o','o','v'):
1384 case MOV_FOURCC('c','m','o','v'): {
1385 // mp_msg(MSGT_DEMUX,MSGL_ERR,MSGTR_MOVcomprhdr);
1386 lschunks(demuxer,level+1,pos+len,NULL);
1387 break;
1389 case MOV_FOURCC('d','c','o','m'): {
1390 // int temp=stream_read_dword(demuxer->stream);
1391 unsigned int algo=be2me_32(stream_read_dword(demuxer->stream));
1392 mp_msg(MSGT_DEMUX, MSGL_V, "Compressed header uses %.4s algo!\n",(char *)&algo);
1393 break;
1395 case MOV_FOURCC('c','m','v','d'): {
1396 // int temp=stream_read_dword(demuxer->stream);
1397 unsigned int moov_sz=stream_read_dword(demuxer->stream);
1398 unsigned int cmov_sz=len-4;
1399 unsigned char* cmov_buf;
1400 unsigned char* moov_buf;
1401 int zret;
1402 z_stream zstrm;
1403 stream_t* backup;
1405 if (moov_sz > SIZE_MAX - 16) {
1406 mp_msg(MSGT_DEMUX, MSGL_ERR, "Invalid cmvd atom size %d\n", moov_sz);
1407 break;
1409 cmov_buf=malloc(cmov_sz);
1410 moov_buf=malloc(moov_sz+16);
1411 mp_msg(MSGT_DEMUX, MSGL_V, "Compressed header size: %d / %d\n",cmov_sz,moov_sz);
1413 stream_read(demuxer->stream,cmov_buf,cmov_sz);
1415 zstrm.zalloc = (alloc_func)0;
1416 zstrm.zfree = (free_func)0;
1417 zstrm.opaque = (voidpf)0;
1418 zstrm.next_in = cmov_buf;
1419 zstrm.avail_in = cmov_sz;
1420 zstrm.next_out = moov_buf;
1421 zstrm.avail_out = moov_sz;
1423 zret = inflateInit(&zstrm);
1424 if (zret != Z_OK)
1425 { mp_msg(MSGT_DEMUX, MSGL_ERR, "QT cmov: inflateInit err %d\n",zret);
1426 return;
1428 zret = inflate(&zstrm, Z_NO_FLUSH);
1429 if ((zret != Z_OK) && (zret != Z_STREAM_END))
1430 { mp_msg(MSGT_DEMUX, MSGL_ERR, "QT cmov inflate: ERR %d\n",zret);
1431 return;
1433 #if 0
1434 else {
1435 FILE *DecOut;
1436 DecOut = fopen("Out.bin", "w");
1437 fwrite(moov_buf, 1, moov_sz, DecOut);
1438 fclose(DecOut);
1440 #endif
1441 if(moov_sz != zstrm.total_out)
1442 mp_msg(MSGT_DEMUX, MSGL_WARN, "Warning! moov size differs cmov: %d zlib: %ld\n",moov_sz,zstrm.total_out);
1443 zret = inflateEnd(&zstrm);
1445 backup=demuxer->stream;
1446 demuxer->stream=new_memory_stream(moov_buf,moov_sz);
1447 stream_skip(demuxer->stream,8);
1448 lschunks(demuxer,level+1,moov_sz,NULL); // parse uncompr. 'moov'
1449 //free_stream(demuxer->stream);
1450 demuxer->stream=backup;
1451 free(cmov_buf);
1452 free(moov_buf);
1453 break;
1455 #endif
1456 case MOV_FOURCC('u','d','t','a'):
1458 unsigned int udta_id;
1459 off_t udta_len;
1460 off_t udta_size = len;
1462 mp_msg(MSGT_DEMUX, MSGL_DBG2, "mov: user data record found\n");
1463 mp_msg(MSGT_DEMUX, MSGL_V, "Quicktime Clip Info:\n");
1465 while((len > 8) && (udta_size > 8))
1467 udta_len = stream_read_dword(demuxer->stream);
1468 udta_id = stream_read_dword(demuxer->stream);
1469 udta_size -= 8;
1470 mp_msg(MSGT_DEMUX, MSGL_DBG2, "udta_id: %.4s (len: %"PRId64")\n", (char *)&udta_id, (int64_t)udta_len);
1471 switch (udta_id)
1473 case MOV_FOURCC(0xa9,'c','p','y'):
1474 case MOV_FOURCC(0xa9,'d','a','y'):
1475 case MOV_FOURCC(0xa9,'d','i','r'):
1476 /* 0xa9,'e','d','1' - '9' : edit timestamps */
1477 case MOV_FOURCC(0xa9,'f','m','t'):
1478 case MOV_FOURCC(0xa9,'i','n','f'):
1479 case MOV_FOURCC(0xa9,'p','r','d'):
1480 case MOV_FOURCC(0xa9,'p','r','f'):
1481 case MOV_FOURCC(0xa9,'r','e','q'):
1482 case MOV_FOURCC(0xa9,'s','r','c'):
1483 case MOV_FOURCC('n','a','m','e'):
1484 case MOV_FOURCC(0xa9,'n','a','m'):
1485 case MOV_FOURCC(0xa9,'A','R','T'):
1486 case MOV_FOURCC(0xa9,'c','m','t'):
1487 case MOV_FOURCC(0xa9,'a','u','t'):
1488 case MOV_FOURCC(0xa9,'s','w','r'):
1490 off_t text_len = stream_read_word(demuxer->stream);
1491 char text[text_len+2+1];
1492 stream_read(demuxer->stream, (char *)&text, text_len+2);
1493 text[text_len+2] = 0x0;
1494 switch(udta_id)
1496 case MOV_FOURCC(0xa9,'a','u','t'):
1497 demux_info_add(demuxer, "author", &text[2]);
1498 mp_msg(MSGT_DEMUX, MSGL_V, " Author: %s\n", &text[2]);
1499 break;
1500 case MOV_FOURCC(0xa9,'c','p','y'):
1501 demux_info_add(demuxer, "copyright", &text[2]);
1502 mp_msg(MSGT_DEMUX, MSGL_V, " Copyright: %s\n", &text[2]);
1503 break;
1504 case MOV_FOURCC(0xa9,'i','n','f'):
1505 mp_msg(MSGT_DEMUX, MSGL_V, " Info: %s\n", &text[2]);
1506 break;
1507 case MOV_FOURCC('n','a','m','e'):
1508 case MOV_FOURCC(0xa9,'n','a','m'):
1509 demux_info_add(demuxer, "name", &text[2]);
1510 mp_msg(MSGT_DEMUX, MSGL_V, " Name: %s\n", &text[2]);
1511 break;
1512 case MOV_FOURCC(0xa9,'A','R','T'):
1513 mp_msg(MSGT_DEMUX, MSGL_V, " Artist: %s\n", &text[2]);
1514 break;
1515 case MOV_FOURCC(0xa9,'d','i','r'):
1516 mp_msg(MSGT_DEMUX, MSGL_V, " Director: %s\n", &text[2]);
1517 break;
1518 case MOV_FOURCC(0xa9,'c','m','t'):
1519 demux_info_add(demuxer, "comments", &text[2]);
1520 mp_msg(MSGT_DEMUX, MSGL_V, " Comment: %s\n", &text[2]);
1521 break;
1522 case MOV_FOURCC(0xa9,'r','e','q'):
1523 mp_msg(MSGT_DEMUX, MSGL_V, " Requirements: %s\n", &text[2]);
1524 break;
1525 case MOV_FOURCC(0xa9,'s','w','r'):
1526 demux_info_add(demuxer, "encoder", &text[2]);
1527 mp_msg(MSGT_DEMUX, MSGL_V, " Software: %s\n", &text[2]);
1528 break;
1529 case MOV_FOURCC(0xa9,'d','a','y'):
1530 mp_msg(MSGT_DEMUX, MSGL_V, " Creation timestamp: %s\n", &text[2]);
1531 break;
1532 case MOV_FOURCC(0xa9,'f','m','t'):
1533 mp_msg(MSGT_DEMUX, MSGL_V, " Format: %s\n", &text[2]);
1534 break;
1535 case MOV_FOURCC(0xa9,'p','r','d'):
1536 mp_msg(MSGT_DEMUX, MSGL_V, " Producer: %s\n", &text[2]);
1537 break;
1538 case MOV_FOURCC(0xa9,'p','r','f'):
1539 mp_msg(MSGT_DEMUX, MSGL_V, " Performer(s): %s\n", &text[2]);
1540 break;
1541 case MOV_FOURCC(0xa9,'s','r','c'):
1542 mp_msg(MSGT_DEMUX, MSGL_V, " Source providers: %s\n", &text[2]);
1543 break;
1545 udta_size -= 4+text_len;
1546 break;
1548 /* some other shits: WLOC - window location,
1549 LOOP - looping style,
1550 SelO - play only selected frames
1551 AllF - play all frames
1553 case MOV_FOURCC('W','L','O','C'):
1554 case MOV_FOURCC('L','O','O','P'):
1555 case MOV_FOURCC('S','e','l','O'):
1556 case MOV_FOURCC('A','l','l','F'):
1557 default:
1559 if( udta_len>udta_size)
1560 udta_len=udta_size;
1562 char dump[udta_len-4];
1563 stream_read(demuxer->stream, (char *)&dump, udta_len-4-4);
1564 udta_size -= udta_len;
1569 break;
1570 } /* eof udta */
1571 default:
1572 id = be2me_32(id);
1573 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: unknown chunk: %.4s %d\n",(char *)&id,(int)len);
1574 } /* endof switch */
1575 } /* endof else */
1577 pos+=len+8;
1578 if(pos>=endpos) break;
1579 if(!stream_seek(demuxer->stream,pos)) break;
1583 static int lschunks_intrak(demuxer_t* demuxer, int level, unsigned int id,
1584 off_t pos, off_t len, mov_track_t* trak)
1586 switch(id) {
1587 case MOV_FOURCC('m','d','a','t'): {
1588 mp_msg(MSGT_DEMUX,MSGL_WARN,"Hmm, strange MOV, parsing mdat in lschunks?\n");
1589 return -1;
1591 case MOV_FOURCC('f','r','e','e'):
1592 case MOV_FOURCC('u','d','t','a'):
1593 /* here not supported :p */
1594 break;
1595 case MOV_FOURCC('t','k','h','d'): {
1596 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*sTrack header!\n", level, "");
1597 // read codec data
1598 trak->tkdata_len = len;
1599 trak->tkdata = malloc(trak->tkdata_len);
1600 stream_read(demuxer->stream, trak->tkdata, trak->tkdata_len);
1602 0 1 Version
1603 1 3 Flags
1604 4 4 Creation time
1605 8 4 Modification time
1606 12 4 Track ID
1607 16 4 Reserved
1608 20 4 Duration
1609 24 8 Reserved
1610 32 2 Layer
1611 34 2 Alternate group
1612 36 2 Volume
1613 38 2 Reserved
1614 40 36 Matrix structure
1615 76 4 Track width
1616 80 4 Track height
1618 mp_msg(MSGT_DEMUX, MSGL_V,
1619 "tkhd len=%d ver=%d flags=0x%X id=%d dur=%d lay=%d vol=%d\n",
1620 trak->tkdata_len, trak->tkdata[0], trak->tkdata[1],
1621 char2int(trak->tkdata, 12), // id
1622 char2int(trak->tkdata, 20), // duration
1623 char2short(trak->tkdata, 32), // layer
1624 char2short(trak->tkdata, 36)); // volume
1625 break;
1627 case MOV_FOURCC('m','d','h','d'): {
1628 int version = stream_read_char(demuxer->stream);
1629 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: %*sMedia header!\n", level, "");
1630 stream_skip(demuxer->stream, (version == 1) ? 19 : 11);
1631 // read timescale
1632 trak->timescale = stream_read_dword(demuxer->stream);
1633 // read length
1634 if (version == 1)
1635 trak->length = stream_read_qword(demuxer->stream);
1636 else
1637 trak->length = stream_read_dword(demuxer->stream);
1638 break;
1640 case MOV_FOURCC('h','d','l','r'): {
1641 unsigned int tmp = stream_read_dword(demuxer->stream);
1642 unsigned int type = stream_read_dword_le(demuxer->stream);
1643 unsigned int subtype = stream_read_dword_le(demuxer->stream);
1644 unsigned int manufact = stream_read_dword_le(demuxer->stream);
1645 unsigned int comp_flags = stream_read_dword(demuxer->stream);
1646 unsigned int comp_mask = stream_read_dword(demuxer->stream);
1647 int len = stream_read_char(demuxer->stream);
1648 char* str = malloc(len + 1);
1649 stream_read(demuxer->stream, str, len);
1650 str[len] = 0;
1651 mp_msg(MSGT_DEMUX, MSGL_V,
1652 "MOV: %*sHandler header: %.4s/%.4s (%.4s) %s\n", level, "",
1653 (char *)&type, (char *)&subtype, (char *)&manufact, str);
1654 free(str);
1655 switch(bswap_32(type)) {
1656 case MOV_FOURCC('m','h','l','r'):
1657 trak->media_handler = bswap_32(subtype);
1658 break;
1659 case MOV_FOURCC('d','h','l','r'):
1660 trak->data_handler = bswap_32(subtype);
1661 break;
1662 default:
1663 mp_msg(MSGT_DEMUX, MSGL_V,
1664 "MOV: unknown handler class: 0x%X (%.4s)\n",
1665 bswap_32(type), (char *)&type);
1667 break;
1669 case MOV_FOURCC('v','m','h','d'): {
1670 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: %*sVideo header!\n", level, "");
1671 trak->type = MOV_TRAK_VIDEO;
1672 // read video data
1673 break;
1675 case MOV_FOURCC('s','m','h','d'): {
1676 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: %*sSound header!\n", level, "");
1677 trak->type = MOV_TRAK_AUDIO;
1678 // read audio data
1679 break;
1681 case MOV_FOURCC('g','m','h','d'): {
1682 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: %*sGeneric header!\n", level, "");
1683 trak->type = MOV_TRAK_GENERIC;
1684 break;
1686 case MOV_FOURCC('n','m','h','d'): {
1687 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: %*sGeneric header!\n", level, "");
1688 trak->type = MOV_TRAK_GENERIC;
1689 break;
1691 case MOV_FOURCC('s','t','s','d'): {
1692 int i = stream_read_dword(demuxer->stream); // temp!
1693 int count = stream_read_dword(demuxer->stream);
1694 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: %*sDescription list! (cnt:%d)\n",
1695 level, "", count);
1696 for (i = 0; i < count; i++) {
1697 off_t pos = stream_tell(demuxer->stream);
1698 off_t len = stream_read_dword(demuxer->stream);
1699 unsigned int fourcc = stream_read_dword_le(demuxer->stream);
1700 /* some files created with Broadcast 2000 (e.g. ilacetest.mov)
1701 contain raw I420 video but have a yv12 fourcc */
1702 if (fourcc == mmioFOURCC('y','v','1','2'))
1703 fourcc = mmioFOURCC('I','4','2','0');
1704 if (len < 8)
1705 break; // error
1706 mp_msg(MSGT_DEMUX, MSGL_V,
1707 "MOV: %*s desc #%d: %.4s (%"PRId64" bytes)\n", level, "",
1708 i, (char *)&fourcc, (int64_t)len - 16);
1709 if (fourcc != trak->fourcc && i)
1710 mp_msg(MSGT_DEMUX, MSGL_WARN, MSGTR_MOVvariableFourCC);
1711 // if(!i)
1713 trak->fourcc = fourcc;
1714 // read type specific (audio/video/time/text etc) header
1715 // NOTE: trak type is not yet known at this point :(((
1716 trak->stdata_len = len - 8;
1717 trak->stdata = malloc(trak->stdata_len);
1718 stream_read(demuxer->stream, trak->stdata, trak->stdata_len);
1720 if (!stream_seek(demuxer->stream, pos + len))
1721 break;
1723 break;
1725 case MOV_FOURCC('s','t','t','s'): {
1726 int temp = stream_read_dword(demuxer->stream);
1727 int len = stream_read_dword(demuxer->stream);
1728 int i;
1729 unsigned int pts = 0;
1730 mp_msg(MSGT_DEMUX, MSGL_V,
1731 "MOV: %*sSample duration table! (%d blocks)\n", level, "",
1732 len);
1733 trak->durmap = calloc(len, sizeof(mov_durmap_t));
1734 trak->durmap_size = len;
1735 for (i = 0; i < len; i++) {
1736 trak->durmap[i].num = stream_read_dword(demuxer->stream);
1737 trak->durmap[i].dur = stream_read_dword(demuxer->stream);
1738 pts += trak->durmap[i].num * trak->durmap[i].dur;
1740 if (trak->length != pts)
1741 mp_msg(MSGT_DEMUX, MSGL_WARN, "Warning! pts=%d length=%d\n",
1742 pts, trak->length);
1743 break;
1745 case MOV_FOURCC('s','t','s','c'): {
1746 int temp = stream_read_dword(demuxer->stream);
1747 int len = stream_read_dword(demuxer->stream);
1748 int ver = (temp << 24);
1749 int flags = (temp << 16) | (temp << 8) | temp;
1750 int i;
1751 mp_msg(MSGT_DEMUX, MSGL_V,
1752 "MOV: %*sSample->Chunk mapping table! (%d blocks) (ver:%d,flags:%d)\n", level, "",
1753 len, ver, flags);
1754 // read data:
1755 trak->chunkmap_size = len;
1756 trak->chunkmap = calloc(len, sizeof(mov_chunkmap_t));
1757 for (i = 0; i < len; i++) {
1758 trak->chunkmap[i].first = stream_read_dword(demuxer->stream) - 1;
1759 trak->chunkmap[i].spc = stream_read_dword(demuxer->stream);
1760 trak->chunkmap[i].sdid = stream_read_dword(demuxer->stream);
1762 break;
1764 case MOV_FOURCC('s','t','s','z'): {
1765 int temp = stream_read_dword(demuxer->stream);
1766 int ss=stream_read_dword(demuxer->stream);
1767 int ver = (temp << 24);
1768 int flags = (temp << 16) | (temp << 8) | temp;
1769 int entries = stream_read_dword(demuxer->stream);
1770 int i;
1771 mp_msg(MSGT_DEMUX, MSGL_V,
1772 "MOV: %*sSample size table! (entries=%d ss=%d) (ver:%d,flags:%d)\n", level, "",
1773 entries, ss, ver, flags);
1774 trak->samplesize = ss;
1775 if (!ss) {
1776 // variable samplesize
1777 trak->samples = realloc_struct(trak->samples, entries, sizeof(mov_sample_t));
1778 trak->samples_size = entries;
1779 for (i = 0; i < entries; i++)
1780 trak->samples[i].size = stream_read_dword(demuxer->stream);
1782 break;
1784 case MOV_FOURCC('s','t','c','o'): {
1785 int temp = stream_read_dword(demuxer->stream);
1786 int len = stream_read_dword(demuxer->stream);
1787 int i;
1788 mp_msg(MSGT_DEMUX, MSGL_V,
1789 "MOV: %*sChunk offset table! (%d chunks)\n", level, "",
1790 len);
1791 // extend array if needed:
1792 if (len > trak->chunks_size) {
1793 trak->chunks = realloc_struct(trak->chunks, len, sizeof(mov_chunk_t));
1794 trak->chunks_size = len;
1796 // read elements:
1797 for(i = 0; i < len; i++)
1798 trak->chunks[i].pos = stream_read_dword(demuxer->stream);
1799 break;
1801 case MOV_FOURCC('c','o','6','4'): {
1802 int temp = stream_read_dword(demuxer->stream);
1803 int len = stream_read_dword(demuxer->stream);
1804 int i;
1805 mp_msg(MSGT_DEMUX, MSGL_V,
1806 "MOV: %*s64bit chunk offset table! (%d chunks)\n", level, "",
1807 len);
1808 // extend array if needed:
1809 if (len > trak->chunks_size) {
1810 trak->chunks = realloc_struct(trak->chunks, len, sizeof(mov_chunk_t));
1811 trak->chunks_size = len;
1813 // read elements:
1814 for (i = 0; i < len; i++) {
1815 #ifndef _LARGEFILE_SOURCE
1816 if (stream_read_dword(demuxer->stream) != 0)
1817 mp_msg(MSGT_DEMUX, MSGL_WARN, "Chunk %d has got 64bit address, but you've MPlayer compiled without LARGEFILE support!\n", i);
1818 trak->chunks[i].pos = stream_read_dword(demuxer->stream);
1819 #else
1820 trak->chunks[i].pos = stream_read_qword(demuxer->stream);
1821 #endif
1823 break;
1825 case MOV_FOURCC('s','t','s','s'): {
1826 int temp = stream_read_dword(demuxer->stream);
1827 int entries = stream_read_dword(demuxer->stream);
1828 int ver = (temp << 24);
1829 int flags = (temp << 16) | (temp<<8) | temp;
1830 int i;
1831 mp_msg(MSGT_DEMUX, MSGL_V,
1832 "MOV: %*sSyncing samples (keyframes) table! (%d entries) (ver:%d,flags:%d)\n", level, "",
1833 entries, ver, flags);
1834 trak->keyframes_size = entries;
1835 trak->keyframes = calloc(entries, sizeof(unsigned int));
1836 for (i = 0; i < entries; i++)
1837 trak->keyframes[i] = stream_read_dword(demuxer->stream) - 1;
1838 break;
1840 case MOV_FOURCC('m','d','i','a'): {
1841 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: %*sMedia stream!\n", level, "");
1842 lschunks(demuxer, level + 1, pos + len, trak);
1843 break;
1845 case MOV_FOURCC('m','i','n','f'): {
1846 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: %*sMedia info!\n", level, "");
1847 lschunks(demuxer, level + 1 ,pos + len, trak);
1848 break;
1850 case MOV_FOURCC('s','t','b','l'): {
1851 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: %*sSample info!\n", level, "");
1852 lschunks(demuxer, level + 1, pos + len, trak);
1853 break;
1855 case MOV_FOURCC('e','d','t','s'): {
1856 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: %*sEdit atom!\n", level, "");
1857 lschunks(demuxer, level + 1, pos + len, trak);
1858 break;
1860 case MOV_FOURCC('e','l','s','t'): {
1861 int temp = stream_read_dword(demuxer->stream);
1862 int entries = stream_read_dword(demuxer->stream);
1863 int ver = (temp << 24);
1864 int flags = (temp << 16) | (temp << 8) | temp;
1865 int i;
1866 mp_msg(MSGT_DEMUX, MSGL_V,
1867 "MOV: %*sEdit list table (%d entries) (ver:%d,flags:%d)\n", level, "",
1868 entries, ver, flags);
1869 #if 1
1870 trak->editlist_size = entries;
1871 trak->editlist = calloc(trak->editlist_size, sizeof(mov_editlist_t));
1872 for (i = 0; i < entries; i++) {
1873 int dur = stream_read_dword(demuxer->stream);
1874 int mt = stream_read_dword(demuxer->stream);
1875 int mr = stream_read_dword(demuxer->stream); // 16.16fp
1876 trak->editlist[i].dur = dur;
1877 trak->editlist[i].pos = mt;
1878 trak->editlist[i].speed = mr;
1879 mp_msg(MSGT_DEMUX, MSGL_V,
1880 "MOV: %*s entry#%d: duration: %d start time: %d speed: %3.1fx\n", level, "",
1881 i, dur, mt, (float)mr/65536.0f);
1883 #endif
1884 break;
1886 case MOV_FOURCC('c','o','d','e'): {
1887 /* XXX: Implement atom 'code' for FLASH support */
1888 break;
1890 default:
1891 id = be2me_32(id);
1892 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: unknown chunk: %.4s %d\n",(char *)&id,(int)len);
1893 break;
1894 }//switch(id)
1895 return 0;
1898 static demuxer_t* mov_read_header(demuxer_t* demuxer){
1899 mov_priv_t* priv=demuxer->priv;
1900 int t_no;
1901 int best_a_id=-1, best_a_len=0;
1902 int best_v_id=-1, best_v_len=0;
1904 mp_msg(MSGT_DEMUX, MSGL_DBG3, "mov_read_header!\n");
1906 // Parse header:
1907 stream_reset(demuxer->stream);
1908 if(!stream_seek(demuxer->stream,priv->moov_start))
1910 mp_msg(MSGT_DEMUX,MSGL_ERR,"MOV: Cannot seek to the beginning of the Movie header (0x%"PRIx64")\n",
1911 (int64_t)priv->moov_start);
1912 return 0;
1914 lschunks(demuxer, 0, priv->moov_end, NULL);
1915 // just in case we have hit eof while parsing...
1916 demuxer->stream->eof = 0;
1917 // mp_msg(MSGT_DEMUX, MSGL_INFO, "--------------\n");
1919 // find the best (longest) streams:
1920 for(t_no=0;t_no<priv->track_db;t_no++){
1921 mov_track_t* trak=priv->tracks[t_no];
1922 int len=(trak->samplesize) ? trak->chunks_size : trak->samples_size;
1923 if(demuxer->a_streams[t_no]){ // need audio
1924 if(len>best_a_len){ best_a_len=len; best_a_id=t_no; }
1926 if(demuxer->v_streams[t_no]){ // need video
1927 if(len>best_v_len){ best_v_len=len; best_v_id=t_no; }
1930 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: longest streams: A: #%d (%d samples) V: #%d (%d samples)\n",
1931 best_a_id,best_a_len,best_v_id,best_v_len);
1932 if(demuxer->audio->id==-1 && best_a_id>=0) demuxer->audio->id=best_a_id;
1933 if(demuxer->video->id==-1 && best_v_id>=0) demuxer->video->id=best_v_id;
1935 // setup sh pointers:
1936 if(demuxer->audio->id>=0){
1937 sh_audio_t* sh=demuxer->a_streams[demuxer->audio->id];
1938 if(sh){
1939 demuxer->audio->sh=sh; sh->ds=demuxer->audio;
1940 } else {
1941 mp_msg(MSGT_DEMUX, MSGL_ERR, "MOV: selected audio stream (%d) does not exist\n",demuxer->audio->id);
1942 demuxer->audio->id=-2;
1945 if(demuxer->video->id>=0){
1946 sh_video_t* sh=demuxer->v_streams[demuxer->video->id];
1947 if(sh){
1948 demuxer->video->sh=sh; sh->ds=demuxer->video;
1949 } else {
1950 mp_msg(MSGT_DEMUX, MSGL_ERR, "MOV: selected video stream (%d) does not exist\n",demuxer->video->id);
1951 demuxer->video->id=-2;
1954 if(demuxer->sub->id>=0){
1955 sh_sub_t* sh=demuxer->s_streams[demuxer->sub->id];
1956 if(sh){
1957 demuxer->sub->sh=sh;
1958 } else {
1959 mp_msg(MSGT_DEMUX, MSGL_ERR, "MOV: selected subtitle stream (%d) does not exist\n",demuxer->sub->id);
1960 demuxer->sub->id=-2;
1964 if(demuxer->video->id<0 && demuxer->audio->id<0) {
1965 /* No AV streams found. Try to find an MPEG stream. */
1966 for(t_no=0;t_no<priv->track_db;t_no++){
1967 mov_track_t* trak=priv->tracks[t_no];
1968 if(trak->media_handler == MOV_FOURCC('M','P','E','G')) {
1969 stream_t *s;
1970 demuxer_t *od;
1972 demuxer->video->id = t_no;
1973 s = new_ds_stream(demuxer->video);
1974 od = demux_open(s, DEMUXER_TYPE_MPEG_PS, -1, -1, -1, NULL);
1975 if(od) return new_demuxers_demuxer(od, od, od);
1976 demuxer->video->id = -2; //new linked demuxer couldn't be allocated
1977 break;
1982 #if 0
1983 if( mp_msg_test(MSGT_DEMUX,MSGL_DBG3) ){
1984 for(t_no=0;t_no<priv->track_db;t_no++){
1985 mov_track_t* trak=priv->tracks[t_no];
1986 if(trak->type==MOV_TRAK_GENERIC){
1987 int i;
1988 int fd;
1989 char name[20];
1990 mp_msg(MSGT_DEMUX, MSGL_INFO, "MOV: Track #%d: Extracting %d data chunks to files\n",t_no,trak->samples_size);
1991 for (i=0; i<trak->samples_size; i++)
1993 int len=trak->samples[i].size;
1994 char buf[len];
1995 stream_seek(demuxer->stream, trak->samples[i].pos);
1996 snprintf(name, 20, "t%02d-s%03d.%s", t_no,i,
1997 (trak->media_handler==MOV_FOURCC('f','l','s','h')) ?
1998 "swf":"dump");
1999 fd = open(name, O_CREAT|O_WRONLY);
2000 // { int j;
2001 // for(j=0;j<trak->stdata_len-3; j++)
2002 // printf("stdata[%d]=0x%X ize=0x%X\n",j,char2int(trak->stdata,j),MOV_FOURCC('z','l','i','b'));
2003 // }
2004 if( //trak->media_handler==MOV_FOURCC('s','p','r','t') &&
2005 trak->stdata_len>=16 &&
2006 char2int(trak->stdata,12)==MOV_FOURCC('z','l','i','b')
2008 int newlen=stream_read_dword(demuxer->stream);
2009 #ifdef HAVE_ZLIB
2010 // unzip:
2011 z_stream zstrm;
2012 int zret;
2013 char buf2[newlen];
2015 len-=4;
2016 stream_read(demuxer->stream, buf, len);
2018 zstrm.zalloc = (alloc_func)0;
2019 zstrm.zfree = (free_func)0;
2020 zstrm.opaque = (voidpf)0;
2021 zstrm.next_in = buf;
2022 zstrm.avail_in = len;
2023 zstrm.next_out = buf2;
2024 zstrm.avail_out = newlen;
2026 zret = inflateInit(&zstrm);
2027 zret = inflate(&zstrm, Z_NO_FLUSH);
2028 if(newlen != zstrm.total_out)
2029 mp_msg(MSGT_DEMUX, MSGL_WARN, "Warning! unzipped frame size differs hdr: %d zlib: %ld\n",newlen,zstrm.total_out);
2031 write(fd, buf2, newlen);
2032 } else {
2033 #else
2034 len-=4;
2035 mp_msg(MSGT_DEMUX, MSGL_INFO, "******* ZLIB COMPRESSED SAMPLE!!!!! (%d->%d bytes) *******\n",len,newlen);
2038 #endif
2039 stream_read(demuxer->stream, buf, len);
2040 write(fd, buf, len);
2042 close(fd);
2047 demuxer->stream->eof = 0;
2048 #endif
2050 return demuxer;
2054 * \brief return the mov track that belongs to a demuxer stream
2055 * \param ds the demuxer stream, may be NULL
2056 * \return the mov track info structure belonging to the stream,
2057 * NULL if not found
2059 static mov_track_t *stream_track(mov_priv_t *priv, demux_stream_t *ds) {
2060 if (ds && (ds->id >= 0) && (ds->id < priv->track_db))
2061 return priv->tracks[ds->id];
2062 return NULL;
2065 // return value:
2066 // 0 = EOF or no stream found
2067 // 1 = successfully read a packet
2068 static int demux_mov_fill_buffer(demuxer_t *demuxer,demux_stream_t* ds){
2069 mov_priv_t* priv=demuxer->priv;
2070 mov_track_t* trak=NULL;
2071 float pts;
2072 int x;
2073 off_t pos;
2075 if (ds->eof) return 0;
2076 trak = stream_track(priv, ds);
2077 if (!trak) return 0;
2079 if(trak->samplesize){
2080 // read chunk:
2081 if(trak->pos>=trak->chunks_size) return 0; // EOF
2082 stream_seek(demuxer->stream,trak->chunks[trak->pos].pos);
2083 pts=(float)(trak->chunks[trak->pos].sample*trak->duration)/(float)trak->timescale;
2084 if(trak->samplesize!=1)
2086 mp_msg(MSGT_DEMUX, MSGL_DBG2, "WARNING! Samplesize(%d) != 1\n",
2087 trak->samplesize);
2088 if((trak->fourcc != MOV_FOURCC('t','w','o','s')) && (trak->fourcc != MOV_FOURCC('s','o','w','t')))
2089 x=trak->chunks[trak->pos].size*trak->samplesize;
2090 else
2091 x=trak->chunks[trak->pos].size;
2093 else
2094 x=trak->chunks[trak->pos].size;
2095 // printf("X = %d\n", x);
2096 /* the following stuff is audio related */
2097 if (trak->type == MOV_TRAK_AUDIO){
2098 if(trak->stdata_len>=44 && trak->stdata[9]>=1 && char2int(trak->stdata,28)>0){
2099 // stsd version 1 - we have audio compression ratio info:
2100 x/=char2int(trak->stdata,28); // samples/packet
2101 // x*=char2int(trak->stdata,32); // bytes/packet
2102 x*=char2int(trak->stdata,36); // bytes/frame
2103 } else {
2104 if(ds->ss_div && ds->ss_mul){
2105 // workaround for buggy files like 7up-high-traffic-areas.mov,
2106 // with missing stsd v1 header containing compression rate
2107 x/=ds->ss_div; x*=ds->ss_mul; // compression ratio fix ! HACK !
2108 } else {
2109 x*=trak->nchannels;
2110 x*=trak->samplebytes;
2113 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Audio sample %d bytes pts %5.3f\n",trak->chunks[trak->pos].size*trak->samplesize,pts);
2114 } /* MOV_TRAK_AUDIO */
2115 pos=trak->chunks[trak->pos].pos;
2116 } else {
2117 int frame=trak->pos;
2118 // editlist support:
2119 if(trak->type == MOV_TRAK_VIDEO && trak->editlist_size>=1){
2120 // find the right editlist entry:
2121 if(frame<trak->editlist[trak->editlist_pos].start_frame)
2122 trak->editlist_pos=0;
2123 while(trak->editlist_pos<trak->editlist_size-1 &&
2124 frame>=trak->editlist[trak->editlist_pos+1].start_frame)
2125 ++trak->editlist_pos;
2126 if(frame>=trak->editlist[trak->editlist_pos].start_frame+
2127 trak->editlist[trak->editlist_pos].frames) return 0; // EOF
2128 // calc real frame index:
2129 frame-=trak->editlist[trak->editlist_pos].start_frame;
2130 frame+=trak->editlist[trak->editlist_pos].start_sample;
2131 // calc pts:
2132 pts=(float)(trak->samples[frame].pts+
2133 trak->editlist[trak->editlist_pos].pts_offset)/(float)trak->timescale;
2134 } else {
2135 if(frame>=trak->samples_size) return 0; // EOF
2136 pts=(float)trak->samples[frame].pts/(float)trak->timescale;
2138 // read sample:
2139 stream_seek(demuxer->stream,trak->samples[frame].pos);
2140 x=trak->samples[frame].size;
2141 pos=trak->samples[frame].pos;
2143 if(trak->pos==0 && trak->stream_header_len>0){
2144 // we have to append the stream header...
2145 demux_packet_t* dp=new_demux_packet(x+trak->stream_header_len);
2146 memcpy(dp->buffer,trak->stream_header,trak->stream_header_len);
2147 stream_read(demuxer->stream,dp->buffer+trak->stream_header_len,x);
2148 free(trak->stream_header);
2149 trak->stream_header = NULL;
2150 trak->stream_header_len = 0;
2151 dp->pts=pts;
2152 dp->flags=0;
2153 dp->pos=pos; // FIXME?
2154 ds_add_packet(ds,dp);
2155 } else
2156 ds_read_packet(ds,demuxer->stream,x,pts,pos,0);
2158 ++trak->pos;
2160 trak = NULL;
2161 if (demuxer->sub->id >= 0 && demuxer->sub->id < priv->track_db)
2162 trak = priv->tracks[demuxer->sub->id];
2163 if (trak) {
2164 int samplenr = 0;
2165 while (samplenr < trak->samples_size) {
2166 double subpts = (double)trak->samples[samplenr].pts / (double)trak->timescale;
2167 if (subpts >= pts) break;
2168 samplenr++;
2170 samplenr--;
2171 if (samplenr < 0)
2172 vo_sub = NULL;
2173 else if (samplenr != priv->current_sub) {
2174 sh_sub_t *sh = demuxer->sub->sh;
2175 off_t pos = trak->samples[samplenr].pos;
2176 int len = trak->samples[samplenr].size;
2177 double subpts = (double)trak->samples[samplenr].pts / (double)trak->timescale;
2178 stream_seek(demuxer->stream, pos);
2179 if (sh->type != 'v') {
2180 stream_skip(demuxer->stream, 2); // size
2181 len -= 2;
2182 if (len < 0) len = 0;
2183 if (len > MOV_MAX_SUBLEN) len = MOV_MAX_SUBLEN;
2184 sub_utf8 = 1;
2186 ds_read_packet(demuxer->sub, demuxer->stream, len, subpts, pos, 0);
2187 priv->current_sub = samplenr;
2191 return 1;
2195 static float mov_seek_track(mov_track_t* trak,float pts,int flags){
2197 // printf("MOV track seek called %5.3f \n",pts);
2198 if(flags&2) pts*=trak->length; else pts*=(float)trak->timescale;
2200 if(trak->samplesize){
2201 int sample=pts/trak->duration;
2202 // printf("MOV track seek - chunk: %d (pts: %5.3f dur=%d) \n",sample,pts,trak->duration);
2203 if(!(flags&1)) sample+=trak->chunks[trak->pos].sample; // relative
2204 trak->pos=0;
2205 while(trak->pos<trak->chunks_size && trak->chunks[trak->pos].sample<sample) ++trak->pos;
2206 if (trak->pos == trak->chunks_size) return -1;
2207 pts=(float)(trak->chunks[trak->pos].sample*trak->duration)/(float)trak->timescale;
2208 } else {
2209 unsigned int ipts;
2210 if(!(flags&1)) pts+=trak->samples[trak->pos].pts;
2211 if(pts<0) pts=0;
2212 ipts=pts;
2213 //printf("MOV track seek - sample: %d \n",ipts);
2214 for(trak->pos=0;trak->pos<trak->samples_size;++trak->pos){
2215 if(trak->samples[trak->pos].pts>=ipts) break; // found it!
2217 if (trak->pos == trak->samples_size) return -1;
2218 if(trak->keyframes_size){
2219 // find nearest keyframe
2220 int i;
2221 for(i=0;i<trak->keyframes_size;i++){
2222 if(trak->keyframes[i]>=trak->pos) break;
2224 if (i == trak->keyframes_size) return -1;
2225 if(i>0 && (trak->keyframes[i]-trak->pos) > (trak->pos-trak->keyframes[i-1]))
2226 --i;
2227 trak->pos=trak->keyframes[i];
2228 // printf("nearest keyframe: %d \n",trak->pos);
2230 pts=(float)trak->samples[trak->pos].pts/(float)trak->timescale;
2233 // printf("MOV track seek done: %5.3f \n",pts);
2235 return pts;
2238 static void demux_seek_mov(demuxer_t *demuxer,float pts,float audio_delay,int flags){
2239 mov_priv_t* priv=demuxer->priv;
2240 demux_stream_t* ds;
2241 mov_track_t* trak;
2243 // printf("MOV seek called %5.3f flag=%d \n",pts,flags);
2245 ds=demuxer->video;
2246 trak = stream_track(priv, ds);
2247 if (trak) {
2248 //if(flags&2) pts*=(float)trak->length/(float)trak->timescale;
2249 //if(!(flags&1)) pts+=ds->pts;
2250 ds->pts=mov_seek_track(trak,pts,flags);
2251 if (ds->pts < 0) ds->eof = 1;
2252 else pts = ds->pts;
2253 flags=1; // absolute seconds
2256 ds=demuxer->audio;
2257 trak = stream_track(priv, ds);
2258 if (trak) {
2259 //if(flags&2) pts*=(float)trak->length/(float)trak->timescale;
2260 //if(!(flags&1)) pts+=ds->pts;
2261 ds->pts=mov_seek_track(trak,pts,flags);
2262 if (ds->pts < 0) ds->eof = 1;
2267 static int demux_mov_control(demuxer_t *demuxer, int cmd, void *arg){
2268 mov_track_t* track;
2270 // try the video track
2271 track = stream_track(demuxer->priv, demuxer->video);
2272 if (!track || !track->length)
2273 // otherwise try to get the info from the audio track
2274 track = stream_track(demuxer->priv, demuxer->audio);
2276 if (!track || !track->length)
2277 return DEMUXER_CTRL_DONTKNOW;
2279 switch(cmd) {
2280 case DEMUXER_CTRL_GET_TIME_LENGTH:
2281 if (!track->timescale)
2282 return DEMUXER_CTRL_DONTKNOW;
2283 *((double *)arg) = (double)track->length / track->timescale;
2284 return DEMUXER_CTRL_OK;
2286 case DEMUXER_CTRL_GET_PERCENT_POS:
2288 off_t pos = track->pos;
2289 if (track->durmap_size >= 1)
2290 pos *= track->durmap[0].dur;
2291 *((int *)arg) = (int)(100 * pos / track->length);
2292 return DEMUXER_CTRL_OK;
2295 return DEMUXER_CTRL_NOTIMPL;
2299 demuxer_desc_t demuxer_desc_mov = {
2300 "Quicktime/MP4 demuxer",
2301 "mov",
2302 "Quicktime/MOV",
2303 "Arpi, Al3x, Atmos, others",
2304 "Handles Quicktime, MP4, 3GP",
2305 DEMUXER_TYPE_MOV,
2306 0, // slow autodetect
2307 mov_check_file,
2308 demux_mov_fill_buffer,
2309 mov_read_header,
2310 demux_close_mov,
2311 demux_seek_mov,
2312 demux_mov_control