* Copyright (c) 2001 Fabrice Bellard
* Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
*
- * This file is part of FFmpeg.
+ * This file is part of Libav.
*
- * FFmpeg is free software; you can redistribute it and/or
+ * Libav 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,
+ * Libav 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
+ * License along with Libav; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "libavutil/intreadwrite.h"
#include "libavutil/avstring.h"
#include "avformat.h"
+#include "avio_internal.h"
#include "riff.h"
#include "isom.h"
-#include "libavcodec/mpeg4audio.h"
-#include "libavcodec/mpegaudiodata.h"
#include "libavcodec/get_bits.h"
#if CONFIG_ZLIB
/* links atom IDs to parse functions */
typedef struct MOVParseTableEntry {
uint32_t type;
- int (*parse)(MOVContext *ctx, ByteIOContext *pb, MOVAtom atom);
+ int (*parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom);
} MOVParseTableEntry;
static const MOVParseTableEntry mov_default_parse_table[];
-static int mov_metadata_trkn(MOVContext *c, ByteIOContext *pb, unsigned len)
+static int mov_metadata_trkn(MOVContext *c, AVIOContext *pb, unsigned len)
{
char buf[16];
- get_be16(pb); // unknown
- snprintf(buf, sizeof(buf), "%d", get_be16(pb));
- av_metadata_set(&c->fc->metadata, "track", buf);
+ avio_rb16(pb); // unknown
+ snprintf(buf, sizeof(buf), "%d", avio_rb16(pb));
+ av_metadata_set2(&c->fc->metadata, "track", buf, 0);
- get_be16(pb); // total tracks
+ avio_rb16(pb); // total tracks
return 0;
}
-static int mov_read_udta_string(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static const uint32_t mac_to_unicode[128] = {
+ 0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1,
+ 0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8,
+ 0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3,
+ 0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC,
+ 0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF,
+ 0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8,
+ 0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211,
+ 0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8,
+ 0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB,
+ 0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153,
+ 0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA,
+ 0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02,
+ 0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1,
+ 0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4,
+ 0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC,
+ 0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7,
+};
+
+static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len,
+ char *dst, int dstlen)
+{
+ char *p = dst;
+ char *end = dst+dstlen-1;
+ int i;
+
+ for (i = 0; i < len; i++) {
+ uint8_t t, c = avio_r8(pb);
+ if (c < 0x80 && p < end)
+ *p++ = c;
+ else
+ PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;);
+ }
+ *p = 0;
+ return p - dst;
+}
+
+static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
#ifdef MOV_EXPORT_ALL_METADATA
char tmp_key[5];
#endif
char str[1024], key2[16], language[4] = {0};
const char *key = NULL;
- uint16_t str_size;
- int (*parse)(MOVContext*, ByteIOContext*, unsigned) = NULL;
+ uint16_t str_size, langcode = 0;
+ uint32_t data_type = 0;
+ int (*parse)(MOVContext*, AVIOContext*, unsigned) = NULL;
switch (atom.type) {
case MKTAG(0xa9,'n','a','m'): key = "title"; break;
case MKTAG(0xa9,'a','u','t'):
- case MKTAG(0xa9,'A','R','T'):
- case MKTAG(0xa9,'w','r','t'): key = "author"; break;
+ case MKTAG(0xa9,'A','R','T'): key = "artist"; break;
+ case MKTAG(0xa9,'w','r','t'): key = "composer"; break;
+ case MKTAG( 'c','p','r','t'):
case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
case MKTAG(0xa9,'c','m','t'):
case MKTAG(0xa9,'i','n','f'): key = "comment"; break;
case MKTAG(0xa9,'a','l','b'): key = "album"; break;
- case MKTAG(0xa9,'d','a','y'): key = "year"; break;
+ case MKTAG(0xa9,'d','a','y'): key = "date"; break;
case MKTAG(0xa9,'g','e','n'): key = "genre"; break;
case MKTAG(0xa9,'t','o','o'):
- case MKTAG(0xa9,'e','n','c'): key = "muxer"; break;
+ case MKTAG(0xa9,'s','w','r'): key = "encoder"; break;
+ case MKTAG(0xa9,'e','n','c'): key = "encoder"; break;
+ case MKTAG( 'd','e','s','c'): key = "description";break;
+ case MKTAG( 'l','d','e','s'): key = "synopsis"; break;
+ case MKTAG( 't','v','s','h'): key = "show"; break;
+ case MKTAG( 't','v','e','n'): key = "episode_id";break;
+ case MKTAG( 't','v','n','n'): key = "network"; break;
case MKTAG( 't','r','k','n'): key = "track";
parse = mov_metadata_trkn; break;
}
if (c->itunes_metadata && atom.size > 8) {
- int data_size = get_be32(pb);
- int tag = get_le32(pb);
+ int data_size = avio_rb32(pb);
+ int tag = avio_rl32(pb);
if (tag == MKTAG('d','a','t','a')) {
- get_be32(pb); // type
- get_be32(pb); // unknown
+ data_type = avio_rb32(pb); // type
+ avio_rb32(pb); // unknown
str_size = data_size - 16;
atom.size -= 16;
} else return 0;
} else if (atom.size > 4 && key && !c->itunes_metadata) {
- str_size = get_be16(pb); // string length
- ff_mov_lang_to_iso639(get_be16(pb), language);
+ str_size = avio_rb16(pb); // string length
+ langcode = avio_rb16(pb);
+ ff_mov_lang_to_iso639(langcode, language);
atom.size -= 4;
} else
str_size = atom.size;
if (parse)
parse(c, pb, str_size);
else {
- get_buffer(pb, str, str_size);
- str[str_size] = 0;
- av_metadata_set(&c->fc->metadata, key, str);
+ if (data_type == 3 || (data_type == 0 && langcode < 0x800)) { // MAC Encoded
+ mov_read_mac_string(c, pb, str_size, str, sizeof(str));
+ } else {
+ avio_read(pb, str, str_size);
+ str[str_size] = 0;
+ }
+ av_metadata_set2(&c->fc->metadata, key, str, 0);
if (*language && strcmp(language, "und")) {
snprintf(key2, sizeof(key2), "%s-%s", key, language);
- av_metadata_set(&c->fc->metadata, key2, str);
+ av_metadata_set2(&c->fc->metadata, key2, str, 0);
}
}
#ifdef DEBUG_METADATA
return 0;
}
-static int mov_read_default(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
+{
+ int64_t start;
+ int i, nb_chapters, str_len, version;
+ char str[256+1];
+
+ if ((atom.size -= 5) < 0)
+ return 0;
+
+ version = avio_r8(pb);
+ avio_rb24(pb);
+ if (version)
+ avio_rb32(pb); // ???
+ nb_chapters = avio_r8(pb);
+
+ for (i = 0; i < nb_chapters; i++) {
+ if (atom.size < 9)
+ return 0;
+
+ start = avio_rb64(pb);
+ str_len = avio_r8(pb);
+
+ if ((atom.size -= 9+str_len) < 0)
+ return 0;
+
+ avio_read(pb, str, str_len);
+ str[str_len] = 0;
+ ff_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str);
+ }
+ return 0;
+}
+
+static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
int64_t total_size = 0;
MOVAtom 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) {
- int (*parse)(MOVContext*, ByteIOContext*, MOVAtom) = NULL;
+ while (total_size + 8 < atom.size && !pb->eof_reached) {
+ int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;
a.size = atom.size;
a.type=0;
if(atom.size >= 8) {
- a.size = get_be32(pb);
- a.type = get_le32(pb);
+ a.size = avio_rb32(pb);
+ a.type = avio_rl32(pb);
}
+ av_dlog(c->fc, "type: %08x '%.4s' parent:'%.4s' sz: %"PRId64" %"PRId64" %"PRId64"\n",
+ a.type, (char*)&a.type, (char*)&atom.type, a.size, total_size, atom.size);
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;
+ a.size = avio_rb64(pb) - 8;
total_size += 8;
}
if (a.size == 0) {
parse = mov_read_udta_string;
if (!parse) { /* skip leaf atoms data */
- url_fskip(pb, a.size);
+ avio_skip(pb, a.size);
} else {
- int64_t start_pos = url_ftell(pb);
+ int64_t start_pos = avio_tell(pb);
int64_t left;
- err = parse(c, pb, a);
- if (url_is_streamed(pb) && c->found_moov && c->found_mdat)
- break;
- left = a.size - url_ftell(pb) + start_pos;
+ int err = parse(c, pb, a);
+ if (err < 0)
+ return err;
+ if (c->found_moov && c->found_mdat &&
+ (url_is_streamed(pb) || start_pos + a.size == avio_size(pb)))
+ return 0;
+ left = a.size - avio_tell(pb) + start_pos;
if (left > 0) /* skip garbage at atom end */
- url_fskip(pb, left);
+ avio_skip(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);
+ if (total_size < atom.size && atom.size < 0x7ffff)
+ avio_skip(pb, atom.size - total_size);
- return err;
+ return 0;
}
-static int mov_read_dref(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
AVStream *st;
MOVStreamContext *sc;
st = c->fc->streams[c->fc->nb_streams-1];
sc = st->priv_data;
- get_be32(pb); // version + flags
- entries = get_be32(pb);
+ avio_rb32(pb); // version + flags
+ entries = avio_rb32(pb);
if (entries >= UINT_MAX / sizeof(*sc->drefs))
return -1;
sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
for (i = 0; i < sc->drefs_count; i++) {
MOVDref *dref = &sc->drefs[i];
- uint32_t size = get_be32(pb);
- int64_t next = url_ftell(pb) + size - 4;
+ uint32_t size = avio_rb32(pb);
+ int64_t next = avio_tell(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 (size < 12)
+ return -1;
+
+ dref->type = avio_rl32(pb);
+ avio_rb32(pb); // version + flags
+ av_dlog(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);
+ avio_skip(pb, 10);
- volume_len = get_byte(pb);
+ volume_len = avio_r8(pb);
volume_len = FFMIN(volume_len, 27);
- get_buffer(pb, volume, 27);
- volume[volume_len] = 0;
- av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", volume, volume_len);
+ avio_read(pb, dref->volume, 27);
+ dref->volume[volume_len] = 0;
+ av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len);
+
+ avio_skip(pb, 12);
- url_fskip(pb, 112);
+ len = avio_r8(pb);
+ len = FFMIN(len, 63);
+ avio_read(pb, dref->filename, 63);
+ dref->filename[len] = 0;
+ av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len);
- for (type = 0; type != -1 && url_ftell(pb) < next; ) {
- type = get_be16(pb);
- len = get_be16(pb);
+ avio_skip(pb, 16);
+
+ /* read next level up_from_alias/down_to_target */
+ dref->nlvl_from = avio_rb16(pb);
+ dref->nlvl_to = avio_rb16(pb);
+ av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n",
+ dref->nlvl_from, dref->nlvl_to);
+
+ avio_skip(pb, 16);
+
+ for (type = 0; type != -1 && avio_tell(pb) < next; ) {
+ type = avio_rb16(pb);
+ len = avio_rb16(pb);
av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
if (len&1)
len += 1;
dref->path = av_mallocz(len+1);
if (!dref->path)
return AVERROR(ENOMEM);
- get_buffer(pb, dref->path, len);
- if (len > volume_len && !strncmp(dref->path, volume, volume_len)) {
+ avio_read(pb, dref->path, len);
+ if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) {
len -= volume_len;
memmove(dref->path, dref->path+volume_len, len);
dref->path[len] = 0;
if (dref->path[j] == ':')
dref->path[j] = '/';
av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
+ } else if (type == 0) { // directory name
+ av_free(dref->dir);
+ dref->dir = av_malloc(len+1);
+ if (!dref->dir)
+ return AVERROR(ENOMEM);
+ avio_read(pb, dref->dir, len);
+ dref->dir[len] = 0;
+ for (j = 0; j < len; j++)
+ if (dref->dir[j] == ':')
+ dref->dir[j] = '/';
+ av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir);
} else
- url_fskip(pb, len);
+ avio_skip(pb, len);
}
}
- url_fseek(pb, next, SEEK_SET);
+ avio_seek(pb, next, SEEK_SET);
}
return 0;
}
-static int mov_read_hdlr(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
AVStream *st;
uint32_t type;
st = c->fc->streams[c->fc->nb_streams-1];
- get_byte(pb); /* version */
- get_be24(pb); /* flags */
+ avio_r8(pb); /* version */
+ avio_rb24(pb); /* flags */
/* component type */
- ctype = get_le32(pb);
- type = get_le32(pb); /* component subtype */
+ ctype = avio_rl32(pb);
+ type = avio_rl32(pb); /* component subtype */
- dprintf(c->fc, "ctype= %.4s (0x%08x)\n", (char*)&ctype, ctype);
- dprintf(c->fc, "stype= %.4s\n", (char*)&type);
+ av_dlog(c->fc, "ctype= %.4s (0x%08x)\n", (char*)&ctype, ctype);
+ av_dlog(c->fc, "stype= %.4s\n", (char*)&type);
if (type == MKTAG('v','i','d','e'))
- st->codec->codec_type = CODEC_TYPE_VIDEO;
+ st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
else if(type == MKTAG('s','o','u','n'))
- st->codec->codec_type = CODEC_TYPE_AUDIO;
+ st->codec->codec_type = AVMEDIA_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_type = AVMEDIA_TYPE_SUBTITLE;
- get_be32(pb); /* component manufacture */
- get_be32(pb); /* component flags */
- get_be32(pb); /* component flags mask */
+ avio_rb32(pb); /* component manufacture */
+ avio_rb32(pb); /* component flags */
+ avio_rb32(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;
}
-int ff_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;
-}
-
-int mp4_read_descr(AVFormatContext *fc, ByteIOContext *pb, int *tag)
-{
- int len;
- *tag = get_byte(pb);
- len = ff_mp4_read_descr_len(pb);
- dprintf(fc, "MPEG4 description: tag=0x%02x len=%d\n", *tag, len);
- return len;
-}
-
-#define MP4ESDescrTag 0x03
-#define MP4DecConfigDescrTag 0x04
-#define MP4DecSpecificDescrTag 0x05
-
-static const AVCodecTag mp4_audio_types[] = {
- { CODEC_ID_MP3ON4, AOT_PS }, /* old mp3on4 draft */
- { CODEC_ID_MP3ON4, AOT_L1 }, /* layer 1 */
- { CODEC_ID_MP3ON4, AOT_L2 }, /* layer 2 */
- { CODEC_ID_MP3ON4, AOT_L3 }, /* layer 3 */
- { CODEC_ID_MP4ALS, AOT_ALS }, /* MPEG-4 ALS */
- { CODEC_ID_NONE, AOT_NULL },
-};
-
-int ff_mov_read_esds(AVFormatContext *fc, ByteIOContext *pb, MOVAtom atom)
+int ff_mov_read_esds(AVFormatContext *fc, AVIOContext *pb, MOVAtom atom)
{
AVStream *st;
int tag, len;
return 0;
st = fc->streams[fc->nb_streams-1];
- get_be32(pb); /* version + flags */
- len = mp4_read_descr(fc, pb, &tag);
+ avio_rb32(pb); /* version + flags */
+ len = ff_mp4_read_descr(fc, pb, &tag);
if (tag == MP4ESDescrTag) {
- get_be16(pb); /* ID */
- get_byte(pb); /* priority */
+ avio_rb16(pb); /* ID */
+ avio_r8(pb); /* priority */
} else
- get_be16(pb); /* ID */
-
- len = mp4_read_descr(fc, 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= ff_codec_get_id(ff_mp4_obj_type, object_type_id);
- dprintf(fc, "esds object type id %d\n", object_type_id);
- len = mp4_read_descr(fc, pb, &tag);
- if (tag == MP4DecSpecificDescrTag) {
- dprintf(fc, "Specific MPEG4 header len=%d\n", len);
- if((uint64_t)len > (1<<30))
- return -1;
- st->codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
- if (!st->codec->extradata)
- return AVERROR(ENOMEM);
- get_buffer(pb, st->codec->extradata, len);
- st->codec->extradata_size = len;
- if (st->codec->codec_id == CODEC_ID_AAC) {
- MPEG4AudioConfig cfg;
- ff_mpeg4audio_get_config(&cfg, st->codec->extradata,
- st->codec->extradata_size);
- if (cfg.chan_config > 7)
- return -1;
- st->codec->channels = ff_mpeg4audio_channels[cfg.chan_config];
- if (cfg.object_type == 29 && cfg.sampling_index < 3) // old mp3on4
- st->codec->sample_rate = ff_mpa_freq_tab[cfg.sampling_index];
- else
- st->codec->sample_rate = cfg.sample_rate; // ext sample rate ?
- dprintf(fc, "mp4a config channels %d obj %d ext obj %d "
- "sample rate %d ext sample rate %d\n", st->codec->channels,
- cfg.object_type, cfg.ext_object_type,
- cfg.sample_rate, cfg.ext_sample_rate);
- if (!(st->codec->codec_id = ff_codec_get_id(mp4_audio_types,
- cfg.object_type)))
- st->codec->codec_id = CODEC_ID_AAC;
- }
- }
- }
+ avio_rb16(pb); /* ID */
+
+ len = ff_mp4_read_descr(fc, pb, &tag);
+ if (tag == MP4DecConfigDescrTag)
+ ff_mp4_read_dec_config_descr(fc, st, pb);
return 0;
}
-static int mov_read_esds(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_read_esds(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
return ff_mov_read_esds(c->fc, pb, atom);
}
-static int mov_read_pasp(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
+{
+ AVStream *st;
+ int ac3info, acmod, lfeon, bsmod;
+
+ if (c->fc->nb_streams < 1)
+ return 0;
+ st = c->fc->streams[c->fc->nb_streams-1];
+
+ ac3info = avio_rb24(pb);
+ bsmod = (ac3info >> 14) & 0x7;
+ acmod = (ac3info >> 11) & 0x7;
+ lfeon = (ac3info >> 10) & 0x1;
+ st->codec->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon;
+ st->codec->audio_service_type = bsmod;
+ if (st->codec->channels > 1 && bsmod == 0x7)
+ st->codec->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
+
+ return 0;
+}
+
+static int mov_read_pasp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
- const int num = get_be32(pb);
- const int den = get_be32(pb);
+ const int num = avio_rb32(pb);
+ const int den = avio_rb32(pb);
AVStream *st;
if (c->fc->nb_streams < 1)
return 0;
st = c->fc->streams[c->fc->nb_streams-1];
- if (den != 0) {
- if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default
- (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num))
- av_log(c->fc, AV_LOG_WARNING,
- "sample aspect ratio already set to %d:%d, overriding by 'pasp' atom\n",
- st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
+ if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default
+ (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num)) {
+ av_log(c->fc, AV_LOG_WARNING,
+ "sample aspect ratio already set to %d:%d, ignoring 'pasp' atom (%d:%d)\n",
+ st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
+ num, den);
+ } else if (den != 0) {
st->sample_aspect_ratio.num = num;
st->sample_aspect_ratio.den = den;
}
}
/* this atom contains actual media data */
-static int mov_read_mdat(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_read_mdat(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
if(atom.size == 0) /* wrong one (MP4) */
return 0;
}
/* read major brand, minor version and compatible brands and store them as metadata */
-static int mov_read_ftyp(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
uint32_t minor_ver;
int comp_brand_size;
char* comp_brands_str;
uint8_t type[5] = {0};
- get_buffer(pb, type, 4);
+ avio_read(pb, type, 4);
if (strcmp(type, "qt "))
c->isom = 1;
av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
- av_metadata_set(&c->fc->metadata, "major_brand", type);
- minor_ver = get_be32(pb); /* minor version */
+ av_metadata_set2(&c->fc->metadata, "major_brand", type, 0);
+ minor_ver = avio_rb32(pb); /* minor version */
snprintf(minor_ver_str, sizeof(minor_ver_str), "%d", minor_ver);
- av_metadata_set(&c->fc->metadata, "minor_version", minor_ver_str);
+ av_metadata_set2(&c->fc->metadata, "minor_version", minor_ver_str, 0);
comp_brand_size = atom.size - 8;
if (comp_brand_size < 0)
comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */
if (!comp_brands_str)
return AVERROR(ENOMEM);
- get_buffer(pb, comp_brands_str, comp_brand_size);
+ avio_read(pb, comp_brands_str, comp_brand_size);
comp_brands_str[comp_brand_size] = 0;
- av_metadata_set(&c->fc->metadata, "compatible_brands", comp_brands_str);
+ av_metadata_set2(&c->fc->metadata, "compatible_brands", comp_brands_str, 0);
av_freep(&comp_brands_str);
return 0;
}
/* this atom should contain all header atoms */
-static int mov_read_moov(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
if (mov_read_default(c, pb, atom) < 0)
return -1;
return 0; /* now go for mdat */
}
-static int mov_read_moof(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
- c->fragment.moof_offset = url_ftell(pb) - 8;
- dprintf(c->fc, "moof offset %llx\n", c->fragment.moof_offset);
+ c->fragment.moof_offset = avio_tell(pb) - 8;
+ av_dlog(c->fc, "moof offset %llx\n", c->fragment.moof_offset);
return mov_read_default(c, pb, atom);
}
-static int mov_read_mdhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static void mov_metadata_creation_time(AVMetadata **metadata, time_t time)
+{
+ char buffer[32];
+ if (time) {
+ struct tm *ptm;
+ time -= 2082844800; /* seconds between 1904-01-01 and Epoch */
+ ptm = gmtime(&time);
+ if (!ptm) return;
+ strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", ptm);
+ av_metadata_set2(metadata, "creation_time", buffer, 0);
+ }
+}
+
+static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
AVStream *st;
MOVStreamContext *sc;
int version;
char language[4] = {0};
unsigned lang;
+ time_t creation_time;
if (c->fc->nb_streams < 1)
return 0;
st = c->fc->streams[c->fc->nb_streams-1];
sc = st->priv_data;
- version = get_byte(pb);
+ version = avio_r8(pb);
if (version > 1)
return -1; /* unsupported */
- get_be24(pb); /* flags */
+ avio_rb24(pb); /* flags */
if (version == 1) {
- get_be64(pb);
- get_be64(pb);
+ creation_time = avio_rb64(pb);
+ avio_rb64(pb);
} else {
- get_be32(pb); /* creation time */
- get_be32(pb); /* modification time */
+ creation_time = avio_rb32(pb);
+ avio_rb32(pb); /* modification time */
}
+ mov_metadata_creation_time(&st->metadata, creation_time);
- sc->time_scale = get_be32(pb);
- st->duration = (version == 1) ? get_be64(pb) : get_be32(pb); /* duration */
+ sc->time_scale = avio_rb32(pb);
+ st->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
- lang = get_be16(pb); /* language */
+ lang = avio_rb16(pb); /* language */
if (ff_mov_lang_to_iso639(lang, language))
- av_metadata_set(&st->metadata, "language", language);
- get_be16(pb); /* quality */
+ av_metadata_set2(&st->metadata, "language", language, 0);
+ avio_rb16(pb); /* quality */
return 0;
}
-static int mov_read_mvhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
- int version = get_byte(pb); /* version */
- get_be24(pb); /* flags */
+ time_t creation_time;
+ int version = avio_r8(pb); /* version */
+ avio_rb24(pb); /* flags */
if (version == 1) {
- get_be64(pb);
- get_be64(pb);
+ creation_time = avio_rb64(pb);
+ avio_rb64(pb);
} else {
- get_be32(pb); /* creation time */
- get_be32(pb); /* modification time */
+ creation_time = avio_rb32(pb);
+ avio_rb32(pb); /* modification time */
}
- c->time_scale = get_be32(pb); /* time scale */
+ mov_metadata_creation_time(&c->fc->metadata, creation_time);
+ c->time_scale = avio_rb32(pb); /* time scale */
- dprintf(c->fc, "time scale = %i\n", c->time_scale);
+ av_dlog(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 */
+ c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
+ avio_rb32(pb); /* preferred scale */
- get_be16(pb); /* preferred volume */
+ avio_rb16(pb); /* preferred volume */
- url_fskip(pb, 10); /* reserved */
+ avio_skip(pb, 10); /* reserved */
- url_fskip(pb, 36); /* display matrix */
+ avio_skip(pb, 36); /* display matrix */
- get_be32(pb); /* preview time */
- get_be32(pb); /* preview duration */
- get_be32(pb); /* poster time */
- get_be32(pb); /* selection time */
- get_be32(pb); /* selection duration */
- get_be32(pb); /* current time */
- get_be32(pb); /* next track ID */
+ avio_rb32(pb); /* preview time */
+ avio_rb32(pb); /* preview duration */
+ avio_rb32(pb); /* poster time */
+ avio_rb32(pb); /* selection time */
+ avio_rb32(pb); /* selection duration */
+ avio_rb32(pb); /* current time */
+ avio_rb32(pb); /* next track ID */
return 0;
}
-static int mov_read_smi(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_read_smi(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
AVStream *st;
return AVERROR(ENOMEM);
st->codec->extradata_size = 0x5a + atom.size;
memcpy(st->codec->extradata, "SVQ3", 4); // fake
- get_buffer(pb, st->codec->extradata + 0x5a, atom.size);
- dprintf(c->fc, "Reading SMI %"PRId64" %s\n", atom.size, st->codec->extradata + 0x5a);
+ avio_read(pb, st->codec->extradata + 0x5a, atom.size);
+ av_dlog(c->fc, "Reading SMI %"PRId64" %s\n", atom.size, st->codec->extradata + 0x5a);
return 0;
}
-static int mov_read_enda(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
AVStream *st;
int little_endian;
return 0;
st = c->fc->streams[c->fc->nb_streams-1];
- little_endian = get_be16(pb);
- dprintf(c->fc, "enda %d\n", little_endian);
+ little_endian = avio_rb16(pb);
+ av_dlog(c->fc, "enda %d\n", little_endian);
if (little_endian == 1) {
switch (st->codec->codec_id) {
case CODEC_ID_PCM_S24BE:
}
/* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
-static int mov_read_extradata(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
AVStream *st;
uint64_t size;
st->codec->extradata_size= size - FF_INPUT_BUFFER_PADDING_SIZE;
AV_WB32( buf , atom.size + 8);
AV_WL32( buf + 4, atom.type);
- get_buffer(pb, buf + 8, atom.size);
+ avio_read(pb, buf + 8, atom.size);
return 0;
}
-static int mov_read_wave(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
AVStream *st;
if((uint64_t)atom.size > (1<<30))
return -1;
- if (st->codec->codec_id == CODEC_ID_QDM2) {
- // pass all frma atom to codec, needed at least for QDM2
+ if (st->codec->codec_id == CODEC_ID_QDM2 || st->codec->codec_id == CODEC_ID_QDMC) {
+ // pass all frma atom to codec, needed at least for QDMC and QDM2
av_free(st->codec->extradata);
st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
if (!st->codec->extradata)
return AVERROR(ENOMEM);
st->codec->extradata_size = atom.size;
- get_buffer(pb, st->codec->extradata, atom.size);
+ avio_read(pb, st->codec->extradata, atom.size);
} else if (atom.size > 8) { /* to read frma, esds atoms */
if (mov_read_default(c, pb, atom) < 0)
return -1;
} else
- url_fskip(pb, atom.size);
+ avio_skip(pb, atom.size);
return 0;
}
* This function reads atom content and puts data in extradata without tag
* nor size unlike mov_read_extradata.
*/
-static int mov_read_glbl(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
AVStream *st;
if (!st->codec->extradata)
return AVERROR(ENOMEM);
st->codec->extradata_size = atom.size;
- get_buffer(pb, st->codec->extradata, atom.size);
+ avio_read(pb, st->codec->extradata, atom.size);
+ return 0;
+}
+
+/**
+ * An strf atom is a BITMAPINFOHEADER struct. This struct is 40 bytes itself,
+ * but can have extradata appended at the end after the 40 bytes belonging
+ * to the struct.
+ */
+static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom)
+{
+ AVStream *st;
+
+ if (c->fc->nb_streams < 1)
+ return 0;
+ if (atom.size <= 40)
+ return 0;
+ st = c->fc->streams[c->fc->nb_streams-1];
+
+ if((uint64_t)atom.size > (1<<30))
+ return -1;
+
+ av_free(st->codec->extradata);
+ st->codec->extradata = av_mallocz(atom.size - 40 + FF_INPUT_BUFFER_PADDING_SIZE);
+ if (!st->codec->extradata)
+ return AVERROR(ENOMEM);
+ st->codec->extradata_size = atom.size - 40;
+ avio_skip(pb, 40);
+ avio_read(pb, st->codec->extradata, atom.size - 40);
return 0;
}
-static int mov_read_stco(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
AVStream *st;
MOVStreamContext *sc;
st = c->fc->streams[c->fc->nb_streams-1];
sc = st->priv_data;
- get_byte(pb); /* version */
- get_be24(pb); /* flags */
+ avio_r8(pb); /* version */
+ avio_rb24(pb); /* flags */
- entries = get_be32(pb);
+ entries = avio_rb32(pb);
if(entries >= UINT_MAX/sizeof(int64_t))
return -1;
if (atom.type == MKTAG('s','t','c','o'))
for(i=0; i<entries; i++)
- sc->chunk_offsets[i] = get_be32(pb);
+ sc->chunk_offsets[i] = avio_rb32(pb);
else if (atom.type == MKTAG('c','o','6','4'))
for(i=0; i<entries; i++)
- sc->chunk_offsets[i] = get_be64(pb);
+ sc->chunk_offsets[i] = avio_rb64(pb);
else
return -1;
return CODEC_ID_NONE;
}
-static int mov_read_stsd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
{
AVStream *st;
MOVStreamContext *sc;
- int j, entries, pseudo_stream_id;
+ int j, pseudo_stream_id;
if (c->fc->nb_streams < 1)
return 0;
st = c->fc->streams[c->fc->nb_streams-1];
sc = st->priv_data;
- get_byte(pb); /* version */
- get_be24(pb); /* flags */
-
- entries = get_be32(pb);
-
for(pseudo_stream_id=0; pseudo_stream_id<entries; pseudo_stream_id++) {
//Parsing Sample description table
enum CodecID id;
int dref_id = 1;
- MOVAtom a = { 0, 0, 0 };
- int64_t start_pos = url_ftell(pb);
- int size = get_be32(pb); /* size */
- uint32_t format = get_le32(pb); /* data format */
+ MOVAtom a = { AV_RL32("stsd") };
+ int64_t start_pos = avio_tell(pb);
+ int size = avio_rb32(pb); /* size */
+ uint32_t format = avio_rl32(pb); /* data format */
if (size >= 16) {
- get_be32(pb); /* reserved */
- get_be16(pb); /* reserved */
- dref_id = get_be16(pb);
+ avio_rb32(pb); /* reserved */
+ avio_rb16(pb); /* reserved */
+ dref_id = avio_rb16(pb);
}
if (st->codec->codec_tag &&
/* Multiple fourcc, we skip JPEG. This is not correct, we should
* export it as a separate AVStream but this needs a few changes
* in the MOV demuxer, patch welcome. */
+ multiple_stsd:
av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
- url_fskip(pb, size - (url_ftell(pb) - start_pos));
+ avio_skip(pb, size - (avio_tell(pb) - start_pos));
continue;
}
+ /* we cannot demux concatenated h264 streams because of different extradata */
+ if (st->codec->codec_tag && st->codec->codec_tag == AV_RL32("avc1"))
+ goto multiple_stsd;
sc->pseudo_stream_id = st->codec->codec_tag ? -1 : pseudo_stream_id;
sc->dref_id= dref_id;
st->codec->codec_tag = format;
id = ff_codec_get_id(codec_movaudio_tags, format);
if (id<=0 && ((format&0xFFFF) == 'm'+('s'<<8) || (format&0xFFFF) == 'T'+('S'<<8)))
- id = ff_codec_get_id(ff_codec_wav_tags, bswap_32(format)&0xFFFF);
+ id = ff_codec_get_id(ff_codec_wav_tags, av_bswap32(format)&0xFFFF);
- if (st->codec->codec_type != CODEC_TYPE_VIDEO && id > 0) {
- st->codec->codec_type = CODEC_TYPE_AUDIO;
- } else if (st->codec->codec_type != CODEC_TYPE_AUDIO && /* do not overwrite codec type */
+ if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) {
+ st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
+ } else if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO && /* do not overwrite codec type */
format && format != MKTAG('m','p','4','s')) { /* skip old asf mpeg4 tag */
id = ff_codec_get_id(codec_movvideo_tags, format);
if (id <= 0)
id = ff_codec_get_id(ff_codec_bmp_tags, format);
if (id > 0)
- st->codec->codec_type = CODEC_TYPE_VIDEO;
- else if(st->codec->codec_type == CODEC_TYPE_DATA){
+ st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
+ else if(st->codec->codec_type == AVMEDIA_TYPE_DATA){
id = ff_codec_get_id(ff_codec_movsubtitle_tags, format);
if(id > 0)
- st->codec->codec_type = CODEC_TYPE_SUBTITLE;
+ st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
}
}
- dprintf(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n", size,
+ av_dlog(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n", size,
(format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff,
(format >> 24) & 0xff, st->codec->codec_type);
- if(st->codec->codec_type==CODEC_TYPE_VIDEO) {
- uint8_t codec_name[32];
- unsigned int color_depth;
+ if(st->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
+ unsigned int color_depth, len;
int color_greyscale;
st->codec->codec_id = id;
- get_be16(pb); /* version */
- get_be16(pb); /* revision level */
- get_be32(pb); /* vendor */
- get_be32(pb); /* temporal quality */
- get_be32(pb); /* spatial quality */
-
- st->codec->width = get_be16(pb); /* width */
- st->codec->height = get_be16(pb); /* height */
-
- get_be32(pb); /* horiz resolution */
- get_be32(pb); /* vert resolution */
- get_be32(pb); /* data size, always 0 */
- get_be16(pb); /* frames per samples */
-
- get_buffer(pb, codec_name, 32); /* codec name, pascal string */
- if (codec_name[0] <= 31) {
- int i;
- int pos = 0;
- for (i = 0; i < codec_name[0] && pos < sizeof(st->codec->codec_name) - 3; i++) {
- uint8_t tmp;
- PUT_UTF8(codec_name[i+1], tmp, st->codec->codec_name[pos++] = tmp;)
- }
- st->codec->codec_name[pos] = 0;
- }
-
- st->codec->bits_per_coded_sample = get_be16(pb); /* depth */
- st->codec->color_table_id = get_be16(pb); /* colortable id */
- dprintf(c->fc, "depth %d, ctab id %d\n",
+ avio_rb16(pb); /* version */
+ avio_rb16(pb); /* revision level */
+ avio_rb32(pb); /* vendor */
+ avio_rb32(pb); /* temporal quality */
+ avio_rb32(pb); /* spatial quality */
+
+ st->codec->width = avio_rb16(pb); /* width */
+ st->codec->height = avio_rb16(pb); /* height */
+
+ avio_rb32(pb); /* horiz resolution */
+ avio_rb32(pb); /* vert resolution */
+ avio_rb32(pb); /* data size, always 0 */
+ avio_rb16(pb); /* frames per samples */
+
+ len = avio_r8(pb); /* codec name, pascal string */
+ if (len > 31)
+ len = 31;
+ mov_read_mac_string(c, pb, len, st->codec->codec_name, 32);
+ if (len < 31)
+ avio_skip(pb, 31 - len);
+ /* codec_tag YV12 triggers an UV swap in rawdec.c */
+ if (!memcmp(st->codec->codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25))
+ st->codec->codec_tag=MKTAG('I', '4', '2', '0');
+
+ st->codec->bits_per_coded_sample = avio_rb16(pb); /* depth */
+ st->codec->color_table_id = avio_rb16(pb); /* colortable id */
+ av_dlog(c->fc, "depth %d, ctab id %d\n",
st->codec->bits_per_coded_sample, st->codec->color_table_id);
/* figure out the palette situation */
color_depth = st->codec->bits_per_coded_sample & 0x1F;
}
} else {
/* load the palette from the file */
- color_start = get_be32(pb);
- color_count = get_be16(pb);
- color_end = get_be16(pb);
+ color_start = avio_rb32(pb);
+ color_count = avio_rb16(pb);
+ color_end = avio_rb16(pb);
if ((color_start <= 255) &&
(color_end <= 255)) {
for (j = color_start; j <= color_end; j++) {
/* each R, G, or B component is 16 bits;
* only use the top 8 bits; skip alpha bytes
* up front */
- get_byte(pb);
- get_byte(pb);
- r = get_byte(pb);
- get_byte(pb);
- g = get_byte(pb);
- get_byte(pb);
- b = get_byte(pb);
- get_byte(pb);
+ avio_r8(pb);
+ avio_r8(pb);
+ r = avio_r8(pb);
+ avio_r8(pb);
+ g = avio_r8(pb);
+ avio_r8(pb);
+ b = avio_r8(pb);
+ avio_r8(pb);
st->codec->palctrl->palette[j] =
(r << 16) | (g << 8) | (b);
}
}
st->codec->palctrl->palette_changed = 1;
}
- } else if(st->codec->codec_type==CODEC_TYPE_AUDIO) {
+ } else if(st->codec->codec_type==AVMEDIA_TYPE_AUDIO) {
int bits_per_sample, flags;
- uint16_t version = get_be16(pb);
+ uint16_t version = avio_rb16(pb);
st->codec->codec_id = id;
- get_be16(pb); /* revision level */
- get_be32(pb); /* vendor */
+ avio_rb16(pb); /* revision level */
+ avio_rb32(pb); /* vendor */
- st->codec->channels = get_be16(pb); /* channel count */
- dprintf(c->fc, "audio channels %d\n", st->codec->channels);
- st->codec->bits_per_coded_sample = get_be16(pb); /* sample size */
+ st->codec->channels = avio_rb16(pb); /* channel count */
+ av_dlog(c->fc, "audio channels %d\n", st->codec->channels);
+ st->codec->bits_per_coded_sample = avio_rb16(pb); /* sample size */
- sc->audio_cid = get_be16(pb);
- get_be16(pb); /* packet size = 0 */
+ sc->audio_cid = avio_rb16(pb);
+ avio_rb16(pb); /* packet size = 0 */
- st->codec->sample_rate = ((get_be32(pb) >> 16));
+ st->codec->sample_rate = ((avio_rb32(pb) >> 16));
//Read QT version 1 fields. In version 0 these do not exist.
- dprintf(c->fc, "version =%d, isom =%d\n",version,c->isom);
+ av_dlog(c->fc, "version =%d, isom =%d\n",version,c->isom);
if(!c->isom) {
if(version==1) {
- sc->samples_per_frame = get_be32(pb);
- get_be32(pb); /* bytes per packet */
- sc->bytes_per_frame = get_be32(pb);
- get_be32(pb); /* bytes per sample */
+ sc->samples_per_frame = avio_rb32(pb);
+ avio_rb32(pb); /* bytes per packet */
+ sc->bytes_per_frame = avio_rb32(pb);
+ avio_rb32(pb); /* bytes per sample */
} else if(version==2) {
- get_be32(pb); /* sizeof struct only */
- st->codec->sample_rate = av_int2dbl(get_be64(pb)); /* float 64 */
- st->codec->channels = get_be32(pb);
- get_be32(pb); /* always 0x7F000000 */
- st->codec->bits_per_coded_sample = get_be32(pb); /* bits per channel if sound is uncompressed */
- flags = get_be32(pb); /* lcpm format specific flag */
- sc->bytes_per_frame = get_be32(pb); /* bytes per audio packet if constant */
- sc->samples_per_frame = get_be32(pb); /* lpcm frames per audio packet if constant */
+ avio_rb32(pb); /* sizeof struct only */
+ st->codec->sample_rate = av_int2dbl(avio_rb64(pb)); /* float 64 */
+ st->codec->channels = avio_rb32(pb);
+ avio_rb32(pb); /* always 0x7F000000 */
+ st->codec->bits_per_coded_sample = avio_rb32(pb); /* bits per channel if sound is uncompressed */
+ flags = avio_rb32(pb); /* lpcm format specific flag */
+ sc->bytes_per_frame = avio_rb32(pb); /* bytes per audio packet if constant */
+ sc->samples_per_frame = avio_rb32(pb); /* lpcm frames per audio packet if constant */
if (format == MKTAG('l','p','c','m'))
st->codec->codec_id = ff_mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample, flags);
}
st->codec->bits_per_coded_sample = bits_per_sample;
sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
}
- } else if(st->codec->codec_type==CODEC_TYPE_SUBTITLE){
+ } else if(st->codec->codec_type==AVMEDIA_TYPE_SUBTITLE){
// ttxt stsd contains display flags, justification, background
// color, fonts, and default styles, so fake an atom to read it
- MOVAtom fake_atom = { .size = size - (url_ftell(pb) - start_pos) };
+ MOVAtom fake_atom = { .size = size - (avio_tell(pb) - start_pos) };
if (format != AV_RL32("mp4s")) // mp4s contains a regular esds atom
mov_read_glbl(c, pb, fake_atom);
st->codec->codec_id= id;
st->codec->height = sc->height;
} else {
/* other codec type, just skip (rtp, mp4s, tmcd ...) */
- url_fskip(pb, size - (url_ftell(pb) - start_pos));
+ avio_skip(pb, size - (avio_tell(pb) - start_pos));
}
/* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */
- a.size = size - (url_ftell(pb) - start_pos);
+ a.size = size - (avio_tell(pb) - start_pos);
if (a.size > 8) {
if (mov_read_default(c, pb, a) < 0)
return -1;
} else if (a.size > 0)
- url_fskip(pb, a.size);
+ avio_skip(pb, a.size);
}
- if(st->codec->codec_type==CODEC_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1)
+ if(st->codec->codec_type==AVMEDIA_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1)
st->codec->sample_rate= sc->time_scale;
/* special codec parameters handling */
break;
case CODEC_ID_MP2:
case CODEC_ID_MP3:
- st->codec->codec_type = CODEC_TYPE_AUDIO; /* force type after stsd for m1a hdlr */
+ st->codec->codec_type = AVMEDIA_TYPE_AUDIO; /* force type after stsd for m1a hdlr */
st->need_parsing = AVSTREAM_PARSE_FULL;
break;
case CODEC_ID_GSM:
case CODEC_ID_ADPCM_MS:
case CODEC_ID_ADPCM_IMA_WAV:
+ st->codec->frame_size = sc->samples_per_frame;
st->codec->block_align = sc->bytes_per_frame;
break;
case CODEC_ID_ALAC:
if (st->codec->extradata_size == 36) {
st->codec->frame_size = AV_RB32(st->codec->extradata+12);
st->codec->channels = AV_RB8 (st->codec->extradata+21);
+ st->codec->sample_rate = AV_RB32(st->codec->extradata+32);
}
break;
default:
return 0;
}
-static int mov_read_stsc(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
+{
+ int entries;
+
+ avio_r8(pb); /* version */
+ avio_rb24(pb); /* flags */
+ entries = avio_rb32(pb);
+
+ return ff_mov_read_stsd_entries(c, pb, entries);
+}
+
+static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
AVStream *st;
MOVStreamContext *sc;
st = c->fc->streams[c->fc->nb_streams-1];
sc = st->priv_data;
- get_byte(pb); /* version */
- get_be24(pb); /* flags */
+ avio_r8(pb); /* version */
+ avio_rb24(pb); /* flags */
- entries = get_be32(pb);
+ entries = avio_rb32(pb);
- dprintf(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
+ av_dlog(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
if(entries >= UINT_MAX / sizeof(*sc->stsc_data))
return -1;
sc->stsc_count = entries;
for(i=0; i<entries; i++) {
- sc->stsc_data[i].first = get_be32(pb);
- sc->stsc_data[i].count = get_be32(pb);
- sc->stsc_data[i].id = get_be32(pb);
+ sc->stsc_data[i].first = avio_rb32(pb);
+ sc->stsc_data[i].count = avio_rb32(pb);
+ sc->stsc_data[i].id = avio_rb32(pb);
}
return 0;
}
-static int mov_read_stps(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
AVStream *st;
MOVStreamContext *sc;
st = c->fc->streams[c->fc->nb_streams-1];
sc = st->priv_data;
- get_be32(pb); // version + flags
+ avio_rb32(pb); // version + flags
- entries = get_be32(pb);
+ entries = avio_rb32(pb);
if (entries >= UINT_MAX / sizeof(*sc->stps_data))
return -1;
sc->stps_data = av_malloc(entries * sizeof(*sc->stps_data));
sc->stps_count = entries;
for (i = 0; i < entries; i++) {
- sc->stps_data[i] = get_be32(pb);
- //dprintf(c->fc, "stps %d\n", sc->stps_data[i]);
+ sc->stps_data[i] = avio_rb32(pb);
+ //av_dlog(c->fc, "stps %d\n", sc->stps_data[i]);
}
return 0;
}
-static int mov_read_stss(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
AVStream *st;
MOVStreamContext *sc;
st = c->fc->streams[c->fc->nb_streams-1];
sc = st->priv_data;
- get_byte(pb); /* version */
- get_be24(pb); /* flags */
+ avio_r8(pb); /* version */
+ avio_rb24(pb); /* flags */
- entries = get_be32(pb);
+ entries = avio_rb32(pb);
- dprintf(c->fc, "keyframe_count = %d\n", entries);
+ av_dlog(c->fc, "keyframe_count = %d\n", entries);
if(entries >= UINT_MAX / sizeof(int))
return -1;
sc->keyframe_count = entries;
for(i=0; i<entries; i++) {
- sc->keyframes[i] = get_be32(pb);
- //dprintf(c->fc, "keyframes[]=%d\n", sc->keyframes[i]);
+ sc->keyframes[i] = avio_rb32(pb);
+ //av_dlog(c->fc, "keyframes[]=%d\n", sc->keyframes[i]);
}
return 0;
}
-static int mov_read_stsz(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
AVStream *st;
MOVStreamContext *sc;
st = c->fc->streams[c->fc->nb_streams-1];
sc = st->priv_data;
- get_byte(pb); /* version */
- get_be24(pb); /* flags */
+ avio_r8(pb); /* version */
+ avio_rb24(pb); /* flags */
if (atom.type == MKTAG('s','t','s','z')) {
- sample_size = get_be32(pb);
+ sample_size = avio_rb32(pb);
if (!sc->sample_size) /* do not overwrite value computed in stsd */
sc->sample_size = sample_size;
field_size = 32;
} else {
sample_size = 0;
- get_be24(pb); /* reserved */
- field_size = get_byte(pb);
+ avio_rb24(pb); /* reserved */
+ field_size = avio_r8(pb);
}
- entries = get_be32(pb);
+ entries = avio_rb32(pb);
- dprintf(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, entries);
+ av_dlog(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, entries);
sc->sample_count = entries;
if (sample_size)
return AVERROR(ENOMEM);
}
- if (get_buffer(pb, buf, num_bytes) < num_bytes) {
+ if (avio_read(pb, buf, num_bytes) < num_bytes) {
av_freep(&sc->sample_sizes);
av_free(buf);
return -1;
return 0;
}
-static int mov_read_stts(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
AVStream *st;
MOVStreamContext *sc;
st = c->fc->streams[c->fc->nb_streams-1];
sc = st->priv_data;
- get_byte(pb); /* version */
- get_be24(pb); /* flags */
- entries = get_be32(pb);
+ avio_r8(pb); /* version */
+ avio_rb24(pb); /* flags */
+ entries = avio_rb32(pb);
- dprintf(c->fc, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
+ av_dlog(c->fc, "track[%i].stts.entries = %i\n", c->fc->nb_streams-1, entries);
if(entries >= UINT_MAX / sizeof(*sc->stts_data))
return -1;
int sample_duration;
int sample_count;
- sample_count=get_be32(pb);
- sample_duration = get_be32(pb);
+ sample_count=avio_rb32(pb);
+ sample_duration = avio_rb32(pb);
sc->stts_data[i].count= sample_count;
sc->stts_data[i].duration= sample_duration;
- dprintf(c->fc, "sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
+ av_dlog(c->fc, "sample_count=%d, sample_duration=%d\n",sample_count,sample_duration);
duration+=(int64_t)sample_duration*sample_count;
total_sample_count+=sample_count;
return 0;
}
-static int mov_read_cslg(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
-{
- AVStream *st;
- MOVStreamContext *sc;
-
- if (c->fc->nb_streams < 1)
- return 0;
- st = c->fc->streams[c->fc->nb_streams-1];
- sc = st->priv_data;
-
- get_be32(pb); // version + flags
-
- sc->dts_shift = get_be32(pb);
- dprintf(c->fc, "dts shift %d\n", sc->dts_shift);
-
- get_be32(pb); // least dts to pts delta
- get_be32(pb); // greatest dts to pts delta
- get_be32(pb); // pts start
- get_be32(pb); // pts end
-
- return 0;
-}
-
-static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
AVStream *st;
MOVStreamContext *sc;
st = c->fc->streams[c->fc->nb_streams-1];
sc = st->priv_data;
- get_byte(pb); /* version */
- get_be24(pb); /* flags */
- entries = get_be32(pb);
+ avio_r8(pb); /* version */
+ avio_rb24(pb); /* flags */
+ entries = avio_rb32(pb);
- dprintf(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
+ av_dlog(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
if(entries >= UINT_MAX / sizeof(*sc->ctts_data))
return -1;
sc->ctts_count = entries;
for(i=0; i<entries; i++) {
- int count =get_be32(pb);
- int duration =get_be32(pb);
+ int count =avio_rb32(pb);
+ int duration =avio_rb32(pb);
sc->ctts_data[i].count = count;
sc->ctts_data[i].duration= duration;
+ if (duration < 0)
+ sc->dts_shift = FFMAX(sc->dts_shift, -duration);
}
+
+ av_dlog(c->fc, "dts shift %d\n", sc->dts_shift);
+
return 0;
}
unsigned int stss_index = 0;
unsigned int stps_index = 0;
unsigned int i, j;
+ uint64_t stream_size = 0;
/* adjust first dts according to edit list */
- if (sc->time_offset) {
+ if (sc->time_offset && mov->time_scale > 0) {
int rescaled = sc->time_offset < 0 ? av_rescale(sc->time_offset, sc->time_scale, mov->time_scale) : sc->time_offset;
current_dts = -rescaled;
- if (sc->ctts_data && sc->ctts_data[0].duration / sc->stts_data[0].duration > 16) {
+ if (sc->ctts_data && sc->stts_data &&
+ sc->ctts_data[0].duration / sc->stts_data[0].duration > 16) {
/* more than 16 frames delay, dts are likely wrong
this happens with files created by iMovie */
sc->wrong_dts = 1;
}
/* only use old uncompressed audio chunk demuxing when stts specifies it */
- if (!(st->codec->codec_type == CODEC_TYPE_AUDIO &&
+ if (!(st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
unsigned int current_sample = 0;
unsigned int stts_sample = 0;
current_dts -= sc->dts_shift;
- st->nb_frames = sc->sample_count;
+ if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries))
+ return;
+ st->index_entries = av_malloc(sc->sample_count*sizeof(*st->index_entries));
+ if (!st->index_entries)
+ return;
+ st->index_entries_allocated_size = sc->sample_count*sizeof(*st->index_entries);
+
for (i = 0; i < sc->chunk_count; i++) {
current_offset = sc->chunk_offsets[i];
if (stsc_index + 1 < sc->stsc_count &&
sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample];
if(sc->pseudo_stream_id == -1 ||
sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
- av_add_index_entry(st, current_offset, current_dts, sample_size, distance,
- keyframe ? AVINDEX_KEYFRAME : 0);
- dprintf(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
+ AVIndexEntry *e = &st->index_entries[st->nb_index_entries++];
+ e->pos = current_offset;
+ e->timestamp = current_dts;
+ e->size = sample_size;
+ e->min_distance = distance;
+ e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
+ av_dlog(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
"size %d, distance %d, keyframe %d\n", st->index, current_sample,
current_offset, current_dts, sample_size, distance, keyframe);
}
current_offset += sample_size;
+ stream_size += sample_size;
current_dts += sc->stts_data[stts_index].duration;
distance++;
stts_sample++;
}
}
}
+ if (st->duration > 0)
+ st->codec->bit_rate = stream_size*8*sc->time_scale/st->duration;
} else {
- for (i = 0; i < sc->chunk_count; i++) {
- unsigned chunk_samples;
+ unsigned chunk_samples, total = 0;
- current_offset = sc->chunk_offsets[i];
- if (stsc_index + 1 < sc->stsc_count &&
- i + 1 == sc->stsc_data[stsc_index + 1].first)
- stsc_index++;
- chunk_samples = sc->stsc_data[stsc_index].count;
+ // compute total chunk count
+ for (i = 0; i < sc->stsc_count; i++) {
+ unsigned count, chunk_count;
+ chunk_samples = sc->stsc_data[i].count;
if (sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
return;
}
+ if (sc->samples_per_frame >= 160) { // gsm
+ count = chunk_samples / sc->samples_per_frame;
+ } else if (sc->samples_per_frame > 1) {
+ unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
+ count = (chunk_samples+samples-1) / samples;
+ } else {
+ count = (chunk_samples+1023) / 1024;
+ }
+
+ if (i < sc->stsc_count - 1)
+ chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
+ else
+ chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
+ total += chunk_count * count;
+ }
+
+ av_dlog(mov->fc, "chunk count %d\n", total);
+ if (total >= UINT_MAX / sizeof(*st->index_entries))
+ return;
+ st->index_entries = av_malloc(total*sizeof(*st->index_entries));
+ if (!st->index_entries)
+ return;
+ st->index_entries_allocated_size = total*sizeof(*st->index_entries);
+
+ // populate index
+ for (i = 0; i < sc->chunk_count; i++) {
+ current_offset = sc->chunk_offsets[i];
+ if (stsc_index + 1 < sc->stsc_count &&
+ i + 1 == sc->stsc_data[stsc_index + 1].first)
+ stsc_index++;
+ chunk_samples = sc->stsc_data[stsc_index].count;
+
while (chunk_samples > 0) {
+ AVIndexEntry *e;
unsigned size, samples;
if (sc->samples_per_frame >= 160) { // gsm
}
}
- av_add_index_entry(st, current_offset, current_dts, size, 0, AVINDEX_KEYFRAME);
- dprintf(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
+ if (st->nb_index_entries >= total) {
+ av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %d\n", total);
+ return;
+ }
+ e = &st->index_entries[st->nb_index_entries++];
+ e->pos = current_offset;
+ e->timestamp = current_dts;
+ e->size = size;
+ e->min_distance = 0;
+ e->flags = AVINDEX_KEYFRAME;
+ av_dlog(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
"size %d, duration %d\n", st->index, i, current_offset, current_dts,
size, samples);
}
}
-static int mov_read_trak(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_open_dref(AVIOContext **pb, char *src, MOVDref *ref)
+{
+ /* try relative path, we do not try the absolute because it can leak information about our
+ system to an attacker */
+ if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
+ char filename[1024];
+ char *src_path;
+ int i, l;
+
+ /* find a source dir */
+ src_path = strrchr(src, '/');
+ if (src_path)
+ src_path++;
+ else
+ src_path = src;
+
+ /* find a next level down to target */
+ for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
+ if (ref->path[l] == '/') {
+ if (i == ref->nlvl_to - 1)
+ break;
+ else
+ i++;
+ }
+
+ /* compose filename if next level down to target was found */
+ if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) {
+ memcpy(filename, src, src_path - src);
+ filename[src_path - src] = 0;
+
+ for (i = 1; i < ref->nlvl_from; i++)
+ av_strlcat(filename, "../", 1024);
+
+ av_strlcat(filename, ref->path + l + 1, 1024);
+
+ if (!avio_open(pb, filename, URL_RDONLY))
+ return 0;
+ }
+ }
+
+ return AVERROR(ENOENT);
+}
+
+static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
AVStream *st;
MOVStreamContext *sc;
if (!sc) return AVERROR(ENOMEM);
st->priv_data = sc;
- st->codec->codec_type = CODEC_TYPE_DATA;
+ st->codec->codec_type = AVMEDIA_TYPE_DATA;
sc->ffindex = st->index;
if ((ret = mov_read_default(c, pb, atom)) < 0)
return 0;
}
- if (!sc->time_scale)
+ if (sc->time_scale <= 0) {
+ av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", st->index);
sc->time_scale = c->time_scale;
+ if (sc->time_scale <= 0)
+ sc->time_scale = 1;
+ }
av_set_pts_info(st, 64, 1, sc->time_scale);
- if (st->codec->codec_type == CODEC_TYPE_AUDIO &&
+ if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
!st->codec->frame_size && sc->stts_count == 1) {
st->codec->frame_size = av_rescale(sc->stts_data[0].duration,
st->codec->sample_rate, sc->time_scale);
- dprintf(c->fc, "frame size %d\n", st->codec->frame_size);
+ av_dlog(c->fc, "frame size %d\n", st->codec->frame_size);
}
mov_build_index(c, st);
if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
- if (url_fopen(&sc->pb, sc->drefs[sc->dref_id-1].path, URL_RDONLY) < 0)
- av_log(c->fc, AV_LOG_ERROR, "stream %d, error opening file %s: %s\n",
- st->index, sc->drefs[sc->dref_id-1].path, strerror(errno));
+ MOVDref *dref = &sc->drefs[sc->dref_id - 1];
+ if (mov_open_dref(&sc->pb, c->fc->filename, dref) < 0)
+ av_log(c->fc, AV_LOG_ERROR,
+ "stream %d, error opening alias: path='%s', dir='%s', "
+ "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
+ st->index, dref->path, dref->dir, dref->filename,
+ dref->volume, dref->nlvl_from, dref->nlvl_to);
} else
sc->pb = c->fc->pb;
+ if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
+ if (!st->sample_aspect_ratio.num &&
+ (st->codec->width != sc->width || st->codec->height != sc->height)) {
+ st->sample_aspect_ratio = av_d2q(((double)st->codec->height * sc->width) /
+ ((double)st->codec->width * sc->height), INT_MAX);
+ }
+
+ av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
+ sc->time_scale*st->nb_frames, st->duration, INT_MAX);
+
+ if (sc->stts_count == 1 || (sc->stts_count == 2 && sc->stts_data[1].count == 1))
+ av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den,
+ sc->time_scale, sc->stts_data[0].duration, INT_MAX);
+ }
+
switch (st->codec->codec_id) {
#if CONFIG_H261_DECODER
case CODEC_ID_H261:
return 0;
}
-static int mov_read_ilst(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
int ret;
c->itunes_metadata = 1;
return ret;
}
-static int mov_read_meta(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
while (atom.size > 8) {
- uint32_t tag = get_le32(pb);
+ uint32_t tag = avio_rl32(pb);
atom.size -= 4;
if (tag == MKTAG('h','d','l','r')) {
- url_fseek(pb, -8, SEEK_CUR);
+ avio_seek(pb, -8, SEEK_CUR);
atom.size += 8;
return mov_read_default(c, pb, atom);
}
return 0;
}
-static int mov_read_tkhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
int i;
int width;
st = c->fc->streams[c->fc->nb_streams-1];
sc = st->priv_data;
- version = get_byte(pb);
- get_be24(pb); /* flags */
+ version = avio_r8(pb);
+ avio_rb24(pb); /* flags */
/*
MOV_TRACK_ENABLED 0x0001
MOV_TRACK_IN_MOVIE 0x0002
*/
if (version == 1) {
- get_be64(pb);
- get_be64(pb);
+ avio_rb64(pb);
+ avio_rb64(pb);
} else {
- get_be32(pb); /* creation time */
- get_be32(pb); /* modification time */
+ avio_rb32(pb); /* creation time */
+ avio_rb32(pb); /* modification time */
}
- st->id = (int)get_be32(pb); /* track id (NOT 0 !)*/
- get_be32(pb); /* reserved */
+ st->id = (int)avio_rb32(pb); /* track id (NOT 0 !)*/
+ avio_rb32(pb); /* reserved */
/* highlevel (considering edits) duration in movie timebase */
- (version == 1) ? get_be64(pb) : get_be32(pb);
- get_be32(pb); /* reserved */
- get_be32(pb); /* reserved */
+ (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
+ avio_rb32(pb); /* reserved */
+ avio_rb32(pb); /* reserved */
- get_be16(pb); /* layer */
- get_be16(pb); /* alternate group */
- get_be16(pb); /* volume */
- get_be16(pb); /* reserved */
+ avio_rb16(pb); /* layer */
+ avio_rb16(pb); /* alternate group */
+ avio_rb16(pb); /* volume */
+ avio_rb16(pb); /* reserved */
//read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
// they're kept in fixed point format through all calculations
// ignore u,v,z b/c we don't need the scale factor to calc aspect ratio
for (i = 0; i < 3; i++) {
- display_matrix[i][0] = get_be32(pb); // 16.16 fixed point
- display_matrix[i][1] = get_be32(pb); // 16.16 fixed point
- get_be32(pb); // 2.30 fixed point (not used)
+ display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
+ display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
+ avio_rb32(pb); // 2.30 fixed point (not used)
}
- width = get_be32(pb); // 16.16 fixed point track width
- height = get_be32(pb); // 16.16 fixed point track height
+ width = avio_rb32(pb); // 16.16 fixed point track width
+ height = avio_rb32(pb); // 16.16 fixed point track height
sc->width = width >> 16;
sc->height = height >> 16;
return 0;
}
-static int mov_read_tfhd(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
MOVFragment *frag = &c->fragment;
MOVTrackExt *trex = NULL;
int flags, track_id, i;
- get_byte(pb); /* version */
- flags = get_be24(pb);
+ avio_r8(pb); /* version */
+ flags = avio_rb24(pb);
- track_id = get_be32(pb);
+ track_id = avio_rb32(pb);
if (!track_id)
return -1;
frag->track_id = track_id;
return -1;
}
- if (flags & 0x01) frag->base_data_offset = get_be64(pb);
+ if (flags & 0x01) frag->base_data_offset = avio_rb64(pb);
else frag->base_data_offset = frag->moof_offset;
- if (flags & 0x02) frag->stsd_id = get_be32(pb);
+ if (flags & 0x02) frag->stsd_id = avio_rb32(pb);
else frag->stsd_id = trex->stsd_id;
- frag->duration = flags & 0x08 ? get_be32(pb) : trex->duration;
- frag->size = flags & 0x10 ? get_be32(pb) : trex->size;
- frag->flags = flags & 0x20 ? get_be32(pb) : trex->flags;
- dprintf(c->fc, "frag flags 0x%x\n", frag->flags);
+ frag->duration = flags & 0x08 ? avio_rb32(pb) : trex->duration;
+ frag->size = flags & 0x10 ? avio_rb32(pb) : trex->size;
+ frag->flags = flags & 0x20 ? avio_rb32(pb) : trex->flags;
+ av_dlog(c->fc, "frag flags 0x%x\n", frag->flags);
+ return 0;
+}
+
+static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom)
+{
+ c->chapter_track = avio_rb32(pb);
return 0;
}
-static int mov_read_trex(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
MOVTrackExt *trex;
return AVERROR(ENOMEM);
c->trex_data = trex;
trex = &c->trex_data[c->trex_count++];
- get_byte(pb); /* version */
- get_be24(pb); /* flags */
- trex->track_id = get_be32(pb);
- trex->stsd_id = get_be32(pb);
- trex->duration = get_be32(pb);
- trex->size = get_be32(pb);
- trex->flags = get_be32(pb);
+ avio_r8(pb); /* version */
+ avio_rb24(pb); /* flags */
+ trex->track_id = avio_rb32(pb);
+ trex->stsd_id = avio_rb32(pb);
+ trex->duration = avio_rb32(pb);
+ trex->size = avio_rb32(pb);
+ trex->flags = avio_rb32(pb);
return 0;
}
-static int mov_read_trun(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
MOVFragment *frag = &c->fragment;
AVStream *st = NULL;
sc = st->priv_data;
if (sc->pseudo_stream_id+1 != frag->stsd_id)
return 0;
- get_byte(pb); /* version */
- flags = get_be24(pb);
- entries = get_be32(pb);
- dprintf(c->fc, "flags 0x%x entries %d\n", flags, entries);
- if (flags & 0x001) data_offset = get_be32(pb);
- if (flags & 0x004) first_sample_flags = get_be32(pb);
+ avio_r8(pb); /* version */
+ flags = avio_rb24(pb);
+ entries = avio_rb32(pb);
+ av_dlog(c->fc, "flags 0x%x entries %d\n", flags, entries);
+ if (flags & 0x001) data_offset = avio_rb32(pb);
+ if (flags & 0x004) first_sample_flags = avio_rb32(pb);
if (flags & 0x800) {
MOVStts *ctts_data;
if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
dts = st->duration;
offset = frag->base_data_offset + data_offset;
distance = 0;
- dprintf(c->fc, "first sample flags 0x%x\n", first_sample_flags);
+ av_dlog(c->fc, "first sample flags 0x%x\n", first_sample_flags);
for (i = 0; i < entries; i++) {
unsigned sample_size = frag->size;
int sample_flags = i ? frag->flags : first_sample_flags;
unsigned sample_duration = frag->duration;
int keyframe;
- if (flags & 0x100) sample_duration = get_be32(pb);
- if (flags & 0x200) sample_size = get_be32(pb);
- if (flags & 0x400) sample_flags = get_be32(pb);
+ if (flags & 0x100) sample_duration = avio_rb32(pb);
+ if (flags & 0x200) sample_size = avio_rb32(pb);
+ if (flags & 0x400) sample_flags = avio_rb32(pb);
if (flags & 0x800) {
sc->ctts_data[sc->ctts_count].count = 1;
- sc->ctts_data[sc->ctts_count].duration = get_be32(pb);
+ sc->ctts_data[sc->ctts_count].duration = avio_rb32(pb);
sc->ctts_count++;
}
- if ((keyframe = st->codec->codec_type == CODEC_TYPE_AUDIO ||
+ if ((keyframe = st->codec->codec_type == AVMEDIA_TYPE_AUDIO ||
(flags & 0x004 && !i && !sample_flags) || sample_flags & 0x2000000))
distance = 0;
av_add_index_entry(st, offset, dts, sample_size, distance,
keyframe ? AVINDEX_KEYFRAME : 0);
- dprintf(c->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
+ av_dlog(c->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
"size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
offset, dts, sample_size, distance, keyframe);
distance++;
/* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
/* like the files created with Adobe Premiere 5.0, for samples see */
/* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
-static int mov_read_wide(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
int err;
if (atom.size < 8)
return 0; /* continue */
- if (get_be32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
- url_fskip(pb, atom.size - 4);
+ if (avio_rb32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
+ avio_skip(pb, atom.size - 4);
return 0;
}
- atom.type = get_le32(pb);
- atom.offset += 8;
+ atom.type = avio_rl32(pb);
atom.size -= 8;
if (atom.type != MKTAG('m','d','a','t')) {
- url_fskip(pb, atom.size);
+ avio_skip(pb, atom.size);
return 0;
}
err = mov_read_mdat(c, pb, atom);
return err;
}
-static int mov_read_cmov(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
#if CONFIG_ZLIB
- ByteIOContext ctx;
+ AVIOContext ctx;
uint8_t *cmov_data;
uint8_t *moov_data; /* uncompressed data */
long cmov_len, moov_len;
int ret = -1;
- get_be32(pb); /* dcom atom */
- if (get_le32(pb) != MKTAG('d','c','o','m'))
+ avio_rb32(pb); /* dcom atom */
+ if (avio_rl32(pb) != MKTAG('d','c','o','m'))
return -1;
- if (get_le32(pb) != MKTAG('z','l','i','b')) {
+ if (avio_rl32(pb) != MKTAG('z','l','i','b')) {
av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !");
return -1;
}
- get_be32(pb); /* cmvd atom */
- if (get_le32(pb) != MKTAG('c','m','v','d'))
+ avio_rb32(pb); /* cmvd atom */
+ if (avio_rl32(pb) != MKTAG('c','m','v','d'))
return -1;
- moov_len = get_be32(pb); /* uncompressed size */
+ moov_len = avio_rb32(pb); /* uncompressed size */
cmov_len = atom.size - 6 * 4;
cmov_data = av_malloc(cmov_len);
av_free(cmov_data);
return AVERROR(ENOMEM);
}
- get_buffer(pb, cmov_data, cmov_len);
+ avio_read(pb, cmov_data, cmov_len);
if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
goto free_and_return;
- if(init_put_byte(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
+ if(ffio_init_context(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
goto free_and_return;
atom.type = MKTAG('m','o','o','v');
- atom.offset = 0;
atom.size = moov_len;
#ifdef DEBUG
// { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); }
}
/* edit list atom */
-static int mov_read_elst(MOVContext *c, ByteIOContext *pb, MOVAtom atom)
+static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
MOVStreamContext *sc;
int i, edit_count;
return 0;
sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
- get_byte(pb); /* version */
- get_be24(pb); /* flags */
- edit_count = get_be32(pb); /* entries */
+ avio_r8(pb); /* version */
+ avio_rb24(pb); /* flags */
+ edit_count = avio_rb32(pb); /* entries */
if((uint64_t)edit_count*12+8 > atom.size)
return -1;
for(i=0; i<edit_count; i++){
int time;
- int duration = get_be32(pb); /* Track duration */
- time = get_be32(pb); /* Media time */
- get_be32(pb); /* Media rate */
+ int duration = avio_rb32(pb); /* Track duration */
+ time = avio_rb32(pb); /* Media time */
+ avio_rb32(pb); /* Media rate */
if (i == 0 && time >= -1) {
sc->time_offset = time != -1 ? time : -duration;
}
av_log(c->fc, AV_LOG_WARNING, "multiple edit list entries, "
"a/v desync might occur, patch welcome\n");
- dprintf(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count);
+ av_dlog(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count);
return 0;
}
static const MOVParseTableEntry mov_default_parse_table[] = {
{ MKTAG('a','v','s','s'), mov_read_extradata },
+{ MKTAG('c','h','p','l'), mov_read_chpl },
{ MKTAG('c','o','6','4'), mov_read_stco },
-{ MKTAG('c','s','l','g'), mov_read_cslg },
{ MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
{ MKTAG('d','i','n','f'), mov_read_default },
{ MKTAG('d','r','e','f'), mov_read_dref },
{ MKTAG('s','t','b','l'), mov_read_default },
{ MKTAG('s','t','c','o'), mov_read_stco },
{ MKTAG('s','t','p','s'), mov_read_stps },
+{ MKTAG('s','t','r','f'), mov_read_strf },
{ MKTAG('s','t','s','c'), mov_read_stsc },
{ MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
{ MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
{ MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
{ MKTAG('t','r','a','k'), mov_read_trak },
{ MKTAG('t','r','a','f'), mov_read_default },
+{ MKTAG('t','r','e','f'), mov_read_default },
+{ MKTAG('c','h','a','p'), mov_read_chap },
{ MKTAG('t','r','e','x'), mov_read_trex },
{ MKTAG('t','r','u','n'), mov_read_trun },
{ MKTAG('u','d','t','a'), mov_read_default },
{ MKTAG('w','a','v','e'), mov_read_wave },
{ MKTAG('e','s','d','s'), mov_read_esds },
+{ MKTAG('d','a','c','3'), mov_read_dac3 }, /* AC-3 info */
{ MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
{ MKTAG('c','m','o','v'), mov_read_cmov },
{ 0, NULL }
return score;
}
+// must be done after parsing all trak because there's no order requirement
+static void mov_read_chapters(AVFormatContext *s)
+{
+ MOVContext *mov = s->priv_data;
+ AVStream *st = NULL;
+ MOVStreamContext *sc;
+ int64_t cur_pos;
+ int i;
+
+ for (i = 0; i < s->nb_streams; i++)
+ if (s->streams[i]->id == mov->chapter_track) {
+ st = s->streams[i];
+ break;
+ }
+ if (!st) {
+ av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
+ return;
+ }
+
+ st->discard = AVDISCARD_ALL;
+ sc = st->priv_data;
+ cur_pos = avio_tell(sc->pb);
+
+ for (i = 0; i < st->nb_index_entries; i++) {
+ AVIndexEntry *sample = &st->index_entries[i];
+ int64_t end = i+1 < st->nb_index_entries ? st->index_entries[i+1].timestamp : st->duration;
+ uint8_t *title;
+ uint16_t ch;
+ int len, title_len;
+
+ if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
+ av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i);
+ goto finish;
+ }
+
+ // the first two bytes are the length of the title
+ len = avio_rb16(sc->pb);
+ if (len > sample->size-2)
+ continue;
+ title_len = 2*len + 1;
+ if (!(title = av_mallocz(title_len)))
+ goto finish;
+
+ // The samples could theoretically be in any encoding if there's an encd
+ // atom following, but in practice are only utf-8 or utf-16, distinguished
+ // instead by the presence of a BOM
+ ch = avio_rb16(sc->pb);
+ if (ch == 0xfeff)
+ avio_get_str16be(sc->pb, len, title, title_len);
+ else if (ch == 0xfffe)
+ avio_get_str16le(sc->pb, len, title, title_len);
+ else {
+ AV_WB16(title, ch);
+ avio_get_str(sc->pb, len - 2, title + 2, title_len - 2);
+ }
+
+ ff_new_chapter(s, i, st->time_base, sample->timestamp, end, title);
+ av_freep(&title);
+ }
+finish:
+ avio_seek(sc->pb, cur_pos, SEEK_SET);
+}
+
static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
{
MOVContext *mov = s->priv_data;
- ByteIOContext *pb = s->pb;
+ AVIOContext *pb = s->pb;
int err;
- MOVAtom atom = { 0, 0, 0 };
+ MOVAtom atom = { AV_RL32("root") };
mov->fc = s;
/* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
if(!url_is_streamed(pb))
- atom.size = url_fsize(pb);
+ atom.size = avio_size(pb);
else
atom.size = INT64_MAX;
av_log(s, AV_LOG_ERROR, "moov atom not found\n");
return -1;
}
- dprintf(mov->fc, "on_parse_exit_offset=%lld\n", url_ftell(pb));
+ av_dlog(mov->fc, "on_parse_exit_offset=%lld\n", avio_tell(pb));
+
+ if (!url_is_streamed(pb) && mov->chapter_track > 0)
+ mov_read_chapters(s);
return 0;
}
if (msc->pb && msc->current_sample < avst->nb_index_entries) {
AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample];
int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
- dprintf(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
+ av_dlog(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
if (!sample || (url_is_streamed(s->pb) && current_sample->pos < sample->pos) ||
(!url_is_streamed(s->pb) &&
((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
if (!sample) {
mov->found_mdat = 0;
if (!url_is_streamed(s->pb) ||
- mov_read_default(mov, s->pb, (MOVAtom){ 0, 0, INT64_MAX }) < 0 ||
- url_feof(s->pb))
+ mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX }) < 0 ||
+ s->pb->eof_reached)
return AVERROR_EOF;
- dprintf(s, "read fragments, offset 0x%llx\n", url_ftell(s->pb));
+ av_dlog(s, "read fragments, offset 0x%llx\n", avio_tell(s->pb));
goto retry;
}
sc = st->priv_data;
sc->current_sample++;
if (st->discard != AVDISCARD_ALL) {
- if (url_fseek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
+ if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
sc->ffindex, sample->pos);
return -1;
}
if (st->discard == AVDISCARD_ALL)
goto retry;
- pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? PKT_FLAG_KEY : 0;
+ pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0;
pkt->pos = sample->pos;
- dprintf(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n",
+ av_dlog(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n",
pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
return 0;
}
-static int mov_seek_stream(AVStream *st, int64_t timestamp, int flags)
+static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
{
MOVStreamContext *sc = st->priv_data;
int sample, time_sample;
int i;
sample = av_index_search_timestamp(st, timestamp, flags);
- dprintf(st->codec, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
+ av_dlog(s, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
+ if (sample < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
+ sample = 0;
if (sample < 0) /* not sure what to do */
return -1;
sc->current_sample = sample;
- dprintf(st->codec, "stream %d, found sample %d\n", st->index, sc->current_sample);
+ av_dlog(s, "stream %d, found sample %d\n", st->index, sc->current_sample);
/* adjust ctts index */
if (sc->ctts_data) {
time_sample = 0;
sample_time = 0;
st = s->streams[stream_index];
- sample = mov_seek_stream(st, sample_time, flags);
+ sample = mov_seek_stream(s, st, sample_time, flags);
if (sample < 0)
return -1;
continue;
timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
- mov_seek_stream(st, timestamp, flags);
+ mov_seek_stream(s, st, timestamp, flags);
}
return 0;
}
MOVStreamContext *sc = st->priv_data;
av_freep(&sc->ctts_data);
- for (j = 0; j < sc->drefs_count; j++)
+ for (j = 0; j < sc->drefs_count; j++) {
av_freep(&sc->drefs[j].path);
+ av_freep(&sc->drefs[j].dir);
+ }
av_freep(&sc->drefs);
if (sc->pb && sc->pb != s->pb)
- url_fclose(sc->pb);
+ avio_close(sc->pb);
av_freep(&st->codec->palctrl);
}
return 0;
}
-AVInputFormat mov_demuxer = {
+AVInputFormat ff_mov_demuxer = {
"mov,mp4,m4a,3gp,3g2,mj2",
NULL_IF_CONFIG_SMALL("QuickTime/MPEG-4/Motion JPEG 2000 format"),
sizeof(MOVContext),