2013-09-19 07:35:34 +00:00
|
|
|
#ifndef GLOBAL_H_
|
|
|
|
#define GLOBAL_H_
|
2014-01-24 10:37:15 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* This file is part of Kvazaar HEVC encoder.
|
2014-02-21 13:00:20 +00:00
|
|
|
*
|
2015-02-23 11:18:48 +00:00
|
|
|
* Copyright (C) 2013-2015 Tampere University of Technology and others (see
|
2014-01-24 10:37:15 +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-01-24 10:37:15 +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-01-24 10:37:15 +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-01-24 10:37:15 +00:00
|
|
|
****************************************************************************/
|
|
|
|
|
2015-12-17 11:42:57 +00:00
|
|
|
/**
|
2013-09-18 14:29:30 +00:00
|
|
|
* \file
|
2015-12-17 11:42:57 +00:00
|
|
|
* Header that is included in every other header.
|
2014-02-07 01:48:38 +00:00
|
|
|
*
|
2013-09-18 14:29:30 +00:00
|
|
|
* This file contains global constants that can be referred to from any header
|
|
|
|
* or source file. It also contains some helper macros and includes stdint.h
|
|
|
|
* so that any file can refer to integer types with exact widths.
|
2012-05-30 12:10:23 +00:00
|
|
|
*/
|
|
|
|
|
2016-01-22 13:15:02 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
// Include config.h generated by automake. This needs to be before any other
|
|
|
|
// includes in every file, which is why it's in global.
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2016-03-30 09:41:37 +00:00
|
|
|
// Include some basics in all files, like assert, primitives and NULL.
|
|
|
|
// IWYU pragma: begin_exports
|
2014-06-04 14:45:46 +00:00
|
|
|
#include <assert.h>
|
2014-06-12 15:16:03 +00:00
|
|
|
#include <stdbool.h>
|
2014-02-04 15:23:50 +00:00
|
|
|
#include <stdint.h>
|
2016-03-30 09:41:37 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
// IWYU pragma: end_exports
|
2014-02-04 15:23:50 +00:00
|
|
|
#include <stdio.h>
|
2015-07-21 11:28:50 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-02-04 15:23:50 +00:00
|
|
|
#include <limits.h>
|
2013-09-19 07:35:34 +00:00
|
|
|
|
2015-05-18 15:21:23 +00:00
|
|
|
#include "kvazaar.h"
|
|
|
|
|
2015-12-17 11:42:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \defgroup Bitstream
|
|
|
|
* HEVC bitstream coding
|
|
|
|
*
|
|
|
|
* \defgroup CABAC
|
|
|
|
* Context Adaptive Binary Arithmetic Encoder implementation
|
|
|
|
*
|
|
|
|
* \defgroup Compression
|
|
|
|
* Prediction parameter decisions and ratedistortion optimization
|
|
|
|
*
|
|
|
|
* \defgroup Control
|
|
|
|
* Initialization and control flow of the encoder
|
|
|
|
*
|
|
|
|
* \defgroup DataStructures
|
|
|
|
* Containers for images, predictions parameters and such
|
|
|
|
*
|
|
|
|
* \defgroup Extras
|
|
|
|
* 3rd party modules not considered part of the encoder.
|
|
|
|
*
|
|
|
|
* \defgroup Optimization
|
|
|
|
* Architecture dependant SIMD optimizations and dynamic dispatch mechanism
|
|
|
|
*
|
|
|
|
* \defgroup Reconstruction
|
|
|
|
* Stuff required for creating the resulting image after lossy compression
|
|
|
|
*
|
|
|
|
* \defgroup Threading
|
|
|
|
* Stuff related to multi-threading using pthreads
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2014-02-21 12:55:07 +00:00
|
|
|
#if defined(_MSC_VER) && defined(_M_AMD64)
|
2013-09-23 13:48:34 +00:00
|
|
|
#define X86_64
|
|
|
|
#endif
|
|
|
|
|
2014-02-21 12:55:07 +00:00
|
|
|
#if defined(__GNUC__) && defined(__x86_64__)
|
2013-09-23 13:48:34 +00:00
|
|
|
#define X86_64
|
|
|
|
#endif
|
|
|
|
|
2013-10-14 14:27:25 +00:00
|
|
|
#define PIXEL_MIN 0
|
2015-06-30 07:38:17 +00:00
|
|
|
#define PIXEL_MAX ((1 << KVZ_BIT_DEPTH) - 1)
|
2013-10-14 14:27:25 +00:00
|
|
|
|
2015-03-04 14:33:47 +00:00
|
|
|
typedef int16_t coeff_t;
|
2012-05-30 12:10:23 +00:00
|
|
|
|
2014-01-17 15:06:24 +00:00
|
|
|
//#define VERBOSE 1
|
|
|
|
|
2012-06-11 12:39:18 +00:00
|
|
|
/* CONFIG VARIABLES */
|
2013-04-17 14:08:52 +00:00
|
|
|
|
2014-04-01 11:29:49 +00:00
|
|
|
//spec: references to variables defined in Rec. ITU-T H.265 (04/2013)
|
|
|
|
|
2015-07-21 14:10:08 +00:00
|
|
|
//! Limits for prediction block sizes. 0 = 64x64, 4 = 4x4.
|
2015-01-09 10:04:23 +00:00
|
|
|
#define PU_DEPTH_INTER_MIN 0
|
|
|
|
#define PU_DEPTH_INTER_MAX 3
|
|
|
|
#define PU_DEPTH_INTRA_MIN 0
|
|
|
|
#define PU_DEPTH_INTRA_MAX 4
|
2013-04-17 14:08:52 +00:00
|
|
|
|
2015-07-21 14:10:08 +00:00
|
|
|
//! Maximum number of layers in GOP structure (for allocating structures)
|
2015-05-27 12:41:45 +00:00
|
|
|
#define MAX_GOP_LAYERS 6
|
|
|
|
|
2015-07-21 14:10:08 +00:00
|
|
|
//! Maximum CU depth when descending form LCU level.
|
|
|
|
//! spec: log2_diff_max_min_luma_coding_block_size
|
|
|
|
#define MAX_DEPTH 3
|
|
|
|
//! Minimum log2 size of CUs.
|
|
|
|
//! spec: MinCbLog2SizeY
|
|
|
|
#define MIN_SIZE 3
|
|
|
|
//! Minimum log2 size of PUs.
|
|
|
|
//! Search is started at depth 0 and goes in Z-order to MAX_PU_DEPTH, see search_cu()
|
|
|
|
#define MAX_PU_DEPTH 4
|
2012-06-11 12:39:18 +00:00
|
|
|
|
2015-07-21 14:10:08 +00:00
|
|
|
//! Minimum log2 transform sizes.
|
|
|
|
//! spec: max_transform_hierarchy_depth_inter
|
|
|
|
#define TR_DEPTH_INTER 2
|
2014-01-14 15:14:56 +00:00
|
|
|
|
2015-07-21 14:10:08 +00:00
|
|
|
//! spec: pcm_enabled_flag, Setting to 1 will enable using PCM blocks (current intra-search does not consider PCM)
|
|
|
|
#define ENABLE_PCM 0
|
2012-06-11 12:39:18 +00:00
|
|
|
|
2015-07-21 14:10:08 +00:00
|
|
|
//! Enable usage of temporal Motion Vector Prediction
|
|
|
|
#define ENABLE_TEMPORAL_MVP 0
|
2013-09-18 07:15:05 +00:00
|
|
|
|
2015-07-21 14:10:08 +00:00
|
|
|
//! skip residual coding when it's under _some_ threshold
|
|
|
|
#define OPTIMIZATION_SKIP_RESIDUAL_ON_THRESHOLD 0
|
2013-10-10 13:31:00 +00:00
|
|
|
|
2012-06-11 12:39:18 +00:00
|
|
|
/* END OF CONFIG VARIABLES */
|
|
|
|
|
2015-07-21 14:10:08 +00:00
|
|
|
//! pow(2, MIN_SIZE)
|
|
|
|
#define CU_MIN_SIZE_PIXELS (1 << MIN_SIZE)
|
|
|
|
//! spec: CtbSizeY
|
|
|
|
#define LCU_WIDTH (1 << (MIN_SIZE + MAX_DEPTH))
|
|
|
|
//! spec: CtbWidthC and CtbHeightC
|
|
|
|
#define LCU_WIDTH_C (LCU_WIDTH / 2)
|
2014-05-13 07:04:05 +00:00
|
|
|
|
2015-07-21 14:10:08 +00:00
|
|
|
//! spec: Log2MaxTrafoSize <= Min(CtbLog2SizeY, 5)
|
|
|
|
#define TR_MAX_LOG2_SIZE 5
|
2014-09-05 08:29:30 +00:00
|
|
|
#define TR_MAX_WIDTH (1 << TR_MAX_LOG2_SIZE)
|
2015-07-21 14:10:08 +00:00
|
|
|
//! spec: Log2MinTrafoSize
|
|
|
|
#define TR_MIN_LOG2_SIZE 2
|
2014-09-05 08:29:30 +00:00
|
|
|
#define TR_MIN_WIDTH (1 << TR_MIN_LOG2_SIZE)
|
2014-04-01 11:29:49 +00:00
|
|
|
|
|
|
|
#if LCU_WIDTH != 64
|
|
|
|
#error "Kvazaar only support LCU_WIDTH == 64"
|
|
|
|
#endif
|
|
|
|
|
2013-10-25 14:14:20 +00:00
|
|
|
#define LCU_LUMA_SIZE (LCU_WIDTH * LCU_WIDTH)
|
|
|
|
#define LCU_CHROMA_SIZE (LCU_WIDTH * LCU_WIDTH >> 2)
|
|
|
|
|
2014-02-19 13:09:17 +00:00
|
|
|
#define MAX_REF_PIC_COUNT 16
|
|
|
|
#define DEFAULT_REF_PIC_COUNT 3
|
2013-09-12 13:28:40 +00:00
|
|
|
|
2013-08-02 13:35:30 +00:00
|
|
|
#define AMVP_MAX_NUM_CANDS 2
|
|
|
|
#define AMVP_MAX_NUM_CANDS_MEM 3
|
|
|
|
#define MRG_MAX_NUM_CANDS 5
|
2012-06-11 12:39:18 +00:00
|
|
|
|
2013-03-08 09:42:22 +00:00
|
|
|
/* Some tools */
|
2012-06-07 14:38:28 +00:00
|
|
|
#define MAX(a,b) (((a)>(b))?(a):(b))
|
|
|
|
#define MIN(a,b) (((a)<(b))?(a):(b))
|
2013-03-08 09:42:22 +00:00
|
|
|
#define CLIP(low,high,value) MAX((low),MIN((high),(value)))
|
2015-10-03 00:36:58 +00:00
|
|
|
#define CLIP_TO_PIXEL(value) CLIP(0, PIXEL_MAX, (value))
|
2013-03-08 09:42:22 +00:00
|
|
|
#define SWAP(a,b,swaptype) { swaptype tempval; tempval = a; a = b; b = tempval; }
|
2013-09-16 15:58:28 +00:00
|
|
|
#define CU_WIDTH_FROM_DEPTH(depth) (LCU_WIDTH >> depth)
|
2013-10-14 14:27:25 +00:00
|
|
|
#define WITHIN(val, min_val, max_val) ((min_val) <= (val) && (val) <= (max_val))
|
2014-03-11 17:19:20 +00:00
|
|
|
#define PU_INDEX(x_pu, y_pu) (((x_pu) % 2) + 2 * ((y_pu) % 2))
|
2014-12-05 12:42:09 +00:00
|
|
|
#define CEILDIV(x,y) (((x) + (y) - 1) / (y))
|
2012-06-04 10:47:12 +00:00
|
|
|
|
2013-10-25 14:14:20 +00:00
|
|
|
#define LOG2_LCU_WIDTH 6
|
|
|
|
// CU_TO_PIXEL = y * lcu_width * pic_width + x * lcu_width
|
2014-06-12 05:21:13 +00:00
|
|
|
#define CU_TO_PIXEL(x, y, depth, stride) (((y) << (LOG2_LCU_WIDTH - (depth))) * (stride) \
|
2013-10-25 14:14:20 +00:00
|
|
|
+ ((x) << (LOG2_LCU_WIDTH - (depth))))
|
2013-11-04 17:27:47 +00:00
|
|
|
//#define SIGN3(x) ((x) > 0) ? +1 : ((x) == 0 ? 0 : -1)
|
|
|
|
#define SIGN3(x) (((x) > 0) - ((x) < 0))
|
2013-10-25 14:14:20 +00:00
|
|
|
|
2015-08-17 13:11:46 +00:00
|
|
|
|
|
|
|
#define QUOTE(x) #x
|
|
|
|
#define QUOTE_EXPAND(x) QUOTE(x)
|
|
|
|
|
2016-01-15 17:42:07 +00:00
|
|
|
// NOTE: When making a release, check to see if incrementing libversion in
|
|
|
|
// configure.ac is necessary.
|
2016-03-09 12:44:48 +00:00
|
|
|
#ifndef KVZ_VERSION
|
2016-03-09 16:13:59 +00:00
|
|
|
#define KVZ_VERSION 0.8.3
|
2016-03-09 12:44:48 +00:00
|
|
|
#endif
|
2015-08-17 13:11:46 +00:00
|
|
|
#define VERSION_STRING QUOTE_EXPAND(KVZ_VERSION)
|
2012-05-30 12:10:23 +00:00
|
|
|
|
|
|
|
//#define VERBOSE 1
|
|
|
|
|
2015-06-30 07:38:17 +00:00
|
|
|
#define SAO_ABS_OFFSET_MAX ((1 << (MIN(KVZ_BIT_DEPTH, 10) - 5)) - 1)
|
2013-11-04 17:27:47 +00:00
|
|
|
|
2014-05-28 12:47:24 +00:00
|
|
|
#define MAX_TILES_PER_DIM 48
|
2014-05-05 13:17:22 +00:00
|
|
|
#define MAX_SLICES 16
|
2013-10-23 16:51:39 +00:00
|
|
|
|
2013-02-05 13:48:06 +00:00
|
|
|
/* Inlining functions */
|
|
|
|
#ifdef _MSC_VER /* Visual studio */
|
|
|
|
#define INLINE __forceinline
|
|
|
|
#pragma inline_recursion(on)
|
|
|
|
#else /* others */
|
|
|
|
#define INLINE inline
|
|
|
|
#endif
|
|
|
|
|
2014-07-10 14:41:13 +00:00
|
|
|
// Return the next aligned address for *p. Result is at most alignment larger than p.
|
|
|
|
#define ALIGNED_POINTER(p, alignment) (void*)((intptr_t)(p) + (alignment) - ((intptr_t)(p) % (alignment)))
|
|
|
|
// 32 bytes is enough for AVX2
|
|
|
|
#define SIMD_ALIGNMENT 32
|
|
|
|
|
2013-10-15 12:27:32 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
// Buggy VS2010 throws intellisense warnings if void* is not casted.
|
2014-04-22 13:25:59 +00:00
|
|
|
#define MALLOC(type, num) (type *)malloc(sizeof(type) * (num))
|
2013-10-15 12:27:32 +00:00
|
|
|
#else
|
2014-04-22 13:25:59 +00:00
|
|
|
#define MALLOC(type, num) malloc(sizeof(type) * (num))
|
2013-10-15 12:27:32 +00:00
|
|
|
#endif
|
|
|
|
|
2015-02-13 09:56:55 +00:00
|
|
|
// Use memset through FILL and FILL_ARRAY when appropriate, such as when
|
|
|
|
// initializing whole structures or arrays. It's still ok to use memset
|
|
|
|
// directly when doing something more complicated.
|
|
|
|
|
|
|
|
// Fill a structure or a static array with val bytes.
|
|
|
|
#define FILL(var, val) memset(&(var), (val), sizeof(var))
|
|
|
|
// Fill a number of elements in an array with val bytes.
|
|
|
|
#define FILL_ARRAY(ar, val, size) memset((ar), (val), (size) * sizeof(*(ar)))
|
|
|
|
|
2014-04-15 09:17:24 +00:00
|
|
|
#define FREE_POINTER(pointer) { free((void*)pointer); pointer = NULL; }
|
2013-10-18 08:39:13 +00:00
|
|
|
#define MOVE_POINTER(dst_pointer,src_pointer) { dst_pointer = src_pointer; src_pointer = NULL; }
|
2013-02-21 14:45:22 +00:00
|
|
|
|
2014-01-20 14:34:11 +00:00
|
|
|
#ifndef MAX_INT
|
|
|
|
#define MAX_INT 0x7FFFFFFF
|
|
|
|
#endif
|
|
|
|
#ifndef MAX_INT64
|
|
|
|
#define MAX_INT64 0x7FFFFFFFFFFFFFFFLL
|
|
|
|
#endif
|
|
|
|
#ifndef MAX_DOUBLE
|
|
|
|
#define MAX_DOUBLE 1.7e+308
|
|
|
|
#endif
|
|
|
|
|
2014-04-14 11:36:45 +00:00
|
|
|
//For transform.h and encoder.h
|
|
|
|
#define SCALING_LIST_4x4 0
|
|
|
|
#define SCALING_LIST_8x8 1
|
|
|
|
#define SCALING_LIST_16x16 2
|
|
|
|
#define SCALING_LIST_32x32 3
|
|
|
|
#define SCALING_LIST_SIZE_NUM 4
|
|
|
|
#define SCALING_LIST_NUM 6
|
|
|
|
#define MAX_MATRIX_COEF_NUM 64
|
|
|
|
#define SCALING_LIST_REM_NUM 6
|
|
|
|
|
2014-04-16 07:40:42 +00:00
|
|
|
#define MAX_TR_DYNAMIC_RANGE 15
|
|
|
|
|
2015-06-03 08:18:45 +00:00
|
|
|
#define EXP_GOLOMB_TABLE_SIZE (4096*8)
|
2014-08-11 09:35:36 +00:00
|
|
|
|
2014-06-05 12:54:58 +00:00
|
|
|
//Constants
|
2015-03-04 14:37:35 +00:00
|
|
|
typedef enum { COLOR_Y = 0, COLOR_U, COLOR_V, NUM_COLORS } color_t;
|
2014-06-05 12:54:58 +00:00
|
|
|
|
2016-03-31 11:31:47 +00:00
|
|
|
|
|
|
|
// Hardware data (abstraction of defines). Extend for other compilers
|
|
|
|
#if defined(_M_IX86) || defined(__i586__) || defined(__i686__) || defined(_M_X64) || defined(_M_AMD64) || defined(__amd64__) || defined(__x86_64__)
|
|
|
|
# define COMPILE_INTEL 1
|
|
|
|
#else
|
|
|
|
# define COMPILE_INTEL 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Visual Studio note:
|
|
|
|
// Because these macros are only used to guard code that is guarded by CPUID
|
|
|
|
// at runtime, use /arch parameter to disable them, but enable all intrinsics
|
|
|
|
// supported by VisualStudio if SSE2 (highest) is enabled.
|
|
|
|
// AVX and AVX2 are handled by /arch directly and sse intrinsics will use VEX
|
|
|
|
// versions if they are defined.
|
|
|
|
#define MSC_X86_SIMD(level) (_M_X64 || (_M_IX86_FP >= (level)))
|
|
|
|
|
|
|
|
#if COMPILE_INTEL
|
|
|
|
# if defined(__MMX__) || MSC_X86_SIMD(1)
|
|
|
|
# define COMPILE_INTEL_MMX 1
|
|
|
|
# endif
|
|
|
|
# if defined(__SSE__) || MSC_X86_SIMD(1)
|
|
|
|
# define COMPILE_INTEL_SSE 1
|
|
|
|
# endif
|
|
|
|
# if defined(__SSE2__) || MSC_X86_SIMD(2)
|
|
|
|
# define COMPILE_INTEL_SSE2 1
|
|
|
|
# endif
|
|
|
|
# if defined(__SSE3__)
|
|
|
|
# define COMPILE_INTEL_SSE3 1
|
|
|
|
# endif
|
|
|
|
# if defined(__SSSE3__) || MSC_X86_SIMD(2)
|
|
|
|
# define COMPILE_INTEL_SSSE3 1
|
|
|
|
# endif
|
|
|
|
# if defined(__SSE4_1__) || MSC_X86_SIMD(2)
|
|
|
|
# define COMPILE_INTEL_SSE41 1
|
|
|
|
# endif
|
|
|
|
# if defined(__SSE4_2__) || MSC_X86_SIMD(2)
|
|
|
|
# define COMPILE_INTEL_SSE42 1
|
|
|
|
# endif
|
|
|
|
# if defined(__AVX__)
|
|
|
|
# define COMPILE_INTEL_AVX 1
|
|
|
|
# endif
|
|
|
|
# if defined(__AVX2__)
|
|
|
|
# define COMPILE_INTEL_AVX2 1
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined (_M_PPC) || defined(__powerpc64__) || defined(__powerpc__)
|
|
|
|
# define COMPILE_POWERPC 1
|
|
|
|
# ifdef __ALTIVEC__
|
|
|
|
# define COMPILE_POWERPC_ALTIVEC 1
|
|
|
|
# else
|
|
|
|
# define COMPILE_POWERPC_ALTIVEC 0
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# define COMPILE_POWERPC 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined (_M_ARM) || defined(__arm__) || defined(__thumb__)
|
|
|
|
# define COMPILE_ARM 1
|
|
|
|
#else
|
|
|
|
# define COMPILE_ARM 0
|
|
|
|
#endif
|
|
|
|
|
2014-02-03 14:32:54 +00:00
|
|
|
#endif
|