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.37 2001/02/19 19:08:59 massiot 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>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
41 #include "stream_control.h"
42 #include "input_ext-intf.h"
43 #include "input_ext-dec.h"
46 #include "mpeg_system.h"
48 #include "main.h" /* AC3/MPEG channel, SPU channel */
50 /*****************************************************************************
52 *****************************************************************************/
56 * PES Packet management
59 /*****************************************************************************
61 *****************************************************************************
62 * Small utility function used to parse discontinuous headers safely. Copies
63 * i_buf_len bytes of data to a buffer and returns the size copied.
64 * This is a variation on the theme of input_ext-dec.h:GetChunk().
65 *****************************************************************************/
66 static __inline__ size_t MoveChunk( byte_t * p_dest,
67 data_packet_t ** pp_data_src,
71 ptrdiff_t i_available;
73 if( (i_available = (*pp_data_src)->p_payload_end - *pp_src)
77 memcpy( p_dest, *pp_src, i_buf_len );
83 size_t i_init_len = i_buf_len;
88 memcpy( p_dest, *pp_src, i_available );
89 *pp_data_src = (*pp_data_src)->p_next;
90 i_buf_len -= i_available;
91 p_dest += i_available;
92 if( *pp_data_src == NULL )
95 return( i_init_len - i_buf_len );
97 *pp_src = (*pp_data_src)->p_payload_start;
99 while( (i_available = (*pp_data_src)->p_payload_end - *pp_src)
105 memcpy( p_dest, *pp_src, i_buf_len );
106 *pp_src += i_buf_len;
108 return( i_init_len );
112 /*****************************************************************************
114 *****************************************************************************
115 * Parse a finished PES packet and analyze its header.
116 *****************************************************************************/
117 #define PES_HEADER_SIZE 7
118 void input_ParsePES( input_thread_t * p_input, es_descriptor_t * p_es )
120 data_packet_t * p_data;
122 byte_t p_header[PES_HEADER_SIZE];
125 #define p_pes (p_es->p_pes)
127 //intf_DbgMsg("End of PES packet %p", p_pes);
129 /* Parse the header. The header has a variable length, but in order
130 * to improve the algorithm, we will read the 14 bytes we may be
132 p_data = p_pes->p_first;
133 p_byte = p_data->p_payload_start;
136 if( MoveChunk( p_header, &p_data, &p_byte, PES_HEADER_SIZE )
139 intf_WarnMsg( 3, "PES packet too short to have a header" );
140 p_input->pf_delete_pes( p_input->p_method_data, p_pes );
145 /* Get the PES size if defined */
146 p_es->i_pes_real_size = U16_AT(p_header + 4) + 6;
148 /* First read the 6 header bytes common to all PES packets:
149 * use them to test the PES validity */
150 if( (p_header[0] || p_header[1] || (p_header[2] != 1)) )
152 /* packet_start_code_prefix != 0x000001 */
153 intf_ErrMsg( "PES packet doesn't start with 0x000001 : data loss" );
154 p_input->pf_delete_pes( p_input->p_method_data, p_pes );
159 int i_pes_header_size, i_payload_size;
161 if ( p_es->i_pes_real_size &&
162 (p_es->i_pes_real_size != p_pes->i_pes_size) )
164 /* PES_packet_length is set and != total received payload */
165 /* Warn the decoder that the data may be corrupt. */
166 intf_WarnMsg( 3, "PES sizes do not match : packet corrupted" );
169 switch( p_es->i_stream_id )
171 case 0xBC: /* Program stream map */
172 case 0xBE: /* Padding */
173 case 0xBF: /* Private stream 2 */
176 case 0xFF: /* Program stream directory */
177 case 0xF2: /* DSMCC stream */
178 case 0xF8: /* ITU-T H.222.1 type E stream */
179 /* The payload begins immediately after the 6 bytes header, so
180 * we have finished with the parsing */
181 i_pes_header_size = 6;
185 if( (p_header[6] & 0xC0) == 0x80 )
187 /* MPEG-2 : the PES header contains at least 3 more bytes. */
189 boolean_t b_has_pts, b_has_dts;
190 byte_t p_full_header[12];
192 p_pes->b_data_alignment = p_header[6] & 0x04;
194 i_max_len = MoveChunk( p_full_header, &p_data, &p_byte, 12 );
198 "PES packet too short to have a MPEG-2 header" );
199 p_input->pf_delete_pes( p_input->p_method_data,
205 b_has_pts = p_full_header[0] & 0x80;
206 b_has_dts = p_full_header[0] & 0x40;
207 i_pes_header_size = p_full_header[1] + 9;
209 /* Now parse the optional header extensions */
215 "PES packet too short to have a MPEG-2 header" );
216 p_input->pf_delete_pes( p_input->p_method_data,
221 p_pes->i_pts = input_ClockGetTS( p_input, p_es->p_pgrm,
222 ( ((mtime_t)(p_full_header[2] & 0x0E) << 29) |
223 (((mtime_t)U16_AT(p_full_header + 3) << 14) - (1 << 14)) |
224 ((mtime_t)U16_AT(p_full_header + 5) >> 1) ) );
231 "PES packet too short to have a MPEG-2 header" );
232 p_input->pf_delete_pes( p_input->p_method_data,
237 p_pes->i_dts = input_ClockGetTS( p_input, p_es->p_pgrm,
238 ( ((mtime_t)(p_full_header[7] & 0x0E) << 29) |
239 (((mtime_t)U16_AT(p_full_header + 8) << 14)
241 ((mtime_t)U16_AT(p_full_header + 10) >> 1) ) );
247 /* Probably MPEG-1 */
248 boolean_t b_has_pts, b_has_dts;
250 i_pes_header_size = 6;
251 p_data = p_pes->p_first;
252 p_byte = p_data->p_payload_start;
253 /* Cannot fail because the previous one succeeded. */
254 MoveChunk( NULL, &p_data, &p_byte, 6 );
256 while( *p_byte == 0xFF && i_pes_header_size < 22 )
259 if( MoveChunk( NULL, &p_data, &p_byte, 1 ) != 1 )
262 "PES packet too short to have a MPEG-1 header" );
263 p_input->pf_delete_pes( p_input->p_method_data, p_pes );
268 if( i_pes_header_size == 22 )
270 intf_ErrMsg( "Too much MPEG-1 stuffing" );
271 p_input->pf_delete_pes( p_input->p_method_data, p_pes );
276 if( (*p_byte & 0xC0) == 0x40 )
278 /* Don't ask why... --Meuuh */
279 /* Erm... why ? --Sam */
280 /* Well... According to the recommendation, it is for
281 * STD_buffer_scale and STD_buffer_size. --Meuuh */
282 i_pes_header_size += 2;
283 if( MoveChunk( NULL, &p_data, &p_byte, 2 ) != 2 )
286 "PES packet too short to have a MPEG-1 header" );
287 p_input->pf_delete_pes( p_input->p_method_data, p_pes );
295 b_has_pts = *p_byte & 0x20;
296 b_has_dts = *p_byte & 0x10;
302 i_pes_header_size += 4;
303 if( MoveChunk( p_ts, &p_data, &p_byte, 5 ) != 5 )
306 "PES packet too short to have a MPEG-1 header" );
307 p_input->pf_delete_pes( p_input->p_method_data, p_pes );
312 p_pes->i_pts = input_ClockGetTS( p_input, p_es->p_pgrm,
313 ( ((mtime_t)(p_ts[0] & 0x0E) << 29) |
314 (((mtime_t)U16_AT(p_ts + 1) << 14) - (1 << 14)) |
315 ((mtime_t)U16_AT(p_ts + 3) >> 1) ) );
319 i_pes_header_size += 5;
320 if( MoveChunk( p_ts, &p_data, &p_byte, 5 ) != 5 )
323 "PES packet too short to have a MPEG-1 header" );
324 p_input->pf_delete_pes( p_input->p_method_data,
330 p_pes->i_dts = input_ClockGetTS( p_input,
332 ( ((mtime_t)(p_ts[0] & 0x0E) << 29) |
333 (((mtime_t)U16_AT(p_ts + 1) << 14) - (1 << 14)) |
334 ((mtime_t)U16_AT(p_ts + 3) >> 1) ) );
342 if( p_es->i_stream_id == 0xbd )
344 /* With private stream 1, the first byte of the payload
345 * is a stream_private_id, so skip it. */
349 /* Now we've parsed the header, we just have to indicate in some
350 * specific data packets where the PES payload begins (renumber
351 * p_payload_start), so that the decoders can find the beginning
352 * of their data right out of the box. */
353 p_data = p_pes->p_first;
354 i_payload_size = p_data->p_payload_end
355 - p_data->p_payload_start;
356 while( i_pes_header_size > i_payload_size )
358 /* These packets are entirely filled by the PES header. */
359 i_pes_header_size -= i_payload_size;
360 p_data->p_payload_start = p_data->p_payload_end;
361 /* Go to the next data packet. */
362 if( (p_data = p_data->p_next) == NULL )
364 intf_ErrMsg( "PES header bigger than payload" );
365 p_input->pf_delete_pes( p_input->p_method_data, p_pes );
369 i_payload_size = p_data->p_payload_end
370 - p_data->p_payload_start;
372 /* This last packet is partly header, partly payload. */
373 if( i_payload_size < i_pes_header_size )
375 intf_ErrMsg( "PES header bigger than payload" );
376 p_input->pf_delete_pes( p_input->p_method_data, p_pes );
380 p_data->p_payload_start += i_pes_header_size;
382 /* Now we can eventually put the PES packet in the decoder's
384 if( p_es->p_decoder_fifo != NULL )
386 input_DecodePES( p_es->p_decoder_fifo, p_pes );
390 intf_ErrMsg("No fifo to receive PES %p (who wrote this damn code ?)",
392 p_input->pf_delete_pes( p_input->p_method_data, p_pes );
400 /*****************************************************************************
402 *****************************************************************************
403 * Gather a PES packet.
404 *****************************************************************************/
405 void input_GatherPES( input_thread_t * p_input, data_packet_t * p_data,
406 es_descriptor_t * p_es,
407 boolean_t b_unit_start, boolean_t b_packet_lost )
409 #define p_pes (p_es->p_pes)
411 //intf_DbgMsg("PES-demultiplexing %p (%p)", p_ts_packet, p_pes);
413 /* If we lost data, insert a NULL data packet (philosophy : 0 is quite
414 * often an escape sequence in decoders, so that should make them wait
415 * for the next start code). */
418 input_NullPacket( p_input, p_es );
421 if( b_unit_start && p_pes != NULL )
423 /* If the data packet contains the begining of a new PES packet, and
424 * if we were reassembling a PES packet, then the PES should be
425 * complete now, so parse its header and give it to the decoders. */
426 input_ParsePES( p_input, p_es );
429 if( !b_unit_start && p_pes == NULL )
431 /* Random access... */
432 p_input->pf_delete_packet( p_input->p_method_data, p_data );
438 /* If we are at the beginning of a new PES packet, we must fetch
439 * a new PES buffer to begin with the reassembly of this PES
440 * packet. This is also here that we can synchronize with the
441 * stream if we lost packets or if the decoder has just
443 if( (p_pes = p_input->pf_new_pes( p_input->p_method_data ) ) == NULL )
445 intf_ErrMsg("Out of memory");
446 p_input->b_error = 1;
449 p_pes->i_rate = p_input->stream.control.i_rate;
450 p_pes->p_first = p_data;
452 /* If the PES header fits in the first data packet, we can
453 * already set p_gather->i_pes_real_size. */
454 if( p_data->p_payload_end - p_data->p_payload_start
457 p_es->i_pes_real_size =
458 U16_AT(p_data->p_payload_start + 4) + 6;
463 p_es->i_pes_real_size = 0;
468 /* Update the relations between the data packets */
469 p_es->p_last->p_next = p_data;
472 p_es->p_last = p_data;
474 /* Size of the payload carried in the data packet */
475 p_pes->i_pes_size += (p_data->p_payload_end
476 - p_data->p_payload_start);
478 /* We can check if the packet is finished */
479 if( p_pes->i_pes_size == p_es->i_pes_real_size )
481 /* The packet is finished, parse it */
482 input_ParsePES( p_input, p_es );
493 /*****************************************************************************
494 * GetID: Get the ID of a stream
495 *****************************************************************************/
496 static u16 GetID( data_packet_t * p_data )
500 i_id = p_data->p_buffer[3]; /* stream_id */
503 /* stream_private_id */
504 i_id |= p_data->p_buffer[ 9 + p_data->p_buffer[8] ] << 8;
509 /*****************************************************************************
510 * DecodePSM: Decode the Program Stream Map information
511 *****************************************************************************/
512 static void DecodePSM( input_thread_t * p_input, data_packet_t * p_data )
514 stream_ps_data_t * p_demux =
515 (stream_ps_data_t *)p_input->stream.p_demux_data;
519 int i_new_es_number = 0;
521 if( p_data->p_payload_start + 10 > p_data->p_payload_end )
523 intf_ErrMsg( "PSM too short : packet corrupt" );
527 if( p_demux->b_has_PSM
528 && p_demux->i_PSM_version == (p_data->p_buffer[6] & 0x1F) )
530 /* Already got that one. */
534 intf_DbgMsg( "Building PSM" );
535 p_demux->b_has_PSM = 1;
536 p_demux->i_PSM_version = p_data->p_buffer[6] & 0x1F;
538 /* Go to elementary_stream_map_length, jumping over
539 * program_stream_info. */
540 p_byte = p_data->p_payload_start + 10
541 + U16_AT(&p_data->p_payload_start[8]);
542 if( p_byte > p_data->p_payload_end )
544 intf_ErrMsg( "PSM too short : packet corrupt" );
547 /* This is the full size of the elementary_stream_map.
548 * 2 == elementary_stream_map_length
549 * Please note that CRC_32 is not included in the length. */
550 p_end = p_byte + 2 + U16_AT(p_byte);
552 if( p_end > p_data->p_payload_end )
554 intf_ErrMsg( "PSM too short : packet corrupt" );
558 vlc_mutex_lock( &p_input->stream.stream_lock );
560 /* 4 == minimum useful size of a section */
561 while( p_byte + 4 <= p_end )
563 es_descriptor_t * p_es = NULL;
564 u8 i_stream_id = p_byte[1];
565 /* FIXME: there will be a problem with private streams... (same
568 /* Look for the ES in the ES table */
569 for( i = i_new_es_number;
570 i < p_input->stream.pp_programs[0]->i_es_number;
573 if( p_input->stream.pp_programs[0]->pp_es[i]->i_stream_id
576 p_es = p_input->stream.pp_programs[0]->pp_es[i];
577 if( p_es->i_type != p_byte[0] )
579 input_DelES( p_input, p_es );
584 /* Move the ES to the beginning. */
585 p_input->stream.pp_programs[0]->pp_es[i]
586 = p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ];
587 p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ]
595 /* The goal is to have all the ES we have just read in the
596 * beginning of the pp_es table, and all the others at the end,
597 * so that we can close them more easily at the end. */
600 p_es = input_AddES( p_input, p_input->stream.pp_programs[0],
602 p_es->i_type = p_byte[0];
603 p_es->b_audio = ( p_es->i_type == MPEG1_AUDIO_ES
604 || p_es->i_type == MPEG2_AUDIO_ES
605 || p_es->i_type == AC3_AUDIO_ES
606 || p_es->i_type == LPCM_AUDIO_ES
609 /* input_AddES has inserted the new element at the end. */
610 p_input->stream.pp_programs[0]->pp_es[
611 p_input->stream.pp_programs[0]->i_es_number ]
612 = p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ];
613 p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ] = p_es;
616 p_byte += 4 + U16_AT(&p_byte[2]);
619 /* Un-select the streams that are no longer parts of the program. */
620 for( i = i_new_es_number;
621 i < p_input->stream.pp_programs[0]->i_es_number;
624 /* We remove pp_es[i_new_es_member] and not pp_es[i] because the
625 * list will be emptied starting from the end */
626 input_DelES( p_input,
627 p_input->stream.pp_programs[0]->pp_es[i_new_es_number] );
631 intf_Msg( "input info: The stream map after the PSM is now :" );
632 input_DumpStream( p_input );
635 vlc_mutex_unlock( &p_input->stream.stream_lock );
638 /*****************************************************************************
639 * input_ParsePS: read the PS header
640 *****************************************************************************/
641 es_descriptor_t * input_ParsePS( input_thread_t * p_input,
642 data_packet_t * p_data )
645 es_descriptor_t * p_es = NULL;
647 i_code = U32_AT( p_data->p_buffer );
648 if( i_code > 0x1BC ) /* ES start code */
653 /* This is a PES packet. Find out if we want it or not. */
654 i_id = GetID( p_data );
656 vlc_mutex_lock( &p_input->stream.stream_lock );
657 if( p_input->stream.pp_programs[0]->b_is_ok )
659 /* Look only at the selected ES. */
660 for( i_dummy = 0; i_dummy < p_input->stream.i_selected_es_number;
663 if( p_input->stream.pp_selected_es[i_dummy] != NULL
664 && p_input->stream.pp_selected_es[i_dummy]->i_id == i_id )
666 p_es = p_input->stream.pp_selected_es[i_dummy];
673 stream_ps_data_t * p_demux =
674 (stream_ps_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
676 /* Search all ES ; if not found -> AddES */
677 p_es = input_FindES( p_input, i_id );
679 if( p_es == NULL && !p_demux->b_has_PSM )
681 p_es = input_AddES( p_input, p_input->stream.pp_programs[0],
685 p_es->i_stream_id = p_data->p_buffer[3];
687 /* Set stream type and auto-spawn. */
688 if( (i_id & 0xF0) == 0xE0 )
691 p_es->i_type = MPEG2_VIDEO_ES;
693 if( !p_input->stream.b_seekable )
694 input_SelectES( p_input, p_es );
697 else if( (i_id & 0xE0) == 0xC0 )
700 p_es->i_type = MPEG2_AUDIO_ES;
703 if( !p_input->stream.b_seekable )
704 if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
705 == (p_es->i_id & 0x1F) )
706 switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
709 main_PutIntVariable( INPUT_CHANNEL_VAR,
712 input_SelectES( p_input, p_es );
716 else if( (i_id & 0xF0FF) == 0x80BD )
718 /* AC3 audio (0x80->0x8F) */
719 p_es->i_type = AC3_AUDIO_ES;
722 if( !p_input->stream.b_seekable )
723 if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
724 == ((p_es->i_id & 0xF00) >> 8) )
725 switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
728 main_PutIntVariable( INPUT_CHANNEL_VAR,
731 input_SelectES( p_input, p_es );
735 else if( (i_id & 0xE0FF) == 0x20BD )
737 /* Subtitles video (0x20->0x3F) */
738 p_es->i_type = DVD_SPU_ES;
740 if( main_GetIntVariable( INPUT_SUBTITLE_VAR, -1 )
741 == ((p_es->i_id & 0x1F00) >> 8) )
743 if( !p_input->stream.b_seekable )
744 input_SelectES( p_input, p_es );
748 else if( (i_id & 0xF0FF) == 0xA0BD )
750 /* LPCM audio (0xA0->0xAF) */
751 p_es->i_type = LPCM_AUDIO_ES;
753 /* FIXME : write the decoder */
757 p_es->i_type = UNKNOWN_ES;
761 } /* stream.b_is_ok */
762 vlc_mutex_unlock( &p_input->stream.stream_lock );
763 } /* i_code > 0xBC */
768 /*****************************************************************************
769 * input_DemuxPS: first step of demultiplexing: the PS header
770 *****************************************************************************/
771 void input_DemuxPS( input_thread_t * p_input, data_packet_t * p_data )
774 boolean_t b_trash = 0;
775 es_descriptor_t * p_es = NULL;
777 i_code = U32_AT( p_data->p_buffer );
778 if( i_code <= 0x1BC )
782 case 0x1BA: /* PACK_START_CODE */
788 if( (p_data->p_buffer[4] & 0xC0) == 0x40 )
792 ((mtime_t)(p_data->p_buffer[4] & 0x38) << 27) |
793 ((mtime_t)(U32_AT(p_data->p_buffer + 4) & 0x03FFF800)
795 ((mtime_t)(U32_AT(p_data->p_buffer + 6) & 0x03FFF800)
799 i_mux_rate = (U32_AT(p_data->p_buffer + 10) & 0xFFFFFC00);
803 /* MPEG-1 SCR is like PTS. */
805 ((mtime_t)(p_data->p_buffer[4] & 0x0E) << 29) |
806 (((mtime_t)U16_AT(p_data->p_buffer + 5) << 14)
808 ((mtime_t)U16_AT(p_data->p_buffer + 7) >> 1);
811 i_mux_rate = (U32_AT(p_data->p_buffer + 8) & 0x8FFFFE);
813 /* Call the pace control. */
814 input_ClockManageRef( p_input, p_input->stream.pp_programs[0],
817 if( i_mux_rate != p_input->stream.i_mux_rate
818 && p_input->stream.i_mux_rate )
821 "Mux_rate changed - expect cosmetic errors");
823 p_input->stream.i_mux_rate = i_mux_rate;
829 case 0x1BB: /* SYSTEM_START_CODE */
830 b_trash = 1; /* Nothing interesting */
833 case 0x1BC: /* PROGRAM_STREAM_MAP_CODE */
834 DecodePSM( p_input, p_data );
838 case 0x1B9: /* PROGRAM_END_CODE */
843 /* This should not happen */
845 intf_WarnMsg( 1, "Unwanted packet received with start code %x",
851 p_es = input_ParsePS( p_input, p_data );
853 vlc_mutex_lock( &p_input->stream.control.control_lock );
854 if( p_es != NULL && p_es->p_decoder_fifo != NULL
855 && (!p_es->b_audio || !p_input->stream.control.b_mute) )
857 vlc_mutex_unlock( &p_input->stream.control.control_lock );
861 input_GatherPES( p_input, p_data, p_es, 1, 0 );
865 vlc_mutex_unlock( &p_input->stream.control.control_lock );
870 /* Trash the packet if it has no payload or if it isn't selected */
873 p_input->pf_delete_packet( p_input->p_method_data, p_data );
875 p_input->c_packets_trashed++;
885 /*****************************************************************************
886 * input_DemuxTS: first step of demultiplexing: the TS header
887 *****************************************************************************/
888 void input_DemuxTS( input_thread_t * p_input, data_packet_t * p_data )
891 boolean_t b_adaptation; /* Adaptation field is present */
892 boolean_t b_payload; /* Packet carries payload */
893 boolean_t b_unit_start; /* A PSI or a PES start in the packet */
894 boolean_t b_trash = 0; /* Is the packet unuseful ? */
895 boolean_t b_lost = 0; /* Was there a packet loss ? */
896 es_descriptor_t * p_es = NULL;
897 es_ts_data_t * p_es_demux = NULL;
898 pgrm_ts_data_t * p_pgrm_demux = NULL;
900 #define p (p_data->p_buffer)
902 //intf_DbgMsg("input debug: TS-demultiplexing packet %p, pid %d",
903 // p_ts_packet, U16_AT(&p[1]) & 0x1fff);
905 /* Extract flags values from TS common header. */
906 i_pid = U16_AT(&p[1]) & 0x1fff;
907 b_unit_start = (p[1] & 0x40);
908 b_adaptation = (p[3] & 0x20);
909 b_payload = (p[3] & 0x10);
911 /* Find out the elementary stream. */
912 vlc_mutex_lock( &p_input->stream.stream_lock );
917 p_es = input_FindES( p_input, 0x78 );
918 p_es->i_type = MPEG2_VIDEO_ES;
919 if( p_es->p_pes == NULL )
920 intf_ErrMsg("Got p_es . p_es->p_pes == null ? %d",p_es->p_pes == NULL);
929 vlc_mutex_lock( &p_input->stream.control.control_lock );
930 if( p_es == NULL || p_es->p_decoder_fifo == NULL
931 || (p_es->b_audio && p_input->stream.control.b_mute) )
933 /* Not selected. Just read the adaptation field for a PCR. */
936 vlc_mutex_unlock( &p_input->stream.control.control_lock );
937 vlc_mutex_unlock( &p_input->stream.stream_lock );
943 p_es_demux = (es_ts_data_t *)p_es->p_demux_data;
944 p_pgrm_demux = (pgrm_ts_data_t *)p_es->p_pgrm->p_demux_data;
946 p_pgrm_demux->i_pcr_pid = 0x78;
948 if( (p_es->p_decoder_fifo != NULL) || (p_pgrm_demux->i_pcr_pid == i_pid) )
954 /* Extract adaptation field information if any */
958 /* We don't have any adaptation_field, so payload starts
959 * immediately after the 4 byte TS header */
960 p_data->p_payload_start += 4;
964 /* p[4] is adaptation_field_length minus one */
965 p_data->p_payload_start += 5 + p[4];
967 /* The adaptation field can be limited to the
968 * adaptation_field_length byte, so that there is nothing to do:
969 * skip this possibility */
972 /* If the packet has both adaptation_field and payload,
973 * adaptation_field cannot be more than 182 bytes long; if
974 * there is only an adaptation_field, it must fill the next
976 if( b_payload ? (p[4] > 182) : (p[4] != 183) )
979 "invalid TS adaptation field (%p)",
981 p_data->b_discard_payload = 1;
983 p_es->c_invalid_packets++;
987 /* Now we are sure that the byte containing flags is present:
991 /* discontinuity_indicator */
995 "discontinuity_indicator"
996 " encountered by TS demux (position read: %d,"
998 p[5] & 0x80, p_es_demux->i_continuity_counter );
1000 /* If the PID carries the PCR, there will be a system
1001 * time-based discontinuity. We let the PCR decoder
1003 p_es->p_pgrm->i_synchro_state = SYNCHRO_REINIT;
1005 /* There also may be a continuity_counter
1006 * discontinuity: resynchronise our counter with
1007 * the one of the stream. */
1008 p_es_demux->i_continuity_counter = (p[3] & 0x0f) - 1;
1011 /* If this is a PCR_PID, and this TS packet contains a
1012 * PCR, we pass it along to the PCR decoder. */
1014 if( (p_pgrm_demux->i_pcr_pid == i_pid) && (p[5] & 0x10) )
1016 /* There should be a PCR field in the packet, check
1017 * if the adaptation field is long enough to carry
1024 ( (mtime_t)U32_AT((u32*)&p[6]) << 1 )
1026 /* Call the pace control. */
1027 input_ClockManageRef( p_input, p_es->p_pgrm,
1031 } /* valid TS adaptation field ? */
1033 } /* has adaptation field */
1034 /* Check the continuity of the stream. */
1035 i_dummy = ((p[3] & 0x0f) - p_es_demux->i_continuity_counter) & 0x0f;
1038 /* Everything is ok, just increase our counter */
1039 (p_es_demux->i_continuity_counter)++;
1043 if( !b_payload && i_dummy == 0 )
1045 /* This is a packet without payload, this is allowed by the
1046 * draft. As there is nothing interesting in this packet
1047 * (except PCR that have already been handled), we can trash
1050 "Packet without payload received by TS demux" );
1053 else if( i_dummy <= 0 )
1055 /* FIXME: this can never happen, can it ? --Meuuh */
1056 /* Duplicate packet: mark it as being to be trashed. */
1057 intf_WarnMsg( 1, "Duplicate packet received by TS demux" );
1060 else if( p_es_demux->i_continuity_counter == 0xFF )
1062 /* This means that the packet is the first one we receive for
1063 * this ES since the continuity counter ranges between 0 and
1064 * 0x0F excepts when it has been initialized by the input:
1065 * init the counter to the correct value. */
1066 intf_DbgMsg( "First packet for PID %d received by TS demux",
1068 p_es_demux->i_continuity_counter = (p[3] & 0x0f);
1072 /* This can indicate that we missed a packet or that the
1073 * continuity_counter wrapped and we received a dup packet:
1074 * as we don't know, do as if we missed a packet to be sure
1075 * to recover from this situation */
1077 "Packet lost by TS demux: current %d, packet %d",
1078 p_es_demux->i_continuity_counter & 0x0f,
1081 p_es_demux->i_continuity_counter = p[3] & 0x0f;
1082 } /* not continuous */
1084 } /* if selected or PCR */
1088 /* Trash the packet if it has no payload or if it isn't selected */
1092 p_input->pf_delete_packet( p_input->p_method_data, p_data );
1094 p_input->c_packets_trashed++;
1099 if( p_es_demux->b_psi )
1102 //printf("DemuxTS : Was a PSI\n");
1103 /* The payload contains PSI tables */
1105 /* FIXME ! write the PSI decoder :p */
1106 input_DemuxPSI( p_input, p_data, p_es,
1107 b_unit_start, b_lost );
1112 /* The payload carries a PES stream */
1113 input_GatherPES( p_input, p_data, p_es, b_unit_start, b_lost );