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