1 /*****************************************************************************
2 * mpeg_ts.c : Transport Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2004 VideoLAN
5 * $Id: ts.c,v 1.46 2004/01/25 20:05:28 hartman 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 *****************************************************************************/
30 #include <vlc/input.h>
34 #if defined MODULE_NAME_IS_ts_dvbpsi
35 # ifdef HAVE_DVBPSI_DR_H
36 # include <dvbpsi/dvbpsi.h>
37 # include <dvbpsi/descriptor.h>
38 # include <dvbpsi/pat.h>
39 # include <dvbpsi/pmt.h>
40 # include <dvbpsi/dr.h>
43 # include "descriptor.h"
44 # include "tables/pat.h"
45 # include "tables/pmt.h"
46 # include "descriptors/dr.h"
53 /*****************************************************************************
55 *****************************************************************************/
56 #define TS_READ_ONCE 200
58 /*****************************************************************************
60 *****************************************************************************/
68 #define local_iso639_getlang(p1, p2) \
70 const iso639_lang_t * p_iso; \
71 p_iso = GetLang_2T((char*)(p1)); \
72 if( p_iso && strcmp(p_iso->psz_native_name,"Unknown")) \
74 if( p_iso->psz_native_name[0] ) \
75 strncpy( (p2), p_iso->psz_native_name, 20 ); \
77 strncpy( (p2), p_iso->psz_eng_name, 20 ); \
81 p_iso = GetLang_2B((char*)(p1)); \
84 if( p_iso->psz_native_name[0] ) \
85 strncpy( (p2), p_iso->psz_native_name, 20 ); \
87 strncpy( (p2), p_iso->psz_eng_name, 20 ); \
91 strncpy( (p2), p1, 3 ); \
96 /*****************************************************************************
98 *****************************************************************************/
99 static int Activate ( vlc_object_t * );
100 static void Deactivate ( vlc_object_t * );
101 static int Demux ( input_thread_t * );
103 #if defined MODULE_NAME_IS_ts
104 static void TSDemuxPSI ( input_thread_t *, data_packet_t *,
105 es_descriptor_t *, vlc_bool_t );
106 static void TSDecodePAT( input_thread_t *, es_descriptor_t *);
107 static void TSDecodePMT( input_thread_t *, es_descriptor_t *);
108 #define PSI_CALLBACK TSDemuxPSI
109 #elif defined MODULE_NAME_IS_ts_dvbpsi
110 static void TS_DVBPSI_DemuxPSI ( input_thread_t *, data_packet_t *,
111 es_descriptor_t *, vlc_bool_t );
112 static void TS_DVBPSI_HandlePAT ( input_thread_t *, dvbpsi_pat_t * );
113 static void TS_DVBPSI_HandlePMT ( input_thread_t *, dvbpsi_pmt_t * );
114 #define PSI_CALLBACK TS_DVBPSI_DemuxPSI
117 /*****************************************************************************
119 *****************************************************************************/
120 #define VLS_BACKWARDS_COMPAT_TEXT N_("Compatibility with pre-0.4 VLS")
121 #define VLS_BACKWARDS_COMPAT_LONGTEXT N_( \
122 "The protocol for transmitting A/52 audio streams changed between VLC " \
123 "0.3.x and 0.4. By default VLC assumes you have the latest VLS. In case " \
124 "you're using an old version, select this option.")
126 #define BUGGY_PSI_TEXT N_("Buggy PSI")
127 #define BUGGY_PSI_LONGTEXT N_( \
128 "If you have a stream whose PSI packets do not feature incremented " \
129 "continuity counters, select this option.")
132 #if defined MODULE_NAME_IS_ts
133 set_description( _("ISO 13818-1 MPEG Transport Stream input") );
134 set_capability( "demux", 160 );
135 add_shortcut( "ts" );
136 #elif defined MODULE_NAME_IS_ts_dvbpsi
137 set_description( _("ISO 13818-1 MPEG Transport Stream input (libdvbpsi)") );
138 set_capability( "demux", 170 );
139 add_shortcut( "ts_dvbpsi" );
142 add_bool( "vls-backwards-compat", 0, NULL,
143 VLS_BACKWARDS_COMPAT_TEXT, VLS_BACKWARDS_COMPAT_LONGTEXT, VLC_TRUE );
144 add_bool( "buggy-psi", 0, NULL, BUGGY_PSI_TEXT, BUGGY_PSI_LONGTEXT, VLC_TRUE );
145 set_callbacks( Activate, Deactivate );
148 /*****************************************************************************
149 * Activate: initialize TS structures
150 *****************************************************************************/
151 static int Activate( vlc_object_t * p_this )
153 input_thread_t * p_input = (input_thread_t *)p_this;
154 demux_sys_t * p_demux;
155 es_descriptor_t * p_pat_es;
156 es_ts_data_t * p_demux_data;
157 stream_ts_data_t * p_stream_data;
159 vlc_bool_t b_force = VLC_FALSE;
162 /* Set the demux function */
163 p_input->pf_demux = Demux;
164 p_input->pf_demux_control = demux_vaControlDefault;
166 /* Have a peep at the show */
167 if( input_Peek( p_input, &p_peek, TS_PACKET_SIZE ) < TS_PACKET_SIZE )
169 msg_Err( p_input, "cannot peek()" );
173 if( *p_input->psz_demux && ( !strncmp( p_input->psz_demux, "ts", 3 )
174 || !strncmp( p_input->psz_demux, "ts_dvbpsi", 10 ) ) )
177 /* In a TS_PACKET_SIZE buffer we should find a sync byte */
178 for( i_sync_pos = 0; i_sync_pos < TS_PACKET_SIZE &&
179 p_peek[i_sync_pos] != TS_SYNC_CODE; i_sync_pos++ );
181 if( i_sync_pos >= TS_PACKET_SIZE )
186 msg_Err( p_input, "this does not look like a TS stream, "
187 "continuing anyway" );
191 msg_Warn( p_input, "TS module discarded (no sync)" );
196 /* Now that we have the first TS_SYNC_CODE, check the following
197 * TS_SYNC_CODEs are where they are supposed to be (one byte sync code
198 * is not enough to ensure the sync). */
201 if( input_Peek( p_input, &p_peek, TS_PACKET_SIZE * TS_SYNC_CODES_MIN )
202 < TS_PACKET_SIZE * TS_SYNC_CODES_MIN )
204 msg_Err( p_input, "cannot peek()" );
208 for( ; i_sync_pos < TS_PACKET_SIZE * TS_SYNC_CODES_MIN;
209 i_sync_pos += TS_PACKET_SIZE )
211 if( p_peek[i_sync_pos] != TS_SYNC_CODE )
213 msg_Warn( p_input, "TS module discarded (lost sync)" );
219 /* Adapt the bufsize for our only use. */
220 if( p_input->i_mtu != 0 )
222 /* Have minimum granularity to avoid bottlenecks at the input level. */
223 p_input->i_bufsize = (p_input->i_mtu / TS_PACKET_SIZE) * TS_PACKET_SIZE;
226 p_demux = p_input->p_demux_data = malloc( sizeof(demux_sys_t ) );
227 if( p_demux == NULL )
232 p_input->p_private = (void*)&p_demux->mpeg;
233 p_demux->p_module = module_Need( p_input, "mpeg-system", NULL );
234 if( p_demux->p_module == NULL )
236 free( p_input->p_demux_data );
240 vlc_mutex_lock( &p_input->stream.stream_lock );
242 if( input_InitStream( p_input, sizeof( stream_ts_data_t ) ) == -1 )
244 module_Unneed( p_input, p_demux->p_module );
245 free( p_input->p_demux_data );
249 p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
250 p_stream_data->i_pat_version = PAT_UNINITIALIZED ;
251 p_stream_data->b_buggy_psi = config_GetInt( p_input, "buggy-psi" );
253 #ifdef MODULE_NAME_IS_ts_dvbpsi
254 p_stream_data->p_pat_handle = (dvbpsi_handle *)
255 dvbpsi_AttachPAT( (dvbpsi_pat_callback) &TS_DVBPSI_HandlePAT, p_input );
257 if( p_stream_data->p_pat_handle == NULL )
259 msg_Err( p_input, "could not create PAT decoder" );
260 module_Unneed( p_input, p_demux->p_module );
261 free( p_input->p_demux_data );
266 /* We'll have to catch the PAT in order to continue
267 * Then the input will catch the PMT and then the others ES
268 * The PAT es is indepedent of any program. */
269 p_pat_es = input_AddES( p_input, NULL, 0x00,
270 UNKNOWN_ES, NULL, sizeof( es_ts_data_t ) );
271 p_pat_es->i_fourcc = VLC_FOURCC( 'p', 'a', 't', ' ' );
272 p_demux_data = (es_ts_data_t *)p_pat_es->p_demux_data;
273 p_demux_data->b_psi = 1;
274 p_demux_data->i_psi_type = PSI_IS_PAT;
275 p_demux_data->p_psi_section = malloc(sizeof(psi_section_t));
276 p_demux_data->p_psi_section->b_is_complete = 1;
277 p_demux_data->i_continuity_counter = 0xFF;
279 vlc_mutex_unlock( &p_input->stream.stream_lock );
284 /*****************************************************************************
285 * Deactivate: deinitialize TS structures
286 *****************************************************************************/
287 static void Deactivate( vlc_object_t * p_this )
289 input_thread_t * p_input = (input_thread_t *)p_this;
291 module_Unneed( p_input, p_input->p_demux_data->p_module );
292 free( p_input->p_demux_data );
295 /*****************************************************************************
296 * Demux: reads and demuxes data packets
297 *****************************************************************************
298 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
300 *****************************************************************************/
301 static int Demux( input_thread_t * p_input )
303 demux_sys_t * p_demux = p_input->p_demux_data;
304 int i_read_once = (p_input->i_mtu ?
305 p_input->i_bufsize / TS_PACKET_SIZE :
309 for( i = 0; i < i_read_once; i++ )
311 data_packet_t * p_data;
314 i_result = p_demux->mpeg.pf_read_ts( p_input, &p_data );
321 p_demux->mpeg.pf_demux_ts( p_input, p_data,
322 (psi_callback_t) &PSI_CALLBACK );
329 #if defined MODULE_NAME_IS_ts
331 * PSI demultiplexing and decoding without libdvbpsi
334 /*****************************************************************************
335 * DemuxPSI : makes up complete PSI data
336 *****************************************************************************/
337 static void TSDemuxPSI( input_thread_t * p_input, data_packet_t * p_data,
338 es_descriptor_t * p_es, vlc_bool_t b_unit_start )
340 es_ts_data_t * p_demux_data;
342 p_demux_data = (es_ts_data_t *)p_es->p_demux_data;
344 #define p_psi (p_demux_data->p_psi_section)
345 #define p (p_data->p_payload_start)
349 /* unit_start set to 1 -> presence of a pointer field
350 * (see ISO/IEC 13818 (2.4.4.2) which should be set to 0x00 */
351 if( (uint8_t)p[0] != 0x00 )
354 "non-zero pointer field found, trying to continue" );
362 /* This is the begining of a new section */
364 if( ((uint8_t)(p[1]) & 0xc0) != 0x80 )
366 msg_Warn( p_input, "invalid PSI packet" );
371 p_psi->i_section_length = ((p[1] & 0xF) << 8) | p[2];
372 p_psi->b_section_complete = 0;
373 p_psi->i_read_in_section = 0;
374 p_psi->i_section_number = (uint8_t)p[6];
376 if( p_psi->b_is_complete || p_psi->i_section_number == 0 )
378 /* This is a new PSI packet */
379 p_psi->b_is_complete = 0;
381 p_psi->i_version_number = ( p[5] >> 1 ) & 0x1f;
382 p_psi->i_last_section_number = (uint8_t)p[7];
384 /* We'll write at the begining of the buffer */
385 p_psi->p_current = p_psi->buffer;
389 if( p_psi->b_section_complete )
391 /* New Section of an already started PSI */
392 p_psi->b_section_complete = 0;
394 if( p_psi->i_version_number != (( p[5] >> 1 ) & 0x1f) )
397 "PSI version differs inside same PAT" );
400 if( p_psi->i_section_number + 1 != (uint8_t)p[6] )
403 "PSI Section discontinuity, packet lost?" );
407 p_psi->i_section_number++;
411 msg_Warn( p_input, "got unexpected new PSI section" );
418 if( !p_psi->b_trash )
421 if( (p_data->p_payload_end - p) >=
422 ( p_psi->i_section_length - p_psi->i_read_in_section ) )
424 /* The end of the section is in this TS packet */
425 memcpy( p_psi->p_current, p,
426 (p_psi->i_section_length - p_psi->i_read_in_section) );
428 p_psi->b_section_complete = 1;
430 (p_psi->i_section_length - p_psi->i_read_in_section);
432 if( p_psi->i_section_number == p_psi->i_last_section_number )
434 /* This was the last section of PSI */
435 p_psi->b_is_complete = 1;
437 switch( p_demux_data->i_psi_type)
440 TSDecodePAT( p_input, p_es );
443 TSDecodePMT( p_input, p_es );
446 msg_Warn( p_input, "received unknown PSI in DemuxPSI" );
452 memcpy( p_psi->buffer, p, p_data->p_payload_end - p );
453 p_psi->i_read_in_section += p_data->p_payload_end - p;
455 p_psi->p_current += p_data->p_payload_end - p;
462 input_DeletePacket( p_input->p_method_data, p_data );
467 /*****************************************************************************
468 * DecodePAT : Decodes Programm association table and deal with it
469 *****************************************************************************/
470 static void TSDecodePAT( input_thread_t * p_input, es_descriptor_t * p_es )
472 stream_ts_data_t * p_stream_data;
473 es_ts_data_t * p_demux_data;
475 pgrm_descriptor_t * p_pgrm;
476 es_descriptor_t * p_current_es;
477 byte_t * p_current_data;
479 int i_section_length, i_program_id, i_pmt_pid;
480 int i_loop, i_current_section;
482 vlc_bool_t b_changed = 0;
484 p_demux_data = (es_ts_data_t *)p_es->p_demux_data;
485 p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
487 #define p_psi (p_demux_data->p_psi_section)
489 /* Not so fast, Mike ! If the PAT version has changed, we first check
490 * that its content has really changed before doing anything */
491 if( p_stream_data->i_pat_version != p_psi->i_version_number )
493 int i_programs = p_input->stream.i_pgrm_number;
495 p_current_data = p_psi->buffer;
499 i_section_length = ((uint32_t)(p_current_data[1] & 0xF) << 8) |
501 i_current_section = (uint8_t)p_current_data[6];
504 ( i_loop < (i_section_length - 9) / 4 ) && !b_changed;
507 i_program_id = ( (uint32_t)*(p_current_data + i_loop * 4 + 8) << 8 )
508 | *(p_current_data + i_loop * 4 + 9);
509 i_pmt_pid = ( ((uint32_t)*(p_current_data + i_loop * 4 + 10) & 0x1F)
511 | *(p_current_data + i_loop * 4 + 11);
515 if( (p_pgrm = input_FindProgram( p_input, i_program_id ))
516 && (p_current_es = input_FindES( p_input, i_pmt_pid ))
517 && p_current_es->p_pgrm == p_pgrm
518 && p_current_es->i_id == i_pmt_pid
519 && ((es_ts_data_t *)p_current_es->p_demux_data)->b_psi
520 && ((es_ts_data_t *)p_current_es->p_demux_data)
521 ->i_psi_type == PSI_IS_PMT )
532 p_current_data += 3 + i_section_length;
534 } while( ( i_current_section < p_psi->i_last_section_number )
537 /* If we didn't find the expected amount of programs, the PAT has
538 * changed. Otherwise, it only changed if b_changed is already != 0 */
539 b_changed = b_changed || i_programs;
544 /* PAT has changed. We are going to delete all programs and
545 * create new ones. We chose not to only change what was needed
546 * as a PAT change may mean the stream is radically changing and
547 * this is a secure method to avoid crashes */
548 es_ts_data_t * p_es_demux;
549 pgrm_ts_data_t * p_pgrm_demux;
551 p_current_data = p_psi->buffer;
553 /* Delete all programs */
554 while( p_input->stream.i_pgrm_number )
556 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
561 i_section_length = ((uint32_t)(p_current_data[1] & 0xF) << 8) |
563 i_current_section = (uint8_t)p_current_data[6];
565 for( i_loop = 0; i_loop < (i_section_length - 9) / 4 ; i_loop++ )
567 i_program_id = ( (uint32_t)*(p_current_data + i_loop * 4 + 8) << 8 )
568 | *(p_current_data + i_loop * 4 + 9);
569 i_pmt_pid = ( ((uint32_t)*(p_current_data + i_loop * 4 + 10) & 0x1F)
571 | *(p_current_data + i_loop * 4 + 11);
573 /* If program = 0, we're having info about NIT not PMT */
576 /* Add this program */
577 p_pgrm = input_AddProgram( p_input, i_program_id,
578 sizeof( pgrm_ts_data_t ) );
580 /* whatis the PID of the PMT of this program */
581 p_pgrm_demux = (pgrm_ts_data_t *)p_pgrm->p_demux_data;
582 p_pgrm_demux->i_pmt_version = PMT_UNINITIALIZED;
584 /* Add the PMT ES to this program */
585 p_current_es = input_AddES( p_input, p_pgrm,(uint16_t)i_pmt_pid,
586 UNKNOWN_ES, NULL, sizeof( es_ts_data_t) );
587 p_current_es->i_fourcc = VLC_FOURCC( 'p', 'm', 't', ' ' );
588 p_es_demux = (es_ts_data_t *)p_current_es->p_demux_data;
589 p_es_demux->b_psi = 1;
590 p_es_demux->i_psi_type = PSI_IS_PMT;
592 p_es_demux->p_psi_section =
593 malloc( sizeof( psi_section_t ) );
594 p_es_demux->p_psi_section->b_is_complete = 0;
595 p_es_demux->i_continuity_counter = 0xFF;
599 p_current_data += 3 + i_section_length;
601 } while( i_current_section < p_psi->i_last_section_number );
603 /* Go to the beginning of the next section */
604 p_stream_data->i_pat_version = p_psi->i_version_number;
611 /*****************************************************************************
612 * DecodePMT : decode a given Program Stream Map
613 * ***************************************************************************
614 * When the PMT changes, it may mean a deep change in the stream, and it is
615 * careful to delete the ES and add them again. If the PMT doesn't change,
616 * there no need to do anything.
617 *****************************************************************************/
618 static void TSDecodePMT( input_thread_t * p_input, es_descriptor_t * p_es )
621 pgrm_ts_data_t * p_pgrm_data;
622 es_ts_data_t * p_demux_data;
623 vlc_bool_t b_vls_compat = config_GetInt( p_input, "vls-backwards-compat" );
625 p_demux_data = (es_ts_data_t *)p_es->p_demux_data;
626 p_pgrm_data = (pgrm_ts_data_t *)p_es->p_pgrm->p_demux_data;
628 #define p_psi (p_demux_data->p_psi_section)
630 if( p_psi->i_version_number != p_pgrm_data->i_pmt_version )
632 es_descriptor_t * p_new_es;
633 es_ts_data_t * p_es_demux;
634 byte_t * p_current_data, * p_current_section;
635 int i_section_length,i_current_section;
636 int i_prog_info_length, i_loop;
637 int i_es_info_length, i_pid, i_stream_type;
639 p_current_section = p_psi->buffer;
640 p_current_data = p_psi->buffer;
642 p_pgrm_data->i_pcr_pid = ( ((uint32_t)*(p_current_section + 8) & 0x1F) << 8 ) |
643 *(p_current_section + 9);
646 /* Lock stream information */
647 vlc_mutex_lock( &p_input->stream.stream_lock );
649 /* Delete all ES in this program except the PSI. We start from the
650 * end because i_es_number gets decremented after each deletion. */
651 for( i_loop = p_es->p_pgrm->i_es_number ; i_loop ; )
654 p_es_demux = (es_ts_data_t *)
655 p_es->p_pgrm->pp_es[i_loop]->p_demux_data;
656 if ( ! p_es_demux->b_psi )
658 input_DelES( p_input, p_es->p_pgrm->pp_es[i_loop] );
662 /* Then add what we received in this PMT */
665 i_section_length = ( ((uint32_t)*(p_current_data + 1) & 0xF) << 8 ) |
666 *(p_current_data + 2);
667 i_current_section = (uint8_t)p_current_data[6];
668 i_prog_info_length = ( ((uint32_t)*(p_current_data + 10) & 0xF) << 8 ) |
669 *(p_current_data + 11);
671 /* For the moment we ignore program descriptors */
672 p_current_data += 12 + i_prog_info_length;
674 /* The end of the section, before the CRC is at
675 * p_current_section + i_section_length -1 */
676 while( p_current_data < p_current_section + i_section_length -1 )
678 i_stream_type = (int)p_current_data[0];
679 i_pid = ( ((uint32_t)*(p_current_data + 1) & 0x1F) << 8 ) |
680 *(p_current_data + 2);
681 i_es_info_length = ( ((uint32_t)*(p_current_data + 3) & 0xF) << 8 ) |
682 *(p_current_data + 4);
684 /* Tell the interface what kind of stream it is and select
685 * the required ones */
687 int i_fourcc, i_cat, i_stream_id;
689 switch( i_stream_type )
693 case MPEG2_MOTO_VIDEO_ES:
694 /* This isn't real, but we don't actually use
697 i_fourcc = VLC_FOURCC('m','p','g','v');
702 /* This isn't real, but we don't actually use
705 i_fourcc = VLC_FOURCC('m','p','g','a');
710 i_fourcc = VLC_FOURCC('a','5','2',' ');
712 i_fourcc = VLC_FOURCC('a','5','2','b');
717 i_fourcc = VLC_FOURCC('l','p','c','m');
723 i_fourcc = VLC_FOURCC('s','p','u',' ');
725 i_fourcc = VLC_FOURCC('s','p','u','b');
730 i_fourcc = VLC_FOURCC('s','d','d','s');
735 i_fourcc = VLC_FOURCC('d','t','s',' ');
739 /* 'b' stands for 'buggy' */
741 i_fourcc = VLC_FOURCC('a','5','2','b');
746 i_fourcc = VLC_FOURCC('l','p','c','b');
751 i_fourcc = VLC_FOURCC('s','p','u','b');
755 case AAC_ADTS_AUDIO_ES:
756 i_fourcc = VLC_FOURCC('m','p','4','a');
768 /* Add this ES to the program */
769 p_new_es = input_AddES( p_input, p_es->p_pgrm, (uint16_t)i_pid,
770 i_cat, NULL, sizeof( es_ts_data_t ) );
772 ((es_ts_data_t *)p_new_es->p_demux_data)->i_continuity_counter = 0xFF;
774 p_new_es->i_stream_id = i_stream_id;
775 p_new_es->i_fourcc = i_fourcc;
779 p_current_data += 5 + i_es_info_length;
782 /* Go to the beginning of the next section*/
783 p_current_data += 3 + i_section_length;
787 } while( i_current_section < p_psi->i_last_section_number );
789 p_pgrm_data->i_pmt_version = p_psi->i_version_number;
791 /* if no program is selected :*/
792 if( !p_input->stream.p_selected_program )
794 pgrm_descriptor_t * p_pgrm_to_select;
795 uint16_t i_id = (uint16_t)config_GetInt( p_input, "program" );
797 if( i_id != 0 ) /* if user specified a program */
799 p_pgrm_to_select = input_FindProgram( p_input, i_id );
801 if( p_pgrm_to_select && p_pgrm_to_select == p_es->p_pgrm )
802 p_input->pf_set_program( p_input, p_pgrm_to_select );
805 p_input->pf_set_program( p_input, p_es->p_pgrm );
808 /* if the pmt belongs to the currently selected program, we
809 * reselect it to update its ES */
810 else if( p_es->p_pgrm == p_input->stream.p_selected_program )
812 p_input->pf_set_program( p_input, p_es->p_pgrm );
815 /* inform interface that stream has changed */
816 p_input->stream.b_changed = 1;
818 vlc_mutex_unlock( &p_input->stream.stream_lock );
824 #elif defined MODULE_NAME_IS_ts_dvbpsi
826 * PSI Decoding using libdvbpsi
829 /*****************************************************************************
830 * DemuxPSI : send the PSI to the right libdvbpsi decoder
831 *****************************************************************************/
832 static void TS_DVBPSI_DemuxPSI( input_thread_t * p_input,
833 data_packet_t * p_data,
834 es_descriptor_t * p_es,
835 vlc_bool_t b_unit_start )
837 es_ts_data_t * p_es_demux_data;
838 pgrm_ts_data_t * p_pgrm_demux_data;
839 stream_ts_data_t * p_stream_demux_data;
841 p_es_demux_data = (es_ts_data_t *)p_es->p_demux_data;
842 p_stream_demux_data = (stream_ts_data_t *) p_input->stream.p_demux_data;
844 switch( p_es_demux_data->i_psi_type)
848 (dvbpsi_handle)p_stream_demux_data->p_pat_handle,
849 p_data->p_demux_start );
852 p_pgrm_demux_data = ( pgrm_ts_data_t * )p_es->p_pgrm->p_demux_data;
854 (dvbpsi_handle)p_pgrm_demux_data->p_pmt_handle,
855 p_data->p_demux_start );
858 msg_Warn( p_input, "received unknown PSI in DemuxPSI" );
861 input_DeletePacket( p_input->p_method_data, p_data );
863 /*****************************************************************************
864 * MP4 specific functions
865 *****************************************************************************/
866 static int MP4_DescriptorLength( int *pi_data, uint8_t **pp_data )
869 unsigned int i_len = 0;
875 i_len = ( i_len << 7 ) + ( i_b&0x7f );
881 static int MP4_GetByte( int *pi_data, uint8_t **pp_data )
896 static int MP4_GetWord( int *pi_data, uint8_t **pp_data )
899 i1 = MP4_GetByte( pi_data, pp_data );
900 i2 = MP4_GetByte( pi_data, pp_data );
901 return( ( i1 << 8 ) | i2 );
903 static int MP4_Get3Bytes( int *pi_data, uint8_t **pp_data )
906 i1 = MP4_GetByte( pi_data, pp_data );
907 i2 = MP4_GetByte( pi_data, pp_data );
908 i3 = MP4_GetByte( pi_data, pp_data );
909 return( ( i1 << 16 ) | ( i2 << 8) | i3 );
912 static uint32_t MP4_GetDWord( int *pi_data, uint8_t **pp_data )
915 i1 = MP4_GetWord( pi_data, pp_data );
916 i2 = MP4_GetWord( pi_data, pp_data );
917 return( ( i1 << 16 ) | i2 );
920 static char* MP4_GetURL( int *pi_data, uint8_t **pp_data )
925 i_url_len = MP4_GetByte( pi_data, pp_data );
926 url = malloc( i_url_len + 1 );
927 for( i = 0; i < i_url_len; i++ )
929 url[i] = MP4_GetByte( pi_data, pp_data );
931 url[i_url_len] = '\0';
935 static void MP4_IODParse( iod_descriptor_t *p_iod, int i_data, uint8_t *p_data )
943 fprintf( stderr, "\n************ IOD ************" );
944 for( i = 0; i < 255; i++ )
946 p_iod->es_descr[i].b_ok = 0;
955 p_iod->i_iod_label = MP4_GetByte( &i_data, &p_data );
956 fprintf( stderr, "\n* iod_label:%d", p_iod->i_iod_label );
957 fprintf( stderr, "\n* ===========" );
958 fprintf( stderr, "\n* tag:0x%x", p_data[0] );
960 if( MP4_GetByte( &i_data, &p_data ) != 0x02 )
962 fprintf( stderr, "\n ERR: tag != 0x02" );
966 i_iod_length = MP4_DescriptorLength( &i_data, &p_data );
967 fprintf( stderr, "\n* length:%d", i_iod_length );
968 if( i_iod_length > i_data )
970 i_iod_length = i_data;
973 p_iod->i_od_id = ( MP4_GetByte( &i_data, &p_data ) << 2 );
974 i_flags = MP4_GetByte( &i_data, &p_data );
975 p_iod->i_od_id |= i_flags >> 6;
976 b_url = ( i_flags >> 5 )&0x01;
978 fprintf( stderr, "\n* od_id:%d", p_iod->i_od_id );
979 fprintf( stderr, "\n* url flag:%d", b_url );
980 fprintf( stderr, "\n* includeInlineProfileLevel flag:%d", ( i_flags >> 4 )&0x01 );
984 p_iod->psz_url = MP4_GetURL( &i_data, &p_data );
985 fprintf( stderr, "\n* url string:%s", p_iod->psz_url );
986 fprintf( stderr, "\n*****************************\n" );
991 p_iod->psz_url = NULL;
994 p_iod->i_ODProfileLevelIndication = MP4_GetByte( &i_data, &p_data );
995 p_iod->i_sceneProfileLevelIndication = MP4_GetByte( &i_data, &p_data );
996 p_iod->i_audioProfileLevelIndication = MP4_GetByte( &i_data, &p_data );
997 p_iod->i_visualProfileLevelIndication = MP4_GetByte( &i_data, &p_data );
998 p_iod->i_graphicsProfileLevelIndication = MP4_GetByte( &i_data, &p_data );
1000 fprintf( stderr, "\n* ODProfileLevelIndication:%d", p_iod->i_ODProfileLevelIndication );
1001 fprintf( stderr, "\n* sceneProfileLevelIndication:%d", p_iod->i_sceneProfileLevelIndication );
1002 fprintf( stderr, "\n* audioProfileLevelIndication:%d", p_iod->i_audioProfileLevelIndication );
1003 fprintf( stderr, "\n* visualProfileLevelIndication:%d", p_iod->i_visualProfileLevelIndication );
1004 fprintf( stderr, "\n* graphicsProfileLevelIndication:%d", p_iod->i_graphicsProfileLevelIndication );
1007 while( i_data > 0 && i_es_index < 255)
1009 int i_tag, i_length;
1011 uint8_t *p_data_sav;
1013 i_tag = MP4_GetByte( &i_data, &p_data );
1014 i_length = MP4_DescriptorLength( &i_data, &p_data );
1016 i_data_sav = i_data;
1017 p_data_sav = p_data;
1025 #define es_descr p_iod->es_descr[i_es_index]
1026 int i_decoderConfigDescr_length;
1027 fprintf( stderr, "\n* - ES_Descriptor length:%d", i_length );
1030 es_descr.i_es_id = MP4_GetWord( &i_data, &p_data );
1031 i_flags = MP4_GetByte( &i_data, &p_data );
1032 es_descr.b_streamDependenceFlag = ( i_flags >> 7 )&0x01;
1033 b_url = ( i_flags >> 6 )&0x01;
1034 es_descr.b_OCRStreamFlag = ( i_flags >> 5 )&0x01;
1035 es_descr.i_streamPriority = i_flags & 0x1f;
1036 fprintf( stderr, "\n* * streamDependenceFlag:%d", es_descr.b_streamDependenceFlag );
1037 fprintf( stderr, "\n* * OCRStreamFlag:%d", es_descr.b_OCRStreamFlag );
1038 fprintf( stderr, "\n* * streamPriority:%d", es_descr.i_streamPriority );
1040 if( es_descr.b_streamDependenceFlag )
1042 es_descr.i_dependOn_es_id = MP4_GetWord( &i_data, &p_data );
1043 fprintf( stderr, "\n* * dependOn_es_id:%d", es_descr.i_dependOn_es_id );
1048 es_descr.psz_url = MP4_GetURL( &i_data, &p_data );
1049 fprintf( stderr, "\n* url string:%s", es_descr.psz_url );
1053 es_descr.psz_url = NULL;
1056 if( es_descr.b_OCRStreamFlag )
1058 es_descr.i_OCR_es_id = MP4_GetWord( &i_data, &p_data );
1059 fprintf( stderr, "\n* * OCR_es_id:%d", es_descr.i_OCR_es_id );
1062 if( MP4_GetByte( &i_data, &p_data ) != 0x04 )
1064 fprintf( stderr, "\n* ERR missing DecoderConfigDescr" );
1068 i_decoderConfigDescr_length = MP4_DescriptorLength( &i_data, &p_data );
1070 fprintf( stderr, "\n* - DecoderConfigDesc length:%d", i_decoderConfigDescr_length );
1071 #define dec_descr es_descr.dec_descr
1072 dec_descr.i_objectTypeIndication = MP4_GetByte( &i_data, &p_data );
1073 i_flags = MP4_GetByte( &i_data, &p_data );
1074 dec_descr.i_streamType = i_flags >> 2;
1075 dec_descr.b_upStream = ( i_flags >> 1 )&0x01;
1076 dec_descr.i_bufferSizeDB = MP4_Get3Bytes( &i_data, &p_data );
1077 dec_descr.i_maxBitrate = MP4_GetDWord( &i_data, &p_data );
1078 dec_descr.i_avgBitrate = MP4_GetDWord( &i_data, &p_data );
1079 fprintf( stderr, "\n* * objectTypeIndication:0x%x", dec_descr.i_objectTypeIndication );
1080 fprintf( stderr, "\n* * streamType:0x%x", dec_descr.i_streamType );
1081 fprintf( stderr, "\n* * upStream:%d", dec_descr.b_upStream );
1082 fprintf( stderr, "\n* * bufferSizeDB:%d", dec_descr.i_bufferSizeDB );
1083 fprintf( stderr, "\n* * maxBitrate:%d", dec_descr.i_maxBitrate );
1084 fprintf( stderr, "\n* * avgBitrate:%d", dec_descr.i_avgBitrate );
1085 if( i_decoderConfigDescr_length > 13 && MP4_GetByte( &i_data, &p_data ) == 0x05 )
1088 dec_descr.i_decoder_specific_info_len =
1089 MP4_DescriptorLength( &i_data, &p_data );
1090 if( dec_descr.i_decoder_specific_info_len > 0 )
1092 dec_descr.p_decoder_specific_info =
1093 malloc( dec_descr.i_decoder_specific_info_len );
1095 for( i = 0; i < dec_descr.i_decoder_specific_info_len; i++ )
1097 dec_descr.p_decoder_specific_info[i] = MP4_GetByte( &i_data, &p_data );
1102 dec_descr.i_decoder_specific_info_len = 0;
1103 dec_descr.p_decoder_specific_info = NULL;
1107 #define sl_descr es_descr.sl_descr
1109 int i_SLConfigDescr_length;
1112 if( MP4_GetByte( &i_data, &p_data ) != 0x06 )
1114 fprintf( stderr, "\n* ERR missing SLConfigDescr" );
1118 i_SLConfigDescr_length = MP4_DescriptorLength( &i_data, &p_data );
1120 fprintf( stderr, "\n* - SLConfigDescr length:%d", i_SLConfigDescr_length );
1121 i_predefined = MP4_GetByte( &i_data, &p_data );
1122 fprintf( stderr, "\n* * i_predefined:0x%x", i_predefined );
1123 switch( i_predefined )
1127 sl_descr.b_useAccessUnitStartFlag = 0;
1128 sl_descr.b_useAccessUnitEndFlag = 0;
1129 sl_descr.b_useRandomAccessPointFlag = 0;
1130 //sl_descr.b_useRandomAccessUnitsOnlyFlag = 0;
1131 sl_descr.b_usePaddingFlag = 0;
1132 sl_descr.b_useTimeStampsFlags = 0;
1133 sl_descr.b_useIdleFlag = 0;
1134 sl_descr.b_durationFlag = 0; // FIXME FIXME
1135 sl_descr.i_timeStampResolution = 1000;
1136 sl_descr.i_OCRResolution = 0; // FIXME FIXME
1137 sl_descr.i_timeStampLength = 32;
1138 sl_descr.i_OCRLength = 0; // FIXME FIXME
1139 sl_descr.i_AU_Length = 0;
1140 sl_descr.i_instantBitrateLength= 0; // FIXME FIXME
1141 sl_descr.i_degradationPriorityLength= 0;
1142 sl_descr.i_AU_seqNumLength = 0;
1143 sl_descr.i_packetSeqNumLength = 0;
1144 if( sl_descr.b_durationFlag )
1146 sl_descr.i_timeScale = 0; // FIXME FIXME
1147 sl_descr.i_accessUnitDuration = 0; // FIXME FIXME
1148 sl_descr.i_compositionUnitDuration= 0; // FIXME FIXME
1150 if( !sl_descr.b_useTimeStampsFlags )
1152 sl_descr.i_startDecodingTimeStamp = 0; // FIXME FIXME
1153 sl_descr.i_startCompositionTimeStamp= 0; // FIXME FIXME
1158 fprintf( stderr, "\n* ERR unsupported SLConfigDescr predefined" );
1167 fprintf( stderr, "\n* - OD tag:0x%x length:%d (Unsupported)", i_tag, i_length );
1171 p_data = p_data_sav + i_length;
1172 i_data = i_data_sav - i_length;
1177 fprintf( stderr, "\n*****************************\n" );
1180 static void MP4_IODClean( iod_descriptor_t *p_iod )
1184 if( p_iod->psz_url )
1186 free( p_iod->psz_url );
1187 p_iod->psz_url = NULL;
1191 for( i = 0; i < 255; i++ )
1193 #define es_descr p_iod->es_descr[i]
1196 if( es_descr.psz_url )
1198 free( es_descr.psz_url );
1199 es_descr.psz_url = NULL;
1203 if( es_descr.dec_descr.p_decoder_specific_info != NULL )
1205 free( es_descr.dec_descr.p_decoder_specific_info );
1206 es_descr.dec_descr.p_decoder_specific_info = NULL;
1207 es_descr.dec_descr.i_decoder_specific_info_len = 0;
1216 /*****************************************************************************
1217 * HandlePAT: will treat a PAT returned by dvbpsi
1218 *****************************************************************************/
1219 static void TS_DVBPSI_HandlePAT( input_thread_t * p_input,
1220 dvbpsi_pat_t * p_new_pat )
1222 dvbpsi_pat_program_t * p_pgrm;
1223 pgrm_descriptor_t * p_new_pgrm;
1224 pgrm_ts_data_t * p_pgrm_demux;
1225 es_descriptor_t * p_current_es;
1226 es_ts_data_t * p_es_demux;
1227 stream_ts_data_t * p_stream_data;
1229 vlc_mutex_lock( &p_input->stream.stream_lock );
1231 p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
1233 if( ( p_new_pat->b_current_next &&
1234 ( p_new_pat->i_version != p_stream_data->i_pat_version ) ) ||
1235 p_stream_data->i_pat_version == PAT_UNINITIALIZED )
1237 msg_Dbg( p_input, "processing PAT version %d", p_new_pat->i_version );
1239 /* Delete all programs */
1240 while( p_input->stream.i_pgrm_number )
1242 pgrm_ts_data_t *p_pgrm_demux_old =
1243 (pgrm_ts_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
1245 if( p_pgrm_demux_old->b_mpeg4 )
1247 MP4_IODClean( &p_pgrm_demux_old->iod );
1250 /* Delete old PMT decoder */
1251 if( p_pgrm_demux_old->p_pmt_handle )
1252 dvbpsi_DetachPMT( p_pgrm_demux_old->p_pmt_handle );
1254 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
1257 /* treat the new programs list */
1258 p_pgrm = p_new_pat->p_first_program;
1262 msg_Dbg( p_input, "new program: %d", p_pgrm->i_number );
1264 /* If program = 0, we're having info about NIT not PMT */
1265 if( p_pgrm->i_number )
1267 /* Add this program */
1268 p_new_pgrm = input_AddProgram( p_input, p_pgrm->i_number,
1269 sizeof( pgrm_ts_data_t ) );
1271 p_pgrm_demux = (pgrm_ts_data_t *)p_new_pgrm->p_demux_data;
1272 p_pgrm_demux->i_pmt_version = PMT_UNINITIALIZED;
1274 /* Add the PMT ES to this program */
1275 p_current_es = input_AddES( p_input, p_new_pgrm,
1276 (uint16_t)p_pgrm->i_pid, UNKNOWN_ES,
1277 NULL, sizeof(es_ts_data_t) );
1278 p_current_es->i_fourcc = VLC_FOURCC( 'p', 'm', 't', ' ' );
1279 p_es_demux = (es_ts_data_t *)p_current_es->p_demux_data;
1280 p_es_demux->b_psi = 1;
1281 p_es_demux->i_psi_type = PSI_IS_PMT;
1282 p_es_demux->p_psi_section = NULL;
1283 p_es_demux->i_continuity_counter = 0xFF;
1285 /* Create a PMT decoder */
1286 p_pgrm_demux->p_pmt_handle = (dvbpsi_handle *)
1287 dvbpsi_AttachPMT( p_pgrm->i_number,
1288 (dvbpsi_pmt_callback) &TS_DVBPSI_HandlePMT,
1291 if( p_pgrm_demux->p_pmt_handle == NULL )
1293 msg_Err( p_input, "could not create PMT decoder" );
1294 p_input->b_error = 1;
1299 p_pgrm = p_pgrm->p_next;
1302 p_stream_data->i_pat_version = p_new_pat->i_version;
1304 vlc_mutex_unlock( &p_input->stream.stream_lock );
1308 /*****************************************************************************
1309 * HandlePMT: will treat a PMT returned by dvbpsi
1310 *****************************************************************************/
1311 static void TS_DVBPSI_HandlePMT( input_thread_t * p_input,
1312 dvbpsi_pmt_t * p_new_pmt )
1314 dvbpsi_pmt_es_t * p_es;
1315 pgrm_descriptor_t * p_pgrm;
1316 es_descriptor_t * p_new_es;
1317 pgrm_ts_data_t * p_pgrm_demux;
1318 vlc_bool_t b_vls_compat = config_GetInt( p_input, "vls-backwards-compat" );
1320 vlc_mutex_lock( &p_input->stream.stream_lock );
1322 p_pgrm = input_FindProgram( p_input, p_new_pmt->i_program_number );
1324 if( p_pgrm == NULL )
1326 msg_Warn( p_input, "PMT of unreferenced program found" );
1330 p_pgrm_demux = (pgrm_ts_data_t *)p_pgrm->p_demux_data;
1331 p_pgrm_demux->i_pcr_pid = p_new_pmt->i_pcr_pid;
1333 if( ( p_new_pmt->b_current_next &&
1334 ( p_new_pmt->i_version != p_pgrm_demux->i_pmt_version ) ) ||
1335 p_pgrm_demux->i_pmt_version == PMT_UNINITIALIZED )
1337 dvbpsi_descriptor_t *p_dr = p_new_pmt->p_first_descriptor;
1340 msg_Dbg( p_input, "processing PMT for program %d version %d",
1341 p_new_pmt->i_program_number, p_new_pmt->i_version );
1343 /* Delete all ES in this program except the PSI. We start from the
1344 * end because i_es_number gets decremented after each deletion. */
1345 for( i_loop = p_pgrm->i_es_number ; i_loop > 0 ; )
1347 es_ts_data_t * p_es_demux;
1350 p_es_demux = (es_ts_data_t *)
1351 p_pgrm->pp_es[i_loop]->p_demux_data;
1352 if ( !p_es_demux->b_psi )
1354 input_DelES( p_input, p_pgrm->pp_es[i_loop] );
1359 while( p_dr && ( p_dr->i_tag != 0x1d ) )
1360 p_dr = p_dr->p_next;
1363 msg_Warn( p_input, "found IOD descriptor" );
1364 MP4_IODParse( &p_pgrm_demux->iod, p_dr->i_length, p_dr->p_data );
1367 p_es = p_new_pmt->p_first_es;
1370 vlc_fourcc_t i_fourcc;
1371 int i_size, i_cat, i_stream_id = 0;
1372 es_ts_data_t demux_data;
1373 BITMAPINFOHEADER *p_bih = NULL;
1374 WAVEFORMATEX *p_wf = NULL;
1377 memset( &demux_data, 0, sizeof(es_ts_data_t) );
1379 msg_Dbg( p_input, "new PID 0x%x stream type 0x%x",
1380 p_es->i_pid, p_es->i_type );
1382 switch( p_es->i_type )
1384 case MPEG1_VIDEO_ES:
1385 case MPEG2_VIDEO_ES:
1386 case MPEG2_MOTO_VIDEO_ES:
1387 i_fourcc = VLC_FOURCC('m','p','g','v');
1390 case MPEG1_AUDIO_ES:
1391 case MPEG2_AUDIO_ES:
1392 i_fourcc = VLC_FOURCC('m','p','g','a');
1396 if ( !b_vls_compat )
1397 i_fourcc = VLC_FOURCC('a','5','2',' ');
1399 i_fourcc = VLC_FOURCC('a','5','2','b');
1404 if ( !b_vls_compat )
1405 i_fourcc = VLC_FOURCC('s','p','u',' ');
1407 i_fourcc = VLC_FOURCC('s','p','u','b');
1412 i_fourcc = VLC_FOURCC('l','p','c','m');
1417 i_fourcc = VLC_FOURCC('s','d','d','s');
1422 i_fourcc = VLC_FOURCC('d','t','s',' ');
1427 i_fourcc = VLC_FOURCC('a','5','2','b');
1432 i_fourcc = VLC_FOURCC('s','p','u','b');
1436 case LPCMB_AUDIO_ES:
1437 i_fourcc = VLC_FOURCC('l','p','c','b');
1441 case MPEG4_VIDEO_ES:
1442 i_fourcc = VLC_FOURCC('m','p','4','v');
1446 case MPEG4_AUDIO_ES:
1447 case AAC_ADTS_AUDIO_ES:
1448 i_fourcc = VLC_FOURCC('m','p','4','a');
1452 case MSCODEC_VIDEO_ES:
1453 i_fourcc = VLC_FOURCC(0,0,0,0); /* fixed later */
1457 case PES_PRIVATE_ES:
1458 /* We need to check a descriptor to find the real codec */
1459 i_fourcc = VLC_FOURCC(0,0,0,0); /* fixed later */
1469 if( p_es->i_type == MPEG4_VIDEO_ES ||
1470 p_es->i_type == MPEG4_AUDIO_ES )
1472 /* mpeg4 stream, search sl_descriptor */
1473 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
1475 while( p_dr && ( p_dr->i_tag != 0x1f ) ) p_dr = p_dr->p_next;
1477 if( p_dr && p_dr->i_length == 2 )
1479 int i_es_descr_index;
1481 demux_data.i_es_id =
1482 ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
1483 demux_data.p_es_descr = NULL;
1485 msg_Warn( p_input, "found SL_descriptor" );
1486 for( i_es_descr_index = 0; i_es_descr_index < 255;
1487 i_es_descr_index++ )
1489 if( p_pgrm_demux->iod.es_descr[i_es_descr_index].b_ok &&
1490 p_pgrm_demux->iod.es_descr[i_es_descr_index].i_es_id == demux_data.i_es_id )
1492 demux_data.p_es_descr =
1493 &p_pgrm_demux->iod.es_descr[i_es_descr_index];
1499 if( demux_data.p_es_descr != NULL )
1501 #define DESCR demux_data.p_es_descr->dec_descr
1502 demux_data.b_mpeg4 = 1;
1505 switch( DESCR.i_streamType )
1507 case 0x04: /* VisualStream */
1509 switch( DESCR.i_objectTypeIndication )
1512 i_fourcc = VLC_FOURCC('m','p','4','v'); // mpeg4
1520 i_fourcc = VLC_FOURCC( 'm','p','g','v' ); // mpeg2
1523 i_fourcc = VLC_FOURCC( 'm','p','g','v' ); // mpeg1
1526 i_fourcc = VLC_FOURCC( 'j','p','e','g' ); // mpeg1
1533 case 0x05: /* AudioStream */
1535 switch( DESCR.i_objectTypeIndication )
1538 i_fourcc = VLC_FOURCC('m','p','4','a'); // mpeg4
1543 i_fourcc = VLC_FOURCC('m','p','4','a');// mpeg2 aac
1546 i_fourcc = VLC_FOURCC('m','p','g','a'); // mpeg2
1549 i_fourcc = VLC_FOURCC('m','p','g','a'); // mpeg1
1565 i_size = sizeof( BITMAPINFOHEADER ) +
1566 DESCR.i_decoder_specific_info_len;
1567 p_bih = malloc( i_size );
1568 p_bih->biSize = i_size;
1570 p_bih->biHeight = 0;
1571 p_bih->biPlanes = 1;
1572 p_bih->biBitCount = 0;
1573 p_bih->biCompression = 0;
1574 p_bih->biSizeImage = 0;
1575 p_bih->biXPelsPerMeter = 0;
1576 p_bih->biYPelsPerMeter = 0;
1577 p_bih->biClrUsed = 0;
1578 p_bih->biClrImportant = 0;
1580 DESCR.p_decoder_specific_info,
1581 DESCR.i_decoder_specific_info_len );
1584 i_size = sizeof( WAVEFORMATEX ) +
1585 DESCR.i_decoder_specific_info_len;
1586 p_wf = malloc( i_size );
1587 p_wf->wFormatTag = 0xffff;
1588 p_wf->nChannels = 0;
1589 p_wf->nSamplesPerSec = 0;
1590 p_wf->nAvgBytesPerSec = 0;
1591 p_wf->nBlockAlign = 1;
1592 p_wf->wBitsPerSample = 0;
1593 p_wf->cbSize = DESCR.i_decoder_specific_info_len;
1595 DESCR.p_decoder_specific_info,
1596 DESCR.i_decoder_specific_info_len );
1605 "mpeg4 stream without (valid) sl_descriptor" );
1606 demux_data.b_mpeg4 = 0;
1610 else if( p_es->i_type == MSCODEC_VIDEO_ES )
1612 /* crapy ms codec stream, search private descriptor */
1613 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
1615 while( p_dr && ( p_dr->i_tag != 0xa0 ) ) p_dr = p_dr->p_next;
1617 if( p_dr && p_dr->i_length >= 8 )
1620 i_fourcc = VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
1621 p_dr->p_data[2], p_dr->p_data[3] );
1623 i_bih_size = (p_dr->p_data[8] << 8) | p_dr->p_data[9];
1624 i_size = sizeof( BITMAPINFOHEADER ) + i_bih_size;
1626 p_bih = malloc( i_size );
1627 p_bih->biSize = i_size;
1628 p_bih->biWidth = ( p_dr->p_data[4] << 8 )|p_dr->p_data[5];
1629 p_bih->biHeight = ( p_dr->p_data[6] << 8 )|p_dr->p_data[7];
1630 p_bih->biPlanes = 1;
1631 p_bih->biBitCount = 0;
1632 p_bih->biCompression = 0;
1633 p_bih->biSizeImage = 0;
1634 p_bih->biXPelsPerMeter = 0;
1635 p_bih->biYPelsPerMeter = 0;
1636 p_bih->biClrUsed = 0;
1637 p_bih->biClrImportant = 0;
1638 memcpy( &p_bih[1], &p_dr->p_data[10], i_bih_size );
1642 msg_Warn( p_input, "private ms-codec stream without bih "
1643 "private sl_descriptor" );
1648 else if( p_es->i_type == PES_PRIVATE_ES )
1650 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
1651 /* We have to find a descriptor giving the right codec */
1653 for(p_dr = p_es->p_first_descriptor; p_dr; p_dr = p_dr->p_next)
1655 if( p_dr->i_tag == 0x6a )
1658 i_fourcc = VLC_FOURCC( 'a', '5', '2', ' ' );
1661 #ifdef _DVBPSI_DR_59_H_
1662 else if( p_dr->i_tag == 0x59 )
1665 es_descriptor_t * p_dvbsub_es;
1666 es_ts_data_t dvbsub_demux_data;
1667 dvb_spuinfo_t* p_info;
1668 dvbpsi_subtitling_dr_t* sub;
1669 demux_data.b_dvbsub = 1;
1670 demux_data.i_dvbsub_es_count = 0;
1673 i_fourcc = VLC_FOURCC( 'd', 'v', 'b', 's' );
1676 sub = dvbpsi_DecodeSubtitlingDr( p_dr );
1677 for( n = 0; n < sub->i_subtitles_number; n++)
1679 /* As each subtitle ES contains n languages,
1680 * We are going to create n fake ES for the n
1682 local_iso639_getlang(
1683 sub->p_subtitle[n].i_iso6392_language_code,
1685 p_dvbsub_es = input_AddES( p_input,
1690 sizeof(es_ts_data_t) );
1691 if( p_dvbsub_es == NULL )
1693 msg_Err( p_input, "could not add ES %d",
1695 p_input->b_error = 1;
1699 p_dvbsub_es->i_fourcc = i_fourcc;
1700 p_dvbsub_es->i_stream_id = i_stream_id;
1701 p_info = malloc(sizeof(dvb_spuinfo_t));
1703 sub->p_subtitle[n].i_composition_page_id;
1704 p_dvbsub_es->p_spuinfo = (void*) p_info;
1705 memcpy( p_dvbsub_es->p_demux_data,
1707 sizeof(es_ts_data_t) );
1708 ((es_ts_data_t *)p_dvbsub_es->p_demux_data)
1709 ->i_continuity_counter = 0xFF;
1710 // Finaly we add this stream to the index
1711 demux_data.p_dvbsub_es[
1712 demux_data.i_dvbsub_es_count++] = p_dvbsub_es;
1717 #endif /* _DVBPSI_DR_59_H_ */
1719 if( i_fourcc == VLC_FOURCC(0,0,0,0) )
1722 "unknown codec/type for Private PES stream" );
1726 if( i_cat == AUDIO_ES || i_cat == SPU_ES )
1728 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
1729 while( p_dr && ( p_dr->i_tag != 0x0a ) ) p_dr = p_dr->p_next;
1733 dvbpsi_iso639_dr_t *p_decoded =
1734 dvbpsi_DecodeISO639Dr( p_dr );
1735 local_iso639_getlang(p_decoded->i_iso_639_code, psz_desc);
1737 if( p_decoded->i_code_count > 0 )
1739 const iso639_lang_t * p_iso;
1740 p_iso = GetLang_2T((char*)p_decoded->i_iso_639_code);
1742 if( p_iso && strcmp(p_iso->psz_native_name,"Unknown"))
1744 if( p_iso->psz_native_name[0] )
1746 p_iso->psz_native_name, 20 );
1749 p_iso->psz_eng_name, 20 );
1754 (char*)p_decoded->i_iso_639_code);
1757 if( p_iso->psz_native_name[0] )
1759 p_iso->psz_native_name, 20 );
1762 p_iso->psz_eng_name, 20 );
1766 strncpy( psz_desc, p_decoded->i_iso_639_code, 3 );
1772 switch( p_es->i_type )
1774 case MPEG1_AUDIO_ES:
1775 case MPEG2_AUDIO_ES:
1776 strcat( psz_desc, " (mpeg)" );
1779 case LPCMB_AUDIO_ES:
1780 strcat( psz_desc, " (lpcm)" );
1784 strcat( psz_desc, " (A52)" );
1786 case MPEG4_AUDIO_ES:
1787 case AAC_ADTS_AUDIO_ES:
1788 strcat( psz_desc, " (aac)" );
1794 p_new_es = input_AddES( p_input, p_pgrm, (uint16_t)p_es->i_pid,
1795 i_cat, psz_desc, sizeof( es_ts_data_t ) );
1796 if( p_new_es == NULL )
1798 msg_Err( p_input, "could not add ES %d", p_es->i_pid );
1799 p_input->b_error = 1;
1802 p_new_es->i_fourcc = i_fourcc;
1803 p_new_es->i_stream_id = i_stream_id;
1804 p_new_es->p_bitmapinfoheader = (void*)p_bih;
1805 p_new_es->p_waveformatex = (void*)p_wf;
1806 memcpy( p_new_es->p_demux_data, &demux_data,
1807 sizeof(es_ts_data_t) );
1809 ((es_ts_data_t *)p_new_es->p_demux_data)->i_continuity_counter =
1812 p_es = p_es->p_next;
1815 /* if no program is selected :*/
1816 if( !p_input->stream.p_selected_program )
1818 pgrm_descriptor_t * p_pgrm_to_select;
1819 uint16_t i_id = (uint16_t)config_GetInt( p_input, "program" );
1821 if( i_id != 0 ) /* if user specified a program */
1823 p_pgrm_to_select = input_FindProgram( p_input, i_id );
1825 if( p_pgrm_to_select && p_pgrm_to_select == p_pgrm )
1826 p_input->pf_set_program( p_input, p_pgrm_to_select );
1829 p_input->pf_set_program( p_input, p_pgrm );
1831 /* if the pmt belongs to the currently selected program, we
1832 * reselect it to update its ES */
1833 else if( p_pgrm == p_input->stream.p_selected_program )
1835 p_input->pf_set_program( p_input, p_pgrm );
1838 p_pgrm_demux->i_pmt_version = p_new_pmt->i_version;
1839 p_input->stream.b_changed = 1;
1841 vlc_mutex_unlock( &p_input->stream.stream_lock );