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.14 2000/12/21 19:24:27 massiot Exp $
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 #include <netinet/in.h>
39 #include "stream_control.h"
40 #include "input_ext-intf.h"
41 #include "input_ext-dec.h"
44 #include "mpeg_system.h"
46 #include "main.h" /* AC3/MPEG channel, SPU channel */
48 /*****************************************************************************
50 *****************************************************************************/
54 * PES Packet management
57 /*****************************************************************************
59 *****************************************************************************
60 * Put a PES in the decoder's fifo.
61 *****************************************************************************/
62 void input_DecodePES( input_thread_t * p_input, es_descriptor_t * p_es )
64 #define p_pes (p_es->p_pes)
66 if( p_es->p_decoder_fifo != NULL )
68 vlc_mutex_lock( &p_es->p_decoder_fifo->data_lock );
71 if( p_input->stream.b_pace_control )
73 /* FIXME : normally we shouldn't need this... */
74 while( DECODER_FIFO_ISFULL( *p_es->p_decoder_fifo ) )
76 vlc_mutex_unlock( &p_es->p_decoder_fifo->data_lock );
78 vlc_mutex_lock( &p_es->p_decoder_fifo->data_lock );
83 if( !DECODER_FIFO_ISFULL( *p_es->p_decoder_fifo ) )
85 //intf_DbgMsg("Putting %p into fifo %p/%d\n",
86 // p_pes, p_fifo, p_fifo->i_end);
87 p_es->p_decoder_fifo->buffer[p_es->p_decoder_fifo->i_end] = p_pes;
88 DECODER_FIFO_INCEND( *p_es->p_decoder_fifo );
90 /* Warn the decoder that it's got work to do. */
91 vlc_cond_signal( &p_es->p_decoder_fifo->data_wait );
95 /* The FIFO is full !!! This should not happen. */
96 p_input->p_plugin->pf_delete_pes( p_input->p_method_data, p_pes );
97 intf_ErrMsg( "PES trashed - fifo full ! (%d, %d)",
98 p_es->i_id, p_es->i_type);
100 vlc_mutex_unlock( &p_es->p_decoder_fifo->data_lock );
104 intf_ErrMsg("No fifo to receive PES %p (who wrote this damn code ?)",
106 p_input->p_plugin->pf_delete_pes( p_input->p_method_data, p_pes );
113 /*****************************************************************************
115 *****************************************************************************
116 * Parse a finished PES packet and analyze its header.
117 *****************************************************************************/
118 #define PES_HEADER_SIZE 14
119 void input_ParsePES( input_thread_t * p_input, es_descriptor_t * p_es )
121 data_packet_t * p_header_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\n", 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_header_data = p_pes->p_first;
137 i_todo = p_header_data->p_payload_end
138 - p_header_data->p_payload_start;
139 if( i_todo > PES_HEADER_SIZE - i_done )
140 i_todo = PES_HEADER_SIZE - i_done;
142 memcpy( p_header + i_done, p_header_data->p_payload_start,
146 if( i_done < PES_HEADER_SIZE && p_header_data->p_next != NULL )
148 p_header_data = p_header_data->p_next;
156 if( i_done != PES_HEADER_SIZE )
158 intf_WarnMsg( 3, "PES packet too short to have a header" );
159 p_input->p_plugin->pf_delete_pes( p_input->p_method_data, p_pes );
164 /* Get the PES size if defined */
165 p_es->i_pes_real_size = U16_AT(p_header + 4) + 6;
167 /* First read the 6 header bytes common to all PES packets:
168 * use them to test the PES validity */
169 if( (p_header[0] || p_header[1] || (p_header[2] != 1)) )
171 /* packet_start_code_prefix != 0x000001 */
172 intf_ErrMsg( "PES packet doesn't start with 0x000001 : data loss" );
173 p_input->p_plugin->pf_delete_pes( p_input->p_method_data, p_pes );
178 int i_pes_header_size, i_payload_size;
180 if ( p_es->i_pes_real_size &&
181 (p_es->i_pes_real_size != p_pes->i_pes_size) )
183 /* PES_packet_length is set and != total received payload */
184 /* Warn the decoder that the data may be corrupt. */
185 intf_WarnMsg( 3, "PES sizes do not match : packet corrupted" );
186 p_pes->b_messed_up = 1;
189 switch( p_es->i_stream_id )
191 case 0xBC: /* Program stream map */
192 case 0xBE: /* Padding */
193 case 0xBF: /* Private stream 2 */
196 case 0xFF: /* Program stream directory */
197 case 0xF2: /* DSMCC stream */
198 case 0xF8: /* ITU-T H.222.1 type E stream */
199 /* The payload begins immediately after the 6 bytes header, so
200 * we have finished with the parsing */
201 i_pes_header_size = 6;
205 if( (p_header[6] & 0xC0) == 0x80 )
207 /* MPEG-2 : the PES header contains at least 3 more bytes. */
208 p_pes->b_data_alignment = p_header[6] & 0x04;
209 p_pes->b_has_pts = p_header[7] & 0x80;
210 i_pes_header_size = p_header[8] + 9;
212 /* Now parse the optional header extensions (in the limit of
214 if( p_pes->b_has_pts )
217 ( ((mtime_t)(p_header[9] & 0x0E) << 29) |
218 (((mtime_t)U16_AT(p_header + 10) << 14) - (1 << 14)) |
219 ((mtime_t)U16_AT(p_header + 12) >> 1) ) * 300;
225 /* Probably MPEG-1 */
227 data_packet_t * p_data;
229 i_pes_header_size = 6;
230 p_data = p_pes->p_first;
231 p_byte = p_data->p_buffer + 6;
232 while( *p_byte == 0xFF && i_pes_header_size < 22 )
236 if( p_byte >= p_data->p_payload_end )
238 p_data = p_data->p_next;
241 intf_ErrMsg( "MPEG-1 packet too short for header" );
242 p_input->p_plugin->pf_delete_pes( p_input->p_method_data, p_pes );
246 p_byte = p_data->p_payload_start;
249 if( i_pes_header_size == 22 )
251 intf_ErrMsg( "Too much MPEG-1 stuffing" );
252 p_input->p_plugin->pf_delete_pes( p_input->p_method_data, p_pes );
257 if( (*p_byte & 0xC0) == 0x40 )
259 /* Don't ask why... --Meuuh */
261 i_pes_header_size += 2;
262 if( p_byte >= p_data->p_payload_end )
264 int i_plus = p_byte - p_data->p_payload_end;
265 p_data = p_data->p_next;
268 intf_ErrMsg( "MPEG-1 packet too short for header" );
269 p_input->p_plugin->pf_delete_pes( p_input->p_method_data, p_pes );
273 p_byte = p_data->p_payload_start + i_plus;
278 p_pes->b_has_pts = *p_byte & 0x20;
283 i_pes_header_size += 5;
291 i_pes_header_size += 4;
293 for( i = 1; i < 5; i++ )
296 if( p_byte >= p_data->p_payload_end )
298 p_data = p_data->p_next;
301 intf_ErrMsg( "MPEG-1 packet too short for header" );
302 p_input->p_plugin->pf_delete_pes( p_input->p_method_data, p_pes );
306 p_byte = p_data->p_payload_start;
312 ( ((mtime_t)(p_pts[0] & 0x0E) << 29) |
313 (((mtime_t)U16_AT(p_pts + 1) << 14) - (1 << 14)) |
314 ((mtime_t)U16_AT(p_pts + 3) >> 1) ) * 300;
320 if( p_pes->b_has_pts )
322 //intf_Msg("%lld\n", p_pes->i_pts);
323 switch( p_es->p_pgrm->i_synchro_state )
325 case SYNCHRO_NOT_STARTED:
327 p_pes->b_has_pts = 0;
330 case SYNCHRO_REINIT: /* We skip a PES | Why ?? --Meuuh */
331 p_pes->b_has_pts = 0;
332 p_es->p_pgrm->i_synchro_state = SYNCHRO_START;
336 p_pes->i_pts += p_es->p_pgrm->delta_cr
337 + p_es->p_pgrm->delta_absolute
345 if( p_es->i_stream_id == 0xBC )
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_header_data = p_pes->p_first;
357 i_payload_size = p_header_data->p_payload_end
358 - p_header_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_header_data->p_payload_start = p_header_data->p_payload_end;
364 /* Go to the next data packet. */
365 if( (p_header_data = p_header_data->p_next) == NULL )
367 intf_ErrMsg( "PES header bigger than payload" );
368 p_input->p_plugin->pf_delete_pes( p_input->p_method_data,
373 i_payload_size = p_header_data->p_payload_end
374 - p_header_data->p_payload_start;
376 /* This last packet is partly header, partly payload. */
377 if( i_payload_size < i_pes_header_size )
379 intf_ErrMsg( "PES header bigger than payload" );
380 p_input->p_plugin->pf_delete_pes( p_input->p_method_data, p_pes );
384 p_header_data->p_payload_start += i_pes_header_size;
386 /* Now we can eventually put the PES packet in the decoder's
388 input_DecodePES( p_input, p_es );
393 /*****************************************************************************
395 *****************************************************************************
396 * Gather a PES packet.
397 *****************************************************************************/
398 void input_GatherPES( input_thread_t * p_input, data_packet_t * p_data,
399 es_descriptor_t * p_es,
400 boolean_t b_unit_start, boolean_t b_packet_lost )
402 #define p_pes (p_es->p_pes)
404 //intf_DbgMsg("PES-demultiplexing %p (%p)\n", p_ts_packet, p_pes);
406 /* If we lost data, insert an NULL data packet (philosophy : 0 is quite
407 * often an escape sequence in decoders, so that should make them wait
408 * for the next start code). */
409 if( b_packet_lost || p_es->b_discontinuity )
411 data_packet_t * p_pad_data;
413 if( (p_pad_data = p_input->p_plugin->pf_new_packet(
414 p_input->p_method_data,
415 PADDING_PACKET_SIZE )) == NULL )
417 intf_ErrMsg("Out of memory\n");
418 p_input->b_error = 1;
421 memset( p_data->p_buffer, 0, PADDING_PACKET_SIZE );
422 p_pad_data->b_discard_payload = 1;
426 p_pes->b_messed_up = p_pes->b_discontinuity = 1;
427 input_GatherPES( p_input, p_pad_data, p_es, 0, 0 );
431 if( (p_pes = p_input->p_plugin->pf_new_pes(
432 p_input->p_method_data )) == NULL )
434 intf_ErrMsg("Out of memory\n");
435 p_input->b_error = 1;
439 p_pes->p_first = p_pad_data;
440 p_pes->b_messed_up = p_pes->b_discontinuity = 1;
441 input_DecodePES( p_input, p_es );
444 p_es->b_discontinuity = 0;
447 if( b_unit_start && p_pes != NULL )
449 /* If the TS packet contains the begining of a new PES packet, and
450 * if we were reassembling a PES packet, then the PES should be
451 * complete now, so parse its header and give it to the decoders. */
452 input_ParsePES( p_input, p_es );
455 if( !b_unit_start && p_pes == NULL )
457 /* Random access... */
458 p_input->p_plugin->pf_delete_packet( p_input->p_method_data, p_data );
464 /* If we are at the beginning of a new PES packet, we must fetch
465 * a new PES buffer to begin with the reassembly of this PES
466 * packet. This is also here that we can synchronize with the
467 * stream if we lost packets or if the decoder has just
469 if( (p_pes = p_input->p_plugin->pf_new_pes( p_input->p_method_data ) ) == NULL )
471 intf_ErrMsg("Out of memory");
472 p_input->b_error = 1;
475 //intf_DbgMsg("New PES packet %p (first data: %p)\n", p_pes, p_data);
476 p_pes->p_first = p_data;
478 /* If the PES header fits in the first data packet, we can
479 * already set p_gather->i_pes_real_size. */
480 if( p_data->p_payload_end - p_data->p_payload_start
483 p_es->i_pes_real_size =
484 U16_AT(p_data->p_payload_start + 4) + 6;
488 p_es->i_pes_real_size = 0;
493 /* Update the relations between the data packets */
494 p_es->p_last->p_next = p_data;
497 p_data->p_next = NULL;
498 p_es->p_last = p_data;
500 /* Size of the payload carried in the data packet */
501 p_pes->i_pes_size += (p_data->p_payload_end
502 - p_data->p_payload_start);
504 /* We can check if the packet is finished */
505 if( p_pes->i_pes_size == p_es->i_pes_real_size )
507 /* The packet is finished, parse it */
508 input_ParsePES( p_input, p_es );
520 * DISCUSSION : SYNCHRONIZATION METHOD
522 * In some cases we can impose the pace of reading (when reading from a
523 * file or a pipe), and for the synchronization we simply sleep() until
524 * it is time to deliver the packet to the decoders. When reading from
525 * the network, we must be read at the same pace as the server writes,
526 * otherwise the kernel's buffer will trash packets. The risk is now to
527 * overflow the input buffers in case the server goes too fast, that is
528 * why we do these calculations :
530 * We compute an average for the pcr because we want to eliminate the
531 * network jitter and keep the low frequency variations. The average is
532 * in fact a low pass filter and the jitter is a high frequency signal
533 * that is why it is eliminated by the filter/average.
535 * The low frequency variations enable us to synchronize the client clock
536 * with the server clock because they represent the time variation between
537 * the 2 clocks. Those variations (ie the filtered pcr) are used to compute
538 * the presentation dates for the audio and video frames. With those dates
539 * we can decode (or trash) the MPEG2 stream at "exactly" the same rate
540 * as it is sent by the server and so we keep the synchronization between
541 * the server and the client.
543 * It is a very important matter if you want to avoid underflow or overflow
544 * in all the FIFOs, but it may be not enough.
547 /*****************************************************************************
549 *****************************************************************************/
551 /* Maximum number of samples used to compute the dynamic average value,
552 * it is also the maximum of c_average_count in pgrm_ts_data_t.
553 * We use the following formula :
554 * new_average = (old_average * c_average + new_sample_value) / (c_average +1) */
555 #define CR_MAX_AVERAGE_COUNTER 40
557 /* Maximum gap allowed between two CRs. */
558 #define CR_MAX_GAP 1000000
560 /*****************************************************************************
561 * CRReInit : Reinitialize the clock reference
562 *****************************************************************************/
563 static void CRReInit( pgrm_descriptor_t * p_pgrm )
565 p_pgrm->delta_cr = 0;
567 p_pgrm->c_average_count = 0;
570 /* FIXME: find a better name */
571 /*****************************************************************************
572 * CRDecode : Decode a clock reference
573 *****************************************************************************/
574 static void CRDecode( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm,
577 if( p_pgrm->i_synchro_state != SYNCHRO_OK )
579 switch( p_pgrm->i_synchro_state )
582 p_pgrm->delta_absolute = mdate() - cr_time;
583 p_pgrm->i_synchro_state = SYNCHRO_OK;
586 case SYNCHRO_NOT_STARTED:
587 p_pgrm->i_synchro_state = SYNCHRO_START;
596 if( p_pgrm->b_discontinuity ||
597 ( p_pgrm->last_cr != 0 &&
598 ( (p_pgrm->last_cr - cr_time) > CR_MAX_GAP
599 || (p_pgrm->last_cr - cr_time) < - CR_MAX_GAP ) ) )
603 /* Stream discontinuity. */
604 intf_WarnMsg( 3, "CR re-initialiazed" );
606 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
607 p_pgrm->b_discontinuity = 0;
609 /* Warn all the elementary streams */
610 for( i_es = 0; i_es < p_pgrm->i_es_number; i_es++ )
612 p_pgrm->pp_es[i_es]->b_discontinuity = 1;
615 p_pgrm->last_cr = cr_time;
617 if( p_input->stream.b_pace_control )
619 /* Wait a while before delivering the packets to the decoder. */
620 mwait( cr_time + p_pgrm->delta_absolute );
624 mtime_t sys_time, delta_cr;
627 delta_cr = sys_time - cr_time;
629 if( p_pgrm->c_average_count == CR_MAX_AVERAGE_COUNTER )
631 p_pgrm->delta_cr = ( delta_cr + (p_pgrm->delta_cr
632 * (CR_MAX_AVERAGE_COUNTER - 1)) )
633 / CR_MAX_AVERAGE_COUNTER;
637 p_pgrm->delta_cr = ( delta_cr + (p_pgrm->delta_cr
638 * p_pgrm->c_average_count) )
639 / ( p_pgrm->c_average_count + 1 );
640 p_pgrm->c_average_count++;
651 /*****************************************************************************
652 * GetID: Get the ID of a stream
653 *****************************************************************************/
654 static u16 GetID( data_packet_t * p_data )
658 i_id = p_data->p_buffer[3]; /* stream_id */
661 /* stream_private_id */
662 i_id |= p_data->p_buffer[ 9 + p_data->p_buffer[8] ] << 8;
667 /*****************************************************************************
668 * DecodePSM: Decode the Program Stream Map information
669 *****************************************************************************/
670 /* FIXME : deprecated code ! */
671 static void DecodePSM( input_thread_t * p_input, data_packet_t * p_data )
673 stream_ps_data_t * p_demux =
674 (stream_ps_data_t *)p_input->stream.p_demux_data;
676 intf_Msg("input info: Your stream contains Program Stream Map information");
677 intf_Msg("input info: Please send a mail to <massiot@via.ecp.fr>");
680 if( !p_demux->b_is_PSM_complete )
685 intf_DbgMsg( "Building PSM" );
686 if( p_data->p_payload_start + 10 > p_data->p_payload_end )
688 intf_ErrMsg( "PSM too short : packet corrupt" );
691 /* Go to elementary_stream_map_length, jumping over
692 * program_stream_info. */
693 p_byte = p_data->p_payload_start + 10
694 + U16_AT(&p_data->p_payload_start[8]);
695 if( p_byte > p_data->p_payload_end )
697 intf_ErrMsg( "PSM too short : packet corrupt" );
700 /* This is the full size of the elementary_stream_map.
701 * 2 == elementary_stream_map_length
703 p_end = p_byte + 2 + U16_AT(p_byte) - 4;
705 if( p_end > p_data->p_payload_end )
707 intf_ErrMsg( "PSM too short : packet corrupt" );
711 vlc_mutex_lock( &p_input->stream.stream_lock );
713 /* 4 == minimum useful size of a section */
714 while( p_byte + 4 <= p_end )
716 es_descriptor_t * p_es;
718 p_es = input_AddES( p_input, p_input->stream.pp_programs[0],
720 p_es->i_type = p_byte[0];
721 p_byte += 4 + U16_AT(&p_byte[2]);
724 vlc_mutex_unlock( &p_input->stream.stream_lock );
725 p_demux->i_PSM_version = p_data->p_buffer[6] & 0x1F;
726 p_demux->b_is_PSM_complete = 1;
728 else if( p_demux->i_PSM_version != (p_data->p_buffer[6] & 0x1F) )
731 intf_ErrMsg( "PSM changed, this is not supported yet !" );
732 p_demux->i_PSM_version = p_data->p_buffer[6] & 0x1F;
737 /*****************************************************************************
738 * input_ParsePS: read the PS header
739 *****************************************************************************/
740 es_descriptor_t * input_ParsePS( input_thread_t * p_input,
741 data_packet_t * p_data )
744 es_descriptor_t * p_es = NULL;
746 i_code = U32_AT( p_data->p_buffer );
747 if( i_code > 0x1BC ) /* ES start code */
752 /* This is a PES packet. Find out if we want it or not. */
753 i_id = GetID( p_data );
755 vlc_mutex_lock( &p_input->stream.stream_lock );
756 if( p_input->stream.pp_programs[0]->b_is_ok )
758 /* Look only at the selected ES. */
759 for( i_dummy = 0; i_dummy < p_input->stream.i_selected_es_number;
762 if( p_input->stream.pp_selected_es[i_dummy] != NULL
763 && p_input->stream.pp_selected_es[i_dummy]->i_id == i_id )
765 p_es = p_input->stream.pp_selected_es[i_dummy];
772 /* Search all ES ; if not found -> AddES */
773 p_es = input_FindES( p_input, i_id );
777 p_es = input_AddES( p_input, p_input->stream.pp_programs[0],
781 p_es->i_stream_id = p_data->p_buffer[3];
783 /* Set stream type and auto-spawn. */
784 if( (i_id & 0xF0) == 0xE0 )
787 p_es->i_type = MPEG2_VIDEO_ES;
789 input_SelectES( p_input, p_es );
792 else if( (i_id & 0xE0) == 0xC0 )
795 p_es->i_type = MPEG2_AUDIO_ES;
797 if( main_GetIntVariable( INPUT_DVD_AUDIO_VAR, 0 )
799 && main_GetIntVariable( INPUT_DVD_CHANNEL_VAR, 0 )
800 == (p_es->i_stream_id & 0x1F) )
802 input_SelectES( p_input, p_es );
806 else if( (i_id & 0xF0FF) == 0x80BD )
808 /* AC3 audio (0x80->0x8F) */
809 p_es->i_type = AC3_AUDIO_ES;
811 if( main_GetIntVariable( INPUT_DVD_AUDIO_VAR, 0 )
813 && main_GetIntVariable( INPUT_DVD_CHANNEL_VAR, 0 )
814 == ((p_es->i_stream_id & 0xF00) >> 8) )
816 input_SelectES( p_input, p_es );
820 else if( (i_id & 0xE0FF) == 0x20BD )
822 /* Subtitles video (0x20->0x3F) */
823 p_es->i_type = DVD_SPU_ES;
825 if( main_GetIntVariable( INPUT_DVD_SUBTITLE_VAR, 0 )
826 == ((p_es->i_stream_id & 0x1F00) >> 8) )
828 input_SelectES( p_input, p_es );
832 else if( (i_id & 0xF0FF) == 0xA0BD )
834 /* LPCM audio (0xA0->0xAF) */
835 p_es->i_type = LPCM_AUDIO_ES;
836 /* FIXME : write the decoder */
840 p_es->i_type = UNKNOWN_ES;
844 } /* stream.b_is_ok */
845 vlc_mutex_unlock( &p_input->stream.stream_lock );
846 } /* i_code > 0xBC */
851 /*****************************************************************************
852 * input_DemuxPS: first step of demultiplexing: the PS header
853 *****************************************************************************/
854 void input_DemuxPS( input_thread_t * p_input, data_packet_t * p_data )
857 boolean_t b_trash = 0;
858 es_descriptor_t * p_es = NULL;
860 i_code = U32_AT( p_data->p_buffer );
861 if( i_code <= 0x1BC )
865 case 0x1BA: /* PACK_START_CODE */
867 /* Convert the SCR in microseconds. */
870 if( (p_data->p_buffer[4] & 0xC0) == 0x40 )
874 (( ((mtime_t)(p_data->p_buffer[4] & 0x38) << 27) |
875 ((mtime_t)(U32_AT(p_data->p_buffer + 4) & 0x03FFF800)
877 ((mtime_t)(U32_AT(p_data->p_buffer + 6) & 0x03FFF800)
883 /* MPEG-1 SCR is like PTS */
885 (( ((mtime_t)(p_data->p_buffer[4] & 0x0E) << 29) |
886 (((mtime_t)U16_AT(p_data->p_buffer + 5) << 14)
888 ((mtime_t)U16_AT(p_data->p_buffer + 7) >> 1)
891 /* Call the pace control. */
892 //intf_Msg("+%lld\n", scr_time);
893 CRDecode( p_input, p_input->stream.pp_programs[0],
899 case 0x1BB: /* SYSTEM_START_CODE */
900 b_trash = 1; /* Nothing interesting */
903 case 0x1BC: /* PROGRAM_STREAM_MAP_CODE */
904 DecodePSM( p_input, p_data );
908 case 0x1B9: /* PROGRAM_END_CODE */
913 /* This should not happen */
915 intf_WarnMsg( 1, "Unwanted packet received with start code %x",
921 p_es = input_ParsePS( p_input, p_data );
923 if( p_es != NULL && p_es->p_decoder_fifo != NULL )
928 input_GatherPES( p_input, p_data, p_es, 1, 0 );
936 /* Trash the packet if it has no payload or if it isn't selected */
939 p_input->p_plugin->pf_delete_packet( p_input, p_data );
941 p_input->c_packets_trashed++;
951 /*****************************************************************************
952 * input_DemuxTS: first step of demultiplexing: the TS header
953 *****************************************************************************/
954 void input_DemuxTS( input_thread_t * p_input, data_packet_t * p_data )
957 boolean_t b_adaptation; /* Adaptation field is present */
958 boolean_t b_payload; /* Packet carries payload */
959 boolean_t b_unit_start; /* A PSI or a PES start in the packet */
960 boolean_t b_trash = 0; /* Is the packet unuseful ? */
961 boolean_t b_lost = 0; /* Was there a packet loss ? */
962 es_descriptor_t * p_es = NULL;
963 es_ts_data_t * p_es_demux = NULL;
964 pgrm_ts_data_t * p_pgrm_demux = NULL;
966 #define p (p_data->p_buffer)
968 //intf_DbgMsg("input debug: TS-demultiplexing packet %p, pid %d\n",
969 // p_ts_packet, U16_AT(&p[1]) & 0x1fff);
971 /* Extract flags values from TS common header. */
972 i_pid = U16_AT(&p[1]) & 0x1fff;
973 b_unit_start = (p[1] & 0x40);
974 b_adaptation = (p[3] & 0x20);
975 b_payload = (p[3] & 0x10);
977 /* Find out the elementary stream. */
978 vlc_mutex_lock( &p_input->stream.stream_lock );
979 p_es = input_FindES( p_input, i_pid );
980 vlc_mutex_unlock( &p_input->stream.stream_lock );
982 if( p_es == NULL || p_es->p_decoder_fifo == NULL )
984 /* Not selected. Just read the adaptation field for a PCR. */
988 if( (p_es->p_decoder_fifo != NULL) || (p_pgrm_demux->i_pcr_pid == i_pid) )
994 /* Extract adaptation field information if any */
997 /* We don't have any adaptation_field, so payload starts
998 * immediately after the 4 byte TS header */
999 p_data->p_payload_start += 4;
1003 /* p[4] is adaptation_field_length minus one */
1004 p_data->p_payload_start += 5 + p[4];
1006 /* The adaptation field can be limited to the
1007 * adaptation_field_length byte, so that there is nothing to do:
1008 * skip this possibility */
1011 /* If the packet has both adaptation_field and payload,
1012 * adaptation_field cannot be more than 182 bytes long; if
1013 * there is only an adaptation_field, it must fill the next
1015 if( b_payload ? (p[4] > 182) : (p[4] != 183) )
1018 "invalid TS adaptation field (%p)",
1020 p_data->b_discard_payload = 1;
1022 p_es->c_invalid_packets++;
1026 /* Now we are sure that the byte containing flags is present:
1030 /* discontinuity_indicator */
1034 "discontinuity_indicator" \
1035 " encountered by TS demux (position read: %d," \
1037 p[5] & 0x80, p_es_demux->i_continuity_counter );
1039 /* If the PID carries the PCR, there will be a system
1040 * time-based discontinuity. We let the PCR decoder
1042 p_es->p_pgrm->b_discontinuity = 1;
1044 /* There also may be a continuity_counter
1045 * discontinuity: resynchronise our counter with
1046 * the one of the stream. */
1047 p_es_demux->i_continuity_counter = (p[3] & 0x0f) - 1;
1050 /* If this is a PCR_PID, and this TS packet contains a
1051 * PCR, we pass it along to the PCR decoder. */
1052 if( (p_pgrm_demux->i_pcr_pid == i_pid) && (p[5] & 0x10) )
1054 /* There should be a PCR field in the packet, check
1055 * if the adaptation field is long enough to carry
1059 /* Convert the PCR in microseconds.
1060 * WARNING: do not remove the casts in the
1061 * following calculation ! */
1064 ( (( (mtime_t)U32_AT((u32*)&p[6]) << 1 )
1065 | ( p[10] >> 7 )) * 300 ) / 27;
1066 /* Call the pace control. */
1067 CRDecode( p_input, p_es->p_pgrm, pcr_time );
1070 } /* valid TS adaptation field ? */
1072 } /* has adaptation field */
1074 /* Check the continuity of the stream. */
1075 i_dummy = ((p[3] & 0x0f) - p_es_demux->i_continuity_counter) & 0x0f;
1078 /* Everything is ok, just increase our counter */
1079 p_es_demux->i_continuity_counter++;
1083 if( !b_payload && i_dummy == 0 )
1085 /* This is a packet without payload, this is allowed by the draft.
1086 * As there is nothing interesting in this packet (except PCR that
1087 * have already been handled), we can trash the packet. */
1089 "Packet without payload received by TS demux" );
1092 else if( i_dummy <= 0 )
1094 /* FIXME: this can never happen, can it ? --Meuuh */
1095 /* Duplicate packet: mark it as being to be trashed. */
1096 intf_WarnMsg( 1, "Duplicate packet received by TS demux" );
1099 else if( p_es_demux->i_continuity_counter == 0xFF )
1101 /* This means that the packet is the first one we receive for this
1102 * ES since the continuity counter ranges between 0 and 0x0F
1103 * excepts when it has been initialized by the input: Init the
1104 * counter to the correct value. */
1105 intf_DbgMsg( "First packet for PID %d received by TS demux",
1107 p_es_demux->i_continuity_counter = (p[3] & 0x0f);
1111 /* This can indicate that we missed a packet or that the
1112 * continuity_counter wrapped and we received a dup packet: as we
1113 * don't know, do as if we missed a packet to be sure to recover
1114 * from this situation */
1116 "Packet lost by TS demux: current %d, packet %d\n",
1117 p_es_demux->i_continuity_counter & 0x0f,
1120 p_es_demux->i_continuity_counter = p[3] & 0x0f;
1121 } /* not continuous */
1123 } /* if selected or PCR */
1125 /* Trash the packet if it has no payload or if it isn't selected */
1128 p_input->p_plugin->pf_delete_packet( p_input, p_data );
1130 p_input->c_packets_trashed++;
1135 if( p_es_demux->b_psi )
1137 /* The payload contains PSI tables */
1139 input_DemuxPSI( p_input, p_data, p_es,
1140 b_unit_start, b_lost );
1145 /* The payload carries a PES stream */
1147 input_GatherPES( p_input, p_data, p_es, b_unit_start, b_lost );