1 /*****************************************************************************
2 * mpeg_system.c: TS, PS and PES management
3 *****************************************************************************
4 * Copyright (C) 1998-2001 VideoLAN
5 * $Id: mpeg_system.c,v 1.100 2002/07/23 00:39:17 sam Exp $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Michel Lespinasse <walken@via.ecp.fr>
9 * BenoƮt Steiner <benny@via.ecp.fr>
10 * Samuel Hocevar <sam@via.ecp.fr>
11 * Henri Fallon <henri@via.ecp.fr>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
26 *****************************************************************************/
28 /*****************************************************************************
30 *****************************************************************************/
32 #include <string.h> /* memcpy(), memset() */
33 #include <sys/types.h> /* off_t */
37 #include "stream_control.h"
38 #include "input_ext-intf.h"
39 #include "input_ext-dec.h"
40 #include "input_ext-plugins.h"
43 * PES Packet management
46 /*****************************************************************************
48 *****************************************************************************
49 * Small utility function used to parse discontinuous headers safely. Copies
50 * i_buf_len bytes of data to a buffer and returns the size copied.
51 * It also solves some alignment problems on non-IA-32, non-PPC processors.
52 * This is a variation on the theme of input_ext-dec.h:GetChunk().
53 *****************************************************************************/
54 static inline size_t MoveChunk( byte_t * p_dest, data_packet_t ** pp_data_src,
55 byte_t ** pp_src, size_t i_buf_len )
57 ptrdiff_t i_available;
59 if( (i_available = (*pp_data_src)->p_payload_end - *pp_src)
63 memcpy( p_dest, *pp_src, i_buf_len );
69 size_t i_init_len = i_buf_len;
74 memcpy( p_dest, *pp_src, i_available );
75 *pp_data_src = (*pp_data_src)->p_next;
76 i_buf_len -= i_available;
77 p_dest += i_available;
78 if( *pp_data_src == NULL )
81 return( i_init_len - i_buf_len );
83 *pp_src = (*pp_data_src)->p_payload_start;
85 while( (i_available = (*pp_data_src)->p_payload_end - *pp_src)
91 memcpy( p_dest, *pp_src, i_buf_len );
98 /*****************************************************************************
100 *****************************************************************************
101 * Parse a finished PES packet and analyze its header.
102 *****************************************************************************/
103 #define PES_HEADER_SIZE 7
104 void input_ParsePES( input_thread_t * p_input, es_descriptor_t * p_es )
106 data_packet_t * p_data;
108 byte_t p_header[PES_HEADER_SIZE];
111 #define p_pes (p_es->p_pes)
113 /* Parse the header. The header has a variable length, but in order
114 * to improve the algorithm, we will read the 14 bytes we may be
116 p_data = p_pes->p_first;
117 p_byte = p_data->p_payload_start;
120 if( MoveChunk( p_header, &p_data, &p_byte, PES_HEADER_SIZE )
123 msg_Warn( p_input, "PES packet too short to have a header" );
124 input_DeletePES( p_input->p_method_data, p_pes );
129 /* Get the PES size if defined */
130 p_es->i_pes_real_size = U16_AT(p_header + 4);
131 if( p_es->i_pes_real_size )
133 p_es->i_pes_real_size += 6;
136 /* First read the 6 header bytes common to all PES packets:
137 * use them to test the PES validity */
138 if( (p_header[0] || p_header[1] || (p_header[2] != 1)) )
140 /* packet_start_code_prefix != 0x000001 */
141 msg_Err( p_input, "data loss, PES packet does not start with 000001" );
142 input_DeletePES( p_input->p_method_data, p_pes );
147 int i_pes_header_size, i_payload_size;
149 if ( p_es->i_pes_real_size &&
150 (p_es->i_pes_real_size != p_pes->i_pes_size) )
152 /* PES_packet_length is set and != total received payload */
153 /* Warn the decoder that the data may be corrupt. */
154 msg_Warn( p_input, "packet corrupted, PES sizes do not match" );
157 switch( p_es->i_stream_id )
159 case 0xBC: /* Program stream map */
160 case 0xBE: /* Padding */
161 case 0xBF: /* Private stream 2 */
164 case 0xFF: /* Program stream directory */
165 case 0xF2: /* DSMCC stream */
166 case 0xF8: /* ITU-T H.222.1 type E stream */
167 /* The payload begins immediately after the 6 bytes header, so
168 * we have finished with the parsing */
169 i_pes_header_size = 6;
173 if( (p_header[6] & 0xC0) == 0x80 )
175 /* MPEG-2 : the PES header contains at least 3 more bytes. */
177 vlc_bool_t b_has_pts, b_has_dts;
178 byte_t p_full_header[12];
180 p_pes->b_data_alignment = p_header[6] & 0x04;
182 i_max_len = MoveChunk( p_full_header, &p_data, &p_byte, 12 );
186 "PES packet too short to have a MPEG-2 header" );
187 input_DeletePES( p_input->p_method_data,
193 b_has_pts = p_full_header[0] & 0x80;
194 b_has_dts = p_full_header[0] & 0x40;
195 i_pes_header_size = p_full_header[1] + 9;
197 /* Now parse the optional header extensions */
203 "PES packet too short to have a MPEG-2 header" );
204 input_DeletePES( p_input->p_method_data,
209 p_pes->i_pts = input_ClockGetTS( p_input, p_es->p_pgrm,
210 ( ((mtime_t)(p_full_header[2] & 0x0E) << 29) |
211 ((mtime_t)(p_full_header[3]) << 22) |
212 ((mtime_t)(p_full_header[4] & 0xFE) << 14) |
213 ((mtime_t)p_full_header[5] << 7) |
214 ((mtime_t)p_full_header[6] >> 1) ) );
221 "PES packet too short to have a MPEG-2 header" );
222 input_DeletePES( p_input->p_method_data,
227 p_pes->i_dts = input_ClockGetTS( p_input, p_es->p_pgrm,
228 ( ((mtime_t)(p_full_header[7] & 0x0E) << 29) |
229 (((mtime_t)U16_AT(p_full_header + 8) << 14)
231 ((mtime_t)U16_AT(p_full_header + 10) >> 1) ) );
237 /* Probably MPEG-1 */
238 vlc_bool_t b_has_pts, b_has_dts;
240 i_pes_header_size = 6;
241 p_data = p_pes->p_first;
242 p_byte = p_data->p_payload_start;
243 /* Cannot fail because the previous one succeeded. */
244 MoveChunk( NULL, &p_data, &p_byte, 6 );
246 while( *p_byte == 0xFF && i_pes_header_size < 23 )
249 if( MoveChunk( NULL, &p_data, &p_byte, 1 ) != 1 )
252 "PES packet too short to have a MPEG-1 header" );
253 input_DeletePES( p_input->p_method_data, p_pes );
258 if( i_pes_header_size == 23 )
260 msg_Err( p_input, "too much MPEG-1 stuffing" );
261 input_DeletePES( p_input->p_method_data, p_pes );
266 if( (*p_byte & 0xC0) == 0x40 )
268 /* Don't ask why... --Meuuh */
269 /* Erm... why ? --Sam */
270 /* Well... According to the recommendation, it is for
271 * STD_buffer_scale and STD_buffer_size. --Meuuh */
272 i_pes_header_size += 2;
273 if( MoveChunk( NULL, &p_data, &p_byte, 2 ) != 2 )
276 "PES packet too short to have a MPEG-1 header" );
277 input_DeletePES( p_input->p_method_data, p_pes );
285 b_has_pts = *p_byte & 0x20;
286 b_has_dts = *p_byte & 0x10;
292 i_pes_header_size += 4;
293 if( MoveChunk( p_ts, &p_data, &p_byte, 5 ) != 5 )
296 "PES packet too short to have a MPEG-1 header" );
297 input_DeletePES( p_input->p_method_data, p_pes );
302 p_pes->i_pts = input_ClockGetTS( p_input, p_es->p_pgrm,
303 ( ((mtime_t)(p_ts[0] & 0x0E) << 29) |
304 (((mtime_t)U32_AT(p_ts) & 0xFFFE00) << 6) |
305 ((mtime_t)p_ts[3] << 7) |
306 ((mtime_t)p_ts[4] >> 1) ) );
310 i_pes_header_size += 5;
311 if( MoveChunk( p_ts, &p_data, &p_byte, 5 ) != 5 )
314 "PES packet too short to have a MPEG-1 header" );
315 input_DeletePES( p_input->p_method_data, p_pes );
320 p_pes->i_dts = input_ClockGetTS( p_input,
322 ( ((mtime_t)(p_ts[0] & 0x0E) << 29) |
323 (((mtime_t)U32_AT(p_ts) & 0xFFFE00) << 6) |
324 ((mtime_t)p_ts[3] << 7) |
325 ((mtime_t)p_ts[4] >> 1) ) );
333 if( p_es->i_stream_id == 0xbd )
335 /* With private stream 1, the first byte of the payload
336 * is a stream_private_id, so skip it. */
340 if( p_es->i_fourcc == VLC_FOURCC('a','5','2',' ') )
342 /* With ac3 audio, we need to skip first 3 bytes */
343 i_pes_header_size += 3;
346 /* Now we've parsed the header, we just have to indicate in some
347 * specific data packets where the PES payload begins (renumber
348 * p_payload_start), so that the decoders can find the beginning
349 * of their data right out of the box. */
350 p_data = p_pes->p_first;
351 i_payload_size = p_data->p_payload_end
352 - p_data->p_payload_start;
353 while( i_pes_header_size > i_payload_size )
355 /* These packets are entirely filled by the PES header. */
356 i_pes_header_size -= i_payload_size;
357 p_data->p_payload_start = p_data->p_payload_end;
358 /* Go to the next data packet. */
359 if( (p_data = p_data->p_next) == NULL )
361 msg_Err( p_input, "PES header bigger than payload" );
362 input_DeletePES( p_input->p_method_data, p_pes );
366 i_payload_size = p_data->p_payload_end
367 - p_data->p_payload_start;
369 /* This last packet is partly header, partly payload. */
370 if( i_payload_size < i_pes_header_size )
372 msg_Err( p_input, "PES header bigger than payload" );
373 input_DeletePES( p_input->p_method_data, p_pes );
377 p_data->p_payload_start += i_pes_header_size;
380 /* Now we can eventually put the PES packet in the decoder's
382 if( p_es->p_decoder_fifo != NULL )
384 input_DecodePES( p_es->p_decoder_fifo, p_pes );
388 msg_Err( p_input, "no fifo to receive PES %p "
389 "(who wrote this damn code ?)", p_pes );
390 input_DeletePES( p_input->p_method_data, p_pes );
398 /*****************************************************************************
400 *****************************************************************************
401 * Gather a PES packet.
402 *****************************************************************************/
403 void input_GatherPES( input_thread_t * p_input, data_packet_t * p_data,
404 es_descriptor_t * p_es,
405 vlc_bool_t b_unit_start, vlc_bool_t b_packet_lost )
407 #define p_pes (p_es->p_pes)
409 /* If we lost data, insert a NULL data packet (philosophy : 0 is quite
410 * often an escape sequence in decoders, so that should make them wait
411 * for the next start code). */
414 input_NullPacket( p_input, p_es );
417 if( b_unit_start && p_pes != NULL )
419 /* If the data packet contains the begining of a new PES packet, and
420 * if we were reassembling a PES packet, then the PES should be
421 * complete now, so parse its header and give it to the decoders. */
422 input_ParsePES( p_input, p_es );
425 if( !b_unit_start && p_pes == NULL )
427 /* Random access... */
428 input_DeletePacket( p_input->p_method_data, p_data );
434 /* If we are at the beginning of a new PES packet, we must fetch
435 * a new PES buffer to begin with the reassembly of this PES
436 * packet. This is also here that we can synchronize with the
437 * stream if we lost packets or if the decoder has just
439 if( (p_pes = input_NewPES( p_input->p_method_data ) ) == NULL )
441 msg_Err( p_input, "out of memory" );
442 p_input->b_error = 1;
445 p_pes->i_rate = p_input->stream.control.i_rate;
446 p_pes->p_first = p_data;
448 /* If the PES header fits in the first data packet, we can
449 * already set p_gather->i_pes_real_size. */
450 if( p_data->p_payload_end - p_data->p_payload_start
453 p_es->i_pes_real_size = ((u16)p_data->p_payload_start[4] << 8)
454 + p_data->p_payload_start[5] + 6;
458 p_es->i_pes_real_size = 0;
463 /* Update the relations between the data packets */
464 p_pes->p_last->p_next = p_data;
467 p_pes->p_last = p_data;
470 /* Size of the payload carried in the data packet */
471 p_pes->i_pes_size += (p_data->p_payload_end
472 - p_data->p_payload_start);
474 /* We can check if the packet is finished */
475 if( p_pes->i_pes_size == p_es->i_pes_real_size )
477 /* The packet is finished, parse it */
478 input_ParsePES( p_input, p_es );
489 /*****************************************************************************
490 * GetID: Get the ID of a stream
491 *****************************************************************************/
492 static u16 GetID( data_packet_t * p_data )
496 i_id = p_data->p_demux_start[3]; /* stream_id */
499 /* FIXME : this is not valid if the header is split in multiple
501 /* stream_private_id */
502 i_id |= p_data->p_demux_start[ 9 + p_data->p_demux_start[8] ] << 8;
507 /*****************************************************************************
508 * DecodePSM: Decode the Program Stream Map information
509 *****************************************************************************
510 * FIXME : loads are not aligned in this function
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_demux_start + 10 > p_data->p_payload_end )
523 msg_Err( p_input, "PSM too short, packet corrupt" );
527 if( p_demux->b_has_PSM
528 && p_demux->i_PSM_version == (p_data->p_demux_start[6] & 0x1F) )
530 /* Already got that one. */
534 p_demux->b_has_PSM = 1;
535 p_demux->i_PSM_version = p_data->p_demux_start[6] & 0x1F;
537 /* Go to elementary_stream_map_length, jumping over
538 * program_stream_info. */
539 p_byte = p_data->p_demux_start + 10
540 + U16_AT(&p_data->p_demux_start[8]);
541 if( p_byte > p_data->p_payload_end )
543 msg_Err( p_input, "PSM too short, packet corrupt" );
546 /* This is the full size of the elementary_stream_map.
547 * 2 == elementary_stream_map_length
548 * Please note that CRC_32 is not included in the length. */
549 p_end = p_byte + 2 + U16_AT(p_byte);
551 if( p_end > p_data->p_payload_end )
553 msg_Err( p_input, "PSM too short, packet corrupt" );
557 vlc_mutex_lock( &p_input->stream.stream_lock );
559 /* 4 == minimum useful size of a section */
560 while( p_byte + 4 <= p_end )
562 es_descriptor_t * p_es = NULL;
563 u8 i_stream_id = p_byte[1];
564 /* FIXME: there will be a problem with private streams... (same
567 /* Look for the ES in the ES table */
568 for( i = i_new_es_number;
569 i < p_input->stream.pp_programs[0]->i_es_number;
572 if( p_input->stream.pp_programs[0]->pp_es[i]->i_stream_id
575 p_es = p_input->stream.pp_programs[0]->pp_es[i];
576 /* FIXME: do something below */
578 if( p_es->i_type != p_byte[0] )
580 input_DelES( p_input, p_es );
586 /* Move the ES to the beginning. */
587 p_input->stream.pp_programs[0]->pp_es[i]
588 = p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ];
589 p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ]
597 /* The goal is to have all the ES we have just read in the
598 * beginning of the pp_es table, and all the others at the end,
599 * so that we can close them more easily at the end. */
602 p_es = input_AddES( p_input, p_input->stream.pp_programs[0],
608 p_es->i_fourcc = VLC_FOURCC('m','p','g','v');
609 p_es->i_cat = VIDEO_ES;
612 p_es->i_fourcc = VLC_FOURCC('s','p','u',' ');
613 p_es->i_cat = SPU_ES;
617 p_es->i_fourcc = VLC_FOURCC('m','p','g','a');
618 p_es->i_cat = AUDIO_ES;
621 p_es->i_fourcc = VLC_FOURCC('a','5','2',' ');
622 p_es->i_cat = AUDIO_ES;
625 p_es->i_fourcc = VLC_FOURCC('l','p','c','m');
626 p_es->i_cat = AUDIO_ES;
633 /* input_AddES has inserted the new element at the end. */
634 p_input->stream.pp_programs[0]->pp_es[
635 p_input->stream.pp_programs[0]->i_es_number ]
636 = p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ];
637 p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ] = p_es;
640 p_byte += 4 + U16_AT(&p_byte[2]);
643 /* Un-select the streams that are no longer parts of the program. */
644 while( i_new_es_number < p_input->stream.pp_programs[0]->i_es_number )
646 /* We remove pp_es[i_new_es_member] and not pp_es[i] because the
647 * list will be emptied starting from the end */
648 input_DelES( p_input,
649 p_input->stream.pp_programs[0]->pp_es[i_new_es_number] );
652 msg_Dbg( p_input, "the stream map after the PSM is now:" );
653 input_DumpStream( p_input );
655 vlc_mutex_unlock( &p_input->stream.stream_lock );
658 /*****************************************************************************
659 * input_ReadPS: store a PS packet into a data_buffer_t
660 *****************************************************************************/
661 #define PEEK( SIZE ) \
662 i_error = input_Peek( p_input, &p_peek, SIZE ); \
663 if( i_error == -1 ) \
667 else if( i_error < SIZE ) \
673 ssize_t input_ReadPS( input_thread_t * p_input, data_packet_t ** pp_data )
676 size_t i_packet_size;
677 ssize_t i_error, i_read;
679 /* Read what we believe to be a packet header. */
682 if( p_peek[0] || p_peek[1] || p_peek[2] != 1 || p_peek[3] < 0xB9 )
684 if( p_peek[0] || p_peek[1] || p_peek[2] )
686 /* It is common for MPEG-1 streams to pad with zeros
687 * (although it is forbidden by the recommendation), so
688 * don't bother everybody in this case. */
689 msg_Warn( p_input, "garbage (0x%.2x%.2x%.2x%.2x)",
690 p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
693 /* This is not the startcode of a packet. Read the stream
694 * until we find one. */
695 while( p_peek[0] || p_peek[1] || p_peek[2] != 1 || p_peek[3] < 0xB9 )
697 p_input->p_current_data++;
699 if( p_input->b_die ) return( -1 );
704 /* 0x1B9 == SYSTEM_END_CODE, it is only 4 bytes long. */
705 if( p_peek[3] != 0xB9 )
707 /* The packet is at least 6 bytes long. */
710 if( p_peek[3] != 0xBA )
712 /* That's the case for all packets, except pack header. */
713 i_packet_size = (p_peek[4] << 8) | p_peek[5];
718 if( (p_peek[4] & 0xC0) == 0x40 )
723 else if( (p_peek[4] & 0xF0) == 0x20 )
730 msg_Err( p_input, "unable to determine stream type" );
737 /* System End Code */
741 /* Fetch a packet of the appropriate size. */
742 i_read = input_SplitBuffer( p_input, pp_data, i_packet_size + 6 );
748 /* In MPEG-2 pack headers we still have to read stuffing bytes. */
749 if( ((*pp_data)->p_demux_start[3] == 0xBA) && (i_packet_size == 8) )
751 size_t i_stuffing = ((*pp_data)->p_demux_start[13] & 0x7);
752 /* Force refill of the input buffer - though we don't care
753 * about p_peek. Please note that this is unoptimized. */
755 p_input->p_current_data += i_stuffing;
763 /*****************************************************************************
764 * input_ParsePS: read the PS header
765 *****************************************************************************/
766 es_descriptor_t * input_ParsePS( input_thread_t * p_input,
767 data_packet_t * p_data )
770 es_descriptor_t * p_es = NULL;
772 i_code = p_data->p_demux_start[3];
774 if( i_code > 0xBC ) /* ES start code */
779 /* This is a PES packet. Find out if we want it or not. */
780 i_id = GetID( p_data );
782 vlc_mutex_lock( &p_input->stream.stream_lock );
783 if( p_input->stream.pp_programs[0]->b_is_ok )
785 /* Look only at the selected ES. */
786 for( i_dummy = 0; i_dummy < p_input->stream.i_selected_es_number;
789 if( p_input->stream.pp_selected_es[i_dummy] != NULL
790 && p_input->stream.pp_selected_es[i_dummy]->i_id == i_id )
792 p_es = p_input->stream.pp_selected_es[i_dummy];
799 stream_ps_data_t * p_demux =
800 (stream_ps_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
802 /* Search all ES ; if not found -> AddES */
803 p_es = input_FindES( p_input, i_id );
805 if( p_es == NULL && !p_demux->b_has_PSM )
807 p_es = input_AddES( p_input, p_input->stream.pp_programs[0],
811 p_es->i_stream_id = p_data->p_demux_start[3];
813 /* Set stream type and auto-spawn. */
814 if( (i_id & 0xF0) == 0xE0 )
817 p_es->i_fourcc = VLC_FOURCC('m','p','g','v');
818 p_es->i_cat = VIDEO_ES;
820 if( !p_input->stream.b_seekable )
821 input_SelectES( p_input, p_es );
824 else if( (i_id & 0xE0) == 0xC0 )
827 p_es->i_fourcc = VLC_FOURCC('m','p','g','a');
828 p_es->i_cat = AUDIO_ES;
830 if( !p_input->stream.b_seekable )
831 if( config_GetInt( p_input, "audio-channel" )
832 == (p_es->i_id & 0x1F) ||
833 ( config_GetInt( p_input, "audio-channel" ) < 0
834 && !(p_es->i_id & 0x1F) ) )
835 switch( config_GetInt( p_input, "audio-type" ) )
839 input_SelectES( p_input, p_es );
843 else if( (i_id & 0xF0FF) == 0x80BD )
845 /* AC3 audio (0x80->0x8F) */
846 p_es->i_fourcc = VLC_FOURCC('a','5','2',' ');
847 p_es->i_cat = AUDIO_ES;
849 if( !p_input->stream.b_seekable )
850 if( config_GetInt( p_input, "audio-channel" )
851 == ((p_es->i_id & 0xF00) >> 8) ||
852 ( config_GetInt( p_input, "audio-channel" ) < 0
853 && !((p_es->i_id & 0xF00) >> 8)) )
854 switch( config_GetInt( p_input, "audio-type" ) )
858 input_SelectES( p_input, p_es );
862 else if( (i_id & 0xE0FF) == 0x20BD )
864 /* Subtitles video (0x20->0x3F) */
865 p_es->i_fourcc = VLC_FOURCC('s','p','u',' ');
866 p_es->i_cat = SPU_ES;
868 if( config_GetInt( p_input, "spu-channel" )
869 == ((p_es->i_id & 0x1F00) >> 8) )
871 if( !p_input->stream.b_seekable )
872 input_SelectES( p_input, p_es );
876 else if( (i_id & 0xF0FF) == 0xA0BD )
878 /* LPCM audio (0xA0->0xAF) */
879 p_es->i_fourcc = VLC_FOURCC('l','p','c','m');
880 p_es->i_cat = AUDIO_ES;
888 /* Tell the interface the stream has changed */
889 p_input->stream.b_changed = 1;
891 } /* stream.b_is_ok */
892 vlc_mutex_unlock( &p_input->stream.stream_lock );
893 } /* i_code > 0xBC */
898 /*****************************************************************************
899 * input_DemuxPS: first step of demultiplexing: the PS header
900 *****************************************************************************/
901 void input_DemuxPS( input_thread_t * p_input, data_packet_t * p_data )
904 vlc_bool_t b_trash = 0;
905 es_descriptor_t * p_es = NULL;
907 i_code = ((u32)p_data->p_demux_start[0] << 24)
908 | ((u32)p_data->p_demux_start[1] << 16)
909 | ((u32)p_data->p_demux_start[2] << 8)
910 | p_data->p_demux_start[3];
911 if( i_code <= 0x1BC )
915 case 0x1BA: /* PACK_START_CODE */
921 if( (p_data->p_demux_start[4] & 0xC0) == 0x40 )
926 p_byte = p_data->p_demux_start;
928 if( MoveChunk( p_header, &p_data, &p_byte, 14 ) != 14 )
931 "packet too short to have a header" );
936 ((mtime_t)(p_header[4] & 0x38) << 27) |
937 ((mtime_t)(U32_AT(p_header + 4) & 0x03FFF800)
939 ((( ((mtime_t)U16_AT(p_header + 6) << 16)
940 | (mtime_t)U16_AT(p_header + 8) ) & 0x03FFF800)
944 i_mux_rate = ((u32)U16_AT(p_header + 10) << 6)
945 | (p_header[12] >> 2);
946 /* FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
947 * This is the biggest kludge ever !
948 * I don't know what's wrong with mux_rate calculation
949 * but this heuristic works well : */
955 /* MPEG-1 SCR is like PTS. */
958 p_byte = p_data->p_demux_start;
960 if( MoveChunk( p_header, &p_data, &p_byte, 12 ) != 12 )
963 "packet too short to have a header" );
968 ((mtime_t)(p_header[4] & 0x0E) << 29) |
969 (((mtime_t)U32_AT(p_header + 4) & 0xFFFE00) << 6) |
970 ((mtime_t)p_header[7] << 7) |
971 ((mtime_t)p_header[8] >> 1);
974 i_mux_rate = (U32_AT(p_header + 8) & 0x7FFFFE) >> 1;
976 /* Call the pace control. */
977 input_ClockManageRef( p_input,
978 p_input->stream.p_selected_program,
981 if( i_mux_rate != p_input->stream.i_mux_rate
982 && p_input->stream.i_mux_rate )
985 "mux_rate changed, expect cosmetic errors" );
987 p_input->stream.i_mux_rate = i_mux_rate;
993 case 0x1BB: /* SYSTEM_START_CODE */
994 b_trash = 1; /* Nothing interesting */
997 case 0x1BC: /* PROGRAM_STREAM_MAP_CODE */
998 DecodePSM( p_input, p_data );
1002 case 0x1B9: /* PROGRAM_END_CODE */
1007 /* This should not happen */
1009 msg_Warn( p_input, "unwanted packet received "
1010 "with startcode 0x%.8x", i_code );
1015 p_es = input_ParsePS( p_input, p_data );
1017 vlc_mutex_lock( &p_input->stream.control.control_lock );
1018 if( p_es != NULL && p_es->p_decoder_fifo != NULL
1019 && (p_es->i_cat != AUDIO_ES || !p_input->stream.control.b_mute) )
1021 vlc_mutex_unlock( &p_input->stream.control.control_lock );
1023 input_GatherPES( p_input, p_data, p_es, 1, 0 );
1027 vlc_mutex_unlock( &p_input->stream.control.control_lock );
1032 /* Trash the packet if it has no payload or if it isn't selected */
1035 input_DeletePacket( p_input->p_method_data, p_data );
1036 p_input->stream.c_packets_trashed++;
1045 /*****************************************************************************
1046 * input_ReadTS: store a TS packet into a data_buffer_t
1047 *****************************************************************************/
1048 #define PEEK( SIZE ) \
1049 i_error = input_Peek( p_input, &p_peek, SIZE ); \
1050 if( i_error == -1 ) \
1054 else if( i_error < SIZE ) \
1060 ssize_t input_ReadTS( input_thread_t * p_input, data_packet_t ** pp_data )
1063 ssize_t i_error, i_read;
1067 if( *p_peek != TS_SYNC_CODE )
1069 msg_Warn( p_input, "garbage at input (%x)", *p_peek );
1071 if( p_input->i_mtu )
1073 while( *p_peek != TS_SYNC_CODE )
1075 /* Try to resync on next packet. */
1076 PEEK( TS_PACKET_SIZE );
1077 p_input->p_current_data += TS_PACKET_SIZE;
1083 /* Move forward until we find 0x47 (and hope it's the good
1085 while( *p_peek != TS_SYNC_CODE )
1087 p_input->p_current_data++;
1093 i_read = input_SplitBuffer( p_input, pp_data, TS_PACKET_SIZE );
1102 /*****************************************************************************
1103 * input_DemuxTS: first step of demultiplexing: the TS header
1104 *****************************************************************************/
1105 void input_DemuxTS( input_thread_t * p_input, data_packet_t * p_data,
1106 psi_callback_t pf_psi_callback )
1110 vlc_bool_t b_adaptation; /* Adaptation field is present */
1111 vlc_bool_t b_payload; /* Packet carries payload */
1112 vlc_bool_t b_unit_start; /* A PSI or a PES start in the packet */
1113 vlc_bool_t b_trash = 0; /* Is the packet unuseful ? */
1114 vlc_bool_t b_lost = 0; /* Was there a packet loss ? */
1115 vlc_bool_t b_psi = 0; /* Is this a PSI ? */
1116 vlc_bool_t b_pcr = 0; /* Does it have a PCR ? */
1117 es_descriptor_t * p_es = NULL;
1118 es_ts_data_t * p_es_demux = NULL;
1119 pgrm_ts_data_t * p_pgrm_demux = NULL;
1121 #define p (p_data->p_demux_start)
1122 /* Extract flags values from TS common header. */
1123 i_pid = ((p[1] & 0x1F) << 8) | p[2];
1124 b_unit_start = (p[1] & 0x40);
1125 b_adaptation = (p[3] & 0x20);
1126 b_payload = (p[3] & 0x10);
1128 /* Find out the elementary stream. */
1129 vlc_mutex_lock( &p_input->stream.stream_lock );
1131 for( i_dummy = 0; i_dummy < p_input->stream.i_pgrm_number; i_dummy ++ )
1133 if( (( pgrm_ts_data_t * ) p_input->stream.pp_programs[i_dummy]->
1134 p_demux_data)->i_pcr_pid == i_pid )
1141 p_es= input_FindES( p_input, i_pid );
1143 if( (p_es != NULL) && (p_es->p_demux_data != NULL) )
1145 p_es_demux = (es_ts_data_t *)p_es->p_demux_data;
1147 if( p_es_demux->b_psi )
1153 p_pgrm_demux = (pgrm_ts_data_t *)p_es->p_pgrm->p_demux_data;
1157 vlc_mutex_lock( &p_input->stream.control.control_lock );
1158 if( ( p_es == NULL ) || (p_es->i_cat == AUDIO_ES
1159 && p_input->stream.control.b_mute) )
1161 /* Not selected. Just read the adaptation field for a PCR. */
1164 else if( p_es->p_decoder_fifo == NULL && !b_psi )
1169 vlc_mutex_unlock( &p_input->stream.control.control_lock );
1170 vlc_mutex_unlock( &p_input->stream.stream_lock );
1173 /* Don't change the order of the tests : if b_psi then p_pgrm_demux
1174 * may still be null. Who said it was ugly ?
1175 * I have written worse. --Meuuh */
1177 ((p_es->p_decoder_fifo != NULL) || b_psi || b_pcr ) )
1181 /* Extract adaptation field information if any */
1185 /* We don't have any adaptation_field, so payload starts
1186 * immediately after the 4 byte TS header */
1187 p_data->p_payload_start += 4;
1191 /* p[4] is adaptation_field_length minus one */
1192 p_data->p_payload_start += 5 + p[4];
1194 /* The adaptation field can be limited to the
1195 * adaptation_field_length byte, so that there is nothing to do:
1196 * skip this possibility */
1199 /* If the packet has both adaptation_field and payload,
1200 * adaptation_field cannot be more than 182 bytes long; if
1201 * there is only an adaptation_field, it must fill the next
1203 if( b_payload ? (p[4] > 182) : (p[4] != 183) )
1205 msg_Warn( p_input, "invalid TS adaptation field (%p)",
1207 p_data->b_discard_payload = 1;
1208 p_es->c_invalid_packets++;
1211 /* Now we are sure that the byte containing flags is present:
1215 /* discontinuity_indicator */
1219 "discontinuity_indicator encountered by TS demux "
1220 "(position read: %d, saved: %d)",
1221 p[5] & 0x80, p_es_demux->i_continuity_counter );
1223 /* If the PID carries the PCR, there will be a system
1224 * time-based discontinuity. We let the PCR decoder
1226 p_es->p_pgrm->i_synchro_state = SYNCHRO_REINIT;
1228 /* There also may be a continuity_counter
1229 * discontinuity: resynchronize our counter with
1230 * the one of the stream. */
1231 p_es_demux->i_continuity_counter = (p[3] & 0x0f) - 1;
1234 } /* valid TS adaptation field ? */
1236 } /* has adaptation field */
1237 /* Check the continuity of the stream. */
1238 i_dummy = ((p[3] & 0x0f) - p_es_demux->i_continuity_counter) & 0x0f;
1241 /* Everything is ok, just increase our counter */
1242 (p_es_demux->i_continuity_counter)++;
1246 if( !b_payload && i_dummy == 0 )
1248 /* This is a packet without payload, this is allowed by the
1249 * draft. As there is nothing interesting in this packet
1250 * (except PCR that have already been handled), we can trash
1254 else if( i_dummy <= 0 )
1256 /* Duplicate packet: mark it as being to be trashed. */
1258 "duplicate packet received by TS demux" );
1261 else if( p_es_demux->i_continuity_counter == 0xFF )
1263 /* This means that the packet is the first one we receive for
1264 * this ES since the continuity counter ranges between 0 and
1265 * 0x0F excepts when it has been initialized by the input:
1266 * init the counter to the correct value. */
1267 msg_Warn( p_input, "first packet for PID %d received "
1268 "by TS demux", p_es->i_id );
1269 p_es_demux->i_continuity_counter = (p[3] & 0x0f);
1273 /* This can indicate that we missed a packet or that the
1274 * continuity_counter wrapped and we received a dup packet:
1275 * as we don't know, do as if we missed a packet to be sure
1276 * to recover from this situation */
1278 "packet lost by TS demux: current %d, packet %d",
1279 p_es_demux->i_continuity_counter & 0x0f,
1282 p_es_demux->i_continuity_counter = p[3] & 0x0f;
1283 } /* not continuous */
1285 } /* if selected or PCR */
1288 if( b_pcr && b_adaptation && (p[5] & 0x10) && p[4]>=7 )
1292 pcr_time = ( (mtime_t)p[6] << 25 ) |
1293 ( (mtime_t)p[7] << 17 ) |
1294 ( (mtime_t)p[8] << 9 ) |
1295 ( (mtime_t)p[9] << 1 ) |
1296 ( (mtime_t)p[10] >> 7 );
1297 /* Call the pace control. */
1298 for( i_dummy = 0; i_dummy < p_input->stream.i_pgrm_number;
1301 if( ( ( pgrm_ts_data_t * ) p_input->stream.pp_programs[i_dummy]->
1302 p_demux_data )->i_pcr_pid == i_pid )
1304 input_ClockManageRef( p_input,
1305 p_input->stream.pp_programs[i_dummy], pcr_time );
1311 /* Trash the packet if it has no payload or if it isn't selected */
1314 input_DeletePacket( p_input->p_method_data, p_data );
1315 p_input->stream.c_packets_trashed++;
1321 /* The payload contains PSI tables */
1322 (* pf_psi_callback) ( p_input, p_data, p_es, b_unit_start );
1326 /* The payload carries a PES stream */
1327 input_GatherPES( p_input, p_data, p_es, b_unit_start, b_lost );