2014-04-29 08:14:42 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* This file is part of Kvazaar HEVC encoder.
|
|
|
|
*
|
2015-02-23 11:18:48 +00:00
|
|
|
* Copyright (C) 2013-2015 Tampere University of Technology and others (see
|
2014-04-29 08:14:42 +00:00
|
|
|
* COPYING file).
|
|
|
|
*
|
2015-02-23 11:18:48 +00:00
|
|
|
* Kvazaar is free software: you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2.1 of the License, or (at your
|
|
|
|
* option) any later version.
|
2014-04-29 08:14:42 +00:00
|
|
|
*
|
2015-02-23 11:18:48 +00:00
|
|
|
* Kvazaar is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
|
|
|
|
* more details.
|
2014-04-29 08:14:42 +00:00
|
|
|
*
|
2015-02-23 11:18:48 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with Kvazaar. If not, see <http://www.gnu.org/licenses/>.
|
2014-04-29 08:14:42 +00:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* \file
|
|
|
|
*/
|
|
|
|
|
2014-06-13 07:20:02 +00:00
|
|
|
#include "strategyselector.h"
|
|
|
|
|
2014-04-29 12:28:16 +00:00
|
|
|
#include <assert.h>
|
2014-04-29 08:14:42 +00:00
|
|
|
#include <string.h>
|
2014-06-13 07:20:02 +00:00
|
|
|
#include <stdlib.h>
|
2014-06-16 09:24:02 +00:00
|
|
|
#if COMPILE_INTEL
|
2014-06-12 15:16:03 +00:00
|
|
|
#include <immintrin.h>
|
2014-06-16 09:24:02 +00:00
|
|
|
#endif
|
2014-04-29 08:14:42 +00:00
|
|
|
|
|
|
|
hardware_flags g_hardware_flags;
|
|
|
|
|
2014-10-14 09:01:56 +00:00
|
|
|
static void set_hardware_flags(int32_t cpuid);
|
2014-04-29 08:14:42 +00:00
|
|
|
static void* strategyselector_choose_for(const strategy_list * const strategies, const char * const strategy_type);
|
|
|
|
|
|
|
|
//Strategies to include (add new file here)
|
|
|
|
|
|
|
|
//Returns 1 if successful
|
2014-10-14 09:01:56 +00:00
|
|
|
int strategyselector_init(int32_t cpuid) {
|
2014-04-29 08:14:42 +00:00
|
|
|
const strategy_to_select *cur_strategy_to_select = strategies_to_select;
|
|
|
|
strategy_list strategies;
|
|
|
|
|
|
|
|
strategies.allocated = 0;
|
|
|
|
strategies.count = 0;
|
|
|
|
strategies.strategies = NULL;
|
|
|
|
|
2014-10-14 09:01:56 +00:00
|
|
|
set_hardware_flags(cpuid);
|
2014-04-29 08:14:42 +00:00
|
|
|
|
|
|
|
//Add new register function here
|
|
|
|
if (!strategy_register_picture(&strategies)) {
|
|
|
|
fprintf(stderr, "strategy_register_picture failed!\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-02 09:20:20 +00:00
|
|
|
if (!strategy_register_nal(&strategies)) {
|
|
|
|
fprintf(stderr, "strategy_register_nal failed!\n");
|
|
|
|
return 0;
|
2014-07-23 12:15:09 +00:00
|
|
|
}
|
|
|
|
|
2014-07-29 15:10:47 +00:00
|
|
|
if (!strategy_register_dct(&strategies)) {
|
2015-02-05 12:07:26 +00:00
|
|
|
fprintf(stderr, "strategy_register_dct failed!\n");
|
2014-07-23 12:15:09 +00:00
|
|
|
return 0;
|
2014-06-02 09:20:20 +00:00
|
|
|
}
|
2014-11-20 16:38:54 +00:00
|
|
|
|
|
|
|
if (!strategy_register_ipol(&strategies)) {
|
|
|
|
fprintf(stderr, "strategy_register_ipol failed!\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-02 09:20:20 +00:00
|
|
|
|
2014-04-29 08:14:42 +00:00
|
|
|
while(cur_strategy_to_select->fptr) {
|
|
|
|
*(cur_strategy_to_select->fptr) = strategyselector_choose_for(&strategies, cur_strategy_to_select->strategy_type);
|
|
|
|
|
|
|
|
if (!(*(cur_strategy_to_select->fptr))) {
|
|
|
|
fprintf(stderr, "Could not find a strategy for %s!\n", cur_strategy_to_select->strategy_type);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
++cur_strategy_to_select;
|
|
|
|
}
|
|
|
|
|
|
|
|
//We can free the structure now, as all strategies are statically set to pointers
|
|
|
|
if (strategies.allocated) {
|
|
|
|
free(strategies.strategies);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void strategyselector_free() {
|
|
|
|
//Do nothing (yet)
|
|
|
|
}
|
|
|
|
|
|
|
|
//Returns 1 if successful, 0 otherwise
|
|
|
|
int strategyselector_register(void * const opaque, const char * const type, const char * const strategy_name, int priority, void * const fptr) {
|
|
|
|
strategy_list * const strategies = opaque;
|
|
|
|
|
|
|
|
if (strategies->allocated == strategies->count) {
|
|
|
|
strategy* new_strategies = realloc(strategies->strategies, sizeof(strategy) * (strategies->allocated + STRATEGY_LIST_ALLOC_SIZE));
|
|
|
|
if (!new_strategies) {
|
|
|
|
fprintf(stderr, "Could not increase strategies list size!\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
strategies->strategies = new_strategies;
|
|
|
|
strategies->allocated += STRATEGY_LIST_ALLOC_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
strategy *new_strategy = &strategies->strategies[strategies->count++];
|
|
|
|
new_strategy->type = type;
|
|
|
|
new_strategy->strategy_name = strategy_name;
|
|
|
|
new_strategy->priority = priority;
|
|
|
|
new_strategy->fptr = fptr;
|
|
|
|
}
|
2014-06-16 09:15:19 +00:00
|
|
|
#ifdef DEBUG_STRATEGYSELECTOR
|
2014-04-29 08:14:42 +00:00
|
|
|
fprintf(stderr, "Registered strategy %s:%s with priority %d (%p)\n", type, strategy_name, priority, fptr);
|
2014-06-16 09:15:19 +00:00
|
|
|
#endif //DEBUG_STRATEGYSELECTOR
|
2014-04-29 08:14:42 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* strategyselector_choose_for(const strategy_list * const strategies, const char * const strategy_type) {
|
|
|
|
unsigned int max_priority = 0;
|
|
|
|
int max_priority_i = -1;
|
|
|
|
char buffer[256];
|
|
|
|
char *override = NULL;
|
|
|
|
int i = 0;
|
|
|
|
|
2014-11-12 14:31:55 +00:00
|
|
|
// Because VS doesn't support snprintf, let's assert that there is
|
|
|
|
// enough room in the buffer. Max length for strategy type is
|
|
|
|
// buffersize (256) - prefix including terminating zero.
|
|
|
|
assert(strlen(strategy_type) < 256 - sizeof("KVAZAAR_OVERRIDE_") );
|
2014-04-29 12:28:16 +00:00
|
|
|
sprintf(buffer, "KVAZAAR_OVERRIDE_%s", strategy_type);
|
|
|
|
|
2014-04-29 08:14:42 +00:00
|
|
|
override = getenv(buffer);
|
|
|
|
|
|
|
|
for (i=0; i < strategies->count; ++i) {
|
|
|
|
if (strcmp(strategies->strategies[i].type, strategy_type) == 0) {
|
|
|
|
if (override && strcmp(strategies->strategies[i].strategy_name, override) == 0) {
|
|
|
|
fprintf(stderr, "%s environment variable present, choosing %s:%s\n", buffer, strategy_type, strategies->strategies[i].strategy_name);
|
|
|
|
return strategies->strategies[i].fptr;
|
|
|
|
}
|
|
|
|
if (strategies->strategies[i].priority >= max_priority) {
|
|
|
|
max_priority_i = i;
|
|
|
|
max_priority = strategies->strategies[i].priority;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (override) {
|
|
|
|
fprintf(stderr, "%s environment variable present, but no strategy %s was found!\n", buffer, override);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-06-16 09:15:19 +00:00
|
|
|
#ifdef DEBUG_STRATEGYSELECTOR
|
2014-04-29 08:14:42 +00:00
|
|
|
fprintf(stderr, "Choosing strategy for %s:\n", strategy_type);
|
|
|
|
for (i=0; i < strategies->count; ++i) {
|
|
|
|
if (strcmp(strategies->strategies[i].type, strategy_type) == 0) {
|
|
|
|
if (i != max_priority_i) {
|
|
|
|
fprintf(stderr, "- %s (%d, %p)\n", strategies->strategies[i].strategy_name, strategies->strategies[i].priority, strategies->strategies[i].fptr);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "> %s (%d, %p)\n", strategies->strategies[i].strategy_name, strategies->strategies[i].priority, strategies->strategies[i].fptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-06-16 09:15:19 +00:00
|
|
|
#endif //DEBUG_STRATEGYSELECTOR
|
2014-04-29 08:14:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (max_priority_i == -1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return strategies->strategies[max_priority_i].fptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if COMPILE_INTEL
|
2014-05-28 12:47:24 +00:00
|
|
|
|
|
|
|
#if defined(__GNUC__)
|
|
|
|
#include <cpuid.h>
|
2014-06-17 13:26:55 +00:00
|
|
|
static INLINE int get_cpuid(unsigned int level, unsigned int *eax, unsigned int *ebx, unsigned int *ecx, unsigned int *edx) {
|
2014-05-29 14:02:44 +00:00
|
|
|
return __get_cpuid(level, eax, ebx, ecx, edx);
|
|
|
|
}
|
2014-05-28 12:47:24 +00:00
|
|
|
#else
|
2014-05-30 05:37:03 +00:00
|
|
|
#include <intrin.h>
|
2014-05-28 12:47:24 +00:00
|
|
|
//Adapter from __cpuid (VS) to __get_cpuid (GNU C).
|
2014-06-17 13:26:55 +00:00
|
|
|
static INLINE int get_cpuid(unsigned int level, unsigned int *eax, unsigned int *ebx, unsigned int *ecx, unsigned int *edx) {
|
2014-05-29 14:02:44 +00:00
|
|
|
int CPUInfo[4] = {*eax, *ebx, *ecx, *edx};
|
2014-05-28 12:47:24 +00:00
|
|
|
__cpuid(CPUInfo, 0);
|
|
|
|
// check if the CPU supports the cpuid instruction.
|
|
|
|
if (CPUInfo[0] != 0) {
|
2014-05-29 14:02:44 +00:00
|
|
|
__cpuid(CPUInfo, level);
|
|
|
|
*eax = CPUInfo[0];
|
|
|
|
*ebx = CPUInfo[1];
|
|
|
|
*ecx = CPUInfo[2];
|
|
|
|
*edx = CPUInfo[3];
|
2014-05-28 12:47:24 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2014-05-30 05:37:03 +00:00
|
|
|
return 0;
|
2014-05-28 12:47:24 +00:00
|
|
|
}
|
|
|
|
#endif //defined(__GNUC__)
|
|
|
|
|
2014-04-29 08:14:42 +00:00
|
|
|
#endif
|
|
|
|
|
2014-06-03 05:39:30 +00:00
|
|
|
#if COMPILE_POWERPC
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <linux/auxvec.h>
|
|
|
|
#include <asm/cputable.h>
|
|
|
|
|
|
|
|
//Source: http://freevec.org/function/altivec_runtime_detection_linux
|
|
|
|
static int altivec_available(void)
|
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
unsigned long buf[64];
|
|
|
|
ssize_t count;
|
|
|
|
int fd, i;
|
|
|
|
|
|
|
|
fd = open("/proc/self/auxv", O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// loop on reading
|
|
|
|
do {
|
|
|
|
count = read(fd, buf, sizeof(buf));
|
|
|
|
if (count < 0)
|
|
|
|
break;
|
|
|
|
for (i=0; i < (count / sizeof(unsigned long)); i += 2) {
|
|
|
|
if (buf[i] == AT_HWCAP) {
|
|
|
|
result = !!(buf[i+1] & PPC_FEATURE_HAS_ALTIVEC);
|
|
|
|
goto out_close;
|
|
|
|
} else if (buf[i] == AT_NULL)
|
|
|
|
goto out_close;
|
|
|
|
}
|
|
|
|
} while (count == sizeof(buf));
|
|
|
|
out_close:
|
|
|
|
close(fd);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif //COMPILE_POWERPC
|
|
|
|
|
2014-10-14 09:01:56 +00:00
|
|
|
static void set_hardware_flags(int32_t cpuid) {
|
2015-02-13 09:56:55 +00:00
|
|
|
FILL(g_hardware_flags, 0);
|
2014-04-29 08:14:42 +00:00
|
|
|
|
|
|
|
g_hardware_flags.arm = COMPILE_ARM;
|
|
|
|
g_hardware_flags.intel = COMPILE_INTEL;
|
|
|
|
g_hardware_flags.powerpc = COMPILE_POWERPC;
|
2014-10-14 09:01:56 +00:00
|
|
|
|
2014-04-29 08:14:42 +00:00
|
|
|
#if COMPILE_INTEL
|
2014-10-14 09:01:56 +00:00
|
|
|
if (cpuid) {
|
2014-05-28 12:47:24 +00:00
|
|
|
unsigned int eax = 0, ebx = 0, ecx = 0, edx =0;
|
2014-04-29 08:14:42 +00:00
|
|
|
/* CPU feature bits */
|
2014-06-12 15:16:03 +00:00
|
|
|
enum { BIT_SSE3 = 0, BIT_SSSE3 = 9, BIT_SSE41 = 19, BIT_SSE42 = 20,
|
|
|
|
BIT_MMX = 24, BIT_SSE = 25, BIT_SSE2 = 26,
|
|
|
|
BIT_OSXSAVE = 27, BIT_AVX = 28};
|
|
|
|
enum { XCR0_XMM = 1, XCR0_YMM = 2 };
|
2014-04-29 08:14:42 +00:00
|
|
|
|
|
|
|
// Dig CPU features with cpuid
|
2014-05-29 14:02:44 +00:00
|
|
|
get_cpuid(1, &eax, &ebx, &ecx, &edx);
|
2014-04-29 08:14:42 +00:00
|
|
|
|
|
|
|
// EDX
|
|
|
|
if (edx & (1<<BIT_MMX)) g_hardware_flags.intel_flags.mmx = 1;
|
|
|
|
if (edx & (1<<BIT_SSE)) g_hardware_flags.intel_flags.sse = 1;
|
|
|
|
if (edx & (1<<BIT_SSE2)) g_hardware_flags.intel_flags.sse2 = 1;
|
|
|
|
// ECX
|
|
|
|
if (ecx & (1<<BIT_SSE3)) g_hardware_flags.intel_flags.sse3 = 1;;
|
|
|
|
if (ecx & (1<<BIT_SSSE3)) g_hardware_flags.intel_flags.ssse3 = 1;
|
|
|
|
if (ecx & (1<<BIT_SSE41)) g_hardware_flags.intel_flags.sse41 = 1;
|
|
|
|
if (ecx & (1<<BIT_SSE42)) g_hardware_flags.intel_flags.sse42 = 1;
|
|
|
|
|
2014-06-12 15:16:03 +00:00
|
|
|
// Check hardware and OS support for AVX.
|
|
|
|
if (ecx & (1 << BIT_OSXSAVE)) {
|
2014-06-18 08:50:17 +00:00
|
|
|
uint64_t xcr0 = 0;
|
|
|
|
// Use _XCR_XFEATURE_ENABLED_MASK to check if _xgetbv intrinsic is
|
|
|
|
// supported by the compiler.
|
|
|
|
#ifdef _XCR_XFEATURE_ENABLED_MASK
|
|
|
|
xcr0 = _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
|
|
|
|
#elif defined(__GNUC__)
|
|
|
|
uint32_t geax = 0;
|
|
|
|
// Apparently there are some older assemblers that don't support xgetbv,
|
|
|
|
// so we use the byte sequence for xgetbv just in case.
|
|
|
|
//__asm__("xgetbv" : "=a" (geax), "=d" (gedx) : "c" (0));
|
|
|
|
__asm__(".byte 0x0f, 0x01, 0xd0" : "=a" (geax) : "c" (0) : "edx");
|
|
|
|
// edx is spillover, but we don't care about those bits.
|
|
|
|
xcr0 = geax;
|
|
|
|
#endif
|
2014-06-12 15:16:03 +00:00
|
|
|
bool avx_support = ecx & (1 << BIT_AVX) || false;
|
|
|
|
bool xmm_support = xcr0 & (1 << XCR0_XMM);
|
|
|
|
bool ymm_support = xcr0 & (1 << XCR0_YMM);
|
|
|
|
|
|
|
|
if (avx_support && xmm_support && ymm_support) {
|
|
|
|
g_hardware_flags.intel_flags.avx = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_hardware_flags.intel_flags.avx) {
|
|
|
|
get_cpuid(7, &eax, &ebx, &ecx, &edx);
|
|
|
|
if (ebx & (1 << 5)) g_hardware_flags.intel_flags.avx2 = 1;
|
|
|
|
}
|
2014-10-14 09:01:56 +00:00
|
|
|
}
|
2014-06-18 08:50:17 +00:00
|
|
|
|
2014-10-14 09:01:56 +00:00
|
|
|
fprintf(stderr, "Compiled: INTEL, flags:");
|
2014-04-29 08:14:42 +00:00
|
|
|
#if COMPILE_INTEL_MMX
|
2014-10-14 09:01:56 +00:00
|
|
|
fprintf(stderr, " MMX");
|
2014-04-29 08:14:42 +00:00
|
|
|
#endif
|
|
|
|
#if COMPILE_INTEL_SSE
|
2014-10-14 09:01:56 +00:00
|
|
|
fprintf(stderr, " SSE");
|
2014-04-29 08:14:42 +00:00
|
|
|
#endif
|
|
|
|
#if COMPILE_INTEL_SSE2
|
2014-10-14 09:01:56 +00:00
|
|
|
fprintf(stderr, " SSE2");
|
2014-04-29 08:14:42 +00:00
|
|
|
#endif
|
|
|
|
#if COMPILE_INTEL_SSE3
|
2014-10-14 09:01:56 +00:00
|
|
|
fprintf(stderr, " SSE3");
|
2014-04-29 08:14:42 +00:00
|
|
|
#endif
|
|
|
|
#if COMPILE_INTEL_SSSE3
|
2014-10-14 09:01:56 +00:00
|
|
|
fprintf(stderr, " SSSE3");
|
2014-04-29 08:14:42 +00:00
|
|
|
#endif
|
|
|
|
#if COMPILE_INTEL_SSE41
|
2014-10-14 09:01:56 +00:00
|
|
|
fprintf(stderr, " SSE41");
|
2014-04-29 08:14:42 +00:00
|
|
|
#endif
|
|
|
|
#if COMPILE_INTEL_SSE42
|
2014-10-14 09:01:56 +00:00
|
|
|
fprintf(stderr, " SSE42");
|
2014-04-29 08:14:42 +00:00
|
|
|
#endif
|
|
|
|
#if COMPILE_INTEL_AVX
|
2014-10-14 09:01:56 +00:00
|
|
|
fprintf(stderr, " AVX");
|
2014-06-12 15:16:03 +00:00
|
|
|
#endif
|
|
|
|
#if COMPILE_INTEL_AVX2
|
2014-10-14 09:01:56 +00:00
|
|
|
fprintf(stderr, " AVX2");
|
2014-04-29 08:14:42 +00:00
|
|
|
#endif
|
2014-10-14 09:01:56 +00:00
|
|
|
fprintf(stderr, "\nDetected: INTEL, flags:");
|
|
|
|
if (g_hardware_flags.intel_flags.mmx) fprintf(stderr, " MMX");
|
|
|
|
if (g_hardware_flags.intel_flags.sse) fprintf(stderr, " SSE");
|
|
|
|
if (g_hardware_flags.intel_flags.sse2) fprintf(stderr, " SSE2");
|
|
|
|
if (g_hardware_flags.intel_flags.sse3) fprintf(stderr, " SSE3");
|
|
|
|
if (g_hardware_flags.intel_flags.ssse3) fprintf(stderr, " SSSE3");
|
|
|
|
if (g_hardware_flags.intel_flags.sse41) fprintf(stderr, " SSE41");
|
|
|
|
if (g_hardware_flags.intel_flags.sse42) fprintf(stderr, " SSE42");
|
|
|
|
if (g_hardware_flags.intel_flags.avx) fprintf(stderr, " AVX");
|
|
|
|
if (g_hardware_flags.intel_flags.avx2) fprintf(stderr, " AVX2");
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
2014-04-29 08:14:42 +00:00
|
|
|
#endif //COMPILE_INTEL
|
2014-06-03 05:39:30 +00:00
|
|
|
|
|
|
|
#if COMPILE_POWERPC
|
2014-10-14 09:01:56 +00:00
|
|
|
if (cpuid) {
|
|
|
|
g_hardware_flags.powerpc_flags.altivec = altivec_available();
|
|
|
|
}
|
2014-06-03 05:39:30 +00:00
|
|
|
|
|
|
|
fprintf(stderr, "Compiled: PowerPC, flags:");
|
|
|
|
#if COMPILE_POWERPC_ALTIVEC
|
|
|
|
fprintf(stderr, " AltiVec");
|
|
|
|
#endif
|
2014-10-14 09:01:56 +00:00
|
|
|
fprintf(stderr, "\nDetected: PowerPC, flags:");
|
2014-06-03 05:39:30 +00:00
|
|
|
if (g_hardware_flags.powerpc_flags.altivec) fprintf(stderr, " AltiVec");
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
#endif
|
|
|
|
|
2014-04-29 08:14:42 +00:00
|
|
|
}
|