1 /*****************************************************************************
2 * ts.c: Transport Stream input module for VLC.
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
5 * $Id: ts.c,v 1.10 2004/02/01 04:50:13 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include <vlc/input.h>
35 #include "../mux/mpeg/csa.h"
37 /* Include dvbpsi headers */
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>
44 # include <dvbpsi/psi.h>
47 # include "descriptor.h"
48 # include "tables/pat.h"
49 # include "tables/pmt.h"
50 # include "descriptors/dr.h"
55 * - XXX: do not mark options message to be translated, they are too osbcure for now ...
60 /*****************************************************************************
62 *****************************************************************************/
63 static int Open ( vlc_object_t * );
64 static void Close ( vlc_object_t * );
67 set_description( _("ISO 13818-1 MPEG Transport Stream input - new" ) );
68 add_string( "ts-extra-pmt", NULL, NULL, "extra PMT", "allow user to specify an extra pmt (pmt_pid=pid:stream_type[,...])", VLC_TRUE );
69 add_bool( "ts-es-id-pid", 0, NULL, "set id of es to pid", "set id of es to pid", VLC_TRUE );
70 add_string( "ts-out", NULL, NULL, "fast udp streaming", "send TS to specific ip:port by udp (you must know what you are doing)", VLC_TRUE );
71 add_integer( "ts-out-mtu", 1500, NULL, "MTU for out mode", "MTU for out mode", VLC_TRUE );
72 add_string( "ts-csa-ck", NULL, NULL, "CSA ck", "CSA ck", VLC_TRUE );
73 set_capability( "demux2", 10 );
74 set_callbacks( Open, Close );
75 add_shortcut( "ts2" );
78 /*****************************************************************************
80 *****************************************************************************/
84 uint8_t i_objectTypeIndication;
86 vlc_bool_t b_upStream;
87 uint32_t i_bufferSizeDB;
88 uint32_t i_maxBitrate;
89 uint32_t i_avgBitrate;
91 int i_decoder_specific_info_len;
92 uint8_t *p_decoder_specific_info;
94 } decoder_config_descriptor_t;
98 vlc_bool_t b_useAccessUnitStartFlag;
99 vlc_bool_t b_useAccessUnitEndFlag;
100 vlc_bool_t b_useRandomAccessPointFlag;
101 vlc_bool_t b_useRandomAccessUnitsOnlyFlag;
102 vlc_bool_t b_usePaddingFlag;
103 vlc_bool_t b_useTimeStampsFlags;
104 vlc_bool_t b_useIdleFlag;
105 vlc_bool_t b_durationFlag;
106 uint32_t i_timeStampResolution;
107 uint32_t i_OCRResolution;
108 uint8_t i_timeStampLength;
111 uint8_t i_instantBitrateLength;
112 uint8_t i_degradationPriorityLength;
113 uint8_t i_AU_seqNumLength;
114 uint8_t i_packetSeqNumLength;
116 uint32_t i_timeScale;
117 uint16_t i_accessUnitDuration;
118 uint16_t i_compositionUnitDuration;
120 uint64_t i_startDecodingTimeStamp;
121 uint64_t i_startCompositionTimeStamp;
123 } sl_config_descriptor_t;
130 vlc_bool_t b_streamDependenceFlag;
131 vlc_bool_t b_OCRStreamFlag;
132 uint8_t i_streamPriority;
136 uint16_t i_dependOn_es_id;
137 uint16_t i_OCR_es_id;
139 decoder_config_descriptor_t dec_descr;
140 sl_config_descriptor_t sl_descr;
141 } es_mpeg4_descriptor_t;
151 uint8_t i_ODProfileLevelIndication;
152 uint8_t i_sceneProfileLevelIndication;
153 uint8_t i_audioProfileLevelIndication;
154 uint8_t i_visualProfileLevelIndication;
155 uint8_t i_graphicsProfileLevelIndication;
157 es_mpeg4_descriptor_t es_descr[255];
166 dvbpsi_handle handle;
168 /* IOD stuff (mpeg4) */
169 iod_descriptor_t *iod;
179 es_mpeg4_descriptor_t *p_mpeg4desc;
188 int i_cc; /* countinuity counter */
190 /* PSI owner (ie PMT -> PAT, ES -> PMT */
201 /* how many TS packet we read at once */
212 vlc_bool_t b_es_id_pid;
215 vlc_bool_t b_udp_out;
216 int fd; /* udp socket */
220 static int Demux ( demux_t *p_demux );
221 static int Control( demux_t *p_demux, int i_query, va_list args );
224 static void PIDInit ( ts_pid_t *pid, vlc_bool_t b_psi, ts_psi_t *p_owner );
225 static void PIDClean( es_out_t *out, ts_pid_t *pid );
226 static int PIDFillFormat( ts_pid_t *pid, int i_stream_type );
228 static void PATCallBack( demux_t *, dvbpsi_pat_t * );
229 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt );
231 static inline int PIDGet( block_t *p )
233 return ( (p->p_buffer[1]&0x1f)<<8 )|p->p_buffer[2];
236 static vlc_bool_t GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk );
238 static iod_descriptor_t *IODNew( int , uint8_t * );
239 static void IODFree( iod_descriptor_t * );
242 /*****************************************************************************
244 *****************************************************************************/
245 static int Open( vlc_object_t *p_this )
247 demux_t *p_demux = (demux_t*)p_this;
259 if( ( i_peek = stream_Peek( p_demux->s, &p_peek, 189 ) ) < 1 )
261 msg_Err( p_demux, "cannot peek" );
265 /* Search first synch */
266 for( i_sync = 0; i_sync < i_peek; i_sync++ )
268 if( p_peek[i_sync] == 0x47 ) break;
270 if( i_sync >= i_peek )
272 if( strcmp( p_demux->psz_demux, "ts2" ) )
274 msg_Warn( p_demux, "TS module discarded" );
277 msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
279 if( strcmp( p_demux->psz_demux, "ts2" ) )
281 /* Check next 3 sync points */
282 i_peek = 188*3 + 1 + i_sync;
283 if( ( stream_Peek( p_demux->s, &p_peek, i_peek ) ) < i_peek )
285 msg_Err( p_demux, "cannot peek" );
288 if( p_peek[i_sync+ 188] != 0x47 || p_peek[i_sync+2*188] != 0x47 ||
289 p_peek[i_sync+3*188] != 0x47 )
291 msg_Warn( p_demux, "TS module discarded (lost sync)" );
296 /* Fill p_demux field */
297 p_demux->pf_demux = Demux;
298 p_demux->pf_control = Control;
299 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
300 memset( p_sys, 0, sizeof( demux_sys_t ) );
302 /* Init p_sys field */
303 for( i = 0; i < 8192; i++ )
305 ts_pid_t *pid = &p_sys->pid[i];
308 pid->b_seen = VLC_FALSE;
309 pid->b_valid = VLC_FALSE;
311 p_sys->b_udp_out = VLC_FALSE;
312 p_sys->i_ts_read = 50;
315 /* Init PAT handler */
316 pat = &p_sys->pid[0];
317 PIDInit( pat, VLC_TRUE, NULL );
318 pat->psi->handle = dvbpsi_AttachPAT( (dvbpsi_pat_callback)PATCallBack, p_demux );
325 var_Create( p_demux, "ts-es-id-pid", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
326 var_Get( p_demux, "ts-es-id-pid", &val );
327 p_sys->b_es_id_pid = val.b_bool,
329 var_Create( p_demux, "ts-out", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
330 var_Get( p_demux, "ts-out", &val );
331 if( val.psz_string && *val.psz_string )
334 char *psz = strchr( val.psz_string, ':' );
337 p_sys->b_udp_out = VLC_TRUE;
342 i_port = atoi( psz );
344 if( i_port <= 0 ) i_port = 1234;
345 msg_Dbg( p_demux, "resend ts to '%s:%d'", val.psz_string, i_port );
347 p_sys->fd = net_OpenUDP( p_demux, "", 0, val.psz_string, i_port );
350 msg_Err( p_demux, "failed to open udp socket, send disabled" );
351 p_sys->b_udp_out = VLC_FALSE;
355 var_Create( p_demux, "ts-out-mtu", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
356 var_Get( p_demux, "ts-out-mtu", &mtu );
357 p_sys->i_ts_read = mtu.i_int / 188;
358 if( p_sys->i_ts_read <= 0 )
360 p_sys->i_ts_read = 1500 / 188;
362 p_sys->buffer = malloc( 188 * p_sys->i_ts_read );
367 free( val.psz_string );
371 /* We handle description of an extra PMT */
372 var_Create( p_demux, "ts-extra-pmt", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
373 var_Get( p_demux, "ts-extra-pmt", &val );
374 if( val.psz_string && strchr( val.psz_string, '=' ) != NULL )
376 char *psz = val.psz_string;
377 int i_pid = strtol( psz, &psz, 0 );
379 if( i_pid >= 2 && i_pid < 8192 )
381 ts_pid_t *pmt = &p_sys->pid[i_pid];
383 msg_Dbg( p_demux, "extra pmt specified (pid=0x%x)", i_pid );
384 PIDInit( pmt, VLC_TRUE, NULL );
385 /* FIXME we should also ask for a number */
386 pmt->psi->handle = dvbpsi_AttachPMT( 1, (dvbpsi_pmt_callback)PMTCallBack, p_demux );
387 pmt->psi->i_number = 0; /* special one */
389 psz = strchr( psz, '=' ) + 1; /* can't failed */
392 char *psz_next = strchr( psz, ',' );
393 int i_pid, i_stream_type;
400 i_pid = strtol( psz, &psz, 0 );
403 i_stream_type = strtol( psz + 1, &psz, 0 );
404 if( i_pid >= 2 && i_pid < 8192 && !p_sys->pid[i_pid].b_valid )
406 ts_pid_t *pid = &p_sys->pid[i_pid];
408 PIDInit( pid, VLC_FALSE, pmt->psi);
409 if( pmt->psi->i_pid_pcr <= 0 )
411 pmt->psi->i_pid_pcr = i_pid;
413 PIDFillFormat( pid, i_stream_type);
414 if( pid->es->fmt.i_cat != UNKNOWN_ES )
416 if( p_sys->b_es_id_pid )
418 pid->es->fmt.i_id = i_pid;
420 msg_Dbg( p_demux, " * es pid=0x%x type=0x%x fcc=%4.4s", i_pid, i_stream_type, (char*)&pid->es->fmt.i_codec );
421 pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
431 free( val.psz_string );
434 var_Create( p_demux, "ts-csa-ck", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
435 var_Get( p_demux, "ts-csa-ck", &val );
436 if( val.psz_string && *val.psz_string )
438 char *psz = val.psz_string;
439 if( psz[0] == '0' && ( psz[1] == 'x' || psz[1] == 'X' ) )
443 if( strlen( psz ) != 16 )
445 msg_Warn( p_demux, "invalid csa ck (it must be 16 chars long)" );
449 uint64_t i_ck = strtoll( psz, NULL, 16 );
452 for( i = 0; i < 8; i++ )
454 ck[i] = ( i_ck >> ( 56 - 8*i) )&0xff;
457 msg_Dbg( p_demux, "using CSA scrambling with ck=%x:%x:%x:%x:%x:%x:%x:%x",
458 ck[0], ck[1], ck[2], ck[3], ck[4], ck[5], ck[6], ck[7] );
460 p_sys->csa = csa_New();
461 csa_SetCW( p_sys->csa, ck, ck );
466 free( val.psz_string );
473 /*****************************************************************************
475 *****************************************************************************/
476 static void Close( vlc_object_t *p_this )
478 demux_t *p_demux = (demux_t*)p_this;
479 demux_sys_t *p_sys = p_demux->p_sys;
483 msg_Dbg( p_demux, "pid list:" );
484 for( i = 0; i < 8192; i++ )
486 ts_pid_t *pid = &p_sys->pid[i];
488 if( pid->b_valid && pid->psi )
493 dvbpsi_DetachPAT( pid->psi->handle );
498 dvbpsi_DetachPMT( pid->psi->handle );
501 IODFree( pid->psi->iod );
507 else if( pid->b_valid && pid->es )
511 block_ChainRelease( pid->es->p_pes );
517 msg_Dbg( p_demux, " - pid[0x%x] seen", pid->i_pid );
521 if( p_sys->b_udp_out )
523 net_Close( p_sys->fd );
524 free( p_sys->buffer );
528 csa_Delete( p_sys->csa );
535 /*****************************************************************************
537 *****************************************************************************/
538 static int Demux( demux_t *p_demux )
540 demux_sys_t *p_sys = p_demux->p_sys;
543 /* We read at most 100 TS packet or until a frame is completed */
544 for( i_pkt = 0; i_pkt < p_sys->i_ts_read; i_pkt++ )
546 vlc_bool_t b_frame = VLC_FALSE;
550 /* Get a new TS packet */
551 if( ( p_pkt = stream_Block( p_demux->s, 188 ) ) == NULL )
553 msg_Dbg( p_demux, "eof ?" );
556 if( p_pkt->p_buffer[0] != 0x47 )
558 msg_Warn( p_demux, "lost synchro" );
559 block_Release( p_pkt );
562 while( !p_demux->b_die )
565 int i_peek = stream_Peek( p_demux->s, &p_peek, 1880 );
567 vlc_bool_t b_ok = VLC_FALSE;
571 msg_Dbg( p_demux, "eof ?" );
575 while( i_skip < i_peek - 188 )
577 if( p_peek[i_skip] == 0x47 && p_peek[i_skip+188] == 0x47 )
584 stream_Read( p_demux->s, NULL, i_skip );
585 msg_Dbg( p_demux, "%d bytes of garbage", i_skip );
591 if( ( p_pkt = stream_Block( p_demux->s, 188 ) ) == NULL )
593 msg_Dbg( p_demux, "eof ?" );
598 if( p_sys->b_udp_out )
600 memcpy( &p_sys->buffer[i_pkt*188], p_pkt->p_buffer, 188 );
603 /* Parse the TS packet */
604 p_pid = &p_sys->pid[PIDGet( p_pkt )];
610 dvbpsi_PushPacket( p_pid->psi->handle, p_pkt->p_buffer );
611 block_Release( p_pkt );
613 else if( !p_sys->b_udp_out )
615 b_frame = GatherPES( p_demux, p_pid, p_pkt );
619 block_Release( p_pkt );
626 msg_Dbg( p_demux, "pid[0x%x] unknown", p_pid->i_pid );
628 block_Release( p_pkt );
630 p_pid->b_seen = VLC_TRUE;
638 if( p_sys->b_udp_out )
640 /* Send the complete block */
641 net_Write( p_demux, p_sys->fd, p_sys->buffer, p_sys->i_ts_read * 188 );
647 /*****************************************************************************
649 *****************************************************************************/
650 static int Control( demux_t *p_demux, int i_query, va_list args )
652 /* demux_sys_t *p_sys = p_demux->p_sys; */
658 case DEMUX_GET_POSITION:
659 pf = (double*) va_arg( args, double* );
660 i64 = stream_Size( p_demux->s );
663 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
670 case DEMUX_SET_POSITION:
671 f = (double) va_arg( args, double );
672 i64 = stream_Size( p_demux->s );
674 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
675 if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) )
683 pi64 = (int64_t*)va_arg( args, int64_t * );
684 if( p_sys->i_time < 0 )
689 *pi64 = p_sys->i_time;
693 case DEMUX_GET_LENGTH:
694 pi64 = (int64_t*)va_arg( args, int64_t * );
695 if( p_sys->i_mux_rate > 0 )
697 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
706 pf = (double*)va_arg( args, double * );
707 *pf = (double)1000000.0 / (double)p_sys->i_pcr_inc;
715 static void PIDInit( ts_pid_t *pid, vlc_bool_t b_psi, ts_psi_t *p_owner )
717 pid->b_valid = VLC_TRUE;
719 pid->p_owner = p_owner;
723 pid->psi = malloc( sizeof( ts_psi_t ) );
726 pid->psi->i_version = -1;
727 pid->psi->i_number = -1;
728 pid->psi->i_pid_pcr = -1;
729 pid->psi->handle = NULL;
730 pid->psi->iod = NULL;
735 pid->es = malloc( sizeof( ts_es_t ) );
737 es_format_Init( &pid->es->fmt, UNKNOWN_ES, 0 );
739 pid->es->p_pes = NULL;
740 pid->es->p_mpeg4desc = NULL;
744 static void PIDClean( es_out_t *out, ts_pid_t *pid )
748 if( pid->psi->handle )
750 dvbpsi_DetachPMT( pid->psi->handle );
754 IODFree( pid->psi->iod );
762 es_out_Del( out, pid->es->id );
766 block_ChainRelease( pid->es->p_pes );
770 pid->b_valid = VLC_FALSE;
773 /****************************************************************************
775 ****************************************************************************/
776 static void ParsePES ( demux_t *p_demux, ts_pid_t *pid )
778 block_t *p_pes = pid->es->p_pes;
786 /* remove the pes from pid */
787 pid->es->p_pes = NULL;
789 /* FIXME find real max size */
790 i_max = block_ChainExtract( p_pes, header, 30 );
792 if( header[0] != 0 || header[1] != 0 || header[2] != 1 )
794 msg_Warn( p_demux, "invalid header [0x%x:%x:%x:%x]", header[0], header[1],header[2],header[3] );
795 block_ChainRelease( p_pes );
799 i_pes_size = (header[4] << 8)|header[5];
801 /* TODO check size */
805 case 0xBC: /* Program stream map */
806 case 0xBE: /* Padding */
807 case 0xBF: /* Private stream 2 */
810 case 0xFF: /* Program stream directory */
811 case 0xF2: /* DSMCC stream */
812 case 0xF8: /* ITU-T H.222.1 type E stream */
816 if( ( header[6]&0xC0 ) == 0x80 )
819 i_skip = header[8] + 9;
821 if( header[7]&0x80 ) /* has pts */
823 i_pts = ((mtime_t)(header[ 9]&0x0e ) << 29)|
824 (mtime_t)(header[10] << 22)|
825 ((mtime_t)(header[11]&0xfe) << 14)|
826 (mtime_t)(header[12] << 7)|
827 (mtime_t)(header[12] >> 1);
829 if( header[7]&0x40 ) /* has dts */
831 i_dts = ((mtime_t)(header[14]&0x0e ) << 29)|
832 (mtime_t)(header[15] << 22)|
833 ((mtime_t)(header[16]&0xfe) << 14)|
834 (mtime_t)(header[17] << 7)|
835 (mtime_t)(header[18] >> 1);
842 while( i_skip < 23 && header[i_skip] == 0xff )
848 msg_Err( p_demux, "too much MPEG-1 stuffing" );
849 block_ChainRelease( p_pes );
852 if( ( header[i_skip] & 0xC0 ) == 0x40 )
857 if( header[i_skip]&0x20 )
859 p_pes->i_pts = ((mtime_t)(header[i_skip]&0x0e ) << 29)|
860 (mtime_t)(header[i_skip+1] << 22)|
861 ((mtime_t)(header[i_skip+2]&0xfe) << 14)|
862 (mtime_t)(header[i_skip+3] << 7)|
863 (mtime_t)(header[i_skip+4] >> 1);
865 if( header[i_skip]&0x10 ) /* has dts */
867 p_pes->i_dts = ((mtime_t)(header[i_skip+5]&0x0e ) << 29)|
868 (mtime_t)(header[i_skip+6] << 22)|
869 ((mtime_t)(header[i_skip+7]&0xfe) << 14)|
870 (mtime_t)(header[i_skip+8] << 7)|
871 (mtime_t)(header[i_skip+9] >> 1);
887 if( pid->es->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', 'b' ) ||
888 pid->es->fmt.i_codec == VLC_FOURCC( 'd', 't', 's', 'b' ) )
892 else if( pid->es->fmt.i_codec == VLC_FOURCC( 'l', 'p', 'c', 'b' ) ||
893 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'p', 'u', 'b' ) ||
894 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'd', 'd', 'b' ) )
900 while( p_pes && i_skip > 0 )
902 if( p_pes->i_buffer <= i_skip )
904 block_t *p_next = p_pes->p_next;
906 i_skip -= p_pes->i_buffer;
907 block_Release( p_pes );
912 p_pes->i_buffer -= i_skip;
913 p_pes->p_buffer += i_skip;
921 p_pes->i_dts = i_dts * 100 / 9;
925 p_pes->i_pts = i_pts * 100 / 9;
928 /* For mpeg4/mscodec we first gather the packet -> will make ffmpeg happier */
929 es_out_Send( p_demux->out, pid->es->id, block_ChainGather( p_pes ) );
933 msg_Warn( p_demux, "empty pes" );
937 static vlc_bool_t GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
939 const uint8_t *p = p_bk->p_buffer;
940 const vlc_bool_t b_adaptation= p[3]&0x20;
941 const vlc_bool_t b_payload = p[3]&0x10;
942 const int i_cc = p[3]&0x0f; /* continuity counter */
943 /* transport_scrambling_control is ignored */
946 vlc_bool_t i_ret = VLC_FALSE;
950 //msg_Dbg( p_demux, "pid=0x%x unit_start=%d adaptation=%d payload=%d cc=0x%x", i_pid, b_unit_start, b_adaptation, b_payload, i_continuity_counter);
953 msg_Dbg( p_demux, "transport_error_indicator set (pid=0x%x)", pid->i_pid );
956 if( p_demux->p_sys->csa )
958 csa_Decrypt( p_demux->p_sys->csa, p_bk->p_buffer );
967 /* p[4] is adaptation length */
973 msg_Warn( p_demux, "discontinuity_indicator (pid=0x%x) ignored", pid->i_pid );
977 /* test continuity counter */
978 /* continuous when (one of this):
980 * diff == 0 and payload == 0
981 * diff == 0 and duplicate packet (playload != 0) <- do we should test the content ?
984 i_diff = ( i_cc - pid->i_cc )&0x0f;
985 if( b_payload && i_diff == 1 )
991 if( pid->i_cc == 0xff )
993 msg_Warn( p_demux, "first packet for pid=0x%x cc=0x%x", pid->i_pid, i_cc );
996 else if( i_diff != 0 )
998 /* FIXME what to do when discontinuity_indicator is set ? */
999 msg_Warn( p_demux, "discontinuity received 0x%x instead of 0x%x",
1000 i_cc, ( pid->i_cc + 1 )&0x0f );
1004 if( pid->es->p_pes )
1006 block_ChainRelease( pid->es->p_pes );
1007 pid->es->p_pes = NULL;
1013 (p[5] & 0x10) && p[4]>=7 && pid->p_owner && pid->p_owner->i_pid_pcr == pid->i_pid )
1015 mtime_t i_pcr; /* 33 bits */
1017 i_pcr = ( (mtime_t)p[6] << 25 ) |
1018 ( (mtime_t)p[7] << 17 ) |
1019 ( (mtime_t)p[8] << 9 ) |
1020 ( (mtime_t)p[9] << 1 ) |
1021 ( (mtime_t)p[10] >> 7 );
1023 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR, (int)pid->p_owner->i_number, (int64_t)(i_pcr * 100 / 9) );
1027 if( i_skip >= 188 || pid->es->id == NULL || p_demux->p_sys->b_udp_out )
1029 block_Release( p_bk );
1033 const vlc_bool_t b_unit_start= p[1]&0x40;
1035 /* we have to gather it */
1036 p_bk->p_buffer += i_skip;
1037 p_bk->i_buffer -= i_skip;
1041 if( pid->es->p_pes )
1043 ParsePES( p_demux, pid );
1047 pid->es->p_pes = p_bk;
1051 if( pid->es->p_pes == NULL )
1053 /* msg_Dbg( p_demux, "broken packet" ); */
1054 block_Release( p_bk );
1058 /* TODO check if when have gather enough packet to form a PES (ie read PES size)*/
1059 block_ChainAppend( &pid->es->p_pes, p_bk );
1067 static int PIDFillFormat( ts_pid_t *pid, int i_stream_type )
1069 es_format_t *fmt = &pid->es->fmt;
1071 switch( i_stream_type )
1073 case 0x01: /* MPEG-1 video */
1074 case 0x02: /* MPEG-2 video */
1075 case 0x80: /* MPEG-2 MOTO video */
1076 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', 'g', 'v' ) );
1078 case 0x03: /* MPEG-1 audio */
1079 case 0x04: /* MPEG-2 audio */
1080 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
1082 case 0x11: /* MPEG4 (audio) */
1083 case 0x0f: /* ISO/IEC 13818-7 Audio with ADTS transport syntax */
1084 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', '4', 'a' ) );
1086 case 0x10: /* MPEG4 (video) */
1087 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', '4', 'v' ) );
1089 case 0x1B: /* H264 <- check transport syntax/needed descriptor */
1090 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'h', '2', '6', '4' ) );
1093 case 0x81: /* A52 (audio) */
1094 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', ' ' ) );
1096 case 0x82: /* DVD_SPU (sub) */
1097 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', ' ' ) );
1099 case 0x83: /* LPCM (audio) */
1100 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'l', 'p', 'c', 'm' ) );
1102 case 0x84: /* SDDS (audio) */
1103 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 's' ) );
1105 case 0x85: /* DTS (audio) */
1106 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'd', 't', 's', ' ' ) );
1109 case 0x91: /* A52 vls (audio) */
1110 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', 'b' ) );
1112 case 0x92: /* DVD_SPU vls (sub) */
1113 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', 'b' ) );
1115 case 0x93: /* LPCM vls (audio) */
1116 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'l', 'p', 'c', 'b' ) );
1118 case 0x94: /* SDDS (audio) */
1119 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 'b' ) );
1122 case 0x06: /* PES_PRIVATE (fixed later) */
1123 case 0xa0: /* MSCODEC vlc (video) (fixed later) */
1125 es_format_Init( fmt, UNKNOWN_ES, 0 );
1129 return fmt->i_cat == UNKNOWN_ES ? VLC_EGENERIC : VLC_SUCCESS ;
1132 /*****************************************************************************
1133 * MP4 specific functions (IOD parser)
1134 *****************************************************************************/
1135 static int IODDescriptorLength( int *pi_data, uint8_t **pp_data )
1138 unsigned int i_len = 0;
1144 i_len = ( i_len << 7 ) + ( i_b&0x7f );
1146 } while( i_b&0x80 );
1150 static int IODGetByte( int *pi_data, uint8_t **pp_data )
1154 const int i_b = **pp_data;
1161 static int IODGetWord( int *pi_data, uint8_t **pp_data )
1163 const int i1 = IODGetByte( pi_data, pp_data );
1164 const int i2 = IODGetByte( pi_data, pp_data );
1165 return( ( i1 << 8 ) | i2 );
1167 static int IODGet3Bytes( int *pi_data, uint8_t **pp_data )
1169 const int i1 = IODGetByte( pi_data, pp_data );
1170 const int i2 = IODGetByte( pi_data, pp_data );
1171 const int i3 = IODGetByte( pi_data, pp_data );
1173 return( ( i1 << 16 ) | ( i2 << 8) | i3 );
1176 static uint32_t IODGetDWord( int *pi_data, uint8_t **pp_data )
1178 const uint32_t i1 = IODGetWord( pi_data, pp_data );
1179 const uint32_t i2 = IODGetWord( pi_data, pp_data );
1180 return( ( i1 << 16 ) | i2 );
1183 static char* IODGetURL( int *pi_data, uint8_t **pp_data )
1188 i_url_len = IODGetByte( pi_data, pp_data );
1189 url = malloc( i_url_len + 1 );
1190 for( i = 0; i < i_url_len; i++ )
1192 url[i] = IODGetByte( pi_data, pp_data );
1194 url[i_url_len] = '\0';
1198 static iod_descriptor_t *IODNew( int i_data, uint8_t *p_data )
1200 iod_descriptor_t *p_iod;
1207 p_iod = malloc( sizeof( iod_descriptor_t ) );
1208 memset( p_iod, 0, sizeof( iod_descriptor_t ) );
1210 fprintf( stderr, "\n************ IOD ************" );
1211 for( i = 0; i < 255; i++ )
1213 p_iod->es_descr[i].b_ok = 0;
1222 p_iod->i_iod_label = IODGetByte( &i_data, &p_data );
1223 fprintf( stderr, "\n* iod_label:%d", p_iod->i_iod_label );
1224 fprintf( stderr, "\n* ===========" );
1225 fprintf( stderr, "\n* tag:0x%x", p_data[0] );
1227 if( IODGetByte( &i_data, &p_data ) != 0x02 )
1229 fprintf( stderr, "\n ERR: tag != 0x02" );
1233 i_iod_length = IODDescriptorLength( &i_data, &p_data );
1234 fprintf( stderr, "\n* length:%d", i_iod_length );
1235 if( i_iod_length > i_data )
1237 i_iod_length = i_data;
1240 p_iod->i_od_id = ( IODGetByte( &i_data, &p_data ) << 2 );
1241 i_flags = IODGetByte( &i_data, &p_data );
1242 p_iod->i_od_id |= i_flags >> 6;
1243 b_url = ( i_flags >> 5 )&0x01;
1245 fprintf( stderr, "\n* od_id:%d", p_iod->i_od_id );
1246 fprintf( stderr, "\n* url flag:%d", b_url );
1247 fprintf( stderr, "\n* includeInlineProfileLevel flag:%d", ( i_flags >> 4 )&0x01 );
1251 p_iod->psz_url = IODGetURL( &i_data, &p_data );
1252 fprintf( stderr, "\n* url string:%s", p_iod->psz_url );
1253 fprintf( stderr, "\n*****************************\n" );
1258 p_iod->psz_url = NULL;
1261 p_iod->i_ODProfileLevelIndication = IODGetByte( &i_data, &p_data );
1262 p_iod->i_sceneProfileLevelIndication = IODGetByte( &i_data, &p_data );
1263 p_iod->i_audioProfileLevelIndication = IODGetByte( &i_data, &p_data );
1264 p_iod->i_visualProfileLevelIndication = IODGetByte( &i_data, &p_data );
1265 p_iod->i_graphicsProfileLevelIndication = IODGetByte( &i_data, &p_data );
1267 fprintf( stderr, "\n* ODProfileLevelIndication:%d", p_iod->i_ODProfileLevelIndication );
1268 fprintf( stderr, "\n* sceneProfileLevelIndication:%d", p_iod->i_sceneProfileLevelIndication );
1269 fprintf( stderr, "\n* audioProfileLevelIndication:%d", p_iod->i_audioProfileLevelIndication );
1270 fprintf( stderr, "\n* visualProfileLevelIndication:%d", p_iod->i_visualProfileLevelIndication );
1271 fprintf( stderr, "\n* graphicsProfileLevelIndication:%d", p_iod->i_graphicsProfileLevelIndication );
1274 while( i_data > 0 && i_es_index < 255)
1276 int i_tag, i_length;
1278 uint8_t *p_data_sav;
1280 i_tag = IODGetByte( &i_data, &p_data );
1281 i_length = IODDescriptorLength( &i_data, &p_data );
1283 i_data_sav = i_data;
1284 p_data_sav = p_data;
1292 #define es_descr p_iod->es_descr[i_es_index]
1293 int i_decoderConfigDescr_length;
1294 fprintf( stderr, "\n* - ES_Descriptor length:%d", i_length );
1297 es_descr.i_es_id = IODGetWord( &i_data, &p_data );
1298 i_flags = IODGetByte( &i_data, &p_data );
1299 es_descr.b_streamDependenceFlag = ( i_flags >> 7 )&0x01;
1300 b_url = ( i_flags >> 6 )&0x01;
1301 es_descr.b_OCRStreamFlag = ( i_flags >> 5 )&0x01;
1302 es_descr.i_streamPriority = i_flags & 0x1f;
1303 fprintf( stderr, "\n* * streamDependenceFlag:%d", es_descr.b_streamDependenceFlag );
1304 fprintf( stderr, "\n* * OCRStreamFlag:%d", es_descr.b_OCRStreamFlag );
1305 fprintf( stderr, "\n* * streamPriority:%d", es_descr.i_streamPriority );
1307 if( es_descr.b_streamDependenceFlag )
1309 es_descr.i_dependOn_es_id = IODGetWord( &i_data, &p_data );
1310 fprintf( stderr, "\n* * dependOn_es_id:%d", es_descr.i_dependOn_es_id );
1315 es_descr.psz_url = IODGetURL( &i_data, &p_data );
1316 fprintf( stderr, "\n* url string:%s", es_descr.psz_url );
1320 es_descr.psz_url = NULL;
1323 if( es_descr.b_OCRStreamFlag )
1325 es_descr.i_OCR_es_id = IODGetWord( &i_data, &p_data );
1326 fprintf( stderr, "\n* * OCR_es_id:%d", es_descr.i_OCR_es_id );
1329 if( IODGetByte( &i_data, &p_data ) != 0x04 )
1331 fprintf( stderr, "\n* ERR missing DecoderConfigDescr" );
1335 i_decoderConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
1337 fprintf( stderr, "\n* - DecoderConfigDesc length:%d", i_decoderConfigDescr_length );
1338 #define dec_descr es_descr.dec_descr
1339 dec_descr.i_objectTypeIndication = IODGetByte( &i_data, &p_data );
1340 i_flags = IODGetByte( &i_data, &p_data );
1341 dec_descr.i_streamType = i_flags >> 2;
1342 dec_descr.b_upStream = ( i_flags >> 1 )&0x01;
1343 dec_descr.i_bufferSizeDB = IODGet3Bytes( &i_data, &p_data );
1344 dec_descr.i_maxBitrate = IODGetDWord( &i_data, &p_data );
1345 dec_descr.i_avgBitrate = IODGetDWord( &i_data, &p_data );
1346 fprintf( stderr, "\n* * objectTypeIndication:0x%x", dec_descr.i_objectTypeIndication );
1347 fprintf( stderr, "\n* * streamType:0x%x", dec_descr.i_streamType );
1348 fprintf( stderr, "\n* * upStream:%d", dec_descr.b_upStream );
1349 fprintf( stderr, "\n* * bufferSizeDB:%d", dec_descr.i_bufferSizeDB );
1350 fprintf( stderr, "\n* * maxBitrate:%d", dec_descr.i_maxBitrate );
1351 fprintf( stderr, "\n* * avgBitrate:%d", dec_descr.i_avgBitrate );
1352 if( i_decoderConfigDescr_length > 13 && IODGetByte( &i_data, &p_data ) == 0x05 )
1355 dec_descr.i_decoder_specific_info_len =
1356 IODDescriptorLength( &i_data, &p_data );
1357 if( dec_descr.i_decoder_specific_info_len > 0 )
1359 dec_descr.p_decoder_specific_info =
1360 malloc( dec_descr.i_decoder_specific_info_len );
1362 for( i = 0; i < dec_descr.i_decoder_specific_info_len; i++ )
1364 dec_descr.p_decoder_specific_info[i] = IODGetByte( &i_data, &p_data );
1369 dec_descr.i_decoder_specific_info_len = 0;
1370 dec_descr.p_decoder_specific_info = NULL;
1374 #define sl_descr es_descr.sl_descr
1376 int i_SLConfigDescr_length;
1379 if( IODGetByte( &i_data, &p_data ) != 0x06 )
1381 fprintf( stderr, "\n* ERR missing SLConfigDescr" );
1385 i_SLConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
1387 fprintf( stderr, "\n* - SLConfigDescr length:%d", i_SLConfigDescr_length );
1388 i_predefined = IODGetByte( &i_data, &p_data );
1389 fprintf( stderr, "\n* * i_predefined:0x%x", i_predefined );
1390 switch( i_predefined )
1394 sl_descr.b_useAccessUnitStartFlag = 0;
1395 sl_descr.b_useAccessUnitEndFlag = 0;
1396 sl_descr.b_useRandomAccessPointFlag = 0;
1397 //sl_descr.b_useRandomAccessUnitsOnlyFlag = 0;
1398 sl_descr.b_usePaddingFlag = 0;
1399 sl_descr.b_useTimeStampsFlags = 0;
1400 sl_descr.b_useIdleFlag = 0;
1401 sl_descr.b_durationFlag = 0; // FIXME FIXME
1402 sl_descr.i_timeStampResolution = 1000;
1403 sl_descr.i_OCRResolution = 0; // FIXME FIXME
1404 sl_descr.i_timeStampLength = 32;
1405 sl_descr.i_OCRLength = 0; // FIXME FIXME
1406 sl_descr.i_AU_Length = 0;
1407 sl_descr.i_instantBitrateLength= 0; // FIXME FIXME
1408 sl_descr.i_degradationPriorityLength= 0;
1409 sl_descr.i_AU_seqNumLength = 0;
1410 sl_descr.i_packetSeqNumLength = 0;
1411 if( sl_descr.b_durationFlag )
1413 sl_descr.i_timeScale = 0; // FIXME FIXME
1414 sl_descr.i_accessUnitDuration = 0; // FIXME FIXME
1415 sl_descr.i_compositionUnitDuration= 0; // FIXME FIXME
1417 if( !sl_descr.b_useTimeStampsFlags )
1419 sl_descr.i_startDecodingTimeStamp = 0; // FIXME FIXME
1420 sl_descr.i_startCompositionTimeStamp= 0; // FIXME FIXME
1425 fprintf( stderr, "\n* ERR unsupported SLConfigDescr predefined" );
1434 fprintf( stderr, "\n* - OD tag:0x%x length:%d (Unsupported)", i_tag, i_length );
1438 p_data = p_data_sav + i_length;
1439 i_data = i_data_sav - i_length;
1444 fprintf( stderr, "\n*****************************\n" );
1448 static void IODFree( iod_descriptor_t *p_iod )
1452 if( p_iod->psz_url )
1454 free( p_iod->psz_url );
1455 p_iod->psz_url = NULL;
1460 for( i = 0; i < 255; i++ )
1462 #define es_descr p_iod->es_descr[i]
1465 if( es_descr.psz_url )
1467 free( es_descr.psz_url );
1468 es_descr.psz_url = NULL;
1472 if( es_descr.dec_descr.p_decoder_specific_info != NULL )
1474 free( es_descr.dec_descr.p_decoder_specific_info );
1475 es_descr.dec_descr.p_decoder_specific_info = NULL;
1476 es_descr.dec_descr.i_decoder_specific_info_len = 0;
1486 /****************************************************************************
1487 ****************************************************************************
1488 ** libdvbpsi callbacks
1489 ****************************************************************************
1490 ****************************************************************************/
1491 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt )
1493 demux_sys_t *p_sys = p_demux->p_sys;
1494 dvbpsi_descriptor_t *p_dr;
1495 dvbpsi_pmt_es_t *p_es;
1497 ts_pid_t *pmt = NULL;
1500 msg_Dbg( p_demux, "PMTCallBack called" );
1501 /* First find this PMT declared in PAT */
1502 for( i = 0; i < p_sys->i_pmt; i++ )
1504 if( p_sys->pmt[i]->psi->i_number == p_pmt->i_program_number )
1506 pmt = p_sys->pmt[i];
1511 msg_Warn( p_demux, "unreferenced program (broken stream)" );
1512 dvbpsi_DeletePMT(p_pmt);
1516 if( pmt->psi->i_version != -1 && ( !p_pmt->b_current_next || pmt->psi->i_version == p_pmt->i_version ) )
1518 dvbpsi_DeletePMT( p_pmt );
1522 /* Clean this program (remove all es) */
1523 for( i = 0; i < 8192; i++ )
1525 ts_pid_t *pid = &p_sys->pid[i];
1527 if( pid->b_valid && pid->p_owner == pmt->psi && pid->psi == NULL )
1529 PIDClean( p_demux->out, pid );
1534 IODFree( pmt->psi->iod );
1535 pmt->psi->iod = NULL;
1538 msg_Dbg( p_demux, "new PMT program number=%d version=%d pid_pcr=0x%x", p_pmt->i_program_number, p_pmt->i_version, p_pmt->i_pcr_pid );
1539 pmt->psi->i_pid_pcr = p_pmt->i_pcr_pid;
1540 pmt->psi->i_version = p_pmt->i_version;
1542 /* Parse descriptor */
1543 for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
1545 if( p_dr->i_tag == 0x1d )
1547 /* We have found an IOD descriptor */
1548 msg_Warn( p_demux, "found IOD descriptor" );
1550 pmt->psi->iod = IODNew( p_dr->i_length, p_dr->p_data );
1554 for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
1556 ts_pid_t *pid = &p_sys->pid[p_es->i_pid];
1560 msg_Warn( p_demux, "pmt error: pid=0x%x already defined", p_es->i_pid );
1564 PIDInit( pid, VLC_FALSE, pmt->psi );
1565 PIDFillFormat( pid, p_es->i_type );
1567 if( p_es->i_type == 0x10 || p_es->i_type == 0x11 )
1569 /* MPEG-4 stream: search SL_DESCRIPTOR */
1570 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
1572 while( p_dr && ( p_dr->i_tag != 0x1f ) ) p_dr = p_dr->p_next;
1574 if( p_dr && p_dr->i_length == 2 )
1577 int i_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
1579 msg_Warn( p_demux, "found SL_descriptor es_id=%d", i_es_id );
1581 pid->es->p_mpeg4desc = NULL;
1583 for( i = 0; i < 255; i++ )
1585 iod_descriptor_t *iod = pmt->psi->iod;
1587 if( iod->es_descr[i].b_ok &&
1588 iod->es_descr[i].i_es_id == i_es_id )
1590 pid->es->p_mpeg4desc = &iod->es_descr[i];
1596 if( pid->es->p_mpeg4desc != NULL )
1598 decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;
1600 if( dcd->i_streamType == 0x04 ) /* VisualStream */
1602 pid->es->fmt.i_cat = VIDEO_ES;
1603 switch( dcd->i_objectTypeIndication )
1606 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','v'); // mpeg4
1614 pid->es->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' ); // mpeg2
1617 pid->es->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' ); // mpeg1
1620 pid->es->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' ); // mpeg1
1624 pid->es->fmt.i_cat = UNKNOWN_ES;
1628 else if( dcd->i_streamType == 0x05 ) /* AudioStream */
1630 pid->es->fmt.i_cat = AUDIO_ES;
1631 switch( dcd->i_objectTypeIndication )
1634 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','a'); // mpeg4
1639 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','a');// mpeg2 aac
1642 pid->es->fmt.i_codec = VLC_FOURCC('m','p','g','a'); // mpeg2
1645 pid->es->fmt.i_codec = VLC_FOURCC('m','p','g','a'); // mpeg1
1648 pid->es->fmt.i_cat = UNKNOWN_ES;
1654 pid->es->fmt.i_cat = UNKNOWN_ES;
1657 if( pid->es->fmt.i_cat != UNKNOWN_ES )
1659 pid->es->fmt.i_extra = dcd->i_decoder_specific_info_len;
1660 if( pid->es->fmt.i_extra > 0 )
1662 pid->es->fmt.p_extra = malloc( pid->es->fmt.i_extra );
1663 memcpy( pid->es->fmt.p_extra,
1664 dcd->p_decoder_specific_info,
1665 pid->es->fmt.i_extra );
1670 else if( p_es->i_type == 0x06 )
1672 dvbpsi_descriptor_t *p_dr;
1674 for( p_dr = p_es->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
1676 if( p_dr->i_tag == 0x6a )
1678 pid->es->fmt.i_cat = AUDIO_ES;
1679 pid->es->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
1681 #ifdef _DVBPSI_DR_59_H_
1682 else if( p_dr->i_tag == 0x59 )
1690 else if( p_es->i_type == 0xa0 )
1692 /* MSCODEC send by vlc */
1693 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
1695 while( p_dr && ( p_dr->i_tag != 0xa0 ) ) p_dr = p_dr->p_next;
1697 if( p_dr && p_dr->i_length >= 8 )
1699 pid->es->fmt.i_cat = VIDEO_ES;
1700 pid->es->fmt.i_codec = VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
1701 p_dr->p_data[2], p_dr->p_data[3] );
1702 pid->es->fmt.video.i_width = ( p_dr->p_data[4] << 8 )|p_dr->p_data[5];
1703 pid->es->fmt.video.i_height= ( p_dr->p_data[6] << 8 )|p_dr->p_data[7];
1704 pid->es->fmt.i_extra = (p_dr->p_data[8] << 8) | p_dr->p_data[9];
1706 if( pid->es->fmt.i_extra > 0 )
1708 pid->es->fmt.p_extra = malloc( pid->es->fmt.i_extra );
1709 memcpy( pid->es->fmt.p_extra, &p_dr->p_data[10], pid->es->fmt.i_extra );
1714 msg_Warn( p_demux, "private MSCODEC (vlc) without bih private descriptor" );
1718 if( pid->es->fmt.i_cat == AUDIO_ES || pid->es->fmt.i_cat == SPU_ES )
1720 /* get language descriptor */
1721 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
1722 while( p_dr && ( p_dr->i_tag != 0x0a ) ) p_dr = p_dr->p_next;
1726 dvbpsi_iso639_dr_t *p_decoded = dvbpsi_DecodeISO639Dr( p_dr );
1730 pid->es->fmt.psz_language = malloc( 4 );
1731 memcpy( pid->es->fmt.psz_language, p_decoded->i_iso_639_code, 3 );
1732 pid->es->fmt.psz_language[3] = 0;
1737 pid->es->fmt.i_group = p_pmt->i_program_number;
1738 if( pid->es->fmt.i_cat == UNKNOWN_ES )
1740 msg_Dbg( p_demux, " * es pid=0x%x type=0x%x *unknown*", p_es->i_pid, p_es->i_type );
1742 else if( !p_sys->b_udp_out )
1744 msg_Dbg( p_demux, " * es pid=0x%x type=0x%x fcc=%4.4s", p_es->i_pid, p_es->i_type, (char*)&pid->es->fmt.i_codec );
1745 if( p_sys->b_es_id_pid )
1747 pid->es->fmt.i_id = p_es->i_pid;
1749 pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
1752 dvbpsi_DeletePMT(p_pmt);
1755 static void PATCallBack( demux_t *p_demux, dvbpsi_pat_t *p_pat )
1757 demux_sys_t *p_sys = p_demux->p_sys;
1758 dvbpsi_pat_program_t *p_program;
1759 ts_pid_t *pat = &p_sys->pid[0];
1762 msg_Dbg( p_demux, "PATCallBack called" );
1764 if( pat->psi->i_version != -1 && ( !p_pat->b_current_next || p_pat->i_version == pat->psi->i_version ) )
1766 dvbpsi_DeletePAT( p_pat );
1770 msg_Dbg( p_demux, "new PAT ts_id=0x%x version=%d current_next=%d", p_pat->i_ts_id, p_pat->i_version, p_pat->b_current_next );
1773 for( i = 2; i < 8192; i++ )
1775 ts_pid_t *pid = &p_sys->pid[i];
1781 if( pid->p_owner == pat->psi )
1783 PIDClean( p_demux->out, pid );
1784 TAB_REMOVE( p_sys->i_pmt, p_sys->pmt, pid );
1787 else if( pid->p_owner && pid->p_owner->i_number != 0 && pid->es->id )
1789 /* We only remove es that aren't defined by extra pmt */
1790 PIDClean( p_demux->out, pid );
1795 /* now create programs */
1796 for( p_program = p_pat->p_first_program; p_program != NULL; p_program = p_program->p_next )
1798 msg_Dbg( p_demux, " * number=%d pid=0x%x", p_program->i_number, p_program->i_pid );
1799 if( p_program->i_number != 0 )
1801 ts_pid_t *pmt = &p_sys->pid[p_program->i_pid];
1803 PIDInit( pmt, VLC_TRUE, pat->psi );
1804 pmt->psi->handle = dvbpsi_AttachPMT( p_program->i_number, (dvbpsi_pmt_callback)PMTCallBack, p_demux );
1805 pmt->psi->i_number = p_program->i_number;
1807 TAB_APPEND( p_sys->i_pmt, p_sys->pmt, pmt );
1810 pat->psi->i_version = p_pat->i_version;
1812 dvbpsi_DeletePAT( p_pat );