1 /*****************************************************************************
2 * dvb.c : DVB channel list import (szap/tzap/czap compatible channel lists)
3 *****************************************************************************
4 * Copyright (C) 2005 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_interface.h>
34 #include <vlc_charset.h>
39 # define LONG_MAX 2147483647L
40 # define LONG_MIN (-LONG_MAX-1)
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Demux( demux_t *p_demux);
47 static int Control( demux_t *p_demux, int i_query, va_list args );
49 static int ParseLine( char *, char **, char ***, int *);
51 /*****************************************************************************
52 * Import_DVB: main import function
53 *****************************************************************************/
54 int Import_DVB( vlc_object_t *p_this )
56 demux_t *p_demux = (demux_t *)p_this;
57 const uint8_t *p_peek;
61 if( !demux_IsPathExtension( p_demux, ".conf" ) && !p_demux->b_force )
64 /* Check if this really is a channels file */
65 if( (i_peek = stream_Peek( p_demux->s, &p_peek, 1024 )) > 0 )
67 char psz_line[1024+1];
70 for( i = 0; i < i_peek; i++ )
72 if( p_peek[i] == '\n' ) break;
73 psz_line[i] = p_peek[i];
77 if( ParseLine( psz_line, 0, 0, 0 ) ) b_valid = true;
80 if( !b_valid ) return VLC_EGENERIC;
82 msg_Dbg( p_demux, "found valid DVB conf playlist file");
83 p_demux->pf_control = Control;
84 p_demux->pf_demux = Demux;
89 /*****************************************************************************
90 * Deactivate: frees unused data
91 *****************************************************************************/
92 void Close_DVB( vlc_object_t *p_this )
97 /*****************************************************************************
98 * Demux: The important stuff
99 *****************************************************************************/
100 static int Demux( demux_t *p_demux )
103 input_item_t *p_input;
106 while( (psz_line = stream_ReadLine( p_demux->s )) )
108 char **ppsz_options = NULL;
109 int i, i_options = 0;
110 char *psz_name = NULL;
112 if( !ParseLine( psz_line, &psz_name, &ppsz_options, &i_options ) )
118 EnsureUTF8( psz_name );
120 p_input = input_item_NewExt( p_demux, "dvb://", psz_name, 0, NULL, -1 );
121 for( i = 0; i< i_options; i++ )
123 EnsureUTF8( ppsz_options[i] );
124 input_item_AddOption( p_input, ppsz_options[i] );
126 input_item_AddSubItem( p_current_input, p_input );
127 vlc_gc_decref( p_input );
128 while( i_options-- ) free( ppsz_options[i_options] );
129 free( ppsz_options );
134 HANDLE_PLAY_AND_RELEASE;
135 return 0; /* Needed for correct operation of go back */
140 const char *psz_name;
141 const char *psz_option;
145 { "INVERSION_OFF", "dvb-inversion=0" },
146 { "INVERSION_ON", "dvb-inversion=1" },
147 { "INVERSION_AUTO", "dvb-inversion=2" },
149 { "BANDWIDTH_AUTO", "dvb-bandwidth=0" },
150 { "BANDWIDTH_6_MHZ", "dvb-bandwidth=6" },
151 { "BANDWIDTH_7_MHZ", "dvb-bandwidth=7" },
152 { "BANDWIDTH_8_MHZ", "dvb-bandwidth=8" },
154 { "FEC_NONE", "dvb-fec=0" },
155 { "FEC_1_2", "dvb-fec=1" },
156 { "FEC_2_3", "dvb-fec=2" },
157 { "FEC_3_4", "dvb-fec=3" },
158 { "FEC_4_5", "dvb-fec=4" },
159 { "FEC_5_6", "dvb-fec=5" },
160 { "FEC_6_7", "dvb-fec=6" },
161 { "FEC_7_8", "dvb-fec=7" },
162 { "FEC_8_9", "dvb-fec=8" },
163 { "FEC_AUTO", "dvb-fec=9" },
165 { "GUARD_INTERVAL_AUTO", "dvb-guard=0" },
166 { "GUARD_INTERVAL_1_4", "dvb-guard=4" },
167 { "GUARD_INTERVAL_1_8", "dvb-guard=8" },
168 { "GUARD_INTERVAL_1_16", "dvb-guard=16" },
169 { "GUARD_INTERVAL_1_32", "dvb-guard=32" },
171 { "HIERARCHY_NONE", "dvb-hierarchy=-1" },
172 { "HIERARCHY_1", "dvb-hierarchy=1" },
173 { "HIERARCHY_2", "dvb-hierarchy=2" },
174 { "HIERARCHY_4", "dvb-hierarchy=4" },
176 { "QPSK", "dvb-modulation=-1" },
177 { "QAM_AUTO", "dvb-modulation=0" },
178 { "QAM_16", "dvb-modulation=16" },
179 { "QAM_32", "dvb-modulation=32" },
180 { "QAM_64", "dvb-modulation=64" },
181 { "QAM_128", "dvb-modulation=128" },
182 { "QAM_256", "dvb-modulation=256" },
183 { "8VSB", "dvb-modulation=8" },
184 { "16VSB", "dvb-modulation=16" },
186 { "TRANSMISSION_MODE_AUTO", "dvb-transmission=0" },
187 { "TRANSMISSION_MODE_2K", "dvb-transmission=2" },
188 { "TRANSMISSION_MODE_8K", "dvb-transmission=8" },
193 static int ParseLine( char *psz_line, char **ppsz_name,
194 char ***pppsz_options, int *pi_options )
196 char *psz_name = NULL, *psz_parse = psz_line;
197 int i_count = 0, i_program = 0, i_frequency = 0;
198 bool b_valid = false;
200 if( pppsz_options ) *pppsz_options = NULL;
201 if( pi_options ) *pi_options = 0;
202 if( ppsz_name ) *ppsz_name = NULL;
204 /* Skip leading tabs and spaces */
205 while( *psz_parse == ' ' || *psz_parse == '\t' ||
206 *psz_parse == '\n' || *psz_parse == '\r' ) psz_parse++;
208 /* Ignore comments */
209 if( *psz_parse == '#' ) return false;
213 const char *psz_option = NULL;
214 char *psz_end = strchr( psz_parse, ':' );
215 if( psz_end ) { *psz_end = 0; psz_end++; }
220 psz_name = psz_parse;
222 else if( i_count == 1 )
228 i_value = strtol( psz_parse, &psz_end, 10 );
229 if( psz_end == psz_parse ||
230 i_value == LONG_MAX || i_value == LONG_MIN ) break;
232 i_frequency = i_value;
238 /* Check option name with our list */
239 for( i = 0; dvb_options[i].psz_name; i++ )
241 if( !strcmp( psz_parse, dvb_options[i].psz_name ) )
243 psz_option = dvb_options[i].psz_option;
245 /* If we recognize one of the strings, then we are sure
246 * the data is really valid (ie. a channels file). */
254 /* Option not recognized, test if it is a number */
258 i_value = strtol( psz_parse, &psz_end, 10 );
259 if( psz_end != psz_parse &&
260 i_value != LONG_MAX && i_value != LONG_MIN )
267 if( psz_option && pppsz_options && pi_options )
269 char *psz_dup = strdup( psz_option );
271 INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
279 if( !b_valid && pppsz_options && pi_options )
281 /* This isn't a valid channels file, cleanup everything */
282 while( (*pi_options)-- ) free( (*pppsz_options)[*pi_options] );
283 free( *pppsz_options );
284 *pppsz_options = NULL; *pi_options = 0;
287 if( i_program && pppsz_options && pi_options )
291 if( asprintf( &psz_option, "program=%i", i_program ) != -1 )
292 INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
295 if( i_frequency && pppsz_options && pi_options )
299 if( asprintf( &psz_option, "dvb-frequency=%i", i_frequency ) != -1 )
300 INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
303 if( ppsz_name && psz_name ) *ppsz_name = strdup( psz_name );
308 static int Control( demux_t *p_demux, int i_query, va_list args )
310 VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);