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