2001-08-12 01:58:05 +00:00
|
|
|
// QuickTime MOV file parser
|
|
|
|
// based on TOOLS/movinfo.c by A'rpi & Al3x
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2001-08-17 00:40:25 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "mp_msg.h"
|
2001-09-26 21:35:14 +00:00
|
|
|
#include "help_mp.h"
|
2001-08-12 01:58:05 +00:00
|
|
|
|
|
|
|
#include "stream.h"
|
|
|
|
#include "demuxer.h"
|
|
|
|
|
|
|
|
#include "wine/mmreg.h"
|
|
|
|
#include "wine/avifmt.h"
|
|
|
|
#include "wine/vfw.h"
|
|
|
|
|
|
|
|
#include "codec-cfg.h"
|
|
|
|
#include "stheader.h"
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int id;
|
|
|
|
int type;
|
|
|
|
int timescale;
|
|
|
|
int width,height; // for video
|
|
|
|
unsigned int fourcc;
|
|
|
|
int data_len;
|
|
|
|
unsigned char* data;
|
|
|
|
} mov_track_t;
|
|
|
|
|
|
|
|
#define MOV_MAX_TRACKS 256
|
|
|
|
|
|
|
|
#define MOV_TRAK_UNKNOWN 0
|
|
|
|
#define MOV_TRAK_VIDEO 1
|
|
|
|
#define MOV_TRAK_AUDIO 2
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
off_t moov_start;
|
|
|
|
off_t moov_end;
|
|
|
|
off_t mdat_start;
|
|
|
|
off_t mdat_end;
|
|
|
|
int track_db;
|
|
|
|
mov_track_t* tracks[MOV_MAX_TRACKS];
|
|
|
|
} mov_priv_t;
|
|
|
|
|
|
|
|
#define MOV_FOURCC(a,b,c,d) ((a<<24)|(b<<16)|(c<<8)|(d))
|
|
|
|
|
|
|
|
int mov_check_file(demuxer_t* demuxer){
|
|
|
|
int flags=0;
|
|
|
|
mov_priv_t* priv=malloc(sizeof(mov_priv_t));
|
|
|
|
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"Checking for MOV\n");
|
2001-08-12 01:58:05 +00:00
|
|
|
|
|
|
|
memset(priv,0,sizeof(mov_priv_t));
|
|
|
|
demuxer->priv=priv;
|
|
|
|
|
|
|
|
while(1){
|
|
|
|
off_t len=stream_read_dword(demuxer->stream);
|
|
|
|
unsigned int id=stream_read_dword(demuxer->stream);
|
|
|
|
if(stream_eof(demuxer->stream)) break; // EOF
|
|
|
|
if(len<8) break; // invalid chunk
|
|
|
|
switch(id){
|
|
|
|
case MOV_FOURCC('m','o','o','v'):
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"MOV: Movie header found!\n");
|
2001-08-12 01:58:05 +00:00
|
|
|
priv->moov_start=stream_tell(demuxer->stream);
|
|
|
|
priv->moov_end=priv->moov_start+len-8;
|
|
|
|
flags|=1;
|
|
|
|
break;
|
|
|
|
case MOV_FOURCC('m','d','a','t'):
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"MOV: Movie DATA found!\n");
|
2001-08-12 01:58:05 +00:00
|
|
|
priv->mdat_start=stream_tell(demuxer->stream);
|
|
|
|
priv->mdat_end=priv->mdat_start+len-8;
|
|
|
|
flags|=2;
|
|
|
|
break;
|
|
|
|
default:
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"MOV: unknown chunk: %.4s %d\n",&id,(int)len);
|
2001-08-12 01:58:05 +00:00
|
|
|
}
|
|
|
|
if(!stream_skip(demuxer->stream,len-8)) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (flags==3);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void lschunks(demuxer_t* demuxer,int level,off_t endpos,mov_track_t* trak){
|
|
|
|
mov_priv_t* priv=demuxer->priv;
|
|
|
|
while(1){
|
|
|
|
off_t pos;
|
|
|
|
off_t len;
|
|
|
|
unsigned int id;
|
|
|
|
//
|
|
|
|
pos=stream_tell(demuxer->stream);
|
|
|
|
if(pos>=endpos) return; // END
|
|
|
|
len=stream_read_dword(demuxer->stream);
|
|
|
|
if(len<8) return; // error
|
|
|
|
len-=8;
|
|
|
|
id=stream_read_dword(demuxer->stream);
|
|
|
|
//
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_DBG2,"lschunks %.4s %d\n",&id,(int)len);
|
2001-08-12 01:58:05 +00:00
|
|
|
//
|
|
|
|
if(trak){
|
|
|
|
switch(id){
|
|
|
|
case MOV_FOURCC('t','k','h','d'): {
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*sTrack header!\n",level,"");
|
2001-08-12 01:58:05 +00:00
|
|
|
// read width x height
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MOV_FOURCC('m','d','h','d'): {
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*sMedia header!\n",level,"");
|
2001-08-12 01:58:05 +00:00
|
|
|
// read timescale
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MOV_FOURCC('v','m','h','d'): {
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*sVideo header!\n",level,"");
|
2001-08-12 01:58:05 +00:00
|
|
|
trak->type=MOV_TRAK_VIDEO;
|
|
|
|
// read video data
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MOV_FOURCC('s','m','h','d'): {
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*sSound header!\n",level,"");
|
2001-08-12 01:58:05 +00:00
|
|
|
trak->type=MOV_TRAK_AUDIO;
|
|
|
|
// read audio data
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MOV_FOURCC('s','t','s','d'): {
|
|
|
|
int i=stream_read_dword(demuxer->stream); // temp!
|
|
|
|
int count=stream_read_dword(demuxer->stream);
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*sDescription list! (cnt:%d)\n",level,"",count);
|
2001-08-12 01:58:05 +00:00
|
|
|
for(i=0;i<count;i++){
|
|
|
|
off_t pos=stream_tell(demuxer->stream);
|
|
|
|
off_t len=stream_read_dword(demuxer->stream);
|
|
|
|
unsigned int fourcc=stream_read_dword_le(demuxer->stream);
|
|
|
|
if(len<8) break; // error
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*s desc #%d: %.4s",level,"",i,&fourcc);
|
2001-08-12 01:58:05 +00:00
|
|
|
if(!i){
|
|
|
|
trak->fourcc=fourcc;
|
|
|
|
// read codec data
|
|
|
|
trak->data_len=len-8;
|
|
|
|
trak->data=malloc(trak->data_len);
|
|
|
|
stream_read(demuxer->stream,trak->data,trak->data_len);
|
|
|
|
if(trak->type==MOV_TRAK_VIDEO && trak->data_len>43){
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V," '%.*s'",trak->data_len-43,trak->data+43);
|
2001-08-12 01:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"\n");
|
2001-08-12 01:58:05 +00:00
|
|
|
if(fourcc!=trak->fourcc && i)
|
2001-09-26 21:35:14 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_WARN,MSGTR_MOVvariableFourCC);
|
2001-08-12 01:58:05 +00:00
|
|
|
if(!stream_seek(demuxer->stream,pos+len)) break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MOV_FOURCC('m','d','i','a'): {
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*sMedia stream!\n",level,"");
|
2001-08-12 01:58:05 +00:00
|
|
|
lschunks(demuxer,level+1,pos+len,trak);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MOV_FOURCC('m','i','n','f'): {
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*sMedia info!\n",level,"");
|
2001-08-12 01:58:05 +00:00
|
|
|
lschunks(demuxer,level+1,pos+len,trak);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MOV_FOURCC('s','t','b','l'): {
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"MOV: %*sSample info!\n",level,"");
|
2001-08-12 01:58:05 +00:00
|
|
|
lschunks(demuxer,level+1,pos+len,trak);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}//switch(id)
|
|
|
|
} else
|
|
|
|
if(id==MOV_FOURCC('t','r','a','k')){
|
|
|
|
// if(trak) printf("MOV: Warning! trak in trak?\n");
|
|
|
|
if(priv->track_db>=MOV_MAX_TRACKS){
|
2001-09-26 21:35:14 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_WARN,MSGTR_MOVtooManyTrk);
|
2001-08-12 01:58:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
trak=malloc(sizeof(mov_track_t));
|
|
|
|
memset(trak,0,sizeof(mov_track_t));
|
2001-08-17 00:40:25 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"MOV: Track #%d:\n",priv->track_db);
|
2001-08-12 01:58:05 +00:00
|
|
|
trak->id=priv->track_db;
|
|
|
|
priv->tracks[priv->track_db++]=trak;
|
|
|
|
lschunks(demuxer,level+1,pos+len,trak);
|
|
|
|
trak=NULL;
|
|
|
|
} else
|
|
|
|
if(id==MOV_FOURCC('c','m','o','v')){
|
2001-09-26 21:35:14 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_ERR,MSGTR_MOVcomprhdr);
|
2001-08-12 01:58:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos+=len+8;
|
|
|
|
if(pos>=endpos) break;
|
|
|
|
if(!stream_seek(demuxer->stream,pos)) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int mov_read_header(demuxer_t* demuxer){
|
|
|
|
mov_priv_t* priv=demuxer->priv;
|
|
|
|
|
|
|
|
// printf("mov_read_header!\n");
|
|
|
|
|
|
|
|
// Parse header:
|
|
|
|
if(!stream_seek(demuxer->stream,priv->moov_start)) return 0; // ???
|
|
|
|
lschunks(demuxer, 0, priv->moov_end, NULL);
|
|
|
|
|
|
|
|
// Build tables:
|
|
|
|
// ...
|
|
|
|
|
2001-09-26 21:35:14 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_ERR,MSGTR_MOVnotyetsupp);
|
2001-08-12 01:58:05 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|