]> git.sesse.net Git - vlc/blobdiff - modules/codec/zvbi.c
Simplify FLAC extradata (streaminfo) parsing
[vlc] / modules / codec / zvbi.c
index 4e5834bee7550324abd54ab3e221b95b95481887..62862ce5bebef26f67ad244396e5c77414a997eb 100644 (file)
@@ -7,20 +7,21 @@
  * Authors: Derk-Jan Hartman <djhartman at m2x dot nl>
  *          Jean-Paul Saman <jpsaman at m2x dot nl>
  *
- * 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.
  *****************************************************************************/
+
 /*****************************************************************************
  *
  * information on teletext format can be found here :
@@ -47,7 +48,6 @@
 #include <libzvbi.h>
 
 #include <vlc_codec.h>
-#include <vlc_vout_osd.h>
 
 /*****************************************************************************
  * Module descriptor.
@@ -59,7 +59,7 @@ static void Close( vlc_object_t * );
 #define PAGE_LONGTEXT N_("Open the indicated Teletext page." \
         "Default page is index 100")
 
-#define OPAQUE_TEXT N_("Text is always opaque")
+#define OPAQUE_TEXT N_("Teletext transparency")
 #define OPAQUE_LONGTEXT N_("Setting vbi-opaque to false " \
         "makes the boxed text transparent." )
 
@@ -104,18 +104,20 @@ vlc_module_end ()
 
 //Guessing table for missing "default region triplet"
 static const int pi_default_triplet[] = {
- 0, 0,           // slo cze
+ 0, 0, 0, 0,     // slo slk cze ces
  8,              // pol
- 24,24,24,24,    //ssc scr slv rum
+ 24,24,24,24,24, //scc scr srp hrv slv
+ 24,24,          //rum ron
  32,32,32,32,32, //est lit rus bul ukr
  48,48,          //gre ell
  64,             //ara
  88,             //heb
  16 };           //default
 static const char *const ppsz_default_triplet[] = {
- "slo", "cze",
+ "slo", "slk", "cze", "ces",
  "pol",
- "ssc", "scr", "slv", "rum",
+ "scc", "scr", "srp", "hrv", "slv",
+ "rum", "ron",
  "est", "lit", "rus", "bul", "ukr",
  "gre", "ell",
  "ara",
@@ -148,10 +150,12 @@ typedef enum {
     DATA_UNIT_STUFFING                      = 0xFF,
 } data_unit_id;
 
+#define MAX_SLICES 32
+
 struct decoder_sys_t
 {
     vbi_decoder *     p_vbi_dec;
-    vbi_dvb_demux *   p_dvb_demux;
+    vbi_sliced        p_vbi_sliced[MAX_SLICES];
     unsigned int      i_last_page;
     bool              b_update;
     bool              b_text;   /* Subtitles as text */
@@ -179,7 +183,9 @@ static subpicture_t *Subpicture( decoder_t *p_dec, video_format_t *p_fmt,
 
 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 );
+                       const video_format_t fmt, bool b_opaque, const int text_offset );
+static int get_first_visible_row( vbi_char *p_text, int rows, int columns);
+static int get_last_visible_row( vbi_char *p_text, int rows, int columns);
 
 /* Properties callbacks */
 static int RequestPage( vlc_object_t *p_this, char const *psz_cmd,
@@ -213,12 +219,11 @@ static int Open( vlc_object_t *p_this )
     p_sys->i_key[0] = p_sys->i_key[1] = p_sys->i_key[2] = '*' - '0';
     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) )
+    if( p_sys->p_vbi_dec == NULL )
     {
-        msg_Err( p_dec, "VBI decoder/demux could not be created." );
+        msg_Err( p_dec, "VBI decoder could not be created." );
         Close( p_this );
         return VLC_ENOMEM;
     }
@@ -293,20 +298,15 @@ static void Close( vlc_object_t *p_this )
 
     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:
  *****************************************************************************/
@@ -318,8 +318,6 @@ static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
     video_format_t  fmt;
     bool            b_cached = false;
     vbi_page        p_page;
-    const uint8_t   *p_pos;
-    unsigned int    i_left;
 
     if( (pp_block == NULL) || (*pp_block == NULL) )
         return NULL;
@@ -327,20 +325,44 @@ static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
     p_block = *pp_block;
     *pp_block = NULL;
 
-    p_pos = p_block->p_buffer;
-    i_left = p_block->i_buffer;
-
-    while( i_left > 0 )
+    if( p_block->i_buffer > 0 &&
+        ( ( p_block->p_buffer[0] >= 0x10 && p_block->p_buffer[0] <= 0x1f ) ||
+          ( p_block->p_buffer[0] >= 0x99 && p_block->p_buffer[0] <= 0x9b ) ) )
     {
-        vbi_sliced      p_sliced[MAX_SLICES];
-        unsigned int    i_lines = 0;
-        int64_t         i_pts;
+        vbi_sliced   *p_sliced = p_sys->p_vbi_sliced;
+        unsigned int i_lines = 0;
+
+        p_block->i_buffer--;
+        p_block->p_buffer++;
+        while( p_block->i_buffer >= 2 )
+        {
+            int      i_id   = p_block->p_buffer[0];
+            unsigned i_size = p_block->p_buffer[1];
+
+            if( 2 + i_size > p_block->i_buffer )
+                break;
 
-        i_lines = vbi_dvb_demux_cor( p_sys->p_dvb_demux, p_sliced,
-                                     MAX_SLICES, &i_pts, &p_pos, &i_left );
+            if( ( i_id == 0x02 || i_id == 0x03 ) && i_size >= 44 && i_lines < MAX_SLICES )
+            {
+                unsigned line_offset  = p_block->p_buffer[2] & 0x1f;
+                unsigned field_parity = p_block->p_buffer[2] & 0x20;
+
+                p_sliced[i_lines].id = VBI_SLICED_TELETEXT_B;
+                if( line_offset > 0 )
+                    p_sliced[i_lines].line = line_offset + (field_parity ? 0 : 313);
+                else
+                    p_sliced[i_lines].line = 0;
+                for( int i = 0; i < 42; i++ )
+                    p_sliced[i_lines].data[i] = vbi_rev8( p_block->p_buffer[4 + i] );
+                i_lines++;
+            }
+
+            p_block->i_buffer -= 2 + i_size;
+            p_block->p_buffer += 2 + i_size;
+        }
 
         if( i_lines > 0 )
-            vbi_decode( p_sys->p_vbi_dec, p_sliced, i_lines, i_pts / 90000.0 );
+            vbi_decode( p_sys->p_vbi_dec, p_sliced, i_lines, (double)p_block->i_pts / 1000000 );
     }
 
     /* */
@@ -386,10 +408,21 @@ static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
     msg_Dbg( p_dec, "we now have page: %d ready for display",
              i_wanted_page );
 #endif
+
+    /* Ignore transparent rows at the beginning and end */
+    int i_first_row = get_first_visible_row( p_page.text, p_page.rows, p_page.columns );
+    if ( i_first_row < 0 )
+        goto error;
+    int i_num_rows = get_last_visible_row( p_page.text, p_page.rows, p_page.columns ) - i_first_row + 1;
+#ifdef ZVBI_DEBUG
+    msg_Dbg( p_dec, "After top and tail of page we have rows %i-%i of %i",
+             i_first_row + 1, i_first_row + i_num_rows, p_page.rows );
+#endif
+
     /* If there is a page or sub to render, then we do that here */
     /* Create the subpicture unit */
     p_spu = Subpicture( p_dec, &fmt, p_sys->b_text,
-                        p_page.columns, p_page.rows,
+                        p_page.columns, i_num_rows,
                         i_align, p_block->i_pts );
     if( !p_spu )
         goto error;
@@ -401,13 +434,9 @@ static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
         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 );
+                        "UTF-8", 0, 0, 0, i_first_row, p_page.columns, i_num_rows );
         p_text[i_total] = '\0';
-        /* Strip off the pagenumber */
-        if( i_total <= 40 )
-            goto error;
-        p_spu->p_region->psz_text = strdup( &p_text[8] );
-
+        p_spu->p_region->psz_text = strdup( p_text );
 #ifdef ZVBI_DEBUG
         msg_Info( p_dec, "page %x-%x(%d)\n%s", p_page.pgno, p_page.subno, i_total, p_text );
 #endif
@@ -418,14 +447,22 @@ static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
 
         /* 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->p_picture->p->p_pixels, 1, 1 );
+
+        /* Maintain subtitle postion */
+        p_spu->p_region->i_y = i_first_row*10;
+        p_spu->i_original_picture_width = p_page.columns*12;
+        p_spu->i_original_picture_height = p_page.rows*10;
+
+        vbi_draw_vt_page_region( &p_page, ZVBI_PIXFMT_RGBA32,
+                          p_spu->p_region->p_picture->p->p_pixels, -1,
+                          0, i_first_row, p_page.columns, i_num_rows,
+                          1, 1);
 
         vlc_mutex_lock( &p_sys->lock );
         memcpy( p_sys->nav_link, &p_page.nav_link, sizeof( p_sys->nav_link )) ;
         vlc_mutex_unlock( &p_sys->lock );
 
-        OpaquePage( p_pic, p_page, fmt, b_opaque );
+        OpaquePage( p_pic, p_page, fmt, b_opaque, i_first_row * p_page.columns );
     }
 
 exit:
@@ -463,18 +500,15 @@ static subpicture_t *Subpicture( decoder_t *p_dec, video_format_t *p_fmt,
     }
 
     memset( &fmt, 0, sizeof(video_format_t) );
-    fmt.i_chroma = b_text ? VLC_CODEC_TEXT :
-                                   VLC_CODEC_RGBA;
+    fmt.i_chroma = b_text ? VLC_CODEC_TEXT : VLC_CODEC_RGBA;
+    fmt.i_sar_num = 0;
+    fmt.i_sar_den = 1;
     if( b_text )
     {
         fmt.i_bits_per_pixel = 0;
-        fmt.i_sar_num = 0;
-        fmt.i_sar_den = 0;
     }
     else
     {
-        fmt.i_sar_num = 1;
-        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;
@@ -496,7 +530,7 @@ static subpicture_t *Subpicture( decoder_t *p_dec, video_format_t *p_fmt,
     p_spu->i_start = i_pts;
     p_spu->i_stop = 0;
     p_spu->b_ephemer = true;
-    p_spu->b_absolute = false;
+    p_spu->b_absolute = true;
 
     if( !b_text )
     {
@@ -550,8 +584,34 @@ static void EventHandler( vbi_event *ev, void *user_data )
         msg_Dbg( p_dec, "Network ID changed" );
 }
 
+static int get_first_visible_row( vbi_char *p_text, int rows, int columns)
+{
+    for ( int i = 0; i < rows * columns; i++ )
+    {
+        if ( p_text[i].opacity != VBI_TRANSPARENT_SPACE )
+        {
+            return i / columns;
+        }
+    }
+
+    return rows;
+}
+
+static int get_last_visible_row( vbi_char *p_text, int rows, int columns)
+{
+    for ( int i = rows * columns - 1; i >= 0; i-- )
+    {
+        if (p_text[i].opacity != VBI_TRANSPARENT_SPACE)
+        {
+            return ( i + columns - 1) / columns;
+        }
+    }
+
+    return 0;
+}
+
 static int OpaquePage( picture_t *p_src, const vbi_page p_page,
-                       const video_format_t fmt, bool b_opaque )
+                       const video_format_t fmt, bool b_opaque, const int text_offset )
 {
     unsigned int    x, y;
 
@@ -562,8 +622,8 @@ static int OpaquePage( picture_t *p_src, const vbi_page p_page,
     {
         for( x = 0; x < fmt.i_width; x++ )
         {
-            const vbi_opacity opacity = p_page.text[ y/10 * p_page.columns + x/12 ].opacity;
-            const int background = p_page.text[ y/10 * p_page.columns + x/12 ].background;
+            const vbi_opacity opacity = p_page.text[ text_offset + y/10 * p_page.columns + x/12 ].opacity;
+            const int background = p_page.text[ text_offset + y/10 * p_page.columns + x/12 ].background;
             uint32_t *p_pixel = (uint32_t*)&p_src->p->p_pixels[y * p_src->p->i_pitch + 4*x];
 
             switch( opacity )
@@ -661,21 +721,13 @@ static int Position( vlc_object_t *p_this, char const *psz_cmd,
     return VLC_SUCCESS;
 }
 
-#include <vlc_vout.h>
-#define OSDMessage(dec, fmt, ...) do { \
-    vout_thread_t *p_vout = vlc_object_find( dec, VLC_OBJECT_VOUT, FIND_ANYWHERE ); \
-    if( p_vout ) { \
-        vout_OSDMessage( p_vout, fmt, __VA_ARGS__ ); \
-        vlc_object_release( p_vout ); \
-    } } while(0)
-
 static int EventKey( vlc_object_t *p_this, char const *psz_cmd,
                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
 {
     decoder_t *p_dec = p_data;
     decoder_sys_t *p_sys = p_dec->p_sys;
 
-    VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
+    VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED( p_this );
 
     /* FIXME: Capture + and - key for subpage browsing */
     if( newval.i_int == '-' || newval.i_int == '+' )
@@ -691,7 +743,8 @@ static int EventKey( vlc_object_t *p_this, char const *psz_cmd,
         if ( !vbi_bcd_digits_greater( p_sys->i_wanted_subpage, 0x00 ) || vbi_bcd_digits_greater( p_sys->i_wanted_subpage, 0x99 ) )
                 p_sys->i_wanted_subpage = VBI_ANY_SUBNO;
         else
-            OSDMessage( p_this, SPU_DEFAULT_CHANNEL, "%s: %d", _("Subpage"), vbi_bcd2dec( p_sys->i_wanted_subpage) );
+            msg_Info( p_dec, "subpage: %d",
+                      vbi_bcd2dec( p_sys->i_wanted_subpage) );
 
         p_sys->b_update = true;
         vlc_mutex_unlock( &p_sys->lock );
@@ -705,7 +758,8 @@ static int EventKey( vlc_object_t *p_this, char const *psz_cmd,
     p_sys->i_key[0] = p_sys->i_key[1];
     p_sys->i_key[1] = p_sys->i_key[2];
     p_sys->i_key[2] = (int)(newval.i_int - '0');
-    OSDMessage( p_this, SPU_DEFAULT_CHANNEL, "%s: %c%c%c", _("Page"), (char)(p_sys->i_key[0]+'0'), (char)(p_sys->i_key[1]+'0'), (char)(p_sys->i_key[2]+'0') );
+    msg_Info( p_dec, "page: %c%c%c", (char)(p_sys->i_key[0]+'0'),
+              (char)(p_sys->i_key[1]+'0'), (char)(p_sys->i_key[2]+'0') );
 
     int i_new_page = 0;