1 /*****************************************************************************
2 * dvb.c : DVB channel list import (szap/tzap/czap compatible channel lists)
3 *****************************************************************************
4 * Copyright (C) 2005-20009 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_demux.h>
33 #include <vlc_charset.h>
38 # define LONG_MAX 2147483647L
39 # define LONG_MIN (-LONG_MAX-1)
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Demux( demux_t *p_demux);
46 static int Control( demux_t *p_demux, int i_query, va_list args );
48 static int ParseLine( char *, char **, char ***, int *);
50 /*****************************************************************************
51 * Import_DVB: main import function
52 *****************************************************************************/
53 int Import_DVB( vlc_object_t *p_this )
55 demux_t *p_demux = (demux_t *)p_this;
56 const uint8_t *p_peek;
60 if( !demux_IsPathExtension( p_demux, ".conf" ) && !p_demux->b_force )
63 /* Check if this really is a channels file */
64 if( (i_peek = stream_Peek( p_demux->s, &p_peek, 1024 )) > 0 )
66 char psz_line[1024+1];
69 for( i = 0; i < i_peek; i++ )
71 if( p_peek[i] == '\n' ) break;
72 psz_line[i] = p_peek[i];
76 if( ParseLine( psz_line, 0, 0, 0 ) ) b_valid = true;
79 if( !b_valid ) return VLC_EGENERIC;
81 msg_Dbg( p_demux, "found valid DVB conf playlist file");
82 p_demux->pf_control = Control;
83 p_demux->pf_demux = Demux;
88 /*****************************************************************************
89 * Deactivate: frees unused data
90 *****************************************************************************/
91 void Close_DVB( vlc_object_t *p_this )
96 /*****************************************************************************
97 * Demux: The important stuff
98 *****************************************************************************/
99 static int Demux( demux_t *p_demux )
102 input_item_t *p_input;
103 input_item_t *p_current_input = GetCurrentItem(p_demux);
105 input_item_node_t *p_subitems = input_item_node_Create( p_current_input );
107 while( (psz_line = stream_ReadLine( p_demux->s )) )
109 char **ppsz_options = NULL;
111 char *psz_name = NULL;
113 if( !ParseLine( psz_line, &psz_name, &ppsz_options, &i_options ) )
119 EnsureUTF8( psz_name );
120 for( int i = 0; i< i_options; i++ )
121 EnsureUTF8( ppsz_options[i] );
123 p_input = input_item_NewExt( p_demux, "dvb://", psz_name,
124 i_options, (const char**)ppsz_options, VLC_INPUT_OPTION_TRUSTED, -1 );
125 input_item_node_AppendItem( p_subitems, p_input );
126 vlc_gc_decref( p_input );
129 free( ppsz_options[i_options] );
130 free( ppsz_options );
135 input_item_node_PostAndDelete( p_subitems );
137 vlc_gc_decref(p_current_input);
138 return 0; /* Needed for correct operation of go back */
143 const char *psz_name;
144 const char *psz_option;
148 { "INVERSION_OFF", "dvb-inversion=0" },
149 { "INVERSION_ON", "dvb-inversion=1" },
150 { "INVERSION_AUTO", "dvb-inversion=2" },
152 { "BANDWIDTH_AUTO", "dvb-bandwidth=0" },
153 { "BANDWIDTH_6_MHZ", "dvb-bandwidth=6" },
154 { "BANDWIDTH_7_MHZ", "dvb-bandwidth=7" },
155 { "BANDWIDTH_8_MHZ", "dvb-bandwidth=8" },
157 { "FEC_NONE", "dvb-fec=0" },
158 { "FEC_1_2", "dvb-fec=1" },
159 { "FEC_2_3", "dvb-fec=2" },
160 { "FEC_3_4", "dvb-fec=3" },
161 { "FEC_4_5", "dvb-fec=4" },
162 { "FEC_5_6", "dvb-fec=5" },
163 { "FEC_6_7", "dvb-fec=6" },
164 { "FEC_7_8", "dvb-fec=7" },
165 { "FEC_8_9", "dvb-fec=8" },
166 { "FEC_AUTO", "dvb-fec=9" },
168 { "GUARD_INTERVAL_AUTO", "dvb-guard=0" },
169 { "GUARD_INTERVAL_1_4", "dvb-guard=4" },
170 { "GUARD_INTERVAL_1_8", "dvb-guard=8" },
171 { "GUARD_INTERVAL_1_16", "dvb-guard=16" },
172 { "GUARD_INTERVAL_1_32", "dvb-guard=32" },
174 { "HIERARCHY_NONE", "dvb-hierarchy=-1" },
175 { "HIERARCHY_1", "dvb-hierarchy=1" },
176 { "HIERARCHY_2", "dvb-hierarchy=2" },
177 { "HIERARCHY_4", "dvb-hierarchy=4" },
179 { "QPSK", "dvb-modulation=-1" },
180 { "QAM_AUTO", "dvb-modulation=0" },
181 { "QAM_16", "dvb-modulation=16" },
182 { "QAM_32", "dvb-modulation=32" },
183 { "QAM_64", "dvb-modulation=64" },
184 { "QAM_128", "dvb-modulation=128" },
185 { "QAM_256", "dvb-modulation=256" },
186 { "8VSB", "dvb-modulation=8" },
187 { "16VSB", "dvb-modulation=16" },
189 { "TRANSMISSION_MODE_AUTO", "dvb-transmission=0" },
190 { "TRANSMISSION_MODE_2K", "dvb-transmission=2" },
191 { "TRANSMISSION_MODE_8K", "dvb-transmission=8" },
196 static int ParseLine( char *psz_line, char **ppsz_name,
197 char ***pppsz_options, int *pi_options )
199 char *psz_name = NULL, *psz_parse = psz_line;
200 int i_count = 0, i_program = 0, i_frequency = 0, i_symbolrate = 0;
201 bool b_valid = false;
203 if( pppsz_options ) *pppsz_options = NULL;
204 if( pi_options ) *pi_options = 0;
205 if( ppsz_name ) *ppsz_name = NULL;
207 /* Skip leading tabs and spaces */
208 while( *psz_parse == ' ' || *psz_parse == '\t' ||
209 *psz_parse == '\n' || *psz_parse == '\r' ) psz_parse++;
211 /* Ignore comments */
212 if( *psz_parse == '#' ) return false;
216 const char *psz_option = NULL;
217 char *psz_option_end = strchr( psz_parse, ':' );
218 if( psz_option_end ) { *psz_option_end = 0; psz_option_end++; }
223 psz_name = psz_parse;
225 else if( i_count == 1 )
231 i_value = strtol( psz_parse, &psz_end, 10 );
232 if( psz_end == psz_parse ||
233 i_value == LONG_MAX || i_value == LONG_MIN ) break;
235 i_frequency = i_value;
241 /* Check option name with our list */
242 for( i = 0; dvb_options[i].psz_name; i++ )
244 if( !strcmp( psz_parse, dvb_options[i].psz_name ) )
246 psz_option = dvb_options[i].psz_option;
248 /* If we recognize one of the strings, then we are sure
249 * the data is really valid (ie. a channels file). */
257 /* Option not recognized, test if it is a number */
261 i_value = strtol( psz_parse, &psz_end, 10 );
262 if( psz_end != psz_parse &&
263 i_value != LONG_MAX && i_value != LONG_MIN &&
266 i_symbolrate = i_value;
268 else if( psz_end != psz_parse &&
269 i_value != LONG_MAX && i_value != LONG_MIN )
276 if( psz_option && pppsz_options && pi_options )
278 char *psz_dup = strdup( psz_option );
280 INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
284 psz_parse = psz_option_end;
288 if( !b_valid && pppsz_options && pi_options )
290 /* This isn't a valid channels file, cleanup everything */
291 while( (*pi_options)-- ) free( (*pppsz_options)[*pi_options] );
292 free( *pppsz_options );
293 *pppsz_options = NULL; *pi_options = 0;
296 if( i_program && pppsz_options && pi_options )
300 if( asprintf( &psz_option, "program=%i", i_program ) != -1 )
301 INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
304 if( i_frequency && pppsz_options && pi_options )
308 if( asprintf( &psz_option, "dvb-frequency=%i", i_frequency ) != -1 )
309 INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
312 if( i_symbolrate && pppsz_options && pi_options )
316 if( asprintf( &psz_option, "dvb-srate=%i", i_symbolrate ) != -1 )
317 INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
320 if( ppsz_name && psz_name ) *ppsz_name = strdup( psz_name );
325 static int Control( demux_t *p_demux, int i_query, va_list args )
327 VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);