[vlc-commits] demux: subtitle: dont break non-seekable streams
Francois Cartegnie
git at videolan.org
Tue Sep 22 00:38:49 CEST 2015
vlc | branch: master | Francois Cartegnie <fcvlcdev at free.fr> | Thu Sep 10 22:54:33 2015 +0200| [c20bdd8d9238a8f3912a341df3a5f413324669e0] | committer: Francois Cartegnie
demux: subtitle: dont break non-seekable streams
> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=c20bdd8d9238a8f3912a341df3a5f413324669e0
---
modules/demux/Makefile.am | 5 +++--
modules/demux/subtitle.c | 25 +++++++++++++++-------
modules/demux/subtitle_helper.h | 44 +++++++++++++++++++++++++++++++++++++++
modules/demux/vobsub.c | 8 +++----
4 files changed, 67 insertions(+), 15 deletions(-)
diff --git a/modules/demux/Makefile.am b/modules/demux/Makefile.am
index cf290df..578ea1d 100644
--- a/modules/demux/Makefile.am
+++ b/modules/demux/Makefile.am
@@ -56,7 +56,7 @@ demux_LTLIBRARIES += libaiff_plugin.la
libmjpeg_plugin_la_SOURCES = demux/mjpeg.c demux/mxpeg_helper.h
demux_LTLIBRARIES += libmjpeg_plugin.la
-libsubtitle_plugin_la_SOURCES = demux/subtitle.c
+libsubtitle_plugin_la_SOURCES = demux/subtitle.c demux/subtitle_helper.h
libsubtitle_plugin_la_LIBADD = $(LIBM)
demux_LTLIBRARIES += libsubtitle_plugin.la
@@ -64,7 +64,8 @@ libty_plugin_la_SOURCES = demux/ty.c codec/cc.h
demux_LTLIBRARIES += libty_plugin.la
libvobsub_plugin_la_SOURCES = demux/vobsub.c demux/vobsub.h \
- demux/mpeg/ps.h demux/mpeg/pes.h
+ demux/mpeg/ps.h demux/mpeg/pes.h \
+ demux/subtitle_helper.h
demux_LTLIBRARIES += libvobsub_plugin.la
libvoc_plugin_la_SOURCES = demux/voc.c
diff --git a/modules/demux/subtitle.c b/modules/demux/subtitle.c
index 68aae24..59b8e02 100644
--- a/modules/demux/subtitle.c
+++ b/modules/demux/subtitle.c
@@ -38,10 +38,13 @@
#include <ctype.h>
#include <math.h>
+#include <assert.h>
#include <vlc_demux.h>
#include <vlc_charset.h>
+#include "subtitle_helper.h"
+
/*****************************************************************************
* Module descriptor
*****************************************************************************/
@@ -299,6 +302,11 @@ static int Open ( vlc_object_t *p_this )
}
free( psz_type );
+#ifndef NDEBUG
+ const uint64_t i_start_pos = stream_Tell( p_demux->s );
+#endif
+ uint64_t i_read_offset = 0;
+
/* Detect Unicode while skipping the UTF-8 Byte Order Mark */
bool unicode = false;
const uint8_t *p_data;
@@ -306,7 +314,7 @@ static int Open ( vlc_object_t *p_this )
&& !memcmp( p_data, "\xEF\xBB\xBF", 3 ) )
{
unicode = true;
- stream_Seek( p_demux->s, 3 ); /* skip BOM */
+ i_read_offset = 3; /* skip BOM */
msg_Dbg( p_demux, "detected Unicode Byte Order Mark" );
}
@@ -322,7 +330,7 @@ static int Open ( vlc_object_t *p_this )
int i_dummy;
char p_dummy;
- if( ( s = stream_ReadLine( p_demux->s ) ) == NULL )
+ if( (s = peek_Readline( p_demux->s, &i_read_offset )) == NULL )
break;
if( strcasestr( s, "<SAMI>" ) )
@@ -474,17 +482,15 @@ static int Open ( vlc_object_t *p_this )
}
free( s );
-
- /* It will nearly always work even for non seekable stream thanks the
- * caching system, and if it fails we lose just a few sub */
- if( stream_Seek( p_demux->s, unicode ? 3 : 0 ) )
- msg_Warn( p_demux, "failed to rewind" );
}
/* Quit on unknown subtitles */
if( p_sys->i_type == SUB_TYPE_UNKNOWN )
{
- stream_Seek( p_demux->s, 0 );
+#ifndef NDEBUG
+ /* Ensure it will work with non seekable streams */
+ assert( i_start_pos == stream_Tell( p_demux->s ) );
+#endif
msg_Warn( p_demux, "failed to recognize subtitle type" );
free( p_sys );
return VLC_EGENERIC;
@@ -503,6 +509,9 @@ static int Open ( vlc_object_t *p_this )
msg_Dbg( p_demux, "loading all subtitles..." );
+ if( unicode ) /* skip BOM */
+ stream_Seek( p_demux->s, 3 );
+
/* Load the whole file */
TextLoad( &p_sys->txt, p_demux->s );
diff --git a/modules/demux/subtitle_helper.h b/modules/demux/subtitle_helper.h
new file mode 100644
index 0000000..7b921ca
--- /dev/null
+++ b/modules/demux/subtitle_helper.h
@@ -0,0 +1,44 @@
+/*****************************************************************************
+ * subtitle.h: subtitle helper functions
+ *****************************************************************************
+ * Copyright (C) 2015 VLC authors and VideoLAN
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+inline static char * peek_Readline( stream_t *p_demuxstream, uint64_t *pi_offset )
+{
+ uint8_t *p_peek;
+ ssize_t i_peek = stream_Peek( p_demuxstream, (const uint8_t **) &p_peek,
+ *pi_offset + 2048 );
+ if( i_peek <= 0 )
+ return NULL;
+
+ const uint64_t i_bufsize = (uint64_t) i_peek - *pi_offset;
+ char *psz_line = NULL;
+
+ /* Create a stream memory from that offset */
+ stream_t *p_memorystream = stream_MemoryNew( p_demuxstream, &p_peek[*pi_offset],
+ i_bufsize, true );
+ if( p_memorystream )
+ {
+ psz_line = stream_ReadLine( p_memorystream );
+
+ *pi_offset += stream_Tell( p_memorystream );
+ stream_Delete( p_memorystream );
+ }
+
+ return psz_line;
+}
diff --git a/modules/demux/vobsub.c b/modules/demux/vobsub.c
index 2b71bd4..ff64e53 100644
--- a/modules/demux/vobsub.c
+++ b/modules/demux/vobsub.c
@@ -39,6 +39,7 @@
#include "mpeg/pes.h"
#include "mpeg/ps.h"
#include "vobsub.h"
+#include "subtitle_helper.h"
/*****************************************************************************
* Module descriptor
@@ -122,17 +123,14 @@ static int Open ( vlc_object_t *p_this )
demux_sys_t *p_sys;
char *psz_vobname, *s;
int i_len;
+ uint64_t i_read_offset = 0;
- if( ( s = stream_ReadLine( p_demux->s ) ) != NULL )
+ if( ( s = peek_Readline( p_demux->s, &i_read_offset ) ) != NULL )
{
if( !strcasestr( s, "# VobSub index file" ) )
{
msg_Dbg( p_demux, "this doesn't seem to be a vobsub file" );
free( s );
- if( stream_Seek( p_demux->s, 0 ) )
- {
- msg_Warn( p_demux, "failed to rewind" );
- }
return VLC_EGENERIC;
}
free( s );
More information about the vlc-commits
mailing list