blob: e6319aa6b22a15dd4940efdfac7ada728c403bdd [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Krishna Rapaka7319db52021-09-28 20:35:29 -07002 * Copyright (c) 2021, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Vibhoothi41c6dd72021-10-12 18:48:26 +00004 * This source code is subject to the terms of the BSD 3-Clause Clear License
5 * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
6 * License was not distributed with this source code in the LICENSE file, you
7 * can obtain it at aomedia.org/license/software-license/bsd-3-c-c/. If the
8 * Alliance for Open Media Patent License 1.0 was not distributed with this
9 * source code in the PATENTS file, you can obtain it at
10 * aomedia.org/license/patent-license/.
Yaowu Xuc27fc142016-08-22 16:08:15 -070011 */
12
James Zerne1cbb132018-08-22 14:10:36 -070013#ifndef AOM_AV1_COMMON_ENTROPY_H_
14#define AOM_AV1_COMMON_ENTROPY_H_
Yaowu Xuc27fc142016-08-22 16:08:15 -070015
Tom Finegan60e653d2018-05-22 11:34:58 -070016#include "config/aom_config.h"
17
Yaowu Xuf883b422016-08-30 14:01:10 -070018#include "aom/aom_integer.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070019#include "aom_dsp/prob.h"
20
Yaowu Xuc27fc142016-08-22 16:08:15 -070021#include "av1/common/common.h"
Alex Converseda3d94f2017-03-15 14:54:29 -070022#include "av1/common/common_data.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070023#include "av1/common/enums.h"
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
hui suef125a02017-07-31 12:56:15 -070029#define TOKEN_CDF_Q_CTXS 4
hui su0d103572017-03-01 17:58:01 -080030
Xin Zhaof54ab052021-07-28 11:18:32 -070031#if CONFIG_CONTEXT_DERIVATION
32#define CROSS_COMPONENT_CONTEXTS 3
33#define V_TXB_SKIP_CONTEXTS 12
34#define V_TXB_SKIP_CONTEXT_OFFSET 6
35#endif // CONFIG_CONTEXT_DERIVATION
36
Alican Nalci9d47c562022-02-23 23:14:24 +000037#define TXB_SKIP_CONTEXTS 20
Alican Nalci9d47c562022-02-23 23:14:24 +000038
Alican Nalci9d47c562022-02-23 23:14:24 +000039#define IDTX_SIGN_CONTEXTS 9
40#define IDTX_SIG_COEF_CONTEXTS 14
41#define IDTX_LEVEL_CONTEXTS 14
Angie Chiangf12cc4a2017-08-28 15:42:33 -070042
Jingning Hanff4a9f82018-06-08 10:48:45 -070043#define EOB_COEF_CONTEXTS 9
Angie Chiangf12cc4a2017-08-28 15:42:33 -070044
Luhang Xu05947a72022-11-09 18:56:12 +000045#if CONFIG_PAR_HIDING
46#define COEFF_BASE_PH_CONTEXTS 5
47#define COEFF_BR_PH_CONTEXTS 7
48#endif // CONFIG_PAR_HIDING
49
Alican Nalcic43cd392022-09-13 23:10:44 +000050#if CONFIG_ATC_COEFCODING
51// Number of coefficient coding contexts for the low-frequency region
52// for 2D and 1D transforms
53#define LF_SIG_COEF_CONTEXTS_2D 21
54#define LF_SIG_COEF_CONTEXTS_1D 12
55#define LF_SIG_COEF_CONTEXTS (LF_SIG_COEF_CONTEXTS_2D + LF_SIG_COEF_CONTEXTS_1D)
56#define LF_LEVEL_CONTEXTS 14 // low-range contexts
57
58// Number of coefficient coding contexts for the higher-frequency default region
59// for 2D and 1D transforms
60#define SIG_COEF_CONTEXTS_2D 20
61#define SIG_COEF_CONTEXTS SIG_COEF_CONTEXTS_2D // base range contexts
62#define LEVEL_CONTEXTS 7 // low range contexts
63
64#define SIG_COEF_CONTEXTS_EOB 4 // context count for the EOB coefficient
65
66// Number of symbols for base range coding in low-frequency region
67#define LF_BASE_SYMBOLS 6
68#define LF_NUM_BASE_LEVELS (LF_BASE_SYMBOLS - 2)
69#define LF_MAX_BASE_BR_RANGE (COEFF_BASE_RANGE + LF_NUM_BASE_LEVELS + 1)
70
71// Limits to determine the low-frequency region for coefficient coding.
72#define LF_2D_LIM 4 // row + column limit
73#define LF_2D_LIM_UV 1 // row + column limit for chroma
74#define LF_RC_LIM 2 // row or column limit
75#define LF_RC_LIM_UV 1 // row or column limit for chroma
76#else
77#define LEVEL_CONTEXTS 21
Jingning Hana24a6902017-10-26 14:25:46 -070078#define SIG_COEF_CONTEXTS_2D 26
Angie Chiang2b38def2017-08-28 14:04:09 -070079#define SIG_COEF_CONTEXTS_1D 16
Ola Hugossond2352ec2017-11-16 09:20:25 +010080#define SIG_COEF_CONTEXTS_EOB 4
Dake He4d447692017-12-15 09:10:06 -080081#define SIG_COEF_CONTEXTS (SIG_COEF_CONTEXTS_2D + SIG_COEF_CONTEXTS_1D)
Alican Nalcic43cd392022-09-13 23:10:44 +000082#endif // CONFIG_ATC_COEFCODING
Dake He03a32922017-10-31 08:06:45 -070083
Dake He03a32922017-10-31 08:06:45 -070084#define COEFF_BASE_CONTEXTS (SIG_COEF_CONTEXTS)
Angie Chiangbd57fc52017-02-24 11:29:27 -080085#define DC_SIGN_CONTEXTS 3
86
87#define BR_TMP_OFFSET 12
88#define BR_REF_CAT 4
Angie Chiangbd57fc52017-02-24 11:29:27 -080089
90#define NUM_BASE_LEVELS 2
Ola Hugossone72a2092017-11-12 09:11:53 +010091
Ola Hugossone72a2092017-11-12 09:11:53 +010092#define BR_CDF_SIZE (4)
93#define COEFF_BASE_RANGE (4 * (BR_CDF_SIZE - 1))
Angie Chiangbd57fc52017-02-24 11:29:27 -080094
Hui Su8570a2c2019-10-01 10:18:47 -070095#define COEFF_CONTEXT_BITS 3
Angie Chiangbd57fc52017-02-24 11:29:27 -080096#define COEFF_CONTEXT_MASK ((1 << COEFF_CONTEXT_BITS) - 1)
Angie Chiang4f5c3522018-02-26 17:01:32 -080097#define MAX_BASE_BR_RANGE (COEFF_BASE_RANGE + NUM_BASE_LEVELS + 1)
Angie Chiangdef11252017-08-18 14:37:32 -070098
99#define BASE_CONTEXT_POSITION_NUM 12
Angie Chiangcf85d5c2017-08-20 12:56:19 -0700100
Satish Kumar Suman4667aa12018-12-14 18:28:19 +0530101enum {
Angie Chiangcf85d5c2017-08-20 12:56:19 -0700102 TX_CLASS_2D = 0,
103 TX_CLASS_HORIZ = 1,
104 TX_CLASS_VERT = 2,
105 TX_CLASSES = 3,
Satish Kumar Suman4667aa12018-12-14 18:28:19 +0530106} UENUM1BYTE(TX_CLASS);
Angie Chiangcf85d5c2017-08-20 12:56:19 -0700107
Yaowu Xuc27fc142016-08-22 16:08:15 -0700108#define DCT_MAX_VALUE 16384
Yaowu Xuc27fc142016-08-22 16:08:15 -0700109#define DCT_MAX_VALUE_HIGH10 65536
110#define DCT_MAX_VALUE_HIGH12 262144
Yaowu Xuc27fc142016-08-22 16:08:15 -0700111
Johann6b41d4d2018-02-08 14:32:53 -0800112/* Coefficients are predicted via a 3-dimensional probability table indexed on
113 * REF_TYPES, COEF_BANDS and COEF_CONTEXTS. */
Yaowu Xuc27fc142016-08-22 16:08:15 -0700114#define REF_TYPES 2 // intra=0, inter=1
115
Yaowu Xuf883b422016-08-30 14:01:10 -0700116struct AV1Common;
Thomas Davies028b57f2017-02-22 16:42:11 +0000117struct frame_contexts;
Hui Sudc54be62018-03-14 19:14:28 -0700118void av1_reset_cdf_symbol_counters(struct frame_contexts *fc);
Yaowu Xuf883b422016-08-30 14:01:10 -0700119void av1_default_coef_probs(struct AV1Common *cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700120
Nathan E. Egge46e84902016-08-24 14:52:42 -0400121struct frame_contexts;
Nathan E. Eggead66b812017-05-09 11:57:31 -0400122
Yaowu Xuc27fc142016-08-22 16:08:15 -0700123typedef char ENTROPY_CONTEXT;
124
125static INLINE int combine_entropy_contexts(ENTROPY_CONTEXT a,
126 ENTROPY_CONTEXT b) {
127 return (a != 0) + (b != 0);
128}
129
Yue Chen30872492023-02-14 21:38:31 +0000130static INLINE ENTROPY_CONTEXT get_entropy_context_1d(const ENTROPY_CONTEXT *ctx,
131 int size) {
132 switch (size) {
133 case 4: return ctx[0] != 0;
134 case 8:
135#if CONFIG_H_PARTITION
136 return ctx[0] != 0 || ctx[1] != 0;
137#else
138 return !!*(const uint16_t *)ctx;
139#endif // CONFIG_H_PARTITION
140 case 16:
141#if CONFIG_H_PARTITION
142 return !!(*(const uint16_t *)ctx | *(const uint16_t *)(ctx + 2));
143#else
144 return !!*(const uint32_t *)ctx;
145#endif // CONFIG_H_PARTITION
146 case 32:
147#if CONFIG_H_PARTITION
148 return !!(*(const uint32_t *)ctx | *(const uint32_t *)(ctx + 4));
149#else
150 return !*(const uint64_t *)ctx;
151#endif // CONFIG_H_PARTITION
152 case 64: return !!(*(const uint64_t *)ctx | *(const uint64_t *)(ctx + 8));
153 default: assert(0 && "Invalid transform 1d size."); break;
154 }
155
156 return 0;
157}
158
Yaowu Xuc27fc142016-08-22 16:08:15 -0700159static INLINE int get_entropy_context(TX_SIZE tx_size, const ENTROPY_CONTEXT *a,
160 const ENTROPY_CONTEXT *l) {
Yue Chen30872492023-02-14 21:38:31 +0000161 assert(tx_size < TX_SIZES_ALL);
162 const int txw = tx_size_wide[tx_size];
163 const int txh = tx_size_high[tx_size];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700164 ENTROPY_CONTEXT above_ec = 0, left_ec = 0;
165
Yue Chen30872492023-02-14 21:38:31 +0000166 above_ec = get_entropy_context_1d(a, txw);
167 left_ec = get_entropy_context_1d(l, txh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700168 return combine_entropy_contexts(above_ec, left_ec);
169}
170
Debargha Mukherjeeb3eda2f2017-11-28 16:00:20 -0800171static INLINE TX_SIZE get_txsize_entropy_ctx(TX_SIZE txsize) {
172 return (TX_SIZE)((txsize_sqr_map[txsize] + txsize_sqr_up_map[txsize] + 1) >>
173 1);
174}
175
Yaowu Xuc27fc142016-08-22 16:08:15 -0700176#ifdef __cplusplus
177} // extern "C"
178#endif
179
James Zerne1cbb132018-08-22 14:10:36 -0700180#endif // AOM_AV1_COMMON_ENTROPY_H_