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.10 2000/12/20 16:04:31 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"
45 #include "mpeg_system.h"
47 /*****************************************************************************
49 *****************************************************************************/
53 * PES Packet management
56 /*****************************************************************************
58 *****************************************************************************
59 * Put a PES in the decoder's fifo.
60 *****************************************************************************/
61 void input_DecodePES( input_thread_t * p_input, es_descriptor_t * p_es )
63 #define p_pes (p_es->p_pes)
65 if( p_es->p_decoder_fifo != NULL )
67 vlc_mutex_lock( &p_es->p_decoder_fifo->data_lock );
70 if( p_input->stream.b_pace_control )
72 /* FIXME : normally we shouldn't need this... */
73 while( DECODER_FIFO_ISFULL( *p_es->p_decoder_fifo ) )
75 vlc_mutex_unlock( &p_es->p_decoder_fifo->data_lock );
77 vlc_mutex_lock( &p_es->p_decoder_fifo->data_lock );
82 if( !DECODER_FIFO_ISFULL( *p_es->p_decoder_fifo ) )
84 //intf_DbgMsg("Putting %p into fifo %p/%d\n",
85 // p_pes, p_fifo, p_fifo->i_end);
86 p_es->p_decoder_fifo->buffer[p_es->p_decoder_fifo->i_end] = p_pes;
87 DECODER_FIFO_INCEND( *p_es->p_decoder_fifo );
89 /* Warn the decoder that it's got work to do. */
90 vlc_cond_signal( &p_es->p_decoder_fifo->data_wait );
94 /* The FIFO is full !!! This should not happen. */
95 p_input->p_plugin->pf_delete_pes( p_input->p_method_data, p_pes );
96 intf_ErrMsg( "PES trashed - fifo full ! (%d, %d)",
97 p_es->i_id, p_es->i_type);
99 vlc_mutex_unlock( &p_es->p_decoder_fifo->data_lock );
103 intf_ErrMsg("No fifo to receive PES %p (who wrote this damn code ?)",
105 p_input->p_plugin->pf_delete_pes( p_input->p_method_data, p_pes );
112 /*****************************************************************************
114 *****************************************************************************
115 * Parse a finished PES packet and analyze its header.
116 *****************************************************************************/
117 #define PES_HEADER_SIZE 14
118 void input_ParsePES( input_thread_t * p_input, es_descriptor_t * p_es )
120 data_packet_t * p_header_data;
121 byte_t p_header[PES_HEADER_SIZE];
124 #define p_pes (p_es->p_pes)
126 //intf_DbgMsg("End of PES packet %p\n", p_pes);
128 /* Parse the header. The header has a variable length, but in order
129 * to improve the algorithm, we will read the 14 bytes we may be
131 p_header_data = p_pes->p_first;
136 i_todo = p_header_data->p_payload_end
137 - p_header_data->p_payload_start;
138 if( i_todo > PES_HEADER_SIZE - i_done )
139 i_todo = PES_HEADER_SIZE - i_done;
141 memcpy( p_header + i_done, p_header_data->p_payload_start,
145 if( i_done < PES_HEADER_SIZE && p_header_data->p_next != NULL )
147 p_header_data = p_header_data->p_next;
155 if( i_done != PES_HEADER_SIZE )
157 intf_WarnMsg( 3, "PES packet too short to have a header" );
158 p_input->p_plugin->pf_delete_pes( p_input->p_method_data, p_pes );
163 /* Get the PES size if defined */
164 p_es->i_pes_real_size = U16_AT(p_header + 4) + 6;
166 /* First read the 6 header bytes common to all PES packets:
167 * use them to test the PES validity */
168 if( (p_header[0] || p_header[1] || (p_header[2] != 1)) )
170 /* packet_start_code_prefix != 0x000001 */
171 intf_ErrMsg( "PES packet doesn't start with 0x000001 : data loss" );
172 p_input->p_plugin->pf_delete_pes( p_input->p_method_data, p_pes );
177 int i_pes_header_size, i_payload_size;
179 if ( p_es->i_pes_real_size &&
180 (p_es->i_pes_real_size != p_pes->i_pes_size) )
182 /* PES_packet_length is set and != total received payload */
183 /* Warn the decoder that the data may be corrupt. */
184 intf_WarnMsg( 3, "PES sizes do not match : packet corrupted" );
185 p_pes->b_messed_up = 1;
188 switch( p_es->i_stream_id )
190 case 0xBC: /* Program stream map */
191 case 0xBE: /* Padding */
192 case 0xBF: /* Private stream 2 */
195 case 0xFF: /* Program stream directory */
196 case 0xF2: /* DSMCC stream */
197 case 0xF8: /* ITU-T H.222.1 type E stream */
198 /* The payload begins immediately after the 6 bytes header, so
199 * we have finished with the parsing */
200 i_pes_header_size = 6;
204 if( (p_header[6] & 0xC0) == 0x80 )
206 /* MPEG-2 : the PES header contains at least 3 more bytes. */
207 p_pes->b_data_alignment = p_header[6] & 0x04;
208 p_pes->b_has_pts = p_header[7] & 0x80;
209 i_pes_header_size = p_header[8] + 9;
211 /* Now parse the optional header extensions (in the limit of
213 if( p_pes->b_has_pts )
216 ( ((mtime_t)(p_header[9] & 0x0E) << 29) |
217 (((mtime_t)U16_AT(p_header + 10) << 14) - (1 << 14)) |
218 ((mtime_t)U16_AT(p_header + 12) >> 1) ) * 300;
224 /* Probably MPEG-1 */
226 data_packet_t * p_data;
228 i_pes_header_size = 6;
229 p_data = p_pes->p_first;
230 p_byte = p_data->p_buffer + 6;
231 while( *p_byte == 0xFF && i_pes_header_size < 22 )
235 if( p_byte >= p_data->p_payload_end )
237 p_data = p_data->p_next;
240 intf_ErrMsg( "MPEG-1 packet too short for header" );
241 p_input->p_plugin->pf_delete_pes( p_input->p_method_data, p_pes );
245 p_byte = p_data->p_payload_start;
248 if( i_pes_header_size == 22 )
250 intf_ErrMsg( "Too much MPEG-1 stuffing" );
251 p_input->p_plugin->pf_delete_pes( p_input->p_method_data, p_pes );
256 if( (*p_byte & 0xC0) == 0x40 )
258 /* Don't ask why... --Meuuh */
260 i_pes_header_size += 2;
261 if( p_byte >= p_data->p_payload_end )
263 int i_plus = p_byte - p_data->p_payload_end;
264 p_data = p_data->p_next;
267 intf_ErrMsg( "MPEG-1 packet too short for header" );
268 p_input->p_plugin->pf_delete_pes( p_input->p_method_data, p_pes );
272 p_byte = p_data->p_payload_start + i_plus;
277 p_pes->b_has_pts = *p_byte & 0x20;
282 i_pes_header_size += 5;
290 i_pes_header_size += 4;
292 for( i = 1; i < 5; i++ )
295 if( p_byte >= p_data->p_payload_end )
297 p_data = p_data->p_next;
300 intf_ErrMsg( "MPEG-1 packet too short for header" );
301 p_input->p_plugin->pf_delete_pes( p_input->p_method_data, p_pes );
305 p_byte = p_data->p_payload_start;
311 ( ((mtime_t)(p_pts[0] & 0x0E) << 29) |
312 (((mtime_t)U16_AT(p_pts + 1) << 14) - (1 << 14)) |
313 ((mtime_t)U16_AT(p_pts + 3) >> 1) ) * 300;
319 if( p_pes->b_has_pts )
321 //intf_Msg("%lld\n", p_pes->i_pts);
322 switch( p_es->p_pgrm->i_synchro_state )
324 case SYNCHRO_NOT_STARTED:
326 p_pes->b_has_pts = 0;
329 case SYNCHRO_REINIT: /* We skip a PES | Why ?? --Meuuh */
330 p_pes->b_has_pts = 0;
331 p_es->p_pgrm->i_synchro_state = SYNCHRO_START;
335 p_pes->i_pts += p_es->p_pgrm->delta_cr
336 + p_es->p_pgrm->delta_absolute
344 if( p_es->i_stream_id == 0xBC )
346 /* With private stream 1, the first byte of the payload
347 * is a stream_private_id, so skip it. */
351 /* Now we've parsed the header, we just have to indicate in some
352 * specific data packets where the PES payload begins (renumber
353 * p_payload_start), so that the decoders can find the beginning
354 * of their data right out of the box. */
355 p_header_data = p_pes->p_first;
356 i_payload_size = p_header_data->p_payload_end
357 - p_header_data->p_payload_start;
358 while( i_pes_header_size > i_payload_size )
360 /* These packets are entirely filled by the PES header. */
361 i_pes_header_size -= i_payload_size;
362 p_header_data->p_payload_start = p_header_data->p_payload_end;
363 /* Go to the next data packet. */
364 if( (p_header_data = p_header_data->p_next) == NULL )
366 intf_ErrMsg( "PES header bigger than payload" );
367 p_input->p_plugin->pf_delete_pes( p_input->p_method_data,
372 i_payload_size = p_header_data->p_payload_end
373 - p_header_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->p_plugin->pf_delete_pes( p_input->p_method_data, p_pes );
383 p_header_data->p_payload_start += i_pes_header_size;
385 /* Now we can eventually put the PES packet in the decoder's
387 input_DecodePES( p_input, p_es );
392 /*****************************************************************************
394 *****************************************************************************
395 * Gather a PES packet.
396 *****************************************************************************/
397 void input_GatherPES( input_thread_t * p_input, data_packet_t * p_data,
398 es_descriptor_t * p_es,
399 boolean_t b_unit_start, boolean_t b_packet_lost )
401 #define p_pes (p_es->p_pes)
403 //intf_DbgMsg("PES-demultiplexing %p (%p)\n", p_ts_packet, p_pes);
405 /* If we lost data, insert an NULL data packet (philosophy : 0 is quite
406 * often an escape sequence in decoders, so that should make them wait
407 * for the next start code). */
408 if( b_packet_lost || p_es->b_discontinuity )
410 data_packet_t * p_pad_data;
412 if( (p_pad_data = p_input->p_plugin->pf_new_packet(
413 p_input->p_method_data,
414 PADDING_PACKET_SIZE )) == NULL )
416 intf_ErrMsg("Out of memory\n");
417 p_input->b_error = 1;
420 memset( p_data->p_buffer, 0, PADDING_PACKET_SIZE );
421 p_pad_data->b_discard_payload = 1;
425 p_pes->b_messed_up = p_pes->b_discontinuity = 1;
426 input_GatherPES( p_input, p_pad_data, p_es, 0, 0 );
430 if( (p_pes = p_input->p_plugin->pf_new_pes(
431 p_input->p_method_data )) == NULL )
433 intf_ErrMsg("Out of memory\n");
434 p_input->b_error = 1;
438 p_pes->p_first = p_pad_data;
439 p_pes->b_messed_up = p_pes->b_discontinuity = 1;
440 input_DecodePES( p_input, p_es );
443 p_es->b_discontinuity = 0;
446 if( b_unit_start && p_pes != NULL )
448 /* If the TS packet contains the begining of a new PES packet, and
449 * if we were reassembling a PES packet, then the PES should be
450 * complete now, so parse its header and give it to the decoders. */
451 input_ParsePES( p_input, p_es );
454 if( !b_unit_start && p_pes == NULL )
456 /* Random access... */
457 p_input->p_plugin->pf_delete_packet( p_input->p_method_data, p_data );
463 /* If we are at the beginning of a new PES packet, we must fetch
464 * a new PES buffer to begin with the reassembly of this PES
465 * packet. This is also here that we can synchronize with the
466 * stream if we lost packets or if the decoder has just
468 if( (p_pes = p_input->p_plugin->pf_new_pes( p_input->p_method_data ) ) == NULL )
470 intf_ErrMsg("Out of memory");
471 p_input->b_error = 1;
474 //intf_DbgMsg("New PES packet %p (first data: %p)\n", p_pes, p_data);
475 p_pes->p_first = p_data;
477 /* If the PES header fits in the first data packet, we can
478 * already set p_gather->i_pes_real_size. */
479 if( p_data->p_payload_end - p_data->p_payload_start
482 p_es->i_pes_real_size =
483 U16_AT(p_data->p_payload_start + 4) + 6;
487 p_es->i_pes_real_size = 0;
492 /* Update the relations between the data packets */
493 p_es->p_last->p_next = p_data;
496 p_data->p_next = NULL;
497 p_es->p_last = p_data;
499 /* Size of the payload carried in the data packet */
500 p_pes->i_pes_size += (p_data->p_payload_end
501 - p_data->p_payload_start);
503 /* We can check if the packet is finished */
504 if( p_pes->i_pes_size == p_es->i_pes_real_size )
506 /* The packet is finished, parse it */
507 input_ParsePES( p_input, p_es );
519 * DISCUSSION : SYNCHRONIZATION METHOD
521 * In some cases we can impose the pace of reading (when reading from a
522 * file or a pipe), and for the synchronization we simply sleep() until
523 * it is time to deliver the packet to the decoders. When reading from
524 * the network, we must be read at the same pace as the server writes,
525 * otherwise the kernel's buffer will trash packets. The risk is now to
526 * overflow the input buffers in case the server goes too fast, that is
527 * why we do these calculations :
529 * We compute an average for the pcr because we want to eliminate the
530 * network jitter and keep the low frequency variations. The average is
531 * in fact a low pass filter and the jitter is a high frequency signal
532 * that is why it is eliminated by the filter/average.
534 * The low frequency variations enable us to synchronize the client clock
535 * with the server clock because they represent the time variation between
536 * the 2 clocks. Those variations (ie the filtered pcr) are used to compute
537 * the presentation dates for the audio and video frames. With those dates
538 * we can decode (or trash) the MPEG2 stream at "exactly" the same rate
539 * as it is sent by the server and so we keep the synchronization between
540 * the server and the client.
542 * It is a very important matter if you want to avoid underflow or overflow
543 * in all the FIFOs, but it may be not enough.
546 /*****************************************************************************
548 *****************************************************************************/
550 /* Maximum number of samples used to compute the dynamic average value,
551 * it is also the maximum of c_average_count in pgrm_ts_data_t.
552 * We use the following formula :
553 * new_average = (old_average * c_average + new_sample_value) / (c_average +1) */
554 #define CR_MAX_AVERAGE_COUNTER 40
556 /* Maximum gap allowed between two CRs. */
557 #define CR_MAX_GAP 1000000
559 /*****************************************************************************
560 * CRReInit : Reinitialize the clock reference
561 *****************************************************************************/
562 static void CRReInit( pgrm_descriptor_t * p_pgrm )
564 p_pgrm->delta_cr = 0;
566 p_pgrm->c_average_count = 0;
569 /* FIXME: find a better name */
570 /*****************************************************************************
571 * CRDecode : Decode a clock reference
572 *****************************************************************************/
573 static void CRDecode( input_thread_t * p_input, pgrm_descriptor_t * p_pgrm,
576 if( p_pgrm->i_synchro_state != SYNCHRO_OK )
578 switch( p_pgrm->i_synchro_state )
581 p_pgrm->delta_absolute = mdate() - cr_time;
582 p_pgrm->i_synchro_state = SYNCHRO_OK;
585 case SYNCHRO_NOT_STARTED:
586 p_pgrm->i_synchro_state = SYNCHRO_START;
595 if( p_pgrm->b_discontinuity ||
596 ( p_pgrm->last_cr != 0 &&
597 ( (p_pgrm->last_cr - cr_time) > CR_MAX_GAP
598 || (p_pgrm->last_cr - cr_time) < - CR_MAX_GAP ) ) )
602 /* Stream discontinuity. */
603 intf_WarnMsg( 3, "CR re-initialiazed" );
605 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
606 p_pgrm->b_discontinuity = 0;
608 /* Warn all the elementary streams */
609 for( i_es = 0; i_es < p_pgrm->i_es_number; i_es++ )
611 p_pgrm->pp_es[i_es]->b_discontinuity = 1;
614 p_pgrm->last_cr = cr_time;
616 if( p_input->stream.b_pace_control )
618 /* Wait a while before delivering the packets to the decoder. */
619 mwait( cr_time + p_pgrm->delta_absolute );
623 mtime_t sys_time, delta_cr;
626 delta_cr = sys_time - cr_time;
628 if( p_pgrm->c_average_count == CR_MAX_AVERAGE_COUNTER )
630 p_pgrm->delta_cr = ( delta_cr + (p_pgrm->delta_cr
631 * (CR_MAX_AVERAGE_COUNTER - 1)) )
632 / CR_MAX_AVERAGE_COUNTER;
636 p_pgrm->delta_cr = ( delta_cr + (p_pgrm->delta_cr
637 * p_pgrm->c_average_count) )
638 / ( p_pgrm->c_average_count + 1 );
639 p_pgrm->c_average_count++;
650 /*****************************************************************************
651 * GetID: Get the ID of a stream
652 *****************************************************************************/
653 static u16 GetID( data_packet_t * p_data )
657 i_id = p_data->p_buffer[3]; /* stream_id */
660 /* stream_private_id */
661 i_id |= p_data->p_buffer[ 9 + p_data->p_buffer[8] ] << 8;
666 /*****************************************************************************
667 * DecodePSM: Decode the Program Stream Map information
668 *****************************************************************************/
669 static void DecodePSM( input_thread_t * p_input, data_packet_t * p_data )
671 stream_ps_data_t * p_demux =
672 (stream_ps_data_t *)p_input->stream.p_demux_data;
674 if( !p_demux->b_is_PSM_complete )
680 intf_DbgMsg( "Building PSM" );
681 if( p_data->p_payload_start + 10 > p_data->p_payload_end )
683 intf_ErrMsg( "PSM too short : packet corrupt" );
686 /* Go to elementary_stream_map_length, jumping over
687 * program_stream_info. */
688 p_byte = p_data->p_payload_start + 10
689 + U16_AT(&p_data->p_payload_start[8]);
690 if( p_byte > p_data->p_payload_end )
692 intf_ErrMsg( "PSM too short : packet corrupt" );
695 /* This is the full size of the elementary_stream_map.
696 * 2 == elementary_stream_map_length
698 p_end = p_byte + 2 + U16_AT(p_byte) - 4;
700 if( p_end > p_data->p_payload_end )
702 intf_ErrMsg( "PSM too short : packet corrupt" );
706 vlc_mutex_lock( &p_input->stream.stream_lock );
708 /* 4 == minimum useful size of a section */
709 while( p_byte + 4 <= p_end )
711 p_input->p_es[i_es].i_id
712 = p_input->p_es[i_es].i_stream_id
714 p_input->p_es[i_es].i_type = p_byte[0];
715 p_input->p_es[i_es].p_pgrm = p_input->stream.pp_programs[0];
716 p_input->p_es[i_es].p_pes = NULL;
717 p_byte += 4 + U16_AT(&p_byte[2]);
720 switch( p_input->p_es[i_es].i_type )
724 /* Spawn audio thread. */
725 intf_DbgMsg( "Starting an MPEG-audio decoder" );
730 /* Spawn video thread. */
731 intf_DbgMsg( "Starting an MPEG-video decoder" );
739 vlc_mutex_unlock( &p_input->stream.stream_lock );
740 p_demux->i_PSM_version = p_data->p_buffer[6] & 0x1F;
741 p_demux->b_is_PSM_complete = 1;
743 else if( p_demux->i_PSM_version != (p_data->p_buffer[6] & 0x1F) )
746 intf_ErrMsg( "PSM changed, this is not supported yet !" );
747 p_demux->i_PSM_version = p_data->p_buffer[6] & 0x1F;
751 /*****************************************************************************
752 * input_ParsePS: read the PS header
753 *****************************************************************************/
754 es_descriptor_t * input_ParsePS( input_thread_t * p_input,
755 data_packet_t * p_data )
758 es_descriptor_t * p_es = NULL;
760 i_code = U32_AT( p_data->p_buffer );
761 if( i_code > 0x1BC ) /* ES start code */
766 /* This is a PES packet. Find out if we want it or not. */
767 i_id = GetID( p_data );
769 vlc_mutex_lock( &p_input->stream.stream_lock );
770 if( p_input->stream.pp_programs[0]->b_is_ok )
772 /* Look only at the selected ES. */
773 for( i_dummy = 0; i_dummy < INPUT_MAX_SELECTED_ES; i_dummy++ )
775 if( p_input->pp_selected_es[i_dummy] != NULL
776 && p_input->pp_selected_es[i_dummy]->i_id == i_id )
778 p_es = p_input->pp_selected_es[i_dummy];
785 /* Search all ES ; if not found -> AddES */
786 for( i_dummy = 0; i_dummy < INPUT_MAX_ES; i_dummy++ )
788 if( p_input->p_es[i_dummy].i_id != EMPTY_ID
789 && p_input->p_es[i_dummy].i_id == i_id )
791 p_es = &p_input->p_es[i_dummy];
798 p_es = input_AddES( p_input, p_input->stream.pp_programs[0],
802 p_es->i_stream_id = p_data->p_buffer[3];
804 /* Set stream type and auto-spawn. */
805 if( (i_id & 0xF0) == 0xE0 )
808 p_es->i_type = MPEG2_VIDEO_ES;
810 input_SelectES( p_input, p_es );
813 else if( (i_id & 0xE0) == 0xC0 )
816 p_es->i_type = MPEG2_AUDIO_ES;
818 input_SelectES( p_input, p_es );
821 else if( (i_id & 0xF0FF) == 0x80BD )
824 p_es->i_type = AC3_AUDIO_ES;
826 input_SelectES( p_input, p_es );
829 else if( (i_id & 0xF0FF) == 0x20BD )
831 /* Subtitles video */
832 p_es->i_type = DVD_SPU_ES;
834 input_SelectES( p_input, p_es );
839 p_es->i_type = UNKNOWN_ES;
843 } /* stream.b_is_ok */
844 vlc_mutex_unlock( &p_input->stream.stream_lock );
845 } /* i_code > 0xBC */
850 /*****************************************************************************
851 * input_DemuxPS: first step of demultiplexing: the PS header
852 *****************************************************************************/
853 void input_DemuxPS( input_thread_t * p_input, data_packet_t * p_data )
856 boolean_t b_trash = 0;
857 es_descriptor_t * p_es = NULL;
859 i_code = U32_AT( p_data->p_buffer );
860 if( i_code <= 0x1BC )
864 case 0x1BA: /* PACK_START_CODE */
866 /* Convert the SCR in microseconds. */
869 if( (p_data->p_buffer[4] & 0xC0) == 0x40 )
873 (( ((mtime_t)(p_data->p_buffer[4] & 0x38) << 27) |
874 ((mtime_t)(U32_AT(p_data->p_buffer + 4) & 0x03FFF800)
876 ((mtime_t)(U32_AT(p_data->p_buffer + 6) & 0x03FFF800)
882 /* MPEG-1 SCR is like PTS */
884 (( ((mtime_t)(p_data->p_buffer[4] & 0x0E) << 29) |
885 (((mtime_t)U16_AT(p_data->p_buffer + 5) << 14)
887 ((mtime_t)U16_AT(p_data->p_buffer + 7) >> 1)
890 /* Call the pace control. */
891 //intf_Msg("+%lld\n", scr_time);
892 CRDecode( p_input, p_input->stream.pp_programs[0],
898 case 0x1BB: /* SYSTEM_START_CODE */
899 b_trash = 1; /* Nothing interesting */
902 case 0x1BC: /* PROGRAM_STREAM_MAP_CODE */
903 intf_ErrMsg("meuuuuh\n");
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 && !b_trash )
928 input_GatherPES( p_input, p_data, p_es, 1, 0 );
932 /* Trash the packet if it has no payload or if it isn't selected */
935 p_input->p_plugin->pf_delete_packet( p_input, p_data );
937 p_input->c_packets_trashed++;
947 /*****************************************************************************
948 * input_DemuxTS: first step of demultiplexing: the TS header
949 *****************************************************************************/
950 void input_DemuxTS( input_thread_t * p_input, data_packet_t * p_data )
953 boolean_t b_adaptation; /* Adaptation field is present */
954 boolean_t b_payload; /* Packet carries payload */
955 boolean_t b_unit_start; /* A PSI or a PES start in the packet */
956 boolean_t b_trash = 0; /* Is the packet unuseful ? */
957 boolean_t b_lost = 0; /* Was there a packet loss ? */
958 es_descriptor_t * p_es = NULL;
959 es_ts_data_t * p_es_demux = NULL;
960 pgrm_ts_data_t * p_pgrm_demux = NULL;
962 #define p (p_data->p_buffer)
964 //intf_DbgMsg("input debug: TS-demultiplexing packet %p, pid %d\n",
965 // p_ts_packet, U16_AT(&p[1]) & 0x1fff);
967 /* Extract flags values from TS common header. */
968 i_pid = U16_AT(&p[1]) & 0x1fff;
969 b_unit_start = (p[1] & 0x40);
970 b_adaptation = (p[3] & 0x20);
971 b_payload = (p[3] & 0x10);
973 /* Find out the elementary stream. */
974 vlc_mutex_lock( &p_input->stream.stream_lock );
975 for( i_dummy = 0; i_dummy < INPUT_MAX_ES; i_dummy++ )
977 if( p_input->p_es[i_dummy].i_id != EMPTY_ID )
979 if( p_input->p_es[i_dummy].i_id == i_pid )
981 p_es = &p_input->p_es[i_dummy];
982 p_es_demux = (es_ts_data_t *)p_es->p_demux_data;
983 p_pgrm_demux = (pgrm_ts_data_t *)p_es->p_pgrm->p_demux_data;
988 vlc_mutex_unlock( &p_input->stream.stream_lock );
994 if( p_es->p_decoder_fifo == NULL )
996 /* Not selected. Just read the adaptation field for a PCR. */
1000 if( (p_es->p_decoder_fifo != NULL) || (p_pgrm_demux->i_pcr_pid == i_pid) )
1002 /* Extract adaptation field information if any */
1005 /* We don't have any adaptation_field, so payload starts
1006 * immediately after the 4 byte TS header */
1007 p_data->p_payload_start += 4;
1011 /* p[4] is adaptation_field_length minus one */
1012 p_data->p_payload_start += 5 + p[4];
1014 /* The adaptation field can be limited to the
1015 * adaptation_field_length byte, so that there is nothing to do:
1016 * skip this possibility */
1019 /* If the packet has both adaptation_field and payload,
1020 * adaptation_field cannot be more than 182 bytes long; if
1021 * there is only an adaptation_field, it must fill the next
1023 if( b_payload ? (p[4] > 182) : (p[4] != 183) )
1026 "invalid TS adaptation field (%p)",
1028 p_data->b_discard_payload = 1;
1030 p_es->c_invalid_packets++;
1034 /* Now we are sure that the byte containing flags is present:
1038 /* discontinuity_indicator */
1042 "discontinuity_indicator" \
1043 " encountered by TS demux (position read: %d," \
1045 p[5] & 0x80, p_es_demux->i_continuity_counter );
1047 /* If the PID carries the PCR, there will be a system
1048 * time-based discontinuity. We let the PCR decoder
1050 p_es->p_pgrm->b_discontinuity = 1;
1052 /* There also may be a continuity_counter
1053 * discontinuity: resynchronise our counter with
1054 * the one of the stream. */
1055 p_es_demux->i_continuity_counter = (p[3] & 0x0f) - 1;
1058 /* If this is a PCR_PID, and this TS packet contains a
1059 * PCR, we pass it along to the PCR decoder. */
1060 if( (p_pgrm_demux->i_pcr_pid == i_pid) && (p[5] & 0x10) )
1062 /* There should be a PCR field in the packet, check
1063 * if the adaptation field is long enough to carry
1067 /* Convert the PCR in microseconds.
1068 * WARNING: do not remove the casts in the
1069 * following calculation ! */
1072 ( (( (mtime_t)U32_AT((u32*)&p[6]) << 1 )
1073 | ( p[10] >> 7 )) * 300 ) / 27;
1074 /* Call the pace control. */
1075 CRDecode( p_input, p_es->p_pgrm, pcr_time );
1078 } /* valid TS adaptation field ? */
1080 } /* has adaptation field */
1082 /* Check the continuity of the stream. */
1083 i_dummy = ((p[3] & 0x0f) - p_es_demux->i_continuity_counter) & 0x0f;
1086 /* Everything is ok, just increase our counter */
1087 p_es_demux->i_continuity_counter++;
1091 if( !b_payload && i_dummy == 0 )
1093 /* This is a packet without payload, this is allowed by the draft.
1094 * As there is nothing interesting in this packet (except PCR that
1095 * have already been handled), we can trash the packet. */
1097 "Packet without payload received by TS demux" );
1100 else if( i_dummy <= 0 )
1102 /* FIXME: this can never happen, can it ? --Meuuh */
1103 /* Duplicate packet: mark it as being to be trashed. */
1104 intf_WarnMsg( 1, "Duplicate packet received by TS demux" );
1107 else if( p_es_demux->i_continuity_counter == 0xFF )
1109 /* This means that the packet is the first one we receive for this
1110 * ES since the continuity counter ranges between 0 and 0x0F
1111 * excepts when it has been initialized by the input: Init the
1112 * counter to the correct value. */
1113 intf_DbgMsg( "First packet for PID %d received by TS demux",
1115 p_es_demux->i_continuity_counter = (p[3] & 0x0f);
1119 /* This can indicate that we missed a packet or that the
1120 * continuity_counter wrapped and we received a dup packet: as we
1121 * don't know, do as if we missed a packet to be sure to recover
1122 * from this situation */
1124 "Packet lost by TS demux: current %d, packet %d\n",
1125 p_es_demux->i_continuity_counter & 0x0f,
1128 p_es_demux->i_continuity_counter = p[3] & 0x0f;
1129 } /* not continuous */
1131 } /* if selected or PCR */
1133 /* Trash the packet if it has no payload or if it isn't selected */
1136 p_input->p_plugin->pf_delete_packet( p_input, p_data );
1138 p_input->c_packets_trashed++;
1143 if( p_es_demux->b_psi )
1145 /* The payload contains PSI tables */
1147 input_DemuxPSI( p_input, p_data, p_es,
1148 b_unit_start, b_lost );
1153 /* The payload carries a PES stream */
1155 input_GatherPES( p_input, p_data, p_es, b_unit_start, b_lost );