2 * QuickTime MOV file parser
3 * copyright(c) 2001 A'rpi
4 * additional work by Atmos
5 * based on TOOLS/movinfo.c by A'rpi & Al3x
6 * compressed header support from moov.c of the openquicktime lib.
8 * references: http://openquicktime.sf.net/, http://www.heroinewarrior.com/
9 * http://www.geocities.com/SiliconValley/Lakes/2160/fformats/files/mov.pdf
10 * (above URL no longer works, file mirrored somewhere? ::atmos)
11 * The QuickTime File Format PDF from Apple:
12 * http://developer.apple.com/techpubs/quicktime/qtdevdocs/PDF/QTFileFormat.pdf
13 * (Complete list of documentation at http://developer.apple.com/quicktime/)
14 * MP4-Lib sources from http://mpeg4ip.sf.net/ might be useful for .mp4
15 * as well as .mov specific stuff.
17 * All sort of Stuff about MPEG4:
18 * http://www.cmlab.csie.ntu.edu.tw/~pkhsiao/thesis.html
19 * I really recommend N4270-1.doc and N4270-2.doc which are exact specs
20 * of the MP4-File Format and the MPEG4 Specific extensions. ::atmos
22 * http://www.3gpp.org/ftp/tsg_sa/TSG_SA/TSGS_15/Docs/pdf/SP-020088.pdf
23 * http://www.3gpp2.org/Public_html/specs/C.S0050-0_v1.0_121503.pdf
25 * This file is part of MPlayer.
27 * MPlayer is free software; you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation; either version 2 of the License, or
30 * (at your option) any later version.
32 * MPlayer is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License along
38 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
39 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
49 #ifdef CONFIG_QUICKTIME
50 #include <QuickTime/QuickTime.h>
51 #include <QuickTime/ImageCompression.h>
52 #include <QuickTime/ImageCodec.h>
54 #include "loader/qtx/qtxsdk/components.h"
59 #include "stream/stream.h"
63 #include "libmpcodecs/img_format.h"
64 #include "libavutil/common.h"
65 #include "ffmpeg_files/intreadwrite.h"
67 #include "libvo/sub.h"
69 #include "demux_mov.h"
70 #include "qtpalette.h"
71 #include "parse_mp4.h" // .MP4 specific stuff
81 #define char2short(x,y) AV_RB16(&(x)[(y)])
82 #define char2int(x,y) AV_RB32(&(x)[(y)])
85 unsigned int pts
; // duration
91 unsigned int sample
; // number of the first sample in the chunk
92 unsigned int size
; // number of samples in the chunk
93 int desc
; // for multiple codecs mode - not used
119 #define MOV_TRAK_UNKNOWN 0
120 #define MOV_TRAK_VIDEO 1
121 #define MOV_TRAK_AUDIO 2
122 #define MOV_TRAK_FLASH 3
123 #define MOV_TRAK_GENERIC 4
124 #define MOV_TRAK_CODE 5
131 unsigned int media_handler
;
132 unsigned int data_handler
;
136 int samplesize
; // 0 = variable
137 int duration
; // 0 = variable
138 int width
,height
; // for video
140 unsigned int nchannels
;
141 unsigned int samplebytes
;
143 int tkdata_len
; // track data
144 unsigned char* tkdata
;
145 int stdata_len
; // stream data
146 unsigned char* stdata
;
148 unsigned char* stream_header
;
149 int stream_header_len
; // if >0, this header should be sent before the 1st frame
152 mov_sample_t
* samples
;
156 mov_chunkmap_t
* chunkmap
;
158 mov_durmap_t
* durmap
;
160 unsigned int* keyframes
;
162 mov_editlist_t
* editlist
;
165 void* desc
; // image/sound/etc description (pointer to ImageDescription etc)
168 static void mov_build_index(mov_track_t
* trak
,int timescale
){
170 int last
=trak
->chunks_size
;
174 if (trak
->chunks_size
<= 0)
176 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "No chunk offset table, trying to build one!\n");
178 trak
->chunks_size
= trak
->samples_size
; /* XXX: FIXME ! */
179 // audit: this code will be vulnerable if it is reenabled (currently #if 0)
180 trak
->chunks
= realloc(trak
->chunks
, sizeof(mov_chunk_t
)*trak
->chunks_size
);
182 for (i
=0; i
< trak
->chunks_size
; i
++)
183 trak
->chunks
[i
].pos
= -1;
187 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV track #%d: %d chunks, %d samples\n",trak
->id
,trak
->chunks_size
,trak
->samples_size
);
188 mp_msg(MSGT_DEMUX
, MSGL_V
, "pts=%d scale=%d time=%5.3f\n",trak
->length
,trak
->timescale
,(float)trak
->length
/(float)trak
->timescale
);
191 i
=trak
->chunkmap_size
;
194 j
=trak
->chunkmap
[i
].first
;
195 for(;j
>=0 && j
<last
;j
++){
196 trak
->chunks
[j
].desc
=trak
->chunkmap
[i
].sdid
;
197 trak
->chunks
[j
].size
=trak
->chunkmap
[i
].spc
;
199 last
=FFMIN(trak
->chunkmap
[i
].first
, trak
->chunks_size
);
203 for (i
=0; i
< trak
->chunks_size
; i
++)
206 if (trak
->chunks
[i
].pos
== -1)
208 trak
->chunks
[i
].pos
= trak
->chunks
[i
-1].pos
+ trak
->chunks
[i
-1].size
;
210 trak
->chunks
[i
].pos
= 0; /* FIXME: set initial pos */
213 // calc pts of chunks:
215 for(j
=0;j
<trak
->chunks_size
;j
++){
216 trak
->chunks
[j
].sample
=s
;
217 s
+=trak
->chunks
[j
].size
;
220 for (j
= 0; j
< trak
->durmap_size
; j
++)
221 i
+= trak
->durmap
[j
].num
;
223 mp_msg(MSGT_DEMUX
, MSGL_WARN
,
224 "MOV: durmap and chunkmap sample count differ (%i vs %i)\n", i
, s
);
228 // workaround for fixed-size video frames (dv and uncompressed)
229 if(!trak
->samples_size
&& trak
->type
!=MOV_TRAK_AUDIO
){
230 trak
->samples
=calloc(s
, sizeof(mov_sample_t
));
231 trak
->samples_size
=trak
->samples
? s
: 0;
232 for(i
=0;i
<trak
->samples_size
;i
++)
233 trak
->samples
[i
].size
=trak
->samplesize
;
237 if(!trak
->samples_size
){
238 // constant sampesize
239 if(trak
->durmap_size
==1 || (trak
->durmap_size
==2 && trak
->durmap
[1].num
==1)){
240 trak
->duration
=trak
->durmap
[0].dur
;
241 } else mp_msg(MSGT_DEMUX
, MSGL_ERR
, "*** constant samplesize & variable duration not yet supported! ***\nContact the author if you have such sample file!\n");
245 if (trak
->samples_size
< s
) {
246 mp_msg(MSGT_DEMUX
, MSGL_WARN
,
247 "MOV: durmap or chunkmap bigger than sample count (%i vs %i)\n",
248 s
, trak
->samples_size
);
249 trak
->samples
= realloc_struct(trak
->samples
, s
, sizeof(mov_sample_t
));
250 trak
->samples_size
= trak
->samples
? s
: 0;
255 for(j
=0;j
<trak
->durmap_size
;j
++){
256 for(i
=0;i
<trak
->durmap
[j
].num
;i
++){
257 if (s
>= trak
->samples_size
)
259 trak
->samples
[s
].pts
=pts
;
261 pts
+=trak
->durmap
[j
].dur
;
265 // calc sample offsets
267 for(j
=0;j
<trak
->chunks_size
;j
++){
268 off_t pos
=trak
->chunks
[j
].pos
;
269 for(i
=0;i
<trak
->chunks
[j
].size
;i
++){
270 if (s
>= trak
->samples_size
)
272 trak
->samples
[s
].pos
=pos
;
273 mp_msg(MSGT_DEMUX
, MSGL_DBG3
, "Sample %5d: pts=%8d off=0x%08X size=%d\n",s
,
274 trak
->samples
[s
].pts
,
275 (int)trak
->samples
[s
].pos
,
276 trak
->samples
[s
].size
);
277 pos
+=trak
->samples
[s
].size
;
282 // precalc editlist entries
283 if(trak
->editlist_size
>0){
286 for(i
=0;i
<trak
->editlist_size
;i
++){
287 mov_editlist_t
* el
=&trak
->editlist
[i
];
290 el
->start_frame
=frame
;
293 el
->frames
=0; continue;
296 for(;sample
<trak
->samples_size
;sample
++){
297 if(pts
<=trak
->samples
[sample
].pts
) break;
299 el
->start_sample
=sample
;
300 el
->pts_offset
=((long long)e_pts
*(long long)trak
->timescale
)/(long long)timescale
-trak
->samples
[sample
].pts
;
301 pts
+=((long long)el
->dur
*(long long)trak
->timescale
)/(long long)timescale
;
304 for(;sample
<trak
->samples_size
;sample
++){
305 if(pts
<trak
->samples
[sample
].pts
) break;
307 el
->frames
=sample
-el
->start_sample
;
309 mp_msg(MSGT_DEMUX
,MSGL_V
,"EL#%d: pts=%d 1st_sample=%d frames=%d (%5.3fs) pts_offs=%d\n",i
,
310 el
->pos
,el
->start_sample
, el
->frames
,
311 (float)(el
->dur
)/(float)timescale
, el
->pts_offset
);
317 #define MOV_MAX_TRACKS 256
318 #define MOV_MAX_SUBLEN 1024
326 mov_track_t
* tracks
[MOV_MAX_TRACKS
];
327 int timescale
; // movie timescale
328 int duration
; // movie duration (in movie timescale units)
330 char subtext
[MOV_MAX_SUBLEN
+ 1];
334 #define MOV_FOURCC(a,b,c,d) ((a<<24)|(b<<16)|(c<<8)|(d))
336 static int mov_check_file(demuxer_t
* demuxer
){
339 mov_priv_t
* priv
=calloc(1, sizeof(mov_priv_t
));
341 mp_msg(MSGT_DEMUX
,MSGL_V
,"Checking for MOV\n");
343 priv
->current_sub
= -1;
348 off_t len
=stream_read_dword(demuxer
->stream
);
349 unsigned int id
=stream_read_dword(demuxer
->stream
);
350 if(stream_eof(demuxer
->stream
)) break; // EOF
351 if (len
== 1) /* real size is 64bits - cjb */
353 #ifndef _LARGEFILE_SOURCE
354 if (stream_read_dword(demuxer
->stream
) != 0)
355 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "64bit file, but you've compiled MPlayer without LARGEFILE support!\n");
356 len
= stream_read_dword(demuxer
->stream
);
358 len
= stream_read_qword(demuxer
->stream
);
363 else if (len
== 0) /* deleted chunk */
365 /* XXX: CJB! is this right? - alex */
369 else if(len
<8) break; // invalid chunk
372 case MOV_FOURCC('f','t','y','p'): {
374 // File Type Box (ftyp):
375 // char[4] major_brand (eg. 'isom')
376 // int minor_version (eg. 0x00000000)
377 // char[4] compatible_brands[] (eg. 'mp41')
378 // compatible_brands list spans to the end of box
380 tmp
= stream_read_dword(demuxer
->stream
);
382 case MOV_FOURCC('i','s','o','m'):
383 mp_msg(MSGT_DEMUX
,MSGL_V
,"ISO: File Type Major Brand: ISO Base Media\n");
385 case MOV_FOURCC('m','p','4','1'):
386 mp_msg(MSGT_DEMUX
,MSGL_INFO
,"ISO: File Type Major Brand: ISO/IEC 14496-1 (MPEG-4 system) v1\n");
388 case MOV_FOURCC('m','p','4','2'):
389 mp_msg(MSGT_DEMUX
,MSGL_INFO
,"ISO: File Type Major Brand: ISO/IEC 14496-1 (MPEG-4 system) v2\n");
391 case MOV_FOURCC('M','4','A',' '):
392 mp_msg(MSGT_DEMUX
,MSGL_INFO
,"ISO: File Type Major Brand: Apple iTunes AAC-LC Audio\n");
394 case MOV_FOURCC('M','4','P',' '):
395 mp_msg(MSGT_DEMUX
,MSGL_INFO
,"ISO: File Type Major Brand: Apple iTunes AAC-LC Protected Audio\n");
397 case MOV_FOURCC('q','t',' ',' '):
398 mp_msg(MSGT_DEMUX
,MSGL_INFO
,"ISO: File Type Major Brand: Original QuickTime\n");
400 case MOV_FOURCC('3','g','p','1'):
401 mp_msg(MSGT_DEMUX
,MSGL_INFO
,"ISO: File Type Major Brand: 3GPP Profile 1\n");
403 case MOV_FOURCC('3','g','p','2'):
404 case MOV_FOURCC('3','g','2','a'):
405 mp_msg(MSGT_DEMUX
,MSGL_INFO
,"ISO: File Type Major Brand: 3GPP Profile 2\n");
407 case MOV_FOURCC('3','g','p','3'):
408 mp_msg(MSGT_DEMUX
,MSGL_INFO
,"ISO: File Type Major Brand: 3GPP Profile 3\n");
410 case MOV_FOURCC('3','g','p','4'):
411 mp_msg(MSGT_DEMUX
,MSGL_INFO
,"ISO: File Type Major Brand: 3GPP Profile 4\n");
413 case MOV_FOURCC('3','g','p','5'):
414 mp_msg(MSGT_DEMUX
,MSGL_INFO
,"ISO: File Type Major Brand: 3GPP Profile 5\n");
416 case MOV_FOURCC('m','m','p','4'):
417 mp_msg(MSGT_DEMUX
,MSGL_INFO
,"ISO: File Type Major Brand: Mobile ISO/IEC 14496-1 (MPEG-4 system)\n");
421 mp_msg(MSGT_DEMUX
,MSGL_WARN
,"ISO: Unknown File Type Major Brand: %.4s\n",(char *)&tmp
);
423 mp_msg(MSGT_DEMUX
,MSGL_V
,"ISO: File Type Minor Version: %d\n",
424 stream_read_dword(demuxer
->stream
));
426 // List all compatible brands
427 for(i
= 0; i
< ((len
-16)/4); i
++) {
428 tmp
= be2me_32(stream_read_dword(demuxer
->stream
));
429 mp_msg(MSGT_DEMUX
,MSGL_V
,"ISO: File Type Compatible Brand #%d: %.4s\n",i
,(char *)&tmp
);
434 case MOV_FOURCC('m','o','o','v'):
435 // case MOV_FOURCC('c','m','o','v'):
436 mp_msg(MSGT_DEMUX
,MSGL_V
,"MOV: Movie header found!\n");
437 priv
->moov_start
=(off_t
)stream_tell(demuxer
->stream
);
438 priv
->moov_end
=(off_t
)priv
->moov_start
+len
-skipped
;
439 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"MOV: Movie header: start: %"PRIx64
" end: %"PRIx64
"\n",
440 (int64_t)priv
->moov_start
, (int64_t)priv
->moov_end
);
442 i
= stream_read_dword(demuxer
->stream
)-8;
443 if(stream_read_dword(demuxer
->stream
)==MOV_FOURCC('r','m','r','a')){
445 mp_msg(MSGT_DEMUX
,MSGL_INFO
,"MOV: Reference Media file!!!\n");
446 //set demuxer type to playlist ...
447 demuxer
->type
=DEMUXER_TYPE_PLAYLIST
;
449 int len
=stream_read_dword(demuxer
->stream
)-8;
450 int fcc
=stream_read_dword(demuxer
->stream
);
451 if(len
<0) break; // EOF!?
453 // printf("i=%d len=%d\n",i,len);
455 case MOV_FOURCC('r','m','d','a'):
457 case MOV_FOURCC('r','d','r','f'): {
458 av_unused
int tmp
=stream_read_dword(demuxer
->stream
);
459 av_unused
int type
=stream_read_dword_le(demuxer
->stream
);
460 int slen
=stream_read_dword(demuxer
->stream
);
461 //char* s=malloc(slen+1);
462 //stream_read(demuxer->stream,s,slen);
464 //FIXME: also store type & data_rate ?
465 ds_read_packet(demuxer
->video
,
469 stream_tell(demuxer
->stream
),
473 mp_msg(MSGT_DEMUX
,MSGL_V
,"Added reference to playlist\n");
475 //mp_msg(MSGT_DEMUX,MSGL_INFO,"REF: [%.4s] %s\n",&type,s);
476 len
-=12+slen
;i
-=12+slen
; break;
478 case MOV_FOURCC('r','m','d','r'): {
479 av_unused
int flags
=stream_read_dword(demuxer
->stream
);
480 int rate
=stream_read_dword(demuxer
->stream
);
481 mp_msg(MSGT_DEMUX
,MSGL_V
," min. data rate: %d bits/sec\n",rate
);
484 case MOV_FOURCC('r','m','q','u'): {
485 int q
=stream_read_dword(demuxer
->stream
);
486 mp_msg(MSGT_DEMUX
,MSGL_V
," quality index: %d\n",q
);
490 i
-=len
;stream_skip(demuxer
->stream
,len
);
495 case MOV_FOURCC('w','i','d','e'):
496 mp_msg(MSGT_DEMUX
,MSGL_V
,"MOV: 'WIDE' chunk found!\n");
498 case MOV_FOURCC('m','d','a','t'):
499 mp_msg(MSGT_DEMUX
,MSGL_V
,"MOV: Movie DATA found!\n");
500 priv
->mdat_start
=stream_tell(demuxer
->stream
);
501 priv
->mdat_end
=priv
->mdat_start
+len
-skipped
;
502 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"MOV: Movie data: start: %"PRIx64
" end: %"PRIx64
"\n",
503 (int64_t)priv
->mdat_start
, (int64_t)priv
->mdat_end
);
506 // if we're over the headers, then we can stop parsing here!
508 return DEMUXER_TYPE_MOV
;
511 case MOV_FOURCC('f','r','e','e'):
512 case MOV_FOURCC('s','k','i','p'):
513 case MOV_FOURCC('j','u','n','k'):
514 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"MOV: free space (len: %"PRId64
")\n", (int64_t)len
);
515 /* unused, if you edit a mov, you can use space provided by free atoms (redefining it) */
517 case MOV_FOURCC('p','n','o','t'):
518 case MOV_FOURCC('P','I','C','T'):
519 /* dunno what, but we shoudl ignore it */
522 if(no
==0){ free(priv
); return 0;} // first chunk is bad!
524 mp_msg(MSGT_DEMUX
,MSGL_V
,"MOV: unknown chunk: %.4s %d\n",(char *)&id
,(int)len
);
527 if(!stream_skip(demuxer
->stream
,len
-skipped
)) break;
533 return DEMUXER_TYPE_MOV
;
537 if ((flags
==5) || (flags
==7)) // reference & header sent
538 return DEMUXER_TYPE_PLAYLIST
;
541 mp_msg(MSGT_DEMUX
,MSGL_WARN
,"MOV: missing data (mdat) chunk! Maybe broken file...\n");
543 mp_msg(MSGT_DEMUX
,MSGL_WARN
,"MOV: missing header (moov/cmov) chunk! Maybe broken file...\n");
548 static void demux_close_mov(demuxer_t
*demuxer
) {
549 mov_priv_t
* priv
= demuxer
->priv
;
553 for (i
= 0; i
< MOV_MAX_TRACKS
; i
++) {
554 mov_track_t
*track
= priv
->tracks
[i
];
558 free(track
->stream_header
);
559 free(track
->samples
);
561 free(track
->chunkmap
);
563 free(track
->keyframes
);
564 free(track
->editlist
);
572 unsigned int store_ughvlc(unsigned char *s
, unsigned int v
){
586 static void init_vobsub(sh_sub_t
*sh
, mov_track_t
*trak
) {
588 if (trak
->stdata_len
< 106)
590 sh
->extradata_len
= 16*4;
591 sh
->extradata
= malloc(sh
->extradata_len
);
592 memcpy(sh
->extradata
, trak
->stdata
+ 42, sh
->extradata_len
);
595 static int lschunks_intrak(demuxer_t
* demuxer
, int level
, unsigned int id
,
596 off_t pos
, off_t len
, mov_track_t
* trak
);
598 static int gen_sh_audio(sh_audio_t
* sh
, mov_track_t
* trak
, int timescale
) {
601 int16_t version
; // 0 or 1 (version 1 is qt3.0+)
602 int16_t revision
; // 0
603 int32_t vendor_id
; // 0
604 int16_t channels
; // 1 or 2 (Mono/Stereo)
605 int16_t samplesize
; // 8 or 16 (8Bit/16Bit)
606 int16_t compression_id
; // if version 0 then 0
607 // if version 1 and vbr then -2 else 0
608 int16_t packet_size
; // 0
609 uint16_t sample_rate
; // samplerate (Hz)
610 // qt3.0+ (version == 1)
611 uint32_t samples_per_packet
; // 0 or num uncompressed samples in a packet
612 // if 0 below three values are also 0
613 uint32_t bytes_per_packet
; // 0 or num compressed bytes for one channel
614 uint32_t bytes_per_frame
; // 0 or num compressed bytes for all channels
615 // (channels * bytes_per_packet)
616 uint32_t bytes_per_sample
; // 0 or size of uncompressed sample
617 // if samples_per_packet and bytes_per_packet are constant (CBR)
618 // then bytes_per_frame and bytes_per_sample must be 0 (else is VBR)
620 // optional additional atom-based fields
621 // ([int32_t size,int32_t type,some data ],repeat)
626 sh
->format
=trak
->fourcc
;
628 // crude audio delay from editlist0 hack ::atm
629 if(trak
->editlist_size
>=1) {
630 if(trak
->editlist
[0].pos
== -1) {
631 sh
->stream_delay
= (float)trak
->editlist
[0].dur
/(float)timescale
;
632 mp_msg(MSGT_DEMUX
,MSGL_V
,"MOV: Initial Audio-Delay: %.3f sec\n", sh
->stream_delay
);
637 switch( sh
->format
) {
638 case 0x726D6173: /* samr */
640 trak
->samplebytes
=sh
->samplesize
=1;
641 trak
->nchannels
=sh
->channels
=1;
645 case 0x62776173: /* sawb */
647 trak
->samplebytes
=sh
->samplesize
=1;
648 trak
->nchannels
=sh
->channels
=1;
649 sh
->samplerate
=16000;
654 // assumptions for below table: short is 16bit, int is 32bit, intfp is 16bit
655 // XXX: 32bit fixed point numbers (intfp) are only 2 Byte!
656 // short values are usually one byte leftpadded by zero
657 // int values are usually two byte leftpadded by zero
663 // 18 short samplesize
664 // 20 short compression_id
665 // 22 short packet_size (==0)
666 // 24 intfp sample_rate
667 // (26 short) unknown (==0)
668 // ---- qt3.0+ (version>=1)
669 // 28 int samples_per_packet
670 // 32 int bytes_per_packet
671 // 36 int bytes_per_frame
672 // 40 int bytes_per_sample
673 // there may be additional atoms following at 28 (version 0)
674 // or 44 (version 1), eg. esds atom of .MP4 files
676 // 28 int atom size (bytes of int size, int type and data)
677 // 32 char[4] atom type (fourc charater code -> esds)
678 // 36 char[] atom data (len=size-8)
680 // TODO: fix parsing for files using version 2.
681 if (trak
->stdata_len
< 26) {
682 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "MOV: broken (too small) sound atom!\n");
685 version
=char2short(trak
->stdata
,8);
687 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "MOV: version %d sound atom may not parse correctly!\n", version
);
688 trak
->samplebytes
=sh
->samplesize
=char2short(trak
->stdata
,18)/8;
690 /* I can't find documentation, but so far this is the case. -Corey */
691 switch (char2short(trak
->stdata
,16)) {
693 trak
->nchannels
= 1; break;
695 trak
->nchannels
= 2; break;
697 trak
->nchannels
= 6; break;
699 mp_msg(MSGT_DEMUX
, MSGL_WARN
,
700 "MOV: unable to determine audio channels, assuming 2 (got %d)\n",
701 char2short(trak
->stdata
,16));
704 sh
->channels
= trak
->nchannels
;
706 /*printf("MOV: timescale: %d samplerate: %d durmap: %d (%d) -> %d (%d)\n",
707 trak->timescale, char2short(trak->stdata,24), trak->durmap[0].dur,
708 trak->durmap[0].num, trak->timescale/trak->durmap[0].dur,
709 char2short(trak->stdata,24)/trak->durmap[0].dur);*/
710 sh
->samplerate
=char2short(trak
->stdata
,24);
711 if((sh
->samplerate
< 7000) && trak
->durmap
&& trak
->durmap
[0].dur
> 1) {
712 switch(char2short(trak
->stdata
,24)/trak
->durmap
[0].dur
) {
713 // TODO: add more cases.
715 sh
->samplerate
= 32000; break;
717 sh
->samplerate
= 44100; break;
719 sh
->samplerate
= 48000; break;
721 mp_msg(MSGT_DEMUX
, MSGL_WARN
,
722 "MOV: unable to determine audio samplerate, "
723 "assuming 44.1kHz (got %d)\n",
724 char2short(trak
->stdata
,24)/trak
->durmap
[0].dur
);
725 sh
->samplerate
= 44100;
729 mp_msg(MSGT_DEMUX
, MSGL_V
, "Audio bits: %d chans: %d rate: %d\n",
730 sh
->samplesize
*8,sh
->channels
,sh
->samplerate
);
732 if(trak
->stdata_len
>= 44 && trak
->stdata
[9]>=1){
733 mp_msg(MSGT_DEMUX
,MSGL_V
,"Audio header: samp/pack=%d bytes/pack=%d bytes/frame=%d bytes/samp=%d \n",
734 char2int(trak
->stdata
,28),
735 char2int(trak
->stdata
,32),
736 char2int(trak
->stdata
,36),
737 char2int(trak
->stdata
,40));
738 if(trak
->stdata_len
>=44+8){
739 int len
=char2int(trak
->stdata
,44);
740 int fcc
=char2int(trak
->stdata
,48);
741 // we have extra audio headers!!!
742 mp_msg(MSGT_DEMUX
,MSGL_V
,"Audio extra header: len=%d fcc=0x%X\n",len
,fcc
);
744 (char2int(trak
->stdata
,52) >= 12) &&
745 (char2int(trak
->stdata
,52+4) == MOV_FOURCC('f','r','m','a'))) {
746 switch(char2int(trak
->stdata
,52+8)) {
747 case MOV_FOURCC('a','l','a','c'):
748 if (len
>= 36 + char2int(trak
->stdata
,52)) {
749 sh
->codecdata_len
= char2int(trak
->stdata
,52+char2int(trak
->stdata
,52));
750 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: Found alac atom (%d)!\n", sh
->codecdata_len
);
751 sh
->codecdata
= malloc(sh
->codecdata_len
);
752 memcpy(sh
->codecdata
, &trak
->stdata
[52+char2int(trak
->stdata
,52)], sh
->codecdata_len
);
755 case MOV_FOURCC('i','n','2','4'):
756 case MOV_FOURCC('i','n','3','2'):
757 case MOV_FOURCC('f','l','3','2'):
758 case MOV_FOURCC('f','l','6','4'):
760 (char2int(trak
->stdata
,52+16)==MOV_FOURCC('e','n','d','a')) &&
761 (char2short(trak
->stdata
,52+20))) {
762 sh
->format
=char2int(trak
->stdata
,52+8);
763 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: Found little endian PCM data, reversed fourcc:%04x\n", sh
->format
);
767 if (len
> 8 && len
+ 44 <= trak
->stdata_len
) {
768 sh
->codecdata_len
= len
-8;
769 sh
->codecdata
= malloc(sh
->codecdata_len
);
770 memcpy(sh
->codecdata
, trak
->stdata
+44+8, sh
->codecdata_len
);
774 if (len
> 8 && len
+ 44 <= trak
->stdata_len
) {
775 sh
->codecdata_len
= len
-8;
776 sh
->codecdata
= malloc(sh
->codecdata_len
);
777 memcpy(sh
->codecdata
, trak
->stdata
+44+8, sh
->codecdata_len
);
791 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "MOV: unknown sound atom version (%d); may not work!\n", version
);
794 if (trak
->stdata_len
>= 36 + adjust
) {
795 int atom_len
= char2int(trak
->stdata
,28+adjust
);
796 if (atom_len
< 0 || atom_len
> trak
->stdata_len
- 28 - adjust
) atom_len
= trak
->stdata_len
- 28 - adjust
;
797 switch(char2int(trak
->stdata
,32+adjust
)) { // atom type
798 case MOV_FOURCC('e','s','d','s'): {
799 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: Found MPEG4 audio Elementary Stream Descriptor atom (%d)!\n", atom_len
);
802 if(!mp4_parse_esds(&trak
->stdata
[36+adjust
], atom_len
-8, &esds
)) {
803 /* 0xdd is a "user private" id, not an official allocated id (see http://www.mp4ra.org/object.html),
804 so perform some extra checks to be sure that this is really vorbis audio */
805 if(esds
.objectTypeId
==0xdd && esds
.streamType
==0x15 && sh
->format
==0x6134706D && esds
.decoderConfigLen
> 8)
808 unsigned char *buf
[3];
809 unsigned short sizes
[3];
811 unsigned char *ptr
= esds
.decoderConfig
;
813 if(ptr
[0] != 0 || ptr
[1] != 30) goto quit_vorbis_block
; //wrong extradata layout
816 for(k
= 0; k
< 3; k
++)
818 sizes
[k
] = (ptr
[offset
]<<8) | ptr
[offset
+1];
821 if(offset
+ sizes
[k
] > esds
.decoderConfigLen
)
823 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
);
824 goto quit_vorbis_block
;
826 buf
[k
] = malloc(sizes
[k
]);
827 if(!buf
[k
]) goto quit_vorbis_block
;
828 memcpy(buf
[k
], &ptr
[offset
], sizes
[k
]);
832 sh
->codecdata_len
= len
+ len
/255 + 64;
833 sh
->codecdata
= malloc(sh
->codecdata_len
);
838 offset
+= store_ughvlc(&ptr
[offset
], sizes
[0]);
839 offset
+= store_ughvlc(&ptr
[offset
], sizes
[1]);
840 for(k
= 0; k
< 3; k
++)
842 memcpy(&ptr
[offset
], buf
[k
], sizes
[k
]);
846 sh
->codecdata_len
= offset
;
847 sh
->codecdata
= realloc(sh
->codecdata
, offset
);
848 mp_msg(MSGT_DEMUX
,MSGL_V
, "demux_mov, vorbis extradata size: %d\n", offset
);
851 sh
->format
= mmioFOURCC('v', 'r', 'b', 's');
853 sh
->i_bps
= esds
.avgBitrate
/8;
855 // printf("######## audio format = %d ########\n",esds.objectTypeId);
856 if(esds
.objectTypeId
==MP4OTI_MPEG1Audio
|| esds
.objectTypeId
==MP4OTI_MPEG2AudioPart3
)
857 sh
->format
=0x55; // .mp3
859 if(esds
.objectTypeId
==MP4OTI_13kVoice
) { // 13K Voice, defined by 3GPP2
860 sh
->format
=mmioFOURCC('Q', 'c', 'l', 'p');
861 trak
->nchannels
=sh
->channels
=1;
862 trak
->samplebytes
=sh
->samplesize
=1;
865 // dump away the codec specific configuration for the AAC decoder
866 if(esds
.decoderConfigLen
){
867 if( (esds
.decoderConfig
[0]>>3) == 29 )
868 sh
->format
= 0x1d61346d; // request multi-channel mp3 decoder
871 sh
->codecdata_len
= esds
.decoderConfigLen
;
872 sh
->codecdata
= malloc(sh
->codecdata_len
);
873 memcpy(sh
->codecdata
, esds
.decoderConfig
, sh
->codecdata_len
);
877 mp4_free_esds(&esds
); // freeup esds mem
879 { FILE* f
=fopen("esds.dat","wb");
880 fwrite(&trak
->stdata
[36],atom_len
-8,1,f
);
885 case MOV_FOURCC('a','l','a','c'): {
886 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: Found alac atom (%d)!\n", atom_len
);
888 // copy all the atom (not only payload) for lavc alac decoder
889 sh
->codecdata_len
= atom_len
;
890 sh
->codecdata
= malloc(sh
->codecdata_len
);
891 memcpy(sh
->codecdata
, &trak
->stdata
[28], sh
->codecdata_len
);
894 case MOV_FOURCC('d','a','m','r'):
895 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
);
897 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
]);
898 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: Modes set: %02x%02x\n",trak
->stdata
[41+adjust
],trak
->stdata
[42+adjust
]);
899 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: Mode change period: %d Frames per sample: %d\n",trak
->stdata
[43+adjust
],trak
->stdata
[44+adjust
]);
903 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: Found unknown audio atom %c%c%c%c (%d)!\n",
904 trak
->stdata
[32+adjust
],trak
->stdata
[33+adjust
],trak
->stdata
[34+adjust
],trak
->stdata
[35+adjust
],
908 mp_msg(MSGT_DEMUX
, MSGL_V
, "Fourcc: %.4s\n",(char *)&trak
->fourcc
);
910 { FILE* f
=fopen("stdata.dat","wb");
911 fwrite(trak
->stdata
,trak
->stdata_len
,1,f
);
913 { FILE* f
=fopen("tkdata.dat","wb");
914 fwrite(trak
->tkdata
,trak
->tkdata_len
,1,f
);
917 // Emulate WAVEFORMATEX struct:
918 sh
->wf
=calloc(1, sizeof(*sh
->wf
) + (is_vorbis
? sh
->codecdata_len
: 0));
919 sh
->wf
->nChannels
=sh
->channels
;
920 sh
->wf
->wBitsPerSample
=(trak
->stdata
[18]<<8)+trak
->stdata
[19];
921 // sh->wf->nSamplesPerSec=trak->timescale;
922 sh
->wf
->nSamplesPerSec
=sh
->samplerate
;
923 if(trak
->stdata_len
>= 44 && trak
->stdata
[9]>=1 && char2int(trak
->stdata
,28)>0){
924 //Audio header: samp/pack=4096 bytes/pack=743 bytes/frame=1486 bytes/samp=2
925 sh
->wf
->nAvgBytesPerSec
=(sh
->wf
->nChannels
*sh
->wf
->nSamplesPerSec
*
926 char2int(trak
->stdata
,32)+char2int(trak
->stdata
,28)/2)
927 /char2int(trak
->stdata
,28);
928 sh
->wf
->nBlockAlign
=char2int(trak
->stdata
,36);
930 sh
->wf
->nAvgBytesPerSec
=sh
->wf
->nChannels
*sh
->wf
->wBitsPerSample
*sh
->wf
->nSamplesPerSec
/8;
931 // workaround for ms11 ima4
932 if (sh
->format
== 0x1100736d && trak
->stdata_len
>= 36)
933 sh
->wf
->nBlockAlign
=char2int(trak
->stdata
,36);
936 if(is_vorbis
&& sh
->codecdata_len
)
938 memcpy(sh
->wf
+1, sh
->codecdata
, sh
->codecdata_len
);
939 sh
->wf
->cbSize
= sh
->codecdata_len
;
942 // if(demuxer->audio->id==-1 || demuxer->audio->id==priv->track_db){
943 // // (auto)selected audio track:
944 // demuxer->audio->id=priv->track_db;
945 // demuxer->audio->sh=sh; sh->ds=demuxer->audio;
950 static int gen_sh_video(sh_video_t
* sh
, mov_track_t
* trak
, int timescale
) {
952 int flag
, start
, count_flag
, end
, palette_count
, gray
;
953 int hdr_ptr
= 76; // the byte just after depth
954 unsigned char *palette_map
;
956 sh
->format
=trak
->fourcc
;
958 // crude video delay from editlist0 hack ::atm
959 if(trak
->editlist_size
>=1) {
960 if(trak
->editlist
[0].pos
== -1) {
961 sh
->stream_delay
= (float)trak
->editlist
[0].dur
/(float)timescale
;
962 mp_msg(MSGT_DEMUX
,MSGL_V
,"MOV: Initial Video-Delay: %.3f sec\n", sh
->stream_delay
);
967 if (trak
->stdata_len
< 78) {
968 mp_msg(MSGT_DEMUXER
, MSGL_WARN
,
969 "MOV: Invalid (%d bytes instead of >= 78) video trak desc\n",
974 depth
= trak
->stdata
[75] | (trak
->stdata
[74] << 8);
975 if (trak
->fourcc
== mmioFOURCC('r', 'a', 'w', ' '))
976 sh
->format
= IMGFMT_RGB
| depth
;
982 // 16 int temporal_quality
983 // 20 int spatial_quality
989 // 40 short frames_per_sample
990 // 42 char[4] compressor_name
992 // 76 short color_table_id
993 // additional atoms may follow,
994 // eg esds atom from .MP4 files
996 // 82 char[4] atom type
999 { ImageDescription
* id
=malloc(8+trak
->stdata_len
); // safe
1001 id
->idSize
=8+trak
->stdata_len
;
1002 // id->cType=bswap_32(trak->fourcc);
1003 id
->cType
=le2me_32(trak
->fourcc
);
1004 id
->version
=char2short(trak
->stdata
,8);
1005 id
->revisionLevel
=char2short(trak
->stdata
,10);
1006 id
->vendor
=char2int(trak
->stdata
,12);
1007 id
->temporalQuality
=char2int(trak
->stdata
,16);
1008 id
->spatialQuality
=char2int(trak
->stdata
,20);
1009 id
->width
=char2short(trak
->stdata
,24);
1010 id
->height
=char2short(trak
->stdata
,26);
1011 id
->hRes
=char2int(trak
->stdata
,28);
1012 id
->vRes
=char2int(trak
->stdata
,32);
1013 id
->dataSize
=char2int(trak
->stdata
,36);
1014 id
->frameCount
=char2short(trak
->stdata
,40);
1015 memcpy(&id
->name
,trak
->stdata
+42,32);
1016 id
->depth
=char2short(trak
->stdata
,74);
1017 id
->clutID
=char2short(trak
->stdata
,76);
1018 if(trak
->stdata_len
>78) memcpy(((char*)&id
->clutID
)+2,trak
->stdata
+78,trak
->stdata_len
-78);
1021 { FILE *f
=fopen("ImageDescription","wb");
1022 fwrite(id
,id
->idSize
,1,f
);
1028 if(trak
->stdata_len
>= 86) { // extra atoms found
1031 while(pos
+8<=trak
->stdata_len
&&
1032 (pos
+(atom_len
=char2int(trak
->stdata
,pos
)))<=trak
->stdata_len
){
1033 switch(char2int(trak
->stdata
,pos
+4)) { // switch atom type
1034 case MOV_FOURCC('g','a','m','a'):
1035 // intfp with gamma value at which movie was captured
1036 // can be used to gamma correct movie display
1037 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: Found unsupported Gamma-Correction movie atom (%d)!\n",
1040 case MOV_FOURCC('f','i','e','l'):
1041 // 2 char-values (8bit int) that specify field handling
1042 // see the Apple's QuickTime Fileformat PDF for more info
1043 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: Found unsupported Field-Handling movie atom (%d)!\n",
1046 case MOV_FOURCC('m','j','q','t'):
1047 // Motion-JPEG default quantization table
1048 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: Found unsupported MJPEG-Quantization movie atom (%d)!\n",
1051 case MOV_FOURCC('m','j','h','t'):
1052 // Motion-JPEG default huffman table
1053 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: Found unsupported MJPEG-Huffman movie atom (%d)!\n",
1056 case MOV_FOURCC('e','s','d','s'):
1057 // MPEG4 Elementary Stream Descriptor header
1058 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: Found MPEG4 movie Elementary Stream Descriptor atom (%d)!\n", atom_len
);
1059 // add code here to save esds header of length atom_len-8
1060 // beginning at stdata[86] to some variable to pass it
1061 // on to the decoder ::atmos
1064 if(!mp4_parse_esds(trak
->stdata
+pos
+8, atom_len
-8, &esds
)) {
1066 if(esds
.objectTypeId
==MP4OTI_MPEG2VisualSimple
|| esds
.objectTypeId
==MP4OTI_MPEG2VisualMain
||
1067 esds
.objectTypeId
==MP4OTI_MPEG2VisualSNR
|| esds
.objectTypeId
==MP4OTI_MPEG2VisualSpatial
||
1068 esds
.objectTypeId
==MP4OTI_MPEG2VisualHigh
|| esds
.objectTypeId
==MP4OTI_MPEG2Visual422
)
1069 sh
->format
=mmioFOURCC('m', 'p', 'g', '2');
1070 else if(esds
.objectTypeId
==MP4OTI_MPEG1Visual
)
1071 sh
->format
=mmioFOURCC('m', 'p', 'g', '1');
1073 // dump away the codec specific configuration for the AAC decoder
1074 trak
->stream_header_len
= esds
.decoderConfigLen
;
1075 trak
->stream_header
= malloc(trak
->stream_header_len
);
1076 memcpy(trak
->stream_header
, esds
.decoderConfig
, trak
->stream_header_len
);
1078 mp4_free_esds(&esds
); // freeup esds mem
1081 case MOV_FOURCC('a','v','c','C'):
1082 // AVC decoder configuration record
1083 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: AVC decoder configuration record atom (%d)!\n", atom_len
);
1086 // Parse some parts of avcC, just for fun :)
1087 // real parsing is done by avc1 decoder
1088 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: avcC version: %d\n", *(trak
->stdata
+pos
+8));
1089 if (*(trak
->stdata
+pos
+8) != 1)
1090 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "MOV: unknown avcC version (%d). Expexct problems.\n", *(trak
->stdata
+pos
+9));
1091 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: avcC profile: %d\n", *(trak
->stdata
+pos
+9));
1092 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: avcC profile compatibility: %d\n", *(trak
->stdata
+pos
+10));
1093 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: avcC level: %d\n", *(trak
->stdata
+pos
+11));
1094 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: avcC nal length size: %d\n", ((*(trak
->stdata
+pos
+12))&0x03)+1);
1095 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: avcC number of sequence param sets: %d\n", cnt
= (*(trak
->stdata
+pos
+13) & 0x1f));
1097 for (i
= 0; i
< cnt
; i
++) {
1098 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: avcC sps %d have length %d\n", i
, AV_RB16(trak
->stdata
+poffs
));
1099 poffs
+= AV_RB16(trak
->stdata
+poffs
) + 2;
1101 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: avcC number of picture param sets: %d\n", *(trak
->stdata
+poffs
));
1103 for (i
= 0; i
< cnt
; i
++) {
1104 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: avcC pps %d have length %d\n", i
, AV_RB16(trak
->stdata
+poffs
));
1105 poffs
+= AV_RB16(trak
->stdata
+poffs
) + 2;
1107 // Copy avcC for the AVC decoder
1108 // This data will be put in extradata below, where BITMAPINFOHEADER is created
1109 trak
->stream_header_len
= atom_len
-8;
1110 trak
->stream_header
= malloc(trak
->stream_header_len
);
1111 memcpy(trak
->stream_header
, trak
->stdata
+pos
+8, trak
->stream_header_len
);
1114 case MOV_FOURCC('d','2','6','3'):
1115 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
);
1117 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]);
1122 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: Found unknown movie atom %c%c%c%c (%d)!\n",
1123 trak
->stdata
[pos
+4],trak
->stdata
[pos
+5],trak
->stdata
[pos
+6],trak
->stdata
[pos
+7],
1126 if(atom_len
<8) break;
1128 // printf("pos=%d max=%d\n",pos,trak->stdata_len);
1131 sh
->fps
=trak
->timescale
/
1132 ((trak
->durmap_size
>=1)?(float)trak
->durmap
[0].dur
:1);
1133 sh
->frametime
=1.0f
/sh
->fps
;
1135 sh
->disp_w
=trak
->stdata
[25]|(trak
->stdata
[24]<<8);
1136 sh
->disp_h
=trak
->stdata
[27]|(trak
->stdata
[26]<<8);
1137 if(trak
->tkdata_len
>81) {
1138 // if image size is zero, fallback to display size
1139 if(!sh
->disp_w
&& !sh
->disp_h
) {
1140 sh
->disp_w
=trak
->tkdata
[77]|(trak
->tkdata
[76]<<8);
1141 sh
->disp_h
=trak
->tkdata
[81]|(trak
->tkdata
[80]<<8);
1142 } else if(sh
->disp_w
!=(trak
->tkdata
[77]|(trak
->tkdata
[76]<<8))){
1143 // codec and display width differ... use display one for aspect
1144 sh
->aspect
=trak
->tkdata
[77]|(trak
->tkdata
[76]<<8);
1145 sh
->aspect
/=trak
->tkdata
[81]|(trak
->tkdata
[80]<<8);
1149 if(depth
>32+8) mp_msg(MSGT_DEMUX
, MSGL_INFO
,"*** depth = 0x%X\n",depth
);
1153 if (depth
> 32) { depth
&=31; gray
= 1; } // depth > 32 means grayscale
1154 if ((depth
== 2) || (depth
== 4) || (depth
== 8))
1155 palette_count
= (1 << depth
);
1159 // emulate BITMAPINFOHEADER:
1162 sh
->bih
=calloc(1, sizeof(*sh
->bih
) + palette_count
* 4);
1163 sh
->bih
->biSize
=40 + palette_count
* 4;
1164 // fetch the relevant fields
1165 flag
= AV_RB16(&trak
->stdata
[hdr_ptr
]);
1167 start
= AV_RB32(&trak
->stdata
[hdr_ptr
]);
1169 count_flag
= AV_RB16(&trak
->stdata
[hdr_ptr
]);
1171 end
= AV_RB16(&trak
->stdata
[hdr_ptr
]);
1173 palette_map
= (unsigned char *)sh
->bih
+ 40;
1174 mp_msg(MSGT_DEMUX
, MSGL_V
, "Allocated %d entries for palette\n",
1176 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "QT palette: start: %x, end: %x, count flag: %d, flags: %x\n",
1177 start
, end
, count_flag
, flag
);
1179 /* XXX: problems with sample (statunit6.mov) with flag&0x4 set! - alex*/
1181 // load default palette
1186 mp_msg(MSGT_DEMUX
, MSGL_V
, "Using default QT grayscale palette\n");
1187 if (palette_count
== 16)
1188 memcpy(palette_map
, qt_default_grayscale_palette_16
, 16 * 4);
1189 else if (palette_count
== 256) {
1190 memcpy(palette_map
, qt_default_grayscale_palette_256
, 256 * 4);
1191 if (trak
->fourcc
== mmioFOURCC('c','v','i','d')) {
1193 // Hack for grayscale CVID, negative palette
1194 // If you have samples where this is not required contact me (rxt)
1195 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: greyscale cvid with default palette,"
1196 " enabling negative palette hack.\n");
1197 for (i
= 0; i
< 256 * 4; i
++)
1198 palette_map
[i
] = palette_map
[i
] ^ 0xff;
1204 mp_msg(MSGT_DEMUX
, MSGL_V
, "Using default QT colour palette\n");
1205 if (palette_count
== 4)
1206 memcpy(palette_map
, qt_default_palette_4
, 4 * 4);
1207 else if (palette_count
== 16)
1208 memcpy(palette_map
, qt_default_palette_16
, 16 * 4);
1209 else if (palette_count
== 256)
1210 memcpy(palette_map
, qt_default_palette_256
, 256 * 4);
1213 // load palette from file
1216 mp_msg(MSGT_DEMUX
, MSGL_V
, "Loading palette from file\n");
1217 for (i
= start
; i
<= end
; i
++)
1219 entry
= AV_RB16(&trak
->stdata
[hdr_ptr
]);
1221 // apparently, if count_flag is set, entry is same as i
1222 if (count_flag
& 0x8000)
1224 // only care about top 8 bits of 16-bit R, G, or B value
1225 if (entry
<= palette_count
&& entry
>= 0)
1227 palette_map
[entry
* 4 + 2] = trak
->stdata
[hdr_ptr
+ 0];
1228 palette_map
[entry
* 4 + 1] = trak
->stdata
[hdr_ptr
+ 2];
1229 palette_map
[entry
* 4 + 0] = trak
->stdata
[hdr_ptr
+ 4];
1230 mp_dbg(MSGT_DEMUX
, MSGL_DBG2
, "QT palette: added entry: %d of %d (colors: R:%x G:%x B:%x)\n",
1231 entry
, palette_count
,
1232 palette_map
[entry
* 4 + 2],
1233 palette_map
[entry
* 4 + 1],
1234 palette_map
[entry
* 4 + 0]);
1237 mp_msg(MSGT_DEMUX
, MSGL_V
, "QT palette: skipped entry (out of count): %d of %d\n",
1238 entry
, palette_count
);
1245 if (trak
->fourcc
== mmioFOURCC('a','v','c','1')) {
1246 if (trak
->stream_header_len
> 0xffffffff - sizeof(*sh
->bih
)) {
1247 mp_msg(MSGT_DEMUXER
, MSGL_ERR
, "Invalid extradata size %d, skipping\n",trak
->stream_header_len
);
1248 trak
->stream_header_len
= 0;
1250 sh
->bih
=calloc(1, sizeof(*sh
->bih
) + trak
->stream_header_len
);
1251 sh
->bih
->biSize
=40 + trak
->stream_header_len
;
1252 memcpy(((unsigned char *)sh
->bih
)+40, trak
->stream_header
, trak
->stream_header_len
);
1253 free (trak
->stream_header
);
1254 trak
->stream_header_len
= 0;
1255 trak
->stream_header
= NULL
;
1257 sh
->bih
=calloc(1, sizeof(*sh
->bih
));
1261 sh
->bih
->biWidth
=sh
->disp_w
;
1262 sh
->bih
->biHeight
=sh
->disp_h
;
1263 sh
->bih
->biPlanes
=0;
1264 sh
->bih
->biBitCount
=depth
;
1265 sh
->bih
->biCompression
=trak
->fourcc
;
1266 sh
->bih
->biSizeImage
=sh
->bih
->biWidth
*sh
->bih
->biHeight
;
1268 mp_msg(MSGT_DEMUX
, MSGL_V
, "Image size: %d x %d (%d bpp)\n",sh
->disp_w
,sh
->disp_h
,sh
->bih
->biBitCount
);
1269 if(trak
->tkdata_len
>81)
1270 mp_msg(MSGT_DEMUX
, MSGL_V
, "Display size: %d x %d\n",
1271 trak
->tkdata
[77]|(trak
->tkdata
[76]<<8),
1272 trak
->tkdata
[81]|(trak
->tkdata
[80]<<8));
1273 mp_msg(MSGT_DEMUX
, MSGL_V
, "Fourcc: %.4s Codec: '%.*s'\n",(char *)&trak
->fourcc
,trak
->stdata
[42]&31,trak
->stdata
+43);
1275 // if(demuxer->video->id==-1 || demuxer->video->id==priv->track_db){
1276 // // (auto)selected video track:
1277 // demuxer->video->id=priv->track_db;
1278 // demuxer->video->sh=sh; sh->ds=demuxer->video;
1283 static void lschunks(demuxer_t
* demuxer
,int level
,off_t endpos
,mov_track_t
* trak
){
1284 mov_priv_t
* priv
=demuxer
->priv
;
1285 // printf("lschunks (level=%d,endpos=%x)\n", level, endpos);
1291 pos
=stream_tell(demuxer
->stream
);
1292 // printf("stream_tell==%d\n",pos);
1293 if(pos
>=endpos
) return; // END
1294 len
=stream_read_dword(demuxer
->stream
);
1295 // printf("len==%d\n",len);
1296 if(len
<8) return; // error
1298 id
=stream_read_dword(demuxer
->stream
);
1300 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"lschunks %.4s %d\n",(char *)&id
,(int)len
);
1303 if (lschunks_intrak(demuxer
, level
, id
, pos
, len
, trak
) < 0)
1305 } else { /* not in track */
1307 case MOV_FOURCC('m','v','h','d'): {
1308 int version
= stream_read_char(demuxer
->stream
);
1309 stream_skip(demuxer
->stream
, (version
== 1) ? 19 : 11);
1310 priv
->timescale
=stream_read_dword(demuxer
->stream
);
1312 priv
->duration
=stream_read_qword(demuxer
->stream
);
1314 priv
->duration
=stream_read_dword(demuxer
->stream
);
1315 mp_msg(MSGT_DEMUX
, MSGL_V
,"MOV: %*sMovie header (%d bytes): tscale=%d dur=%d\n",level
,"",(int)len
,
1316 (int)priv
->timescale
,(int)priv
->duration
);
1319 case MOV_FOURCC('t','r','a','k'): {
1320 // if(trak) printf("MOV: Warning! trak in trak?\n");
1321 if(priv
->track_db
>=MOV_MAX_TRACKS
){
1322 mp_tmsg(MSGT_DEMUX
,MSGL_WARN
,"MOV: WARNING: too many tracks");
1325 if(!priv
->track_db
) mp_msg(MSGT_DEMUX
, MSGL_V
, "--------------\n");
1326 trak
=calloc(1, sizeof(mov_track_t
));
1327 mp_msg(MSGT_DEMUX
,MSGL_V
,"MOV: Track #%d:\n",priv
->track_db
);
1328 trak
->id
=priv
->track_db
;
1329 priv
->tracks
[priv
->track_db
]=trak
;
1330 lschunks(demuxer
,level
+1,pos
+len
,trak
);
1331 mov_build_index(trak
,priv
->timescale
);
1333 case MOV_TRAK_AUDIO
: {
1334 sh_audio_t
* sh
=new_sh_audio(demuxer
,priv
->track_db
);
1335 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "[%s] Audio stream found, -aid %d\n", "mov", priv
->track_db
);
1336 gen_sh_audio(sh
, trak
, priv
->timescale
);
1339 case MOV_TRAK_VIDEO
: {
1340 sh_video_t
* sh
=new_sh_video(demuxer
,priv
->track_db
);
1341 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "[%s] Video stream found, -vid %d\n", "mov", priv
->track_db
);
1342 gen_sh_video(sh
, trak
, priv
->timescale
);
1345 case MOV_TRAK_GENERIC
:
1346 if (trak
->fourcc
== mmioFOURCC('m','p','4','s') ||
1347 trak
->fourcc
== mmioFOURCC('t','x','3','g') ||
1348 trak
->fourcc
== mmioFOURCC('t','e','x','t')) {
1349 sh_sub_t
*sh
= new_sh_sub(demuxer
, priv
->track_db
);
1350 mp_tmsg(MSGT_DEMUX
, MSGL_INFO
, "[%s] Subtitle stream found, -sid %d\n", "mov", priv
->track_db
);
1351 if (trak
->fourcc
== mmioFOURCC('m','p','4','s'))
1352 init_vobsub(sh
, trak
);
1358 mp_msg(MSGT_DEMUX
, MSGL_V
, "Generic track - not completely understood! (id: %d)\n",
1360 /* XXX: Also this contains the FLASH data */
1364 int pos
= stream_tell(demuxer
->stream
);
1369 for (i
=0; i
<trak
->samples_size
; i
++)
1371 char buf
[trak
->samples
[i
].size
];
1372 stream_seek(demuxer
->stream
, trak
->samples
[i
].pos
);
1373 snprintf((char *)&name
[0], 20, "samp%d", i
);
1374 fd
= open((char *)&name
[0], O_CREAT
|O_WRONLY
);
1375 stream_read(demuxer
->stream
, &buf
[0], trak
->samples
[i
].size
);
1376 write(fd
, &buf
[0], trak
->samples
[i
].size
);
1379 for (i
=0; i
<trak
->chunks_size
; i
++)
1381 char buf
[trak
->length
];
1382 stream_seek(demuxer
->stream
, trak
->chunks
[i
].pos
);
1383 snprintf((char *)&name
[0], 20, "chunk%d", i
);
1384 fd
= open((char *)&name
[0], O_CREAT
|O_WRONLY
);
1385 stream_read(demuxer
->stream
, &buf
[0], trak
->length
);
1386 write(fd
, &buf
[0], trak
->length
);
1389 if (trak
->samplesize
> 0)
1393 buf
= malloc(trak
->samplesize
);
1394 stream_seek(demuxer
->stream
, trak
->chunks
[0].pos
);
1395 snprintf((char *)&name
[0], 20, "trak%d", trak
->id
);
1396 fd
= open((char *)&name
[0], O_CREAT
|O_WRONLY
);
1397 stream_read(demuxer
->stream
, buf
, trak
->samplesize
);
1398 write(fd
, buf
, trak
->samplesize
);
1401 stream_seek(demuxer
->stream
, pos
);
1406 mp_msg(MSGT_DEMUX
, MSGL_V
, "Unknown track type found (type: %d)\n", trak
->type
);
1409 mp_msg(MSGT_DEMUX
, MSGL_V
, "--------------\n");
1415 case MOV_FOURCC('c','m','o','v'): {
1416 mp_tmsg(MSGT_DEMUX
,MSGL_ERR
,"MOV: Compressed headers support requires ZLIB!\n");
1420 case MOV_FOURCC('m','o','o','v'):
1421 case MOV_FOURCC('c','m','o','v'): {
1422 // mp_tmsg(MSGT_DEMUX,MSGL_ERR,"MOV: Compressed headers support requires ZLIB!\n");
1423 lschunks(demuxer
,level
+1,pos
+len
,NULL
);
1426 case MOV_FOURCC('d','c','o','m'): {
1427 // int temp=stream_read_dword(demuxer->stream);
1428 unsigned int algo
=be2me_32(stream_read_dword(demuxer
->stream
));
1429 mp_msg(MSGT_DEMUX
, MSGL_V
, "Compressed header uses %.4s algo!\n",(char *)&algo
);
1432 case MOV_FOURCC('c','m','v','d'): {
1433 // int temp=stream_read_dword(demuxer->stream);
1434 unsigned int moov_sz
=stream_read_dword(demuxer
->stream
);
1435 unsigned int cmov_sz
=len
-4;
1436 unsigned char* cmov_buf
;
1437 unsigned char* moov_buf
;
1442 if (moov_sz
> UINT_MAX
- 16) {
1443 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "Invalid cmvd atom size %d\n", moov_sz
);
1446 cmov_buf
=malloc(cmov_sz
);
1447 moov_buf
=malloc(moov_sz
+16);
1448 mp_msg(MSGT_DEMUX
, MSGL_V
, "Compressed header size: %d / %d\n",cmov_sz
,moov_sz
);
1450 stream_read(demuxer
->stream
,cmov_buf
,cmov_sz
);
1452 zstrm
.zalloc
= (alloc_func
)0;
1453 zstrm
.zfree
= (free_func
)0;
1454 zstrm
.opaque
= (voidpf
)0;
1455 zstrm
.next_in
= cmov_buf
;
1456 zstrm
.avail_in
= cmov_sz
;
1457 zstrm
.next_out
= moov_buf
;
1458 zstrm
.avail_out
= moov_sz
;
1460 zret
= inflateInit(&zstrm
);
1462 { mp_msg(MSGT_DEMUX
, MSGL_ERR
, "QT cmov: inflateInit err %d\n",zret
);
1465 zret
= inflate(&zstrm
, Z_NO_FLUSH
);
1466 if ((zret
!= Z_OK
) && (zret
!= Z_STREAM_END
))
1467 { mp_msg(MSGT_DEMUX
, MSGL_ERR
, "QT cmov inflate: ERR %d\n",zret
);
1473 DecOut
= fopen("Out.bin", "w");
1474 fwrite(moov_buf
, 1, moov_sz
, DecOut
);
1478 if(moov_sz
!= zstrm
.total_out
)
1479 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "Warning! moov size differs cmov: %d zlib: %ld\n",moov_sz
,zstrm
.total_out
);
1480 zret
= inflateEnd(&zstrm
);
1482 backup
=demuxer
->stream
;
1483 demuxer
->stream
=new_memory_stream(moov_buf
,moov_sz
);
1484 stream_skip(demuxer
->stream
,8);
1485 lschunks(demuxer
,level
+1,moov_sz
,NULL
); // parse uncompr. 'moov'
1486 //free_stream(demuxer->stream);
1487 demuxer
->stream
=backup
;
1493 case MOV_FOURCC('u','d','t','a'):
1495 unsigned int udta_id
;
1497 off_t udta_size
= len
;
1499 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "mov: user data record found\n");
1500 mp_msg(MSGT_DEMUX
, MSGL_V
, "Quicktime Clip Info:\n");
1502 while((len
> 8) && (udta_size
> 8))
1504 udta_len
= stream_read_dword(demuxer
->stream
);
1505 udta_id
= stream_read_dword(demuxer
->stream
);
1507 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "udta_id: %.4s (len: %"PRId64
")\n", (char *)&udta_id
, (int64_t)udta_len
);
1510 case MOV_FOURCC(0xa9,'c','p','y'):
1511 case MOV_FOURCC(0xa9,'d','a','y'):
1512 case MOV_FOURCC(0xa9,'d','i','r'):
1513 /* 0xa9,'e','d','1' - '9' : edit timestamps */
1514 case MOV_FOURCC(0xa9,'f','m','t'):
1515 case MOV_FOURCC(0xa9,'i','n','f'):
1516 case MOV_FOURCC(0xa9,'p','r','d'):
1517 case MOV_FOURCC(0xa9,'p','r','f'):
1518 case MOV_FOURCC(0xa9,'r','e','q'):
1519 case MOV_FOURCC(0xa9,'s','r','c'):
1520 case MOV_FOURCC('n','a','m','e'):
1521 case MOV_FOURCC(0xa9,'n','a','m'):
1522 case MOV_FOURCC(0xa9,'A','R','T'):
1523 case MOV_FOURCC(0xa9,'c','m','t'):
1524 case MOV_FOURCC(0xa9,'a','u','t'):
1525 case MOV_FOURCC(0xa9,'s','w','r'):
1527 off_t text_len
= stream_read_word(demuxer
->stream
);
1528 char text
[text_len
+2+1];
1529 stream_read(demuxer
->stream
, (char *)&text
, text_len
+2);
1530 text
[text_len
+2] = 0x0;
1533 case MOV_FOURCC(0xa9,'a','u','t'):
1534 demux_info_add(demuxer
, "author", &text
[2]);
1535 mp_msg(MSGT_DEMUX
, MSGL_V
, " Author: %s\n", &text
[2]);
1537 case MOV_FOURCC(0xa9,'c','p','y'):
1538 demux_info_add(demuxer
, "copyright", &text
[2]);
1539 mp_msg(MSGT_DEMUX
, MSGL_V
, " Copyright: %s\n", &text
[2]);
1541 case MOV_FOURCC(0xa9,'i','n','f'):
1542 mp_msg(MSGT_DEMUX
, MSGL_V
, " Info: %s\n", &text
[2]);
1544 case MOV_FOURCC('n','a','m','e'):
1545 case MOV_FOURCC(0xa9,'n','a','m'):
1546 demux_info_add(demuxer
, "title", &text
[2]);
1547 mp_msg(MSGT_DEMUX
, MSGL_V
, " Name: %s\n", &text
[2]);
1549 case MOV_FOURCC(0xa9,'A','R','T'):
1550 mp_msg(MSGT_DEMUX
, MSGL_V
, " Artist: %s\n", &text
[2]);
1552 case MOV_FOURCC(0xa9,'d','i','r'):
1553 mp_msg(MSGT_DEMUX
, MSGL_V
, " Director: %s\n", &text
[2]);
1555 case MOV_FOURCC(0xa9,'c','m','t'):
1556 demux_info_add(demuxer
, "comments", &text
[2]);
1557 mp_msg(MSGT_DEMUX
, MSGL_V
, " Comment: %s\n", &text
[2]);
1559 case MOV_FOURCC(0xa9,'r','e','q'):
1560 mp_msg(MSGT_DEMUX
, MSGL_V
, " Requirements: %s\n", &text
[2]);
1562 case MOV_FOURCC(0xa9,'s','w','r'):
1563 demux_info_add(demuxer
, "encoder", &text
[2]);
1564 mp_msg(MSGT_DEMUX
, MSGL_V
, " Software: %s\n", &text
[2]);
1566 case MOV_FOURCC(0xa9,'d','a','y'):
1567 mp_msg(MSGT_DEMUX
, MSGL_V
, " Creation timestamp: %s\n", &text
[2]);
1569 case MOV_FOURCC(0xa9,'f','m','t'):
1570 mp_msg(MSGT_DEMUX
, MSGL_V
, " Format: %s\n", &text
[2]);
1572 case MOV_FOURCC(0xa9,'p','r','d'):
1573 mp_msg(MSGT_DEMUX
, MSGL_V
, " Producer: %s\n", &text
[2]);
1575 case MOV_FOURCC(0xa9,'p','r','f'):
1576 mp_msg(MSGT_DEMUX
, MSGL_V
, " Performer(s): %s\n", &text
[2]);
1578 case MOV_FOURCC(0xa9,'s','r','c'):
1579 mp_msg(MSGT_DEMUX
, MSGL_V
, " Source providers: %s\n", &text
[2]);
1582 udta_size
-= 4+text_len
;
1585 /* some other shits: WLOC - window location,
1586 LOOP - looping style,
1587 SelO - play only selected frames
1588 AllF - play all frames
1590 case MOV_FOURCC('W','L','O','C'):
1591 case MOV_FOURCC('L','O','O','P'):
1592 case MOV_FOURCC('S','e','l','O'):
1593 case MOV_FOURCC('A','l','l','F'):
1596 if( udta_len
>udta_size
)
1599 stream_skip(demuxer
->stream
, udta_len
-4-4);
1600 udta_size
-= udta_len
;
1609 mp_msg(MSGT_DEMUX
,MSGL_V
,"MOV: unknown chunk: %.4s %d\n",(char *)&id
,(int)len
);
1610 } /* endof switch */
1614 if(pos
>=endpos
) break;
1615 if(!stream_seek(demuxer
->stream
,pos
)) break;
1619 static int lschunks_intrak(demuxer_t
* demuxer
, int level
, unsigned int id
,
1620 off_t pos
, off_t len
, mov_track_t
* trak
)
1623 case MOV_FOURCC('m','d','a','t'): {
1624 mp_msg(MSGT_DEMUX
,MSGL_WARN
,"Hmm, strange MOV, parsing mdat in lschunks?\n");
1627 case MOV_FOURCC('f','r','e','e'):
1628 case MOV_FOURCC('u','d','t','a'):
1629 /* here not supported :p */
1631 case MOV_FOURCC('t','k','h','d'): {
1632 mp_msg(MSGT_DEMUX
,MSGL_V
,"MOV: %*sTrack header!\n", level
, "");
1634 trak
->tkdata_len
= len
;
1635 trak
->tkdata
= malloc(trak
->tkdata_len
);
1636 stream_read(demuxer
->stream
, trak
->tkdata
, trak
->tkdata_len
);
1641 8 4 Modification time
1647 34 2 Alternate group
1650 40 36 Matrix structure
1654 mp_msg(MSGT_DEMUX
, MSGL_V
,
1655 "tkhd len=%d ver=%d flags=0x%X id=%d dur=%d lay=%d vol=%d\n",
1656 trak
->tkdata_len
, trak
->tkdata
[0], trak
->tkdata
[1],
1657 char2int(trak
->tkdata
, 12), // id
1658 char2int(trak
->tkdata
, 20), // duration
1659 char2short(trak
->tkdata
, 32), // layer
1660 char2short(trak
->tkdata
, 36)); // volume
1663 case MOV_FOURCC('m','d','h','d'): {
1664 int version
= stream_read_char(demuxer
->stream
);
1665 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: %*sMedia header!\n", level
, "");
1666 stream_skip(demuxer
->stream
, (version
== 1) ? 19 : 11);
1668 trak
->timescale
= stream_read_dword(demuxer
->stream
);
1671 trak
->length
= stream_read_qword(demuxer
->stream
);
1673 trak
->length
= stream_read_dword(demuxer
->stream
);
1676 case MOV_FOURCC('h','d','l','r'): {
1677 av_unused
unsigned int tmp
= stream_read_dword(demuxer
->stream
);
1678 unsigned int type
= stream_read_dword_le(demuxer
->stream
);
1679 unsigned int subtype
= stream_read_dword_le(demuxer
->stream
);
1680 unsigned int manufact
= stream_read_dword_le(demuxer
->stream
);
1681 av_unused
unsigned int comp_flags
= stream_read_dword(demuxer
->stream
);
1682 av_unused
unsigned int comp_mask
= stream_read_dword(demuxer
->stream
);
1683 int len
= stream_read_char(demuxer
->stream
);
1684 char* str
= malloc(len
+ 1);
1685 stream_read(demuxer
->stream
, str
, len
);
1687 mp_msg(MSGT_DEMUX
, MSGL_V
,
1688 "MOV: %*sHandler header: %.4s/%.4s (%.4s) %s\n", level
, "",
1689 (char *)&type
, (char *)&subtype
, (char *)&manufact
, str
);
1691 switch(bswap_32(type
)) {
1692 case MOV_FOURCC('m','h','l','r'):
1693 trak
->media_handler
= bswap_32(subtype
);
1695 case MOV_FOURCC('d','h','l','r'):
1696 trak
->data_handler
= bswap_32(subtype
);
1699 mp_msg(MSGT_DEMUX
, MSGL_V
,
1700 "MOV: unknown handler class: 0x%X (%.4s)\n",
1701 bswap_32(type
), (char *)&type
);
1705 case MOV_FOURCC('v','m','h','d'): {
1706 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: %*sVideo header!\n", level
, "");
1707 trak
->type
= MOV_TRAK_VIDEO
;
1711 case MOV_FOURCC('s','m','h','d'): {
1712 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: %*sSound header!\n", level
, "");
1713 trak
->type
= MOV_TRAK_AUDIO
;
1717 case MOV_FOURCC('g','m','h','d'): {
1718 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: %*sGeneric header!\n", level
, "");
1719 trak
->type
= MOV_TRAK_GENERIC
;
1722 case MOV_FOURCC('n','m','h','d'): {
1723 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: %*sGeneric header!\n", level
, "");
1724 trak
->type
= MOV_TRAK_GENERIC
;
1727 case MOV_FOURCC('s','t','s','d'): {
1728 int i
= stream_read_dword(demuxer
->stream
); // temp!
1729 int count
= stream_read_dword(demuxer
->stream
);
1730 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: %*sDescription list! (cnt:%d)\n",
1732 for (i
= 0; i
< count
; i
++) {
1733 off_t pos
= stream_tell(demuxer
->stream
);
1734 off_t len
= stream_read_dword(demuxer
->stream
);
1735 unsigned int fourcc
= stream_read_dword_le(demuxer
->stream
);
1736 /* some files created with Broadcast 2000 (e.g. ilacetest.mov)
1737 contain raw I420 video but have a yv12 fourcc */
1738 if (fourcc
== mmioFOURCC('y','v','1','2'))
1739 fourcc
= mmioFOURCC('I','4','2','0');
1742 mp_msg(MSGT_DEMUX
, MSGL_V
,
1743 "MOV: %*s desc #%d: %.4s (%"PRId64
" bytes)\n", level
, "",
1744 i
, (char *)&fourcc
, (int64_t)len
- 16);
1745 if (fourcc
!= trak
->fourcc
&& i
)
1746 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
, "MOV: WARNING: Variable FourCC detected!?\n");
1749 trak
->fourcc
= fourcc
;
1750 // read type specific (audio/video/time/text etc) header
1751 // NOTE: trak type is not yet known at this point :(((
1752 trak
->stdata_len
= len
- 8;
1753 trak
->stdata
= malloc(trak
->stdata_len
);
1754 stream_read(demuxer
->stream
, trak
->stdata
, trak
->stdata_len
);
1756 if (!stream_seek(demuxer
->stream
, pos
+ len
))
1761 case MOV_FOURCC('s','t','t','s'): {
1762 av_unused
int temp
= stream_read_dword(demuxer
->stream
);
1763 int len
= stream_read_dword(demuxer
->stream
);
1765 unsigned int pts
= 0;
1766 mp_msg(MSGT_DEMUX
, MSGL_V
,
1767 "MOV: %*sSample duration table! (%d blocks)\n", level
, "",
1769 trak
->durmap
= calloc(len
, sizeof(mov_durmap_t
));
1770 trak
->durmap_size
= trak
->durmap
? len
: 0;
1771 for (i
= 0; i
< trak
->durmap_size
; i
++) {
1772 trak
->durmap
[i
].num
= stream_read_dword(demuxer
->stream
);
1773 trak
->durmap
[i
].dur
= stream_read_dword(demuxer
->stream
);
1774 pts
+= trak
->durmap
[i
].num
* trak
->durmap
[i
].dur
;
1776 if (trak
->length
!= pts
)
1777 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "Warning! pts=%d length=%d\n",
1781 case MOV_FOURCC('s','t','s','c'): {
1782 int temp
= stream_read_dword(demuxer
->stream
);
1783 int len
= stream_read_dword(demuxer
->stream
);
1784 int ver
= (temp
<< 24);
1785 int flags
= (temp
<< 16) | (temp
<< 8) | temp
;
1787 mp_msg(MSGT_DEMUX
, MSGL_V
,
1788 "MOV: %*sSample->Chunk mapping table! (%d blocks) (ver:%d,flags:%d)\n", level
, "",
1791 trak
->chunkmap
= calloc(len
, sizeof(mov_chunkmap_t
));
1792 trak
->chunkmap_size
= trak
->chunkmap
? len
: 0;
1793 for (i
= 0; i
< trak
->chunkmap_size
; i
++) {
1794 trak
->chunkmap
[i
].first
= stream_read_dword(demuxer
->stream
) - 1;
1795 trak
->chunkmap
[i
].spc
= stream_read_dword(demuxer
->stream
);
1796 trak
->chunkmap
[i
].sdid
= stream_read_dword(demuxer
->stream
);
1800 case MOV_FOURCC('s','t','s','z'): {
1801 int temp
= stream_read_dword(demuxer
->stream
);
1802 int ss
=stream_read_dword(demuxer
->stream
);
1803 int ver
= (temp
<< 24);
1804 int flags
= (temp
<< 16) | (temp
<< 8) | temp
;
1805 int entries
= stream_read_dword(demuxer
->stream
);
1807 mp_msg(MSGT_DEMUX
, MSGL_V
,
1808 "MOV: %*sSample size table! (entries=%d ss=%d) (ver:%d,flags:%d)\n", level
, "",
1809 entries
, ss
, ver
, flags
);
1810 trak
->samplesize
= ss
;
1812 // variable samplesize
1813 trak
->samples
= realloc_struct(trak
->samples
, entries
, sizeof(mov_sample_t
));
1814 trak
->samples_size
= entries
;
1815 for (i
= 0; i
< trak
->samples_size
; i
++)
1816 trak
->samples
[i
].size
= stream_read_dword(demuxer
->stream
);
1820 case MOV_FOURCC('s','t','c','o'): {
1821 av_unused
int temp
= stream_read_dword(demuxer
->stream
);
1822 int len
= stream_read_dword(demuxer
->stream
);
1824 mp_msg(MSGT_DEMUX
, MSGL_V
,
1825 "MOV: %*sChunk offset table! (%d chunks)\n", level
, "",
1827 // extend array if needed:
1828 if (len
> trak
->chunks_size
) {
1829 trak
->chunks
= realloc_struct(trak
->chunks
, len
, sizeof(mov_chunk_t
));
1830 trak
->chunks_size
= trak
->chunks
? len
: 0;
1833 for(i
= 0; i
< trak
->chunks_size
; i
++)
1834 trak
->chunks
[i
].pos
= stream_read_dword(demuxer
->stream
);
1837 case MOV_FOURCC('c','o','6','4'): {
1838 av_unused
int temp
= stream_read_dword(demuxer
->stream
);
1839 int len
= stream_read_dword(demuxer
->stream
);
1841 mp_msg(MSGT_DEMUX
, MSGL_V
,
1842 "MOV: %*s64bit chunk offset table! (%d chunks)\n", level
, "",
1844 // extend array if needed:
1845 if (len
> trak
->chunks_size
) {
1846 trak
->chunks
= realloc_struct(trak
->chunks
, len
, sizeof(mov_chunk_t
));
1847 trak
->chunks_size
= trak
->chunks
? len
: 0;
1850 for (i
= 0; i
< trak
->chunks_size
; i
++) {
1851 #ifndef _LARGEFILE_SOURCE
1852 if (stream_read_dword(demuxer
->stream
) != 0)
1853 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "Chunk %d has got 64bit address, but you've MPlayer compiled without LARGEFILE support!\n", i
);
1854 trak
->chunks
[i
].pos
= stream_read_dword(demuxer
->stream
);
1856 trak
->chunks
[i
].pos
= stream_read_qword(demuxer
->stream
);
1861 case MOV_FOURCC('s','t','s','s'): {
1862 int temp
= stream_read_dword(demuxer
->stream
);
1863 int entries
= stream_read_dword(demuxer
->stream
);
1864 int ver
= (temp
<< 24);
1865 int flags
= (temp
<< 16) | (temp
<<8) | temp
;
1867 mp_msg(MSGT_DEMUX
, MSGL_V
,
1868 "MOV: %*sSyncing samples (keyframes) table! (%d entries) (ver:%d,flags:%d)\n", level
, "",
1869 entries
, ver
, flags
);
1870 trak
->keyframes
= calloc(entries
, sizeof(unsigned int));
1871 trak
->keyframes_size
= trak
->keyframes
? entries
: 0;
1872 for (i
= 0; i
< trak
->keyframes_size
; i
++)
1873 trak
->keyframes
[i
] = stream_read_dword(demuxer
->stream
) - 1;
1876 case MOV_FOURCC('m','d','i','a'): {
1877 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: %*sMedia stream!\n", level
, "");
1878 lschunks(demuxer
, level
+ 1, pos
+ len
, trak
);
1881 case MOV_FOURCC('m','i','n','f'): {
1882 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: %*sMedia info!\n", level
, "");
1883 lschunks(demuxer
, level
+ 1 ,pos
+ len
, trak
);
1886 case MOV_FOURCC('s','t','b','l'): {
1887 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: %*sSample info!\n", level
, "");
1888 lschunks(demuxer
, level
+ 1, pos
+ len
, trak
);
1891 case MOV_FOURCC('e','d','t','s'): {
1892 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: %*sEdit atom!\n", level
, "");
1893 lschunks(demuxer
, level
+ 1, pos
+ len
, trak
);
1896 case MOV_FOURCC('e','l','s','t'): {
1897 int temp
= stream_read_dword(demuxer
->stream
);
1898 int entries
= stream_read_dword(demuxer
->stream
);
1899 int ver
= (temp
<< 24);
1900 int flags
= (temp
<< 16) | (temp
<< 8) | temp
;
1902 mp_msg(MSGT_DEMUX
, MSGL_V
,
1903 "MOV: %*sEdit list table (%d entries) (ver:%d,flags:%d)\n", level
, "",
1904 entries
, ver
, flags
);
1906 trak
->editlist
= calloc(entries
, sizeof(mov_editlist_t
));
1907 trak
->editlist_size
= trak
->editlist
? entries
: 0;
1908 for (i
= 0; i
< trak
->editlist_size
; i
++) {
1909 int dur
= stream_read_dword(demuxer
->stream
);
1910 int mt
= stream_read_dword(demuxer
->stream
);
1911 int mr
= stream_read_dword(demuxer
->stream
); // 16.16fp
1912 trak
->editlist
[i
].dur
= dur
;
1913 trak
->editlist
[i
].pos
= mt
;
1914 trak
->editlist
[i
].speed
= mr
;
1915 mp_msg(MSGT_DEMUX
, MSGL_V
,
1916 "MOV: %*s entry#%d: duration: %d start time: %d speed: %3.1fx\n", level
, "",
1917 i
, dur
, mt
, (float)mr
/65536.0f
);
1922 case MOV_FOURCC('c','o','d','e'): {
1923 /* XXX: Implement atom 'code' for FLASH support */
1928 mp_msg(MSGT_DEMUX
,MSGL_V
,"MOV: unknown chunk: %.4s %d\n",(char *)&id
,(int)len
);
1934 static demuxer_t
* mov_read_header(demuxer_t
* demuxer
){
1935 struct MPOpts
*opts
= demuxer
->opts
;
1936 mov_priv_t
* priv
=demuxer
->priv
;
1938 int best_a_id
=-1, best_a_len
=0;
1939 int best_v_id
=-1, best_v_len
=0;
1941 mp_msg(MSGT_DEMUX
, MSGL_DBG3
, "mov_read_header!\n");
1944 stream_reset(demuxer
->stream
);
1945 if(!stream_seek(demuxer
->stream
,priv
->moov_start
))
1947 mp_msg(MSGT_DEMUX
,MSGL_ERR
,"MOV: Cannot seek to the beginning of the Movie header (0x%"PRIx64
")\n",
1948 (int64_t)priv
->moov_start
);
1951 lschunks(demuxer
, 0, priv
->moov_end
, NULL
);
1952 // just in case we have hit eof while parsing...
1953 demuxer
->stream
->eof
= 0;
1954 // mp_msg(MSGT_DEMUX, MSGL_INFO, "--------------\n");
1956 // find the best (longest) streams:
1957 for(t_no
=0;t_no
<priv
->track_db
;t_no
++){
1958 mov_track_t
* trak
=priv
->tracks
[t_no
];
1959 int len
=(trak
->samplesize
) ? trak
->chunks_size
: trak
->samples_size
;
1960 if(demuxer
->a_streams
[t_no
]){ // need audio
1961 if(len
>best_a_len
){ best_a_len
=len
; best_a_id
=t_no
; }
1963 if(demuxer
->v_streams
[t_no
]){ // need video
1964 if(len
>best_v_len
){ best_v_len
=len
; best_v_id
=t_no
; }
1967 mp_msg(MSGT_DEMUX
, MSGL_V
, "MOV: longest streams: A: #%d (%d samples) V: #%d (%d samples)\n",
1968 best_a_id
,best_a_len
,best_v_id
,best_v_len
);
1969 if(demuxer
->audio
->id
==-1 && best_a_id
>=0) demuxer
->audio
->id
=best_a_id
;
1970 if(demuxer
->video
->id
==-1 && best_v_id
>=0) demuxer
->video
->id
=best_v_id
;
1972 // setup sh pointers:
1973 if(demuxer
->audio
->id
>=0){
1974 sh_audio_t
* sh
=demuxer
->a_streams
[demuxer
->audio
->id
];
1976 demuxer
->audio
->sh
=sh
; sh
->ds
=demuxer
->audio
;
1978 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "MOV: selected audio stream (%d) does not exist\n",demuxer
->audio
->id
);
1979 demuxer
->audio
->id
=-2;
1982 if(demuxer
->video
->id
>=0){
1983 sh_video_t
* sh
=demuxer
->v_streams
[demuxer
->video
->id
];
1985 demuxer
->video
->sh
=sh
; sh
->ds
=demuxer
->video
;
1987 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "MOV: selected video stream (%d) does not exist\n",demuxer
->video
->id
);
1988 demuxer
->video
->id
=-2;
1991 if(demuxer
->sub
->id
>=0){
1992 sh_sub_t
* sh
=demuxer
->s_streams
[demuxer
->sub
->id
];
1994 demuxer
->sub
->sh
=sh
;
1996 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "MOV: selected subtitle stream (%d) does not exist\n",demuxer
->sub
->id
);
1997 demuxer
->sub
->id
=-2;
2001 if(demuxer
->video
->id
<0 && demuxer
->audio
->id
<0) {
2002 /* No AV streams found. Try to find an MPEG stream. */
2003 for(t_no
=0;t_no
<priv
->track_db
;t_no
++){
2004 mov_track_t
* trak
=priv
->tracks
[t_no
];
2005 if(trak
->media_handler
== MOV_FOURCC('M','P','E','G')) {
2009 demuxer
->video
->id
= t_no
;
2010 s
= new_ds_stream(demuxer
->video
);
2011 od
= demux_open(opts
, s
, DEMUXER_TYPE_MPEG_PS
, -1, -1, -1, NULL
);
2012 if(od
) return new_demuxers_demuxer(od
, od
, od
);
2013 demuxer
->video
->id
= -2; //new linked demuxer couldn't be allocated
2020 if( mp_msg_test(MSGT_DEMUX
,MSGL_DBG3
) ){
2021 for(t_no
=0;t_no
<priv
->track_db
;t_no
++){
2022 mov_track_t
* trak
=priv
->tracks
[t_no
];
2023 if(trak
->type
==MOV_TRAK_GENERIC
){
2027 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "MOV: Track #%d: Extracting %d data chunks to files\n",t_no
,trak
->samples_size
);
2028 for (i
=0; i
<trak
->samples_size
; i
++)
2030 int len
=trak
->samples
[i
].size
;
2032 stream_seek(demuxer
->stream
, trak
->samples
[i
].pos
);
2033 snprintf(name
, 20, "t%02d-s%03d.%s", t_no
,i
,
2034 (trak
->media_handler
==MOV_FOURCC('f','l','s','h')) ?
2036 fd
= open(name
, O_CREAT
|O_WRONLY
);
2038 // for(j=0;j<trak->stdata_len-3; j++)
2039 // printf("stdata[%d]=0x%X ize=0x%X\n",j,char2int(trak->stdata,j),MOV_FOURCC('z','l','i','b'));
2041 if( //trak->media_handler==MOV_FOURCC('s','p','r','t') &&
2042 trak
->stdata_len
>=16 &&
2043 char2int(trak
->stdata
,12)==MOV_FOURCC('z','l','i','b')
2045 int newlen
=stream_read_dword(demuxer
->stream
);
2053 stream_read(demuxer
->stream
, buf
, len
);
2055 zstrm
.zalloc
= (alloc_func
)0;
2056 zstrm
.zfree
= (free_func
)0;
2057 zstrm
.opaque
= (voidpf
)0;
2058 zstrm
.next_in
= buf
;
2059 zstrm
.avail_in
= len
;
2060 zstrm
.next_out
= buf2
;
2061 zstrm
.avail_out
= newlen
;
2063 zret
= inflateInit(&zstrm
);
2064 zret
= inflate(&zstrm
, Z_NO_FLUSH
);
2065 if(newlen
!= zstrm
.total_out
)
2066 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "Warning! unzipped frame size differs hdr: %d zlib: %ld\n",newlen
,zstrm
.total_out
);
2068 write(fd
, buf2
, newlen
);
2072 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "******* ZLIB COMPRESSED SAMPLE!!!!! (%d->%d bytes) *******\n",len
,newlen
);
2076 stream_read(demuxer
->stream
, buf
, len
);
2077 write(fd
, buf
, len
);
2084 demuxer
->stream
->eof
= 0;
2091 * \brief return the mov track that belongs to a demuxer stream
2092 * \param ds the demuxer stream, may be NULL
2093 * \return the mov track info structure belonging to the stream,
2096 static mov_track_t
*stream_track(mov_priv_t
*priv
, demux_stream_t
*ds
) {
2097 if (ds
&& (ds
->id
>= 0) && (ds
->id
< priv
->track_db
))
2098 return priv
->tracks
[ds
->id
];
2103 // 0 = EOF or no stream found
2104 // 1 = successfully read a packet
2105 static int demux_mov_fill_buffer(demuxer_t
*demuxer
,demux_stream_t
* ds
){
2106 mov_priv_t
* priv
=demuxer
->priv
;
2107 mov_track_t
* trak
=NULL
;
2112 if (ds
->eof
) return 0;
2113 trak
= stream_track(priv
, ds
);
2114 if (!trak
) return 0;
2116 if(trak
->samplesize
){
2118 if(trak
->pos
>=trak
->chunks_size
) return 0; // EOF
2119 stream_seek(demuxer
->stream
,trak
->chunks
[trak
->pos
].pos
);
2120 pts
=(float)(trak
->chunks
[trak
->pos
].sample
*trak
->duration
)/(float)trak
->timescale
;
2121 if(trak
->samplesize
!=1)
2123 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "WARNING! Samplesize(%d) != 1\n",
2125 if((trak
->fourcc
!= MOV_FOURCC('t','w','o','s')) && (trak
->fourcc
!= MOV_FOURCC('s','o','w','t')))
2126 x
=trak
->chunks
[trak
->pos
].size
*trak
->samplesize
;
2128 x
=trak
->chunks
[trak
->pos
].size
;
2131 x
=trak
->chunks
[trak
->pos
].size
;
2132 // printf("X = %d\n", x);
2133 /* the following stuff is audio related */
2134 if (trak
->type
== MOV_TRAK_AUDIO
){
2135 if(trak
->stdata_len
>=44 && trak
->stdata
[9]>=1 && char2int(trak
->stdata
,28)>0){
2136 // stsd version 1 - we have audio compression ratio info:
2137 x
/=char2int(trak
->stdata
,28); // samples/packet
2138 // x*=char2int(trak->stdata,32); // bytes/packet
2139 x
*=char2int(trak
->stdata
,36); // bytes/frame
2141 if(ds
->ss_div
&& ds
->ss_mul
){
2142 // workaround for buggy files like 7up-high-traffic-areas.mov,
2143 // with missing stsd v1 header containing compression rate
2144 x
/=ds
->ss_div
; x
*=ds
->ss_mul
; // compression ratio fix ! HACK !
2147 x
*=trak
->samplebytes
;
2150 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Audio sample %d bytes pts %5.3f\n",trak
->chunks
[trak
->pos
].size
*trak
->samplesize
,pts
);
2151 } /* MOV_TRAK_AUDIO */
2152 pos
=trak
->chunks
[trak
->pos
].pos
;
2154 int frame
=trak
->pos
;
2155 // editlist support:
2156 if(trak
->type
== MOV_TRAK_VIDEO
&& trak
->editlist_size
>=1){
2157 // find the right editlist entry:
2158 if(frame
<trak
->editlist
[trak
->editlist_pos
].start_frame
)
2159 trak
->editlist_pos
=0;
2160 while(trak
->editlist_pos
<trak
->editlist_size
-1 &&
2161 frame
>=trak
->editlist
[trak
->editlist_pos
+1].start_frame
)
2162 ++trak
->editlist_pos
;
2163 if(frame
>=trak
->editlist
[trak
->editlist_pos
].start_frame
+
2164 trak
->editlist
[trak
->editlist_pos
].frames
) return 0; // EOF
2165 // calc real frame index:
2166 frame
-=trak
->editlist
[trak
->editlist_pos
].start_frame
;
2167 frame
+=trak
->editlist
[trak
->editlist_pos
].start_sample
;
2169 pts
=(float)(trak
->samples
[frame
].pts
+
2170 trak
->editlist
[trak
->editlist_pos
].pts_offset
)/(float)trak
->timescale
;
2172 if(frame
>=trak
->samples_size
) return 0; // EOF
2173 pts
=(float)trak
->samples
[frame
].pts
/(float)trak
->timescale
;
2176 stream_seek(demuxer
->stream
,trak
->samples
[frame
].pos
);
2177 x
=trak
->samples
[frame
].size
;
2178 pos
=trak
->samples
[frame
].pos
;
2180 if(trak
->pos
==0 && trak
->stream_header_len
>0){
2181 // we have to append the stream header...
2182 demux_packet_t
* dp
=new_demux_packet(x
+trak
->stream_header_len
);
2183 memcpy(dp
->buffer
,trak
->stream_header
,trak
->stream_header_len
);
2184 stream_read(demuxer
->stream
,dp
->buffer
+trak
->stream_header_len
,x
);
2185 free(trak
->stream_header
);
2186 trak
->stream_header
= NULL
;
2187 trak
->stream_header_len
= 0;
2190 dp
->pos
=pos
; // FIXME?
2191 ds_add_packet(ds
,dp
);
2193 ds_read_packet(ds
,demuxer
->stream
,x
,pts
,pos
,0);
2198 if (demuxer
->sub
->id
>= 0 && demuxer
->sub
->id
< priv
->track_db
)
2199 trak
= priv
->tracks
[demuxer
->sub
->id
];
2202 while (samplenr
< trak
->samples_size
) {
2203 double subpts
= (double)trak
->samples
[samplenr
].pts
/ (double)trak
->timescale
;
2204 if (subpts
>= pts
) break;
2210 else if (samplenr
!= priv
->current_sub
) {
2211 off_t pos
= trak
->samples
[samplenr
].pos
;
2212 int len
= trak
->samples
[samplenr
].size
;
2213 double subpts
= (double)trak
->samples
[samplenr
].pts
/ (double)trak
->timescale
;
2214 stream_seek(demuxer
->stream
, pos
);
2215 ds_read_packet(demuxer
->sub
, demuxer
->stream
, len
, subpts
, pos
, 0);
2216 priv
->current_sub
= samplenr
;
2224 static float mov_seek_track(mov_track_t
* trak
,float pts
,int flags
){
2226 // printf("MOV track seek called %5.3f \n",pts);
2227 if(flags
&SEEK_FACTOR
) pts
*=trak
->length
; else pts
*=(float)trak
->timescale
;
2229 if(trak
->samplesize
){
2230 int sample
=pts
/trak
->duration
;
2231 // printf("MOV track seek - chunk: %d (pts: %5.3f dur=%d) \n",sample,pts,trak->duration);
2232 if(!(flags
&SEEK_ABSOLUTE
)) sample
+=trak
->chunks
[trak
->pos
].sample
; // relative
2234 while(trak
->pos
<trak
->chunks_size
&& trak
->chunks
[trak
->pos
].sample
<sample
) ++trak
->pos
;
2235 if (trak
->pos
== trak
->chunks_size
) return -1;
2236 pts
=(float)(trak
->chunks
[trak
->pos
].sample
*trak
->duration
)/(float)trak
->timescale
;
2239 if(!(flags
&SEEK_ABSOLUTE
)) pts
+=trak
->samples
[trak
->pos
].pts
;
2242 //printf("MOV track seek - sample: %d \n",ipts);
2243 for(trak
->pos
=0;trak
->pos
<trak
->samples_size
;++trak
->pos
){
2244 if(trak
->samples
[trak
->pos
].pts
>=ipts
) break; // found it!
2246 if (trak
->pos
== trak
->samples_size
) return -1;
2247 if(trak
->keyframes_size
){
2248 // find nearest keyframe
2250 for(i
=0;i
<trak
->keyframes_size
;i
++){
2251 if(trak
->keyframes
[i
]>=trak
->pos
) break;
2253 if (i
== trak
->keyframes_size
) return -1;
2254 if(i
>0 && (trak
->keyframes
[i
]-trak
->pos
) > (trak
->pos
-trak
->keyframes
[i
-1]))
2256 trak
->pos
=trak
->keyframes
[i
];
2257 // printf("nearest keyframe: %d \n",trak->pos);
2259 pts
=(float)trak
->samples
[trak
->pos
].pts
/(float)trak
->timescale
;
2262 // printf("MOV track seek done: %5.3f \n",pts);
2267 static void demux_seek_mov(demuxer_t
*demuxer
,float pts
,float audio_delay
,int flags
){
2268 mov_priv_t
* priv
=demuxer
->priv
;
2272 // printf("MOV seek called %5.3f flag=%d \n",pts,flags);
2275 trak
= stream_track(priv
, ds
);
2277 //if(flags&2) pts*=(float)trak->length/(float)trak->timescale;
2278 //if(!(flags&1)) pts+=ds->pts;
2279 ds
->pts
=mov_seek_track(trak
,pts
,flags
);
2280 if (ds
->pts
< 0) ds
->eof
= 1;
2282 flags
=1; // absolute seconds
2286 trak
= stream_track(priv
, ds
);
2288 //if(flags&2) pts*=(float)trak->length/(float)trak->timescale;
2289 //if(!(flags&1)) pts+=ds->pts;
2290 ds
->pts
=mov_seek_track(trak
,pts
,flags
);
2291 if (ds
->pts
< 0) ds
->eof
= 1;
2296 static int demux_mov_control(demuxer_t
*demuxer
, int cmd
, void *arg
){
2299 // try the video track
2300 track
= stream_track(demuxer
->priv
, demuxer
->video
);
2301 if (!track
|| !track
->length
)
2302 // otherwise try to get the info from the audio track
2303 track
= stream_track(demuxer
->priv
, demuxer
->audio
);
2305 if (!track
|| !track
->length
)
2306 return DEMUXER_CTRL_DONTKNOW
;
2309 case DEMUXER_CTRL_GET_TIME_LENGTH
:
2310 if (!track
->timescale
)
2311 return DEMUXER_CTRL_DONTKNOW
;
2312 *((double *)arg
) = (double)track
->length
/ track
->timescale
;
2313 return DEMUXER_CTRL_OK
;
2315 case DEMUXER_CTRL_GET_PERCENT_POS
:
2317 off_t pos
= track
->pos
;
2318 if (track
->durmap_size
>= 1)
2319 pos
*= track
->durmap
[0].dur
;
2320 *((int *)arg
) = (int)(100 * pos
/ track
->length
);
2321 return DEMUXER_CTRL_OK
;
2324 return DEMUXER_CTRL_NOTIMPL
;
2328 const demuxer_desc_t demuxer_desc_mov
= {
2329 "Quicktime/MP4 demuxer",
2332 "Arpi, Al3x, Atmos, others",
2333 "Handles Quicktime, MP4, 3GP",
2335 0, // slow autodetect
2337 demux_mov_fill_buffer
,