TUT HEVC Encoder
Loading...
Searching...
No Matches
global.h
Go to the documentation of this file.
1#ifndef GLOBAL_H_
2#define GLOBAL_H_
3/*****************************************************************************
4 * This file is part of Kvazaar HEVC encoder.
5 *
6 * Copyright (c) 2021, Tampere University, ITU/ISO/IEC, project contributors
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without modification,
10 * are permitted provided that the following conditions are met:
11 *
12 * * Redistributions of source code must retain the above copyright notice, this
13 * list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above copyright notice, this
16 * list of conditions and the following disclaimer in the documentation and/or
17 * other materials provided with the distribution.
18 *
19 * * Neither the name of the Tampere University or ITU/ISO/IEC nor the names of its
20 * contributors may be used to endorse or promote products derived from
21 * this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
27 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND ON
30 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
33 ****************************************************************************/
34
44#ifdef HAVE_CONFIG_H
45// Include config.h generated by automake. This needs to be before any other
46// includes in every file, which is why it's in global.
47#include "config.h" // IWYU pragma: export
48#endif
49
50#ifdef CMAKE_BUILD
51#include "version.h"
52#endif
53
54// Include some basics in all files, like assert, primitives and NULL.
55// If you add anything to this list with export pragma, think long and
56// and hard if it's actually a good idea to incude it for every c-file.
57#include <assert.h> // IWYU pragma: export
58#include <stdbool.h> // IWYU pragma: export
59#include <stdint.h> // IWYU pragma: export
60#include <stddef.h> // IWYU pragma: export
61
62// The stdlib.h and string.h headers are needed because of MALLOC and FILL
63// macros defined here, as IWYU will remove them from files that use only
64// those macros.
65#include <stdlib.h>
66#include <string.h>
67
97 // Pthreads-win32 tries to define timespec even if it has already been defined.
98 // In Visual Studio 2015 timespec is defined in time.h so we may need to define
99 // HAVE_STRUCT_TIMESPEC.
100#if _MSC_VER >= 1900 && !defined(HAVE_STRUCT_TIMESPEC)
101# define HAVE_STRUCT_TIMESPEC
102#endif
103
104#if defined(_MSC_VER) && defined(_M_AMD64)
105 #define X86_64
106#endif
107
108#if defined(__GNUC__) && defined(__x86_64__)
109 #define X86_64
110#endif
111
112#define PIXEL_MIN 0
113#define PIXEL_MAX ((1 << KVZ_BIT_DEPTH) - 1)
114
116
117// #define VERBOSE 1
118
119/* CONFIG VARIABLES */
120
121//spec: references to variables defined in Rec. ITU-T H.265 (04/2013)
122
124#define PU_DEPTH_INTER_MIN 0
125#define PU_DEPTH_INTER_MAX 3
126#define PU_DEPTH_INTRA_MIN 0
127#define PU_DEPTH_INTRA_MAX 4
128
130#define MAX_GOP_LAYERS 6
131
134#define MAX_DEPTH 3
137#define MIN_SIZE 3
140#define MAX_PU_DEPTH 4
141
143#define ENABLE_PCM 0
144
146#define OPTIMIZATION_SKIP_RESIDUAL_ON_THRESHOLD 0
147
148/* END OF CONFIG VARIABLES */
149
151#define CU_MIN_SIZE_PIXELS (1 << MIN_SIZE)
153#define LCU_WIDTH (1 << (MIN_SIZE + MAX_DEPTH))
155#define LCU_WIDTH_C (LCU_WIDTH / 2)
156
158#define TR_MAX_LOG2_SIZE 5
159#define TR_MAX_WIDTH (1 << TR_MAX_LOG2_SIZE)
161#define TR_MIN_LOG2_SIZE 2
162#define TR_MIN_WIDTH (1 << TR_MIN_LOG2_SIZE)
163
164#if LCU_WIDTH != 64
165 #error "Kvazaar only support LCU_WIDTH == 64"
166#endif
167
168#define LCU_LUMA_SIZE (LCU_WIDTH * LCU_WIDTH)
169#define LCU_CHROMA_SIZE (LCU_WIDTH * LCU_WIDTH >> 2)
170
179#define DEBLOCK_DELAY_PX 8
180
191#define SAO_DELAY_PX (DEBLOCK_DELAY_PX + 2)
192
193#define MAX_REF_PIC_COUNT 16
194
195#define AMVP_MAX_NUM_CANDS 2
196#define AMVP_MAX_NUM_CANDS_MEM 3
197#define MRG_MAX_NUM_CANDS 5
198
199/* Some tools */
200#define ABS(a) ((a) >= 0 ? (a) : (-a))
201#define MAX(a,b) (((a)>(b))?(a):(b))
202#define MIN(a,b) (((a)<(b))?(a):(b))
203#define CLIP(low,high,value) MAX((low),MIN((high),(value)))
204#define CLIP_TO_PIXEL(value) CLIP(0, PIXEL_MAX, (value))
205#define CLIP_TO_QP(value) CLIP(0, 51, (value))
206#define SWAP(a,b,swaptype) { swaptype tempval; tempval = a; a = b; b = tempval; }
207#define CU_WIDTH_FROM_DEPTH(depth) (LCU_WIDTH >> depth)
208#define WITHIN(val, min_val, max_val) ((min_val) <= (val) && (val) <= (max_val))
209#define CEILDIV(x,y) (((x) + (y) - 1) / (y))
210
211#define LOG2_LCU_WIDTH 6
212// CU_TO_PIXEL = y * lcu_width * pic_width + x * lcu_width
213#define CU_TO_PIXEL(x, y, depth, stride) (((y) << (LOG2_LCU_WIDTH - (depth))) * (stride) \
214 + ((x) << (LOG2_LCU_WIDTH - (depth))))
215//#define SIGN3(x) ((x) > 0) ? +1 : ((x) == 0 ? 0 : -1)
216#define SIGN3(x) (((x) > 0) - ((x) < 0))
217
218
219#define QUOTE(x) #x
220#define QUOTE_EXPAND(x) QUOTE(x)
221
222#ifndef KVZ_VERSION
223#define KVZ_VERSION 2.3.1
224#endif
225#define VERSION_STRING QUOTE_EXPAND(KVZ_VERSION)
226
227
228//#define VERBOSE 1
229
230#define SAO_ABS_OFFSET_MAX ((1 << (MIN(KVZ_BIT_DEPTH, 10) - 5)) - 1)
231
232#define MAX_TILES_PER_DIM 48
233#define MAX_SLICES 16
234
235/* Inlining functions */
236#ifdef _MSC_VER /* Visual studio */
237 #define INLINE __forceinline
238 #pragma inline_recursion(on)
239#else /* others */
240 #define INLINE inline
241#endif
242
243// Return the next aligned address for *p. Result is at most alignment larger than p.
244#define ALIGNED_POINTER(p, alignment) (void*)((intptr_t)(p) + (alignment) - ((intptr_t)(p) % (alignment)))
245// 32 bytes is enough for AVX2
246#define SIMD_ALIGNMENT 32
247
248#ifdef _MSC_VER
249 #define ALIGNED(alignment) __declspec(align(alignment))
250#else
251 #define ALIGNED(alignment) __attribute__((aligned (alignment)))
252#endif
253
254#ifdef _MSC_VER
255#define NO_ASAN
256#else
257#define NO_ASAN __attribute__((no_sanitize("address")))
258#endif
259
260
261#ifdef _MSC_VER
262// Buggy VS2010 throws intellisense warnings if void* is not casted.
263 #define MALLOC(type, num) (type *)malloc(sizeof(type) * (num))
264 #define MALLOC_SIMD_PADDED(type, num, padding) (type *)malloc(sizeof(type) * (num) + (padding))
265#else
266 #define MALLOC(type, num) malloc(sizeof(type) * (num))
267 #define MALLOC_SIMD_PADDED(type, num, padding) malloc(sizeof(type) * (num) + (padding))
268#endif
269
270// Use memset through FILL and FILL_ARRAY when appropriate, such as when
271// initializing whole structures or arrays. It's still ok to use memset
272// directly when doing something more complicated.
273
274// Fill a structure or a static array with val bytes.
275#define FILL(var, val) memset(&(var), (val), sizeof(var))
276// Fill a number of elements in an array with val bytes.
277#define FILL_ARRAY(ar, val, size) \
278{\
279 void *temp_ptr = (void*)(ar);\
280 memset((temp_ptr), (val), (size) * sizeof(*(ar)));\
281}
282
283#define FREE_POINTER(pointer) { free((void*)pointer); pointer = NULL; }
284#define MOVE_POINTER(dst_pointer,src_pointer) { dst_pointer = src_pointer; src_pointer = NULL; }
285
286#ifndef MAX_INT
287#define MAX_INT 0x7FFFFFFF
288#endif
289#ifndef MAX_INT64
290#define MAX_INT64 0x7FFFFFFFFFFFFFFFLL
291#endif
292#ifndef MAX_DOUBLE
293#define MAX_DOUBLE 1.7e+308
294#endif
295
296//For transform.h and encoder.h
297#define SCALING_LIST_4x4 0
298#define SCALING_LIST_8x8 1
299#define SCALING_LIST_16x16 2
300#define SCALING_LIST_32x32 3
301#define SCALING_LIST_SIZE_NUM 4
302#define SCALING_LIST_NUM 6
303#define MAX_MATRIX_COEF_NUM 64
304#define SCALING_LIST_REM_NUM 6
305
306#define MAX_TR_DYNAMIC_RANGE 15
307
308//Constants
309typedef enum { COLOR_Y = 0, COLOR_U, COLOR_V } color_t;
310
311
312// Hardware data (abstraction of defines). Extend for other compilers
313#if defined(_M_IX86) || defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || defined(_M_X64) || defined(_M_AMD64) || defined(__amd64__) || defined(__x86_64__)
314# define COMPILE_INTEL 1
315#else
316# define COMPILE_INTEL 0
317#endif
318
319// Visual Studio note:
320// Because these macros are only used to guard code that is guarded by CPUID
321// at runtime, use /arch parameter to disable them, but enable all intrinsics
322// supported by VisualStudio if SSE2 (highest) is enabled.
323// AVX and AVX2 are handled by /arch directly and sse intrinsics will use VEX
324// versions if they are defined.
325#define MSC_X86_SIMD(level) (_M_X64 || (_M_IX86_FP >= (level)))
326
327#if COMPILE_INTEL
328# if defined(__MMX__) || MSC_X86_SIMD(1)
329# define COMPILE_INTEL_MMX 1
330# endif
331# if defined(__SSE__) || MSC_X86_SIMD(1)
332# define COMPILE_INTEL_SSE 1
333# endif
334# if defined(__SSE2__) || MSC_X86_SIMD(2)
335# define COMPILE_INTEL_SSE2 1
336# endif
337# if defined(__SSE3__)
338# define COMPILE_INTEL_SSE3 1
339# endif
340# if defined(__SSSE3__) || MSC_X86_SIMD(2)
341# define COMPILE_INTEL_SSSE3 1
342# endif
343# if defined(__SSE4_1__) || MSC_X86_SIMD(2)
344# define COMPILE_INTEL_SSE41 1
345# endif
346# if defined(__SSE4_2__) || MSC_X86_SIMD(2)
347# define COMPILE_INTEL_SSE42 1
348# endif
349# if defined(__AVX__)
350# define COMPILE_INTEL_AVX 1
351# endif
352# if defined(__AVX2__)
353# define COMPILE_INTEL_AVX2 1
354# endif
355#endif
356
357#if defined (_M_PPC) || defined(__powerpc64__) || defined(__powerpc__)
358# define COMPILE_POWERPC 1
359# ifdef __ALTIVEC__
360# define COMPILE_POWERPC_ALTIVEC 1
361# else
362# define COMPILE_POWERPC_ALTIVEC 0
363# endif
364#else
365# define COMPILE_POWERPC 0
366#endif
367
368#if defined (_M_ARM) || defined(__arm__) || defined(__thumb__)
369# define COMPILE_ARM 1
370#else
371# define COMPILE_ARM 0
372#endif
373
374// Min & max delta QP limits based on bit depth
375#define KVZ_QP_DELTA_MIN -(26 + 3 * (KVZ_BIT_DEPTH - 8))
376#define KVZ_QP_DELTA_MAX 25 + 3 * (KVZ_BIT_DEPTH - 8)
377
378#endif
color_t
Definition global.h:309
@ COLOR_V
Definition global.h:309
@ COLOR_Y
Definition global.h:309
@ COLOR_U
Definition global.h:309
#define MAX_TILES_PER_DIM
Definition global.h:232
int16_t coeff_t
Definition global.h:115