10866
|
1 /*
|
|
2 * H.26L/H.264/AVC/JVT/14496-10/... cavlc bitstream 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 */
|
|
21
|
|
22 /**
|
|
23 * @file libavcodec/h264_cavlc.c
|
|
24 * H.264 / AVC / MPEG4 part10 cavlc bitstream decoding.
|
|
25 * @author Michael Niedermayer <michaelni@gmx.at>
|
|
26 */
|
|
27
|
|
28 #include "internal.h"
|
|
29 #include "avcodec.h"
|
|
30 #include "mpegvideo.h"
|
|
31 #include "h264.h"
|
|
32 #include "h264data.h" // FIXME FIXME FIXME
|
|
33 #include "h264_mvpred.h"
|
|
34 #include "golomb.h"
|
|
35
|
|
36 #if ARCH_X86
|
|
37 #include "x86/h264_i386.h"
|
|
38 #endif
|
|
39
|
|
40 //#undef NDEBUG
|
|
41 #include <assert.h>
|
|
42
|
|
43 static const uint8_t golomb_to_inter_cbp_gray[16]={
|
|
44 0, 1, 2, 4, 8, 3, 5,10,12,15, 7,11,13,14, 6, 9,
|
|
45 };
|
|
46
|
|
47 static const uint8_t golomb_to_intra4x4_cbp_gray[16]={
|
|
48 15, 0, 7,11,13,14, 3, 5,10,12, 1, 2, 4, 8, 6, 9,
|
|
49 };
|
|
50
|
|
51 static const uint8_t chroma_dc_coeff_token_len[4*5]={
|
|
52 2, 0, 0, 0,
|
|
53 6, 1, 0, 0,
|
|
54 6, 6, 3, 0,
|
|
55 6, 7, 7, 6,
|
|
56 6, 8, 8, 7,
|
|
57 };
|
|
58
|
|
59 static const uint8_t chroma_dc_coeff_token_bits[4*5]={
|
|
60 1, 0, 0, 0,
|
|
61 7, 1, 0, 0,
|
|
62 4, 6, 1, 0,
|
|
63 3, 3, 2, 5,
|
|
64 2, 3, 2, 0,
|
|
65 };
|
|
66
|
|
67 static const uint8_t coeff_token_len[4][4*17]={
|
|
68 {
|
|
69 1, 0, 0, 0,
|
|
70 6, 2, 0, 0, 8, 6, 3, 0, 9, 8, 7, 5, 10, 9, 8, 6,
|
|
71 11,10, 9, 7, 13,11,10, 8, 13,13,11, 9, 13,13,13,10,
|
|
72 14,14,13,11, 14,14,14,13, 15,15,14,14, 15,15,15,14,
|
|
73 16,15,15,15, 16,16,16,15, 16,16,16,16, 16,16,16,16,
|
|
74 },
|
|
75 {
|
|
76 2, 0, 0, 0,
|
|
77 6, 2, 0, 0, 6, 5, 3, 0, 7, 6, 6, 4, 8, 6, 6, 4,
|
|
78 8, 7, 7, 5, 9, 8, 8, 6, 11, 9, 9, 6, 11,11,11, 7,
|
|
79 12,11,11, 9, 12,12,12,11, 12,12,12,11, 13,13,13,12,
|
|
80 13,13,13,13, 13,14,13,13, 14,14,14,13, 14,14,14,14,
|
|
81 },
|
|
82 {
|
|
83 4, 0, 0, 0,
|
|
84 6, 4, 0, 0, 6, 5, 4, 0, 6, 5, 5, 4, 7, 5, 5, 4,
|
|
85 7, 5, 5, 4, 7, 6, 6, 4, 7, 6, 6, 4, 8, 7, 7, 5,
|
|
86 8, 8, 7, 6, 9, 8, 8, 7, 9, 9, 8, 8, 9, 9, 9, 8,
|
|
87 10, 9, 9, 9, 10,10,10,10, 10,10,10,10, 10,10,10,10,
|
|
88 },
|
|
89 {
|
|
90 6, 0, 0, 0,
|
|
91 6, 6, 0, 0, 6, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6,
|
|
92 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
|
|
93 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
|
|
94 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
|
|
95 }
|
|
96 };
|
|
97
|
|
98 static const uint8_t coeff_token_bits[4][4*17]={
|
|
99 {
|
|
100 1, 0, 0, 0,
|
|
101 5, 1, 0, 0, 7, 4, 1, 0, 7, 6, 5, 3, 7, 6, 5, 3,
|
|
102 7, 6, 5, 4, 15, 6, 5, 4, 11,14, 5, 4, 8,10,13, 4,
|
|
103 15,14, 9, 4, 11,10,13,12, 15,14, 9,12, 11,10,13, 8,
|
|
104 15, 1, 9,12, 11,14,13, 8, 7,10, 9,12, 4, 6, 5, 8,
|
|
105 },
|
|
106 {
|
|
107 3, 0, 0, 0,
|
|
108 11, 2, 0, 0, 7, 7, 3, 0, 7,10, 9, 5, 7, 6, 5, 4,
|
|
109 4, 6, 5, 6, 7, 6, 5, 8, 15, 6, 5, 4, 11,14,13, 4,
|
|
110 15,10, 9, 4, 11,14,13,12, 8,10, 9, 8, 15,14,13,12,
|
|
111 11,10, 9,12, 7,11, 6, 8, 9, 8,10, 1, 7, 6, 5, 4,
|
|
112 },
|
|
113 {
|
|
114 15, 0, 0, 0,
|
|
115 15,14, 0, 0, 11,15,13, 0, 8,12,14,12, 15,10,11,11,
|
|
116 11, 8, 9,10, 9,14,13, 9, 8,10, 9, 8, 15,14,13,13,
|
|
117 11,14,10,12, 15,10,13,12, 11,14, 9,12, 8,10,13, 8,
|
|
118 13, 7, 9,12, 9,12,11,10, 5, 8, 7, 6, 1, 4, 3, 2,
|
|
119 },
|
|
120 {
|
|
121 3, 0, 0, 0,
|
|
122 0, 1, 0, 0, 4, 5, 6, 0, 8, 9,10,11, 12,13,14,15,
|
|
123 16,17,18,19, 20,21,22,23, 24,25,26,27, 28,29,30,31,
|
|
124 32,33,34,35, 36,37,38,39, 40,41,42,43, 44,45,46,47,
|
|
125 48,49,50,51, 52,53,54,55, 56,57,58,59, 60,61,62,63,
|
|
126 }
|
|
127 };
|
|
128
|
|
129 static const uint8_t total_zeros_len[16][16]= {
|
|
130 {1,3,3,4,4,5,5,6,6,7,7,8,8,9,9,9},
|
|
131 {3,3,3,3,3,4,4,4,4,5,5,6,6,6,6},
|
|
132 {4,3,3,3,4,4,3,3,4,5,5,6,5,6},
|
|
133 {5,3,4,4,3,3,3,4,3,4,5,5,5},
|
|
134 {4,4,4,3,3,3,3,3,4,5,4,5},
|
|
135 {6,5,3,3,3,3,3,3,4,3,6},
|
|
136 {6,5,3,3,3,2,3,4,3,6},
|
|
137 {6,4,5,3,2,2,3,3,6},
|
|
138 {6,6,4,2,2,3,2,5},
|
|
139 {5,5,3,2,2,2,4},
|
|
140 {4,4,3,3,1,3},
|
|
141 {4,4,2,1,3},
|
|
142 {3,3,1,2},
|
|
143 {2,2,1},
|
|
144 {1,1},
|
|
145 };
|
|
146
|
|
147 static const uint8_t total_zeros_bits[16][16]= {
|
|
148 {1,3,2,3,2,3,2,3,2,3,2,3,2,3,2,1},
|
|
149 {7,6,5,4,3,5,4,3,2,3,2,3,2,1,0},
|
|
150 {5,7,6,5,4,3,4,3,2,3,2,1,1,0},
|
|
151 {3,7,5,4,6,5,4,3,3,2,2,1,0},
|
|
152 {5,4,3,7,6,5,4,3,2,1,1,0},
|
|
153 {1,1,7,6,5,4,3,2,1,1,0},
|
|
154 {1,1,5,4,3,3,2,1,1,0},
|
|
155 {1,1,1,3,3,2,2,1,0},
|
|
156 {1,0,1,3,2,1,1,1},
|
|
157 {1,0,1,3,2,1,1},
|
|
158 {0,1,1,2,1,3},
|
|
159 {0,1,1,1,1},
|
|
160 {0,1,1,1},
|
|
161 {0,1,1},
|
|
162 {0,1},
|
|
163 };
|
|
164
|
|
165 static const uint8_t chroma_dc_total_zeros_len[3][4]= {
|
|
166 { 1, 2, 3, 3,},
|
|
167 { 1, 2, 2, 0,},
|
|
168 { 1, 1, 0, 0,},
|
|
169 };
|
|
170
|
|
171 static const uint8_t chroma_dc_total_zeros_bits[3][4]= {
|
|
172 { 1, 1, 1, 0,},
|
|
173 { 1, 1, 0, 0,},
|
|
174 { 1, 0, 0, 0,},
|
|
175 };
|
|
176
|
|
177 static const uint8_t run_len[7][16]={
|
|
178 {1,1},
|
|
179 {1,2,2},
|
|
180 {2,2,2,2},
|
|
181 {2,2,2,3,3},
|
|
182 {2,2,3,3,3,3},
|
|
183 {2,3,3,3,3,3,3},
|
|
184 {3,3,3,3,3,3,3,4,5,6,7,8,9,10,11},
|
|
185 };
|
|
186
|
|
187 static const uint8_t run_bits[7][16]={
|
|
188 {1,0},
|
|
189 {1,1,0},
|
|
190 {3,2,1,0},
|
|
191 {3,2,1,1,0},
|
|
192 {3,2,3,2,1,0},
|
|
193 {3,0,1,3,2,5,4},
|
|
194 {7,6,5,4,3,2,1,1,1,1,1,1,1,1,1},
|
|
195 };
|
|
196
|
|
197 static VLC coeff_token_vlc[4];
|
|
198 static VLC_TYPE coeff_token_vlc_tables[520+332+280+256][2];
|
|
199 static const int coeff_token_vlc_tables_size[4]={520,332,280,256};
|
|
200
|
|
201 static VLC chroma_dc_coeff_token_vlc;
|
|
202 static VLC_TYPE chroma_dc_coeff_token_vlc_table[256][2];
|
|
203 static const int chroma_dc_coeff_token_vlc_table_size = 256;
|
|
204
|
|
205 static VLC total_zeros_vlc[15];
|
|
206 static VLC_TYPE total_zeros_vlc_tables[15][512][2];
|
|
207 static const int total_zeros_vlc_tables_size = 512;
|
|
208
|
|
209 static VLC chroma_dc_total_zeros_vlc[3];
|
|
210 static VLC_TYPE chroma_dc_total_zeros_vlc_tables[3][8][2];
|
|
211 static const int chroma_dc_total_zeros_vlc_tables_size = 8;
|
|
212
|
|
213 static VLC run_vlc[6];
|
|
214 static VLC_TYPE run_vlc_tables[6][8][2];
|
|
215 static const int run_vlc_tables_size = 8;
|
|
216
|
|
217 static VLC run7_vlc;
|
|
218 static VLC_TYPE run7_vlc_table[96][2];
|
|
219 static const int run7_vlc_table_size = 96;
|
|
220
|
|
221 #define LEVEL_TAB_BITS 8
|
|
222 static int8_t cavlc_level_tab[7][1<<LEVEL_TAB_BITS][2];
|
|
223
|
|
224
|
|
225 /**
|
|
226 * gets the predicted number of non-zero coefficients.
|
|
227 * @param n block index
|
|
228 */
|
|
229 static inline int pred_non_zero_count(H264Context *h, int n){
|
|
230 const int index8= scan8[n];
|
|
231 const int left= h->non_zero_count_cache[index8 - 1];
|
|
232 const int top = h->non_zero_count_cache[index8 - 8];
|
|
233 int i= left + top;
|
|
234
|
|
235 if(i<64) i= (i+1)>>1;
|
|
236
|
|
237 tprintf(h->s.avctx, "pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
|
|
238
|
|
239 return i&31;
|
|
240 }
|
|
241
|
|
242 static av_cold void init_cavlc_level_tab(void){
|
|
243 int suffix_length, mask;
|
|
244 unsigned int i;
|
|
245
|
|
246 for(suffix_length=0; suffix_length<7; suffix_length++){
|
|
247 for(i=0; i<(1<<LEVEL_TAB_BITS); i++){
|
|
248 int prefix= LEVEL_TAB_BITS - av_log2(2*i);
|
|
249 int level_code= (prefix<<suffix_length) + (i>>(LEVEL_TAB_BITS-prefix-1-suffix_length)) - (1<<suffix_length);
|
|
250
|
|
251 mask= -(level_code&1);
|
|
252 level_code= (((2+level_code)>>1) ^ mask) - mask;
|
|
253 if(prefix + 1 + suffix_length <= LEVEL_TAB_BITS){
|
|
254 cavlc_level_tab[suffix_length][i][0]= level_code;
|
|
255 cavlc_level_tab[suffix_length][i][1]= prefix + 1 + suffix_length;
|
|
256 }else if(prefix + 1 <= LEVEL_TAB_BITS){
|
|
257 cavlc_level_tab[suffix_length][i][0]= prefix+100;
|
|
258 cavlc_level_tab[suffix_length][i][1]= prefix + 1;
|
|
259 }else{
|
|
260 cavlc_level_tab[suffix_length][i][0]= LEVEL_TAB_BITS+100;
|
|
261 cavlc_level_tab[suffix_length][i][1]= LEVEL_TAB_BITS;
|
|
262 }
|
|
263 }
|
|
264 }
|
|
265 }
|
|
266
|
|
267 av_cold void ff_h264_decode_init_vlc(void){
|
|
268 static int done = 0;
|
|
269
|
|
270 if (!done) {
|
|
271 int i;
|
|
272 int offset;
|
|
273 done = 1;
|
|
274
|
|
275 chroma_dc_coeff_token_vlc.table = chroma_dc_coeff_token_vlc_table;
|
|
276 chroma_dc_coeff_token_vlc.table_allocated = chroma_dc_coeff_token_vlc_table_size;
|
|
277 init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5,
|
|
278 &chroma_dc_coeff_token_len [0], 1, 1,
|
|
279 &chroma_dc_coeff_token_bits[0], 1, 1,
|
|
280 INIT_VLC_USE_NEW_STATIC);
|
|
281
|
|
282 offset = 0;
|
|
283 for(i=0; i<4; i++){
|
|
284 coeff_token_vlc[i].table = coeff_token_vlc_tables+offset;
|
|
285 coeff_token_vlc[i].table_allocated = coeff_token_vlc_tables_size[i];
|
|
286 init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17,
|
|
287 &coeff_token_len [i][0], 1, 1,
|
|
288 &coeff_token_bits[i][0], 1, 1,
|
|
289 INIT_VLC_USE_NEW_STATIC);
|
|
290 offset += coeff_token_vlc_tables_size[i];
|
|
291 }
|
|
292 /*
|
|
293 * This is a one time safety check to make sure that
|
|
294 * the packed static coeff_token_vlc table sizes
|
|
295 * were initialized correctly.
|
|
296 */
|
|
297 assert(offset == FF_ARRAY_ELEMS(coeff_token_vlc_tables));
|
|
298
|
|
299 for(i=0; i<3; i++){
|
|
300 chroma_dc_total_zeros_vlc[i].table = chroma_dc_total_zeros_vlc_tables[i];
|
|
301 chroma_dc_total_zeros_vlc[i].table_allocated = chroma_dc_total_zeros_vlc_tables_size;
|
|
302 init_vlc(&chroma_dc_total_zeros_vlc[i],
|
|
303 CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
|
|
304 &chroma_dc_total_zeros_len [i][0], 1, 1,
|
|
305 &chroma_dc_total_zeros_bits[i][0], 1, 1,
|
|
306 INIT_VLC_USE_NEW_STATIC);
|
|
307 }
|
|
308 for(i=0; i<15; i++){
|
|
309 total_zeros_vlc[i].table = total_zeros_vlc_tables[i];
|
|
310 total_zeros_vlc[i].table_allocated = total_zeros_vlc_tables_size;
|
|
311 init_vlc(&total_zeros_vlc[i],
|
|
312 TOTAL_ZEROS_VLC_BITS, 16,
|
|
313 &total_zeros_len [i][0], 1, 1,
|
|
314 &total_zeros_bits[i][0], 1, 1,
|
|
315 INIT_VLC_USE_NEW_STATIC);
|
|
316 }
|
|
317
|
|
318 for(i=0; i<6; i++){
|
|
319 run_vlc[i].table = run_vlc_tables[i];
|
|
320 run_vlc[i].table_allocated = run_vlc_tables_size;
|
|
321 init_vlc(&run_vlc[i],
|
|
322 RUN_VLC_BITS, 7,
|
|
323 &run_len [i][0], 1, 1,
|
|
324 &run_bits[i][0], 1, 1,
|
|
325 INIT_VLC_USE_NEW_STATIC);
|
|
326 }
|
|
327 run7_vlc.table = run7_vlc_table,
|
|
328 run7_vlc.table_allocated = run7_vlc_table_size;
|
|
329 init_vlc(&run7_vlc, RUN7_VLC_BITS, 16,
|
|
330 &run_len [6][0], 1, 1,
|
|
331 &run_bits[6][0], 1, 1,
|
|
332 INIT_VLC_USE_NEW_STATIC);
|
|
333
|
|
334 init_cavlc_level_tab();
|
|
335 }
|
|
336 }
|
|
337
|
|
338 /**
|
|
339 *
|
|
340 */
|
|
341 static inline int get_level_prefix(GetBitContext *gb){
|
|
342 unsigned int buf;
|
|
343 int log;
|
|
344
|
|
345 OPEN_READER(re, gb);
|
|
346 UPDATE_CACHE(re, gb);
|
|
347 buf=GET_CACHE(re, gb);
|
|
348
|
|
349 log= 32 - av_log2(buf);
|
|
350 #ifdef TRACE
|
|
351 print_bin(buf>>(32-log), log);
|
|
352 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d lpr @%5d in %s get_level_prefix\n", buf>>(32-log), log, log-1, get_bits_count(gb), __FILE__);
|
|
353 #endif
|
|
354
|
|
355 LAST_SKIP_BITS(re, gb, log);
|
|
356 CLOSE_READER(re, gb);
|
|
357
|
|
358 return log-1;
|
|
359 }
|
|
360
|
|
361 /**
|
|
362 * decodes a residual block.
|
|
363 * @param n block index
|
|
364 * @param scantable scantable
|
|
365 * @param max_coeff number of coefficients in the block
|
|
366 * @return <0 if an error occurred
|
|
367 */
|
|
368 static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff){
|
|
369 MpegEncContext * const s = &h->s;
|
|
370 static const int coeff_token_table_index[17]= {0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3};
|
|
371 int level[16];
|
|
372 int zeros_left, coeff_num, coeff_token, total_coeff, i, j, trailing_ones, run_before;
|
|
373
|
|
374 //FIXME put trailing_onex into the context
|
|
375
|
|
376 if(n == CHROMA_DC_BLOCK_INDEX){
|
|
377 coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1);
|
|
378 total_coeff= coeff_token>>2;
|
|
379 }else{
|
|
380 if(n == LUMA_DC_BLOCK_INDEX){
|
|
381 total_coeff= pred_non_zero_count(h, 0);
|
|
382 coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
|
|
383 total_coeff= coeff_token>>2;
|
|
384 }else{
|
|
385 total_coeff= pred_non_zero_count(h, n);
|
|
386 coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2);
|
|
387 total_coeff= coeff_token>>2;
|
|
388 h->non_zero_count_cache[ scan8[n] ]= total_coeff;
|
|
389 }
|
|
390 }
|
|
391
|
|
392 //FIXME set last_non_zero?
|
|
393
|
|
394 if(total_coeff==0)
|
|
395 return 0;
|
|
396 if(total_coeff > (unsigned)max_coeff) {
|
|
397 av_log(h->s.avctx, AV_LOG_ERROR, "corrupted macroblock %d %d (total_coeff=%d)\n", s->mb_x, s->mb_y, total_coeff);
|
|
398 return -1;
|
|
399 }
|
|
400
|
|
401 trailing_ones= coeff_token&3;
|
|
402 tprintf(h->s.avctx, "trailing:%d, total:%d\n", trailing_ones, total_coeff);
|
|
403 assert(total_coeff<=16);
|
|
404
|
|
405 i = show_bits(gb, 3);
|
|
406 skip_bits(gb, trailing_ones);
|
|
407 level[0] = 1-((i&4)>>1);
|
|
408 level[1] = 1-((i&2) );
|
|
409 level[2] = 1-((i&1)<<1);
|
|
410
|
|
411 if(trailing_ones<total_coeff) {
|
|
412 int mask, prefix;
|
|
413 int suffix_length = total_coeff > 10 && trailing_ones < 3;
|
|
414 int bitsi= show_bits(gb, LEVEL_TAB_BITS);
|
|
415 int level_code= cavlc_level_tab[suffix_length][bitsi][0];
|
|
416
|
|
417 skip_bits(gb, cavlc_level_tab[suffix_length][bitsi][1]);
|
|
418 if(level_code >= 100){
|
|
419 prefix= level_code - 100;
|
|
420 if(prefix == LEVEL_TAB_BITS)
|
|
421 prefix += get_level_prefix(gb);
|
|
422
|
|
423 //first coefficient has suffix_length equal to 0 or 1
|
|
424 if(prefix<14){ //FIXME try to build a large unified VLC table for all this
|
|
425 if(suffix_length)
|
|
426 level_code= (prefix<<1) + get_bits1(gb); //part
|
|
427 else
|
|
428 level_code= prefix; //part
|
|
429 }else if(prefix==14){
|
|
430 if(suffix_length)
|
|
431 level_code= (prefix<<1) + get_bits1(gb); //part
|
|
432 else
|
|
433 level_code= prefix + get_bits(gb, 4); //part
|
|
434 }else{
|
|
435 level_code= 30 + get_bits(gb, prefix-3); //part
|
|
436 if(prefix>=16)
|
|
437 level_code += (1<<(prefix-3))-4096;
|
|
438 }
|
|
439
|
|
440 if(trailing_ones < 3) level_code += 2;
|
|
441
|
|
442 suffix_length = 2;
|
|
443 mask= -(level_code&1);
|
|
444 level[trailing_ones]= (((2+level_code)>>1) ^ mask) - mask;
|
|
445 }else{
|
|
446 if(trailing_ones < 3) level_code += (level_code>>31)|1;
|
|
447
|
|
448 suffix_length = 1;
|
|
449 if(level_code + 3U > 6U)
|
|
450 suffix_length++;
|
|
451 level[trailing_ones]= level_code;
|
|
452 }
|
|
453
|
|
454 //remaining coefficients have suffix_length > 0
|
|
455 for(i=trailing_ones+1;i<total_coeff;i++) {
|
|
456 static const unsigned int suffix_limit[7] = {0,3,6,12,24,48,INT_MAX };
|
|
457 int bitsi= show_bits(gb, LEVEL_TAB_BITS);
|
|
458 level_code= cavlc_level_tab[suffix_length][bitsi][0];
|
|
459
|
|
460 skip_bits(gb, cavlc_level_tab[suffix_length][bitsi][1]);
|
|
461 if(level_code >= 100){
|
|
462 prefix= level_code - 100;
|
|
463 if(prefix == LEVEL_TAB_BITS){
|
|
464 prefix += get_level_prefix(gb);
|
|
465 }
|
|
466 if(prefix<15){
|
|
467 level_code = (prefix<<suffix_length) + get_bits(gb, suffix_length);
|
|
468 }else{
|
|
469 level_code = (15<<suffix_length) + get_bits(gb, prefix-3);
|
|
470 if(prefix>=16)
|
|
471 level_code += (1<<(prefix-3))-4096;
|
|
472 }
|
|
473 mask= -(level_code&1);
|
|
474 level_code= (((2+level_code)>>1) ^ mask) - mask;
|
|
475 }
|
|
476 level[i]= level_code;
|
|
477
|
|
478 if(suffix_limit[suffix_length] + level_code > 2U*suffix_limit[suffix_length])
|
|
479 suffix_length++;
|
|
480 }
|
|
481 }
|
|
482
|
|
483 if(total_coeff == max_coeff)
|
|
484 zeros_left=0;
|
|
485 else{
|
|
486 if(n == CHROMA_DC_BLOCK_INDEX)
|
|
487 zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1);
|
|
488 else
|
|
489 zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1);
|
|
490 }
|
|
491
|
|
492 coeff_num = zeros_left + total_coeff - 1;
|
|
493 j = scantable[coeff_num];
|
|
494 if(n > 24){
|
|
495 block[j] = level[0];
|
|
496 for(i=1;i<total_coeff;i++) {
|
|
497 if(zeros_left <= 0)
|
|
498 run_before = 0;
|
|
499 else if(zeros_left < 7){
|
|
500 run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
|
|
501 }else{
|
|
502 run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
|
|
503 }
|
|
504 zeros_left -= run_before;
|
|
505 coeff_num -= 1 + run_before;
|
|
506 j= scantable[ coeff_num ];
|
|
507
|
|
508 block[j]= level[i];
|
|
509 }
|
|
510 }else{
|
|
511 block[j] = (level[0] * qmul[j] + 32)>>6;
|
|
512 for(i=1;i<total_coeff;i++) {
|
|
513 if(zeros_left <= 0)
|
|
514 run_before = 0;
|
|
515 else if(zeros_left < 7){
|
|
516 run_before= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1);
|
|
517 }else{
|
|
518 run_before= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2);
|
|
519 }
|
|
520 zeros_left -= run_before;
|
|
521 coeff_num -= 1 + run_before;
|
|
522 j= scantable[ coeff_num ];
|
|
523
|
|
524 block[j]= (level[i] * qmul[j] + 32)>>6;
|
|
525 }
|
|
526 }
|
|
527
|
|
528 if(zeros_left<0){
|
|
529 av_log(h->s.avctx, AV_LOG_ERROR, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y);
|
|
530 return -1;
|
|
531 }
|
|
532
|
|
533 return 0;
|
|
534 }
|
|
535
|
|
536 int ff_h264_decode_mb_cavlc(H264Context *h){
|
|
537 MpegEncContext * const s = &h->s;
|
|
538 int mb_xy;
|
|
539 int partition_count;
|
|
540 unsigned int mb_type, cbp;
|
|
541 int dct8x8_allowed= h->pps.transform_8x8_mode;
|
|
542
|
|
543 mb_xy = h->mb_xy = s->mb_x + s->mb_y*s->mb_stride;
|
|
544
|
|
545 tprintf(s->avctx, "pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
|
|
546 cbp = 0; /* avoid warning. FIXME: find a solution without slowing
|
|
547 down the code */
|
|
548 if(h->slice_type_nos != FF_I_TYPE){
|
|
549 if(s->mb_skip_run==-1)
|
|
550 s->mb_skip_run= get_ue_golomb(&s->gb);
|
|
551
|
|
552 if (s->mb_skip_run--) {
|
|
553 if(FRAME_MBAFF && (s->mb_y&1) == 0){
|
|
554 if(s->mb_skip_run==0)
|
|
555 h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
|
|
556 else
|
|
557 predict_field_decoding_flag(h);
|
|
558 }
|
|
559 decode_mb_skip(h);
|
|
560 return 0;
|
|
561 }
|
|
562 }
|
|
563 if(FRAME_MBAFF){
|
|
564 if( (s->mb_y&1) == 0 )
|
|
565 h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
|
|
566 }
|
|
567
|
|
568 h->prev_mb_skipped= 0;
|
|
569
|
|
570 mb_type= get_ue_golomb(&s->gb);
|
|
571 if(h->slice_type_nos == FF_B_TYPE){
|
|
572 if(mb_type < 23){
|
|
573 partition_count= b_mb_type_info[mb_type].partition_count;
|
|
574 mb_type= b_mb_type_info[mb_type].type;
|
|
575 }else{
|
|
576 mb_type -= 23;
|
|
577 goto decode_intra_mb;
|
|
578 }
|
|
579 }else if(h->slice_type_nos == FF_P_TYPE){
|
|
580 if(mb_type < 5){
|
|
581 partition_count= p_mb_type_info[mb_type].partition_count;
|
|
582 mb_type= p_mb_type_info[mb_type].type;
|
|
583 }else{
|
|
584 mb_type -= 5;
|
|
585 goto decode_intra_mb;
|
|
586 }
|
|
587 }else{
|
|
588 assert(h->slice_type_nos == FF_I_TYPE);
|
|
589 if(h->slice_type == FF_SI_TYPE && mb_type)
|
|
590 mb_type--;
|
|
591 decode_intra_mb:
|
|
592 if(mb_type > 25){
|
|
593 av_log(h->s.avctx, AV_LOG_ERROR, "mb_type %d in %c slice too large at %d %d\n", mb_type, av_get_pict_type_char(h->slice_type), s->mb_x, s->mb_y);
|
|
594 return -1;
|
|
595 }
|
|
596 partition_count=0;
|
|
597 cbp= i_mb_type_info[mb_type].cbp;
|
|
598 h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
|
|
599 mb_type= i_mb_type_info[mb_type].type;
|
|
600 }
|
|
601
|
|
602 if(MB_FIELD)
|
|
603 mb_type |= MB_TYPE_INTERLACED;
|
|
604
|
|
605 h->slice_table[ mb_xy ]= h->slice_num;
|
|
606
|
|
607 if(IS_INTRA_PCM(mb_type)){
|
|
608 unsigned int x;
|
|
609
|
|
610 // We assume these blocks are very rare so we do not optimize it.
|
|
611 align_get_bits(&s->gb);
|
|
612
|
|
613 // The pixels are stored in the same order as levels in h->mb array.
|
|
614 for(x=0; x < (CHROMA ? 384 : 256); x++){
|
|
615 ((uint8_t*)h->mb)[x]= get_bits(&s->gb, 8);
|
|
616 }
|
|
617
|
|
618 // In deblocking, the quantizer is 0
|
|
619 s->current_picture.qscale_table[mb_xy]= 0;
|
|
620 // All coeffs are present
|
|
621 memset(h->non_zero_count[mb_xy], 16, 16);
|
|
622
|
|
623 s->current_picture.mb_type[mb_xy]= mb_type;
|
|
624 return 0;
|
|
625 }
|
|
626
|
|
627 if(MB_MBAFF){
|
|
628 h->ref_count[0] <<= 1;
|
|
629 h->ref_count[1] <<= 1;
|
|
630 }
|
|
631
|
|
632 fill_caches(h, mb_type, 0);
|
|
633
|
|
634 //mb_pred
|
|
635 if(IS_INTRA(mb_type)){
|
|
636 int pred_mode;
|
|
637 // init_top_left_availability(h);
|
|
638 if(IS_INTRA4x4(mb_type)){
|
|
639 int i;
|
|
640 int di = 1;
|
|
641 if(dct8x8_allowed && get_bits1(&s->gb)){
|
|
642 mb_type |= MB_TYPE_8x8DCT;
|
|
643 di = 4;
|
|
644 }
|
|
645
|
|
646 // fill_intra4x4_pred_table(h);
|
|
647 for(i=0; i<16; i+=di){
|
|
648 int mode= pred_intra_mode(h, i);
|
|
649
|
|
650 if(!get_bits1(&s->gb)){
|
|
651 const int rem_mode= get_bits(&s->gb, 3);
|
|
652 mode = rem_mode + (rem_mode >= mode);
|
|
653 }
|
|
654
|
|
655 if(di==4)
|
|
656 fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
|
|
657 else
|
|
658 h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;
|
|
659 }
|
|
660 ff_h264_write_back_intra_pred_mode(h);
|
|
661 if( ff_h264_check_intra4x4_pred_mode(h) < 0)
|
|
662 return -1;
|
|
663 }else{
|
|
664 h->intra16x16_pred_mode= ff_h264_check_intra_pred_mode(h, h->intra16x16_pred_mode);
|
|
665 if(h->intra16x16_pred_mode < 0)
|
|
666 return -1;
|
|
667 }
|
|
668 if(CHROMA){
|
|
669 pred_mode= ff_h264_check_intra_pred_mode(h, get_ue_golomb_31(&s->gb));
|
|
670 if(pred_mode < 0)
|
|
671 return -1;
|
|
672 h->chroma_pred_mode= pred_mode;
|
|
673 }
|
|
674 }else if(partition_count==4){
|
|
675 int i, j, sub_partition_count[4], list, ref[2][4];
|
|
676
|
|
677 if(h->slice_type_nos == FF_B_TYPE){
|
|
678 for(i=0; i<4; i++){
|
|
679 h->sub_mb_type[i]= get_ue_golomb_31(&s->gb);
|
|
680 if(h->sub_mb_type[i] >=13){
|
|
681 av_log(h->s.avctx, AV_LOG_ERROR, "B sub_mb_type %u out of range at %d %d\n", h->sub_mb_type[i], s->mb_x, s->mb_y);
|
|
682 return -1;
|
|
683 }
|
|
684 sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
|
|
685 h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
|
|
686 }
|
|
687 if( IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
|
|
688 || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3])) {
|
|
689 ff_h264_pred_direct_motion(h, &mb_type);
|
|
690 h->ref_cache[0][scan8[4]] =
|
|
691 h->ref_cache[1][scan8[4]] =
|
|
692 h->ref_cache[0][scan8[12]] =
|
|
693 h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;
|
|
694 }
|
|
695 }else{
|
|
696 assert(h->slice_type_nos == FF_P_TYPE); //FIXME SP correct ?
|
|
697 for(i=0; i<4; i++){
|
|
698 h->sub_mb_type[i]= get_ue_golomb_31(&s->gb);
|
|
699 if(h->sub_mb_type[i] >=4){
|
|
700 av_log(h->s.avctx, AV_LOG_ERROR, "P sub_mb_type %u out of range at %d %d\n", h->sub_mb_type[i], s->mb_x, s->mb_y);
|
|
701 return -1;
|
|
702 }
|
|
703 sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
|
|
704 h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
|
|
705 }
|
|
706 }
|
|
707
|
|
708 for(list=0; list<h->list_count; list++){
|
|
709 int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
|
|
710 for(i=0; i<4; i++){
|
|
711 if(IS_DIRECT(h->sub_mb_type[i])) continue;
|
|
712 if(IS_DIR(h->sub_mb_type[i], 0, list)){
|
|
713 unsigned int tmp;
|
|
714 if(ref_count == 1){
|
|
715 tmp= 0;
|
|
716 }else if(ref_count == 2){
|
|
717 tmp= get_bits1(&s->gb)^1;
|
|
718 }else{
|
|
719 tmp= get_ue_golomb_31(&s->gb);
|
|
720 if(tmp>=ref_count){
|
|
721 av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", tmp);
|
|
722 return -1;
|
|
723 }
|
|
724 }
|
|
725 ref[list][i]= tmp;
|
|
726 }else{
|
|
727 //FIXME
|
|
728 ref[list][i] = -1;
|
|
729 }
|
|
730 }
|
|
731 }
|
|
732
|
|
733 if(dct8x8_allowed)
|
|
734 dct8x8_allowed = get_dct8x8_allowed(h);
|
|
735
|
|
736 for(list=0; list<h->list_count; list++){
|
|
737 for(i=0; i<4; i++){
|
|
738 if(IS_DIRECT(h->sub_mb_type[i])) {
|
|
739 h->ref_cache[list][ scan8[4*i] ] = h->ref_cache[list][ scan8[4*i]+1 ];
|
|
740 continue;
|
|
741 }
|
|
742 h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]=
|
|
743 h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
|
|
744
|
|
745 if(IS_DIR(h->sub_mb_type[i], 0, list)){
|
|
746 const int sub_mb_type= h->sub_mb_type[i];
|
|
747 const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
|
|
748 for(j=0; j<sub_partition_count[i]; j++){
|
|
749 int mx, my;
|
|
750 const int index= 4*i + block_width*j;
|
|
751 int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
|
|
752 pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
|
|
753 mx += get_se_golomb(&s->gb);
|
|
754 my += get_se_golomb(&s->gb);
|
|
755 tprintf(s->avctx, "final mv:%d %d\n", mx, my);
|
|
756
|
|
757 if(IS_SUB_8X8(sub_mb_type)){
|
|
758 mv_cache[ 1 ][0]=
|
|
759 mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
|
|
760 mv_cache[ 1 ][1]=
|
|
761 mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
|
|
762 }else if(IS_SUB_8X4(sub_mb_type)){
|
|
763 mv_cache[ 1 ][0]= mx;
|
|
764 mv_cache[ 1 ][1]= my;
|
|
765 }else if(IS_SUB_4X8(sub_mb_type)){
|
|
766 mv_cache[ 8 ][0]= mx;
|
|
767 mv_cache[ 8 ][1]= my;
|
|
768 }
|
|
769 mv_cache[ 0 ][0]= mx;
|
|
770 mv_cache[ 0 ][1]= my;
|
|
771 }
|
|
772 }else{
|
|
773 uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
|
|
774 p[0] = p[1]=
|
|
775 p[8] = p[9]= 0;
|
|
776 }
|
|
777 }
|
|
778 }
|
|
779 }else if(IS_DIRECT(mb_type)){
|
|
780 ff_h264_pred_direct_motion(h, &mb_type);
|
|
781 dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
|
|
782 }else{
|
|
783 int list, mx, my, i;
|
|
784 //FIXME we should set ref_idx_l? to 0 if we use that later ...
|
|
785 if(IS_16X16(mb_type)){
|
|
786 for(list=0; list<h->list_count; list++){
|
|
787 unsigned int val;
|
|
788 if(IS_DIR(mb_type, 0, list)){
|
|
789 if(h->ref_count[list]==1){
|
|
790 val= 0;
|
|
791 }else if(h->ref_count[list]==2){
|
|
792 val= get_bits1(&s->gb)^1;
|
|
793 }else{
|
|
794 val= get_ue_golomb_31(&s->gb);
|
|
795 if(val >= h->ref_count[list]){
|
|
796 av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
|
|
797 return -1;
|
|
798 }
|
|
799 }
|
|
800 }else
|
|
801 val= LIST_NOT_USED&0xFF;
|
|
802 fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
|
|
803 }
|
|
804 for(list=0; list<h->list_count; list++){
|
|
805 unsigned int val;
|
|
806 if(IS_DIR(mb_type, 0, list)){
|
|
807 pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
|
|
808 mx += get_se_golomb(&s->gb);
|
|
809 my += get_se_golomb(&s->gb);
|
|
810 tprintf(s->avctx, "final mv:%d %d\n", mx, my);
|
|
811
|
|
812 val= pack16to32(mx,my);
|
|
813 }else
|
|
814 val=0;
|
|
815 fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, val, 4);
|
|
816 }
|
|
817 }
|
|
818 else if(IS_16X8(mb_type)){
|
|
819 for(list=0; list<h->list_count; list++){
|
|
820 for(i=0; i<2; i++){
|
|
821 unsigned int val;
|
|
822 if(IS_DIR(mb_type, i, list)){
|
|
823 if(h->ref_count[list] == 1){
|
|
824 val= 0;
|
|
825 }else if(h->ref_count[list] == 2){
|
|
826 val= get_bits1(&s->gb)^1;
|
|
827 }else{
|
|
828 val= get_ue_golomb_31(&s->gb);
|
|
829 if(val >= h->ref_count[list]){
|
|
830 av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
|
|
831 return -1;
|
|
832 }
|
|
833 }
|
|
834 }else
|
|
835 val= LIST_NOT_USED&0xFF;
|
|
836 fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1);
|
|
837 }
|
|
838 }
|
|
839 for(list=0; list<h->list_count; list++){
|
|
840 for(i=0; i<2; i++){
|
|
841 unsigned int val;
|
|
842 if(IS_DIR(mb_type, i, list)){
|
|
843 pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);
|
|
844 mx += get_se_golomb(&s->gb);
|
|
845 my += get_se_golomb(&s->gb);
|
|
846 tprintf(s->avctx, "final mv:%d %d\n", mx, my);
|
|
847
|
|
848 val= pack16to32(mx,my);
|
|
849 }else
|
|
850 val=0;
|
|
851 fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 4);
|
|
852 }
|
|
853 }
|
|
854 }else{
|
|
855 assert(IS_8X16(mb_type));
|
|
856 for(list=0; list<h->list_count; list++){
|
|
857 for(i=0; i<2; i++){
|
|
858 unsigned int val;
|
|
859 if(IS_DIR(mb_type, i, list)){ //FIXME optimize
|
|
860 if(h->ref_count[list]==1){
|
|
861 val= 0;
|
|
862 }else if(h->ref_count[list]==2){
|
|
863 val= get_bits1(&s->gb)^1;
|
|
864 }else{
|
|
865 val= get_ue_golomb_31(&s->gb);
|
|
866 if(val >= h->ref_count[list]){
|
|
867 av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
|
|
868 return -1;
|
|
869 }
|
|
870 }
|
|
871 }else
|
|
872 val= LIST_NOT_USED&0xFF;
|
|
873 fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);
|
|
874 }
|
|
875 }
|
|
876 for(list=0; list<h->list_count; list++){
|
|
877 for(i=0; i<2; i++){
|
|
878 unsigned int val;
|
|
879 if(IS_DIR(mb_type, i, list)){
|
|
880 pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
|
|
881 mx += get_se_golomb(&s->gb);
|
|
882 my += get_se_golomb(&s->gb);
|
|
883 tprintf(s->avctx, "final mv:%d %d\n", mx, my);
|
|
884
|
|
885 val= pack16to32(mx,my);
|
|
886 }else
|
|
887 val=0;
|
|
888 fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 4);
|
|
889 }
|
|
890 }
|
|
891 }
|
|
892 }
|
|
893
|
|
894 if(IS_INTER(mb_type))
|
|
895 write_back_motion(h, mb_type);
|
|
896
|
|
897 if(!IS_INTRA16x16(mb_type)){
|
|
898 cbp= get_ue_golomb(&s->gb);
|
|
899 if(cbp > 47){
|
|
900 av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%u) at %d %d\n", cbp, s->mb_x, s->mb_y);
|
|
901 return -1;
|
|
902 }
|
|
903
|
|
904 if(CHROMA){
|
|
905 if(IS_INTRA4x4(mb_type)) cbp= golomb_to_intra4x4_cbp[cbp];
|
|
906 else cbp= golomb_to_inter_cbp [cbp];
|
|
907 }else{
|
|
908 if(IS_INTRA4x4(mb_type)) cbp= golomb_to_intra4x4_cbp_gray[cbp];
|
|
909 else cbp= golomb_to_inter_cbp_gray[cbp];
|
|
910 }
|
|
911 }
|
|
912 h->cbp = cbp;
|
|
913
|
|
914 if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){
|
|
915 if(get_bits1(&s->gb)){
|
|
916 mb_type |= MB_TYPE_8x8DCT;
|
|
917 h->cbp_table[mb_xy]= cbp;
|
|
918 }
|
|
919 }
|
|
920 s->current_picture.mb_type[mb_xy]= mb_type;
|
|
921
|
|
922 if(cbp || IS_INTRA16x16(mb_type)){
|
|
923 int i8x8, i4x4, chroma_idx;
|
|
924 int dquant;
|
|
925 GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
|
|
926 const uint8_t *scan, *scan8x8, *dc_scan;
|
|
927
|
|
928 // fill_non_zero_count_cache(h);
|
|
929
|
|
930 if(IS_INTERLACED(mb_type)){
|
|
931 scan8x8= s->qscale ? h->field_scan8x8_cavlc : h->field_scan8x8_cavlc_q0;
|
|
932 scan= s->qscale ? h->field_scan : h->field_scan_q0;
|
|
933 dc_scan= luma_dc_field_scan;
|
|
934 }else{
|
|
935 scan8x8= s->qscale ? h->zigzag_scan8x8_cavlc : h->zigzag_scan8x8_cavlc_q0;
|
|
936 scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
|
|
937 dc_scan= luma_dc_zigzag_scan;
|
|
938 }
|
|
939
|
|
940 dquant= get_se_golomb(&s->gb);
|
|
941
|
|
942 if( dquant > 25 || dquant < -26 ){
|
|
943 av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
|
|
944 return -1;
|
|
945 }
|
|
946
|
|
947 s->qscale += dquant;
|
|
948 if(((unsigned)s->qscale) > 51){
|
|
949 if(s->qscale<0) s->qscale+= 52;
|
|
950 else s->qscale-= 52;
|
|
951 }
|
|
952
|
|
953 h->chroma_qp[0]= get_chroma_qp(h, 0, s->qscale);
|
|
954 h->chroma_qp[1]= get_chroma_qp(h, 1, s->qscale);
|
|
955 if(IS_INTRA16x16(mb_type)){
|
|
956 if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, h->dequant4_coeff[0][s->qscale], 16) < 0){
|
|
957 return -1; //FIXME continue if partitioned and other return -1 too
|
|
958 }
|
|
959
|
|
960 assert((cbp&15) == 0 || (cbp&15) == 15);
|
|
961
|
|
962 if(cbp&15){
|
|
963 for(i8x8=0; i8x8<4; i8x8++){
|
|
964 for(i4x4=0; i4x4<4; i4x4++){
|
|
965 const int index= i4x4 + 4*i8x8;
|
|
966 if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 ){
|
|
967 return -1;
|
|
968 }
|
|
969 }
|
|
970 }
|
|
971 }else{
|
|
972 fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
|
|
973 }
|
|
974 }else{
|
|
975 for(i8x8=0; i8x8<4; i8x8++){
|
|
976 if(cbp & (1<<i8x8)){
|
|
977 if(IS_8x8DCT(mb_type)){
|
|
978 DCTELEM *buf = &h->mb[64*i8x8];
|
|
979 uint8_t *nnz;
|
|
980 for(i4x4=0; i4x4<4; i4x4++){
|
|
981 if( decode_residual(h, gb, buf, i4x4+4*i8x8, scan8x8+16*i4x4,
|
|
982 h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 16) <0 )
|
|
983 return -1;
|
|
984 }
|
|
985 nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
|
|
986 nnz[0] += nnz[1] + nnz[8] + nnz[9];
|
|
987 }else{
|
|
988 for(i4x4=0; i4x4<4; i4x4++){
|
|
989 const int index= i4x4 + 4*i8x8;
|
|
990
|
|
991 if( decode_residual(h, gb, h->mb + 16*index, index, scan, h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale], 16) <0 ){
|
|
992 return -1;
|
|
993 }
|
|
994 }
|
|
995 }
|
|
996 }else{
|
|
997 uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
|
|
998 nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
|
|
999 }
|
|
1000 }
|
|
1001 }
|
|
1002
|
|
1003 if(cbp&0x30){
|
|
1004 for(chroma_idx=0; chroma_idx<2; chroma_idx++)
|
|
1005 if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, NULL, 4) < 0){
|
|
1006 return -1;
|
|
1007 }
|
|
1008 }
|
|
1009
|
|
1010 if(cbp&0x20){
|
|
1011 for(chroma_idx=0; chroma_idx<2; chroma_idx++){
|
|
1012 const uint32_t *qmul = h->dequant4_coeff[chroma_idx+1+(IS_INTRA( mb_type ) ? 0:3)][h->chroma_qp[chroma_idx]];
|
|
1013 for(i4x4=0; i4x4<4; i4x4++){
|
|
1014 const int index= 16 + 4*chroma_idx + i4x4;
|
|
1015 if( decode_residual(h, gb, h->mb + 16*index, index, scan + 1, qmul, 15) < 0){
|
|
1016 return -1;
|
|
1017 }
|
|
1018 }
|
|
1019 }
|
|
1020 }else{
|
|
1021 uint8_t * const nnz= &h->non_zero_count_cache[0];
|
|
1022 nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
|
|
1023 nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
|
|
1024 }
|
|
1025 }else{
|
|
1026 uint8_t * const nnz= &h->non_zero_count_cache[0];
|
|
1027 fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
|
|
1028 nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
|
|
1029 nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
|
|
1030 }
|
|
1031 s->current_picture.qscale_table[mb_xy]= s->qscale;
|
|
1032 write_back_non_zero_count(h);
|
|
1033
|
|
1034 if(MB_MBAFF){
|
|
1035 h->ref_count[0] >>= 1;
|
|
1036 h->ref_count[1] >>= 1;
|
|
1037 }
|
|
1038
|
|
1039 return 0;
|
|
1040 }
|
|
1041
|