@@ -715,6 +715,11 @@ specify.
16-byte key, in hex, to decrypt files encrypted using ISO Common Encryption (CENC/AES-128 CTR; ISO/IEC 23001-7).
@end table
+@item max_stts_delta
+The sample offsets stored in a track's stts box are 32-bit unsigned integers. However, very large values usually indicate
+a value written by error or a storage of a small negative value as a way to correct accumulated DTS delay.
+Range is 0 to UINT_MAX. Default is INT_MAX.
+
@subsection Audible AAX
Audible AAX files are encrypted M4B files, and they can be decrypted by specifying a 4 byte activation secret.
@@ -305,6 +305,7 @@ typedef struct MOVContext {
int32_t movie_display_matrix[3][3]; ///< display matrix from mvhd
int have_read_mfra_size;
uint32_t mfra_size;
+ uint32_t max_stts_delta;
} MOVContext;
int ff_mp4_read_descr_len(AVIOContext *pb);
@@ -3965,14 +3965,17 @@ static void mov_build_index(MOVContext *mov, AVStream *st)
current_offset += sample_size;
stream_size += sample_size;
- /* A negative sample duration is invalid based on the spec,
- * but some samples need it to correct the DTS. */
- if (sc->stts_data[stts_index].duration < 0) {
+ /* STTS sample offsets are uint32 but some files store it as int32
+ * with negative values used to correct DTS delays.
+ There may be abnormally large values as well. */
+ if (sc->stts_data[stts_index].duration > mov->max_stts_delta) {
+ // assume high delta is a negative correction if less than 1 second
+ int32_t delta_magnitude = *((int32_t *)&sc->stts_data[stts_index].duration);
av_log(mov->fc, AV_LOG_WARNING,
- "Invalid SampleDelta %d in STTS, at %d st:%d\n",
+ "Correcting too large SampleDelta %u in STTS, at %d st:%d.\n",
sc->stts_data[stts_index].duration, stts_index,
st->index);
- dts_correction += sc->stts_data[stts_index].duration - 1;
+ dts_correction += (delta_magnitude < 0 && FFABS(delta_magnitude) < sc->time_scale ? delta_magnitude - 1 : 0);
sc->stts_data[stts_index].duration = 1;
}
current_dts += sc->stts_data[stts_index].duration;
@@ -8566,6 +8569,7 @@ static const AVOption mov_options[] = {
{ "decryption_key", "The media decryption key (hex)", OFFSET(decryption_key), AV_OPT_TYPE_BINARY, .flags = AV_OPT_FLAG_DECODING_PARAM },
{ "enable_drefs", "Enable external track support.", OFFSET(enable_drefs), AV_OPT_TYPE_BOOL,
{.i64 = 0}, 0, 1, FLAGS },
+ { "max_stts_delta", "treat offsets above this value as invalid", OFFSET(max_stts_delta), AV_OPT_TYPE_INT, {.i64 = INT_MAX}, 0, UINT_MAX, .flags = AV_OPT_FLAG_DECODING_PARAM },
{ NULL },
};