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