1 /*****************************************************************************
2 * mpeg_ts.c : Transport Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: mpeg_ts.c,v 1.17 2002/07/31 20:56:52 sam Exp $
7 * Authors: Henri Fallon <henri@via.ecp.fr>
8 * Johan Bilien <jobi@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc/input.h>
37 #if defined MODULE_NAME_IS_mpeg_ts_dvbpsi
38 # ifdef HAVE_DVBPSI_DR_H
39 # include <dvbpsi/dvbpsi.h>
40 # include <dvbpsi/descriptor.h>
41 # include <dvbpsi/pat.h>
42 # include <dvbpsi/pmt.h>
43 # include <dvbpsi/dr.h>
46 # include "descriptor.h"
47 # include "tables/pat.h"
48 # include "tables/pmt.h"
49 # include "descriptors/dr.h"
53 /*****************************************************************************
55 *****************************************************************************/
56 #define TS_READ_ONCE 200
58 /*****************************************************************************
60 *****************************************************************************/
61 static int Activate ( vlc_object_t * );
62 static int Demux ( input_thread_t * );
64 #if defined MODULE_NAME_IS_mpeg_ts
65 static void TSDemuxPSI ( input_thread_t *, data_packet_t *,
66 es_descriptor_t *, vlc_bool_t );
67 static void TSDecodePAT( input_thread_t *, es_descriptor_t *);
68 static void TSDecodePMT( input_thread_t *, es_descriptor_t *);
69 #define PSI_CALLBACK TSDemuxPSI
70 #elif defined MODULE_NAME_IS_mpeg_ts_dvbpsi
71 static void TS_DVBPSI_DemuxPSI ( input_thread_t *, data_packet_t *,
72 es_descriptor_t *, vlc_bool_t );
73 static void TS_DVBPSI_HandlePAT ( input_thread_t *, dvbpsi_pat_t * );
74 static void TS_DVBPSI_HandlePMT ( input_thread_t *, dvbpsi_pmt_t * );
75 #define PSI_CALLBACK TS_DVBPSI_DemuxPSI
78 /*****************************************************************************
80 *****************************************************************************/
82 #if defined MODULE_NAME_IS_mpeg_ts
83 set_description( _("ISO 13818-1 MPEG Transport Stream input") );
84 set_capability( "demux", 160 );
86 #elif defined MODULE_NAME_IS_mpeg_ts_dvbpsi
87 set_description( _("ISO 13818-1 MPEG Transport Stream input (libdvbpsi)") );
88 set_capability( "demux", 170 );
89 add_shortcut( "ts_dvbpsi" );
91 set_callbacks( Activate, NULL );
94 /*****************************************************************************
95 * Activate: initializes TS structures
96 *****************************************************************************/
97 static int Activate( vlc_object_t * p_this )
99 input_thread_t * p_input = (input_thread_t *)p_this;
100 es_descriptor_t * p_pat_es;
101 es_ts_data_t * p_demux_data;
102 stream_ts_data_t * p_stream_data;
105 /* Set the demux function */
106 p_input->pf_demux = Demux;
108 /* Initialize access plug-in structures. */
109 if( p_input->i_mtu == 0 )
112 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
115 /* Have a peep at the show. */
116 if( input_Peek( p_input, &p_peek, 1 ) < 1 )
118 msg_Err( p_input, "cannot peek()" );
122 if( *p_peek != TS_SYNC_CODE )
124 if( *p_input->psz_demux && !strncmp( p_input->psz_demux, "ts", 3 ) )
127 msg_Err( p_input, "this does not look like a TS stream, continuing" );
131 msg_Warn( p_input, "TS module discarded (no sync)" );
136 /* Adapt the bufsize for our only use. */
137 if( p_input->i_mtu != 0 )
139 /* Have minimum granularity to avoid bottlenecks at the input level. */
140 p_input->i_bufsize = (p_input->i_mtu / TS_PACKET_SIZE) * TS_PACKET_SIZE;
143 vlc_mutex_lock( &p_input->stream.stream_lock );
145 if( input_InitStream( p_input, sizeof( stream_ts_data_t ) ) == -1 )
150 p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
151 p_stream_data->i_pat_version = PAT_UNINITIALIZED ;
153 #ifdef MODULE_NAME_IS_mpeg_ts_dvbpsi
154 p_stream_data->p_pat_handle = (dvbpsi_handle *)
155 dvbpsi_AttachPAT( (dvbpsi_pat_callback) &TS_DVBPSI_HandlePAT, p_input );
157 if( p_stream_data->p_pat_handle == NULL )
159 msg_Err( p_input, "could not create PAT decoder" );
164 /* We'll have to catch the PAT in order to continue
165 * Then the input will catch the PMT and then the others ES
166 * The PAT es is indepedent of any program. */
167 p_pat_es = input_AddES( p_input, NULL,
168 0x00, sizeof( es_ts_data_t ) );
169 p_demux_data = (es_ts_data_t *)p_pat_es->p_demux_data;
170 p_demux_data->b_psi = 1;
171 p_demux_data->i_psi_type = PSI_IS_PAT;
172 p_demux_data->p_psi_section = malloc(sizeof(psi_section_t));
173 p_demux_data->p_psi_section->b_is_complete = 1;
175 vlc_mutex_unlock( &p_input->stream.stream_lock );
180 /*****************************************************************************
181 * Demux: reads and demuxes data packets
182 *****************************************************************************
183 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
185 *****************************************************************************/
186 static int Demux( input_thread_t * p_input )
188 int i_read_once = (p_input->i_mtu ?
189 p_input->i_bufsize / TS_PACKET_SIZE :
193 for( i = 0; i < i_read_once; i++ )
195 data_packet_t * p_data;
198 i_result = input_ReadTS( p_input, &p_data );
205 input_DemuxTS( p_input, p_data, (psi_callback_t) &PSI_CALLBACK );
208 return( i_read_once );
212 #if defined MODULE_NAME_IS_mpeg_ts
214 * PSI demultiplexing and decoding without libdvbpsi
217 /*****************************************************************************
218 * DemuxPSI : makes up complete PSI data
219 *****************************************************************************/
220 static void TSDemuxPSI( input_thread_t * p_input, data_packet_t * p_data,
221 es_descriptor_t * p_es, vlc_bool_t b_unit_start )
223 es_ts_data_t * p_demux_data;
225 p_demux_data = (es_ts_data_t *)p_es->p_demux_data;
227 #define p_psi (p_demux_data->p_psi_section)
228 #define p (p_data->p_payload_start)
232 /* unit_start set to 1 -> presence of a pointer field
233 * (see ISO/IEC 13818 (2.4.4.2) which should be set to 0x00 */
234 if( (u8)p[0] != 0x00 )
237 "non-zero pointer field found, trying to continue" );
245 /* This is the begining of a new section */
247 if( ((u8)(p[1]) & 0xc0) != 0x80 )
249 msg_Warn( p_input, "invalid PSI packet" );
254 p_psi->i_section_length = ((p[1] & 0xF) << 8) | p[2];
255 p_psi->b_section_complete = 0;
256 p_psi->i_read_in_section = 0;
257 p_psi->i_section_number = (u8)p[6];
259 if( p_psi->b_is_complete || p_psi->i_section_number == 0 )
261 /* This is a new PSI packet */
262 p_psi->b_is_complete = 0;
264 p_psi->i_version_number = ( p[5] >> 1 ) & 0x1f;
265 p_psi->i_last_section_number = (u8)p[7];
267 /* We'll write at the begining of the buffer */
268 p_psi->p_current = p_psi->buffer;
272 if( p_psi->b_section_complete )
274 /* New Section of an already started PSI */
275 p_psi->b_section_complete = 0;
277 if( p_psi->i_version_number != (( p[5] >> 1 ) & 0x1f) )
280 "PSI version differs inside same PAT" );
283 if( p_psi->i_section_number + 1 != (u8)p[6] )
286 "PSI Section discontinuity, packet lost?" );
290 p_psi->i_section_number++;
294 msg_Warn( p_input, "got unexpected new PSI section" );
301 if( !p_psi->b_trash )
304 if( (p_data->p_payload_end - p) >=
305 ( p_psi->i_section_length - p_psi->i_read_in_section ) )
307 /* The end of the section is in this TS packet */
308 memcpy( p_psi->p_current, p,
309 (p_psi->i_section_length - p_psi->i_read_in_section) );
311 p_psi->b_section_complete = 1;
313 (p_psi->i_section_length - p_psi->i_read_in_section);
315 if( p_psi->i_section_number == p_psi->i_last_section_number )
317 /* This was the last section of PSI */
318 p_psi->b_is_complete = 1;
320 switch( p_demux_data->i_psi_type)
323 TSDecodePAT( p_input, p_es );
326 TSDecodePMT( p_input, p_es );
329 msg_Warn( p_input, "received unknown PSI in DemuxPSI" );
335 memcpy( p_psi->buffer, p, p_data->p_payload_end - p );
336 p_psi->i_read_in_section += p_data->p_payload_end - p;
338 p_psi->p_current += p_data->p_payload_end - p;
345 input_DeletePacket( p_input->p_method_data, p_data );
350 /*****************************************************************************
351 * DecodePAT : Decodes Programm association table and deal with it
352 *****************************************************************************/
353 static void TSDecodePAT( input_thread_t * p_input, es_descriptor_t * p_es )
355 stream_ts_data_t * p_stream_data;
356 es_ts_data_t * p_demux_data;
358 pgrm_descriptor_t * p_pgrm;
359 es_descriptor_t * p_current_es;
360 byte_t * p_current_data;
362 int i_section_length, i_program_id, i_pmt_pid;
363 int i_loop, i_current_section;
365 vlc_bool_t b_changed = 0;
367 p_demux_data = (es_ts_data_t *)p_es->p_demux_data;
368 p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
370 #define p_psi (p_demux_data->p_psi_section)
372 /* Not so fast, Mike ! If the PAT version has changed, we first check
373 * that its content has really changed before doing anything */
374 if( p_stream_data->i_pat_version != p_psi->i_version_number )
376 int i_programs = p_input->stream.i_pgrm_number;
378 p_current_data = p_psi->buffer;
382 i_section_length = ((u32)(p_current_data[1] & 0xF) << 8) |
384 i_current_section = (u8)p_current_data[6];
387 ( i_loop < (i_section_length - 9) / 4 ) && !b_changed;
390 i_program_id = ( (u32)*(p_current_data + i_loop * 4 + 8) << 8 )
391 | *(p_current_data + i_loop * 4 + 9);
392 i_pmt_pid = ( ((u32)*(p_current_data + i_loop * 4 + 10) & 0x1F)
394 | *(p_current_data + i_loop * 4 + 11);
398 if( (p_pgrm = input_FindProgram( p_input, i_program_id ))
399 && (p_current_es = input_FindES( p_input, i_pmt_pid ))
400 && p_current_es->p_pgrm == p_pgrm
401 && p_current_es->i_id == i_pmt_pid
402 && ((es_ts_data_t *)p_current_es->p_demux_data)->b_psi
403 && ((es_ts_data_t *)p_current_es->p_demux_data)
404 ->i_psi_type == PSI_IS_PMT )
415 p_current_data += 3 + i_section_length;
417 } while( ( i_current_section < p_psi->i_last_section_number )
420 /* If we didn't find the expected amount of programs, the PAT has
421 * changed. Otherwise, it only changed if b_changed is already != 0 */
422 b_changed = b_changed || i_programs;
427 /* PAT has changed. We are going to delete all programs and
428 * create new ones. We chose not to only change what was needed
429 * as a PAT change may mean the stream is radically changing and
430 * this is a secure method to avoid crashes */
431 es_ts_data_t * p_es_demux;
432 pgrm_ts_data_t * p_pgrm_demux;
434 p_current_data = p_psi->buffer;
436 /* Delete all programs */
437 while( p_input->stream.i_pgrm_number )
439 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
444 i_section_length = ((u32)(p_current_data[1] & 0xF) << 8) |
446 i_current_section = (u8)p_current_data[6];
448 for( i_loop = 0; i_loop < (i_section_length - 9) / 4 ; i_loop++ )
450 i_program_id = ( (u32)*(p_current_data + i_loop * 4 + 8) << 8 )
451 | *(p_current_data + i_loop * 4 + 9);
452 i_pmt_pid = ( ((u32)*(p_current_data + i_loop * 4 + 10) & 0x1F)
454 | *(p_current_data + i_loop * 4 + 11);
456 /* If program = 0, we're having info about NIT not PMT */
459 /* Add this program */
460 p_pgrm = input_AddProgram( p_input, i_program_id,
461 sizeof( pgrm_ts_data_t ) );
463 /* whatis the PID of the PMT of this program */
464 p_pgrm_demux = (pgrm_ts_data_t *)p_pgrm->p_demux_data;
465 p_pgrm_demux->i_pmt_version = PMT_UNINITIALIZED;
467 /* Add the PMT ES to this program */
468 p_current_es = input_AddES( p_input, p_pgrm,(u16)i_pmt_pid,
469 sizeof( es_ts_data_t) );
470 p_es_demux = (es_ts_data_t *)p_current_es->p_demux_data;
471 p_es_demux->b_psi = 1;
472 p_es_demux->i_psi_type = PSI_IS_PMT;
474 p_es_demux->p_psi_section =
475 malloc( sizeof( psi_section_t ) );
476 p_es_demux->p_psi_section->b_is_complete = 0;
480 p_current_data += 3 + i_section_length;
482 } while( i_current_section < p_psi->i_last_section_number );
484 /* Go to the beginning of the next section */
485 p_stream_data->i_pat_version = p_psi->i_version_number;
492 /*****************************************************************************
493 * DecodePMT : decode a given Program Stream Map
494 * ***************************************************************************
495 * When the PMT changes, it may mean a deep change in the stream, and it is
496 * careful to delete the ES and add them again. If the PMT doesn't change,
497 * there no need to do anything.
498 *****************************************************************************/
499 static void TSDecodePMT( input_thread_t * p_input, es_descriptor_t * p_es )
502 pgrm_ts_data_t * p_pgrm_data;
503 es_ts_data_t * p_demux_data;
505 p_demux_data = (es_ts_data_t *)p_es->p_demux_data;
506 p_pgrm_data = (pgrm_ts_data_t *)p_es->p_pgrm->p_demux_data;
508 #define p_psi (p_demux_data->p_psi_section)
510 if( p_psi->i_version_number != p_pgrm_data->i_pmt_version )
512 es_descriptor_t * p_new_es;
513 es_ts_data_t * p_es_demux;
514 byte_t * p_current_data, * p_current_section;
515 int i_section_length,i_current_section;
516 int i_prog_info_length, i_loop;
517 int i_es_info_length, i_pid, i_stream_type;
519 p_current_section = p_psi->buffer;
520 p_current_data = p_psi->buffer;
522 p_pgrm_data->i_pcr_pid = ( ((u32)*(p_current_section + 8) & 0x1F) << 8 ) |
523 *(p_current_section + 9);
526 /* Lock stream information */
527 vlc_mutex_lock( &p_input->stream.stream_lock );
529 /* Delete all ES in this program except the PSI. We start from the
530 * end because i_es_number gets decremented after each deletion. */
531 for( i_loop = p_es->p_pgrm->i_es_number ; i_loop ; )
534 p_es_demux = (es_ts_data_t *)
535 p_es->p_pgrm->pp_es[i_loop]->p_demux_data;
536 if ( ! p_es_demux->b_psi )
538 input_DelES( p_input, p_es->p_pgrm->pp_es[i_loop] );
542 /* Then add what we received in this PMT */
545 i_section_length = ( ((u32)*(p_current_data + 1) & 0xF) << 8 ) |
546 *(p_current_data + 2);
547 i_current_section = (u8)p_current_data[6];
548 i_prog_info_length = ( ((u32)*(p_current_data + 10) & 0xF) << 8 ) |
549 *(p_current_data + 11);
551 /* For the moment we ignore program descriptors */
552 p_current_data += 12 + i_prog_info_length;
554 /* The end of the section, before the CRC is at
555 * p_current_section + i_section_length -1 */
556 while( p_current_data < p_current_section + i_section_length -1 )
558 i_stream_type = (int)p_current_data[0];
559 i_pid = ( ((u32)*(p_current_data + 1) & 0x1F) << 8 ) |
560 *(p_current_data + 2);
561 i_es_info_length = ( ((u32)*(p_current_data + 3) & 0xF) << 8 ) |
562 *(p_current_data + 4);
564 /* Add this ES to the program */
565 p_new_es = input_AddES( p_input, p_es->p_pgrm,
566 (u16)i_pid, sizeof( es_ts_data_t ) );
568 /* Tell the interface what kind of stream it is and select
569 * the required ones */
571 switch( i_stream_type )
575 p_new_es->i_fourcc = VLC_FOURCC('m','p','g','v');
576 p_new_es->i_cat = VIDEO_ES;
580 p_new_es->i_fourcc = VLC_FOURCC('m','p','g','a');
581 p_new_es->i_cat = AUDIO_ES;
584 p_new_es->i_fourcc = VLC_FOURCC('l','p','c','m');
585 p_new_es->i_stream_id = 0xBD;
586 p_new_es->i_cat = AUDIO_ES;
589 p_new_es->i_fourcc = VLC_FOURCC('a','5','2',' ');
590 p_new_es->i_stream_id = 0xBD;
591 p_new_es->i_cat = AUDIO_ES;
593 /* Not sure this one is fully specification-compliant */
595 p_new_es->i_fourcc = VLC_FOURCC('s','p','u',' ');
596 p_new_es->i_stream_id = 0xBD;
597 p_new_es->i_cat = SPU_ES;
600 p_new_es->i_fourcc = 0;
601 p_new_es->i_cat = UNKNOWN_ES;
606 p_current_data += 5 + i_es_info_length;
609 /* Go to the beginning of the next section*/
610 p_current_data += 3 + i_section_length;
614 } while( i_current_section < p_psi->i_last_section_number );
616 p_pgrm_data->i_pmt_version = p_psi->i_version_number;
618 /* if no program is selected :*/
619 if( !p_input->stream.p_selected_program )
621 pgrm_descriptor_t * p_pgrm_to_select;
622 u16 i_id = (u16)config_GetInt( p_input, "program" );
624 if( i_id != 0 ) /* if user specified a program */
626 p_pgrm_to_select = input_FindProgram( p_input, i_id );
628 if( p_pgrm_to_select && p_pgrm_to_select == p_es->p_pgrm )
629 p_input->pf_set_program( p_input, p_pgrm_to_select );
632 p_input->pf_set_program( p_input, p_es->p_pgrm );
635 /* inform interface that stream has changed */
636 p_input->stream.b_changed = 1;
638 vlc_mutex_unlock( &p_input->stream.stream_lock );
644 #elif defined MODULE_NAME_IS_mpeg_ts_dvbpsi
646 * PSI Decoding using libdvbcss
649 /*****************************************************************************
650 * DemuxPSI : send the PSI to the right libdvbpsi decoder
651 *****************************************************************************/
652 static void TS_DVBPSI_DemuxPSI( input_thread_t * p_input,
653 data_packet_t * p_data,
654 es_descriptor_t * p_es,
655 vlc_bool_t b_unit_start )
657 es_ts_data_t * p_es_demux_data;
658 pgrm_ts_data_t * p_pgrm_demux_data;
659 stream_ts_data_t * p_stream_demux_data;
661 p_es_demux_data = ( es_ts_data_t * ) p_es->p_demux_data;
662 p_stream_demux_data = ( stream_ts_data_t * ) p_input->stream.p_demux_data;
664 switch( p_es_demux_data->i_psi_type)
668 ( dvbpsi_handle ) p_stream_demux_data->p_pat_handle,
669 p_data->p_demux_start );
672 p_pgrm_demux_data = ( pgrm_ts_data_t * )p_es->p_pgrm->p_demux_data;
674 ( dvbpsi_handle ) p_pgrm_demux_data->p_pmt_handle,
675 p_data->p_demux_start );
678 msg_Warn( p_input, "received unknown PSI in DemuxPSI" );
681 input_DeletePacket( p_input->p_method_data, p_data );
684 /*****************************************************************************
685 * HandlePAT: will treat a PAT returned by dvbpsi
686 *****************************************************************************/
688 void TS_DVBPSI_HandlePAT( input_thread_t * p_input, dvbpsi_pat_t * p_new_pat )
690 dvbpsi_pat_program_t * p_pgrm;
691 pgrm_descriptor_t * p_new_pgrm;
692 pgrm_ts_data_t * p_pgrm_demux;
693 es_descriptor_t * p_current_es;
694 es_ts_data_t * p_es_demux;
695 stream_ts_data_t * p_stream_data;
697 vlc_mutex_lock( &p_input->stream.stream_lock );
699 p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
701 if ( !p_new_pat->b_current_next ||
702 p_stream_data->i_pat_version == PAT_UNINITIALIZED )
704 /* Delete all programs */
705 while( p_input->stream.i_pgrm_number )
707 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
710 /* treat the new programs list */
711 p_pgrm = p_new_pat->p_first_program;
715 /* If program = 0, we're having info about NIT not PMT */
716 if( p_pgrm->i_number )
718 /* Add this program */
719 p_new_pgrm = input_AddProgram( p_input, p_pgrm->i_number,
720 sizeof( pgrm_ts_data_t ) );
722 p_pgrm_demux = (pgrm_ts_data_t *)p_new_pgrm->p_demux_data;
723 p_pgrm_demux->i_pmt_version = PMT_UNINITIALIZED;
725 /* Add the PMT ES to this program */
726 p_current_es = input_AddES( p_input, p_new_pgrm,
728 sizeof( es_ts_data_t) );
729 p_es_demux = (es_ts_data_t *)p_current_es->p_demux_data;
730 p_es_demux->b_psi = 1;
731 p_es_demux->i_psi_type = PSI_IS_PMT;
733 p_es_demux->p_psi_section = malloc( sizeof( psi_section_t ) );
734 if ( p_es_demux->p_psi_section == NULL )
736 msg_Err( p_input, "out of memory" );
737 p_input->b_error = 1;
741 p_es_demux->p_psi_section->b_is_complete = 0;
743 /* Create a PMT decoder */
744 p_pgrm_demux->p_pmt_handle = (dvbpsi_handle *)
745 dvbpsi_AttachPMT( p_pgrm->i_number,
746 (dvbpsi_pmt_callback) &TS_DVBPSI_HandlePMT,
749 if( p_pgrm_demux->p_pmt_handle == NULL )
751 msg_Err( p_input, "could not create PMT decoder" );
752 p_input->b_error = 1;
757 p_pgrm = p_pgrm->p_next;
760 p_stream_data->i_pat_version = p_new_pat->i_version;
762 vlc_mutex_unlock( &p_input->stream.stream_lock );
765 /*****************************************************************************
766 * HandlePMT: will treat a PMT returned by dvbpsi
767 *****************************************************************************/
768 void TS_DVBPSI_HandlePMT( input_thread_t * p_input, dvbpsi_pmt_t * p_new_pmt )
770 dvbpsi_pmt_es_t * p_es;
771 pgrm_descriptor_t * p_pgrm;
772 es_descriptor_t * p_new_es;
773 pgrm_ts_data_t * p_pgrm_demux;
775 vlc_mutex_lock( &p_input->stream.stream_lock );
777 p_pgrm = input_FindProgram( p_input, p_new_pmt->i_program_number );
781 msg_Warn( p_input, "PMT of unreferenced program found" );
785 p_pgrm_demux = (pgrm_ts_data_t *)p_pgrm->p_demux_data;
786 p_pgrm_demux->i_pcr_pid = p_new_pmt->i_pcr_pid;
788 if( !p_new_pmt->b_current_next ||
789 p_pgrm_demux->i_pmt_version == PMT_UNINITIALIZED )
791 p_es = p_new_pmt->p_first_es;
795 p_new_es = input_AddES( p_input, p_pgrm,
796 (u16)p_es->i_pid, sizeof( es_ts_data_t ) );
797 if( p_new_es == NULL )
799 msg_Err( p_input, "could not add ES %d", p_es->i_pid );
800 p_input->b_error = 1;
804 switch( p_es->i_type )
808 p_new_es->i_fourcc = VLC_FOURCC('m','p','g','v');
809 p_new_es->i_cat = VIDEO_ES;
813 p_new_es->i_fourcc = VLC_FOURCC('m','p','g','a');
814 p_new_es->i_cat = AUDIO_ES;
817 p_new_es->i_fourcc = VLC_FOURCC('l','p','c','m');
818 p_new_es->i_cat = AUDIO_ES;
819 p_new_es->i_stream_id = 0xBD;
822 p_new_es->i_fourcc = VLC_FOURCC('a','5','2',' ');
823 p_new_es->i_cat = AUDIO_ES;
824 p_new_es->i_stream_id = 0xBD;
827 p_new_es->i_fourcc = VLC_FOURCC('s','p','u',' ');
828 p_new_es->i_cat = SPU_ES;
829 p_new_es->i_stream_id = 0xBD;
832 p_new_es->i_fourcc = 0;
833 p_new_es->i_cat = UNKNOWN_ES;
836 if( ( p_new_es->i_cat == AUDIO_ES )
837 || (p_new_es->i_cat == SPU_ES ) )
839 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
840 while( p_dr && ( p_dr->i_tag != 0x0a ) )
844 dvbpsi_iso639_dr_t *p_decoded =
845 dvbpsi_DecodeISO639Dr( p_dr );
846 if( p_decoded->i_code_count > 0 )
848 const iso639_lang_t * p_iso;
849 p_iso = GetLang_2T(p_decoded->i_iso_639_code);
852 if(p_iso->psz_native_name[0])
853 strcpy( p_new_es->psz_desc,
854 p_iso->psz_native_name );
856 strcpy( p_new_es->psz_desc,
857 p_iso->psz_eng_name );
861 strncpy( p_new_es->psz_desc,
862 p_decoded->i_iso_639_code, 3 );
866 switch( p_es->i_type )
870 strcat( p_new_es->psz_desc, " (mpeg)" );
873 strcat( p_new_es->psz_desc, " (lpcm)" );
876 strcat( p_new_es->psz_desc, " (ac3)" );
884 /* if no program is selected :*/
885 if( !p_input->stream.p_selected_program )
887 pgrm_descriptor_t * p_pgrm_to_select;
888 u16 i_id = (u16)config_GetInt( p_input, "program" );
890 if( i_id != 0 ) /* if user specified a program */
892 p_pgrm_to_select = input_FindProgram( p_input, i_id );
894 if( p_pgrm_to_select && p_pgrm_to_select == p_pgrm )
895 p_input->pf_set_program( p_input, p_pgrm_to_select );
898 p_input->pf_set_program( p_input, p_pgrm );
900 /* if the pmt belongs to the currently selected program, we
901 * reselect it to update its ES */
902 else if( p_pgrm == p_input->stream.p_selected_program )
904 p_input->pf_set_program( p_input, p_pgrm );
907 p_pgrm_demux->i_pmt_version = p_new_pmt->i_version;
908 p_input->stream.b_changed = 1;
910 vlc_mutex_unlock( &p_input->stream.stream_lock );