Skip to content

Commit

Permalink
EbTime: Refactor time functions (#120)
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Degawa <christopher.degawa@intel.com>
  • Loading branch information
1480c1 authored Aug 10, 2020
1 parent df7b86d commit 8f4cda5
Show file tree
Hide file tree
Showing 11 changed files with 246 additions and 283 deletions.
42 changes: 0 additions & 42 deletions Source/API/EbSvtVp9Time.h

This file was deleted.

15 changes: 11 additions & 4 deletions Source/App/EbAppMain.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@
#include <stdint.h>
#include "EbAppConfig.h"
#include "EbAppContext.h"
#include "EbSvtVp9Time.h"
#include "EbAppTime.h"
#ifdef _WIN32
#include <windows.h>
#include <fcntl.h> /* _O_BINARY */
#include <io.h> /* _setmode() */
#else
#include <pthread.h>
#include <semaphore.h>
#include <time.h>
#include <errno.h>
#endif

Expand Down Expand Up @@ -151,7 +150,11 @@ int32_t main(int32_t argc, char* argv[])

configs[instance_count]->active_channel_count = num_channels;
configs[instance_count]->channel_id = instance_count;
eb_start_time((uint64_t*)&configs[instance_count]->performance_context.lib_start_time[0], (uint64_t*)&configs[instance_count]->performance_context.lib_start_time[1]);
app_svt_vp9_get_time(
&configs[instance_count]
->performance_context.lib_start_time[0],
&configs[instance_count]
->performance_context.lib_start_time[1]);
return_errors[instance_count] = init_encoder(configs[instance_count], app_callbacks[instance_count], instance_count);
return_error = (EbErrorType)(return_error | return_errors[instance_count]);
}
Expand All @@ -170,7 +173,11 @@ int32_t main(int32_t argc, char* argv[])
exit_conditions_recon[instance_count] = configs[instance_count]->recon_file ? APP_ExitConditionNone : APP_ExitConditionError;
exit_conditions_input[instance_count] = APP_ExitConditionNone;
channel_active[instance_count] = EB_TRUE;
eb_start_time((uint64_t*)&configs[instance_count]->performance_context.encode_start_time[0], (uint64_t*)&configs[instance_count]->performance_context.encode_start_time[1]);
app_svt_vp9_get_time(
&configs[instance_count]
->performance_context.encode_start_time[0],
&configs[instance_count]
->performance_context.encode_start_time[1]);

}
else {
Expand Down
69 changes: 49 additions & 20 deletions Source/App/EbAppProcessCmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "EbAppContext.h"
#include "EbAppConfig.h"
#include "EbSvtVp9ErrorCodes.h"
#include "EbSvtVp9Time.h"
#include "EbAppTime.h"

/***************************************
* Macros
Expand Down Expand Up @@ -444,6 +444,35 @@ void send_qp_on_the_fly(
return;
}

static void app_svt_vp9_injector(uint64_t processed_frame_count,
uint32_t injector_frame_rate) {
static uint64_t start_times_seconds;
static uint64_t start_timesu_seconds;
static int first_time = 0;

if (first_time == 0) {
first_time = 1;
app_svt_vp9_get_time(&start_times_seconds, &start_timesu_seconds);
} else {
uint64_t current_times_seconds, current_timesu_seconds;
app_svt_vp9_get_time(&current_times_seconds, &current_timesu_seconds);
const double elapsed_time = app_svt_vp9_compute_overall_elapsed_time(
start_times_seconds, start_timesu_seconds, current_times_seconds,
current_timesu_seconds);
const int buffer_frames =
1; // How far ahead of time should we let it get
const double injector_interval =
(double)(1 << 16) /
injector_frame_rate; // 1.0 / injector frame rate (in this
// case, 1.0/encodRate)
const double predicted_time =
(processed_frame_count - buffer_frames) * injector_interval;
const int milli_sec_ahead =
(int)(1000 * (predicted_time - elapsed_time));
if (milli_sec_ahead > 0) app_svt_vp9_sleep(milli_sec_ahead);
}
}

//************************************/
// process_input_buffer
// Reads yuv frames from file and copy
Expand All @@ -466,7 +495,7 @@ AppExitConditionType process_input_buffer(
int64_t remaining_byte_count;

if (config->injector && config->processed_frame_count)
eb_injector(config->processed_frame_count, config->injector_frame_rate);
app_svt_vp9_injector(config->processed_frame_count, config->injector_frame_rate);

total_bytes_to_process_count = (frames_to_be_encoded < 0) ? -1 :
frames_to_be_encoded * SIZE_OF_ONE_FRAME_IN_BYTES(input_padded_width, input_padded_height, is16bit);
Expand Down Expand Up @@ -624,26 +653,24 @@ AppExitConditionType process_output_stream_buffer(
}
else if (stream_status != EB_NoErrorEmptyQueue) {
++(config->performance_context.frame_count);
*total_latency += (uint64_t)header_ptr->n_tick_count;
*total_latency += header_ptr->n_tick_count;
*max_latency = (header_ptr->n_tick_count > *max_latency) ? header_ptr->n_tick_count : *max_latency;

eb_finish_time((uint64_t*)&finishs_time, (uint64_t*)&finishu_time);
app_svt_vp9_get_time(&finishs_time, &finishu_time);

// total execution time, inc init time
eb_compute_overall_elapsed_time(
config->performance_context.lib_start_time[0],
config->performance_context.lib_start_time[1],
finishs_time,
finishu_time,
&config->performance_context.total_execution_time);
config->performance_context.total_execution_time =
app_svt_vp9_compute_overall_elapsed_time(
config->performance_context.lib_start_time[0],
config->performance_context.lib_start_time[1], finishs_time,
finishu_time);

// total encode time
eb_compute_overall_elapsed_time(
config->performance_context.encode_start_time[0],
config->performance_context.encode_start_time[1],
finishs_time,
finishu_time,
&config->performance_context.total_encode_time);
config->performance_context.total_encode_time =
app_svt_vp9_compute_overall_elapsed_time(
config->performance_context.encode_start_time[0],
config->performance_context.encode_start_time[1], finishs_time,
finishu_time);

// Write Stream Data to file
if (stream_file) {
Expand Down Expand Up @@ -687,10 +714,12 @@ AppExitConditionType process_output_stream_buffer(
//++frame_count;
fflush(stdout);

{
config->performance_context.average_speed = (config->performance_context.frame_count) / config->performance_context.total_encode_time;
config->performance_context.average_latency = config->performance_context.total_latency / (double)(config->performance_context.frame_count);
}
config->performance_context.average_speed =
config->performance_context.frame_count /
(double)config->performance_context.total_encode_time;
config->performance_context.average_latency =
(double)config->performance_context.total_latency /
config->performance_context.frame_count;

if (!(frame_count % SPEED_MEASUREMENT_INTERVAL)) {
{
Expand Down
57 changes: 57 additions & 0 deletions Source/App/EbAppTime.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright(c) 2019 Intel Corporation
* SPDX - License - Identifier: BSD - 2 - Clause - Patent
*/

#ifdef _WIN32
#include <sys/timeb.h>
#include <windows.h>
#elif !defined(__USE_POSIX199309)
#define __USE_POSIX199309
#endif

#include <time.h>

#if !defined(CLOCK_MONOTONIC) && !defined(_WIN32)
#include <sys/time.h>
#endif

#include "EbAppTime.h"

void app_svt_vp9_sleep(const unsigned milliseconds) {
if (!milliseconds) return;
#ifdef _WIN32
Sleep(milliseconds);
#else
nanosleep(&(struct timespec){milliseconds / 1000,
(milliseconds % 1000) * 1000000},
NULL);
#endif
}

double app_svt_vp9_compute_overall_elapsed_time(
const uint64_t start_seconds, const uint64_t start_useconds,
const uint64_t finish_seconds, const uint64_t finish_useconds) {
const int64_t s_diff = (int64_t)finish_seconds - (int64_t)start_seconds,
u_diff = (int64_t)finish_useconds - (int64_t)start_useconds;
return ((double)s_diff * 1000.0 + (double)u_diff / 1000.0 + 0.5) / 1000.0;
}

void app_svt_vp9_get_time(uint64_t *const seconds, uint64_t *const useconds) {
#ifdef _WIN32
struct _timeb curr_time;
_ftime_s(&curr_time);
*seconds = curr_time.time;
*useconds = curr_time.millitm;
#elif defined(CLOCK_MONOTONIC)
struct timespec curr_time;
clock_gettime(CLOCK_MONOTONIC, &curr_time);
*seconds = (uint64_t)curr_time.tv_sec;
*useconds = (uint64_t)curr_time.tv_nsec / 1000UL;
#else
struct timeval curr_time;
gettimeofday(&curr_time, NULL);
*seconds = curr_time.tv_sec;
*useconds = curr_time.tv_usec;
#endif
}
18 changes: 18 additions & 0 deletions Source/App/EbAppTime.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright(c) 2019 Intel Corporation
* SPDX - License - Identifier: BSD - 2 - Clause - Patent
*/

#ifndef EbAppTime_h
#define EbAppTime_h

#include <stdint.h>

void app_svt_vp9_sleep(const unsigned milliseconds);
double app_svt_vp9_compute_overall_elapsed_time(const uint64_t start_seconds,
const uint64_t start_useconds,
const uint64_t finish_seconds,
const uint64_t finish_useconds);
void app_svt_vp9_get_time(uint64_t *const seconds, uint64_t *const useconds);

#endif // EbAppTime_h
99 changes: 0 additions & 99 deletions Source/App/EbTime.c

This file was deleted.

Loading

0 comments on commit 8f4cda5

Please sign in to comment.