]> git.sesse.net Git - vlc/blobdiff - modules/codec/cc.c
Simplify FLAC extradata (streaminfo) parsing
[vlc] / modules / codec / cc.c
index 0e62866dc264f81dbfd29fb73a33d18a19f1b047..8863f1c6f5aa40afc37aa01b45729bc9251ab96c 100644 (file)
@@ -1,24 +1,23 @@
 /*****************************************************************************
- * cc608.c : CC 608/708 subtitles decoder
+ * cc.c : CC 608/708 subtitles decoder
  *****************************************************************************
- * Copyright (C) 2007 Laurent Aimar
- * $Id$
+ * Copyright © 2007-2010 Laurent Aimar, 2011 VLC authors and VideoLAN
  *
  * Authors: Laurent Aimar < fenrir # via.ecp.fr>
  *
- * 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
+ * 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 General Public License for more details.
+ * 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 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.
+ * 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.
  *****************************************************************************/
 
 /*****************************************************************************
 # include "config.h"
 #endif
 
-#include <vlc/vlc.h>
-#include <vlc_vout.h>
-#include <vlc_codec.h>
-#include <vlc_input.h>
+#include <assert.h>
 
-#include <vlc_osd.h>
-#include <vlc_filter.h>
-#include <vlc_image.h>
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_codec.h>
 #include <vlc_charset.h>
-#include <vlc_stream.h>
-#include <vlc_xml.h>
-#include <errno.h>
-#include <string.h>
+
+#include "substext.h"
 
 /*****************************************************************************
  * Module descriptor.
 static int  Open ( vlc_object_t * );
 static void Close( vlc_object_t * );
 
-vlc_module_begin();
-    set_shortname( _("CC 608/708"));
-    set_description( _("Closed Captions decoder") );
-    set_capability( "decoder", 50 );
-    set_callbacks( Open, Close );
-vlc_module_end();
+vlc_module_begin ()
+    set_shortname( N_("CC 608/708"))
+    set_description( N_("Closed Captions decoder") )
+    set_capability( "decoder", 50 )
+    set_callbacks( Open, Close )
+vlc_module_end ()
 
 /*****************************************************************************
  * Local prototypes
@@ -139,17 +133,14 @@ typedef struct
 } eia608_t;
 
 static void         Eia608Init( eia608_t * );
-static vlc_bool_t   Eia608Parse( eia608_t *h, int i_channel_selected, const uint8_t data[2] );
-static char        *Eia608Text( eia608_t *h, vlc_bool_t b_html );
-static void         Eia608Exit( eia608_t * );
+static bool   Eia608Parse( eia608_t *h, int i_channel_selected, const uint8_t data[2] );
+static char        *Eia608Text( eia608_t *h, bool b_html );
 
 /* It will be enough up to 63 B frames, which is far too high for
  * broadcast environment */
 #define CC_MAX_REORDER_SIZE (64)
 struct decoder_sys_t
 {
-    int i;
-
     int     i_block;
     block_t *pp_block[CC_MAX_REORDER_SIZE];
 
@@ -196,22 +187,19 @@ static int Open( vlc_object_t *p_this )
     p_dec->pf_decode_sub = Decode;
 
     /* Allocate the memory needed to store the decoder's structure */
-    p_dec->p_sys = p_sys = malloc( sizeof( *p_sys ) );
+    p_dec->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) );
     if( p_sys == NULL )
-    {
-        msg_Err( p_dec, "out of memory" );
         return VLC_ENOMEM;
-    }
 
     /* init of p_sys */
-    memset( p_sys, 0, sizeof( *p_sys ) );
-    p_sys->i_block = 0;
-
     p_sys->i_field = i_field;
     p_sys->i_channel = i_channel;
 
     Eia608Init( &p_sys->eia608 );
 
+    p_dec->fmt_out.i_cat = SPU_ES;
+    p_dec->fmt_out.i_codec = VLC_CODEC_TEXT;
+
     return VLC_SUCCESS;
 }
 
@@ -252,11 +240,9 @@ static void Close( vlc_object_t *p_this )
 {
     decoder_t *p_dec = (decoder_t *)p_this;
     decoder_sys_t *p_sys = p_dec->p_sys;
-    int i;
 
-    for( i = 0; i < p_sys->i_block; i++ )
+    for( int i = 0; i < p_sys->i_block; i++ )
         block_Release( p_sys->pp_block[i] );
-    Eia608Exit( &p_sys->eia608 );
     free( p_sys );
 }
 
@@ -280,7 +266,6 @@ static block_t *Pop( decoder_t *p_dec )
     decoder_sys_t *p_sys = p_dec->p_sys;
     block_t *p_block;
     int i_index;
-    int i;
     /* XXX Cc captions data are OUT OF ORDER (because we receive them in the bitstream
      * order (ie ordered by video picture dts) instead of the display order.
      *  We will simulate a simple IPB buffer scheme
@@ -295,11 +280,11 @@ static block_t *Pop( decoder_t *p_dec )
         return NULL;
 
     p_block = p_sys->pp_block[i_index = 0];
-    if( p_block->i_pts > 0 )
+    if( p_block->i_pts > VLC_TS_INVALID )
     {
-        for( i = 1; i < p_sys->i_block-1; i++ )
+        for( int i = 1; i < p_sys->i_block-1; i++ )
         {
-            if( p_sys->pp_block[i]->i_pts > 0 && p_block->i_pts > 0 &&
+            if( p_sys->pp_block[i]->i_pts > VLC_TS_INVALID && p_block->i_pts > VLC_TS_INVALID &&
                 p_sys->pp_block[i]->i_pts < p_block->i_pts )
                 p_block = p_sys->pp_block[i_index = i];
         }
@@ -315,10 +300,9 @@ static subpicture_t *Subtitle( decoder_t *p_dec, char *psz_subtitle, char *psz_h
 {
     //decoder_sys_t *p_sys = p_dec->p_sys;
     subpicture_t *p_spu = NULL;
-    video_format_t fmt;
 
     /* We cannot display a subpicture with no date */
-    if( i_pts == 0 )
+    if( i_pts <= VLC_TS_INVALID )
     {
         msg_Warn( p_dec, "subtitle without a date" );
         return NULL;
@@ -329,57 +313,38 @@ static subpicture_t *Subtitle( decoder_t *p_dec, char *psz_subtitle, char *psz_h
         EnsureUTF8( psz_html );
 
     /* Create the subpicture unit */
-    p_spu = p_dec->pf_spu_buffer_new( p_dec );
+    p_spu = decoder_NewSubpictureText( p_dec );
     if( !p_spu )
     {
-        msg_Warn( p_dec, "can't get spu buffer" );
         free( psz_subtitle );
-        if( psz_html )
-            free( psz_html );
+        free( psz_html );
         return NULL;
     }
+    p_spu->i_start    = i_pts;
+    p_spu->i_stop     = i_pts + 10000000;   /* 10s max */
+    p_spu->b_ephemer  = true;
+    p_spu->b_absolute = false;
 
-    p_spu->b_pausable = VLC_TRUE;
-
-    /* Create a new subpicture region */
-    memset( &fmt, 0, sizeof(video_format_t) );
-    fmt.i_chroma = VLC_FOURCC('T','E','X','T');
-    fmt.i_aspect = 0;
-    fmt.i_width = fmt.i_height = 0;
-    fmt.i_x_offset = fmt.i_y_offset = 0;
-    p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
-    if( !p_spu->p_region )
-    {
-        msg_Err( p_dec, "cannot allocate SPU region" );
-        free( psz_subtitle );
-        if( psz_html )
-            free( psz_html );
-        p_dec->pf_spu_buffer_del( p_dec, p_spu );
-        return NULL;
-    }
-
-    /* Decode and format the subpicture unit */
-    /* Normal text subs, easy markup */
-    p_spu->p_region->i_align = SUBPICTURE_ALIGN_BOTTOM;// | SUBPICTURE_ALIGN_LEFT;// | p_sys->i_align;
-    p_spu->i_x = 0; //p_sys->i_align ? 20 : 0;
-    p_spu->i_y = 10;
-
-    p_spu->p_region->psz_text = psz_subtitle;
-    p_spu->p_region->psz_html = psz_html;
+    subpicture_updater_sys_t *p_spu_sys = p_spu->updater.p_sys;
 
-    p_spu->i_start = i_pts;
-    p_spu->i_stop = i_pts + 10000000;   /* 10s max */
-    p_spu->b_ephemer = VLC_TRUE;
-    p_spu->b_absolute = VLC_FALSE;
+    /* The "leavetext" alignment is a special mode where the subpicture
+       region itself gets aligned, but the text inside it does not */
+    p_spu_sys->align = SUBPICTURE_ALIGN_LEAVETEXT;
+    p_spu_sys->text  = psz_subtitle;
+    p_spu_sys->html  = psz_html;
+    p_spu_sys->i_font_height_percent = 5;
+    p_spu_sys->renderbg = true;
 
     return p_spu;
 }
 
 static subpicture_t *Convert( decoder_t *p_dec, block_t *p_block )
 {
+    assert( p_block );
+
     decoder_sys_t *p_sys = p_dec->p_sys;
     const int64_t i_pts = p_block->i_pts;
-    vlc_bool_t b_changed = VLC_FALSE;
+    bool b_changed = false;
 
     /* TODO do the real decoding here */
     while( p_block->i_buffer >= 3 )
@@ -393,12 +358,10 @@ static subpicture_t *Convert( decoder_t *p_dec, block_t *p_block )
     if( p_block )
         block_Release( p_block );
 
-    static int64_t i_last = 0;
-    if( b_changed )//&& i_pts - i_last > 100*1000 )
+    if( b_changed )
     {
-        char *psz_subtitle = Eia608Text( &p_sys->eia608, VLC_FALSE );
-        char *psz_html     = NULL;//Eia608Text( &p_sys->eia608, VLC_TRUE );
-        i_last = i_pts;
+        char *psz_subtitle = Eia608Text( &p_sys->eia608, false );
+        char *psz_html = Eia608Text( &p_sys->eia608, true );
         return Subtitle( p_dec, psz_subtitle, psz_html, i_pts );
     }
     return NULL;
@@ -461,22 +424,21 @@ static void Eia608Cursor( eia608_t *h, int dx )
 static void Eia608ClearScreenRowX( eia608_t *h, int i_screen, int i_row, int x )
 {
     eia608_screen *screen = &h->screen[i_screen];
-    int i;
 
     if( x == 0 )
     {
-        screen->row_used[i_row] = VLC_FALSE;
+        screen->row_used[i_row] = false;
     }
     else
     {
-        screen->row_used[i_row] = VLC_FALSE;
-        for( i = 0; i < x; i++ )
+        screen->row_used[i_row] = false;
+        for( int i = 0; i < x; i++ )
         {
             if( screen->characters[i_row][i] != ' ' ||
                 screen->colors[i_row][i] != EIA608_COLOR_DEFAULT ||
                 screen->fonts[i_row][i] != EIA608_FONT_REGULAR )
             {
-                screen->row_used[i_row] = VLC_TRUE;
+                screen->row_used[i_row] = true;
                 break;
             }
         }
@@ -497,8 +459,7 @@ static void Eia608ClearScreenRow( eia608_t *h, int i_screen, int i_row )
 
 static void Eia608ClearScreen( eia608_t *h, int i_screen )
 {
-    int i;
-    for( i = 0; i < EIA608_SCREEN_ROWS; i++ )
+    for( int i = 0; i < EIA608_SCREEN_ROWS; i++ )
         Eia608ClearScreenRow( h, i_screen, i );
 }
 
@@ -521,7 +482,7 @@ static int Eia608GetWritingScreenIndex( eia608_t *h )
     }
 }
 
-static void Eia608EraseScreen( eia608_t *h, vlc_bool_t b_displayed )
+static void Eia608EraseScreen( eia608_t *h, bool b_displayed )
 {
     Eia608ClearScreen( h, b_displayed ? h->i_screen : (1-h->i_screen) );
 }
@@ -540,7 +501,7 @@ static void Eia608Write( eia608_t *h, const uint8_t c )
     screen->characters[i_row][i_column] = c;
     screen->colors[i_row][i_column] = h->color;
     screen->fonts[i_row][i_column] = h->font;
-    screen->row_used[i_row] = VLC_TRUE;
+    screen->row_used[i_row] = true;
     Eia608Cursor( h, 1 );
 }
 static void Eia608Erase( eia608_t *h )
@@ -570,11 +531,13 @@ static void Eia608EraseToEndOfRow( eia608_t *h )
 
 static void Eia608RollUp( eia608_t *h )
 {
+    if( h->mode == EIA608_MODE_TEXT )
+        return;
+
     const int i_screen = Eia608GetWritingScreenIndex( h );
     eia608_screen *screen = &h->screen[i_screen];
 
     int keep_lines;
-    int i;
 
     /* Window size */
     if( h->mode == EIA608_MODE_ROLLUP_2 )
@@ -590,11 +553,11 @@ static void Eia608RollUp( eia608_t *h )
     h->cursor.i_column = 0;
 
     /* Erase lines above our window */
-    for( i = 0; i < h->cursor.i_row - keep_lines; i++ )
+    for( int i = 0; i < h->cursor.i_row - keep_lines; i++ )
         Eia608ClearScreenRow( h, i_screen, i );
 
     /* Move up */
-    for( i = 0; i < keep_lines-1; i++ )
+    for( int i = 0; i < keep_lines-1; i++ )
     {
         const int i_row = h->cursor.i_row - keep_lines + i + 1;
         if( i_row < 0 )
@@ -608,18 +571,27 @@ static void Eia608RollUp( eia608_t *h )
     /* Reset current row */
     Eia608ClearScreenRow( h, i_screen, h->cursor.i_row );
 }
-static void Eia608ParseChannel( eia608_t *h, uint8_t d1 )
+static void Eia608ParseChannel( eia608_t *h, const uint8_t d[2] )
 {
-    if( d1 == 0x14 )
-        h->i_channel = 1;
-    else if( d1 == 0x1c )
-        h->i_channel = 2;
-    else if( ( d1 >= 0x01 && d1 <= 0x0f ) || d1 == 0x15 )
+    /* Check odd parity */
+    static const int p4[16] = {
+        0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0
+    };
+    if( p4[d[0] & 0xf] == p4[d[0] >> 4] ||
+        p4[d[1] & 0xf] == p4[ d[1] >> 4] )
+    {
+        h->i_channel = -1;
+        return;
+    }
+
+    /* */
+    const int d1 = d[0] & 0x7f;
+    if( d1 >= 0x10 && d1 <= 0x1f )
+        h->i_channel = 1 + ((d1 & 0x08) != 0);
+    else if( d1 < 0x10 )
         h->i_channel = 3;
-    else if( d1 == 0x1d )
-        h->i_channel = 4;
 }
-static vlc_bool_t Eia608ParseTextAttribute( eia608_t *h, uint8_t d2 )
+static bool Eia608ParseTextAttribute( eia608_t *h, uint8_t d2 )
 {
     const int i_index = d2 - 0x20;
     assert( d2 >= 0x20 && d2 <= 0x2f );
@@ -628,21 +600,21 @@ static vlc_bool_t Eia608ParseTextAttribute( eia608_t *h, uint8_t d2 )
     h->font  = pac2_attribs[i_index].i_font;
     Eia608Cursor( h, 1 );
 
-    return VLC_FALSE;
+    return false;
 }
-static vlc_bool_t Eia608ParseSingle( eia608_t *h, const uint8_t dx )
+static bool Eia608ParseSingle( eia608_t *h, const uint8_t dx )
 {
     assert( dx >= 0x20 );
     Eia608Write( h, dx );
-    return VLC_TRUE;
+    return true;
 }
-static vlc_bool_t Eia608ParseDouble( eia608_t *h, uint8_t d2 )
+static bool Eia608ParseDouble( eia608_t *h, uint8_t d2 )
 {
     assert( d2 >= 0x30 && d2 <= 0x3f );
     Eia608Write( h, d2 + 0x50 ); /* We use charaters 0x80...0x8f */
-    return VLC_TRUE;
+    return true;
 }
-static vlc_bool_t Eia608ParseExtended( eia608_t *h, uint8_t d1, uint8_t d2 )
+static bool Eia608ParseExtended( eia608_t *h, uint8_t d1, uint8_t d2 )
 {
     assert( d2 >= 0x20 && d2 <= 0x3f );
     assert( d1 == 0x12 || d1 == 0x13 );
@@ -655,11 +627,11 @@ static vlc_bool_t Eia608ParseExtended( eia608_t *h, uint8_t d1, uint8_t d2 )
      * advanced one */
     Eia608Cursor( h, -1 );
     Eia608Write( h, d2 );
-    return VLC_TRUE;
+    return true;
 }
-static vlc_bool_t Eia608ParseCommand0x14( eia608_t *h, uint8_t d2 )
+static bool Eia608ParseCommand0x14( eia608_t *h, uint8_t d2 )
 {
-    vlc_bool_t b_changed = VLC_FALSE;
+    bool b_changed = false;
 
     switch( d2 )
     {
@@ -668,7 +640,7 @@ static vlc_bool_t Eia608ParseCommand0x14( eia608_t *h, uint8_t d2 )
         break;
     case 0x21:  /* Backspace */
         Eia608Erase( h );
-        b_changed = VLC_TRUE;
+        b_changed = true;
         break;
     case 0x22:  /* Reserved */
     case 0x23:
@@ -681,9 +653,9 @@ static vlc_bool_t Eia608ParseCommand0x14( eia608_t *h, uint8_t d2 )
     case 0x27:  /* Rollup 4 */
         if( h->mode == EIA608_MODE_POPUP || h->mode == EIA608_MODE_PAINTON )
         {
-            Eia608EraseScreen( h, VLC_TRUE );
-            Eia608EraseScreen( h, VLC_FALSE );
-            b_changed = VLC_TRUE;
+            Eia608EraseScreen( h, true );
+            Eia608EraseScreen( h, false );
+            b_changed = true;
         }
 
         if( d2 == 0x25 )
@@ -711,15 +683,15 @@ static vlc_bool_t Eia608ParseCommand0x14( eia608_t *h, uint8_t d2 )
         break;
 
     case 0x2c: /* Erase displayed memory */
-        Eia608EraseScreen( h, VLC_TRUE );
-        b_changed = VLC_TRUE;
+        Eia608EraseScreen( h, true );
+        b_changed = true;
         break;
     case 0x2d: /* Carriage return */
         Eia608RollUp(h);
-        b_changed = VLC_TRUE;
+        b_changed = true;
         break;
     case 0x2e: /* Erase non displayed memory */
-        Eia608EraseScreen( h, VLC_FALSE );
+        Eia608EraseScreen( h, false );
         break;
     case 0x2f: /* End of caption (flip screen if not paint on) */
         if( h->mode != EIA608_MODE_PAINTON )
@@ -729,12 +701,12 @@ static vlc_bool_t Eia608ParseCommand0x14( eia608_t *h, uint8_t d2 )
         h->cursor.i_row = 0;
         h->color = EIA608_COLOR_DEFAULT;
         h->font = EIA608_FONT_REGULAR;
-        b_changed = VLC_TRUE;
+        b_changed = true;
         break;
     }
     return b_changed;
 }
-static vlc_bool_t Eia608ParseCommand0x17( eia608_t *h, uint8_t d2 )
+static bool Eia608ParseCommand0x17( eia608_t *h, uint8_t d2 )
 {
     switch( d2 )
     {
@@ -748,9 +720,9 @@ static vlc_bool_t Eia608ParseCommand0x17( eia608_t *h, uint8_t d2 )
         Eia608Cursor( h, 3 );
         break;
     }
-    return VLC_FALSE;
+    return false;
 }
-static vlc_bool_t Eia608ParsePac( eia608_t *h, uint8_t d1, uint8_t d2 )
+static bool Eia608ParsePac( eia608_t *h, uint8_t d1, uint8_t d2 )
 {
     static const int pi_row[] = {
         11, -1, 1, 2, 3, 4, 12, 13, 14, 15, 5, 6, 7, 8, 9, 10
@@ -760,7 +732,7 @@ static vlc_bool_t Eia608ParsePac( eia608_t *h, uint8_t d1, uint8_t d2 )
     assert( d2 >= 0x40 && d2 <= 0x7f );
 
     if( pi_row[i_row_index] <= 0 )
-        return VLC_FALSE;
+        return false;
 
     /* Row */
     if( h->mode != EIA608_MODE_TEXT )
@@ -772,12 +744,15 @@ static vlc_bool_t Eia608ParsePac( eia608_t *h, uint8_t d1, uint8_t d2 )
     else if( d2 >= 0x40 )
         d2 -= 0x40;
     h->cursor.i_column = pac2_attribs[d2].i_column;
-    return VLC_FALSE;
+    h->color = pac2_attribs[d2].i_color;
+    h->font  = pac2_attribs[d2].i_font;
+
+    return false;
 }
 
-static vlc_bool_t Eia608ParseData( eia608_t *h, uint8_t d1, uint8_t d2 )
+static bool Eia608ParseData( eia608_t *h, uint8_t d1, uint8_t d2 )
 {
-    vlc_bool_t b_changed = VLC_FALSE;
+    bool b_changed = false;
 
     if( d1 >= 0x18 && d1 <= 0x1f )
         d1 -= 8;
@@ -844,7 +819,7 @@ static void Eia608TextUtf8( char *psz_utf8, uint8_t c ) // Returns number of byt
         E2( 0x86, 0xc2,0xa3), // Pounds sterling
         E3( 0x87, 0xe2,0x99,0xaa), // Music note
         E2( 0x88, 0xc3,0xa0), // lowercase a, grave accent
-        E1( 0x89, 0x20), // transparent space, we make it regular
+        E2( 0x89, 0xc2,0xa0), // transparent space
         E2( 0x8a, 0xc3,0xa8), // lowercase e, grave accent
         E2( 0x8b, 0xc3,0xa2), // lowercase a, circumflex accent
         E2( 0x8c, 0xc3,0xaa), // lowercase e, circumflex accent
@@ -860,15 +835,15 @@ static void Eia608TextUtf8( char *psz_utf8, uint8_t c ) // Returns number of byt
         E2( 0x94, 0xc3,0x9c), // capital letter U with diaresis
         E2( 0x95, 0xc3,0xbc), // lowercase letter U with diaeresis
         E1( 0x96, 0x27), // apostrophe
-        E2( 0x97, 0xc1,0xa1), // inverted exclamation mark
+        E2( 0x97, 0xc2,0xa1), // inverted exclamation mark
         E1( 0x98, 0x2a), // asterisk
         E1( 0x99, 0x27), // apostrophe (yes, duped). See CCADI source code.
         E1( 0x9a, 0x2d), // hyphen-minus
         E2( 0x9b, 0xc2,0xa9), // copyright sign
         E3( 0x9c, 0xe2,0x84,0xa0), // Service mark
         E1( 0x9d, 0x2e), // Full stop (.)
-        E1( 0x9e, 0x22), // Quoatation mark
-        E1( 0x9f, 0x22), // Quoatation mark
+        E3( 0x9e, 0xe2,0x80,0x9c), // Quotation mark
+        E3( 0x9f, 0xe2,0x80,0x9d), // Quotation mark
         E2( 0xa0, 0xc3,0x80), // uppercase A, grave accent
         E2( 0xa1, 0xc3,0x82), // uppercase A, circumflex
         E2( 0xa2, 0xc3,0x87), // uppercase C with cedilla
@@ -926,23 +901,14 @@ static void Eia608TextUtf8( char *psz_utf8, uint8_t c ) // Returns number of byt
 #undef E2
 #undef E1
 
-    static const int i_c2utf8 = sizeof(c2utf8)/sizeof(*c2utf8);
-    int i;
+    for( size_t i = 0; i < ARRAY_SIZE(c2utf8) ; i++ )
+        if( c2utf8[i].c == c ) {
+            strcpy( psz_utf8, c2utf8[i].utf8 );
+            return;
+        }
 
-    for( i = 0; i < i_c2utf8; i++ )
-    {
-        if( c2utf8[i].c == c )
-            break;
-    }
-    if( i >= i_c2utf8 )
-    {
-        psz_utf8[0] = c < 0x80 ? c : '?';   /* Normal : Unsupported */
-        psz_utf8[1] = '\0';
-    }
-    else
-    {
-        strcpy( psz_utf8, c2utf8[i].utf8 );
-    }
+    psz_utf8[0] = c < 0x80 ? c : '?';   /* Normal : Unsupported */
+    psz_utf8[1] = '\0';
 }
 
 static void Eia608Strlcat( char *d, const char *s, int i_max )
@@ -953,7 +919,9 @@ static void Eia608Strlcat( char *d, const char *s, int i_max )
         d[i_max-1] = '\0';
 }
 
-static void Eia608TextLine( struct eia608_screen *screen, char *psz_text, int i_text_max, int i_row, vlc_bool_t b_html )
+#define CAT(t) Eia608Strlcat( psz_text, t, i_text_max )
+
+static void Eia608TextLine( struct eia608_screen *screen, char *psz_text, int i_text_max, int i_row, bool b_html )
 {
     const uint8_t *p_char = screen->characters[i_row];
     const eia608_color_t *p_color = screen->colors[i_row];
@@ -962,13 +930,24 @@ static void Eia608TextLine( struct eia608_screen *screen, char *psz_text, int i_
     int i_end;
     int x;
     eia608_color_t last_color = EIA608_COLOR_DEFAULT;
-    vlc_bool_t     b_last_italics = VLC_FALSE;
-    vlc_bool_t     b_last_underline = VLC_FALSE;
+    bool     b_last_italics = false;
+    bool     b_last_underline = false;
+    char utf8[4];
 
     /* Search the start */
     i_start = 0;
-    while( i_start < EIA608_SCREEN_COLUMNS-1 && p_char[i_start] == ' ' )
+
+    /* Ensure we get a monospaced font (required for accurate positioning */
+    if( b_html )
+        CAT( "<tt>" );
+
+    /* Convert leading spaces to non-breaking so that they don't get
+       stripped by the RenderHtml routine as regular whitespace */
+    while( i_start < EIA608_SCREEN_COLUMNS && p_char[i_start] == ' ' ) {
+        Eia608TextUtf8( utf8, 0x89 );
+        CAT( utf8 );
         i_start++;
+    }
 
     /* Search the end */
     i_end = EIA608_SCREEN_COLUMNS-1;
@@ -976,18 +955,16 @@ static void Eia608TextLine( struct eia608_screen *screen, char *psz_text, int i_
         i_end--;
 
     /* */
-#define CAT(t) Eia608Strlcat( psz_text, t, i_text_max )
     for( x = i_start; x <= i_end; x++ )
     {
         eia608_color_t color = p_color[x];
-        vlc_bool_t b_italics = p_font[x] & EIA608_FONT_ITALICS;
-        vlc_bool_t b_underline = p_font[x] & EIA608_FONT_UNDERLINE;
-        char utf8[4];
+        bool b_italics = p_font[x] & EIA608_FONT_ITALICS;
+        bool b_underline = p_font[x] & EIA608_FONT_UNDERLINE;
 
         /* */
         if( b_html )
         {
-            vlc_bool_t b_close_color, b_close_italics, b_close_underline;
+            bool b_close_color, b_close_italics, b_close_underline;
 
             /* We create the tags font / i / u in that orders */
             b_close_color = color != last_color && last_color != EIA608_COLOR_DEFAULT;
@@ -996,7 +973,7 @@ static void Eia608TextLine( struct eia608_screen *screen, char *psz_text, int i_
 
             /* Be sure to create valid html */
             b_close_italics |= b_last_italics && b_close_color;
-            b_close_underline = b_last_underline && ( b_close_italics || b_close_color );
+            b_close_underline |= b_last_underline && ( b_close_italics || b_close_color );
 
             if( b_close_underline )
                 CAT( "</u>" );
@@ -1017,9 +994,9 @@ static void Eia608TextLine( struct eia608_screen *screen, char *psz_text, int i_
                     "#ff00ff",  // magenta
                     "#ffffff",  // user defined XXX we use white
                 };
-                CAT( "<font color=" );
+                CAT( "<font color=\"" );
                 CAT( ppsz_color[color] );
-                CAT( ">" );
+                CAT( "\">" );
             }
             if( ( b_close_italics && b_italics ) || ( b_italics && !b_last_italics ) )
                 CAT( "<i>" );
@@ -1027,9 +1004,34 @@ static void Eia608TextLine( struct eia608_screen *screen, char *psz_text, int i_
                 CAT( "<u>" );
         }
 
-        /* */ 
-        Eia608TextUtf8( utf8, p_char[x] );
-        CAT( utf8 );
+        if( b_html ) {
+            /* Escape XML reserved characters
+               http://www.w3.org/TR/xml/#syntax */
+            switch (p_char[x]) {
+            case '>':
+                CAT( "&gt;" );
+                break;
+            case '<':
+                CAT( "&lt;" );
+                break;
+            case '"':
+                CAT( "&quot;" );
+                break;
+            case '\'':
+                CAT( "&apos;" );
+                break;
+            case '&':
+                CAT( "&amp;" );
+                break;
+            default:
+                Eia608TextUtf8( utf8, p_char[x] );
+                CAT( utf8 );
+                break;
+            }
+        } else {
+            Eia608TextUtf8( utf8, p_char[x] );
+            CAT( utf8 );
+        }
 
         /* */
         b_last_underline = b_underline;
@@ -1044,6 +1046,7 @@ static void Eia608TextLine( struct eia608_screen *screen, char *psz_text, int i_
             CAT( "</i>" );
         if( last_color != EIA608_COLOR_DEFAULT )
             CAT( "</font>" );
+        CAT( "</tt>" );
     }
 #undef CAT
 }
@@ -1071,18 +1074,18 @@ static void Eia608Init( eia608_t *h )
     h->font = EIA608_FONT_REGULAR;
     h->i_row_rollup = EIA608_SCREEN_ROWS-1;
 }
-static vlc_bool_t Eia608Parse( eia608_t *h, int i_channel_selected, const uint8_t data[2] )
+static bool Eia608Parse( eia608_t *h, int i_channel_selected, const uint8_t data[2] )
 {
-    const uint8_t d1 = data[0] & 0x7f; /* Removed parity bit TODO we might want to check them */
+    const uint8_t d1 = data[0] & 0x7f; /* Removed parity bit */
     const uint8_t d2 = data[1] & 0x7f;
-    vlc_bool_t b_screen_changed = VLC_FALSE;
+    bool b_screen_changed = false;
 
     if( d1 == 0 && d2 == 0 )
-        return VLC_FALSE;   /* Ignore padding */
+        return false;   /* Ignore padding (parity check are sometimes invalid on them) */
 
-    Eia608ParseChannel( h, d1 );
+    Eia608ParseChannel( h, data );
     if( h->i_channel != i_channel_selected )
-        return VLC_FALSE;
+        return false;
     //fprintf( stderr, "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC %x %x\n", data[0], data[1] );
 
     if( d1 >= 0x10 )
@@ -1101,27 +1104,25 @@ static vlc_bool_t Eia608Parse( eia608_t *h, int i_channel_selected, const uint8_
     return b_screen_changed;
 }
 
-static char *Eia608Text( eia608_t *h, vlc_bool_t b_html )
+static char *Eia608Text( eia608_t *h, bool b_html )
 {
-    const int i_size = EIA608_SCREEN_ROWS * 3 * EIA608_SCREEN_COLUMNS+1;
+    const int i_size = EIA608_SCREEN_ROWS * 10 * EIA608_SCREEN_COLUMNS+1;
     struct eia608_screen *screen = &h->screen[h->i_screen];
-    vlc_bool_t b_first = VLC_TRUE;
+    bool b_first = true;
     char *psz;
-    int i;
 
     /* We allocate a buffer big enough for normal case */
     psz = malloc( i_size );
+    if( !psz )
+        return NULL;
     *psz = '\0';
     if( b_html )
         Eia608Strlcat( psz, "<text>", i_size );
-    for( i = 0; i < EIA608_SCREEN_ROWS; i++ )
+    for( int i = 0; i < EIA608_SCREEN_ROWS; i++ )
     {
-        if( !screen->row_used[i] )
-            continue;
-
         if( !b_first )
             Eia608Strlcat( psz, b_html ? "<br />" : "\n", i_size );
-        b_first = VLC_FALSE;
+        b_first = false;
 
         Eia608TextLine( screen, psz, i_size, i, b_html );
     }
@@ -1129,8 +1130,3 @@ static char *Eia608Text( eia608_t *h, vlc_bool_t b_html )
         Eia608Strlcat( psz, "</text>", i_size );
     return psz;
 }
-
-static void Eia608Exit( eia608_t *h )
-{
-}
-