1 /*****************************************************************************
2 * system.c: helper module for TS, PS and PES management
3 *****************************************************************************
4 * Copyright (C) 1998-2002 VideoLAN
5 * $Id: system.c,v 1.12 2003/03/18 23:59:07 massiot Exp $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Michel Lespinasse <walken@via.ecp.fr>
9 * BenoƮt Steiner <benny@via.ecp.fr>
10 * Samuel Hocevar <sam@zoy.org>
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() */
35 #include <vlc/input.h>
39 /*****************************************************************************
41 *****************************************************************************/
42 static int Activate ( vlc_object_t * );
44 static ssize_t ReadPS ( input_thread_t *, data_packet_t ** );
45 static es_descriptor_t * ParsePS ( input_thread_t *, data_packet_t * );
46 static void DemuxPS ( input_thread_t *, data_packet_t * );
48 static ssize_t ReadTS ( input_thread_t *, data_packet_t ** );
49 static void DemuxTS ( input_thread_t *, data_packet_t *,
52 /*****************************************************************************
54 *****************************************************************************/
56 set_description( _("generic ISO 13818-1 MPEG demultiplexing") );
57 set_capability( "mpeg-system", 100 );
58 set_callbacks( Activate, NULL );
61 /*****************************************************************************
62 * Activate: initializes helper functions
63 *****************************************************************************/
64 static int Activate ( vlc_object_t *p_this )
66 static mpeg_demux_t mpeg_demux =
67 { NULL, ReadPS, ParsePS, DemuxPS, ReadTS, DemuxTS };
69 memcpy( p_this->p_private, &mpeg_demux, sizeof( mpeg_demux ) );
75 * PES Packet management
78 /*****************************************************************************
80 *****************************************************************************
81 * Small utility function used to parse discontinuous headers safely. Copies
82 * i_buf_len bytes of data to a buffer and returns the size copied.
83 * It also solves some alignment problems on non-IA-32, non-PPC processors.
84 * This is a variation on the theme of input_ext-dec.h:GetChunk().
85 *****************************************************************************/
86 static inline size_t MoveChunk( byte_t * p_dest, data_packet_t ** pp_data_src,
87 byte_t ** pp_src, size_t i_buf_len )
91 i_available = (ptrdiff_t)((*pp_data_src)->p_payload_end - *pp_src);
92 if( i_available >= i_buf_len )
95 memcpy( p_dest, *pp_src, i_buf_len );
101 size_t i_init_len = i_buf_len;
106 memcpy( p_dest, *pp_src, i_available );
107 *pp_data_src = (*pp_data_src)->p_next;
108 i_buf_len -= i_available;
109 p_dest += i_available;
110 if( *pp_data_src == NULL )
113 return( i_init_len - i_buf_len );
115 *pp_src = (*pp_data_src)->p_payload_start;
117 i_available = (ptrdiff_t)((*pp_data_src)->p_payload_end - *pp_src);
119 while( i_available <= i_buf_len );
124 memcpy( p_dest, *pp_src, i_buf_len );
125 *pp_src += i_buf_len;
127 return( i_init_len );
131 /*****************************************************************************
133 *****************************************************************************
134 * Parse a finished PES packet and analyze its header.
135 *****************************************************************************/
136 #define PES_HEADER_SIZE 7
137 static void ParsePES( input_thread_t * p_input, es_descriptor_t * p_es )
139 data_packet_t * p_data;
141 byte_t p_header[PES_HEADER_SIZE];
144 #define p_pes (p_es->p_pes)
146 /* Parse the header. The header has a variable length, but in order
147 * to improve the algorithm, we will read the 14 bytes we may be
149 p_data = p_pes->p_first;
150 p_byte = p_data->p_payload_start;
153 if( MoveChunk( p_header, &p_data, &p_byte, PES_HEADER_SIZE )
156 msg_Warn( p_input, "PES packet too short to have a header" );
157 input_DeletePES( p_input->p_method_data, p_pes );
162 /* Get the PES size if defined */
163 p_es->i_pes_real_size = U16_AT(p_header + 4);
164 if( p_es->i_pes_real_size )
166 p_es->i_pes_real_size += 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 msg_Err( p_input, "data loss, PES packet does not start with 000001" );
175 input_DeletePES( p_input->p_method_data, p_pes );
180 unsigned 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 msg_Warn( p_input, "packet corrupted, PES sizes do not match" );
190 switch( p_header[3] )
192 case 0xBC: /* Program stream map */
193 case 0xBE: /* Padding */
194 case 0xBF: /* Private stream 2 */
197 case 0xFF: /* Program stream directory */
198 case 0xF2: /* DSMCC stream */
199 case 0xF8: /* ITU-T H.222.1 type E stream */
200 /* The payload begins immediately after the 6 bytes header, so
201 * we have finished with the parsing */
202 i_pes_header_size = 6;
206 if( (p_header[6] & 0xC0) == 0x80 )
208 /* MPEG-2 : the PES header contains at least 3 more bytes. */
210 vlc_bool_t b_has_pts, b_has_dts;
211 byte_t p_full_header[12];
213 p_pes->b_data_alignment = p_header[6] & 0x04;
215 i_max_len = MoveChunk( p_full_header, &p_data, &p_byte, 12 );
219 "PES packet too short to have a MPEG-2 header" );
220 input_DeletePES( p_input->p_method_data,
226 b_has_pts = p_full_header[0] & 0x80;
227 b_has_dts = p_full_header[0] & 0x40;
228 i_pes_header_size = p_full_header[1] + 9;
230 /* Now parse the optional header extensions */
236 "PES packet too short to have a MPEG-2 header" );
237 input_DeletePES( p_input->p_method_data,
242 p_pes->i_pts = input_ClockGetTS( p_input, p_es->p_pgrm,
243 ( ((mtime_t)(p_full_header[2] & 0x0E) << 29) |
244 ((mtime_t)(p_full_header[3]) << 22) |
245 ((mtime_t)(p_full_header[4] & 0xFE) << 14) |
246 ((mtime_t)p_full_header[5] << 7) |
247 ((mtime_t)p_full_header[6] >> 1) ) );
254 "PES packet too short to have a MPEG-2 header" );
255 input_DeletePES( p_input->p_method_data,
260 p_pes->i_dts = input_ClockGetTS( p_input, p_es->p_pgrm,
261 ( ((mtime_t)(p_full_header[7] & 0x0E) << 29) |
262 (((mtime_t)U16_AT(p_full_header + 8) << 14)
264 ((mtime_t)U16_AT(p_full_header + 10) >> 1) ) );
270 /* Probably MPEG-1 */
271 vlc_bool_t b_has_pts, b_has_dts;
273 i_pes_header_size = 6;
274 p_data = p_pes->p_first;
275 p_byte = p_data->p_payload_start;
276 /* Cannot fail because the previous one succeeded. */
277 MoveChunk( NULL, &p_data, &p_byte, 6 );
279 while( *p_byte == 0xFF && i_pes_header_size < 23 )
282 if( MoveChunk( NULL, &p_data, &p_byte, 1 ) != 1 )
285 "PES packet too short to have a MPEG-1 header" );
286 input_DeletePES( p_input->p_method_data, p_pes );
291 if( i_pes_header_size == 23 )
293 msg_Err( p_input, "too much MPEG-1 stuffing" );
294 input_DeletePES( p_input->p_method_data, p_pes );
299 if( (*p_byte & 0xC0) == 0x40 )
301 /* Don't ask why... --Meuuh */
302 /* Erm... why ? --Sam */
303 /* Well... According to the recommendation, it is for
304 * STD_buffer_scale and STD_buffer_size. --Meuuh */
305 i_pes_header_size += 2;
306 if( MoveChunk( NULL, &p_data, &p_byte, 2 ) != 2 )
309 "PES packet too short to have a MPEG-1 header" );
310 input_DeletePES( p_input->p_method_data, p_pes );
318 b_has_pts = *p_byte & 0x20;
319 b_has_dts = *p_byte & 0x10;
325 i_pes_header_size += 4;
326 if( MoveChunk( p_ts, &p_data, &p_byte, 5 ) != 5 )
329 "PES packet too short to have a MPEG-1 header" );
330 input_DeletePES( p_input->p_method_data, p_pes );
335 p_pes->i_pts = input_ClockGetTS( p_input, p_es->p_pgrm,
336 ( ((mtime_t)(p_ts[0] & 0x0E) << 29) |
337 (((mtime_t)U32_AT(p_ts) & 0xFFFE00) << 6) |
338 ((mtime_t)p_ts[3] << 7) |
339 ((mtime_t)p_ts[4] >> 1) ) );
343 i_pes_header_size += 5;
344 if( MoveChunk( p_ts, &p_data, &p_byte, 5 ) != 5 )
347 "PES packet too short to have a MPEG-1 header" );
348 input_DeletePES( p_input->p_method_data, p_pes );
353 p_pes->i_dts = input_ClockGetTS( p_input,
355 ( ((mtime_t)(p_ts[0] & 0x0E) << 29) |
356 (((mtime_t)U32_AT(p_ts) & 0xFFFE00) << 6) |
357 ((mtime_t)p_ts[3] << 7) |
358 ((mtime_t)p_ts[4] >> 1) ) );
366 /* Welcome to the kludge area ! --Meuuh */
367 if ( p_es->i_fourcc == VLC_FOURCC('a','5','2','b') )
369 /* With A/52 audio, we need to skip the first 4 bytes */
370 i_pes_header_size += 4;
373 if ( p_es->i_fourcc == VLC_FOURCC('l','p','c','b')
374 || p_es->i_fourcc == VLC_FOURCC('s','p','u','b')
375 || p_es->i_fourcc == VLC_FOURCC('d','t','s','b')
376 || p_es->i_fourcc == VLC_FOURCC('s','d','d','b') )
378 /* stream_private_id */
379 i_pes_header_size += 1;
382 /* Now we've parsed the header, we just have to indicate in some
383 * specific data packets where the PES payload begins (renumber
384 * p_payload_start), so that the decoders can find the beginning
385 * of their data right out of the box. */
386 p_data = p_pes->p_first;
387 i_payload_size = p_data->p_payload_end
388 - p_data->p_payload_start;
389 while( i_pes_header_size > i_payload_size )
391 /* These packets are entirely filled by the PES header. */
392 i_pes_header_size -= i_payload_size;
393 p_data->p_payload_start = p_data->p_payload_end;
394 /* Go to the next data packet. */
395 if( (p_data = p_data->p_next) == NULL )
397 msg_Err( p_input, "PES header bigger than payload" );
398 input_DeletePES( p_input->p_method_data, p_pes );
402 i_payload_size = p_data->p_payload_end
403 - p_data->p_payload_start;
405 /* This last packet is partly header, partly payload. */
406 if( i_payload_size < i_pes_header_size )
408 msg_Err( p_input, "PES header bigger than payload" );
409 input_DeletePES( p_input->p_method_data, p_pes );
413 p_data->p_payload_start += i_pes_header_size;
416 /* Now we can eventually put the PES packet in the decoder's
418 if( p_es->p_decoder_fifo != NULL )
420 input_DecodePES( p_es->p_decoder_fifo, p_pes );
424 msg_Err( p_input, "no fifo to receive PES %p "
425 "(who wrote this damn code ?)", p_pes );
426 input_DeletePES( p_input->p_method_data, p_pes );
434 /*****************************************************************************
436 *****************************************************************************
437 * Gather a PES packet.
438 *****************************************************************************/
439 static void GatherPES( input_thread_t * p_input, data_packet_t * p_data,
440 es_descriptor_t * p_es,
441 vlc_bool_t b_unit_start, vlc_bool_t b_packet_lost )
443 #define p_pes (p_es->p_pes)
445 /* If we lost data, insert a NULL data packet (philosophy : 0 is quite
446 * often an escape sequence in decoders, so that should make them wait
447 * for the next start code). */
450 input_NullPacket( p_input, p_es );
453 if( b_unit_start && p_pes != NULL )
455 /* If the data packet contains the begining of a new PES packet, and
456 * if we were reassembling a PES packet, then the PES should be
457 * complete now, so parse its header and give it to the decoders. */
458 ParsePES( p_input, p_es );
461 if( !b_unit_start && p_pes == NULL )
463 /* Random access... */
464 input_DeletePacket( p_input->p_method_data, p_data );
470 /* If we are at the beginning of a new PES packet, we must fetch
471 * a new PES buffer to begin with the reassembly of this PES
472 * packet. This is also here that we can synchronize with the
473 * stream if we lost packets or if the decoder has just
475 if( (p_pes = input_NewPES( p_input->p_method_data ) ) == NULL )
477 msg_Err( p_input, "out of memory" );
478 p_input->b_error = 1;
481 p_pes->i_rate = p_input->stream.control.i_rate;
482 p_pes->p_first = p_data;
484 /* If the PES header fits in the first data packet, we can
485 * already set p_gather->i_pes_real_size. */
486 if( p_data->p_payload_end - p_data->p_payload_start
489 p_es->i_pes_real_size = ((u16)p_data->p_payload_start[4] << 8)
490 + p_data->p_payload_start[5] + 6;
494 p_es->i_pes_real_size = 0;
499 /* Update the relations between the data packets */
500 p_pes->p_last->p_next = p_data;
503 p_pes->p_last = p_data;
506 /* Size of the payload carried in the data packet */
507 p_pes->i_pes_size += (p_data->p_payload_end
508 - p_data->p_payload_start);
510 /* We can check if the packet is finished */
511 if( p_pes->i_pes_size == p_es->i_pes_real_size )
513 /* The packet is finished, parse it */
514 ParsePES( p_input, p_es );
525 /*****************************************************************************
526 * GetID: Get the ID of a stream
527 *****************************************************************************/
528 static u16 GetID( data_packet_t * p_data )
532 i_id = p_data->p_demux_start[3]; /* stream_id */
535 /* FIXME : this is not valid if the header is split in multiple
537 /* stream_private_id */
538 i_id |= p_data->p_demux_start[ 9 + p_data->p_demux_start[8] ] << 8;
543 /*****************************************************************************
544 * DecodePSM: Decode the Program Stream Map information
545 *****************************************************************************
546 * FIXME : loads are not aligned in this function
547 *****************************************************************************/
548 static void DecodePSM( input_thread_t * p_input, data_packet_t * p_data )
550 stream_ps_data_t * p_demux =
551 (stream_ps_data_t *)p_input->stream.p_demux_data;
555 unsigned int i_new_es_number = 0;
557 if( p_data->p_demux_start + 10 > p_data->p_payload_end )
559 msg_Err( p_input, "PSM too short, packet corrupt" );
563 if( p_demux->b_has_PSM
564 && p_demux->i_PSM_version == (p_data->p_demux_start[6] & 0x1F) )
566 /* Already got that one. */
570 p_demux->b_has_PSM = 1;
571 p_demux->i_PSM_version = p_data->p_demux_start[6] & 0x1F;
573 /* Go to elementary_stream_map_length, jumping over
574 * program_stream_info. */
575 p_byte = p_data->p_demux_start + 10
576 + U16_AT(&p_data->p_demux_start[8]);
577 if( p_byte > p_data->p_payload_end )
579 msg_Err( p_input, "PSM too short, packet corrupt" );
582 /* This is the full size of the elementary_stream_map.
583 * 2 == elementary_stream_map_length
584 * Please note that CRC_32 is not included in the length. */
585 p_end = p_byte + 2 + U16_AT(p_byte);
587 if( p_end > p_data->p_payload_end )
589 msg_Err( p_input, "PSM too short, packet corrupt" );
593 vlc_mutex_lock( &p_input->stream.stream_lock );
595 /* 4 == minimum useful size of a section */
596 while( p_byte + 4 <= p_end )
598 es_descriptor_t * p_es = NULL;
599 uint8_t i_stream_id = p_byte[1];
600 /* FIXME: there will be a problem with private streams... (same
603 /* Look for the ES in the ES table */
604 for( i = i_new_es_number;
605 i < p_input->stream.pp_programs[0]->i_es_number;
608 if( p_input->stream.pp_programs[0]->pp_es[i]->i_stream_id
611 p_es = p_input->stream.pp_programs[0]->pp_es[i];
612 /* FIXME: do something below */
614 if( p_es->i_type != p_byte[0] )
616 input_DelES( p_input, p_es );
622 /* Move the ES to the beginning. */
623 p_input->stream.pp_programs[0]->pp_es[i]
624 = p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ];
625 p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ]
633 /* The goal is to have all the ES we have just read in the
634 * beginning of the pp_es table, and all the others at the end,
635 * so that we can close them more easily at the end. */
638 p_es = input_AddES( p_input, p_input->stream.pp_programs[0],
644 case MPEG2_MOTO_VIDEO_ES:
645 p_es->i_fourcc = VLC_FOURCC('m','p','g','v');
646 p_es->i_cat = VIDEO_ES;
649 p_es->i_fourcc = VLC_FOURCC('s','p','u','b');
650 p_es->i_cat = SPU_ES;
654 p_es->i_fourcc = VLC_FOURCC('m','p','g','a');
655 p_es->i_cat = AUDIO_ES;
658 case A52DVB_AUDIO_ES:
659 p_es->i_fourcc = VLC_FOURCC('a','5','2','b');
660 p_es->i_cat = AUDIO_ES;
663 p_es->i_fourcc = VLC_FOURCC('l','p','c','b');
664 p_es->i_cat = AUDIO_ES;
671 /* input_AddES has inserted the new element at the end. */
672 p_input->stream.pp_programs[0]->pp_es[
673 p_input->stream.pp_programs[0]->i_es_number ]
674 = p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ];
675 p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ] = p_es;
678 p_byte += 4 + U16_AT(&p_byte[2]);
681 /* Un-select the streams that are no longer parts of the program. */
682 while( i_new_es_number < p_input->stream.pp_programs[0]->i_es_number )
684 /* We remove pp_es[i_new_es_member] and not pp_es[i] because the
685 * list will be emptied starting from the end */
686 input_DelES( p_input,
687 p_input->stream.pp_programs[0]->pp_es[i_new_es_number] );
690 msg_Dbg( p_input, "the stream map after the PSM is now:" );
691 input_DumpStream( p_input );
693 vlc_mutex_unlock( &p_input->stream.stream_lock );
696 /*****************************************************************************
697 * ReadPS: store a PS packet into a data_buffer_t
698 *****************************************************************************/
699 #define PEEK( SIZE ) \
700 i_error = input_Peek( p_input, &p_peek, SIZE ); \
701 if( i_error == -1 ) \
705 else if( (size_t)i_error < SIZE ) \
711 static ssize_t ReadPS( input_thread_t * p_input, data_packet_t ** pp_data )
714 size_t i_packet_size;
715 ssize_t i_error, i_read;
717 /* Read what we believe to be a packet header. */
720 if( p_peek[0] || p_peek[1] || p_peek[2] != 1 || p_peek[3] < 0xB9 )
722 if( p_peek[0] || p_peek[1] || p_peek[2] )
724 /* It is common for MPEG-1 streams to pad with zeros
725 * (although it is forbidden by the recommendation), so
726 * don't bother everybody in this case. */
727 msg_Warn( p_input, "garbage (0x%.2x%.2x%.2x%.2x)",
728 p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
731 /* This is not the startcode of a packet. Read the stream
732 * until we find one. */
733 while( p_peek[0] || p_peek[1] || p_peek[2] != 1 || p_peek[3] < 0xB9 )
735 p_input->p_current_data++;
737 if( p_input->b_die ) return( -1 );
742 /* 0x1B9 == SYSTEM_END_CODE, it is only 4 bytes long. */
743 if( p_peek[3] != 0xB9 )
745 /* The packet is at least 6 bytes long. */
748 if( p_peek[3] != 0xBA )
750 /* That's the case for all packets, except pack header. */
751 i_packet_size = (p_peek[4] << 8) | p_peek[5];
756 if( (p_peek[4] & 0xC0) == 0x40 )
761 else if( (p_peek[4] & 0xF0) == 0x20 )
768 msg_Err( p_input, "unable to determine stream type" );
775 /* System End Code */
779 /* Fetch a packet of the appropriate size. */
780 i_read = input_SplitBuffer( p_input, pp_data, i_packet_size + 6 );
786 /* In MPEG-2 pack headers we still have to read stuffing bytes. */
787 if( ((*pp_data)->p_demux_start[3] == 0xBA) && (i_packet_size == 8) )
789 size_t i_stuffing = ((*pp_data)->p_demux_start[13] & 0x7);
790 /* Force refill of the input buffer - though we don't care
791 * about p_peek. Please note that this is unoptimized. */
793 p_input->p_current_data += i_stuffing;
801 /*****************************************************************************
802 * ParsePS: read the PS header
803 *****************************************************************************/
804 static es_descriptor_t * ParsePS( input_thread_t * p_input,
805 data_packet_t * p_data )
808 es_descriptor_t * p_es = NULL;
810 i_code = p_data->p_demux_start[3];
812 if( i_code > 0xBC ) /* ES start code */
815 unsigned int i_dummy;
817 /* This is a PES packet. Find out if we want it or not. */
818 i_id = GetID( p_data );
820 vlc_mutex_lock( &p_input->stream.stream_lock );
821 if( p_input->stream.pp_programs[0]->b_is_ok )
823 /* Look only at the selected ES. */
824 for( i_dummy = 0; i_dummy < p_input->stream.i_selected_es_number;
827 if( p_input->stream.pp_selected_es[i_dummy] != NULL
828 && p_input->stream.pp_selected_es[i_dummy]->i_id == i_id )
830 p_es = p_input->stream.pp_selected_es[i_dummy];
837 stream_ps_data_t * p_demux =
838 (stream_ps_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
840 /* Search all ES ; if not found -> AddES */
841 p_es = input_FindES( p_input, i_id );
843 if( p_es == NULL && !p_demux->b_has_PSM )
845 p_es = input_AddES( p_input, p_input->stream.pp_programs[0],
849 p_es->i_stream_id = p_data->p_demux_start[3];
851 /* Set stream type and auto-spawn. */
852 if( (i_id & 0xF0) == 0xE0 )
855 p_es->i_fourcc = VLC_FOURCC('m','p','g','v');
856 p_es->i_cat = VIDEO_ES;
858 if( !p_input->stream.b_seekable )
859 input_SelectES( p_input, p_es );
862 else if( (i_id & 0xE0) == 0xC0 )
865 p_es->i_fourcc = VLC_FOURCC('m','p','g','a');
866 p_es->i_cat = AUDIO_ES;
868 if( !p_input->stream.b_seekable )
869 if( config_GetInt( p_input, "audio-channel" )
870 == (p_es->i_id & 0x1F) ||
871 ( config_GetInt( p_input, "audio-channel" ) < 0
872 && !(p_es->i_id & 0x1F) ) )
873 switch( config_GetInt( p_input, "audio-type" ) )
877 input_SelectES( p_input, p_es );
881 else if( (i_id & 0xF8FF) == 0x88BD )
883 p_es->i_fourcc = VLC_FOURCC('d','t','s','b');
884 p_es->i_cat = AUDIO_ES;
886 if( !p_input->stream.b_seekable )
887 if( config_GetInt( p_input, "audio-channel" )
888 == ((p_es->i_id & 0x700) >> 8) ||
889 ( config_GetInt( p_input, "audio-channel" ) < 0
890 && !((p_es->i_id & 0x700) >> 8)) )
891 switch( config_GetInt( p_input, "audio-type" ) )
895 input_SelectES( p_input, p_es );
899 else if( (i_id & 0xF0FF) == 0x80BD )
901 /* A52 audio (0x80->0x8F) */
902 p_es->i_fourcc = VLC_FOURCC('a','5','2','b');
903 p_es->i_cat = AUDIO_ES;
905 if( !p_input->stream.b_seekable )
906 if( config_GetInt( p_input, "audio-channel" )
907 == ((p_es->i_id & 0xF00) >> 8) ||
908 ( config_GetInt( p_input, "audio-channel" ) < 0
909 && !((p_es->i_id & 0xF00) >> 8)) )
910 switch( config_GetInt( p_input, "audio-type" ) )
914 input_SelectES( p_input, p_es );
918 else if( (i_id & 0xE0FF) == 0x20BD )
920 /* Subtitles video (0x20->0x3F) */
921 p_es->i_fourcc = VLC_FOURCC('s','p','u','b');
922 p_es->i_cat = SPU_ES;
924 if( config_GetInt( p_input, "spu-channel" )
925 == ((p_es->i_id & 0x1F00) >> 8) )
927 if( !p_input->stream.b_seekable )
928 input_SelectES( p_input, p_es );
932 else if( (i_id & 0xF0FF) == 0xA0BD )
934 /* LPCM audio (0xA0->0xAF) */
935 p_es->i_fourcc = VLC_FOURCC('l','p','c','b');
936 p_es->i_cat = AUDIO_ES;
944 /* Tell the interface the stream has changed */
945 p_input->stream.b_changed = 1;
947 } /* stream.b_is_ok */
948 vlc_mutex_unlock( &p_input->stream.stream_lock );
949 } /* i_code > 0xBC */
954 /*****************************************************************************
955 * DemuxPS: first step of demultiplexing: the PS header
956 *****************************************************************************/
957 static void DemuxPS( input_thread_t * p_input, data_packet_t * p_data )
960 vlc_bool_t b_trash = 0;
961 es_descriptor_t * p_es = NULL;
963 i_code = ((u32)p_data->p_demux_start[0] << 24)
964 | ((u32)p_data->p_demux_start[1] << 16)
965 | ((u32)p_data->p_demux_start[2] << 8)
966 | p_data->p_demux_start[3];
967 if( i_code <= 0x1BC )
971 case 0x1BA: /* PACK_START_CODE */
977 if( (p_data->p_demux_start[4] & 0xC0) == 0x40 )
982 p_byte = p_data->p_demux_start;
984 if( MoveChunk( p_header, &p_data, &p_byte, 14 ) != 14 )
987 "packet too short to have a header" );
992 ((mtime_t)(p_header[4] & 0x38) << 27) |
993 ((mtime_t)(U32_AT(p_header + 4) & 0x03FFF800)
995 ((( ((mtime_t)U16_AT(p_header + 6) << 16)
996 | (mtime_t)U16_AT(p_header + 8) ) & 0x03FFF800)
1000 i_mux_rate = ((u32)U16_AT(p_header + 10) << 6)
1001 | (p_header[12] >> 2);
1002 /* FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1003 * This is the biggest kludge ever !
1004 * I don't know what's wrong with mux_rate calculation
1005 * but this heuristic works well : */
1011 /* MPEG-1 SCR is like PTS. */
1012 byte_t p_header[12];
1014 p_byte = p_data->p_demux_start;
1016 if( MoveChunk( p_header, &p_data, &p_byte, 12 ) != 12 )
1019 "packet too short to have a header" );
1024 ((mtime_t)(p_header[4] & 0x0E) << 29) |
1025 (((mtime_t)U32_AT(p_header + 4) & 0xFFFE00) << 6) |
1026 ((mtime_t)p_header[7] << 7) |
1027 ((mtime_t)p_header[8] >> 1);
1030 i_mux_rate = (U32_AT(p_header + 8) & 0x7FFFFE) >> 1;
1032 /* Call the pace control. */
1033 input_ClockManageRef( p_input,
1034 p_input->stream.p_selected_program,
1037 if( i_mux_rate != p_input->stream.i_mux_rate
1038 && p_input->stream.i_mux_rate )
1041 "mux_rate changed, expect cosmetic errors" );
1043 p_input->stream.i_mux_rate = i_mux_rate;
1049 case 0x1BB: /* SYSTEM_START_CODE */
1050 b_trash = 1; /* Nothing interesting */
1053 case 0x1BC: /* PROGRAM_STREAM_MAP_CODE */
1054 DecodePSM( p_input, p_data );
1058 case 0x1B9: /* PROGRAM_END_CODE */
1063 /* This should not happen */
1065 msg_Warn( p_input, "unwanted packet received "
1066 "with startcode 0x%.8x", i_code );
1071 p_es = ParsePS( p_input, p_data );
1073 vlc_mutex_lock( &p_input->stream.control.control_lock );
1074 if( p_es != NULL && p_es->p_decoder_fifo != NULL
1075 && (p_es->i_cat != AUDIO_ES || !p_input->stream.control.b_mute) )
1077 vlc_mutex_unlock( &p_input->stream.control.control_lock );
1079 GatherPES( p_input, p_data, p_es, 1, 0 );
1083 vlc_mutex_unlock( &p_input->stream.control.control_lock );
1088 /* Trash the packet if it has no payload or if it isn't selected */
1091 input_DeletePacket( p_input->p_method_data, p_data );
1092 p_input->stream.c_packets_trashed++;
1101 /*****************************************************************************
1102 * ReadTS: store a TS packet into a data_buffer_t
1103 *****************************************************************************/
1104 #define PEEK( SIZE ) \
1105 i_error = input_Peek( p_input, &p_peek, SIZE ); \
1106 if( i_error == -1 ) \
1110 else if( i_error < SIZE ) \
1116 static ssize_t ReadTS( input_thread_t * p_input, data_packet_t ** pp_data )
1119 ssize_t i_error, i_read;
1123 if( *p_peek != TS_SYNC_CODE )
1125 msg_Warn( p_input, "garbage at input (%x)", *p_peek );
1127 if( p_input->i_mtu )
1129 while( *p_peek != TS_SYNC_CODE )
1131 /* Try to resync on next packet. */
1132 PEEK( TS_PACKET_SIZE );
1133 p_input->p_current_data += TS_PACKET_SIZE;
1139 /* Move forward until we find 0x47 (and hope it's the good
1141 while( *p_peek != TS_SYNC_CODE )
1143 p_input->p_current_data++;
1149 i_read = input_SplitBuffer( p_input, pp_data, TS_PACKET_SIZE );
1158 /*****************************************************************************
1159 * DemuxTS: first step of demultiplexing: the TS header
1160 *****************************************************************************/
1161 static void DemuxTS( input_thread_t * p_input, data_packet_t * p_data,
1162 psi_callback_t pf_psi_callback )
1165 unsigned int i_dummy;
1166 vlc_bool_t b_adaptation; /* Adaptation field is present */
1167 vlc_bool_t b_payload; /* Packet carries payload */
1168 vlc_bool_t b_unit_start; /* A PSI or a PES start in the packet */
1169 vlc_bool_t b_trash = 0; /* Is the packet unuseful ? */
1170 vlc_bool_t b_lost = 0; /* Was there a packet loss ? */
1171 vlc_bool_t b_psi = 0; /* Is this a PSI ? */
1172 vlc_bool_t b_pcr = 0; /* Does it have a PCR ? */
1173 es_descriptor_t * p_es = NULL;
1174 es_ts_data_t * p_es_demux = NULL;
1175 pgrm_ts_data_t * p_pgrm_demux = NULL;
1176 stream_ts_data_t * p_stream_demux =
1177 (stream_ts_data_t *)p_input->stream.p_demux_data;
1179 #define p (p_data->p_demux_start)
1180 /* Extract flags values from TS common header. */
1181 i_pid = ((p[1] & 0x1F) << 8) | p[2];
1182 b_unit_start = (p[1] & 0x40);
1183 b_adaptation = (p[3] & 0x20);
1184 b_payload = (p[3] & 0x10);
1186 /* Find out the elementary stream. */
1187 vlc_mutex_lock( &p_input->stream.stream_lock );
1189 for( i_dummy = 0; i_dummy < p_input->stream.i_pgrm_number; i_dummy ++ )
1191 if( (( pgrm_ts_data_t * ) p_input->stream.pp_programs[i_dummy]->
1192 p_demux_data)->i_pcr_pid == i_pid )
1199 p_es = input_FindES( p_input, i_pid );
1201 if( (p_es != NULL) && (p_es->p_demux_data != NULL) )
1203 p_es_demux = (es_ts_data_t *)p_es->p_demux_data;
1205 if( p_es_demux->b_psi )
1211 p_pgrm_demux = (pgrm_ts_data_t *)p_es->p_pgrm->p_demux_data;
1215 vlc_mutex_lock( &p_input->stream.control.control_lock );
1216 if( ( p_es == NULL ) || (p_es->i_cat == AUDIO_ES
1217 && p_input->stream.control.b_mute) )
1219 /* Not selected. Just read the adaptation field for a PCR. */
1222 else if( p_es->p_decoder_fifo == NULL && !b_psi )
1227 vlc_mutex_unlock( &p_input->stream.control.control_lock );
1228 vlc_mutex_unlock( &p_input->stream.stream_lock );
1231 /* Don't change the order of the tests : if b_psi then p_pgrm_demux
1232 * may still be null. Who said it was ugly ?
1233 * I have written worse. --Meuuh */
1235 ((p_es->p_decoder_fifo != NULL) || b_psi || b_pcr ) )
1239 /* Extract adaptation field information if any */
1243 /* We don't have any adaptation_field, so payload starts
1244 * immediately after the 4 byte TS header */
1245 p_data->p_payload_start += 4;
1249 /* p[4] is adaptation_field_length minus one */
1250 p_data->p_payload_start += 5 + p[4];
1252 /* The adaptation field can be limited to the
1253 * adaptation_field_length byte, so that there is nothing to do:
1254 * skip this possibility */
1257 /* If the packet has both adaptation_field and payload,
1258 * adaptation_field cannot be more than 182 bytes long; if
1259 * there is only an adaptation_field, it must fill the next
1261 if( b_payload ? (p[4] > 182) : (p[4] != 183) )
1263 msg_Warn( p_input, "invalid TS adaptation field (%p)",
1265 p_data->b_discard_payload = 1;
1266 p_es->c_invalid_packets++;
1269 /* Now we are sure that the byte containing flags is present:
1273 /* discontinuity_indicator */
1277 "discontinuity_indicator encountered by TS demux "
1278 "(position read: %d, saved: %d)",
1279 p[5] & 0x80, p_es_demux->i_continuity_counter );
1281 /* If the PID carries the PCR, there will be a system
1282 * time-based discontinuity. We let the PCR decoder
1284 p_es->p_pgrm->i_synchro_state = SYNCHRO_REINIT;
1286 /* There also may be a continuity_counter
1287 * discontinuity: resynchronize our counter with
1288 * the one of the stream. */
1289 p_es_demux->i_continuity_counter = (p[3] & 0x0f) - 1;
1292 } /* valid TS adaptation field ? */
1294 } /* has adaptation field */
1295 /* Check the continuity of the stream. */
1296 i_dummy = ((p[3] & 0x0f) - p_es_demux->i_continuity_counter) & 0x0f;
1297 if( i_dummy == 1 || (b_psi && p_stream_demux->b_buggy_psi) )
1299 /* Everything is ok, just increase our counter */
1300 (p_es_demux->i_continuity_counter)++;
1304 if( !b_payload && i_dummy == 0 )
1306 /* This is a packet without payload, this is allowed by the
1307 * draft. As there is nothing interesting in this packet
1308 * (except PCR that have already been handled), we can trash
1312 else if( i_dummy <= 0 )
1314 /* Duplicate packet: mark it as being to be trashed. */
1316 "duplicate packet received by TS demux (%d)",
1320 else if( p_es_demux->i_continuity_counter == 0xFF )
1322 /* This means that the packet is the first one we receive for
1323 * this ES since the continuity counter ranges between 0 and
1324 * 0x0F excepts when it has been initialized by the input:
1325 * init the counter to the correct value. */
1326 msg_Warn( p_input, "first packet for PID %d received "
1327 "by TS demux", p_es->i_id );
1328 p_es_demux->i_continuity_counter = (p[3] & 0x0f);
1332 /* This can indicate that we missed a packet or that the
1333 * continuity_counter wrapped and we received a dup packet:
1334 * as we don't know, do as if we missed a packet to be sure
1335 * to recover from this situation */
1337 "packet lost by TS demux: current %d, packet %d",
1338 p_es_demux->i_continuity_counter & 0x0f,
1341 p_es_demux->i_continuity_counter = p[3] & 0x0f;
1342 } /* not continuous */
1344 } /* if selected or PCR */
1347 if( b_pcr && b_adaptation && (p[5] & 0x10) && p[4]>=7 )
1351 pcr_time = ( (mtime_t)p[6] << 25 ) |
1352 ( (mtime_t)p[7] << 17 ) |
1353 ( (mtime_t)p[8] << 9 ) |
1354 ( (mtime_t)p[9] << 1 ) |
1355 ( (mtime_t)p[10] >> 7 );
1356 /* Call the pace control. */
1357 for( i_dummy = 0; i_dummy < p_input->stream.i_pgrm_number;
1360 if( ( ( pgrm_ts_data_t * ) p_input->stream.pp_programs[i_dummy]->
1361 p_demux_data )->i_pcr_pid == i_pid )
1363 input_ClockManageRef( p_input,
1364 p_input->stream.pp_programs[i_dummy], pcr_time );
1370 /* Trash the packet if it has no payload or if it isn't selected */
1373 input_DeletePacket( p_input->p_method_data, p_data );
1374 p_input->stream.c_packets_trashed++;
1380 /* The payload contains PSI tables */
1381 (* pf_psi_callback) ( p_input, p_data, p_es, b_unit_start );
1385 /* The payload carries a PES stream */
1386 GatherPES( p_input, p_data, p_es, b_unit_start, b_lost );