* modules/stream_out/transrate: New transrating module, based on GPL
[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.42 2003/11/07 21:30:52 massiot 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             msg_Dbg( p_input, "new PID 0x%x stream type 0x%x",
1360                      p_es->i_pid, p_es->i_type );
1361
1362             switch( p_es->i_type )
1363             {
1364                 case MPEG1_VIDEO_ES:
1365                 case MPEG2_VIDEO_ES:
1366                 case MPEG2_MOTO_VIDEO_ES:
1367                     i_fourcc = VLC_FOURCC('m','p','g','v');
1368                     i_cat = VIDEO_ES;
1369                     break;
1370                 case MPEG1_AUDIO_ES:
1371                 case MPEG2_AUDIO_ES:
1372                     i_fourcc = VLC_FOURCC('m','p','g','a');
1373                     i_cat = AUDIO_ES;
1374                     break;
1375                 case A52_AUDIO_ES:
1376                     if ( !b_vls_compat )
1377                         i_fourcc = VLC_FOURCC('a','5','2',' ');
1378                     else
1379                         i_fourcc = VLC_FOURCC('a','5','2','b');
1380                     i_cat = AUDIO_ES;
1381                     i_stream_id = 0xBD;
1382                     break;
1383                 case DVD_SPU_ES:
1384                     if ( !b_vls_compat )
1385                         i_fourcc = VLC_FOURCC('s','p','u',' ');
1386                     else
1387                         i_fourcc = VLC_FOURCC('s','p','u','b');
1388                     i_cat = SPU_ES;
1389                     i_stream_id = 0xBD;
1390                     break;
1391                 case LPCM_AUDIO_ES:
1392                     i_fourcc = VLC_FOURCC('l','p','c','m');
1393                     i_cat = AUDIO_ES;
1394                     i_stream_id = 0xBD;
1395                     break;
1396                 case SDDS_AUDIO_ES:
1397                     i_fourcc = VLC_FOURCC('s','d','d','s');
1398                     i_stream_id = 0xBD;
1399                     i_cat = AUDIO_ES;
1400                     break;
1401                 case DTS_AUDIO_ES:
1402                     i_fourcc = VLC_FOURCC('d','t','s',' ');
1403                     i_stream_id = 0xBD;
1404                     i_cat = AUDIO_ES;
1405                     break;
1406                 case A52B_AUDIO_ES:
1407                     i_fourcc = VLC_FOURCC('a','5','2','b');
1408                     i_cat = AUDIO_ES;
1409                     i_stream_id = 0xBD;
1410                     break;
1411                 case DVDB_SPU_ES:
1412                     i_fourcc = VLC_FOURCC('s','p','u','b');
1413                     i_cat = SPU_ES;
1414                     i_stream_id = 0xBD;
1415                     break;
1416                 case LPCMB_AUDIO_ES:
1417                     i_fourcc = VLC_FOURCC('l','p','c','b');
1418                     i_cat = AUDIO_ES;
1419                     i_stream_id = 0xBD;
1420                     break;
1421                 case MPEG4_VIDEO_ES:
1422                     i_fourcc = VLC_FOURCC('m','p','4','v');
1423                     i_cat = VIDEO_ES;
1424                     i_stream_id = 0xfa;
1425                     break;
1426                 case MPEG4_AUDIO_ES:
1427                 case AAC_ADTS_AUDIO_ES:
1428                     i_fourcc = VLC_FOURCC('m','p','4','a');
1429                     i_cat = AUDIO_ES;
1430                     i_stream_id = 0xfa;
1431                     break;
1432                 case MSCODEC_VIDEO_ES:
1433                     i_fourcc = VLC_FOURCC(0,0,0,0);   /* fixed later */
1434                     i_cat = VIDEO_ES;
1435                     i_stream_id = 0xa0;
1436                     break;
1437                 case PES_PRIVATE_ES:
1438                     /* We need to check a descriptor to find the real codec */
1439                     i_fourcc = VLC_FOURCC(0,0,0,0);   /* fixed later */
1440                     i_cat = UNKNOWN_ES;
1441                     i_stream_id = 0xbd;
1442                     break;
1443                 default:
1444                     i_fourcc = 0;
1445                     i_cat = UNKNOWN_ES;
1446                     i_stream_id = 0;
1447             }
1448
1449             if( p_es->i_type == MPEG4_VIDEO_ES ||
1450                 p_es->i_type == MPEG4_AUDIO_ES )
1451             {
1452                 /* mpeg4 stream, search sl_descriptor */
1453                 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
1454
1455                 while( p_dr && ( p_dr->i_tag != 0x1f ) ) p_dr = p_dr->p_next;
1456
1457                 if( p_dr && p_dr->i_length == 2 )
1458                 {
1459                     int i_es_descr_index;
1460
1461                     demux_data.i_es_id =
1462                         ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
1463                     demux_data.p_es_descr = NULL;
1464
1465                     msg_Warn( p_input, "found SL_descriptor" );
1466                     for( i_es_descr_index = 0; i_es_descr_index < 255;
1467                          i_es_descr_index++ )
1468                     {
1469                         if( p_pgrm_demux->iod.es_descr[i_es_descr_index].b_ok &&
1470                             p_pgrm_demux->iod.es_descr[i_es_descr_index].i_es_id == demux_data.i_es_id )
1471                         {
1472                             demux_data.p_es_descr =
1473                                 &p_pgrm_demux->iod.es_descr[i_es_descr_index];
1474                             break;
1475                         }
1476                     }
1477                 }
1478
1479                 if( demux_data.p_es_descr != NULL )
1480                 {
1481 #define DESCR demux_data.p_es_descr->dec_descr
1482                     demux_data.b_mpeg4 = 1;
1483
1484                     /* fix fourcc */
1485                     switch( DESCR.i_streamType )
1486                     {
1487                     case 0x04:  /* VisualStream */
1488                         i_cat = VIDEO_ES;
1489                         switch( DESCR.i_objectTypeIndication )
1490                         {
1491                         case 0x20:
1492                             i_fourcc = VLC_FOURCC('m','p','4','v');    // mpeg4
1493                             break;
1494                         case 0x60:
1495                         case 0x61:
1496                         case 0x62:
1497                         case 0x63:
1498                         case 0x64:
1499                         case 0x65:
1500                             i_fourcc = VLC_FOURCC( 'm','p','g','v' );  // mpeg2
1501                             break;
1502                         case 0x6a:
1503                             i_fourcc = VLC_FOURCC( 'm','p','g','v' );  // mpeg1
1504                             break;
1505                         case 0x6c:
1506                             i_fourcc = VLC_FOURCC( 'j','p','e','g' );  // mpeg1
1507                             break;
1508                         default:
1509                             i_fourcc = 0;
1510                             break;
1511                         }
1512                         break;
1513                     case 0x05:  /* AudioStream */
1514                         i_cat = AUDIO_ES;
1515                         switch( DESCR.i_objectTypeIndication )
1516                         {
1517                         case 0x40:
1518                             i_fourcc = VLC_FOURCC('m','p','4','a');    // mpeg4
1519                             break;
1520                         case 0x66:
1521                         case 0x67:
1522                         case 0x68:
1523                             i_fourcc = VLC_FOURCC('m','p','4','a');// mpeg2 aac
1524                             break;
1525                         case 0x69:
1526                             i_fourcc = VLC_FOURCC('m','p','g','a');    // mpeg2
1527                             break;
1528                         case 0x6b:
1529                             i_fourcc = VLC_FOURCC('m','p','g','a');    // mpeg1
1530                             break;
1531                         default:
1532                             i_fourcc = 0;
1533                             break;
1534                         }
1535                         break;
1536                     default:
1537                         i_cat = UNKNOWN_ES;
1538                         i_fourcc = 0;
1539                         break;
1540                     }
1541
1542                     switch( i_cat )
1543                     {
1544                     case VIDEO_ES:
1545                         i_size = sizeof( BITMAPINFOHEADER ) +
1546                                  DESCR.i_decoder_specific_info_len;
1547                         p_bih = malloc( i_size );
1548                         p_bih->biSize = i_size;
1549                         p_bih->biWidth = 0;
1550                         p_bih->biHeight = 0;
1551                         p_bih->biPlanes = 1;
1552                         p_bih->biBitCount = 0;
1553                         p_bih->biCompression = 0;
1554                         p_bih->biSizeImage = 0;
1555                         p_bih->biXPelsPerMeter = 0;
1556                         p_bih->biYPelsPerMeter = 0;
1557                         p_bih->biClrUsed = 0;
1558                         p_bih->biClrImportant = 0;
1559                         memcpy( &p_bih[1],
1560                                 DESCR.p_decoder_specific_info,
1561                                 DESCR.i_decoder_specific_info_len );
1562                         break;
1563                     case AUDIO_ES:
1564                         i_size = sizeof( WAVEFORMATEX ) +
1565                                  DESCR.i_decoder_specific_info_len;
1566                         p_wf = malloc( i_size );
1567                         p_wf->wFormatTag = 0xffff;
1568                         p_wf->nChannels = 0;
1569                         p_wf->nSamplesPerSec = 0;
1570                         p_wf->nAvgBytesPerSec = 0;
1571                         p_wf->nBlockAlign = 1;
1572                         p_wf->wBitsPerSample = 0;
1573                         p_wf->cbSize = DESCR.i_decoder_specific_info_len;
1574                         memcpy( &p_wf[1],
1575                                 DESCR.p_decoder_specific_info,
1576                                 DESCR.i_decoder_specific_info_len );
1577                         break;
1578                     default:
1579                         break;
1580                     }
1581                 }
1582                 else
1583                 {
1584                     msg_Warn( p_input,
1585                               "mpeg4 stream without (valid) sl_descriptor" );
1586                     demux_data.b_mpeg4 = 0;
1587                 }
1588
1589             }
1590             else if( p_es->i_type == MSCODEC_VIDEO_ES )
1591             {
1592                 /* crapy ms codec stream, search private descriptor */
1593                 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
1594
1595                 while( p_dr && ( p_dr->i_tag != 0xa0 ) ) p_dr = p_dr->p_next;
1596
1597                 if( p_dr && p_dr->i_length >= 8 )
1598                 {
1599                     int i_bih_size;
1600                     i_fourcc = VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
1601                                            p_dr->p_data[2], p_dr->p_data[3] );
1602
1603                     i_bih_size = (p_dr->p_data[8] << 8) | p_dr->p_data[9];
1604                     i_size = sizeof( BITMAPINFOHEADER ) + i_bih_size;
1605
1606                     p_bih = malloc( i_size );
1607                     p_bih->biSize = i_size;
1608                     p_bih->biWidth = ( p_dr->p_data[4] << 8 )|p_dr->p_data[5];
1609                     p_bih->biHeight = ( p_dr->p_data[6] << 8 )|p_dr->p_data[7];
1610                     p_bih->biPlanes = 1;
1611                     p_bih->biBitCount = 0;
1612                     p_bih->biCompression = 0;
1613                     p_bih->biSizeImage = 0;
1614                     p_bih->biXPelsPerMeter = 0;
1615                     p_bih->biYPelsPerMeter = 0;
1616                     p_bih->biClrUsed = 0;
1617                     p_bih->biClrImportant = 0;
1618                     memcpy( &p_bih[1], &p_dr->p_data[10], i_bih_size );
1619                 }
1620                 else
1621                 {
1622                     msg_Warn( p_input, "private ms-codec stream without bih "
1623                               "private sl_descriptor" );
1624                     i_fourcc = 0;
1625                     i_cat = UNKNOWN_ES;
1626                 }
1627             }
1628             else if( p_es->i_type == PES_PRIVATE_ES )
1629             {
1630                 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
1631                 /* We have to find a descriptor giving the right codec */
1632
1633                 for(p_dr = p_es->p_first_descriptor; p_dr; p_dr = p_dr->p_next)
1634                 {
1635                     if( p_dr->i_tag == 0x6a )
1636                     {
1637                         /* A52 */
1638                         i_fourcc = VLC_FOURCC( 'a', '5', '2', ' ' );
1639                         i_cat    = AUDIO_ES;
1640                     }
1641 #ifdef _DVBPSI_DR_59_H_
1642                     else if( p_dr->i_tag == 0x59 )
1643                     {
1644                         uint16_t                n;
1645                         es_descriptor_t *       p_dvbsub_es;
1646                         es_ts_data_t            dvbsub_demux_data;
1647                         dvb_spuinfo_t*          p_info;
1648                         dvbpsi_subtitling_dr_t* sub;
1649                         demux_data.b_dvbsub = 1;
1650                         demux_data.i_dvbsub_es_count = 0;
1651
1652                         /* DVB subtitle */
1653                         i_fourcc = VLC_FOURCC( 'd', 'v', 'b', 's' );
1654                         i_cat    = SPU_ES;
1655
1656                         sub = dvbpsi_DecodeSubtitlingDr( p_dr );
1657                         for( n = 0; n < sub->i_subtitles_number; n++)
1658                         {
1659                             /* As each subtitle ES contains n languages, 
1660                              * We are going to create n fake ES for the n
1661                              * tracks */
1662                             local_iso639_getlang(
1663                                     sub->p_subtitle[n].i_iso6392_language_code,
1664                                     psz_desc);
1665                             p_dvbsub_es = input_AddES( p_input,
1666                                                        p_pgrm,
1667                                                        0xfe12+n,
1668                                                        SPU_ES,
1669                                                        psz_desc,
1670                                                        sizeof(es_ts_data_t) );
1671                             if( p_dvbsub_es == NULL )
1672                             {
1673                                 msg_Err( p_input, "could not add ES %d",
1674                                                                 p_es->i_pid );
1675                                 p_input->b_error = 1;
1676                                 return;
1677                             }
1678
1679                             p_dvbsub_es->i_fourcc = i_fourcc;
1680                             p_dvbsub_es->i_stream_id = i_stream_id;
1681                             p_info = malloc(sizeof(dvb_spuinfo_t));
1682                             p_info->i_id =
1683                                     sub->p_subtitle[n].i_composition_page_id;
1684                             p_dvbsub_es->p_spuinfo = (void*) p_info;
1685                             memcpy( p_dvbsub_es->p_demux_data,
1686                                     &dvbsub_demux_data,
1687                                     sizeof(es_ts_data_t) );
1688                             ((es_ts_data_t *)p_dvbsub_es->p_demux_data)
1689                                                 ->i_continuity_counter = 0xFF;
1690                             // Finaly we add this stream to the index
1691                             demux_data.p_dvbsub_es[
1692                                 demux_data.i_dvbsub_es_count++] = p_dvbsub_es;
1693                             i_cat = UNKNOWN_ES;
1694                         }
1695
1696                     }
1697 #endif /* _DVBPSI_DR_59_H_ */
1698                 }
1699                 if( i_fourcc == VLC_FOURCC(0,0,0,0) )
1700                 {
1701                     msg_Warn( p_input,
1702                               "Unknown codec/type for Private PES stream" );
1703                 }
1704             }
1705
1706             if( i_cat == AUDIO_ES || i_cat == SPU_ES )
1707             {
1708                 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
1709                 while( p_dr && ( p_dr->i_tag != 0x0a ) ) p_dr = p_dr->p_next;
1710
1711                 if( p_dr )
1712                 {
1713                     dvbpsi_iso639_dr_t *p_decoded =
1714                                                 dvbpsi_DecodeISO639Dr( p_dr );
1715                     local_iso639_getlang(p_decoded->i_iso_639_code, psz_desc);
1716 #if 0
1717                     if( p_decoded->i_code_count > 0 )
1718                     {
1719                         const iso639_lang_t * p_iso;
1720                         p_iso = GetLang_2T((char*)p_decoded->i_iso_639_code);
1721
1722                         if( p_iso && strcmp(p_iso->psz_native_name,"Unknown"))
1723                         {
1724                             if( p_iso->psz_native_name[0] )
1725                                 strncpy( psz_desc,
1726                                          p_iso->psz_native_name, 20 );
1727                             else
1728                                 strncpy( psz_desc,
1729                                          p_iso->psz_eng_name, 20 );
1730                         }
1731                         else
1732                         {
1733                             p_iso = GetLang_2B(
1734                                         (char*)p_decoded->i_iso_639_code);
1735                             if ( p_iso )
1736                             {
1737                               if( p_iso->psz_native_name[0] )
1738                                 strncpy( psz_desc,
1739                                          p_iso->psz_native_name, 20 );
1740                               else
1741                                 strncpy( psz_desc,
1742                                          p_iso->psz_eng_name, 20 );
1743                             }
1744                             else
1745                             {
1746                               strncpy( psz_desc, p_decoded->i_iso_639_code, 3 );
1747                             }
1748                         }
1749                     }
1750 #endif
1751                 }
1752                 switch( p_es->i_type )
1753                 {
1754                     case MPEG1_AUDIO_ES:
1755                     case MPEG2_AUDIO_ES:
1756                         strcat( psz_desc, " (mpeg)" );
1757                         break;
1758                     case LPCM_AUDIO_ES:
1759                     case LPCMB_AUDIO_ES:
1760                         strcat( psz_desc, " (lpcm)" );
1761                         break;
1762                     case A52_AUDIO_ES:
1763                     case A52B_AUDIO_ES:
1764                         strcat( psz_desc, " (A52)" );
1765                         break;
1766                     case MPEG4_AUDIO_ES:
1767                     case AAC_ADTS_AUDIO_ES:
1768                         strcat( psz_desc, " (aac)" );
1769                         break;
1770                 }
1771             }
1772
1773             /* Add this ES */
1774             p_new_es = input_AddES( p_input, p_pgrm, (uint16_t)p_es->i_pid,
1775                                     i_cat, psz_desc, sizeof( es_ts_data_t ) );
1776             if( p_new_es == NULL )
1777             {
1778                 msg_Err( p_input, "could not add ES %d", p_es->i_pid );
1779                 p_input->b_error = 1;
1780                 return;
1781             }
1782             p_new_es->i_fourcc = i_fourcc;
1783             p_new_es->i_stream_id = i_stream_id;
1784             p_new_es->p_bitmapinfoheader = (void*)p_bih;
1785             p_new_es->p_waveformatex = (void*)p_wf;
1786             memcpy( p_new_es->p_demux_data, &demux_data,
1787                     sizeof(es_ts_data_t) );
1788
1789             ((es_ts_data_t *)p_new_es->p_demux_data)->i_continuity_counter =
1790                 0xFF;
1791
1792             p_es = p_es->p_next;
1793         }
1794
1795         /* if no program is selected :*/
1796         if( !p_input->stream.p_selected_program )
1797         {
1798             pgrm_descriptor_t *     p_pgrm_to_select;
1799             uint16_t i_id = (uint16_t)config_GetInt( p_input, "program" );
1800
1801             if( i_id != 0 ) /* if user specified a program */
1802             {
1803                 p_pgrm_to_select = input_FindProgram( p_input, i_id );
1804
1805                 if( p_pgrm_to_select && p_pgrm_to_select == p_pgrm )
1806                     p_input->pf_set_program( p_input, p_pgrm_to_select );
1807             }
1808             else
1809                 p_input->pf_set_program( p_input, p_pgrm );
1810         }
1811         /* if the pmt belongs to the currently selected program, we
1812          * reselect it to update its ES */
1813         else if( p_pgrm == p_input->stream.p_selected_program )
1814         {
1815             p_input->pf_set_program( p_input, p_pgrm );
1816         }
1817
1818         p_pgrm_demux->i_pmt_version = p_new_pmt->i_version;
1819         p_input->stream.b_changed = 1;
1820     }
1821     vlc_mutex_unlock( &p_input->stream.stream_lock );
1822 }
1823 #endif