Skip to content

Commit 999c512

Browse files
committed
Remove rate control singleton
1 parent 4aeeba7 commit 999c512

File tree

6 files changed

+51
-40
lines changed

6 files changed

+51
-40
lines changed

src/encoder.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "strategyselector.h"
4242
#include "kvz_math.h"
4343
#include "fast_coeff_cost.h"
44+
#include "rate_control.h"
4445

4546
static int encoder_control_init_gop_layer_weights(encoder_control_t * const);
4647

@@ -602,6 +603,15 @@ encoder_control_t* kvz_encoder_control_init(const kvz_config *const cfg)
602603
memcpy(encoder->cfg.optional_key, cfg->optional_key, 16);
603604
}
604605

606+
if (encoder->cfg.target_bitrate > 0 && cfg->rc_algorithm != KVZ_NO_RC)
607+
{
608+
encoder->rc_data = kvz_get_rc_data(encoder);
609+
if (!encoder->rc_data) {
610+
fprintf(stderr, "Failed to initialize rate control.\n");
611+
goto init_failed;
612+
}
613+
}
614+
605615
return encoder;
606616

607617
init_failed:
@@ -645,6 +655,8 @@ void kvz_encoder_control_free(encoder_control_t *const encoder)
645655
fclose(encoder->roi_file);
646656
}
647657

658+
kvz_free_rc_data(encoder->rc_data);
659+
648660
free(encoder);
649661
}
650662

src/encoder.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
#include "threadqueue.h"
4545
#include "fast_coeff_cost.h"
4646

47+
struct rc_data;
48+
4749
/* Encoder control options, the main struct */
4850
typedef struct encoder_control_t
4951
{
@@ -149,6 +151,8 @@ typedef struct encoder_control_t
149151

150152
fast_coeff_table_t fast_coeff_table;
151153

154+
struct kvz_rc_data *rc_data;
155+
152156
} encoder_control_t;
153157

154158
encoder_control_t* kvz_encoder_control_init(const kvz_config *cfg);

src/encoder_state-ctors_dtors.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static int encoder_state_config_frame_init(encoder_state_t * const state) {
9494

9595
pthread_mutex_init(&state->frame->rc_lock, NULL);
9696

97-
state->frame->new_ratecontrol = kvz_get_rc_data(NULL);
97+
state->frame->new_ratecontrol = state->encoder_control->rc_data;
9898

9999
return 1;
100100
}

src/kvazaar.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ static void kvazaar_close(kvz_encoder *encoder)
7878
}
7979
FREE_POINTER(encoder->states);
8080

81-
kvz_free_rc_data();
8281
// Discard const from the pointer.
8382
kvz_encoder_control_free((void*) encoder->control);
8483
encoder->control = NULL;
@@ -114,11 +113,6 @@ static kvz_encoder * kvazaar_open(const kvz_config *cfg)
114113
encoder->frames_started = 0;
115114
encoder->frames_done = 0;
116115

117-
// Assure that the rc data allocation was successful
118-
if(!kvz_get_rc_data(encoder->control)) {
119-
goto kvazaar_open_failure;
120-
}
121-
122116
kvz_init_input_frame_buffer(&encoder->input_buffer);
123117

124118
encoder->states = calloc(encoder->num_encoder_states, sizeof(encoder_state_t));

src/rate_control.c

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,10 @@
4040

4141

4242
static const int MIN_SMOOTHING_WINDOW = 40;
43-
static int smoothing_window = 40;
4443
static const double MIN_LAMBDA = 0.1;
4544
static const double MAX_LAMBDA = 10000;
4645
#define BETA1 1.2517
4746

48-
static kvz_rc_data *data;
49-
50-
static FILE *dist_file;
51-
static FILE *bits_file;
52-
static FILE *qp_file;
53-
static FILE *lambda_file;
5447

5548
/**
5649
* \brief Clip lambda value to a valid range.
@@ -61,9 +54,7 @@ static double clip_lambda(double lambda) {
6154
}
6255

6356
kvz_rc_data * kvz_get_rc_data(const encoder_control_t * const encoder) {
64-
if (data != NULL || encoder == NULL) return data;
65-
66-
data = calloc(1, sizeof(kvz_rc_data));
57+
kvz_rc_data* data = calloc(1, sizeof(kvz_rc_data));
6758

6859
if (data == NULL) return NULL;
6960
if (pthread_mutex_init(&data->ck_frame_lock, NULL) != 0) return NULL;
@@ -107,18 +98,21 @@ kvz_rc_data * kvz_get_rc_data(const encoder_control_t * const encoder) {
10798
if(encoder->cfg.stats_file_prefix) {
10899
char buff[128];
109100
sprintf(buff, "%sbits.txt", encoder->cfg.stats_file_prefix);
110-
bits_file = fopen(buff, "w");
101+
data->bits_file = fopen(buff, "w");
111102
sprintf(buff, "%sdist.txt", encoder->cfg.stats_file_prefix);
112-
dist_file = fopen(buff, "w");
103+
data->dist_file = fopen(buff, "w");
113104
sprintf(buff, "%sqp.txt", encoder->cfg.stats_file_prefix);
114-
qp_file = fopen(buff, "w");
105+
data->qp_file = fopen(buff, "w");
115106
sprintf(buff, "%slambda.txt", encoder->cfg.stats_file_prefix);
116-
lambda_file = fopen(buff, "w");
107+
data->lambda_file = fopen(buff, "w");
117108
}
109+
110+
data->smoothing_window = MIN_SMOOTHING_WINDOW;
111+
118112
return data;
119113
}
120114

121-
void kvz_free_rc_data() {
115+
void kvz_free_rc_data(kvz_rc_data *data) {
122116
if (data == NULL) return;
123117

124118
pthread_mutex_destroy(&data->ck_frame_lock);
@@ -189,16 +183,16 @@ static double gop_allocate_bits(encoder_state_t * const state)
189183
bits_coded -= state->frame->cur_gop_bits_coded;
190184
}
191185

192-
smoothing_window = MAX(MIN_SMOOTHING_WINDOW, smoothing_window - MAX(encoder->cfg.gop_len / 2, 1));
186+
state->frame->new_ratecontrol->smoothing_window = MAX(MIN_SMOOTHING_WINDOW, state->frame->new_ratecontrol->smoothing_window - MAX(encoder->cfg.gop_len / 2, 1));
193187
double gop_target_bits = -1;
194188

195-
while( gop_target_bits < 0 && smoothing_window < 150) {
189+
while( gop_target_bits < 0 && state->frame->new_ratecontrol->smoothing_window < 150) {
196190
// Equation 12 from https://doi.org/10.1109/TIP.2014.2336550
197191
gop_target_bits =
198-
(encoder->target_avg_bppic * (pictures_coded + smoothing_window) - bits_coded)
199-
* MAX(1, encoder->cfg.gop_len) / smoothing_window;
192+
(encoder->target_avg_bppic * (pictures_coded + state->frame->new_ratecontrol->smoothing_window) - bits_coded)
193+
* MAX(1, encoder->cfg.gop_len) / state->frame->new_ratecontrol->smoothing_window;
200194
if(gop_target_bits < 0) {
201-
smoothing_window += 10;
195+
state->frame->new_ratecontrol->smoothing_window += 10;
202196
}
203197
}
204198
// Allocate at least 200 bits for each GOP like HM does.
@@ -924,10 +918,10 @@ void kvz_update_after_picture(encoder_state_t * const state) {
924918

925919
if (encoder->cfg.stats_file_prefix) {
926920
int poc = calc_poc(state);
927-
fprintf(dist_file, "%d %d %d\n", poc, encoder->in.width_in_lcu, encoder->in.height_in_lcu);
928-
fprintf(bits_file, "%d %d %d\n", poc, encoder->in.width_in_lcu, encoder->in.height_in_lcu);
929-
fprintf(qp_file, "%d %d %d\n", poc, encoder->in.width_in_lcu, encoder->in.height_in_lcu);
930-
fprintf(lambda_file, "%d %d %d\n", poc, encoder->in.width_in_lcu, encoder->in.height_in_lcu);
921+
fprintf(state->frame->new_ratecontrol->dist_file, "%d %d %d\n", poc, encoder->in.width_in_lcu, encoder->in.height_in_lcu);
922+
fprintf(state->frame->new_ratecontrol->bits_file, "%d %d %d\n", poc, encoder->in.width_in_lcu, encoder->in.height_in_lcu);
923+
fprintf(state->frame->new_ratecontrol->qp_file, "%d %d %d\n", poc, encoder->in.width_in_lcu, encoder->in.height_in_lcu);
924+
fprintf(state->frame->new_ratecontrol->lambda_file, "%d %d %d\n", poc, encoder->in.width_in_lcu, encoder->in.height_in_lcu);
931925
}
932926

933927
for(int y_ctu = 0; y_ctu < state->encoder_control->in.height_in_lcu; y_ctu++) {
@@ -945,17 +939,17 @@ void kvz_update_after_picture(encoder_state_t * const state) {
945939
total_distortion += (double)ctu_distortion / ctu->pixels;
946940
lambda += ctu->lambda / (state->encoder_control->in.width_in_lcu * state->encoder_control->in.height_in_lcu);
947941
if(encoder->cfg.stats_file_prefix) {
948-
fprintf(dist_file, "%f ", ctu->distortion);
949-
fprintf(bits_file, "%d ", ctu->bits);
950-
fprintf(qp_file, "%d ", ctu->adjust_qp ? ctu->adjust_qp : ctu->qp);
951-
fprintf(lambda_file, "%f ", ctu->adjust_lambda ? ctu->adjust_lambda : ctu->lambda);
942+
fprintf(state->frame->new_ratecontrol->dist_file, "%f ", ctu->distortion);
943+
fprintf(state->frame->new_ratecontrol->bits_file, "%d ", ctu->bits);
944+
fprintf(state->frame->new_ratecontrol->qp_file, "%d ", ctu->adjust_qp ? ctu->adjust_qp : ctu->qp);
945+
fprintf(state->frame->new_ratecontrol->lambda_file, "%f ", ctu->adjust_lambda ? ctu->adjust_lambda : ctu->lambda);
952946
}
953947
}
954948
if (encoder->cfg.stats_file_prefix) {
955-
fprintf(dist_file, "\n");
956-
fprintf(bits_file, "\n");
957-
fprintf(qp_file, "\n");
958-
fprintf(lambda_file, "\n");
949+
fprintf(state->frame->new_ratecontrol->dist_file, "\n");
950+
fprintf(state->frame->new_ratecontrol->bits_file, "\n");
951+
fprintf(state->frame->new_ratecontrol->qp_file, "\n");
952+
fprintf(state->frame->new_ratecontrol->lambda_file, "\n");
959953
}
960954
}
961955

src/rate_control.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,17 @@ typedef struct kvz_rc_data {
6262
pthread_mutex_t ck_frame_lock;
6363
pthread_mutex_t lambda_lock;
6464
pthread_mutex_t intra_lock;
65+
66+
int smoothing_window;
67+
68+
FILE* dist_file;
69+
FILE* bits_file;
70+
FILE* qp_file;
71+
FILE* lambda_file;
6572
} kvz_rc_data;
6673

6774
kvz_rc_data * kvz_get_rc_data(const encoder_control_t * const encoder);
68-
void kvz_free_rc_data();
75+
void kvz_free_rc_data(kvz_rc_data* data);
6976

7077
void kvz_set_picture_lambda_and_qp(encoder_state_t * const state);
7178

0 commit comments

Comments
 (0)