1 /*****************************************************************************
2 * ts.c: Transport Stream input module for VLC.
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
5 * $Id: ts.c,v 1.5 2004/01/19 18:16:32 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"
52 * - XXX: do not mark options message to be translated, they are too osbcure for now ...
57 /*****************************************************************************
59 *****************************************************************************/
60 static int Open ( vlc_object_t * );
61 static void Close ( vlc_object_t * );
64 set_description( _("ISO 13818-1 MPEG Transport Stream input - new" ) );
65 add_category_hint( "TS demuxer", NULL, VLC_TRUE );
66 add_string( "ts-extra-pmt", NULL, NULL, "extra PMT", "allow user to specify an extra pmt (pmt_pid=pid:stream_type[,...])", VLC_TRUE );
67 add_bool( "ts-es-id-pid", 0, NULL, "set id of es to pid", "set id of es to pid", VLC_TRUE );
68 set_capability( "demux2", 10 );
69 set_callbacks( Open, Close );
70 add_shortcut( "ts2" );
73 /*****************************************************************************
75 *****************************************************************************/
79 uint8_t i_objectTypeIndication;
81 vlc_bool_t b_upStream;
82 uint32_t i_bufferSizeDB;
83 uint32_t i_maxBitrate;
84 uint32_t i_avgBitrate;
86 int i_decoder_specific_info_len;
87 uint8_t *p_decoder_specific_info;
89 } decoder_config_descriptor_t;
93 vlc_bool_t b_useAccessUnitStartFlag;
94 vlc_bool_t b_useAccessUnitEndFlag;
95 vlc_bool_t b_useRandomAccessPointFlag;
96 vlc_bool_t b_useRandomAccessUnitsOnlyFlag;
97 vlc_bool_t b_usePaddingFlag;
98 vlc_bool_t b_useTimeStampsFlags;
99 vlc_bool_t b_useIdleFlag;
100 vlc_bool_t b_durationFlag;
101 uint32_t i_timeStampResolution;
102 uint32_t i_OCRResolution;
103 uint8_t i_timeStampLength;
106 uint8_t i_instantBitrateLength;
107 uint8_t i_degradationPriorityLength;
108 uint8_t i_AU_seqNumLength;
109 uint8_t i_packetSeqNumLength;
111 uint32_t i_timeScale;
112 uint16_t i_accessUnitDuration;
113 uint16_t i_compositionUnitDuration;
115 uint64_t i_startDecodingTimeStamp;
116 uint64_t i_startCompositionTimeStamp;
118 } sl_config_descriptor_t;
125 vlc_bool_t b_streamDependenceFlag;
126 vlc_bool_t b_OCRStreamFlag;
127 uint8_t i_streamPriority;
131 uint16_t i_dependOn_es_id;
132 uint16_t i_OCR_es_id;
134 decoder_config_descriptor_t dec_descr;
135 sl_config_descriptor_t sl_descr;
136 } es_mpeg4_descriptor_t;
146 uint8_t i_ODProfileLevelIndication;
147 uint8_t i_sceneProfileLevelIndication;
148 uint8_t i_audioProfileLevelIndication;
149 uint8_t i_visualProfileLevelIndication;
150 uint8_t i_graphicsProfileLevelIndication;
152 es_mpeg4_descriptor_t es_descr[255];
161 dvbpsi_handle handle;
163 /* IOD stuff (mpeg4) */
164 iod_descriptor_t *iod;
174 es_mpeg4_descriptor_t *p_mpeg4desc;
183 int i_cc; /* countinuity counter */
185 /* PSI owner (ie PMT -> PAT, ES -> PMT */
204 vlc_bool_t b_es_id_pid;
207 static int Demux ( demux_t *p_demux );
208 static int Control( demux_t *p_demux, int i_query, va_list args );
211 static void PIDInit ( ts_pid_t *pid, vlc_bool_t b_psi, ts_psi_t *p_owner );
212 static void PIDClean( es_out_t *out, ts_pid_t *pid );
213 static int PIDFillFormat( ts_pid_t *pid, int i_stream_type );
215 static void PATCallBack( demux_t *, dvbpsi_pat_t * );
216 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt );
218 static inline int PIDGet( block_t *p )
220 return ( (p->p_buffer[1]&0x1f)<<8 )|p->p_buffer[2];
223 static vlc_bool_t GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk );
224 static char *LanguageNameISO639( char *p );
226 static iod_descriptor_t *IODNew( int , uint8_t * );
227 static void IODFree( iod_descriptor_t * );
230 /*****************************************************************************
232 *****************************************************************************/
233 static int Open( vlc_object_t *p_this )
235 demux_t *p_demux = (demux_t*)p_this;
246 if( ( i_peek = stream_Peek( p_demux->s, &p_peek, 189 ) ) < 1 )
248 msg_Err( p_demux, "cannot peek" );
251 if( p_peek[0] != 0x47 || ( i_peek >= 189 && p_peek[188] != 0x47 ) )
253 if( strcmp( p_demux->psz_demux, "ts" ) )
255 msg_Warn( p_demux, "TS module discarded" );
258 msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
261 /* Fill p_demux field */
262 p_demux->pf_demux = Demux;
263 p_demux->pf_control = Control;
264 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
265 memset( p_sys, 0, sizeof( demux_sys_t ) );
267 /* Init p_sys field */
268 for( i = 0; i < 8192; i++ )
270 ts_pid_t *pid = &p_sys->pid[i];
273 pid->b_seen = VLC_FALSE;
274 pid->b_valid = VLC_FALSE;
277 /* Init PAT handler */
278 pat = &p_sys->pid[0];
279 PIDInit( pat, VLC_TRUE, NULL );
280 pat->psi->handle = dvbpsi_AttachPAT( (dvbpsi_pat_callback)PATCallBack, p_demux );
286 var_Create( p_demux, "ts-es-id-pid", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
287 var_Get( p_demux, "ts-es-id-pid", &val );
288 p_sys->b_es_id_pid = val.b_bool,
291 /* We handle description of an extra PMT */
292 var_Create( p_demux, "ts-extra-pmt", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
293 var_Get( p_demux, "ts-extra-pmt", &val );
294 if( val.psz_string && strchr( val.psz_string, '=' ) != NULL )
296 char *psz = val.psz_string;
297 int i_pid = strtol( psz, &psz, 0 );
299 if( i_pid >= 2 && i_pid < 8192 )
301 ts_pid_t *pmt = &p_sys->pid[i_pid];
303 msg_Dbg( p_demux, "Extra pmt specified (pid=0x%x)", i_pid );
304 PIDInit( pmt, VLC_TRUE, NULL );
305 /* FIXME we should also ask for a number */
306 pmt->psi->handle = dvbpsi_AttachPMT( 1, (dvbpsi_pmt_callback)PMTCallBack, p_demux );
307 pmt->psi->i_number = 0; /* special one */
309 psz = strchr( psz, '=' ) + 1; /* can't failed */
312 char *psz_next = strchr( psz, ',' );
313 int i_pid, i_stream_type;
320 i_pid = strtol( psz, &psz, 0 );
323 i_stream_type = strtol( psz + 1, &psz, 0 );
324 if( i_pid >= 2 && i_pid < 8192 && !p_sys->pid[i_pid].b_valid )
326 ts_pid_t *pid = &p_sys->pid[i_pid];
328 PIDInit( pid, VLC_FALSE, pmt->psi);
329 if( pmt->psi->i_pid_pcr <= 0 )
331 pmt->psi->i_pid_pcr = i_pid;
333 PIDFillFormat( pid, i_stream_type);
334 if( pid->es->fmt.i_cat != UNKNOWN_ES )
336 if( p_sys->b_es_id_pid )
338 pid->es->fmt.i_id = i_pid;
340 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 );
341 pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
351 free( val.psz_string );
357 /*****************************************************************************
359 *****************************************************************************/
360 static void Close( vlc_object_t *p_this )
362 demux_t *p_demux = (demux_t*)p_this;
363 demux_sys_t *p_sys = p_demux->p_sys;
367 msg_Dbg( p_demux, "pid list:" );
368 for( i = 0; i < 8192; i++ )
370 ts_pid_t *pid = &p_sys->pid[i];
372 if( pid->b_valid && pid->psi )
377 dvbpsi_DetachPAT( pid->psi->handle );
382 dvbpsi_DetachPMT( pid->psi->handle );
385 IODFree( pid->psi->iod );
391 else if( pid->b_valid && pid->es )
395 block_ChainRelease( pid->es->p_pes );
401 msg_Dbg( p_demux, " - pid[0x%x] seen", pid->i_pid );
409 /*****************************************************************************
411 *****************************************************************************/
412 static int Demux( demux_t *p_demux )
414 demux_sys_t *p_sys = p_demux->p_sys;
417 /* We read at most 100 TS packet or until a frame is completed */
418 for( i_pkt = 0; i_pkt < 100; i_pkt++ )
420 vlc_bool_t b_frame = VLC_FALSE;
424 /* Get a new TS packet */
425 if( ( p_pkt = stream_Block( p_demux->s, 188 ) ) == NULL )
427 msg_Dbg( p_demux, "eof ?" );
430 if( p_pkt->p_buffer[0] != 0x47 )
432 msg_Warn( p_demux, "lost synchro" );
433 block_Release( p_pkt );
439 int i_peek = stream_Peek( p_demux->s, &p_peek, 1880 );
441 vlc_bool_t b_ok = VLC_FALSE;
445 msg_Dbg( p_demux, "eof ?" );
449 while( i_skip < i_peek - 188 )
451 if( p_peek[i_skip] == 0x47 && p_peek[i_skip+188] == 0x47 )
458 stream_Read( p_demux->s, NULL, i_skip );
459 msg_Dbg( p_demux, "%d bytes of garbage", i_skip );
465 if( ( p_pkt = stream_Block( p_demux->s, 188 ) ) == NULL )
467 msg_Dbg( p_demux, "eof ?" );
472 /* Parse the TS packet */
473 p_pid = &p_sys->pid[PIDGet( p_pkt )];
479 dvbpsi_PushPacket( p_pid->psi->handle, p_pkt->p_buffer );
480 block_Release( p_pkt );
484 b_frame = GatherPES( p_demux, p_pid, p_pkt );
491 msg_Dbg( p_demux, "pid[0x%x] unknown", p_pid->i_pid );
493 block_Release( p_pkt );
495 p_pid->b_seen = VLC_TRUE;
506 /*****************************************************************************
508 *****************************************************************************/
509 static int Control( demux_t *p_demux, int i_query, va_list args )
511 demux_sys_t *p_sys = p_demux->p_sys;
517 case DEMUX_GET_POSITION:
518 pf = (double*) va_arg( args, double* );
519 i64 = stream_Size( p_demux->s );
522 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
529 case DEMUX_SET_POSITION:
530 f = (double) va_arg( args, double );
531 i64 = stream_Size( p_demux->s );
533 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
534 if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) )
542 pi64 = (int64_t*)va_arg( args, int64_t * );
543 if( p_sys->i_time < 0 )
548 *pi64 = p_sys->i_time;
552 case DEMUX_GET_LENGTH:
553 pi64 = (int64_t*)va_arg( args, int64_t * );
554 if( p_sys->i_mux_rate > 0 )
556 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
565 pf = (double*)va_arg( args, double * );
566 *pf = (double)1000000.0 / (double)p_sys->i_pcr_inc;
574 static void PIDInit( ts_pid_t *pid, vlc_bool_t b_psi, ts_psi_t *p_owner )
576 pid->b_valid = VLC_TRUE;
578 pid->p_owner = p_owner;
582 pid->psi = malloc( sizeof( ts_psi_t ) );
585 pid->psi->i_version = -1;
586 pid->psi->i_number = -1;
587 pid->psi->i_pid_pcr = -1;
588 pid->psi->handle = NULL;
589 pid->psi->iod = NULL;
594 pid->es = malloc( sizeof( ts_es_t ) );
596 es_format_Init( &pid->es->fmt, UNKNOWN_ES, 0 );
598 pid->es->p_pes = NULL;
599 pid->es->p_mpeg4desc = NULL;
603 static void PIDClean( es_out_t *out, ts_pid_t *pid )
607 if( pid->psi->handle )
609 dvbpsi_DetachPMT( pid->psi->handle );
613 IODFree( pid->psi->iod );
621 es_out_Del( out, pid->es->id );
625 block_ChainRelease( pid->es->p_pes );
629 pid->b_valid = VLC_FALSE;
632 /****************************************************************************
634 ****************************************************************************/
635 static void ParsePES ( demux_t *p_demux, ts_pid_t *pid )
637 block_t *p_pes = pid->es->p_pes;
645 /* remove the pes from pid */
646 pid->es->p_pes = NULL;
648 /* FIXME find real max size */
649 i_max = block_ChainExtract( p_pes, header, 30 );
651 if( header[0] != 0 || header[1] != 0 || header[2] != 1 )
653 msg_Err( p_demux, "invalid header [0x%x:%x:%x:%x]", header[0], header[1],header[2],header[3] );
654 block_ChainRelease( p_pes );
658 i_pes_size = (header[4] << 8)|header[5];
660 /* TODO check size */
664 case 0xBC: /* Program stream map */
665 case 0xBE: /* Padding */
666 case 0xBF: /* Private stream 2 */
669 case 0xFF: /* Program stream directory */
670 case 0xF2: /* DSMCC stream */
671 case 0xF8: /* ITU-T H.222.1 type E stream */
675 if( ( header[6]&0xC0 ) == 0x80 )
678 i_skip = header[8] + 9;
680 if( header[7]&0x80 ) /* has pts */
682 i_pts = ((mtime_t)(header[ 9]&0x0e ) << 29)|
683 (mtime_t)(header[10] << 22)|
684 ((mtime_t)(header[11]&0xfe) << 14)|
685 (mtime_t)(header[12] << 7)|
686 (mtime_t)(header[12] >> 1);
688 if( header[7]&0x40 ) /* has dts */
690 i_dts = ((mtime_t)(header[14]&0x0e ) << 29)|
691 (mtime_t)(header[15] << 22)|
692 ((mtime_t)(header[16]&0xfe) << 14)|
693 (mtime_t)(header[17] << 7)|
694 (mtime_t)(header[18] >> 1);
701 while( i_skip < 23 && header[i_skip] == 0xff )
707 msg_Err( p_demux, "too much MPEG-1 stuffing" );
708 block_ChainRelease( p_pes );
711 if( ( header[i_skip] & 0xC0 ) == 0x40 )
716 if( header[i_skip]&0x20 )
718 p_pes->i_pts = ((mtime_t)(header[i_skip]&0x0e ) << 29)|
719 (mtime_t)(header[i_skip+1] << 22)|
720 ((mtime_t)(header[i_skip+2]&0xfe) << 14)|
721 (mtime_t)(header[i_skip+3] << 7)|
722 (mtime_t)(header[i_skip+4] >> 1);
724 if( header[i_skip]&0x10 ) /* has dts */
726 p_pes->i_dts = ((mtime_t)(header[i_skip+5]&0x0e ) << 29)|
727 (mtime_t)(header[i_skip+6] << 22)|
728 ((mtime_t)(header[i_skip+7]&0xfe) << 14)|
729 (mtime_t)(header[i_skip+8] << 7)|
730 (mtime_t)(header[i_skip+9] >> 1);
746 if( pid->es->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', 'b' ) ||
747 pid->es->fmt.i_codec == VLC_FOURCC( 'd', 't', 's', 'b' ) )
751 else if( pid->es->fmt.i_codec == VLC_FOURCC( 'l', 'p', 'c', 'b' ) ||
752 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'p', 'u', 'b' ) ||
753 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'd', 'd', 'b' ) )
759 while( p_pes && i_skip > 0 )
761 if( p_pes->i_buffer <= i_skip )
763 block_t *p_next = p_pes->p_next;
765 i_skip -= p_pes->i_buffer;
766 block_Release( p_pes );
771 p_pes->i_buffer -= i_skip;
772 p_pes->p_buffer += i_skip;
780 p_pes->i_dts = i_dts * 100 / 9;
784 p_pes->i_pts = i_pts * 100 / 9;
787 /* For mpeg4/mscodec we first gather the packet -> will make ffmpeg happier */
788 es_out_Send( p_demux->out, pid->es->id, block_ChainGather( p_pes ) );
792 msg_Warn( p_demux, "empty pes" );
796 static vlc_bool_t GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
798 const uint8_t *p = p_bk->p_buffer;
799 const vlc_bool_t b_adaptation= p[3]&0x20;
800 const vlc_bool_t b_payload = p[3]&0x10;
801 const int i_cc = p[3]&0x0f; /* continuity counter */
802 /* transport_scrambling_control is ignored */
805 vlc_bool_t i_ret = VLC_FALSE;
809 //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);
812 msg_Dbg( p_demux, "transport_error_indicator set (pid=0x%x)", pid->i_pid );
821 /* p[4] is adaptation length */
827 msg_Warn( p_demux, "discontinuity_indicator (pid=0x%x) ignored", pid->i_pid );
831 /* test continuity counter */
832 /* continuous when (one of this):
834 * diff == 0 and payload == 0
835 * diff == 0 and duplicate packet (playload != 0) <- do we should test the content ?
838 i_diff = ( i_cc - pid->i_cc )&0x0f;
839 if( b_payload && i_diff == 1 )
845 if( pid->i_cc == 0xff )
847 msg_Warn( p_demux, "first packet for pid=0x%x cc=0x%x", pid->i_pid, i_cc );
850 else if( i_diff != 0 )
852 /* FIXME what to do when discontinuity_indicator is set ? */
853 msg_Warn( p_demux, "discontinuity received 0x%x instead of 0x%x",
854 i_cc, ( pid->i_cc + 1 )&0x0f );
860 block_ChainRelease( pid->es->p_pes );
861 pid->es->p_pes = NULL;
867 (p[5] & 0x10) && p[4]>=7 && pid->p_owner && pid->p_owner->i_pid_pcr == pid->i_pid )
869 mtime_t i_pcr; /* 33 bits */
871 i_pcr = ( (mtime_t)p[6] << 25 ) |
872 ( (mtime_t)p[7] << 17 ) |
873 ( (mtime_t)p[8] << 9 ) |
874 ( (mtime_t)p[9] << 1 ) |
875 ( (mtime_t)p[10] >> 7 );
877 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR, (int)pid->p_owner->i_number, (int64_t)(i_pcr * 100 / 9) );
881 if( i_skip >= 188 || pid->es->id == NULL )
883 block_Release( p_bk );
887 const vlc_bool_t b_unit_start= p[1]&0x40;
889 /* we have to gather it */
890 p_bk->p_buffer += i_skip;
891 p_bk->i_buffer -= i_skip;
897 ParsePES( p_demux, pid );
901 pid->es->p_pes = p_bk;
905 if( pid->es->p_pes == NULL )
907 /* msg_Dbg( p_demux, "broken packet" ); */
908 block_Release( p_bk );
912 /* TODO check if when have gather enough packet to form a PES (ie read PES size)*/
913 block_ChainAppend( &pid->es->p_pes, p_bk );
921 static char *LanguageNameISO639( char *psz_code )
923 const iso639_lang_t *pl;
925 pl = GetLang_2B( psz_code );
926 if( !strcmp( pl->psz_iso639_1, "??" ) )
928 pl = GetLang_2T( psz_code );
931 if( !strcmp( pl->psz_iso639_1, "??" ) )
933 return strdup( psz_code );
937 if( *pl->psz_native_name )
939 return strdup( pl->psz_native_name );
941 return strdup( pl->psz_eng_name );
945 static int PIDFillFormat( ts_pid_t *pid, int i_stream_type )
947 es_format_t *fmt = &pid->es->fmt;
949 switch( i_stream_type )
951 case 0x01: /* MPEG-1 video */
952 case 0x02: /* MPEG-2 video */
953 case 0x80: /* MPEG-2 MOTO video */
954 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', 'g', 'v' ) );
956 case 0x03: /* MPEG-1 audio */
957 case 0x04: /* MPEG-2 audio */
958 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
960 case 0x11: /* MPEG4 (audio) */
961 case 0x0f: /* ISO/IEC 13818-7 Audio with ADTS transport syntax */
962 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', '4', 'a' ) );
964 case 0x10: /* MPEG4 (video) */
965 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', '4', 'v' ) );
967 case 0x1B: /* H264 <- check transport syntax/needed descriptor */
968 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'h', '2', '6', '4' ) );
971 case 0x81: /* A52 (audio) */
972 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', ' ' ) );
974 case 0x82: /* DVD_SPU (sub) */
975 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', ' ' ) );
977 case 0x83: /* LPCM (audio) */
978 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'l', 'p', 'c', 'm' ) );
980 case 0x84: /* SDDS (audio) */
981 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 's' ) );
983 case 0x85: /* DTS (audio) */
984 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'd', 't', 's', ' ' ) );
987 case 0x91: /* A52 vls (audio) */
988 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', 'b' ) );
990 case 0x92: /* DVD_SPU vls (sub) */
991 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', 'b' ) );
993 case 0x93: /* LPCM vls (audio) */
994 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'l', 'p', 'c', 'b' ) );
996 case 0x94: /* SDDS (audio) */
997 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 'b' ) );
1000 case 0x06: /* PES_PRIVATE (fixed later) */
1001 case 0xa0: /* MSCODEC vlc (video) (fixed later) */
1003 es_format_Init( fmt, UNKNOWN_ES, 0 );
1007 return fmt->i_cat == UNKNOWN_ES ? VLC_EGENERIC : VLC_SUCCESS ;
1010 /*****************************************************************************
1011 * MP4 specific functions (IOD parser)
1012 *****************************************************************************/
1013 static int IODDescriptorLength( int *pi_data, uint8_t **pp_data )
1016 unsigned int i_len = 0;
1022 i_len = ( i_len << 7 ) + ( i_b&0x7f );
1024 } while( i_b&0x80 );
1028 static int IODGetByte( int *pi_data, uint8_t **pp_data )
1032 const int i_b = **pp_data;
1039 static int IODGetWord( int *pi_data, uint8_t **pp_data )
1041 const int i1 = IODGetByte( pi_data, pp_data );
1042 const int i2 = IODGetByte( pi_data, pp_data );
1043 return( ( i1 << 8 ) | i2 );
1045 static int IODGet3Bytes( int *pi_data, uint8_t **pp_data )
1047 const int i1 = IODGetByte( pi_data, pp_data );
1048 const int i2 = IODGetByte( pi_data, pp_data );
1049 const int i3 = IODGetByte( pi_data, pp_data );
1051 return( ( i1 << 16 ) | ( i2 << 8) | i3 );
1054 static uint32_t IODGetDWord( int *pi_data, uint8_t **pp_data )
1056 const uint32_t i1 = IODGetWord( pi_data, pp_data );
1057 const uint32_t i2 = IODGetWord( pi_data, pp_data );
1058 return( ( i1 << 16 ) | i2 );
1061 static char* IODGetURL( int *pi_data, uint8_t **pp_data )
1066 i_url_len = IODGetByte( pi_data, pp_data );
1067 url = malloc( i_url_len + 1 );
1068 for( i = 0; i < i_url_len; i++ )
1070 url[i] = IODGetByte( pi_data, pp_data );
1072 url[i_url_len] = '\0';
1076 static iod_descriptor_t *IODNew( int i_data, uint8_t *p_data )
1078 iod_descriptor_t *p_iod;
1085 p_iod = malloc( sizeof( iod_descriptor_t ) );
1086 memset( p_iod, 0, sizeof( iod_descriptor_t ) );
1088 fprintf( stderr, "\n************ IOD ************" );
1089 for( i = 0; i < 255; i++ )
1091 p_iod->es_descr[i].b_ok = 0;
1100 p_iod->i_iod_label = IODGetByte( &i_data, &p_data );
1101 fprintf( stderr, "\n* iod_label:%d", p_iod->i_iod_label );
1102 fprintf( stderr, "\n* ===========" );
1103 fprintf( stderr, "\n* tag:0x%x", p_data[0] );
1105 if( IODGetByte( &i_data, &p_data ) != 0x02 )
1107 fprintf( stderr, "\n ERR: tag != 0x02" );
1111 i_iod_length = IODDescriptorLength( &i_data, &p_data );
1112 fprintf( stderr, "\n* length:%d", i_iod_length );
1113 if( i_iod_length > i_data )
1115 i_iod_length = i_data;
1118 p_iod->i_od_id = ( IODGetByte( &i_data, &p_data ) << 2 );
1119 i_flags = IODGetByte( &i_data, &p_data );
1120 p_iod->i_od_id |= i_flags >> 6;
1121 b_url = ( i_flags >> 5 )&0x01;
1123 fprintf( stderr, "\n* od_id:%d", p_iod->i_od_id );
1124 fprintf( stderr, "\n* url flag:%d", b_url );
1125 fprintf( stderr, "\n* includeInlineProfileLevel flag:%d", ( i_flags >> 4 )&0x01 );
1129 p_iod->psz_url = IODGetURL( &i_data, &p_data );
1130 fprintf( stderr, "\n* url string:%s", p_iod->psz_url );
1131 fprintf( stderr, "\n*****************************\n" );
1136 p_iod->psz_url = NULL;
1139 p_iod->i_ODProfileLevelIndication = IODGetByte( &i_data, &p_data );
1140 p_iod->i_sceneProfileLevelIndication = IODGetByte( &i_data, &p_data );
1141 p_iod->i_audioProfileLevelIndication = IODGetByte( &i_data, &p_data );
1142 p_iod->i_visualProfileLevelIndication = IODGetByte( &i_data, &p_data );
1143 p_iod->i_graphicsProfileLevelIndication = IODGetByte( &i_data, &p_data );
1145 fprintf( stderr, "\n* ODProfileLevelIndication:%d", p_iod->i_ODProfileLevelIndication );
1146 fprintf( stderr, "\n* sceneProfileLevelIndication:%d", p_iod->i_sceneProfileLevelIndication );
1147 fprintf( stderr, "\n* audioProfileLevelIndication:%d", p_iod->i_audioProfileLevelIndication );
1148 fprintf( stderr, "\n* visualProfileLevelIndication:%d", p_iod->i_visualProfileLevelIndication );
1149 fprintf( stderr, "\n* graphicsProfileLevelIndication:%d", p_iod->i_graphicsProfileLevelIndication );
1152 while( i_data > 0 && i_es_index < 255)
1154 int i_tag, i_length;
1156 uint8_t *p_data_sav;
1158 i_tag = IODGetByte( &i_data, &p_data );
1159 i_length = IODDescriptorLength( &i_data, &p_data );
1161 i_data_sav = i_data;
1162 p_data_sav = p_data;
1170 #define es_descr p_iod->es_descr[i_es_index]
1171 int i_decoderConfigDescr_length;
1172 fprintf( stderr, "\n* - ES_Descriptor length:%d", i_length );
1175 es_descr.i_es_id = IODGetWord( &i_data, &p_data );
1176 i_flags = IODGetByte( &i_data, &p_data );
1177 es_descr.b_streamDependenceFlag = ( i_flags >> 7 )&0x01;
1178 b_url = ( i_flags >> 6 )&0x01;
1179 es_descr.b_OCRStreamFlag = ( i_flags >> 5 )&0x01;
1180 es_descr.i_streamPriority = i_flags & 0x1f;
1181 fprintf( stderr, "\n* * streamDependenceFlag:%d", es_descr.b_streamDependenceFlag );
1182 fprintf( stderr, "\n* * OCRStreamFlag:%d", es_descr.b_OCRStreamFlag );
1183 fprintf( stderr, "\n* * streamPriority:%d", es_descr.i_streamPriority );
1185 if( es_descr.b_streamDependenceFlag )
1187 es_descr.i_dependOn_es_id = IODGetWord( &i_data, &p_data );
1188 fprintf( stderr, "\n* * dependOn_es_id:%d", es_descr.i_dependOn_es_id );
1193 es_descr.psz_url = IODGetURL( &i_data, &p_data );
1194 fprintf( stderr, "\n* url string:%s", es_descr.psz_url );
1198 es_descr.psz_url = NULL;
1201 if( es_descr.b_OCRStreamFlag )
1203 es_descr.i_OCR_es_id = IODGetWord( &i_data, &p_data );
1204 fprintf( stderr, "\n* * OCR_es_id:%d", es_descr.i_OCR_es_id );
1207 if( IODGetByte( &i_data, &p_data ) != 0x04 )
1209 fprintf( stderr, "\n* ERR missing DecoderConfigDescr" );
1213 i_decoderConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
1215 fprintf( stderr, "\n* - DecoderConfigDesc length:%d", i_decoderConfigDescr_length );
1216 #define dec_descr es_descr.dec_descr
1217 dec_descr.i_objectTypeIndication = IODGetByte( &i_data, &p_data );
1218 i_flags = IODGetByte( &i_data, &p_data );
1219 dec_descr.i_streamType = i_flags >> 2;
1220 dec_descr.b_upStream = ( i_flags >> 1 )&0x01;
1221 dec_descr.i_bufferSizeDB = IODGet3Bytes( &i_data, &p_data );
1222 dec_descr.i_maxBitrate = IODGetDWord( &i_data, &p_data );
1223 dec_descr.i_avgBitrate = IODGetDWord( &i_data, &p_data );
1224 fprintf( stderr, "\n* * objectTypeIndication:0x%x", dec_descr.i_objectTypeIndication );
1225 fprintf( stderr, "\n* * streamType:0x%x", dec_descr.i_streamType );
1226 fprintf( stderr, "\n* * upStream:%d", dec_descr.b_upStream );
1227 fprintf( stderr, "\n* * bufferSizeDB:%d", dec_descr.i_bufferSizeDB );
1228 fprintf( stderr, "\n* * maxBitrate:%d", dec_descr.i_maxBitrate );
1229 fprintf( stderr, "\n* * avgBitrate:%d", dec_descr.i_avgBitrate );
1230 if( i_decoderConfigDescr_length > 13 && IODGetByte( &i_data, &p_data ) == 0x05 )
1233 dec_descr.i_decoder_specific_info_len =
1234 IODDescriptorLength( &i_data, &p_data );
1235 if( dec_descr.i_decoder_specific_info_len > 0 )
1237 dec_descr.p_decoder_specific_info =
1238 malloc( dec_descr.i_decoder_specific_info_len );
1240 for( i = 0; i < dec_descr.i_decoder_specific_info_len; i++ )
1242 dec_descr.p_decoder_specific_info[i] = IODGetByte( &i_data, &p_data );
1247 dec_descr.i_decoder_specific_info_len = 0;
1248 dec_descr.p_decoder_specific_info = NULL;
1252 #define sl_descr es_descr.sl_descr
1254 int i_SLConfigDescr_length;
1257 if( IODGetByte( &i_data, &p_data ) != 0x06 )
1259 fprintf( stderr, "\n* ERR missing SLConfigDescr" );
1263 i_SLConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
1265 fprintf( stderr, "\n* - SLConfigDescr length:%d", i_SLConfigDescr_length );
1266 i_predefined = IODGetByte( &i_data, &p_data );
1267 fprintf( stderr, "\n* * i_predefined:0x%x", i_predefined );
1268 switch( i_predefined )
1272 sl_descr.b_useAccessUnitStartFlag = 0;
1273 sl_descr.b_useAccessUnitEndFlag = 0;
1274 sl_descr.b_useRandomAccessPointFlag = 0;
1275 //sl_descr.b_useRandomAccessUnitsOnlyFlag = 0;
1276 sl_descr.b_usePaddingFlag = 0;
1277 sl_descr.b_useTimeStampsFlags = 0;
1278 sl_descr.b_useIdleFlag = 0;
1279 sl_descr.b_durationFlag = 0; // FIXME FIXME
1280 sl_descr.i_timeStampResolution = 1000;
1281 sl_descr.i_OCRResolution = 0; // FIXME FIXME
1282 sl_descr.i_timeStampLength = 32;
1283 sl_descr.i_OCRLength = 0; // FIXME FIXME
1284 sl_descr.i_AU_Length = 0;
1285 sl_descr.i_instantBitrateLength= 0; // FIXME FIXME
1286 sl_descr.i_degradationPriorityLength= 0;
1287 sl_descr.i_AU_seqNumLength = 0;
1288 sl_descr.i_packetSeqNumLength = 0;
1289 if( sl_descr.b_durationFlag )
1291 sl_descr.i_timeScale = 0; // FIXME FIXME
1292 sl_descr.i_accessUnitDuration = 0; // FIXME FIXME
1293 sl_descr.i_compositionUnitDuration= 0; // FIXME FIXME
1295 if( !sl_descr.b_useTimeStampsFlags )
1297 sl_descr.i_startDecodingTimeStamp = 0; // FIXME FIXME
1298 sl_descr.i_startCompositionTimeStamp= 0; // FIXME FIXME
1303 fprintf( stderr, "\n* ERR unsupported SLConfigDescr predefined" );
1312 fprintf( stderr, "\n* - OD tag:0x%x length:%d (Unsupported)", i_tag, i_length );
1316 p_data = p_data_sav + i_length;
1317 i_data = i_data_sav - i_length;
1322 fprintf( stderr, "\n*****************************\n" );
1326 static void IODFree( iod_descriptor_t *p_iod )
1330 if( p_iod->psz_url )
1332 free( p_iod->psz_url );
1333 p_iod->psz_url = NULL;
1338 for( i = 0; i < 255; i++ )
1340 #define es_descr p_iod->es_descr[i]
1343 if( es_descr.psz_url )
1345 free( es_descr.psz_url );
1346 es_descr.psz_url = NULL;
1350 if( es_descr.dec_descr.p_decoder_specific_info != NULL )
1352 free( es_descr.dec_descr.p_decoder_specific_info );
1353 es_descr.dec_descr.p_decoder_specific_info = NULL;
1354 es_descr.dec_descr.i_decoder_specific_info_len = 0;
1364 /****************************************************************************
1365 ****************************************************************************
1366 ** libdvbpsi callbacks
1367 ****************************************************************************
1368 ****************************************************************************/
1369 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt )
1371 demux_sys_t *p_sys = p_demux->p_sys;
1372 dvbpsi_descriptor_t *p_dr;
1373 dvbpsi_pmt_es_t *p_es;
1375 ts_pid_t *pmt = NULL;
1378 msg_Dbg( p_demux, "PMTCallBack called" );
1379 /* First find this PMT declared in PAT */
1380 for( i = 0; i < p_sys->i_pmt; i++ )
1382 if( p_sys->pmt[i]->psi->i_number == p_pmt->i_program_number )
1384 pmt = p_sys->pmt[i];
1389 msg_Warn( p_demux, "unreferenced program (broken stream)" );
1390 dvbpsi_DeletePMT(p_pmt);
1394 if( pmt->psi->i_version != -1 && ( !p_pmt->b_current_next || pmt->psi->i_version == p_pmt->i_version ) )
1396 dvbpsi_DeletePMT( p_pmt );
1400 /* Clean this program (remove all es) */
1401 for( i = 0; i < 8192; i++ )
1403 ts_pid_t *pid = &p_sys->pid[i];
1405 if( pid->b_valid && pid->p_owner == pmt->psi && pid->psi == NULL )
1407 PIDClean( p_demux->out, pid );
1412 IODFree( pmt->psi->iod );
1413 pmt->psi->iod = NULL;
1416 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 );
1417 pmt->psi->i_pid_pcr = p_pmt->i_pcr_pid;
1418 pmt->psi->i_version = p_pmt->i_version;
1420 /* Parse descriptor */
1421 for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
1423 if( p_dr->i_tag == 0x1d )
1425 /* We have found an IOD descriptor */
1426 msg_Warn( p_demux, "found IOD descriptor" );
1428 pmt->psi->iod = IODNew( p_dr->i_length, p_dr->p_data );
1432 for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
1434 ts_pid_t *pid = &p_sys->pid[p_es->i_pid];
1438 msg_Warn( p_demux, "pmt error: pid=0x%x already defined", p_es->i_pid );
1442 PIDInit( pid, VLC_FALSE, pmt->psi );
1443 PIDFillFormat( pid, p_es->i_type );
1445 if( p_es->i_type == 0x10 || p_es->i_type == 0x11 )
1447 /* MPEG-4 stream: search SL_DESCRIPTOR */
1448 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
1450 while( p_dr && ( p_dr->i_tag != 0x1f ) ) p_dr = p_dr->p_next;
1452 if( p_dr && p_dr->i_length == 2 )
1455 int i_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
1457 msg_Warn( p_demux, "found SL_descriptor es_id=%d", i_es_id );
1459 pid->es->p_mpeg4desc = NULL;
1461 for( i = 0; i < 255; i++ )
1463 iod_descriptor_t *iod = pmt->psi->iod;
1465 if( iod->es_descr[i].b_ok &&
1466 iod->es_descr[i].i_es_id == i_es_id )
1468 pid->es->p_mpeg4desc = &iod->es_descr[i];
1474 if( pid->es->p_mpeg4desc != NULL )
1476 decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;
1478 if( dcd->i_streamType == 0x04 ) /* VisualStream */
1480 pid->es->fmt.i_cat = VIDEO_ES;
1481 switch( dcd->i_objectTypeIndication )
1484 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','v'); // mpeg4
1492 pid->es->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' ); // mpeg2
1495 pid->es->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' ); // mpeg1
1498 pid->es->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' ); // mpeg1
1502 pid->es->fmt.i_cat = UNKNOWN_ES;
1506 else if( dcd->i_streamType == 0x05 ) /* AudioStream */
1508 pid->es->fmt.i_cat = AUDIO_ES;
1509 switch( dcd->i_objectTypeIndication )
1512 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','a'); // mpeg4
1517 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','a');// mpeg2 aac
1520 pid->es->fmt.i_codec = VLC_FOURCC('m','p','g','a'); // mpeg2
1523 pid->es->fmt.i_codec = VLC_FOURCC('m','p','g','a'); // mpeg1
1526 pid->es->fmt.i_cat = UNKNOWN_ES;
1532 pid->es->fmt.i_cat = UNKNOWN_ES;
1535 if( pid->es->fmt.i_cat != UNKNOWN_ES )
1537 pid->es->fmt.i_extra = dcd->i_decoder_specific_info_len;
1538 if( pid->es->fmt.i_extra > 0 )
1540 pid->es->fmt.p_extra = malloc( pid->es->fmt.i_extra );
1541 memcpy( pid->es->fmt.p_extra,
1542 dcd->p_decoder_specific_info,
1543 pid->es->fmt.i_extra );
1548 else if( p_es->i_type == 0x06 )
1550 dvbpsi_descriptor_t *p_dr;
1552 for( p_dr = p_es->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
1554 if( p_dr->i_tag == 0x6a )
1556 pid->es->fmt.i_cat = AUDIO_ES;
1557 pid->es->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
1559 #ifdef _DVBPSI_DR_59_H_
1560 else if( p_dr->i_tag == 0x59 )
1568 else if( p_es->i_type == 0xa0 )
1570 /* MSCODEC send by vlc */
1571 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
1573 while( p_dr && ( p_dr->i_tag != 0xa0 ) ) p_dr = p_dr->p_next;
1575 if( p_dr && p_dr->i_length >= 8 )
1577 pid->es->fmt.i_cat = VIDEO_ES;
1578 pid->es->fmt.i_codec = VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
1579 p_dr->p_data[2], p_dr->p_data[3] );
1580 pid->es->fmt.video.i_width = ( p_dr->p_data[4] << 8 )|p_dr->p_data[5];
1581 pid->es->fmt.video.i_height= ( p_dr->p_data[6] << 8 )|p_dr->p_data[7];
1582 pid->es->fmt.i_extra = (p_dr->p_data[8] << 8) | p_dr->p_data[9];
1584 if( pid->es->fmt.i_extra > 0 )
1586 pid->es->fmt.p_extra = malloc( pid->es->fmt.i_extra );
1587 memcpy( pid->es->fmt.p_extra, &p_dr->p_data[10], pid->es->fmt.i_extra );
1592 msg_Warn( p_demux, "private MSCODEC (vlc) without bih private descriptor" );
1596 if( pid->es->fmt.i_cat == AUDIO_ES || pid->es->fmt.i_cat == SPU_ES )
1598 /* get language descriptor */
1599 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
1600 while( p_dr && ( p_dr->i_tag != 0x0a ) ) p_dr = p_dr->p_next;
1604 dvbpsi_iso639_dr_t *p_decoded = dvbpsi_DecodeISO639Dr( p_dr );
1605 pid->es->fmt.psz_language =
1606 LanguageNameISO639( (char*)&p_decoded->i_iso_639_code );
1610 pid->es->fmt.i_group = p_pmt->i_program_number;
1611 if( pid->es->fmt.i_cat == UNKNOWN_ES )
1613 msg_Dbg( p_demux, " * es pid=0x%x type=0x%x *unknown*", p_es->i_pid, p_es->i_type );
1617 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 );
1618 if( p_sys->b_es_id_pid )
1620 pid->es->fmt.i_id = p_es->i_pid;
1622 pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
1625 dvbpsi_DeletePMT(p_pmt);
1628 static void PATCallBack( demux_t *p_demux, dvbpsi_pat_t *p_pat )
1630 demux_sys_t *p_sys = p_demux->p_sys;
1631 dvbpsi_pat_program_t *p_program;
1632 ts_pid_t *pat = &p_sys->pid[0];
1635 msg_Dbg( p_demux, "PATCallBack called" );
1637 if( pat->psi->i_version != -1 && ( !p_pat->b_current_next || p_pat->i_version == pat->psi->i_version ) )
1639 dvbpsi_DeletePAT( p_pat );
1643 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 );
1646 for( i = 2; i < 8192; i++ )
1648 ts_pid_t *pid = &p_sys->pid[i];
1654 if( pid->p_owner == pat->psi )
1656 PIDClean( p_demux->out, pid );
1657 TAB_REMOVE( p_sys->i_pmt, p_sys->pmt, pid );
1660 else if( pid->p_owner && pid->p_owner->i_number != 0 && pid->es->id )
1662 /* We only remove es that aren't defined by extra pmt */
1663 PIDClean( p_demux->out, pid );
1668 /* now create programs */
1669 for( p_program = p_pat->p_first_program; p_program != NULL; p_program = p_program->p_next )
1671 msg_Dbg( p_demux, " * number=%d pid=0x%x", p_program->i_number, p_program->i_pid );
1672 if( p_program->i_number != 0 )
1674 ts_pid_t *pmt = &p_sys->pid[p_program->i_pid];
1676 PIDInit( pmt, VLC_TRUE, pat->psi );
1677 pmt->psi->handle = dvbpsi_AttachPMT( p_program->i_number, (dvbpsi_pmt_callback)PMTCallBack, p_demux );
1678 pmt->psi->i_number = p_program->i_number;
1680 TAB_APPEND( p_sys->i_pmt, p_sys->pmt, pmt );
1683 pat->psi->i_version = p_pat->i_version;
1685 dvbpsi_DeletePAT( p_pat );