diff --git a/modules/demux/avformat/demux.c b/modules/demux/avformat/demux.c
index dd0f7c9..9224c62 100644
--- a/modules/demux/avformat/demux.c
+++ b/modules/demux/avformat/demux.c
@@ -47,6 +47,7 @@
 
 #include "../../codec/avcodec/avcodec.h"
 #include "avformat.h"
+#include "../vobsub.h"
 
 //#define AVFORMAT_DEBUG 1
 
@@ -271,6 +272,48 @@ int OpenDemux( vlc_object_t *p_this )
 
         case CODEC_TYPE_SUBTITLE:
             es_format_Init( &fmt, SPU_ES, fcc );
+            if( strncmp( p_sys->ic->iformat->name, "matroska", 8 ) == 0 &&
+                cc->codec_id == CODEC_ID_DVD_SUBTITLE &&
+                cc->extradata != NULL &&
+                cc->extradata_size > 0 )
+            {
+                char *p_start;
+                char *p_buf = malloc( cc->extradata_size + 1);
+                if( p_buf != NULL )
+                {
+                    memcpy( p_buf, cc->extradata , cc->extradata_size );
+                    p_buf[cc->extradata_size] = '\0';
+
+                    p_start = strstr( p_buf, "size:" );
+                    if( p_start && 
+                        vobsub_size_parse( p_start, 
+                                           &fmt.subs.spu.i_original_frame_width,
+                                           &fmt.subs.spu.i_original_frame_height ) )
+                    {
+                        msg_Dbg( p_demux, "original frame size: %dx%d", 
+                                 fmt.subs.spu.i_original_frame_width, 
+                                 fmt.subs.spu.i_original_frame_height );
+                    }
+                    else
+                    {
+                        msg_Warn( p_demux, "reading original frame size failed" );
+                    }
+
+                    p_start = strstr( p_buf, "palette:" );
+                    if( p_start && 
+                        vobsub_palette_parse( p_start, &fmt.subs.spu.palette[1] ) )
+                    {
+                        fmt.subs.spu.palette[0] =  0xBeef; 
+                        msg_Dbg( p_demux, "vobsub palette read" );
+                    }
+                    else
+                    {
+                        msg_Warn( p_demux, "reading original palette failed" );
+                    }
+                    free( p_buf );
+                }
+            }
+
             psz_type = "subtitle";
             break;
 
diff --git a/modules/demux/mkv/matroska_segment.cpp b/modules/demux/mkv/matroska_segment.cpp
index 26d43b4..0797b91 100644
--- a/modules/demux/mkv/matroska_segment.cpp
+++ b/modules/demux/mkv/matroska_segment.cpp
@@ -28,6 +28,10 @@
 
 #include "demux.hpp"
 
+extern "C" {
+#include "../vobsub.h"
+}
+
 /* GetFourCC helper */
 #define GetFOURCC( p )  __GetFOURCC( (uint8_t*)p )
 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
@@ -1097,20 +1101,39 @@ bool matroska_segment_c::Select( mtime_t i_start_time )
             {
                 char *p_start;
                 char *p_buf = (char *)malloc( tracks[i_track]->i_extra_data + 1);
-                memcpy( p_buf, tracks[i_track]->p_extra_data , tracks[i_track]->i_extra_data );
-                p_buf[tracks[i_track]->i_extra_data] = '\0';
- 
-                p_start = strstr( p_buf, "size:" );
-                if( sscanf( p_start, "size: %dx%d",
-                        &tracks[i_track]->fmt.subs.spu.i_original_frame_width, &tracks[i_track]->fmt.subs.spu.i_original_frame_height ) == 2 )
+                if (p_buf != NULL)
                 {
-                    msg_Dbg( &sys.demuxer, "original frame size vobsubs: %dx%d", tracks[i_track]->fmt.subs.spu.i_original_frame_width, tracks[i_track]->fmt.subs.spu.i_original_frame_height );
-                }
-                else
-                {
-                    msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
+                    memcpy( p_buf, tracks[i_track]->p_extra_data , tracks[i_track]->i_extra_data );
+                    p_buf[tracks[i_track]->i_extra_data] = '\0';
+ 
+                    p_start = strstr( p_buf, "size:" );
+                    if( p_start && 
+                        vobsub_size_parse( p_start, 
+                                           &tracks[i_track]->fmt.subs.spu.i_original_frame_width,
+                                           &tracks[i_track]->fmt.subs.spu.i_original_frame_height ) )
+                    {
+                        msg_Dbg( &sys.demuxer, "original frame size vobsubs: %dx%d", 
+                                 tracks[i_track]->fmt.subs.spu.i_original_frame_width, 
+                                 tracks[i_track]->fmt.subs.spu.i_original_frame_height );
+                    }
+                    else
+                    {
+                        msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
+                    }
+
+                    p_start = strstr( p_buf, "palette:" );
+                    if( p_start && 
+                        vobsub_palette_parse( p_start, &tracks[i_track]->fmt.subs.spu.palette[1] ) )
+                    {
+                        tracks[i_track]->fmt.subs.spu.palette[0] =  0xBeef;
+                        msg_Dbg( &sys.demuxer, "vobsub palette read" );
+                    }
+                    else
+                    {
+                        msg_Warn( &sys.demuxer, "reading original palette failed" );
+                    }
+                    free( p_buf );
                 }
-                free( p_buf );
             }
         }
         else if( !strcmp( tracks[i_track]->psz_codec, "B_VOBBTN" ) )
diff --git a/modules/demux/vobsub.c b/modules/demux/vobsub.c
index 3b90e2d..bb71153 100644
--- a/modules/demux/vobsub.c
+++ b/modules/demux/vobsub.c
@@ -40,6 +40,7 @@
 #include <vlc_charset.h>
 
 #include "ps.h"
+#include "vobsub.h"
 
 #define MAX_LINE 8192
 
@@ -488,8 +489,8 @@ static int ParseVobSubIDX( demux_t *p_demux )
         else if( !strncmp( "size:", line, 5 ) )
         {
             /* Store the original size of the video */
-            if( sscanf( line, "size: %dx%d",
-                        &p_sys->i_original_frame_width, &p_sys->i_original_frame_height ) == 2 )
+            if( vobsub_size_parse( line, &p_sys->i_original_frame_width,
+                                   &p_sys->i_original_frame_height ) )
             {
                 msg_Dbg( p_demux, "original frame size: %dx%d", p_sys->i_original_frame_width, p_sys->i_original_frame_height );
             }
@@ -500,33 +501,8 @@ static int ParseVobSubIDX( demux_t *p_demux )
         }
         else if( !strncmp( "palette:", line, 8 ) )
         {
-            int i;
-
-            /* Store the palette of the subs */
-            if( sscanf( line, "palette: %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x, %x",
-                        &p_sys->palette[0], &p_sys->palette[1], &p_sys->palette[2], &p_sys->palette[3],
-                        &p_sys->palette[4], &p_sys->palette[5], &p_sys->palette[6], &p_sys->palette[7],
-                        &p_sys->palette[8], &p_sys->palette[9], &p_sys->palette[10], &p_sys->palette[11],
-                        &p_sys->palette[12], &p_sys->palette[13], &p_sys->palette[14], &p_sys->palette[15] ) == 16 )
+            if( vobsub_palette_parse( line, p_sys->palette ) )
             {
-                for( i = 0; i < 16; i++ )
-                {
-                    uint8_t r = 0, g = 0, b = 0;
-                    uint8_t y = 0, u = 0, v = 0;
-                    r = (p_sys->palette[i] >> 16) & 0xff;
-                    g = (p_sys->palette[i] >> 8) & 0xff;
-                    b = (p_sys->palette[i] >> 0) & 0xff;
-                    /* msg_Dbg( p_demux, "palette %d: R=%x, G=%x, B=%x", i, r, g, b ); */
-                    y = (uint8_t) __MIN(abs(r * 2104 + g * 4130 + b * 802 + 4096 + 131072) >> 13, 235);
-                    u = (uint8_t) __MIN(abs(r * -1214 + g * -2384 + b * 3598 + 4096 + 1048576) >> 13, 240);
-                    v = (uint8_t) __MIN(abs(r * 3598 + g * -3013 + b * -585 + 4096 + 1048576) >> 13, 240);
-                    p_sys->palette[i] = 0;
-                    p_sys->palette[i] |= (y&0xff)<<16;
-                    p_sys->palette[i] |= (u&0xff);
-                    p_sys->palette[i] |= (v&0xff)<<8;
-                    /* msg_Dbg( p_demux, "palette %d: y=%x, u=%x, v=%x", i, y, u, v ); */
-
-                }
                 p_sys->b_palette = true;
                 msg_Dbg( p_demux, "vobsub palette read" );
             }
diff --git a/modules/demux/vobsub.h b/modules/demux/vobsub.h
new file mode 100644
index 0000000..80e14bb
--- /dev/null
+++ b/modules/demux/vobsub.h
@@ -0,0 +1,70 @@
+/*****************************************************************************
+ * vobsub.h: Vobsub support
+ *****************************************************************************
+ * Copyright (C) 2008 the VideoLAN team
+ * $Id$
+ *
+ * Authors: John Stebbins
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU 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.
+ *****************************************************************************/
+
+static inline void vobsub_palette_rgb2yuv( uint32_t *palette )
+{
+    int i;
+    for( i = 0; i < 16; i++ )
+    {
+        uint8_t r, g, b, y, u, v;
+        r = (palette[i] >> 16) & 0xff;
+        g = (palette[i] >> 8) & 0xff;
+        b = (palette[i] >> 0) & 0xff;
+        y = (uint8_t) __MIN(abs(r * 2104 + g * 4130 + b * 802 + 4096 + 131072) >> 13, 235);
+        u = (uint8_t) __MIN(abs(r * -1214 + g * -2384 + b * 3598 + 4096 + 1048576) >> 13, 240);
+        v = (uint8_t) __MIN(abs(r * 3598 + g * -3013 + b * -585 + 4096 + 1048576) >> 13, 240);
+        palette[i] = (y&0xff)<<16 | (v&0xff)<<8 | (u&0xff);
+    }
+}
+
+static inline bool vobsub_palette_parse( const char *buf, uint32_t *palette )
+{
+    if( sscanf( buf, "palette: %x, %x, %x, %x, %x, %x, %x, %x, "
+                              "%x, %x, %x, %x, %x, %x, %x, %x",
+                &palette[0], &palette[1], &palette[2], &palette[3],
+                &palette[4], &palette[5], &palette[6], &palette[7],
+                &palette[8], &palette[9], &palette[10], &palette[11],
+                &palette[12], &palette[13], &palette[14], &palette[15] ) == 16 )
+    {
+        vobsub_palette_rgb2yuv( palette );
+        return true;
+    }
+    else
+    {
+        return false;
+    }
+}
+
+static inline bool vobsub_size_parse( const char *buf, int *original_frame_width, int *original_frame_height )
+{
+    if( sscanf( buf, "size: %dx%d",
+                original_frame_width, original_frame_height ) == 2 )
+    {
+        return true;
+    }
+    else
+    {
+        return false;
+    }
+}
+
