1 /*****************************************************************************
2 * mpeg_system.c: TS, PS and PES management
3 *****************************************************************************
4 * Copyright (C) 1998-2001 VideoLAN
5 * $Id: mpeg_system.c,v 1.83 2002/03/05 17:46:33 stef Exp $
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>
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.
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.
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 *****************************************************************************/
28 /*****************************************************************************
30 *****************************************************************************/
32 #include <string.h> /* memcpy(), memset() */
33 #include <sys/types.h> /* off_t */
35 #include <videolan/vlc.h>
37 #include "stream_control.h"
38 #include "input_ext-intf.h"
39 #include "input_ext-dec.h"
40 #include "input_ext-plugins.h"
42 /*****************************************************************************
44 *****************************************************************************/
46 static void input_DecodePAT( input_thread_t *, es_descriptor_t *);
47 static void input_DecodePMT( input_thread_t *, es_descriptor_t *);
50 * PES Packet management
53 /*****************************************************************************
55 *****************************************************************************
56 * Small utility function used to parse discontinuous headers safely. Copies
57 * i_buf_len bytes of data to a buffer and returns the size copied.
58 * It also solves some alignment problems on non-IA-32, non-PPC processors.
59 * This is a variation on the theme of input_ext-dec.h:GetChunk().
60 *****************************************************************************/
61 static __inline__ size_t MoveChunk( byte_t * p_dest,
62 data_packet_t ** pp_data_src,
66 ptrdiff_t i_available;
68 if( (i_available = (*pp_data_src)->p_payload_end - *pp_src)
72 memcpy( p_dest, *pp_src, i_buf_len );
78 size_t i_init_len = i_buf_len;
83 memcpy( p_dest, *pp_src, i_available );
84 *pp_data_src = (*pp_data_src)->p_next;
85 i_buf_len -= i_available;
86 p_dest += i_available;
87 if( *pp_data_src == NULL )
90 return( i_init_len - i_buf_len );
92 *pp_src = (*pp_data_src)->p_payload_start;
94 while( (i_available = (*pp_data_src)->p_payload_end - *pp_src)
100 memcpy( p_dest, *pp_src, i_buf_len );
101 *pp_src += i_buf_len;
103 return( i_init_len );
107 /*****************************************************************************
109 *****************************************************************************
110 * Parse a finished PES packet and analyze its header.
111 *****************************************************************************/
112 #define PES_HEADER_SIZE 7
113 void input_ParsePES( input_thread_t * p_input, es_descriptor_t * p_es )
115 data_packet_t * p_data;
117 byte_t p_header[PES_HEADER_SIZE];
120 #define p_pes (p_es->p_pes)
122 /* Parse the header. The header has a variable length, but in order
123 * to improve the algorithm, we will read the 14 bytes we may be
125 p_data = p_pes->p_first;
126 p_byte = p_data->p_payload_start;
129 if( MoveChunk( p_header, &p_data, &p_byte, PES_HEADER_SIZE )
132 intf_WarnMsg( 1, "input: PES packet too short to have a header" );
133 input_DeletePES( p_input->p_method_data, p_pes );
138 /* Get the PES size if defined */
139 p_es->i_pes_real_size = U16_AT(p_header + 4);
140 if( p_es->i_pes_real_size )
142 p_es->i_pes_real_size += 6;
145 /* First read the 6 header bytes common to all PES packets:
146 * use them to test the PES validity */
147 if( (p_header[0] || p_header[1] || (p_header[2] != 1)) )
149 /* packet_start_code_prefix != 0x000001 */
150 intf_ErrMsg( "input error: data loss, "
151 "PES packet doesn't start with 0x000001" );
152 input_DeletePES( p_input->p_method_data, p_pes );
157 int i_pes_header_size, i_payload_size;
159 if ( p_es->i_pes_real_size &&
160 (p_es->i_pes_real_size != p_pes->i_pes_size) )
162 /* PES_packet_length is set and != total received payload */
163 /* Warn the decoder that the data may be corrupt. */
164 intf_WarnMsg( 1, "input: packet corrupted, "
165 "PES sizes do not match" );
168 switch( p_es->i_stream_id )
170 case 0xBC: /* Program stream map */
171 case 0xBE: /* Padding */
172 case 0xBF: /* Private stream 2 */
175 case 0xFF: /* Program stream directory */
176 case 0xF2: /* DSMCC stream */
177 case 0xF8: /* ITU-T H.222.1 type E stream */
178 /* The payload begins immediately after the 6 bytes header, so
179 * we have finished with the parsing */
180 i_pes_header_size = 6;
184 if( (p_header[6] & 0xC0) == 0x80 )
186 /* MPEG-2 : the PES header contains at least 3 more bytes. */
188 boolean_t b_has_pts, b_has_dts;
189 byte_t p_full_header[12];
191 p_pes->b_data_alignment = p_header[6] & 0x04;
193 i_max_len = MoveChunk( p_full_header, &p_data, &p_byte, 12 );
197 "PES packet too short to have a MPEG-2 header" );
198 input_DeletePES( p_input->p_method_data,
204 b_has_pts = p_full_header[0] & 0x80;
205 b_has_dts = p_full_header[0] & 0x40;
206 i_pes_header_size = p_full_header[1] + 9;
208 /* Now parse the optional header extensions */
214 "PES packet too short to have a MPEG-2 header" );
215 input_DeletePES( p_input->p_method_data,
220 p_pes->i_pts = input_ClockGetTS( p_input, p_es->p_pgrm,
221 ( ((mtime_t)(p_full_header[2] & 0x0E) << 29) |
222 ((mtime_t)(p_full_header[3]) << 22) |
223 ((mtime_t)(p_full_header[4] & 0xFE) << 14) |
224 ((mtime_t)p_full_header[5] << 7) |
225 ((mtime_t)p_full_header[6] >> 1) ) );
232 "PES packet too short to have a MPEG-2 header" );
233 input_DeletePES( p_input->p_method_data,
238 p_pes->i_dts = input_ClockGetTS( p_input, p_es->p_pgrm,
239 ( ((mtime_t)(p_full_header[7] & 0x0E) << 29) |
240 (((mtime_t)U16_AT(p_full_header + 8) << 14)
242 ((mtime_t)U16_AT(p_full_header + 10) >> 1) ) );
248 /* Probably MPEG-1 */
249 boolean_t b_has_pts, b_has_dts;
251 i_pes_header_size = 6;
252 p_data = p_pes->p_first;
253 p_byte = p_data->p_payload_start;
254 /* Cannot fail because the previous one succeeded. */
255 MoveChunk( NULL, &p_data, &p_byte, 6 );
257 while( *p_byte == 0xFF && i_pes_header_size < 23 )
260 if( MoveChunk( NULL, &p_data, &p_byte, 1 ) != 1 )
263 "PES packet too short to have a MPEG-1 header" );
264 input_DeletePES( p_input->p_method_data, p_pes );
269 if( i_pes_header_size == 23 )
271 intf_ErrMsg( "input error: too much MPEG-1 stuffing" );
272 input_DeletePES( p_input->p_method_data, p_pes );
277 if( (*p_byte & 0xC0) == 0x40 )
279 /* Don't ask why... --Meuuh */
280 /* Erm... why ? --Sam */
281 /* Well... According to the recommendation, it is for
282 * STD_buffer_scale and STD_buffer_size. --Meuuh */
283 i_pes_header_size += 2;
284 if( MoveChunk( NULL, &p_data, &p_byte, 2 ) != 2 )
286 intf_WarnMsg( 1, "input: PES packet too short "
287 "to have a MPEG-1 header" );
288 input_DeletePES( p_input->p_method_data, p_pes );
296 b_has_pts = *p_byte & 0x20;
297 b_has_dts = *p_byte & 0x10;
303 i_pes_header_size += 4;
304 if( MoveChunk( p_ts, &p_data, &p_byte, 5 ) != 5 )
306 intf_WarnMsg( 1, "input: PES packet too short "
307 "to have a MPEG-1 header" );
308 input_DeletePES( p_input->p_method_data, p_pes );
313 p_pes->i_pts = input_ClockGetTS( p_input, p_es->p_pgrm,
314 ( ((mtime_t)(p_ts[0] & 0x0E) << 29) |
315 (((mtime_t)U32_AT(p_ts) & 0xFFFE00) << 6) |
316 ((mtime_t)p_ts[3] << 7) |
317 ((mtime_t)p_ts[4] >> 1) ) );
321 i_pes_header_size += 5;
322 if( MoveChunk( p_ts, &p_data, &p_byte, 5 ) != 5 )
324 intf_WarnMsg( 1, "input: PES packet too short "
325 "to have a MPEG-1 header" );
326 input_DeletePES( p_input->p_method_data,
332 p_pes->i_dts = input_ClockGetTS( p_input,
334 ( ((mtime_t)(p_ts[0] & 0x0E) << 29) |
335 (((mtime_t)U32_AT(p_ts) & 0xFFFE00) << 6) |
336 ((mtime_t)p_ts[3] << 7) |
337 ((mtime_t)p_ts[4] >> 1) ) );
345 if( p_es->i_stream_id == 0xbd )
347 /* With private stream 1, the first byte of the payload
348 * is a stream_private_id, so skip it. */
352 /* Now we've parsed the header, we just have to indicate in some
353 * specific data packets where the PES payload begins (renumber
354 * p_payload_start), so that the decoders can find the beginning
355 * of their data right out of the box. */
356 p_data = p_pes->p_first;
357 i_payload_size = p_data->p_payload_end
358 - p_data->p_payload_start;
359 while( i_pes_header_size > i_payload_size )
361 /* These packets are entirely filled by the PES header. */
362 i_pes_header_size -= i_payload_size;
363 p_data->p_payload_start = p_data->p_payload_end;
364 /* Go to the next data packet. */
365 if( (p_data = p_data->p_next) == NULL )
367 intf_ErrMsg( "input error: PES header bigger than payload" );
368 input_DeletePES( p_input->p_method_data, p_pes );
372 i_payload_size = p_data->p_payload_end
373 - p_data->p_payload_start;
375 /* This last packet is partly header, partly payload. */
376 if( i_payload_size < i_pes_header_size )
378 intf_ErrMsg( "input error: PES header bigger than payload" );
379 input_DeletePES( p_input->p_method_data, p_pes );
383 p_data->p_payload_start += i_pes_header_size;
385 /* Now we can eventually put the PES packet in the decoder's
387 if( p_es->p_decoder_fifo != NULL )
389 input_DecodePES( p_es->p_decoder_fifo, p_pes );
393 intf_ErrMsg( "input error: no fifo to receive PES %p "
394 "(who wrote this damn code ?)", p_pes );
395 input_DeletePES( p_input->p_method_data, p_pes );
403 /*****************************************************************************
405 *****************************************************************************
406 * Gather a PES packet.
407 *****************************************************************************/
408 void input_GatherPES( input_thread_t * p_input, data_packet_t * p_data,
409 es_descriptor_t * p_es,
410 boolean_t b_unit_start, boolean_t b_packet_lost )
412 #define p_pes (p_es->p_pes)
414 /* If we lost data, insert a NULL data packet (philosophy : 0 is quite
415 * often an escape sequence in decoders, so that should make them wait
416 * for the next start code). */
419 input_NullPacket( p_input, p_es );
422 if( b_unit_start && p_pes != NULL )
424 /* If the data packet contains the begining of a new PES packet, and
425 * if we were reassembling a PES packet, then the PES should be
426 * complete now, so parse its header and give it to the decoders. */
427 input_ParsePES( p_input, p_es );
430 if( !b_unit_start && p_pes == NULL )
432 /* Random access... */
433 input_DeletePacket( p_input->p_method_data, p_data );
439 /* If we are at the beginning of a new PES packet, we must fetch
440 * a new PES buffer to begin with the reassembly of this PES
441 * packet. This is also here that we can synchronize with the
442 * stream if we lost packets or if the decoder has just
444 if( (p_pes = input_NewPES( p_input->p_method_data ) ) == NULL )
446 intf_ErrMsg( "input error: out of memory" );
447 p_input->b_error = 1;
450 p_pes->i_rate = p_input->stream.control.i_rate;
451 p_pes->p_first = p_data;
453 /* If the PES header fits in the first data packet, we can
454 * already set p_gather->i_pes_real_size. */
455 if( p_data->p_payload_end - p_data->p_payload_start
458 p_es->i_pes_real_size =
459 U16_AT(p_data->p_payload_start + 4) + 6;
464 p_es->i_pes_real_size = 0;
469 /* Update the relations between the data packets */
470 p_pes->p_last->p_next = p_data;
473 p_pes->p_last = p_data;
476 /* Size of the payload carried in the data packet */
477 p_pes->i_pes_size += (p_data->p_payload_end
478 - p_data->p_payload_start);
480 /* We can check if the packet is finished */
481 if( p_pes->i_pes_size == p_es->i_pes_real_size )
483 /* The packet is finished, parse it */
484 input_ParsePES( p_input, p_es );
495 /*****************************************************************************
496 * GetID: Get the ID of a stream
497 *****************************************************************************/
498 static u16 GetID( data_packet_t * p_data )
502 i_id = p_data->p_demux_start[3]; /* stream_id */
505 /* FIXME : this is not valid if the header is split in multiple
507 /* stream_private_id */
508 i_id |= p_data->p_demux_start[ 9 + p_data->p_demux_start[8] ] << 8;
513 /*****************************************************************************
514 * DecodePSM: Decode the Program Stream Map information
515 *****************************************************************************
516 * FIXME : loads are not aligned in this function
517 *****************************************************************************/
518 static void DecodePSM( input_thread_t * p_input, data_packet_t * p_data )
520 stream_ps_data_t * p_demux =
521 (stream_ps_data_t *)p_input->stream.p_demux_data;
525 int i_new_es_number = 0;
527 if( p_data->p_demux_start + 10 > p_data->p_payload_end )
529 intf_ErrMsg( "input error: PSM too short : packet corrupt" );
533 if( p_demux->b_has_PSM
534 && p_demux->i_PSM_version == (p_data->p_demux_start[6] & 0x1F) )
536 /* Already got that one. */
540 p_demux->b_has_PSM = 1;
541 p_demux->i_PSM_version = p_data->p_demux_start[6] & 0x1F;
543 /* Go to elementary_stream_map_length, jumping over
544 * program_stream_info. */
545 p_byte = p_data->p_demux_start + 10
546 + U16_AT(&p_data->p_demux_start[8]);
547 if( p_byte > p_data->p_payload_end )
549 intf_ErrMsg( "input error: PSM too short, packet corrupt" );
552 /* This is the full size of the elementary_stream_map.
553 * 2 == elementary_stream_map_length
554 * Please note that CRC_32 is not included in the length. */
555 p_end = p_byte + 2 + U16_AT(p_byte);
557 if( p_end > p_data->p_payload_end )
559 intf_ErrMsg( "input error: PSM too short, packet corrupt" );
563 vlc_mutex_lock( &p_input->stream.stream_lock );
565 /* 4 == minimum useful size of a section */
566 while( p_byte + 4 <= p_end )
568 es_descriptor_t * p_es = NULL;
569 u8 i_stream_id = p_byte[1];
570 /* FIXME: there will be a problem with private streams... (same
573 /* Look for the ES in the ES table */
574 for( i = i_new_es_number;
575 i < p_input->stream.pp_programs[0]->i_es_number;
578 if( p_input->stream.pp_programs[0]->pp_es[i]->i_stream_id
581 p_es = p_input->stream.pp_programs[0]->pp_es[i];
582 if( p_es->i_type != p_byte[0] )
584 input_DelES( p_input, p_es );
589 /* Move the ES to the beginning. */
590 p_input->stream.pp_programs[0]->pp_es[i]
591 = p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ];
592 p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ]
600 /* The goal is to have all the ES we have just read in the
601 * beginning of the pp_es table, and all the others at the end,
602 * so that we can close them more easily at the end. */
605 p_es = input_AddES( p_input, p_input->stream.pp_programs[0],
607 p_es->i_type = p_byte[0];
608 p_es->b_audio = ( p_es->i_type == MPEG1_AUDIO_ES
609 || p_es->i_type == MPEG2_AUDIO_ES
610 || p_es->i_type == AC3_AUDIO_ES
611 || p_es->i_type == LPCM_AUDIO_ES
614 /* input_AddES has inserted the new element at the end. */
615 p_input->stream.pp_programs[0]->pp_es[
616 p_input->stream.pp_programs[0]->i_es_number ]
617 = p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ];
618 p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ] = p_es;
621 p_byte += 4 + U16_AT(&p_byte[2]);
624 /* Un-select the streams that are no longer parts of the program. */
625 while( i_new_es_number < p_input->stream.pp_programs[0]->i_es_number )
627 /* We remove pp_es[i_new_es_member] and not pp_es[i] because the
628 * list will be emptied starting from the end */
629 input_DelES( p_input,
630 p_input->stream.pp_programs[0]->pp_es[i_new_es_number] );
633 if( p_main->b_stats )
635 intf_StatMsg( "input info: The stream map after the PSM is now :" );
636 input_DumpStream( p_input );
639 vlc_mutex_unlock( &p_input->stream.stream_lock );
642 /*****************************************************************************
643 * input_ReadPS: store a PS packet into a data_buffer_t
644 *****************************************************************************/
645 #define PEEK( SIZE ) \
646 i_error = input_Peek( p_input, &p_peek, SIZE ); \
647 if( i_error == -1 ) \
651 else if( i_error < SIZE ) \
657 ssize_t input_ReadPS( input_thread_t * p_input, data_packet_t ** pp_data )
660 size_t i_packet_size;
661 ssize_t i_error, i_read;
663 /* Read what we believe to be a packet header. */
666 if( *p_peek || *(p_peek + 1) || *(p_peek + 2) != 1 )
668 if( *p_peek || *(p_peek + 1) || *(p_peek + 2) )
670 /* It is common for MPEG-1 streams to pad with zeros
671 * (although it is forbidden by the recommendation), so
672 * don't bother everybody in this case. */
673 intf_WarnMsg( 3, "input warning: garbage at input (0x%x%x%x%x)",
674 *p_peek, *(p_peek + 1), *(p_peek + 2), *(p_peek + 3) );
677 /* This is not the startcode of a packet. Read the stream
678 * until we find one. */
679 while( *p_peek || *(p_peek + 1) || *(p_peek + 2) != 1 )
681 p_input->p_current_data++;
687 /* 0x1B9 == SYSTEM_END_CODE, it is only 4 bytes long. */
688 if( p_peek[3] != 0xB9 )
690 /* The packet is at least 6 bytes long. */
693 if( p_peek[3] != 0xBA )
695 /* That's the case for all packets, except pack header. */
696 i_packet_size = (p_peek[4] << 8) | p_peek[5];
701 if( (p_peek[4] & 0xC0) == 0x40 )
706 else if( (p_peek[4] & 0xF0) == 0x20 )
713 intf_ErrMsg( "Unable to determine stream type" );
720 /* System End Code */
724 /* Fetch a packet of the appropriate size. */
725 i_read = input_SplitBuffer( p_input, pp_data, i_packet_size + 6 );
731 /* In MPEG-2 pack headers we still have to read stuffing bytes. */
732 if( ((*pp_data)->p_demux_start[3] == 0xBA) && (i_packet_size == 8) )
734 size_t i_stuffing = ((*pp_data)->p_demux_start[13] & 0x7);
735 /* Force refill of the input buffer - though we don't care
736 * about p_peek. Please note that this is unoptimized. */
738 p_input->p_current_data += i_stuffing;
746 /*****************************************************************************
747 * input_ParsePS: read the PS header
748 *****************************************************************************/
749 es_descriptor_t * input_ParsePS( input_thread_t * p_input,
750 data_packet_t * p_data )
753 es_descriptor_t * p_es = NULL;
755 i_code = p_data->p_demux_start[3];
757 if( i_code > 0xBC ) /* ES start code */
762 /* This is a PES packet. Find out if we want it or not. */
763 i_id = GetID( p_data );
765 vlc_mutex_lock( &p_input->stream.stream_lock );
766 if( p_input->stream.pp_programs[0]->b_is_ok )
768 /* Look only at the selected ES. */
769 for( i_dummy = 0; i_dummy < p_input->stream.i_selected_es_number;
772 if( p_input->stream.pp_selected_es[i_dummy] != NULL
773 && p_input->stream.pp_selected_es[i_dummy]->i_id == i_id )
775 p_es = p_input->stream.pp_selected_es[i_dummy];
782 stream_ps_data_t * p_demux =
783 (stream_ps_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
785 /* Search all ES ; if not found -> AddES */
786 p_es = input_FindES( p_input, i_id );
788 if( p_es == NULL && !p_demux->b_has_PSM )
790 p_es = input_AddES( p_input, p_input->stream.pp_programs[0],
794 p_es->i_stream_id = p_data->p_demux_start[3];
796 /* Set stream type and auto-spawn. */
797 if( (i_id & 0xF0) == 0xE0 )
800 p_es->i_type = MPEG2_VIDEO_ES;
801 p_es->i_cat = VIDEO_ES;
803 if( !p_input->stream.b_seekable )
804 input_SelectES( p_input, p_es );
807 else if( (i_id & 0xE0) == 0xC0 )
810 p_es->i_type = MPEG2_AUDIO_ES;
812 p_es->i_cat = AUDIO_ES;
814 if( !p_input->stream.b_seekable )
815 if( config_GetIntVariable( INPUT_CHANNEL_VAR )
816 == (p_es->i_id & 0x1F) ||
817 ( config_GetIntVariable( INPUT_CHANNEL_VAR ) < 0
818 && !(p_es->i_id & 0x1F) ) )
819 switch( config_GetIntVariable( INPUT_AUDIO_VAR ) )
823 input_SelectES( p_input, p_es );
827 else if( (i_id & 0xF0FF) == 0x80BD )
829 /* AC3 audio (0x80->0x8F) */
830 p_es->i_type = AC3_AUDIO_ES;
832 p_es->i_cat = AUDIO_ES;
834 if( !p_input->stream.b_seekable )
835 if( config_GetIntVariable( INPUT_CHANNEL_VAR )
836 == ((p_es->i_id & 0xF00) >> 8) ||
837 ( config_GetIntVariable( INPUT_CHANNEL_VAR ) < 0
838 && !((p_es->i_id & 0xF00) >> 8)) )
839 switch( config_GetIntVariable( INPUT_AUDIO_VAR ) )
843 input_SelectES( p_input, p_es );
847 else if( (i_id & 0xE0FF) == 0x20BD )
849 /* Subtitles video (0x20->0x3F) */
850 p_es->i_type = DVD_SPU_ES;
851 p_es->i_cat = SPU_ES;
853 if( config_GetIntVariable( INPUT_SUBTITLE_VAR )
854 == ((p_es->i_id & 0x1F00) >> 8) )
856 if( !p_input->stream.b_seekable )
857 input_SelectES( p_input, p_es );
861 else if( (i_id & 0xF0FF) == 0xA0BD )
863 /* LPCM audio (0xA0->0xAF) */
864 p_es->i_type = LPCM_AUDIO_ES;
866 p_es->i_cat = AUDIO_ES;
870 p_es->i_type = UNKNOWN_ES;
874 /* Tell the interface the stream has changed */
875 p_input->stream.b_changed = 1;
877 } /* stream.b_is_ok */
878 vlc_mutex_unlock( &p_input->stream.stream_lock );
879 } /* i_code > 0xBC */
884 /*****************************************************************************
885 * input_DemuxPS: first step of demultiplexing: the PS header
886 *****************************************************************************/
887 void input_DemuxPS( input_thread_t * p_input, data_packet_t * p_data )
890 boolean_t b_trash = 0;
891 es_descriptor_t * p_es = NULL;
893 i_code = ((u32)p_data->p_demux_start[0] << 24)
894 | ((u32)p_data->p_demux_start[1] << 16)
895 | ((u32)p_data->p_demux_start[2] << 8)
896 | p_data->p_demux_start[3];
897 if( i_code <= 0x1BC )
901 case 0x1BA: /* PACK_START_CODE */
907 if( (p_data->p_demux_start[4] & 0xC0) == 0x40 )
912 p_byte = p_data->p_demux_start;
914 if( MoveChunk( p_header, &p_data, &p_byte, 14 ) != 14 )
916 intf_WarnMsg( 1, "input: packet too short "
917 "to have a header" );
922 ((mtime_t)(p_header[4] & 0x38) << 27) |
923 ((mtime_t)(U32_AT(p_header + 4) & 0x03FFF800)
925 ((( ((mtime_t)U16_AT(p_header + 6) << 16)
926 | (mtime_t)U16_AT(p_header + 8) ) & 0x03FFF800)
930 i_mux_rate = ((u32)U16_AT(p_header + 10) << 6)
931 | (p_header[12] >> 2);
932 /* FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
933 * This is the biggest kludge ever !
934 * I don't know what's wrong with mux_rate calculation
935 * but this heuristic work well : */
941 /* MPEG-1 SCR is like PTS. */
944 p_byte = p_data->p_demux_start;
946 if( MoveChunk( p_header, &p_data, &p_byte, 12 ) != 12 )
948 intf_WarnMsg( 1, "input: packet too short "
949 "to have a header" );
954 ((mtime_t)(p_header[4] & 0x0E) << 29) |
955 (((mtime_t)U32_AT(p_header + 4) & 0xFFFE00) << 6) |
956 ((mtime_t)p_header[7] << 7) |
957 ((mtime_t)p_header[8] >> 1);
960 i_mux_rate = (U32_AT(p_header + 8) & 0x7FFFFE) >> 1;
962 /* Call the pace control. */
963 input_ClockManageRef( p_input,
964 p_input->stream.p_selected_program,
967 if( i_mux_rate != p_input->stream.i_mux_rate
968 && p_input->stream.i_mux_rate )
970 intf_WarnMsg( 2, "input: mux_rate changed, "
971 "expect cosmetic errors" );
973 p_input->stream.i_mux_rate = i_mux_rate;
979 case 0x1BB: /* SYSTEM_START_CODE */
980 b_trash = 1; /* Nothing interesting */
983 case 0x1BC: /* PROGRAM_STREAM_MAP_CODE */
984 DecodePSM( p_input, p_data );
988 case 0x1B9: /* PROGRAM_END_CODE */
993 /* This should not happen */
995 intf_WarnMsg( 3, "input: unwanted packet received "
996 "with start code 0x%.8x", i_code );
1001 p_es = input_ParsePS( p_input, p_data );
1003 vlc_mutex_lock( &p_input->stream.control.control_lock );
1004 if( p_es != NULL && p_es->p_decoder_fifo != NULL
1005 && (!p_es->b_audio || !p_input->stream.control.b_mute) )
1007 vlc_mutex_unlock( &p_input->stream.control.control_lock );
1009 input_GatherPES( p_input, p_data, p_es, 1, 0 );
1013 vlc_mutex_unlock( &p_input->stream.control.control_lock );
1018 /* Trash the packet if it has no payload or if it isn't selected */
1021 input_DeletePacket( p_input->p_method_data, p_data );
1022 p_input->stream.c_packets_trashed++;
1031 /*****************************************************************************
1032 * input_ReadTS: store a TS packet into a data_buffer_t
1033 *****************************************************************************/
1034 #define PEEK( SIZE ) \
1035 i_error = input_Peek( p_input, &p_peek, SIZE ); \
1036 if( i_error == -1 ) \
1040 else if( i_error < SIZE ) \
1046 ssize_t input_ReadTS( input_thread_t * p_input, data_packet_t ** pp_data )
1049 ssize_t i_error, i_read;
1053 if( *p_peek != TS_SYNC_CODE )
1055 intf_WarnMsg( 3, "input warning: garbage at input (%x)", *p_peek );
1057 if( p_input->i_mtu )
1059 while( *p_peek != TS_SYNC_CODE )
1061 /* Try to resync on next packet. */
1062 PEEK( TS_PACKET_SIZE );
1063 p_input->p_current_data += TS_PACKET_SIZE;
1069 /* Move forward until we find 0x47 (and hope it's the good
1071 while( *p_peek != TS_SYNC_CODE )
1073 p_input->p_current_data++;
1079 i_read = input_SplitBuffer( p_input, pp_data, TS_PACKET_SIZE );
1088 /*****************************************************************************
1089 * input_DemuxTS: first step of demultiplexing: the TS header
1090 *****************************************************************************/
1091 void input_DemuxTS( input_thread_t * p_input, data_packet_t * p_data )
1095 boolean_t b_adaptation; /* Adaptation field is present */
1096 boolean_t b_payload; /* Packet carries payload */
1097 boolean_t b_unit_start; /* A PSI or a PES start in the packet */
1098 boolean_t b_trash = 0; /* Is the packet unuseful ? */
1099 boolean_t b_lost = 0; /* Was there a packet loss ? */
1100 boolean_t b_psi = 0; /* Is this a PSI ? */
1101 es_descriptor_t * p_es = NULL;
1102 es_ts_data_t * p_es_demux = NULL;
1103 pgrm_ts_data_t * p_pgrm_demux = NULL;
1105 #define p (p_data->p_demux_start)
1106 /* Extract flags values from TS common header. */
1107 i_pid = ((p[1] & 0x1F) << 8) | p[2];
1108 b_unit_start = (p[1] & 0x40);
1109 b_adaptation = (p[3] & 0x20);
1110 b_payload = (p[3] & 0x10);
1112 /* Find out the elementary stream. */
1113 vlc_mutex_lock( &p_input->stream.stream_lock );
1115 p_es= input_FindES( p_input, i_pid );
1117 if( (p_es != NULL) && (p_es->p_demux_data != NULL) )
1119 p_es_demux = (es_ts_data_t *)p_es->p_demux_data;
1121 if( p_es_demux->b_psi )
1127 p_pgrm_demux = (pgrm_ts_data_t *)p_es->p_pgrm->p_demux_data;
1131 vlc_mutex_lock( &p_input->stream.control.control_lock );
1132 if( ( p_es == NULL ) || (p_es->b_audio && p_input->stream.control.b_mute) )
1134 /* Not selected. Just read the adaptation field for a PCR. */
1137 else if( p_es->p_decoder_fifo == NULL && !b_psi )
1142 vlc_mutex_unlock( &p_input->stream.control.control_lock );
1143 vlc_mutex_unlock( &p_input->stream.stream_lock );
1146 /* Don't change the order of the tests : if b_psi then p_pgrm_demux
1147 * may still be null. Who said it was ugly ?
1148 * I have written worse. --Meuuh */
1149 if( ( p_es != NULL ) &&
1150 ((p_es->p_decoder_fifo != NULL) || b_psi
1151 || (p_pgrm_demux->i_pcr_pid == i_pid) ) )
1155 /* Extract adaptation field information if any */
1159 /* We don't have any adaptation_field, so payload starts
1160 * immediately after the 4 byte TS header */
1161 p_data->p_payload_start += 4;
1165 /* p[4] is adaptation_field_length minus one */
1166 p_data->p_payload_start += 5 + p[4];
1168 /* The adaptation field can be limited to the
1169 * adaptation_field_length byte, so that there is nothing to do:
1170 * skip this possibility */
1173 /* If the packet has both adaptation_field and payload,
1174 * adaptation_field cannot be more than 182 bytes long; if
1175 * there is only an adaptation_field, it must fill the next
1177 if( b_payload ? (p[4] > 182) : (p[4] != 183) )
1180 "input: invalid TS adaptation field (%p)",
1182 p_data->b_discard_payload = 1;
1183 p_es->c_invalid_packets++;
1186 /* Now we are sure that the byte containing flags is present:
1190 /* discontinuity_indicator */
1194 "input: discontinuity_indicator"
1195 " encountered by TS demux (position read: %d,"
1197 p[5] & 0x80, p_es_demux->i_continuity_counter );
1199 /* If the PID carries the PCR, there will be a system
1200 * time-based discontinuity. We let the PCR decoder
1202 p_es->p_pgrm->i_synchro_state = SYNCHRO_REINIT;
1204 /* There also may be a continuity_counter
1205 * discontinuity: resynchronize our counter with
1206 * the one of the stream. */
1207 p_es_demux->i_continuity_counter = (p[3] & 0x0f) - 1;
1210 /* If this is a PCR_PID, and this TS packet contains a
1211 * PCR, we pass it along to the PCR decoder. */
1213 if( !b_psi && (p_pgrm_demux->i_pcr_pid == i_pid)
1216 /* There should be a PCR field in the packet, check
1217 * if the adaptation field is long enough to carry
1223 pcr_time = ( (mtime_t)p[6] << 25 ) |
1224 ( (mtime_t)p[7] << 17 ) |
1225 ( (mtime_t)p[8] << 9 ) |
1226 ( (mtime_t)p[9] << 1 ) |
1227 ( (mtime_t)p[10] >> 7 );
1228 /* Call the pace control. */
1229 input_ClockManageRef( p_input, p_es->p_pgrm,
1233 } /* valid TS adaptation field ? */
1235 } /* has adaptation field */
1236 /* Check the continuity of the stream. */
1237 i_dummy = ((p[3] & 0x0f) - p_es_demux->i_continuity_counter) & 0x0f;
1240 /* Everything is ok, just increase our counter */
1241 (p_es_demux->i_continuity_counter)++;
1245 if( !b_payload && i_dummy == 0 )
1247 /* This is a packet without payload, this is allowed by the
1248 * draft. As there is nothing interesting in this packet
1249 * (except PCR that have already been handled), we can trash
1251 intf_WarnMsg( 3, "input: packet without payload received "
1255 else if( i_dummy <= 0 )
1257 /* Duplicate packet: mark it as being to be trashed. */
1258 intf_WarnMsg( 3, "input: duplicate packet received "
1262 else if( p_es_demux->i_continuity_counter == 0xFF )
1264 /* This means that the packet is the first one we receive for
1265 * this ES since the continuity counter ranges between 0 and
1266 * 0x0F excepts when it has been initialized by the input:
1267 * init the counter to the correct value. */
1268 intf_WarnMsg( 3, "input: first packet for PID %d received "
1269 "by TS demux", p_es->i_id );
1270 p_es_demux->i_continuity_counter = (p[3] & 0x0f);
1274 /* This can indicate that we missed a packet or that the
1275 * continuity_counter wrapped and we received a dup packet:
1276 * as we don't know, do as if we missed a packet to be sure
1277 * to recover from this situation */
1278 intf_WarnMsg( 2, "input: packet lost by TS demux: "
1279 "current %d, packet %d",
1280 p_es_demux->i_continuity_counter & 0x0f,
1283 p_es_demux->i_continuity_counter = p[3] & 0x0f;
1284 } /* not continuous */
1286 } /* if selected or PCR */
1288 /* Trash the packet if it has no payload or if it isn't selected */
1291 input_DeletePacket( p_input->p_method_data, p_data );
1292 p_input->stream.c_packets_trashed++;
1298 /* The payload contains PSI tables */
1299 input_DemuxPSI( p_input, p_data, p_es,
1300 b_unit_start, b_lost );
1305 /* The payload carries a PES stream */
1306 input_GatherPES( p_input, p_data, p_es, b_unit_start, b_lost );
1316 * PSI demultiplexing and decoding
1319 /*****************************************************************************
1320 * DemuxPSI : makes up complete PSI data
1321 *****************************************************************************/
1322 void input_DemuxPSI( input_thread_t * p_input, data_packet_t * p_data,
1323 es_descriptor_t * p_es, boolean_t b_unit_start, boolean_t b_lost )
1325 es_ts_data_t * p_demux_data;
1327 p_demux_data = (es_ts_data_t *)p_es->p_demux_data;
1329 #define p_psi (p_demux_data->p_psi_section)
1330 #define p (p_data->p_payload_start)
1334 /* unit_start set to 1 -> presence of a pointer field
1335 * (see ISO/IEC 13818 (2.4.4.2) which should be set to 0x00 */
1336 if( (u8)p[0] != 0x00 )
1338 intf_WarnMsg( 2, "input: non zero pointer field found, "
1339 "trying to continue" );
1347 /* This is the begining of a new section */
1349 if( ((u8)(p[1]) & 0xc0) != 0x80 )
1351 intf_WarnMsg( 2, "input: invalid PSI packet" );
1356 p_psi->i_section_length = ((p[1] & 0xF) << 8) | p[2];
1357 p_psi->b_section_complete = 0;
1358 p_psi->i_read_in_section = 0;
1359 p_psi->i_section_number = (u8)p[6];
1361 if( p_psi->b_is_complete || p_psi->i_section_number == 0 )
1363 /* This is a new PSI packet */
1364 p_psi->b_is_complete = 0;
1366 p_psi->i_version_number = ( p[5] >> 1 ) & 0x1f;
1367 p_psi->i_last_section_number = (u8)p[7];
1369 /* We'll write at the begining of the buffer */
1370 p_psi->p_current = p_psi->buffer;
1374 if( p_psi->b_section_complete )
1376 /* New Section of an already started PSI */
1377 p_psi->b_section_complete = 0;
1379 if( p_psi->i_version_number != (( p[5] >> 1 ) & 0x1f) )
1381 intf_WarnMsg( 2, "input: PSI version differs "
1382 "inside same PAT" );
1385 if( p_psi->i_section_number + 1 != (u8)p[6] )
1387 intf_WarnMsg( 2, "input: PSI Section discontinuity, "
1392 p_psi->i_section_number++;
1396 intf_WarnMsg( 2, "input: got unexpected new PSI section" );
1401 } /* b_unit_start */
1403 if( !p_psi->b_trash )
1406 if( (p_data->p_payload_end - p) >=
1407 ( p_psi->i_section_length - p_psi->i_read_in_section ) )
1409 /* The end of the section is in this TS packet */
1410 memcpy( p_psi->p_current, p,
1411 (p_psi->i_section_length - p_psi->i_read_in_section) );
1413 p_psi->b_section_complete = 1;
1415 (p_psi->i_section_length - p_psi->i_read_in_section);
1417 if( p_psi->i_section_number == p_psi->i_last_section_number )
1419 /* This was the last section of PSI */
1420 p_psi->b_is_complete = 1;
1422 switch( p_demux_data->i_psi_type)
1425 input_DecodePAT( p_input, p_es );
1428 input_DecodePMT( p_input, p_es );
1431 intf_WarnMsg(2, "Received unknown PSI in DemuxPSI");
1437 memcpy( p_psi->buffer, p, p_data->p_payload_end - p );
1438 p_psi->i_read_in_section += p_data->p_payload_end - p;
1440 p_psi->p_current += p_data->p_payload_end - p;
1447 input_DeletePacket( p_input->p_method_data, p_data );
1452 /*****************************************************************************
1453 * DecodePAT : Decodes Programm association table and deal with it
1454 *****************************************************************************/
1455 static void input_DecodePAT( input_thread_t * p_input, es_descriptor_t * p_es )
1457 stream_ts_data_t * p_stream_data;
1458 es_ts_data_t * p_demux_data;
1460 pgrm_descriptor_t * p_pgrm;
1461 es_descriptor_t * p_current_es;
1462 byte_t * p_current_data;
1464 int i_section_length, i_program_id, i_pmt_pid;
1465 int i_loop, i_current_section;
1467 boolean_t b_changed = 0;
1469 p_demux_data = (es_ts_data_t *)p_es->p_demux_data;
1470 p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
1472 #define p_psi (p_demux_data->p_psi_section)
1474 /* Not so fast, Mike ! If the PAT version has changed, we first check
1475 * that its content has really changed before doing anything */
1476 if( p_stream_data->i_pat_version != p_psi->i_version_number )
1478 int i_programs = p_input->stream.i_pgrm_number;
1480 p_current_data = p_psi->buffer;
1484 i_section_length = ((u32)(p_current_data[1] & 0xF) << 8) |
1486 i_current_section = (u8)p_current_data[6];
1489 ( i_loop < (i_section_length - 9) / 4 ) && !b_changed;
1492 i_program_id = ( (u32)*(p_current_data + i_loop * 4 + 8) << 8 )
1493 | *(p_current_data + i_loop * 4 + 9);
1494 i_pmt_pid = ( ((u32)*(p_current_data + i_loop * 4 + 10) & 0x1F)
1496 | *(p_current_data + i_loop * 4 + 11);
1500 if( (p_pgrm = input_FindProgram( p_input, i_program_id ))
1501 && (p_current_es = input_FindES( p_input, i_pmt_pid ))
1502 && p_current_es->p_pgrm == p_pgrm
1503 && p_current_es->i_id == i_pmt_pid
1504 && ((es_ts_data_t *)p_current_es->p_demux_data)->b_psi
1505 && ((es_ts_data_t *)p_current_es->p_demux_data)
1506 ->i_psi_type == PSI_IS_PMT )
1517 p_current_data += 3 + i_section_length;
1519 } while( ( i_current_section < p_psi->i_last_section_number )
1522 /* If we didn't find the expected amount of programs, the PAT has
1523 * changed. Otherwise, it only changed if b_changed is already != 0 */
1524 b_changed = b_changed || i_programs;
1529 /* PAT has changed. We are going to delete all programs and
1530 * create new ones. We chose not to only change what was needed
1531 * as a PAT change may mean the stream is radically changing and
1532 * this is a secure method to avoid crashes */
1533 es_ts_data_t * p_es_demux;
1534 pgrm_ts_data_t * p_pgrm_demux;
1536 p_current_data = p_psi->buffer;
1538 /* Delete all programs */
1539 while( p_input->stream.i_pgrm_number )
1541 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
1546 i_section_length = ((u32)(p_current_data[1] & 0xF) << 8) |
1548 i_current_section = (u8)p_current_data[6];
1550 for( i_loop = 0; i_loop < (i_section_length - 9) / 4 ; i_loop++ )
1552 i_program_id = ( (u32)*(p_current_data + i_loop * 4 + 8) << 8 )
1553 | *(p_current_data + i_loop * 4 + 9);
1554 i_pmt_pid = ( ((u32)*(p_current_data + i_loop * 4 + 10) & 0x1F)
1556 | *(p_current_data + i_loop * 4 + 11);
1558 /* If program = 0, we're having info about NIT not PMT */
1561 /* Add this program */
1562 p_pgrm = input_AddProgram( p_input, i_program_id,
1563 sizeof( pgrm_ts_data_t ) );
1565 /* whatis the PID of the PMT of this program */
1566 p_pgrm_demux = (pgrm_ts_data_t *)p_pgrm->p_demux_data;
1567 p_pgrm_demux->i_pmt_version = PMT_UNINITIALIZED;
1569 /* Add the PMT ES to this program */
1570 p_current_es = input_AddES( p_input, p_pgrm,(u16)i_pmt_pid,
1571 sizeof( es_ts_data_t) );
1572 p_es_demux = (es_ts_data_t *)p_current_es->p_demux_data;
1573 p_es_demux->b_psi = 1;
1574 p_es_demux->i_psi_type = PSI_IS_PMT;
1576 p_es_demux->p_psi_section =
1577 malloc( sizeof( psi_section_t ) );
1578 p_es_demux->p_psi_section->b_is_complete = 0;
1582 p_current_data += 3 + i_section_length;
1584 } while( i_current_section < p_psi->i_last_section_number );
1586 /* Go to the beginning of the next section */
1587 p_stream_data->i_pat_version = p_psi->i_version_number;
1592 /* FIXME This has nothing to do here */
1593 p_input->stream.p_selected_program = p_input->stream.pp_programs[0] ;
1596 /*****************************************************************************
1597 * DecodePMT : decode a given Program Stream Map
1598 * ***************************************************************************
1599 * When the PMT changes, it may mean a deep change in the stream, and it is
1600 * careful to delete the ES and add them again. If the PMT doesn't change,
1601 * there no need to do anything.
1602 *****************************************************************************/
1603 static void input_DecodePMT( input_thread_t * p_input, es_descriptor_t * p_es )
1606 pgrm_ts_data_t * p_pgrm_data;
1607 es_ts_data_t * p_demux_data;
1609 p_demux_data = (es_ts_data_t *)p_es->p_demux_data;
1610 p_pgrm_data = (pgrm_ts_data_t *)p_es->p_pgrm->p_demux_data;
1612 #define p_psi (p_demux_data->p_psi_section)
1614 if( p_psi->i_version_number != p_pgrm_data->i_pmt_version )
1616 es_descriptor_t * p_new_es;
1617 es_ts_data_t * p_es_demux;
1618 byte_t * p_current_data, * p_current_section;
1619 int i_section_length,i_current_section;
1620 int i_prog_info_length, i_loop;
1621 int i_es_info_length, i_pid, i_stream_type;
1622 int i_audio_es, i_spu_es;
1623 int i_required_audio_es, i_required_spu_es;
1625 p_current_section = p_psi->buffer;
1626 p_current_data = p_psi->buffer;
1628 p_pgrm_data->i_pcr_pid = ( ((u32)*(p_current_section + 8) & 0x1F) << 8 ) |
1629 *(p_current_section + 9);
1634 /* Lock stream information */
1635 vlc_mutex_lock( &p_input->stream.stream_lock );
1637 /* Get the number of the required audio stream */
1638 if( p_main->b_audio )
1640 /* Default is the first one */
1641 i_required_audio_es = config_GetIntVariable( INPUT_CHANNEL_VAR );
1642 if( i_required_audio_es < 0 )
1644 i_required_audio_es = 1;
1649 i_required_audio_es = 0;
1652 /* Same thing for subtitles */
1653 if( p_main->b_video )
1655 /* for spu, default is none */
1656 i_required_spu_es = config_GetIntVariable( INPUT_SUBTITLE_VAR );
1657 if( i_required_spu_es < 0 )
1659 i_required_spu_es = 0;
1664 i_required_spu_es = 0;
1667 /* Delete all ES in this program except the PSI. We start from the
1668 * end because i_es_number gets decremented after each deletion. */
1669 for( i_loop = p_es->p_pgrm->i_es_number ; i_loop ; )
1672 p_es_demux = (es_ts_data_t *)
1673 p_es->p_pgrm->pp_es[i_loop]->p_demux_data;
1674 if ( ! p_es_demux->b_psi )
1676 input_DelES( p_input, p_es->p_pgrm->pp_es[i_loop] );
1680 /* Then add what we received in this PMT */
1683 i_section_length = ( ((u32)*(p_current_data + 1) & 0xF) << 8 ) |
1684 *(p_current_data + 2);
1685 i_current_section = (u8)p_current_data[6];
1686 i_prog_info_length = ( ((u32)*(p_current_data + 10) & 0xF) << 8 ) |
1687 *(p_current_data + 11);
1689 /* For the moment we ignore program descriptors */
1690 p_current_data += 12 + i_prog_info_length;
1692 /* The end of the section, before the CRC is at
1693 * p_current_section + i_section_length -1 */
1694 while( p_current_data < p_current_section + i_section_length -1 )
1696 i_stream_type = (int)p_current_data[0];
1697 i_pid = ( ((u32)*(p_current_data + 1) & 0x1F) << 8 ) |
1698 *(p_current_data + 2);
1699 i_es_info_length = ( ((u32)*(p_current_data + 3) & 0xF) << 8 ) |
1700 *(p_current_data + 4);
1702 /* Add this ES to the program */
1703 p_new_es = input_AddES( p_input, p_es->p_pgrm,
1704 (u16)i_pid, sizeof( es_ts_data_t ) );
1706 /* Tell the decoders what kind of stream it is */
1707 p_new_es->i_type = i_stream_type;
1709 /* Tell the interface what kind of stream it is and select
1710 * the required ones */
1711 switch( i_stream_type )
1713 case MPEG1_VIDEO_ES:
1714 case MPEG2_VIDEO_ES:
1715 p_new_es->i_cat = VIDEO_ES;
1716 input_SelectES( p_input, p_new_es );
1718 case MPEG1_AUDIO_ES:
1719 case MPEG2_AUDIO_ES:
1720 p_new_es->i_cat = AUDIO_ES;
1722 if( i_audio_es == i_required_audio_es )
1723 input_SelectES( p_input, p_new_es );
1725 case LPCM_AUDIO_ES :
1727 p_new_es->i_stream_id = 0xBD;
1728 p_new_es->i_cat = AUDIO_ES;
1730 if( i_audio_es == i_required_audio_es )
1731 input_SelectES( p_input, p_new_es );
1733 /* Not sure this one is fully specification-compliant */
1735 p_new_es->i_stream_id = 0xBD;
1736 p_new_es->i_cat = SPU_ES;
1738 if( i_spu_es == i_required_spu_es )
1739 input_SelectES( p_input, p_new_es );
1742 p_new_es->i_cat = UNKNOWN_ES;
1746 p_current_data += 5 + i_es_info_length;
1749 /* Go to the beginning of the next section*/
1750 p_current_data += 3 + i_section_length;
1752 p_current_section++;
1754 } while( i_current_section < p_psi->i_last_section_number );
1756 if( i_required_audio_es > i_audio_es )
1758 intf_WarnMsg( 2, "input: non-existing audio ES required" );
1761 if( i_required_spu_es > i_spu_es )
1763 intf_WarnMsg( 2, "input: non-existing subtitles ES required" );
1766 p_pgrm_data->i_pmt_version = p_psi->i_version_number;
1768 /* inform interface that stream has changed */
1769 p_input->stream.b_changed = 1;
1771 vlc_mutex_unlock( &p_input->stream.stream_lock );