Merge pull request #218 from saper/build-fixes
[envytools.git] / vstream / h264_cabac.h
blob908332906126b0da393a62d3da8181b7ea48fec5
1 /*
2 * Copyright (C) 2011 Marcelina Koƛcielnicka <mwk@0x04.net>
3 * All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
25 #ifndef H264_CABAC_H
26 #define H264_CABAC_H
28 #include "h264.h"
29 #include <inttypes.h>
31 struct h264_cabac_ctx_init {
32 int8_t m;
33 int8_t n;
36 enum h264_cabac_ctxidx {
37 H264_CABAC_CTXIDX_MB_TYPE_SI_PRE = 0,
38 H264_CABAC_CTXIDX_MB_TYPE_I = 3, /* also SI suffix */
40 H264_CABAC_CTXIDX_MB_SKIP_FLAG_P = 11, /* and SP */
41 H264_CABAC_CTXIDX_MB_TYPE_P_PRE = 14,
42 H264_CABAC_CTXIDX_MB_TYPE_P_SUF = 17,
43 H264_CABAC_CTXIDX_SUB_MB_TYPE_P = 21,
45 H264_CABAC_CTXIDX_MB_SKIP_FLAG_B = 24,
46 H264_CABAC_CTXIDX_MB_TYPE_B_PRE = 27,
47 H264_CABAC_CTXIDX_MB_TYPE_B_SUF = 32,
48 H264_CABAC_CTXIDX_SUB_MB_TYPE_B = 36,
50 H264_CABAC_CTXIDX_MVD_X = 40,
51 H264_CABAC_CTXIDX_MVD_Y = 47,
53 H264_CABAC_CTXIDX_REF_IDX = 54,
55 H264_CABAC_CTXIDX_MB_QP_DELTA = 60,
56 H264_CABAC_CTXIDX_INTRA_CHROMA_PRED_MODE = 64,
57 H264_CABAC_CTXIDX_PREV_INTRA_PRED_MODE_FLAG = 68,
58 H264_CABAC_CTXIDX_REM_INTRA_PRED_MODE = 69,
60 H264_CABAC_CTXIDX_MB_FIELD_DECODING_FLAG = 70,
61 H264_CABAC_CTXIDX_CODED_BLOCK_PATTERN_LUMA = 73,
62 H264_CABAC_CTXIDX_CODED_BLOCK_PATTERN_CHROMA = 77,
64 H264_CABAC_CTXIDX_CODED_BLOCK_FLAG_CAT0 = 85,
65 H264_CABAC_CTXIDX_CODED_BLOCK_FLAG_CAT1 = 89,
66 H264_CABAC_CTXIDX_CODED_BLOCK_FLAG_CAT2 = 93,
67 H264_CABAC_CTXIDX_CODED_BLOCK_FLAG_CAT3 = 97,
68 H264_CABAC_CTXIDX_CODED_BLOCK_FLAG_CAT4 = 101,
70 H264_CABAC_CTXIDX_SIGNIFICANT_COEFF_FLAG_FRAME_CAT0 = 105,
71 H264_CABAC_CTXIDX_SIGNIFICANT_COEFF_FLAG_FRAME_CAT1 = 120,
72 H264_CABAC_CTXIDX_SIGNIFICANT_COEFF_FLAG_FRAME_CAT2 = 134,
73 H264_CABAC_CTXIDX_SIGNIFICANT_COEFF_FLAG_FRAME_CAT3 = 149,
74 H264_CABAC_CTXIDX_SIGNIFICANT_COEFF_FLAG_FRAME_CAT4 = 152,
76 H264_CABAC_CTXIDX_LAST_SIGNIFICANT_COEFF_FLAG_FRAME_CAT0 = 166,
77 H264_CABAC_CTXIDX_LAST_SIGNIFICANT_COEFF_FLAG_FRAME_CAT1 = 181,
78 H264_CABAC_CTXIDX_LAST_SIGNIFICANT_COEFF_FLAG_FRAME_CAT2 = 195,
79 H264_CABAC_CTXIDX_LAST_SIGNIFICANT_COEFF_FLAG_FRAME_CAT3 = 210,
80 H264_CABAC_CTXIDX_LAST_SIGNIFICANT_COEFF_FLAG_FRAME_CAT4 = 213,
82 H264_CABAC_CTXIDX_COEFF_ABS_LEVEL_MINUS1_PRE_CAT0 = 227,
83 H264_CABAC_CTXIDX_COEFF_ABS_LEVEL_MINUS1_PRE_CAT1 = 237,
84 H264_CABAC_CTXIDX_COEFF_ABS_LEVEL_MINUS1_PRE_CAT2 = 247,
85 H264_CABAC_CTXIDX_COEFF_ABS_LEVEL_MINUS1_PRE_CAT3 = 257,
86 H264_CABAC_CTXIDX_COEFF_ABS_LEVEL_MINUS1_PRE_CAT4 = 266,
88 H264_CABAC_CTXIDX_TERMINATE = 276, /* special */
90 H264_CABAC_CTXIDX_SIGNIFICANT_COEFF_FLAG_FIELD_CAT0 = 277,
91 H264_CABAC_CTXIDX_SIGNIFICANT_COEFF_FLAG_FIELD_CAT1 = 292,
92 H264_CABAC_CTXIDX_SIGNIFICANT_COEFF_FLAG_FIELD_CAT2 = 306,
93 H264_CABAC_CTXIDX_SIGNIFICANT_COEFF_FLAG_FIELD_CAT3 = 321,
94 H264_CABAC_CTXIDX_SIGNIFICANT_COEFF_FLAG_FIELD_CAT4 = 324,
96 H264_CABAC_CTXIDX_LAST_SIGNIFICANT_COEFF_FLAG_FIELD_CAT0 = 338,
97 H264_CABAC_CTXIDX_LAST_SIGNIFICANT_COEFF_FLAG_FIELD_CAT1 = 353,
98 H264_CABAC_CTXIDX_LAST_SIGNIFICANT_COEFF_FLAG_FIELD_CAT2 = 367,
99 H264_CABAC_CTXIDX_LAST_SIGNIFICANT_COEFF_FLAG_FIELD_CAT3 = 382,
100 H264_CABAC_CTXIDX_LAST_SIGNIFICANT_COEFF_FLAG_FIELD_CAT4 = 385,
102 H264_CABAC_CTXIDX_TRANSFORM_SIZE_8X8_FLAG = 399,
104 H264_CABAC_CTXIDX_SIGNIFICANT_COEFF_FLAG_FRAME_CAT5 = 402,
105 H264_CABAC_CTXIDX_LAST_SIGNIFICANT_COEFF_FLAG_FRAME_CAT5 = 417,
106 H264_CABAC_CTXIDX_COEFF_ABS_LEVEL_MINUS1_PRE_CAT5 = 426,
107 H264_CABAC_CTXIDX_SIGNIFICANT_COEFF_FLAG_FIELD_CAT5 = 436,
108 H264_CABAC_CTXIDX_LAST_SIGNIFICANT_COEFF_FLAG_FIELD_CAT5 = 451,
110 H264_CABAC_CTXIDX_CODED_BLOCK_FLAG_CAT6 = 460,
111 H264_CABAC_CTXIDX_CODED_BLOCK_FLAG_CAT7 = 464,
112 H264_CABAC_CTXIDX_CODED_BLOCK_FLAG_CAT8 = 468,
113 H264_CABAC_CTXIDX_CODED_BLOCK_FLAG_CAT10 = 472,
114 H264_CABAC_CTXIDX_CODED_BLOCK_FLAG_CAT11 = 476,
115 H264_CABAC_CTXIDX_CODED_BLOCK_FLAG_CAT12 = 480,
117 H264_CABAC_CTXIDX_SIGNIFICANT_COEFF_FLAG_FRAME_CAT6 = 484,
118 H264_CABAC_CTXIDX_SIGNIFICANT_COEFF_FLAG_FRAME_CAT7 = 499,
119 H264_CABAC_CTXIDX_SIGNIFICANT_COEFF_FLAG_FRAME_CAT8 = 513,
120 H264_CABAC_CTXIDX_SIGNIFICANT_COEFF_FLAG_FRAME_CAT10 = 528,
121 H264_CABAC_CTXIDX_SIGNIFICANT_COEFF_FLAG_FRAME_CAT11 = 543,
122 H264_CABAC_CTXIDX_SIGNIFICANT_COEFF_FLAG_FRAME_CAT12 = 557,
124 H264_CABAC_CTXIDX_LAST_SIGNIFICANT_COEFF_FLAG_FRAME_CAT6 = 572,
125 H264_CABAC_CTXIDX_LAST_SIGNIFICANT_COEFF_FLAG_FRAME_CAT7 = 587,
126 H264_CABAC_CTXIDX_LAST_SIGNIFICANT_COEFF_FLAG_FRAME_CAT8 = 601,
127 H264_CABAC_CTXIDX_LAST_SIGNIFICANT_COEFF_FLAG_FRAME_CAT10 = 616,
128 H264_CABAC_CTXIDX_LAST_SIGNIFICANT_COEFF_FLAG_FRAME_CAT11 = 631,
129 H264_CABAC_CTXIDX_LAST_SIGNIFICANT_COEFF_FLAG_FRAME_CAT12 = 645,
131 H264_CABAC_CTXIDX_SIGNIFICANT_COEFF_FLAG_FRAME_CAT9 = 660,
132 H264_CABAC_CTXIDX_SIGNIFICANT_COEFF_FLAG_FIELD_CAT9 = 675,
133 H264_CABAC_CTXIDX_LAST_SIGNIFICANT_COEFF_FLAG_FRAME_CAT9 = 690,
134 H264_CABAC_CTXIDX_LAST_SIGNIFICANT_COEFF_FLAG_FIELD_CAT9 = 699,
135 H264_CABAC_CTXIDX_COEFF_ABS_LEVEL_MINUS1_PRE_CAT9 = 708,
137 H264_CABAC_CTXIDX_SIGNIFICANT_COEFF_FLAG_FRAME_CAT13 = 718,
138 H264_CABAC_CTXIDX_SIGNIFICANT_COEFF_FLAG_FIELD_CAT13 = 733,
139 H264_CABAC_CTXIDX_LAST_SIGNIFICANT_COEFF_FLAG_FRAME_CAT13 = 748,
140 H264_CABAC_CTXIDX_LAST_SIGNIFICANT_COEFF_FLAG_FIELD_CAT13 = 757,
141 H264_CABAC_CTXIDX_COEFF_ABS_LEVEL_MINUS1_PRE_CAT13 = 766,
143 H264_CABAC_CTXIDX_SIGNIFICANT_COEFF_FLAG_FIELD_CAT6 = 776,
144 H264_CABAC_CTXIDX_SIGNIFICANT_COEFF_FLAG_FIELD_CAT7 = 791,
145 H264_CABAC_CTXIDX_SIGNIFICANT_COEFF_FLAG_FIELD_CAT8 = 805,
146 H264_CABAC_CTXIDX_SIGNIFICANT_COEFF_FLAG_FIELD_CAT10 = 820,
147 H264_CABAC_CTXIDX_SIGNIFICANT_COEFF_FLAG_FIELD_CAT11 = 835,
148 H264_CABAC_CTXIDX_SIGNIFICANT_COEFF_FLAG_FIELD_CAT12 = 849,
150 H264_CABAC_CTXIDX_LAST_SIGNIFICANT_COEFF_FLAG_FIELD_CAT6 = 864,
151 H264_CABAC_CTXIDX_LAST_SIGNIFICANT_COEFF_FLAG_FIELD_CAT7 = 879,
152 H264_CABAC_CTXIDX_LAST_SIGNIFICANT_COEFF_FLAG_FIELD_CAT8 = 893,
153 H264_CABAC_CTXIDX_LAST_SIGNIFICANT_COEFF_FLAG_FIELD_CAT10 = 908,
154 H264_CABAC_CTXIDX_LAST_SIGNIFICANT_COEFF_FLAG_FIELD_CAT11 = 923,
155 H264_CABAC_CTXIDX_LAST_SIGNIFICANT_COEFF_FLAG_FIELD_CAT12 = 937,
157 H264_CABAC_CTXIDX_COEFF_ABS_LEVEL_MINUS1_PRE_CAT6 = 952,
158 H264_CABAC_CTXIDX_COEFF_ABS_LEVEL_MINUS1_PRE_CAT7 = 962,
159 H264_CABAC_CTXIDX_COEFF_ABS_LEVEL_MINUS1_PRE_CAT8 = 972,
160 H264_CABAC_CTXIDX_COEFF_ABS_LEVEL_MINUS1_PRE_CAT10 = 982,
161 H264_CABAC_CTXIDX_COEFF_ABS_LEVEL_MINUS1_PRE_CAT11 = 992,
162 H264_CABAC_CTXIDX_COEFF_ABS_LEVEL_MINUS1_PRE_CAT12 = 1002,
164 H264_CABAC_CTXIDX_CODED_BLOCK_FLAG_CAT5 = 1012,
165 H264_CABAC_CTXIDX_CODED_BLOCK_FLAG_CAT9 = 1016,
166 H264_CABAC_CTXIDX_CODED_BLOCK_FLAG_CAT13 = 1020,
168 /* SVC */
169 H264_CABAC_CTXIDX_BASE_MODE_FLAG = 1024,
170 H264_CABAC_CTXIDX_MOTION_PREDICTION_FLAG_L0 = 1027,
171 H264_CABAC_CTXIDX_MOTION_PREDICTION_FLAG_L1 = 1028,
172 H264_CABAC_CTXIDX_RESIDUAL_PREDICTION_FLAG = 1029,
174 H264_CABAC_CTXIDX_NUM = 1031,
177 struct h264_cabac_context {
178 struct h264_slice *slice;
179 uint8_t pStateIdx[H264_CABAC_CTXIDX_NUM];
180 uint8_t valMPS[H264_CABAC_CTXIDX_NUM];
181 uint32_t codIOffset; /* and codILow */
182 uint32_t codIRange;
183 int firstBitFlag;
184 int bitsOutstanding;
185 int BinCount;
188 struct h264_cabac_se_val {
189 int val;
190 const struct h264_cabac_se_val *subtab;
191 int blen;
192 struct h264_cabac_se_bit {
193 int bidx;
194 int val;
195 } bits[8];
198 struct h264_cabac_context *h264_cabac_new(struct h264_slice *slice);
199 int h264_cabac_init_arith(struct bitstream *str, struct h264_cabac_context *cabac);
200 int h264_cabac_renorm(struct bitstream *str, struct h264_cabac_context *cabac);
201 int h264_cabac_decision(struct bitstream *str, struct h264_cabac_context *cabac, int ctxIdx, uint32_t *binVal);
202 int h264_cabac_bypass(struct bitstream *str, struct h264_cabac_context *cabac, uint32_t *binVal);
203 int h264_cabac_terminate(struct bitstream *str, struct h264_cabac_context *cabac, uint32_t *binVal);
204 int h264_cabac_se(struct bitstream *str, struct h264_cabac_context *cabac, const struct h264_cabac_se_val *tab, int *ctxIdx, uint32_t *val);
205 int h264_cabac_tu(struct bitstream *str, struct h264_cabac_context *cabac, int *ctxIdx, int numidx, uint32_t cMax, uint32_t *val);
206 int h264_cabac_ueg(struct bitstream *str, struct h264_cabac_context *cabac, int *ctxIdx, int numidx, int k, int sign, uint32_t uCoff, int32_t *val);
207 void h264_cabac_destroy(struct h264_cabac_context *cabac);
209 #endif