From a4cdf1a727a3d9d59ba200c944a7eb74806c752c Mon Sep 17 00:00:00 2001 From: wm4 Date: Tue, 12 Jan 2016 23:50:01 +0100 Subject: demux_lavf: fix charset conversion with UTF-16 subtitles UTF-16 subtitles are special in that they are usually read by libavformat directly, even though they are not in UTF-8. This is explicitly handled convert_charset() and skips conversion to UTF-8. There was a bug due to not resetting the file position: if conversion happens, the actual stream is replaced with a memory stream containing the converted data, but if conversion is skipped, the original stream with the wrong file position is kept. Fix by always opening a memory stream. (We _could_ seek back, but there is a slight possibility of additional failure due to unseekable streams.) Also, don't enter conversion if the subtitle is detected as UTF-8 either. Fixes #2700. --- demux/demux_lavf.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'demux/demux_lavf.c') diff --git a/demux/demux_lavf.c b/demux/demux_lavf.c index 712b208460..e054d29bbd 100644 --- a/demux/demux_lavf.c +++ b/demux/demux_lavf.c @@ -275,18 +275,19 @@ static void convert_charset(struct demuxer *demuxer) MP_WARN(demuxer, "File too big (or error reading) - skip charset probing.\n"); return; } + void *alloc = data.start; cp = (char *)mp_charset_guess(priv, demuxer->log, data, cp, 0); if (cp && !mp_charset_is_utf8(cp)) MP_INFO(demuxer, "Using subtitle charset: %s\n", cp); // libavformat transparently converts UTF-16 to UTF-8 - if (!mp_charset_is_utf16(cp)) { + if (!mp_charset_is_utf16(cp) && !mp_charset_is_utf8(cp)) { bstr conv = mp_iconv_to_utf8(demuxer->log, data, cp, MP_ICONV_VERBOSE); if (conv.start) - priv->stream = open_memory_stream(conv.start, conv.len); - if (conv.start != data.start) - talloc_free(conv.start); + data = conv; } - talloc_free(data.start); + if (data.start) + priv->stream = open_memory_stream(data.start, data.len); + talloc_free(alloc); } static char *remove_prefix(char *s, const char *const *prefixes) -- cgit v1.2.3