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