1 /*****************************************************************************
2 * langfromtelx.c: dynamic language setting from telx
3 *****************************************************************************
4 * Copyright (C) 2009, 2011 VideoLAN
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
36 #include <vlc_block.h>
38 /*****************************************************************************
40 *****************************************************************************/
42 #define ID_TEXT N_("Elementary Stream ID")
43 #define ID_LONGTEXT N_( \
44 "Specify an identifier integer for this elementary stream to change" )
45 #define MAGAZINE_TEXT N_("Magazine")
46 #define MAGAZINE_LONGTEXT N_( \
47 "Specify the magazine containing the language page" )
48 #define PAGE_TEXT N_("Page")
49 #define PAGE_LONGTEXT N_( \
50 "Specify the page containing the language" )
51 #define ROW_TEXT N_("Row")
52 #define ROW_LONGTEXT N_( \
53 "Specify the row containing the language" )
55 static int Open ( vlc_object_t * );
56 static void Close ( vlc_object_t * );
58 #define SOUT_CFG_PREFIX "sout-langfromtelx-"
61 set_shortname( N_("Lang From Telx"))
62 set_description( N_("Dynamic language setting from teletext"))
63 set_capability( "sout stream", 50 )
64 add_shortcut( "langfromtelx" )
65 set_category( CAT_SOUT )
66 set_subcategory( SUBCAT_SOUT_STREAM )
68 set_callbacks( Open, Close )
69 add_integer( SOUT_CFG_PREFIX "id", 0, ID_TEXT, ID_LONGTEXT,
71 add_integer( SOUT_CFG_PREFIX "magazine", 7, MAGAZINE_TEXT,
72 MAGAZINE_LONGTEXT, false )
73 add_integer( SOUT_CFG_PREFIX "page", 0x99, PAGE_TEXT, PAGE_LONGTEXT,
75 add_integer( SOUT_CFG_PREFIX "row", 1, ROW_TEXT, ROW_LONGTEXT,
80 /*****************************************************************************
82 *****************************************************************************/
83 static const char *ppsz_sout_options[] = {
84 "id", "magazine", "page", "row", NULL
87 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
88 static int Del ( sout_stream_t *, sout_stream_id_t * );
89 static int Send ( sout_stream_t *, sout_stream_id_t *, block_t * );
91 struct sout_stream_sys_t
94 int i_id, i_magazine, i_page, i_row;
95 char *psz_language, *psz_old_language;
96 sout_stream_id_t *p_id, *p_telx;
100 /*****************************************************************************
102 *****************************************************************************/
103 static int Open( vlc_object_t *p_this )
105 sout_stream_t *p_stream = (sout_stream_t*)p_this;
106 sout_stream_sys_t *p_sys;
108 if( !p_stream->p_next )
110 msg_Err( p_stream, "cannot create chain" );
114 p_sys = malloc( sizeof( sout_stream_sys_t ) );
115 if( unlikely( !p_sys ) )
118 config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
121 p_sys->i_id = var_GetInteger( p_stream, SOUT_CFG_PREFIX "id" );
122 p_sys->i_magazine = var_GetInteger( p_stream, SOUT_CFG_PREFIX "magazine" );
123 p_sys->i_page = var_GetInteger( p_stream, SOUT_CFG_PREFIX "page" );
124 p_sys->i_row = var_GetInteger( p_stream, SOUT_CFG_PREFIX "row" );
126 p_stream->pf_add = Add;
127 p_stream->pf_del = Del;
128 p_stream->pf_send = Send;
130 p_stream->p_sys = p_sys;
135 /*****************************************************************************
137 *****************************************************************************/
138 static void Close( vlc_object_t * p_this )
140 sout_stream_t *p_stream = (sout_stream_t*)p_this;
141 sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
143 free( p_sys->psz_old_language );
147 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
149 sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
151 if ( p_fmt->i_id == p_sys->i_id )
153 p_sys->psz_old_language = p_fmt->psz_language;
155 "changing language of ID %d (magazine %d page %x row %d)",
156 p_sys->i_id, p_sys->i_magazine, p_sys->i_page, p_sys->i_row );
157 p_sys->psz_language = p_fmt->psz_language = malloc(4);
158 if ( p_sys->psz_old_language != NULL )
159 strncpy( p_fmt->psz_language, p_sys->psz_old_language, 3 );
161 strcpy( p_fmt->psz_language, "unk" );
162 p_fmt->psz_language[3] = '\0';
164 p_sys->p_id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
168 if ( p_fmt->i_codec == VLC_CODEC_TELETEXT )
170 p_sys->p_telx = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
171 return p_sys->p_telx;
174 return p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
177 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
179 sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
181 if ( id == p_sys->p_id ) p_sys->p_id = NULL;
182 if ( id == p_sys->p_telx ) p_sys->p_telx = NULL;
184 return p_sys->p_out->pf_del( p_sys->p_out, id );
187 static void SetLanguage( sout_stream_t *p_stream, char *psz_language )
189 sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
191 if ( strncmp( p_sys->psz_language, psz_language, 3 ) )
192 msg_Dbg( p_stream, "changing language to %s", psz_language );
194 strncpy( p_sys->psz_language, psz_language, 3 );
197 /*****************************************************************************
199 *****************************************************************************/
200 static uint8_t bytereverse( int n )
202 n = (((n >> 1) & 0x55) | ((n << 1) & 0xaa));
203 n = (((n >> 2) & 0x33) | ((n << 2) & 0xcc));
204 n = (((n >> 4) & 0x0f) | ((n << 4) & 0xf0));
208 static int hamming_8_4( int a )
244 return -1; // decoding error , not yet corrected
248 static void decode_string( char *res, uint8_t *packet, int len )
250 for ( int i = 0; i < len; i++ )
251 res[i] = bytereverse( packet[i] ) & 0x7f;
256 static void HandleTelx( sout_stream_t *p_stream, block_t *p_block )
258 sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
259 int len = p_block->i_buffer;
261 for ( int offset = 1; offset + 46 <= len; offset += 46 )
263 uint8_t * packet = (uint8_t *) p_block->p_buffer+offset;
264 if ( packet[0] == 0xFF )
267 int mpag = (hamming_8_4( packet[4] ) << 4) | hamming_8_4( packet[5] );
268 int i_row, i_magazine;
275 i_row = 0xFF & bytereverse(mpag);
276 i_magazine = (7 & i_row) == 0 ? 8 : (7 & i_row);
279 if ( i_magazine != p_sys->i_magazine )
284 /* row 0 : flags and header line */
285 p_sys->i_current_page =
286 (0xF0 & bytereverse( hamming_8_4(packet[7]) )) |
287 (0xF & (bytereverse( hamming_8_4(packet[6]) ) >> 4) );
289 if ( p_sys->i_current_page != p_sys->i_page ) continue;
290 if ( i_row == p_sys->i_row )
292 /* row 1-23 : normal lines */
294 decode_string( psz_line, packet + 6, 40 );
295 psz_line[0] = tolower(psz_line[0]);
296 psz_line[1] = tolower(psz_line[1]);
297 psz_line[2] = tolower(psz_line[2]);
299 SetLanguage( p_stream, psz_line );
304 /*****************************************************************************
306 *****************************************************************************/
307 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
310 sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
312 if ( id == p_sys->p_telx )
313 HandleTelx( p_stream, p_buffer );
315 return p_sys->p_out->pf_send( p_sys->p_out, id, p_buffer );