1 /*****************************************************************************
2 * mpeg_system.c: TS, PS and PES management
3 *****************************************************************************
4 * Copyright (C) 1998, 1999, 2000 VideoLAN
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
29 #include <netinet/in.h>
38 #include "stream_control.h"
39 #include "input_ext-intf.h"
40 #include "input_ext-dec.h"
44 #include "mpeg_system.h"
46 /*****************************************************************************
48 *****************************************************************************/
52 * PES Packet management
55 /*****************************************************************************
57 *****************************************************************************
58 * Put a PES in the decoder's fifo.
59 *****************************************************************************/
60 void input_DecodePES( input_thread_t * p_input, es_descriptor_t * p_es )
62 #define p_pes (p_es->p_pes)
64 /* FIXME: since we don't check the type of the stream anymore, we don't
65 * do the following : p_data->p_payload_start++; for DVD_SPU_ES, and
66 * DVD SPU support is BROKEN ! */
68 if( p_es->p_decoder_fifo != NULL )
70 vlc_mutex_lock( &p_es->p_decoder_fifo->data_lock );
73 if( p_input->stream.b_pace_control )
75 /* FIXME : normally we shouldn't need this... */
76 while( DECODER_FIFO_ISFULL( *p_es->p_decoder_fifo ) )
78 vlc_mutex_unlock( &p_es->p_decoder_fifo->data_lock );
80 vlc_mutex_lock( &p_es->p_decoder_fifo->data_lock );
85 if( !DECODER_FIFO_ISFULL( *p_es->p_decoder_fifo ) )
87 //intf_DbgMsg("Putting %p into fifo %p/%d\n",
88 // p_pes, p_fifo, p_fifo->i_end);
89 p_es->p_decoder_fifo->buffer[p_es->p_decoder_fifo->i_end] = p_pes;
90 DECODER_FIFO_INCEND( *p_es->p_decoder_fifo );
92 /* Warn the decoder that it's got work to do. */
93 vlc_cond_signal( &p_es->p_decoder_fifo->data_wait );
97 /* The FIFO is full !!! This should not happen. */
98 p_input->p_plugin->pf_delete_pes( p_input->p_method_data, p_pes );
99 intf_ErrMsg( "PES trashed - fifo full ! (%d, %d)",
100 p_es->i_id, p_es->i_type);
102 vlc_mutex_unlock( &p_es->p_decoder_fifo->data_lock );
106 intf_ErrMsg("No fifo to receive PES %p (who wrote this damn code ?)",
108 p_input->p_plugin->pf_delete_pes( p_input->p_method_data, p_pes );
115 /*****************************************************************************
117 *****************************************************************************
118 * Parse a finished PES packet and analyze its header.
119 *****************************************************************************/
120 #define PES_HEADER_SIZE 14
121 void input_ParsePES( input_thread_t * p_input, es_descriptor_t * p_es )
123 data_packet_t * p_header_data;
124 byte_t p_header[PES_HEADER_SIZE];
127 #define p_pes (p_es->p_pes)
129 //intf_DbgMsg("End of PES packet %p\n", p_pes);
131 /* Parse the header. The header has a variable length, but in order
132 * to improve the algorithm, we will read the 14 bytes we may be
134 p_header_data = p_pes->p_first;
139 i_todo = p_header_data->p_payload_end
140 - p_header_data->p_payload_start;
141 if( i_todo > PES_HEADER_SIZE - i_done )
142 i_todo = PES_HEADER_SIZE - i_done;
144 memcpy( p_header + i_done, p_header_data->p_payload_start,
148 if( i_done < PES_HEADER_SIZE && p_header_data->p_next != NULL )
150 p_header_data = p_header_data->p_next;
158 if( i_done != PES_HEADER_SIZE )
160 intf_WarnMsg( 3, "PES packet too short to have a header" );
161 p_input->p_plugin->pf_delete_pes( p_input->p_method_data, p_pes );
166 /* Get the PES size if defined */
167 p_es->i_pes_real_size = U16_AT(p_header + 4) + 6;
169 /* First read the 6 header bytes common to all PES packets:
170 * use them to test the PES validity */
171 if( (p_header[0] || p_header[1] || (p_header[2] != 1)) )
173 /* packet_start_code_prefix != 0x000001 */
174 intf_ErrMsg( "PES packet doesn't start with 0x000001 : data loss" );
175 p_input->p_plugin->pf_delete_pes( p_input->p_method_data, p_pes );
180 int i_pes_header_size, i_payload_size;
182 if ( p_es->i_pes_real_size &&
183 (p_es->i_pes_real_size != p_pes->i_pes_size) )
185 /* PES_packet_length is set and != total received payload */
186 /* Warn the decoder that the data may be corrupt. */
187 intf_WarnMsg( 3, "PES sizes do not match : packet corrupted" );
188 p_pes->b_messed_up = 1;
191 switch( p_es->i_stream_id )
193 case 0xBC: /* Program stream map */
194 case 0xBE: /* Padding */
195 case 0xBF: /* Private stream 2 */
198 case 0xFF: /* Program stream directory */
199 case 0xF2: /* DSMCC stream */
200 case 0xF8: /* ITU-T H.222.1 type E stream */
201 /* The payload begins immediately after the 6 bytes header, so
202 * we have finished with the parsing */
203 i_pes_header_size = 6;
207 if( (p_header[6] & 0xC0) == 0x80 )
209 /* MPEG-2 : the PES header contains at least 3 more bytes. */
210 p_pes->b_data_alignment = p_header[6] & 0x04;
211 p_pes->b_has_pts = p_header[7] & 0x80;
212 i_pes_header_size = p_header[8] + 9;
214 /* Now parse the optional header extensions (in the limit of
216 if( p_pes->b_has_pts )
219 ( ((mtime_t)(p_header[9] & 0x0E) << 29) |
220 (((mtime_t)U16_AT(p_header + 10) << 14) - (1 << 14)) |
221 ((mtime_t)U16_AT(p_header + 12) >> 1) ) * 300;
227 /* Probably MPEG-1 */
229 data_packet_t * p_data;
231 i_pes_header_size = 6;
232 p_data = p_pes->p_first;
233 p_byte = p_data->p_buffer + 6;
234 while( *p_byte == 0xFF && i_pes_header_size < 22 )
238 if( p_byte >= p_data->p_payload_end )
240 p_data = p_data->p_next;
243 intf_ErrMsg( "MPEG-1 packet too short for header" );
244 p_input->p_plugin->pf_delete_pes( p_input->p_method_data, p_pes );
248 p_byte = p_data->p_payload_start;
251 if( i_pes_header_size == 22 )
253 intf_ErrMsg( "Too much MPEG-1 stuffing" );
254 p_input->p_plugin->pf_delete_pes( p_input->p_method_data, p_pes );
259 if( (*p_byte & 0xC0) == 0x40 )
261 /* Don't ask why... --Meuuh */
263 i_pes_header_size += 2;
264 if( p_byte >= p_data->p_payload_end )
266 int i_plus = p_byte - p_data->p_payload_end;
267 p_data = p_data->p_next;
270 intf_ErrMsg( "MPEG-1 packet too short for header" );
271 p_input->p_plugin->pf_delete_pes( p_input->p_method_data, p_pes );
275 p_byte = p_data->p_payload_start + i_plus;
280 p_pes->b_has_pts = *p_byte & 0x20;
285 i_pes_header_size += 5;
293 i_pes_header_size += 4;
295 for( i = 1; i < 5; i++ )
298 if( p_byte >= p_data->p_payload_end )
300 p_data = p_data->p_next;
303 intf_ErrMsg( "MPEG-1 packet too short for header" );
304 p_input->p_plugin->pf_delete_pes( p_input->p_method_data, p_pes );
308 p_byte = p_data->p_payload_start;
314 ( ((mtime_t)(p_pts[0] & 0x0E) << 29) |
315 (((mtime_t)U16_AT(p_pts + 1) << 14) - (1 << 14)) |
316 ((mtime_t)U16_AT(p_pts + 3) >> 1) ) * 300;
322 if( p_pes->b_has_pts )
324 //intf_Msg("%lld\n", p_pes->i_pts);
325 switch( p_es->p_pgrm->i_synchro_state )
327 case SYNCHRO_NOT_STARTED:
329 p_pes->b_has_pts = 0;
332 case SYNCHRO_REINIT: /* We skip a PES | Why ?? --Meuuh */
333 p_pes->b_has_pts = 0;
334 p_es->p_pgrm->i_synchro_state = SYNCHRO_START;
338 p_pes->i_pts += p_es->p_pgrm->delta_cr
339 + p_es->p_pgrm->delta_absolute
347 /* Now we've parsed the header, we just have to indicate in some
348 * specific data packets where the PES payload begins (renumber
349 * p_payload_start), so that the decoders can find the beginning
350 * of their data right out of the box. */
351 p_header_data = p_pes->p_first;
352 i_payload_size = p_header_data->p_payload_end
353 - p_header_data->p_payload_start;
354 while( i_pes_header_size > i_payload_size )
356 /* These packets are entirely filled by the PES header. */
357 i_pes_header_size -= i_payload_size;
358 p_header_data->p_payload_start = p_header_data->p_payload_end;
359 /* Go to the next data packet. */
360 if( (p_header_data = p_header_data->p_next) == NULL )
362 intf_ErrMsg( "PES header bigger than payload" );
363 p_input->p_plugin->pf_delete_pes( p_input->p_method_data,
368 i_payload_size = p_header_data->p_payload_end
369 - p_header_data->p_payload_start;
371 /* This last packet is partly header, partly payload. */
372 if( i_payload_size < i_pes_header_size )
374 intf_ErrMsg( "PES header bigger than payload" );
375 p_input->p_plugin->pf_delete_pes( p_input->p_method_data, p_pes );
379 p_header_data->p_payload_start += i_pes_header_size;
381 /* Now we can eventually put the PES packet in the decoder's
383 input_DecodePES( p_input, p_es );
388 /*****************************************************************************
390 *****************************************************************************
391 * Gather a PES packet.
392 *****************************************************************************/
393 void input_GatherPES( input_thread_t * p_input, data_packet_t *p_data,
394 es_descriptor_t * p_es,
395 boolean_t b_unit_start, boolean_t b_packet_lost )
397 #define p_pes (p_es->p_pes)
399 //intf_DbgMsg("PES-demultiplexing %p (%p)\n", p_ts_packet, p_pes);
401 /* If we lost data, insert an NULL data packet (philosophy : 0 is quite
402 * often an escape sequence in decoders, so that should make them wait
403 * for the next start code). */
404 if( b_packet_lost && p_pes != NULL )
406 data_packet_t * p_pad_data;
407 if( (p_pad_data = p_input->p_plugin->pf_new_packet( p_input,
408 PADDING_PACKET_SIZE )) == NULL )
410 intf_ErrMsg("Out of memory\n");
411 p_input->b_error = 1;
414 memset( p_data->p_buffer, 0, PADDING_PACKET_SIZE );
415 p_pad_data->b_discard_payload = 1;
416 p_pes->b_messed_up = 1;
417 input_GatherPES( p_input, p_pad_data, p_es, 0, 0 );
420 if( b_unit_start && p_pes != NULL )
422 /* If the TS packet contains the begining of a new PES packet, and
423 * if we were reassembling a PES packet, then the PES should be
424 * complete now, so parse its header and give it to the decoders. */
425 input_ParsePES( p_input, p_es );
428 if( !b_unit_start && p_pes == NULL )
430 /* Random access... */
431 p_input->p_plugin->pf_delete_packet( p_input->p_method_data, p_data );
437 /* If we are at the beginning of a new PES packet, we must fetch
438 * a new PES buffer to begin with the reassembly of this PES
439 * packet. This is also here that we can synchronize with the
440 * stream if we lost packets or if the decoder has just
442 if( (p_pes = (pes_packet_t *)malloc( sizeof(pes_packet_t) )) == NULL )
444 intf_ErrMsg("Out of memory");
445 p_input->b_error = 1;
448 //intf_DbgMsg("New PES packet %p (first data: %p)\n", p_pes, p_data);
450 /* Init the PES fields so that the first data packet could be
451 * correctly added to the PES packet (see below). */
452 p_pes->p_first = p_data;
453 p_pes->b_messed_up = p_pes->b_discontinuity = 0;
454 p_pes->i_pes_size = 0;
456 /* If the PES header fits in the first data packet, we can
457 * already set p_gather->i_pes_real_size. */
458 if( p_data->p_payload_end - p_data->p_payload_start
461 p_es->i_pes_real_size =
462 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_data->p_next = NULL;
476 p_es->p_last = p_data;
478 /* Size of the payload carried in the data packet */
479 p_pes->i_pes_size += (p_data->p_payload_end
480 - p_data->p_payload_start);
482 /* We can check if the packet is finished */
483 if( p_pes->i_pes_size == p_es->i_pes_real_size )
485 /* The packet is finished, parse it */
486 input_ParsePES( p_input, p_es );
498 * DISCUSSION : SYNCHRONIZATION METHOD
500 * In some cases we can impose the pace of reading (when reading from a
501 * file or a pipe), and for the synchronization we simply sleep() until
502 * it is time to deliver the packet to the decoders. When reading from
503 * the network, we must be read at the same pace as the server writes,
504 * otherwise the kernel's buffer will trash packets. The risk is now to
505 * overflow the input buffers in case the server goes too fast, that is
506 * why we do these calculations :
508 * We compute an average for the pcr because we want to eliminate the
509 * network jitter and keep the low frequency variations. The average is
510 * in fact a low pass filter and the jitter is a high frequency signal
511 * that is why it is eliminated by the filter/average.
513 * The low frequency variations enable us to synchronize the client clock
514 * with the server clock because they represent the time variation between
515 * the 2 clocks. Those variations (ie the filtered pcr) are used to compute
516 * the presentation dates for the audio and video frames. With those dates
517 * we can decode (or trash) the MPEG2 stream at "exactly" the same rate
518 * as it is sent by the server and so we keep the synchronization between
519 * the server and the client.
521 * It is a very important matter if you want to avoid underflow or overflow
522 * in all the FIFOs, but it may be not enough.
525 /*****************************************************************************
527 *****************************************************************************/
529 /* Maximum number of samples used to compute the dynamic average value,
530 * it is also the maximum of c_average_count in pgrm_ts_data_t.
531 * We use the following formula :
532 * new_average = (old_average * c_average + new_sample_value) / (c_average +1) */
533 #define CR_MAX_AVERAGE_COUNTER 40
535 /* Maximum gap allowed between two CRs. */
536 #define CR_MAX_GAP 1000000
538 /*****************************************************************************
539 * CRReInit : Reinitialize the clock reference
540 *****************************************************************************/
541 static void CRReInit( pgrm_descriptor_t * p_pgrm )
543 p_pgrm->delta_cr = 0;
545 p_pgrm->c_average_count = 0;
548 /* FIXME: find a better name */
549 /*****************************************************************************
550 * CRDecode : Decode a clock reference
551 *****************************************************************************/
552 static void CRDecode( input_thread_t * p_input, es_descriptor_t * p_es,
555 pgrm_descriptor_t * p_pgrm;
558 p_pgrm = p_es->p_pgrm;
562 p_pgrm = p_input->stream.pp_programs[0];
565 if( p_pgrm->i_synchro_state != SYNCHRO_OK )
567 switch( p_pgrm->i_synchro_state )
570 p_pgrm->delta_absolute = mdate() - cr_time;
571 p_pgrm->i_synchro_state = SYNCHRO_OK;
578 else if( p_input->stream.b_pace_control )
580 /* Wait a while before delivering the packets to the decoder. */
581 mwait( cr_time + p_pgrm->delta_absolute );
585 mtime_t sys_time, delta_cr;
588 delta_cr = sys_time - cr_time;
590 if( (p_es != NULL && p_es->b_discontinuity) ||
591 ( p_pgrm->last_cr != 0 &&
592 ( (p_pgrm->last_cr - cr_time) > CR_MAX_GAP
593 || (p_pgrm->last_cr - cr_time) < - CR_MAX_GAP ) ) )
595 intf_WarnMsg( 3, "CR re-initialiazed" );
597 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
600 p_es->b_discontinuity = 0;
603 p_pgrm->last_cr = cr_time;
605 if( p_pgrm->c_average_count == CR_MAX_AVERAGE_COUNTER )
607 p_pgrm->delta_cr = ( delta_cr + (p_pgrm->delta_cr
608 * (CR_MAX_AVERAGE_COUNTER - 1)) )
609 / CR_MAX_AVERAGE_COUNTER;
613 p_pgrm->delta_cr = ( delta_cr + (p_pgrm->delta_cr
614 * p_pgrm->c_average_count) )
615 / ( p_pgrm->c_average_count + 1 );
616 p_pgrm->c_average_count++;
619 if( p_pgrm->i_synchro_state == SYNCHRO_NOT_STARTED )
621 p_pgrm->i_synchro_state = SYNCHRO_START;
631 /*****************************************************************************
632 * DecodePSM: Decode the Program Stream Map information
633 *****************************************************************************/
634 static void DecodePSM( input_thread_t * p_input, data_packet_t * p_data )
636 stream_ps_data_t * p_demux =
637 (stream_ps_data_t *)p_input->stream.p_demux_data;
639 if( !p_demux->b_is_PSM_complete )
645 intf_DbgMsg( "Building PSM" );
646 if( p_data->p_payload_start + 10 > p_data->p_payload_end )
648 intf_ErrMsg( "PSM too short : packet corrupt" );
651 /* Go to elementary_stream_map_length, jumping over
652 * program_stream_info. */
653 p_byte = p_data->p_payload_start + 10
654 + U16_AT(&p_data->p_payload_start[8]);
655 if( p_byte > p_data->p_payload_end )
657 intf_ErrMsg( "PSM too short : packet corrupt" );
660 /* This is the full size of the elementary_stream_map.
661 * 2 == elementary_stream_map_length
663 p_end = p_byte + 2 + U16_AT(p_byte) - 4;
665 if( p_end > p_data->p_payload_end )
667 intf_ErrMsg( "PSM too short : packet corrupt" );
671 vlc_mutex_lock( &p_input->stream.stream_lock );
673 /* 4 == minimum useful size of a section */
674 while( p_byte + 4 <= p_end )
676 p_input->p_es[i_es].i_id
677 = p_input->p_es[i_es].i_stream_id
679 p_input->p_es[i_es].i_type = p_byte[0];
680 p_input->p_es[i_es].p_pgrm = p_input->stream.pp_programs[0];
681 p_input->p_es[i_es].b_discontinuity = 0;
682 p_input->p_es[i_es].p_pes = NULL;
683 p_byte += 4 + U16_AT(&p_byte[2]);
686 switch( p_input->p_es[i_es].i_type )
690 /* Spawn audio thread. */
691 intf_DbgMsg( "Starting an MPEG-audio decoder" );
696 /* Spawn video thread. */
697 intf_DbgMsg( "Starting an MPEG-video decoder" );
705 vlc_mutex_unlock( &p_input->stream.stream_lock );
706 p_demux->i_PSM_version = p_data->p_buffer[6] & 0x1F;
707 p_demux->b_is_PSM_complete = 1;
709 else if( p_demux->i_PSM_version != (p_data->p_buffer[6] & 0x1F) )
712 intf_ErrMsg( "PSM changed, this is not supported yet !" );
713 p_demux->i_PSM_version = p_data->p_buffer[6] & 0x1F;
717 /*****************************************************************************
718 * input_DemuxPS: first step of demultiplexing: the PS header
719 *****************************************************************************/
720 void input_DemuxPS( input_thread_t * p_input, data_packet_t * p_data )
723 boolean_t b_trash = 0;
724 es_descriptor_t * p_es = NULL;
726 i_code = U32_AT( p_data->p_buffer );
727 if( i_code >= 0x1B9 && i_code <= 0x1BC )
731 case 0x1BA: /* PACK_START_CODE */
733 /* Convert the SCR in microseconds. */
736 if( (p_data->p_buffer[4] & 0xC0) == 0x40 )
740 (( ((mtime_t)(p_data->p_buffer[4] & 0x38) << 27) |
741 ((mtime_t)(p_data->p_buffer[4] & 0x3) << 26) |
742 ((mtime_t)(p_data->p_buffer[5]) << 20) |
743 ((mtime_t)(p_data->p_buffer[6] & 0xF8) << 12) |
744 ((mtime_t)(p_data->p_buffer[6] & 0x3) << 13) |
745 ((mtime_t)(p_data->p_buffer[7]) << 5) |
746 ((mtime_t)(p_data->p_buffer[8] & 0xF8) >> 3)
751 /* MPEG-1 SCR is like PTS */
753 (( ((mtime_t)(p_data->p_buffer[4] & 0x0E) << 29) |
754 (((mtime_t)U16_AT(p_data->p_buffer + 5) << 14)
756 ((mtime_t)U16_AT(p_data->p_buffer + 7) >> 1)
759 /* Call the pace control. */
760 //intf_Msg("+%lld\n", scr_time);
761 CRDecode( p_input, NULL, scr_time - 200000 );
766 case 0x1BB: /* SYSTEM_START_CODE */
767 b_trash = 1; /* Nothing interesting */
770 case 0x1BC: /* PROGRAM_STREAM_MAP_CODE */
771 intf_ErrMsg("meuuuuh\n");
772 DecodePSM( p_input, p_data );
776 case 0x1B9: /* PROGRAM_END_CODE */
781 /* This should not happen */
783 intf_WarnMsg( 1, "Unwanted packet received with start code %x",
792 /* This is a PES packet. Find out if we want it or not. */
793 i_id = p_data->p_buffer[3]; /* ID of the stream. */
795 vlc_mutex_lock( &p_input->stream.stream_lock );
796 for( i_dummy = 0; i_dummy < INPUT_MAX_ES; i_dummy++ )
798 if( p_input->p_es[i_dummy].i_id == i_id )
800 p_es = &p_input->p_es[i_dummy];
804 vlc_mutex_unlock( &p_input->stream.stream_lock );
810 if( (i_id & 0xC0L) == 0xC0L )
812 /* MPEG video and audio */
813 for( i_dummy = 0; i_dummy < INPUT_MAX_ES; i_dummy++ )
815 if( p_input->p_es[i_dummy].i_id == EMPTY_ID )
817 p_es = &p_input->p_es[i_dummy];
822 if( p_es != NULL && (i_id & 0xF0L) == 0xE0L )
825 vdec_config_t * p_config;
826 p_es->i_id = p_es->i_stream_id = i_id;
827 p_es->i_type = MPEG2_VIDEO_ES;
828 p_es->p_pgrm = p_input->stream.pp_programs[0];
829 p_es->b_discontinuity = 0;
833 p_config = (vdec_config_t *)malloc( sizeof(vdec_config_t) );
834 p_config->p_vout = p_input->p_default_vout;
836 p_config->decoder_config.i_stream_id = i_id;
837 p_config->decoder_config.i_type = MPEG2_VIDEO_ES;
838 p_config->decoder_config.p_stream_ctrl =
839 &p_input->stream.control;
840 p_config->decoder_config.p_decoder_fifo =
841 (decoder_fifo_t *)malloc( sizeof(decoder_fifo_t) );
842 vlc_mutex_init(&p_config->decoder_config.p_decoder_fifo->data_lock);
843 vlc_cond_init(&p_config->decoder_config.p_decoder_fifo->data_wait);
844 p_config->decoder_config.p_decoder_fifo->i_start =
845 p_config->decoder_config.p_decoder_fifo->i_end = 0;
846 p_config->decoder_config.p_decoder_fifo->b_die = 0;
847 p_config->decoder_config.p_decoder_fifo->p_packets_mgt =
848 p_input->p_method_data;
849 p_config->decoder_config.p_decoder_fifo->pf_delete_pes =
850 p_input->p_plugin->pf_delete_pes;
851 p_es->p_decoder_fifo = p_config->decoder_config.p_decoder_fifo;
852 p_config->decoder_config.pf_init_bit_stream =
854 for( i_dummy = 0; i_dummy < INPUT_MAX_SELECTED_ES; i_dummy++ )
856 if( p_input->pp_selected_es[i_dummy] == NULL )
858 p_input->pp_selected_es[i_dummy] = p_es;
863 p_es->thread_id = vpar_CreateThread( p_config );
866 else if( p_es != NULL && (i_id & 0xE0) == 0xC0 )
869 adec_config_t * p_config;
870 p_es->i_id = p_es->i_stream_id = i_id;
871 p_es->i_type = MPEG2_AUDIO_ES;
872 p_es->p_pgrm = p_input->stream.pp_programs[0];
873 p_es->b_discontinuity = 0;
877 p_config = (adec_config_t *)malloc( sizeof(adec_config_t) );
878 p_config->p_aout = p_input->p_default_aout;
880 p_config->decoder_config.i_stream_id = i_id;
881 p_config->decoder_config.i_type = MPEG2_AUDIO_ES;
882 p_config->decoder_config.p_stream_ctrl =
883 &p_input->stream.control;
884 p_config->decoder_config.p_decoder_fifo =
885 (decoder_fifo_t *)malloc( sizeof(decoder_fifo_t) );
886 vlc_mutex_init(&p_config->decoder_config.p_decoder_fifo->data_lock);
887 vlc_cond_init(&p_config->decoder_config.p_decoder_fifo->data_wait);
888 p_config->decoder_config.p_decoder_fifo->i_start =
889 p_config->decoder_config.p_decoder_fifo->i_end = 0;
890 p_config->decoder_config.p_decoder_fifo->b_die = 0;
891 p_config->decoder_config.p_decoder_fifo->p_packets_mgt =
892 p_input->p_method_data;
893 p_config->decoder_config.p_decoder_fifo->pf_delete_pes =
894 p_input->p_plugin->pf_delete_pes;
895 p_es->p_decoder_fifo = p_config->decoder_config.p_decoder_fifo;
896 p_config->decoder_config.pf_init_bit_stream =
898 for( i_dummy = 0; i_dummy < INPUT_MAX_SELECTED_ES; i_dummy++ )
900 if( p_input->pp_selected_es[i_dummy] == NULL )
902 p_input->pp_selected_es[i_dummy] = p_es;
907 p_es->thread_id = adec_CreateThread( p_config );
927 input_GatherPES( p_input, p_data, p_es, 1, 0 );
931 /* Trash the packet if it has no payload or if it isn't selected */
934 p_input->p_plugin->pf_delete_packet( p_input, p_data );
936 p_input->c_packets_trashed++;
946 /*****************************************************************************
947 * input_DemuxTS: first step of demultiplexing: the TS header
948 *****************************************************************************/
949 void input_DemuxTS( input_thread_t * p_input, data_packet_t * p_data )
952 boolean_t b_adaptation; /* Adaptation field is present */
953 boolean_t b_payload; /* Packet carries payload */
954 boolean_t b_unit_start; /* A PSI or a PES start in the packet */
955 boolean_t b_trash = 0; /* Is the packet unuseful ? */
956 boolean_t b_lost = 0; /* Was there a packet loss ? */
957 es_descriptor_t * p_es = NULL;
958 es_ts_data_t * p_es_demux = NULL;
959 pgrm_ts_data_t * p_pgrm_demux = NULL;
961 #define p (p_data->p_buffer)
963 //intf_DbgMsg("input debug: TS-demultiplexing packet %p, pid %d\n",
964 // p_ts_packet, U16_AT(&p[1]) & 0x1fff);
966 /* Extract flags values from TS common header. */
967 i_pid = U16_AT(&p[1]) & 0x1fff;
968 b_unit_start = (p[1] & 0x40);
969 b_adaptation = (p[3] & 0x20);
970 b_payload = (p[3] & 0x10);
972 /* Find out the elementary stream. */
973 vlc_mutex_lock( &p_input->stream.stream_lock );
974 for( i_dummy = 0; i_dummy < INPUT_MAX_ES; i_dummy++ )
976 if( p_input->p_es[i_dummy].i_id != EMPTY_ID )
978 if( p_input->p_es[i_dummy].i_id == i_pid )
980 p_es = &p_input->p_es[i_dummy];
981 p_es_demux = (es_ts_data_t *)p_es->p_demux_data;
982 p_pgrm_demux = (pgrm_ts_data_t *)p_es->p_pgrm->p_demux_data;
987 vlc_mutex_unlock( &p_input->stream.stream_lock );
993 if( p_es->p_decoder_fifo == NULL )
995 /* Not selected. Just read the adaptation field for a PCR. */
999 if( (p_es->p_decoder_fifo != NULL) || (p_pgrm_demux->i_pcr_pid == i_pid) )
1001 /* Extract adaptation field information if any */
1004 /* We don't have any adaptation_field, so payload starts
1005 * immediately after the 4 byte TS header */
1006 p_data->p_payload_start += 4;
1010 /* p[4] is adaptation_field_length minus one */
1011 p_data->p_payload_start += 5 + p[4];
1013 /* The adaptation field can be limited to the
1014 * adaptation_field_length byte, so that there is nothing to do:
1015 * skip this possibility */
1018 /* If the packet has both adaptation_field and payload,
1019 * adaptation_field cannot be more than 182 bytes long; if
1020 * there is only an adaptation_field, it must fill the next
1022 if( b_payload ? (p[4] > 182) : (p[4] != 183) )
1025 "invalid TS adaptation field (%p)",
1027 p_data->b_discard_payload = 1;
1029 p_es->c_invalid_packets++;
1033 /* Now we are sure that the byte containing flags is present:
1037 /* discontinuity_indicator */
1041 "discontinuity_indicator" \
1042 " encountered by TS demux (position read: %d," \
1044 p[5] & 0x80, p_es_demux->i_continuity_counter );
1046 /* If the PID carries the PCR, there will be a system
1047 * time-based discontinuity. We let the PCR decoder
1049 p_es->b_discontinuity = 1;
1051 /* There also may be a continuity_counter
1052 * discontinuity: resynchronise our counter with
1053 * the one of the stream. */
1054 p_es_demux->i_continuity_counter = (p[3] & 0x0f) - 1;
1057 /* If this is a PCR_PID, and this TS packet contains a
1058 * PCR, we pass it along to the PCR decoder. */
1059 if( (p_pgrm_demux->i_pcr_pid == i_pid) && (p[5] & 0x10) )
1061 /* There should be a PCR field in the packet, check
1062 * if the adaptation field is long enough to carry
1066 /* Convert the PCR in microseconds.
1067 * WARNING: do not remove the casts in the
1068 * following calculation ! */
1071 ( (( (mtime_t)U32_AT((u32*)&p[6]) << 1 )
1072 | ( p[10] >> 7 )) * 300 ) / 27;
1073 /* Call the pace control. */
1074 CRDecode( p_input, p_es, pcr_time );
1077 } /* valid TS adaptation field ? */
1079 } /* has adaptation field */
1081 /* Check the continuity of the stream. */
1082 i_dummy = ((p[3] & 0x0f) - p_es_demux->i_continuity_counter) & 0x0f;
1085 /* Everything is ok, just increase our counter */
1086 p_es_demux->i_continuity_counter++;
1090 if( !b_payload && i_dummy == 0 )
1092 /* This is a packet without payload, this is allowed by the draft.
1093 * As there is nothing interesting in this packet (except PCR that
1094 * have already been handled), we can trash the packet. */
1096 "Packet without payload received by TS demux" );
1099 else if( i_dummy <= 0 )
1101 /* FIXME: this can never happen, can it ? --Meuuh */
1102 /* Duplicate packet: mark it as being to be trashed. */
1103 intf_WarnMsg( 1, "Duplicate packet received by TS demux" );
1106 else if( p_es_demux->i_continuity_counter == 0xFF )
1108 /* This means that the packet is the first one we receive for this
1109 * ES since the continuity counter ranges between 0 and 0x0F
1110 * excepts when it has been initialized by the input: Init the
1111 * counter to the correct value. */
1112 intf_DbgMsg( "First packet for PID %d received by TS demux",
1114 p_es_demux->i_continuity_counter = (p[3] & 0x0f);
1118 /* This can indicate that we missed a packet or that the
1119 * continuity_counter wrapped and we received a dup packet: as we
1120 * don't know, do as if we missed a packet to be sure to recover
1121 * from this situation */
1123 "Packet lost by TS demux: current %d, packet %d\n",
1124 p_es_demux->i_continuity_counter & 0x0f,
1127 p_es_demux->i_continuity_counter = p[3] & 0x0f;
1128 } /* not continuous */
1130 } /* if selected or PCR */
1132 /* Trash the packet if it has no payload or if it isn't selected */
1135 p_input->p_plugin->pf_delete_packet( p_input, p_data );
1137 p_input->c_packets_trashed++;
1142 if( p_es_demux->b_psi )
1144 /* The payload contains PSI tables */
1146 input_DemuxPSI( p_input, p_data, p_es,
1147 b_unit_start, b_lost );
1152 /* The payload carries a PES stream */
1154 input_GatherPES( p_input, p_data, p_es, b_unit_start, b_lost );