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 *****************************************************************************/
44 #define FENC_STRIDE 64
45 #define NUM_INTRA_MODE 35
48 #define ALIGN_VAR_8(T, var) T var __attribute__((aligned(8)))
49 #define ALIGN_VAR_16(T, var) T var __attribute__((aligned(16)))
50 #define ALIGN_VAR_32(T, var) T var __attribute__((aligned(32)))
52 #if X265_ARCH_X86 && !defined(X86_64)
53 extern "C" intptr_t x265_stack_align(void (*func
)(), ...);
54 #define x265_stack_align(func, ...) x265_stack_align((void (*)())func, __VA_ARGS__)
56 #define x265_stack_align(func, ...) func(__VA_ARGS__)
59 #elif defined(_MSC_VER)
61 #define ALIGN_VAR_8(T, var) __declspec(align(8)) T var
62 #define ALIGN_VAR_16(T, var) __declspec(align(16)) T var
63 #define ALIGN_VAR_32(T, var) __declspec(align(32)) T var
64 #define x265_stack_align(func, ...) func(__VA_ARGS__)
65 #define fseeko _fseeki64
67 #endif // if defined(__GNUC__)
70 #define __STDC_FORMAT_MACROS
72 #define X265_LL "%" PRIu64
74 #define X265_LL "%lld"
77 #if _DEBUG && defined(_MSC_VER)
78 #define DEBUG_BREAK() __debugbreak()
80 #define DEBUG_BREAK() __builtin_trap();
85 /* If compiled with CHECKED_BUILD perform run-time checks and log any that
86 * fail, both to stderr and to a file */
87 #if CHECKED_BUILD || _DEBUG
88 #define X265_CHECK(expr, ...) if (!(expr)) { \
89 x265_log(NULL, X265_LOG_ERROR, __VA_ARGS__); \
91 FILE *fp = fopen("x265_check_failures.txt", "a"); \
92 if (fp) { fprintf(fp, "%s:%d\n", __FILE__, __LINE__); fprintf(fp, __VA_ARGS__); fclose(fp); } \
95 #pragma warning(disable: 4127) // some checks have constant conditions
98 #define X265_CHECK(expr, ...)
102 typedef uint16_t pixel
;
103 typedef uint32_t sum_t
;
104 typedef uint64_t sum2_t
;
105 typedef uint64_t pixel4
;
106 typedef int64_t ssum2_t
;
107 #define X265_DEPTH 10 // compile time configurable bit depth
109 typedef uint8_t pixel
;
110 typedef uint16_t sum_t
;
111 typedef uint32_t sum2_t
;
112 typedef uint32_t pixel4
;
113 typedef int32_t ssum2_t
; //Signed sum
114 #define X265_DEPTH 8 // compile time configurable bit depth
115 #endif // if HIGH_BIT_DEPTH
121 #define MAX_UINT 0xFFFFFFFFU // max. value of unsigned 32-bit integer
122 #define MAX_INT 2147483647 // max. value of signed 32-bit integer
123 #define MAX_INT64 0x7FFFFFFFFFFFFFFFLL // max. value of signed 64-bit integer
124 #define MAX_DOUBLE 1.7e+308 // max. value of double-type value
127 #define QP_MAX_SPEC 51 /* max allowed signaled QP in HEVC */
128 #define QP_MAX_MAX 69 /* max allowed QP to be output by rate control */
130 #define MIN_QPSCALE 0.21249999999999999
131 #define MAX_MAX_QPSCALE 615.46574234477100
133 #define BITS_FOR_POC 8
136 inline pixel
Clip(T x
)
138 return (pixel
)std::min
<T
>(T((1 << X265_DEPTH
) - 1), std::max
<T
>(T(0), x
));
142 inline T
Clip3(T minVal
, T maxVal
, T a
)
144 return std::min
<T
>(std::max
<T
>(minVal
, a
), maxVal
);
148 inline T
x265_min(T a
, T b
) { return a
< b
? a
: b
; }
151 inline T
x265_max(T a
, T b
) { return a
> b
? a
: b
; }
153 typedef int16_t coeff_t
; // transform coefficient
155 #define X265_MIN(a, b) ((a) < (b) ? (a) : (b))
156 #define X265_MAX(a, b) ((a) > (b) ? (a) : (b))
157 #define COPY1_IF_LT(x, y) if ((y) < (x)) (x) = (y);
158 #define COPY2_IF_LT(x, y, a, b) \
164 #define COPY3_IF_LT(x, y, a, b, c, d) \
171 #define COPY4_IF_LT(x, y, a, b, c, d, e, f) \
179 #define X265_MIN3(a, b, c) X265_MIN((a), X265_MIN((b), (c)))
180 #define X265_MAX3(a, b, c) X265_MAX((a), X265_MAX((b), (c)))
181 #define X265_MIN4(a, b, c, d) X265_MIN((a), X265_MIN3((b), (c), (d)))
182 #define X265_MAX4(a, b, c, d) X265_MAX((a), X265_MAX3((b), (c), (d)))
183 #define QP_BD_OFFSET (6 * (X265_DEPTH - 8))
184 #define MAX_CHROMA_LAMBDA_OFFSET 36
186 // arbitrary, but low because SATD scores are 1/4 normal
187 #define X265_LOOKAHEAD_QP (12 + QP_BD_OFFSET)
188 #define X265_LOOKAHEAD_MAX 250
190 // Use the same size blocks as x264. Using larger blocks seems to give artificially
191 // high cost estimates (intra and inter both suffer)
192 #define X265_LOWRES_CU_SIZE 8
193 #define X265_LOWRES_CU_BITS 3
195 #define X265_MALLOC(type, count) (type*)x265_malloc(sizeof(type) * (count))
196 #define X265_FREE(ptr) x265_free(ptr)
197 #define CHECKED_MALLOC(var, type, count) \
199 var = (type*)x265_malloc(sizeof(type) * (count)); \
202 x265_log(NULL, X265_LOG_ERROR, "malloc of size %d failed\n", sizeof(type) * (count)); \
206 #define CHECKED_MALLOC_ZERO(var, type, count) \
208 var = (type*)x265_malloc(sizeof(type) * (count)); \
210 memset((void*)var, 0, sizeof(type) * (count)); \
213 x265_log(NULL, X265_LOG_ERROR, "malloc of size %d failed\n", sizeof(type) * (count)); \
218 #if defined(_MSC_VER)
219 #define X265_LOG2F(x) (logf((float)(x)) * 1.44269504088896405f)
220 #define X265_LOG2(x) (log((double)(x)) * 1.4426950408889640513713538072172)
222 #define X265_LOG2F(x) log2f(x)
223 #define X265_LOG2(x) log2(x)
226 #define NUM_CU_DEPTH 4 // maximum number of CU depths
227 #define NUM_FULL_DEPTH 5 // maximum number of full depths
228 #define MIN_LOG2_CU_SIZE 3 // log2(minCUSize)
229 #define MAX_LOG2_CU_SIZE 6 // log2(maxCUSize)
230 #define MIN_CU_SIZE (1 << MIN_LOG2_CU_SIZE) // minimum allowable size of CU
231 #define MAX_CU_SIZE (1 << MAX_LOG2_CU_SIZE) // maximum allowable size of CU
233 #define LOG2_UNIT_SIZE 2 // log2(unitSize)
234 #define UNIT_SIZE (1 << LOG2_UNIT_SIZE) // unit size of CU partition
236 #define MAX_NUM_PARTITIONS 256
237 #define NUM_CU_PARTITIONS (1U << (g_maxFullDepth << 1))
239 #define MIN_PU_SIZE 4
240 #define MIN_TU_SIZE 4
241 #define MAX_NUM_SPU_W (MAX_CU_SIZE / MIN_PU_SIZE) // maximum number of SPU in horizontal line
243 #define MAX_LOG2_TR_SIZE 5
244 #define MAX_LOG2_TS_SIZE 2 // TODO: RExt
245 #define MAX_TR_SIZE (1 << MAX_LOG2_TR_SIZE)
246 #define MAX_TS_SIZE (1 << MAX_LOG2_TS_SIZE)
248 #define MAX_NUM_TR_COEFFS MAX_TR_SIZE * MAX_TR_SIZE /* Maximum number of transform coefficients, for a 32x32 transform */
249 #define MAX_NUM_TR_CATEGORIES 8 /* 32, 16, 8, 4 transform categories each for luma and chroma */
251 #define COEF_REMAIN_BIN_REDUCTION 3 // indicates the level at which the VLC
252 // transitions from Golomb-Rice to TU+EG(k)
254 #define SBH_THRESHOLD 4 // fixed sign bit hiding controlling threshold
256 #define C1FLAG_NUMBER 8 // maximum number of largerThan1 flag coded in one chunk: 16 in HM5
257 #define C2FLAG_NUMBER 1 // maximum number of largerThan2 flag coded in one chunk: 16 in HM5
259 #define SAO_ENCODING_RATE 0.75
260 #define SAO_ENCODING_RATE_CHROMA 0.5
262 #define MLS_GRP_NUM 64 // Max number of coefficient groups, max(16, 64)
263 #define MLS_CG_SIZE 4 // Coefficient group size of 4x4
264 #define MLS_CG_LOG2_SIZE 2
266 #define QUANT_IQUANT_SHIFT 20 // Q(QP%6) * IQ(QP%6) = 2^20
267 #define QUANT_SHIFT 14 // Q(4) = 2^14
268 #define SCALE_BITS 15 // Inherited from TMuC, presumably for fractional bit estimates in RDOQ
269 #define MAX_TR_DYNAMIC_RANGE 15 // Maximum transform dynamic range (excluding sign bit)
271 #define SHIFT_INV_1ST 7 // Shift after first inverse transform stage
272 #define SHIFT_INV_2ND 12 // Shift after second inverse transform stage
274 #define AMVP_DECIMATION_FACTOR 4
276 #define SCAN_SET_SIZE 16
277 #define LOG2_SCAN_SET_SIZE 4
281 #define VER_IDX 26 // index for intra VERTICAL mode
282 #define HOR_IDX 10 // index for intra HORIZONTAL mode
283 #define DC_IDX 1 // index for intra DC mode
284 #define NUM_CHROMA_MODE 5 // total number of chroma modes
285 #define DM_CHROMA_IDX 36 // chroma mode index for derived from luma intra mode
287 #define MDCS_ANGLE_LIMIT 4 // distance from true angle that horiz or vertical scan is allowed
288 #define MDCS_LOG2_MAX_SIZE 3 // TUs with log2 of size greater than this can only use diagonal scan
290 #define MAX_NUM_REF_PICS 16 // max. number of pictures used for reference
291 #define MAX_NUM_REF 16 // max. number of entries in picture reference list
293 #define REF_NOT_VALID -1
295 #define AMVP_NUM_CANDS 2 // number of AMVP candidates
296 #define MRG_MAX_NUM_CANDS 5 // max number of final merge candidates
298 #define CHROMA_H_SHIFT(x) (x == X265_CSP_I420 || x == X265_CSP_I422)
299 #define CHROMA_V_SHIFT(x) (x == X265_CSP_I420)
303 enum { SAO_NUM_OFFSET
= 4 };
305 // NOTE: MUST be alignment to 16 or 32 bytes for asm code
306 struct NoiseReduction
308 /* 0 = luma 4x4, 1 = luma 8x8, 2 = luma 16x16, 3 = luma 32x32
309 * 4 = chroma 4x4, 5 = chroma 8x8, 6 = chroma 16x16, 7 = chroma 32x32 */
310 uint16_t offsetDenoise
[MAX_NUM_TR_CATEGORIES
][MAX_NUM_TR_COEFFS
];
311 uint32_t residualSum
[MAX_NUM_TR_CATEGORIES
][MAX_NUM_TR_COEFFS
];
312 uint32_t count
[MAX_NUM_TR_CATEGORIES
];
324 SaoMergeMode mergeMode
;
326 uint32_t bandPos
; // BO band position
327 int offset
[SAO_NUM_OFFSET
];
331 mergeMode
= SAO_MERGE_NONE
;
343 SaoCtuParam
* ctuParam
[3];
349 for (int i
= 0; i
< 3; i
++)
355 delete[] ctuParam
[0];
356 delete[] ctuParam
[1];
357 delete[] ctuParam
[2];
363 TEXT_LUMA
= 0, // luma
364 TEXT_CHROMA_U
= 1, // chroma U
365 TEXT_CHROMA_V
= 2, // chroma V
366 MAX_NUM_COMPONENT
= 3
369 // coefficient scanning type used in ACS
372 SCAN_DIAG
= 0, // up-right diagonal scan
373 SCAN_HOR
= 1, // horizontal first scan
374 SCAN_VER
= 2, // vertical first scan
378 enum SignificanceMapContextType
380 CONTEXT_TYPE_4x4
= 0,
381 CONTEXT_TYPE_8x8
= 1,
382 CONTEXT_TYPE_NxN
= 2,
383 CONTEXT_NUMBER_OF_TYPES
= 3
387 /* outside x265 namespace, but prefixed. defined in common.cpp */
388 int64_t x265_mdate(void);
389 void x265_log(const x265_param
*param
, int level
, const char *fmt
, ...);
390 int x265_exp2fix8(double x
);
392 double x265_ssim2dB(double ssim
);
393 double x265_qScale2qp(double qScale
);
394 double x265_qp2qScale(double qp
);
395 uint32_t x265_picturePlaneSize(int csp
, int width
, int height
, int plane
);
397 void* x265_malloc(size_t size
);
398 void x265_free(void *ptr
);
399 char* x265_slurp_file(const char *filename
);
401 #include "constants.h"
403 #endif // ifndef X265_COMMON_H