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 *****************************************************************************/
28 #include <vlc_demux.h>
29 #include <vlc_interface.h>
30 #include <vlc_charset.h>
35 # define LONG_MAX 2147483647L
36 # define LONG_MIN (-LONG_MAX-1)
39 /*****************************************************************************
41 *****************************************************************************/
42 static int Demux( demux_t *p_demux);
43 static int Control( demux_t *p_demux, int i_query, va_list args );
45 static int ParseLine( char *, char **, char ***, int *);
47 /*****************************************************************************
48 * Import_DVB: main import function
49 *****************************************************************************/
50 int E_(Import_DVB)( vlc_object_t *p_this )
52 demux_t *p_demux = (demux_t *)p_this;
53 const uint8_t *p_peek;
55 vlc_bool_t b_valid = VLC_FALSE;
57 if( !demux2_IsPathExtension( p_demux, ".conf" ) && !p_demux->b_force )
60 /* Check if this really is a channels file */
61 if( (i_peek = stream_Peek( p_demux->s, &p_peek, 1024 )) > 0 )
63 char psz_line[1024+1];
66 for( i = 0; i < i_peek; i++ )
68 if( p_peek[i] == '\n' ) break;
69 psz_line[i] = p_peek[i];
73 if( ParseLine( psz_line, 0, 0, 0 ) ) b_valid = VLC_TRUE;
76 if( !b_valid ) return VLC_EGENERIC;
78 msg_Dbg( p_demux, "found valid DVB conf playlist file");
79 p_demux->pf_control = Control;
80 p_demux->pf_demux = Demux;
85 /*****************************************************************************
86 * Deactivate: frees unused data
87 *****************************************************************************/
88 void E_(Close_DVB)( vlc_object_t *p_this )
92 /*****************************************************************************
93 * Demux: The important stuff
94 *****************************************************************************/
95 static int Demux( demux_t *p_demux )
98 input_item_t *p_input;
101 while( (psz_line = stream_ReadLine( p_demux->s )) )
103 char **ppsz_options = NULL;
104 int i, i_options = 0;
105 char *psz_name = NULL;
107 if( !ParseLine( psz_line, &psz_name, &ppsz_options, &i_options ) )
113 EnsureUTF8( psz_name );
115 p_input = input_ItemNewExt( p_playlist, "dvb:", psz_name, 0, NULL, -1 );
116 for( i = 0; i< i_options; i++ )
118 EnsureUTF8( ppsz_options[i] );
119 input_ItemAddOption( p_input, ppsz_options[i] );
121 input_ItemAddSubItem( p_current_input, p_input );
123 while( i_options-- ) free( ppsz_options[i_options] );
124 if( ppsz_options ) free( ppsz_options );
129 HANDLE_PLAY_AND_RELEASE;
130 return -1; /* Needed for correct operation of go back */
135 const char *psz_name;
136 const char *psz_option;
140 { "INVERSION_OFF", "dvb-inversion=0" },
141 { "INVERSION_ON", "dvb-inversion=1" },
142 { "INVERSION_AUTO", "dvb-inversion=2" },
144 { "BANDWIDTH_AUTO", "dvb-bandwidth=0" },
145 { "BANDWIDTH_6_MHZ", "dvb-bandwidth=6" },
146 { "BANDWIDTH_7_MHZ", "dvb-bandwidth=7" },
147 { "BANDWIDTH_8_MHZ", "dvb-bandwidth=8" },
149 { "FEC_NONE", "dvb-fec=0" },
150 { "FEC_1_2", "dvb-fec=1" },
151 { "FEC_2_3", "dvb-fec=2" },
152 { "FEC_3_4", "dvb-fec=3" },
153 { "FEC_4_5", "dvb-fec=4" },
154 { "FEC_5_6", "dvb-fec=5" },
155 { "FEC_6_7", "dvb-fec=6" },
156 { "FEC_7_8", "dvb-fec=7" },
157 { "FEC_8_9", "dvb-fec=8" },
158 { "FEC_AUTO", "dvb-fec=9" },
160 { "GUARD_INTERVAL_AUTO", "dvb-guard=0" },
161 { "GUARD_INTERVAL_1_4", "dvb-guard=4" },
162 { "GUARD_INTERVAL_1_8", "dvb-guard=8" },
163 { "GUARD_INTERVAL_1_16", "dvb-guard=16" },
164 { "GUARD_INTERVAL_1_32", "dvb-guard=32" },
166 { "HIERARCHY_NONE", "dvb-hierarchy=-1" },
167 { "HIERARCHY_1", "dvb-hierarchy=1" },
168 { "HIERARCHY_2", "dvb-hierarchy=2" },
169 { "HIERARCHY_4", "dvb-hierarchy=4" },
171 { "QPSK", "dvb-modulation=-1" },
172 { "QAM_AUTO", "dvb-modulation=0" },
173 { "QAM_16", "dvb-modulation=16" },
174 { "QAM_32", "dvb-modulation=32" },
175 { "QAM_64", "dvb-modulation=64" },
176 { "QAM_128", "dvb-modulation=128" },
177 { "QAM_256", "dvb-modulation=256" },
178 { "8VSB", "dvb-modulation=8" },
179 { "16VSB", "dvb-modulation=16" },
181 { "TRANSMISSION_MODE_AUTO", "dvb-transmission=0" },
182 { "TRANSMISSION_MODE_2K", "dvb-transmission=2" },
183 { "TRANSMISSION_MODE_8K", "dvb-transmission=8" },
188 static int ParseLine( char *psz_line, char **ppsz_name,
189 char ***pppsz_options, int *pi_options )
191 char *psz_name = 0, *psz_parse = psz_line;
192 int i_count = 0, i_program = 0, i_frequency = 0;
193 vlc_bool_t b_valid = VLC_FALSE;
195 if( pppsz_options ) *pppsz_options = 0;
196 if( pi_options ) *pi_options = 0;
197 if( ppsz_name ) *ppsz_name = 0;
199 /* Skip leading tabs and spaces */
200 while( *psz_parse == ' ' || *psz_parse == '\t' ||
201 *psz_parse == '\n' || *psz_parse == '\r' ) psz_parse++;
203 /* Ignore comments */
204 if( *psz_parse == '#' ) return VLC_FALSE;
208 const char *psz_option = 0;
209 char *psz_end = strchr( psz_parse, ':' );
210 if( psz_end ) { *psz_end = 0; psz_end++; }
215 psz_name = psz_parse;
217 else if( i_count == 1 )
223 i_value = strtol( psz_parse, &psz_end, 10 );
224 if( psz_end == psz_parse ||
225 i_value == LONG_MAX || i_value == LONG_MIN ) break;
227 i_frequency = i_value;
233 /* Check option name with our list */
234 for( i = 0; dvb_options[i].psz_name; i++ )
236 if( !strcmp( psz_parse, dvb_options[i].psz_name ) )
238 psz_option = dvb_options[i].psz_option;
240 /* If we recognize one of the strings, then we are sure
241 * the data is really valid (ie. a channels file). */
249 /* Option not recognized, test if it is a number */
253 i_value = strtol( psz_parse, &psz_end, 10 );
254 if( psz_end != psz_parse &&
255 i_value != LONG_MAX && i_value != LONG_MIN )
262 if( psz_option && pppsz_options && pi_options )
264 char *psz_dup = strdup( psz_option );
266 INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
274 if( !b_valid && pppsz_options && pi_options )
276 /* This isn't a valid channels file, cleanup everything */
277 while( (*pi_options)-- ) free( (*pppsz_options)[*pi_options] );
278 if( *pppsz_options ) free( *pppsz_options );
279 *pppsz_options = 0; *pi_options = 0;
282 if( i_program && pppsz_options && pi_options )
286 asprintf( &psz_option, "program=%i", i_program );
287 INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
290 if( i_frequency && pppsz_options && pi_options )
294 asprintf( &psz_option, "dvb-frequency=%i", i_frequency );
295 INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
298 if( ppsz_name && psz_name ) *ppsz_name = strdup( psz_name );
303 static int Control( demux_t *p_demux, int i_query, va_list args )