]> git.sesse.net Git - vlc/blobdiff - modules/codec/zvbi.c
macosx: Fix addNode:.
[vlc] / modules / codec / zvbi.c
index 9600c881b341a832420e22123f056aaa5b10a6b0..b442dd811e27265439da230004aea4a8370ab636 100644 (file)
@@ -123,26 +123,33 @@ struct decoder_sys_t
 {
     vbi_decoder *     p_vbi_dec;
     vbi_dvb_demux *   p_dvb_demux;
-    unsigned int      i_wanted_page;
     unsigned int      i_last_page;
     bool              b_update;
-    bool              b_opaque;
-
-    /* Subtitles as text */
-    bool              b_text;
+    bool              b_text;   /* Subtitles as text */
 
+    vlc_mutex_t       lock; /* Lock to protect the following variables */
     /* Positioning of Teletext images */
     int               i_align;
+    /* */
+    unsigned int      i_wanted_page;
+    /* */
+    bool              b_opaque;
 };
 
 static subpicture_t *Decode( decoder_t *, block_t ** );
 
-static void event_handler( vbi_event *ev, void *user_data );
+static subpicture_t *Subpicture( decoder_t *p_dec, video_format_t *p_fmt,
+                                 bool b_text,
+                                 int i_columns, int i_rows,
+                                 int i_align, mtime_t i_pts );
+
+static void EventHandler( vbi_event *ev, void *user_data );
+static int OpaquePage( picture_t *p_src, const vbi_page p_page,
+                       const video_format_t fmt, bool b_opaque );
+
+/* Properties callbacks */
 static int RequestPage( vlc_object_t *p_this, char const *psz_cmd,
                         vlc_value_t oldval, vlc_value_t newval, void *p_data );
-static int OpaquePage( decoder_t *p_dec, vbi_page p_page, video_format_t fmt,
-                        picture_t **p_src );
-
 static int Opaque( vlc_object_t *p_this, char const *psz_cmd,
                    vlc_value_t oldval, vlc_value_t newval, void *p_data );
 static int Position( vlc_object_t *p_this, char const *psz_cmd,
@@ -171,6 +178,7 @@ static int Open( vlc_object_t *p_this )
     p_sys->b_update = false;
     p_sys->p_vbi_dec = vbi_decoder_new();
     p_sys->p_dvb_demux = vbi_dvb_pes_demux_new( NULL, NULL );
+    vlc_mutex_init( &p_sys->lock );
 
     if( (p_sys->p_vbi_dec == NULL) || (p_sys->p_dvb_demux == NULL) )
     {
@@ -182,7 +190,7 @@ static int Open( vlc_object_t *p_this )
     vbi_event_handler_register( p_sys->p_vbi_dec, VBI_EVENT_TTX_PAGE |
                                 VBI_EVENT_CAPTION | VBI_EVENT_NETWORK |
                                 VBI_EVENT_ASPECT | VBI_EVENT_PROG_INFO,
-                                event_handler, p_dec );
+                                EventHandler, p_dec );
 
     /* Create the var on vlc_global. */
     p_sys->i_wanted_page = var_CreateGetInteger( p_dec, "vbi-page" );
@@ -214,24 +222,34 @@ 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;
 
-    var_Destroy( p_dec, "vbi-opaque" );
-    var_Destroy( p_dec, "vbi-page" );
-    var_DelCallback( p_dec, "vbi-page", RequestPage, p_sys );
+    var_DelCallback( p_dec, "vbi-position", Position, p_sys );
     var_DelCallback( p_dec, "vbi-opaque", Opaque, p_sys );
+    var_DelCallback( p_dec, "vbi-page", RequestPage, p_sys );
 
-    if( p_sys->p_vbi_dec ) vbi_decoder_delete( p_sys->p_vbi_dec );
-    if( p_sys->p_dvb_demux ) vbi_dvb_demux_delete( p_sys->p_dvb_demux );
+    vlc_mutex_destroy( &p_sys->lock );
+
+    if( p_sys->p_vbi_dec )
+        vbi_decoder_delete( p_sys->p_vbi_dec );
+    if( p_sys->p_dvb_demux )
+        vbi_dvb_demux_delete( p_sys->p_dvb_demux );
     free( p_sys );
 }
 
 #define MAX_SLICES 32
 
+#ifdef WORDS_BIGENDIAN
+# define ZVBI_PIXFMT_RGBA32 VBI_PIXFMT_RGBA32_BE
+#else
+# define ZVBI_PIXFMT_RGBA32 VBI_PIXFMT_RGBA32_LE
+#endif
+
+
 /*****************************************************************************
  * Decode:
  *****************************************************************************/
 static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
 {
-    decoder_sys_t   *p_sys = (decoder_sys_t *) p_dec->p_sys;
+    decoder_sys_t   *p_sys = p_dec->p_sys;
     block_t         *p_block;
     subpicture_t    *p_spu = NULL;
     video_format_t  fmt;
@@ -262,92 +280,68 @@ static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
             vbi_decode( p_sys->p_vbi_dec, p_sliced, i_lines, i_pts / 90000.0 );
     }
 
+    /* */
+    vlc_mutex_lock( &p_sys->lock );
+    const int i_align = p_sys->i_align;
+    const unsigned int i_wanted_page = p_sys->i_wanted_page;
+    const bool b_opaque = p_sys->b_opaque;
+    vlc_mutex_unlock( &p_sys->lock );
+
+
     /* Try to see if the page we want is in the cache yet */
     b_cached = vbi_fetch_vt_page( p_sys->p_vbi_dec, &p_page,
-                                  vbi_dec2bcd( p_sys->i_wanted_page ),
+                                  vbi_dec2bcd( i_wanted_page ),
                                   VBI_ANY_SUBNO, VBI_WST_LEVEL_3p5,
                                   25, FALSE );
 
-    if( !b_cached )
+    if( i_wanted_page == p_sys->i_last_page && !p_sys->b_update )
         goto error;
 
-    if( ( p_sys->i_wanted_page == p_sys->i_last_page ) &&
-        ( p_sys->b_update != true ) )
+    if( !b_cached )
+    {
+        if( p_sys->i_last_page != i_wanted_page )
+        {
+            /* We need to reset the subtitle */
+            p_spu = Subpicture( p_dec, &fmt, true,
+                                p_page.columns, p_page.rows,
+                                i_align, p_block->i_pts );
+            if( !p_spu )
+                goto error;
+            p_spu->p_region->psz_text = strdup("");
+
+            p_sys->b_update = true;
+            p_sys->i_last_page = i_wanted_page;
+            goto exit;
+        }
         goto error;
+    }
 
     p_sys->b_update = false;
-    p_sys->i_last_page = p_sys->i_wanted_page;
+    p_sys->i_last_page = i_wanted_page;
 #ifdef ZVBI_DEBUG
     msg_Dbg( p_dec, "we now have page: %d ready for display",
-              p_sys->i_wanted_page );
+             i_wanted_page );
 #endif
     /* If there is a page or sub to render, then we do that here */
     /* Create the subpicture unit */
-    p_spu = p_dec->pf_spu_buffer_new( p_dec );
+    p_spu = Subpicture( p_dec, &fmt, p_sys->b_text,
+                        p_page.columns, p_page.rows,
+                        i_align, p_block->i_pts );
     if( !p_spu )
-    {
-        msg_Warn( p_dec, "can't get spu buffer" );
         goto error;
-    }
-
-    /* Create a new subpicture region */
-    memset( &fmt, 0, sizeof(video_format_t) );
-    fmt.i_chroma = p_sys->b_text ? VLC_FOURCC('T','E','X','T') :
-                                   VLC_FOURCC('R','G','B','A');
-    fmt.i_aspect = p_sys->b_text ? 0 : VOUT_ASPECT_FACTOR;
-    if( !p_sys->b_text )
-    {
-        fmt.i_sar_num = fmt.i_sar_den = 1;
-        fmt.i_width = fmt.i_visible_width = p_page.columns * 12;
-        fmt.i_height = fmt.i_visible_height = p_page.rows * 10;
-    }
-    fmt.i_bits_per_pixel = p_sys->b_text ? 0 : 32;
-    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 == NULL )
-    {
-        msg_Err( p_dec, "cannot allocate SPU region" );
-        goto error;
-    }
-
-    p_spu->p_region->i_x = 0;
-    p_spu->p_region->i_y = 0;
-    p_spu->p_region->i_align = p_sys->i_align;
-
-    /* Normal text subs, easy markup */
-    p_spu->i_flags = SUBPICTURE_ALIGN_BOTTOM;
-
-    p_spu->i_start = (mtime_t) p_block->i_pts;
-    p_spu->i_stop = (mtime_t) 0;
-    p_spu->b_ephemer = true;
-    p_spu->b_absolute = false;
-    p_spu->b_pausable = true;
-    if( !p_sys->b_text )
-    {
-        p_spu->i_width = fmt.i_width;
-        p_spu->i_height = fmt.i_height;
-        p_spu->i_original_picture_width = p_page.columns * 12;
-        p_spu->i_original_picture_height = p_page.rows * 10;
-    }
-
-#ifdef WORDS_BIGENDIAN
-# define ZVBI_PIXFMT_RGBA32 VBI_PIXFMT_RGBA32_BE
-#else
-# define ZVBI_PIXFMT_RGBA32 VBI_PIXFMT_RGBA32_LE
-#endif
 
     if( p_sys->b_text )
     {
         unsigned int i_textsize = 7000;
         int i_total;
-        char p_text[7000];
+        char p_text[i_textsize+1];
 
         i_total = vbi_print_page_region( &p_page, p_text, i_textsize,
                         "UTF-8", 0, 0, 0, 0, p_page.columns, p_page.rows );
         p_text[i_total] = '\0';
         /* Strip off the pagenumber */
-        if( i_total <= 40 ) goto error;
+        if( i_total <= 40 )
+            goto error;
         p_spu->p_region->psz_text = strdup( &p_text[8] );
 
 #ifdef ZVBI_DEBUG
@@ -356,20 +350,17 @@ static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
     }
     else
     {
-        picture_t *p_pic;
+        picture_t *p_pic = &p_spu->p_region->picture;
 
+        /* ZVBI is stupid enough to assume pitch == width */
+        p_pic->p->i_pitch = 4 * fmt.i_width;
         vbi_draw_vt_page( &p_page, ZVBI_PIXFMT_RGBA32,
                           p_spu->p_region->picture.p->p_pixels, 1, 1 );
 
-        p_spu->p_region->picture.p->i_lines = p_page.rows * 10;
-        p_spu->p_region->picture.p->i_pitch = p_page.columns * 12 * 4;
-
-        p_pic = &(p_spu->p_region->picture);
-        OpaquePage( p_dec, p_page, fmt, &p_pic );
+        OpaquePage( p_pic, p_page, fmt, b_opaque );
     }
 
-#undef ZVBI_PIXFMT_RGBA32
-
+exit:
     vbi_unref_page( &p_page );
     block_Release( p_block );
     return p_spu;
@@ -386,7 +377,76 @@ error:
     return NULL;
 }
 
-static void event_handler( vbi_event *ev, void *user_data )
+static subpicture_t *Subpicture( decoder_t *p_dec, video_format_t *p_fmt,
+                                 bool b_text,
+                                 int i_columns, int i_rows, int i_align,
+                                 mtime_t i_pts )
+{
+    video_format_t fmt;
+    subpicture_t *p_spu;
+
+    /* If there is a page or sub to render, then we do that here */
+    /* Create the subpicture unit */
+    p_spu = p_dec->pf_spu_buffer_new( p_dec );
+    if( !p_spu )
+    {
+        msg_Warn( p_dec, "can't get spu buffer" );
+        return NULL;
+    }
+
+    memset( &fmt, 0, sizeof(video_format_t) );
+    fmt.i_chroma = b_text ? VLC_FOURCC('T','E','X','T') :
+                                   VLC_FOURCC('R','G','B','A');
+    fmt.i_aspect = b_text ? 0 : VOUT_ASPECT_FACTOR;
+    if( b_text )
+    {
+        fmt.i_bits_per_pixel = 0;
+    }
+    else
+    {
+        fmt.i_sar_num = fmt.i_sar_den = 1;
+        fmt.i_width = fmt.i_visible_width = i_columns * 12;
+        fmt.i_height = fmt.i_visible_height = i_rows * 10;
+        fmt.i_bits_per_pixel = 32;
+    }
+    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 == NULL )
+    {
+        msg_Err( p_dec, "cannot allocate SPU region" );
+        p_dec->pf_spu_buffer_del( p_dec, p_spu );
+        return NULL;
+    }
+
+    p_spu->p_region->i_x = 0;
+    p_spu->p_region->i_y = 0;
+    p_spu->p_region->i_align = i_align;
+
+    /* Normal text subs, easy markup */
+    p_spu->i_flags = SUBPICTURE_ALIGN_BOTTOM;
+
+    p_spu->i_start = i_pts;
+    p_spu->i_stop = 0;
+    p_spu->b_ephemer = true;
+    p_spu->b_absolute = false;
+    p_spu->b_pausable = true;
+
+
+    if( !b_text )
+    {
+        p_spu->i_width =
+        p_spu->i_original_picture_width = fmt.i_width;
+        p_spu->i_height =
+        p_spu->i_original_picture_height = fmt.i_height;
+    }
+
+    /* */
+    *p_fmt = fmt;
+    return p_spu;
+}
+
+static void EventHandler( vbi_event *ev, void *user_data )
 {
     decoder_t *p_dec        = (decoder_t *)user_data;
     decoder_sys_t *p_sys    = p_dec->p_sys;
@@ -418,74 +478,59 @@ static void event_handler( vbi_event *ev, void *user_data )
         msg_Dbg( p_dec, "Program info received" );
 }
 
-static int OpaquePage( decoder_t *p_dec, vbi_page p_page,
-                       video_format_t fmt, picture_t **p_src )
+static int OpaquePage( picture_t *p_src, const vbi_page p_page,
+                       const video_format_t fmt, bool b_opaque )
 {
-    decoder_sys_t   *p_sys = (decoder_sys_t *) p_dec->p_sys;
-    uint32_t        *p_begin, *p_end;
-    unsigned int    x = 0, y = 0;
-    vbi_opacity     opacity;
+    unsigned int    x, y;
 
     assert( fmt.i_chroma == VLC_FOURCC('R','G','B','A' ) );
 
     /* Kludge since zvbi doesn't provide an option to specify opacity. */
-    switch( fmt.i_chroma )
+    for( y = 0; y < fmt.i_height; y++ )
     {
-        case VLC_FOURCC('R','G','B','A' ):
-            p_begin = (uint32_t *)(*p_src)->p->p_pixels;
-            p_end   = (uint32_t *)(*p_src)->p->p_pixels +
-                    ( fmt.i_width * fmt.i_height );
-            break;
-        default:
-            msg_Err( p_dec, "chroma not supported %4.4s", (char *)&fmt.i_chroma );
-            return VLC_EGENERIC;
-    }
-
-    for( ; p_begin < p_end; p_begin++ )
-    {
-        opacity = p_page.text[ y / 10 * p_page.columns + x / 12 ].opacity;
-        switch( opacity )
+        for( x = 0; x < fmt.i_width; x++ )
         {
-        /* Show video instead of this character */
-        case VBI_TRANSPARENT_SPACE:
-            *p_begin = 0;
-            break;
-        /* To make the boxed text "closed captioning" transparent
-         * change true to false.
-         */
-        case VBI_OPAQUE:
-            if( p_sys->b_opaque )
+            const vbi_opacity opacity = p_page.text[ y/10 * p_page.columns + x/12 ].opacity;
+            uint32_t *p_pixel = (uint32_t*)&p_src->p->p_pixels[y * p_src->p->i_pitch + 4*x];
+            switch( opacity )
+            {
+            /* Show video instead of this character */
+            case VBI_TRANSPARENT_SPACE:
+                *p_pixel = 0;
                 break;
-        /* Full text transparency. only foreground color is show */
-        case VBI_TRANSPARENT_FULL:
-            *p_begin = 0;
-            break;
-        /* Transparency for boxed text */
-        case VBI_SEMI_TRANSPARENT:
-            if( (*p_begin & 0xffffff00) == 0xff )
-                *p_begin = 0;
-            break;
-        }
-        x++;
-        if( x >= fmt.i_width )
-        {
-            x = 0;
-            y++;
+            /* To make the boxed text "closed captioning" transparent
+             * change true to false.
+             */
+            case VBI_OPAQUE:
+                if( b_opaque )
+                    break;
+            /* Full text transparency. only foreground color is show */
+            case VBI_TRANSPARENT_FULL:
+                *p_pixel = 0;
+                break;
+            /* Transparency for boxed text */
+            case VBI_SEMI_TRANSPARENT:
+                if( (*p_pixel & 0xffffff00) == 0xff ) /* FIXME cannot be true ? */
+                    *p_pixel = 0;
+                break;
+            }
         }
     }
     /* end of kludge */
     return VLC_SUCCESS;
-
 }
 
+/* Callbacks */
 static int RequestPage( vlc_object_t *p_this, char const *psz_cmd,
                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
 {
-    decoder_sys_t   *p_sys = p_data;
+    decoder_sys_t *p_sys = p_data;
     VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
 
-    if( (newval.i_int > 0) && (newval.i_int < 999) )
+    vlc_mutex_lock( &p_sys->lock );
+    if( newval.i_int > 0 && newval.i_int < 999 )
         p_sys->i_wanted_page = newval.i_int;
+    vlc_mutex_unlock( &p_sys->lock );
 
     return VLC_SUCCESS;
 }
@@ -496,8 +541,10 @@ static int Opaque( vlc_object_t *p_this, char const *psz_cmd,
     decoder_sys_t *p_sys = p_data;
     VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
 
-    if( p_sys )
-        p_sys->b_opaque = newval.b_bool;
+    vlc_mutex_lock( &p_sys->lock );
+    p_sys->b_opaque = newval.b_bool;
+    vlc_mutex_unlock( &p_sys->lock );
+
     return VLC_SUCCESS;
 }
 
@@ -507,7 +554,9 @@ static int Position( vlc_object_t *p_this, char const *psz_cmd,
     decoder_sys_t *p_sys = p_data;
     VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
 
-    if( p_sys )
-        p_sys->i_align = newval.i_int;
+    vlc_mutex_lock( &p_sys->lock );
+    p_sys->i_align = newval.i_int;
+    vlc_mutex_unlock( &p_sys->lock );
+
     return VLC_SUCCESS;
 }