compile Kvazaar encoder with ITpp library

This commit is contained in:
Wassim Hamidouche 2016-06-06 16:23:47 +02:00
parent 8f182ac6de
commit 4637c8a828
7 changed files with 179 additions and 14 deletions

View file

@ -38,6 +38,7 @@ AM_SILENT_RULES([yes])
AC_PROG_CC AC_PROG_CC
AC_PROG_CC_C99 AC_PROG_CC_C99
AM_PROG_AR AM_PROG_AR
AC_PROG_CXX
# Get fread that can read more than 2GB on 32 bit systems. # Get fread that can read more than 2GB on 32 bit systems.
AC_SYS_LARGEFILE AC_SYS_LARGEFILE
@ -52,14 +53,31 @@ AM_CONDITIONAL([HAVE_AVX2], [test x"$flag_avx2" = x"true"])
AM_CONDITIONAL([HAVE_SSE4_1], [test x"$flag_sse4_1" = x"true"]) AM_CONDITIONAL([HAVE_SSE4_1], [test x"$flag_sse4_1" = x"true"])
AM_CONDITIONAL([HAVE_SSE2], [test x"$flag_sse2" = x"true"]) AM_CONDITIONAL([HAVE_SSE2], [test x"$flag_sse2" = x"true"])
KVZ_CFLAGS="-Wall -Wtype-limits -Wvla -I$srcdir/src -ftree-vectorize -fvisibility=hidden" KVZ_CFLAGS="-Wall -Wtype-limits -Wvla -I$srcdir/src -I$srcdir/src/extras -ftree-vectorize -fvisibility=hidden"
CFLAGS="$KVZ_CFLAGS $CFLAGS" CFLAGS="$KVZ_CFLAGS $CFLAGS"
AC_ARG_WITH([libcryptopp],
AS_HELP_STRING([--with-cryptopp],
[Build with libcryptopp Enables selective encryption.]))
AS_IF([test "x$with_libcryptopp" = "xyes"], [
PKG_CHECK_MODULES([libcryptopp], [cryptopp],
AC_DEFINE([KVC_SEL_ENCRYPTION], [1], [With libcryptopp]),
AC_MSG_ERROR([libcryptopp not found with pkg-config])
)
])
CPPFLAGS="$CPPFLAGS $libcryptopp_CFLAGS"
LIBS="$LIBS $libcryptopp_LIBS"
CPPFLAGS="-DKVZ_DLL_EXPORTS $CPPFLAGS" CPPFLAGS="-DKVZ_DLL_EXPORTS $CPPFLAGS"
AC_SEARCH_LIBS([log], [m c], [], [exit 1]) AC_SEARCH_LIBS([log], [m c], [], [exit 1])
AC_SEARCH_LIBS([pow], [m c], [], [exit 1]) AC_SEARCH_LIBS([pow], [m c], [], [exit 1])
AC_SEARCH_LIBS([sqrt], [m c], [], [exit 1]) AC_SEARCH_LIBS([sqrt], [m c], [], [exit 1])
# This does workarounds for pthreads on various compilers. # This does workarounds for pthreads on various compilers.
AX_PTHREAD AX_PTHREAD
CFLAGS="$PTHREAD_CFLAGS $CFLAGS" CFLAGS="$PTHREAD_CFLAGS $CFLAGS"
@ -72,10 +90,6 @@ AC_ARG_ENABLE([werror], [AS_HELP_STRING([--disable-werror], [don't treat warning
) )
# check for getopt
AC_CHECK_HEADER([getopt.h], [], [CFLAGS="$CFLAGS -I$srcdir/src/extras"])
# host and cpu specific settings # host and cpu specific settings
AS_CASE([$host_cpu], AS_CASE([$host_cpu],
[i?86], [BITS="32" ASFLAGS="$ASFLAGS -DARCH_X86_64=0" X86="true"], [i?86], [BITS="32" ASFLAGS="$ASFLAGS -DARCH_X86_64=0" X86="true"],
@ -133,10 +147,8 @@ AC_SUBST([ASFLAGS])
KVZ_API_VERSION="$ver_major:$ver_minor:$ver_release" KVZ_API_VERSION="$ver_major:$ver_minor:$ver_release"
AC_SUBST([KVZ_API_VERSION]) AC_SUBST([KVZ_API_VERSION])
AC_CONFIG_FILES([Makefile AC_CONFIG_FILES([Makefile
src/Makefile src/Makefile
src/kvazaar.pc src/kvazaar.pc
tests/Makefile]) tests/Makefile])
AC_OUTPUT AC_OUTPUT

View file

@ -131,7 +131,9 @@ libkvazaar_la_SOURCES = \
strategyselector.c \ strategyselector.c \
strategyselector.h \ strategyselector.h \
extras/libmd5.c \ extras/libmd5.c \
extras/libmd5.h extras/libmd5.h \
extras/crypto.cpp \
extras/crypto.h
libkvazaar_la_CFLAGS = libkvazaar_la_CFLAGS =

View file

@ -104,8 +104,10 @@ static int encoder_state_config_tile_init(encoder_state_t * const state,
} else { } else {
state->tile->wf_jobs = NULL; state->tile->wf_jobs = NULL;
} }
state->tile->id = encoder->tiles_tile_id[state->tile->lcu_offset_in_ts]; state->tile->id = encoder->tiles_tile_id[state->tile->lcu_offset_in_ts];
state->tile->dbs_g = InitC();
return 1; return 1;
} }

View file

@ -1186,6 +1186,7 @@ static void encode_inter_prediction_unit(encoder_state_t * const state,
const uint32_t mvd_hor_abs = abs(mvd_hor); const uint32_t mvd_hor_abs = abs(mvd_hor);
const uint32_t mvd_ver_abs = abs(mvd_ver); const uint32_t mvd_ver_abs = abs(mvd_ver);
cabac->cur_ctx = &(cabac->ctx.cu_mvd_model[0]); cabac->cur_ctx = &(cabac->ctx.cu_mvd_model[0]);
CABAC_BIN(cabac, (mvd_hor != 0), "abs_mvd_greater0_flag_hor"); CABAC_BIN(cabac, (mvd_hor != 0), "abs_mvd_greater0_flag_hor");
CABAC_BIN(cabac, (mvd_ver != 0), "abs_mvd_greater0_flag_ver"); CABAC_BIN(cabac, (mvd_ver != 0), "abs_mvd_greater0_flag_ver");
@ -1204,16 +1205,21 @@ static void encode_inter_prediction_unit(encoder_state_t * const state,
if (mvd_hor_abs > 1) { if (mvd_hor_abs > 1) {
kvz_cabac_write_ep_ex_golomb(cabac,mvd_hor_abs-2, 1); kvz_cabac_write_ep_ex_golomb(cabac,mvd_hor_abs-2, 1);
} }
uint8_t mvd_hor_sign = (mvd_hor>0)?0:1;
CABAC_BIN_EP(cabac, (mvd_hor>0)?0:1, "mvd_sign_flag_hor"); #if KVC_SEL_ENCRYPTION
mvd_hor_sign = mvd_hor^ff_get_key(&state->tile->dbs_g, 1);
#endif
CABAC_BIN_EP(cabac, mvd_hor_sign, "mvd_sign_flag_hor");
} }
if (ver_abs_gr0) { if (ver_abs_gr0) {
if (mvd_ver_abs > 1) { if (mvd_ver_abs > 1) {
kvz_cabac_write_ep_ex_golomb(cabac,mvd_ver_abs-2, 1); kvz_cabac_write_ep_ex_golomb(cabac,mvd_ver_abs-2, 1);
} }
uint8_t mvd_ver_sign = (mvd_ver>0)?0:1;
CABAC_BIN_EP(cabac, (mvd_ver>0)?0:1, "mvd_sign_flag_ver"); #if KVC_SEL_ENCRYPTION
mvd_ver_sign = mvd_ver^ff_get_key(&state->tile->dbs_g, 1);
#endif
CABAC_BIN_EP(cabac, mvd_ver_sign, "mvd_sign_flag_ver");
} }
} }

View file

@ -37,6 +37,7 @@
#include "tables.h" #include "tables.h"
#include "threadqueue.h" #include "threadqueue.h"
#include "videoframe.h" #include "videoframe.h"
#include "extras/crypto.h"
typedef enum { typedef enum {
@ -113,6 +114,10 @@ typedef struct {
//Jobs for each individual LCU of a wavefront row. //Jobs for each individual LCU of a wavefront row.
threadqueue_job_t **wf_jobs; threadqueue_job_t **wf_jobs;
// Instance of encryption generator by tile
Crypto_Handle dbs_g;
} encoder_state_config_tile_t; } encoder_state_config_tile_t;
typedef struct { typedef struct {

119
src/extras/crypto.cpp Normal file
View file

@ -0,0 +1,119 @@
#include <extras/crypto.h>
#include <cryptopp/aes.h>
#include <cryptopp/modes.h>
#include <cryptopp/osrng.h>
typedef struct AESDecoder {
#if AESEncryptionStreamMode
CryptoPP::CFB_Mode<CryptoPP::AES>::Encryption *CFBdec;
#else
CryptoPP::CFB_Mode<CryptoPP::AES>::Decryption *CFBdec;
#endif
byte key[CryptoPP::AES::DEFAULT_KEYLENGTH], iv[CryptoPP::AES::BLOCKSIZE], out_stream_counter[CryptoPP::AES::BLOCKSIZE], counter[CryptoPP::AES::BLOCKSIZE];
int couter_avail, counter_index, counter_index_pos;
} AESDecoder;
AESDecoder* Init() {
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];
AESdecoder->key[i] = init_val[i+16];
}
#if AESEncryptionStreamMode
AESdecoder->CFBdec = new CryptoPP::CFB_Mode<CryptoPP::AES >::Encryption(AESdecoder->key, CryptoPP::AES::DEFAULT_KEYLENGTH, AESdecoder->iv);
#else
AESdecoder->CFBdec = new CryptoPP::CFB_Mode<CryptoPP::AES >::Decryption(AESdecoder->key, CryptoPP::AES::DEFAULT_KEYLENGTH, AESdecoder->iv);
#endif
AESdecoder->couter_avail = 0;
AESdecoder->counter_index = 0;
AESdecoder->counter_index_pos = 0;
return AESdecoder;
}
void DeleteCrypto(AESDecoder * AESdecoder) {
if(AESdecoder)
free(AESdecoder);
}
void Decrypt(AESDecoder *AESdecoder, const unsigned char *in_stream, int size_bits, unsigned char *out_stream) {
int nb_bytes = ceil((double)size_bits/8);
AESdecoder->CFBdec->ProcessData(out_stream, in_stream, nb_bytes);
if(size_bits&7)
AESdecoder->CFBdec->SetKeyWithIV(AESdecoder->key, CryptoPP::AES::DEFAULT_KEYLENGTH, AESdecoder->iv);
}
void Incr_counter (unsigned char *counter) {
counter[0]++;
}
#if AESEncryptionStreamMode
void Decrypt_counter(AESDecoder * AESdecoder) {
AESdecoder->CFBdec->ProcessData(AESdecoder->out_stream_counter, AESdecoder->counter, 16);
AESdecoder->couter_avail = 128;
AESdecoder->counter_index = 15;
AESdecoder->counter_index_pos = 8;
Incr_counter(AESdecoder->counter);
}
#endif
#if AESEncryptionStreamMode
unsigned int get_key (AESDecoder * AESdecoder, int nb_bits) {
unsigned int key_ = 0;
if(nb_bits > 32) {
printf("The Generator can not generate more than 32 bit %d \n", nb_bits);
return 0;
}
if( !nb_bits )
return 0;
if(!AESdecoder->couter_avail)
Decrypt_counter(AESdecoder);
if(AESdecoder->couter_avail >= nb_bits)
AESdecoder->couter_avail -= nb_bits;
else
AESdecoder->couter_avail = 0;
int nb = 0;
while( nb_bits ) {
if( nb_bits >= AESdecoder->counter_index_pos )
nb = AESdecoder->counter_index_pos;
else
nb = nb_bits;
key_ <<= nb;
key_ += (AESdecoder->out_stream_counter[AESdecoder->counter_index] & ((1<<nb)-1));
AESdecoder->out_stream_counter[AESdecoder->counter_index] >>= nb;
nb_bits -= nb;
if(AESdecoder->counter_index && nb == AESdecoder->counter_index_pos ) {
AESdecoder->counter_index--;
AESdecoder->counter_index_pos = 8;
} else {
AESdecoder->counter_index_pos -= nb;
if(nb_bits) {
Decrypt_counter(AESdecoder);
AESdecoder->couter_avail -= nb_bits;
}
}
}
return key_;
}
#endif
Crypto_Handle InitC(){
AESDecoder* AESdecoder = Init();
return AESdecoder;
}
#if AESEncryptionStreamMode
unsigned int ff_get_key (Crypto_Handle *hdl, int nb_bits) {
return get_key ((AESDecoder*)*hdl, nb_bits);
}
#endif
void DecryptC(Crypto_Handle hdl, const unsigned char *in_stream, int size_bits, unsigned char *out_stream) {
Decrypt((AESDecoder*)hdl, in_stream, size_bits, out_stream);
}
void DeleteCryptoC(Crypto_Handle hdl) {
DeleteCryptoC(hdl);
}

19
src/extras/crypto.h Normal file
View file

@ -0,0 +1,19 @@
#include <stdio.h>
#include <math.h>
#define AESEncryptionStreamMode 1
#ifdef __cplusplus
extern "C" {
#endif
typedef void* Crypto_Handle;
Crypto_Handle InitC();
void DecryptC(Crypto_Handle hdl, const unsigned char *in_stream, int size_bits, unsigned char *out_stream);
#if AESEncryptionStreamMode
unsigned int ff_get_key (Crypto_Handle *hdl, int nb_bits);
#endif
void DeleteCryptoC(Crypto_Handle hdl);
#ifdef __cplusplus
}
#endif