Mercurial > cgi-bin > hgwebdir.cgi > PR > Applications > VSs > VSs__H264__App
view libavcodec/h264_entropy.c @ 3:0b056460c67d
changed code to use VSs
| author | Nina Engelhardt <nengel@mailbox.tu-berlin.de> |
|---|---|
| date | Mon, 29 Oct 2012 16:44:27 +0100 |
| parents | |
| children |
line source
1 /*
2 * H.26L/H.264/AVC/JVT/14496-10/... cabac decoding
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
22 /**
23 * @file
24 * H.264 / AVC / MPEG4 part10 cabac decoding.
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
28 #include "avcodec.h"
29 #include "h264_types.h"
30 #include "h264_data.h"
31 #include "cabac.h"
32 #include "rectangle.h"
33 #include "h264_misc.h"
35 // #undef NDEBUG
36 #include <assert.h>
38 /* Cabac pre state table */
40 static const int8_t cabac_context_init_I[460][2] =
41 {
42 /* 0 - 10 */
43 { 20, -15 }, { 2, 54 }, { 3, 74 }, { 20, -15 },
44 { 2, 54 }, { 3, 74 }, { -28,127 }, { -23, 104 },
45 { -6, 53 }, { -1, 54 }, { 7, 51 },
47 /* 11 - 23 unsused for I */
48 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
49 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
50 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
51 { 0, 0 },
53 /* 24- 39 */
54 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
55 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
56 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
57 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
59 /* 40 - 53 */
60 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
61 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
62 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
63 { 0, 0 }, { 0, 0 },
65 /* 54 - 59 */
66 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
67 { 0, 0 }, { 0, 0 },
69 /* 60 - 69 */
70 { 0, 41 }, { 0, 63 }, { 0, 63 }, { 0, 63 },
71 { -9, 83 }, { 4, 86 }, { 0, 97 }, { -7, 72 },
72 { 13, 41 }, { 3, 62 },
74 /* 70 -> 87 */
75 { 0, 11 }, { 1, 55 }, { 0, 69 }, { -17, 127 },
76 { -13, 102 },{ 0, 82 }, { -7, 74 }, { -21, 107 },
77 { -27, 127 },{ -31, 127 },{ -24, 127 }, { -18, 95 },
78 { -27, 127 },{ -21, 114 },{ -30, 127 }, { -17, 123 },
79 { -12, 115 },{ -16, 122 },
81 /* 88 -> 104 */
82 { -11, 115 },{ -12, 63 }, { -2, 68 }, { -15, 84 },
83 { -13, 104 },{ -3, 70 }, { -8, 93 }, { -10, 90 },
84 { -30, 127 },{ -1, 74 }, { -6, 97 }, { -7, 91 },
85 { -20, 127 },{ -4, 56 }, { -5, 82 }, { -7, 76 },
86 { -22, 125 },
88 /* 105 -> 135 */
89 { -7, 93 }, { -11, 87 }, { -3, 77 }, { -5, 71 },
90 { -4, 63 }, { -4, 68 }, { -12, 84 }, { -7, 62 },
91 { -7, 65 }, { 8, 61 }, { 5, 56 }, { -2, 66 },
92 { 1, 64 }, { 0, 61 }, { -2, 78 }, { 1, 50 },
93 { 7, 52 }, { 10, 35 }, { 0, 44 }, { 11, 38 },
94 { 1, 45 }, { 0, 46 }, { 5, 44 }, { 31, 17 },
95 { 1, 51 }, { 7, 50 }, { 28, 19 }, { 16, 33 },
96 { 14, 62 }, { -13, 108 },{ -15, 100 },
98 /* 136 -> 165 */
99 { -13, 101 },{ -13, 91 }, { -12, 94 }, { -10, 88 },
100 { -16, 84 }, { -10, 86 }, { -7, 83 }, { -13, 87 },
101 { -19, 94 }, { 1, 70 }, { 0, 72 }, { -5, 74 },
102 { 18, 59 }, { -8, 102 }, { -15, 100 }, { 0, 95 },
103 { -4, 75 }, { 2, 72 }, { -11, 75 }, { -3, 71 },
104 { 15, 46 }, { -13, 69 }, { 0, 62 }, { 0, 65 },
105 { 21, 37 }, { -15, 72 }, { 9, 57 }, { 16, 54 },
106 { 0, 62 }, { 12, 72 },
108 /* 166 -> 196 */
109 { 24, 0 }, { 15, 9 }, { 8, 25 }, { 13, 18 },
110 { 15, 9 }, { 13, 19 }, { 10, 37 }, { 12, 18 },
111 { 6, 29 }, { 20, 33 }, { 15, 30 }, { 4, 45 },
112 { 1, 58 }, { 0, 62 }, { 7, 61 }, { 12, 38 },
113 { 11, 45 }, { 15, 39 }, { 11, 42 }, { 13, 44 },
114 { 16, 45 }, { 12, 41 }, { 10, 49 }, { 30, 34 },
115 { 18, 42 }, { 10, 55 }, { 17, 51 }, { 17, 46 },
116 { 0, 89 }, { 26, -19 }, { 22, -17 },
118 /* 197 -> 226 */
119 { 26, -17 }, { 30, -25 }, { 28, -20 }, { 33, -23 },
120 { 37, -27 }, { 33, -23 }, { 40, -28 }, { 38, -17 },
121 { 33, -11 }, { 40, -15 }, { 41, -6 }, { 38, 1 },
122 { 41, 17 }, { 30, -6 }, { 27, 3 }, { 26, 22 },
123 { 37, -16 }, { 35, -4 }, { 38, -8 }, { 38, -3 },
124 { 37, 3 }, { 38, 5 }, { 42, 0 }, { 35, 16 },
125 { 39, 22 }, { 14, 48 }, { 27, 37 }, { 21, 60 },
126 { 12, 68 }, { 2, 97 },
128 /* 227 -> 251 */
129 { -3, 71 }, { -6, 42 }, { -5, 50 }, { -3, 54 },
130 { -2, 62 }, { 0, 58 }, { 1, 63 }, { -2, 72 },
131 { -1, 74 }, { -9, 91 }, { -5, 67 }, { -5, 27 },
132 { -3, 39 }, { -2, 44 }, { 0, 46 }, { -16, 64 },
133 { -8, 68 }, { -10, 78 }, { -6, 77 }, { -10, 86 },
134 { -12, 92 }, { -15, 55 }, { -10, 60 }, { -6, 62 },
135 { -4, 65 },
137 /* 252 -> 275 */
138 { -12, 73 }, { -8, 76 }, { -7, 80 }, { -9, 88 },
139 { -17, 110 },{ -11, 97 }, { -20, 84 }, { -11, 79 },
140 { -6, 73 }, { -4, 74 }, { -13, 86 }, { -13, 96 },
141 { -11, 97 }, { -19, 117 },{ -8, 78 }, { -5, 33 },
142 { -4, 48 }, { -2, 53 }, { -3, 62 }, { -13, 71 },
143 { -10, 79 }, { -12, 86 }, { -13, 90 }, { -14, 97 },
145 /* 276 a bit special (not used, bypass is used instead) */
146 { 0, 0 },
148 /* 277 -> 307 */
149 { -6, 93 }, { -6, 84 }, { -8, 79 }, { 0, 66 },
150 { -1, 71 }, { 0, 62 }, { -2, 60 }, { -2, 59 },
151 { -5, 75 }, { -3, 62 }, { -4, 58 }, { -9, 66 },
152 { -1, 79 }, { 0, 71 }, { 3, 68 }, { 10, 44 },
153 { -7, 62 }, { 15, 36 }, { 14, 40 }, { 16, 27 },
154 { 12, 29 }, { 1, 44 }, { 20, 36 }, { 18, 32 },
155 { 5, 42 }, { 1, 48 }, { 10, 62 }, { 17, 46 },
156 { 9, 64 }, { -12, 104 },{ -11, 97 },
158 /* 308 -> 337 */
159 { -16, 96 }, { -7, 88 }, { -8, 85 }, { -7, 85 },
160 { -9, 85 }, { -13, 88 }, { 4, 66 }, { -3, 77 },
161 { -3, 76 }, { -6, 76 }, { 10, 58 }, { -1, 76 },
162 { -1, 83 }, { -7, 99 }, { -14, 95 }, { 2, 95 },
163 { 0, 76 }, { -5, 74 }, { 0, 70 }, { -11, 75 },
164 { 1, 68 }, { 0, 65 }, { -14, 73 }, { 3, 62 },
165 { 4, 62 }, { -1, 68 }, { -13, 75 }, { 11, 55 },
166 { 5, 64 }, { 12, 70 },
168 /* 338 -> 368 */
169 { 15, 6 }, { 6, 19 }, { 7, 16 }, { 12, 14 },
170 { 18, 13 }, { 13, 11 }, { 13, 15 }, { 15, 16 },
171 { 12, 23 }, { 13, 23 }, { 15, 20 }, { 14, 26 },
172 { 14, 44 }, { 17, 40 }, { 17, 47 }, { 24, 17 },
173 { 21, 21 }, { 25, 22 }, { 31, 27 }, { 22, 29 },
174 { 19, 35 }, { 14, 50 }, { 10, 57 }, { 7, 63 },
175 { -2, 77 }, { -4, 82 }, { -3, 94 }, { 9, 69 },
176 { -12, 109 },{ 36, -35 }, { 36, -34 },
178 /* 369 -> 398 */
179 { 32, -26 }, { 37, -30 }, { 44, -32 }, { 34, -18 },
180 { 34, -15 }, { 40, -15 }, { 33, -7 }, { 35, -5 },
181 { 33, 0 }, { 38, 2 }, { 33, 13 }, { 23, 35 },
182 { 13, 58 }, { 29, -3 }, { 26, 0 }, { 22, 30 },
183 { 31, -7 }, { 35, -15 }, { 34, -3 }, { 34, 3 },
184 { 36, -1 }, { 34, 5 }, { 32, 11 }, { 35, 5 },
185 { 34, 12 }, { 39, 11 }, { 30, 29 }, { 34, 26 },
186 { 29, 39 }, { 19, 66 },
188 /* 399 -> 435 */
189 { 31, 21 }, { 31, 31 }, { 25, 50 },
190 { -17, 120 }, { -20, 112 }, { -18, 114 }, { -11, 85 },
191 { -15, 92 }, { -14, 89 }, { -26, 71 }, { -15, 81 },
192 { -14, 80 }, { 0, 68 }, { -14, 70 }, { -24, 56 },
193 { -23, 68 }, { -24, 50 }, { -11, 74 }, { 23, -13 },
194 { 26, -13 }, { 40, -15 }, { 49, -14 }, { 44, 3 },
195 { 45, 6 }, { 44, 34 }, { 33, 54 }, { 19, 82 },
196 { -3, 75 }, { -1, 23 }, { 1, 34 }, { 1, 43 },
197 { 0, 54 }, { -2, 55 }, { 0, 61 }, { 1, 64 },
198 { 0, 68 }, { -9, 92 },
200 /* 436 -> 459 */
201 { -14, 106 }, { -13, 97 }, { -15, 90 }, { -12, 90 },
202 { -18, 88 }, { -10, 73 }, { -9, 79 }, { -14, 86 },
203 { -10, 73 }, { -10, 70 }, { -10, 69 }, { -5, 66 },
204 { -9, 64 }, { -5, 58 }, { 2, 59 }, { 21, -10 },
205 { 24, -11 }, { 28, -8 }, { 28, -1 }, { 29, 3 },
206 { 29, 9 }, { 35, 20 }, { 29, 36 }, { 14, 67 }
207 };
209 static const int8_t cabac_context_init_PB[3][460][2] =
210 {
211 /* i_cabac_init_idc == 0 */
212 {
213 /* 0 - 10 */
214 { 20, -15 }, { 2, 54 }, { 3, 74 }, { 20, -15 },
215 { 2, 54 }, { 3, 74 }, { -28, 127 }, { -23, 104 },
216 { -6, 53 }, { -1, 54 }, { 7, 51 },
218 /* 11 - 23 */
219 { 23, 33 }, { 23, 2 }, { 21, 0 }, { 1, 9 },
220 { 0, 49 }, { -37, 118 }, { 5, 57 }, { -13, 78 },
221 { -11, 65 }, { 1, 62 }, { 12, 49 }, { -4, 73 },
222 { 17, 50 },
224 /* 24 - 39 */
225 { 18, 64 }, { 9, 43 }, { 29, 0 }, { 26, 67 },
226 { 16, 90 }, { 9, 104 }, { -46, 127 }, { -20, 104 },
227 { 1, 67 }, { -13, 78 }, { -11, 65 }, { 1, 62 },
228 { -6, 86 }, { -17, 95 }, { -6, 61 }, { 9, 45 },
230 /* 40 - 53 */
231 { -3, 69 }, { -6, 81 }, { -11, 96 }, { 6, 55 },
232 { 7, 67 }, { -5, 86 }, { 2, 88 }, { 0, 58 },
233 { -3, 76 }, { -10, 94 }, { 5, 54 }, { 4, 69 },
234 { -3, 81 }, { 0, 88 },
236 /* 54 - 59 */
237 { -7, 67 }, { -5, 74 }, { -4, 74 }, { -5, 80 },
238 { -7, 72 }, { 1, 58 },
240 /* 60 - 69 */
241 { 0, 41 }, { 0, 63 }, { 0, 63 }, { 0, 63 },
242 { -9, 83 }, { 4, 86 }, { 0, 97 }, { -7, 72 },
243 { 13, 41 }, { 3, 62 },
245 /* 70 - 87 */
246 { 0, 45 }, { -4, 78 }, { -3, 96 }, { -27, 126 },
247 { -28, 98 }, { -25, 101 }, { -23, 67 }, { -28, 82 },
248 { -20, 94 }, { -16, 83 }, { -22, 110 }, { -21, 91 },
249 { -18, 102 }, { -13, 93 }, { -29, 127 }, { -7, 92 },
250 { -5, 89 }, { -7, 96 }, { -13, 108 }, { -3, 46 },
251 { -1, 65 }, { -1, 57 }, { -9, 93 }, { -3, 74 },
252 { -9, 92 }, { -8, 87 }, { -23, 126 }, { 5, 54 },
253 { 6, 60 }, { 6, 59 }, { 6, 69 }, { -1, 48 },
254 { 0, 68 }, { -4, 69 }, { -8, 88 },
256 /* 105 -> 165 */
257 { -2, 85 }, { -6, 78 }, { -1, 75 }, { -7, 77 },
258 { 2, 54 }, { 5, 50 }, { -3, 68 }, { 1, 50 },
259 { 6, 42 }, { -4, 81 }, { 1, 63 }, { -4, 70 },
260 { 0, 67 }, { 2, 57 }, { -2, 76 }, { 11, 35 },
261 { 4, 64 }, { 1, 61 }, { 11, 35 }, { 18, 25 },
262 { 12, 24 }, { 13, 29 }, { 13, 36 }, { -10, 93 },
263 { -7, 73 }, { -2, 73 }, { 13, 46 }, { 9, 49 },
264 { -7, 100 }, { 9, 53 }, { 2, 53 }, { 5, 53 },
265 { -2, 61 }, { 0, 56 }, { 0, 56 }, { -13, 63 },
266 { -5, 60 }, { -1, 62 }, { 4, 57 }, { -6, 69 },
267 { 4, 57 }, { 14, 39 }, { 4, 51 }, { 13, 68 },
268 { 3, 64 }, { 1, 61 }, { 9, 63 }, { 7, 50 },
269 { 16, 39 }, { 5, 44 }, { 4, 52 }, { 11, 48 },
270 { -5, 60 }, { -1, 59 }, { 0, 59 }, { 22, 33 },
271 { 5, 44 }, { 14, 43 }, { -1, 78 }, { 0, 60 },
272 { 9, 69 },
274 /* 166 - 226 */
275 { 11, 28 }, { 2, 40 }, { 3, 44 }, { 0, 49 },
276 { 0, 46 }, { 2, 44 }, { 2, 51 }, { 0, 47 },
277 { 4, 39 }, { 2, 62 }, { 6, 46 }, { 0, 54 },
278 { 3, 54 }, { 2, 58 }, { 4, 63 }, { 6, 51 },
279 { 6, 57 }, { 7, 53 }, { 6, 52 }, { 6, 55 },
280 { 11, 45 }, { 14, 36 }, { 8, 53 }, { -1, 82 },
281 { 7, 55 }, { -3, 78 }, { 15, 46 }, { 22, 31 },
282 { -1, 84 }, { 25, 7 }, { 30, -7 }, { 28, 3 },
283 { 28, 4 }, { 32, 0 }, { 34, -1 }, { 30, 6 },
284 { 30, 6 }, { 32, 9 }, { 31, 19 }, { 26, 27 },
285 { 26, 30 }, { 37, 20 }, { 28, 34 }, { 17, 70 },
286 { 1, 67 }, { 5, 59 }, { 9, 67 }, { 16, 30 },
287 { 18, 32 }, { 18, 35 }, { 22, 29 }, { 24, 31 },
288 { 23, 38 }, { 18, 43 }, { 20, 41 }, { 11, 63 },
289 { 9, 59 }, { 9, 64 }, { -1, 94 }, { -2, 89 },
290 { -9, 108 },
292 /* 227 - 275 */
293 { -6, 76 }, { -2, 44 }, { 0, 45 }, { 0, 52 },
294 { -3, 64 }, { -2, 59 }, { -4, 70 }, { -4, 75 },
295 { -8, 82 }, { -17, 102 }, { -9, 77 }, { 3, 24 },
296 { 0, 42 }, { 0, 48 }, { 0, 55 }, { -6, 59 },
297 { -7, 71 }, { -12, 83 }, { -11, 87 }, { -30, 119 },
298 { 1, 58 }, { -3, 29 }, { -1, 36 }, { 1, 38 },
299 { 2, 43 }, { -6, 55 }, { 0, 58 }, { 0, 64 },
300 { -3, 74 }, { -10, 90 }, { 0, 70 }, { -4, 29 },
301 { 5, 31 }, { 7, 42 }, { 1, 59 }, { -2, 58 },
302 { -3, 72 }, { -3, 81 }, { -11, 97 }, { 0, 58 },
303 { 8, 5 }, { 10, 14 }, { 14, 18 }, { 13, 27 },
304 { 2, 40 }, { 0, 58 }, { -3, 70 }, { -6, 79 },
305 { -8, 85 },
307 /* 276 a bit special (not used, bypass is used instead) */
308 { 0, 0 },
310 /* 277 - 337 */
311 { -13, 106 }, { -16, 106 }, { -10, 87 }, { -21, 114 },
312 { -18, 110 }, { -14, 98 }, { -22, 110 }, { -21, 106 },
313 { -18, 103 }, { -21, 107 }, { -23, 108 }, { -26, 112 },
314 { -10, 96 }, { -12, 95 }, { -5, 91 }, { -9, 93 },
315 { -22, 94 }, { -5, 86 }, { 9, 67 }, { -4, 80 },
316 { -10, 85 }, { -1, 70 }, { 7, 60 }, { 9, 58 },
317 { 5, 61 }, { 12, 50 }, { 15, 50 }, { 18, 49 },
318 { 17, 54 }, { 10, 41 }, { 7, 46 }, { -1, 51 },
319 { 7, 49 }, { 8, 52 }, { 9, 41 }, { 6, 47 },
320 { 2, 55 }, { 13, 41 }, { 10, 44 }, { 6, 50 },
321 { 5, 53 }, { 13, 49 }, { 4, 63 }, { 6, 64 },
322 { -2, 69 }, { -2, 59 }, { 6, 70 }, { 10, 44 },
323 { 9, 31 }, { 12, 43 }, { 3, 53 }, { 14, 34 },
324 { 10, 38 }, { -3, 52 }, { 13, 40 }, { 17, 32 },
325 { 7, 44 }, { 7, 38 }, { 13, 50 }, { 10, 57 },
326 { 26, 43 },
328 /* 338 - 398 */
329 { 14, 11 }, { 11, 14 }, { 9, 11 }, { 18, 11 },
330 { 21, 9 }, { 23, -2 }, { 32, -15 }, { 32, -15 },
331 { 34, -21 }, { 39, -23 }, { 42, -33 }, { 41, -31 },
332 { 46, -28 }, { 38, -12 }, { 21, 29 }, { 45, -24 },
333 { 53, -45 }, { 48, -26 }, { 65, -43 }, { 43, -19 },
334 { 39, -10 }, { 30, 9 }, { 18, 26 }, { 20, 27 },
335 { 0, 57 }, { -14, 82 }, { -5, 75 }, { -19, 97 },
336 { -35, 125 }, { 27, 0 }, { 28, 0 }, { 31, -4 },
337 { 27, 6 }, { 34, 8 }, { 30, 10 }, { 24, 22 },
338 { 33, 19 }, { 22, 32 }, { 26, 31 }, { 21, 41 },
339 { 26, 44 }, { 23, 47 }, { 16, 65 }, { 14, 71 },
340 { 8, 60 }, { 6, 63 }, { 17, 65 }, { 21, 24 },
341 { 23, 20 }, { 26, 23 }, { 27, 32 }, { 28, 23 },
342 { 28, 24 }, { 23, 40 }, { 24, 32 }, { 28, 29 },
343 { 23, 42 }, { 19, 57 }, { 22, 53 }, { 22, 61 },
344 { 11, 86 },
346 /* 399 - 435 */
347 { 12, 40 }, { 11, 51 }, { 14, 59 },
348 { -4, 79 }, { -7, 71 }, { -5, 69 }, { -9, 70 },
349 { -8, 66 }, { -10, 68 }, { -19, 73 }, { -12, 69 },
350 { -16, 70 }, { -15, 67 }, { -20, 62 }, { -19, 70 },
351 { -16, 66 }, { -22, 65 }, { -20, 63 }, { 9, -2 },
352 { 26, -9 }, { 33, -9 }, { 39, -7 }, { 41, -2 },
353 { 45, 3 }, { 49, 9 }, { 45, 27 }, { 36, 59 },
354 { -6, 66 }, { -7, 35 }, { -7, 42 }, { -8, 45 },
355 { -5, 48 }, { -12, 56 }, { -6, 60 }, { -5, 62 },
356 { -8, 66 }, { -8, 76 },
358 /* 436 - 459 */
359 { -5, 85 }, { -6, 81 }, { -10, 77 }, { -7, 81 },
360 { -17, 80 }, { -18, 73 }, { -4, 74 }, { -10, 83 },
361 { -9, 71 }, { -9, 67 }, { -1, 61 }, { -8, 66 },
362 { -14, 66 }, { 0, 59 }, { 2, 59 }, { 21, -13 },
363 { 33, -14 }, { 39, -7 }, { 46, -2 }, { 51, 2 },
364 { 60, 6 }, { 61, 17 }, { 55, 34 }, { 42, 62 },
365 },
367 /* i_cabac_init_idc == 1 */
368 {
369 /* 0 - 10 */
370 { 20, -15 }, { 2, 54 }, { 3, 74 }, { 20, -15 },
371 { 2, 54 }, { 3, 74 }, { -28, 127 }, { -23, 104 },
372 { -6, 53 }, { -1, 54 }, { 7, 51 },
374 /* 11 - 23 */
375 { 22, 25 }, { 34, 0 }, { 16, 0 }, { -2, 9 },
376 { 4, 41 }, { -29, 118 }, { 2, 65 }, { -6, 71 },
377 { -13, 79 }, { 5, 52 }, { 9, 50 }, { -3, 70 },
378 { 10, 54 },
380 /* 24 - 39 */
381 { 26, 34 }, { 19, 22 }, { 40, 0 }, { 57, 2 },
382 { 41, 36 }, { 26, 69 }, { -45, 127 }, { -15, 101 },
383 { -4, 76 }, { -6, 71 }, { -13, 79 }, { 5, 52 },
384 { 6, 69 }, { -13, 90 }, { 0, 52 }, { 8, 43 },
386 /* 40 - 53 */
387 { -2, 69 },{ -5, 82 },{ -10, 96 },{ 2, 59 },
388 { 2, 75 },{ -3, 87 },{ -3, 100 },{ 1, 56 },
389 { -3, 74 },{ -6, 85 },{ 0, 59 },{ -3, 81 },
390 { -7, 86 },{ -5, 95 },
392 /* 54 - 59 */
393 { -1, 66 },{ -1, 77 },{ 1, 70 },{ -2, 86 },
394 { -5, 72 },{ 0, 61 },
396 /* 60 - 69 */
397 { 0, 41 }, { 0, 63 }, { 0, 63 }, { 0, 63 },
398 { -9, 83 }, { 4, 86 }, { 0, 97 }, { -7, 72 },
399 { 13, 41 }, { 3, 62 },
401 /* 70 - 104 */
402 { 13, 15 }, { 7, 51 }, { 2, 80 }, { -39, 127 },
403 { -18, 91 }, { -17, 96 }, { -26, 81 }, { -35, 98 },
404 { -24, 102 }, { -23, 97 }, { -27, 119 }, { -24, 99 },
405 { -21, 110 }, { -18, 102 }, { -36, 127 }, { 0, 80 },
406 { -5, 89 }, { -7, 94 }, { -4, 92 }, { 0, 39 },
407 { 0, 65 }, { -15, 84 }, { -35, 127 }, { -2, 73 },
408 { -12, 104 }, { -9, 91 }, { -31, 127 }, { 3, 55 },
409 { 7, 56 }, { 7, 55 }, { 8, 61 }, { -3, 53 },
410 { 0, 68 }, { -7, 74 }, { -9, 88 },
412 /* 105 -> 165 */
413 { -13, 103 }, { -13, 91 }, { -9, 89 }, { -14, 92 },
414 { -8, 76 }, { -12, 87 }, { -23, 110 }, { -24, 105 },
415 { -10, 78 }, { -20, 112 }, { -17, 99 }, { -78, 127 },
416 { -70, 127 }, { -50, 127 }, { -46, 127 }, { -4, 66 },
417 { -5, 78 }, { -4, 71 }, { -8, 72 }, { 2, 59 },
418 { -1, 55 }, { -7, 70 }, { -6, 75 }, { -8, 89 },
419 { -34, 119 }, { -3, 75 }, { 32, 20 }, { 30, 22 },
420 { -44, 127 }, { 0, 54 }, { -5, 61 }, { 0, 58 },
421 { -1, 60 }, { -3, 61 }, { -8, 67 }, { -25, 84 },
422 { -14, 74 }, { -5, 65 }, { 5, 52 }, { 2, 57 },
423 { 0, 61 }, { -9, 69 }, { -11, 70 }, { 18, 55 },
424 { -4, 71 }, { 0, 58 }, { 7, 61 }, { 9, 41 },
425 { 18, 25 }, { 9, 32 }, { 5, 43 }, { 9, 47 },
426 { 0, 44 }, { 0, 51 }, { 2, 46 }, { 19, 38 },
427 { -4, 66 }, { 15, 38 }, { 12, 42 }, { 9, 34 },
428 { 0, 89 },
430 /* 166 - 226 */
431 { 4, 45 }, { 10, 28 }, { 10, 31 }, { 33, -11 },
432 { 52, -43 }, { 18, 15 }, { 28, 0 }, { 35, -22 },
433 { 38, -25 }, { 34, 0 }, { 39, -18 }, { 32, -12 },
434 { 102, -94 }, { 0, 0 }, { 56, -15 }, { 33, -4 },
435 { 29, 10 }, { 37, -5 }, { 51, -29 }, { 39, -9 },
436 { 52, -34 }, { 69, -58 }, { 67, -63 }, { 44, -5 },
437 { 32, 7 }, { 55, -29 }, { 32, 1 }, { 0, 0 },
438 { 27, 36 }, { 33, -25 }, { 34, -30 }, { 36, -28 },
439 { 38, -28 }, { 38, -27 }, { 34, -18 }, { 35, -16 },
440 { 34, -14 }, { 32, -8 }, { 37, -6 }, { 35, 0 },
441 { 30, 10 }, { 28, 18 }, { 26, 25 }, { 29, 41 },
442 { 0, 75 }, { 2, 72 }, { 8, 77 }, { 14, 35 },
443 { 18, 31 }, { 17, 35 }, { 21, 30 }, { 17, 45 },
444 { 20, 42 }, { 18, 45 }, { 27, 26 }, { 16, 54 },
445 { 7, 66 }, { 16, 56 }, { 11, 73 }, { 10, 67 },
446 { -10, 116 },
448 /* 227 - 275 */
449 { -23, 112 }, { -15, 71 }, { -7, 61 }, { 0, 53 },
450 { -5, 66 }, { -11, 77 }, { -9, 80 }, { -9, 84 },
451 { -10, 87 }, { -34, 127 }, { -21, 101 }, { -3, 39 },
452 { -5, 53 }, { -7, 61 }, { -11, 75 }, { -15, 77 },
453 { -17, 91 }, { -25, 107 }, { -25, 111 }, { -28, 122 },
454 { -11, 76 }, { -10, 44 }, { -10, 52 }, { -10, 57 },
455 { -9, 58 }, { -16, 72 }, { -7, 69 }, { -4, 69 },
456 { -5, 74 }, { -9, 86 }, { 2, 66 }, { -9, 34 },
457 { 1, 32 }, { 11, 31 }, { 5, 52 }, { -2, 55 },
458 { -2, 67 }, { 0, 73 }, { -8, 89 }, { 3, 52 },
459 { 7, 4 }, { 10, 8 }, { 17, 8 }, { 16, 19 },
460 { 3, 37 }, { -1, 61 }, { -5, 73 }, { -1, 70 },
461 { -4, 78 },
463 /* 276 a bit special (not used, bypass is used instead) */
464 { 0, 0 },
466 /* 277 - 337 */
467 { -21, 126 }, { -23, 124 }, { -20, 110 }, { -26, 126 },
468 { -25, 124 }, { -17, 105 }, { -27, 121 }, { -27, 117 },
469 { -17, 102 }, { -26, 117 }, { -27, 116 }, { -33, 122 },
470 { -10, 95 }, { -14, 100 }, { -8, 95 }, { -17, 111 },
471 { -28, 114 }, { -6, 89 }, { -2, 80 }, { -4, 82 },
472 { -9, 85 }, { -8, 81 }, { -1, 72 }, { 5, 64 },
473 { 1, 67 }, { 9, 56 }, { 0, 69 }, { 1, 69 },
474 { 7, 69 }, { -7, 69 }, { -6, 67 }, { -16, 77 },
475 { -2, 64 }, { 2, 61 }, { -6, 67 }, { -3, 64 },
476 { 2, 57 }, { -3, 65 }, { -3, 66 }, { 0, 62 },
477 { 9, 51 }, { -1, 66 }, { -2, 71 }, { -2, 75 },
478 { -1, 70 }, { -9, 72 }, { 14, 60 }, { 16, 37 },
479 { 0, 47 }, { 18, 35 }, { 11, 37 }, { 12, 41 },
480 { 10, 41 }, { 2, 48 }, { 12, 41 }, { 13, 41 },
481 { 0, 59 }, { 3, 50 }, { 19, 40 }, { 3, 66 },
482 { 18, 50 },
484 /* 338 - 398 */
485 { 19, -6 }, { 18, -6 }, { 14, 0 }, { 26, -12 },
486 { 31, -16 }, { 33, -25 }, { 33, -22 }, { 37, -28 },
487 { 39, -30 }, { 42, -30 }, { 47, -42 }, { 45, -36 },
488 { 49, -34 }, { 41, -17 }, { 32, 9 }, { 69, -71 },
489 { 63, -63 }, { 66, -64 }, { 77, -74 }, { 54, -39 },
490 { 52, -35 }, { 41, -10 }, { 36, 0 }, { 40, -1 },
491 { 30, 14 }, { 28, 26 }, { 23, 37 }, { 12, 55 },
492 { 11, 65 }, { 37, -33 }, { 39, -36 }, { 40, -37 },
493 { 38, -30 }, { 46, -33 }, { 42, -30 }, { 40, -24 },
494 { 49, -29 }, { 38, -12 }, { 40, -10 }, { 38, -3 },
495 { 46, -5 }, { 31, 20 }, { 29, 30 }, { 25, 44 },
496 { 12, 48 }, { 11, 49 }, { 26, 45 }, { 22, 22 },
497 { 23, 22 }, { 27, 21 }, { 33, 20 }, { 26, 28 },
498 { 30, 24 }, { 27, 34 }, { 18, 42 }, { 25, 39 },
499 { 18, 50 }, { 12, 70 }, { 21, 54 }, { 14, 71 },
500 { 11, 83 },
502 /* 399 - 435 */
503 { 25, 32 }, { 21, 49 }, { 21, 54 },
504 { -5, 85 }, { -6, 81 }, { -10, 77 }, { -7, 81 },
505 { -17, 80 }, { -18, 73 }, { -4, 74 }, { -10, 83 },
506 { -9, 71 }, { -9, 67 }, { -1, 61 }, { -8, 66 },
507 { -14, 66 }, { 0, 59 }, { 2, 59 }, { 17, -10 },
508 { 32, -13 }, { 42, -9 }, { 49, -5 }, { 53, 0 },
509 { 64, 3 }, { 68, 10 }, { 66, 27 }, { 47, 57 },
510 { -5, 71 }, { 0, 24 }, { -1, 36 }, { -2, 42 },
511 { -2, 52 }, { -9, 57 }, { -6, 63 }, { -4, 65 },
512 { -4, 67 }, { -7, 82 },
514 /* 436 - 459 */
515 { -3, 81 }, { -3, 76 }, { -7, 72 }, { -6, 78 },
516 { -12, 72 }, { -14, 68 }, { -3, 70 }, { -6, 76 },
517 { -5, 66 }, { -5, 62 }, { 0, 57 }, { -4, 61 },
518 { -9, 60 }, { 1, 54 }, { 2, 58 }, { 17, -10 },
519 { 32, -13 }, { 42, -9 }, { 49, -5 }, { 53, 0 },
520 { 64, 3 }, { 68, 10 }, { 66, 27 }, { 47, 57 },
521 },
523 /* i_cabac_init_idc == 2 */
524 {
525 /* 0 - 10 */
526 { 20, -15 }, { 2, 54 }, { 3, 74 }, { 20, -15 },
527 { 2, 54 }, { 3, 74 }, { -28, 127 }, { -23, 104 },
528 { -6, 53 }, { -1, 54 }, { 7, 51 },
530 /* 11 - 23 */
531 { 29, 16 }, { 25, 0 }, { 14, 0 }, { -10, 51 },
532 { -3, 62 }, { -27, 99 }, { 26, 16 }, { -4, 85 },
533 { -24, 102 }, { 5, 57 }, { 6, 57 }, { -17, 73 },
534 { 14, 57 },
536 /* 24 - 39 */
537 { 20, 40 }, { 20, 10 }, { 29, 0 }, { 54, 0 },
538 { 37, 42 }, { 12, 97 }, { -32, 127 }, { -22, 117 },
539 { -2, 74 }, { -4, 85 }, { -24, 102 }, { 5, 57 },
540 { -6, 93 }, { -14, 88 }, { -6, 44 }, { 4, 55 },
542 /* 40 - 53 */
543 { -11, 89 },{ -15, 103 },{ -21, 116 },{ 19, 57 },
544 { 20, 58 },{ 4, 84 },{ 6, 96 },{ 1, 63 },
545 { -5, 85 },{ -13, 106 },{ 5, 63 },{ 6, 75 },
546 { -3, 90 },{ -1, 101 },
548 /* 54 - 59 */
549 { 3, 55 },{ -4, 79 },{ -2, 75 },{ -12, 97 },
550 { -7, 50 },{ 1, 60 },
552 /* 60 - 69 */
553 { 0, 41 }, { 0, 63 }, { 0, 63 }, { 0, 63 },
554 { -9, 83 }, { 4, 86 }, { 0, 97 }, { -7, 72 },
555 { 13, 41 }, { 3, 62 },
557 /* 70 - 104 */
558 { 7, 34 }, { -9, 88 }, { -20, 127 }, { -36, 127 },
559 { -17, 91 }, { -14, 95 }, { -25, 84 }, { -25, 86 },
560 { -12, 89 }, { -17, 91 }, { -31, 127 }, { -14, 76 },
561 { -18, 103 }, { -13, 90 }, { -37, 127 }, { 11, 80 },
562 { 5, 76 }, { 2, 84 }, { 5, 78 }, { -6, 55 },
563 { 4, 61 }, { -14, 83 }, { -37, 127 }, { -5, 79 },
564 { -11, 104 }, { -11, 91 }, { -30, 127 }, { 0, 65 },
565 { -2, 79 }, { 0, 72 }, { -4, 92 }, { -6, 56 },
566 { 3, 68 }, { -8, 71 }, { -13, 98 },
568 /* 105 -> 165 */
569 { -4, 86 }, { -12, 88 }, { -5, 82 }, { -3, 72 },
570 { -4, 67 }, { -8, 72 }, { -16, 89 }, { -9, 69 },
571 { -1, 59 }, { 5, 66 }, { 4, 57 }, { -4, 71 },
572 { -2, 71 }, { 2, 58 }, { -1, 74 }, { -4, 44 },
573 { -1, 69 }, { 0, 62 }, { -7, 51 }, { -4, 47 },
574 { -6, 42 }, { -3, 41 }, { -6, 53 }, { 8, 76 },
575 { -9, 78 }, { -11, 83 }, { 9, 52 }, { 0, 67 },
576 { -5, 90 }, { 1, 67 }, { -15, 72 }, { -5, 75 },
577 { -8, 80 }, { -21, 83 }, { -21, 64 }, { -13, 31 },
578 { -25, 64 }, { -29, 94 }, { 9, 75 }, { 17, 63 },
579 { -8, 74 }, { -5, 35 }, { -2, 27 }, { 13, 91 },
580 { 3, 65 }, { -7, 69 }, { 8, 77 }, { -10, 66 },
581 { 3, 62 }, { -3, 68 }, { -20, 81 }, { 0, 30 },
582 { 1, 7 }, { -3, 23 }, { -21, 74 }, { 16, 66 },
583 { -23, 124 }, { 17, 37 }, { 44, -18 }, { 50, -34 },
584 { -22, 127 },
586 /* 166 - 226 */
587 { 4, 39 }, { 0, 42 }, { 7, 34 }, { 11, 29 },
588 { 8, 31 }, { 6, 37 }, { 7, 42 }, { 3, 40 },
589 { 8, 33 }, { 13, 43 }, { 13, 36 }, { 4, 47 },
590 { 3, 55 }, { 2, 58 }, { 6, 60 }, { 8, 44 },
591 { 11, 44 }, { 14, 42 }, { 7, 48 }, { 4, 56 },
592 { 4, 52 }, { 13, 37 }, { 9, 49 }, { 19, 58 },
593 { 10, 48 }, { 12, 45 }, { 0, 69 }, { 20, 33 },
594 { 8, 63 }, { 35, -18 }, { 33, -25 }, { 28, -3 },
595 { 24, 10 }, { 27, 0 }, { 34, -14 }, { 52, -44 },
596 { 39, -24 }, { 19, 17 }, { 31, 25 }, { 36, 29 },
597 { 24, 33 }, { 34, 15 }, { 30, 20 }, { 22, 73 },
598 { 20, 34 }, { 19, 31 }, { 27, 44 }, { 19, 16 },
599 { 15, 36 }, { 15, 36 }, { 21, 28 }, { 25, 21 },
600 { 30, 20 }, { 31, 12 }, { 27, 16 }, { 24, 42 },
601 { 0, 93 }, { 14, 56 }, { 15, 57 }, { 26, 38 },
602 { -24, 127 },
604 /* 227 - 275 */
605 { -24, 115 }, { -22, 82 }, { -9, 62 }, { 0, 53 },
606 { 0, 59 }, { -14, 85 }, { -13, 89 }, { -13, 94 },
607 { -11, 92 }, { -29, 127 }, { -21, 100 }, { -14, 57 },
608 { -12, 67 }, { -11, 71 }, { -10, 77 }, { -21, 85 },
609 { -16, 88 }, { -23, 104 }, { -15, 98 }, { -37, 127 },
610 { -10, 82 }, { -8, 48 }, { -8, 61 }, { -8, 66 },
611 { -7, 70 }, { -14, 75 }, { -10, 79 }, { -9, 83 },
612 { -12, 92 }, { -18, 108 }, { -4, 79 }, { -22, 69 },
613 { -16, 75 }, { -2, 58 }, { 1, 58 }, { -13, 78 },
614 { -9, 83 }, { -4, 81 }, { -13, 99 }, { -13, 81 },
615 { -6, 38 }, { -13, 62 }, { -6, 58 }, { -2, 59 },
616 { -16, 73 }, { -10, 76 }, { -13, 86 }, { -9, 83 },
617 { -10, 87 },
619 /* 276 a bit special (not used, bypass is used instead) */
620 { 0, 0 },
622 /* 277 - 337 */
623 { -22, 127 }, { -25, 127 }, { -25, 120 }, { -27, 127 },
624 { -19, 114 }, { -23, 117 }, { -25, 118 }, { -26, 117 },
625 { -24, 113 }, { -28, 118 }, { -31, 120 }, { -37, 124 },
626 { -10, 94 }, { -15, 102 }, { -10, 99 }, { -13, 106 },
627 { -50, 127 }, { -5, 92 }, { 17, 57 }, { -5, 86 },
628 { -13, 94 }, { -12, 91 }, { -2, 77 }, { 0, 71 },
629 { -1, 73 }, { 4, 64 }, { -7, 81 }, { 5, 64 },
630 { 15, 57 }, { 1, 67 }, { 0, 68 }, { -10, 67 },
631 { 1, 68 }, { 0, 77 }, { 2, 64 }, { 0, 68 },
632 { -5, 78 }, { 7, 55 }, { 5, 59 }, { 2, 65 },
633 { 14, 54 }, { 15, 44 }, { 5, 60 }, { 2, 70 },
634 { -2, 76 }, { -18, 86 }, { 12, 70 }, { 5, 64 },
635 { -12, 70 }, { 11, 55 }, { 5, 56 }, { 0, 69 },
636 { 2, 65 }, { -6, 74 }, { 5, 54 }, { 7, 54 },
637 { -6, 76 }, { -11, 82 }, { -2, 77 }, { -2, 77 },
638 { 25, 42 },
640 /* 338 - 398 */
641 { 17, -13 }, { 16, -9 }, { 17, -12 }, { 27, -21 },
642 { 37, -30 }, { 41, -40 }, { 42, -41 }, { 48, -47 },
643 { 39, -32 }, { 46, -40 }, { 52, -51 }, { 46, -41 },
644 { 52, -39 }, { 43, -19 }, { 32, 11 }, { 61, -55 },
645 { 56, -46 }, { 62, -50 }, { 81, -67 }, { 45, -20 },
646 { 35, -2 }, { 28, 15 }, { 34, 1 }, { 39, 1 },
647 { 30, 17 }, { 20, 38 }, { 18, 45 }, { 15, 54 },
648 { 0, 79 }, { 36, -16 }, { 37, -14 }, { 37, -17 },
649 { 32, 1 }, { 34, 15 }, { 29, 15 }, { 24, 25 },
650 { 34, 22 }, { 31, 16 }, { 35, 18 }, { 31, 28 },
651 { 33, 41 }, { 36, 28 }, { 27, 47 }, { 21, 62 },
652 { 18, 31 }, { 19, 26 }, { 36, 24 }, { 24, 23 },
653 { 27, 16 }, { 24, 30 }, { 31, 29 }, { 22, 41 },
654 { 22, 42 }, { 16, 60 }, { 15, 52 }, { 14, 60 },
655 { 3, 78 }, { -16, 123 }, { 21, 53 }, { 22, 56 },
656 { 25, 61 },
658 /* 399 - 435 */
659 { 21, 33 }, { 19, 50 }, { 17, 61 },
660 { -3, 78 }, { -8, 74 }, { -9, 72 }, { -10, 72 },
661 { -18, 75 }, { -12, 71 }, { -11, 63 }, { -5, 70 },
662 { -17, 75 }, { -14, 72 }, { -16, 67 }, { -8, 53 },
663 { -14, 59 }, { -9, 52 }, { -11, 68 }, { 9, -2 },
664 { 30, -10 }, { 31, -4 }, { 33, -1 }, { 33, 7 },
665 { 31, 12 }, { 37, 23 }, { 31, 38 }, { 20, 64 },
666 { -9, 71 }, { -7, 37 }, { -8, 44 }, { -11, 49 },
667 { -10, 56 }, { -12, 59 }, { -8, 63 }, { -9, 67 },
668 { -6, 68 }, { -10, 79 },
670 /* 436 - 459 */
671 { -3, 78 }, { -8, 74 }, { -9, 72 }, { -10, 72 },
672 { -18, 75 }, { -12, 71 }, { -11, 63 }, { -5, 70 },
673 { -17, 75 }, { -14, 72 }, { -16, 67 }, { -8, 53 },
674 { -14, 59 }, { -9, 52 }, { -11, 68 }, { 9, -2 },
675 { 30, -10 }, { 31, -4 }, { 33, -1 }, { 33, 7 },
676 { 31, 12 }, { 37, 23 }, { 31, 38 }, { 20, 64 },
677 }
678 };
680 static const uint8_t left_block_options[4][16]={
681 {0,1,2,3,7,10,8,11,7+0*8, 7+1*8, 7+2*8, 7+3*8, 2+0*8, 2+3*8, 2+1*8, 2+2*8},
682 {2,2,3,3,8,11,8,11,7+2*8, 7+2*8, 7+3*8, 7+3*8, 2+1*8, 2+2*8, 2+1*8, 2+2*8},
683 {0,0,1,1,7,10,7,10,7+0*8, 7+0*8, 7+1*8, 7+1*8, 2+0*8, 2+3*8, 2+0*8, 2+3*8},
684 {0,2,0,2,7,10,7,10,7+0*8, 7+2*8, 7+0*8, 7+2*8, 2+0*8, 2+3*8, 2+0*8, 2+3*8}
685 };
687 static const uint8_t rem6[52]={
688 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
689 };
691 static const uint8_t div6[52]={
692 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8,
693 };
695 static void init_dequant8_coeff_table(H264Slice *s, EntropyContext *ec){
696 int i,q,x;
697 const int transpose = HAVE_MMX | HAVE_ALTIVEC | HAVE_NEON;
698 ec->dequant8_coeff[0] = ec->dequant8_buffer[0];
699 ec->dequant8_coeff[1] = ec->dequant8_buffer[1];
701 for(i=0; i<2; i++){
702 if(i && !memcmp(s->pps.scaling_matrix8[0], s->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
703 ec->dequant8_coeff[1] = ec->dequant8_buffer[0];
704 break;
705 }
707 for(q=0; q<52; q++){
708 int shift = div6[q];
709 int idx = rem6[q];
710 for(x=0; x<64; x++)
711 ec->dequant8_coeff[i][q][transpose ? (x>>3)|((x&7)<<3) : x] =
712 ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
713 s->pps.scaling_matrix8[i][x]) << shift;
714 }
715 }
716 }
718 static void init_dequant4_coeff_table(H264Slice *s, EntropyContext *ec){
719 int i,j,q,x;
720 const int transpose = HAVE_MMX | HAVE_ALTIVEC | HAVE_NEON;
721 for(i=0; i<6; i++ ){
722 ec->dequant4_coeff[i] = ec->dequant4_buffer[i];
723 for(j=0; j<i; j++){
724 if(!memcmp(s->pps.scaling_matrix4[j], s->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
725 ec->dequant4_coeff[i] = ec->dequant4_buffer[j];
726 break;
727 }
728 }
729 if(j<i)
730 continue;
732 for(q=0; q<52; q++){
733 int shift = div6[q] + 2;
734 int idx = rem6[q];
735 for(x=0; x<16; x++)
736 ec->dequant4_coeff[i][q][transpose ? (x>>2)|((x<<2)&0xF) : x] =
737 ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
738 s->pps.scaling_matrix4[i][x]) << shift;
739 }
740 }
741 }
743 void init_dequant_tables(H264Slice *s, EntropyContext *ec){
744 int i,x;
746 init_dequant4_coeff_table(s, ec);
747 if(s->pps.transform_8x8_mode)
748 init_dequant8_coeff_table(s, ec);
749 if(s->transform_bypass){
750 for(i=0; i<6; i++)
751 for(x=0; x<16; x++)
752 ec->dequant4_coeff[i][0][x] = 1<<6;
753 if(s->pps.transform_8x8_mode)
754 for(i=0; i<2; i++)
755 for(x=0; x<64; x++)
756 ec->dequant8_coeff[i][0][x] = 1<<6;
757 }
758 }
760 void ff_h264_init_cabac_states(EntropyContext *ec, H264Slice *s, CABACContext *c) {
761 int i;
762 const int8_t (*tab)[2];
764 if( s->slice_type_nos == FF_I_TYPE ) tab = cabac_context_init_I;
765 else tab = cabac_context_init_PB[s->cabac_init_idc];
767 /* calculate pre-state */
768 for( i= 0; i < 460; i++ ) {
769 int pre = 2*(((tab[i][0] * ec->curr_qscale) >>4 ) + tab[i][1]) - 127;
771 pre^= pre>>31;
772 if(pre > 124)
773 pre= 124 + (pre&1);
775 c->cabac_state[i] = pre;
776 }
777 }
779 static void fill_decode_neighbors(EntropyContext *ec, H264Slice *s){
780 H264Mb *m = ec->m;
781 const int mb_x = m->mb_x;
783 if (m->mb_y){
784 ec->top_type = ec->mb_type_top[mb_x];
785 ec->topright_type= ec->mb_type_top[mb_x+1];
786 ec->topleft_type = ec->mb_type_top[mb_x-1];
787 m->qscale_top_mb_xy = ec->qscale_top[mb_x];
788 } else {
789 ec->top_type = 0;
790 ec->topright_type= 0;
791 ec->topleft_type = 0;
792 m->qscale_top_mb_xy = 0;
793 }
795 ec->left_type = ec->mb_type[mb_x-1] ;
796 m->qscale_left_mb_xy = ec->qscale[mb_x-1];
798 }
800 static void fill_decode_caches(EntropyContext *ec, H264Slice *s, int mb_type){
801 H264Mb *m = ec->m;
802 int topleft_type, top_type, topright_type, left_type;
803 const uint8_t * left_block= left_block_options[0];
804 const int mb_x = m->mb_x;
805 int i;
807 topleft_type = ec->topleft_type;
808 top_type = ec->top_type;
809 topright_type= ec->topright_type;
810 left_type = ec->left_type;
812 if(!IS_SKIP(mb_type)){
813 if(top_type){
814 AV_COPY32(&ec->non_zero_count_cache[4+8*0], &ec->non_zero_count_top[mb_x][0]);
815 ec->non_zero_count_cache[1+8*0]= ec->non_zero_count_top[mb_x][4];
816 ec->non_zero_count_cache[2+8*0]= ec->non_zero_count_top[mb_x][5];
817 ec->non_zero_count_cache[1+8*3]= ec->non_zero_count_top[mb_x][6];
818 ec->non_zero_count_cache[2+8*3]= ec->non_zero_count_top[mb_x][7];
820 }else {
821 ec->non_zero_count_cache[1+8*0]=
822 ec->non_zero_count_cache[2+8*0]=
823 ec->non_zero_count_cache[1+8*3]=
824 ec->non_zero_count_cache[2+8*3]=
825 AV_WN32A(&ec->non_zero_count_cache[4+8*0], !IS_INTRA(mb_type) ? 0 : 0x40404040);
826 }
828 if(left_type){
829 for (i=0; i<2; i++) {
830 ec->non_zero_count_cache[3+8*1 + 2*8*i]= ec->non_zero_count_left[i*2+0];
831 ec->non_zero_count_cache[3+8*2 + 2*8*i]= ec->non_zero_count_left[i*2+1];
832 ec->non_zero_count_cache[0+8*1 + 3*8*i]= ec->non_zero_count_left[4+i*2+0];
833 ec->non_zero_count_cache[0+8*2 + 3*8*i]= ec->non_zero_count_left[4+i*2+1];
834 }
835 }
836 else{
837 for (i=0; i<2; i++) {
838 ec->non_zero_count_cache[3+8*1 + 2*8*i]=
839 ec->non_zero_count_cache[3+8*2 + 2*8*i]=
840 ec->non_zero_count_cache[0+8*1 + 3*8*i]=
841 ec->non_zero_count_cache[0+8*2 + 3*8*i]= !IS_INTRA(mb_type) ? 0 : 64;
842 }
843 }
845 // top_cbp
846 if(top_type) {
847 ec->top_cbp = ec->cbp_top[mb_x];
848 } else {
849 ec->top_cbp = IS_INTRA(mb_type) ? 0x1CF : 0x00F;
850 }
851 // left_cbp
852 if (left_type) {
853 ec->left_cbp = (ec->cbp[mb_x-1] & 0x1f0)
854 | ((ec->cbp[mb_x-1]>>(left_block[0]&(~1)))&2)
855 | (((ec->cbp[mb_x-1]>>(left_block[2]&(~1)))&2) << 2);
856 } else {
857 ec->left_cbp = IS_INTRA(mb_type) ? 0x1CF : 0x00F;
858 }
859 }
861 if(IS_INTER(mb_type) ||(IS_DIRECT(mb_type) && s->direct_spatial_mv_pred)){
862 int list;
864 ec->ref_cache[0][scan8[5 ]+1] = ec->ref_cache[0][scan8[7 ]+1] = ec->ref_cache[0][scan8[13]+1] =
865 ec->ref_cache[1][scan8[5 ]+1] = ec->ref_cache[1][scan8[7 ]+1] = ec->ref_cache[1][scan8[13]+1] = PART_NOT_AVAILABLE;
867 for(list=0; list<s->list_count; list++){
868 if(!USES_LIST(mb_type, list)){
869 continue;
870 }
871 assert(!(IS_DIRECT(mb_type) && !s->direct_spatial_mv_pred));
873 if(USES_LIST(top_type, list)){
874 ec->ref_cache[list][scan8[0] + 0 - 1*8]=
875 ec->ref_cache[list][scan8[0] + 1 - 1*8]= ec->ref_index_top[list][4*mb_x + 2];
876 ec->ref_cache[list][scan8[0] + 2 - 1*8]=
877 ec->ref_cache[list][scan8[0] + 3 - 1*8]= ec->ref_index_top[list][4*mb_x + 3];
878 }else{
879 AV_WN32A(&ec->ref_cache[list][scan8[0] + 0 - 1*8], ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101);
880 }
882 if(mb_type & (MB_TYPE_16x8|MB_TYPE_8x8)){
883 for(i=0; i<2; i++){
884 int cache_idx = scan8[0] - 1 + i*2*8;
885 if(USES_LIST(left_type, list)){
886 const int b8_x= 4*(mb_x-1) + 1;
887 ec->ref_cache[list][cache_idx ]= ec->ref_index[list][b8_x + (left_block[0+i*2]&~1)];
888 ec->ref_cache[list][cache_idx+8]= ec->ref_index[list][b8_x + (left_block[1+i*2]&~1)];
889 }else{
890 ec->ref_cache[list][cache_idx ]=
891 ec->ref_cache[list][cache_idx+8]= (left_type ? LIST_NOT_USED : PART_NOT_AVAILABLE);
892 }
893 }
894 }else{
895 if(USES_LIST(left_type, list)){
896 const int b8_x= 4*(mb_x-1) + 1;
897 ec->ref_cache[list][scan8[0] - 1]= ec->ref_index[list][b8_x + (left_block[0]&~1)];
898 }else{
899 ec->ref_cache[list][scan8[0] - 1]= left_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
900 }
901 }
903 if(USES_LIST(topright_type, list)){
904 ec->ref_cache[list][scan8[0] + 4 - 1*8]= ec->ref_index_top[list][4*(mb_x+1) + 2];
905 }else{
906 ec->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
907 }
908 if(ec->ref_cache[list][scan8[0] + 4 - 1*8] < 0){
909 int topleft_partition= -1;
910 if(USES_LIST(topleft_type, list)){
911 const int b8_x= 4*(mb_x-1) + 1 + (topleft_partition & 2);
912 ec->ref_cache[list][scan8[0] - 1 - 1*8]= ec->ref_index_top[list][b8_x];
913 }else{
914 ec->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
915 }
916 }
918 if((mb_type&(MB_TYPE_SKIP|MB_TYPE_DIRECT2)))
919 continue;
921 if(!(mb_type&(MB_TYPE_SKIP|MB_TYPE_DIRECT2))) {
922 ec->ref_cache[list][scan8[4 ]] =
923 ec->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
925 /* XXX beurk, Load mvd */
926 if(USES_LIST(top_type, list)){
927 AV_COPY64(ec->mvd_cache[list][scan8[0] + 0 - 1*8], ec->mvd_top[list][8*mb_x + 0]);
928 }else{
929 AV_ZERO64(ec->mvd_cache[list][scan8[0] + 0 - 1*8]);
930 }
931 if(USES_LIST(left_type, list)){
932 AV_COPY16(ec->mvd_cache[list][scan8[0] - 1 + 0*8], ec->mvd[list][8*(mb_x-1) + 6 - left_block[0]]);
933 AV_COPY16(ec->mvd_cache[list][scan8[0] - 1 + 1*8], ec->mvd[list][8*(mb_x-1) + 6 - left_block[1]]);
934 }else{
935 AV_ZERO16(ec->mvd_cache [list][scan8[0] - 1 + 0*8]);
936 AV_ZERO16(ec->mvd_cache [list][scan8[0] - 1 + 1*8]);
937 }
938 if(USES_LIST(left_type, list)){
939 AV_COPY16(ec->mvd_cache[list][scan8[0] - 1 + 2*8], ec->mvd[list][8*(mb_x-1) + 6 - left_block[2]]);
940 AV_COPY16(ec->mvd_cache[list][scan8[0] - 1 + 3*8], ec->mvd[list][8*(mb_x-1) + 6 - left_block[3]]);
941 }else{
942 AV_ZERO16(ec->mvd_cache [list][scan8[0] - 1 + 2*8]);
943 AV_ZERO16(ec->mvd_cache [list][scan8[0] - 1 + 3*8]);
944 }
945 AV_ZERO16(ec->mvd_cache [list][scan8[4 ]]);
946 AV_ZERO16(ec->mvd_cache [list][scan8[12]]);
947 if(s->slice_type_nos == FF_B_TYPE){
948 fill_rectangle(&ec->direct_cache[scan8[0]], 4, 4, 8, MB_TYPE_16x16>>1, 1);
950 if(IS_DIRECT(top_type)){
951 AV_WN32A(&ec->direct_cache[scan8[0] - 1*8], 0x01010101u*(MB_TYPE_DIRECT2>>1));
952 }else if(IS_8X8(top_type)){
953 int b8_x = 4*mb_x;
954 ec->direct_cache[scan8[0] + 0 - 1*8]= ec->direct_top[b8_x + 2];
955 ec->direct_cache[scan8[0] + 2 - 1*8]= ec->direct_top[b8_x + 3];
956 }else{
957 AV_WN32A(&ec->direct_cache[scan8[0] - 1*8], 0x01010101*(MB_TYPE_16x16>>1));
958 }
960 if(IS_DIRECT(left_type))
961 ec->direct_cache[scan8[0] - 1 + 0*8]= MB_TYPE_DIRECT2>>1;
962 else if(IS_8X8(left_type))
963 ec->direct_cache[scan8[0] - 1 + 0*8]= ec->direct[4*(mb_x-1) + 1 + (left_block[0]&~1)];
964 else
965 ec->direct_cache[scan8[0] - 1 + 0*8]= MB_TYPE_16x16>>1;
967 if(IS_DIRECT(left_type))
968 ec->direct_cache[scan8[0] - 1 + 2*8]= MB_TYPE_DIRECT2>>1;
969 else if(IS_8X8(left_type))
970 ec->direct_cache[scan8[0] - 1 + 2*8]= ec->direct[4*(mb_x-1) + 1 + (left_block[2]&~1)];
971 else
972 ec->direct_cache[scan8[0] - 1 + 2*8]= MB_TYPE_16x16>>1;
973 }
974 }
975 }
976 }
977 ec->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type);
978 }
980 static inline void write_back_non_zero_count(EntropyContext *ec, H264Slice *s){
981 H264Mb *m = ec->m;
982 const int mb_x= m->mb_x;
984 //bottom nnz
985 AV_COPY32(&ec->non_zero_count[mb_x][0], &ec->non_zero_count_cache[4+8*4] );
986 ec->non_zero_count[mb_x][4] = ec->non_zero_count_cache[1+8*2];
987 ec->non_zero_count[mb_x][5] = ec->non_zero_count_cache[2+8*2];
988 ec->non_zero_count[mb_x][6] = ec->non_zero_count_cache[1+8*5];
989 ec->non_zero_count[mb_x][7] = ec->non_zero_count_cache[2+8*5];
991 for (int i=0; i<2; i++) {
992 ec->non_zero_count_left[i*2+0] = ec->non_zero_count_cache[7+8*1 + 2*8*i];
993 ec->non_zero_count_left[i*2+1] = ec->non_zero_count_cache[7+8*2 + 2*8*i];
994 ec->non_zero_count_left[4+i*2+0] = ec->non_zero_count_cache[2+8*1 + 3*8*i];
995 ec->non_zero_count_left[4+i*2+1] = ec->non_zero_count_cache[2+8*2 + 3*8*i];
996 }
998 AV_COPY32(&m->non_zero_count[ 0], &ec->non_zero_count_cache[4+8*1]);
999 AV_COPY32(&m->non_zero_count[ 4], &ec->non_zero_count_cache[4+8*2]);
1000 AV_COPY32(&m->non_zero_count[ 8], &ec->non_zero_count_cache[4+8*3]);
1001 AV_COPY32(&m->non_zero_count[12], &ec->non_zero_count_cache[4+8*4]);
1003 for (int i=0; i<2; i++) {
1004 m->non_zero_count[16 + i*2 ] = ec->non_zero_count_cache[8*1 + 8*i + 1];
1005 m->non_zero_count[16 + i*2 +1] = ec->non_zero_count_cache[8*1 + 8*i + 2];
1006 m->non_zero_count[20 + i*2 ] = ec->non_zero_count_cache[8*4 + 8*i + 1];
1007 m->non_zero_count[20 + i*2 +1] = ec->non_zero_count_cache[8*4 + 8*i + 2];
1008 }
1009 }
1011 static inline void write_back_motion(EntropyContext *ec, H264Slice *s, int mb_type){
1012 H264Mb *m = ec->m;
1013 const int mb_x = m->mb_x;
1014 const int b_x = 4*m->mb_x; //try mb2b(8)_xy
1015 int list;
1017 for(list=0; list<s->list_count; list++){
1018 if(!USES_LIST(mb_type, list))
1019 continue;
1021 {
1022 uint8_t (*mvd_dst)[2] = (void *) ec->mvd[list][8*mb_x];
1023 uint8_t (*mvd_src)[2] = &ec->mvd_cache[list][scan8[0]];
1024 if(IS_SKIP(mb_type))
1025 AV_ZERO128(mvd_dst);
1026 else{
1027 AV_COPY64(mvd_dst, mvd_src + 8*3);
1028 AV_COPY16(mvd_dst + 3 + 3, mvd_src + 3 + 8*0);
1029 AV_COPY16(mvd_dst + 3 + 2, mvd_src + 3 + 8*1);
1030 AV_COPY16(mvd_dst + 3 + 1, mvd_src + 3 + 8*2);
1031 }
1032 }
1033 int8_t *ref_index = &ec->ref_index[list][b_x];
1034 {
1035 ref_index[0+0*2]= ec->ref_cache[list][scan8[0]];
1036 ref_index[1+0*2]= ec->ref_cache[list][scan8[4]];
1037 ref_index[0+1*2]= ec->ref_cache[list][scan8[8]];
1038 ref_index[1+1*2]= ec->ref_cache[list][scan8[12]];
1039 }
1040 }
1042 if(s->slice_type_nos == FF_B_TYPE){
1043 if(IS_8X8(mb_type)){
1044 uint8_t *direct = &ec->direct[4*mb_x];
1045 direct[1] = m->sub_mb_type[1]>>1;
1046 direct[2] = m->sub_mb_type[2]>>1;
1047 direct[3] = m->sub_mb_type[3]>>1;
1048 }
1049 }
1050 }
1052 static inline int get_dct8x8_allowed(EntropyContext *ec, H264Slice *s){
1053 H264Mb *m = ec->m;
1054 if(s->direct_8x8_inference_flag)
1055 return !(AV_RN64A(m->sub_mb_type) & ((MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_8x8 )*0x0001000100010001ULL));
1056 else
1057 return !(AV_RN64A(m->sub_mb_type) & ((MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_8x8|MB_TYPE_DIRECT2)*0x0001000100010001ULL));
1058 }
1060 /**
1061 * decodes a P_SKIP or B_SKIP macroblock
1062 */
1063 static void decode_mb_skip(EntropyContext *ec, H264Slice *s){
1064 H264Mb *m = ec->m;
1065 const int mb_x = m->mb_x;
1066 int mb_type;
1068 if( s->slice_type_nos == FF_B_TYPE )
1069 mb_type= MB_TYPE_16x16|MB_TYPE_L0L1|MB_TYPE_DIRECT2|MB_TYPE_SKIP;
1070 else
1071 mb_type= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP;
1073 fill_rectangle(&ec->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
1074 write_back_motion(ec, s, mb_type);
1075 m->mb_type = ec->mb_type[mb_x] = mb_type;
1076 m->qscale_mb_xy = ec->qscale[mb_x]= ec->curr_qscale;
1078 AV_ZERO64(ec->non_zero_count[mb_x]);
1079 AV_ZERO64(ec->non_zero_count_left);
1080 memset(m->non_zero_count, 0, 24);
1081 }
1083 static int decode_cabac_intra_mb_type(EntropyContext *ec, H264Slice *s, CABACContext *c, int ctx_base, int intra_slice) {
1084 uint8_t *state= &c->cabac_state[ctx_base];
1085 int mb_type;
1087 if(intra_slice){
1088 int ctx=0;
1089 if( ec->left_type & (MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM))
1090 ctx++;
1091 if( ec->top_type & (MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM))
1092 ctx++;
1093 if( get_cabac_noinline( c, &state[ctx] ) == 0 )
1094 return 0; /* I4x4 */
1095 state += 2;
1096 }else{
1097 if( get_cabac_noinline( c, state ) == 0 )
1098 return 0; /* I4x4 */
1099 }
1101 if( get_cabac_terminate( c ) )
1102 return 25; /* PCM */
1104 mb_type = 1; /* I16x16 */
1105 mb_type += 12 * get_cabac_noinline( c, &state[1] ); /* cbp_luma != 0 */
1106 if( get_cabac_noinline(c, &state[2] ) ) /* cbp_chroma */
1107 mb_type += 4 + 4 * get_cabac_noinline(c, &state[2+intra_slice] );
1108 mb_type += 2 * get_cabac_noinline(c, &state[3+intra_slice] );
1109 mb_type += 1 * get_cabac_noinline(c, &state[3+2*intra_slice] );
1110 return mb_type;
1111 }
1113 static int decode_cabac_mb_skip(EntropyContext *ec, H264Slice *s, H264Mb *m, CABACContext *c) {
1114 int ctx = 0;
1116 if( m->mb_x>0 && !IS_SKIP( ec->left_type ))
1117 ctx++;
1118 if( m->mb_y>0 && !IS_SKIP( ec->top_type ))
1119 ctx++;
1121 if( s->slice_type_nos == FF_B_TYPE )
1122 ctx += 13;
1123 return get_cabac_noinline(c, &c->cabac_state[11+ctx] );
1124 }
1126 static int decode_cabac_mb_intra4x4_pred_mode_delta( CABACContext *c) {
1127 int mode = 0;
1129 if( get_cabac(c, &c->cabac_state[68] ) )
1130 return -1;
1132 mode += 1 * get_cabac(c, &c->cabac_state[69] );
1133 mode += 2 * get_cabac(c, &c->cabac_state[69] );
1134 mode += 4 * get_cabac(c, &c->cabac_state[69] );
1136 return mode;
1137 }
1139 static int decode_cabac_mb_chroma_pre_mode(EntropyContext *ec, H264Slice *s, CABACContext *c) {
1140 H264Mb *m = ec->m;
1141 const int mb_x = m->mb_x;
1143 int ctx = 0;
1145 /* No need to test for IS_INTRA4x4 and IS_INTRA16x16, as we set chroma_pred_mode to 0 */
1146 if( ec->left_type && ec->chroma_pred_mode[mb_x-1] != 0 )
1147 ctx++;
1149 if( ec->top_type && ec->chroma_pred_mode_top[mb_x] != 0 )
1150 ctx++;
1152 if( get_cabac_noinline(c, &c->cabac_state[64+ctx] ) == 0 )
1153 return 0;
1155 if( get_cabac_noinline(c, &c->cabac_state[64+3] ) == 0 )
1156 return 1;
1157 if( get_cabac_noinline(c, &c->cabac_state[64+3] ) == 0 )
1158 return 2;
1159 else
1160 return 3;
1161 }
1163 static int decode_cabac_mb_cbp_luma(EntropyContext *ec, CABACContext *c) {
1164 int cbp_b, cbp_a, ctx, cbp = 0;
1166 cbp_a = ec->left_cbp;
1167 cbp_b = ec->top_cbp;
1169 ctx = !(cbp_a & 0x02) + 2 * !(cbp_b & 0x04);
1170 cbp += get_cabac_noinline(c, &c->cabac_state[73 + ctx]);
1171 ctx = !(cbp & 0x01) + 2 * !(cbp_b & 0x08);
1172 cbp += get_cabac_noinline(c, &c->cabac_state[73 + ctx]) << 1;
1173 ctx = !(cbp_a & 0x08) + 2 * !(cbp & 0x01);
1174 cbp += get_cabac_noinline(c, &c->cabac_state[73 + ctx]) << 2;
1175 ctx = !(cbp & 0x04) + 2 * !(cbp & 0x02);
1176 cbp += get_cabac_noinline(c, &c->cabac_state[73 + ctx]) << 3;
1177 return cbp;
1178 }
1179 static int decode_cabac_mb_cbp_chroma(EntropyContext *ec, CABACContext *c) {
1180 int ctx;
1181 int cbp_a, cbp_b;
1183 cbp_a = (ec->left_cbp>>4)&0x03;
1184 cbp_b = (ec-> top_cbp>>4)&0x03;
1186 ctx = 0;
1187 if( cbp_a > 0 ) ctx++;
1188 if( cbp_b > 0 ) ctx += 2;
1189 if( get_cabac_noinline(c, &c->cabac_state[77 + ctx] ) == 0 )
1190 return 0;
1192 ctx = 4;
1193 if( cbp_a == 2 ) ctx++;
1194 if( cbp_b == 2 ) ctx += 2;
1195 return 1 + get_cabac_noinline(c, &c->cabac_state[77 + ctx] );
1196 }
1198 static int decode_cabac_p_mb_sub_type( CABACContext *c) {
1199 if( get_cabac(c, &c->cabac_state[21] ) )
1200 return 0; /* 8x8 */
1201 if( !get_cabac(c, &c->cabac_state[22] ) )
1202 return 1; /* 8x4 */
1203 if( get_cabac(c, &c->cabac_state[23] ) )
1204 return 2; /* 4x8 */
1205 return 3; /* 4x4 */
1206 }
1207 static int decode_cabac_b_mb_sub_type(CABACContext *c) {
1208 int type;
1209 if( !get_cabac(c, &c->cabac_state[36] ) )
1210 return 0; /* B_Direct_8x8 */
1211 if( !get_cabac(c, &c->cabac_state[37] ) )
1212 return 1 + get_cabac(c, &c->cabac_state[39] ); /* B_L0_8x8, B_L1_8x8 */
1213 type = 3;
1214 if( get_cabac(c, &c->cabac_state[38] ) ) {
1215 if( get_cabac(c, &c->cabac_state[39] ) )
1216 return 11 + get_cabac(c, &c->cabac_state[39] ); /* B_L1_4x4, B_Bi_4x4 */
1217 type += 4;
1218 }
1219 type += 2*get_cabac(c, &c->cabac_state[39] );
1220 type += get_cabac(c, &c->cabac_state[39] );
1221 return type;
1222 }
1224 static int decode_cabac_mb_ref(EntropyContext *ec, H264Slice *s, CABACContext *c, int list, int n ) {
1225 int refa = ec->ref_cache[list][scan8[n] - 1];
1226 int refb = ec->ref_cache[list][scan8[n] - 8];
1227 int ref = 0;
1228 int ctx = 0;
1230 if( s->slice_type_nos == FF_B_TYPE) {
1231 if( refa > 0 && !(ec->direct_cache[scan8[n] - 1]&(MB_TYPE_DIRECT2>>1)) )
1232 ctx++;
1233 if( refb > 0 && !(ec->direct_cache[scan8[n] - 8]&(MB_TYPE_DIRECT2>>1)) )
1234 ctx += 2;
1235 } else {
1236 if( refa > 0 )
1237 ctx++;
1238 if( refb > 0 )
1239 ctx += 2;
1240 }
1242 while( get_cabac(c, &c->cabac_state[54+ctx] ) ) {
1243 ref++;
1244 ctx = (ctx>>2)+4;
1245 if(ref >= 32 /*h->ref_list[list]*/){
1246 return -1;
1247 }
1248 }
1249 return ref;
1250 }
1252 static int decode_cabac_mb_mvd( CABACContext *c, int ctxbase, int amvd, int *mvda) {
1253 int mvd;
1255 if(!get_cabac(c, &c->cabac_state[ctxbase+((amvd-3)>>(INT_BIT-1))+((amvd-33)>>(INT_BIT-1))+2])){
1256 *mvda= 0;
1257 return 0;
1258 }
1260 mvd= 1;
1261 ctxbase+= 3;
1262 while( mvd < 9 && get_cabac(c, &c->cabac_state[ctxbase] ) ) {
1263 if( mvd < 4 )
1264 ctxbase++;
1265 mvd++;
1266 }
1268 if( mvd >= 9 ) {
1269 int k = 3;
1270 while( get_cabac_bypass(c ) ) {
1271 mvd += 1 << k;
1272 k++;
1273 if(k>24){
1274 av_log(AV_LOG_ERROR, "overflow in decode_cabac_mb_mvd\n");
1275 return INT_MIN;
1276 }
1277 }
1278 while( k-- ) {
1279 mvd += get_cabac_bypass(c )<<k;
1280 }
1281 *mvda=mvd < 70 ? mvd : 70;
1282 }else
1283 *mvda=mvd;
1284 return get_cabac_bypass_sign(c, -mvd );
1285 }
1287 #define DECODE_CABAC_MB_MVD( ec, c, list, n )\
1288 {\
1289 int amvd0 = ec->mvd_cache[list][scan8[n] - 1][0] +\
1290 ec->mvd_cache[list][scan8[n] - 8][0];\
1291 int amvd1 = ec->mvd_cache[list][scan8[n] - 1][1] +\
1292 ec->mvd_cache[list][scan8[n] - 8][1];\
1293 \
1294 m->mvd[list][mp][0] = decode_cabac_mb_mvd( c, 40, amvd0, &mpx ); \
1295 m->mvd[list][mp][1] = decode_cabac_mb_mvd( c, 47, amvd1, &mpy ); \
1296 mp++; \
1297 }
1299 static av_always_inline int get_cabac_cbf_ctx(EntropyContext *ec, H264Slice *s, int cat, int idx, int is_dc ) {
1300 int nza, nzb;
1301 int ctx = 0;
1303 if( is_dc ) {
1304 if( cat == 0 ) {
1305 nza = ec->left_cbp&0x100;
1306 nzb = ec-> top_cbp&0x100;
1307 } else {
1308 nza = (ec->left_cbp>>(6+idx))&0x01;
1309 nzb = (ec-> top_cbp>>(6+idx))&0x01;
1310 }
1311 } else {
1312 assert(cat == 1 || cat == 2 || cat == 4);
1313 nza = ec->non_zero_count_cache[scan8[idx] - 1];
1314 nzb = ec->non_zero_count_cache[scan8[idx] - 8];
1315 }
1317 if( nza > 0 )
1318 ctx++;
1320 if( nzb > 0 )
1321 ctx += 2;
1323 return ctx + 4 * cat;
1324 }
1326 DECLARE_ASM_CONST(1, uint8_t, last_coeff_flag_offset_8x8)[63] = {
1327 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1328 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1329 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
1330 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
1331 };
1333 static const int significant_coeff_flag_offset[2][6] = {
1334 { 105+0, 105+15, 105+29, 105+44, 105+47, 402 },
1335 { 277+0, 277+15, 277+29, 277+44, 277+47, 436 }
1336 };
1337 static const int last_coeff_flag_offset[2][6] = {
1338 { 166+0, 166+15, 166+29, 166+44, 166+47, 417 },
1339 { 338+0, 338+15, 338+29, 338+44, 338+47, 451 }
1340 };
1341 static const int coeff_abs_level_m1_offset[6] = {
1342 227+0, 227+10, 227+20, 227+30, 227+39, 426
1343 };
1344 static const uint8_t significant_coeff_flag_offset_8x8[2][63] = {
1345 { 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
1346 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
1347 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
1348 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12 },
1349 { 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,
1350 6, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,11,12,11,
1351 9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9,
1352 9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14 }
1353 };
1354 /* node ctx: 0..3: abslevel1 (with abslevelgt1 == 0).
1355 * 4..7: abslevelgt1 + 3 (and abslevel1 doesn't matter).
1356 * map node ctx => cabac ctx for level=1 */
1357 static const uint8_t coeff_abs_level1_ctx[8] = { 1, 2, 3, 4, 0, 0, 0, 0 };
1358 /* map node ctx => cabac ctx for level>1 */
1359 static const uint8_t coeff_abs_levelgt1_ctx[8] = { 5, 5, 5, 5, 6, 7, 8, 9 };
1360 static const uint8_t coeff_abs_level_transition[2][8] = {
1361 /* update node ctx after decoding a level=1 */
1362 { 1, 2, 3, 3, 4, 5, 6, 7 },
1363 /* update node ctx after decoding a level>1 */
1364 { 4, 4, 4, 4, 5, 6, 7, 7 }
1365 };
1367 static av_always_inline void decode_cabac_residual_internal(EntropyContext *ec, H264Slice *s, CABACContext *c, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff, int is_dc ) {
1368 H264Mb *m = ec->m;
1369 const int mb_x = m->mb_x;
1370 int index[64];
1372 int av_unused last;
1373 int coeff_count = 0;
1374 int node_ctx = 0;
1376 uint8_t *significant_coeff_ctx_base;
1377 uint8_t *last_coeff_ctx_base;
1378 uint8_t *abs_level_m1_ctx_base;
1380 /* read coded block flag */
1381 if( is_dc || cat != 5 ) {
1382 if( get_cabac( c, &c->cabac_state[85 + get_cabac_cbf_ctx( ec, s, cat, n, is_dc ) ] ) == 0 ) {
1383 if( !is_dc )
1384 ec->non_zero_count_cache[scan8[n]] = 0;
1385 return;
1386 }
1387 }
1389 significant_coeff_ctx_base = c->cabac_state
1390 + significant_coeff_flag_offset[0][cat];
1391 last_coeff_ctx_base = c->cabac_state
1392 + last_coeff_flag_offset[0][cat];
1393 abs_level_m1_ctx_base = c->cabac_state
1394 + coeff_abs_level_m1_offset[cat];
1396 if( !is_dc && cat == 5 ) {
1397 #define DECODE_SIGNIFICANCE( coefs, sig_off, last_off ) \
1398 for(last= 0; last < coefs; last++) { \
1399 uint8_t *sig_ctx = significant_coeff_ctx_base + sig_off; \
1400 if( get_cabac( c, sig_ctx )) { \
1401 uint8_t *last_ctx = last_coeff_ctx_base + last_off; \
1402 index[coeff_count++] = last; \
1403 if( get_cabac( c, last_ctx ) ) { \
1404 last= max_coeff; \
1405 break; \
1406 } \
1407 } \
1408 }\
1409 if( last == max_coeff -1 ) {\
1410 index[coeff_count++] = last;\
1411 }
1413 const uint8_t *sig_off = significant_coeff_flag_offset_8x8[0];
1414 DECODE_SIGNIFICANCE( 63, sig_off[last], last_coeff_flag_offset_8x8[last] );
1415 } else {
1416 DECODE_SIGNIFICANCE( max_coeff - 1, last, last );
1417 }
1418 assert(coeff_count > 0);
1420 if( is_dc ) {
1421 if( cat == 0 )
1422 ec->cbp[mb_x] |= 0x100;
1423 else
1424 ec->cbp[mb_x] |= 0x40 << n;
1425 } else {
1426 if( cat == 5 )
1427 fill_rectangle(&ec->non_zero_count_cache[scan8[n]], 2, 2, 8, coeff_count, 1);
1428 else {
1429 assert( cat == 1 || cat == 2 || cat == 4 );
1430 ec->non_zero_count_cache[scan8[n]] = coeff_count;
1431 }
1432 }
1434 do {
1435 uint8_t *ctx = coeff_abs_level1_ctx[node_ctx] + abs_level_m1_ctx_base;
1437 int j= scantable[index[--coeff_count]];
1439 if( get_cabac( c, ctx ) == 0 ) {
1440 node_ctx = coeff_abs_level_transition[0][node_ctx];
1441 if( is_dc ) {
1442 block[j] = get_cabac_bypass_sign( c, -1);
1443 }else{
1444 block[j] = (get_cabac_bypass_sign( c, -qmul[j]) + 32) >> 6;
1445 }
1446 } else {
1447 int coeff_abs = 2;
1448 ctx = coeff_abs_levelgt1_ctx[node_ctx] + abs_level_m1_ctx_base;
1449 node_ctx = coeff_abs_level_transition[1][node_ctx];
1451 while( coeff_abs < 15 && get_cabac( c, ctx ) ) {
1452 coeff_abs++;
1453 }
1455 if( coeff_abs >= 15 ) {
1456 int j = 0;
1457 while( get_cabac_bypass( c ) ) {
1458 j++;
1459 }
1461 coeff_abs=1;
1462 while( j-- ) {
1463 coeff_abs += coeff_abs + get_cabac_bypass( c );
1464 }
1465 coeff_abs+= 14;
1466 }
1468 if( is_dc ) {
1469 block[j] = get_cabac_bypass_sign( c, -coeff_abs );
1470 }else{
1471 block[j] = (get_cabac_bypass_sign( c, -coeff_abs ) * qmul[j] + 32) >> 6;
1472 }
1473 }
1474 } while( coeff_count );
1476 }
1478 static void decode_cabac_residual_dc( EntropyContext *ec, H264Slice *s, CABACContext *c, DCTELEM *block, int cat, int n, const uint8_t *scantable, int max_coeff ) {
1479 decode_cabac_residual_internal( ec, s, c, block, cat, n, scantable, NULL, max_coeff, 1);
1480 }
1482 static void decode_cabac_residual_nondc( EntropyContext *ec, H264Slice *s, CABACContext *c, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff ) {
1483 decode_cabac_residual_internal( ec, s, c, block, cat, n, scantable, qmul, max_coeff, 0);
1484 }
1486 /**
1487 * decodes a macroblock
1488 * @return 0 if OK, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
1489 */
1490 int ff_h264_decode_mb_cabac(EntropyContext *ec, H264Slice *s, CABACContext *c) {
1491 H264Mb *m = ec->m;
1492 int mb_x = m->mb_x;
1493 int mb_type, partition_count, cbp = 0;
1494 int dct8x8_allowed= s->pps.transform_8x8_mode;
1496 fill_decode_neighbors(ec, s);
1498 if( s->slice_type_nos != FF_I_TYPE ) {
1499 int skip;
1500 /* a skipped mb needs the aff flag from the following mb */
1501 skip = decode_cabac_mb_skip( ec, s, m, c);
1503 /* read skip flags */
1504 if( skip ) {
1505 decode_mb_skip(ec, s);
1506 m->cbp = ec->cbp[mb_x] = 0;
1507 ec->chroma_pred_mode[mb_x] = 0;
1508 ec->last_qscale_diff = 0;
1509 return 0;
1510 }
1511 }
1513 if( s->slice_type_nos == FF_B_TYPE ) {
1514 int ctx = 0;
1516 if( !IS_DIRECT( ec->left_type-1 ) )
1517 ctx++;
1518 if( !IS_DIRECT( ec->top_type-1 ) )
1519 ctx++;
1521 if( !get_cabac_noinline(c, &c->cabac_state[27+ctx] ) ){
1522 mb_type= 0; /* B_Direct_16x16 */
1523 }else if( !get_cabac_noinline(c, &c->cabac_state[27+3] ) ) {
1524 mb_type= 1 + get_cabac_noinline(c, &c->cabac_state[27+5] ); /* B_L[01]_16x16 */
1525 }else{
1526 int bits;
1527 bits = get_cabac_noinline(c, &c->cabac_state[27+4] ) << 3;
1528 bits+= get_cabac_noinline(c, &c->cabac_state[27+5] ) << 2;
1529 bits+= get_cabac_noinline(c, &c->cabac_state[27+5] ) << 1;
1530 bits+= get_cabac_noinline(c, &c->cabac_state[27+5] );
1531 if( bits < 8 ){
1532 mb_type= bits + 3; /* B_Bi_16x16 through B_L1_L0_16x8 */
1533 }else if( bits == 13 ){
1534 mb_type= decode_cabac_intra_mb_type(ec, s, c, 32, 0);
1535 goto decode_intra_mb;
1536 }else if( bits == 14 ){
1537 mb_type= 11; /* B_L1_L0_8x16 */
1538 }else if( bits == 15 ){
1539 mb_type= 22; /* B_8x8 */
1540 }else{
1541 bits= ( bits<<1 ) + get_cabac_noinline(c, &c->cabac_state[27+5] );
1542 mb_type= bits - 4; /* B_L0_Bi_* through B_Bi_Bi_* */
1543 }
1544 }
1545 partition_count= b_mb_type_info[mb_type].partition_count;
1546 mb_type= b_mb_type_info[mb_type].type;
1547 } else if( s->slice_type_nos == FF_P_TYPE ) {
1548 if( get_cabac_noinline(c, &c->cabac_state[14] ) == 0 ) {
1549 /* P-type */
1550 if( get_cabac_noinline(c, &c->cabac_state[15] ) == 0 ) {
1551 /* P_L0_D16x16, P_8x8 */
1552 mb_type= 3 * get_cabac_noinline(c, &c->cabac_state[16] );
1553 } else {
1554 /* P_L0_D8x16, P_L0_D16x8 */
1555 mb_type= 2 - get_cabac_noinline(c, &c->cabac_state[17] );
1556 }
1557 partition_count= p_mb_type_info[mb_type].partition_count;
1558 mb_type= p_mb_type_info[mb_type].type;
1559 } else {
1560 mb_type= decode_cabac_intra_mb_type(ec, s, c, 17, 0);
1561 goto decode_intra_mb;
1562 }
1563 } else {
1564 mb_type= decode_cabac_intra_mb_type(ec, s ,c, 3, 1);
1565 if(s->slice_type == FF_SI_TYPE && mb_type)
1566 mb_type--;
1567 assert(s->slice_type_nos == FF_I_TYPE);
1568 decode_intra_mb:
1569 partition_count = 0;
1570 cbp= i_mb_type_info[mb_type].cbp;
1571 m->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
1572 mb_type= i_mb_type_info[mb_type].type;
1573 }
1575 if(IS_INTRA_PCM(mb_type)) {
1576 const uint8_t *ptr;
1577 // We assume these blocks are very rare so we do not optimize it.
1578 // FIXME The two following lines get the bitstream position in the cabac
1579 // decode, I think it should be done by a function in cabac.h (or cabac.c).
1580 ptr=c->bytestream;
1581 if(c->low&0x1) ptr--;
1582 if(CABAC_BITS==16){
1583 if(c->low&0x1FF) ptr--;
1584 }
1585 //printf("pcm\n");
1586 // The pixels are stored in the same order as levels in h->mb array.
1587 memcpy(m->mb, ptr, 256); ptr+=256;
1588 memcpy(m->mb+128, ptr, 128); ptr+=128;
1590 ff_init_cabac_decoder(c, ptr, c->bytestream_end - ptr);
1592 // All blocks are present
1593 m->cbp= ec->cbp[mb_x] = 0x1ef;
1594 ec->chroma_pred_mode[mb_x] = 0;
1595 // In deblocking, the quantizer is 0
1596 m->qscale_mb_xy = ec->qscale[mb_x]= 0;
1597 // All coeffs are present
1598 memset(ec->non_zero_count[mb_x], 16, 8);
1599 m->mb_type = ec->mb_type[mb_x]= mb_type;
1600 ec->last_qscale_diff = 0;
1602 return 0;
1603 }
1605 fill_decode_caches(ec, s, mb_type);
1607 int mp = 0;
1608 if( IS_INTRA( mb_type ) ) {
1609 int i, pred_mode;
1610 if( IS_INTRA4x4( mb_type ) ) {
1611 if( dct8x8_allowed && get_cabac_noinline(c, &c->cabac_state[399 + ec->neighbor_transform_size] ) ) {
1612 mb_type |= MB_TYPE_8x8DCT;
1613 for( i = 0; i < 16; i+=4 ) {
1614 m->intra4x4_pred_mode[i] = decode_cabac_mb_intra4x4_pred_mode_delta(c);
1615 }
1616 } else {
1617 for( i = 0; i < 16; i++ ) {
1618 m->intra4x4_pred_mode[i] = decode_cabac_mb_intra4x4_pred_mode_delta(c);
1619 }
1620 }
1621 }
1623 m->chroma_pred_mode= ec->chroma_pred_mode[mb_x] =
1624 pred_mode = decode_cabac_mb_chroma_pre_mode( ec, s, c );
1626 } else if( partition_count == 4 ) {
1627 int i, j, sub_partition_count[4], list;
1629 if( s->slice_type_nos == FF_B_TYPE ) {
1630 for( i = 0; i < 4; i++ ) {
1631 m->sub_mb_type[i] = decode_cabac_b_mb_sub_type( c );
1632 sub_partition_count[i]= b_sub_mb_type_info[ m->sub_mb_type[i] ].partition_count;
1633 m->sub_mb_type[i]= b_sub_mb_type_info[ m->sub_mb_type[i] ].type;
1634 }
1635 if( IS_DIRECT(m->sub_mb_type[0] | m->sub_mb_type[1] |
1636 m->sub_mb_type[2] | m->sub_mb_type[3]) ) {
1637 ec->ref_cache[0][scan8[4]] =
1638 ec->ref_cache[1][scan8[4]] =
1639 ec->ref_cache[0][scan8[12]] =
1640 ec->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;
1642 for( i = 0; i < 4; i++ )
1643 fill_rectangle( &ec->direct_cache[scan8[4*i]], 2, 2, 8, (m->sub_mb_type[i]>>1)&0xFF, 1 );
1644 }
1645 } else {
1646 for( i = 0; i < 4; i++ ) {
1647 m->sub_mb_type[i] = decode_cabac_p_mb_sub_type( c );
1648 sub_partition_count[i]= p_sub_mb_type_info[ m->sub_mb_type[i] ].partition_count;
1649 m->sub_mb_type[i]= p_sub_mb_type_info[ m->sub_mb_type[i] ].type;
1650 }
1651 }
1653 for( list = 0; list < s->list_count; list++ ) {
1654 for( i = 0; i < 4; i++ ) {
1655 if(IS_DIRECT(m->sub_mb_type[i])) continue;
1656 if(IS_DIR(m->sub_mb_type[i], 0, list)){
1657 if( s->ref_count[list] > 1 ){
1658 m->ref_index[list][i] = decode_cabac_mb_ref(ec, s, c, list, 4*i );
1659 if(m->ref_index[list][i] >= s->ref_count[list]){
1660 av_log(AV_LOG_ERROR, "Reference %d >= %d\n", m->ref_index[list][i], s->ref_count[list]);
1661 return -1;
1662 }
1663 }else
1664 m->ref_index[list][i] = 0;
1665 } else {
1666 m->ref_index[list][i] = -1;
1667 }
1668 ec->ref_cache[list][ scan8[4*i] ]=ec->ref_cache[list][ scan8[4*i]+1 ]=
1669 ec->ref_cache[list][ scan8[4*i]+8 ]=ec->ref_cache[list][ scan8[4*i]+9 ]= m->ref_index[list][i];
1670 }
1671 }
1673 if(dct8x8_allowed){
1674 // assert(0);
1675 dct8x8_allowed = get_dct8x8_allowed(ec, s);
1676 }
1678 for(list=0; list<s->list_count; list++){
1679 for(i=0; i<4; i++){
1680 // ec->ref_cache[list][ scan8[4*i] ]=ec->ref_cache[list][ scan8[4*i]+1 ];
1681 if(IS_DIRECT(m->sub_mb_type[i])){
1682 fill_rectangle(ec->mvd_cache[list][scan8[4*i]], 2, 2, 8, 0, 2);
1683 continue;
1684 }
1686 if(IS_DIR(m->sub_mb_type[i], 0, list) && !IS_DIRECT(m->sub_mb_type[i])){
1687 const int sub_mb_type= m->sub_mb_type[i];
1688 const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
1689 for(j=0; j<sub_partition_count[i]; j++){
1690 int mpx, mpy;
1691 const int index= 4*i + block_width*j;
1692 uint8_t (* mvd_cache)[2]= &ec->mvd_cache[list][ scan8[index]];
1694 DECODE_CABAC_MB_MVD( ec, c, list, index)
1696 if(IS_SUB_8X8(sub_mb_type)){
1697 mvd_cache[ 1 ][0]=
1698 mvd_cache[ 8 ][0]= mvd_cache[ 9 ][0]= mpx;
1699 mvd_cache[ 1 ][1]=
1700 mvd_cache[ 8 ][1]= mvd_cache[ 9 ][1]= mpy;
1701 }else if(IS_SUB_8X4(sub_mb_type)){
1702 mvd_cache[ 1 ][0]= mpx;
1703 mvd_cache[ 1 ][1]= mpy;
1704 }else if(IS_SUB_4X8(sub_mb_type)){
1705 mvd_cache[ 8 ][0]= mpx;
1706 mvd_cache[ 8 ][1]= mpy;
1707 }
1708 mvd_cache[ 0 ][0]= mpx;
1709 mvd_cache[ 0 ][1]= mpy;
1710 }
1711 }else{
1712 fill_rectangle(ec->mvd_cache[list][ scan8[4*i] ], 2, 2, 8, 0, 2);
1713 }
1714 }
1715 }
1716 } else if( IS_DIRECT(mb_type) ) {
1717 mb_type |= MB_TYPE_16x16;
1718 fill_rectangle(ec->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 2);
1719 fill_rectangle(ec->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 2);
1720 dct8x8_allowed &= s->direct_8x8_inference_flag;
1721 } else {
1722 int list, i;
1723 if(IS_16X16(mb_type)){
1724 for(list=0; list<s->list_count; list++){
1725 if(IS_DIR(mb_type, 0, list)){
1726 int ref;
1727 if(s->ref_count[list] > 1){
1728 ref= decode_cabac_mb_ref(ec, s, c, list, 0);
1729 if(ref >= s->ref_count[list]){
1730 av_log(AV_LOG_ERROR, "Reference %d >= %d\n", ref, s->ref_count[list]);
1731 return -1;
1732 }
1733 }else
1734 ref=0;
1735 m->ref_index[list][0]= ref;
1736 fill_rectangle(&ec->ref_cache[list][ scan8[0] ], 4, 4, 8, ref, 1);
1737 }
1738 }
1739 for(list=0; list<s->list_count; list++){
1740 if(IS_DIR(mb_type, 0, list)){
1741 int mpx,mpy;
1742 DECODE_CABAC_MB_MVD( ec, c, list, 0)
1744 fill_rectangle(ec->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack8to16(mpx,mpy), 2);
1745 }
1747 }
1748 }
1749 else if(IS_16X8(mb_type)){
1750 for(list=0; list<s->list_count; list++){
1751 for(i=0; i<2; i++){
1752 if(IS_DIR(mb_type, i, list)){
1753 int ref;
1754 if(s->ref_count[list] > 1){
1755 ref= decode_cabac_mb_ref(ec, s, c, list, 8*i );
1756 if(ref >= s->ref_count[list]){
1757 av_log(AV_LOG_ERROR, "Reference %d >= %d\n", ref, s->ref_count[list]);
1758 return -1;
1759 }
1760 }else
1761 ref=0;
1762 m->ref_index[list][i]= ref;
1763 fill_rectangle(&ec->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, ref, 1);
1764 }else{
1765 m->ref_index[list][i]= LIST_NOT_USED;
1766 fill_rectangle(&ec->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
1767 }
1768 }
1769 }
1770 for(list=0; list<s->list_count; list++){
1771 for(i=0; i<2; i++){
1772 if(IS_DIR(mb_type, i, list)){
1773 int mpx,mpy;
1774 DECODE_CABAC_MB_MVD( ec, c, list, 8*i)
1776 fill_rectangle(ec->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack8to16(mpx,mpy), 2);
1777 }else{
1778 fill_rectangle(ec->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 2);
1779 }
1780 }
1781 }
1782 }else{
1783 assert(IS_8X16(mb_type));
1784 for(list=0; list<s->list_count; list++){
1785 for(i=0; i<2; i++){
1786 if(IS_DIR(mb_type, i, list)){ //FIXME optimize
1787 int ref;
1788 if(s->ref_count[list] > 1){
1789 ref= decode_cabac_mb_ref(ec, s, c, list, 4*i );
1790 if(ref >= s->ref_count[list]){
1791 av_log(AV_LOG_ERROR, "Reference %d >= %d\n", ref, s->ref_count[list]);
1792 return -1;
1793 }
1794 }else
1795 ref=0;
1796 m->ref_index[list][i]= ref;
1797 fill_rectangle(&ec->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, ref, 1);
1798 }else{
1799 m->ref_index[list][i]= LIST_NOT_USED;
1800 fill_rectangle(&ec->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
1801 }
1802 }
1803 }
1804 for(list=0; list<s->list_count; list++){
1805 for(i=0; i<2; i++){
1806 if(IS_DIR(mb_type, i, list)){
1807 int mpx,mpy;
1808 DECODE_CABAC_MB_MVD( ec, c, list, 4*i)
1810 fill_rectangle(ec->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack8to16(mpx,mpy), 2);
1811 }else{
1812 fill_rectangle(ec->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 2);
1813 }
1814 }
1815 }
1816 }
1817 }
1819 if( IS_INTER( mb_type ) ||(IS_DIRECT(mb_type))) {
1820 ec->chroma_pred_mode[mb_x] = 0;
1821 write_back_motion( ec, s, mb_type );
1822 }
1824 if( !IS_INTRA16x16( mb_type ) ) {
1825 cbp = decode_cabac_mb_cbp_luma( ec, c);
1826 cbp |= decode_cabac_mb_cbp_chroma( ec, c ) << 4;
1827 }
1829 ec->cbp[mb_x] = m->cbp = cbp;
1831 if( dct8x8_allowed && (cbp&15) && !IS_INTRA( mb_type ) ) {
1832 int t = get_cabac_noinline(c, &c->cabac_state[399 + ec->neighbor_transform_size] );
1833 mb_type |= MB_TYPE_8x8DCT * t;
1834 }
1835 m->mb_type = ec->mb_type[mb_x] = mb_type;
1837 if( cbp || IS_INTRA16x16( mb_type ) ) {
1838 const uint8_t *scan, *scan8x8, *dc_scan;
1839 const uint32_t *qmul;
1842 if (s->transform_bypass && ec->curr_qscale){
1843 scan8x8= ff_zigzag_direct;
1844 scan= zigzag_scan;
1845 }else{
1846 scan8x8= ec->zigzag_scan8x8;
1847 scan= ec->zigzag_scan;
1848 }
1849 dc_scan= luma_dc_zigzag_scan;
1851 // decode_cabac_mb_dqp
1852 if(get_cabac_noinline(c, &c->cabac_state[60 + (ec->last_qscale_diff != 0)])){
1853 int val = 1;
1854 int ctx= 2;
1856 while( get_cabac_noinline(c, &c->cabac_state[60 + ctx] ) ) {
1857 ctx= 3;
1858 val++;
1859 if(val > 102){ //prevent infinite loop
1860 av_log(AV_LOG_ERROR, "cabac decode of qscale diff failed at %d %d\n", m->mb_x, m->mb_y);
1861 return -1;
1862 }
1863 }
1865 if( val&0x01 )
1866 val= (val + 1)>>1 ;
1867 else
1868 val= -((val + 1)>>1);
1869 ec->last_qscale_diff = val;
1870 ec->curr_qscale += val;
1871 if(((unsigned)ec->curr_qscale) > 51){
1872 if(ec->curr_qscale<0) ec->curr_qscale+= 52;
1873 else ec->curr_qscale-= 52;
1874 }
1875 ec->chroma_qp[0] = get_chroma_qp( s, 0, ec->curr_qscale);
1876 ec->chroma_qp[1] = get_chroma_qp( s, 1, ec->curr_qscale);
1877 }else
1878 ec->last_qscale_diff=0;
1880 memset(m->mb, 0, 16*16 * sizeof(DCTELEM));
1881 if( IS_INTRA16x16( mb_type ) ) {
1882 int i;
1884 //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 DC\n" );
1885 decode_cabac_residual_dc( ec, s, c, m->mb, 0, 0, dc_scan, 16);
1886 qmul = ec->dequant4_coeff[0][ec->curr_qscale];
1887 if( cbp&15 ) {
1888 for( i = 0; i < 16; i++ ) {
1889 //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 AC:%d\n", i );
1890 decode_cabac_residual_nondc( ec, s, c, m->mb + 16*i, 1, i, scan + 1, qmul, 15);
1891 }
1892 } else {
1893 fill_rectangle(&ec->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
1894 }
1895 h264_luma_dc_dequant_idct_c(m->mb, qmul[0]);
1896 } else {
1898 int i8x8, i4x4;
1899 for( i8x8 = 0; i8x8 < 4; i8x8++ ) {
1900 if( cbp & (1<<i8x8) ) {
1901 if( IS_8x8DCT(mb_type) ) {
1902 decode_cabac_residual_nondc(ec, s, c, m->mb + 64*i8x8, 5, 4*i8x8,
1903 scan8x8, ec->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][ec->curr_qscale], 64);
1904 } else {
1905 qmul = ec->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][ec->curr_qscale];
1906 for( i4x4 = 0; i4x4 < 4; i4x4++ ) {
1907 const int index = 4*i8x8 + i4x4;
1908 //av_log( s->avctx, AV_LOG_ERROR, "Luma4x4: %d\n", index );
1909 //START_TIMER
1910 decode_cabac_residual_nondc(ec, s, c, m->mb + 16*index, 2, index, scan, qmul, 16);
1911 //STOP_TIMER("decode_residual")
1912 }
1913 }
1914 } else {
1915 uint8_t * const nnz= &ec->non_zero_count_cache[ scan8[4*i8x8] ];
1916 nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
1917 }
1918 }
1919 }
1921 if( cbp&0x30 ){
1922 memset(m->mb + 256, 0, 2*64 * sizeof(DCTELEM));
1923 for( int i = 0; i < 2; i++ ) {
1924 const uint32_t dequant4_coeff = ec->dequant4_coeff[IS_INTRA(mb_type) ? 1+i:4+i][ec->chroma_qp[i]][0];
1926 //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-DC\n",c );
1927 decode_cabac_residual_dc(ec, s, c, m->mb + 256 + 16*4*i, 3, i, chroma_dc_scan, 4);
1928 chroma_dc_dequant_idct_c(m->mb + 256 + 16*4*i, dequant4_coeff);
1929 }
1930 }
1932 if( cbp&0x20 ) {
1933 int i, j;
1934 for( i = 0; i < 2; i++ ) {
1935 qmul = ec->dequant4_coeff[i+1+(IS_INTRA( mb_type ) ? 0:3)][ec->chroma_qp[i]];
1936 for( j = 0; j < 4; j++ ) {
1937 const int index = 16 + 4 * i + j;
1938 //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-AC %d\n",c, index - 16 );
1939 decode_cabac_residual_nondc( ec, s, c, m->mb + 16*index, 4, index, scan + 1, qmul, 15);
1940 }
1941 }
1942 } else {
1943 uint8_t * const nnz= &ec->non_zero_count_cache[0];
1944 nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
1945 nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
1946 }
1948 } else {
1949 uint8_t * const nnz= &ec->non_zero_count_cache[0];
1950 fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
1951 nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
1952 nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
1953 ec->last_qscale_diff = 0;
1954 }
1956 m->qscale_mb_xy = ec->qscale[mb_x]= ec->curr_qscale;
1957 write_back_non_zero_count(ec, s);
1960 return 0;
1961 }
1963 void free_entropy_context(EntropyContext *ec){
1964 av_freep(&ec->non_zero_count_row[0]);
1965 av_freep(&ec->non_zero_count_row[1]);
1966 av_freep(&ec->mvd_table[0][0]);
1967 av_freep(&ec->mvd_table[0][1]);
1968 av_freep(&ec->mvd_table[1][0]);
1969 av_freep(&ec->mvd_table[1][1]);
1971 av_freep(&ec->direct_table[0]);
1972 av_freep(&ec->direct_table[1]);
1973 av_freep(&ec->chroma_pred_mode_table[0]);
1974 av_freep(&ec->chroma_pred_mode_table[1]);
1975 av_freep(&ec->cbp_table[0]);
1976 av_freep(&ec->cbp_table[1]);
1977 av_freep(&ec->qscale_table[0]);
1978 av_freep(&ec->qscale_table[1]);
1980 av_freep(&ec->mb_type_table[0]);
1981 av_freep(&ec->mb_type_table[1]);
1982 av_freep(&ec->ref_index_table[0][0]);
1983 av_freep(&ec->ref_index_table[0][1]);
1984 av_freep(&ec->ref_index_table[1][0]);
1985 av_freep(&ec->ref_index_table[1][1]);
1988 av_free(ec);
1989 }
1991 EntropyContext *get_entropy_context(H264Context *h){
1992 const int mb_height = h->mb_height;
1993 const int mb_width = h->mb_width;
1994 const int mb_stride = h->mb_stride;
1996 EntropyContext *ec = av_mallocz(sizeof(EntropyContext));
1998 ec->mb_width = mb_width;
1999 ec->mb_height = mb_height;
2000 ec->b_stride = mb_width*4;
2001 ec->mb_stride = mb_stride;
2003 FF_ALLOCZ_OR_GOTO(ec->non_zero_count_row[0], mb_stride * 8 * sizeof(uint8_t), fail)
2004 FF_ALLOCZ_OR_GOTO(ec->non_zero_count_row[1], mb_stride * 8 * sizeof(uint8_t), fail)
2006 FF_ALLOCZ_OR_GOTO(ec->mvd_table[0][0], 16*mb_stride * sizeof(uint8_t), fail);
2007 FF_ALLOCZ_OR_GOTO(ec->mvd_table[0][1], 16*mb_stride * sizeof(uint8_t), fail);
2008 FF_ALLOCZ_OR_GOTO(ec->mvd_table[1][0], 16*mb_stride * sizeof(uint8_t), fail);
2009 FF_ALLOCZ_OR_GOTO(ec->mvd_table[1][1], 16*mb_stride * sizeof(uint8_t), fail);
2011 FF_ALLOCZ_OR_GOTO(ec->direct_table[0], 4*mb_stride * sizeof(uint8_t) , fail);
2012 FF_ALLOCZ_OR_GOTO(ec->direct_table[1], 4*mb_stride * sizeof(uint8_t) , fail);
2014 FF_ALLOCZ_OR_GOTO(ec->chroma_pred_mode_table[0], mb_stride * sizeof(uint8_t), fail)
2015 FF_ALLOCZ_OR_GOTO(ec->chroma_pred_mode_table[1], mb_stride * sizeof(uint8_t), fail)
2017 FF_ALLOCZ_OR_GOTO(ec->cbp_table[0], mb_stride * sizeof(uint16_t), fail)
2018 FF_ALLOCZ_OR_GOTO(ec->cbp_table[1], mb_stride * sizeof(uint16_t), fail)
2020 FF_ALLOCZ_OR_GOTO(ec->qscale_table[0], mb_stride * sizeof(uint8_t) , fail)
2021 FF_ALLOCZ_OR_GOTO(ec->qscale_table[1], mb_stride * sizeof(uint8_t) , fail)
2023 FF_ALLOCZ_OR_GOTO(ec->mb_type_table[0] , (mb_stride+1) * sizeof(uint32_t), fail)
2024 FF_ALLOCZ_OR_GOTO(ec->mb_type_table[1] , (mb_stride+1) * sizeof(uint32_t), fail)
2026 FF_ALLOCZ_OR_GOTO(ec->ref_index_table[0][0], 4*mb_stride * sizeof(int8_t), fail)
2027 FF_ALLOCZ_OR_GOTO(ec->ref_index_table[1][0], 4*mb_stride * sizeof(int8_t), fail)
2028 FF_ALLOCZ_OR_GOTO(ec->ref_index_table[0][1], 4*mb_stride * sizeof(int8_t), fail)
2029 FF_ALLOCZ_OR_GOTO(ec->ref_index_table[1][1], 4*mb_stride * sizeof(int8_t), fail)
2031 ec->zigzag_scan = h->zigzag_scan;
2032 ec->zigzag_scan8x8 = h->zigzag_scan8x8;
2034 return ec;
2035 fail:
2036 free_entropy_context(ec);
2037 return NULL;
2038 }
2040 void init_entropy_buf(EntropyContext *ec, H264Slice *s, int line){
2041 int top = (line+1)%2;
2042 int cur = line%2;
2044 ec->non_zero_count_top = ec->non_zero_count_row[top];
2045 ec->non_zero_count = ec->non_zero_count_row[cur];
2046 ec->mvd_top[0] = ec->mvd_table[0][top];
2047 ec->mvd[0] = ec->mvd_table[0][cur];
2048 ec->mvd_top[1] = ec->mvd_table[1][top];
2049 ec->mvd[1] = ec->mvd_table[1][cur];
2050 ec->direct_top = ec->direct_table[top];
2051 ec->direct = ec->direct_table[cur];
2052 ec->chroma_pred_mode_top = ec->chroma_pred_mode_table[top];
2053 ec->chroma_pred_mode = ec->chroma_pred_mode_table[cur];
2054 ec->cbp_top = ec->cbp_table[top];
2055 ec->cbp = ec->cbp_table[cur];
2056 ec->qscale_top = ec->qscale_table[top] +1;
2057 ec->qscale = ec->qscale_table[cur] +1;
2058 ec->mb_type_top = ec->mb_type_table[top]+1;
2059 ec->mb_type = ec->mb_type_table[cur]+1;
2060 ec->ref_index_top[0] = ec->ref_index_table[0][top];
2061 ec->ref_index_top[1] = ec->ref_index_table[1][top];
2062 ec->ref_index[0] = ec->ref_index_table[0][cur];
2063 ec->ref_index[1] = ec->ref_index_table[1][cur];
2065 }
