/amffmpeg/libavformat/mov.c
C | 2833 lines | 2297 code | 354 blank | 182 comment | 686 complexity | 520dfe3f0762d8393ec2ecf855d6a51b MD5 | raw file
Possible License(s): LGPL-3.0, CC-BY-SA-3.0, GPL-2.0, GPL-3.0, LGPL-2.1
Large files files are truncated, but you can click here to view the full file
- /*
- * MOV demuxer
- * Copyright (c) 2001 Fabrice Bellard
- * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
- *
- * This file is part of FFmpeg.
- *
- * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * FFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
- #include <limits.h>
- //#define DEBUG
- //#define MOV_EXPORT_ALL_METADATA
- #include "libavutil/intreadwrite.h"
- #include "libavutil/avstring.h"
- #include "libavutil/dict.h"
- #include "avformat.h"
- #include "avio_internal.h"
- #include "riff.h"
- #include "isom.h"
- #include "libavcodec/get_bits.h"
- #if CONFIG_ZLIB
- #include <zlib.h>
- #endif
- /*
- * First version by Francois Revol revol@free.fr
- * Seek function by Gael Chardon gael.dev@4now.net
- *
- * Features and limitations:
- * - reads most of the QT files I have (at least the structure),
- * Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html
- * - the code is quite ugly... maybe I won't do it recursive next time :-)
- *
- * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/
- * when coding this :) (it's a writer anyway)
- *
- * Reference documents:
- * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt
- * Apple:
- * http://developer.apple.com/documentation/QuickTime/QTFF/
- * http://developer.apple.com/documentation/QuickTime/QTFF/qtff.pdf
- * QuickTime is a trademark of Apple (AFAIK :))
- */
- #include "qtpalette.h"
- #undef NDEBUG
- #include <assert.h>
- /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
- /* those functions parse an atom */
- /* return code:
- 0: continue to parse next atom
- <0: error occurred, exit
- */
- /* links atom IDs to parse functions */
- typedef struct MOVParseTableEntry {
- uint32_t type;
- int (*parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom);
- } MOVParseTableEntry;
- static const MOVParseTableEntry mov_default_parse_table[];
- #define MAX_READ_SEEK (1024*1024*3-32*1024)//DEF_MAX_READ_SEEK-block_read_size
- static int mov_metadata_track_or_disc_number(MOVContext *c, AVIOContext *pb, unsigned len, const char *type)
- {
- char buf[16];
- avio_rb16(pb); // unknown
- snprintf(buf, sizeof(buf), "%d", avio_rb16(pb));
- av_dict_set(&c->fc->metadata, type, buf, 0);
- avio_rb16(pb); // total tracks/discs
- 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
- PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;);
- }
- *p = 0;
- return p - dst;
- }
- static int mov_extract_cover_pic(AVFormatContext *s, AVIOContext *pb, int type, int size, char *value)
- {
- if(s->cover_data){
- av_log(s, AV_LOG_INFO, "Extract cover picture in other atom!\n");
- return 0;
- }
- s->cover_data = av_malloc(size);
- if(!s->cover_data){
- av_log(s, AV_LOG_INFO, "no memery, av_alloc failed!\n");
- return -1;
- }
- s->cover_data_len = size;
- avio_read(pb, s->cover_data, size);
- if (type == 13)
- strcpy(value, "image/jpeg"); // jpeg
- else if (type == 14)
- strcpy(value, "image/png"); // png
- return 0;
- }
- static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom)
- {
- #ifdef MOV_EXPORT_ALL_METADATA
- char tmp_key[5];
- #endif
- char str[1024], key2[16], language[4] = {0};
- const char *key = NULL;
- uint16_t str_size, langcode = 0;
- uint32_t cover_size = 0;
- uint32_t data_type = 0;
- 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;
- case MKTAG(0xa9,'g','r','p'): key = "grouping"; break;
- case MKTAG(0xa9,'l','y','r'): key = "lyrics"; break;
- case MKTAG(0xa9,'c','m','t'):
- case MKTAG(0xa9,'i','n','f'): key = "comment"; break;
- case MKTAG(0xa9,'a','l','b'): key = "album"; break;
- case MKTAG(0xa9,'d','a','y'): key = "date"; break;
- case MKTAG(0xa9,'g','e','n'): key = "genre"; 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;
- case MKTAG( 'd','e','s','c'): key = "description";break;
- case MKTAG( 'l','d','e','s'): key = "synopsis"; break;
- case MKTAG( 't','v','s','h'): key = "show"; break;
- case MKTAG( 't','v','e','n'): key = "episode_id";break;
- case MKTAG( 't','v','n','n'): key = "network"; break;
- case MKTAG( 't','r','k','n'): key = "track";
- parse = mov_metadata_track_or_disc_number; break;
- case MKTAG( 'd','i','s','k'): key = "disc";
- parse = mov_metadata_track_or_disc_number; break;
- case MKTAG( 'c','o','v','r'): key = "cover_pic"; break;
- case MKTAG(0xa9,'x','y','z'): key = "GPSCoordinates"; break;
- }
- 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_type = avio_rb32(pb); // type
- avio_rb32(pb); // unknown
- str_size = data_size - 16;
- cover_size = data_size -16;
- atom.size -= 16;
- } else return 0;
- } else if (atom.size > 4 && key && !c->itunes_metadata) {
- str_size = avio_rb16(pb); // string length
- langcode = avio_rb16(pb);
- ff_mov_lang_to_iso639(langcode, language);
- atom.size -= 4;
- } else
- str_size = atom.size;
- #ifdef MOV_EXPORT_ALL_METADATA
- if (!key) {
- snprintf(tmp_key, 5, "%.4s", (char*)&atom.type);
- key = tmp_key;
- }
- #endif
- if (!key)
- return 0;
- if (atom.size < 0)
- return -1;
- str_size = FFMIN3(sizeof(str)-1, str_size, atom.size);
- if (parse)
- 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 if (data_type == 13 || data_type == 14){
- mov_extract_cover_pic(c->fc, pb, data_type, cover_size, str);
- } else {
- avio_read(pb, str, str_size);
- str[str_size] = 0;
- }
- // Android MP4 writer put an additional '/' at the end, discard it.
- // The CTS test seems the added '/' is not needed.
- if ((atom.type == MKTAG(0xa9,'x','y','z')) && (str[str_size-1] == 0x2f)) {
- str[str_size-1] = 0;
- }
- 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);
- }
- }
- 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;
- }
- static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
- {
- int64_t start;
- int i, nb_chapters, str_len, version;
- char str[256+1];
- if ((atom.size -= 5) < 0)
- return 0;
- version = avio_r8(pb);
- avio_rb24(pb);
- if (version)
- avio_rb32(pb); // ???
- nb_chapters = avio_r8(pb);
- for (i = 0; i < nb_chapters; i++) {
- if (atom.size < 9)
- return 0;
- start = avio_rb64(pb);
- str_len = avio_r8(pb);
- if ((atom.size -= 9+str_len) < 0)
- return 0;
- avio_read(pb, str, str_len);
- str[str_len] = 0;
- ff_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str);
- }
- return 0;
- }
- static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom)
- {
- int64_t total_size = 0;
- MOVAtom a;
- int i;
- int searchtag = 0;/*found a error tag,changed to search mod*/
- int serachenable = (atom.type==MKTAG('r','o','o','t'));
-
- if (atom.size < 0)
- atom.size = INT64_MAX;
- while (total_size + 8 < atom.size && !url_feof(pb) && !url_interrupt_cb()) {
- int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;
- a.size = atom.size;
- a.type=0;
- 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);
- //av_log(c->fc, AV_LOG_INFO, "type: %08x '%.4s' offset=%llx aize=%llx atome.size=%llx total_size=%llx\n", a.type, (char*)&a.type,avio_tell(pb), a.size, atom.size, total_size);
- total_size += 8;
- if (a.size == 1) { /* 64 bit extended size */
- a.size = avio_rb64(pb) - 8;
- total_size += 8;
- }
- if (a.size == 0) {
- a.size = atom.size - total_size;
- if (a.size <= 8){
- av_log(c->fc, AV_LOG_INFO, "L%d: a.size (%x)<8, break!\n", __LINE__, a.size);
- break;
- }
- }
- a.size -= 8;
- if (!c->found_moov && (a.size > atom.size - total_size)) {
- if (!searchtag) {
- searchtag = serachenable;
- //av_log(c->fc, AV_LOG_INFO, "******set search mode************\n");
- }
- continue;
- }
-
- if(a.size < 0){
- av_log(c->fc, AV_LOG_INFO, "L%d: a.size (%x)<8, break!\n", __LINE__, a.size);
- break;
- }
-
- a.size = FFMIN(a.size, atom.size - total_size);
- for (i = 0; mov_default_parse_table[i].type; i++)
- if (mov_default_parse_table[i].type == a.type) {
- parse = mov_default_parse_table[i].parse;
- if (searchtag) {
- searchtag = 0;/*exit serach mode*/
- //av_log(c->fc, AV_LOG_INFO, "******exit search mode************\n");
- }
- break;
- }
-
- // container is user data
- if (!parse && (atom.type == MKTAG('u','d','t','a') ||
- atom.type == MKTAG('i','l','s','t')))
- parse = mov_read_udta_string;
- if (!parse) { /* skip leaf atoms data */
- if(!searchtag)
- avio_skip(pb, a.size);
- else
- continue;
- } else {
- 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 &&
- (!pb->seekable ||pb->is_slowmedia ||pb->is_streamed|| start_pos + a.size == avio_size(pb)))
- return 0;/*can't seek,slowmedia,streamed all don't do else parser now*/
- left = a.size - avio_tell(pb) + start_pos;
- if (left > 0){ /* skip garbage at atom end */
- avio_skip(pb, left);
- }
- }
- total_size += a.size;
- }
- if (total_size < atom.size && atom.size < 0x7ffff)
- avio_skip(pb, atom.size - total_size);
- if (url_interrupt_cb()) {
- av_log(NULL, AV_LOG_WARNING, "mov_read_default interrupt, exit\n");
- return AVERROR_EXIT;
- }
- return 0;
- }
- 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 >= UINT_MAX / sizeof(*sc->drefs))
- return -1;
- sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
- if (!sc->drefs)
- return AVERROR(ENOMEM);
- sc->drefs_count = entries;
- for (i = 0; i < sc->drefs_count; i++) {
- MOVDref *dref = &sc->drefs[i];
- uint32_t size = avio_rb32(pb);
- int64_t next = avio_tell(pb) + size - 4;
- if (size < 12)
- return -1;
- dref->type = avio_rl32(pb);
- avio_rb32(pb); // version + flags
- av_dlog(c->fc, "type %.4s size %d\n", (char*)&dref->type, size);
- if (dref->type == MKTAG('a','l','i','s') && size > 150) {
- /* macintosh alias record */
- uint16_t volume_len, len;
- int16_t type;
- avio_skip(pb, 10);
- volume_len = avio_r8(pb);
- volume_len = FFMIN(volume_len, 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);
- avio_skip(pb, 12);
- len = avio_r8(pb);
- len = FFMIN(len, 63);
- avio_read(pb, dref->filename, 63);
- dref->filename[len] = 0;
- av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len);
- avio_skip(pb, 16);
- /* read next level up_from_alias/down_to_target */
- dref->nlvl_from = avio_rb16(pb);
- dref->nlvl_to = avio_rb16(pb);
- av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n",
- dref->nlvl_from, dref->nlvl_to);
- avio_skip(pb, 16);
- for (type = 0; type != -1 && avio_tell(pb) < next; ) {
- type = avio_rb16(pb);
- len = avio_rb16(pb);
- av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
- if (len&1)
- len += 1;
- if (type == 2) { // absolute path
- av_free(dref->path);
- dref->path = av_mallocz(len+1);
- if (!dref->path)
- return AVERROR(ENOMEM);
- avio_read(pb, dref->path, len);
- if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) {
- len -= volume_len;
- memmove(dref->path, dref->path+volume_len, len);
- dref->path[len] = 0;
- }
- for (j = 0; j < len; j++)
- if (dref->path[j] == ':')
- dref->path[j] = '/';
- av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
- } else if (type == 0) { // directory name
- av_free(dref->dir);
- dref->dir = av_malloc(len+1);
- if (!dref->dir)
- return AVERROR(ENOMEM);
- avio_read(pb, dref->dir, len);
- dref->dir[len] = 0;
- for (j = 0; j < len; j++)
- if (dref->dir[j] == ':')
- dref->dir[j] = '/';
- av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir);
- } else
- avio_skip(pb, len);
- }
- }
- 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;
- if (c->fc->nb_streams < 1) // meta before first trak
- return 0;
- st = c->fc->streams[c->fc->nb_streams-1];
- avio_r8(pb); /* version */
- avio_rb24(pb); /* flags */
- /* component type */
- 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'))
- st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
- else if(type == MKTAG('m','1','a',' '))
- st->codec->codec_id = CODEC_ID_MP2;
- else if(type == MKTAG('s','u','b','p'))
- st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
- avio_rb32(pb); /* component manufacture */
- avio_rb32(pb); /* component flags */
- avio_rb32(pb); /* component flags mask */
- return 0;
- }
- int ff_mov_read_esds(AVFormatContext *fc, AVIOContext *pb, MOVAtom atom)
- {
- 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) {
- avio_rb16(pb); /* ID */
- avio_r8(pb); /* priority */
- } 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, atom);
- }
- static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
- {
- AVStream *st;
- int ac3info, acmod, lfeon, bsmod;
- if (c->fc->nb_streams < 1)
- return 0;
- st = c->fc->streams[c->fc->nb_streams-1];
- ac3info = avio_rb24(pb);
- bsmod = (ac3info >> 14) & 0x7;
- acmod = (ac3info >> 11) & 0x7;
- lfeon = (ac3info >> 10) & 0x1;
- st->codec->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon;
- st->codec->audio_service_type = bsmod;
- if (st->codec->channels > 1 && bsmod == 0x7)
- st->codec->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
- return 0;
- }
- static int mov_read_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 = 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) {
- st->sample_aspect_ratio.num = num;
- st->sample_aspect_ratio.den = den;
- }
- 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;
- /*
- * Lujian.Hu 2012-12-14
- * only use media_dataoffset to time_search(player_av.c) can not work correctly, because in mov_read_seek not only modify the pos to
- * media data offset but reset the sample pointer to the correct position according to the very timestamp
- */
- //c->fc->media_dataoffset = url_ftell(pb);
- return 0; /* now go for moov */
- }
- /* 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 minor_ver_str[11]; /* 32 bit integer -> 10 digits + null */
- char* comp_brands_str;
- uint8_t type[5] = {0};
- 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_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_dict_set(&c->fc->metadata, "minor_version", minor_ver_str, 0);
- comp_brand_size = atom.size - 8;
- if (comp_brand_size < 0)
- return -1;
- comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */
- if (!comp_brands_str)
- return AVERROR(ENOMEM);
- avio_read(pb, comp_brands_str, comp_brand_size);
- 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)
- {
- if (mov_read_default(c, pb, atom) < 0)
- return -1;
- /* 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)
- {
- 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(AVDictionary **metadata, time_t time)
- {
- char buffer[32];
- if (time) {
- struct tm *ptm;
- time -= 2082844800; /* seconds between 1904-01-01 and Epoch */
- ptm = gmtime(&time);
- if (!ptm) return;
- strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", ptm);
- av_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;
- time_t creation_time;
- if (c->fc->nb_streams < 1)
- return 0;
- st = c->fc->streams[c->fc->nb_streams-1];
- sc = st->priv_data;
- version = avio_r8(pb);
- if (version > 1)
- return -1; /* unsupported */
- 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)
- {
- time_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_dlog(c->fc, "time scale = %i\n", c->time_scale);
- c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
- 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_smi(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((uint64_t)atom.size > (1<<30))
- return -1;
- // currently SVQ3 decoder expect full STSD header - so let's fake it
- // this should be fixed and just SMI header should be passed
- av_free(st->codec->extradata);
- st->codec->extradata = av_mallocz(atom.size + 0x5a + FF_INPUT_BUFFER_PADDING_SIZE);
- if (!st->codec->extradata)
- return AVERROR(ENOMEM);
- st->codec->extradata_size = 0x5a + atom.size;
- memcpy(st->codec->extradata, "SVQ3", 4); // fake
- avio_read(pb, st->codec->extradata + 0x5a, atom.size);
- av_dlog(c->fc, "Reading SMI %"PRId64" %s\n", atom.size, st->codec->extradata + 0x5a);
- return 0;
- }
- static int mov_read_enda(MOVContext *c, 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_dlog(c->fc, "enda %d\n", little_endian);
- if (little_endian == 1) {
- switch (st->codec->codec_id) {
- case CODEC_ID_PCM_S24BE:
- st->codec->codec_id = CODEC_ID_PCM_S24LE;
- break;
- case CODEC_ID_PCM_S32BE:
- st->codec->codec_id = CODEC_ID_PCM_S32LE;
- break;
- case CODEC_ID_PCM_F32BE:
- st->codec->codec_id = CODEC_ID_PCM_F32LE;
- break;
- case CODEC_ID_PCM_F64BE:
- st->codec->codec_id = CODEC_ID_PCM_F64LE;
- break;
- default:
- break;
- }
- }
- 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)
- {
- AVStream *st;
- uint64_t size;
- uint8_t *buf;
- if (c->fc->nb_streams < 1) // will happen with jp2 files
- 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;
- buf= av_realloc(st->codec->extradata, size);
- if(!buf)
- return -1;
- 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);
- avio_read(pb, buf + 8, atom.size);
- return 0;
- }
- static int mov_read_wave(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((uint64_t)atom.size > (1<<30))
- return -1;
- if (st->codec->codec_id == CODEC_ID_QDM2 || st->codec->codec_id == CODEC_ID_QDMC) {
- // pass all frma atom to codec, needed at least for QDMC and QDM2
- av_free(st->codec->extradata);
- st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
- if (!st->codec->extradata)
- return AVERROR(ENOMEM);
- st->codec->extradata_size = atom.size;
- avio_read(pb, st->codec->extradata, atom.size);
- } else if (atom.size > 8) { /* to read frma, esds atoms */
- if (mov_read_default(c, pb, atom) < 0)
- return -1;
- } else
- 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;
- 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 -1;
- 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;
- avio_read(pb, st->codec->extradata, atom.size);
- return 0;
- }
- /**
- * An mvcC atom contains the necessary information about the multiview data for
- * 3D content. This function reads atom content and puts data in extradata.
- */
- static int mov_read_mvcC(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];
- st->codec->codec_id = CODEC_ID_H264MVC;
- return mov_read_glbl(c, pb, atom);
- }
- /**
- * An strf atom is a BITMAPINFOHEADER struct. This struct is 40 bytes itself,
- * but can have extradata appended at the end after the 40 bytes belonging
- * to the struct.
- */
- static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom)
- {
- AVStream *st;
- if (c->fc->nb_streams < 1)
- return 0;
- if (atom.size <= 40)
- return 0;
- st = c->fc->streams[c->fc->nb_streams-1];
- if((uint64_t)atom.size > (1<<30))
- return -1;
- av_free(st->codec->extradata);
- st->codec->extradata = av_mallocz(atom.size - 40 + FF_INPUT_BUFFER_PADDING_SIZE);
- if (!st->codec->extradata)
- return AVERROR(ENOMEM);
- st->codec->extradata_size = atom.size - 40;
- avio_skip(pb, 40);
- avio_read(pb, st->codec->extradata, atom.size - 40);
- return 0;
- }
- static int mov_read_stco(MOVContext *c, 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 >= UINT_MAX/sizeof(int64_t))
- return -1;
- sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
- if (!sc->chunk_offsets)
- return AVERROR(ENOMEM);
- sc->chunk_count = entries;
- if (atom.type == MKTAG('s','t','c','o'))
- for(i=0; i<entries; i++)
- sc->chunk_offsets[i] = avio_rb32(pb);
- else if (atom.type == MKTAG('c','o','6','4'))
- for(i=0; i<entries; i++)
- sc->chunk_offsets[i] = avio_rb64(pb);
- else
- return -1;
- return 0;
- }
- /**
- * Compute codec id for 'lpcm' tag.
- * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
- */
- enum CodecID ff_mov_get_lpcm_codec_id(int bps, int flags)
- {
- if (flags & 1) { // floating point
- if (flags & 2) { // big endian
- if (bps == 32) return CODEC_ID_PCM_F32BE;
- else if (bps == 64) return CODEC_ID_PCM_F64BE;
- } else {
- if (bps == 32) return CODEC_ID_PCM_F32LE;
- else if (bps == 64) return CODEC_ID_PCM_F64LE;
- }
- } else {
- if (flags & 2) {
- if (bps == 8)
- // signed integer
- if (flags & 4) return CODEC_ID_PCM_S8;
- else return CODEC_ID_PCM_U8;
- else if (bps == 16) return CODEC_ID_PCM_S16BE;
- else if (bps == 24) return CODEC_ID_PCM_S24BE;
- else if (bps == 32) return CODEC_ID_PCM_S32BE;
- } else {
- if (bps == 8)
- if (flags & 4) return CODEC_ID_PCM_S8;
- else return CODEC_ID_PCM_U8;
- else if (bps == 16) return CODEC_ID_PCM_S16LE;
- else if (bps == 24) return CODEC_ID_PCM_S24LE;
- else if (bps == 32) return CODEC_ID_PCM_S32LE;
- }
- }
- return CODEC_ID_NONE;
- }
- int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
- {
- AVStream *st;
- MOVStreamContext *sc;
- int j, pseudo_stream_id;
- if (c->fc->nb_streams < 1)
- return 0;
- st = c->fc->streams[c->fc->nb_streams-1];
- sc = st->priv_data;
- for(pseudo_stream_id=0; pseudo_stream_id<entries; pseudo_stream_id++) {
- //Parsing Sample description table
- enum CodecID id;
- int dref_id = 1;
- MOVAtom a = { AV_RL32("stsd") };
- int64_t start_pos = avio_tell(pb);
- int size = avio_rb32(pb); /* size */
- uint32_t format = avio_rl32(pb); /* data format */
- if (size >= 16) {
- avio_rb32(pb); /* reserved */
- avio_rb16(pb); /* reserved */
- dref_id = avio_rb16(pb);
- }
- if (st->codec->codec_tag &&
- st->codec->codec_tag != format &&
- (c->fc->video_codec_id ? ff_codec_get_id(codec_movvideo_tags, format) != c->fc->video_codec_id
- : st->codec->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. */
- multiple_stsd:
- av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
- avio_skip(pb, size - (avio_tell(pb) - start_pos));
- continue;
- }
- /* we cannot demux concatenated h264 streams because of different extradata */
- if (st->codec->codec_tag && st->codec->codec_tag == AV_RL32("avc1"))
- goto multiple_stsd;
- sc->pseudo_stream_id = st->codec->codec_tag ? -1 : pseudo_stream_id;
- sc->dref_id= dref_id;
- st->codec->codec_tag = format;
- id = ff_codec_get_id(codec_movaudio_tags, format);
- if (id<=0 && ((format&0xFFFF) == 'm'+('s'<<8) || (format&0xFFFF) == 'T'+('S'<<8)))
- id = ff_codec_get_id(ff_codec_wav_tags, 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 && /* do not overwrite codec type */
- format && format != MKTAG('m','p','4','s')) { /* skip old asf mpeg4 tag */
- id = ff_codec_get_id(codec_movvideo_tags, format);
- if (id <= 0)
- id = ff_codec_get_id(ff_codec_bmp_tags, format);
- if (id > 0)
- st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
- else if(st->codec->codec_type == AVMEDIA_TYPE_DATA){
- id = ff_codec_get_id(ff_codec_movsubtitle_tags, format);
- if(id > 0)
- st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
- }
- }
- av_dlog(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n", size,
- (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff,
- (format >> 24) & 0xff, st->codec->codec_type);
- if(st->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
- unsigned int color_depth, len;
- int color_greyscale;
- if ((id != CODEC_ID_H264) || (st->codec->codec_id != CODEC_ID_H264MVC)) {
- st->codec->codec_id = id;
- } else {
- st->codec->codec_tag = CODEC_ID_H264MVC;
- av_dlog(c->fc, "code_tag set to MVC with mvcC tag\n");
- }
- avio_rb16(pb); /* version */
- avio_rb16(pb); /* revision level */
- avio_rb32(pb); /* vendor */
- avio_rb32(pb); /* temporal quality */
- avio_rb32(pb); /* spatial quality */
- st->codec->width = avio_rb16(pb); /* width */
- st->codec->height = avio_rb16(pb); /* height */
- avio_rb32(pb); /* horiz resolution */
- avio_rb32(pb); /* vert resolution */
- avio_rb32(pb); /* data size, always 0 */
- avio_rb16(pb); /* frames per samples */
- len = avio_r8(pb); /* codec name, pascal string */
- if (len > 31)
- len = 31;
- mov_read_mac_string(c, pb, len, st->codec->codec_name, 32);
- if (len < 31)
- avio_skip(pb, 31 - len);
- /* codec_tag YV12 triggers an UV swap in rawdec.c */
- if (!memcmp(st->codec->codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25))
- st->codec->codec_tag=MKTAG('I', '4', '2', '0');
- st->codec->bits_per_coded_sample = avio_rb16(pb); /* depth */
- st->codec->color_table_id = avio_rb16(pb); /* colortable id */
- av_dlog(c->fc, "depth %d, ctab id %d\n",
- st->codec->bits_per_coded_sample, st->codec->color_table_id);
- /* figure out the palette situation */
- color_depth = st->codec->bits_per_coded_sample & 0x1F;
- color_greyscale = st->codec->bits_per_coded_sample & 0x20;
- /* if the depth is 2, 4, or 8 bpp, file is palettized */
- if ((color_depth == 2) || (color_depth == 4) ||
- (color_depth == 8)) {
- /* for palette traversal */
- unsigned int color_start, color_count, color_end;
- unsigned char r, g, b;
- if (color_greyscale) {
- int color_index, color_dec;
- /* compute the greyscale palette */
- st->codec->bits_per_coded_sample = color_depth;
- color_count = 1 << color_depth;
- color_index = 255;
- color_dec = 256 / (color_count - 1);
- for (j = 0; j < color_count; j++) {
- r = g = b = color_index;
- sc->palette[j] =
- (r << 16) | (g << 8) | (b);
- color_index -= color_dec;
- if (color_index < 0)
- color_index = 0;
- }
- } else if (st->codec->color_table_id) {
- const uint8_t *color_table;
- /* if flag bit 3 is set, use the default palette */
- color_count = 1 << color_depth;
- if (color_depth == 2)
- color_table = ff_qt_default_palette_4;
- else if (color_depth == 4)
- color_table = ff_qt_default_palette_16;
- else
- color_table = ff_qt_default_palette_256;
- for (j = 0; j < color_count; j++) {
- r = color_table[j * 3 + 0];
- g = color_table[j * 3 + 1];
- b = color_table[j * 3 + 2];
- sc->palette[j] =
- (r << 16) | (g << 8) | (b);
- }
- } else {
- /* load the palette from the file */
- 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 */
- 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);
- }
- }
- }
- sc->has_palette = 1;
- }
- } else if(st->codec->codec_type==AVMEDIA_TYPE_AUDIO) {
- int bits_per_sample, flags;
- uint16_t version = avio_rb16(pb);
- st->codec->codec_id = id;
- avio_rb16(pb); /* revision level */
- avio_rb32(pb); /* vendor */
- st->codec->channels = avio_rb16(pb); /* channel count */
- av_dlog(c->fc, "audio channels %d\n", st->codec->channels);
- st->codec->bits_per_coded_sample = avio_rb16(pb); /* sample size */
- sc->audio_cid = 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_dlog(c->fc, "version =%d, isom =%d\n",version,c->isom);
- 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_int2dbl(avio_rb64(pb)); /* float 64 */
- st->codec->channels = avio_rb32(pb);
- avio_rb32(pb); /* always 0x7F000000 */
- st->codec->bits_per_coded_sample = avio_rb32(pb); /* bits per channel if sound is uncompressed */
- flags = avio_rb32(pb); /* lpcm format specific flag */
- sc->bytes_per_frame = avio_rb32(pb); /* bytes per audio packet if constant */
- sc->samples_per_frame = avio_rb32(pb); /* lpcm frames per audio packet if constant */
- if (format == MKTAG('l','p','c','m'))
- st->codec->codec_id = ff_mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample, flags);
- }
- }
- switch (st->codec->codec_id) {
- case CODEC_ID_PCM_S8:
- case CODEC_ID_PCM_U8:
- if (st->codec->bits_per_coded_sample == 16)
- st->codec->codec_id = CODEC_ID_PCM_S16BE;
- break;
- case CODEC_ID_PCM_S16LE:
- case CODEC_ID_PCM_S16BE:
- if (st->codec->bits_per_coded_sample == 8)
- st->codec->codec_id = CODEC_ID_PCM_S8;
- else if (st->codec->bits_per_coded_sample == 24)
- st->codec->codec_id =
- st->codec->codec_id == CODEC_ID_PCM_S16BE ?
- CODEC_ID_PCM_S24BE : CODEC_ID_PCM_S24LE;
- break;
- /* set values for old format before stsd version 1 appeared */
- case CODEC_ID_MACE3:
- sc->samples_per_frame = 6;
- sc->bytes_per_frame = 2*st->codec->channels;
- break;
- case CODEC_ID_MACE6:
- sc->samples_per_frame = 6;
- sc->bytes_per_frame = 1*st->codec->channels;
- break;
- case CODEC_ID_ADPCM_IMA_QT:
- sc->samples_per_frame = 64;
- sc->bytes_per_frame = 34*st->codec->channels;
- break;
- case 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;
- }
- } 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 - (avio_tell(pb) - start_pos) };
- if (format != AV_RL32("mp4s")) // mp4s contains a regular esds atom
- mov_read_glbl(c, pb, fake_atom);
- st->codec->codec_id= id;
- st->codec->width = sc->width;
- st->codec->height = sc->height;
- } else {
- /* other codec type, just skip (rtp, mp4s, tmcd ...) */
- 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 - (avio_tell(pb) - start_pos);
- if (a.size > 8) {
- if (mov_read_default(c, pb, a) < 0)
- return -1;
- } else if (a.size > 0)
- avio_skip(pb, a.size);
- }
- 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 */
- switch (st->codec->codec_id) {
- #if CONFIG_DV_DEMUXER
- case CODEC_ID_DVAUDIO:
- c->dv_fctx = avformat_alloc_context();
- c->dv_demux = 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;
- }
- sc->dv_audio_container = 1;
- st->codec->codec_id = CODEC_ID_PCM_S16LE;
- break;
- #endif
- /* no ifdef since parameters are always those */
- case CODEC_ID_QCELP:
- // 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;
- st->codec->frame_size= 160;
- 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;
- break;
- case CODEC_ID_MP2:
- case CODEC_ID_MP3:
- st->codec->codec_type = AVMEDIA_TYPE_AUDIO; /* force type after stsd for m1a hdlr */
- st->need_parsing = AVSTREAM_PARSE_FULL;
- break;
- case CODEC_ID_GSM:
- case CODEC_ID_ADPCM_MS:
- case CODEC_ID_ADPCM_IMA_WAV:
- st->codec->frame_size = sc->samples_per_frame;
- st->codec->block_align = sc->bytes_per_frame;
- break;
- case CODEC_ID_ALAC:
- if (st->codec->extradata_size == 36) {
- st->codec->frame_size = AV_RB32(st->codec->extradata+12);
- st->codec->channels = AV_RB8 (st->codec->extradata+21);
- st->codec->sample_rate = AV_RB32(st->codec->extradata+32);
- }
- break;
- default:
- break;
- }
- return 0;
- }
- 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_dlog(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
- if(entries >= UINT_MAX / sizeof(*sc->stsc_data))
- return -1;
- sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data));
- if (!sc->stsc_data)
- return AVERROR(ENOMEM);
- sc->stsc_count = entries;
- for(i=0; i<entries; 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);
- }
- 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 (entries >= UINT_MAX / sizeof(*sc->stps_data))
- return -1;
- 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] = avio_rb32(pb);
- //av_dlog(c->fc, "stps %d\n", sc->stps_data[i]);
- }
- 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_dlog(c->fc, "keyframe_count = %d\n", entries);
- if(entries >= UINT_MAX / sizeof(int))
- …
Large files files are truncated, but you can click here to view the full file