Fixes #7855 (“Last subtitle in MP4 is displayed forever”).
---
libavformat/isom.h | 3 +
libavformat/mov.c | 158 +
2 files changed, 135 insertions(+), 26 deletions(-)
diff --git a/libavformat/isom.h b/libavformat/isom.h
index 69452cae8e..b83744ba09 100644
--- a/libavformat/isom.h
+++ b/libavformat/isom.h
@@ -162,6 +162,7 @@ typedef struct MOVStreamContext {
unsigned int chunk_count;
int64_t *chunk_offsets;
unsigned int stts_count;
+unsigned int stts_allocated_size;
MOVStts *stts_data;
unsigned int ctts_count;
unsigned int ctts_allocated_size;
@@ -174,6 +175,8 @@ typedef struct MOVStreamContext {
unsigned *stps_data; ///< partial sync sample for mpeg-2 open gop
MOVElst *elst_data;
unsigned int elst_count;
+int stts_index;
+int stts_sample;
int ctts_index;
int ctts_sample;
unsigned int sample_size; ///< may contain value calculated from stsd or
value from stsz atom
diff --git a/libavformat/mov.c b/libavformat/mov.c
index d0347b2970..3d5f5f7ab0 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -2886,7 +2886,7 @@ static int mov_read_stts(MOVContext *c, AVIOContext *pb,
MOVAtom atom)
{
AVStream *st;
MOVStreamContext *sc;
-unsigned int i, entries, alloc_size = 0;
+unsigned int i, entries = 0;
int64_t duration=0;
int64_t total_sample_count=0;
@@ -2913,7 +2913,7 @@ static int mov_read_stts(MOVContext *c, AVIOContext *pb,
MOVAtom atom)
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,
+MOVStts *stts_data = av_fast_realloc(sc->stts_data,
&sc->stts_allocated_size,
min_entries *
sizeof(*sc->stts_data));
if (!stts_data) {
av_freep(&sc->stts_data);
@@ -3130,11 +3130,15 @@ static int get_edit_list_entry(MOVContext *mov,
static int find_prev_closest_index(AVStream *st,
AVIndexEntry *e_old,
int nb_old,
+ MOVStts* stts_data,
+ int64_t stts_count,
MOVStts* ctts_data,
int64_t ctts_count,
int64_t timestamp_pts,
int flag,
int64_t* index,
+ int64_t* stts_index,
+ int64_t* stts_sample,
int64_t* ctts_index,
int64_t* ctts_sample)
{
@@ -3176,6 +3180,15 @@ static int find_prev_closest_index(AVStream *st,
// Find out the ctts_index for the found frame.
*ctts_index = 0;
*ctts_sample = 0;
+
+if (stts_data) {
+av_assert0(stts_index);
+av_assert0(stts_sample);
+
+*stts_index = 0;
+*stts_sample = 0;
+}
+
for (index_ctts_count = 0; index_ctts_count < *index;
index_ctts_count++) {
if (*ctts_index < ctts_count) {
(*ctts_sample)++;
@@ -3184,6 +3197,13 @@ static int find_prev_closest_index(AVStream *st,
*ctts_sample = 0;
}
}
+if (stts_data && *stts_index < stts_count) {
+(*stts_sample)++;
+if (*stts_sample == stts_data[*stts_index].count) {
+(*stts_index)++;
+*stts_sample = 0;
+}
+}
}
while (*index >= 0 && (*ctts_index) >= 0 && (*ctts_index) <
ctts_count) {
@@ -3203,6 +3223,16 @@ static int find_prev_closest_index(AVStream *st,
} else {
(*ctts_sample)--;
}
+if (stts_data) {
+if (*stts_sample == 0) {
+(*stts_index)--;
+if (*stts_index >= 0) {
+*stts_sample = stts_data[*stts_index].count - 1;
+}
+} else {
+(*stts_sample)--;
+}
+}
}
}
@@ -3275,34 +3305,44 @@ static void fix_index_entry_timestamps(AVStream* st,
int end_index, int64_t end_
}
/**
- * Append a new ctts entry to ctts_data.
- * Returns the new ctts_count if successful, else returns -1.
+ * Append a new stts entry to stts_data.
+ * Returns the new stts_count if successful, else returns -1.
*/
-static int64_t add_ctts_entry(MOVStts** ctts_data, unsigned int* ctts_count,
unsigned int* allocated_size,
+static int64_t add_stts_entry(MOVStts** stts_data, unsigned int* stts_count,
unsigned int* allocated_size,
int count, int duration)
{
-MOVStts *ctts_b