]> git.sesse.net Git - vlc/blob - modules/demux/playlist/dvb.c
Revert [23949].
[vlc] / modules / demux / playlist / dvb.c
1 /*****************************************************************************
2  * dvb.c : DVB channel list import (szap/tzap/czap compatible channel lists)
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
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.
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 General Public License for more details.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc_demux.h>
29 #include <vlc_interface.h>
30 #include <vlc_charset.h>
31
32 #include "playlist.h"
33
34 #ifndef LONG_MAX
35 #   define LONG_MAX 2147483647L
36 #   define LONG_MIN (-LONG_MAX-1)
37 #endif
38
39 /*****************************************************************************
40  * Local prototypes
41  *****************************************************************************/
42 static int Demux( demux_t *p_demux);
43 static int Control( demux_t *p_demux, int i_query, va_list args );
44
45 static int ParseLine( char *, char **, char ***, int *);
46
47 /*****************************************************************************
48  * Import_DVB: main import function
49  *****************************************************************************/
50 int E_(Import_DVB)( vlc_object_t *p_this )
51 {
52     demux_t *p_demux = (demux_t *)p_this;
53     const uint8_t *p_peek;
54     int     i_peek;
55     vlc_bool_t b_valid = VLC_FALSE;
56
57     if( !demux2_IsPathExtension( p_demux, ".conf" ) && !p_demux->b_force )
58         return VLC_EGENERIC;
59
60     /* Check if this really is a channels file */
61     if( (i_peek = stream_Peek( p_demux->s, &p_peek, 1024 )) > 0 )
62     {
63         char psz_line[1024+1];
64         int i;
65
66         for( i = 0; i < i_peek; i++ )
67         {
68             if( p_peek[i] == '\n' ) break;
69             psz_line[i] = p_peek[i];
70         }
71         psz_line[i] = 0;
72
73         if( ParseLine( psz_line, 0, 0, 0 ) ) b_valid = VLC_TRUE;
74     }
75
76     if( !b_valid ) return VLC_EGENERIC;
77
78     msg_Dbg( p_demux, "found valid DVB conf playlist file");
79     p_demux->pf_control = Control;
80     p_demux->pf_demux = Demux;
81
82     return VLC_SUCCESS;
83 }
84
85 /*****************************************************************************
86  * Deactivate: frees unused data
87  *****************************************************************************/
88 void E_(Close_DVB)( vlc_object_t *p_this )
89 {
90 }
91
92 /*****************************************************************************
93  * Demux: The important stuff
94  *****************************************************************************/
95 static int Demux( demux_t *p_demux )
96 {
97     char       *psz_line;
98     input_item_t *p_input;
99     INIT_PLAYLIST_STUFF;
100
101     while( (psz_line = stream_ReadLine( p_demux->s )) )
102     {
103         char **ppsz_options = NULL;
104         int  i, i_options = 0;
105         char *psz_name = NULL;
106
107         if( !ParseLine( psz_line, &psz_name, &ppsz_options, &i_options ) )
108         {
109             free( psz_line );
110             continue;
111         }
112
113         EnsureUTF8( psz_name );
114
115         p_input = input_ItemNewExt( p_playlist, "dvb:", psz_name, 0, NULL, -1 );
116         for( i = 0; i< i_options; i++ )
117         {
118             EnsureUTF8( ppsz_options[i] );
119             input_ItemAddOption( p_input, ppsz_options[i] );
120         }
121         input_ItemAddSubItem( p_current_input, p_input );
122         vlc_gc_decref( p_input );
123         while( i_options-- ) free( ppsz_options[i_options] );
124         if( ppsz_options ) free( ppsz_options );
125
126         free( psz_line );
127     }
128
129     HANDLE_PLAY_AND_RELEASE;
130     return 0; /* Needed for correct operation of go back */
131 }
132
133 static struct
134 {
135     const char *psz_name;
136     const char *psz_option;
137
138 } dvb_options[] =
139 {
140     { "INVERSION_OFF", "dvb-inversion=0" },
141     { "INVERSION_ON", "dvb-inversion=1" },
142     { "INVERSION_AUTO", "dvb-inversion=2" },
143
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" },
148
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" },
159
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" },
165
166     { "HIERARCHY_NONE", "dvb-hierarchy=-1" },
167     { "HIERARCHY_1", "dvb-hierarchy=1" },
168     { "HIERARCHY_2", "dvb-hierarchy=2" },
169     { "HIERARCHY_4", "dvb-hierarchy=4" },
170
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"  },
180
181     { "TRANSMISSION_MODE_AUTO", "dvb-transmission=0" },
182     { "TRANSMISSION_MODE_2K", "dvb-transmission=2" },
183     { "TRANSMISSION_MODE_8K", "dvb-transmission=8" },
184     { 0, 0 }
185
186 };
187
188 static int ParseLine( char *psz_line, char **ppsz_name,
189                       char ***pppsz_options, int *pi_options )
190 {
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;
194
195     if( pppsz_options ) *pppsz_options = 0;
196     if( pi_options ) *pi_options = 0;
197     if( ppsz_name ) *ppsz_name = 0;
198
199     /* Skip leading tabs and spaces */
200     while( *psz_parse == ' ' || *psz_parse == '\t' ||
201            *psz_parse == '\n' || *psz_parse == '\r' ) psz_parse++;
202
203     /* Ignore comments */
204     if( *psz_parse == '#' ) return VLC_FALSE;
205
206     while( psz_parse )
207     {
208         const char *psz_option = 0;
209         char *psz_end = strchr( psz_parse, ':' );
210         if( psz_end ) { *psz_end = 0; psz_end++; }
211
212         if( i_count == 0 )
213         {
214             /* Channel name */
215             psz_name = psz_parse;
216         }
217         else if( i_count == 1 )
218         {
219             /* Frequency */
220             char *psz_end;
221             long i_value;
222
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;
226
227             i_frequency = i_value;
228         }
229         else
230         {
231             int i;
232
233             /* Check option name with our list */
234             for( i = 0; dvb_options[i].psz_name; i++ )
235             {
236                 if( !strcmp( psz_parse, dvb_options[i].psz_name ) )
237                 {
238                     psz_option = dvb_options[i].psz_option;
239
240                     /* If we recognize one of the strings, then we are sure
241                      * the data is really valid (ie. a channels file). */
242                     b_valid = VLC_TRUE;
243                     break;
244                 }
245             }
246
247             if( !psz_option )
248             {
249                 /* Option not recognized, test if it is a number */
250                 char *psz_end;
251                 long i_value;
252
253                 i_value = strtol( psz_parse, &psz_end, 10 );
254                 if( psz_end != psz_parse &&
255                     i_value != LONG_MAX && i_value != LONG_MIN )
256                 {
257                     i_program = i_value;
258                 }
259             }
260         }
261
262         if( psz_option && pppsz_options && pi_options )
263         {
264             char *psz_dup = strdup( psz_option );
265             if (psz_dup != NULL)
266                 INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
267                              psz_dup );
268         }
269
270         psz_parse = psz_end;
271         i_count++;
272     }
273
274     if( !b_valid && pppsz_options && pi_options )
275     {
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;
280     }
281
282     if( i_program && pppsz_options && pi_options )
283     {
284         char *psz_option;
285
286         asprintf( &psz_option, "program=%i", i_program );
287         INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
288                      psz_option );
289     }
290     if( i_frequency && pppsz_options && pi_options )
291     {
292         char *psz_option;
293
294         asprintf( &psz_option, "dvb-frequency=%i", i_frequency );
295         INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
296                      psz_option );
297     }
298     if( ppsz_name && psz_name ) *ppsz_name = strdup( psz_name );
299
300     return b_valid;
301 }
302
303 static int Control( demux_t *p_demux, int i_query, va_list args )
304 {
305     return VLC_EGENERIC;
306 }