diff --git a/src/encoderstate.c b/src/encoderstate.c index d72cd853..dcc526db 100644 --- a/src/encoderstate.c +++ b/src/encoderstate.c @@ -37,6 +37,8 @@ #include "tables.h" #include "threadqueue.h" +#include "strategies/strategies-picture.h" + int kvz_encoder_state_match_children_of_previous_frame(encoder_state_t * const state) { int i; @@ -1190,31 +1192,6 @@ static void normalize_lcu_weights(encoder_state_t * const state) } } -// Calculate pixel value variance. Takes in arrays of kvz_pixel -static double pixel_var(kvz_pixel * const arr, const uint32_t len) { - double var = 0; - double arr_mean = 0; - - // Calculate array mean - int i = 0; - double sum = 0; - - for (; i < len; ++i) { - sum += arr[i]; - } - arr_mean = sum / (double)len; - - // Calculate array variance - for (i = 0; i < len; ++i) { - double tmp = (double)arr[i] - arr_mean; - var += tmp*tmp; - } - - var /= len; - - return var; -} - static void encoder_state_init_new_frame(encoder_state_t * const state, kvz_picture* frame) { assert(state->type == ENCODER_STATE_TYPE_MAIN); @@ -1235,7 +1212,7 @@ static void encoder_state_init_new_frame(encoder_state_t * const state, kvz_pict // Calculate frame pixel variance uint32_t len = state->tile->frame->width * state->tile->frame->height; - double frame_var = pixel_var(state->tile->frame->source->y, len); + double frame_var = kvz_pixel_var(state->tile->frame->source->y, len); // Loop through LCUs // For each LCU calculate: D * (log(LCU pixel variance) - log(frame pixel variance)) @@ -1255,7 +1232,7 @@ static void encoder_state_init_new_frame(encoder_state_t * const state, kvz_pict kvz_pixels_blit(&state->tile->frame->source->y[pxl_x + pxl_y * state->tile->frame->source->stride], tmp, x_max, y_max, state->tile->frame->source->stride, LCU_WIDTH); - double lcu_var = pixel_var(tmp, LCU_LUMA_SIZE); + double lcu_var = kvz_pixel_var(tmp, LCU_LUMA_SIZE); // UNCOMMENT AND CONTINUE HERE /* diff --git a/src/strategies/generic/picture-generic.c b/src/strategies/generic/picture-generic.c index 3d0fca9f..53073487 100644 --- a/src/strategies/generic/picture-generic.c +++ b/src/strategies/generic/picture-generic.c @@ -675,6 +675,32 @@ static uint32_t hor_sad_generic(const kvz_pixel *pic_data, const kvz_pixel *ref_ return result; } +// Calculate pixel value variance. Takes in arrays of kvz_pixel +static double pixel_var_generic(const kvz_pixel *arr, const uint32_t len) +{ + double var = 0; + double arr_mean = 0; + + // Calculate array mean + int i = 0; + double sum = 0; + + for (; i < len; ++i) { + sum += arr[i]; + } + arr_mean = sum / (double)len; + + // Calculate array variance + for (i = 0; i < len; ++i) { + double tmp = (double)arr[i] - arr_mean; + var += tmp*tmp; + } + + var /= len; + + return var; +} + int kvz_strategy_register_picture_generic(void* opaque, uint8_t bitdepth) { bool success = true; @@ -714,5 +740,7 @@ int kvz_strategy_register_picture_generic(void* opaque, uint8_t bitdepth) success &= kvz_strategyselector_register(opaque, "ver_sad", "generic", 0, &ver_sad_generic); success &= kvz_strategyselector_register(opaque, "hor_sad", "generic", 0, &hor_sad_generic); + success &= kvz_strategyselector_register(opaque, "pixel_var", "generic", 0, &pixel_var_generic); + return success; } diff --git a/src/strategies/strategies-picture.c b/src/strategies/strategies-picture.c index c9615546..37431544 100644 --- a/src/strategies/strategies-picture.c +++ b/src/strategies/strategies-picture.c @@ -67,6 +67,8 @@ get_optimized_sad_func *kvz_get_optimized_sad = 0; ver_sad_func *kvz_ver_sad = 0; hor_sad_func *kvz_hor_sad = 0; +pixel_var_func *kvz_pixel_var = 0; + int kvz_strategy_register_picture(void* opaque, uint8_t bitdepth) { bool success = true; diff --git a/src/strategies/strategies-picture.h b/src/strategies/strategies-picture.h index f6f7340f..c5545628 100644 --- a/src/strategies/strategies-picture.h +++ b/src/strategies/strategies-picture.h @@ -138,6 +138,8 @@ typedef void (inter_recon_bipred_func)(const int hi_prec_luma_rec0, bool predict_luma, bool predict_chroma); +typedef double (pixel_var_func)(const kvz_pixel *buf, const uint32_t len); + // Declare function pointers. extern reg_sad_func * kvz_reg_sad; @@ -176,6 +178,8 @@ extern get_optimized_sad_func *kvz_get_optimized_sad; extern ver_sad_func *kvz_ver_sad; extern hor_sad_func *kvz_hor_sad; +extern pixel_var_func *kvz_pixel_var; + int kvz_strategy_register_picture(void* opaque, uint8_t bitdepth); cost_pixel_nxn_func * kvz_pixels_get_satd_func(unsigned n); cost_pixel_nxn_func * kvz_pixels_get_sad_func(unsigned n); @@ -211,6 +215,7 @@ cost_pixel_nxn_multi_func * kvz_pixels_get_sad_dual_func(unsigned n); {"get_optimized_sad", (void**) &kvz_get_optimized_sad}, \ {"ver_sad", (void**) &kvz_ver_sad}, \ {"hor_sad", (void**) &kvz_hor_sad}, \ + {"pixel_var", (void**) &kvz_pixel_var}, \