1 /*****************************************************************************
2 * pcr.c: PCR management
4 *****************************************************************************
5 * Manages structures containing PCR information.
6 *****************************************************************************/
8 /*****************************************************************************
10 *****************************************************************************/
12 #include <sys/types.h>
13 #include <sys/uio.h> /* iovec */
14 #include <stdlib.h> /* atoi(), malloc(), free() */
15 #include <sys/socket.h>
16 #include <netinet/in.h>
21 #include "vlc_thread.h"
25 #include "input_pcr.h"
29 * SYNCHRONIZATION METHOD
31 * We compute an average for the pcr because we want to eliminate the
32 * network jitter and keep the low frequency variations. The average is
33 * in fact a low pass filter and the jitter is a high frequency signal
34 * that is why it is eliminated by the filter/average.
36 * The low frequency variations enable us to synchronize the client clock
37 * with the server clock because they represent the time variation between
38 * the 2 clocks. Those variations (ie the filtered pcr) are used to compute
39 * the presentation dates for the audio and video frames. With those dates
40 * we can decoding (or trashing) the MPEG2 stream at "exactly" the same rate
41 * as it is sent by the server and so we keep the synchronization between
42 * the server and the client.
44 * It is a very important matter if you want to avoid underflow or overflow
45 * in all the FIFOs, but it may be not enough.
49 /*****************************************************************************
50 * input_PcrReInit : Reinitialize the pcr_descriptor
51 *****************************************************************************/
52 void input_PcrReInit( input_thread_t *p_input )
56 p_input->p_pcr->delta_pcr = 0;
57 p_input->p_pcr->last_pcr = 0;
58 p_input->p_pcr->c_average_count = 0;
61 /*****************************************************************************
62 * input_PcrInit : Initialize PCR decoder
63 *****************************************************************************/
64 int input_PcrInit( input_thread_t *p_input )
68 if( (p_input->p_pcr = malloc(sizeof(pcr_descriptor_t))) == NULL )
72 input_PcrReInit(p_input);
73 p_input->p_pcr->i_synchro_state = SYNCHRO_NOT_STARTED;
78 /*****************************************************************************
79 * input_PcrDecode : Decode a PCR frame
80 *****************************************************************************/
81 void input_PcrDecode( input_thread_t *p_input, es_descriptor_t *p_es,
84 mtime_t pcr_time, sys_time, delta_pcr;
85 pcr_descriptor_t *p_pcr;
91 p_pcr = p_input->p_pcr;
93 /* Convert the PCR in microseconde
94 * WARNING: do not remove the casts in the following calculation ! */
95 pcr_time = ( (( (mtime_t)U32_AT((u32*)p_pcr_data) << 1 ) | ( p_pcr_data[4] >> 7 )) * 300 ) / 27;
97 delta_pcr = sys_time - pcr_time;
99 if( p_es->b_discontinuity ||
100 ( p_pcr->last_pcr != 0 &&
101 ( (p_pcr->last_pcr - pcr_time) > PCR_MAX_GAP
102 || (p_pcr->last_pcr - pcr_time) < - PCR_MAX_GAP ) ) )
104 intf_DbgMsg("input debug: input_PcrReInit()\n");
105 input_PcrReInit(p_input);
106 p_pcr->i_synchro_state = SYNCHRO_REINIT;
107 p_es->b_discontinuity = 0;
109 p_pcr->last_pcr = pcr_time;
111 if( p_pcr->c_average_count == PCR_MAX_AVERAGE_COUNTER )
114 ( delta_pcr + (p_pcr->delta_pcr * (PCR_MAX_AVERAGE_COUNTER-1)) )
115 / PCR_MAX_AVERAGE_COUNTER;
120 ( delta_pcr + (p_pcr->delta_pcr * p_pcr->c_average_count) )
121 / ( p_pcr->c_average_count + 1 );
122 p_pcr->c_average_count++;
125 if( p_pcr->i_synchro_state == SYNCHRO_NOT_STARTED )
127 p_pcr->i_synchro_state = SYNCHRO_START;
131 /*****************************************************************************
132 * input_PcrEnd : Clean PCR structures before dying
133 *****************************************************************************/
134 void input_PcrEnd( input_thread_t *p_input )
138 free( p_input->p_pcr );