summaryrefslogtreecommitdiffstats
path: root/demux/demux_lavf.c
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2016-08-26 13:05:14 +0200
committerwm4 <wm4@nowhere>2016-08-26 13:34:52 +0200
commitbc97d60542b353141bedeee985f9e5bd68b9f076 (patch)
tree63a0f4d0e077929ad0f46934e4f07c6016433e1f /demux/demux_lavf.c
parent4121016689668921538317449c0ea9063fc086fc (diff)
downloadmpv-bc97d60542b353141bedeee985f9e5bd68b9f076.tar.bz2
mpv-bc97d60542b353141bedeee985f9e5bd68b9f076.tar.xz
demux: close underlying stream if it's fully read anyway
This is for text subtitles. libavformat currently always reads text subtitles completely on init. This means the underlying stream is useless and will consume resources for various reasons (network connection, file handles, cache memory). Take care of this by closing the underlying stream if we think the demuxer has read everything. Since libavformat doesn't export whether it did (or whether it may access the stream again in the future), we rely on a whitelist. Also, instead of setting the stream to NULL or so, set it to an empty dummy stream. This way we don't have to litter the code with NULL checks. demux_lavf.c needs extra changes, because it tries to do clever things for the sake of subtitle charset conversion. The main reason we keep the demuxer etc. open is because we fell for libavformat being so generic, and we tried to remove corresponding special-cases in the higher-level player code. Some of this is forced due to ass/srt mkv/mp4 demuxing being very similar to external text files. In the future it might be better to do this in a more straight-forward way, such as reading text subtitles into libass and then discarding the demuxer entirely, but for aforementioned reasons this could be more of a mess than the solution introduced by this commit. Probably fixes #3456.
Diffstat (limited to 'demux/demux_lavf.c')
-rw-r--r--demux/demux_lavf.c13
1 files changed, 11 insertions, 2 deletions
diff --git a/demux/demux_lavf.c b/demux/demux_lavf.c
index 4b236ee2be..d7dbfe3ff1 100644
--- a/demux/demux_lavf.c
+++ b/demux/demux_lavf.c
@@ -162,6 +162,7 @@ static const struct format_hack format_hacks[] = {
typedef struct lavf_priv {
struct stream *stream;
+ bool own_stream;
char *filename;
struct format_hack format_hack;
AVInputFormat *avif;
@@ -290,8 +291,10 @@ static void convert_charset(struct demuxer *demuxer)
if (conv.start)
data = conv;
}
- if (data.start)
+ if (data.start) {
priv->stream = open_memory_stream(data.start, data.len);
+ priv->own_stream = true;
+ }
talloc_free(alloc);
}
@@ -1068,6 +1071,12 @@ redo:
av_seek_frame(priv->avfc, 0, stream_tell(priv->stream),
AVSEEK_FLAG_BYTE);
return DEMUXER_CTRL_OK;
+ case DEMUXER_CTRL_REPLACE_STREAM:
+ if (priv->own_stream)
+ free_stream(priv->stream);
+ priv->own_stream = false;
+ priv->stream = demuxer->stream;
+ return DEMUXER_CTRL_OK;
default:
return DEMUXER_CTRL_NOTIMPL;
}
@@ -1092,7 +1101,7 @@ static void demux_close_lavf(demuxer_t *demuxer)
#endif
}
}
- if (priv->stream != demuxer->stream)
+ if (priv->own_stream)
free_stream(priv->stream);
talloc_free(priv);
demuxer->priv = NULL;