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