Convert "rate" variable to float everywhere

This follows the historical LibVLC semantic (and new --rate one), so
bigger is faster. In the process, simplify a few code paths, especially
those in LibVLC, but make many other code paths more complicated due to
integer<->float conversion. Most of those paths could probably be
simplified.

 * Fix RC "fastforward" command behaviour (it was slowing down instead)
 * Fix str_format_mera 'R' format string with negative rates and
   rates such that (rate % 1000 < 100).
This commit is contained in:
Rémi Denis-Courmont 2009-11-23 18:44:52 +02:00
parent d2a714c1e4
commit e87dd9f0fd
14 changed files with 63 additions and 53 deletions

View File

@ -344,12 +344,14 @@ typedef enum input_state_e
/** /**
* Input rate. * Input rate.
* *
* It is an integer used by the variable "rate" in the * It is an float used by the variable "rate" in the
* range [INPUT_RATE_MIN, INPUT_RATE_MAX] the default value * range [INPUT_RATE_DEFAULT/INPUT_RATE_MAX, INPUT_RATE_DEFAULT/INPUT_RATE_MAX]
* being INPUT_RATE_DEFAULT. * the default value being 1. It represents the ratio of playback speed to
* nominal speed (bigger is faster).
* *
* A value lower than INPUT_RATE_DEFAULT plays faster. * Internally, the rate is stored as a value in the range
* A value higher than INPUT_RATE_DEFAULT plays slower. * [INPUT_RATE_MIN, INPUT_RATE_MAX].
* internal rate = INPUT_RATE_DEFAULT / rate variable
*/ */
/** /**
@ -452,7 +454,7 @@ enum input_query_e
INPUT_GET_TIME, /* arg1= int64_t * res= */ INPUT_GET_TIME, /* arg1= int64_t * res= */
INPUT_SET_TIME, /* arg1= int64_t res=can fail */ INPUT_SET_TIME, /* arg1= int64_t res=can fail */
/* input variable "rate" (1 is DEFAULT_RATE) */ /* input variable "rate" (nominal is INPUT_RATE_DEFAULT) */
INPUT_GET_RATE, /* arg1= int * res= */ INPUT_GET_RATE, /* arg1= int * res= */
INPUT_SET_RATE, /* arg1= int res=can fail */ INPUT_SET_RATE, /* arg1= int res=can fail */

View File

@ -710,7 +710,7 @@ static int PutAction( intf_thread_t *p_intf, int i_action )
} }
else if( i_action == ACTIONID_RATE_NORMAL ) else if( i_action == ACTIONID_RATE_NORMAL )
{ {
var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT ); var_SetFloat( p_input, "rate", 1. );
vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN, vout_OSDMessage( VLC_OBJECT(p_input), DEFAULT_CHAN,
"%s", _("1.00x") ); "%s", _("1.00x") );
} }
@ -721,9 +721,12 @@ static int PutAction( intf_thread_t *p_intf, int i_action )
* and we want to increase/decrease it by 0.1 while making sure * and we want to increase/decrease it by 0.1 while making sure
* that the resulting playback rate is a multiple of 0.1 * that the resulting playback rate is a multiple of 0.1
*/ */
int i_rate = var_GetInteger( p_input, "rate" ); int i_rate = 1. * INPUT_RATE_DEFAULT
if( i_rate == 0 ) / var_GetFloat( p_input, "rate" );
if( i_rate < INPUT_RATE_MIN )
i_rate = INPUT_RATE_MIN; i_rate = INPUT_RATE_MIN;
else if( i_rate > INPUT_RATE_MAX )
i_rate = INPUT_RATE_MAX;
int i_sign = i_rate < 0 ? -1 : 1; int i_sign = i_rate < 0 ? -1 : 1;
const int i_dir = i_action == ACTIONID_RATE_FASTER_FINE ? 1 : -1; const int i_dir = i_action == ACTIONID_RATE_FASTER_FINE ? 1 : -1;
@ -735,7 +738,7 @@ static int PutAction( intf_thread_t *p_intf, int i_action )
i_rate = i_sign * __MIN( __MAX( i_rate, INPUT_RATE_MIN ), INPUT_RATE_MAX ); i_rate = i_sign * __MIN( __MAX( i_rate, INPUT_RATE_MIN ), INPUT_RATE_MAX );
var_SetInteger( p_input, "rate", i_rate ); var_SetFloat( p_input, "rate", i_rate );
char psz_msg[7+1]; char psz_msg[7+1];
snprintf( psz_msg, sizeof(psz_msg), _("%.2fx"), (double)INPUT_RATE_DEFAULT / i_rate ); snprintf( psz_msg, sizeof(psz_msg), _("%.2fx"), (double)INPUT_RATE_DEFAULT / i_rate );
@ -809,9 +812,9 @@ static int PutAction( intf_thread_t *p_intf, int i_action )
} }
else if( i_action == ACTIONID_PLAY ) else if( i_action == ACTIONID_PLAY )
{ {
if( var_GetInteger( p_input, "rate" ) != INPUT_RATE_DEFAULT ) if( var_GetFloat( p_input, "rate" ) != 1. )
/* Return to normal speed */ /* Return to normal speed */
var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT ); var_SetFloat( p_input, "rate", 1. );
else else
{ {
ClearChannels( p_intf, p_vout ); ClearChannels( p_intf, p_vout );

View File

@ -962,8 +962,8 @@ static void RateChanged( intf_thread_t *p_intf,
input_thread_t *p_input ) input_thread_t *p_input )
{ {
vlc_mutex_lock( &p_intf->p_sys->status_lock ); vlc_mutex_lock( &p_intf->p_sys->status_lock );
msg_rc( STATUS_CHANGE "( new rate: %d )", msg_rc( STATUS_CHANGE "( new rate: %.3f )",
var_GetInteger( p_input, "rate" ) ); var_GetFloat( p_input, "rate" ) );
vlc_mutex_unlock( &p_intf->p_sys->status_lock ); vlc_mutex_unlock( &p_intf->p_sys->status_lock );
} }
static void PositionChanged( intf_thread_t *p_intf, static void PositionChanged( intf_thread_t *p_intf,
@ -1059,9 +1059,9 @@ static int Input( vlc_object_t *p_this, char const *psz_cmd,
{ {
if( var_GetBool( p_input, "can-rate" ) ) if( var_GetBool( p_input, "can-rate" ) )
{ {
int i_rate = var_GetInteger( p_input, "rate" ); float f_rate = var_GetFloat( p_input, "rate" );
i_rate = (i_rate < 0) ? -i_rate : i_rate * 2; f_rate = (f_rate < 0) ? -f_rate : f_rate * 2;
var_SetInteger( p_input, "rate", i_rate ); var_SetFloat( p_input, "rate", f_rate );
} }
else else
{ {
@ -1073,9 +1073,9 @@ static int Input( vlc_object_t *p_this, char const *psz_cmd,
{ {
if( var_GetBool( p_input, "can-rewind" ) ) if( var_GetBool( p_input, "can-rewind" ) )
{ {
int i_rate = var_GetInteger( p_input, "rate" ); float f_rate = var_GetFloat( p_input, "rate" );
i_rate = (i_rate > 0) ? -i_rate : i_rate / 2; f_rate = (f_rate > 0) ? -f_rate : f_rate * 2;
var_SetInteger( p_input, "rate", i_rate ); var_SetFloat( p_input, "rate", f_rate );
} }
else else
{ {

View File

@ -487,49 +487,49 @@ void InterfaceWindow::MessageReceived( BMessage * p_message )
case HEIGHTH_PLAY: case HEIGHTH_PLAY:
if( p_input ) if( p_input )
{ {
var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT * 8 ); var_SetFloat( p_input, "rate", .125 );
} }
break; break;
case QUARTER_PLAY: case QUARTER_PLAY:
if( p_input ) if( p_input )
{ {
var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT * 4 ); var_SetFloat( p_input, "rate", .25 );
} }
break; break;
case HALF_PLAY: case HALF_PLAY:
if( p_input ) if( p_input )
{ {
var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT * 2 ); var_SetFloat( p_input, "rate", .5 );
} }
break; break;
case NORMAL_PLAY: case NORMAL_PLAY:
if( p_input ) if( p_input )
{ {
var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT ); var_SetFloat( p_input, "rate", 1. );
} }
break; break;
case TWICE_PLAY: case TWICE_PLAY:
if( p_input ) if( p_input )
{ {
var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT / 2 ); var_SetFloat( p_input, "rate", 2. );
} }
break; break;
case FOUR_PLAY: case FOUR_PLAY:
if( p_input ) if( p_input )
{ {
var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT / 4 ); var_SetFloat( p_input, "rate", 4. );
} }
break; break;
case HEIGHT_PLAY: case HEIGHT_PLAY:
if( p_input ) if( p_input )
{ {
var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT / 8 ); var_SetFloat( p_input, "rate", 8. );
} }
break; break;
@ -840,12 +840,12 @@ void InterfaceWindow::UpdateInterface()
p_mediaControl->SetEnabled( true ); p_mediaControl->SetEnabled( true );
bool hasTitles = !var_Get( p_input, "title", &val ); bool hasTitles = !var_Get( p_input, "title", &val );
bool hasChapters = !var_Get( p_input, "chapter", &val ); bool hasChapters = !var_Get( p_input, "chapter", &val );
p_mediaControl->SetStatus( var_GetInteger( p_input, "state" ), p_mediaControl->SetStatus( INPUT_RATE_DEFAULT / var_GetFloat( p_input, "state" ),
var_GetInteger( p_input, "rate" ) ); INPUT_RATE_DEFAULT / var_GetFloat( p_input, "rate" ) );
var_Get( p_input, "position", &val ); var_Get( p_input, "position", &val );
p_mediaControl->SetProgress( val.f_float ); p_mediaControl->SetProgress( val.f_float );
_SetMenusEnabled( true, hasChapters, hasTitles ); _SetMenusEnabled( true, hasChapters, hasTitles );
_UpdateSpeedMenu( var_GetInteger( p_input, "rate" ) ); _UpdateSpeedMenu( INPUT_RATE_DEFAULT / var_GetFloat( p_input, "rate" ) );
// enable/disable skip buttons // enable/disable skip buttons
#if 0 #if 0

View File

@ -407,7 +407,7 @@ void InputManager::UpdateStatus()
void InputManager::UpdateRate() void InputManager::UpdateRate()
{ {
/* Update Rate */ /* Update Rate */
int i_new_rate = var_GetInteger( p_input, "rate"); int i_new_rate = INPUT_RATE_DEFAULT / var_GetFloat( p_input, "rate" );
if( i_new_rate != i_rate ) if( i_new_rate != i_rate )
{ {
i_rate = i_new_rate; i_rate = i_new_rate;
@ -776,8 +776,8 @@ void InputManager::reverse()
{ {
if( hasInput() ) if( hasInput() )
{ {
int i_rate = var_GetInteger( p_input, "rate" ); float f_rate = var_GetFloat( p_input, "rate" );
var_SetInteger( p_input, "rate", -i_rate ); var_SetFloat( p_input, "rate", -f_rate );
} }
} }
@ -806,13 +806,14 @@ void InputManager::littleslower()
void InputManager::normalRate() void InputManager::normalRate()
{ {
if( hasInput() ) if( hasInput() )
var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT ); var_SetFloat( p_input, "rate", 1. );
} }
void InputManager::setRate( int new_rate ) void InputManager::setRate( int new_rate )
{ {
if( hasInput() ) if( hasInput() )
var_SetInteger( p_input, "rate", new_rate ); var_SetFloat( p_input, "rate",
(float)INPUT_RATE_DEFAULT / (float)new_rate );
} }
void InputManager::jumpFwd() void InputManager::jumpFwd()

View File

@ -195,7 +195,7 @@ void Timer::Notify( void )
/* Manage Speed status */ /* Manage Speed status */
var_Get( p_input, "rate", &val ); var_Get( p_input, "rate", &val );
if( i_old_rate != val.i_int ) if( i_old_rate != (int)((float)INPUT_RATE_DEFAULT / val.f_float) )
{ {
TCHAR psz_text[15]; TCHAR psz_text[15];
_stprintf( psz_text + 2, _T("x%.2f"), 1000.0 / val.i_int ); _stprintf( psz_text + 2, _T("x%.2f"), 1000.0 / val.i_int );

View File

@ -407,7 +407,7 @@ end
function rate(name,client) function rate(name,client)
local input = vlc.object.input() local input = vlc.object.input()
if name == "normal" then if name == "normal" then
vlc.var.set(input,"rate",1000) -- FIXME: INPUT_RATE_DEFAULT vlc.var.set(input,"rate",1)
else else
vlc.var.set(input,"rate-"..name,nil) vlc.var.set(input,"rate-"..name,nil)
end end

View File

@ -1052,7 +1052,7 @@ void libvlc_media_player_set_rate(
return; return;
} }
var_SetInteger( p_input_thread, "rate", 1000.0f/rate ); var_SetFloat( p_input_thread, "rate", rate );
vlc_object_release( p_input_thread ); vlc_object_release( p_input_thread );
} }
@ -1061,23 +1061,24 @@ float libvlc_media_player_get_rate(
libvlc_exception_t *p_e ) libvlc_exception_t *p_e )
{ {
input_thread_t *p_input_thread; input_thread_t *p_input_thread;
int i_rate; float f_rate;
bool b_can_rewind; bool b_can_rewind;
p_input_thread = libvlc_get_input_thread ( p_mi, p_e ); p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
if( !p_input_thread ) if( !p_input_thread )
return 0.0; /* rate < 0 indicates rewind */ return 0.0; /* rate < 0 indicates rewind */
i_rate = var_GetInteger( p_input_thread, "rate" ); f_rate = var_GetFloat( p_input_thread, "rate" );
b_can_rewind = var_GetBool( p_input_thread, "can-rewind" ); b_can_rewind = var_GetBool( p_input_thread, "can-rewind" );
if( i_rate < 0 && !b_can_rewind ) /* FIXME: why are negative values forbidden ?? (rewinding) */
if( f_rate < 0 && !b_can_rewind )
{ {
vlc_object_release( p_input_thread ); vlc_object_release( p_input_thread );
return 0.0; return 0.0;
} }
vlc_object_release( p_input_thread ); vlc_object_release( p_input_thread );
return (float)1000.0f/i_rate; return f_rate;
} }
libvlc_state_t libvlc_media_player_get_state( libvlc_state_t libvlc_media_player_get_state(

View File

@ -99,12 +99,13 @@ int input_vaControl( input_thread_t *p_input, int i_query, va_list args )
case INPUT_GET_RATE: case INPUT_GET_RATE:
pi_int = (int*)va_arg( args, int * ); pi_int = (int*)va_arg( args, int * );
*pi_int = var_GetInteger( p_input, "rate" ); *pi_int = INPUT_RATE_DEFAULT / var_GetFloat( p_input, "rate" );
return VLC_SUCCESS; return VLC_SUCCESS;
case INPUT_SET_RATE: case INPUT_SET_RATE:
i_int = (int)va_arg( args, int ); i_int = (int)va_arg( args, int );
return var_SetInteger( p_input, "rate", i_int ); return var_SetFloat( p_input, "rate",
(float)INPUT_RATE_DEFAULT / (float)i_int );
case INPUT_GET_STATE: case INPUT_GET_STATE:
pi_int = (int*)va_arg( args, int * ); pi_int = (int*)va_arg( args, int * );

View File

@ -95,10 +95,10 @@ void input_SendEventStatistics( input_thread_t *p_input )
} }
void input_SendEventRate( input_thread_t *p_input, int i_rate ) void input_SendEventRate( input_thread_t *p_input, int i_rate )
{ {
vlc_value_t val; vlc_value_t val;
val.i_int = i_rate; val.f_float = (float)INPUT_RATE_DEFAULT / (float)i_rate;
var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL ); var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
Trigger( p_input, INPUT_EVENT_RATE ); Trigger( p_input, INPUT_EVENT_RATE );
} }

View File

@ -136,8 +136,8 @@ void input_ControlVarInit ( input_thread_t *p_input )
var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL ); var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
/* Rate */ /* Rate */
var_Create( p_input, "rate", VLC_VAR_INTEGER ); var_Create( p_input, "rate", VLC_VAR_FLOAT );
val.i_int = p_input->p->i_rate; val.f_float = (float)INPUT_RATE_DEFAULT / (float)p_input->p->i_rate;
var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL ); var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
var_Create( p_input, "rate-slower", VLC_VAR_VOID ); var_Create( p_input, "rate-slower", VLC_VAR_VOID );
@ -574,6 +574,7 @@ static int RateCallback( vlc_object_t *p_this, char const *psz_cmd,
} }
else else
{ {
int i_rate = INPUT_RATE_DEFAULT / newval.f_float;
input_ControlPush( p_input, INPUT_CONTROL_SET_RATE, &newval ); input_ControlPush( p_input, INPUT_CONTROL_SET_RATE, &newval );
} }
return VLC_SUCCESS; return VLC_SUCCESS;

View File

@ -1064,7 +1064,8 @@ static int vlm_ControlMediaInstanceGets( vlm_t *p_vlm, int64_t id, vlm_media_ins
p_idsc->d_position = var_GetFloat( p_instance->p_input, "position" ); p_idsc->d_position = var_GetFloat( p_instance->p_input, "position" );
if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S ) if( var_GetInteger( p_instance->p_input, "state" ) == PAUSE_S )
p_idsc->b_paused = true; p_idsc->b_paused = true;
p_idsc->i_rate = var_GetInteger( p_instance->p_input, "rate" ); p_idsc->i_rate = INPUT_RATE_DEFAULT
/ var_GetFloat( p_instance->p_input, "rate" );
} }
TAB_APPEND( i_idsc, pp_idsc, p_idsc ); TAB_APPEND( i_idsc, pp_idsc, p_idsc );

View File

@ -1365,7 +1365,7 @@ static vlm_message_t *vlm_ShowMedia( vlm_media_sys_t *p_media )
APPEND_INPUT_INFO( "position", "%f", Float ); APPEND_INPUT_INFO( "position", "%f", Float );
APPEND_INPUT_INFO( "time", "%"PRIi64, Time ); APPEND_INPUT_INFO( "time", "%"PRIi64, Time );
APPEND_INPUT_INFO( "length", "%"PRIi64, Time ); APPEND_INPUT_INFO( "length", "%"PRIi64, Time );
APPEND_INPUT_INFO( "rate", "%d", Integer ); APPEND_INPUT_INFO( "rate", "%f", Float );
APPEND_INPUT_INFO( "title", "%d", Integer ); APPEND_INPUT_INFO( "title", "%d", Integer );
APPEND_INPUT_INFO( "chapter", "%d", Integer ); APPEND_INPUT_INFO( "chapter", "%d", Integer );
APPEND_INPUT_INFO( "can-seek", "%d", Bool ); APPEND_INPUT_INFO( "can-seek", "%d", Bool );

View File

@ -858,8 +858,8 @@ char *__str_format_meta( vlc_object_t *p_object, const char *string )
case 'R': case 'R':
if( p_input ) if( p_input )
{ {
int r = var_GetInteger( p_input, "rate" ); float f = var_GetFloat( p_input, "rate" );
snprintf( buf, 10, "%d.%d", r/1000, r%1000 ); snprintf( buf, 10, "%.3f", f );
} }
else else
{ {