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