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