?? 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"#include "libavcodec/mpeg4audio.h"#include "libavcodec/mpegaudiodata.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; char *path;} MOV_dref_t;typedef struct { uint32_t type; int64_t offset; int64_t size; /* total size (excluding the size and type fields) */} MOV_atom_t;struct MOVParseTableEntry;typedef struct { unsigned track_id; uint64_t base_data_offset; uint64_t moof_offset; unsigned stsd_id; unsigned duration; unsigned size; unsigned flags;} MOVFragment;typedef struct { unsigned track_id; unsigned stsd_id; unsigned duration; unsigned size; unsigned flags;} MOVTrackExt;typedef struct MOVStreamContext { ByteIOContext *pb; 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; int pseudo_stream_id; ///< -1 means demux all ids int16_t audio_cid; ///< stsd audio compression id unsigned drefs_count; MOV_dref_t *drefs; int dref_id;} 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 */ AVPaletteControl palette_control; DVDemuxContext *dv_demux; AVFormatContext *dv_fctx; int isom; /* 1 if file is ISO Media (mp4/3gp) */ MOVFragment fragment; ///< current fragment in moof atom MOVTrackExt *trex_data; unsigned trex_count;} 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: 0: continue to parse next atom <0: error occurred, exit*//* links atom IDs to parse functions */typedef struct MOVParseTableEntry { uint32_t type; int (*parse)(MOVContext *ctx, ByteIOContext *pb, MOV_atom_t atom);} MOVParseTableEntry;#ifdef __CW32__const MOVParseTableEntry mov_default_parse_table[];#elsestatic const MOVParseTableEntry mov_default_parse_table[];#endifstatic 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=0; 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) break; a.size = FFMIN(a.size, atom.size - total_size); for (i = 0; mov_default_parse_table[i].type != 0 && mov_default_parse_table[i].type != a.type; i++) /* empty */; if (mov_default_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 = mov_default_parse_table[i].parse(c, pb, a); if (url_is_streamed(pb) && c->found_moov && c->found_mdat) break; 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_dref(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom){ AVStream *st = c->fc->streams[c->fc->nb_streams-1]; MOVStreamContext *sc = st->priv_data; int entries, i, j; get_be32(pb); // version + flags entries = get_be32(pb); if (entries >= UINT_MAX / sizeof(*sc->drefs)) return -1; sc->drefs_count = entries; sc->drefs = av_mallocz(entries * sizeof(*sc->drefs)); for (i = 0; i < sc->drefs_count; i++) { MOV_dref_t *dref = &sc->drefs[i]; uint32_t size = get_be32(pb); offset_t next = url_ftell(pb) + size - 4; dref->type = get_le32(pb); get_be32(pb); // version + flags dprintf(c->fc, "type %.4s size %d\n", (char*)&dref->type, size); if (dref->type == MKTAG('a','l','i','s') && size > 150) { /* macintosh alias record */ uint16_t volume_len, len; char volume[28]; int16_t type; url_fskip(pb, 10); volume_len = get_byte(pb); volume_len = FFMIN(volume_len, 27);#ifndef __CW32__ get_buffer(pb, volume, 27);#else get_buffer(pb, (unsigned char*)volume, 27);#endif volume[volume_len] = 0; av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", volume, volume_len); url_fskip(pb, 112); for (type = 0; type != -1 && url_ftell(pb) < next; ) { type = get_be16(pb); len = get_be16(pb); av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len); if (len&1) len += 1; if (type == 2) { // absolute path av_free(dref->path); dref->path = av_mallocz(len+1); if (!dref->path) return AVERROR(ENOMEM);#ifndef __CW32__ get_buffer(pb, dref->path, len);#else get_buffer(pb, (unsigned char*)dref->path, len);#endif if (len > volume_len && !strncmp(dref->path, volume, volume_len)) { len -= volume_len; memmove(dref->path, dref->path+volume_len, len); dref->path[len] = 0; } for (j = 0; j < len; j++) if (dref->path[j] == ':') dref->path[j] = '/'; av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path); } else url_fskip(pb, len); } } url_fseek(pb, next, SEEK_SET); } return 0;}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_be24(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; } 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
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -