* Making [START|STOP]_TIMER work on architectures that support gethrtime()
[libav.git] / libavcodec / vc1.c
CommitLineData
21aa398f 1/*
10b9c374 2 * VC-1 and WMV3 decoder
c52ff688 3 * Copyright (c) 2006-2007 Konstantin Shishkov
be3492ec 4 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
21aa398f 5 *
b78e7197
DB
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
21aa398f
AB
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
b78e7197 11 * version 2.1 of the License, or (at your option) any later version.
21aa398f 12 *
b78e7197 13 * FFmpeg is distributed in the hope that it will be useful,
21aa398f
AB
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
b78e7197 19 * License along with FFmpeg; if not, write to the Free Software
5509bffa 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21aa398f
AB
21 */
22
23/**
10b9c374
KS
24 * @file vc1.c
25 * VC-1 and WMV3 decoder
21aa398f 26 *
21aa398f 27 */
21aa398f
AB
28#include "dsputil.h"
29#include "avcodec.h"
30#include "mpegvideo.h"
22c3029d 31#include "vc1.h"
10b9c374 32#include "vc1data.h"
be3492ec 33#include "vc1acdata.h"
5ecb0677 34#include "msmpeg4data.h"
0d33db8a 35
9da235c8
MN
36#undef NDEBUG
37#include <assert.h>
38
0d33db8a 39#define MB_INTRA_VLC_BITS 9
0d33db8a 40#define DC_VLC_BITS 9
be3492ec 41#define AC_VLC_BITS 9
0d33db8a 42static const uint16_t table_mb_intra[64][2];
21aa398f 43
21aa398f 44
be3492ec 45static inline int decode210(GetBitContext *gb){
3662aa76 46 if (get_bits1(gb))
be3492ec
KS
47 return 0;
48 else
49 return 2 - get_bits1(gb);
50}
51
2ce151f8 52/**
10b9c374
KS
53 * Init VC-1 specific tables and VC1Context members
54 * @param v The VC1Context to initialize
2ce151f8 55 * @return Status
56 */
10b9c374 57static int vc1_init_common(VC1Context *v)
21aa398f
AB
58{
59 static int done = 0;
bf2bc926 60 int i = 0;
21aa398f 61
21aa398f 62 v->hrd_rate = v->hrd_buffer = NULL;
e5540b3f 63
64 /* VLC tables */
21aa398f
AB
65 if(!done)
66 {
67 done = 1;
a5c14fca
KS
68 init_vlc(&ff_vc1_bfraction_vlc, VC1_BFRACTION_VLC_BITS, 23,
69 ff_vc1_bfraction_bits, 1, 1,
70 ff_vc1_bfraction_codes, 1, 1, 1);
71 init_vlc(&ff_vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4,
72 ff_vc1_norm2_bits, 1, 1,
73 ff_vc1_norm2_codes, 1, 1, 1);
74 init_vlc(&ff_vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 64,
75 ff_vc1_norm6_bits, 1, 1,
76 ff_vc1_norm6_codes, 2, 2, 1);
77 init_vlc(&ff_vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7,
78 ff_vc1_imode_bits, 1, 1,
79 ff_vc1_imode_codes, 1, 1, 1);
e5540b3f 80 for (i=0; i<3; i++)
81 {
a5c14fca
KS
82 init_vlc(&ff_vc1_ttmb_vlc[i], VC1_TTMB_VLC_BITS, 16,
83 ff_vc1_ttmb_bits[i], 1, 1,
84 ff_vc1_ttmb_codes[i], 2, 2, 1);
85 init_vlc(&ff_vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 8,
86 ff_vc1_ttblk_bits[i], 1, 1,
87 ff_vc1_ttblk_codes[i], 1, 1, 1);
88 init_vlc(&ff_vc1_subblkpat_vlc[i], VC1_SUBBLKPAT_VLC_BITS, 15,
89 ff_vc1_subblkpat_bits[i], 1, 1,
90 ff_vc1_subblkpat_codes[i], 1, 1, 1);
e5540b3f 91 }
92 for(i=0; i<4; i++)
21aa398f 93 {
a5c14fca
KS
94 init_vlc(&ff_vc1_4mv_block_pattern_vlc[i], VC1_4MV_BLOCK_PATTERN_VLC_BITS, 16,
95 ff_vc1_4mv_block_pattern_bits[i], 1, 1,
96 ff_vc1_4mv_block_pattern_codes[i], 1, 1, 1);
97 init_vlc(&ff_vc1_cbpcy_p_vlc[i], VC1_CBPCY_P_VLC_BITS, 64,
98 ff_vc1_cbpcy_p_bits[i], 1, 1,
99 ff_vc1_cbpcy_p_codes[i], 2, 2, 1);
100 init_vlc(&ff_vc1_mv_diff_vlc[i], VC1_MV_DIFF_VLC_BITS, 73,
101 ff_vc1_mv_diff_bits[i], 1, 1,
102 ff_vc1_mv_diff_codes[i], 2, 2, 1);
21aa398f 103 }
be3492ec 104 for(i=0; i<8; i++)
a5c14fca 105 init_vlc(&ff_vc1_ac_coeff_table[i], AC_VLC_BITS, vc1_ac_sizes[i],
be3492ec
KS
106 &vc1_ac_tables[i][0][1], 8, 4,
107 &vc1_ac_tables[i][0][0], 8, 4, 1);
108 init_vlc(&ff_msmp4_mb_i_vlc, MB_INTRA_VLC_BITS, 64,
109 &ff_msmp4_mb_i_table[0][1], 4, 2,
110 &ff_msmp4_mb_i_table[0][0], 4, 2, 1);
21aa398f
AB
111 }
112
e5540b3f 113 /* Other defaults */
114 v->pq = -1;
115 v->mvrange = 0; /* 7.1.1.18, p80 */
116
21aa398f
AB
117 return 0;
118}
119
be3492ec 120/***********************************************************************/
2ce151f8 121/**
be3492ec
KS
122 * @defgroup bitplane VC9 Bitplane decoding
123 * @see 8.7, p56
124 * @{
125 */
126
127/** @addtogroup bitplane
128 * Imode types
129 * @{
130 */
131enum Imode {
132 IMODE_RAW,
133 IMODE_NORM2,
134 IMODE_DIFF2,
135 IMODE_NORM6,
136 IMODE_DIFF6,
137 IMODE_ROWSKIP,
138 IMODE_COLSKIP
139};
140/** @} */ //imode defines
141
be3492ec
KS
142/** Decode rows by checking if they are skipped
143 * @param plane Buffer to store decoded bits
144 * @param[in] width Width of this buffer
145 * @param[in] height Height of this buffer
146 * @param[in] stride of this buffer
147 */
148static void decode_rowskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
149 int x, y;
21aa398f 150
be3492ec
KS
151 for (y=0; y<height; y++){
152 if (!get_bits(gb, 1)) //rowskip
153 memset(plane, 0, width);
154 else
155 for (x=0; x<width; x++)
156 plane[x] = get_bits(gb, 1);
157 plane += stride;
1cf9f514 158 }
be3492ec 159}
21aa398f 160
be3492ec
KS
161/** Decode columns by checking if they are skipped
162 * @param plane Buffer to store decoded bits
163 * @param[in] width Width of this buffer
164 * @param[in] height Height of this buffer
165 * @param[in] stride of this buffer
63e24954 166 * @todo FIXME: Optimize
be3492ec
KS
167 */
168static void decode_colskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
169 int x, y;
21aa398f 170
be3492ec
KS
171 for (x=0; x<width; x++){
172 if (!get_bits(gb, 1)) //colskip
173 for (y=0; y<height; y++)
174 plane[y*stride] = 0;
175 else
176 for (y=0; y<height; y++)
177 plane[y*stride] = get_bits(gb, 1);
178 plane ++;
21aa398f 179 }
21aa398f
AB
180}
181
be3492ec
KS
182/** Decode a bitplane's bits
183 * @param bp Bitplane where to store the decode bits
184 * @param v VC-1 context for bit reading and logging
2ce151f8 185 * @return Status
63e24954 186 * @todo FIXME: Optimize
2ce151f8 187 */
87dfe848 188static int bitplane_decoding(uint8_t* data, int *raw_flag, VC1Context *v)
21aa398f 189{
be3492ec 190 GetBitContext *gb = &v->s.gb;
21aa398f 191
be3492ec 192 int imode, x, y, code, offset;
87dfe848
KS
193 uint8_t invert, *planep = data;
194 int width, height, stride;
21aa398f 195
87dfe848
KS
196 width = v->s.mb_width;
197 height = v->s.mb_height;
198 stride = v->s.mb_stride;
be3492ec 199 invert = get_bits(gb, 1);
a5c14fca 200 imode = get_vlc2(gb, ff_vc1_imode_vlc.table, VC1_IMODE_VLC_BITS, 1);
21aa398f 201
87dfe848 202 *raw_flag = 0;
be3492ec 203 switch (imode)
21aa398f 204 {
be3492ec
KS
205 case IMODE_RAW:
206 //Data is actually read in the MB layer (same for all tests == "raw")
87dfe848 207 *raw_flag = 1; //invert ignored
be3492ec
KS
208 return invert;
209 case IMODE_DIFF2:
210 case IMODE_NORM2:
87dfe848 211 if ((height * width) & 1)
21aa398f 212 {
be3492ec
KS
213 *planep++ = get_bits(gb, 1);
214 offset = 1;
21aa398f 215 }
be3492ec
KS
216 else offset = 0;
217 // decode bitplane as one long line
87dfe848 218 for (y = offset; y < height * width; y += 2) {
a5c14fca 219 code = get_vlc2(gb, ff_vc1_norm2_vlc.table, VC1_NORM2_VLC_BITS, 1);
be3492ec
KS
220 *planep++ = code & 1;
221 offset++;
87dfe848 222 if(offset == width) {
be3492ec 223 offset = 0;
87dfe848 224 planep += stride - width;
21aa398f 225 }
be3492ec
KS
226 *planep++ = code >> 1;
227 offset++;
87dfe848 228 if(offset == width) {
be3492ec 229 offset = 0;
87dfe848 230 planep += stride - width;
21aa398f 231 }
be3492ec
KS
232 }
233 break;
234 case IMODE_DIFF6:
235 case IMODE_NORM6:
87dfe848
KS
236 if(!(height % 3) && (width % 3)) { // use 2x3 decoding
237 for(y = 0; y < height; y+= 3) {
238 for(x = width & 1; x < width; x += 2) {
a5c14fca 239 code = get_vlc2(gb, ff_vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
be3492ec
KS
240 if(code < 0){
241 av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
242 return -1;
243 }
244 planep[x + 0] = (code >> 0) & 1;
245 planep[x + 1] = (code >> 1) & 1;
87dfe848
KS
246 planep[x + 0 + stride] = (code >> 2) & 1;
247 planep[x + 1 + stride] = (code >> 3) & 1;
248 planep[x + 0 + stride * 2] = (code >> 4) & 1;
249 planep[x + 1 + stride * 2] = (code >> 5) & 1;
be3492ec 250 }
87dfe848 251 planep += stride * 3;
be3492ec 252 }
87dfe848 253 if(width & 1) decode_colskip(data, 1, height, stride, &v->s.gb);
be3492ec 254 } else { // 3x2
8a66a390 255 planep += (height & 1) * stride;
87dfe848
KS
256 for(y = height & 1; y < height; y += 2) {
257 for(x = width % 3; x < width; x += 3) {
a5c14fca 258 code = get_vlc2(gb, ff_vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
be3492ec
KS
259 if(code < 0){
260 av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
261 return -1;
262 }
263 planep[x + 0] = (code >> 0) & 1;
264 planep[x + 1] = (code >> 1) & 1;
265 planep[x + 2] = (code >> 2) & 1;
87dfe848
KS
266 planep[x + 0 + stride] = (code >> 3) & 1;
267 planep[x + 1 + stride] = (code >> 4) & 1;
268 planep[x + 2 + stride] = (code >> 5) & 1;
be3492ec 269 }
87dfe848 270 planep += stride * 2;
be3492ec 271 }
87dfe848
KS
272 x = width % 3;
273 if(x) decode_colskip(data , x, height , stride, &v->s.gb);
274 if(height & 1) decode_rowskip(data+x, width - x, 1, stride, &v->s.gb);
be3492ec
KS
275 }
276 break;
277 case IMODE_ROWSKIP:
87dfe848 278 decode_rowskip(data, width, height, stride, &v->s.gb);
be3492ec
KS
279 break;
280 case IMODE_COLSKIP:
87dfe848 281 decode_colskip(data, width, height, stride, &v->s.gb);
be3492ec
KS
282 break;
283 default: break;
284 }
285
286 /* Applying diff operator */
287 if (imode == IMODE_DIFF2 || imode == IMODE_DIFF6)
288 {
87dfe848 289 planep = data;
be3492ec 290 planep[0] ^= invert;
87dfe848 291 for (x=1; x<width; x++)
be3492ec 292 planep[x] ^= planep[x-1];
87dfe848 293 for (y=1; y<height; y++)
be3492ec 294 {
87dfe848
KS
295 planep += stride;
296 planep[0] ^= planep[-stride];
297 for (x=1; x<width; x++)
21aa398f 298 {
87dfe848
KS
299 if (planep[x-1] != planep[x-stride]) planep[x] ^= invert;
300 else planep[x] ^= planep[x-1];
21aa398f
AB
301 }
302 }
303 }
be3492ec 304 else if (invert)
21aa398f 305 {
87dfe848
KS
306 planep = data;
307 for (x=0; x<stride*height; x++) planep[x] = !planep[x]; //FIXME stride
21aa398f 308 }
be3492ec
KS
309 return (imode<<1) + invert;
310}
87dfe848 311
be3492ec 312/** @} */ //Bitplane group
21aa398f 313
be3492ec
KS
314/***********************************************************************/
315/** VOP Dquant decoding
316 * @param v VC-1 Context
317 */
318static int vop_dquant_decoding(VC1Context *v)
319{
320 GetBitContext *gb = &v->s.gb;
321 int pqdiff;
322
323 //variable size
324 if (v->dquant == 2)
325 {
326 pqdiff = get_bits(gb, 3);
327 if (pqdiff == 7) v->altpq = get_bits(gb, 5);
328 else v->altpq = v->pq + pqdiff + 1;
329 }
330 else
21aa398f 331 {
be3492ec
KS
332 v->dquantfrm = get_bits(gb, 1);
333 if ( v->dquantfrm )
21aa398f 334 {
be3492ec
KS
335 v->dqprofile = get_bits(gb, 2);
336 switch (v->dqprofile)
21aa398f 337 {
be3492ec
KS
338 case DQPROFILE_SINGLE_EDGE:
339 case DQPROFILE_DOUBLE_EDGES:
340 v->dqsbedge = get_bits(gb, 2);
341 break;
342 case DQPROFILE_ALL_MBS:
343 v->dqbilevel = get_bits(gb, 1);
344 default: break; //Forbidden ?
42cc17f9 345 }
3a3f1cf3 346 if (v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS)
21aa398f 347 {
be3492ec
KS
348 pqdiff = get_bits(gb, 3);
349 if (pqdiff == 7) v->altpq = get_bits(gb, 5);
350 else v->altpq = v->pq + pqdiff + 1;
21aa398f 351 }
21aa398f
AB
352 }
353 }
be3492ec
KS
354 return 0;
355}
21aa398f 356
be3492ec 357/** Put block onto picture
be3492ec
KS
358 */
359static void vc1_put_block(VC1Context *v, DCTELEM block[6][64])
360{
361 uint8_t *Y;
362 int ys, us, vs;
363 DSPContext *dsp = &v->s.dsp;
364
ffb9a8b1
KS
365 if(v->rangeredfrm) {
366 int i, j, k;
367 for(k = 0; k < 6; k++)
368 for(j = 0; j < 8; j++)
369 for(i = 0; i < 8; i++)
370 block[k][i + j*8] = ((block[k][i + j*8] - 128) << 1) + 128;
371
372 }
be3492ec
KS
373 ys = v->s.current_picture.linesize[0];
374 us = v->s.current_picture.linesize[1];
375 vs = v->s.current_picture.linesize[2];
376 Y = v->s.dest[0];
377
378 dsp->put_pixels_clamped(block[0], Y, ys);
379 dsp->put_pixels_clamped(block[1], Y + 8, ys);
380 Y += ys * 8;
381 dsp->put_pixels_clamped(block[2], Y, ys);
382 dsp->put_pixels_clamped(block[3], Y + 8, ys);
383
138712fe
KS
384 if(!(v->s.flags & CODEC_FLAG_GRAY)) {
385 dsp->put_pixels_clamped(block[4], v->s.dest[1], us);
386 dsp->put_pixels_clamped(block[5], v->s.dest[2], vs);
387 }
be3492ec
KS
388}
389
390/** Do motion compensation over 1 macroblock
391 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
392 */
5df68893 393static void vc1_mc_1mv(VC1Context *v, int dir)
be3492ec
KS
394{
395 MpegEncContext *s = &v->s;
396 DSPContext *dsp = &v->s.dsp;
397 uint8_t *srcY, *srcU, *srcV;
87dfe848 398 int dxy, uvdxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
be3492ec
KS
399
400 if(!v->s.last_picture.data[0])return;
401
1dc1ce64
KS
402 mx = s->mv[dir][0][0];
403 my = s->mv[dir][0][1];
404
405 // store motion vectors for further use in B frames
406 if(s->pict_type == P_TYPE) {
407 s->current_picture.motion_val[1][s->block_index[0]][0] = mx;
408 s->current_picture.motion_val[1][s->block_index[0]][1] = my;
409 }
87dfe848
KS
410 uvmx = (mx + ((mx & 3) == 3)) >> 1;
411 uvmy = (my + ((my & 3) == 3)) >> 1;
08baa3e0
KS
412 if(v->fastuvmc) {
413 uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
414 uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
415 }
5df68893
KS
416 if(!dir) {
417 srcY = s->last_picture.data[0];
418 srcU = s->last_picture.data[1];
419 srcV = s->last_picture.data[2];
420 } else {
421 srcY = s->next_picture.data[0];
422 srcU = s->next_picture.data[1];
423 srcV = s->next_picture.data[2];
424 }
be3492ec 425
87dfe848
KS
426 src_x = s->mb_x * 16 + (mx >> 2);
427 src_y = s->mb_y * 16 + (my >> 2);
428 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
429 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
430
7c971233
KS
431 if(v->profile != PROFILE_ADVANCED){
432 src_x = av_clip( src_x, -16, s->mb_width * 16);
433 src_y = av_clip( src_y, -16, s->mb_height * 16);
434 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
435 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
436 }else{
437 src_x = av_clip( src_x, -17, s->avctx->coded_width);
438 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
439 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
440 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
441 }
87dfe848
KS
442
443 srcY += src_y * s->linesize + src_x;
444 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
445 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
446
138712fe
KS
447 /* for grayscale we should not try to read from unknown area */
448 if(s->flags & CODEC_FLAG_GRAY) {
449 srcU = s->edge_emu_buffer + 18 * s->linesize;
450 srcV = s->edge_emu_buffer + 18 * s->linesize;
451 }
452
ffb9a8b1 453 if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
8295eb30
KS
454 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel*3
455 || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 16 - s->mspel*3){
456 uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
87dfe848 457
8295eb30
KS
458 srcY -= s->mspel * (1 + s->linesize);
459 ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2,
460 src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
87dfe848
KS
461 srcY = s->edge_emu_buffer;
462 ff_emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8+1, 8+1,
463 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
464 ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
465 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
466 srcU = uvbuf;
467 srcV = uvbuf + 16;
ffb9a8b1
KS
468 /* if we deal with range reduction we need to scale source blocks */
469 if(v->rangeredfrm) {
470 int i, j;
471 uint8_t *src, *src2;
472
473 src = srcY;
8295eb30
KS
474 for(j = 0; j < 17 + s->mspel*2; j++) {
475 for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
ffb9a8b1
KS
476 src += s->linesize;
477 }
478 src = srcU; src2 = srcV;
479 for(j = 0; j < 9; j++) {
480 for(i = 0; i < 9; i++) {
481 src[i] = ((src[i] - 128) >> 1) + 128;
482 src2[i] = ((src2[i] - 128) >> 1) + 128;
483 }
484 src += s->uvlinesize;
485 src2 += s->uvlinesize;
486 }
487 }
66d0ad26
KS
488 /* if we deal with intensity compensation we need to scale source blocks */
489 if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
490 int i, j;
491 uint8_t *src, *src2;
492
493 src = srcY;
8295eb30
KS
494 for(j = 0; j < 17 + s->mspel*2; j++) {
495 for(i = 0; i < 17 + s->mspel*2; i++) src[i] = v->luty[src[i]];
66d0ad26
KS
496 src += s->linesize;
497 }
498 src = srcU; src2 = srcV;
499 for(j = 0; j < 9; j++) {
500 for(i = 0; i < 9; i++) {
501 src[i] = v->lutuv[src[i]];
502 src2[i] = v->lutuv[src2[i]];
503 }
504 src += s->uvlinesize;
505 src2 += s->uvlinesize;
506 }
507 }
8295eb30 508 srcY += s->mspel * (1 + s->linesize);
87dfe848
KS
509 }
510
8295eb30
KS
511 if(s->mspel) {
512 dxy = ((my & 3) << 2) | (mx & 3);
513 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] , srcY , s->linesize, v->rnd);
514 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
515 srcY += s->linesize * 8;
516 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize , srcY , s->linesize, v->rnd);
517 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
66ff2c1f
KS
518 } else { // hpel mc - always used for luma
519 dxy = (my & 2) | ((mx & 2) >> 1);
87dfe848 520
c5b32ec1
KS
521 if(!v->rnd)
522 dsp->put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
523 else
524 dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
c5b32ec1 525 }
138712fe
KS
526
527 if(s->flags & CODEC_FLAG_GRAY) return;
2d5eadcc 528 /* Chroma MC always uses qpel bilinear */
c5b32ec1 529 uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
b0c8e1b8
KS
530 uvmx = (uvmx&3)<<1;
531 uvmy = (uvmy&3)<<1;
532 if(!v->rnd){
533 dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
534 dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
535 }else{
536 dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
537 dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
538 }
21aa398f 539}
21aa398f 540
e4bf0302
KS
541/** Do motion compensation for 4-MV macroblock - luminance block
542 */
543static void vc1_mc_4mv_luma(VC1Context *v, int n)
544{
545 MpegEncContext *s = &v->s;
546 DSPContext *dsp = &v->s.dsp;
547 uint8_t *srcY;
548 int dxy, mx, my, src_x, src_y;
549 int off;
550
551 if(!v->s.last_picture.data[0])return;
552 mx = s->mv[0][n][0];
553 my = s->mv[0][n][1];
554 srcY = s->last_picture.data[0];
555
556 off = s->linesize * 4 * (n&2) + (n&1) * 8;
557
558 src_x = s->mb_x * 16 + (n&1) * 8 + (mx >> 2);
559 src_y = s->mb_y * 16 + (n&2) * 4 + (my >> 2);
560
7c971233
KS
561 if(v->profile != PROFILE_ADVANCED){
562 src_x = av_clip( src_x, -16, s->mb_width * 16);
563 src_y = av_clip( src_y, -16, s->mb_height * 16);
564 }else{
565 src_x = av_clip( src_x, -17, s->avctx->coded_width);
566 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
567 }
e4bf0302
KS
568
569 srcY += src_y * s->linesize + src_x;
570
99f649a2 571 if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
78cbfc0c
KS
572 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 8 - s->mspel*2
573 || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 8 - s->mspel*2){
8295eb30
KS
574 srcY -= s->mspel * (1 + s->linesize);
575 ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 9+s->mspel*2, 9+s->mspel*2,
576 src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
e4bf0302 577 srcY = s->edge_emu_buffer;
ffb9a8b1
KS
578 /* if we deal with range reduction we need to scale source blocks */
579 if(v->rangeredfrm) {
580 int i, j;
581 uint8_t *src;
582
583 src = srcY;
8295eb30
KS
584 for(j = 0; j < 9 + s->mspel*2; j++) {
585 for(i = 0; i < 9 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
ffb9a8b1
KS
586 src += s->linesize;
587 }
588 }
99f649a2
KS
589 /* if we deal with intensity compensation we need to scale source blocks */
590 if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
591 int i, j;
592 uint8_t *src;
593
594 src = srcY;
595 for(j = 0; j < 9 + s->mspel*2; j++) {
596 for(i = 0; i < 9 + s->mspel*2; i++) src[i] = v->luty[src[i]];
597 src += s->linesize;
598 }
599 }
8295eb30 600 srcY += s->mspel * (1 + s->linesize);
e4bf0302
KS
601 }
602
8295eb30
KS
603 if(s->mspel) {
604 dxy = ((my & 3) << 2) | (mx & 3);
605 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, v->rnd);
66ff2c1f
KS
606 } else { // hpel mc - always used for luma
607 dxy = (my & 2) | ((mx & 2) >> 1);
c5b32ec1
KS
608 if(!v->rnd)
609 dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
610 else
611 dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
e4bf0302
KS
612 }
613}
614
e4bf0302
KS
615static inline int median4(int a, int b, int c, int d)
616{
35a9cac8 617 if(a < b) {
913e93d5
KS
618 if(c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
619 else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
35a9cac8 620 } else {
913e93d5
KS
621 if(c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
622 else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
35a9cac8 623 }
e4bf0302
KS
624}
625
626
627/** Do motion compensation for 4-MV macroblock - both chroma blocks
628 */
629static void vc1_mc_4mv_chroma(VC1Context *v)
630{
631 MpegEncContext *s = &v->s;
632 DSPContext *dsp = &v->s.dsp;
633 uint8_t *srcU, *srcV;
634 int uvdxy, uvmx, uvmy, uvsrc_x, uvsrc_y;
635 int i, idx, tx = 0, ty = 0;
636 int mvx[4], mvy[4], intra[4];
637 static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
638
639 if(!v->s.last_picture.data[0])return;
138712fe 640 if(s->flags & CODEC_FLAG_GRAY) return;
e4bf0302
KS
641
642 for(i = 0; i < 4; i++) {
643 mvx[i] = s->mv[0][i][0];
644 mvy[i] = s->mv[0][i][1];
645 intra[i] = v->mb_type[0][s->block_index[i]];
646 }
647
648 /* calculate chroma MV vector from four luma MVs */
1ae4a8e6 649 idx = (intra[3] << 3) | (intra[2] << 2) | (intra[1] << 1) | intra[0];
e4bf0302
KS
650 if(!idx) { // all blocks are inter
651 tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
652 ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
653 } else if(count[idx] == 1) { // 3 inter blocks
654 switch(idx) {
655 case 0x1:
656 tx = mid_pred(mvx[1], mvx[2], mvx[3]);
1ae4a8e6 657 ty = mid_pred(mvy[1], mvy[2], mvy[3]);
e4bf0302
KS
658 break;
659 case 0x2:
660 tx = mid_pred(mvx[0], mvx[2], mvx[3]);
1ae4a8e6 661 ty = mid_pred(mvy[0], mvy[2], mvy[3]);
e4bf0302
KS
662 break;
663 case 0x4:
664 tx = mid_pred(mvx[0], mvx[1], mvx[3]);
1ae4a8e6 665 ty = mid_pred(mvy[0], mvy[1], mvy[3]);
e4bf0302
KS
666 break;
667 case 0x8:
668 tx = mid_pred(mvx[0], mvx[1], mvx[2]);
1ae4a8e6 669 ty = mid_pred(mvy[0], mvy[1], mvy[2]);
e4bf0302
KS
670 break;
671 }
672 } else if(count[idx] == 2) {
673 int t1 = 0, t2 = 0;
674 for(i=0; i<3;i++) if(!intra[i]) {t1 = i; break;}
675 for(i= t1+1; i<4; i++)if(!intra[i]) {t2 = i; break;}
913e93d5
KS
676 tx = (mvx[t1] + mvx[t2]) / 2;
677 ty = (mvy[t1] + mvy[t2]) / 2;
9a44385e
KS
678 } else {
679 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
680 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
e4bf0302 681 return; //no need to do MC for inter blocks
9a44385e 682 }
e4bf0302 683
1dc1ce64
KS
684 s->current_picture.motion_val[1][s->block_index[0]][0] = tx;
685 s->current_picture.motion_val[1][s->block_index[0]][1] = ty;
e4bf0302
KS
686 uvmx = (tx + ((tx&3) == 3)) >> 1;
687 uvmy = (ty + ((ty&3) == 3)) >> 1;
08baa3e0
KS
688 if(v->fastuvmc) {
689 uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
690 uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
691 }
e4bf0302
KS
692
693 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
694 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
695
7c971233
KS
696 if(v->profile != PROFILE_ADVANCED){
697 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
698 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
699 }else{
700 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
701 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
702 }
ab475795 703
e4bf0302
KS
704 srcU = s->last_picture.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
705 srcV = s->last_picture.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
99f649a2
KS
706 if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
707 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
83b3df83 708 || (unsigned)uvsrc_y > (s->v_edge_pos >> 1) - 9){
e4bf0302
KS
709 ff_emulated_edge_mc(s->edge_emu_buffer , srcU, s->uvlinesize, 8+1, 8+1,
710 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
711 ff_emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize, 8+1, 8+1,
712 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
713 srcU = s->edge_emu_buffer;
714 srcV = s->edge_emu_buffer + 16;
ffb9a8b1
KS
715
716 /* if we deal with range reduction we need to scale source blocks */
717 if(v->rangeredfrm) {
718 int i, j;
719 uint8_t *src, *src2;
720
721 src = srcU; src2 = srcV;
722 for(j = 0; j < 9; j++) {
723 for(i = 0; i < 9; i++) {
724 src[i] = ((src[i] - 128) >> 1) + 128;
725 src2[i] = ((src2[i] - 128) >> 1) + 128;
726 }
727 src += s->uvlinesize;
728 src2 += s->uvlinesize;
729 }
730 }
99f649a2
KS
731 /* if we deal with intensity compensation we need to scale source blocks */
732 if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
733 int i, j;
734 uint8_t *src, *src2;
735
736 src = srcU; src2 = srcV;
737 for(j = 0; j < 9; j++) {
738 for(i = 0; i < 9; i++) {
739 src[i] = v->lutuv[src[i]];
740 src2[i] = v->lutuv[src2[i]];
741 }
742 src += s->uvlinesize;
743 src2 += s->uvlinesize;
744 }
745 }
e4bf0302
KS
746 }
747
2d5eadcc 748 /* Chroma MC always uses qpel bilinear */
c5b32ec1 749 uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
b0c8e1b8
KS
750 uvmx = (uvmx&3)<<1;
751 uvmy = (uvmy&3)<<1;
752 if(!v->rnd){
753 dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
754 dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
755 }else{
756 dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
757 dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
758 }
e4bf0302
KS
759}
760
3c275f6d
KS
761static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb);
762
115329f1 763/**
2ce151f8 764 * Decode Simple/Main Profiles sequence header
765 * @see Figure 7-8, p16-17
766 * @param avctx Codec context
767 * @param gb GetBit context initialized from Codec context extra_data
768 * @return Status
769 */
21aa398f
AB
770static int decode_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
771{
10b9c374 772 VC1Context *v = avctx->priv_data;
21aa398f 773
d429c982 774 av_log(avctx, AV_LOG_DEBUG, "Header: %0X\n", show_bits(gb, 32));
21aa398f 775 v->profile = get_bits(gb, 2);
9d1f80f2 776 if (v->profile == PROFILE_COMPLEX)
25a0a0a5 777 {
9d1f80f2 778 av_log(avctx, AV_LOG_ERROR, "WMV3 Complex Profile is not fully supported\n");
25a0a0a5 779 }
21aa398f 780
7cc84d24 781 if (v->profile == PROFILE_ADVANCED)
21aa398f 782 {
3c275f6d 783 return decode_sequence_header_adv(v, gb);
21aa398f
AB
784 }
785 else
21aa398f
AB
786 {
787 v->res_sm = get_bits(gb, 2); //reserved
788 if (v->res_sm)
789 {
790 av_log(avctx, AV_LOG_ERROR,
791 "Reserved RES_SM=%i is forbidden\n", v->res_sm);
7cc84d24 792 return -1;
21aa398f
AB
793 }
794 }
795
796 // (fps-2)/4 (->30)
797 v->frmrtq_postproc = get_bits(gb, 3); //common
798 // (bitrate-32kbps)/64kbps
799 v->bitrtq_postproc = get_bits(gb, 5); //common
5e424311 800 v->s.loop_filter = get_bits(gb, 1); //common
25a0a0a5
IK
801 if(v->s.loop_filter == 1 && v->profile == PROFILE_SIMPLE)
802 {
803 av_log(avctx, AV_LOG_ERROR,
804 "LOOPFILTER shell not be enabled in simple profile\n");
805 }
21aa398f 806
6cf6d0ec
KS
807 v->res_x8 = get_bits(gb, 1); //reserved
808 if (v->res_x8)
21aa398f 809 {
6cf6d0ec
KS
810 av_log(avctx, AV_LOG_ERROR,
811 "1 for reserved RES_X8 is forbidden\n");
812 //return -1;
813 }
814 v->multires = get_bits(gb, 1);
815 v->res_fasttx = get_bits(gb, 1);
816 if (!v->res_fasttx)
817 {
818 av_log(avctx, AV_LOG_ERROR,
819 "0 for reserved RES_FASTTX is forbidden\n");
820 //return -1;
21aa398f
AB
821 }
822
823 v->fastuvmc = get_bits(gb, 1); //common
824 if (!v->profile && !v->fastuvmc)
825 {
826 av_log(avctx, AV_LOG_ERROR,
827 "FASTUVMC unavailable in Simple Profile\n");
828 return -1;
829 }
830 v->extended_mv = get_bits(gb, 1); //common
831 if (!v->profile && v->extended_mv)
832 {
833 av_log(avctx, AV_LOG_ERROR,
834 "Extended MVs unavailable in Simple Profile\n");
835 return -1;
836 }
837 v->dquant = get_bits(gb, 2); //common
838 v->vstransform = get_bits(gb, 1); //common
42cc17f9 839
6cf6d0ec
KS
840 v->res_transtab = get_bits(gb, 1);
841 if (v->res_transtab)
21aa398f 842 {
6cf6d0ec
KS
843 av_log(avctx, AV_LOG_ERROR,
844 "1 for reserved RES_TRANSTAB is forbidden\n");
845 return -1;
21aa398f
AB
846 }
847
848 v->overlap = get_bits(gb, 1); //common
849
6cf6d0ec
KS
850 v->s.resync_marker = get_bits(gb, 1);
851 v->rangered = get_bits(gb, 1);
852 if (v->rangered && v->profile == PROFILE_SIMPLE)
21aa398f 853 {
6cf6d0ec
KS
854 av_log(avctx, AV_LOG_INFO,
855 "RANGERED should be set to 0 in simple profile\n");
21aa398f
AB
856 }
857
0d33db8a 858 v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
21aa398f
AB
859 v->quantizer_mode = get_bits(gb, 2); //common
860
6cf6d0ec
KS
861 v->finterpflag = get_bits(gb, 1); //common
862 v->res_rtm_flag = get_bits(gb, 1); //reserved
863 if (!v->res_rtm_flag)
21aa398f 864 {
48d3fca7
KS
865// av_log(avctx, AV_LOG_ERROR,
866// "0 for reserved RES_RTM_FLAG is forbidden\n");
6cf6d0ec
KS
867 av_log(avctx, AV_LOG_ERROR,
868 "Old WMV3 version detected, only I-frames will be decoded\n");
869 //return -1;
870 }
351f6b4e
KS
871 //TODO: figure out what they mean (always 0x402F)
872 if(!v->res_fasttx) skip_bits(gb, 16);
6cf6d0ec 873 av_log(avctx, AV_LOG_DEBUG,
21aa398f 874 "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
e148c6e1 875 "LoopFilter=%i, MultiRes=%i, FastUVMC=%i, Extended MV=%i\n"
21aa398f
AB
876 "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
877 "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
878 v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
2ce151f8 879 v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
0d33db8a 880 v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
21aa398f
AB
881 v->dquant, v->quantizer_mode, avctx->max_b_frames
882 );
6cf6d0ec 883 return 0;
21aa398f
AB
884}
885
3c275f6d
KS
886static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb)
887{
888 v->res_rtm_flag = 1;
889 v->level = get_bits(gb, 3);
890 if(v->level >= 5)
891 {
892 av_log(v->s.avctx, AV_LOG_ERROR, "Reserved LEVEL %i\n",v->level);
893 }
894 v->chromaformat = get_bits(gb, 2);
895 if (v->chromaformat != 1)
896 {
897 av_log(v->s.avctx, AV_LOG_ERROR,
898 "Only 4:2:0 chroma format supported\n");
899 return -1;
900 }
901
902 // (fps-2)/4 (->30)
903 v->frmrtq_postproc = get_bits(gb, 3); //common
904 // (bitrate-32kbps)/64kbps
905 v->bitrtq_postproc = get_bits(gb, 5); //common
906 v->postprocflag = get_bits(gb, 1); //common
907
908 v->s.avctx->coded_width = (get_bits(gb, 12) + 1) << 1;
909 v->s.avctx->coded_height = (get_bits(gb, 12) + 1) << 1;
4305a4ef
KS
910 v->s.avctx->width = v->s.avctx->coded_width;
911 v->s.avctx->height = v->s.avctx->coded_height;
3c275f6d
KS
912 v->broadcast = get_bits1(gb);
913 v->interlace = get_bits1(gb);
914 v->tfcntrflag = get_bits1(gb);
915 v->finterpflag = get_bits1(gb);
916 get_bits1(gb); // reserved
8ea780d7 917
ab475795
KS
918 v->s.h_edge_pos = v->s.avctx->coded_width;
919 v->s.v_edge_pos = v->s.avctx->coded_height;
920
8ea780d7
KS
921 av_log(v->s.avctx, AV_LOG_DEBUG,
922 "Advanced Profile level %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
923 "LoopFilter=%i, ChromaFormat=%i, Pulldown=%i, Interlace: %i\n"
924 "TFCTRflag=%i, FINTERPflag=%i\n",
925 v->level, v->frmrtq_postproc, v->bitrtq_postproc,
926 v->s.loop_filter, v->chromaformat, v->broadcast, v->interlace,
927 v->tfcntrflag, v->finterpflag
928 );
929
3c275f6d
KS
930 v->psf = get_bits1(gb);
931 if(v->psf) { //PsF, 6.1.13
932 av_log(v->s.avctx, AV_LOG_ERROR, "Progressive Segmented Frame mode: not supported (yet)\n");
933 return -1;
934 }
6eda6e37 935 v->s.max_b_frames = v->s.avctx->max_b_frames = 7;
3c275f6d
KS
936 if(get_bits1(gb)) { //Display Info - decoding is not affected by it
937 int w, h, ar = 0;
f0a85d5f 938 av_log(v->s.avctx, AV_LOG_DEBUG, "Display extended info:\n");
e7cf38e5
KS
939 v->s.avctx->width = v->s.width = w = get_bits(gb, 14) + 1;
940 v->s.avctx->height = v->s.height = h = get_bits(gb, 14) + 1;
f0a85d5f 941 av_log(v->s.avctx, AV_LOG_DEBUG, "Display dimensions: %ix%i\n", w, h);
3c275f6d
KS
942 if(get_bits1(gb))
943 ar = get_bits(gb, 4);
74fd63d0 944 if(ar && ar < 14){
a5c14fca 945 v->s.avctx->sample_aspect_ratio = ff_vc1_pixel_aspect[ar];
74fd63d0 946 }else if(ar == 15){
3c275f6d
KS
947 w = get_bits(gb, 8);
948 h = get_bits(gb, 8);
74fd63d0 949 v->s.avctx->sample_aspect_ratio = (AVRational){w, h};
3c275f6d
KS
950 }
951
952 if(get_bits1(gb)){ //framerate stuff
953 if(get_bits1(gb)) {
f0c31621
KS
954 v->s.avctx->time_base.num = 32;
955 v->s.avctx->time_base.den = get_bits(gb, 16) + 1;
3c275f6d 956 } else {
f0c31621
KS
957 int nr, dr;
958 nr = get_bits(gb, 8);
959 dr = get_bits(gb, 4);
960 if(nr && nr < 8 && dr && dr < 3){
a5c14fca
KS
961 v->s.avctx->time_base.num = ff_vc1_fps_dr[dr - 1];
962 v->s.avctx->time_base.den = ff_vc1_fps_nr[nr - 1] * 1000;
f0c31621 963 }
3c275f6d
KS
964 }
965 }
966
967 if(get_bits1(gb)){
968 v->color_prim = get_bits(gb, 8);
969 v->transfer_char = get_bits(gb, 8);
970 v->matrix_coef = get_bits(gb, 8);
971 }
972 }
973
974 v->hrd_param_flag = get_bits1(gb);
975 if(v->hrd_param_flag) {
976 int i;
977 v->hrd_num_leaky_buckets = get_bits(gb, 5);
978 get_bits(gb, 4); //bitrate exponent
979 get_bits(gb, 4); //buffer size exponent
980 for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
981 get_bits(gb, 16); //hrd_rate[n]
982 get_bits(gb, 16); //hrd_buffer[n]
983 }
984 }
985 return 0;
986}
987
988static int decode_entry_point(AVCodecContext *avctx, GetBitContext *gb)
989{
990 VC1Context *v = avctx->priv_data;
6eda6e37 991 int i, blink, clentry, refdist;
3c275f6d
KS
992
993 av_log(avctx, AV_LOG_DEBUG, "Entry point: %08X\n", show_bits_long(gb, 32));
8ea780d7 994 blink = get_bits1(gb); // broken link
6eda6e37 995 clentry = get_bits1(gb); // closed entry
3c275f6d 996 v->panscanflag = get_bits1(gb);
8ea780d7 997 refdist = get_bits1(gb); // refdist flag
3c275f6d
KS
998 v->s.loop_filter = get_bits1(gb);
999 v->fastuvmc = get_bits1(gb);
1000 v->extended_mv = get_bits1(gb);
1001 v->dquant = get_bits(gb, 2);
1002 v->vstransform = get_bits1(gb);
1003 v->overlap = get_bits1(gb);
1004 v->quantizer_mode = get_bits(gb, 2);
1005
1006 if(v->hrd_param_flag){
1007 for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
1008 get_bits(gb, 8); //hrd_full[n]
1009 }
1010 }
1011
1012 if(get_bits1(gb)){
1013 avctx->coded_width = (get_bits(gb, 12)+1)<<1;
1014 avctx->coded_height = (get_bits(gb, 12)+1)<<1;
1015 }
1016 if(v->extended_mv)
1017 v->extended_dmv = get_bits1(gb);
1018 if(get_bits1(gb)) {
1019 av_log(avctx, AV_LOG_ERROR, "Luma scaling is not supported, expect wrong picture\n");
1020 skip_bits(gb, 3); // Y range, ignored for now
1021 }
1022 if(get_bits1(gb)) {
1023 av_log(avctx, AV_LOG_ERROR, "Chroma scaling is not supported, expect wrong picture\n");
1024 skip_bits(gb, 3); // UV range, ignored for now
1025 }
1026
8ea780d7
KS
1027 av_log(avctx, AV_LOG_DEBUG, "Entry point info:\n"
1028 "BrokenLink=%i, ClosedEntry=%i, PanscanFlag=%i\n"
1029 "RefDist=%i, Postproc=%i, FastUVMC=%i, ExtMV=%i\n"
1030 "DQuant=%i, VSTransform=%i, Overlap=%i, Qmode=%i\n",
6eda6e37 1031 blink, clentry, v->panscanflag, refdist, v->s.loop_filter,
8ea780d7
KS
1032 v->fastuvmc, v->extended_mv, v->dquant, v->vstransform, v->overlap, v->quantizer_mode);
1033
3c275f6d
KS
1034 return 0;
1035}
21aa398f 1036
be3492ec 1037static int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
21aa398f 1038{
be3492ec
KS
1039 int pqindex, lowquant, status;
1040
1041 if(v->finterpflag) v->interpfrm = get_bits(gb, 1);
1042 skip_bits(gb, 2); //framecnt unused
1043 v->rangeredfrm = 0;
1044 if (v->rangered) v->rangeredfrm = get_bits(gb, 1);
1045 v->s.pict_type = get_bits(gb, 1);
1046 if (v->s.avctx->max_b_frames) {
1047 if (!v->s.pict_type) {
1048 if (get_bits(gb, 1)) v->s.pict_type = I_TYPE;
1049 else v->s.pict_type = B_TYPE;
1050 } else v->s.pict_type = P_TYPE;
1051 } else v->s.pict_type = v->s.pict_type ? P_TYPE : I_TYPE;
1052
1dc1ce64 1053 v->bi_type = 0;
5df68893 1054 if(v->s.pict_type == B_TYPE) {
a5c14fca
KS
1055 v->bfraction = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
1056 v->bfraction = ff_vc1_bfraction_lut[v->bfraction];
1dc1ce64 1057 if(v->bfraction == 0) {
5df68893
KS
1058 v->s.pict_type = BI_TYPE;
1059 }
1060 }
1dc1ce64
KS
1061 if(v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
1062 get_bits(gb, 7); // skip buffer fullness
be3492ec 1063
c5b32ec1 1064 /* calculate RND */
1dc1ce64 1065 if(v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
c5b32ec1
KS
1066 v->rnd = 1;
1067 if(v->s.pict_type == P_TYPE)
1068 v->rnd ^= 1;
1069
be3492ec
KS
1070 /* Quantizer stuff */
1071 pqindex = get_bits(gb, 5);
1072 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
a5c14fca 1073 v->pq = ff_vc1_pquant_table[0][pqindex];
be3492ec 1074 else
a5c14fca 1075 v->pq = ff_vc1_pquant_table[1][pqindex];
be3492ec 1076
0a45801f 1077 v->pquantizer = 1;
be3492ec
KS
1078 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1079 v->pquantizer = pqindex < 9;
0a45801f
KS
1080 if (v->quantizer_mode == QUANT_NON_UNIFORM)
1081 v->pquantizer = 0;
be3492ec
KS
1082 v->pqindex = pqindex;
1083 if (pqindex < 9) v->halfpq = get_bits(gb, 1);
1084 else v->halfpq = 0;
1085 if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
1086 v->pquantizer = get_bits(gb, 1);
1087 v->dquantfrm = 0;
3c2ddb59 1088 if (v->extended_mv == 1) v->mvrange = get_unary(gb, 0, 3);
5c4b8efd
KS
1089 v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
1090 v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
1091 v->range_x = 1 << (v->k_x - 1);
1092 v->range_y = 1 << (v->k_y - 1);
1093 if (v->profile == PROFILE_ADVANCED)
1094 {
1095 if (v->postprocflag) v->postproc = get_bits(gb, 1);
1096 }
1097 else
1098 if (v->multires && v->s.pict_type != B_TYPE) v->respic = get_bits(gb, 2);
be3492ec 1099
9d1f80f2
KS
1100 if(v->res_x8 && (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)){
1101 if(get_bits1(gb))return -1;
1102 }
be3492ec
KS
1103//av_log(v->s.avctx, AV_LOG_INFO, "%c Frame: QP=[%i]%i (+%i/2) %i\n",
1104// (v->s.pict_type == P_TYPE) ? 'P' : ((v->s.pict_type == I_TYPE) ? 'I' : 'B'), pqindex, v->pq, v->halfpq, v->rangeredfrm);
1105
6b33eed8
KS
1106 if(v->s.pict_type == I_TYPE || v->s.pict_type == P_TYPE) v->use_ic = 0;
1107
be3492ec
KS
1108 switch(v->s.pict_type) {
1109 case P_TYPE:
1110 if (v->pq < 5) v->tt_index = 0;
1111 else if(v->pq < 13) v->tt_index = 1;
1112 else v->tt_index = 2;
1113
be3492ec 1114 lowquant = (v->pq > 12) ? 0 : 1;
3c2ddb59 1115 v->mv_mode = ff_vc1_mv_pmode_table[lowquant][get_unary(gb, 1, 4)];
be3492ec
KS
1116 if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
1117 {
66d0ad26 1118 int scale, shift, i;
3c2ddb59 1119 v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
be3492ec
KS
1120 v->lumscale = get_bits(gb, 6);
1121 v->lumshift = get_bits(gb, 6);
6b33eed8 1122 v->use_ic = 1;
66d0ad26
KS
1123 /* fill lookup tables for intensity compensation */
1124 if(!v->lumscale) {
1125 scale = -64;
1126 shift = (255 - v->lumshift * 2) << 6;
1127 if(v->lumshift > 31)
1128 shift += 128 << 6;
1129 } else {
1130 scale = v->lumscale + 32;
1131 if(v->lumshift > 31)
1132 shift = (v->lumshift - 64) << 6;
1133 else
1134 shift = v->lumshift << 6;
1135 }
1136 for(i = 0; i < 256; i++) {
f66e4f5f
RD
1137 v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
1138 v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
66d0ad26 1139 }
be3492ec
KS
1140 }
1141 if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
87dfe848 1142 v->s.quarter_sample = 0;
05103ed3
KS
1143 else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1144 if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
1145 v->s.quarter_sample = 0;
1146 else
1147 v->s.quarter_sample = 1;
1148 } else
87dfe848 1149 v->s.quarter_sample = 1;
09be55df 1150 v->s.mspel = !(v->mv_mode == MV_PMODE_1MV_HPEL_BILIN || (v->mv_mode == MV_PMODE_INTENSITY_COMP && v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN));
be3492ec 1151
be3492ec
KS
1152 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
1153 v->mv_mode2 == MV_PMODE_MIXED_MV)
1154 || v->mv_mode == MV_PMODE_MIXED_MV)
1155 {
87dfe848 1156 status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
be3492ec
KS
1157 if (status < 0) return -1;
1158 av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
1159 "Imode: %i, Invert: %i\n", status>>1, status&1);
87dfe848
KS
1160 } else {
1161 v->mv_type_is_raw = 0;
1162 memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
be3492ec 1163 }
0f7344aa 1164 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
be3492ec
KS
1165 if (status < 0) return -1;
1166 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1167 "Imode: %i, Invert: %i\n", status>>1, status&1);
1168
1169 /* Hopefully this is correct for P frames */
a5c14fca
KS
1170 v->s.mv_table_index = get_bits(gb, 2); //but using ff_vc1_ tables
1171 v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
be3492ec
KS
1172
1173 if (v->dquant)
1174 {
1175 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1176 vop_dquant_decoding(v);
1177 }
1178
1179 v->ttfrm = 0; //FIXME Is that so ?
1180 if (v->vstransform)
1181 {
1182 v->ttmbf = get_bits(gb, 1);
1183 if (v->ttmbf)
1184 {
a5c14fca 1185 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
be3492ec 1186 }
b53921aa
KS
1187 } else {
1188 v->ttmbf = 1;
1189 v->ttfrm = TT_8X8;
be3492ec
KS
1190 }
1191 break;
1192 case B_TYPE:
5df68893
KS
1193 if (v->pq < 5) v->tt_index = 0;
1194 else if(v->pq < 13) v->tt_index = 1;
1195 else v->tt_index = 2;
1196
1197 lowquant = (v->pq > 12) ? 0 : 1;
1198 v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
1199 v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
09be55df 1200 v->s.mspel = v->s.quarter_sample;
5df68893
KS
1201
1202 status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
1203 if (status < 0) return -1;
1204 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
1205 "Imode: %i, Invert: %i\n", status>>1, status&1);
1206 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1207 if (status < 0) return -1;
1208 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1209 "Imode: %i, Invert: %i\n", status>>1, status&1);
1210
1211 v->s.mv_table_index = get_bits(gb, 2);
a5c14fca 1212 v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
5df68893
KS
1213
1214 if (v->dquant)
1215 {
1216 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1217 vop_dquant_decoding(v);
1218 }
1219
1220 v->ttfrm = 0;
1221 if (v->vstransform)
1222 {
1223 v->ttmbf = get_bits(gb, 1);
1224 if (v->ttmbf)
1225 {
a5c14fca 1226 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
5df68893
KS
1227 }
1228 } else {
1229 v->ttmbf = 1;
1230 v->ttfrm = TT_8X8;
1231 }
be3492ec 1232 break;
21aa398f 1233 }
be3492ec
KS
1234
1235 /* AC Syntax */
1236 v->c_ac_table_index = decode012(gb);
1237 if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
21aa398f 1238 {
be3492ec 1239 v->y_ac_table_index = decode012(gb);
21aa398f 1240 }
be3492ec
KS
1241 /* DC Syntax */
1242 v->s.dc_table_index = get_bits(gb, 1);
1243
1dc1ce64
KS
1244 if(v->s.pict_type == BI_TYPE) {
1245 v->s.pict_type = B_TYPE;
1246 v->bi_type = 1;
1247 }
21aa398f
AB
1248 return 0;
1249}
21aa398f 1250
3c275f6d
KS
1251static int vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb)
1252{
3c275f6d
KS
1253 int pqindex, lowquant;
1254 int status;
1255
1256 v->p_frame_skipped = 0;
1257
ac91185f 1258 if(v->interlace){
93731ff2 1259 v->fcm = decode012(gb);
ac91185f
KS
1260 if(v->fcm) return -1; // interlaced frames/fields are not implemented
1261 }
3c2ddb59 1262 switch(get_unary(gb, 0, 4)) {
3c275f6d
KS
1263 case 0:
1264 v->s.pict_type = P_TYPE;
1265 break;
1266 case 1:
1267 v->s.pict_type = B_TYPE;
5081f3aa 1268 break;
3c275f6d
KS
1269 case 2:
1270 v->s.pict_type = I_TYPE;
1271 break;
1272 case 3:
1273 v->s.pict_type = BI_TYPE;
76751653 1274 break;
3c275f6d
KS
1275 case 4:
1276 v->s.pict_type = P_TYPE; // skipped pic
1277 v->p_frame_skipped = 1;
1278 return 0;
1279 }
1280 if(v->tfcntrflag)
1281 get_bits(gb, 8);
1282 if(v->broadcast) {
02c823d4 1283 if(!v->interlace || v->psf) {
93731ff2 1284 v->rptfrm = get_bits(gb, 2);
3c275f6d 1285 } else {
93731ff2
KS
1286 v->tff = get_bits1(gb);
1287 v->rptfrm = get_bits1(gb);
3c275f6d
KS
1288 }
1289 }
1290 if(v->panscanflag) {
1291 //...
1292 }
1293 v->rnd = get_bits1(gb);
1294 if(v->interlace)
1295 v->uvsamp = get_bits1(gb);
1296 if(v->finterpflag) v->interpfrm = get_bits(gb, 1);
5081f3aa 1297 if(v->s.pict_type == B_TYPE) {
a5c14fca
KS
1298 v->bfraction = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
1299 v->bfraction = ff_vc1_bfraction_lut[v->bfraction];
5081f3aa
KS
1300 if(v->bfraction == 0) {
1301 v->s.pict_type = BI_TYPE; /* XXX: should not happen here */
1302 }
1303 }
3c275f6d
KS
1304 pqindex = get_bits(gb, 5);
1305 v->pqindex = pqindex;
1306 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
a5c14fca 1307 v->pq = ff_vc1_pquant_table[0][pqindex];
3c275f6d 1308 else
a5c14fca 1309 v->pq = ff_vc1_pquant_table[1][pqindex];
3c275f6d
KS
1310
1311 v->pquantizer = 1;
1312 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1313 v->pquantizer = pqindex < 9;
1314 if (v->quantizer_mode == QUANT_NON_UNIFORM)
1315 v->pquantizer = 0;
1316 v->pqindex = pqindex;
1317 if (pqindex < 9) v->halfpq = get_bits(gb, 1);
1318 else v->halfpq = 0;
1319 if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
1320 v->pquantizer = get_bits(gb, 1);
1321
674678b0
KS
1322 if(v->s.pict_type == I_TYPE || v->s.pict_type == P_TYPE) v->use_ic = 0;
1323
3c275f6d
KS
1324 switch(v->s.pict_type) {
1325 case I_TYPE:
76751653 1326 case BI_TYPE:
3c275f6d
KS
1327 status = bitplane_decoding(v->acpred_plane, &v->acpred_is_raw, v);
1328 if (status < 0) return -1;
1329 av_log(v->s.avctx, AV_LOG_DEBUG, "ACPRED plane encoding: "
1330 "Imode: %i, Invert: %i\n", status>>1, status&1);
1331 v->condover = CONDOVER_NONE;
1332 if(v->overlap && v->pq <= 8) {
1333 v->condover = decode012(gb);
1334 if(v->condover == CONDOVER_SELECT) {
1335 status = bitplane_decoding(v->over_flags_plane, &v->overflg_is_raw, v);
1336 if (status < 0) return -1;
1337 av_log(v->s.avctx, AV_LOG_DEBUG, "CONDOVER plane encoding: "
1338 "Imode: %i, Invert: %i\n", status>>1, status&1);
1339 }
1340 }
1341 break;
1342 case P_TYPE:
1343 if(v->postprocflag)
1344 v->postproc = get_bits1(gb);
3c2ddb59 1345 if (v->extended_mv) v->mvrange = get_unary(gb, 0, 3);
3c275f6d
KS
1346 else v->mvrange = 0;
1347 v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
1348 v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
1349 v->range_x = 1 << (v->k_x - 1);
1350 v->range_y = 1 << (v->k_y - 1);
1351
1352 if (v->pq < 5) v->tt_index = 0;
1353 else if(v->pq < 13) v->tt_index = 1;
1354 else v->tt_index = 2;
1355
1356 lowquant = (v->pq > 12) ? 0 : 1;
3c2ddb59 1357 v->mv_mode = ff_vc1_mv_pmode_table[lowquant][get_unary(gb, 1, 4)];
3c275f6d
KS
1358 if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
1359 {
1360 int scale, shift, i;
3c2ddb59 1361 v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
3c275f6d
KS
1362 v->lumscale = get_bits(gb, 6);
1363 v->lumshift = get_bits(gb, 6);
1364 /* fill lookup tables for intensity compensation */
1365 if(!v->lumscale) {
1366 scale = -64;
1367 shift = (255 - v->lumshift * 2) << 6;
1368 if(v->lumshift > 31)
1369 shift += 128 << 6;
1370 } else {
1371 scale = v->lumscale + 32;
1372 if(v->lumshift > 31)
1373 shift = (v->lumshift - 64) << 6;
1374 else
1375 shift = v->lumshift << 6;
1376 }
1377 for(i = 0; i < 256; i++) {
f66e4f5f
RD
1378 v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
1379 v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
3c275f6d 1380 }
674678b0 1381 v->use_ic = 1;
3c275f6d
KS
1382 }
1383 if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
1384 v->s.quarter_sample = 0;
1385 else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1386 if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
1387 v->s.quarter_sample = 0;
1388 else
1389 v->s.quarter_sample = 1;
1390 } else
1391 v->s.quarter_sample = 1;
1392 v->s.mspel = !(v->mv_mode == MV_PMODE_1MV_HPEL_BILIN || (v->mv_mode == MV_PMODE_INTENSITY_COMP && v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN));
1393
1394 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
1395 v->mv_mode2 == MV_PMODE_MIXED_MV)
1396 || v->mv_mode == MV_PMODE_MIXED_MV)
1397 {
1398 status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
1399 if (status < 0) return -1;
1400 av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
1401 "Imode: %i, Invert: %i\n", status>>1, status&1);
1402 } else {
1403 v->mv_type_is_raw = 0;
1404 memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
1405 }
1406 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1407 if (status < 0) return -1;
1408 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1409 "Imode: %i, Invert: %i\n", status>>1, status&1);
1410
1411 /* Hopefully this is correct for P frames */
a5c14fca
KS
1412 v->s.mv_table_index = get_bits(gb, 2); //but using ff_vc1_ tables
1413 v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
3c275f6d
KS
1414 if (v->dquant)
1415 {
1416 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1417 vop_dquant_decoding(v);
1418 }
1419
1420 v->ttfrm = 0; //FIXME Is that so ?
1421 if (v->vstransform)
1422 {
1423 v->ttmbf = get_bits(gb, 1);
1424 if (v->ttmbf)
1425 {
a5c14fca 1426 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
3c275f6d
KS
1427 }
1428 } else {
1429 v->ttmbf = 1;
1430 v->ttfrm = TT_8X8;
1431 }
1432 break;
5081f3aa
KS
1433 case B_TYPE:
1434 if(v->postprocflag)
1435 v->postproc = get_bits1(gb);
3c2ddb59 1436 if (v->extended_mv) v->mvrange = get_unary(gb, 0, 3);
5081f3aa
KS
1437 else v->mvrange = 0;
1438 v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
1439 v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
1440 v->range_x = 1 << (v->k_x - 1);
1441 v->range_y = 1 << (v->k_y - 1);
1442
1443 if (v->pq < 5) v->tt_index = 0;
1444 else if(v->pq < 13) v->tt_index = 1;
1445 else v->tt_index = 2;
1446
1447 lowquant = (v->pq > 12) ? 0 : 1;
1448 v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
1449 v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
1450 v->s.mspel = v->s.quarter_sample;
1451
1452 status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
1453 if (status < 0) return -1;
1454 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
1455 "Imode: %i, Invert: %i\n", status>>1, status&1);
1456 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1457 if (status < 0) return -1;
1458 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1459 "Imode: %i, Invert: %i\n", status>>1, status&1);
1460
1461 v->s.mv_table_index = get_bits(gb, 2);
a5c14fca 1462 v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
5081f3aa
KS
1463
1464 if (v->dquant)
1465 {
1466 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1467 vop_dquant_decoding(v);
1468 }
1469
1470 v->ttfrm = 0;
1471 if (v->vstransform)
1472 {
1473 v->ttmbf = get_bits(gb, 1);
1474 if (v->ttmbf)
1475 {
a5c14fca 1476 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
5081f3aa
KS
1477 }
1478 } else {
1479 v->ttmbf = 1;
1480 v->ttfrm = TT_8X8;
1481 }
1482 break;
3c275f6d
KS
1483 }
1484
1485 /* AC Syntax */
1486 v->c_ac_table_index = decode012(gb);
1487 if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
1488 {
1489 v->y_ac_table_index = decode012(gb);
1490 }
1491 /* DC Syntax */
1492 v->s.dc_table_index = get_bits(gb, 1);
b024824b 1493 if ((v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE) && v->dquant) {
3c275f6d
KS
1494 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1495 vop_dquant_decoding(v);
1496 }
1497
76751653
KS
1498 v->bi_type = 0;
1499 if(v->s.pict_type == BI_TYPE) {
1500 v->s.pict_type = B_TYPE;
1501 v->bi_type = 1;
1502 }
3c275f6d
KS
1503 return 0;
1504}
1505
2ce151f8 1506/***********************************************************************/
1507/**
be3492ec
KS
1508 * @defgroup block VC-1 Block-level functions
1509 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2ce151f8 1510 * @{
1511 */
1512
be3492ec
KS
1513/**
1514 * @def GET_MQUANT
1515 * @brief Get macroblock-level quantizer scale
2ce151f8 1516 */
be3492ec
KS
1517#define GET_MQUANT() \
1518 if (v->dquantfrm) \
1519 { \
35a9cac8 1520 int edges = 0; \
be3492ec
KS
1521 if (v->dqprofile == DQPROFILE_ALL_MBS) \
1522 { \
1523 if (v->dqbilevel) \
1524 { \
3a3f1cf3 1525 mquant = (get_bits(gb, 1)) ? v->altpq : v->pq; \
be3492ec
KS
1526 } \
1527 else \
1528 { \
1529 mqdiff = get_bits(gb, 3); \
1530 if (mqdiff != 7) mquant = v->pq + mqdiff; \
1531 else mquant = get_bits(gb, 5); \
1532 } \
1533 } \
35a9cac8
KS
1534 if(v->dqprofile == DQPROFILE_SINGLE_EDGE) \
1535 edges = 1 << v->dqsbedge; \
e4bf0302 1536 else if(v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
35a9cac8 1537 edges = (3 << v->dqsbedge) % 15; \
e4bf0302 1538 else if(v->dqprofile == DQPROFILE_FOUR_EDGES) \
35a9cac8 1539 edges = 15; \
35a9cac8
KS
1540 if((edges&1) && !s->mb_x) \
1541 mquant = v->altpq; \
3a3f1cf3 1542 if((edges&2) && s->first_slice_line) \
35a9cac8
KS
1543 mquant = v->altpq; \
1544 if((edges&4) && s->mb_x == (s->mb_width - 1)) \
1545 mquant = v->altpq; \
1546 if((edges&8) && s->mb_y == (s->mb_height - 1)) \
1547 mquant = v->altpq; \
be3492ec 1548 }
2ce151f8 1549
be3492ec
KS
1550/**
1551 * @def GET_MVDATA(_dmv_x, _dmv_y)
1552 * @brief Get MV differentials
1553 * @see MVDATA decoding from 8.3.5.2, p(1)20
1554 * @param _dmv_x Horizontal differential for decoded MV
1555 * @param _dmv_y Vertical differential for decoded MV
2ce151f8 1556 */
be3492ec 1557#define GET_MVDATA(_dmv_x, _dmv_y) \
a5c14fca 1558 index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table,\
be3492ec
KS
1559 VC1_MV_DIFF_VLC_BITS, 2); \
1560 if (index > 36) \
1561 { \
1562 mb_has_coeffs = 1; \
1563 index -= 37; \
1564 } \
1565 else mb_has_coeffs = 0; \
1566 s->mb_intra = 0; \
1567 if (!index) { _dmv_x = _dmv_y = 0; } \
1568 else if (index == 35) \
1569 { \
87dfe848
KS
1570 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
1571 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
be3492ec
KS
1572 } \
1573 else if (index == 36) \
1574 { \
1575 _dmv_x = 0; \
1576 _dmv_y = 0; \
1577 s->mb_intra = 1; \
1578 } \
1579 else \
1580 { \
1581 index1 = index%6; \
87dfe848
KS
1582 if (!s->quarter_sample && index1 == 5) val = 1; \
1583 else val = 0; \
e8ba1cea
KS
1584 if(size_table[index1] - val > 0) \
1585 val = get_bits(gb, size_table[index1] - val); \
1586 else val = 0; \
be3492ec
KS
1587 sign = 0 - (val&1); \
1588 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1589 \
1590 index1 = index/6; \
87dfe848
KS
1591 if (!s->quarter_sample && index1 == 5) val = 1; \
1592 else val = 0; \
e8ba1cea
KS
1593 if(size_table[index1] - val > 0) \
1594 val = get_bits(gb, size_table[index1] - val); \
1595 else val = 0; \
be3492ec
KS
1596 sign = 0 - (val&1); \
1597 _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1598 }
21aa398f 1599
be3492ec 1600/** Predict and set motion vector
2ce151f8 1601 */
e4bf0302 1602static 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)
0d33db8a 1603{
e4bf0302 1604 int xy, wrap, off = 0;
be3492ec
KS
1605 int16_t *A, *B, *C;
1606 int px, py;
1607 int sum;
0d33db8a 1608
be3492ec 1609 /* scale MV difference to be quad-pel */
87dfe848
KS
1610 dmv_x <<= 1 - s->quarter_sample;
1611 dmv_y <<= 1 - s->quarter_sample;
d29f0cd9 1612
be3492ec 1613 wrap = s->b8_stride;
e4bf0302 1614 xy = s->block_index[n];
d29f0cd9 1615
e4bf0302
KS
1616 if(s->mb_intra){
1617 s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
1618 s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
9a44385e
KS
1619 s->current_picture.motion_val[1][xy][0] = 0;
1620 s->current_picture.motion_val[1][xy][1] = 0;
e4bf0302
KS
1621 if(mv1) { /* duplicate motion data for 1-MV block */
1622 s->current_picture.motion_val[0][xy + 1][0] = 0;
1623 s->current_picture.motion_val[0][xy + 1][1] = 0;
1624 s->current_picture.motion_val[0][xy + wrap][0] = 0;
1625 s->current_picture.motion_val[0][xy + wrap][1] = 0;
1626 s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
1627 s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
9a44385e
KS
1628 s->current_picture.motion_val[1][xy + 1][0] = 0;
1629 s->current_picture.motion_val[1][xy + 1][1] = 0;
1630 s->current_picture.motion_val[1][xy + wrap][0] = 0;
1631 s->current_picture.motion_val[1][xy + wrap][1] = 0;
1632 s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
1633 s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
e4bf0302
KS
1634 }
1635 return;
1636 }
1637
1638 C = s->current_picture.motion_val[0][xy - 1];
1639 A = s->current_picture.motion_val[0][xy - wrap];
1640 if(mv1)
1641 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1642 else {
1643 //in 4-MV mode different blocks have different B predictor position
1644 switch(n){
1645 case 0:
1646 off = (s->mb_x > 0) ? -1 : 1;
1647 break;
1648 case 1:
1649 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1650 break;
1651 case 2:
1652 off = 1;
1653 break;
1654 case 3:
1655 off = -1;
1656 }
1657 }
1658 B = s->current_picture.motion_val[0][xy - wrap + off];
d29f0cd9 1659
e4bf0302 1660 if(!s->first_slice_line || (n==2 || n==3)) { // predictor A is not out of bounds
be3492ec
KS
1661 if(s->mb_width == 1) {
1662 px = A[0];
1663 py = A[1];
1664 } else {
1665 px = mid_pred(A[0], B[0], C[0]);
1666 py = mid_pred(A[1], B[1], C[1]);
1667 }
e4bf0302 1668 } else if(s->mb_x || (n==1 || n==3)) { // predictor C is not out of bounds
be3492ec
KS
1669 px = C[0];
1670 py = C[1];
1671 } else {
1672 px = py = 0;
1673 }
be3492ec
KS
1674 /* Pullback MV as specified in 8.3.5.3.4 */
1675 {
1676 int qx, qy, X, Y;
ef6cc8ce
KS
1677 qx = (s->mb_x << 6) + ((n==1 || n==3) ? 32 : 0);
1678 qy = (s->mb_y << 6) + ((n==2 || n==3) ? 32 : 0);
be3492ec
KS
1679 X = (s->mb_width << 6) - 4;
1680 Y = (s->mb_height << 6) - 4;
1681 if(mv1) {
1682 if(qx + px < -60) px = -60 - qx;
1683 if(qy + py < -60) py = -60 - qy;
1684 } else {
1685 if(qx + px < -28) px = -28 - qx;
1686 if(qy + py < -28) py = -28 - qy;
1687 }
1688 if(qx + px > X) px = X - qx;
1689 if(qy + py > Y) py = Y - qy;
1690 }
1691 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
e4bf0302
KS
1692 if((!s->first_slice_line || (n==2 || n==3)) && (s->mb_x || (n==1 || n==3))) {
1693 if(is_intra[xy - wrap])
c26abfa5 1694 sum = FFABS(px) + FFABS(py);
d29f0cd9 1695 else
c26abfa5 1696 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
be3492ec
KS
1697 if(sum > 32) {
1698 if(get_bits1(&s->gb)) {
1699 px = A[0];
1700 py = A[1];
1701 } else {
1702 px = C[0];
1703 py = C[1];
1704 }
1705 } else {
e4bf0302 1706 if(is_intra[xy - 1])
c26abfa5 1707 sum = FFABS(px) + FFABS(py);
be3492ec 1708 else
c26abfa5 1709 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
be3492ec
KS
1710 if(sum > 32) {
1711 if(get_bits1(&s->gb)) {
1712 px = A[0];
1713 py = A[1];
1714 } else {
1715 px = C[0];
1716 py = C[1];
1717 }
1718 }
1719 }
d29f0cd9 1720 }
be3492ec 1721 /* store MV using signed modulus of MV range defined in 4.11 */
e4bf0302
KS
1722 s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1723 s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
1724 if(mv1) { /* duplicate motion data for 1-MV block */
1725 s->current_picture.motion_val[0][xy + 1][0] = s->current_picture.motion_val[0][xy][0];
1726 s->current_picture.motion_val[0][xy + 1][1] = s->current_picture.motion_val[0][xy][1];
1727 s->current_picture.motion_val[0][xy + wrap][0] = s->current_picture.motion_val[0][xy][0];
1728 s->current_picture.motion_val[0][xy + wrap][1] = s->current_picture.motion_val[0][xy][1];
1729 s->current_picture.motion_val[0][xy + wrap + 1][0] = s->current_picture.motion_val[0][xy][0];
1730 s->current_picture.motion_val[0][xy + wrap + 1][1] = s->current_picture.motion_val[0][xy][1];
1731 }
d29f0cd9
MN
1732}
1733
fb2d9140
KS
1734/** Motion compensation for direct or interpolated blocks in B-frames
1735 */
1736static void vc1_interp_mc(VC1Context *v)
1737{
1738 MpegEncContext *s = &v->s;
1739 DSPContext *dsp = &v->s.dsp;
1740 uint8_t *srcY, *srcU, *srcV;
1741 int dxy, uvdxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1742
1743 if(!v->s.next_picture.data[0])return;
1744
1745 mx = s->mv[1][0][0];
1746 my = s->mv[1][0][1];
1747 uvmx = (mx + ((mx & 3) == 3)) >> 1;
1748 uvmy = (my + ((my & 3) == 3)) >> 1;
08baa3e0
KS
1749 if(v->fastuvmc) {
1750 uvmx = uvmx + ((uvmx<0)?-(uvmx&1):(uvmx&1));
1751 uvmy = uvmy + ((uvmy<0)?-(uvmy&1):(uvmy&1));
1752 }
fb2d9140
KS
1753 srcY = s->next_picture.data[0];
1754 srcU = s->next_picture.data[1];
1755 srcV = s->next_picture.data[2];
1756
1757 src_x = s->mb_x * 16 + (mx >> 2);
1758 src_y = s->mb_y * 16 + (my >> 2);
1759 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1760 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1761
7c971233
KS
1762 if(v->profile != PROFILE_ADVANCED){
1763 src_x = av_clip( src_x, -16, s->mb_width * 16);
1764 src_y = av_clip( src_y, -16, s->mb_height * 16);
1765 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
1766 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
1767 }else{
1768 src_x = av_clip( src_x, -17, s->avctx->coded_width);
1769 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
1770 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1771 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1772 }
fb2d9140
KS
1773
1774 srcY += src_y * s->linesize + src_x;
1775 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1776 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1777
1778 /* for grayscale we should not try to read from unknown area */
1779 if(s->flags & CODEC_FLAG_GRAY) {
1780 srcU = s->edge_emu_buffer + 18 * s->linesize;
1781 srcV = s->edge_emu_buffer + 18 * s->linesize;
1782 }
1783
1784 if(v->rangeredfrm
1785 || (unsigned)src_x > s->h_edge_pos - (mx&3) - 16
1786 || (unsigned)src_y > s->v_edge_pos - (my&3) - 16){
1787 uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
1788
b116cc7f
KS
1789 srcY -= s->mspel * (1 + s->linesize);
1790 ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2,
fb2d9140
KS
1791 src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
1792 srcY = s->edge_emu_buffer;
1793 ff_emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8+1, 8+1,
1794 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
1795 ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
1796 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
1797 srcU = uvbuf;
1798 srcV = uvbuf + 16;
1799 /* if we deal with range reduction we need to scale source blocks */
1800 if(v->rangeredfrm) {
1801 int i, j;
1802 uint8_t *src, *src2;
1803
1804 src = srcY;
b116cc7f
KS
1805 for(j = 0; j < 17 + s->mspel*2; j++) {
1806 for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
fb2d9140
KS
1807 src += s->linesize;
1808 }
1809 src = srcU; src2 = srcV;
1810 for(j = 0; j < 9; j++) {
1811 for(i = 0; i < 9; i++) {
1812 src[i] = ((src[i] - 128) >> 1) + 128;
1813 src2[i] = ((src2[i] - 128) >> 1) + 128;
1814 }
1815 src += s->uvlinesize;
1816 src2 += s->uvlinesize;
1817 }
1818 }
b116cc7f 1819 srcY += s->mspel * (1 + s->linesize);
fb2d9140
KS
1820 }
1821
66ff2c1f
KS
1822 mx >>= 1;
1823 my >>= 1;
1824 dxy = ((my & 1) << 1) | (mx & 1);
fb2d9140 1825
66ff2c1f 1826 dsp->avg_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
fb2d9140
KS
1827
1828 if(s->flags & CODEC_FLAG_GRAY) return;
1829 /* Chroma MC always uses qpel blilinear */
1830 uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
c8868640
KS
1831 uvmx = (uvmx&3)<<1;
1832 uvmy = (uvmy&3)<<1;
1833 dsp->avg_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
1834 dsp->avg_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
fb2d9140
KS
1835}
1836
849f1035 1837static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
1dc1ce64
KS
1838{
1839 int n = bfrac;
1840
1841#if B_FRACTION_DEN==256
1842 if(inv)
1843 n -= 256;
1844 if(!qs)
1845 return 2 * ((value * n + 255) >> 9);
1846 return (value * n + 128) >> 8;
1847#else
1848 if(inv)
1849 n -= B_FRACTION_DEN;
1850 if(!qs)
1851 return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
1852 return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
1853#endif
1854}
1855
5df68893
KS
1856/** Reconstruct motion vector for B-frame and do motion compensation
1857 */
1858static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mode)
1859{
6b33eed8
KS
1860 if(v->use_ic) {
1861 v->mv_mode2 = v->mv_mode;
1862 v->mv_mode = MV_PMODE_INTENSITY_COMP;
1863 }
1dc1ce64
KS
1864 if(direct) {
1865 vc1_mc_1mv(v, 0);
1866 vc1_interp_mc(v);
6b33eed8 1867 if(v->use_ic) v->mv_mode = v->mv_mode2;
1dc1ce64
KS
1868 return;
1869 }
1870 if(mode == BMV_TYPE_INTERPOLATED) {
1871 vc1_mc_1mv(v, 0);
1872 vc1_interp_mc(v);
6b33eed8 1873 if(v->use_ic) v->mv_mode = v->mv_mode2;
1dc1ce64
KS
1874 return;
1875 }
1876
6b33eed8 1877 if(v->use_ic && (mode == BMV_TYPE_BACKWARD)) v->mv_mode = v->mv_mode2;
e179fbc8 1878 vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
6b33eed8 1879 if(v->use_ic) v->mv_mode = v->mv_mode2;
1dc1ce64
KS
1880}
1881
1882static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mvtype)
1883{
5df68893 1884 MpegEncContext *s = &v->s;
1dc1ce64
KS
1885 int xy, wrap, off = 0;
1886 int16_t *A, *B, *C;
1887 int px, py;
1888 int sum;
1889 int r_x, r_y;
1890 const uint8_t *is_intra = v->mb_type[0];
5df68893 1891
1dc1ce64
KS
1892 r_x = v->range_x;
1893 r_y = v->range_y;
5df68893
KS
1894 /* scale MV difference to be quad-pel */
1895 dmv_x[0] <<= 1 - s->quarter_sample;
1896 dmv_y[0] <<= 1 - s->quarter_sample;
1897 dmv_x[1] <<= 1 - s->quarter_sample;
1898 dmv_y[1] <<= 1 - s->quarter_sample;
1899
1dc1ce64
KS
1900 wrap = s->b8_stride;
1901 xy = s->block_index[0];
fb2d9140 1902
1dc1ce64
KS
1903 if(s->mb_intra) {
1904 s->current_picture.motion_val[0][xy][0] =
1905 s->current_picture.motion_val[0][xy][1] =
1906 s->current_picture.motion_val[1][xy][0] =
1907 s->current_picture.motion_val[1][xy][1] = 0;
fb2d9140
KS
1908 return;
1909 }
162f412d
KS
1910 s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
1911 s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
1912 s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
1913 s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
a6145615
KS
1914
1915 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
1916 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));
1917 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));
1918 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));
1919 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));
1dc1ce64
KS
1920 if(direct) {
1921 s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
1922 s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
1923 s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
1924 s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
5df68893
KS
1925 return;
1926 }
1927
162f412d 1928 if((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
1dc1ce64
KS
1929 C = s->current_picture.motion_val[0][xy - 2];
1930 A = s->current_picture.motion_val[0][xy - wrap*2];
1931 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1932 B = s->current_picture.motion_val[0][xy - wrap*2 + off];
1933
44942d52 1934 if(!s->mb_x) C[0] = C[1] = 0;
1dc1ce64
KS
1935 if(!s->first_slice_line) { // predictor A is not out of bounds
1936 if(s->mb_width == 1) {
1937 px = A[0];
1938 py = A[1];
1939 } else {
1940 px = mid_pred(A[0], B[0], C[0]);
1941 py = mid_pred(A[1], B[1], C[1]);
1942 }
1943 } else if(s->mb_x) { // predictor C is not out of bounds
1944 px = C[0];
1945 py = C[1];
1946 } else {
1947 px = py = 0;
5df68893 1948 }
1dc1ce64
KS
1949 /* Pullback MV as specified in 8.3.5.3.4 */
1950 {
1951 int qx, qy, X, Y;
1952 if(v->profile < PROFILE_ADVANCED) {
1953 qx = (s->mb_x << 5);
1954 qy = (s->mb_y << 5);
1955 X = (s->mb_width << 5) - 4;
1956 Y = (s->mb_height << 5) - 4;
1957 if(qx + px < -28) px = -28 - qx;
1958 if(qy + py < -28) py = -28 - qy;
1959 if(qx + px > X) px = X - qx;
1960 if(qy + py > Y) py = Y - qy;
1961 } else {
1962 qx = (s->mb_x << 6);
1963 qy = (s->mb_y << 6);
1964 X = (s->mb_width << 6) - 4;
1965 Y = (s->mb_height << 6) - 4;
1966 if(qx + px < -60) px = -60 - qx;
1967 if(qy + py < -60) py = -60 - qy;
1968 if(qx + px > X) px = X - qx;
1969 if(qy + py > Y) py = Y - qy;
1970 }
1971 }
1972 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
1973 if(0 && !s->first_slice_line && s->mb_x) {
1974 if(is_intra[xy - wrap])
c26abfa5 1975 sum = FFABS(px) + FFABS(py);
1dc1ce64 1976 else
c26abfa5 1977 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
1dc1ce64
KS
1978 if(sum > 32) {
1979 if(get_bits1(&s->gb)) {
1980 px = A[0];
1981 py = A[1];
1982 } else {
1983 px = C[0];
1984 py = C[1];
1985 }
1986 } else {
1987 if(is_intra[xy - 2])
c26abfa5 1988 sum = FFABS(px) + FFABS(py);
1dc1ce64 1989 else
c26abfa5 1990 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
1dc1ce64
KS
1991 if(sum > 32) {
1992 if(get_bits1(&s->gb)) {
1993 px = A[0];
1994 py = A[1];
1995 } else {
1996 px = C[0];
1997 py = C[1];
1998 }
1999 }
2000 }
5df68893 2001 }
1dc1ce64
KS
2002 /* store MV using signed modulus of MV range defined in 4.11 */
2003 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2004 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
5df68893 2005 }
162f412d 2006 if((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
1dc1ce64
KS
2007 C = s->current_picture.motion_val[1][xy - 2];
2008 A = s->current_picture.motion_val[1][xy - wrap*2];
2009 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2010 B = s->current_picture.motion_val[1][xy - wrap*2 + off];
5df68893 2011
44942d52 2012 if(!s->mb_x) C[0] = C[1] = 0;
1dc1ce64
KS
2013 if(!s->first_slice_line) { // predictor A is not out of bounds
2014 if(s->mb_width == 1) {
2015 px = A[0];
2016 py = A[1];
2017 } else {
2018 px = mid_pred(A[0], B[0], C[0]);
2019 py = mid_pred(A[1], B[1], C[1]);
2020 }
2021 } else if(s->mb_x) { // predictor C is not out of bounds
2022 px = C[0];
2023 py = C[1];
2024 } else {
2025 px = py = 0;
2026 }
2027 /* Pullback MV as specified in 8.3.5.3.4 */
2028 {
2029 int qx, qy, X, Y;
2030 if(v->profile < PROFILE_ADVANCED) {
2031 qx = (s->mb_x << 5);
2032 qy = (s->mb_y << 5);
2033 X = (s->mb_width << 5) - 4;
2034 Y = (s->mb_height << 5) - 4;
2035 if(qx + px < -28) px = -28 - qx;
2036 if(qy + py < -28) py = -28 - qy;
2037 if(qx + px > X) px = X - qx;
2038 if(qy + py > Y) py = Y - qy;
2039 } else {
2040 qx = (s->mb_x << 6);
2041 qy = (s->mb_y << 6);
2042 X = (s->mb_width << 6) - 4;
2043 Y = (s->mb_height << 6) - 4;
2044 if(qx + px < -60) px = -60 - qx;
2045 if(qy + py < -60) py = -60 - qy;
2046 if(qx + px > X) px = X - qx;
2047 if(qy + py > Y) py = Y - qy;
2048 }
2049 }
2050 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2051 if(0 && !s->first_slice_line && s->mb_x) {
2052 if(is_intra[xy - wrap])
c26abfa5 2053 sum = FFABS(px) + FFABS(py);
1dc1ce64 2054 else
c26abfa5 2055 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
1dc1ce64
KS
2056 if(sum > 32) {
2057 if(get_bits1(&s->gb)) {
2058 px = A[0];
2059 py = A[1];
2060 } else {
2061 px = C[0];
2062 py = C[1];
2063 }
2064 } else {
2065 if(is_intra[xy - 2])
c26abfa5 2066 sum = FFABS(px) + FFABS(py);
1dc1ce64 2067 else
c26abfa5 2068 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
1dc1ce64
KS
2069 if(sum > 32) {
2070 if(get_bits1(&s->gb)) {
2071 px = A[0];
2072 py = A[1];
2073 } else {
2074 px = C[0];
2075 py = C[1];
2076 }
2077 }
2078 }
2079 }
2080 /* store MV using signed modulus of MV range defined in 4.11 */
5df68893 2081
1dc1ce64
KS
2082 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2083 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2084 }
2085 s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
2086 s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
2087 s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
2088 s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
5df68893
KS
2089}
2090
be3492ec
KS
2091/** Get predicted DC value for I-frames only
2092 * prediction dir: left=0, top=1
2093 * @param s MpegEncContext
2094 * @param[in] n block index in the current MB
2095 * @param dc_val_ptr Pointer to DC predictor
2096 * @param dir_ptr Prediction direction for use in AC prediction
2ce151f8 2097 */
be3492ec 2098static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
b86216de 2099 int16_t **dc_val_ptr, int *dir_ptr)
21aa398f 2100{
be3492ec 2101 int a, b, c, wrap, pred, scale;
b86216de 2102 int16_t *dc_val;
be3492ec
KS
2103 static const uint16_t dcpred[32] = {
2104 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
2105 114, 102, 93, 85, 79, 73, 68, 64,
2106 60, 57, 54, 51, 49, 47, 45, 43,
2107 41, 39, 38, 37, 35, 34, 33
2108 };
0d33db8a 2109
be3492ec
KS
2110 /* find prediction - wmv3_dc_scale always used here in fact */
2111 if (n < 4) scale = s->y_dc_scale;
2112 else scale = s->c_dc_scale;
21aa398f 2113
be3492ec
KS
2114 wrap = s->block_wrap[n];
2115 dc_val= s->dc_val[0] + s->block_index[n];
21aa398f 2116
be3492ec
KS
2117 /* B A
2118 * C X
2119 */
2120 c = dc_val[ - 1];
2121 b = dc_val[ - 1 - wrap];
2122 a = dc_val[ - wrap];
21aa398f 2123
be3492ec 2124 if (pq < 9 || !overlap)
21aa398f 2125 {
be3492ec 2126 /* Set outer values */
d2779ecd 2127 if (s->first_slice_line && (n!=2 && n!=3)) b=a=dcpred[scale];
be3492ec 2128 if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=dcpred[scale];
bf2bc926 2129 }
2130 else
2131 {
be3492ec 2132 /* Set outer values */
d2779ecd 2133 if (s->first_slice_line && (n!=2 && n!=3)) b=a=0;
be3492ec 2134 if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=0;
21aa398f 2135 }
21aa398f 2136
be3492ec
KS
2137 if (abs(a - b) <= abs(b - c)) {
2138 pred = c;
2139 *dir_ptr = 1;//left
2140 } else {
2141 pred = a;
2142 *dir_ptr = 0;//top
21aa398f
AB
2143 }
2144
be3492ec
KS
2145 /* update predictor */
2146 *dc_val_ptr = &dc_val[0];
2147 return pred;
bf2bc926 2148}
2149
21aa398f 2150
be3492ec
KS
2151/** Get predicted DC value
2152 * prediction dir: left=0, top=1
2153 * @param s MpegEncContext
2154 * @param[in] n block index in the current MB
2155 * @param dc_val_ptr Pointer to DC predictor
2156 * @param dir_ptr Prediction direction for use in AC prediction
2ce151f8 2157 */
be3492ec
KS
2158static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2159 int a_avail, int c_avail,
b86216de 2160 int16_t **dc_val_ptr, int *dir_ptr)
21aa398f 2161{
be3492ec 2162 int a, b, c, wrap, pred, scale;
b86216de 2163 int16_t *dc_val;
be3492ec 2164 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
50af2fd3 2165 int q1, q2 = 0;
21aa398f 2166
be3492ec
KS
2167 /* find prediction - wmv3_dc_scale always used here in fact */
2168 if (n < 4) scale = s->y_dc_scale;
2169 else scale = s->c_dc_scale;
21aa398f 2170
be3492ec
KS
2171 wrap = s->block_wrap[n];
2172 dc_val= s->dc_val[0] + s->block_index[n];
21aa398f 2173
be3492ec
KS
2174 /* B A
2175 * C X
2176 */
2177 c = dc_val[ - 1];
2178 b = dc_val[ - 1 - wrap];
2179 a = dc_val[ - wrap];
27ed1a0d
KS
2180 /* scale predictors if needed */
2181 q1 = s->current_picture.qscale_table[mb_pos];
2182 if(c_avail && (n!= 1 && n!=3)) {
2183 q2 = s->current_picture.qscale_table[mb_pos - 1];
2184 if(q2 && q2 != q1)
a5c14fca 2185 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
27ed1a0d
KS
2186 }
2187 if(a_avail && (n!= 2 && n!=3)) {
2188 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2189 if(q2 && q2 != q1)
a5c14fca 2190 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
27ed1a0d
KS
2191 }
2192 if(a_avail && c_avail && (n!=3)) {
2193 int off = mb_pos;
2194 if(n != 1) off--;
2195 if(n != 2) off -= s->mb_stride;
2196 q2 = s->current_picture.qscale_table[off];
2197 if(q2 && q2 != q1)
a5c14fca 2198 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
27ed1a0d 2199 }
21aa398f 2200
be3492ec
KS
2201 if(a_avail && c_avail) {
2202 if(abs(a - b) <= abs(b - c)) {
2203 pred = c;
2204 *dir_ptr = 1;//left
2205 } else {
2206 pred = a;
2207 *dir_ptr = 0;//top
2208 }
2209 } else if(a_avail) {
2210 pred = a;
2211 *dir_ptr = 0;//top
2212 } else if(c_avail) {
2213 pred = c;
2214 *dir_ptr = 1;//left
2215 } else {
2216 pred = 0;
2217 *dir_ptr = 1;//left
21aa398f
AB
2218 }
2219
be3492ec
KS
2220 /* update predictor */
2221 *dc_val_ptr = &dc_val[0];
2222 return pred;
21aa398f 2223}
2ce151f8 2224
21aa398f 2225
115329f1 2226/**
be3492ec
KS
2227 * @defgroup std_mb VC1 Macroblock-level functions in Simple/Main Profiles
2228 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2ce151f8 2229 * @{
2230 */
21aa398f 2231
be3492ec 2232static inline int vc1_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
21aa398f 2233{
be3492ec 2234 int xy, wrap, pred, a, b, c;
21aa398f 2235
be3492ec
KS
2236 xy = s->block_index[n];
2237 wrap = s->b8_stride;
21aa398f 2238
be3492ec
KS
2239 /* B C
2240 * A X
2241 */
2242 a = s->coded_block[xy - 1 ];
2243 b = s->coded_block[xy - 1 - wrap];
2244 c = s->coded_block[xy - wrap];
21aa398f 2245
be3492ec
KS
2246 if (b == c) {
2247 pred = a;
2248 } else {
2249 pred = c;
21aa398f 2250 }
be3492ec
KS
2251
2252 /* store value */
2253 *coded_block_ptr = &s->coded_block[xy];
2254
2255 return pred;
0d33db8a 2256}
2257
be3492ec
KS
2258/**
2259 * Decode one AC coefficient
2260 * @param v The VC1 context
2261 * @param last Last coefficient
2262 * @param skip How much zero coefficients to skip
2263 * @param value Decoded AC coefficient value
2264 * @see 8.1.3.4
2ce151f8 2265 */
be3492ec 2266static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip, int *value, int codingset)
0d33db8a 2267{
2268 GetBitContext *gb = &v->s.gb;
be3492ec
KS
2269 int index, escape, run = 0, level = 0, lst = 0;
2270
a5c14fca 2271 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
be3492ec
KS
2272 if (index != vc1_ac_sizes[codingset] - 1) {
2273 run = vc1_index_decode_table[codingset][index][0];
2274 level = vc1_index_decode_table[codingset][index][1];
2275 lst = index >= vc1_last_decode_table[codingset];
2276 if(get_bits(gb, 1))
2277 level = -level;
2278 } else {
2279 escape = decode210(gb);
87dfe848 2280 if (escape != 2) {
a5c14fca 2281 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
be3492ec
KS
2282 run = vc1_index_decode_table[codingset][index][0];
2283 level = vc1_index_decode_table[codingset][index][1];
2284 lst = index >= vc1_last_decode_table[codingset];
87dfe848
KS
2285 if(escape == 0) {
2286 if(lst)
2287 level += vc1_last_delta_level_table[codingset][run];
2288 else
2289 level += vc1_delta_level_table[codingset][run];
2290 } else {
2291 if(lst)
2292 run += vc1_last_delta_run_table[codingset][level] + 1;
2293 else
2294 run += vc1_delta_run_table[codingset][level] + 1;
2295 }
be3492ec
KS
2296 if(get_bits(gb, 1))
2297 level = -level;
2298 } else {
2299 int sign;
2300 lst = get_bits(gb, 1);
2301 if(v->s.esc3_level_length == 0) {
2302 if(v->pq < 8 || v->dquantfrm) { // table 59
2303 v->s.esc3_level_length = get_bits(gb, 3);
2304 if(!v->s.esc3_level_length)
2305 v->s.esc3_level_length = get_bits(gb, 2) + 8;
2306 } else { //table 60
3c2ddb59 2307 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
be3492ec
KS
2308 }
2309 v->s.esc3_run_length = 3 + get_bits(gb, 2);
2310 }
2311 run = get_bits(gb, v->s.esc3_run_length);
2312 sign = get_bits(gb, 1);
2313 level = get_bits(gb, v->s.esc3_level_length);
2314 if(sign)
2315 level = -level;
2316 }
21aa398f 2317 }
7cc84d24 2318
be3492ec
KS
2319 *last = lst;
2320 *skip = run;
2321 *value = level;
21aa398f 2322}
21aa398f 2323
be3492ec
KS
2324/** Decode intra block in intra frames - should be faster than decode_intra_block
2325 * @param v VC1Context
2326 * @param block block to decode
2327 * @param coded are AC coeffs present or not
2328 * @param codingset set of VLC to decode data
2ce151f8 2329 */
be3492ec 2330static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset)
21aa398f 2331{
0d33db8a 2332 GetBitContext *gb = &v->s.gb;
be3492ec
KS
2333 MpegEncContext *s = &v->s;
2334 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2335 int run_diff, i;
b86216de 2336 int16_t *dc_val;
be3492ec
KS
2337 int16_t *ac_val, *ac_val2;
2338 int dcdiff;
21aa398f 2339
be3492ec
KS
2340 /* Get DC differential */
2341 if (n < 4) {
2342 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2343 } else {
2344 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
21aa398f 2345 }
be3492ec
KS
2346 if (dcdiff < 0){
2347 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2348 return -1;
2349 }
2350 if (dcdiff)
21aa398f 2351 {
be3492ec 2352 if (dcdiff == 119 /* ESC index value */)
21aa398f 2353 {
be3492ec
KS
2354 /* TODO: Optimize */
2355 if (v->pq == 1) dcdiff = get_bits(gb, 10);
2356 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
2357 else dcdiff = get_bits(gb, 8);
21aa398f 2358 }
be3492ec 2359 else
21aa398f 2360 {
be3492ec
KS
2361 if (v->pq == 1)
2362 dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
2363 else if (v->pq == 2)
2364 dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
21aa398f 2365 }
be3492ec
KS
2366 if (get_bits(gb, 1))
2367 dcdiff = -dcdiff;
21aa398f 2368 }
21aa398f 2369
be3492ec
KS
2370 /* Prediction */
2371 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2372 *dc_val = dcdiff;
0d33db8a 2373
be3492ec 2374 /* Store the quantized DC coeff, used for prediction */
be3492ec
KS
2375 if (n < 4) {
2376 block[0] = dcdiff * s->y_dc_scale;
2377 } else {
2378 block[0] = dcdiff * s->c_dc_scale;
21aa398f 2379 }
be3492ec
KS
2380 /* Skip ? */
2381 run_diff = 0;
2382 i = 0;
2383 if (!coded) {
2384 goto not_coded;
21aa398f 2385 }
7cc84d24 2386
be3492ec
KS
2387 //AC Decoding
2388 i = 1;
7cc84d24 2389
be3492ec
KS
2390 {
2391 int last = 0, skip, value;
2392 const int8_t *zz_table;
2393 int scale;
2394 int k;
8da75fb2 2395
be3492ec 2396 scale = v->pq * 2 + v->halfpq;
42cc17f9 2397
be3492ec
KS
2398 if(v->s.ac_pred) {
2399 if(!dc_pred_dir)
a5c14fca 2400 zz_table = ff_vc1_horizontal_zz;
be3492ec 2401 else
a5c14fca 2402 zz_table = ff_vc1_vertical_zz;
be3492ec 2403 } else
a5c14fca 2404 zz_table = ff_vc1_normal_zz;
be3492ec
KS
2405
2406 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2407 ac_val2 = ac_val;
2408 if(dc_pred_dir) //left
2409 ac_val -= 16;
2410 else //top
2411 ac_val -= 16 * s->block_wrap[n];
2412
2413 while (!last) {
2414 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2415 i += skip;
2416 if(i > 63)
2417 break;
2418 block[zz_table[i++]] = value;
2419 }
7cc84d24 2420
be3492ec
KS
2421 /* apply AC prediction if needed */
2422 if(s->ac_pred) {
2423 if(dc_pred_dir) { //left
2424 for(k = 1; k < 8; k++)
2425 block[k << 3] += ac_val[k];
2426 } else { //top
2427 for(k = 1; k < 8; k++)
2428 block[k] += ac_val[k + 8];
2429 }
2430 }
2431 /* save AC coeffs for further prediction */
2432 for(k = 1; k < 8; k++) {
2433 ac_val2[k] = block[k << 3];
2434 ac_val2[k + 8] = block[k];
2435 }
42cc17f9 2436
be3492ec
KS
2437 /* scale AC coeffs */
2438 for(k = 1; k < 64; k++)
2439 if(block[k]) {
2440 block[k] *= scale;
2441 if(!v->pquantizer)
2442 block[k] += (block[k] < 0) ? -v->pq : v->pq;
2443 }
8da75fb2 2444
be3492ec
KS
2445 if(s->ac_pred) i = 63;
2446 }
2447
2448not_coded:
2449 if(!coded) {
2450 int k, scale;
2451 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2452 ac_val2 = ac_val;
2453
2454 scale = v->pq * 2 + v->halfpq;
2455 memset(ac_val2, 0, 16 * 2);
2456 if(dc_pred_dir) {//left
2457 ac_val -= 16;
2458 if(s->ac_pred)
2459 memcpy(ac_val2, ac_val, 8 * 2);
2460 } else {//top
2461 ac_val -= 16 * s->block_wrap[n];
2462 if(s->ac_pred)
2463 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
8da75fb2 2464 }
7cc84d24 2465
be3492ec
KS
2466 /* apply AC prediction if needed */
2467 if(s->ac_pred) {
2468 if(dc_pred_dir) { //left
2469 for(k = 1; k < 8; k++) {
2470 block[k << 3] = ac_val[k] * scale;
4b944659 2471 if(!v->pquantizer && block[k << 3])
be3492ec
KS
2472 block[k << 3] += (block[k << 3] < 0) ? -v->pq : v->pq;
2473 }
2474 } else { //top
2475 for(k = 1; k < 8; k++) {
2476 block[k] = ac_val[k + 8] * scale;
4b944659 2477 if(!v->pquantizer && block[k])
be3492ec
KS
2478 block[k] += (block[k] < 0) ? -v->pq : v->pq;
2479 }
2480 }
2481 i = 63;
2482 }
7cc84d24 2483 }
be3492ec 2484 s->block_last_index[n] = i;
7cc84d24 2485
be3492ec 2486 return 0;
7cc84d24 2487}
2488
3c275f6d 2489/** Decode intra block in intra frames - should be faster than decode_intra_block
be3492ec
KS
2490 * @param v VC1Context
2491 * @param block block to decode
2492 * @param coded are AC coeffs present or not
be3492ec 2493 * @param codingset set of VLC to decode data
7cc84d24 2494 */
3c275f6d 2495static int vc1_decode_i_block_adv(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset, int mquant)
e5540b3f 2496{
0d33db8a 2497 GetBitContext *gb = &v->s.gb;
7cc84d24 2498 MpegEncContext *s = &v->s;
be3492ec 2499 int dc_pred_dir = 0; /* Direction of the DC prediction used */
7cc84d24 2500 int run_diff, i;
b86216de 2501 int16_t *dc_val;
be3492ec
KS
2502 int16_t *ac_val, *ac_val2;
2503 int dcdiff;
f26c2ef5 2504 int a_avail = v->a_avail, c_avail = v->c_avail;
4a5343fa
KS
2505 int use_pred = s->ac_pred;
2506 int scale;
50af2fd3 2507 int q1, q2 = 0;
3c275f6d 2508 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
be3492ec 2509
be3492ec
KS
2510 /* Get DC differential */
2511 if (n < 4) {
2512 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2513 } else {
2514 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2515 }
2516 if (dcdiff < 0){
2517 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2518 return -1;
2519 }
2520 if (dcdiff)
2521 {
2522 if (dcdiff == 119 /* ESC index value */)
2523 {
2524 /* TODO: Optimize */
2525 if (mquant == 1) dcdiff = get_bits(gb, 10);
2526 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2527 else dcdiff = get_bits(gb, 8);
2528 }
2529 else
2530 {
2531 if (mquant == 1)
2532 dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
2533 else if (mquant == 2)
2534 dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
2535 }
2536 if (get_bits(gb, 1))
2537 dcdiff = -dcdiff;
2538 }
2539
2540 /* Prediction */
3c275f6d 2541 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
be3492ec
KS
2542 *dc_val = dcdiff;
2543
2544 /* Store the quantized DC coeff, used for prediction */
be3492ec
KS
2545 if (n < 4) {
2546 block[0] = dcdiff * s->y_dc_scale;
2547 } else {
2548 block[0] = dcdiff * s->c_dc_scale;
2549 }
2550 /* Skip ? */
2551 run_diff = 0;
2552 i = 0;
be3492ec
KS
2553
2554 //AC Decoding
2555 i = 1;
8da75fb2 2556
26bdc6bc 2557 /* check if AC is needed at all */
4a5343fa
KS
2558 if(!a_avail && !c_avail) use_pred = 0;
2559 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2560 ac_val2 = ac_val;
2561
0a45801f 2562 scale = mquant * 2 + v->halfpq;
4a5343fa
KS
2563
2564 if(dc_pred_dir) //left
2565 ac_val -= 16;
2566 else //top
2567 ac_val -= 16 * s->block_wrap[n];
2568
50af2fd3 2569 q1 = s->current_picture.qscale_table[mb_pos];
b956373b
KS
2570 if(dc_pred_dir && c_avail && mb_pos) q2 = s->current_picture.qscale_table[mb_pos - 1];
2571 if(!dc_pred_dir && a_avail && mb_pos >= s->mb_stride) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
b6661510
KS
2572 if(dc_pred_dir && n==1) q2 = q1;
2573 if(!dc_pred_dir && n==2) q2 = q1;
2574 if(n==3) q2 = q1;
50af2fd3 2575
4a5343fa 2576 if(coded) {
be3492ec
KS
2577 int last = 0, skip, value;
2578 const int8_t *zz_table;
be3492ec 2579 int k;
be3492ec 2580
3c275f6d
KS
2581 if(v->s.ac_pred) {
2582 if(!dc_pred_dir)
a5c14fca 2583 zz_table = ff_vc1_horizontal_zz;
3c275f6d 2584 else
a5c14fca 2585 zz_table = ff_vc1_vertical_zz;
3c275f6d 2586 } else
a5c14fca 2587 zz_table = ff_vc1_normal_zz;
be3492ec 2588
be3492ec
KS
2589 while (!last) {
2590 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2591 i += skip;
2592 if(i > 63)
2593 break;
2594 block[zz_table[i++]] = value;
e5540b3f 2595 }
7cc84d24 2596
be3492ec 2597 /* apply AC prediction if needed */
8f8d0e48 2598 if(use_pred) {
be3492ec 2599 /* scale predictors if needed*/
50af2fd3 2600 if(q2 && q1!=q2) {
25e9c7ef
KS
2601 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2602 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
be3492ec
KS
2603
2604 if(dc_pred_dir) { //left
2605 for(k = 1; k < 8; k++)
a5c14fca 2606 block[k << 3] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
be3492ec
KS
2607 } else { //top
2608 for(k = 1; k < 8; k++)
a5c14fca 2609 block[k] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
be3492ec
KS
2610 }
2611 } else {
2612 if(dc_pred_dir) { //left
2613 for(k = 1; k < 8; k++)
2614 block[k << 3] += ac_val[k];
2615 } else { //top
2616 for(k = 1; k < 8; k++)
2617 block[k] += ac_val[k + 8];
2618 }
2619 }
7cc84d24 2620 }
be3492ec
KS
2621 /* save AC coeffs for further prediction */
2622 for(k = 1; k < 8; k++) {
2623 ac_val2[k] = block[k << 3];
2624 ac_val2[k + 8] = block[k];
e5540b3f 2625 }
7cc84d24 2626
be3492ec
KS
2627 /* scale AC coeffs */
2628 for(k = 1; k < 64; k++)
2629 if(block[k]) {
2630 block[k] *= scale;
2631 if(!v->pquantizer)
2632 block[k] += (block[k] < 0) ? -mquant : mquant;
2633 }
7cc84d24 2634
8f8d0e48 2635 if(use_pred) i = 63;
4a5343fa
KS
2636 } else { // no AC coeffs
2637 int k;
8f8d0e48 2638
be3492ec
KS
2639 memset(ac_val2, 0, 16 * 2);
2640 if(dc_pred_dir) {//left
e4bf0302 2641 if(use_pred) {
be3492ec 2642 memcpy(ac_val2, ac_val, 8 * 2);
50af2fd3 2643 if(q2 && q1!=q2) {
25e9c7ef
KS
2644 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2645 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
e4bf0302 2646 for(k = 1; k < 8; k++)
a5c14fca 2647 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
e4bf0302
KS
2648 }
2649 }
be3492ec 2650 } else {//top
e4bf0302 2651 if(use_pred) {
be3492ec 2652 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
50af2fd3 2653 if(q2 && q1!=q2) {
25e9c7ef
KS
2654 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2655 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
e4bf0302 2656 for(k = 1; k < 8; k++)
a5c14fca 2657 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
e4bf0302
KS
2658 }
2659 }
be3492ec 2660 }
7cc84d24 2661
be3492ec 2662 /* apply AC prediction if needed */
8f8d0e48 2663 if(use_pred) {
be3492ec
KS
2664 if(dc_pred_dir) { //left
2665 for(k = 1; k < 8; k++) {
e4bf0302 2666 block[k << 3] = ac_val2[k] * scale;
4b944659 2667 if(!v->pquantizer && block[k << 3])
be3492ec
KS
2668 block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
2669 }
2670 } else { //top
2671 for(k = 1; k < 8; k++) {
e4bf0302 2672 block[k] = ac_val2[k + 8] * scale;
4b944659 2673 if(!v->pquantizer && block[k])
be3492ec
KS
2674 block[k] += (block[k] < 0) ? -mquant : mquant;
2675 }
2676 }
2677 i = 63;
7cc84d24 2678 }
2679 }
7cc84d24 2680 s->block_last_index[n] = i;
be3492ec 2681
e5540b3f 2682 return 0;
2683}
7cc84d24 2684
3c275f6d
KS
2685/** Decode intra block in inter frames - more generic version than vc1_decode_i_block
2686 * @param v VC1Context
2687 * @param block block to decode
2688 * @param coded are AC coeffs present or not
2689 * @param mquant block quantizer
2690 * @param codingset set of VLC to decode data
2ce151f8 2691 */
3c275f6d 2692static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n, int coded, int mquant, int codingset)
7cc84d24 2693{
3c275f6d 2694 GetBitContext *gb = &v->s.gb;
be3492ec 2695 MpegEncContext *s = &v->s;
3c275f6d
KS
2696 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2697 int run_diff, i;
b86216de 2698 int16_t *dc_val;
3c275f6d
KS
2699 int16_t *ac_val, *ac_val2;
2700 int dcdiff;
2701 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2702 int a_avail = v->a_avail, c_avail = v->c_avail;
2703 int use_pred = s->ac_pred;
2704 int scale;
2705 int q1, q2 = 0;
42cc17f9 2706
3c275f6d
KS
2707 /* XXX: Guard against dumb values of mquant */
2708 mquant = (mquant < 1) ? 0 : ( (mquant>31) ? 31 : mquant );
42cc17f9 2709
3c275f6d
KS
2710 /* Set DC scale - y and c use the same */
2711 s->y_dc_scale = s->y_dc_scale_table[mquant];
2712 s->c_dc_scale = s->c_dc_scale_table[mquant];
2713
2714 /* Get DC differential */
2715 if (n < 4) {
2716 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2717 } else {
2718 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2719 }
2720 if (dcdiff < 0){
2721 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2722 return -1;
2723 }
2724 if (dcdiff)
2725 {
2726 if (dcdiff == 119 /* ESC index value */)
2727 {
2728 /* TODO: Optimize */
2729 if (mquant == 1) dcdiff = get_bits(gb, 10);
2730 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2731 else dcdiff = get_bits(gb, 8);
2732 }
2733 else
2734 {
2735 if (mquant == 1)
2736 dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
2737 else if (mquant == 2)
2738 dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
2739 }
2740 if (get_bits(gb, 1))
2741 dcdiff = -dcdiff;
2742 }
2743
2744 /* Prediction */
2745 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
2746 *dc_val = dcdiff;
2747
2748 /* Store the quantized DC coeff, used for prediction */
2749
2750 if (n < 4) {
2751 block[0] = dcdiff * s->y_dc_scale;
2752 } else {
2753 block[0] = dcdiff * s->c_dc_scale;
2754 }
2755 /* Skip ? */
2756 run_diff = 0;
2757 i = 0;
2758
2759 //AC Decoding
2760 i = 1;
2761
2762 /* check if AC is needed at all and adjust direction if needed */
2763 if(!a_avail) dc_pred_dir = 1;
2764 if(!c_avail) dc_pred_dir = 0;
2765 if(!a_avail && !c_avail) use_pred = 0;
2766 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2767 ac_val2 = ac_val;
2768
2769 scale = mquant * 2 + v->halfpq;
2770
2771 if(dc_pred_dir) //left
2772 ac_val -= 16;
2773 else //top
2774 ac_val -= 16 * s->block_wrap[n];
2775
2776 q1 = s->current_picture.qscale_table[mb_pos];
6f3e4e17
RD
2777 if(dc_pred_dir && c_avail && mb_pos) q2 = s->current_picture.qscale_table[mb_pos - 1];
2778 if(!dc_pred_dir && a_avail && mb_pos >= s->mb_stride) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
b6661510
KS
2779 if(dc_pred_dir && n==1) q2 = q1;
2780 if(!dc_pred_dir && n==2) q2 = q1;
2781 if(n==3) q2 = q1;
3c275f6d
KS
2782
2783 if(coded) {
2784 int last = 0, skip, value;
2785 const int8_t *zz_table;
2786 int k;
2787
a5c14fca 2788 zz_table = ff_vc1_simple_progressive_8x8_zz;
3c275f6d
KS
2789
2790 while (!last) {
2791 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2792 i += skip;
2793 if(i > 63)
2794 break;
2795 block[zz_table[i++]] = value;
2796 }
2797
2798 /* apply AC prediction if needed */
2799 if(use_pred) {
2800 /* scale predictors if needed*/
2801 if(q2 && q1!=q2) {
25e9c7ef
KS
2802 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2803 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3c275f6d
KS
2804
2805 if(dc_pred_dir) { //left
2806 for(k = 1; k < 8; k++)
a5c14fca 2807 block[k << 3] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3c275f6d
KS
2808 } else { //top
2809 for(k = 1; k < 8; k++)
a5c14fca 2810 block[k] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3c275f6d
KS
2811 }
2812 } else {
2813 if(dc_pred_dir) { //left
2814 for(k = 1; k < 8; k++)
2815 block[k << 3] += ac_val[k];
2816 } else { //top
2817 for(k = 1; k < 8; k++)
2818 block[k] += ac_val[k + 8];
2819 }
2820 }
2821 }
2822 /* save AC coeffs for further prediction */
2823 for(k = 1; k < 8; k++) {
2824 ac_val2[k] = block[k << 3];
2825 ac_val2[k + 8] = block[k];
2826 }
2827
2828 /* scale AC coeffs */
2829 for(k = 1; k < 64; k++)
2830 if(block[k]) {
2831 block[k] *= scale;
2832 if(!v->pquantizer)
2833 block[k] += (block[k] < 0) ? -mquant : mquant;
2834 }
2835
2836 if(use_pred) i = 63;
2837 } else { // no AC coeffs
2838 int k;
2839
2840 memset(ac_val2, 0, 16 * 2);
2841 if(dc_pred_dir) {//left
2842 if(use_pred) {
2843 memcpy(ac_val2, ac_val, 8 * 2);
2844 if(q2 && q1!=q2) {
25e9c7ef
KS
2845 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2846 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3c275f6d 2847 for(k = 1; k < 8; k++)
a5c14fca 2848 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3c275f6d
KS
2849 }
2850 }
2851 } else {//top
2852 if(use_pred) {
2853 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2854 if(q2 && q1!=q2) {
25e9c7ef
KS
2855 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2856 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3c275f6d 2857 for(k = 1; k < 8; k++)
a5c14fca 2858 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3c275f6d
KS
2859 }
2860 }
2861 }
2862
2863 /* apply AC prediction if needed */
2864 if(use_pred) {
2865 if(dc_pred_dir) { //left
2866 for(k = 1; k < 8; k++) {
2867 block[k << 3] = ac_val2[k] * scale;
2868 if(!v->pquantizer && block[k << 3])
2869 block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
2870 }
2871 } else { //top
2872 for(k = 1; k < 8; k++) {
2873 block[k] = ac_val2[k + 8] * scale;
2874 if(!v->pquantizer && block[k])
2875 block[k] += (block[k] < 0) ? -mquant : mquant;
2876 }
2877 }
2878 i = 63;
2879 }
2880 }
2881 s->block_last_index[n] = i;
2882
2883 return 0;
2884}
2885
2886/** Decode P block
2887 */
2888static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n, int mquant, int ttmb, int first_block)
2889{
2890 MpegEncContext *s = &v->s;
2891 GetBitContext *gb = &s->gb;
2892 int i, j;
2893 int subblkpat = 0;
2894 int scale, off, idx, last, skip, value;
2895 int ttblk = ttmb & 7;
2896
2897 if(ttmb == -1) {
a5c14fca 2898 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)];
3c275f6d
KS
2899 }
2900 if(ttblk == TT_4X4) {
a5c14fca 2901 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
3c275f6d
KS
2902 }
2903 if((ttblk != TT_8X8 && ttblk != TT_4X4) && (v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))) {
2904 subblkpat = decode012(gb);
2905 if(subblkpat) subblkpat ^= 3; //swap decoded pattern bits
2906 if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) ttblk = TT_8X4;
2907 if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) ttblk = TT_4X8;
2908 }
2a2072fe 2909 scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
3c275f6d
KS
2910
2911 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
be3492ec 2912 if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
be3492ec 2913 subblkpat = 2 - (ttblk == TT_8X4_TOP);
e9f2396e 2914 ttblk = TT_8X4;
be3492ec
KS
2915 }
2916 if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
be3492ec 2917 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
e9f2396e 2918 ttblk = TT_4X8;
be3492ec 2919 }
be3492ec
KS
2920 switch(ttblk) {
2921 case TT_8X8:
2922 i = 0;
2923 last = 0;
2924 while (!last) {
2925 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2926 i += skip;
2927 if(i > 63)
2928 break;
a5c14fca 2929 idx = ff_vc1_simple_progressive_8x8_zz[i++];
be3492ec 2930 block[idx] = value * scale;
0a45801f
KS
2931 if(!v->pquantizer)
2932 block[idx] += (block[idx] < 0) ? -mquant : mquant;
7cc84d24 2933 }
5ce425f7 2934 s->dsp.vc1_inv_trans_8x8(block);
be3492ec
KS
2935 break;
2936 case TT_4X4:
2937 for(j = 0; j < 4; j++) {
2938 last = subblkpat & (1 << (3 - j));
2939 i = 0;
87dfe848 2940 off = (j & 1) * 4 + (j & 2) * 16;
be3492ec
KS
2941 while (!last) {
2942 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2943 i += skip;
2944 if(i > 15)
2945 break;
a5c14fca 2946 idx = ff_vc1_simple_progressive_4x4_zz[i++];
be3492ec 2947 block[idx + off] = value * scale;
0a45801f
KS
2948 if(!v->pquantizer)
2949 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
be3492ec 2950 }
87dfe848 2951 if(!(subblkpat & (1 << (3 - j))))
5ce425f7 2952 s->dsp.vc1_inv_trans_4x4(block, j);
be3492ec
KS
2953 }
2954 break;
2955 case TT_8X4:
2956 for(j = 0; j < 2; j++) {
2957 last = subblkpat & (1 << (1 - j));
2958 i = 0;
2959 off = j * 32;
2960 while (!last) {
2961 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2962 i += skip;
2963 if(i > 31)
2964 break;
3c275f6d 2965 if(v->profile < PROFILE_ADVANCED)
a5c14fca 2966 idx = ff_vc1_simple_progressive_8x4_zz[i++];
3c275f6d 2967 else
a5c14fca 2968 idx = ff_vc1_adv_progressive_8x4_zz[i++];
be3492ec 2969 block[idx + off] = value * scale;
0a45801f
KS
2970 if(!v->pquantizer)
2971 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
be3492ec 2972 }
87dfe848 2973 if(!(subblkpat & (1 << (1 - j))))
5ce425f7 2974 s->dsp.vc1_inv_trans_8x4(block, j);
be3492ec
KS
2975 }
2976 break;
2977 case TT_4X8:
2978 for(j = 0; j < 2; j++) {
2979 last = subblkpat & (1 << (1 - j));
2980 i = 0;
2981 off = j * 4;
2982 while (!last) {
2983 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2984 i += skip;
2985 if(i > 31)
2986 break;
3c275f6d 2987 if(v->profile < PROFILE_ADVANCED)
a5c14fca 2988 idx = ff_vc1_simple_progressive_4x8_zz[i++];
3c275f6d 2989 else
a5c14fca 2990 idx = ff_vc1_adv_progressive_4x8_zz[i++];
be3492ec 2991 block[idx + off] = value * scale;
0a45801f
KS
2992 if(!v->pquantizer)
2993 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
be3492ec 2994 }
87dfe848 2995 if(!(subblkpat & (1 << (1 - j))))
5ce425f7 2996 s->dsp.vc1_inv_trans_4x8(block, j);
8da75fb2 2997 }
be3492ec 2998 break;
7cc84d24 2999 }
3000 return 0;
3001}
3002
be3492ec 3003
8da75fb2 3004/** Decode one P-frame MB (in Simple/Main profile)
8da75fb2 3005 */
7e84f276 3006static int vc1_decode_p_mb(VC1Context *v)
7cc84d24 3007{
3008 MpegEncContext *s = &v->s;
3009 GetBitContext *gb = &s->gb;
87dfe848 3010 int i, j;
be3492ec 3011 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
7cc84d24 3012 int cbp; /* cbp decoding stuff */
7cc84d24 3013 int mqdiff, mquant; /* MB quantization */
8a66a390 3014 int ttmb = v->ttfrm; /* MB Transform type */
7cc84d24 3015 int status;
7cc84d24 3016
3017 static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
3018 offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3019 int mb_has_coeffs = 1; /* last_flag */
3020 int dmv_x, dmv_y; /* Differential MV components */
3021 int index, index1; /* LUT indices */
3022 int val, sign; /* temp values */
be3492ec
KS
3023 int first_block = 1;
3024 int dst_idx, off;
87dfe848 3025 int skipped, fourmv;
7cc84d24 3026
8da75fb2 3027 mquant = v->pq; /* Loosy initialization */
3028
87dfe848
KS
3029 if (v->mv_type_is_raw)
3030 fourmv = get_bits1(gb);
3031 else
3032 fourmv = v->mv_type_mb_plane[mb_pos];
3033 if (v->skip_is_raw)
3034 skipped = get_bits1(gb);
3035 else
0f7344aa 3036 skipped = v->s.mbskip_table[mb_pos];
87dfe848 3037
e4bf0302
KS
3038 s->dsp.clear_blocks(s->block[0]);
3039
87dfe848 3040 if (!fourmv) /* 1MV mode */
7cc84d24 3041 {
87dfe848 3042 if (!skipped)
7cc84d24 3043 {
3044 GET_MVDATA(dmv_x, dmv_y);
42cc17f9 3045
1dc1ce64
KS
3046 if (s->mb_intra) {
3047 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
3048 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
3049 }
be3492ec 3050 s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
e4bf0302 3051 vc1_pred_mv(s, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
be3492ec 3052
8da75fb2 3053 /* FIXME Set DC val for inter block ? */
7cc84d24 3054 if (s->mb_intra && !mb_has_coeffs)
3055 {
3056 GET_MQUANT();
3057 s->ac_pred = get_bits(gb, 1);
8da75fb2 3058 cbp = 0;
7cc84d24 3059 }
3060 else if (mb_has_coeffs)
3061 {
3062 if (s->mb_intra) s->ac_pred = get_bits(gb, 1);
10b9c374 3063 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
7cc84d24 3064 GET_MQUANT();
3065 }
3066 else
3067 {
3068 mquant = v->pq;
8da75fb2 3069 cbp = 0;
7cc84d24 3070 }
be3492ec 3071 s->current_picture.qscale_table[mb_pos] = mquant;
7cc84d24 3072
be3492ec 3073 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
a5c14fca 3074 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
be3492ec 3075 VC1_TTMB_VLC_BITS, 2);
5df68893 3076 if(!s->mb_intra) vc1_mc_1mv(v, 0);
be3492ec 3077 dst_idx = 0;
7cc84d24 3078 for (i=0; i<6; i++)
3079 {
be3492ec
KS
3080 s->dc_val[0][s->block_index[i]] = 0;
3081 dst_idx += i >> 2;
7cc84d24 3082 val = ((cbp >> (5 - i)) & 1);
be3492ec 3083 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
e4bf0302 3084 v->mb_type[0][s->block_index[i]] = s->mb_intra;
be3492ec 3085 if(s->mb_intra) {
f26c2ef5
KS
3086 /* check if prediction blocks A and C are available */
3087 v->a_avail = v->c_avail = 0;
d2779ecd 3088 if(i == 2 || i == 3 || !s->first_slice_line)
e4bf0302
KS
3089 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3090 if(i == 1 || i == 3 || s->mb_x)
3091 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
f26c2ef5 3092
7e84f276 3093 vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
138712fe 3094 if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
5ce425f7 3095 s->dsp.vc1_inv_trans_8x8(s->block[i]);
ffb9a8b1 3096 if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
7e84f276 3097 for(j = 0; j < 64; j++) s->block[i][j] += 128;
9d1f80f2 3098 if(!v->res_fasttx && v->res_x8) for(j = 0; j < 64; j++) s->block[i][j] += 16;
7e84f276 3099 s->dsp.put_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
87dfe848 3100 if(v->pq >= 9 && v->overlap) {
87dfe848 3101 if(v->c_avail)
61f5b14a 3102 s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
b6fa8993 3103 if(v->a_avail)
61f5b14a 3104 s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
87dfe848 3105 }
be3492ec 3106 } else if(val) {
7e84f276 3107 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block);
be3492ec
KS
3108 if(!v->ttmbf && ttmb < 8) ttmb = -1;
3109 first_block = 0;
138712fe
KS
3110 if((i<4) || !(s->flags & CODEC_FLAG_GRAY))
3111 s->dsp.add_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
7cc84d24 3112 }
7cc84d24 3113 }
3114 }
3115 else //Skipped
3116 {
be3492ec 3117 s->mb_intra = 0;
92ad0d9d
KS
3118 for(i = 0; i < 6; i++) {
3119 v->mb_type[0][s->block_index[i]] = 0;
3120 s->dc_val[0][s->block_index[i]] = 0;
3121 }
be3492ec 3122 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
e4bf0302
KS
3123 s->current_picture.qscale_table[mb_pos] = 0;
3124 vc1_pred_mv(s, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);
5df68893 3125 vc1_mc_1mv(v, 0);
7cc84d24 3126 return 0;
3127 }
3128 } //1MV mode
3129 else //4MV mode
e4bf0302 3130 {
87dfe848 3131 if (!skipped /* unskipped MB */)
7cc84d24 3132 {
e4bf0302
KS
3133 int intra_count = 0, coded_inter = 0;
3134 int is_intra[6], is_coded[6];
7cc84d24 3135 /* Get CBPCY */
10b9c374 3136 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
e4bf0302 3137 for (i=0; i<6; i++)
7cc84d24 3138 {
3139 val = ((cbp >> (5 - i)) & 1);
e4bf0302
KS
3140 s->dc_val[0][s->block_index[i]] = 0;
3141 s->mb_intra = 0;
3142 if(i < 4) {
3143 dmv_x = dmv_y = 0;
3144 s->mb_intra = 0;
3145 mb_has_coeffs = 0;
3146 if(val) {
3147 GET_MVDATA(dmv_x, dmv_y);
3148 }
3149 vc1_pred_mv(s, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);
3150 if(!s->mb_intra) vc1_mc_4mv_luma(v, i);
3151 intra_count += s->mb_intra;
3152 is_intra[i] = s->mb_intra;
3153 is_coded[i] = mb_has_coeffs;
3154 }
3155 if(i&4){
3156 is_intra[i] = (intra_count >= 3);
3157 is_coded[i] = val;
7cc84d24 3158 }
e4bf0302
KS
3159 if(i == 4) vc1_mc_4mv_chroma(v);
3160 v->mb_type[0][s->block_index[i]] = is_intra[i];
3161 if(!coded_inter) coded_inter = !is_intra[i] & is_coded[i];
be3492ec 3162 }
fdb59832 3163 // if there are no coded blocks then don't do anything more
c39e3c6f 3164 if(!intra_count && !coded_inter) return 0;
e4bf0302
KS
3165 dst_idx = 0;
3166 GET_MQUANT();
3167 s->current_picture.qscale_table[mb_pos] = mquant;
3168 /* test if block is intra and has pred */
3169 {
3170 int intrapred = 0;
3171 for(i=0; i<6; i++)
3172 if(is_intra[i]) {
d2779ecd
KS
3173 if(((!s->first_slice_line || (i==2 || i==3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
3174 || ((s->mb_x || (i==1 || i==3)) && v->mb_type[0][s->block_index[i] - 1])) {
e4bf0302
KS
3175 intrapred = 1;
3176 break;
3177 }
3178 }
3179 if(intrapred)s->ac_pred = get_bits(gb, 1);
3180 else s->ac_pred = 0;
3181 }
3182 if (!v->ttmbf && coded_inter)
a5c14fca 3183 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
e4bf0302
KS
3184 for (i=0; i<6; i++)
3185 {
3186 dst_idx += i >> 2;
3187 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3188 s->mb_intra = is_intra[i];
3189 if (is_intra[i]) {
3190 /* check if prediction blocks A and C are available */
3191 v->a_avail = v->c_avail = 0;
d2779ecd 3192 if(i == 2 || i == 3 || !s->first_slice_line)
e4bf0302
KS
3193 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3194 if(i == 1 || i == 3 || s->mb_x)
3195 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
8da75fb2 3196
e4bf0302 3197 vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant, (i&4)?v->codingset2:v->codingset);
138712fe 3198 if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
5ce425f7 3199 s->dsp.vc1_inv_trans_8x8(s->block[i]);
ffb9a8b1 3200 if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
7e84f276 3201 for(j = 0; j < 64; j++) s->block[i][j] += 128;
9d1f80f2 3202 if(!v->res_fasttx && v->res_x8) for(j = 0; j < 64; j++) s->block[i][j] += 16;
ef6cc8ce 3203 s->dsp.put_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
e4bf0302 3204 if(v->pq >= 9 && v->overlap) {
e4bf0302 3205 if(v->c_avail)
61f5b14a 3206 s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
b6fa8993 3207 if(v->a_avail)
61f5b14a 3208 s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
be3492ec 3209 }
e4bf0302
KS
3210 } else if(is_coded[i]) {
3211 status = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block);
3212 if(!v->ttmbf && ttmb < 8) ttmb = -1;
3213 first_block = 0;
138712fe
KS
3214 if((i<4) || !(s->flags & CODEC_FLAG_GRAY))
3215 s->dsp.add_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
be3492ec 3216 }
7cc84d24 3217 }
3218 return status;
3219 }
3220 else //Skipped MB
3221 {
ef6cc8ce 3222 s->mb_intra = 0;
c39e3c6f 3223 s->current_picture.qscale_table[mb_pos] = 0;
92ad0d9d
KS
3224 for (i=0; i<6; i++) {
3225 v->mb_type[0][s->block_index[i]] = 0;
3226 s->dc_val[0][s->block_index[i]] = 0;
3227 }
7cc84d24 3228 for (i=0; i<4; i++)
3229 {
e4bf0302
KS
3230 vc1_pred_mv(s, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0]);
3231 vc1_mc_4mv_luma(v, i);
7cc84d24 3232 }
e4bf0302 3233 vc1_mc_4mv_chroma(v);
e4bf0302 3234 s->current_picture.qscale_table[mb_pos] = 0;
7cc84d24 3235 return 0;
3236 }
3237 }
42cc17f9 3238
7cc84d24 3239 /* Should never happen */
3240 return -1;
3241}
3242
5df68893
KS
3243/** Decode one B-frame MB (in Main profile)
3244 */
3245static void vc1_decode_b_mb(VC1Context *v)
3246{
3247 MpegEncContext *s = &v->s;
3248 GetBitContext *gb = &s->gb;
3249 int i, j;
3250 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
fb2d9140 3251 int cbp = 0; /* cbp decoding stuff */
5df68893
KS
3252 int mqdiff, mquant; /* MB quantization */
3253 int ttmb = v->ttfrm; /* MB Transform type */
3254
3255 static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
3256 offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3257 int mb_has_coeffs = 0; /* last_flag */
3258 int index, index1; /* LUT indices */
3259 int val, sign; /* temp values */
3260 int first_block = 1;
3261 int dst_idx, off;
3262 int skipped, direct;
3263 int dmv_x[2], dmv_y[2];
d1d28ddf 3264 int bmvtype = BMV_TYPE_BACKWARD;
5df68893
KS
3265
3266 mquant = v->pq; /* Loosy initialization */
3267 s->mb_intra = 0;
3268
3269 if (v->dmb_is_raw)
3270 direct = get_bits1(gb);
3271 else
3272 direct = v->direct_mb_plane[mb_pos];
3273 if (v->skip_is_raw)
3274 skipped = get_bits1(gb);
3275 else
3276 skipped = v->s.mbskip_table[mb_pos];
3277
3278 s->dsp.clear_blocks(s->block[0]);
3279 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
3280 for(i = 0; i < 6; i++) {
3281 v->mb_type[0][s->block_index[i]] = 0;
3282 s->dc_val[0][s->block_index[i]] = 0;
3283 }
3284 s->current_picture.qscale_table[mb_pos] = 0;
3285
3286 if (!direct) {
3287 if (!skipped) {
3288 GET_MVDATA(dmv_x[0], dmv_y[0]);
fb2d9140
KS
3289 dmv_x[1] = dmv_x[0];
3290 dmv_y[1] = dmv_y[0];
5df68893
KS
3291 }
3292 if(skipped || !s->mb_intra) {
3293 bmvtype = decode012(gb);
3294 switch(bmvtype) {
3295 case 0:
3296 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
3297 break;
3298 case 1:
3299 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
3300 break;
3301 case 2:
3302 bmvtype = BMV_TYPE_INTERPOLATED;
162f412d 3303 dmv_x[0] = dmv_y[0] = 0;
5df68893
KS
3304 }
3305 }
3306 }
1dc1ce64
KS
3307 for(i = 0; i < 6; i++)
3308 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3309
5df68893 3310 if (skipped) {
1dc1ce64
KS
3311 if(direct) bmvtype = BMV_TYPE_INTERPOLATED;
3312 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
5df68893
KS
3313 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3314 return;
3315 }
3316 if (direct) {
3317 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3318 GET_MQUANT();
fb2d9140
KS
3319 s->mb_intra = 0;
3320 mb_has_coeffs = 0;
5df68893 3321 s->current_picture.qscale_table[mb_pos] = mquant;
fb2d9140 3322 if(!v->ttmbf)
a5c14fca 3323 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1dc1ce64
KS
3324 dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
3325 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
5df68893
KS
3326 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3327 } else {
3328 if(!mb_has_coeffs && !s->mb_intra) {
3329 /* no coded blocks - effectively skipped */
1dc1ce64 3330 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
5df68893
KS
3331 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3332 return;
3333 }
3334 if(s->mb_intra && !mb_has_coeffs) {
3335 GET_MQUANT();
3336 s->current_picture.qscale_table[mb_pos] = mquant;
3337 s->ac_pred = get_bits1(gb);
3338 cbp = 0;
1dc1ce64 3339 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
5df68893
KS
3340 } else {
3341 if(bmvtype == BMV_TYPE_INTERPOLATED) {
162f412d 3342 GET_MVDATA(dmv_x[0], dmv_y[0]);
5df68893
KS
3343 if(!mb_has_coeffs) {
3344 /* interpolated skipped block */
1dc1ce64 3345 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
5df68893
KS
3346 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3347 return;
3348 }
3349 }
1dc1ce64
KS
3350 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
3351 if(!s->mb_intra) {
5df68893 3352 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1dc1ce64 3353 }
5df68893
KS
3354 if(s->mb_intra)
3355 s->ac_pred = get_bits1(gb);
3356 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3357 GET_MQUANT();
3358 s->current_picture.qscale_table[mb_pos] = mquant;
3359 if(!v->ttmbf && !s->mb_intra && mb_has_coeffs)
a5c14fca 3360 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
5df68893
KS
3361 }
3362 }
3363 dst_idx = 0;
3364 for (i=0; i<6; i++)
3365 {
3366 s->dc_val[0][s->block_index[i]] = 0;
3367 dst_idx += i >> 2;
3368 val = ((cbp >> (5 - i)) & 1);
3369 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3370 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3371 if(s->mb_intra) {
3372 /* check if prediction blocks A and C are available */
3373 v->a_avail = v->c_avail = 0;
3374 if(i == 2 || i == 3 || !s->first_slice_line)
3375 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3376 if(i == 1 || i == 3 || s->mb_x)
3377 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3378
3379 vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
138712fe 3380 if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
5ce425f7 3381 s->dsp.vc1_inv_trans_8x8(s->block[i]);
ffb9a8b1 3382 if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
5df68893
KS
3383 for(j = 0; j < 64; j++) s->block[i][j] += 128;
3384 s->dsp.put_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
5df68893
KS
3385 } else if(val) {
3386 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block);
3387 if(!v->ttmbf && ttmb < 8) ttmb = -1;
3388 first_block = 0;
138712fe
KS
3389 if((i<4) || !(s->flags & CODEC_FLAG_GRAY))
3390 s->dsp.add_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
5df68893
KS
3391 }
3392 }
3393}
3394
be3492ec 3395/** Decode blocks of I-frame
8da75fb2 3396 */
be3492ec 3397static void vc1_decode_i_blocks(VC1Context *v)
7cc84d24 3398{
65c7bb9e 3399 int k, j;
8da75fb2 3400 MpegEncContext *s = &v->s;
be3492ec
KS
3401 int cbp, val;
3402 uint8_t *coded_val;
3403 int mb_pos;
42cc17f9 3404
be3492ec
KS
3405 /* select codingmode used for VLC tables selection */
3406 switch(v->y_ac_table_index){
3407 case 0: