Imported Upstream version 1.4
[deb_x265.git] / source / common / contexts.h
CommitLineData
72b9787e
JB
1/*****************************************************************************
2* Copyright (C) 2014 x265 project
3*
4* Authors: Steve Borho <steve@borho.org>
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_CONTEXTS_H
25#define X265_CONTEXTS_H
26
27#include "common.h"
28
29#define NUM_SPLIT_FLAG_CTX 3 // number of context models for split flag
30#define NUM_SKIP_FLAG_CTX 3 // number of context models for skip flag
31
32#define NUM_MERGE_FLAG_EXT_CTX 1 // number of context models for merge flag of merge extended
33#define NUM_MERGE_IDX_EXT_CTX 1 // number of context models for merge index of merge extended
34
35#define NUM_PART_SIZE_CTX 4 // number of context models for partition size
36#define NUM_PRED_MODE_CTX 1 // number of context models for prediction mode
37
38#define NUM_ADI_CTX 1 // number of context models for intra prediction
39
40#define NUM_CHROMA_PRED_CTX 2 // number of context models for intra prediction (chroma)
41#define NUM_INTER_DIR_CTX 5 // number of context models for inter prediction direction
42#define NUM_MV_RES_CTX 2 // number of context models for motion vector difference
43
44#define NUM_REF_NO_CTX 2 // number of context models for reference index
45#define NUM_TRANS_SUBDIV_FLAG_CTX 3 // number of context models for transform subdivision flags
46#define NUM_QT_CBF_CTX 7 // number of context models for QT CBF
47#define NUM_QT_ROOT_CBF_CTX 1 // number of context models for QT ROOT CBF
48#define NUM_DELTA_QP_CTX 3 // number of context models for dQP
49
50#define NUM_SIG_CG_FLAG_CTX 2 // number of context models for MULTI_LEVEL_SIGNIFICANCE
51
52#define NUM_SIG_FLAG_CTX 42 // number of context models for sig flag
53#define NUM_SIG_FLAG_CTX_LUMA 27 // number of context models for luma sig flag
54#define NUM_SIG_FLAG_CTX_CHROMA 15 // number of context models for chroma sig flag
55
56#define NUM_CTX_LAST_FLAG_XY 18 // number of context models for last coefficient position
57#define NUM_CTX_LAST_FLAG_XY_LUMA 15 // number of context models for last coefficient position of luma
58#define NUM_CTX_LAST_FLAG_XY_CHROMA 3 // number of context models for last coefficient position of chroma
59
60#define NUM_ONE_FLAG_CTX 24 // number of context models for greater than 1 flag
61#define NUM_ONE_FLAG_CTX_LUMA 16 // number of context models for greater than 1 flag of luma
62#define NUM_ONE_FLAG_CTX_CHROMA 8 // number of context models for greater than 1 flag of chroma
63#define NUM_ABS_FLAG_CTX 6 // number of context models for greater than 2 flag
64#define NUM_ABS_FLAG_CTX_LUMA 4 // number of context models for greater than 2 flag of luma
65#define NUM_ABS_FLAG_CTX_CHROMA 2 // number of context models for greater than 2 flag of chroma
66
67#define NUM_MVP_IDX_CTX 1 // number of context models for MVP index
68
69#define NUM_SAO_MERGE_FLAG_CTX 1 // number of context models for SAO merge flags
70#define NUM_SAO_TYPE_IDX_CTX 1 // number of context models for SAO type index
71
72#define NUM_TRANSFORMSKIP_FLAG_CTX 1 // number of context models for transform skipping
73#define NUM_TQUANT_BYPASS_FLAG_CTX 1
74#define CNU 154 // dummy initialization value for unused context models 'Context model Not Used'
75
76// Offset for context
77#define OFF_SPLIT_FLAG_CTX (0)
78#define OFF_SKIP_FLAG_CTX (OFF_SPLIT_FLAG_CTX + NUM_SPLIT_FLAG_CTX)
79#define OFF_MERGE_FLAG_EXT_CTX (OFF_SKIP_FLAG_CTX + NUM_SKIP_FLAG_CTX)
80#define OFF_MERGE_IDX_EXT_CTX (OFF_MERGE_FLAG_EXT_CTX + NUM_MERGE_FLAG_EXT_CTX)
81#define OFF_PART_SIZE_CTX (OFF_MERGE_IDX_EXT_CTX + NUM_MERGE_IDX_EXT_CTX)
82#define OFF_PRED_MODE_CTX (OFF_PART_SIZE_CTX + NUM_PART_SIZE_CTX)
83#define OFF_ADI_CTX (OFF_PRED_MODE_CTX + NUM_PRED_MODE_CTX)
84#define OFF_CHROMA_PRED_CTX (OFF_ADI_CTX + NUM_ADI_CTX)
85#define OFF_DELTA_QP_CTX (OFF_CHROMA_PRED_CTX + NUM_CHROMA_PRED_CTX)
86#define OFF_INTER_DIR_CTX (OFF_DELTA_QP_CTX + NUM_DELTA_QP_CTX)
87#define OFF_REF_NO_CTX (OFF_INTER_DIR_CTX + NUM_INTER_DIR_CTX)
88#define OFF_MV_RES_CTX (OFF_REF_NO_CTX + NUM_REF_NO_CTX)
89#define OFF_QT_CBF_CTX (OFF_MV_RES_CTX + NUM_MV_RES_CTX)
90#define OFF_TRANS_SUBDIV_FLAG_CTX (OFF_QT_CBF_CTX + NUM_QT_CBF_CTX)
91#define OFF_QT_ROOT_CBF_CTX (OFF_TRANS_SUBDIV_FLAG_CTX + NUM_TRANS_SUBDIV_FLAG_CTX)
92#define OFF_SIG_CG_FLAG_CTX (OFF_QT_ROOT_CBF_CTX + NUM_QT_ROOT_CBF_CTX)
93#define OFF_SIG_FLAG_CTX (OFF_SIG_CG_FLAG_CTX + 2 * NUM_SIG_CG_FLAG_CTX)
94#define OFF_CTX_LAST_FLAG_X (OFF_SIG_FLAG_CTX + NUM_SIG_FLAG_CTX)
95#define OFF_CTX_LAST_FLAG_Y (OFF_CTX_LAST_FLAG_X + NUM_CTX_LAST_FLAG_XY)
96#define OFF_ONE_FLAG_CTX (OFF_CTX_LAST_FLAG_Y + NUM_CTX_LAST_FLAG_XY)
97#define OFF_ABS_FLAG_CTX (OFF_ONE_FLAG_CTX + NUM_ONE_FLAG_CTX)
98#define OFF_MVP_IDX_CTX (OFF_ABS_FLAG_CTX + NUM_ABS_FLAG_CTX)
99#define OFF_SAO_MERGE_FLAG_CTX (OFF_MVP_IDX_CTX + NUM_MVP_IDX_CTX)
100#define OFF_SAO_TYPE_IDX_CTX (OFF_SAO_MERGE_FLAG_CTX + NUM_SAO_MERGE_FLAG_CTX)
101#define OFF_TRANSFORMSKIP_FLAG_CTX (OFF_SAO_TYPE_IDX_CTX + NUM_SAO_TYPE_IDX_CTX)
102#define OFF_TQUANT_BYPASS_FLAG_CTX (OFF_TRANSFORMSKIP_FLAG_CTX + 2 * NUM_TRANSFORMSKIP_FLAG_CTX)
103#define MAX_OFF_CTX_MOD (OFF_TQUANT_BYPASS_FLAG_CTX + NUM_TQUANT_BYPASS_FLAG_CTX)
104
105namespace x265 {
106// private namespace
107
108extern const uint32_t g_entropyBits[128];
109extern const uint8_t g_nextState[128][2];
110
111#define sbacGetMps(S) ((S) & 1)
112#define sbacGetState(S) ((S) >> 1)
113#define sbacNext(S, V) (g_nextState[(S)][(V)])
114#define sbacGetEntropyBits(S, V) (g_entropyBits[(S) ^ (V)])
115#define sbacGetEntropyBitsTrm(V) (g_entropyBits[126 ^ (V)])
116
117#define MAX_NUM_CHANNEL_TYPE 2
118
119static const uint32_t ctxCbf[3][5] = { { 1, 0, 0, 0, 0 }, { 2, 3, 4, 5, 6 }, { 2, 3, 4, 5, 6 } };
120static const uint32_t significanceMapContextSetStart[MAX_NUM_CHANNEL_TYPE][3] = { { 0, 9, 21 }, { 0, 9, 12 } };
121static const uint32_t significanceMapContextSetSize[MAX_NUM_CHANNEL_TYPE][3] = { { 9, 12, 6 }, { 9, 3, 3 } };
122static const uint32_t nonDiagonalScan8x8ContextOffset[MAX_NUM_CHANNEL_TYPE] = { 6, 0 };
123static const uint32_t notFirstGroupNeighbourhoodContextOffset[MAX_NUM_CHANNEL_TYPE] = { 3, 0 };
124
125// initial probability for cu_transquant_bypass flag
126static const uint8_t INIT_CU_TRANSQUANT_BYPASS_FLAG[3][NUM_TQUANT_BYPASS_FLAG_CTX] =
127{
128 { 154 },
129 { 154 },
130 { 154 },
131};
132
133// initial probability for split flag
134static const uint8_t INIT_SPLIT_FLAG[3][NUM_SPLIT_FLAG_CTX] =
135{
136 { 107, 139, 126, },
137 { 107, 139, 126, },
138 { 139, 141, 157, },
139};
140
141static const uint8_t INIT_SKIP_FLAG[3][NUM_SKIP_FLAG_CTX] =
142{
143 { 197, 185, 201, },
144 { 197, 185, 201, },
145 { CNU, CNU, CNU, },
146};
147
148static const uint8_t INIT_MERGE_FLAG_EXT[3][NUM_MERGE_FLAG_EXT_CTX] =
149{
150 { 154, },
151 { 110, },
152 { CNU, },
153};
154
155static const uint8_t INIT_MERGE_IDX_EXT[3][NUM_MERGE_IDX_EXT_CTX] =
156{
157 { 137, },
158 { 122, },
159 { CNU, },
160};
161
162static const uint8_t INIT_PART_SIZE[3][NUM_PART_SIZE_CTX] =
163{
164 { 154, 139, 154, 154 },
165 { 154, 139, 154, 154 },
166 { 184, CNU, CNU, CNU },
167};
168
169static const uint8_t INIT_PRED_MODE[3][NUM_PRED_MODE_CTX] =
170{
171 { 134, },
172 { 149, },
173 { CNU, },
174};
175
176static const uint8_t INIT_INTRA_PRED_MODE[3][NUM_ADI_CTX] =
177{
178 { 183, },
179 { 154, },
180 { 184, },
181};
182
183static const uint8_t INIT_CHROMA_PRED_MODE[3][NUM_CHROMA_PRED_CTX] =
184{
185 { 152, 139, },
186 { 152, 139, },
187 { 63, 139, },
188};
189
190static const uint8_t INIT_INTER_DIR[3][NUM_INTER_DIR_CTX] =
191{
192 { 95, 79, 63, 31, 31, },
193 { 95, 79, 63, 31, 31, },
194 { CNU, CNU, CNU, CNU, CNU, },
195};
196
197static const uint8_t INIT_MVD[3][NUM_MV_RES_CTX] =
198{
199 { 169, 198, },
200 { 140, 198, },
201 { CNU, CNU, },
202};
203
204static const uint8_t INIT_REF_PIC[3][NUM_REF_NO_CTX] =
205{
206 { 153, 153 },
207 { 153, 153 },
208 { CNU, CNU },
209};
210
211static const uint8_t INIT_DQP[3][NUM_DELTA_QP_CTX] =
212{
213 { 154, 154, 154, },
214 { 154, 154, 154, },
215 { 154, 154, 154, },
216};
217
218static const uint8_t INIT_QT_CBF[3][NUM_QT_CBF_CTX] =
219{
220 { 153, 111, 149, 92, 167, 154, 154 },
221 { 153, 111, 149, 107, 167, 154, 154 },
222 { 111, 141, 94, 138, 182, 154, 154 },
223};
224
225static const uint8_t INIT_QT_ROOT_CBF[3][NUM_QT_ROOT_CBF_CTX] =
226{
227 { 79, },
228 { 79, },
229 { CNU, },
230};
231
232static const uint8_t INIT_LAST[3][NUM_CTX_LAST_FLAG_XY] =
233{
234 { 125, 110, 124, 110, 95, 94, 125, 111, 111, 79, 125, 126, 111, 111, 79,
235 108, 123, 93 },
236 { 125, 110, 94, 110, 95, 79, 125, 111, 110, 78, 110, 111, 111, 95, 94,
237 108, 123, 108 },
238 { 110, 110, 124, 125, 140, 153, 125, 127, 140, 109, 111, 143, 127, 111, 79,
239 108, 123, 63 },
240};
241
242static const uint8_t INIT_SIG_CG_FLAG[3][2 * NUM_SIG_CG_FLAG_CTX] =
243{
244 { 121, 140,
245 61, 154, },
246 { 121, 140,
247 61, 154, },
248 { 91, 171,
249 134, 141, },
250};
251
252static const uint8_t INIT_SIG_FLAG[3][NUM_SIG_FLAG_CTX] =
253{
254 { 170, 154, 139, 153, 139, 123, 123, 63, 124, 166, 183, 140, 136, 153, 154, 166, 183, 140, 136, 153, 154, 166, 183, 140, 136, 153, 154, 170, 153, 138, 138, 122, 121, 122, 121, 167, 151, 183, 140, 151, 183, 140, },
255 { 155, 154, 139, 153, 139, 123, 123, 63, 153, 166, 183, 140, 136, 153, 154, 166, 183, 140, 136, 153, 154, 166, 183, 140, 136, 153, 154, 170, 153, 123, 123, 107, 121, 107, 121, 167, 151, 183, 140, 151, 183, 140, },
256 { 111, 111, 125, 110, 110, 94, 124, 108, 124, 107, 125, 141, 179, 153, 125, 107, 125, 141, 179, 153, 125, 107, 125, 141, 179, 153, 125, 140, 139, 182, 182, 152, 136, 152, 136, 153, 136, 139, 111, 136, 139, 111, },
257};
258
259static const uint8_t INIT_ONE_FLAG[3][NUM_ONE_FLAG_CTX] =
260{
261 { 154, 196, 167, 167, 154, 152, 167, 182, 182, 134, 149, 136, 153, 121, 136, 122, 169, 208, 166, 167, 154, 152, 167, 182, },
262 { 154, 196, 196, 167, 154, 152, 167, 182, 182, 134, 149, 136, 153, 121, 136, 137, 169, 194, 166, 167, 154, 167, 137, 182, },
263 { 140, 92, 137, 138, 140, 152, 138, 139, 153, 74, 149, 92, 139, 107, 122, 152, 140, 179, 166, 182, 140, 227, 122, 197, },
264};
265
266static const uint8_t INIT_ABS_FLAG[3][NUM_ABS_FLAG_CTX] =
267{
268 { 107, 167, 91, 107, 107, 167, },
269 { 107, 167, 91, 122, 107, 167, },
270 { 138, 153, 136, 167, 152, 152, },
271};
272
273static const uint8_t INIT_MVP_IDX[3][NUM_MVP_IDX_CTX] =
274{
275 { 168 },
276 { 168 },
277 { CNU },
278};
279
280static const uint8_t INIT_SAO_MERGE_FLAG[3][NUM_SAO_MERGE_FLAG_CTX] =
281{
282 { 153, },
283 { 153, },
284 { 153, },
285};
286
287static const uint8_t INIT_SAO_TYPE_IDX[3][NUM_SAO_TYPE_IDX_CTX] =
288{
289 { 160, },
290 { 185, },
291 { 200, },
292};
293
294static const uint8_t INIT_TRANS_SUBDIV_FLAG[3][NUM_TRANS_SUBDIV_FLAG_CTX] =
295{
296 { 224, 167, 122, },
297 { 124, 138, 94, },
298 { 153, 138, 138, },
299};
300
301static const uint8_t INIT_TRANSFORMSKIP_FLAG[3][2 * NUM_TRANSFORMSKIP_FLAG_CTX] =
302{
303 { 139, 139 },
304 { 139, 139 },
305 { 139, 139 },
306};
307}
308
309#endif // ifndef X265_CONTEXTS_H