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