]> git.sesse.net Git - vlc/blob - modules/demux/ts.c
* src/input/input_programs.c: Fixed the --program option (thanks gibalou)
[vlc] / modules / demux / ts.c
1 /*****************************************************************************
2  * ts.c: Transport Stream input module for VLC.
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
5  * $Id$
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 #include "network.h"
34
35 #include "../mux/mpeg/csa.h"
36
37 /* Include dvbpsi headers */
38 #ifdef HAVE_DVBPSI_DR_H
39 #   include <dvbpsi/dvbpsi.h>
40 #   include <dvbpsi/descriptor.h>
41 #   include <dvbpsi/pat.h>
42 #   include <dvbpsi/pmt.h>
43 #   include <dvbpsi/dr.h>
44 #   include <dvbpsi/psi.h>
45 #else
46 #   include "dvbpsi.h"
47 #   include "descriptor.h"
48 #   include "tables/pat.h"
49 #   include "tables/pmt.h"
50 #   include "descriptors/dr.h"
51 #   include "psi.h"
52 #endif
53
54 /* TODO:
55  *  - XXX: do not mark options message to be translated, they are too osbcure for now ...
56  *  - test it
57  *  - ...
58  */
59
60 /*****************************************************************************
61  * Module descriptor
62  *****************************************************************************/
63 static int  Open  ( vlc_object_t * );
64 static void Close ( vlc_object_t * );
65
66 vlc_module_begin();
67     set_description( _("ISO 13818-1 MPEG Transport Stream input - new" ) );
68     add_string( "ts-extra-pmt", NULL, NULL, "extra PMT", "allow user to specify an extra pmt (pmt_pid=pid:stream_type[,...])", VLC_TRUE );
69     add_bool( "ts-es-id-pid", 0, NULL, "set id of es to pid", "set id of es to pid", VLC_TRUE );
70     add_string( "ts-out", NULL, NULL, "fast udp streaming", "send TS to specific ip:port by udp (you must know what you are doing)", VLC_TRUE );
71     add_integer( "ts-out-mtu", 1500, NULL, "MTU for out mode", "MTU for out mode", VLC_TRUE );
72     add_string( "ts-csa-ck", NULL, NULL, "CSA ck", "CSA ck", VLC_TRUE );
73     add_bool( "ts-silent", 0, NULL, "Silent mode", "do not complain on encrypted PES", VLC_TRUE );
74     set_capability( "demux2", 10 );
75     set_callbacks( Open, Close );
76     add_shortcut( "ts2" );
77 vlc_module_end();
78
79 /*****************************************************************************
80  * Local prototypes
81  *****************************************************************************/
82
83 typedef struct
84 {
85     uint8_t                 i_objectTypeIndication;
86     uint8_t                 i_streamType;
87     vlc_bool_t              b_upStream;
88     uint32_t                i_bufferSizeDB;
89     uint32_t                i_maxBitrate;
90     uint32_t                i_avgBitrate;
91
92     int                     i_decoder_specific_info_len;
93     uint8_t                 *p_decoder_specific_info;
94
95 } decoder_config_descriptor_t;
96
97 typedef struct
98 {
99     vlc_bool_t              b_useAccessUnitStartFlag;
100     vlc_bool_t              b_useAccessUnitEndFlag;
101     vlc_bool_t              b_useRandomAccessPointFlag;
102     vlc_bool_t              b_useRandomAccessUnitsOnlyFlag;
103     vlc_bool_t              b_usePaddingFlag;
104     vlc_bool_t              b_useTimeStampsFlags;
105     vlc_bool_t              b_useIdleFlag;
106     vlc_bool_t              b_durationFlag;
107     uint32_t                i_timeStampResolution;
108     uint32_t                i_OCRResolution;
109     uint8_t                 i_timeStampLength;
110     uint8_t                 i_OCRLength;
111     uint8_t                 i_AU_Length;
112     uint8_t                 i_instantBitrateLength;
113     uint8_t                 i_degradationPriorityLength;
114     uint8_t                 i_AU_seqNumLength;
115     uint8_t                 i_packetSeqNumLength;
116
117     uint32_t                i_timeScale;
118     uint16_t                i_accessUnitDuration;
119     uint16_t                i_compositionUnitDuration;
120
121     uint64_t                i_startDecodingTimeStamp;
122     uint64_t                i_startCompositionTimeStamp;
123
124 } sl_config_descriptor_t;
125
126 typedef struct
127 {
128     vlc_bool_t              b_ok;
129     uint16_t                i_es_id;
130
131     vlc_bool_t              b_streamDependenceFlag;
132     vlc_bool_t              b_OCRStreamFlag;
133     uint8_t                 i_streamPriority;
134
135     char                    *psz_url;
136
137     uint16_t                i_dependOn_es_id;
138     uint16_t                i_OCR_es_id;
139
140     decoder_config_descriptor_t    dec_descr;
141     sl_config_descriptor_t         sl_descr;
142 } es_mpeg4_descriptor_t;
143
144 typedef struct
145 {
146     uint8_t                i_iod_label;
147
148     /* IOD */
149     uint16_t                i_od_id;
150     char                    *psz_url;
151
152     uint8_t                 i_ODProfileLevelIndication;
153     uint8_t                 i_sceneProfileLevelIndication;
154     uint8_t                 i_audioProfileLevelIndication;
155     uint8_t                 i_visualProfileLevelIndication;
156     uint8_t                 i_graphicsProfileLevelIndication;
157
158     es_mpeg4_descriptor_t   es_descr[255];
159
160 } iod_descriptor_t;
161
162 typedef struct
163 {
164     int             i_version;
165     int             i_number;
166     int             i_pid_pcr;
167     dvbpsi_handle   handle;
168
169     /* IOD stuff (mpeg4) */
170     iod_descriptor_t *iod;
171
172 } ts_psi_t;
173
174 typedef struct
175 {
176     es_format_t  fmt;
177     es_out_id_t *id;
178     block_t     *p_pes;
179
180     es_mpeg4_descriptor_t *p_mpeg4desc;
181 } ts_es_t;
182
183 typedef struct
184 {
185     int         i_pid;
186
187     vlc_bool_t  b_seen;
188     vlc_bool_t  b_valid;
189     int         i_cc;   /* countinuity counter */
190
191     /* PSI owner (ie PMT -> PAT, ES -> PMT */
192     ts_psi_t   *p_owner;
193
194     /* */
195     ts_psi_t    *psi;
196     ts_es_t     *es;
197
198     /* Some private streams encapsulate several ES (eg. DVB subtitles)*/
199     ts_es_t     **extra_es;
200     int         i_extra_es;
201
202 } ts_pid_t;
203
204 struct demux_sys_t
205 {
206     /* how many TS packet we read at once */
207     int         i_ts_read;
208
209     /* All pid */
210     ts_pid_t    pid[8192];
211
212     /* All PMT */
213     int         i_pmt;
214     ts_pid_t    **pmt;
215
216     /* */
217     vlc_bool_t  b_es_id_pid;
218     csa_t       *csa;
219     vlc_bool_t  b_silent;
220
221     vlc_bool_t  b_udp_out;
222     int         fd; /* udp socket */
223     uint8_t     *buffer;
224 };
225
226 static int Demux  ( demux_t *p_demux );
227 static int Control( demux_t *p_demux, int i_query, va_list args );
228
229
230 static void PIDInit ( ts_pid_t *pid, vlc_bool_t b_psi, ts_psi_t *p_owner );
231 static void PIDClean( es_out_t *out, ts_pid_t *pid );
232 static int  PIDFillFormat( ts_pid_t *pid, int i_stream_type );
233
234 static void PATCallBack( demux_t *, dvbpsi_pat_t * );
235 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt );
236
237 static inline int PIDGet( block_t *p )
238 {
239     return ( (p->p_buffer[1]&0x1f)<<8 )|p->p_buffer[2];
240 }
241
242 static vlc_bool_t GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk );
243
244 static void PCRHandle( demux_t *p_demux, ts_pid_t *, block_t * );
245
246 static iod_descriptor_t *IODNew( int , uint8_t * );
247 static void              IODFree( iod_descriptor_t * );
248
249
250 /*****************************************************************************
251  * Open
252  *****************************************************************************/
253 static int Open( vlc_object_t *p_this )
254 {
255     demux_t     *p_demux = (demux_t*)p_this;
256     demux_sys_t *p_sys;
257
258     uint8_t     *p_peek;
259     int          i_peek;
260     int          i_sync;
261     int          i;
262
263     ts_pid_t     *pat;
264
265     vlc_value_t  val;
266
267     if( ( i_peek = stream_Peek( p_demux->s, &p_peek, 189 ) ) < 1 )
268     {
269         msg_Err( p_demux, "cannot peek" );
270         return VLC_EGENERIC;
271     }
272
273     /* Search first synch */
274     for( i_sync = 0; i_sync < i_peek; i_sync++ )
275     {
276         if( p_peek[i_sync] == 0x47 ) break;
277     }
278     if( i_sync >= i_peek )
279     {
280         if( strcmp( p_demux->psz_demux, "ts2" ) )
281         {
282             msg_Warn( p_demux, "TS module discarded" );
283             return VLC_EGENERIC;
284         }
285         msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
286     }
287     if( strcmp( p_demux->psz_demux, "ts2" ) )
288     {
289         /* Check next 3 sync points */
290         i_peek = 188*3 + 1 + i_sync;
291         if( ( stream_Peek( p_demux->s, &p_peek, i_peek ) ) < i_peek )
292         {
293             msg_Err( p_demux, "cannot peek" );
294             return VLC_EGENERIC;
295         }
296         if( p_peek[i_sync+  188] != 0x47 || p_peek[i_sync+2*188] != 0x47 ||
297             p_peek[i_sync+3*188] != 0x47 )
298         {
299             msg_Warn( p_demux, "TS module discarded (lost sync)" );
300             return VLC_EGENERIC;
301         }
302     }
303
304     /* Fill p_demux field */
305     p_demux->pf_demux = Demux;
306     p_demux->pf_control = Control;
307     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
308     memset( p_sys, 0, sizeof( demux_sys_t ) );
309
310     /* Init p_sys field */
311     for( i = 0; i < 8192; i++ )
312     {
313         ts_pid_t *pid = &p_sys->pid[i];
314
315         pid->i_pid      = i;
316         pid->b_seen     = VLC_FALSE;
317         pid->b_valid    = VLC_FALSE;
318     }
319     p_sys->b_udp_out = VLC_FALSE;
320     p_sys->i_ts_read = 50;
321     p_sys->csa = NULL;
322
323     /* Init PAT handler */
324     pat = &p_sys->pid[0];
325     PIDInit( pat, VLC_TRUE, NULL );
326     pat->psi->handle = dvbpsi_AttachPAT( (dvbpsi_pat_callback)PATCallBack,
327                                          p_demux );
328
329     /* Init PMT array */
330     p_sys->i_pmt = 0;
331     p_sys->pmt   = NULL;
332
333     /* Read config */
334     var_Create( p_demux, "ts-es-id-pid", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
335     var_Get( p_demux, "ts-es-id-pid", &val );
336     p_sys->b_es_id_pid = val.b_bool,
337
338     var_Create( p_demux, "ts-out", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
339     var_Get( p_demux, "ts-out", &val );
340     if( val.psz_string && *val.psz_string )
341     {
342         vlc_value_t mtu;
343         char *psz = strchr( val.psz_string, ':' );
344         int   i_port = 0;
345
346         p_sys->b_udp_out = VLC_TRUE;
347
348         if( psz )
349         {
350             *psz++ = '\0';
351             i_port = atoi( psz );
352         }
353         if( i_port <= 0 ) i_port  = 1234;
354         msg_Dbg( p_demux, "resend ts to '%s:%d'", val.psz_string, i_port );
355
356         p_sys->fd = net_OpenUDP( p_demux, "", 0, val.psz_string, i_port );
357         if( p_sys->fd < 0 )
358         {
359             msg_Err( p_demux, "failed to open udp socket, send disabled" );
360             p_sys->b_udp_out = VLC_FALSE;
361         }
362         else
363         {
364             var_Create( p_demux, "ts-out-mtu",
365                         VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
366             var_Get( p_demux, "ts-out-mtu", &mtu );
367             p_sys->i_ts_read = mtu.i_int / 188;
368             if( p_sys->i_ts_read <= 0 )
369             {
370                 p_sys->i_ts_read = 1500 / 188;
371             }
372             p_sys->buffer = malloc( 188 * p_sys->i_ts_read );
373         }
374     }
375     if( val.psz_string )
376     {
377         free( val.psz_string );
378     }
379
380
381     /* We handle description of an extra PMT */
382     var_Create( p_demux, "ts-extra-pmt", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
383     var_Get( p_demux, "ts-extra-pmt", &val );
384     if( val.psz_string && strchr( val.psz_string, '=' ) != NULL )
385     {
386         char *psz = val.psz_string;
387         int  i_pid = strtol( psz, &psz, 0 );
388
389         if( i_pid >= 2 && i_pid < 8192 )
390         {
391             ts_pid_t *pmt = &p_sys->pid[i_pid];
392
393             msg_Dbg( p_demux, "extra pmt specified (pid=0x%x)", i_pid );
394             PIDInit( pmt, VLC_TRUE, NULL );
395             /* FIXME we should also ask for a number */
396             pmt->psi->handle =
397                 dvbpsi_AttachPMT( 1, (dvbpsi_pmt_callback)PMTCallBack,
398                                   p_demux );
399             pmt->psi->i_number = 0; /* special one */
400
401             psz = strchr( psz, '=' ) + 1;   /* can't failed */
402             while( psz && *psz )
403             {
404                 char *psz_next = strchr( psz, ',' );
405                 int i_pid, i_stream_type;
406
407                 if( psz_next )
408                 {
409                     *psz_next++ = '\0';
410                 }
411
412                 i_pid = strtol( psz, &psz, 0 );
413                 if( *psz == ':' )
414                 {
415                     i_stream_type = strtol( psz + 1, &psz, 0 );
416                     if( i_pid >= 2 && i_pid < 8192 &&
417                         !p_sys->pid[i_pid].b_valid )
418                     {
419                         ts_pid_t *pid = &p_sys->pid[i_pid];
420
421                         PIDInit( pid, VLC_FALSE, pmt->psi);
422                         if( pmt->psi->i_pid_pcr <= 0 )
423                         {
424                             pmt->psi->i_pid_pcr = i_pid;
425                         }
426                         PIDFillFormat( pid, i_stream_type);
427                         if( pid->es->fmt.i_cat != UNKNOWN_ES )
428                         {
429                             if( p_sys->b_es_id_pid )
430                             {
431                                 pid->es->fmt.i_id = i_pid;
432                             }
433                             msg_Dbg( p_demux, "  * es pid=0x%x type=0x%x "
434                                      "fcc=%4.4s", i_pid, i_stream_type,
435                                      (char*)&pid->es->fmt.i_codec );
436                             pid->es->id = es_out_Add( p_demux->out,
437                                                       &pid->es->fmt );
438                         }
439                     }
440                 }
441                 psz = psz_next;
442             }
443         }
444     }
445     if( val.psz_string )
446     {
447         free( val.psz_string );
448     }
449
450     var_Create( p_demux, "ts-csa-ck", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
451     var_Get( p_demux, "ts-csa-ck", &val );
452     if( val.psz_string && *val.psz_string )
453     {
454         char *psz = val.psz_string;
455         if( psz[0] == '0' && ( psz[1] == 'x' || psz[1] == 'X' ) )
456         {
457             psz += 2;
458         }
459         if( strlen( psz ) != 16 )
460         {
461             msg_Warn( p_demux, "invalid csa ck (it must be 16 chars long)" );
462         }
463         else
464         {
465             uint64_t i_ck = strtoll( psz, NULL, 16 );
466             uint8_t ck[8];
467             int     i;
468             for( i = 0; i < 8; i++ )
469             {
470                 ck[i] = ( i_ck >> ( 56 - 8*i) )&0xff;
471             }
472
473             msg_Dbg( p_demux, "using CSA scrambling with "
474                      "ck=%x:%x:%x:%x:%x:%x:%x:%x",
475                      ck[0], ck[1], ck[2], ck[3], ck[4], ck[5], ck[6], ck[7] );
476
477             p_sys->csa = csa_New();
478             csa_SetCW( p_sys->csa, ck, ck );
479         }
480     }
481     if( val.psz_string )
482     {
483         free( val.psz_string );
484     }
485
486     var_Create( p_demux, "ts-silent", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
487     var_Get( p_demux, "ts-silent", &val );
488     p_sys->b_silent = val.b_bool;
489
490     return VLC_SUCCESS;
491 }
492
493 /*****************************************************************************
494  * Close
495  *****************************************************************************/
496 static void Close( vlc_object_t *p_this )
497 {
498     demux_t     *p_demux = (demux_t*)p_this;
499     demux_sys_t *p_sys = p_demux->p_sys;
500
501     int          i;
502
503     msg_Dbg( p_demux, "pid list:" );
504     for( i = 0; i < 8192; i++ )
505     {
506         ts_pid_t *pid = &p_sys->pid[i];
507
508         if( pid->b_valid && pid->psi )
509         {
510             switch( pid->i_pid )
511             {
512                 case 0: /* PAT */
513                     dvbpsi_DetachPAT( pid->psi->handle );
514                     free( pid->psi );
515                     break;
516                 case 1: /* CAT */
517                     free( pid->psi );
518                     break;
519                 default:
520                     PIDClean( p_demux->out, pid );
521                     break;
522             }
523         }
524         else if( pid->b_valid && pid->es )
525         {
526             PIDClean( p_demux->out, pid );
527         }
528
529         if( pid->b_seen )
530         {
531             msg_Dbg( p_demux, "  - pid[0x%x] seen", pid->i_pid );
532         }
533     }
534
535     if( p_sys->b_udp_out )
536     {
537         net_Close( p_sys->fd );
538         free( p_sys->buffer );
539     }
540     if( p_sys->csa )
541     {
542         csa_Delete( p_sys->csa );
543     }
544
545     if( p_sys->i_pmt ) free( p_sys->pmt );
546     free( p_sys );
547 }
548
549 /*****************************************************************************
550  * Demux:
551  *****************************************************************************/
552 static int Demux( demux_t *p_demux )
553 {
554     demux_sys_t *p_sys = p_demux->p_sys;
555     int          i_pkt;
556
557     /* We read at most 100 TS packet or until a frame is completed */
558     for( i_pkt = 0; i_pkt < p_sys->i_ts_read; i_pkt++ )
559     {
560         vlc_bool_t  b_frame = VLC_FALSE;
561         block_t     *p_pkt;
562         ts_pid_t    *p_pid;
563
564         /* Get a new TS packet */
565         if( ( p_pkt = stream_Block( p_demux->s, 188 ) ) == NULL )
566         {
567             msg_Dbg( p_demux, "eof ?" );
568             return 0;
569         }
570         if( p_pkt->p_buffer[0] != 0x47 )
571         {
572             msg_Warn( p_demux, "lost synchro" );
573             block_Release( p_pkt );
574
575             /* Resynch */
576             while( !p_demux->b_die )
577             {
578                 uint8_t    *p_peek;
579                 int         i_peek = stream_Peek( p_demux->s, &p_peek, 1880 );
580                 int         i_skip = 0;
581                 vlc_bool_t  b_ok = VLC_FALSE;
582
583                 if( i_peek < 189 )
584                 {
585                     msg_Dbg( p_demux, "eof ?" );
586                     return 0;
587                 }
588
589                 while( i_skip < i_peek - 188 )
590                 {
591                     if( p_peek[i_skip] == 0x47 && p_peek[i_skip+188] == 0x47 )
592                     {
593                         b_ok = VLC_TRUE;
594                         break;
595                     }
596                     i_skip++;
597                 }
598                 stream_Read( p_demux->s, NULL, i_skip );
599                 msg_Dbg( p_demux, "%d bytes of garbage", i_skip );
600                 if( b_ok )
601                 {
602                     break;
603                 }
604             }
605             if( ( p_pkt = stream_Block( p_demux->s, 188 ) ) == NULL )
606             {
607                 msg_Dbg( p_demux, "eof ?" );
608                 return 0;
609             }
610         }
611
612         if( p_sys->b_udp_out )
613         {
614             memcpy( &p_sys->buffer[i_pkt*188], p_pkt->p_buffer, 188 );
615         }
616
617         /* Parse the TS packet */
618         p_pid = &p_sys->pid[PIDGet( p_pkt )];
619
620         if( p_pid->b_valid )
621         {
622             if( p_pid->psi )
623             {
624                 dvbpsi_PushPacket( p_pid->psi->handle, p_pkt->p_buffer );
625                 block_Release( p_pkt );
626             }
627             else if( !p_sys->b_udp_out )
628             {
629                 b_frame = GatherPES( p_demux, p_pid, p_pkt );
630             }
631             else
632             {
633                 block_Release( p_pkt );
634             }
635         }
636         else
637         {
638             if( !p_pid->b_seen )
639             {
640                 msg_Dbg( p_demux, "pid[0x%x] unknown", p_pid->i_pid );
641             }
642             /* We have to handle PCR if present */
643             PCRHandle( p_demux, p_pid, p_pkt );
644             block_Release( p_pkt );
645         }
646         p_pid->b_seen = VLC_TRUE;
647
648         if( b_frame )
649         {
650             break;
651         }
652     }
653
654     if( p_sys->b_udp_out )
655     {
656         /* Send the complete block */
657         net_Write( p_demux, p_sys->fd, p_sys->buffer, p_sys->i_ts_read * 188 );
658     }
659
660     return 1;
661 }
662
663 /*****************************************************************************
664  * Control:
665  *****************************************************************************/
666 static int Control( demux_t *p_demux, int i_query, va_list args )
667 {
668     /* demux_sys_t *p_sys = p_demux->p_sys; */
669     double f, *pf;
670     int64_t i64;
671
672     switch( i_query )
673     {
674         case DEMUX_GET_POSITION:
675             pf = (double*) va_arg( args, double* );
676             i64 = stream_Size( p_demux->s );
677             if( i64 > 0 )
678             {
679                 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
680             }
681             else
682             {
683                 *pf = 0.0;
684             }
685             return VLC_SUCCESS;
686         case DEMUX_SET_POSITION:
687             f = (double) va_arg( args, double );
688             i64 = stream_Size( p_demux->s );
689
690             es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
691             if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) )
692             {
693                 return VLC_EGENERIC;
694             }
695             return VLC_SUCCESS;
696 #if 0
697
698         case DEMUX_GET_TIME:
699             pi64 = (int64_t*)va_arg( args, int64_t * );
700             if( p_sys->i_time < 0 )
701             {
702                 *pi64 = 0;
703                 return VLC_EGENERIC;
704             }
705             *pi64 = p_sys->i_time;
706             return VLC_SUCCESS;
707
708         case DEMUX_GET_LENGTH:
709             pi64 = (int64_t*)va_arg( args, int64_t * );
710             if( p_sys->i_mux_rate > 0 )
711             {
712                 *pi64 = I64C(1000000) * ( stream_Size( p_demux->s ) / 50 ) /
713                         p_sys->i_mux_rate;
714                 return VLC_SUCCESS;
715             }
716             *pi64 = 0;
717             return VLC_EGENERIC;
718 #endif
719         case DEMUX_GET_FPS:
720         case DEMUX_SET_TIME:
721         default:
722             return VLC_EGENERIC;
723     }
724 }
725
726 static void PIDInit( ts_pid_t *pid, vlc_bool_t b_psi, ts_psi_t *p_owner )
727 {
728     pid->b_valid    = VLC_TRUE;
729     pid->i_cc       = 0xff;
730     pid->p_owner    = p_owner;
731
732     pid->extra_es   = NULL;
733     pid->i_extra_es = 0;
734
735     if( b_psi )
736     {
737         pid->psi = malloc( sizeof( ts_psi_t ) );
738         pid->es  = NULL;
739
740         pid->psi->i_version  = -1;
741         pid->psi->i_number   = -1;
742         pid->psi->i_pid_pcr  = -1;
743         pid->psi->handle     = NULL;
744         pid->psi->iod        = NULL;
745     }
746     else
747     {
748         pid->psi = NULL;
749         pid->es  = malloc( sizeof( ts_es_t ) );
750
751         es_format_Init( &pid->es->fmt, UNKNOWN_ES, 0 );
752         pid->es->id      = NULL;
753         pid->es->p_pes   = NULL;
754         pid->es->p_mpeg4desc = NULL;
755     }
756 }
757
758 static void PIDClean( es_out_t *out, ts_pid_t *pid )
759 {
760     if( pid->psi )
761     {
762         if( pid->psi->handle ) dvbpsi_DetachPMT( pid->psi->handle );
763         if( pid->psi->iod ) IODFree( pid->psi->iod );
764         free( pid->psi );
765     }
766     else
767     {
768         int i;
769
770         if( pid->es->id )
771             es_out_Del( out, pid->es->id );
772
773         if( pid->es->p_pes )
774             block_ChainRelease( pid->es->p_pes );
775
776         es_format_Clean( &pid->es->fmt );
777
778         free( pid->es );
779
780         for( i = 0; i < pid->i_extra_es; i++ )
781         {
782             if( pid->extra_es[i]->id )
783                 es_out_Del( out, pid->extra_es[i]->id );
784
785             if( pid->extra_es[i]->p_pes )
786                 block_ChainRelease( pid->extra_es[i]->p_pes );
787
788             es_format_Clean( &pid->extra_es[i]->fmt );
789
790             free( pid->extra_es[i] );
791         }
792         if( pid->i_extra_es ) free( pid->extra_es );
793     }
794
795     pid->b_valid = VLC_FALSE;
796 }
797
798 /****************************************************************************
799  * gathering stuff
800  ****************************************************************************/
801 static void ParsePES ( demux_t *p_demux, ts_pid_t *pid )
802 {
803     block_t *p_pes = pid->es->p_pes;
804     uint8_t header[30];
805     int     i_pes_size;
806     int     i_skip = 0;
807     mtime_t i_dts = -1;
808     mtime_t i_pts = -1;
809     int i_max;
810
811     /* remove the pes from pid */
812     pid->es->p_pes = NULL;
813
814     /* FIXME find real max size */
815     i_max = block_ChainExtract( p_pes, header, 30 );
816
817     if( header[0] != 0 || header[1] != 0 || header[2] != 1 )
818     {
819         if ( !p_demux->p_sys->b_silent )
820             msg_Warn( p_demux, "invalid header [0x%x:%x:%x:%x]", header[0],
821                       header[1],header[2],header[3] );
822         block_ChainRelease( p_pes );
823         return;
824     }
825
826     i_pes_size = (header[4] << 8)|header[5];
827
828     /* TODO check size */
829
830     switch( header[3] )
831     {
832         case 0xBC:  /* Program stream map */
833         case 0xBE:  /* Padding */
834         case 0xBF:  /* Private stream 2 */
835         case 0xB0:  /* ECM */
836         case 0xB1:  /* EMM */
837         case 0xFF:  /* Program stream directory */
838         case 0xF2:  /* DSMCC stream */
839         case 0xF8:  /* ITU-T H.222.1 type E stream */
840             i_skip = 6;
841             break;
842         default:
843             if( ( header[6]&0xC0 ) == 0x80 )
844             {
845                 /* mpeg2 PES */
846                 i_skip = header[8] + 9;
847
848                 if( header[7]&0x80 )    /* has pts */
849                 {
850                     i_pts = ((mtime_t)(header[ 9]&0x0e ) << 29)|
851                              (mtime_t)(header[10] << 22)|
852                             ((mtime_t)(header[11]&0xfe) << 14)|
853                              (mtime_t)(header[12] << 7)|
854                              (mtime_t)(header[12] >> 1);
855
856                     if( header[7]&0x40 )    /* has dts */
857                     {
858                          i_dts = ((mtime_t)(header[14]&0x0e ) << 29)|
859                                  (mtime_t)(header[15] << 22)|
860                                 ((mtime_t)(header[16]&0xfe) << 14)|
861                                  (mtime_t)(header[17] << 7)|
862                                  (mtime_t)(header[18] >> 1);
863                     }
864                 }
865             }
866             else
867             {
868                 i_skip = 6;
869                 while( i_skip < 23 && header[i_skip] == 0xff )
870                 {
871                     i_skip++;
872                 }
873                 if( i_skip == 23 )
874                 {
875                     msg_Err( p_demux, "too much MPEG-1 stuffing" );
876                     block_ChainRelease( p_pes );
877                     return;
878                 }
879                 if( ( header[i_skip] & 0xC0 ) == 0x40 )
880                 {
881                     i_skip += 2;
882                 }
883
884                 if(  header[i_skip]&0x20 )
885                 {
886                      p_pes->i_pts = ((mtime_t)(header[i_skip]&0x0e ) << 29)|
887                                      (mtime_t)(header[i_skip+1] << 22)|
888                                     ((mtime_t)(header[i_skip+2]&0xfe) << 14)|
889                                      (mtime_t)(header[i_skip+3] << 7)|
890                                      (mtime_t)(header[i_skip+4] >> 1);
891
892                     if( header[i_skip]&0x10 )    /* has dts */
893                     {
894                          p_pes->i_dts = ((mtime_t)(header[i_skip+5]&0x0e ) << 29)|
895                                          (mtime_t)(header[i_skip+6] << 22)|
896                                         ((mtime_t)(header[i_skip+7]&0xfe) << 14)|
897                                          (mtime_t)(header[i_skip+8] << 7)|
898                                          (mtime_t)(header[i_skip+9] >> 1);
899                          i_skip += 10;
900                     }
901                     else
902                     {
903                         i_skip += 5;
904                     }
905                 }
906                 else
907                 {
908                     i_skip += 1;
909                 }
910             }
911             break;
912     }
913
914     if( pid->es->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', 'b' ) ||
915         pid->es->fmt.i_codec == VLC_FOURCC( 'd', 't', 's', 'b' ) )
916     {
917         i_skip += 4;
918     }
919     else if( pid->es->fmt.i_codec == VLC_FOURCC( 'l', 'p', 'c', 'b' ) ||
920              pid->es->fmt.i_codec == VLC_FOURCC( 's', 'p', 'u', 'b' ) ||
921              pid->es->fmt.i_codec == VLC_FOURCC( 's', 'd', 'd', 'b' ) )
922     {
923         i_skip += 1;
924     }
925
926     /* skip header */
927     while( p_pes && i_skip > 0 )
928     {
929         if( p_pes->i_buffer <= i_skip )
930         {
931             block_t *p_next = p_pes->p_next;
932
933             i_skip -= p_pes->i_buffer;
934             block_Release( p_pes );
935             p_pes = p_next;
936         }
937         else
938         {
939             p_pes->i_buffer -= i_skip;
940             p_pes->p_buffer += i_skip;
941             break;
942         }
943     }
944
945     if( p_pes )
946     {
947         block_t *p_block;
948         int i;
949
950         if( i_dts >= 0 )
951         {
952             p_pes->i_dts = i_dts * 100 / 9;
953         }
954         if( i_pts >= 0 )
955         {
956             p_pes->i_pts = i_pts * 100 / 9;
957         }
958
959         /* For mpeg4/mscodec we first gather the packet.
960          * This will make ffmpeg a lot happier */
961         p_block = block_ChainGather( p_pes );
962
963         for( i = 0; i < pid->i_extra_es; i++ )
964         {
965             es_out_Send( p_demux->out, pid->extra_es[i]->id,
966                          block_Duplicate( p_block ) );
967         }
968
969         es_out_Send( p_demux->out, pid->es->id, p_block );
970     }
971     else
972     {
973         msg_Warn( p_demux, "empty pes" );
974     }
975 }
976
977 static void PCRHandle( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
978 {
979     demux_sys_t   *p_sys = p_demux->p_sys;
980     const uint8_t *p = p_bk->p_buffer;
981
982     if( ( p[3]&0x20 ) && /* adaptation */
983         ( p[5]&0x10 ) &&
984         ( p[4] >= 7 ) )
985     {
986         int i;
987         mtime_t i_pcr;  /* 33 bits */
988
989         i_pcr = ( (mtime_t)p[6] << 25 ) |
990                 ( (mtime_t)p[7] << 17 ) |
991                 ( (mtime_t)p[8] << 9 ) |
992                 ( (mtime_t)p[9] << 1 ) |
993                 ( (mtime_t)p[10] >> 7 );
994
995         /* Search program and set the PCR */
996         for( i = 0; i < p_sys->i_pmt; i++ )
997         {
998             if( pid->i_pid == p_sys->pmt[i]->psi->i_pid_pcr )
999             {
1000                 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
1001                                 (int)p_sys->pmt[i]->psi->i_number,
1002                                 (int64_t)(i_pcr * 100 / 9) );
1003             }
1004         }
1005     }
1006 }
1007
1008 static vlc_bool_t GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
1009 {
1010     const uint8_t    *p = p_bk->p_buffer;
1011     const vlc_bool_t  b_adaptation= p[3]&0x20;
1012     const vlc_bool_t  b_payload   = p[3]&0x10;
1013     const int         i_cc        = p[3]&0x0f;   /* continuity counter */
1014     /* transport_scrambling_control is ignored */
1015
1016     int         i_skip = 0;
1017     vlc_bool_t  i_ret   = VLC_FALSE;
1018
1019     int         i_diff;
1020
1021     //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);
1022     if( p[1]&0x80 )
1023     {
1024         msg_Dbg( p_demux, "transport_error_indicator set (pid=0x%x)", pid->i_pid );
1025     }
1026
1027     if( p_demux->p_sys->csa )
1028     {
1029         csa_Decrypt( p_demux->p_sys->csa, p_bk->p_buffer );
1030     }
1031
1032     if( !b_adaptation )
1033     {
1034         i_skip = 4;
1035     }
1036     else
1037     {
1038         /* p[4] is adaptation length */
1039         i_skip = 5 + p[4];
1040         if( p[4] > 0 )
1041         {
1042             if( p[5]&0x80 )
1043             {
1044                 msg_Warn( p_demux, "discontinuity_indicator (pid=0x%x) "
1045                           "ignored", pid->i_pid );
1046             }
1047         }
1048     }
1049     /* test continuity counter */
1050     /* continuous when (one of this):
1051         * diff == 1
1052         * diff == 0 and payload == 0
1053         * diff == 0 and duplicate packet (playload != 0) <- do we should test the content ?
1054      */
1055
1056     i_diff = ( i_cc - pid->i_cc )&0x0f;
1057     if( b_payload && i_diff == 1 )
1058     {
1059         pid->i_cc++;
1060     }
1061     else
1062     {
1063         if( pid->i_cc == 0xff )
1064         {
1065             msg_Warn( p_demux, "first packet for pid=0x%x cc=0x%x",
1066                       pid->i_pid, i_cc );
1067             pid->i_cc = i_cc;
1068         }
1069         else if( i_diff != 0 )
1070         {
1071             /* FIXME what to do when discontinuity_indicator is set ? */
1072             msg_Warn( p_demux, "discontinuity received 0x%x instead of 0x%x",
1073                       i_cc, ( pid->i_cc + 1 )&0x0f );
1074
1075             pid->i_cc = i_cc;
1076
1077             if( pid->es->p_pes )
1078             {
1079                 pid->es->p_pes->i_flags |= BLOCK_FLAG_DISCONTINUITY;
1080             }
1081         }
1082     }
1083
1084     PCRHandle( p_demux, pid, p_bk );
1085
1086     if( i_skip >= 188 || pid->es->id == NULL || p_demux->p_sys->b_udp_out )
1087     {
1088         block_Release( p_bk );
1089     }
1090     else
1091     {
1092         const vlc_bool_t b_unit_start= p[1]&0x40;
1093
1094         /* we have to gather it */
1095         p_bk->p_buffer += i_skip;
1096         p_bk->i_buffer -= i_skip;
1097
1098         if( b_unit_start )
1099         {
1100             if( pid->es->p_pes )
1101             {
1102                 ParsePES( p_demux, pid );
1103                 i_ret = VLC_TRUE;
1104             }
1105
1106             pid->es->p_pes = p_bk;
1107         }
1108         else
1109         {
1110             if( pid->es->p_pes == NULL )
1111             {
1112                 /* msg_Dbg( p_demux, "broken packet" ); */
1113                 block_Release( p_bk );
1114             }
1115             else
1116             {
1117                 /* TODO check if when have gathered enough packets to form a
1118                  * PES (ie read PES size)*/
1119                 block_ChainAppend( &pid->es->p_pes, p_bk );
1120             }
1121         }
1122     }
1123
1124     return i_ret;
1125 }
1126
1127 static int PIDFillFormat( ts_pid_t *pid, int i_stream_type )
1128 {
1129     es_format_t *fmt = &pid->es->fmt;
1130
1131     switch( i_stream_type )
1132     {
1133         case 0x01:  /* MPEG-1 video */
1134         case 0x02:  /* MPEG-2 video */
1135         case 0x80:  /* MPEG-2 MOTO video */
1136             es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', 'g', 'v' ) );
1137             break;
1138         case 0x03:  /* MPEG-1 audio */
1139         case 0x04:  /* MPEG-2 audio */
1140             es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
1141             break;
1142         case 0x11:  /* MPEG4 (audio) */
1143         case 0x0f:  /* ISO/IEC 13818-7 Audio with ADTS transport syntax */
1144             es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', '4', 'a' ) );
1145             break;
1146         case 0x10:  /* MPEG4 (video) */
1147             es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', '4', 'v' ) );
1148             break;
1149         case 0x1B:  /* H264 <- check transport syntax/needed descriptor */
1150             es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'h', '2', '6', '4' ) );
1151             break;
1152
1153         case 0x81:  /* A52 (audio) */
1154             es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', ' ' ) );
1155             break;
1156         case 0x82:  /* DVD_SPU (sub) */
1157             es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', ' ' ) );
1158             break;
1159         case 0x83:  /* LPCM (audio) */
1160             es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'l', 'p', 'c', 'm' ) );
1161             break;
1162         case 0x84:  /* SDDS (audio) */
1163             es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 's' ) );
1164             break;
1165         case 0x85:  /* DTS (audio) */
1166             es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'd', 't', 's', ' ' ) );
1167             break;
1168
1169         case 0x91:  /* A52 vls (audio) */
1170             es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', 'b' ) );
1171             break;
1172         case 0x92:  /* DVD_SPU vls (sub) */
1173             es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', 'b' ) );
1174             break;
1175         case 0x93:  /* LPCM vls (audio) */
1176             es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'l', 'p', 'c', 'b' ) );
1177             break;
1178         case 0x94:  /* SDDS (audio) */
1179             es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 'b' ) );
1180             break;
1181
1182         case 0x06:  /* PES_PRIVATE  (fixed later) */
1183         case 0xa0:  /* MSCODEC vlc (video) (fixed later) */
1184         default:
1185             es_format_Init( fmt, UNKNOWN_ES, 0 );
1186             break;
1187     }
1188
1189     /* PES packets usually contain truncated frames */
1190     fmt->b_packetized = VLC_FALSE;
1191
1192     return fmt->i_cat == UNKNOWN_ES ? VLC_EGENERIC : VLC_SUCCESS ;
1193 }
1194
1195 /*****************************************************************************
1196  * MP4 specific functions (IOD parser)
1197  *****************************************************************************/
1198 static int  IODDescriptorLength( int *pi_data, uint8_t **pp_data )
1199 {
1200     unsigned int i_b;
1201     unsigned int i_len = 0;
1202     do
1203     {
1204         i_b = **pp_data;
1205         (*pp_data)++;
1206         (*pi_data)--;
1207         i_len = ( i_len << 7 ) + ( i_b&0x7f );
1208
1209     } while( i_b&0x80 );
1210
1211     return( i_len );
1212 }
1213 static int IODGetByte( int *pi_data, uint8_t **pp_data )
1214 {
1215     if( *pi_data > 0 )
1216     {
1217         const int i_b = **pp_data;
1218         (*pp_data)++;
1219         (*pi_data)--;
1220         return( i_b );
1221     }
1222     return( 0 );
1223 }
1224 static int IODGetWord( int *pi_data, uint8_t **pp_data )
1225 {
1226     const int i1 = IODGetByte( pi_data, pp_data );
1227     const int i2 = IODGetByte( pi_data, pp_data );
1228     return( ( i1 << 8 ) | i2 );
1229 }
1230 static int IODGet3Bytes( int *pi_data, uint8_t **pp_data )
1231 {
1232     const int i1 = IODGetByte( pi_data, pp_data );
1233     const int i2 = IODGetByte( pi_data, pp_data );
1234     const int i3 = IODGetByte( pi_data, pp_data );
1235
1236     return( ( i1 << 16 ) | ( i2 << 8) | i3 );
1237 }
1238
1239 static uint32_t IODGetDWord( int *pi_data, uint8_t **pp_data )
1240 {
1241     const uint32_t i1 = IODGetWord( pi_data, pp_data );
1242     const uint32_t i2 = IODGetWord( pi_data, pp_data );
1243     return( ( i1 << 16 ) | i2 );
1244 }
1245
1246 static char* IODGetURL( int *pi_data, uint8_t **pp_data )
1247 {
1248     char *url;
1249     int i_url_len, i;
1250
1251     i_url_len = IODGetByte( pi_data, pp_data );
1252     url = malloc( i_url_len + 1 );
1253     for( i = 0; i < i_url_len; i++ )
1254     {
1255         url[i] = IODGetByte( pi_data, pp_data );
1256     }
1257     url[i_url_len] = '\0';
1258     return( url );
1259 }
1260
1261 static iod_descriptor_t *IODNew( int i_data, uint8_t *p_data )
1262 {
1263     iod_descriptor_t *p_iod;
1264     int i;
1265     int i_es_index;
1266     uint8_t     i_flags;
1267     vlc_bool_t  b_url;
1268     int         i_iod_length;
1269
1270     p_iod = malloc( sizeof( iod_descriptor_t ) );
1271     memset( p_iod, 0, sizeof( iod_descriptor_t ) );
1272
1273     fprintf( stderr, "\n************ IOD ************" );
1274     for( i = 0; i < 255; i++ )
1275     {
1276         p_iod->es_descr[i].b_ok = 0;
1277     }
1278     i_es_index = 0;
1279
1280     if( i_data < 3 )
1281     {
1282         return p_iod;
1283     }
1284
1285     p_iod->i_iod_label = IODGetByte( &i_data, &p_data );
1286     fprintf( stderr, "\n* iod_label:%d", p_iod->i_iod_label );
1287     fprintf( stderr, "\n* ===========" );
1288     fprintf( stderr, "\n* tag:0x%x", p_data[0] );
1289
1290     if( IODGetByte( &i_data, &p_data ) != 0x02 )
1291     {
1292         fprintf( stderr, "\n ERR: tag != 0x02" );
1293         return p_iod;
1294     }
1295
1296     i_iod_length = IODDescriptorLength( &i_data, &p_data );
1297     fprintf( stderr, "\n* length:%d", i_iod_length );
1298     if( i_iod_length > i_data )
1299     {
1300         i_iod_length = i_data;
1301     }
1302
1303     p_iod->i_od_id = ( IODGetByte( &i_data, &p_data ) << 2 );
1304     i_flags = IODGetByte( &i_data, &p_data );
1305     p_iod->i_od_id |= i_flags >> 6;
1306     b_url = ( i_flags >> 5  )&0x01;
1307
1308     fprintf( stderr, "\n* od_id:%d", p_iod->i_od_id );
1309     fprintf( stderr, "\n* url flag:%d", b_url );
1310     fprintf( stderr, "\n* includeInlineProfileLevel flag:%d", ( i_flags >> 4 )&0x01 );
1311
1312     if( b_url )
1313     {
1314         p_iod->psz_url = IODGetURL( &i_data, &p_data );
1315         fprintf( stderr, "\n* url string:%s", p_iod->psz_url );
1316         fprintf( stderr, "\n*****************************\n" );
1317         return p_iod;
1318     }
1319     else
1320     {
1321         p_iod->psz_url = NULL;
1322     }
1323
1324     p_iod->i_ODProfileLevelIndication = IODGetByte( &i_data, &p_data );
1325     p_iod->i_sceneProfileLevelIndication = IODGetByte( &i_data, &p_data );
1326     p_iod->i_audioProfileLevelIndication = IODGetByte( &i_data, &p_data );
1327     p_iod->i_visualProfileLevelIndication = IODGetByte( &i_data, &p_data );
1328     p_iod->i_graphicsProfileLevelIndication = IODGetByte( &i_data, &p_data );
1329
1330     fprintf( stderr, "\n* ODProfileLevelIndication:%d", p_iod->i_ODProfileLevelIndication );
1331     fprintf( stderr, "\n* sceneProfileLevelIndication:%d", p_iod->i_sceneProfileLevelIndication );
1332     fprintf( stderr, "\n* audioProfileLevelIndication:%d", p_iod->i_audioProfileLevelIndication );
1333     fprintf( stderr, "\n* visualProfileLevelIndication:%d", p_iod->i_visualProfileLevelIndication );
1334     fprintf( stderr, "\n* graphicsProfileLevelIndication:%d", p_iod->i_graphicsProfileLevelIndication );
1335
1336
1337     while( i_data > 0 && i_es_index < 255)
1338     {
1339         int i_tag, i_length;
1340         int     i_data_sav;
1341         uint8_t *p_data_sav;
1342
1343         i_tag = IODGetByte( &i_data, &p_data );
1344         i_length = IODDescriptorLength( &i_data, &p_data );
1345
1346         i_data_sav = i_data;
1347         p_data_sav = p_data;
1348
1349         i_data = i_length;
1350
1351         switch( i_tag )
1352         {
1353             case 0x03:
1354                 {
1355 #define es_descr    p_iod->es_descr[i_es_index]
1356                     int i_decoderConfigDescr_length;
1357                     fprintf( stderr, "\n* - ES_Descriptor length:%d", i_length );
1358                     es_descr.b_ok = 1;
1359
1360                     es_descr.i_es_id = IODGetWord( &i_data, &p_data );
1361                     i_flags = IODGetByte( &i_data, &p_data );
1362                     es_descr.b_streamDependenceFlag = ( i_flags >> 7 )&0x01;
1363                     b_url = ( i_flags >> 6 )&0x01;
1364                     es_descr.b_OCRStreamFlag = ( i_flags >> 5 )&0x01;
1365                     es_descr.i_streamPriority = i_flags & 0x1f;
1366                     fprintf( stderr, "\n*   * streamDependenceFlag:%d", es_descr.b_streamDependenceFlag );
1367                     fprintf( stderr, "\n*   * OCRStreamFlag:%d", es_descr.b_OCRStreamFlag );
1368                     fprintf( stderr, "\n*   * streamPriority:%d", es_descr.i_streamPriority );
1369
1370                     if( es_descr.b_streamDependenceFlag )
1371                     {
1372                         es_descr.i_dependOn_es_id = IODGetWord( &i_data, &p_data );
1373                         fprintf( stderr, "\n*   * dependOn_es_id:%d", es_descr.i_dependOn_es_id );
1374                     }
1375
1376                     if( b_url )
1377                     {
1378                         es_descr.psz_url = IODGetURL( &i_data, &p_data );
1379                         fprintf( stderr, "\n* url string:%s", es_descr.psz_url );
1380                     }
1381                     else
1382                     {
1383                         es_descr.psz_url = NULL;
1384                     }
1385
1386                     if( es_descr.b_OCRStreamFlag )
1387                     {
1388                         es_descr.i_OCR_es_id = IODGetWord( &i_data, &p_data );
1389                         fprintf( stderr, "\n*   * OCR_es_id:%d", es_descr.i_OCR_es_id );
1390                     }
1391
1392                     if( IODGetByte( &i_data, &p_data ) != 0x04 )
1393                     {
1394                         fprintf( stderr, "\n* ERR missing DecoderConfigDescr" );
1395                         es_descr.b_ok = 0;
1396                         break;
1397                     }
1398                     i_decoderConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
1399
1400                     fprintf( stderr, "\n*   - DecoderConfigDesc length:%d", i_decoderConfigDescr_length );
1401 #define dec_descr   es_descr.dec_descr
1402                     dec_descr.i_objectTypeIndication = IODGetByte( &i_data, &p_data );
1403                     i_flags = IODGetByte( &i_data, &p_data );
1404                     dec_descr.i_streamType = i_flags >> 2;
1405                     dec_descr.b_upStream = ( i_flags >> 1 )&0x01;
1406                     dec_descr.i_bufferSizeDB = IODGet3Bytes( &i_data, &p_data );
1407                     dec_descr.i_maxBitrate = IODGetDWord( &i_data, &p_data );
1408                     dec_descr.i_avgBitrate = IODGetDWord( &i_data, &p_data );
1409                     fprintf( stderr, "\n*     * objectTypeIndication:0x%x", dec_descr.i_objectTypeIndication  );
1410                     fprintf( stderr, "\n*     * streamType:0x%x", dec_descr.i_streamType );
1411                     fprintf( stderr, "\n*     * upStream:%d", dec_descr.b_upStream );
1412                     fprintf( stderr, "\n*     * bufferSizeDB:%d", dec_descr.i_bufferSizeDB );
1413                     fprintf( stderr, "\n*     * maxBitrate:%d", dec_descr.i_maxBitrate );
1414                     fprintf( stderr, "\n*     * avgBitrate:%d", dec_descr.i_avgBitrate );
1415                     if( i_decoderConfigDescr_length > 13 && IODGetByte( &i_data, &p_data ) == 0x05 )
1416                     {
1417                         int i;
1418                         dec_descr.i_decoder_specific_info_len =
1419                             IODDescriptorLength( &i_data, &p_data );
1420                         if( dec_descr.i_decoder_specific_info_len > 0 )
1421                         {
1422                             dec_descr.p_decoder_specific_info =
1423                                 malloc( dec_descr.i_decoder_specific_info_len );
1424                         }
1425                         for( i = 0; i < dec_descr.i_decoder_specific_info_len; i++ )
1426                         {
1427                             dec_descr.p_decoder_specific_info[i] = IODGetByte( &i_data, &p_data );
1428                         }
1429                     }
1430                     else
1431                     {
1432                         dec_descr.i_decoder_specific_info_len = 0;
1433                         dec_descr.p_decoder_specific_info = NULL;
1434                     }
1435                 }
1436 #undef  dec_descr
1437 #define sl_descr    es_descr.sl_descr
1438                 {
1439                     int i_SLConfigDescr_length;
1440                     int i_predefined;
1441
1442                     if( IODGetByte( &i_data, &p_data ) != 0x06 )
1443                     {
1444                         fprintf( stderr, "\n* ERR missing SLConfigDescr" );
1445                         es_descr.b_ok = 0;
1446                         break;
1447                     }
1448                     i_SLConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
1449
1450                     fprintf( stderr, "\n*   - SLConfigDescr length:%d", i_SLConfigDescr_length );
1451                     i_predefined = IODGetByte( &i_data, &p_data );
1452                     fprintf( stderr, "\n*     * i_predefined:0x%x", i_predefined  );
1453                     switch( i_predefined )
1454                     {
1455                         case 0x01:
1456                             {
1457                                 sl_descr.b_useAccessUnitStartFlag   = 0;
1458                                 sl_descr.b_useAccessUnitEndFlag     = 0;
1459                                 sl_descr.b_useRandomAccessPointFlag = 0;
1460                                 //sl_descr.b_useRandomAccessUnitsOnlyFlag = 0;
1461                                 sl_descr.b_usePaddingFlag           = 0;
1462                                 sl_descr.b_useTimeStampsFlags       = 0;
1463                                 sl_descr.b_useIdleFlag              = 0;
1464                                 sl_descr.b_durationFlag     = 0;    // FIXME FIXME
1465                                 sl_descr.i_timeStampResolution      = 1000;
1466                                 sl_descr.i_OCRResolution    = 0;    // FIXME FIXME
1467                                 sl_descr.i_timeStampLength          = 32;
1468                                 sl_descr.i_OCRLength        = 0;    // FIXME FIXME
1469                                 sl_descr.i_AU_Length                = 0;
1470                                 sl_descr.i_instantBitrateLength= 0; // FIXME FIXME
1471                                 sl_descr.i_degradationPriorityLength= 0;
1472                                 sl_descr.i_AU_seqNumLength          = 0;
1473                                 sl_descr.i_packetSeqNumLength       = 0;
1474                                 if( sl_descr.b_durationFlag )
1475                                 {
1476                                     sl_descr.i_timeScale            = 0;    // FIXME FIXME
1477                                     sl_descr.i_accessUnitDuration   = 0;    // FIXME FIXME
1478                                     sl_descr.i_compositionUnitDuration= 0;    // FIXME FIXME
1479                                 }
1480                                 if( !sl_descr.b_useTimeStampsFlags )
1481                                 {
1482                                     sl_descr.i_startDecodingTimeStamp   = 0;    // FIXME FIXME
1483                                     sl_descr.i_startCompositionTimeStamp= 0;    // FIXME FIXME
1484                                 }
1485                             }
1486                             break;
1487                         default:
1488                             fprintf( stderr, "\n* ERR unsupported SLConfigDescr predefined" );
1489                             es_descr.b_ok = 0;
1490                             break;
1491                     }
1492                 }
1493                 break;
1494 #undef  sl_descr
1495 #undef  es_descr
1496             default:
1497                 fprintf( stderr, "\n* - OD tag:0x%x length:%d (Unsupported)", i_tag, i_length );
1498                 break;
1499         }
1500
1501         p_data = p_data_sav + i_length;
1502         i_data = i_data_sav - i_length;
1503         i_es_index++;
1504     }
1505
1506
1507     fprintf( stderr, "\n*****************************\n" );
1508     return p_iod;
1509 }
1510
1511 static void IODFree( iod_descriptor_t *p_iod )
1512 {
1513     int i;
1514
1515     if( p_iod->psz_url )
1516     {
1517         free( p_iod->psz_url );
1518         p_iod->psz_url = NULL;
1519         free( p_iod );
1520         return;
1521     }
1522
1523     for( i = 0; i < 255; i++ )
1524     {
1525 #define es_descr p_iod->es_descr[i]
1526         if( es_descr.b_ok )
1527         {
1528             if( es_descr.psz_url )
1529             {
1530                 free( es_descr.psz_url );
1531                 es_descr.psz_url = NULL;
1532             }
1533             else
1534             {
1535                 if( es_descr.dec_descr.p_decoder_specific_info != NULL )
1536                 {
1537                     free( es_descr.dec_descr.p_decoder_specific_info );
1538                     es_descr.dec_descr.p_decoder_specific_info = NULL;
1539                     es_descr.dec_descr.i_decoder_specific_info_len = 0;
1540                 }
1541             }
1542         }
1543         es_descr.b_ok = 0;
1544 #undef  es_descr
1545     }
1546     free( p_iod );
1547 }
1548
1549 /****************************************************************************
1550  ****************************************************************************
1551  ** libdvbpsi callbacks
1552  ****************************************************************************
1553  ****************************************************************************/
1554 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt )
1555 {
1556     demux_sys_t          *p_sys = p_demux->p_sys;
1557     dvbpsi_descriptor_t  *p_dr;
1558     dvbpsi_pmt_es_t      *p_es;
1559
1560     ts_pid_t             *pmt = NULL;
1561     int                  i;
1562
1563     msg_Dbg( p_demux, "PMTCallBack called" );
1564
1565     /* First find this PMT declared in PAT */
1566     for( i = 0; i < p_sys->i_pmt; i++ )
1567     {
1568         if( p_sys->pmt[i]->psi->i_number == p_pmt->i_program_number )
1569         {
1570             pmt = p_sys->pmt[i];
1571         }
1572     }
1573     if( pmt == NULL )
1574     {
1575         msg_Warn( p_demux, "unreferenced program (broken stream)" );
1576         dvbpsi_DeletePMT(p_pmt);
1577         return;
1578     }
1579
1580     if( pmt->psi->i_version != -1 &&
1581         ( !p_pmt->b_current_next || pmt->psi->i_version == p_pmt->i_version ) )
1582     {
1583         dvbpsi_DeletePMT( p_pmt );
1584         return;
1585     }
1586
1587     /* Clean this program (remove all es) */
1588     for( i = 0; i < 8192; i++ )
1589     {
1590         ts_pid_t *pid = &p_sys->pid[i];
1591
1592         if( pid->b_valid && pid->p_owner == pmt->psi && pid->psi == NULL )
1593         {
1594             PIDClean( p_demux->out, pid );
1595         }
1596     }
1597     if( pmt->psi->iod )
1598     {
1599         IODFree( pmt->psi->iod );
1600         pmt->psi->iod = NULL;
1601     }
1602
1603     msg_Dbg( p_demux, "new PMT program number=%d version=%d pid_pcr=0x%x",
1604              p_pmt->i_program_number, p_pmt->i_version, p_pmt->i_pcr_pid );
1605     pmt->psi->i_pid_pcr = p_pmt->i_pcr_pid;
1606     pmt->psi->i_version = p_pmt->i_version;
1607
1608     /* Parse descriptor */
1609     for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
1610     {
1611         if( p_dr->i_tag == 0x1d )
1612         {
1613             /* We have found an IOD descriptor */
1614             msg_Warn( p_demux, " * descriptor : IOD (0x1d)" );
1615
1616             pmt->psi->iod = IODNew( p_dr->i_length, p_dr->p_data );
1617         }
1618         else
1619         {
1620             msg_Dbg( p_demux, " * descriptor : unknown (0x%x)", p_dr->i_tag );
1621         }
1622     }
1623
1624     for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
1625     {
1626         ts_pid_t *pid = &p_sys->pid[p_es->i_pid];
1627
1628         if( pid->b_valid )
1629         {
1630             msg_Warn( p_demux, "pmt error: pid=0x%x already defined",
1631                       p_es->i_pid );
1632             continue;
1633         }
1634
1635         PIDInit( pid, VLC_FALSE, pmt->psi );
1636         PIDFillFormat( pid, p_es->i_type );
1637
1638         if( p_es->i_type == 0x10 || p_es->i_type == 0x11 )
1639         {
1640             /* MPEG-4 stream: search SL_DESCRIPTOR */
1641             dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
1642
1643             while( p_dr && ( p_dr->i_tag != 0x1f ) ) p_dr = p_dr->p_next;
1644
1645             if( p_dr && p_dr->i_length == 2 )
1646             {
1647                 int i;
1648                 int i_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
1649
1650                 msg_Warn( p_demux, "found SL_descriptor es_id=%d", i_es_id );
1651
1652                 pid->es->p_mpeg4desc = NULL;
1653
1654                 for( i = 0; i < 255; i++ )
1655                 {
1656                     iod_descriptor_t *iod = pmt->psi->iod;
1657
1658                     if( iod->es_descr[i].b_ok &&
1659                         iod->es_descr[i].i_es_id == i_es_id )
1660                     {
1661                         pid->es->p_mpeg4desc = &iod->es_descr[i];
1662                         break;
1663                     }
1664                 }
1665             }
1666
1667             if( pid->es->p_mpeg4desc != NULL )
1668             {
1669                 decoder_config_descriptor_t *dcd =
1670                     &pid->es->p_mpeg4desc->dec_descr;
1671
1672                 if( dcd->i_streamType == 0x04 )    /* VisualStream */
1673                 {
1674                     pid->es->fmt.i_cat = VIDEO_ES;
1675                     switch( dcd->i_objectTypeIndication )
1676                     {
1677                     case 0x20: /* mpeg4 */
1678                         pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','v');
1679                         break;
1680                     case 0x60:
1681                     case 0x61:
1682                     case 0x62:
1683                     case 0x63:
1684                     case 0x64:
1685                     case 0x65: /* mpeg2 */
1686                         pid->es->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1687                         break;
1688                     case 0x6a: /* mpeg1 */
1689                         pid->es->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1690                         break;
1691                     case 0x6c: /* mpeg1 */
1692                         pid->es->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
1693                         break;
1694                     default:
1695                         pid->es->fmt.i_cat = UNKNOWN_ES;
1696                         break;
1697                     }
1698                 }
1699                 else if( dcd->i_streamType == 0x05 )    /* AudioStream */
1700                 {
1701                     pid->es->fmt.i_cat = AUDIO_ES;
1702                     switch( dcd->i_objectTypeIndication )
1703                     {
1704                     case 0x40: /* mpeg4 */
1705                         pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','a');
1706                         break;
1707                     case 0x66:
1708                     case 0x67:
1709                     case 0x68: /* mpeg2 aac */
1710                         pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','a');
1711                         break;
1712                     case 0x69: /* mpeg2 */
1713                         pid->es->fmt.i_codec = VLC_FOURCC('m','p','g','a');
1714                         break;
1715                     case 0x6b: /* mpeg1 */
1716                         pid->es->fmt.i_codec = VLC_FOURCC('m','p','g','a');
1717                         break;
1718                     default:
1719                         pid->es->fmt.i_cat = UNKNOWN_ES;
1720                         break;
1721                     }
1722                 }
1723                 else
1724                 {
1725                     pid->es->fmt.i_cat = UNKNOWN_ES;
1726                 }
1727
1728                 if( pid->es->fmt.i_cat != UNKNOWN_ES )
1729                 {
1730                     pid->es->fmt.i_extra = dcd->i_decoder_specific_info_len;
1731                     if( pid->es->fmt.i_extra > 0 )
1732                     {
1733                         pid->es->fmt.p_extra = malloc( pid->es->fmt.i_extra );
1734                         memcpy( pid->es->fmt.p_extra,
1735                                 dcd->p_decoder_specific_info,
1736                                 pid->es->fmt.i_extra );
1737                     }
1738                 }
1739             }
1740         }
1741         else if( p_es->i_type == 0x06 )
1742         {
1743             dvbpsi_descriptor_t *p_dr;
1744
1745             for( p_dr = p_es->p_first_descriptor; p_dr != NULL;
1746                  p_dr = p_dr->p_next )
1747             {
1748                 msg_Dbg( p_demux, "  * es pid=0x%x type=0x%x dr->i_tag=0x%x",
1749                          p_es->i_pid, p_es->i_type, p_dr->i_tag );
1750
1751                 if( p_dr->i_tag == 0x6a )
1752                 {
1753                     pid->es->fmt.i_cat = AUDIO_ES;
1754                     pid->es->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
1755                 }
1756 #ifdef _DVBPSI_DR_59_H_
1757                 else if( p_dr->i_tag == 0x59 )
1758                 {
1759                     uint16_t n;
1760                     dvbpsi_subtitling_dr_t *sub;
1761
1762                     /* DVB subtitles */
1763                     pid->es->fmt.i_cat = SPU_ES;
1764                     pid->es->fmt.i_codec = VLC_FOURCC( 'd', 'v', 'b', 's' );
1765                     pid->es->fmt.i_group = p_pmt->i_program_number;
1766
1767                     sub = dvbpsi_DecodeSubtitlingDr( p_dr );
1768                     if( !sub ) continue;
1769
1770                     /* Each subtitle ES contains n languages,
1771                      * We are going to create n ES for the n tracks */
1772                     if( sub->i_subtitles_number > 0 )
1773                     {
1774                         pid->es->fmt.psz_language = malloc( 4 );
1775                         memcpy( pid->es->fmt.psz_language,
1776                                 sub->p_subtitle[0].i_iso6392_language_code, 3);
1777                         pid->es->fmt.psz_language[3] = 0;
1778
1779                         pid->es->fmt.subs.dvb.i_id =
1780                             sub->p_subtitle[0].i_composition_page_id;
1781                     }
1782                     else pid->es->fmt.i_cat = UNKNOWN_ES;
1783
1784                     for( n = 1; n < sub->i_subtitles_number; n++ )
1785                     {
1786                         ts_es_t *p_es = malloc( sizeof( ts_es_t ) );
1787                         p_es->fmt = pid->es->fmt;
1788                         p_es->id = NULL;
1789                         p_es->p_pes = NULL;
1790                         p_es->p_mpeg4desc = NULL;
1791
1792                         p_es->fmt.psz_language = malloc( 4 );
1793                         memcpy( p_es->fmt.psz_language,
1794                                 sub->p_subtitle[n].i_iso6392_language_code, 3);
1795                         p_es->fmt.psz_language[3] = 0;
1796
1797                         p_es->fmt.subs.dvb.i_id =
1798                             sub->p_subtitle[n].i_composition_page_id;
1799
1800                         TAB_APPEND( pid->i_extra_es, pid->extra_es, p_es );
1801                     }
1802                 }
1803 #endif /* _DVBPSI_DR_59_H_ */
1804             }
1805         }
1806         else if( p_es->i_type == 0xa0 )
1807         {
1808             /* MSCODEC sent by vlc */
1809             dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
1810
1811             while( p_dr && ( p_dr->i_tag != 0xa0 ) ) p_dr = p_dr->p_next;
1812
1813             if( p_dr && p_dr->i_length >= 8 )
1814             {
1815                 pid->es->fmt.i_cat = VIDEO_ES;
1816                 pid->es->fmt.i_codec =
1817                     VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
1818                                 p_dr->p_data[2], p_dr->p_data[3] );
1819                 pid->es->fmt.video.i_width =
1820                     ( p_dr->p_data[4] << 8 ) | p_dr->p_data[5];
1821                 pid->es->fmt.video.i_height =
1822                     ( p_dr->p_data[6] << 8 ) | p_dr->p_data[7];
1823                 pid->es->fmt.i_extra = 
1824                     (p_dr->p_data[8] << 8) | p_dr->p_data[9];
1825
1826                 if( pid->es->fmt.i_extra > 0 )
1827                 {
1828                     pid->es->fmt.p_extra = malloc( pid->es->fmt.i_extra );
1829                     memcpy( pid->es->fmt.p_extra, &p_dr->p_data[10],
1830                             pid->es->fmt.i_extra );
1831                 }
1832             }
1833             else
1834             {
1835                 msg_Warn( p_demux, "private MSCODEC (vlc) without bih private "
1836                           "descriptor" );
1837             }
1838             /* For such stream we will gather them ourself and don't launch a
1839              * packetizer.
1840              * Yes it's ugly but it's the only way to have DIV3 working */
1841             pid->es->fmt.b_packetized = VLC_TRUE;
1842         }
1843
1844         if( pid->es->fmt.i_cat == AUDIO_ES ||
1845             ( pid->es->fmt.i_cat == SPU_ES &&
1846               pid->es->fmt.i_codec != VLC_FOURCC('d','v','b','s') ) )
1847         {
1848             /* get language descriptor */
1849             dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
1850             while( p_dr && ( p_dr->i_tag != 0x0a ) ) p_dr = p_dr->p_next;
1851
1852             if( p_dr )
1853             {
1854                 dvbpsi_iso639_dr_t *p_decoded = dvbpsi_DecodeISO639Dr( p_dr );
1855
1856                 if( p_decoded )
1857                 {
1858                     pid->es->fmt.psz_language = malloc( 4 );
1859                     memcpy( pid->es->fmt.psz_language,
1860                             p_decoded->i_iso_639_code, 3 );
1861                     pid->es->fmt.psz_language[3] = 0;
1862                 }
1863             }
1864         }
1865
1866         pid->es->fmt.i_group = p_pmt->i_program_number;
1867         if( pid->es->fmt.i_cat == UNKNOWN_ES )
1868         {
1869             msg_Dbg( p_demux, "  * es pid=0x%x type=0x%x *unknown*",
1870                      p_es->i_pid, p_es->i_type );
1871         }
1872         else if( !p_sys->b_udp_out )
1873         {
1874             int i;
1875
1876             msg_Dbg( p_demux, "  * es pid=0x%x type=0x%x fcc=%4.4s",
1877                      p_es->i_pid, p_es->i_type, (char*)&pid->es->fmt.i_codec );
1878
1879             if( p_sys->b_es_id_pid )
1880             {
1881                 pid->es->fmt.i_id = p_es->i_pid;
1882             }
1883
1884             pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
1885
1886             for( i = 0; i < pid->i_extra_es; i++ )
1887             {
1888                 pid->extra_es[i]->id =
1889                     es_out_Add( p_demux->out, &pid->extra_es[i]->fmt);
1890             }
1891         }
1892     }
1893     dvbpsi_DeletePMT(p_pmt);
1894 }
1895
1896 static void PATCallBack( demux_t *p_demux, dvbpsi_pat_t *p_pat )
1897 {
1898     demux_sys_t          *p_sys = p_demux->p_sys;
1899     dvbpsi_pat_program_t *p_program;
1900     ts_pid_t             *pat = &p_sys->pid[0];
1901     int                  i, j;
1902
1903     msg_Dbg( p_demux, "PATCallBack called" );
1904
1905     if( pat->psi->i_version != -1 &&
1906         ( !p_pat->b_current_next || p_pat->i_version == pat->psi->i_version ) )
1907     {
1908         dvbpsi_DeletePAT( p_pat );
1909         return;
1910     }
1911
1912     msg_Dbg( p_demux, "new PAT ts_id=0x%x version=%d current_next=%d",
1913              p_pat->i_ts_id, p_pat->i_version, p_pat->b_current_next );
1914
1915     /* Clean old */
1916     if( p_sys->i_pmt > 0 )
1917     {
1918         int      i_pmt_rm = 0;
1919         ts_pid_t **pmt_rm = NULL;
1920
1921         /* Search pmt to be deleted */
1922         for( i = 0; i < p_sys->i_pmt; i++ )
1923         {
1924             ts_pid_t *pmt = p_sys->pmt[i];
1925             vlc_bool_t b_keep = VLC_FALSE;
1926
1927             for( p_program = p_pat->p_first_program; p_program != NULL;
1928                  p_program = p_program->p_next )
1929             {
1930                 if( p_program->i_pid == pmt->i_pid &&
1931                     p_program->i_number == pmt->psi->i_number )
1932                 {
1933                     b_keep = VLC_TRUE;
1934                     break;
1935                 }
1936             }
1937             if( !b_keep )
1938             {
1939                 TAB_APPEND( i_pmt_rm, pmt_rm, pmt );
1940             }
1941         }
1942
1943         /* Delete all ES attached to thoses PMT */
1944         for( i = 2; i < 8192; i++ )
1945         {
1946             ts_pid_t *pid = &p_sys->pid[i];
1947
1948             if( !pid->b_valid || pid->psi ) continue;
1949
1950             for( j = 0; j < i_pmt_rm; j++ )
1951             {
1952                 if( pid->p_owner->i_pid_pcr == pmt_rm[j]->i_pid &&
1953                     pid->es->id )
1954                 {
1955                     /* We only remove es that aren't defined by extra pmt */
1956                     PIDClean( p_demux->out, pid );
1957                     break;
1958                 }
1959             }
1960         }
1961
1962         /* Delete PMT pid */
1963         for( i = 0; i < i_pmt_rm; i++ )
1964         {
1965             PIDClean( p_demux->out, &p_sys->pid[pmt_rm[i]->i_pid] );
1966             TAB_REMOVE( p_sys->i_pmt, p_sys->pmt, pmt_rm[i] );
1967         }
1968         if( pmt_rm )
1969         {
1970             free( pmt_rm );
1971         }
1972     }
1973
1974     /* now create programs */
1975     for( p_program = p_pat->p_first_program; p_program != NULL;
1976          p_program = p_program->p_next )
1977     {
1978         msg_Dbg( p_demux, "  * number=%d pid=0x%x", p_program->i_number,
1979                  p_program->i_pid );
1980         if( p_program->i_number != 0 )
1981         {
1982             ts_pid_t *pmt = &p_sys->pid[p_program->i_pid];
1983
1984             if( !pmt->b_valid )
1985             {
1986                 PIDInit( pmt, VLC_TRUE, pat->psi );
1987                 pmt->psi->handle =
1988                     dvbpsi_AttachPMT( p_program->i_number,
1989                                       (dvbpsi_pmt_callback)PMTCallBack, p_demux );
1990                 pmt->psi->i_number = p_program->i_number;
1991
1992                 TAB_APPEND( p_sys->i_pmt, p_sys->pmt, pmt );
1993             }
1994         }
1995     }
1996     pat->psi->i_version = p_pat->i_version;
1997
1998     dvbpsi_DeletePAT( p_pat );
1999 }