From da3e2d1d075c9d602cf6fd1b624c61630aa97ce3 Mon Sep 17 00:00:00 2001 From: Wassim Hamidouche Date: Tue, 15 Nov 2016 14:48:46 +0100 Subject: [PATCH 1/4] resolve parallel encryption --- src/encoder_state-ctors_dtors.c | 7 ------- src/encoderstate.c | 6 +++++- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/encoder_state-ctors_dtors.c b/src/encoder_state-ctors_dtors.c index 234b9f42..c72ab458 100644 --- a/src/encoder_state-ctors_dtors.c +++ b/src/encoder_state-ctors_dtors.c @@ -113,13 +113,6 @@ static int encoder_state_config_tile_init(encoder_state_t * const state, state->tile->wf_jobs = NULL; } state->tile->id = encoder->tiles_tile_id[state->tile->lcu_offset_in_ts]; - - state->tile->dbs_g = NULL; - if (state->encoder_control->cfg->crypto_features) { - state->tile->dbs_g = InitC(); - } - state->tile->m_prev_pos = 0; - return 1; } diff --git a/src/encoderstate.c b/src/encoderstate.c index b53f994c..fb8c324c 100644 --- a/src/encoderstate.c +++ b/src/encoderstate.c @@ -297,7 +297,11 @@ static void encoder_state_encode_leaf(encoder_state_t * const state) { assert(state->lcu_order_count > 0); const kvz_config *cfg = state->encoder_control->cfg; - + if ( state->encoder_control->cfg->crypto_features) { + DeleteCryptoC(state->tile->dbs_g); + state->tile->dbs_g = InitC(); + state->tile->m_prev_pos = 0; + } // Select whether to encode the frame/tile in current thread or to define // wavefront jobs for other threads to handle. bool wavefront = state->type == ENCODER_STATE_TYPE_WAVEFRONT_ROW; From ea82c38906edf0e6dd75764a6e32fa4fc2300964 Mon Sep 17 00:00:00 2001 From: Wassim Hamidouche Date: Tue, 15 Nov 2016 23:43:04 +0100 Subject: [PATCH 2/4] correct memory allocation --- src/encoder_state-ctors_dtors.c | 7 ++++--- src/encoderstate.c | 3 +-- src/extras/crypto.cpp | 20 +++++++++++++------- src/extras/crypto.h | 15 ++++++++------- 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/encoder_state-ctors_dtors.c b/src/encoder_state-ctors_dtors.c index c72ab458..6d6dd1b1 100644 --- a/src/encoder_state-ctors_dtors.c +++ b/src/encoder_state-ctors_dtors.c @@ -124,9 +124,7 @@ static void encoder_state_config_tile_finalize(encoder_state_t * const state) { kvz_videoframe_free(state->tile->frame); state->tile->frame = NULL; - if (state->encoder_control->cfg->crypto_features) { - DeleteCryptoC(state->tile->dbs_g); - } + DeleteCryptoC(state->tile->dbs_g); FREE_POINTER(state->tile->wf_jobs); } @@ -320,6 +318,8 @@ int kvz_encoder_state_init(encoder_state_t * const child_state, encoder_state_t fprintf(stderr, "Could not initialize encoder_state->tile!\n"); return 0; } + + child_state->tile->dbs_g = CreateC(); child_state->slice = MALLOC(encoder_state_config_slice_t, 1); if (!child_state->slice || !encoder_state_config_slice_init(child_state, 0, encoder->in.width_in_lcu * encoder->in.height_in_lcu - 1)) { fprintf(stderr, "Could not initialize encoder_state->slice!\n"); @@ -449,6 +449,7 @@ int kvz_encoder_state_init(encoder_state_t * const child_state, encoder_state_t new_child->type = ENCODER_STATE_TYPE_TILE; new_child->frame = child_state->frame; new_child->tile = MALLOC(encoder_state_config_tile_t, 1); + new_child->tile->dbs_g = CreateC(); new_child->slice = child_state->slice; new_child->wfrow = child_state->wfrow; diff --git a/src/encoderstate.c b/src/encoderstate.c index fb8c324c..172c101a 100644 --- a/src/encoderstate.c +++ b/src/encoderstate.c @@ -298,8 +298,7 @@ static void encoder_state_encode_leaf(encoder_state_t * const state) { const kvz_config *cfg = state->encoder_control->cfg; if ( state->encoder_control->cfg->crypto_features) { - DeleteCryptoC(state->tile->dbs_g); - state->tile->dbs_g = InitC(); + InitC(state->tile->dbs_g); state->tile->m_prev_pos = 0; } // Select whether to encode the frame/tile in current thread or to define diff --git a/src/extras/crypto.cpp b/src/extras/crypto.cpp index cb2f4308..b0d9797d 100644 --- a/src/extras/crypto.cpp +++ b/src/extras/crypto.cpp @@ -19,9 +19,12 @@ typedef struct AESDecoder { } AESDecoder; -AESDecoder* Init() { +AESDecoder* Create() { + AESDecoder * AESdecoder = (AESDecoder *)malloc(sizeof(AESDecoder)); + return AESdecoder; +} +void Init(AESDecoder* AESdecoder) { int init_val[32] = {201, 75, 219, 152, 6, 245, 237, 107, 179, 194, 81, 29, 66, 98, 198, 0, 16, 213, 27, 56, 255, 127, 242, 112, 97, 126, 197, 204, 25, 59, 38, 30}; - AESDecoder * AESdecoder = (AESDecoder *)malloc(sizeof(AESDecoder)); for(int i=0;i<16; i++) { AESdecoder->iv [i] = init_val[i]; AESdecoder->counter[i] = init_val[5+i]; @@ -35,7 +38,6 @@ AESDecoder* Init() { AESdecoder->couter_avail = 0; AESdecoder->counter_index = 0; AESdecoder->counter_index_pos = 0; - return AESdecoder; } void DeleteCrypto(AESDecoder * AESdecoder) { @@ -105,11 +107,15 @@ unsigned int get_key (AESDecoder * AESdecoder, int nb_bits) { return key_; } #endif - -Crypto_Handle InitC(){ - AESDecoder* AESdecoder = Init(); - return AESdecoder; +Crypto_Handle CreateC() { + AESDecoder* AESdecoder = Create(); + return AESdecoder; } + +void InitC(Crypto_Handle hdl) { + Init((AESDecoder*)hdl); +} + #if AESEncryptionStreamMode unsigned int ff_get_key (Crypto_Handle *hdl, int nb_bits) { return get_key ((AESDecoder*)*hdl, nb_bits); diff --git a/src/extras/crypto.h b/src/extras/crypto.h index 22efedf5..80ce4429 100644 --- a/src/extras/crypto.h +++ b/src/extras/crypto.h @@ -16,8 +16,8 @@ extern "C" { #endif typedef void* Crypto_Handle; - - STUBBED Crypto_Handle InitC(); + STUBBED Crypto_Handle CreateC(); + STUBBED void InitC(Crypto_Handle hdl); STUBBED void DecryptC(Crypto_Handle hdl, const unsigned char *in_stream, int size_bits, unsigned char *out_stream); #if AESEncryptionStreamMode STUBBED unsigned int ff_get_key(Crypto_Handle *hdl, int nb_bits); @@ -38,11 +38,12 @@ extern "C" { #include -static INLINE Crypto_Handle InitC() -{ - // Stub. - assert(0); - return 0; +static INLINE Crypto_Handle CreateC() { + assert(0); + return 0; +} +static INLINE void InitC(Crypto_Handle hdl) { + assert(0); } static INLINE void DecryptC(Crypto_Handle hdl, const unsigned char *in_stream, From 8951e34fd25e222ccccd1e8b3d1f83a6305dbd2f Mon Sep 17 00:00:00 2001 From: Ari Koivula Date: Wed, 16 Nov 2016 13:26:45 +0200 Subject: [PATCH 3/4] Change crypto.h stubs to print instead of assert --- src/extras/crypto.h | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/extras/crypto.h b/src/extras/crypto.h index 80ce4429..634bf435 100644 --- a/src/extras/crypto.h +++ b/src/extras/crypto.h @@ -36,28 +36,36 @@ extern "C" { // Provide them in the header so we can avoid compiling the cpp file, which // means we don't need a C++ compiler when crypto is not enabled. -#include +#include +#include +#include + +static uint64_t handle_id = 1; static INLINE Crypto_Handle CreateC() { - assert(0); - return 0; + printf("Crypto CreateC %" PRIu64 "\n", (uint64_t)handle_id); + return (void*)(handle_id++); } static INLINE void InitC(Crypto_Handle hdl) { - assert(0); + printf("Crypto InitC %" PRIu64 "\n", (uint64_t)hdl); } static INLINE void DecryptC(Crypto_Handle hdl, const unsigned char *in_stream, int size_bits, unsigned char *out_stream) { // Stub. - assert(0); + printf("Crypto DecryptC %" PRIu64 "\n", (uint64_t)hdl); } #if AESEncryptionStreamMode static INLINE unsigned int ff_get_key(Crypto_Handle *hdl, int nb_bits) { // Stub. - assert(0); + static Crypto_Handle ff_get_key_last_hdl = 0; + if (*hdl != ff_get_key_last_hdl) { + printf("Crypto ff_get_key %" PRIu64 "\n", (uint64_t)*hdl); + } + ff_get_key_last_hdl = *hdl; return 0; } #endif @@ -65,7 +73,7 @@ static INLINE unsigned int ff_get_key(Crypto_Handle *hdl, int nb_bits) static INLINE void DeleteCryptoC(Crypto_Handle hdl) { // Stub. - assert(0); + printf("Crypto DeleteCryptoC %" PRIu64 "\n", (uint64_t)hdl); } #endif // KVZ_SEL_ENCRYPTION From 24f2a23ef8aeec87303b2816edbcb2be0ce0e6f2 Mon Sep 17 00:00:00 2001 From: Ari Koivula Date: Wed, 16 Nov 2016 13:31:26 +0200 Subject: [PATCH 4/4] Remove unnecessary crypto state The frame does not need it's own crypto state, since it always has at least one sub tile. --- src/encoder_state-ctors_dtors.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/encoder_state-ctors_dtors.c b/src/encoder_state-ctors_dtors.c index 6d6dd1b1..24b7add2 100644 --- a/src/encoder_state-ctors_dtors.c +++ b/src/encoder_state-ctors_dtors.c @@ -124,7 +124,9 @@ static void encoder_state_config_tile_finalize(encoder_state_t * const state) { kvz_videoframe_free(state->tile->frame); state->tile->frame = NULL; - DeleteCryptoC(state->tile->dbs_g); + if (state->encoder_control->cfg->crypto_features && state->tile->dbs_g) { + DeleteCryptoC(state->tile->dbs_g); + } FREE_POINTER(state->tile->wf_jobs); } @@ -319,7 +321,7 @@ int kvz_encoder_state_init(encoder_state_t * const child_state, encoder_state_t return 0; } - child_state->tile->dbs_g = CreateC(); + child_state->tile->dbs_g = NULL; // Not used. The used state is in the sub-tile. child_state->slice = MALLOC(encoder_state_config_slice_t, 1); if (!child_state->slice || !encoder_state_config_slice_init(child_state, 0, encoder->in.width_in_lcu * encoder->in.height_in_lcu - 1)) { fprintf(stderr, "Could not initialize encoder_state->slice!\n"); @@ -449,7 +451,9 @@ int kvz_encoder_state_init(encoder_state_t * const child_state, encoder_state_t new_child->type = ENCODER_STATE_TYPE_TILE; new_child->frame = child_state->frame; new_child->tile = MALLOC(encoder_state_config_tile_t, 1); - new_child->tile->dbs_g = CreateC(); + if (child_state->encoder_control->cfg->crypto_features) { + new_child->tile->dbs_g = CreateC(); + } new_child->slice = child_state->slice; new_child->wfrow = child_state->wfrow;