]> git.sesse.net Git - vlc/blob - modules/access/dvb/scan.c
d256d031544d721075976f6157ba94fbabccac75
[vlc] / modules / access / dvb / scan.c
1 /*****************************************************************************
2  * scan.c: DVB scanner helpers
3  *****************************************************************************
4  * Copyright (C) 2008 the VideoLAN team
5  *
6  * Authors: Laurent Aimar <fenrir@videolan.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_access.h>
33 #include <vlc_dialog.h>
34
35 #ifdef HAVE_UNISTD_H
36 #   include <unistd.h>
37 #endif
38
39 #include <fcntl.h>
40 #include <sys/types.h>
41 #include <sys/poll.h>
42
43 #include <errno.h>
44
45 /* Include dvbpsi headers */
46 #ifdef HAVE_DVBPSI_DR_H
47 #   include <dvbpsi/dvbpsi.h>
48 #   include <dvbpsi/descriptor.h>
49 #   include <dvbpsi/pat.h>
50 #   include <dvbpsi/pmt.h>
51 #   include <dvbpsi/dr.h>
52 #   include <dvbpsi/psi.h>
53 #   include <dvbpsi/demux.h>
54 #   include <dvbpsi/sdt.h>
55 #else
56 #   include "dvbpsi.h"
57 #   include "descriptor.h"
58 #   include "tables/pat.h"
59 #   include "tables/pmt.h"
60 #   include "descriptors/dr.h"
61 #   include "psi.h"
62 #   include "demux.h"
63 #   include "tables/sdt.h"
64 #endif
65
66 #ifdef ENABLE_HTTPD
67 #   include "vlc_httpd.h"
68 #endif
69
70 #include "dvb.h"
71
72 /* */
73 scan_service_t *scan_service_New( int i_program, const scan_configuration_t *p_cfg  )
74 {
75     scan_service_t *p_srv = malloc( sizeof(*p_srv) );
76     if( !p_srv )
77         return NULL;
78
79     p_srv->i_program = i_program;
80     p_srv->cfg = *p_cfg;
81     p_srv->i_snr = -1;
82
83     p_srv->type = SERVICE_UNKNOWN;
84     p_srv->psz_name = NULL;
85     p_srv->i_channel = -1;
86     p_srv->b_crypted = false;
87
88     p_srv->i_network_id = -1;
89     p_srv->i_nit_version = -1;
90     p_srv->i_sdt_version = -1;
91
92     return p_srv;
93 }
94
95 void scan_service_Delete( scan_service_t *p_srv )
96 {
97     free( p_srv->psz_name );
98     free( p_srv );
99 }
100
101 /* */
102 int scan_Init( vlc_object_t *p_obj, scan_t *p_scan, const scan_parameter_t *p_parameter )
103 {
104     if( p_parameter->type == SCAN_DVB_T )
105     {
106         msg_Dbg( p_obj, "DVB-T scanning:" );
107         msg_Dbg( p_obj, " - frequency [%d, %d]",
108                  p_parameter->frequency.i_min, p_parameter->frequency.i_max );
109         msg_Dbg( p_obj, " - bandwidth [%d,%d]",
110                  p_parameter->bandwidth.i_min, p_parameter->bandwidth.i_max );
111         msg_Dbg( p_obj, " - exhaustive mode %s", p_parameter->b_exhaustive ? "on" : "off" );
112     }
113     else if( p_parameter->type == SCAN_DVB_C )
114     {
115         msg_Dbg( p_obj, "DVB-C scanning:" );
116         msg_Dbg( p_obj, " - frequency [%d, %d]",
117                  p_parameter->frequency.i_min, p_parameter->frequency.i_max );
118         msg_Dbg( p_obj, " - bandwidth [%d,%d]",
119                  p_parameter->bandwidth.i_min, p_parameter->bandwidth.i_max );
120         msg_Dbg( p_obj, " - exhaustive mode %s", p_parameter->b_exhaustive ? "on" : "off" );
121     }
122     else
123     {
124         return VLC_EGENERIC;
125     }
126
127     p_scan->p_obj = VLC_OBJECT(p_obj);
128     p_scan->i_index = 0;
129     p_scan->p_dialog = NULL;
130     TAB_INIT( p_scan->i_service, p_scan->pp_service );
131     p_scan->parameter = *p_parameter;
132     p_scan->i_time_start = mdate();
133
134     return VLC_SUCCESS;
135 }
136 void scan_Clean( scan_t *p_scan )
137 {
138     if( p_scan->p_dialog != NULL )
139         dialog_ProgressDestroy( p_scan->p_dialog );
140
141     for( int i = 0; i < p_scan->i_service; i++ )
142         scan_service_Delete( p_scan->pp_service[i] );
143     TAB_CLEAN( p_scan->i_service, p_scan->pp_service );
144 }
145
146 static int ScanDvbCNextFast( scan_t *p_scan, scan_configuration_t *p_cfg, double *pf_pos )
147 {
148     msg_Dbg( p_scan->p_obj, "Scan index %"PRId64, p_scan->i_index );
149     if( p_scan->i_index <= ( 10 ) )
150     {
151         p_cfg->i_frequency = 100500000 + ( ( p_scan->i_index ) * 700000);
152     } 
153     else if ( p_scan->i_index <= (10 + 9  ) )
154     {
155         p_cfg->i_frequency = 97000000 + ( ( p_scan->i_index - 20 ) * 800000);
156     }
157     else if ( p_scan->i_index <= (38 + 17 ) )
158     {
159         p_cfg->i_frequency = 14250000 + ( ( p_scan->i_index - 33 ) * 700000);
160     }
161     else if ( p_scan->i_index <= (72 + (90-21) ) )
162     {
163         p_cfg->i_frequency = 13800000 + ( ( p_scan->i_index - (72 + 21 ) )  * 800000);
164     }
165     else
166     {
167         return VLC_EGENERIC;
168     }
169     *pf_pos = (double)p_scan->i_index / ( (90-21+22-5+10+9));
170     return VLC_SUCCESS;
171
172 }
173
174 static int ScanDvbTNextExhaustive( scan_t *p_scan, scan_configuration_t *p_cfg, double *pf_pos )
175 {
176     if( p_scan->i_index > p_scan->parameter.frequency.i_count * p_scan->parameter.bandwidth.i_count )
177         return VLC_EGENERIC;
178
179     const int i_bi = p_scan->i_index % p_scan->parameter.bandwidth.i_count;
180     const int i_fi = p_scan->i_index / p_scan->parameter.bandwidth.i_count;
181
182     p_cfg->i_frequency = p_scan->parameter.frequency.i_min + i_fi * p_scan->parameter.frequency.i_step;
183     p_cfg->i_bandwidth = p_scan->parameter.bandwidth.i_min + i_bi * p_scan->parameter.bandwidth.i_step;
184
185     *pf_pos = (double)p_scan->i_index / p_scan->parameter.frequency.i_count;
186     return VLC_SUCCESS;
187 }
188
189 static int ScanDvbTNextFast( scan_t *p_scan, scan_configuration_t *p_cfg, double *pf_pos )
190 {
191     static const int i_band_count = 2;
192     static const struct
193     {
194         const char *psz_name;
195         int i_min;
196         int i_max;
197     }
198     band[2] =
199     {
200         { "VHF", 174, 230 },
201         { "UHF", 470, 862 },
202     };
203     const int i_offset_count = 5;
204     const int i_mhz = 1000000;
205
206     /* We will probe the whole band divided in all bandwidth possibility trying 
207      * i_offset_count offset around the position
208      */
209     for( ;; p_scan->i_index++ )
210     {
211
212         const int i_bi = p_scan->i_index % p_scan->parameter.bandwidth.i_count;
213         const int i_oi = (p_scan->i_index / p_scan->parameter.bandwidth.i_count) % i_offset_count;
214         const int i_fi = (p_scan->i_index / p_scan->parameter.bandwidth.i_count) / i_offset_count;
215
216         const int i_bandwidth = p_scan->parameter.bandwidth.i_min + i_bi * p_scan->parameter.bandwidth.i_step;
217         int i;
218
219         for( i = 0; i < i_band_count; i++ )
220         {
221             if( i_fi >= band[i].i_min && i_fi <= band[i].i_max )
222                 break;
223         }
224         if( i >=i_band_count )
225         {
226             if( i_fi > band[i_band_count-1].i_max )
227                 return VLC_EGENERIC;
228             continue;
229         }
230
231         const int i_frequency_min = band[i].i_min*i_mhz + i_bandwidth*i_mhz/2;
232         const int i_frequency_base = i_fi*i_mhz;
233
234         if( i_frequency_base >= i_frequency_min && ( i_frequency_base - i_frequency_min ) % ( i_bandwidth*i_mhz ) == 0 )
235         {
236             const int i_frequency = i_frequency_base + ( i_oi - i_offset_count/2 ) * p_scan->parameter.frequency.i_step;
237
238             if( i_frequency < p_scan->parameter.frequency.i_min ||
239                 i_frequency > p_scan->parameter.frequency.i_max )
240                 continue;
241
242             p_cfg->i_frequency = i_frequency;
243             p_cfg->i_bandwidth = i_bandwidth;
244
245             int i_current = 0, i_total = 0;
246             for( int i = 0; i < i_band_count; i++ )
247             {
248                 const int i_frag = band[i].i_max-band[i].i_min;
249
250                 if( i_fi >= band[i].i_min )
251                     i_current += __MIN( i_fi - band[i].i_min, i_frag );
252                 i_total += i_frag;
253             }
254
255             *pf_pos = (double)( i_current + (double)i_oi / i_offset_count ) / i_total;
256             return VLC_SUCCESS;
257         }
258     }
259 }
260
261 static int ScanDvbCNext( scan_t *p_scan, scan_configuration_t *p_cfg, double *pf_pos )
262 {
263     if( p_scan->parameter.b_exhaustive )
264         return ScanDvbTNextExhaustive( p_scan, p_cfg, pf_pos );
265     else
266         return ScanDvbCNextFast( p_scan, p_cfg, pf_pos );
267 }
268
269 static int ScanDvbTNext( scan_t *p_scan, scan_configuration_t *p_cfg, double *pf_pos )
270 {
271     if( p_scan->parameter.b_exhaustive )
272         return ScanDvbTNextExhaustive( p_scan, p_cfg, pf_pos );
273     else
274         return ScanDvbTNextFast( p_scan, p_cfg, pf_pos );
275 }
276
277 int scan_Next( scan_t *p_scan, scan_configuration_t *p_cfg )
278 {
279     double f_position;
280     int i_ret;
281
282     if( scan_IsCancelled( p_scan ) )
283         return VLC_EGENERIC;
284
285     memset( p_cfg, 0, sizeof(*p_cfg) );
286     switch( p_scan->parameter.type )
287     {
288     case SCAN_DVB_T:
289         i_ret = ScanDvbTNext( p_scan, p_cfg, &f_position );
290         break;
291     case SCAN_DVB_C:
292         i_ret = ScanDvbCNext( p_scan, p_cfg, &f_position );
293         break;
294     default:
295         i_ret = VLC_EGENERIC;
296         break;
297     }
298
299     if( i_ret )
300         return i_ret;
301
302     char *psz_text;
303     int i_service = 0;
304
305     for( int i = 0; i < p_scan->i_service; i++ )
306     {
307         if( p_scan->pp_service[i]->type != SERVICE_UNKNOWN )
308             i_service++;
309     }
310
311     if( asprintf( &psz_text, _("%.1f MHz (%d services)"), 
312                   (double)p_cfg->i_frequency / 1000000, i_service ) >= 0 )
313     {
314         const mtime_t i_eta = f_position > 0.005 ? (mdate() - p_scan->i_time_start) * ( 1.0 / f_position - 1.0 ) : -1;
315         char psz_eta[MSTRTIME_MAX_SIZE];
316
317         if( i_eta >= 0 )
318             msg_Info( p_scan->p_obj, "Scan ETA %s | %f", secstotimestr( psz_eta, i_eta/1000000 ), f_position * 100 );
319
320         if( p_scan->p_dialog == NULL )
321             p_scan->p_dialog = dialog_ProgressCreate( p_scan->p_obj, _("Scanning DVB-T"), psz_text, _("Cancel") );
322         if( p_scan->p_dialog != NULL )
323             /* FIXME: update text, not just percentage */
324             dialog_ProgressSet( p_scan->p_dialog, /*psz_text, */100 * f_position );
325         free( psz_text );
326     }
327
328     p_scan->i_index++;
329     return VLC_SUCCESS;
330 }
331
332 bool scan_IsCancelled( scan_t *p_scan )
333 {
334     return p_scan->p_dialog && dialog_ProgressCancelled( p_scan->p_dialog );
335 }
336
337 static scan_service_t *ScanFindService( scan_t *p_scan, int i_service_start, int i_program )
338 {
339     for( int i = i_service_start; i < p_scan->i_service; i++ )
340     {
341         if( p_scan->pp_service[i]->i_program == i_program )
342             return p_scan->pp_service[i];
343     }
344     return NULL;
345 }
346
347 /* FIXME handle properly string (convert to utf8) */
348 static void PATCallBack( scan_session_t *p_session, dvbpsi_pat_t *p_pat )
349 {
350     vlc_object_t *p_obj = p_session->p_obj;
351
352     msg_Dbg( p_obj, "PATCallBack" );
353
354     /* */
355     if( p_session->p_pat && p_session->p_pat->b_current_next )
356     {
357         dvbpsi_DeletePAT( p_session->p_pat );
358         p_session->p_pat = NULL;
359     }
360     if( p_session->p_pat )
361     {
362         dvbpsi_DeletePAT( p_pat );
363         return;
364     }
365
366     dvbpsi_pat_program_t *p_program;
367
368     /* */
369     p_session->p_pat = p_pat;
370
371     /* */
372     msg_Dbg( p_obj, "new PAT ts_id=%d version=%d current_next=%d",
373              p_pat->i_ts_id, p_pat->i_version, p_pat->b_current_next );
374     for( p_program = p_pat->p_first_program; p_program != NULL; p_program = p_program->p_next )
375     {
376         msg_Dbg( p_obj, "  * number=%d pid=%d", p_program->i_number, p_program->i_pid );
377         if( p_program->i_number == 0 )
378             p_session->i_nit_pid = p_program->i_pid;
379     }
380 }
381 static void SDTCallBack( scan_session_t *p_session, dvbpsi_sdt_t *p_sdt )
382 {
383     vlc_object_t *p_obj = p_session->p_obj;
384
385     msg_Dbg( p_obj, "SDTCallBack" );
386
387     if( p_session->p_sdt && p_session->p_sdt->b_current_next )
388     {
389         dvbpsi_DeleteSDT( p_session->p_sdt );
390         p_session->p_sdt = NULL;
391     }
392     if( p_session->p_sdt )
393     {
394         dvbpsi_DeleteSDT( p_sdt );
395         return;
396     }
397
398     /* */
399     p_session->p_sdt = p_sdt;
400
401     /* */
402     msg_Dbg( p_obj, "new SDT ts_id=%d version=%d current_next=%d network_id=%d",
403              p_sdt->i_ts_id, p_sdt->i_version, p_sdt->b_current_next,
404              p_sdt->i_network_id );
405
406
407     dvbpsi_sdt_service_t *p_srv;
408     for( p_srv = p_sdt->p_first_service; p_srv; p_srv = p_srv->p_next )
409     {
410         dvbpsi_descriptor_t *p_dr;
411
412         msg_Dbg( p_obj, "  * service id=%d eit schedule=%d present=%d running=%d free_ca=%d",
413                  p_srv->i_service_id, p_srv->b_eit_schedule,
414                  p_srv->b_eit_present, p_srv->i_running_status,
415                  p_srv->b_free_ca );
416         for( p_dr = p_srv->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
417         {
418             if( p_dr->i_tag == 0x48 )
419             {
420                 dvbpsi_service_dr_t *pD = dvbpsi_DecodeServiceDr( p_dr );
421                 char str2[257];
422
423                 memcpy( str2, pD->i_service_name, pD->i_service_name_length );
424                 str2[pD->i_service_name_length] = '\0';
425
426                 msg_Dbg( p_obj, "    - type=%d name=%s",
427                          pD->i_service_type, str2 );
428             }
429             else
430             {
431                 msg_Dbg( p_obj, "    * dsc 0x%x", p_dr->i_tag );
432             }
433         }
434     }
435 }
436
437 #ifdef DVBPSI_USE_NIT
438 static void NITCallBack( scan_session_t *p_session, dvbpsi_nit_t *p_nit )
439 {
440     vlc_object_t *p_obj = p_session->p_obj;
441
442     msg_Dbg( p_obj, "NITCallBack" );
443     msg_Dbg( p_obj, "new NIT network_id=%d version=%d current_next=%d",
444              p_nit->i_network_id, p_nit->i_version, p_nit->b_current_next );
445
446     /* */
447     if( p_session->p_nit && p_session->p_nit->b_current_next )
448     {
449         dvbpsi_DeleteNIT( p_session->p_nit );
450         p_session->p_nit = NULL;
451     }
452     if( p_session->p_nit )
453     {
454         dvbpsi_DeleteNIT( p_nit );
455         return;
456     }
457
458     /* */
459     p_session->p_nit = p_nit;
460
461     dvbpsi_descriptor_t *p_dsc;
462     for( p_dsc = p_nit->p_first_descriptor; p_dsc != NULL; p_dsc = p_dsc->p_next )
463     {
464         if( p_dsc->i_tag == 0x40 )
465         {
466             msg_Dbg( p_obj, "   * network name descriptor" );
467             char str1[257];
468
469             memcpy( str1, p_dsc->p_data, p_dsc->i_length );
470             str1[p_dsc->i_length] = '\0';
471             msg_Dbg( p_obj, "       * name %s", str1 );
472         }
473         else if( p_dsc->i_tag == 0x4a )
474         {
475             msg_Dbg( p_obj, "   * linkage descriptor" );
476             uint16_t i_ts_id = GetWBE( &p_dsc->p_data[0] );
477             uint16_t i_on_id = GetWBE( &p_dsc->p_data[2] );
478             uint16_t i_service_id = GetWBE( &p_dsc->p_data[4] );
479             int i_linkage_type = p_dsc->p_data[6];
480
481             msg_Dbg( p_obj, "       * ts_id %d", i_ts_id );
482             msg_Dbg( p_obj, "       * on_id %d", i_on_id );
483             msg_Dbg( p_obj, "       * service_id %d", i_service_id );
484             msg_Dbg( p_obj, "       * linkage_type %d", i_linkage_type );
485         }
486         else 
487         {
488             msg_Dbg( p_obj, "   * dsc 0x%x", p_dsc->i_tag );
489         }
490     }
491
492     dvbpsi_nit_ts_t *p_ts;
493     for( p_ts = p_nit->p_first_ts; p_ts != NULL; p_ts = p_ts->p_next )
494     {
495         msg_Dbg( p_obj, "   * ts ts_id=0x%x original_id=0x%x", p_ts->i_ts_id, p_ts->i_orig_network_id );
496
497         uint32_t i_private_data_id = 0;
498         dvbpsi_descriptor_t *p_dsc;
499         for( p_dsc = p_ts->p_first_descriptor; p_dsc != NULL; p_dsc = p_dsc->p_next )
500         {
501             if( p_dsc->i_tag == 0x41 )
502             {
503                 msg_Dbg( p_obj, "       * service list descriptor" );
504                 for( int i = 0; i < p_dsc->i_length/3; i++ )
505                 {
506                     uint16_t i_service_id = GetWBE( &p_dsc->p_data[3*i+0] );
507                     uint8_t  i_service_type = p_dsc->p_data[3*i+2];
508                     msg_Dbg( p_obj, "           * service_id=%d type=%d", i_service_id, i_service_type );
509                 }
510             }
511             else if( p_dsc->i_tag == 0x5a )
512             {
513                 dvbpsi_terr_deliv_sys_dr_t *p_t = dvbpsi_DecodeTerrDelivSysDr( p_dsc );
514                 msg_Dbg( p_obj, "       * terrestrial delivery system" );
515                 msg_Dbg( p_obj, "           * centre_frequency 0x%x", p_t->i_centre_frequency  );
516                 msg_Dbg( p_obj, "           * bandwidth %d", 8 - p_t->i_bandwidth );
517                 msg_Dbg( p_obj, "           * constellation %d", p_t->i_constellation );
518                 msg_Dbg( p_obj, "           * hierarchy %d", p_t->i_hierarchy_information );
519                 msg_Dbg( p_obj, "           * code_rate hp %d lp %d", p_t->i_code_rate_hp_stream, p_t->i_code_rate_lp_stream );
520                 msg_Dbg( p_obj, "           * guard_interval %d", p_t->i_guard_interval );
521                 msg_Dbg( p_obj, "           * transmission_mode %d", p_t->i_transmission_mode );
522                 msg_Dbg( p_obj, "           * other_frequency_flag %d", p_t->i_other_frequency_flag );
523             }
524             else if( p_dsc->i_tag == 0x5f )
525             {
526                 msg_Dbg( p_obj, "       * private data specifier descriptor" );
527                 i_private_data_id = GetDWBE( &p_dsc->p_data[0] );
528                 msg_Dbg( p_obj, "           * value 0x%8.8x", i_private_data_id );
529             }
530             else if( i_private_data_id == 0x28 && p_dsc->i_tag == 0x83 )
531             {
532                 msg_Dbg( p_obj, "       * logical channel descriptor (EICTA)" );
533                 for( int i = 0; i < p_dsc->i_length/4; i++ )
534                 {
535                     uint16_t i_service_id = GetWBE( &p_dsc->p_data[4*i+0] );
536                     int i_channel_number = GetWBE( &p_dsc->p_data[4*i+2] ) & 0x3ff;
537                     msg_Dbg( p_obj, "           * service_id=%d channel_number=%d", i_service_id, i_channel_number );
538                 }
539
540             }
541             else
542             {
543                 msg_Warn( p_obj, "       * dsc 0x%x", p_dsc->i_tag );
544             }
545         }
546     }
547 }
548 #endif
549
550 static void PSINewTableCallBack( scan_session_t *p_session, dvbpsi_handle h, uint8_t  i_table_id, uint16_t i_extension )
551 {
552     if( i_table_id == 0x42 )
553         dvbpsi_AttachSDT( h, i_table_id, i_extension, (dvbpsi_sdt_callback)SDTCallBack, p_session );
554 #ifdef DVBPSI_USE_NIT
555     else if( i_table_id == 0x40 )
556         dvbpsi_AttachNIT( h, i_table_id, i_extension, (dvbpsi_nit_callback)NITCallBack, p_session );
557 #endif
558 }
559
560
561 int scan_session_Init( vlc_object_t *p_obj, scan_session_t *p_session, const scan_configuration_t *p_cfg )
562 {
563     /* */
564     memset( p_session, 0, sizeof(*p_session) );
565     p_session->p_obj = p_obj;
566     p_session->cfg = *p_cfg;
567     p_session->i_snr = -1;
568     p_session->pat = NULL;
569     p_session->p_pat = NULL;
570     p_session->i_nit_pid = -1;
571     p_session->sdt = NULL;
572     p_session->p_sdt = NULL;
573 #ifdef DVBPSI_USE_NIT
574     p_session->nit = NULL;
575     p_session->p_nit = NULL;
576 #endif
577     return VLC_SUCCESS;
578 }
579
580 void scan_session_Clean( scan_t *p_scan, scan_session_t *p_session )
581 {
582     const int i_service_start = p_scan->i_service;
583
584     dvbpsi_pat_t *p_pat = p_session->p_pat;
585     dvbpsi_sdt_t *p_sdt = p_session->p_sdt;
586
587 #ifdef DVBPSI_USE_NIT
588     dvbpsi_nit_t *p_nit = p_session->p_nit;
589 #endif
590
591     if( p_pat )
592     {
593         /* Parse PAT */
594         dvbpsi_pat_program_t *p_program;
595         for( p_program = p_pat->p_first_program; p_program != NULL; p_program = p_program->p_next )
596         {
597             if( p_program->i_number == 0 )  /* NIT */
598                 continue;
599
600             scan_service_t *s = scan_service_New( p_program->i_number, &p_session->cfg );
601             TAB_APPEND( p_scan->i_service, p_scan->pp_service, s );
602         }
603     }
604     /* Parse SDT */
605     if( p_pat && p_sdt )
606     {
607         dvbpsi_sdt_service_t *p_srv;
608         for( p_srv = p_sdt->p_first_service; p_srv; p_srv = p_srv->p_next )
609         {
610             scan_service_t *s = ScanFindService( p_scan, i_service_start, p_srv->i_service_id );
611             dvbpsi_descriptor_t *p_dr;
612
613             if( s )
614                 s->b_crypted = p_srv->b_free_ca;
615
616             for( p_dr = p_srv->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
617             {
618                 if( p_dr->i_tag == 0x48 )
619                 {
620                     dvbpsi_service_dr_t *pD = dvbpsi_DecodeServiceDr( p_dr );
621
622                     if( s )
623                     {
624                         if( !s->psz_name )
625                             s->psz_name = dvbsi_to_utf8( pD->i_service_name, pD->i_service_name_length );
626
627                         if( s->type == SERVICE_UNKNOWN )
628                         {
629                             switch( pD->i_service_type )
630                             {
631                             case 0x01: s->type = SERVICE_DIGITAL_TELEVISION; break;
632                             case 0x02: s->type = SERVICE_DIGITAL_RADIO; break;
633                             case 0x16: s->type = SERVICE_DIGITAL_TELEVISION_AC_SD; break;
634                             case 0x19: s->type = SERVICE_DIGITAL_TELEVISION_AC_HD; break;
635                             }
636                         }
637                     }
638                 }
639             }
640         }
641     }
642
643 #ifdef DVBPSI_USE_NIT
644     /* Parse NIT */
645     if( p_pat && p_nit )
646     {
647         dvbpsi_nit_ts_t *p_ts;
648         for( p_ts = p_nit->p_first_ts; p_ts != NULL; p_ts = p_ts->p_next )
649         {
650             uint32_t i_private_data_id = 0;
651             dvbpsi_descriptor_t *p_dsc;
652
653             if( p_ts->i_orig_network_id != p_nit->i_network_id || p_ts->i_ts_id != p_pat->i_ts_id )
654                 continue;
655
656             for( p_dsc = p_ts->p_first_descriptor; p_dsc != NULL; p_dsc = p_dsc->p_next )
657             {
658                 if( p_dsc->i_tag == 0x5f )
659                 {
660                     i_private_data_id = GetDWBE( &p_dsc->p_data[0] );
661                 }
662                 else if( i_private_data_id == 0x28 && p_dsc->i_tag == 0x83 )
663                 {
664                     for( int i = 0; i < p_dsc->i_length/4; i++ )
665                     {
666                         uint16_t i_service_id = GetWBE( &p_dsc->p_data[4*i+0] );
667                         int i_channel_number = GetWBE( &p_dsc->p_data[4*i+2] ) & 0x3ff;
668
669                         scan_service_t *s = ScanFindService( p_scan, i_service_start, i_service_id );
670                         if( s && s->i_channel < 0 )
671                             s->i_channel = i_channel_number;
672                     }
673                 }
674             }
675         }
676     }
677 #endif
678
679     /* */
680     for( int i = i_service_start; i < p_scan->i_service; i++ )
681     {
682         scan_service_t *p_srv = p_scan->pp_service[i];
683
684         p_srv->i_snr = p_session->i_snr;
685         if( p_sdt )
686             p_srv->i_sdt_version = p_sdt->i_version;
687 #ifdef DVBPSI_USE_NIT
688         if( p_nit )
689         {
690             p_srv->i_network_id = p_nit->i_network_id;
691             p_srv->i_nit_version = p_nit->i_version;
692         }
693 #endif
694     }
695
696
697     /* */
698     if( p_session->pat )
699         dvbpsi_DetachPAT( p_session->pat );
700     if( p_session->p_pat )
701         dvbpsi_DeletePAT( p_session->p_pat );
702
703     if( p_session->sdt )
704         dvbpsi_DetachDemux( p_session->sdt );
705     if( p_session->p_sdt )
706         dvbpsi_DeleteSDT( p_session->p_sdt );
707 #ifdef DVBPSI_USE_NIT
708     if( p_session->nit )
709         dvbpsi_DetachDemux( p_session->nit );
710     if( p_session->p_nit )
711         dvbpsi_DeleteNIT( p_session->p_nit );
712 #endif
713 }
714
715 static int ScanServiceCmp( const void *a, const void *b )
716 {
717     scan_service_t *sa = *(scan_service_t**)a;
718     scan_service_t *sb = *(scan_service_t**)b;
719
720     if( sa->i_channel == sb->i_channel )
721     {
722         if( sa->psz_name && sb->psz_name )
723             return strcmp( sa->psz_name, sb->psz_name );
724         return 0;
725     }
726     if( sa->i_channel == -1 )
727         return 1;
728     else if( sb->i_channel == -1 )
729         return -1;
730
731     if( sa->i_channel < sb->i_channel )
732         return -1;
733     else if( sa->i_channel > sb->i_channel )
734         return 1;
735     return 0;
736 }
737
738 static block_t *BlockString( const char *psz )
739 {
740     block_t *p = block_Alloc( strlen(psz) );
741     if( p )
742         memcpy( p->p_buffer, psz, p->i_buffer );
743     return p;
744 }
745
746 block_t *scan_GetM3U( scan_t *p_scan )
747 {
748     vlc_object_t *p_obj = p_scan->p_obj;
749     block_t *p_playlist = NULL;
750
751     if( p_scan->i_service <= 0 )
752         return NULL;
753
754     /* */
755     qsort( p_scan->pp_service, p_scan->i_service, sizeof(scan_service_t*), ScanServiceCmp );
756
757     /* */
758     p_playlist = BlockString( "#EXTM3U\n\n" );/* */
759
760     for( int i = 0; i < p_scan->i_service; i++ )
761     {
762         scan_service_t *s = p_scan->pp_service[i];
763
764         if( s->type == SERVICE_UNKNOWN )
765         {
766             /* We should only select service that have been described by SDT */
767             msg_Dbg( p_obj, "scan_GetM3U: ignoring service number %d", s->i_program );
768             continue;
769         }
770
771         const char *psz_type;
772         switch( s->type )
773         {
774         case SERVICE_DIGITAL_TELEVISION:       psz_type = "Digital television"; break;
775         case SERVICE_DIGITAL_TELEVISION_AC_SD: psz_type = "Digital television advanced codec SD"; break;
776         case SERVICE_DIGITAL_TELEVISION_AC_HD: psz_type = "Digital television advanced codec HD"; break;
777         case SERVICE_DIGITAL_RADIO:            psz_type = "Digital radio"; break;
778         default:
779             psz_type = "Unknown";
780             break;
781         }
782         msg_Warn( p_obj, "scan_GetM3U: service number %d type '%s' name '%s' channel %d cypted=%d| network_id %d (nit:%d sdt:%d)| f=%d bw=%d snr=%d",
783                   s->i_program, psz_type, s->psz_name, s->i_channel, s->b_crypted,
784                   s->i_network_id, s->i_nit_version, s->i_sdt_version,
785                   s->cfg.i_frequency, s->cfg.i_bandwidth, s->i_snr );
786
787         char *psz;
788         if( asprintf( &psz, "#EXTINF:,,%s\n"
789                         "#EXTVLCOPT:program=%d\n"
790                         "dvb://frequency=%d:bandwidth=%d\n"
791                         "\n",
792                       s->psz_name && * s->psz_name ? s->psz_name : "Unknown",
793                       s->i_program,
794                       s->cfg.i_frequency, s->cfg.i_bandwidth ) < 0 )
795             psz = NULL;
796         if( psz )
797         {
798             block_t *p_block = BlockString( psz );
799             if( p_block )
800                 block_ChainAppend( &p_playlist, p_block );
801         }
802     }
803
804     return p_playlist ? block_ChainGather( p_playlist ) : NULL;
805 }
806
807 bool scan_session_Push( scan_session_t *p_scan, block_t *p_block )
808 {
809     if( p_block->i_buffer < 188 || p_block->p_buffer[0] != 0x47 )
810     {
811         block_Release( p_block );
812         return false;
813     }
814
815     /* */
816     const int i_pid = ( (p_block->p_buffer[1]&0x1f)<<8) | p_block->p_buffer[2];
817     if( i_pid == 0x00 )
818     {
819         if( !p_scan->pat )
820             p_scan->pat = dvbpsi_AttachPAT( (dvbpsi_pat_callback)PATCallBack, p_scan );
821
822         if( p_scan->pat )
823             dvbpsi_PushPacket( p_scan->pat, p_block->p_buffer );
824     }
825     else if( i_pid == 0x11 )
826     {
827         if( !p_scan->sdt )
828             p_scan->sdt = dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack, p_scan );
829
830         if( p_scan->sdt )
831             dvbpsi_PushPacket( p_scan->sdt, p_block->p_buffer );
832     }
833     else if( i_pid == p_scan->i_nit_pid )
834     {
835 #ifdef DVBPSI_USE_NIT
836         if( !p_scan->nit )
837             p_scan->nit = dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack, p_scan );
838
839         if( p_scan->nit )
840             dvbpsi_PushPacket( p_scan->nit, p_block->p_buffer );
841 #endif
842     }
843
844     block_Release( p_block );
845
846     return p_scan->p_pat && p_scan->p_sdt && 
847 #ifdef DVBPSI_USE_NIT
848         p_scan->p_nit;
849 #else
850         true;
851 #endif
852 }
853
854 void scan_service_SetSNR( scan_session_t *p_session, int i_snr )
855 {
856     p_session->i_snr = i_snr;
857 }
858