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