Statistics
| Branch: | Revision:

ffmpeg / libavcodec / vc1dec.c @ 713f4904

History | View | Annotate | Download (124 KB)

1
/*
2
 * VC-1 and WMV3 decoder
3
 * Copyright (c) 2006-2007 Konstantin Shishkov
4
 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
5
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22

    
23
/**
24
 * @file
25
 * VC-1 and WMV3 decoder
26
 *
27
 */
28
#include "internal.h"
29
#include "dsputil.h"
30
#include "avcodec.h"
31
#include "mpegvideo.h"
32
#include "h263.h"
33
#include "vc1.h"
34
#include "vc1data.h"
35
#include "vc1acdata.h"
36
#include "msmpeg4data.h"
37
#include "unary.h"
38
#include "simple_idct.h"
39
#include "mathops.h"
40
#include "vdpau_internal.h"
41

    
42
#undef NDEBUG
43
#include <assert.h>
44

    
45
#define MB_INTRA_VLC_BITS 9
46
#define DC_VLC_BITS 9
47
#define AC_VLC_BITS 9
48
static const uint16_t table_mb_intra[64][2];
49

    
50

    
51
static const uint16_t vlc_offs[] = {
52
       0,   520,   552,   616,  1128,  1160, 1224, 1740, 1772, 1836, 1900, 2436,
53
    2986,  3050,  3610,  4154,  4218,  4746, 5326, 5390, 5902, 6554, 7658, 8620,
54
    9262, 10202, 10756, 11310, 12228, 15078
55
};
56

    
57
/**
58
 * Init VC-1 specific tables and VC1Context members
59
 * @param v The VC1Context to initialize
60
 * @return Status
61
 */
62
static int vc1_init_common(VC1Context *v)
63
{
64
    static int done = 0;
65
    int i = 0;
66
    static VLC_TYPE vlc_table[15078][2];
67

    
68
    v->hrd_rate = v->hrd_buffer = NULL;
69

    
70
    /* VLC tables */
71
    if(!done)
72
    {
73
        INIT_VLC_STATIC(&ff_vc1_bfraction_vlc, VC1_BFRACTION_VLC_BITS, 23,
74
                 ff_vc1_bfraction_bits, 1, 1,
75
                 ff_vc1_bfraction_codes, 1, 1, 1 << VC1_BFRACTION_VLC_BITS);
76
        INIT_VLC_STATIC(&ff_vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4,
77
                 ff_vc1_norm2_bits, 1, 1,
78
                 ff_vc1_norm2_codes, 1, 1, 1 << VC1_NORM2_VLC_BITS);
79
        INIT_VLC_STATIC(&ff_vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 64,
80
                 ff_vc1_norm6_bits, 1, 1,
81
                 ff_vc1_norm6_codes, 2, 2, 556);
82
        INIT_VLC_STATIC(&ff_vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7,
83
                 ff_vc1_imode_bits, 1, 1,
84
                 ff_vc1_imode_codes, 1, 1, 1 << VC1_IMODE_VLC_BITS);
85
        for (i=0; i<3; i++)
86
        {
87
            ff_vc1_ttmb_vlc[i].table = &vlc_table[vlc_offs[i*3+0]];
88
            ff_vc1_ttmb_vlc[i].table_allocated = vlc_offs[i*3+1] - vlc_offs[i*3+0];
89
            init_vlc(&ff_vc1_ttmb_vlc[i], VC1_TTMB_VLC_BITS, 16,
90
                     ff_vc1_ttmb_bits[i], 1, 1,
91
                     ff_vc1_ttmb_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
92
            ff_vc1_ttblk_vlc[i].table = &vlc_table[vlc_offs[i*3+1]];
93
            ff_vc1_ttblk_vlc[i].table_allocated = vlc_offs[i*3+2] - vlc_offs[i*3+1];
94
            init_vlc(&ff_vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 8,
95
                     ff_vc1_ttblk_bits[i], 1, 1,
96
                     ff_vc1_ttblk_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
97
            ff_vc1_subblkpat_vlc[i].table = &vlc_table[vlc_offs[i*3+2]];
98
            ff_vc1_subblkpat_vlc[i].table_allocated = vlc_offs[i*3+3] - vlc_offs[i*3+2];
99
            init_vlc(&ff_vc1_subblkpat_vlc[i], VC1_SUBBLKPAT_VLC_BITS, 15,
100
                     ff_vc1_subblkpat_bits[i], 1, 1,
101
                     ff_vc1_subblkpat_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
102
        }
103
        for(i=0; i<4; i++)
104
        {
105
            ff_vc1_4mv_block_pattern_vlc[i].table = &vlc_table[vlc_offs[i*3+9]];
106
            ff_vc1_4mv_block_pattern_vlc[i].table_allocated = vlc_offs[i*3+10] - vlc_offs[i*3+9];
107
            init_vlc(&ff_vc1_4mv_block_pattern_vlc[i], VC1_4MV_BLOCK_PATTERN_VLC_BITS, 16,
108
                     ff_vc1_4mv_block_pattern_bits[i], 1, 1,
109
                     ff_vc1_4mv_block_pattern_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
110
            ff_vc1_cbpcy_p_vlc[i].table = &vlc_table[vlc_offs[i*3+10]];
111
            ff_vc1_cbpcy_p_vlc[i].table_allocated = vlc_offs[i*3+11] - vlc_offs[i*3+10];
112
            init_vlc(&ff_vc1_cbpcy_p_vlc[i], VC1_CBPCY_P_VLC_BITS, 64,
113
                     ff_vc1_cbpcy_p_bits[i], 1, 1,
114
                     ff_vc1_cbpcy_p_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
115
            ff_vc1_mv_diff_vlc[i].table = &vlc_table[vlc_offs[i*3+11]];
116
            ff_vc1_mv_diff_vlc[i].table_allocated = vlc_offs[i*3+12] - vlc_offs[i*3+11];
117
            init_vlc(&ff_vc1_mv_diff_vlc[i], VC1_MV_DIFF_VLC_BITS, 73,
118
                     ff_vc1_mv_diff_bits[i], 1, 1,
119
                     ff_vc1_mv_diff_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
120
        }
121
        for(i=0; i<8; i++){
122
            ff_vc1_ac_coeff_table[i].table = &vlc_table[vlc_offs[i+21]];
123
            ff_vc1_ac_coeff_table[i].table_allocated = vlc_offs[i+22] - vlc_offs[i+21];
124
            init_vlc(&ff_vc1_ac_coeff_table[i], AC_VLC_BITS, vc1_ac_sizes[i],
125
                     &vc1_ac_tables[i][0][1], 8, 4,
126
                     &vc1_ac_tables[i][0][0], 8, 4, INIT_VLC_USE_NEW_STATIC);
127
        }
128
        done = 1;
129
    }
130

    
131
    /* Other defaults */
132
    v->pq = -1;
133
    v->mvrange = 0; /* 7.1.1.18, p80 */
134

    
135
    return 0;
136
}
137

    
138
/***********************************************************************/
139
/**
140
 * @defgroup vc1bitplane VC-1 Bitplane decoding
141
 * @see 8.7, p56
142
 * @{
143
 */
144

    
145
/**
146
 * Imode types
147
 * @{
148
 */
149
enum Imode {
150
    IMODE_RAW,
151
    IMODE_NORM2,
152
    IMODE_DIFF2,
153
    IMODE_NORM6,
154
    IMODE_DIFF6,
155
    IMODE_ROWSKIP,
156
    IMODE_COLSKIP
157
};
158
/** @} */ //imode defines
159

    
160

    
161
/** @} */ //Bitplane group
162

    
163
static void vc1_loop_filter_iblk(VC1Context *v, int pq)
164
{
165
    MpegEncContext *s = &v->s;
166
    int j;
167
    if (!s->first_slice_line) {
168
        v->vc1dsp.vc1_v_loop_filter16(s->dest[0], s->linesize, pq);
169
        if (s->mb_x)
170
            v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16*s->linesize, s->linesize, pq);
171
        v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16*s->linesize+8, s->linesize, pq);
172
        for(j = 0; j < 2; j++){
173
            v->vc1dsp.vc1_v_loop_filter8(s->dest[j+1], s->uvlinesize, pq);
174
            if (s->mb_x)
175
                v->vc1dsp.vc1_h_loop_filter8(s->dest[j+1]-8*s->uvlinesize, s->uvlinesize, pq);
176
        }
177
    }
178
    v->vc1dsp.vc1_v_loop_filter16(s->dest[0] + 8*s->linesize, s->linesize, pq);
179

    
180
    if (s->mb_y == s->mb_height-1) {
181
        if (s->mb_x) {
182
            v->vc1dsp.vc1_h_loop_filter16(s->dest[0], s->linesize, pq);
183
            v->vc1dsp.vc1_h_loop_filter8(s->dest[1], s->uvlinesize, pq);
184
            v->vc1dsp.vc1_h_loop_filter8(s->dest[2], s->uvlinesize, pq);
185
        }
186
        v->vc1dsp.vc1_h_loop_filter16(s->dest[0] + 8, s->linesize, pq);
187
    }
188
}
189

    
190
/** Do motion compensation over 1 macroblock
191
 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
192
 */
193
static void vc1_mc_1mv(VC1Context *v, int dir)
194
{
195
    MpegEncContext *s = &v->s;
196
    DSPContext *dsp = &v->s.dsp;
197
    uint8_t *srcY, *srcU, *srcV;
198
    int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
199

    
200
    if(!v->s.last_picture.data[0])return;
201

    
202
    mx = s->mv[dir][0][0];
203
    my = s->mv[dir][0][1];
204

    
205
    // store motion vectors for further use in B frames
206
    if(s->pict_type == FF_P_TYPE) {
207
        s->current_picture.motion_val[1][s->block_index[0]][0] = mx;
208
        s->current_picture.motion_val[1][s->block_index[0]][1] = my;
209
    }
210
    uvmx = (mx + ((mx & 3) == 3)) >> 1;
211
    uvmy = (my + ((my & 3) == 3)) >> 1;
212
    if(v->fastuvmc) {
213
        uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
214
        uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
215
    }
216
    if(!dir) {
217
        srcY = s->last_picture.data[0];
218
        srcU = s->last_picture.data[1];
219
        srcV = s->last_picture.data[2];
220
    } else {
221
        srcY = s->next_picture.data[0];
222
        srcU = s->next_picture.data[1];
223
        srcV = s->next_picture.data[2];
224
    }
225

    
226
    src_x = s->mb_x * 16 + (mx >> 2);
227
    src_y = s->mb_y * 16 + (my >> 2);
228
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
229
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
230

    
231
    if(v->profile != PROFILE_ADVANCED){
232
        src_x   = av_clip(  src_x, -16, s->mb_width  * 16);
233
        src_y   = av_clip(  src_y, -16, s->mb_height * 16);
234
        uvsrc_x = av_clip(uvsrc_x,  -8, s->mb_width  *  8);
235
        uvsrc_y = av_clip(uvsrc_y,  -8, s->mb_height *  8);
236
    }else{
237
        src_x   = av_clip(  src_x, -17, s->avctx->coded_width);
238
        src_y   = av_clip(  src_y, -18, s->avctx->coded_height + 1);
239
        uvsrc_x = av_clip(uvsrc_x,  -8, s->avctx->coded_width  >> 1);
240
        uvsrc_y = av_clip(uvsrc_y,  -8, s->avctx->coded_height >> 1);
241
    }
242

    
243
    srcY += src_y * s->linesize + src_x;
244
    srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
245
    srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
246

    
247
    /* for grayscale we should not try to read from unknown area */
248
    if(s->flags & CODEC_FLAG_GRAY) {
249
        srcU = s->edge_emu_buffer + 18 * s->linesize;
250
        srcV = s->edge_emu_buffer + 18 * s->linesize;
251
    }
252

    
253
    if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
254
       || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel*3
255
       || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 16 - s->mspel*3){
256
        uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
257

    
258
        srcY -= s->mspel * (1 + s->linesize);
259
        s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2,
260
                            src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
261
        srcY = s->edge_emu_buffer;
262
        s->dsp.emulated_edge_mc(uvbuf     , srcU, s->uvlinesize, 8+1, 8+1,
263
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
264
        s->dsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
265
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
266
        srcU = uvbuf;
267
        srcV = uvbuf + 16;
268
        /* if we deal with range reduction we need to scale source blocks */
269
        if(v->rangeredfrm) {
270
            int i, j;
271
            uint8_t *src, *src2;
272

    
273
            src = srcY;
274
            for(j = 0; j < 17 + s->mspel*2; j++) {
275
                for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
276
                src += s->linesize;
277
            }
278
            src = srcU; src2 = srcV;
279
            for(j = 0; j < 9; j++) {
280
                for(i = 0; i < 9; i++) {
281
                    src[i] = ((src[i] - 128) >> 1) + 128;
282
                    src2[i] = ((src2[i] - 128) >> 1) + 128;
283
                }
284
                src += s->uvlinesize;
285
                src2 += s->uvlinesize;
286
            }
287
        }
288
        /* if we deal with intensity compensation we need to scale source blocks */
289
        if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
290
            int i, j;
291
            uint8_t *src, *src2;
292

    
293
            src = srcY;
294
            for(j = 0; j < 17 + s->mspel*2; j++) {
295
                for(i = 0; i < 17 + s->mspel*2; i++) src[i] = v->luty[src[i]];
296
                src += s->linesize;
297
            }
298
            src = srcU; src2 = srcV;
299
            for(j = 0; j < 9; j++) {
300
                for(i = 0; i < 9; i++) {
301
                    src[i] = v->lutuv[src[i]];
302
                    src2[i] = v->lutuv[src2[i]];
303
                }
304
                src += s->uvlinesize;
305
                src2 += s->uvlinesize;
306
            }
307
        }
308
        srcY += s->mspel * (1 + s->linesize);
309
    }
310

    
311
    if(s->mspel) {
312
        dxy = ((my & 3) << 2) | (mx & 3);
313
        v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0]    , srcY    , s->linesize, v->rnd);
314
        v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
315
        srcY += s->linesize * 8;
316
        v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize    , srcY    , s->linesize, v->rnd);
317
        v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
318
    } else { // hpel mc - always used for luma
319
        dxy = (my & 2) | ((mx & 2) >> 1);
320

    
321
        if(!v->rnd)
322
            dsp->put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
323
        else
324
            dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
325
    }
326

    
327
    if(s->flags & CODEC_FLAG_GRAY) return;
328
    /* Chroma MC always uses qpel bilinear */
329
    uvmx = (uvmx&3)<<1;
330
    uvmy = (uvmy&3)<<1;
331
    if(!v->rnd){
332
        dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
333
        dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
334
    }else{
335
        v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
336
        v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
337
    }
338
}
339

    
340
/** Do motion compensation for 4-MV macroblock - luminance block
341
 */
342
static void vc1_mc_4mv_luma(VC1Context *v, int n)
343
{
344
    MpegEncContext *s = &v->s;
345
    DSPContext *dsp = &v->s.dsp;
346
    uint8_t *srcY;
347
    int dxy, mx, my, src_x, src_y;
348
    int off;
349

    
350
    if(!v->s.last_picture.data[0])return;
351
    mx = s->mv[0][n][0];
352
    my = s->mv[0][n][1];
353
    srcY = s->last_picture.data[0];
354

    
355
    off = s->linesize * 4 * (n&2) + (n&1) * 8;
356

    
357
    src_x = s->mb_x * 16 + (n&1) * 8 + (mx >> 2);
358
    src_y = s->mb_y * 16 + (n&2) * 4 + (my >> 2);
359

    
360
    if(v->profile != PROFILE_ADVANCED){
361
        src_x   = av_clip(  src_x, -16, s->mb_width  * 16);
362
        src_y   = av_clip(  src_y, -16, s->mb_height * 16);
363
    }else{
364
        src_x   = av_clip(  src_x, -17, s->avctx->coded_width);
365
        src_y   = av_clip(  src_y, -18, s->avctx->coded_height + 1);
366
    }
367

    
368
    srcY += src_y * s->linesize + src_x;
369

    
370
    if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
371
       || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 8 - s->mspel*2
372
       || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 8 - s->mspel*2){
373
        srcY -= s->mspel * (1 + s->linesize);
374
        s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 9+s->mspel*2, 9+s->mspel*2,
375
                            src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
376
        srcY = s->edge_emu_buffer;
377
        /* if we deal with range reduction we need to scale source blocks */
378
        if(v->rangeredfrm) {
379
            int i, j;
380
            uint8_t *src;
381

    
382
            src = srcY;
383
            for(j = 0; j < 9 + s->mspel*2; j++) {
384
                for(i = 0; i < 9 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
385
                src += s->linesize;
386
            }
387
        }
388
        /* if we deal with intensity compensation we need to scale source blocks */
389
        if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
390
            int i, j;
391
            uint8_t *src;
392

    
393
            src = srcY;
394
            for(j = 0; j < 9 + s->mspel*2; j++) {
395
                for(i = 0; i < 9 + s->mspel*2; i++) src[i] = v->luty[src[i]];
396
                src += s->linesize;
397
            }
398
        }
399
        srcY += s->mspel * (1 + s->linesize);
400
    }
401

    
402
    if(s->mspel) {
403
        dxy = ((my & 3) << 2) | (mx & 3);
404
        v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, v->rnd);
405
    } else { // hpel mc - always used for luma
406
        dxy = (my & 2) | ((mx & 2) >> 1);
407
        if(!v->rnd)
408
            dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
409
        else
410
            dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
411
    }
412
}
413

    
414
static inline int median4(int a, int b, int c, int d)
415
{
416
    if(a < b) {
417
        if(c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
418
        else      return (FFMIN(b, c) + FFMAX(a, d)) / 2;
419
    } else {
420
        if(c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
421
        else      return (FFMIN(a, c) + FFMAX(b, d)) / 2;
422
    }
423
}
424

    
425

    
426
/** Do motion compensation for 4-MV macroblock - both chroma blocks
427
 */
428
static void vc1_mc_4mv_chroma(VC1Context *v)
429
{
430
    MpegEncContext *s = &v->s;
431
    DSPContext *dsp = &v->s.dsp;
432
    uint8_t *srcU, *srcV;
433
    int uvmx, uvmy, uvsrc_x, uvsrc_y;
434
    int i, idx, tx = 0, ty = 0;
435
    int mvx[4], mvy[4], intra[4];
436
    static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
437

    
438
    if(!v->s.last_picture.data[0])return;
439
    if(s->flags & CODEC_FLAG_GRAY) return;
440

    
441
    for(i = 0; i < 4; i++) {
442
        mvx[i] = s->mv[0][i][0];
443
        mvy[i] = s->mv[0][i][1];
444
        intra[i] = v->mb_type[0][s->block_index[i]];
445
    }
446

    
447
    /* calculate chroma MV vector from four luma MVs */
448
    idx = (intra[3] << 3) | (intra[2] << 2) | (intra[1] << 1) | intra[0];
449
    if(!idx) { // all blocks are inter
450
        tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
451
        ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
452
    } else if(count[idx] == 1) { // 3 inter blocks
453
        switch(idx) {
454
        case 0x1:
455
            tx = mid_pred(mvx[1], mvx[2], mvx[3]);
456
            ty = mid_pred(mvy[1], mvy[2], mvy[3]);
457
            break;
458
        case 0x2:
459
            tx = mid_pred(mvx[0], mvx[2], mvx[3]);
460
            ty = mid_pred(mvy[0], mvy[2], mvy[3]);
461
            break;
462
        case 0x4:
463
            tx = mid_pred(mvx[0], mvx[1], mvx[3]);
464
            ty = mid_pred(mvy[0], mvy[1], mvy[3]);
465
            break;
466
        case 0x8:
467
            tx = mid_pred(mvx[0], mvx[1], mvx[2]);
468
            ty = mid_pred(mvy[0], mvy[1], mvy[2]);
469
            break;
470
        }
471
    } else if(count[idx] == 2) {
472
        int t1 = 0, t2 = 0;
473
        for(i=0; i<3;i++) if(!intra[i]) {t1 = i; break;}
474
        for(i= t1+1; i<4; i++)if(!intra[i]) {t2 = i; break;}
475
        tx = (mvx[t1] + mvx[t2]) / 2;
476
        ty = (mvy[t1] + mvy[t2]) / 2;
477
    } else {
478
        s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
479
        s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
480
        return; //no need to do MC for inter blocks
481
    }
482

    
483
    s->current_picture.motion_val[1][s->block_index[0]][0] = tx;
484
    s->current_picture.motion_val[1][s->block_index[0]][1] = ty;
485
    uvmx = (tx + ((tx&3) == 3)) >> 1;
486
    uvmy = (ty + ((ty&3) == 3)) >> 1;
487
    if(v->fastuvmc) {
488
        uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
489
        uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
490
    }
491

    
492
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
493
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
494

    
495
    if(v->profile != PROFILE_ADVANCED){
496
        uvsrc_x = av_clip(uvsrc_x,  -8, s->mb_width  *  8);
497
        uvsrc_y = av_clip(uvsrc_y,  -8, s->mb_height *  8);
498
    }else{
499
        uvsrc_x = av_clip(uvsrc_x,  -8, s->avctx->coded_width  >> 1);
500
        uvsrc_y = av_clip(uvsrc_y,  -8, s->avctx->coded_height >> 1);
501
    }
502

    
503
    srcU = s->last_picture.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
504
    srcV = s->last_picture.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
505
    if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
506
       || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
507
       || (unsigned)uvsrc_y > (s->v_edge_pos >> 1) - 9){
508
        s->dsp.emulated_edge_mc(s->edge_emu_buffer     , srcU, s->uvlinesize, 8+1, 8+1,
509
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
510
        s->dsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize, 8+1, 8+1,
511
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
512
        srcU = s->edge_emu_buffer;
513
        srcV = s->edge_emu_buffer + 16;
514

    
515
        /* if we deal with range reduction we need to scale source blocks */
516
        if(v->rangeredfrm) {
517
            int i, j;
518
            uint8_t *src, *src2;
519

    
520
            src = srcU; src2 = srcV;
521
            for(j = 0; j < 9; j++) {
522
                for(i = 0; i < 9; i++) {
523
                    src[i] = ((src[i] - 128) >> 1) + 128;
524
                    src2[i] = ((src2[i] - 128) >> 1) + 128;
525
                }
526
                src += s->uvlinesize;
527
                src2 += s->uvlinesize;
528
            }
529
        }
530
        /* if we deal with intensity compensation we need to scale source blocks */
531
        if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
532
            int i, j;
533
            uint8_t *src, *src2;
534

    
535
            src = srcU; src2 = srcV;
536
            for(j = 0; j < 9; j++) {
537
                for(i = 0; i < 9; i++) {
538
                    src[i] = v->lutuv[src[i]];
539
                    src2[i] = v->lutuv[src2[i]];
540
                }
541
                src += s->uvlinesize;
542
                src2 += s->uvlinesize;
543
            }
544
        }
545
    }
546

    
547
    /* Chroma MC always uses qpel bilinear */
548
    uvmx = (uvmx&3)<<1;
549
    uvmy = (uvmy&3)<<1;
550
    if(!v->rnd){
551
        dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
552
        dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
553
    }else{
554
        v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
555
        v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
556
    }
557
}
558

    
559
/***********************************************************************/
560
/**
561
 * @defgroup vc1block VC-1 Block-level functions
562
 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
563
 * @{
564
 */
565

    
566
/**
567
 * @def GET_MQUANT
568
 * @brief Get macroblock-level quantizer scale
569
 */
570
#define GET_MQUANT()                                           \
571
  if (v->dquantfrm)                                            \
572
  {                                                            \
573
    int edges = 0;                                             \
574
    if (v->dqprofile == DQPROFILE_ALL_MBS)                     \
575
    {                                                          \
576
      if (v->dqbilevel)                                        \
577
      {                                                        \
578
        mquant = (get_bits1(gb)) ? v->altpq : v->pq;           \
579
      }                                                        \
580
      else                                                     \
581
      {                                                        \
582
        mqdiff = get_bits(gb, 3);                              \
583
        if (mqdiff != 7) mquant = v->pq + mqdiff;              \
584
        else mquant = get_bits(gb, 5);                         \
585
      }                                                        \
586
    }                                                          \
587
    if(v->dqprofile == DQPROFILE_SINGLE_EDGE)                  \
588
        edges = 1 << v->dqsbedge;                              \
589
    else if(v->dqprofile == DQPROFILE_DOUBLE_EDGES)            \
590
        edges = (3 << v->dqsbedge) % 15;                       \
591
    else if(v->dqprofile == DQPROFILE_FOUR_EDGES)              \
592
        edges = 15;                                            \
593
    if((edges&1) && !s->mb_x)                                  \
594
        mquant = v->altpq;                                     \
595
    if((edges&2) && s->first_slice_line)                       \
596
        mquant = v->altpq;                                     \
597
    if((edges&4) && s->mb_x == (s->mb_width - 1))              \
598
        mquant = v->altpq;                                     \
599
    if((edges&8) && s->mb_y == (s->mb_height - 1))             \
600
        mquant = v->altpq;                                     \
601
  }
602

    
603
/**
604
 * @def GET_MVDATA(_dmv_x, _dmv_y)
605
 * @brief Get MV differentials
606
 * @see MVDATA decoding from 8.3.5.2, p(1)20
607
 * @param _dmv_x Horizontal differential for decoded MV
608
 * @param _dmv_y Vertical differential for decoded MV
609
 */
610
#define GET_MVDATA(_dmv_x, _dmv_y)                                  \
611
  index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table,\
612
                       VC1_MV_DIFF_VLC_BITS, 2);                    \
613
  if (index > 36)                                                   \
614
  {                                                                 \
615
    mb_has_coeffs = 1;                                              \
616
    index -= 37;                                                    \
617
  }                                                                 \
618
  else mb_has_coeffs = 0;                                           \
619
  s->mb_intra = 0;                                                  \
620
  if (!index) { _dmv_x = _dmv_y = 0; }                              \
621
  else if (index == 35)                                             \
622
  {                                                                 \
623
    _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample);          \
624
    _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample);          \
625
  }                                                                 \
626
  else if (index == 36)                                             \
627
  {                                                                 \
628
    _dmv_x = 0;                                                     \
629
    _dmv_y = 0;                                                     \
630
    s->mb_intra = 1;                                                \
631
  }                                                                 \
632
  else                                                              \
633
  {                                                                 \
634
    index1 = index%6;                                               \
635
    if (!s->quarter_sample && index1 == 5) val = 1;                 \
636
    else                                   val = 0;                 \
637
    if(size_table[index1] - val > 0)                                \
638
        val = get_bits(gb, size_table[index1] - val);               \
639
    else                                   val = 0;                 \
640
    sign = 0 - (val&1);                                             \
641
    _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign;     \
642
                                                                    \
643
    index1 = index/6;                                               \
644
    if (!s->quarter_sample && index1 == 5) val = 1;                 \
645
    else                                   val = 0;                 \
646
    if(size_table[index1] - val > 0)                                \
647
        val = get_bits(gb, size_table[index1] - val);               \
648
    else                                   val = 0;                 \
649
    sign = 0 - (val&1);                                             \
650
    _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign;     \
651
  }
652

    
653
/** Predict and set motion vector
654
 */
655
static inline void vc1_pred_mv(MpegEncContext *s, int n, int dmv_x, int dmv_y, int mv1, int r_x, int r_y, uint8_t* is_intra)
656
{
657
    int xy, wrap, off = 0;
658
    int16_t *A, *B, *C;
659
    int px, py;
660
    int sum;
661

    
662
    /* scale MV difference to be quad-pel */
663
    dmv_x <<= 1 - s->quarter_sample;
664
    dmv_y <<= 1 - s->quarter_sample;
665

    
666
    wrap = s->b8_stride;
667
    xy = s->block_index[n];
668

    
669
    if(s->mb_intra){
670
        s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
671
        s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
672
        s->current_picture.motion_val[1][xy][0] = 0;
673
        s->current_picture.motion_val[1][xy][1] = 0;
674
        if(mv1) { /* duplicate motion data for 1-MV block */
675
            s->current_picture.motion_val[0][xy + 1][0] = 0;
676
            s->current_picture.motion_val[0][xy + 1][1] = 0;
677
            s->current_picture.motion_val[0][xy + wrap][0] = 0;
678
            s->current_picture.motion_val[0][xy + wrap][1] = 0;
679
            s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
680
            s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
681
            s->current_picture.motion_val[1][xy + 1][0] = 0;
682
            s->current_picture.motion_val[1][xy + 1][1] = 0;
683
            s->current_picture.motion_val[1][xy + wrap][0] = 0;
684
            s->current_picture.motion_val[1][xy + wrap][1] = 0;
685
            s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
686
            s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
687
        }
688
        return;
689
    }
690

    
691
    C = s->current_picture.motion_val[0][xy - 1];
692
    A = s->current_picture.motion_val[0][xy - wrap];
693
    if(mv1)
694
        off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
695
    else {
696
        //in 4-MV mode different blocks have different B predictor position
697
        switch(n){
698
        case 0:
699
            off = (s->mb_x > 0) ? -1 : 1;
700
            break;
701
        case 1:
702
            off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
703
            break;
704
        case 2:
705
            off = 1;
706
            break;
707
        case 3:
708
            off = -1;
709
        }
710
    }
711
    B = s->current_picture.motion_val[0][xy - wrap + off];
712

    
713
    if(!s->first_slice_line || (n==2 || n==3)) { // predictor A is not out of bounds
714
        if(s->mb_width == 1) {
715
            px = A[0];
716
            py = A[1];
717
        } else {
718
            px = mid_pred(A[0], B[0], C[0]);
719
            py = mid_pred(A[1], B[1], C[1]);
720
        }
721
    } else if(s->mb_x || (n==1 || n==3)) { // predictor C is not out of bounds
722
        px = C[0];
723
        py = C[1];
724
    } else {
725
        px = py = 0;
726
    }
727
    /* Pullback MV as specified in 8.3.5.3.4 */
728
    {
729
        int qx, qy, X, Y;
730
        qx = (s->mb_x << 6) + ((n==1 || n==3) ? 32 : 0);
731
        qy = (s->mb_y << 6) + ((n==2 || n==3) ? 32 : 0);
732
        X = (s->mb_width << 6) - 4;
733
        Y = (s->mb_height << 6) - 4;
734
        if(mv1) {
735
            if(qx + px < -60) px = -60 - qx;
736
            if(qy + py < -60) py = -60 - qy;
737
        } else {
738
            if(qx + px < -28) px = -28 - qx;
739
            if(qy + py < -28) py = -28 - qy;
740
        }
741
        if(qx + px > X) px = X - qx;
742
        if(qy + py > Y) py = Y - qy;
743
    }
744
    /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
745
    if((!s->first_slice_line || (n==2 || n==3)) && (s->mb_x || (n==1 || n==3))) {
746
        if(is_intra[xy - wrap])
747
            sum = FFABS(px) + FFABS(py);
748
        else
749
            sum = FFABS(px - A[0]) + FFABS(py - A[1]);
750
        if(sum > 32) {
751
            if(get_bits1(&s->gb)) {
752
                px = A[0];
753
                py = A[1];
754
            } else {
755
                px = C[0];
756
                py = C[1];
757
            }
758
        } else {
759
            if(is_intra[xy - 1])
760
                sum = FFABS(px) + FFABS(py);
761
            else
762
                sum = FFABS(px - C[0]) + FFABS(py - C[1]);
763
            if(sum > 32) {
764
                if(get_bits1(&s->gb)) {
765
                    px = A[0];
766
                    py = A[1];
767
                } else {
768
                    px = C[0];
769
                    py = C[1];
770
                }
771
            }
772
        }
773
    }
774
    /* store MV using signed modulus of MV range defined in 4.11 */
775
    s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
776
    s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
777
    if(mv1) { /* duplicate motion data for 1-MV block */
778
        s->current_picture.motion_val[0][xy + 1][0] = s->current_picture.motion_val[0][xy][0];
779
        s->current_picture.motion_val[0][xy + 1][1] = s->current_picture.motion_val[0][xy][1];
780
        s->current_picture.motion_val[0][xy + wrap][0] = s->current_picture.motion_val[0][xy][0];
781
        s->current_picture.motion_val[0][xy + wrap][1] = s->current_picture.motion_val[0][xy][1];
782
        s->current_picture.motion_val[0][xy + wrap + 1][0] = s->current_picture.motion_val[0][xy][0];
783
        s->current_picture.motion_val[0][xy + wrap + 1][1] = s->current_picture.motion_val[0][xy][1];
784
    }
785
}
786

    
787
/** Motion compensation for direct or interpolated blocks in B-frames
788
 */
789
static void vc1_interp_mc(VC1Context *v)
790
{
791
    MpegEncContext *s = &v->s;
792
    DSPContext *dsp = &v->s.dsp;
793
    uint8_t *srcY, *srcU, *srcV;
794
    int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
795

    
796
    if(!v->s.next_picture.data[0])return;
797

    
798
    mx = s->mv[1][0][0];
799
    my = s->mv[1][0][1];
800
    uvmx = (mx + ((mx & 3) == 3)) >> 1;
801
    uvmy = (my + ((my & 3) == 3)) >> 1;
802
    if(v->fastuvmc) {
803
        uvmx = uvmx + ((uvmx<0)?-(uvmx&1):(uvmx&1));
804
        uvmy = uvmy + ((uvmy<0)?-(uvmy&1):(uvmy&1));
805
    }
806
    srcY = s->next_picture.data[0];
807
    srcU = s->next_picture.data[1];
808
    srcV = s->next_picture.data[2];
809

    
810
    src_x = s->mb_x * 16 + (mx >> 2);
811
    src_y = s->mb_y * 16 + (my >> 2);
812
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
813
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
814

    
815
    if(v->profile != PROFILE_ADVANCED){
816
        src_x   = av_clip(  src_x, -16, s->mb_width  * 16);
817
        src_y   = av_clip(  src_y, -16, s->mb_height * 16);
818
        uvsrc_x = av_clip(uvsrc_x,  -8, s->mb_width  *  8);
819
        uvsrc_y = av_clip(uvsrc_y,  -8, s->mb_height *  8);
820
    }else{
821
        src_x   = av_clip(  src_x, -17, s->avctx->coded_width);
822
        src_y   = av_clip(  src_y, -18, s->avctx->coded_height + 1);
823
        uvsrc_x = av_clip(uvsrc_x,  -8, s->avctx->coded_width  >> 1);
824
        uvsrc_y = av_clip(uvsrc_y,  -8, s->avctx->coded_height >> 1);
825
    }
826

    
827
    srcY += src_y * s->linesize + src_x;
828
    srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
829
    srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
830

    
831
    /* for grayscale we should not try to read from unknown area */
832
    if(s->flags & CODEC_FLAG_GRAY) {
833
        srcU = s->edge_emu_buffer + 18 * s->linesize;
834
        srcV = s->edge_emu_buffer + 18 * s->linesize;
835
    }
836

    
837
    if(v->rangeredfrm
838
       || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel*3
839
       || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 16 - s->mspel*3){
840
        uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
841

    
842
        srcY -= s->mspel * (1 + s->linesize);
843
        s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2,
844
                            src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
845
        srcY = s->edge_emu_buffer;
846
        s->dsp.emulated_edge_mc(uvbuf     , srcU, s->uvlinesize, 8+1, 8+1,
847
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
848
        s->dsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
849
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
850
        srcU = uvbuf;
851
        srcV = uvbuf + 16;
852
        /* if we deal with range reduction we need to scale source blocks */
853
        if(v->rangeredfrm) {
854
            int i, j;
855
            uint8_t *src, *src2;
856

    
857
            src = srcY;
858
            for(j = 0; j < 17 + s->mspel*2; j++) {
859
                for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
860
                src += s->linesize;
861
            }
862
            src = srcU; src2 = srcV;
863
            for(j = 0; j < 9; j++) {
864
                for(i = 0; i < 9; i++) {
865
                    src[i] = ((src[i] - 128) >> 1) + 128;
866
                    src2[i] = ((src2[i] - 128) >> 1) + 128;
867
                }
868
                src += s->uvlinesize;
869
                src2 += s->uvlinesize;
870
            }
871
        }
872
        srcY += s->mspel * (1 + s->linesize);
873
    }
874

    
875
    if(s->mspel) {
876
        dxy = ((my & 3) << 2) | (mx & 3);
877
        v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0]    , srcY    , s->linesize, v->rnd);
878
        v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
879
        srcY += s->linesize * 8;
880
        v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize    , srcY    , s->linesize, v->rnd);
881
        v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
882
    } else { // hpel mc
883
        dxy = (my & 2) | ((mx & 2) >> 1);
884

    
885
        if(!v->rnd)
886
            dsp->avg_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
887
        else
888
            dsp->avg_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
889
    }
890

    
891
    if(s->flags & CODEC_FLAG_GRAY) return;
892
    /* Chroma MC always uses qpel blilinear */
893
    uvmx = (uvmx&3)<<1;
894
    uvmy = (uvmy&3)<<1;
895
    if(!v->rnd){
896
        dsp->avg_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
897
        dsp->avg_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
898
    }else{
899
        v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
900
        v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
901
    }
902
}
903

    
904
static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
905
{
906
    int n = bfrac;
907

    
908
#if B_FRACTION_DEN==256
909
    if(inv)
910
        n -= 256;
911
    if(!qs)
912
        return 2 * ((value * n + 255) >> 9);
913
    return (value * n + 128) >> 8;
914
#else
915
    if(inv)
916
        n -= B_FRACTION_DEN;
917
    if(!qs)
918
        return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
919
    return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
920
#endif
921
}
922

    
923
/** Reconstruct motion vector for B-frame and do motion compensation
924
 */
925
static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mode)
926
{
927
    if(v->use_ic) {
928
        v->mv_mode2 = v->mv_mode;
929
        v->mv_mode = MV_PMODE_INTENSITY_COMP;
930
    }
931
    if(direct) {
932
        vc1_mc_1mv(v, 0);
933
        vc1_interp_mc(v);
934
        if(v->use_ic) v->mv_mode = v->mv_mode2;
935
        return;
936
    }
937
    if(mode == BMV_TYPE_INTERPOLATED) {
938
        vc1_mc_1mv(v, 0);
939
        vc1_interp_mc(v);
940
        if(v->use_ic) v->mv_mode = v->mv_mode2;
941
        return;
942
    }
943

    
944
    if(v->use_ic && (mode == BMV_TYPE_BACKWARD)) v->mv_mode = v->mv_mode2;
945
    vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
946
    if(v->use_ic) v->mv_mode = v->mv_mode2;
947
}
948

    
949
static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mvtype)
950
{
951
    MpegEncContext *s = &v->s;
952
    int xy, wrap, off = 0;
953
    int16_t *A, *B, *C;
954
    int px, py;
955
    int sum;
956
    int r_x, r_y;
957
    const uint8_t *is_intra = v->mb_type[0];
958

    
959
    r_x = v->range_x;
960
    r_y = v->range_y;
961
    /* scale MV difference to be quad-pel */
962
    dmv_x[0] <<= 1 - s->quarter_sample;
963
    dmv_y[0] <<= 1 - s->quarter_sample;
964
    dmv_x[1] <<= 1 - s->quarter_sample;
965
    dmv_y[1] <<= 1 - s->quarter_sample;
966

    
967
    wrap = s->b8_stride;
968
    xy = s->block_index[0];
969

    
970
    if(s->mb_intra) {
971
        s->current_picture.motion_val[0][xy][0] =
972
        s->current_picture.motion_val[0][xy][1] =
973
        s->current_picture.motion_val[1][xy][0] =
974
        s->current_picture.motion_val[1][xy][1] = 0;
975
        return;
976
    }
977
    s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
978
    s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
979
    s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
980
    s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
981

    
982
    /* Pullback predicted motion vectors as specified in 8.4.5.4 */
983
    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));
984
    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));
985
    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));
986
    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));
987
    if(direct) {
988
        s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
989
        s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
990
        s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
991
        s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
992
        return;
993
    }
994

    
995
    if((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
996
        C = s->current_picture.motion_val[0][xy - 2];
997
        A = s->current_picture.motion_val[0][xy - wrap*2];
998
        off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
999
        B = s->current_picture.motion_val[0][xy - wrap*2 + off];
1000

    
1001
        if(!s->mb_x) C[0] = C[1] = 0;
1002
        if(!s->first_slice_line) { // predictor A is not out of bounds
1003
            if(s->mb_width == 1) {
1004
                px = A[0];
1005
                py = A[1];
1006
            } else {
1007
                px = mid_pred(A[0], B[0], C[0]);
1008
                py = mid_pred(A[1], B[1], C[1]);
1009
            }
1010
        } else if(s->mb_x) { // predictor C is not out of bounds
1011
            px = C[0];
1012
            py = C[1];
1013
        } else {
1014
            px = py = 0;
1015
        }
1016
        /* Pullback MV as specified in 8.3.5.3.4 */
1017
        {
1018
            int qx, qy, X, Y;
1019
            if(v->profile < PROFILE_ADVANCED) {
1020
                qx = (s->mb_x << 5);
1021
                qy = (s->mb_y << 5);
1022
                X = (s->mb_width << 5) - 4;
1023
                Y = (s->mb_height << 5) - 4;
1024
                if(qx + px < -28) px = -28 - qx;
1025
                if(qy + py < -28) py = -28 - qy;
1026
                if(qx + px > X) px = X - qx;
1027
                if(qy + py > Y) py = Y - qy;
1028
            } else {
1029
                qx = (s->mb_x << 6);
1030
                qy = (s->mb_y << 6);
1031
                X = (s->mb_width << 6) - 4;
1032
                Y = (s->mb_height << 6) - 4;
1033
                if(qx + px < -60) px = -60 - qx;
1034
                if(qy + py < -60) py = -60 - qy;
1035
                if(qx + px > X) px = X - qx;
1036
                if(qy + py > Y) py = Y - qy;
1037
            }
1038
        }
1039
        /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
1040
        if(0 && !s->first_slice_line && s->mb_x) {
1041
            if(is_intra[xy - wrap])
1042
                sum = FFABS(px) + FFABS(py);
1043
            else
1044
                sum = FFABS(px - A[0]) + FFABS(py - A[1]);
1045
            if(sum > 32) {
1046
                if(get_bits1(&s->gb)) {
1047
                    px = A[0];
1048
                    py = A[1];
1049
                } else {
1050
                    px = C[0];
1051
                    py = C[1];
1052
                }
1053
            } else {
1054
                if(is_intra[xy - 2])
1055
                    sum = FFABS(px) + FFABS(py);
1056
                else
1057
                    sum = FFABS(px - C[0]) + FFABS(py - C[1]);
1058
                if(sum > 32) {
1059
                    if(get_bits1(&s->gb)) {
1060
                        px = A[0];
1061
                        py = A[1];
1062
                    } else {
1063
                        px = C[0];
1064
                        py = C[1];
1065
                    }
1066
                }
1067
            }
1068
        }
1069
        /* store MV using signed modulus of MV range defined in 4.11 */
1070
        s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
1071
        s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
1072
    }
1073
    if((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
1074
        C = s->current_picture.motion_val[1][xy - 2];
1075
        A = s->current_picture.motion_val[1][xy - wrap*2];
1076
        off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1077
        B = s->current_picture.motion_val[1][xy - wrap*2 + off];
1078

    
1079
        if(!s->mb_x) C[0] = C[1] = 0;
1080
        if(!s->first_slice_line) { // predictor A is not out of bounds
1081
            if(s->mb_width == 1) {
1082
                px = A[0];
1083
                py = A[1];
1084
            } else {
1085
                px = mid_pred(A[0], B[0], C[0]);
1086
                py = mid_pred(A[1], B[1], C[1]);
1087
            }
1088
        } else if(s->mb_x) { // predictor C is not out of bounds
1089
            px = C[0];
1090
            py = C[1];
1091
        } else {
1092
            px = py = 0;
1093
        }
1094
        /* Pullback MV as specified in 8.3.5.3.4 */
1095
        {
1096
            int qx, qy, X, Y;
1097
            if(v->profile < PROFILE_ADVANCED) {
1098
                qx = (s->mb_x << 5);
1099
                qy = (s->mb_y << 5);
1100
                X = (s->mb_width << 5) - 4;
1101
                Y = (s->mb_height << 5) - 4;
1102
                if(qx + px < -28) px = -28 - qx;
1103
                if(qy + py < -28) py = -28 - qy;
1104
                if(qx + px > X) px = X - qx;
1105
                if(qy + py > Y) py = Y - qy;
1106
            } else {
1107
                qx = (s->mb_x << 6);
1108
                qy = (s->mb_y << 6);
1109
                X = (s->mb_width << 6) - 4;
1110
                Y = (s->mb_height << 6) - 4;
1111
                if(qx + px < -60) px = -60 - qx;
1112
                if(qy + py < -60) py = -60 - qy;
1113
                if(qx + px > X) px = X - qx;
1114
                if(qy + py > Y) py = Y - qy;
1115
            }
1116
        }
1117
        /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
1118
        if(0 && !s->first_slice_line && s->mb_x) {
1119
            if(is_intra[xy - wrap])
1120
                sum = FFABS(px) + FFABS(py);
1121
            else
1122
                sum = FFABS(px - A[0]) + FFABS(py - A[1]);
1123
            if(sum > 32) {
1124
                if(get_bits1(&s->gb)) {
1125
                    px = A[0];
1126
                    py = A[1];
1127
                } else {
1128
                    px = C[0];
1129
                    py = C[1];
1130
                }
1131
            } else {
1132
                if(is_intra[xy - 2])
1133
                    sum = FFABS(px) + FFABS(py);
1134
                else
1135
                    sum = FFABS(px - C[0]) + FFABS(py - C[1]);
1136
                if(sum > 32) {
1137
                    if(get_bits1(&s->gb)) {
1138
                        px = A[0];
1139
                        py = A[1];
1140
                    } else {
1141
                        px = C[0];
1142
                        py = C[1];
1143
                    }
1144
                }
1145
            }
1146
        }
1147
        /* store MV using signed modulus of MV range defined in 4.11 */
1148

    
1149
        s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
1150
        s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
1151
    }
1152
    s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
1153
    s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
1154
    s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
1155
    s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
1156
}
1157

    
1158
/** Get predicted DC value for I-frames only
1159
 * prediction dir: left=0, top=1
1160
 * @param s MpegEncContext
1161
 * @param overlap flag indicating that overlap filtering is used
1162
 * @param pq integer part of picture quantizer
1163
 * @param[in] n block index in the current MB
1164
 * @param dc_val_ptr Pointer to DC predictor
1165
 * @param dir_ptr Prediction direction for use in AC prediction
1166
 */
1167
static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
1168
                              int16_t **dc_val_ptr, int *dir_ptr)
1169
{
1170
    int a, b, c, wrap, pred, scale;
1171
    int16_t *dc_val;
1172
    static const uint16_t dcpred[32] = {
1173
    -1, 1024,  512,  341,  256,  205,  171,  146,  128,
1174
         114,  102,   93,   85,   79,   73,   68,   64,
1175
          60,   57,   54,   51,   49,   47,   45,   43,
1176
          41,   39,   38,   37,   35,   34,   33
1177
    };
1178

    
1179
    /* find prediction - wmv3_dc_scale always used here in fact */
1180
    if (n < 4)     scale = s->y_dc_scale;
1181
    else           scale = s->c_dc_scale;
1182

    
1183
    wrap = s->block_wrap[n];
1184
    dc_val= s->dc_val[0] + s->block_index[n];
1185

    
1186
    /* B A
1187
     * C X
1188
     */
1189
    c = dc_val[ - 1];
1190
    b = dc_val[ - 1 - wrap];
1191
    a = dc_val[ - wrap];
1192

    
1193
    if (pq < 9 || !overlap)
1194
    {
1195
        /* Set outer values */
1196
        if (s->first_slice_line && (n!=2 && n!=3)) b=a=dcpred[scale];
1197
        if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=dcpred[scale];
1198
    }
1199
    else
1200
    {
1201
        /* Set outer values */
1202
        if (s->first_slice_line && (n!=2 && n!=3)) b=a=0;
1203
        if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=0;
1204
    }
1205

    
1206
    if (abs(a - b) <= abs(b - c)) {
1207
        pred = c;
1208
        *dir_ptr = 1;//left
1209
    } else {
1210
        pred = a;
1211
        *dir_ptr = 0;//top
1212
    }
1213

    
1214
    /* update predictor */
1215
    *dc_val_ptr = &dc_val[0];
1216
    return pred;
1217
}
1218

    
1219

    
1220
/** Get predicted DC value
1221
 * prediction dir: left=0, top=1
1222
 * @param s MpegEncContext
1223
 * @param overlap flag indicating that overlap filtering is used
1224
 * @param pq integer part of picture quantizer
1225
 * @param[in] n block index in the current MB
1226
 * @param a_avail flag indicating top block availability
1227
 * @param c_avail flag indicating left block availability
1228
 * @param dc_val_ptr Pointer to DC predictor
1229
 * @param dir_ptr Prediction direction for use in AC prediction
1230
 */
1231
static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
1232
                              int a_avail, int c_avail,
1233
                              int16_t **dc_val_ptr, int *dir_ptr)
1234
{
1235
    int a, b, c, wrap, pred;
1236
    int16_t *dc_val;
1237
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1238
    int q1, q2 = 0;
1239

    
1240
    wrap = s->block_wrap[n];
1241
    dc_val= s->dc_val[0] + s->block_index[n];
1242

    
1243
    /* B A
1244
     * C X
1245
     */
1246
    c = dc_val[ - 1];
1247
    b = dc_val[ - 1 - wrap];
1248
    a = dc_val[ - wrap];
1249
    /* scale predictors if needed */
1250
    q1 = s->current_picture.qscale_table[mb_pos];
1251
    if(c_avail && (n!= 1 && n!=3)) {
1252
        q2 = s->current_picture.qscale_table[mb_pos - 1];
1253
        if(q2 && q2 != q1)
1254
            c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
1255
    }
1256
    if(a_avail && (n!= 2 && n!=3)) {
1257
        q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
1258
        if(q2 && q2 != q1)
1259
            a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
1260
    }
1261
    if(a_avail && c_avail && (n!=3)) {
1262
        int off = mb_pos;
1263
        if(n != 1) off--;
1264
        if(n != 2) off -= s->mb_stride;
1265
        q2 = s->current_picture.qscale_table[off];
1266
        if(q2 && q2 != q1)
1267
            b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
1268
    }
1269

    
1270
    if(a_avail && c_avail) {
1271
        if(abs(a - b) <= abs(b - c)) {
1272
            pred = c;
1273
            *dir_ptr = 1;//left
1274
        } else {
1275
            pred = a;
1276
            *dir_ptr = 0;//top
1277
        }
1278
    } else if(a_avail) {
1279
        pred = a;
1280
        *dir_ptr = 0;//top
1281
    } else if(c_avail) {
1282
        pred = c;
1283
        *dir_ptr = 1;//left
1284
    } else {
1285
        pred = 0;
1286
        *dir_ptr = 1;//left
1287
    }
1288

    
1289
    /* update predictor */
1290
    *dc_val_ptr = &dc_val[0];
1291
    return pred;
1292
}
1293

    
1294
/** @} */ // Block group
1295

    
1296
/**
1297
 * @defgroup vc1_std_mb VC1 Macroblock-level functions in Simple/Main Profiles
1298
 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1299
 * @{
1300
 */
1301

    
1302
static inline int vc1_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
1303
{
1304
    int xy, wrap, pred, a, b, c;
1305

    
1306
    xy = s->block_index[n];
1307
    wrap = s->b8_stride;
1308

    
1309
    /* B C
1310
     * A X
1311
     */
1312
    a = s->coded_block[xy - 1       ];
1313
    b = s->coded_block[xy - 1 - wrap];
1314
    c = s->coded_block[xy     - wrap];
1315

    
1316
    if (b == c) {
1317
        pred = a;
1318
    } else {
1319
        pred = c;
1320
    }
1321

    
1322
    /* store value */
1323
    *coded_block_ptr = &s->coded_block[xy];
1324

    
1325
    return pred;
1326
}
1327

    
1328
/**
1329
 * Decode one AC coefficient
1330
 * @param v The VC1 context
1331
 * @param last Last coefficient
1332
 * @param skip How much zero coefficients to skip
1333
 * @param value Decoded AC coefficient value
1334
 * @param codingset set of VLC to decode data
1335
 * @see 8.1.3.4
1336
 */
1337
static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip, int *value, int codingset)
1338
{
1339
    GetBitContext *gb = &v->s.gb;
1340
    int index, escape, run = 0, level = 0, lst = 0;
1341

    
1342
    index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
1343
    if (index != vc1_ac_sizes[codingset] - 1) {
1344
        run = vc1_index_decode_table[codingset][index][0];
1345
        level = vc1_index_decode_table[codingset][index][1];
1346
        lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
1347
        if(get_bits1(gb))
1348
            level = -level;
1349
    } else {
1350
        escape = decode210(gb);
1351
        if (escape != 2) {
1352
            index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
1353
            run = vc1_index_decode_table[codingset][index][0];
1354
            level = vc1_index_decode_table[codingset][index][1];
1355
            lst = index >= vc1_last_decode_table[codingset];
1356
            if(escape == 0) {
1357
                if(lst)
1358
                    level += vc1_last_delta_level_table[codingset][run];
1359
                else
1360
                    level += vc1_delta_level_table[codingset][run];
1361
            } else {
1362
                if(lst)
1363
                    run += vc1_last_delta_run_table[codingset][level] + 1;
1364
                else
1365
                    run += vc1_delta_run_table[codingset][level] + 1;
1366
            }
1367
            if(get_bits1(gb))
1368
                level = -level;
1369
        } else {
1370
            int sign;
1371
            lst = get_bits1(gb);
1372
            if(v->s.esc3_level_length == 0) {
1373
                if(v->pq < 8 || v->dquantfrm) { // table 59
1374
                    v->s.esc3_level_length = get_bits(gb, 3);
1375
                    if(!v->s.esc3_level_length)
1376
                        v->s.esc3_level_length = get_bits(gb, 2) + 8;
1377
                } else { //table 60
1378
                    v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
1379
                }
1380
                v->s.esc3_run_length = 3 + get_bits(gb, 2);
1381
            }
1382
            run = get_bits(gb, v->s.esc3_run_length);
1383
            sign = get_bits1(gb);
1384
            level = get_bits(gb, v->s.esc3_level_length);
1385
            if(sign)
1386
                level = -level;
1387
        }
1388
    }
1389

    
1390
    *last = lst;
1391
    *skip = run;
1392
    *value = level;
1393
}
1394

    
1395
/** Decode intra block in intra frames - should be faster than decode_intra_block
1396
 * @param v VC1Context
1397
 * @param block block to decode
1398
 * @param[in] n subblock index
1399
 * @param coded are AC coeffs present or not
1400
 * @param codingset set of VLC to decode data
1401
 */
1402
static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset)
1403
{
1404
    GetBitContext *gb = &v->s.gb;
1405
    MpegEncContext *s = &v->s;
1406
    int dc_pred_dir = 0; /* Direction of the DC prediction used */
1407
    int i;
1408
    int16_t *dc_val;
1409
    int16_t *ac_val, *ac_val2;
1410
    int dcdiff;
1411

    
1412
    /* Get DC differential */
1413
    if (n < 4) {
1414
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1415
    } else {
1416
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1417
    }
1418
    if (dcdiff < 0){
1419
        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
1420
        return -1;
1421
    }
1422
    if (dcdiff)
1423
    {
1424
        if (dcdiff == 119 /* ESC index value */)
1425
        {
1426
            /* TODO: Optimize */
1427
            if (v->pq == 1) dcdiff = get_bits(gb, 10);
1428
            else if (v->pq == 2) dcdiff = get_bits(gb, 9);
1429
            else dcdiff = get_bits(gb, 8);
1430
        }
1431
        else
1432
        {
1433
            if (v->pq == 1)
1434
                dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
1435
            else if (v->pq == 2)
1436
                dcdiff = (dcdiff<<1) + get_bits1(gb)   - 1;
1437
        }
1438
        if (get_bits1(gb))
1439
            dcdiff = -dcdiff;
1440
    }
1441

    
1442
    /* Prediction */
1443
    dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
1444
    *dc_val = dcdiff;
1445

    
1446
    /* Store the quantized DC coeff, used for prediction */
1447
    if (n < 4) {
1448
        block[0] = dcdiff * s->y_dc_scale;
1449
    } else {
1450
        block[0] = dcdiff * s->c_dc_scale;
1451
    }
1452
    /* Skip ? */
1453
    if (!coded) {
1454
        goto not_coded;
1455
    }
1456

    
1457
    //AC Decoding
1458
    i = 1;
1459

    
1460
    {
1461
        int last = 0, skip, value;
1462
        const uint8_t *zz_table;
1463
        int scale;
1464
        int k;
1465

    
1466
        scale = v->pq * 2 + v->halfpq;
1467

    
1468
        if(v->s.ac_pred) {
1469
            if(!dc_pred_dir)
1470
                zz_table = v->zz_8x8[2];
1471
            else
1472
                zz_table = v->zz_8x8[3];
1473
        } else
1474
            zz_table = v->zz_8x8[1];
1475

    
1476
        ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1477
        ac_val2 = ac_val;
1478
        if(dc_pred_dir) //left
1479
            ac_val -= 16;
1480
        else //top
1481
            ac_val -= 16 * s->block_wrap[n];
1482

    
1483
        while (!last) {
1484
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1485
            i += skip;
1486
            if(i > 63)
1487
                break;
1488
            block[zz_table[i++]] = value;
1489
        }
1490

    
1491
        /* apply AC prediction if needed */
1492
        if(s->ac_pred) {
1493
            if(dc_pred_dir) { //left
1494
                for(k = 1; k < 8; k++)
1495
                    block[k] += ac_val[k];
1496
            } else { //top
1497
                for(k = 1; k < 8; k++)
1498
                    block[k << 3] += ac_val[k + 8];
1499
            }
1500
        }
1501
        /* save AC coeffs for further prediction */
1502
        for(k = 1; k < 8; k++) {
1503
            ac_val2[k] = block[k];
1504
            ac_val2[k + 8] = block[k << 3];
1505
        }
1506

    
1507
        /* scale AC coeffs */
1508
        for(k = 1; k < 64; k++)
1509
            if(block[k]) {
1510
                block[k] *= scale;
1511
                if(!v->pquantizer)
1512
                    block[k] += (block[k] < 0) ? -v->pq : v->pq;
1513
            }
1514

    
1515
        if(s->ac_pred) i = 63;
1516
    }
1517

    
1518
not_coded:
1519
    if(!coded) {
1520
        int k, scale;
1521
        ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1522
        ac_val2 = ac_val;
1523

    
1524
        i = 0;
1525
        scale = v->pq * 2 + v->halfpq;
1526
        memset(ac_val2, 0, 16 * 2);
1527
        if(dc_pred_dir) {//left
1528
            ac_val -= 16;
1529
            if(s->ac_pred)
1530
                memcpy(ac_val2, ac_val, 8 * 2);
1531
        } else {//top
1532
            ac_val -= 16 * s->block_wrap[n];
1533
            if(s->ac_pred)
1534
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1535
        }
1536

    
1537
        /* apply AC prediction if needed */
1538
        if(s->ac_pred) {
1539
            if(dc_pred_dir) { //left
1540
                for(k = 1; k < 8; k++) {
1541
                    block[k] = ac_val[k] * scale;
1542
                    if(!v->pquantizer && block[k])
1543
                        block[k] += (block[k] < 0) ? -v->pq : v->pq;
1544
                }
1545
            } else { //top
1546
                for(k = 1; k < 8; k++) {
1547
                    block[k << 3] = ac_val[k + 8] * scale;
1548
                    if(!v->pquantizer && block[k << 3])
1549
                        block[k << 3] += (block[k << 3] < 0) ? -v->pq : v->pq;
1550
                }
1551
            }
1552
            i = 63;
1553
        }
1554
    }
1555
    s->block_last_index[n] = i;
1556

    
1557
    return 0;
1558
}
1559

    
1560
/** Decode intra block in intra frames - should be faster than decode_intra_block
1561
 * @param v VC1Context
1562
 * @param block block to decode
1563
 * @param[in] n subblock number
1564
 * @param coded are AC coeffs present or not
1565
 * @param codingset set of VLC to decode data
1566
 * @param mquant quantizer value for this macroblock
1567
 */
1568
static int vc1_decode_i_block_adv(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset, int mquant)
1569
{
1570
    GetBitContext *gb = &v->s.gb;
1571
    MpegEncContext *s = &v->s;
1572
    int dc_pred_dir = 0; /* Direction of the DC prediction used */
1573
    int i;
1574
    int16_t *dc_val;
1575
    int16_t *ac_val, *ac_val2;
1576
    int dcdiff;
1577
    int a_avail = v->a_avail, c_avail = v->c_avail;
1578
    int use_pred = s->ac_pred;
1579
    int scale;
1580
    int q1, q2 = 0;
1581
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1582

    
1583
    /* Get DC differential */
1584
    if (n < 4) {
1585
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1586
    } else {
1587
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1588
    }
1589
    if (dcdiff < 0){
1590
        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
1591
        return -1;
1592
    }
1593
    if (dcdiff)
1594
    {
1595
        if (dcdiff == 119 /* ESC index value */)
1596
        {
1597
            /* TODO: Optimize */
1598
            if (mquant == 1) dcdiff = get_bits(gb, 10);
1599
            else if (mquant == 2) dcdiff = get_bits(gb, 9);
1600
            else dcdiff = get_bits(gb, 8);
1601
        }
1602
        else
1603
        {
1604
            if (mquant == 1)
1605
                dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
1606
            else if (mquant == 2)
1607
                dcdiff = (dcdiff<<1) + get_bits1(gb)   - 1;
1608
        }
1609
        if (get_bits1(gb))
1610
            dcdiff = -dcdiff;
1611
    }
1612

    
1613
    /* Prediction */
1614
    dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
1615
    *dc_val = dcdiff;
1616

    
1617
    /* Store the quantized DC coeff, used for prediction */
1618
    if (n < 4) {
1619
        block[0] = dcdiff * s->y_dc_scale;
1620
    } else {
1621
        block[0] = dcdiff * s->c_dc_scale;
1622
    }
1623

    
1624
    //AC Decoding
1625
    i = 1;
1626

    
1627
    /* check if AC is needed at all */
1628
    if(!a_avail && !c_avail) use_pred = 0;
1629
    ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1630
    ac_val2 = ac_val;
1631

    
1632
    scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
1633

    
1634
    if(dc_pred_dir) //left
1635
        ac_val -= 16;
1636
    else //top
1637
        ac_val -= 16 * s->block_wrap[n];
1638

    
1639
    q1 = s->current_picture.qscale_table[mb_pos];
1640
    if(dc_pred_dir && c_avail && mb_pos) q2 = s->current_picture.qscale_table[mb_pos - 1];
1641
    if(!dc_pred_dir && a_avail && mb_pos >= s->mb_stride) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
1642
    if(dc_pred_dir && n==1) q2 = q1;
1643
    if(!dc_pred_dir && n==2) q2 = q1;
1644
    if(n==3) q2 = q1;
1645

    
1646
    if(coded) {
1647
        int last = 0, skip, value;
1648
        const uint8_t *zz_table;
1649
        int k;
1650

    
1651
        if(v->s.ac_pred) {
1652
            if(!dc_pred_dir)
1653
                zz_table = v->zz_8x8[2];
1654
            else
1655
                zz_table = v->zz_8x8[3];
1656
        } else
1657
            zz_table = v->zz_8x8[1];
1658

    
1659
        while (!last) {
1660
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1661
            i += skip;
1662
            if(i > 63)
1663
                break;
1664
            block[zz_table[i++]] = value;
1665
        }
1666

    
1667
        /* apply AC prediction if needed */
1668
        if(use_pred) {
1669
            /* scale predictors if needed*/
1670
            if(q2 && q1!=q2) {
1671
                q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1672
                q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1673

    
1674
                if(dc_pred_dir) { //left
1675
                    for(k = 1; k < 8; k++)
1676
                        block[k] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1677
                } else { //top
1678
                    for(k = 1; k < 8; k++)
1679
                        block[k << 3] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1680
                }
1681
            } else {
1682
                if(dc_pred_dir) { //left
1683
                    for(k = 1; k < 8; k++)
1684
                        block[k] += ac_val[k];
1685
                } else { //top
1686
                    for(k = 1; k < 8; k++)
1687
                        block[k << 3] += ac_val[k + 8];
1688
                }
1689
            }
1690
        }
1691
        /* save AC coeffs for further prediction */
1692
        for(k = 1; k < 8; k++) {
1693
            ac_val2[k] = block[k];
1694
            ac_val2[k + 8] = block[k << 3];
1695
        }
1696

    
1697
        /* scale AC coeffs */
1698
        for(k = 1; k < 64; k++)
1699
            if(block[k]) {
1700
                block[k] *= scale;
1701
                if(!v->pquantizer)
1702
                    block[k] += (block[k] < 0) ? -mquant : mquant;
1703
            }
1704

    
1705
        if(use_pred) i = 63;
1706
    } else { // no AC coeffs
1707
        int k;
1708

    
1709
        memset(ac_val2, 0, 16 * 2);
1710
        if(dc_pred_dir) {//left
1711
            if(use_pred) {
1712
                memcpy(ac_val2, ac_val, 8 * 2);
1713
                if(q2 && q1!=q2) {
1714
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1715
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1716
                    for(k = 1; k < 8; k++)
1717
                        ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1718
                }
1719
            }
1720
        } else {//top
1721
            if(use_pred) {
1722
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1723
                if(q2 && q1!=q2) {
1724
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1725
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1726
                    for(k = 1; k < 8; k++)
1727
                        ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1728
                }
1729
            }
1730
        }
1731

    
1732
        /* apply AC prediction if needed */
1733
        if(use_pred) {
1734
            if(dc_pred_dir) { //left
1735
                for(k = 1; k < 8; k++) {
1736
                    block[k] = ac_val2[k] * scale;
1737
                    if(!v->pquantizer && block[k])
1738
                        block[k] += (block[k] < 0) ? -mquant : mquant;
1739
                }
1740
            } else { //top
1741
                for(k = 1; k < 8; k++) {
1742
                    block[k << 3] = ac_val2[k + 8] * scale;
1743
                    if(!v->pquantizer && block[k << 3])
1744
                        block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
1745
                }
1746
            }
1747
            i = 63;
1748
        }
1749
    }
1750
    s->block_last_index[n] = i;
1751

    
1752
    return 0;
1753
}
1754

    
1755
/** Decode intra block in inter frames - more generic version than vc1_decode_i_block
1756
 * @param v VC1Context
1757
 * @param block block to decode
1758
 * @param[in] n subblock index
1759
 * @param coded are AC coeffs present or not
1760
 * @param mquant block quantizer
1761
 * @param codingset set of VLC to decode data
1762
 */
1763
static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n, int coded, int mquant, int codingset)
1764
{
1765
    GetBitContext *gb = &v->s.gb;
1766
    MpegEncContext *s = &v->s;
1767
    int dc_pred_dir = 0; /* Direction of the DC prediction used */
1768
    int i;
1769
    int16_t *dc_val;
1770
    int16_t *ac_val, *ac_val2;
1771
    int dcdiff;
1772
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1773
    int a_avail = v->a_avail, c_avail = v->c_avail;
1774
    int use_pred = s->ac_pred;
1775
    int scale;
1776
    int q1, q2 = 0;
1777

    
1778
    s->dsp.clear_block(block);
1779

    
1780
    /* XXX: Guard against dumb values of mquant */
1781
    mquant = (mquant < 1) ? 0 : ( (mquant>31) ? 31 : mquant );
1782

    
1783
    /* Set DC scale - y and c use the same */
1784
    s->y_dc_scale = s->y_dc_scale_table[mquant];
1785
    s->c_dc_scale = s->c_dc_scale_table[mquant];
1786

    
1787
    /* Get DC differential */
1788
    if (n < 4) {
1789
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1790
    } else {
1791
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1792
    }
1793
    if (dcdiff < 0){
1794
        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
1795
        return -1;
1796
    }
1797
    if (dcdiff)
1798
    {
1799
        if (dcdiff == 119 /* ESC index value */)
1800
        {
1801
            /* TODO: Optimize */
1802
            if (mquant == 1) dcdiff = get_bits(gb, 10);
1803
            else if (mquant == 2) dcdiff = get_bits(gb, 9);
1804
            else dcdiff = get_bits(gb, 8);
1805
        }
1806
        else
1807
        {
1808
            if (mquant == 1)
1809
                dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
1810
            else if (mquant == 2)
1811
                dcdiff = (dcdiff<<1) + get_bits1(gb)   - 1;
1812
        }
1813
        if (get_bits1(gb))
1814
            dcdiff = -dcdiff;
1815
    }
1816

    
1817
    /* Prediction */
1818
    dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
1819
    *dc_val = dcdiff;
1820

    
1821
    /* Store the quantized DC coeff, used for prediction */
1822

    
1823
    if (n < 4) {
1824
        block[0] = dcdiff * s->y_dc_scale;
1825
    } else {
1826
        block[0] = dcdiff * s->c_dc_scale;
1827
    }
1828

    
1829
    //AC Decoding
1830
    i = 1;
1831

    
1832
    /* check if AC is needed at all and adjust direction if needed */
1833
    if(!a_avail) dc_pred_dir = 1;
1834
    if(!c_avail) dc_pred_dir = 0;
1835
    if(!a_avail && !c_avail) use_pred = 0;
1836
    ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1837
    ac_val2 = ac_val;
1838

    
1839
    scale = mquant * 2 + v->halfpq;
1840

    
1841
    if(dc_pred_dir) //left
1842
        ac_val -= 16;
1843
    else //top
1844
        ac_val -= 16 * s->block_wrap[n];
1845

    
1846
    q1 = s->current_picture.qscale_table[mb_pos];
1847
    if(dc_pred_dir && c_avail && mb_pos) q2 = s->current_picture.qscale_table[mb_pos - 1];
1848
    if(!dc_pred_dir && a_avail && mb_pos >= s->mb_stride) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
1849
    if(dc_pred_dir && n==1) q2 = q1;
1850
    if(!dc_pred_dir && n==2) q2 = q1;
1851
    if(n==3) q2 = q1;
1852

    
1853
    if(coded) {
1854
        int last = 0, skip, value;
1855
        int k;
1856

    
1857
        while (!last) {
1858
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1859
            i += skip;
1860
            if(i > 63)
1861
                break;
1862
            block[v->zz_8x8[0][i++]] = value;
1863
        }
1864

    
1865
        /* apply AC prediction if needed */
1866
        if(use_pred) {
1867
            /* scale predictors if needed*/
1868
            if(q2 && q1!=q2) {
1869
                q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1870
                q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1871

    
1872
                if(dc_pred_dir) { //left
1873
                    for(k = 1; k < 8; k++)
1874
                        block[k] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1875
                } else { //top
1876
                    for(k = 1; k < 8; k++)
1877
                        block[k << 3] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1878
                }
1879
            } else {
1880
                if(dc_pred_dir) { //left
1881
                    for(k = 1; k < 8; k++)
1882
                        block[k] += ac_val[k];
1883
                } else { //top
1884
                    for(k = 1; k < 8; k++)
1885
                        block[k << 3] += ac_val[k + 8];
1886
                }
1887
            }
1888
        }
1889
        /* save AC coeffs for further prediction */
1890
        for(k = 1; k < 8; k++) {
1891
            ac_val2[k] = block[k];
1892
            ac_val2[k + 8] = block[k << 3];
1893
        }
1894

    
1895
        /* scale AC coeffs */
1896
        for(k = 1; k < 64; k++)
1897
            if(block[k]) {
1898
                block[k] *= scale;
1899
                if(!v->pquantizer)
1900
                    block[k] += (block[k] < 0) ? -mquant : mquant;
1901
            }
1902

    
1903
        if(use_pred) i = 63;
1904
    } else { // no AC coeffs
1905
        int k;
1906

    
1907
        memset(ac_val2, 0, 16 * 2);
1908
        if(dc_pred_dir) {//left
1909
            if(use_pred) {
1910
                memcpy(ac_val2, ac_val, 8 * 2);
1911
                if(q2 && q1!=q2) {
1912
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1913
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1914
                    for(k = 1; k < 8; k++)
1915
                        ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1916
                }
1917
            }
1918
        } else {//top
1919
            if(use_pred) {
1920
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1921
                if(q2 && q1!=q2) {
1922
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1923
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1924
                    for(k = 1; k < 8; k++)
1925
                        ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1926
                }
1927
            }
1928
        }
1929

    
1930
        /* apply AC prediction if needed */
1931
        if(use_pred) {
1932
            if(dc_pred_dir) { //left
1933
                for(k = 1; k < 8; k++) {
1934
                    block[k] = ac_val2[k] * scale;
1935
                    if(!v->pquantizer && block[k])
1936
                        block[k] += (block[k] < 0) ? -mquant : mquant;
1937
                }
1938
            } else { //top
1939
                for(k = 1; k < 8; k++) {
1940
                    block[k << 3] = ac_val2[k + 8] * scale;
1941
                    if(!v->pquantizer && block[k << 3])
1942
                        block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
1943
                }
1944
            }
1945
            i = 63;
1946
        }
1947
    }
1948
    s->block_last_index[n] = i;
1949

    
1950
    return 0;
1951
}
1952

    
1953
/** Decode P block
1954
 */
1955
static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n, int mquant, int ttmb, int first_block,
1956
                              uint8_t *dst, int linesize, int skip_block, int apply_filter, int cbp_top, int cbp_left)
1957
{
1958
    MpegEncContext *s = &v->s;
1959
    GetBitContext *gb = &s->gb;
1960
    int i, j;
1961
    int subblkpat = 0;
1962
    int scale, off, idx, last, skip, value;
1963
    int ttblk = ttmb & 7;
1964
    int pat = 0;
1965

    
1966
    s->dsp.clear_block(block);
1967

    
1968
    if(ttmb == -1) {
1969
        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)];
1970
    }
1971
    if(ttblk == TT_4X4) {
1972
        subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
1973
    }
1974
    if((ttblk != TT_8X8 && ttblk != TT_4X4)
1975
        && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
1976
            || (!v->res_rtm_flag && !first_block))) {
1977
        subblkpat = decode012(gb);
1978
        if(subblkpat) subblkpat ^= 3; //swap decoded pattern bits
1979
        if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) ttblk = TT_8X4;
1980
        if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) ttblk = TT_4X8;
1981
    }
1982
    scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
1983

    
1984
    // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
1985
    if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
1986
        subblkpat = 2 - (ttblk == TT_8X4_TOP);
1987
        ttblk = TT_8X4;
1988
    }
1989
    if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
1990
        subblkpat = 2 - (ttblk == TT_4X8_LEFT);
1991
        ttblk = TT_4X8;
1992
    }
1993
    switch(ttblk) {
1994
    case TT_8X8:
1995
        pat = 0xF;
1996
        i = 0;
1997
        last = 0;
1998
        while (!last) {
1999
            vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2000
            i += skip;
2001
            if(i > 63)
2002
                break;
2003
            idx = v->zz_8x8[0][i++];
2004
            block[idx] = value * scale;
2005
            if(!v->pquantizer)
2006
                block[idx] += (block[idx] < 0) ? -mquant : mquant;
2007
        }
2008
        if(!skip_block){
2009
            if(i==1)
2010
                v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
2011
            else{
2012
                v->vc1dsp.vc1_inv_trans_8x8(block);
2013
                s->dsp.add_pixels_clamped(block, dst, linesize);
2014
            }
2015
            if(apply_filter && cbp_top  & 0xC)
2016
                v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
2017
            if(apply_filter && cbp_left & 0xA)
2018
                v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
2019
        }
2020
        break;
2021
    case TT_4X4:
2022
        pat = ~subblkpat & 0xF;
2023
        for(j = 0; j < 4; j++) {
2024
            last = subblkpat & (1 << (3 - j));
2025
            i = 0;
2026
            off = (j & 1) * 4 + (j & 2) * 16;
2027
            while (!last) {
2028
                vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2029
                i += skip;
2030
                if(i > 15)
2031
                    break;
2032
                idx = ff_vc1_simple_progressive_4x4_zz[i++];
2033
                block[idx + off] = value * scale;
2034
                if(!v->pquantizer)
2035
                    block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
2036
            }
2037
            if(!(subblkpat & (1 << (3 - j))) && !skip_block){
2038
                if(i==1)
2039
                    v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j&1)*4 + (j&2)*2*linesize, linesize, block + off);
2040
                else
2041
                    v->vc1dsp.vc1_inv_trans_4x4(dst + (j&1)*4 + (j&2)*2*linesize, linesize, block + off);
2042
                if(apply_filter && (j&2 ? pat & (1<<(j-2)) : (cbp_top & (1 << (j + 2)))))
2043
                    v->vc1dsp.vc1_v_loop_filter4(dst + (j&1)*4 + (j&2)*2*linesize, linesize, v->pq);
2044
                if(apply_filter && (j&1 ? pat & (1<<(j-1)) : (cbp_left & (1 << (j + 1)))))
2045
                    v->vc1dsp.vc1_h_loop_filter4(dst + (j&1)*4 + (j&2)*2*linesize, linesize, v->pq);
2046
            }
2047
        }
2048
        break;
2049
    case TT_8X4:
2050
        pat = ~((subblkpat & 2)*6 + (subblkpat & 1)*3) & 0xF;
2051
        for(j = 0; j < 2; j++) {
2052
            last = subblkpat & (1 << (1 - j));
2053
            i = 0;
2054
            off = j * 32;
2055
            while (!last) {
2056
                vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2057
                i += skip;
2058
                if(i > 31)
2059
                    break;
2060
                idx = v->zz_8x4[i++]+off;
2061
                block[idx] = value * scale;
2062
                if(!v->pquantizer)
2063
                    block[idx] += (block[idx] < 0) ? -mquant : mquant;
2064
            }
2065
            if(!(subblkpat & (1 << (1 - j))) && !skip_block){
2066
                if(i==1)
2067
                    v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j*4*linesize, linesize, block + off);
2068
                else
2069
                    v->vc1dsp.vc1_inv_trans_8x4(dst + j*4*linesize, linesize, block + off);
2070
                if(apply_filter && j ? pat & 0x3 : (cbp_top & 0xC))
2071
                    v->vc1dsp.vc1_v_loop_filter8(dst + j*4*linesize, linesize, v->pq);
2072
                if(apply_filter && cbp_left & (2 << j))
2073
                    v->vc1dsp.vc1_h_loop_filter4(dst + j*4*linesize, linesize, v->pq);
2074
            }
2075
        }
2076
        break;
2077
    case TT_4X8:
2078
        pat = ~(subblkpat*5) & 0xF;
2079
        for(j = 0; j < 2; j++) {
2080
            last = subblkpat & (1 << (1 - j));
2081
            i = 0;
2082
            off = j * 4;
2083
            while (!last) {
2084
                vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2085
                i += skip;
2086
                if(i > 31)
2087
                    break;
2088
                idx = v->zz_4x8[i++]+off;
2089
                block[idx] = value * scale;
2090
                if(!v->pquantizer)
2091
                    block[idx] += (block[idx] < 0) ? -mquant : mquant;
2092
            }
2093
            if(!(subblkpat & (1 << (1 - j))) && !skip_block){
2094
                if(i==1)
2095
                    v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j*4, linesize, block + off);
2096
                else
2097
                    v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
2098
                if(apply_filter && cbp_top & (2 << j))
2099
                    v->vc1dsp.vc1_v_loop_filter4(dst + j*4, linesize, v->pq);
2100
                if(apply_filter && j ? pat & 0x5 : (cbp_left & 0xA))
2101
                    v->vc1dsp.vc1_h_loop_filter8(dst + j*4, linesize, v->pq);
2102
            }
2103
        }
2104
        break;
2105
    }
2106
    return pat;
2107
}
2108

    
2109
/** @} */ // Macroblock group
2110

    
2111
static const int size_table  [6] = { 0, 2, 3, 4,  5,  8 };
2112
static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
2113

    
2114
/** Decode one P-frame MB (in Simple/Main profile)
2115
 */
2116
static int vc1_decode_p_mb(VC1Context *v)
2117
{
2118
    MpegEncContext *s = &v->s;
2119
    GetBitContext *gb = &s->gb;
2120
    int i, j;
2121
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2122
    int cbp; /* cbp decoding stuff */
2123
    int mqdiff, mquant; /* MB quantization */
2124
    int ttmb = v->ttfrm; /* MB Transform type */
2125

    
2126
    int mb_has_coeffs = 1; /* last_flag */
2127
    int dmv_x, dmv_y; /* Differential MV components */
2128
    int index, index1; /* LUT indexes */
2129
    int val, sign; /* temp values */
2130
    int first_block = 1;
2131
    int dst_idx, off;
2132
    int skipped, fourmv;
2133
    int block_cbp = 0, pat;
2134
    int apply_loop_filter;
2135

    
2136
    mquant = v->pq; /* Loosy initialization */
2137

    
2138
    if (v->mv_type_is_raw)
2139
        fourmv = get_bits1(gb);
2140
    else
2141
        fourmv = v->mv_type_mb_plane[mb_pos];
2142
    if (v->skip_is_raw)
2143
        skipped = get_bits1(gb);
2144
    else
2145
        skipped = v->s.mbskip_table[mb_pos];
2146

    
2147
    apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY);
2148
    if (!fourmv) /* 1MV mode */
2149
    {
2150
        if (!skipped)
2151
        {
2152
            GET_MVDATA(dmv_x, dmv_y);
2153

    
2154
            if (s->mb_intra) {
2155
                s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2156
                s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2157
            }
2158
            s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
2159
            vc1_pred_mv(s, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
2160

    
2161
            /* FIXME Set DC val for inter block ? */
2162
            if (s->mb_intra && !mb_has_coeffs)
2163
            {
2164
                GET_MQUANT();
2165
                s->ac_pred = get_bits1(gb);
2166
                cbp = 0;
2167
            }
2168
            else if (mb_has_coeffs)
2169
            {
2170
                if (s->mb_intra) s->ac_pred = get_bits1(gb);
2171
                cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2172
                GET_MQUANT();
2173
            }
2174
            else
2175
            {
2176
                mquant = v->pq;
2177
                cbp = 0;
2178
            }
2179
            s->current_picture.qscale_table[mb_pos] = mquant;
2180

    
2181
            if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
2182
                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
2183
                                VC1_TTMB_VLC_BITS, 2);
2184
            if(!s->mb_intra) vc1_mc_1mv(v, 0);
2185
            dst_idx = 0;
2186
            for (i=0; i<6; i++)
2187
            {
2188
                s->dc_val[0][s->block_index[i]] = 0;
2189
                dst_idx += i >> 2;
2190
                val = ((cbp >> (5 - i)) & 1);
2191
                off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2192
                v->mb_type[0][s->block_index[i]] = s->mb_intra;
2193
                if(s->mb_intra) {
2194
                    /* check if prediction blocks A and C are available */
2195
                    v->a_avail = v->c_avail = 0;
2196
                    if(i == 2 || i == 3 || !s->first_slice_line)
2197
                        v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2198
                    if(i == 1 || i == 3 || s->mb_x)
2199
                        v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2200

    
2201
                    vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
2202
                    if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
2203
                    v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2204
                    if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
2205
                    s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
2206
                    if(v->pq >= 9 && v->overlap) {
2207
                        if(v->c_avail)
2208
                            v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
2209
                        if(v->a_avail)
2210
                            v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
2211
                    }
2212
                    if(apply_loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){
2213
                        int left_cbp, top_cbp;
2214
                        if(i & 4){
2215
                            left_cbp = v->cbp[s->mb_x - 1]            >> (i * 4);
2216
                            top_cbp  = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
2217
                        }else{
2218
                            left_cbp = (i & 1) ? (cbp >> ((i-1)*4)) : (v->cbp[s->mb_x - 1]           >> ((i+1)*4));
2219
                            top_cbp  = (i & 2) ? (cbp >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));
2220
                        }
2221
                        if(left_cbp & 0xC)
2222
                            v->vc1dsp.vc1_v_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2223
                        if(top_cbp  & 0xA)
2224
                            v->vc1dsp.vc1_h_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2225
                    }
2226
                    block_cbp |= 0xF << (i << 2);
2227
                } else if(val) {
2228
                    int left_cbp = 0, top_cbp = 0, filter = 0;
2229
                    if(apply_loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){
2230
                        filter = 1;
2231
                        if(i & 4){
2232
                            left_cbp = v->cbp[s->mb_x - 1]            >> (i * 4);
2233
                            top_cbp  = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
2234
                        }else{
2235
                            left_cbp = (i & 1) ? (cbp >> ((i-1)*4)) : (v->cbp[s->mb_x - 1]           >> ((i+1)*4));
2236
                            top_cbp  = (i & 2) ? (cbp >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));
2237
                        }
2238
                        if(left_cbp & 0xC)
2239
                            v->vc1dsp.vc1_v_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2240
                        if(top_cbp  & 0xA)
2241
                            v->vc1dsp.vc1_h_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2242
                    }
2243
                    pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize, (i&4) && (s->flags & CODEC_FLAG_GRAY), filter, left_cbp, top_cbp);
2244
                    block_cbp |= pat << (i << 2);
2245
                    if(!v->ttmbf && ttmb < 8) ttmb = -1;
2246
                    first_block = 0;
2247
                }
2248
            }
2249
        }
2250
        else //Skipped
2251
        {
2252
            s->mb_intra = 0;
2253
            for(i = 0; i < 6; i++) {
2254
                v->mb_type[0][s->block_index[i]] = 0;
2255
                s->dc_val[0][s->block_index[i]] = 0;
2256
            }
2257
            s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
2258
            s->current_picture.qscale_table[mb_pos] = 0;
2259
            vc1_pred_mv(s, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);
2260
            vc1_mc_1mv(v, 0);
2261
            return 0;
2262
        }
2263
    } //1MV mode
2264
    else //4MV mode
2265
    {
2266
        if (!skipped /* unskipped MB */)
2267
        {
2268
            int intra_count = 0, coded_inter = 0;
2269
            int is_intra[6], is_coded[6];
2270
            /* Get CBPCY */
2271
            cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2272
            for (i=0; i<6; i++)
2273
            {
2274
                val = ((cbp >> (5 - i)) & 1);
2275
                s->dc_val[0][s->block_index[i]] = 0;
2276
                s->mb_intra = 0;
2277
                if(i < 4) {
2278
                    dmv_x = dmv_y = 0;
2279
                    s->mb_intra = 0;
2280
                    mb_has_coeffs = 0;
2281
                    if(val) {
2282
                        GET_MVDATA(dmv_x, dmv_y);
2283
                    }
2284
                    vc1_pred_mv(s, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);
2285
                    if(!s->mb_intra) vc1_mc_4mv_luma(v, i);
2286
                    intra_count += s->mb_intra;
2287
                    is_intra[i] = s->mb_intra;
2288
                    is_coded[i] = mb_has_coeffs;
2289
                }
2290
                if(i&4){
2291
                    is_intra[i] = (intra_count >= 3);
2292
                    is_coded[i] = val;
2293
                }
2294
                if(i == 4) vc1_mc_4mv_chroma(v);
2295
                v->mb_type[0][s->block_index[i]] = is_intra[i];
2296
                if(!coded_inter) coded_inter = !is_intra[i] & is_coded[i];
2297
            }
2298
            // if there are no coded blocks then don't do anything more
2299
            if(!intra_count && !coded_inter) return 0;
2300
            dst_idx = 0;
2301
            GET_MQUANT();
2302
            s->current_picture.qscale_table[mb_pos] = mquant;
2303
            /* test if block is intra and has pred */
2304
            {
2305
                int intrapred = 0;
2306
                for(i=0; i<6; i++)
2307
                    if(is_intra[i]) {
2308
                        if(((!s->first_slice_line || (i==2 || i==3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
2309
                            || ((s->mb_x || (i==1 || i==3)) && v->mb_type[0][s->block_index[i] - 1])) {
2310
                            intrapred = 1;
2311
                            break;
2312
                        }
2313
                    }
2314
                if(intrapred)s->ac_pred = get_bits1(gb);
2315
                else s->ac_pred = 0;
2316
            }
2317
            if (!v->ttmbf && coded_inter)
2318
                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2319
            for (i=0; i<6; i++)
2320
            {
2321
                dst_idx += i >> 2;
2322
                off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2323
                s->mb_intra = is_intra[i];
2324
                if (is_intra[i]) {
2325
                    /* check if prediction blocks A and C are available */
2326
                    v->a_avail = v->c_avail = 0;
2327
                    if(i == 2 || i == 3 || !s->first_slice_line)
2328
                        v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2329
                    if(i == 1 || i == 3 || s->mb_x)
2330
                        v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2331

    
2332
                    vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant, (i&4)?v->codingset2:v->codingset);
2333
                    if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
2334
                    v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2335
                    if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
2336
                    s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
2337
                    if(v->pq >= 9 && v->overlap) {
2338
                        if(v->c_avail)
2339
                            v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
2340
                        if(v->a_avail)
2341
                            v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
2342
                    }
2343
                    if(v->s.loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){
2344
                        int left_cbp, top_cbp;
2345
                        if(i & 4){
2346
                            left_cbp = v->cbp[s->mb_x - 1]            >> (i * 4);
2347
                            top_cbp  = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
2348
                        }else{
2349
                            left_cbp = (i & 1) ? (cbp >> ((i-1)*4)) : (v->cbp[s->mb_x - 1]           >> ((i+1)*4));
2350
                            top_cbp  = (i & 2) ? (cbp >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));
2351
                        }
2352
                        if(left_cbp & 0xC)
2353
                            v->vc1dsp.vc1_v_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2354
                        if(top_cbp  & 0xA)
2355
                            v->vc1dsp.vc1_h_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2356
                    }
2357
                    block_cbp |= 0xF << (i << 2);
2358
                } else if(is_coded[i]) {
2359
                    int left_cbp = 0, top_cbp = 0, filter = 0;
2360
                    if(v->s.loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){
2361
                        filter = 1;
2362
                        if(i & 4){
2363
                            left_cbp = v->cbp[s->mb_x - 1]            >> (i * 4);
2364
                            top_cbp  = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
2365
                        }else{
2366
                            left_cbp = (i & 1) ? (cbp >> ((i-1)*4)) : (v->cbp[s->mb_x - 1]           >> ((i+1)*4));
2367
                            top_cbp  = (i & 2) ? (cbp >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));
2368
                        }
2369
                        if(left_cbp & 0xC)
2370
                            v->vc1dsp.vc1_v_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2371
                        if(top_cbp  & 0xA)
2372
                            v->vc1dsp.vc1_h_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2373
                    }
2374
                    pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize, (i&4) && (s->flags & CODEC_FLAG_GRAY), filter, left_cbp, top_cbp);
2375
                    block_cbp |= pat << (i << 2);
2376
                    if(!v->ttmbf && ttmb < 8) ttmb = -1;
2377
                    first_block = 0;
2378
                }
2379
            }
2380
            return 0;
2381
        }
2382
        else //Skipped MB
2383
        {
2384
            s->mb_intra = 0;
2385
            s->current_picture.qscale_table[mb_pos] = 0;
2386
            for (i=0; i<6; i++) {
2387
                v->mb_type[0][s->block_index[i]] = 0;
2388
                s->dc_val[0][s->block_index[i]] = 0;
2389
            }
2390
            for (i=0; i<4; i++)
2391
            {
2392
                vc1_pred_mv(s, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0]);
2393
                vc1_mc_4mv_luma(v, i);
2394
            }
2395
            vc1_mc_4mv_chroma(v);
2396
            s->current_picture.qscale_table[mb_pos] = 0;
2397
            return 0;
2398
        }
2399
    }
2400
    v->cbp[s->mb_x] = block_cbp;
2401

    
2402
    /* Should never happen */
2403
    return -1;
2404
}
2405

    
2406
/** Decode one B-frame MB (in Main profile)
2407
 */
2408
static void vc1_decode_b_mb(VC1Context *v)
2409
{
2410
    MpegEncContext *s = &v->s;
2411
    GetBitContext *gb = &s->gb;
2412
    int i, j;
2413
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2414
    int cbp = 0; /* cbp decoding stuff */
2415
    int mqdiff, mquant; /* MB quantization */
2416
    int ttmb = v->ttfrm; /* MB Transform type */
2417
    int mb_has_coeffs = 0; /* last_flag */
2418
    int index, index1; /* LUT indexes */
2419
    int val, sign; /* temp values */
2420
    int first_block = 1;
2421
    int dst_idx, off;
2422
    int skipped, direct;
2423
    int dmv_x[2], dmv_y[2];
2424
    int bmvtype = BMV_TYPE_BACKWARD;
2425

    
2426
    mquant = v->pq; /* Loosy initialization */
2427
    s->mb_intra = 0;
2428

    
2429
    if (v->dmb_is_raw)
2430
        direct = get_bits1(gb);
2431
    else
2432
        direct = v->direct_mb_plane[mb_pos];
2433
    if (v->skip_is_raw)
2434
        skipped = get_bits1(gb);
2435
    else
2436
        skipped = v->s.mbskip_table[mb_pos];
2437

    
2438
    dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
2439
    for(i = 0; i < 6; i++) {
2440
        v->mb_type[0][s->block_index[i]] = 0;
2441
        s->dc_val[0][s->block_index[i]] = 0;
2442
    }
2443
    s->current_picture.qscale_table[mb_pos] = 0;
2444

    
2445
    if (!direct) {
2446
        if (!skipped) {
2447
            GET_MVDATA(dmv_x[0], dmv_y[0]);
2448
            dmv_x[1] = dmv_x[0];
2449
            dmv_y[1] = dmv_y[0];
2450
        }
2451
        if(skipped || !s->mb_intra) {
2452
            bmvtype = decode012(gb);
2453
            switch(bmvtype) {
2454
            case 0:
2455
                bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
2456
                break;
2457
            case 1:
2458
                bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
2459
                break;
2460
            case 2:
2461
                bmvtype = BMV_TYPE_INTERPOLATED;
2462
                dmv_x[0] = dmv_y[0] = 0;
2463
            }
2464
        }
2465
    }
2466
    for(i = 0; i < 6; i++)
2467
        v->mb_type[0][s->block_index[i]] = s->mb_intra;
2468

    
2469
    if (skipped) {
2470
        if(direct) bmvtype = BMV_TYPE_INTERPOLATED;
2471
        vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2472
        vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2473
        return;
2474
    }
2475
    if (direct) {
2476
        cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2477
        GET_MQUANT();
2478
        s->mb_intra = 0;
2479
        s->current_picture.qscale_table[mb_pos] = mquant;
2480
        if(!v->ttmbf)
2481
            ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2482
        dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
2483
        vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2484
        vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2485
    } else {
2486
        if(!mb_has_coeffs && !s->mb_intra) {
2487
            /* no coded blocks - effectively skipped */
2488
            vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2489
            vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2490
            return;
2491
        }
2492
        if(s->mb_intra && !mb_has_coeffs) {
2493
            GET_MQUANT();
2494
            s->current_picture.qscale_table[mb_pos] = mquant;
2495
            s->ac_pred = get_bits1(gb);
2496
            cbp = 0;
2497
            vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2498
        } else {
2499
            if(bmvtype == BMV_TYPE_INTERPOLATED) {
2500
                GET_MVDATA(dmv_x[0], dmv_y[0]);
2501
                if(!mb_has_coeffs) {
2502
                    /* interpolated skipped block */
2503
                    vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2504
                    vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2505
                    return;
2506
                }
2507
            }
2508
            vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2509
            if(!s->mb_intra) {
2510
                vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2511
            }
2512
            if(s->mb_intra)
2513
                s->ac_pred = get_bits1(gb);
2514
            cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2515
            GET_MQUANT();
2516
            s->current_picture.qscale_table[mb_pos] = mquant;
2517
            if(!v->ttmbf && !s->mb_intra && mb_has_coeffs)
2518
                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2519
        }
2520
    }
2521
    dst_idx = 0;
2522
    for (i=0; i<6; i++)
2523
    {
2524
        s->dc_val[0][s->block_index[i]] = 0;
2525
        dst_idx += i >> 2;
2526
        val = ((cbp >> (5 - i)) & 1);
2527
        off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2528
        v->mb_type[0][s->block_index[i]] = s->mb_intra;
2529
        if(s->mb_intra) {
2530
            /* check if prediction blocks A and C are available */
2531
            v->a_avail = v->c_avail = 0;
2532
            if(i == 2 || i == 3 || !s->first_slice_line)
2533
                v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2534
            if(i == 1 || i == 3 || s->mb_x)
2535
                v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2536

    
2537
            vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
2538
            if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
2539
            v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2540
            if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
2541
            s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
2542
        } else if(val) {
2543
            vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize, (i&4) && (s->flags & CODEC_FLAG_GRAY), 0, 0, 0);
2544
            if(!v->ttmbf && ttmb < 8) ttmb = -1;
2545
            first_block = 0;
2546
        }
2547
    }
2548
}
2549

    
2550
/** Decode blocks of I-frame
2551
 */
2552
static void vc1_decode_i_blocks(VC1Context *v)
2553
{
2554
    int k, j;
2555
    MpegEncContext *s = &v->s;
2556
    int cbp, val;
2557
    uint8_t *coded_val;
2558
    int mb_pos;
2559

    
2560
    /* select codingmode used for VLC tables selection */
2561
    switch(v->y_ac_table_index){
2562
    case 0:
2563
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2564
        break;
2565
    case 1:
2566
        v->codingset = CS_HIGH_MOT_INTRA;
2567
        break;
2568
    case 2:
2569
        v->codingset = CS_MID_RATE_INTRA;
2570
        break;
2571
    }
2572

    
2573
    switch(v->c_ac_table_index){
2574
    case 0:
2575
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2576
        break;
2577
    case 1:
2578
        v->codingset2 = CS_HIGH_MOT_INTER;
2579
        break;
2580
    case 2:
2581
        v->codingset2 = CS_MID_RATE_INTER;
2582
        break;
2583
    }
2584

    
2585
    /* Set DC scale - y and c use the same */
2586
    s->y_dc_scale = s->y_dc_scale_table[v->pq];
2587
    s->c_dc_scale = s->c_dc_scale_table[v->pq];
2588

    
2589
    //do frame decode
2590
    s->mb_x = s->mb_y = 0;
2591
    s->mb_intra = 1;
2592
    s->first_slice_line = 1;
2593
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2594
        s->mb_x = 0;
2595
        ff_init_block_index(s);
2596
        for(; s->mb_x < s->mb_width; s->mb_x++) {
2597
            uint8_t *dst[6];
2598
            ff_update_block_index(s);
2599
            dst[0] = s->dest[0];
2600
            dst[1] = dst[0] + 8;
2601
            dst[2] = s->dest[0] + s->linesize * 8;
2602
            dst[3] = dst[2] + 8;
2603
            dst[4] = s->dest[1];
2604
            dst[5] = s->dest[2];
2605
            s->dsp.clear_blocks(s->block[0]);
2606
            mb_pos = s->mb_x + s->mb_y * s->mb_width;
2607
            s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2608
            s->current_picture.qscale_table[mb_pos] = v->pq;
2609
            s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2610
            s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2611

    
2612
            // do actual MB decoding and displaying
2613
            cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2614
            v->s.ac_pred = get_bits1(&v->s.gb);
2615

    
2616
            for(k = 0; k < 6; k++) {
2617
                val = ((cbp >> (5 - k)) & 1);
2618

    
2619
                if (k < 4) {
2620
                    int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2621
                    val = val ^ pred;
2622
                    *coded_val = val;
2623
                }
2624
                cbp |= val << (5 - k);
2625

    
2626
                vc1_decode_i_block(v, s->block[k], k, val, (k<4)? v->codingset : v->codingset2);
2627

    
2628
                if (k > 3 && (s->flags & CODEC_FLAG_GRAY)) continue;
2629
                v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
2630
                if(v->pq >= 9 && v->overlap) {
2631
                    if (v->rangeredfrm) for(j = 0; j < 64; j++) s->block[k][j] <<= 1;
2632
                    s->dsp.put_signed_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
2633
                } else {
2634
                    if (v->rangeredfrm) for(j = 0; j < 64; j++) s->block[k][j] = (s->block[k][j] - 64) << 1;
2635
                    s->dsp.put_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
2636
                }
2637
            }
2638

    
2639
            if(v->pq >= 9 && v->overlap) {
2640
                if(s->mb_x) {
2641
                    v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
2642
                    v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2643
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
2644
                        v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
2645
                        v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
2646
                    }
2647
                }
2648
                v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
2649
                v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2650
                if(!s->first_slice_line) {
2651
                    v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
2652
                    v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
2653
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
2654
                        v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
2655
                        v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
2656
                    }
2657
                }
2658
                v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2659
                v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2660
            }
2661
            if(v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
2662

    
2663
            if(get_bits_count(&s->gb) > v->bits) {
2664
                ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
2665
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
2666
                return;
2667
            }
2668
        }
2669
        if (!v->s.loop_filter)
2670
            ff_draw_horiz_band(s, s->mb_y * 16, 16);
2671
        else if (s->mb_y)
2672
            ff_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
2673

    
2674
        s->first_slice_line = 0;
2675
    }
2676
    if (v->s.loop_filter)
2677
        ff_draw_horiz_band(s, (s->mb_height-1)*16, 16);
2678
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2679
}
2680

    
2681
/** Decode blocks of I-frame for advanced profile
2682
 */
2683
static void vc1_decode_i_blocks_adv(VC1Context *v)
2684
{
2685
    int k;
2686
    MpegEncContext *s = &v->s;
2687
    int cbp, val;
2688
    uint8_t *coded_val;
2689
    int mb_pos;
2690
    int mquant = v->pq;
2691
    int mqdiff;
2692
    int overlap;
2693
    GetBitContext *gb = &s->gb;
2694

    
2695
    /* select codingmode used for VLC tables selection */
2696
    switch(v->y_ac_table_index){
2697
    case 0:
2698
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2699
        break;
2700
    case 1:
2701
        v->codingset = CS_HIGH_MOT_INTRA;
2702
        break;
2703
    case 2:
2704
        v->codingset = CS_MID_RATE_INTRA;
2705
        break;
2706
    }
2707

    
2708
    switch(v->c_ac_table_index){
2709
    case 0:
2710
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2711
        break;
2712
    case 1:
2713
        v->codingset2 = CS_HIGH_MOT_INTER;
2714
        break;
2715
    case 2:
2716
        v->codingset2 = CS_MID_RATE_INTER;
2717
        break;
2718
    }
2719

    
2720
    //do frame decode
2721
    s->mb_x = s->mb_y = 0;
2722
    s->mb_intra = 1;
2723
    s->first_slice_line = 1;
2724
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2725
        s->mb_x = 0;
2726
        ff_init_block_index(s);
2727
        for(;s->mb_x < s->mb_width; s->mb_x++) {
2728
            uint8_t *dst[6];
2729
            ff_update_block_index(s);
2730
            dst[0] = s->dest[0];
2731
            dst[1] = dst[0] + 8;
2732
            dst[2] = s->dest[0] + s->linesize * 8;
2733
            dst[3] = dst[2] + 8;
2734
            dst[4] = s->dest[1];
2735
            dst[5] = s->dest[2];
2736
            s->dsp.clear_blocks(s->block[0]);
2737
            mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2738
            s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2739
            s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2740
            s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2741

    
2742
            // do actual MB decoding and displaying
2743
            cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2744
            if(v->acpred_is_raw)
2745
                v->s.ac_pred = get_bits1(&v->s.gb);
2746
            else
2747
                v->s.ac_pred = v->acpred_plane[mb_pos];
2748

    
2749
            if(v->condover == CONDOVER_SELECT) {
2750
                if(v->overflg_is_raw)
2751
                    overlap = get_bits1(&v->s.gb);
2752
                else
2753
                    overlap = v->over_flags_plane[mb_pos];
2754
            } else
2755
                overlap = (v->condover == CONDOVER_ALL);
2756

    
2757
            GET_MQUANT();
2758

    
2759
            s->current_picture.qscale_table[mb_pos] = mquant;
2760
            /* Set DC scale - y and c use the same */
2761
            s->y_dc_scale = s->y_dc_scale_table[mquant];
2762
            s->c_dc_scale = s->c_dc_scale_table[mquant];
2763

    
2764
            for(k = 0; k < 6; k++) {
2765
                val = ((cbp >> (5 - k)) & 1);
2766

    
2767
                if (k < 4) {
2768
                    int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2769
                    val = val ^ pred;
2770
                    *coded_val = val;
2771
                }
2772
                cbp |= val << (5 - k);
2773

    
2774
                v->a_avail = !s->first_slice_line || (k==2 || k==3);
2775
                v->c_avail = !!s->mb_x || (k==1 || k==3);
2776

    
2777
                vc1_decode_i_block_adv(v, s->block[k], k, val, (k<4)? v->codingset : v->codingset2, mquant);
2778

    
2779
                if (k > 3 && (s->flags & CODEC_FLAG_GRAY)) continue;
2780
                v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
2781
                s->dsp.put_signed_pixels_clamped(s->block[k], dst[k],
2782
                                                 k & 4 ? s->uvlinesize : s->linesize);
2783
            }
2784

    
2785
            if(overlap) {
2786
                if(s->mb_x) {
2787
                    v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
2788
                    v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2789
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
2790
                        v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
2791
                        v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
2792
                    }
2793
                }
2794
                v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
2795
                v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2796
                if(!s->first_slice_line) {
2797
                    v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
2798
                    v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
2799
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
2800
                        v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
2801
                        v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
2802
                    }
2803
                }
2804
                v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2805
                v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2806
            }
2807
            if(v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
2808

    
2809
            if(get_bits_count(&s->gb) > v->bits) {
2810
                ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
2811
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
2812
                return;
2813
            }
2814
        }
2815
        if (!v->s.loop_filter)
2816
            ff_draw_horiz_band(s, s->mb_y * 16, 16);
2817
        else if (s->mb_y)
2818
            ff_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
2819
        s->first_slice_line = 0;
2820
    }
2821
    if (v->s.loop_filter)
2822
        ff_draw_horiz_band(s, (s->mb_height-1)*16, 16);
2823
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2824
}
2825

    
2826
static void vc1_decode_p_blocks(VC1Context *v)
2827
{
2828
    MpegEncContext *s = &v->s;
2829

    
2830
    /* select codingmode used for VLC tables selection */
2831
    switch(v->c_ac_table_index){
2832
    case 0:
2833
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2834
        break;
2835
    case 1:
2836
        v->codingset = CS_HIGH_MOT_INTRA;
2837
        break;
2838
    case 2:
2839
        v->codingset = CS_MID_RATE_INTRA;
2840
        break;
2841
    }
2842

    
2843
    switch(v->c_ac_table_index){
2844
    case 0:
2845
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2846
        break;
2847
    case 1:
2848
        v->codingset2 = CS_HIGH_MOT_INTER;
2849
        break;
2850
    case 2:
2851
        v->codingset2 = CS_MID_RATE_INTER;
2852
        break;
2853
    }
2854

    
2855
    s->first_slice_line = 1;
2856
    memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
2857
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2858
        s->mb_x = 0;
2859
        ff_init_block_index(s);
2860
        for(; s->mb_x < s->mb_width; s->mb_x++) {
2861
            ff_update_block_index(s);
2862

    
2863
            vc1_decode_p_mb(v);
2864
            if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2865
                ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
2866
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n", get_bits_count(&s->gb), v->bits,s->mb_x,s->mb_y);
2867
                return;
2868
            }
2869
        }
2870
        memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0])*s->mb_stride);
2871
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
2872
        s->first_slice_line = 0;
2873
    }
2874
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2875
}
2876

    
2877
static void vc1_decode_b_blocks(VC1Context *v)
2878
{
2879
    MpegEncContext *s = &v->s;
2880

    
2881
    /* select codingmode used for VLC tables selection */
2882
    switch(v->c_ac_table_index){
2883
    case 0:
2884
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2885
        break;
2886
    case 1:
2887
        v->codingset = CS_HIGH_MOT_INTRA;
2888
        break;
2889
    case 2:
2890
        v->codingset = CS_MID_RATE_INTRA;
2891
        break;
2892
    }
2893

    
2894
    switch(v->c_ac_table_index){
2895
    case 0:
2896
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2897
        break;
2898
    case 1:
2899
        v->codingset2 = CS_HIGH_MOT_INTER;
2900
        break;
2901
    case 2:
2902
        v->codingset2 = CS_MID_RATE_INTER;
2903
        break;
2904
    }
2905

    
2906
    s->first_slice_line = 1;
2907
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2908
        s->mb_x = 0;
2909
        ff_init_block_index(s);
2910
        for(; s->mb_x < s->mb_width; s->mb_x++) {
2911
            ff_update_block_index(s);
2912

    
2913
            vc1_decode_b_mb(v);
2914
            if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2915
                ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
2916
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n", get_bits_count(&s->gb), v->bits,s->mb_x,s->mb_y);
2917
                return;
2918
            }
2919
            if(v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
2920
        }
2921
        if (!v->s.loop_filter)
2922
            ff_draw_horiz_band(s, s->mb_y * 16, 16);
2923
        else if (s->mb_y)
2924
            ff_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
2925
        s->first_slice_line = 0;
2926
    }
2927
    if (v->s.loop_filter)
2928
        ff_draw_horiz_band(s, (s->mb_height-1)*16, 16);
2929
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2930
}
2931

    
2932
static void vc1_decode_skip_blocks(VC1Context *v)
2933
{
2934
    MpegEncContext *s = &v->s;
2935

    
2936
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2937
    s->first_slice_line = 1;
2938
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2939
        s->mb_x = 0;
2940
        ff_init_block_index(s);
2941
        ff_update_block_index(s);
2942
        memcpy(s->dest[0], s->last_picture.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
2943
        memcpy(s->dest[1], s->last_picture.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
2944
        memcpy(s->dest[2], s->last_picture.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
2945
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
2946
        s->first_slice_line = 0;
2947
    }
2948
    s->pict_type = FF_P_TYPE;
2949
}
2950

    
2951
static void vc1_decode_blocks(VC1Context *v)
2952
{
2953

    
2954
    v->s.esc3_level_length = 0;
2955
    if(v->x8_type){
2956
        ff_intrax8_decode_picture(&v->x8, 2*v->pq+v->halfpq, v->pq*(!v->pquantizer) );
2957
    }else{
2958

    
2959
        switch(v->s.pict_type) {
2960
        case FF_I_TYPE:
2961
            if(v->profile == PROFILE_ADVANCED)
2962
                vc1_decode_i_blocks_adv(v);
2963
            else
2964
                vc1_decode_i_blocks(v);
2965
            break;
2966
        case FF_P_TYPE:
2967
            if(v->p_frame_skipped)
2968
                vc1_decode_skip_blocks(v);
2969
            else
2970
                vc1_decode_p_blocks(v);
2971
            break;
2972
        case FF_B_TYPE:
2973
            if(v->bi_type){
2974
                if(v->profile == PROFILE_ADVANCED)
2975
                    vc1_decode_i_blocks_adv(v);
2976
                else
2977
                    vc1_decode_i_blocks(v);
2978
            }else
2979
                vc1_decode_b_blocks(v);
2980
            break;
2981
        }
2982
    }
2983
}
2984

    
2985
/** Initialize a VC1/WMV3 decoder
2986
 * @todo TODO: Handle VC-1 IDUs (Transport level?)
2987
 * @todo TODO: Decypher remaining bits in extra_data
2988
 */
2989
static av_cold int vc1_decode_init(AVCodecContext *avctx)
2990
{
2991
    VC1Context *v = avctx->priv_data;
2992
    MpegEncContext *s = &v->s;
2993
    GetBitContext gb;
2994
    int i;
2995

    
2996
    if (!avctx->extradata_size || !avctx->extradata) return -1;
2997
    if (!(avctx->flags & CODEC_FLAG_GRAY))
2998
        avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
2999
    else
3000
        avctx->pix_fmt = PIX_FMT_GRAY8;
3001
    avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
3002
    v->s.avctx = avctx;
3003
    avctx->flags |= CODEC_FLAG_EMU_EDGE;
3004
    v->s.flags |= CODEC_FLAG_EMU_EDGE;
3005

    
3006
    if(avctx->idct_algo==FF_IDCT_AUTO){
3007
        avctx->idct_algo=FF_IDCT_WMV2;
3008
    }
3009

    
3010
    if(ff_msmpeg4_decode_init(avctx) < 0)
3011
        return -1;
3012
    if (vc1_init_common(v) < 0) return -1;
3013
    ff_vc1dsp_init(&v->vc1dsp);
3014
    for (i = 0; i < 64;  i++) {
3015
#define transpose(x) ((x>>3) | ((x&7)<<3))
3016
        v->zz_8x8[0][i] = transpose(wmv1_scantable[0][i]);
3017
        v->zz_8x8[1][i] = transpose(wmv1_scantable[1][i]);
3018
        v->zz_8x8[2][i] = transpose(wmv1_scantable[2][i]);
3019
        v->zz_8x8[3][i] = transpose(wmv1_scantable[3][i]);
3020
    }
3021

    
3022
    avctx->coded_width = avctx->width;
3023
    avctx->coded_height = avctx->height;
3024
    if (avctx->codec_id == CODEC_ID_WMV3)
3025
    {
3026
        int count = 0;
3027

    
3028
        // looks like WMV3 has a sequence header stored in the extradata
3029
        // advanced sequence header may be before the first frame
3030
        // the last byte of the extradata is a version number, 1 for the
3031
        // samples we can decode
3032

    
3033
        init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
3034

    
3035
        if (vc1_decode_sequence_header(avctx, v, &gb) < 0)
3036
          return -1;
3037

    
3038
        count = avctx->extradata_size*8 - get_bits_count(&gb);
3039
        if (count>0)
3040
        {
3041
            av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
3042
                   count, get_bits(&gb, count));
3043
        }
3044
        else if (count < 0)
3045
        {
3046
            av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
3047
        }
3048
    } else { // VC1/WVC1
3049
        const uint8_t *start = avctx->extradata;
3050
        uint8_t *end = avctx->extradata + avctx->extradata_size;
3051
        const uint8_t *next;
3052
        int size, buf2_size;
3053
        uint8_t *buf2 = NULL;
3054
        int seq_initialized = 0, ep_initialized = 0;
3055

    
3056
        if(avctx->extradata_size < 16) {
3057
            av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
3058
            return -1;
3059
        }
3060

    
3061
        buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
3062
        start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
3063
        next = start;
3064
        for(; next < end; start = next){
3065
            next = find_next_marker(start + 4, end);
3066
            size = next - start - 4;
3067
            if(size <= 0) continue;
3068
            buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
3069
            init_get_bits(&gb, buf2, buf2_size * 8);
3070
            switch(AV_RB32(start)){
3071
            case VC1_CODE_SEQHDR:
3072
                if(vc1_decode_sequence_header(avctx, v, &gb) < 0){
3073
                    av_free(buf2);
3074
                    return -1;
3075
                }
3076
                seq_initialized = 1;
3077
                break;
3078
            case VC1_CODE_ENTRYPOINT:
3079
                if(vc1_decode_entry_point(avctx, v, &gb) < 0){
3080
                    av_free(buf2);
3081
                    return -1;
3082
                }
3083
                ep_initialized = 1;
3084
                break;
3085
            }
3086
        }
3087
        av_free(buf2);
3088
        if(!seq_initialized || !ep_initialized){
3089
            av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
3090
            return -1;
3091
        }
3092
    }
3093
    avctx->has_b_frames= !!(avctx->max_b_frames);
3094
    s->low_delay = !avctx->has_b_frames;
3095

    
3096
    s->mb_width = (avctx->coded_width+15)>>4;
3097
    s->mb_height = (avctx->coded_height+15)>>4;
3098

    
3099
    /* Allocate mb bitplanes */
3100
    v->mv_type_mb_plane = av_malloc(s->mb_stride * s->mb_height);
3101
    v->direct_mb_plane = av_malloc(s->mb_stride * s->mb_height);
3102
    v->acpred_plane = av_malloc(s->mb_stride * s->mb_height);
3103
    v->over_flags_plane = av_malloc(s->mb_stride * s->mb_height);
3104

    
3105
    v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
3106
    v->cbp = v->cbp_base + s->mb_stride;
3107

    
3108
    /* allocate block type info in that way so it could be used with s->block_index[] */
3109
    v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
3110
    v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
3111
    v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
3112
    v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
3113

    
3114
    /* Init coded blocks info */
3115
    if (v->profile == PROFILE_ADVANCED)
3116
    {
3117
//        if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
3118
//            return -1;
3119
//        if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
3120
//            return -1;
3121
    }
3122

    
3123
    ff_intrax8_common_init(&v->x8,s);
3124
    return 0;
3125
}
3126

    
3127

    
3128
/** Decode a VC1/WMV3 frame
3129
 * @todo TODO: Handle VC-1 IDUs (Transport level?)
3130
 */
3131
static int vc1_decode_frame(AVCodecContext *avctx,
3132
                            void *data, int *data_size,
3133
                            AVPacket *avpkt)
3134
{
3135
    const uint8_t *buf = avpkt->data;
3136
    int buf_size = avpkt->size;
3137
    VC1Context *v = avctx->priv_data;
3138
    MpegEncContext *s = &v->s;
3139
    AVFrame *pict = data;
3140
    uint8_t *buf2 = NULL;
3141
    const uint8_t *buf_start = buf;
3142

    
3143
    /* no supplementary picture */
3144
    if (buf_size == 0) {
3145
        /* special case for last picture */
3146
        if (s->low_delay==0 && s->next_picture_ptr) {
3147
            *pict= *(AVFrame*)s->next_picture_ptr;
3148
            s->next_picture_ptr= NULL;
3149

    
3150
            *data_size = sizeof(AVFrame);
3151
        }
3152

    
3153
        return 0;
3154
    }
3155

    
3156
    /* We need to set current_picture_ptr before reading the header,
3157
     * otherwise we cannot store anything in there. */
3158
    if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
3159
        int i= ff_find_unused_picture(s, 0);
3160
        s->current_picture_ptr= &s->picture[i];
3161
    }
3162

    
3163
    if (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){
3164
        if (v->profile < PROFILE_ADVANCED)
3165
            avctx->pix_fmt = PIX_FMT_VDPAU_WMV3;
3166
        else
3167
            avctx->pix_fmt = PIX_FMT_VDPAU_VC1;
3168
    }
3169

    
3170
    //for advanced profile we may need to parse and unescape data
3171
    if (avctx->codec_id == CODEC_ID_VC1) {
3172
        int buf_size2 = 0;
3173
        buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
3174

    
3175
        if(IS_MARKER(AV_RB32(buf))){ /* frame starts with marker and needs to be parsed */
3176
            const uint8_t *start, *end, *next;
3177
            int size;
3178

    
3179
            next = buf;
3180
            for(start = buf, end = buf + buf_size; next < end; start = next){
3181
                next = find_next_marker(start + 4, end);
3182
                size = next - start - 4;
3183
                if(size <= 0) continue;
3184
                switch(AV_RB32(start)){
3185
                case VC1_CODE_FRAME:
3186
                    if (avctx->hwaccel ||
3187
                        s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
3188
                        buf_start = start;
3189
                    buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
3190
                    break;
3191
                case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
3192
                    buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
3193
                    init_get_bits(&s->gb, buf2, buf_size2*8);
3194
                    vc1_decode_entry_point(avctx, v, &s->gb);
3195
                    break;
3196
                case VC1_CODE_SLICE:
3197
                    av_log(avctx, AV_LOG_ERROR, "Sliced decoding is not implemented (yet)\n");
3198
                    av_free(buf2);
3199
                    return -1;
3200
                }
3201
            }
3202
        }else if(v->interlace && ((buf[0] & 0xC0) == 0xC0)){ /* WVC1 interlaced stores both fields divided by marker */
3203
            const uint8_t *divider;
3204

    
3205
            divider = find_next_marker(buf, buf + buf_size);
3206
            if((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD){
3207
                av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
3208
                av_free(buf2);
3209
                return -1;
3210
            }
3211

    
3212
            buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
3213
            // TODO
3214
            if(!v->warn_interlaced++)
3215
                av_log(v->s.avctx, AV_LOG_ERROR, "Interlaced WVC1 support is not implemented\n");
3216
            av_free(buf2);return -1;
3217
        }else{
3218
            buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
3219
        }
3220
        init_get_bits(&s->gb, buf2, buf_size2*8);
3221
    } else
3222
        init_get_bits(&s->gb, buf, buf_size*8);
3223
    // do parse frame header
3224
    if(v->profile < PROFILE_ADVANCED) {
3225
        if(vc1_parse_frame_header(v, &s->gb) == -1) {
3226
            av_free(buf2);
3227
            return -1;
3228
        }
3229
    } else {
3230
        if(vc1_parse_frame_header_adv(v, &s->gb) == -1) {
3231
            av_free(buf2);
3232
            return -1;
3233
        }
3234
    }
3235

    
3236
    if(v->res_sprite && (s->pict_type!=FF_I_TYPE)){
3237
        av_free(buf2);
3238
        return -1;
3239
    }
3240

    
3241
    // for hurry_up==5
3242
    s->current_picture.pict_type= s->pict_type;
3243
    s->current_picture.key_frame= s->pict_type == FF_I_TYPE;
3244

    
3245
    /* skip B-frames if we don't have reference frames */
3246
    if(s->last_picture_ptr==NULL && (s->pict_type==FF_B_TYPE || s->dropable)){
3247
        av_free(buf2);
3248
        return -1;//buf_size;
3249
    }
3250
    /* skip b frames if we are in a hurry */
3251
    if(avctx->hurry_up && s->pict_type==FF_B_TYPE) return -1;//buf_size;
3252
    if(   (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==FF_B_TYPE)
3253
       || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=FF_I_TYPE)
3254
       ||  avctx->skip_frame >= AVDISCARD_ALL) {
3255
        av_free(buf2);
3256
        return buf_size;
3257
    }
3258
    /* skip everything if we are in a hurry>=5 */
3259
    if(avctx->hurry_up>=5) {
3260
        av_free(buf2);
3261
        return -1;//buf_size;
3262
    }
3263

    
3264
    if(s->next_p_frame_damaged){
3265
        if(s->pict_type==FF_B_TYPE)
3266
            return buf_size;
3267
        else
3268
            s->next_p_frame_damaged=0;
3269
    }
3270

    
3271
    if(MPV_frame_start(s, avctx) < 0) {
3272
        av_free(buf2);
3273
        return -1;
3274
    }
3275

    
3276
    s->me.qpel_put= s->dsp.put_qpel_pixels_tab;
3277
    s->me.qpel_avg= s->dsp.avg_qpel_pixels_tab;
3278

    
3279
    if ((CONFIG_VC1_VDPAU_DECODER)
3280
        &&s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
3281
        ff_vdpau_vc1_decode_picture(s, buf_start, (buf + buf_size) - buf_start);
3282
    else if (avctx->hwaccel) {
3283
        if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
3284
            return -1;
3285
        if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
3286
            return -1;
3287
        if (avctx->hwaccel->end_frame(avctx) < 0)
3288
            return -1;
3289
    } else {
3290
        ff_er_frame_start(s);
3291

    
3292
        v->bits = buf_size * 8;
3293
        vc1_decode_blocks(v);
3294
//av_log(s->avctx, AV_LOG_INFO, "Consumed %i/%i bits\n", get_bits_count(&s->gb), buf_size*8);
3295
//  if(get_bits_count(&s->gb) > buf_size * 8)
3296
//      return -1;
3297
        ff_er_frame_end(s);
3298
    }
3299

    
3300
    MPV_frame_end(s);
3301

    
3302
assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
3303
assert(s->current_picture.pict_type == s->pict_type);
3304
    if (s->pict_type == FF_B_TYPE || s->low_delay) {
3305
        *pict= *(AVFrame*)s->current_picture_ptr;
3306
    } else if (s->last_picture_ptr != NULL) {
3307
        *pict= *(AVFrame*)s->last_picture_ptr;
3308
    }
3309

    
3310
    if(s->last_picture_ptr || s->low_delay){
3311
        *data_size = sizeof(AVFrame);
3312
        ff_print_debug_info(s, pict);
3313
    }
3314

    
3315
    av_free(buf2);
3316
    return buf_size;
3317
}
3318

    
3319

    
3320
/** Close a VC1/WMV3 decoder
3321
 * @warning Initial try at using MpegEncContext stuff
3322
 */
3323
static av_cold int vc1_decode_end(AVCodecContext *avctx)
3324
{
3325
    VC1Context *v = avctx->priv_data;
3326

    
3327
    av_freep(&v->hrd_rate);
3328
    av_freep(&v->hrd_buffer);
3329
    MPV_common_end(&v->s);
3330
    av_freep(&v->mv_type_mb_plane);
3331
    av_freep(&v->direct_mb_plane);
3332
    av_freep(&v->acpred_plane);
3333
    av_freep(&v->over_flags_plane);
3334
    av_freep(&v->mb_type_base);
3335
    av_freep(&v->cbp_base);
3336
    ff_intrax8_common_end(&v->x8);
3337
    return 0;
3338
}
3339

    
3340

    
3341
AVCodec ff_vc1_decoder = {
3342
    "vc1",
3343
    AVMEDIA_TYPE_VIDEO,
3344
    CODEC_ID_VC1,
3345
    sizeof(VC1Context),
3346
    vc1_decode_init,
3347
    NULL,
3348
    vc1_decode_end,
3349
    vc1_decode_frame,
3350
    CODEC_CAP_DR1 | CODEC_CAP_DELAY,
3351
    NULL,
3352
    .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
3353
    .pix_fmts = ff_hwaccel_pixfmt_list_420
3354
};
3355

    
3356
#if CONFIG_WMV3_DECODER
3357
AVCodec ff_wmv3_decoder = {
3358
    "wmv3",
3359
    AVMEDIA_TYPE_VIDEO,
3360
    CODEC_ID_WMV3,
3361
    sizeof(VC1Context),
3362
    vc1_decode_init,
3363
    NULL,
3364
    vc1_decode_end,
3365
    vc1_decode_frame,
3366
    CODEC_CAP_DR1 | CODEC_CAP_DELAY,
3367
    NULL,
3368
    .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
3369
    .pix_fmts = ff_hwaccel_pixfmt_list_420
3370
};
3371
#endif
3372

    
3373
#if CONFIG_WMV3_VDPAU_DECODER
3374
AVCodec ff_wmv3_vdpau_decoder = {
3375
    "wmv3_vdpau",
3376
    AVMEDIA_TYPE_VIDEO,
3377
    CODEC_ID_WMV3,
3378
    sizeof(VC1Context),
3379
    vc1_decode_init,
3380
    NULL,
3381
    vc1_decode_end,
3382
    vc1_decode_frame,
3383
    CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
3384
    NULL,
3385
    .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
3386
    .pix_fmts = (const enum PixelFormat[]){PIX_FMT_VDPAU_WMV3, PIX_FMT_NONE}
3387
};
3388
#endif
3389

    
3390
#if CONFIG_VC1_VDPAU_DECODER
3391
AVCodec ff_vc1_vdpau_decoder = {
3392
    "vc1_vdpau",
3393
    AVMEDIA_TYPE_VIDEO,
3394
    CODEC_ID_VC1,
3395
    sizeof(VC1Context),
3396
    vc1_decode_init,
3397
    NULL,
3398
    vc1_decode_end,
3399
    vc1_decode_frame,
3400
    CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
3401
    NULL,
3402
    .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
3403
    .pix_fmts = (const enum PixelFormat[]){PIX_FMT_VDPAU_VC1, PIX_FMT_NONE}
3404
};
3405
#endif