//#define USE_QTX_CODECS
// QuickTime MOV file parser by A'rpi
// additional work by Atmos
// based on TOOLS/movinfo.c by A'rpi & Al3x
// compressed header support from moov.c of the openquicktime lib.
// References: http://openquicktime.sf.net/, http://www.heroinewarrior.com/
// http://www.geocities.com/SiliconValley/Lakes/2160/fformats/files/mov.pdf
// (above url no longer works, file mirrored somewhere? ::atmos)
// The QuickTime File Format PDF from Apple:
// http://developer.apple.com/techpubs/quicktime/qtdevdocs/PDF/QTFileFormat.pdf
// (Complete list of documentation at http://developer.apple.com/quicktime/)
// MP4-Lib sources from http://mpeg4ip.sf.net/ might be usefull fot .mp4
// aswell as .mov specific stuff.
// All sort of Stuff about MPEG4:
// http://www.cmlab.csie.ntu.edu.tw/~pkhsiao/thesis.html
// I really recommend N4270-1.doc and N4270-2.doc which are exact specs
// of the MP4-File Format and the MPEG4 Specific extensions. ::atmos
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "config.h"
#include "mp_msg.h"
#include "help_mp.h"
#include "stream.h"
#include "demuxer.h"
#include "stheader.h"
#include "bswap.h"
#include "qtpalette.h"
#include "parse_mp4.h" // .MP4 specific stuff
#ifdef USE_QTX_CODECS
#include "../loader/qtx/qtxsdk/components.h"
#endif
#ifdef HAVE_ZLIB
#include <zlib.h>
#endif
// inclusion of fcntl.h cause cygwin gcc crash
#ifndef __CYGWIN__
#include <fcntl.h>
#endif
#define BE_16(x) (((unsigned char *)(x))[0] << 8 | \
((unsigned char *)(x))[1])
#define BE_32(x) (((unsigned char *)(x))[0] << 24 | \
((unsigned char *)(x))[1] << 16 | \
((unsigned char *)(x))[2] << 8 | \
((unsigned char *)(x))[3])
#define char2short(x,y) BE_16(&(x)[(y)])
#define char2int(x,y) BE_32(&(x)[(y)])
typedef struct {
unsigned int pts; // duration
unsigned int size;
off_t pos;
} mov_sample_t;
typedef struct {
unsigned int sample; // number of the first sample in the chunk
unsigned int size; // number of samples in the chunk
int desc; // for multiple codecs mode - not used
off_t pos;
} mov_chunk_t;
typedef struct {
unsigned int first;
unsigned int spc;
unsigned int sdid;
} mov_chunkmap_t;
typedef struct {
unsigned int num;
unsigned int dur;
} mov_durmap_t;
typedef struct {
unsigned int dur;
unsigned int pos;
int speed;
//
int frames;
int start_sample;
int start_frame;
int pts_offset;
} mov_editlist_t;
#define MOV_TRAK_UNKNOWN 0
#define MOV_TRAK_VIDEO 1
#define MOV_TRAK_AUDIO 2
#define MOV_TRAK_FLASH 3
#define MOV_TRAK_GENERIC 4
#define MOV_TRAK_CODE 5
typedef struct {
int id;
int type;
off_t pos;
//
int timescale;
unsigned int length;
int samplesize; // 0 = variable
int duration; // 0 = variable
int width,height; // for video
unsigned int fourcc;
//
int tkdata_len; // track data
unsigned char* tkdata;
int stdata_len; // stream data
unsigned char* stdata;
//
unsigned char* stream_header;
int stream_header_len; // if >0, this header should be sent before the 1st frame
//
int samples_size;
mov_sample_t* samples;
int chunks_size;
mov_chunk_t* chunks;
int chunkmap_size;
mov_chunkmap_t* chunkmap;
int durmap_size;
mov_durmap_t* durmap;
int keyframes_size;
unsigned int* keyframes;
int editlist_size;
mov_editlist_t* editlist;
int editlist_pos;
//
void* desc; // image/sound/etc description (pointer to ImageDescription etc)
} mov_track_t;
void mov_build_index(mov_track_t* trak,int timescale){
int i,j,s;
int last=trak->chunks_size;
unsigned int pts=0;
#if 0
if (trak->chunks_size <= 0)
{
mp_msg(MSGT_DEMUX, MSGL_WARN, "No chunk offset table, trying to build one!\n");
trak->chunks_size = trak->samples_size; /* XXX: FIXME ! */
trak->chunks = realloc(trak->chunks, sizeof(mov_chunk_t)*trak->chunks_size);
for (i=0; i < trak->chunks_size; i++)
trak->chunks[i].pos = -1;
}
#endif
mp_msg(MSGT_DEMUX, MSGL_INFO, "MOV track #%d: %d chunks, %d samples\n",trak->id,trak->chunks_size,trak->samples_size);
mp_msg(MSGT_DEMUX, MSGL_V, "pts=%d scale=%d time=%5.3f\n",trak->length,trak->timescale,(float)trak->length/(float)trak->timescale);
// process chunkmap:
i=trak->chunkmap_size;
while(i>0){
--i;
for(j=trak->chunkmap[i].first;j<last;j++){
trak->chunks[j].desc=trak->chunkmap[i].sdid;
trak->chunks[j].size=trak->chunkmap[i].spc;
}
last=trak->chunkmap[i].first;
}
#if 0
for (i=0; i < trak->chunks_size; i++)
{
/* fixup position */
if (trak->chunks[i].pos == -1)
if (i > 0)
trak->chunks[i].pos = trak->chunks[i-1].pos + trak->chunks[i-1].size;
else
trak->chunks[i].pos = 0; /* FIXME: set initial pos */
#endif
// calc pts of chunks:
s=0;
for(j=0;j<trak->chunks_size;j++){
trak->chunks[j].sample=s;
s+=trak->chunks[j].size;
}
// workaround for fixed-size video frames (dv and uncompressed)
if(!trak->samples_size && trak->type==MOV_TRAK_VIDEO){
trak->samples_size=s;
trak->samples=malloc(sizeof(mov_sample_t)*s);
for(i=0;i<s;i++)
trak->samples[i].size=trak->samplesize;
trak->samplesize=0;
}
if(!trak->samples_size){
// constant sampesize
if(trak->durmap_size==1 || (trak->durmap_size==2 && trak->durmap[1].num==1)){
trak->duration=trak->durmap[0].dur;
} else mp_msg(MSGT_DEMUX, MSGL_ERR, "*** constant samplesize & variable duration not yet supported! ***\nContact the author if you have such sample file!\n");
return;
}
// calc pts:
s=0;
for(j=0;j<trak->durmap_size;j++){
for(i=0;i<trak->durmap[j].num;i++){
trak->samples[s].pts=pts;
++s;
pts+=trak->durmap[j].dur;
}
}
// calc
|