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