Imported Upstream version 1.4+222+hg5f9f7194267b
[deb_x265.git] / source / common / common.h
1 /*****************************************************************************
2 * Copyright (C) 2013 x265 project
3 *
4 * Authors: Deepthi Nandakumar <deepthi@multicorewareinc.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
19 *
20 * This program is also available under a commercial proprietary license.
21 * For more information, contact us at license @ x265.com.
22 *****************************************************************************/
23
24 #ifndef X265_COMMON_H
25 #define X265_COMMON_H
26
27 #include <algorithm>
28 #include <climits>
29 #include <cmath>
30 #include <cstdarg>
31 #include <cstddef>
32 #include <cstdio>
33 #include <cstdlib>
34 #include <cstring>
35 #include <cctype>
36 #include <ctime>
37
38 #include <stdint.h>
39 #include <memory.h>
40 #include <assert.h>
41
42 #include "x265.h"
43
44 #if ENABLE_PPA
45 #include "PPA/ppa.h"
46 #define ProfileScopeEvent(x) PPAScopeEvent(x)
47 #define PROFILE_INIT() PPA_INIT()
48 #else
49 #define ProfileScopeEvent(x)
50 #define PROFILE_INIT()
51 #endif
52
53 #define FENC_STRIDE 64
54 #define NUM_INTRA_MODE 35
55
56 #if defined(__GNUC__)
57 #define ALIGN_VAR_8(T, var) T var __attribute__((aligned(8)))
58 #define ALIGN_VAR_16(T, var) T var __attribute__((aligned(16)))
59 #define ALIGN_VAR_32(T, var) T var __attribute__((aligned(32)))
60
61 #if X265_ARCH_X86 && !defined(X86_64)
62 extern "C" intptr_t x265_stack_align(void (*func)(), ...);
63 #define x265_stack_align(func, ...) x265_stack_align((void (*)())func, __VA_ARGS__)
64 #else
65 #define x265_stack_align(func, ...) func(__VA_ARGS__)
66 #endif
67
68 #if defined(__MINGW32__)
69 #define fseeko fseeko64
70 #endif
71
72 #elif defined(_MSC_VER)
73
74 #define ALIGN_VAR_8(T, var) __declspec(align(8)) T var
75 #define ALIGN_VAR_16(T, var) __declspec(align(16)) T var
76 #define ALIGN_VAR_32(T, var) __declspec(align(32)) T var
77 #define x265_stack_align(func, ...) func(__VA_ARGS__)
78 #define fseeko _fseeki64
79
80 #endif // if defined(__GNUC__)
81
82 #if HAVE_INT_TYPES_H
83 #define __STDC_FORMAT_MACROS
84 #include <inttypes.h>
85 #define X265_LL "%" PRIu64
86 #else
87 #define X265_LL "%lld"
88 #endif
89
90 #if _DEBUG && defined(_MSC_VER)
91 #define DEBUG_BREAK() __debugbreak()
92 #elif __APPLE_CC__
93 #define DEBUG_BREAK() __builtin_trap();
94 #else
95 #define DEBUG_BREAK()
96 #endif
97
98 /* If compiled with CHECKED_BUILD perform run-time checks and log any that
99 * fail, both to stderr and to a file */
100 #if CHECKED_BUILD || _DEBUG
101 #define X265_CHECK(expr, ...) if (!(expr)) { \
102 x265_log(NULL, X265_LOG_ERROR, __VA_ARGS__); \
103 DEBUG_BREAK(); \
104 FILE *fp = fopen("x265_check_failures.txt", "a"); \
105 if (fp) { fprintf(fp, "%s:%d\n", __FILE__, __LINE__); fprintf(fp, __VA_ARGS__); fclose(fp); } \
106 }
107 #if _MSC_VER
108 #pragma warning(disable: 4127) // some checks have constant conditions
109 #endif
110 #else
111 #define X265_CHECK(expr, ...)
112 #endif
113
114 #if HIGH_BIT_DEPTH
115 typedef uint16_t pixel;
116 typedef uint32_t sum_t;
117 typedef uint64_t sum2_t;
118 typedef uint64_t pixel4;
119 typedef int64_t ssum2_t;
120 #define X265_DEPTH 10 // compile time configurable bit depth
121 #else
122 typedef uint8_t pixel;
123 typedef uint16_t sum_t;
124 typedef uint32_t sum2_t;
125 typedef uint32_t pixel4;
126 typedef int32_t ssum2_t; //Signed sum
127 #define X265_DEPTH 8 // compile time configurable bit depth
128 #endif // if HIGH_BIT_DEPTH
129
130 #ifndef NULL
131 #define NULL 0
132 #endif
133
134 #define MAX_UINT 0xFFFFFFFFU // max. value of unsigned 32-bit integer
135 #define MAX_INT 2147483647 // max. value of signed 32-bit integer
136 #define MAX_INT64 0x7FFFFFFFFFFFFFFFLL // max. value of signed 64-bit integer
137 #define MAX_DOUBLE 1.7e+308 // max. value of double-type value
138
139 #define QP_MIN 0
140 #define QP_MAX_SPEC 51 /* max allowed signaled QP in HEVC */
141 #define QP_MAX_MAX 69 /* max allowed QP to be output by rate control */
142
143 #define MIN_QPSCALE 0.21249999999999999
144 #define MAX_MAX_QPSCALE 615.46574234477100
145
146 #define BITS_FOR_POC 8
147
148 template<typename T>
149 inline pixel Clip(T x)
150 {
151 return (pixel)std::min<T>(T((1 << X265_DEPTH) - 1), std::max<T>(T(0), x));
152 }
153
154 template<typename T>
155 inline T Clip3(T minVal, T maxVal, T a)
156 {
157 return std::min<T>(std::max<T>(minVal, a), maxVal);
158 }
159
160 template<typename T>
161 inline T x265_min(T a, T b) { return a < b ? a : b; }
162
163 template<typename T>
164 inline T x265_max(T a, T b) { return a > b ? a : b; }
165
166 typedef int16_t coeff_t; // transform coefficient
167
168 #define X265_MIN(a, b) ((a) < (b) ? (a) : (b))
169 #define X265_MAX(a, b) ((a) > (b) ? (a) : (b))
170 #define COPY1_IF_LT(x, y) if ((y) < (x)) (x) = (y);
171 #define COPY2_IF_LT(x, y, a, b) \
172 if ((y) < (x)) \
173 { \
174 (x) = (y); \
175 (a) = (b); \
176 }
177 #define COPY3_IF_LT(x, y, a, b, c, d) \
178 if ((y) < (x)) \
179 { \
180 (x) = (y); \
181 (a) = (b); \
182 (c) = (d); \
183 }
184 #define COPY4_IF_LT(x, y, a, b, c, d, e, f) \
185 if ((y) < (x)) \
186 { \
187 (x) = (y); \
188 (a) = (b); \
189 (c) = (d); \
190 (e) = (f); \
191 }
192 #define X265_MIN3(a, b, c) X265_MIN((a), X265_MIN((b), (c)))
193 #define X265_MAX3(a, b, c) X265_MAX((a), X265_MAX((b), (c)))
194 #define X265_MIN4(a, b, c, d) X265_MIN((a), X265_MIN3((b), (c), (d)))
195 #define X265_MAX4(a, b, c, d) X265_MAX((a), X265_MAX3((b), (c), (d)))
196 #define QP_BD_OFFSET (6 * (X265_DEPTH - 8))
197 #define MAX_CHROMA_LAMBDA_OFFSET 36
198
199 // arbitrary, but low because SATD scores are 1/4 normal
200 #define X265_LOOKAHEAD_QP (12 + QP_BD_OFFSET)
201 #define X265_LOOKAHEAD_MAX 250
202
203 // Use the same size blocks as x264. Using larger blocks seems to give artificially
204 // high cost estimates (intra and inter both suffer)
205 #define X265_LOWRES_CU_SIZE 8
206 #define X265_LOWRES_CU_BITS 3
207
208 #define X265_MALLOC(type, count) (type*)x265_malloc(sizeof(type) * (count))
209 #define X265_FREE(ptr) x265_free(ptr)
210 #define CHECKED_MALLOC(var, type, count) \
211 { \
212 var = (type*)x265_malloc(sizeof(type) * (count)); \
213 if (!var) \
214 { \
215 x265_log(NULL, X265_LOG_ERROR, "malloc of size %d failed\n", sizeof(type) * (count)); \
216 goto fail; \
217 } \
218 }
219 #define CHECKED_MALLOC_ZERO(var, type, count) \
220 { \
221 var = (type*)x265_malloc(sizeof(type) * (count)); \
222 if (var) \
223 memset((void*)var, 0, sizeof(type) * (count)); \
224 else \
225 { \
226 x265_log(NULL, X265_LOG_ERROR, "malloc of size %d failed\n", sizeof(type) * (count)); \
227 goto fail; \
228 } \
229 }
230
231 #if defined(_MSC_VER)
232 #define X265_LOG2F(x) (logf((float)(x)) * 1.44269504088896405f)
233 #define X265_LOG2(x) (log((double)(x)) * 1.4426950408889640513713538072172)
234 #else
235 #define X265_LOG2F(x) log2f(x)
236 #define X265_LOG2(x) log2(x)
237 #endif
238
239 #define NUM_CU_DEPTH 4 // maximum number of CU depths
240 #define NUM_FULL_DEPTH 5 // maximum number of full depths
241 #define MIN_LOG2_CU_SIZE 3 // log2(minCUSize)
242 #define MAX_LOG2_CU_SIZE 6 // log2(maxCUSize)
243 #define MIN_CU_SIZE (1 << MIN_LOG2_CU_SIZE) // minimum allowable size of CU
244 #define MAX_CU_SIZE (1 << MAX_LOG2_CU_SIZE) // maximum allowable size of CU
245
246 #define LOG2_UNIT_SIZE 2 // log2(unitSize)
247 #define UNIT_SIZE (1 << LOG2_UNIT_SIZE) // unit size of CU partition
248
249 #define MAX_NUM_PARTITIONS 256
250 #define NUM_CU_PARTITIONS (1U << (g_maxFullDepth << 1))
251
252 #define MIN_PU_SIZE 4
253 #define MIN_TU_SIZE 4
254 #define MAX_NUM_SPU_W (MAX_CU_SIZE / MIN_PU_SIZE) // maximum number of SPU in horizontal line
255
256 #define MAX_LOG2_TR_SIZE 5
257 #define MAX_LOG2_TS_SIZE 2 // TODO: RExt
258 #define MAX_TR_SIZE (1 << MAX_LOG2_TR_SIZE)
259 #define MAX_TS_SIZE (1 << MAX_LOG2_TS_SIZE)
260
261 #define COEF_REMAIN_BIN_REDUCTION 3 // indicates the level at which the VLC
262 // transitions from Golomb-Rice to TU+EG(k)
263
264 #define SBH_THRESHOLD 4 // fixed sign bit hiding controlling threshold
265
266 #define C1FLAG_NUMBER 8 // maximum number of largerThan1 flag coded in one chunk: 16 in HM5
267 #define C2FLAG_NUMBER 1 // maximum number of largerThan2 flag coded in one chunk: 16 in HM5
268
269 #define SAO_ENCODING_RATE 0.75
270 #define SAO_ENCODING_RATE_CHROMA 0.5
271
272 #define MLS_GRP_NUM 64 // Max number of coefficient groups, max(16, 64)
273 #define MLS_CG_SIZE 4 // Coefficient group size of 4x4
274 #define MLS_CG_LOG2_SIZE 2
275
276 #define QUANT_IQUANT_SHIFT 20 // Q(QP%6) * IQ(QP%6) = 2^20
277 #define QUANT_SHIFT 14 // Q(4) = 2^14
278 #define SCALE_BITS 15 // Inherited from TMuC, presumably for fractional bit estimates in RDOQ
279 #define MAX_TR_DYNAMIC_RANGE 15 // Maximum transform dynamic range (excluding sign bit)
280
281 #define SHIFT_INV_1ST 7 // Shift after first inverse transform stage
282 #define SHIFT_INV_2ND 12 // Shift after second inverse transform stage
283
284 #define AMVP_DECIMATION_FACTOR 4
285
286 #define SCAN_SET_SIZE 16
287 #define LOG2_SCAN_SET_SIZE 4
288
289 #define ALL_IDX -1
290 #define PLANAR_IDX 0
291 #define VER_IDX 26 // index for intra VERTICAL mode
292 #define HOR_IDX 10 // index for intra HORIZONTAL mode
293 #define DC_IDX 1 // index for intra DC mode
294 #define NUM_CHROMA_MODE 5 // total number of chroma modes
295 #define DM_CHROMA_IDX 36 // chroma mode index for derived from luma intra mode
296
297 #define MDCS_ANGLE_LIMIT 4 // distance from true angle that horiz or vertical scan is allowed
298 #define MDCS_LOG2_MAX_SIZE 3 // TUs with log2 of size greater than this can only use diagonal scan
299
300 #define MAX_NUM_REF_PICS 16 // max. number of pictures used for reference
301 #define MAX_NUM_REF 16 // max. number of entries in picture reference list
302
303 #define REF_NOT_VALID -1
304
305 #define AMVP_NUM_CANDS 2 // number of AMVP candidates
306 #define MRG_MAX_NUM_CANDS 5 // max number of final merge candidates
307
308 #define CHROMA_H_SHIFT(x) (x == X265_CSP_I420 || x == X265_CSP_I422)
309 #define CHROMA_V_SHIFT(x) (x == X265_CSP_I420)
310 #define X265_MAX_PRED_MODE_PER_CTU 85 * 2 * 8
311
312 namespace x265 {
313
314 enum { SAO_NUM_OFFSET = 4 };
315
316 enum SaoMergeMode
317 {
318 SAO_MERGE_NONE,
319 SAO_MERGE_LEFT,
320 SAO_MERGE_UP
321 };
322
323 struct SaoCtuParam
324 {
325 SaoMergeMode mergeMode;
326 int typeIdx;
327 uint32_t bandPos; // BO band position
328 int offset[SAO_NUM_OFFSET];
329
330 void reset()
331 {
332 mergeMode = SAO_MERGE_NONE;
333 typeIdx = -1;
334 bandPos = 0;
335 offset[0] = 0;
336 offset[1] = 0;
337 offset[2] = 0;
338 offset[3] = 0;
339 }
340 };
341
342 struct SAOParam
343 {
344 SaoCtuParam* ctuParam[3];
345 bool bSaoFlag[2];
346 int numCuInWidth;
347
348 SAOParam()
349 {
350 for (int i = 0; i < 3; i++)
351 ctuParam[i] = NULL;
352 }
353
354 ~SAOParam()
355 {
356 delete[] ctuParam[0];
357 delete[] ctuParam[1];
358 delete[] ctuParam[2];
359 }
360 };
361
362 /* Stores inter (motion estimation) analysis data for a single frame */
363 struct analysis_inter_data
364 {
365 int ref;
366 };
367
368 /* Stores intra analysis data for a single frame. This struct needs better packing */
369 struct analysis_intra_data
370 {
371 uint8_t* depth;
372 uint8_t* modes;
373 char* partSizes;
374 };
375
376 enum TextType
377 {
378 TEXT_LUMA = 0, // luma
379 TEXT_CHROMA_U = 1, // chroma U
380 TEXT_CHROMA_V = 2, // chroma V
381 MAX_NUM_COMPONENT = 3
382 };
383
384 // coefficient scanning type used in ACS
385 enum ScanType
386 {
387 SCAN_DIAG = 0, // up-right diagonal scan
388 SCAN_HOR = 1, // horizontal first scan
389 SCAN_VER = 2, // vertical first scan
390 NUM_SCAN_TYPE = 3
391 };
392
393 enum SignificanceMapContextType
394 {
395 CONTEXT_TYPE_4x4 = 0,
396 CONTEXT_TYPE_8x8 = 1,
397 CONTEXT_TYPE_NxN = 2,
398 CONTEXT_NUMBER_OF_TYPES = 3
399 };
400 }
401
402 /* outside x265 namespace, but prefixed. defined in common.cpp */
403 int64_t x265_mdate(void);
404 void x265_log(const x265_param *param, int level, const char *fmt, ...);
405 int x265_exp2fix8(double x);
406
407 double x265_ssim2dB(double ssim);
408 double x265_qScale2qp(double qScale);
409 double x265_qp2qScale(double qp);
410 uint32_t x265_picturePlaneSize(int csp, int width, int height, int plane);
411
412 void* x265_malloc(size_t size);
413 void x265_free(void *ptr);
414 char* x265_slurp_file(const char *filename);
415
416 #include "constants.h"
417
418 #endif // ifndef X265_COMMON_H