| /* |
| * MOV demuxer |
| * Copyright (c) 2001 Fabrice Bellard |
| * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com> |
| * |
| * first version by Francois Revol <revol@free.fr> |
| * seek function by Gael Chardon <gael.dev@4now.net> |
| * |
| * This file is part of FFmpeg. |
| * |
| * FFmpeg is free software; you can redistribute it and/or |
| * modify it under the terms of the GNU Lesser General Public |
| * License as published by the Free Software Foundation; either |
| * version 2.1 of the License, or (at your option) any later version. |
| * |
| * FFmpeg is distributed in the hope that it will be useful, |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| * Lesser General Public License for more details. |
| * |
| * You should have received a copy of the GNU Lesser General Public |
| * License along with FFmpeg; if not, write to the Free Software |
| * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| */ |
| |
| #include <inttypes.h> |
| #include <limits.h> |
| #include <stdint.h> |
| |
| #include "libavutil/attributes.h" |
| #include "libavutil/channel_layout.h" |
| #include "libavutil/internal.h" |
| #include "libavutil/intreadwrite.h" |
| #include "libavutil/intfloat.h" |
| #include "libavutil/mathematics.h" |
| #include "libavutil/time_internal.h" |
| #include "libavutil/avstring.h" |
| #include "libavutil/dict.h" |
| #include "libavutil/display.h" |
| #include "libavutil/opt.h" |
| #include "libavutil/aes.h" |
| #include "libavutil/aes_ctr.h" |
| #include "libavutil/sha.h" |
| #include "libavutil/timecode.h" |
| #include "libavcodec/ac3tab.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" |
| #include "replaygain.h" |
| |
| #if CONFIG_ZLIB |
| #include <zlib.h> |
| #endif |
| |
| #include "qtpalette.h" |
| |
| /* those functions parse an atom */ |
| /* links atom IDs to parse functions */ |
| typedef struct MOVParseTableEntry { |
| uint32_t type; |
| int (*parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom); |
| } MOVParseTableEntry; |
| |
| static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom); |
| static int mov_read_mfra(MOVContext *c, AVIOContext *f); |
| |
| static int mov_metadata_track_or_disc_number(MOVContext *c, AVIOContext *pb, |
| unsigned len, const char *key) |
| { |
| char buf[16]; |
| |
| short current, total = 0; |
| avio_rb16(pb); // unknown |
| current = avio_rb16(pb); |
| if (len >= 6) |
| total = avio_rb16(pb); |
| if (!total) |
| snprintf(buf, sizeof(buf), "%d", current); |
| else |
| snprintf(buf, sizeof(buf), "%d/%d", current, total); |
| c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED; |
| av_dict_set(&c->fc->metadata, key, buf, 0); |
| |
| return 0; |
| } |
| |
| static int mov_metadata_int8_bypass_padding(MOVContext *c, AVIOContext *pb, |
| unsigned len, const char *key) |
| { |
| /* bypass padding bytes */ |
| avio_r8(pb); |
| avio_r8(pb); |
| avio_r8(pb); |
| |
| c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED; |
| av_dict_set_int(&c->fc->metadata, key, avio_r8(pb), 0); |
| |
| return 0; |
| } |
| |
| static int mov_metadata_int8_no_padding(MOVContext *c, AVIOContext *pb, |
| unsigned len, const char *key) |
| { |
| c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED; |
| av_dict_set_int(&c->fc->metadata, key, avio_r8(pb), 0); |
| |
| return 0; |
| } |
| |
| static int mov_metadata_gnre(MOVContext *c, AVIOContext *pb, |
| unsigned len, const char *key) |
| { |
| short genre; |
| |
| avio_r8(pb); // unknown |
| |
| genre = avio_r8(pb); |
| if (genre < 1 || genre > ID3v1_GENRE_MAX) |
| return 0; |
| c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED; |
| av_dict_set(&c->fc->metadata, key, ff_id3v1_genre_str[genre-1], 0); |
| |
| return 0; |
| } |
| |
| 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 if (p < end) |
| PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;); |
| } |
| *p = 0; |
| return p - dst; |
| } |
| |
| static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len) |
| { |
| AVPacket pkt; |
| AVStream *st; |
| MOVStreamContext *sc; |
| enum AVCodecID id; |
| int ret; |
| |
| switch (type) { |
| case 0xd: id = AV_CODEC_ID_MJPEG; break; |
| case 0xe: id = AV_CODEC_ID_PNG; break; |
| case 0x1b: id = AV_CODEC_ID_BMP; break; |
| default: |
| av_log(c->fc, AV_LOG_WARNING, "Unknown cover type: 0x%x.\n", type); |
| avio_skip(pb, len); |
| return 0; |
| } |
| |
| st = avformat_new_stream(c->fc, NULL); |
| if (!st) |
| return AVERROR(ENOMEM); |
| sc = av_mallocz(sizeof(*sc)); |
| if (!sc) |
| return AVERROR(ENOMEM); |
| st->priv_data = sc; |
| |
| ret = av_get_packet(pb, &pkt, len); |
| if (ret < 0) |
| return ret; |
| |
| if (pkt.size >= 8 && id != AV_CODEC_ID_BMP) { |
| if (AV_RB64(pkt.data) == 0x89504e470d0a1a0a) { |
| id = AV_CODEC_ID_PNG; |
| } else { |
| id = AV_CODEC_ID_MJPEG; |
| } |
| } |
| |
| st->disposition |= AV_DISPOSITION_ATTACHED_PIC; |
| |
| st->attached_pic = pkt; |
| st->attached_pic.stream_index = st->index; |
| st->attached_pic.flags |= AV_PKT_FLAG_KEY; |
| |
| st->codec->codec_type = AVMEDIA_TYPE_VIDEO; |
| st->codec->codec_id = id; |
| |
| return 0; |
| } |
| |
| static int mov_metadata_loci(MOVContext *c, AVIOContext *pb, unsigned len) |
| { |
| char language[4] = { 0 }; |
| char buf[200], place[100]; |
| uint16_t langcode = 0; |
| double longitude, latitude, altitude; |
| const char *key = "location"; |
| |
| if (len < 4 + 2 + 1 + 1 + 4 + 4 + 4) { |
| av_log(c->fc, AV_LOG_ERROR, "loci too short\n"); |
| return AVERROR_INVALIDDATA; |
| } |
| |
| avio_skip(pb, 4); // version+flags |
| langcode = avio_rb16(pb); |
| ff_mov_lang_to_iso639(langcode, language); |
| len -= 6; |
| |
| len -= avio_get_str(pb, len, place, sizeof(place)); |
| if (len < 1) { |
| av_log(c->fc, AV_LOG_ERROR, "place name too long\n"); |
| return AVERROR_INVALIDDATA; |
| } |
| avio_skip(pb, 1); // role |
| len -= 1; |
| |
| if (len < 12) { |
| av_log(c->fc, AV_LOG_ERROR, "no space for coordinates left (%d)\n", len); |
| return AVERROR_INVALIDDATA; |
| } |
| longitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16); |
| latitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16); |
| altitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16); |
| |
| // Try to output in the same format as the ?xyz field |
| snprintf(buf, sizeof(buf), "%+08.4f%+09.4f", latitude, longitude); |
| if (altitude) |
| av_strlcatf(buf, sizeof(buf), "%+f", altitude); |
| av_strlcatf(buf, sizeof(buf), "/%s", place); |
| |
| if (*language && strcmp(language, "und")) { |
| char key2[16]; |
| snprintf(key2, sizeof(key2), "%s-%s", key, language); |
| av_dict_set(&c->fc->metadata, key2, buf, 0); |
| } |
| c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED; |
| return av_dict_set(&c->fc->metadata, key, buf, 0); |
| } |
| |
| static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
| { |
| char tmp_key[5]; |
| char key2[32], language[4] = {0}; |
| char *str = NULL; |
| const char *key = NULL; |
| uint16_t langcode = 0; |
| uint32_t data_type = 0, str_size, str_size_alloc; |
| int (*parse)(MOVContext*, AVIOContext*, unsigned, const char*) = NULL; |
| int raw = 0; |
| int num = 0; |
| |
| switch (atom.type) { |
| case MKTAG( '@','P','R','M'): key = "premiere_version"; raw = 1; break; |
| case MKTAG( '@','P','R','Q'): key = "quicktime_version"; raw = 1; break; |
| case MKTAG( 'X','M','P','_'): |
| if (c->export_xmp) { key = "xmp"; raw = 1; } break; |
| case MKTAG( 'a','A','R','T'): key = "album_artist"; break; |
| case MKTAG( 'a','k','I','D'): key = "account_type"; |
| parse = mov_metadata_int8_no_padding; break; |
| case MKTAG( 'a','p','I','D'): key = "account_id"; break; |
| case MKTAG( 'c','a','t','g'): key = "category"; break; |
| case MKTAG( 'c','p','i','l'): key = "compilation"; |
| parse = mov_metadata_int8_no_padding; break; |
| case MKTAG( 'c','p','r','t'): key = "copyright"; break; |
| case MKTAG( 'd','e','s','c'): key = "description"; break; |
| case MKTAG( 'd','i','s','k'): key = "disc"; |
| parse = mov_metadata_track_or_disc_number; break; |
| case MKTAG( 'e','g','i','d'): key = "episode_uid"; |
| parse = mov_metadata_int8_no_padding; break; |
| case MKTAG( 'g','n','r','e'): key = "genre"; |
| parse = mov_metadata_gnre; break; |
| case MKTAG( 'h','d','v','d'): key = "hd_video"; |
| parse = mov_metadata_int8_no_padding; break; |
| case MKTAG( 'k','e','y','w'): key = "keywords"; break; |
| case MKTAG( 'l','d','e','s'): key = "synopsis"; break; |
| case MKTAG( 'l','o','c','i'): |
| return mov_metadata_loci(c, pb, atom.size); |
| case MKTAG( 'p','c','s','t'): key = "podcast"; |
| parse = mov_metadata_int8_no_padding; break; |
| case MKTAG( 'p','g','a','p'): key = "gapless_playback"; |
| parse = mov_metadata_int8_no_padding; break; |
| case MKTAG( 'p','u','r','d'): key = "purchase_date"; break; |
| case MKTAG( 'r','t','n','g'): key = "rating"; |
| parse = mov_metadata_int8_no_padding; break; |
| case MKTAG( 's','o','a','a'): key = "sort_album_artist"; break; |
| case MKTAG( 's','o','a','l'): key = "sort_album"; break; |
| case MKTAG( 's','o','a','r'): key = "sort_artist"; break; |
| case MKTAG( 's','o','c','o'): key = "sort_composer"; break; |
| case MKTAG( 's','o','n','m'): key = "sort_name"; break; |
| case MKTAG( 's','o','s','n'): key = "sort_show"; break; |
| case MKTAG( 's','t','i','k'): key = "media_type"; |
| parse = mov_metadata_int8_no_padding; break; |
| case MKTAG( 't','r','k','n'): key = "track"; |
| parse = mov_metadata_track_or_disc_number; break; |
| case MKTAG( 't','v','e','n'): key = "episode_id"; break; |
| case MKTAG( 't','v','e','s'): key = "episode_sort"; |
| parse = mov_metadata_int8_bypass_padding; break; |
| case MKTAG( 't','v','n','n'): key = "network"; break; |
| case MKTAG( 't','v','s','h'): key = "show"; break; |
| case MKTAG( 't','v','s','n'): key = "season_number"; |
| parse = mov_metadata_int8_bypass_padding; break; |
| case MKTAG(0xa9,'A','R','T'): key = "artist"; break; |
| case MKTAG(0xa9,'P','R','D'): key = "producer"; break; |
| case MKTAG(0xa9,'a','l','b'): key = "album"; break; |
| case MKTAG(0xa9,'a','u','t'): key = "artist"; break; |
| case MKTAG(0xa9,'c','h','p'): key = "chapter"; break; |
| case MKTAG(0xa9,'c','m','t'): key = "comment"; break; |
| case MKTAG(0xa9,'c','o','m'): key = "composer"; break; |
| case MKTAG(0xa9,'c','p','y'): key = "copyright"; break; |
| case MKTAG(0xa9,'d','a','y'): key = "date"; break; |
| case MKTAG(0xa9,'d','i','r'): key = "director"; break; |
| case MKTAG(0xa9,'d','i','s'): key = "disclaimer"; break; |
| case MKTAG(0xa9,'e','d','1'): key = "edit_date"; break; |
| case MKTAG(0xa9,'e','n','c'): key = "encoder"; break; |
| case MKTAG(0xa9,'f','m','t'): key = "original_format"; break; |
| case MKTAG(0xa9,'g','e','n'): key = "genre"; break; |
| case MKTAG(0xa9,'g','r','p'): key = "grouping"; break; |
| case MKTAG(0xa9,'h','s','t'): key = "host_computer"; break; |
| case MKTAG(0xa9,'i','n','f'): key = "comment"; break; |
| case MKTAG(0xa9,'l','y','r'): key = "lyrics"; break; |
| case MKTAG(0xa9,'m','a','k'): key = "make"; break; |
| case MKTAG(0xa9,'m','o','d'): key = "model"; break; |
| case MKTAG(0xa9,'n','a','m'): key = "title"; break; |
| case MKTAG(0xa9,'o','p','e'): key = "original_artist"; break; |
| case MKTAG(0xa9,'p','r','d'): key = "producer"; break; |
| case MKTAG(0xa9,'p','r','f'): key = "performers"; break; |
| case MKTAG(0xa9,'r','e','q'): key = "playback_requirements"; break; |
| case MKTAG(0xa9,'s','r','c'): key = "original_source"; break; |
| case MKTAG(0xa9,'s','t','3'): key = "subtitle"; break; |
| case MKTAG(0xa9,'s','w','r'): key = "encoder"; break; |
| case MKTAG(0xa9,'t','o','o'): key = "encoder"; break; |
| case MKTAG(0xa9,'t','r','k'): key = "track"; break; |
| case MKTAG(0xa9,'u','r','l'): key = "URL"; break; |
| case MKTAG(0xa9,'w','r','n'): key = "warning"; break; |
| case MKTAG(0xa9,'w','r','t'): key = "composer"; break; |
| case MKTAG(0xa9,'x','y','z'): key = "location"; break; |
| } |
| retry: |
| if (c->itunes_metadata && atom.size > 8) { |
| int data_size = avio_rb32(pb); |
| int tag = avio_rl32(pb); |
| if (tag == MKTAG('d','a','t','a') && data_size <= atom.size) { |
| data_type = avio_rb32(pb); // type |
| avio_rb32(pb); // unknown |
| str_size = data_size - 16; |
| atom.size -= 16; |
| |
| if (atom.type == MKTAG('c', 'o', 'v', 'r')) { |
| int ret = mov_read_covr(c, pb, data_type, str_size); |
| if (ret < 0) { |
| av_log(c->fc, AV_LOG_ERROR, "Error parsing cover art.\n"); |
| } |
| return ret; |
| } else if (!key && c->found_hdlr_mdta && c->meta_keys) { |
| uint32_t index = AV_RB32(&atom.type); |
| if (index < c->meta_keys_count) { |
| key = c->meta_keys[index]; |
| } else { |
| av_log(c->fc, AV_LOG_WARNING, |
| "The index of 'data' is out of range: %d >= %d.\n", |
| index, c->meta_keys_count); |
| } |
| } |
| } else return 0; |
| } else if (atom.size > 4 && key && !c->itunes_metadata && !raw) { |
| str_size = avio_rb16(pb); // string length |
| if (str_size > atom.size) { |
| raw = 1; |
| avio_seek(pb, -2, SEEK_CUR); |
| av_log(c->fc, AV_LOG_WARNING, "UDTA parsing failed retrying raw\n"); |
| goto retry; |
| } |
| langcode = avio_rb16(pb); |
| ff_mov_lang_to_iso639(langcode, language); |
| atom.size -= 4; |
| } else |
| str_size = atom.size; |
| |
| if (c->export_all && !key) { |
| snprintf(tmp_key, 5, "%.4s", (char*)&atom.type); |
| key = tmp_key; |
| } |
| |
| if (!key) |
| return 0; |
| if (atom.size < 0 || str_size >= INT_MAX/2) |
| return AVERROR_INVALIDDATA; |
| |
| // Allocates enough space if data_type is a float32 number, otherwise |
| // worst-case requirement for output string in case of utf8 coded input |
| num = (data_type == 23); |
| str_size_alloc = (num ? 512 : (raw ? str_size : str_size * 2)) + 1; |
| str = av_mallocz(str_size_alloc); |
| if (!str) |
| return AVERROR(ENOMEM); |
| |
| if (parse) |
| parse(c, pb, str_size, key); |
| else { |
| if (!raw && (data_type == 3 || (data_type == 0 && (langcode < 0x400 || langcode == 0x7fff)))) { // MAC Encoded |
| mov_read_mac_string(c, pb, str_size, str, str_size_alloc); |
| } else if (data_type == 23 && str_size >= 4) { // BE float32 |
| float val = av_int2float(avio_rb32(pb)); |
| if (snprintf(str, str_size_alloc, "%f", val) >= str_size_alloc) { |
| av_log(c->fc, AV_LOG_ERROR, |
| "Failed to store the float32 number (%f) in string.\n", val); |
| av_free(str); |
| return AVERROR_INVALIDDATA; |
| } |
| } else { |
| int ret = ffio_read_size(pb, str, str_size); |
| if (ret < 0) { |
| av_free(str); |
| return ret; |
| } |
| str[str_size] = 0; |
| } |
| c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED; |
| av_dict_set(&c->fc->metadata, key, str, 0); |
| if (*language && strcmp(language, "und")) { |
| snprintf(key2, sizeof(key2), "%s-%s", key, language); |
| av_dict_set(&c->fc->metadata, key2, str, 0); |
| } |
| if (!strcmp(key, "encoder")) { |
| int major, minor, micro; |
| if (sscanf(str, "HandBrake %d.%d.%d", &major, &minor, µ) == 3) { |
| c->handbrake_version = 1000000*major + 1000*minor + micro; |
| } |
| } |
| } |
| av_log(c->fc, AV_LOG_TRACE, "lang \"%3s\" ", language); |
| av_log(c->fc, AV_LOG_TRACE, "tag \"%s\" value \"%s\" atom \"%.4s\" %d %"PRId64"\n", |
| key, str, (char*)&atom.type, str_size_alloc, atom.size); |
| |
| av_freep(&str); |
| return 0; |
| } |
| |
| 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]; |
| int ret; |
| |
| if (c->ignore_chapters) |
| return 0; |
| |
| 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; |
| |
| ret = ffio_read_size(pb, str, str_len); |
| if (ret < 0) |
| return ret; |
| str[str_len] = 0; |
| avpriv_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str); |
| } |
| return 0; |
| } |
| |
| #define MIN_DATA_ENTRY_BOX_SIZE 12 |
| static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
| { |
| AVStream *st; |
| MOVStreamContext *sc; |
| int entries, i, j; |
| |
| if (c->fc->nb_streams < 1) |
| return 0; |
| st = c->fc->streams[c->fc->nb_streams-1]; |
| sc = st->priv_data; |
| |
| avio_rb32(pb); // version + flags |
| entries = avio_rb32(pb); |
| if (entries > (atom.size - 1) / MIN_DATA_ENTRY_BOX_SIZE + 1 || |
| entries >= UINT_MAX / sizeof(*sc->drefs)) |
| return AVERROR_INVALIDDATA; |
| av_free(sc->drefs); |
| sc->drefs_count = 0; |
| sc->drefs = av_mallocz(entries * sizeof(*sc->drefs)); |
| if (!sc->drefs) |
| return AVERROR(ENOMEM); |
| sc->drefs_count = entries; |
| |
| for (i = 0; i < entries; i++) { |
| MOVDref *dref = &sc->drefs[i]; |
| uint32_t size = avio_rb32(pb); |
| int64_t next = avio_tell(pb) + size - 4; |
| |
| if (size < 12) |
| return AVERROR_INVALIDDATA; |
| |
| dref->type = avio_rl32(pb); |
| avio_rb32(pb); // version + flags |
| av_log(c->fc, AV_LOG_TRACE, "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; |
| int16_t type; |
| int ret; |
| |
| avio_skip(pb, 10); |
| |
| volume_len = avio_r8(pb); |
| volume_len = FFMIN(volume_len, 27); |
| ret = ffio_read_size(pb, dref->volume, 27); |
| if (ret < 0) |
| return ret; |
| 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); |
| |
| len = avio_r8(pb); |
| len = FFMIN(len, 63); |
| ret = ffio_read_size(pb, dref->filename, 63); |
| if (ret < 0) |
| return ret; |
| dref->filename[len] = 0; |
| av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len); |
| |
| 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; ) { |
| if(avio_feof(pb)) |
| return AVERROR_EOF; |
| 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; |
| if (type == 2) { // absolute path |
| av_free(dref->path); |
| dref->path = av_mallocz(len+1); |
| if (!dref->path) |
| return AVERROR(ENOMEM); |
| |
| ret = ffio_read_size(pb, dref->path, len); |
| if (ret < 0) { |
| av_freep(&dref->path); |
| return ret; |
| } |
| 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; |
| } |
| for (j = 0; j < len; j++) |
| if (dref->path[j] == ':' || dref->path[j] == 0) |
| 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); |
| |
| ret = ffio_read_size(pb, dref->dir, len); |
| if (ret < 0) { |
| av_freep(&dref->dir); |
| return ret; |
| } |
| 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 |
| avio_skip(pb, len); |
| } |
| } else { |
| av_log(c->fc, AV_LOG_DEBUG, "Unknown dref type 0x08%x size %d\n", |
| dref->type, size); |
| entries--; |
| i--; |
| } |
| avio_seek(pb, next, SEEK_SET); |
| } |
| return 0; |
| } |
| |
| static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
| { |
| AVStream *st; |
| uint32_t type; |
| uint32_t av_unused ctype; |
| int64_t title_size; |
| char *title_str; |
| int ret; |
| |
| avio_r8(pb); /* version */ |
| avio_rb24(pb); /* flags */ |
| |
| /* component type */ |
| ctype = avio_rl32(pb); |
| type = avio_rl32(pb); /* component subtype */ |
| |
| av_log(c->fc, AV_LOG_TRACE, "ctype= %.4s (0x%08x)\n", (char*)&ctype, ctype); |
| av_log(c->fc, AV_LOG_TRACE, "stype= %.4s\n", (char*)&type); |
| |
| if (c->trak_index < 0) { // meta not inside a trak |
| if (type == MKTAG('m','d','t','a')) { |
| c->found_hdlr_mdta = 1; |
| } |
| return 0; |
| } |
| |
| st = c->fc->streams[c->fc->nb_streams-1]; |
| |
| if (type == MKTAG('v','i','d','e')) |
| st->codec->codec_type = AVMEDIA_TYPE_VIDEO; |
| else if (type == MKTAG('s','o','u','n')) |
| st->codec->codec_type = AVMEDIA_TYPE_AUDIO; |
| else if (type == MKTAG('m','1','a',' ')) |
| st->codec->codec_id = AV_CODEC_ID_MP2; |
| else if ((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p'))) |
| st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; |
| |
| avio_rb32(pb); /* component manufacture */ |
| avio_rb32(pb); /* component flags */ |
| avio_rb32(pb); /* component flags mask */ |
| |
| title_size = atom.size - 24; |
| if (title_size > 0) { |
| title_str = av_malloc(title_size + 1); /* Add null terminator */ |
| if (!title_str) |
| return AVERROR(ENOMEM); |
| |
| ret = ffio_read_size(pb, title_str, title_size); |
| if (ret < 0) { |
| av_freep(&title_str); |
| return ret; |
| } |
| title_str[title_size] = 0; |
| if (title_str[0]) { |
| int off = (!c->isom && title_str[0] == title_size - 1); |
| av_dict_set(&st->metadata, "handler_name", title_str + off, 0); |
| } |
| av_freep(&title_str); |
| } |
| |
| return 0; |
| } |
| |
| int ff_mov_read_esds(AVFormatContext *fc, AVIOContext *pb) |
| { |
| AVStream *st; |
| int tag; |
| |
| if (fc->nb_streams < 1) |
| return 0; |
| st = fc->streams[fc->nb_streams-1]; |
| |
| avio_rb32(pb); /* version + flags */ |
| ff_mp4_read_descr(fc, pb, &tag); |
| if (tag == MP4ESDescrTag) { |
| ff_mp4_parse_es_descr(pb, NULL); |
| } else |
| avio_rb16(pb); /* ID */ |
| |
| 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, AVIOContext *pb, MOVAtom atom) |
| { |
| return ff_mov_read_esds(c->fc, pb); |
| } |
| |
| static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
| { |
| AVStream *st; |
| enum AVAudioServiceType *ast; |
| int ac3info, acmod, lfeon, bsmod; |
| |
| if (c->fc->nb_streams < 1) |
| return 0; |
| st = c->fc->streams[c->fc->nb_streams-1]; |
| |
| ast = (enum AVAudioServiceType*)av_stream_new_side_data(st, AV_PKT_DATA_AUDIO_SERVICE_TYPE, |
| sizeof(*ast)); |
| if (!ast) |
| return AVERROR(ENOMEM); |
| |
| 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->channel_layout = avpriv_ac3_channel_layout_tab[acmod]; |
| if (lfeon) |
| st->codec->channel_layout |= AV_CH_LOW_FREQUENCY; |
| *ast = bsmod; |
| if (st->codec->channels > 1 && bsmod == 0x7) |
| *ast = AV_AUDIO_SERVICE_TYPE_KARAOKE; |
| |
| st->codec->audio_service_type = *ast; |
| |
| return 0; |
| } |
| |
| static int mov_read_dec3(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
| { |
| AVStream *st; |
| enum AVAudioServiceType *ast; |
| int eac3info, acmod, lfeon, bsmod; |
| |
| if (c->fc->nb_streams < 1) |
| return 0; |
| st = c->fc->streams[c->fc->nb_streams-1]; |
| |
| ast = (enum AVAudioServiceType*)av_stream_new_side_data(st, AV_PKT_DATA_AUDIO_SERVICE_TYPE, |
| sizeof(*ast)); |
| if (!ast) |
| return AVERROR(ENOMEM); |
| |
| /* No need to parse fields for additional independent substreams and its |
| * associated dependent substreams since libavcodec's E-AC-3 decoder |
| * does not support them yet. */ |
| avio_rb16(pb); /* data_rate and num_ind_sub */ |
| eac3info = avio_rb24(pb); |
| bsmod = (eac3info >> 12) & 0x1f; |
| acmod = (eac3info >> 9) & 0x7; |
| lfeon = (eac3info >> 8) & 0x1; |
| st->codec->channel_layout = avpriv_ac3_channel_layout_tab[acmod]; |
| if (lfeon) |
| st->codec->channel_layout |= AV_CH_LOW_FREQUENCY; |
| st->codec->channels = av_get_channel_layout_nb_channels(st->codec->channel_layout); |
| *ast = bsmod; |
| if (st->codec->channels > 1 && bsmod == 0x7) |
| *ast = AV_AUDIO_SERVICE_TYPE_KARAOKE; |
| |
| st->codec->audio_service_type = *ast; |
| |
| return 0; |
| } |
| |
| static int mov_read_ddts(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
| { |
| const uint32_t ddts_size = 20; |
| AVStream *st = NULL; |
| uint8_t *buf = NULL; |
| uint32_t frame_duration_code = 0; |
| uint32_t channel_layout_code = 0; |
| GetBitContext gb; |
| |
| buf = av_malloc(ddts_size + FF_INPUT_BUFFER_PADDING_SIZE); |
| if (!buf) { |
| return AVERROR(ENOMEM); |
| } |
| if (avio_read(pb, buf, ddts_size) < ddts_size) { |
| av_free(buf); |
| return AVERROR_INVALIDDATA; |
| } |
| |
| init_get_bits(&gb, buf, 8*ddts_size); |
| |
| if (c->fc->nb_streams < 1) { |
| return 0; |
| } |
| st = c->fc->streams[c->fc->nb_streams-1]; |
| |
| st->codec->sample_rate = get_bits_long(&gb, 32); |
| skip_bits_long(&gb, 32); /* max bitrate */ |
| st->codec->bit_rate = get_bits_long(&gb, 32); |
| st->codec->bits_per_coded_sample = get_bits(&gb, 8); |
| frame_duration_code = get_bits(&gb, 2); |
| skip_bits(&gb, 30); /* various fields */ |
| channel_layout_code = get_bits(&gb, 16); |
| |
| st->codec->frame_size = |
| (frame_duration_code == 0) ? 512 : |
| (frame_duration_code == 1) ? 1024 : |
| (frame_duration_code == 2) ? 2048 : |
| (frame_duration_code == 3) ? 4096 : 0; |
| |
| if (channel_layout_code > 0xff) { |
| av_log(c->fc, AV_LOG_WARNING, "Unsupported DTS audio channel layout"); |
| } |
| st->codec->channel_layout = |
| ((channel_layout_code & 0x1) ? AV_CH_FRONT_CENTER : 0) | |
| ((channel_layout_code & 0x2) ? AV_CH_FRONT_LEFT : 0) | |
| ((channel_layout_code & 0x2) ? AV_CH_FRONT_RIGHT : 0) | |
| ((channel_layout_code & 0x4) ? AV_CH_SIDE_LEFT : 0) | |
| ((channel_layout_code & 0x4) ? AV_CH_SIDE_RIGHT : 0) | |
| ((channel_layout_code & 0x8) ? AV_CH_LOW_FREQUENCY : 0); |
| |
| st->codec->channels = av_get_channel_layout_nb_channels(st->codec->channel_layout); |
| |
| return 0; |
| } |
| |
| static int mov_read_chan(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]; |
| |
| if (atom.size < 16) |
| return 0; |
| |
| /* skip version and flags */ |
| avio_skip(pb, 4); |
| |
| ff_mov_read_chan(c->fc, pb, st, atom.size - 4); |
| |
| return 0; |
| } |
| |
| static int mov_read_wfex(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
| { |
| AVStream *st; |
| int ret; |
| |
| if (c->fc->nb_streams < 1) |
| return 0; |
| st = c->fc->streams[c->fc->nb_streams-1]; |
| |
| if ((ret = ff_get_wav_header(c->fc, pb, st->codec, atom.size, 0)) < 0) |
| av_log(c->fc, AV_LOG_WARNING, "get_wav_header failed\n"); |
| |
| return ret; |
| } |
| |
| static int mov_read_pasp(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
| { |
| 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 ((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) { |
| av_reduce(&st->sample_aspect_ratio.num, &st->sample_aspect_ratio.den, |
| num, den, 32767); |
| } |
| return 0; |
| } |
| |
| /* this atom contains actual media data */ |
| static int mov_read_mdat(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
| { |
| if (atom.size == 0) /* wrong one (MP4) */ |
| return 0; |
| c->found_mdat=1; |
| return 0; /* now go for moov */ |
| } |
| |
| #define DRM_BLOB_SIZE 56 |
| |
| static int mov_read_adrm(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
| { |
| uint8_t intermediate_key[20]; |
| uint8_t intermediate_iv[20]; |
| uint8_t input[64]; |
| uint8_t output[64]; |
| uint8_t file_checksum[20]; |
| uint8_t calculated_checksum[20]; |
| struct AVSHA *sha; |
| int i; |
| int ret = 0; |
| uint8_t *activation_bytes = c->activation_bytes; |
| uint8_t *fixed_key = c->audible_fixed_key; |
| |
| c->aax_mode = 1; |
| |
| sha = av_sha_alloc(); |
| if (!sha) |
| return AVERROR(ENOMEM); |
| c->aes_decrypt = av_aes_alloc(); |
| if (!c->aes_decrypt) { |
| ret = AVERROR(ENOMEM); |
| goto fail; |
| } |
| |
| /* drm blob processing */ |
| avio_read(pb, output, 8); // go to offset 8, absolute position 0x251 |
| avio_read(pb, input, DRM_BLOB_SIZE); |
| avio_read(pb, output, 4); // go to offset 4, absolute position 0x28d |
| avio_read(pb, file_checksum, 20); |
| |
| av_log(c->fc, AV_LOG_INFO, "[aax] file checksum == "); // required by external tools |
| for (i = 0; i < 20; i++) |
| av_log(sha, AV_LOG_INFO, "%02x", file_checksum[i]); |
| av_log(c->fc, AV_LOG_INFO, "\n"); |
| |
| /* verify activation data */ |
| if (!activation_bytes) { |
| av_log(c->fc, AV_LOG_WARNING, "[aax] activation_bytes option is missing!\n"); |
| ret = 0; /* allow ffprobe to continue working on .aax files */ |
| goto fail; |
| } |
| if (c->activation_bytes_size != 4) { |
| av_log(c->fc, AV_LOG_FATAL, "[aax] activation_bytes value needs to be 4 bytes!\n"); |
| ret = AVERROR(EINVAL); |
| goto fail; |
| } |
| |
| /* verify fixed key */ |
| if (c->audible_fixed_key_size != 16) { |
| av_log(c->fc, AV_LOG_FATAL, "[aax] audible_fixed_key value needs to be 16 bytes!\n"); |
| ret = AVERROR(EINVAL); |
| goto fail; |
| } |
| |
| /* AAX (and AAX+) key derivation */ |
| av_sha_init(sha, 160); |
| av_sha_update(sha, fixed_key, 16); |
| av_sha_update(sha, activation_bytes, 4); |
| av_sha_final(sha, intermediate_key); |
| av_sha_init(sha, 160); |
| av_sha_update(sha, fixed_key, 16); |
| av_sha_update(sha, intermediate_key, 20); |
| av_sha_update(sha, activation_bytes, 4); |
| av_sha_final(sha, intermediate_iv); |
| av_sha_init(sha, 160); |
| av_sha_update(sha, intermediate_key, 16); |
| av_sha_update(sha, intermediate_iv, 16); |
| av_sha_final(sha, calculated_checksum); |
| if (memcmp(calculated_checksum, file_checksum, 20)) { // critical error |
| av_log(c->fc, AV_LOG_ERROR, "[aax] mismatch in checksums!\n"); |
| ret = AVERROR_INVALIDDATA; |
| goto fail; |
| } |
| av_aes_init(c->aes_decrypt, intermediate_key, 128, 1); |
| av_aes_crypt(c->aes_decrypt, output, input, DRM_BLOB_SIZE >> 4, intermediate_iv, 1); |
| for (i = 0; i < 4; i++) { |
| // file data (in output) is stored in big-endian mode |
| if (activation_bytes[i] != output[3 - i]) { // critical error |
| av_log(c->fc, AV_LOG_ERROR, "[aax] error in drm blob decryption!\n"); |
| ret = AVERROR_INVALIDDATA; |
| goto fail; |
| } |
| } |
| memcpy(c->file_key, output + 8, 16); |
| memcpy(input, output + 26, 16); |
| av_sha_init(sha, 160); |
| av_sha_update(sha, input, 16); |
| av_sha_update(sha, c->file_key, 16); |
| av_sha_update(sha, fixed_key, 16); |
| av_sha_final(sha, c->file_iv); |
| |
| fail: |
| av_free(sha); |
| |
| return ret; |
| } |
| |
| // Audible AAX (and AAX+) bytestream decryption |
| static int aax_filter(uint8_t *input, int size, MOVContext *c) |
| { |
| int blocks = 0; |
| unsigned char iv[16]; |
| |
| memcpy(iv, c->file_iv, 16); // iv is overwritten |
| blocks = size >> 4; // trailing bytes are not encrypted! |
| av_aes_init(c->aes_decrypt, c->file_key, 128, 1); |
| av_aes_crypt(c->aes_decrypt, input, input, blocks, iv, 1); |
| |
| return 0; |
| } |
| |
| /* read major brand, minor version and compatible brands and store them as metadata */ |
| 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}; |
| int ret = ffio_read_size(pb, type, 4); |
| if (ret < 0) |
| return ret; |
| |
| if (strcmp(type, "qt ")) |
| c->isom = 1; |
| av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type); |
| av_dict_set(&c->fc->metadata, "major_brand", type, 0); |
| minor_ver = avio_rb32(pb); /* minor version */ |
| av_dict_set_int(&c->fc->metadata, "minor_version", minor_ver, 0); |
| |
| comp_brand_size = atom.size - 8; |
| if (comp_brand_size < 0) |
| return AVERROR_INVALIDDATA; |
| comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */ |
| if (!comp_brands_str) |
| return AVERROR(ENOMEM); |
| |
| ret = ffio_read_size(pb, comp_brands_str, comp_brand_size); |
| if (ret < 0) { |
| av_freep(&comp_brands_str); |
| return ret; |
| } |
| comp_brands_str[comp_brand_size] = 0; |
| av_dict_set(&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, AVIOContext *pb, MOVAtom atom) |
| { |
| int ret; |
| |
| if (c->found_moov) { |
| av_log(c->fc, AV_LOG_WARNING, "Found duplicated MOOV Atom. Skipped it\n"); |
| avio_skip(pb, atom.size); |
| return 0; |
| } |
| |
| 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; |
| return 0; /* now go for mdat */ |
| } |
| |
| static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
| { |
| if (!c->has_looked_for_mfra && c->use_mfra_for > 0) { |
| c->has_looked_for_mfra = 1; |
| if (pb->seekable) { |
| int ret; |
| av_log(c->fc, AV_LOG_VERBOSE, "stream has moof boxes, will look " |
| "for a mfra\n"); |
| if ((ret = mov_read_mfra(c, pb)) < 0) { |
| av_log(c->fc, AV_LOG_VERBOSE, "found a moof box but failed to " |
| "read the mfra (may be a live ismv)\n"); |
| } |
| } else { |
| av_log(c->fc, AV_LOG_VERBOSE, "found a moof box but stream is not " |
| "seekable, can not look for mfra\n"); |
| } |
| } |
| c->fragment.moof_offset = c->fragment.implicit_offset = avio_tell(pb) - 8; |
| av_log(c->fc, AV_LOG_TRACE, "moof offset %"PRIx64"\n", c->fragment.moof_offset); |
| return mov_read_default(c, pb, atom); |
| } |
| |
| static void mov_metadata_creation_time(AVDictionary **metadata, int64_t time) |
| { |
| char buffer[32]; |
| if (time) { |
| struct tm *ptm, tmbuf; |
| time_t timet; |
| if(time >= 2082844800) |
| time -= 2082844800; /* seconds between 1904-01-01 and Epoch */ |
| timet = time; |
| ptm = gmtime_r(&timet, &tmbuf); |
| if (!ptm) return; |
| if (strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", ptm)) |
| av_dict_set(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; |
| int64_t creation_time; |
| |
| if (c->fc->nb_streams < 1) |
| return 0; |
| st = c->fc->streams[c->fc->nb_streams-1]; |
| sc = st->priv_data; |
| |
| if (sc->time_scale) { |
| av_log(c->fc, AV_LOG_ERROR, "Multiple mdhd?\n"); |
| return AVERROR_INVALIDDATA; |
| } |
| |
| version = avio_r8(pb); |
| if (version > 1) { |
| avpriv_request_sample(c->fc, "Version %d", version); |
| return AVERROR_PATCHWELCOME; |
| } |
| avio_rb24(pb); /* flags */ |
| if (version == 1) { |
| creation_time = avio_rb64(pb); |
| avio_rb64(pb); |
| } else { |
| creation_time = avio_rb32(pb); |
| avio_rb32(pb); /* modification time */ |
| } |
| mov_metadata_creation_time(&st->metadata, creation_time); |
| |
| sc->time_scale = avio_rb32(pb); |
| st->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */ |
| |
| lang = avio_rb16(pb); /* language */ |
| if (ff_mov_lang_to_iso639(lang, language)) |
| av_dict_set(&st->metadata, "language", language, 0); |
| avio_rb16(pb); /* quality */ |
| |
| return 0; |
| } |
| |
| static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
| { |
| int64_t creation_time; |
| int version = avio_r8(pb); /* version */ |
| avio_rb24(pb); /* flags */ |
| |
| if (version == 1) { |
| creation_time = avio_rb64(pb); |
| avio_rb64(pb); |
| } else { |
| creation_time = avio_rb32(pb); |
| avio_rb32(pb); /* modification time */ |
| } |
| mov_metadata_creation_time(&c->fc->metadata, creation_time); |
| c->time_scale = avio_rb32(pb); /* time scale */ |
| |
| av_log(c->fc, AV_LOG_TRACE, "time scale = %i\n", c->time_scale); |
| |
| c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */ |
| // set the AVCodecContext duration because the duration of individual tracks |
| // may be inaccurate |
| if (c->time_scale > 0 && !c->trex_data) |
| c->fc->duration = av_rescale(c->duration, AV_TIME_BASE, c->time_scale); |
| avio_rb32(pb); /* preferred scale */ |
| |
| avio_rb16(pb); /* preferred volume */ |
| |
| avio_skip(pb, 10); /* reserved */ |
| |
| avio_skip(pb, 36); /* display matrix */ |
| |
| 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_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
| { |
| AVStream *st; |
| int little_endian; |
| |
| if (c->fc->nb_streams < 1) |
| return 0; |
| st = c->fc->streams[c->fc->nb_streams-1]; |
| |
| little_endian = avio_rb16(pb) & 0xFF; |
| av_log(c->fc, AV_LOG_TRACE, "enda %d\n", little_endian); |
| if (little_endian == 1) { |
| switch (st->codec->codec_id) { |
| case AV_CODEC_ID_PCM_S24BE: |
| st->codec->codec_id = AV_CODEC_ID_PCM_S24LE; |
| break; |
| case AV_CODEC_ID_PCM_S32BE: |
| st->codec->codec_id = AV_CODEC_ID_PCM_S32LE; |
| break; |
| case AV_CODEC_ID_PCM_F32BE: |
| st->codec->codec_id = AV_CODEC_ID_PCM_F32LE; |
| break; |
| case AV_CODEC_ID_PCM_F64BE: |
| st->codec->codec_id = AV_CODEC_ID_PCM_F64LE; |
| break; |
| default: |
| break; |
| } |
| } |
| return 0; |
| } |
| |
| static int mov_read_colr(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
| { |
| AVStream *st; |
| char color_parameter_type[5] = { 0 }; |
| uint16_t color_primaries, color_trc, color_matrix; |
| int ret; |
| |
| if (c->fc->nb_streams < 1) |
| return 0; |
| st = c->fc->streams[c->fc->nb_streams - 1]; |
| |
| ret = ffio_read_size(pb, color_parameter_type, 4); |
| if (ret < 0) |
| return ret; |
| if (strncmp(color_parameter_type, "nclx", 4) && |
| strncmp(color_parameter_type, "nclc", 4)) { |
| av_log(c->fc, AV_LOG_WARNING, "unsupported color_parameter_type %s\n", |
| color_parameter_type); |
| return 0; |
| } |
| |
| color_primaries = avio_rb16(pb); |
| color_trc = avio_rb16(pb); |
| color_matrix = avio_rb16(pb); |
| |
| av_log(c->fc, AV_LOG_TRACE, |
| "%s: pri %d trc %d matrix %d", |
| color_parameter_type, color_primaries, color_trc, color_matrix); |
| |
| if (!strncmp(color_parameter_type, "nclx", 4)) { |
| uint8_t color_range = avio_r8(pb) >> 7; |
| av_log(c->fc, AV_LOG_TRACE, " full %"PRIu8"", color_range); |
| if (color_range) |
| st->codec->color_range = AVCOL_RANGE_JPEG; |
| else |
| st->codec->color_range = AVCOL_RANGE_MPEG; |
| /* 14496-12 references JPEG XR specs (rather than the more complete |
| * 23001-8) so some adjusting is required */ |
| if (color_primaries >= AVCOL_PRI_FILM) |
| color_primaries = AVCOL_PRI_UNSPECIFIED; |
| if ((color_trc >= AVCOL_TRC_LINEAR && |
| color_trc <= AVCOL_TRC_LOG_SQRT) || |
| color_trc >= AVCOL_TRC_BT2020_10) |
| color_trc = AVCOL_TRC_UNSPECIFIED; |
| if (color_matrix >= AVCOL_SPC_BT2020_NCL) |
| color_matrix = AVCOL_SPC_UNSPECIFIED; |
| st->codec->color_primaries = color_primaries; |
| st->codec->color_trc = color_trc; |
| st->codec->colorspace = color_matrix; |
| } else if (!strncmp(color_parameter_type, "nclc", 4)) { |
| /* color primaries, Table 4-4 */ |
| switch (color_primaries) { |
| case 1: st->codec->color_primaries = AVCOL_PRI_BT709; break; |
| case 5: st->codec->color_primaries = AVCOL_PRI_SMPTE170M; break; |
| case 6: st->codec->color_primaries = AVCOL_PRI_SMPTE240M; break; |
| } |
| /* color transfer, Table 4-5 */ |
| switch (color_trc) { |
| case 1: st->codec->color_trc = AVCOL_TRC_BT709; break; |
| case 7: st->codec->color_trc = AVCOL_TRC_SMPTE240M; break; |
| } |
| /* color matrix, Table 4-6 */ |
| switch (color_matrix) { |
| case 1: st->codec->colorspace = AVCOL_SPC_BT709; break; |
| case 6: st->codec->colorspace = AVCOL_SPC_BT470BG; break; |
| case 7: st->codec->colorspace = AVCOL_SPC_SMPTE240M; break; |
| } |
| } |
| av_log(c->fc, AV_LOG_TRACE, "\n"); |
| |
| 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; |
| } |
| |
| static int mov_realloc_extradata(AVCodecContext *codec, MOVAtom atom) |
| { |
| int err = 0; |
| uint64_t size = (uint64_t)codec->extradata_size + atom.size + 8 + AV_INPUT_BUFFER_PADDING_SIZE; |
| if (size > INT_MAX || (uint64_t)atom.size > INT_MAX) |
| return AVERROR_INVALIDDATA; |
| if ((err = av_reallocp(&codec->extradata, size)) < 0) { |
| codec->extradata_size = 0; |
| return err; |
| } |
| codec->extradata_size = size - AV_INPUT_BUFFER_PADDING_SIZE; |
| return 0; |
| } |
| |
| /* Read a whole atom into the extradata return the size of the atom read, possibly truncated if != atom.size */ |
| static int64_t mov_read_atom_into_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom, |
| AVCodecContext *codec, uint8_t *buf) |
| { |
| int64_t result = atom.size; |
| int err; |
| |
| AV_WB32(buf , atom.size + 8); |
| AV_WL32(buf + 4, atom.type); |
| err = ffio_read_size(pb, buf + 8, atom.size); |
| if (err < 0) { |
| codec->extradata_size -= atom.size; |
| return err; |
| } else if (err < atom.size) { |
| av_log(c->fc, AV_LOG_WARNING, "truncated extradata\n"); |
| codec->extradata_size -= atom.size - err; |
| result = err; |
| } |
| memset(buf + 8 + err, 0, AV_INPUT_BUFFER_PADDING_SIZE); |
| return result; |
| } |
| |
| /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */ |
| static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom, |
| enum AVCodecID codec_id) |
| { |
| AVStream *st; |
| uint64_t original_size; |
| int err; |
| |
| if (c->fc->nb_streams < 1) // will happen with jp2 files |
| return 0; |
| st = c->fc->streams[c->fc->nb_streams-1]; |
| |
| if (st->codec->codec_id != codec_id) |
| return 0; /* unexpected codec_id - don't mess with extradata */ |
| |
| original_size = st->codec->extradata_size; |
| err = mov_realloc_extradata(st->codec, atom); |
| if (err) |
| return err; |
| |
| err = mov_read_atom_into_extradata(c, pb, atom, st->codec, st->codec->extradata + original_size); |
| if (err < 0) |
| return err; |
| return 0; // Note: this is the original behavior to ignore truncation. |
| } |
| |
| /* wrapper functions for reading ALAC/AVS/MJPEG/MJPEG2000 extradata atoms only for those codecs */ |
| static int mov_read_alac(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
| { |
| return mov_read_extradata(c, pb, atom, AV_CODEC_ID_ALAC); |
| } |
| |
| static int mov_read_avss(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
| { |
| return mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVS); |
| } |
| |
| static int mov_read_jp2h(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
| { |
| return mov_read_extradata(c, pb, atom, AV_CODEC_ID_JPEG2000); |
| } |
| |
| static int mov_read_dpxe(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
| { |
| return mov_read_extradata(c, pb, atom, AV_CODEC_ID_R10K); |
| } |
| |
| static int mov_read_avid(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
| { |
| int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVUI); |
| if(ret == 0) |
| ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_DNXHD); |
| return ret; |
| } |
| |
| static int mov_read_targa_y216(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
| { |
| int ret = mov_read_extradata(c, pb, atom, AV_CODEC_ID_TARGA_Y216); |
| |
| if (!ret && c->fc->nb_streams >= 1) { |
| AVCodecContext *avctx = c->fc->streams[c->fc->nb_streams-1]->codec; |
| if (avctx->extradata_size >= 40) { |
| avctx->height = AV_RB16(&avctx->extradata[36]); |
| avctx->width = AV_RB16(&avctx->extradata[38]); |
| } |
| } |
| return ret; |
| } |
| |
| static int mov_read_ares(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
| { |
| if (c->fc->nb_streams >= 1) { |
| AVCodecContext *codec = c->fc->streams[c->fc->nb_streams-1]->codec; |
| if (codec->codec_tag == MKTAG('A', 'V', 'i', 'n') && |
| codec->codec_id == AV_CODEC_ID_H264 && |
| atom.size > 11) { |
| avio_skip(pb, 10); |
| /* For AVID AVCI50, force width of 1440 to be able to select the correct SPS and PPS */ |
| if (avio_rb16(pb) == 0xd4d) |
| codec->width = 1440; |
| return 0; |
| } |
| } |
| |
| return mov_read_avid(c, pb, atom); |
| } |
| |
| static int mov_read_aclr(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
| { |
| int ret = 0; |
| int length = 0; |
| uint64_t original_size; |
| if (c->fc->nb_streams >= 1) { |
| AVCodecContext *codec = c->fc->streams[c->fc->nb_streams-1]->codec; |
| if (codec->codec_id == AV_CODEC_ID_H264) |
| return 0; |
| if (atom.size == 16) { |
| original_size = codec->extradata_size; |
| ret = mov_realloc_extradata(codec, atom); |
| if (!ret) { |
| length = mov_read_atom_into_extradata(c, pb, atom, codec, codec->extradata + original_size); |
| if (length == atom.size) { |
| const uint8_t range_value = codec->extradata[original_size + 19]; |
| switch (range_value) { |
| case 1: |
| codec->color_range = AVCOL_RANGE_MPEG; |
| break; |
| case 2: |
| codec->color_range = AVCOL_RANGE_JPEG; |
| break; |
| default: |
| av_log(c, AV_LOG_WARNING, "ignored unknown aclr value (%d)\n", range_value); |
| break; |
| } |
| ff_dlog(c, "color_range: %d\n", codec->color_range); |
| } else { |
| /* For some reason the whole atom was not added to the extradata */ |
| av_log(c, AV_LOG_ERROR, "aclr not decoded - incomplete atom\n"); |
| } |
| } else { |
| av_log(c, AV_LOG_ERROR, "aclr not decoded - unable to add atom to extradata\n"); |
| } |
| } else { |
| av_log(c, AV_LOG_WARNING, "aclr not decoded - unexpected size %"PRId64"\n", atom.size); |
| } |
| } |
| |
| return ret; |
| } |
| |
| static int mov_read_svq3(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
| { |
| return mov_read_extradata(c, pb, atom, AV_CODEC_ID_SVQ3); |
| } |
| |
| static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
| { |
| AVStream *st; |
| int ret; |
| |
| if (c->fc->nb_streams < 1) |
| return 0; |
| st = c->fc->streams[c->fc->nb_streams-1]; |
| |
| if ((uint64_t)atom.size > (1<<30)) |
| return AVERROR_INVALIDDATA; |
| |
| if (st->codec->codec_id == AV_CODEC_ID_QDM2 || |
| st->codec->codec_id == AV_CODEC_ID_QDMC || |
| st->codec->codec_id == AV_CODEC_ID_SPEEX) { |
| // pass all frma atom to codec, needed at least for QDMC and QDM2 |
| av_freep(&st->codec->extradata); |
| ret = ff_get_extradata(st->codec, pb, atom.size); |
| if (ret < 0) |
| return ret; |
| } else if (atom.size > 8) { /* to read frma, esds atoms */ |
| if (st->codec->codec_id == AV_CODEC_ID_ALAC && atom.size >= 24) { |
| uint64_t buffer; |
| ret = ffio_ensure_seekback(pb, 8); |
| if (ret < 0) |
| return ret; |
| buffer = avio_rb64(pb); |
| atom.size -= 8; |
| if ( (buffer & 0xFFFFFFFF) == MKBETAG('f','r','m','a') |
| && buffer >> 32 <= atom.size |
| && buffer >> 32 >= 8) { |
| avio_skip(pb, -8); |
| atom.size += 8; |
| } else if (!st->codec->extradata_size) { |
| #define ALAC_EXTRADATA_SIZE 36 |
| st->codec->extradata = av_mallocz(ALAC_EXTRADATA_SIZE + AV_INPUT_BUFFER_PADDING_SIZE); |
| if (!st->codec->extradata) |
| return AVERROR(ENOMEM); |
| st->codec->extradata_size = ALAC_EXTRADATA_SIZE; |
| AV_WB32(st->codec->extradata , ALAC_EXTRADATA_SIZE); |
| AV_WB32(st->codec->extradata + 4, MKTAG('a','l','a','c')); |
| AV_WB64(st->codec->extradata + 12, buffer); |
| avio_read(pb, st->codec->extradata + 20, 16); |
| avio_skip(pb, atom.size - 24); |
| return 0; |
| } |
| } |
| if ((ret = mov_read_default(c, pb, atom)) < 0) |
| return ret; |
| } else |
| 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, AVIOContext *pb, MOVAtom atom) |
| { |
| AVStream *st; |
| int ret; |
| |
| if (c->fc->nb_streams < 1) |
| return 0; |
| st = c->fc->streams[c->fc->nb_streams-1]; |
| |
| if ((uint64_t)atom.size > (1<<30)) |
| return AVERROR_INVALIDDATA; |
| |
| if (atom.size >= 10) { |
| // Broken files created by legacy versions of libavformat 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); |
| } |
| if (st->codec->extradata_size > 1 && st->codec->extradata) { |
| av_log(c, AV_LOG_WARNING, "ignoring multiple glbl\n"); |
| return 0; |
| } |
| av_freep(&st->codec->extradata); |
| ret = ff_get_extradata(st->codec, pb, atom.size); |
| if (ret < 0) |
| return ret; |
| |
| return 0; |
| } |
| |
| static int mov_read_dvc1(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
| { |
| AVStream *st; |
| uint8_t profile_level; |
| int ret; |
| |
| if (c->fc->nb_streams < 1) |
| return 0; |
| st = c->fc->streams[c->fc->nb_streams-1]; |
| |
| if (atom.size >= (1<<28) || atom.size < 7) |
| return AVERROR_INVALIDDATA; |
| |
| profile_level = avio_r8(pb); |
| if ((profile_level & 0xf0) != 0xc0) |
| return 0; |
| |
| avio_seek(pb, 6, SEEK_CUR); |
| av_freep(&st->codec->extradata); |
| ret = ff_get_extradata(st->codec, pb, atom.size - 7); |
| if (ret < 0) |
| return ret; |
| |
| 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; |
| int ret; |
| |
| 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 AVERROR_INVALIDDATA; |
| |
| avio_skip(pb, 40); |
| av_freep(&st->codec->extradata); |
| ret = ff_get_extradata(st->codec, pb, atom.size - 40); |
| if (ret < 0) |
| return ret; |
| |
| return 0; |
| } |
| |
| static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
| { |
| AVStream *st; |
| MOVStreamContext *sc; |
| unsigned int i, entries; |
| |
| if (c->fc->nb_streams < 1) |
| return 0; |
| st = c->fc->streams[c->fc->nb_streams-1]; |
| sc = st->priv_data; |
| |
| avio_r8(pb); /* version */ |
| avio_rb24(pb); /* flags */ |
| |
| entries = avio_rb32(pb); |
| |
| if (!entries) |
| return 0; |
| |
| if (sc->chunk_offsets) |
| av_log(c->fc, AV_LOG_WARNING, "Duplicated STCO atom\n"); |
| av_free(sc->chunk_offsets); |
| sc->chunk_count = 0; |
| sc->chunk_offsets = av_malloc_array(entries, sizeof(*sc->chunk_offsets)); |
| if (!sc->chunk_offsets) |
| return AVERROR(ENOMEM); |
| sc->chunk_count = entries; |
| |
| if (atom.type == MKTAG('s','t','c','o')) |
| for (i = 0; i < entries && !pb->eof_reached; i++) |
| sc->chunk_offsets[i] = avio_rb32(pb); |
| else if (atom.type == MKTAG('c','o','6','4')) |
| for (i = 0; i < entries && !pb->eof_reached; i++) |
| sc->chunk_offsets[i] = avio_rb64(pb); |
| else |
| return AVERROR_INVALIDDATA; |
| |
| sc->chunk_count = i; |
| |
| if (pb->eof_reached) |
| return AVERROR_EOF; |
| |
| return 0; |
| } |
| |
| /** |
| * Compute codec id for 'lpcm' tag. |
| * See CoreAudioTypes and AudioStreamBasicDescription at Apple. |
| */ |
| enum AVCodecID ff_mov_get_lpcm_codec_id(int bps, int flags) |
| { |
| /* lpcm flags: |
| * 0x1 = float |
| * 0x2 = big-endian |
| * 0x4 = signed |
| */ |
| return ff_get_pcm_codec_id(bps, flags & 1, flags & 2, flags & 4 ? -1 : 0); |
| } |
| |
| static int mov_codec_id(AVStream *st, uint32_t format) |
| { |
| int id = ff_codec_get_id(ff_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, av_bswap32(format) & 0xFFFF); |
| |
| 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 && |
| /* skip old asf mpeg4 tag */ |
| format && format != MKTAG('m','p','4','s')) { |
| id = ff_codec_get_id(ff_codec_movvideo_tags, format); |
| if (id <= 0) |
| 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 || |
| (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE && |
| st->codec->codec_id == AV_CODEC_ID_NONE)) { |
| id = ff_codec_get_id(ff_codec_movsubtitle_tags, format); |
| if (id > 0) |
| st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; |
| } |
| } |
| |
| st->codec->codec_tag = format; |
| |
| return id; |
| } |
| |
| static void mov_parse_stsd_video(MOVContext *c, AVIOContext *pb, |
| AVStream *st, MOVStreamContext *sc) |
| { |
| uint8_t codec_name[32]; |
| int64_t stsd_start; |
| unsigned int len; |
| |
| /* The first 16 bytes of the video sample description are already |
| * read in ff_mov_read_stsd_entries() */ |
| stsd_start = avio_tell(pb) - 16; |
| |
| 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, codec_name, sizeof(codec_name)); |
| if (len < 31) |
| avio_skip(pb, 31 - len); |
| |
| if (codec_name[0]) |
| av_dict_set(&st->metadata, "encoder", codec_name, 0); |
| |
| /* codec_tag YV12 triggers an UV swap in rawdec.c */ |
| if (!memcmp(codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25)) { |
| st->codec->codec_tag = MKTAG('I', '4', '2', '0'); |
| st->codec->width &= ~1; |
| st->codec->height &= ~1; |
| } |
| /* Flash Media Server uses tag H263 with Sorenson Spark */ |
| if (st->codec->codec_tag == MKTAG('H','2','6','3') && |
| !memcmp(codec_name, "Sorenson H263", 13)) |
| st->codec->codec_id = AV_CODEC_ID_FLV1; |
| |
| st->codec->bits_per_coded_sample = avio_rb16(pb); /* depth */ |
| |
| avio_seek(pb, stsd_start, SEEK_SET); |
| |
| if (ff_get_qtpalette(st->codec->codec_id, pb, sc->palette)) { |
| st->codec->bits_per_coded_sample &= 0x1F; |
| sc->has_palette = 1; |
| } |
| } |
| |
| static void mov_parse_stsd_audio(MOVContext *c, AVIOContext *pb, |
| AVStream *st, MOVStreamContext *sc) |
| { |
| int bits_per_sample, flags; |
| uint16_t version = avio_rb16(pb); |
| AVDictionaryEntry *compatible_brands = av_dict_get(c->fc->metadata, "compatible_brands", NULL, AV_DICT_MATCH_CASE); |
| |
| avio_rb16(pb); /* revision level */ |
| avio_rb32(pb); /* vendor */ |
| |
| st->codec->channels = avio_rb16(pb); /* channel count */ |
| st->codec->bits_per_coded_sample = avio_rb16(pb); /* sample size */ |
| av_log(c->fc, AV_LOG_TRACE, "audio channels %d\n", st->codec->channels); |
| |
| sc->audio_cid = avio_rb16(pb); |
| avio_rb16(pb); /* packet size = 0 */ |
| |
| st->codec->sample_rate = ((avio_rb32(pb) >> 16)); |
| |
| // Read QT version 1 fields. In version 0 these do not exist. |
| av_log(c->fc, AV_LOG_TRACE, "version =%d, isom =%d\n", version, c->isom); |
| if (!c->isom || |
| (compatible_brands && strstr(compatible_brands->value, "qt "))) { |
| |
| 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)); |
| st->codec->channels = avio_rb32(pb); |
| avio_rb32(pb); /* always 0x7F000000 */ |
| st->codec->bits_per_coded_sample = avio_rb32(pb); |
| |
| flags = avio_rb32(pb); /* lpcm format specific flag */ |
| sc->bytes_per_frame = avio_rb32(pb); |
| sc->samples_per_frame = avio_rb32(pb); |
| if (st->codec->codec_tag == MKTAG('l','p','c','m')) |
| st->codec->codec_id = |
| ff_mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample, |
| flags); |
| } |
| if (version == 0 || (version == 1 && sc->audio_cid != -2)) { |
| /* can't correctly handle variable sized packet as audio unit */ |
| switch (st->codec->codec_id) { |
| case AV_CODEC_ID_MP2: |
| case AV_CODEC_ID_MP3: |
| st->need_parsing = AVSTREAM_PARSE_FULL; |
| break; |
| } |
| } |
| } |
| |
| if (sc->format == 0) { |
| if (st->codec->bits_per_coded_sample == 8) |
| st->codec->codec_id = mov_codec_id(st, MKTAG('r','a','w',' ')); |
| else if (st->codec->bits_per_coded_sample == 16) |
| st->codec->codec_id = mov_codec_id(st, MKTAG('t','w','o','s')); |
| } |
| |
| switch (st->codec->codec_id) { |
| case AV_CODEC_ID_PCM_S8: |
| case AV_CODEC_ID_PCM_U8: |
| if (st->codec->bits_per_coded_sample == 16) |
| st->codec->codec_id = AV_CODEC_ID_PCM_S16BE; |
| break; |
| case AV_CODEC_ID_PCM_S16LE: |
| case AV_CODEC_ID_PCM_S16BE: |
| if (st->codec->bits_per_coded_sample == 8) |
| st->codec->codec_id = AV_CODEC_ID_PCM_S8; |
| else if (st->codec->bits_per_coded_sample == 24) |
| st->codec->codec_id = |
| st->codec->codec_id == AV_CODEC_ID_PCM_S16BE ? |
| AV_CODEC_ID_PCM_S24BE : AV_CODEC_ID_PCM_S24LE; |
| else if (st->codec->bits_per_coded_sample == 32) |
| st->codec->codec_id = |
| st->codec->codec_id == AV_CODEC_ID_PCM_S16BE ? |
| AV_CODEC_ID_PCM_S32BE : AV_CODEC_ID_PCM_S32LE; |
| break; |
| /* set values for old format before stsd version 1 appeared */ |
| case AV_CODEC_ID_MACE3: |
| sc->samples_per_frame = 6; |
| sc->bytes_per_frame = 2 * st->codec->channels; |
| break; |
| case AV_CODEC_ID_MACE6: |
| sc->samples_per_frame = 6; |
| sc->bytes_per_frame = 1 * st->codec->channels; |
| break; |
| case AV_CODEC_ID_ADPCM_IMA_QT: |
| sc->samples_per_frame = 64; |
| sc->bytes_per_frame = 34 * st->codec->channels; |
| break; |
| case AV_CODEC_ID_GSM: |
| sc->samples_per_frame = 160; |
| sc->bytes_per_frame = 33; |
| break; |
| default: |
| break; |
| } |
| |
| bits_per_sample = av_get_bits_per_sample(st->codec->codec_id); |
| if (bits_per_sample) { |
| st->codec->bits_per_coded_sample = bits_per_sample; |
| sc->sample_size = (bits_per_sample >> 3) * st->codec->channels; |
| } |
| } |
| |
| static void mov_parse_stsd_subtitle(MOVContext *c, AVIOContext *pb, |
| AVStream *st, MOVStreamContext *sc, |
| int64_t size) |
| { |
| // ttxt stsd contains display flags, justification, background |
| // color, fonts, and default styles, so fake an atom to read it |
| MOVAtom fake_atom = { .size = size }; |
| // mp4s contains a regular esds atom |
| if (st->codec->codec_tag != AV_RL32("mp4s")) |
| mov_read_glbl(c, pb, fake_atom); |
| st->codec->width = sc->width; |
| st->codec->height = sc->height; |
| } |
| |
| static uint32_t yuv_to_rgba(uint32_t ycbcr) |
| { |
| uint8_t r, g, b; |
| int y, cb, cr; |
| |
| y = (ycbcr >> 16) & 0xFF; |
| cr = (ycbcr >> 8) & 0xFF; |
| cb = ycbcr & 0xFF; |
| |
| b = av_clip_uint8((1164 * (y - 16) + 2018 * (cb - 128)) / 1000); |
| g = av_clip_uint8((1164 * (y - 16) - 813 * (cr - 128) - 391 * (cb - 128)) / 1000); |
| r = av_clip_uint8((1164 * (y - 16) + 1596 * (cr - 128) ) / 1000); |
| |
| return (r << 16) | (g << 8) | b; |
| } |
| |
| static int mov_rewrite_dvd_sub_extradata(AVStream *st) |
| { |
| char buf[256] = {0}; |
| uint8_t *src = st->codec->extradata; |
| int i; |
| |
| if (st->codec->extradata_size != 64) |
| return 0; |
| |
| if (st->codec->width > 0 && st->codec->height > 0) |
| snprintf(buf, sizeof(buf), "size: %dx%d\n", |
| st->codec->width, st->codec->height); |
| av_strlcat(buf, "palette: ", sizeof(buf)); |
| |
| for (i = 0; i < 16; i++) { |
| uint32_t yuv = AV_RB32(src + i * 4); |
| uint32_t rgba = yuv_to_rgba(yuv); |
| |
| av_strlcatf(buf, sizeof(buf), "%06"PRIx32"%s", rgba, i != 15 ? ", " : ""); |
| } |
| |
| if (av_strlcat(buf, "\n", sizeof(buf)) >= sizeof(buf)) |
| return 0; |
| |
| av_freep(&st->codec->extradata); |
| st->codec->extradata_size = 0; |
| st->codec->extradata = av_mallocz(strlen(buf) + AV_INPUT_BUFFER_PADDING_SIZE); |
| if (!st->codec->extradata) |
| return AVERROR(ENOMEM); |
| st->codec->extradata_size = strlen(buf); |
| memcpy(st->codec->extradata, buf, st->codec->extradata_size); |
| |
| return 0; |
| } |
| |
| static int mov_parse_stsd_data(MOVContext *c, AVIOContext *pb, |
| AVStream *st, MOVStreamContext *sc, |
| int64_t size) |
| { |
| int ret; |
| |
| if (st->codec->codec_tag == MKTAG('t','m','c','d')) { |
| if ((int)size != size) |
| return AVERROR(ENOMEM); |
| |
| ret = ff_get_extradata(st->codec, pb, size); |
| if (ret < 0) |
| return ret; |
| if (size > 16) { |
| MOVStreamContext *tmcd_ctx = st->priv_data; |
| int val; |
| val = AV_RB32(st->codec->extradata + 4); |
| tmcd_ctx->tmcd_flags = val; |
| if (val & 1) |
| st->codec->flags2 |= AV_CODEC_FLAG2_DROP_FRAME_TIMECODE; |
| st->codec->time_base.den = st->codec->extradata[16]; /* number of frame */ |
| st->codec->time_base.num = 1; |
| /* adjust for per frame dur in counter mode */ |
| if (tmcd_ctx->tmcd_flags & 0x0008) { |
| int timescale = AV_RB32(st->codec->extradata + 8); |
| int framedur = AV_RB32(st->codec->extradata + 12); |
| st->codec->time_base.den *= timescale; |
| st->codec->time_base.num *= framedur; |
| } |
| if (size > 30) { |
| uint32_t len = AV_RB32(st->codec->extradata + 18); /* name atom length */ |
| uint32_t format = AV_RB32(st->codec->extradata + 22); |
| if (format == AV_RB32("name") && (int64_t)size >= (int64_t)len + 18) { |
| uint16_t str_size = AV_RB16(st->codec->extradata + 26); /* string length */ |
| if (str_size > 0 && size >= (int)str_size + 26) { |
| char *reel_name = av_malloc(str_size + 1); |
| if (!reel_name) |
| return AVERROR(ENOMEM); |
| memcpy(reel_name, st->codec->extradata + 30, str_size); |
| reel_name[str_size] = 0; /* Add null terminator */ |
| /* don't add reel_name if emtpy string */ |
| if (*reel_name == 0) { |
| av_free(reel_name); |
| } else { |
| av_dict_set(&st->metadata, "reel_name", reel_name, AV_DICT_DONT_STRDUP_VAL); |
| } |
| } |
| } |
| } |
| } |
| } else { |
| /* other codec type, just skip (rtp, mp4s ...) */ |
| avio_skip(pb, size); |
| } |
| return 0; |
| } |
| |
| static int mov_finalize_stsd_codec(MOVContext *c, AVIOContext *pb, |
| AVStream *st, MOVStreamContext *sc) |
| { |
| if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && |
| !st->codec->sample_rate && sc->time_scale > 1) |
| st->codec->sample_rate = sc->time_scale; |
| |
| /* special codec parameters handling */ |
| switch (st->codec->codec_id) { |
| #if CONFIG_DV_DEMUXER |
| case AV_CODEC_ID_DVAUDIO: |
| c->dv_fctx = avformat_alloc_context(); |
| if (!c->dv_fctx) { |
| av_log(c->fc, AV_LOG_ERROR, "dv demux context alloc error\n"); |
| return AVERROR(ENOMEM); |
| } |
| 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 AVERROR(ENOMEM); |
| } |
| sc->dv_audio_container = 1; |
| st->codec->codec_id = AV_CODEC_ID_PCM_S16LE; |
| break; |
| #endif |
| /* no ifdef since parameters are always those */ |
| case AV_CODEC_ID_QCELP: |
| st->codec->channels = 1; |
| // force sample rate for qcelp when not stored in mov |
| if (st->codec->codec_tag != MKTAG('Q','c','l','p')) |
| st->codec->sample_rate = 8000; |
| // FIXME: Why is the following needed for some files? |
| sc->samples_per_frame = 160; |
| if (!sc->bytes_per_frame) |
| sc->bytes_per_frame = 35; |
| break; |
| case AV_CODEC_ID_AMR_NB: |
| st->codec->channels = 1; |
| /* force sample rate for amr, stsd in 3gp does not store sample rate */ |
| st->codec->sample_rate = 8000; |
| break; |
| case AV_CODEC_ID_AMR_WB: |
| st->codec->channels = 1; |
| st->codec->sample_rate = 16000; |
| break; |
| case AV_CODEC_ID_MP2: |
| case AV_CODEC_ID_MP3: |
| /* force type after stsd for m1a hdlr */ |
| st->codec->codec_type = AVMEDIA_TYPE_AUDIO; |
| break; |
| case AV_CODEC_ID_GSM: |
| case AV_CODEC_ID_ADPCM_MS: |
| case AV_CODEC_ID_ADPCM_IMA_WAV: |
| case AV_CODEC_ID_ILBC: |
| case AV_CODEC_ID_MACE3: |
| case AV_CODEC_ID_MACE6: |
| case AV_CODEC_ID_QDM2: |
| st->codec->block_align = sc->bytes_per_frame; |
| break; |
| case AV_CODEC_ID_ALAC: |
| if (st->codec->extradata_size == 36) { |
| st->codec->channels = AV_RB8 (st->codec->extradata + 21); |
| st->codec->sample_rate = AV_RB32(st->codec->extradata + 32); |
| } |
| break; |
| case AV_CODEC_ID_AC3: |
| case AV_CODEC_ID_EAC3: |
| case AV_CODEC_ID_MPEG1VIDEO: |
| case AV_CODEC_ID_VC1: |
| st->need_parsing = AVSTREAM_PARSE_FULL; |
| break; |
| default: |
| break; |
| } |
| return 0; |
| } |
| |
| static int mov_skip_multiple_stsd(MOVContext *c, AVIOContext *pb, |
| int codec_tag, int format, |
| int64_t size) |
| { |
| int video_codec_id = ff_codec_get_id(ff_codec_movvideo_tags, format); |
| |
| if (codec_tag && |
| (codec_tag != format && |
| (c->fc->video_codec_id ? video_codec_id != c->fc->video_codec_id |
| : codec_tag != MKTAG('j','p','e','g')))) { |
| /* 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. */ |
| |
| av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n"); |
| avio_skip(pb, size); |
| return 1; |
| } |
| if ( codec_tag == AV_RL32("avc1") || |
| codec_tag == AV_RL32("hvc1") || |
| codec_tag == AV_RL32("hev1") |
| ) |
| av_log(c->fc, AV_LOG_WARNING, "Concatenated H.264 or H.265 might not play correctly.\n"); |
| |
| return 0; |
| } |
| |
| int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries) |
| { |
| AVStream *st; |
| MOVStreamContext *sc; |
| int pseudo_stream_id; |
| |
| if (c->fc->nb_streams < 1) |
| return 0; |
| st = c->fc->streams[c->fc->nb_streams-1]; |
| sc = st->priv_data; |
| |
| for (pseudo_stream_id = 0; |
| pseudo_stream_id < entries && !pb->eof_reached; |
| pseudo_stream_id++) { |
| //Parsing Sample description table |
| enum AVCodecID id; |
| int ret, dref_id = 1; |
| MOVAtom a = { AV_RL32("stsd") }; |
| int64_t start_pos = avio_tell(pb); |
| int64_t size = avio_rb32(pb); /* size */ |
| uint32_t format = avio_rl32(pb); /* data format */ |
| |
| if (size >= 16) { |
| avio_rb32(pb); /* reserved */ |
| avio_rb16(pb); /* reserved */ |
| dref_id = avio_rb16(pb); |
| }else if (size <= 7){ |
| av_log(c->fc, AV_LOG_ERROR, "invalid size %"PRId64" in stsd\n", size); |
| return AVERROR_INVALIDDATA; |
| } |
| |
| if (mov_skip_multiple_stsd(c, pb, st->codec->codec_tag, format, |
| size - (avio_tell(pb) - start_pos))) |
| continue; |
| |
| sc->pseudo_stream_id = st->codec->codec_tag ? -1 : pseudo_stream_id; |
| sc->dref_id= dref_id; |
| sc->format = format; |
| |
| id = mov_codec_id(st, format); |
| |
| av_log(c->fc, AV_LOG_TRACE, |
| "size=%"PRId64" 4CC= %c%c%c%c/0x%08x codec_type=%d\n", size, |
| (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff, |
| (format >> 24) & 0xff, format, st->codec->codec_type); |
| |
| if (st->codec->codec_type==AVMEDIA_TYPE_VIDEO) { |
| st->codec->codec_id = id; |
| mov_parse_stsd_video(c, pb, st, sc); |
| } else if (st->codec->codec_type==AVMEDIA_TYPE_AUDIO) { |
| st->codec->codec_id = id; |
| mov_parse_stsd_audio(c, pb, st, sc); |
| } else if (st->codec->codec_type==AVMEDIA_TYPE_SUBTITLE){ |
| st->codec->codec_id = id; |
| mov_parse_stsd_subtitle(c, pb, st, sc, |
| size - (avio_tell(pb) - start_pos)); |
| } else { |
| ret = mov_parse_stsd_data(c, pb, st, sc, |
| size - (avio_tell(pb) - start_pos)); |
| if (ret < 0) |
| return ret; |
| } |
| /* this will read extra atoms at the end (wave, alac, damr, avcC, hvcC, SMI ...) */ |
| a.size = size - (avio_tell(pb) - start_pos); |
| if (a.size > 8) { |
| if ((ret = mov_read_default(c, pb, a)) < 0) |
| return ret; |
| } else if (a.size > 0) |
| avio_skip(pb, a.size); |
| } |
| |
| if (pb->eof_reached) |
| return AVERROR_EOF; |
| |
| return mov_finalize_stsd_codec(c, pb, st, sc); |
| } |
| |
| 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; |
| unsigned int i, entries; |
| |
| if (c->fc->nb_streams < 1) |
| return 0; |
| st = c->fc->streams[c->fc->nb_streams-1]; |
| sc = st->priv_data; |
| |
| avio_r8(pb); /* version */ |
| avio_rb24(pb); /* flags */ |
| |
| entries = avio_rb32(pb); |
| |
| av_log(c->fc, AV_LOG_TRACE, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries); |
| |
| if (!entries) |
| return 0; |
| if (sc->stsc_data) |
| av_log(c->fc, AV_LOG_WARNING, "Duplicated STSC atom\n"); |
| av_free(sc->stsc_data); |
| sc->stsc_count = 0; |
| sc->stsc_data = av_malloc_array(entries, sizeof(*sc->stsc_data)); |
| if (!sc->stsc_data) |
| return AVERROR(ENOMEM); |
| |
| for (i = 0; i < entries && !pb->eof_reached; i++) { |
| sc->stsc_data[i].first = avio_rb32(pb); |
| sc->stsc_data[i].count = avio_rb32(pb); |
| sc->stsc_data[i].id = avio_rb32(pb); |
| } |
| |
| sc->stsc_count = i; |
| |
| if (pb->eof_reached) |
| return AVERROR_EOF; |
| |
| return 0; |
| } |
| |
| static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
| { |
| AVStream *st; |
| MOVStreamContext *sc; |
| unsigned i, entries; |
| |
| if (c->fc->nb_streams < 1) |
| return 0; |
| st = c->fc->streams[c->fc->nb_streams-1]; |
| sc = st->priv_data; |
| |
| avio_rb32(pb); // version + flags |
| |
| entries = avio_rb32(pb); |
| if (sc->stps_data) |
| av_log(c->fc, AV_LOG_WARNING, "Duplicated STPS atom\n"); |
| av_free(sc->stps_data); |
| sc->stps_count = 0; |
| sc->stps_data = av_malloc_array(entries, sizeof(*sc->stps_data)); |
| if (!sc->stps_data) |
| return AVERROR(ENOMEM); |
| |
| for (i = 0; i < entries && !pb->eof_reached; i++) { |
| sc->stps_data[i] = avio_rb32(pb); |
| //av_log(c->fc, AV_LOG_TRACE, "stps %d\n", sc->stps_data[i]); |
| } |
| |
| sc->stps_count = i; |
| |
| if (pb->eof_reached) |
| return AVERROR_EOF; |
| |
| return 0; |
| } |
| |
| static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
| { |
| AVStream *st; |
| MOVStreamContext *sc; |
| unsigned int i, entries; |
| |
| if (c->fc->nb_streams < 1) |
| return 0; |
| st = c->fc->streams[c->fc->nb_streams-1]; |
| sc = st->priv_data; |
| |
| avio_r8(pb); /* version */ |
| avio_rb24(pb); /* flags */ |
| |
| entries = avio_rb32(pb); |
| |
| av_log(c->fc, AV_LOG_TRACE, "keyframe_count = %d\n", entries); |
| |
| if (!entries) |
| { |
| sc->keyframe_absent = 1; |
| if (!st->need_parsing && st->codec->codec_type == AVMEDIA_TYPE_VIDEO) |
| st->need_parsing = AVSTREAM_PARSE_HEADERS; |
| return 0; |
| } |
| if (sc->keyframes) |
| av_log(c->fc, AV_LOG_WARNING, "Duplicated STSS atom\n"); |
| if (entries >= UINT_MAX / sizeof(int)) |
| return AVERROR_INVALIDDATA; |
| av_freep(&sc->keyframes); |
| sc->keyframe_count = 0; |
| sc->keyframes = av_malloc_array(entries, sizeof(*sc->keyframes)); |
| if (!sc->keyframes) |
| return AVERROR(ENOMEM); |
| |
| for (i = 0; i < entries && !pb->eof_reached; i++) { |
| sc->keyframes[i] = avio_rb32(pb); |
| //av_log(c->fc, AV_LOG_TRACE, "keyframes[]=%d\n", sc->keyframes[i]); |
| } |
| |
| sc->keyframe_count = i; |
| |
| if (pb->eof_reached) |
| return AVERROR_EOF; |
| |
| return 0; |
| } |
| |
| static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
| { |
| AVStream *st; |
| MOVStreamContext *sc; |
| unsigned int i, entries, sample_size, field_size, num_bytes; |
| GetBitContext gb; |
| unsigned char* buf; |
| int ret; |
| |
| if (c->fc->nb_streams < 1) |
| return 0; |
| st = c->fc->streams[c->fc->nb_streams-1]; |
| sc = st->priv_data; |
| |
| avio_r8(pb); /* version */ |
| avio_rb24(pb); /* flags */ |
| |
| if (atom.type == MKTAG('s','t','s','z')) { |
| sample_size = avio_rb32(pb); |
| if (!sc->sample_size) /* do not overwrite value computed in stsd */ |
| sc->sample_size = sample_size; |
| sc->stsz_sample_size = sample_size; |
| field_size = 32; |
| } else { |
| sample_size = 0; |
| avio_rb24(pb); /* reserved */ |
| field_size = avio_r8(pb); |
| } |
| entries = avio_rb32(pb); |
| |
| av_log(c->fc, AV_LOG_TRACE, "sample_size = %d sample_count = %d\n", sc->sample_size, entries); |
| |
| sc->sample_count = entries; |
| if (sample_size) |
| return 0; |
| |
| 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 AVERROR_INVALIDDATA; |
| } |
| |
| if (!entries) |
| return 0; |
| if (entries >= (UINT_MAX - 4) / field_size) |
| return AVERROR_INVALIDDATA; |
| if (sc->sample_sizes) |
| av_log(c->fc, AV_LOG_WARNING, "Duplicated STSZ atom\n"); |
| av_free(sc->sample_sizes); |
| sc->sample_count = 0; |
| sc->sample_sizes = av_malloc_array(entries, sizeof(*sc->sample_sizes)); |
| if (!sc->sample_sizes) |
| return AVERROR(ENOMEM); |
| |
| num_bytes = (entries*field_size+4)>>3; |
| |
| buf = av_malloc(num_bytes+AV_INPUT_BUFFER_PADDING_SIZE); |
| if (!buf) { |
| av_freep(&sc->sample_sizes); |
| return AVERROR(ENOMEM); |
| } |
| |
| ret = ffio_read_size(pb, buf, num_bytes); |
| if (ret < 0) { |
| av_freep(&sc->sample_sizes); |
| av_free(buf); |
| return ret; |
| } |
| |
| init_get_bits(&gb, buf, 8*num_bytes); |
| |
| for (i = 0; i < entries && !pb->eof_reached; i++) { |
| sc->sample_sizes[i] = get_bits_long(&gb, field_size); |
| sc->data_size += sc->sample_sizes[i]; |
| } |
| |
| sc->sample_count = i; |
| |
| av_free(buf); |
| |
| if (pb->eof_reached) |
| return AVERROR_EOF; |
| |
| return 0; |
| } |
| |
| static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
| { |
| AVStream *st; |
| MOVStreamContext *sc; |
| unsigned int i, entries; |
| int64_t duration=0; |
| int64_t total_sample_count=0; |
| |
| if (c->fc->nb_streams < 1) |
| return 0; |
| st = c->fc->streams[c->fc->nb_streams-1]; |
| sc = st->priv_data; |
| |
| avio_r8(pb); /* version */ |
| avio_rb24(pb); /* flags */ |
| entries = avio_rb32(pb); |
| |
| av_log(c->fc, AV_LOG_TRACE, "track[%i].stts.entries = %i\n", |
| c->fc->nb_streams-1, entries); |
| |
| if (sc->stts_data) |
| av_log(c->fc, AV_LOG_WARNING, "Duplicated STTS atom\n"); |
| av_free(sc->stts_data); |
| sc->stts_count = 0; |
| sc->stts_data = av_malloc_array(entries, sizeof(*sc->stts_data)); |
| if (!sc->stts_data) |
| return AVERROR(ENOMEM); |
| |
| for (i = 0; i < entries && !pb->eof_reached; i++) { |
| int sample_duration; |
| int sample_count; |
| |
| sample_count=avio_rb32(pb); |
| sample_duration = avio_rb32(pb); |
| |
| if (sample_count < 0) { |
| av_log(c->fc, AV_LOG_ERROR, "Invalid sample_count=%d\n", sample_count); |
| return AVERROR_INVALIDDATA; |
| } |
| sc->stts_data[i].count= sample_count; |
| sc->stts_data[i].duration= sample_duration; |
| |
| av_log(c->fc, AV_LOG_TRACE, "sample_count=%d, sample_duration=%d\n", |
| sample_count, sample_duration); |
| |
| if ( i+1 == entries |
| && i |
| && sample_count == 1 |
| && total_sample_count > 100 |
| && sample_duration/10 > duration / total_sample_count) |
| sample_duration = duration / total_sample_count; |
| duration+=(int64_t)sample_duration*sample_count; |
| total_sample_count+=sample_count; |
| } |
| |
| sc->stts_count = i; |
| |
| sc->duration_for_fps += duration; |
| sc->nb_frames_for_fps += total_sample_count; |
| |
| if (pb->eof_reached) |
| return AVERROR_EOF; |
| |
| st->nb_frames= total_sample_count; |
| if (duration) |
| st->duration= duration; |
| sc->track_end = duration; |
| return 0; |
| } |
| |
| static void mov_update_dts_shift(MOVStreamContext *sc, int duration) |
| { |
| if (duration < 0) { |
| if (duration == INT_MIN) { |
| av_log(NULL, AV_LOG_WARNING, "mov_update_dts_shift(): dts_shift set to %d\n", INT_MAX); |
| duration++; |
| } |
| sc->dts_shift = FFMAX(sc->dts_shift, -duration); |
| } |
| } |
| |
| static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
| { |
| AVStream *st; |
| MOVStreamContext *sc; |
| unsigned int i, entries; |
| |
| if (c->fc->nb_streams < 1) |
| return 0; |
| st = c->fc->streams[c->fc->nb_streams-1]; |
| sc = st->priv_data; |
| |
| avio_r8(pb); /* version */ |
| avio_rb24(pb); /* flags */ |
| entries = avio_rb32(pb); |
| |
| av_log(c->fc, AV_LOG_TRACE, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries); |
| |
| if (!entries) |
| return 0; |
| if (entries >= UINT_MAX / sizeof(*sc->ctts_data)) |
| return AVERROR_INVALIDDATA; |
| av_freep(&sc->ctts_data); |
| sc->ctts_data = av_realloc(NULL, entries * sizeof(*sc->ctts_data)); |
| if (!sc->ctts_data) |
| return AVERROR(ENOMEM); |
| |
| for (i = 0; i < entries && !pb->eof_reached;
|