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