]> git.sesse.net Git - vlc/blobdiff - modules/codec/dvbsub.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / modules / codec / dvbsub.c
index 3daddbf20137b52976c4e20a23a86b02af628887..fb7619cfa4a22e54605cce34572b852095c48024 100644 (file)
@@ -3,7 +3,7 @@
  *            DVB subtitles encoder (developed for Anevia, www.anevia.com)
  *****************************************************************************
  * Copyright (C) 2003 ANEVIA
- * Copyright (C) 2003-2005 the VideoLAN team
+ * Copyright (C) 2003-2009 the VideoLAN team
  * $Id$
  *
  * Authors: Gildas Bazin <gbazin@videolan.org>
@@ -11,6 +11,7 @@
  *          Laurent Aimar <fenrir@via.ecp.fr>
  *          Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
  *          Derk-Jan Hartman <hartman #at# videolan dot org>
+ *          Simon Hailes <simon _a_ screen.subtitling.com>
  *
  * 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
  * 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.
+ * along with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
+
 /*****************************************************************************
  * Preamble
  *
  * character table identified in the subtitle descriptor.
  *
  * The spec is quite vague in this area, but what is meant is perhaps that it
- * refers to the character index in the codepage belonging to the language specified
- * in the subtitle descriptor. Potentially it's designed for widechar
+ * refers to the character index in the codepage belonging to the language
+ * specified in the subtitle descriptor. Potentially it's designed for widechar
  * (but not for UTF-*) codepages.
- *****************************************************************************/
+ *****************************************************************************
+ *
+ *****************************************************************************
+ * Notes on DDS (Display Definition Segment)
+ * -----------------------------------------
+ * DDS (Display Definition Segment) tells the decoder how the subtitle image
+ * relates to the video image.
+ * For SD, the subtitle image is always considered to be for display at
+ * 720x576 (although it's assumed that for NTSC, this is 720x480, this
+ * is not documented well) Also, for SD, the subtitle image is drawn 'on
+ * the glass' (i.e. after video scaling, letterbox, etc.)
+ * For 'HD' (subs marked type 0x14/0x24 in PSI), a DDS must be present,
+ * and the subs area is drawn onto the video area (scales if necessary).
+ * The DDS tells the decoder what resolution the subtitle images were
+ * intended for, and hence how to scale the subtitle images for a
+ * particular video size
+ * i.e. if HD video is presented as letterbox, the subs will be in the
+ * same place on the video as if the video was presented on an HD set
+ * indeed, if the HD video was pillarboxed by the decoder, the subs may
+ * be cut off as well as the video. The intent here is that the subs can
+ * be placed accurately on the video - something which was missed in the
+ * original spec.
+ *
+ * A DDS may also specify a window - this is where the subs images are moved so that the (0,0)
+ * origin of decode is offset.
+ ********************************************************************************************/
+
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
@@ -150,7 +178,8 @@ typedef struct dvbsub_display_s
     int                     i_width;
     int                     i_height;
 
-    bool              b_windowed;
+    bool                    b_windowed;
+    /* these values are only relevant if windowed */
     int                     i_x;
     int                     i_y;
     int                     i_max_x;
@@ -218,24 +247,25 @@ typedef struct
 
 struct decoder_sys_t
 {
-    bsw_t            bs;
+    bs_t               bs;
 
     /* Decoder internal data */
-    int             i_id;
-    int             i_ancillary_id;
-    mtime_t         i_pts;
-
-    bool      b_absolute;
-    int             i_spu_position;
-    int             i_spu_x;
-    int             i_spu_y;
-
-    bool      b_page;
-    dvbsub_page_t   *p_page;
-    dvbsub_region_t *p_regions;
-    dvbsub_clut_t   *p_cluts;
-    dvbsub_display_t *p_display;
-    dvbsub_clut_t    default_clut;
+    int                i_id;
+    int                i_ancillary_id;
+    mtime_t            i_pts;
+
+    bool               b_absolute;
+    int                i_spu_position;
+    int                i_spu_x;
+    int                i_spu_y;
+
+    bool               b_page;
+    dvbsub_page_t      *p_page;
+    dvbsub_region_t    *p_regions;
+    dvbsub_clut_t      *p_cluts;
+    /* this is very small, so keep forever */
+    dvbsub_display_t   display;
+    dvbsub_clut_t      default_clut;
 };
 
 
@@ -270,15 +300,16 @@ struct decoder_sys_t
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
-static void decode_segment( decoder_t *, bsw_t * );
-static void decode_page_composition( decoder_t *, bsw_t * );
-static void decode_region_composition( decoder_t *, bsw_t * );
-static void decode_object( decoder_t *, bsw_t * );
-static void decode_display_definition( decoder_t *, bsw_t * );
-static void decode_clut( decoder_t *, bsw_t * );
+static void decode_segment( decoder_t *, bs_t * );
+static void decode_page_composition( decoder_t *, bs_t * );
+static void decode_region_composition( decoder_t *, bs_t * );
+static void decode_object( decoder_t *, bs_t * );
+static void decode_display_definition( decoder_t *, bs_t * );
+static void decode_clut( decoder_t *, bs_t * );
 static void free_all( decoder_t * );
 
 static void default_clut_init( decoder_t * );
+static void default_dds_init( decoder_t * );
 
 static subpicture_t *render( decoder_t * );
 
@@ -292,7 +323,6 @@ static int Open( vlc_object_t *p_this )
 {
     decoder_t     *p_dec = (decoder_t *) p_this;
     decoder_sys_t *p_sys;
-    vlc_value_t    val;
     int i_posx, i_posy;
 
     if( p_dec->fmt_in.i_codec != VLC_CODEC_DVBS )
@@ -301,32 +331,25 @@ static int Open( vlc_object_t *p_this )
     }
 
     p_dec->pf_decode_sub = Decode;
-    p_sys = p_dec->p_sys = malloc( sizeof(decoder_sys_t) );
+    p_sys = p_dec->p_sys = calloc( 1, sizeof(decoder_sys_t) );
     if( !p_sys )
         return VLC_ENOMEM;
-    memset( p_sys, 0, sizeof(decoder_sys_t) );
 
-    p_sys->i_pts          = (mtime_t) 0;
+    p_sys->i_pts          = VLC_TS_INVALID;
     p_sys->i_id           = p_dec->fmt_in.subs.dvb.i_id & 0xFFFF;
     p_sys->i_ancillary_id = p_dec->fmt_in.subs.dvb.i_id >> 16;
 
     p_sys->p_regions      = NULL;
     p_sys->p_cluts        = NULL;
     p_sys->p_page         = NULL;
-    p_sys->p_display      = NULL;
-
-    var_Create( p_this, DVBSUB_CFG_PREFIX "position",
-                VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
-    var_Get( p_this, DVBSUB_CFG_PREFIX "position", &val );
-    p_sys->i_spu_position = val.i_int;
-    var_Create( p_this, DVBSUB_CFG_PREFIX "x",
-                VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
-    var_Get( p_this, DVBSUB_CFG_PREFIX "x", &val );
-    i_posx = val.i_int;
-    var_Create( p_this, DVBSUB_CFG_PREFIX "y",
-                VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
-    var_Get( p_this, DVBSUB_CFG_PREFIX "y", &val );
-    i_posy = val.i_int;
+
+    /* configure for SD res in case DDS is not present */
+    default_dds_init( p_dec );
+
+    p_sys->i_spu_position = var_CreateGetInteger( p_this,
+                                    DVBSUB_CFG_PREFIX "position" );
+    i_posx = var_CreateGetInteger( p_this, DVBSUB_CFG_PREFIX "x" );
+    i_posy = var_CreateGetInteger( p_this, DVBSUB_CFG_PREFIX "y" );
 
     /* Check if subpicture position was overridden */
     p_sys->b_absolute = true;
@@ -376,8 +399,13 @@ static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
     p_block = *pp_block;
     *pp_block = NULL;
 
+    /* configure for SD res in case DDS is not present */
+    /* a change of PTS is a good indication we must get a new DDS */
+    if (p_sys->i_pts != p_block->i_pts)
+        default_dds_init( p_dec );
+
     p_sys->i_pts = p_block->i_pts;
-    if( p_sys->i_pts <= 0 )
+    if( p_sys->i_pts <= VLC_TS_INVALID )
     {
 #ifdef DEBUG_DVBSUB
         /* Some DVB channels send stuffing segments in non-dated packets so
@@ -388,16 +416,16 @@ static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
         return NULL;
     }
 
-    bsw_init_writable( &p_sys->bs, p_block->p_buffer, p_block->i_buffer );
+    bs_init( &p_sys->bs, p_block->p_buffer, p_block->i_buffer );
 
-    if( bsw_read( &p_sys->bs, 8 ) != 0x20 ) /* Data identifier */
+    if( bs_read( &p_sys->bs, 8 ) != 0x20 ) /* Data identifier */
     {
         msg_Dbg( p_dec, "invalid data identifier" );
         block_Release( p_block );
         return NULL;
     }
 
-    if( bsw_read( &p_sys->bs, 8 ) ) /* Subtitle stream id */
+    if( bs_read( &p_sys->bs, 8 ) ) /* Subtitle stream id */
     {
         msg_Dbg( p_dec, "invalid subtitle stream id" );
         block_Release( p_block );
@@ -409,12 +437,12 @@ static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
 #endif
 
     p_sys->b_page = false;
-    while( bsw_show( &p_sys->bs, 8 ) == 0x0f ) /* Sync byte */
+    while( bs_show( &p_sys->bs, 8 ) == 0x0f ) /* Sync byte */
     {
         decode_segment( p_dec, &p_sys->bs );
     }
 
-    if( bsw_read( &p_sys->bs, 8 ) != 0xff ) /* End marker */
+    if( bs_read( &p_sys->bs, 8 ) != 0xff ) /* End marker */
     {
         msg_Warn( p_dec, "end marker not found (corrupted subtitle ?)" );
         block_Release( p_block );
@@ -495,7 +523,7 @@ static void default_clut_init( decoder_t *p_dec )
     memset( p_sys->default_clut.c_8b, 0xFF, 256 * sizeof(dvbsub_color_t) );
 }
 
-static void decode_segment( decoder_t *p_dec, bsw_t *s )
+static void decode_segment( decoder_t *p_dec, bs_t *s )
 {
     decoder_sys_t *p_sys = p_dec->p_sys;
     int i_type;
@@ -503,16 +531,16 @@ static void decode_segment( decoder_t *p_dec, bsw_t *s )
     int i_size;
 
     /* sync_byte (already checked) */
-    bsw_skip( s, 8 );
+    bs_skip( s, 8 );
 
     /* segment type */
-    i_type = bsw_read( s, 8 );
+    i_type = bs_read( s, 8 );
 
     /* page id */
-    i_page_id = bsw_read( s, 16 );
+    i_page_id = bs_read( s, 16 );
 
     /* segment size */
-    i_size = bsw_show( s, 16 );
+    i_size = bs_show( s, 16 );
 
     if( ( i_page_id != p_sys->i_id ) &&
         ( i_page_id != p_sys->i_ancillary_id ) )
@@ -521,7 +549,7 @@ static void decode_segment( decoder_t *p_dec, bsw_t *s )
         msg_Dbg( p_dec, "subtitle skipped (page id: %i, %i)",
                  i_page_id, p_sys->i_id );
 #endif
-        bsw_skip( s,  8 * ( 2 + i_size ) );
+        bs_skip( s,  8 * ( 2 + i_size ) );
         return;
     }
 
@@ -532,7 +560,7 @@ static void decode_segment( decoder_t *p_dec, bsw_t *s )
 #ifdef DEBUG_DVBSUB
         msg_Dbg( p_dec, "skipped invalid ancillary subtitle packet" );
 #endif
-        bsw_skip( s,  8 * ( 2 + i_size ) );
+        bs_skip( s,  8 * ( 2 + i_size ) );
         return;
     }
 
@@ -584,24 +612,24 @@ static void decode_segment( decoder_t *p_dec, bsw_t *s )
 #ifdef DEBUG_DVBSUB
         msg_Dbg( p_dec, "end of display" );
 #endif
-        bsw_skip( s,  8 * ( 2 + i_size ) );
+        bs_skip( s,  8 * ( 2 + i_size ) );
         break;
 
     case DVBSUB_ST_STUFFING:
 #ifdef DEBUG_DVBSUB
         msg_Dbg( p_dec, "skip stuffing" );
 #endif
-        bsw_skip( s,  8 * ( 2 + i_size ) );
+        bs_skip( s,  8 * ( 2 + i_size ) );
         break;
 
     default:
         msg_Warn( p_dec, "unsupported segment type: (%04x)", i_type );
-        bsw_skip( s,  8 * ( 2 + i_size ) );
+        bs_skip( s,  8 * ( 2 + i_size ) );
         break;
     }
 }
 
-static void decode_clut( decoder_t *p_dec, bsw_t *s )
+static void decode_clut( decoder_t *p_dec, bs_t *s )
 {
     decoder_sys_t *p_sys = p_dec->p_sys;
     uint16_t      i_segment_length;
@@ -609,9 +637,9 @@ static void decode_clut( decoder_t *p_dec, bsw_t *s )
     dvbsub_clut_t *p_clut, *p_next;
     int           i_id, i_version;
 
-    i_segment_length = bsw_read( s, 16 );
-    i_id             = bsw_read( s, 8 );
-    i_version        = bsw_read( s, 4 );
+    i_segment_length = bs_read( s, 16 );
+    i_id             = bs_read( s, 8 );
+    i_version        = bs_read( s, 4 );
 
     /* Check if we already have this clut */
     for( p_clut = p_sys->p_cluts; p_clut != NULL; p_clut = p_clut->p_next )
@@ -623,7 +651,7 @@ static void decode_clut( decoder_t *p_dec, bsw_t *s )
     if( p_clut && ( p_clut->i_version == i_version ) )
     {
         /* Nothing to do */
-        bsw_skip( s, 8 * i_segment_length - 12 );
+        bs_skip( s, 8 * i_segment_length - 12 );
         return;
     }
 
@@ -647,7 +675,7 @@ static void decode_clut( decoder_t *p_dec, bsw_t *s )
     /* We don't have this version of the CLUT: Parse it */
     p_clut->i_version = i_version;
     p_clut->i_id = i_id;
-    bsw_skip( s, 4 ); /* Reserved bits */
+    bs_skip( s, 4 ); /* Reserved bits */
     i_processed_length = 2;
     while( i_processed_length < i_segment_length )
     {
@@ -655,25 +683,25 @@ static void decode_clut( decoder_t *p_dec, bsw_t *s )
         uint8_t i_id;
         uint8_t i_type;
 
-        i_id = bsw_read( s, 8 );
-        i_type = bsw_read( s, 3 );
+        i_id = bs_read( s, 8 );
+        i_type = bs_read( s, 3 );
 
-        bsw_skip( s, 4 );
+        bs_skip( s, 4 );
 
-        if( bsw_read( s, 1 ) )
+        if( bs_read( s, 1 ) )
         {
-            y  = bsw_read( s, 8 );
-            cr = bsw_read( s, 8 );
-            cb = bsw_read( s, 8 );
-            t  = bsw_read( s, 8 );
+            y  = bs_read( s, 8 );
+            cr = bs_read( s, 8 );
+            cb = bs_read( s, 8 );
+            t  = bs_read( s, 8 );
             i_processed_length += 6;
         }
         else
         {
-            y  = bsw_read( s, 6 ) << 2;
-            cr = bsw_read( s, 4 ) << 4;
-            cb = bsw_read( s, 4 ) << 4;
-            t  = bsw_read( s, 2 ) << 6;
+            y  = bs_read( s, 6 ) << 2;
+            cr = bs_read( s, 4 ) << 4;
+            cb = bs_read( s, 4 ) << 4;
+            t  = bs_read( s, 2 ) << 6;
             i_processed_length += 4;
         }
 
@@ -713,17 +741,17 @@ static void decode_clut( decoder_t *p_dec, bsw_t *s )
     }
 }
 
-static void decode_page_composition( decoder_t *p_dec, bsw_t *s )
+static void decode_page_composition( decoder_t *p_dec, bs_t *s )
 {
     decoder_sys_t *p_sys = p_dec->p_sys;
     int i_version, i_state, i_segment_length, i_timeout, i;
 
     /* A page is composed by 0 or more region */
-    i_segment_length = bsw_read( s, 16 );
-    i_timeout = bsw_read( s, 8 );
-    i_version = bsw_read( s, 4 );
-    i_state = bsw_read( s, 2 );
-    bsw_skip( s, 2 ); /* Reserved */
+    i_segment_length = bs_read( s, 16 );
+    i_timeout = bs_read( s, 8 );
+    i_version = bs_read( s, 4 );
+    i_state = bs_read( s, 2 );
+    bs_skip( s, 2 ); /* Reserved */
 
     if( i_state == DVBSUB_PCS_STATE_CHANGE )
     {
@@ -741,7 +769,7 @@ static void decode_page_composition( decoder_t *p_dec, bsw_t *s )
 
 #if 0
         /* Try to start decoding even without an acquisition page */
-        bsw_skip( s,  8 * (i_segment_length - 2) );
+        bs_skip( s,  8 * (i_segment_length - 2) );
         return;
 #endif
     }
@@ -754,7 +782,7 @@ static void decode_page_composition( decoder_t *p_dec, bsw_t *s )
     /* Check version number */
     if( p_sys->p_page && ( p_sys->p_page->i_version == i_version ) )
     {
-        bsw_skip( s,  8 * (i_segment_length - 2) );
+        bs_skip( s,  8 * (i_segment_length - 2) );
         return;
     }
     else if( p_sys->p_page )
@@ -791,10 +819,10 @@ static void decode_page_composition( decoder_t *p_dec, bsw_t *s )
     {
         for( i = 0; i < p_sys->p_page->i_region_defs; i++ )
         {
-            p_sys->p_page->p_region_defs[i].i_id = bsw_read( s, 8 );
-            bsw_skip( s, 8 ); /* Reserved */
-            p_sys->p_page->p_region_defs[i].i_x = bsw_read( s, 16 );
-            p_sys->p_page->p_region_defs[i].i_y = bsw_read( s, 16 );
+            p_sys->p_page->p_region_defs[i].i_id = bs_read( s, 8 );
+            bs_skip( s, 8 ); /* Reserved */
+            p_sys->p_page->p_region_defs[i].i_x = bs_read( s, 16 );
+            p_sys->p_page->p_region_defs[i].i_y = bs_read( s, 16 );
 
 #ifdef DEBUG_DVBSUB
             msg_Dbg( p_dec, "page_composition, region %i (%i,%i)",
@@ -805,7 +833,7 @@ static void decode_page_composition( decoder_t *p_dec, bsw_t *s )
     }
 }
 
-static void decode_region_composition( decoder_t *p_dec, bsw_t *s )
+static void decode_region_composition( decoder_t *p_dec, bs_t *s )
 {
     decoder_sys_t *p_sys = p_dec->p_sys;
     dvbsub_region_t *p_region, **pp_region = &p_sys->p_regions;
@@ -814,9 +842,9 @@ static void decode_region_composition( decoder_t *p_dec, bsw_t *s )
     int i_8_bg, i_4_bg, i_2_bg;
     bool b_fill;
 
-    i_segment_length = bsw_read( s, 16 );
-    i_id = bsw_read( s, 8 );
-    i_version = bsw_read( s, 4 );
+    i_segment_length = bs_read( s, 16 );
+    i_id = bs_read( s, 8 );
+    i_version = bs_read( s, 4 );
 
     /* Check if we already have this region */
     for( p_region = p_sys->p_regions; p_region != NULL;
@@ -829,7 +857,7 @@ static void decode_region_composition( decoder_t *p_dec, bsw_t *s )
     /* Check version number */
     if( p_region && ( p_region->i_version == i_version ) )
     {
-        bsw_skip( s, 8 * (i_segment_length - 1) - 4 );
+        bs_skip( s, 8 * (i_segment_length - 1) - 4 );
         return;
     }
 
@@ -838,10 +866,9 @@ static void decode_region_composition( decoder_t *p_dec, bsw_t *s )
 #ifdef DEBUG_DVBSUB
         msg_Dbg( p_dec, "new region: %i", i_id );
 #endif
-        p_region = *pp_region = malloc( sizeof(dvbsub_region_t) );
+        p_region = *pp_region = calloc( 1, sizeof(dvbsub_region_t) );
         if( !p_region )
             return;
-        memset( p_region, 0, sizeof(dvbsub_region_t) );
         p_region->p_object_defs = NULL;
         p_region->p_pixbuf = NULL;
         p_region->p_next = NULL;
@@ -850,23 +877,23 @@ static void decode_region_composition( decoder_t *p_dec, bsw_t *s )
     /* Region attributes */
     p_region->i_id = i_id;
     p_region->i_version = i_version;
-    b_fill = bsw_read( s, 1 );
-    bsw_skip( s, 3 ); /* Reserved */
+    b_fill = bs_read( s, 1 );
+    bs_skip( s, 3 ); /* Reserved */
 
-    i_width = bsw_read( s, 16 );
-    i_height = bsw_read( s, 16 );
+    i_width = bs_read( s, 16 );
+    i_height = bs_read( s, 16 );
 #ifdef DEBUG_DVBSUB
     msg_Dbg( p_dec, " width=%d height=%d", i_width, i_height );
 #endif
-    i_level_comp = bsw_read( s, 3 );
-    i_depth = bsw_read( s, 3 );
-    bsw_skip( s, 2 ); /* Reserved */
-    i_clut = bsw_read( s, 8 );
+    i_level_comp = bs_read( s, 3 );
+    i_depth = bs_read( s, 3 );
+    bs_skip( s, 2 ); /* Reserved */
+    i_clut = bs_read( s, 8 );
 
-    i_8_bg = bsw_read( s, 8 );
-    i_4_bg = bsw_read( s, 4 );
-    i_2_bg = bsw_read( s, 2 );
-    bsw_skip( s, 2 ); /* Reserved */
+    i_8_bg = bs_read( s, 8 );
+    i_4_bg = bs_read( s, 4 );
+    i_2_bg = bs_read( s, 2 );
+    bs_skip( s, 2 ); /* Reserved */
 
     /* Free old object defs */
     while( p_region->i_object_defs )
@@ -886,7 +913,7 @@ static void decode_region_composition( decoder_t *p_dec, bsw_t *s )
             free( p_region->p_pixbuf );
         }
 
-        p_region->p_pixbuf = malloc( i_height * i_width );
+        p_region->p_pixbuf = xmalloc( i_height * i_width );
         p_region->i_depth = 0;
         b_fill = true;
     }
@@ -920,18 +947,17 @@ static void decode_region_composition( decoder_t *p_dec, bsw_t *s )
 
         /* We create a new object */
         p_region->i_object_defs++;
-        p_region->p_object_defs =
-            realloc( p_region->p_object_defs,
+        p_region->p_object_defs = xrealloc( p_region->p_object_defs,
                      sizeof(dvbsub_objectdef_t) * p_region->i_object_defs );
 
         /* We parse object properties */
         p_obj = &p_region->p_object_defs[p_region->i_object_defs - 1];
-        p_obj->i_id         = bsw_read( s, 16 );
-        p_obj->i_type       = bsw_read( s, 2 );
-        bsw_skip( s, 2 ); /* Provider */
-        p_obj->i_x          = bsw_read( s, 12 );
-        bsw_skip( s, 4 ); /* Reserved */
-        p_obj->i_y          = bsw_read( s, 12 );
+        p_obj->i_id         = bs_read( s, 16 );
+        p_obj->i_type       = bs_read( s, 2 );
+        bs_skip( s, 2 ); /* Provider */
+        p_obj->i_x          = bs_read( s, 12 );
+        bs_skip( s, 4 ); /* Reserved */
+        p_obj->i_y          = bs_read( s, 12 );
         p_obj->psz_text     = NULL;
 
         i_processed_length += 6;
@@ -939,86 +965,87 @@ static void decode_region_composition( decoder_t *p_dec, bsw_t *s )
         if( ( p_obj->i_type == DVBSUB_OT_BASIC_CHAR ) ||
             ( p_obj->i_type == DVBSUB_OT_COMPOSITE_STRING ) )
         {
-            p_obj->i_fg_pc =  bsw_read( s, 8 );
-            p_obj->i_bg_pc =  bsw_read( s, 8 );
+            p_obj->i_fg_pc =  bs_read( s, 8 );
+            p_obj->i_bg_pc =  bs_read( s, 8 );
             i_processed_length += 2;
         }
     }
 }
 
 /* ETSI 300 743 [7.2.1] */
-static void decode_display_definition( decoder_t *p_dec, bsw_t *s )
+static void decode_display_definition( decoder_t *p_dec, bs_t *s )
 {
     decoder_sys_t *p_sys = p_dec->p_sys;
     uint16_t      i_segment_length;
     uint16_t      i_processed_length = 40;
-    dvbsub_display_t *p_display;
-    dvbsub_display_t *p_old = p_sys->p_display;
     int           i_version;
 
-    i_segment_length = bsw_read( s, 16 );
-    i_version        = bsw_read( s, 4 );
+    i_segment_length = bs_read( s, 16 );
+    i_version        = bs_read( s, 4 );
 
     /* Check version number */
-    if( p_old && ( p_old->i_version == i_version ) )
+    if( p_sys->display.i_version == i_version )
     {
         /* The definition did not change */
-        bsw_skip( s, 8*i_segment_length - 4 );
+        bs_skip( s, 8*i_segment_length - 4 );
         return;
     }
 
 #ifdef DEBUG_DVBSUB
     msg_Dbg( p_dec, "new display definition: %i", i_version );
 #endif
-    p_display = malloc( sizeof(dvbsub_display_t) );
-    if( p_display )
-    {
-        /* We don't have this version of the display definition: Parse it */
-        p_display->i_version = i_version;
-        p_display->b_windowed = bsw_read( s, 1 );
-        bsw_skip( s, 3 ); /* Reserved bits */
-        p_display->i_width = bsw_read( s, 16 )+1;
-        p_display->i_height = bsw_read( s, 16 )+1;
 
-        if( p_display->b_windowed )
-        {
+    /* We don't have this version of the display definition: Parse it */
+    p_sys->display.i_version = i_version;
+    p_sys->display.b_windowed = bs_read( s, 1 );
+    bs_skip( s, 3 ); /* Reserved bits */
+    p_sys->display.i_width = bs_read( s, 16 )+1;
+    p_sys->display.i_height = bs_read( s, 16 )+1;
+
+    if( p_sys->display.b_windowed )
+    {
 #ifdef DEBUG_DVBSUB
-            msg_Dbg( p_dec, "display definition with offsets (windowed)" );
+        msg_Dbg( p_dec, "display definition with offsets (windowed)" );
 #endif
-            /* Coordinates are measured from the top left corner */
-            p_display->i_x     = bsw_read( s, 16 );
-            p_display->i_max_x = bsw_read( s, 16 );
-            p_display->i_y     = bsw_read( s, 16 );
-            p_display->i_max_y = bsw_read( s, 16 );
-            i_processed_length += 64;
-        }
+        /* Coordinates are measured from the top left corner */
+        p_sys->display.i_x     = bs_read( s, 16 );
+        p_sys->display.i_max_x = bs_read( s, 16 );
+        p_sys->display.i_y     = bs_read( s, 16 );
+        p_sys->display.i_max_y = bs_read( s, 16 );
+        i_processed_length += 64;
+    }
+    else
+    {
+        /* if not windowed, setup the window variables to good defaults */
+        /* not necessary, but to avoid future confusion.. */
+        p_sys->display.i_x     = 0;
+        p_sys->display.i_max_x = p_sys->display.i_width-1;
+        p_sys->display.i_y     = 0;
+        p_sys->display.i_max_y = p_sys->display.i_height-1;
     }
-
-    p_sys->p_display = p_display;
-    free( p_old );
 
     if( i_processed_length != i_segment_length*8 )
     {
-        msg_Err( p_dec, "processed length %d != segment length %d",
-                 i_processed_length, i_segment_length );
+        msg_Err( p_dec, "processed length %d bytes != segment length %d bytes",
+                 i_processed_length / 8 , i_segment_length );
     }
 
 #ifdef DEBUG_DVBSUB
     msg_Dbg( p_dec, "version: %d, width: %d, height: %d",
-             p_display->i_version, p_display->i_width, p_display->i_height );
-    if( p_display->b_windowed )
+             p_sys->display.i_version, p_sys->display.i_width, p_sys->display.i_height );
+    if( p_sys->display.b_windowed )
         msg_Dbg( p_dec, "xmin: %d, xmax: %d, ymin: %d, ymax: %d",
-                 p_display->i_x, p_display->i_max_x, p_display->i_y, p_display->i_max_y );
+                 p_sys->display.i_x, p_sys->display.i_max_x, p_sys->display.i_y, p_sys->display.i_max_y );
 #endif
 }
 
 static void dvbsub_render_pdata( decoder_t *, dvbsub_region_t *, int, int,
                                  uint8_t *, int );
-static void dvbsub_pdata2bpp( bsw_t *, uint8_t *, int, int * );
-static void dvbsub_pdata4bpp( bsw_t *, uint8_t *, int, int * );
-static void dvbsub_pdata8bpp( bsw_t *, uint8_t *, int, int * );
+static void dvbsub_pdata2bpp( bs_t *, uint8_t *, int, int * );
+static void dvbsub_pdata4bpp( bs_t *, uint8_t *, int, int * );
+static void dvbsub_pdata8bpp( bs_t *, uint8_t *, int, int * );
 
-static void decode_object( decoder_t *p_dec, bsw_t *s )
+static void decode_object( decoder_t *p_dec, bs_t *s )
 {
     decoder_sys_t *p_sys = p_dec->p_sys;
     dvbsub_region_t *p_region;
@@ -1028,15 +1055,15 @@ static void decode_object( decoder_t *p_dec, bsw_t *s )
     /* ETSI 300-743 paragraph 7.2.4
      * sync_byte, segment_type and page_id have already been processed.
      */
-    i_segment_length = bsw_read( s, 16 );
-    i_id             = bsw_read( s, 16 );
-    i_version        = bsw_read( s, 4 );
-    i_coding_method  = bsw_read( s, 2 );
+    i_segment_length = bs_read( s, 16 );
+    i_id             = bs_read( s, 16 );
+    i_version        = bs_read( s, 4 );
+    i_coding_method  = bs_read( s, 2 );
 
     if( i_coding_method > 1 )
     {
         msg_Dbg( p_dec, "unknown DVB subtitling coding %d is not handled!", i_coding_method );
-        bsw_skip( s, 8 * (i_segment_length - 2) - 6 );
+        bs_skip( s, 8 * (i_segment_length - 2) - 6 );
         return;
     }
 
@@ -1052,7 +1079,7 @@ static void decode_object( decoder_t *p_dec, bsw_t *s )
     }
     if( !p_region )
     {
-        bsw_skip( s, 8 * (i_segment_length - 2) - 6 );
+        bs_skip( s, 8 * (i_segment_length - 2) - 6 );
         return;
     }
 
@@ -1060,20 +1087,20 @@ static void decode_object( decoder_t *p_dec, bsw_t *s )
     msg_Dbg( p_dec, "new object: %i", i_id );
 #endif
 
-    b_non_modify_color = bsw_read( s, 1 );
-    bsw_skip( s, 1 ); /* Reserved */
+    b_non_modify_color = bs_read( s, 1 );
+    bs_skip( s, 1 ); /* Reserved */
 
     if( i_coding_method == 0x00 )
     {
         int i_topfield, i_bottomfield;
         uint8_t *p_topfield, *p_bottomfield;
 
-        i_topfield    = bsw_read( s, 16 );
-        i_bottomfield = bsw_read( s, 16 );
-        p_topfield    = s->p_start + bsw_pos( s ) / 8;
+        i_topfield    = bs_read( s, 16 );
+        i_bottomfield = bs_read( s, 16 );
+        p_topfield    = s->p_start + bs_pos( s ) / 8;
         p_bottomfield = p_topfield + i_topfield;
 
-        bsw_skip( s, 8 * (i_segment_length - 7) );
+        bs_skip( s, 8 * (i_segment_length - 7) );
 
         /* Sanity check */
         if( ( i_segment_length < ( i_topfield + i_bottomfield + 7 ) ) ||
@@ -1116,8 +1143,8 @@ static void decode_object( decoder_t *p_dec, bsw_t *s )
     else
     {
         /* DVB subtitling as characters */
-        int i_number_of_codes = bsw_read( s, 8 );
-        uint8_t* p_start = s->p_start + bsw_pos( s ) / 8;
+        int i_number_of_codes = bs_read( s, 8 );
+        uint8_t* p_start = s->p_start + bs_pos( s ) / 8;
 
         /* Sanity check */
         if( ( i_segment_length < ( i_number_of_codes*2 + 4 ) ) ||
@@ -1137,13 +1164,13 @@ static void decode_object( decoder_t *p_dec, bsw_t *s )
                 if( p_region->p_object_defs[i].i_id != i_id ) continue;
 
                 p_region->p_object_defs[i].psz_text =
-                    realloc( p_region->p_object_defs[i].psz_text,
+                    xrealloc( p_region->p_object_defs[i].psz_text,
                              i_number_of_codes + 1 );
 
                 /* FIXME 16bits -> char ??? See Preamble */
                 for( j = 0; j < i_number_of_codes; j++ )
                 {
-                    p_region->p_object_defs[i].psz_text[j] = (char)(bsw_read( s, 16 ) & 0xFF);
+                    p_region->p_object_defs[i].psz_text[j] = (char)(bs_read( s, 16 ) & 0xFF);
                 }
                 /* Null terminate the string */
                 p_region->p_object_defs[i].psz_text[j] = 0;
@@ -1162,7 +1189,7 @@ static void dvbsub_render_pdata( decoder_t *p_dec, dvbsub_region_t *p_region,
 {
     uint8_t *p_pixbuf;
     int i_offset = 0;
-    bsw_t bs;
+    bs_t bs;
 
     /* Sanity check */
     if( !p_region->p_pixbuf )
@@ -1178,14 +1205,14 @@ static void dvbsub_render_pdata( decoder_t *p_dec, dvbsub_region_t *p_region,
     }
 
     p_pixbuf = p_region->p_pixbuf + i_y * p_region->i_width;
-    bsw_init_writable( &bs, p_field, i_field );
+    bs_init( &bs, p_field, i_field );
 
-    while( !bsw_eof( &bs ) )
+    while( !bs_eof( &bs ) )
     {
         /* Sanity check */
         if( i_y >= p_region->i_height ) return;
 
-        switch( bsw_read( &bs, 8 ) )
+        switch( bs_read( &bs, 8 ) )
         {
         case 0x10:
             dvbsub_pdata2bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
@@ -1216,31 +1243,31 @@ static void dvbsub_render_pdata( decoder_t *p_dec, dvbsub_region_t *p_region,
     }
 }
 
-static void dvbsub_pdata2bpp( bsw_t *s, uint8_t *p, int i_width, int *pi_off )
+static void dvbsub_pdata2bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
 {
     bool b_stop = false;
 
-    while( !b_stop && !bsw_eof( s ) )
+    while( !b_stop && !bs_eof( s ) )
     {
         int i_count = 0, i_color = 0;
 
-        i_color = bsw_read( s, 2 );
+        i_color = bs_read( s, 2 );
         if( i_color != 0x00 )
         {
             i_count = 1;
         }
         else
         {
-            if( bsw_read( s, 1 ) == 0x01 )         // Switch1
+            if( bs_read( s, 1 ) == 0x01 )         // Switch1
             {
-                i_count = 3 + bsw_read( s, 3 );
-                i_color = bsw_read( s, 2 );
+                i_count = 3 + bs_read( s, 3 );
+                i_color = bs_read( s, 2 );
             }
             else
             {
-                if( bsw_read( s, 1 ) == 0x00 )     //Switch2
+                if( bs_read( s, 1 ) == 0x00 )     //Switch2
                 {
-                    switch( bsw_read( s, 2 ) )     //Switch3
+                    switch( bs_read( s, 2 ) )     //Switch3
                     {
                     case 0x00:
                         b_stop = true;
@@ -1249,12 +1276,12 @@ static void dvbsub_pdata2bpp( bsw_t *s, uint8_t *p, int i_width, int *pi_off )
                         i_count = 2;
                         break;
                     case 0x02:
-                        i_count =  12 + bsw_read( s, 4 );
-                        i_color = bsw_read( s, 2 );
+                        i_count =  12 + bs_read( s, 4 );
+                        i_color = bs_read( s, 2 );
                         break;
                     case 0x03:
-                        i_count =  29 + bsw_read( s, 8 );
-                        i_color = bsw_read( s, 2 );
+                        i_count =  29 + bs_read( s, 8 );
+                        i_color = bs_read( s, 2 );
                         break;
                     default:
                         break;
@@ -1279,18 +1306,18 @@ static void dvbsub_pdata2bpp( bsw_t *s, uint8_t *p, int i_width, int *pi_off )
         (*pi_off) += i_count;
     }
 
-    bsw_align( s );
+    bs_align( s );
 }
 
-static void dvbsub_pdata4bpp( bsw_t *s, uint8_t *p, int i_width, int *pi_off )
+static void dvbsub_pdata4bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
 {
     bool b_stop = false;
 
-    while( !b_stop && !bsw_eof( s ) )
+    while( !b_stop && !bs_eof( s ) )
     {
         int i_count = 0, i_color = 0;
 
-        i_color = bsw_read( s, 4 );
+        i_color = bs_read( s, 4 );
         if( i_color != 0x00 )
         {
             /* Add 1 pixel */
@@ -1298,28 +1325,28 @@ static void dvbsub_pdata4bpp( bsw_t *s, uint8_t *p, int i_width, int *pi_off )
         }
         else
         {
-            if( bsw_read( s, 1 ) == 0x00 )           // Switch1
+            if( bs_read( s, 1 ) == 0x00 )           // Switch1
             {
-                if( bsw_show( s, 3 ) != 0x00 )
+                if( bs_show( s, 3 ) != 0x00 )
                 {
-                    i_count = 2 + bsw_read( s, 3 );
+                    i_count = 2 + bs_read( s, 3 );
                 }
                 else
                 {
-                    bsw_skip( s, 3 );
+                    bs_skip( s, 3 );
                     b_stop = true;
                 }
             }
             else
             {
-                if( bsw_read( s, 1 ) == 0x00)        //Switch2
+                if( bs_read( s, 1 ) == 0x00)        //Switch2
                 {
-                    i_count =  4 + bsw_read( s, 2 );
-                    i_color = bsw_read( s, 4 );
+                    i_count =  4 + bs_read( s, 2 );
+                    i_color = bs_read( s, 4 );
                 }
                 else
                 {
-                    switch ( bsw_read( s, 2 ) )     //Switch3
+                    switch ( bs_read( s, 2 ) )     //Switch3
                     {
                     case 0x0:
                         i_count = 1;
@@ -1328,12 +1355,12 @@ static void dvbsub_pdata4bpp( bsw_t *s, uint8_t *p, int i_width, int *pi_off )
                         i_count = 2;
                         break;
                     case 0x2:
-                        i_count = 9 + bsw_read( s, 4 );
-                        i_color = bsw_read( s, 4 );
+                        i_count = 9 + bs_read( s, 4 );
+                        i_color = bs_read( s, 4 );
                         break;
                     case 0x3:
-                        i_count= 25 + bsw_read( s, 8 );
-                        i_color = bsw_read( s, 4 );
+                        i_count= 25 + bs_read( s, 8 );
+                        i_color = bs_read( s, 4 );
                         break;
                     }
                 }
@@ -1351,18 +1378,18 @@ static void dvbsub_pdata4bpp( bsw_t *s, uint8_t *p, int i_width, int *pi_off )
         (*pi_off) += i_count;
     }
 
-    bsw_align( s );
+    bs_align( s );
 }
 
-static void dvbsub_pdata8bpp( bsw_t *s, uint8_t *p, int i_width, int *pi_off )
+static void dvbsub_pdata8bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
 {
     bool b_stop = false;
 
-    while( !b_stop && !bsw_eof( s ) )
+    while( !b_stop && !bs_eof( s ) )
     {
         int i_count = 0, i_color = 0;
 
-        i_color = bsw_read( s, 8 );
+        i_color = bs_read( s, 8 );
         if( i_color != 0x00 )
         {
             /* Add 1 pixel */
@@ -1370,22 +1397,22 @@ static void dvbsub_pdata8bpp( bsw_t *s, uint8_t *p, int i_width, int *pi_off )
         }
         else
         {
-            if( bsw_read( s, 1 ) == 0x00 )           // Switch1
+            if( bs_read( s, 1 ) == 0x00 )           // Switch1
             {
-                if( bsw_show( s, 7 ) != 0x00 )
+                if( bs_show( s, 7 ) != 0x00 )
                 {
-                    i_count = bsw_read( s, 7 );
+                    i_count = bs_read( s, 7 );
                 }
                 else
                 {
-                    bsw_skip( s, 7 );
+                    bs_skip( s, 7 );
                     b_stop = true;
                 }
             }
             else
             {
-                i_count = bsw_read( s, 7 );
-                i_color = bsw_read( s, 8 );
+                i_count = bs_read( s, 7 );
+                i_color = bs_read( s, 8 );
             }
         }
 
@@ -1400,7 +1427,7 @@ static void dvbsub_pdata8bpp( bsw_t *s, uint8_t *p, int i_width, int *pi_off )
         (*pi_off) += i_count;
     }
 
-    bsw_align( s );
+    bs_align( s );
 }
 
 static void free_all( decoder_t *p_dec )
@@ -1409,7 +1436,7 @@ static void free_all( decoder_t *p_dec )
     dvbsub_region_t *p_reg, *p_reg_next;
     dvbsub_clut_t *p_clut, *p_clut_next;
 
-    free( p_sys->p_display );
+    /*free( p_sys->p_display ); No longer malloced */
 
     for( p_clut = p_sys->p_cluts; p_clut != NULL; p_clut = p_clut_next )
     {
@@ -1450,17 +1477,18 @@ static subpicture_t *render( decoder_t *p_dec )
     int i_base_y;
 
     /* Allocate the subpicture internal data. */
-    p_spu = decoder_NewSubpicture( p_dec );
+    p_spu = decoder_NewSubpicture( p_dec, NULL );
     if( !p_spu )
         return NULL;
 
     p_spu->b_absolute = p_sys->b_absolute;
     /* Set the pf_render callback */
-    p_spu->i_start = (mtime_t) p_sys->i_pts;
+    p_spu->i_start = p_sys->i_pts;
     //p_spu->i_stop = (mtime_t) 0;
     p_spu->b_ephemer = true;
     //p_spu->b_fade = true;
     //p_spu->i_stop = p_spu->i_start + (mtime_t) (i_timeout * 1000000);
+    p_spu->b_subtitle = true;
 
     /* Correct positioning of SPU */
     i_base_x = p_sys->i_spu_x;
@@ -1468,19 +1496,21 @@ static subpicture_t *render( decoder_t *p_dec )
     p_spu->i_original_picture_width = 720;
     p_spu->i_original_picture_height = 576;
 
-    if( p_sys->p_display )
+    p_spu->i_original_picture_width = p_sys->display.i_width;
+    p_spu->i_original_picture_height = p_sys->display.i_height;
+
+    if( p_sys->display.b_windowed )
     {
-        p_spu->i_original_picture_width = p_sys->p_display->i_width;
-        p_spu->i_original_picture_height = p_sys->p_display->i_height;
+        /* From en_300743v01 - */
+        /* the DDS is there to indicate intended size/position of text */
+        /* the intended video area is ->i_width/height */
+        /* the window is within this... SO... we should leave i_original_picture_width etc. as is */
+        /* and ONLY change i_base_x.  effectively i_max_x/y are only there to limit memory requirements*/
+        /* we COULD use the upper limits to limit rendering to within these? */
 
-        if( p_sys->p_display->b_windowed )
-        {
-            /* TODO: check that this actually works */
-            p_spu->i_original_picture_width = p_sys->p_display->i_max_x - p_sys->p_display->i_x;
-            p_spu->i_original_picture_height = p_sys->p_display->i_max_y - p_sys->p_display->i_y;
-            i_base_x += p_sys->p_display->i_x;
-            i_base_y += p_sys->p_display->i_y;
-        }
+        /* see notes on DDS at the top of the file */
+        i_base_x += p_sys->display.i_x;
+        i_base_y += p_sys->display.i_y;
     }
 
     pp_spu_region = &p_spu->p_region;
@@ -1507,11 +1537,6 @@ static subpicture_t *render( decoder_t *p_dec )
 
         p_regiondef = &p_sys->p_page->p_region_defs[i];
 
-#ifdef DEBUG_DVBSUB
-        msg_Dbg( p_dec, "rendering region %i (%i,%i)", i,
-                 p_regiondef->i_x, p_regiondef->i_y );
-#endif
-
         /* Find associated region */
         for( p_region = p_sys->p_regions; p_region != NULL;
              p_region = p_region->p_next )
@@ -1519,6 +1544,22 @@ static subpicture_t *render( decoder_t *p_dec )
             if( p_regiondef->i_id == p_region->i_id ) break;
         }
 
+#ifdef DEBUG_DVBSUB
+        /* if a region exists, then print it's size */
+        if (p_region)
+        {
+                msg_Dbg( p_dec, "rendering region %i (%i,%i) to (%i,%i)", i,
+                        p_regiondef->i_x, p_regiondef->i_y,
+                p_regiondef->i_x + p_region->i_width,
+                p_regiondef->i_y + p_region->i_height );
+        }
+        else
+        {
+                msg_Dbg( p_dec, "rendering region %i (%i,%i) (no region matched to render)", i,
+                      p_regiondef->i_x, p_regiondef->i_y );
+        }
+#endif
+
         if( !p_region )
         {
             msg_Dbg( p_dec, "region %i not found", p_regiondef->i_id );
@@ -1536,13 +1577,14 @@ static subpicture_t *render( decoder_t *p_dec )
             continue;
         }
 
-        /* FIXME: don't create a subpicture region with VLC CODEC YUVP 
+        /* FIXME: don't create a subpicture region with VLC CODEC YUVP
          * when it actually is a TEXT region */
 
         /* Create new SPU region */
         memset( &fmt, 0, sizeof(video_format_t) );
         fmt.i_chroma = VLC_CODEC_YUVP;
-        fmt.i_aspect = 0; /* 0 means use aspect ratio of background video */
+        fmt.i_sar_num = 0; /* 0 means use aspect ratio of background video */
+        fmt.i_sar_den = 1;
         fmt.i_width = fmt.i_visible_width = p_region->i_width;
         fmt.i_height = fmt.i_visible_height = p_region->i_height;
         fmt.i_x_offset = fmt.i_y_offset = 0;
@@ -1595,16 +1637,12 @@ static subpicture_t *render( decoder_t *p_dec )
             /* Create new SPU region */
             memset( &fmt, 0, sizeof(video_format_t) );
             fmt.i_chroma = VLC_CODEC_TEXT;
-            fmt.i_aspect = VOUT_ASPECT_FACTOR;
+            fmt.i_sar_num = 1;
+            fmt.i_sar_den = 1;
             fmt.i_width = fmt.i_visible_width = p_region->i_width;
             fmt.i_height = fmt.i_visible_height = p_region->i_height;
             fmt.i_x_offset = fmt.i_y_offset = 0;
             p_spu_region = subpicture_region_New( &fmt );
-            if( !p_region )
-            {
-                msg_Err( p_dec, "cannot allocate SPU region" );
-                continue;
-            }
 
             p_spu_region->psz_text = strdup( p_object_def->psz_text );
             p_spu_region->i_x = i_base_x + p_regiondef->i_x + p_object_def->i_x;
@@ -1644,10 +1682,10 @@ struct encoder_sys_t
     int i_offset_y;
 };
 
-static void encode_page_composition( encoder_t *, bsw_t *, subpicture_t * );
-static void encode_clut( encoder_t *, bsw_t *, subpicture_t * );
-static void encode_region_composition( encoder_t *, bsw_t *, subpicture_t * );
-static void encode_object( encoder_t *, bsw_t *, subpicture_t * );
+static void encode_page_composition( encoder_t *, bs_t *, subpicture_t * );
+static void encode_clut( encoder_t *, bs_t *, subpicture_t * );
+static void encode_region_composition( encoder_t *, bs_t *, subpicture_t * );
+static void encode_object( encoder_t *, bs_t *, subpicture_t * );
 
 /*****************************************************************************
  * OpenEncoder: probe the encoder and return score
@@ -1656,7 +1694,6 @@ static int OpenEncoder( vlc_object_t *p_this )
 {
     encoder_t *p_enc = (encoder_t *)p_this;
     encoder_sys_t *p_sys;
-    vlc_value_t val;
 
     if( ( p_enc->fmt_out.i_codec != VLC_CODEC_DVBS ) &&
         !p_enc->b_force )
@@ -1681,12 +1718,8 @@ static int OpenEncoder( vlc_object_t *p_this )
     p_sys->i_regions = 0;
     p_sys->p_regions = 0;
 
-    var_Create( p_this, ENC_CFG_PREFIX "x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
-    var_Get( p_this, ENC_CFG_PREFIX "x", &val );
-    p_sys->i_offset_x = val.i_int;
-    var_Create( p_this, ENC_CFG_PREFIX "y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
-    var_Get( p_this, ENC_CFG_PREFIX "y", &val );
-    p_sys->i_offset_y = val.i_int;
+    p_sys->i_offset_x = var_CreateGetInteger( p_this, ENC_CFG_PREFIX "x" );
+    p_sys->i_offset_y = var_CreateGetInteger( p_this, ENC_CFG_PREFIX "y" );
 
     return VLC_SUCCESS;
 }
@@ -1718,7 +1751,8 @@ static subpicture_t *YuvaYuvp( subpicture_t *p_subpic )
                         + p_region->p_picture->p[0].i_pitch * 1 / 3;
         int i_tolerance = 0;
 
-#ifdef DEBUG_DVBSUB
+#ifdef DEBUG_DVBSUB1
+        /* p_enc not valid here */
         msg_Dbg( p_enc, "YuvaYuvp: i_pixels=%d, i_iterator=%d", i_pixels, i_iterator );
 #endif
         p_fmt->i_chroma = VLC_CODEC_YUVP;
@@ -1795,12 +1829,13 @@ static subpicture_t *YuvaYuvp( subpicture_t *p_subpic )
             }
         }
 
-#ifdef DEBUG_DVBSUB
+#ifdef DEBUG_DVBSUB1
+        /* p_enc not valid here */
         msg_Dbg( p_enc, "best palette has %d colors", p_fmt->p_palette->i_entries );
 #endif
 
 #ifndef RANDOM_DITHERING
-        pi_delta = malloc( ( p_region->p_picture->p[0].i_pitch + 1 )
+        pi_delta = xmalloc( ( p_region->p_picture->p[0].i_pitch + 1 )
                             * sizeof(int) * 4  );
         for( i = 0; i < (p_region->p_picture->p[0].i_pitch + 1) * 4 ; i++ )
         {
@@ -1889,7 +1924,8 @@ static subpicture_t *YuvaYuvp( subpicture_t *p_subpic )
             p_fmt->p_palette->palette[i][3] = 0;
         }
         p_fmt->p_palette->i_entries = i_max_entries;
-#ifdef DEBUG_DVBSUB
+#ifdef DEBUG_DVBSUB1
+        /* p_enc not valid here */
         msg_Dbg( p_enc, "best palette has %d colors", p_fmt->p_palette->i_entries );
 #endif
     }
@@ -1903,7 +1939,7 @@ static block_t *Encode( encoder_t *p_enc, subpicture_t *p_subpic )
 {
     subpicture_t *p_temp = NULL;
     subpicture_region_t *p_region = NULL;
-    bsw_t bits, *s = &bits;
+    bs_t bits, *s = &bits;
     block_t *p_block;
 
     if( !p_subpic || !p_subpic->p_region ) return NULL;
@@ -1932,7 +1968,7 @@ static block_t *Encode( encoder_t *p_enc, subpicture_t *p_subpic )
         char psz_fourcc[5];
         memset( &psz_fourcc, 0, sizeof( psz_fourcc ) );
         vlc_fourcc_to_char( p_region->fmt.i_chroma, &psz_fourcc );
-        msg_Err( p_enc, "chroma %4s not supported", psz_fourcc );
+        msg_Err( p_enc, "chroma %4.4s not supported", psz_fourcc );
         return NULL;
     }
 
@@ -1957,10 +1993,10 @@ static block_t *Encode( encoder_t *p_enc, subpicture_t *p_subpic )
     msg_Dbg( p_enc, "encoding subpicture" );
 #endif
     p_block = block_New( p_enc, 64000 );
-    bsw_init_writable( s, p_block->p_buffer, p_block->i_buffer );
+    bs_init( s, p_block->p_buffer, p_block->i_buffer );
 
-    bsw_write( s, 8, 0x20 ); /* Data identifier */
-    bsw_write( s, 8, 0x0 );  /* Subtitle stream id */
+    bs_write( s, 8, 0x20 ); /* Data identifier */
+    bs_write( s, 8, 0x0 );  /* Subtitle stream id */
 
     encode_page_composition( p_enc, s, p_subpic );
     encode_region_composition( p_enc, s, p_subpic );
@@ -1968,13 +2004,13 @@ static block_t *Encode( encoder_t *p_enc, subpicture_t *p_subpic )
     encode_object( p_enc, s, p_subpic );
 
     /* End of display */
-    bsw_write( s, 8, 0x0f ); /* Sync byte */
-    bsw_write( s, 8, DVBSUB_ST_ENDOFDISPLAY ); /* Segment type */
-    bsw_write( s, 16, 1 );  /* Page id */
-    bsw_write( s, 16, 0 );  /* Segment length */
+    bs_write( s, 8, 0x0f ); /* Sync byte */
+    bs_write( s, 8, DVBSUB_ST_ENDOFDISPLAY ); /* Segment type */
+    bs_write( s, 16, 1 );  /* Page id */
+    bs_write( s, 16, 0 );  /* Segment length */
 
-    bsw_write( s, 8, 0xff );/* End marker */
-    p_block->i_buffer = bsw_pos( s ) / 8;
+    bs_write( s, 8, 0xff );/* End marker */
+    p_block->i_buffer = bs_pos( s ) / 8;
     p_block->i_pts = p_block->i_dts = p_subpic->i_start;
     if( !p_subpic->b_ephemer && ( p_subpic->i_stop > p_subpic->i_start ) )
     {
@@ -1984,16 +2020,16 @@ static block_t *Encode( encoder_t *p_enc, subpicture_t *p_subpic )
 
         /* Send another (empty) subtitle to signal the end of display */
         p_block_stop = block_New( p_enc, 64000 );
-        bsw_init_writable( s, p_block_stop->p_buffer, p_block_stop->i_buffer );
-        bsw_write( s, 8, 0x20 ); /* Data identifier */
-        bsw_write( s, 8, 0x0 );  /* Subtitle stream id */
+        bs_init( s, p_block_stop->p_buffer, p_block_stop->i_buffer );
+        bs_write( s, 8, 0x20 ); /* Data identifier */
+        bs_write( s, 8, 0x0 );  /* Subtitle stream id */
         encode_page_composition( p_enc, s, 0 );
-        bsw_write( s, 8, 0x0f ); /* Sync byte */
-        bsw_write( s, 8, DVBSUB_ST_ENDOFDISPLAY ); /* Segment type */
-        bsw_write( s, 16, 1 );  /* Page id */
-        bsw_write( s, 16, 0 );  /* Segment length */
-        bsw_write( s, 8, 0xff );/* End marker */
-        p_block_stop->i_buffer = bsw_pos( s ) / 8;
+        bs_write( s, 8, 0x0f ); /* Sync byte */
+        bs_write( s, 8, DVBSUB_ST_ENDOFDISPLAY ); /* Segment type */
+        bs_write( s, 16, 1 );  /* Page id */
+        bs_write( s, 16, 0 );  /* Segment length */
+        bs_write( s, 8, 0xff );/* End marker */
+        p_block_stop->i_buffer = bs_pos( s ) / 8;
         p_block_stop->i_pts = p_block_stop->i_dts = p_subpic->i_stop;
         block_ChainAppend( &p_block, p_block_stop );
         p_block_stop->i_length = 100000; /* p_subpic->i_stop - p_subpic->i_start; */
@@ -2020,7 +2056,7 @@ static void CloseEncoder( vlc_object_t *p_this )
     free( p_sys );
 }
 
-static void encode_page_composition( encoder_t *p_enc, bsw_t *s,
+static void encode_page_composition( encoder_t *p_enc, bs_t *s,
                                      subpicture_t *p_subpic )
 {
     encoder_sys_t *p_sys = p_enc->p_sys;
@@ -2028,9 +2064,9 @@ static void encode_page_composition( encoder_t *p_enc, bsw_t *s,
     bool b_mode_change = false;
     int i_regions, i_timeout;
 
-    bsw_write( s, 8, 0x0f ); /* Sync byte */
-    bsw_write( s, 8, DVBSUB_ST_PAGE_COMPOSITION ); /* Segment type */
-    bsw_write( s, 16, 1 ); /* Page id */
+    bs_write( s, 8, 0x0f ); /* Sync byte */
+    bs_write( s, 8, DVBSUB_ST_PAGE_COMPOSITION ); /* Segment type */
+    bs_write( s, 16, 1 ); /* Page id */
 
     for( i_regions = 0, p_region = p_subpic ? p_subpic->p_region : 0;
          p_region; p_region = p_region->p_next, i_regions++ )
@@ -2039,9 +2075,8 @@ static void encode_page_composition( encoder_t *p_enc, bsw_t *s,
         {
             encoder_region_t region;
             region.i_width = region.i_height = 0;
-            p_sys->p_regions =
-                realloc( p_sys->p_regions, sizeof(encoder_region_t) *
-                         (p_sys->i_regions + 1) );
+            p_sys->p_regions = xrealloc( p_sys->p_regions,
+                          sizeof(encoder_region_t) * (p_sys->i_regions + 1) );
             p_sys->p_regions[p_sys->i_regions++] = region;
         }
 
@@ -2069,7 +2104,7 @@ static void encode_page_composition( encoder_t *p_enc, bsw_t *s,
         }
     }
 
-    bsw_write( s, 16, i_regions * 6 + 2 ); /* Segment length */
+    bs_write( s, 16, i_regions * 6 + 2 ); /* Segment length */
 
     i_timeout = 0;
     if( p_subpic && !p_subpic->b_ephemer &&
@@ -2078,31 +2113,31 @@ static void encode_page_composition( encoder_t *p_enc, bsw_t *s,
         i_timeout = (p_subpic->i_stop - p_subpic->i_start) / 1000000;
     }
 
-    bsw_write( s, 8, i_timeout ); /* Timeout */
-    bsw_write( s, 4, p_sys->i_page_ver++ );
-    bsw_write( s, 2, b_mode_change ?
+    bs_write( s, 8, i_timeout ); /* Timeout */
+    bs_write( s, 4, p_sys->i_page_ver++ );
+    bs_write( s, 2, b_mode_change ?
               DVBSUB_PCS_STATE_CHANGE : DVBSUB_PCS_STATE_ACQUISITION );
-    bsw_write( s, 2, 0 ); /* Reserved */
+    bs_write( s, 2, 0 ); /* Reserved */
 
     for( i_regions = 0, p_region = p_subpic ? p_subpic->p_region : 0;
          p_region; p_region = p_region->p_next, i_regions++ )
     {
-        bsw_write( s, 8, i_regions );
-        bsw_write( s, 8, 0 ); /* Reserved */
+        bs_write( s, 8, i_regions );
+        bs_write( s, 8, 0 ); /* Reserved */
         if( (p_sys->i_offset_x > 0) && (p_sys->i_offset_y > 0) )
         {
-            bsw_write( s, 16, p_sys->i_offset_x ); /* override x position */
-            bsw_write( s, 16, p_sys->i_offset_y ); /* override y position */
+            bs_write( s, 16, p_sys->i_offset_x ); /* override x position */
+            bs_write( s, 16, p_sys->i_offset_y ); /* override y position */
         }
         else
         {
-            bsw_write( s, 16, p_region->i_x );
-            bsw_write( s, 16, p_region->i_y );
+            bs_write( s, 16, p_region->i_x );
+            bs_write( s, 16, p_region->i_y );
         }
     }
 }
 
-static void encode_clut( encoder_t *p_enc, bsw_t *s, subpicture_t *p_subpic )
+static void encode_clut( encoder_t *p_enc, bs_t *s, subpicture_t *p_subpic )
 {
     encoder_sys_t *p_sys = p_enc->p_sys;
     subpicture_region_t *p_region = p_subpic->p_region;
@@ -2129,32 +2164,32 @@ static void encode_clut( encoder_t *p_enc, bsw_t *s, subpicture_t *p_subpic )
         p_pal = &pal;
     }
 
-    bsw_write( s, 8, 0x0f ); /* Sync byte */
-    bsw_write( s, 8, DVBSUB_ST_CLUT_DEFINITION ); /* Segment type */
-    bsw_write( s, 16, 1 );  /* Page id */
+    bs_write( s, 8, 0x0f ); /* Sync byte */
+    bs_write( s, 8, DVBSUB_ST_CLUT_DEFINITION ); /* Segment type */
+    bs_write( s, 16, 1 );  /* Page id */
 
-    bsw_write( s, 16, p_pal->i_entries * 6 + 2 ); /* Segment length */
-    bsw_write( s, 8, 1 ); /* Clut id */
-    bsw_write( s, 4, p_sys->i_clut_ver++ );
-    bsw_write( s, 4, 0 ); /* Reserved */
+    bs_write( s, 16, p_pal->i_entries * 6 + 2 ); /* Segment length */
+    bs_write( s, 8, 1 ); /* Clut id */
+    bs_write( s, 4, p_sys->i_clut_ver++ );
+    bs_write( s, 4, 0 ); /* Reserved */
 
     for( i = 0; i < p_pal->i_entries; i++ )
     {
-        bsw_write( s, 8, i ); /* Clut entry id */
-        bsw_write( s, 1, p_pal->i_entries == 4 );   /* 2bit/entry flag */
-        bsw_write( s, 1, p_pal->i_entries == 16 );  /* 4bit/entry flag */
-        bsw_write( s, 1, p_pal->i_entries == 256 ); /* 8bit/entry flag */
-        bsw_write( s, 4, 0 ); /* Reserved */
-        bsw_write( s, 1, 1 ); /* Full range flag */
-        bsw_write( s, 8, p_pal->palette[i][3] ?  /* Y value */
+        bs_write( s, 8, i ); /* Clut entry id */
+        bs_write( s, 1, p_pal->i_entries == 4 );   /* 2bit/entry flag */
+        bs_write( s, 1, p_pal->i_entries == 16 );  /* 4bit/entry flag */
+        bs_write( s, 1, p_pal->i_entries == 256 ); /* 8bit/entry flag */
+        bs_write( s, 4, 0 ); /* Reserved */
+        bs_write( s, 1, 1 ); /* Full range flag */
+        bs_write( s, 8, p_pal->palette[i][3] ?  /* Y value */
                   (p_pal->palette[i][0] ? p_pal->palette[i][0] : 16) : 0 );
-        bsw_write( s, 8, p_pal->palette[i][1] ); /* Cr value */
-        bsw_write( s, 8, p_pal->palette[i][2] ); /* Cb value */
-        bsw_write( s, 8, 0xff - p_pal->palette[i][3] ); /* T value */
+        bs_write( s, 8, p_pal->palette[i][1] ); /* Cr value */
+        bs_write( s, 8, p_pal->palette[i][2] ); /* Cb value */
+        bs_write( s, 8, 0xff - p_pal->palette[i][3] ); /* T value */
     }
 }
 
-static void encode_region_composition( encoder_t *p_enc, bsw_t *s,
+static void encode_region_composition( encoder_t *p_enc, bs_t *s,
                                        subpicture_t *p_subpic )
 {
     encoder_sys_t *p_sys = p_enc->p_sys;
@@ -2187,49 +2222,49 @@ static void encode_region_composition( encoder_t *p_enc, bsw_t *s,
             }
         }
 
-        bsw_write( s, 8, 0x0f ); /* Sync byte */
-        bsw_write( s, 8, DVBSUB_ST_REGION_COMPOSITION ); /* Segment type */
-        bsw_write( s, 16, 1 );   /* Page id */
+        bs_write( s, 8, 0x0f ); /* Sync byte */
+        bs_write( s, 8, DVBSUB_ST_REGION_COMPOSITION ); /* Segment type */
+        bs_write( s, 16, 1 );   /* Page id */
 
-        bsw_write( s, 16, 10 + 6 + (b_text ? 2 : 0) ); /* Segment length */
-        bsw_write( s, 8, i_region );
-        bsw_write( s, 4, p_sys->i_region_ver++ );
+        bs_write( s, 16, 10 + 6 + (b_text ? 2 : 0) ); /* Segment length */
+        bs_write( s, 8, i_region );
+        bs_write( s, 4, p_sys->i_region_ver++ );
 
         /* Region attributes */
-        bsw_write( s, 1, i_bg < i_entries ); /* Fill */
-        bsw_write( s, 3, 0 ); /* Reserved */
-        bsw_write( s, 16, p_sys->p_regions[i_region].i_width );
-        bsw_write( s, 16, p_sys->p_regions[i_region].i_height );
-        bsw_write( s, 3, i_depth );  /* Region level of compatibility */
-        bsw_write( s, 3, i_depth  ); /* Region depth */
-        bsw_write( s, 2, 0 ); /* Reserved */
-        bsw_write( s, 8, 1 ); /* Clut id */
-        bsw_write( s, 8, i_bg ); /* region 8bit pixel code */
-        bsw_write( s, 4, i_bg ); /* region 4bit pixel code */
-        bsw_write( s, 2, i_bg ); /* region 2bit pixel code */
-        bsw_write( s, 2, 0 ); /* Reserved */
+        bs_write( s, 1, i_bg < i_entries ); /* Fill */
+        bs_write( s, 3, 0 ); /* Reserved */
+        bs_write( s, 16, p_sys->p_regions[i_region].i_width );
+        bs_write( s, 16, p_sys->p_regions[i_region].i_height );
+        bs_write( s, 3, i_depth );  /* Region level of compatibility */
+        bs_write( s, 3, i_depth  ); /* Region depth */
+        bs_write( s, 2, 0 ); /* Reserved */
+        bs_write( s, 8, 1 ); /* Clut id */
+        bs_write( s, 8, i_bg ); /* region 8bit pixel code */
+        bs_write( s, 4, i_bg ); /* region 4bit pixel code */
+        bs_write( s, 2, i_bg ); /* region 2bit pixel code */
+        bs_write( s, 2, 0 ); /* Reserved */
 
         /* In our implementation we only have 1 object per region */
-        bsw_write( s, 16, i_region );
-        bsw_write( s, 2, b_text ? DVBSUB_OT_BASIC_CHAR:DVBSUB_OT_BASIC_BITMAP );
-        bsw_write( s, 2, 0 ); /* object provider flag */
-        bsw_write( s, 12, 0 );/* object horizontal position */
-        bsw_write( s, 4, 0 ); /* Reserved */
-        bsw_write( s, 12, 0 );/* object vertical position */
+        bs_write( s, 16, i_region );
+        bs_write( s, 2, b_text ? DVBSUB_OT_BASIC_CHAR:DVBSUB_OT_BASIC_BITMAP );
+        bs_write( s, 2, 0 ); /* object provider flag */
+        bs_write( s, 12, 0 );/* object horizontal position */
+        bs_write( s, 4, 0 ); /* Reserved */
+        bs_write( s, 12, 0 );/* object vertical position */
 
         if( b_text )
         {
-            bsw_write( s, 8, 1 ); /* foreground pixel code */
-            bsw_write( s, 8, 0 ); /* background pixel code */
+            bs_write( s, 8, 1 ); /* foreground pixel code */
+            bs_write( s, 8, 0 ); /* background pixel code */
         }
     }
 }
 
-static void encode_pixel_data( encoder_t *p_enc, bsw_t *s,
+static void encode_pixel_data( encoder_t *p_enc, bs_t *s,
                                subpicture_region_t *p_region,
                                bool b_top );
 
-static void encode_object( encoder_t *p_enc, bsw_t *s, subpicture_t *p_subpic )
+static void encode_object( encoder_t *p_enc, bs_t *s, subpicture_t *p_subpic )
 {
     encoder_sys_t *p_sys = p_enc->p_sys;
     subpicture_region_t *p_region;
@@ -2240,31 +2275,31 @@ static void encode_object( encoder_t *p_enc, bsw_t *s, subpicture_t *p_subpic )
     for( i_region = 0, p_region = p_subpic->p_region; p_region;
          p_region = p_region->p_next, i_region++ )
     {
-        bsw_write( s, 8, 0x0f ); /* Sync byte */
-        bsw_write( s, 8, DVBSUB_ST_OBJECT_DATA ); /* Segment type */
-        bsw_write( s, 16, 1 ); /* Page id */
+        bs_write( s, 8, 0x0f ); /* Sync byte */
+        bs_write( s, 8, DVBSUB_ST_OBJECT_DATA ); /* Segment type */
+        bs_write( s, 16, 1 ); /* Page id */
 
-        i_length_pos = bsw_pos( s );
-        bsw_write( s, 16, 0 ); /* Segment length */
-        bsw_write( s, 16, i_region ); /* Object id */
-        bsw_write( s, 4, p_sys->i_region_ver++ );
+        i_length_pos = bs_pos( s );
+        bs_write( s, 16, 0 ); /* Segment length */
+        bs_write( s, 16, i_region ); /* Object id */
+        bs_write( s, 4, p_sys->i_region_ver++ );
 
         /* object coding method */
         switch( p_region->fmt.i_chroma )
         {
         case VLC_CODEC_YUVP:
-            bsw_write( s, 2, 0 );
+            bs_write( s, 2, 0 );
             break;
         case VLC_CODEC_TEXT:
-            bsw_write( s, 2, 1 );
+            bs_write( s, 2, 1 );
             break;
         default:
             msg_Err( p_enc, "FOURCC %d not supported by encoder.", p_region->fmt.i_chroma );
             continue;
         }
 
-        bsw_write( s, 1, 0 ); /* non modifying color flag */
-        bsw_write( s, 1, 0 ); /* Reserved */
+        bs_write( s, 1, 0 ); /* non modifying color flag */
+        bs_write( s, 1, 0 ); /* Reserved */
 
         if( p_region->fmt.i_chroma == VLC_CODEC_TEXT )
         {
@@ -2274,51 +2309,51 @@ static void encode_object( encoder_t *p_enc, bsw_t *s, subpicture_t *p_subpic )
 
             i_size = __MIN( strlen( p_region->psz_text ), 256 );
 
-            bsw_write( s, 8, i_size ); /* number of characters in string */
+            bs_write( s, 8, i_size ); /* number of characters in string */
             for( i = 0; i < i_size; i++ )
             {
-                bsw_write( s, 16, p_region->psz_text[i] );
+                bs_write( s, 16, p_region->psz_text[i] );
             }
 
             /* Update segment length */
             SetWBE( &s->p_start[i_length_pos/8],
-                    (bsw_pos(s) - i_length_pos)/8 -2 );
+                    (bs_pos(s) - i_length_pos)/8 -2 );
             continue;
         }
 
         /* Coding of a bitmap object */
-        i_update_pos = bsw_pos( s );
-        bsw_write( s, 16, 0 ); /* topfield data block length */
-        bsw_write( s, 16, 0 ); /* bottomfield data block length */
+        i_update_pos = bs_pos( s );
+        bs_write( s, 16, 0 ); /* topfield data block length */
+        bs_write( s, 16, 0 ); /* bottomfield data block length */
 
         /* Top field */
-        i_pixel_data_pos = bsw_pos( s );
+        i_pixel_data_pos = bs_pos( s );
         encode_pixel_data( p_enc, s, p_region, true );
-        i_pixel_data_pos = ( bsw_pos( s ) - i_pixel_data_pos ) / 8;
+        i_pixel_data_pos = ( bs_pos( s ) - i_pixel_data_pos ) / 8;
         SetWBE( &s->p_start[i_update_pos/8], i_pixel_data_pos );
 
         /* Bottom field */
-        i_pixel_data_pos = bsw_pos( s );
+        i_pixel_data_pos = bs_pos( s );
         encode_pixel_data( p_enc, s, p_region, false );
-        i_pixel_data_pos = ( bsw_pos( s ) - i_pixel_data_pos ) / 8;
+        i_pixel_data_pos = ( bs_pos( s ) - i_pixel_data_pos ) / 8;
         SetWBE( &s->p_start[i_update_pos/8+2], i_pixel_data_pos );
 
         /* Stuffing for word alignment */
-        bsw_align_0( s );
-        if( bsw_pos( s ) % 16 ) bsw_write( s, 8, 0 );
+        bs_align_0( s );
+        if( bs_pos( s ) % 16 ) bs_write( s, 8, 0 );
 
         /* Update segment length */
-        SetWBE( &s->p_start[i_length_pos/8], (bsw_pos(s) - i_length_pos)/8 -2 );
+        SetWBE( &s->p_start[i_length_pos/8], (bs_pos(s) - i_length_pos)/8 -2 );
     }
 }
 
-static void encode_pixel_line_2bp( bsw_t *s, subpicture_region_t *p_region,
+static void encode_pixel_line_2bp( bs_t *s, subpicture_region_t *p_region,
                                    int i_line );
-static void encode_pixel_line_4bp( bsw_t *s, subpicture_region_t *p_region,
+static void encode_pixel_line_4bp( bs_t *s, subpicture_region_t *p_region,
                                    int i_line );
-static void encode_pixel_line_8bp( bsw_t *s, subpicture_region_t *p_region,
+static void encode_pixel_line_8bp( bs_t *s, subpicture_region_t *p_region,
                                    int i_line );
-static void encode_pixel_data( encoder_t *p_enc, bsw_t *s,
+static void encode_pixel_data( encoder_t *p_enc, bs_t *s,
                                subpicture_region_t *p_region,
                                bool b_top )
 {
@@ -2337,17 +2372,17 @@ static void encode_pixel_data( encoder_t *p_enc, bsw_t *s,
             break;
 
         case 4:
-            bsw_write( s, 8, 0x10 ); /* 2 bit/pixel code string */
+            bs_write( s, 8, 0x10 ); /* 2 bit/pixel code string */
             encode_pixel_line_2bp( s, p_region, i_line );
             break;
 
         case 16:
-            bsw_write( s, 8, 0x11 ); /* 4 bit/pixel code string */
+            bs_write( s, 8, 0x11 ); /* 4 bit/pixel code string */
             encode_pixel_line_4bp( s, p_region, i_line );
             break;
 
         case 256:
-            bsw_write( s, 8, 0x12 ); /* 8 bit/pixel code string */
+            bs_write( s, 8, 0x12 ); /* 8 bit/pixel code string */
             encode_pixel_line_8bp( s, p_region, i_line );
             break;
 
@@ -2357,11 +2392,11 @@ static void encode_pixel_data( encoder_t *p_enc, bsw_t *s,
             break;
         }
 
-        bsw_write( s, 8, 0xf0 ); /* End of object line code */
+        bs_write( s, 8, 0xf0 ); /* End of object line code */
     }
 }
 
-static void encode_pixel_line_2bp( bsw_t *s, subpicture_region_t *p_region,
+static void encode_pixel_line_2bp( bs_t *s, subpicture_region_t *p_region,
                                    int i_line )
 {
     unsigned int i, i_length = 0;
@@ -2382,12 +2417,12 @@ static void encode_pixel_line_2bp( bsw_t *s, subpicture_region_t *p_region,
         {
             /* 2bit/pixel code */
             if( i_last_pixel )
-                bsw_write( s, 2, i_last_pixel );
+                bs_write( s, 2, i_last_pixel );
             else
             {
-                bsw_write( s, 2, 0 );
-                bsw_write( s, 1, 0 );
-                bsw_write( s, 1, 1 ); /* pseudo color 0 */
+                bs_write( s, 2, 0 );
+                bs_write( s, 1, 0 );
+                bs_write( s, 1, 1 ); /* pseudo color 0 */
             }
             i_length--;
         }
@@ -2396,42 +2431,42 @@ static void encode_pixel_line_2bp( bsw_t *s, subpicture_region_t *p_region,
         {
             if( i_last_pixel )
             {
-                bsw_write( s, 2, i_last_pixel );
-                bsw_write( s, 2, i_last_pixel );
+                bs_write( s, 2, i_last_pixel );
+                bs_write( s, 2, i_last_pixel );
             }
             else
             {
-                bsw_write( s, 2, 0 );
-                bsw_write( s, 1, 0 );
-                bsw_write( s, 1, 0 );
-                bsw_write( s, 2, 1 ); /* 2 * pseudo color 0 */
+                bs_write( s, 2, 0 );
+                bs_write( s, 1, 0 );
+                bs_write( s, 1, 0 );
+                bs_write( s, 2, 1 ); /* 2 * pseudo color 0 */
             }
         }
         else if( i_length > 2 )
         {
-            bsw_write( s, 2, 0 );
+            bs_write( s, 2, 0 );
             if( i_length <= 10 )
             {
-                bsw_write( s, 1, 1 );
-                bsw_write( s, 3, i_length - 3 );
-                bsw_write( s, 2, i_last_pixel );
+                bs_write( s, 1, 1 );
+                bs_write( s, 3, i_length - 3 );
+                bs_write( s, 2, i_last_pixel );
             }
             else
             {
-                bsw_write( s, 1, 0 );
-                bsw_write( s, 1, 0 );
+                bs_write( s, 1, 0 );
+                bs_write( s, 1, 0 );
 
                 if( i_length <= 27 )
                 {
-                    bsw_write( s, 2, 2 );
-                    bsw_write( s, 4, i_length - 12 );
-                    bsw_write( s, 2, i_last_pixel );
+                    bs_write( s, 2, 2 );
+                    bs_write( s, 4, i_length - 12 );
+                    bs_write( s, 2, i_last_pixel );
                 }
                 else
                 {
-                    bsw_write( s, 2, 3 );
-                    bsw_write( s, 8, i_length - 29 );
-                    bsw_write( s, 2, i_last_pixel );
+                    bs_write( s, 2, 3 );
+                    bs_write( s, 8, i_length - 29 );
+                    bs_write( s, 2, i_last_pixel );
                 }
             }
         }
@@ -2443,16 +2478,16 @@ static void encode_pixel_line_2bp( bsw_t *s, subpicture_region_t *p_region,
     }
 
     /* Stop */
-    bsw_write( s, 2, 0 );
-    bsw_write( s, 1, 0 );
-    bsw_write( s, 1, 0 );
-    bsw_write( s, 2, 0 );
+    bs_write( s, 2, 0 );
+    bs_write( s, 1, 0 );
+    bs_write( s, 1, 0 );
+    bs_write( s, 2, 0 );
 
     /* Stuffing */
-    bsw_align_0( s );
+    bs_align_0( s );
 }
 
-static void encode_pixel_line_4bp( bsw_t *s, subpicture_region_t *p_region,
+static void encode_pixel_line_4bp( bs_t *s, subpicture_region_t *p_region,
                                    int i_line )
 {
     unsigned int i, i_length = 0;
@@ -2475,13 +2510,13 @@ static void encode_pixel_line_4bp( bsw_t *s, subpicture_region_t *p_region,
         {
             /* 4bit/pixel code */
             if( i_last_pixel )
-                bsw_write( s, 4, i_last_pixel );
+                bs_write( s, 4, i_last_pixel );
             else
             {
-                bsw_write( s, 4, 0 );
-                bsw_write( s, 1, 1 );
-                bsw_write( s, 1, 1 );
-                bsw_write( s, 2, 0 ); /* pseudo color 0 */
+                bs_write( s, 4, 0 );
+                bs_write( s, 1, 1 );
+                bs_write( s, 1, 1 );
+                bs_write( s, 2, 0 ); /* pseudo color 0 */
             }
             i_length--;
         }
@@ -2490,49 +2525,49 @@ static void encode_pixel_line_4bp( bsw_t *s, subpicture_region_t *p_region,
         {
             if( i_last_pixel )
             {
-                bsw_write( s, 4, i_last_pixel );
-                bsw_write( s, 4, i_last_pixel );
+                bs_write( s, 4, i_last_pixel );
+                bs_write( s, 4, i_last_pixel );
             }
             else
             {
-                bsw_write( s, 4, 0 );
-                bsw_write( s, 1, 1 );
-                bsw_write( s, 1, 1 );
-                bsw_write( s, 2, 1 ); /* 2 * pseudo color 0 */
+                bs_write( s, 4, 0 );
+                bs_write( s, 1, 1 );
+                bs_write( s, 1, 1 );
+                bs_write( s, 2, 1 ); /* 2 * pseudo color 0 */
             }
         }
         else if( !i_last_pixel && ( i_length >= 3 ) && ( i_length <= 9 ) )
         {
-            bsw_write( s, 4, 0 );
-            bsw_write( s, 1, 0 );
-            bsw_write( s, 3, i_length - 2 ); /* (i_length - 2) * color 0 */
+            bs_write( s, 4, 0 );
+            bs_write( s, 1, 0 );
+            bs_write( s, 3, i_length - 2 ); /* (i_length - 2) * color 0 */
         }
         else if( i_length > 2 )
         {
-            bsw_write( s, 4, 0 );
-            bsw_write( s, 1, 1 );
+            bs_write( s, 4, 0 );
+            bs_write( s, 1, 1 );
 
             if( i_length <= 7 )
             {
-                bsw_write( s, 1, 0 );
-                bsw_write( s, 2, i_length - 4 );
-                bsw_write( s, 4, i_last_pixel );
+                bs_write( s, 1, 0 );
+                bs_write( s, 2, i_length - 4 );
+                bs_write( s, 4, i_last_pixel );
             }
             else
             {
-                bsw_write( s, 1, 1 );
+                bs_write( s, 1, 1 );
 
                 if( i_length <= 24 )
                 {
-                    bsw_write( s, 2, 2 );
-                    bsw_write( s, 4, i_length - 9 );
-                    bsw_write( s, 4, i_last_pixel );
+                    bs_write( s, 2, 2 );
+                    bs_write( s, 4, i_length - 9 );
+                    bs_write( s, 4, i_last_pixel );
                 }
                 else
                 {
-                    bsw_write( s, 2, 3 );
-                    bsw_write( s, 8, i_length - 25 );
-                    bsw_write( s, 4, i_last_pixel );
+                    bs_write( s, 2, 3 );
+                    bs_write( s, 8, i_length - 25 );
+                    bs_write( s, 4, i_last_pixel );
                 }
             }
         }
@@ -2544,13 +2579,13 @@ static void encode_pixel_line_4bp( bsw_t *s, subpicture_region_t *p_region,
     }
 
     /* Stop */
-    bsw_write( s, 8, 0 );
+    bs_write( s, 8, 0 );
 
     /* Stuffing */
-    bsw_align_0( s );
+    bs_align_0( s );
 }
 
-static void encode_pixel_line_8bp( bsw_t *s, subpicture_region_t *p_region,
+static void encode_pixel_line_8bp( bs_t *s, subpicture_region_t *p_region,
                                    int i_line )
 {
     unsigned int i, i_length = 0;
@@ -2570,28 +2605,28 @@ static void encode_pixel_line_8bp( bsw_t *s, subpicture_region_t *p_region,
         if( ( i_length == 1 ) && i_last_pixel )
         {
             /* 8bit/pixel code */
-            bsw_write( s, 8, i_last_pixel );
+            bs_write( s, 8, i_last_pixel );
         }
         else if( ( i_length == 2 ) && i_last_pixel )
         {
             /* 8bit/pixel code */
-            bsw_write( s, 8, i_last_pixel );
-            bsw_write( s, 8, i_last_pixel );
+            bs_write( s, 8, i_last_pixel );
+            bs_write( s, 8, i_last_pixel );
         }
         else if( i_length <= 127 )
         {
-            bsw_write( s, 8, 0 );
+            bs_write( s, 8, 0 );
 
             if( !i_last_pixel )
             {
-                bsw_write( s, 1, 0 );
-                bsw_write( s, 7, i_length ); /* pseudo color 0 */
+                bs_write( s, 1, 0 );
+                bs_write( s, 7, i_length ); /* pseudo color 0 */
             }
             else
             {
-                bsw_write( s, 1, 1 );
-                bsw_write( s, 7, i_length );
-                bsw_write( s, 8, i_last_pixel );
+                bs_write( s, 1, 1 );
+                bs_write( s, 7, i_length );
+                bs_write( s, 8, i_last_pixel );
             }
         }
 
@@ -2602,9 +2637,24 @@ static void encode_pixel_line_8bp( bsw_t *s, subpicture_region_t *p_region,
     }
 
     /* Stop */
-    bsw_write( s, 8, 0 );
-    bsw_write( s, 8, 0 );
+    bs_write( s, 8, 0 );
+    bs_write( s, 8, 0 );
 
     /* Stuffing */
-    bsw_align_0( s );
+    bs_align_0( s );
 }
+
+
+static void default_dds_init( decoder_t * p_dec )
+{
+    decoder_sys_t *p_sys = p_dec->p_sys;
+
+    /* see notes on DDS at the top of the file */
+
+    /* configure for SD res in case DDS is not present */
+    p_sys->display.i_version = 0xff; /* an invalid version so it's always different */
+    p_sys->display.i_width = 720;
+    p_sys->display.i_height = 576;
+    p_sys->display.b_windowed = false;
+}
+