]> git.sesse.net Git - vlc/blob - modules/stream_out/langfromtelx.c
sout: constify format parameter to sout_stream_t.pf_add
[vlc] / modules / stream_out / langfromtelx.c
1 /*****************************************************************************
2  * langfromtelx.c: dynamic language setting from telx
3  *****************************************************************************
4  * Copyright © 2009-2011 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <ctype.h>
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_sout.h>
36 #include <vlc_block.h>
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41
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" )
54
55 static int  Open    ( vlc_object_t * );
56 static void Close   ( vlc_object_t * );
57
58 #define SOUT_CFG_PREFIX "sout-langfromtelx-"
59
60 vlc_module_begin()
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 )
67
68     set_callbacks( Open, Close )
69     add_integer( SOUT_CFG_PREFIX "id", 0, ID_TEXT, ID_LONGTEXT,
70                  false )
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,
74                  false )
75     add_integer( SOUT_CFG_PREFIX "row", 1, ROW_TEXT, ROW_LONGTEXT,
76                  false )
77 vlc_module_end();
78
79
80 /*****************************************************************************
81  * Local prototypes
82  *****************************************************************************/
83 static const char *ppsz_sout_options[] = {
84     "id", "magazine", "page", "row", NULL
85 };
86
87 static sout_stream_id_sys_t *Add   ( sout_stream_t *, es_format_t * );
88 static void              Del   ( sout_stream_t *, sout_stream_id_sys_t * );
89 static int               Send  ( sout_stream_t *, sout_stream_id_sys_t *, block_t * );
90
91 struct sout_stream_sys_t
92 {
93     int i_id, i_magazine, i_page, i_row;
94     char *psz_language, *psz_old_language;
95     sout_stream_id_sys_t *p_id, *p_telx;
96     int i_current_page;
97 };
98
99 /*****************************************************************************
100  * Open:
101  *****************************************************************************/
102 static int Open( vlc_object_t *p_this )
103 {
104     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
105     sout_stream_sys_t *p_sys;
106
107     if( !p_stream->p_next )
108     {
109         msg_Err( p_stream, "cannot create chain" );
110         return VLC_EGENERIC;
111     }
112
113     p_sys = malloc( sizeof( sout_stream_sys_t ) );
114     if( unlikely( !p_sys ) )
115         return VLC_ENOMEM;
116
117     config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
118                        p_stream->p_cfg );
119
120     p_sys->i_id       = var_GetInteger( p_stream, SOUT_CFG_PREFIX "id" );
121     p_sys->i_magazine = var_GetInteger( p_stream, SOUT_CFG_PREFIX "magazine" );
122     p_sys->i_page     = var_GetInteger( p_stream, SOUT_CFG_PREFIX "page" );
123     p_sys->i_row      = var_GetInteger( p_stream, SOUT_CFG_PREFIX "row" );
124
125     p_stream->pf_add  = Add;
126     p_stream->pf_del  = Del;
127     p_stream->pf_send = Send;
128
129     p_stream->p_sys   = p_sys;
130
131     return VLC_SUCCESS;
132 }
133
134 /*****************************************************************************
135  * Close:
136  *****************************************************************************/
137 static void Close( vlc_object_t * p_this )
138 {
139     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
140     sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
141
142     free( p_sys->psz_old_language );
143     free( p_sys );
144 }
145
146 static sout_stream_id_sys_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
147 {
148     sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
149     sout_stream_id_sys_t *id;
150     es_format_t fmt;
151
152     if ( p_fmt->i_id == p_sys->i_id )
153     {
154         fmt = *p_fmt;
155
156         p_sys->psz_old_language = p_fmt->psz_language;
157         msg_Dbg( p_stream,
158                  "changing language of ID %d (magazine %d page %x row %d)",
159                  p_sys->i_id, p_sys->i_magazine, p_sys->i_page, p_sys->i_row );
160         p_sys->psz_language = p_fmt->psz_language = malloc(4);
161         if ( p_sys->psz_old_language != NULL )
162             strncpy( p_fmt->psz_language, p_sys->psz_old_language, 3 );
163         else
164             strcpy( p_fmt->psz_language, "unk" );
165         p_fmt->psz_language[3] = '\0';
166     }
167
168     id = sout_StreamIdAdd( p_stream->p_next, p_fmt );
169
170     if( p_fmt->i_id == p_sys->i_id )
171         p_sys->p_id = id;
172     if( p_fmt->i_codec == VLC_CODEC_TELETEXT )
173         p_sys->p_telx = id;
174
175     return id;
176 }
177
178 static void Del( sout_stream_t *p_stream, sout_stream_id_sys_t *id )
179 {
180     sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
181
182     if ( id == p_sys->p_id ) p_sys->p_id = NULL;
183     if ( id == p_sys->p_telx ) p_sys->p_telx = NULL;
184
185     p_stream->p_next->pf_del( p_stream->p_next, id );
186 }
187
188 static void SetLanguage( sout_stream_t *p_stream, char *psz_language )
189 {
190     sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
191
192     if ( strncmp( p_sys->psz_language, psz_language, 3 ) )
193         msg_Dbg( p_stream, "changing language to %s", psz_language );
194
195     strncpy( p_sys->psz_language, (const char *)psz_language, 3 );
196 }
197
198 /*****************************************************************************
199  * Teletext stuff
200  *****************************************************************************/
201 static uint8_t bytereverse( int n )
202 {
203     n = (((n >> 1) & 0x55) | ((n << 1) & 0xaa));
204     n = (((n >> 2) & 0x33) | ((n << 2) & 0xcc));
205     n = (((n >> 4) & 0x0f) | ((n << 4) & 0xf0));
206     return n;
207 }
208
209 static int hamming_8_4( int a )
210 {
211     switch (a) {
212     case 0xA8:
213         return 0;
214     case 0x0B:
215         return 1;
216     case 0x26:
217         return 2;
218     case 0x85:
219         return 3;
220     case 0x92:
221         return 4;
222     case 0x31:
223         return 5;
224     case 0x1C:
225         return 6;
226     case 0xBF:
227         return 7;
228     case 0x40:
229         return 8;
230     case 0xE3:
231         return 9;
232     case 0xCE:
233         return 10;
234     case 0x6D:
235         return 11;
236     case 0x7A:
237         return 12;
238     case 0xD9:
239         return 13;
240     case 0xF4:
241         return 14;
242     case 0x57:
243         return 15;
244     default:
245         return -1;     // decoding error , not yet corrected
246     }
247 }
248
249 static void decode_string( char *res, uint8_t *packet, int len )
250 {
251     for ( int i = 0; i < len; i++ )
252         res[i] = bytereverse( packet[i] ) & 0x7f;
253
254     res[len] = '\0';
255 }
256
257 static void HandleTelx( sout_stream_t *p_stream, block_t *p_block )
258 {
259     sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
260     int len = p_block->i_buffer;
261
262     for ( int offset = 1; offset + 46 <= len; offset += 46 )
263     {
264         uint8_t * packet = (uint8_t *) p_block->p_buffer+offset;
265         if ( packet[0] == 0xFF )
266             continue;
267
268         int mpag = (hamming_8_4( packet[4] ) << 4) | hamming_8_4( packet[5] );
269         int i_row, i_magazine;
270         if ( mpag < 0 )
271         {
272             /* decode error */
273             continue;
274         }
275
276         i_row = 0xFF & bytereverse(mpag);
277         i_magazine = (7 & i_row) == 0 ? 8 : (7 & i_row);
278         i_row >>= 3;
279
280         if ( i_magazine != p_sys->i_magazine )
281             continue;
282
283         if ( i_row == 0 )
284         {
285             /* row 0 : flags and header line */
286             p_sys->i_current_page =
287                 (0xF0 & bytereverse( hamming_8_4(packet[7]) )) |
288                 (0xF & (bytereverse( hamming_8_4(packet[6]) ) >> 4) );
289         }
290         if ( p_sys->i_current_page != p_sys->i_page ) continue;
291         if ( i_row == p_sys->i_row )
292         {
293             /* row 1-23 : normal lines */
294             char psz_line[41];
295             decode_string( psz_line, packet + 6, 40 );
296             psz_line[0] = tolower(psz_line[0]);
297             psz_line[1] = tolower(psz_line[1]);
298             psz_line[2] = tolower(psz_line[2]);
299             psz_line[3] = '\0';
300             SetLanguage( p_stream, psz_line );
301         }
302     }
303 }
304
305 /*****************************************************************************
306  * Send:
307  *****************************************************************************/
308 static int Send( sout_stream_t *p_stream, sout_stream_id_sys_t *id,
309                  block_t *p_buffer )
310 {
311     sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
312
313     if ( id == p_sys->p_telx )
314         HandleTelx( p_stream, p_buffer );
315
316     return p_stream->p_next->pf_send( p_stream->p_next, id, p_buffer );
317 }