]> git.sesse.net Git - vlc/blob - modules/demux/ts.c
* ts: added a ts-es-id-pid option to mark es with their pid.
[vlc] / modules / demux / ts.c
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 $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31
32 #include "iso_lang.h"
33
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>
42 #else
43 #   include "dvbpsi.h"
44 #   include "descriptor.h"
45 #   include "tables/pat.h"
46 #   include "tables/pmt.h"
47 #   include "descriptors/dr.h"
48 #   include "psi.h"
49 #endif
50
51 /* TODO:
52  *  - XXX: do not mark options message to be translated, they are too osbcure for now ...
53  *  - test it
54  *  - ...
55  */
56
57 /*****************************************************************************
58  * Module descriptor
59  *****************************************************************************/
60 static int  Open    ( vlc_object_t * );
61 static void Close  ( vlc_object_t * );
62
63 vlc_module_begin();
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" );
71 vlc_module_end();
72
73 /*****************************************************************************
74  * Local prototypes
75  *****************************************************************************/
76
77 typedef struct
78 {
79     uint8_t                 i_objectTypeIndication;
80     uint8_t                 i_streamType;
81     vlc_bool_t              b_upStream;
82     uint32_t                i_bufferSizeDB;
83     uint32_t                i_maxBitrate;
84     uint32_t                i_avgBitrate;
85
86     int                     i_decoder_specific_info_len;
87     uint8_t                 *p_decoder_specific_info;
88
89 } decoder_config_descriptor_t;
90
91 typedef struct
92 {
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;
104     uint8_t                 i_OCRLength;
105     uint8_t                 i_AU_Length;
106     uint8_t                 i_instantBitrateLength;
107     uint8_t                 i_degradationPriorityLength;
108     uint8_t                 i_AU_seqNumLength;
109     uint8_t                 i_packetSeqNumLength;
110
111     uint32_t                i_timeScale;
112     uint16_t                i_accessUnitDuration;
113     uint16_t                i_compositionUnitDuration;
114
115     uint64_t                i_startDecodingTimeStamp;
116     uint64_t                i_startCompositionTimeStamp;
117
118 } sl_config_descriptor_t;
119
120 typedef struct
121 {
122     vlc_bool_t              b_ok;
123     uint16_t                i_es_id;
124
125     vlc_bool_t              b_streamDependenceFlag;
126     vlc_bool_t              b_OCRStreamFlag;
127     uint8_t                 i_streamPriority;
128
129     char                    *psz_url;
130
131     uint16_t                i_dependOn_es_id;
132     uint16_t                i_OCR_es_id;
133
134     decoder_config_descriptor_t    dec_descr;
135     sl_config_descriptor_t         sl_descr;
136 } es_mpeg4_descriptor_t;
137
138 typedef struct
139 {
140     uint8_t                i_iod_label;
141
142     /* IOD */
143     uint16_t                i_od_id;
144     char                    *psz_url;
145
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;
151
152     es_mpeg4_descriptor_t   es_descr[255];
153
154 } iod_descriptor_t;
155
156 typedef struct
157 {
158     int             i_version;
159     int             i_number;
160     int             i_pid_pcr;
161     dvbpsi_handle   handle;
162
163     /* IOD stuff (mpeg4) */
164     iod_descriptor_t *iod;
165
166 } ts_psi_t;
167
168 typedef struct
169 {
170     es_format_t  fmt;
171     es_out_id_t *id;
172     block_t     *p_pes;
173
174     es_mpeg4_descriptor_t *p_mpeg4desc;
175 } ts_es_t;
176
177 typedef struct
178 {
179     int         i_pid;
180
181     vlc_bool_t  b_seen;
182     vlc_bool_t  b_valid;
183     int         i_cc;   /* countinuity counter */
184
185     /* PSI owner (ie PMT -> PAT, ES -> PMT */
186     ts_psi_t   *p_owner;
187
188     /* */
189     ts_psi_t    *psi;
190     ts_es_t     *es;
191
192 } ts_pid_t;
193
194 struct demux_sys_t
195 {
196     /* All pid */
197     ts_pid_t    pid[8192];
198
199     /* All PMT */
200     int         i_pmt;
201     ts_pid_t    **pmt;
202
203     /* */
204     vlc_bool_t  b_es_id_pid;
205 };
206
207 static int Demux  ( demux_t *p_demux );
208 static int Control( demux_t *p_demux, int i_query, va_list args );
209
210
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 );
214
215 static void PATCallBack( demux_t *, dvbpsi_pat_t * );
216 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt );
217
218 static inline int PIDGet( block_t *p )
219 {
220     return ( (p->p_buffer[1]&0x1f)<<8 )|p->p_buffer[2];
221 }
222
223 static vlc_bool_t GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk );
224 static char *LanguageNameISO639( char *p );
225
226 static iod_descriptor_t *IODNew( int , uint8_t * );
227 static void              IODFree( iod_descriptor_t * );
228
229
230 /*****************************************************************************
231  * Open
232  *****************************************************************************/
233 static int Open( vlc_object_t *p_this )
234 {
235     demux_t     *p_demux = (demux_t*)p_this;
236     demux_sys_t *p_sys;
237
238     uint8_t     *p_peek;
239     int          i_peek;
240     int          i;
241
242     ts_pid_t     *pat;
243
244     vlc_value_t  val;
245
246     if( ( i_peek = stream_Peek( p_demux->s, &p_peek, 189 ) ) < 1 )
247     {
248         msg_Err( p_demux, "cannot peek" );
249         return VLC_EGENERIC;
250     }
251     if( p_peek[0] != 0x47 || ( i_peek >= 189 && p_peek[188] != 0x47 ) )
252     {
253         if( strcmp( p_demux->psz_demux, "ts" ) )
254         {
255             msg_Warn( p_demux, "TS module discarded" );
256             return VLC_EGENERIC;
257         }
258         msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
259     }
260
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 ) );
266
267     /* Init p_sys field */
268     for( i = 0; i < 8192; i++ )
269     {
270         ts_pid_t *pid = &p_sys->pid[i];
271
272         pid->i_pid      = i;
273         pid->b_seen     = VLC_FALSE;
274         pid->b_valid    = VLC_FALSE;
275     }
276
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 );
281
282     /* Init PMT array */
283     p_sys->i_pmt = 0;
284     p_sys->pmt   = NULL;
285
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,
289
290
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 )
295     {
296         char *psz = val.psz_string;
297         int  i_pid = strtol( psz, &psz, 0 );
298
299         if( i_pid >= 2 && i_pid < 8192 )
300         {
301             ts_pid_t *pmt = &p_sys->pid[i_pid];
302
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 */
308
309             psz = strchr( psz, '=' ) + 1;   /* can't failed */
310             while( psz && *psz )
311             {
312                 char *psz_next = strchr( psz, ',' );
313                 int i_pid, i_stream_type;
314
315                 if( psz_next )
316                 {
317                     *psz_next++ = '\0';
318                 }
319
320                 i_pid = strtol( psz, &psz, 0 );
321                 if( *psz == ':' )
322                 {
323                     i_stream_type = strtol( psz + 1, &psz, 0 );
324                     if( i_pid >= 2 && i_pid < 8192 && !p_sys->pid[i_pid].b_valid )
325                     {
326                         ts_pid_t *pid = &p_sys->pid[i_pid];
327
328                         PIDInit( pid, VLC_FALSE, pmt->psi);
329                         if( pmt->psi->i_pid_pcr <= 0 )
330                         {
331                             pmt->psi->i_pid_pcr = i_pid;
332                         }
333                         PIDFillFormat( pid, i_stream_type);
334                         if( pid->es->fmt.i_cat != UNKNOWN_ES )
335                         {
336                             if( p_sys->b_es_id_pid )
337                             {
338                                 pid->es->fmt.i_id = i_pid;
339                             }
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 );
342                         }
343                     }
344                 }
345                 psz = psz_next;
346             }
347         }
348     }
349     if( val.psz_string )
350     {
351         free( val.psz_string );
352     }
353
354     return VLC_SUCCESS;
355 }
356
357 /*****************************************************************************
358  * Close
359  *****************************************************************************/
360 static void Close( vlc_object_t *p_this )
361 {
362     demux_t     *p_demux = (demux_t*)p_this;
363     demux_sys_t *p_sys = p_demux->p_sys;
364
365     int          i;
366
367     msg_Dbg( p_demux, "pid list:" );
368     for( i = 0; i < 8192; i++ )
369     {
370         ts_pid_t *pid = &p_sys->pid[i];
371
372         if( pid->b_valid && pid->psi )
373         {
374             switch( pid->i_pid )
375             {
376                 case 0: /* PAT */
377                     dvbpsi_DetachPAT( pid->psi->handle );
378                     break;
379                 case 1: /* CAT */
380                     break;
381                 default:
382                     dvbpsi_DetachPMT( pid->psi->handle );
383                     if( pid->psi->iod )
384                     {
385                         IODFree( pid->psi->iod );
386                     }
387                     break;
388             }
389             free( pid->psi );
390         }
391         else if( pid->b_valid && pid->es )
392         {
393             if( pid->es->p_pes )
394             {
395                 block_ChainRelease( pid->es->p_pes );
396             }
397             free( pid->es );
398         }
399         if( pid->b_seen )
400         {
401             msg_Dbg( p_demux, "  - pid[0x%x] seen", pid->i_pid );
402         }
403     }
404
405     free( p_sys );
406 }
407
408
409 /*****************************************************************************
410  * Demux:
411  *****************************************************************************/
412 static int Demux( demux_t *p_demux )
413 {
414     demux_sys_t *p_sys = p_demux->p_sys;
415     int          i_pkt;
416
417     /* We read at most 100 TS packet or until a frame is completed */
418     for( i_pkt = 0; i_pkt < 100; i_pkt++ )
419     {
420         vlc_bool_t  b_frame = VLC_FALSE;
421         block_t     *p_pkt;
422         ts_pid_t    *p_pid;
423
424         /* Get a new TS packet */
425         if( ( p_pkt = stream_Block( p_demux->s, 188 ) ) == NULL )
426         {
427             msg_Dbg( p_demux, "eof ?" );
428             return 0;
429         }
430         if( p_pkt->p_buffer[0] != 0x47 )
431         {
432             msg_Warn( p_demux, "lost synchro" );
433             block_Release( p_pkt );
434
435             /* Resynch */
436             for( ;; )
437             {
438                 uint8_t    *p_peek;
439                 int         i_peek = stream_Peek( p_demux->s, &p_peek, 1880 );
440                 int         i_skip = 0;
441                 vlc_bool_t  b_ok = VLC_FALSE;
442
443                 if( i_peek < 189 )
444                 {
445                     msg_Dbg( p_demux, "eof ?" );
446                     return 0;
447                 }
448
449                 while( i_skip < i_peek - 188 )
450                 {
451                     if( p_peek[i_skip] == 0x47 && p_peek[i_skip+188] == 0x47 )
452                     {
453                         b_ok = VLC_TRUE;
454                         break;
455                     }
456                     i_skip++;
457                 }
458                 stream_Read( p_demux->s, NULL, i_skip );
459                 msg_Dbg( p_demux, "%d bytes of garbage", i_skip );
460                 if( b_ok )
461                 {
462                     break;
463                 }
464             }
465             if( ( p_pkt = stream_Block( p_demux->s, 188 ) ) == NULL )
466             {
467                 msg_Dbg( p_demux, "eof ?" );
468                 return 0;
469             }
470         }
471
472         /* Parse the TS packet */
473         p_pid = &p_sys->pid[PIDGet( p_pkt )];
474
475         if( p_pid->b_valid )
476         {
477             if( p_pid->psi )
478             {
479                 dvbpsi_PushPacket( p_pid->psi->handle, p_pkt->p_buffer );
480                 block_Release( p_pkt );
481             }
482             else
483             {
484                 b_frame = GatherPES( p_demux, p_pid, p_pkt );
485             }
486         }
487         else
488         {
489             if( !p_pid->b_seen )
490             {
491                 msg_Dbg( p_demux, "pid[0x%x] unknown", p_pid->i_pid );
492             }
493             block_Release( p_pkt );
494         }
495         p_pid->b_seen = VLC_TRUE;
496
497         if( b_frame )
498         {
499             break;
500         }
501     }
502
503     return 1;
504 }
505
506 /*****************************************************************************
507  * Control:
508  *****************************************************************************/
509 static int Control( demux_t *p_demux, int i_query, va_list args )
510 {
511     demux_sys_t *p_sys = p_demux->p_sys;
512     double f, *pf;
513     int64_t i64, *pi64;
514
515     switch( i_query )
516     {
517         case DEMUX_GET_POSITION:
518             pf = (double*) va_arg( args, double* );
519             i64 = stream_Size( p_demux->s );
520             if( i64 > 0 )
521             {
522                 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
523             }
524             else
525             {
526                 *pf = 0.0;
527             }
528             return VLC_SUCCESS;
529         case DEMUX_SET_POSITION:
530             f = (double) va_arg( args, double );
531             i64 = stream_Size( p_demux->s );
532
533             es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
534             if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) )
535             {
536                 return VLC_EGENERIC;
537             }
538             return VLC_SUCCESS;
539 #if 0
540
541         case DEMUX_GET_TIME:
542             pi64 = (int64_t*)va_arg( args, int64_t * );
543             if( p_sys->i_time < 0 )
544             {
545                 *pi64 = 0;
546                 return VLC_EGENERIC;
547             }
548             *pi64 = p_sys->i_time;
549             return VLC_SUCCESS;
550
551 #if 0
552         case DEMUX_GET_LENGTH:
553             pi64 = (int64_t*)va_arg( args, int64_t * );
554             if( p_sys->i_mux_rate > 0 )
555             {
556                 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
557                 return VLC_SUCCESS;
558             }
559             *pi64 = 0;
560             return VLC_EGENERIC;
561
562         case DEMUX_SET_TIME:
563 #endif
564         case DEMUX_GET_FPS:
565             pf = (double*)va_arg( args, double * );
566             *pf = (double)1000000.0 / (double)p_sys->i_pcr_inc;
567             return VLC_SUCCESS;
568 #endif
569         default:
570             return VLC_EGENERIC;
571     }
572 }
573
574 static void PIDInit( ts_pid_t *pid, vlc_bool_t b_psi, ts_psi_t *p_owner )
575 {
576     pid->b_valid    = VLC_TRUE;
577     pid->i_cc       = 0xff;
578     pid->p_owner    = p_owner;
579
580     if( b_psi )
581     {
582         pid->psi = malloc( sizeof( ts_psi_t ) );
583         pid->es  = NULL;
584
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;
590     }
591     else
592     {
593         pid->psi = NULL;
594         pid->es  = malloc( sizeof( ts_es_t ) );
595
596         es_format_Init( &pid->es->fmt, UNKNOWN_ES, 0 );
597         pid->es->id      = NULL;
598         pid->es->p_pes   = NULL;
599         pid->es->p_mpeg4desc = NULL;
600     }
601 }
602
603 static void PIDClean( es_out_t *out, ts_pid_t *pid )
604 {
605     if( pid->psi )
606     {
607         if( pid->psi->handle )
608         {
609             dvbpsi_DetachPMT( pid->psi->handle );
610         }
611         if( pid->psi->iod )
612         {
613             IODFree( pid->psi->iod );
614         }
615         free( pid->psi );
616     }
617     else
618     {
619         if( pid->es->id )
620         {
621             es_out_Del( out, pid->es->id );
622         }
623         if( pid->es->p_pes )
624         {
625             block_ChainRelease( pid->es->p_pes );
626         }
627         free( pid->es );
628     }
629     pid->b_valid = VLC_FALSE;
630 }
631
632 /****************************************************************************
633  * gathering stuff
634  ****************************************************************************/
635 static void ParsePES ( demux_t *p_demux, ts_pid_t *pid )
636 {
637     block_t *p_pes = pid->es->p_pes;
638     uint8_t header[30];
639     int     i_pes_size;
640     int     i_skip = 0;
641     mtime_t i_dts = -1;
642     mtime_t i_pts = -1;
643     int i_max;
644
645     /* remove the pes from pid */
646     pid->es->p_pes = NULL;
647
648     /* FIXME find real max size */
649     i_max = block_ChainExtract( p_pes, header, 30 );
650
651     if( header[0] != 0 || header[1] != 0 || header[2] != 1 )
652     {
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 );
655         return;
656     }
657
658     i_pes_size = (header[4] << 8)|header[5];
659
660     /* TODO check size */
661
662     switch( header[3] )
663     {
664         case 0xBC:  /* Program stream map */
665         case 0xBE:  /* Padding */
666         case 0xBF:  /* Private stream 2 */
667         case 0xB0:  /* ECM */
668         case 0xB1:  /* EMM */
669         case 0xFF:  /* Program stream directory */
670         case 0xF2:  /* DSMCC stream */
671         case 0xF8:  /* ITU-T H.222.1 type E stream */
672             i_skip = 6;
673             break;
674         default:
675             if( ( header[6]&0xC0 ) == 0x80 )
676             {
677                 /* mpeg2 PES */
678                 i_skip = header[8] + 9;
679
680                 if( header[7]&0x80 )    /* has pts */
681                 {
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);
687
688                     if( header[7]&0x40 )    /* has dts */
689                     {
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);
695                     }
696                 }
697             }
698             else
699             {
700                 i_skip = 6;
701                 while( i_skip < 23 && header[i_skip] == 0xff )
702                 {
703                     i_skip++;
704                 }
705                 if( i_skip == 23 )
706                 {
707                     msg_Err( p_demux, "too much MPEG-1 stuffing" );
708                     block_ChainRelease( p_pes );
709                     return;
710                 }
711                 if( ( header[i_skip] & 0xC0 ) == 0x40 )
712                 {
713                     i_skip += 2;
714                 }
715
716                 if(  header[i_skip]&0x20 )
717                 {
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);
723
724                     if( header[i_skip]&0x10 )    /* has dts */
725                     {
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);
731                          i_skip += 10;
732                     }
733                     else
734                     {
735                         i_skip += 5;
736                     }
737                 }
738                 else
739                 {
740                     i_skip += 1;
741                 }
742             }
743             break;
744     }
745
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' ) )
748     {
749         i_skip += 4;
750     }
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' ) )
754     {
755         i_skip += 1;
756     }
757
758     /* skip header */
759     while( p_pes && i_skip > 0 )
760     {
761         if( p_pes->i_buffer <= i_skip )
762         {
763             block_t *p_next = p_pes->p_next;
764
765             i_skip -= p_pes->i_buffer;
766             block_Release( p_pes );
767             p_pes = p_next;
768         }
769         else
770         {
771             p_pes->i_buffer -= i_skip;
772             p_pes->p_buffer += i_skip;
773             break;
774         }
775     }
776     if( p_pes )
777     {
778         if( i_dts >= 0 )
779         {
780             p_pes->i_dts = i_dts * 100 / 9;
781         }
782         if( i_pts >= 0 )
783         {
784             p_pes->i_pts = i_pts * 100 / 9;
785         }
786
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 ) );
789     }
790     else
791     {
792         msg_Warn( p_demux, "empty pes" );
793     }
794 };
795
796 static vlc_bool_t GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
797 {
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 */
803
804     int         i_skip = 0;
805     vlc_bool_t  i_ret   = VLC_FALSE;
806
807     int         i_diff;
808
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);
810     if( p[1]&0x80 )
811     {
812         msg_Dbg( p_demux, "transport_error_indicator set (pid=0x%x)", pid->i_pid );
813     }
814
815     if( !b_adaptation )
816     {
817         i_skip = 4;
818     }
819     else
820     {
821         /* p[4] is adaptation length */
822         i_skip = 5 + p[4];
823         if( p[4] > 0 )
824         {
825             if( p[5]&0x80 )
826             {
827                 msg_Warn( p_demux, "discontinuity_indicator (pid=0x%x) ignored", pid->i_pid );
828             }
829         }
830     }
831     /* test continuity counter */
832     /* continuous when (one of this):
833         * diff == 1
834         * diff == 0 and payload == 0
835         * diff == 0 and duplicate packet (playload != 0) <- do we should test the content ?
836      */
837
838     i_diff = ( i_cc - pid->i_cc )&0x0f;
839     if( b_payload && i_diff == 1 )
840     {
841         pid->i_cc++;
842     }
843     else
844     {
845         if( pid->i_cc == 0xff )
846         {
847             msg_Warn( p_demux, "first packet for pid=0x%x cc=0x%x", pid->i_pid, i_cc );
848             pid->i_cc = i_cc;
849         }
850         else if( i_diff != 0 )
851         {
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 );
855
856             pid->i_cc = i_cc;
857
858             if( pid->es->p_pes )
859             {
860                 block_ChainRelease( pid->es->p_pes );
861                 pid->es->p_pes = NULL;
862             }
863         }
864     }
865
866     if( b_adaptation &&
867         (p[5] & 0x10) && p[4]>=7 && pid->p_owner && pid->p_owner->i_pid_pcr == pid->i_pid )
868     {
869         mtime_t i_pcr;  /* 33 bits */
870
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 );
876
877         es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR, (int)pid->p_owner->i_number, (int64_t)(i_pcr * 100 / 9) );
878     }
879
880
881     if( i_skip >= 188 || pid->es->id == NULL )
882     {
883         block_Release( p_bk );
884     }
885     else
886     {
887         const vlc_bool_t b_unit_start= p[1]&0x40;
888
889         /* we have to gather it */
890         p_bk->p_buffer += i_skip;
891         p_bk->i_buffer -= i_skip;
892
893         if( b_unit_start )
894         {
895             if( pid->es->p_pes )
896             {
897                 ParsePES( p_demux, pid );
898                 i_ret = VLC_TRUE;
899             }
900
901             pid->es->p_pes = p_bk;
902         }
903         else
904         {
905             if( pid->es->p_pes == NULL )
906             {
907                 /* msg_Dbg( p_demux, "broken packet" ); */
908                 block_Release( p_bk );
909             }
910             else
911             {
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 );
914             }
915         }
916     }
917
918     return i_ret;
919 }
920
921 static char *LanguageNameISO639( char *psz_code )
922 {
923     const iso639_lang_t *pl;
924
925     pl = GetLang_2B( psz_code );
926     if( !strcmp( pl->psz_iso639_1, "??" ) )
927     {
928         pl = GetLang_2T( psz_code );
929     }
930
931     if( !strcmp( pl->psz_iso639_1, "??" ) )
932     {
933        return strdup( psz_code );
934     }
935     else
936     {
937         if( *pl->psz_native_name )
938         {
939             return strdup( pl->psz_native_name );
940         }
941         return strdup( pl->psz_eng_name );
942     }
943 }
944
945 static int PIDFillFormat( ts_pid_t *pid, int i_stream_type )
946 {
947     es_format_t *fmt = &pid->es->fmt;
948
949     switch( i_stream_type )
950     {
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' ) );
955             break;
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' ) );
959             break;
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' ) );
963             break;
964         case 0x10:  /* MPEG4 (video) */
965             es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', '4', 'v' ) );
966             break;
967         case 0x1B:  /* H264 <- check transport syntax/needed descriptor */
968             es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'h', '2', '6', '4' ) );
969             break;
970
971         case 0x81:  /* A52 (audio) */
972             es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', ' ' ) );
973             break;
974         case 0x82:  /* DVD_SPU (sub) */
975             es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', ' ' ) );
976             break;
977         case 0x83:  /* LPCM (audio) */
978             es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'l', 'p', 'c', 'm' ) );
979             break;
980         case 0x84:  /* SDDS (audio) */
981             es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 's' ) );
982             break;
983         case 0x85:  /* DTS (audio) */
984             es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'd', 't', 's', ' ' ) );
985             break;
986
987         case 0x91:  /* A52 vls (audio) */
988             es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', 'b' ) );
989             break;
990         case 0x92:  /* DVD_SPU vls (sub) */
991             es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', 'b' ) );
992             break;
993         case 0x93:  /* LPCM vls (audio) */
994             es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'l', 'p', 'c', 'b' ) );
995             break;
996         case 0x94:  /* SDDS (audio) */
997             es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 'b' ) );
998             break;
999
1000         case 0x06:  /* PES_PRIVATE  (fixed later) */
1001         case 0xa0:  /* MSCODEC vlc (video) (fixed later) */
1002         default:
1003             es_format_Init( fmt, UNKNOWN_ES, 0 );
1004             break;
1005     }
1006
1007     return fmt->i_cat == UNKNOWN_ES ? VLC_EGENERIC : VLC_SUCCESS ;
1008 }
1009
1010 /*****************************************************************************
1011  * MP4 specific functions (IOD parser)
1012  *****************************************************************************/
1013 static int  IODDescriptorLength( int *pi_data, uint8_t **pp_data )
1014 {
1015     unsigned int i_b;
1016     unsigned int i_len = 0;
1017     do
1018     {
1019         i_b = **pp_data;
1020         (*pp_data)++;
1021         (*pi_data)--;
1022         i_len = ( i_len << 7 ) + ( i_b&0x7f );
1023
1024     } while( i_b&0x80 );
1025
1026     return( i_len );
1027 }
1028 static int IODGetByte( int *pi_data, uint8_t **pp_data )
1029 {
1030     if( *pi_data > 0 )
1031     {
1032         const int i_b = **pp_data;
1033         (*pp_data)++;
1034         (*pi_data)--;
1035         return( i_b );
1036     }
1037     return( 0 );
1038 }
1039 static int IODGetWord( int *pi_data, uint8_t **pp_data )
1040 {
1041     const int i1 = IODGetByte( pi_data, pp_data );
1042     const int i2 = IODGetByte( pi_data, pp_data );
1043     return( ( i1 << 8 ) | i2 );
1044 }
1045 static int IODGet3Bytes( int *pi_data, uint8_t **pp_data )
1046 {
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 );
1050
1051     return( ( i1 << 16 ) | ( i2 << 8) | i3 );
1052 }
1053
1054 static uint32_t IODGetDWord( int *pi_data, uint8_t **pp_data )
1055 {
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 );
1059 }
1060
1061 static char* IODGetURL( int *pi_data, uint8_t **pp_data )
1062 {
1063     char *url;
1064     int i_url_len, i;
1065
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++ )
1069     {
1070         url[i] = IODGetByte( pi_data, pp_data );
1071     }
1072     url[i_url_len] = '\0';
1073     return( url );
1074 }
1075
1076 static iod_descriptor_t *IODNew( int i_data, uint8_t *p_data )
1077 {
1078     iod_descriptor_t *p_iod;
1079     int i;
1080     int i_es_index;
1081     uint8_t     i_flags;
1082     vlc_bool_t  b_url;
1083     int         i_iod_length;
1084
1085     p_iod = malloc( sizeof( iod_descriptor_t ) );
1086     memset( p_iod, 0, sizeof( iod_descriptor_t ) );
1087
1088     fprintf( stderr, "\n************ IOD ************" );
1089     for( i = 0; i < 255; i++ )
1090     {
1091         p_iod->es_descr[i].b_ok = 0;
1092     }
1093     i_es_index = 0;
1094
1095     if( i_data < 3 )
1096     {
1097         return p_iod;
1098     }
1099
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] );
1104
1105     if( IODGetByte( &i_data, &p_data ) != 0x02 )
1106     {
1107         fprintf( stderr, "\n ERR: tag != 0x02" );
1108         return p_iod;
1109     }
1110
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 )
1114     {
1115         i_iod_length = i_data;
1116     }
1117
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;
1122
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 );
1126
1127     if( b_url )
1128     {
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" );
1132         return p_iod;
1133     }
1134     else
1135     {
1136         p_iod->psz_url = NULL;
1137     }
1138
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 );
1144
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 );
1150
1151
1152     while( i_data > 0 && i_es_index < 255)
1153     {
1154         int i_tag, i_length;
1155         int     i_data_sav;
1156         uint8_t *p_data_sav;
1157
1158         i_tag = IODGetByte( &i_data, &p_data );
1159         i_length = IODDescriptorLength( &i_data, &p_data );
1160
1161         i_data_sav = i_data;
1162         p_data_sav = p_data;
1163
1164         i_data = i_length;
1165
1166         switch( i_tag )
1167         {
1168             case 0x03:
1169                 {
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 );
1173                     es_descr.b_ok = 1;
1174
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 );
1184
1185                     if( es_descr.b_streamDependenceFlag )
1186                     {
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 );
1189                     }
1190
1191                     if( b_url )
1192                     {
1193                         es_descr.psz_url = IODGetURL( &i_data, &p_data );
1194                         fprintf( stderr, "\n* url string:%s", es_descr.psz_url );
1195                     }
1196                     else
1197                     {
1198                         es_descr.psz_url = NULL;
1199                     }
1200
1201                     if( es_descr.b_OCRStreamFlag )
1202                     {
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 );
1205                     }
1206
1207                     if( IODGetByte( &i_data, &p_data ) != 0x04 )
1208                     {
1209                         fprintf( stderr, "\n* ERR missing DecoderConfigDescr" );
1210                         es_descr.b_ok = 0;
1211                         break;
1212                     }
1213                     i_decoderConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
1214
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 )
1231                     {
1232                         int i;
1233                         dec_descr.i_decoder_specific_info_len =
1234                             IODDescriptorLength( &i_data, &p_data );
1235                         if( dec_descr.i_decoder_specific_info_len > 0 )
1236                         {
1237                             dec_descr.p_decoder_specific_info =
1238                                 malloc( dec_descr.i_decoder_specific_info_len );
1239                         }
1240                         for( i = 0; i < dec_descr.i_decoder_specific_info_len; i++ )
1241                         {
1242                             dec_descr.p_decoder_specific_info[i] = IODGetByte( &i_data, &p_data );
1243                         }
1244                     }
1245                     else
1246                     {
1247                         dec_descr.i_decoder_specific_info_len = 0;
1248                         dec_descr.p_decoder_specific_info = NULL;
1249                     }
1250                 }
1251 #undef  dec_descr
1252 #define sl_descr    es_descr.sl_descr
1253                 {
1254                     int i_SLConfigDescr_length;
1255                     int i_predefined;
1256
1257                     if( IODGetByte( &i_data, &p_data ) != 0x06 )
1258                     {
1259                         fprintf( stderr, "\n* ERR missing SLConfigDescr" );
1260                         es_descr.b_ok = 0;
1261                         break;
1262                     }
1263                     i_SLConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
1264
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 )
1269                     {
1270                         case 0x01:
1271                             {
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 )
1290                                 {
1291                                     sl_descr.i_timeScale            = 0;    // FIXME FIXME
1292                                     sl_descr.i_accessUnitDuration   = 0;    // FIXME FIXME
1293                                     sl_descr.i_compositionUnitDuration= 0;    // FIXME FIXME
1294                                 }
1295                                 if( !sl_descr.b_useTimeStampsFlags )
1296                                 {
1297                                     sl_descr.i_startDecodingTimeStamp   = 0;    // FIXME FIXME
1298                                     sl_descr.i_startCompositionTimeStamp= 0;    // FIXME FIXME
1299                                 }
1300                             }
1301                             break;
1302                         default:
1303                             fprintf( stderr, "\n* ERR unsupported SLConfigDescr predefined" );
1304                             es_descr.b_ok = 0;
1305                             break;
1306                     }
1307                 }
1308                 break;
1309 #undef  sl_descr
1310 #undef  es_descr
1311             default:
1312                 fprintf( stderr, "\n* - OD tag:0x%x length:%d (Unsupported)", i_tag, i_length );
1313                 break;
1314         }
1315
1316         p_data = p_data_sav + i_length;
1317         i_data = i_data_sav - i_length;
1318         i_es_index++;
1319     }
1320
1321
1322     fprintf( stderr, "\n*****************************\n" );
1323     return p_iod;
1324 }
1325
1326 static void IODFree( iod_descriptor_t *p_iod )
1327 {
1328     int i;
1329
1330     if( p_iod->psz_url )
1331     {
1332         free( p_iod->psz_url );
1333         p_iod->psz_url = NULL;
1334         free( p_iod );
1335         return;
1336     }
1337
1338     for( i = 0; i < 255; i++ )
1339     {
1340 #define es_descr p_iod->es_descr[i]
1341         if( es_descr.b_ok )
1342         {
1343             if( es_descr.psz_url )
1344             {
1345                 free( es_descr.psz_url );
1346                 es_descr.psz_url = NULL;
1347             }
1348             else
1349             {
1350                 if( es_descr.dec_descr.p_decoder_specific_info != NULL )
1351                 {
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;
1355                 }
1356             }
1357         }
1358         es_descr.b_ok = 0;
1359 #undef  es_descr
1360     }
1361     free( p_iod );
1362 }
1363
1364 /****************************************************************************
1365  ****************************************************************************
1366  ** libdvbpsi callbacks
1367  ****************************************************************************
1368  ****************************************************************************/
1369 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt )
1370 {
1371     demux_sys_t          *p_sys = p_demux->p_sys;
1372     dvbpsi_descriptor_t  *p_dr;
1373     dvbpsi_pmt_es_t      *p_es;
1374
1375     ts_pid_t             *pmt = NULL;
1376     int                  i;
1377
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++ )
1381     {
1382         if( p_sys->pmt[i]->psi->i_number == p_pmt->i_program_number )
1383         {
1384             pmt = p_sys->pmt[i];
1385         }
1386     }
1387     if( pmt == NULL )
1388     {
1389         msg_Warn( p_demux, "unreferenced program (broken stream)" );
1390         dvbpsi_DeletePMT(p_pmt);
1391         return;
1392     }
1393
1394     if( pmt->psi->i_version != -1 && ( !p_pmt->b_current_next || pmt->psi->i_version == p_pmt->i_version ) )
1395     {
1396         dvbpsi_DeletePMT( p_pmt );
1397         return;
1398     }
1399
1400     /* Clean this program (remove all es) */
1401     for( i = 0; i < 8192; i++ )
1402     {
1403         ts_pid_t *pid = &p_sys->pid[i];
1404
1405         if( pid->b_valid && pid->p_owner == pmt->psi && pid->psi == NULL )
1406         {
1407             PIDClean( p_demux->out, pid );
1408         }
1409     }
1410     if( pmt->psi->iod )
1411     {
1412         IODFree( pmt->psi->iod );
1413         pmt->psi->iod = NULL;
1414     }
1415
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;
1419
1420     /* Parse descriptor */
1421     for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
1422     {
1423         if( p_dr->i_tag == 0x1d )
1424         {
1425             /* We have found an IOD descriptor */
1426             msg_Warn( p_demux, "found IOD descriptor" );
1427
1428             pmt->psi->iod = IODNew( p_dr->i_length, p_dr->p_data );
1429         }
1430     }
1431
1432     for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
1433     {
1434         ts_pid_t *pid = &p_sys->pid[p_es->i_pid];
1435
1436         if( pid->b_valid )
1437         {
1438             msg_Warn( p_demux, "pmt error: pid=0x%x already defined", p_es->i_pid );
1439             continue;
1440         }
1441
1442         PIDInit( pid, VLC_FALSE, pmt->psi );
1443         PIDFillFormat( pid, p_es->i_type );
1444
1445         if( p_es->i_type == 0x10 || p_es->i_type == 0x11 )
1446         {
1447             /* MPEG-4 stream: search SL_DESCRIPTOR */
1448             dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
1449
1450             while( p_dr && ( p_dr->i_tag != 0x1f ) ) p_dr = p_dr->p_next;
1451
1452             if( p_dr && p_dr->i_length == 2 )
1453             {
1454                 int i;
1455                 int i_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
1456
1457                 msg_Warn( p_demux, "found SL_descriptor es_id=%d", i_es_id );
1458
1459                 pid->es->p_mpeg4desc = NULL;
1460
1461                 for( i = 0; i < 255; i++ )
1462                 {
1463                     iod_descriptor_t *iod = pmt->psi->iod;
1464
1465                     if( iod->es_descr[i].b_ok &&
1466                         iod->es_descr[i].i_es_id == i_es_id )
1467                     {
1468                         pid->es->p_mpeg4desc = &iod->es_descr[i];
1469                         break;
1470                     }
1471                 }
1472             }
1473
1474             if( pid->es->p_mpeg4desc != NULL )
1475             {
1476                 decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;
1477
1478                 if( dcd->i_streamType == 0x04 )    /* VisualStream */
1479                 {
1480                     pid->es->fmt.i_cat = VIDEO_ES;
1481                     switch( dcd->i_objectTypeIndication )
1482                     {
1483                         case 0x20:
1484                             pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','v');    // mpeg4
1485                             break;
1486                         case 0x60:
1487                         case 0x61:
1488                         case 0x62:
1489                         case 0x63:
1490                         case 0x64:
1491                         case 0x65:
1492                             pid->es->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );  // mpeg2
1493                             break;
1494                         case 0x6a:
1495                             pid->es->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );  // mpeg1
1496                             break;
1497                         case 0x6c:
1498                             pid->es->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );  // mpeg1
1499                             break;
1500
1501                         default:
1502                             pid->es->fmt.i_cat = UNKNOWN_ES;
1503                             break;
1504                     }
1505                 }
1506                 else if( dcd->i_streamType == 0x05 )    /* AudioStream */
1507                 {
1508                     pid->es->fmt.i_cat = AUDIO_ES;
1509                     switch( dcd->i_objectTypeIndication )
1510                     {
1511                         case 0x40:
1512                             pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','a');    // mpeg4
1513                             break;
1514                         case 0x66:
1515                         case 0x67:
1516                         case 0x68:
1517                             pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','a');// mpeg2 aac
1518                             break;
1519                         case 0x69:
1520                             pid->es->fmt.i_codec = VLC_FOURCC('m','p','g','a');    // mpeg2
1521                             break;
1522                         case 0x6b:
1523                             pid->es->fmt.i_codec = VLC_FOURCC('m','p','g','a');    // mpeg1
1524                             break;
1525                         default:
1526                             pid->es->fmt.i_cat = UNKNOWN_ES;
1527                             break;
1528                     }
1529                 }
1530                 else
1531                 {
1532                     pid->es->fmt.i_cat = UNKNOWN_ES;
1533                 }
1534
1535                 if( pid->es->fmt.i_cat != UNKNOWN_ES )
1536                 {
1537                     pid->es->fmt.i_extra = dcd->i_decoder_specific_info_len;
1538                     if( pid->es->fmt.i_extra > 0 )
1539                     {
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 );
1544                     }
1545                 }
1546             }
1547         }
1548         else if( p_es->i_type == 0x06 )
1549         {
1550             dvbpsi_descriptor_t *p_dr;
1551
1552             for( p_dr = p_es->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
1553             {
1554                 if( p_dr->i_tag == 0x6a )
1555                 {
1556                     pid->es->fmt.i_cat = AUDIO_ES;
1557                     pid->es->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
1558                 }
1559 #ifdef _DVBPSI_DR_59_H_
1560                 else if( p_dr->i_tag == 0x59 )
1561                 {
1562                     /* DVB subtitle */
1563                     /* TODO */
1564                 }
1565 #endif
1566             }
1567         }
1568         else if( p_es->i_type == 0xa0 )
1569         {
1570             /* MSCODEC send by vlc */
1571             dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
1572
1573             while( p_dr && ( p_dr->i_tag != 0xa0 ) ) p_dr = p_dr->p_next;
1574
1575             if( p_dr && p_dr->i_length >= 8 )
1576             {
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];
1583
1584                 if( pid->es->fmt.i_extra > 0 )
1585                 {
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 );
1588                 }
1589             }
1590             else
1591             {
1592                 msg_Warn( p_demux, "private MSCODEC (vlc) without bih private descriptor" );
1593             }
1594         }
1595
1596         if( pid->es->fmt.i_cat == AUDIO_ES || pid->es->fmt.i_cat == SPU_ES )
1597         {
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;
1601
1602             if( p_dr )
1603             {
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 );
1607             }
1608         }
1609
1610         pid->es->fmt.i_group = p_pmt->i_program_number;
1611         if( pid->es->fmt.i_cat == UNKNOWN_ES )
1612         {
1613             msg_Dbg( p_demux, "  * es pid=0x%x type=0x%x *unknown*", p_es->i_pid, p_es->i_type );
1614         }
1615         else
1616         {
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 )
1619             {
1620                 pid->es->fmt.i_id = p_es->i_pid;
1621             }
1622             pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
1623         }
1624     }
1625     dvbpsi_DeletePMT(p_pmt);
1626 }
1627
1628 static void PATCallBack( demux_t *p_demux, dvbpsi_pat_t *p_pat )
1629 {
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];
1633     int                  i;
1634
1635     msg_Dbg( p_demux, "PATCallBack called" );
1636
1637     if( pat->psi->i_version != -1 && ( !p_pat->b_current_next || p_pat->i_version == pat->psi->i_version ) )
1638     {
1639         dvbpsi_DeletePAT( p_pat );
1640         return;
1641     }
1642
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 );
1644
1645     /* Clean old */
1646     for( i = 2; i < 8192; i++ )
1647     {
1648         ts_pid_t *pid = &p_sys->pid[i];
1649
1650         if( pid->b_valid )
1651         {
1652             if( pid->psi )
1653             {
1654                 if( pid->p_owner == pat->psi )
1655                 {
1656                     PIDClean( p_demux->out, pid );
1657                     TAB_REMOVE( p_sys->i_pmt, p_sys->pmt, pid );
1658                 }
1659             }
1660             else if( pid->p_owner && pid->p_owner->i_number != 0 && pid->es->id )
1661             {
1662                 /* We only remove es that aren't defined by extra pmt */
1663                 PIDClean( p_demux->out, pid );
1664             }
1665         }
1666     }
1667
1668     /* now create programs */
1669     for( p_program = p_pat->p_first_program; p_program != NULL; p_program = p_program->p_next )
1670     {
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 )
1673         {
1674             ts_pid_t *pmt = &p_sys->pid[p_program->i_pid];
1675
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;
1679
1680             TAB_APPEND( p_sys->i_pmt, p_sys->pmt, pmt );
1681         }
1682     }
1683     pat->psi->i_version = p_pat->i_version;
1684
1685     dvbpsi_DeletePAT( p_pat );
1686 }
1687