blob: ba8e04df79f95e76b64eb290d342ca233755aa58 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
Yaowu Xuf883b422016-08-30 14:01:10 -070012#ifndef AOM_DSP_AOM_DSP_COMMON_H_
13#define AOM_DSP_AOM_DSP_COMMON_H_
Yaowu Xuc27fc142016-08-22 16:08:15 -070014
Yaowu Xuf883b422016-08-30 14:01:10 -070015#include "./aom_config.h"
16#include "aom/aom_integer.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070017#include "aom_ports/mem.h"
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
23#ifndef MAX_SB_SIZE
Debargha Mukherjee2ccf4b92018-02-27 17:30:46 -080024#if CONFIG_AV1
Yaowu Xuc27fc142016-08-22 16:08:15 -070025#define MAX_SB_SIZE 128
26#else
27#define MAX_SB_SIZE 64
Debargha Mukherjee2ccf4b92018-02-27 17:30:46 -080028#endif // CONFIG_AV1
Yaowu Xuc27fc142016-08-22 16:08:15 -070029#endif // ndef MAX_SB_SIZE
30
Yaowu Xuf883b422016-08-30 14:01:10 -070031#define AOMMIN(x, y) (((x) < (y)) ? (x) : (y))
32#define AOMMAX(x, y) (((x) > (y)) ? (x) : (y))
Yaowu Xuc27fc142016-08-22 16:08:15 -070033
34#define IMPLIES(a, b) (!(a) || (b)) // Logical 'a implies b' (or 'a -> b')
35
36#define IS_POWER_OF_TWO(x) (((x) & ((x)-1)) == 0)
37
Timothy B. Terriberry4e6a8f32017-02-24 11:00:59 -080038/* Left shifting a negative value became undefined behavior in C99 (downgraded
39 from merely implementation-defined in C89). This should still compile to the
40 correct thing on any two's-complement machine, but avoid ubsan warnings.*/
41#define AOM_SIGNED_SHL(x, shift) ((x) * (((x)*0 + 1) << (shift)))
42
Yaowu Xuc27fc142016-08-22 16:08:15 -070043// These can be used to give a hint about branch outcomes.
44// This can have an effect, even if your target processor has a
45// good branch predictor, as these hints can affect basic block
46// ordering by the compiler.
47#ifdef __GNUC__
48#define LIKELY(v) __builtin_expect(v, 1)
49#define UNLIKELY(v) __builtin_expect(v, 0)
50#else
51#define LIKELY(v) (v)
52#define UNLIKELY(v) (v)
53#endif
54
Yaowu Xucd44a052018-02-19 14:23:40 -080055typedef uint8_t qm_val_t;
Thomas Davies92aa22a2017-06-07 12:30:04 +010056#define AOM_QM_BITS 5
Thomas Daviesf3b5ee12017-07-18 15:16:43 +010057
Yaowu Xuc27fc142016-08-22 16:08:15 -070058// Note:
59// tran_low_t is the datatype used for final transform coefficients.
60// tran_high_t is the datatype used for intermediate transform stages.
61typedef int64_t tran_high_t;
62typedef int32_t tran_low_t;
Yaowu Xuc27fc142016-08-22 16:08:15 -070063
64static INLINE uint8_t clip_pixel(int val) {
65 return (val > 255) ? 255 : (val < 0) ? 0 : val;
66}
67
68static INLINE int clamp(int value, int low, int high) {
69 return value < low ? low : (value > high ? high : value);
70}
71
Debargha Mukherjeef053cba2017-06-21 19:04:00 -070072static INLINE int64_t clamp64(int64_t value, int64_t low, int64_t high) {
73 return value < low ? low : (value > high ? high : value);
74}
75
Yaowu Xuc27fc142016-08-22 16:08:15 -070076static INLINE double fclamp(double value, double low, double high) {
77 return value < low ? low : (value > high ? high : value);
78}
79
Yaowu Xuc27fc142016-08-22 16:08:15 -070080static INLINE uint16_t clip_pixel_highbd(int val, int bd) {
81 switch (bd) {
82 case 8:
83 default: return (uint16_t)clamp(val, 0, 255);
84 case 10: return (uint16_t)clamp(val, 0, 1023);
85 case 12: return (uint16_t)clamp(val, 0, 4095);
86 }
87}
Yaowu Xuc27fc142016-08-22 16:08:15 -070088
89#ifdef __cplusplus
90} // extern "C"
91#endif
92
Yaowu Xuf883b422016-08-30 14:01:10 -070093#endif // AOM_DSP_AOM_DSP_COMMON_H_