1 /*****************************************************************************
2 * ts.c: Transport Stream input module for VLC.
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
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 void PCRHandle( demux_t *p_demux, ts_pid_t *, block_t * );
240 static iod_descriptor_t *IODNew( int , uint8_t * );
241 static void IODFree( iod_descriptor_t * );
244 /*****************************************************************************
246 *****************************************************************************/
247 static int Open( vlc_object_t *p_this )
249 demux_t *p_demux = (demux_t*)p_this;
261 if( ( i_peek = stream_Peek( p_demux->s, &p_peek, 189 ) ) < 1 )
263 msg_Err( p_demux, "cannot peek" );
267 /* Search first synch */
268 for( i_sync = 0; i_sync < i_peek; i_sync++ )
270 if( p_peek[i_sync] == 0x47 ) break;
272 if( i_sync >= i_peek )
274 if( strcmp( p_demux->psz_demux, "ts2" ) )
276 msg_Warn( p_demux, "TS module discarded" );
279 msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
281 if( strcmp( p_demux->psz_demux, "ts2" ) )
283 /* Check next 3 sync points */
284 i_peek = 188*3 + 1 + i_sync;
285 if( ( stream_Peek( p_demux->s, &p_peek, i_peek ) ) < i_peek )
287 msg_Err( p_demux, "cannot peek" );
290 if( p_peek[i_sync+ 188] != 0x47 || p_peek[i_sync+2*188] != 0x47 ||
291 p_peek[i_sync+3*188] != 0x47 )
293 msg_Warn( p_demux, "TS module discarded (lost sync)" );
298 /* Fill p_demux field */
299 p_demux->pf_demux = Demux;
300 p_demux->pf_control = Control;
301 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
302 memset( p_sys, 0, sizeof( demux_sys_t ) );
304 /* Init p_sys field */
305 for( i = 0; i < 8192; i++ )
307 ts_pid_t *pid = &p_sys->pid[i];
310 pid->b_seen = VLC_FALSE;
311 pid->b_valid = VLC_FALSE;
313 p_sys->b_udp_out = VLC_FALSE;
314 p_sys->i_ts_read = 50;
317 /* Init PAT handler */
318 pat = &p_sys->pid[0];
319 PIDInit( pat, VLC_TRUE, NULL );
320 pat->psi->handle = dvbpsi_AttachPAT( (dvbpsi_pat_callback)PATCallBack, p_demux );
327 var_Create( p_demux, "ts-es-id-pid", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
328 var_Get( p_demux, "ts-es-id-pid", &val );
329 p_sys->b_es_id_pid = val.b_bool,
331 var_Create( p_demux, "ts-out", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
332 var_Get( p_demux, "ts-out", &val );
333 if( val.psz_string && *val.psz_string )
336 char *psz = strchr( val.psz_string, ':' );
339 p_sys->b_udp_out = VLC_TRUE;
344 i_port = atoi( psz );
346 if( i_port <= 0 ) i_port = 1234;
347 msg_Dbg( p_demux, "resend ts to '%s:%d'", val.psz_string, i_port );
349 p_sys->fd = net_OpenUDP( p_demux, "", 0, val.psz_string, i_port );
352 msg_Err( p_demux, "failed to open udp socket, send disabled" );
353 p_sys->b_udp_out = VLC_FALSE;
357 var_Create( p_demux, "ts-out-mtu", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
358 var_Get( p_demux, "ts-out-mtu", &mtu );
359 p_sys->i_ts_read = mtu.i_int / 188;
360 if( p_sys->i_ts_read <= 0 )
362 p_sys->i_ts_read = 1500 / 188;
364 p_sys->buffer = malloc( 188 * p_sys->i_ts_read );
369 free( val.psz_string );
373 /* We handle description of an extra PMT */
374 var_Create( p_demux, "ts-extra-pmt", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
375 var_Get( p_demux, "ts-extra-pmt", &val );
376 if( val.psz_string && strchr( val.psz_string, '=' ) != NULL )
378 char *psz = val.psz_string;
379 int i_pid = strtol( psz, &psz, 0 );
381 if( i_pid >= 2 && i_pid < 8192 )
383 ts_pid_t *pmt = &p_sys->pid[i_pid];
385 msg_Dbg( p_demux, "extra pmt specified (pid=0x%x)", i_pid );
386 PIDInit( pmt, VLC_TRUE, NULL );
387 /* FIXME we should also ask for a number */
388 pmt->psi->handle = dvbpsi_AttachPMT( 1, (dvbpsi_pmt_callback)PMTCallBack, p_demux );
389 pmt->psi->i_number = 0; /* special one */
391 psz = strchr( psz, '=' ) + 1; /* can't failed */
394 char *psz_next = strchr( psz, ',' );
395 int i_pid, i_stream_type;
402 i_pid = strtol( psz, &psz, 0 );
405 i_stream_type = strtol( psz + 1, &psz, 0 );
406 if( i_pid >= 2 && i_pid < 8192 && !p_sys->pid[i_pid].b_valid )
408 ts_pid_t *pid = &p_sys->pid[i_pid];
410 PIDInit( pid, VLC_FALSE, pmt->psi);
411 if( pmt->psi->i_pid_pcr <= 0 )
413 pmt->psi->i_pid_pcr = i_pid;
415 PIDFillFormat( pid, i_stream_type);
416 if( pid->es->fmt.i_cat != UNKNOWN_ES )
418 if( p_sys->b_es_id_pid )
420 pid->es->fmt.i_id = i_pid;
422 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 );
423 pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
433 free( val.psz_string );
436 var_Create( p_demux, "ts-csa-ck", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
437 var_Get( p_demux, "ts-csa-ck", &val );
438 if( val.psz_string && *val.psz_string )
440 char *psz = val.psz_string;
441 if( psz[0] == '0' && ( psz[1] == 'x' || psz[1] == 'X' ) )
445 if( strlen( psz ) != 16 )
447 msg_Warn( p_demux, "invalid csa ck (it must be 16 chars long)" );
451 uint64_t i_ck = strtoll( psz, NULL, 16 );
454 for( i = 0; i < 8; i++ )
456 ck[i] = ( i_ck >> ( 56 - 8*i) )&0xff;
459 msg_Dbg( p_demux, "using CSA scrambling with ck=%x:%x:%x:%x:%x:%x:%x:%x",
460 ck[0], ck[1], ck[2], ck[3], ck[4], ck[5], ck[6], ck[7] );
462 p_sys->csa = csa_New();
463 csa_SetCW( p_sys->csa, ck, ck );
468 free( val.psz_string );
475 /*****************************************************************************
477 *****************************************************************************/
478 static void Close( vlc_object_t *p_this )
480 demux_t *p_demux = (demux_t*)p_this;
481 demux_sys_t *p_sys = p_demux->p_sys;
485 msg_Dbg( p_demux, "pid list:" );
486 for( i = 0; i < 8192; i++ )
488 ts_pid_t *pid = &p_sys->pid[i];
490 if( pid->b_valid && pid->psi )
495 dvbpsi_DetachPAT( pid->psi->handle );
500 dvbpsi_DetachPMT( pid->psi->handle );
503 IODFree( pid->psi->iod );
509 else if( pid->b_valid && pid->es )
513 block_ChainRelease( pid->es->p_pes );
519 msg_Dbg( p_demux, " - pid[0x%x] seen", pid->i_pid );
523 if( p_sys->b_udp_out )
525 net_Close( p_sys->fd );
526 free( p_sys->buffer );
530 csa_Delete( p_sys->csa );
533 if( p_sys->i_pmt ) free( p_sys->pmt );
537 /*****************************************************************************
539 *****************************************************************************/
540 static int Demux( demux_t *p_demux )
542 demux_sys_t *p_sys = p_demux->p_sys;
545 /* We read at most 100 TS packet or until a frame is completed */
546 for( i_pkt = 0; i_pkt < p_sys->i_ts_read; i_pkt++ )
548 vlc_bool_t b_frame = VLC_FALSE;
552 /* Get a new TS packet */
553 if( ( p_pkt = stream_Block( p_demux->s, 188 ) ) == NULL )
555 msg_Dbg( p_demux, "eof ?" );
558 if( p_pkt->p_buffer[0] != 0x47 )
560 msg_Warn( p_demux, "lost synchro" );
561 block_Release( p_pkt );
564 while( !p_demux->b_die )
567 int i_peek = stream_Peek( p_demux->s, &p_peek, 1880 );
569 vlc_bool_t b_ok = VLC_FALSE;
573 msg_Dbg( p_demux, "eof ?" );
577 while( i_skip < i_peek - 188 )
579 if( p_peek[i_skip] == 0x47 && p_peek[i_skip+188] == 0x47 )
586 stream_Read( p_demux->s, NULL, i_skip );
587 msg_Dbg( p_demux, "%d bytes of garbage", i_skip );
593 if( ( p_pkt = stream_Block( p_demux->s, 188 ) ) == NULL )
595 msg_Dbg( p_demux, "eof ?" );
600 if( p_sys->b_udp_out )
602 memcpy( &p_sys->buffer[i_pkt*188], p_pkt->p_buffer, 188 );
605 /* Parse the TS packet */
606 p_pid = &p_sys->pid[PIDGet( p_pkt )];
612 dvbpsi_PushPacket( p_pid->psi->handle, p_pkt->p_buffer );
613 block_Release( p_pkt );
615 else if( !p_sys->b_udp_out )
617 b_frame = GatherPES( p_demux, p_pid, p_pkt );
621 block_Release( p_pkt );
628 msg_Dbg( p_demux, "pid[0x%x] unknown", p_pid->i_pid );
630 /* We have to handle PCR if present */
631 PCRHandle( p_demux, p_pid, p_pkt );
632 block_Release( p_pkt );
634 p_pid->b_seen = VLC_TRUE;
642 if( p_sys->b_udp_out )
644 /* Send the complete block */
645 net_Write( p_demux, p_sys->fd, p_sys->buffer, p_sys->i_ts_read * 188 );
651 /*****************************************************************************
653 *****************************************************************************/
654 static int Control( demux_t *p_demux, int i_query, va_list args )
656 /* demux_sys_t *p_sys = p_demux->p_sys; */
662 case DEMUX_GET_POSITION:
663 pf = (double*) va_arg( args, double* );
664 i64 = stream_Size( p_demux->s );
667 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
674 case DEMUX_SET_POSITION:
675 f = (double) va_arg( args, double );
676 i64 = stream_Size( p_demux->s );
678 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
679 if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) )
687 pi64 = (int64_t*)va_arg( args, int64_t * );
688 if( p_sys->i_time < 0 )
693 *pi64 = p_sys->i_time;
696 case DEMUX_GET_LENGTH:
697 pi64 = (int64_t*)va_arg( args, int64_t * );
698 if( p_sys->i_mux_rate > 0 )
700 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
713 static void PIDInit( ts_pid_t *pid, vlc_bool_t b_psi, ts_psi_t *p_owner )
715 pid->b_valid = VLC_TRUE;
717 pid->p_owner = p_owner;
721 pid->psi = malloc( sizeof( ts_psi_t ) );
724 pid->psi->i_version = -1;
725 pid->psi->i_number = -1;
726 pid->psi->i_pid_pcr = -1;
727 pid->psi->handle = NULL;
728 pid->psi->iod = NULL;
733 pid->es = malloc( sizeof( ts_es_t ) );
735 es_format_Init( &pid->es->fmt, UNKNOWN_ES, 0 );
737 pid->es->p_pes = NULL;
738 pid->es->p_mpeg4desc = NULL;
742 static void PIDClean( es_out_t *out, ts_pid_t *pid )
746 if( pid->psi->handle )
748 dvbpsi_DetachPMT( pid->psi->handle );
752 IODFree( pid->psi->iod );
760 es_out_Del( out, pid->es->id );
764 block_ChainRelease( pid->es->p_pes );
768 pid->b_valid = VLC_FALSE;
771 /****************************************************************************
773 ****************************************************************************/
774 static void ParsePES ( demux_t *p_demux, ts_pid_t *pid )
776 block_t *p_pes = pid->es->p_pes;
784 /* remove the pes from pid */
785 pid->es->p_pes = NULL;
787 /* FIXME find real max size */
788 i_max = block_ChainExtract( p_pes, header, 30 );
790 if( header[0] != 0 || header[1] != 0 || header[2] != 1 )
792 msg_Warn( p_demux, "invalid header [0x%x:%x:%x:%x]", header[0], header[1],header[2],header[3] );
793 block_ChainRelease( p_pes );
797 i_pes_size = (header[4] << 8)|header[5];
799 /* TODO check size */
803 case 0xBC: /* Program stream map */
804 case 0xBE: /* Padding */
805 case 0xBF: /* Private stream 2 */
808 case 0xFF: /* Program stream directory */
809 case 0xF2: /* DSMCC stream */
810 case 0xF8: /* ITU-T H.222.1 type E stream */
814 if( ( header[6]&0xC0 ) == 0x80 )
817 i_skip = header[8] + 9;
819 if( header[7]&0x80 ) /* has pts */
821 i_pts = ((mtime_t)(header[ 9]&0x0e ) << 29)|
822 (mtime_t)(header[10] << 22)|
823 ((mtime_t)(header[11]&0xfe) << 14)|
824 (mtime_t)(header[12] << 7)|
825 (mtime_t)(header[12] >> 1);
827 if( header[7]&0x40 ) /* has dts */
829 i_dts = ((mtime_t)(header[14]&0x0e ) << 29)|
830 (mtime_t)(header[15] << 22)|
831 ((mtime_t)(header[16]&0xfe) << 14)|
832 (mtime_t)(header[17] << 7)|
833 (mtime_t)(header[18] >> 1);
840 while( i_skip < 23 && header[i_skip] == 0xff )
846 msg_Err( p_demux, "too much MPEG-1 stuffing" );
847 block_ChainRelease( p_pes );
850 if( ( header[i_skip] & 0xC0 ) == 0x40 )
855 if( header[i_skip]&0x20 )
857 p_pes->i_pts = ((mtime_t)(header[i_skip]&0x0e ) << 29)|
858 (mtime_t)(header[i_skip+1] << 22)|
859 ((mtime_t)(header[i_skip+2]&0xfe) << 14)|
860 (mtime_t)(header[i_skip+3] << 7)|
861 (mtime_t)(header[i_skip+4] >> 1);
863 if( header[i_skip]&0x10 ) /* has dts */
865 p_pes->i_dts = ((mtime_t)(header[i_skip+5]&0x0e ) << 29)|
866 (mtime_t)(header[i_skip+6] << 22)|
867 ((mtime_t)(header[i_skip+7]&0xfe) << 14)|
868 (mtime_t)(header[i_skip+8] << 7)|
869 (mtime_t)(header[i_skip+9] >> 1);
885 if( pid->es->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', 'b' ) ||
886 pid->es->fmt.i_codec == VLC_FOURCC( 'd', 't', 's', 'b' ) )
890 else if( pid->es->fmt.i_codec == VLC_FOURCC( 'l', 'p', 'c', 'b' ) ||
891 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'p', 'u', 'b' ) ||
892 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'd', 'd', 'b' ) )
898 while( p_pes && i_skip > 0 )
900 if( p_pes->i_buffer <= i_skip )
902 block_t *p_next = p_pes->p_next;
904 i_skip -= p_pes->i_buffer;
905 block_Release( p_pes );
910 p_pes->i_buffer -= i_skip;
911 p_pes->p_buffer += i_skip;
919 p_pes->i_dts = i_dts * 100 / 9;
923 p_pes->i_pts = i_pts * 100 / 9;
926 /* For mpeg4/mscodec we first gather the packet -> will make ffmpeg happier */
927 es_out_Send( p_demux->out, pid->es->id, block_ChainGather( p_pes ) );
931 msg_Warn( p_demux, "empty pes" );
935 static void PCRHandle( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
937 demux_sys_t *p_sys = p_demux->p_sys;
938 const uint8_t *p = p_bk->p_buffer;
940 if( ( p[3]&0x20 ) && /* adaptation */
945 mtime_t i_pcr; /* 33 bits */
947 i_pcr = ( (mtime_t)p[6] << 25 ) |
948 ( (mtime_t)p[7] << 17 ) |
949 ( (mtime_t)p[8] << 9 ) |
950 ( (mtime_t)p[9] << 1 ) |
951 ( (mtime_t)p[10] >> 7 );
953 /* Search program and set the PCR */
954 for( i = 0; i < p_sys->i_pmt; i++ )
956 if( pid->i_pid == p_sys->pmt[i]->psi->i_pid_pcr )
958 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
959 (int)p_sys->pmt[i]->psi->i_number,
960 (int64_t)(i_pcr * 100 / 9) );
966 static vlc_bool_t GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
968 const uint8_t *p = p_bk->p_buffer;
969 const vlc_bool_t b_adaptation= p[3]&0x20;
970 const vlc_bool_t b_payload = p[3]&0x10;
971 const int i_cc = p[3]&0x0f; /* continuity counter */
972 /* transport_scrambling_control is ignored */
975 vlc_bool_t i_ret = VLC_FALSE;
979 //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);
982 msg_Dbg( p_demux, "transport_error_indicator set (pid=0x%x)", pid->i_pid );
985 if( p_demux->p_sys->csa )
987 csa_Decrypt( p_demux->p_sys->csa, p_bk->p_buffer );
996 /* p[4] is adaptation length */
1002 msg_Warn( p_demux, "discontinuity_indicator (pid=0x%x) ignored", pid->i_pid );
1006 /* test continuity counter */
1007 /* continuous when (one of this):
1009 * diff == 0 and payload == 0
1010 * diff == 0 and duplicate packet (playload != 0) <- do we should test the content ?
1013 i_diff = ( i_cc - pid->i_cc )&0x0f;
1014 if( b_payload && i_diff == 1 )
1020 if( pid->i_cc == 0xff )
1022 msg_Warn( p_demux, "first packet for pid=0x%x cc=0x%x", pid->i_pid, i_cc );
1025 else if( i_diff != 0 )
1027 /* FIXME what to do when discontinuity_indicator is set ? */
1028 msg_Warn( p_demux, "discontinuity received 0x%x instead of 0x%x",
1029 i_cc, ( pid->i_cc + 1 )&0x0f );
1033 if( pid->es->p_pes )
1035 pid->es->p_pes->i_flags |= BLOCK_FLAG_DISCONTINUITY;
1040 PCRHandle( p_demux, pid, p_bk );
1042 if( i_skip >= 188 || pid->es->id == NULL || p_demux->p_sys->b_udp_out )
1044 block_Release( p_bk );
1048 const vlc_bool_t b_unit_start= p[1]&0x40;
1050 /* we have to gather it */
1051 p_bk->p_buffer += i_skip;
1052 p_bk->i_buffer -= i_skip;
1056 if( pid->es->p_pes )
1058 ParsePES( p_demux, pid );
1062 pid->es->p_pes = p_bk;
1066 if( pid->es->p_pes == NULL )
1068 /* msg_Dbg( p_demux, "broken packet" ); */
1069 block_Release( p_bk );
1073 /* TODO check if when have gather enough packet to form a PES (ie read PES size)*/
1074 block_ChainAppend( &pid->es->p_pes, p_bk );
1082 static int PIDFillFormat( ts_pid_t *pid, int i_stream_type )
1084 es_format_t *fmt = &pid->es->fmt;
1086 switch( i_stream_type )
1088 case 0x01: /* MPEG-1 video */
1089 case 0x02: /* MPEG-2 video */
1090 case 0x80: /* MPEG-2 MOTO video */
1091 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', 'g', 'v' ) );
1093 case 0x03: /* MPEG-1 audio */
1094 case 0x04: /* MPEG-2 audio */
1095 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
1097 case 0x11: /* MPEG4 (audio) */
1098 case 0x0f: /* ISO/IEC 13818-7 Audio with ADTS transport syntax */
1099 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', '4', 'a' ) );
1101 case 0x10: /* MPEG4 (video) */
1102 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', '4', 'v' ) );
1104 case 0x1B: /* H264 <- check transport syntax/needed descriptor */
1105 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'h', '2', '6', '4' ) );
1108 case 0x81: /* A52 (audio) */
1109 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', ' ' ) );
1111 case 0x82: /* DVD_SPU (sub) */
1112 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', ' ' ) );
1114 case 0x83: /* LPCM (audio) */
1115 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'l', 'p', 'c', 'm' ) );
1117 case 0x84: /* SDDS (audio) */
1118 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 's' ) );
1120 case 0x85: /* DTS (audio) */
1121 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'd', 't', 's', ' ' ) );
1124 case 0x91: /* A52 vls (audio) */
1125 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', 'b' ) );
1127 case 0x92: /* DVD_SPU vls (sub) */
1128 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', 'b' ) );
1130 case 0x93: /* LPCM vls (audio) */
1131 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'l', 'p', 'c', 'b' ) );
1133 case 0x94: /* SDDS (audio) */
1134 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 'b' ) );
1137 case 0x06: /* PES_PRIVATE (fixed later) */
1138 case 0xa0: /* MSCODEC vlc (video) (fixed later) */
1140 es_format_Init( fmt, UNKNOWN_ES, 0 );
1144 /* PES packets usually contain truncated frames */
1145 fmt->b_packetized = VLC_FALSE;
1147 return fmt->i_cat == UNKNOWN_ES ? VLC_EGENERIC : VLC_SUCCESS ;
1150 /*****************************************************************************
1151 * MP4 specific functions (IOD parser)
1152 *****************************************************************************/
1153 static int IODDescriptorLength( int *pi_data, uint8_t **pp_data )
1156 unsigned int i_len = 0;
1162 i_len = ( i_len << 7 ) + ( i_b&0x7f );
1164 } while( i_b&0x80 );
1168 static int IODGetByte( int *pi_data, uint8_t **pp_data )
1172 const int i_b = **pp_data;
1179 static int IODGetWord( int *pi_data, uint8_t **pp_data )
1181 const int i1 = IODGetByte( pi_data, pp_data );
1182 const int i2 = IODGetByte( pi_data, pp_data );
1183 return( ( i1 << 8 ) | i2 );
1185 static int IODGet3Bytes( int *pi_data, uint8_t **pp_data )
1187 const int i1 = IODGetByte( pi_data, pp_data );
1188 const int i2 = IODGetByte( pi_data, pp_data );
1189 const int i3 = IODGetByte( pi_data, pp_data );
1191 return( ( i1 << 16 ) | ( i2 << 8) | i3 );
1194 static uint32_t IODGetDWord( int *pi_data, uint8_t **pp_data )
1196 const uint32_t i1 = IODGetWord( pi_data, pp_data );
1197 const uint32_t i2 = IODGetWord( pi_data, pp_data );
1198 return( ( i1 << 16 ) | i2 );
1201 static char* IODGetURL( int *pi_data, uint8_t **pp_data )
1206 i_url_len = IODGetByte( pi_data, pp_data );
1207 url = malloc( i_url_len + 1 );
1208 for( i = 0; i < i_url_len; i++ )
1210 url[i] = IODGetByte( pi_data, pp_data );
1212 url[i_url_len] = '\0';
1216 static iod_descriptor_t *IODNew( int i_data, uint8_t *p_data )
1218 iod_descriptor_t *p_iod;
1225 p_iod = malloc( sizeof( iod_descriptor_t ) );
1226 memset( p_iod, 0, sizeof( iod_descriptor_t ) );
1228 fprintf( stderr, "\n************ IOD ************" );
1229 for( i = 0; i < 255; i++ )
1231 p_iod->es_descr[i].b_ok = 0;
1240 p_iod->i_iod_label = IODGetByte( &i_data, &p_data );
1241 fprintf( stderr, "\n* iod_label:%d", p_iod->i_iod_label );
1242 fprintf( stderr, "\n* ===========" );
1243 fprintf( stderr, "\n* tag:0x%x", p_data[0] );
1245 if( IODGetByte( &i_data, &p_data ) != 0x02 )
1247 fprintf( stderr, "\n ERR: tag != 0x02" );
1251 i_iod_length = IODDescriptorLength( &i_data, &p_data );
1252 fprintf( stderr, "\n* length:%d", i_iod_length );
1253 if( i_iod_length > i_data )
1255 i_iod_length = i_data;
1258 p_iod->i_od_id = ( IODGetByte( &i_data, &p_data ) << 2 );
1259 i_flags = IODGetByte( &i_data, &p_data );
1260 p_iod->i_od_id |= i_flags >> 6;
1261 b_url = ( i_flags >> 5 )&0x01;
1263 fprintf( stderr, "\n* od_id:%d", p_iod->i_od_id );
1264 fprintf( stderr, "\n* url flag:%d", b_url );
1265 fprintf( stderr, "\n* includeInlineProfileLevel flag:%d", ( i_flags >> 4 )&0x01 );
1269 p_iod->psz_url = IODGetURL( &i_data, &p_data );
1270 fprintf( stderr, "\n* url string:%s", p_iod->psz_url );
1271 fprintf( stderr, "\n*****************************\n" );
1276 p_iod->psz_url = NULL;
1279 p_iod->i_ODProfileLevelIndication = IODGetByte( &i_data, &p_data );
1280 p_iod->i_sceneProfileLevelIndication = IODGetByte( &i_data, &p_data );
1281 p_iod->i_audioProfileLevelIndication = IODGetByte( &i_data, &p_data );
1282 p_iod->i_visualProfileLevelIndication = IODGetByte( &i_data, &p_data );
1283 p_iod->i_graphicsProfileLevelIndication = IODGetByte( &i_data, &p_data );
1285 fprintf( stderr, "\n* ODProfileLevelIndication:%d", p_iod->i_ODProfileLevelIndication );
1286 fprintf( stderr, "\n* sceneProfileLevelIndication:%d", p_iod->i_sceneProfileLevelIndication );
1287 fprintf( stderr, "\n* audioProfileLevelIndication:%d", p_iod->i_audioProfileLevelIndication );
1288 fprintf( stderr, "\n* visualProfileLevelIndication:%d", p_iod->i_visualProfileLevelIndication );
1289 fprintf( stderr, "\n* graphicsProfileLevelIndication:%d", p_iod->i_graphicsProfileLevelIndication );
1292 while( i_data > 0 && i_es_index < 255)
1294 int i_tag, i_length;
1296 uint8_t *p_data_sav;
1298 i_tag = IODGetByte( &i_data, &p_data );
1299 i_length = IODDescriptorLength( &i_data, &p_data );
1301 i_data_sav = i_data;
1302 p_data_sav = p_data;
1310 #define es_descr p_iod->es_descr[i_es_index]
1311 int i_decoderConfigDescr_length;
1312 fprintf( stderr, "\n* - ES_Descriptor length:%d", i_length );
1315 es_descr.i_es_id = IODGetWord( &i_data, &p_data );
1316 i_flags = IODGetByte( &i_data, &p_data );
1317 es_descr.b_streamDependenceFlag = ( i_flags >> 7 )&0x01;
1318 b_url = ( i_flags >> 6 )&0x01;
1319 es_descr.b_OCRStreamFlag = ( i_flags >> 5 )&0x01;
1320 es_descr.i_streamPriority = i_flags & 0x1f;
1321 fprintf( stderr, "\n* * streamDependenceFlag:%d", es_descr.b_streamDependenceFlag );
1322 fprintf( stderr, "\n* * OCRStreamFlag:%d", es_descr.b_OCRStreamFlag );
1323 fprintf( stderr, "\n* * streamPriority:%d", es_descr.i_streamPriority );
1325 if( es_descr.b_streamDependenceFlag )
1327 es_descr.i_dependOn_es_id = IODGetWord( &i_data, &p_data );
1328 fprintf( stderr, "\n* * dependOn_es_id:%d", es_descr.i_dependOn_es_id );
1333 es_descr.psz_url = IODGetURL( &i_data, &p_data );
1334 fprintf( stderr, "\n* url string:%s", es_descr.psz_url );
1338 es_descr.psz_url = NULL;
1341 if( es_descr.b_OCRStreamFlag )
1343 es_descr.i_OCR_es_id = IODGetWord( &i_data, &p_data );
1344 fprintf( stderr, "\n* * OCR_es_id:%d", es_descr.i_OCR_es_id );
1347 if( IODGetByte( &i_data, &p_data ) != 0x04 )
1349 fprintf( stderr, "\n* ERR missing DecoderConfigDescr" );
1353 i_decoderConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
1355 fprintf( stderr, "\n* - DecoderConfigDesc length:%d", i_decoderConfigDescr_length );
1356 #define dec_descr es_descr.dec_descr
1357 dec_descr.i_objectTypeIndication = IODGetByte( &i_data, &p_data );
1358 i_flags = IODGetByte( &i_data, &p_data );
1359 dec_descr.i_streamType = i_flags >> 2;
1360 dec_descr.b_upStream = ( i_flags >> 1 )&0x01;
1361 dec_descr.i_bufferSizeDB = IODGet3Bytes( &i_data, &p_data );
1362 dec_descr.i_maxBitrate = IODGetDWord( &i_data, &p_data );
1363 dec_descr.i_avgBitrate = IODGetDWord( &i_data, &p_data );
1364 fprintf( stderr, "\n* * objectTypeIndication:0x%x", dec_descr.i_objectTypeIndication );
1365 fprintf( stderr, "\n* * streamType:0x%x", dec_descr.i_streamType );
1366 fprintf( stderr, "\n* * upStream:%d", dec_descr.b_upStream );
1367 fprintf( stderr, "\n* * bufferSizeDB:%d", dec_descr.i_bufferSizeDB );
1368 fprintf( stderr, "\n* * maxBitrate:%d", dec_descr.i_maxBitrate );
1369 fprintf( stderr, "\n* * avgBitrate:%d", dec_descr.i_avgBitrate );
1370 if( i_decoderConfigDescr_length > 13 && IODGetByte( &i_data, &p_data ) == 0x05 )
1373 dec_descr.i_decoder_specific_info_len =
1374 IODDescriptorLength( &i_data, &p_data );
1375 if( dec_descr.i_decoder_specific_info_len > 0 )
1377 dec_descr.p_decoder_specific_info =
1378 malloc( dec_descr.i_decoder_specific_info_len );
1380 for( i = 0; i < dec_descr.i_decoder_specific_info_len; i++ )
1382 dec_descr.p_decoder_specific_info[i] = IODGetByte( &i_data, &p_data );
1387 dec_descr.i_decoder_specific_info_len = 0;
1388 dec_descr.p_decoder_specific_info = NULL;
1392 #define sl_descr es_descr.sl_descr
1394 int i_SLConfigDescr_length;
1397 if( IODGetByte( &i_data, &p_data ) != 0x06 )
1399 fprintf( stderr, "\n* ERR missing SLConfigDescr" );
1403 i_SLConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
1405 fprintf( stderr, "\n* - SLConfigDescr length:%d", i_SLConfigDescr_length );
1406 i_predefined = IODGetByte( &i_data, &p_data );
1407 fprintf( stderr, "\n* * i_predefined:0x%x", i_predefined );
1408 switch( i_predefined )
1412 sl_descr.b_useAccessUnitStartFlag = 0;
1413 sl_descr.b_useAccessUnitEndFlag = 0;
1414 sl_descr.b_useRandomAccessPointFlag = 0;
1415 //sl_descr.b_useRandomAccessUnitsOnlyFlag = 0;
1416 sl_descr.b_usePaddingFlag = 0;
1417 sl_descr.b_useTimeStampsFlags = 0;
1418 sl_descr.b_useIdleFlag = 0;
1419 sl_descr.b_durationFlag = 0; // FIXME FIXME
1420 sl_descr.i_timeStampResolution = 1000;
1421 sl_descr.i_OCRResolution = 0; // FIXME FIXME
1422 sl_descr.i_timeStampLength = 32;
1423 sl_descr.i_OCRLength = 0; // FIXME FIXME
1424 sl_descr.i_AU_Length = 0;
1425 sl_descr.i_instantBitrateLength= 0; // FIXME FIXME
1426 sl_descr.i_degradationPriorityLength= 0;
1427 sl_descr.i_AU_seqNumLength = 0;
1428 sl_descr.i_packetSeqNumLength = 0;
1429 if( sl_descr.b_durationFlag )
1431 sl_descr.i_timeScale = 0; // FIXME FIXME
1432 sl_descr.i_accessUnitDuration = 0; // FIXME FIXME
1433 sl_descr.i_compositionUnitDuration= 0; // FIXME FIXME
1435 if( !sl_descr.b_useTimeStampsFlags )
1437 sl_descr.i_startDecodingTimeStamp = 0; // FIXME FIXME
1438 sl_descr.i_startCompositionTimeStamp= 0; // FIXME FIXME
1443 fprintf( stderr, "\n* ERR unsupported SLConfigDescr predefined" );
1452 fprintf( stderr, "\n* - OD tag:0x%x length:%d (Unsupported)", i_tag, i_length );
1456 p_data = p_data_sav + i_length;
1457 i_data = i_data_sav - i_length;
1462 fprintf( stderr, "\n*****************************\n" );
1466 static void IODFree( iod_descriptor_t *p_iod )
1470 if( p_iod->psz_url )
1472 free( p_iod->psz_url );
1473 p_iod->psz_url = NULL;
1478 for( i = 0; i < 255; i++ )
1480 #define es_descr p_iod->es_descr[i]
1483 if( es_descr.psz_url )
1485 free( es_descr.psz_url );
1486 es_descr.psz_url = NULL;
1490 if( es_descr.dec_descr.p_decoder_specific_info != NULL )
1492 free( es_descr.dec_descr.p_decoder_specific_info );
1493 es_descr.dec_descr.p_decoder_specific_info = NULL;
1494 es_descr.dec_descr.i_decoder_specific_info_len = 0;
1504 /****************************************************************************
1505 ****************************************************************************
1506 ** libdvbpsi callbacks
1507 ****************************************************************************
1508 ****************************************************************************/
1509 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt )
1511 demux_sys_t *p_sys = p_demux->p_sys;
1512 dvbpsi_descriptor_t *p_dr;
1513 dvbpsi_pmt_es_t *p_es;
1515 ts_pid_t *pmt = NULL;
1518 msg_Dbg( p_demux, "PMTCallBack called" );
1519 /* First find this PMT declared in PAT */
1520 for( i = 0; i < p_sys->i_pmt; i++ )
1522 if( p_sys->pmt[i]->psi->i_number == p_pmt->i_program_number )
1524 pmt = p_sys->pmt[i];
1529 msg_Warn( p_demux, "unreferenced program (broken stream)" );
1530 dvbpsi_DeletePMT(p_pmt);
1534 if( pmt->psi->i_version != -1 && ( !p_pmt->b_current_next || pmt->psi->i_version == p_pmt->i_version ) )
1536 dvbpsi_DeletePMT( p_pmt );
1540 /* Clean this program (remove all es) */
1541 for( i = 0; i < 8192; i++ )
1543 ts_pid_t *pid = &p_sys->pid[i];
1545 if( pid->b_valid && pid->p_owner == pmt->psi && pid->psi == NULL )
1547 PIDClean( p_demux->out, pid );
1552 IODFree( pmt->psi->iod );
1553 pmt->psi->iod = NULL;
1556 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 );
1557 pmt->psi->i_pid_pcr = p_pmt->i_pcr_pid;
1558 pmt->psi->i_version = p_pmt->i_version;
1560 /* Parse descriptor */
1561 for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
1563 if( p_dr->i_tag == 0x1d )
1565 /* We have found an IOD descriptor */
1566 msg_Warn( p_demux, " * descriptor : IOD (0x1d)" );
1568 pmt->psi->iod = IODNew( p_dr->i_length, p_dr->p_data );
1572 msg_Dbg( p_demux, " * descriptor : unknown (0x%x)", p_dr->i_tag );
1576 for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
1578 ts_pid_t *pid = &p_sys->pid[p_es->i_pid];
1582 msg_Warn( p_demux, "pmt error: pid=0x%x already defined", p_es->i_pid );
1586 PIDInit( pid, VLC_FALSE, pmt->psi );
1587 PIDFillFormat( pid, p_es->i_type );
1589 if( p_es->i_type == 0x10 || p_es->i_type == 0x11 )
1591 /* MPEG-4 stream: search SL_DESCRIPTOR */
1592 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
1594 while( p_dr && ( p_dr->i_tag != 0x1f ) ) p_dr = p_dr->p_next;
1596 if( p_dr && p_dr->i_length == 2 )
1599 int i_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
1601 msg_Warn( p_demux, "found SL_descriptor es_id=%d", i_es_id );
1603 pid->es->p_mpeg4desc = NULL;
1605 for( i = 0; i < 255; i++ )
1607 iod_descriptor_t *iod = pmt->psi->iod;
1609 if( iod->es_descr[i].b_ok &&
1610 iod->es_descr[i].i_es_id == i_es_id )
1612 pid->es->p_mpeg4desc = &iod->es_descr[i];
1618 if( pid->es->p_mpeg4desc != NULL )
1620 decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;
1622 if( dcd->i_streamType == 0x04 ) /* VisualStream */
1624 pid->es->fmt.i_cat = VIDEO_ES;
1625 switch( dcd->i_objectTypeIndication )
1628 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','v'); // mpeg4
1636 pid->es->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' ); // mpeg2
1639 pid->es->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' ); // mpeg1
1642 pid->es->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' ); // mpeg1
1646 pid->es->fmt.i_cat = UNKNOWN_ES;
1650 else if( dcd->i_streamType == 0x05 ) /* AudioStream */
1652 pid->es->fmt.i_cat = AUDIO_ES;
1653 switch( dcd->i_objectTypeIndication )
1656 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','a'); // mpeg4
1661 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','a');// mpeg2 aac
1664 pid->es->fmt.i_codec = VLC_FOURCC('m','p','g','a'); // mpeg2
1667 pid->es->fmt.i_codec = VLC_FOURCC('m','p','g','a'); // mpeg1
1670 pid->es->fmt.i_cat = UNKNOWN_ES;
1676 pid->es->fmt.i_cat = UNKNOWN_ES;
1679 if( pid->es->fmt.i_cat != UNKNOWN_ES )
1681 pid->es->fmt.i_extra = dcd->i_decoder_specific_info_len;
1682 if( pid->es->fmt.i_extra > 0 )
1684 pid->es->fmt.p_extra = malloc( pid->es->fmt.i_extra );
1685 memcpy( pid->es->fmt.p_extra,
1686 dcd->p_decoder_specific_info,
1687 pid->es->fmt.i_extra );
1692 else if( p_es->i_type == 0x06 )
1694 dvbpsi_descriptor_t *p_dr;
1696 for( p_dr = p_es->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
1698 msg_Dbg( p_demux, " * es pid=0x%x type=0x%x dr->i_tag=0x%x",
1699 p_es->i_pid, p_es->i_type, p_dr->i_tag );
1701 if( p_dr->i_tag == 0x6a )
1703 pid->es->fmt.i_cat = AUDIO_ES;
1704 pid->es->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
1706 #ifdef _DVBPSI_DR_59_H_
1707 else if( p_dr->i_tag == 0x59 )
1715 else if( p_es->i_type == 0xa0 )
1717 /* MSCODEC send by vlc */
1718 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
1720 while( p_dr && ( p_dr->i_tag != 0xa0 ) ) p_dr = p_dr->p_next;
1722 if( p_dr && p_dr->i_length >= 8 )
1724 pid->es->fmt.i_cat = VIDEO_ES;
1725 pid->es->fmt.i_codec = VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
1726 p_dr->p_data[2], p_dr->p_data[3] );
1727 pid->es->fmt.video.i_width = ( p_dr->p_data[4] << 8 )|p_dr->p_data[5];
1728 pid->es->fmt.video.i_height= ( p_dr->p_data[6] << 8 )|p_dr->p_data[7];
1729 pid->es->fmt.i_extra = (p_dr->p_data[8] << 8) | p_dr->p_data[9];
1731 if( pid->es->fmt.i_extra > 0 )
1733 pid->es->fmt.p_extra = malloc( pid->es->fmt.i_extra );
1734 memcpy( pid->es->fmt.p_extra, &p_dr->p_data[10], pid->es->fmt.i_extra );
1739 msg_Warn( p_demux, "private MSCODEC (vlc) without bih private descriptor" );
1741 /* For such stream we will gather them ourself and don't launch a packetize,
1742 * Yes it's ugly but it's the only way to make DIV3 working */
1743 pid->es->fmt.b_packetized = VLC_TRUE;
1746 if( pid->es->fmt.i_cat == AUDIO_ES || pid->es->fmt.i_cat == SPU_ES )
1748 /* get language descriptor */
1749 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
1750 while( p_dr && ( p_dr->i_tag != 0x0a ) ) p_dr = p_dr->p_next;
1754 dvbpsi_iso639_dr_t *p_decoded = dvbpsi_DecodeISO639Dr( p_dr );
1758 pid->es->fmt.psz_language = malloc( 4 );
1759 memcpy( pid->es->fmt.psz_language, p_decoded->i_iso_639_code, 3 );
1760 pid->es->fmt.psz_language[3] = 0;
1765 pid->es->fmt.i_group = p_pmt->i_program_number;
1766 if( pid->es->fmt.i_cat == UNKNOWN_ES )
1768 msg_Dbg( p_demux, " * es pid=0x%x type=0x%x *unknown*", p_es->i_pid, p_es->i_type );
1770 else if( !p_sys->b_udp_out )
1772 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 );
1773 if( p_sys->b_es_id_pid )
1775 pid->es->fmt.i_id = p_es->i_pid;
1777 pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
1780 dvbpsi_DeletePMT(p_pmt);
1783 static void PATCallBack( demux_t *p_demux, dvbpsi_pat_t *p_pat )
1785 demux_sys_t *p_sys = p_demux->p_sys;
1786 dvbpsi_pat_program_t *p_program;
1787 ts_pid_t *pat = &p_sys->pid[0];
1790 msg_Dbg( p_demux, "PATCallBack called" );
1792 if( pat->psi->i_version != -1 &&
1793 ( !p_pat->b_current_next || p_pat->i_version == pat->psi->i_version ) )
1795 dvbpsi_DeletePAT( p_pat );
1799 msg_Dbg( p_demux, "new PAT ts_id=0x%x version=%d current_next=%d",
1800 p_pat->i_ts_id, p_pat->i_version, p_pat->b_current_next );
1803 if( p_sys->i_pmt > 0 )
1806 ts_pid_t **pmt_rm = NULL;
1808 /* Search pmt to be deleted */
1809 for( i = 0; i < p_sys->i_pmt; i++ )
1811 ts_pid_t *pmt = p_sys->pmt[i];
1812 vlc_bool_t b_keep = VLC_FALSE;
1814 for( p_program = p_pat->p_first_program; p_program != NULL; p_program = p_program->p_next )
1816 if( p_program->i_pid == pmt->i_pid && p_program->i_number == pmt->psi->i_number )
1824 TAB_APPEND( i_pmt_rm, pmt_rm, pmt );
1828 /* Delete all ES attached to thoses PMT */
1829 for( i = 2; i < 8192; i++ )
1831 ts_pid_t *pid = &p_sys->pid[i];
1832 if( pid->b_valid && !pid->psi )
1834 for( j = 0; j < i_pmt_rm; j++ )
1836 if( pid->p_owner->i_pid_pcr == pmt_rm[j]->i_pid && pid->es->id )
1838 /* We only remove es that aren't defined by extra pmt */
1839 PIDClean( p_demux->out, pid );
1846 /* Delete PMT pid */
1847 for( i = 0; i < i_pmt_rm; i++ )
1849 PIDClean( p_demux->out, &p_sys->pid[pmt_rm[i]->i_pid] );
1850 TAB_REMOVE( p_sys->i_pmt, p_sys->pmt, pmt_rm[i] );
1858 /* now create programs */
1859 for( p_program = p_pat->p_first_program; p_program != NULL;
1860 p_program = p_program->p_next )
1862 msg_Dbg( p_demux, " * number=%d pid=0x%x", p_program->i_number,
1864 if( p_program->i_number != 0 )
1866 ts_pid_t *pmt = &p_sys->pid[p_program->i_pid];
1870 PIDInit( pmt, VLC_TRUE, pat->psi );
1872 dvbpsi_AttachPMT( p_program->i_number,
1873 (dvbpsi_pmt_callback)PMTCallBack, p_demux );
1874 pmt->psi->i_number = p_program->i_number;
1876 TAB_APPEND( p_sys->i_pmt, p_sys->pmt, pmt );
1880 pat->psi->i_version = p_pat->i_version;
1882 dvbpsi_DeletePAT( p_pat );