1 /*****************************************************************************
2 * Copyright (C) 2013 x265 project
4 * Authors: Deepthi Nandakumar <deepthi@multicorewareinc.com>
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.
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.
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.
20 * This program is also available under a commercial proprietary license.
21 * For more information, contact us at license @ x265.com.
22 *****************************************************************************/
46 #define ProfileScopeEvent(x) PPAScopeEvent(x)
47 #define PROFILE_INIT() PPA_INIT()
49 #define ProfileScopeEvent(x)
50 #define PROFILE_INIT()
53 #define FENC_STRIDE 64
54 #define NUM_INTRA_MODE 35
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)))
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__)
65 #define x265_stack_align(func, ...) func(__VA_ARGS__)
68 #if defined(__MINGW32__)
69 #define fseeko fseeko64
72 #elif defined(_MSC_VER)
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
80 #endif // if defined(__GNUC__)
83 #define __STDC_FORMAT_MACROS
85 #define X265_LL "%" PRIu64
87 #define X265_LL "%lld"
90 #if _DEBUG && defined(_MSC_VER)
91 #define DEBUG_BREAK() __debugbreak()
93 #define DEBUG_BREAK() __builtin_trap();
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__); \
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); } \
108 #pragma warning(disable: 4127) // some checks have constant conditions
111 #define X265_CHECK(expr, ...)
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
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
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
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 */
143 #define MIN_QPSCALE 0.21249999999999999
144 #define MAX_MAX_QPSCALE 615.46574234477100
146 #define BITS_FOR_POC 8
149 inline pixel
Clip(T x
)
151 return (pixel
)std::min
<T
>(T((1 << X265_DEPTH
) - 1), std::max
<T
>(T(0), x
));
155 inline T
Clip3(T minVal
, T maxVal
, T a
)
157 return std::min
<T
>(std::max
<T
>(minVal
, a
), maxVal
);
161 inline T
x265_min(T a
, T b
) { return a
< b
? a
: b
; }
164 inline T
x265_max(T a
, T b
) { return a
> b
? a
: b
; }
166 typedef int16_t coeff_t
; // transform coefficient
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) \
177 #define COPY3_IF_LT(x, y, a, b, c, d) \
184 #define COPY4_IF_LT(x, y, a, b, c, d, e, f) \
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
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
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
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) \
212 var = (type*)x265_malloc(sizeof(type) * (count)); \
215 x265_log(NULL, X265_LOG_ERROR, "malloc of size %d failed\n", sizeof(type) * (count)); \
219 #define CHECKED_MALLOC_ZERO(var, type, count) \
221 var = (type*)x265_malloc(sizeof(type) * (count)); \
223 memset((void*)var, 0, sizeof(type) * (count)); \
226 x265_log(NULL, X265_LOG_ERROR, "malloc of size %d failed\n", sizeof(type) * (count)); \
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)
235 #define X265_LOG2F(x) log2f(x)
236 #define X265_LOG2(x) log2(x)
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
246 #define LOG2_UNIT_SIZE 2 // log2(unitSize)
247 #define UNIT_SIZE (1 << LOG2_UNIT_SIZE) // unit size of CU partition
249 #define MAX_NUM_PARTITIONS 256
250 #define NUM_CU_PARTITIONS (1U << (g_maxFullDepth << 1))
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
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)
261 #define COEF_REMAIN_BIN_REDUCTION 3 // indicates the level at which the VLC
262 // transitions from Golomb-Rice to TU+EG(k)
264 #define SBH_THRESHOLD 4 // fixed sign bit hiding controlling threshold
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
269 #define SAO_ENCODING_RATE 0.75
270 #define SAO_ENCODING_RATE_CHROMA 0.5
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
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)
281 #define SHIFT_INV_1ST 7 // Shift after first inverse transform stage
282 #define SHIFT_INV_2ND 12 // Shift after second inverse transform stage
284 #define AMVP_DECIMATION_FACTOR 4
286 #define SCAN_SET_SIZE 16
287 #define LOG2_SCAN_SET_SIZE 4
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
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
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
303 #define REF_NOT_VALID -1
305 #define AMVP_NUM_CANDS 2 // number of AMVP candidates
306 #define MRG_MAX_NUM_CANDS 5 // max number of final merge candidates
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
314 enum { SAO_NUM_OFFSET
= 4 };
325 SaoMergeMode mergeMode
;
327 uint32_t bandPos
; // BO band position
328 int offset
[SAO_NUM_OFFSET
];
332 mergeMode
= SAO_MERGE_NONE
;
344 SaoCtuParam
* ctuParam
[3];
350 for (int i
= 0; i
< 3; i
++)
356 delete[] ctuParam
[0];
357 delete[] ctuParam
[1];
358 delete[] ctuParam
[2];
362 /* Stores inter (motion estimation) analysis data for a single frame */
363 struct analysis_inter_data
368 /* Stores intra analysis data for a single frame. This struct needs better packing */
369 struct analysis_intra_data
378 TEXT_LUMA
= 0, // luma
379 TEXT_CHROMA_U
= 1, // chroma U
380 TEXT_CHROMA_V
= 2, // chroma V
381 MAX_NUM_COMPONENT
= 3
384 // coefficient scanning type used in ACS
387 SCAN_DIAG
= 0, // up-right diagonal scan
388 SCAN_HOR
= 1, // horizontal first scan
389 SCAN_VER
= 2, // vertical first scan
393 enum SignificanceMapContextType
395 CONTEXT_TYPE_4x4
= 0,
396 CONTEXT_TYPE_8x8
= 1,
397 CONTEXT_TYPE_NxN
= 2,
398 CONTEXT_NUMBER_OF_TYPES
= 3
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
);
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
);
412 void* x265_malloc(size_t size
);
413 void x265_free(void *ptr
);
414 char* x265_slurp_file(const char *filename
);
416 #include "constants.h"
418 #endif // ifndef X265_COMMON_H