+/*****************************************************************************
+ * Synchronize: update synchro level depending of heap state
+ *****************************************************************************
+ * This function is called during the main vout loop.
+ *****************************************************************************/
+static void Synchronize( vout_thread_t *p_vout, s64 i_delay )
+{
+ int i_synchro_inc = 0;
+ /* XXX?? gore following */
+ static int i_panic_count = 0;
+ static int i_last_synchro_inc = 0;
+ static float r_synchro_level = VOUT_SYNCHRO_LEVEL_START;
+ static int i_truc = 10;
+
+ if( i_delay < 0 )
+ {
+ //fprintf( stderr, "PANIC %d\n", i_panic_count );
+ i_panic_count++;
+ }
+
+ i_truc *= 2;
+
+ if( p_vout->i_pictures > VOUT_SYNCHRO_HEAP_IDEAL_SIZE+1 )
+ {
+ i_truc = 40;
+ i_synchro_inc += p_vout->i_pictures - VOUT_SYNCHRO_HEAP_IDEAL_SIZE - 1;
+
+ }
+ else
+ {
+ if( p_vout->i_pictures < VOUT_SYNCHRO_HEAP_IDEAL_SIZE )
+ {
+ i_truc = 32;
+ i_synchro_inc += p_vout->i_pictures - VOUT_SYNCHRO_HEAP_IDEAL_SIZE;
+ }
+ }
+
+ if( i_truc > VOUT_SYNCHRO_LEVEL_MAX*2*2*2*2*2 ||
+ i_synchro_inc*i_last_synchro_inc < 0 )
+ {
+ i_truc = 32;
+ }
+
+ if( i_delay < 6000 )
+ {
+ i_truc = 16;
+ i_synchro_inc -= 2;
+ }
+ else if( i_delay < 70000 )
+ {
+ i_truc = 24+(24*i_delay)/70000;
+ if( i_truc < 16 )
+ i_truc = 16;
+ i_synchro_inc -= 1+(5*(70000-i_delay))/70000;
+ }
+ else if( i_delay > 100000 )
+ {
+ r_synchro_level += 1;
+ if( i_delay > 130000 )
+ r_synchro_level += 1;
+ }
+
+ r_synchro_level += (float)i_synchro_inc / i_truc;
+ p_vout->i_synchro_level = (int)(r_synchro_level+0.5);
+
+ if( r_synchro_level > VOUT_SYNCHRO_LEVEL_MAX )
+ {
+ r_synchro_level = VOUT_SYNCHRO_LEVEL_MAX;
+ }
+
+ //fprintf( stderr, "synchro level : %d, heap : %d (%d, %d) (%d, %f) - %Ld\n", p_vout->i_synchro_level,
+ // p_vout->i_pictures, i_last_synchro_inc, i_synchro_inc, i_truc, r_synchro_level, i_delay );
+ i_last_synchro_inc = i_synchro_inc;
+}
+
+/*****************************************************************************