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