]> git.sesse.net Git - vlc/blob - modules/codec/zvbi.c
zvbi: fix subpage "down"-key to not alternate between "signaled" and "subpage 1"
[vlc] / modules / codec / zvbi.c
1 /*****************************************************************************
2  * zvbi.c : VBI and Teletext PES demux and decoder using libzvbi
3  *****************************************************************************
4  * Copyright (C) 2007, M2X
5  * $Id$
6  *
7  * Authors: Derk-Jan Hartman <djhartman at m2x dot nl>
8  *          Jean-Paul Saman <jpsaman at m2x dot nl>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24 /*****************************************************************************
25  *
26  * information on teletext format can be found here :
27  * http://pdc.ro.nu/teletext.html
28  *
29  *****************************************************************************/
30
31 /* This module implements:
32  * ETSI EN 301 775: VBI data in PES
33  * ETSI EN 300 472: EBU Teletext data in PES
34  * ETSI EN 300 706: Enhanced Teletext (libzvbi)
35  * ETSI EN 300 231: Video Programme System [VPS] (libzvbi)
36  * ETSI EN 300 294: 625-line Wide Screen Signaling [WSS] (libzvbi)
37  * EIA-608 Revision A: Closed Captioning [CC] (libzvbi)
38  */
39
40 #ifdef HAVE_CONFIG_H
41 # include "config.h"
42 #endif
43
44 #include <vlc_common.h>
45 #include <vlc_plugin.h>
46 #include <assert.h>
47 #include <libzvbi.h>
48
49 #include <vlc_vout.h>
50 #include <vlc_codec.h>
51 #include <vlc_osd.h>
52
53 /*****************************************************************************
54  * Module descriptor.
55  *****************************************************************************/
56 static int  Open ( vlc_object_t * );
57 static void Close( vlc_object_t * );
58
59 #define PAGE_TEXT N_("Teletext page")
60 #define PAGE_LONGTEXT N_("Open the indicated Teletext page." \
61         "Default page is index 100")
62
63 #define OPAQUE_TEXT N_("Text is always opaque")
64 #define OPAQUE_LONGTEXT N_("Setting vbi-opaque to false " \
65         "makes the boxed text transparent." )
66
67 #define POS_TEXT N_("Teletext alignment")
68 #define POS_LONGTEXT N_( \
69   "You can enforce the teletext position on the video " \
70   "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
71   "also use combinations of these values, eg. 6 = top-right).")
72
73 #define TELX_TEXT N_("Teletext text subtitles")
74 #define TELX_LONGTEXT N_( "Output teletext subtitles as text " \
75   "instead of as RGBA" )
76
77 static const int pi_pos_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
78 static const char *const ppsz_pos_descriptions[] =
79 { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
80   N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
81
82 vlc_module_begin();
83     set_description( N_("VBI and Teletext decoder") );
84     set_shortname( N_("VBI & Teletext") );
85     set_capability( "decoder", 51 );
86     set_category( CAT_INPUT );
87     set_subcategory( SUBCAT_INPUT_SCODEC );
88     set_callbacks( Open, Close );
89
90     add_integer( "vbi-page", 100, NULL,
91                  PAGE_TEXT, PAGE_LONGTEXT, false );
92     add_bool( "vbi-opaque", true, NULL,
93                  OPAQUE_TEXT, OPAQUE_LONGTEXT, false );
94     add_integer( "vbi-position", 4, NULL, POS_TEXT, POS_LONGTEXT, false );
95         change_integer_list( pi_pos_values, ppsz_pos_descriptions, NULL );
96     add_bool( "vbi-text", false, NULL,
97               TELX_TEXT, TELX_LONGTEXT, false );
98 vlc_module_end();
99
100 /****************************************************************************
101  * Local structures
102  ****************************************************************************/
103
104 // #define ZVBI_DEBUG
105
106 //Guessing table for missing "default region triplet"
107 static const int pi_default_triplet[] = {
108  0, 0,           // slo cze
109  8,              // pol
110  24,24,24,24,    //ssc scr slv rum
111  32,32,32,32,32, //est lit rus bul ukr
112  48,48,          //gre ell
113  64,             //ara
114  88,             //heb
115  16 };           //default
116 static const char *const ppsz_default_triplet[] = {
117  "slo", "cze",
118  "pol",
119  "ssc", "scr", "slv", "rum",
120  "est", "lit", "rus", "bul", "ukr",
121  "gre", "ell",
122  "ara",
123  "heb",
124  NULL
125 };
126
127 typedef enum {
128     ZVBI_KEY_RED    = 'r' << 16,
129     ZVBI_KEY_GREEN  = 'g' << 16,
130     ZVBI_KEY_YELLOW = 'y' << 16,
131     ZVBI_KEY_BLUE   = 'b' << 16,
132     ZVBI_KEY_INDEX  = 'i' << 16,
133 } ttxt_key_id;
134
135 typedef enum {
136     DATA_UNIT_EBU_TELETEXT_NON_SUBTITLE     = 0x02,
137     DATA_UNIT_EBU_TELETEXT_SUBTITLE         = 0x03,
138     DATA_UNIT_EBU_TELETEXT_INVERTED         = 0x0C,
139
140     DATA_UNIT_ZVBI_WSS_CPR1204              = 0xB4,
141     DATA_UNIT_ZVBI_CLOSED_CAPTION_525       = 0xB5,
142     DATA_UNIT_ZVBI_MONOCHROME_SAMPLES_525   = 0xB6,
143
144     DATA_UNIT_VPS                           = 0xC3,
145     DATA_UNIT_WSS                           = 0xC4,
146     DATA_UNIT_CLOSED_CAPTION                = 0xC5,
147     DATA_UNIT_MONOCHROME_SAMPLES            = 0xC6,
148
149     DATA_UNIT_STUFFING                      = 0xFF,
150 } data_unit_id;
151
152 struct decoder_sys_t
153 {
154     vbi_decoder *     p_vbi_dec;
155     vbi_dvb_demux *   p_dvb_demux;
156     unsigned int      i_last_page;
157     bool              b_update;
158     bool              b_text;   /* Subtitles as text */
159
160     vlc_mutex_t       lock; /* Lock to protect the following variables */
161     /* Positioning of Teletext images */
162     int               i_align;
163     /* */
164     unsigned int      i_wanted_page;
165     unsigned int      i_wanted_subpage;
166     /* */
167     bool              b_opaque;
168     struct {
169         int pgno, subno;
170     }                 nav_link[6];
171     int               i_key[3];
172 };
173
174 static subpicture_t *Decode( decoder_t *, block_t ** );
175
176 static subpicture_t *Subpicture( decoder_t *p_dec, video_format_t *p_fmt,
177                                  bool b_text,
178                                  int i_columns, int i_rows,
179                                  int i_align, mtime_t i_pts );
180
181 static void EventHandler( vbi_event *ev, void *user_data );
182 static int OpaquePage( picture_t *p_src, const vbi_page p_page,
183                        const video_format_t fmt, bool b_opaque );
184
185 /* Properties callbacks */
186 static int RequestPage( vlc_object_t *p_this, char const *psz_cmd,
187                         vlc_value_t oldval, vlc_value_t newval, void *p_data );
188 static int Opaque( vlc_object_t *p_this, char const *psz_cmd,
189                    vlc_value_t oldval, vlc_value_t newval, void *p_data );
190 static int Position( vlc_object_t *p_this, char const *psz_cmd,
191                      vlc_value_t oldval, vlc_value_t newval, void *p_data );
192 static int EventKey( vlc_object_t *p_this, char const *psz_cmd,
193                      vlc_value_t oldval, vlc_value_t newval, void *p_data );
194
195 /*****************************************************************************
196  * Open: probe the decoder and return score
197  *****************************************************************************
198  * Tries to launch a decoder and return score so that the interface is able
199  * to chose.
200  *****************************************************************************/
201 static int Open( vlc_object_t *p_this )
202 {
203     decoder_t     *p_dec = (decoder_t *) p_this;
204     decoder_sys_t *p_sys = NULL;
205
206     if( p_dec->fmt_in.i_codec != VLC_FOURCC('t','e','l','x') )
207         return VLC_EGENERIC;
208
209     p_dec->pf_decode_sub = Decode;
210     p_sys = p_dec->p_sys = malloc( sizeof(decoder_sys_t) );
211     if( p_sys == NULL )
212         return VLC_ENOMEM;
213     memset( p_sys, 0, sizeof(decoder_sys_t) );
214
215     p_sys->i_key[0] = p_sys->i_key[1] = p_sys->i_key[2] = '*' - '0';
216     p_sys->b_update = false;
217     p_sys->p_vbi_dec = vbi_decoder_new();
218     p_sys->p_dvb_demux = vbi_dvb_pes_demux_new( NULL, NULL );
219     vlc_mutex_init( &p_sys->lock );
220
221     if( (p_sys->p_vbi_dec == NULL) || (p_sys->p_dvb_demux == NULL) )
222     {
223         msg_Err( p_dec, "VBI decoder/demux could not be created." );
224         Close( p_this );
225         return VLC_ENOMEM;
226     }
227
228     /* Some broadcasters in countries with level 1 and level 1.5 still not send a G0 to do 
229      * matches against table 32 of ETSI 300 706. We try to do some best effort guessing
230      * This is not perfect, but might handle some cases where we know the vbi language 
231      * is known. It would be better if people started sending G0 */
232     for( int i = 0; ppsz_default_triplet[i] != NULL; i++ )
233     {
234         if( p_dec->fmt_in.psz_language && !strcasecmp( p_dec->fmt_in.psz_language, ppsz_default_triplet[i] ) )
235         {
236             vbi_teletext_set_default_region( p_sys->p_vbi_dec, pi_default_triplet[i]);
237             msg_Dbg( p_dec, "overwriting default zvbi region: %d", pi_default_triplet[i] );
238         }
239     }
240
241     vbi_event_handler_register( p_sys->p_vbi_dec, VBI_EVENT_TTX_PAGE | VBI_EVENT_NETWORK |
242 #ifdef ZVBI_DEBUG
243                                 VBI_EVENT_CAPTION | VBI_EVENT_TRIGGER |
244                                 VBI_EVENT_ASPECT | VBI_EVENT_PROG_INFO | VBI_EVENT_NETWORK_ID |
245 #endif
246                                 0 , EventHandler, p_dec );
247
248     /* Create the var on vlc_global. */
249     p_sys->i_wanted_page = var_CreateGetInteger( p_dec, "vbi-page" );
250     var_AddCallback( p_dec, "vbi-page",
251                      RequestPage, p_sys );
252
253     /* Check if the Teletext track has a known "initial page". */
254     if( p_sys->i_wanted_page == 100 && p_dec->fmt_in.subs.dvb.i_id != -1 )
255     {
256         int i_wanted_magazine = p_dec->fmt_in.subs.dvb.i_id >> 16;
257         if( i_wanted_magazine == 0 )
258             i_wanted_magazine = 8;
259         p_sys->i_wanted_page = vbi_bcd2dec(p_dec->fmt_in.subs.dvb.i_id & 0xff);
260         p_sys->i_wanted_page += 100*i_wanted_magazine;
261     }
262     p_sys->i_wanted_subpage = VBI_ANY_SUBNO;
263
264     p_sys->b_opaque = var_CreateGetBool( p_dec, "vbi-opaque" );
265     var_AddCallback( p_dec, "vbi-opaque", Opaque, p_sys );
266
267     p_sys->i_align = var_CreateGetInteger( p_dec, "vbi-position" );
268     var_AddCallback( p_dec, "vbi-position", Position, p_sys );
269
270     p_sys->b_text = var_CreateGetBool( p_dec, "vbi-text" );
271 //    var_AddCallback( p_dec, "vbi-text", Text, p_sys );
272
273     /* Listen for keys */
274     var_AddCallback( p_dec->p_libvlc, "key-pressed", EventKey, p_sys );
275
276     es_format_Init( &p_dec->fmt_out, SPU_ES, VLC_FOURCC( 's','p','u',' ' ) );
277     if( p_sys->b_text )
278         p_dec->fmt_out.video.i_chroma = VLC_FOURCC('T','E','X','T');
279     else
280         p_dec->fmt_out.video.i_chroma = VLC_FOURCC('R','G','B','A');
281     return VLC_SUCCESS;
282 }
283
284 /*****************************************************************************
285  * Close:
286  *****************************************************************************/
287 static void Close( vlc_object_t *p_this )
288 {
289     decoder_t     *p_dec = (decoder_t*) p_this;
290     decoder_sys_t *p_sys = p_dec->p_sys;
291
292     var_DelCallback( p_dec, "vbi-position", Position, p_sys );
293     var_DelCallback( p_dec, "vbi-opaque", Opaque, p_sys );
294     var_DelCallback( p_dec, "vbi-page", RequestPage, p_sys );
295     var_DelCallback( p_dec->p_libvlc, "key-pressed", EventKey, p_sys );
296
297     vlc_mutex_destroy( &p_sys->lock );
298
299     if( p_sys->p_vbi_dec )
300         vbi_decoder_delete( p_sys->p_vbi_dec );
301     if( p_sys->p_dvb_demux )
302         vbi_dvb_demux_delete( p_sys->p_dvb_demux );
303     free( p_sys );
304 }
305
306 #define MAX_SLICES 32
307
308 #ifdef WORDS_BIGENDIAN
309 # define ZVBI_PIXFMT_RGBA32 VBI_PIXFMT_RGBA32_BE
310 #else
311 # define ZVBI_PIXFMT_RGBA32 VBI_PIXFMT_RGBA32_LE
312 #endif
313
314
315 /*****************************************************************************
316  * Decode:
317  *****************************************************************************/
318 static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
319 {
320     decoder_sys_t   *p_sys = p_dec->p_sys;
321     block_t         *p_block;
322     subpicture_t    *p_spu = NULL;
323     video_format_t  fmt;
324     bool            b_cached = false;
325     vbi_page        p_page;
326     const uint8_t   *p_pos;
327     unsigned int    i_left;
328
329     if( (pp_block == NULL) || (*pp_block == NULL) )
330         return NULL;
331
332     p_block = *pp_block;
333     *pp_block = NULL;
334
335     p_pos = p_block->p_buffer;
336     i_left = p_block->i_buffer;
337
338     while( i_left > 0 )
339     {
340         vbi_sliced      p_sliced[MAX_SLICES];
341         unsigned int    i_lines = 0;
342         int64_t         i_pts;
343
344         i_lines = vbi_dvb_demux_cor( p_sys->p_dvb_demux, p_sliced,
345                                      MAX_SLICES, &i_pts, &p_pos, &i_left );
346
347         if( i_lines > 0 )
348             vbi_decode( p_sys->p_vbi_dec, p_sliced, i_lines, i_pts / 90000.0 );
349     }
350
351     /* */
352     vlc_mutex_lock( &p_sys->lock );
353     const int i_align = p_sys->i_align;
354     const unsigned int i_wanted_page = p_sys->i_wanted_page;
355     const unsigned int i_wanted_subpage = p_sys->i_wanted_subpage;
356     const bool b_opaque = p_sys->b_opaque;
357     vlc_mutex_unlock( &p_sys->lock );
358
359     /* Try to see if the page we want is in the cache yet */
360     b_cached = vbi_fetch_vt_page( p_sys->p_vbi_dec, &p_page,
361                                   vbi_dec2bcd( i_wanted_page ),
362                                   i_wanted_subpage, VBI_WST_LEVEL_3p5,
363                                   25, true );
364
365     if( i_wanted_page == p_sys->i_last_page && !p_sys->b_update )
366         goto error;
367
368     if( !b_cached )
369     {
370         if( p_sys->i_last_page != i_wanted_page )
371         {
372             /* We need to reset the subtitle */
373             p_spu = Subpicture( p_dec, &fmt, true,
374                                 p_page.columns, p_page.rows,
375                                 i_align, p_block->i_pts );
376             if( !p_spu )
377                 goto error;
378             p_spu->p_region->psz_text = strdup("");
379
380             p_sys->b_update = true;
381             p_sys->i_last_page = i_wanted_page;
382             goto exit;
383         }
384         goto error;
385     }
386
387     p_sys->b_update = false;
388     p_sys->i_last_page = i_wanted_page;
389 #ifdef ZVBI_DEBUG
390     msg_Dbg( p_dec, "we now have page: %d ready for display",
391              i_wanted_page );
392 #endif
393     /* If there is a page or sub to render, then we do that here */
394     /* Create the subpicture unit */
395     p_spu = Subpicture( p_dec, &fmt, p_sys->b_text,
396                         p_page.columns, p_page.rows,
397                         i_align, p_block->i_pts );
398     if( !p_spu )
399         goto error;
400
401     if( p_sys->b_text )
402     {
403         unsigned int i_textsize = 7000;
404         int i_total;
405         char p_text[i_textsize+1];
406
407         i_total = vbi_print_page_region( &p_page, p_text, i_textsize,
408                         "UTF-8", 0, 0, 0, 0, p_page.columns, p_page.rows );
409         p_text[i_total] = '\0';
410         /* Strip off the pagenumber */
411         if( i_total <= 40 )
412             goto error;
413         p_spu->p_region->psz_text = strdup( &p_text[8] );
414
415 #ifdef ZVBI_DEBUG
416         msg_Info( p_dec, "page %x-%x(%d)\n%s", p_page.pgno, p_page.subno, i_total, p_text );
417 #endif
418     }
419     else
420     {
421         picture_t *p_pic = &p_spu->p_region->picture;
422
423         /* ZVBI is stupid enough to assume pitch == width */
424         p_pic->p->i_pitch = 4 * fmt.i_width;
425         vbi_draw_vt_page( &p_page, ZVBI_PIXFMT_RGBA32,
426                           p_spu->p_region->picture.p->p_pixels, 1, 1 );
427
428         vlc_mutex_lock( &p_sys->lock );
429         memcpy( p_sys->nav_link, &p_page.nav_link, sizeof( p_sys->nav_link )) ;
430         vlc_mutex_unlock( &p_sys->lock );
431
432         OpaquePage( p_pic, p_page, fmt, b_opaque );
433     }
434
435 exit:
436     vbi_unref_page( &p_page );
437     block_Release( p_block );
438     return p_spu;
439
440 error:
441     vbi_unref_page( &p_page );
442     if( p_spu != NULL )
443     {
444         p_dec->pf_spu_buffer_del( p_dec, p_spu );
445         p_spu = NULL;
446     }
447
448     block_Release( p_block );
449     return NULL;
450 }
451
452 static subpicture_t *Subpicture( decoder_t *p_dec, video_format_t *p_fmt,
453                                  bool b_text,
454                                  int i_columns, int i_rows, int i_align,
455                                  mtime_t i_pts )
456 {
457     video_format_t fmt;
458     subpicture_t *p_spu;
459
460     /* If there is a page or sub to render, then we do that here */
461     /* Create the subpicture unit */
462     p_spu = p_dec->pf_spu_buffer_new( p_dec );
463     if( !p_spu )
464     {
465         msg_Warn( p_dec, "can't get spu buffer" );
466         return NULL;
467     }
468
469     memset( &fmt, 0, sizeof(video_format_t) );
470     fmt.i_chroma = b_text ? VLC_FOURCC('T','E','X','T') :
471                                    VLC_FOURCC('R','G','B','A');
472     fmt.i_aspect = b_text ? 0 : VOUT_ASPECT_FACTOR;
473     if( b_text )
474     {
475         fmt.i_bits_per_pixel = 0;
476     }
477     else
478     {
479         fmt.i_sar_num = fmt.i_sar_den = 1;
480         fmt.i_width = fmt.i_visible_width = i_columns * 12;
481         fmt.i_height = fmt.i_visible_height = i_rows * 10;
482         fmt.i_bits_per_pixel = 32;
483     }
484     fmt.i_x_offset = fmt.i_y_offset = 0;
485
486     p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
487     if( p_spu->p_region == NULL )
488     {
489         msg_Err( p_dec, "cannot allocate SPU region" );
490         p_dec->pf_spu_buffer_del( p_dec, p_spu );
491         return NULL;
492     }
493
494     p_spu->p_region->i_x = 0;
495     p_spu->p_region->i_y = 0;
496     p_spu->p_region->i_align = i_align;
497
498     /* Normal text subs, easy markup */
499     p_spu->i_flags = SUBPICTURE_ALIGN_BOTTOM;
500
501     p_spu->i_start = i_pts;
502     p_spu->i_stop = 0;
503     p_spu->b_ephemer = true;
504     p_spu->b_absolute = false;
505     p_spu->b_pausable = true;
506
507     if( !b_text )
508     {
509         p_spu->i_width =
510         p_spu->i_original_picture_width = fmt.i_width;
511         p_spu->i_height =
512         p_spu->i_original_picture_height = fmt.i_height;
513     }
514
515     /* */
516     *p_fmt = fmt;
517     return p_spu;
518 }
519
520 static void EventHandler( vbi_event *ev, void *user_data )
521 {
522     decoder_t *p_dec        = (decoder_t *)user_data;
523     decoder_sys_t *p_sys    = p_dec->p_sys;
524
525     if( ev->type == VBI_EVENT_TTX_PAGE )
526     {
527 #ifdef ZVBI_DEBUG
528         msg_Info( p_dec, "Page %03x.%02x ",
529                     ev->ev.ttx_page.pgno,
530                     ev->ev.ttx_page.subno & 0xFF);
531 #endif
532         if( p_sys->i_last_page == vbi_bcd2dec( ev->ev.ttx_page.pgno ) )
533             p_sys->b_update = true;
534 #ifdef ZVBI_DEBUG
535         if( ev->ev.ttx_page.clock_update )
536             msg_Dbg( p_dec, "clock" );
537         if( ev->ev.ttx_page.header_update )
538             msg_Dbg( p_dec, "header" );
539 #endif
540     }
541     else if( ev->type == VBI_EVENT_CLOSE )
542         msg_Dbg( p_dec, "Close event" );
543     else if( ev->type == VBI_EVENT_CAPTION )
544         msg_Dbg( p_dec, "Caption line: %x", ev->ev.caption.pgno );
545     else if( ev->type == VBI_EVENT_NETWORK )
546     {
547         msg_Dbg( p_dec, "Network change");
548         vbi_network n = ev->ev.network;
549         msg_Dbg( p_dec, "Network id:%d name: %s, call: %s ", n.nuid, n.name, n.call );
550     }
551     else if( ev->type == VBI_EVENT_TRIGGER )
552         msg_Dbg( p_dec, "Trigger event" );
553     else if( ev->type == VBI_EVENT_ASPECT )
554         msg_Dbg( p_dec, "Aspect update" );
555     else if( ev->type == VBI_EVENT_PROG_INFO )
556         msg_Dbg( p_dec, "Program info received" );
557     else if( ev->type == VBI_EVENT_NETWORK_ID )
558         msg_Dbg( p_dec, "Network ID changed" );
559 }
560
561 static int OpaquePage( picture_t *p_src, const vbi_page p_page,
562                        const video_format_t fmt, bool b_opaque )
563 {
564     unsigned int    x, y;
565
566     assert( fmt.i_chroma == VLC_FOURCC('R','G','B','A' ) );
567
568     /* Kludge since zvbi doesn't provide an option to specify opacity. */
569     for( y = 0; y < fmt.i_height; y++ )
570     {
571         for( x = 0; x < fmt.i_width; x++ )
572         {
573             const vbi_opacity opacity = p_page.text[ y/10 * p_page.columns + x/12 ].opacity;
574             const int background = p_page.text[ y/10 * p_page.columns + x/12 ].background;
575             uint32_t *p_pixel = (uint32_t*)&p_src->p->p_pixels[y * p_src->p->i_pitch + 4*x];
576
577             switch( opacity )
578             {
579             /* Show video instead of this character */
580             case VBI_TRANSPARENT_SPACE:
581                 *p_pixel = 0;
582                 break;
583             /* Display foreground and background color */
584             /* To make the boxed text "closed captioning" transparent
585              * change true to false.
586              */
587             case VBI_OPAQUE:
588             /* alpha blend video into background color */
589             case VBI_SEMI_TRANSPARENT:
590                 if( b_opaque )
591                     break;
592             /* Full text transparency. only foreground color is show */
593             case VBI_TRANSPARENT_FULL:
594                 if( (*p_pixel) == (0xff000000 | p_page.color_map[background] ) )
595                     *p_pixel = 0;
596                 break;
597             }
598         }
599     }
600     /* end of kludge */
601     return VLC_SUCCESS;
602 }
603
604 /* Callbacks */
605 static int RequestPage( vlc_object_t *p_this, char const *psz_cmd,
606                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
607 {
608     decoder_sys_t *p_sys = p_data;
609     VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
610
611     vlc_mutex_lock( &p_sys->lock );
612     switch( newval.i_int )
613     {
614         case ZVBI_KEY_RED:
615             p_sys->i_wanted_page = vbi_bcd2dec( p_sys->nav_link[0].pgno );
616             p_sys->i_wanted_subpage = p_sys->nav_link[0].subno;
617             break;
618         case ZVBI_KEY_GREEN:
619             p_sys->i_wanted_page = vbi_bcd2dec( p_sys->nav_link[1].pgno );
620             p_sys->i_wanted_subpage = p_sys->nav_link[1].subno;
621             break;
622         case ZVBI_KEY_YELLOW:
623             p_sys->i_wanted_page = vbi_bcd2dec( p_sys->nav_link[2].pgno );
624             p_sys->i_wanted_subpage = p_sys->nav_link[2].subno;
625             break;
626         case ZVBI_KEY_BLUE:
627             p_sys->i_wanted_page = vbi_bcd2dec( p_sys->nav_link[3].pgno );
628             p_sys->i_wanted_subpage = p_sys->nav_link[3].subno;
629             break;
630         case ZVBI_KEY_INDEX:
631             p_sys->i_wanted_page = vbi_bcd2dec( p_sys->nav_link[5].pgno ); /* #4 is SKIPPED */
632             p_sys->i_wanted_subpage = p_sys->nav_link[5].subno;
633             break;
634     }
635     if( newval.i_int > 0 && newval.i_int < 999 )
636     {
637         p_sys->i_wanted_page = newval.i_int;
638         p_sys->i_wanted_subpage = VBI_ANY_SUBNO;
639     }
640     vlc_mutex_unlock( &p_sys->lock );
641
642     return VLC_SUCCESS;
643 }
644
645 static int Opaque( vlc_object_t *p_this, char const *psz_cmd,
646                    vlc_value_t oldval, vlc_value_t newval, void *p_data )
647 {
648     decoder_sys_t *p_sys = p_data;
649     VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
650
651     vlc_mutex_lock( &p_sys->lock );
652     p_sys->b_opaque = newval.b_bool;
653     p_sys->b_update = true;
654     vlc_mutex_unlock( &p_sys->lock );
655
656     return VLC_SUCCESS;
657 }
658
659 static int Position( vlc_object_t *p_this, char const *psz_cmd,
660                      vlc_value_t oldval, vlc_value_t newval, void *p_data )
661 {
662     decoder_sys_t *p_sys = p_data;
663     VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
664
665     vlc_mutex_lock( &p_sys->lock );
666     p_sys->i_align = newval.i_int;
667     vlc_mutex_unlock( &p_sys->lock );
668
669     return VLC_SUCCESS;
670 }
671
672 static int EventKey( vlc_object_t *p_this, char const *psz_cmd,
673                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
674 {
675     decoder_sys_t *p_sys = p_data;
676     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
677
678     /* FIXME: Capture + and - key for subpage browsing */
679     if( newval.i_int == '-' || newval.i_int == '+' )
680     {
681         vlc_mutex_lock( &p_sys->lock );
682         if( p_sys->i_wanted_subpage == VBI_ANY_SUBNO && newval.i_int == '+' )
683             p_sys->i_wanted_subpage = vbi_dec2bcd(1);
684         else if ( newval.i_int == '+' )
685             p_sys->i_wanted_subpage = vbi_add_bcd( p_sys->i_wanted_subpage, 1);
686         else if( newval.i_int == '-')
687             p_sys->i_wanted_subpage = vbi_add_bcd( p_sys->i_wanted_subpage, 0xF9999999); /* BCD complement - 1 */
688
689         if ( !vbi_bcd_digits_greater( p_sys->i_wanted_subpage, 0x00 ) || vbi_bcd_digits_greater( p_sys->i_wanted_subpage, 0x99 ) )
690                 p_sys->i_wanted_subpage = VBI_ANY_SUBNO;
691         else
692             vout_OSDMessage( p_this, DEFAULT_CHAN, "%s: %d", _("Subpage"), vbi_bcd2dec( p_sys->i_wanted_subpage) );
693
694         p_sys->b_update = true;
695         vlc_mutex_unlock( &p_sys->lock );
696     }
697
698     /* Capture 0-9 for page selection */
699     if( newval.i_int < '0' || newval.i_int > '9' )
700         return VLC_SUCCESS;
701
702     vlc_mutex_lock( &p_sys->lock );
703     p_sys->i_key[0] = p_sys->i_key[1];
704     p_sys->i_key[1] = p_sys->i_key[2];
705     p_sys->i_key[2] = (int)(newval.i_int - '0');
706     vout_OSDMessage( p_this, DEFAULT_CHAN, "%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') );
707
708     if( p_sys->i_key[0] > 0 && p_sys->i_key[0] <= 8 &&
709         p_sys->i_key[1] >= 0 && p_sys->i_key[1] <= 9 &&
710         p_sys->i_key[2] >= 0 && p_sys->i_key[2] <= 9 )
711     {
712         p_sys->i_wanted_page = p_sys->i_key[0]*100 + p_sys->i_key[1]*10 + p_sys->i_key[2];
713         p_sys->i_wanted_subpage = VBI_ANY_SUBNO;
714         p_sys->i_key[0] = p_sys->i_key[1] = p_sys->i_key[2] = '*' - '0';
715     }
716     vlc_mutex_unlock( &p_sys->lock );
717
718     return VLC_SUCCESS;
719 }