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