af311bf5b4328bb291fd97d817d07a3922af1e0b
[vlc.git] / modules / demux / mpeg / ts.c
1 /*****************************************************************************
2  * mpeg_ts.c : Transport Stream input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id: ts.c,v 1.41 2003/11/06 20:12:37 gbazin Exp $
6  *
7  * Authors: Henri Fallon <henri@via.ecp.fr>
8  *          Johan Bilien <jobi@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31
32 #include "iso_lang.h"
33
34 #if defined MODULE_NAME_IS_ts_dvbpsi
35 #   ifdef HAVE_DVBPSI_DR_H
36 #       include <dvbpsi/dvbpsi.h>
37 #       include <dvbpsi/descriptor.h>
38 #       include <dvbpsi/pat.h>
39 #       include <dvbpsi/pmt.h>
40 #       include <dvbpsi/dr.h>
41 #   else
42 #       include "dvbpsi.h"
43 #       include "descriptor.h"
44 #       include "tables/pat.h"
45 #       include "tables/pmt.h"
46 #       include "descriptors/dr.h"
47 #   endif
48 #endif
49
50 #include "system.h"
51 #include "codecs.h"
52
53 /*****************************************************************************
54  * Constants
55  *****************************************************************************/
56 #define TS_READ_ONCE 200
57
58 /*****************************************************************************
59  * Private structure
60  *****************************************************************************/
61 struct demux_sys_t
62 {
63     module_t *   p_module;
64     mpeg_demux_t mpeg;
65 };
66
67
68
69 #define local_iso639_getlang(p1, p2)                                         \
70 {                                                                           \
71     const iso639_lang_t * p_iso;                                            \
72     p_iso = GetLang_2T((char*)(p1));                                        \
73     if( p_iso && strcmp(p_iso->psz_native_name,"Unknown"))                  \
74     {                                                                       \
75         if( p_iso->psz_native_name[0] )                                     \
76             strncpy( (p2), p_iso->psz_native_name, 20 );                    \
77         else                                                                \
78             strncpy( (p2), p_iso->psz_eng_name, 20 );                       \
79     }                                                                       \
80     else                                                                    \
81     {                                                                       \
82         p_iso = GetLang_2B((char*)(p1));                                    \
83         if ( p_iso )                                                        \
84         {                                                                   \
85             if( p_iso->psz_native_name[0] )                                 \
86                 strncpy( (p2), p_iso->psz_native_name, 20 );                \
87             else                                                            \
88                 strncpy( (p2), p_iso->psz_eng_name, 20 );                   \
89         }                                                                   \
90         else                                                                \
91         {                                                                   \
92             strncpy( (p2), p1, 3 );                                         \
93         }                                                                   \
94     }                                                                       \
95 }
96
97 /*****************************************************************************
98  * Local prototypes
99  *****************************************************************************/
100 static int  Activate   ( vlc_object_t * );
101 static void Deactivate ( vlc_object_t * );
102 static int  Demux      ( input_thread_t * );
103
104 #if defined MODULE_NAME_IS_ts
105 static void TSDemuxPSI ( input_thread_t *, data_packet_t *,
106                           es_descriptor_t *, vlc_bool_t );
107 static void TSDecodePAT( input_thread_t *, es_descriptor_t *);
108 static void TSDecodePMT( input_thread_t *, es_descriptor_t *);
109 #define PSI_CALLBACK TSDemuxPSI
110 #elif defined MODULE_NAME_IS_ts_dvbpsi
111 static void TS_DVBPSI_DemuxPSI  ( input_thread_t *, data_packet_t *,
112                                   es_descriptor_t *, vlc_bool_t );
113 static void TS_DVBPSI_HandlePAT ( input_thread_t *, dvbpsi_pat_t * );
114 static void TS_DVBPSI_HandlePMT ( input_thread_t *, dvbpsi_pmt_t * );
115 #define PSI_CALLBACK TS_DVBPSI_DemuxPSI
116 #endif
117
118 /*****************************************************************************
119  * Module descriptor
120  *****************************************************************************/
121 #define VLS_BACKWARDS_COMPAT_TEXT N_("Compatibility with pre-0.4 VLS")
122 #define VLS_BACKWARDS_COMPAT_LONGTEXT N_( \
123     "The protocol for transmitting A/52 audio streams changed between VLC " \
124     "0.3.x and 0.4. By default VLC assumes you have the latest VLS. In case " \
125     "you're using an old version, select this option.")
126
127 #define BUGGY_PSI_TEXT N_("Buggy PSI")
128 #define BUGGY_PSI_LONGTEXT N_( \
129     "If you have a stream whose PSI packets do not feature incremented " \
130     "continuity counters, select this option.")
131
132 vlc_module_begin();
133 #if defined MODULE_NAME_IS_ts
134     set_description( _("ISO 13818-1 MPEG Transport Stream input") );
135     set_capability( "demux", 160 );
136     add_shortcut( "ts" );
137 #elif defined MODULE_NAME_IS_ts_dvbpsi
138     set_description( _("ISO 13818-1 MPEG Transport Stream input (libdvbpsi)") );
139     set_capability( "demux", 170 );
140     add_shortcut( "ts_dvbpsi" );
141 #endif
142     add_category_hint( N_("Miscellaneous"), NULL, VLC_TRUE );
143     add_bool( "vls-backwards-compat", 0, NULL,
144               VLS_BACKWARDS_COMPAT_TEXT, VLS_BACKWARDS_COMPAT_LONGTEXT, VLC_TRUE );
145     add_bool( "buggy-psi", 0, NULL, BUGGY_PSI_TEXT, BUGGY_PSI_LONGTEXT, VLC_TRUE );
146     set_callbacks( Activate, Deactivate );
147 vlc_module_end();
148
149 /*****************************************************************************
150  * Activate: initialize TS structures
151  *****************************************************************************/
152 static int Activate( vlc_object_t * p_this )
153 {
154     input_thread_t *    p_input = (input_thread_t *)p_this;
155     demux_sys_t *       p_demux;
156     es_descriptor_t *   p_pat_es;
157     es_ts_data_t *      p_demux_data;
158     stream_ts_data_t *  p_stream_data;
159     byte_t *            p_peek;
160
161     /* Set the demux function */
162     p_input->pf_demux = Demux;
163     p_input->pf_demux_control = demux_vaControlDefault;
164
165 #if 0
166     /* XXX Unused already done by src/input.c */
167     /* Initialize access plug-in structures. */
168     if( p_input->i_mtu == 0 )
169     {
170         /* Improve speed. */
171         msg_Dbg( p_input, "using default mtu (%d) with bufsize (%d)\n",
172                  p_input->i_mtu, INPUT_DEFAULT_BUFSIZE );
173         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
174     }
175 #endif
176
177     /* Have a peep at the show. */
178     if( input_Peek( p_input, &p_peek, 1 ) < 1 )
179     {
180         msg_Err( p_input, "cannot peek()" );
181         return -1;
182     }
183
184     if( *p_peek != TS_SYNC_CODE )
185     {
186         if( *p_input->psz_demux && ( !strncmp( p_input->psz_demux, "ts", 3 )
187                          || !strncmp( p_input->psz_demux, "ts_dvbpsi", 10 ) ) )
188         {
189             /* User forced */
190             msg_Err( p_input, "this does not look like a TS stream, continuing" );
191         }
192         else
193         {
194             msg_Warn( p_input, "TS module discarded (no sync)" );
195             return -1;
196         }
197     }
198
199     /* Adapt the bufsize for our only use. */
200     if( p_input->i_mtu != 0 )
201     {
202         /* Have minimum granularity to avoid bottlenecks at the input level. */
203         p_input->i_bufsize = (p_input->i_mtu / TS_PACKET_SIZE) * TS_PACKET_SIZE;
204     }
205
206     p_demux = p_input->p_demux_data = malloc( sizeof(demux_sys_t ) );
207     if( p_demux == NULL )
208     {
209         return -1;
210     }
211
212     p_input->p_private = (void*)&p_demux->mpeg;
213     p_demux->p_module = module_Need( p_input, "mpeg-system", NULL );
214     if( p_demux->p_module == NULL )
215     {
216         free( p_input->p_demux_data );
217         return -1;
218     }
219
220     vlc_mutex_lock( &p_input->stream.stream_lock );
221
222     if( input_InitStream( p_input, sizeof( stream_ts_data_t ) ) == -1 )
223     {
224         module_Unneed( p_input, p_demux->p_module );
225         free( p_input->p_demux_data );
226         return -1;
227     }
228
229     p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
230     p_stream_data->i_pat_version = PAT_UNINITIALIZED ;
231     p_stream_data->b_buggy_psi = config_GetInt( p_input, "buggy-psi" );
232
233 #ifdef MODULE_NAME_IS_ts_dvbpsi
234     p_stream_data->p_pat_handle = (dvbpsi_handle *)
235       dvbpsi_AttachPAT( (dvbpsi_pat_callback) &TS_DVBPSI_HandlePAT, p_input );
236
237     if( p_stream_data->p_pat_handle == NULL )
238     {
239         msg_Err( p_input, "could not create PAT decoder" );
240         module_Unneed( p_input, p_demux->p_module );
241         free( p_input->p_demux_data );
242         return -1;
243     }
244 #endif
245
246     /* We'll have to catch the PAT in order to continue
247      * Then the input will catch the PMT and then the others ES
248      * The PAT es is indepedent of any program. */
249     p_pat_es = input_AddES( p_input, NULL, 0x00,
250                             UNKNOWN_ES, NULL, sizeof( es_ts_data_t ) );
251     p_pat_es->i_fourcc = VLC_FOURCC( 'p', 'a', 't', ' ' );
252     p_demux_data = (es_ts_data_t *)p_pat_es->p_demux_data;
253     p_demux_data->b_psi = 1;
254     p_demux_data->i_psi_type = PSI_IS_PAT;
255     p_demux_data->p_psi_section = malloc(sizeof(psi_section_t));
256     p_demux_data->p_psi_section->b_is_complete = 1;
257     p_demux_data->i_continuity_counter = 0xFF;
258
259     vlc_mutex_unlock( &p_input->stream.stream_lock );
260
261     return 0;
262 }
263
264 /*****************************************************************************
265  * Deactivate: deinitialize TS structures
266  *****************************************************************************/
267 static void Deactivate( vlc_object_t * p_this )
268 {
269     input_thread_t *    p_input = (input_thread_t *)p_this;
270
271     module_Unneed( p_input, p_input->p_demux_data->p_module );
272     free( p_input->p_demux_data );
273 }
274
275 /*****************************************************************************
276  * Demux: reads and demuxes data packets
277  *****************************************************************************
278  * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
279  * packets.
280  *****************************************************************************/
281 static int Demux( input_thread_t * p_input )
282 {
283     demux_sys_t *   p_demux = p_input->p_demux_data;
284     int             i_read_once = (p_input->i_mtu ?
285                                    p_input->i_bufsize / TS_PACKET_SIZE :
286                                    TS_READ_ONCE);
287     int             i;
288
289     for( i = 0; i < i_read_once; i++ )
290     {
291         data_packet_t *     p_data;
292         ssize_t             i_result;
293
294         i_result = p_demux->mpeg.pf_read_ts( p_input, &p_data );
295
296         if( i_result <= 0 )
297         {
298             return i_result;
299         }
300
301         p_demux->mpeg.pf_demux_ts( p_input, p_data,
302                                    (psi_callback_t) &PSI_CALLBACK );
303     }
304
305     return i_read_once;
306 }
307
308
309 #if defined MODULE_NAME_IS_ts
310 /*
311  * PSI demultiplexing and decoding without libdvbpsi
312  */
313
314 /*****************************************************************************
315  * DemuxPSI : makes up complete PSI data
316  *****************************************************************************/
317 static void TSDemuxPSI( input_thread_t * p_input, data_packet_t * p_data,
318         es_descriptor_t * p_es, vlc_bool_t b_unit_start )
319 {
320     es_ts_data_t  * p_demux_data;
321
322     p_demux_data = (es_ts_data_t *)p_es->p_demux_data;
323
324 #define p_psi (p_demux_data->p_psi_section)
325 #define p (p_data->p_payload_start)
326
327     if( b_unit_start )
328     {
329         /* unit_start set to 1 -> presence of a pointer field
330          * (see ISO/IEC 13818 (2.4.4.2) which should be set to 0x00 */
331         if( (uint8_t)p[0] != 0x00 )
332         {
333             msg_Warn( p_input,
334                       "non-zero pointer field found, trying to continue" );
335             p+=(uint8_t)p[0];
336         }
337         else
338         {
339             p++;
340         }
341
342         /* This is the begining of a new section */
343
344         if( ((uint8_t)(p[1]) & 0xc0) != 0x80 )
345         {
346             msg_Warn( p_input, "invalid PSI packet" );
347             p_psi->b_trash = 1;
348         }
349         else
350         {
351             p_psi->i_section_length = ((p[1] & 0xF) << 8) | p[2];
352             p_psi->b_section_complete = 0;
353             p_psi->i_read_in_section = 0;
354             p_psi->i_section_number = (uint8_t)p[6];
355
356             if( p_psi->b_is_complete || p_psi->i_section_number == 0 )
357             {
358                 /* This is a new PSI packet */
359                 p_psi->b_is_complete = 0;
360                 p_psi->b_trash = 0;
361                 p_psi->i_version_number = ( p[5] >> 1 ) & 0x1f;
362                 p_psi->i_last_section_number = (uint8_t)p[7];
363
364                 /* We'll write at the begining of the buffer */
365                 p_psi->p_current = p_psi->buffer;
366             }
367             else
368             {
369                 if( p_psi->b_section_complete )
370                 {
371                     /* New Section of an already started PSI */
372                     p_psi->b_section_complete = 0;
373
374                     if( p_psi->i_version_number != (( p[5] >> 1 ) & 0x1f) )
375                     {
376                         msg_Warn( p_input,
377                                   "PSI version differs inside same PAT" );
378                         p_psi->b_trash = 1;
379                     }
380                     if( p_psi->i_section_number + 1 != (uint8_t)p[6] )
381                     {
382                         msg_Warn( p_input,
383                                   "PSI Section discontinuity, packet lost?" );
384                         p_psi->b_trash = 1;
385                     }
386                     else
387                         p_psi->i_section_number++;
388                 }
389                 else
390                 {
391                     msg_Warn( p_input, "got unexpected new PSI section" );
392                     p_psi->b_trash = 1;
393                 }
394             }
395         }
396     } /* b_unit_start */
397
398     if( !p_psi->b_trash )
399     {
400         /* read */
401         if( (p_data->p_payload_end - p) >=
402             ( p_psi->i_section_length - p_psi->i_read_in_section ) )
403         {
404             /* The end of the section is in this TS packet */
405             memcpy( p_psi->p_current, p,
406             (p_psi->i_section_length - p_psi->i_read_in_section) );
407
408             p_psi->b_section_complete = 1;
409             p_psi->p_current +=
410                 (p_psi->i_section_length - p_psi->i_read_in_section);
411
412             if( p_psi->i_section_number == p_psi->i_last_section_number )
413             {
414                 /* This was the last section of PSI */
415                 p_psi->b_is_complete = 1;
416
417                 switch( p_demux_data->i_psi_type)
418                 {
419                 case PSI_IS_PAT:
420                     TSDecodePAT( p_input, p_es );
421                     break;
422                 case PSI_IS_PMT:
423                     TSDecodePMT( p_input, p_es );
424                     break;
425                 default:
426                     msg_Warn( p_input, "received unknown PSI in DemuxPSI" );
427                 }
428             }
429         }
430         else
431         {
432             memcpy( p_psi->buffer, p, p_data->p_payload_end - p );
433             p_psi->i_read_in_section += p_data->p_payload_end - p;
434
435             p_psi->p_current += p_data->p_payload_end - p;
436         }
437     }
438
439 #undef p_psi
440 #undef p
441
442     input_DeletePacket( p_input->p_method_data, p_data );
443
444     return ;
445 }
446
447 /*****************************************************************************
448  * DecodePAT : Decodes Programm association table and deal with it
449  *****************************************************************************/
450 static void TSDecodePAT( input_thread_t * p_input, es_descriptor_t * p_es )
451 {
452     stream_ts_data_t  * p_stream_data;
453     es_ts_data_t      * p_demux_data;
454
455     pgrm_descriptor_t * p_pgrm;
456     es_descriptor_t   * p_current_es;
457     byte_t            * p_current_data;
458
459     int                 i_section_length, i_program_id, i_pmt_pid;
460     int                 i_loop, i_current_section;
461
462     vlc_bool_t          b_changed = 0;
463
464     p_demux_data = (es_ts_data_t *)p_es->p_demux_data;
465     p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
466
467 #define p_psi (p_demux_data->p_psi_section)
468
469     /* Not so fast, Mike ! If the PAT version has changed, we first check
470      * that its content has really changed before doing anything */
471     if( p_stream_data->i_pat_version != p_psi->i_version_number )
472     {
473         int i_programs = p_input->stream.i_pgrm_number;
474
475         p_current_data = p_psi->buffer;
476
477         do
478         {
479             i_section_length = ((uint32_t)(p_current_data[1] & 0xF) << 8) |
480                                  p_current_data[2];
481             i_current_section = (uint8_t)p_current_data[6];
482
483             for( i_loop = 0;
484                  ( i_loop < (i_section_length - 9) / 4 ) && !b_changed;
485                  i_loop++ )
486             {
487                 i_program_id = ( (uint32_t)*(p_current_data + i_loop * 4 + 8) << 8 )
488                                  | *(p_current_data + i_loop * 4 + 9);
489                 i_pmt_pid = ( ((uint32_t)*(p_current_data + i_loop * 4 + 10) & 0x1F)
490                                     << 8 )
491                                | *(p_current_data + i_loop * 4 + 11);
492
493                 if( i_program_id )
494                 {
495                     if( (p_pgrm = input_FindProgram( p_input, i_program_id ))
496                         && (p_current_es = input_FindES( p_input, i_pmt_pid ))
497                         && p_current_es->p_pgrm == p_pgrm
498                         && p_current_es->i_id == i_pmt_pid
499                         && ((es_ts_data_t *)p_current_es->p_demux_data)->b_psi
500                         && ((es_ts_data_t *)p_current_es->p_demux_data)
501                             ->i_psi_type == PSI_IS_PMT )
502                     {
503                         i_programs--;
504                     }
505                     else
506                     {
507                         b_changed = 1;
508                     }
509                 }
510             }
511
512             p_current_data += 3 + i_section_length;
513
514         } while( ( i_current_section < p_psi->i_last_section_number )
515                   && !b_changed );
516
517         /* If we didn't find the expected amount of programs, the PAT has
518          * changed. Otherwise, it only changed if b_changed is already != 0 */
519         b_changed = b_changed || i_programs;
520     }
521
522     if( b_changed )
523     {
524         /* PAT has changed. We are going to delete all programs and
525          * create new ones. We chose not to only change what was needed
526          * as a PAT change may mean the stream is radically changing and
527          * this is a secure method to avoid crashes */
528         es_ts_data_t      * p_es_demux;
529         pgrm_ts_data_t    * p_pgrm_demux;
530
531         p_current_data = p_psi->buffer;
532
533         /* Delete all programs */
534         while( p_input->stream.i_pgrm_number )
535         {
536             input_DelProgram( p_input, p_input->stream.pp_programs[0] );
537         }
538
539         do
540         {
541             i_section_length = ((uint32_t)(p_current_data[1] & 0xF) << 8) |
542                                  p_current_data[2];
543             i_current_section = (uint8_t)p_current_data[6];
544
545             for( i_loop = 0; i_loop < (i_section_length - 9) / 4 ; i_loop++ )
546             {
547                 i_program_id = ( (uint32_t)*(p_current_data + i_loop * 4 + 8) << 8 )
548                                  | *(p_current_data + i_loop * 4 + 9);
549                 i_pmt_pid = ( ((uint32_t)*(p_current_data + i_loop * 4 + 10) & 0x1F)
550                                     << 8 )
551                                | *(p_current_data + i_loop * 4 + 11);
552
553                 /* If program = 0, we're having info about NIT not PMT */
554                 if( i_program_id )
555                 {
556                     /* Add this program */
557                     p_pgrm = input_AddProgram( p_input, i_program_id,
558                                                sizeof( pgrm_ts_data_t ) );
559
560                     /* whatis the PID of the PMT of this program */
561                     p_pgrm_demux = (pgrm_ts_data_t *)p_pgrm->p_demux_data;
562                     p_pgrm_demux->i_pmt_version = PMT_UNINITIALIZED;
563
564                     /* Add the PMT ES to this program */
565                     p_current_es = input_AddES( p_input, p_pgrm,(uint16_t)i_pmt_pid,
566                         UNKNOWN_ES, NULL, sizeof( es_ts_data_t) );
567                     p_current_es->i_fourcc = VLC_FOURCC( 'p', 'm', 't', ' ' );
568                     p_es_demux = (es_ts_data_t *)p_current_es->p_demux_data;
569                     p_es_demux->b_psi = 1;
570                     p_es_demux->i_psi_type = PSI_IS_PMT;
571
572                     p_es_demux->p_psi_section =
573                                             malloc( sizeof( psi_section_t ) );
574                     p_es_demux->p_psi_section->b_is_complete = 0;
575                     p_es_demux->i_continuity_counter = 0xFF;
576                 }
577             }
578
579             p_current_data += 3 + i_section_length;
580
581         } while( i_current_section < p_psi->i_last_section_number );
582
583         /* Go to the beginning of the next section */
584         p_stream_data->i_pat_version = p_psi->i_version_number;
585
586     }
587 #undef p_psi
588
589 }
590
591 /*****************************************************************************
592  * DecodePMT : decode a given Program Stream Map
593  * ***************************************************************************
594  * When the PMT changes, it may mean a deep change in the stream, and it is
595  * careful to delete the ES and add them again. If the PMT doesn't change,
596  * there no need to do anything.
597  *****************************************************************************/
598 static void TSDecodePMT( input_thread_t * p_input, es_descriptor_t * p_es )
599 {
600
601     pgrm_ts_data_t            * p_pgrm_data;
602     es_ts_data_t              * p_demux_data;
603     vlc_bool_t b_vls_compat = config_GetInt( p_input, "vls-backwards-compat" );
604
605     p_demux_data = (es_ts_data_t *)p_es->p_demux_data;
606     p_pgrm_data = (pgrm_ts_data_t *)p_es->p_pgrm->p_demux_data;
607
608 #define p_psi (p_demux_data->p_psi_section)
609
610     if( p_psi->i_version_number != p_pgrm_data->i_pmt_version )
611     {
612         es_descriptor_t   * p_new_es;
613         es_ts_data_t      * p_es_demux;
614         byte_t            * p_current_data, * p_current_section;
615         int                 i_section_length,i_current_section;
616         int                 i_prog_info_length, i_loop;
617         int                 i_es_info_length, i_pid, i_stream_type;
618
619         p_current_section = p_psi->buffer;
620         p_current_data = p_psi->buffer;
621
622         p_pgrm_data->i_pcr_pid = ( ((uint32_t)*(p_current_section + 8) & 0x1F) << 8 ) |
623                                     *(p_current_section + 9);
624
625
626         /* Lock stream information */
627         vlc_mutex_lock( &p_input->stream.stream_lock );
628
629         /* Delete all ES in this program  except the PSI. We start from the
630          * end because i_es_number gets decremented after each deletion. */
631         for( i_loop = p_es->p_pgrm->i_es_number ; i_loop ; )
632         {
633             i_loop--;
634             p_es_demux = (es_ts_data_t *)
635                          p_es->p_pgrm->pp_es[i_loop]->p_demux_data;
636             if ( ! p_es_demux->b_psi )
637             {
638                 input_DelES( p_input, p_es->p_pgrm->pp_es[i_loop] );
639             }
640         }
641
642         /* Then add what we received in this PMT */
643         do
644         {
645             i_section_length = ( ((uint32_t)*(p_current_data + 1) & 0xF) << 8 ) |
646                                   *(p_current_data + 2);
647             i_current_section = (uint8_t)p_current_data[6];
648             i_prog_info_length = ( ((uint32_t)*(p_current_data + 10) & 0xF) << 8 ) |
649                                     *(p_current_data + 11);
650
651             /* For the moment we ignore program descriptors */
652             p_current_data += 12 + i_prog_info_length;
653
654             /* The end of the section, before the CRC is at
655              * p_current_section + i_section_length -1 */
656             while( p_current_data < p_current_section + i_section_length -1 )
657             {
658                 i_stream_type = (int)p_current_data[0];
659                 i_pid = ( ((uint32_t)*(p_current_data + 1) & 0x1F) << 8 ) |
660                            *(p_current_data + 2);
661                 i_es_info_length = ( ((uint32_t)*(p_current_data + 3) & 0xF) << 8 ) |
662                                       *(p_current_data + 4);
663
664                 /* Tell the interface what kind of stream it is and select
665                  * the required ones */
666                 {
667                     int i_fourcc, i_cat, i_stream_id;
668
669                     switch( i_stream_type )
670                     {
671                         case MPEG1_VIDEO_ES:
672                         case MPEG2_VIDEO_ES:
673                         case MPEG2_MOTO_VIDEO_ES:
674                             /* This isn't real, but we don't actually use
675                              * it. */
676                             i_stream_id = 0xE0;
677                             i_fourcc = VLC_FOURCC('m','p','g','v');
678                             i_cat = VIDEO_ES;
679                             break;
680                         case MPEG1_AUDIO_ES:
681                         case MPEG2_AUDIO_ES:
682                             /* This isn't real, but we don't actually use
683                              * it. */
684                             i_stream_id = 0xC0;
685                             i_fourcc = VLC_FOURCC('m','p','g','a');
686                             i_cat = AUDIO_ES;
687                             break;
688                         case A52_AUDIO_ES:
689                             if ( !b_vls_compat )
690                                 i_fourcc = VLC_FOURCC('a','5','2',' ');
691                             else
692                                 i_fourcc = VLC_FOURCC('a','5','2','b');
693                             i_stream_id = 0xBD;
694                             i_cat = AUDIO_ES;
695                             break;
696                         case LPCM_AUDIO_ES:
697                             i_fourcc = VLC_FOURCC('l','p','c','m');
698                             i_stream_id = 0xBD;
699                             i_cat = AUDIO_ES;
700                             break;
701                         case DVD_SPU_ES:
702                             if ( !b_vls_compat )
703                                 i_fourcc = VLC_FOURCC('s','p','u',' ');
704                             else
705                                 i_fourcc = VLC_FOURCC('s','p','u','b');
706                             i_stream_id = 0xBD;
707                             i_cat = SPU_ES;
708                             break;
709                         case SDDS_AUDIO_ES:
710                             i_fourcc = VLC_FOURCC('s','d','d','s');
711                             i_stream_id = 0xBD;
712                             i_cat = AUDIO_ES;
713                             break;
714                         case DTS_AUDIO_ES:
715                             i_fourcc = VLC_FOURCC('d','t','s',' ');
716                             i_stream_id = 0xBD;
717                             i_cat = AUDIO_ES;
718                             break;
719                         /* 'b' stands for 'buggy' */
720                         case A52B_AUDIO_ES:
721                             i_fourcc = VLC_FOURCC('a','5','2','b');
722                             i_stream_id = 0xBD;
723                             i_cat = AUDIO_ES;
724                             break;
725                         case LPCMB_AUDIO_ES:
726                             i_fourcc = VLC_FOURCC('l','p','c','b');
727                             i_stream_id = 0xBD;
728                             i_cat = AUDIO_ES;
729                             break;
730                         case DVDB_SPU_ES:
731                             i_fourcc = VLC_FOURCC('s','p','u','b');
732                             i_stream_id = 0xBD;
733                             i_cat = SPU_ES;
734                             break;
735                         case AAC_ADTS_AUDIO_ES:
736                             i_fourcc = VLC_FOURCC('m','p','4','a');
737                             i_cat = AUDIO_ES;
738                             i_stream_id = 0xfa;
739                             break;
740
741                         default :
742                             i_stream_id = 0;
743                             i_fourcc = 0;
744                             i_cat = UNKNOWN_ES;
745                             break;
746                     }
747
748                     /* Add this ES to the program */
749                     p_new_es = input_AddES( p_input, p_es->p_pgrm, (uint16_t)i_pid,
750                                    i_cat, NULL, sizeof( es_ts_data_t ) );
751
752                     ((es_ts_data_t *)p_new_es->p_demux_data)->i_continuity_counter = 0xFF;
753
754                     p_new_es->i_stream_id = i_stream_id;
755                     p_new_es->i_fourcc = i_fourcc;
756
757                 }
758
759                 p_current_data += 5 + i_es_info_length;
760             }
761
762             /* Go to the beginning of the next section*/
763             p_current_data += 3 + i_section_length;
764
765             p_current_section++;
766
767         } while( i_current_section < p_psi->i_last_section_number );
768
769         p_pgrm_data->i_pmt_version = p_psi->i_version_number;
770
771         /* if no program is selected :*/
772         if( !p_input->stream.p_selected_program )
773         {
774             pgrm_descriptor_t *     p_pgrm_to_select;
775             uint16_t i_id = (uint16_t)config_GetInt( p_input, "program" );
776
777             if( i_id != 0 ) /* if user specified a program */
778             {
779                 p_pgrm_to_select = input_FindProgram( p_input, i_id );
780
781                 if( p_pgrm_to_select && p_pgrm_to_select == p_es->p_pgrm )
782                     p_input->pf_set_program( p_input, p_pgrm_to_select );
783             }
784             else
785                     p_input->pf_set_program( p_input, p_es->p_pgrm );
786         }
787
788         /* if the pmt belongs to the currently selected program, we
789          * reselect it to update its ES */
790         else if( p_es->p_pgrm == p_input->stream.p_selected_program )
791         {
792             p_input->pf_set_program( p_input, p_es->p_pgrm );
793         }
794
795         /* inform interface that stream has changed */
796         p_input->stream.b_changed = 1;
797         /*  Remove lock */
798         vlc_mutex_unlock( &p_input->stream.stream_lock );
799     }
800
801 #undef p_psi
802 }
803
804 #elif defined MODULE_NAME_IS_ts_dvbpsi
805 /*
806  * PSI Decoding using libdvbpsi
807  */
808
809 /*****************************************************************************
810  * DemuxPSI : send the PSI to the right libdvbpsi decoder
811  *****************************************************************************/
812 static void TS_DVBPSI_DemuxPSI( input_thread_t  * p_input,
813                                 data_packet_t   * p_data,
814                                 es_descriptor_t * p_es,
815                                 vlc_bool_t        b_unit_start )
816 {
817     es_ts_data_t        * p_es_demux_data;
818     pgrm_ts_data_t      * p_pgrm_demux_data;
819     stream_ts_data_t    * p_stream_demux_data;
820
821     p_es_demux_data = (es_ts_data_t *)p_es->p_demux_data;
822     p_stream_demux_data = (stream_ts_data_t *) p_input->stream.p_demux_data;
823
824     switch( p_es_demux_data->i_psi_type)
825     {
826         case PSI_IS_PAT:
827             dvbpsi_PushPacket(
828                     (dvbpsi_handle)p_stream_demux_data->p_pat_handle,
829                     p_data->p_demux_start );
830             break;
831         case PSI_IS_PMT:
832             p_pgrm_demux_data = ( pgrm_ts_data_t * )p_es->p_pgrm->p_demux_data;
833             dvbpsi_PushPacket(
834                     (dvbpsi_handle)p_pgrm_demux_data->p_pmt_handle,
835                     p_data->p_demux_start );
836             break;
837         default:
838             msg_Warn( p_input, "received unknown PSI in DemuxPSI" );
839     }
840
841     input_DeletePacket( p_input->p_method_data, p_data );
842 }
843 /*****************************************************************************
844  * MP4 specific functions
845  *****************************************************************************/
846 static int  MP4_DescriptorLength( int *pi_data, uint8_t **pp_data )
847 {
848     unsigned int i_b;
849     unsigned int i_len = 0;
850     do
851     {
852         i_b = **pp_data;
853         (*pp_data)++;
854         (*pi_data)--;
855         i_len = ( i_len << 7 ) + ( i_b&0x7f );
856
857     } while( i_b&0x80 );
858
859     return( i_len );
860 }
861 static int MP4_GetByte( int *pi_data, uint8_t **pp_data )
862 {
863     if( *pi_data > 0 )
864     {
865         int i_b = **pp_data;
866         (*pp_data)++;
867         (*pi_data)--;
868         return( i_b );
869     }
870     else
871     {
872         return( 0 );
873     }
874 }
875
876 static int MP4_GetWord( int *pi_data, uint8_t **pp_data )
877 {
878     int i1, i2;
879     i1 = MP4_GetByte( pi_data, pp_data );
880     i2 = MP4_GetByte( pi_data, pp_data );
881     return( ( i1 << 8 ) | i2 );
882 }
883 static int MP4_Get3Bytes( int *pi_data, uint8_t **pp_data )
884 {
885     int i1, i2, i3;
886     i1 = MP4_GetByte( pi_data, pp_data );
887     i2 = MP4_GetByte( pi_data, pp_data );
888     i3 = MP4_GetByte( pi_data, pp_data );
889     return( ( i1 << 16 ) | ( i2 << 8) | i3 );
890 }
891
892 static uint32_t MP4_GetDWord( int *pi_data, uint8_t **pp_data )
893 {
894     uint32_t i1, i2;
895     i1 = MP4_GetWord( pi_data, pp_data );
896     i2 = MP4_GetWord( pi_data, pp_data );
897     return( ( i1 << 16 ) | i2 );
898 }
899
900 static char* MP4_GetURL( int *pi_data, uint8_t **pp_data )
901 {
902     char *url;
903     int i_url_len, i;
904
905     i_url_len = MP4_GetByte( pi_data, pp_data );
906     url = malloc( i_url_len + 1 );
907     for( i = 0; i < i_url_len; i++ )
908     {
909         url[i] = MP4_GetByte( pi_data, pp_data );
910     }
911     url[i_url_len] = '\0';
912     return( url );
913 }
914
915 static void MP4_IODParse( iod_descriptor_t *p_iod, int i_data, uint8_t *p_data )
916 {
917     int i;
918     int i_es_index;
919     uint8_t     i_flags;
920     vlc_bool_t  b_url;
921     int         i_iod_length;
922
923     fprintf( stderr, "\n************ IOD ************" );
924     for( i = 0; i < 255; i++ )
925     {
926         p_iod->es_descr[i].b_ok = 0;
927     }
928     i_es_index = 0;
929
930     if( i_data < 3 )
931     {
932         return;
933     }
934
935     p_iod->i_iod_label = MP4_GetByte( &i_data, &p_data );
936     fprintf( stderr, "\n* iod_label:%d", p_iod->i_iod_label );
937     fprintf( stderr, "\n* ===========" );
938     fprintf( stderr, "\n* tag:0x%x", p_data[0] );
939
940     if( MP4_GetByte( &i_data, &p_data ) != 0x02 )
941     {
942         fprintf( stderr, "\n ERR: tag != 0x02" );
943         return;
944     }
945
946     i_iod_length = MP4_DescriptorLength( &i_data, &p_data );
947     fprintf( stderr, "\n* length:%d", i_iod_length );
948     if( i_iod_length > i_data )
949     {
950         i_iod_length = i_data;
951     }
952
953     p_iod->i_od_id = ( MP4_GetByte( &i_data, &p_data ) << 2 );
954     i_flags = MP4_GetByte( &i_data, &p_data );
955     p_iod->i_od_id |= i_flags >> 6;
956     b_url = ( i_flags >> 5  )&0x01;
957
958     fprintf( stderr, "\n* od_id:%d", p_iod->i_od_id );
959     fprintf( stderr, "\n* url flag:%d", b_url );
960     fprintf( stderr, "\n* includeInlineProfileLevel flag:%d", ( i_flags >> 4 )&0x01 );
961
962     if( b_url )
963     {
964         p_iod->psz_url = MP4_GetURL( &i_data, &p_data );
965         fprintf( stderr, "\n* url string:%s", p_iod->psz_url );
966         fprintf( stderr, "\n*****************************\n" );
967         return;
968     }
969     else
970     {
971         p_iod->psz_url = NULL;
972     }
973
974     p_iod->i_ODProfileLevelIndication = MP4_GetByte( &i_data, &p_data );
975     p_iod->i_sceneProfileLevelIndication = MP4_GetByte( &i_data, &p_data );
976     p_iod->i_audioProfileLevelIndication = MP4_GetByte( &i_data, &p_data );
977     p_iod->i_visualProfileLevelIndication = MP4_GetByte( &i_data, &p_data );
978     p_iod->i_graphicsProfileLevelIndication = MP4_GetByte( &i_data, &p_data );
979
980     fprintf( stderr, "\n* ODProfileLevelIndication:%d", p_iod->i_ODProfileLevelIndication );
981     fprintf( stderr, "\n* sceneProfileLevelIndication:%d", p_iod->i_sceneProfileLevelIndication );
982     fprintf( stderr, "\n* audioProfileLevelIndication:%d", p_iod->i_audioProfileLevelIndication );
983     fprintf( stderr, "\n* visualProfileLevelIndication:%d", p_iod->i_visualProfileLevelIndication );
984     fprintf( stderr, "\n* graphicsProfileLevelIndication:%d", p_iod->i_graphicsProfileLevelIndication );
985
986
987     while( i_data > 0 && i_es_index < 255)
988     {
989         int i_tag, i_length;
990         int     i_data_sav;
991         uint8_t *p_data_sav;
992
993         i_tag = MP4_GetByte( &i_data, &p_data );
994         i_length = MP4_DescriptorLength( &i_data, &p_data );
995
996         i_data_sav = i_data;
997         p_data_sav = p_data;
998
999         i_data = i_length;
1000
1001         switch( i_tag )
1002         {
1003             case 0x03:
1004                 {
1005 #define es_descr    p_iod->es_descr[i_es_index]
1006                     int i_decoderConfigDescr_length;
1007                     fprintf( stderr, "\n* - ES_Descriptor length:%d", i_length );
1008                     es_descr.b_ok = 1;
1009
1010                     es_descr.i_es_id = MP4_GetWord( &i_data, &p_data );
1011                     i_flags = MP4_GetByte( &i_data, &p_data );
1012                     es_descr.b_streamDependenceFlag = ( i_flags >> 7 )&0x01;
1013                     b_url = ( i_flags >> 6 )&0x01;
1014                     es_descr.b_OCRStreamFlag = ( i_flags >> 5 )&0x01;
1015                     es_descr.i_streamPriority = i_flags & 0x1f;
1016                     fprintf( stderr, "\n*   * streamDependenceFlag:%d", es_descr.b_streamDependenceFlag );
1017                     fprintf( stderr, "\n*   * OCRStreamFlag:%d", es_descr.b_OCRStreamFlag );
1018                     fprintf( stderr, "\n*   * streamPriority:%d", es_descr.i_streamPriority );
1019
1020                     if( es_descr.b_streamDependenceFlag )
1021                     {
1022                         es_descr.i_dependOn_es_id = MP4_GetWord( &i_data, &p_data );
1023                         fprintf( stderr, "\n*   * dependOn_es_id:%d", es_descr.i_dependOn_es_id );
1024                     }
1025
1026                     if( b_url )
1027                     {
1028                         es_descr.psz_url = MP4_GetURL( &i_data, &p_data );
1029                         fprintf( stderr, "\n* url string:%s", es_descr.psz_url );
1030                     }
1031                     else
1032                     {
1033                         es_descr.psz_url = NULL;
1034                     }
1035
1036                     if( es_descr.b_OCRStreamFlag )
1037                     {
1038                         es_descr.i_OCR_es_id = MP4_GetWord( &i_data, &p_data );
1039                         fprintf( stderr, "\n*   * OCR_es_id:%d", es_descr.i_OCR_es_id );
1040                     }
1041
1042                     if( MP4_GetByte( &i_data, &p_data ) != 0x04 )
1043                     {
1044                         fprintf( stderr, "\n* ERR missing DecoderConfigDescr" );
1045                         es_descr.b_ok = 0;
1046                         break;
1047                     }
1048                     i_decoderConfigDescr_length = MP4_DescriptorLength( &i_data, &p_data );
1049
1050                     fprintf( stderr, "\n*   - DecoderConfigDesc length:%d", i_decoderConfigDescr_length );
1051 #define dec_descr   es_descr.dec_descr
1052                     dec_descr.i_objectTypeIndication = MP4_GetByte( &i_data, &p_data );
1053                     i_flags = MP4_GetByte( &i_data, &p_data );
1054                     dec_descr.i_streamType = i_flags >> 2;
1055                     dec_descr.b_upStream = ( i_flags >> 1 )&0x01;
1056                     dec_descr.i_bufferSizeDB = MP4_Get3Bytes( &i_data, &p_data );
1057                     dec_descr.i_maxBitrate = MP4_GetDWord( &i_data, &p_data );
1058                     dec_descr.i_avgBitrate = MP4_GetDWord( &i_data, &p_data );
1059                     fprintf( stderr, "\n*     * objectTypeIndication:0x%x", dec_descr.i_objectTypeIndication  );
1060                     fprintf( stderr, "\n*     * streamType:0x%x", dec_descr.i_streamType );
1061                     fprintf( stderr, "\n*     * upStream:%d", dec_descr.b_upStream );
1062                     fprintf( stderr, "\n*     * bufferSizeDB:%d", dec_descr.i_bufferSizeDB );
1063                     fprintf( stderr, "\n*     * maxBitrate:%d", dec_descr.i_maxBitrate );
1064                     fprintf( stderr, "\n*     * avgBitrate:%d", dec_descr.i_avgBitrate );
1065                     if( i_decoderConfigDescr_length > 13 && MP4_GetByte( &i_data, &p_data ) == 0x05 )
1066                     {
1067                         int i;
1068                         dec_descr.i_decoder_specific_info_len =
1069                             MP4_DescriptorLength( &i_data, &p_data );
1070                         if( dec_descr.i_decoder_specific_info_len > 0 )
1071                         {
1072                             dec_descr.p_decoder_specific_info =
1073                                 malloc( dec_descr.i_decoder_specific_info_len );
1074                         }
1075                         for( i = 0; i < dec_descr.i_decoder_specific_info_len; i++ )
1076                         {
1077                             dec_descr.p_decoder_specific_info[i] = MP4_GetByte( &i_data, &p_data );
1078                         }
1079                     }
1080                     else
1081                     {
1082                         dec_descr.i_decoder_specific_info_len = 0;
1083                         dec_descr.p_decoder_specific_info = NULL;
1084                     }
1085                 }
1086 #undef  dec_descr
1087 #define sl_descr    es_descr.sl_descr
1088                 {
1089                     int i_SLConfigDescr_length;
1090                     int i_predefined;
1091
1092                     if( MP4_GetByte( &i_data, &p_data ) != 0x06 )
1093                     {
1094                         fprintf( stderr, "\n* ERR missing SLConfigDescr" );
1095                         es_descr.b_ok = 0;
1096                         break;
1097                     }
1098                     i_SLConfigDescr_length = MP4_DescriptorLength( &i_data, &p_data );
1099
1100                     fprintf( stderr, "\n*   - SLConfigDescr length:%d", i_SLConfigDescr_length );
1101                     i_predefined = MP4_GetByte( &i_data, &p_data );
1102                     fprintf( stderr, "\n*     * i_predefined:0x%x", i_predefined  );
1103                     switch( i_predefined )
1104                     {
1105                         case 0x01:
1106                             {
1107                                 sl_descr.b_useAccessUnitStartFlag   = 0;
1108                                 sl_descr.b_useAccessUnitEndFlag     = 0;
1109                                 sl_descr.b_useRandomAccessPointFlag = 0;
1110                                 //sl_descr.b_useRandomAccessUnitsOnlyFlag = 0;
1111                                 sl_descr.b_usePaddingFlag           = 0;
1112                                 sl_descr.b_useTimeStampsFlags       = 0;
1113                                 sl_descr.b_useIdleFlag              = 0;
1114                                 sl_descr.b_durationFlag     = 0;    // FIXME FIXME
1115                                 sl_descr.i_timeStampResolution      = 1000;
1116                                 sl_descr.i_OCRResolution    = 0;    // FIXME FIXME
1117                                 sl_descr.i_timeStampLength          = 32;
1118                                 sl_descr.i_OCRLength        = 0;    // FIXME FIXME
1119                                 sl_descr.i_AU_Length                = 0;
1120                                 sl_descr.i_instantBitrateLength= 0; // FIXME FIXME
1121                                 sl_descr.i_degradationPriorityLength= 0;
1122                                 sl_descr.i_AU_seqNumLength          = 0;
1123                                 sl_descr.i_packetSeqNumLength       = 0;
1124                                 if( sl_descr.b_durationFlag )
1125                                 {
1126                                     sl_descr.i_timeScale            = 0;    // FIXME FIXME
1127                                     sl_descr.i_accessUnitDuration   = 0;    // FIXME FIXME
1128                                     sl_descr.i_compositionUnitDuration= 0;    // FIXME FIXME
1129                                 }
1130                                 if( !sl_descr.b_useTimeStampsFlags )
1131                                 {
1132                                     sl_descr.i_startDecodingTimeStamp   = 0;    // FIXME FIXME
1133                                     sl_descr.i_startCompositionTimeStamp= 0;    // FIXME FIXME
1134                                 }
1135                             }
1136                             break;
1137                         default:
1138                             fprintf( stderr, "\n* ERR unsupported SLConfigDescr predefined" );
1139                             es_descr.b_ok = 0;
1140                             break;
1141                     }
1142                 }
1143                 break;
1144 #undef  sl_descr
1145 #undef  es_descr
1146             default:
1147                 fprintf( stderr, "\n* - OD tag:0x%x length:%d (Unsupported)", i_tag, i_length );
1148                 break;
1149         }
1150
1151         p_data = p_data_sav + i_length;
1152         i_data = i_data_sav - i_length;
1153         i_es_index++;
1154     }
1155
1156
1157     fprintf( stderr, "\n*****************************\n" );
1158 }
1159
1160 static void MP4_IODClean( iod_descriptor_t *p_iod )
1161 {
1162     int i;
1163
1164     if( p_iod->psz_url )
1165     {
1166         free( p_iod->psz_url );
1167         p_iod->psz_url = NULL;
1168         return;
1169     }
1170
1171     for( i = 0; i < 255; i++ )
1172     {
1173 #define es_descr p_iod->es_descr[i]
1174         if( es_descr.b_ok )
1175         {
1176             if( es_descr.psz_url )
1177             {
1178                 free( es_descr.psz_url );
1179                 es_descr.psz_url = NULL;
1180             }
1181             else
1182             {
1183                 if( es_descr.dec_descr.p_decoder_specific_info != NULL )
1184                 {
1185                     free( es_descr.dec_descr.p_decoder_specific_info );
1186                     es_descr.dec_descr.p_decoder_specific_info = NULL;
1187                     es_descr.dec_descr.i_decoder_specific_info_len = 0;
1188                 }
1189             }
1190         }
1191         es_descr.b_ok = 0;
1192 #undef  es_descr
1193     }
1194 }
1195
1196 /*****************************************************************************
1197  * HandlePAT: will treat a PAT returned by dvbpsi
1198  *****************************************************************************/
1199 static void TS_DVBPSI_HandlePAT( input_thread_t * p_input,
1200                                  dvbpsi_pat_t * p_new_pat )
1201 {
1202     dvbpsi_pat_program_t *      p_pgrm;
1203     pgrm_descriptor_t *         p_new_pgrm;
1204     pgrm_ts_data_t *            p_pgrm_demux;
1205     es_descriptor_t *           p_current_es;
1206     es_ts_data_t *              p_es_demux;
1207     stream_ts_data_t *          p_stream_data;
1208
1209     vlc_mutex_lock( &p_input->stream.stream_lock );
1210
1211     p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
1212
1213     if( ( p_new_pat->b_current_next &&
1214           ( p_new_pat->i_version != p_stream_data->i_pat_version ) ) ||
1215         p_stream_data->i_pat_version == PAT_UNINITIALIZED  )
1216     {
1217         msg_Dbg( p_input, "Processing PAT version %d", p_new_pat->i_version );
1218
1219         /* Delete all programs */
1220         while( p_input->stream.i_pgrm_number )
1221         {
1222             pgrm_ts_data_t *p_pgrm_demux_old =
1223                 (pgrm_ts_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
1224
1225             if( p_pgrm_demux_old->b_mpeg4 )
1226             {
1227                 MP4_IODClean( &p_pgrm_demux_old->iod );
1228             }
1229
1230             /* Delete old PMT decoder */
1231             if( p_pgrm_demux_old->p_pmt_handle )
1232                 dvbpsi_DetachPMT( p_pgrm_demux_old->p_pmt_handle );
1233
1234             input_DelProgram( p_input, p_input->stream.pp_programs[0] );
1235         }
1236
1237         /* treat the new programs list */
1238         p_pgrm = p_new_pat->p_first_program;
1239
1240         while( p_pgrm )
1241         {
1242             msg_Dbg( p_input, "New program: %d", p_pgrm->i_number );
1243
1244             /* If program = 0, we're having info about NIT not PMT */
1245             if( p_pgrm->i_number )
1246             {
1247                 /* Add this program */
1248                 p_new_pgrm = input_AddProgram( p_input, p_pgrm->i_number,
1249                                             sizeof( pgrm_ts_data_t ) );
1250
1251                 p_pgrm_demux = (pgrm_ts_data_t *)p_new_pgrm->p_demux_data;
1252                 p_pgrm_demux->i_pmt_version = PMT_UNINITIALIZED;
1253
1254                 /* Add the PMT ES to this program */
1255                 p_current_es = input_AddES( p_input, p_new_pgrm,
1256                                             (uint16_t)p_pgrm->i_pid, UNKNOWN_ES,
1257                                             NULL, sizeof(es_ts_data_t) );
1258                 p_current_es->i_fourcc = VLC_FOURCC( 'p', 'm', 't', ' ' );
1259                 p_es_demux = (es_ts_data_t *)p_current_es->p_demux_data;
1260                 p_es_demux->b_psi = 1;
1261                 p_es_demux->i_psi_type = PSI_IS_PMT;
1262                 p_es_demux->p_psi_section = NULL;
1263                 p_es_demux->i_continuity_counter = 0xFF;
1264
1265                 /* Create a PMT decoder */
1266                 p_pgrm_demux->p_pmt_handle = (dvbpsi_handle *)
1267                     dvbpsi_AttachPMT( p_pgrm->i_number,
1268                             (dvbpsi_pmt_callback) &TS_DVBPSI_HandlePMT,
1269                             p_input );
1270
1271                 if( p_pgrm_demux->p_pmt_handle == NULL )
1272                 {
1273                     msg_Err( p_input, "could not create PMT decoder" );
1274                     p_input->b_error = 1;
1275                     return;
1276                 }
1277
1278             }
1279             p_pgrm = p_pgrm->p_next;
1280         }
1281
1282         p_stream_data->i_pat_version = p_new_pat->i_version;
1283     }
1284     vlc_mutex_unlock( &p_input->stream.stream_lock );
1285 }
1286
1287
1288 /*****************************************************************************
1289  * HandlePMT: will treat a PMT returned by dvbpsi
1290  *****************************************************************************/
1291 static void TS_DVBPSI_HandlePMT( input_thread_t * p_input,
1292                                  dvbpsi_pmt_t * p_new_pmt )
1293 {
1294     dvbpsi_pmt_es_t *       p_es;
1295     pgrm_descriptor_t *     p_pgrm;
1296     es_descriptor_t *       p_new_es;
1297     pgrm_ts_data_t *        p_pgrm_demux;
1298     vlc_bool_t b_vls_compat = config_GetInt( p_input, "vls-backwards-compat" );
1299
1300     vlc_mutex_lock( &p_input->stream.stream_lock );
1301
1302     p_pgrm = input_FindProgram( p_input, p_new_pmt->i_program_number );
1303
1304     if( p_pgrm == NULL )
1305     {
1306         msg_Warn( p_input, "PMT of unreferenced program found" );
1307         return;
1308     }
1309
1310     p_pgrm_demux = (pgrm_ts_data_t *)p_pgrm->p_demux_data;
1311     p_pgrm_demux->i_pcr_pid = p_new_pmt->i_pcr_pid;
1312
1313     if( ( p_new_pmt->b_current_next &&
1314           ( p_new_pmt->i_version != p_pgrm_demux->i_pmt_version ) ) ||
1315           p_pgrm_demux->i_pmt_version == PMT_UNINITIALIZED )
1316     {
1317         dvbpsi_descriptor_t *p_dr = p_new_pmt->p_first_descriptor;
1318         int i_loop;
1319
1320         msg_Dbg( p_input, "Processing PMT for program %d version %d",
1321                  p_new_pmt->i_program_number, p_new_pmt->i_version );
1322
1323         /* Delete all ES in this program  except the PSI. We start from the
1324          * end because i_es_number gets decremented after each deletion. */
1325         for( i_loop = p_pgrm->i_es_number ; i_loop > 0 ; )
1326         {
1327             es_ts_data_t *              p_es_demux;
1328
1329             i_loop--;
1330             p_es_demux = (es_ts_data_t *)
1331                          p_pgrm->pp_es[i_loop]->p_demux_data;
1332             if ( !p_es_demux->b_psi )
1333             {
1334                 input_DelES( p_input, p_pgrm->pp_es[i_loop] );
1335             }
1336         }
1337
1338         /* IOD */
1339         while( p_dr && ( p_dr->i_tag != 0x1d ) )
1340             p_dr = p_dr->p_next;
1341         if( p_dr)
1342         {
1343             msg_Warn( p_input, "found IOD descriptor" );
1344             MP4_IODParse( &p_pgrm_demux->iod, p_dr->i_length, p_dr->p_data );
1345         }
1346
1347         p_es = p_new_pmt->p_first_es;
1348         while( p_es )
1349         {
1350             vlc_fourcc_t i_fourcc;
1351             int i_size, i_cat, i_stream_id = 0;
1352             es_ts_data_t demux_data;
1353             BITMAPINFOHEADER *p_bih = NULL;
1354             WAVEFORMATEX *p_wf = NULL;
1355             char psz_desc[30];
1356
1357             memset( &demux_data, 0, sizeof(es_ts_data_t) );
1358             *psz_desc = 0;
1359
1360             switch( p_es->i_type )
1361             {
1362                 case MPEG1_VIDEO_ES:
1363                 case MPEG2_VIDEO_ES:
1364                 case MPEG2_MOTO_VIDEO_ES:
1365                     i_fourcc = VLC_FOURCC('m','p','g','v');
1366                     i_cat = VIDEO_ES;
1367                     break;
1368                 case MPEG1_AUDIO_ES:
1369                 case MPEG2_AUDIO_ES:
1370                     i_fourcc = VLC_FOURCC('m','p','g','a');
1371                     i_cat = AUDIO_ES;
1372                     break;
1373                 case A52_AUDIO_ES:
1374                     if ( !b_vls_compat )
1375                         i_fourcc = VLC_FOURCC('a','5','2',' ');
1376                     else
1377                         i_fourcc = VLC_FOURCC('a','5','2','b');
1378                     i_cat = AUDIO_ES;
1379                     i_stream_id = 0xBD;
1380                     break;
1381                 case DVD_SPU_ES:
1382                     if ( !b_vls_compat )
1383                         i_fourcc = VLC_FOURCC('s','p','u',' ');
1384                     else
1385                         i_fourcc = VLC_FOURCC('s','p','u','b');
1386                     i_cat = SPU_ES;
1387                     i_stream_id = 0xBD;
1388                     break;
1389                 case LPCM_AUDIO_ES:
1390                     i_fourcc = VLC_FOURCC('l','p','c','m');
1391                     i_cat = AUDIO_ES;
1392                     i_stream_id = 0xBD;
1393                     break;
1394                 case SDDS_AUDIO_ES:
1395                     i_fourcc = VLC_FOURCC('s','d','d','s');
1396                     i_stream_id = 0xBD;
1397                     i_cat = AUDIO_ES;
1398                     break;
1399                 case DTS_AUDIO_ES:
1400                     i_fourcc = VLC_FOURCC('d','t','s',' ');
1401                     i_stream_id = 0xBD;
1402                     i_cat = AUDIO_ES;
1403                     break;
1404                 case A52B_AUDIO_ES:
1405                     i_fourcc = VLC_FOURCC('a','5','2','b');
1406                     i_cat = AUDIO_ES;
1407                     i_stream_id = 0xBD;
1408                     break;
1409                 case DVDB_SPU_ES:
1410                     i_fourcc = VLC_FOURCC('s','p','u','b');
1411                     i_cat = SPU_ES;
1412                     i_stream_id = 0xBD;
1413                     break;
1414                 case LPCMB_AUDIO_ES:
1415                     i_fourcc = VLC_FOURCC('l','p','c','b');
1416                     i_cat = AUDIO_ES;
1417                     i_stream_id = 0xBD;
1418                     break;
1419                 case MPEG4_VIDEO_ES:
1420                     i_fourcc = VLC_FOURCC('m','p','4','v');
1421                     i_cat = VIDEO_ES;
1422                     i_stream_id = 0xfa;
1423                     break;
1424                 case MPEG4_AUDIO_ES:
1425                 case AAC_ADTS_AUDIO_ES:
1426                     i_fourcc = VLC_FOURCC('m','p','4','a');
1427                     i_cat = AUDIO_ES;
1428                     i_stream_id = 0xfa;
1429                     break;
1430                 case MSCODEC_VIDEO_ES:
1431                     i_fourcc = VLC_FOURCC(0,0,0,0);   /* fixed later */
1432                     i_cat = VIDEO_ES;
1433                     i_stream_id = 0xa0;
1434                     break;
1435                 case PES_PRIVATE_ES:
1436                     /* We need to check a descriptor to find the real codec */
1437                     i_fourcc = VLC_FOURCC(0,0,0,0);   /* fixed later */
1438                     i_cat = UNKNOWN_ES;
1439                     i_stream_id = 0xbd;
1440                     break;
1441                 default:
1442                     i_fourcc = 0;
1443                     i_cat = UNKNOWN_ES;
1444                     i_stream_id = 0;
1445             }
1446
1447             if( p_es->i_type == MPEG4_VIDEO_ES ||
1448                 p_es->i_type == MPEG4_AUDIO_ES )
1449             {
1450                 /* mpeg4 stream, search sl_descriptor */
1451                 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
1452
1453                 while( p_dr && ( p_dr->i_tag != 0x1f ) ) p_dr = p_dr->p_next;
1454
1455                 if( p_dr && p_dr->i_length == 2 )
1456                 {
1457                     int i_es_descr_index;
1458
1459                     demux_data.i_es_id =
1460                         ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
1461                     demux_data.p_es_descr = NULL;
1462
1463                     msg_Warn( p_input, "found SL_descriptor" );
1464                     for( i_es_descr_index = 0; i_es_descr_index < 255;
1465                          i_es_descr_index++ )
1466                     {
1467                         if( p_pgrm_demux->iod.es_descr[i_es_descr_index].b_ok &&
1468                             p_pgrm_demux->iod.es_descr[i_es_descr_index].i_es_id == demux_data.i_es_id )
1469                         {
1470                             demux_data.p_es_descr =
1471                                 &p_pgrm_demux->iod.es_descr[i_es_descr_index];
1472                             break;
1473                         }
1474                     }
1475                 }
1476
1477                 if( demux_data.p_es_descr != NULL )
1478                 {
1479 #define DESCR demux_data.p_es_descr->dec_descr
1480                     demux_data.b_mpeg4 = 1;
1481
1482                     /* fix fourcc */
1483                     switch( DESCR.i_streamType )
1484                     {
1485                     case 0x04:  /* VisualStream */
1486                         i_cat = VIDEO_ES;
1487                         switch( DESCR.i_objectTypeIndication )
1488                         {
1489                         case 0x20:
1490                             i_fourcc = VLC_FOURCC('m','p','4','v');    // mpeg4
1491                             break;
1492                         case 0x60:
1493                         case 0x61:
1494                         case 0x62:
1495                         case 0x63:
1496                         case 0x64:
1497                         case 0x65:
1498                             i_fourcc = VLC_FOURCC( 'm','p','g','v' );  // mpeg2
1499                             break;
1500                         case 0x6a:
1501                             i_fourcc = VLC_FOURCC( 'm','p','g','v' );  // mpeg1
1502                             break;
1503                         case 0x6c:
1504                             i_fourcc = VLC_FOURCC( 'j','p','e','g' );  // mpeg1
1505                             break;
1506                         default:
1507                             i_fourcc = 0;
1508                             break;
1509                         }
1510                         break;
1511                     case 0x05:  /* AudioStream */
1512                         i_cat = AUDIO_ES;
1513                         switch( DESCR.i_objectTypeIndication )
1514                         {
1515                         case 0x40:
1516                             i_fourcc = VLC_FOURCC('m','p','4','a');    // mpeg4
1517                             break;
1518                         case 0x66:
1519                         case 0x67:
1520                         case 0x68:
1521                             i_fourcc = VLC_FOURCC('m','p','4','a');// mpeg2 aac
1522                             break;
1523                         case 0x69:
1524                             i_fourcc = VLC_FOURCC('m','p','g','a');    // mpeg2
1525                             break;
1526                         case 0x6b:
1527                             i_fourcc = VLC_FOURCC('m','p','g','a');    // mpeg1
1528                             break;
1529                         default:
1530                             i_fourcc = 0;
1531                             break;
1532                         }
1533                         break;
1534                     default:
1535                         i_cat = UNKNOWN_ES;
1536                         i_fourcc = 0;
1537                         break;
1538                     }
1539
1540                     switch( i_cat )
1541                     {
1542                     case VIDEO_ES:
1543                         i_size = sizeof( BITMAPINFOHEADER ) +
1544                                  DESCR.i_decoder_specific_info_len;
1545                         p_bih = malloc( i_size );
1546                         p_bih->biSize = i_size;
1547                         p_bih->biWidth = 0;
1548                         p_bih->biHeight = 0;
1549                         p_bih->biPlanes = 1;
1550                         p_bih->biBitCount = 0;
1551                         p_bih->biCompression = 0;
1552                         p_bih->biSizeImage = 0;
1553                         p_bih->biXPelsPerMeter = 0;
1554                         p_bih->biYPelsPerMeter = 0;
1555                         p_bih->biClrUsed = 0;
1556                         p_bih->biClrImportant = 0;
1557                         memcpy( &p_bih[1],
1558                                 DESCR.p_decoder_specific_info,
1559                                 DESCR.i_decoder_specific_info_len );
1560                         break;
1561                     case AUDIO_ES:
1562                         i_size = sizeof( WAVEFORMATEX ) +
1563                                  DESCR.i_decoder_specific_info_len;
1564                         p_wf = malloc( i_size );
1565                         p_wf->wFormatTag = 0xffff;
1566                         p_wf->nChannels = 0;
1567                         p_wf->nSamplesPerSec = 0;
1568                         p_wf->nAvgBytesPerSec = 0;
1569                         p_wf->nBlockAlign = 1;
1570                         p_wf->wBitsPerSample = 0;
1571                         p_wf->cbSize = DESCR.i_decoder_specific_info_len;
1572                         memcpy( &p_wf[1],
1573                                 DESCR.p_decoder_specific_info,
1574                                 DESCR.i_decoder_specific_info_len );
1575                         break;
1576                     default:
1577                         break;
1578                     }
1579                 }
1580                 else
1581                 {
1582                     msg_Warn( p_input,
1583                               "mpeg4 stream without (valid) sl_descriptor" );
1584                     demux_data.b_mpeg4 = 0;
1585                 }
1586
1587             }
1588             else if( p_es->i_type == MSCODEC_VIDEO_ES )
1589             {
1590                 /* crapy ms codec stream, search private descriptor */
1591                 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
1592
1593                 while( p_dr && ( p_dr->i_tag != 0xa0 ) ) p_dr = p_dr->p_next;
1594
1595                 if( p_dr && p_dr->i_length >= 8 )
1596                 {
1597                     int i_bih_size;
1598                     i_fourcc = VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
1599                                            p_dr->p_data[2], p_dr->p_data[3] );
1600
1601                     i_bih_size = (p_dr->p_data[8] << 8) | p_dr->p_data[9];
1602                     i_size = sizeof( BITMAPINFOHEADER ) + i_bih_size;
1603
1604                     p_bih = malloc( i_size );
1605                     p_bih->biSize = i_size;
1606                     p_bih->biWidth = ( p_dr->p_data[4] << 8 )|p_dr->p_data[5];
1607                     p_bih->biHeight = ( p_dr->p_data[6] << 8 )|p_dr->p_data[7];
1608                     p_bih->biPlanes = 1;
1609                     p_bih->biBitCount = 0;
1610                     p_bih->biCompression = 0;
1611                     p_bih->biSizeImage = 0;
1612                     p_bih->biXPelsPerMeter = 0;
1613                     p_bih->biYPelsPerMeter = 0;
1614                     p_bih->biClrUsed = 0;
1615                     p_bih->biClrImportant = 0;
1616                     memcpy( &p_bih[1], &p_dr->p_data[10], i_bih_size );
1617                 }
1618                 else
1619                 {
1620                     msg_Warn( p_input, "private ms-codec stream without bih "
1621                               "private sl_descriptor" );
1622                     i_fourcc = 0;
1623                     i_cat = UNKNOWN_ES;
1624                 }
1625             }
1626             else if( p_es->i_type == PES_PRIVATE_ES )
1627             {
1628                 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
1629                 /* We have to find a descriptor giving the right codec */
1630
1631                 for(p_dr = p_es->p_first_descriptor; p_dr; p_dr = p_dr->p_next)
1632                 {
1633                     if( p_dr->i_tag == 0x6a )
1634                     {
1635                         /* A52 */
1636                         i_fourcc = VLC_FOURCC( 'a', '5', '2', ' ' );
1637                         i_cat    = AUDIO_ES;
1638                     }
1639 #ifdef _DVBPSI_DR_59_H_
1640                     else if( p_dr->i_tag == 0x59 )
1641                     {
1642                         uint16_t                n;
1643                         es_descriptor_t *       p_dvbsub_es;
1644                         es_ts_data_t            dvbsub_demux_data;
1645                         dvb_spuinfo_t*          p_info;
1646                         dvbpsi_subtitling_dr_t* sub;
1647                         demux_data.b_dvbsub = 1;
1648                         demux_data.i_dvbsub_es_count = 0;
1649
1650                         /* DVB subtitle */
1651                         i_fourcc = VLC_FOURCC( 'd', 'v', 'b', 's' );
1652                         i_cat    = SPU_ES;
1653
1654                         sub = dvbpsi_DecodeSubtitlingDr( p_dr );
1655                         for( n = 0; n < sub->i_subtitles_number; n++)
1656                         {
1657                             /* As each subtitle ES contains n languages, 
1658                              * We are going to create n fake ES for the n
1659                              * tracks */
1660                             local_iso639_getlang(
1661                                     sub->p_subtitle[n].i_iso6392_language_code,
1662                                     psz_desc);
1663                             p_dvbsub_es = input_AddES( p_input,
1664                                                        p_pgrm,
1665                                                        0xfe12+n,
1666                                                        SPU_ES,
1667                                                        psz_desc,
1668                                                        sizeof(es_ts_data_t) );
1669                             if( p_dvbsub_es == NULL )
1670                             {
1671                                 msg_Err( p_input, "could not add ES %d",
1672                                                                 p_es->i_pid );
1673                                 p_input->b_error = 1;
1674                                 return;
1675                             }
1676
1677                             p_dvbsub_es->i_fourcc = i_fourcc;
1678                             p_dvbsub_es->i_stream_id = i_stream_id;
1679                             p_info = malloc(sizeof(dvb_spuinfo_t));
1680                             p_info->i_id =
1681                                     sub->p_subtitle[n].i_composition_page_id;
1682                             p_dvbsub_es->p_spuinfo = (void*) p_info;
1683                             memcpy( p_dvbsub_es->p_demux_data,
1684                                     &dvbsub_demux_data,
1685                                     sizeof(es_ts_data_t) );
1686                             ((es_ts_data_t *)p_dvbsub_es->p_demux_data)
1687                                                 ->i_continuity_counter = 0xFF;
1688                             // Finaly we add this stream to the index
1689                             demux_data.p_dvbsub_es[
1690                                 demux_data.i_dvbsub_es_count++] = p_dvbsub_es;
1691                             i_cat = UNKNOWN_ES;
1692                         }
1693
1694                     }
1695 #endif /* _DVBPSI_DR_59_H_ */
1696                 }
1697                 if( i_fourcc == VLC_FOURCC(0,0,0,0) )
1698                 {
1699                     msg_Warn( p_input,
1700                               "Unknown codec/type for Private PES stream" );
1701                 }
1702             }
1703
1704             if( i_cat == AUDIO_ES || i_cat == SPU_ES )
1705             {
1706                 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
1707                 while( p_dr && ( p_dr->i_tag != 0x0a ) ) p_dr = p_dr->p_next;
1708
1709                 if( p_dr )
1710                 {
1711                     dvbpsi_iso639_dr_t *p_decoded =
1712                                                 dvbpsi_DecodeISO639Dr( p_dr );
1713                     local_iso639_getlang(p_decoded->i_iso_639_code, psz_desc);
1714 #if 0
1715                     if( p_decoded->i_code_count > 0 )
1716                     {
1717                         const iso639_lang_t * p_iso;
1718                         p_iso = GetLang_2T((char*)p_decoded->i_iso_639_code);
1719
1720                         if( p_iso && strcmp(p_iso->psz_native_name,"Unknown"))
1721                         {
1722                             if( p_iso->psz_native_name[0] )
1723                                 strncpy( psz_desc,
1724                                          p_iso->psz_native_name, 20 );
1725                             else
1726                                 strncpy( psz_desc,
1727                                          p_iso->psz_eng_name, 20 );
1728                         }
1729                         else
1730                         {
1731                             p_iso = GetLang_2B(
1732                                         (char*)p_decoded->i_iso_639_code);
1733                             if ( p_iso )
1734                             {
1735                               if( p_iso->psz_native_name[0] )
1736                                 strncpy( psz_desc,
1737                                          p_iso->psz_native_name, 20 );
1738                               else
1739                                 strncpy( psz_desc,
1740                                          p_iso->psz_eng_name, 20 );
1741                             }
1742                             else
1743                             {
1744                               strncpy( psz_desc, p_decoded->i_iso_639_code, 3 );
1745                             }
1746                         }
1747                     }
1748 #endif
1749                 }
1750                 switch( p_es->i_type )
1751                 {
1752                     case MPEG1_AUDIO_ES:
1753                     case MPEG2_AUDIO_ES:
1754                         strcat( psz_desc, " (mpeg)" );
1755                         break;
1756                     case LPCM_AUDIO_ES:
1757                     case LPCMB_AUDIO_ES:
1758                         strcat( psz_desc, " (lpcm)" );
1759                         break;
1760                     case A52_AUDIO_ES:
1761                     case A52B_AUDIO_ES:
1762                         strcat( psz_desc, " (A52)" );
1763                         break;
1764                     case MPEG4_AUDIO_ES:
1765                     case AAC_ADTS_AUDIO_ES:
1766                         strcat( psz_desc, " (aac)" );
1767                         break;
1768                 }
1769             }
1770
1771             /* Add this ES */
1772             p_new_es = input_AddES( p_input, p_pgrm, (uint16_t)p_es->i_pid,
1773                                     i_cat, psz_desc, sizeof( es_ts_data_t ) );
1774             if( p_new_es == NULL )
1775             {
1776                 msg_Err( p_input, "could not add ES %d", p_es->i_pid );
1777                 p_input->b_error = 1;
1778                 return;
1779             }
1780             p_new_es->i_fourcc = i_fourcc;
1781             p_new_es->i_stream_id = i_stream_id;
1782             p_new_es->p_bitmapinfoheader = (void*)p_bih;
1783             p_new_es->p_waveformatex = (void*)p_wf;
1784             memcpy( p_new_es->p_demux_data, &demux_data,
1785                     sizeof(es_ts_data_t) );
1786
1787             ((es_ts_data_t *)p_new_es->p_demux_data)->i_continuity_counter =
1788                 0xFF;
1789
1790             p_es = p_es->p_next;
1791         }
1792
1793         /* if no program is selected :*/
1794         if( !p_input->stream.p_selected_program )
1795         {
1796             pgrm_descriptor_t *     p_pgrm_to_select;
1797             uint16_t i_id = (uint16_t)config_GetInt( p_input, "program" );
1798
1799             if( i_id != 0 ) /* if user specified a program */
1800             {
1801                 p_pgrm_to_select = input_FindProgram( p_input, i_id );
1802
1803                 if( p_pgrm_to_select && p_pgrm_to_select == p_pgrm )
1804                     p_input->pf_set_program( p_input, p_pgrm_to_select );
1805             }
1806             else
1807                 p_input->pf_set_program( p_input, p_pgrm );
1808         }
1809         /* if the pmt belongs to the currently selected program, we
1810          * reselect it to update its ES */
1811         else if( p_pgrm == p_input->stream.p_selected_program )
1812         {
1813             p_input->pf_set_program( p_input, p_pgrm );
1814         }
1815
1816         p_pgrm_demux->i_pmt_version = p_new_pmt->i_version;
1817         p_input->stream.b_changed = 1;
1818     }
1819     vlc_mutex_unlock( &p_input->stream.stream_lock );
1820 }
1821 #endif