options: more mplayer.c options moved to option struct
[mplayer/greg.git] / libmpdemux / demux_mov.c
blob432d0cad18991be2dc4c919cec6d8568e35392c0
1 /*
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
21 * TSGS#15(02)0088
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.
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <inttypes.h>
47 #include "config.h"
49 #ifdef CONFIG_QUICKTIME
50 #include <QuickTime/QuickTime.h>
51 #include <QuickTime/ImageCompression.h>
52 #include <QuickTime/ImageCodec.h>
53 #else
54 #include "loader/qtx/qtxsdk/components.h"
55 #endif
57 #include "mp_msg.h"
59 #include "stream/stream.h"
60 #include "demuxer.h"
61 #include "stheader.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
73 #if CONFIG_ZLIB
74 #include <zlib.h>
75 #endif
77 #ifndef _FCNTL_H
78 #include <fcntl.h>
79 #endif
81 #define char2short(x,y) AV_RB16(&(x)[(y)])
82 #define char2int(x,y) AV_RB32(&(x)[(y)])
84 typedef struct {
85 unsigned int pts; // duration
86 unsigned int size;
87 off_t pos;
88 } mov_sample_t;
90 typedef struct {
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
94 off_t pos;
95 } mov_chunk_t;
97 typedef struct {
98 unsigned int first;
99 unsigned int spc;
100 unsigned int sdid;
101 } mov_chunkmap_t;
103 typedef struct {
104 unsigned int num;
105 unsigned int dur;
106 } mov_durmap_t;
108 typedef struct {
109 unsigned int dur;
110 unsigned int pos;
111 int speed;
113 int frames;
114 int start_sample;
115 int start_frame;
116 int pts_offset;
117 } mov_editlist_t;
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
126 typedef struct {
127 int id;
128 int type;
129 off_t pos;
131 unsigned int media_handler;
132 unsigned int data_handler;
134 int timescale;
135 unsigned int length;
136 int samplesize; // 0 = variable
137 int duration; // 0 = variable
138 int width,height; // for video
139 unsigned int fourcc;
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
151 int samples_size;
152 mov_sample_t* samples;
153 int chunks_size;
154 mov_chunk_t* chunks;
155 int chunkmap_size;
156 mov_chunkmap_t* chunkmap;
157 int durmap_size;
158 mov_durmap_t* durmap;
159 int keyframes_size;
160 unsigned int* keyframes;
161 int editlist_size;
162 mov_editlist_t* editlist;
163 int editlist_pos;
165 void* desc; // image/sound/etc description (pointer to ImageDescription etc)
166 } mov_track_t;
168 static void mov_build_index(mov_track_t* trak,int timescale){
169 int i,j,s;
170 int last=trak->chunks_size;
171 unsigned int pts=0;
173 #if 0
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;
185 #endif
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);
190 // process chunkmap:
191 i=trak->chunkmap_size;
192 while(i>0){
193 --i;
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);
202 #if 0
203 for (i=0; i < trak->chunks_size; i++)
205 /* fixup position */
206 if (trak->chunks[i].pos == -1)
207 if (i > 0)
208 trak->chunks[i].pos = trak->chunks[i-1].pos + trak->chunks[i-1].size;
209 else
210 trak->chunks[i].pos = 0; /* FIXME: set initial pos */
211 #endif
213 // calc pts of chunks:
214 s=0;
215 for(j=0;j<trak->chunks_size;j++){
216 trak->chunks[j].sample=s;
217 s+=trak->chunks[j].size;
219 i = 0;
220 for (j = 0; j < trak->durmap_size; j++)
221 i += trak->durmap[j].num;
222 if (i != s) {
223 mp_msg(MSGT_DEMUX, MSGL_WARN,
224 "MOV: durmap and chunkmap sample count differ (%i vs %i)\n", i, s);
225 if (i > s) s = i;
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;
234 trak->samplesize=0;
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");
242 return;
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;
253 // calc pts:
254 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)
258 break;
259 trak->samples[s].pts=pts;
260 ++s;
261 pts+=trak->durmap[j].dur;
265 // calc sample offsets
266 s=0;
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)
271 break;
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;
278 ++s;
282 // precalc editlist entries
283 if(trak->editlist_size>0){
284 int frame=0;
285 int e_pts=0;
286 for(i=0;i<trak->editlist_size;i++){
287 mov_editlist_t* el=&trak->editlist[i];
288 int sample=0;
289 int pts=el->pos;
290 el->start_frame=frame;
291 if(pts<0){
292 // skip!
293 el->frames=0; continue;
295 // find start sample
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;
302 e_pts+=el->dur;
303 // find end sample
304 for(;sample<trak->samples_size;sample++){
305 if(pts<trak->samples[sample].pts) break;
307 el->frames=sample-el->start_sample;
308 frame+=el->frames;
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
320 typedef struct {
321 off_t moov_start;
322 off_t moov_end;
323 off_t mdat_start;
324 off_t mdat_end;
325 int track_db;
326 mov_track_t* tracks[MOV_MAX_TRACKS];
327 int timescale; // movie timescale
328 int duration; // movie duration (in movie timescale units)
329 subtitle subs;
330 char subtext[MOV_MAX_SUBLEN + 1];
331 int current_sub;
332 } mov_priv_t;
334 #define MOV_FOURCC(a,b,c,d) ((a<<24)|(b<<16)|(c<<8)|(d))
336 static int mov_check_file(demuxer_t* demuxer){
337 int flags=0;
338 int no=0;
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;
345 while(1){
346 int i;
347 int skipped=8;
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);
357 #else
358 len = stream_read_qword(demuxer->stream);
359 #endif
360 skipped += 8;
362 #if 0
363 else if (len == 0) /* deleted chunk */
365 /* XXX: CJB! is this right? - alex */
366 goto skip_chunk;
368 #endif
369 else if(len<8) break; // invalid chunk
371 switch(id){
372 case MOV_FOURCC('f','t','y','p'): {
373 unsigned int tmp;
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
379 #if 1
380 tmp = stream_read_dword(demuxer->stream);
381 switch(tmp) {
382 case MOV_FOURCC('i','s','o','m'):
383 mp_msg(MSGT_DEMUX,MSGL_V,"ISO: File Type Major Brand: ISO Base Media\n");
384 break;
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");
387 break;
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");
390 break;
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");
393 break;
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");
396 break;
397 case MOV_FOURCC('q','t',' ',' '):
398 mp_msg(MSGT_DEMUX,MSGL_INFO,"ISO: File Type Major Brand: Original QuickTime\n");
399 break;
400 case MOV_FOURCC('3','g','p','1'):
401 mp_msg(MSGT_DEMUX,MSGL_INFO,"ISO: File Type Major Brand: 3GPP Profile 1\n");
402 break;
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");
406 break;
407 case MOV_FOURCC('3','g','p','3'):
408 mp_msg(MSGT_DEMUX,MSGL_INFO,"ISO: File Type Major Brand: 3GPP Profile 3\n");
409 break;
410 case MOV_FOURCC('3','g','p','4'):
411 mp_msg(MSGT_DEMUX,MSGL_INFO,"ISO: File Type Major Brand: 3GPP Profile 4\n");
412 break;
413 case MOV_FOURCC('3','g','p','5'):
414 mp_msg(MSGT_DEMUX,MSGL_INFO,"ISO: File Type Major Brand: 3GPP Profile 5\n");
415 break;
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");
418 break;
419 default:
420 tmp = be2me_32(tmp);
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));
425 skipped += 8;
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);
430 skipped += 4;
432 #endif
433 } break;
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);
441 skipped+=8;
442 i = stream_read_dword(demuxer->stream)-8;
443 if(stream_read_dword(demuxer->stream)==MOV_FOURCC('r','m','r','a')){
444 skipped+=i;
445 mp_msg(MSGT_DEMUX,MSGL_INFO,"MOV: Reference Media file!!!\n");
446 //set demuxer type to playlist ...
447 demuxer->type=DEMUXER_TYPE_PLAYLIST;
448 while(i>0){
449 int len=stream_read_dword(demuxer->stream)-8;
450 int fcc=stream_read_dword(demuxer->stream);
451 if(len<0) break; // EOF!?
452 i-=8;
453 // printf("i=%d len=%d\n",i,len);
454 switch(fcc){
455 case MOV_FOURCC('r','m','d','a'):
456 continue;
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,
466 demuxer->stream,
467 slen,
469 stream_tell(demuxer->stream),
470 0 // no flags
472 flags|=4;
473 mp_msg(MSGT_DEMUX,MSGL_V,"Added reference to playlist\n");
474 //s[slen]=0;
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);
482 len-=8; i-=8; break;
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);
487 len-=4; i-=4; break;
490 i-=len;stream_skip(demuxer->stream,len);
493 flags|=1;
494 break;
495 case MOV_FOURCC('w','i','d','e'):
496 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: 'WIDE' chunk found!\n");
497 if(flags&2) break;
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);
504 flags|=2;
505 if(flags==3){
506 // if we're over the headers, then we can stop parsing here!
507 demuxer->priv=priv;
508 return DEMUXER_TYPE_MOV;
510 break;
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) */
516 break;
517 case MOV_FOURCC('p','n','o','t'):
518 case MOV_FOURCC('P','I','C','T'):
519 /* dunno what, but we shoudl ignore it */
520 break;
521 default:
522 if(no==0){ free(priv); return 0;} // first chunk is bad!
523 id = be2me_32(id);
524 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: unknown chunk: %.4s %d\n",(char *)&id,(int)len);
526 //skip_chunk:
527 if(!stream_skip(demuxer->stream,len-skipped)) break;
528 ++no;
531 if(flags==3){
532 demuxer->priv=priv;
533 return DEMUXER_TYPE_MOV;
535 free(priv);
537 if ((flags==5) || (flags==7)) // reference & header sent
538 return DEMUXER_TYPE_PLAYLIST;
540 if(flags==1)
541 mp_msg(MSGT_DEMUX,MSGL_WARN,"MOV: missing data (mdat) chunk! Maybe broken file...\n");
542 else if(flags==2)
543 mp_msg(MSGT_DEMUX,MSGL_WARN,"MOV: missing header (moov/cmov) chunk! Maybe broken file...\n");
545 return 0;
548 static void demux_close_mov(demuxer_t *demuxer) {
549 mov_priv_t* priv = demuxer->priv;
550 int i;
551 if (!priv)
552 return;
553 for (i = 0; i < MOV_MAX_TRACKS; i++) {
554 mov_track_t *track = priv->tracks[i];
555 if (track) {
556 free(track->tkdata);
557 free(track->stdata);
558 free(track->stream_header);
559 free(track->samples);
560 free(track->chunks);
561 free(track->chunkmap);
562 free(track->durmap);
563 free(track->keyframes);
564 free(track->editlist);
565 free(track->desc);
566 free(track);
569 free(priv);
572 unsigned int store_ughvlc(unsigned char *s, unsigned int v){
573 unsigned int n = 0;
575 while(v >= 0xff) {
576 *s++ = 0xff;
577 v -= 0xff;
578 n++;
580 *s = v;
581 n++;
583 return n;
586 static void init_vobsub(sh_sub_t *sh, mov_track_t *trak) {
587 sh->type = 'v';
588 if (trak->stdata_len < 106)
589 return;
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) {
599 #if 0
600 struct {
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)
619 // ---
620 // optional additional atom-based fields
621 // ([int32_t size,int32_t type,some data ],repeat)
622 } my_stdata;
623 #endif
624 int version, adjust;
625 int is_vorbis = 0;
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 */
639 /* amr narrowband */
640 trak->samplebytes=sh->samplesize=1;
641 trak->nchannels=sh->channels=1;
642 sh->samplerate=8000;
643 break;
645 case 0x62776173: /* sawb */
646 /* amr wideband */
647 trak->samplebytes=sh->samplesize=1;
648 trak->nchannels=sh->channels=1;
649 sh->samplerate=16000;
650 break;
652 default:
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
658 // stdata[]:
659 // 8 short version
660 // 10 short revision
661 // 12 int vendor_id
662 // 16 short channels
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
675 // esds atom:
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");
683 return 0;
685 version=char2short(trak->stdata,8);
686 if (version > 1)
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)) {
692 case 1:
693 trak->nchannels = 1; break;
694 case 2:
695 trak->nchannels = 2; break;
696 case 3:
697 trak->nchannels = 6; break;
698 default:
699 mp_msg(MSGT_DEMUX, MSGL_WARN,
700 "MOV: unable to determine audio channels, assuming 2 (got %d)\n",
701 char2short(trak->stdata,16));
702 trak->nchannels = 2;
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.
714 case 31:
715 sh->samplerate = 32000; break;
716 case 43:
717 sh->samplerate = 44100; break;
718 case 47:
719 sh->samplerate = 48000; break;
720 default:
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);
743 if((len >= 4) &&
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);
754 break;
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'):
759 if ((len >= 22) &&
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);
765 break;
766 default:
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);
773 } else {
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);
783 switch (version) {
784 case 0:
785 adjust = 0; break;
786 case 1:
787 adjust = 48; break;
788 case 2:
789 adjust = 68; break;
790 default:
791 mp_msg(MSGT_DEMUX, MSGL_WARN, "MOV: unknown sound atom version (%d); may not work!\n", version);
792 adjust = 68;
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);
800 if(atom_len > 8) {
801 esds_t esds;
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)
807 //vorbis audio
808 unsigned char *buf[3];
809 unsigned short sizes[3];
810 int offset, len, k;
811 unsigned char *ptr = esds.decoderConfig;
813 if(ptr[0] != 0 || ptr[1] != 30) goto quit_vorbis_block; //wrong extradata layout
815 offset = len = 0;
816 for(k = 0; k < 3; k++)
818 sizes[k] = (ptr[offset]<<8) | ptr[offset+1];
819 len += sizes[k];
820 offset += 2;
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]);
829 offset += sizes[k];
832 sh->codecdata_len = len + len/255 + 64;
833 sh->codecdata = malloc(sh->codecdata_len);
834 ptr = sh->codecdata;
836 ptr[0] = 2;
837 offset = 1;
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]);
843 offset += 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);
849 is_vorbis = 1;
850 quit_vorbis_block:
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
869 if(!is_vorbis)
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
878 #if 0
879 { FILE* f=fopen("esds.dat","wb");
880 fwrite(&trak->stdata[36],atom_len-8,1,f);
881 fclose(f); }
882 #endif
884 } break;
885 case MOV_FOURCC('a','l','a','c'): {
886 mp_msg(MSGT_DEMUX, MSGL_V, "MOV: Found alac atom (%d)!\n", atom_len);
887 if(atom_len > 8) {
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);
893 } break;
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);
896 if (atom_len>14) {
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]);
901 break;
902 default:
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],
905 atom_len);
908 mp_msg(MSGT_DEMUX, MSGL_V, "Fourcc: %.4s\n",(char *)&trak->fourcc);
909 #if 0
910 { FILE* f=fopen("stdata.dat","wb");
911 fwrite(trak->stdata,trak->stdata_len,1,f);
912 fclose(f); }
913 { FILE* f=fopen("tkdata.dat","wb");
914 fwrite(trak->tkdata,trak->tkdata_len,1,f);
915 fclose(f); }
916 #endif
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);
929 } else {
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;
941 // Selection:
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;
946 // }
947 return 1;
950 static int gen_sh_video(sh_video_t* sh, mov_track_t* trak, int timescale) {
951 int depth, i, entry;
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",
970 trak->stdata_len);
971 return 0;
974 depth = trak->stdata[75] | (trak->stdata[74] << 8);
975 if (trak->fourcc == mmioFOURCC('r', 'a', 'w', ' '))
976 sh->format = IMGFMT_RGB | depth;
978 // stdata[]:
979 // 8 short version
980 // 10 short revision
981 // 12 int vendor_id
982 // 16 int temporal_quality
983 // 20 int spatial_quality
984 // 24 short width
985 // 26 short height
986 // 28 int h_dpi
987 // 32 int v_dpi
988 // 36 int 0
989 // 40 short frames_per_sample
990 // 42 char[4] compressor_name
991 // 74 short depth
992 // 76 short color_table_id
993 // additional atoms may follow,
994 // eg esds atom from .MP4 files
995 // 78 int atom size
996 // 82 char[4] atom type
997 // 86 ... atom data
999 { ImageDescription* id=malloc(8+trak->stdata_len); // safe
1000 trak->desc=id;
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);
1019 sh->ImageDesc=id;
1020 #if 0
1021 { FILE *f=fopen("ImageDescription","wb");
1022 fwrite(id,id->idSize,1,f);
1023 fclose(f);
1025 #endif
1028 if(trak->stdata_len >= 86) { // extra atoms found
1029 int pos=78;
1030 int atom_len;
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",
1038 atom_len);
1039 break;
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",
1044 atom_len);
1045 break;
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",
1049 atom_len);
1050 break;
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",
1054 atom_len);
1055 break;
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
1062 if(atom_len > 8) {
1063 esds_t esds;
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
1080 break;
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);
1084 if(atom_len > 8) {
1085 int i, poffs, cnt;
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));
1096 poffs = pos + 14;
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));
1102 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);
1113 break;
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);
1116 if (atom_len>10)
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]);
1118 break;
1119 case 0:
1120 break;
1121 default:
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],
1124 atom_len);
1126 if(atom_len<8) break;
1127 pos+=atom_len;
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);
1151 // palettized?
1152 gray = 0;
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);
1156 else
1157 palette_count = 0;
1159 // emulate BITMAPINFOHEADER:
1160 if (palette_count)
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]);
1166 hdr_ptr += 2;
1167 start = AV_RB32(&trak->stdata[hdr_ptr]);
1168 hdr_ptr += 4;
1169 count_flag = AV_RB16(&trak->stdata[hdr_ptr]);
1170 hdr_ptr += 2;
1171 end = AV_RB16(&trak->stdata[hdr_ptr]);
1172 hdr_ptr += 2;
1173 palette_map = (unsigned char *)sh->bih + 40;
1174 mp_msg(MSGT_DEMUX, MSGL_V, "Allocated %d entries for palette\n",
1175 palette_count);
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
1182 if (flag & 0x08)
1184 if (gray)
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')) {
1192 int i;
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;
1202 else
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
1214 else
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]);
1220 hdr_ptr += 2;
1221 // apparently, if count_flag is set, entry is same as i
1222 if (count_flag & 0x8000)
1223 entry = i;
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]);
1236 else
1237 mp_msg(MSGT_DEMUX, MSGL_V, "QT palette: skipped entry (out of count): %d of %d\n",
1238 entry, palette_count);
1239 hdr_ptr += 6;
1243 else
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;
1256 } else {
1257 sh->bih=calloc(1, sizeof(*sh->bih));
1258 sh->bih->biSize=40;
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;
1279 // }
1280 return 1;
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);
1286 while(1){
1287 off_t pos;
1288 off_t len;
1289 unsigned int id;
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
1297 len-=8;
1298 id=stream_read_dword(demuxer->stream);
1300 mp_msg(MSGT_DEMUX,MSGL_DBG2,"lschunks %.4s %d\n",(char *)&id,(int)len);
1302 if(trak){
1303 if (lschunks_intrak(demuxer, level, id, pos, len, trak) < 0)
1304 return;
1305 } else { /* not in track */
1306 switch(id) {
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);
1311 if (version == 1)
1312 priv->duration=stream_read_qword(demuxer->stream);
1313 else
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);
1317 break;
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");
1323 return;
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);
1332 switch(trak->type){
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);
1337 break;
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);
1343 break;
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);
1353 else {
1354 sh->type = 'm';
1355 sub_utf8 = 1;
1357 } else
1358 mp_msg(MSGT_DEMUX, MSGL_V, "Generic track - not completely understood! (id: %d)\n",
1359 trak->id);
1360 /* XXX: Also this contains the FLASH data */
1362 #if 0
1364 int pos = stream_tell(demuxer->stream);
1365 int i;
1366 int fd;
1367 char name[20];
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);
1377 close(fd);
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);
1387 close(fd);
1389 if (trak->samplesize > 0)
1391 char *buf;
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);
1399 close(fd);
1401 stream_seek(demuxer->stream, pos);
1403 #endif
1404 break;
1405 default:
1406 mp_msg(MSGT_DEMUX, MSGL_V, "Unknown track type found (type: %d)\n", trak->type);
1407 break;
1409 mp_msg(MSGT_DEMUX, MSGL_V, "--------------\n");
1410 priv->track_db++;
1411 trak=NULL;
1412 break;
1414 #if !CONFIG_ZLIB
1415 case MOV_FOURCC('c','m','o','v'): {
1416 mp_tmsg(MSGT_DEMUX,MSGL_ERR,"MOV: Compressed headers support requires ZLIB!\n");
1417 return;
1419 #else
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);
1424 break;
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);
1430 break;
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;
1438 int zret;
1439 z_stream zstrm;
1440 stream_t* backup;
1442 if (moov_sz > UINT_MAX - 16) {
1443 mp_msg(MSGT_DEMUX, MSGL_ERR, "Invalid cmvd atom size %d\n", moov_sz);
1444 break;
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);
1461 if (zret != Z_OK)
1462 { mp_msg(MSGT_DEMUX, MSGL_ERR, "QT cmov: inflateInit err %d\n",zret);
1463 return;
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);
1468 return;
1470 #if 0
1471 else {
1472 FILE *DecOut;
1473 DecOut = fopen("Out.bin", "w");
1474 fwrite(moov_buf, 1, moov_sz, DecOut);
1475 fclose(DecOut);
1477 #endif
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;
1488 free(cmov_buf);
1489 free(moov_buf);
1490 break;
1492 #endif
1493 case MOV_FOURCC('u','d','t','a'):
1495 unsigned int udta_id;
1496 off_t udta_len;
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);
1506 udta_size -= 8;
1507 mp_msg(MSGT_DEMUX, MSGL_DBG2, "udta_id: %.4s (len: %"PRId64")\n", (char *)&udta_id, (int64_t)udta_len);
1508 switch (udta_id)
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;
1531 switch(udta_id)
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]);
1536 break;
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]);
1540 break;
1541 case MOV_FOURCC(0xa9,'i','n','f'):
1542 mp_msg(MSGT_DEMUX, MSGL_V, " Info: %s\n", &text[2]);
1543 break;
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]);
1548 break;
1549 case MOV_FOURCC(0xa9,'A','R','T'):
1550 mp_msg(MSGT_DEMUX, MSGL_V, " Artist: %s\n", &text[2]);
1551 break;
1552 case MOV_FOURCC(0xa9,'d','i','r'):
1553 mp_msg(MSGT_DEMUX, MSGL_V, " Director: %s\n", &text[2]);
1554 break;
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]);
1558 break;
1559 case MOV_FOURCC(0xa9,'r','e','q'):
1560 mp_msg(MSGT_DEMUX, MSGL_V, " Requirements: %s\n", &text[2]);
1561 break;
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]);
1565 break;
1566 case MOV_FOURCC(0xa9,'d','a','y'):
1567 mp_msg(MSGT_DEMUX, MSGL_V, " Creation timestamp: %s\n", &text[2]);
1568 break;
1569 case MOV_FOURCC(0xa9,'f','m','t'):
1570 mp_msg(MSGT_DEMUX, MSGL_V, " Format: %s\n", &text[2]);
1571 break;
1572 case MOV_FOURCC(0xa9,'p','r','d'):
1573 mp_msg(MSGT_DEMUX, MSGL_V, " Producer: %s\n", &text[2]);
1574 break;
1575 case MOV_FOURCC(0xa9,'p','r','f'):
1576 mp_msg(MSGT_DEMUX, MSGL_V, " Performer(s): %s\n", &text[2]);
1577 break;
1578 case MOV_FOURCC(0xa9,'s','r','c'):
1579 mp_msg(MSGT_DEMUX, MSGL_V, " Source providers: %s\n", &text[2]);
1580 break;
1582 udta_size -= 4+text_len;
1583 break;
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'):
1594 default:
1596 if( udta_len>udta_size)
1597 udta_len=udta_size;
1599 stream_skip(demuxer->stream, udta_len-4-4);
1600 udta_size -= udta_len;
1605 break;
1606 } /* eof udta */
1607 default:
1608 id = be2me_32(id);
1609 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: unknown chunk: %.4s %d\n",(char *)&id,(int)len);
1610 } /* endof switch */
1611 } /* endof else */
1613 pos+=len+8;
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)
1622 switch(id) {
1623 case MOV_FOURCC('m','d','a','t'): {
1624 mp_msg(MSGT_DEMUX,MSGL_WARN,"Hmm, strange MOV, parsing mdat in lschunks?\n");
1625 return -1;
1627 case MOV_FOURCC('f','r','e','e'):
1628 case MOV_FOURCC('u','d','t','a'):
1629 /* here not supported :p */
1630 break;
1631 case MOV_FOURCC('t','k','h','d'): {
1632 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*sTrack header!\n", level, "");
1633 // read codec data
1634 trak->tkdata_len = len;
1635 trak->tkdata = malloc(trak->tkdata_len);
1636 stream_read(demuxer->stream, trak->tkdata, trak->tkdata_len);
1638 0 1 Version
1639 1 3 Flags
1640 4 4 Creation time
1641 8 4 Modification time
1642 12 4 Track ID
1643 16 4 Reserved
1644 20 4 Duration
1645 24 8 Reserved
1646 32 2 Layer
1647 34 2 Alternate group
1648 36 2 Volume
1649 38 2 Reserved
1650 40 36 Matrix structure
1651 76 4 Track width
1652 80 4 Track height
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
1661 break;
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);
1667 // read timescale
1668 trak->timescale = stream_read_dword(demuxer->stream);
1669 // read length
1670 if (version == 1)
1671 trak->length = stream_read_qword(demuxer->stream);
1672 else
1673 trak->length = stream_read_dword(demuxer->stream);
1674 break;
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);
1686 str[len] = 0;
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);
1690 free(str);
1691 switch(bswap_32(type)) {
1692 case MOV_FOURCC('m','h','l','r'):
1693 trak->media_handler = bswap_32(subtype);
1694 break;
1695 case MOV_FOURCC('d','h','l','r'):
1696 trak->data_handler = bswap_32(subtype);
1697 break;
1698 default:
1699 mp_msg(MSGT_DEMUX, MSGL_V,
1700 "MOV: unknown handler class: 0x%X (%.4s)\n",
1701 bswap_32(type), (char *)&type);
1703 break;
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;
1708 // read video data
1709 break;
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;
1714 // read audio data
1715 break;
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;
1720 break;
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;
1725 break;
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",
1731 level, "", count);
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');
1740 if (len < 8)
1741 break; // error
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");
1747 // if(!i)
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))
1757 break;
1759 break;
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);
1764 int i;
1765 unsigned int pts = 0;
1766 mp_msg(MSGT_DEMUX, MSGL_V,
1767 "MOV: %*sSample duration table! (%d blocks)\n", level, "",
1768 len);
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",
1778 pts, trak->length);
1779 break;
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;
1786 int i;
1787 mp_msg(MSGT_DEMUX, MSGL_V,
1788 "MOV: %*sSample->Chunk mapping table! (%d blocks) (ver:%d,flags:%d)\n", level, "",
1789 len, ver, flags);
1790 // read data:
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);
1798 break;
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);
1806 int i;
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;
1811 if (!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);
1818 break;
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);
1823 int i;
1824 mp_msg(MSGT_DEMUX, MSGL_V,
1825 "MOV: %*sChunk offset table! (%d chunks)\n", level, "",
1826 len);
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;
1832 // read elements:
1833 for(i = 0; i < trak->chunks_size; i++)
1834 trak->chunks[i].pos = stream_read_dword(demuxer->stream);
1835 break;
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);
1840 int i;
1841 mp_msg(MSGT_DEMUX, MSGL_V,
1842 "MOV: %*s64bit chunk offset table! (%d chunks)\n", level, "",
1843 len);
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;
1849 // read elements:
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);
1855 #else
1856 trak->chunks[i].pos = stream_read_qword(demuxer->stream);
1857 #endif
1859 break;
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;
1866 int i;
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;
1874 break;
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);
1879 break;
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);
1884 break;
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);
1889 break;
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);
1894 break;
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;
1901 int i;
1902 mp_msg(MSGT_DEMUX, MSGL_V,
1903 "MOV: %*sEdit list table (%d entries) (ver:%d,flags:%d)\n", level, "",
1904 entries, ver, flags);
1905 #if 1
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);
1919 #endif
1920 break;
1922 case MOV_FOURCC('c','o','d','e'): {
1923 /* XXX: Implement atom 'code' for FLASH support */
1924 break;
1926 default:
1927 id = be2me_32(id);
1928 mp_msg(MSGT_DEMUX,MSGL_V,"MOV: unknown chunk: %.4s %d\n",(char *)&id,(int)len);
1929 break;
1930 }//switch(id)
1931 return 0;
1934 static demuxer_t* mov_read_header(demuxer_t* demuxer){
1935 struct MPOpts *opts = demuxer->opts;
1936 mov_priv_t* priv=demuxer->priv;
1937 int t_no;
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");
1943 // Parse header:
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);
1949 return 0;
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];
1975 if(sh){
1976 demuxer->audio->sh=sh; sh->ds=demuxer->audio;
1977 } else {
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];
1984 if(sh){
1985 demuxer->video->sh=sh; sh->ds=demuxer->video;
1986 } else {
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];
1993 if(sh){
1994 demuxer->sub->sh=sh;
1995 } else {
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')) {
2006 stream_t *s;
2007 demuxer_t *od;
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
2014 break;
2019 #if 0
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){
2024 int i;
2025 int fd;
2026 char name[20];
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;
2031 char buf[len];
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')) ?
2035 "swf":"dump");
2036 fd = open(name, O_CREAT|O_WRONLY);
2037 // { int j;
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'));
2040 // }
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);
2046 #if CONFIG_ZLIB
2047 // unzip:
2048 z_stream zstrm;
2049 int zret;
2050 char buf2[newlen];
2052 len-=4;
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);
2069 } else {
2070 #else
2071 len-=4;
2072 mp_msg(MSGT_DEMUX, MSGL_INFO, "******* ZLIB COMPRESSED SAMPLE!!!!! (%d->%d bytes) *******\n",len,newlen);
2075 #endif
2076 stream_read(demuxer->stream, buf, len);
2077 write(fd, buf, len);
2079 close(fd);
2084 demuxer->stream->eof = 0;
2085 #endif
2087 return demuxer;
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,
2094 * NULL if not found
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];
2099 return NULL;
2102 // return value:
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;
2108 float pts;
2109 int x;
2110 off_t pos;
2112 if (ds->eof) return 0;
2113 trak = stream_track(priv, ds);
2114 if (!trak) return 0;
2116 if(trak->samplesize){
2117 // read chunk:
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",
2124 trak->samplesize);
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;
2127 else
2128 x=trak->chunks[trak->pos].size;
2130 else
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
2140 } else {
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 !
2145 } else {
2146 x*=trak->nchannels;
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;
2153 } else {
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;
2168 // calc pts:
2169 pts=(float)(trak->samples[frame].pts+
2170 trak->editlist[trak->editlist_pos].pts_offset)/(float)trak->timescale;
2171 } else {
2172 if(frame>=trak->samples_size) return 0; // EOF
2173 pts=(float)trak->samples[frame].pts/(float)trak->timescale;
2175 // read sample:
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;
2188 dp->pts=pts;
2189 dp->flags=0;
2190 dp->pos=pos; // FIXME?
2191 ds_add_packet(ds,dp);
2192 } else
2193 ds_read_packet(ds,demuxer->stream,x,pts,pos,0);
2195 ++trak->pos;
2197 trak = NULL;
2198 if (demuxer->sub->id >= 0 && demuxer->sub->id < priv->track_db)
2199 trak = priv->tracks[demuxer->sub->id];
2200 if (trak) {
2201 int samplenr = 0;
2202 while (samplenr < trak->samples_size) {
2203 double subpts = (double)trak->samples[samplenr].pts / (double)trak->timescale;
2204 if (subpts >= pts) break;
2205 samplenr++;
2207 samplenr--;
2208 if (samplenr < 0)
2209 vo_sub = NULL;
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;
2220 return 1;
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
2233 trak->pos=0;
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;
2237 } else {
2238 unsigned int ipts;
2239 if(!(flags&SEEK_ABSOLUTE)) pts+=trak->samples[trak->pos].pts;
2240 if(pts<0) pts=0;
2241 ipts=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
2249 int i;
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]))
2255 --i;
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);
2264 return pts;
2267 static void demux_seek_mov(demuxer_t *demuxer,float pts,float audio_delay,int flags){
2268 mov_priv_t* priv=demuxer->priv;
2269 demux_stream_t* ds;
2270 mov_track_t* trak;
2272 // printf("MOV seek called %5.3f flag=%d \n",pts,flags);
2274 ds=demuxer->video;
2275 trak = stream_track(priv, ds);
2276 if (trak) {
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;
2281 else pts = ds->pts;
2282 flags=1; // absolute seconds
2285 ds=demuxer->audio;
2286 trak = stream_track(priv, ds);
2287 if (trak) {
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){
2297 mov_track_t* track;
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;
2308 switch(cmd) {
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",
2330 "mov",
2331 "Quicktime/MOV",
2332 "Arpi, Al3x, Atmos, others",
2333 "Handles Quicktime, MP4, 3GP",
2334 DEMUXER_TYPE_MOV,
2335 0, // slow autodetect
2336 mov_check_file,
2337 demux_mov_fill_buffer,
2338 mov_read_header,
2339 demux_close_mov,
2340 demux_seek_mov,
2341 demux_mov_control