Define AVMediaType enum, and use it instead of enum CodecType, which
[libav.git] / libavcodec / vc1dec.c
1 /*
2 * VC-1 and WMV3 decoder
3 * Copyright (c) 2006-2007 Konstantin Shishkov
4 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file libavcodec/vc1dec.c
25 * VC-1 and WMV3 decoder
26 *
27 */
28 #include "internal.h"
29 #include "dsputil.h"
30 #include "avcodec.h"
31 #include "mpegvideo.h"
32 #include "h263.h"
33 #include "vc1.h"
34 #include "vc1data.h"
35 #include "vc1acdata.h"
36 #include "msmpeg4data.h"
37 #include "unary.h"
38 #include "simple_idct.h"
39 #include "mathops.h"
40 #include "vdpau_internal.h"
41
42 #undef NDEBUG
43 #include <assert.h>
44
45 #define MB_INTRA_VLC_BITS 9
46 #define DC_VLC_BITS 9
47 #define AC_VLC_BITS 9
48 static const uint16_t table_mb_intra[64][2];
49
50
51 static const uint16_t vlc_offs[] = {
52 0, 520, 552, 616, 1128, 1160, 1224, 1740, 1772, 1836, 1900, 2436,
53 2986, 3050, 3610, 4154, 4218, 4746, 5326, 5390, 5902, 6554, 7658, 8620,
54 9262, 10202, 10756, 11310, 12228, 15078
55 };
56
57 /**
58 * Init VC-1 specific tables and VC1Context members
59 * @param v The VC1Context to initialize
60 * @return Status
61 */
62 static int vc1_init_common(VC1Context *v)
63 {
64 static int done = 0;
65 int i = 0;
66 static VLC_TYPE vlc_table[15078][2];
67
68 v->hrd_rate = v->hrd_buffer = NULL;
69
70 /* VLC tables */
71 if(!done)
72 {
73 INIT_VLC_STATIC(&ff_vc1_bfraction_vlc, VC1_BFRACTION_VLC_BITS, 23,
74 ff_vc1_bfraction_bits, 1, 1,
75 ff_vc1_bfraction_codes, 1, 1, 1 << VC1_BFRACTION_VLC_BITS);
76 INIT_VLC_STATIC(&ff_vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4,
77 ff_vc1_norm2_bits, 1, 1,
78 ff_vc1_norm2_codes, 1, 1, 1 << VC1_NORM2_VLC_BITS);
79 INIT_VLC_STATIC(&ff_vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 64,
80 ff_vc1_norm6_bits, 1, 1,
81 ff_vc1_norm6_codes, 2, 2, 556);
82 INIT_VLC_STATIC(&ff_vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7,
83 ff_vc1_imode_bits, 1, 1,
84 ff_vc1_imode_codes, 1, 1, 1 << VC1_IMODE_VLC_BITS);
85 for (i=0; i<3; i++)
86 {
87 ff_vc1_ttmb_vlc[i].table = &vlc_table[vlc_offs[i*3+0]];
88 ff_vc1_ttmb_vlc[i].table_allocated = vlc_offs[i*3+1] - vlc_offs[i*3+0];
89 init_vlc(&ff_vc1_ttmb_vlc[i], VC1_TTMB_VLC_BITS, 16,
90 ff_vc1_ttmb_bits[i], 1, 1,
91 ff_vc1_ttmb_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
92 ff_vc1_ttblk_vlc[i].table = &vlc_table[vlc_offs[i*3+1]];
93 ff_vc1_ttblk_vlc[i].table_allocated = vlc_offs[i*3+2] - vlc_offs[i*3+1];
94 init_vlc(&ff_vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 8,
95 ff_vc1_ttblk_bits[i], 1, 1,
96 ff_vc1_ttblk_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
97 ff_vc1_subblkpat_vlc[i].table = &vlc_table[vlc_offs[i*3+2]];
98 ff_vc1_subblkpat_vlc[i].table_allocated = vlc_offs[i*3+3] - vlc_offs[i*3+2];
99 init_vlc(&ff_vc1_subblkpat_vlc[i], VC1_SUBBLKPAT_VLC_BITS, 15,
100 ff_vc1_subblkpat_bits[i], 1, 1,
101 ff_vc1_subblkpat_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
102 }
103 for(i=0; i<4; i++)
104 {
105 ff_vc1_4mv_block_pattern_vlc[i].table = &vlc_table[vlc_offs[i*3+9]];
106 ff_vc1_4mv_block_pattern_vlc[i].table_allocated = vlc_offs[i*3+10] - vlc_offs[i*3+9];
107 init_vlc(&ff_vc1_4mv_block_pattern_vlc[i], VC1_4MV_BLOCK_PATTERN_VLC_BITS, 16,
108 ff_vc1_4mv_block_pattern_bits[i], 1, 1,
109 ff_vc1_4mv_block_pattern_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
110 ff_vc1_cbpcy_p_vlc[i].table = &vlc_table[vlc_offs[i*3+10]];
111 ff_vc1_cbpcy_p_vlc[i].table_allocated = vlc_offs[i*3+11] - vlc_offs[i*3+10];
112 init_vlc(&ff_vc1_cbpcy_p_vlc[i], VC1_CBPCY_P_VLC_BITS, 64,
113 ff_vc1_cbpcy_p_bits[i], 1, 1,
114 ff_vc1_cbpcy_p_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
115 ff_vc1_mv_diff_vlc[i].table = &vlc_table[vlc_offs[i*3+11]];
116 ff_vc1_mv_diff_vlc[i].table_allocated = vlc_offs[i*3+12] - vlc_offs[i*3+11];
117 init_vlc(&ff_vc1_mv_diff_vlc[i], VC1_MV_DIFF_VLC_BITS, 73,
118 ff_vc1_mv_diff_bits[i], 1, 1,
119 ff_vc1_mv_diff_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
120 }
121 for(i=0; i<8; i++){
122 ff_vc1_ac_coeff_table[i].table = &vlc_table[vlc_offs[i+21]];
123 ff_vc1_ac_coeff_table[i].table_allocated = vlc_offs[i+22] - vlc_offs[i+21];
124 init_vlc(&ff_vc1_ac_coeff_table[i], AC_VLC_BITS, vc1_ac_sizes[i],
125 &vc1_ac_tables[i][0][1], 8, 4,
126 &vc1_ac_tables[i][0][0], 8, 4, INIT_VLC_USE_NEW_STATIC);
127 }
128 done = 1;
129 }
130
131 /* Other defaults */
132 v->pq = -1;
133 v->mvrange = 0; /* 7.1.1.18, p80 */
134
135 return 0;
136 }
137
138 /***********************************************************************/
139 /**
140 * @defgroup vc1bitplane VC-1 Bitplane decoding
141 * @see 8.7, p56
142 * @{
143 */
144
145 /**
146 * Imode types
147 * @{
148 */
149 enum Imode {
150 IMODE_RAW,
151 IMODE_NORM2,
152 IMODE_DIFF2,
153 IMODE_NORM6,
154 IMODE_DIFF6,
155 IMODE_ROWSKIP,
156 IMODE_COLSKIP
157 };
158 /** @} */ //imode defines
159
160
161 /** @} */ //Bitplane group
162
163 static void vc1_loop_filter_iblk(MpegEncContext *s, int pq)
164 {
165 int i, j;
166 if(!s->first_slice_line)
167 s->dsp.vc1_v_loop_filter16(s->dest[0], s->linesize, pq);
168 s->dsp.vc1_v_loop_filter16(s->dest[0] + 8*s->linesize, s->linesize, pq);
169 for(i = !s->mb_x*8; i < 16; i += 8)
170 s->dsp.vc1_h_loop_filter16(s->dest[0] + i, s->linesize, pq);
171 for(j = 0; j < 2; j++){
172 if(!s->first_slice_line)
173 s->dsp.vc1_v_loop_filter8(s->dest[j+1], s->uvlinesize, pq);
174 if(s->mb_x)
175 s->dsp.vc1_h_loop_filter8(s->dest[j+1], s->uvlinesize, pq);
176 }
177 }
178
179 /** Put block onto picture
180 */
181 static void vc1_put_block(VC1Context *v, DCTELEM block[6][64])
182 {
183 uint8_t *Y;
184 int ys, us, vs;
185 DSPContext *dsp = &v->s.dsp;
186
187 if(v->rangeredfrm) {
188 int i, j, k;
189 for(k = 0; k < 6; k++)
190 for(j = 0; j < 8; j++)
191 for(i = 0; i < 8; i++)
192 block[k][i + j*8] = ((block[k][i + j*8] - 128) << 1) + 128;
193
194 }
195 ys = v->s.current_picture.linesize[0];
196 us = v->s.current_picture.linesize[1];
197 vs = v->s.current_picture.linesize[2];
198 Y = v->s.dest[0];
199
200 dsp->put_pixels_clamped(block[0], Y, ys);
201 dsp->put_pixels_clamped(block[1], Y + 8, ys);
202 Y += ys * 8;
203 dsp->put_pixels_clamped(block[2], Y, ys);
204 dsp->put_pixels_clamped(block[3], Y + 8, ys);
205
206 if(!(v->s.flags & CODEC_FLAG_GRAY)) {
207 dsp->put_pixels_clamped(block[4], v->s.dest[1], us);
208 dsp->put_pixels_clamped(block[5], v->s.dest[2], vs);
209 }
210 }
211
212 /** Do motion compensation over 1 macroblock
213 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
214 */
215 static void vc1_mc_1mv(VC1Context *v, int dir)
216 {
217 MpegEncContext *s = &v->s;
218 DSPContext *dsp = &v->s.dsp;
219 uint8_t *srcY, *srcU, *srcV;
220 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
221
222 if(!v->s.last_picture.data[0])return;
223
224 mx = s->mv[dir][0][0];
225 my = s->mv[dir][0][1];
226
227 // store motion vectors for further use in B frames
228 if(s->pict_type == FF_P_TYPE) {
229 s->current_picture.motion_val[1][s->block_index[0]][0] = mx;
230 s->current_picture.motion_val[1][s->block_index[0]][1] = my;
231 }
232 uvmx = (mx + ((mx & 3) == 3)) >> 1;
233 uvmy = (my + ((my & 3) == 3)) >> 1;
234 if(v->fastuvmc) {
235 uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
236 uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
237 }
238 if(!dir) {
239 srcY = s->last_picture.data[0];
240 srcU = s->last_picture.data[1];
241 srcV = s->last_picture.data[2];
242 } else {
243 srcY = s->next_picture.data[0];
244 srcU = s->next_picture.data[1];
245 srcV = s->next_picture.data[2];
246 }
247
248 src_x = s->mb_x * 16 + (mx >> 2);
249 src_y = s->mb_y * 16 + (my >> 2);
250 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
251 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
252
253 if(v->profile != PROFILE_ADVANCED){
254 src_x = av_clip( src_x, -16, s->mb_width * 16);
255 src_y = av_clip( src_y, -16, s->mb_height * 16);
256 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
257 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
258 }else{
259 src_x = av_clip( src_x, -17, s->avctx->coded_width);
260 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
261 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
262 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
263 }
264
265 srcY += src_y * s->linesize + src_x;
266 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
267 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
268
269 /* for grayscale we should not try to read from unknown area */
270 if(s->flags & CODEC_FLAG_GRAY) {
271 srcU = s->edge_emu_buffer + 18 * s->linesize;
272 srcV = s->edge_emu_buffer + 18 * s->linesize;
273 }
274
275 if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
276 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel*3
277 || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 16 - s->mspel*3){
278 uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
279
280 srcY -= s->mspel * (1 + s->linesize);
281 ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2,
282 src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
283 srcY = s->edge_emu_buffer;
284 ff_emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8+1, 8+1,
285 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
286 ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
287 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
288 srcU = uvbuf;
289 srcV = uvbuf + 16;
290 /* if we deal with range reduction we need to scale source blocks */
291 if(v->rangeredfrm) {
292 int i, j;
293 uint8_t *src, *src2;
294
295 src = srcY;
296 for(j = 0; j < 17 + s->mspel*2; j++) {
297 for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
298 src += s->linesize;
299 }
300 src = srcU; src2 = srcV;
301 for(j = 0; j < 9; j++) {
302 for(i = 0; i < 9; i++) {
303 src[i] = ((src[i] - 128) >> 1) + 128;
304 src2[i] = ((src2[i] - 128) >> 1) + 128;
305 }
306 src += s->uvlinesize;
307 src2 += s->uvlinesize;
308 }
309 }
310 /* if we deal with intensity compensation we need to scale source blocks */
311 if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
312 int i, j;
313 uint8_t *src, *src2;
314
315 src = srcY;
316 for(j = 0; j < 17 + s->mspel*2; j++) {
317 for(i = 0; i < 17 + s->mspel*2; i++) src[i] = v->luty[src[i]];
318 src += s->linesize;
319 }
320 src = srcU; src2 = srcV;
321 for(j = 0; j < 9; j++) {
322 for(i = 0; i < 9; i++) {
323 src[i] = v->lutuv[src[i]];
324 src2[i] = v->lutuv[src2[i]];
325 }
326 src += s->uvlinesize;
327 src2 += s->uvlinesize;
328 }
329 }
330 srcY += s->mspel * (1 + s->linesize);
331 }
332
333 if(s->mspel) {
334 dxy = ((my & 3) << 2) | (mx & 3);
335 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] , srcY , s->linesize, v->rnd);
336 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
337 srcY += s->linesize * 8;
338 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize , srcY , s->linesize, v->rnd);
339 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
340 } else { // hpel mc - always used for luma
341 dxy = (my & 2) | ((mx & 2) >> 1);
342
343 if(!v->rnd)
344 dsp->put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
345 else
346 dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
347 }
348
349 if(s->flags & CODEC_FLAG_GRAY) return;
350 /* Chroma MC always uses qpel bilinear */
351 uvmx = (uvmx&3)<<1;
352 uvmy = (uvmy&3)<<1;
353 if(!v->rnd){
354 dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
355 dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
356 }else{
357 dsp->put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
358 dsp->put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
359 }
360 }
361
362 /** Do motion compensation for 4-MV macroblock - luminance block
363 */
364 static void vc1_mc_4mv_luma(VC1Context *v, int n)
365 {
366 MpegEncContext *s = &v->s;
367 DSPContext *dsp = &v->s.dsp;
368 uint8_t *srcY;
369 int dxy, mx, my, src_x, src_y;
370 int off;
371
372 if(!v->s.last_picture.data[0])return;
373 mx = s->mv[0][n][0];
374 my = s->mv[0][n][1];
375 srcY = s->last_picture.data[0];
376
377 off = s->linesize * 4 * (n&2) + (n&1) * 8;
378
379 src_x = s->mb_x * 16 + (n&1) * 8 + (mx >> 2);
380 src_y = s->mb_y * 16 + (n&2) * 4 + (my >> 2);
381
382 if(v->profile != PROFILE_ADVANCED){
383 src_x = av_clip( src_x, -16, s->mb_width * 16);
384 src_y = av_clip( src_y, -16, s->mb_height * 16);
385 }else{
386 src_x = av_clip( src_x, -17, s->avctx->coded_width);
387 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
388 }
389
390 srcY += src_y * s->linesize + src_x;
391
392 if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
393 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 8 - s->mspel*2
394 || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 8 - s->mspel*2){
395 srcY -= s->mspel * (1 + s->linesize);
396 ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 9+s->mspel*2, 9+s->mspel*2,
397 src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
398 srcY = s->edge_emu_buffer;
399 /* if we deal with range reduction we need to scale source blocks */
400 if(v->rangeredfrm) {
401 int i, j;
402 uint8_t *src;
403
404 src = srcY;
405 for(j = 0; j < 9 + s->mspel*2; j++) {
406 for(i = 0; i < 9 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
407 src += s->linesize;
408 }
409 }
410 /* if we deal with intensity compensation we need to scale source blocks */
411 if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
412 int i, j;
413 uint8_t *src;
414
415 src = srcY;
416 for(j = 0; j < 9 + s->mspel*2; j++) {
417 for(i = 0; i < 9 + s->mspel*2; i++) src[i] = v->luty[src[i]];
418 src += s->linesize;
419 }
420 }
421 srcY += s->mspel * (1 + s->linesize);
422 }
423
424 if(s->mspel) {
425 dxy = ((my & 3) << 2) | (mx & 3);
426 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, v->rnd);
427 } else { // hpel mc - always used for luma
428 dxy = (my & 2) | ((mx & 2) >> 1);
429 if(!v->rnd)
430 dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
431 else
432 dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
433 }
434 }
435
436 static inline int median4(int a, int b, int c, int d)
437 {
438 if(a < b) {
439 if(c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
440 else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
441 } else {
442 if(c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
443 else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
444 }
445 }
446
447
448 /** Do motion compensation for 4-MV macroblock - both chroma blocks
449 */
450 static void vc1_mc_4mv_chroma(VC1Context *v)
451 {
452 MpegEncContext *s = &v->s;
453 DSPContext *dsp = &v->s.dsp;
454 uint8_t *srcU, *srcV;
455 int uvmx, uvmy, uvsrc_x, uvsrc_y;
456 int i, idx, tx = 0, ty = 0;
457 int mvx[4], mvy[4], intra[4];
458 static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
459
460 if(!v->s.last_picture.data[0])return;
461 if(s->flags & CODEC_FLAG_GRAY) return;
462
463 for(i = 0; i < 4; i++) {
464 mvx[i] = s->mv[0][i][0];
465 mvy[i] = s->mv[0][i][1];
466 intra[i] = v->mb_type[0][s->block_index[i]];
467 }
468
469 /* calculate chroma MV vector from four luma MVs */
470 idx = (intra[3] << 3) | (intra[2] << 2) | (intra[1] << 1) | intra[0];
471 if(!idx) { // all blocks are inter
472 tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
473 ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
474 } else if(count[idx] == 1) { // 3 inter blocks
475 switch(idx) {
476 case 0x1:
477 tx = mid_pred(mvx[1], mvx[2], mvx[3]);
478 ty = mid_pred(mvy[1], mvy[2], mvy[3]);
479 break;
480 case 0x2:
481 tx = mid_pred(mvx[0], mvx[2], mvx[3]);
482 ty = mid_pred(mvy[0], mvy[2], mvy[3]);
483 break;
484 case 0x4:
485 tx = mid_pred(mvx[0], mvx[1], mvx[3]);
486 ty = mid_pred(mvy[0], mvy[1], mvy[3]);
487 break;
488 case 0x8:
489 tx = mid_pred(mvx[0], mvx[1], mvx[2]);
490 ty = mid_pred(mvy[0], mvy[1], mvy[2]);
491 break;
492 }
493 } else if(count[idx] == 2) {
494 int t1 = 0, t2 = 0;
495 for(i=0; i<3;i++) if(!intra[i]) {t1 = i; break;}
496 for(i= t1+1; i<4; i++)if(!intra[i]) {t2 = i; break;}
497 tx = (mvx[t1] + mvx[t2]) / 2;
498 ty = (mvy[t1] + mvy[t2]) / 2;
499 } else {
500 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
501 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
502 return; //no need to do MC for inter blocks
503 }
504
505 s->current_picture.motion_val[1][s->block_index[0]][0] = tx;
506 s->current_picture.motion_val[1][s->block_index[0]][1] = ty;
507 uvmx = (tx + ((tx&3) == 3)) >> 1;
508 uvmy = (ty + ((ty&3) == 3)) >> 1;
509 if(v->fastuvmc) {
510 uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
511 uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
512 }
513
514 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
515 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
516
517 if(v->profile != PROFILE_ADVANCED){
518 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
519 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
520 }else{
521 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
522 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
523 }
524
525 srcU = s->last_picture.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
526 srcV = s->last_picture.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
527 if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
528 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
529 || (unsigned)uvsrc_y > (s->v_edge_pos >> 1) - 9){
530 ff_emulated_edge_mc(s->edge_emu_buffer , srcU, s->uvlinesize, 8+1, 8+1,
531 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
532 ff_emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize, 8+1, 8+1,
533 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
534 srcU = s->edge_emu_buffer;
535 srcV = s->edge_emu_buffer + 16;
536
537 /* if we deal with range reduction we need to scale source blocks */
538 if(v->rangeredfrm) {
539 int i, j;
540 uint8_t *src, *src2;
541
542 src = srcU; src2 = srcV;
543 for(j = 0; j < 9; j++) {
544 for(i = 0; i < 9; i++) {
545 src[i] = ((src[i] - 128) >> 1) + 128;
546 src2[i] = ((src2[i] - 128) >> 1) + 128;
547 }
548 src += s->uvlinesize;
549 src2 += s->uvlinesize;
550 }
551 }
552 /* if we deal with intensity compensation we need to scale source blocks */
553 if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
554 int i, j;
555 uint8_t *src, *src2;
556
557 src = srcU; src2 = srcV;
558 for(j = 0; j < 9; j++) {
559 for(i = 0; i < 9; i++) {
560 src[i] = v->lutuv[src[i]];
561 src2[i] = v->lutuv[src2[i]];
562 }
563 src += s->uvlinesize;
564 src2 += s->uvlinesize;
565 }
566 }
567 }
568
569 /* Chroma MC always uses qpel bilinear */
570 uvmx = (uvmx&3)<<1;
571 uvmy = (uvmy&3)<<1;
572 if(!v->rnd){
573 dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
574 dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
575 }else{
576 dsp->put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
577 dsp->put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
578 }
579 }
580
581 /***********************************************************************/
582 /**
583 * @defgroup vc1block VC-1 Block-level functions
584 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
585 * @{
586 */
587
588 /**
589 * @def GET_MQUANT
590 * @brief Get macroblock-level quantizer scale
591 */
592 #define GET_MQUANT() \
593 if (v->dquantfrm) \
594 { \
595 int edges = 0; \
596 if (v->dqprofile == DQPROFILE_ALL_MBS) \
597 { \
598 if (v->dqbilevel) \
599 { \
600 mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
601 } \
602 else \
603 { \
604 mqdiff = get_bits(gb, 3); \
605 if (mqdiff != 7) mquant = v->pq + mqdiff; \
606 else mquant = get_bits(gb, 5); \
607 } \
608 } \
609 if(v->dqprofile == DQPROFILE_SINGLE_EDGE) \
610 edges = 1 << v->dqsbedge; \
611 else if(v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
612 edges = (3 << v->dqsbedge) % 15; \
613 else if(v->dqprofile == DQPROFILE_FOUR_EDGES) \
614 edges = 15; \
615 if((edges&1) && !s->mb_x) \
616 mquant = v->altpq; \
617 if((edges&2) && s->first_slice_line) \
618 mquant = v->altpq; \
619 if((edges&4) && s->mb_x == (s->mb_width - 1)) \
620 mquant = v->altpq; \
621 if((edges&8) && s->mb_y == (s->mb_height - 1)) \
622 mquant = v->altpq; \
623 }
624
625 /**
626 * @def GET_MVDATA(_dmv_x, _dmv_y)
627 * @brief Get MV differentials
628 * @see MVDATA decoding from 8.3.5.2, p(1)20
629 * @param _dmv_x Horizontal differential for decoded MV
630 * @param _dmv_y Vertical differential for decoded MV
631 */
632 #define GET_MVDATA(_dmv_x, _dmv_y) \
633 index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table,\
634 VC1_MV_DIFF_VLC_BITS, 2); \
635 if (index > 36) \
636 { \
637 mb_has_coeffs = 1; \
638 index -= 37; \
639 } \
640 else mb_has_coeffs = 0; \
641 s->mb_intra = 0; \
642 if (!index) { _dmv_x = _dmv_y = 0; } \
643 else if (index == 35) \
644 { \
645 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
646 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
647 } \
648 else if (index == 36) \
649 { \
650 _dmv_x = 0; \
651 _dmv_y = 0; \
652 s->mb_intra = 1; \
653 } \
654 else \
655 { \
656 index1 = index%6; \
657 if (!s->quarter_sample && index1 == 5) val = 1; \
658 else val = 0; \
659 if(size_table[index1] - val > 0) \
660 val = get_bits(gb, size_table[index1] - val); \
661 else val = 0; \
662 sign = 0 - (val&1); \
663 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
664 \
665 index1 = index/6; \
666 if (!s->quarter_sample && index1 == 5) val = 1; \
667 else val = 0; \
668 if(size_table[index1] - val > 0) \
669 val = get_bits(gb, size_table[index1] - val); \
670 else val = 0; \
671 sign = 0 - (val&1); \
672 _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
673 }
674
675 /** Predict and set motion vector
676 */
677 static inline void vc1_pred_mv(MpegEncContext *s, int n, int dmv_x, int dmv_y, int mv1, int r_x, int r_y, uint8_t* is_intra)
678 {
679 int xy, wrap, off = 0;
680 int16_t *A, *B, *C;
681 int px, py;
682 int sum;
683
684 /* scale MV difference to be quad-pel */
685 dmv_x <<= 1 - s->quarter_sample;
686 dmv_y <<= 1 - s->quarter_sample;
687
688 wrap = s->b8_stride;
689 xy = s->block_index[n];
690
691 if(s->mb_intra){
692 s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
693 s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
694 s->current_picture.motion_val[1][xy][0] = 0;
695 s->current_picture.motion_val[1][xy][1] = 0;
696 if(mv1) { /* duplicate motion data for 1-MV block */
697 s->current_picture.motion_val[0][xy + 1][0] = 0;
698 s->current_picture.motion_val[0][xy + 1][1] = 0;
699 s->current_picture.motion_val[0][xy + wrap][0] = 0;
700 s->current_picture.motion_val[0][xy + wrap][1] = 0;
701 s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
702 s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
703 s->current_picture.motion_val[1][xy + 1][0] = 0;
704 s->current_picture.motion_val[1][xy + 1][1] = 0;
705 s->current_picture.motion_val[1][xy + wrap][0] = 0;
706 s->current_picture.motion_val[1][xy + wrap][1] = 0;
707 s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
708 s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
709 }
710 return;
711 }
712
713 C = s->current_picture.motion_val[0][xy - 1];
714 A = s->current_picture.motion_val[0][xy - wrap];
715 if(mv1)
716 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
717 else {
718 //in 4-MV mode different blocks have different B predictor position
719 switch(n){
720 case 0:
721 off = (s->mb_x > 0) ? -1 : 1;
722 break;
723 case 1:
724 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
725 break;
726 case 2:
727 off = 1;
728 break;
729 case 3:
730 off = -1;
731 }
732 }
733 B = s->current_picture.motion_val[0][xy - wrap + off];
734
735 if(!s->first_slice_line || (n==2 || n==3)) { // predictor A is not out of bounds
736 if(s->mb_width == 1) {
737 px = A[0];
738 py = A[1];
739 } else {
740 px = mid_pred(A[0], B[0], C[0]);
741 py = mid_pred(A[1], B[1], C[1]);
742 }
743 } else if(s->mb_x || (n==1 || n==3)) { // predictor C is not out of bounds
744 px = C[0];
745 py = C[1];
746 } else {
747 px = py = 0;
748 }
749 /* Pullback MV as specified in 8.3.5.3.4 */
750 {
751 int qx, qy, X, Y;
752 qx = (s->mb_x << 6) + ((n==1 || n==3) ? 32 : 0);
753 qy = (s->mb_y << 6) + ((n==2 || n==3) ? 32 : 0);
754 X = (s->mb_width << 6) - 4;
755 Y = (s->mb_height << 6) - 4;
756 if(mv1) {
757 if(qx + px < -60) px = -60 - qx;
758 if(qy + py < -60) py = -60 - qy;
759 } else {
760 if(qx + px < -28) px = -28 - qx;
761 if(qy + py < -28) py = -28 - qy;
762 }
763 if(qx + px > X) px = X - qx;
764 if(qy + py > Y) py = Y - qy;
765 }
766 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
767 if((!s->first_slice_line || (n==2 || n==3)) && (s->mb_x || (n==1 || n==3))) {
768 if(is_intra[xy - wrap])
769 sum = FFABS(px) + FFABS(py);
770 else
771 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
772 if(sum > 32) {
773 if(get_bits1(&s->gb)) {
774 px = A[0];
775 py = A[1];
776 } else {
777 px = C[0];
778 py = C[1];
779 }
780 } else {
781 if(is_intra[xy - 1])
782 sum = FFABS(px) + FFABS(py);
783 else
784 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
785 if(sum > 32) {
786 if(get_bits1(&s->gb)) {
787 px = A[0];
788 py = A[1];
789 } else {
790 px = C[0];
791 py = C[1];
792 }
793 }
794 }
795 }
796 /* store MV using signed modulus of MV range defined in 4.11 */
797 s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
798 s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
799 if(mv1) { /* duplicate motion data for 1-MV block */
800 s->current_picture.motion_val[0][xy + 1][0] = s->current_picture.motion_val[0][xy][0];
801 s->current_picture.motion_val[0][xy + 1][1] = s->current_picture.motion_val[0][xy][1];
802 s->current_picture.motion_val[0][xy + wrap][0] = s->current_picture.motion_val[0][xy][0];
803 s->current_picture.motion_val[0][xy + wrap][1] = s->current_picture.motion_val[0][xy][1];
804 s->current_picture.motion_val[0][xy + wrap + 1][0] = s->current_picture.motion_val[0][xy][0];
805 s->current_picture.motion_val[0][xy + wrap + 1][1] = s->current_picture.motion_val[0][xy][1];
806 }
807 }
808
809 /** Motion compensation for direct or interpolated blocks in B-frames
810 */
811 static void vc1_interp_mc(VC1Context *v)
812 {
813 MpegEncContext *s = &v->s;
814 DSPContext *dsp = &v->s.dsp;
815 uint8_t *srcY, *srcU, *srcV;
816 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
817
818 if(!v->s.next_picture.data[0])return;
819
820 mx = s->mv[1][0][0];
821 my = s->mv[1][0][1];
822 uvmx = (mx + ((mx & 3) == 3)) >> 1;
823 uvmy = (my + ((my & 3) == 3)) >> 1;
824 if(v->fastuvmc) {
825 uvmx = uvmx + ((uvmx<0)?-(uvmx&1):(uvmx&1));
826 uvmy = uvmy + ((uvmy<0)?-(uvmy&1):(uvmy&1));
827 }
828 srcY = s->next_picture.data[0];
829 srcU = s->next_picture.data[1];
830 srcV = s->next_picture.data[2];
831
832 src_x = s->mb_x * 16 + (mx >> 2);
833 src_y = s->mb_y * 16 + (my >> 2);
834 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
835 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
836
837 if(v->profile != PROFILE_ADVANCED){
838 src_x = av_clip( src_x, -16, s->mb_width * 16);
839 src_y = av_clip( src_y, -16, s->mb_height * 16);
840 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
841 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
842 }else{
843 src_x = av_clip( src_x, -17, s->avctx->coded_width);
844 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
845 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
846 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
847 }
848
849 srcY += src_y * s->linesize + src_x;
850 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
851 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
852
853 /* for grayscale we should not try to read from unknown area */
854 if(s->flags & CODEC_FLAG_GRAY) {
855 srcU = s->edge_emu_buffer + 18 * s->linesize;
856 srcV = s->edge_emu_buffer + 18 * s->linesize;
857 }
858
859 if(v->rangeredfrm
860 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel*3
861 || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 16 - s->mspel*3){
862 uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
863
864 srcY -= s->mspel * (1 + s->linesize);
865 ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2,
866 src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
867 srcY = s->edge_emu_buffer;
868 ff_emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8+1, 8+1,
869 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
870 ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
871 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
872 srcU = uvbuf;
873 srcV = uvbuf + 16;
874 /* if we deal with range reduction we need to scale source blocks */
875 if(v->rangeredfrm) {
876 int i, j;
877 uint8_t *src, *src2;
878
879 src = srcY;
880 for(j = 0; j < 17 + s->mspel*2; j++) {
881 for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
882 src += s->linesize;
883 }
884 src = srcU; src2 = srcV;
885 for(j = 0; j < 9; j++) {
886 for(i = 0; i < 9; i++) {
887 src[i] = ((src[i] - 128) >> 1) + 128;
888 src2[i] = ((src2[i] - 128) >> 1) + 128;
889 }
890 src += s->uvlinesize;
891 src2 += s->uvlinesize;
892 }
893 }
894 srcY += s->mspel * (1 + s->linesize);
895 }
896
897 if(s->mspel) {
898 dxy = ((my & 3) << 2) | (mx & 3);
899 dsp->avg_vc1_mspel_pixels_tab[dxy](s->dest[0] , srcY , s->linesize, v->rnd);
900 dsp->avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
901 srcY += s->linesize * 8;
902 dsp->avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize , srcY , s->linesize, v->rnd);
903 dsp->avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
904 } else { // hpel mc
905 dxy = (my & 2) | ((mx & 2) >> 1);
906
907 if(!v->rnd)
908 dsp->avg_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
909 else
910 dsp->avg_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
911 }
912
913 if(s->flags & CODEC_FLAG_GRAY) return;
914 /* Chroma MC always uses qpel blilinear */
915 uvmx = (uvmx&3)<<1;
916 uvmy = (uvmy&3)<<1;
917 if(!v->rnd){
918 dsp->avg_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
919 dsp->avg_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
920 }else{
921 dsp->avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
922 dsp->avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
923 }
924 }
925
926 static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
927 {
928 int n = bfrac;
929
930 #if B_FRACTION_DEN==256
931 if(inv)
932 n -= 256;
933 if(!qs)
934 return 2 * ((value * n + 255) >> 9);
935 return (value * n + 128) >> 8;
936 #else
937 if(inv)
938 n -= B_FRACTION_DEN;
939 if(!qs)
940 return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
941 return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
942 #endif
943 }
944
945 /** Reconstruct motion vector for B-frame and do motion compensation
946 */
947 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mode)
948 {
949 if(v->use_ic) {
950 v->mv_mode2 = v->mv_mode;
951 v->mv_mode = MV_PMODE_INTENSITY_COMP;
952 }
953 if(direct) {
954 vc1_mc_1mv(v, 0);
955 vc1_interp_mc(v);
956 if(v->use_ic) v->mv_mode = v->mv_mode2;
957 return;
958 }
959 if(mode == BMV_TYPE_INTERPOLATED) {
960 vc1_mc_1mv(v, 0);
961 vc1_interp_mc(v);
962 if(v->use_ic) v->mv_mode = v->mv_mode2;
963 return;
964 }
965
966 if(v->use_ic && (mode == BMV_TYPE_BACKWARD)) v->mv_mode = v->mv_mode2;
967 vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
968 if(v->use_ic) v->mv_mode = v->mv_mode2;
969 }
970
971 static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mvtype)
972 {
973 MpegEncContext *s = &v->s;
974 int xy, wrap, off = 0;
975 int16_t *A, *B, *C;
976 int px, py;
977 int sum;
978 int r_x, r_y;
979 const uint8_t *is_intra = v->mb_type[0];
980
981 r_x = v->range_x;
982 r_y = v->range_y;
983 /* scale MV difference to be quad-pel */
984 dmv_x[0] <<= 1 - s->quarter_sample;
985 dmv_y[0] <<= 1 - s->quarter_sample;
986 dmv_x[1] <<= 1 - s->quarter_sample;
987 dmv_y[1] <<= 1 - s->quarter_sample;
988
989 wrap = s->b8_stride;
990 xy = s->block_index[0];
991
992 if(s->mb_intra) {
993 s->current_picture.motion_val[0][xy][0] =
994 s->current_picture.motion_val[0][xy][1] =
995 s->current_picture.motion_val[1][xy][0] =
996 s->current_picture.motion_val[1][xy][1] = 0;
997 return;
998 }
999 s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
1000 s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
1001 s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
1002 s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
1003
1004 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
1005 s->mv[0][0][0] = av_clip(s->mv[0][0][0], -60 - (s->mb_x << 6), (s->mb_width << 6) - 4 - (s->mb_x << 6));
1006 s->mv[0][0][1] = av_clip(s->mv[0][0][1], -60 - (s->mb_y << 6), (s->mb_height << 6) - 4 - (s->mb_y << 6));
1007 s->mv[1][0][0] = av_clip(s->mv[1][0][0], -60 - (s->mb_x << 6), (s->mb_width << 6) - 4 - (s->mb_x << 6));
1008 s->mv[1][0][1] = av_clip(s->mv[1][0][1], -60 - (s->mb_y << 6), (s->mb_height << 6) - 4 - (s->mb_y << 6));
1009 if(direct) {
1010 s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
1011 s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
1012 s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
1013 s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
1014 return;
1015 }
1016
1017 if((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
1018 C = s->current_picture.motion_val[0][xy - 2];
1019 A = s->current_picture.motion_val[0][xy - wrap*2];
1020 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1021 B = s->current_picture.motion_val[0][xy - wrap*2 + off];
1022
1023 if(!s->mb_x) C[0] = C[1] = 0;
1024 if(!s->first_slice_line) { // predictor A is not out of bounds
1025 if(s->mb_width == 1) {
1026 px = A[0];
1027 py = A[1];
1028 } else {
1029 px = mid_pred(A[0], B[0], C[0]);
1030 py = mid_pred(A[1], B[1], C[1]);
1031 }
1032 } else if(s->mb_x) { // predictor C is not out of bounds
1033 px = C[0];
1034 py = C[1];
1035 } else {
1036 px = py = 0;
1037 }
1038 /* Pullback MV as specified in 8.3.5.3.4 */
1039 {
1040 int qx, qy, X, Y;
1041 if(v->profile < PROFILE_ADVANCED) {
1042 qx = (s->mb_x << 5);
1043 qy = (s->mb_y << 5);
1044 X = (s->mb_width << 5) - 4;
1045 Y = (s->mb_height << 5) - 4;
1046 if(qx + px < -28) px = -28 - qx;
1047 if(qy + py < -28) py = -28 - qy;
1048 if(qx + px > X) px = X - qx;
1049 if(qy + py > Y) py = Y - qy;
1050 } else {
1051 qx = (s->mb_x << 6);
1052 qy = (s->mb_y << 6);
1053 X = (s->mb_width << 6) - 4;
1054 Y = (s->mb_height << 6) - 4;
1055 if(qx + px < -60) px = -60 - qx;
1056 if(qy + py < -60) py = -60 - qy;
1057 if(qx + px > X) px = X - qx;
1058 if(qy + py > Y) py = Y - qy;
1059 }
1060 }
1061 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
1062 if(0 && !s->first_slice_line && s->mb_x) {
1063 if(is_intra[xy - wrap])
1064 sum = FFABS(px) + FFABS(py);
1065 else
1066 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
1067 if(sum > 32) {
1068 if(get_bits1(&s->gb)) {
1069 px = A[0];
1070 py = A[1];
1071 } else {
1072 px = C[0];
1073 py = C[1];
1074 }
1075 } else {
1076 if(is_intra[xy - 2])
1077 sum = FFABS(px) + FFABS(py);
1078 else
1079 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
1080 if(sum > 32) {
1081 if(get_bits1(&s->gb)) {
1082 px = A[0];
1083 py = A[1];
1084 } else {
1085 px = C[0];
1086 py = C[1];
1087 }
1088 }
1089 }
1090 }
1091 /* store MV using signed modulus of MV range defined in 4.11 */
1092 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
1093 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
1094 }
1095 if((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
1096 C = s->current_picture.motion_val[1][xy - 2];
1097 A = s->current_picture.motion_val[1][xy - wrap*2];
1098 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1099 B = s->current_picture.motion_val[1][xy - wrap*2 + off];
1100
1101 if(!s->mb_x) C[0] = C[1] = 0;
1102 if(!s->first_slice_line) { // predictor A is not out of bounds
1103 if(s->mb_width == 1) {
1104 px = A[0];
1105 py = A[1];
1106 } else {
1107 px = mid_pred(A[0], B[0], C[0]);
1108 py = mid_pred(A[1], B[1], C[1]);
1109 }
1110 } else if(s->mb_x) { // predictor C is not out of bounds
1111 px = C[0];
1112 py = C[1];
1113 } else {
1114 px = py = 0;
1115 }
1116 /* Pullback MV as specified in 8.3.5.3.4 */
1117 {
1118 int qx, qy, X, Y;
1119 if(v->profile < PROFILE_ADVANCED) {
1120 qx = (s->mb_x << 5);
1121 qy = (s->mb_y << 5);
1122 X = (s->mb_width << 5) - 4;
1123 Y = (s->mb_height << 5) - 4;
1124 if(qx + px < -28) px = -28 - qx;
1125 if(qy + py < -28) py = -28 - qy;
1126 if(qx + px > X) px = X - qx;
1127 if(qy + py > Y) py = Y - qy;
1128 } else {
1129 qx = (s->mb_x << 6);
1130 qy = (s->mb_y << 6);
1131 X = (s->mb_width << 6) - 4;
1132 Y = (s->mb_height << 6) - 4;
1133 if(qx + px < -60) px = -60 - qx;
1134 if(qy + py < -60) py = -60 - qy;
1135 if(qx + px > X) px = X - qx;
1136 if(qy + py > Y) py = Y - qy;
1137 }
1138 }
1139 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
1140 if(0 && !s->first_slice_line && s->mb_x) {
1141 if(is_intra[xy - wrap])
1142 sum = FFABS(px) + FFABS(py);
1143 else
1144 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
1145 if(sum > 32) {
1146 if(get_bits1(&s->gb)) {
1147 px = A[0];
1148 py = A[1];
1149 } else {
1150 px = C[0];
1151 py = C[1];
1152 }
1153 } else {
1154 if(is_intra[xy - 2])
1155 sum = FFABS(px) + FFABS(py);
1156 else
1157 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
1158 if(sum > 32) {
1159 if(get_bits1(&s->gb)) {
1160 px = A[0];
1161 py = A[1];
1162 } else {
1163 px = C[0];
1164 py = C[1];
1165 }
1166 }
1167 }
1168 }
1169 /* store MV using signed modulus of MV range defined in 4.11 */
1170
1171 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
1172 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
1173 }
1174 s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
1175 s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
1176 s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
1177 s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
1178 }
1179
1180 /** Get predicted DC value for I-frames only
1181 * prediction dir: left=0, top=1
1182 * @param s MpegEncContext
1183 * @param overlap flag indicating that overlap filtering is used
1184 * @param pq integer part of picture quantizer
1185 * @param[in] n block index in the current MB
1186 * @param dc_val_ptr Pointer to DC predictor
1187 * @param dir_ptr Prediction direction for use in AC prediction
1188 */
1189 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
1190 int16_t **dc_val_ptr, int *dir_ptr)
1191 {
1192 int a, b, c, wrap, pred, scale;
1193 int16_t *dc_val;
1194 static const uint16_t dcpred[32] = {
1195 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
1196 114, 102, 93, 85, 79, 73, 68, 64,
1197 60, 57, 54, 51, 49, 47, 45, 43,
1198 41, 39, 38, 37, 35, 34, 33
1199 };
1200
1201 /* find prediction - wmv3_dc_scale always used here in fact */
1202 if (n < 4) scale = s->y_dc_scale;
1203 else scale = s->c_dc_scale;
1204
1205 wrap = s->block_wrap[n];
1206 dc_val= s->dc_val[0] + s->block_index[n];
1207
1208 /* B A
1209 * C X
1210 */
1211 c = dc_val[ - 1];
1212 b = dc_val[ - 1 - wrap];
1213 a = dc_val[ - wrap];
1214
1215 if (pq < 9 || !overlap)
1216 {
1217 /* Set outer values */
1218 if (s->first_slice_line && (n!=2 && n!=3)) b=a=dcpred[scale];
1219 if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=dcpred[scale];
1220 }
1221 else
1222 {
1223 /* Set outer values */
1224 if (s->first_slice_line && (n!=2 && n!=3)) b=a=0;
1225 if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=0;
1226 }
1227
1228 if (abs(a - b) <= abs(b - c)) {
1229 pred = c;
1230 *dir_ptr = 1;//left
1231 } else {
1232 pred = a;
1233 *dir_ptr = 0;//top
1234 }
1235
1236 /* update predictor */
1237 *dc_val_ptr = &dc_val[0];
1238 return pred;
1239 }
1240
1241
1242 /** Get predicted DC value
1243 * prediction dir: left=0, top=1
1244 * @param s MpegEncContext
1245 * @param overlap flag indicating that overlap filtering is used
1246 * @param pq integer part of picture quantizer
1247 * @param[in] n block index in the current MB
1248 * @param a_avail flag indicating top block availability
1249 * @param c_avail flag indicating left block availability
1250 * @param dc_val_ptr Pointer to DC predictor
1251 * @param dir_ptr Prediction direction for use in AC prediction
1252 */
1253 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
1254 int a_avail, int c_avail,
1255 int16_t **dc_val_ptr, int *dir_ptr)
1256 {
1257 int a, b, c, wrap, pred;
1258 int16_t *dc_val;
1259 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1260 int q1, q2 = 0;
1261
1262 wrap = s->block_wrap[n];
1263 dc_val= s->dc_val[0] + s->block_index[n];
1264
1265 /* B A
1266 * C X
1267 */
1268 c = dc_val[ - 1];
1269 b = dc_val[ - 1 - wrap];
1270 a = dc_val[ - wrap];
1271 /* scale predictors if needed */
1272 q1 = s->current_picture.qscale_table[mb_pos];
1273 if(c_avail && (n!= 1 && n!=3)) {
1274 q2 = s->current_picture.qscale_table[mb_pos - 1];
1275 if(q2 && q2 != q1)
1276 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
1277 }
1278 if(a_avail && (n!= 2 && n!=3)) {
1279 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
1280 if(q2 && q2 != q1)
1281 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
1282 }
1283 if(a_avail && c_avail && (n!=3)) {
1284 int off = mb_pos;
1285 if(n != 1) off--;
1286 if(n != 2) off -= s->mb_stride;
1287 q2 = s->current_picture.qscale_table[off];
1288 if(q2 && q2 != q1)
1289 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
1290 }
1291
1292 if(a_avail && c_avail) {
1293 if(abs(a - b) <= abs(b - c)) {
1294 pred = c;
1295 *dir_ptr = 1;//left
1296 } else {
1297 pred = a;
1298 *dir_ptr = 0;//top
1299 }
1300 } else if(a_avail) {
1301 pred = a;
1302 *dir_ptr = 0;//top
1303 } else if(c_avail) {
1304 pred = c;
1305 *dir_ptr = 1;//left
1306 } else {
1307 pred = 0;
1308 *dir_ptr = 1;//left
1309 }
1310
1311 /* update predictor */
1312 *dc_val_ptr = &dc_val[0];
1313 return pred;
1314 }
1315
1316 /** @} */ // Block group
1317
1318 /**
1319 * @defgroup vc1_std_mb VC1 Macroblock-level functions in Simple/Main Profiles
1320 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1321 * @{
1322 */
1323
1324 static inline int vc1_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
1325 {
1326 int xy, wrap, pred, a, b, c;
1327
1328 xy = s->block_index[n];
1329 wrap = s->b8_stride;
1330
1331 /* B C
1332 * A X
1333 */
1334 a = s->coded_block[xy - 1 ];
1335 b = s->coded_block[xy - 1 - wrap];
1336 c = s->coded_block[xy - wrap];
1337
1338 if (b == c) {
1339 pred = a;
1340 } else {
1341 pred = c;
1342 }
1343
1344 /* store value */
1345 *coded_block_ptr = &s->coded_block[xy];
1346
1347 return pred;
1348 }
1349
1350 /**
1351 * Decode one AC coefficient
1352 * @param v The VC1 context
1353 * @param last Last coefficient
1354 * @param skip How much zero coefficients to skip
1355 * @param value Decoded AC coefficient value
1356 * @param codingset set of VLC to decode data
1357 * @see 8.1.3.4
1358 */
1359 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip, int *value, int codingset)
1360 {
1361 GetBitContext *gb = &v->s.gb;
1362 int index, escape, run = 0, level = 0, lst = 0;
1363
1364 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
1365 if (index != vc1_ac_sizes[codingset] - 1) {
1366 run = vc1_index_decode_table[codingset][index][0];
1367 level = vc1_index_decode_table[codingset][index][1];
1368 lst = index >= vc1_last_decode_table[codingset];
1369 if(get_bits1(gb))
1370 level = -level;
1371 } else {
1372 escape = decode210(gb);
1373 if (escape != 2) {
1374 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
1375 run = vc1_index_decode_table[codingset][index][0];
1376 level = vc1_index_decode_table[codingset][index][1];
1377 lst = index >= vc1_last_decode_table[codingset];
1378 if(escape == 0) {
1379 if(lst)
1380 level += vc1_last_delta_level_table[codingset][run];
1381 else
1382 level += vc1_delta_level_table[codingset][run];
1383 } else {
1384 if(lst)
1385 run += vc1_last_delta_run_table[codingset][level] + 1;
1386 else
1387 run += vc1_delta_run_table[codingset][level] + 1;
1388 }
1389 if(get_bits1(gb))
1390 level = -level;
1391 } else {
1392 int sign;
1393 lst = get_bits1(gb);
1394 if(v->s.esc3_level_length == 0) {
1395 if(v->pq < 8 || v->dquantfrm) { // table 59
1396 v->s.esc3_level_length = get_bits(gb, 3);
1397 if(!v->s.esc3_level_length)
1398 v->s.esc3_level_length = get_bits(gb, 2) + 8;
1399 } else { //table 60
1400 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
1401 }
1402 v->s.esc3_run_length = 3 + get_bits(gb, 2);
1403 }
1404 run = get_bits(gb, v->s.esc3_run_length);
1405 sign = get_bits1(gb);
1406 level = get_bits(gb, v->s.esc3_level_length);
1407 if(sign)
1408 level = -level;
1409 }
1410 }
1411
1412 *last = lst;
1413 *skip = run;
1414 *value = level;
1415 }
1416
1417 /** Decode intra block in intra frames - should be faster than decode_intra_block
1418 * @param v VC1Context
1419 * @param block block to decode
1420 * @param[in] n subblock index
1421 * @param coded are AC coeffs present or not
1422 * @param codingset set of VLC to decode data
1423 */
1424 static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset)
1425 {
1426 GetBitContext *gb = &v->s.gb;
1427 MpegEncContext *s = &v->s;
1428 int dc_pred_dir = 0; /* Direction of the DC prediction used */
1429 int i;
1430 int16_t *dc_val;
1431 int16_t *ac_val, *ac_val2;
1432 int dcdiff;
1433
1434 /* Get DC differential */
1435 if (n < 4) {
1436 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1437 } else {
1438 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1439 }
1440 if (dcdiff < 0){
1441 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
1442 return -1;
1443 }
1444 if (dcdiff)
1445 {
1446 if (dcdiff == 119 /* ESC index value */)
1447 {
1448 /* TODO: Optimize */
1449 if (v->pq == 1) dcdiff = get_bits(gb, 10);
1450 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
1451 else dcdiff = get_bits(gb, 8);
1452 }
1453 else
1454 {
1455 if (v->pq == 1)
1456 dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
1457 else if (v->pq == 2)
1458 dcdiff = (dcdiff<<1) + get_bits1(gb) - 1;
1459 }
1460 if (get_bits1(gb))
1461 dcdiff = -dcdiff;
1462 }
1463
1464 /* Prediction */
1465 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
1466 *dc_val = dcdiff;
1467
1468 /* Store the quantized DC coeff, used for prediction */
1469 if (n < 4) {
1470 block[0] = dcdiff * s->y_dc_scale;
1471 } else {
1472 block[0] = dcdiff * s->c_dc_scale;
1473 }
1474 /* Skip ? */
1475 if (!coded) {
1476 goto not_coded;
1477 }
1478
1479 //AC Decoding
1480 i = 1;
1481
1482 {
1483 int last = 0, skip, value;
1484 const int8_t *zz_table;
1485 int scale;
1486 int k;
1487
1488 scale = v->pq * 2 + v->halfpq;
1489
1490 if(v->s.ac_pred) {
1491 if(!dc_pred_dir)
1492 zz_table = wmv1_scantable[2];
1493 else
1494 zz_table = wmv1_scantable[3];
1495 } else
1496 zz_table = wmv1_scantable[1];
1497
1498 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1499 ac_val2 = ac_val;
1500 if(dc_pred_dir) //left
1501 ac_val -= 16;
1502 else //top
1503 ac_val -= 16 * s->block_wrap[n];
1504
1505 while (!last) {
1506 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1507 i += skip;
1508 if(i > 63)
1509 break;
1510 block[zz_table[i++]] = value;
1511 }
1512
1513 /* apply AC prediction if needed */
1514 if(s->ac_pred) {
1515 if(dc_pred_dir) { //left
1516 for(k = 1; k < 8; k++)
1517 block[k << 3] += ac_val[k];
1518 } else { //top
1519 for(k = 1; k < 8; k++)
1520 block[k] += ac_val[k + 8];
1521 }
1522 }
1523 /* save AC coeffs for further prediction */
1524 for(k = 1; k < 8; k++) {
1525 ac_val2[k] = block[k << 3];
1526 ac_val2[k + 8] = block[k];
1527 }
1528
1529 /* scale AC coeffs */
1530 for(k = 1; k < 64; k++)
1531 if(block[k]) {
1532 block[k] *= scale;
1533 if(!v->pquantizer)
1534 block[k] += (block[k] < 0) ? -v->pq : v->pq;
1535 }
1536
1537 if(s->ac_pred) i = 63;
1538 }
1539
1540 not_coded:
1541 if(!coded) {
1542 int k, scale;
1543 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1544 ac_val2 = ac_val;
1545
1546 i = 0;
1547 scale = v->pq * 2 + v->halfpq;
1548 memset(ac_val2, 0, 16 * 2);
1549 if(dc_pred_dir) {//left
1550 ac_val -= 16;
1551 if(s->ac_pred)
1552 memcpy(ac_val2, ac_val, 8 * 2);
1553 } else {//top
1554 ac_val -= 16 * s->block_wrap[n];
1555 if(s->ac_pred)
1556 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1557 }
1558
1559 /* apply AC prediction if needed */
1560 if(s->ac_pred) {
1561 if(dc_pred_dir) { //left
1562 for(k = 1; k < 8; k++) {
1563 block[k << 3] = ac_val[k] * scale;
1564 if(!v->pquantizer && block[k << 3])
1565 block[k << 3] += (block[k << 3] < 0) ? -v->pq : v->pq;
1566 }
1567 } else { //top
1568 for(k = 1; k < 8; k++) {
1569 block[k] = ac_val[k + 8] * scale;
1570 if(!v->pquantizer && block[k])
1571 block[k] += (block[k] < 0) ? -v->pq : v->pq;
1572 }
1573 }
1574 i = 63;
1575 }
1576 }
1577 s->block_last_index[n] = i;
1578
1579 return 0;
1580 }
1581
1582 /** Decode intra block in intra frames - should be faster than decode_intra_block
1583 * @param v VC1Context
1584 * @param block block to decode
1585 * @param[in] n subblock number
1586 * @param coded are AC coeffs present or not
1587 * @param codingset set of VLC to decode data
1588 * @param mquant quantizer value for this macroblock
1589 */
1590 static int vc1_decode_i_block_adv(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset, int mquant)
1591 {
1592 GetBitContext *gb = &v->s.gb;
1593 MpegEncContext *s = &v->s;
1594 int dc_pred_dir = 0; /* Direction of the DC prediction used */
1595 int i;
1596 int16_t *dc_val;
1597 int16_t *ac_val, *ac_val2;
1598 int dcdiff;
1599 int a_avail = v->a_avail, c_avail = v->c_avail;
1600 int use_pred = s->ac_pred;
1601 int scale;
1602 int q1, q2 = 0;
1603 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1604
1605 /* Get DC differential */
1606 if (n < 4) {
1607 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1608 } else {
1609 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1610 }
1611 if (dcdiff < 0){
1612 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
1613 return -1;
1614 }
1615 if (dcdiff)
1616 {
1617 if (dcdiff == 119 /* ESC index value */)
1618 {
1619 /* TODO: Optimize */
1620 if (mquant == 1) dcdiff = get_bits(gb, 10);
1621 else if (mquant == 2) dcdiff = get_bits(gb, 9);
1622 else dcdiff = get_bits(gb, 8);
1623 }
1624 else
1625 {
1626 if (mquant == 1)
1627 dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
1628 else if (mquant == 2)
1629 dcdiff = (dcdiff<<1) + get_bits1(gb) - 1;
1630 }
1631 if (get_bits1(gb))
1632 dcdiff = -dcdiff;
1633 }
1634
1635 /* Prediction */
1636 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
1637 *dc_val = dcdiff;
1638
1639 /* Store the quantized DC coeff, used for prediction */
1640 if (n < 4) {
1641 block[0] = dcdiff * s->y_dc_scale;
1642 } else {
1643 block[0] = dcdiff * s->c_dc_scale;
1644 }
1645
1646 //AC Decoding
1647 i = 1;
1648
1649 /* check if AC is needed at all */
1650 if(!a_avail && !c_avail) use_pred = 0;
1651 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1652 ac_val2 = ac_val;
1653
1654 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
1655
1656 if(dc_pred_dir) //left
1657 ac_val -= 16;
1658 else //top
1659 ac_val -= 16 * s->block_wrap[n];
1660
1661 q1 = s->current_picture.qscale_table[mb_pos];
1662 if(dc_pred_dir && c_avail && mb_pos) q2 = s->current_picture.qscale_table[mb_pos - 1];
1663 if(!dc_pred_dir && a_avail && mb_pos >= s->mb_stride) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
1664 if(dc_pred_dir && n==1) q2 = q1;
1665 if(!dc_pred_dir && n==2) q2 = q1;
1666 if(n==3) q2 = q1;
1667
1668 if(coded) {
1669 int last = 0, skip, value;
1670 const int8_t *zz_table;
1671 int k;
1672
1673 if(v->s.ac_pred) {
1674 if(!dc_pred_dir)
1675 zz_table = wmv1_scantable[2];
1676 else
1677 zz_table = wmv1_scantable[3];
1678 } else
1679 zz_table = wmv1_scantable[1];
1680
1681 while (!last) {
1682 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1683 i += skip;
1684 if(i > 63)
1685 break;
1686 block[zz_table[i++]] = value;
1687 }
1688
1689 /* apply AC prediction if needed */
1690 if(use_pred) {
1691 /* scale predictors if needed*/
1692 if(q2 && q1!=q2) {
1693 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1694 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1695
1696 if(dc_pred_dir) { //left
1697 for(k = 1; k < 8; k++)
1698 block[k << 3] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1699 } else { //top
1700 for(k = 1; k < 8; k++)
1701 block[k] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1702 }
1703 } else {
1704 if(dc_pred_dir) { //left
1705 for(k = 1; k < 8; k++)
1706 block[k << 3] += ac_val[k];
1707 } else { //top
1708 for(k = 1; k < 8; k++)
1709 block[k] += ac_val[k + 8];
1710 }
1711 }
1712 }
1713 /* save AC coeffs for further prediction */
1714 for(k = 1; k < 8; k++) {
1715 ac_val2[k] = block[k << 3];
1716 ac_val2[k + 8] = block[k];
1717 }
1718
1719 /* scale AC coeffs */
1720 for(k = 1; k < 64; k++)
1721 if(block[k]) {
1722 block[k] *= scale;
1723 if(!v->pquantizer)
1724 block[k] += (block[k] < 0) ? -mquant : mquant;
1725 }
1726
1727 if(use_pred) i = 63;
1728 } else { // no AC coeffs
1729 int k;
1730
1731 memset(ac_val2, 0, 16 * 2);
1732 if(dc_pred_dir) {//left
1733 if(use_pred) {
1734 memcpy(ac_val2, ac_val, 8 * 2);
1735 if(q2 && q1!=q2) {
1736 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1737 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1738 for(k = 1; k < 8; k++)
1739 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1740 }
1741 }
1742 } else {//top
1743 if(use_pred) {
1744 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1745 if(q2 && q1!=q2) {
1746 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1747 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1748 for(k = 1; k < 8; k++)
1749 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1750 }
1751 }
1752 }
1753
1754 /* apply AC prediction if needed */
1755 if(use_pred) {
1756 if(dc_pred_dir) { //left
1757 for(k = 1; k < 8; k++) {
1758 block[k << 3] = ac_val2[k] * scale;
1759 if(!v->pquantizer && block[k << 3])
1760 block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
1761 }
1762 } else { //top
1763 for(k = 1; k < 8; k++) {
1764 block[k] = ac_val2[k + 8] * scale;
1765 if(!v->pquantizer && block[k])
1766 block[k] += (block[k] < 0) ? -mquant : mquant;
1767 }
1768 }
1769 i = 63;
1770 }
1771 }
1772 s->block_last_index[n] = i;
1773
1774 return 0;
1775 }
1776
1777 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
1778 * @param v VC1Context
1779 * @param block block to decode
1780 * @param[in] n subblock index
1781 * @param coded are AC coeffs present or not
1782 * @param mquant block quantizer
1783 * @param codingset set of VLC to decode data
1784 */
1785 static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n, int coded, int mquant, int codingset)
1786 {
1787 GetBitContext *gb = &v->s.gb;
1788 MpegEncContext *s = &v->s;
1789 int dc_pred_dir = 0; /* Direction of the DC prediction used */
1790 int i;
1791 int16_t *dc_val;
1792 int16_t *ac_val, *ac_val2;
1793 int dcdiff;
1794 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1795 int a_avail = v->a_avail, c_avail = v->c_avail;
1796 int use_pred = s->ac_pred;
1797 int scale;
1798 int q1, q2 = 0;
1799
1800 s->dsp.clear_block(block);
1801
1802 /* XXX: Guard against dumb values of mquant */
1803 mquant = (mquant < 1) ? 0 : ( (mquant>31) ? 31 : mquant );
1804
1805 /* Set DC scale - y and c use the same */
1806 s->y_dc_scale = s->y_dc_scale_table[mquant];
1807 s->c_dc_scale = s->c_dc_scale_table[mquant];
1808
1809 /* Get DC differential */
1810 if (n < 4) {
1811 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1812 } else {
1813 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1814 }
1815 if (dcdiff < 0){
1816 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
1817 return -1;
1818 }
1819 if (dcdiff)
1820 {
1821 if (dcdiff == 119 /* ESC index value */)
1822 {
1823 /* TODO: Optimize */
1824 if (mquant == 1) dcdiff = get_bits(gb, 10);
1825 else if (mquant == 2) dcdiff = get_bits(gb, 9);
1826 else dcdiff = get_bits(gb, 8);
1827 }
1828 else
1829 {
1830 if (mquant == 1)
1831 dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
1832 else if (mquant == 2)
1833 dcdiff = (dcdiff<<1) + get_bits1(gb) - 1;
1834 }
1835 if (get_bits1(gb))
1836 dcdiff = -dcdiff;
1837 }
1838
1839 /* Prediction */
1840 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
1841 *dc_val = dcdiff;
1842
1843 /* Store the quantized DC coeff, used for prediction */
1844
1845 if (n < 4) {
1846 block[0] = dcdiff * s->y_dc_scale;
1847 } else {
1848 block[0] = dcdiff * s->c_dc_scale;
1849 }
1850
1851 //AC Decoding
1852 i = 1;
1853
1854 /* check if AC is needed at all and adjust direction if needed */
1855 if(!a_avail) dc_pred_dir = 1;
1856 if(!c_avail) dc_pred_dir = 0;
1857 if(!a_avail && !c_avail) use_pred = 0;
1858 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1859 ac_val2 = ac_val;
1860
1861 scale = mquant * 2 + v->halfpq;
1862
1863 if(dc_pred_dir) //left
1864 ac_val -= 16;
1865 else //top
1866 ac_val -= 16 * s->block_wrap[n];
1867
1868 q1 = s->current_picture.qscale_table[mb_pos];
1869 if(dc_pred_dir && c_avail && mb_pos) q2 = s->current_picture.qscale_table[mb_pos - 1];
1870 if(!dc_pred_dir && a_avail && mb_pos >= s->mb_stride) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
1871 if(dc_pred_dir && n==1) q2 = q1;
1872 if(!dc_pred_dir && n==2) q2 = q1;
1873 if(n==3) q2 = q1;
1874
1875 if(coded) {
1876 int last = 0, skip, value;
1877 const int8_t *zz_table;
1878 int k;
1879
1880 zz_table = wmv1_scantable[0];
1881
1882 while (!last) {
1883 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1884 i += skip;
1885 if(i > 63)
1886 break;
1887 block[zz_table[i++]] = value;
1888 }
1889
1890 /* apply AC prediction if needed */
1891 if(use_pred) {
1892 /* scale predictors if needed*/
1893 if(q2 && q1!=q2) {
1894 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1895 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1896
1897 if(dc_pred_dir) { //left
1898 for(k = 1; k < 8; k++)
1899 block[k << 3] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1900 } else { //top
1901 for(k = 1; k < 8; k++)
1902 block[k] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1903 }
1904 } else {
1905 if(dc_pred_dir) { //left
1906 for(k = 1; k < 8; k++)
1907 block[k << 3] += ac_val[k];
1908 } else { //top
1909 for(k = 1; k < 8; k++)
1910 block[k] += ac_val[k + 8];
1911 }
1912 }
1913 }
1914 /* save AC coeffs for further prediction */
1915 for(k = 1; k < 8; k++) {
1916 ac_val2[k] = block[k << 3];
1917 ac_val2[k + 8] = block[k];
1918 }
1919
1920 /* scale AC coeffs */
1921 for(k = 1; k < 64; k++)
1922 if(block[k]) {
1923 block[k] *= scale;
1924 if(!v->pquantizer)
1925 block[k] += (block[k] < 0) ? -mquant : mquant;
1926 }
1927
1928 if(use_pred) i = 63;
1929 } else { // no AC coeffs
1930 int k;
1931
1932 memset(ac_val2, 0, 16 * 2);
1933 if(dc_pred_dir) {//left
1934 if(use_pred) {
1935 memcpy(ac_val2, ac_val, 8 * 2);
1936 if(q2 && q1!=q2) {
1937 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1938 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1939 for(k = 1; k < 8; k++)
1940 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1941 }
1942 }
1943 } else {//top
1944 if(use_pred) {
1945 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1946 if(q2 && q1!=q2) {
1947 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1948 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1949 for(k = 1; k < 8; k++)
1950 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1951 }
1952 }
1953 }
1954
1955 /* apply AC prediction if needed */
1956 if(use_pred) {
1957 if(dc_pred_dir) { //left
1958 for(k = 1; k < 8; k++) {
1959 block[k << 3] = ac_val2[k] * scale;
1960 if(!v->pquantizer && block[k << 3])
1961 block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
1962 }
1963 } else { //top
1964 for(k = 1; k < 8; k++) {
1965 block[k] = ac_val2[k + 8] * scale;
1966 if(!v->pquantizer && block[k])
1967 block[k] += (block[k] < 0) ? -mquant : mquant;
1968 }
1969 }
1970 i = 63;
1971 }
1972 }
1973 s->block_last_index[n] = i;
1974
1975 return 0;
1976 }
1977
1978 /** Decode P block
1979 */
1980 static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n, int mquant, int ttmb, int first_block,
1981 uint8_t *dst, int linesize, int skip_block, int apply_filter, int cbp_top, int cbp_left)
1982 {
1983 MpegEncContext *s = &v->s;
1984 GetBitContext *gb = &s->gb;
1985 int i, j;
1986 int subblkpat = 0;
1987 int scale, off, idx, last, skip, value;
1988 int ttblk = ttmb & 7;
1989 int pat = 0;
1990
1991 s->dsp.clear_block(block);
1992
1993 if(ttmb == -1) {
1994 ttblk = ff_vc1_ttblk_to_tt[v->tt_index][get_vlc2(gb, ff_vc1_ttblk_vlc[v->tt_index].table, VC1_TTBLK_VLC_BITS, 1)];
1995 }
1996 if(ttblk == TT_4X4) {
1997 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
1998 }
1999 if((ttblk != TT_8X8 && ttblk != TT_4X4) && (v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))) {
2000 subblkpat = decode012(gb);
2001 if(subblkpat) subblkpat ^= 3; //swap decoded pattern bits
2002 if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) ttblk = TT_8X4;
2003 if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) ttblk = TT_4X8;
2004 }
2005 scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
2006
2007 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
2008 if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
2009 subblkpat = 2 - (ttblk == TT_8X4_TOP);
2010 ttblk = TT_8X4;
2011 }
2012 if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
2013 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
2014 ttblk = TT_4X8;
2015 }
2016 switch(ttblk) {
2017 case TT_8X8:
2018 pat = 0xF;
2019 i = 0;
2020 last = 0;
2021 while (!last) {
2022 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2023 i += skip;
2024 if(i > 63)
2025 break;
2026 idx = wmv1_scantable[0][i++];
2027 block[idx] = value * scale;
2028 if(!v->pquantizer)
2029 block[idx] += (block[idx] < 0) ? -mquant : mquant;
2030 }
2031 if(!skip_block){
2032 if(i==1)
2033 s->dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
2034 else{
2035 s->dsp.vc1_inv_trans_8x8(block);
2036 s->dsp.add_pixels_clamped(block, dst, linesize);
2037 }
2038 if(apply_filter && cbp_top & 0xC)
2039 s->dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
2040 if(apply_filter && cbp_left & 0xA)
2041 s->dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
2042 }
2043 break;
2044 case TT_4X4:
2045 pat = ~subblkpat & 0xF;
2046 for(j = 0; j < 4; j++) {
2047 last = subblkpat & (1 << (3 - j));
2048 i = 0;
2049 off = (j & 1) * 4 + (j & 2) * 16;
2050 while (!last) {
2051 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2052 i += skip;
2053 if(i > 15)
2054 break;
2055 idx = ff_vc1_simple_progressive_4x4_zz[i++];
2056 block[idx + off] = value * scale;
2057 if(!v->pquantizer)
2058 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
2059 }
2060 if(!(subblkpat & (1 << (3 - j))) && !skip_block){
2061 if(i==1)
2062 s->dsp.vc1_inv_trans_4x4_dc(dst + (j&1)*4 + (j&2)*2*linesize, linesize, block + off);
2063 else
2064 s->dsp.vc1_inv_trans_4x4(dst + (j&1)*4 + (j&2)*2*linesize, linesize, block + off);
2065 if(apply_filter && (j&2 ? pat & (1<<(j-2)) : (cbp_top & (1 << (j + 2)))))
2066 s->dsp.vc1_v_loop_filter4(dst + (j&1)*4 + (j&2)*2*linesize, linesize, v->pq);
2067 if(apply_filter && (j&1 ? pat & (1<<(j-1)) : (cbp_left & (1 << (j + 1)))))
2068 s->dsp.vc1_h_loop_filter4(dst + (j&1)*4 + (j&2)*2*linesize, linesize, v->pq);
2069 }
2070 }
2071 break;
2072 case TT_8X4:
2073 pat = ~((subblkpat & 2)*6 + (subblkpat & 1)*3) & 0xF;
2074 for(j = 0; j < 2; j++) {
2075 last = subblkpat & (1 << (1 - j));
2076 i = 0;
2077 off = j * 32;
2078 while (!last) {
2079 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2080 i += skip;
2081 if(i > 31)
2082 break;
2083 idx = v->zz_8x4[i++]+off;
2084 block[idx] = value * scale;
2085 if(!v->pquantizer)
2086 block[idx] += (block[idx] < 0) ? -mquant : mquant;
2087 }
2088 if(!(subblkpat & (1 << (1 - j))) && !skip_block){
2089 if(i==1)
2090 s->dsp.vc1_inv_trans_8x4_dc(dst + j*4*linesize, linesize, block + off);
2091 else
2092 s->dsp.vc1_inv_trans_8x4(dst + j*4*linesize, linesize, block + off);
2093 if(apply_filter && j ? pat & 0x3 : (cbp_top & 0xC))
2094 s->dsp.vc1_v_loop_filter8(dst + j*4*linesize, linesize, v->pq);
2095 if(apply_filter && cbp_left & (2 << j))
2096 s->dsp.vc1_h_loop_filter4(dst + j*4*linesize, linesize, v->pq);
2097 }
2098 }
2099 break;
2100 case TT_4X8:
2101 pat = ~(subblkpat*5) & 0xF;
2102 for(j = 0; j < 2; j++) {
2103 last = subblkpat & (1 << (1 - j));
2104 i = 0;
2105 off = j * 4;
2106 while (!last) {
2107 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2108 i += skip;
2109 if(i > 31)
2110 break;
2111 idx = v->zz_4x8[i++]+off;
2112 block[idx] = value * scale;
2113 if(!v->pquantizer)
2114 block[idx] += (block[idx] < 0) ? -mquant : mquant;
2115 }
2116 if(!(subblkpat & (1 << (1 - j))) && !skip_block){
2117 if(i==1)
2118 s->dsp.vc1_inv_trans_4x8_dc(dst + j*4, linesize, block + off);
2119 else
2120 s->dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
2121 if(apply_filter && cbp_top & (2 << j))
2122 s->dsp.vc1_v_loop_filter4(dst + j*4, linesize, v->pq);
2123 if(apply_filter && j ? pat & 0x5 : (cbp_left & 0xA))
2124 s->dsp.vc1_h_loop_filter8(dst + j*4, linesize, v->pq);
2125 }
2126 }
2127 break;
2128 }
2129 return pat;
2130 }
2131
2132 /** @} */ // Macroblock group
2133
2134 static const int size_table [6] = { 0, 2, 3, 4, 5, 8 };
2135 static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
2136
2137 /** Decode one P-frame MB (in Simple/Main profile)
2138 */
2139 static int vc1_decode_p_mb(VC1Context *v)
2140 {
2141 MpegEncContext *s = &v->s;
2142 GetBitContext *gb = &s->gb;
2143 int i, j;
2144 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2145 int cbp; /* cbp decoding stuff */
2146 int mqdiff, mquant; /* MB quantization */
2147 int ttmb = v->ttfrm; /* MB Transform type */
2148
2149 int mb_has_coeffs = 1; /* last_flag */
2150 int dmv_x, dmv_y; /* Differential MV components */
2151 int index, index1; /* LUT indexes */
2152 int val, sign; /* temp values */
2153 int first_block = 1;
2154 int dst_idx, off;
2155 int skipped, fourmv;
2156 int block_cbp = 0, pat;
2157 int apply_loop_filter;
2158
2159 mquant = v->pq; /* Loosy initialization */
2160
2161 if (v->mv_type_is_raw)
2162 fourmv = get_bits1(gb);
2163 else
2164 fourmv = v->mv_type_mb_plane[mb_pos];
2165 if (v->skip_is_raw)
2166 skipped = get_bits1(gb);
2167 else
2168 skipped = v->s.mbskip_table[mb_pos];
2169
2170 apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY);
2171 if (!fourmv) /* 1MV mode */
2172 {
2173 if (!skipped)
2174 {
2175 GET_MVDATA(dmv_x, dmv_y);
2176
2177 if (s->mb_intra) {
2178 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2179 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2180 }
2181 s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
2182 vc1_pred_mv(s, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
2183
2184 /* FIXME Set DC val for inter block ? */
2185 if (s->mb_intra && !mb_has_coeffs)
2186 {
2187 GET_MQUANT();
2188 s->ac_pred = get_bits1(gb);
2189 cbp = 0;
2190 }
2191 else if (mb_has_coeffs)
2192 {
2193 if (s->mb_intra) s->ac_pred = get_bits1(gb);
2194 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2195 GET_MQUANT();
2196 }
2197 else
2198 {
2199 mquant = v->pq;
2200 cbp = 0;
2201 }
2202 s->current_picture.qscale_table[mb_pos] = mquant;
2203
2204 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
2205 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
2206 VC1_TTMB_VLC_BITS, 2);
2207 if(!s->mb_intra) vc1_mc_1mv(v, 0);
2208 dst_idx = 0;
2209 for (i=0; i<6; i++)
2210 {
2211 s->dc_val[0][s->block_index[i]] = 0;
2212 dst_idx += i >> 2;
2213 val = ((cbp >> (5 - i)) & 1);
2214 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2215 v->mb_type[0][s->block_index[i]] = s->mb_intra;
2216 if(s->mb_intra) {
2217 /* check if prediction blocks A and C are available */
2218 v->a_avail = v->c_avail = 0;
2219 if(i == 2 || i == 3 || !s->first_slice_line)
2220 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2221 if(i == 1 || i == 3 || s->mb_x)
2222 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2223
2224 vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
2225 if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
2226 s->dsp.vc1_inv_trans_8x8(s->block[i]);
2227 if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
2228 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
2229 if(v->pq >= 9 && v->overlap) {
2230 if(v->c_avail)
2231 s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
2232 if(v->a_avail)
2233 s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
2234 }
2235 if(apply_loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){
2236 int left_cbp, top_cbp;
2237 if(i & 4){
2238 left_cbp = v->cbp[s->mb_x - 1] >> (i * 4);
2239 top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
2240 }else{
2241 left_cbp = (i & 1) ? (cbp >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4));
2242 top_cbp = (i & 2) ? (cbp >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));
2243 }
2244 if(left_cbp & 0xC)
2245 s->dsp.vc1_v_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2246 if(top_cbp & 0xA)
2247 s->dsp.vc1_h_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2248 }
2249 block_cbp |= 0xF << (i << 2);
2250 } else if(val) {
2251 int left_cbp = 0, top_cbp = 0, filter = 0;
2252 if(apply_loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){
2253 filter = 1;
2254 if(i & 4){
2255 left_cbp = v->cbp[s->mb_x - 1] >> (i * 4);
2256 top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
2257 }else{
2258 left_cbp = (i & 1) ? (cbp >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4));
2259 top_cbp = (i & 2) ? (cbp >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));
2260 }
2261 if(left_cbp & 0xC)
2262 s->dsp.vc1_v_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2263 if(top_cbp & 0xA)
2264 s->dsp.vc1_h_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2265 }
2266 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize, (i&4) && (s->flags & CODEC_FLAG_GRAY), filter, left_cbp, top_cbp);
2267 block_cbp |= pat << (i << 2);
2268 if(!v->ttmbf && ttmb < 8) ttmb = -1;
2269 first_block = 0;
2270 }
2271 }
2272 }
2273 else //Skipped
2274 {
2275 s->mb_intra = 0;
2276 for(i = 0; i < 6; i++) {
2277 v->mb_type[0][s->block_index[i]] = 0;
2278 s->dc_val[0][s->block_index[i]] = 0;
2279 }
2280 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
2281 s->current_picture.qscale_table[mb_pos] = 0;
2282 vc1_pred_mv(s, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);
2283 vc1_mc_1mv(v, 0);
2284 return 0;
2285 }
2286 } //1MV mode
2287 else //4MV mode
2288 {
2289 if (!skipped /* unskipped MB */)
2290 {
2291 int intra_count = 0, coded_inter = 0;
2292 int is_intra[6], is_coded[6];
2293 /* Get CBPCY */
2294 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2295 for (i=0; i<6; i++)
2296 {
2297 val = ((cbp >> (5 - i)) & 1);
2298 s->dc_val[0][s->block_index[i]] = 0;
2299 s->mb_intra = 0;
2300 if(i < 4) {
2301 dmv_x = dmv_y = 0;
2302 s->mb_intra = 0;
2303 mb_has_coeffs = 0;
2304 if(val) {
2305 GET_MVDATA(dmv_x, dmv_y);
2306 }
2307 vc1_pred_mv(s, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);
2308 if(!s->mb_intra) vc1_mc_4mv_luma(v, i);
2309 intra_count += s->mb_intra;
2310 is_intra[i] = s->mb_intra;
2311 is_coded[i] = mb_has_coeffs;
2312 }
2313 if(i&4){
2314 is_intra[i] = (intra_count >= 3);
2315 is_coded[i] = val;
2316 }
2317 if(i == 4) vc1_mc_4mv_chroma(v);
2318 v->mb_type[0][s->block_index[i]] = is_intra[i];
2319 if(!coded_inter) coded_inter = !is_intra[i] & is_coded[i];
2320 }
2321 // if there are no coded blocks then don't do anything more
2322 if(!intra_count && !coded_inter) return 0;
2323 dst_idx = 0;
2324 GET_MQUANT();
2325 s->current_picture.qscale_table[mb_pos] = mquant;
2326 /* test if block is intra and has pred */
2327 {
2328 int intrapred = 0;
2329 for(i=0; i<6; i++)
2330 if(is_intra[i]) {
2331 if(((!s->first_slice_line || (i==2 || i==3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
2332 || ((s->mb_x || (i==1 || i==3)) && v->mb_type[0][s->block_index[i] - 1])) {
2333 intrapred = 1;
2334 break;
2335 }
2336 }
2337 if(intrapred)s->ac_pred = get_bits1(gb);
2338 else s->ac_pred = 0;
2339 }
2340 if (!v->ttmbf && coded_inter)
2341 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2342 for (i=0; i<6; i++)
2343 {
2344 dst_idx += i >> 2;
2345 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2346 s->mb_intra = is_intra[i];
2347 if (is_intra[i]) {
2348 /* check if prediction blocks A and C are available */
2349 v->a_avail = v->c_avail = 0;
2350 if(i == 2 || i == 3 || !s->first_slice_line)
2351 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2352 if(i == 1 || i == 3 || s->mb_x)
2353 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2354
2355 vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant, (i&4)?v->codingset2:v->codingset);
2356 if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
2357 s->dsp.vc1_inv_trans_8x8(s->block[i]);
2358 if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
2359 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
2360 if(v->pq >= 9 && v->overlap) {
2361 if(v->c_avail)
2362 s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
2363 if(v->a_avail)
2364 s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
2365 }
2366 if(v->s.loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){
2367 int left_cbp, top_cbp;
2368 if(i & 4){
2369 left_cbp = v->cbp[s->mb_x - 1] >> (i * 4);
2370 top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
2371 }else{
2372 left_cbp = (i & 1) ? (cbp >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4));
2373 top_cbp = (i & 2) ? (cbp >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));
2374 }
2375 if(left_cbp & 0xC)
2376 s->dsp.vc1_v_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2377 if(top_cbp & 0xA)
2378 s->dsp.vc1_h_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2379 }
2380 block_cbp |= 0xF << (i << 2);
2381 } else if(is_coded[i]) {
2382 int left_cbp = 0, top_cbp = 0, filter = 0;
2383 if(v->s.loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){
2384 filter = 1;
2385 if(i & 4){
2386 left_cbp = v->cbp[s->mb_x - 1] >> (i * 4);
2387 top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
2388 }else{
2389 left_cbp = (i & 1) ? (cbp >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4));
2390 top_cbp = (i & 2) ? (cbp >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));
2391 }
2392 if(left_cbp & 0xC)
2393 s->dsp.vc1_v_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2394 if(top_cbp & 0xA)
2395 s->dsp.vc1_h_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2396 }
2397 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize, (i&4) && (s->flags & CODEC_FLAG_GRAY), filter, left_cbp, top_cbp);
2398 block_cbp |= pat << (i << 2);
2399 if(!v->ttmbf && ttmb < 8) ttmb = -1;
2400 first_block = 0;
2401 }
2402 }
2403 return 0;
2404 }
2405 else //Skipped MB
2406 {
2407 s->mb_intra = 0;
2408 s->current_picture.qscale_table[mb_pos] = 0;
2409 for (i=0; i<6; i++) {
2410 v->mb_type[0][s->block_index[i]] = 0;
2411 s->dc_val[0][s->block_index[i]] = 0;
2412 }
2413 for (i=0; i<4; i++)
2414 {
2415 vc1_pred_mv(s, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0]);
2416 vc1_mc_4mv_luma(v, i);
2417 }
2418 vc1_mc_4mv_chroma(v);
2419 s->current_picture.qscale_table[mb_pos] = 0;
2420 return 0;
2421 }
2422 }
2423 v->cbp[s->mb_x] = block_cbp;
2424
2425 /* Should never happen */
2426 return -1;
2427 }
2428
2429 /** Decode one B-frame MB (in Main profile)
2430 */
2431 static void vc1_decode_b_mb(VC1Context *v)
2432 {
2433 MpegEncContext *s = &v->s;
2434 GetBitContext *gb = &s->gb;
2435 int i, j;
2436 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2437 int cbp = 0; /* cbp decoding stuff */
2438 int mqdiff, mquant; /* MB quantization */
2439 int ttmb = v->ttfrm; /* MB Transform type */
2440 int mb_has_coeffs = 0; /* last_flag */
2441 int index, index1; /* LUT indexes */
2442 int val, sign; /* temp values */
2443 int first_block = 1;
2444 int dst_idx, off;
2445 int skipped, direct;
2446 int dmv_x[2], dmv_y[2];
2447 int bmvtype = BMV_TYPE_BACKWARD;
2448
2449 mquant = v->pq; /* Loosy initialization */
2450 s->mb_intra = 0;
2451
2452 if (v->dmb_is_raw)
2453 direct = get_bits1(gb);
2454 else
2455 direct = v->direct_mb_plane[mb_pos];
2456 if (v->skip_is_raw)
2457 skipped = get_bits1(gb);
2458 else
2459 skipped = v->s.mbskip_table[mb_pos];
2460
2461 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
2462 for(i = 0; i < 6; i++) {
2463 v->mb_type[0][s->block_index[i]] = 0;
2464 s->dc_val[0][s->block_index[i]] = 0;
2465 }
2466 s->current_picture.qscale_table[mb_pos] = 0;
2467
2468 if (!direct) {
2469 if (!skipped) {
2470 GET_MVDATA(dmv_x[0], dmv_y[0]);
2471 dmv_x[1] = dmv_x[0];
2472 dmv_y[1] = dmv_y[0];
2473 }
2474 if(skipped || !s->mb_intra) {
2475 bmvtype = decode012(gb);
2476 switch(bmvtype) {
2477 case 0:
2478 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
2479 break;
2480 case 1:
2481 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
2482 break;
2483 case 2:
2484 bmvtype = BMV_TYPE_INTERPOLATED;
2485 dmv_x[0] = dmv_y[0] = 0;
2486 }
2487 }
2488 }
2489 for(i = 0; i < 6; i++)
2490 v->mb_type[0][s->block_index[i]] = s->mb_intra;
2491
2492 if (skipped) {
2493 if(direct) bmvtype = BMV_TYPE_INTERPOLATED;
2494 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2495 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2496 return;
2497 }
2498 if (direct) {
2499 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2500 GET_MQUANT();
2501 s->mb_intra = 0;
2502 s->current_picture.qscale_table[mb_pos] = mquant;
2503 if(!v->ttmbf)
2504 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2505 dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
2506 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2507 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2508 } else {
2509 if(!mb_has_coeffs && !s->mb_intra) {
2510 /* no coded blocks - effectively skipped */
2511 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2512 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2513 return;
2514 }
2515 if(s->mb_intra && !mb_has_coeffs) {
2516 GET_MQUANT();
2517 s->current_picture.qscale_table[mb_pos] = mquant;
2518 s->ac_pred = get_bits1(gb);
2519 cbp = 0;
2520 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2521 } else {
2522 if(bmvtype == BMV_TYPE_INTERPOLATED) {
2523 GET_MVDATA(dmv_x[0], dmv_y[0]);
2524 if(!mb_has_coeffs) {
2525 /* interpolated skipped block */
2526 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2527 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2528 return;
2529 }
2530 }
2531 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2532 if(!s->mb_intra) {
2533 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2534 }
2535 if(s->mb_intra)
2536 s->ac_pred = get_bits1(gb);
2537 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2538 GET_MQUANT();
2539 s->current_picture.qscale_table[mb_pos] = mquant;
2540 if(!v->ttmbf && !s->mb_intra && mb_has_coeffs)
2541 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2542 }
2543 }
2544 dst_idx = 0;
2545 for (i=0; i<6; i++)
2546 {
2547 s->dc_val[0][s->block_index[i]] = 0;
2548 dst_idx += i >> 2;
2549 val = ((cbp >> (5 - i)) & 1);
2550 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2551 v->mb_type[0][s->block_index[i]] = s->mb_intra;
2552 if(s->mb_intra) {
2553 /* check if prediction blocks A and C are available */
2554 v->a_avail = v->c_avail = 0;
2555 if(i == 2 || i == 3 || !s->first_slice_line)
2556 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2557 if(i == 1 || i == 3 || s->mb_x)
2558 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2559
2560 vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
2561 if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
2562 s->dsp.vc1_inv_trans_8x8(s->block[i]);
2563 if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
2564 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
2565 } else if(val) {
2566 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize, (i&4) && (s->flags & CODEC_FLAG_GRAY), 0, 0, 0);
2567 if(!v->ttmbf && ttmb < 8) ttmb = -1;
2568 first_block = 0;
2569 }
2570 }
2571 }
2572
2573 /** Decode blocks of I-frame
2574 */
2575 static void vc1_decode_i_blocks(VC1Context *v)
2576 {
2577 int k, j;
2578 MpegEncContext *s = &v->s;
2579 int cbp, val;
2580 uint8_t *coded_val;
2581 int mb_pos;
2582
2583 /* select codingmode used for VLC tables selection */
2584 switch(v->y_ac_table_index){
2585 case 0:
2586 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2587 break;
2588 case 1:
2589 v->codingset = CS_HIGH_MOT_INTRA;
2590 break;
2591 case 2:
2592 v->codingset = CS_MID_RATE_INTRA;
2593 break;
2594 }
2595
2596 switch(v->c_ac_table_index){
2597 case 0:
2598 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2599 break;
2600 case 1:
2601 v->codingset2 = CS_HIGH_MOT_INTER;
2602 break;
2603 case 2:
2604 v->codingset2 = CS_MID_RATE_INTER;
2605 break;
2606 }
2607
2608 /* Set DC scale - y and c use the same */
2609 s->y_dc_scale = s->y_dc_scale_table[v->pq];
2610 s->c_dc_scale = s->c_dc_scale_table[v->pq];
2611
2612 //do frame decode
2613 s->mb_x = s->mb_y = 0;
2614 s->mb_intra = 1;
2615 s->first_slice_line = 1;
2616 for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2617 s->mb_x = 0;
2618 ff_init_block_index(s);
2619 for(; s->mb_x < s->mb_width; s->mb_x++) {
2620 ff_update_block_index(s);
2621 s->dsp.clear_blocks(s->block[0]);
2622 mb_pos = s->mb_x + s->mb_y * s->mb_width;
2623 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2624 s->current_picture.qscale_table[mb_pos] = v->pq;
2625 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2626 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2627
2628 // do actual MB decoding and displaying
2629 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2630 v->s.ac_pred = get_bits1(&v->s.gb);
2631
2632 for(k = 0; k < 6; k++) {
2633 val = ((cbp >> (5 - k)) & 1);
2634
2635 if (k < 4) {
2636 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2637 val = val ^ pred;
2638 *coded_val = val;
2639 }
2640 cbp |= val << (5 - k);
2641
2642 vc1_decode_i_block(v, s->block[k], k, val, (k<4)? v->codingset : v->codingset2);
2643
2644 s->dsp.vc1_inv_trans_8x8(s->block[k]);
2645 if(v->pq >= 9 && v->overlap) {
2646 for(j = 0; j < 64; j++) s->block[k][j] += 128;
2647 }
2648 }
2649
2650 vc1_put_block(v, s->block);
2651 if(v->pq >= 9 && v->overlap) {
2652 if(s->mb_x) {
2653 s->dsp.vc1_h_overlap(s->dest[0], s->linesize);
2654 s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2655 if(!(s->flags & CODEC_FLAG_GRAY)) {
2656 s->dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
2657 s->dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
2658 }
2659 }
2660 s->dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
2661 s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2662 if(!s->first_slice_line) {
2663 s->dsp.vc1_v_overlap(s->dest[0], s->linesize);
2664 s->dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
2665 if(!(s->flags & CODEC_FLAG_GRAY)) {
2666 s->dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
2667 s->dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
2668 }
2669 }
2670 s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2671 s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2672 }
2673 if(v->s.loop_filter) vc1_loop_filter_iblk(s, v->pq);
2674
2675 if(get_bits_count(&s->gb) > v->bits) {
2676 ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
2677 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
2678 return;
2679 }
2680 }
2681 ff_draw_horiz_band(s, s->mb_y * 16, 16);
2682 s->first_slice_line = 0;
2683 }
2684 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2685 }
2686
2687 /** Decode blocks of I-frame for advanced profile
2688 */
2689 static void vc1_decode_i_blocks_adv(VC1Context *v)
2690 {
2691 int k, j;
2692 MpegEncContext *s = &v->s;
2693 int cbp, val;
2694 uint8_t *coded_val;