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.39 2001/03/02 03:32:46 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 *****************************************************************************/
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 * This is a variation on the theme of input_ext-dec.h:GetChunk().
68 *****************************************************************************/
69 static __inline__ size_t MoveChunk( byte_t * p_dest,
70 data_packet_t ** pp_data_src,
74 ptrdiff_t i_available;
76 if( (i_available = (*pp_data_src)->p_payload_end - *pp_src)
80 memcpy( p_dest, *pp_src, i_buf_len );
86 size_t i_init_len = i_buf_len;
91 memcpy( p_dest, *pp_src, i_available );
92 *pp_data_src = (*pp_data_src)->p_next;
93 i_buf_len -= i_available;
94 p_dest += i_available;
95 if( *pp_data_src == NULL )
98 return( i_init_len - i_buf_len );
100 *pp_src = (*pp_data_src)->p_payload_start;
102 while( (i_available = (*pp_data_src)->p_payload_end - *pp_src)
108 memcpy( p_dest, *pp_src, i_buf_len );
109 *pp_src += i_buf_len;
111 return( i_init_len );
115 /*****************************************************************************
117 *****************************************************************************
118 * Parse a finished PES packet and analyze its header.
119 *****************************************************************************/
120 #define PES_HEADER_SIZE 7
121 void input_ParsePES( input_thread_t * p_input, es_descriptor_t * p_es )
123 data_packet_t * p_data;
125 byte_t p_header[PES_HEADER_SIZE];
128 #define p_pes (p_es->p_pes)
130 //intf_DbgMsg("End of PES packet %p", p_pes);
132 /* Parse the header. The header has a variable length, but in order
133 * to improve the algorithm, we will read the 14 bytes we may be
135 p_data = p_pes->p_first;
136 p_byte = p_data->p_payload_start;
139 if( MoveChunk( p_header, &p_data, &p_byte, PES_HEADER_SIZE )
142 intf_WarnMsg( 3, "PES packet too short to have a header" );
143 p_input->pf_delete_pes( p_input->p_method_data, p_pes );
148 /* Get the PES size if defined */
149 p_es->i_pes_real_size = U16_AT(p_header + 4) + 6;
151 /* First read the 6 header bytes common to all PES packets:
152 * use them to test the PES validity */
153 if( (p_header[0] || p_header[1] || (p_header[2] != 1)) )
155 /* packet_start_code_prefix != 0x000001 */
156 intf_ErrMsg( "PES packet doesn't start with 0x000001 : data loss" );
157 p_input->pf_delete_pes( p_input->p_method_data, p_pes );
162 int i_pes_header_size, i_payload_size;
164 if ( p_es->i_pes_real_size &&
165 (p_es->i_pes_real_size != p_pes->i_pes_size) )
167 /* PES_packet_length is set and != total received payload */
168 /* Warn the decoder that the data may be corrupt. */
169 intf_WarnMsg( 3, "PES sizes do not match : packet corrupted" );
172 switch( p_es->i_stream_id )
174 case 0xBC: /* Program stream map */
175 case 0xBE: /* Padding */
176 case 0xBF: /* Private stream 2 */
179 case 0xFF: /* Program stream directory */
180 case 0xF2: /* DSMCC stream */
181 case 0xF8: /* ITU-T H.222.1 type E stream */
182 /* The payload begins immediately after the 6 bytes header, so
183 * we have finished with the parsing */
184 i_pes_header_size = 6;
188 if( (p_header[6] & 0xC0) == 0x80 )
190 /* MPEG-2 : the PES header contains at least 3 more bytes. */
192 boolean_t b_has_pts, b_has_dts;
193 byte_t p_full_header[12];
195 p_pes->b_data_alignment = p_header[6] & 0x04;
197 i_max_len = MoveChunk( p_full_header, &p_data, &p_byte, 12 );
201 "PES packet too short to have a MPEG-2 header" );
202 p_input->pf_delete_pes( p_input->p_method_data,
208 b_has_pts = p_full_header[0] & 0x80;
209 b_has_dts = p_full_header[0] & 0x40;
210 i_pes_header_size = p_full_header[1] + 9;
212 /* Now parse the optional header extensions */
218 "PES packet too short to have a MPEG-2 header" );
219 p_input->pf_delete_pes( p_input->p_method_data,
224 p_pes->i_pts = input_ClockGetTS( p_input, p_es->p_pgrm,
225 ( ((mtime_t)(p_full_header[2] & 0x0E) << 29) |
226 (((mtime_t)U16_AT(p_full_header + 3) << 14) - (1 << 14)) |
227 ((mtime_t)U16_AT(p_full_header + 5) >> 1) ) );
234 "PES packet too short to have a MPEG-2 header" );
235 p_input->pf_delete_pes( p_input->p_method_data,
240 p_pes->i_dts = input_ClockGetTS( p_input, p_es->p_pgrm,
241 ( ((mtime_t)(p_full_header[7] & 0x0E) << 29) |
242 (((mtime_t)U16_AT(p_full_header + 8) << 14)
244 ((mtime_t)U16_AT(p_full_header + 10) >> 1) ) );
250 /* Probably MPEG-1 */
251 boolean_t b_has_pts, b_has_dts;
253 i_pes_header_size = 6;
254 p_data = p_pes->p_first;
255 p_byte = p_data->p_payload_start;
256 /* Cannot fail because the previous one succeeded. */
257 MoveChunk( NULL, &p_data, &p_byte, 6 );
259 while( *p_byte == 0xFF && i_pes_header_size < 22 )
262 if( MoveChunk( NULL, &p_data, &p_byte, 1 ) != 1 )
265 "PES packet too short to have a MPEG-1 header" );
266 p_input->pf_delete_pes( p_input->p_method_data, p_pes );
271 if( i_pes_header_size == 22 )
273 intf_ErrMsg( "Too much MPEG-1 stuffing" );
274 p_input->pf_delete_pes( p_input->p_method_data, p_pes );
279 if( (*p_byte & 0xC0) == 0x40 )
281 /* Don't ask why... --Meuuh */
282 /* Erm... why ? --Sam */
283 /* Well... According to the recommendation, it is for
284 * STD_buffer_scale and STD_buffer_size. --Meuuh */
285 i_pes_header_size += 2;
286 if( MoveChunk( NULL, &p_data, &p_byte, 2 ) != 2 )
289 "PES packet too short to have a MPEG-1 header" );
290 p_input->pf_delete_pes( p_input->p_method_data, p_pes );
298 b_has_pts = *p_byte & 0x20;
299 b_has_dts = *p_byte & 0x10;
305 i_pes_header_size += 4;
306 if( MoveChunk( p_ts, &p_data, &p_byte, 5 ) != 5 )
309 "PES packet too short to have a MPEG-1 header" );
310 p_input->pf_delete_pes( p_input->p_method_data, p_pes );
315 p_pes->i_pts = input_ClockGetTS( p_input, p_es->p_pgrm,
316 ( ((mtime_t)(p_ts[0] & 0x0E) << 29) |
317 (((mtime_t)U16_AT(p_ts + 1) << 14) - (1 << 14)) |
318 ((mtime_t)U16_AT(p_ts + 3) >> 1) ) );
322 i_pes_header_size += 5;
323 if( MoveChunk( p_ts, &p_data, &p_byte, 5 ) != 5 )
326 "PES packet too short to have a MPEG-1 header" );
327 p_input->pf_delete_pes( p_input->p_method_data,
333 p_pes->i_dts = input_ClockGetTS( p_input,
335 ( ((mtime_t)(p_ts[0] & 0x0E) << 29) |
336 (((mtime_t)U16_AT(p_ts + 1) << 14) - (1 << 14)) |
337 ((mtime_t)U16_AT(p_ts + 3) >> 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( "PES header bigger than payload" );
368 p_input->pf_delete_pes( 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( "PES header bigger than payload" );
379 p_input->pf_delete_pes( 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("No fifo to receive PES %p (who wrote this damn code ?)",
395 p_input->pf_delete_pes( 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 //intf_DbgMsg("PES-demultiplexing %p (%p)", p_ts_packet, p_pes);
416 /* If we lost data, insert a NULL data packet (philosophy : 0 is quite
417 * often an escape sequence in decoders, so that should make them wait
418 * for the next start code). */
421 input_NullPacket( p_input, p_es );
424 if( b_unit_start && p_pes != NULL )
426 /* If the data packet contains the begining of a new PES packet, and
427 * if we were reassembling a PES packet, then the PES should be
428 * complete now, so parse its header and give it to the decoders. */
429 input_ParsePES( p_input, p_es );
432 if( !b_unit_start && p_pes == NULL )
434 /* Random access... */
435 p_input->pf_delete_packet( p_input->p_method_data, p_data );
441 /* If we are at the beginning of a new PES packet, we must fetch
442 * a new PES buffer to begin with the reassembly of this PES
443 * packet. This is also here that we can synchronize with the
444 * stream if we lost packets or if the decoder has just
446 if( (p_pes = p_input->pf_new_pes( p_input->p_method_data ) ) == NULL )
448 intf_ErrMsg("Out of memory");
449 p_input->b_error = 1;
452 p_pes->i_rate = p_input->stream.control.i_rate;
453 p_pes->p_first = p_data;
455 /* If the PES header fits in the first data packet, we can
456 * already set p_gather->i_pes_real_size. */
457 if( p_data->p_payload_end - p_data->p_payload_start
460 p_es->i_pes_real_size =
461 U16_AT(p_data->p_payload_start + 4) + 6;
466 p_es->i_pes_real_size = 0;
471 /* Update the relations between the data packets */
472 p_es->p_last->p_next = p_data;
475 p_es->p_last = p_data;
477 /* Size of the payload carried in the data packet */
478 p_pes->i_pes_size += (p_data->p_payload_end
479 - p_data->p_payload_start);
481 /* We can check if the packet is finished */
482 if( p_pes->i_pes_size == p_es->i_pes_real_size )
484 /* The packet is finished, parse it */
485 input_ParsePES( p_input, p_es );
496 /*****************************************************************************
497 * GetID: Get the ID of a stream
498 *****************************************************************************/
499 static u16 GetID( data_packet_t * p_data )
503 i_id = p_data->p_payload_start[3]; /* stream_id */
506 /* stream_private_id */
507 i_id |= p_data->p_payload_start[ 9 + p_data->p_payload_start[8] ] << 8;
512 /*****************************************************************************
513 * DecodePSM: Decode the Program Stream Map information
514 *****************************************************************************/
515 static void DecodePSM( input_thread_t * p_input, data_packet_t * p_data )
517 stream_ps_data_t * p_demux =
518 (stream_ps_data_t *)p_input->stream.p_demux_data;
522 int i_new_es_number = 0;
524 if( p_data->p_payload_start + 10 > p_data->p_payload_end )
526 intf_ErrMsg( "PSM too short : packet corrupt" );
530 if( p_demux->b_has_PSM
531 && p_demux->i_PSM_version == (p_data->p_payload_start[6] & 0x1F) )
533 /* Already got that one. */
537 intf_DbgMsg( "Building PSM" );
538 p_demux->b_has_PSM = 1;
539 p_demux->i_PSM_version = p_data->p_payload_start[6] & 0x1F;
541 /* Go to elementary_stream_map_length, jumping over
542 * program_stream_info. */
543 p_byte = p_data->p_payload_start + 10
544 + U16_AT(&p_data->p_payload_start[8]);
545 if( p_byte > p_data->p_payload_end )
547 intf_ErrMsg( "PSM too short : packet corrupt" );
550 /* This is the full size of the elementary_stream_map.
551 * 2 == elementary_stream_map_length
552 * Please note that CRC_32 is not included in the length. */
553 p_end = p_byte + 2 + U16_AT(p_byte);
555 if( p_end > p_data->p_payload_end )
557 intf_ErrMsg( "PSM too short : packet corrupt" );
561 vlc_mutex_lock( &p_input->stream.stream_lock );
563 /* 4 == minimum useful size of a section */
564 while( p_byte + 4 <= p_end )
566 es_descriptor_t * p_es = NULL;
567 u8 i_stream_id = p_byte[1];
568 /* FIXME: there will be a problem with private streams... (same
571 /* Look for the ES in the ES table */
572 for( i = i_new_es_number;
573 i < p_input->stream.pp_programs[0]->i_es_number;
576 if( p_input->stream.pp_programs[0]->pp_es[i]->i_stream_id
579 p_es = p_input->stream.pp_programs[0]->pp_es[i];
580 if( p_es->i_type != p_byte[0] )
582 input_DelES( p_input, p_es );
587 /* Move the ES to the beginning. */
588 p_input->stream.pp_programs[0]->pp_es[i]
589 = p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ];
590 p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ]
598 /* The goal is to have all the ES we have just read in the
599 * beginning of the pp_es table, and all the others at the end,
600 * so that we can close them more easily at the end. */
603 p_es = input_AddES( p_input, p_input->stream.pp_programs[0],
605 p_es->i_type = p_byte[0];
606 p_es->b_audio = ( p_es->i_type == MPEG1_AUDIO_ES
607 || p_es->i_type == MPEG2_AUDIO_ES
608 || p_es->i_type == AC3_AUDIO_ES
609 || p_es->i_type == LPCM_AUDIO_ES
612 /* input_AddES has inserted the new element at the end. */
613 p_input->stream.pp_programs[0]->pp_es[
614 p_input->stream.pp_programs[0]->i_es_number ]
615 = p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ];
616 p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ] = p_es;
619 p_byte += 4 + U16_AT(&p_byte[2]);
622 /* Un-select the streams that are no longer parts of the program. */
623 for( i = i_new_es_number;
624 i < 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] );
634 intf_Msg( "input info: The stream map after the PSM is now :" );
635 input_DumpStream( p_input );
638 vlc_mutex_unlock( &p_input->stream.stream_lock );
641 /*****************************************************************************
642 * input_ParsePS: read the PS header
643 *****************************************************************************/
644 es_descriptor_t * input_ParsePS( input_thread_t * p_input,
645 data_packet_t * p_data )
648 es_descriptor_t * p_es = NULL;
650 i_code = U32_AT( p_data->p_payload_start );
651 if( i_code > 0x1BC ) /* ES start code */
656 /* This is a PES packet. Find out if we want it or not. */
657 i_id = GetID( p_data );
659 vlc_mutex_lock( &p_input->stream.stream_lock );
660 if( p_input->stream.pp_programs[0]->b_is_ok )
662 /* Look only at the selected ES. */
663 for( i_dummy = 0; i_dummy < p_input->stream.i_selected_es_number;
666 if( p_input->stream.pp_selected_es[i_dummy] != NULL
667 && p_input->stream.pp_selected_es[i_dummy]->i_id == i_id )
669 p_es = p_input->stream.pp_selected_es[i_dummy];
676 stream_ps_data_t * p_demux =
677 (stream_ps_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
679 /* Search all ES ; if not found -> AddES */
680 p_es = input_FindES( p_input, i_id );
682 if( p_es == NULL && !p_demux->b_has_PSM )
684 p_es = input_AddES( p_input, p_input->stream.pp_programs[0],
688 p_es->i_stream_id = p_data->p_payload_start[3];
690 /* Set stream type and auto-spawn. */
691 if( (i_id & 0xF0) == 0xE0 )
694 p_es->i_type = MPEG2_VIDEO_ES;
696 if( !p_input->stream.b_seekable )
697 input_SelectES( p_input, p_es );
700 else if( (i_id & 0xE0) == 0xC0 )
703 p_es->i_type = MPEG2_AUDIO_ES;
706 if( !p_input->stream.b_seekable )
707 if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
708 == (p_es->i_id & 0x1F) )
709 switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
712 main_PutIntVariable( INPUT_CHANNEL_VAR,
715 input_SelectES( p_input, p_es );
719 else if( (i_id & 0xF0FF) == 0x80BD )
721 /* AC3 audio (0x80->0x8F) */
722 p_es->i_type = AC3_AUDIO_ES;
725 if( !p_input->stream.b_seekable )
726 if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
727 == ((p_es->i_id & 0xF00) >> 8) )
728 switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
731 main_PutIntVariable( INPUT_CHANNEL_VAR,
734 input_SelectES( p_input, p_es );
738 else if( (i_id & 0xE0FF) == 0x20BD )
740 /* Subtitles video (0x20->0x3F) */
741 p_es->i_type = DVD_SPU_ES;
743 if( main_GetIntVariable( INPUT_SUBTITLE_VAR, -1 )
744 == ((p_es->i_id & 0x1F00) >> 8) )
746 if( !p_input->stream.b_seekable )
747 input_SelectES( p_input, p_es );
751 else if( (i_id & 0xF0FF) == 0xA0BD )
753 /* LPCM audio (0xA0->0xAF) */
754 p_es->i_type = LPCM_AUDIO_ES;
756 /* FIXME : write the decoder */
760 p_es->i_type = UNKNOWN_ES;
764 } /* stream.b_is_ok */
765 vlc_mutex_unlock( &p_input->stream.stream_lock );
766 } /* i_code > 0xBC */
771 /*****************************************************************************
772 * input_DemuxPS: first step of demultiplexing: the PS header
773 *****************************************************************************/
774 void input_DemuxPS( input_thread_t * p_input, data_packet_t * p_data )
777 boolean_t b_trash = 0;
778 es_descriptor_t * p_es = NULL;
780 i_code = U32_AT( p_data->p_payload_start );
781 if( i_code <= 0x1BC )
785 case 0x1BA: /* PACK_START_CODE */
791 if( (p_data->p_payload_start[4] & 0xC0) == 0x40 )
795 ((mtime_t)(p_data->p_payload_start[4] & 0x38) << 27) |
796 ((mtime_t)(U32_AT(p_data->p_payload_start + 4) & 0x03FFF800)
798 ((mtime_t)(U32_AT(p_data->p_payload_start + 6) & 0x03FFF800)
802 i_mux_rate = (U32_AT(p_data->p_payload_start + 10) & 0xFFFFFC00);
806 /* MPEG-1 SCR is like PTS. */
808 ((mtime_t)(p_data->p_payload_start[4] & 0x0E) << 29) |
809 (((mtime_t)U16_AT(p_data->p_payload_start + 5) << 14)
811 ((mtime_t)U16_AT(p_data->p_payload_start + 7) >> 1);
814 i_mux_rate = (U32_AT(p_data->p_payload_start + 8) & 0x8FFFFE);
816 /* Call the pace control. */
817 input_ClockManageRef( p_input, p_input->stream.pp_programs[0],
820 if( i_mux_rate != p_input->stream.i_mux_rate
821 && p_input->stream.i_mux_rate )
824 "Mux_rate changed - expect cosmetic errors");
826 p_input->stream.i_mux_rate = i_mux_rate;
832 case 0x1BB: /* SYSTEM_START_CODE */
833 b_trash = 1; /* Nothing interesting */
836 case 0x1BC: /* PROGRAM_STREAM_MAP_CODE */
837 DecodePSM( p_input, p_data );
841 case 0x1B9: /* PROGRAM_END_CODE */
846 /* This should not happen */
848 intf_WarnMsg( 1, "Unwanted packet received with start code %x",
854 p_es = input_ParsePS( p_input, p_data );
856 vlc_mutex_lock( &p_input->stream.control.control_lock );
857 if( p_es != NULL && p_es->p_decoder_fifo != NULL
858 && (!p_es->b_audio || !p_input->stream.control.b_mute) )
860 vlc_mutex_unlock( &p_input->stream.control.control_lock );
864 input_GatherPES( p_input, p_data, p_es, 1, 0 );
868 vlc_mutex_unlock( &p_input->stream.control.control_lock );
873 /* Trash the packet if it has no payload or if it isn't selected */
876 p_input->pf_delete_packet( p_input->p_method_data, p_data );
878 p_input->c_packets_trashed++;
888 /*****************************************************************************
889 * input_DemuxTS: first step of demultiplexing: the TS header
890 *****************************************************************************/
891 void input_DemuxTS( input_thread_t * p_input, data_packet_t * p_data )
895 boolean_t b_adaptation; /* Adaptation field is present */
896 boolean_t b_payload; /* Packet carries payload */
897 boolean_t b_unit_start; /* A PSI or a PES start in the packet */
898 boolean_t b_trash = 0; /* Is the packet unuseful ? */
899 boolean_t b_lost = 0; /* Was there a packet loss ? */
900 boolean_t b_psi = 0; /* Is this a PSI ? */
901 es_descriptor_t * p_es = NULL;
902 es_ts_data_t * p_es_demux = NULL;
903 pgrm_ts_data_t * p_pgrm_demux = NULL;
905 #define p (p_data->p_buffer)
907 /* Extract flags values from TS common header. */
908 i_pid = U16_AT(&p[1]) & 0x1fff;
909 b_unit_start = (p[1] & 0x40);
910 b_adaptation = (p[3] & 0x20);
911 b_payload = (p[3] & 0x10);
913 /* Find out the elementary stream. */
914 vlc_mutex_lock( &p_input->stream.stream_lock );
917 p_es= input_FindES( p_input, i_pid );
919 if( (p_es != NULL) && (p_es->p_demux_data != NULL) )
921 p_es_demux = (es_ts_data_t *)p_es->p_demux_data;
923 if( p_es_demux->b_psi )
927 vlc_mutex_lock( &p_input->stream.control.control_lock );
929 if( ( p_es == NULL ) || (p_es->b_audio && p_input->stream.control.b_mute) )
931 /* Not selected. Just read the adaptation field for a PCR. */
934 else if( p_es->p_decoder_fifo == NULL && !b_psi )
937 vlc_mutex_unlock( &p_input->stream.control.control_lock );
938 vlc_mutex_unlock( &p_input->stream.stream_lock );
941 if( ( p_es != NULL ) &&
942 ((p_es->p_decoder_fifo != NULL) || b_psi
943 || (p_pgrm_demux->i_pcr_pid == i_pid) ) )
945 p_es_demux = (es_ts_data_t *)p_es->p_demux_data;
947 if( ! p_es_demux->b_psi )
949 p_pgrm_demux = (pgrm_ts_data_t *)p_es->p_pgrm->p_demux_data;
956 /* Extract adaptation field information if any */
960 /* We don't have any adaptation_field, so payload starts
961 * immediately after the 4 byte TS header */
962 p_data->p_payload_start += 4;
966 /* p[4] is adaptation_field_length minus one */
967 p_data->p_payload_start += 5 + p[4];
969 /* The adaptation field can be limited to the
970 * adaptation_field_length byte, so that there is nothing to do:
971 * skip this possibility */
974 /* If the packet has both adaptation_field and payload,
975 * adaptation_field cannot be more than 182 bytes long; if
976 * there is only an adaptation_field, it must fill the next
978 if( b_payload ? (p[4] > 182) : (p[4] != 183) )
981 "invalid TS adaptation field (%p)",
983 p_data->b_discard_payload = 1;
985 p_es->c_invalid_packets++;
989 /* Now we are sure that the byte containing flags is present:
993 /* discontinuity_indicator */
997 "discontinuity_indicator"
998 " encountered by TS demux (position read: %d,"
1000 p[5] & 0x80, p_es_demux->i_continuity_counter );
1002 /* If the PID carries the PCR, there will be a system
1003 * time-based discontinuity. We let the PCR decoder
1005 p_es->p_pgrm->i_synchro_state = SYNCHRO_REINIT;
1007 /* There also may be a continuity_counter
1008 * discontinuity: resynchronise our counter with
1009 * the one of the stream. */
1010 p_es_demux->i_continuity_counter = (p[3] & 0x0f) - 1;
1013 /* If this is a PCR_PID, and this TS packet contains a
1014 * PCR, we pass it along to the PCR decoder. */
1016 if( (p_pgrm_demux->i_pcr_pid == i_pid) && (p[5] & 0x10) )
1018 /* There should be a PCR field in the packet, check
1019 * if the adaptation field is long enough to carry
1026 ( (mtime_t)U32_AT((u32*)&p[6]) << 1 )
1028 /* Call the pace control. */
1029 input_ClockManageRef( p_input, p_es->p_pgrm,
1033 } /* valid TS adaptation field ? */
1035 } /* has adaptation field */
1036 /* Check the continuity of the stream. */
1037 i_dummy = ((p[3] & 0x0f) - p_es_demux->i_continuity_counter) & 0x0f;
1040 /* Everything is ok, just increase our counter */
1041 (p_es_demux->i_continuity_counter)++;
1045 if( !b_payload && i_dummy == 0 )
1047 /* This is a packet without payload, this is allowed by the
1048 * draft. As there is nothing interesting in this packet
1049 * (except PCR that have already been handled), we can trash
1052 "Packet without payload received by TS demux" );
1055 else if( i_dummy <= 0 )
1057 /* FIXME: this can never happen, can it ? --Meuuh */
1058 /* Duplicate packet: mark it as being to be trashed. */
1059 intf_WarnMsg( 1, "Duplicate packet received by TS demux" );
1062 else if( p_es_demux->i_continuity_counter == 0xFF )
1064 /* This means that the packet is the first one we receive for
1065 * this ES since the continuity counter ranges between 0 and
1066 * 0x0F excepts when it has been initialized by the input:
1067 * init the counter to the correct value. */
1068 intf_DbgMsg( "First packet for PID %d received by TS demux",
1070 p_es_demux->i_continuity_counter = (p[3] & 0x0f);
1074 /* This can indicate that we missed a packet or that the
1075 * continuity_counter wrapped and we received a dup packet:
1076 * as we don't know, do as if we missed a packet to be sure
1077 * to recover from this situation */
1079 "Packet lost by TS demux: current %d, packet %d",
1080 p_es_demux->i_continuity_counter & 0x0f,
1083 p_es_demux->i_continuity_counter = p[3] & 0x0f;
1084 } /* not continuous */
1086 } /* if selected or PCR */
1088 /* Trash the packet if it has no payload or if it isn't selected */
1091 p_input->pf_delete_packet( p_input->p_method_data, p_data );
1093 p_input->c_packets_trashed++;
1100 /* The payload contains PSI tables */
1101 input_DemuxPSI( p_input, p_data, p_es,
1102 b_unit_start, b_lost );
1107 /* The payload carries a PES stream */
1108 input_GatherPES( p_input, p_data, p_es, b_unit_start, b_lost );
1118 * PSI demultiplexing and decoding
1121 /*****************************************************************************
1122 * DemuxPSI : makes up complete PSI data
1123 *****************************************************************************/
1124 void input_DemuxPSI( input_thread_t * p_input, data_packet_t * p_data,
1125 es_descriptor_t * p_es, boolean_t b_unit_start, boolean_t b_lost )
1127 es_ts_data_t * p_demux_data;
1129 p_demux_data = (es_ts_data_t *)p_es->p_demux_data;
1131 #define p_psi (p_demux_data->p_psi_section)
1132 #define p (p_data->p_payload_start)
1136 /* unit_start set to 1 -> presence of a pointer field
1137 * (see ISO/IEC 13818 (2.4.4.2) which should be set to 0x00 */
1138 if( (u8)p[0] != 0x00 )
1140 /* intf_WarnMsg( 2, */
1141 intf_ErrMsg( "Non zero pointer field found. Trying to continue" );
1147 /* This is the begining of a new section */
1149 if( ((u8)(p[1]) & 0xc0) != 0x80 )
1151 intf_ErrMsg( "Invalid PSI packet" );
1156 p_psi->i_section_length = U16_AT(p+1) & 0x0fff;
1157 p_psi->b_section_complete = 0;
1158 p_psi->i_read_in_section = 0;
1159 p_psi->i_section_number = (u8)p[6];
1161 if( p_psi->b_is_complete || p_psi->i_section_number == 0 )
1163 /* This is a new PSI packet */
1164 p_psi->b_is_complete = 0;
1166 p_psi->i_version_number = ( p[5] >> 1 ) & 0x1f;
1167 p_psi->i_last_section_number = (u8)p[7];
1169 /* We'll write at the begining of the buffer */
1170 p_psi->p_current = p_psi->buffer;
1174 if( p_psi->b_section_complete )
1176 /* New Section of an already started PSI */
1177 p_psi->b_section_complete = 0;
1179 if( p_psi->i_version_number != (( p[5] >> 1 ) & 0x1f) )
1181 intf_WarnMsg( 2,"PSI version differs inside same PAT" );
1184 if( p_psi->i_section_number + 1 != (u8)p[6] )
1187 "PSI Section discontinuity. Packet lost ?");
1191 p_psi->i_section_number++;
1195 intf_WarnMsg( 2, "Received unexpected new PSI section" );
1200 } /* b_unit_start */
1202 if( !p_psi->b_trash )
1205 if( (p_data->p_payload_end - p) >=
1206 ( p_psi->i_section_length - p_psi->i_read_in_section ) )
1208 /* The end of the section is in this TS packet */
1209 memcpy( p_psi->p_current, p,
1210 (p_psi->i_section_length - p_psi->i_read_in_section) );
1212 p_psi->b_section_complete = 1;
1214 (p_psi->i_section_length - p_psi->i_read_in_section);
1216 if( p_psi->i_section_number == p_psi->i_last_section_number )
1218 /* This was the last section of PSI */
1219 p_psi->b_is_complete = 1;
1224 memcpy( p_psi->buffer, p, p_data->p_payload_end - p );
1225 p_psi->i_read_in_section+= p_data->p_payload_end - p;
1227 p_psi->p_current += p_data->p_payload_end - p;
1231 if ( p_psi->b_is_complete )
1233 switch( p_demux_data->i_psi_type)
1236 input_DecodePAT( p_input, p_es );
1239 input_DecodePMT( p_input, p_es );
1242 intf_ErrMsg("Received unknown PSI in demuxPSI");
1251 /*****************************************************************************
1252 * DecodePAT : Decodes Programm association table and deal with it
1253 *****************************************************************************/
1254 static void input_DecodePAT( input_thread_t * p_input, es_descriptor_t * p_es )
1257 stream_ts_data_t * p_stream_data;
1258 es_ts_data_t * p_demux_data;
1261 p_demux_data = (es_ts_data_t *)p_es->p_demux_data;
1262 p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
1264 #define p_psi (p_demux_data->p_psi_section)
1266 if( p_stream_data->i_pat_version != p_psi->i_version_number )
1268 /* PAT has changed. We are going to delete all programms and
1269 * create new ones. We chose not to only change what was needed
1270 * as a PAT change may mean the stream is radically changing and
1271 * this is a secure method to avoid krashed */
1272 pgrm_descriptor_t * p_pgrm;
1273 es_descriptor_t * p_current_es;
1274 es_ts_data_t * p_es_demux;
1275 pgrm_ts_data_t * p_pgrm_demux;
1276 byte_t * p_current_data;
1278 int i_section_length,i_program_id,i_pmt_pid;
1279 int i_loop, i_current_section;
1281 p_current_data = p_psi->buffer;
1284 for( i_loop = 0; i_loop < p_input->stream.i_pgrm_number; i_loop++ )
1286 input_DelProgram( p_input, p_input->stream.pp_programs[i_loop] );
1291 i_section_length = U16_AT(p_current_data+1) & 0x0fff;
1292 i_current_section = (u8)p_current_data[6];
1294 for( i_loop = 0; i_loop < (i_section_length-9)/4 ; i_loop++ )
1296 i_program_id = U16_AT(p_current_data + i_loop*4 + 8);
1297 i_pmt_pid = U16_AT( p_current_data + i_loop*4 + 10) & 0x1fff;
1299 /* If program = 0, we're having info about NIT not PMT */
1302 /* Add this program */
1303 p_pgrm = input_AddProgram( p_input, i_program_id,
1304 sizeof( pgrm_ts_data_t ) );
1306 /* whatis the PID of the PMT of this program */
1307 p_pgrm_demux = (pgrm_ts_data_t *)p_pgrm->p_demux_data;
1308 p_pgrm_demux->i_pmt_version = PMT_UNINITIALIZED;
1310 /* Add the PMT ES to this program */
1311 p_current_es = input_AddES( p_input, p_pgrm,(u16)i_pmt_pid,
1312 sizeof( es_ts_data_t) );
1313 p_es_demux = (es_ts_data_t *)p_current_es->p_demux_data;
1314 p_es_demux->b_psi = 1;
1315 p_es_demux->i_psi_type = PSI_IS_PMT;
1317 p_es_demux->p_psi_section =
1318 malloc( sizeof( psi_section_t ) );
1319 p_es_demux->p_psi_section->b_is_complete = 0;
1323 p_current_data+=3+i_section_length;
1325 } while( i_current_section < p_psi->i_last_section_number );
1327 /* Go to the beginning of the next section*/
1328 p_stream_data->i_pat_version = p_psi->i_version_number;
1335 /*****************************************************************************
1336 * DecodePMT : decode a given Program Stream Map
1337 * ***************************************************************************
1338 * When the PMT changes, it may mean a deep change in the stream, and it is
1339 * careful to deletes the ES and add them again. If the PMT doesn't change,
1340 * there no need to do anything.
1341 *****************************************************************************/
1342 static void input_DecodePMT( input_thread_t * p_input, es_descriptor_t * p_es )
1345 pgrm_ts_data_t * p_pgrm_data;
1346 es_ts_data_t * p_demux_data;
1348 p_demux_data = (es_ts_data_t *)p_es->p_demux_data;
1349 p_pgrm_data = (pgrm_ts_data_t *)p_es->p_pgrm->p_demux_data;
1351 #define p_psi (p_demux_data->p_psi_section)
1353 if( p_psi->i_version_number != p_pgrm_data->i_pmt_version )
1355 es_descriptor_t * p_new_es;
1356 es_ts_data_t * p_es_demux;
1357 byte_t * p_current_data, * p_current_section;
1358 int i_section_length,i_current_section;
1359 int i_prog_info_length, i_loop;
1360 int i_es_info_length, i_pid, i_stream_type;
1362 p_current_section = p_psi->buffer;
1363 p_current_data = p_psi->buffer;
1365 p_pgrm_data->i_pcr_pid = U16_AT(p_current_section + 8) & 0x1fff;
1367 /* Delete all ES in this program except the PSI */
1368 for( i_loop=0; i_loop < p_es->p_pgrm->i_es_number; i_loop++ )
1370 p_es_demux = (es_ts_data_t *)
1371 p_es->p_pgrm->pp_es[i_loop]->p_demux_data;
1372 if ( ! p_es_demux->b_psi )
1373 input_DelES( p_input, p_es->p_pgrm->pp_es[i_loop] );
1376 /* Then add what we received in this PMT */
1380 i_section_length = U16_AT(p_current_data+1) & 0x0fff;
1381 i_current_section = (u8)p_current_data[6];
1382 i_prog_info_length = U16_AT(p_current_data+10) & 0x0fff;
1384 /* For the moment we ignore program descriptors */
1385 p_current_data += 12+i_prog_info_length;
1387 /* The end of the section, before the CRC is at
1388 * p_current_section + i_section_length -1 */
1389 while( p_current_data < p_current_section + i_section_length -1 )
1391 i_stream_type = (int)p_current_data[0];
1392 i_pid = U16_AT( p_current_data + 1 ) & 0x1fff;
1393 i_es_info_length = U16_AT( p_current_data + 3 ) & 0x0fff;
1395 /* Add this ES to the program */
1396 p_new_es = input_AddES( p_input, p_es->p_pgrm,
1397 (u16)i_pid, sizeof( es_ts_data_t ) );
1398 p_new_es->i_type = i_stream_type;
1400 /* We want to decode */
1401 input_SelectES( p_input, p_new_es );
1403 p_current_data += 5 + i_es_info_length;
1406 /* Go to the beginning of the next section*/
1407 p_current_data += 3+i_section_length;
1409 p_current_section+=1;
1411 } while( i_current_section < p_psi->i_last_section_number );
1413 p_pgrm_data->i_pmt_version = p_psi->i_version_number;