ts.c
上传用户:riyaled888
上传日期:2009-03-27
资源大小:7338k
文件大小:93k
源码类别:

多媒体

开发平台:

MultiPlatform

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