title_str[title_size] = 0;
if (title_str[0]) {
int off = (!c->isom && title_str[0] == title_size - 1);
- av_dict_set(&st->metadata, "handler_name", title_str + off, 0);
+ // 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);
}
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'))
+ st->codecpar->codec_id = AV_CODEC_ID_HEVC;
return 0;
}
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);
}
}
// 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 "))) {
-
+ (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 */
"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) {
- st->codecpar->codec_id = id;
mov_parse_stsd_video(c, pb, st, sc);
} else if (st->codecpar->codec_type==AVMEDIA_TYPE_AUDIO) {
- st->codecpar->codec_id = id;
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){
- st->codecpar->codec_id = id;
mov_parse_stsd_subtitle(c, pb, st, sc,
size - (avio_tell(pb) - start_pos));
} else {
st = c->fc->streams[c->fc->nb_streams - 1];
sc = st->priv_data;
- avio_r8(pb); /* version */
+ sc->stsd_version = avio_r8(pb);
avio_rb24(pb); /* flags */
entries = avio_rb32(pb);
- if (entries <= 0) {
+ /* 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 (ret < 0) {
av_freep(&sc->sample_sizes);
av_free(buf);
- return ret;
+ av_log(c->fc, AV_LOG_WARNING, "STSZ atom truncated\n");
+ return 0;
}
init_get_bits(&gb, buf, 8*num_bytes);
int ctts_sample = 0;
int64_t pts_buf[MAX_REORDER_DELAY + 1]; // Circular buffer to sort pts.
int buf_start = 0;
- int buf_size = 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) {
- if (buf_size == (MAX_REORDER_DELAY + 1)) {
- // If circular buffer is full, then move the first element forward.
- buf_start = (buf_start + 1) % buf_size;
- } else {
- ++buf_size;
- }
-
// Point j to the last elem of the buffer and insert the current pts there.
- j = (buf_start + buf_size - 1) % buf_size;
+ 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
// go through, to keep this buffer in sorted order.
num_swaps = 0;
while (j != buf_start) {
- r = (j - 1 + buf_size) % buf_size;
+ 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->index_entries[i].timestamp -= msc->min_corrected_pts;
}
}
- // Start time should be equal to zero or the duration of any empty edits.
- st->start_time = empty_edits_sum_duration;
}
+ // Start time should be equal to zero or the duration of any empty edits.
+ st->start_time = empty_edits_sum_duration;
// Update av stream length, if it ends up shorter than the track's media duration
st->duration = FFMIN(st->duration, edit_list_dts_entry_end - start_dts);
return 0;
}
+static int mov_read_av1c(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 (atom.size < 4) {
+ av_log(c->fc, AV_LOG_ERROR, "Empty AV1 Codec Configuration Box\n");
+ return AVERROR_INVALIDDATA;
+ }
+
+ /* For now, propagate only the OBUs, if any. Once libavcodec is
+ updated to handle isobmff style extradata this can be removed. */
+ avio_skip(pb, 4);
+
+ if (atom.size == 4)
+ return 0;
+
+ ret = ff_get_extradata(c->fc, st->codecpar, pb, atom.size - 4);
+ if (ret < 0)
+ return ret;
+
+ return 0;
+}
+
static int mov_read_vpcc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
AVStream *st;
*sc = st->priv_data;
if (!frag_stream_info->encryption_index) {
+ // If this stream isn't encrypted, don't create the index.
+ if (!(*sc)->cenc.default_encrypted_sample)
+ return 0;
frag_stream_info->encryption_index = av_mallocz(sizeof(*frag_stream_info->encryption_index));
if (!frag_stream_info->encryption_index)
return AVERROR(ENOMEM);
*sc = st->priv_data;
if (!(*sc)->cenc.encryption_index) {
+ // If this stream isn't encrypted, don't create the index.
+ if (!(*sc)->cenc.default_encrypted_sample)
+ return 0;
(*sc)->cenc.encryption_index = av_mallocz(sizeof(*frag_stream_info->encryption_index));
if (!(*sc)->cenc.encryption_index)
return AVERROR(ENOMEM);
unsigned int subsample_count;
AVSubsampleEncryptionInfo *subsamples;
+ if (!sc->cenc.default_encrypted_sample) {
+ av_log(c->fc, AV_LOG_ERROR, "Missing schm or tenc\n");
+ return AVERROR_INVALIDDATA;
+ }
+
*sample = av_encryption_info_clone(sc->cenc.default_encrypted_sample);
if (!*sample)
return AVERROR(ENOMEM);
return 0;
}
+static int mov_read_pssh(MOVContext *c, AVIOContext *pb, MOVAtom atom)
+{
+ AVEncryptionInitInfo *info, *old_init_info;
+ uint8_t **key_ids;
+ AVStream *st;
+ uint8_t *side_data, *extra_data, *old_side_data;
+ size_t side_data_size;
+ int ret = 0, old_side_data_size;
+ unsigned int version, kid_count, extra_data_size, alloc_size = 0;
+
+ if (c->fc->nb_streams < 1)
+ return 0;
+ st = c->fc->streams[c->fc->nb_streams-1];
+
+ version = avio_r8(pb); /* version */
+ avio_rb24(pb); /* flags */
+
+ info = av_encryption_init_info_alloc(/* system_id_size */ 16, /* num_key_ids */ 0,
+ /* key_id_size */ 16, /* data_size */ 0);
+ if (!info)
+ return AVERROR(ENOMEM);
+
+ if (avio_read(pb, info->system_id, 16) != 16) {
+ av_log(c->fc, AV_LOG_ERROR, "Failed to read the system id\n");
+ ret = AVERROR_INVALIDDATA;
+ goto finish;
+ }
+
+ if (version > 0) {
+ kid_count = avio_rb32(pb);
+ if (kid_count >= INT_MAX / sizeof(*key_ids))
+ return AVERROR(ENOMEM);
+
+ for (unsigned int i = 0; i < kid_count && !pb->eof_reached; i++) {
+ unsigned int min_kid_count = FFMIN(FFMAX(i + 1, 1024), kid_count);
+ key_ids = av_fast_realloc(info->key_ids, &alloc_size,
+ min_kid_count * sizeof(*key_ids));
+ if (!key_ids) {
+ ret = AVERROR(ENOMEM);
+ goto finish;
+ }
+ info->key_ids = key_ids;
+
+ info->key_ids[i] = av_mallocz(16);
+ if (!info->key_ids[i]) {
+ ret = AVERROR(ENOMEM);
+ goto finish;
+ }
+ info->num_key_ids = i + 1;
+
+ if (avio_read(pb, info->key_ids[i], 16) != 16) {
+ av_log(c->fc, AV_LOG_ERROR, "Failed to read the key id\n");
+ ret = AVERROR_INVALIDDATA;
+ goto finish;
+ }
+ }
+
+ if (pb->eof_reached) {
+ av_log(c->fc, AV_LOG_ERROR, "Hit EOF while reading pssh\n");
+ ret = AVERROR_INVALIDDATA;
+ goto finish;
+ }
+ }
+
+ extra_data_size = avio_rb32(pb);
+ ret = mov_try_read_block(pb, extra_data_size, &extra_data);
+ if (ret < 0)
+ goto finish;
+
+ av_freep(&info->data); // malloc(0) may still allocate something.
+ info->data = extra_data;
+ info->data_size = extra_data_size;
+
+ // If there is existing initialization data, append to the list.
+ old_side_data = av_stream_get_side_data(st, AV_PKT_DATA_ENCRYPTION_INIT_INFO, &old_side_data_size);
+ if (old_side_data) {
+ old_init_info = av_encryption_init_info_get_side_data(old_side_data, old_side_data_size);
+ if (old_init_info) {
+ // Append to the end of the list.
+ for (AVEncryptionInitInfo *cur = old_init_info;; cur = cur->next) {
+ if (!cur->next) {
+ cur->next = info;
+ break;
+ }
+ }
+ info = old_init_info;
+ } else {
+ // Assume existing side-data will be valid, so the only error we could get is OOM.
+ ret = AVERROR(ENOMEM);
+ goto finish;
+ }
+ }
+
+ side_data = av_encryption_init_info_add_side_data(info, &side_data_size);
+ if (!side_data) {
+ ret = AVERROR(ENOMEM);
+ goto finish;
+ }
+ ret = av_stream_add_side_data(st, AV_PKT_DATA_ENCRYPTION_INIT_INFO,
+ side_data, side_data_size);
+ if (ret < 0)
+ av_free(side_data);
+
+finish:
+ av_encryption_init_info_free(info);
+ return ret;
+}
+
static int mov_read_schm(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
AVStream *st;
return AVERROR(ENOMEM);
}
sc->cenc.per_sample_iv_size = avio_r8(pb);
+ if (sc->cenc.per_sample_iv_size != 0 && sc->cenc.per_sample_iv_size != 8 &&
+ sc->cenc.per_sample_iv_size != 16) {
+ av_log(c->fc, AV_LOG_ERROR, "invalid per-sample IV size value\n");
+ return AVERROR_INVALIDDATA;
+ }
if (avio_read(pb, sc->cenc.default_encrypted_sample->key_id, 16) != 16) {
- av_log(c->fc, AV_LOG_ERROR, "failed to read the default key ID");
+ av_log(c->fc, AV_LOG_ERROR, "failed to read the default key ID\n");
return AVERROR_INVALIDDATA;
}
}
if (avio_read(pb, sc->cenc.default_encrypted_sample->iv, iv_size) != iv_size) {
- av_log(c->fc, AV_LOG_ERROR, "failed to read the default IV");
+ av_log(c->fc, AV_LOG_ERROR, "failed to read the default IV\n");
return AVERROR_INVALIDDATA;
}
}
MOVFragmentStreamInfo *frag_stream_info;
MOVEncryptionIndex *encryption_index;
AVEncryptionInfo *encrypted_sample;
- int encrypted_index;
+ int encrypted_index, ret;
frag_stream_info = get_current_frag_stream_info(&mov->frag_index);
encrypted_index = current_index;
if (mov->decryption_key) {
return cenc_decrypt(mov, sc, encrypted_sample, pkt->data, pkt->size);
+ } else {
+ size_t size;
+ uint8_t *side_data = av_encryption_info_add_side_data(encrypted_sample, &size);
+ if (!side_data)
+ return AVERROR(ENOMEM);
+ ret = av_packet_add_side_data(pkt, AV_PKT_DATA_ENCRYPTION_INFO, side_data, size);
+ if (ret < 0)
+ av_free(side_data);
+ return ret;
}
}
const int OPUS_SEEK_PREROLL_MS = 80;
AVStream *st;
size_t size;
- int16_t pre_skip;
+ uint16_t pre_skip;
if (c->fc->nb_streams < 1)
return 0;
{ MKTAG('A','A','L','P'), mov_read_avid },
{ MKTAG('A','R','E','S'), mov_read_ares },
{ MKTAG('a','v','s','s'), mov_read_avss },
+{ MKTAG('a','v','1','C'), mov_read_av1c },
{ MKTAG('c','h','p','l'), mov_read_chpl },
{ MKTAG('c','o','6','4'), mov_read_stco },
{ MKTAG('c','o','l','r'), mov_read_colr },
{ MKTAG('s','e','n','c'), mov_read_senc },
{ MKTAG('s','a','i','z'), mov_read_saiz },
{ MKTAG('s','a','i','o'), mov_read_saio },
+{ MKTAG('p','s','s','h'), mov_read_pssh },
{ MKTAG('s','c','h','m'), mov_read_schm },
{ MKTAG('s','c','h','i'), mov_read_default },
{ MKTAG('t','e','n','c'), mov_read_tenc },
} else {
int64_t next_dts = (sc->current_sample < st->nb_index_entries) ?
st->index_entries[sc->current_sample].timestamp : st->duration;
- pkt->duration = next_dts - pkt->dts;
+
+ if (next_dts >= pkt->dts)
+ pkt->duration = next_dts - pkt->dts;
pkt->pts = pkt->dts;
}
if (st->discard == AVDISCARD_ALL)