1 /*
2 * Copyright 2015 Rockchip Electronics Co. LTD
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define MODULE_TAG "h265e_cabac"
18
19 #include "mpp_mem.h"
20 #include "mpp_common.h"
21
22 #include "h265e_slice.h"
23 #include "h265e_codec.h"
24 #include "h265e_enctropy.h"
25
26 #ifdef __GNUC__ /* GCCs builtin atomics */
27 #define CLZ32(id, x) id = (unsigned long)__builtin_clz(x) ^ 31
28 #elif defined(_MSC_VER) /* Windows atomic intrinsics */
29 #define CLZ32(id, x) _BitScanReverse(&id, x)
30 #endif
31
32 const RK_U8 g_lpsTable[64][4] = {
33 { 128, 176, 208, 240 },
34 { 128, 167, 197, 227 },
35 { 128, 158, 187, 216 },
36 { 123, 150, 178, 205 },
37 { 116, 142, 169, 195 },
38 { 111, 135, 160, 185 },
39 { 105, 128, 152, 175 },
40 { 100, 122, 144, 166 },
41 { 95, 116, 137, 158 },
42 { 90, 110, 130, 150 },
43 { 85, 104, 123, 142 },
44 { 81, 99, 117, 135 },
45 { 77, 94, 111, 128 },
46 { 73, 89, 105, 122 },
47 { 69, 85, 100, 116 },
48 { 66, 80, 95, 110 },
49 { 62, 76, 90, 104 },
50 { 59, 72, 86, 99 },
51 { 56, 69, 81, 94 },
52 { 53, 65, 77, 89 },
53 { 51, 62, 73, 85 },
54 { 48, 59, 69, 80 },
55 { 46, 56, 66, 76 },
56 { 43, 53, 63, 72 },
57 { 41, 50, 59, 69 },
58 { 39, 48, 56, 65 },
59 { 37, 45, 54, 62 },
60 { 35, 43, 51, 59 },
61 { 33, 41, 48, 56 },
62 { 32, 39, 46, 53 },
63 { 30, 37, 43, 50 },
64 { 29, 35, 41, 48 },
65 { 27, 33, 39, 45 },
66 { 26, 31, 37, 43 },
67 { 24, 30, 35, 41 },
68 { 23, 28, 33, 39 },
69 { 22, 27, 32, 37 },
70 { 21, 26, 30, 35 },
71 { 20, 24, 29, 33 },
72 { 19, 23, 27, 31 },
73 { 18, 22, 26, 30 },
74 { 17, 21, 25, 28 },
75 { 16, 20, 23, 27 },
76 { 15, 19, 22, 25 },
77 { 14, 18, 21, 24 },
78 { 14, 17, 20, 23 },
79 { 13, 16, 19, 22 },
80 { 12, 15, 18, 21 },
81 { 12, 14, 17, 20 },
82 { 11, 14, 16, 19 },
83 { 11, 13, 15, 18 },
84 { 10, 12, 15, 17 },
85 { 10, 12, 14, 16 },
86 { 9, 11, 13, 15 },
87 { 9, 11, 12, 14 },
88 { 8, 10, 12, 14 },
89 { 8, 9, 11, 13 },
90 { 7, 9, 11, 12 },
91 { 7, 9, 10, 12 },
92 { 7, 8, 10, 11 },
93 { 6, 8, 9, 11 },
94 { 6, 7, 9, 10 },
95 { 6, 7, 8, 9 },
96 { 2, 2, 2, 2 }
97 };
98
99 const RK_U8 g_nextState[128][2] = {
100 { 2, 1 }, { 0, 3 }, { 4, 0 }, { 1, 5 }, { 6, 2 }, { 3, 7 }, { 8, 4 }, { 5, 9 },
101 { 10, 4 }, { 5, 11 }, { 12, 8 }, { 9, 13 }, { 14, 8 }, { 9, 15 }, { 16, 10 }, { 11, 17 },
102 { 18, 12 }, { 13, 19 }, { 20, 14 }, { 15, 21 }, { 22, 16 }, { 17, 23 }, { 24, 18 }, { 19, 25 },
103 { 26, 18 }, { 19, 27 }, { 28, 22 }, { 23, 29 }, { 30, 22 }, { 23, 31 }, { 32, 24 }, { 25, 33 },
104 { 34, 26 }, { 27, 35 }, { 36, 26 }, { 27, 37 }, { 38, 30 }, { 31, 39 }, { 40, 30 }, { 31, 41 },
105 { 42, 32 }, { 33, 43 }, { 44, 32 }, { 33, 45 }, { 46, 36 }, { 37, 47 }, { 48, 36 }, { 37, 49 },
106 { 50, 38 }, { 39, 51 }, { 52, 38 }, { 39, 53 }, { 54, 42 }, { 43, 55 }, { 56, 42 }, { 43, 57 },
107 { 58, 44 }, { 45, 59 }, { 60, 44 }, { 45, 61 }, { 62, 46 }, { 47, 63 }, { 64, 48 }, { 49, 65 },
108 { 66, 48 }, { 49, 67 }, { 68, 50 }, { 51, 69 }, { 70, 52 }, { 53, 71 }, { 72, 52 }, { 53, 73 },
109 { 74, 54 }, { 55, 75 }, { 76, 54 }, { 55, 77 }, { 78, 56 }, { 57, 79 }, { 80, 58 }, { 59, 81 },
110 { 82, 58 }, { 59, 83 }, { 84, 60 }, { 61, 85 }, { 86, 60 }, { 61, 87 }, { 88, 60 }, { 61, 89 },
111 { 90, 62 }, { 63, 91 }, { 92, 64 }, { 65, 93 }, { 94, 64 }, { 65, 95 }, { 96, 66 }, { 67, 97 },
112 { 98, 66 }, { 67, 99 }, { 100, 66 }, { 67, 101 }, { 102, 68 }, { 69, 103 }, { 104, 68 }, { 69, 105 },
113 { 106, 70 }, { 71, 107 }, { 108, 70 }, { 71, 109 }, { 110, 70 }, { 71, 111 }, { 112, 72 }, { 73, 113 },
114 { 114, 72 }, { 73, 115 }, { 116, 72 }, { 73, 117 }, { 118, 74 }, { 75, 119 }, { 120, 74 }, { 75, 121 },
115 { 122, 74 }, { 75, 123 }, { 124, 76 }, { 77, 125 }, { 124, 76 }, { 77, 125 }, { 126, 126 }, { 127, 127 }
116 };
117
sbacInit(RK_S32 qp,RK_S32 initValue)118 RK_U8 sbacInit(RK_S32 qp, RK_S32 initValue)
119 {
120 RK_S32 slope = (initValue >> 4) * 5 - 45;
121 RK_S32 offset = ((initValue & 15) << 3) - 16;
122 RK_S32 initState = MPP_MIN(MPP_MAX(1, (((slope * qp) >> 4) + offset)), 126);
123 RK_U32 mpState = (initState >= 64);
124 RK_U8 m_state = ((mpState ? (initState - 64) : (63 - initState)) << 1) + mpState;
125
126 return m_state;
127 }
128
initBuffer(H265eContextModel_t * contextModel,SliceType sliceType,RK_U8 cabacIntFlag,RK_S32 qp,RK_U8 * ctxModel,RK_S32 size)129 static void initBuffer(H265eContextModel_t* contextModel, SliceType sliceType, RK_U8 cabacIntFlag, RK_S32 qp, RK_U8* ctxModel, RK_S32 size)
130 {
131 RK_S8 initType = 0;
132 RK_S32 n = 0;
133 h265e_dbg_skip("sliceType = %d", sliceType);
134 if (sliceType == I_SLICE)
135 initType = 0;
136 else if (sliceType == P_SLICE)
137 initType = cabacIntFlag ? 2 : 1;
138 else
139 initType = cabacIntFlag ? 1 : 2;
140
141 ctxModel += (2 - initType) * size;
142
143 for (n = 0; n < size; n++) {
144 contextModel[n].m_state = sbacInit(qp, ctxModel[n]);
145 //mpp_log("contextModel[%d].m_state = %d", n, contextModel[n].m_state);
146 contextModel[n].bBinsCoded = 0;
147 }
148 }
149
h265e_reset_enctropy(void * slice_ctx)150 void h265e_reset_enctropy(void *slice_ctx)
151 {
152 H265eSlice *slice = (H265eSlice *)slice_ctx;
153 RK_U8 cabacInitFlag = slice->m_cabacInitFlag;
154 RK_S32 qp = slice->m_sliceQp;
155 SliceType sliceType = slice->m_sliceType;
156
157 h265e_dbg_func("enter\n");
158 initBuffer(&slice->m_contextModels[OFF_SPLIT_FLAG_CTX], sliceType, cabacInitFlag, qp, (RK_U8*)INIT_SPLIT_FLAG, NUM_SPLIT_FLAG_CTX);
159 initBuffer(&slice->m_contextModels[OFF_SKIP_FLAG_CTX], sliceType, cabacInitFlag, qp, (RK_U8*)INIT_SKIP_FLAG, NUM_SKIP_FLAG_CTX);
160 initBuffer(&slice->m_contextModels[OFF_MERGE_FLAG_EXT_CTX], sliceType, cabacInitFlag, qp, (RK_U8*)INIT_MERGE_FLAG_EXT, NUM_MERGE_FLAG_EXT_CTX);
161 initBuffer(&slice->m_contextModels[OFF_MERGE_IDX_EXT_CTX], sliceType, cabacInitFlag, qp, (uint8_t*)INIT_MERGE_IDX_EXT, NUM_MERGE_IDX_EXT_CTX);
162
163 h265e_dbg_func("leave\n");
164 }
165
h265e_cabac_resetBits(H265eCabacCtx * cabac_ctx)166 void h265e_cabac_resetBits(H265eCabacCtx *cabac_ctx)
167 {
168 h265e_dbg_func("enter\n");
169
170 cabac_ctx->m_low = 0;
171 cabac_ctx->m_bitsLeft = -12;
172 cabac_ctx->m_numBufferedBytes = 0;
173 cabac_ctx->m_bufferedByte = 0xff;
174 cabac_ctx->m_fracBits = 0;
175
176 h265e_dbg_func("leave\n");
177 }
178
h265e_cabac_start(H265eCabacCtx * cabac_ctx)179 void h265e_cabac_start(H265eCabacCtx *cabac_ctx)
180 {
181 h265e_dbg_func("enter\n");
182 cabac_ctx->m_low = 0;
183 cabac_ctx->m_range = 510;
184 cabac_ctx->m_bitsLeft = -12;
185 cabac_ctx->m_numBufferedBytes = 0;
186 cabac_ctx->m_bufferedByte = 0xff;
187 }
188
h265e_cabac_init(H265eCabacCtx * cabac_ctx,MppWriteCtx * bitIf)189 void h265e_cabac_init(H265eCabacCtx *cabac_ctx, MppWriteCtx * bitIf)
190 {
191 h265e_dbg_func("enter\n");
192 cabac_ctx->m_bitIf = bitIf;
193 h265e_cabac_start(cabac_ctx);
194 h265e_dbg_func("leave\n");
195 }
196
h265e_cabac_writeOut(H265eCabacCtx * cabac_ctx)197 void h265e_cabac_writeOut(H265eCabacCtx *cabac_ctx)
198 {
199 MppWriteCtx* s = cabac_ctx->m_bitIf;
200 RK_U32 leadByte = cabac_ctx->m_low >> (13 + cabac_ctx->m_bitsLeft);
201 RK_U32 low_mask = (RK_U32)(~0) >> (11 + 8 - cabac_ctx->m_bitsLeft);
202 h265e_dbg_func("enter\n");
203
204 cabac_ctx->m_bitsLeft -= 8;
205 cabac_ctx->m_low &= low_mask;
206
207 if (leadByte == 0xff) {
208 cabac_ctx->m_numBufferedBytes++;
209 } else {
210 RK_U32 numBufferedBytes = cabac_ctx->m_numBufferedBytes;
211 if (numBufferedBytes > 0) {
212 RK_U32 carry = leadByte >> 8;
213 RK_U32 byteTowrite = cabac_ctx->m_bufferedByte + carry;
214 mpp_writer_put_bits(s, byteTowrite, 8);
215 h265e_dbg_skip("byteTowrite = %x", byteTowrite);
216 byteTowrite = (0xff + carry) & 0xff;
217 while (numBufferedBytes > 1) {
218 h265e_dbg_skip("byteTowrite = %x", byteTowrite);
219 mpp_writer_put_bits(s, byteTowrite, 8);
220 numBufferedBytes--;
221 }
222 }
223 cabac_ctx->m_numBufferedBytes = 1;
224 cabac_ctx->m_bufferedByte = (uint8_t)leadByte;
225 }
226
227 h265e_dbg_func("leave\n");
228 }
229
230 /**
231 * \brief Encode bin
232 *
233 * \param binValue bin value
234 * \param rcCtxModel context model
235 */
h265e_cabac_encodeBin(H265eCabacCtx * cabac_ctx,H265eContextModel_t * ctxModel,RK_U32 binValue)236 void h265e_cabac_encodeBin(H265eCabacCtx *cabac_ctx, H265eContextModel_t *ctxModel, RK_U32 binValue)
237 {
238 RK_U32 mstate = ctxModel->m_state;
239
240 h265e_dbg_func("enter\n");
241
242 // mpp_log("before m_low %d ctxModel.m_state %d m_range %d \n", cabac_ctx->m_low, ctxModel->m_state,cabac_ctx->m_range);
243 ctxModel->m_state = sbacNext(mstate, binValue);
244
245 ctxModel->bBinsCoded = 1;
246 RK_U32 range = cabac_ctx->m_range;
247 RK_U32 state = sbacGetState(mstate);
248 RK_U32 lps = g_lpsTable[state][((uint8_t)range >> 6)];
249 range -= lps;
250
251 // mpp_log("ctxModel.m_state %d binValue %d \n", ctxModel->m_state, binValue);
252 // assert(lps >= 2);
253
254 RK_S32 numBits = (RK_U32)(range - 256) >> 31;
255 RK_U32 low = cabac_ctx->m_low;
256
257 // NOTE: MPS must be LOWEST bit in mstate
258 // assert(((binValue ^ mstate) & 1) == (binValue != sbacGetMps(mstate)));
259 if ((binValue ^ mstate) & 1) {
260 unsigned long idx;
261 CLZ32(idx, lps);
262 // assert(state != 63 || idx == 1);
263
264 numBits = 8 - idx;
265 if (state >= 63)
266 numBits = 6;
267 // assert(numBits <= 6);
268
269 low += range;
270 range = lps;
271 }
272 cabac_ctx->m_low = (low << numBits);
273 cabac_ctx->m_range = (range << numBits);
274 cabac_ctx->m_bitsLeft += numBits;
275
276 // mpp_log("after m_low %d ctxModel.m_state %d m_range %d \n", cabac_ctx->m_low, ctxModel->m_state,cabac_ctx->m_range);
277 if (cabac_ctx->m_bitsLeft >= 0) {
278 h265e_cabac_writeOut(cabac_ctx);
279 }
280
281 h265e_dbg_func("leave\n");
282 }
283
h265e_cabac_encodeBinTrm(H265eCabacCtx * cabac_ctx,RK_U32 binValue)284 void h265e_cabac_encodeBinTrm(H265eCabacCtx *cabac_ctx, RK_U32 binValue)
285 {
286 h265e_dbg_func("enter\n");
287
288 // mpp_log("encodeBinTrm m_range %d binValue %d \n", cabac_ctx->m_range, binValue);
289 cabac_ctx->m_range -= 2;
290 if (binValue) {
291 cabac_ctx->m_low += cabac_ctx->m_range;
292 cabac_ctx->m_low <<= 7;
293 cabac_ctx->m_range = 2 << 7;
294 cabac_ctx->m_bitsLeft += 7;
295 } else if (cabac_ctx->m_range >= 256) {
296 return;
297 } else {
298 cabac_ctx->m_low <<= 1;
299 cabac_ctx->m_range <<= 1;
300 cabac_ctx->m_bitsLeft++;
301 }
302
303 if (cabac_ctx->m_bitsLeft >= 0) {
304 h265e_cabac_writeOut(cabac_ctx);
305 }
306
307 h265e_dbg_func("leave\n");
308 }
309
h265e_cabac_finish(H265eCabacCtx * cabac_ctx)310 void h265e_cabac_finish(H265eCabacCtx *cabac_ctx)
311 {
312 MppWriteCtx* s = cabac_ctx->m_bitIf;
313
314 h265e_dbg_func("enter\n");
315
316 if (cabac_ctx->m_low >> (21 + cabac_ctx->m_bitsLeft)) {
317
318 mpp_writer_put_bits(s, cabac_ctx->m_bufferedByte + 1, 8);
319 while (cabac_ctx->m_numBufferedBytes > 1) {
320 mpp_writer_put_bits(s, 0, 8);
321 cabac_ctx->m_numBufferedBytes--;
322 }
323
324 cabac_ctx->m_low -= 1 << (21 + cabac_ctx->m_bitsLeft);
325 } else {
326 if (cabac_ctx->m_numBufferedBytes > 0) {
327
328 mpp_writer_put_bits(s, cabac_ctx->m_bufferedByte , 8);
329 }
330 while (cabac_ctx->m_numBufferedBytes > 1) {
331 mpp_writer_put_bits(s, 0xff , 8);
332 cabac_ctx->m_numBufferedBytes--;
333 }
334 }
335 mpp_writer_put_bits(s, cabac_ctx->m_low >> 8 , 13 + cabac_ctx->m_bitsLeft);
336
337 h265e_dbg_func("leave\n");
338 }
339
h265e_cabac_flush(H265eCabacCtx * cabac_ctx)340 void h265e_cabac_flush(H265eCabacCtx *cabac_ctx)
341 {
342 MppWriteCtx* s = cabac_ctx->m_bitIf;
343
344 h265e_dbg_func("enter\n");
345
346 h265e_cabac_encodeBinTrm(cabac_ctx, 1);
347 h265e_cabac_finish(cabac_ctx);
348 mpp_writer_put_bits(s, 1, 1);
349 mpp_writer_align_zero(s);
350 h265e_cabac_start(cabac_ctx);
351
352 h265e_dbg_func("leave\n");
353 }
354