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.
This commit is contained in:
wm4 2016-01-12 23:50:01 +01:00
parent e420464ba6
commit a4cdf1a727
1 changed files with 6 additions and 5 deletions

View File

@ -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)