]> git.sesse.net Git - vlc/blob - src/input/mpeg_system.c
* Filter plugins. Not very polished (please don't look at how the dates are
[vlc] / src / input / mpeg_system.c
1 /*****************************************************************************
2  * mpeg_system.c: TS, PS and PES management
3  *****************************************************************************
4  * Copyright (C) 1998-2001 VideoLAN
5  * $Id: mpeg_system.c,v 1.73 2001/12/13 17:33:47 massiot Exp $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Michel Lespinasse <walken@via.ecp.fr>
9  *          BenoĆ®t Steiner <benny@via.ecp.fr>
10  *          Samuel Hocevar <sam@via.ecp.fr>
11  *          Henri Fallon <henri@via.ecp.fr>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  * 
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
26  *****************************************************************************/
27
28 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31 #include "defs.h"
32
33 #include <stdlib.h>
34 #include <string.h>                                    /* memcpy(), memset() */
35 #include <sys/types.h>                                              /* off_t */
36
37 #include "common.h"
38 #include "intf_msg.h"
39 #include "threads.h"
40 #include "mtime.h"
41
42 #include "stream_control.h"
43 #include "input_ext-intf.h"
44 #include "input_ext-dec.h"
45 #include "input_ext-plugins.h"
46
47 /*****************************************************************************
48  * Local prototypes
49  *****************************************************************************/
50
51 static void input_DecodePAT( input_thread_t *, es_descriptor_t *);
52 static void input_DecodePMT( input_thread_t *, es_descriptor_t *);
53
54 /*
55  * PES Packet management
56  */
57
58 /*****************************************************************************
59  * MoveChunk
60  *****************************************************************************
61  * Small utility function used to parse discontinuous headers safely. Copies
62  * i_buf_len bytes of data to a buffer and returns the size copied.
63  * It also solves some alignment problems on non-IA-32, non-PPC processors.
64  * This is a variation on the theme of input_ext-dec.h:GetChunk().
65  *****************************************************************************/
66 static __inline__ size_t MoveChunk( byte_t * p_dest,
67                                     data_packet_t ** pp_data_src,
68                                     byte_t ** pp_src,
69                                     size_t i_buf_len )
70 {
71     ptrdiff_t           i_available;
72
73     if( (i_available = (*pp_data_src)->p_payload_end - *pp_src)
74             >= i_buf_len )
75     {
76         if( p_dest != NULL )
77             memcpy( p_dest, *pp_src, i_buf_len );
78         *pp_src += i_buf_len;
79         return( i_buf_len );
80     }
81     else
82     {
83         size_t          i_init_len = i_buf_len;
84
85         do
86         {
87             if( p_dest != NULL )
88                 memcpy( p_dest, *pp_src, i_available );
89             *pp_data_src = (*pp_data_src)->p_next;
90             i_buf_len -= i_available;
91             p_dest += i_available;
92             if( *pp_data_src == NULL )
93             {
94                 *pp_src = NULL;
95                 return( i_init_len - i_buf_len );
96             }
97             *pp_src = (*pp_data_src)->p_payload_start;
98         }
99         while( (i_available = (*pp_data_src)->p_payload_end - *pp_src)
100                 <= i_buf_len );
101
102         if( i_buf_len )
103         {
104             if( p_dest != NULL )
105                 memcpy( p_dest, *pp_src, i_buf_len );
106             *pp_src += i_buf_len;
107         }
108         return( i_init_len );
109     }
110 }
111
112 /*****************************************************************************
113  * input_ParsePES
114  *****************************************************************************
115  * Parse a finished PES packet and analyze its header.
116  *****************************************************************************/
117 #define PES_HEADER_SIZE     7
118 void input_ParsePES( input_thread_t * p_input, es_descriptor_t * p_es )
119 {
120     data_packet_t * p_data;
121     byte_t *        p_byte;
122     byte_t          p_header[PES_HEADER_SIZE];
123     int             i_done;
124
125 #define p_pes (p_es->p_pes)
126
127     //intf_DbgMsg("End of PES packet %p", p_pes);
128
129     /* Parse the header. The header has a variable length, but in order
130      * to improve the algorithm, we will read the 14 bytes we may be
131      * interested in */
132     p_data = p_pes->p_first;
133     p_byte = p_data->p_payload_start;
134     i_done = 0;
135
136     if( MoveChunk( p_header, &p_data, &p_byte, PES_HEADER_SIZE )
137             != PES_HEADER_SIZE )
138     {
139         intf_WarnMsg( 1, "input: PES packet too short to have a header" );
140         p_input->pf_delete_pes( p_input->p_method_data, p_pes );
141         p_pes = NULL;
142         return;
143     }
144
145     /* Get the PES size if defined */
146     p_es->i_pes_real_size = U16_AT(p_header + 4);
147     if( p_es->i_pes_real_size )
148     {
149         p_es->i_pes_real_size += 6;
150     }
151
152     /* First read the 6 header bytes common to all PES packets:
153      * use them to test the PES validity */
154     if( (p_header[0] || p_header[1] || (p_header[2] != 1)) )
155     {
156         /* packet_start_code_prefix != 0x000001 */
157         intf_ErrMsg( "input error: data loss, "
158                      "PES packet doesn't start with 0x000001" );
159         p_input->pf_delete_pes( p_input->p_method_data, p_pes );
160         p_pes = NULL;
161     }
162     else
163     {
164         int i_pes_header_size, i_payload_size;
165
166         if ( p_es->i_pes_real_size &&
167              (p_es->i_pes_real_size != p_pes->i_pes_size) )
168         {
169             /* PES_packet_length is set and != total received payload */
170             /* Warn the decoder that the data may be corrupt. */
171             intf_WarnMsg( 1, "input: packet corrupted, "
172                              "PES sizes do not match" );
173         }
174
175         switch( p_es->i_stream_id )
176         {
177         case 0xBC:  /* Program stream map */
178         case 0xBE:  /* Padding */
179         case 0xBF:  /* Private stream 2 */
180         case 0xB0:  /* ECM */
181         case 0xB1:  /* EMM */
182         case 0xFF:  /* Program stream directory */
183         case 0xF2:  /* DSMCC stream */
184         case 0xF8:  /* ITU-T H.222.1 type E stream */
185             /* The payload begins immediately after the 6 bytes header, so
186              * we have finished with the parsing */
187             i_pes_header_size = 6;
188             break;
189
190         default:
191             if( (p_header[6] & 0xC0) == 0x80 )
192             {
193                 /* MPEG-2 : the PES header contains at least 3 more bytes. */
194                 size_t      i_max_len;
195                 boolean_t   b_has_pts, b_has_dts;
196                 byte_t      p_full_header[12];
197
198                 p_pes->b_data_alignment = p_header[6] & 0x04;
199
200                 i_max_len = MoveChunk( p_full_header, &p_data, &p_byte, 12 );
201                 if( i_max_len < 2 )
202                 {
203                     intf_WarnMsg( 1,
204                             "PES packet too short to have a MPEG-2 header" );
205                     p_input->pf_delete_pes( p_input->p_method_data,
206                                             p_pes );
207                     p_pes = NULL;
208                     return;
209                 }
210
211                 b_has_pts = p_full_header[0] & 0x80;
212                 b_has_dts = p_full_header[0] & 0x40;
213                 i_pes_header_size = p_full_header[1] + 9;
214
215                 /* Now parse the optional header extensions */
216                 if( b_has_pts )
217                 {
218                     if( i_max_len < 7 )
219                     {
220                         intf_WarnMsg( 1,
221                             "PES packet too short to have a MPEG-2 header" );
222                         p_input->pf_delete_pes( p_input->p_method_data,
223                                                 p_pes );
224                         p_pes = NULL;
225                         return;
226                     }
227                     p_pes->i_pts = input_ClockGetTS( p_input, p_es->p_pgrm,
228                     ( ((mtime_t)(p_full_header[2] & 0x0E) << 29) |
229                       ((mtime_t)(p_full_header[3]) << 22) |
230                       ((mtime_t)(p_full_header[4] & 0xFE) << 14) |
231                       ((mtime_t)p_full_header[5] << 7) |
232                       ((mtime_t)p_full_header[6] >> 1) ) );
233
234                     if( b_has_dts )
235                     {
236                         if( i_max_len < 12 )
237                         {
238                             intf_WarnMsg( 1,
239                               "PES packet too short to have a MPEG-2 header" );
240                             p_input->pf_delete_pes( p_input->p_method_data,
241                                                     p_pes );
242                             p_pes = NULL;
243                             return;
244                         }
245                         p_pes->i_dts = input_ClockGetTS( p_input, p_es->p_pgrm,
246                         ( ((mtime_t)(p_full_header[7] & 0x0E) << 29) |
247                           (((mtime_t)U16_AT(p_full_header + 8) << 14)
248                                 - (1 << 14)) |
249                           ((mtime_t)U16_AT(p_full_header + 10) >> 1) ) );
250                     }
251                 }
252             }
253             else
254             {
255                 /* Probably MPEG-1 */
256                 boolean_t       b_has_pts, b_has_dts;
257
258                 i_pes_header_size = 6;
259                 p_data = p_pes->p_first;
260                 p_byte = p_data->p_payload_start;
261                 /* Cannot fail because the previous one succeeded. */
262                 MoveChunk( NULL, &p_data, &p_byte, 6 );
263
264                 while( *p_byte == 0xFF && i_pes_header_size < 23 )
265                 {
266                     i_pes_header_size++;
267                     if( MoveChunk( NULL, &p_data, &p_byte, 1 ) != 1 )
268                     {
269                         intf_WarnMsg( 1,
270                             "PES packet too short to have a MPEG-1 header" );
271                         p_input->pf_delete_pes( p_input->p_method_data, p_pes );
272                         p_pes = NULL;
273                         return;
274                     }
275                 }
276                 if( i_pes_header_size == 23 )
277                 {
278                     intf_ErrMsg( "input error: too much MPEG-1 stuffing" );
279                     p_input->pf_delete_pes( p_input->p_method_data, p_pes );
280                     p_pes = NULL;
281                     return;
282                 }
283
284                 if( (*p_byte & 0xC0) == 0x40 )
285                 {
286                     /* Don't ask why... --Meuuh */
287                     /* Erm... why ? --Sam */
288                     /* Well... According to the recommendation, it is for
289                      * STD_buffer_scale and STD_buffer_size. --Meuuh */
290                     i_pes_header_size += 2;
291                     if( MoveChunk( NULL, &p_data, &p_byte, 2 ) != 2 )
292                     {
293                         intf_WarnMsg( 1, "input: PES packet too short "
294                                          "to have a MPEG-1 header" );
295                         p_input->pf_delete_pes( p_input->p_method_data, p_pes );
296                         p_pes = NULL;
297                         return;
298                     }
299                 }
300
301                 i_pes_header_size++;
302
303                 b_has_pts = *p_byte & 0x20;
304                 b_has_dts = *p_byte & 0x10;
305
306                 if( b_has_pts )
307                 {
308                     byte_t      p_ts[5];
309
310                     i_pes_header_size += 4;
311                     if( MoveChunk( p_ts, &p_data, &p_byte, 5 ) != 5 )
312                     {
313                         intf_WarnMsg( 1, "input: PES packet too short "
314                                          "to have a MPEG-1 header" );
315                         p_input->pf_delete_pes( p_input->p_method_data, p_pes );
316                         p_pes = NULL;
317                         return;
318                     }
319
320                     p_pes->i_pts = input_ClockGetTS( p_input, p_es->p_pgrm,
321                        ( ((mtime_t)(p_ts[0] & 0x0E) << 29) |
322                          (((mtime_t)U32_AT(p_ts) & 0xFFFE00) << 6) |
323                          ((mtime_t)p_ts[3] << 7) |
324                          ((mtime_t)p_ts[4] >> 1) ) );
325
326                     if( b_has_dts )
327                     {
328                         i_pes_header_size += 5;
329                         if( MoveChunk( p_ts, &p_data, &p_byte, 5 ) != 5 )
330                         {
331                             intf_WarnMsg( 1, "input: PES packet too short "
332                                              "to have a MPEG-1 header" );
333                             p_input->pf_delete_pes( p_input->p_method_data,
334                                                     p_pes );
335                             p_pes = NULL;
336                             return;
337                         }
338
339                         p_pes->i_dts = input_ClockGetTS( p_input,
340                                                          p_es->p_pgrm,
341                             ( ((mtime_t)(p_ts[0] & 0x0E) << 29) |
342                               (((mtime_t)U32_AT(p_ts) & 0xFFFE00) << 6) |
343                               ((mtime_t)p_ts[3] << 7) |
344                               ((mtime_t)p_ts[4] >> 1) ) );
345                     }
346                 }
347             }
348
349             break;
350         }
351
352         if( p_es->i_stream_id == 0xbd )
353         {
354             /* With private stream 1, the first byte of the payload
355              * is a stream_private_id, so skip it. */
356             i_pes_header_size++;
357         }
358
359         /* Now we've parsed the header, we just have to indicate in some
360          * specific data packets where the PES payload begins (renumber
361          * p_payload_start), so that the decoders can find the beginning
362          * of their data right out of the box. */
363         p_data = p_pes->p_first;
364         i_payload_size = p_data->p_payload_end
365                                  - p_data->p_payload_start;
366         while( i_pes_header_size > i_payload_size )
367         {
368             /* These packets are entirely filled by the PES header. */
369             i_pes_header_size -= i_payload_size;
370             p_data->p_payload_start = p_data->p_payload_end;
371             /* Go to the next data packet. */
372             if( (p_data = p_data->p_next) == NULL )
373             {
374                 intf_ErrMsg( "input error: PES header bigger than payload" );
375                 p_input->pf_delete_pes( p_input->p_method_data, p_pes );
376                 p_pes = NULL;
377                 return;
378             }
379             i_payload_size = p_data->p_payload_end
380                                  - p_data->p_payload_start;
381         }
382         /* This last packet is partly header, partly payload. */
383         if( i_payload_size < i_pes_header_size )
384         {
385             intf_ErrMsg( "input error: PES header bigger than payload" );
386             p_input->pf_delete_pes( p_input->p_method_data, p_pes );
387             p_pes = NULL;
388             return;
389         }
390         p_data->p_payload_start += i_pes_header_size;
391
392         /* Now we can eventually put the PES packet in the decoder's
393          * PES fifo */
394         if( p_es->p_decoder_fifo != NULL )
395         {
396             input_DecodePES( p_es->p_decoder_fifo, p_pes );
397         }
398         else
399         {
400             intf_ErrMsg( "input error: no fifo to receive PES %p "
401                          "(who wrote this damn code ?)", p_pes );
402             p_input->pf_delete_pes( p_input->p_method_data, p_pes );
403         }
404         p_pes = NULL;
405     }
406 #undef p_pes
407
408 }
409
410 /*****************************************************************************
411  * input_GatherPES:
412  *****************************************************************************
413  * Gather a PES packet.
414  *****************************************************************************/
415 void input_GatherPES( input_thread_t * p_input, data_packet_t * p_data,
416                       es_descriptor_t * p_es,
417                       boolean_t b_unit_start, boolean_t b_packet_lost )
418 {
419 #define p_pes (p_es->p_pes)
420
421     //intf_DbgMsg("PES-demultiplexing %p (%p)", p_ts_packet, p_pes);
422
423     /* If we lost data, insert a NULL data packet (philosophy : 0 is quite
424      * often an escape sequence in decoders, so that should make them wait
425      * for the next start code). */
426     if( b_packet_lost )
427     {
428         input_NullPacket( p_input, p_es );
429     }
430
431     if( b_unit_start && p_pes != NULL )
432     {
433         /* If the data packet contains the begining of a new PES packet, and
434          * if we were reassembling a PES packet, then the PES should be
435          * complete now, so parse its header and give it to the decoders. */
436         input_ParsePES( p_input, p_es );
437     }
438
439     if( !b_unit_start && p_pes == NULL )
440     {
441         /* Random access... */
442         p_input->pf_delete_packet( p_input->p_method_data, p_data );
443     }
444     else
445     {
446         if( b_unit_start )
447         {
448             /* If we are at the beginning of a new PES packet, we must fetch
449              * a new PES buffer to begin with the reassembly of this PES
450              * packet. This is also here that we can synchronize with the
451              * stream if we lost packets or if the decoder has just
452              * started. */
453             if( (p_pes = p_input->pf_new_pes( p_input->p_method_data ) ) == NULL )
454             {
455                 intf_ErrMsg( "input error: out of memory" );
456                 p_input->b_error = 1;
457                 return;
458             }
459             p_pes->i_rate = p_input->stream.control.i_rate;
460             p_pes->p_first = p_data;
461             
462             /* If the PES header fits in the first data packet, we can
463              * already set p_gather->i_pes_real_size. */
464             if( p_data->p_payload_end - p_data->p_payload_start
465                     >= PES_HEADER_SIZE )
466             {
467                 p_es->i_pes_real_size =
468                                 U16_AT(p_data->p_payload_start + 4) + 6;
469                 
470             }
471             else
472             { 
473                 p_es->i_pes_real_size = 0;
474             } 
475         }
476         else
477         {
478             /* Update the relations between the data packets */
479             p_pes->p_last->p_next = p_data;
480         }
481
482         p_pes->p_last = p_data;
483         p_pes->i_nb_data++;
484
485         /* Size of the payload carried in the data packet */
486         p_pes->i_pes_size += (p_data->p_payload_end
487                                  - p_data->p_payload_start);
488     
489         /* We can check if the packet is finished */
490         if( p_pes->i_pes_size == p_es->i_pes_real_size )
491         {
492             /* The packet is finished, parse it */
493             input_ParsePES( p_input, p_es );
494         }
495     }
496 #undef p_pes
497 }
498
499
500 /*
501  * PS Demultiplexing
502  */
503
504 /*****************************************************************************
505  * GetID: Get the ID of a stream
506  *****************************************************************************/
507 static u16 GetID( data_packet_t * p_data )
508 {
509     u16         i_id;
510
511     i_id = p_data->p_payload_start[3];                                 /* stream_id */
512     if( i_id == 0xBD )
513     {
514         /* FIXME : this is not valid if the header is split in multiple
515          * packets */
516         /* stream_private_id */
517         i_id |= p_data->p_payload_start[ 9 + p_data->p_payload_start[8] ] << 8;
518     }
519     return( i_id );
520 }
521
522 /*****************************************************************************
523  * DecodePSM: Decode the Program Stream Map information
524  *****************************************************************************
525  * FIXME : loads are not aligned in this function
526  *****************************************************************************/
527 static void DecodePSM( input_thread_t * p_input, data_packet_t * p_data )
528 {
529     stream_ps_data_t *  p_demux =
530                  (stream_ps_data_t *)p_input->stream.p_demux_data;
531     byte_t *            p_byte;
532     byte_t *            p_end;
533     int                 i;
534     int                 i_new_es_number = 0;
535
536     if( p_data->p_payload_start + 10 > p_data->p_payload_end )
537     {
538         intf_ErrMsg( "input error: PSM too short : packet corrupt" );
539         return;
540     }
541
542     if( p_demux->b_has_PSM
543         && p_demux->i_PSM_version == (p_data->p_payload_start[6] & 0x1F) )
544     {
545         /* Already got that one. */
546         return;
547     }
548
549     intf_DbgMsg( "input: building PSM" );
550     p_demux->b_has_PSM = 1;
551     p_demux->i_PSM_version = p_data->p_payload_start[6] & 0x1F;
552
553     /* Go to elementary_stream_map_length, jumping over
554      * program_stream_info. */
555     p_byte = p_data->p_payload_start + 10
556               + U16_AT(&p_data->p_payload_start[8]);
557     if( p_byte > p_data->p_payload_end )
558     {
559         intf_ErrMsg( "input error: PSM too short, packet corrupt" );
560         return;
561     }
562     /* This is the full size of the elementary_stream_map.
563      * 2 == elementary_stream_map_length
564      * Please note that CRC_32 is not included in the length. */
565     p_end = p_byte + 2 + U16_AT(p_byte);
566     p_byte += 2;
567     if( p_end > p_data->p_payload_end )
568     {
569         intf_ErrMsg( "input error: PSM too short, packet corrupt" );
570         return;
571     }
572
573     vlc_mutex_lock( &p_input->stream.stream_lock );
574
575     /* 4 == minimum useful size of a section */
576     while( p_byte + 4 <= p_end )
577     {
578         es_descriptor_t *   p_es = NULL;
579         u8                  i_stream_id = p_byte[1];
580         /* FIXME: there will be a problem with private streams... (same
581          * stream_id) */
582
583         /* Look for the ES in the ES table */
584         for( i = i_new_es_number;
585              i < p_input->stream.pp_programs[0]->i_es_number;
586              i++ )
587         {
588             if( p_input->stream.pp_programs[0]->pp_es[i]->i_stream_id
589                     == i_stream_id )
590             {
591                 p_es = p_input->stream.pp_programs[0]->pp_es[i];
592                 if( p_es->i_type != p_byte[0] )
593                 {
594                     input_DelES( p_input, p_es );
595                     p_es = NULL;
596                 }
597                 else
598                 {
599                     /* Move the ES to the beginning. */
600                     p_input->stream.pp_programs[0]->pp_es[i]
601                         = p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ];
602                     p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ]
603                         = p_es;
604                     i_new_es_number++;
605                 }
606                 break;
607             }
608         }
609
610         /* The goal is to have all the ES we have just read in the
611          * beginning of the pp_es table, and all the others at the end,
612          * so that we can close them more easily at the end. */
613         if( p_es == NULL )
614         {
615             p_es = input_AddES( p_input, p_input->stream.pp_programs[0],
616                                 i_stream_id, 0 );
617             p_es->i_type = p_byte[0];
618             p_es->b_audio = ( p_es->i_type == MPEG1_AUDIO_ES
619                               || p_es->i_type == MPEG2_AUDIO_ES
620                               || p_es->i_type == AC3_AUDIO_ES
621                               || p_es->i_type == LPCM_AUDIO_ES
622                             );
623
624             /* input_AddES has inserted the new element at the end. */
625             p_input->stream.pp_programs[0]->pp_es[
626                 p_input->stream.pp_programs[0]->i_es_number ]
627                 = p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ];
628             p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ] = p_es;
629             i_new_es_number++;
630         }
631         p_byte += 4 + U16_AT(&p_byte[2]);
632     }
633
634     /* Un-select the streams that are no longer parts of the program. */
635     for( i = i_new_es_number;
636          i < p_input->stream.pp_programs[0]->i_es_number;
637          i++ )
638     {
639         /* We remove pp_es[i_new_es_member] and not pp_es[i] because the
640          * list will be emptied starting from the end */
641         input_DelES( p_input,
642                      p_input->stream.pp_programs[0]->pp_es[i_new_es_number] );
643     }
644
645     if( p_main->b_stats )
646     {
647         intf_StatMsg( "input info: The stream map after the PSM is now :" );
648         input_DumpStream( p_input );
649     }
650
651     vlc_mutex_unlock( &p_input->stream.stream_lock );
652 }
653
654 /*****************************************************************************
655  * input_ParsePS: read the PS header
656  *****************************************************************************/
657 es_descriptor_t * input_ParsePS( input_thread_t * p_input,
658                                  data_packet_t * p_data )
659 {
660     u32                 i_code;
661     es_descriptor_t *   p_es = NULL;
662
663     i_code = p_data->p_payload_start[3];
664
665     if( i_code > 0xBC ) /* ES start code */
666     {
667         u16                 i_id;
668         int                 i_dummy;
669
670         /* This is a PES packet. Find out if we want it or not. */
671         i_id = GetID( p_data );
672
673         vlc_mutex_lock( &p_input->stream.stream_lock );
674         if( p_input->stream.pp_programs[0]->b_is_ok )
675         {
676             /* Look only at the selected ES. */
677             for( i_dummy = 0; i_dummy < p_input->stream.i_selected_es_number;
678                  i_dummy++ )
679             {
680                 if( p_input->stream.pp_selected_es[i_dummy] != NULL
681                     && p_input->stream.pp_selected_es[i_dummy]->i_id == i_id )
682                 {
683                     p_es = p_input->stream.pp_selected_es[i_dummy];
684                     break;
685                 }
686             }
687         }
688         else
689         {
690             stream_ps_data_t * p_demux =
691               (stream_ps_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
692
693             /* Search all ES ; if not found -> AddES */
694             p_es = input_FindES( p_input, i_id );
695
696             if( p_es == NULL && !p_demux->b_has_PSM )
697             {
698                 p_es = input_AddES( p_input, p_input->stream.pp_programs[0],
699                                     i_id, 0 );
700                 if( p_es != NULL )
701                 {
702                     p_es->i_stream_id = p_data->p_payload_start[3];
703
704                     /* Set stream type and auto-spawn. */
705                     if( (i_id & 0xF0) == 0xE0 )
706                     {
707                         /* MPEG video */
708                         p_es->i_type = MPEG2_VIDEO_ES;
709                         p_es->i_cat = VIDEO_ES;
710 #ifdef AUTO_SPAWN
711                         if( !p_input->stream.b_seekable )
712                             input_SelectES( p_input, p_es );
713 #endif
714                     }
715                     else if( (i_id & 0xE0) == 0xC0 )
716                     {
717                         /* MPEG audio */
718                         p_es->i_type = MPEG2_AUDIO_ES;
719                         p_es->b_audio = 1;
720                         p_es->i_cat = AUDIO_ES;
721 #ifdef AUTO_SPAWN
722                         if( !p_input->stream.b_seekable )
723                         if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
724                                 == (p_es->i_id & 0x1F) )
725                         switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
726                         {
727                         case 0:
728                             main_PutIntVariable( INPUT_CHANNEL_VAR,
729                                                  REQUESTED_MPEG );
730                         case REQUESTED_MPEG:
731                             input_SelectES( p_input, p_es );
732                         }
733 #endif
734                     }
735                     else if( (i_id & 0xF0FF) == 0x80BD )
736                     {
737                         /* AC3 audio (0x80->0x8F) */
738                         p_es->i_type = AC3_AUDIO_ES;
739                         p_es->b_audio = 1;
740                         p_es->i_cat = AUDIO_ES;
741 #ifdef AUTO_SPAWN
742                         if( !p_input->stream.b_seekable )
743                         if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
744                                 == ((p_es->i_id & 0xF00) >> 8) )
745                         switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
746                         {
747                         case 0:
748                             main_PutIntVariable( INPUT_CHANNEL_VAR,
749                                                  REQUESTED_AC3 );
750                         case REQUESTED_AC3:
751                             input_SelectES( p_input, p_es );
752                         }
753 #endif
754                     }
755                     else if( (i_id & 0xE0FF) == 0x20BD )
756                     {
757                         /* Subtitles video (0x20->0x3F) */
758                         p_es->i_type = DVD_SPU_ES;
759                         p_es->i_cat = SPU_ES;
760 #ifdef AUTO_SPAWN
761                         if( main_GetIntVariable( INPUT_SUBTITLE_VAR, -1 )
762                                 == ((p_es->i_id & 0x1F00) >> 8) )
763                         {
764                             if( !p_input->stream.b_seekable )
765                                 input_SelectES( p_input, p_es );
766                         }
767 #endif
768                     }
769                     else if( (i_id & 0xF0FF) == 0xA0BD )
770                     {
771                         /* LPCM audio (0xA0->0xAF) */
772                         p_es->i_type = LPCM_AUDIO_ES;
773                         p_es->b_audio = 1;
774                         p_es->i_cat = AUDIO_ES;
775                         /* FIXME : write the decoder */
776                     }
777                     else
778                     {
779                         p_es->i_type = UNKNOWN_ES;
780                     }
781                 }
782
783                 /* Tell the interface the stream has changed */
784                 p_input->stream.b_changed = 1;
785             }
786         } /* stream.b_is_ok */
787         vlc_mutex_unlock( &p_input->stream.stream_lock );
788     } /* i_code > 0xBC */
789
790     return( p_es );
791 }
792
793 /*****************************************************************************
794  * input_DemuxPS: first step of demultiplexing: the PS header
795  *****************************************************************************/
796 void input_DemuxPS( input_thread_t * p_input, data_packet_t * p_data )
797 {
798     u32                 i_code;
799     boolean_t           b_trash = 0;
800     es_descriptor_t *   p_es = NULL;
801
802     i_code = ((u32)p_data->p_payload_start[0] << 24)
803                 | ((u32)p_data->p_payload_start[1] << 16)
804                 | ((u32)p_data->p_payload_start[2] << 8)
805                 | p_data->p_payload_start[3];
806     if( i_code <= 0x1BC )
807     {
808         switch( i_code )
809         {
810         case 0x1BA: /* PACK_START_CODE */
811             {
812                 /* Read the SCR. */
813                 mtime_t         scr_time;
814                 u32             i_mux_rate;
815
816                 if( (p_data->p_payload_start[4] & 0xC0) == 0x40 )
817                 {
818                     /* MPEG-2 */
819                     byte_t      p_header[14];
820                     byte_t *    p_byte;
821                     p_byte = p_data->p_payload_start;
822
823                     if( MoveChunk( p_header, &p_data, &p_byte, 14 ) != 14 )
824                     {
825                         intf_WarnMsg( 1, "input: packet too short "
826                                          "to have a header" );
827                         b_trash = 1;
828                         break;
829                     }
830                     scr_time =
831                          ((mtime_t)(p_header[4] & 0x38) << 27) |
832                          ((mtime_t)(U32_AT(p_header + 4) & 0x03FFF800)
833                                         << 4) |
834                          ((( ((mtime_t)U16_AT(p_header + 6) << 16)
835                             | (mtime_t)U16_AT(p_header + 8) ) & 0x03FFF800)
836                                         >> 11);
837
838                     /* mux_rate */
839                     i_mux_rate = ((u32)U16_AT(p_header + 10) << 6)
840                                    | (p_header[12] >> 2);
841                     /* FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
842                      * This is the biggest kludge ever !
843                      * I don't know what's wrong with mux_rate calculation
844                      * but this heuristic work well : */
845                     i_mux_rate <<= 1;
846                     i_mux_rate /= 3;
847                 }
848                 else
849                 {
850                     /* MPEG-1 SCR is like PTS. */
851                     byte_t      p_header[12];
852                     byte_t *    p_byte;
853                     p_byte = p_data->p_payload_start;
854
855                     if( MoveChunk( p_header, &p_data, &p_byte, 12 ) != 12 )
856                     {
857                         intf_WarnMsg( 1, "input: packet too short "
858                                          "to have a header" );
859                         b_trash = 1;
860                         break;
861                     }
862                     scr_time =
863                          ((mtime_t)(p_header[4] & 0x0E) << 29) |
864                          (((mtime_t)U32_AT(p_header + 4) & 0xFFFE00) << 6) |
865                          ((mtime_t)p_header[7] << 7) |
866                          ((mtime_t)p_header[8] >> 1);
867
868                     /* mux_rate */
869                     i_mux_rate = (U32_AT(p_header + 8) & 0x7FFFFE) >> 1;
870                 }
871                 /* Call the pace control. */
872                 input_ClockManageRef( p_input, p_input->stream.pp_programs[0],
873                                       scr_time );
874
875                 if( i_mux_rate != p_input->stream.i_mux_rate
876                      && p_input->stream.i_mux_rate )
877                 {
878                     intf_WarnMsg( 2, "input: mux_rate changed, "
879                                      "expect cosmetic errors" );
880                 }
881                 p_input->stream.i_mux_rate = i_mux_rate;
882
883                 b_trash = 1;
884             }
885             break;
886
887         case 0x1BB: /* SYSTEM_START_CODE */
888             b_trash = 1;                              /* Nothing interesting */
889             break;
890
891         case 0x1BC: /* PROGRAM_STREAM_MAP_CODE */
892             DecodePSM( p_input, p_data );
893             b_trash = 1;
894             break;
895     
896         case 0x1B9: /* PROGRAM_END_CODE */
897             b_trash = 1;
898             break;
899    
900         default:
901             /* This should not happen */
902             b_trash = 1;
903             intf_WarnMsg( 3, "input: unwanted packet received "
904                              "with start code 0x%.8x", i_code );
905         }
906     }
907     else
908     {
909         p_es = input_ParsePS( p_input, p_data );
910
911         vlc_mutex_lock( &p_input->stream.control.control_lock );
912         if( p_es != NULL && p_es->p_decoder_fifo != NULL
913              && (!p_es->b_audio || !p_input->stream.control.b_mute) )
914         {
915             vlc_mutex_unlock( &p_input->stream.control.control_lock );
916             p_es->c_packets++;
917             input_GatherPES( p_input, p_data, p_es, 1, 0 );
918         }
919         else
920         {
921             vlc_mutex_unlock( &p_input->stream.control.control_lock );
922             b_trash = 1;
923         }
924     }
925
926     /* Trash the packet if it has no payload or if it isn't selected */
927     if( b_trash )
928     {
929         p_input->pf_delete_packet( p_input->p_method_data, p_data );
930         p_input->stream.c_packets_trashed++;
931     }
932 }
933
934  
935 /*
936  * TS Demultiplexing
937  */
938
939 /*****************************************************************************
940  * input_DemuxTS: first step of demultiplexing: the TS header
941  *****************************************************************************/
942 void input_DemuxTS( input_thread_t * p_input, data_packet_t * p_data )
943 {
944     u16                 i_pid;
945     int                 i_dummy;
946     boolean_t           b_adaptation;         /* Adaptation field is present */
947     boolean_t           b_payload;                 /* Packet carries payload */
948     boolean_t           b_unit_start;  /* A PSI or a PES start in the packet */
949     boolean_t           b_trash = 0;             /* Is the packet unuseful ? */
950     boolean_t           b_lost = 0;             /* Was there a packet loss ? */
951     boolean_t           b_psi = 0;                        /* Is this a PSI ? */
952     es_descriptor_t *   p_es = NULL;
953     es_ts_data_t *      p_es_demux = NULL;
954     pgrm_ts_data_t *    p_pgrm_demux = NULL;
955
956 #define p (p_data->p_buffer)
957     /* Extract flags values from TS common header. */
958     i_pid = ((p[1] & 0x1F) << 8) | p[2];
959     b_unit_start = (p[1] & 0x40);
960     b_adaptation = (p[3] & 0x20);
961     b_payload = (p[3] & 0x10);
962
963     /* Find out the elementary stream. */
964     vlc_mutex_lock( &p_input->stream.stream_lock );
965         
966     p_es= input_FindES( p_input, i_pid );
967     
968     if( (p_es != NULL) && (p_es->p_demux_data != NULL) )
969     {
970         p_es_demux = (es_ts_data_t *)p_es->p_demux_data;
971         
972         if( p_es_demux->b_psi )
973         {
974             b_psi = 1;
975         }
976         else
977         {
978             p_pgrm_demux = (pgrm_ts_data_t *)p_es->p_pgrm->p_demux_data; 
979         }
980     }
981
982     vlc_mutex_lock( &p_input->stream.control.control_lock );
983     if( ( p_es == NULL ) || (p_es->b_audio && p_input->stream.control.b_mute) )
984     {
985         /* Not selected. Just read the adaptation field for a PCR. */
986         b_trash = 1;
987     }
988     else if( p_es->p_decoder_fifo == NULL && !b_psi )
989     {
990         b_trash = 1; 
991     }
992
993     vlc_mutex_unlock( &p_input->stream.control.control_lock );
994     vlc_mutex_unlock( &p_input->stream.stream_lock );
995
996
997     /* Don't change the order of the tests : if b_psi then p_pgrm_demux 
998      * may still be null. Who said it was ugly ?
999      * I have written worse. --Meuuh */
1000     if( ( p_es != NULL ) && 
1001         ((p_es->p_decoder_fifo != NULL) || b_psi 
1002                                    || (p_pgrm_demux->i_pcr_pid == i_pid) ) )
1003     {
1004         p_es->c_packets++;
1005
1006         /* Extract adaptation field information if any */
1007
1008         if( !b_adaptation )
1009         {
1010             /* We don't have any adaptation_field, so payload starts
1011              * immediately after the 4 byte TS header */
1012             p_data->p_payload_start += 4;
1013         }
1014         else
1015         {
1016             /* p[4] is adaptation_field_length minus one */
1017             p_data->p_payload_start += 5 + p[4];
1018     
1019             /* The adaptation field can be limited to the
1020              * adaptation_field_length byte, so that there is nothing to do:
1021              * skip this possibility */
1022             if( p[4] )
1023             {
1024                 /* If the packet has both adaptation_field and payload,
1025                  * adaptation_field cannot be more than 182 bytes long; if
1026                  * there is only an adaptation_field, it must fill the next
1027                  * 183 bytes. */
1028                 if( b_payload ? (p[4] > 182) : (p[4] != 183) )
1029                 {
1030                     intf_WarnMsg( 2,
1031                         "input: invalid TS adaptation field (%p)",
1032                         p_data );
1033                     p_data->b_discard_payload = 1;
1034                     p_es->c_invalid_packets++;
1035                 }
1036     
1037                 /* Now we are sure that the byte containing flags is present:
1038                  * read it */
1039                 else
1040                 {
1041                     /* discontinuity_indicator */
1042                     if( p[5] & 0x80 )
1043                     {
1044                         intf_WarnMsg( 2,
1045                             "input: discontinuity_indicator"
1046                             " encountered by TS demux (position read: %d,"
1047                             " saved: %d)",
1048                             p[5] & 0x80, p_es_demux->i_continuity_counter );
1049     
1050                         /* If the PID carries the PCR, there will be a system
1051                          * time-based discontinuity. We let the PCR decoder
1052                          * handle that. */
1053                         p_es->p_pgrm->i_synchro_state = SYNCHRO_REINIT;
1054     
1055                         /* There also may be a continuity_counter
1056                          * discontinuity: resynchronize our counter with
1057                          * the one of the stream. */
1058                         p_es_demux->i_continuity_counter = (p[3] & 0x0f) - 1;
1059                     }
1060     
1061                     /* If this is a PCR_PID, and this TS packet contains a
1062                      * PCR, we pass it along to the PCR decoder. */
1063
1064                     if( !b_psi && (p_pgrm_demux->i_pcr_pid == i_pid)
1065                         && (p[5] & 0x10) )
1066                     {
1067                         /* There should be a PCR field in the packet, check
1068                          * if the adaptation field is long enough to carry
1069                          * it. */
1070                         if( p[4] >= 7 )
1071                         {
1072                             /* Read the PCR. */
1073                             mtime_t     pcr_time;
1074                             pcr_time = ( (mtime_t)p[6] << 25 ) |
1075                                        ( (mtime_t)p[7] << 17 ) |
1076                                        ( (mtime_t)p[8] << 9 ) |
1077                                        ( (mtime_t)p[9] << 1 ) |
1078                                        ( (mtime_t)p[10] >> 7 );
1079                             /* Call the pace control. */
1080                             input_ClockManageRef( p_input, p_es->p_pgrm,
1081                                                   pcr_time );
1082                         }
1083                     } /* PCR ? */
1084                 } /* valid TS adaptation field ? */
1085             } /* length > 0 */
1086         } /* has adaptation field */
1087         /* Check the continuity of the stream. */
1088         i_dummy = ((p[3] & 0x0f) - p_es_demux->i_continuity_counter) & 0x0f;
1089         if( i_dummy == 1 )
1090         {
1091             /* Everything is ok, just increase our counter */
1092             (p_es_demux->i_continuity_counter)++;
1093         }
1094         else
1095         {
1096             if( !b_payload && i_dummy == 0 )
1097             {
1098                 /* This is a packet without payload, this is allowed by the
1099                  * draft. As there is nothing interesting in this packet
1100                  * (except PCR that have already been handled), we can trash
1101                  * the packet. */
1102                 intf_WarnMsg( 3, "input: packet without payload received "
1103                                  "by TS demux" );
1104                 b_trash = 1;
1105             }
1106             else if( i_dummy <= 0 )
1107             {
1108                 /* Duplicate packet: mark it as being to be trashed. */
1109                 intf_WarnMsg( 3, "input: duplicate packet received "
1110                                  "by TS demux" );
1111                 b_trash = 1;
1112             }
1113             else if( p_es_demux->i_continuity_counter == 0xFF )
1114             {
1115                 /* This means that the packet is the first one we receive for
1116                  * this ES since the continuity counter ranges between 0 and
1117                  * 0x0F excepts when it has been initialized by the input:
1118                  * init the counter to the correct value. */
1119                 intf_WarnMsg( 3, "input: first packet for PID %d received "
1120                              "by TS demux", p_es->i_id );
1121                 p_es_demux->i_continuity_counter = (p[3] & 0x0f);
1122             }
1123             else
1124             {
1125                 /* This can indicate that we missed a packet or that the
1126                  * continuity_counter wrapped and we received a dup packet:
1127                  * as we don't know, do as if we missed a packet to be sure
1128                  * to recover from this situation */
1129                 intf_WarnMsg( 2, "input: packet lost by TS demux: "
1130                                  "current %d, packet %d",
1131                               p_es_demux->i_continuity_counter & 0x0f,
1132                               p[3] & 0x0f );
1133                 b_lost = 1;
1134                 p_es_demux->i_continuity_counter = p[3] & 0x0f;
1135             } /* not continuous */
1136         } /* continuity */
1137     } /* if selected or PCR */
1138     
1139     /* Trash the packet if it has no payload or if it isn't selected */
1140     if( b_trash )
1141     {
1142         p_input->pf_delete_packet( p_input->p_method_data, p_data );
1143         p_input->stream.c_packets_trashed++;
1144     }
1145     else
1146     {
1147         if( b_psi )
1148         {
1149             /* The payload contains PSI tables */
1150             input_DemuxPSI( p_input, p_data, p_es,
1151                             b_unit_start, b_lost );
1152
1153         }
1154         else
1155         {
1156             /* The payload carries a PES stream */
1157             input_GatherPES( p_input, p_data, p_es, b_unit_start, b_lost ); 
1158         }
1159
1160     }
1161
1162 #undef p
1163
1164 }
1165
1166 /*
1167  * PSI demultiplexing and decoding
1168  */
1169
1170 /*****************************************************************************
1171  * DemuxPSI : makes up complete PSI data
1172  *****************************************************************************/
1173 void input_DemuxPSI( input_thread_t * p_input, data_packet_t * p_data, 
1174         es_descriptor_t * p_es, boolean_t b_unit_start, boolean_t b_lost )
1175 {
1176     es_ts_data_t  * p_demux_data;
1177     
1178     p_demux_data = (es_ts_data_t *)p_es->p_demux_data;
1179
1180 #define p_psi (p_demux_data->p_psi_section)
1181 #define p (p_data->p_payload_start)
1182
1183     if( b_unit_start )
1184     {
1185         /* unit_start set to 1 -> presence of a pointer field
1186          * (see ISO/IEC 13818 (2.4.4.2) which should be set to 0x00 */
1187         if( (u8)p[0] != 0x00 )
1188         {
1189             intf_WarnMsg( 2, "input: non zero pointer field found, "
1190                              "trying to continue" );
1191             p+=(u8)p[0];
1192         }
1193         else
1194         {
1195             p++;
1196         }
1197
1198         /* This is the begining of a new section */
1199
1200         if( ((u8)(p[1]) & 0xc0) != 0x80 ) 
1201         {
1202             intf_WarnMsg( 2, "input: invalid PSI packet" );
1203             p_psi->b_trash = 1;
1204         }
1205         else 
1206         {
1207             p_psi->i_section_length = ((p[1] & 0xF) << 8) | p[2];
1208             p_psi->b_section_complete = 0;
1209             p_psi->i_read_in_section = 0;
1210             p_psi->i_section_number = (u8)p[6];
1211
1212             if( p_psi->b_is_complete || p_psi->i_section_number == 0 )
1213             {
1214                 /* This is a new PSI packet */
1215                 p_psi->b_is_complete = 0;
1216                 p_psi->b_trash = 0;
1217                 p_psi->i_version_number = ( p[5] >> 1 ) & 0x1f;
1218                 p_psi->i_last_section_number = (u8)p[7];
1219
1220                 /* We'll write at the begining of the buffer */
1221                 p_psi->p_current = p_psi->buffer;
1222             }
1223             else
1224             {
1225                 if( p_psi->b_section_complete )
1226                 {
1227                     /* New Section of an already started PSI */
1228                     p_psi->b_section_complete = 0;
1229                     
1230                     if( p_psi->i_version_number != (( p[5] >> 1 ) & 0x1f) )
1231                     {
1232                         intf_WarnMsg( 2, "input: PSI version differs "
1233                                          "inside same PAT" );
1234                         p_psi->b_trash = 1;
1235                     }
1236                     if( p_psi->i_section_number + 1 != (u8)p[6] )
1237                     {
1238                         intf_WarnMsg( 2, "input: PSI Section discontinuity, "
1239                                          "packet lost ?" );
1240                         p_psi->b_trash = 1;
1241                     }
1242                     else
1243                         p_psi->i_section_number++;
1244                 }
1245                 else
1246                 {
1247                     intf_WarnMsg( 2, "input: got unexpected new PSI section" );
1248                     p_psi->b_trash = 1;
1249                 }
1250             }
1251         }
1252     } /* b_unit_start */
1253     
1254     if( !p_psi->b_trash )
1255     {
1256         /* read */
1257         if( (p_data->p_payload_end - p) >=
1258             ( p_psi->i_section_length - p_psi->i_read_in_section ) )
1259         {
1260             /* The end of the section is in this TS packet */
1261             memcpy( p_psi->p_current, p, 
1262             (p_psi->i_section_length - p_psi->i_read_in_section) );
1263     
1264             p_psi->b_section_complete = 1;
1265             p_psi->p_current += 
1266                 (p_psi->i_section_length - p_psi->i_read_in_section);
1267                         
1268             if( p_psi->i_section_number == p_psi->i_last_section_number )
1269             {
1270                 /* This was the last section of PSI */
1271                 p_psi->b_is_complete = 1;
1272
1273                 switch( p_demux_data->i_psi_type)
1274                 {
1275                 case PSI_IS_PAT:
1276                     input_DecodePAT( p_input, p_es );
1277                     break;
1278                 case PSI_IS_PMT:
1279                     input_DecodePMT( p_input, p_es );
1280                     break;
1281                 default:
1282                     intf_WarnMsg(2, "Received unknown PSI in DemuxPSI");
1283                 }
1284             }
1285         }
1286         else
1287         {
1288             memcpy( p_psi->buffer, p, p_data->p_payload_end - p );
1289             p_psi->i_read_in_section += p_data->p_payload_end - p;
1290
1291             p_psi->p_current += p_data->p_payload_end - p;
1292         }
1293     }
1294
1295 #undef p_psi    
1296 #undef p
1297    
1298     p_input->pf_delete_packet( p_input->p_method_data, p_data );
1299     
1300     return ;
1301 }
1302
1303 /*****************************************************************************
1304  * DecodePAT : Decodes Programm association table and deal with it
1305  *****************************************************************************/
1306 static void input_DecodePAT( input_thread_t * p_input, es_descriptor_t * p_es )
1307 {
1308     
1309     stream_ts_data_t  * p_stream_data;
1310     es_ts_data_t      * p_demux_data;
1311
1312     p_demux_data = (es_ts_data_t *)p_es->p_demux_data;
1313     p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
1314     
1315 #define p_psi (p_demux_data->p_psi_section)
1316
1317     if( p_stream_data->i_pat_version != p_psi->i_version_number )
1318     {
1319         /* PAT has changed. We are going to delete all programms and 
1320          * create new ones. We chose not to only change what was needed
1321          * as a PAT change may mean the stream is radically changing and
1322          * this is a secure method to avoid krashes */
1323         pgrm_descriptor_t * p_pgrm;
1324         es_descriptor_t   * p_current_es;
1325         es_ts_data_t      * p_es_demux;
1326         pgrm_ts_data_t    * p_pgrm_demux;
1327         byte_t            * p_current_data;           
1328         
1329         int                 i_section_length,i_program_id,i_pmt_pid;
1330         int                 i_loop, i_current_section;
1331         
1332         p_current_data = p_psi->buffer;
1333
1334
1335         for( i_loop = 0; i_loop < p_input->stream.i_pgrm_number; i_loop++ )
1336         {
1337             input_DelProgram( p_input, p_input->stream.pp_programs[i_loop] );
1338         }
1339         
1340         do
1341         {
1342             i_section_length = (((u32)p_current_data[1] & 0xF) << 8) |
1343                                  p_current_data[2];
1344             i_current_section = (u8)p_current_data[6];
1345     
1346             for( i_loop = 0; i_loop < (i_section_length - 9) / 4 ; i_loop++ )
1347             {
1348                 i_program_id = ( (u32)*(p_current_data + i_loop * 4 + 8) << 8 ) |
1349                                  *(p_current_data + i_loop * 4 + 9);
1350                 i_pmt_pid = ( ((u32)*( p_current_data + i_loop * 4 + 10) & 0x1F)
1351                                     << 8 ) |
1352                                *( p_current_data + i_loop * 4 + 11);
1353     
1354                 /* If program = 0, we're having info about NIT not PMT */
1355                 if( i_program_id )
1356                 {
1357                     /* Add this program */
1358                     p_pgrm = input_AddProgram( p_input, i_program_id, 
1359                                                sizeof( pgrm_ts_data_t ) );
1360                    
1361                     /* whatis the PID of the PMT of this program */
1362                     p_pgrm_demux = (pgrm_ts_data_t *)p_pgrm->p_demux_data;
1363                     p_pgrm_demux->i_pmt_version = PMT_UNINITIALIZED;
1364     
1365                     /* Add the PMT ES to this program */
1366                     p_current_es = input_AddES( p_input, p_pgrm,(u16)i_pmt_pid,
1367                                         sizeof( es_ts_data_t) );
1368                     p_es_demux = (es_ts_data_t *)p_current_es->p_demux_data;
1369                     p_es_demux->b_psi = 1;
1370                     p_es_demux->i_psi_type = PSI_IS_PMT;
1371                     
1372                     p_es_demux->p_psi_section = 
1373                                             malloc( sizeof( psi_section_t ) );
1374                     p_es_demux->p_psi_section->b_is_complete = 0;
1375                 }
1376             }
1377             
1378             p_current_data += 3 + i_section_length;
1379             
1380         } while( i_current_section < p_psi->i_last_section_number );
1381         
1382         /* Go to the beginning of the next section*/
1383         p_stream_data->i_pat_version = p_psi->i_version_number;
1384
1385     }
1386 #undef p_psi    
1387     /* FIXME This has nothing to do here */
1388     p_input->stream.p_selected_program = p_input->stream.pp_programs[0] ;
1389
1390 }
1391
1392 /*****************************************************************************
1393  * DecodePMT : decode a given Program Stream Map
1394  * ***************************************************************************
1395  * When the PMT changes, it may mean a deep change in the stream, and it is
1396  * careful to delete the ES and add them again. If the PMT doesn't change,
1397  * there no need to do anything.
1398  *****************************************************************************/
1399 static void input_DecodePMT( input_thread_t * p_input, es_descriptor_t * p_es )
1400 {
1401
1402     pgrm_ts_data_t            * p_pgrm_data;
1403     es_ts_data_t              * p_demux_data;
1404
1405     p_demux_data = (es_ts_data_t *)p_es->p_demux_data;
1406     p_pgrm_data = (pgrm_ts_data_t *)p_es->p_pgrm->p_demux_data;
1407     
1408 #define p_psi (p_demux_data->p_psi_section)
1409
1410     if( p_psi->i_version_number != p_pgrm_data->i_pmt_version ) 
1411     {
1412         es_descriptor_t   * p_new_es;  
1413         es_ts_data_t      * p_es_demux;
1414         byte_t            * p_current_data, * p_current_section;
1415         int                 i_section_length,i_current_section;
1416         int                 i_prog_info_length, i_loop;
1417         int                 i_es_info_length, i_pid, i_stream_type;
1418         int                 i_audio_es, i_spu_es;
1419         int                 i_required_audio_es, i_required_spu_es;
1420         
1421         p_current_section = p_psi->buffer;
1422         p_current_data = p_psi->buffer;
1423
1424         p_pgrm_data->i_pcr_pid = ( ((u32)*(p_current_section + 8) & 0x1F) << 8 ) |
1425                                     *(p_current_section + 9);
1426
1427         i_audio_es = 0;
1428         i_spu_es = 0;
1429
1430         /* Lock stream information */
1431         vlc_mutex_lock( &p_input->stream.stream_lock );
1432
1433         /* Get the number of the required audio stream */
1434         if( p_main->b_audio )
1435         {
1436             /* Default is the first one */
1437             i_required_audio_es = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
1438             if( i_required_audio_es < 0 )
1439             {
1440                 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
1441                 i_required_audio_es = 1;
1442             }
1443         }
1444         else
1445         {
1446             i_required_audio_es = 0;
1447         }
1448
1449         /* Same thing for subtitles */
1450         if( p_main->b_video )
1451         {
1452             /* for spu, default is none */
1453             i_required_spu_es = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
1454             if( i_required_spu_es < 0 )
1455             {
1456                 main_PutIntVariable( INPUT_SUBTITLE_VAR, 0 );
1457                 i_required_spu_es = 0;
1458             }
1459         }
1460         else
1461         {
1462             i_required_spu_es = 0;
1463         }
1464         
1465         /* Delete all ES in this program  except the PSI */
1466         for( i_loop=0; i_loop < p_es->p_pgrm->i_es_number; i_loop++ )
1467         {
1468             p_es_demux = (es_ts_data_t *)
1469                          p_es->p_pgrm->pp_es[i_loop]->p_demux_data;
1470             if ( ! p_es_demux->b_psi )
1471             input_DelES( p_input, p_es->p_pgrm->pp_es[i_loop] );
1472         }
1473
1474         /* Then add what we received in this PMT */
1475         do
1476         {
1477             i_section_length = ( ((u32)*(p_current_data + 1) & 0xF) << 8 ) |
1478                                   *(p_current_data + 2);
1479             i_current_section = (u8)p_current_data[6];
1480             i_prog_info_length = ( ((u32)*(p_current_data + 10) & 0xF) << 8 ) |
1481                                     *(p_current_data + 11);
1482
1483             /* For the moment we ignore program descriptors */
1484             p_current_data += 12 + i_prog_info_length;
1485     
1486             /* The end of the section, before the CRC is at 
1487              * p_current_section + i_section_length -1 */
1488             while( p_current_data < p_current_section + i_section_length -1 )
1489             {
1490                 i_stream_type = (int)p_current_data[0];
1491                 i_pid = ( ((u32)*(p_current_data + 1) & 0x1F) << 8 ) |
1492                            *(p_current_data + 2);
1493                 i_es_info_length = ( ((u32)*(p_current_data + 3) & 0xF) << 8 ) |
1494                                       *(p_current_data + 4);
1495                 
1496                 /* Add this ES to the program */
1497                 p_new_es = input_AddES( p_input, p_es->p_pgrm, 
1498                                         (u16)i_pid, sizeof( es_ts_data_t ) );
1499
1500                 /* Tell the decoders what kind of stream it is */
1501                 p_new_es->i_type = i_stream_type;
1502
1503                 /* Tell the interface what kind of stream it is and select 
1504                  * the required ones */
1505                 switch( i_stream_type )
1506                 {
1507                     case MPEG1_VIDEO_ES:
1508                     case MPEG2_VIDEO_ES:
1509                         p_new_es->i_cat = VIDEO_ES;
1510                         input_SelectES( p_input, p_new_es );
1511                         break;
1512                     case MPEG1_AUDIO_ES:
1513                     case MPEG2_AUDIO_ES:
1514                         p_new_es->i_cat = AUDIO_ES;
1515                         i_audio_es += 1;
1516                         if( i_audio_es == i_required_audio_es )
1517                             input_SelectES( p_input, p_new_es );
1518                         break;
1519                     case LPCM_AUDIO_ES :
1520                     case AC3_AUDIO_ES :
1521                         p_new_es->i_stream_id = 0xBD;
1522                         p_new_es->i_cat = AUDIO_ES;
1523                         i_audio_es += 1;
1524                         if( i_audio_es == i_required_audio_es )
1525                             input_SelectES( p_input, p_new_es );
1526                         break;
1527                     /* Not sure this one is fully specification-compliant */
1528                     case DVD_SPU_ES :
1529                         p_new_es->i_stream_id = 0xBD;
1530                         p_new_es->i_cat = SPU_ES;
1531                         i_spu_es += 1;
1532                         if( i_spu_es == i_required_spu_es )
1533                             input_SelectES( p_input, p_new_es );
1534                         break;
1535                     default :
1536                         p_new_es->i_cat = UNKNOWN_ES;
1537                         break;
1538                 }
1539                 
1540                 p_current_data += 5 + i_es_info_length;
1541             }
1542
1543             /* Go to the beginning of the next section*/
1544             p_current_data += 3 + i_section_length;
1545            
1546             p_current_section++;
1547             
1548         } while( i_current_section < p_psi->i_last_section_number );
1549
1550         if( i_required_audio_es > i_audio_es )
1551         {
1552             intf_WarnMsg( 2, "input: non-existing audio ES required" );
1553         }
1554         
1555         if( i_required_spu_es > i_spu_es )
1556         {
1557             intf_WarnMsg( 2, "input: non-existing subtitles ES required" );
1558         }
1559         
1560         p_pgrm_data->i_pmt_version = p_psi->i_version_number;
1561
1562         /* inform interface that stream has changed */
1563         p_input->stream.b_changed = 1;
1564         /*  Remove lock */
1565         vlc_mutex_unlock( &p_input->stream.stream_lock );
1566     }
1567     
1568 #undef p_psi
1569 }
1570