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_AddSubItem( p_current_input, p_input );
126 input_item_node_AppendItem( p_subitems, p_input );
127 vlc_gc_decref( p_input );
130 free( ppsz_options[i_options] );
131 free( ppsz_options );
136 input_item_AddSubItemTree( p_subitems );
137 input_item_node_Delete( p_subitems );
139 vlc_gc_decref(p_current_input);
140 return 0; /* Needed for correct operation of go back */
145 const char *psz_name;
146 const char *psz_option;
150 { "INVERSION_OFF", "dvb-inversion=0" },
151 { "INVERSION_ON", "dvb-inversion=1" },
152 { "INVERSION_AUTO", "dvb-inversion=2" },
154 { "BANDWIDTH_AUTO", "dvb-bandwidth=0" },
155 { "BANDWIDTH_6_MHZ", "dvb-bandwidth=6" },
156 { "BANDWIDTH_7_MHZ", "dvb-bandwidth=7" },
157 { "BANDWIDTH_8_MHZ", "dvb-bandwidth=8" },
159 { "FEC_NONE", "dvb-fec=0" },
160 { "FEC_1_2", "dvb-fec=1" },
161 { "FEC_2_3", "dvb-fec=2" },
162 { "FEC_3_4", "dvb-fec=3" },
163 { "FEC_4_5", "dvb-fec=4" },
164 { "FEC_5_6", "dvb-fec=5" },
165 { "FEC_6_7", "dvb-fec=6" },
166 { "FEC_7_8", "dvb-fec=7" },
167 { "FEC_8_9", "dvb-fec=8" },
168 { "FEC_AUTO", "dvb-fec=9" },
170 { "GUARD_INTERVAL_AUTO", "dvb-guard=0" },
171 { "GUARD_INTERVAL_1_4", "dvb-guard=4" },
172 { "GUARD_INTERVAL_1_8", "dvb-guard=8" },
173 { "GUARD_INTERVAL_1_16", "dvb-guard=16" },
174 { "GUARD_INTERVAL_1_32", "dvb-guard=32" },
176 { "HIERARCHY_NONE", "dvb-hierarchy=-1" },
177 { "HIERARCHY_1", "dvb-hierarchy=1" },
178 { "HIERARCHY_2", "dvb-hierarchy=2" },
179 { "HIERARCHY_4", "dvb-hierarchy=4" },
181 { "QPSK", "dvb-modulation=-1" },
182 { "QAM_AUTO", "dvb-modulation=0" },
183 { "QAM_16", "dvb-modulation=16" },
184 { "QAM_32", "dvb-modulation=32" },
185 { "QAM_64", "dvb-modulation=64" },
186 { "QAM_128", "dvb-modulation=128" },
187 { "QAM_256", "dvb-modulation=256" },
188 { "8VSB", "dvb-modulation=8" },
189 { "16VSB", "dvb-modulation=16" },
191 { "TRANSMISSION_MODE_AUTO", "dvb-transmission=0" },
192 { "TRANSMISSION_MODE_2K", "dvb-transmission=2" },
193 { "TRANSMISSION_MODE_8K", "dvb-transmission=8" },
198 static int ParseLine( char *psz_line, char **ppsz_name,
199 char ***pppsz_options, int *pi_options )
201 char *psz_name = NULL, *psz_parse = psz_line;
202 int i_count = 0, i_program = 0, i_frequency = 0, i_symbolrate = 0;
203 bool b_valid = false;
205 if( pppsz_options ) *pppsz_options = NULL;
206 if( pi_options ) *pi_options = 0;
207 if( ppsz_name ) *ppsz_name = NULL;
209 /* Skip leading tabs and spaces */
210 while( *psz_parse == ' ' || *psz_parse == '\t' ||
211 *psz_parse == '\n' || *psz_parse == '\r' ) psz_parse++;
213 /* Ignore comments */
214 if( *psz_parse == '#' ) return false;
218 const char *psz_option = NULL;
219 char *psz_option_end = strchr( psz_parse, ':' );
220 if( psz_option_end ) { *psz_option_end = 0; psz_option_end++; }
225 psz_name = psz_parse;
227 else if( i_count == 1 )
233 i_value = strtol( psz_parse, &psz_end, 10 );
234 if( psz_end == psz_parse ||
235 i_value == LONG_MAX || i_value == LONG_MIN ) break;
237 i_frequency = i_value;
243 /* Check option name with our list */
244 for( i = 0; dvb_options[i].psz_name; i++ )
246 if( !strcmp( psz_parse, dvb_options[i].psz_name ) )
248 psz_option = dvb_options[i].psz_option;
250 /* If we recognize one of the strings, then we are sure
251 * the data is really valid (ie. a channels file). */
259 /* Option not recognized, test if it is a number */
263 i_value = strtol( psz_parse, &psz_end, 10 );
264 if( psz_end != psz_parse &&
265 i_value != LONG_MAX && i_value != LONG_MIN &&
268 i_symbolrate = i_value;
270 else if( psz_end != psz_parse &&
271 i_value != LONG_MAX && i_value != LONG_MIN )
278 if( psz_option && pppsz_options && pi_options )
280 char *psz_dup = strdup( psz_option );
282 INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
286 psz_parse = psz_option_end;
290 if( !b_valid && pppsz_options && pi_options )
292 /* This isn't a valid channels file, cleanup everything */
293 while( (*pi_options)-- ) free( (*pppsz_options)[*pi_options] );
294 free( *pppsz_options );
295 *pppsz_options = NULL; *pi_options = 0;
298 if( i_program && pppsz_options && pi_options )
302 if( asprintf( &psz_option, "program=%i", i_program ) != -1 )
303 INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
306 if( i_frequency && pppsz_options && pi_options )
310 if( asprintf( &psz_option, "dvb-frequency=%i", i_frequency ) != -1 )
311 INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
314 if( i_symbolrate && pppsz_options && pi_options )
318 if( asprintf( &psz_option, "dvb-srate=%i", i_symbolrate ) != -1 )
319 INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
322 if( ppsz_name && psz_name ) *ppsz_name = strdup( psz_name );
327 static int Control( demux_t *p_demux, int i_query, va_list args )
329 VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);