1 /*****************************************************************************
2 * ts.c: Transport Stream input module for VLC.
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
5 * $Id: ts.c,v 1.2 2004/01/16 02:01:11 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>
34 /* Include dvbpsi headers */
35 #ifdef HAVE_DVBPSI_DR_H
36 # include <dvbpsi/dvbpsi.h>
37 # include <dvbpsi/descriptor.h>
38 # include <dvbpsi/pat.h>
39 # include <dvbpsi/pmt.h>
40 # include <dvbpsi/dr.h>
41 # include <dvbpsi/psi.h>
44 # include "descriptor.h"
45 # include "tables/pat.h"
46 # include "tables/pmt.h"
47 # include "descriptors/dr.h"
56 /*****************************************************************************
58 *****************************************************************************/
59 static int Open ( vlc_object_t * );
60 static void Close ( vlc_object_t * );
63 set_description( _("ISO 13818-1 MPEG Transport Stream input - new" ) );
64 add_category_hint( "TS demuxer", NULL, VLC_TRUE );
65 add_string( "ts-extra-pmt", NULL, NULL, "extra PMT", "allow user to specify an extra pmt (pmt_pid=pid:stream_type[,...])", VLC_TRUE );
66 set_capability( "demux2", 10 );
67 set_callbacks( Open, Close );
68 add_shortcut( "ts2" );
71 /*****************************************************************************
73 *****************************************************************************/
77 uint8_t i_objectTypeIndication;
79 vlc_bool_t b_upStream;
80 uint32_t i_bufferSizeDB;
81 uint32_t i_maxBitrate;
82 uint32_t i_avgBitrate;
84 int i_decoder_specific_info_len;
85 uint8_t *p_decoder_specific_info;
87 } decoder_config_descriptor_t;
91 vlc_bool_t b_useAccessUnitStartFlag;
92 vlc_bool_t b_useAccessUnitEndFlag;
93 vlc_bool_t b_useRandomAccessPointFlag;
94 vlc_bool_t b_useRandomAccessUnitsOnlyFlag;
95 vlc_bool_t b_usePaddingFlag;
96 vlc_bool_t b_useTimeStampsFlags;
97 vlc_bool_t b_useIdleFlag;
98 vlc_bool_t b_durationFlag;
99 uint32_t i_timeStampResolution;
100 uint32_t i_OCRResolution;
101 uint8_t i_timeStampLength;
104 uint8_t i_instantBitrateLength;
105 uint8_t i_degradationPriorityLength;
106 uint8_t i_AU_seqNumLength;
107 uint8_t i_packetSeqNumLength;
109 uint32_t i_timeScale;
110 uint16_t i_accessUnitDuration;
111 uint16_t i_compositionUnitDuration;
113 uint64_t i_startDecodingTimeStamp;
114 uint64_t i_startCompositionTimeStamp;
116 } sl_config_descriptor_t;
123 vlc_bool_t b_streamDependenceFlag;
124 vlc_bool_t b_OCRStreamFlag;
125 uint8_t i_streamPriority;
129 uint16_t i_dependOn_es_id;
130 uint16_t i_OCR_es_id;
132 decoder_config_descriptor_t dec_descr;
133 sl_config_descriptor_t sl_descr;
134 } es_mpeg4_descriptor_t;
144 uint8_t i_ODProfileLevelIndication;
145 uint8_t i_sceneProfileLevelIndication;
146 uint8_t i_audioProfileLevelIndication;
147 uint8_t i_visualProfileLevelIndication;
148 uint8_t i_graphicsProfileLevelIndication;
150 es_mpeg4_descriptor_t es_descr[255];
159 dvbpsi_handle handle;
161 /* IOD stuff (mpeg4) */
162 iod_descriptor_t *iod;
172 es_mpeg4_descriptor_t *p_mpeg4desc;
181 int i_cc; /* countinuity counter */
183 /* PSI owner (ie PMT -> PAT, ES -> PMT */
202 static int Demux ( demux_t *p_demux );
203 static int Control( demux_t *p_demux, int i_query, va_list args );
205 static void PIDInit( ts_pid_t *pid, vlc_bool_t b_psi, ts_psi_t *p_owner );
206 static void PATCallBack( demux_t *, dvbpsi_pat_t * );
207 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt );
209 static inline int PIDGet( block_t *p )
211 return ( (p->p_buffer[1]&0x1f)<<8 )|p->p_buffer[2];
214 static vlc_bool_t GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk );
215 static char *LanguageNameISO639( char *p );
217 static iod_descriptor_t *IODNew( int , uint8_t * );
218 static void IODFree( iod_descriptor_t * );
220 static int ESFillFormat( es_format_t *fmt, int i_stream_type );
222 /*****************************************************************************
224 *****************************************************************************/
225 static int Open( vlc_object_t *p_this )
227 demux_t *p_demux = (demux_t*)p_this;
238 if( ( i_peek = stream_Peek( p_demux->s, &p_peek, 189 ) ) < 1 )
240 msg_Err( p_demux, "cannot peek" );
243 if( p_peek[0] != 0x47 || ( i_peek >= 189 && p_peek[188] != 0x47 ) )
245 if( strcmp( p_demux->psz_demux, "ts" ) )
247 msg_Warn( p_demux, "TS module discarded" );
250 msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
253 /* Fill p_demux field */
254 p_demux->pf_demux = Demux;
255 p_demux->pf_control = Control;
256 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
257 memset( p_sys, 0, sizeof( demux_sys_t ) );
259 /* Init p_sys field */
260 for( i = 0; i < 8192; i++ )
262 ts_pid_t *pid = &p_sys->pid[i];
265 pid->b_seen = VLC_FALSE;
266 pid->b_valid = VLC_FALSE;
269 /* Init PAT handler */
270 pat = &p_sys->pid[0];
271 PIDInit( pat, VLC_TRUE, NULL );
272 pat->psi->handle = dvbpsi_AttachPAT( (dvbpsi_pat_callback)PATCallBack, p_demux );
278 /* We handle description of an extra PMT */
279 var_Create( p_demux, "ts-extra-pmt", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
280 var_Get( p_demux, "ts-extra-pmt", &val );
281 if( val.psz_string && strchr( val.psz_string, '=' ) != NULL )
283 char *psz = val.psz_string;
284 int i_pid = strtol( psz, &psz, 0 );
286 if( i_pid >= 2 && i_pid < 8192 )
288 ts_pid_t *pmt = &p_sys->pid[i_pid];
290 msg_Dbg( p_demux, "Extra pmt specified (pid=0x%x)", i_pid );
291 PIDInit( pmt, VLC_TRUE, NULL );
292 /* FIXME we should also ask for a number */
293 pmt->psi->handle = dvbpsi_AttachPMT( 1, (dvbpsi_pmt_callback)PMTCallBack, p_demux );
294 pmt->psi->i_number = 0; /* special one */
296 psz = strchr( psz, '=' ) + 1; /* can't failed */
299 char *psz_next = strchr( psz, ',' );
300 int i_pid, i_stream_type;
307 i_pid = strtol( psz, &psz, 0 );
310 i_stream_type = strtol( psz + 1, &psz, 0 );
311 if( i_pid >= 2 && i_pid < 8192 && !p_sys->pid[i_pid].b_valid )
313 ts_pid_t *pid = &p_sys->pid[i_pid];
315 PIDInit( pid, VLC_FALSE, pmt->psi);
316 if( pmt->psi->i_pid_pcr <= 0 )
318 pmt->psi->i_pid_pcr = i_pid;
320 ESFillFormat( &pid->es->fmt, i_stream_type);
321 if( pid->es->fmt.i_cat != UNKNOWN_ES )
323 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 );
324 pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
334 free( val.psz_string );
340 /*****************************************************************************
342 *****************************************************************************/
343 static void Close( vlc_object_t *p_this )
345 demux_t *p_demux = (demux_t*)p_this;
346 demux_sys_t *p_sys = p_demux->p_sys;
350 msg_Dbg( p_demux, "pid list:" );
351 for( i = 0; i < 8192; i++ )
353 ts_pid_t *pid = &p_sys->pid[i];
355 if( pid->b_valid && pid->psi )
360 dvbpsi_DetachPAT( pid->psi->handle );
365 dvbpsi_DetachPMT( pid->psi->handle );
368 IODFree( pid->psi->iod );
374 else if( pid->b_valid && pid->es )
378 block_ChainRelease( pid->es->p_pes );
384 msg_Dbg( p_demux, " - pid[0x%x] seen", pid->i_pid );
392 /*****************************************************************************
394 *****************************************************************************/
395 static int Demux( demux_t *p_demux )
397 demux_sys_t *p_sys = p_demux->p_sys;
400 /* We read at most 100 TS packet or until a frame is completed */
401 for( i_pkt = 0; i_pkt < 100; i_pkt++ )
403 vlc_bool_t b_frame = VLC_FALSE;
407 /* Get a new TS packet */
408 if( ( p_pkt = stream_Block( p_demux->s, 188 ) ) == NULL )
410 msg_Dbg( p_demux, "eof ?" );
413 if( p_pkt->p_buffer[0] != 0x47 )
415 msg_Warn( p_demux, "lost synchro" );
416 block_Release( p_pkt );
422 int i_peek = stream_Peek( p_demux->s, &p_peek, 1880 );
424 vlc_bool_t b_ok = VLC_FALSE;
428 msg_Dbg( p_demux, "eof ?" );
432 while( i_skip < i_peek - 188 )
434 if( p_peek[i_skip] == 0x47 && p_peek[i_skip+188] == 0x47 )
441 stream_Read( p_demux->s, NULL, i_skip );
442 msg_Dbg( p_demux, "%d bytes of garbage", i_skip );
448 if( ( p_pkt = stream_Block( p_demux->s, 188 ) ) == NULL )
450 msg_Dbg( p_demux, "eof ?" );
455 /* Parse the TS packet */
456 p_pid = &p_sys->pid[PIDGet( p_pkt )];
462 dvbpsi_PushPacket( p_pid->psi->handle, p_pkt->p_buffer );
463 block_Release( p_pkt );
467 b_frame = GatherPES( p_demux, p_pid, p_pkt );
474 msg_Dbg( p_demux, "pid[0x%x] unknown", p_pid->i_pid );
476 block_Release( p_pkt );
478 p_pid->b_seen = VLC_TRUE;
489 /*****************************************************************************
491 *****************************************************************************/
492 static int Control( demux_t *p_demux, int i_query, va_list args )
494 demux_sys_t *p_sys = p_demux->p_sys;
500 case DEMUX_GET_POSITION:
501 pf = (double*) va_arg( args, double* );
502 i64 = stream_Size( p_demux->s );
505 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
512 case DEMUX_SET_POSITION:
513 f = (double) va_arg( args, double );
514 i64 = stream_Size( p_demux->s );
516 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
517 if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) )
525 pi64 = (int64_t*)va_arg( args, int64_t * );
526 if( p_sys->i_time < 0 )
531 *pi64 = p_sys->i_time;
535 case DEMUX_GET_LENGTH:
536 pi64 = (int64_t*)va_arg( args, int64_t * );
537 if( p_sys->i_mux_rate > 0 )
539 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
548 pf = (double*)va_arg( args, double * );
549 *pf = (double)1000000.0 / (double)p_sys->i_pcr_inc;
557 static void PIDInit( ts_pid_t *pid, vlc_bool_t b_psi, ts_psi_t *p_owner )
559 pid->b_valid = VLC_TRUE;
561 pid->p_owner = p_owner;
565 pid->psi = malloc( sizeof( ts_psi_t ) );
568 pid->psi->i_version = -1;
569 pid->psi->i_number = -1;
570 pid->psi->i_pid_pcr = -1;
571 pid->psi->handle = NULL;
572 pid->psi->iod = NULL;
577 pid->es = malloc( sizeof( ts_es_t ) );
579 es_format_Init( &pid->es->fmt, UNKNOWN_ES, 0 );
581 pid->es->p_pes = NULL;
582 pid->es->p_mpeg4desc = NULL;
586 /****************************************************************************
588 ****************************************************************************/
589 static void ParsePES ( demux_t *p_demux, ts_pid_t *pid )
591 block_t *p_pes = pid->es->p_pes;
599 /* remove the pes from pid */
600 pid->es->p_pes = NULL;
602 /* FIXME find real max size */
603 i_max = block_ChainExtract( p_pes, header, 30 );
605 if( header[0] != 0 || header[1] != 0 || header[2] != 1 )
607 msg_Err( p_demux, "invalid header [0x%x:%x:%x:%x]", header[0], header[1],header[2],header[3] );
608 block_ChainRelease( p_pes );
612 i_pes_size = (header[4] << 8)|header[5];
614 /* TODO check size */
618 case 0xBC: /* Program stream map */
619 case 0xBE: /* Padding */
620 case 0xBF: /* Private stream 2 */
623 case 0xFF: /* Program stream directory */
624 case 0xF2: /* DSMCC stream */
625 case 0xF8: /* ITU-T H.222.1 type E stream */
629 if( ( header[6]&0xC0 ) == 0x80 )
632 i_skip = header[8] + 9;
634 if( header[7]&0x80 ) /* has pts */
636 i_pts = ((mtime_t)(header[ 9]&0x0e ) << 29)|
637 (mtime_t)(header[10] << 22)|
638 ((mtime_t)(header[11]&0xfe) << 14)|
639 (mtime_t)(header[12] << 7)|
640 (mtime_t)(header[12] >> 1);
642 if( header[7]&0x40 ) /* has dts */
644 i_dts = ((mtime_t)(header[14]&0x0e ) << 29)|
645 (mtime_t)(header[15] << 22)|
646 ((mtime_t)(header[16]&0xfe) << 14)|
647 (mtime_t)(header[17] << 7)|
648 (mtime_t)(header[18] >> 1);
655 while( i_skip < 23 && header[i_skip] == 0xff )
661 msg_Err( p_demux, "too much MPEG-1 stuffing" );
662 block_ChainRelease( p_pes );
665 if( ( header[i_skip] & 0xC0 ) == 0x40 )
670 if( header[i_skip]&0x20 )
672 p_pes->i_pts = ((mtime_t)(header[i_skip]&0x0e ) << 29)|
673 (mtime_t)(header[i_skip+1] << 22)|
674 ((mtime_t)(header[i_skip+2]&0xfe) << 14)|
675 (mtime_t)(header[i_skip+3] << 7)|
676 (mtime_t)(header[i_skip+4] >> 1);
678 if( header[i_skip]&0x10 ) /* has dts */
680 p_pes->i_dts = ((mtime_t)(header[i_skip+5]&0x0e ) << 29)|
681 (mtime_t)(header[i_skip+6] << 22)|
682 ((mtime_t)(header[i_skip+7]&0xfe) << 14)|
683 (mtime_t)(header[i_skip+8] << 7)|
684 (mtime_t)(header[i_skip+9] >> 1);
700 if( pid->es->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', 'b' ) ||
701 pid->es->fmt.i_codec == VLC_FOURCC( 'd', 't', 's', 'b' ) )
705 else if( pid->es->fmt.i_codec == VLC_FOURCC( 'l', 'p', 'c', 'b' ) ||
706 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'p', 'u', 'b' ) ||
707 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'd', 'd', 'b' ) )
713 while( p_pes && i_skip > 0 )
715 if( p_pes->i_buffer <= i_skip )
717 block_t *p_next = p_pes->p_next;
719 i_skip -= p_pes->i_buffer;
720 block_Release( p_pes );
725 p_pes->i_buffer -= i_skip;
726 p_pes->p_buffer += i_skip;
734 p_pes->i_dts = i_dts * 100 / 9;
738 p_pes->i_pts = i_pts * 100 / 9;
740 if( pid->es->p_mpeg4desc )
742 /* For mpeg4 we first gather the packet -> will make ffmpeg happier */
743 es_out_Send( p_demux->out, pid->es->id, block_ChainGather( p_pes ) );
747 es_out_Send( p_demux->out, pid->es->id, p_pes );
752 msg_Warn( p_demux, "empty pes" );
756 static vlc_bool_t GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
758 uint8_t *p = p_bk->p_buffer;
760 vlc_bool_t b_unit_start= p[1]&0x40;
761 vlc_bool_t b_adaptation= p[3]&0x20;
762 vlc_bool_t b_payload = p[3]&0x10;
763 int i_cc = p[3]&0x0f; /* continuity counter */
764 /* transport_scrambling_control is ignored */
767 vlc_bool_t i_ret = VLC_FALSE;
771 //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);
774 msg_Dbg( p_demux, "transport_error_indicator set (pid=0x%x)", pid->i_pid );
783 /* p[4] is adaptation length */
789 msg_Warn( p_demux, "discontinuity_indicator (pid=0x%x) ignored", pid->i_pid );
793 /* test continuity counter */
794 /* continuous when (one of this):
796 * diff == 0 and payload == 0
797 * diff == 0 and duplicate packet (playload != 0) <- do we should test the content ?
800 i_diff = ( i_cc - pid->i_cc )&0x0f;
801 if( b_payload && i_diff == 1 )
807 if( pid->i_cc == 0xff )
809 msg_Warn( p_demux, "first packet for pid=0x%x c=0x%x", pid->i_pid, i_cc );
812 else if( i_diff != 0 )
814 /* FIXME what to do when discontinuity_indicator is set ? */
815 msg_Warn( p_demux, "discontinuity received 0x%x instead of 0x%x",
816 i_cc, ( pid->i_cc + 1 )&0x0f );
822 block_ChainRelease( pid->es->p_pes );
823 pid->es->p_pes = NULL;
829 (p[5] & 0x10) && p[4]>=7 && pid->p_owner && pid->p_owner->i_pid_pcr == pid->i_pid )
831 mtime_t i_pcr; /* 33 bits */
833 i_pcr = ( (mtime_t)p[6] << 25 ) |
834 ( (mtime_t)p[7] << 17 ) |
835 ( (mtime_t)p[8] << 9 ) |
836 ( (mtime_t)p[9] << 1 ) |
837 ( (mtime_t)p[10] >> 7 );
839 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR, (int)pid->p_owner->i_number, (int64_t)(i_pcr * 100 / 9) );
843 if( i_skip >= 188 || pid->es->id == NULL )
845 block_Release( p_bk );
849 /* we have to gather it */
850 p_bk->p_buffer += i_skip;
851 p_bk->i_buffer -= i_skip;
857 ParsePES( p_demux, pid );
861 pid->es->p_pes = p_bk;
865 if( pid->es->p_pes == NULL )
867 /* msg_Dbg( p_demux, "broken packet" ); */
868 block_Release( p_bk );
872 /* TODO check if when have gather enough packet to form a PES (ie read PES size)*/
873 block_ChainAppend( &pid->es->p_pes, p_bk );
881 static char *LanguageNameISO639( char *psz_code )
883 const iso639_lang_t *pl;
885 pl = GetLang_2B( psz_code );
886 if( !strcmp( pl->psz_iso639_1, "??" ) )
888 pl = GetLang_2T( psz_code );
891 if( !strcmp( pl->psz_iso639_1, "??" ) )
893 return strdup( psz_code );
897 if( *pl->psz_native_name )
899 return strdup( pl->psz_native_name );
901 return strdup( pl->psz_eng_name );
905 static int ESFillFormat( es_format_t *fmt, int i_stream_type )
907 switch( i_stream_type )
909 case 0x01: /* MPEG-1 video */
910 case 0x02: /* MPEG-2 video */
911 case 0x80: /* MPEG-2 MOTO video */
912 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', 'g', 'v' ) );
914 case 0x03: /* MPEG-1 audio */
915 case 0x04: /* MPEG-2 audio */
916 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
918 case 0x11: /* MPEG4 (audio) */
919 case 0x0f: /* ISO/IEC 13818-7 Audio with ADTS transport syntax */
920 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', '4', 'a' ) );
922 case 0x10: /* MPEG4 (video) */
923 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', '4', 'v' ) );
925 case 0x1B: /* H264 <- check transport syntax/needed descriptor */
926 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'h', '2', '6', '4' ) );
929 case 0x81: /* A52 (audio) */
930 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', ' ' ) );
932 case 0x82: /* DVD_SPU (sub) */
933 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', ' ' ) );
935 case 0x83: /* LPCM (audio) */
936 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'l', 'p', 'c', 'm' ) );
938 case 0x84: /* SDDS (audio) */
939 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 's' ) );
941 case 0x85: /* DTS (audio) */
942 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'd', 't', 's', ' ' ) );
945 case 0x91: /* A52 vls (audio) */
946 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', 'b' ) );
948 case 0x92: /* DVD_SPU vls (sub) */
949 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', 'b' ) );
951 case 0x93: /* LPCM vls (audio) */
952 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'l', 'p', 'c', 'b' ) );
954 case 0x94: /* SDDS (audio) */
955 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 'b' ) );
958 case 0x06: /* PES_PRIVATE (fixed later) */
959 case 0xa0: /* MSCODEC vlc (video) (fixed later) */
961 es_format_Init( fmt, UNKNOWN_ES, 0 );
965 return fmt->i_cat == UNKNOWN_ES ? VLC_EGENERIC : VLC_SUCCESS ;
968 /*****************************************************************************
969 * MP4 specific functions
970 *****************************************************************************/
971 static int IODDescriptorLength( int *pi_data, uint8_t **pp_data )
974 unsigned int i_len = 0;
980 i_len = ( i_len << 7 ) + ( i_b&0x7f );
986 static int IODGetByte( int *pi_data, uint8_t **pp_data )
1001 static int MP4_GetWord( int *pi_data, uint8_t **pp_data )
1004 i1 = IODGetByte( pi_data, pp_data );
1005 i2 = IODGetByte( pi_data, pp_data );
1006 return( ( i1 << 8 ) | i2 );
1008 static int IODGet3Bytes( int *pi_data, uint8_t **pp_data )
1011 i1 = IODGetByte( pi_data, pp_data );
1012 i2 = IODGetByte( pi_data, pp_data );
1013 i3 = IODGetByte( pi_data, pp_data );
1014 return( ( i1 << 16 ) | ( i2 << 8) | i3 );
1017 static uint32_t IODGetWord( int *pi_data, uint8_t **pp_data )
1020 i1 = MP4_GetWord( pi_data, pp_data );
1021 i2 = MP4_GetWord( pi_data, pp_data );
1022 return( ( i1 << 16 ) | i2 );
1025 static char* IODGetURL( int *pi_data, uint8_t **pp_data )
1030 i_url_len = IODGetByte( pi_data, pp_data );
1031 url = malloc( i_url_len + 1 );
1032 for( i = 0; i < i_url_len; i++ )
1034 url[i] = IODGetByte( pi_data, pp_data );
1036 url[i_url_len] = '\0';
1040 static iod_descriptor_t *IODNew( int i_data, uint8_t *p_data )
1042 iod_descriptor_t *p_iod;
1049 p_iod = malloc( sizeof( iod_descriptor_t ) );
1050 memset( p_iod, 0, sizeof( iod_descriptor_t ) );
1052 fprintf( stderr, "\n************ IOD ************" );
1053 for( i = 0; i < 255; i++ )
1055 p_iod->es_descr[i].b_ok = 0;
1064 p_iod->i_iod_label = IODGetByte( &i_data, &p_data );
1065 fprintf( stderr, "\n* iod_label:%d", p_iod->i_iod_label );
1066 fprintf( stderr, "\n* ===========" );
1067 fprintf( stderr, "\n* tag:0x%x", p_data[0] );
1069 if( IODGetByte( &i_data, &p_data ) != 0x02 )
1071 fprintf( stderr, "\n ERR: tag != 0x02" );
1075 i_iod_length = IODDescriptorLength( &i_data, &p_data );
1076 fprintf( stderr, "\n* length:%d", i_iod_length );
1077 if( i_iod_length > i_data )
1079 i_iod_length = i_data;
1082 p_iod->i_od_id = ( IODGetByte( &i_data, &p_data ) << 2 );
1083 i_flags = IODGetByte( &i_data, &p_data );
1084 p_iod->i_od_id |= i_flags >> 6;
1085 b_url = ( i_flags >> 5 )&0x01;
1087 fprintf( stderr, "\n* od_id:%d", p_iod->i_od_id );
1088 fprintf( stderr, "\n* url flag:%d", b_url );
1089 fprintf( stderr, "\n* includeInlineProfileLevel flag:%d", ( i_flags >> 4 )&0x01 );
1093 p_iod->psz_url = IODGetURL( &i_data, &p_data );
1094 fprintf( stderr, "\n* url string:%s", p_iod->psz_url );
1095 fprintf( stderr, "\n*****************************\n" );
1100 p_iod->psz_url = NULL;
1103 p_iod->i_ODProfileLevelIndication = IODGetByte( &i_data, &p_data );
1104 p_iod->i_sceneProfileLevelIndication = IODGetByte( &i_data, &p_data );
1105 p_iod->i_audioProfileLevelIndication = IODGetByte( &i_data, &p_data );
1106 p_iod->i_visualProfileLevelIndication = IODGetByte( &i_data, &p_data );
1107 p_iod->i_graphicsProfileLevelIndication = IODGetByte( &i_data, &p_data );
1109 fprintf( stderr, "\n* ODProfileLevelIndication:%d", p_iod->i_ODProfileLevelIndication );
1110 fprintf( stderr, "\n* sceneProfileLevelIndication:%d", p_iod->i_sceneProfileLevelIndication );
1111 fprintf( stderr, "\n* audioProfileLevelIndication:%d", p_iod->i_audioProfileLevelIndication );
1112 fprintf( stderr, "\n* visualProfileLevelIndication:%d", p_iod->i_visualProfileLevelIndication );
1113 fprintf( stderr, "\n* graphicsProfileLevelIndication:%d", p_iod->i_graphicsProfileLevelIndication );
1116 while( i_data > 0 && i_es_index < 255)
1118 int i_tag, i_length;
1120 uint8_t *p_data_sav;
1122 i_tag = IODGetByte( &i_data, &p_data );
1123 i_length = IODDescriptorLength( &i_data, &p_data );
1125 i_data_sav = i_data;
1126 p_data_sav = p_data;
1134 #define es_descr p_iod->es_descr[i_es_index]
1135 int i_decoderConfigDescr_length;
1136 fprintf( stderr, "\n* - ES_Descriptor length:%d", i_length );
1139 es_descr.i_es_id = MP4_GetWord( &i_data, &p_data );
1140 i_flags = IODGetByte( &i_data, &p_data );
1141 es_descr.b_streamDependenceFlag = ( i_flags >> 7 )&0x01;
1142 b_url = ( i_flags >> 6 )&0x01;
1143 es_descr.b_OCRStreamFlag = ( i_flags >> 5 )&0x01;
1144 es_descr.i_streamPriority = i_flags & 0x1f;
1145 fprintf( stderr, "\n* * streamDependenceFlag:%d", es_descr.b_streamDependenceFlag );
1146 fprintf( stderr, "\n* * OCRStreamFlag:%d", es_descr.b_OCRStreamFlag );
1147 fprintf( stderr, "\n* * streamPriority:%d", es_descr.i_streamPriority );
1149 if( es_descr.b_streamDependenceFlag )
1151 es_descr.i_dependOn_es_id = MP4_GetWord( &i_data, &p_data );
1152 fprintf( stderr, "\n* * dependOn_es_id:%d", es_descr.i_dependOn_es_id );
1157 es_descr.psz_url = IODGetURL( &i_data, &p_data );
1158 fprintf( stderr, "\n* url string:%s", es_descr.psz_url );
1162 es_descr.psz_url = NULL;
1165 if( es_descr.b_OCRStreamFlag )
1167 es_descr.i_OCR_es_id = MP4_GetWord( &i_data, &p_data );
1168 fprintf( stderr, "\n* * OCR_es_id:%d", es_descr.i_OCR_es_id );
1171 if( IODGetByte( &i_data, &p_data ) != 0x04 )
1173 fprintf( stderr, "\n* ERR missing DecoderConfigDescr" );
1177 i_decoderConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
1179 fprintf( stderr, "\n* - DecoderConfigDesc length:%d", i_decoderConfigDescr_length );
1180 #define dec_descr es_descr.dec_descr
1181 dec_descr.i_objectTypeIndication = IODGetByte( &i_data, &p_data );
1182 i_flags = IODGetByte( &i_data, &p_data );
1183 dec_descr.i_streamType = i_flags >> 2;
1184 dec_descr.b_upStream = ( i_flags >> 1 )&0x01;
1185 dec_descr.i_bufferSizeDB = IODGet3Bytes( &i_data, &p_data );
1186 dec_descr.i_maxBitrate = IODGetWord( &i_data, &p_data );
1187 dec_descr.i_avgBitrate = IODGetWord( &i_data, &p_data );
1188 fprintf( stderr, "\n* * objectTypeIndication:0x%x", dec_descr.i_objectTypeIndication );
1189 fprintf( stderr, "\n* * streamType:0x%x", dec_descr.i_streamType );
1190 fprintf( stderr, "\n* * upStream:%d", dec_descr.b_upStream );
1191 fprintf( stderr, "\n* * bufferSizeDB:%d", dec_descr.i_bufferSizeDB );
1192 fprintf( stderr, "\n* * maxBitrate:%d", dec_descr.i_maxBitrate );
1193 fprintf( stderr, "\n* * avgBitrate:%d", dec_descr.i_avgBitrate );
1194 if( i_decoderConfigDescr_length > 13 && IODGetByte( &i_data, &p_data ) == 0x05 )
1197 dec_descr.i_decoder_specific_info_len =
1198 IODDescriptorLength( &i_data, &p_data );
1199 if( dec_descr.i_decoder_specific_info_len > 0 )
1201 dec_descr.p_decoder_specific_info =
1202 malloc( dec_descr.i_decoder_specific_info_len );
1204 for( i = 0; i < dec_descr.i_decoder_specific_info_len; i++ )
1206 dec_descr.p_decoder_specific_info[i] = IODGetByte( &i_data, &p_data );
1211 dec_descr.i_decoder_specific_info_len = 0;
1212 dec_descr.p_decoder_specific_info = NULL;
1216 #define sl_descr es_descr.sl_descr
1218 int i_SLConfigDescr_length;
1221 if( IODGetByte( &i_data, &p_data ) != 0x06 )
1223 fprintf( stderr, "\n* ERR missing SLConfigDescr" );
1227 i_SLConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
1229 fprintf( stderr, "\n* - SLConfigDescr length:%d", i_SLConfigDescr_length );
1230 i_predefined = IODGetByte( &i_data, &p_data );
1231 fprintf( stderr, "\n* * i_predefined:0x%x", i_predefined );
1232 switch( i_predefined )
1236 sl_descr.b_useAccessUnitStartFlag = 0;
1237 sl_descr.b_useAccessUnitEndFlag = 0;
1238 sl_descr.b_useRandomAccessPointFlag = 0;
1239 //sl_descr.b_useRandomAccessUnitsOnlyFlag = 0;
1240 sl_descr.b_usePaddingFlag = 0;
1241 sl_descr.b_useTimeStampsFlags = 0;
1242 sl_descr.b_useIdleFlag = 0;
1243 sl_descr.b_durationFlag = 0; // FIXME FIXME
1244 sl_descr.i_timeStampResolution = 1000;
1245 sl_descr.i_OCRResolution = 0; // FIXME FIXME
1246 sl_descr.i_timeStampLength = 32;
1247 sl_descr.i_OCRLength = 0; // FIXME FIXME
1248 sl_descr.i_AU_Length = 0;
1249 sl_descr.i_instantBitrateLength= 0; // FIXME FIXME
1250 sl_descr.i_degradationPriorityLength= 0;
1251 sl_descr.i_AU_seqNumLength = 0;
1252 sl_descr.i_packetSeqNumLength = 0;
1253 if( sl_descr.b_durationFlag )
1255 sl_descr.i_timeScale = 0; // FIXME FIXME
1256 sl_descr.i_accessUnitDuration = 0; // FIXME FIXME
1257 sl_descr.i_compositionUnitDuration= 0; // FIXME FIXME
1259 if( !sl_descr.b_useTimeStampsFlags )
1261 sl_descr.i_startDecodingTimeStamp = 0; // FIXME FIXME
1262 sl_descr.i_startCompositionTimeStamp= 0; // FIXME FIXME
1267 fprintf( stderr, "\n* ERR unsupported SLConfigDescr predefined" );
1276 fprintf( stderr, "\n* - OD tag:0x%x length:%d (Unsupported)", i_tag, i_length );
1280 p_data = p_data_sav + i_length;
1281 i_data = i_data_sav - i_length;
1286 fprintf( stderr, "\n*****************************\n" );
1290 static void IODFree( iod_descriptor_t *p_iod )
1294 if( p_iod->psz_url )
1296 free( p_iod->psz_url );
1297 p_iod->psz_url = NULL;
1302 for( i = 0; i < 255; i++ )
1304 #define es_descr p_iod->es_descr[i]
1307 if( es_descr.psz_url )
1309 free( es_descr.psz_url );
1310 es_descr.psz_url = NULL;
1314 if( es_descr.dec_descr.p_decoder_specific_info != NULL )
1316 free( es_descr.dec_descr.p_decoder_specific_info );
1317 es_descr.dec_descr.p_decoder_specific_info = NULL;
1318 es_descr.dec_descr.i_decoder_specific_info_len = 0;
1328 /****************************************************************************
1329 ****************************************************************************
1330 ** libdvbpsi callbacks
1331 ****************************************************************************
1332 ****************************************************************************/
1333 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt )
1335 demux_sys_t *p_sys = p_demux->p_sys;
1336 dvbpsi_descriptor_t *p_dr;
1337 dvbpsi_pmt_es_t *p_es;
1339 ts_pid_t *pmt = NULL;
1342 msg_Dbg( p_demux, "PMTCallBack called" );
1343 /* First find this PMT declared in PAT */
1344 for( i = 0; i < p_sys->i_pmt; i++ )
1346 if( p_sys->pmt[i]->psi->i_number == p_pmt->i_program_number )
1348 pmt = p_sys->pmt[i];
1353 msg_Warn( p_demux, "unreferenced program (broken stream)" );
1354 dvbpsi_DeletePMT(p_pmt);
1358 if( pmt->psi->i_version != -1 && ( !p_pmt->b_current_next || pmt->psi->i_version == p_pmt->i_version ) )
1360 dvbpsi_DeletePMT( p_pmt );
1364 /* Clean this program (remove all es) */
1365 for( i = 0; i < 8192; i++ )
1367 ts_pid_t *pid = &p_sys->pid[i];
1369 if( pid->b_valid && pid->p_owner == pmt->psi && pid->psi == NULL )
1371 pid->b_valid = VLC_FALSE;
1374 es_out_Del( p_demux->out, pid->es->id );
1376 if( pid->es->p_pes )
1378 block_ChainRelease( pid->es->p_pes );
1384 IODFree( pmt->psi->iod );
1385 pmt->psi->iod = NULL;
1388 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 );
1389 pmt->psi->i_pid_pcr = p_pmt->i_pcr_pid;
1390 pmt->psi->i_version = p_pmt->i_version;
1392 /* Parse descriptor */
1393 for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
1395 if( p_dr->i_tag == 0x1d )
1397 /* We have found an IOD descriptor */
1398 msg_Warn( p_demux, "found IOD descriptor" );
1400 pmt->psi->iod = IODNew( p_dr->i_length, p_dr->p_data );
1404 for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
1406 ts_pid_t *pid = &p_sys->pid[p_es->i_pid];
1410 msg_Warn( p_demux, "pmt error: pid=0x%x already defined", p_es->i_pid );
1414 PIDInit( pid, VLC_FALSE, pmt->psi );
1415 ESFillFormat( &pid->es->fmt, p_es->i_type );
1417 if( p_es->i_type == 0x10 || p_es->i_type == 0x11 )
1419 /* MPEG-4 stream: search SL_DESCRIPTOR */
1420 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
1422 while( p_dr && ( p_dr->i_tag != 0x1f ) ) p_dr = p_dr->p_next;
1424 if( p_dr && p_dr->i_length == 2 )
1427 int i_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
1429 msg_Warn( p_demux, "found SL_descriptor es_id=%d", i_es_id );
1431 pid->es->p_mpeg4desc = NULL;
1433 for( i = 0; i < 255; i++ )
1435 iod_descriptor_t *iod = pmt->psi->iod;
1437 if( iod->es_descr[i].b_ok &&
1438 iod->es_descr[i].i_es_id == i_es_id )
1440 pid->es->p_mpeg4desc = &iod->es_descr[i];
1446 if( pid->es->p_mpeg4desc != NULL )
1448 decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;
1450 if( dcd->i_streamType == 0x04 ) /* VisualStream */
1452 pid->es->fmt.i_cat = VIDEO_ES;
1453 switch( dcd->i_objectTypeIndication )
1456 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','v'); // mpeg4
1464 pid->es->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' ); // mpeg2
1467 pid->es->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' ); // mpeg1
1470 pid->es->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' ); // mpeg1
1474 pid->es->fmt.i_cat = UNKNOWN_ES;
1478 else if( dcd->i_streamType == 0x05 ) /* AudioStream */
1480 pid->es->fmt.i_cat = AUDIO_ES;
1481 switch( dcd->i_objectTypeIndication )
1484 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','a'); // mpeg4
1489 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','a');// mpeg2 aac
1492 pid->es->fmt.i_codec = VLC_FOURCC('m','p','g','a'); // mpeg2
1495 pid->es->fmt.i_codec = VLC_FOURCC('m','p','g','a'); // mpeg1
1498 pid->es->fmt.i_cat = UNKNOWN_ES;
1504 pid->es->fmt.i_cat = UNKNOWN_ES;
1507 if( pid->es->fmt.i_cat != UNKNOWN_ES )
1509 pid->es->fmt.i_extra = dcd->i_decoder_specific_info_len;
1510 if( pid->es->fmt.i_extra > 0 )
1512 pid->es->fmt.p_extra = malloc( pid->es->fmt.i_extra );
1513 memcpy( pid->es->fmt.p_extra,
1514 dcd->p_decoder_specific_info,
1515 pid->es->fmt.i_extra );
1520 else if( p_es->i_type == 0x06 )
1522 dvbpsi_descriptor_t *p_dr;
1524 for( p_dr = p_es->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
1526 if( p_dr->i_tag == 0x6a )
1528 pid->es->fmt.i_cat = AUDIO_ES;
1529 pid->es->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
1531 #ifdef _DVBPSI_DR_59_H_
1532 else if( p_dr->i_tag == 0x59 )
1540 else if( p_es->i_type == 0xa0 )
1542 /* MSCODEC send by vlc */
1543 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
1545 while( p_dr && ( p_dr->i_tag != 0xa0 ) ) p_dr = p_dr->p_next;
1547 if( p_dr && p_dr->i_length >= 8 )
1549 pid->es->fmt.i_codec = VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
1550 p_dr->p_data[2], p_dr->p_data[3] );
1551 pid->es->fmt.i_extra = (p_dr->p_data[8] << 8) | p_dr->p_data[9];
1553 if( pid->es->fmt.i_extra > 0 )
1555 pid->es->fmt.p_extra = malloc( pid->es->fmt.i_extra );
1556 memcpy( pid->es->fmt.p_extra, &p_dr->p_data[10], pid->es->fmt.i_extra );
1561 msg_Warn( p_demux, "private MSCODEC (vlc) without bih private descriptor" );
1565 if( pid->es->fmt.i_cat == AUDIO_ES || pid->es->fmt.i_cat == SPU_ES )
1567 /* get language descriptor */
1568 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
1569 while( p_dr && ( p_dr->i_tag != 0x0a ) ) p_dr = p_dr->p_next;
1573 dvbpsi_iso639_dr_t *p_decoded = dvbpsi_DecodeISO639Dr( p_dr );
1574 pid->es->fmt.psz_language =
1575 LanguageNameISO639( (char*)&p_decoded->i_iso_639_code );
1579 pid->es->fmt.i_group = p_pmt->i_program_number;
1580 if( pid->es->fmt.i_cat == UNKNOWN_ES )
1582 msg_Dbg( p_demux, " * es pid=0x%x type=0x%x *unknown*", p_es->i_pid, p_es->i_type );
1586 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 );
1587 pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
1590 dvbpsi_DeletePMT(p_pmt);
1593 static void PATCallBack( demux_t *p_demux, dvbpsi_pat_t *p_pat )
1595 demux_sys_t *p_sys = p_demux->p_sys;
1596 dvbpsi_pat_program_t *p_program;
1597 ts_pid_t *pat = &p_sys->pid[0];
1600 msg_Dbg( p_demux, "PATCallBack called" );
1602 if( pat->psi->i_version != -1 && ( !p_pat->b_current_next || p_pat->i_version == pat->psi->i_version ) )
1604 dvbpsi_DeletePAT( p_pat );
1608 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 );
1611 for( i = 2; i < 8192; i++ )
1613 ts_pid_t *pid = &p_sys->pid[i];
1619 if( pid->p_owner == pat->psi )
1621 dvbpsi_DetachPMT( pid->psi->handle );
1624 IODFree( pid->psi->iod );
1625 pid->psi->iod = NULL;
1627 pid->b_valid = VLC_FALSE;
1628 TAB_REMOVE( p_sys->i_pmt, p_sys->pmt, pid );
1631 else if( pid->p_owner && pid->p_owner->i_number != 0 && pid->es->id )
1633 /* We only remove es that aren't defined by extra pmt */
1634 es_out_Del( p_demux->out, pid->es->id );
1636 pid->b_valid = VLC_FALSE;
1641 /* now create programs */
1642 for( p_program = p_pat->p_first_program; p_program != NULL; p_program = p_program->p_next )
1644 msg_Dbg( p_demux, " * number=%d pid=0x%x", p_program->i_number, p_program->i_pid );
1645 if( p_program->i_number != 0 )
1647 ts_pid_t *pmt = &p_sys->pid[p_program->i_pid];
1649 PIDInit( pmt, VLC_TRUE, pat->psi );
1650 pmt->psi->handle = dvbpsi_AttachPMT( p_program->i_number, (dvbpsi_pmt_callback)PMTCallBack, p_demux );
1651 pmt->psi->i_number = p_program->i_number;
1653 TAB_APPEND( p_sys->i_pmt, p_sys->pmt, pmt );
1656 pat->psi->i_version = p_pat->i_version;
1658 dvbpsi_DeletePAT( p_pat );