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