Make the buildbot happy until it gets newer libdvbpsi
[vlc.git] / modules / demux / ts.c
1 /*****************************************************************************
2  * ts.c: Transport Stream input module for VLC.
3  *****************************************************************************
4  * Copyright (C) 2004-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Jean-Paul Saman <jpsaman #_at_# m2x.nl>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35
36 #include <ctype.h>
37 #include <assert.h>
38
39 #include <vlc_access.h> /* DVB-specific things */
40 #include <vlc_demux.h>
41 #include <vlc_meta.h>
42 #include <vlc_epg.h>
43
44 #include <vlc_iso_lang.h>
45 #include <vlc_network.h>
46 #include <vlc_charset.h>
47
48 #include "../mux/mpeg/csa.h"
49
50 /* Include dvbpsi headers */
51 #ifdef HAVE_DVBPSI_DR_H
52 #   include <dvbpsi/dvbpsi.h>
53 #   include <dvbpsi/demux.h>
54 #   include <dvbpsi/descriptor.h>
55 #   include <dvbpsi/pat.h>
56 #   include <dvbpsi/pmt.h>
57 #   include <dvbpsi/sdt.h>
58 #   include <dvbpsi/dr.h>
59 #   include <dvbpsi/psi.h>
60 #else
61 #   include "dvbpsi.h"
62 #   include "demux.h"
63 #   include "descriptor.h"
64 #   include "tables/pat.h"
65 #   include "tables/pmt.h"
66 #   include "tables/sdt.h"
67 #   include "descriptors/dr.h"
68 #   include "psi.h"
69 #endif
70
71 /* EIT support */
72 #ifdef _DVBPSI_DR_4D_H_
73 #   define TS_USE_DVB_SI 1
74 #   ifdef HAVE_DVBPSI_DR_H
75 #       include <dvbpsi/eit.h>
76 #   else
77 #       include "tables/eit.h"
78 #   endif
79 #endif
80
81 /* TDT support */
82 #ifdef _DVBPSI_TOT_H_
83 #   define TS_USE_TDT 1
84 #   ifdef HAVE_DVBPSI_DR_H
85 #       include <dvbpsi/tot.h>
86 #   else
87 #       include "tables/tot.h"
88 #   endif
89 #endif
90
91 #include <time.h>
92 #undef TS_DEBUG
93
94 /*****************************************************************************
95  * Module descriptor
96  *****************************************************************************/
97 static int  Open  ( vlc_object_t * );
98 static void Close ( vlc_object_t * );
99
100 /* TODO
101  * - Rename "extra pmt" to "user pmt"
102  * - Update extra pmt description
103  *      pmt_pid[:pmt_number][=pid_description[,pid_description]]
104  *      where pid_description could take 3 forms:
105  *          1. pid:pcr (to force the pcr pid)
106  *          2. pid:stream_type
107  *          3. pid:type=fourcc where type=(video|audio|spu)
108  */
109 #define PMT_TEXT N_("Extra PMT")
110 #define PMT_LONGTEXT N_( \
111   "Allows a user to specify an extra pmt (pmt_pid=pid:stream_type[,...])." )
112
113 #define PID_TEXT N_("Set id of ES to PID")
114 #define PID_LONGTEXT N_("Set the internal ID of each elementary stream" \
115                        " handled by VLC to the same value as the PID in" \
116                        " the TS stream, instead of 1, 2, 3, etc. Useful to" \
117                        " do \'#duplicate{..., select=\"es=<pid>\"}\'.")
118
119 #define TSOUT_TEXT N_("Fast udp streaming")
120 #define TSOUT_LONGTEXT N_( \
121   "Sends TS to specific ip:port by udp (you must know what you are doing).")
122
123 #define MTUOUT_TEXT N_("MTU for out mode")
124 #define MTUOUT_LONGTEXT N_("MTU for out mode.")
125
126 #define CSA_TEXT N_("CSA ck")
127 #define CSA_LONGTEXT N_("Control word for the CSA encryption algorithm")
128
129 #define CSA2_TEXT N_("Second CSA Key")
130 #define CSA2_LONGTEXT N_("The even CSA encryption key. This must be a " \
131   "16 char string (8 hexadecimal bytes).")
132
133 #define SILENT_TEXT N_("Silent mode")
134 #define SILENT_LONGTEXT N_("Do not complain on encrypted PES.")
135
136 #define CAPMT_SYSID_TEXT N_("CAPMT System ID")
137 #define CAPMT_SYSID_LONGTEXT N_("Only forward descriptors from this SysID to the CAM.")
138
139 #define CPKT_TEXT N_("Packet size in bytes to decrypt")
140 #define CPKT_LONGTEXT N_("Specify the size of the TS packet to decrypt. " \
141     "The decryption routines subtract the TS-header from the value before " \
142     "decrypting. " )
143
144 #define TSDUMP_TEXT N_("Filename of dump")
145 #define TSDUMP_LONGTEXT N_("Specify a filename where to dump the TS in.")
146
147 #define APPEND_TEXT N_("Append")
148 #define APPEND_LONGTEXT N_( \
149     "If the file exists and this option is selected, the existing file " \
150     "will not be overwritten." )
151
152 #define DUMPSIZE_TEXT N_("Dump buffer size")
153 #define DUMPSIZE_LONGTEXT N_( \
154     "Tweak the buffer size for reading and writing an integer number of packets." \
155     "Specify the size of the buffer here and not the number of packets." )
156
157 vlc_module_begin ()
158     set_description( N_("MPEG Transport Stream demuxer") )
159     set_shortname ( "MPEG-TS" )
160     set_category( CAT_INPUT )
161     set_subcategory( SUBCAT_INPUT_DEMUX )
162
163     add_string( "ts-extra-pmt", NULL, NULL, PMT_TEXT, PMT_LONGTEXT, true )
164     add_bool( "ts-es-id-pid", true, NULL, PID_TEXT, PID_LONGTEXT, true )
165     add_string( "ts-out", NULL, NULL, TSOUT_TEXT, TSOUT_LONGTEXT, true )
166     add_integer( "ts-out-mtu", 1400, NULL, MTUOUT_TEXT,
167                  MTUOUT_LONGTEXT, true )
168     add_string( "ts-csa-ck", NULL, NULL, CSA_TEXT, CSA_LONGTEXT, true )
169     add_string( "ts-csa2-ck", NULL, NULL, CSA_TEXT, CSA_LONGTEXT, true )
170     add_integer( "ts-csa-pkt", 188, NULL, CPKT_TEXT, CPKT_LONGTEXT, true )
171     add_bool( "ts-silent", false, NULL, SILENT_TEXT, SILENT_LONGTEXT, true )
172
173     add_file( "ts-dump-file", NULL, NULL, TSDUMP_TEXT, TSDUMP_LONGTEXT, false )
174     add_bool( "ts-dump-append", false, NULL, APPEND_TEXT, APPEND_LONGTEXT, false )
175     add_integer( "ts-dump-size", 16384, NULL, DUMPSIZE_TEXT,
176                  DUMPSIZE_LONGTEXT, true )
177
178     set_capability( "demux", 10 )
179     set_callbacks( Open, Close )
180     add_shortcut( "ts" )
181 vlc_module_end ()
182
183 /*****************************************************************************
184  * Local prototypes
185  *****************************************************************************/
186 static const char *const ppsz_teletext_type[] = {
187  "",
188  N_("Teletext"),
189  N_("Teletext subtitles"),
190  N_("Teletext: additional information"),
191  N_("Teletext: program schedule"),
192  N_("Teletext subtitles: hearing impaired")
193 };
194
195 typedef struct
196 {
197     uint8_t                 i_objectTypeIndication;
198     uint8_t                 i_streamType;
199     bool                    b_upStream;
200     uint32_t                i_bufferSizeDB;
201     uint32_t                i_maxBitrate;
202     uint32_t                i_avgBitrate;
203
204     int                     i_decoder_specific_info_len;
205     uint8_t                 *p_decoder_specific_info;
206
207 } decoder_config_descriptor_t;
208
209 typedef struct
210 {
211     bool                    b_useAccessUnitStartFlag;
212     bool                    b_useAccessUnitEndFlag;
213     bool                    b_useRandomAccessPointFlag;
214     bool                    b_useRandomAccessUnitsOnlyFlag;
215     bool                    b_usePaddingFlag;
216     bool                    b_useTimeStampsFlags;
217     bool                    b_useIdleFlag;
218     bool                    b_durationFlag;
219     uint32_t                i_timeStampResolution;
220     uint32_t                i_OCRResolution;
221     uint8_t                 i_timeStampLength;
222     uint8_t                 i_OCRLength;
223     uint8_t                 i_AU_Length;
224     uint8_t                 i_instantBitrateLength;
225     uint8_t                 i_degradationPriorityLength;
226     uint8_t                 i_AU_seqNumLength;
227     uint8_t                 i_packetSeqNumLength;
228
229     uint32_t                i_timeScale;
230     uint16_t                i_accessUnitDuration;
231     uint16_t                i_compositionUnitDuration;
232
233     uint64_t                i_startDecodingTimeStamp;
234     uint64_t                i_startCompositionTimeStamp;
235
236 } sl_config_descriptor_t;
237
238 typedef struct
239 {
240     bool                    b_ok;
241     uint16_t                i_es_id;
242
243     bool                    b_streamDependenceFlag;
244     bool                    b_OCRStreamFlag;
245     uint8_t                 i_streamPriority;
246
247     char                    *psz_url;
248
249     uint16_t                i_dependOn_es_id;
250     uint16_t                i_OCR_es_id;
251
252     decoder_config_descriptor_t    dec_descr;
253     sl_config_descriptor_t         sl_descr;
254
255 } es_mpeg4_descriptor_t;
256
257 typedef struct
258 {
259     uint8_t                 i_iod_label, i_iod_label_scope;
260
261     /* IOD */
262     uint16_t                i_od_id;
263     char                    *psz_url;
264
265     uint8_t                 i_ODProfileLevelIndication;
266     uint8_t                 i_sceneProfileLevelIndication;
267     uint8_t                 i_audioProfileLevelIndication;
268     uint8_t                 i_visualProfileLevelIndication;
269     uint8_t                 i_graphicsProfileLevelIndication;
270
271     es_mpeg4_descriptor_t   es_descr[255];
272
273 } iod_descriptor_t;
274
275 typedef struct
276 {
277     dvbpsi_handle   handle;
278
279     int             i_version;
280     int             i_number;
281     int             i_pid_pcr;
282     int             i_pid_pmt;
283     /* IOD stuff (mpeg4) */
284     iod_descriptor_t *iod;
285
286 } ts_prg_psi_t;
287
288 typedef struct
289 {
290     /* for special PAT/SDT case */
291     dvbpsi_handle   handle; /* PAT/SDT/EIT */
292     int             i_pat_version;
293     int             i_sdt_version;
294
295     /* For PMT */
296     int             i_prg;
297     ts_prg_psi_t    **prg;
298
299 } ts_psi_t;
300
301 typedef struct
302 {
303     es_format_t  fmt;
304     es_out_id_t *id;
305     int         i_pes_size;
306     int         i_pes_gathered;
307     block_t     *p_pes;
308     block_t     **pp_last;
309
310     es_mpeg4_descriptor_t *p_mpeg4desc;
311     int         b_gather;
312
313 } ts_es_t;
314
315 typedef struct
316 {
317     int         i_pid;
318
319     bool        b_seen;
320     bool        b_valid;
321     int         i_cc;   /* countinuity counter */
322     bool        b_scrambled;
323
324     /* PSI owner (ie PMT -> PAT, ES -> PMT */
325     ts_psi_t   *p_owner;
326     int         i_owner_number;
327
328     /* */
329     ts_psi_t    *psi;
330     ts_es_t     *es;
331
332     /* Some private streams encapsulate several ES (eg. DVB subtitles)*/
333     ts_es_t     **extra_es;
334     int         i_extra_es;
335
336 } ts_pid_t;
337
338 struct demux_sys_t
339 {
340     vlc_mutex_t     csa_lock;
341
342     /* TS packet size (188, 192, 204) */
343     int         i_packet_size;
344
345     /* how many TS packet we read at once */
346     int         i_ts_read;
347
348     /* All pid */
349     ts_pid_t    pid[8192];
350
351     /* All PMT */
352     bool        b_user_pmt;
353     int         i_pmt;
354     ts_pid_t    **pmt;
355
356     /* */
357     bool        b_es_id_pid;
358     csa_t       *csa;
359     int         i_csa_pkt_size;
360     bool        b_silent;
361
362     bool        b_udp_out;
363     int         fd; /* udp socket */
364     uint8_t     *buffer;
365
366     /* */
367     bool        b_access_control;
368
369     /* */
370     bool        b_dvb_meta;
371     int64_t     i_tdt_delta;
372     int64_t     i_dvb_start;
373     int64_t     i_dvb_length;
374
375     /* */
376     int         i_current_program;
377     vlc_list_t  *p_programs_list;
378
379     /* TS dump */
380     char        *psz_file;  /* file to dump data in */
381     FILE        *p_file;    /* filehandle */
382     uint64_t    i_write;    /* bytes written */
383     bool        b_file_out; /* dump mode enabled */
384
385     /* */
386     bool        b_start_record;
387 };
388
389 static int i_broken_epg;
390
391 static int Demux    ( demux_t *p_demux );
392 static int DemuxFile( demux_t *p_demux );
393 static int Control( demux_t *p_demux, int i_query, va_list args );
394
395 static void PIDInit ( ts_pid_t *pid, bool b_psi, ts_psi_t *p_owner );
396 static void PIDClean( es_out_t *out, ts_pid_t *pid );
397 static int  PIDFillFormat( ts_pid_t *pid, int i_stream_type );
398
399 static void PATCallBack( demux_t *, dvbpsi_pat_t * );
400 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt );
401 #ifdef TS_USE_DVB_SI
402 static void PSINewTableCallBack( demux_t *, dvbpsi_handle,
403                                  uint8_t  i_table_id, uint16_t i_extension );
404 #endif
405 static int ChangeKeyCallback( vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void * );
406
407 static inline int PIDGet( block_t *p )
408 {
409     return ( (p->p_buffer[1]&0x1f)<<8 )|p->p_buffer[2];
410 }
411
412 static bool GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk );
413
414 static void PCRHandle( demux_t *p_demux, ts_pid_t *, block_t * );
415
416 static iod_descriptor_t *IODNew( int , uint8_t * );
417 static void              IODFree( iod_descriptor_t * );
418
419 #define TS_USER_PMT_NUMBER (0)
420 static int UserPmt( demux_t *p_demux, const char * );
421
422 #define TS_PACKET_SIZE_188 188
423 #define TS_PACKET_SIZE_192 192
424 #define TS_PACKET_SIZE_204 204
425 #define TS_PACKET_SIZE_MAX 204
426 #define TS_TOPFIELD_HEADER 1320
427
428 /*****************************************************************************
429  * Open
430  *****************************************************************************/
431 static int Open( vlc_object_t *p_this )
432 {
433     demux_t     *p_demux = (demux_t*)p_this;
434     demux_sys_t *p_sys;
435
436     const uint8_t *p_peek;
437     int          i_sync, i_peek, i;
438     int          i_packet_size;
439
440     ts_pid_t    *pat;
441     const char  *psz_mode;
442     bool         b_append;
443     bool         b_topfield = false;
444
445     if( stream_Peek( p_demux->s, &p_peek, TS_PACKET_SIZE_MAX ) <
446         TS_PACKET_SIZE_MAX ) return VLC_EGENERIC;
447
448     if( memcmp( p_peek, "TFrc", 4 ) == 0 )
449     {
450         b_topfield = true;
451         msg_Dbg( p_demux, "this is a topfield file" );
452     }
453
454     /* Search first sync byte */
455     for( i_sync = 0; i_sync < TS_PACKET_SIZE_MAX; i_sync++ )
456     {
457         if( p_peek[i_sync] == 0x47 ) break;
458     }
459     if( i_sync >= TS_PACKET_SIZE_MAX && !b_topfield )
460     {
461         if( !p_demux->b_force )
462             return VLC_EGENERIC;
463         msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
464     }
465
466     if( b_topfield )
467     {
468         /* Read the entire Topfield header */
469         i_peek = TS_TOPFIELD_HEADER;
470     }
471     else
472     {
473         /* Check next 3 sync bytes */
474         i_peek = TS_PACKET_SIZE_MAX * 3 + i_sync + 1;
475     }
476
477     if( ( stream_Peek( p_demux->s, &p_peek, i_peek ) ) < i_peek )
478     {
479         msg_Err( p_demux, "cannot peek" );
480         return VLC_EGENERIC;
481     }
482     if( p_peek[i_sync + TS_PACKET_SIZE_188] == 0x47 &&
483         p_peek[i_sync + 2 * TS_PACKET_SIZE_188] == 0x47 &&
484         p_peek[i_sync + 3 * TS_PACKET_SIZE_188] == 0x47 )
485     {
486         i_packet_size = TS_PACKET_SIZE_188;
487     }
488     else if( p_peek[i_sync + TS_PACKET_SIZE_192] == 0x47 &&
489              p_peek[i_sync + 2 * TS_PACKET_SIZE_192] == 0x47 &&
490              p_peek[i_sync + 3 * TS_PACKET_SIZE_192] == 0x47 )
491     {
492         i_packet_size = TS_PACKET_SIZE_192;
493     }
494     else if( p_peek[i_sync + TS_PACKET_SIZE_204] == 0x47 &&
495              p_peek[i_sync + 2 * TS_PACKET_SIZE_204] == 0x47 &&
496              p_peek[i_sync + 3 * TS_PACKET_SIZE_204] == 0x47 )
497     {
498         i_packet_size = TS_PACKET_SIZE_204;
499     }
500     else if( p_demux->b_force )
501     {
502         i_packet_size = TS_PACKET_SIZE_188;
503     }
504     else if( b_topfield )
505     {
506         i_packet_size = TS_PACKET_SIZE_188;
507 #if 0
508         /* I used the TF5000PVR 2004 Firmware .doc header documentation,
509          * http://www.i-topfield.com/data/product/firmware/Structure%20of%20Recorded%20File%20in%20TF5000PVR%20(Feb%2021%202004).doc
510          * but after the filename the offsets seem to be incorrect.  - DJ */
511         int i_duration, i_name;
512         char *psz_name = malloc(25);
513         char *psz_event_name;
514         char *psz_event_text = malloc(130);
515         char *psz_ext_text = malloc(1025);
516
517         // 2 bytes version Uimsbf (4,5)
518         // 2 bytes reserved (6,7)
519         // 2 bytes duration in minutes Uimsbf (8,9(
520         i_duration = (int) (p_peek[8] << 8) | p_peek[9];
521         msg_Dbg( p_demux, "Topfield recording length: +/- %d minutes", i_duration);
522         // 2 bytes service number in channel list (10, 11)
523         // 2 bytes service type Bslbf 0=TV 1=Radio Bslb (12, 13)
524         // 4 bytes of reserved + tuner info (14,15,16,17)
525         // 2 bytes of Service ID  Bslbf (18,19)
526         // 2 bytes of PMT PID  Uimsbf (20,21)
527         // 2 bytes of PCR PID  Uimsbf (22,23)
528         // 2 bytes of Video PID  Uimsbf (24,25)
529         // 2 bytes of Audio PID  Uimsbf (26,27)
530         // 24 bytes filename Bslbf
531         memcpy( psz_name, &p_peek[28], 24 );
532         psz_name[24] = '\0';
533         msg_Dbg( p_demux, "recordingname=%s", psz_name );
534         // 1 byte of sat index Uimsbf  (52)
535         // 3 bytes (1 bit of polarity Bslbf +23 bits reserved)
536         // 4 bytes of freq. Uimsbf (56,57,58,59)
537         // 2 bytes of symbol rate Uimsbf (60,61)
538         // 2 bytes of TS stream ID Uimsbf (62,63)
539         // 4 bytes reserved
540         // 2 bytes reserved
541         // 2 bytes duration Uimsbf (70,71)
542         //i_duration = (int) (p_peek[70] << 8) | p_peek[71];
543         //msg_Dbg( p_demux, "Topfield 2nd duration field: +/- %d minutes", i_duration);
544         // 4 bytes EventID Uimsbf (72-75)
545         // 8 bytes of Start and End time info (76-83)
546         // 1 byte reserved (84)
547         // 1 byte event name length Uimsbf (89)
548         i_name = (int)(p_peek[89]&~0x81);
549         msg_Dbg( p_demux, "event name length = %d", i_name);
550         psz_event_name = malloc( i_name+1 );
551         // 1 byte parental rating (90)
552         // 129 bytes of event text
553         memcpy( psz_event_name, &p_peek[91], i_name );
554         psz_event_name[i_name] = '\0';
555         memcpy( psz_event_text, &p_peek[91+i_name], 129-i_name );
556         psz_event_text[129-i_name] = '\0';
557         msg_Dbg( p_demux, "event name=%s", psz_event_name );
558         msg_Dbg( p_demux, "event text=%s", psz_event_text );
559         // 12 bytes reserved (220)
560         // 6 bytes reserved
561         // 2 bytes Event Text Length Uimsbf
562         // 4 bytes EventID Uimsbf
563         // FIXME We just have 613 bytes. not enough for this entire text
564         // 1024 bytes Extended Event Text Bslbf
565         memcpy( psz_ext_text, p_peek+372, 1024 );
566         psz_ext_text[1024] = '\0';
567         msg_Dbg( p_demux, "extended event text=%s", psz_ext_text );
568         // 52 bytes reserved Bslbf
569 #endif
570     }
571     else
572     {
573         msg_Warn( p_demux, "TS module discarded (lost sync)" );
574         return VLC_EGENERIC;
575     }
576
577     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
578     if( !p_sys )
579         return VLC_ENOMEM;
580     memset( p_sys, 0, sizeof( demux_sys_t ) );
581     p_sys->i_packet_size = i_packet_size;
582     vlc_mutex_init( &p_sys->csa_lock );
583
584     /* Fill dump mode fields */
585     p_sys->i_write = 0;
586     p_sys->buffer = NULL;
587     p_sys->p_file = NULL;
588     p_sys->b_file_out = false;
589     p_sys->psz_file = var_CreateGetString( p_demux, "ts-dump-file" );
590     if( *p_sys->psz_file != '\0' )
591     {
592         p_sys->b_file_out = true;
593
594         b_append = var_CreateGetBool( p_demux, "ts-dump-append" );
595         if ( b_append )
596             psz_mode = "ab";
597         else
598             psz_mode = "wb";
599
600         if( !strcmp( p_sys->psz_file, "-" ) )
601         {
602             msg_Info( p_demux, "dumping raw stream to standard output" );
603             p_sys->p_file = stdout;
604         }
605         else if( ( p_sys->p_file = utf8_fopen( p_sys->psz_file, psz_mode ) ) == NULL )
606         {
607             msg_Err( p_demux, "cannot create `%s' for writing", p_sys->psz_file );
608             p_sys->b_file_out = false;
609         }
610
611         if( p_sys->b_file_out )
612         {
613             /* Determine how many packets to read. */
614             int bufsize = var_CreateGetInteger( p_demux, "ts-dump-size" );
615             p_sys->i_ts_read = (int) (bufsize / p_sys->i_packet_size);
616             if( p_sys->i_ts_read <= 0 )
617             {
618                 p_sys->i_ts_read = 1500 / p_sys->i_packet_size;
619             }
620             p_sys->buffer = malloc( p_sys->i_packet_size * p_sys->i_ts_read );
621             msg_Info( p_demux, "%s raw stream to file `%s' reading packets %d",
622                       b_append ? "appending" : "dumping", p_sys->psz_file,
623                       p_sys->i_ts_read );
624         }
625     }
626
627     /* Fill p_demux field */
628     if( p_sys->b_file_out )
629         p_demux->pf_demux = DemuxFile;
630     else
631         p_demux->pf_demux = Demux;
632     p_demux->pf_control = Control;
633
634     /* Init p_sys field */
635     p_sys->b_dvb_meta = true;
636     p_sys->b_access_control = true;
637     p_sys->i_current_program = 0;
638     p_sys->i_tdt_delta = 0;
639     p_sys->i_dvb_start = 0;
640     p_sys->i_dvb_length = 0;
641
642     for( i = 0; i < 8192; i++ )
643     {
644         ts_pid_t *pid = &p_sys->pid[i];
645
646         pid->i_pid      = i;
647         pid->b_seen     = false;
648         pid->b_valid    = false;
649     }
650     /* PID 8191 is padding */
651     p_sys->pid[8191].b_seen = true;
652     p_sys->i_packet_size = i_packet_size;
653     p_sys->b_udp_out = false;
654     p_sys->fd = -1;
655     p_sys->i_ts_read = 50;
656     p_sys->csa = NULL;
657     p_sys->b_start_record = false;
658
659     /* Init PAT handler */
660     pat = &p_sys->pid[0];
661     PIDInit( pat, true, NULL );
662     pat->psi->handle = dvbpsi_AttachPAT( (dvbpsi_pat_callback)PATCallBack,
663                                          p_demux );
664 #ifdef TS_USE_DVB_SI
665     if( p_sys->b_dvb_meta )
666     {
667         ts_pid_t *sdt = &p_sys->pid[0x11];
668         ts_pid_t *eit = &p_sys->pid[0x12];
669
670         PIDInit( sdt, true, NULL );
671         sdt->psi->handle =
672             dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
673                                 p_demux );
674         PIDInit( eit, true, NULL );
675         eit->psi->handle =
676             dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
677                                 p_demux );
678 #ifdef TS_USE_TDT
679         ts_pid_t *tdt = &p_sys->pid[0x14];
680         PIDInit( tdt, true, NULL );
681         tdt->psi->handle =
682             dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
683                                 p_demux );
684 #endif
685         if( p_sys->b_access_control )
686         {
687             if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
688                                 ACCESS_SET_PRIVATE_ID_STATE, 0x11, true ) ||
689 #ifdef TS_USE_TDT
690                 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
691                                 ACCESS_SET_PRIVATE_ID_STATE, 0x14, true ) ||
692 #endif
693                 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
694                                 ACCESS_SET_PRIVATE_ID_STATE, 0x12, true ) )
695                 p_sys->b_access_control = false;
696         }
697     }
698 #endif
699
700     /* Init PMT array */
701     TAB_INIT( p_sys->i_pmt, p_sys->pmt );
702
703     /* Read config */
704     p_sys->b_es_id_pid = var_CreateGetBool( p_demux, "ts-es-id-pid" );
705
706     char* psz_string = var_CreateGetString( p_demux, "ts-out" );
707     if( psz_string && *psz_string && !p_sys->b_file_out )
708     {
709         char *psz = strchr( psz_string, ':' );
710         int   i_port = 0;
711
712         p_sys->b_udp_out = true;
713
714         if( psz )
715         {
716             *psz++ = '\0';
717             i_port = atoi( psz );
718         }
719         if( i_port <= 0 ) i_port  = 1234;
720         msg_Dbg( p_demux, "resend ts to '%s:%d'", psz_string, i_port );
721
722         p_sys->fd = net_ConnectUDP( VLC_OBJECT(p_demux), psz_string, i_port, -1 );
723         if( p_sys->fd < 0 )
724         {
725             msg_Err( p_demux, "failed to open udp socket, send disabled" );
726             p_sys->b_udp_out = false;
727         }
728         else
729         {
730             int i_mtu = var_CreateGetInteger( p_demux, "ts-out-mtu" );
731             p_sys->i_ts_read = i_mtu / p_sys->i_packet_size;
732             if( p_sys->i_ts_read <= 0 )
733             {
734                 p_sys->i_ts_read = 1500 / p_sys->i_packet_size;
735             }
736             p_sys->buffer = malloc( p_sys->i_packet_size * p_sys->i_ts_read );
737         }
738     }
739     free( psz_string );
740
741     /* We handle description of an extra PMT */
742     psz_string = var_CreateGetString( p_demux, "ts-extra-pmt" );
743     p_sys->b_user_pmt = false;
744     if( psz_string && *psz_string )
745         UserPmt( p_demux, psz_string );
746     free( psz_string );
747
748     psz_string = var_CreateGetStringCommand( p_demux, "ts-csa-ck" );
749     if( psz_string && *psz_string )
750     {
751         int i_res;
752         char* psz_csa2;
753
754         p_sys->csa = csa_New();
755
756         psz_csa2 = var_CreateGetStringCommand( p_demux, "ts-csa2-ck" );
757         i_res = csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, true );
758         if( i_res == VLC_SUCCESS && psz_csa2 && *psz_csa2 )
759         {
760             if( csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_csa2, false ) != VLC_SUCCESS )
761             {
762                 csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, false );
763             }
764         }
765         else if ( i_res == VLC_SUCCESS )
766         {
767             csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, false );
768         }
769         else
770         {
771             csa_Delete( p_sys->csa );
772             p_sys->csa = NULL;
773         }
774
775         if( p_sys->csa )
776         {
777             var_AddCallback( p_demux, "ts-csa-ck", ChangeKeyCallback, (void *)1 );
778             var_AddCallback( p_demux, "ts-csa2-ck", ChangeKeyCallback, NULL );
779
780             int i_pkt = var_CreateGetInteger( p_demux, "ts-csa-pkt" );
781             if( i_pkt < 4 || i_pkt > 188 )
782             {
783                 msg_Err( p_demux, "wrong packet size %d specified.", i_pkt );
784                 msg_Warn( p_demux, "using default packet size of 188 bytes" );
785                 p_sys->i_csa_pkt_size = 188;
786             }
787             else
788                 p_sys->i_csa_pkt_size = i_pkt;
789             msg_Dbg( p_demux, "decrypting %d bytes of packet", p_sys->i_csa_pkt_size );
790         }
791         free( psz_csa2 );
792     }
793     free( psz_string );
794
795     p_sys->b_silent = var_CreateGetBool( p_demux, "ts-silent" );
796
797     return VLC_SUCCESS;
798 }
799
800 /*****************************************************************************
801  * Close
802  *****************************************************************************/
803 static void Close( vlc_object_t *p_this )
804 {
805     demux_t     *p_demux = (demux_t*)p_this;
806     demux_sys_t *p_sys = p_demux->p_sys;
807
808     msg_Dbg( p_demux, "pid list:" );
809     for( int i = 0; i < 8192; i++ )
810     {
811         ts_pid_t *pid = &p_sys->pid[i];
812
813         if( pid->b_valid && pid->psi )
814         {
815             switch( pid->i_pid )
816             {
817             case 0: /* PAT */
818                 dvbpsi_DetachPAT( pid->psi->handle );
819                 free( pid->psi );
820                 break;
821             case 1: /* CAT */
822                 free( pid->psi );
823                 break;
824             default:
825                 if( p_sys->b_dvb_meta && ( pid->i_pid == 0x11 || pid->i_pid == 0x12 || pid->i_pid == 0x14 ) )
826                 {
827                     /* SDT or EIT or TDT */
828                     dvbpsi_DetachDemux( pid->psi->handle );
829                     free( pid->psi );
830                 }
831                 else
832                 {
833                     PIDClean( p_demux->out, pid );
834                 }
835                 break;
836             }
837         }
838         else if( pid->b_valid && pid->es )
839         {
840             PIDClean( p_demux->out, pid );
841         }
842
843         if( pid->b_seen )
844         {
845             msg_Dbg( p_demux, "  - pid[%d] seen", pid->i_pid );
846         }
847
848         if( p_sys->b_access_control && pid->i_pid > 0 )
849         {
850             /* too much */
851             stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
852                             ACCESS_SET_PRIVATE_ID_STATE, pid->i_pid,
853                             false );
854         }
855     }
856
857     vlc_mutex_lock( &p_sys->csa_lock );
858     if( p_sys->csa )
859     {
860         var_DelCallback( p_demux, "ts-csa-ck", ChangeKeyCallback, NULL );
861         var_DelCallback( p_demux, "ts-csa2-ck", ChangeKeyCallback, NULL );
862         csa_Delete( p_sys->csa );
863     }
864     vlc_mutex_unlock( &p_sys->csa_lock );
865
866     TAB_CLEAN( p_sys->i_pmt, p_sys->pmt );
867
868     if( p_sys->p_programs_list )
869     {
870         vlc_value_t val;
871         val.p_list = p_sys->p_programs_list;
872         var_FreeList( &val, NULL );
873     }
874
875     /* If in dump mode, then close the file */
876     if( p_sys->b_file_out )
877     {
878         msg_Info( p_demux ,"closing %s (%"PRId64" Kbytes dumped)",
879                   p_sys->psz_file, p_sys->i_write / 1024 );
880
881         if( p_sys->p_file != stdout )
882         {
883             fclose( p_sys->p_file );
884         }
885     }
886     /* When streaming, close the port */
887     if( p_sys->fd > -1 )
888     {
889         net_Close( p_sys->fd );
890     }
891
892     free( p_sys->buffer );
893     free( p_sys->psz_file );
894
895     vlc_mutex_destroy( &p_sys->csa_lock );
896     free( p_sys );
897 }
898
899 /*****************************************************************************
900  * ChangeKeyCallback: called when changing the odd encryption key on the fly.
901  *****************************************************************************/
902 static int ChangeKeyCallback( vlc_object_t *p_this, char const *psz_cmd,
903                            vlc_value_t oldval, vlc_value_t newval,
904                            void *p_data )
905 {
906     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
907     demux_t     *p_demux = (demux_t*)p_this;
908     demux_sys_t *p_sys = p_demux->p_sys;
909     int         i_tmp = (intptr_t)p_data;
910
911     vlc_mutex_lock( &p_sys->csa_lock );
912     if ( i_tmp )
913         i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, true );
914     else
915         i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, false );
916
917     vlc_mutex_unlock( &p_sys->csa_lock );
918     return i_tmp;
919 }
920
921 /*****************************************************************************
922  * DemuxFile:
923  *****************************************************************************/
924 static int DemuxFile( demux_t *p_demux )
925 {
926     demux_sys_t *p_sys = p_demux->p_sys;
927     const int i_bufsize = p_sys->i_packet_size * p_sys->i_ts_read;
928     uint8_t   *p_buffer = p_sys->buffer; /* Put first on sync byte */
929     const int i_data = stream_Read( p_demux->s, p_sys->buffer, i_bufsize );
930
931     if( i_data <= 0 && i_data < p_sys->i_packet_size )
932     {
933         msg_Dbg( p_demux, "error reading malformed packets" );
934         return i_data;
935     }
936
937     /* Test continuity counter */
938     for( int i_pos = 0; i_pos < i_data;  )
939     {
940         if( p_sys->buffer[i_pos] != 0x47 )
941         {
942             msg_Warn( p_demux, "lost sync" );
943             while( vlc_object_alive (p_demux) && (i_pos < i_data) )
944             {
945                 i_pos++;
946                 if( p_sys->buffer[i_pos] == 0x47 )
947                     break;
948             }
949             if( vlc_object_alive (p_demux) )
950                 msg_Warn( p_demux, "sync found" );
951         }
952
953         /* continuous when (one of this):
954          * diff == 1
955          * diff == 0 and payload == 0
956          * diff == 0 and duplicate packet (playload != 0) <- should we
957          *   test the content ?
958          */
959         const int i_cc  = p_buffer[i_pos+3]&0x0f;
960         const bool b_payload = p_buffer[i_pos+3]&0x10;
961         const bool b_adaptation = p_buffer[i_pos+3]&0x20;
962
963         /* Get the PID */
964         ts_pid_t *p_pid = &p_sys->pid[ ((p_buffer[i_pos+1]&0x1f)<<8)|p_buffer[i_pos+2] ];
965
966         /* Detect discontinuity indicator in adaptation field */
967         if( b_adaptation && p_buffer[i_pos + 4] > 0 )
968         {
969             if( p_buffer[i_pos+5]&0x80 )
970                 msg_Warn( p_demux, "discontinuity indicator (pid=%d) ", p_pid->i_pid );
971             if( p_buffer[i_pos+5]&0x40 )
972                 msg_Warn( p_demux, "random access indicator (pid=%d) ", p_pid->i_pid );
973         }
974
975         const int i_diff = ( i_cc - p_pid->i_cc )&0x0f;
976         if( b_payload && i_diff == 1 )
977         {
978             p_pid->i_cc = ( p_pid->i_cc + 1 ) & 0xf;
979         }
980         else
981         {
982             if( p_pid->i_cc == 0xff )
983             {
984                 msg_Warn( p_demux, "first packet for pid=%d cc=0x%x",
985                         p_pid->i_pid, i_cc );
986                 p_pid->i_cc = i_cc;
987             }
988             else if( i_diff != 0 )
989             {
990                 /* FIXME what to do when discontinuity_indicator is set ? */
991                 msg_Warn( p_demux, "transport error detected 0x%x instead of 0x%x",
992                           i_cc, ( p_pid->i_cc + 1 )&0x0f );
993
994                 p_pid->i_cc = i_cc;
995                 /* Mark transport error in the TS packet. */
996                 p_buffer[i_pos+1] |= 0x80;
997             }
998         }
999
1000         /* Test if user wants to decrypt it first */
1001         if( p_sys->csa )
1002         {
1003             vlc_mutex_lock( &p_sys->csa_lock );
1004             csa_Decrypt( p_demux->p_sys->csa, &p_buffer[i_pos], p_demux->p_sys->i_csa_pkt_size );
1005             vlc_mutex_unlock( &p_sys->csa_lock );
1006         }
1007
1008         i_pos += p_sys->i_packet_size;
1009     }
1010
1011     /* Then write */
1012     const int i_write = fwrite( p_sys->buffer, 1, i_data, p_sys->p_file );
1013     if( i_write < 0 )
1014     {
1015         msg_Err( p_demux, "failed to write data" );
1016         return -1;
1017     }
1018
1019     p_sys->i_write += i_write;
1020     return 1;
1021 }
1022
1023 /*****************************************************************************
1024  * Demux:
1025  *****************************************************************************/
1026 static int Demux( demux_t *p_demux )
1027 {
1028     demux_sys_t *p_sys = p_demux->p_sys;
1029
1030     /* We read at most 100 TS packet or until a frame is completed */
1031     for( int i_pkt = 0; i_pkt < p_sys->i_ts_read; i_pkt++ )
1032     {
1033         bool         b_frame = false;
1034         block_t     *p_pkt;
1035
1036         /* Get a new TS packet */
1037         if( !( p_pkt = stream_Block( p_demux->s, p_sys->i_packet_size ) ) )
1038         {
1039             msg_Dbg( p_demux, "eof ?" );
1040             return 0;
1041         }
1042
1043         /* Check sync byte and re-sync if needed */
1044         if( p_pkt->p_buffer[0] != 0x47 )
1045         {
1046             msg_Warn( p_demux, "lost synchro" );
1047             block_Release( p_pkt );
1048
1049             while( vlc_object_alive (p_demux) )
1050             {
1051                 const uint8_t *p_peek;
1052                 int i_peek, i_skip = 0;
1053
1054                 i_peek = stream_Peek( p_demux->s, &p_peek,
1055                                       p_sys->i_packet_size * 10 );
1056                 if( i_peek < p_sys->i_packet_size + 1 )
1057                 {
1058                     msg_Dbg( p_demux, "eof ?" );
1059                     return 0;
1060                 }
1061
1062                 while( i_skip < i_peek - p_sys->i_packet_size )
1063                 {
1064                     if( p_peek[i_skip] == 0x47 &&
1065                         p_peek[i_skip + p_sys->i_packet_size] == 0x47 )
1066                     {
1067                         break;
1068                     }
1069                     i_skip++;
1070                 }
1071
1072                 msg_Dbg( p_demux, "skipping %d bytes of garbage", i_skip );
1073                 stream_Read( p_demux->s, NULL, i_skip );
1074
1075                 if( i_skip < i_peek - p_sys->i_packet_size )
1076                 {
1077                     break;
1078                 }
1079             }
1080
1081             if( !( p_pkt = stream_Block( p_demux->s, p_sys->i_packet_size ) ) )
1082             {
1083                 msg_Dbg( p_demux, "eof ?" );
1084                 return 0;
1085             }
1086         }
1087
1088         if( p_sys->b_start_record )
1089         {
1090             /* Enable recording once synchronized */
1091             stream_Control( p_demux->s, STREAM_SET_RECORD_STATE, true, "ts" );
1092             p_sys->b_start_record = false;
1093         }
1094
1095         if( p_sys->b_udp_out )
1096         {
1097             memcpy( &p_sys->buffer[i_pkt * p_sys->i_packet_size],
1098                     p_pkt->p_buffer, p_sys->i_packet_size );
1099         }
1100
1101         /* Parse the TS packet */
1102         ts_pid_t *p_pid = &p_sys->pid[PIDGet( p_pkt )];
1103
1104         if( p_pid->b_valid )
1105         {
1106             if( p_pid->psi )
1107             {
1108                 if( p_pid->i_pid == 0 || ( p_sys->b_dvb_meta && ( p_pid->i_pid == 0x11 || p_pid->i_pid == 0x12 || p_pid->i_pid == 0x14 ) ) )
1109                 {
1110                     dvbpsi_PushPacket( p_pid->psi->handle, p_pkt->p_buffer );
1111                 }
1112                 else
1113                 {
1114                     for( int i_prg = 0; i_prg < p_pid->psi->i_prg; i_prg++ )
1115                     {
1116                         dvbpsi_PushPacket( p_pid->psi->prg[i_prg]->handle,
1117                                            p_pkt->p_buffer );
1118                     }
1119                 }
1120                 block_Release( p_pkt );
1121             }
1122             else if( !p_sys->b_udp_out )
1123             {
1124                 b_frame = GatherPES( p_demux, p_pid, p_pkt );
1125             }
1126             else
1127             {
1128                 PCRHandle( p_demux, p_pid, p_pkt );
1129                 block_Release( p_pkt );
1130             }
1131         }
1132         else
1133         {
1134             if( !p_pid->b_seen )
1135             {
1136                 msg_Dbg( p_demux, "pid[%d] unknown", p_pid->i_pid );
1137             }
1138             /* We have to handle PCR if present */
1139             PCRHandle( p_demux, p_pid, p_pkt );
1140             block_Release( p_pkt );
1141         }
1142         p_pid->b_seen = true;
1143
1144         if( b_frame )
1145             break;
1146     }
1147
1148     if( p_sys->b_udp_out )
1149     {
1150         /* Send the complete block */
1151         net_Write( p_demux, p_sys->fd, NULL, p_sys->buffer,
1152                    p_sys->i_ts_read * p_sys->i_packet_size );
1153     }
1154
1155     return 1;
1156 }
1157
1158 /*****************************************************************************
1159  * Control:
1160  *****************************************************************************/
1161 static int DVBEventInformation( demux_t *p_demux, int64_t *pi_time, int64_t *pi_length )
1162 {
1163     demux_sys_t *p_sys = p_demux->p_sys;
1164     if( pi_length )
1165         *pi_length = 0;
1166     if( pi_time )
1167         *pi_time = 0;
1168
1169     if( p_sys->i_dvb_length > 0 )
1170     {
1171         const time_t t = time (NULL) + p_sys->i_tdt_delta;
1172         if( p_sys->i_dvb_start <= t && t < p_sys->i_dvb_start + p_sys->i_dvb_length )
1173         {
1174             if( pi_length )
1175                 *pi_length = p_sys->i_dvb_length * INT64_C(1000000);
1176             if( pi_time )
1177                 *pi_time   = (t - p_sys->i_dvb_start) * INT64_C(1000000);
1178             return VLC_SUCCESS;
1179         }
1180     }
1181     return VLC_EGENERIC;
1182 }
1183
1184 static int Control( demux_t *p_demux, int i_query, va_list args )
1185 {
1186     demux_sys_t *p_sys = p_demux->p_sys;
1187     double f, *pf;
1188     bool b_bool, *pb_bool;
1189     int64_t i64;
1190     int64_t *pi64;
1191     int i_int;
1192
1193     if( p_sys->b_file_out )
1194         return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
1195
1196     switch( i_query )
1197     {
1198     case DEMUX_GET_POSITION:
1199         pf = (double*) va_arg( args, double* );
1200         i64 = stream_Size( p_demux->s );
1201         if( i64 > 0 )
1202         {
1203             double f_current = stream_Tell( p_demux->s );
1204             *pf = f_current / (double)i64;
1205         }
1206         else
1207         {
1208             int64_t i_time, i_length;
1209             if( !DVBEventInformation( p_demux, &i_time, &i_length ) && i_length > 0 )
1210                 *pf = (double)i_time/(double)i_length;
1211             else
1212                 *pf = 0.0;
1213         }
1214         return VLC_SUCCESS;
1215     case DEMUX_SET_POSITION:
1216         f = (double) va_arg( args, double );
1217         i64 = stream_Size( p_demux->s );
1218
1219         if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) )
1220             return VLC_EGENERIC;
1221
1222         return VLC_SUCCESS;
1223 #if 0
1224
1225     case DEMUX_GET_TIME:
1226         pi64 = (int64_t*)va_arg( args, int64_t * );
1227         if( p_sys->i_time < 0 )
1228         {
1229             *pi64 = 0;
1230             return VLC_EGENERIC;
1231         }
1232         *pi64 = p_sys->i_time;
1233         return VLC_SUCCESS;
1234
1235     case DEMUX_GET_LENGTH:
1236         pi64 = (int64_t*)va_arg( args, int64_t * );
1237         if( p_sys->i_mux_rate > 0 )
1238         {
1239             *pi64 = INT64_C(1000000) * ( stream_Size( p_demux->s ) / 50 ) /
1240                     p_sys->i_mux_rate;
1241             return VLC_SUCCESS;
1242         }
1243         *pi64 = 0;
1244         return VLC_EGENERIC;
1245 #else
1246     case DEMUX_GET_TIME:
1247         pi64 = (int64_t*)va_arg( args, int64_t * );
1248         if( DVBEventInformation( p_demux, pi64, NULL ) )
1249             *pi64 = 0;
1250         return VLC_SUCCESS;
1251
1252     case DEMUX_GET_LENGTH:
1253         pi64 = (int64_t*)va_arg( args, int64_t * );
1254         if( DVBEventInformation( p_demux, NULL, pi64 ) )
1255             *pi64 = 0;
1256         return VLC_SUCCESS;
1257 #endif
1258     case DEMUX_SET_GROUP:
1259     {
1260         uint16_t i_vpid = 0, i_apid1 = 0, i_apid2 = 0, i_apid3 = 0;
1261         ts_prg_psi_t *p_prg = NULL;
1262         vlc_list_t *p_list;
1263
1264         i_int = (int)va_arg( args, int );
1265         p_list = (vlc_list_t *)va_arg( args, vlc_list_t * );
1266         msg_Dbg( p_demux, "DEMUX_SET_GROUP %d %p", i_int, p_list );
1267
1268         if( p_sys->b_access_control && i_int > 0 && i_int != p_sys->i_current_program )
1269         {
1270             int i_pmt_pid = -1;
1271
1272             /* Search pmt to be unselected */
1273             for( int i = 0; i < p_sys->i_pmt; i++ )
1274             {
1275                 ts_pid_t *pmt = p_sys->pmt[i];
1276
1277                 for( int i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
1278                 {
1279                     if( pmt->psi->prg[i_prg]->i_number == p_sys->i_current_program )
1280                     {
1281                         i_pmt_pid = p_sys->pmt[i]->i_pid;
1282                         break;
1283                     }
1284                 }
1285                 if( i_pmt_pid > 0 ) break;
1286             }
1287
1288             if( i_pmt_pid > 0 )
1289             {
1290                 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
1291                                 ACCESS_SET_PRIVATE_ID_STATE, i_pmt_pid,
1292                                 false );
1293                 /* All ES */
1294                 for( int i = 2; i < 8192; i++ )
1295                 {
1296                     ts_pid_t *pid = &p_sys->pid[i];
1297
1298                     if( !pid->b_valid || pid->psi )
1299                         continue;
1300
1301                     for( int i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
1302                     {
1303                         if( pid->p_owner->prg[i_prg]->i_pid_pmt == i_pmt_pid && pid->es->id )
1304                         {
1305                             /* We only remove es that aren't defined by extra pmt */
1306                             stream_Control( p_demux->s,
1307                                             STREAM_CONTROL_ACCESS,
1308                                             ACCESS_SET_PRIVATE_ID_STATE,
1309                                             i, false );
1310                             break;
1311                         }
1312                     }
1313                 }
1314             }
1315
1316             /* select new program */
1317             p_sys->i_current_program = i_int;
1318             i_pmt_pid = -1;
1319             for( int i = 0; i < p_sys->i_pmt; i++ )
1320             {
1321                 ts_pid_t *pmt = p_sys->pmt[i];
1322
1323                 for( int i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
1324                 {
1325                     if( pmt->psi->prg[i_prg]->i_number == i_int )
1326                     {
1327                         i_pmt_pid = p_sys->pmt[i]->i_pid;
1328                         p_prg = p_sys->pmt[i]->psi->prg[i_prg];
1329                         break;
1330                     }
1331                 }
1332                 if( i_pmt_pid > 0 )
1333                     break;
1334             }
1335             if( i_pmt_pid > 0 )
1336             {
1337                 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
1338                                 ACCESS_SET_PRIVATE_ID_STATE, i_pmt_pid,
1339                                 true );
1340                 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
1341                                 ACCESS_SET_PRIVATE_ID_STATE, p_prg->i_pid_pcr,
1342                                 true );
1343
1344                 for( int i = 2; i < 8192; i++ )
1345                 {
1346                     ts_pid_t *pid = &p_sys->pid[i];
1347
1348                     if( !pid->b_valid || pid->psi )
1349                         continue;
1350
1351                     for( int i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
1352                     {
1353                         if( pid->p_owner->prg[i_prg]->i_pid_pmt == i_pmt_pid && pid->es->id )
1354                         {
1355                             if ( pid->es->fmt.i_cat == VIDEO_ES && !i_vpid )
1356                                 i_vpid = i;
1357                             if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid1 )
1358                                 i_apid1 = i;
1359                             else if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid2 )
1360                                 i_apid2 = i;
1361                             else if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid3 )
1362                                 i_apid3 = i;
1363
1364                             stream_Control( p_demux->s,
1365                                             STREAM_CONTROL_ACCESS,
1366                                             ACCESS_SET_PRIVATE_ID_STATE,
1367                                             i, true );
1368                             break;
1369                         }
1370                     }
1371                 }
1372             }
1373         }
1374         else
1375         {
1376             p_sys->i_current_program = -1;
1377             p_sys->p_programs_list = p_list;
1378         }
1379         return VLC_SUCCESS;
1380     }
1381
1382     case DEMUX_CAN_RECORD:
1383         pb_bool = (bool*)va_arg( args, bool * );
1384         *pb_bool = true;
1385         return VLC_SUCCESS;
1386
1387     case DEMUX_SET_RECORD_STATE:
1388         b_bool = (bool)va_arg( args, int );
1389
1390         if( !b_bool )
1391             stream_Control( p_demux->s, STREAM_SET_RECORD_STATE, false );
1392         p_sys->b_start_record = b_bool;
1393         return VLC_SUCCESS;
1394
1395     case DEMUX_GET_FPS:
1396     case DEMUX_SET_TIME:
1397     default:
1398         return VLC_EGENERIC;
1399     }
1400 }
1401
1402 /*****************************************************************************
1403  *
1404  *****************************************************************************/
1405 static int UserPmt( demux_t *p_demux, const char *psz_fmt )
1406 {
1407     demux_sys_t *p_sys = p_demux->p_sys;
1408     char *psz_dup = strdup( psz_fmt );
1409     char *psz = psz_dup;
1410     int  i_pid;
1411     int  i_number;
1412
1413     if( !psz_dup )
1414         return VLC_ENOMEM;
1415
1416     /* Parse PID */
1417     i_pid = strtol( psz, &psz, 0 );
1418     if( i_pid < 2 || i_pid >= 8192 )
1419         goto error;
1420
1421     /* Parse optional program number */
1422     i_number = 0;
1423     if( *psz == ':' )
1424         i_number = strtol( &psz[1], &psz, 0 );
1425
1426     /* */
1427     ts_pid_t *pmt = &p_sys->pid[i_pid];
1428     ts_prg_psi_t *prg;
1429
1430     msg_Dbg( p_demux, "user pmt specified (pid=%d,number=%d)", i_pid, i_number );
1431     PIDInit( pmt, true, NULL );
1432
1433     /* Dummy PMT */
1434     prg = malloc( sizeof( ts_prg_psi_t ) );
1435     if( !prg )
1436         goto error;
1437
1438     memset( prg, 0, sizeof( ts_prg_psi_t ) );
1439     prg->i_pid_pcr  = -1;
1440     prg->i_pid_pmt  = -1;
1441     prg->i_version  = -1;
1442     prg->i_number   = i_number != 0 ? i_number : TS_USER_PMT_NUMBER;
1443     prg->handle     = dvbpsi_AttachPMT( i_number != TS_USER_PMT_NUMBER ? i_number : 1, (dvbpsi_pmt_callback)PMTCallBack, p_demux );
1444     TAB_APPEND( pmt->psi->i_prg, pmt->psi->prg, prg );
1445
1446     psz = strchr( psz, '=' );
1447     if( psz )
1448         psz++;
1449     while( psz && *psz )
1450     {
1451         char *psz_next = strchr( psz, ',' );
1452         int i_pid;
1453
1454         if( psz_next )
1455             *psz_next++ = '\0';
1456
1457         i_pid = strtol( psz, &psz, 0 );
1458         if( *psz != ':' || i_pid < 2 || i_pid >= 8192 )
1459             goto next;
1460
1461         char *psz_opt = &psz[1];
1462         if( !strcmp( psz_opt, "pcr" ) )
1463         {
1464             prg->i_pid_pcr = i_pid;
1465         }
1466         else if( !p_sys->pid[i_pid].b_valid )
1467         {
1468             ts_pid_t *pid = &p_sys->pid[i_pid];
1469
1470             char *psz_arg = strchr( psz_opt, '=' );
1471             if( psz_arg )
1472                 *psz_arg++ = '\0';
1473
1474             PIDInit( pid, false, pmt->psi);
1475             if( prg->i_pid_pcr <= 0 )
1476                 prg->i_pid_pcr = i_pid;
1477
1478             if( psz_arg && strlen( psz_arg ) == 4 )
1479             {
1480                 const vlc_fourcc_t i_codec = VLC_FOURCC( psz_arg[0], psz_arg[1],
1481                                                          psz_arg[2], psz_arg[3] );
1482                 int i_cat = UNKNOWN_ES;
1483                 es_format_t *fmt = &pid->es->fmt;
1484
1485                 if( !strcmp( psz_opt, "video" ) )
1486                     i_cat = VIDEO_ES;
1487                 else if( !strcmp( psz_opt, "audio" ) )
1488                     i_cat = AUDIO_ES;
1489                 else if( !strcmp( psz_opt, "spu" ) )
1490                     i_cat = SPU_ES;
1491
1492                 es_format_Init( fmt, i_cat, i_codec );
1493                 fmt->b_packetized = false;
1494             }
1495             else
1496             {
1497                 const int i_stream_type = strtol( psz_opt, NULL, 0 );
1498                 PIDFillFormat( pid, i_stream_type );
1499             }
1500             pid->es->fmt.i_group = i_number;
1501             if( p_sys->b_es_id_pid )
1502                 pid->es->fmt.i_id = i_pid;
1503
1504             if( pid->es->fmt.i_cat != UNKNOWN_ES )
1505             {
1506                 msg_Dbg( p_demux, "  * es pid=%d fcc=%4.4s", i_pid,
1507                          (char*)&pid->es->fmt.i_codec );
1508                 pid->es->id = es_out_Add( p_demux->out,
1509                                           &pid->es->fmt );
1510             }
1511         }
1512
1513     next:
1514         psz = psz_next;
1515     }
1516
1517     p_sys->b_user_pmt = true;
1518     TAB_APPEND( p_sys->i_pmt, p_sys->pmt, pmt );
1519     free( psz_dup );
1520     return VLC_SUCCESS;
1521
1522 error:
1523     free( psz_dup );
1524     return VLC_EGENERIC;
1525 }
1526
1527 static void PIDInit( ts_pid_t *pid, bool b_psi, ts_psi_t *p_owner )
1528 {
1529     bool b_old_valid = pid->b_valid;
1530
1531     pid->b_valid    = true;
1532     pid->i_cc       = 0xff;
1533     pid->b_scrambled = false;
1534     pid->p_owner    = p_owner;
1535     pid->i_owner_number = 0;
1536
1537     TAB_INIT( pid->i_extra_es, pid->extra_es );
1538
1539     if( b_psi )
1540     {
1541         pid->es  = NULL;
1542
1543         if( !b_old_valid )
1544         {
1545             pid->psi = malloc( sizeof( ts_psi_t ) );
1546             if( pid->psi )
1547             {
1548                 pid->psi->handle = NULL;
1549                 TAB_INIT( pid->psi->i_prg, pid->psi->prg );
1550             }
1551         }
1552         assert( pid->psi );
1553
1554         pid->psi->i_pat_version  = -1;
1555         pid->psi->i_sdt_version  = -1;
1556         if( p_owner )
1557         {
1558             ts_prg_psi_t *prg = malloc( sizeof( ts_prg_psi_t ) );
1559             if( prg )
1560             {
1561                 /* PMT */
1562                 prg->i_version  = -1;
1563                 prg->i_number   = -1;
1564                 prg->i_pid_pcr  = -1;
1565                 prg->i_pid_pmt  = -1;
1566                 prg->iod        = NULL;
1567                 prg->handle     = NULL;
1568
1569                 TAB_APPEND( pid->psi->i_prg, pid->psi->prg, prg );
1570             }
1571         }
1572     }
1573     else
1574     {
1575         pid->psi = NULL;
1576         pid->es  = malloc( sizeof( ts_es_t ) );
1577         if( pid->es )
1578         {
1579             es_format_Init( &pid->es->fmt, UNKNOWN_ES, 0 );
1580             pid->es->id      = NULL;
1581             pid->es->p_pes   = NULL;
1582             pid->es->i_pes_size= 0;
1583             pid->es->i_pes_gathered= 0;
1584             pid->es->pp_last = &pid->es->p_pes;
1585             pid->es->p_mpeg4desc = NULL;
1586             pid->es->b_gather = false;
1587         }
1588     }
1589 }
1590
1591 static void PIDClean( es_out_t *out, ts_pid_t *pid )
1592 {
1593     if( pid->psi )
1594     {
1595         if( pid->psi->handle )
1596             dvbpsi_DetachPMT( pid->psi->handle );
1597         for( int i = 0; i < pid->psi->i_prg; i++ )
1598         {
1599             if( pid->psi->prg[i]->iod )
1600                 IODFree( pid->psi->prg[i]->iod );
1601             if( pid->psi->prg[i]->handle )
1602                 dvbpsi_DetachPMT( pid->psi->prg[i]->handle );
1603             free( pid->psi->prg[i] );
1604         }
1605         free( pid->psi->prg );
1606         free( pid->psi );
1607     }
1608     else
1609     {
1610         if( pid->es->id )
1611             es_out_Del( out, pid->es->id );
1612
1613         if( pid->es->p_pes )
1614             block_ChainRelease( pid->es->p_pes );
1615
1616         es_format_Clean( &pid->es->fmt );
1617
1618         free( pid->es );
1619
1620         for( int i = 0; i < pid->i_extra_es; i++ )
1621         {
1622             if( pid->extra_es[i]->id )
1623                 es_out_Del( out, pid->extra_es[i]->id );
1624
1625             if( pid->extra_es[i]->p_pes )
1626                 block_ChainRelease( pid->extra_es[i]->p_pes );
1627
1628             es_format_Clean( &pid->extra_es[i]->fmt );
1629
1630             free( pid->extra_es[i] );
1631         }
1632         if( pid->i_extra_es )
1633             free( pid->extra_es );
1634     }
1635
1636     pid->b_valid = false;
1637 }
1638
1639 /****************************************************************************
1640  * gathering stuff
1641  ****************************************************************************/
1642 static void ParsePES( demux_t *p_demux, ts_pid_t *pid )
1643 {
1644     block_t *p_pes = pid->es->p_pes;
1645     uint8_t header[34];
1646     unsigned i_pes_size = 0;
1647     unsigned i_skip = 0;
1648     mtime_t i_dts = -1;
1649     mtime_t i_pts = -1;
1650     mtime_t i_length = 0;
1651
1652     /* remove the pes from pid */
1653     pid->es->p_pes = NULL;
1654     pid->es->i_pes_size= 0;
1655     pid->es->i_pes_gathered= 0;
1656     pid->es->pp_last = &pid->es->p_pes;
1657
1658     /* FIXME find real max size */
1659     /* const int i_max = */ block_ChainExtract( p_pes, header, 34 );
1660     
1661     if( header[0] != 0 || header[1] != 0 || header[2] != 1 )
1662     {
1663         if( !p_demux->p_sys->b_silent )
1664             msg_Warn( p_demux, "invalid header [0x%x:%x:%x:%x] (pid: %d)",
1665                       header[0], header[1],header[2],header[3], pid->i_pid );
1666         block_ChainRelease( p_pes );
1667         return;
1668     }
1669
1670     /* TODO check size */
1671     switch( header[3] )
1672     {
1673     case 0xBC:  /* Program stream map */
1674     case 0xBE:  /* Padding */
1675     case 0xBF:  /* Private stream 2 */
1676     case 0xF0:  /* ECM */
1677     case 0xF1:  /* EMM */
1678     case 0xFF:  /* Program stream directory */
1679     case 0xF2:  /* DSMCC stream */
1680     case 0xF8:  /* ITU-T H.222.1 type E stream */
1681         i_skip = 6;
1682         break;
1683     default:
1684         if( ( header[6]&0xC0 ) == 0x80 )
1685         {
1686             /* mpeg2 PES */
1687             i_skip = header[8] + 9;
1688
1689             if( header[7]&0x80 )    /* has pts */
1690             {
1691                 i_pts = ((mtime_t)(header[ 9]&0x0e ) << 29)|
1692                          (mtime_t)(header[10] << 22)|
1693                         ((mtime_t)(header[11]&0xfe) << 14)|
1694                          (mtime_t)(header[12] << 7)|
1695                          (mtime_t)(header[13] >> 1);
1696
1697                 if( header[7]&0x40 )    /* has dts */
1698                 {
1699                      i_dts = ((mtime_t)(header[14]&0x0e ) << 29)|
1700                              (mtime_t)(header[15] << 22)|
1701                             ((mtime_t)(header[16]&0xfe) << 14)|
1702                              (mtime_t)(header[17] << 7)|
1703                              (mtime_t)(header[18] >> 1);
1704                 }
1705             }
1706         }
1707         else
1708         {
1709             i_skip = 6;
1710             while( i_skip < 23 && header[i_skip] == 0xff )
1711             {
1712                 i_skip++;
1713             }
1714             if( i_skip == 23 )
1715             {
1716                 msg_Err( p_demux, "too much MPEG-1 stuffing" );
1717                 block_ChainRelease( p_pes );
1718                 return;
1719             }
1720             if( ( header[i_skip] & 0xC0 ) == 0x40 )
1721             {
1722                 i_skip += 2;
1723             }
1724
1725             if(  header[i_skip]&0x20 )
1726             {
1727                  i_pts = ((mtime_t)(header[i_skip]&0x0e ) << 29)|
1728                           (mtime_t)(header[i_skip+1] << 22)|
1729                          ((mtime_t)(header[i_skip+2]&0xfe) << 14)|
1730                           (mtime_t)(header[i_skip+3] << 7)|
1731                           (mtime_t)(header[i_skip+4] >> 1);
1732
1733                 if( header[i_skip]&0x10 )    /* has dts */
1734                 {
1735                      i_dts = ((mtime_t)(header[i_skip+5]&0x0e ) << 29)|
1736                               (mtime_t)(header[i_skip+6] << 22)|
1737                              ((mtime_t)(header[i_skip+7]&0xfe) << 14)|
1738                               (mtime_t)(header[i_skip+8] << 7)|
1739                               (mtime_t)(header[i_skip+9] >> 1);
1740                      i_skip += 10;
1741                 }
1742                 else
1743                 {
1744                     i_skip += 5;
1745                 }
1746             }
1747             else
1748             {
1749                 i_skip += 1;
1750             }
1751         }
1752         break;
1753     }
1754
1755     if( pid->es->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', 'b' ) ||
1756         pid->es->fmt.i_codec == VLC_FOURCC( 'd', 't', 's', 'b' ) )
1757     {
1758         i_skip += 4;
1759     }
1760     else if( pid->es->fmt.i_codec == VLC_FOURCC( 'l', 'p', 'c', 'b' ) ||
1761              pid->es->fmt.i_codec == VLC_FOURCC( 's', 'p', 'u', 'b' ) ||
1762              pid->es->fmt.i_codec == VLC_FOURCC( 's', 'd', 'd', 'b' ) )
1763     {
1764         i_skip += 1;
1765     }
1766     else if( pid->es->fmt.i_codec == VLC_CODEC_SUBT &&
1767              pid->es->p_mpeg4desc )
1768     {
1769         decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;
1770
1771         if( dcd->i_decoder_specific_info_len > 2 &&
1772             dcd->p_decoder_specific_info[0] == 0x10 &&
1773             ( dcd->p_decoder_specific_info[1]&0x10 ) )
1774         {
1775             /* display length */
1776             if( p_pes->i_buffer + 2 <= i_skip )
1777                 i_length = GetWBE( &p_pes->p_buffer[i_skip] );
1778
1779             i_skip += 2;
1780         }
1781         if( p_pes->i_buffer + 2 <= i_skip )
1782             i_pes_size = GetWBE( &p_pes->p_buffer[i_skip] );
1783         /* */
1784         i_skip += 2;
1785     }
1786 #ifdef ZVBI_COMPILED
1787     else if( pid->es->fmt.i_codec == VLC_CODEC_TELETEXT )
1788         i_skip = 0; /*hack for zvbi support */
1789 #endif
1790     /* skip header */
1791     while( p_pes && i_skip > 0 )
1792     {
1793         if( p_pes->i_buffer <= i_skip )
1794         {
1795             block_t *p_next = p_pes->p_next;
1796
1797             i_skip -= p_pes->i_buffer;
1798             block_Release( p_pes );
1799             p_pes = p_next;
1800         }
1801         else
1802         {
1803             p_pes->i_buffer -= i_skip;
1804             p_pes->p_buffer += i_skip;
1805             break;
1806         }
1807     }
1808
1809     /* ISO/IEC 13818-1 2.7.5: if no pts and no dts, then dts == pts */
1810     if( i_pts >= 0 && i_dts < 0 )
1811         i_dts = i_pts;
1812
1813     if( p_pes )
1814     {
1815         block_t *p_block;
1816         int i;
1817
1818         if( i_dts >= 0 )
1819         {
1820             p_pes->i_dts = i_dts * 100 / 9;
1821         }
1822         if( i_pts >= 0 )
1823         {
1824             p_pes->i_pts = i_pts * 100 / 9;
1825         }
1826         p_pes->i_length = i_length * 100 / 9;
1827
1828         p_block = block_ChainGather( p_pes );
1829         if( pid->es->fmt.i_codec == VLC_CODEC_SUBT )
1830         {
1831             if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
1832             {
1833                 p_block->i_buffer = i_pes_size;
1834             }
1835             /* Append a \0 */
1836             p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
1837             p_block->p_buffer[p_block->i_buffer -1] = '\0';
1838         }
1839
1840         for( i = 0; i < pid->i_extra_es; i++ )
1841         {
1842             es_out_Send( p_demux->out, pid->extra_es[i]->id,
1843                          block_Duplicate( p_block ) );
1844         }
1845
1846         es_out_Send( p_demux->out, pid->es->id, p_block );
1847     }
1848     else
1849     {
1850         msg_Warn( p_demux, "empty pes" );
1851     }
1852 }
1853
1854 static void PCRHandle( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
1855 {
1856     demux_sys_t   *p_sys = p_demux->p_sys;
1857     const uint8_t *p = p_bk->p_buffer;
1858
1859     if( ( p[3]&0x20 ) && /* adaptation */
1860         ( p[5]&0x10 ) &&
1861         ( p[4] >= 7 ) )
1862     {
1863         /* PCR is 33 bits */
1864         const mtime_t i_pcr = ( (mtime_t)p[6] << 25 ) |
1865                               ( (mtime_t)p[7] << 17 ) |
1866                               ( (mtime_t)p[8] << 9 ) |
1867                               ( (mtime_t)p[9] << 1 ) |
1868                               ( (mtime_t)p[10] >> 7 );
1869
1870         /* Search program and set the PCR */
1871         for( int i = 0; i < p_sys->i_pmt; i++ )
1872         {
1873             for( int i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
1874             {
1875                 if( pid->i_pid == p_sys->pmt[i]->psi->prg[i_prg]->i_pid_pcr )
1876                 {
1877                     es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
1878                                     (int)p_sys->pmt[i]->psi->prg[i_prg]->i_number,
1879                                     (int64_t)(i_pcr * 100 / 9) );
1880                 }
1881             }
1882         }
1883     }
1884 }
1885
1886 static bool GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
1887 {
1888     const uint8_t *p = p_bk->p_buffer;
1889     const bool b_unit_start = p[1]&0x40;
1890     const bool b_scrambled  = p[3]&0x80;
1891     const bool b_adaptation = p[3]&0x20;
1892     const bool b_payload    = p[3]&0x10;
1893     const int  i_cc         = p[3]&0x0f; /* continuity counter */
1894     bool       b_discontinuity = false;  /* discontinuity */
1895
1896     /* transport_scrambling_control is ignored */
1897     int         i_skip = 0;
1898     bool        i_ret  = false;
1899
1900 #if 0
1901     msg_Dbg( p_demux, "pid=%d unit_start=%d adaptation=%d payload=%d "
1902              "cc=0x%x", pid->i_pid, b_unit_start, b_adaptation,
1903              b_payload, i_cc );
1904 #endif
1905
1906     /* For now, ignore additional error correction
1907      * TODO: handle Reed-Solomon 204,188 error correction */
1908     p_bk->i_buffer = TS_PACKET_SIZE_188;
1909
1910     if( p[1]&0x80 )
1911     {
1912         msg_Dbg( p_demux, "transport_error_indicator set (pid=%d)",
1913                  pid->i_pid );
1914         if( pid->es->p_pes ) //&& pid->es->fmt.i_cat == VIDEO_ES )
1915             pid->es->p_pes->i_flags |= BLOCK_FLAG_CORRUPTED;
1916     }
1917
1918     if( p_demux->p_sys->csa )
1919     {
1920         vlc_mutex_lock( &p_demux->p_sys->csa_lock );
1921         csa_Decrypt( p_demux->p_sys->csa, p_bk->p_buffer, p_demux->p_sys->i_csa_pkt_size );
1922         vlc_mutex_unlock( &p_demux->p_sys->csa_lock );
1923     }
1924
1925     if( !b_adaptation )
1926     {
1927         /* We don't have any adaptation_field, so payload starts
1928          * immediately after the 4 byte TS header */
1929         i_skip = 4;
1930     }
1931     else
1932     {
1933         /* p[4] is adaptation_field_length minus one */
1934         i_skip = 5 + p[4];
1935         if( p[4] > 0 )
1936         {
1937             /* discontinuity indicator found in stream */
1938             b_discontinuity = (p[5]&0x80) ? true : false;
1939             if( b_discontinuity && pid->es->p_pes )
1940             {
1941                 msg_Warn( p_demux, "discontinuity indicator (pid=%d) ",
1942                             pid->i_pid );
1943                 /* pid->es->p_pes->i_flags |= BLOCK_FLAG_DISCONTINUITY; */
1944             }
1945 #if 0
1946             if( p[5]&0x40 )
1947                 msg_Dbg( p_demux, "random access indicator (pid=%d) ", pid->i_pid );
1948 #endif
1949         }
1950     }
1951
1952     /* Test continuity counter */
1953     /* continuous when (one of this):
1954         * diff == 1
1955         * diff == 0 and payload == 0
1956         * diff == 0 and duplicate packet (playload != 0) <- should we
1957         *   test the content ?
1958      */
1959     const int i_diff = ( i_cc - pid->i_cc )&0x0f;
1960     if( b_payload && i_diff == 1 )
1961     {
1962         pid->i_cc = ( pid->i_cc + 1 ) & 0xf;
1963     }
1964     else
1965     {
1966         if( pid->i_cc == 0xff )
1967         {
1968             msg_Warn( p_demux, "first packet for pid=%d cc=0x%x",
1969                       pid->i_pid, i_cc );
1970             pid->i_cc = i_cc;
1971         }
1972         else if( i_diff != 0 && !b_discontinuity )
1973         {
1974             msg_Warn( p_demux, "discontinuity received 0x%x instead of 0x%x (pid=%d)",
1975                       i_cc, ( pid->i_cc + 1 )&0x0f, pid->i_pid );
1976
1977             pid->i_cc = i_cc;
1978             if( pid->es->p_pes && pid->es->fmt.i_cat != VIDEO_ES )
1979             {
1980                 /* Small video artifacts are usually better then
1981                  * dropping full frames */
1982                 pid->es->p_pes->i_flags |= BLOCK_FLAG_CORRUPTED;
1983             }
1984         }
1985     }
1986
1987     PCRHandle( p_demux, pid, p_bk );
1988
1989     if( i_skip >= 188 || pid->es->id == NULL || p_demux->p_sys->b_udp_out )
1990     {
1991         block_Release( p_bk );
1992         return i_ret;
1993     }
1994
1995     /* */
1996     if( !pid->b_scrambled != !b_scrambled )
1997     {
1998         msg_Warn( p_demux, "scrambled state changed on pid %d (%d->%d)",
1999                   pid->i_pid, pid->b_scrambled, b_scrambled );
2000
2001         pid->b_scrambled = b_scrambled;
2002
2003         for( int i = 0; i < pid->i_extra_es; i++ )
2004         {
2005             es_out_Control( p_demux->out, ES_OUT_SET_ES_SCRAMBLED_STATE,
2006                             pid->extra_es[i]->id, b_scrambled );
2007         }
2008         es_out_Control( p_demux->out, ES_OUT_SET_ES_SCRAMBLED_STATE,
2009                         pid->es->id, b_scrambled );
2010     }
2011
2012     /* We have to gather it */
2013     p_bk->p_buffer += i_skip;
2014     p_bk->i_buffer -= i_skip;
2015
2016     if( b_unit_start )
2017     {
2018         if( pid->es->p_pes )
2019         {
2020             ParsePES( p_demux, pid );
2021             i_ret = true;
2022         }
2023
2024         block_ChainLastAppend( &pid->es->pp_last, p_bk );
2025         if( p_bk->i_buffer > 6 )
2026         {
2027             pid->es->i_pes_size = GetWBE( &p_bk->p_buffer[4] );
2028             if( pid->es->i_pes_size > 0 )
2029             {
2030                 pid->es->i_pes_size += 6;
2031             }
2032         }
2033         pid->es->i_pes_gathered += p_bk->i_buffer;
2034         if( pid->es->i_pes_size > 0 &&
2035             pid->es->i_pes_gathered >= pid->es->i_pes_size )
2036         {
2037             ParsePES( p_demux, pid );
2038             i_ret = true;
2039         }
2040     }
2041     else
2042     {
2043         if( pid->es->p_pes == NULL )
2044         {
2045             /* msg_Dbg( p_demux, "broken packet" ); */
2046             block_Release( p_bk );
2047         }
2048         else
2049         {
2050             block_ChainLastAppend( &pid->es->pp_last, p_bk );
2051             pid->es->i_pes_gathered += p_bk->i_buffer;
2052             if( pid->es->i_pes_size > 0 &&
2053                 pid->es->i_pes_gathered >= pid->es->i_pes_size )
2054             {
2055                 ParsePES( p_demux, pid );
2056                 i_ret = true;
2057             }
2058         }
2059     }
2060
2061     return i_ret;
2062 }
2063
2064 static int PIDFillFormat( ts_pid_t *pid, int i_stream_type )
2065 {
2066     es_format_t *fmt = &pid->es->fmt;
2067
2068     switch( i_stream_type )
2069     {
2070     case 0x01:  /* MPEG-1 video */
2071     case 0x02:  /* MPEG-2 video */
2072     case 0x80:  /* MPEG-2 MOTO video */
2073         es_format_Init( fmt, VIDEO_ES, VLC_CODEC_MPGV );
2074         break;
2075     case 0x03:  /* MPEG-1 audio */
2076     case 0x04:  /* MPEG-2 audio */
2077         es_format_Init( fmt, AUDIO_ES, VLC_CODEC_MPGA );
2078         break;
2079     case 0x11:  /* MPEG4 (audio) */
2080     case 0x0f:  /* ISO/IEC 13818-7 Audio with ADTS transport syntax */
2081         es_format_Init( fmt, AUDIO_ES, VLC_CODEC_MP4A );
2082         break;
2083     case 0x10:  /* MPEG4 (video) */
2084         es_format_Init( fmt, VIDEO_ES, VLC_CODEC_MP4V );
2085         pid->es->b_gather = true;
2086         break;
2087     case 0x1B:  /* H264 <- check transport syntax/needed descriptor */
2088         es_format_Init( fmt, VIDEO_ES, VLC_CODEC_H264 );
2089         break;
2090
2091     case 0x81:  /* A52 (audio) */
2092         es_format_Init( fmt, AUDIO_ES, VLC_CODEC_A52 );
2093         break;
2094     case 0x82:  /* DVD_SPU (sub) */
2095         es_format_Init( fmt, SPU_ES, VLC_CODEC_SPU );
2096         break;
2097     case 0x83:  /* LPCM (audio) */
2098         es_format_Init( fmt, AUDIO_ES, VLC_CODEC_DVD_LPCM );
2099         break;
2100     case 0x84:  /* SDDS (audio) */
2101         es_format_Init( fmt, AUDIO_ES, VLC_CODEC_SDDS );
2102         break;
2103     case 0x85:  /* DTS (audio) */
2104         es_format_Init( fmt, AUDIO_ES, VLC_CODEC_DTS );
2105         break;
2106     case 0x87: /* E-AC3 */
2107         es_format_Init( fmt, AUDIO_ES, VLC_CODEC_EAC3 );
2108         break;
2109
2110     case 0x91:  /* A52 vls (audio) */
2111         es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', 'b' ) );
2112         break;
2113     case 0x92:  /* DVD_SPU vls (sub) */
2114         es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', 'b' ) );
2115         break;
2116
2117     case 0x94:  /* SDDS (audio) */
2118         es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 'b' ) );
2119         break;
2120
2121     case 0xa0:  /* MSCODEC vlc (video) (fixed later) */
2122         es_format_Init( fmt, UNKNOWN_ES, 0 );
2123         pid->es->b_gather = true;
2124         break;
2125
2126     case 0x06:  /* PES_PRIVATE  (fixed later) */
2127     case 0x12:  /* MPEG-4 generic (sub/scene/...) (fixed later) */
2128     case 0xEA:  /* Privately managed ES (VC-1) (fixed later */
2129     default:
2130         es_format_Init( fmt, UNKNOWN_ES, 0 );
2131         break;
2132     }
2133
2134     /* PES packets usually contain truncated frames */
2135     fmt->b_packetized = false;
2136
2137     return fmt->i_cat == UNKNOWN_ES ? VLC_EGENERIC : VLC_SUCCESS ;
2138 }
2139
2140 /*****************************************************************************
2141  * MP4 specific functions (IOD parser)
2142  *****************************************************************************/
2143 static int  IODDescriptorLength( int *pi_data, uint8_t **pp_data )
2144 {
2145     unsigned int i_b;
2146     unsigned int i_len = 0;
2147     do
2148     {
2149         i_b = **pp_data;
2150         (*pp_data)++;
2151         (*pi_data)--;
2152         i_len = ( i_len << 7 ) + ( i_b&0x7f );
2153
2154     } while( i_b&0x80 );
2155
2156     return( i_len );
2157 }
2158
2159 static int IODGetByte( int *pi_data, uint8_t **pp_data )
2160 {
2161     if( *pi_data > 0 )
2162     {
2163         const int i_b = **pp_data;
2164         (*pp_data)++;
2165         (*pi_data)--;
2166         return( i_b );
2167     }
2168     return( 0 );
2169 }
2170
2171 static int IODGetWord( int *pi_data, uint8_t **pp_data )
2172 {
2173     const int i1 = IODGetByte( pi_data, pp_data );
2174     const int i2 = IODGetByte( pi_data, pp_data );
2175     return( ( i1 << 8 ) | i2 );
2176 }
2177
2178 static int IODGet3Bytes( int *pi_data, uint8_t **pp_data )
2179 {
2180     const int i1 = IODGetByte( pi_data, pp_data );
2181     const int i2 = IODGetByte( pi_data, pp_data );
2182     const int i3 = IODGetByte( pi_data, pp_data );
2183
2184     return( ( i1 << 16 ) | ( i2 << 8) | i3 );
2185 }
2186
2187 static uint32_t IODGetDWord( int *pi_data, uint8_t **pp_data )
2188 {
2189     const uint32_t i1 = IODGetWord( pi_data, pp_data );
2190     const uint32_t i2 = IODGetWord( pi_data, pp_data );
2191     return( ( i1 << 16 ) | i2 );
2192 }
2193
2194 static char* IODGetURL( int *pi_data, uint8_t **pp_data )
2195 {
2196     char *url;
2197     int i_url_len, i;
2198
2199     i_url_len = IODGetByte( pi_data, pp_data );
2200     url = malloc( i_url_len + 1 );
2201     if( !url ) return NULL;
2202     for( i = 0; i < i_url_len; i++ )
2203     {
2204         url[i] = IODGetByte( pi_data, pp_data );
2205     }
2206     url[i_url_len] = '\0';
2207     return( url );
2208 }
2209
2210 static iod_descriptor_t *IODNew( int i_data, uint8_t *p_data )
2211 {
2212     iod_descriptor_t *p_iod;
2213     int i;
2214     int i_es_index;
2215     uint8_t i_flags, i_iod_tag, byte1, byte2, byte3;
2216     bool  b_url;
2217     int   i_iod_length;
2218
2219     p_iod = malloc( sizeof( iod_descriptor_t ) );
2220     if( !p_iod ) return NULL;
2221     memset( p_iod, 0, sizeof( iod_descriptor_t ) );
2222
2223 #ifdef TS_DEBUG
2224     fprintf( stderr, "\n************ IOD ************" );
2225 #endif
2226     for( i = 0; i < 255; i++ )
2227     {
2228         p_iod->es_descr[i].b_ok = 0;
2229     }
2230     i_es_index = 0;
2231
2232     if( i_data < 3 )
2233     {
2234         return p_iod;
2235     }
2236
2237     byte1 = IODGetByte( &i_data, &p_data );
2238     byte2 = IODGetByte( &i_data, &p_data );
2239     byte3 = IODGetByte( &i_data, &p_data );
2240     if( byte2 == 0x02 ) //old vlc's buggy implementation of the IOD_descriptor
2241     {
2242         p_iod->i_iod_label_scope = 0x11;
2243         p_iod->i_iod_label = byte1;
2244         i_iod_tag = byte2;
2245     }
2246     else  //correct implementation of the IOD_descriptor
2247     {
2248         p_iod->i_iod_label_scope = byte1;
2249         p_iod->i_iod_label = byte2;
2250         i_iod_tag = byte3;
2251     }
2252 #ifdef TS_DEBUG
2253     fprintf( stderr, "\n* iod_label:%d", p_iod->i_iod_label );
2254     fprintf( stderr, "\n* ===========" );
2255     fprintf( stderr, "\n* tag:0x%x", i_iod_tag );
2256 #endif
2257     if( i_iod_tag != 0x02 )
2258     {
2259 #ifdef TS_DEBUG
2260         fprintf( stderr, "\n ERR: tag %02x != 0x02", i_iod_tag );
2261 #endif
2262         return p_iod;
2263     }
2264
2265     i_iod_length = IODDescriptorLength( &i_data, &p_data );
2266 #ifdef TS_DEBUG
2267     fprintf( stderr, "\n* length:%d", i_iod_length );
2268 #endif
2269     if( i_iod_length > i_data )
2270     {
2271         i_iod_length = i_data;
2272     }
2273
2274     p_iod->i_od_id = ( IODGetByte( &i_data, &p_data ) << 2 );
2275     i_flags = IODGetByte( &i_data, &p_data );
2276     p_iod->i_od_id |= i_flags >> 6;
2277     b_url = ( i_flags >> 5  )&0x01;
2278 #ifdef TS_DEBUG
2279     fprintf( stderr, "\n* od_id:%d", p_iod->i_od_id );
2280     fprintf( stderr, "\n* url flag:%d", b_url );
2281     fprintf( stderr, "\n* includeInlineProfileLevel flag:%d", ( i_flags >> 4 )&0x01 );
2282 #endif
2283     if( b_url )
2284     {
2285         p_iod->psz_url = IODGetURL( &i_data, &p_data );
2286 #ifdef TS_DEBUG
2287         fprintf( stderr, "\n* url string:%s", p_iod->psz_url );
2288         fprintf( stderr, "\n*****************************\n" );
2289 #endif
2290         return p_iod;
2291     }
2292     else
2293     {
2294         p_iod->psz_url = NULL;
2295     }
2296
2297     p_iod->i_ODProfileLevelIndication = IODGetByte( &i_data, &p_data );
2298     p_iod->i_sceneProfileLevelIndication = IODGetByte( &i_data, &p_data );
2299     p_iod->i_audioProfileLevelIndication = IODGetByte( &i_data, &p_data );
2300     p_iod->i_visualProfileLevelIndication = IODGetByte( &i_data, &p_data );
2301     p_iod->i_graphicsProfileLevelIndication = IODGetByte( &i_data, &p_data );
2302 #ifdef TS_DEBUG
2303     fprintf( stderr, "\n* ODProfileLevelIndication:%d", p_iod->i_ODProfileLevelIndication );
2304     fprintf( stderr, "\n* sceneProfileLevelIndication:%d", p_iod->i_sceneProfileLevelIndication );
2305     fprintf( stderr, "\n* audioProfileLevelIndication:%d", p_iod->i_audioProfileLevelIndication );
2306     fprintf( stderr, "\n* visualProfileLevelIndication:%d", p_iod->i_visualProfileLevelIndication );
2307     fprintf( stderr, "\n* graphicsProfileLevelIndication:%d", p_iod->i_graphicsProfileLevelIndication );
2308 #endif
2309
2310     while( i_data > 0 && i_es_index < 255)
2311     {
2312         int i_tag, i_length;
2313         int     i_data_sav;
2314         uint8_t *p_data_sav;
2315
2316         i_tag = IODGetByte( &i_data, &p_data );
2317         i_length = IODDescriptorLength( &i_data, &p_data );
2318
2319         i_data_sav = i_data;
2320         p_data_sav = p_data;
2321
2322         i_data = i_length;
2323
2324         switch( i_tag )
2325         {
2326         case 0x03:
2327             {
2328 #define es_descr    p_iod->es_descr[i_es_index]
2329                 int i_decoderConfigDescr_length;
2330 #ifdef TS_DEBUG
2331                 fprintf( stderr, "\n* - ES_Descriptor length:%d", i_length );
2332 #endif
2333                 es_descr.b_ok = 1;
2334
2335                 es_descr.i_es_id = IODGetWord( &i_data, &p_data );
2336                 i_flags = IODGetByte( &i_data, &p_data );
2337                 es_descr.b_streamDependenceFlag = ( i_flags >> 7 )&0x01;
2338                 b_url = ( i_flags >> 6 )&0x01;
2339                 es_descr.b_OCRStreamFlag = ( i_flags >> 5 )&0x01;
2340                 es_descr.i_streamPriority = i_flags & 0x1f;
2341 #ifdef TS_DEBUG
2342                 fprintf( stderr, "\n*   * streamDependenceFlag:%d", es_descr.b_streamDependenceFlag );
2343                 fprintf( stderr, "\n*   * OCRStreamFlag:%d", es_descr.b_OCRStreamFlag );
2344                 fprintf( stderr, "\n*   * streamPriority:%d", es_descr.i_streamPriority );
2345 #endif
2346                 if( es_descr.b_streamDependenceFlag )
2347                 {
2348                     es_descr.i_dependOn_es_id = IODGetWord( &i_data, &p_data );
2349 #ifdef TS_DEBUG
2350                     fprintf( stderr, "\n*   * dependOn_es_id:%d", es_descr.i_dependOn_es_id );
2351 #endif
2352                 }
2353
2354                 if( b_url )
2355                 {
2356                     es_descr.psz_url = IODGetURL( &i_data, &p_data );
2357 #ifdef TS_DEBUG
2358                     fprintf( stderr, "\n* url string:%s", es_descr.psz_url );
2359 #endif
2360                 }
2361                 else
2362                 {
2363                     es_descr.psz_url = NULL;
2364                 }
2365
2366                 if( es_descr.b_OCRStreamFlag )
2367                 {
2368                     es_descr.i_OCR_es_id = IODGetWord( &i_data, &p_data );
2369 #ifdef TS_DEBUG
2370                     fprintf( stderr, "\n*   * OCR_es_id:%d", es_descr.i_OCR_es_id );
2371 #endif
2372                 }
2373
2374                 if( IODGetByte( &i_data, &p_data ) != 0x04 )
2375                 {
2376 #ifdef TS_DEBUG
2377                     fprintf( stderr, "\n* ERR missing DecoderConfigDescr" );
2378 #endif
2379                     es_descr.b_ok = 0;
2380                     break;
2381                 }
2382                 i_decoderConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
2383 #ifdef TS_DEBUG
2384                 fprintf( stderr, "\n*   - DecoderConfigDesc length:%d", i_decoderConfigDescr_length );
2385 #endif
2386 #define dec_descr   es_descr.dec_descr
2387                 dec_descr.i_objectTypeIndication = IODGetByte( &i_data, &p_data );
2388                 i_flags = IODGetByte( &i_data, &p_data );
2389                 dec_descr.i_streamType = i_flags >> 2;
2390                 dec_descr.b_upStream = ( i_flags >> 1 )&0x01;
2391                 dec_descr.i_bufferSizeDB = IODGet3Bytes( &i_data, &p_data );
2392                 dec_descr.i_maxBitrate = IODGetDWord( &i_data, &p_data );
2393                 dec_descr.i_avgBitrate = IODGetDWord( &i_data, &p_data );
2394 #ifdef TS_DEBUG
2395                 fprintf( stderr, "\n*     * objectTypeIndication:0x%x", dec_descr.i_objectTypeIndication  );
2396                 fprintf( stderr, "\n*     * streamType:0x%x", dec_descr.i_streamType );
2397                 fprintf( stderr, "\n*     * upStream:%d", dec_descr.b_upStream );
2398                 fprintf( stderr, "\n*     * bufferSizeDB:%d", dec_descr.i_bufferSizeDB );
2399                 fprintf( stderr, "\n*     * maxBitrate:%d", dec_descr.i_maxBitrate );
2400                 fprintf( stderr, "\n*     * avgBitrate:%d", dec_descr.i_avgBitrate );
2401 #endif
2402                 if( i_decoderConfigDescr_length > 13 && IODGetByte( &i_data, &p_data ) == 0x05 )
2403                 {
2404                     int i;
2405                     dec_descr.i_decoder_specific_info_len =
2406                         IODDescriptorLength( &i_data, &p_data );
2407                     if( dec_descr.i_decoder_specific_info_len > 0 )
2408                     {
2409                         dec_descr.p_decoder_specific_info =
2410                             malloc( dec_descr.i_decoder_specific_info_len );
2411                     }
2412                     for( i = 0; i < dec_descr.i_decoder_specific_info_len; i++ )
2413                     {
2414                         dec_descr.p_decoder_specific_info[i] = IODGetByte( &i_data, &p_data );
2415                     }
2416                 }
2417                 else
2418                 {
2419                     dec_descr.i_decoder_specific_info_len = 0;
2420                     dec_descr.p_decoder_specific_info = NULL;
2421                 }
2422             }
2423 #undef  dec_descr
2424 #define sl_descr    es_descr.sl_descr
2425             {
2426                 int i_SLConfigDescr_length;
2427                 int i_predefined;
2428
2429                 if( IODGetByte( &i_data, &p_data ) != 0x06 )
2430                 {
2431 #ifdef TS_DEBUG
2432                     fprintf( stderr, "\n* ERR missing SLConfigDescr" );
2433 #endif
2434                     es_descr.b_ok = 0;
2435                     break;
2436                 }
2437                 i_SLConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
2438 #ifdef TS_DEBUG
2439                 fprintf( stderr, "\n*   - SLConfigDescr length:%d", i_SLConfigDescr_length );
2440 #endif
2441                 i_predefined = IODGetByte( &i_data, &p_data );
2442 #ifdef TS_DEBUG
2443                 fprintf( stderr, "\n*     * i_predefined:0x%x", i_predefined  );
2444 #endif
2445                 switch( i_predefined )
2446                 {
2447                 case 0x01:
2448                     {
2449                         sl_descr.b_useAccessUnitStartFlag   = 0;
2450                         sl_descr.b_useAccessUnitEndFlag     = 0;
2451                         sl_descr.b_useRandomAccessPointFlag = 0;
2452                         //sl_descr.b_useRandomAccessUnitsOnlyFlag = 0;
2453                         sl_descr.b_usePaddingFlag           = 0;
2454                         sl_descr.b_useTimeStampsFlags       = 0;
2455                         sl_descr.b_useIdleFlag              = 0;
2456                         sl_descr.b_durationFlag     = 0;    // FIXME FIXME
2457                         sl_descr.i_timeStampResolution      = 1000;
2458                         sl_descr.i_OCRResolution    = 0;    // FIXME FIXME
2459                         sl_descr.i_timeStampLength          = 32;
2460                         sl_descr.i_OCRLength        = 0;    // FIXME FIXME
2461                         sl_descr.i_AU_Length                = 0;
2462                         sl_descr.i_instantBitrateLength= 0; // FIXME FIXME
2463                         sl_descr.i_degradationPriorityLength= 0;
2464                         sl_descr.i_AU_seqNumLength          = 0;
2465                         sl_descr.i_packetSeqNumLength       = 0;
2466                         if( sl_descr.b_durationFlag )
2467                         {
2468                             sl_descr.i_timeScale            = 0;    // FIXME FIXME
2469                             sl_descr.i_accessUnitDuration   = 0;    // FIXME FIXME
2470                             sl_descr.i_compositionUnitDuration= 0;    // FIXME FIXME
2471                         }
2472                         if( !sl_descr.b_useTimeStampsFlags )
2473                         {
2474                             sl_descr.i_startDecodingTimeStamp   = 0;    // FIXME FIXME
2475                             sl_descr.i_startCompositionTimeStamp= 0;    // FIXME FIXME
2476                         }
2477                     }
2478                     break;
2479                 default:
2480 #ifdef TS_DEBUG
2481                     fprintf( stderr, "\n* ERR unsupported SLConfigDescr predefined" );
2482 #endif
2483                     es_descr.b_ok = 0;
2484                     break;
2485                 }
2486             }
2487             break;
2488 #undef  sl_descr
2489 #undef  es_descr
2490         default:
2491 #ifdef TS_DEBUG
2492             fprintf( stderr, "\n* - OD tag:0x%x length:%d (Unsupported)", i_tag, i_length );
2493 #endif
2494             break;
2495         }
2496
2497         p_data = p_data_sav + i_length;
2498         i_data = i_data_sav - i_length;
2499         i_es_index++;
2500     }
2501 #ifdef TS_DEBUG
2502     fprintf( stderr, "\n*****************************\n" );
2503 #endif
2504     return p_iod;
2505 }
2506
2507 static void IODFree( iod_descriptor_t *p_iod )
2508 {
2509     int i;
2510
2511     if( p_iod->psz_url )
2512     {
2513         free( p_iod->psz_url );
2514         p_iod->psz_url = NULL;
2515         free( p_iod );
2516         return;
2517     }
2518
2519     for( i = 0; i < 255; i++ )
2520     {
2521 #define es_descr p_iod->es_descr[i]
2522         if( es_descr.b_ok )
2523         {
2524             if( es_descr.psz_url )
2525             {
2526                 free( es_descr.psz_url );
2527                 es_descr.psz_url = NULL;
2528             }
2529             else
2530             {
2531                 free( es_descr.dec_descr.p_decoder_specific_info );
2532                 es_descr.dec_descr.p_decoder_specific_info = NULL;
2533                 es_descr.dec_descr.i_decoder_specific_info_len = 0;
2534             }
2535         }
2536         es_descr.b_ok = 0;
2537 #undef  es_descr
2538     }
2539     free( p_iod );
2540 }
2541
2542 /****************************************************************************
2543  ****************************************************************************
2544  ** libdvbpsi callbacks
2545  ****************************************************************************
2546  ****************************************************************************/
2547 static bool ProgramIsSelected( demux_t *p_demux, uint16_t i_pgrm )
2548 {
2549     demux_sys_t          *p_sys = p_demux->p_sys;
2550
2551     if( !p_sys->b_access_control )
2552         return false;
2553     if( ( p_sys->i_current_program == -1 && p_sys->p_programs_list == NULL ) ||
2554         p_sys->i_current_program == 0 )
2555         return true;
2556     if( p_sys->i_current_program == i_pgrm )
2557         return true;
2558
2559     if( p_sys->p_programs_list != NULL )
2560     {
2561         for( int i = 0; i < p_sys->p_programs_list->i_count; i++ )
2562         {
2563             if( i_pgrm == p_sys->p_programs_list->p_values[i].i_int )
2564                 return true;
2565         }
2566     }
2567     return false;
2568 }
2569
2570 static void ValidateDVBMeta( demux_t *p_demux, int i_pid )
2571 {
2572     demux_sys_t *p_sys = p_demux->p_sys;
2573
2574     if( !p_sys->b_dvb_meta || ( i_pid != 0x11 && i_pid != 0x12 && i_pid != 0x14 ) )
2575         return;
2576
2577     msg_Warn( p_demux, "Switching to non DVB mode" );
2578
2579     /* This doesn't look like a DVB stream so don't try
2580      * parsing the SDT/EDT/TDT */
2581
2582     for( int i = 0x11; i <= 0x14; i++ )
2583     {
2584         if( i == 0x13 ) continue;
2585         ts_pid_t *p_pid = &p_sys->pid[i];
2586         if( p_pid->psi )
2587         {
2588             dvbpsi_DetachDemux( p_pid->psi->handle );
2589             free( p_pid->psi );
2590             p_pid->psi = NULL;
2591             p_pid->b_valid = false;
2592         }
2593         if( p_sys->b_access_control )
2594             stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2595                             ACCESS_SET_PRIVATE_ID_STATE, i, false );
2596
2597     }
2598     p_sys->b_dvb_meta = false;
2599 }
2600
2601
2602 #ifdef TS_USE_DVB_SI
2603 /* FIXME same than dvbsi_to_utf8 from dvb access */
2604 static char *EITConvertToUTF8( const unsigned char *psz_instring,
2605                                size_t i_length )
2606 {
2607     const char *psz_encoding;
2608     char *psz_outstring;
2609     char psz_encbuf[sizeof( "ISO_8859-123" )];
2610     size_t i_in, i_out, offset = 1;
2611     vlc_iconv_t iconv_handle;
2612
2613     if( i_length < 1 ) return NULL;
2614     if( psz_instring[0] >= 0x20 )
2615     {
2616         /* According to ETSI EN 300 468 Annex A, this should be ISO6937,
2617          * but some broadcasters use different charset... */
2618         if ( i_broken_epg == 1 )
2619         {
2620            psz_encoding = "ISO_8859-1";
2621         }
2622         else
2623         {
2624            psz_encoding = "ISO_6937";
2625         }
2626
2627         offset = 0;
2628     }
2629     else switch( psz_instring[0] )
2630     {
2631     case 0x01:
2632         psz_encoding = "ISO_8859-5";
2633         break;
2634     case 0x02:
2635         psz_encoding = "ISO_8859-6";
2636         break;
2637     case 0x03:
2638         psz_encoding = "ISO_8859-7";
2639         break;
2640     case 0x04:
2641         psz_encoding = "ISO_8859-8";
2642         break;
2643     case 0x05:
2644         psz_encoding = "ISO_8859-9";
2645         break;
2646     case 0x06:
2647         psz_encoding = "ISO_8859-10";
2648         break;
2649     case 0x07:
2650         psz_encoding = "ISO_8859-11";
2651         break;
2652     case 0x08:
2653         psz_encoding = "ISO_8859-12";
2654         break;
2655     case 0x09:
2656         psz_encoding = "ISO_8859-13";
2657         break;
2658     case 0x0a:
2659         psz_encoding = "ISO_8859-14";
2660         break;
2661     case 0x0b:
2662         psz_encoding = "ISO_8859-15";
2663         break;
2664     case 0x10:
2665 #warning Is Latin-10 (psz_instring[2] == 16) really illegal?
2666         if( i_length < 3 || psz_instring[1] != 0x00 || psz_instring[2] > 15
2667          || psz_instring[2] == 0 )
2668         {
2669             psz_encoding = "UTF-8";
2670             offset = 0;
2671         }
2672         else
2673         {
2674             sprintf( psz_encbuf, "ISO_8859-%u", psz_instring[2] );
2675             psz_encoding = psz_encbuf;
2676             offset = 3;
2677         }
2678         break;
2679     case 0x11:
2680 #warning Is there a BOM or do we use a fixed endianess?
2681         psz_encoding = "UTF-16";
2682         break;
2683     case 0x12:
2684         psz_encoding = "KSC5601-1987";
2685         break;
2686     case 0x13:
2687         psz_encoding = "GB2312"; /* GB-2312-1980 */
2688         break;
2689     case 0x14:
2690         psz_encoding = "BIG-5";
2691         break;
2692     case 0x15:
2693         psz_encoding = "UTF-8";
2694         break;
2695     default:
2696         /* invalid */
2697         psz_encoding = "UTF-8";
2698         offset = 0;
2699     }
2700
2701     i_in = i_length - offset;
2702     i_out = i_in * 6 + 1;
2703
2704     psz_outstring = malloc( i_out );
2705     if( !psz_outstring )
2706     {
2707         return NULL;
2708     }
2709
2710     iconv_handle = vlc_iconv_open( "UTF-8", psz_encoding );
2711     if( iconv_handle == (vlc_iconv_t)(-1) )
2712     {
2713          /* Invalid character set (e.g. ISO_8859-12) */
2714          memcpy( psz_outstring, &psz_instring[offset], i_in );
2715          psz_outstring[i_in] = '\0';
2716          EnsureUTF8( psz_outstring );
2717     }
2718     else
2719     {
2720         const char *psz_in = (const char *)&psz_instring[offset];
2721         char *psz_out = psz_outstring;
2722
2723         while( vlc_iconv( iconv_handle, &psz_in, &i_in,
2724                           &psz_out, &i_out ) == (size_t)(-1) )
2725         {
2726             /* skip naughty byte. This may fail terribly for multibyte stuff,
2727              * but what can we do anyway? */
2728             psz_in++;
2729             i_in--;
2730             vlc_iconv( iconv_handle, NULL, NULL, NULL, NULL ); /* reset */
2731         }
2732         vlc_iconv_close( iconv_handle );
2733
2734         *psz_out = '\0';
2735
2736         /* Convert EIT-coded CR/LFs */
2737         unsigned char *pbuf = (unsigned char *)psz_outstring;
2738         for( ; pbuf < (unsigned char *)psz_out ; pbuf++)
2739         {
2740             if( pbuf[0] == 0xc2 && pbuf[1] == 0x8a )
2741             {
2742                 pbuf[0] = ' ';
2743                 pbuf[1] = '\n';
2744             }
2745         }
2746
2747
2748     }
2749     return psz_outstring;
2750 }
2751
2752 static void SDTCallBack( demux_t *p_demux, dvbpsi_sdt_t *p_sdt )
2753 {
2754     demux_sys_t          *p_sys = p_demux->p_sys;
2755     ts_pid_t             *sdt = &p_sys->pid[0x11];
2756     dvbpsi_sdt_service_t *p_srv;
2757
2758     msg_Dbg( p_demux, "SDTCallBack called" );
2759
2760     if( sdt->psi->i_sdt_version != -1 &&
2761         ( !p_sdt->b_current_next ||
2762           p_sdt->i_version == sdt->psi->i_sdt_version ) )
2763     {
2764         dvbpsi_DeleteSDT( p_sdt );
2765         return;
2766     }
2767
2768     msg_Dbg( p_demux, "new SDT ts_id=%d version=%d current_next=%d "
2769              "network_id=%d",
2770              p_sdt->i_ts_id, p_sdt->i_version, p_sdt->b_current_next,
2771              p_sdt->i_network_id );
2772
2773     i_broken_epg = 0;
2774
2775     for( p_srv = p_sdt->p_first_service; p_srv; p_srv = p_srv->p_next )
2776     {
2777         vlc_meta_t          *p_meta;
2778         dvbpsi_descriptor_t *p_dr;
2779
2780         const char *psz_type = NULL;
2781         const char *psz_status = NULL;
2782
2783         msg_Dbg( p_demux, "  * service id=%d eit schedule=%d present=%d "
2784                  "running=%d free_ca=%d",
2785                  p_srv->i_service_id, p_srv->b_eit_schedule,
2786                  p_srv->b_eit_present, p_srv->i_running_status,
2787                  p_srv->b_free_ca );
2788
2789         if( p_sys->i_current_program != -1 && p_sys->i_current_program != p_srv->i_service_id )
2790             continue;
2791
2792         p_meta = vlc_meta_New();
2793         for( p_dr = p_srv->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
2794         {
2795             if( p_dr->i_tag == 0x48 )
2796             {
2797                 static const char *ppsz_type[17] = {
2798                     "Reserved",
2799                     "Digital television service",
2800                     "Digital radio sound service",
2801                     "Teletext service",
2802                     "NVOD reference service",
2803                     "NVOD time-shifted service",
2804                     "Mosaic service",
2805                     "PAL coded signal",
2806                     "SECAM coded signal",
2807                     "D/D2-MAC",
2808                     "FM Radio",
2809                     "NTSC coded signal",
2810                     "Data broadcast service",
2811                     "Reserved for Common Interface Usage",
2812                     "RCS Map (see EN 301 790 [35])",
2813                     "RCS FLS (see EN 301 790 [35])",
2814                     "DVB MHP service"
2815                 };
2816                 dvbpsi_service_dr_t *pD = dvbpsi_DecodeServiceDr( p_dr );
2817                 char *str1 = NULL;
2818                 char *str2 = NULL;
2819
2820                 /* Workarounds for broadcasters with broken EPG */
2821
2822                 if ( p_sdt->i_network_id == 133 )
2823                    i_broken_epg = 1;  /* SKY DE & BetaDigital use ISO8859-1 */
2824
2825                 if ( (pD->i_service_provider_name_length == 4) &&
2826                      !strncmp(pD->i_service_provider_name, "CSAT", 4) )
2827                    i_broken_epg = 1;  /* CanalSat FR uses ISO8859-1 */
2828
2829                 /* FIXME: Digital+ ES also uses ISO8859-1 */
2830
2831                 str1 = EITConvertToUTF8(pD->i_service_provider_name,
2832                                         pD->i_service_provider_name_length);
2833                 str2 = EITConvertToUTF8(pD->i_service_name,
2834                                         pD->i_service_name_length);
2835
2836                 msg_Dbg( p_demux, "    - type=%d provider=%s name=%s",
2837                          pD->i_service_type, str1, str2 );
2838
2839                 vlc_meta_SetTitle( p_meta, str2 );
2840                 vlc_meta_SetPublisher( p_meta, str1 );
2841                 if( pD->i_service_type >= 0x01 && pD->i_service_type <= 0x10 )
2842                     psz_type = ppsz_type[pD->i_service_type];
2843                 free( str1 );
2844                 free( str2 );
2845             }
2846         }
2847
2848         if( p_srv->i_running_status >= 0x01 && p_srv->i_running_status <= 0x04 )
2849         {
2850             static const char *ppsz_status[5] = {
2851                 "Unknown",
2852                 "Not running",
2853                 "Starts in a few seconds",
2854                 "Pausing",
2855                 "Running"
2856             };
2857             psz_status = ppsz_status[p_srv->i_running_status];
2858         }
2859
2860         if( psz_type )
2861             vlc_meta_AddExtra( p_meta, "Type", psz_type );
2862         if( psz_status )
2863             vlc_meta_AddExtra( p_meta, "Status", psz_status );
2864
2865         es_out_Control( p_demux->out, ES_OUT_SET_GROUP_META,
2866                         p_srv->i_service_id, p_meta );
2867         vlc_meta_Delete( p_meta );
2868     }
2869
2870     sdt->psi->i_sdt_version = p_sdt->i_version;
2871     dvbpsi_DeleteSDT( p_sdt );
2872 }
2873
2874 /* i_year: year - 1900  i_month: 0-11  i_mday: 1-31 i_hour: 0-23 i_minute: 0-59 i_second: 0-59 */
2875 static int64_t vlc_timegm( int i_year, int i_month, int i_mday, int i_hour, int i_minute, int i_second )
2876 {
2877     static const int pn_day[12+1] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
2878     int64_t i_day;
2879     int i;
2880
2881     if( i_year < 70 ||
2882         i_month < 0 || i_month > 11 || i_mday < 1 || i_mday > 31 ||
2883         i_hour < 0 || i_hour > 23 || i_minute < 0 || i_minute > 59 || i_second < 0 || i_second > 59 )
2884         return -1;
2885
2886     /* Count the number of days */
2887     i_day = 365 * (i_year-70) + pn_day[i_month] + i_mday - 1;
2888 #define LEAP(y) ( ((y)%4) == 0 && (((y)%100) != 0 || ((y)%400) == 0) ? 1 : 0)
2889     for( i = 70; i < i_year; i++ )
2890         i_day += LEAP(1900+i);
2891     if( i_month > 1 )
2892         i_day += LEAP(1900+i_year);
2893 #undef LEAP
2894     /**/
2895     return ((24*i_day + i_hour)*60 + i_minute)*60 + i_second;
2896 }
2897
2898 static void EITDecodeMjd( int i_mjd, int *p_y, int *p_m, int *p_d )
2899 {
2900     const int yp = (int)( ( (double)i_mjd - 15078.2)/365.25 );
2901     const int mp = (int)( ((double)i_mjd - 14956.1 - (int)(yp * 365.25)) / 30.6001 );
2902     const int c = ( mp == 14 || mp == 15 ) ? 1 : 0;
2903
2904     *p_y = 1900 + yp + c*1;
2905     *p_m = mp - 1 - c*12;
2906     *p_d = i_mjd - 14956 - (int)(yp*365.25) - (int)(mp*30.6001);
2907 }
2908 #define CVT_FROM_BCD(v) ((((v) >> 4)&0xf)*10 + ((v)&0xf))
2909 static int64_t EITConvertStartTime( uint64_t i_date )
2910 {
2911     const int i_mjd = i_date >> 24;
2912     const int i_hour   = CVT_FROM_BCD(i_date >> 16);
2913     const int i_minute = CVT_FROM_BCD(i_date >>  8);
2914     const int i_second = CVT_FROM_BCD(i_date      );
2915     int i_year;
2916     int i_month;
2917     int i_day;
2918
2919     /* if all 40 bits are 1, the start is unknown */
2920     if( i_date == UINT64_C(0xffffffffff) )
2921         return -1;
2922
2923     EITDecodeMjd( i_mjd, &i_year, &i_month, &i_day );
2924     return vlc_timegm( i_year - 1900, i_month - 1, i_day, i_hour, i_minute, i_second );
2925 }
2926 static int EITConvertDuration( uint32_t i_duration )
2927 {
2928     return CVT_FROM_BCD(i_duration >> 16) * 3600 +
2929            CVT_FROM_BCD(i_duration >> 8 ) * 60 +
2930            CVT_FROM_BCD(i_duration      );
2931 }
2932 #undef CVT_FROM_BCD
2933
2934 #ifdef TS_USE_TDT
2935 static void TDTCallBack( demux_t *p_demux, dvbpsi_tot_t *p_tdt )
2936 {
2937     demux_sys_t        *p_sys = p_demux->p_sys;
2938
2939     p_sys->i_tdt_delta = EITConvertStartTime( p_tdt->i_utc_time ) - time (NULL);
2940     dvbpsi_DeleteTOT(p_tdt);
2941 }
2942 #endif
2943
2944
2945 static void EITCallBack( demux_t *p_demux,
2946                          dvbpsi_eit_t *p_eit, bool b_current_following )
2947 {
2948     demux_sys_t        *p_sys = p_demux->p_sys;
2949     dvbpsi_eit_event_t *p_evt;
2950     vlc_epg_t *p_epg;
2951
2952     msg_Dbg( p_demux, "EITCallBack called" );
2953     if( !p_eit->b_current_next || ( p_sys->i_current_program != -1 && p_sys->i_current_program != p_eit->i_service_id ) )
2954     {
2955         dvbpsi_DeleteEIT( p_eit );
2956         return;
2957     }
2958
2959     msg_Dbg( p_demux, "new EIT service_id=%d version=%d current_next=%d "
2960              "ts_id=%d network_id=%d segment_last_section_number=%d "
2961              "last_table_id=%d",
2962              p_eit->i_service_id, p_eit->i_version, p_eit->b_current_next,
2963              p_eit->i_ts_id, p_eit->i_network_id,
2964              p_eit->i_segment_last_section_number, p_eit->i_last_table_id );
2965
2966     p_epg = vlc_epg_New( NULL );
2967     for( p_evt = p_eit->p_first_event; p_evt; p_evt = p_evt->p_next )
2968     {
2969         dvbpsi_descriptor_t *p_dr;
2970         char                *psz_name = NULL;
2971         char                *psz_text = NULL;
2972         char                *psz_extra = strdup("");
2973         int64_t i_start;
2974         int i_duration;
2975
2976         i_start = EITConvertStartTime( p_evt->i_start_time );
2977         i_duration = EITConvertDuration( p_evt->i_duration );
2978
2979         msg_Dbg( p_demux, "  * event id=%d start_time:%d duration=%d "
2980                           "running=%d free_ca=%d",
2981                  p_evt->i_event_id, (int)i_start, (int)i_duration,
2982                  p_evt->i_running_status, p_evt->b_free_ca );
2983
2984         for( p_dr = p_evt->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
2985         {
2986             if( p_dr->i_tag == 0x4d )
2987             {
2988                 dvbpsi_short_event_dr_t *pE = dvbpsi_DecodeShortEventDr( p_dr );
2989
2990                 if( pE )
2991                 {
2992                     psz_name = EITConvertToUTF8( pE->i_event_name, pE->i_event_name_length);
2993                     psz_text = EITConvertToUTF8( pE->i_text, pE->i_text_length );
2994                     msg_Dbg( p_demux, "    - short event lang=%3.3s '%s' : '%s'",
2995                              pE->i_iso_639_code, psz_name, psz_text );
2996                 }
2997             }
2998             else if( p_dr->i_tag == 0x4e )
2999             {
3000                 dvbpsi_extended_event_dr_t *pE = dvbpsi_DecodeExtendedEventDr( p_dr );
3001                 if( pE )
3002                 {
3003                     msg_Dbg( p_demux, "    - extended event lang=%3.3s [%d/%d]",
3004                              pE->i_iso_639_code,
3005                              pE->i_descriptor_number, pE->i_last_descriptor_number );
3006
3007                     if( pE->i_text_length > 0 )
3008                     {
3009                         char *psz_text = EITConvertToUTF8( pE->i_text, pE->i_text_length );
3010                         if( psz_text )
3011                         {
3012                             msg_Dbg( p_demux, "       - text='%s'", psz_text );
3013
3014                             psz_extra = realloc( psz_extra, strlen(psz_extra) + strlen(psz_text) + 1 );
3015                             strcat( psz_extra, psz_text );
3016                             free( psz_text );
3017                         }
3018                     }
3019
3020                     for( int i = 0; i < pE->i_entry_count; i++ )
3021                     {
3022                         char *psz_dsc = EITConvertToUTF8( pE->i_item_description[i],
3023                                                           pE->i_item_description_length[i] );
3024                         char *psz_itm = EITConvertToUTF8( pE->i_item[i], pE->i_item_length[i] );
3025
3026                         if( psz_dsc && psz_itm )
3027                         {
3028                             msg_Dbg( p_demux, "       - desc='%s' item='%s'", psz_dsc, psz_itm );
3029 #if 0
3030                             psz_extra = realloc( psz_extra, strlen(psz_extra) + strlen(psz_dsc) + strlen(psz_itm) + 3 + 1 );
3031                             strcat( psz_extra, "(" );
3032                             strcat( psz_extra, psz_dsc );
3033                             strcat( psz_extra, " " );
3034                             strcat( psz_extra, psz_itm );
3035                             strcat( psz_extra, ")" );
3036 #endif
3037                         }
3038                         free( psz_dsc );
3039                         free( psz_itm );
3040                     }
3041                 }
3042             }
3043             else
3044             {
3045                 msg_Dbg( p_demux, "    - tag=0x%x(%d)", p_dr->i_tag, p_dr->i_tag );
3046             }
3047         }
3048
3049         /* */
3050         if( i_start > 0 )
3051             vlc_epg_AddEvent( p_epg, i_start, i_duration, psz_name, psz_text,
3052                               *psz_extra ? psz_extra : NULL );
3053
3054         /* Update "now playing" field */
3055         if( p_evt->i_running_status == 0x04 && i_start > 0 )
3056             vlc_epg_SetCurrent( p_epg, i_start );
3057
3058         free( psz_name );
3059         free( psz_text );
3060
3061         free( psz_extra );
3062     }
3063     if( p_epg->i_event > 0 )
3064     {
3065         if( b_current_following &&
3066             (  p_sys->i_current_program == -1 ||
3067                p_sys->i_current_program == p_eit->i_service_id ) )
3068         {
3069             p_sys->i_dvb_length = 0;
3070             p_sys->i_dvb_start = 0;
3071
3072             if( p_epg->p_current )
3073             {
3074                 p_sys->i_dvb_start = p_epg->p_current->i_start;
3075                 p_sys->i_dvb_length = p_epg->p_current->i_duration;
3076             }
3077         }
3078         es_out_Control( p_demux->out, ES_OUT_SET_GROUP_EPG, p_eit->i_service_id, p_epg );
3079     }
3080     vlc_epg_Delete( p_epg );
3081
3082     dvbpsi_DeleteEIT( p_eit );
3083 }
3084 static void EITCallBackCurrentFollowing( demux_t *p_demux, dvbpsi_eit_t *p_eit )
3085 {
3086     EITCallBack( p_demux, p_eit, true );
3087 }
3088 static void EITCallBackSchedule( demux_t *p_demux, dvbpsi_eit_t *p_eit )
3089 {
3090     EITCallBack( p_demux, p_eit, false );
3091 }
3092
3093 static void PSINewTableCallBack( demux_t *p_demux, dvbpsi_handle h,
3094                                  uint8_t  i_table_id, uint16_t i_extension )
3095 {
3096 #if 0
3097     msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3098              i_table_id, i_table_id, i_extension, i_extension );
3099 #endif
3100     if( p_demux->p_sys->pid[0].psi->i_pat_version != -1 && i_table_id == 0x42 )
3101     {
3102         msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3103                  i_table_id, i_table_id, i_extension, i_extension );
3104
3105         dvbpsi_AttachSDT( h, i_table_id, i_extension,
3106                           (dvbpsi_sdt_callback)SDTCallBack, p_demux );
3107     }
3108     else if( p_demux->p_sys->pid[0x11].psi->i_sdt_version != -1 &&
3109              ( i_table_id == 0x4e || /* Current/Following */
3110                (i_table_id >= 0x50 && i_table_id <= 0x5f) ) ) /* Schedule */
3111     {
3112         msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3113                  i_table_id, i_table_id, i_extension, i_extension );
3114
3115         dvbpsi_eit_callback cb = i_table_id == 0x4e ?
3116                                     (dvbpsi_eit_callback)EITCallBackCurrentFollowing :
3117                                     (dvbpsi_eit_callback)EITCallBackSchedule;
3118         dvbpsi_AttachEIT( h, i_table_id, i_extension, cb, p_demux );
3119     }
3120 #ifdef TS_USE_TDT
3121     else if( p_demux->p_sys->pid[0x11].psi->i_sdt_version != -1 &&
3122               i_table_id == 0x73 )  /* TDT */
3123     {
3124          msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3125                  i_table_id, i_table_id, i_extension, i_extension );
3126          dvbpsi_AttachTOT( h, i_table_id, i_extension,
3127                            (dvbpsi_tot_callback)TDTCallBack, p_demux);
3128     }
3129 #endif
3130
3131 }
3132 #endif
3133
3134 /*****************************************************************************
3135  * PMT callback and helpers
3136  *****************************************************************************/
3137 static dvbpsi_descriptor_t *PMTEsFindDescriptor( const dvbpsi_pmt_es_t *p_es,
3138                                                  int i_tag )
3139 {
3140     dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
3141     while( p_dr && ( p_dr->i_tag != i_tag ) )
3142         p_dr = p_dr->p_next;
3143     return p_dr;
3144 }
3145 static bool PMTEsHasRegistration( demux_t *p_demux,
3146                                   const dvbpsi_pmt_es_t *p_es,
3147                                   const char *psz_tag )
3148 {
3149     dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x05 );
3150     if( !p_dr )
3151         return false;
3152
3153     if( p_dr->i_length < 4 )
3154     {
3155         msg_Warn( p_demux, "invalid Registration Descriptor" );
3156         return false;
3157     }
3158
3159     assert( strlen(psz_tag) == 4 );
3160     return !memcmp( p_dr->p_data, psz_tag, 4 );
3161 }
3162 static void PMTSetupEsISO14496( demux_t *p_demux, ts_pid_t *pid,
3163                                 const ts_prg_psi_t *prg, const dvbpsi_pmt_es_t *p_es )
3164 {
3165     es_format_t *p_fmt = &pid->es->fmt;
3166
3167     /* MPEG-4 stream: search SL_DESCRIPTOR */
3168     dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x1f );
3169
3170     if( p_dr && p_dr->i_length == 2 )
3171     {
3172         const int i_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
3173
3174         msg_Warn( p_demux, "found SL_descriptor es_id=%d", i_es_id );
3175
3176         pid->es->p_mpeg4desc = NULL;
3177
3178         for( int i = 0; i < 255; i++ )
3179         {
3180             iod_descriptor_t *iod = prg->iod;
3181
3182             if( iod->es_descr[i].b_ok &&
3183                 iod->es_descr[i].i_es_id == i_es_id )
3184             {
3185                 pid->es->p_mpeg4desc = &iod->es_descr[i];
3186                 break;
3187             }
3188         }
3189     }
3190     if( !pid->es->p_mpeg4desc )
3191     {
3192         msg_Err( p_demux, "MPEG-4 descriptor not found" );
3193         return;
3194     }
3195
3196     const decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;
3197     if( dcd->i_streamType == 0x04 )    /* VisualStream */
3198     {
3199         p_fmt->i_cat = VIDEO_ES;
3200         switch( dcd->i_objectTypeIndication )
3201         {
3202         case 0x0B: /* mpeg4 sub */
3203             p_fmt->i_cat = SPU_ES;
3204             p_fmt->i_codec = VLC_CODEC_SUBT;
3205             break;
3206
3207         case 0x20: /* mpeg4 */
3208             p_fmt->i_codec = VLC_CODEC_MP4V;
3209             break;
3210         case 0x21: /* h264 */
3211             p_fmt->i_codec = VLC_CODEC_H264;
3212             break;
3213         case 0x60:
3214         case 0x61:
3215         case 0x62:
3216         case 0x63:
3217         case 0x64:
3218         case 0x65: /* mpeg2 */
3219             p_fmt->i_codec = VLC_CODEC_MPGV;
3220             break;
3221         case 0x6a: /* mpeg1 */
3222             p_fmt->i_codec = VLC_CODEC_MPGV;
3223             break;
3224         case 0x6c: /* mpeg1 */
3225             p_fmt->i_codec = VLC_CODEC_JPEG;
3226             break;
3227         default:
3228             p_fmt->i_cat = UNKNOWN_ES;
3229             break;
3230         }
3231     }
3232     else if( dcd->i_streamType == 0x05 )    /* AudioStream */
3233     {
3234         p_fmt->i_cat = AUDIO_ES;
3235         switch( dcd->i_objectTypeIndication )
3236         {
3237         case 0x40: /* mpeg4 */
3238             p_fmt->i_codec = VLC_CODEC_MP4A;
3239             break;
3240         case 0x66:
3241         case 0x67:
3242         case 0x68: /* mpeg2 aac */
3243             p_fmt->i_codec = VLC_CODEC_MP4A;
3244             break;
3245         case 0x69: /* mpeg2 */
3246             p_fmt->i_codec = VLC_CODEC_MPGA;
3247             break;
3248         case 0x6b: /* mpeg1 */
3249             p_fmt->i_codec = VLC_CODEC_MPGA;
3250             break;
3251         default:
3252             p_fmt->i_cat = UNKNOWN_ES;
3253             break;
3254         }
3255     }
3256     else
3257     {
3258         p_fmt->i_cat = UNKNOWN_ES;
3259     }
3260
3261     if( p_fmt->i_cat != UNKNOWN_ES )
3262     {
3263         p_fmt->i_extra = dcd->i_decoder_specific_info_len;
3264         if( p_fmt->i_extra > 0 )
3265         {
3266             p_fmt->p_extra = malloc( p_fmt->i_extra );
3267             if( p_fmt->p_extra )
3268                 memcpy( p_fmt->p_extra,
3269                         dcd->p_decoder_specific_info,
3270                         p_fmt->i_extra );
3271             else
3272                 p_fmt->i_extra = 0;
3273         }
3274     }
3275 }
3276
3277 typedef struct
3278 {
3279     int  i_type;
3280     int  i_magazine;
3281     int  i_page;
3282     char p_iso639[3];
3283 } ts_teletext_page_t;
3284
3285 static void PMTSetupEsTeletext( demux_t *p_demux, ts_pid_t *pid,
3286                                 const dvbpsi_pmt_es_t *p_es )
3287 {
3288     es_format_t *p_fmt = &pid->es->fmt;
3289
3290     ts_teletext_page_t p_page[2 * 64 + 20];
3291     unsigned i_page = 0;
3292
3293     /* Gather pages informations */
3294 #if defined _DVBPSI_DR_56_H_ && \
3295     defined DVBPSI_VERSION && DVBPSI_VERSION_INT > ((0<<16)+(1<<8)+5)
3296     for( unsigned i_tag_idx = 0; i_tag_idx < 2; i_tag_idx++ )
3297     {
3298         dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, i_tag_idx == 0 ? 0x46 : 0x56 );
3299         if( !p_dr )
3300             continue;
3301
3302         dvbpsi_teletext_dr_t *p_sub = dvbpsi_DecodeTeletextDr( p_dr );
3303         if( !p_sub )
3304             continue;
3305
3306         for( int i = 0; i < p_sub->i_pages_number; i++ )
3307         {
3308             const dvbpsi_teletextpage_t *p_src = &p_sub->p_pages[i];
3309
3310             if( p_src->i_teletext_type >= 0x06 )
3311                 continue;
3312
3313             assert( i_page < sizeof(p_page)/sizeof(*p_page) );
3314
3315             ts_teletext_page_t *p_dst = &p_page[i_page++];
3316
3317             p_dst->i_type = p_src->i_teletext_type;
3318             p_dst->i_magazine = p_src->i_teletext_magazine_number
3319                 ? p_src->i_teletext_magazine_number : 8;
3320             p_dst->i_page = p_src->i_teletext_page_number;
3321             memcpy( p_dst->p_iso639, p_src->i_iso6392_language_code, 3 );
3322         }
3323     }
3324 #endif
3325
3326 #ifdef _DVBPSI_DR_59_H_
3327     dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x59 );
3328     if( p_dr )
3329     {
3330         dvbpsi_subtitling_dr_t *p_sub = dvbpsi_DecodeSubtitlingDr( p_dr );
3331         for( int i = 0; p_sub && i < p_sub->i_subtitles_number; i++ )
3332         {
3333             dvbpsi_subtitle_t *p_src = &p_sub->p_subtitle[i];
3334
3335             if( p_src->i_subtitling_type < 0x01 || p_src->i_subtitling_type > 0x03 )
3336                 continue;
3337
3338             assert( i_page < sizeof(p_page)/sizeof(*p_page) );
3339
3340             ts_teletext_page_t *p_dst = &p_page[i_page++];
3341
3342             switch( p_src->i_subtitling_type )
3343             {
3344             case 0x01:
3345                 p_dst->i_type = 0x02;
3346                 break;
3347             default:
3348                 p_dst->i_type = 0x03;
3349                 break;
3350             }
3351             /* FIXME check if it is the right split */
3352             p_dst->i_magazine = (p_src->i_composition_page_id >> 8)
3353                 ? (p_src->i_composition_page_id >> 8) : 8;
3354             p_dst->i_page = p_src->i_composition_page_id & 0xff;
3355             memcpy( p_dst->p_iso639, p_src->i_iso6392_language_code, 3 );
3356         }
3357     }
3358 #endif
3359
3360     /* */
3361     es_format_Init( p_fmt, SPU_ES, VLC_CODEC_TELETEXT );
3362
3363     /* In stream output mode, do not separate the stream by page */
3364     if( p_demux->out->b_sout || i_page <= 0 )
3365     {
3366         p_fmt->subs.teletext.i_magazine = -1;
3367         p_fmt->subs.teletext.i_page = 0;
3368         p_fmt->psz_description = strdup( vlc_gettext(ppsz_teletext_type[1]) );
3369
3370         dvbpsi_descriptor_t *p_dr;
3371         p_dr = PMTEsFindDescriptor( p_es, 0x46 );
3372         if( !p_dr )
3373             p_dr = PMTEsFindDescriptor( p_es, 0x56 );
3374
3375         if( p_demux->out->b_sout && p_dr && p_dr->i_length > 0 )
3376         {
3377             /* Descriptor pass-through for sout */
3378             p_fmt->p_extra = malloc( p_dr->i_length );
3379             if( p_fmt->p_extra )
3380             {
3381                 p_fmt->i_extra = p_dr->i_length;
3382                 memcpy( p_fmt->p_extra, p_dr->p_data, p_dr->i_length );
3383             }
3384         }
3385     }
3386     else
3387     {
3388         for( unsigned i = 0; i < i_page; i++ )
3389         {
3390             ts_es_t *p_es;
3391
3392             /* */
3393             if( i == 0 )
3394             {
3395                 p_es = pid->es;
3396             }
3397             else
3398             {
3399                 p_es = malloc( sizeof(*p_es) );
3400                 if( !p_es )
3401                     break;
3402
3403                 es_format_Copy( &p_es->fmt, &pid->es->fmt );
3404                 free( p_es->fmt.psz_language );
3405                 free( p_es->fmt.psz_description );