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 add_bool( "ts-silent", 0, NULL, "Silent mode", "do not complain on encrypted PES", VLC_TRUE );
74 set_capability( "demux2", 10 );
75 set_callbacks( Open, Close );
76 add_shortcut( "ts2" );
79 /*****************************************************************************
81 *****************************************************************************/
85 uint8_t i_objectTypeIndication;
87 vlc_bool_t b_upStream;
88 uint32_t i_bufferSizeDB;
89 uint32_t i_maxBitrate;
90 uint32_t i_avgBitrate;
92 int i_decoder_specific_info_len;
93 uint8_t *p_decoder_specific_info;
95 } decoder_config_descriptor_t;
99 vlc_bool_t b_useAccessUnitStartFlag;
100 vlc_bool_t b_useAccessUnitEndFlag;
101 vlc_bool_t b_useRandomAccessPointFlag;
102 vlc_bool_t b_useRandomAccessUnitsOnlyFlag;
103 vlc_bool_t b_usePaddingFlag;
104 vlc_bool_t b_useTimeStampsFlags;
105 vlc_bool_t b_useIdleFlag;
106 vlc_bool_t b_durationFlag;
107 uint32_t i_timeStampResolution;
108 uint32_t i_OCRResolution;
109 uint8_t i_timeStampLength;
112 uint8_t i_instantBitrateLength;
113 uint8_t i_degradationPriorityLength;
114 uint8_t i_AU_seqNumLength;
115 uint8_t i_packetSeqNumLength;
117 uint32_t i_timeScale;
118 uint16_t i_accessUnitDuration;
119 uint16_t i_compositionUnitDuration;
121 uint64_t i_startDecodingTimeStamp;
122 uint64_t i_startCompositionTimeStamp;
124 } sl_config_descriptor_t;
131 vlc_bool_t b_streamDependenceFlag;
132 vlc_bool_t b_OCRStreamFlag;
133 uint8_t i_streamPriority;
137 uint16_t i_dependOn_es_id;
138 uint16_t i_OCR_es_id;
140 decoder_config_descriptor_t dec_descr;
141 sl_config_descriptor_t sl_descr;
142 } es_mpeg4_descriptor_t;
152 uint8_t i_ODProfileLevelIndication;
153 uint8_t i_sceneProfileLevelIndication;
154 uint8_t i_audioProfileLevelIndication;
155 uint8_t i_visualProfileLevelIndication;
156 uint8_t i_graphicsProfileLevelIndication;
158 es_mpeg4_descriptor_t es_descr[255];
164 dvbpsi_handle handle;
169 /* IOD stuff (mpeg4) */
170 iod_descriptor_t *iod;
175 /* for special PAT case */
176 dvbpsi_handle handle;
193 es_mpeg4_descriptor_t *p_mpeg4desc;
203 int i_cc; /* countinuity counter */
205 /* PSI owner (ie PMT -> PAT, ES -> PMT */
213 /* Some private streams encapsulate several ES (eg. DVB subtitles)*/
221 /* how many TS packet we read at once */
232 vlc_bool_t b_es_id_pid;
236 vlc_bool_t b_udp_out;
237 int fd; /* udp socket */
241 static int Demux ( demux_t *p_demux );
242 static int Control( demux_t *p_demux, int i_query, va_list args );
245 static void PIDInit ( ts_pid_t *pid, vlc_bool_t b_psi, ts_psi_t *p_owner );
246 static void PIDClean( es_out_t *out, ts_pid_t *pid );
247 static int PIDFillFormat( ts_pid_t *pid, int i_stream_type );
249 static void PATCallBack( demux_t *, dvbpsi_pat_t * );
250 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt );
252 static inline int PIDGet( block_t *p )
254 return ( (p->p_buffer[1]&0x1f)<<8 )|p->p_buffer[2];
257 static vlc_bool_t GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk );
259 static void PCRHandle( demux_t *p_demux, ts_pid_t *, block_t * );
261 static iod_descriptor_t *IODNew( int , uint8_t * );
262 static void IODFree( iod_descriptor_t * );
265 /*****************************************************************************
267 *****************************************************************************/
268 static int Open( vlc_object_t *p_this )
270 demux_t *p_demux = (demux_t*)p_this;
282 if( ( i_peek = stream_Peek( p_demux->s, &p_peek, 189 ) ) < 1 )
284 msg_Err( p_demux, "cannot peek" );
288 /* Search first synch */
289 for( i_sync = 0; i_sync < i_peek; i_sync++ )
291 if( p_peek[i_sync] == 0x47 ) break;
293 if( i_sync >= i_peek )
295 if( strcmp( p_demux->psz_demux, "ts2" ) )
297 msg_Warn( p_demux, "TS module discarded" );
300 msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
302 if( strcmp( p_demux->psz_demux, "ts2" ) )
304 /* Check next 3 sync points */
305 i_peek = 188*3 + 1 + i_sync;
306 if( ( stream_Peek( p_demux->s, &p_peek, i_peek ) ) < i_peek )
308 msg_Err( p_demux, "cannot peek" );
311 if( p_peek[i_sync+ 188] != 0x47 || p_peek[i_sync+2*188] != 0x47 ||
312 p_peek[i_sync+3*188] != 0x47 )
314 msg_Warn( p_demux, "TS module discarded (lost sync)" );
319 /* Fill p_demux field */
320 p_demux->pf_demux = Demux;
321 p_demux->pf_control = Control;
322 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
323 memset( p_sys, 0, sizeof( demux_sys_t ) );
325 /* Init p_sys field */
326 for( i = 0; i < 8192; i++ )
328 ts_pid_t *pid = &p_sys->pid[i];
331 pid->b_seen = VLC_FALSE;
332 pid->b_valid = VLC_FALSE;
334 p_sys->b_udp_out = VLC_FALSE;
335 p_sys->i_ts_read = 50;
338 /* Init PAT handler */
339 pat = &p_sys->pid[0];
340 PIDInit( pat, VLC_TRUE, NULL );
341 pat->psi->handle = dvbpsi_AttachPAT( (dvbpsi_pat_callback)PATCallBack,
349 var_Create( p_demux, "ts-es-id-pid", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
350 var_Get( p_demux, "ts-es-id-pid", &val );
351 p_sys->b_es_id_pid = val.b_bool,
353 var_Create( p_demux, "ts-out", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
354 var_Get( p_demux, "ts-out", &val );
355 if( val.psz_string && *val.psz_string )
358 char *psz = strchr( val.psz_string, ':' );
361 p_sys->b_udp_out = VLC_TRUE;
366 i_port = atoi( psz );
368 if( i_port <= 0 ) i_port = 1234;
369 msg_Dbg( p_demux, "resend ts to '%s:%d'", val.psz_string, i_port );
371 p_sys->fd = net_OpenUDP( p_demux, "", 0, val.psz_string, i_port );
374 msg_Err( p_demux, "failed to open udp socket, send disabled" );
375 p_sys->b_udp_out = VLC_FALSE;
379 var_Create( p_demux, "ts-out-mtu",
380 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
381 var_Get( p_demux, "ts-out-mtu", &mtu );
382 p_sys->i_ts_read = mtu.i_int / 188;
383 if( p_sys->i_ts_read <= 0 )
385 p_sys->i_ts_read = 1500 / 188;
387 p_sys->buffer = malloc( 188 * p_sys->i_ts_read );
392 free( val.psz_string );
396 /* We handle description of an extra PMT */
397 var_Create( p_demux, "ts-extra-pmt", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
398 var_Get( p_demux, "ts-extra-pmt", &val );
399 if( val.psz_string && strchr( val.psz_string, '=' ) != NULL )
401 char *psz = val.psz_string;
402 int i_pid = strtol( psz, &psz, 0 );
404 if( i_pid >= 2 && i_pid < 8192 )
406 ts_pid_t *pmt = &p_sys->pid[i_pid];
408 msg_Dbg( p_demux, "extra pmt specified (pid=0x%x)", i_pid );
409 PIDInit( pmt, VLC_TRUE, NULL );
410 /* FIXME we should also ask for a number */
411 pmt->psi->prg[0]->handle =
412 dvbpsi_AttachPMT( 1, (dvbpsi_pmt_callback)PMTCallBack,
414 pmt->psi->prg[0]->i_number = 0; /* special one */
416 psz = strchr( psz, '=' ) + 1; /* can't failed */
419 char *psz_next = strchr( psz, ',' );
420 int i_pid, i_stream_type;
427 i_pid = strtol( psz, &psz, 0 );
430 i_stream_type = strtol( psz + 1, &psz, 0 );
431 if( i_pid >= 2 && i_pid < 8192 &&
432 !p_sys->pid[i_pid].b_valid )
434 ts_pid_t *pid = &p_sys->pid[i_pid];
436 PIDInit( pid, VLC_FALSE, pmt->psi);
437 if( pmt->psi->prg[0]->i_pid_pcr <= 0 )
439 pmt->psi->prg[0]->i_pid_pcr = i_pid;
441 PIDFillFormat( pid, i_stream_type);
442 if( pid->es->fmt.i_cat != UNKNOWN_ES )
444 if( p_sys->b_es_id_pid )
446 pid->es->fmt.i_id = i_pid;
448 msg_Dbg( p_demux, " * es pid=0x%x type=0x%x "
449 "fcc=%4.4s", i_pid, i_stream_type,
450 (char*)&pid->es->fmt.i_codec );
451 pid->es->id = es_out_Add( p_demux->out,
462 free( val.psz_string );
465 var_Create( p_demux, "ts-csa-ck", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
466 var_Get( p_demux, "ts-csa-ck", &val );
467 if( val.psz_string && *val.psz_string )
469 char *psz = val.psz_string;
470 if( psz[0] == '0' && ( psz[1] == 'x' || psz[1] == 'X' ) )
474 if( strlen( psz ) != 16 )
476 msg_Warn( p_demux, "invalid csa ck (it must be 16 chars long)" );
480 uint64_t i_ck = strtoll( psz, NULL, 16 );
483 for( i = 0; i < 8; i++ )
485 ck[i] = ( i_ck >> ( 56 - 8*i) )&0xff;
488 msg_Dbg( p_demux, "using CSA scrambling with "
489 "ck=%x:%x:%x:%x:%x:%x:%x:%x",
490 ck[0], ck[1], ck[2], ck[3], ck[4], ck[5], ck[6], ck[7] );
492 p_sys->csa = csa_New();
493 csa_SetCW( p_sys->csa, ck, ck );
498 free( val.psz_string );
501 var_Create( p_demux, "ts-silent", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
502 var_Get( p_demux, "ts-silent", &val );
503 p_sys->b_silent = val.b_bool;
508 /*****************************************************************************
510 *****************************************************************************/
511 static void Close( vlc_object_t *p_this )
513 demux_t *p_demux = (demux_t*)p_this;
514 demux_sys_t *p_sys = p_demux->p_sys;
518 msg_Dbg( p_demux, "pid list:" );
519 for( i = 0; i < 8192; i++ )
521 ts_pid_t *pid = &p_sys->pid[i];
523 if( pid->b_valid && pid->psi )
528 dvbpsi_DetachPAT( pid->psi->handle );
535 PIDClean( p_demux->out, pid );
539 else if( pid->b_valid && pid->es )
541 PIDClean( p_demux->out, pid );
546 msg_Dbg( p_demux, " - pid[0x%x] seen", pid->i_pid );
550 if( p_sys->b_udp_out )
552 net_Close( p_sys->fd );
553 free( p_sys->buffer );
557 csa_Delete( p_sys->csa );
560 if( p_sys->i_pmt ) free( p_sys->pmt );
564 /*****************************************************************************
566 *****************************************************************************/
567 static int Demux( demux_t *p_demux )
569 demux_sys_t *p_sys = p_demux->p_sys;
572 /* We read at most 100 TS packet or until a frame is completed */
573 for( i_pkt = 0; i_pkt < p_sys->i_ts_read; i_pkt++ )
575 vlc_bool_t b_frame = VLC_FALSE;
579 /* Get a new TS packet */
580 if( ( p_pkt = stream_Block( p_demux->s, 188 ) ) == NULL )
582 msg_Dbg( p_demux, "eof ?" );
585 if( p_pkt->p_buffer[0] != 0x47 )
587 msg_Warn( p_demux, "lost synchro" );
588 block_Release( p_pkt );
591 while( !p_demux->b_die )
594 int i_peek = stream_Peek( p_demux->s, &p_peek, 1880 );
596 vlc_bool_t b_ok = VLC_FALSE;
600 msg_Dbg( p_demux, "eof ?" );
604 while( i_skip < i_peek - 188 )
606 if( p_peek[i_skip] == 0x47 && p_peek[i_skip+188] == 0x47 )
613 stream_Read( p_demux->s, NULL, i_skip );
614 msg_Dbg( p_demux, "%d bytes of garbage", i_skip );
620 if( ( p_pkt = stream_Block( p_demux->s, 188 ) ) == NULL )
622 msg_Dbg( p_demux, "eof ?" );
627 if( p_sys->b_udp_out )
629 memcpy( &p_sys->buffer[i_pkt*188], p_pkt->p_buffer, 188 );
632 /* Parse the TS packet */
633 p_pid = &p_sys->pid[PIDGet( p_pkt )];
639 if( p_pid->i_pid == 0 )
641 dvbpsi_PushPacket( p_pid->psi->handle, p_pkt->p_buffer );
646 for( i_prg = 0; i_prg < p_pid->psi->i_prg; i_prg++ )
648 dvbpsi_PushPacket( p_pid->psi->prg[i_prg]->handle, p_pkt->p_buffer );
651 block_Release( p_pkt );
653 else if( !p_sys->b_udp_out )
655 b_frame = GatherPES( p_demux, p_pid, p_pkt );
659 block_Release( p_pkt );
666 msg_Dbg( p_demux, "pid[0x%x] unknown", p_pid->i_pid );
668 /* We have to handle PCR if present */
669 PCRHandle( p_demux, p_pid, p_pkt );
670 block_Release( p_pkt );
672 p_pid->b_seen = VLC_TRUE;
680 if( p_sys->b_udp_out )
682 /* Send the complete block */
683 net_Write( p_demux, p_sys->fd, p_sys->buffer, p_sys->i_ts_read * 188 );
689 /*****************************************************************************
691 *****************************************************************************/
692 static int Control( demux_t *p_demux, int i_query, va_list args )
694 /* demux_sys_t *p_sys = p_demux->p_sys; */
700 case DEMUX_GET_POSITION:
701 pf = (double*) va_arg( args, double* );
702 i64 = stream_Size( p_demux->s );
705 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
712 case DEMUX_SET_POSITION:
713 f = (double) va_arg( args, double );
714 i64 = stream_Size( p_demux->s );
716 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
717 if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) )
725 pi64 = (int64_t*)va_arg( args, int64_t * );
726 if( p_sys->i_time < 0 )
731 *pi64 = p_sys->i_time;
734 case DEMUX_GET_LENGTH:
735 pi64 = (int64_t*)va_arg( args, int64_t * );
736 if( p_sys->i_mux_rate > 0 )
738 *pi64 = I64C(1000000) * ( stream_Size( p_demux->s ) / 50 ) /
752 static void PIDInit( ts_pid_t *pid, vlc_bool_t b_psi, ts_psi_t *p_owner )
754 vlc_bool_t b_old_valid = pid->b_valid;
756 pid->b_valid = VLC_TRUE;
758 pid->p_owner = p_owner;
759 pid->i_owner_number = 0;
761 pid->extra_es = NULL;
770 pid->psi = malloc( sizeof( ts_psi_t ) );
772 pid->psi->prg = NULL;
773 pid->psi->handle= NULL;
775 pid->psi->i_pat_version = -1;
778 ts_prg_psi_t *prg = malloc( sizeof( ts_prg_psi_t ) );
786 TAB_APPEND( pid->psi->i_prg, pid->psi->prg, prg );
792 pid->es = malloc( sizeof( ts_es_t ) );
794 es_format_Init( &pid->es->fmt, UNKNOWN_ES, 0 );
796 pid->es->p_pes = NULL;
797 pid->es->i_pes_size= 0;
798 pid->es->i_pes_gathered= 0;
799 pid->es->pp_last = &pid->es->p_pes;
800 pid->es->p_mpeg4desc = NULL;
801 pid->es->b_gather = VLC_FALSE;
805 static void PIDClean( es_out_t *out, ts_pid_t *pid )
811 if( pid->psi->handle ) dvbpsi_DetachPMT( pid->psi->handle );
812 for( i = 0; i < pid->psi->i_prg; i++ )
814 if( pid->psi->prg[i]->iod ) IODFree( pid->psi->prg[i]->iod );
815 if( pid->psi->prg[i]->handle ) dvbpsi_DetachPMT( pid->psi->prg[i]->handle );
816 free( pid->psi->prg[i] );
818 if( pid->psi->prg ) free( pid->psi->prg );
826 es_out_Del( out, pid->es->id );
829 block_ChainRelease( pid->es->p_pes );
831 es_format_Clean( &pid->es->fmt );
835 for( i = 0; i < pid->i_extra_es; i++ )
837 if( pid->extra_es[i]->id )
838 es_out_Del( out, pid->extra_es[i]->id );
840 if( pid->extra_es[i]->p_pes )
841 block_ChainRelease( pid->extra_es[i]->p_pes );
843 es_format_Clean( &pid->extra_es[i]->fmt );
845 free( pid->extra_es[i] );
847 if( pid->i_extra_es ) free( pid->extra_es );
850 pid->b_valid = VLC_FALSE;
853 /****************************************************************************
855 ****************************************************************************/
856 static void ParsePES ( demux_t *p_demux, ts_pid_t *pid )
858 block_t *p_pes = pid->es->p_pes;
864 mtime_t i_length = 0;
867 /* remove the pes from pid */
868 pid->es->p_pes = NULL;
869 pid->es->i_pes_size= 0;
870 pid->es->i_pes_gathered= 0;
871 pid->es->pp_last = &pid->es->p_pes;
873 /* FIXME find real max size */
874 i_max = block_ChainExtract( p_pes, header, 30 );
876 if( header[0] != 0 || header[1] != 0 || header[2] != 1 )
878 if ( !p_demux->p_sys->b_silent )
879 msg_Warn( p_demux, "invalid header [0x%x:%x:%x:%x]", header[0],
880 header[1],header[2],header[3] );
881 block_ChainRelease( p_pes );
885 /* TODO check size */
888 case 0xBC: /* Program stream map */
889 case 0xBE: /* Padding */
890 case 0xBF: /* Private stream 2 */
893 case 0xFF: /* Program stream directory */
894 case 0xF2: /* DSMCC stream */
895 case 0xF8: /* ITU-T H.222.1 type E stream */
899 if( ( header[6]&0xC0 ) == 0x80 )
902 i_skip = header[8] + 9;
904 if( header[7]&0x80 ) /* has pts */
906 i_pts = ((mtime_t)(header[ 9]&0x0e ) << 29)|
907 (mtime_t)(header[10] << 22)|
908 ((mtime_t)(header[11]&0xfe) << 14)|
909 (mtime_t)(header[12] << 7)|
910 (mtime_t)(header[13] >> 1);
912 if( header[7]&0x40 ) /* has dts */
914 i_dts = ((mtime_t)(header[14]&0x0e ) << 29)|
915 (mtime_t)(header[15] << 22)|
916 ((mtime_t)(header[16]&0xfe) << 14)|
917 (mtime_t)(header[17] << 7)|
918 (mtime_t)(header[18] >> 1);
925 while( i_skip < 23 && header[i_skip] == 0xff )
931 msg_Err( p_demux, "too much MPEG-1 stuffing" );
932 block_ChainRelease( p_pes );
935 if( ( header[i_skip] & 0xC0 ) == 0x40 )
940 if( header[i_skip]&0x20 )
942 i_pts = ((mtime_t)(header[i_skip]&0x0e ) << 29)|
943 (mtime_t)(header[i_skip+1] << 22)|
944 ((mtime_t)(header[i_skip+2]&0xfe) << 14)|
945 (mtime_t)(header[i_skip+3] << 7)|
946 (mtime_t)(header[i_skip+4] >> 1);
948 if( header[i_skip]&0x10 ) /* has dts */
950 i_dts = ((mtime_t)(header[i_skip+5]&0x0e ) << 29)|
951 (mtime_t)(header[i_skip+6] << 22)|
952 ((mtime_t)(header[i_skip+7]&0xfe) << 14)|
953 (mtime_t)(header[i_skip+8] << 7)|
954 (mtime_t)(header[i_skip+9] >> 1);
970 if( pid->es->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', 'b' ) ||
971 pid->es->fmt.i_codec == VLC_FOURCC( 'd', 't', 's', 'b' ) )
975 else if( pid->es->fmt.i_codec == VLC_FOURCC( 'l', 'p', 'c', 'b' ) ||
976 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'p', 'u', 'b' ) ||
977 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'd', 'd', 'b' ) )
981 else if( pid->es->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) && pid->es->p_mpeg4desc )
983 decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;
985 if( dcd->i_decoder_specific_info_len > 2 &&
986 dcd->p_decoder_specific_info[0] == 0x10 &&
987 ( dcd->p_decoder_specific_info[1]&0x10 ) )
990 if( p_pes->i_buffer + 2 <= i_skip )
992 msg_Warn( p_demux, "length: %lld", i_length );
993 i_length = GetWBE( &p_pes->p_buffer[i_skip] );
998 if( p_pes->i_buffer + 2 <= i_skip )
1000 i_pes_size = GetWBE( &p_pes->p_buffer[i_skip] );
1007 while( p_pes && i_skip > 0 )
1009 if( p_pes->i_buffer <= i_skip )
1011 block_t *p_next = p_pes->p_next;
1013 i_skip -= p_pes->i_buffer;
1014 block_Release( p_pes );
1019 p_pes->i_buffer -= i_skip;
1020 p_pes->p_buffer += i_skip;
1032 p_pes->i_dts = i_dts * 100 / 9;
1036 p_pes->i_pts = i_pts * 100 / 9;
1038 p_pes->i_length = i_length * 100 / 9;
1040 p_block = block_ChainGather( p_pes );
1041 if( pid->es->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) )
1043 if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
1045 p_block->i_buffer = i_pes_size;
1048 p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
1049 p_block->p_buffer[p_block->i_buffer -1] = '\0';
1051 msg_Warn( p_demux, "subs: pts=%lld str='%s'",
1052 p_block->i_pts, p_block->p_buffer );
1055 for( i = 0; i < pid->i_extra_es; i++ )
1057 es_out_Send( p_demux->out, pid->extra_es[i]->id,
1058 block_Duplicate( p_block ) );
1061 es_out_Send( p_demux->out, pid->es->id, p_block );
1065 msg_Warn( p_demux, "empty pes" );
1069 static void PCRHandle( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
1071 demux_sys_t *p_sys = p_demux->p_sys;
1072 const uint8_t *p = p_bk->p_buffer;
1074 if( ( p[3]&0x20 ) && /* adaptation */
1079 mtime_t i_pcr; /* 33 bits */
1081 i_pcr = ( (mtime_t)p[6] << 25 ) |
1082 ( (mtime_t)p[7] << 17 ) |
1083 ( (mtime_t)p[8] << 9 ) |
1084 ( (mtime_t)p[9] << 1 ) |
1085 ( (mtime_t)p[10] >> 7 );
1087 /* Search program and set the PCR */
1088 for( i = 0; i < p_sys->i_pmt; i++ )
1091 for( i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
1093 if( pid->i_pid == p_sys->pmt[i]->psi->prg[i_prg]->i_pid_pcr )
1095 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
1096 (int)p_sys->pmt[i]->psi->prg[i_prg]->i_number,
1097 (int64_t)(i_pcr * 100 / 9) );
1104 static vlc_bool_t GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
1106 const uint8_t *p = p_bk->p_buffer;
1107 const vlc_bool_t b_adaptation= p[3]&0x20;
1108 const vlc_bool_t b_payload = p[3]&0x10;
1109 const int i_cc = p[3]&0x0f; /* continuity counter */
1110 /* transport_scrambling_control is ignored */
1113 vlc_bool_t i_ret = VLC_FALSE;
1117 //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);
1120 msg_Dbg( p_demux, "transport_error_indicator set (pid=0x%x)", pid->i_pid );
1123 if( p_demux->p_sys->csa )
1125 csa_Decrypt( p_demux->p_sys->csa, p_bk->p_buffer );
1134 /* p[4] is adaptation length */
1140 msg_Warn( p_demux, "discontinuity_indicator (pid=0x%x) "
1141 "ignored", pid->i_pid );
1145 /* test continuity counter */
1146 /* continuous when (one of this):
1148 * diff == 0 and payload == 0
1149 * diff == 0 and duplicate packet (playload != 0) <- do we should test the content ?
1152 i_diff = ( i_cc - pid->i_cc )&0x0f;
1153 if( b_payload && i_diff == 1 )
1159 if( pid->i_cc == 0xff )
1161 msg_Warn( p_demux, "first packet for pid=0x%x cc=0x%x",
1165 else if( i_diff != 0 )
1167 /* FIXME what to do when discontinuity_indicator is set ? */
1168 msg_Warn( p_demux, "discontinuity received 0x%x instead of 0x%x",
1169 i_cc, ( pid->i_cc + 1 )&0x0f );
1173 if( pid->es->p_pes )
1175 pid->es->p_pes->i_flags |= BLOCK_FLAG_DISCONTINUITY;
1180 PCRHandle( p_demux, pid, p_bk );
1182 if( i_skip >= 188 || pid->es->id == NULL || p_demux->p_sys->b_udp_out )
1184 block_Release( p_bk );
1188 const vlc_bool_t b_unit_start= p[1]&0x40;
1190 /* we have to gather it */
1191 p_bk->p_buffer += i_skip;
1192 p_bk->i_buffer -= i_skip;
1196 if( pid->es->p_pes )
1198 ParsePES( p_demux, pid );
1202 block_ChainLastAppend( &pid->es->pp_last, p_bk );
1203 if( p_bk->i_buffer > 6 )
1205 pid->es->i_pes_size = GetWBE( &p_bk->p_buffer[4] );
1206 if( pid->es->i_pes_size > 0 )
1208 pid->es->i_pes_size += 6;
1211 pid->es->i_pes_gathered += p_bk->i_buffer;
1212 if( pid->es->i_pes_size > 0 &&
1213 pid->es->i_pes_gathered >= pid->es->i_pes_size )
1215 ParsePES( p_demux, pid );
1221 if( pid->es->p_pes == NULL )
1223 /* msg_Dbg( p_demux, "broken packet" ); */
1224 block_Release( p_bk );
1228 block_ChainLastAppend( &pid->es->pp_last, p_bk );
1229 pid->es->i_pes_gathered += p_bk->i_buffer;
1230 if( pid->es->i_pes_size > 0 &&
1231 pid->es->i_pes_gathered >= pid->es->i_pes_size )
1233 ParsePES( p_demux, pid );
1243 static int PIDFillFormat( ts_pid_t *pid, int i_stream_type )
1245 es_format_t *fmt = &pid->es->fmt;
1247 switch( i_stream_type )
1249 case 0x01: /* MPEG-1 video */
1250 case 0x02: /* MPEG-2 video */
1251 case 0x80: /* MPEG-2 MOTO video */
1252 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', 'g', 'v' ) );
1254 case 0x03: /* MPEG-1 audio */
1255 case 0x04: /* MPEG-2 audio */
1256 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
1258 case 0x11: /* MPEG4 (audio) */
1259 case 0x0f: /* ISO/IEC 13818-7 Audio with ADTS transport syntax */
1260 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', '4', 'a' ) );
1262 case 0x10: /* MPEG4 (video) */
1263 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', '4', 'v' ) );
1264 pid->es->b_gather = VLC_TRUE;
1266 case 0x1B: /* H264 <- check transport syntax/needed descriptor */
1267 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'h', '2', '6', '4' ) );
1270 case 0x81: /* A52 (audio) */
1271 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', ' ' ) );
1273 case 0x82: /* DVD_SPU (sub) */
1274 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', ' ' ) );
1276 case 0x83: /* LPCM (audio) */
1277 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'l', 'p', 'c', 'm' ) );
1279 case 0x84: /* SDDS (audio) */
1280 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 's' ) );
1282 case 0x85: /* DTS (audio) */
1283 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'd', 't', 's', ' ' ) );
1286 case 0x91: /* A52 vls (audio) */
1287 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', 'b' ) );
1289 case 0x92: /* DVD_SPU vls (sub) */
1290 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', 'b' ) );
1292 case 0x93: /* LPCM vls (audio) */
1293 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'l', 'p', 'c', 'b' ) );
1295 case 0x94: /* SDDS (audio) */
1296 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 'b' ) );
1299 case 0xa0: /* MSCODEC vlc (video) (fixed later) */
1300 es_format_Init( fmt, UNKNOWN_ES, 0 );
1301 pid->es->b_gather = VLC_TRUE;
1304 case 0x06: /* PES_PRIVATE (fixed later) */
1305 case 0x12: /* MPEG-4 generic (sub/scene/...) (fixed later) */
1307 es_format_Init( fmt, UNKNOWN_ES, 0 );
1311 /* PES packets usually contain truncated frames */
1312 fmt->b_packetized = VLC_FALSE;
1314 return fmt->i_cat == UNKNOWN_ES ? VLC_EGENERIC : VLC_SUCCESS ;
1317 /*****************************************************************************
1318 * MP4 specific functions (IOD parser)
1319 *****************************************************************************/
1320 static int IODDescriptorLength( int *pi_data, uint8_t **pp_data )
1323 unsigned int i_len = 0;
1329 i_len = ( i_len << 7 ) + ( i_b&0x7f );
1331 } while( i_b&0x80 );
1335 static int IODGetByte( int *pi_data, uint8_t **pp_data )
1339 const int i_b = **pp_data;
1346 static int IODGetWord( int *pi_data, uint8_t **pp_data )
1348 const int i1 = IODGetByte( pi_data, pp_data );
1349 const int i2 = IODGetByte( pi_data, pp_data );
1350 return( ( i1 << 8 ) | i2 );
1352 static int IODGet3Bytes( int *pi_data, uint8_t **pp_data )
1354 const int i1 = IODGetByte( pi_data, pp_data );
1355 const int i2 = IODGetByte( pi_data, pp_data );
1356 const int i3 = IODGetByte( pi_data, pp_data );
1358 return( ( i1 << 16 ) | ( i2 << 8) | i3 );
1361 static uint32_t IODGetDWord( int *pi_data, uint8_t **pp_data )
1363 const uint32_t i1 = IODGetWord( pi_data, pp_data );
1364 const uint32_t i2 = IODGetWord( pi_data, pp_data );
1365 return( ( i1 << 16 ) | i2 );
1368 static char* IODGetURL( int *pi_data, uint8_t **pp_data )
1373 i_url_len = IODGetByte( pi_data, pp_data );
1374 url = malloc( i_url_len + 1 );
1375 for( i = 0; i < i_url_len; i++ )
1377 url[i] = IODGetByte( pi_data, pp_data );
1379 url[i_url_len] = '\0';
1383 static iod_descriptor_t *IODNew( int i_data, uint8_t *p_data )
1385 iod_descriptor_t *p_iod;
1392 p_iod = malloc( sizeof( iod_descriptor_t ) );
1393 memset( p_iod, 0, sizeof( iod_descriptor_t ) );
1395 fprintf( stderr, "\n************ IOD ************" );
1396 for( i = 0; i < 255; i++ )
1398 p_iod->es_descr[i].b_ok = 0;
1407 p_iod->i_iod_label = IODGetByte( &i_data, &p_data );
1408 fprintf( stderr, "\n* iod_label:%d", p_iod->i_iod_label );
1409 fprintf( stderr, "\n* ===========" );
1410 fprintf( stderr, "\n* tag:0x%x", p_data[0] );
1412 if( IODGetByte( &i_data, &p_data ) != 0x02 )
1414 fprintf( stderr, "\n ERR: tag != 0x02" );
1418 i_iod_length = IODDescriptorLength( &i_data, &p_data );
1419 fprintf( stderr, "\n* length:%d", i_iod_length );
1420 if( i_iod_length > i_data )
1422 i_iod_length = i_data;
1425 p_iod->i_od_id = ( IODGetByte( &i_data, &p_data ) << 2 );
1426 i_flags = IODGetByte( &i_data, &p_data );
1427 p_iod->i_od_id |= i_flags >> 6;
1428 b_url = ( i_flags >> 5 )&0x01;
1430 fprintf( stderr, "\n* od_id:%d", p_iod->i_od_id );
1431 fprintf( stderr, "\n* url flag:%d", b_url );
1432 fprintf( stderr, "\n* includeInlineProfileLevel flag:%d", ( i_flags >> 4 )&0x01 );
1436 p_iod->psz_url = IODGetURL( &i_data, &p_data );
1437 fprintf( stderr, "\n* url string:%s", p_iod->psz_url );
1438 fprintf( stderr, "\n*****************************\n" );
1443 p_iod->psz_url = NULL;
1446 p_iod->i_ODProfileLevelIndication = IODGetByte( &i_data, &p_data );
1447 p_iod->i_sceneProfileLevelIndication = IODGetByte( &i_data, &p_data );
1448 p_iod->i_audioProfileLevelIndication = IODGetByte( &i_data, &p_data );
1449 p_iod->i_visualProfileLevelIndication = IODGetByte( &i_data, &p_data );
1450 p_iod->i_graphicsProfileLevelIndication = IODGetByte( &i_data, &p_data );
1452 fprintf( stderr, "\n* ODProfileLevelIndication:%d", p_iod->i_ODProfileLevelIndication );
1453 fprintf( stderr, "\n* sceneProfileLevelIndication:%d", p_iod->i_sceneProfileLevelIndication );
1454 fprintf( stderr, "\n* audioProfileLevelIndication:%d", p_iod->i_audioProfileLevelIndication );
1455 fprintf( stderr, "\n* visualProfileLevelIndication:%d", p_iod->i_visualProfileLevelIndication );
1456 fprintf( stderr, "\n* graphicsProfileLevelIndication:%d", p_iod->i_graphicsProfileLevelIndication );
1459 while( i_data > 0 && i_es_index < 255)
1461 int i_tag, i_length;
1463 uint8_t *p_data_sav;
1465 i_tag = IODGetByte( &i_data, &p_data );
1466 i_length = IODDescriptorLength( &i_data, &p_data );
1468 i_data_sav = i_data;
1469 p_data_sav = p_data;
1477 #define es_descr p_iod->es_descr[i_es_index]
1478 int i_decoderConfigDescr_length;
1479 fprintf( stderr, "\n* - ES_Descriptor length:%d", i_length );
1482 es_descr.i_es_id = IODGetWord( &i_data, &p_data );
1483 i_flags = IODGetByte( &i_data, &p_data );
1484 es_descr.b_streamDependenceFlag = ( i_flags >> 7 )&0x01;
1485 b_url = ( i_flags >> 6 )&0x01;
1486 es_descr.b_OCRStreamFlag = ( i_flags >> 5 )&0x01;
1487 es_descr.i_streamPriority = i_flags & 0x1f;
1488 fprintf( stderr, "\n* * streamDependenceFlag:%d", es_descr.b_streamDependenceFlag );
1489 fprintf( stderr, "\n* * OCRStreamFlag:%d", es_descr.b_OCRStreamFlag );
1490 fprintf( stderr, "\n* * streamPriority:%d", es_descr.i_streamPriority );
1492 if( es_descr.b_streamDependenceFlag )
1494 es_descr.i_dependOn_es_id = IODGetWord( &i_data, &p_data );
1495 fprintf( stderr, "\n* * dependOn_es_id:%d", es_descr.i_dependOn_es_id );
1500 es_descr.psz_url = IODGetURL( &i_data, &p_data );
1501 fprintf( stderr, "\n* url string:%s", es_descr.psz_url );
1505 es_descr.psz_url = NULL;
1508 if( es_descr.b_OCRStreamFlag )
1510 es_descr.i_OCR_es_id = IODGetWord( &i_data, &p_data );
1511 fprintf( stderr, "\n* * OCR_es_id:%d", es_descr.i_OCR_es_id );
1514 if( IODGetByte( &i_data, &p_data ) != 0x04 )
1516 fprintf( stderr, "\n* ERR missing DecoderConfigDescr" );
1520 i_decoderConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
1522 fprintf( stderr, "\n* - DecoderConfigDesc length:%d", i_decoderConfigDescr_length );
1523 #define dec_descr es_descr.dec_descr
1524 dec_descr.i_objectTypeIndication = IODGetByte( &i_data, &p_data );
1525 i_flags = IODGetByte( &i_data, &p_data );
1526 dec_descr.i_streamType = i_flags >> 2;
1527 dec_descr.b_upStream = ( i_flags >> 1 )&0x01;
1528 dec_descr.i_bufferSizeDB = IODGet3Bytes( &i_data, &p_data );
1529 dec_descr.i_maxBitrate = IODGetDWord( &i_data, &p_data );
1530 dec_descr.i_avgBitrate = IODGetDWord( &i_data, &p_data );
1531 fprintf( stderr, "\n* * objectTypeIndication:0x%x", dec_descr.i_objectTypeIndication );
1532 fprintf( stderr, "\n* * streamType:0x%x", dec_descr.i_streamType );
1533 fprintf( stderr, "\n* * upStream:%d", dec_descr.b_upStream );
1534 fprintf( stderr, "\n* * bufferSizeDB:%d", dec_descr.i_bufferSizeDB );
1535 fprintf( stderr, "\n* * maxBitrate:%d", dec_descr.i_maxBitrate );
1536 fprintf( stderr, "\n* * avgBitrate:%d", dec_descr.i_avgBitrate );
1537 if( i_decoderConfigDescr_length > 13 && IODGetByte( &i_data, &p_data ) == 0x05 )
1540 dec_descr.i_decoder_specific_info_len =
1541 IODDescriptorLength( &i_data, &p_data );
1542 if( dec_descr.i_decoder_specific_info_len > 0 )
1544 dec_descr.p_decoder_specific_info =
1545 malloc( dec_descr.i_decoder_specific_info_len );
1547 for( i = 0; i < dec_descr.i_decoder_specific_info_len; i++ )
1549 dec_descr.p_decoder_specific_info[i] = IODGetByte( &i_data, &p_data );
1554 dec_descr.i_decoder_specific_info_len = 0;
1555 dec_descr.p_decoder_specific_info = NULL;
1559 #define sl_descr es_descr.sl_descr
1561 int i_SLConfigDescr_length;
1564 if( IODGetByte( &i_data, &p_data ) != 0x06 )
1566 fprintf( stderr, "\n* ERR missing SLConfigDescr" );
1570 i_SLConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
1572 fprintf( stderr, "\n* - SLConfigDescr length:%d", i_SLConfigDescr_length );
1573 i_predefined = IODGetByte( &i_data, &p_data );
1574 fprintf( stderr, "\n* * i_predefined:0x%x", i_predefined );
1575 switch( i_predefined )
1579 sl_descr.b_useAccessUnitStartFlag = 0;
1580 sl_descr.b_useAccessUnitEndFlag = 0;
1581 sl_descr.b_useRandomAccessPointFlag = 0;
1582 //sl_descr.b_useRandomAccessUnitsOnlyFlag = 0;
1583 sl_descr.b_usePaddingFlag = 0;
1584 sl_descr.b_useTimeStampsFlags = 0;
1585 sl_descr.b_useIdleFlag = 0;
1586 sl_descr.b_durationFlag = 0; // FIXME FIXME
1587 sl_descr.i_timeStampResolution = 1000;
1588 sl_descr.i_OCRResolution = 0; // FIXME FIXME
1589 sl_descr.i_timeStampLength = 32;
1590 sl_descr.i_OCRLength = 0; // FIXME FIXME
1591 sl_descr.i_AU_Length = 0;
1592 sl_descr.i_instantBitrateLength= 0; // FIXME FIXME
1593 sl_descr.i_degradationPriorityLength= 0;
1594 sl_descr.i_AU_seqNumLength = 0;
1595 sl_descr.i_packetSeqNumLength = 0;
1596 if( sl_descr.b_durationFlag )
1598 sl_descr.i_timeScale = 0; // FIXME FIXME
1599 sl_descr.i_accessUnitDuration = 0; // FIXME FIXME
1600 sl_descr.i_compositionUnitDuration= 0; // FIXME FIXME
1602 if( !sl_descr.b_useTimeStampsFlags )
1604 sl_descr.i_startDecodingTimeStamp = 0; // FIXME FIXME
1605 sl_descr.i_startCompositionTimeStamp= 0; // FIXME FIXME
1610 fprintf( stderr, "\n* ERR unsupported SLConfigDescr predefined" );
1619 fprintf( stderr, "\n* - OD tag:0x%x length:%d (Unsupported)", i_tag, i_length );
1623 p_data = p_data_sav + i_length;
1624 i_data = i_data_sav - i_length;
1629 fprintf( stderr, "\n*****************************\n" );
1633 static void IODFree( iod_descriptor_t *p_iod )
1637 if( p_iod->psz_url )
1639 free( p_iod->psz_url );
1640 p_iod->psz_url = NULL;
1645 for( i = 0; i < 255; i++ )
1647 #define es_descr p_iod->es_descr[i]
1650 if( es_descr.psz_url )
1652 free( es_descr.psz_url );
1653 es_descr.psz_url = NULL;
1657 if( es_descr.dec_descr.p_decoder_specific_info != NULL )
1659 free( es_descr.dec_descr.p_decoder_specific_info );
1660 es_descr.dec_descr.p_decoder_specific_info = NULL;
1661 es_descr.dec_descr.i_decoder_specific_info_len = 0;
1671 /****************************************************************************
1672 ****************************************************************************
1673 ** libdvbpsi callbacks
1674 ****************************************************************************
1675 ****************************************************************************/
1676 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt )
1678 demux_sys_t *p_sys = p_demux->p_sys;
1679 dvbpsi_descriptor_t *p_dr;
1680 dvbpsi_pmt_es_t *p_es;
1682 ts_pid_t *pmt = NULL;
1683 ts_prg_psi_t *prg = NULL;
1686 msg_Dbg( p_demux, "PMTCallBack called" );
1688 /* First find this PMT declared in PAT */
1689 for( i = 0; i < p_sys->i_pmt; i++ )
1692 for( i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
1694 if( p_sys->pmt[i]->psi->prg[i_prg]->i_number == p_pmt->i_program_number )
1696 pmt = p_sys->pmt[i];
1697 prg = p_sys->pmt[i]->psi->prg[i_prg];
1707 msg_Warn( p_demux, "unreferenced program (broken stream)" );
1708 dvbpsi_DeletePMT(p_pmt);
1712 if( prg->i_version != -1 &&
1713 ( !p_pmt->b_current_next || prg->i_version == p_pmt->i_version ) )
1715 dvbpsi_DeletePMT( p_pmt );
1719 /* Clean this program (remove all es) */
1720 for( i = 0; i < 8192; i++ )
1722 ts_pid_t *pid = &p_sys->pid[i];
1724 if( pid->b_valid && pid->p_owner == pmt->psi && pid->i_owner_number == prg->i_number && pid->psi == NULL )
1726 PIDClean( p_demux->out, pid );
1731 IODFree( prg->iod );
1735 msg_Dbg( p_demux, "new PMT program number=%d version=%d pid_pcr=0x%x",
1736 p_pmt->i_program_number, p_pmt->i_version, p_pmt->i_pcr_pid );
1737 prg->i_pid_pcr = p_pmt->i_pcr_pid;
1738 prg->i_version = p_pmt->i_version;
1740 /* Parse descriptor */
1741 for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
1743 if( p_dr->i_tag == 0x1d )
1745 /* We have found an IOD descriptor */
1746 msg_Warn( p_demux, " * descriptor : IOD (0x1d)" );
1748 prg->iod = IODNew( p_dr->i_length, p_dr->p_data );
1752 msg_Dbg( p_demux, " * descriptor : unknown (0x%x)", p_dr->i_tag );
1756 for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
1758 ts_pid_t *pid = &p_sys->pid[p_es->i_pid];
1762 msg_Warn( p_demux, "pmt error: pid=0x%x already defined",
1767 PIDInit( pid, VLC_FALSE, pmt->psi );
1768 PIDFillFormat( pid, p_es->i_type );
1769 pid->i_owner_number = prg->i_number;
1771 if( p_es->i_type == 0x10 || p_es->i_type == 0x11 || p_es->i_type == 0x12 )
1773 /* MPEG-4 stream: search SL_DESCRIPTOR */
1774 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
1776 while( p_dr && ( p_dr->i_tag != 0x1f ) ) p_dr = p_dr->p_next;
1778 if( p_dr && p_dr->i_length == 2 )
1781 int i_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
1783 msg_Warn( p_demux, "found SL_descriptor es_id=%d", i_es_id );
1785 pid->es->p_mpeg4desc = NULL;
1787 for( i = 0; i < 255; i++ )
1789 iod_descriptor_t *iod = prg->iod;
1791 if( iod->es_descr[i].b_ok &&
1792 iod->es_descr[i].i_es_id == i_es_id )
1794 pid->es->p_mpeg4desc = &iod->es_descr[i];
1800 if( pid->es->p_mpeg4desc != NULL )
1802 decoder_config_descriptor_t *dcd =
1803 &pid->es->p_mpeg4desc->dec_descr;
1805 if( dcd->i_streamType == 0x04 ) /* VisualStream */
1807 pid->es->fmt.i_cat = VIDEO_ES;
1808 switch( dcd->i_objectTypeIndication )
1810 case 0x0B: /* mpeg4 sub */
1811 pid->es->fmt.i_cat = SPU_ES;
1812 pid->es->fmt.i_codec = VLC_FOURCC('s','u','b','t');
1815 case 0x20: /* mpeg4 */
1816 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','v');
1823 case 0x65: /* mpeg2 */
1824 pid->es->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1826 case 0x6a: /* mpeg1 */
1827 pid->es->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1829 case 0x6c: /* mpeg1 */
1830 pid->es->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
1833 pid->es->fmt.i_cat = UNKNOWN_ES;
1837 else if( dcd->i_streamType == 0x05 ) /* AudioStream */
1839 pid->es->fmt.i_cat = AUDIO_ES;
1840 switch( dcd->i_objectTypeIndication )
1842 case 0x40: /* mpeg4 */
1843 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','a');
1847 case 0x68: /* mpeg2 aac */
1848 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','a');
1850 case 0x69: /* mpeg2 */
1851 pid->es->fmt.i_codec = VLC_FOURCC('m','p','g','a');
1853 case 0x6b: /* mpeg1 */
1854 pid->es->fmt.i_codec = VLC_FOURCC('m','p','g','a');
1857 pid->es->fmt.i_cat = UNKNOWN_ES;
1863 pid->es->fmt.i_cat = UNKNOWN_ES;
1866 if( pid->es->fmt.i_cat != UNKNOWN_ES )
1868 pid->es->fmt.i_extra = dcd->i_decoder_specific_info_len;
1869 if( pid->es->fmt.i_extra > 0 )
1871 pid->es->fmt.p_extra = malloc( pid->es->fmt.i_extra );
1872 memcpy( pid->es->fmt.p_extra,
1873 dcd->p_decoder_specific_info,
1874 pid->es->fmt.i_extra );
1879 else if( p_es->i_type == 0x06 )
1881 dvbpsi_descriptor_t *p_dr;
1883 for( p_dr = p_es->p_first_descriptor; p_dr != NULL;
1884 p_dr = p_dr->p_next )
1886 msg_Dbg( p_demux, " * es pid=0x%x type=0x%x dr->i_tag=0x%x",
1887 p_es->i_pid, p_es->i_type, p_dr->i_tag );
1889 if( p_dr->i_tag == 0x6a )
1891 pid->es->fmt.i_cat = AUDIO_ES;
1892 pid->es->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
1894 #ifdef _DVBPSI_DR_59_H_
1895 else if( p_dr->i_tag == 0x59 )
1898 dvbpsi_subtitling_dr_t *sub;
1901 pid->es->fmt.i_cat = SPU_ES;
1902 pid->es->fmt.i_codec = VLC_FOURCC( 'd', 'v', 'b', 's' );
1903 pid->es->fmt.i_group = p_pmt->i_program_number;
1905 sub = dvbpsi_DecodeSubtitlingDr( p_dr );
1906 if( !sub ) continue;
1908 /* Each subtitle ES contains n languages,
1909 * We are going to create n ES for the n tracks */
1910 if( sub->i_subtitles_number > 0 )
1912 pid->es->fmt.psz_language = malloc( 4 );
1913 memcpy( pid->es->fmt.psz_language,
1914 sub->p_subtitle[0].i_iso6392_language_code, 3);
1915 pid->es->fmt.psz_language[3] = 0;
1917 pid->es->fmt.subs.dvb.i_id =
1918 sub->p_subtitle[0].i_composition_page_id;
1920 else pid->es->fmt.i_cat = UNKNOWN_ES;
1922 for( n = 1; n < sub->i_subtitles_number; n++ )
1924 ts_es_t *p_es = malloc( sizeof( ts_es_t ) );
1925 p_es->fmt = pid->es->fmt;
1928 p_es->i_pes_size = 0;
1929 p_es->i_pes_gathered = 0;
1930 p_es->pp_last = &p_es->p_pes;
1931 p_es->p_mpeg4desc = NULL;
1933 p_es->fmt.psz_language = malloc( 4 );
1934 memcpy( p_es->fmt.psz_language,
1935 sub->p_subtitle[n].i_iso6392_language_code, 3);
1936 p_es->fmt.psz_language[3] = 0;
1938 p_es->fmt.subs.dvb.i_id =
1939 sub->p_subtitle[n].i_composition_page_id;
1941 TAB_APPEND( pid->i_extra_es, pid->extra_es, p_es );
1944 #endif /* _DVBPSI_DR_59_H_ */
1947 else if( p_es->i_type == 0xa0 )
1949 /* MSCODEC sent by vlc */
1950 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
1952 while( p_dr && ( p_dr->i_tag != 0xa0 ) ) p_dr = p_dr->p_next;
1954 if( p_dr && p_dr->i_length >= 8 )
1956 pid->es->fmt.i_cat = VIDEO_ES;
1957 pid->es->fmt.i_codec =
1958 VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
1959 p_dr->p_data[2], p_dr->p_data[3] );
1960 pid->es->fmt.video.i_width =
1961 ( p_dr->p_data[4] << 8 ) | p_dr->p_data[5];
1962 pid->es->fmt.video.i_height =
1963 ( p_dr->p_data[6] << 8 ) | p_dr->p_data[7];
1964 pid->es->fmt.i_extra =
1965 (p_dr->p_data[8] << 8) | p_dr->p_data[9];
1967 if( pid->es->fmt.i_extra > 0 )
1969 pid->es->fmt.p_extra = malloc( pid->es->fmt.i_extra );
1970 memcpy( pid->es->fmt.p_extra, &p_dr->p_data[10],
1971 pid->es->fmt.i_extra );
1976 msg_Warn( p_demux, "private MSCODEC (vlc) without bih private "
1979 /* For such stream we will gather them ourself and don't launch a
1981 * Yes it's ugly but it's the only way to have DIV3 working */
1982 pid->es->fmt.b_packetized = VLC_TRUE;
1985 if( pid->es->fmt.i_cat == AUDIO_ES ||
1986 ( pid->es->fmt.i_cat == SPU_ES &&
1987 pid->es->fmt.i_codec != VLC_FOURCC('d','v','b','s') ) )
1989 /* get language descriptor */
1990 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
1991 while( p_dr && ( p_dr->i_tag != 0x0a ) ) p_dr = p_dr->p_next;
1995 dvbpsi_iso639_dr_t *p_decoded = dvbpsi_DecodeISO639Dr( p_dr );
1999 pid->es->fmt.psz_language = malloc( 4 );
2000 memcpy( pid->es->fmt.psz_language,
2001 p_decoded->i_iso_639_code, 3 );
2002 pid->es->fmt.psz_language[3] = 0;
2007 pid->es->fmt.i_group = p_pmt->i_program_number;
2008 if( pid->es->fmt.i_cat == UNKNOWN_ES )
2010 msg_Dbg( p_demux, " * es pid=0x%x type=0x%x *unknown*",
2011 p_es->i_pid, p_es->i_type );
2013 else if( !p_sys->b_udp_out )
2017 msg_Dbg( p_demux, " * es pid=0x%x type=0x%x fcc=%4.4s",
2018 p_es->i_pid, p_es->i_type, (char*)&pid->es->fmt.i_codec );
2020 if( p_sys->b_es_id_pid )
2022 pid->es->fmt.i_id = p_es->i_pid;
2025 pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
2027 for( i = 0; i < pid->i_extra_es; i++ )
2029 pid->extra_es[i]->id =
2030 es_out_Add( p_demux->out, &pid->extra_es[i]->fmt);
2034 dvbpsi_DeletePMT(p_pmt);
2037 static void PATCallBack( demux_t *p_demux, dvbpsi_pat_t *p_pat )
2039 demux_sys_t *p_sys = p_demux->p_sys;
2040 dvbpsi_pat_program_t *p_program;
2041 ts_pid_t *pat = &p_sys->pid[0];
2044 msg_Dbg( p_demux, "PATCallBack called" );
2046 if( pat->psi->i_pat_version != -1 &&
2047 ( !p_pat->b_current_next || p_pat->i_version == pat->psi->i_pat_version ) )
2049 dvbpsi_DeletePAT( p_pat );
2053 msg_Dbg( p_demux, "new PAT ts_id=0x%x version=%d current_next=%d",
2054 p_pat->i_ts_id, p_pat->i_version, p_pat->b_current_next );
2057 if( p_sys->i_pmt > 0 )
2060 ts_pid_t **pmt_rm = NULL;
2062 /* Search pmt to be deleted */
2063 for( i = 0; i < p_sys->i_pmt; i++ )
2065 ts_pid_t *pmt = p_sys->pmt[i];
2066 vlc_bool_t b_keep = VLC_FALSE;
2068 for( p_program = p_pat->p_first_program; p_program != NULL;
2069 p_program = p_program->p_next )
2071 if( p_program->i_pid == pmt->i_pid )
2074 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
2076 if( p_program->i_number == pmt->psi->prg[i_prg]->i_number )
2088 TAB_APPEND( i_pmt_rm, pmt_rm, pmt );
2092 /* Delete all ES attached to thoses PMT */
2093 for( i = 2; i < 8192; i++ )
2095 ts_pid_t *pid = &p_sys->pid[i];
2097 if( !pid->b_valid || pid->psi ) continue;
2099 for( j = 0; j < i_pmt_rm; j++ )
2102 for( i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
2104 if( pid->p_owner->prg[i_prg]->i_pid_pcr == pmt_rm[j]->i_pid &&
2107 /* We only remove es that aren't defined by extra pmt */
2108 PIDClean( p_demux->out, pid );
2117 /* Delete PMT pid */
2118 for( i = 0; i < i_pmt_rm; i++ )
2120 PIDClean( p_demux->out, &p_sys->pid[pmt_rm[i]->i_pid] );
2121 TAB_REMOVE( p_sys->i_pmt, p_sys->pmt, pmt_rm[i] );
2129 /* now create programs */
2130 for( p_program = p_pat->p_first_program; p_program != NULL;
2131 p_program = p_program->p_next )
2133 msg_Dbg( p_demux, " * number=%d pid=0x%x", p_program->i_number,
2135 if( p_program->i_number != 0 )
2137 ts_pid_t *pmt = &p_sys->pid[p_program->i_pid];
2138 vlc_bool_t b_add = VLC_TRUE;
2143 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
2145 if( pmt->psi->prg[i_prg]->i_number == p_program->i_number )
2154 TAB_APPEND( p_sys->i_pmt, p_sys->pmt, pmt );
2159 PIDInit( pmt, VLC_TRUE, pat->psi );
2160 pmt->psi->prg[pmt->psi->i_prg-1]->handle =
2161 dvbpsi_AttachPMT( p_program->i_number,
2162 (dvbpsi_pmt_callback)PMTCallBack, p_demux );
2163 pmt->psi->prg[pmt->psi->i_prg-1]->i_number = p_program->i_number;
2167 pat->psi->i_pat_version = p_pat->i_version;
2169 dvbpsi_DeletePAT( p_pat );