X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavformat%2Fmov.c;h=c6022d5d3a17371366c29863d938f58fb930a18d;hb=53ed79a260099c761f8a74872d695a2aeb7a0ced;hp=2c314e3a390a5855eef237eae6b93ca0ef5fc7f6;hpb=e731b8d8729e75bfb69f5540e6446d6118dac549;p=ffmpeg diff --git a/libavformat/mov.c b/libavformat/mov.c index 2c314e3a390..c6022d5d3a1 100644 --- a/libavformat/mov.c +++ b/libavformat/mov.c @@ -3,36 +3,41 @@ * Copyright (c) 2001 Fabrice Bellard * Copyright (c) 2009 Baptiste Coudurier * - * 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 //#define DEBUG -//#define DEBUG_METADATA //#define MOV_EXPORT_ALL_METADATA #include "libavutil/intreadwrite.h" +#include "libavutil/intfloat.h" +#include "libavutil/mathematics.h" #include "libavutil/avstring.h" +#include "libavutil/dict.h" #include "avformat.h" +#include "internal.h" #include "avio_internal.h" #include "riff.h" #include "isom.h" #include "libavcodec/get_bits.h" +#include "id3v1.h" +#include "mov_chan.h" #if CONFIG_ZLIB #include @@ -41,21 +46,6 @@ /* * 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" @@ -64,13 +54,7 @@ #undef NDEBUG #include -/* 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; @@ -79,15 +63,64 @@ typedef struct MOVParseTableEntry { static const MOVParseTableEntry mov_default_parse_table[]; -static int mov_metadata_trkn(MOVContext *c, AVIOContext *pb, unsigned len) +static int mov_metadata_track_or_disc_number(MOVContext *c, AVIOContext *pb, + unsigned len, const char *key) { char buf[16]; - get_be16(pb); // unknown - snprintf(buf, sizeof(buf), "%d", get_be16(pb)); - av_metadata_set2(&c->fc->metadata, "track", buf, 0); + short current, total; + avio_rb16(pb); // unknown + current = avio_rb16(pb); + total = avio_rb16(pb); + if (!total) + snprintf(buf, sizeof(buf), "%d", current); + else + snprintf(buf, sizeof(buf), "%d/%d", current, total); + av_dict_set(&c->fc->metadata, key, buf, 0); - get_be16(pb); // total tracks + return 0; +} + +static int mov_metadata_int8_bypass_padding(MOVContext *c, AVIOContext *pb, + unsigned len, const char *key) +{ + char buf[16]; + + /* bypass padding bytes */ + avio_r8(pb); + avio_r8(pb); + avio_r8(pb); + + snprintf(buf, sizeof(buf), "%d", avio_r8(pb)); + av_dict_set(&c->fc->metadata, key, buf, 0); + + return 0; +} + +static int mov_metadata_int8_no_padding(MOVContext *c, AVIOContext *pb, + unsigned len, const char *key) +{ + char buf[16]; + + snprintf(buf, sizeof(buf), "%d", avio_r8(pb)); + av_dict_set(&c->fc->metadata, key, buf, 0); + + return 0; +} + +static int mov_metadata_gnre(MOVContext *c, AVIOContext *pb, + unsigned len, const char *key) +{ + short genre; + char buf[20]; + + avio_r8(pb); // unknown + + genre = avio_r8(pb); + if (genre < 1 || genre > ID3v1_GENRE_MAX) + return 0; + snprintf(buf, sizeof(buf), "%s", ff_id3v1_genre_str[genre-1]); + av_dict_set(&c->fc->metadata, key, buf, 0); return 0; } @@ -119,7 +152,7 @@ static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len, int i; for (i = 0; i < len; i++) { - uint8_t t, c = get_byte(pb); + uint8_t t, c = avio_r8(pb); if (c < 0x80 && p < end) *p++ = c; else @@ -138,12 +171,13 @@ static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom) const char *key = NULL; uint16_t str_size, langcode = 0; uint32_t data_type = 0; - int (*parse)(MOVContext*, AVIOContext*, unsigned) = NULL; + int (*parse)(MOVContext*, AVIOContext*, unsigned, const char*) = 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'): key = "artist"; break; + case MKTAG( 'a','A','R','T'): key = "album_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; @@ -152,6 +186,8 @@ static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom) case MKTAG(0xa9,'a','l','b'): key = "album"; break; case MKTAG(0xa9,'d','a','y'): key = "date"; break; case MKTAG(0xa9,'g','e','n'): key = "genre"; break; + case MKTAG( 'g','n','r','e'): key = "genre"; + parse = mov_metadata_gnre; break; case MKTAG(0xa9,'t','o','o'): case MKTAG(0xa9,'s','w','r'): key = "encoder"; break; case MKTAG(0xa9,'e','n','c'): key = "encoder"; break; @@ -161,21 +197,33 @@ static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom) 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; + parse = mov_metadata_track_or_disc_number; break; + case MKTAG( 'd','i','s','k'): key = "disc"; + parse = mov_metadata_track_or_disc_number; break; + case MKTAG( 't','v','e','s'): key = "episode_sort"; + parse = mov_metadata_int8_bypass_padding; break; + case MKTAG( 't','v','s','n'): key = "season_number"; + parse = mov_metadata_int8_bypass_padding; break; + case MKTAG( 's','t','i','k'): key = "media_type"; + parse = mov_metadata_int8_no_padding; break; + case MKTAG( 'h','d','v','d'): key = "hd_video"; + parse = mov_metadata_int8_no_padding; break; + case MKTAG( 'p','g','a','p'): key = "gapless_playback"; + parse = mov_metadata_int8_no_padding; 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')) { - data_type = 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 - langcode = get_be16(pb); + str_size = avio_rb16(pb); // string length + langcode = avio_rb16(pb); ff_mov_lang_to_iso639(langcode, language); atom.size -= 4; } else @@ -191,30 +239,28 @@ static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom) if (!key) return 0; if (atom.size < 0) - return -1; + return AVERROR_INVALIDDATA; str_size = FFMIN3(sizeof(str)-1, str_size, atom.size); if (parse) - parse(c, pb, str_size); + parse(c, pb, str_size, key); else { if (data_type == 3 || (data_type == 0 && langcode < 0x800)) { // MAC Encoded mov_read_mac_string(c, pb, str_size, str, sizeof(str)); } else { - get_buffer(pb, str, str_size); + avio_read(pb, str, str_size); str[str_size] = 0; } - av_metadata_set2(&c->fc->metadata, key, str, 0); + av_dict_set(&c->fc->metadata, key, str, 0); if (*language && strcmp(language, "und")) { snprintf(key2, sizeof(key2), "%s-%s", key, language); - av_metadata_set2(&c->fc->metadata, key2, str, 0); + av_dict_set(&c->fc->metadata, key2, str, 0); } } -#ifdef DEBUG_METADATA - av_log(c->fc, AV_LOG_DEBUG, "lang \"%3s\" ", language); - av_log(c->fc, AV_LOG_DEBUG, "tag \"%s\" value \"%s\" atom \"%.4s\" %d %lld\n", - key, str, (char*)&atom.type, str_size, atom.size); -#endif + av_dlog(c->fc, "lang \"%3s\" ", language); + av_dlog(c->fc, "tag \"%s\" value \"%s\" atom \"%.4s\" %d %"PRId64"\n", + key, str, (char*)&atom.type, str_size, atom.size); return 0; } @@ -228,25 +274,25 @@ static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom) if ((atom.size -= 5) < 0) return 0; - version = get_byte(pb); - get_be24(pb); + version = avio_r8(pb); + avio_rb24(pb); if (version) - get_be32(pb); // ??? - nb_chapters = get_byte(pb); + avio_rb32(pb); // ??? + nb_chapters = avio_r8(pb); for (i = 0; i < nb_chapters; i++) { if (atom.size < 9) return 0; - start = get_be64(pb); - str_len = get_byte(pb); + start = avio_rb64(pb); + str_len = avio_r8(pb); if ((atom.size -= 9+str_len) < 0) return 0; - get_buffer(pb, str, str_len); + avio_read(pb, str, str_len); str[str_len] = 0; - ff_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str); + avpriv_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str); } return 0; } @@ -259,19 +305,19 @@ static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom) if (atom.size < 0) atom.size = INT64_MAX; - while (total_size + 8 < atom.size && !url_feof(pb)) { + 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); + if (atom.size >= 8) { + 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; if (a.size == 1) { /* 64 bit extended size */ - a.size = get_be64(pb) - 8; + a.size = avio_rb64(pb) - 8; total_size += 8; } if (a.size == 0) { @@ -280,7 +326,7 @@ static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom) break; } a.size -= 8; - if(a.size < 0) + if (a.size < 0) break; a.size = FFMIN(a.size, atom.size - total_size); @@ -296,26 +342,26 @@ static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom) 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; 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 == url_fsize(pb))) + (!pb->seekable || start_pos + a.size == avio_size(pb))) return 0; - left = a.size - url_ftell(pb) + start_pos; + left = a.size - avio_tell(pb) + start_pos; if (left > 0) /* skip garbage at atom end */ - url_fskip(pb, left); + avio_skip(pb, left); } total_size += a.size; } if (total_size < atom.size && atom.size < 0x7ffff) - url_fskip(pb, atom.size - total_size); + avio_skip(pb, atom.size - total_size); return 0; } @@ -331,10 +377,10 @@ static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom) 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; + return AVERROR_INVALIDDATA; sc->drefs = av_mallocz(entries * sizeof(*sc->drefs)); if (!sc->drefs) return AVERROR(ENOMEM); @@ -342,14 +388,14 @@ static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom) 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; if (size < 12) - return -1; + return AVERROR_INVALIDDATA; - dref->type = get_le32(pb); - get_be32(pb); // version + flags + 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) { @@ -357,35 +403,35 @@ static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom) uint16_t volume_len, len; 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, dref->volume, 27); + 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); - url_fskip(pb, 12); + avio_skip(pb, 12); - len = get_byte(pb); + len = avio_r8(pb); len = FFMIN(len, 63); - get_buffer(pb, dref->filename, 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); - url_fskip(pb, 16); + avio_skip(pb, 16); /* read next level up_from_alias/down_to_target */ - dref->nlvl_from = get_be16(pb); - dref->nlvl_to = get_be16(pb); + 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); - url_fskip(pb, 16); + avio_skip(pb, 16); - for (type = 0; type != -1 && url_ftell(pb) < next; ) { - type = get_be16(pb); - len = get_be16(pb); + 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; @@ -394,7 +440,7 @@ static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom) dref->path = av_mallocz(len+1); if (!dref->path) return AVERROR(ENOMEM); - get_buffer(pb, dref->path, 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); @@ -409,17 +455,17 @@ static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom) dref->dir = av_malloc(len+1); if (!dref->dir) return AVERROR(ENOMEM); - get_buffer(pb, dref->dir, len); + 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; } @@ -428,35 +474,35 @@ static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom) { AVStream *st; uint32_t type; - uint32_t ctype; + uint32_t av_unused ctype; if (c->fc->nb_streams < 1) // meta before first trak return 0; 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 */ 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 = AVMEDIA_TYPE_VIDEO; - else if(type == MKTAG('s','o','u','n')) + else if (type == MKTAG('s','o','u','n')) st->codec->codec_type = AVMEDIA_TYPE_AUDIO; - else if(type == MKTAG('m','1','a',' ')) + else if (type == MKTAG('m','1','a',' ')) st->codec->codec_id = CODEC_ID_MP2; - else if(type == MKTAG('s','u','b','p')) + else if ((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p'))) 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 */ return 0; } @@ -464,21 +510,20 @@ static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom) int ff_mov_read_esds(AVFormatContext *fc, AVIOContext *pb, MOVAtom atom) { AVStream *st; - int tag, len; + int tag; if (fc->nb_streams < 1) return 0; st = fc->streams[fc->nb_streams-1]; - get_be32(pb); /* version + flags */ - len = ff_mp4_read_descr(fc, pb, &tag); + avio_rb32(pb); /* version + flags */ + ff_mp4_read_descr(fc, pb, &tag); if (tag == MP4ESDescrTag) { - get_be16(pb); /* ID */ - get_byte(pb); /* priority */ + ff_mp4_parse_es_descr(pb, NULL); } else - get_be16(pb); /* ID */ + avio_rb16(pb); /* ID */ - len = ff_mp4_read_descr(fc, pb, &tag); + ff_mp4_read_descr(fc, pb, &tag); if (tag == MP4DecConfigDescrTag) ff_mp4_read_dec_config_descr(fc, st, pb); return 0; @@ -492,24 +537,93 @@ static int mov_read_esds(MOVContext *c, AVIOContext *pb, MOVAtom atom) static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom) { AVStream *st; - int ac3info, acmod, lfeon; + int ac3info, acmod, lfeon, bsmod; if (c->fc->nb_streams < 1) return 0; st = c->fc->streams[c->fc->nb_streams-1]; - ac3info = get_be24(pb); + 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_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom) +{ + AVStream *st; + uint8_t version; + uint32_t flags, layout_tag, bitmap, num_descr, label_mask; + int i; + + if (c->fc->nb_streams < 1) + return 0; + st = c->fc->streams[c->fc->nb_streams-1]; + + if (atom.size < 16) + return 0; + + version = avio_r8(pb); + flags = avio_rb24(pb); + + layout_tag = avio_rb32(pb); + bitmap = avio_rb32(pb); + num_descr = avio_rb32(pb); + + if (atom.size < 16ULL + num_descr * 20ULL) + return 0; + + av_dlog(c->fc, "chan: size=%ld version=%u flags=%u layout=%u bitmap=%u num_descr=%u\n", + atom.size, version, flags, layout_tag, bitmap, num_descr); + + label_mask = 0; + for (i = 0; i < num_descr; i++) { + uint32_t label, cflags; + label = avio_rb32(pb); // mChannelLabel + cflags = avio_rb32(pb); // mChannelFlags + avio_rl32(pb); // mCoordinates[0] + avio_rl32(pb); // mCoordinates[1] + avio_rl32(pb); // mCoordinates[2] + if (layout_tag == 0) { + uint32_t mask_incr = ff_mov_get_channel_label(label); + if (mask_incr == 0) { + label_mask = 0; + break; + } + label_mask |= mask_incr; + } + } + if (layout_tag == 0) + st->codec->channel_layout = label_mask; + else + st->codec->channel_layout = ff_mov_get_channel_layout(layout_tag, bitmap); + + return 0; +} + +static int mov_read_wfex(MOVContext *c, AVIOContext *pb, MOVAtom atom) +{ + AVStream *st; + + if (c->fc->nb_streams < 1) + return 0; + st = c->fc->streams[c->fc->nb_streams-1]; + + ff_get_wav_header(pb, st->codec, atom.size); 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) @@ -532,7 +646,7 @@ static int mov_read_pasp(MOVContext *c, AVIOContext *pb, MOVAtom atom) /* this atom contains actual media data */ static int mov_read_mdat(MOVContext *c, AVIOContext *pb, MOVAtom atom) { - if(atom.size == 0) /* wrong one (MP4) */ + if (atom.size == 0) /* wrong one (MP4) */ return 0; c->found_mdat=1; return 0; /* now go for moov */ @@ -547,24 +661,24 @@ static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom) 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_set2(&c->fc->metadata, "major_brand", type, 0); - minor_ver = get_be32(pb); /* minor version */ + av_dict_set(&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_set2(&c->fc->metadata, "minor_version", minor_ver_str, 0); + av_dict_set(&c->fc->metadata, "minor_version", minor_ver_str, 0); comp_brand_size = atom.size - 8; if (comp_brand_size < 0) - return -1; + return AVERROR_INVALIDDATA; 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_set2(&c->fc->metadata, "compatible_brands", comp_brands_str, 0); + av_dict_set(&c->fc->metadata, "compatible_brands", comp_brands_str, 0); av_freep(&comp_brands_str); return 0; @@ -573,8 +687,10 @@ static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom) /* this atom should contain all header atoms */ static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom) { - if (mov_read_default(c, pb, atom) < 0) - return -1; + int ret; + + if ((ret = mov_read_default(c, pb, atom)) < 0) + return ret; /* 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; @@ -583,12 +699,12 @@ static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom) static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom) { - c->fragment.moof_offset = url_ftell(pb) - 8; - av_dlog(c->fc, "moof offset %llx\n", c->fragment.moof_offset); + c->fragment.moof_offset = avio_tell(pb) - 8; + av_dlog(c->fc, "moof offset %"PRIx64"\n", c->fragment.moof_offset); return mov_read_default(c, pb, atom); } -static void mov_metadata_creation_time(AVMetadata **metadata, time_t time) +static void mov_metadata_creation_time(AVDictionary **metadata, time_t time) { char buffer[32]; if (time) { @@ -597,7 +713,7 @@ static void mov_metadata_creation_time(AVMetadata **metadata, time_t time) 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); + av_dict_set(metadata, "creation_time", buffer, 0); } } @@ -615,27 +731,28 @@ static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom) st = c->fc->streams[c->fc->nb_streams-1]; sc = st->priv_data; - version = get_byte(pb); - if (version > 1) - return -1; /* unsupported */ - - get_be24(pb); /* flags */ + version = avio_r8(pb); + if (version > 1) { + av_log_ask_for_sample(c, "unsupported version %d\n", version); + return AVERROR_PATCHWELCOME; + } + avio_rb24(pb); /* flags */ if (version == 1) { - creation_time = get_be64(pb); - get_be64(pb); + creation_time = avio_rb64(pb); + avio_rb64(pb); } else { - creation_time = get_be32(pb); - 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_set2(&st->metadata, "language", language, 0); - get_be16(pb); /* quality */ + av_dict_set(&st->metadata, "language", language, 0); + avio_rb16(pb); /* quality */ return 0; } @@ -643,37 +760,37 @@ static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom) static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom) { time_t creation_time; - int version = get_byte(pb); /* version */ - get_be24(pb); /* flags */ + int version = avio_r8(pb); /* version */ + avio_rb24(pb); /* flags */ if (version == 1) { - creation_time = get_be64(pb); - get_be64(pb); + creation_time = avio_rb64(pb); + avio_rb64(pb); } else { - creation_time = get_be32(pb); - get_be32(pb); /* modification time */ + creation_time = avio_rb32(pb); + avio_rb32(pb); /* modification time */ } mov_metadata_creation_time(&c->fc->metadata, creation_time); - c->time_scale = get_be32(pb); /* time scale */ + c->time_scale = avio_rb32(pb); /* 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; } @@ -686,8 +803,8 @@ static int mov_read_smi(MOVContext *c, AVIOContext *pb, MOVAtom atom) return 0; st = c->fc->streams[c->fc->nb_streams-1]; - if((uint64_t)atom.size > (1<<30)) - return -1; + if ((uint64_t)atom.size > (1<<30)) + return AVERROR_INVALIDDATA; // currently SVQ3 decoder expect full STSD header - so let's fake it // this should be fixed and just SMI header should be passed @@ -697,7 +814,7 @@ static int mov_read_smi(MOVContext *c, AVIOContext *pb, MOVAtom atom) 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); + 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; } @@ -711,7 +828,7 @@ static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom) return 0; st = c->fc->streams[c->fc->nb_streams-1]; - little_endian = get_be16(pb); + little_endian = avio_rb16(pb); av_dlog(c->fc, "enda %d\n", little_endian); if (little_endian == 1) { switch (st->codec->codec_id) { @@ -734,6 +851,40 @@ static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom) return 0; } +static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom) +{ + AVStream *st; + unsigned mov_field_order; + enum AVFieldOrder decoded_field_order = AV_FIELD_UNKNOWN; + + if (c->fc->nb_streams < 1) // will happen with jp2 files + return 0; + st = c->fc->streams[c->fc->nb_streams-1]; + if (atom.size < 2) + return AVERROR_INVALIDDATA; + mov_field_order = avio_rb16(pb); + if ((mov_field_order & 0xFF00) == 0x0100) + decoded_field_order = AV_FIELD_PROGRESSIVE; + else if ((mov_field_order & 0xFF00) == 0x0200) { + switch (mov_field_order & 0xFF) { + case 0x01: decoded_field_order = AV_FIELD_TT; + break; + case 0x06: decoded_field_order = AV_FIELD_BB; + break; + case 0x09: decoded_field_order = AV_FIELD_TB; + break; + case 0x0E: decoded_field_order = AV_FIELD_BT; + break; + } + } + if (decoded_field_order == AV_FIELD_UNKNOWN && mov_field_order) { + av_log(NULL, AV_LOG_ERROR, "Unknown MOV field order 0x%04x\n", mov_field_order); + } + st->codec->field_order = decoded_field_order; + + return 0; +} + /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */ static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom) { @@ -745,17 +896,17 @@ static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom) return 0; st= c->fc->streams[c->fc->nb_streams-1]; size= (uint64_t)st->codec->extradata_size + atom.size + 8 + FF_INPUT_BUFFER_PADDING_SIZE; - if(size > INT_MAX || (uint64_t)atom.size > INT_MAX) - return -1; + if (size > INT_MAX || (uint64_t)atom.size > INT_MAX) + return AVERROR_INVALIDDATA; buf= av_realloc(st->codec->extradata, size); - if(!buf) - return -1; + if (!buf) + return AVERROR(ENOMEM); st->codec->extradata= buf; buf+= st->codec->extradata_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; } @@ -767,8 +918,8 @@ static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom) return 0; st = c->fc->streams[c->fc->nb_streams-1]; - if((uint64_t)atom.size > (1<<30)) - return -1; + if ((uint64_t)atom.size > (1<<30)) + return AVERROR_INVALIDDATA; 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 @@ -777,12 +928,13 @@ static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom) 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; + int ret; + if ((ret = mov_read_default(c, pb, atom)) < 0) + return ret; } else - url_fskip(pb, atom.size); + avio_skip(pb, atom.size); return 0; } @@ -798,15 +950,24 @@ static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom) return 0; st = c->fc->streams[c->fc->nb_streams-1]; - if((uint64_t)atom.size > (1<<30)) - return -1; + if ((uint64_t)atom.size > (1<<30)) + return AVERROR_INVALIDDATA; + if (atom.size >= 10) { + // Broken files created by legacy versions of Libav and FFmpeg will + // wrap a whole fiel atom inside of a glbl atom. + unsigned size = avio_rb32(pb); + unsigned type = avio_rl32(pb); + avio_seek(pb, -8, SEEK_CUR); + if (type == MKTAG('f','i','e','l') && size == atom.size) + return mov_read_default(c, pb, atom); + } 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); return 0; } @@ -825,16 +986,16 @@ static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom) return 0; st = c->fc->streams[c->fc->nb_streams-1]; - if((uint64_t)atom.size > (1<<30)) - return -1; + if ((uint64_t)atom.size > (1<<30)) + return AVERROR_INVALIDDATA; 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; - url_fskip(pb, 40); - get_buffer(pb, st->codec->extradata, atom.size - 40); + avio_skip(pb, 40); + avio_read(pb, st->codec->extradata, atom.size - 40); return 0; } @@ -849,13 +1010,15 @@ static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom) 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 (!entries) + return 0; + if (entries >= UINT_MAX/sizeof(int64_t)) + return AVERROR_INVALIDDATA; sc->chunk_offsets = av_malloc(entries * sizeof(int64_t)); if (!sc->chunk_offsets) @@ -863,13 +1026,13 @@ static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom) sc->chunk_count = entries; if (atom.type == MKTAG('s','t','c','o')) - for(i=0; ichunk_offsets[i] = get_be32(pb); + for (i=0; ichunk_offsets[i] = avio_rb32(pb); else if (atom.type == MKTAG('c','o','6','4')) - for(i=0; ichunk_offsets[i] = get_be64(pb); + for (i=0; ichunk_offsets[i] = avio_rb64(pb); else - return -1; + return AVERROR_INVALIDDATA; return 0; } @@ -920,19 +1083,19 @@ int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries) st = c->fc->streams[c->fc->nb_streams-1]; sc = st->priv_data; - for(pseudo_stream_id=0; pseudo_stream_id= 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 && @@ -945,7 +1108,7 @@ int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries) * 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 */ @@ -968,9 +1131,9 @@ int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries) id = ff_codec_get_id(ff_codec_bmp_tags, format); if (id > 0) st->codec->codec_type = AVMEDIA_TYPE_VIDEO; - else if(st->codec->codec_type == AVMEDIA_TYPE_DATA){ + else if (st->codec->codec_type == AVMEDIA_TYPE_DATA){ id = ff_codec_get_id(ff_codec_movsubtitle_tags, format); - if(id > 0) + if (id > 0) st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; } } @@ -979,37 +1142,37 @@ int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries) (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff, (format >> 24) & 0xff, st->codec->codec_type); - if(st->codec->codec_type==AVMEDIA_TYPE_VIDEO) { + 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 */ + 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 = get_be16(pb); /* width */ - st->codec->height = get_be16(pb); /* height */ + st->codec->width = avio_rb16(pb); /* width */ + st->codec->height = avio_rb16(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 */ + avio_rb32(pb); /* horiz resolution */ + avio_rb32(pb); /* vert resolution */ + avio_rb32(pb); /* data size, always 0 */ + avio_rb16(pb); /* frames per samples */ - len = get_byte(pb); /* codec name, pascal string */ + 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) - url_fskip(pb, 31 - len); + 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 = get_be16(pb); /* depth */ - st->codec->color_table_id = get_be16(pb); /* colortable id */ + 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 */ @@ -1023,7 +1186,6 @@ int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries) unsigned int color_start, color_count, color_end; unsigned char r, g, b; - st->codec->palctrl = av_malloc(sizeof(*st->codec->palctrl)); if (color_greyscale) { int color_index, color_dec; /* compute the greyscale palette */ @@ -1033,7 +1195,7 @@ int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries) color_dec = 256 / (color_count - 1); for (j = 0; j < color_count; j++) { r = g = b = color_index; - st->codec->palctrl->palette[j] = + sc->palette[j] = (r << 16) | (g << 8) | (b); color_index -= color_dec; if (color_index < 0) @@ -1054,69 +1216,69 @@ int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries) r = color_table[j * 3 + 0]; g = color_table[j * 3 + 1]; b = color_table[j * 3 + 2]; - st->codec->palctrl->palette[j] = + sc->palette[j] = (r << 16) | (g << 8) | (b); } } 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); - st->codec->palctrl->palette[j] = + 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); + sc->palette[j] = (r << 16) | (g << 8) | (b); } } } - st->codec->palctrl->palette_changed = 1; + sc->has_palette = 1; } - } else if(st->codec->codec_type==AVMEDIA_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 */ + 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 = get_be16(pb); /* sample size */ + 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. 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 */ - } 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); /* lpcm 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 */ + if (!c->isom) { + if (version==1) { + 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) { + avio_rb32(pb); /* sizeof struct only */ + st->codec->sample_rate = av_int2double(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); } @@ -1163,10 +1325,10 @@ int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries) 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==AVMEDIA_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; @@ -1174,18 +1336,19 @@ int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries) 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; + int ret; + if ((ret = mov_read_default(c, pb, a)) < 0) + return ret; } else if (a.size > 0) - url_fskip(pb, a.size); + avio_skip(pb, a.size); } - if(st->codec->codec_type==AVMEDIA_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 */ @@ -1193,10 +1356,10 @@ int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries) #if CONFIG_DV_DEMUXER case CODEC_ID_DVAUDIO: c->dv_fctx = avformat_alloc_context(); - c->dv_demux = dv_init_demux(c->dv_fctx); + c->dv_demux = avpriv_dv_init_demux(c->dv_fctx); if (!c->dv_demux) { av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n"); - return -1; + return AVERROR(ENOMEM); } sc->dv_audio_container = 1; st->codec->codec_id = CODEC_ID_PCM_S16LE; @@ -1211,14 +1374,16 @@ int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries) st->codec->channels= 1; /* really needed */ break; case CODEC_ID_AMR_NB: - case CODEC_ID_AMR_WB: - st->codec->frame_size= sc->samples_per_frame; st->codec->channels= 1; /* really needed */ /* force sample rate for amr, stsd in 3gp does not store sample rate */ - if (st->codec->codec_id == CODEC_ID_AMR_NB) - st->codec->sample_rate = 8000; - else if (st->codec->codec_id == CODEC_ID_AMR_WB) - st->codec->sample_rate = 16000; + st->codec->sample_rate = 8000; + /* force frame_size, too, samples_per_frame isn't always set properly */ + st->codec->frame_size = 160; + break; + case CODEC_ID_AMR_WB: + st->codec->channels = 1; + st->codec->sample_rate = 16000; + st->codec->frame_size = 320; break; case CODEC_ID_MP2: case CODEC_ID_MP3: @@ -1249,9 +1414,9 @@ static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom) { int entries; - get_byte(pb); /* version */ - get_be24(pb); /* flags */ - entries = get_be32(pb); + avio_r8(pb); /* version */ + avio_rb24(pb); /* flags */ + entries = avio_rb32(pb); return ff_mov_read_stsd_entries(c, pb, entries); } @@ -1267,24 +1432,26 @@ static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom) 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); 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; + if (!entries) + return 0; + if (entries >= UINT_MAX / sizeof(*sc->stsc_data)) + return AVERROR_INVALIDDATA; sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data)); if (!sc->stsc_data) return AVERROR(ENOMEM); sc->stsc_count = entries; - for(i=0; istsc_data[i].first = get_be32(pb); - sc->stsc_data[i].count = get_be32(pb); - sc->stsc_data[i].id = get_be32(pb); + for (i=0; istsc_data[i].first = avio_rb32(pb); + sc->stsc_data[i].count = avio_rb32(pb); + sc->stsc_data[i].id = avio_rb32(pb); } return 0; } @@ -1300,18 +1467,18 @@ static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom) 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; + return AVERROR_INVALIDDATA; sc->stps_data = av_malloc(entries * sizeof(*sc->stps_data)); if (!sc->stps_data) return AVERROR(ENOMEM); sc->stps_count = entries; for (i = 0; i < entries; i++) { - sc->stps_data[i] = get_be32(pb); + sc->stps_data[i] = avio_rb32(pb); //av_dlog(c->fc, "stps %d\n", sc->stps_data[i]); } @@ -1329,22 +1496,22 @@ static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom) 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); av_dlog(c->fc, "keyframe_count = %d\n", entries); - if(entries >= UINT_MAX / sizeof(int)) - return -1; + if (entries >= UINT_MAX / sizeof(int)) + return AVERROR_INVALIDDATA; sc->keyframes = av_malloc(entries * sizeof(int)); if (!sc->keyframes) return AVERROR(ENOMEM); sc->keyframe_count = entries; - for(i=0; ikeyframes[i] = get_be32(pb); + for (i=0; ikeyframes[i] = avio_rb32(pb); //av_dlog(c->fc, "keyframes[]=%d\n", sc->keyframes[i]); } return 0; @@ -1363,20 +1530,20 @@ static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom) 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); av_dlog(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, entries); @@ -1386,11 +1553,13 @@ static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom) if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) { av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %d\n", field_size); - return -1; + return AVERROR_INVALIDDATA; } + if (!entries) + return 0; if (entries >= UINT_MAX / sizeof(int) || entries >= (UINT_MAX - 4) / field_size) - return -1; + return AVERROR_INVALIDDATA; sc->sample_sizes = av_malloc(entries * sizeof(int)); if (!sc->sample_sizes) return AVERROR(ENOMEM); @@ -1403,15 +1572,15 @@ static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom) 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 AVERROR_INVALIDDATA; } init_get_bits(&gb, buf, 8*num_bytes); - for(i=0; isample_sizes[i] = get_bits_long(&gb, field_size); av_free(buf); @@ -1431,36 +1600,42 @@ static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom) 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); - av_dlog(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) + return 0; + if (entries >= UINT_MAX / sizeof(*sc->stts_data)) + return AVERROR(EINVAL); - if(entries >= UINT_MAX / sizeof(*sc->stts_data)) - return -1; sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data)); if (!sc->stts_data) return AVERROR(ENOMEM); + sc->stts_count = entries; - for(i=0; istts_data[i].count= sample_count; sc->stts_data[i].duration= sample_duration; - av_dlog(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; } st->nb_frames= total_sample_count; - if(duration) + if (duration) st->duration= duration; return 0; } @@ -1476,22 +1651,24 @@ static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom) 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); 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; + if (!entries) + return 0; + if (entries >= UINT_MAX / sizeof(*sc->ctts_data)) + return AVERROR_INVALIDDATA; sc->ctts_data = av_malloc(entries * sizeof(*sc->ctts_data)); if (!sc->ctts_data) return AVERROR(ENOMEM); sc->ctts_count = entries; - for(i=0; ictts_data[i].count = count; sc->ctts_data[i].duration= duration; @@ -1518,9 +1695,10 @@ static void mov_build_index(MOVContext *mov, AVStream *st) /* adjust first dts according to edit list */ 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->stts_data && + if (sc->time_offset < 0) + sc->time_offset = av_rescale(sc->time_offset, sc->time_scale, mov->time_scale); + current_dts = -sc->time_offset; + if (sc->ctts_data && sc->stts_data && sc->stts_data[0].duration && 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 */ @@ -1540,6 +1718,8 @@ static void mov_build_index(MOVContext *mov, AVStream *st) current_dts -= sc->dts_shift; + if (!sc->sample_count) + return; if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries)) return; st->index_entries = av_malloc(sc->sample_count*sizeof(*st->index_entries)); @@ -1549,7 +1729,7 @@ static void mov_build_index(MOVContext *mov, AVStream *st) for (i = 0; i < sc->chunk_count; i++) { current_offset = sc->chunk_offsets[i]; - if (stsc_index + 1 < sc->stsc_count && + while (stsc_index + 1 < sc->stsc_count && i + 1 == sc->stsc_data[stsc_index + 1].first) stsc_index++; for (j = 0; j < sc->stsc_data[stsc_index].count; j++) { @@ -1571,7 +1751,7 @@ static void mov_build_index(MOVContext *mov, AVStream *st) if (keyframe) distance = 0; sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample]; - if(sc->pseudo_stream_id == -1 || + if (sc->pseudo_stream_id == -1 || sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) { AVIndexEntry *e = &st->index_entries[st->nb_index_entries++]; e->pos = current_offset; @@ -1683,7 +1863,8 @@ static void mov_build_index(MOVContext *mov, AVStream *st) } } -static int mov_open_dref(AVIOContext **pb, char *src, MOVDref *ref) +static int mov_open_dref(AVIOContext **pb, char *src, MOVDref *ref, + AVIOInterruptCB *int_cb) { /* try relative path, we do not try the absolute because it can leak information about our system to an attacker */ @@ -1718,7 +1899,7 @@ static int mov_open_dref(AVIOContext **pb, char *src, MOVDref *ref) av_strlcat(filename, ref->path + l + 1, 1024); - if (!url_fopen(pb, filename, URL_RDONLY)) + if (!avio_open2(pb, filename, AVIO_FLAG_READ, int_cb, NULL)) return 0; } } @@ -1732,8 +1913,9 @@ static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom) MOVStreamContext *sc; int ret; - st = av_new_stream(c->fc, c->fc->nb_streams); + st = avformat_new_stream(c->fc, NULL); if (!st) return AVERROR(ENOMEM); + st->id = c->fc->nb_streams; sc = av_mallocz(sizeof(MOVStreamContext)); if (!sc) return AVERROR(ENOMEM); @@ -1759,7 +1941,7 @@ static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom) sc->time_scale = 1; } - av_set_pts_info(st, 64, 1, sc->time_scale); + avpriv_set_pts_info(st, 64, 1, sc->time_scale); if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && !st->codec->frame_size && sc->stts_count == 1) { @@ -1772,7 +1954,7 @@ static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom) if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) { MOVDref *dref = &sc->drefs[sc->dref_id - 1]; - if (mov_open_dref(&sc->pb, c->fc->filename, dref) < 0) + if (mov_open_dref(&sc->pb, c->fc->filename, dref, &c->fc->interrupt_callback) < 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", @@ -1837,10 +2019,10 @@ static int mov_read_ilst(MOVContext *c, AVIOContext *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); } @@ -1864,8 +2046,8 @@ static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom) 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 @@ -1874,36 +2056,36 @@ static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom) */ 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; @@ -1937,12 +2119,12 @@ static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom) 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; + return AVERROR_INVALIDDATA; frag->track_id = track_id; for (i = 0; i < c->trex_count; i++) if (c->trex_data[i].track_id == frag->track_id) { @@ -1951,24 +2133,24 @@ static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom) } if (!trex) { av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n"); - return -1; + return AVERROR_INVALIDDATA; } - 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; + 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 = get_be32(pb); + c->chapter_track = avio_rb32(pb); return 0; } @@ -1977,19 +2159,19 @@ static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom) MOVTrackExt *trex; if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data)) - return -1; + return AVERROR_INVALIDDATA; trex = av_realloc(c->trex_data, (c->trex_count+1)*sizeof(*c->trex_data)); if (!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; } @@ -1998,6 +2180,7 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom) MOVFragment *frag = &c->fragment; AVStream *st = NULL; MOVStreamContext *sc; + MOVStts *ctts_data; uint64_t offset; int64_t dts; int data_offset = 0; @@ -2012,28 +2195,43 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom) } if (!st) { av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id); - return -1; + return AVERROR_INVALIDDATA; } 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); + 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 = get_be32(pb); - if (flags & 0x004) first_sample_flags = get_be32(pb); - if (flags & 0x800) { - MOVStts *ctts_data; - if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data)) - return -1; - ctts_data = av_realloc(sc->ctts_data, - (entries+sc->ctts_count)*sizeof(*sc->ctts_data)); + + /* Always assume the presence of composition time offsets. + * Without this assumption, for instance, we cannot deal with a track in fragmented movies that meet the following. + * 1) in the initial movie, there are no samples. + * 2) in the first movie fragment, there is only one sample without composition time offset. + * 3) in the subsequent movie fragments, there are samples with composition time offset. */ + if (!sc->ctts_count && sc->sample_count) + { + /* Complement ctts table if moov atom doesn't have ctts atom. */ + ctts_data = av_malloc(sizeof(*sc->ctts_data)); if (!ctts_data) return AVERROR(ENOMEM); sc->ctts_data = ctts_data; + sc->ctts_data[sc->ctts_count].count = sc->sample_count; + sc->ctts_data[sc->ctts_count].duration = 0; + sc->ctts_count++; } - dts = st->duration; + if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data)) + return AVERROR_INVALIDDATA; + ctts_data = av_realloc(sc->ctts_data, + (entries+sc->ctts_count)*sizeof(*sc->ctts_data)); + if (!ctts_data) + return AVERROR(ENOMEM); + sc->ctts_data = ctts_data; + + if (flags & 0x001) data_offset = avio_rb32(pb); + if (flags & 0x004) first_sample_flags = avio_rb32(pb); + dts = st->duration - sc->time_offset; offset = frag->base_data_offset + data_offset; distance = 0; av_dlog(c->fc, "first sample flags 0x%x\n", first_sample_flags); @@ -2043,14 +2241,12 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom) 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 & 0x800) { - sc->ctts_data[sc->ctts_count].count = 1; - sc->ctts_data[sc->ctts_count].duration = get_be32(pb); - sc->ctts_count++; - } + if (flags & 0x100) sample_duration = avio_rb32(pb); + if (flags & 0x200) sample_size = avio_rb32(pb); + if (flags & 0x400) sample_flags = avio_rb32(pb); + sc->ctts_data[sc->ctts_count].count = 1; + sc->ctts_data[sc->ctts_count].duration = (flags & 0x800) ? avio_rb32(pb) : 0; + sc->ctts_count++; if ((keyframe = st->codec->codec_type == AVMEDIA_TYPE_AUDIO || (flags & 0x004 && !i && !sample_flags) || sample_flags & 0x2000000)) distance = 0; @@ -2064,7 +2260,7 @@ static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom) offset += sample_size; } frag->moof_offset = offset; - st->duration = dts; + st->duration = dts + sc->time_offset; return 0; } @@ -2077,14 +2273,14 @@ static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom) 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.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); @@ -2100,17 +2296,17 @@ static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom) long cmov_len, moov_len; int ret = -1; - get_be32(pb); /* dcom atom */ - if (get_le32(pb) != MKTAG('d','c','o','m')) - return -1; - if (get_le32(pb) != MKTAG('z','l','i','b')) { + avio_rb32(pb); /* dcom atom */ + if (avio_rl32(pb) != MKTAG('d','c','o','m')) + return AVERROR_INVALIDDATA; + if (avio_rl32(pb) != MKTAG('z','l','i','b')) { av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !"); - return -1; + return AVERROR_INVALIDDATA; } - get_be32(pb); /* cmvd atom */ - if (get_le32(pb) != MKTAG('c','m','v','d')) - return -1; - moov_len = get_be32(pb); /* uncompressed size */ + avio_rb32(pb); /* cmvd atom */ + if (avio_rl32(pb) != MKTAG('c','m','v','d')) + return AVERROR_INVALIDDATA; + moov_len = avio_rb32(pb); /* uncompressed size */ cmov_len = atom.size - 6 * 4; cmov_data = av_malloc(cmov_len); @@ -2121,16 +2317,13 @@ static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom) av_free(cmov_data); return AVERROR(ENOMEM); } - get_buffer(pb, cmov_data, cmov_len); - if(uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK) + 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(ffio_init_context(&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.size = moov_len; -#ifdef DEBUG -// { int fd = open("/tmp/uncompheader.mov", O_WRONLY | O_CREAT); write(fd, moov_data, moov_len); close(fd); } -#endif ret = mov_read_default(c, &ctx, atom); free_and_return: av_free(moov_data); @@ -2138,7 +2331,7 @@ free_and_return: return ret; #else av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n"); - return -1; + return AVERROR(ENOSYS); #endif } @@ -2146,30 +2339,36 @@ free_and_return: static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom) { MOVStreamContext *sc; - int i, edit_count; + int i, edit_count, version; if (c->fc->nb_streams < 1) 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 */ + version = 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; + if ((uint64_t)edit_count*12+8 > atom.size) + return AVERROR_INVALIDDATA; - for(i=0; i= -1) { sc->time_offset = time != -1 ? time : -duration; } } - if(edit_count > 1) + if (edit_count > 1) av_log(c->fc, AV_LOG_WARNING, "multiple edit list entries, " "a/v desync might occur, patch welcome\n"); @@ -2187,7 +2386,7 @@ static const MOVParseTableEntry mov_default_parse_table[] = { { MKTAG('e','d','t','s'), mov_read_default }, { MKTAG('e','l','s','t'), mov_read_elst }, { MKTAG('e','n','d','a'), mov_read_enda }, -{ MKTAG('f','i','e','l'), mov_read_extradata }, +{ MKTAG('f','i','e','l'), mov_read_fiel }, { MKTAG('f','t','y','p'), mov_read_ftyp }, { MKTAG('g','l','b','l'), mov_read_glbl }, { MKTAG('h','d','l','r'), mov_read_hdlr }, @@ -2229,7 +2428,9 @@ static const MOVParseTableEntry mov_default_parse_table[] = { { 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('w','f','e','x'), mov_read_wfex }, { MKTAG('c','m','o','v'), mov_read_cmov }, +{ MKTAG('c','h','a','n'), mov_read_chan }, /* channel layout */ { 0, NULL } }; @@ -2241,7 +2442,7 @@ static int mov_probe(AVProbeData *p) /* check file header */ offset = 0; - for(;;) { + for (;;) { /* ignore invalid offset */ if ((offset + 8) > (unsigned int)p->buf_size) return score; @@ -2275,7 +2476,6 @@ static int mov_probe(AVProbeData *p) return score; } } - return score; } // must be done after parsing all trak because there's no order requirement @@ -2299,7 +2499,7 @@ static void mov_read_chapters(AVFormatContext *s) st->discard = AVDISCARD_ALL; sc = st->priv_data; - cur_pos = url_ftell(sc->pb); + cur_pos = avio_tell(sc->pb); for (i = 0; i < st->nb_index_entries; i++) { AVIndexEntry *sample = &st->index_entries[i]; @@ -2308,13 +2508,13 @@ static void mov_read_chapters(AVFormatContext *s) uint16_t ch; int len, title_len; - if (url_fseek(sc->pb, sample->pos, SEEK_SET) != sample->pos) { + 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 = get_be16(sc->pb); + len = avio_rb16(sc->pb); if (len > sample->size-2) continue; title_len = 2*len + 1; @@ -2324,21 +2524,28 @@ static void mov_read_chapters(AVFormatContext *s) // 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 = get_be16(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); - get_strz(sc->pb, title + 2, len - 1); + if (!len) { + title[0] = 0; + } else { + 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); + if (len == 1 || len == 2) + title[len] = 0; + else + avio_get_str(sc->pb, len - 2, title + 2, title_len - 2); + } } - ff_new_chapter(s, i, st->time_base, sample->timestamp, end, title); + avpriv_new_chapter(s, i, st->time_base, sample->timestamp, end, title); av_freep(&title); } finish: - url_fseek(sc->pb, cur_pos, SEEK_SET); + avio_seek(sc->pb, cur_pos, SEEK_SET); } static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap) @@ -2350,8 +2557,8 @@ static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap) 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); + if (pb->seekable) + atom.size = avio_size(pb); else atom.size = INT64_MAX; @@ -2362,11 +2569,11 @@ static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap) } if (!mov->found_moov) { av_log(s, AV_LOG_ERROR, "moov atom not found\n"); - return -1; + return AVERROR_INVALIDDATA; } - av_dlog(mov->fc, "on_parse_exit_offset=%lld\n", url_ftell(pb)); + av_dlog(mov->fc, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb)); - if (!url_is_streamed(pb) && mov->chapter_track > 0) + if (pb->seekable && mov->chapter_track > 0) mov_read_chapters(s); return 0; @@ -2384,8 +2591,8 @@ static AVIndexEntry *mov_find_next_sample(AVFormatContext *s, AVStream **st) AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample]; int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale); 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) && + if (!sample || (!s->pb->seekable && current_sample->pos < sample->pos) || + (s->pb->seekable && ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb && ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) || (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) { @@ -2409,11 +2616,11 @@ static int mov_read_packet(AVFormatContext *s, AVPacket *pkt) sample = mov_find_next_sample(s, &st); if (!sample) { mov->found_mdat = 0; - if (!url_is_streamed(s->pb) || + if (s->pb->seekable|| mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX }) < 0 || - url_feof(s->pb)) + s->pb->eof_reached) return AVERROR_EOF; - av_dlog(s, "read fragments, offset 0x%llx\n", url_ftell(s->pb)); + av_dlog(s, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb)); goto retry; } sc = st->priv_data; @@ -2421,20 +2628,31 @@ static int mov_read_packet(AVFormatContext *s, AVPacket *pkt) 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; + return AVERROR_INVALIDDATA; } ret = av_get_packet(sc->pb, pkt, sample->size); if (ret < 0) return ret; + if (sc->has_palette) { + uint8_t *pal; + + pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE); + if (!pal) { + av_log(mov->fc, AV_LOG_ERROR, "Cannot append palette to packet\n"); + } else { + memcpy(pal, sc->palette, AVPALETTE_SIZE); + sc->has_palette = 0; + } + } #if CONFIG_DV_DEMUXER if (mov->dv_demux && sc->dv_audio_container) { - dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size); + avpriv_dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size); av_free(pkt->data); pkt->size = 0; - ret = dv_get_packet(mov->dv_demux, pkt); + ret = avpriv_dv_get_packet(mov->dv_demux, pkt); if (ret < 0) return ret; } @@ -2480,7 +2698,7 @@ static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, 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; + return AVERROR_INVALIDDATA; sc->current_sample = sample; av_dlog(s, "stream %d, found sample %d\n", st->index, sc->current_sample); /* adjust ctts index */ @@ -2507,14 +2725,14 @@ static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_ti int i; if (stream_index >= s->nb_streams) - return -1; + return AVERROR_INVALIDDATA; if (sample_time < 0) sample_time = 0; st = s->streams[stream_index]; sample = mov_seek_stream(s, st, sample_time, flags); if (sample < 0) - return -1; + return sample; /* adjust seek timestamp to found sample timestamp */ seek_timestamp = st->index_entries[sample].timestamp; @@ -2546,13 +2764,11 @@ static int mov_read_close(AVFormatContext *s) } av_freep(&sc->drefs); if (sc->pb && sc->pb != s->pb) - url_fclose(sc->pb); - - av_freep(&st->codec->palctrl); + avio_close(sc->pb); } if (mov->dv_demux) { - for(i = 0; i < mov->dv_fctx->nb_streams; i++) { + for (i = 0; i < mov->dv_fctx->nb_streams; i++) { av_freep(&mov->dv_fctx->streams[i]->codec); av_freep(&mov->dv_fctx->streams[i]); } @@ -2566,12 +2782,12 @@ static int mov_read_close(AVFormatContext *s) } AVInputFormat ff_mov_demuxer = { - "mov,mp4,m4a,3gp,3g2,mj2", - NULL_IF_CONFIG_SMALL("QuickTime/MPEG-4/Motion JPEG 2000 format"), - sizeof(MOVContext), - mov_probe, - mov_read_header, - mov_read_packet, - mov_read_close, - mov_read_seek, + .name = "mov,mp4,m4a,3gp,3g2,mj2", + .long_name = NULL_IF_CONFIG_SMALL("QuickTime/MPEG-4/Motion JPEG 2000 format"), + .priv_data_size = sizeof(MOVContext), + .read_probe = mov_probe, + .read_header = mov_read_header, + .read_packet = mov_read_packet, + .read_close = mov_read_close, + .read_seek = mov_read_seek, };