📄 mov.c
字号:
/*
* MOV demuxer
* Copyright (c) 2001 Fabrice Bellard.
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <limits.h>
//#define DEBUG
#include "avformat.h"
#include "riff.h"
#include "isom.h"
#include "dv.h"
#ifdef CONFIG_ZLIB
#include <zlib.h>
#endif
/*
* First version by Francois Revol revol@free.fr
* Seek function by Gael Chardon gael.dev@4now.net
*
* Features and limitations:
* - reads most of the QT files I have (at least the structure),
* Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html
* - the code is quite ugly... maybe I won't do it recursive next time :-)
*
* Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/
* when coding this :) (it's a writer anyway)
*
* Reference documents:
* http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt
* Apple:
* http://developer.apple.com/documentation/QuickTime/QTFF/
* http://developer.apple.com/documentation/QuickTime/QTFF/qtff.pdf
* QuickTime is a trademark of Apple (AFAIK :))
*/
#include "qtpalette.h"
#undef NDEBUG
#include <assert.h>
/* the QuickTime file format is quite convoluted...
* it has lots of index tables, each indexing something in another one...
* Here we just use what is needed to read the chunks
*/
typedef struct {
int first;
int count;
int id;
} MOV_stsc_t;
typedef struct {
uint32_t type;
int64_t offset;
int64_t size; /* total size (excluding the size and type fields) */
} MOV_atom_t;
typedef struct {
offset_t offset;
int64_t size;
} MOV_mdat_t;
struct MOVParseTableEntry;
typedef struct MOVStreamContext {
int ffindex; /* the ffmpeg stream id */
int next_chunk;
unsigned int chunk_count;
int64_t *chunk_offsets;
unsigned int stts_count;
MOV_stts_t *stts_data;
unsigned int ctts_count;
MOV_stts_t *ctts_data;
unsigned int edit_count; /* number of 'edit' (elst atom) */
unsigned int sample_to_chunk_sz;
MOV_stsc_t *sample_to_chunk;
int sample_to_ctime_index;
int sample_to_ctime_sample;
unsigned int sample_size;
unsigned int sample_count;
int *sample_sizes;
unsigned int keyframe_count;
int *keyframes;
int time_scale;
int time_rate;
int current_sample;
unsigned int bytes_per_frame;
unsigned int samples_per_frame;
int dv_audio_container;
} MOVStreamContext;
typedef struct MOVContext {
AVFormatContext *fc;
int time_scale;
int64_t duration; /* duration of the longest track */
int found_moov; /* when both 'moov' and 'mdat' sections has been found */
int found_mdat; /* we suppose we have enough data to read the file */
int64_t mdat_offset;
int total_streams;
MOVStreamContext *streams[MAX_STREAMS];
const struct MOVParseTableEntry *parse_table; /* could be eventually used to change the table */
/* NOTE: for recursion save to/ restore from local variable! */
AVPaletteControl palette_control;
MOV_mdat_t *mdat_list;
int mdat_count;
DVDemuxContext *dv_demux;
AVFormatContext *dv_fctx;
int isom; /* 1 if file is ISO Media (mp4/3gp) */
} MOVContext;
/* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
/* those functions parse an atom */
/* return code:
1: found what I wanted, exit
0: continue to parse next atom
-1: error occured, exit
*/
typedef int (*mov_parse_function)(MOVContext *ctx, ByteIOContext *pb, MOV_atom_t atom);
/* links atom IDs to parse functions */
typedef struct MOVParseTableEntry {
uint32_t type;
mov_parse_function func;
} MOVParseTableEntry;
static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
{
int64_t total_size = 0;
MOV_atom_t a;
int i;
int err = 0;
a.offset = atom.offset;
if (atom.size < 0)
atom.size = INT64_MAX;
while(((total_size + 8) < atom.size) && !url_feof(pb) && !err) {
a.size = atom.size;
a.type=0L;
if(atom.size >= 8) {
a.size = get_be32(pb);
a.type = get_le32(pb);
}
total_size += 8;
a.offset += 8;
dprintf(c->fc, "type: %08x %.4s sz: %"PRIx64" %"PRIx64" %"PRIx64"\n", a.type, (char*)&a.type, a.size, atom.size, total_size);
if (a.size == 1) { /* 64 bit extended size */
a.size = get_be64(pb) - 8;
a.offset += 8;
total_size += 8;
}
if (a.size == 0) {
a.size = atom.size - total_size;
if (a.size <= 8)
break;
}
a.size -= 8;
if(a.size < 0 || a.size > atom.size - total_size)
break;
for (i = 0; c->parse_table[i].type != 0L
&& c->parse_table[i].type != a.type; i++)
/* empty */;
if (c->parse_table[i].type == 0) { /* skip leaf atoms data */
url_fskip(pb, a.size);
} else {
offset_t start_pos = url_ftell(pb);
int64_t left;
err = (c->parse_table[i].func)(c, pb, a);
left = a.size - url_ftell(pb) + start_pos;
if (left > 0) /* skip garbage at atom end */
url_fskip(pb, left);
}
a.offset += a.size;
total_size += a.size;
}
if (!err && total_size < atom.size && atom.size < 0x7ffff) {
url_fskip(pb, atom.size - total_size);
}
return err;
}
static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
{
AVStream *st = c->fc->streams[c->fc->nb_streams-1];
uint32_t type;
uint32_t ctype;
get_byte(pb); /* version */
get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
/* component type */
ctype = get_le32(pb);
type = get_le32(pb); /* component subtype */
dprintf(c->fc, "ctype= %c%c%c%c (0x%08x)\n", *((char *)&ctype), ((char *)&ctype)[1], ((char *)&ctype)[2], ((char *)&ctype)[3], (int) ctype);
dprintf(c->fc, "stype= %c%c%c%c\n", *((char *)&type), ((char *)&type)[1], ((char *)&type)[2], ((char *)&type)[3]);
if(!ctype)
c->isom = 1;
if(type == MKTAG('v', 'i', 'd', 'e'))
st->codec->codec_type = CODEC_TYPE_VIDEO;
else if(type == MKTAG('s', 'o', 'u', 'n'))
st->codec->codec_type = CODEC_TYPE_AUDIO;
else if(type == MKTAG('m', '1', 'a', ' '))
st->codec->codec_id = CODEC_ID_MP2;
else if(type == MKTAG('s', 'u', 'b', 'p')) {
st->codec->codec_type = CODEC_TYPE_SUBTITLE;
st->codec->codec_id = CODEC_ID_DVD_SUBTITLE;
}
get_be32(pb); /* component manufacture */
get_be32(pb); /* component flags */
get_be32(pb); /* component flags mask */
if(atom.size <= 24)
return 0; /* nothing left to read */
url_fskip(pb, atom.size - (url_ftell(pb) - atom.offset));
return 0;
}
static int mp4_read_descr_len(ByteIOContext *pb)
{
int len = 0;
int count = 4;
while (count--) {
int c = get_byte(pb);
len = (len << 7) | (c & 0x7f);
if (!(c & 0x80))
break;
}
return len;
}
static int mp4_read_descr(MOVContext *c, ByteIOContext *pb, int *tag)
{
int len;
*tag = get_byte(pb);
len = mp4_read_descr_len(pb);
dprintf(c->fc, "MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
return len;
}
#define MP4ESDescrTag 0x03
#define MP4DecConfigDescrTag 0x04
#define MP4DecSpecificDescrTag 0x05
static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
{
AVStream *st = c->fc->streams[c->fc->nb_streams-1];
int tag, len;
get_be32(pb); /* version + flags */
len = mp4_read_descr(c, pb, &tag);
if (tag == MP4ESDescrTag) {
get_be16(pb); /* ID */
get_byte(pb); /* priority */
} else
get_be16(pb); /* ID */
len = mp4_read_descr(c, pb, &tag);
if (tag == MP4DecConfigDescrTag) {
int object_type_id = get_byte(pb);
get_byte(pb); /* stream type */
get_be24(pb); /* buffer size db */
get_be32(pb); /* max bitrate */
get_be32(pb); /* avg bitrate */
st->codec->codec_id= codec_get_id(ff_mp4_obj_type, object_type_id);
dprintf(c->fc, "esds object type id %d\n", object_type_id);
len = mp4_read_descr(c, pb, &tag);
if (tag == MP4DecSpecificDescrTag) {
dprintf(c->fc, "Specific MPEG4 header len=%d\n", len);
st->codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
if (st->codec->extradata) {
get_buffer(pb, st->codec->extradata, len);
st->codec->extradata_size = len;
/* from mplayer */
if ((*st->codec->extradata >> 3) == 29) {
st->codec->codec_id = CODEC_ID_MP3ON4;
}
}
}
}
return 0;
}
/* this atom contains actual media data */
static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
{
if(atom.size == 0) /* wrong one (MP4) */
return 0;
c->mdat_list = av_realloc(c->mdat_list, (c->mdat_count + 1) * sizeof(*c->mdat_list));
c->mdat_list[c->mdat_count].offset = atom.offset;
c->mdat_list[c->mdat_count].size = atom.size;
c->mdat_count++;
c->found_mdat=1;
c->mdat_offset = atom.offset;
if(c->found_moov)
return 1; /* found both, just go */
url_fskip(pb, atom.size);
return 0; /* now go for moov */
}
static int mov_read_ftyp(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
{
uint32_t type = get_le32(pb);
if (type != MKTAG('q','t',' ',' '))
c->isom = 1;
av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
get_be32(pb); /* minor version */
url_fskip(pb, atom.size - 8);
return 0;
}
/* this atom should contain all header atoms */
static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
{
int err;
err = mov_read_default(c, pb, atom);
/* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
/* so we don't parse the whole file if over a network */
c->found_moov=1;
if(c->found_mdat)
return 1; /* found both, just go */
return 0; /* now go for mdat */
}
static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
{
AVStream *st = c->fc->streams[c->fc->nb_streams-1];
MOVStreamContext *sc = st->priv_data;
int version = get_byte(pb);
int lang;
if (version > 1)
return 1; /* unsupported */
get_byte(pb); get_byte(pb);
get_byte(pb); /* flags */
if (version == 1) {
get_be64(pb);
get_be64(pb);
} else {
get_be32(pb); /* creation time */
get_be32(pb); /* modification time */
}
sc->time_scale = get_be32(pb);
st->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
lang = get_be16(pb); /* language */
ff_mov_lang_to_iso639(lang, st->language);
get_be16(pb); /* quality */
return 0;
}
static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
{
int version = get_byte(pb); /* version */
get_byte(pb); get_byte(pb); get_byte(pb); /* flags */
if (version == 1) {
get_be64(pb);
get_be64(pb);
} else {
get_be32(pb); /* creation time */
get_be32(pb); /* modification time */
}
c->time_scale = get_be32(pb); /* time scale */
dprintf(c->fc, "time scale = %i\n", c->time_scale);
c->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
get_be32(pb); /* preferred scale */
get_be16(pb); /* preferred volume */
url_fskip(pb, 10); /* reserved */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -