1 /*****************************************************************************
2 * system.c: helper module for TS, PS and PES management
3 *****************************************************************************
4 * Copyright (C) 1998-2004 VideoLAN
5 * $Id: system.c,v 1.30 2004/01/25 20:05:28 hartman 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 * Sam 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 *****************************************************************************/
34 #include <vlc/input.h>
38 /*****************************************************************************
40 *****************************************************************************/
41 static int Activate ( vlc_object_t * );
43 static ssize_t ReadPS ( input_thread_t *, data_packet_t ** );
44 static es_descriptor_t * ParsePS ( input_thread_t *, data_packet_t * );
45 static void DemuxPS ( input_thread_t *, data_packet_t * );
47 static ssize_t ReadTS ( input_thread_t *, data_packet_t ** );
48 static void DemuxTS ( input_thread_t *, data_packet_t *,
51 /*****************************************************************************
53 *****************************************************************************/
55 set_description( _("Generic ISO 13818-1 MPEG demultiplexing") );
56 set_capability( "mpeg-system", 100 );
57 set_callbacks( Activate, NULL );
60 /*****************************************************************************
61 * Activate: initializes helper functions
62 *****************************************************************************/
63 static int Activate ( vlc_object_t *p_this )
65 static mpeg_demux_t mpeg_demux =
66 { NULL, ReadPS, ParsePS, DemuxPS, ReadTS, DemuxTS };
67 mpeg_demux.cur_scr_time = -1;
68 memcpy( p_this->p_private, &mpeg_demux, sizeof( mpeg_demux ) );
74 * PES Packet management
77 /*****************************************************************************
79 *****************************************************************************
80 * Small utility function used to parse discontinuous headers safely. Copies
81 * i_buf_len bytes of data to a buffer and returns the size copied.
82 * It also solves some alignment problems on non-IA-32, non-PPC processors.
83 * This is a variation on the theme of input_ext-dec.h:GetChunk().
84 *****************************************************************************/
85 static inline size_t MoveChunk( byte_t * p_dest, data_packet_t ** pp_data_src,
86 byte_t ** pp_src, size_t i_buf_len )
90 i_available = (ptrdiff_t)((*pp_data_src)->p_payload_end - *pp_src);
91 if( i_available >= i_buf_len )
94 memcpy( p_dest, *pp_src, i_buf_len );
100 size_t i_init_len = i_buf_len;
105 memcpy( p_dest, *pp_src, i_available );
106 *pp_data_src = (*pp_data_src)->p_next;
107 i_buf_len -= i_available;
108 p_dest += i_available;
109 if( *pp_data_src == NULL )
112 return( i_init_len - i_buf_len );
114 *pp_src = (*pp_data_src)->p_payload_start;
116 i_available = (ptrdiff_t)((*pp_data_src)->p_payload_end - *pp_src);
118 while( i_available <= i_buf_len );
123 memcpy( p_dest, *pp_src, i_buf_len );
124 *pp_src += i_buf_len;
126 return( i_init_len );
130 /*****************************************************************************
132 *****************************************************************************
133 * Parse a finished PES packet and analyze its header.
134 *****************************************************************************/
135 #define PES_HEADER_SIZE 7
136 static void ParsePES( input_thread_t * p_input, es_descriptor_t * p_es )
138 data_packet_t * p_data;
140 byte_t p_header[PES_HEADER_SIZE];
143 #define p_pes (p_es->p_pes)
145 /* Parse the header. The header has a variable length, but in order
146 * to improve the algorithm, we will read the 14 bytes we may be
148 p_data = p_pes->p_first;
149 p_byte = p_data->p_payload_start;
152 if( MoveChunk( p_header, &p_data, &p_byte, PES_HEADER_SIZE )
155 msg_Warn( p_input, "PES packet too short to have a header" );
156 input_DeletePES( p_input->p_method_data, p_pes );
161 /* Get the PES size if defined */
162 p_es->i_pes_real_size = U16_AT(p_header + 4);
163 if( p_es->i_pes_real_size )
165 p_es->i_pes_real_size += 6;
168 /* First read the 6 header bytes common to all PES packets:
169 * use them to test the PES validity */
170 if( (p_header[0] || p_header[1] || (p_header[2] != 1)) )
172 /* packet_start_code_prefix != 0x000001 */
173 msg_Warn( p_input, "data loss, PES packet does not start with 000001" );
174 input_DeletePES( p_input->p_method_data, p_pes );
179 unsigned int i_pes_header_size, i_payload_size;
181 if ( p_es->i_pes_real_size &&
182 (p_es->i_pes_real_size != p_pes->i_pes_size) )
184 /* PES_packet_length is set and != total received payload */
185 /* Warn the decoder that the data may be corrupt. */
186 msg_Warn( p_input, "packet corrupted, PES sizes do not match" );
189 switch( p_header[3] )
191 case 0xBC: /* Program stream map */
192 case 0xBE: /* Padding */
193 case 0xBF: /* Private stream 2 */
196 case 0xFF: /* Program stream directory */
197 case 0xF2: /* DSMCC stream */
198 case 0xF8: /* ITU-T H.222.1 type E stream */
199 /* The payload begins immediately after the 6 bytes header, so
200 * we have finished with the parsing */
201 i_pes_header_size = 6;
205 if( (p_header[6] & 0xC0) == 0x80 )
207 /* MPEG-2 : the PES header contains at least 3 more bytes. */
209 vlc_bool_t b_has_pts, b_has_dts;
210 byte_t p_full_header[12];
212 p_pes->b_data_alignment = p_header[6] & 0x04;
214 i_max_len = MoveChunk( p_full_header, &p_data, &p_byte, 12 );
218 "PES packet too short to have a MPEG-2 header" );
219 input_DeletePES( p_input->p_method_data,
225 b_has_pts = p_full_header[0] & 0x80;
226 b_has_dts = p_full_header[0] & 0x40;
227 i_pes_header_size = p_full_header[1] + 9;
229 /* Now parse the optional header extensions */
235 "PES packet too short to have a MPEG-2 header" );
236 input_DeletePES( p_input->p_method_data,
241 p_pes->i_pts = input_ClockGetTS( p_input, p_es->p_pgrm,
242 ( ((mtime_t)(p_full_header[2] & 0x0E) << 29) |
243 ((mtime_t)(p_full_header[3]) << 22) |
244 ((mtime_t)(p_full_header[4] & 0xFE) << 14) |
245 ((mtime_t)p_full_header[5] << 7) |
246 ((mtime_t)p_full_header[6] >> 1) ) );
253 "PES packet too short to have a MPEG-2 header" );
254 input_DeletePES( p_input->p_method_data,
259 p_pes->i_dts = input_ClockGetTS( p_input, p_es->p_pgrm,
260 ( ((mtime_t)(p_full_header[7] & 0x0E) << 29) |
261 (((mtime_t)U16_AT(p_full_header + 8) << 14)
263 ((mtime_t)U16_AT(p_full_header + 10) >> 1) ) );
269 /* Probably MPEG-1 */
270 vlc_bool_t b_has_pts, b_has_dts;
272 i_pes_header_size = 6;
273 p_data = p_pes->p_first;
274 p_byte = p_data->p_payload_start;
275 /* Cannot fail because the previous one succeeded. */
276 MoveChunk( NULL, &p_data, &p_byte, 6 );
278 while( *p_byte == 0xFF && i_pes_header_size < 23 )
281 if( MoveChunk( NULL, &p_data, &p_byte, 1 ) != 1 )
284 "PES packet too short to have a MPEG-1 header" );
285 input_DeletePES( p_input->p_method_data, p_pes );
290 if( i_pes_header_size == 23 )
292 msg_Warn( p_input, "too much MPEG-1 stuffing" );
293 input_DeletePES( p_input->p_method_data, p_pes );
298 if( (*p_byte & 0xC0) == 0x40 )
300 /* Don't ask why... --Meuuh */
301 /* Erm... why ? --Sam */
302 /* Well... According to the recommendation, it is for
303 * STD_buffer_scale and STD_buffer_size. --Meuuh */
304 i_pes_header_size += 2;
305 if( MoveChunk( NULL, &p_data, &p_byte, 2 ) != 2 )
308 "PES packet too short to have a MPEG-1 header" );
309 input_DeletePES( p_input->p_method_data, p_pes );
317 b_has_pts = *p_byte & 0x20;
318 b_has_dts = *p_byte & 0x10;
324 i_pes_header_size += 4;
325 if( MoveChunk( p_ts, &p_data, &p_byte, 5 ) != 5 )
328 "PES packet too short to have a MPEG-1 header" );
329 input_DeletePES( p_input->p_method_data, p_pes );
334 p_pes->i_pts = input_ClockGetTS( p_input, p_es->p_pgrm,
335 ( ((mtime_t)(p_ts[0] & 0x0E) << 29) |
336 (((mtime_t)U32_AT(p_ts) & 0xFFFE00) << 6) |
337 ((mtime_t)p_ts[3] << 7) |
338 ((mtime_t)p_ts[4] >> 1) ) );
342 i_pes_header_size += 5;
343 if( MoveChunk( p_ts, &p_data, &p_byte, 5 ) != 5 )
346 "PES packet too short to have a MPEG-1 header" );
347 input_DeletePES( p_input->p_method_data, p_pes );
352 p_pes->i_dts = input_ClockGetTS( p_input,
354 ( ((mtime_t)(p_ts[0] & 0x0E) << 29) |
355 (((mtime_t)U32_AT(p_ts) & 0xFFFE00) << 6) |
356 ((mtime_t)p_ts[3] << 7) |
357 ((mtime_t)p_ts[4] >> 1) ) );
365 /* Welcome to the kludge area ! --Meuuh */
366 if ( p_es->i_fourcc == VLC_FOURCC('a','5','2','b')
367 || p_es->i_fourcc == VLC_FOURCC('d','t','s','b') )
369 /* With A/52 or DTS 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('s','d','d','b') )
377 /* stream_private_id */
378 i_pes_header_size += 1;
381 /* Now we've parsed the header, we just have to indicate in some
382 * specific data packets where the PES payload begins (renumber
383 * p_payload_start), so that the decoders can find the beginning
384 * of their data right out of the box. */
385 p_data = p_pes->p_first;
386 i_payload_size = p_data->p_payload_end
387 - p_data->p_payload_start;
388 while( i_pes_header_size > i_payload_size )
390 /* These packets are entirely filled by the PES header. */
391 i_pes_header_size -= i_payload_size;
392 p_data->p_payload_start = p_data->p_payload_end;
393 /* Go to the next data packet. */
394 if( (p_data = p_data->p_next) == NULL )
396 msg_Warn( p_input, "PES header bigger than payload" );
397 input_DeletePES( p_input->p_method_data, p_pes );
401 i_payload_size = p_data->p_payload_end
402 - p_data->p_payload_start;
404 /* This last packet is partly header, partly payload. */
405 if( i_payload_size < i_pes_header_size )
407 msg_Warn( p_input, "PES header bigger than payload" );
408 input_DeletePES( p_input->p_method_data, p_pes );
412 p_data->p_payload_start += i_pes_header_size;
415 /* Now we can eventually put the PES packet in the decoder's
417 if( p_es->p_dec != NULL )
419 input_DecodePES( p_es->p_dec, p_pes );
421 else if ( p_es->p_dec == NULL &&
422 ((es_ts_data_t*)p_es->p_demux_data)->b_dvbsub)
424 es_descriptor_t* p_dvbsub;
427 i_count = ((es_ts_data_t*)p_es->p_demux_data)->i_dvbsub_es_count;
429 // If a language is selected, we send the packet to the decoder
430 for(i = 0; i < i_count; i++)
432 p_dvbsub = ((es_ts_data_t*)p_es->p_demux_data)->p_dvbsub_es[i];
433 if(p_dvbsub->p_dec !=NULL)
435 input_DecodePES ( p_dvbsub->p_dec, p_pes );
441 input_DeletePES( p_input->p_method_data, p_pes );
446 msg_Err( p_input, "no fifo to receive PES %p "
447 "(who wrote this damn code ?)", p_pes );
448 input_DeletePES( p_input->p_method_data, p_pes );
456 /*****************************************************************************
458 *****************************************************************************
459 * Gather a PES packet.
460 *****************************************************************************/
461 static void GatherPES( input_thread_t * p_input, data_packet_t * p_data,
462 es_descriptor_t * p_es,
463 vlc_bool_t b_unit_start, vlc_bool_t b_packet_lost )
465 #define p_pes (p_es->p_pes)
467 if( b_packet_lost && p_pes != NULL )
469 p_pes->b_discontinuity = 1;
472 if( b_unit_start && p_pes != NULL )
474 /* If the data packet contains the begining of a new PES packet, and
475 * if we were reassembling a PES packet, then the PES should be
476 * complete now, so parse its header and give it to the decoders. */
477 ParsePES( p_input, p_es );
480 if( !b_unit_start && p_pes == NULL )
482 /* Random access... */
483 input_DeletePacket( p_input->p_method_data, p_data );
489 /* If we are at the beginning of a new PES packet, we must fetch
490 * a new PES buffer to begin with the reassembly of this PES
491 * packet. This is also here that we can synchronize with the
492 * stream if we lost packets or if the decoder has just
494 if( (p_pes = input_NewPES( p_input->p_method_data ) ) == NULL )
496 msg_Err( p_input, "out of memory" );
497 p_input->b_error = 1;
500 p_pes->i_rate = p_input->stream.control.i_rate;
501 p_pes->p_first = p_data;
503 /* If the PES header fits in the first data packet, we can
504 * already set p_gather->i_pes_real_size. */
505 if( p_data->p_payload_end - p_data->p_payload_start
508 p_es->i_pes_real_size = ((uint16_t)p_data->p_payload_start[4] << 8)
509 + p_data->p_payload_start[5];
510 if ( p_es->i_pes_real_size )
512 p_es->i_pes_real_size += 6;
517 p_es->i_pes_real_size = 0;
522 /* Update the relations between the data packets */
523 p_pes->p_last->p_next = p_data;
526 p_pes->p_last = p_data;
529 /* Size of the payload carried in the data packet */
530 p_pes->i_pes_size += (p_data->p_payload_end
531 - p_data->p_payload_start);
533 /* We can check if the packet is finished */
534 if( p_es->i_pes_real_size && p_pes->i_pes_size >= p_es->i_pes_real_size )
536 if( p_pes->i_pes_size > p_es->i_pes_real_size )
539 "Oversized PES packet for PID %d: expected %d, actual %d",
540 p_es->i_id, p_es->i_pes_real_size, p_pes->i_pes_size );
542 /* The packet is finished, parse it */
543 ParsePES( p_input, p_es );
554 /*****************************************************************************
555 * GetID: Get the ID of a stream
556 *****************************************************************************/
557 static uint16_t GetID( input_thread_t *p_input, data_packet_t * p_data )
561 i_id = p_data->p_demux_start[3]; /* stream_id */
564 mpeg_demux_t *p_mpeg_demux = (mpeg_demux_t *)p_input->p_private;
566 /* FIXME : this is not valid if the header is split in multiple
568 /* stream_private_id */
569 i_id |= p_data->p_demux_start[ 9 + p_data->p_demux_start[8] ] << 8;
571 /* FIXME: See note about cur_scr_time above. */
572 p_mpeg_demux->cur_scr_time = -1;
577 /*****************************************************************************
578 * DecodePSM: Decode the Program Stream Map information
579 *****************************************************************************
580 * FIXME : loads are not aligned in this function
581 *****************************************************************************/
582 static void DecodePSM( input_thread_t * p_input, data_packet_t * p_data )
584 stream_ps_data_t * p_demux =
585 (stream_ps_data_t *)p_input->stream.p_demux_data;
589 unsigned int i_new_es_number = 0;
591 if( p_data->p_demux_start + 10 > p_data->p_payload_end )
593 msg_Err( p_input, "PSM too short, packet corrupt" );
597 if( p_demux->b_has_PSM
598 && p_demux->i_PSM_version == (p_data->p_demux_start[6] & 0x1F) )
600 /* Already got that one. */
604 p_demux->b_has_PSM = 1;
605 p_demux->i_PSM_version = p_data->p_demux_start[6] & 0x1F;
607 /* Go to elementary_stream_map_length, jumping over
608 * program_stream_info. */
609 p_byte = p_data->p_demux_start + 10
610 + U16_AT(&p_data->p_demux_start[8]);
611 if( p_byte > p_data->p_payload_end )
613 msg_Err( p_input, "PSM too short, packet corrupt" );
616 /* This is the full size of the elementary_stream_map.
617 * 2 == elementary_stream_map_length
618 * Please note that CRC_32 is not included in the length. */
619 p_end = p_byte + 2 + U16_AT(p_byte);
621 if( p_end > p_data->p_payload_end )
623 msg_Err( p_input, "PSM too short, packet corrupt" );
627 vlc_mutex_lock( &p_input->stream.stream_lock );
629 /* 4 == minimum useful size of a section */
630 while( p_byte + 4 <= p_end )
632 es_descriptor_t * p_es = NULL;
633 uint8_t i_stream_id = p_byte[1];
634 /* FIXME: there will be a problem with private streams... (same
637 /* Look for the ES in the ES table */
638 for( i = i_new_es_number;
639 i < p_input->stream.pp_programs[0]->i_es_number;
642 if( p_input->stream.pp_programs[0]->pp_es[i]->i_stream_id
645 p_es = p_input->stream.pp_programs[0]->pp_es[i];
646 /* FIXME: do something below */
648 if( p_es->i_type != p_byte[0] )
650 input_DelES( p_input, p_es );
656 /* Move the ES to the beginning. */
657 p_input->stream.pp_programs[0]->pp_es[i]
658 = p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ];
659 p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ]
667 /* The goal is to have all the ES we have just read in the
668 * beginning of the pp_es table, and all the others at the end,
669 * so that we can close them more easily at the end. */
678 case MPEG2_MOTO_VIDEO_ES:
679 i_fourcc = VLC_FOURCC('m','p','g','v');
683 i_fourcc = VLC_FOURCC('s','p','u','b');
688 i_fourcc = VLC_FOURCC('m','p','g','a');
692 i_fourcc = VLC_FOURCC('a','5','2','b');
696 i_fourcc = VLC_FOURCC('l','p','c','b');
705 p_es = input_AddES( p_input, p_input->stream.pp_programs[0],
706 i_stream_id, i_cat, NULL, 0 );
707 p_es->i_fourcc = i_fourcc;
709 /* input_AddES has inserted the new element at the end. */
710 p_input->stream.pp_programs[0]->pp_es[
711 p_input->stream.pp_programs[0]->i_es_number ]
712 = p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ];
713 p_input->stream.pp_programs[0]->pp_es[ i_new_es_number ] = p_es;
716 p_byte += 4 + U16_AT(&p_byte[2]);
719 /* Un-select the streams that are no longer parts of the program. */
720 while( i_new_es_number < p_input->stream.pp_programs[0]->i_es_number )
722 /* We remove pp_es[i_new_es_member] and not pp_es[i] because the
723 * list will be emptied starting from the end */
724 input_DelES( p_input,
725 p_input->stream.pp_programs[0]->pp_es[i_new_es_number] );
728 msg_Dbg( p_input, "the stream map after the PSM is now:" );
729 input_DumpStream( p_input );
731 vlc_mutex_unlock( &p_input->stream.stream_lock );
734 /*****************************************************************************
735 * ReadPS: store a PS packet into a data_buffer_t
736 *****************************************************************************/
737 #define PEEK( SIZE ) \
738 i_error = input_Peek( p_input, &p_peek, SIZE ); \
739 if( i_error == -1 ) \
743 else if( (size_t)i_error < SIZE ) \
749 static ssize_t ReadPS( input_thread_t * p_input, data_packet_t ** pp_data )
752 size_t i_packet_size;
753 ssize_t i_error, i_read;
755 /* Read what we believe to be a packet header. */
758 if( p_peek[0] || p_peek[1] || p_peek[2] != 1 || p_peek[3] < 0xB9 )
760 if( p_peek[0] || p_peek[1] || p_peek[2] )
762 /* It is common for MPEG-1 streams to pad with zeros
763 * (although it is forbidden by the recommendation), so
764 * don't bother everybody in this case. */
765 msg_Warn( p_input, "garbage (0x%.2x%.2x%.2x%.2x)",
766 p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
769 /* This is not the startcode of a packet. Read the stream
770 * until we find one. */
771 while( p_peek[0] || p_peek[1] || p_peek[2] != 1 || p_peek[3] < 0xB9 )
773 p_input->p_current_data++;
775 if( p_input->b_die ) return( -1 );
780 /* 0x1B9 == SYSTEM_END_CODE, it is only 4 bytes long. */
781 if( p_peek[3] != 0xB9 )
783 /* The packet is at least 6 bytes long. */
786 if( p_peek[3] != 0xBA )
788 /* That's the case for all packets, except pack header. */
789 i_packet_size = (p_peek[4] << 8) | p_peek[5];
794 if( (p_peek[4] & 0xC0) == 0x40 )
799 else if( (p_peek[4] & 0xF0) == 0x20 )
806 msg_Err( p_input, "unable to determine stream type" );
807 p_input->p_current_data++;
814 /* System End Code */
818 /* Fetch a packet of the appropriate size. */
819 i_read = input_SplitBuffer( p_input, pp_data, i_packet_size + 6 );
825 /* In MPEG-2 pack headers we still have to read stuffing bytes. */
826 if( ((*pp_data)->p_demux_start[3] == 0xBA) && (i_packet_size == 8) )
828 size_t i_stuffing = ((*pp_data)->p_demux_start[13] & 0x7);
829 /* Force refill of the input buffer - though we don't care
830 * about p_peek. Please note that this is unoptimized. */
832 p_input->p_current_data += i_stuffing;
840 /*****************************************************************************
841 * ParsePS: read the PS header
842 *****************************************************************************/
843 static es_descriptor_t * ParsePS( input_thread_t * p_input,
844 data_packet_t * p_data )
847 es_descriptor_t * p_es = NULL;
849 i_code = p_data->p_demux_start[3];
851 if( i_code > 0xBC ) /* ES start code */
854 unsigned int i_dummy;
856 /* This is a PES packet. Find out if we want it or not. */
857 i_id = GetID( p_input, p_data );
859 vlc_mutex_lock( &p_input->stream.stream_lock );
860 if( p_input->stream.pp_programs[0]->b_is_ok )
862 /* Look only at the selected ES. */
863 for( i_dummy = 0; i_dummy < p_input->stream.i_selected_es_number;
866 if( p_input->stream.pp_selected_es[i_dummy] != NULL
867 && p_input->stream.pp_selected_es[i_dummy]->i_id == i_id )
869 p_es = p_input->stream.pp_selected_es[i_dummy];
876 vlc_bool_t b_auto_spawn = VLC_FALSE;
877 stream_ps_data_t * p_demux =
878 (stream_ps_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
880 /* Search all ES ; if not found -> AddES */
881 p_es = input_FindES( p_input, i_id );
883 if( p_es == NULL && !p_demux->b_has_PSM )
887 /* Set stream type and auto-spawn. */
888 if( (i_id & 0xF0) == 0xE0 )
891 i_fourcc = VLC_FOURCC('m','p','g','v');
894 if( !p_input->stream.b_seekable ) b_auto_spawn = VLC_TRUE;
897 else if( (i_id & 0xE0) == 0xC0 )
900 i_fourcc = VLC_FOURCC('m','p','g','a');
903 if( !p_input->stream.b_seekable )
904 if( config_GetInt( p_input, "audio-channel" )
906 ( config_GetInt( p_input, "audio-channel" ) < 0
907 && !(i_id & 0x1F) ) )
908 switch( config_GetInt( p_input, "audio-type" ) )
912 b_auto_spawn = VLC_TRUE;
916 else if( (i_id & 0xF8FF) == 0x88BD )
918 i_fourcc = VLC_FOURCC('d','t','s','b');
921 if( !p_input->stream.b_seekable )
922 if( config_GetInt( p_input, "audio-channel" )
923 == ((i_id & 0x700) >> 8) ||
924 ( config_GetInt( p_input, "audio-channel" ) < 0
925 && !((i_id & 0x700) >> 8)) )
926 switch( config_GetInt( p_input, "audio-type" ) )
930 b_auto_spawn = VLC_TRUE;
934 else if( (i_id & 0xF0FF) == 0x80BD )
936 /* A52 audio (0x80->0x8F) */
937 i_fourcc = VLC_FOURCC('a','5','2','b');
940 if( !p_input->stream.b_seekable )
941 if( config_GetInt( p_input, "audio-channel" )
942 == ((i_id & 0xF00) >> 8) ||
943 ( config_GetInt( p_input, "audio-channel" ) < 0
944 && !((i_id & 0xF00) >> 8)) )
945 switch( config_GetInt( p_input, "audio-type" ) )
949 b_auto_spawn = VLC_TRUE;
953 else if( (i_id & 0xE0FF) == 0x20BD )
955 /* Subtitles video (0x20->0x3F) */
956 i_fourcc = VLC_FOURCC('s','p','u','b');
959 if( !p_input->stream.b_seekable )
960 if( config_GetInt( p_input, "spu-channel" )
961 == ((i_id & 0x1F00) >> 8) )
963 b_auto_spawn = VLC_TRUE;
967 else if( (i_id & 0xF0FF) == 0xA0BD )
969 /* LPCM audio (0xA0->0xAF) */
970 i_fourcc = VLC_FOURCC('l','p','c','b');
973 else if( (i_id & 0xFFFF) == 0x70BD )
975 /* SVCD OGT subtitles in stream 0x070 */
976 i_fourcc = VLC_FOURCC('o','g','t', ' ');
978 b_auto_spawn = VLC_TRUE;
980 else if( ((i_id >> 8) & 0xFF) <= 0x03 &&
981 (i_id & 0x00FF) == 0x00BD )
983 /* CVD subtitles (0x00->0x03) */
984 i_fourcc = VLC_FOURCC('c','v','d', ' ');
986 b_auto_spawn = VLC_TRUE;
994 p_es = input_AddES( p_input, p_input->stream.pp_programs[0],
995 i_id, i_cat, NULL, 0 );
997 p_es->i_stream_id = p_data->p_demux_start[3];
998 p_es->i_fourcc = i_fourcc;
1000 if( b_auto_spawn ) input_SelectES( p_input, p_es );
1002 /* Tell the interface the stream has changed */
1003 p_input->stream.b_changed = 1;
1005 } /* stream.b_is_ok */
1006 vlc_mutex_unlock( &p_input->stream.stream_lock );
1007 } /* i_code > 0xBC */
1012 /*****************************************************************************
1013 * DemuxPS: first step of demultiplexing: the PS header
1014 *****************************************************************************/
1015 static void DemuxPS( input_thread_t * p_input, data_packet_t * p_data )
1018 vlc_bool_t b_trash = 0;
1019 es_descriptor_t * p_es = NULL;
1020 mpeg_demux_t *p_mpeg_demux = (mpeg_demux_t *)p_input->p_private;
1022 i_code = ((uint32_t)p_data->p_demux_start[0] << 24)
1023 | ((uint32_t)p_data->p_demux_start[1] << 16)
1024 | ((uint32_t)p_data->p_demux_start[2] << 8)
1025 | p_data->p_demux_start[3];
1026 if( i_code <= 0x1BC )
1030 case 0x1BA: /* PACK_START_CODE */
1034 uint32_t i_mux_rate;
1036 if( (p_data->p_demux_start[4] & 0xC0) == 0x40 )
1039 byte_t p_header[14];
1041 p_byte = p_data->p_demux_start;
1043 if( MoveChunk( p_header, &p_data, &p_byte, 14 ) != 14 )
1046 "packet too short to have a header" );
1051 ((mtime_t)(p_header[4] & 0x38) << 27) |
1052 ((mtime_t)(U32_AT(p_header + 4) & 0x03FFF800)
1054 ((( ((mtime_t)U16_AT(p_header + 6) << 16)
1055 | (mtime_t)U16_AT(p_header + 8) ) & 0x03FFF800)
1059 i_mux_rate = ((uint32_t)U16_AT(p_header + 10) << 6)
1060 | (p_header[12] >> 2);
1061 /* FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
1062 * This is the biggest kludge ever !
1063 * I don't know what's wrong with mux_rate calculation
1064 * but this heuristic works well : */
1070 /* MPEG-1 SCR is like PTS. */
1071 byte_t p_header[12];
1073 p_byte = p_data->p_demux_start;
1075 if( MoveChunk( p_header, &p_data, &p_byte, 12 ) != 12 )
1078 "packet too short to have a header" );
1083 ((mtime_t)(p_header[4] & 0x0E) << 29) |
1084 (((mtime_t)U32_AT(p_header + 4) & 0xFFFE00) << 6) |
1085 ((mtime_t)p_header[7] << 7) |
1086 ((mtime_t)p_header[8] >> 1);
1089 i_mux_rate = (U32_AT(p_header + 8) & 0x7FFFFE) >> 1;
1091 p_mpeg_demux->cur_scr_time = scr_time;
1092 p_mpeg_demux->i_cur_mux_rate = i_mux_rate;
1098 case 0x1BB: /* SYSTEM_START_CODE */
1099 b_trash = 1; /* Nothing interesting */
1102 case 0x1BC: /* PROGRAM_STREAM_MAP_CODE */
1103 DecodePSM( p_input, p_data );
1107 case 0x1B9: /* PROGRAM_END_CODE */
1112 /* This should not happen */
1114 msg_Warn( p_input, "unwanted packet received "
1115 "with startcode 0x%.8x", i_code );
1120 p_es = ParsePS( p_input, p_data );
1122 /* Call the pace control.
1123 * FIXME: see hack note about cur_scr_time in system.h.
1125 if( p_mpeg_demux->cur_scr_time != -1 )
1127 input_ClockManageRef( p_input,
1128 p_input->stream.p_selected_program,
1129 p_mpeg_demux->cur_scr_time );
1131 if( p_mpeg_demux->i_cur_mux_rate != p_input->stream.i_mux_rate
1132 && p_input->stream.i_mux_rate )
1135 "mux_rate changed prev: %ud, cur: %ud;"
1136 " expect cosmetic errors" ,
1137 (unsigned int) p_input->stream.i_mux_rate,
1138 (unsigned int) p_mpeg_demux->i_cur_mux_rate );
1140 p_input->stream.i_mux_rate = p_mpeg_demux->i_cur_mux_rate;
1142 vlc_mutex_lock( &p_input->stream.control.control_lock );
1143 if( p_es != NULL && p_es->p_dec != NULL
1144 && (p_es->i_cat != AUDIO_ES || !p_input->stream.control.b_mute) )
1146 vlc_mutex_unlock( &p_input->stream.control.control_lock );
1148 GatherPES( p_input, p_data, p_es, 1, 0 );
1152 vlc_mutex_unlock( &p_input->stream.control.control_lock );
1157 /* Trash the packet if it has no payload or if it isn't selected */
1160 input_DeletePacket( p_input->p_method_data, p_data );
1161 p_input->stream.c_packets_trashed++;
1170 /*****************************************************************************
1171 * ReadTS: store a TS packet into a data_buffer_t
1172 *****************************************************************************/
1173 #define PEEK( SIZE ) \
1174 i_error = input_Peek( p_input, &p_peek, SIZE ); \
1175 if( i_error == -1 ) \
1179 else if( i_error < SIZE ) \
1185 static ssize_t ReadTS( input_thread_t * p_input, data_packet_t ** pp_data )
1188 ssize_t i_error, i_read;
1192 if( *p_peek != TS_SYNC_CODE )
1194 msg_Warn( p_input, "garbage at input (%x)", *p_peek );
1196 if( p_input->i_mtu )
1198 while( *p_peek != TS_SYNC_CODE )
1200 /* Try to resync on next packet. */
1201 PEEK( TS_PACKET_SIZE );
1202 p_input->p_current_data += TS_PACKET_SIZE;
1208 /* Move forward until we find 0x47 (and hope it's the good
1210 while( *p_peek != TS_SYNC_CODE )
1212 p_input->p_current_data++;
1218 i_read = input_SplitBuffer( p_input, pp_data, TS_PACKET_SIZE );
1227 /*****************************************************************************
1228 * DemuxTS: first step of demultiplexing: the TS header
1229 *****************************************************************************/
1230 static void DemuxTS( input_thread_t * p_input, data_packet_t * p_data,
1231 psi_callback_t pf_psi_callback )
1234 unsigned int i_dummy;
1235 vlc_bool_t b_adaptation; /* Adaptation field is present */
1236 vlc_bool_t b_payload; /* Packet carries payload */
1237 vlc_bool_t b_unit_start; /* A PSI or a PES start in the packet */
1238 vlc_bool_t b_trash = 0; /* Is the packet unuseful ? */
1239 vlc_bool_t b_lost = 0; /* Was there a packet loss ? */
1240 vlc_bool_t b_psi = 0; /* Is this a PSI ? */
1241 vlc_bool_t b_dvbsub = 0; /* Is this a dvb subtitle ? */
1242 vlc_bool_t b_pcr = 0; /* Does it have a PCR ? */
1243 es_descriptor_t * p_es = NULL;
1244 es_ts_data_t * p_es_demux = NULL;
1245 pgrm_ts_data_t * p_pgrm_demux = NULL;
1246 stream_ts_data_t * p_stream_demux =
1247 (stream_ts_data_t *)p_input->stream.p_demux_data;
1249 #define p (p_data->p_demux_start)
1250 /* Extract flags values from TS common header. */
1251 i_pid = ((p[1] & 0x1F) << 8) | p[2];
1252 b_unit_start = (p[1] & 0x40);
1253 b_adaptation = (p[3] & 0x20);
1254 b_payload = (p[3] & 0x10);
1256 /* Was there a transport error ? */
1259 msg_Warn( p_input, "transport_error_indicator set for PID %d counter %x", i_pid, p[3] & 0x0f );
1262 /* Find out the elementary stream. */
1263 vlc_mutex_lock( &p_input->stream.stream_lock );
1265 for( i_dummy = 0; i_dummy < p_input->stream.i_pgrm_number; i_dummy ++ )
1267 if( (( pgrm_ts_data_t * ) p_input->stream.pp_programs[i_dummy]->
1268 p_demux_data)->i_pcr_pid == i_pid )
1275 p_es = input_FindES( p_input, i_pid );
1277 if( (p_es != NULL) && (p_es->p_demux_data != NULL) )
1279 p_es_demux = (es_ts_data_t *)p_es->p_demux_data;
1281 if( p_es_demux->b_psi )
1285 else if ( p_es_demux->b_dvbsub )
1291 p_pgrm_demux = (pgrm_ts_data_t *)p_es->p_pgrm->p_demux_data;
1295 vlc_mutex_lock( &p_input->stream.control.control_lock );
1296 if( ( p_es == NULL ) || (p_es->i_cat == AUDIO_ES
1297 && p_input->stream.control.b_mute) )
1299 /* Not selected. Just read the adaptation field for a PCR. */
1302 else if( p_es->p_dec == NULL && !b_psi && !b_dvbsub )
1307 vlc_mutex_unlock( &p_input->stream.control.control_lock );
1308 vlc_mutex_unlock( &p_input->stream.stream_lock );
1311 /* Don't change the order of the tests : if b_psi then p_pgrm_demux
1312 * may still be null. Who said it was ugly ?
1313 * I have written worse. --Meuuh */
1315 ((p_es->p_dec != NULL) || b_psi || b_pcr || b_dvbsub) )
1319 /* Extract adaptation field information if any */
1323 /* We don't have any adaptation_field, so payload starts
1324 * immediately after the 4 byte TS header */
1325 p_data->p_payload_start += 4;
1329 /* p[4] is adaptation_field_length minus one */
1330 p_data->p_payload_start += 5 + p[4];
1332 /* The adaptation field can be limited to the
1333 * adaptation_field_length byte, so that there is nothing to do:
1334 * skip this possibility */
1337 /* If the packet has both adaptation_field and payload,
1338 * adaptation_field cannot be more than 182 bytes long; if
1339 * there is only an adaptation_field, it must fill the next
1341 if( b_payload ? (p[4] > 182) : (p[4] != 183) )
1343 msg_Warn( p_input, "invalid TS adaptation field for PID %d (%2x)",
1345 p_data->b_discard_payload = 1;
1346 p_es->c_invalid_packets++;
1348 /* The length was invalid so we shouldn't have added it to
1349 * p_payload_start above. Ensure p_payload_start has a
1350 * valid value by setting it equal to p_payload_end. This
1351 * also stops any data being processed from the packet.
1353 p_data->p_payload_start = p_data->p_payload_end;
1356 /* Now we are sure that the byte containing flags is present:
1360 /* discontinuity_indicator */
1364 "discontinuity_indicator encountered by TS demux "
1365 "(PID %d: current %d, packet %d)",
1367 ( p_es_demux->i_continuity_counter ) & 0x0f,
1370 /* If the PID carries the PCR, there will be a system
1371 * time-based discontinuity. We let the PCR decoder
1373 p_es->p_pgrm->i_synchro_state = SYNCHRO_REINIT;
1375 /* Don't resynchronise the counter here - it will
1376 * be checked later and b_lost will then be set if
1381 } /* valid TS adaptation field ? */
1383 } /* has adaptation field */
1384 /* Check the continuity of the stream. */
1385 i_dummy = ((p[3] & 0x0f) - p_es_demux->i_continuity_counter) & 0x0f;
1387 ( i_dummy == 1 || (b_psi && p_stream_demux->b_buggy_psi ) ) )
1389 /* Everything is ok, just increase our counter */
1390 (p_es_demux->i_continuity_counter)++;
1394 if( !b_payload && i_dummy == 0 )
1396 /* This is a packet without payload, this is allowed by the
1397 * draft. As there is nothing interesting in this packet
1398 * (except PCR that have already been handled), we can trash
1402 else if( !b_payload )
1404 /* If there is no payload, the counter should be unchanged */
1405 msg_Warn( p_input, "packet rxd for PID %d with no payload but "
1406 "wrong counter: current %d, packet %d", i_pid,
1407 p_es_demux->i_continuity_counter & 0x0f, p[3] & 0x0f );
1409 else if( i_dummy <= 0 )
1411 /* Duplicate packet: mark it as being to be trashed. */
1413 "duplicate packet received for PID %d (counter %d)",
1414 p_es->i_id, p[3] & 0x0f );
1417 else if( p_es_demux->i_continuity_counter == 0xFF )
1419 /* This means that the packet is the first one we receive for
1420 * this ES since the continuity counter ranges between 0 and
1421 * 0x0F excepts when it has been initialized by the input:
1422 * init the counter to the correct value. */
1423 msg_Warn( p_input, "first packet for PID %d received "
1424 "by TS demux", p_es->i_id );
1425 p_es_demux->i_continuity_counter = (p[3] & 0x0f);
1429 /* This can indicate that we missed a packet or that the
1430 * continuity_counter wrapped and we received a dup packet:
1431 * as we don't know, do as if we missed a packet to be sure
1432 * to recover from this situation */
1434 "packet lost by TS demux for PID %d: current %d, packet %d",
1436 p_es_demux->i_continuity_counter & 0x0f,
1439 p_es_demux->i_continuity_counter = p[3] & 0x0f;
1440 } /* not continuous */
1442 } /* if selected or PCR */
1445 if( b_pcr && b_adaptation && (p[5] & 0x10) && p[4]>=7 )
1449 pcr_time = ( (mtime_t)p[6] << 25 ) |
1450 ( (mtime_t)p[7] << 17 ) |
1451 ( (mtime_t)p[8] << 9 ) |
1452 ( (mtime_t)p[9] << 1 ) |
1453 ( (mtime_t)p[10] >> 7 );
1454 /* Call the pace control. */
1455 for( i_dummy = 0; i_dummy < p_input->stream.i_pgrm_number;
1458 if( ( ( pgrm_ts_data_t * ) p_input->stream.pp_programs[i_dummy]->
1459 p_demux_data )->i_pcr_pid == i_pid )
1461 input_ClockManageRef( p_input,
1462 p_input->stream.pp_programs[i_dummy], pcr_time );
1468 /* Trash the packet if it has no payload or if it isn't selected */
1471 input_DeletePacket( p_input->p_method_data, p_data );
1472 p_input->stream.c_packets_trashed++;
1478 /* The payload contains PSI tables */
1479 (* pf_psi_callback) ( p_input, p_data, p_es, b_unit_start );
1483 /* The payload carries a PES stream */
1484 GatherPES( p_input, p_data, p_es, b_unit_start, b_lost );