1 /*****************************************************************************
2 * mpeg_system.c: TS, PS and PES management
3 *****************************************************************************
4 * Copyright (C) 1998, 1999, 2000 VideoLAN
5 * $Id: mpeg_system.c,v 1.45 2001/03/15 01:42:20 sam 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 *****************************************************************************/
42 #include "stream_control.h"
43 #include "input_ext-intf.h"
44 #include "input_ext-dec.h"
47 #include "mpeg_system.h"
49 #include "main.h" /* AC3/MPEG channel, SPU channel */
51 /*****************************************************************************
53 *****************************************************************************/
55 static void input_DecodePAT( input_thread_t *, es_descriptor_t *);
56 static void input_DecodePMT( input_thread_t *, es_descriptor_t *);
59 * PES Packet management
62 /*****************************************************************************
64 *****************************************************************************
65 * Small utility function used to parse discontinuous headers safely. Copies
66 * i_buf_len bytes of data to a buffer and returns the size copied.
67 * It also solves some alignment problems on non-IA-32, non-PPC processors.
68 * This is a variation on the theme of input_ext-dec.h:GetChunk().
69 *****************************************************************************/
70 static __inline__ size_t MoveChunk( byte_t * p_dest,
71 data_packet_t ** pp_data_src,
75 ptrdiff_t i_available;
77 if( (i_available = (*pp_data_src)->p_payload_end - *pp_src)
81 memcpy( p_dest, *pp_src, i_buf_len );
87 size_t i_init_len = i_buf_len;
92 memcpy( p_dest, *pp_src, i_available );
93 *pp_data_src = (*pp_data_src)->p_next;
94 i_buf_len -= i_available;
95 p_dest += i_available;
96 if( *pp_data_src == NULL )
99 return( i_init_len - i_buf_len );
101 *pp_src = (*pp_data_src)->p_payload_start;
103 while( (i_available = (*pp_data_src)->p_payload_end - *pp_src)
109 memcpy( p_dest, *pp_src, i_buf_len );
110 *pp_src += i_buf_len;
112 return( i_init_len );
116 /*****************************************************************************
118 *****************************************************************************
119 * Parse a finished PES packet and analyze its header.
120 *****************************************************************************/
121 #define PES_HEADER_SIZE 7
122 void input_ParsePES( input_thread_t * p_input, es_descriptor_t * p_es )
124 data_packet_t * p_data;
126 byte_t p_header[PES_HEADER_SIZE];
129 #define p_pes (p_es->p_pes)
131 //intf_DbgMsg("End of PES packet %p", p_pes);
133 /* Parse the header. The header has a variable length, but in order
134 * to improve the algorithm, we will read the 14 bytes we may be
136 p_data = p_pes->p_first;
137 p_byte = p_data->p_payload_start;
140 if( MoveChunk( p_header, &p_data, &p_byte, PES_HEADER_SIZE )
143 intf_WarnMsg( 3, "PES packet too short to have a header" );
144 p_input->pf_delete_pes( p_input->p_method_data, p_pes );
149 /* Get the PES size if defined */
150 p_es->i_pes_real_size = U16_AT(p_header + 4) + 6;
152 /* First read the 6 header bytes common to all PES packets:
153 * use them to test the PES validity */
154 if( (p_header[0] || p_header[1] || (p_header[2] != 1)) )
156 /* packet_start_code_prefix != 0x000001 */
157 intf_ErrMsg( "PES packet doesn't start with 0x000001 : data loss" );
158 p_input->pf_delete_pes( p_input->p_method_data, p_pes );
163 int i_pes_header_size, i_payload_size;
165 if ( p_es->i_pes_real_size &&
166 (p_es->i_pes_real_size != p_pes->i_pes_size) )
168 /* PES_packet_length is set and != total received payload */
169 /* Warn the decoder that the data may be corrupt. */
170 intf_WarnMsg( 3, "PES sizes do not match : packet corrupted" );
173 switch( p_es->i_stream_id )
175 case 0xBC: /* Program stream map */
176 case 0xBE: /* Padding */
177 case 0xBF: /* Private stream 2 */
180 case 0xFF: /* Program stream directory */
181 case 0xF2: /* DSMCC stream */
182 case 0xF8: /* ITU-T H.222.1 type E stream */
183 /* The payload begins immediately after the 6 bytes header, so
184 * we have finished with the parsing */
185 i_pes_header_size = 6;
189 if( (p_header[6] & 0xC0) == 0x80 )
191 /* MPEG-2 : the PES header contains at least 3 more bytes. */
193 boolean_t b_has_pts, b_has_dts;
194 byte_t p_full_header[12];
196 p_pes->b_data_alignment = p_header[6] & 0x04;
198 i_max_len = MoveChunk( p_full_header, &p_data, &p_byte, 12 );
202 "PES packet too short to have a MPEG-2 header" );
203 p_input->pf_delete_pes( p_input->p_method_data,
209 b_has_pts = p_full_header[0] & 0x80;
210 b_has_dts = p_full_header[0] & 0x40;
211 i_pes_header_size = p_full_header[1] + 9;
213 /* Now parse the optional header extensions */
219 "PES packet too short to have a MPEG-2 header" );
220 p_input->pf_delete_pes( p_input->p_method_data,
225 p_pes->i_pts = input_ClockGetTS( p_input, p_es->p_pgrm,
226 ( ((mtime_t)(p_full_header[2] & 0x0E) << 29) |
227 ((mtime_t)(p_full_header[3]) << 22) |
228 ((mtime_t)(p_full_header[4] & 0xFE) << 14) |
229 ((mtime_t)p_full_header[5] << 7) |
230 ((mtime_t)p_full_header[6] >> 1) ) );
237 "PES packet too short to have a MPEG-2 header" );
238 p_input->pf_delete_pes( p_input->p_method_data,
243 p_pes->i_dts = input_ClockGetTS( p_input, p_es->p_pgrm,
244 ( ((mtime_t)(p_full_header[7] & 0x0E) << 29) |
245 (((mtime_t)U16_AT(p_full_header + 8) << 14)
247 ((mtime_t)U16_AT(p_full_header + 10) >> 1) ) );
253 /* Probably MPEG-1 */
254 boolean_t b_has_pts, b_has_dts;
256 i_pes_header_size = 6;
257 p_data = p_pes->p_first;
258 p_byte = p_data->p_payload_start;
259 /* Cannot fail because the previous one succeeded. */
260 MoveChunk( NULL, &p_data, &p_byte, 6 );
262 while( *p_byte == 0xFF && i_pes_header_size < 23 )
265 if( MoveChunk( NULL, &p_data, &p_byte, 1 ) != 1 )
268 "PES packet too short to have a MPEG-1 header" );
269 p_input->pf_delete_pes( p_input->p_method_data, p_pes );
274 if( i_pes_header_size == 23 )
276 intf_ErrMsg( "Too much MPEG-1 stuffing" );
277 p_input->pf_delete_pes( p_input->p_method_data, p_pes );
282 if( (*p_byte & 0xC0) == 0x40 )
284 /* Don't ask why... --Meuuh */
285 /* Erm... why ? --Sam */
286 /* Well... According to the recommendation, it is for
287 * STD_buffer_scale and STD_buffer_size. --Meuuh */
288 i_pes_header_size += 2;
289 if( MoveChunk( NULL, &p_data, &p_byte, 2 ) != 2 )
292 "PES packet too short to have a MPEG-1 header" );
293 p_input->pf_delete_pes( p_input->p_method_data, p_pes );
301 b_has_pts = *p_byte & 0x20;
302 b_has_dts = *p_byte & 0x10;
308 i_pes_header_size += 4;
309 if( MoveChunk( p_ts, &p_data, &p_byte, 5 ) != 5 )
312 "PES packet too short to have a MPEG-1 header" );
313 p_input->pf_delete_pes( p_input->p_method_data, p_pes );
318 p_pes->i_pts = input_ClockGetTS( p_input, p_es->p_pgrm,
319 ( ((mtime_t)(p_ts[0] & 0x0E) << 29) |
320 (((mtime_t)U32_AT(p_ts) & 0xFFFE00) << 6) |
321 ((mtime_t)p_ts[3] << 7) |
322 ((mtime_t)p_ts[4] >> 1) ) );
326 i_pes_header_size += 5;
327 if( MoveChunk( p_ts, &p_data, &p_byte, 5 ) != 5 )
330 "PES packet too short to have a MPEG-1 header" );
331 p_input->pf_delete_pes( p_input->p_method_data,
337 p_pes->i_dts = input_ClockGetTS( p_input,
339 ( ((mtime_t)(p_ts[0] & 0x0E) << 29) |
340 (((mtime_t)U32_AT(p_ts) & 0xFFFE00) << 6) |
341 ((mtime_t)p_ts[3] << 7) |
342 ((mtime_t)p_ts[4] >> 1) ) );
350 if( p_es->i_stream_id == 0xbd )
352 /* With private stream 1, the first byte of the payload
353 * is a stream_private_id, so skip it. */
357 /* Now we've parsed the header, we just have to indicate in some
358 * specific data packets where the PES payload begins (renumber
359 * p_payload_start), so that the decoders can find the beginning
360 * of their data right out of the box. */
361 p_data = p_pes->p_first;
362 i_payload_size = p_data->p_payload_end
363 - p_data->p_payload_start;
364 while( i_pes_header_size > i_payload_size )
366 /* These packets are entirely filled by the PES header. */
367 i_pes_header_size -= i_payload_size;
368 p_data->p_payload_start = p_data->p_payload_end;
369 /* Go to the next data packet. */
370 if( (p_data = p_data->p_next) == NULL )
372 intf_ErrMsg( "PES header bigger than payload" );
373 p_input->pf_delete_pes( p_input->p_method_data, p_pes );
377 i_payload_size = p_data->p_payload_end
378 - p_data->p_payload_start;
380 /* This last packet is partly header, partly payload. */
381 if( i_payload_size < i_pes_header_size )
383 intf_ErrMsg( "PES header bigger than payload" );
384 p_input->pf_delete_pes( p_input->p_method_data, p_pes );
388 p_data->p_payload_start += i_pes_header_size;
390 /* Now we can eventually put the PES packet in the decoder's
392 if( p_es->p_decoder_fifo != NULL )
394 input_DecodePES( p_es->p_decoder_fifo, p_pes );
398 intf_ErrMsg("No fifo to receive PES %p (who wrote this damn code ?)",
400 p_input->pf_delete_pes( p_input->p_method_data, p_pes );
408 /*****************************************************************************
410 *****************************************************************************
411 * Gather a PES packet.
412 *****************************************************************************/
413 void input_GatherPES( input_thread_t * p_input, data_packet_t * p_data,
414 es_descriptor_t * p_es,
415 boolean_t b_unit_start, boolean_t b_packet_lost )
417 #define p_pes (p_es->p_pes)
419 //intf_DbgMsg("PES-demultiplexing %p (%p)", p_ts_packet, p_pes);
421 /* If we lost data, insert a NULL data packet (philosophy : 0 is quite
422 * often an escape sequence in decoders, so that should make them wait
423 * for the next start code). */
426 input_NullPacket( p_input, p_es );
429 if( b_unit_start && p_pes != NULL )
431 /* If the data packet contains the begining of a new PES packet, and
432 * if we were reassembling a PES packet, then the PES should be
433 * complete now, so parse its header and give it to the decoders. */
434 input_ParsePES( p_input, p_es );
437 if( !b_unit_start && p_pes == NULL )
439 /* Random access... */
440 p_input->pf_delete_packet( p_input->p_method_data, p_data );
446 /* If we are at the beginning of a new PES packet, we must fetch
447 * a new PES buffer to begin with the reassembly of this PES
448 * packet. This is also here that we can synchronize with the
449 * stream if we lost packets or if the decoder has just
451 if( (p_pes = p_input->pf_new_pes( p_input->p_method_data ) ) == NULL )
453 intf_ErrMsg("Out of memory");
454 p_input->b_error = 1;
457 p_pes->i_rate = p_input->stream.control.i_rate;
458 p_pes->p_first = p_data;
460 /* If the PES header fits in the first data packet, we can
461 * already set p_gather->i_pes_real_size. */
462 if( p_data->p_payload_end - p_data->p_payload_start
465 p_es->i_pes_real_size =
466 U16_AT(p_data->p_payload_start + 4) + 6;
471 p_es->i_pes_real_size = 0;
476 /* Update the relations between the data packets */
477 p_es->p_last->p_next = p_data;
480 p_es->p_last = p_data;
482 /* Size of the payload carried in the data packet */
483 p_pes->i_pes_size += (p_data->p_payload_end
484 - p_data->p_payload_start);
486 /* We can check if the packet is finished */
487 if( p_pes->i_pes_size == p_es->i_pes_real_size )
489 /* The packet is finished, parse it */
490 input_ParsePES( p_input, p_es );
501 /*****************************************************************************
502 * GetID: Get the ID of a stream
503 *****************************************************************************/
504 static u16 GetID( data_packet_t * p_data )
508 i_id = p_data->p_payload_start[3]; /* stream_id */
511 /* FIXME : this is not valid if the header is split in multiple
513 /* stream_private_id */
514 i_id |= p_data->p_payload_start[ 9 + p_data->p_payload_start[8] ] << 8;
519 /*****************************************************************************
520 * DecodePSM: Decode the Program Stream Map information
521 *****************************************************************************
522 * FIXME : loads are not aligned in this function
523 *****************************************************************************/
524 static void DecodePSM( input_thread_t * p_input, data_packet_t * p_data )
526 stream_ps_data_t * p_demux =
527 (stream_ps_data_t *)p_input->stream.p_demux_data;
531 int i_new_es_number = 0;
533 if( p_data->p_payload_start + 10 > p_data->p_payload_end )
535 intf_ErrMsg( "PSM too short : packet corrupt" );
539 if( p_demux->b_has_PSM
540 && p_demux->i_PSM_version == (p_data->p_payload_start[6] & 0x1F) )
542 /* Already got that one. */
546 intf_DbgMsg( "Building PSM" );
547 p_demux->b_has_PSM = 1;
548 p_demux->i_PSM_version = p_data->p_payload_start[6] & 0x1F;
550 /* Go to elementary_stream_map_length, jumping over
551 * program_stream_info. */
552 p_byte = p_data->p_payload_start + 10
553 + U16_AT(&p_data->p_payload_start[8]);
554 if( p_byte > p_data->p_payload_end )
556 intf_ErrMsg( "PSM too short : packet corrupt" );
559 /* This is the full size of the elementary_stream_map.
560 * 2 == elementary_stream_map_length
561 * Please note that CRC_32 is not included in the length. */
562 p_end = p_byte + 2 + U16_AT(p_byte);
564 if( p_end > p_data->p_payload_end )
566 intf_ErrMsg( "PSM too short : packet corrupt" );
570 vlc_mutex_lock( &p_input->stream.stream_lock );
572 /* 4 == minimum useful size of a section */
573 while( p_byte + 4 <= p_end )
575 es_descriptor_t * p_es = NULL;
576 u8 i_stream_id = p_byte[1];
577 /* FIXME: there will be a problem with private streams... (same
580 /* Look for the ES in the ES table */
581 for( i = i_new_es_number;
582 i < p_input->stream.pp_programs[0]->i_es_number;
585 if( p_input->stream.pp_programs[0]->pp_es[i]->i_stream_id
588 p_es = p_input->stream.pp_programs[0]->pp_es[i];
589 if( p_es->i_type != p_byte[0] )
591 input_DelES( p_input, p_es );
596 /* Move the ES to the beginning. */
597 p_input->stream.pp_programs[0]->pp_es[i]
598 = p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ];
599 p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ]
607 /* The goal is to have all the ES we have just read in the
608 * beginning of the pp_es table, and all the others at the end,
609 * so that we can close them more easily at the end. */
612 p_es = input_AddES( p_input, p_input->stream.pp_programs[0],
614 p_es->i_type = p_byte[0];
615 p_es->b_audio = ( p_es->i_type == MPEG1_AUDIO_ES
616 || p_es->i_type == MPEG2_AUDIO_ES
617 || p_es->i_type == AC3_AUDIO_ES
618 || p_es->i_type == LPCM_AUDIO_ES
621 /* input_AddES has inserted the new element at the end. */
622 p_input->stream.pp_programs[0]->pp_es[
623 p_input->stream.pp_programs[0]->i_es_number ]
624 = p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ];
625 p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ] = p_es;
628 p_byte += 4 + U16_AT(&p_byte[2]);
631 /* Un-select the streams that are no longer parts of the program. */
632 for( i = i_new_es_number;
633 i < p_input->stream.pp_programs[0]->i_es_number;
636 /* We remove pp_es[i_new_es_member] and not pp_es[i] because the
637 * list will be emptied starting from the end */
638 input_DelES( p_input,
639 p_input->stream.pp_programs[0]->pp_es[i_new_es_number] );
643 intf_Msg( "input info: The stream map after the PSM is now :" );
644 input_DumpStream( p_input );
647 vlc_mutex_unlock( &p_input->stream.stream_lock );
650 /*****************************************************************************
651 * input_ParsePS: read the PS header
652 *****************************************************************************/
653 es_descriptor_t * input_ParsePS( input_thread_t * p_input,
654 data_packet_t * p_data )
657 es_descriptor_t * p_es = NULL;
659 i_code = p_data->p_payload_start[3];
660 if( i_code > 0xBC ) /* ES start code */
665 /* This is a PES packet. Find out if we want it or not. */
666 i_id = GetID( p_data );
668 vlc_mutex_lock( &p_input->stream.stream_lock );
669 if( p_input->stream.pp_programs[0]->b_is_ok )
671 /* Look only at the selected ES. */
672 for( i_dummy = 0; i_dummy < p_input->stream.i_selected_es_number;
675 if( p_input->stream.pp_selected_es[i_dummy] != NULL
676 && p_input->stream.pp_selected_es[i_dummy]->i_id == i_id )
678 p_es = p_input->stream.pp_selected_es[i_dummy];
685 stream_ps_data_t * p_demux =
686 (stream_ps_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
688 /* Search all ES ; if not found -> AddES */
689 p_es = input_FindES( p_input, i_id );
691 if( p_es == NULL && !p_demux->b_has_PSM )
693 p_es = input_AddES( p_input, p_input->stream.pp_programs[0],
697 p_es->i_stream_id = p_data->p_payload_start[3];
699 /* Set stream type and auto-spawn. */
700 if( (i_id & 0xF0) == 0xE0 )
703 p_es->i_type = MPEG2_VIDEO_ES;
705 if( !p_input->stream.b_seekable )
706 input_SelectES( p_input, p_es );
709 else if( (i_id & 0xE0) == 0xC0 )
712 p_es->i_type = MPEG2_AUDIO_ES;
715 if( !p_input->stream.b_seekable )
716 if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
717 == (p_es->i_id & 0x1F) )
718 switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
721 main_PutIntVariable( INPUT_CHANNEL_VAR,
724 input_SelectES( p_input, p_es );
728 else if( (i_id & 0xF0FF) == 0x80BD )
730 /* AC3 audio (0x80->0x8F) */
731 p_es->i_type = AC3_AUDIO_ES;
734 if( !p_input->stream.b_seekable )
735 if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
736 == ((p_es->i_id & 0xF00) >> 8) )
737 switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
740 main_PutIntVariable( INPUT_CHANNEL_VAR,
743 input_SelectES( p_input, p_es );
747 else if( (i_id & 0xE0FF) == 0x20BD )
749 /* Subtitles video (0x20->0x3F) */
750 p_es->i_type = DVD_SPU_ES;
753 if( main_GetIntVariable( INPUT_SUBTITLE_VAR, -1 )
754 == ((p_es->i_id & 0x1F00) >> 8) )
756 if( !p_input->stream.b_seekable )
757 input_SelectES( p_input, p_es );
761 else if( (i_id & 0xF0FF) == 0xA0BD )
763 /* LPCM audio (0xA0->0xAF) */
764 p_es->i_type = LPCM_AUDIO_ES;
766 /* FIXME : write the decoder */
770 p_es->i_type = UNKNOWN_ES;
774 } /* stream.b_is_ok */
775 vlc_mutex_unlock( &p_input->stream.stream_lock );
776 } /* i_code > 0xBC */
781 /*****************************************************************************
782 * input_DemuxPS: first step of demultiplexing: the PS header
783 *****************************************************************************/
784 void input_DemuxPS( input_thread_t * p_input, data_packet_t * p_data )
787 boolean_t b_trash = 0;
788 es_descriptor_t * p_es = NULL;
790 i_code = U32_AT( p_data->p_payload_start );
791 if( i_code <= 0x1BC )
795 case 0x1BA: /* PACK_START_CODE */
801 if( (p_data->p_payload_start[4] & 0xC0) == 0x40 )
806 p_byte = p_data->p_payload_start;
808 if( MoveChunk( p_header, &p_data, &p_byte, 14 ) != 14 )
810 intf_WarnMsg( 3, "Packet too short to have a header" );
815 ((mtime_t)(p_header[4] & 0x38) << 27) |
816 ((mtime_t)(U32_AT(p_header + 4) & 0x03FFF800)
818 ((( ((mtime_t)U16_AT(p_header + 6) << 16)
819 | (mtime_t)U16_AT(p_header + 8) ) & 0x03FFF800)
823 i_mux_rate = ((u32)U16_AT(p_header + 10) << 6)
824 | (p_header[12] >> 2);
828 /* MPEG-1 SCR is like PTS. */
831 p_byte = p_data->p_payload_start;
833 if( MoveChunk( p_header, &p_data, &p_byte, 12 ) != 12 )
835 intf_WarnMsg( 3, "Packet too short to have a header" );
840 ((mtime_t)(p_header[4] & 0x0E) << 29) |
841 (((mtime_t)U32_AT(p_header + 4) & 0xFFFE00) << 6) |
842 ((mtime_t)p_header[7] << 7) |
843 ((mtime_t)p_header[8] >> 1);
846 i_mux_rate = (U32_AT(p_header + 8) & 0x7FFFFE) >> 1;
848 /* Call the pace control. */
849 input_ClockManageRef( p_input, p_input->stream.pp_programs[0],
852 if( i_mux_rate != p_input->stream.i_mux_rate
853 && p_input->stream.i_mux_rate )
856 "Mux_rate changed - expect cosmetic errors");
858 p_input->stream.i_mux_rate = i_mux_rate;
864 case 0x1BB: /* SYSTEM_START_CODE */
865 b_trash = 1; /* Nothing interesting */
868 case 0x1BC: /* PROGRAM_STREAM_MAP_CODE */
869 DecodePSM( p_input, p_data );
873 case 0x1B9: /* PROGRAM_END_CODE */
878 /* This should not happen */
880 intf_WarnMsg( 1, "Unwanted packet received with start code 0x%.8x",
886 p_es = input_ParsePS( p_input, p_data );
888 vlc_mutex_lock( &p_input->stream.control.control_lock );
889 if( p_es != NULL && p_es->p_decoder_fifo != NULL
890 && (!p_es->b_audio || !p_input->stream.control.b_mute) )
892 vlc_mutex_unlock( &p_input->stream.control.control_lock );
896 input_GatherPES( p_input, p_data, p_es, 1, 0 );
900 vlc_mutex_unlock( &p_input->stream.control.control_lock );
905 /* Trash the packet if it has no payload or if it isn't selected */
908 p_input->pf_delete_packet( p_input->p_method_data, p_data );
910 p_input->c_packets_trashed++;
920 /*****************************************************************************
921 * input_DemuxTS: first step of demultiplexing: the TS header
922 *****************************************************************************/
923 void input_DemuxTS( input_thread_t * p_input, data_packet_t * p_data )
927 boolean_t b_adaptation; /* Adaptation field is present */
928 boolean_t b_payload; /* Packet carries payload */
929 boolean_t b_unit_start; /* A PSI or a PES start in the packet */
930 boolean_t b_trash = 0; /* Is the packet unuseful ? */
931 boolean_t b_lost = 0; /* Was there a packet loss ? */
932 boolean_t b_psi = 0; /* Is this a PSI ? */
933 es_descriptor_t * p_es = NULL;
934 es_ts_data_t * p_es_demux = NULL;
935 pgrm_ts_data_t * p_pgrm_demux = NULL;
937 #define p (p_data->p_buffer)
938 /* Extract flags values from TS common header. */
939 i_pid = U16_AT(&p[1]) & 0x1fff;
940 b_unit_start = (p[1] & 0x40);
941 b_adaptation = (p[3] & 0x20);
942 b_payload = (p[3] & 0x10);
944 /* Find out the elementary stream. */
945 vlc_mutex_lock( &p_input->stream.stream_lock );
947 p_es= input_FindES( p_input, i_pid );
949 if( (p_es != NULL) && (p_es->p_demux_data != NULL) )
951 p_es_demux = (es_ts_data_t *)p_es->p_demux_data;
953 if( p_es_demux->b_psi )
956 p_pgrm_demux = (pgrm_ts_data_t *)p_es->p_pgrm->p_demux_data;
959 vlc_mutex_lock( &p_input->stream.control.control_lock );
960 if( ( p_es == NULL ) || (p_es->b_audio && p_input->stream.control.b_mute) )
962 /* Not selected. Just read the adaptation field for a PCR. */
965 else if( p_es->p_decoder_fifo == NULL && !b_psi )
968 vlc_mutex_unlock( &p_input->stream.control.control_lock );
969 vlc_mutex_unlock( &p_input->stream.stream_lock );
972 /* Don't change the order of the tests : if b_psi then p_pgrm_demux
973 * may still be null. Who said it was ugly ? */
974 if( ( p_es != NULL ) &&
975 ((p_es->p_decoder_fifo != NULL) || b_psi
976 || (p_pgrm_demux->i_pcr_pid == i_pid) ) )
982 /* Extract adaptation field information if any */
986 /* We don't have any adaptation_field, so payload starts
987 * immediately after the 4 byte TS header */
988 p_data->p_payload_start += 4;
992 /* p[4] is adaptation_field_length minus one */
993 p_data->p_payload_start += 5 + p[4];
995 /* The adaptation field can be limited to the
996 * adaptation_field_length byte, so that there is nothing to do:
997 * skip this possibility */
1000 /* If the packet has both adaptation_field and payload,
1001 * adaptation_field cannot be more than 182 bytes long; if
1002 * there is only an adaptation_field, it must fill the next
1004 if( b_payload ? (p[4] > 182) : (p[4] != 183) )
1007 "invalid TS adaptation field (%p)",
1009 p_data->b_discard_payload = 1;
1011 p_es->c_invalid_packets++;
1015 /* Now we are sure that the byte containing flags is present:
1019 /* discontinuity_indicator */
1023 "discontinuity_indicator"
1024 " encountered by TS demux (position read: %d,"
1026 p[5] & 0x80, p_es_demux->i_continuity_counter );
1028 /* If the PID carries the PCR, there will be a system
1029 * time-based discontinuity. We let the PCR decoder
1031 p_es->p_pgrm->i_synchro_state = SYNCHRO_REINIT;
1033 /* There also may be a continuity_counter
1034 * discontinuity: resynchronise our counter with
1035 * the one of the stream. */
1036 p_es_demux->i_continuity_counter = (p[3] & 0x0f) - 1;
1039 /* If this is a PCR_PID, and this TS packet contains a
1040 * PCR, we pass it along to the PCR decoder. */
1042 if( (p_pgrm_demux->i_pcr_pid == i_pid) && (p[5] & 0x10) )
1044 /* There should be a PCR field in the packet, check
1045 * if the adaptation field is long enough to carry
1052 ( (mtime_t)U32_AT((u32*)&p[6]) << 1 )
1054 /* Call the pace control. */
1055 input_ClockManageRef( p_input, p_es->p_pgrm,
1059 } /* valid TS adaptation field ? */
1061 } /* has adaptation field */
1062 /* Check the continuity of the stream. */
1063 i_dummy = ((p[3] & 0x0f) - p_es_demux->i_continuity_counter) & 0x0f;
1066 /* Everything is ok, just increase our counter */
1067 (p_es_demux->i_continuity_counter)++;
1071 if( !b_payload && i_dummy == 0 )
1073 /* This is a packet without payload, this is allowed by the
1074 * draft. As there is nothing interesting in this packet
1075 * (except PCR that have already been handled), we can trash
1078 "Packet without payload received by TS demux" );
1081 else if( i_dummy <= 0 )
1083 /* FIXME: this can never happen, can it ? --Meuuh */
1084 /* Duplicate packet: mark it as being to be trashed. */
1085 intf_WarnMsg( 1, "Duplicate packet received by TS demux" );
1088 else if( p_es_demux->i_continuity_counter == 0xFF )
1090 /* This means that the packet is the first one we receive for
1091 * this ES since the continuity counter ranges between 0 and
1092 * 0x0F excepts when it has been initialized by the input:
1093 * init the counter to the correct value. */
1094 intf_DbgMsg( "First packet for PID %d received by TS demux",
1096 p_es_demux->i_continuity_counter = (p[3] & 0x0f);
1100 /* This can indicate that we missed a packet or that the
1101 * continuity_counter wrapped and we received a dup packet:
1102 * as we don't know, do as if we missed a packet to be sure
1103 * to recover from this situation */
1105 "Packet lost by TS demux: current %d, packet %d",
1106 p_es_demux->i_continuity_counter & 0x0f,
1109 p_es_demux->i_continuity_counter = p[3] & 0x0f;
1110 } /* not continuous */
1112 } /* if selected or PCR */
1114 /* Trash the packet if it has no payload or if it isn't selected */
1117 p_input->pf_delete_packet( p_input->p_method_data, p_data );
1119 p_input->c_packets_trashed++;
1126 /* The payload contains PSI tables */
1127 input_DemuxPSI( p_input, p_data, p_es,
1128 b_unit_start, b_lost );
1133 /* The payload carries a PES stream */
1134 input_GatherPES( p_input, p_data, p_es, b_unit_start, b_lost );
1144 * PSI demultiplexing and decoding
1147 /*****************************************************************************
1148 * DemuxPSI : makes up complete PSI data
1149 *****************************************************************************/
1150 void input_DemuxPSI( input_thread_t * p_input, data_packet_t * p_data,
1151 es_descriptor_t * p_es, boolean_t b_unit_start, boolean_t b_lost )
1153 es_ts_data_t * p_demux_data;
1155 p_demux_data = (es_ts_data_t *)p_es->p_demux_data;
1157 #define p_psi (p_demux_data->p_psi_section)
1158 #define p (p_data->p_payload_start)
1162 /* unit_start set to 1 -> presence of a pointer field
1163 * (see ISO/IEC 13818 (2.4.4.2) which should be set to 0x00 */
1164 if( (u8)p[0] != 0x00 )
1166 /* intf_WarnMsg( 2, */
1167 intf_ErrMsg( "Non zero pointer field found. Trying to continue" );
1173 /* This is the begining of a new section */
1175 if( ((u8)(p[1]) & 0xc0) != 0x80 )
1177 intf_ErrMsg( "Invalid PSI packet" );
1182 p_psi->i_section_length = U16_AT(p+1) & 0x0fff;
1183 p_psi->b_section_complete = 0;
1184 p_psi->i_read_in_section = 0;
1185 p_psi->i_section_number = (u8)p[6];
1187 if( p_psi->b_is_complete || p_psi->i_section_number == 0 )
1189 /* This is a new PSI packet */
1190 p_psi->b_is_complete = 0;
1192 p_psi->i_version_number = ( p[5] >> 1 ) & 0x1f;
1193 p_psi->i_last_section_number = (u8)p[7];
1195 /* We'll write at the begining of the buffer */
1196 p_psi->p_current = p_psi->buffer;
1200 if( p_psi->b_section_complete )
1202 /* New Section of an already started PSI */
1203 p_psi->b_section_complete = 0;
1205 if( p_psi->i_version_number != (( p[5] >> 1 ) & 0x1f) )
1207 intf_WarnMsg( 2,"PSI version differs inside same PAT" );
1210 if( p_psi->i_section_number + 1 != (u8)p[6] )
1213 "PSI Section discontinuity. Packet lost ?");
1217 p_psi->i_section_number++;
1221 intf_WarnMsg( 2, "Received unexpected new PSI section" );
1226 } /* b_unit_start */
1228 if( !p_psi->b_trash )
1231 if( (p_data->p_payload_end - p) >=
1232 ( p_psi->i_section_length - p_psi->i_read_in_section ) )
1234 /* The end of the section is in this TS packet */
1235 memcpy( p_psi->p_current, p,
1236 (p_psi->i_section_length - p_psi->i_read_in_section) );
1238 p_psi->b_section_complete = 1;
1240 (p_psi->i_section_length - p_psi->i_read_in_section);
1242 if( p_psi->i_section_number == p_psi->i_last_section_number )
1244 /* This was the last section of PSI */
1245 p_psi->b_is_complete = 1;
1250 memcpy( p_psi->buffer, p, p_data->p_payload_end - p );
1251 p_psi->i_read_in_section+= p_data->p_payload_end - p;
1253 p_psi->p_current += p_data->p_payload_end - p;
1257 if ( p_psi->b_is_complete )
1259 switch( p_demux_data->i_psi_type)
1262 input_DecodePAT( p_input, p_es );
1265 input_DecodePMT( p_input, p_es );
1268 intf_ErrMsg("Received unknown PSI in demuxPSI");
1277 /*****************************************************************************
1278 * DecodePAT : Decodes Programm association table and deal with it
1279 *****************************************************************************/
1280 static void input_DecodePAT( input_thread_t * p_input, es_descriptor_t * p_es )
1283 stream_ts_data_t * p_stream_data;
1284 es_ts_data_t * p_demux_data;
1286 p_demux_data = (es_ts_data_t *)p_es->p_demux_data;
1287 p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
1289 #define p_psi (p_demux_data->p_psi_section)
1291 if( p_stream_data->i_pat_version != p_psi->i_version_number )
1293 /* PAT has changed. We are going to delete all programms and
1294 * create new ones. We chose not to only change what was needed
1295 * as a PAT change may mean the stream is radically changing and
1296 * this is a secure method to avoid krashed */
1297 pgrm_descriptor_t * p_pgrm;
1298 es_descriptor_t * p_current_es;
1299 es_ts_data_t * p_es_demux;
1300 pgrm_ts_data_t * p_pgrm_demux;
1301 byte_t * p_current_data;
1303 int i_section_length,i_program_id,i_pmt_pid;
1304 int i_loop, i_current_section;
1306 p_current_data = p_psi->buffer;
1309 for( i_loop = 0; i_loop < p_input->stream.i_pgrm_number; i_loop++ )
1311 input_DelProgram( p_input, p_input->stream.pp_programs[i_loop] );
1316 i_section_length = U16_AT(p_current_data+1) & 0x0fff;
1317 i_current_section = (u8)p_current_data[6];
1319 for( i_loop = 0; i_loop < (i_section_length-9)/4 ; i_loop++ )
1321 i_program_id = U16_AT(p_current_data + i_loop*4 + 8);
1322 i_pmt_pid = U16_AT( p_current_data + i_loop*4 + 10) & 0x1fff;
1324 /* If program = 0, we're having info about NIT not PMT */
1327 /* Add this program */
1328 p_pgrm = input_AddProgram( p_input, i_program_id,
1329 sizeof( pgrm_ts_data_t ) );
1331 /* whatis the PID of the PMT of this program */
1332 p_pgrm_demux = (pgrm_ts_data_t *)p_pgrm->p_demux_data;
1333 p_pgrm_demux->i_pmt_version = PMT_UNINITIALIZED;
1335 /* Add the PMT ES to this program */
1336 p_current_es = input_AddES( p_input, p_pgrm,(u16)i_pmt_pid,
1337 sizeof( es_ts_data_t) );
1338 p_es_demux = (es_ts_data_t *)p_current_es->p_demux_data;
1339 p_es_demux->b_psi = 1;
1340 p_es_demux->i_psi_type = PSI_IS_PMT;
1342 p_es_demux->p_psi_section =
1343 malloc( sizeof( psi_section_t ) );
1344 p_es_demux->p_psi_section->b_is_complete = 0;
1348 p_current_data+=3+i_section_length;
1350 } while( i_current_section < p_psi->i_last_section_number );
1352 /* Go to the beginning of the next section*/
1353 p_stream_data->i_pat_version = p_psi->i_version_number;
1360 /*****************************************************************************
1361 * DecodePMT : decode a given Program Stream Map
1362 * ***************************************************************************
1363 * When the PMT changes, it may mean a deep change in the stream, and it is
1364 * careful to deletes the ES and add them again. If the PMT doesn't change,
1365 * there no need to do anything.
1366 *****************************************************************************/
1367 static void input_DecodePMT( input_thread_t * p_input, es_descriptor_t * p_es )
1370 pgrm_ts_data_t * p_pgrm_data;
1371 es_ts_data_t * p_demux_data;
1373 p_demux_data = (es_ts_data_t *)p_es->p_demux_data;
1374 p_pgrm_data = (pgrm_ts_data_t *)p_es->p_pgrm->p_demux_data;
1376 #define p_psi (p_demux_data->p_psi_section)
1378 if( p_psi->i_version_number != p_pgrm_data->i_pmt_version )
1380 es_descriptor_t * p_new_es;
1381 es_ts_data_t * p_es_demux;
1382 byte_t * p_current_data, * p_current_section;
1383 int i_section_length,i_current_section;
1384 int i_prog_info_length, i_loop;
1385 int i_es_info_length, i_pid, i_stream_type;
1387 p_current_section = p_psi->buffer;
1388 p_current_data = p_psi->buffer;
1390 p_pgrm_data->i_pcr_pid = U16_AT(p_current_section + 8) & 0x1fff;
1392 /* Lock stream information */
1393 vlc_mutex_lock( &p_input->stream.stream_lock );
1395 /* Delete all ES in this program except the PSI */
1396 for( i_loop=0; i_loop < p_es->p_pgrm->i_es_number; i_loop++ )
1398 p_es_demux = (es_ts_data_t *)
1399 p_es->p_pgrm->pp_es[i_loop]->p_demux_data;
1400 if ( ! p_es_demux->b_psi )
1401 input_DelES( p_input, p_es->p_pgrm->pp_es[i_loop] );
1404 /* Then add what we received in this PMT */
1408 i_section_length = U16_AT(p_current_data+1) & 0x0fff;
1409 i_current_section = (u8)p_current_data[6];
1410 i_prog_info_length = U16_AT(p_current_data+10) & 0x0fff;
1412 /* For the moment we ignore program descriptors */
1413 p_current_data += 12+i_prog_info_length;
1415 /* The end of the section, before the CRC is at
1416 * p_current_section + i_section_length -1 */
1417 while( p_current_data < p_current_section + i_section_length -1 )
1419 i_stream_type = (int)p_current_data[0];
1420 i_pid = U16_AT( p_current_data + 1 ) & 0x1fff;
1421 i_es_info_length = U16_AT( p_current_data + 3 ) & 0x0fff;
1423 /* Add this ES to the program */
1424 p_new_es = input_AddES( p_input, p_es->p_pgrm,
1425 (u16)i_pid, sizeof( es_ts_data_t ) );
1426 p_new_es->i_type = i_stream_type;
1428 /* We want to decode */
1429 input_SelectES( p_input, p_new_es );
1430 p_current_data += 5 + i_es_info_length;
1433 /* Go to the beginning of the next section*/
1434 p_current_data += 3+i_section_length;
1436 p_current_section+=1;
1438 } while( i_current_section < p_psi->i_last_section_number );
1440 p_pgrm_data->i_pmt_version = p_psi->i_version_number;
1447 vlc_mutex_unlock( &p_input->stream.stream_lock );