[FFmpeg-devel] [PATCH] avformat/fivdec: cached keyframes before video or audio stream was created
Xinzheng Zhang
zhangxzheng at gmail.com
Tue Jul 26 15:17:46 EEST 2016
---
libavformat/flvdec.c | 84 ++++++++++++++++++++++++++++++++++++++++++----------
1 file changed, 69 insertions(+), 15 deletions(-)
diff --git a/libavformat/flvdec.c b/libavformat/flvdec.c
index 2bf1e05..82b9f83 100644
--- a/libavformat/flvdec.c
+++ b/libavformat/flvdec.c
@@ -61,6 +61,12 @@ typedef struct FLVContext {
int broken_sizes;
int sum_flv_tag_size;
+
+ int vhead_exists;
+ int ahead_exists;
+ int keyframe_count;
+ int64_t *keyframe_times;
+ int64_t *keyframe_filepositions;
} FLVContext;
static int probe(AVProbeData *p, int live)
@@ -92,6 +98,42 @@ static int live_flv_probe(AVProbeData *p)
return probe(p, 1);
}
+static void add_keyframes_index(AVFormatContext *s)
+{
+ FLVContext *flv = s->priv_data;
+ AVStream *current_stream = NULL;
+ AVStream *vstream = NULL;
+ AVStream *astream = NULL;
+ unsigned int i,j ;
+
+ for (i = 0; i< s->nb_streams; i++) {
+ if (s->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
+ vstream = s->streams[i];
+ } else if (s->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
+ astream = s->streams[i];
+ }
+ }
+
+ AVStream *streams[2] = {vstream, astream};
+ for (i = 0; i < 2; i++) {
+ current_stream = streams[i];
+ if (current_stream && current_stream->nb_index_entries==0) {
+ for (j = 0; j < flv->keyframe_count; j++) {
+ av_add_index_entry(current_stream, flv->keyframe_filepositions[j], flv->keyframe_times[j] * 1000,
+ 0, 0, AVINDEX_KEYFRAME);
+ }
+ }
+ }
+
+ // free keyframe index only if all expected streams have been created
+ if (((vstream && vstream->nb_index_entries>0) || !flv->vhead_exists) &&
+ ((astream && astream->nb_index_entries>0) || !flv->ahead_exists)) {
+ av_freep(&flv->keyframe_times);
+ av_freep(&flv->keyframe_filepositions);
+ flv->keyframe_count = 0;
+ }
+}
+
static AVStream *create_stream(AVFormatContext *s, int codec_type)
{
AVStream *st = avformat_new_stream(s, NULL);
@@ -104,6 +146,7 @@ static AVStream *create_stream(AVFormatContext *s, int codec_type)
s->ctx_flags &= ~AVFMTCTX_NOHEADER;
avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
+ add_keyframes_index(s);
return st;
}
@@ -305,8 +348,7 @@ static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize)
return length;
}
-static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc,
- AVStream *vstream, int64_t max_pos)
+static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc, int64_t max_pos)
{
FLVContext *flv = s->priv_data;
unsigned int timeslen = 0, fileposlen = 0, i;
@@ -316,7 +358,7 @@ static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc,
int ret = AVERROR(ENOSYS);
int64_t initial_pos = avio_tell(ioc);
- if (vstream->nb_index_entries>0) {
+ if (flv->keyframe_count > 0) {
av_log(s, AV_LOG_WARNING, "Skipping duplicate index\n");
return 0;
}
@@ -324,6 +366,10 @@ static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc,
if (s->flags & AVFMT_FLAG_IGNIDX)
return 0;
+ av_freep(&flv->keyframe_times);
+ av_freep(&flv->keyframe_filepositions);
+ flv->keyframe_count = 0;
+
while (avio_tell(ioc) < max_pos - 2 &&
amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
int64_t **current_array;
@@ -366,17 +412,17 @@ static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc,
break;
}
}
-
if (timeslen == fileposlen && fileposlen>1 && max_pos <= filepositions[0]) {
- for (i = 0; i < fileposlen; i++) {
- av_add_index_entry(vstream, filepositions[i], times[i] * 1000,
- 0, 0, AVINDEX_KEYFRAME);
- if (i < 2) {
- flv->validate_index[i].pos = filepositions[i];
- flv->validate_index[i].dts = times[i] * 1000;
- flv->validate_count = i + 1;
- }
+ for (i = 0; i < FFMIN(2,fileposlen); i++) {
+ flv->validate_index[i].pos = filepositions[i];
+ flv->validate_index[i].dts = times[i] * 1000;
+ flv->validate_count = i + 1;
}
+ flv->keyframe_times = times;
+ flv->keyframe_filepositions = filepositions;
+ flv->keyframe_count = timeslen;
+ times = NULL;
+ filepositions = NULL;
} else {
invalid:
av_log(s, AV_LOG_WARNING, "Invalid keyframes object, skipping.\n");
@@ -418,12 +464,14 @@ static int amf_parse_object(AVFormatContext *s, AVStream *astream,
}
break;
case AMF_DATA_TYPE_OBJECT:
- if ((vstream || astream) && key &&
+ if (key &&
ioc->seekable &&
!strcmp(KEYFRAMES_TAG, key) && depth == 1)
- if (parse_keyframes_index(s, ioc, vstream ? vstream : astream,
+ if (parse_keyframes_index(s, ioc,
max_pos) < 0)
av_log(s, AV_LOG_ERROR, "Keyframe index parsing failed\n");
+ else
+ add_keyframes_index(s);
while (avio_tell(ioc) < max_pos - 2 &&
amf_get_string(ioc, str_val, sizeof(str_val)) > 0)
@@ -631,9 +679,13 @@ static int flv_read_header(AVFormatContext *s)
{
FLVContext *flv = s->priv_data;
int offset;
+ int flags;
avio_skip(s->pb, 4);
- avio_r8(s->pb); // flags
+ flags = avio_r8(s->pb); // flags
+
+ flv->vhead_exists = flags & FLV_HEADER_FLAG_HASVIDEO;
+ flv->ahead_exists = flags & FLV_HEADER_FLAG_HASAUDIO;
s->ctx_flags |= AVFMTCTX_NOHEADER;
@@ -653,6 +705,8 @@ static int flv_read_close(AVFormatContext *s)
FLVContext *flv = s->priv_data;
for (i=0; i<FLV_STREAM_TYPE_NB; i++)
av_freep(&flv->new_extradata[i]);
+ av_freep(&flv->keyframe_times);
+ av_freep(&flv->keyframe_filepositions);
return 0;
}
--
2.6.4 (Apple Git-63)
More information about the ffmpeg-devel
mailing list