Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
8010 lines (6964 sloc) 275 KB
/*
* 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/avassert.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/pixdesc.h"
#include "libavutil/sha.h"
#include "libavutil/spherical.h"
#include "libavutil/stereo3d.h"
#include "libavutil/timecode.h"
#include "libavcodec/ac3tab.h"
#include "libavcodec/flac.h"
#include "libavcodec/mpegaudiodecheader.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 int64_t add_ctts_entry(MOVStts** ctts_data, unsigned int* ctts_count, unsigned int* allocated_size,
int count, int duration);
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 (p >= end)
continue;
if (c < 0x80)
*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->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
st->codecpar->codec_id = id;
return 0;
}
// 3GPP TS 26.244
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,
"loci too short (%u bytes left, need at least %d)\n", len, 12);
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_metadata_hmmt(MOVContext *c, AVIOContext *pb, unsigned len)
{
int i, n_hmmt;
if (len < 2)
return 0;
if (c->ignore_chapters)
return 0;
n_hmmt = avio_rb32(pb);
for (i = 0; i < n_hmmt && !pb->eof_reached; i++) {
int moment_time = avio_rb32(pb);
avpriv_new_chapter(c->fc, i, av_make_q(1, 1000), moment_time, AV_NOPTS_VALUE, NULL);
}
return 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( 'F','I','R','M'): key = "firmware"; raw = 1; 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( 'H','M','M','T'):
return mov_metadata_hmmt(c, pb, atom.size);
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( 'm','a','n','u'): key = "make"; break;
case MKTAG( 'm','o','d','l'): key = "model"; break;
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;
}
atom.size -= str_size;
if (atom.size > 8)
goto retry;
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 && index > 0) {
key = c->meta_keys[index];
} else {
av_log(c->fc, AV_LOG_WARNING,
"The index of 'data' is out of range: %"PRId32" < 1 or >= %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 int32 or float32 number, otherwise
// worst-case requirement for output string in case of utf8 coded input
num = (data_type >= 21 && 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 == 21) { // BE signed integer, variable size
int val = 0;
if (str_size == 1)
val = (int8_t)avio_r8(pb);
else if (str_size == 2)
val = (int16_t)avio_rb16(pb);
else if (str_size == 3)
val = ((int32_t)(avio_rb24(pb)<<8))>>8;
else if (str_size == 4)
val = (int32_t)avio_rb32(pb);
if (snprintf(str, str_size_alloc, "%d", val) >= str_size_alloc) {
av_log(c->fc, AV_LOG_ERROR,
"Failed to store the number (%d) in string.\n", val);
av_free(str);
return AVERROR_INVALIDDATA;
}
} else if (data_type == 22) { // BE unsigned integer, variable size
unsigned int val = 0;
if (str_size == 1)
val = avio_r8(pb);
else if (str_size == 2)
val = avio_rb16(pb);
else if (str_size == 3)
val = avio_rb24(pb);
else if (str_size == 4)
val = avio_rb32(pb);
if (snprintf(str, str_size_alloc, "%u", val) >= str_size_alloc) {
av_log(c->fc, AV_LOG_ERROR,
"Failed to store the number (%u) in string.\n", val);
av_free(str);
return AVERROR_INVALIDDATA;
}
} 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, &micro) == 3) {
c->handbrake_version = 1000000*major + 1000*minor + micro;
}
}
}
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 ||
entries > (atom.size - 1) / MIN_DATA_ENTRY_BOX_SIZE + 1 ||
entries >= UINT_MAX / sizeof(*sc->drefs))
return AVERROR_INVALIDDATA;
sc->drefs_count = 0;
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
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;
}
// trim string of any ending zeros
for (j = len - 1; j >= 0; j--) {
if (dref->path[j] == 0)
len--;
else
break;
}
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 0x%08"PRIx32" size %"PRIu32"\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 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=%s\n", av_fourcc2str(ctype));
av_log(c->fc, AV_LOG_TRACE, "stype=%s\n", av_fourcc2str(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->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
else if (type == MKTAG('s','o','u','n'))
st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
else if (type == MKTAG('m','1','a',' '))
st->codecpar->codec_id = AV_CODEC_ID_MP2;
else if ((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p')))
st->codecpar->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) {
if (title_size > FFMIN(INT_MAX, SIZE_MAX-1))
return AVERROR_INVALIDDATA;
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);
// flag added so as to not set stream handler name if already set from mdia->hdlr
av_dict_set(&st->metadata, "handler_name", title_str + off, AV_DICT_DONT_OVERWRITE);
}
av_freep(&title_str);
}
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->codecpar->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon;
st->codecpar->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
if (lfeon)
st->codecpar->channel_layout |= AV_CH_LOW_FREQUENCY;
*ast = bsmod;
if (st->codecpar->channels > 1 && bsmod == 0x7)
*ast = AV_AUDIO_SERVICE_TYPE_KARAOKE;
#if FF_API_LAVF_AVCTX
FF_DISABLE_DEPRECATION_WARNINGS
st->codec->audio_service_type = *ast;
FF_ENABLE_DEPRECATION_WARNINGS
#endif
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->codecpar->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
if (lfeon)
st->codecpar->channel_layout |= AV_CH_LOW_FREQUENCY;
st->codecpar->channels = av_get_channel_layout_nb_channels(st->codecpar->channel_layout);
*ast = bsmod;
if (st->codecpar->channels > 1 && bsmod == 0x7)
*ast = AV_AUDIO_SERVICE_TYPE_KARAOKE;
#if FF_API_LAVF_AVCTX
FF_DISABLE_DEPRECATION_WARNINGS
st->codec->audio_service_type = *ast;
FF_ENABLE_DEPRECATION_WARNINGS
#endif
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 + AV_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) {
av_free(buf);
return 0;
}
st = c->fc->streams[c->fc->nb_streams-1];
st->codecpar->sample_rate = get_bits_long(&gb, 32);
if (st->codecpar->sample_rate <= 0) {
av_log(c->fc, AV_LOG_ERROR, "Invalid sample rate %d\n", st->codecpar->sample_rate);
av_free(buf);
return AVERROR_INVALIDDATA;
}
skip_bits_long(&gb, 32); /* max bitrate */
st->codecpar->bit_rate = get_bits_long(&gb, 32);
st->codecpar->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->codecpar->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->codecpar->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->codecpar->channels = av_get_channel_layout_nb_channels(st->codecpar->channel_layout);
av_free(buf);
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->codecpar, 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(c->fc, 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 MOVFragmentStreamInfo * get_frag_stream_info(
MOVFragmentIndex *frag_index,
int index,
int id)
{
int i;
MOVFragmentIndexItem * item;
if (index < 0 || index >= frag_index->nb_items)
return NULL;
item = &frag_index->item[index];
for (i = 0; i < item->nb_stream_info; i++)
if (item->stream_info[i].id == id)
return &item->stream_info[i];
// This shouldn't happen
return NULL;
}
static void set_frag_stream(MOVFragmentIndex *frag_index, int id)
{
int i;
MOVFragmentIndexItem * item;
if (frag_index->current < 0 ||
frag_index->current >= frag_index->nb_items)
return;
item = &frag_index->item[frag_index->current];
for (i = 0; i < item->nb_stream_info; i++)
if (item->stream_info[i].id == id) {
item->current = i;
return;
}
// id not found. This shouldn't happen.
item->current = -1;
}
static MOVFragmentStreamInfo * get_current_frag_stream_info(
MOVFragmentIndex *frag_index)
{
MOVFragmentIndexItem *item;
if (frag_index->current < 0 ||
frag_index->current >= frag_index->nb_items)
return NULL;
item = &frag_index->item[frag_index->current];
if (item->current >= 0 && item->current < item->nb_stream_info)
return &item->stream_info[item->current];
// This shouldn't happen
return NULL;
}
static int search_frag_moof_offset(MOVFragmentIndex *frag_index, int64_t offset)
{
int a, b, m;
int64_t moof_offset;
// Optimize for appending new entries
if (!frag_index->nb_items ||
frag_index->item[frag_index->nb_items - 1].moof_offset < offset)
return frag_index->nb_items;
a = -1;
b = frag_index->nb_items;
while (b - a > 1) {
m = (a + b) >> 1;
moof_offset = frag_index->item[m].moof_offset;
if (moof_offset >= offset)
b = m;
if (moof_offset <= offset)
a = m;
}
return b;
}
static int64_t get_stream_info_time(MOVFragmentStreamInfo * frag_stream_info)
{
av_assert0(frag_stream_info);
if (frag_stream_info->sidx_pts != AV_NOPTS_VALUE)
return frag_stream_info->sidx_pts;
if (frag_stream_info->first_tfra_pts != AV_NOPTS_VALUE)
return frag_stream_info->first_tfra_pts;
return frag_stream_info->tfdt_dts;
}
static int64_t get_frag_time(MOVFragmentIndex *frag_index,
int index, int track_id)
{
MOVFragmentStreamInfo * frag_stream_info;
int64_t timestamp;
int i;
if (track_id >= 0) {
frag_stream_info = get_frag_stream_info(frag_index, index, track_id);
return frag_stream_info->sidx_pts;
}
for (i = 0; i < frag_index->item[index].nb_stream_info; i++) {
frag_stream_info = &frag_index->item[index].stream_info[i];
timestamp = get_stream_info_time(frag_stream_info);
if (timestamp != AV_NOPTS_VALUE)
return timestamp;
}
return AV_NOPTS_VALUE;
}
static int search_frag_timestamp(MOVFragmentIndex *frag_index,
AVStream *st, int64_t timestamp)
{
int a, b, m, m0;
int64_t frag_time;
int id = -1;
if (st) {
// If the stream is referenced by any sidx, limit the search
// to fragments that referenced this stream in the sidx
MOVStreamContext *sc = st->priv_data;
if (sc->has_sidx)
id = st->id;
}
a = -1;
b = frag_index->nb_items;
while (b - a > 1) {
m0 = m = (a + b) >> 1;
while (m < b &&
(frag_time = get_frag_time(frag_index, m, id)) == AV_NOPTS_VALUE)
m++;
if (m < b && frag_time <= timestamp)
a = m;
else
b = m0;
}
return a;
}
static int update_frag_index(MOVContext *c, int64_t offset)
{
int index, i;
MOVFragmentIndexItem * item;
MOVFragmentStreamInfo * frag_stream_info;
// If moof_offset already exists in frag_index, return index to it
index = search_frag_moof_offset(&c->frag_index, offset);
if (index < c->frag_index.nb_items &&
c->frag_index.item[index].moof_offset == offset)
return index;
// offset is not yet in frag index.
// Insert new item at index (sorted by moof offset)
item = av_fast_realloc(c->frag_index.item,
&c->frag_index.allocated_size,
(c->frag_index.nb_items + 1) *
sizeof(*c->frag_index.item));
if(!item)
return -1;
c->frag_index.item = item;
frag_stream_info = av_realloc_array(NULL, c->fc->nb_streams,
sizeof(*item->stream_info));
if (!frag_stream_info)
return -1;
for (i = 0; i < c->fc->nb_streams; i++) {
// Avoid building frag index if streams lack track id.
if (c->fc->streams[i]->id < 0)
return AVERROR_INVALIDDATA;
frag_stream_info[i].id = c->fc->streams[i]->id;
frag_stream_info[i].sidx_pts = AV_NOPTS_VALUE;
frag_stream_info[i].tfdt_dts = AV_NOPTS_VALUE;
frag_stream_info[i].first_tfra_pts = AV_NOPTS_VALUE;
frag_stream_info[i].index_entry = -1;
frag_stream_info[i].encryption_index = NULL;
}
if (index < c->frag_index.nb_items)
memmove(c->frag_index.item + index + 1, c->frag_index.item + index,
(c->frag_index.nb_items - index) * sizeof(*c->frag_index.item));
item = &c->frag_index.item[index];
item->headers_read = 0;
item->current = 0;
item->nb_stream_info = c->fc->nb_streams;
item->moof_offset = offset;
item->stream_info = frag_stream_info;
c->frag_index.nb_items++;
return index;
}
static void fix_frag_index_entries(MOVFragmentIndex *frag_index, int index,
int id, int entries)
{
int i;
MOVFragmentStreamInfo * frag_stream_info;
if (index < 0)
return;
for (i = index; i < frag_index->nb_items; i++) {
frag_stream_info = get_frag_stream_info(frag_index, i, id);
if (frag_stream_info && frag_stream_info->index_entry >= 0)
frag_stream_info->index_entry += entries;
}
}
static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
// Set by mov_read_tfhd(). mov_read_trun() will reject files missing tfhd.
c->fragment.found_tfhd = 0;
if (!c->has_looked_for_mfra && c->use_mfra_for > 0) {
c->has_looked_for_mfra = 1;
if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
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);
c->frag_index.current = update_frag_index(c, c->fragment.moof_offset);
return mov_read_default(c, pb, atom);
}
static void mov_metadata_creation_time(AVDictionary **metadata, int64_t time)
{
if (time) {
if(time >= 2082844800)
time -= 2082844800; /* seconds between 1904-01-01 and Epoch */
if ((int64_t)(time * 1000000ULL) / 1000000 != time) {
av_log(NULL, AV_LOG_DEBUG, "creation_time is not representable\n");
return;
}
avpriv_dict_set_timestamp(metadata, "creation_time", time * 1000000);
}
}
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);
if (sc->time_scale <= 0) {
av_log(c->fc, AV_LOG_ERROR, "Invalid mdhd time scale %d, defaulting to 1\n", sc->time_scale);
sc->time_scale = 1;
}
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)
{
int i;
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 */
if (c->time_scale <= 0) {
av_log(c->fc, AV_LOG_ERROR, "Invalid mvhd time scale %d, defaulting to 1\n", c->time_scale);
c->time_scale = 1;
}
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 */
/* movie display matrix, store it in main context and use it later on */
for (i = 0; i < 3; i++) {
c->movie_display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
c->movie_display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
c->movie_display_matrix[i][2] = avio_rb32(pb); // 2.30 fixed point
}
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->codecpar->codec_id) {
case AV_CODEC_ID_PCM_S24BE:
st->codecpar->codec_id = AV_CODEC_ID_PCM_S24LE;
break;
case AV_CODEC_ID_PCM_S32BE:
st->codecpar->codec_id = AV_CODEC_ID_PCM_S32LE;
break;
case AV_CODEC_ID_PCM_F32BE:
st->codecpar->codec_id = AV_CODEC_ID_PCM_F32LE;
break;
case AV_CODEC_ID_PCM_F64BE:
st->codecpar->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->codecpar->color_range = AVCOL_RANGE_JPEG;
else
st->codecpar->color_range = AVCOL_RANGE_MPEG;
}
if (!av_color_primaries_name(color_primaries))
color_primaries = AVCOL_PRI_UNSPECIFIED;
if (!av_color_transfer_name(color_trc))
color_trc = AVCOL_TRC_UNSPECIFIED;
if (!av_color_space_name(color_matrix))
color_matrix = AVCOL_SPC_UNSPECIFIED;
st->codecpar->color_primaries = color_primaries;
st->codecpar->color_trc = color_trc;
st->codecpar->color_space = color_matrix;
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->codecpar->field_order = decoded_field_order;
return 0;
}
static int mov_realloc_extradata(AVCodecParameters *par, MOVAtom atom)
{
int err = 0;
uint64_t size = (uint64_t)par->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(&par->extradata, size)) < 0) {
par->extradata_size = 0;
return err;
}
par->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,
AVCodecParameters *par, 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) {
par->extradata_size -= atom.size;
return err;
} else if (err < atom.size) {
av_log(c->fc, AV_LOG_WARNING, "truncated extradata\n");
par->extradata_size -= atom.size - err;
result = err;
}
memset(buf + 8 + err, 0, AV_INPUT_BUFFER_PADDING_SIZE);
return result;
}
/* FIXME modify QDM2/SVQ3/H.264 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->codecpar->codec_id != codec_id)
return 0; /* unexpected codec_id - don't mess with extradata */
original_size = st->codecpar->extradata_size;
err = mov_realloc_extradata(st->codecpar, atom);
if (err)
return err;
err = mov_read_atom_into_extradata(c, pb, atom, st->codecpar, st->codecpar->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) {
AVCodecParameters *par = c->fc->streams[c->fc->nb_streams-1]->codecpar;
if (par->extradata_size >= 40) {
par->height = AV_RB16(&par->extradata[36]);
par->width = AV_RB16(&par->extradata[38]);
}
}
return ret;
}
static int mov_read_ares(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
if (c->fc->nb_streams >= 1) {
AVCodecParameters *par = c->fc->streams[c->fc->nb_streams-1]->codecpar;
if (par->codec_tag == MKTAG('A', 'V', 'i', 'n') &&
par->codec_id == AV_CODEC_ID_H264 &&
atom.size > 11) {
int cid;
avio_skip(pb, 10);
cid = avio_rb16(pb);
/* For AVID AVCI50, force width of 1440 to be able to select the correct SPS and PPS */
if (cid == 0xd4d || cid == 0xd4e)
par->width = 1440;
return 0;
} else if ((par->codec_tag == MKTAG('A', 'V', 'd', '1') ||
par->codec_tag == MKTAG('A', 'V', 'j', '2') ||
par->codec_tag == MKTAG('A', 'V', 'd', 'n')) &&
atom.size >= 24) {
int num, den;
avio_skip(pb, 12);
num = avio_rb32(pb);
den = avio_rb32(pb);
if (num <= 0 || den <= 0)
return 0;
switch (avio_rb32(pb)) {
case 2:
if (den >= INT_MAX / 2)
return 0;
den *= 2;
case 1:
c->fc->streams[c->fc->nb_streams-1]->display_aspect_ratio.num = num;
c->fc->streams[c->fc->nb_streams-1]->display_aspect_ratio.den = den;
default:
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) {
AVCodecParameters *par = c->fc->streams[c->fc->nb_streams-1]->codecpar;
if (par->codec_id == AV_CODEC_ID_H264)
return 0;
if (atom.size == 16) {
original_size = par->extradata_size;
ret = mov_realloc_extradata(par, atom);
if (!ret) {
length = mov_read_atom_into_extradata(c, pb, atom, par, par->extradata + original_size);
if (length == atom.size) {
const uint8_t range_value = par->extradata[original_size + 19];
switch (range_value) {
case 1:
par->color_range = AVCOL_RANGE_MPEG;
break;
case 2:
par->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", par->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->codecpar->codec_id == AV_CODEC_ID_QDM2 ||
st->codecpar->codec_id == AV_CODEC_ID_QDMC ||
st->codecpar->codec_id == AV_CODEC_ID_SPEEX) {
// pass all frma atom to codec, needed at least for QDMC and QDM2
av_freep(&st->codecpar->extradata);
ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size);
if (ret < 0)
return ret;
} else if (atom.size > 8) { /* to read frma, esds atoms */
if (st->codecpar->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->codecpar->extradata_size) {
#define ALAC_EXTRADATA_SIZE 36
st->codecpar->extradata = av_mallocz(ALAC_EXTRADATA_SIZE + AV_INPUT_BUFFER_PADDING_SIZE);
if (!st->codecpar->extradata)
return AVERROR(ENOMEM);
st->codecpar->extradata_size = ALAC_EXTRADATA_SIZE;
AV_WB32(st->codecpar->extradata , ALAC_EXTRADATA_SIZE);
AV_WB32(st->codecpar->extradata + 4, MKTAG('a','l','a','c'));
AV_WB64(st->codecpar->extradata + 12, buffer);
avio_read(pb, st->codecpar->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->codecpar->extradata_size > 1 && st->codecpar->extradata) {
av_log(c, AV_LOG_WARNING, "ignoring multiple glbl\n");
return 0;
}
av_freep(&st->codecpar->extradata);
ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size);
if (ret < 0)
return ret;
if (atom.type == MKTAG('h','v','c','C') && st->codecpar->codec_tag == MKTAG('d','v','h','1'))
/* HEVC-based Dolby Vision derived from hvc1.
Happens to match with an identifier
previously utilized for DV. Thus, if we have
the hvcC extradata box available as specified,
set codec to HEVC */
st->codecpar->codec_id = AV_CODEC_ID_HEVC;
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->codecpar->extradata);
ret = ff_get_extradata(c->fc, st->codecpar, 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->codecpar->extradata);
ret = ff_get_extradata(c->fc, st->codecpar, 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) {
av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STCO atom\n");
return AVERROR_EOF;
}
return 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->codecpar->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) {
st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
} else if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&
/* skip old ASF MPEG-4 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->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA ||
(st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE &&
st->codecpar->codec_id == AV_CODEC_ID_NONE)) {
id = ff_codec_get_id(ff_codec_movsubtitle_tags, format);
if (id > 0)
st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
else
id = ff_codec_get_id(ff_codec_movdata_tags, format);
}
}
st->codecpar->codec_tag = format;
return id;
}
static void mov_parse_stsd_video(MOVContext *c, AVIOContext *pb,
AVStream *st, MOVStreamContext *sc)
{
uint8_t codec_name[32] = { 0 };
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->codecpar->width = avio_rb16(pb); /* width */
st->codecpar->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 (!strncmp(codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25)) {
st->codecpar->codec_tag = MKTAG('I', '4', '2', '0');
st->codecpar->width &= ~1;
st->codecpar->height &= ~1;
}
/* Flash Media Server uses tag H.263 with Sorenson Spark */
if (st->codecpar->codec_tag == MKTAG('H','2','6','3') &&
!strncmp(codec_name, "Sorenson H263", 13))
st->codecpar->codec_id = AV_CODEC_ID_FLV1;
st->codecpar->bits_per_coded_sample = avio_rb16(pb); /* depth */
avio_seek(pb, stsd_start, SEEK_SET);
if (ff_get_qtpalette(st->codecpar->codec_id, pb, sc->palette)) {
st->codecpar->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->codecpar->channels = avio_rb16(pb); /* channel count */
st->codecpar->bits_per_coded_sample = avio_rb16(pb); /* sample size */
av_log(c->fc, AV_LOG_TRACE, "audio channels %d\n", st->codecpar->channels);
sc->audio_cid = avio_rb16(pb);
avio_rb16(pb); /* packet size = 0 */
st->codecpar->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 ")) ||
(sc->stsd_version == 0 && version > 0)) {
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->codecpar->sample_rate = av_int2double(avio_rb64(pb));
st->codecpar->channels = avio_rb32(pb);
avio_rb32(pb); /* always 0x7F000000 */
st->codecpar->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->codecpar->codec_tag == MKTAG('l','p','c','m'))
st->codecpar->codec_id =
ff_mov_get_lpcm_codec_id(st->codecpar->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->codecpar->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->codecpar->bits_per_coded_sample == 8)
st->codecpar->codec_id = mov_codec_id(st, MKTAG('r','a','w',' '));
else if (st->codecpar->bits_per_coded_sample == 16)
st->codecpar->codec_id = mov_codec_id(st, MKTAG('t','w','o','s'));
}
switch (st->codecpar->codec_id) {
case AV_CODEC_ID_PCM_S8:
case AV_CODEC_ID_PCM_U8:
if (st->codecpar->bits_per_coded_sample == 16)
st->codecpar->codec_id = AV_CODEC_ID_PCM_S16BE;
break;
case AV_CODEC_ID_PCM_S16LE:
case AV_CODEC_ID_PCM_S16BE:
if (st->codecpar->bits_per_coded_sample == 8)
st->codecpar->codec_id = AV_CODEC_ID_PCM_S8;
else if (st->codecpar->bits_per_coded_sample == 24)
st->codecpar->codec_id =
st->codecpar->codec_id == AV_CODEC_ID_PCM_S16BE ?
AV_CODEC_ID_PCM_S24BE : AV_CODEC_ID_PCM_S24LE;
else if (st->codecpar->bits_per_coded_sample == 32)
st->codecpar->codec_id =
st->codecpar->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->codecpar->channels;
break;
case AV_CODEC_ID_MACE6:
sc->samples_per_frame = 6;
sc->bytes_per_frame = 1 * st->codecpar->channels;
break;
case AV_CODEC_ID_ADPCM_IMA_QT:
sc->samples_per_frame = 64;
sc->bytes_per_frame = 34 * st->codecpar->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->codecpar->codec_id);
if (bits_per_sample) {
st->codecpar->bits_per_coded_sample = bits_per_sample;
sc->sample_size = (bits_per_sample >> 3) * st->codecpar->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->codecpar->codec_tag != AV_RL32("mp4s"))
mov_read_glbl(c, pb, fake_atom);
st->codecpar->width = sc->width;
st->codecpar->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->codecpar->extradata;
int i;
if (st->codecpar->extradata_size != 64)
return 0;
if (st->codecpar->width > 0 && st->codecpar->height > 0)
snprintf(buf, sizeof(buf), "size: %dx%d\n",
st->codecpar->width, st->codecpar->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->codecpar->extradata);
st->codecpar->extradata_size = 0;
st->codecpar->extradata = av_mallocz(strlen(buf) + AV_INPUT_BUFFER_PADDING_SIZE);
if (!st->codecpar->extradata)
return AVERROR(ENOMEM);
st->codecpar->extradata_size = strlen(buf);
memcpy(st->codecpar->extradata, buf, st->codecpar->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->codecpar->codec_tag == MKTAG('t','m','c','d')) {
if ((int)size != size)
return AVERROR(ENOMEM);
ret = ff_get_extradata(c->fc, st->codecpar, pb, size);
if (ret < 0)
return ret;
if (size > 16) {
MOVStreamContext *tmcd_ctx = st->priv_data;
int val;
val = AV_RB32(st->codecpar->extradata + 4);
tmcd_ctx->tmcd_flags = val;
st->avg_frame_rate.num = st->codecpar->extradata[16]; /* number of frame */
st->avg_frame_rate.den = 1;
#if FF_API_LAVF_AVCTX
FF_DISABLE_DEPRECATION_WARNINGS
st->codec->time_base = av_inv_q(st->avg_frame_rate);
FF_ENABLE_DEPRECATION_WARNINGS
#endif
/* adjust for per frame dur in counter mode */
if (tmcd_ctx->tmcd_flags & 0x0008) {
int timescale = AV_RB32(st->codecpar->extradata + 8);
int framedur = AV_RB32(st->codecpar->extradata + 12);
st->avg_frame_rate.num *= timescale;
st->avg_frame_rate.den *= framedur;
#if FF_API_LAVF_AVCTX
FF_DISABLE_DEPRECATION_WARNINGS
st->codec->time_base.den *= timescale;
st->codec->time_base.num *= framedur;
FF_ENABLE_DEPRECATION_WARNINGS
#endif
}
if (size > 30) {
uint32_t len = AV_RB32(st->codecpar->extradata + 18); /* name atom length */
uint32_t format = AV_RB32(st->codecpar->extradata + 22);
if (format == AV_RB32("name") && (int64_t)size >= (int64_t)len + 18) {
uint16_t str_size = AV_RB16(st->codecpar->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->codecpar->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->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
!st->codecpar->sample_rate && sc->time_scale > 1)
st->codecpar->sample_rate = sc->time_scale;
/* special codec parameters handling */
switch (st->codecpar->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->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
break;
#endif
/* no ifdef since parameters are always those */
case AV_CODEC_ID_QCELP:
st->codecpar->channels = 1;
// force sample rate for qcelp when not stored in mov
if (st->codecpar->codec_tag != MKTAG('Q','c','l','p'))
st->codecpar->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->codecpar->channels = 1;
/* force sample rate for amr, stsd in 3gp does not store sample rate */
st->codecpar->sample_rate = 8000;
break;
case AV_CODEC_ID_AMR_WB:
st->codecpar->channels = 1;
st->codecpar->sample_rate = 16000;
break;
case AV_CODEC_ID_MP2:
case AV_CODEC_ID_MP3:
/* force type after stsd for m1a hdlr */
st->codecpar->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->codecpar->block_align = sc->bytes_per_frame;
break;
case AV_CODEC_ID_ALAC:
if (st->codecpar->extradata_size == 36) {
st->codecpar->channels = AV_RB8 (st->codecpar->extradata + 21);
st->codecpar->sample_rate = AV_RB32(st->codecpar->extradata + 32);
}
break;
case AV_CODEC_ID_AC3:
case AV_CODEC_ID_EAC3:
case AV_CODEC_ID_MPEG1VIDEO:
case AV_CODEC_ID_VC1:
case AV_CODEC_ID_VP8:
case AV_CODEC_ID_VP9:
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 &&
// AVID 1:1 samples with differing data format and codec tag exist
(codec_tag != AV_RL32("AV1x") || format != AV_RL32("AVup")) &&
// prores is allowed to have differing data format and codec tag
codec_tag != AV_RL32("apcn") && codec_tag != AV_RL32("apch") &&
// so is dv (sigh)
codec_tag != AV_RL32("dvpp") && codec_tag != AV_RL32("dvcp") &&
(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;
}
return 0;
}
int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
{
AVStream *st;
MOVStreamContext *sc;
int pseudo_stream_id;
av_assert0 (c->fc->nb_streams >= 1);
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->codecpar->codec_tag, format,
size - (avio_tell(pb) - start_pos))) {
sc->stsd_count++;
continue;
}
sc->pseudo_stream_id = st->codecpar->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=%s codec_type=%d\n", size,
av_fourcc2str(format), st->codecpar->codec_type);
st->codecpar->codec_id = id;
if (st->codecpar->codec_type==AVMEDIA_TYPE_VIDEO) {
mov_parse_stsd_video(c, pb, st, sc);
} else if (st->codecpar->codec_type==AVMEDIA_TYPE_AUDIO) {
mov_parse_stsd_audio(c, pb, st, sc);
if (st->codecpar->sample_rate < 0) {
av_log(c->fc, AV_LOG_ERROR, "Invalid sample rate %d\n", st->codecpar->sample_rate);
return AVERROR_INVALIDDATA;
}
} else if (st->codecpar->codec_type==AVMEDIA_TYPE_SUBTITLE){
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 (sc->extradata && st->codecpar->extradata) {
int extra_size = st->codecpar->extradata_size;
/* Move the current stream extradata to the stream context one. */
sc->extradata_size[pseudo_stream_id] = extra_size;
sc->extradata[pseudo_stream_id] = av_malloc(extra_size + AV_INPUT_BUFFER_PADDING_SIZE);
if (!sc->extradata[pseudo_stream_id])
return AVERROR(ENOMEM);
memcpy(sc->extradata[pseudo_stream_id], st->codecpar->extradata, extra_size);
av_freep(&st->codecpar->extradata);
st->codecpar->extradata_size = 0;
}
sc->stsd_count++;
}
if (pb->eof_reached) {
av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STSD atom\n");
return AVERROR_EOF;
}
return 0;
}
static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
AVStream *st;
MOVStreamContext *sc;
int ret, entries;
if (c->fc->nb_streams < 1)
return 0;
st = c->fc->streams[c->fc->nb_streams - 1];
sc = st->priv_data;
sc->stsd_version = avio_r8(pb);
avio_rb24(pb); /* flags */
entries = avio_rb32(pb);
/* Each entry contains a size (4 bytes) and format (4 bytes). */
if (entries <= 0 || entries > atom.size / 8) {
av_log(c->fc, AV_LOG_ERROR, "invalid STSD entries %d\n", entries);
return AVERROR_INVALIDDATA;
}
if (sc->extradata) {
av_log(c->fc, AV_LOG_ERROR,
"Duplicate stsd found in this track.\n");
return AVERROR_INVALIDDATA;
}
/* Prepare space for hosting multiple extradata. */
sc->extradata = av_mallocz_array(entries, sizeof(*sc->extradata));
if (!sc->extradata)
return AVERROR(ENOMEM);
sc->extradata_size = av_mallocz_array(entries, sizeof(*sc->extradata_size));
if (!sc->extradata_size) {
ret = AVERROR(ENOMEM);
goto fail;
}
ret = ff_mov_read_stsd_entries(c, pb, entries);
if (ret < 0)
goto fail;
/* Restore back the primary extradata. */
av_freep(&st->codecpar->extradata);
st->codecpar->extradata_size = sc->extradata_size[0];
if (sc->extradata_size[0]) {
st->codecpar->extradata = av_mallocz(sc->extradata_size[0] + AV_INPUT_BUFFER_PADDING_SIZE);
if (!st->codecpar->extradata)
return AVERROR(ENOMEM);
memcpy(st->codecpar->extradata, sc->extradata[0], sc->extradata_size[0]);
}
return mov_finalize_stsd_codec(c, pb, st, sc);
fail:
if (sc->extradata) {
int j;
for (j = 0; j < sc->stsd_count; j++)
av_freep(&sc->extradata[j]);
}
av_freep(&sc->extradata);
av_freep(&sc->extradata_size);
return ret;
}
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);
if ((uint64_t)entries * 12 + 4 > atom.size)
return AVERROR_INVALIDDATA;
av_log(c->fc, AV_LOG_TRACE, "track[%u].stsc.entries = %u\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;
for (i = sc->stsc_count - 1; i < UINT_MAX; i--) {
int64_t first_min = i + 1;
if ((i+1 < sc->stsc_count && sc->stsc_data[i].first >= sc->stsc_data[i+1].first) ||
(i > 0 && sc->stsc_data[i].first <= sc->stsc_data[i-1].first) ||
sc->stsc_data[i].first < first_min ||
sc->stsc_data[i].count < 1 ||
sc->stsc_data[i].id < 1) {
av_log(c->fc, AV_LOG_WARNING, "STSC entry %d is invalid (first=%d count=%d id=%d)\n", i, sc->stsc_data[i].first, sc->stsc_data[i].count, sc->stsc_data[i].id);
if (i+1 >= sc->stsc_count) {
sc->stsc_data[i].first = FFMAX(sc->stsc_data[i].first, first_min);
if (i > 0 && sc->stsc_data[i].first <= sc->stsc_data[i-1].first)
sc->stsc_data[i].first = FFMIN(sc->stsc_data[i-1].first + 1LL, INT_MAX);
sc->stsc_data[i].count = FFMAX(sc->stsc_data[i].count, 1);
sc->stsc_data[i].id = FFMAX(sc->stsc_data[i].id, 1);
continue;
}
av_assert0(sc->stsc_data[i+1].first >= 2);
// We replace this entry by the next valid
sc->stsc_data[i].first = sc->stsc_data[i+1].first - 1;
sc->stsc_data[i].count = sc->stsc_data[i+1].count;
sc->stsc_data[i].id = sc->stsc_data[i+1].id;
}
}
if (pb->eof_reached) {
av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STSC atom\n");
return AVERROR_EOF;
}
return 0;
}
static inline int mov_stsc_index_valid(unsigned int index, unsigned int count)
{
return index < count - 1;
}
/* Compute the samples value for the stsc entry at the given index. */
static inline int64_t mov_get_stsc_samples(MOVStreamContext *sc, unsigned int index)
{
int chunk_count;
if (mov_stsc_index_valid(index, sc->stsc_count))
chunk_count = sc->stsc_data[index + 1].first - sc->stsc_data[index].first;
else {
// Validation for stsc / stco happens earlier in mov_read_stsc + mov_read_trak.
av_assert0(sc->stsc_data[index].first <= sc->chunk_count);
chunk_count = sc->chunk_count - (sc->stsc_data[index].first - 1);
}
return sc->stsc_data[index].count * (int64_t)chunk_count;
}
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);
}
sc->stps_count = i;
if (pb->eof_reached) {
av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STPS atom\n");
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 = %u\n", entries);
if (!entries)
{
sc->keyframe_absent = 1;
if (!st->need_parsing && st->codecpar->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);
}
sc->keyframe_count = i;
if (pb->eof_reached) {
av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STSS atom\n");
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 = %u sample_count = %u\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 %u\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);
av_log(c->fc, AV_LOG_WARNING, "STSZ atom truncated\n");
return 0;
}
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) {
av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STSZ atom\n");
return AVERROR_EOF;
}
return 0;
}
static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
AVStream *st;
MOVStreamContext *sc;
unsigned int i, entries, alloc_size = 0;
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[%u].stts.entries = %u\n",
c->fc->nb_streams-1, entries);
if (sc->stts_data)
av_log(c->fc, AV_LOG_WARNING, "Duplicated STTS atom\n");
av_freep(&sc->stts_data);
sc->stts_count = 0;
if (entries >= INT_MAX / sizeof(*sc->stts_data))
return AVERROR(ENOMEM);
for (i = 0; i < entries && !pb->eof_reached; i++) {
int sample_duration;
unsigned int sample_count;
unsigned int min_entries = FFMIN(FFMAX(i + 1, 1024 * 1024), entries);
MOVStts *stts_data = av_fast_realloc(sc->stts_data, &alloc_size,
min_entries * sizeof(*sc->stts_data));
if (!stts_data) {
av_freep(&sc->stts_data);
sc->stts_count = 0;
return AVERROR(ENOMEM);
}
sc->stts_count = min_entries;
sc->stts_data = stts_data;
sample_count=avio_rb32(pb);
sample_duration = avio_rb32(pb);
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);
duration+=(int64_t)sample_duration*(uint64_t)sample_count;
total_sample_count+=sample_count;
}
sc->stts_count = i;
if (duration > 0 &&
duration <= INT64_MAX - sc->duration_for_fps &&
total_sample_count <= INT_MAX - sc->nb_frames_for_fps
) {
sc->duration_for_fps += duration;
sc->nb_frames_for_fps += total_sample_count;
}
if (pb->eof_reached) {
av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted STTS atom\n");
return AVERROR_EOF;
}
st->nb_frames= total_sample_count;
if (duration)
st->duration= FFMIN(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, ctts_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[%u].ctts.entries = %u\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_fast_realloc(NULL, &sc->ctts_allocated_size, entries * sizeof(*sc->ctts_data));
if (!sc->ctts_data)
return AVERROR(ENOMEM);
for (i = 0; i < entries && !pb->eof_reached; i++) {
int count =avio_rb32(pb);
int duration =avio_rb32(pb);
if (count <= 0) {
av_log(c->fc, AV_LOG_TRACE,
"ignoring CTTS entry with count=%d duration=%d\n",
count, duration);
continue;
}
add_ctts_entry(&sc->ctts_data, &ctts_count, &sc->ctts_allocated_size,
count, duration);
av_log(c->fc, AV_LOG_TRACE, "count=%d, duration=%d\n",
count, duration);
if (FFNABS(duration) < -(1<<28) && i+2<entries) {
av_log(c->fc, AV_LOG_WARNING, "CTTS invalid\n");
av_freep(&sc->ctts_data);
sc->ctts_count = 0;
return 0;
}
if (i+2<entries)
mov_update_dts_shift(sc, duration);
}
sc->ctts_count = ctts_count;
if (pb->eof_reached) {
av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted CTTS atom\n");
return AVERROR_EOF;
}
av_log(c->fc, AV_LOG_TRACE, "dts shift %d\n", sc->dts_shift);
return 0;
}
static int mov_read_sbgp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
AVStream *st;
MOVStreamContext *sc;
unsigned int i, entries;
uint8_t version;
uint32_t grouping_type;
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); /* version */
avio_rb24(pb); /* flags */
grouping_type = avio_rl32(pb);
if (grouping_type != MKTAG( 'r','a','p',' '))
return 0; /* only support 'rap ' grouping */
if (version == 1)
avio_rb32(pb); /* grouping_type_parameter */
entries = avio_rb32(pb);
if (!entries)
return 0;
if (sc->rap_group)
av_log(c->fc, AV_LOG_WARNING, "Duplicated SBGP atom\n");
av_free(sc->rap_group);
sc->rap_group_count = 0;
sc->rap_group = av_malloc_array(entries, sizeof(*sc->rap_group));
if (!sc->rap_group)
return AVERROR(ENOMEM);
for (i = 0; i < entries && !pb->eof_reached; i++) {
sc->rap_group[i].count = avio_rb32(pb); /* sample_count */
sc->rap_group[i].index = avio_rb32(pb); /* group_description_index */
}
sc->rap_group_count = i;
if (pb->eof_reached) {
av_log(c->fc, AV_LOG_WARNING, "reached eof, corrupted SBGP atom\n");
return AVERROR_EOF;
}
return 0;
}
/**
* Get ith edit list entry (media time, duration).
*/
static int get_edit_list_entry(MOVContext *mov,
const MOVStreamContext *msc,
unsigned int edit_list_index,
int64_t *edit_list_media_time,
int64_t *edit_list_duration,
int64_t global_timescale)
{
if (edit_list_index == msc->elst_count) {
return 0;
}
*edit_list_media_time = msc->elst_data[edit_list_index].time;
*edit_list_duration = msc->elst_data[edit_list_index].duration;
/* duration is in global timescale units;convert to msc timescale */
if (global_timescale == 0) {
avpriv_request_sample(mov->fc, "Support for mvhd.timescale = 0 with editlists");
return 0;
}
*edit_list_duration = av_rescale(*edit_list_duration, msc->time_scale,
global_timescale);
return 1;
}
/**
* Find the closest previous frame to the timestamp_pts, in e_old index
* entries. Searching for just any frame / just key frames can be controlled by
* last argument 'flag'.
* Note that if ctts_data is not NULL, we will always search for a key frame
* irrespective of the value of 'flag'. If we don't find any keyframe, we will
* return the first frame of the video.
*
* Here the timestamp_pts is considered to be a presentation timestamp and
* the timestamp of index entries are considered to be decoding timestamps.
*
* Returns 0 if successful in finding a frame, else returns -1.
* Places the found index corresponding output arg.
*
* If ctts_old is not NULL, then refines the searched entry by searching
* backwards from the found timestamp, to find the frame with correct PTS.
*
* Places the found ctts_index and ctts_sample in corresponding output args.
*/
static int find_prev_closest_index(AVStream *st,
AVIndexEntry *e_old,
int nb_old,
MOVStts* ctts_data,
int64_t ctts_count,
int64_t timestamp_pts,
int flag,
int64_t* index,
int64_t* ctts_index,
int64_t* ctts_sample)
{
MOVStreamContext *msc = st->priv_data;
AVIndexEntry *e_keep = st->index_entries;
int nb_keep = st->nb_index_entries;
int64_t i = 0;
int64_t index_ctts_count;
av_assert0(index);
// If dts_shift > 0, then all the index timestamps will have to be offset by
// at least dts_shift amount to obtain PTS.
// Hence we decrement the searched timestamp_pts by dts_shift to find the closest index element.
if (msc->dts_shift > 0) {
timestamp_pts -= msc->dts_shift;
}
st->index_entries = e_old;
st->nb_index_entries = nb_old;
*index = av_index_search_timestamp(st, timestamp_pts, flag | AVSEEK_FLAG_BACKWARD);
// Keep going backwards in the index entries until the timestamp is the same.
if (*index >= 0) {
for (i = *index; i > 0 && e_old[i].timestamp == e_old[i - 1].timestamp;
i--) {
if ((flag & AVSEEK_FLAG_ANY) ||
(e_old[i - 1].flags & AVINDEX_KEYFRAME)) {
*index = i - 1;
}
}
}
// If we have CTTS then refine the search, by searching backwards over PTS
// computed by adding corresponding CTTS durations to index timestamps.
if (ctts_data && *index >= 0) {
av_assert0(ctts_index);
av_assert0(ctts_sample);
// Find out the ctts_index for the found frame.
*ctts_index = 0;
*ctts_sample = 0;
for (index_ctts_count = 0; index_ctts_count < *index; index_ctts_count++) {
if (*ctts_index < ctts_count) {
(*ctts_sample)++;
if (ctts_data[*ctts_index].count == *ctts_sample) {
(*ctts_index)++;
*ctts_sample = 0;
}
}
}
while (*index >= 0 && (*ctts_index) >= 0 && (*ctts_index) < ctts_count) {
// Find a "key frame" with PTS <= timestamp_pts (So that we can decode B-frames correctly).
// No need to add dts_shift to the timestamp here becase timestamp_pts has already been
// compensated by dts_shift above.
if ((e_old[*index].timestamp + ctts_data[*ctts_index].duration) <= timestamp_pts &&
(e_old[*index].flags & AVINDEX_KEYFRAME)) {
break;
}
(*index)--;
if (*ctts_sample == 0) {
(*ctts_index)--;
if (*ctts_index >= 0)
*ctts_sample = ctts_data[*ctts_index].count - 1;
} else {
(*ctts_sample)--;
}
}
}
/* restore AVStream state*/
st->index_entries = e_keep;
st->nb_index_entries = nb_keep;
return *index >= 0 ? 0 : -1;
}
/**
* Add index entry with the given values, to the end of st->index_entries.
* Returns the new size st->index_entries if successful, else returns -1.
*
* This function is similar to ff_add_index_entry in libavformat/utils.c
* except that here we are always unconditionally adding an index entry to
* the end, instead of searching the entries list and skipping the add if
* there is an existing entry with the same timestamp.
* This is needed because the mov_fix_index calls this func with the same
* unincremented timestamp for successive discarded frames.
*/
static int64_t add_index_entry(AVStream *st, int64_t pos, int64_t timestamp,
int size, int distance, int flags)
{
AVIndexEntry *entries, *ie;
int64_t index = -1;
const size_t min_size_needed = (st->nb_index_entries + 1) * sizeof(AVIndexEntry);
// Double the allocation each time, to lower memory fragmentation.
// Another difference from ff_add_index_entry function.
const size_t requested_size =
min_size_needed > st->index_entries_allocated_size ?
FFMAX(min_size_needed, 2 * st->index_entries_allocated_size) :
min_size_needed;
if((unsigned)st->nb_index_entries + 1 >= UINT_MAX / sizeof(AVIndexEntry))
return -1;
entries = av_fast_realloc(st->index_entries,
&st->index_entries_allocated_size,
requested_size);
if(!entries)
return -1;
st->index_entries= entries;
index= st->nb_index_entries++;
ie= &entries[index];
ie->pos = pos;
ie->timestamp = timestamp;
ie->min_distance= distance;
ie->size= size;
ie->flags = flags;
return index;
}
/**
* Rewrite timestamps of index entries in the range [end_index - frame_duration_buffer_size, end_index)
* by subtracting end_ts successively by the amounts given in frame_duration_buffer.
*/
static void fix_index_entry_timestamps(AVStream* st, int end_index, int64_t end_ts,
int64_t* frame_duration_buffer,
int frame_duration_buffer_size) {
int i = 0;
av_assert0(end_index >= 0 && end_index <= st->nb_index_entries);
for (i = 0; i < frame_duration_buffer_size; i++) {
end_ts -= frame_duration_buffer[frame_duration_buffer_size - 1 - i];
st->index_entries[end_index - 1 - i].timestamp = end_ts;
}
}
/**
* Append a new ctts entry to ctts_data.
* Returns the new ctts_count if successful, else returns -1.
*/
static int64_t add_ctts_entry(MOVStts** ctts_data, unsigned int* ctts_count, unsigned int* allocated_size,
int count, int duration)
{
MOVStts *ctts_buf_new;
const size_t min_size_needed = (*ctts_count + 1) * sizeof(MOVStts);
const size_t requested_size =
min_size_needed > *allocated_size ?
FFMAX(min_size_needed, 2 * (*allocated_size)) :
min_size_needed;
if((unsigned)(*ctts_count) >= UINT_MAX / sizeof(MOVStts) - 1)
return -1;
ctts_buf_new = av_fast_realloc(*ctts_data, allocated_size, requested_size);
if(!ctts_buf_new)
return -1;
*ctts_data = ctts_buf_new;
ctts_buf_new[*ctts_count].count = count;
ctts_buf_new[*ctts_count].duration = duration;
*ctts_count = (*ctts_count) + 1;
return *ctts_count;
}
#define MAX_REORDER_DELAY 16
static void mov_estimate_video_delay(MOVContext *c, AVStream* st) {
MOVStreamContext *msc = st->priv_data;
int ind;
int ctts_ind = 0;
int ctts_sample = 0;
int64_t pts_buf[MAX_REORDER_DELAY + 1]; // Circular buffer to sort pts.
int buf_start = 0;
int j, r, num_swaps;
for (j = 0; j < MAX_REORDER_DELAY + 1; j++)
pts_buf[j] = INT64_MIN;
if (st->codecpar->video_delay <= 0 && msc->ctts_data &&
st->codecpar->codec_id == AV_CODEC_ID_H264) {
st->codecpar->video_delay = 0;
for(ind = 0; ind < st->nb_index_entries && ctts_ind < msc->ctts_count; ++ind) {
// Point j to the last elem of the buffer and insert the current pts there.
j = buf_start;
buf_start = (buf_start + 1);
if (buf_start == MAX_REORDER_DELAY + 1)
buf_start = 0;
pts_buf[j] = st->index_entries[ind].timestamp + msc->ctts_data[ctts_ind].duration;
// The timestamps that are already in the sorted buffer, and are greater than the
// current pts, are exactly the timestamps that need to be buffered to output PTS
// in correct sorted order.
// Hence the video delay (which is the buffer size used to sort DTS and output PTS),
// can be computed as the maximum no. of swaps any particular timestamp needs to
// go through, to keep this buffer in sorted order.
num_swaps = 0;
while (j != buf_start) {
r = j - 1;
if (r < 0) r = MAX_REORDER_DELAY;
if (pts_buf[j] < pts_buf[r]) {
FFSWAP(int64_t, pts_buf[j], pts_buf[r]);
++num_swaps;
} else {
break;
}
j = r;
}
st->codecpar->video_delay = FFMAX(st->codecpar->video_delay, num_swaps);
ctts_sample++;
if (ctts_sample == msc->ctts_data[ctts_ind].count) {
ctts_ind++;
ctts_sample = 0;
}
}
av_log(c->fc, AV_LOG_DEBUG, "Setting codecpar->delay to %d for stream st: %d\n",
st->codecpar->video_delay, st->index);
}
}
static void mov_current_sample_inc(MOVStreamContext *sc)
{
sc->current_sample++;
sc->current_index++;
if (sc->index_ranges &&
sc->current_index >= sc->current_index_range->end &&
sc->current_index_range->end) {
sc->current_index_range++;
sc->current_index = sc->current_index_range->start;
}
}
static void mov_current_sample_dec(MOVStreamContext *sc)
{
sc->current_sample--;
sc->current_index--;
if (sc->index_ranges &&
sc->current_index < sc->current_index_range->start &&
sc->current_index_range > sc->index_ranges) {
sc->current_index_range--;
sc->current_index = sc->current_index_range->end - 1;
}
}
static void mov_current_sample_set(MOVStreamContext *sc, int current_sample)
{
int64_t range_size;
sc->current_sample = current_sample;
sc->current_index = current_sample;
if (!sc->index_ranges) {
return;
}
for (sc->current_index_range = sc->index_ranges;
sc->current_index_range->end;
sc->current_index_range++) {
range_size = sc->current_index_range->end - sc->current_index_range->start;
if (range_size > current_sample) {
sc->current_index = sc->current_index_range->start + current_sample;
break;
}
current_sample -= range_size;
}
}
/**
* Fix st->index_entries, so that it contains only the entries (and the entries
* which are needed to decode them) that fall in the edit list time ranges.
* Also fixes the timestamps of the index entries to match the timeline
* specified the edit lists.
*/
static void mov_fix_index(MOVContext *mov, AVStream *st)
{
MOVStreamContext *msc = st->priv_data;
AVIndexEntry *e_old = st->index_entries;
int nb_old = st->nb_index_entries;
const AVIndexEntry *e_old_end = e_old + nb_old;
const AVIndexEntry *current = NULL;
MOVStts *ctts_data_old = msc->ctts_data;
int64_t ctts_index_old = 0;
int64_t ctts_sample_old = 0;
int64_t ctts_count_old = msc->ctts_count;
int64_t edit_list_media_time = 0;
int64_t edit_list_duration = 0;
int64_t frame_duration = 0;
int64_t edit_list_dts_counter = 0;
int64_t edit_list_dts_entry_end = 0;
int64_t edit_list_start_ctts_sample = 0;
int64_t curr_cts;
int64_t curr_ctts = 0;
int64_t empty_edits_sum_duration = 0;
int64_t edit_list_index = 0;
int64_t index;
int flags;
int64_t start_dts = 0;
int64_t edit_list_start_encountered = 0;
int64_t search_timestamp = 0;
int64_t* frame_duration_buffer = NULL;
int num_discarded_begin = 0;
int first_non_zero_audio_edit = -1;
int packet_skip_samples = 0;
MOVIndexRange *current_index_range;
int i;
int found_keyframe_after_edit = 0;
int found_non_empty_edit = 0;
if (!msc->elst_data || msc->elst_count <= 0 || nb_old <= 0) {
return;
}
// allocate the index ranges array
msc->index_ranges = av_malloc((msc->elst_count + 1) * sizeof(msc->index_ranges[0]));
if (!msc->index_ranges) {
av_log(mov->fc, AV_LOG_ERROR, "Cannot allocate index ranges buffer\n");
return;
}
msc->current_index_range = msc->index_ranges;
current_index_range = msc->index_ranges - 1;
// Clean AVStream from traces of old index
st->index_entries = NULL;
st->index_entries_allocated_size = 0;
st->nb_index_entries = 0;
// Clean ctts fields of MOVStreamContext