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