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