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