1 /*****************************************************************************
2 * demuxstream.c: Read an MPEG stream from the satellite and stream it
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: demuxstream.c,v 1.3 2004/03/03 20:39:51 gbazin Exp $
7 * Authors: Henri Fallon <henri@via.ecp.fr>
8 * Johan Bilien <jobi@via.ecp.fr>
9 * Laurent Aimar <fenrir@via.ecp.fr>
10 * Christophe Massiot <massiot@via.ecp.fr>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
29 * The goal of this module is to stream packets coming from a DVB interface
30 * as fast as possible onto the network, like VLS does. This is a copy/paste
31 * from modules/demux/mpeg/ts.c, because I need to parse the PMT to set
32 * the demux filters in the satellite module. So I need the full TS module.
33 * It would probably have been better not to copy/paste and use some kind
34 * of hooks, but it is too complicated for me. Use this module that way :
36 * vlc satellite/demuxstream: --program 4242 --demuxstream-mrl \
37 * udp:192.168.255.1:1234
39 * Also note that this module doesn't rebuild the PAT/PMT, and doesn't
40 * filter the PIDs : it just takes whatever comes from the DVB interface
41 * and sends it to the network. The client VLC should have no problem to
42 * deal with it, but other implementations will probably die.
44 * This file is part of the Dreambox port of VLC.
48 /*****************************************************************************
50 *****************************************************************************/
55 #include <vlc/input.h>
59 #if defined MODULE_NAME_IS_demuxstream
60 # ifdef HAVE_DVBPSI_DR_H
61 # include <dvbpsi/dvbpsi.h>
62 # include <dvbpsi/descriptor.h>
63 # include <dvbpsi/pat.h>
64 # include <dvbpsi/pmt.h>
65 # include <dvbpsi/dr.h>
68 # include "descriptor.h"
69 # include "tables/pat.h"
70 # include "tables/pmt.h"
71 # include "descriptors/dr.h"
75 #include "mpeg/system.h"
79 /*****************************************************************************
81 *****************************************************************************/
82 #define TS_READ_ONCE 7
83 #define DEFAULT_PORT 1234
85 /*****************************************************************************
87 *****************************************************************************/
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_demuxstream_old
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_demuxstream
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.")
131 #define MRL_TEXT N_("Output MRL")
134 #if defined MODULE_NAME_IS_demuxstream_old
135 set_description( _("ISO 13818-1 MPEG Transport Stream demuxstream") );
136 set_capability( "demux", 0 );
137 add_shortcut( "demuxstream_old" );
138 #elif defined MODULE_NAME_IS_demuxstream
139 set_description( _("ISO 13818-1 MPEG Transport Stream demuxstream (libdvbpsi)") );
140 set_capability( "demux", 0 );
141 add_shortcut( "demuxstream" );
143 add_bool( "vls-backwards-compat", 0, NULL,
144 VLS_BACKWARDS_COMPAT_TEXT, VLS_BACKWARDS_COMPAT_LONGTEXT, VLC_TRUE );
145 add_bool( "buggy-psi", 0, NULL, BUGGY_PSI_TEXT, BUGGY_PSI_LONGTEXT, VLC_TRUE );
146 add_string( "demuxstream-mrl", "", NULL, MRL_TEXT, NULL, VLC_FALSE );
147 set_callbacks( Activate, Deactivate );
150 /*****************************************************************************
151 * Activate: initialize TS structures
152 *****************************************************************************/
153 static int Activate( vlc_object_t * p_this )
155 input_thread_t * p_input = (input_thread_t *)p_this;
156 demux_sys_t * p_demux;
157 es_descriptor_t * p_pat_es;
158 es_ts_data_t * p_demux_data;
159 stream_ts_data_t * p_stream_data;
161 char * psz_mrl = config_GetPsz( p_input, "demuxstream-mrl" );
162 char * psz_parser = psz_mrl;
166 network_socket_t socket_desc;
168 /* Open the output socket */
169 if ( psz_mrl == NULL || !*psz_mrl )
171 msg_Err( p_input, "invalid demuxstream-mrl" );
175 if ( strncmp( psz_mrl, "udp:", 4 ) )
177 msg_Err( p_input, "demuxstream-mrl should start with udp: (%s)",
183 while ( *psz_parser == '/' )
188 psz_dst_addr = psz_parser;
191 if ( *psz_parser == '[' )
193 while( *psz_parser && *psz_parser != ']' )
198 while( *psz_parser && *psz_parser != ':' )
202 if( *psz_parser == ':' )
206 i_dst_port = atoi( psz_parser );
208 if( i_dst_port <= 0 )
210 i_dst_port = DEFAULT_PORT;
213 socket_desc.i_type = NETWORK_UDP;
214 socket_desc.psz_server_addr = psz_dst_addr;
215 socket_desc.i_server_port = i_dst_port;
216 socket_desc.psz_bind_addr = "";
217 socket_desc.i_bind_port = 0;
218 socket_desc.i_ttl = 0;
219 p_input->p_private = (void*)&socket_desc;
220 if( !( p_network = module_Need( p_input, "network", NULL, 0 ) ) )
222 msg_Err( p_input, "failed to open a connection (udp)" );
223 return( VLC_EGENERIC );
225 module_Unneed( p_input, p_network );
227 /* Set the demux function */
228 p_input->pf_demux = Demux;
229 p_input->pf_demux_control = demux_vaControlDefault;
232 /* XXX Unused already done by src/input.c */
233 /* Initialize access plug-in structures. */
234 if( p_input->i_mtu == 0 )
237 msg_Dbg( p_input, "using default mtu (%d) with bufsize (%d)\n",
238 p_input->i_mtu, INPUT_DEFAULT_BUFSIZE );
239 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
243 /* Have a peep at the show. */
244 if( input_Peek( p_input, &p_peek, 1 ) < 1 )
246 msg_Err( p_input, "cannot peek()" );
250 if( *p_peek != TS_SYNC_CODE )
252 msg_Err( p_input, "this does not look like a TS stream, continuing" );
255 /* Adapt the bufsize for our only use. */
256 if( p_input->i_mtu != 0 )
258 /* Have minimum granularity to avoid bottlenecks at the input level. */
259 p_input->i_bufsize = (p_input->i_mtu / TS_PACKET_SIZE) * TS_PACKET_SIZE;
262 p_demux = p_input->p_demux_data = malloc( sizeof(demux_sys_t ) );
263 if( p_demux == NULL )
268 p_demux->i_handle = socket_desc.i_handle;
270 p_input->p_private = (void*)&p_demux->mpeg;
271 p_demux->p_module = module_Need( p_input, "mpeg-system", NULL, 0 );
272 if( p_demux->p_module == NULL )
274 free( p_input->p_demux_data );
278 vlc_mutex_lock( &p_input->stream.stream_lock );
280 if( input_InitStream( p_input, sizeof( stream_ts_data_t ) ) == -1 )
282 module_Unneed( p_input, p_demux->p_module );
283 free( p_input->p_demux_data );
287 p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
288 p_stream_data->i_pat_version = PAT_UNINITIALIZED ;
289 p_stream_data->b_buggy_psi = config_GetInt( p_input, "buggy-psi" );
291 #ifdef MODULE_NAME_IS_demuxstream
292 p_stream_data->p_pat_handle = (dvbpsi_handle *)
293 dvbpsi_AttachPAT( (dvbpsi_pat_callback) &TS_DVBPSI_HandlePAT, p_input );
295 if( p_stream_data->p_pat_handle == NULL )
297 msg_Err( p_input, "could not create PAT decoder" );
298 module_Unneed( p_input, p_demux->p_module );
299 free( p_input->p_demux_data );
304 /* We'll have to catch the PAT in order to continue
305 * Then the input will catch the PMT and then the others ES
306 * The PAT es is indepedent of any program. */
307 p_pat_es = input_AddES( p_input, NULL, 0x00,
308 UNKNOWN_ES, NULL, sizeof( es_ts_data_t ) );
309 p_pat_es->i_fourcc = VLC_FOURCC( 'p', 'a', 't', ' ' );
310 p_demux_data = (es_ts_data_t *)p_pat_es->p_demux_data;
311 p_demux_data->b_psi = 1;
312 p_demux_data->i_psi_type = PSI_IS_PAT;
313 p_demux_data->p_psi_section = malloc(sizeof(psi_section_t));
314 p_demux_data->p_psi_section->b_is_complete = 1;
315 p_demux_data->i_continuity_counter = 0xFF;
317 vlc_mutex_unlock( &p_input->stream.stream_lock );
322 /*****************************************************************************
323 * Deactivate: deinitialize TS structures
324 *****************************************************************************/
325 static void Deactivate( vlc_object_t * p_this )
327 input_thread_t * p_input = (input_thread_t *)p_this;
329 module_Unneed( p_input, p_input->p_demux_data->p_module );
330 close( p_input->p_demux_data->i_handle );
331 free( p_input->p_demux_data );
334 /*****************************************************************************
335 * Demux: reads and demuxes data packets
336 *****************************************************************************
337 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
339 *****************************************************************************/
340 static int Demux( input_thread_t * p_input )
342 demux_sys_t * p_demux = p_input->p_demux_data;
343 int i_read_once = TS_READ_ONCE;
345 data_packet_t * p_data[i_read_once];
346 struct iovec vector[i_read_once];
348 for( i = 0; i < i_read_once; i++ )
352 i_result = p_demux->mpeg.pf_read_ts( p_input, &p_data[i] );
359 vector[i].iov_base = p_data[i]->p_demux_start;
360 vector[i].iov_len = TS_PACKET_SIZE;
363 /* Send the buffer immediately. */
364 writev( p_demux->i_handle, vector, i );
366 for( j = 0; j < i; j++ )
368 p_demux->mpeg.pf_demux_ts( p_input, p_data[j],
369 (psi_callback_t) &PSI_CALLBACK );
376 #if defined MODULE_NAME_IS_demuxstream_old
378 * PSI demultiplexing and decoding without libdvbpsi
381 /*****************************************************************************
382 * DemuxPSI : makes up complete PSI data
383 *****************************************************************************/
384 static void TSDemuxPSI( input_thread_t * p_input, data_packet_t * p_data,
385 es_descriptor_t * p_es, vlc_bool_t b_unit_start )
387 es_ts_data_t * p_demux_data;
389 p_demux_data = (es_ts_data_t *)p_es->p_demux_data;
391 #define p_psi (p_demux_data->p_psi_section)
392 #define p (p_data->p_payload_start)
396 /* unit_start set to 1 -> presence of a pointer field
397 * (see ISO/IEC 13818 (2.4.4.2) which should be set to 0x00 */
398 if( (uint8_t)p[0] != 0x00 )
401 "non-zero pointer field found, trying to continue" );
409 /* This is the begining of a new section */
411 if( ((uint8_t)(p[1]) & 0xc0) != 0x80 )
413 msg_Warn( p_input, "invalid PSI packet" );
418 p_psi->i_section_length = ((p[1] & 0xF) << 8) | p[2];
419 p_psi->b_section_complete = 0;
420 p_psi->i_read_in_section = 0;
421 p_psi->i_section_number = (uint8_t)p[6];
423 if( p_psi->b_is_complete || p_psi->i_section_number == 0 )
425 /* This is a new PSI packet */
426 p_psi->b_is_complete = 0;
428 p_psi->i_version_number = ( p[5] >> 1 ) & 0x1f;
429 p_psi->i_last_section_number = (uint8_t)p[7];
431 /* We'll write at the begining of the buffer */
432 p_psi->p_current = p_psi->buffer;
436 if( p_psi->b_section_complete )
438 /* New Section of an already started PSI */
439 p_psi->b_section_complete = 0;
441 if( p_psi->i_version_number != (( p[5] >> 1 ) & 0x1f) )
444 "PSI version differs inside same PAT" );
447 if( p_psi->i_section_number + 1 != (uint8_t)p[6] )
450 "PSI Section discontinuity, packet lost?" );
454 p_psi->i_section_number++;
458 msg_Warn( p_input, "got unexpected new PSI section" );
465 if( !p_psi->b_trash )
468 if( (p_data->p_payload_end - p) >=
469 ( p_psi->i_section_length - p_psi->i_read_in_section ) )
471 /* The end of the section is in this TS packet */
472 memcpy( p_psi->p_current, p,
473 (p_psi->i_section_length - p_psi->i_read_in_section) );
475 p_psi->b_section_complete = 1;
477 (p_psi->i_section_length - p_psi->i_read_in_section);
479 if( p_psi->i_section_number == p_psi->i_last_section_number )
481 /* This was the last section of PSI */
482 p_psi->b_is_complete = 1;
484 switch( p_demux_data->i_psi_type)
487 TSDecodePAT( p_input, p_es );
490 TSDecodePMT( p_input, p_es );
493 msg_Warn( p_input, "received unknown PSI in DemuxPSI" );
499 memcpy( p_psi->buffer, p, p_data->p_payload_end - p );
500 p_psi->i_read_in_section += p_data->p_payload_end - p;
502 p_psi->p_current += p_data->p_payload_end - p;
509 input_DeletePacket( p_input->p_method_data, p_data );
514 /*****************************************************************************
515 * DecodePAT : Decodes Programm association table and deal with it
516 *****************************************************************************/
517 static void TSDecodePAT( input_thread_t * p_input, es_descriptor_t * p_es )
519 stream_ts_data_t * p_stream_data;
520 es_ts_data_t * p_demux_data;
522 pgrm_descriptor_t * p_pgrm;
523 es_descriptor_t * p_current_es;
524 byte_t * p_current_data;
526 int i_section_length, i_program_id, i_pmt_pid;
527 int i_loop, i_current_section;
529 vlc_bool_t b_changed = 0;
531 p_demux_data = (es_ts_data_t *)p_es->p_demux_data;
532 p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
534 #define p_psi (p_demux_data->p_psi_section)
536 /* Not so fast, Mike ! If the PAT version has changed, we first check
537 * that its content has really changed before doing anything */
538 if( p_stream_data->i_pat_version != p_psi->i_version_number )
540 int i_programs = p_input->stream.i_pgrm_number;
542 p_current_data = p_psi->buffer;
546 i_section_length = ((uint32_t)(p_current_data[1] & 0xF) << 8) |
548 i_current_section = (uint8_t)p_current_data[6];
551 ( i_loop < (i_section_length - 9) / 4 ) && !b_changed;
554 i_program_id = ( (uint32_t)*(p_current_data + i_loop * 4 + 8) << 8 )
555 | *(p_current_data + i_loop * 4 + 9);
556 i_pmt_pid = ( ((uint32_t)*(p_current_data + i_loop * 4 + 10) & 0x1F)
558 | *(p_current_data + i_loop * 4 + 11);
562 if( (p_pgrm = input_FindProgram( p_input, i_program_id ))
563 && (p_current_es = input_FindES( p_input, i_pmt_pid ))
564 && p_current_es->p_pgrm == p_pgrm
565 && p_current_es->i_id == i_pmt_pid
566 && ((es_ts_data_t *)p_current_es->p_demux_data)->b_psi
567 && ((es_ts_data_t *)p_current_es->p_demux_data)
568 ->i_psi_type == PSI_IS_PMT )
579 p_current_data += 3 + i_section_length;
581 } while( ( i_current_section < p_psi->i_last_section_number )
584 /* If we didn't find the expected amount of programs, the PAT has
585 * changed. Otherwise, it only changed if b_changed is already != 0 */
586 b_changed = b_changed || i_programs;
591 /* PAT has changed. We are going to delete all programs and
592 * create new ones. We chose not to only change what was needed
593 * as a PAT change may mean the stream is radically changing and
594 * this is a secure method to avoid crashes */
595 es_ts_data_t * p_es_demux;
596 pgrm_ts_data_t * p_pgrm_demux;
598 p_current_data = p_psi->buffer;
600 /* Delete all programs */
601 while( p_input->stream.i_pgrm_number )
603 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
608 i_section_length = ((uint32_t)(p_current_data[1] & 0xF) << 8) |
610 i_current_section = (uint8_t)p_current_data[6];
612 for( i_loop = 0; i_loop < (i_section_length - 9) / 4 ; i_loop++ )
614 i_program_id = ( (uint32_t)*(p_current_data + i_loop * 4 + 8) << 8 )
615 | *(p_current_data + i_loop * 4 + 9);
616 i_pmt_pid = ( ((uint32_t)*(p_current_data + i_loop * 4 + 10) & 0x1F)
618 | *(p_current_data + i_loop * 4 + 11);
620 /* If program = 0, we're having info about NIT not PMT */
623 /* Add this program */
624 p_pgrm = input_AddProgram( p_input, i_program_id,
625 sizeof( pgrm_ts_data_t ) );
627 /* whatis the PID of the PMT of this program */
628 p_pgrm_demux = (pgrm_ts_data_t *)p_pgrm->p_demux_data;
629 p_pgrm_demux->i_pmt_version = PMT_UNINITIALIZED;
631 /* Add the PMT ES to this program */
632 p_current_es = input_AddES( p_input, p_pgrm,(uint16_t)i_pmt_pid,
633 UNKNOWN_ES, NULL, sizeof( es_ts_data_t) );
634 p_current_es->i_fourcc = VLC_FOURCC( 'p', 'm', 't', ' ' );
635 p_es_demux = (es_ts_data_t *)p_current_es->p_demux_data;
636 p_es_demux->b_psi = 1;
637 p_es_demux->i_psi_type = PSI_IS_PMT;
639 p_es_demux->p_psi_section =
640 malloc( sizeof( psi_section_t ) );
641 p_es_demux->p_psi_section->b_is_complete = 0;
642 p_es_demux->i_continuity_counter = 0xFF;
646 p_current_data += 3 + i_section_length;
648 } while( i_current_section < p_psi->i_last_section_number );
650 /* Go to the beginning of the next section */
651 p_stream_data->i_pat_version = p_psi->i_version_number;
658 /*****************************************************************************
659 * DecodePMT : decode a given Program Stream Map
660 * ***************************************************************************
661 * When the PMT changes, it may mean a deep change in the stream, and it is
662 * careful to delete the ES and add them again. If the PMT doesn't change,
663 * there no need to do anything.
664 *****************************************************************************/
665 static void TSDecodePMT( input_thread_t * p_input, es_descriptor_t * p_es )
668 pgrm_ts_data_t * p_pgrm_data;
669 es_ts_data_t * p_demux_data;
670 vlc_bool_t b_vls_compat = config_GetInt( p_input, "vls-backwards-compat" );
672 p_demux_data = (es_ts_data_t *)p_es->p_demux_data;
673 p_pgrm_data = (pgrm_ts_data_t *)p_es->p_pgrm->p_demux_data;
675 #define p_psi (p_demux_data->p_psi_section)
677 if( p_psi->i_version_number != p_pgrm_data->i_pmt_version )
679 es_descriptor_t * p_new_es;
680 es_ts_data_t * p_es_demux;
681 byte_t * p_current_data, * p_current_section;
682 int i_section_length,i_current_section;
683 int i_prog_info_length, i_loop;
684 int i_es_info_length, i_pid, i_stream_type;
686 p_current_section = p_psi->buffer;
687 p_current_data = p_psi->buffer;
689 p_pgrm_data->i_pcr_pid = ( ((uint32_t)*(p_current_section + 8) & 0x1F) << 8 ) |
690 *(p_current_section + 9);
693 /* Lock stream information */
694 vlc_mutex_lock( &p_input->stream.stream_lock );
696 /* Delete all ES in this program except the PSI. We start from the
697 * end because i_es_number gets decremented after each deletion. */
698 for( i_loop = p_es->p_pgrm->i_es_number ; i_loop ; )
701 p_es_demux = (es_ts_data_t *)
702 p_es->p_pgrm->pp_es[i_loop]->p_demux_data;
703 if ( ! p_es_demux->b_psi )
705 input_DelES( p_input, p_es->p_pgrm->pp_es[i_loop] );
709 /* Then add what we received in this PMT */
712 i_section_length = ( ((uint32_t)*(p_current_data + 1) & 0xF) << 8 ) |
713 *(p_current_data + 2);
714 i_current_section = (uint8_t)p_current_data[6];
715 i_prog_info_length = ( ((uint32_t)*(p_current_data + 10) & 0xF) << 8 ) |
716 *(p_current_data + 11);
718 /* For the moment we ignore program descriptors */
719 p_current_data += 12 + i_prog_info_length;
721 /* The end of the section, before the CRC is at
722 * p_current_section + i_section_length -1 */
723 while( p_current_data < p_current_section + i_section_length -1 )
725 i_stream_type = (int)p_current_data[0];
726 i_pid = ( ((uint32_t)*(p_current_data + 1) & 0x1F) << 8 ) |
727 *(p_current_data + 2);
728 i_es_info_length = ( ((uint32_t)*(p_current_data + 3) & 0xF) << 8 ) |
729 *(p_current_data + 4);
731 /* Tell the interface what kind of stream it is and select
732 * the required ones */
734 int i_fourcc, i_cat, i_stream_id;
736 switch( i_stream_type )
740 case MPEG2_MOTO_VIDEO_ES:
741 /* This isn't real, but we don't actually use
744 i_fourcc = VLC_FOURCC('m','p','g','v');
749 /* This isn't real, but we don't actually use
752 i_fourcc = VLC_FOURCC('m','p','g','a');
757 i_fourcc = VLC_FOURCC('a','5','2',' ');
759 i_fourcc = VLC_FOURCC('a','5','2','b');
764 i_fourcc = VLC_FOURCC('l','p','c','m');
770 i_fourcc = VLC_FOURCC('s','p','u',' ');
772 i_fourcc = VLC_FOURCC('s','p','u','b');
777 i_fourcc = VLC_FOURCC('s','d','d','s');
782 i_fourcc = VLC_FOURCC('d','t','s',' ');
786 /* 'b' stands for 'buggy' */
788 i_fourcc = VLC_FOURCC('a','5','2','b');
793 i_fourcc = VLC_FOURCC('l','p','c','b');
798 i_fourcc = VLC_FOURCC('s','p','u','b');
802 case AAC_ADTS_AUDIO_ES:
803 i_fourcc = VLC_FOURCC('m','p','4','a');
815 /* Add this ES to the program */
816 p_new_es = input_AddES( p_input, p_es->p_pgrm, (uint16_t)i_pid,
817 i_cat, NULL, sizeof( es_ts_data_t ) );
819 ((es_ts_data_t *)p_new_es->p_demux_data)->i_continuity_counter = 0xFF;
821 p_new_es->i_stream_id = i_stream_id;
822 p_new_es->i_fourcc = i_fourcc;
826 p_current_data += 5 + i_es_info_length;
829 /* Go to the beginning of the next section*/
830 p_current_data += 3 + i_section_length;
834 } while( i_current_section < p_psi->i_last_section_number );
836 p_pgrm_data->i_pmt_version = p_psi->i_version_number;
838 /* if no program is selected :*/
839 if( !p_input->stream.p_selected_program )
841 pgrm_descriptor_t * p_pgrm_to_select;
842 uint16_t i_id = (uint16_t)config_GetInt( p_input, "program" );
844 if( i_id != 0 ) /* if user specified a program */
846 p_pgrm_to_select = input_FindProgram( p_input, i_id );
848 if( p_pgrm_to_select && p_pgrm_to_select == p_es->p_pgrm )
849 p_input->pf_set_program( p_input, p_pgrm_to_select );
852 p_input->pf_set_program( p_input, p_es->p_pgrm );
855 /* if the pmt belongs to the currently selected program, we
856 * reselect it to update its ES */
857 else if( p_es->p_pgrm == p_input->stream.p_selected_program )
859 p_input->pf_set_program( p_input, p_es->p_pgrm );
862 /* inform interface that stream has changed */
863 p_input->stream.b_changed = 1;
865 vlc_mutex_unlock( &p_input->stream.stream_lock );
871 #elif defined MODULE_NAME_IS_demuxstream
873 * PSI Decoding using libdvbpsi
876 /*****************************************************************************
877 * DemuxPSI : send the PSI to the right libdvbpsi decoder
878 *****************************************************************************/
879 static void TS_DVBPSI_DemuxPSI( input_thread_t * p_input,
880 data_packet_t * p_data,
881 es_descriptor_t * p_es,
882 vlc_bool_t b_unit_start )
884 es_ts_data_t * p_es_demux_data;
885 pgrm_ts_data_t * p_pgrm_demux_data;
886 stream_ts_data_t * p_stream_demux_data;
888 p_es_demux_data = (es_ts_data_t *)p_es->p_demux_data;
889 p_stream_demux_data = (stream_ts_data_t *) p_input->stream.p_demux_data;
891 switch( p_es_demux_data->i_psi_type)
895 (dvbpsi_handle)p_stream_demux_data->p_pat_handle,
896 p_data->p_demux_start );
899 p_pgrm_demux_data = ( pgrm_ts_data_t * )p_es->p_pgrm->p_demux_data;
901 (dvbpsi_handle)p_pgrm_demux_data->p_pmt_handle,
902 p_data->p_demux_start );
905 msg_Warn( p_input, "received unknown PSI in DemuxPSI" );
908 input_DeletePacket( p_input->p_method_data, p_data );
910 /*****************************************************************************
911 * MP4 specific functions
912 *****************************************************************************/
913 static int MP4_DescriptorLength( int *pi_data, uint8_t **pp_data )
916 unsigned int i_len = 0;
922 i_len = ( i_len << 7 ) + ( i_b&0x7f );
928 static int MP4_GetByte( int *pi_data, uint8_t **pp_data )
943 static int MP4_GetWord( int *pi_data, uint8_t **pp_data )
946 i1 = MP4_GetByte( pi_data, pp_data );
947 i2 = MP4_GetByte( pi_data, pp_data );
948 return( ( i1 << 8 ) | i2 );
950 static int MP4_Get3Bytes( int *pi_data, uint8_t **pp_data )
953 i1 = MP4_GetByte( pi_data, pp_data );
954 i2 = MP4_GetByte( pi_data, pp_data );
955 i3 = MP4_GetByte( pi_data, pp_data );
956 return( ( i1 << 16 ) | ( i2 << 8) | i3 );
959 static uint32_t MP4_GetDWord( int *pi_data, uint8_t **pp_data )
962 i1 = MP4_GetWord( pi_data, pp_data );
963 i2 = MP4_GetWord( pi_data, pp_data );
964 return( ( i1 << 16 ) | i2 );
967 static char* MP4_GetURL( int *pi_data, uint8_t **pp_data )
972 i_url_len = MP4_GetByte( pi_data, pp_data );
973 url = malloc( i_url_len + 1 );
974 for( i = 0; i < i_url_len; i++ )
976 url[i] = MP4_GetByte( pi_data, pp_data );
978 url[i_url_len] = '\0';
982 static void MP4_IODParse( iod_descriptor_t *p_iod, int i_data, uint8_t *p_data )
990 fprintf( stderr, "\n************ IOD ************" );
991 for( i = 0; i < 255; i++ )
993 p_iod->es_descr[i].b_ok = 0;
1002 p_iod->i_iod_label = MP4_GetByte( &i_data, &p_data );
1003 fprintf( stderr, "\n* iod_label:%d", p_iod->i_iod_label );
1004 fprintf( stderr, "\n* ===========" );
1005 fprintf( stderr, "\n* tag:0x%x", p_data[0] );
1007 if( MP4_GetByte( &i_data, &p_data ) != 0x02 )
1009 fprintf( stderr, "\n ERR: tag != 0x02" );
1013 i_iod_length = MP4_DescriptorLength( &i_data, &p_data );
1014 fprintf( stderr, "\n* length:%d", i_iod_length );
1015 if( i_iod_length > i_data )
1017 i_iod_length = i_data;
1020 p_iod->i_od_id = ( MP4_GetByte( &i_data, &p_data ) << 2 );
1021 i_flags = MP4_GetByte( &i_data, &p_data );
1022 p_iod->i_od_id |= i_flags >> 6;
1023 b_url = ( i_flags >> 5 )&0x01;
1025 fprintf( stderr, "\n* od_id:%d", p_iod->i_od_id );
1026 fprintf( stderr, "\n* url flag:%d", b_url );
1027 fprintf( stderr, "\n* includeInlineProfileLevel flag:%d", ( i_flags >> 4 )&0x01 );
1031 p_iod->psz_url = MP4_GetURL( &i_data, &p_data );
1032 fprintf( stderr, "\n* url string:%s", p_iod->psz_url );
1033 fprintf( stderr, "\n*****************************\n" );
1038 p_iod->psz_url = NULL;
1041 p_iod->i_ODProfileLevelIndication = MP4_GetByte( &i_data, &p_data );
1042 p_iod->i_sceneProfileLevelIndication = MP4_GetByte( &i_data, &p_data );
1043 p_iod->i_audioProfileLevelIndication = MP4_GetByte( &i_data, &p_data );
1044 p_iod->i_visualProfileLevelIndication = MP4_GetByte( &i_data, &p_data );
1045 p_iod->i_graphicsProfileLevelIndication = MP4_GetByte( &i_data, &p_data );
1047 fprintf( stderr, "\n* ODProfileLevelIndication:%d", p_iod->i_ODProfileLevelIndication );
1048 fprintf( stderr, "\n* sceneProfileLevelIndication:%d", p_iod->i_sceneProfileLevelIndication );
1049 fprintf( stderr, "\n* audioProfileLevelIndication:%d", p_iod->i_audioProfileLevelIndication );
1050 fprintf( stderr, "\n* visualProfileLevelIndication:%d", p_iod->i_visualProfileLevelIndication );
1051 fprintf( stderr, "\n* graphicsProfileLevelIndication:%d", p_iod->i_graphicsProfileLevelIndication );
1054 while( i_data > 0 && i_es_index < 255)
1056 int i_tag, i_length;
1058 uint8_t *p_data_sav;
1060 i_tag = MP4_GetByte( &i_data, &p_data );
1061 i_length = MP4_DescriptorLength( &i_data, &p_data );
1063 i_data_sav = i_data;
1064 p_data_sav = p_data;
1072 #define es_descr p_iod->es_descr[i_es_index]
1073 int i_decoderConfigDescr_length;
1074 fprintf( stderr, "\n* - ES_Descriptor length:%d", i_length );
1077 es_descr.i_es_id = MP4_GetWord( &i_data, &p_data );
1078 i_flags = MP4_GetByte( &i_data, &p_data );
1079 es_descr.b_streamDependenceFlag = ( i_flags >> 7 )&0x01;
1080 b_url = ( i_flags >> 6 )&0x01;
1081 es_descr.b_OCRStreamFlag = ( i_flags >> 5 )&0x01;
1082 es_descr.i_streamPriority = i_flags & 0x1f;
1083 fprintf( stderr, "\n* * streamDependenceFlag:%d", es_descr.b_streamDependenceFlag );
1084 fprintf( stderr, "\n* * OCRStreamFlag:%d", es_descr.b_OCRStreamFlag );
1085 fprintf( stderr, "\n* * streamPriority:%d", es_descr.i_streamPriority );
1087 if( es_descr.b_streamDependenceFlag )
1089 es_descr.i_dependOn_es_id = MP4_GetWord( &i_data, &p_data );
1090 fprintf( stderr, "\n* * dependOn_es_id:%d", es_descr.i_dependOn_es_id );
1095 es_descr.psz_url = MP4_GetURL( &i_data, &p_data );
1096 fprintf( stderr, "\n* url string:%s", es_descr.psz_url );
1100 es_descr.psz_url = NULL;
1103 if( es_descr.b_OCRStreamFlag )
1105 es_descr.i_OCR_es_id = MP4_GetWord( &i_data, &p_data );
1106 fprintf( stderr, "\n* * OCR_es_id:%d", es_descr.i_OCR_es_id );
1109 if( MP4_GetByte( &i_data, &p_data ) != 0x04 )
1111 fprintf( stderr, "\n* ERR missing DecoderConfigDescr" );
1115 i_decoderConfigDescr_length = MP4_DescriptorLength( &i_data, &p_data );
1117 fprintf( stderr, "\n* - DecoderConfigDesc length:%d", i_decoderConfigDescr_length );
1118 #define dec_descr es_descr.dec_descr
1119 dec_descr.i_objectTypeIndication = MP4_GetByte( &i_data, &p_data );
1120 i_flags = MP4_GetByte( &i_data, &p_data );
1121 dec_descr.i_streamType = i_flags >> 2;
1122 dec_descr.b_upStream = ( i_flags >> 1 )&0x01;
1123 dec_descr.i_bufferSizeDB = MP4_Get3Bytes( &i_data, &p_data );
1124 dec_descr.i_maxBitrate = MP4_GetDWord( &i_data, &p_data );
1125 dec_descr.i_avgBitrate = MP4_GetDWord( &i_data, &p_data );
1126 fprintf( stderr, "\n* * objectTypeIndication:0x%x", dec_descr.i_objectTypeIndication );
1127 fprintf( stderr, "\n* * streamType:0x%x", dec_descr.i_streamType );
1128 fprintf( stderr, "\n* * upStream:%d", dec_descr.b_upStream );
1129 fprintf( stderr, "\n* * bufferSizeDB:%d", dec_descr.i_bufferSizeDB );
1130 fprintf( stderr, "\n* * maxBitrate:%d", dec_descr.i_maxBitrate );
1131 fprintf( stderr, "\n* * avgBitrate:%d", dec_descr.i_avgBitrate );
1132 if( i_decoderConfigDescr_length > 13 && MP4_GetByte( &i_data, &p_data ) == 0x05 )
1135 dec_descr.i_decoder_specific_info_len =
1136 MP4_DescriptorLength( &i_data, &p_data );
1137 if( dec_descr.i_decoder_specific_info_len > 0 )
1139 dec_descr.p_decoder_specific_info =
1140 malloc( dec_descr.i_decoder_specific_info_len );
1142 for( i = 0; i < dec_descr.i_decoder_specific_info_len; i++ )
1144 dec_descr.p_decoder_specific_info[i] = MP4_GetByte( &i_data, &p_data );
1149 dec_descr.i_decoder_specific_info_len = 0;
1150 dec_descr.p_decoder_specific_info = NULL;
1154 #define sl_descr es_descr.sl_descr
1156 int i_SLConfigDescr_length;
1159 if( MP4_GetByte( &i_data, &p_data ) != 0x06 )
1161 fprintf( stderr, "\n* ERR missing SLConfigDescr" );
1165 i_SLConfigDescr_length = MP4_DescriptorLength( &i_data, &p_data );
1167 fprintf( stderr, "\n* - SLConfigDescr length:%d", i_SLConfigDescr_length );
1168 i_predefined = MP4_GetByte( &i_data, &p_data );
1169 fprintf( stderr, "\n* * i_predefined:0x%x", i_predefined );
1170 switch( i_predefined )
1174 sl_descr.b_useAccessUnitStartFlag = 0;
1175 sl_descr.b_useAccessUnitEndFlag = 0;
1176 sl_descr.b_useRandomAccessPointFlag = 0;
1177 //sl_descr.b_useRandomAccessUnitsOnlyFlag = 0;
1178 sl_descr.b_usePaddingFlag = 0;
1179 sl_descr.b_useTimeStampsFlags = 0;
1180 sl_descr.b_useIdleFlag = 0;
1181 sl_descr.b_durationFlag = 0; // FIXME FIXME
1182 sl_descr.i_timeStampResolution = 1000;
1183 sl_descr.i_OCRResolution = 0; // FIXME FIXME
1184 sl_descr.i_timeStampLength = 32;
1185 sl_descr.i_OCRLength = 0; // FIXME FIXME
1186 sl_descr.i_AU_Length = 0;
1187 sl_descr.i_instantBitrateLength= 0; // FIXME FIXME
1188 sl_descr.i_degradationPriorityLength= 0;
1189 sl_descr.i_AU_seqNumLength = 0;
1190 sl_descr.i_packetSeqNumLength = 0;
1191 if( sl_descr.b_durationFlag )
1193 sl_descr.i_timeScale = 0; // FIXME FIXME
1194 sl_descr.i_accessUnitDuration = 0; // FIXME FIXME
1195 sl_descr.i_compositionUnitDuration= 0; // FIXME FIXME
1197 if( !sl_descr.b_useTimeStampsFlags )
1199 sl_descr.i_startDecodingTimeStamp = 0; // FIXME FIXME
1200 sl_descr.i_startCompositionTimeStamp= 0; // FIXME FIXME
1205 fprintf( stderr, "\n* ERR unsupported SLConfigDescr predefined" );
1214 fprintf( stderr, "\n* - OD tag:0x%x length:%d (Unsupported)", i_tag, i_length );
1218 p_data = p_data_sav + i_length;
1219 i_data = i_data_sav - i_length;
1224 fprintf( stderr, "\n*****************************\n" );
1227 static void MP4_IODClean( iod_descriptor_t *p_iod )
1231 if( p_iod->psz_url )
1233 free( p_iod->psz_url );
1234 p_iod->psz_url = NULL;
1238 for( i = 0; i < 255; i++ )
1240 #define es_descr p_iod->es_descr[i]
1243 if( es_descr.psz_url )
1245 free( es_descr.psz_url );
1246 es_descr.psz_url = NULL;
1250 if( es_descr.dec_descr.p_decoder_specific_info != NULL )
1252 free( es_descr.dec_descr.p_decoder_specific_info );
1253 es_descr.dec_descr.p_decoder_specific_info = NULL;
1254 es_descr.dec_descr.i_decoder_specific_info_len = 0;
1263 /*****************************************************************************
1264 * HandlePAT: will treat a PAT returned by dvbpsi
1265 *****************************************************************************/
1266 static void TS_DVBPSI_HandlePAT( input_thread_t * p_input,
1267 dvbpsi_pat_t * p_new_pat )
1269 dvbpsi_pat_program_t * p_pgrm;
1270 pgrm_descriptor_t * p_new_pgrm;
1271 pgrm_ts_data_t * p_pgrm_demux;
1272 es_descriptor_t * p_current_es;
1273 es_ts_data_t * p_es_demux;
1274 stream_ts_data_t * p_stream_data;
1276 vlc_mutex_lock( &p_input->stream.stream_lock );
1278 p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
1280 if( ( p_new_pat->b_current_next &&
1281 ( p_new_pat->i_version != p_stream_data->i_pat_version ) ) ||
1282 p_stream_data->i_pat_version == PAT_UNINITIALIZED )
1284 msg_Dbg( p_input, "processing PAT version %d", p_new_pat->i_version );
1286 /* Delete all programs */
1287 while( p_input->stream.i_pgrm_number )
1289 pgrm_ts_data_t *p_pgrm_demux_old =
1290 (pgrm_ts_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
1292 if( p_pgrm_demux_old->b_mpeg4 )
1294 MP4_IODClean( &p_pgrm_demux_old->iod );
1297 /* Delete old PMT decoder */
1298 if( p_pgrm_demux_old->p_pmt_handle )
1299 dvbpsi_DetachPMT( p_pgrm_demux_old->p_pmt_handle );
1301 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
1304 /* treat the new programs list */
1305 p_pgrm = p_new_pat->p_first_program;
1309 msg_Dbg( p_input, "new program: %d", p_pgrm->i_number );
1311 /* If program = 0, we're having info about NIT not PMT */
1312 if( p_pgrm->i_number )
1314 /* Add this program */
1315 p_new_pgrm = input_AddProgram( p_input, p_pgrm->i_number,
1316 sizeof( pgrm_ts_data_t ) );
1318 p_pgrm_demux = (pgrm_ts_data_t *)p_new_pgrm->p_demux_data;
1319 p_pgrm_demux->i_pmt_version = PMT_UNINITIALIZED;
1321 /* Add the PMT ES to this program */
1322 p_current_es = input_AddES( p_input, p_new_pgrm,
1323 (uint16_t)p_pgrm->i_pid, UNKNOWN_ES,
1324 NULL, sizeof(es_ts_data_t) );
1325 p_current_es->i_fourcc = VLC_FOURCC( 'p', 'm', 't', ' ' );
1326 p_es_demux = (es_ts_data_t *)p_current_es->p_demux_data;
1327 p_es_demux->b_psi = 1;
1328 p_es_demux->i_psi_type = PSI_IS_PMT;
1329 p_es_demux->p_psi_section = NULL;
1330 p_es_demux->i_continuity_counter = 0xFF;
1332 /* Create a PMT decoder */
1333 p_pgrm_demux->p_pmt_handle = (dvbpsi_handle *)
1334 dvbpsi_AttachPMT( p_pgrm->i_number,
1335 (dvbpsi_pmt_callback) &TS_DVBPSI_HandlePMT,
1338 if( p_pgrm_demux->p_pmt_handle == NULL )
1340 msg_Err( p_input, "could not create PMT decoder" );
1341 p_input->b_error = 1;
1346 p_pgrm = p_pgrm->p_next;
1349 p_stream_data->i_pat_version = p_new_pat->i_version;
1351 vlc_mutex_unlock( &p_input->stream.stream_lock );
1355 /*****************************************************************************
1356 * HandlePMT: will treat a PMT returned by dvbpsi
1357 *****************************************************************************/
1358 static void TS_DVBPSI_HandlePMT( input_thread_t * p_input,
1359 dvbpsi_pmt_t * p_new_pmt )
1361 dvbpsi_pmt_es_t * p_es;
1362 pgrm_descriptor_t * p_pgrm;
1363 es_descriptor_t * p_new_es;
1364 pgrm_ts_data_t * p_pgrm_demux;
1365 vlc_bool_t b_vls_compat = config_GetInt( p_input, "vls-backwards-compat" );
1367 vlc_mutex_lock( &p_input->stream.stream_lock );
1369 p_pgrm = input_FindProgram( p_input, p_new_pmt->i_program_number );
1371 if( p_pgrm == NULL )
1373 msg_Warn( p_input, "PMT of unreferenced program found" );
1377 p_pgrm_demux = (pgrm_ts_data_t *)p_pgrm->p_demux_data;
1378 p_pgrm_demux->i_pcr_pid = p_new_pmt->i_pcr_pid;
1380 if( ( p_new_pmt->b_current_next &&
1381 ( p_new_pmt->i_version != p_pgrm_demux->i_pmt_version ) ) ||
1382 p_pgrm_demux->i_pmt_version == PMT_UNINITIALIZED )
1384 dvbpsi_descriptor_t *p_dr = p_new_pmt->p_first_descriptor;
1387 msg_Dbg( p_input, "processing PMT for program %d version %d",
1388 p_new_pmt->i_program_number, p_new_pmt->i_version );
1390 /* Delete all ES in this program except the PSI. We start from the
1391 * end because i_es_number gets decremented after each deletion. */
1392 for( i_loop = p_pgrm->i_es_number ; i_loop > 0 ; )
1394 es_ts_data_t * p_es_demux;
1397 p_es_demux = (es_ts_data_t *)
1398 p_pgrm->pp_es[i_loop]->p_demux_data;
1399 if ( !p_es_demux->b_psi )
1401 input_DelES( p_input, p_pgrm->pp_es[i_loop] );
1406 while( p_dr && ( p_dr->i_tag != 0x1d ) )
1407 p_dr = p_dr->p_next;
1410 msg_Warn( p_input, "found IOD descriptor" );
1411 MP4_IODParse( &p_pgrm_demux->iod, p_dr->i_length, p_dr->p_data );
1414 p_es = p_new_pmt->p_first_es;
1417 vlc_fourcc_t i_fourcc;
1418 int i_size, i_cat, i_stream_id = 0;
1419 es_ts_data_t demux_data;
1420 BITMAPINFOHEADER *p_bih = NULL;
1421 WAVEFORMATEX *p_wf = NULL;
1424 memset( &demux_data, 0, sizeof(es_ts_data_t) );
1426 msg_Dbg( p_input, "new PID 0x%x stream type 0x%x",
1427 p_es->i_pid, p_es->i_type );
1429 switch( p_es->i_type )
1431 case MPEG1_VIDEO_ES:
1432 case MPEG2_VIDEO_ES:
1433 case MPEG2_MOTO_VIDEO_ES:
1434 i_fourcc = VLC_FOURCC('m','p','g','v');
1437 case MPEG1_AUDIO_ES:
1438 case MPEG2_AUDIO_ES:
1439 i_fourcc = VLC_FOURCC('m','p','g','a');
1443 if ( !b_vls_compat )
1444 i_fourcc = VLC_FOURCC('a','5','2',' ');
1446 i_fourcc = VLC_FOURCC('a','5','2','b');
1451 if ( !b_vls_compat )
1452 i_fourcc = VLC_FOURCC('s','p','u',' ');
1454 i_fourcc = VLC_FOURCC('s','p','u','b');
1459 i_fourcc = VLC_FOURCC('l','p','c','m');
1464 i_fourcc = VLC_FOURCC('s','d','d','s');
1469 i_fourcc = VLC_FOURCC('d','t','s',' ');
1474 i_fourcc = VLC_FOURCC('a','5','2','b');
1479 i_fourcc = VLC_FOURCC('s','p','u','b');
1483 case LPCMB_AUDIO_ES:
1484 i_fourcc = VLC_FOURCC('l','p','c','b');
1488 case MPEG4_VIDEO_ES:
1489 i_fourcc = VLC_FOURCC('m','p','4','v');
1493 case MPEG4_AUDIO_ES:
1494 case AAC_ADTS_AUDIO_ES:
1495 i_fourcc = VLC_FOURCC('m','p','4','a');
1499 case MSCODEC_VIDEO_ES:
1500 i_fourcc = VLC_FOURCC(0,0,0,0); /* fixed later */
1504 case PES_PRIVATE_ES:
1505 /* We need to check a descriptor to find the real codec */
1506 i_fourcc = VLC_FOURCC(0,0,0,0); /* fixed later */
1516 if( p_es->i_type == MPEG4_VIDEO_ES ||
1517 p_es->i_type == MPEG4_AUDIO_ES )
1519 /* mpeg4 stream, search sl_descriptor */
1520 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
1522 while( p_dr && ( p_dr->i_tag != 0x1f ) ) p_dr = p_dr->p_next;
1524 if( p_dr && p_dr->i_length == 2 )
1526 int i_es_descr_index;
1528 demux_data.i_es_id =
1529 ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
1530 demux_data.p_es_descr = NULL;
1532 msg_Warn( p_input, "found SL_descriptor" );
1533 for( i_es_descr_index = 0; i_es_descr_index < 255;
1534 i_es_descr_index++ )
1536 if( p_pgrm_demux->iod.es_descr[i_es_descr_index].b_ok &&
1537 p_pgrm_demux->iod.es_descr[i_es_descr_index].i_es_id == demux_data.i_es_id )
1539 demux_data.p_es_descr =
1540 &p_pgrm_demux->iod.es_descr[i_es_descr_index];
1546 if( demux_data.p_es_descr != NULL )
1548 #define DESCR demux_data.p_es_descr->dec_descr
1549 demux_data.b_mpeg4 = 1;
1552 switch( DESCR.i_streamType )
1554 case 0x04: /* VisualStream */
1556 switch( DESCR.i_objectTypeIndication )
1559 i_fourcc = VLC_FOURCC('m','p','4','v'); // mpeg4
1567 i_fourcc = VLC_FOURCC( 'm','p','g','v' ); // mpeg2
1570 i_fourcc = VLC_FOURCC( 'm','p','g','v' ); // mpeg1
1573 i_fourcc = VLC_FOURCC( 'j','p','e','g' ); // mpeg1
1580 case 0x05: /* AudioStream */
1582 switch( DESCR.i_objectTypeIndication )
1585 i_fourcc = VLC_FOURCC('m','p','4','a'); // mpeg4
1590 i_fourcc = VLC_FOURCC('m','p','4','a');// mpeg2 aac
1593 i_fourcc = VLC_FOURCC('m','p','g','a'); // mpeg2
1596 i_fourcc = VLC_FOURCC('m','p','g','a'); // mpeg1
1612 i_size = sizeof( BITMAPINFOHEADER ) +
1613 DESCR.i_decoder_specific_info_len;
1614 p_bih = malloc( i_size );
1615 p_bih->biSize = i_size;
1617 p_bih->biHeight = 0;
1618 p_bih->biPlanes = 1;
1619 p_bih->biBitCount = 0;
1620 p_bih->biCompression = 0;
1621 p_bih->biSizeImage = 0;
1622 p_bih->biXPelsPerMeter = 0;
1623 p_bih->biYPelsPerMeter = 0;
1624 p_bih->biClrUsed = 0;
1625 p_bih->biClrImportant = 0;
1627 DESCR.p_decoder_specific_info,
1628 DESCR.i_decoder_specific_info_len );
1631 i_size = sizeof( WAVEFORMATEX ) +
1632 DESCR.i_decoder_specific_info_len;
1633 p_wf = malloc( i_size );
1634 p_wf->wFormatTag = 0xffff;
1635 p_wf->nChannels = 0;
1636 p_wf->nSamplesPerSec = 0;
1637 p_wf->nAvgBytesPerSec = 0;
1638 p_wf->nBlockAlign = 1;
1639 p_wf->wBitsPerSample = 0;
1640 p_wf->cbSize = DESCR.i_decoder_specific_info_len;
1642 DESCR.p_decoder_specific_info,
1643 DESCR.i_decoder_specific_info_len );
1652 "mpeg4 stream without (valid) sl_descriptor" );
1653 demux_data.b_mpeg4 = 0;
1657 else if( p_es->i_type == MSCODEC_VIDEO_ES )
1659 /* crapy ms codec stream, search private descriptor */
1660 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
1662 while( p_dr && ( p_dr->i_tag != 0xa0 ) ) p_dr = p_dr->p_next;
1664 if( p_dr && p_dr->i_length >= 8 )
1667 i_fourcc = VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
1668 p_dr->p_data[2], p_dr->p_data[3] );
1670 i_bih_size = (p_dr->p_data[8] << 8) | p_dr->p_data[9];
1671 i_size = sizeof( BITMAPINFOHEADER ) + i_bih_size;
1673 p_bih = malloc( i_size );
1674 p_bih->biSize = i_size;
1675 p_bih->biWidth = ( p_dr->p_data[4] << 8 )|p_dr->p_data[5];
1676 p_bih->biHeight = ( p_dr->p_data[6] << 8 )|p_dr->p_data[7];
1677 p_bih->biPlanes = 1;
1678 p_bih->biBitCount = 0;
1679 p_bih->biCompression = 0;
1680 p_bih->biSizeImage = 0;
1681 p_bih->biXPelsPerMeter = 0;
1682 p_bih->biYPelsPerMeter = 0;
1683 p_bih->biClrUsed = 0;
1684 p_bih->biClrImportant = 0;
1685 memcpy( &p_bih[1], &p_dr->p_data[10], i_bih_size );
1689 msg_Warn( p_input, "private ms-codec stream without bih "
1690 "private sl_descriptor" );
1695 else if( p_es->i_type == PES_PRIVATE_ES )
1697 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
1698 /* We have to find a descriptor giving the right codec */
1700 for(p_dr = p_es->p_first_descriptor; p_dr; p_dr = p_dr->p_next)
1702 if( p_dr->i_tag == 0x6a )
1705 i_fourcc = VLC_FOURCC( 'a', '5', '2', ' ' );
1708 else if( p_dr->i_tag == 0x59 )
1711 i_fourcc = VLC_FOURCC( 'd', 'v', 'b', 's' );
1715 if( i_fourcc == VLC_FOURCC(0,0,0,0) )
1718 "unknown codec/type for Private PES stream" );
1722 if( i_cat == AUDIO_ES || i_cat == SPU_ES )
1724 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
1725 while( p_dr && ( p_dr->i_tag != 0x0a ) ) p_dr = p_dr->p_next;
1729 dvbpsi_iso639_dr_t *p_decoded =
1730 dvbpsi_DecodeISO639Dr( p_dr );
1731 if( p_decoded->i_code_count > 0 )
1733 const iso639_lang_t * p_iso;
1734 p_iso = GetLang_2T((char*)p_decoded->i_iso_639_code);
1736 if( p_iso && strcmp(p_iso->psz_native_name,"Unknown"))
1738 if( p_iso->psz_native_name[0] )
1740 p_iso->psz_native_name, 20 );
1743 p_iso->psz_eng_name, 20 );
1748 (char*)p_decoded->i_iso_639_code);
1751 if( p_iso->psz_native_name[0] )
1753 p_iso->psz_native_name, 20 );
1756 p_iso->psz_eng_name, 20 );
1760 strncpy( psz_desc, p_decoded->i_iso_639_code, 3 );
1765 switch( p_es->i_type )
1767 case MPEG1_AUDIO_ES:
1768 case MPEG2_AUDIO_ES:
1769 strcat( psz_desc, " (mpeg)" );
1772 case LPCMB_AUDIO_ES:
1773 strcat( psz_desc, " (lpcm)" );
1777 strcat( psz_desc, " (A52)" );
1779 case MPEG4_AUDIO_ES:
1780 case AAC_ADTS_AUDIO_ES:
1781 strcat( psz_desc, " (aac)" );
1787 p_new_es = input_AddES( p_input, p_pgrm, (uint16_t)p_es->i_pid,
1788 i_cat, psz_desc, sizeof( es_ts_data_t ) );
1789 if( p_new_es == NULL )
1791 msg_Err( p_input, "could not add ES %d", p_es->i_pid );
1792 p_input->b_error = 1;
1795 p_new_es->i_fourcc = i_fourcc;
1796 p_new_es->i_stream_id = i_stream_id;
1797 p_new_es->p_bitmapinfoheader = (void*)p_bih;
1798 p_new_es->p_waveformatex = (void*)p_wf;
1799 memcpy( p_new_es->p_demux_data, &demux_data,
1800 sizeof(es_ts_data_t) );
1802 ((es_ts_data_t *)p_new_es->p_demux_data)->i_continuity_counter =
1805 p_es = p_es->p_next;
1808 /* if no program is selected :*/
1809 if( !p_input->stream.p_selected_program )
1811 pgrm_descriptor_t * p_pgrm_to_select;
1812 uint16_t i_id = (uint16_t)config_GetInt( p_input, "program" );
1814 if( i_id != 0 ) /* if user specified a program */
1816 p_pgrm_to_select = input_FindProgram( p_input, i_id );
1818 if( p_pgrm_to_select && p_pgrm_to_select == p_pgrm )
1819 p_input->pf_set_program( p_input, p_pgrm_to_select );
1822 p_input->pf_set_program( p_input, p_pgrm );
1824 /* if the pmt belongs to the currently selected program, we
1825 * reselect it to update its ES */
1826 else if( p_pgrm == p_input->stream.p_selected_program )
1828 p_input->pf_set_program( p_input, p_pgrm );
1831 p_pgrm_demux->i_pmt_version = p_new_pmt->i_version;
1832 p_input->stream.b_changed = 1;
1834 vlc_mutex_unlock( &p_input->stream.stream_lock );