]> git.sesse.net Git - vlc/blob - modules/demux/ts.c
TS demux: remove unneeded header and add comment
[vlc] / 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 <assert.h>
37
38 #include <vlc_access.h>    /* DVB-specific things */
39 #include <vlc_demux.h>
40 #include <vlc_meta.h>
41 #include <vlc_epg.h>
42 #include <vlc_charset.h>   /* FromCharset, for EIT */
43
44 #include <vlc_network.h>   /* net_ for ts-out mode */
45 #include <vlc_fs.h>        /* vlc_fopen for file-dump mode */
46
47 #include "../mux/mpeg/csa.h"
48
49 /* Include dvbpsi headers */
50 # include <dvbpsi/dvbpsi.h>
51 # include <dvbpsi/demux.h>
52 # include <dvbpsi/descriptor.h>
53 # include <dvbpsi/pat.h>
54 # include <dvbpsi/pmt.h>
55 # include <dvbpsi/sdt.h>
56 # include <dvbpsi/dr.h>
57 # include <dvbpsi/psi.h>
58
59 /* EIT support */
60 # include <dvbpsi/eit.h>
61
62 /* TDT support */
63 #ifdef _DVBPSI_DR_58_H_
64 #   define TS_USE_TDT 1
65 #   include <dvbpsi/tot.h>
66 #else
67 #   include <time.h>
68 #endif
69
70 #undef TS_DEBUG
71
72 /*****************************************************************************
73  * Module descriptor
74  *****************************************************************************/
75 static int  Open  ( vlc_object_t * );
76 static void Close ( vlc_object_t * );
77
78 /* TODO
79  * - Rename "extra pmt" to "user pmt"
80  * - Update extra pmt description
81  *      pmt_pid[:pmt_number][=pid_description[,pid_description]]
82  *      where pid_description could take 3 forms:
83  *          1. pid:pcr (to force the pcr pid)
84  *          2. pid:stream_type
85  *          3. pid:type=fourcc where type=(video|audio|spu)
86  */
87 #define PMT_TEXT N_("Extra PMT")
88 #define PMT_LONGTEXT N_( \
89   "Allows a user to specify an extra pmt (pmt_pid=pid:stream_type[,...])." )
90
91 #define PID_TEXT N_("Set id of ES to PID")
92 #define PID_LONGTEXT N_("Set the internal ID of each elementary stream" \
93                        " handled by VLC to the same value as the PID in" \
94                        " the TS stream, instead of 1, 2, 3, etc. Useful to" \
95                        " do \'#duplicate{..., select=\"es=<pid>\"}\'.")
96
97 #define TSOUT_TEXT N_("Fast udp streaming")
98 #define TSOUT_LONGTEXT N_( \
99   "Sends TS to specific ip:port by udp (you must know what you are doing).")
100
101 #define MTUOUT_TEXT N_("MTU for out mode")
102 #define MTUOUT_LONGTEXT N_("MTU for out mode.")
103
104 #define CSA_TEXT N_("CSA Key")
105 #define CSA_LONGTEXT N_("CSA encryption key. This must be a " \
106   "16 char string (8 hexadecimal bytes).")
107
108 #define CSA2_TEXT N_("Second CSA Key")
109 #define CSA2_LONGTEXT N_("The even CSA encryption key. This must be a " \
110   "16 char string (8 hexadecimal bytes).")
111
112 #define SILENT_TEXT N_("Silent mode")
113 #define SILENT_LONGTEXT N_("Do not complain on encrypted PES.")
114
115 #define CAPMT_SYSID_TEXT N_("CAPMT System ID")
116 #define CAPMT_SYSID_LONGTEXT N_("Only forward descriptors from this SysID to the CAM.")
117
118 #define CPKT_TEXT N_("Packet size in bytes to decrypt")
119 #define CPKT_LONGTEXT N_("Specify the size of the TS packet to decrypt. " \
120     "The decryption routines subtract the TS-header from the value before " \
121     "decrypting. " )
122
123 #define TSDUMP_TEXT N_("Filename of dump")
124 #define TSDUMP_LONGTEXT N_("Specify a filename where to dump the TS in.")
125
126 #define APPEND_TEXT N_("Append")
127 #define APPEND_LONGTEXT N_( \
128     "If the file exists and this option is selected, the existing file " \
129     "will not be overwritten." )
130
131 #define DUMPSIZE_TEXT N_("Dump buffer size")
132 #define DUMPSIZE_LONGTEXT N_( \
133     "Tweak the buffer size for reading and writing an integer number of packets. " \
134     "Specify the size of the buffer here and not the number of packets." )
135
136 #define SPLIT_ES_TEXT N_("Separate sub-streams")
137 #define SPLIT_ES_LONGTEXT N_( \
138     "Separate teletex/dvbs pages into independent ES. " \
139     "It can be useful to turn off this option when using stream output." )
140
141 vlc_module_begin ()
142     set_description( N_("MPEG Transport Stream demuxer") )
143     set_shortname ( "MPEG-TS" )
144     set_category( CAT_INPUT )
145     set_subcategory( SUBCAT_INPUT_DEMUX )
146
147     add_string( "ts-extra-pmt", NULL, PMT_TEXT, PMT_LONGTEXT, true )
148     add_bool( "ts-es-id-pid", true, PID_TEXT, PID_LONGTEXT, true )
149         change_safe()
150     add_string( "ts-out", NULL, TSOUT_TEXT, TSOUT_LONGTEXT, true )
151     add_integer( "ts-out-mtu", 1400, MTUOUT_TEXT,
152                  MTUOUT_LONGTEXT, true )
153
154     add_string( "ts-csa-ck", NULL, CSA_TEXT, CSA_LONGTEXT, true )
155         change_safe()
156     add_string( "ts-csa2-ck", NULL, CSA2_TEXT, CSA2_LONGTEXT, true )
157         change_safe()
158     add_integer( "ts-csa-pkt", 188, CPKT_TEXT, CPKT_LONGTEXT, true )
159         change_safe()
160
161     add_bool( "ts-silent", false, SILENT_TEXT, SILENT_LONGTEXT, true )
162
163     add_savefile( "ts-dump-file", NULL, TSDUMP_TEXT, TSDUMP_LONGTEXT, false )
164     add_bool( "ts-dump-append", false, APPEND_TEXT, APPEND_LONGTEXT, false )
165     add_integer( "ts-dump-size", 16384, DUMPSIZE_TEXT,
166                  DUMPSIZE_LONGTEXT, true )
167     add_bool( "ts-split-es", true, SPLIT_ES_TEXT, SPLIT_ES_LONGTEXT, false )
168
169     set_capability( "demux", 10 )
170     set_callbacks( Open, Close )
171     add_shortcut( "ts" )
172 vlc_module_end ()
173
174 /*****************************************************************************
175  * Local prototypes
176  *****************************************************************************/
177 static const char *const ppsz_teletext_type[] = {
178  "",
179  N_("Teletext"),
180  N_("Teletext subtitles"),
181  N_("Teletext: additional information"),
182  N_("Teletext: program schedule"),
183  N_("Teletext subtitles: hearing impaired")
184 };
185
186 typedef struct
187 {
188     uint8_t                 i_objectTypeIndication;
189     uint8_t                 i_streamType;
190     bool                    b_upStream;
191     uint32_t                i_bufferSizeDB;
192     uint32_t                i_maxBitrate;
193     uint32_t                i_avgBitrate;
194
195     int                     i_decoder_specific_info_len;
196     uint8_t                 *p_decoder_specific_info;
197
198 } decoder_config_descriptor_t;
199
200 typedef struct
201 {
202     bool                    b_useAccessUnitStartFlag;
203     bool                    b_useAccessUnitEndFlag;
204     bool                    b_useRandomAccessPointFlag;
205     bool                    b_useRandomAccessUnitsOnlyFlag;
206     bool                    b_usePaddingFlag;
207     bool                    b_useTimeStampsFlags;
208     bool                    b_useIdleFlag;
209     bool                    b_durationFlag;
210     uint32_t                i_timeStampResolution;
211     uint32_t                i_OCRResolution;
212     uint8_t                 i_timeStampLength;
213     uint8_t                 i_OCRLength;
214     uint8_t                 i_AU_Length;
215     uint8_t                 i_instantBitrateLength;
216     uint8_t                 i_degradationPriorityLength;
217     uint8_t                 i_AU_seqNumLength;
218     uint8_t                 i_packetSeqNumLength;
219
220     uint32_t                i_timeScale;
221     uint16_t                i_accessUnitDuration;
222     uint16_t                i_compositionUnitDuration;
223
224     uint64_t                i_startDecodingTimeStamp;
225     uint64_t                i_startCompositionTimeStamp;
226
227 } sl_config_descriptor_t;
228
229 typedef struct
230 {
231     bool                    b_ok;
232     uint16_t                i_es_id;
233
234     bool                    b_streamDependenceFlag;
235     bool                    b_OCRStreamFlag;
236     uint8_t                 i_streamPriority;
237
238     char                    *psz_url;
239
240     uint16_t                i_dependOn_es_id;
241     uint16_t                i_OCR_es_id;
242
243     decoder_config_descriptor_t    dec_descr;
244     sl_config_descriptor_t         sl_descr;
245
246 } es_mpeg4_descriptor_t;
247
248 typedef struct
249 {
250     uint8_t                 i_iod_label, i_iod_label_scope;
251
252     /* IOD */
253     uint16_t                i_od_id;
254     char                    *psz_url;
255
256     uint8_t                 i_ODProfileLevelIndication;
257     uint8_t                 i_sceneProfileLevelIndication;
258     uint8_t                 i_audioProfileLevelIndication;
259     uint8_t                 i_visualProfileLevelIndication;
260     uint8_t                 i_graphicsProfileLevelIndication;
261
262     es_mpeg4_descriptor_t   es_descr[255];
263
264 } iod_descriptor_t;
265
266 typedef struct
267 {
268     dvbpsi_handle   handle;
269
270     int             i_version;
271     int             i_number;
272     int             i_pid_pcr;
273     int             i_pid_pmt;
274     /* IOD stuff (mpeg4) */
275     iod_descriptor_t *iod;
276
277 } ts_prg_psi_t;
278
279 typedef struct
280 {
281     /* for special PAT/SDT case */
282     dvbpsi_handle   handle; /* PAT/SDT/EIT */
283     int             i_pat_version;
284     int             i_sdt_version;
285
286     /* For PMT */
287     int             i_prg;
288     ts_prg_psi_t    **prg;
289
290 } ts_psi_t;
291
292 typedef struct
293 {
294     es_format_t  fmt;
295     es_out_id_t *id;
296     int         i_pes_size;
297     int         i_pes_gathered;
298     block_t     *p_pes;
299     block_t     **pp_last;
300
301     es_mpeg4_descriptor_t *p_mpeg4desc;
302     int         b_gather;
303
304 } ts_es_t;
305
306 typedef struct
307 {
308     int         i_pid;
309
310     bool        b_seen;
311     bool        b_valid;
312     int         i_cc;   /* countinuity counter */
313     bool        b_scrambled;
314
315     /* PSI owner (ie PMT -> PAT, ES -> PMT */
316     ts_psi_t   *p_owner;
317     int         i_owner_number;
318
319     /* */
320     ts_psi_t    *psi;
321     ts_es_t     *es;
322
323     /* Some private streams encapsulate several ES (eg. DVB subtitles)*/
324     ts_es_t     **extra_es;
325     int         i_extra_es;
326
327 } ts_pid_t;
328
329 struct demux_sys_t
330 {
331     vlc_mutex_t     csa_lock;
332
333     /* TS packet size (188, 192, 204) */
334     int         i_packet_size;
335
336     /* how many TS packet we read at once */
337     int         i_ts_read;
338
339     /* All pid */
340     ts_pid_t    pid[8192];
341
342     /* All PMT */
343     bool        b_user_pmt;
344     int         i_pmt;
345     ts_pid_t    **pmt;
346     int         i_pmt_es;
347
348     /* */
349     bool        b_es_id_pid;
350     csa_t       *csa;
351     int         i_csa_pkt_size;
352     bool        b_silent;
353     bool        b_split_es;
354
355     bool        b_udp_out;
356     int         fd; /* udp socket */
357     uint8_t     *buffer;
358
359     /* */
360     bool        b_access_control;
361
362     /* */
363     bool        b_dvb_meta;
364     int64_t     i_tdt_delta;
365     int64_t     i_dvb_start;
366     int64_t     i_dvb_length;
367     bool        b_broken_charset; /* True if broken encoding is used in EPG/SDT */
368
369     /* */
370     int         i_current_program;
371     vlc_list_t  programs_list;
372
373     /* TS dump */
374     char        *psz_file;  /* file to dump data in */
375     FILE        *p_file;    /* filehandle */
376     uint64_t    i_write;    /* bytes written */
377     bool        b_file_out; /* dump mode enabled */
378
379     /* */
380     bool        b_start_record;
381 };
382
383 static int Demux    ( demux_t *p_demux );
384 static int DemuxFile( demux_t *p_demux );
385 static int Control( demux_t *p_demux, int i_query, va_list args );
386
387 static void PIDInit ( ts_pid_t *pid, bool b_psi, ts_psi_t *p_owner );
388 static void PIDClean( demux_t *, ts_pid_t *pid );
389 static int  PIDFillFormat( ts_pid_t *pid, int i_stream_type );
390
391 static void PATCallBack( demux_t *, dvbpsi_pat_t * );
392 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt );
393 static void PSINewTableCallBack( demux_t *, dvbpsi_handle,
394                                  uint8_t  i_table_id, uint16_t i_extension );
395 static int ChangeKeyCallback( vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void * );
396
397 static inline int PIDGet( block_t *p )
398 {
399     return ( (p->p_buffer[1]&0x1f)<<8 )|p->p_buffer[2];
400 }
401
402 static bool GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk );
403
404 static void PCRHandle( demux_t *p_demux, ts_pid_t *, block_t * );
405
406 static iod_descriptor_t *IODNew( int , uint8_t * );
407 static void              IODFree( iod_descriptor_t * );
408
409 #define TS_USER_PMT_NUMBER (0)
410 static int UserPmt( demux_t *p_demux, const char * );
411
412 static int  SetPIDFilter( demux_t *, int i_pid, bool b_selected );
413 static void SetPrgFilter( demux_t *, int i_prg, bool b_selected );
414
415 #define TS_PACKET_SIZE_188 188
416 #define TS_PACKET_SIZE_192 192
417 #define TS_PACKET_SIZE_204 204
418 #define TS_PACKET_SIZE_MAX 204
419 #define TS_TOPFIELD_HEADER 1320
420
421 /*****************************************************************************
422  * Open
423  *****************************************************************************/
424 static int Open( vlc_object_t *p_this )
425 {
426     demux_t     *p_demux = (demux_t*)p_this;
427     demux_sys_t *p_sys;
428
429     const uint8_t *p_peek;
430     int          i_sync, i_peek, i;
431     int          i_packet_size;
432
433     ts_pid_t    *pat;
434     const char  *psz_mode;
435     bool         b_append;
436     bool         b_topfield = false;
437
438     if( stream_Peek( p_demux->s, &p_peek, TS_PACKET_SIZE_MAX ) <
439         TS_PACKET_SIZE_MAX ) return VLC_EGENERIC;
440
441     if( memcmp( p_peek, "TFrc", 4 ) == 0 )
442     {
443         b_topfield = true;
444         msg_Dbg( p_demux, "this is a topfield file" );
445     }
446
447     /* Search first sync byte */
448     for( i_sync = 0; i_sync < TS_PACKET_SIZE_MAX; i_sync++ )
449     {
450         if( p_peek[i_sync] == 0x47 ) break;
451     }
452     if( i_sync >= TS_PACKET_SIZE_MAX && !b_topfield )
453     {
454         if( !p_demux->b_force )
455             return VLC_EGENERIC;
456         msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
457     }
458
459     if( b_topfield )
460     {
461         /* Read the entire Topfield header */
462         i_peek = TS_TOPFIELD_HEADER;
463     }
464     else
465     {
466         /* Check next 3 sync bytes */
467         i_peek = TS_PACKET_SIZE_MAX * 3 + i_sync + 1;
468     }
469
470     if( ( stream_Peek( p_demux->s, &p_peek, i_peek ) ) < i_peek )
471     {
472         msg_Err( p_demux, "cannot peek" );
473         return VLC_EGENERIC;
474     }
475     if( p_peek[i_sync + TS_PACKET_SIZE_188] == 0x47 &&
476         p_peek[i_sync + 2 * TS_PACKET_SIZE_188] == 0x47 &&
477         p_peek[i_sync + 3 * TS_PACKET_SIZE_188] == 0x47 )
478     {
479         i_packet_size = TS_PACKET_SIZE_188;
480     }
481     else if( p_peek[i_sync + TS_PACKET_SIZE_192] == 0x47 &&
482              p_peek[i_sync + 2 * TS_PACKET_SIZE_192] == 0x47 &&
483              p_peek[i_sync + 3 * TS_PACKET_SIZE_192] == 0x47 )
484     {
485         i_packet_size = TS_PACKET_SIZE_192;
486     }
487     else if( p_peek[i_sync + TS_PACKET_SIZE_204] == 0x47 &&
488              p_peek[i_sync + 2 * TS_PACKET_SIZE_204] == 0x47 &&
489              p_peek[i_sync + 3 * TS_PACKET_SIZE_204] == 0x47 )
490     {
491         i_packet_size = TS_PACKET_SIZE_204;
492     }
493     else if( p_demux->b_force )
494     {
495         i_packet_size = TS_PACKET_SIZE_188;
496     }
497     else if( b_topfield )
498     {
499         i_packet_size = TS_PACKET_SIZE_188;
500 #if 0
501         /* I used the TF5000PVR 2004 Firmware .doc header documentation,
502          * http://www.i-topfield.com/data/product/firmware/Structure%20of%20Recorded%20File%20in%20TF5000PVR%20(Feb%2021%202004).doc
503          * but after the filename the offsets seem to be incorrect.  - DJ */
504         int i_duration, i_name;
505         char *psz_name = xmalloc(25);
506         char *psz_event_name;
507         char *psz_event_text = xmalloc(130);
508         char *psz_ext_text = xmalloc(1025);
509
510         // 2 bytes version Uimsbf (4,5)
511         // 2 bytes reserved (6,7)
512         // 2 bytes duration in minutes Uimsbf (8,9(
513         i_duration = (int) (p_peek[8] << 8) | p_peek[9];
514         msg_Dbg( p_demux, "Topfield recording length: +/- %d minutes", i_duration);
515         // 2 bytes service number in channel list (10, 11)
516         // 2 bytes service type Bslbf 0=TV 1=Radio Bslb (12, 13)
517         // 4 bytes of reserved + tuner info (14,15,16,17)
518         // 2 bytes of Service ID  Bslbf (18,19)
519         // 2 bytes of PMT PID  Uimsbf (20,21)
520         // 2 bytes of PCR PID  Uimsbf (22,23)
521         // 2 bytes of Video PID  Uimsbf (24,25)
522         // 2 bytes of Audio PID  Uimsbf (26,27)
523         // 24 bytes filename Bslbf
524         memcpy( psz_name, &p_peek[28], 24 );
525         psz_name[24] = '\0';
526         msg_Dbg( p_demux, "recordingname=%s", psz_name );
527         // 1 byte of sat index Uimsbf  (52)
528         // 3 bytes (1 bit of polarity Bslbf +23 bits reserved)
529         // 4 bytes of freq. Uimsbf (56,57,58,59)
530         // 2 bytes of symbol rate Uimsbf (60,61)
531         // 2 bytes of TS stream ID Uimsbf (62,63)
532         // 4 bytes reserved
533         // 2 bytes reserved
534         // 2 bytes duration Uimsbf (70,71)
535         //i_duration = (int) (p_peek[70] << 8) | p_peek[71];
536         //msg_Dbg( p_demux, "Topfield 2nd duration field: +/- %d minutes", i_duration);
537         // 4 bytes EventID Uimsbf (72-75)
538         // 8 bytes of Start and End time info (76-83)
539         // 1 byte reserved (84)
540         // 1 byte event name length Uimsbf (89)
541         i_name = (int)(p_peek[89]&~0x81);
542         msg_Dbg( p_demux, "event name length = %d", i_name);
543         psz_event_name = xmalloc( i_name+1 );
544         // 1 byte parental rating (90)
545         // 129 bytes of event text
546         memcpy( psz_event_name, &p_peek[91], i_name );
547         psz_event_name[i_name] = '\0';
548         memcpy( psz_event_text, &p_peek[91+i_name], 129-i_name );
549         psz_event_text[129-i_name] = '\0';
550         msg_Dbg( p_demux, "event name=%s", psz_event_name );
551         msg_Dbg( p_demux, "event text=%s", psz_event_text );
552         // 12 bytes reserved (220)
553         // 6 bytes reserved
554         // 2 bytes Event Text Length Uimsbf
555         // 4 bytes EventID Uimsbf
556         // FIXME We just have 613 bytes. not enough for this entire text
557         // 1024 bytes Extended Event Text Bslbf
558         memcpy( psz_ext_text, p_peek+372, 1024 );
559         psz_ext_text[1024] = '\0';
560         msg_Dbg( p_demux, "extended event text=%s", psz_ext_text );
561         // 52 bytes reserved Bslbf
562 #endif
563     }
564     else
565     {
566         msg_Warn( p_demux, "TS module discarded (lost sync)" );
567         return VLC_EGENERIC;
568     }
569
570     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
571     if( !p_sys )
572         return VLC_ENOMEM;
573     memset( p_sys, 0, sizeof( demux_sys_t ) );
574     p_sys->i_packet_size = i_packet_size;
575     vlc_mutex_init( &p_sys->csa_lock );
576
577     /* Fill dump mode fields */
578     p_sys->i_write = 0;
579     p_sys->buffer = NULL;
580     p_sys->p_file = NULL;
581     p_sys->b_file_out = false;
582     p_sys->psz_file = var_CreateGetString( p_demux, "ts-dump-file" );
583     if( *p_sys->psz_file != '\0' )
584     {
585         p_sys->b_file_out = true;
586
587         b_append = var_CreateGetBool( p_demux, "ts-dump-append" );
588         if ( b_append )
589             psz_mode = "ab";
590         else
591             psz_mode = "wb";
592
593         if( !strcmp( p_sys->psz_file, "-" ) )
594         {
595             msg_Info( p_demux, "dumping raw stream to standard output" );
596             p_sys->p_file = stdout;
597         }
598         else if( ( p_sys->p_file = vlc_fopen( p_sys->psz_file, psz_mode ) ) == NULL )
599         {
600             msg_Err( p_demux, "cannot create `%s' for writing", p_sys->psz_file );
601             p_sys->b_file_out = false;
602         }
603
604         if( p_sys->b_file_out )
605         {
606             /* Determine how many packets to read. */
607             int bufsize = var_CreateGetInteger( p_demux, "ts-dump-size" );
608             p_sys->i_ts_read = (int) (bufsize / p_sys->i_packet_size);
609             if( p_sys->i_ts_read <= 0 )
610             {
611                 p_sys->i_ts_read = 1500 / p_sys->i_packet_size;
612             }
613             p_sys->buffer = xmalloc( p_sys->i_packet_size * p_sys->i_ts_read );
614             msg_Info( p_demux, "%s raw stream to file `%s' reading packets %d",
615                       b_append ? "appending" : "dumping", p_sys->psz_file,
616                       p_sys->i_ts_read );
617         }
618     }
619
620     /* Fill p_demux field */
621     if( p_sys->b_file_out )
622         p_demux->pf_demux = DemuxFile;
623     else
624         p_demux->pf_demux = Demux;
625     p_demux->pf_control = Control;
626
627     /* Init p_sys field */
628     p_sys->b_dvb_meta = true;
629     p_sys->b_access_control = true;
630     p_sys->i_current_program = 0;
631     p_sys->programs_list.i_count = 0;
632     p_sys->programs_list.p_values = NULL;
633     p_sys->i_tdt_delta = 0;
634     p_sys->i_dvb_start = 0;
635     p_sys->i_dvb_length = 0;
636
637     p_sys->b_broken_charset = false;
638
639     for( i = 0; i < 8192; i++ )
640     {
641         ts_pid_t *pid = &p_sys->pid[i];
642
643         pid->i_pid      = i;
644         pid->b_seen     = false;
645         pid->b_valid    = false;
646     }
647     /* PID 8191 is padding */
648     p_sys->pid[8191].b_seen = true;
649     p_sys->i_packet_size = i_packet_size;
650     p_sys->b_udp_out = false;
651     p_sys->fd = -1;
652     p_sys->i_ts_read = 50;
653     p_sys->csa = NULL;
654     p_sys->b_start_record = false;
655
656     /* Init PAT handler */
657     pat = &p_sys->pid[0];
658     PIDInit( pat, true, NULL );
659     pat->psi->handle = dvbpsi_AttachPAT( (dvbpsi_pat_callback)PATCallBack,
660                                          p_demux );
661     if( p_sys->b_dvb_meta )
662     {
663         ts_pid_t *sdt = &p_sys->pid[0x11];
664         ts_pid_t *eit = &p_sys->pid[0x12];
665
666         PIDInit( sdt, true, NULL );
667         sdt->psi->handle =
668             dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
669                                 p_demux );
670         PIDInit( eit, true, NULL );
671         eit->psi->handle =
672             dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
673                                 p_demux );
674 #ifdef TS_USE_TDT
675         ts_pid_t *tdt = &p_sys->pid[0x14];
676         PIDInit( tdt, true, NULL );
677         tdt->psi->handle =
678             dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
679                                 p_demux );
680 #endif
681         if( p_sys->b_access_control )
682         {
683             if( SetPIDFilter( p_demux, 0x11, true ) ||
684 #ifdef TS_USE_TDT
685                 SetPIDFilter( p_demux, 0x14, true ) ||
686 #endif
687                 SetPIDFilter( p_demux, 0x12, true ) )
688                 p_sys->b_access_control = false;
689         }
690     }
691
692     /* Init PMT array */
693     TAB_INIT( p_sys->i_pmt, p_sys->pmt );
694     p_sys->i_pmt_es = 0;
695
696     /* Read config */
697     p_sys->b_es_id_pid = var_CreateGetBool( p_demux, "ts-es-id-pid" );
698
699     char* psz_string = var_CreateGetString( p_demux, "ts-out" );
700     if( psz_string && *psz_string && !p_sys->b_file_out )
701     {
702         char *psz = strchr( psz_string, ':' );
703         int   i_port = 0;
704
705         p_sys->b_udp_out = true;
706
707         if( psz )
708         {
709             *psz++ = '\0';
710             i_port = atoi( psz );
711         }
712         if( i_port <= 0 ) i_port  = 1234;
713         msg_Dbg( p_demux, "resend ts to '%s:%d'", psz_string, i_port );
714
715         p_sys->fd = net_ConnectUDP( VLC_OBJECT(p_demux), psz_string, i_port, -1 );
716         if( p_sys->fd < 0 )
717         {
718             msg_Err( p_demux, "failed to open udp socket, send disabled" );
719             p_sys->b_udp_out = false;
720         }
721         else
722         {
723             int i_mtu = var_CreateGetInteger( p_demux, "ts-out-mtu" );
724             p_sys->i_ts_read = i_mtu / p_sys->i_packet_size;
725             if( p_sys->i_ts_read <= 0 )
726             {
727                 p_sys->i_ts_read = 1500 / p_sys->i_packet_size;
728             }
729             p_sys->buffer = xmalloc( p_sys->i_packet_size * p_sys->i_ts_read );
730         }
731     }
732     free( psz_string );
733
734     /* We handle description of an extra PMT */
735     psz_string = var_CreateGetString( p_demux, "ts-extra-pmt" );
736     p_sys->b_user_pmt = false;
737     if( psz_string && *psz_string )
738         UserPmt( p_demux, psz_string );
739     free( psz_string );
740
741     psz_string = var_CreateGetStringCommand( p_demux, "ts-csa-ck" );
742     if( psz_string && *psz_string )
743     {
744         int i_res;
745         char* psz_csa2;
746
747         p_sys->csa = csa_New();
748
749         psz_csa2 = var_CreateGetStringCommand( p_demux, "ts-csa2-ck" );
750         i_res = csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, true );
751         if( i_res == VLC_SUCCESS && psz_csa2 && *psz_csa2 )
752         {
753             if( csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_csa2, false ) != VLC_SUCCESS )
754             {
755                 csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, false );
756             }
757         }
758         else if ( i_res == VLC_SUCCESS )
759         {
760             csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, false );
761         }
762         else
763         {
764             csa_Delete( p_sys->csa );
765             p_sys->csa = NULL;
766         }
767
768         if( p_sys->csa )
769         {
770             var_AddCallback( p_demux, "ts-csa-ck", ChangeKeyCallback, (void *)1 );
771             var_AddCallback( p_demux, "ts-csa2-ck", ChangeKeyCallback, NULL );
772
773             int i_pkt = var_CreateGetInteger( p_demux, "ts-csa-pkt" );
774             if( i_pkt < 4 || i_pkt > 188 )
775             {
776                 msg_Err( p_demux, "wrong packet size %d specified.", i_pkt );
777                 msg_Warn( p_demux, "using default packet size of 188 bytes" );
778                 p_sys->i_csa_pkt_size = 188;
779             }
780             else
781                 p_sys->i_csa_pkt_size = i_pkt;
782             msg_Dbg( p_demux, "decrypting %d bytes of packet", p_sys->i_csa_pkt_size );
783         }
784         free( psz_csa2 );
785     }
786     free( psz_string );
787
788     p_sys->b_silent = var_CreateGetBool( p_demux, "ts-silent" );
789     p_sys->b_split_es = var_InheritBool( p_demux, "ts-split-es" );
790
791     while( !p_sys->b_file_out && p_sys->i_pmt_es <= 0 &&
792            vlc_object_alive( p_demux ) )
793     {
794         if( p_demux->pf_demux( p_demux ) != 1 )
795             break;
796     }
797
798
799     return VLC_SUCCESS;
800 }
801
802 /*****************************************************************************
803  * Close
804  *****************************************************************************/
805 static void Close( vlc_object_t *p_this )
806 {
807     demux_t     *p_demux = (demux_t*)p_this;
808     demux_sys_t *p_sys = p_demux->p_sys;
809
810     msg_Dbg( p_demux, "pid list:" );
811     for( int i = 0; i < 8192; i++ )
812     {
813         ts_pid_t *pid = &p_sys->pid[i];
814
815         if( pid->b_valid && pid->psi )
816         {
817             switch( pid->i_pid )
818             {
819             case 0: /* PAT */
820                 dvbpsi_DetachPAT( pid->psi->handle );
821                 free( pid->psi );
822                 break;
823             case 1: /* CAT */
824                 free( pid->psi );
825                 break;
826             default:
827                 if( p_sys->b_dvb_meta && ( pid->i_pid == 0x11 || pid->i_pid == 0x12 || pid->i_pid == 0x14 ) )
828                 {
829                     /* SDT or EIT or TDT */
830                     dvbpsi_DetachDemux( pid->psi->handle );
831                     free( pid->psi );
832                 }
833                 else
834                 {
835                     PIDClean( p_demux, pid );
836                 }
837                 break;
838             }
839         }
840         else if( pid->b_valid && pid->es )
841         {
842             PIDClean( p_demux, pid );
843         }
844
845         if( pid->b_seen )
846         {
847             msg_Dbg( p_demux, "  - pid[%d] seen", pid->i_pid );
848         }
849
850         /* too much */
851         if( pid->i_pid > 0 )
852             SetPIDFilter( p_demux, pid->i_pid, false );
853     }
854
855     vlc_mutex_lock( &p_sys->csa_lock );
856     if( p_sys->csa )
857     {
858         var_DelCallback( p_demux, "ts-csa-ck", ChangeKeyCallback, NULL );
859         var_DelCallback( p_demux, "ts-csa2-ck", ChangeKeyCallback, NULL );
860         csa_Delete( p_sys->csa );
861     }
862     vlc_mutex_unlock( &p_sys->csa_lock );
863
864     TAB_CLEAN( p_sys->i_pmt, p_sys->pmt );
865
866     free( p_sys->programs_list.p_values );
867
868     /* If in dump mode, then close the file */
869     if( p_sys->b_file_out )
870     {
871         msg_Info( p_demux ,"closing %s (%"PRId64" KiB dumped)",
872                   p_sys->psz_file, p_sys->i_write / 1024 );
873
874         if( p_sys->p_file != stdout )
875         {
876             fclose( p_sys->p_file );
877         }
878     }
879     /* When streaming, close the port */
880     if( p_sys->fd > -1 )
881     {
882         net_Close( p_sys->fd );
883     }
884
885     free( p_sys->buffer );
886     free( p_sys->psz_file );
887
888     vlc_mutex_destroy( &p_sys->csa_lock );
889     free( p_sys );
890 }
891
892 /*****************************************************************************
893  * ChangeKeyCallback: called when changing the odd encryption key on the fly.
894  *****************************************************************************/
895 static int ChangeKeyCallback( vlc_object_t *p_this, char const *psz_cmd,
896                            vlc_value_t oldval, vlc_value_t newval,
897                            void *p_data )
898 {
899     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
900     demux_t     *p_demux = (demux_t*)p_this;
901     demux_sys_t *p_sys = p_demux->p_sys;
902     int         i_tmp = (intptr_t)p_data;
903
904     vlc_mutex_lock( &p_sys->csa_lock );
905     if ( i_tmp )
906         i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, true );
907     else
908         i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, false );
909
910     vlc_mutex_unlock( &p_sys->csa_lock );
911     return i_tmp;
912 }
913
914 /*****************************************************************************
915  * DemuxFile:
916  *****************************************************************************/
917 static int DemuxFile( demux_t *p_demux )
918 {
919     demux_sys_t *p_sys = p_demux->p_sys;
920     const int i_bufsize = p_sys->i_packet_size * p_sys->i_ts_read;
921     uint8_t   *p_buffer = p_sys->buffer; /* Put first on sync byte */
922     const int i_data = stream_Read( p_demux->s, p_sys->buffer, i_bufsize );
923
924     if( i_data <= 0 && i_data < p_sys->i_packet_size )
925     {
926         msg_Dbg( p_demux, "error reading malformed packets" );
927         return i_data;
928     }
929
930     /* Test continuity counter */
931     for( int i_pos = 0; i_pos < i_data;  )
932     {
933         if( p_sys->buffer[i_pos] != 0x47 )
934         {
935             msg_Warn( p_demux, "lost sync" );
936             while( vlc_object_alive (p_demux) && (i_pos < i_data) )
937             {
938                 i_pos++;
939                 if( p_sys->buffer[i_pos] == 0x47 )
940                     break;
941             }
942             if( vlc_object_alive (p_demux) )
943                 msg_Warn( p_demux, "sync found" );
944         }
945
946         /* continuous when (one of this):
947          * diff == 1
948          * diff == 0 and payload == 0
949          * diff == 0 and duplicate packet (playload != 0) <- should we
950          *   test the content ?
951          */
952         const int i_cc  = p_buffer[i_pos+3]&0x0f;
953         const bool b_payload = p_buffer[i_pos+3]&0x10;
954         const bool b_adaptation = p_buffer[i_pos+3]&0x20;
955
956         /* Get the PID */
957         ts_pid_t *p_pid = &p_sys->pid[ ((p_buffer[i_pos+1]&0x1f)<<8)|p_buffer[i_pos+2] ];
958
959         /* Detect discontinuity indicator in adaptation field */
960         if( b_adaptation && p_buffer[i_pos + 4] > 0 )
961         {
962             if( p_buffer[i_pos+5]&0x80 )
963                 msg_Warn( p_demux, "discontinuity indicator (pid=%d) ", p_pid->i_pid );
964             if( p_buffer[i_pos+5]&0x40 )
965                 msg_Warn( p_demux, "random access indicator (pid=%d) ", p_pid->i_pid );
966         }
967
968         const int i_diff = ( i_cc - p_pid->i_cc )&0x0f;
969         if( b_payload && i_diff == 1 )
970         {
971             p_pid->i_cc = ( p_pid->i_cc + 1 ) & 0xf;
972         }
973         else
974         {
975             if( p_pid->i_cc == 0xff )
976             {
977                 msg_Warn( p_demux, "first packet for pid=%d cc=0x%x",
978                         p_pid->i_pid, i_cc );
979                 p_pid->i_cc = i_cc;
980             }
981             else if( i_diff != 0 )
982             {
983                 /* FIXME what to do when discontinuity_indicator is set ? */
984                 msg_Warn( p_demux, "transport error detected 0x%x instead of 0x%x",
985                           i_cc, ( p_pid->i_cc + 1 )&0x0f );
986
987                 p_pid->i_cc = i_cc;
988                 /* Mark transport error in the TS packet. */
989                 p_buffer[i_pos+1] |= 0x80;
990             }
991         }
992
993         /* Test if user wants to decrypt it first */
994         if( p_sys->csa )
995         {
996             vlc_mutex_lock( &p_sys->csa_lock );
997             csa_Decrypt( p_demux->p_sys->csa, &p_buffer[i_pos], p_demux->p_sys->i_csa_pkt_size );
998             vlc_mutex_unlock( &p_sys->csa_lock );
999         }
1000
1001         i_pos += p_sys->i_packet_size;
1002     }
1003
1004     /* Then write */
1005     const int i_write = fwrite( p_sys->buffer, 1, i_data, p_sys->p_file );
1006     if( i_write < 0 )
1007     {
1008         msg_Err( p_demux, "failed to write data" );
1009         return -1;
1010     }
1011
1012     p_sys->i_write += i_write;
1013     return 1;
1014 }
1015
1016 /*****************************************************************************
1017  * Demux:
1018  *****************************************************************************/
1019 static int Demux( demux_t *p_demux )
1020 {
1021     demux_sys_t *p_sys = p_demux->p_sys;
1022     bool b_wait_es = p_sys->i_pmt_es <= 0;
1023
1024     /* We read at most 100 TS packet or until a frame is completed */
1025     for( int i_pkt = 0; i_pkt < p_sys->i_ts_read; i_pkt++ )
1026     {
1027         bool         b_frame = false;
1028         block_t     *p_pkt;
1029
1030         /* Get a new TS packet */
1031         if( !( p_pkt = stream_Block( p_demux->s, p_sys->i_packet_size ) ) )
1032         {
1033             msg_Dbg( p_demux, "eof ?" );
1034             return 0;
1035         }
1036
1037         /* Check sync byte and re-sync if needed */
1038         if( p_pkt->p_buffer[0] != 0x47 )
1039         {
1040             msg_Warn( p_demux, "lost synchro" );
1041             block_Release( p_pkt );
1042
1043             while( vlc_object_alive (p_demux) )
1044             {
1045                 const uint8_t *p_peek;
1046                 int i_peek, i_skip = 0;
1047
1048                 i_peek = stream_Peek( p_demux->s, &p_peek,
1049                                       p_sys->i_packet_size * 10 );
1050                 if( i_peek < p_sys->i_packet_size + 1 )
1051                 {
1052                     msg_Dbg( p_demux, "eof ?" );
1053                     return 0;
1054                 }
1055
1056                 while( i_skip < i_peek - p_sys->i_packet_size )
1057                 {
1058                     if( p_peek[i_skip] == 0x47 &&
1059                         p_peek[i_skip + p_sys->i_packet_size] == 0x47 )
1060                     {
1061                         break;
1062                     }
1063                     i_skip++;
1064                 }
1065
1066                 msg_Dbg( p_demux, "skipping %d bytes of garbage", i_skip );
1067                 stream_Read( p_demux->s, NULL, i_skip );
1068
1069                 if( i_skip < i_peek - p_sys->i_packet_size )
1070                 {
1071                     break;
1072                 }
1073             }
1074
1075             if( !( p_pkt = stream_Block( p_demux->s, p_sys->i_packet_size ) ) )
1076             {
1077                 msg_Dbg( p_demux, "eof ?" );
1078                 return 0;
1079             }
1080         }
1081
1082         if( p_sys->b_start_record )
1083         {
1084             /* Enable recording once synchronized */
1085             stream_Control( p_demux->s, STREAM_SET_RECORD_STATE, true, "ts" );
1086             p_sys->b_start_record = false;
1087         }
1088
1089         if( p_sys->b_udp_out )
1090         {
1091             memcpy( &p_sys->buffer[i_pkt * p_sys->i_packet_size],
1092                     p_pkt->p_buffer, p_sys->i_packet_size );
1093         }
1094
1095         /* Parse the TS packet */
1096         ts_pid_t *p_pid = &p_sys->pid[PIDGet( p_pkt )];
1097
1098         if( p_pid->b_valid )
1099         {
1100             if( p_pid->psi )
1101             {
1102                 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 ) ) )
1103                 {
1104                     dvbpsi_PushPacket( p_pid->psi->handle, p_pkt->p_buffer );
1105                 }
1106                 else
1107                 {
1108                     for( int i_prg = 0; i_prg < p_pid->psi->i_prg; i_prg++ )
1109                     {
1110                         dvbpsi_PushPacket( p_pid->psi->prg[i_prg]->handle,
1111                                            p_pkt->p_buffer );
1112                     }
1113                 }
1114                 block_Release( p_pkt );
1115             }
1116             else if( !p_sys->b_udp_out )
1117             {
1118                 b_frame = GatherPES( p_demux, p_pid, p_pkt );
1119             }
1120             else
1121             {
1122                 PCRHandle( p_demux, p_pid, p_pkt );
1123                 block_Release( p_pkt );
1124             }
1125         }
1126         else
1127         {
1128             if( !p_pid->b_seen )
1129             {
1130                 msg_Dbg( p_demux, "pid[%d] unknown", p_pid->i_pid );
1131             }
1132             /* We have to handle PCR if present */
1133             PCRHandle( p_demux, p_pid, p_pkt );
1134             block_Release( p_pkt );
1135         }
1136         p_pid->b_seen = true;
1137
1138         if( b_frame || ( b_wait_es && p_sys->i_pmt_es > 0 ) )
1139             break;
1140     }
1141
1142     if( p_sys->b_udp_out )
1143     {
1144         /* Send the complete block */
1145         net_Write( p_demux, p_sys->fd, NULL, p_sys->buffer,
1146                    p_sys->i_ts_read * p_sys->i_packet_size );
1147     }
1148
1149     return 1;
1150 }
1151
1152 /*****************************************************************************
1153  * Control:
1154  *****************************************************************************/
1155 static int DVBEventInformation( demux_t *p_demux, int64_t *pi_time, int64_t *pi_length )
1156 {
1157     demux_sys_t *p_sys = p_demux->p_sys;
1158     if( pi_length )
1159         *pi_length = 0;
1160     if( pi_time )
1161         *pi_time = 0;
1162
1163     if( p_sys->i_dvb_length > 0 )
1164     {
1165 #ifdef TS_USE_TDT
1166         const int64_t t = mdate() + p_sys->i_tdt_delta;
1167 #else
1168         const int64_t t = CLOCK_FREQ * time ( NULL );
1169 #endif
1170
1171         if( p_sys->i_dvb_start <= t && t < p_sys->i_dvb_start + p_sys->i_dvb_length )
1172         {
1173             if( pi_length )
1174                 *pi_length = p_sys->i_dvb_length;
1175             if( pi_time )
1176                 *pi_time   = t - p_sys->i_dvb_start;
1177             return VLC_SUCCESS;
1178         }
1179     }
1180     return VLC_EGENERIC;
1181 }
1182
1183 static int Control( demux_t *p_demux, int i_query, va_list args )
1184 {
1185     demux_sys_t *p_sys = p_demux->p_sys;
1186     double f, *pf;
1187     bool b_bool, *pb_bool;
1188     int64_t i64;
1189     int64_t *pi64;
1190     int i_int;
1191
1192     if( p_sys->b_file_out )
1193         return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
1194
1195     switch( i_query )
1196     {
1197     case DEMUX_GET_POSITION:
1198         pf = (double*) va_arg( args, double* );
1199         i64 = stream_Size( p_demux->s );
1200         if( i64 > 0 )
1201         {
1202             double f_current = stream_Tell( p_demux->s );
1203             *pf = f_current / (double)i64;
1204         }
1205         else
1206         {
1207             int64_t i_time, i_length;
1208             if( !DVBEventInformation( p_demux, &i_time, &i_length ) && i_length > 0 )
1209                 *pf = (double)i_time/(double)i_length;
1210             else
1211                 *pf = 0.0;
1212         }
1213         return VLC_SUCCESS;
1214     case DEMUX_SET_POSITION:
1215         f = (double) va_arg( args, double );
1216         i64 = stream_Size( p_demux->s );
1217
1218         if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) )
1219             return VLC_EGENERIC;
1220
1221         return VLC_SUCCESS;
1222 #if 0
1223
1224     case DEMUX_GET_TIME:
1225         pi64 = (int64_t*)va_arg( args, int64_t * );
1226         if( p_sys->i_time < 0 )
1227         {
1228             *pi64 = 0;
1229             return VLC_EGENERIC;
1230         }
1231         *pi64 = p_sys->i_time;
1232         return VLC_SUCCESS;
1233
1234     case DEMUX_GET_LENGTH:
1235         pi64 = (int64_t*)va_arg( args, int64_t * );
1236         if( p_sys->i_mux_rate > 0 )
1237         {
1238             *pi64 = INT64_C(1000000) * ( stream_Size( p_demux->s ) / 50 ) /
1239                     p_sys->i_mux_rate;
1240             return VLC_SUCCESS;
1241         }
1242         *pi64 = 0;
1243         return VLC_EGENERIC;
1244 #else
1245     case DEMUX_GET_TIME:
1246         pi64 = (int64_t*)va_arg( args, int64_t * );
1247         if( DVBEventInformation( p_demux, pi64, NULL ) )
1248             *pi64 = 0;
1249         return VLC_SUCCESS;
1250
1251     case DEMUX_GET_LENGTH:
1252         pi64 = (int64_t*)va_arg( args, int64_t * );
1253         if( DVBEventInformation( p_demux, NULL, pi64 ) )
1254             *pi64 = 0;
1255         return VLC_SUCCESS;
1256 #endif
1257     case DEMUX_SET_GROUP:
1258     {
1259         vlc_list_t *p_list;
1260
1261         i_int = (int)va_arg( args, int );
1262         p_list = (vlc_list_t *)va_arg( args, vlc_list_t * );
1263         msg_Dbg( p_demux, "DEMUX_SET_GROUP %d %p", i_int, p_list );
1264
1265         if( i_int == 0 && p_sys->i_current_program > 0 )
1266             i_int = p_sys->i_current_program;
1267
1268         if( p_sys->i_current_program > 0 )
1269         {
1270             if( p_sys->i_current_program != i_int )
1271                 SetPrgFilter( p_demux, p_sys->i_current_program, false );
1272         }
1273         else if( p_sys->i_current_program < 0 )
1274         {
1275             for( int i = 0; i < p_sys->programs_list.i_count; i++ )
1276                 SetPrgFilter( p_demux, p_sys->programs_list.p_values[i].i_int, false );
1277         }
1278
1279         if( i_int > 0 )
1280         {
1281             p_sys->i_current_program = i_int;
1282             SetPrgFilter( p_demux, p_sys->i_current_program, true );
1283         }
1284         else if( i_int < 0 )
1285         {
1286             p_sys->i_current_program = -1;
1287             p_sys->programs_list.i_count = 0;
1288             if( p_list )
1289             {
1290                 vlc_list_t *p_dst = &p_sys->programs_list;
1291                 free( p_dst->p_values );
1292
1293                 p_dst->p_values = calloc( p_list->i_count,
1294                                           sizeof(*p_dst->p_values) );
1295                 if( p_dst->p_values )
1296                 {
1297                     p_dst->i_count = p_list->i_count;
1298                     for( int i = 0; i < p_list->i_count; i++ )
1299                     {
1300                         p_dst->p_values[i] = p_list->p_values[i];
1301                         SetPrgFilter( p_demux, p_dst->p_values[i].i_int, true );
1302                     }
1303                 }
1304             }
1305         }
1306         return VLC_SUCCESS;
1307     }
1308
1309     case DEMUX_CAN_RECORD:
1310         pb_bool = (bool*)va_arg( args, bool * );
1311         *pb_bool = true;
1312         return VLC_SUCCESS;
1313
1314     case DEMUX_SET_RECORD_STATE:
1315         b_bool = (bool)va_arg( args, int );
1316
1317         if( !b_bool )
1318             stream_Control( p_demux->s, STREAM_SET_RECORD_STATE, false );
1319         p_sys->b_start_record = b_bool;
1320         return VLC_SUCCESS;
1321
1322     case DEMUX_GET_FPS:
1323     case DEMUX_SET_TIME:
1324     default:
1325         return VLC_EGENERIC;
1326     }
1327 }
1328
1329 /*****************************************************************************
1330  *
1331  *****************************************************************************/
1332 static int UserPmt( demux_t *p_demux, const char *psz_fmt )
1333 {
1334     demux_sys_t *p_sys = p_demux->p_sys;
1335     char *psz_dup = strdup( psz_fmt );
1336     char *psz = psz_dup;
1337     int  i_pid;
1338     int  i_number;
1339
1340     if( !psz_dup )
1341         return VLC_ENOMEM;
1342
1343     /* Parse PID */
1344     i_pid = strtol( psz, &psz, 0 );
1345     if( i_pid < 2 || i_pid >= 8192 )
1346         goto error;
1347
1348     /* Parse optional program number */
1349     i_number = 0;
1350     if( *psz == ':' )
1351         i_number = strtol( &psz[1], &psz, 0 );
1352
1353     /* */
1354     ts_pid_t *pmt = &p_sys->pid[i_pid];
1355     ts_prg_psi_t *prg;
1356
1357     msg_Dbg( p_demux, "user pmt specified (pid=%d,number=%d)", i_pid, i_number );
1358     PIDInit( pmt, true, NULL );
1359
1360     /* Dummy PMT */
1361     prg = malloc( sizeof( ts_prg_psi_t ) );
1362     if( !prg )
1363         goto error;
1364
1365     memset( prg, 0, sizeof( ts_prg_psi_t ) );
1366     prg->i_pid_pcr  = -1;
1367     prg->i_pid_pmt  = -1;
1368     prg->i_version  = -1;
1369     prg->i_number   = i_number != 0 ? i_number : TS_USER_PMT_NUMBER;
1370     prg->handle     = dvbpsi_AttachPMT( i_number != TS_USER_PMT_NUMBER ? i_number : 1, (dvbpsi_pmt_callback)PMTCallBack, p_demux );
1371     TAB_APPEND( pmt->psi->i_prg, pmt->psi->prg, prg );
1372
1373     psz = strchr( psz, '=' );
1374     if( psz )
1375         psz++;
1376     while( psz && *psz )
1377     {
1378         char *psz_next = strchr( psz, ',' );
1379         int i_pid;
1380
1381         if( psz_next )
1382             *psz_next++ = '\0';
1383
1384         i_pid = strtol( psz, &psz, 0 );
1385         if( *psz != ':' || i_pid < 2 || i_pid >= 8192 )
1386             goto next;
1387
1388         char *psz_opt = &psz[1];
1389         if( !strcmp( psz_opt, "pcr" ) )
1390         {
1391             prg->i_pid_pcr = i_pid;
1392         }
1393         else if( !p_sys->pid[i_pid].b_valid )
1394         {
1395             ts_pid_t *pid = &p_sys->pid[i_pid];
1396
1397             char *psz_arg = strchr( psz_opt, '=' );
1398             if( psz_arg )
1399                 *psz_arg++ = '\0';
1400
1401             PIDInit( pid, false, pmt->psi);
1402             if( prg->i_pid_pcr <= 0 )
1403                 prg->i_pid_pcr = i_pid;
1404
1405             if( psz_arg && strlen( psz_arg ) == 4 )
1406             {
1407                 const vlc_fourcc_t i_codec = VLC_FOURCC( psz_arg[0], psz_arg[1],
1408                                                          psz_arg[2], psz_arg[3] );
1409                 int i_cat = UNKNOWN_ES;
1410                 es_format_t *fmt = &pid->es->fmt;
1411
1412                 if( !strcmp( psz_opt, "video" ) )
1413                     i_cat = VIDEO_ES;
1414                 else if( !strcmp( psz_opt, "audio" ) )
1415                     i_cat = AUDIO_ES;
1416                 else if( !strcmp( psz_opt, "spu" ) )
1417                     i_cat = SPU_ES;
1418
1419                 es_format_Init( fmt, i_cat, i_codec );
1420                 fmt->b_packetized = false;
1421             }
1422             else
1423             {
1424                 const int i_stream_type = strtol( psz_opt, NULL, 0 );
1425                 PIDFillFormat( pid, i_stream_type );
1426             }
1427             pid->es->fmt.i_group = i_number;
1428             if( p_sys->b_es_id_pid )
1429                 pid->es->fmt.i_id = i_pid;
1430
1431             if( pid->es->fmt.i_cat != UNKNOWN_ES )
1432             {
1433                 msg_Dbg( p_demux, "  * es pid=%d fcc=%4.4s", i_pid,
1434                          (char*)&pid->es->fmt.i_codec );
1435                 pid->es->id = es_out_Add( p_demux->out,
1436                                           &pid->es->fmt );
1437                 p_sys->i_pmt_es++;
1438             }
1439         }
1440
1441     next:
1442         psz = psz_next;
1443     }
1444
1445     p_sys->b_user_pmt = true;
1446     TAB_APPEND( p_sys->i_pmt, p_sys->pmt, pmt );
1447     free( psz_dup );
1448     return VLC_SUCCESS;
1449
1450 error:
1451     free( psz_dup );
1452     return VLC_EGENERIC;
1453 }
1454
1455 static int SetPIDFilter( demux_t *p_demux, int i_pid, bool b_selected )
1456 {
1457     demux_sys_t *p_sys = p_demux->p_sys;
1458
1459     if( !p_sys->b_access_control )
1460         return VLC_EGENERIC;
1461
1462     return stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
1463                            ACCESS_SET_PRIVATE_ID_STATE, i_pid, b_selected );
1464 }
1465
1466 static void SetPrgFilter( demux_t *p_demux, int i_prg_id, bool b_selected )
1467 {
1468     demux_sys_t *p_sys = p_demux->p_sys;
1469     ts_prg_psi_t *p_prg = NULL;
1470     int i_pmt_pid = -1;
1471
1472     /* Search pmt to be unselected */
1473     for( int i = 0; i < p_sys->i_pmt; i++ )
1474     {
1475         ts_pid_t *pmt = p_sys->pmt[i];
1476
1477         for( int i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
1478         {
1479             if( pmt->psi->prg[i_prg]->i_number == i_prg_id )
1480             {
1481                 i_pmt_pid = p_sys->pmt[i]->i_pid;
1482                 p_prg = p_sys->pmt[i]->psi->prg[i_prg];
1483                 break;
1484             }
1485         }
1486         if( i_pmt_pid > 0 )
1487             break;
1488     }
1489     if( i_pmt_pid <= 0 )
1490         return;
1491     assert( p_prg );
1492
1493     SetPIDFilter( p_demux, i_pmt_pid, b_selected );
1494     if( p_prg->i_pid_pcr > 0 )
1495         SetPIDFilter( p_demux, p_prg->i_pid_pcr, b_selected );
1496
1497     /* All ES */
1498     for( int i = 2; i < 8192; i++ )
1499     {
1500         ts_pid_t *pid = &p_sys->pid[i];
1501
1502         if( !pid->b_valid || pid->psi )
1503             continue;
1504
1505         for( int i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
1506         {
1507             if( pid->p_owner->prg[i_prg]->i_pid_pmt == i_pmt_pid && pid->es->id )
1508             {
1509                 /* We only remove/select es that aren't defined by extra pmt */
1510                 SetPIDFilter( p_demux, i, b_selected );
1511                 break;
1512             }
1513         }
1514     }
1515 }
1516
1517 static void PIDInit( ts_pid_t *pid, bool b_psi, ts_psi_t *p_owner )
1518 {
1519     bool b_old_valid = pid->b_valid;
1520
1521     pid->b_valid    = true;
1522     pid->i_cc       = 0xff;
1523     pid->b_scrambled = false;
1524     pid->p_owner    = p_owner;
1525     pid->i_owner_number = 0;
1526
1527     TAB_INIT( pid->i_extra_es, pid->extra_es );
1528
1529     if( b_psi )
1530     {
1531         pid->es  = NULL;
1532
1533         if( !b_old_valid )
1534         {
1535             pid->psi = xmalloc( sizeof( ts_psi_t ) );
1536             pid->psi->handle = NULL;
1537             TAB_INIT( pid->psi->i_prg, pid->psi->prg );
1538         }
1539         assert( pid->psi );
1540
1541         pid->psi->i_pat_version  = -1;
1542         pid->psi->i_sdt_version  = -1;
1543         if( p_owner )
1544         {
1545             ts_prg_psi_t *prg = malloc( sizeof( ts_prg_psi_t ) );
1546             if( prg )
1547             {
1548                 /* PMT */
1549                 prg->i_version  = -1;
1550                 prg->i_number   = -1;
1551                 prg->i_pid_pcr  = -1;
1552                 prg->i_pid_pmt  = -1;
1553                 prg->iod        = NULL;
1554                 prg->handle     = NULL;
1555
1556                 TAB_APPEND( pid->psi->i_prg, pid->psi->prg, prg );
1557             }
1558         }
1559     }
1560     else
1561     {
1562         pid->psi = NULL;
1563         pid->es  = malloc( sizeof( ts_es_t ) );
1564         if( pid->es )
1565         {
1566             es_format_Init( &pid->es->fmt, UNKNOWN_ES, 0 );
1567             pid->es->id      = NULL;
1568             pid->es->p_pes   = NULL;
1569             pid->es->i_pes_size= 0;
1570             pid->es->i_pes_gathered= 0;
1571             pid->es->pp_last = &pid->es->p_pes;
1572             pid->es->p_mpeg4desc = NULL;
1573             pid->es->b_gather = false;
1574         }
1575     }
1576 }
1577
1578 static void PIDClean( demux_t *p_demux, ts_pid_t *pid )
1579 {
1580     demux_sys_t *p_sys = p_demux->p_sys;
1581     es_out_t *out = p_demux->out;
1582
1583     if( pid->psi )
1584     {
1585         if( pid->psi->handle )
1586             dvbpsi_DetachPMT( pid->psi->handle );
1587         for( int i = 0; i < pid->psi->i_prg; i++ )
1588         {
1589             if( pid->psi->prg[i]->iod )
1590                 IODFree( pid->psi->prg[i]->iod );
1591             if( pid->psi->prg[i]->handle )
1592                 dvbpsi_DetachPMT( pid->psi->prg[i]->handle );
1593             free( pid->psi->prg[i] );
1594         }
1595         free( pid->psi->prg );
1596         free( pid->psi );
1597     }
1598     else
1599     {
1600         if( pid->es->id )
1601         {
1602             es_out_Del( out, pid->es->id );
1603             p_sys->i_pmt_es--;
1604         }
1605
1606         if( pid->es->p_pes )
1607             block_ChainRelease( pid->es->p_pes );
1608
1609         es_format_Clean( &pid->es->fmt );
1610
1611         free( pid->es );
1612
1613         for( int i = 0; i < pid->i_extra_es; i++ )
1614         {
1615             if( pid->extra_es[i]->id )
1616             {
1617                 es_out_Del( out, pid->extra_es[i]->id );
1618                 p_sys->i_pmt_es--;
1619             }
1620
1621             if( pid->extra_es[i]->p_pes )
1622                 block_ChainRelease( pid->extra_es[i]->p_pes );
1623
1624             es_format_Clean( &pid->extra_es[i]->fmt );
1625
1626             free( pid->extra_es[i] );
1627         }
1628         if( pid->i_extra_es )
1629             free( pid->extra_es );
1630     }
1631
1632     pid->b_valid = false;
1633 }
1634
1635 /****************************************************************************
1636  * gathering stuff
1637  ****************************************************************************/
1638 static void ParsePES( demux_t *p_demux, ts_pid_t *pid )
1639 {
1640     block_t *p_pes = pid->es->p_pes;
1641     uint8_t header[34];
1642     unsigned i_pes_size = 0;
1643     unsigned i_skip = 0;
1644     mtime_t i_dts = -1;
1645     mtime_t i_pts = -1;
1646     mtime_t i_length = 0;
1647
1648     /* remove the pes from pid */
1649     pid->es->p_pes = NULL;
1650     pid->es->i_pes_size= 0;
1651     pid->es->i_pes_gathered= 0;
1652     pid->es->pp_last = &pid->es->p_pes;
1653
1654     /* FIXME find real max size */
1655     /* const int i_max = */ block_ChainExtract( p_pes, header, 34 );
1656
1657     if( header[0] != 0 || header[1] != 0 || header[2] != 1 )
1658     {
1659         if( !p_demux->p_sys->b_silent )
1660             msg_Warn( p_demux, "invalid header [0x%02x:%02x:%02x:%02x] (pid: %d)",
1661                       header[0], header[1],header[2],header[3], pid->i_pid );
1662         block_ChainRelease( p_pes );
1663         return;
1664     }
1665
1666     /* TODO check size */
1667     switch( header[3] )
1668     {
1669     case 0xBC:  /* Program stream map */
1670     case 0xBE:  /* Padding */
1671     case 0xBF:  /* Private stream 2 */
1672     case 0xF0:  /* ECM */
1673     case 0xF1:  /* EMM */
1674     case 0xFF:  /* Program stream directory */
1675     case 0xF2:  /* DSMCC stream */
1676     case 0xF8:  /* ITU-T H.222.1 type E stream */
1677         i_skip = 6;
1678         break;
1679     default:
1680         if( ( header[6]&0xC0 ) == 0x80 )
1681         {
1682             /* mpeg2 PES */
1683             i_skip = header[8] + 9;
1684
1685             if( header[7]&0x80 )    /* has pts */
1686             {
1687                 i_pts = ((mtime_t)(header[ 9]&0x0e ) << 29)|
1688                          (mtime_t)(header[10] << 22)|
1689                         ((mtime_t)(header[11]&0xfe) << 14)|
1690                          (mtime_t)(header[12] << 7)|
1691                          (mtime_t)(header[13] >> 1);
1692
1693                 if( header[7]&0x40 )    /* has dts */
1694                 {
1695                      i_dts = ((mtime_t)(header[14]&0x0e ) << 29)|
1696                              (mtime_t)(header[15] << 22)|
1697                             ((mtime_t)(header[16]&0xfe) << 14)|
1698                              (mtime_t)(header[17] << 7)|
1699                              (mtime_t)(header[18] >> 1);
1700                 }
1701             }
1702         }
1703         else
1704         {
1705             i_skip = 6;
1706             while( i_skip < 23 && header[i_skip] == 0xff )
1707             {
1708                 i_skip++;
1709             }
1710             if( i_skip == 23 )
1711             {
1712                 msg_Err( p_demux, "too much MPEG-1 stuffing" );
1713                 block_ChainRelease( p_pes );
1714                 return;
1715             }
1716             if( ( header[i_skip] & 0xC0 ) == 0x40 )
1717             {
1718                 i_skip += 2;
1719             }
1720
1721             if(  header[i_skip]&0x20 )
1722             {
1723                  i_pts = ((mtime_t)(header[i_skip]&0x0e ) << 29)|
1724                           (mtime_t)(header[i_skip+1] << 22)|
1725                          ((mtime_t)(header[i_skip+2]&0xfe) << 14)|
1726                           (mtime_t)(header[i_skip+3] << 7)|
1727                           (mtime_t)(header[i_skip+4] >> 1);
1728
1729                 if( header[i_skip]&0x10 )    /* has dts */
1730                 {
1731                      i_dts = ((mtime_t)(header[i_skip+5]&0x0e ) << 29)|
1732                               (mtime_t)(header[i_skip+6] << 22)|
1733                              ((mtime_t)(header[i_skip+7]&0xfe) << 14)|
1734                               (mtime_t)(header[i_skip+8] << 7)|
1735                               (mtime_t)(header[i_skip+9] >> 1);
1736                      i_skip += 10;
1737                 }
1738                 else
1739                 {
1740                     i_skip += 5;
1741                 }
1742             }
1743             else
1744             {
1745                 i_skip += 1;
1746             }
1747         }
1748         break;
1749     }
1750
1751     if( pid->es->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', 'b' ) ||
1752         pid->es->fmt.i_codec == VLC_FOURCC( 'd', 't', 's', 'b' ) )
1753     {
1754         i_skip += 4;
1755     }
1756     else if( pid->es->fmt.i_codec == VLC_FOURCC( 'l', 'p', 'c', 'b' ) ||
1757              pid->es->fmt.i_codec == VLC_FOURCC( 's', 'p', 'u', 'b' ) ||
1758              pid->es->fmt.i_codec == VLC_FOURCC( 's', 'd', 'd', 'b' ) )
1759     {
1760         i_skip += 1;
1761     }
1762     else if( pid->es->fmt.i_codec == VLC_CODEC_SUBT &&
1763              pid->es->p_mpeg4desc )
1764     {
1765         decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;
1766
1767         if( dcd->i_decoder_specific_info_len > 2 &&
1768             dcd->p_decoder_specific_info[0] == 0x10 &&
1769             ( dcd->p_decoder_specific_info[1]&0x10 ) )
1770         {
1771             /* display length */
1772             if( p_pes->i_buffer + 2 <= i_skip )
1773                 i_length = GetWBE( &p_pes->p_buffer[i_skip] );
1774
1775             i_skip += 2;
1776         }
1777         if( p_pes->i_buffer + 2 <= i_skip )
1778             i_pes_size = GetWBE( &p_pes->p_buffer[i_skip] );
1779         /* */
1780         i_skip += 2;
1781     }
1782 #ifdef ZVBI_COMPILED
1783     else if( pid->es->fmt.i_codec == VLC_CODEC_TELETEXT )
1784         i_skip = 0; /*hack for zvbi support */
1785 #endif
1786     /* skip header */
1787     while( p_pes && i_skip > 0 )
1788     {
1789         if( p_pes->i_buffer <= i_skip )
1790         {
1791             block_t *p_next = p_pes->p_next;
1792
1793             i_skip -= p_pes->i_buffer;
1794             block_Release( p_pes );
1795             p_pes = p_next;
1796         }
1797         else
1798         {
1799             p_pes->i_buffer -= i_skip;
1800             p_pes->p_buffer += i_skip;
1801             break;
1802         }
1803     }
1804
1805     /* ISO/IEC 13818-1 2.7.5: if no pts and no dts, then dts == pts */
1806     if( i_pts >= 0 && i_dts < 0 )
1807         i_dts = i_pts;
1808
1809     if( p_pes )
1810     {
1811         block_t *p_block;
1812         int i;
1813
1814         if( i_dts >= 0 )
1815             p_pes->i_dts = VLC_TS_0 + i_dts * 100 / 9;
1816
1817         if( i_pts >= 0 )
1818             p_pes->i_pts = VLC_TS_0 + i_pts * 100 / 9;
1819
1820         p_pes->i_length = i_length * 100 / 9;
1821
1822         p_block = block_ChainGather( p_pes );
1823         if( pid->es->fmt.i_codec == VLC_CODEC_SUBT )
1824         {
1825             if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
1826             {
1827                 p_block->i_buffer = i_pes_size;
1828             }
1829             /* Append a \0 */
1830             p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
1831             if( !p_block )
1832                 abort();
1833             p_block->p_buffer[p_block->i_buffer -1] = '\0';
1834         }
1835
1836         for( i = 0; i < pid->i_extra_es; i++ )
1837         {
1838             es_out_Send( p_demux->out, pid->extra_es[i]->id,
1839                          block_Duplicate( p_block ) );
1840         }
1841
1842         es_out_Send( p_demux->out, pid->es->id, p_block );
1843     }
1844     else
1845     {
1846         msg_Warn( p_demux, "empty pes" );
1847     }
1848 }
1849
1850 static void PCRHandle( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
1851 {
1852     demux_sys_t   *p_sys = p_demux->p_sys;
1853     const uint8_t *p = p_bk->p_buffer;
1854
1855     if( p_sys->i_pmt_es <= 0 )
1856         return;
1857
1858     if( ( p[3]&0x20 ) && /* adaptation */
1859         ( p[5]&0x10 ) &&
1860         ( p[4] >= 7 ) )
1861     {
1862         /* PCR is 33 bits */
1863         const mtime_t i_pcr = ( (mtime_t)p[6] << 25 ) |
1864                               ( (mtime_t)p[7] << 17 ) |
1865                               ( (mtime_t)p[8] << 9 ) |
1866                               ( (mtime_t)p[9] << 1 ) |
1867                               ( (mtime_t)p[10] >> 7 );
1868
1869         /* Search program and set the PCR */
1870         for( int i = 0; i < p_sys->i_pmt; i++ )
1871         {
1872             for( int i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
1873             {
1874                 if( pid->i_pid == p_sys->pmt[i]->psi->prg[i_prg]->i_pid_pcr )
1875                 {
1876                     es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
1877                                     (int)p_sys->pmt[i]->psi->prg[i_prg]->i_number,
1878                                     (int64_t)(VLC_TS_0 + i_pcr * 100 / 9) );
1879                 }
1880             }
1881         }
1882     }
1883 }
1884
1885 static bool GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
1886 {
1887     const uint8_t *p = p_bk->p_buffer;
1888     const bool b_unit_start = p[1]&0x40;
1889     const bool b_scrambled  = p[3]&0x80;
1890     const bool b_adaptation = p[3]&0x20;
1891     const bool b_payload    = p[3]&0x10;
1892     const int  i_cc         = p[3]&0x0f; /* continuity counter */
1893     bool       b_discontinuity = false;  /* discontinuity */
1894
1895     /* transport_scrambling_control is ignored */
1896     int         i_skip = 0;
1897     bool        i_ret  = false;
1898
1899 #if 0
1900     msg_Dbg( p_demux, "pid=%d unit_start=%d adaptation=%d payload=%d "
1901              "cc=0x%x", pid->i_pid, b_unit_start, b_adaptation,
1902              b_payload, i_cc );
1903 #endif
1904
1905     /* For now, ignore additional error correction
1906      * TODO: handle Reed-Solomon 204,188 error correction */
1907     p_bk->i_buffer = TS_PACKET_SIZE_188;
1908
1909     if( p[1]&0x80 )
1910     {
1911         msg_Dbg( p_demux, "transport_error_indicator set (pid=%d)",
1912                  pid->i_pid );
1913         if( pid->es->p_pes ) //&& pid->es->fmt.i_cat == VIDEO_ES )
1914             pid->es->p_pes->i_flags |= BLOCK_FLAG_CORRUPTED;
1915     }
1916
1917     if( p_demux->p_sys->csa )
1918     {
1919         vlc_mutex_lock( &p_demux->p_sys->csa_lock );
1920         csa_Decrypt( p_demux->p_sys->csa, p_bk->p_buffer, p_demux->p_sys->i_csa_pkt_size );
1921         vlc_mutex_unlock( &p_demux->p_sys->csa_lock );
1922     }
1923
1924     if( !b_adaptation )
1925     {
1926         /* We don't have any adaptation_field, so payload starts
1927          * immediately after the 4 byte TS header */
1928         i_skip = 4;
1929     }
1930     else
1931     {
1932         /* p[4] is adaptation_field_length minus one */
1933         i_skip = 5 + p[4];
1934         if( p[4] > 0 )
1935         {
1936             /* discontinuity indicator found in stream */
1937             b_discontinuity = (p[5]&0x80) ? true : false;
1938             if( b_discontinuity && pid->es->p_pes )
1939             {
1940                 msg_Warn( p_demux, "discontinuity indicator (pid=%d) ",
1941                             pid->i_pid );
1942                 /* pid->es->p_pes->i_flags |= BLOCK_FLAG_DISCONTINUITY; */
1943             }
1944 #if 0
1945             if( p[5]&0x40 )
1946                 msg_Dbg( p_demux, "random access indicator (pid=%d) ", pid->i_pid );
1947 #endif
1948         }
1949     }
1950
1951     /* Test continuity counter */
1952     /* continuous when (one of this):
1953         * diff == 1
1954         * diff == 0 and payload == 0
1955         * diff == 0 and duplicate packet (playload != 0) <- should we
1956         *   test the content ?
1957      */
1958     const int i_diff = ( i_cc - pid->i_cc )&0x0f;
1959     if( b_payload && i_diff == 1 )
1960     {
1961         pid->i_cc = ( pid->i_cc + 1 ) & 0xf;
1962     }
1963     else
1964     {
1965         if( pid->i_cc == 0xff )
1966         {
1967             msg_Warn( p_demux, "first packet for pid=%d cc=0x%x",
1968                       pid->i_pid, i_cc );
1969             pid->i_cc = i_cc;
1970         }
1971         else if( i_diff != 0 && !b_discontinuity )
1972         {
1973             msg_Warn( p_demux, "discontinuity received 0x%x instead of 0x%x (pid=%d)",
1974                       i_cc, ( pid->i_cc + 1 )&0x0f, pid->i_pid );
1975
1976             pid->i_cc = i_cc;
1977             if( pid->es->p_pes && pid->es->fmt.i_cat != VIDEO_ES )
1978             {
1979                 /* Small video artifacts are usually better than
1980                  * dropping full frames */
1981                 pid->es->p_pes->i_flags |= BLOCK_FLAG_CORRUPTED;
1982             }
1983         }
1984     }
1985
1986     PCRHandle( p_demux, pid, p_bk );
1987
1988     if( i_skip >= 188 || pid->es->id == NULL || p_demux->p_sys->b_udp_out )
1989     {
1990         block_Release( p_bk );
1991         return i_ret;
1992     }
1993
1994     /* */
1995     if( !pid->b_scrambled != !b_scrambled )
1996     {
1997         msg_Warn( p_demux, "scrambled state changed on pid %d (%d->%d)",
1998                   pid->i_pid, pid->b_scrambled, b_scrambled );
1999
2000         pid->b_scrambled = b_scrambled;
2001
2002         for( int i = 0; i < pid->i_extra_es; i++ )
2003         {
2004             es_out_Control( p_demux->out, ES_OUT_SET_ES_SCRAMBLED_STATE,
2005                             pid->extra_es[i]->id, b_scrambled );
2006         }
2007         es_out_Control( p_demux->out, ES_OUT_SET_ES_SCRAMBLED_STATE,
2008                         pid->es->id, b_scrambled );
2009     }
2010
2011     /* We have to gather it */
2012     p_bk->p_buffer += i_skip;
2013     p_bk->i_buffer -= i_skip;
2014
2015     if( b_unit_start )
2016     {
2017         if( pid->es->p_pes )
2018         {
2019             ParsePES( p_demux, pid );
2020             i_ret = true;
2021         }
2022
2023         block_ChainLastAppend( &pid->es->pp_last, p_bk );
2024         if( p_bk->i_buffer > 6 )
2025         {
2026             pid->es->i_pes_size = GetWBE( &p_bk->p_buffer[4] );
2027             if( pid->es->i_pes_size > 0 )
2028             {
2029                 pid->es->i_pes_size += 6;
2030             }
2031         }
2032         pid->es->i_pes_gathered += p_bk->i_buffer;
2033         if( pid->es->i_pes_size > 0 &&
2034             pid->es->i_pes_gathered >= pid->es->i_pes_size )
2035         {
2036             ParsePES( p_demux, pid );
2037             i_ret = true;
2038         }
2039     }
2040     else
2041     {
2042         if( pid->es->p_pes == NULL )
2043         {
2044             /* msg_Dbg( p_demux, "broken packet" ); */
2045             block_Release( p_bk );
2046         }
2047         else
2048         {
2049             block_ChainLastAppend( &pid->es->pp_last, p_bk );
2050             pid->es->i_pes_gathered += p_bk->i_buffer;
2051             if( pid->es->i_pes_size > 0 &&
2052                 pid->es->i_pes_gathered >= pid->es->i_pes_size )
2053             {
2054                 ParsePES( p_demux, pid );
2055                 i_ret = true;
2056             }
2057         }
2058     }
2059
2060     return i_ret;
2061 }
2062
2063 static int PIDFillFormat( ts_pid_t *pid, int i_stream_type )
2064 {
2065     es_format_t *fmt = &pid->es->fmt;
2066
2067     switch( i_stream_type )
2068     {
2069     case 0x01:  /* MPEG-1 video */
2070     case 0x02:  /* MPEG-2 video */
2071     case 0x80:  /* MPEG-2 MOTO video */
2072         es_format_Init( fmt, VIDEO_ES, VLC_CODEC_MPGV );
2073         break;
2074     case 0x03:  /* MPEG-1 audio */
2075     case 0x04:  /* MPEG-2 audio */
2076         es_format_Init( fmt, AUDIO_ES, VLC_CODEC_MPGA );
2077         break;
2078     case 0x11:  /* MPEG4 (audio) LATM */
2079     case 0x0f:  /* ISO/IEC 13818-7 Audio with ADTS transport syntax */
2080         es_format_Init( fmt, AUDIO_ES, VLC_CODEC_MP4A );
2081         break;
2082     case 0x10:  /* MPEG4 (video) */
2083         es_format_Init( fmt, VIDEO_ES, VLC_CODEC_MP4V );
2084         pid->es->b_gather = true;
2085         break;
2086     case 0x1B:  /* H264 <- check transport syntax/needed descriptor */
2087         es_format_Init( fmt, VIDEO_ES, VLC_CODEC_H264 );
2088         break;
2089
2090     case 0x81:  /* A52 (audio) */
2091         es_format_Init( fmt, AUDIO_ES, VLC_CODEC_A52 );
2092         break;
2093     case 0x82:  /* DVD_SPU (sub) */
2094         es_format_Init( fmt, SPU_ES, VLC_CODEC_SPU );
2095         break;
2096     case 0x83:  /* LPCM (audio) */
2097         es_format_Init( fmt, AUDIO_ES, VLC_CODEC_DVD_LPCM );
2098         break;
2099     case 0x84:  /* SDDS (audio) */
2100         es_format_Init( fmt, AUDIO_ES, VLC_CODEC_SDDS );
2101         break;
2102     case 0x85:  /* DTS (audio) */
2103         es_format_Init( fmt, AUDIO_ES, VLC_CODEC_DTS );
2104         break;
2105     case 0x87: /* E-AC3 */
2106         es_format_Init( fmt, AUDIO_ES, VLC_CODEC_EAC3 );
2107         break;
2108
2109     case 0x91:  /* A52 vls (audio) */
2110         es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', 'b' ) );
2111         break;
2112     case 0x92:  /* DVD_SPU vls (sub) */
2113         es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', 'b' ) );
2114         break;
2115
2116     case 0x94:  /* SDDS (audio) */
2117         es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 'b' ) );
2118         break;
2119
2120     case 0xa0:  /* MSCODEC vlc (video) (fixed later) */
2121         es_format_Init( fmt, UNKNOWN_ES, 0 );
2122         pid->es->b_gather = true;
2123         break;
2124
2125     case 0x06:  /* PES_PRIVATE  (fixed later) */
2126     case 0x12:  /* MPEG-4 generic (sub/scene/...) (fixed later) */
2127     case 0xEA:  /* Privately managed ES (VC-1) (fixed later */
2128     default:
2129         es_format_Init( fmt, UNKNOWN_ES, 0 );
2130         break;
2131     }
2132
2133     /* PES packets usually contain truncated frames */
2134     fmt->b_packetized = false;
2135
2136     return fmt->i_cat == UNKNOWN_ES ? VLC_EGENERIC : VLC_SUCCESS ;
2137 }
2138
2139 /*****************************************************************************
2140  * MP4 specific functions (IOD parser)
2141  *****************************************************************************/
2142 static int  IODDescriptorLength( int *pi_data, uint8_t **pp_data )
2143 {
2144     unsigned int i_b;
2145     unsigned int i_len = 0;
2146     do
2147     {
2148         i_b = **pp_data;
2149         (*pp_data)++;
2150         (*pi_data)--;
2151         i_len = ( i_len << 7 ) + ( i_b&0x7f );
2152
2153     } while( i_b&0x80 );
2154
2155     return( i_len );
2156 }
2157
2158 static int IODGetByte( int *pi_data, uint8_t **pp_data )
2159 {
2160     if( *pi_data > 0 )
2161     {
2162         const int i_b = **pp_data;
2163         (*pp_data)++;
2164         (*pi_data)--;
2165         return( i_b );
2166     }
2167     return( 0 );
2168 }
2169
2170 static int IODGetWord( int *pi_data, uint8_t **pp_data )
2171 {
2172     const int i1 = IODGetByte( pi_data, pp_data );
2173     const int i2 = IODGetByte( pi_data, pp_data );
2174     return( ( i1 << 8 ) | i2 );
2175 }
2176
2177 static int IODGet3Bytes( int *pi_data, uint8_t **pp_data )
2178 {
2179     const int i1 = IODGetByte( pi_data, pp_data );
2180     const int i2 = IODGetByte( pi_data, pp_data );
2181     const int i3 = IODGetByte( pi_data, pp_data );
2182
2183     return( ( i1 << 16 ) | ( i2 << 8) | i3 );
2184 }
2185
2186 static uint32_t IODGetDWord( int *pi_data, uint8_t **pp_data )
2187 {
2188     const uint32_t i1 = IODGetWord( pi_data, pp_data );
2189     const uint32_t i2 = IODGetWord( pi_data, pp_data );
2190     return( ( i1 << 16 ) | i2 );
2191 }
2192
2193 static char* IODGetURL( int *pi_data, uint8_t **pp_data )
2194 {
2195     char *url;
2196     int i_url_len, i;
2197
2198     i_url_len = IODGetByte( pi_data, pp_data );
2199     url = malloc( i_url_len + 1 );
2200     if( !url ) return NULL;
2201     for( i = 0; i < i_url_len; i++ )
2202     {
2203         url[i] = IODGetByte( pi_data, pp_data );
2204     }
2205     url[i_url_len] = '\0';
2206     return( url );
2207 }
2208
2209 static iod_descriptor_t *IODNew( int i_data, uint8_t *p_data )
2210 {
2211     iod_descriptor_t *p_iod;
2212     int i;
2213     int i_es_index;
2214     uint8_t i_flags, i_iod_tag, byte1, byte2, byte3;
2215     bool  b_url;
2216     int   i_iod_length;
2217
2218     p_iod = malloc( sizeof( iod_descriptor_t ) );
2219     if( !p_iod ) return NULL;
2220     memset( p_iod, 0, sizeof( iod_descriptor_t ) );
2221
2222 #ifdef TS_DEBUG
2223     fprintf( stderr, "\n************ IOD ************" );
2224 #endif
2225     for( i = 0; i < 255; i++ )
2226     {
2227         p_iod->es_descr[i].b_ok = 0;
2228     }
2229     i_es_index = 0;
2230
2231     if( i_data < 3 )
2232     {
2233         return p_iod;
2234     }
2235
2236     byte1 = IODGetByte( &i_data, &p_data );
2237     byte2 = IODGetByte( &i_data, &p_data );
2238     byte3 = IODGetByte( &i_data, &p_data );
2239     if( byte2 == 0x02 ) //old vlc's buggy implementation of the IOD_descriptor
2240     {
2241         p_iod->i_iod_label_scope = 0x11;
2242         p_iod->i_iod_label = byte1;
2243         i_iod_tag = byte2;
2244     }
2245     else  //correct implementation of the IOD_descriptor
2246     {
2247         p_iod->i_iod_label_scope = byte1;
2248         p_iod->i_iod_label = byte2;
2249         i_iod_tag = byte3;
2250     }
2251 #ifdef TS_DEBUG
2252     fprintf( stderr, "\n* iod_label:%d", p_iod->i_iod_label );
2253     fprintf( stderr, "\n* ===========" );
2254     fprintf( stderr, "\n* tag:0x%x", i_iod_tag );
2255 #endif
2256     if( i_iod_tag != 0x02 )
2257     {
2258 #ifdef TS_DEBUG
2259         fprintf( stderr, "\n ERR: tag %02x != 0x02", i_iod_tag );
2260 #endif
2261         return p_iod;
2262     }
2263
2264     i_iod_length = IODDescriptorLength( &i_data, &p_data );
2265 #ifdef TS_DEBUG
2266     fprintf( stderr, "\n* length:%d", i_iod_length );
2267 #endif
2268     if( i_iod_length > i_data )
2269     {
2270         i_iod_length = i_data;
2271     }
2272
2273     p_iod->i_od_id = ( IODGetByte( &i_data, &p_data ) << 2 );
2274     i_flags = IODGetByte( &i_data, &p_data );
2275     p_iod->i_od_id |= i_flags >> 6;
2276     b_url = ( i_flags >> 5  )&0x01;
2277 #ifdef TS_DEBUG
2278     fprintf( stderr, "\n* od_id:%d", p_iod->i_od_id );
2279     fprintf( stderr, "\n* url flag:%d", b_url );
2280     fprintf( stderr, "\n* includeInlineProfileLevel flag:%d", ( i_flags >> 4 )&0x01 );
2281 #endif
2282     if( b_url )
2283     {
2284         p_iod->psz_url = IODGetURL( &i_data, &p_data );
2285 #ifdef TS_DEBUG
2286         fprintf( stderr, "\n* url string:%s", p_iod->psz_url );
2287         fprintf( stderr, "\n*****************************\n" );
2288 #endif
2289         return p_iod;
2290     }
2291     else
2292     {
2293         p_iod->psz_url = NULL;
2294     }
2295
2296     p_iod->i_ODProfileLevelIndication = IODGetByte( &i_data, &p_data );
2297     p_iod->i_sceneProfileLevelIndication = IODGetByte( &i_data, &p_data );
2298     p_iod->i_audioProfileLevelIndication = IODGetByte( &i_data, &p_data );
2299     p_iod->i_visualProfileLevelIndication = IODGetByte( &i_data, &p_data );
2300     p_iod->i_graphicsProfileLevelIndication = IODGetByte( &i_data, &p_data );
2301 #ifdef TS_DEBUG
2302     fprintf( stderr, "\n* ODProfileLevelIndication:%d", p_iod->i_ODProfileLevelIndication );
2303     fprintf( stderr, "\n* sceneProfileLevelIndication:%d", p_iod->i_sceneProfileLevelIndication );
2304     fprintf( stderr, "\n* audioProfileLevelIndication:%d", p_iod->i_audioProfileLevelIndication );
2305     fprintf( stderr, "\n* visualProfileLevelIndication:%d", p_iod->i_visualProfileLevelIndication );
2306     fprintf( stderr, "\n* graphicsProfileLevelIndication:%d", p_iod->i_graphicsProfileLevelIndication );
2307 #endif
2308
2309     while( i_data > 0 && i_es_index < 255)
2310     {
2311         int i_tag, i_length;
2312         int     i_data_sav;
2313         uint8_t *p_data_sav;
2314
2315         i_tag = IODGetByte( &i_data, &p_data );
2316         i_length = IODDescriptorLength( &i_data, &p_data );
2317
2318         i_data_sav = i_data;
2319         p_data_sav = p_data;
2320
2321         i_data = i_length;
2322
2323         switch( i_tag )
2324         {
2325         case 0x03:
2326             {
2327 #define es_descr    p_iod->es_descr[i_es_index]
2328                 int i_decoderConfigDescr_length;
2329 #ifdef TS_DEBUG
2330                 fprintf( stderr, "\n* - ES_Descriptor length:%d", i_length );
2331 #endif
2332                 es_descr.b_ok = 1;
2333
2334                 es_descr.i_es_id = IODGetWord( &i_data, &p_data );
2335                 i_flags = IODGetByte( &i_data, &p_data );
2336                 es_descr.b_streamDependenceFlag = ( i_flags >> 7 )&0x01;
2337                 b_url = ( i_flags >> 6 )&0x01;
2338                 es_descr.b_OCRStreamFlag = ( i_flags >> 5 )&0x01;
2339                 es_descr.i_streamPriority = i_flags & 0x1f;
2340 #ifdef TS_DEBUG
2341                 fprintf( stderr, "\n*   * streamDependenceFlag:%d", es_descr.b_streamDependenceFlag );
2342                 fprintf( stderr, "\n*   * OCRStreamFlag:%d", es_descr.b_OCRStreamFlag );
2343                 fprintf( stderr, "\n*   * streamPriority:%d", es_descr.i_streamPriority );
2344 #endif
2345                 if( es_descr.b_streamDependenceFlag )
2346                 {
2347                     es_descr.i_dependOn_es_id = IODGetWord( &i_data, &p_data );
2348 #ifdef TS_DEBUG
2349                     fprintf( stderr, "\n*   * dependOn_es_id:%d", es_descr.i_dependOn_es_id );
2350 #endif
2351                 }
2352
2353                 if( b_url )
2354                 {
2355                     es_descr.psz_url = IODGetURL( &i_data, &p_data );
2356 #ifdef TS_DEBUG
2357                     fprintf( stderr, "\n* url string:%s", es_descr.psz_url );
2358 #endif
2359                 }
2360                 else
2361                 {
2362                     es_descr.psz_url = NULL;
2363                 }
2364
2365                 if( es_descr.b_OCRStreamFlag )
2366                 {
2367                     es_descr.i_OCR_es_id = IODGetWord( &i_data, &p_data );
2368 #ifdef TS_DEBUG
2369                     fprintf( stderr, "\n*   * OCR_es_id:%d", es_descr.i_OCR_es_id );
2370 #endif
2371                 }
2372
2373                 if( IODGetByte( &i_data, &p_data ) != 0x04 )
2374                 {
2375 #ifdef TS_DEBUG
2376                     fprintf( stderr, "\n* ERR missing DecoderConfigDescr" );
2377 #endif
2378                     es_descr.b_ok = 0;
2379                     break;
2380                 }
2381                 i_decoderConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
2382 #ifdef TS_DEBUG
2383                 fprintf( stderr, "\n*   - DecoderConfigDesc length:%d", i_decoderConfigDescr_length );
2384 #endif
2385 #define dec_descr   es_descr.dec_descr
2386                 dec_descr.i_objectTypeIndication = IODGetByte( &i_data, &p_data );
2387                 i_flags = IODGetByte( &i_data, &p_data );
2388                 dec_descr.i_streamType = i_flags >> 2;
2389                 dec_descr.b_upStream = ( i_flags >> 1 )&0x01;
2390                 dec_descr.i_bufferSizeDB = IODGet3Bytes( &i_data, &p_data );
2391                 dec_descr.i_maxBitrate = IODGetDWord( &i_data, &p_data );
2392                 dec_descr.i_avgBitrate = IODGetDWord( &i_data, &p_data );
2393 #ifdef TS_DEBUG
2394                 fprintf( stderr, "\n*     * objectTypeIndication:0x%x", dec_descr.i_objectTypeIndication  );
2395                 fprintf( stderr, "\n*     * streamType:0x%x", dec_descr.i_streamType );
2396                 fprintf( stderr, "\n*     * upStream:%d", dec_descr.b_upStream );
2397                 fprintf( stderr, "\n*     * bufferSizeDB:%d", dec_descr.i_bufferSizeDB );
2398                 fprintf( stderr, "\n*     * maxBitrate:%d", dec_descr.i_maxBitrate );
2399                 fprintf( stderr, "\n*     * avgBitrate:%d", dec_descr.i_avgBitrate );
2400 #endif
2401                 if( i_decoderConfigDescr_length > 13 && IODGetByte( &i_data, &p_data ) == 0x05 )
2402                 {
2403                     int i;
2404                     dec_descr.i_decoder_specific_info_len =
2405                         IODDescriptorLength( &i_data, &p_data );
2406                     if( dec_descr.i_decoder_specific_info_len > 0 )
2407                     {
2408                         dec_descr.p_decoder_specific_info =
2409                             xmalloc( dec_descr.i_decoder_specific_info_len );
2410                     }
2411                     for( i = 0; i < dec_descr.i_decoder_specific_info_len; i++ )
2412                     {
2413                         dec_descr.p_decoder_specific_info[i] = IODGetByte( &i_data, &p_data );
2414                     }
2415                 }
2416                 else
2417                 {
2418                     dec_descr.i_decoder_specific_info_len = 0;
2419                     dec_descr.p_decoder_specific_info = NULL;
2420                 }
2421             }
2422 #undef  dec_descr
2423 #define sl_descr    es_descr.sl_descr
2424             {
2425                 int i_SLConfigDescr_length;
2426                 int i_predefined;
2427
2428                 if( IODGetByte( &i_data, &p_data ) != 0x06 )
2429                 {
2430 #ifdef TS_DEBUG
2431                     fprintf( stderr, "\n* ERR missing SLConfigDescr" );
2432 #endif
2433                     es_descr.b_ok = 0;
2434                     break;
2435                 }
2436                 i_SLConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
2437 #ifdef TS_DEBUG
2438                 fprintf( stderr, "\n*   - SLConfigDescr length:%d", i_SLConfigDescr_length );
2439 #endif
2440                 i_predefined = IODGetByte( &i_data, &p_data );
2441 #ifdef TS_DEBUG
2442                 fprintf( stderr, "\n*     * i_predefined:0x%x", i_predefined  );
2443 #endif
2444                 switch( i_predefined )
2445                 {
2446                 case 0x01:
2447                     {
2448                         sl_descr.b_useAccessUnitStartFlag   = 0;
2449                         sl_descr.b_useAccessUnitEndFlag     = 0;
2450                         sl_descr.b_useRandomAccessPointFlag = 0;
2451                         //sl_descr.b_useRandomAccessUnitsOnlyFlag = 0;
2452                         sl_descr.b_usePaddingFlag           = 0;
2453                         sl_descr.b_useTimeStampsFlags       = 0;
2454                         sl_descr.b_useIdleFlag              = 0;
2455                         sl_descr.b_durationFlag     = 0;    // FIXME FIXME
2456                         sl_descr.i_timeStampResolution      = 1000;
2457                         sl_descr.i_OCRResolution    = 0;    // FIXME FIXME
2458                         sl_descr.i_timeStampLength          = 32;
2459                         sl_descr.i_OCRLength        = 0;    // FIXME FIXME
2460                         sl_descr.i_AU_Length                = 0;
2461                         sl_descr.i_instantBitrateLength= 0; // FIXME FIXME
2462                         sl_descr.i_degradationPriorityLength= 0;
2463                         sl_descr.i_AU_seqNumLength          = 0;
2464                         sl_descr.i_packetSeqNumLength       = 0;
2465                         if( sl_descr.b_durationFlag )
2466                         {
2467                             sl_descr.i_timeScale            = 0;    // FIXME FIXME
2468                             sl_descr.i_accessUnitDuration   = 0;    // FIXME FIXME
2469                             sl_descr.i_compositionUnitDuration= 0;    // FIXME FIXME
2470                         }
2471                         if( !sl_descr.b_useTimeStampsFlags )
2472                         {
2473                             sl_descr.i_startDecodingTimeStamp   = 0;    // FIXME FIXME
2474                             sl_descr.i_startCompositionTimeStamp= 0;    // FIXME FIXME
2475                         }
2476                     }
2477                     break;
2478                 default:
2479 #ifdef TS_DEBUG
2480                     fprintf( stderr, "\n* ERR unsupported SLConfigDescr predefined" );
2481 #endif
2482                     es_descr.b_ok = 0;
2483                     break;
2484                 }
2485             }
2486             break;
2487 #undef  sl_descr
2488 #undef  es_descr
2489         default:
2490 #ifdef TS_DEBUG
2491             fprintf( stderr, "\n* - OD tag:0x%x length:%d (Unsupported)", i_tag, i_length );
2492 #endif
2493             break;
2494         }
2495
2496         p_data = p_data_sav + i_length;
2497         i_data = i_data_sav - i_length;
2498         i_es_index++;
2499     }
2500 #ifdef TS_DEBUG
2501     fprintf( stderr, "\n*****************************\n" );
2502 #endif
2503     return p_iod;
2504 }
2505
2506 static void IODFree( iod_descriptor_t *p_iod )
2507 {
2508     int i;
2509
2510     if( p_iod->psz_url )
2511     {
2512         free( p_iod->psz_url );
2513         p_iod->psz_url = NULL;
2514         free( p_iod );
2515         return;
2516     }
2517
2518     for( i = 0; i < 255; i++ )
2519     {
2520 #define es_descr p_iod->es_descr[i]
2521         if( es_descr.b_ok )
2522         {
2523             if( es_descr.psz_url )
2524             {
2525                 free( es_descr.psz_url );
2526                 es_descr.psz_url = NULL;
2527             }
2528             else
2529             {
2530                 free( es_descr.dec_descr.p_decoder_specific_info );
2531                 es_descr.dec_descr.p_decoder_specific_info = NULL;
2532                 es_descr.dec_descr.i_decoder_specific_info_len = 0;
2533             }
2534         }
2535         es_descr.b_ok = 0;
2536 #undef  es_descr
2537     }
2538     free( p_iod );
2539 }
2540
2541 /****************************************************************************
2542  ****************************************************************************
2543  ** libdvbpsi callbacks
2544  ****************************************************************************
2545  ****************************************************************************/
2546 static bool ProgramIsSelected( demux_t *p_demux, uint16_t i_pgrm )
2547 {
2548     demux_sys_t          *p_sys = p_demux->p_sys;
2549
2550     if( ( p_sys->i_current_program == -1 && p_sys->programs_list.i_count == 0 ) ||
2551         p_sys->i_current_program == 0 )
2552         return true;
2553     if( p_sys->i_current_program == i_pgrm )
2554         return true;
2555
2556     if( p_sys->programs_list.i_count != 0 )
2557     {
2558         for( int i = 0; i < p_sys->programs_list.i_count; i++ )
2559         {
2560             if( i_pgrm == p_sys->programs_list.p_values[i].i_int )
2561                 return true;
2562         }
2563     }
2564     return false;
2565 }
2566
2567 static void ValidateDVBMeta( demux_t *p_demux, int i_pid )
2568 {
2569     demux_sys_t *p_sys = p_demux->p_sys;
2570
2571     if( !p_sys->b_dvb_meta || ( i_pid != 0x11 && i_pid != 0x12 && i_pid != 0x14 ) )
2572         return;
2573
2574     msg_Warn( p_demux, "Switching to non DVB mode" );
2575
2576     /* This doesn't look like a DVB stream so don't try
2577      * parsing the SDT/EDT/TDT */
2578
2579     for( int i = 0x11; i <= 0x14; i++ )
2580     {
2581         if( i == 0x13 ) continue;
2582         ts_pid_t *p_pid = &p_sys->pid[i];
2583         if( p_pid->psi )
2584         {
2585             dvbpsi_DetachDemux( p_pid->psi->handle );
2586             free( p_pid->psi );
2587             p_pid->psi = NULL;
2588             p_pid->b_valid = false;
2589         }
2590         SetPIDFilter( p_demux, i, false );
2591     }
2592     p_sys->b_dvb_meta = false;
2593 }
2594
2595 #include "dvb-text.h"
2596
2597 static char *EITConvertToUTF8( const unsigned char *psz_instring,
2598                                size_t i_length,
2599                                bool b_broken )
2600 {
2601     if( b_broken )
2602         return FromCharset( "ISO_8859-1", psz_instring, i_length );
2603     return vlc_from_EIT( psz_instring, i_length );
2604 }
2605
2606 static void SDTCallBack( demux_t *p_demux, dvbpsi_sdt_t *p_sdt )
2607 {
2608     demux_sys_t          *p_sys = p_demux->p_sys;
2609     ts_pid_t             *sdt = &p_sys->pid[0x11];
2610     dvbpsi_sdt_service_t *p_srv;
2611
2612     msg_Dbg( p_demux, "SDTCallBack called" );
2613
2614     if( sdt->psi->i_sdt_version != -1 &&
2615         ( !p_sdt->b_current_next ||
2616           p_sdt->i_version == sdt->psi->i_sdt_version ) )
2617     {
2618         dvbpsi_DeleteSDT( p_sdt );
2619         return;
2620     }
2621
2622     msg_Dbg( p_demux, "new SDT ts_id=%d version=%d current_next=%d "
2623              "network_id=%d",
2624              p_sdt->i_ts_id, p_sdt->i_version, p_sdt->b_current_next,
2625              p_sdt->i_network_id );
2626
2627     p_sys->b_broken_charset = false;
2628
2629     for( p_srv = p_sdt->p_first_service; p_srv; p_srv = p_srv->p_next )
2630     {
2631         vlc_meta_t          *p_meta;
2632         dvbpsi_descriptor_t *p_dr;
2633
2634         const char *psz_type = NULL;
2635         const char *psz_status = NULL;
2636
2637         msg_Dbg( p_demux, "  * service id=%d eit schedule=%d present=%d "
2638                  "running=%d free_ca=%d",
2639                  p_srv->i_service_id, p_srv->b_eit_schedule,
2640                  p_srv->b_eit_present, p_srv->i_running_status,
2641                  p_srv->b_free_ca );
2642
2643         p_meta = vlc_meta_New();
2644         for( p_dr = p_srv->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
2645         {
2646             if( p_dr->i_tag == 0x48 )
2647             {
2648                 static const char *ppsz_type[17] = {
2649                     "Reserved",
2650                     "Digital television service",
2651                     "Digital radio sound service",
2652                     "Teletext service",
2653                     "NVOD reference service",
2654                     "NVOD time-shifted service",
2655                     "Mosaic service",
2656                     "PAL coded signal",
2657                     "SECAM coded signal",
2658                     "D/D2-MAC",
2659                     "FM Radio",
2660                     "NTSC coded signal",
2661                     "Data broadcast service",
2662                     "Reserved for Common Interface Usage",
2663                     "RCS Map (see EN 301 790 [35])",
2664                     "RCS FLS (see EN 301 790 [35])",
2665                     "DVB MHP service"
2666                 };
2667                 dvbpsi_service_dr_t *pD = dvbpsi_DecodeServiceDr( p_dr );
2668                 char *str1 = NULL;
2669                 char *str2 = NULL;
2670
2671                 /* Workarounds for broadcasters with broken EPG */
2672
2673                 if( p_sdt->i_network_id == 133 )
2674                     p_sys->b_broken_charset = true;  /* SKY DE & BetaDigital use ISO8859-1 */
2675
2676                 /* List of providers using ISO8859-1 */
2677                 static const char ppsz_broken_providers[][8] = {
2678                     "CSAT",     /* CanalSat FR */
2679                     "GR1",      /* France televisions */
2680                     "MULTI4",   /* NT1 */
2681                     "MR5",      /* France 2/M6 HD */
2682                     ""
2683                 };
2684                 for( int i = 0; *ppsz_broken_providers[i]; i++ )
2685                 {
2686                     const size_t i_length = strlen(ppsz_broken_providers[i]);
2687                     if( pD->i_service_provider_name_length == i_length &&
2688                         !strncmp( (char *)pD->i_service_provider_name, ppsz_broken_providers[i], i_length ) )
2689                         p_sys->b_broken_charset = true;
2690                 }
2691
2692                 /* FIXME: Digital+ ES also uses ISO8859-1 */
2693
2694                 str1 = EITConvertToUTF8(pD->i_service_provider_name,
2695                                         pD->i_service_provider_name_length,
2696                                         p_sys->b_broken_charset );
2697                 str2 = EITConvertToUTF8(pD->i_service_name,
2698                                         pD->i_service_name_length,
2699                                         p_sys->b_broken_charset );
2700
2701                 msg_Dbg( p_demux, "    - type=%d provider=%s name=%s",
2702                          pD->i_service_type, str1, str2 );
2703
2704                 vlc_meta_SetTitle( p_meta, str2 );
2705                 vlc_meta_SetPublisher( p_meta, str1 );
2706                 if( pD->i_service_type >= 0x01 && pD->i_service_type <= 0x10 )
2707                     psz_type = ppsz_type[pD->i_service_type];
2708                 free( str1 );
2709                 free( str2 );
2710             }
2711         }
2712
2713         if( p_srv->i_running_status >= 0x01 && p_srv->i_running_status <= 0x04 )
2714         {
2715             static const char *ppsz_status[5] = {
2716                 "Unknown",
2717                 "Not running",
2718                 "Starts in a few seconds",
2719                 "Pausing",
2720                 "Running"
2721             };
2722             psz_status = ppsz_status[p_srv->i_running_status];
2723         }
2724
2725         if( psz_type )
2726             vlc_meta_AddExtra( p_meta, "Type", psz_type );
2727         if( psz_status )
2728             vlc_meta_AddExtra( p_meta, "Status", psz_status );
2729
2730         es_out_Control( p_demux->out, ES_OUT_SET_GROUP_META,
2731                         p_srv->i_service_id, p_meta );
2732         vlc_meta_Delete( p_meta );
2733     }
2734
2735     sdt->psi->i_sdt_version = p_sdt->i_version;
2736     dvbpsi_DeleteSDT( p_sdt );
2737 }
2738
2739 /* i_year: year - 1900  i_month: 0-11  i_mday: 1-31 i_hour: 0-23 i_minute: 0-59 i_second: 0-59 */
2740 static int64_t vlc_timegm( int i_year, int i_month, int i_mday, int i_hour, int i_minute, int i_second )
2741 {
2742     static const int pn_day[12+1] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
2743     int64_t i_day;
2744     int i;
2745
2746     if( i_year < 70 ||
2747         i_month < 0 || i_month > 11 || i_mday < 1 || i_mday > 31 ||
2748         i_hour < 0 || i_hour > 23 || i_minute < 0 || i_minute > 59 || i_second < 0 || i_second > 59 )
2749         return -1;
2750
2751     /* Count the number of days */
2752     i_day = 365 * (i_year-70) + pn_day[i_month] + i_mday - 1;
2753 #define LEAP(y) ( ((y)%4) == 0 && (((y)%100) != 0 || ((y)%400) == 0) ? 1 : 0)
2754     for( i = 70; i < i_year; i++ )
2755         i_day += LEAP(1900+i);
2756     if( i_month > 1 )
2757         i_day += LEAP(1900+i_year);
2758 #undef LEAP
2759     /**/
2760     return ((24*i_day + i_hour)*60 + i_minute)*60 + i_second;
2761 }
2762
2763 static void EITDecodeMjd( int i_mjd, int *p_y, int *p_m, int *p_d )
2764 {
2765     const int yp = (int)( ( (double)i_mjd - 15078.2)/365.25 );
2766     const int mp = (int)( ((double)i_mjd - 14956.1 - (int)(yp * 365.25)) / 30.6001 );
2767     const int c = ( mp == 14 || mp == 15 ) ? 1 : 0;
2768
2769     *p_y = 1900 + yp + c*1;
2770     *p_m = mp - 1 - c*12;
2771     *p_d = i_mjd - 14956 - (int)(yp*365.25) - (int)(mp*30.6001);
2772 }
2773 #define CVT_FROM_BCD(v) ((((v) >> 4)&0xf)*10 + ((v)&0xf))
2774 static int64_t EITConvertStartTime( uint64_t i_date )
2775 {
2776     const int i_mjd = i_date >> 24;
2777     const int i_hour   = CVT_FROM_BCD(i_date >> 16);
2778     const int i_minute = CVT_FROM_BCD(i_date >>  8);
2779     const int i_second = CVT_FROM_BCD(i_date      );
2780     int i_year;
2781     int i_month;
2782     int i_day;
2783
2784     /* if all 40 bits are 1, the start is unknown */
2785     if( i_date == UINT64_C(0xffffffffff) )
2786         return -1;
2787
2788     EITDecodeMjd( i_mjd, &i_year, &i_month, &i_day );
2789     return vlc_timegm( i_year - 1900, i_month - 1, i_day, i_hour, i_minute, i_second );
2790 }
2791 static int EITConvertDuration( uint32_t i_duration )
2792 {
2793     return CVT_FROM_BCD(i_duration >> 16) * 3600 +
2794            CVT_FROM_BCD(i_duration >> 8 ) * 60 +
2795            CVT_FROM_BCD(i_duration      );
2796 }
2797 #undef CVT_FROM_BCD
2798
2799 #ifdef TS_USE_TDT
2800 static void TDTCallBack( demux_t *p_demux, dvbpsi_tot_t *p_tdt )
2801 {
2802     demux_sys_t        *p_sys = p_demux->p_sys;
2803
2804     p_sys->i_tdt_delta = CLOCK_FREQ * EITConvertStartTime( p_tdt->i_utc_time )
2805                          - mdate();
2806     dvbpsi_DeleteTOT(p_tdt);
2807 }
2808 #endif
2809
2810
2811 static void EITCallBack( demux_t *p_demux,
2812                          dvbpsi_eit_t *p_eit, bool b_current_following )
2813 {
2814     demux_sys_t        *p_sys = p_demux->p_sys;
2815     dvbpsi_eit_event_t *p_evt;
2816     vlc_epg_t *p_epg;
2817
2818     msg_Dbg( p_demux, "EITCallBack called" );
2819     if( !p_eit->b_current_next )
2820     {
2821         dvbpsi_DeleteEIT( p_eit );
2822         return;
2823     }
2824
2825     msg_Dbg( p_demux, "new EIT service_id=%d version=%d current_next=%d "
2826              "ts_id=%d network_id=%d segment_last_section_number=%d "
2827              "last_table_id=%d",
2828              p_eit->i_service_id, p_eit->i_version, p_eit->b_current_next,
2829              p_eit->i_ts_id, p_eit->i_network_id,
2830              p_eit->i_segment_last_section_number, p_eit->i_last_table_id );
2831
2832     p_epg = vlc_epg_New( NULL );
2833     for( p_evt = p_eit->p_first_event; p_evt; p_evt = p_evt->p_next )
2834     {
2835         dvbpsi_descriptor_t *p_dr;
2836         char                *psz_name = NULL;
2837         char                *psz_text = NULL;
2838         char                *psz_extra = strdup("");
2839         int64_t i_start;
2840         int i_duration;
2841
2842         i_start = EITConvertStartTime( p_evt->i_start_time );
2843         i_duration = EITConvertDuration( p_evt->i_duration );
2844
2845         msg_Dbg( p_demux, "  * event id=%d start_time:%d duration=%d "
2846                           "running=%d free_ca=%d",
2847                  p_evt->i_event_id, (int)i_start, (int)i_duration,
2848                  p_evt->i_running_status, p_evt->b_free_ca );
2849
2850         for( p_dr = p_evt->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
2851         {
2852             if( p_dr->i_tag == 0x4d )
2853             {
2854                 dvbpsi_short_event_dr_t *pE = dvbpsi_DecodeShortEventDr( p_dr );
2855
2856                 /* Only take first description, as we don't handle language-info
2857                    for epg atm*/
2858                 if( pE && psz_name == NULL)
2859                 {
2860                     psz_name = EITConvertToUTF8( pE->i_event_name, pE->i_event_name_length,
2861                                                  p_sys->b_broken_charset );
2862                     psz_text = EITConvertToUTF8( pE->i_text, pE->i_text_length,
2863                                                  p_sys->b_broken_charset );
2864                     msg_Dbg( p_demux, "    - short event lang=%3.3s '%s' : '%s'",
2865                              pE->i_iso_639_code, psz_name, psz_text );
2866                 }
2867             }
2868             else if( p_dr->i_tag == 0x4e )
2869             {
2870                 dvbpsi_extended_event_dr_t *pE = dvbpsi_DecodeExtendedEventDr( p_dr );
2871                 if( pE )
2872                 {
2873                     msg_Dbg( p_demux, "    - extended event lang=%3.3s [%d/%d]",
2874                              pE->i_iso_639_code,
2875                              pE->i_descriptor_number, pE->i_last_descriptor_number );
2876
2877                     if( pE->i_text_length > 0 )
2878                     {
2879                         char *psz_text = EITConvertToUTF8( pE->i_text, pE->i_text_length,
2880                                                            p_sys->b_broken_charset );
2881                         if( psz_text )
2882                         {
2883                             msg_Dbg( p_demux, "       - text='%s'", psz_text );
2884
2885                             psz_extra = xrealloc( psz_extra,
2886                                    strlen(psz_extra) + strlen(psz_text) + 1 );
2887                             strcat( psz_extra, psz_text );
2888                             free( psz_text );
2889                         }
2890                     }
2891
2892                     for( int i = 0; i < pE->i_entry_count; i++ )
2893                     {
2894                         char *psz_dsc = EITConvertToUTF8( pE->i_item_description[i],
2895                                                           pE->i_item_description_length[i],
2896                                                           p_sys->b_broken_charset );
2897                         char *psz_itm = EITConvertToUTF8( pE->i_item[i], pE->i_item_length[i],
2898                                                           p_sys->b_broken_charset );
2899
2900                         if( psz_dsc && psz_itm )
2901                         {
2902                             msg_Dbg( p_demux, "       - desc='%s' item='%s'", psz_dsc, psz_itm );
2903 #if 0
2904                             psz_extra = xrealloc( psz_extra,
2905                                          strlen(psz_extra) + strlen(psz_dsc) +
2906                                          strlen(psz_itm) + 3 + 1 );
2907                             strcat( psz_extra, "(" );
2908                             strcat( psz_extra, psz_dsc );
2909                             strcat( psz_extra, " " );
2910                             strcat( psz_extra, psz_itm );
2911                             strcat( psz_extra, ")" );
2912 #endif
2913                         }
2914                         free( psz_dsc );
2915                         free( psz_itm );
2916                     }
2917                 }
2918             }
2919             else
2920             {
2921                 msg_Dbg( p_demux, "    - tag=0x%x(%d)", p_dr->i_tag, p_dr->i_tag );
2922             }
2923         }
2924
2925         /* */
2926         if( i_start > 0 )
2927             vlc_epg_AddEvent( p_epg, i_start, i_duration, psz_name, psz_text,
2928                               *psz_extra ? psz_extra : NULL );
2929
2930         /* Update "now playing" field */
2931         if( p_evt->i_running_status == 0x04 && i_start > 0 )
2932             vlc_epg_SetCurrent( p_epg, i_start );
2933
2934         free( psz_name );
2935         free( psz_text );
2936
2937         free( psz_extra );
2938     }
2939     if( p_epg->i_event > 0 )
2940     {
2941         if( b_current_following &&
2942             (  p_sys->i_current_program == -1 ||
2943                p_sys->i_current_program == p_eit->i_service_id ) )
2944         {
2945             p_sys->i_dvb_length = 0;
2946             p_sys->i_dvb_start = 0;
2947
2948             if( p_epg->p_current )
2949             {
2950                 p_sys->i_dvb_start = CLOCK_FREQ * p_epg->p_current->i_start;
2951                 p_sys->i_dvb_length = CLOCK_FREQ * p_epg->p_current->i_duration;
2952             }
2953         }
2954         es_out_Control( p_demux->out, ES_OUT_SET_GROUP_EPG, p_eit->i_service_id, p_epg );
2955     }
2956     vlc_epg_Delete( p_epg );
2957
2958     dvbpsi_DeleteEIT( p_eit );
2959 }
2960 static void EITCallBackCurrentFollowing( demux_t *p_demux, dvbpsi_eit_t *p_eit )
2961 {
2962     EITCallBack( p_demux, p_eit, true );
2963 }
2964 static void EITCallBackSchedule( demux_t *p_demux, dvbpsi_eit_t *p_eit )
2965 {
2966     EITCallBack( p_demux, p_eit, false );
2967 }
2968
2969 static void PSINewTableCallBack( demux_t *p_demux, dvbpsi_handle h,
2970                                  uint8_t  i_table_id, uint16_t i_extension )
2971 {
2972 #if 0
2973     msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
2974              i_table_id, i_table_id, i_extension, i_extension );
2975 #endif
2976     if( p_demux->p_sys->pid[0].psi->i_pat_version != -1 && i_table_id == 0x42 )
2977     {
2978         msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
2979                  i_table_id, i_table_id, i_extension, i_extension );
2980
2981         dvbpsi_AttachSDT( h, i_table_id, i_extension,
2982                           (dvbpsi_sdt_callback)SDTCallBack, p_demux );
2983     }
2984     else if( p_demux->p_sys->pid[0x11].psi->i_sdt_version != -1 &&
2985              ( i_table_id == 0x4e || /* Current/Following */
2986                (i_table_id >= 0x50 && i_table_id <= 0x5f) ) ) /* Schedule */
2987     {
2988         msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
2989                  i_table_id, i_table_id, i_extension, i_extension );
2990
2991         dvbpsi_eit_callback cb = i_table_id == 0x4e ?
2992                                     (dvbpsi_eit_callback)EITCallBackCurrentFollowing :
2993                                     (dvbpsi_eit_callback)EITCallBackSchedule;
2994         dvbpsi_AttachEIT( h, i_table_id, i_extension, cb, p_demux );
2995     }
2996 #ifdef TS_USE_TDT
2997     else if( p_demux->p_sys->pid[0x11].psi->i_sdt_version != -1 &&
2998               i_table_id == 0x70 )  /* TDT */
2999     {
3000          msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3001                  i_table_id, i_table_id, i_extension, i_extension );
3002          dvbpsi_AttachTOT( h, i_table_id, i_extension,
3003                            (dvbpsi_tot_callback)TDTCallBack, p_demux);
3004     }
3005 #endif
3006
3007 }
3008
3009 /*****************************************************************************
3010  * PMT callback and helpers
3011  *****************************************************************************/
3012 static dvbpsi_descriptor_t *PMTEsFindDescriptor( const dvbpsi_pmt_es_t *p_es,
3013                                                  int i_tag )
3014 {
3015     dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
3016     while( p_dr && ( p_dr->i_tag != i_tag ) )
3017         p_dr = p_dr->p_next;
3018     return p_dr;
3019 }
3020 static bool PMTEsHasRegistration( demux_t *p_demux,
3021                                   const dvbpsi_pmt_es_t *p_es,
3022                                   const char *psz_tag )
3023 {
3024     dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x05 );
3025     if( !p_dr )
3026         return false;
3027
3028     if( p_dr->i_length < 4 )
3029     {
3030         msg_Warn( p_demux, "invalid Registration Descriptor" );
3031         return false;
3032     }
3033
3034     assert( strlen(psz_tag) == 4 );
3035     return !memcmp( p_dr->p_data, psz_tag, 4 );
3036 }
3037 static void PMTSetupEsISO14496( demux_t *p_demux, ts_pid_t *pid,
3038                                 const ts_prg_psi_t *prg, const dvbpsi_pmt_es_t *p_es )
3039 {
3040     es_format_t *p_fmt = &pid->es->fmt;
3041
3042     /* MPEG-4 stream: search SL_DESCRIPTOR */
3043     dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x1f );
3044
3045     if( p_dr && p_dr->i_length == 2 )
3046     {
3047         const int i_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
3048
3049         msg_Warn( p_demux, "found SL_descriptor es_id=%d", i_es_id );
3050
3051         pid->es->p_mpeg4desc = NULL;
3052
3053         for( int i = 0; i < 255; i++ )
3054         {
3055             iod_descriptor_t *iod = prg->iod;
3056
3057             if( iod->es_descr[i].b_ok &&
3058                 iod->es_descr[i].i_es_id == i_es_id )
3059             {
3060                 pid->es->p_mpeg4desc = &iod->es_descr[i];
3061                 break;
3062             }
3063         }
3064     }
3065     if( !pid->es->p_mpeg4desc )
3066     {
3067         msg_Err( p_demux, "MPEG-4 descriptor not found" );
3068         return;
3069     }
3070
3071     const decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;
3072     if( dcd->i_streamType == 0x04 )    /* VisualStream */
3073     {
3074         p_fmt->i_cat = VIDEO_ES;
3075         switch( dcd->i_objectTypeIndication )
3076         {
3077         case 0x0B: /* mpeg4 sub */
3078             p_fmt->i_cat = SPU_ES;
3079             p_fmt->i_codec = VLC_CODEC_SUBT;
3080             break;
3081
3082         case 0x20: /* mpeg4 */
3083             p_fmt->i_codec = VLC_CODEC_MP4V;
3084             break;
3085         case 0x21: /* h264 */
3086             p_fmt->i_codec = VLC_CODEC_H264;
3087             break;
3088         case 0x60:
3089         case 0x61:
3090         case 0x62:
3091         case 0x63:
3092         case 0x64:
3093         case 0x65: /* mpeg2 */
3094             p_fmt->i_codec = VLC_CODEC_MPGV;
3095             break;
3096         case 0x6a: /* mpeg1 */
3097             p_fmt->i_codec = VLC_CODEC_MPGV;
3098             break;
3099         case 0x6c: /* mpeg1 */
3100             p_fmt->i_codec = VLC_CODEC_JPEG;
3101             break;
3102         default:
3103             p_fmt->i_cat = UNKNOWN_ES;
3104             break;
3105         }
3106     }
3107     else if( dcd->i_streamType == 0x05 )    /* AudioStream */
3108     {
3109         p_fmt->i_cat = AUDIO_ES;
3110         switch( dcd->i_objectTypeIndication )
3111         {
3112         case 0x40: /* mpeg4 */
3113             p_fmt->i_codec = VLC_CODEC_MP4A;
3114             break;
3115         case 0x66:
3116         case 0x67:
3117         case 0x68: /* mpeg2 aac */
3118             p_fmt->i_codec = VLC_CODEC_MP4A;
3119             break;
3120         case 0x69: /* mpeg2 */
3121             p_fmt->i_codec = VLC_CODEC_MPGA;
3122             break;
3123         case 0x6b: /* mpeg1 */
3124             p_fmt->i_codec = VLC_CODEC_MPGA;
3125             break;
3126         default:
3127             p_fmt->i_cat = UNKNOWN_ES;
3128             break;
3129         }
3130     }
3131     else
3132     {
3133         p_fmt->i_cat = UNKNOWN_ES;
3134     }
3135
3136     if( p_fmt->i_cat != UNKNOWN_ES )
3137     {
3138         p_fmt->i_extra = dcd->i_decoder_specific_info_len;
3139         if( p_fmt->i_extra > 0 )
3140         {
3141             p_fmt->p_extra = malloc( p_fmt->i_extra );
3142             if( p_fmt->p_extra )
3143                 memcpy( p_fmt->p_extra,
3144                         dcd->p_decoder_specific_info,
3145                         p_fmt->i_extra );
3146             else
3147                 p_fmt->i_extra = 0;
3148         }
3149     }
3150 }
3151
3152 typedef struct
3153 {
3154     int  i_type;
3155     int  i_magazine;
3156     int  i_page;
3157     char p_iso639[3];
3158 } ts_teletext_page_t;
3159
3160 static void PMTSetupEsTeletext( demux_t *p_demux, ts_pid_t *pid,
3161                                 const dvbpsi_pmt_es_t *p_es )
3162 {
3163     es_format_t *p_fmt = &pid->es->fmt;
3164
3165     ts_teletext_page_t p_page[2 * 64 + 20];
3166     unsigned i_page = 0;
3167
3168     /* Gather pages information */
3169 #if defined _DVBPSI_DR_56_H_ && \
3170     defined DVBPSI_VERSION && DVBPSI_VERSION_INT > ((0<<16)+(1<<8)+5)
3171     for( unsigned i_tag_idx = 0; i_tag_idx < 2; i_tag_idx++ )
3172     {
3173         dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, i_tag_idx == 0 ? 0x46 : 0x56 );
3174         if( !p_dr )
3175             continue;
3176
3177         dvbpsi_teletext_dr_t *p_sub = dvbpsi_DecodeTeletextDr( p_dr );
3178         if( !p_sub )
3179             continue;
3180
3181         for( int i = 0; i < p_sub->i_pages_number; i++ )
3182         {
3183             const dvbpsi_teletextpage_t *p_src = &p_sub->p_pages[i];
3184
3185             if( p_src->i_teletext_type >= 0x06 )
3186                 continue;
3187
3188             assert( i_page < sizeof(p_page)/sizeof(*p_page) );
3189
3190             ts_teletext_page_t *p_dst = &p_page[i_page++];
3191
3192             p_dst->i_type = p_src->i_teletext_type;
3193             p_dst->i_magazine = p_src->i_teletext_magazine_number
3194                 ? p_src->i_teletext_magazine_number : 8;
3195             p_dst->i_page = p_src->i_teletext_page_number;
3196             memcpy( p_dst->p_iso639, p_src->i_iso6392_language_code, 3 );
3197         }
3198     }
3199 #endif
3200
3201 #ifdef _DVBPSI_DR_59_H_
3202     dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x59 );
3203     if( p_dr )
3204     {
3205         dvbpsi_subtitling_dr_t *p_sub = dvbpsi_DecodeSubtitlingDr( p_dr );
3206         for( int i = 0; p_sub && i < p_sub->i_subtitles_number; i++ )
3207         {
3208             dvbpsi_subtitle_t *p_src = &p_sub->p_subtitle[i];
3209
3210             if( p_src->i_subtitling_type < 0x01 || p_src->i_subtitling_type > 0x03 )
3211                 continue;
3212
3213             assert( i_page < sizeof(p_page)/sizeof(*p_page) );
3214
3215             ts_teletext_page_t *p_dst = &p_page[i_page++];
3216
3217             switch( p_src->i_subtitling_type )
3218             {
3219             case 0x01:
3220                 p_dst->i_type = 0x02;
3221                 break;
3222             default:
3223                 p_dst->i_type = 0x03;
3224                 break;
3225             }
3226             /* FIXME check if it is the right split */
3227             p_dst->i_magazine = (p_src->i_composition_page_id >> 8)
3228                 ? (p_src->i_composition_page_id >> 8) : 8;
3229             p_dst->i_page = p_src->i_composition_page_id & 0xff;
3230             memcpy( p_dst->p_iso639, p_src->i_iso6392_language_code, 3 );
3231         }
3232     }
3233 #endif
3234
3235     /* */
3236     es_format_Init( p_fmt, SPU_ES, VLC_CODEC_TELETEXT );
3237
3238     if( !p_demux->p_sys->b_split_es || i_page <= 0 )
3239     {
3240         p_fmt->subs.teletext.i_magazine = -1;
3241         p_fmt->subs.teletext.i_page = 0;
3242         p_fmt->psz_description = strdup( vlc_gettext(ppsz_teletext_type[1]) );
3243
3244         dvbpsi_descriptor_t *p_dr;
3245         p_dr = PMTEsFindDescriptor( p_es, 0x46 );
3246         if( !p_dr )
3247             p_dr = PMTEsFindDescriptor( p_es, 0x56 );
3248
3249         if( !p_demux->p_sys->b_split_es && p_dr && p_dr->i_length > 0 )
3250         {
3251             /* Descriptor pass-through */
3252             p_fmt->p_extra = malloc( p_dr->i_length );
3253             if( p_fmt->p_extra )
3254             {
3255                 p_fmt->i_extra = p_dr->i_length;
3256                 memcpy( p_fmt->p_extra, p_dr->p_data, p_dr->i_length );
3257             }
3258         }
3259     }
3260     else
3261     {
3262         for( unsigned i = 0; i < i_page; i++ )
3263         {
3264             ts_es_t *p_es;
3265
3266             /* */
3267             if( i == 0 )
3268             {
3269                 p_es = pid->es;
3270             }
3271             else
3272             {
3273                 p_es = malloc( sizeof(*p_es) );
3274                 if( !p_es )
3275                     break;
3276
3277                 es_format_Copy( &p_es->fmt, &pid->es->fmt );
3278                 free( p_es->fmt.psz_language );
3279                 free( p_es->fmt.psz_description );
3280                 p_es->fmt.psz_language = NULL;
3281                 p_es->fmt.psz_description = NULL;
3282
3283                 p_es->id      = NULL;
3284                 p_es->p_pes   = NULL;
3285                 p_es->i_pes_size = 0;
3286                 p_es->i_pes_gathered = 0;
3287                 p_es->pp_last = &p_es->p_pes;
3288                 p_es->p_mpeg4desc = NULL;
3289                 p_es->b_gather = false;
3290
3291                 TAB_APPEND( pid->i_extra_es, pid->extra_es, p_es );
3292             }
3293
3294             /* */
3295             const ts_teletext_page_t *p = &p_page[i];
3296             p_es->fmt.i_priority = (p->i_type == 0x02 || p->i_type == 0x05) ? 0 : -1;
3297             p_es->fmt.psz_language = strndup( p->p_iso639, 3 );
3298             p_es->fmt.psz_description = strdup(vlc_gettext(ppsz_teletext_type[p->i_type]));
3299             p_es->fmt.subs.teletext.i_magazine = p->i_magazine;
3300             p_es->fmt.subs.teletext.i_page = p->i_page;
3301
3302             msg_Dbg( p_demux,
3303                          "    * ttxt type=%s lan=%s page=%d%02x",
3304                          p_es->fmt.psz_description,
3305                          p_es->fmt.psz_language,
3306                          p->i_magazine, p->i_page );
3307         }
3308     }
3309 }
3310 static void PMTSetupEsDvbSubtitle( demux_t *p_demux, ts_pid_t *pid,
3311                                    const dvbpsi_pmt_es_t *p_es )
3312 {
3313     es_format_t *p_fmt = &pid->es->fmt;
3314
3315     es_format_Init( p_fmt, SPU_ES, VLC_CODEC_DVBS );
3316
3317     dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x59 );
3318     int i_page = 0;
3319 #ifdef _DVBPSI_DR_59_H_
3320     dvbpsi_subtitling_dr_t *p_sub = dvbpsi_DecodeSubtitlingDr( p_dr );
3321     for( int i = 0; p_sub && i < p_sub->i_subtitles_number; i++ )
3322     {
3323         const int i_type = p_sub->p_subtitle[i].i_subtitling_type;
3324         if( ( i_type >= 0x10 && i_type <= 0x14 ) ||
3325             ( i_type >= 0x20 && i_type <= 0x24 ) )
3326             i_page++;
3327     }
3328 #endif
3329
3330     if( !p_demux->p_sys->b_split_es  || i_page <= 0 )
3331     {
3332         p_fmt->subs.dvb.i_id = -1;
3333         p_fmt->psz_description = strdup( _("DVB subtitles") );
3334
3335         if( !p_demux->p_sys->b_split_es && p_dr && p_dr->i_length > 0 )
3336         {
3337             /* Descriptor pass-through */
3338             p_fmt->p_extra = malloc( p_dr->i_length );
3339             if( p_fmt->p_extra )
3340             {
3341                 p_fmt->i_extra = p_dr->i_length;
3342                 memcpy( p_fmt->p_extra, p_dr->p_data, p_dr->i_length );
3343             }
3344         }
3345     }
3346     else
3347     {
3348 #ifdef _DVBPSI_DR_59_H_
3349         for( int i = 0; i < p_sub->i_subtitles_number; i++ )
3350         {
3351             ts_es_t *p_es;
3352
3353             /* */
3354             if( i == 0 )
3355             {
3356                 p_es = pid->es;
3357             }
3358             else
3359             {
3360                 p_es = malloc( sizeof(*p_es) );
3361                 if( !p_es )
3362                     break;
3363
3364                 es_format_Copy( &p_es->fmt, &pid->es->fmt );
3365                 free( p_es->fmt.psz_language );
3366                 free( p_es->fmt.psz_description );
3367                 p_es->fmt.psz_language = NULL;
3368                 p_es->fmt.psz_description = NULL;
3369
3370                 p_es->id      = NULL;
3371                 p_es->p_pes   = NULL;
3372                 p_es->i_pes_size = 0;
3373                 p_es->i_pes_gathered = 0;
3374                 p_es->pp_last = &p_es->p_pes;
3375                 p_es->p_mpeg4desc = NULL;
3376                 p_es->b_gather = false;
3377
3378                 TAB_APPEND( pid->i_extra_es, pid->extra_es, p_es );
3379             }
3380
3381             /* */
3382             const dvbpsi_subtitle_t *p = &p_sub->p_subtitle[i];
3383             p_es->fmt.psz_language = strndup( (char *)p->i_iso6392_language_code, 3 );
3384             switch( p->i_subtitling_type )
3385             {
3386             case 0x10: /* unspec. */
3387             case 0x11: /* 4:3 */
3388             case 0x12: /* 16:9 */
3389             case 0x13: /* 2.21:1 */
3390             case 0x14: /* HD monitor */
3391                 p_es->fmt.psz_description = strdup( _("DVB subtitles") );
3392                 break;
3393             case 0x20: /* Hearing impaired unspec. */
3394             case 0x21: /* h.i. 4:3 */
3395             case 0x22: /* h.i. 16:9 */
3396             case 0x23: /* h.i. 2.21:1 */
3397             case 0x24: /* h.i. HD monitor */
3398                 p_es->fmt.psz_description = strdup( _("DVB subtitles: hearing impaired") );
3399                 break;
3400             default:
3401                 break;
3402             }
3403
3404             /* Hack, FIXME */
3405             p_es->fmt.subs.dvb.i_id = ( p->i_composition_page_id <<  0 ) |
3406                                       ( p->i_ancillary_page_id   << 16 );
3407         }
3408 #endif
3409     }
3410 }
3411 static void PMTSetupEs0x06( demux_t *p_demux, ts_pid_t *pid,
3412                             const dvbpsi_pmt_es_t *p_es )
3413 {
3414     es_format_t *p_fmt = &pid->es->fmt;
3415
3416     if( PMTEsHasRegistration( p_demux, p_es, "AC-3" ) ||
3417         PMTEsFindDescriptor( p_es, 0x6a ) ||
3418         PMTEsFindDescriptor( p_es, 0x81 ) )
3419     {
3420         p_fmt->i_cat = AUDIO_ES;
3421         p_fmt->i_codec = VLC_CODEC_A52;
3422     }
3423     else if( PMTEsFindDescriptor( p_es, 0x7a ) )
3424     {
3425         /* DVB with stream_type 0x06 (ETS EN 300 468) */
3426         p_fmt->i_cat = AUDIO_ES;
3427         p_fmt->i_codec = VLC_CODEC_EAC3;
3428     }
3429     else if( PMTEsHasRegistration( p_demux, p_es, "DTS1" ) ||
3430              PMTEsHasRegistration( p_demux, p_es, "DTS2" ) ||
3431              PMTEsHasRegistration( p_demux, p_es, "DTS3" ) ||
3432              PMTEsFindDescriptor( p_es, 0x73 ) )
3433     {
3434         /*registration descriptor(ETSI TS 101 154 Annex F)*/
3435         p_fmt->i_cat = AUDIO_ES;
3436         p_fmt->i_codec = VLC_CODEC_DTS;
3437     }
3438     else if( PMTEsHasRegistration( p_demux, p_es, "BSSD" ) )
3439     {
3440         p_fmt->i_cat = AUDIO_ES;
3441         p_fmt->b_packetized = true;
3442         p_fmt->i_codec = VLC_CODEC_302M;
3443     }
3444     else
3445     {
3446         /* Subtitle/Teletext/VBI fallbacks */
3447         dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x59 );
3448
3449 #ifdef _DVBPSI_DR_59_H_
3450         dvbpsi_subtitling_dr_t *p_sub;
3451         if( p_dr && ( p_sub = dvbpsi_DecodeSubtitlingDr( p_dr ) ) )
3452         {
3453             for( int i = 0; i < p_sub->i_subtitles_number; i++ )
3454             {
3455                 if( p_fmt->i_cat != UNKNOWN_ES )
3456                     break;
3457
3458                 switch( p_sub->p_subtitle[i].i_subtitling_type )
3459                 {
3460                 case 0x01: /* EBU Teletext subtitles */
3461                 case 0x02: /* Associated EBU Teletext */
3462                 case 0x03: /* VBI data */
3463                     PMTSetupEsTeletext( p_demux, pid, p_es );
3464                     break;
3465                 case 0x10: /* DVB Subtitle (normal) with no monitor AR critical */
3466                 case 0x11: /*                 ...   on 4:3 AR monitor */
3467                 case 0x12: /*                 ...   on 16:9 AR monitor */
3468                 case 0x13: /*                 ...   on 2.21:1 AR monitor */
3469                 case 0x14: /*                 ...   for display on a high definition monitor */
3470                 case 0x20: /* DVB Subtitle (impaired) with no monitor AR critical */
3471                 case 0x21: /*                 ...   on 4:3 AR monitor */
3472                 case 0x22: /*                 ...   on 16:9 AR monitor */
3473                 case 0x23: /*                 ...   on 2.21:1 AR monitor */
3474                 case 0x24: /*                 ...   for display on a high definition monitor */
3475                     PMTSetupEsDvbSubtitle( p_demux, pid, p_es );
3476                     break;
3477                 default:
3478                     msg_Err( p_demux, "Unrecognized DVB subtitle type (0x%x)",
3479                              p_sub->p_subtitle[i].i_subtitling_type );
3480                     break;
3481                 }
3482             }
3483         }
3484 #else
3485         if( p_fmt->i_cat == UNKNOWN_ES && p_dr )
3486             PMTSetupEsDvbSubtitle( p_demux, pid, p_es );
3487 #endif
3488         if( p_fmt->i_cat == UNKNOWN_ES &&
3489             ( PMTEsFindDescriptor( p_es, 0x45 ) ||  /* VBI Data descriptor */
3490               PMTEsFindDescriptor( p_es, 0x46 ) ||  /* VBI Teletext descriptor */
3491               PMTEsFindDescriptor( p_es, 0x56 ) ) ) /* EBU Teletext descriptor */
3492         {
3493             /* Teletext/VBI */
3494             PMTSetupEsTeletext( p_demux, pid, p_es );
3495         }
3496     }
3497
3498 #ifdef _DVBPSI_DR_52_H_
3499     /* FIXME is it useful ? */
3500     if( PMTEsFindDescriptor( p_es, 0x52 ) )
3501     {
3502         dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x52 );
3503         dvbpsi_stream_identifier_dr_t *p_si = dvbpsi_DecodeStreamIdentifierDr( p_dr );
3504
3505         msg_Dbg( p_demux, "    * Stream Component Identifier: %d", p_si->i_component_tag );
3506     }
3507 #endif
3508 }
3509
3510 static void PMTSetupEs0xEA( demux_t *p_demux, ts_pid_t *pid,
3511                            const dvbpsi_pmt_es_t *p_es )
3512 {
3513     /* Registration Descriptor */
3514     if( !PMTEsHasRegistration( p_demux, p_es, "VC-1" ) )
3515     {
3516         msg_Err( p_demux, "Registration descriptor not found or invalid" );
3517         return;
3518     }
3519
3520     es_format_t *p_fmt = &pid->es->fmt;
3521
3522     /* registration descriptor for VC-1 (SMPTE rp227) */
3523     p_fmt->i_cat = VIDEO_ES;
3524     p_fmt->i_codec = VLC_CODEC_VC1;
3525
3526     /* XXX With Simple and Main profile the SEQUENCE
3527      * header is modified: video width and height are
3528      * inserted just after the start code as 2 int16_t
3529      * The packetizer will take care of that. */
3530 }
3531
3532 static void PMTSetupEs0xD1( demux_t *p_demux, ts_pid_t *pid,
3533                            const dvbpsi_pmt_es_t *p_es )
3534 {
3535     /* Registration Descriptor */
3536     if( !PMTEsHasRegistration( p_demux, p_es, "drac" ) )
3537     {
3538         msg_Err( p_demux, "Registration descriptor not found or invalid" );
3539         return;
3540     }
3541
3542     es_format_t *p_fmt = &pid->es->fmt;
3543
3544     /* registration descriptor for Dirac
3545      * (backwards compatable with VC-2 (SMPTE Sxxxx:2008)) */
3546     p_fmt->i_cat = VIDEO_ES;
3547     p_fmt->i_codec = VLC_CODEC_DIRAC;
3548 }
3549
3550 static void PMTSetupEs0xA0( demux_t *p_demux, ts_pid_t *pid,
3551                            const dvbpsi_pmt_es_t *p_es )
3552 {
3553     /* MSCODEC sent by vlc */
3554     dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0xa0 );
3555     if( !p_dr || p_dr->i_length < 10 )
3556     {
3557         msg_Warn( p_demux,
3558                   "private MSCODEC (vlc) without bih private descriptor" );
3559         return;
3560     }
3561
3562     es_format_t *p_fmt = &pid->es->fmt;
3563     p_fmt->i_cat = VIDEO_ES;
3564     p_fmt->i_codec = VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
3565                                  p_dr->p_data[2], p_dr->p_data[3] );
3566     p_fmt->video.i_width = GetWBE( &p_dr->p_data[4] );
3567     p_fmt->video.i_height = GetWBE( &p_dr->p_data[6] );
3568     p_fmt->i_extra = GetWBE( &p_dr->p_data[8] );
3569
3570     if( p_fmt->i_extra > 0 )
3571     {
3572         p_fmt->p_extra = malloc( p_fmt->i_extra );
3573         if( p_fmt->p_extra )
3574             memcpy( p_fmt->p_extra, &p_dr->p_data[10],
3575                     __MIN( p_fmt->i_extra, p_dr->i_length - 10 ) );
3576         else
3577             p_fmt->i_extra = 0;
3578     }
3579     /* For such stream we will gather them ourself and don't launch a
3580      * packetizer.
3581      * Yes it's ugly but it's the only way to have DIV3 working */
3582     p_fmt->b_packetized = true;
3583 }
3584
3585 static void PMTSetupEsHDMV( demux_t *p_demux, ts_pid_t *pid,
3586                            const dvbpsi_pmt_es_t *p_es )
3587 {
3588     VLC_UNUSED(p_demux);
3589     es_format_t *p_fmt = &pid->es->fmt;
3590
3591     /* Blu-Ray mapping */
3592     switch( p_es->i_type )
3593     {
3594     case 0x80:
3595         p_fmt->i_cat = AUDIO_ES;
3596         p_fmt->i_codec = VLC_CODEC_BD_LPCM;
3597         break;
3598     case 0x82:
3599     case 0x85: /* DTS-HD High resolution audio */
3600     case 0x86: /* DTS-HD Master audio */
3601     case 0xA2: /* Secondary DTS audio */
3602         p_fmt->i_cat = AUDIO_ES;
3603         p_fmt->i_codec = VLC_CODEC_DTS;
3604         break;
3605
3606     case 0x83: /* TrueHD AC3 */
3607         p_fmt->i_cat = AUDIO_ES;
3608         p_fmt->i_codec = VLC_CODEC_TRUEHD;
3609         break;
3610
3611     case 0x84: /* E-AC3 */
3612     case 0xA1: /* Secondary E-AC3 */
3613         p_fmt->i_cat = AUDIO_ES;
3614         p_fmt->i_codec = VLC_CODEC_EAC3;
3615         break;
3616     case 0x90: /* Presentation graphics */
3617         p_fmt->i_cat = SPU_ES;
3618         p_fmt->i_codec = VLC_CODEC_BD_PG;
3619         break;
3620     case 0x91: /* Interactive graphics */
3621     case 0x92: /* Subtitle */
3622     default:
3623         break;
3624     }
3625 }
3626
3627 static void PMTSetupEsRegistration( demux_t *p_demux, ts_pid_t *pid,
3628                                     const dvbpsi_pmt_es_t *p_es )
3629 {
3630     static const struct
3631     {
3632         char         psz_tag[5];
3633         int          i_cat;
3634         vlc_fourcc_t i_codec;
3635     } p_regs[] = {
3636         { "AC-3", AUDIO_ES, VLC_CODEC_A52   },
3637         { "DTS1", AUDIO_ES, VLC_CODEC_DTS   },
3638         { "DTS2", AUDIO_ES, VLC_CODEC_DTS   },
3639         { "DTS3", AUDIO_ES, VLC_CODEC_DTS   },
3640         { "BSSD", AUDIO_ES, VLC_CODEC_302M  },
3641         { "VC-1", VIDEO_ES, VLC_CODEC_VC1   },
3642         { "drac", VIDEO_ES, VLC_CODEC_DIRAC },
3643         { "", UNKNOWN_ES, 0 }
3644     };
3645     es_format_t *p_fmt = &pid->es->fmt;
3646
3647     for( int i = 0; p_regs[i].i_cat != UNKNOWN_ES; i++ )
3648     {
3649         if( PMTEsHasRegistration( p_demux, p_es, p_regs[i].psz_tag ) )
3650         {
3651             p_fmt->i_cat   = p_regs[i].i_cat;
3652             p_fmt->i_codec = p_regs[i].i_codec;
3653             break;
3654         }
3655     }
3656 }
3657
3658 static void PMTParseEsIso639( demux_t *p_demux, ts_pid_t *pid,
3659                               const dvbpsi_pmt_es_t *p_es )
3660 {
3661     /* get language descriptor */
3662     dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x0a );
3663
3664     if( !p_dr )
3665         return;
3666
3667     dvbpsi_iso639_dr_t *p_decoded = dvbpsi_DecodeISO639Dr( p_dr );
3668     if( !p_decoded )
3669     {
3670         msg_Err( p_demux, "Failed to decode a ISO 639 descriptor" );
3671         return;
3672     }
3673
3674 #if defined(DR_0A_API_VER) && (DR_0A_API_VER >= 2)
3675     pid->es->fmt.psz_language = malloc( 4 );
3676     if( pid->es->fmt.psz_language )
3677     {
3678         memcpy( pid->es->fmt.psz_language,
3679                 p_decoded->code[0].iso_639_code, 3 );
3680         pid->es->fmt.psz_language[3] = 0;
3681         msg_Dbg( p_demux, "found language: %s", pid->es->fmt.psz_language);
3682     }
3683     switch( p_decoded->code[0].i_audio_type )
3684     {
3685     case 0:
3686         pid->es->fmt.i_priority = 1; // prioritize normal audio tracks
3687         pid->es->fmt.psz_description = NULL;
3688         break;
3689     case 1:
3690         pid->es->fmt.psz_description =
3691             strdup(_("clean effects"));
3692         break;
3693     case 2:
3694         pid->es->fmt.psz_description =
3695             strdup(_("hearing impaired"));
3696         break;
3697     case 3:
3698         pid->es->fmt.psz_description =
3699             strdup(_("visual impaired commentary"));
3700         break;
3701     default:
3702         msg_Dbg( p_demux, "unknown audio type: %d",
3703                  p_decoded->code[0].i_audio_type);
3704         pid->es->fmt.psz_description = NULL;
3705         break;
3706     }
3707     pid->es->fmt.i_extra_languages = p_decoded->i_code_count-1;
3708     if( pid->es->fmt.i_extra_languages > 0 )
3709         pid->es->fmt.p_extra_languages =
3710             malloc( sizeof(*pid->es->fmt.p_extra_languages) *
3711                     pid->es->fmt.i_extra_languages );
3712     if( pid->es->fmt.p_extra_languages )
3713     {
3714         for( int i = 0; i < pid->es->fmt.i_extra_languages; i++ )
3715         {
3716             msg_Dbg( p_demux, "bang" );
3717             pid->es->fmt.p_extra_languages[i].psz_language =
3718                 malloc(4);
3719             if( pid->es->fmt.p_extra_languages[i].psz_language )
3720             {
3721                 memcpy( pid->es->fmt.p_extra_languages[i].psz_language,
3722                     p_decoded->code[i+1].iso_639_code, 3 );
3723                 pid->es->fmt.p_extra_languages[i].psz_language[3] = '\0';
3724             }
3725             switch( p_decoded->code[i].i_audio_type )
3726             {
3727             case 0:
3728                 pid->es->fmt.p_extra_languages[i].psz_description =
3729                     NULL;
3730                 break;
3731             case 1:
3732                 pid->es->fmt.p_extra_languages[i].psz_description =
3733                     strdup(_("clean effects"));
3734                 break;
3735             case 2:
3736                 pid->es->fmt.p_extra_languages[i].psz_description =
3737                     strdup(_("hearing impaired"));
3738                 break;
3739             case 3:
3740                 pid->es->fmt.p_extra_languages[i].psz_description =
3741                     strdup(_("visual impaired commentary"));
3742                 break;
3743             default:
3744                 msg_Dbg( p_demux, "unknown audio type: %d",
3745                         p_decoded->code[i].i_audio_type);
3746                 pid->es->fmt.psz_description = NULL;
3747                 break;
3748             }
3749
3750         }
3751     }
3752 #else
3753     pid->es->fmt.psz_language = malloc( 4 );
3754     if( pid->es->fmt.psz_language )
3755     {
3756         memcpy( pid->es->fmt.psz_language,
3757                 p_decoded->i_iso_639_code, 3 );
3758         pid->es->fmt.psz_language[3] = 0;
3759     }
3760 #endif
3761 }
3762
3763 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt )
3764 {
3765     demux_sys_t          *p_sys = p_demux->p_sys;
3766     dvbpsi_descriptor_t  *p_dr;
3767     dvbpsi_pmt_es_t      *p_es;
3768
3769     ts_pid_t             *pmt = NULL;
3770     ts_prg_psi_t         *prg = NULL;
3771
3772     ts_pid_t             **pp_clean = NULL;
3773     int                  i_clean = 0;
3774     bool                 b_hdmv = false;
3775
3776     msg_Dbg( p_demux, "PMTCallBack called" );
3777
3778     /* First find this PMT declared in PAT */
3779     for( int i = 0; i < p_sys->i_pmt; i++ )
3780     {
3781         int i_prg;
3782         for( i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
3783         {
3784             const int i_pmt_number = p_sys->pmt[i]->psi->prg[i_prg]->i_number;
3785             if( i_pmt_number != TS_USER_PMT_NUMBER && i_pmt_number == p_pmt->i_program_number )
3786             {
3787                 pmt = p_sys->pmt[i];
3788                 prg = p_sys->pmt[i]->psi->prg[i_prg];
3789                 break;
3790             }
3791         }
3792         if( pmt )
3793             break;
3794     }
3795
3796     if( pmt == NULL )
3797     {
3798         msg_Warn( p_demux, "unreferenced program (broken stream)" );
3799         dvbpsi_DeletePMT(p_pmt);
3800         return;
3801     }
3802
3803     if( prg->i_version != -1 &&
3804         ( !p_pmt->b_current_next || prg->i_version == p_pmt->i_version ) )
3805     {
3806         dvbpsi_DeletePMT( p_pmt );
3807         return;
3808     }
3809
3810     /* Clean this program (remove all es) */
3811     for( int i = 0; i < 8192; i++ )
3812     {
3813         ts_pid_t *pid = &p_sys->pid[i];
3814
3815         if( pid->b_valid && pid->p_owner == pmt->psi &&
3816             pid->i_owner_number == prg->i_number && pid->psi == NULL )
3817         {
3818             TAB_APPEND( i_clean, pp_clean, pid );
3819         }
3820     }
3821     if( prg->iod )
3822     {
3823         IODFree( prg->iod );
3824         prg->iod = NULL;
3825     }
3826
3827     msg_Dbg( p_demux, "new PMT program number=%d version=%d pid_pcr=%d",
3828              p_pmt->i_program_number, p_pmt->i_version, p_pmt->i_pcr_pid );
3829     prg->i_pid_pcr = p_pmt->i_pcr_pid;
3830     prg->i_version = p_pmt->i_version;
3831
3832     ValidateDVBMeta( p_demux, prg->i_pid_pcr );
3833     if( ProgramIsSelected( p_demux, prg->i_number ) )
3834     {
3835         /* Set demux filter */
3836         SetPIDFilter( p_demux, prg->i_pid_pcr, true );
3837     }
3838
3839     /* Parse descriptor */
3840     for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
3841     {
3842         if( p_dr->i_tag == 0x1d )
3843         {
3844             /* We have found an IOD descriptor */
3845             msg_Dbg( p_demux, " * descriptor : IOD (0x1d)" );
3846
3847             prg->iod = IODNew( p_dr->i_length, p_dr->p_data );
3848         }
3849         else if( p_dr->i_tag == 0x9 )
3850         {
3851             uint16_t i_sysid = ((uint16_t)p_dr->p_data[0] << 8)
3852                                 | p_dr->p_data[1];
3853             msg_Dbg( p_demux, " * descriptor : CA (0x9) SysID 0x%x", i_sysid );
3854         }
3855         else if( p_dr->i_tag == 0x05 )
3856         {
3857             /* Registration Descriptor */
3858             if( p_dr->i_length != 4 )
3859             {
3860                 msg_Warn( p_demux, "invalid Registration Descriptor" );
3861             }
3862             else
3863             {
3864                 msg_Dbg( p_demux, " * descriptor : registration %4.4s", p_dr->p_data );
3865                 if( !memcmp( p_dr->p_data, "HDMV", 4 ) )
3866                 {
3867                     /* Blu-Ray */
3868                     b_hdmv = true;
3869                 }
3870             }
3871         }
3872         else
3873         {
3874             msg_Dbg( p_demux, " * descriptor : unknown (0x%x)", p_dr->i_tag );
3875         }
3876     }
3877
3878     for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
3879     {
3880         ts_pid_t tmp_pid, *old_pid = 0, *pid = &tmp_pid;
3881
3882         /* Find out if the PID was already declared */
3883         for( int i = 0; i < i_clean; i++ )
3884         {
3885             if( pp_clean[i] == &p_sys->pid[p_es->i_pid] )
3886             {
3887                 old_pid = pp_clean[i];
3888                 break;
3889             }
3890         }
3891         ValidateDVBMeta( p_demux, p_es->i_pid );
3892
3893         if( !old_pid && p_sys->pid[p_es->i_pid].b_valid )
3894         {
3895             msg_Warn( p_demux, "pmt error: pid=%d already defined",
3896                       p_es->i_pid );
3897             continue;
3898         }
3899
3900         for( p_dr = p_es->p_first_descriptor; p_dr != NULL;
3901              p_dr = p_dr->p_next )
3902         {
3903             msg_Dbg( p_demux, "  * es pid=%d type=%d dr->i_tag=0x%x",
3904                      p_es->i_pid, p_es->i_type, p_dr->i_tag );
3905         }
3906
3907         PIDInit( pid, false, pmt->psi );
3908         PIDFillFormat( pid, p_es->i_type );
3909         pid->i_owner_number = prg->i_number;
3910         pid->i_pid          = p_es->i_pid;
3911         pid->b_seen         = p_sys->pid[p_es->i_pid].b_seen;
3912
3913         if( p_es->i_type == 0x10 || p_es->i_type == 0x11 ||
3914             p_es->i_type == 0x12 || p_es->i_type == 0x0f )
3915         {
3916             PMTSetupEsISO14496( p_demux, pid, prg, p_es );
3917         }
3918         else if( p_es->i_type == 0x06 )
3919         {
3920             PMTSetupEs0x06(  p_demux, pid, p_es );
3921         }
3922         else if( p_es->i_type == 0xEA )
3923         {
3924             PMTSetupEs0xEA( p_demux, pid, p_es );
3925         }
3926         else if( p_es->i_type == 0xd1 )
3927         {
3928             PMTSetupEs0xD1( p_demux, pid, p_es );
3929         }
3930         else if( p_es->i_type == 0xa0 )
3931         {
3932             PMTSetupEs0xA0( p_demux, pid, p_es );
3933         }
3934         else if( b_hdmv )
3935         {
3936             PMTSetupEsHDMV( p_demux, pid, p_es );
3937         }
3938         else if( p_es->i_type >= 0x80 )
3939         {
3940             PMTSetupEsRegistration( p_demux, pid, p_es );
3941         }
3942
3943         if( pid->es->fmt.i_cat == AUDIO_ES ||
3944             ( pid->es->fmt.i_cat == SPU_ES &&
3945               pid->es->fmt.i_codec != VLC_CODEC_DVBS &&
3946               pid->es->fmt.i_codec != VLC_CODEC_TELETEXT ) )
3947         {
3948             PMTParseEsIso639( p_demux, pid, p_es );
3949         }
3950
3951         pid->es->fmt.i_group = p_pmt->i_program_number;
3952         for( int i = 0; i < pid->i_extra_es; i++ )
3953             pid->extra_es[i]->fmt.i_group = p_pmt->i_program_number;
3954
3955         if( pid->es->fmt.i_cat == UNKNOWN_ES )
3956         {
3957             msg_Dbg( p_demux, "  * es pid=%d type=%d *unknown*",
3958                      p_es->i_pid, p_es->i_type );
3959         }
3960         else if( !p_sys->b_udp_out )
3961         {
3962             msg_Dbg( p_demux, "  * es pid=%d type=%d fcc=%4.4s",
3963                      p_es->i_pid, p_es->i_type, (char*)&pid->es->fmt.i_codec );
3964
3965             if( p_sys->b_es_id_pid ) pid->es->fmt.i_id = p_es->i_pid;
3966
3967             /* Check if we can avoid restarting the ES */
3968             if( old_pid &&
3969                 pid->es->fmt.i_codec == old_pid->es->fmt.i_codec &&
3970                 pid->es->fmt.i_extra == old_pid->es->fmt.i_extra &&
3971                 pid->es->fmt.i_extra == 0 &&
3972                 pid->i_extra_es == old_pid->i_extra_es &&
3973                 ( ( !pid->es->fmt.psz_language &&
3974                     !old_pid->es->fmt.psz_language ) ||
3975                   ( pid->es->fmt.psz_language &&
3976                     old_pid->es->fmt.psz_language &&
3977                     !strcmp( pid->es->fmt.psz_language,
3978                              old_pid->es->fmt.psz_language ) ) ) )
3979             {
3980                 pid->es->id = old_pid->es->id;
3981                 old_pid->es->id = NULL;
3982                 for( int i = 0; i < pid->i_extra_es; i++ )
3983                 {
3984                     pid->extra_es[i]->id = old_pid->extra_es[i]->id;
3985                     old_pid->extra_es[i]->id = NULL;
3986                 }
3987             }
3988             else
3989             {
3990                 if( old_pid )
3991                 {
3992                     PIDClean( p_demux, old_pid );
3993                     TAB_REMOVE( i_clean, pp_clean, old_pid );
3994                     old_pid = 0;
3995                 }
3996
3997                 pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
3998                 for( int i = 0; i < pid->i_extra_es; i++ )
3999                 {
4000                     pid->extra_es[i]->id =
4001                         es_out_Add( p_demux->out, &pid->extra_es[i]->fmt );
4002                 }
4003                 p_sys->i_pmt_es += 1 + pid->i_extra_es;
4004             }
4005         }
4006
4007         /* Add ES to the list */
4008         if( old_pid )
4009         {
4010             PIDClean( p_demux, old_pid );
4011             TAB_REMOVE( i_clean, pp_clean, old_pid );
4012         }
4013         p_sys->pid[p_es->i_pid] = *pid;
4014
4015         p_dr = PMTEsFindDescriptor( p_es, 0x09 );
4016         if( p_dr && p_dr->i_length >= 2 )
4017         {
4018             uint16_t i_sysid = (p_dr->p_data[0] << 8) | p_dr->p_data[1];
4019             msg_Dbg( p_demux, "   * descriptor : CA (0x9) SysID 0x%x",
4020                      i_sysid );
4021         }
4022
4023         if( ProgramIsSelected( p_demux, prg->i_number ) &&
4024             ( pid->es->id != NULL || p_sys->b_udp_out ) )
4025         {
4026             /* Set demux filter */
4027             SetPIDFilter( p_demux, p_es->i_pid, true );
4028         }
4029     }
4030
4031     /* Set CAM descrambling */
4032     if( !ProgramIsSelected( p_demux, prg->i_number )
4033      || stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
4034                         ACCESS_SET_PRIVATE_ID_CA, p_pmt ) != VLC_SUCCESS )
4035         dvbpsi_DeletePMT( p_pmt );
4036
4037     for( int i = 0; i < i_clean; i++ )
4038     {
4039         if( ProgramIsSelected( p_demux, prg->i_number ) )
4040         {
4041             SetPIDFilter( p_demux, pp_clean[i]->i_pid, false );
4042         }
4043
4044         PIDClean( p_demux, pp_clean[i] );
4045     }
4046     if( i_clean )
4047         free( pp_clean );
4048 }
4049
4050 static void PATCallBack( demux_t *p_demux, dvbpsi_pat_t *p_pat )
4051 {
4052     demux_sys_t          *p_sys = p_demux->p_sys;
4053     dvbpsi_pat_program_t *p_program;
4054     ts_pid_t             *pat = &p_sys->pid[0];
4055
4056     msg_Dbg( p_demux, "PATCallBack called" );
4057
4058     if( ( pat->psi->i_pat_version != -1 &&
4059             ( !p_pat->b_current_next ||
4060               p_pat->i_version == pat->psi->i_pat_version ) ) ||
4061         p_sys->b_user_pmt )
4062     {
4063         dvbpsi_DeletePAT( p_pat );
4064         return;
4065     }
4066
4067     msg_Dbg( p_demux, "new PAT ts_id=%d version=%d current_next=%d",
4068              p_pat->i_ts_id, p_pat->i_version, p_pat->b_current_next );
4069
4070     /* Clean old */
4071     if( p_sys->i_pmt > 0 )
4072     {
4073         int      i_pmt_rm = 0;
4074         ts_pid_t **pmt_rm = NULL;
4075
4076         /* Search pmt to be deleted */
4077         for( int i = 0; i < p_sys->i_pmt; i++ )
4078         {
4079             ts_pid_t *pmt = p_sys->pmt[i];
4080             bool b_keep = false;
4081
4082             for( p_program = p_pat->p_first_program; p_program != NULL;
4083                  p_program = p_program->p_next )
4084             {
4085                 if( p_program->i_pid == pmt->i_pid )
4086                 {
4087                     for( int i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
4088                     {
4089                         if( p_program->i_number ==
4090                             pmt->psi->prg[i_prg]->i_number )
4091                         {
4092                             b_keep = true;
4093                             break;
4094                         }
4095                     }
4096                     if( b_keep )
4097                         break;
4098                 }
4099             }
4100
4101             if( !b_keep )
4102             {
4103                 TAB_APPEND( i_pmt_rm, pmt_rm, pmt );
4104             }
4105         }
4106
4107         /* Delete all ES attached to thoses PMT */
4108         for( int i = 2; i < 8192; i++ )
4109         {
4110             ts_pid_t *pid = &p_sys->pid[i];
4111
4112             if( !pid->b_valid || pid->psi )
4113                 continue;
4114
4115             for( int j = 0; j < i_pmt_rm && pid->b_valid; j++ )
4116             {
4117                 for( int i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
4118                 {
4119                     /* We only remove es that aren't defined by extra pmt */
4120                     if( pid->p_owner->prg[i_prg]->i_pid_pmt != pmt_rm[j]->i_pid )
4121                         continue;
4122
4123                     if( pid->es->id )
4124                         SetPIDFilter( p_demux, i, false );
4125
4126                     PIDClean( p_demux, pid );
4127                     break;
4128                 }
4129             }
4130         }
4131
4132         /* Delete PMT pid */
4133         for( int i = 0; i < i_pmt_rm; i++ )
4134         {
4135             SetPIDFilter( p_demux, pmt_rm[i]->i_pid, false );
4136
4137             for( int i_prg = 0; i_prg < pmt_rm[i]->psi->i_prg; i_prg++ )
4138             {
4139                 const int i_number = pmt_rm[i]->psi->prg[i_prg]->i_number;
4140                 es_out_Control( p_demux->out, ES_OUT_DEL_GROUP, i_number );
4141             }
4142
4143             PIDClean( p_demux, &p_sys->pid[pmt_rm[i]->i_pid] );
4144             TAB_REMOVE( p_sys->i_pmt, p_sys->pmt, pmt_rm[i] );
4145         }
4146
4147         free( pmt_rm );
4148     }
4149
4150     /* now create programs */
4151     for( p_program = p_pat->p_first_program; p_program != NULL;
4152          p_program = p_program->p_next )
4153     {
4154         msg_Dbg( p_demux, "  * number=%d pid=%d", p_program->i_number,
4155                  p_program->i_pid );
4156         if( p_program->i_number != 0 )
4157         {
4158             ts_pid_t *pmt = &p_sys->pid[p_program->i_pid];
4159             bool b_add = true;
4160
4161             ValidateDVBMeta( p_demux, p_program->i_pid );
4162
4163             if( pmt->b_valid )
4164             {
4165                 int i_prg;
4166                 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
4167                 {
4168                     if( pmt->psi->prg[i_prg]->i_number == p_program->i_number )
4169                     {
4170                         b_add = false;
4171                         break;
4172                     }
4173                 }
4174             }
4175             else
4176             {
4177                 TAB_APPEND( p_sys->i_pmt, p_sys->pmt, pmt );
4178             }
4179
4180             if( b_add )
4181             {
4182                 PIDInit( pmt, true, pat->psi );
4183                 pmt->psi->prg[pmt->psi->i_prg-1]->handle =
4184                     dvbpsi_AttachPMT( p_program->i_number,
4185                                       (dvbpsi_pmt_callback)PMTCallBack,
4186                                       p_demux );
4187                 pmt->psi->prg[pmt->psi->i_prg-1]->i_number =
4188                     p_program->i_number;
4189                 pmt->psi->prg[pmt->psi->i_prg-1]->i_pid_pmt =
4190                     p_program->i_pid;
4191
4192                 /* Now select PID at access level */
4193                 if( ProgramIsSelected( p_demux, p_program->i_number ) )
4194                 {
4195                     if( p_sys->i_current_program == 0 )
4196                         p_sys->i_current_program = p_program->i_number;
4197
4198                     if( SetPIDFilter( p_demux, p_program->i_pid, true ) )
4199                         p_sys->b_access_control = false;
4200                 }
4201             }
4202         }
4203     }
4204     pat->psi->i_pat_version = p_pat->i_version;
4205
4206     dvbpsi_DeletePAT( p_pat );
4207 }