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