Statistics
| Branch: | Revision:

ffmpeg / libavcodec / vc1dec.c @ 95ce961d

History | View | Annotate | Download (122 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 libavcodec/vc1dec.c
25
 * VC-1 and WMV3 decoder
26
 *
27
 */
28
#include "internal.h"
29
#include "dsputil.h"
30
#include "avcodec.h"
31
#include "mpegvideo.h"
32
#include "vc1.h"
33
#include "vc1data.h"
34
#include "vc1acdata.h"
35
#include "msmpeg4data.h"
36
#include "unary.h"
37
#include "simple_idct.h"
38
#include "mathops.h"
39
#include "vdpau_internal.h"
40

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

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

    
49

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

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

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

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

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

    
134
    return 0;
135
}
136

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

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

    
159

    
160
/** @} */ //Bitplane group
161

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

    
178
/** Put block onto picture
179
 */
180
static void vc1_put_block(VC1Context *v, DCTELEM block[6][64])
181
{
182
    uint8_t *Y;
183
    int ys, us, vs;
184
    DSPContext *dsp = &v->s.dsp;
185

    
186
    if(v->rangeredfrm) {
187
        int i, j, k;
188
        for(k = 0; k < 6; k++)
189
            for(j = 0; j < 8; j++)
190
                for(i = 0; i < 8; i++)
191
                    block[k][i + j*8] = ((block[k][i + j*8] - 128) << 1) + 128;
192

    
193
    }
194
    ys = v->s.current_picture.linesize[0];
195
    us = v->s.current_picture.linesize[1];
196
    vs = v->s.current_picture.linesize[2];
197
    Y = v->s.dest[0];
198

    
199
    dsp->put_pixels_clamped(block[0], Y, ys);
200
    dsp->put_pixels_clamped(block[1], Y + 8, ys);
201
    Y += ys * 8;
202
    dsp->put_pixels_clamped(block[2], Y, ys);
203
    dsp->put_pixels_clamped(block[3], Y + 8, ys);
204

    
205
    if(!(v->s.flags & CODEC_FLAG_GRAY)) {
206
        dsp->put_pixels_clamped(block[4], v->s.dest[1], us);
207
        dsp->put_pixels_clamped(block[5], v->s.dest[2], vs);
208
    }
209
}
210

    
211
/** Do motion compensation over 1 macroblock
212
 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
213
 */
214
static void vc1_mc_1mv(VC1Context *v, int dir)
215
{
216
    MpegEncContext *s = &v->s;
217
    DSPContext *dsp = &v->s.dsp;
218
    uint8_t *srcY, *srcU, *srcV;
219
    int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
220

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

    
223
    mx = s->mv[dir][0][0];
224
    my = s->mv[dir][0][1];
225

    
226
    // store motion vectors for further use in B frames
227
    if(s->pict_type == FF_P_TYPE) {
228
        s->current_picture.motion_val[1][s->block_index[0]][0] = mx;
229
        s->current_picture.motion_val[1][s->block_index[0]][1] = my;
230
    }
231
    uvmx = (mx + ((mx & 3) == 3)) >> 1;
232
    uvmy = (my + ((my & 3) == 3)) >> 1;
233
    if(v->fastuvmc) {
234
        uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
235
        uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
236
    }
237
    if(!dir) {
238
        srcY = s->last_picture.data[0];
239
        srcU = s->last_picture.data[1];
240
        srcV = s->last_picture.data[2];
241
    } else {
242
        srcY = s->next_picture.data[0];
243
        srcU = s->next_picture.data[1];
244
        srcV = s->next_picture.data[2];
245
    }
246

    
247
    src_x = s->mb_x * 16 + (mx >> 2);
248
    src_y = s->mb_y * 16 + (my >> 2);
249
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
250
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
251

    
252
    if(v->profile != PROFILE_ADVANCED){
253
        src_x   = av_clip(  src_x, -16, s->mb_width  * 16);
254
        src_y   = av_clip(  src_y, -16, s->mb_height * 16);
255
        uvsrc_x = av_clip(uvsrc_x,  -8, s->mb_width  *  8);
256
        uvsrc_y = av_clip(uvsrc_y,  -8, s->mb_height *  8);
257
    }else{
258
        src_x   = av_clip(  src_x, -17, s->avctx->coded_width);
259
        src_y   = av_clip(  src_y, -18, s->avctx->coded_height + 1);
260
        uvsrc_x = av_clip(uvsrc_x,  -8, s->avctx->coded_width  >> 1);
261
        uvsrc_y = av_clip(uvsrc_y,  -8, s->avctx->coded_height >> 1);
262
    }
263

    
264
    srcY += src_y * s->linesize + src_x;
265
    srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
266
    srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
267

    
268
    /* for grayscale we should not try to read from unknown area */
269
    if(s->flags & CODEC_FLAG_GRAY) {
270
        srcU = s->edge_emu_buffer + 18 * s->linesize;
271
        srcV = s->edge_emu_buffer + 18 * s->linesize;
272
    }
273

    
274
    if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
275
       || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel*3
276
       || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 16 - s->mspel*3){
277
        uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
278

    
279
        srcY -= s->mspel * (1 + s->linesize);
280
        ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2,
281
                            src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
282
        srcY = s->edge_emu_buffer;
283
        ff_emulated_edge_mc(uvbuf     , srcU, s->uvlinesize, 8+1, 8+1,
284
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
285
        ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
286
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
287
        srcU = uvbuf;
288
        srcV = uvbuf + 16;
289
        /* if we deal with range reduction we need to scale source blocks */
290
        if(v->rangeredfrm) {
291
            int i, j;
292
            uint8_t *src, *src2;
293

    
294
            src = srcY;
295
            for(j = 0; j < 17 + s->mspel*2; j++) {
296
                for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
297
                src += s->linesize;
298
            }
299
            src = srcU; src2 = srcV;
300
            for(j = 0; j < 9; j++) {
301
                for(i = 0; i < 9; i++) {
302
                    src[i] = ((src[i] - 128) >> 1) + 128;
303
                    src2[i] = ((src2[i] - 128) >> 1) + 128;
304
                }
305
                src += s->uvlinesize;
306
                src2 += s->uvlinesize;
307
            }
308
        }
309
        /* if we deal with intensity compensation we need to scale source blocks */
310
        if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
311
            int i, j;
312
            uint8_t *src, *src2;
313

    
314
            src = srcY;
315
            for(j = 0; j < 17 + s->mspel*2; j++) {
316
                for(i = 0; i < 17 + s->mspel*2; i++) src[i] = v->luty[src[i]];
317
                src += s->linesize;
318
            }
319
            src = srcU; src2 = srcV;
320
            for(j = 0; j < 9; j++) {
321
                for(i = 0; i < 9; i++) {
322
                    src[i] = v->lutuv[src[i]];
323
                    src2[i] = v->lutuv[src2[i]];
324
                }
325
                src += s->uvlinesize;
326
                src2 += s->uvlinesize;
327
            }
328
        }
329
        srcY += s->mspel * (1 + s->linesize);
330
    }
331

    
332
    if(s->mspel) {
333
        dxy = ((my & 3) << 2) | (mx & 3);
334
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0]    , srcY    , s->linesize, v->rnd);
335
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
336
        srcY += s->linesize * 8;
337
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize    , srcY    , s->linesize, v->rnd);
338
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
339
    } else { // hpel mc - always used for luma
340
        dxy = (my & 2) | ((mx & 2) >> 1);
341

    
342
        if(!v->rnd)
343
            dsp->put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
344
        else
345
            dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
346
    }
347

    
348
    if(s->flags & CODEC_FLAG_GRAY) return;
349
    /* Chroma MC always uses qpel bilinear */
350
    uvmx = (uvmx&3)<<1;
351
    uvmy = (uvmy&3)<<1;
352
    if(!v->rnd){
353
        dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
354
        dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
355
    }else{
356
        dsp->put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
357
        dsp->put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
358
    }
359
}
360

    
361
/** Do motion compensation for 4-MV macroblock - luminance block
362
 */
363
static void vc1_mc_4mv_luma(VC1Context *v, int n)
364
{
365
    MpegEncContext *s = &v->s;
366
    DSPContext *dsp = &v->s.dsp;
367
    uint8_t *srcY;
368
    int dxy, mx, my, src_x, src_y;
369
    int off;
370

    
371
    if(!v->s.last_picture.data[0])return;
372
    mx = s->mv[0][n][0];
373
    my = s->mv[0][n][1];
374
    srcY = s->last_picture.data[0];
375

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

    
378
    src_x = s->mb_x * 16 + (n&1) * 8 + (mx >> 2);
379
    src_y = s->mb_y * 16 + (n&2) * 4 + (my >> 2);
380

    
381
    if(v->profile != PROFILE_ADVANCED){
382
        src_x   = av_clip(  src_x, -16, s->mb_width  * 16);
383
        src_y   = av_clip(  src_y, -16, s->mb_height * 16);
384
    }else{
385
        src_x   = av_clip(  src_x, -17, s->avctx->coded_width);
386
        src_y   = av_clip(  src_y, -18, s->avctx->coded_height + 1);
387
    }
388

    
389
    srcY += src_y * s->linesize + src_x;
390

    
391
    if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
392
       || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 8 - s->mspel*2
393
       || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 8 - s->mspel*2){
394
        srcY -= s->mspel * (1 + s->linesize);
395
        ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 9+s->mspel*2, 9+s->mspel*2,
396
                            src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
397
        srcY = s->edge_emu_buffer;
398
        /* if we deal with range reduction we need to scale source blocks */
399
        if(v->rangeredfrm) {
400
            int i, j;
401
            uint8_t *src;
402

    
403
            src = srcY;
404
            for(j = 0; j < 9 + s->mspel*2; j++) {
405
                for(i = 0; i < 9 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
406
                src += s->linesize;
407
            }
408
        }
409
        /* if we deal with intensity compensation we need to scale source blocks */
410
        if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
411
            int i, j;
412
            uint8_t *src;
413

    
414
            src = srcY;
415
            for(j = 0; j < 9 + s->mspel*2; j++) {
416
                for(i = 0; i < 9 + s->mspel*2; i++) src[i] = v->luty[src[i]];
417
                src += s->linesize;
418
            }
419
        }
420
        srcY += s->mspel * (1 + s->linesize);
421
    }
422

    
423
    if(s->mspel) {
424
        dxy = ((my & 3) << 2) | (mx & 3);
425
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, v->rnd);
426
    } else { // hpel mc - always used for luma
427
        dxy = (my & 2) | ((mx & 2) >> 1);
428
        if(!v->rnd)
429
            dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
430
        else
431
            dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
432
    }
433
}
434

    
435
static inline int median4(int a, int b, int c, int d)
436
{
437
    if(a < b) {
438
        if(c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
439
        else      return (FFMIN(b, c) + FFMAX(a, d)) / 2;
440
    } else {
441
        if(c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
442
        else      return (FFMIN(a, c) + FFMAX(b, d)) / 2;
443
    }
444
}
445

    
446

    
447
/** Do motion compensation for 4-MV macroblock - both chroma blocks
448
 */
449
static void vc1_mc_4mv_chroma(VC1Context *v)
450
{
451
    MpegEncContext *s = &v->s;
452
    DSPContext *dsp = &v->s.dsp;
453
    uint8_t *srcU, *srcV;
454
    int uvmx, uvmy, uvsrc_x, uvsrc_y;
455
    int i, idx, tx = 0, ty = 0;
456
    int mvx[4], mvy[4], intra[4];
457
    static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
458

    
459
    if(!v->s.last_picture.data[0])return;
460
    if(s->flags & CODEC_FLAG_GRAY) return;
461

    
462
    for(i = 0; i < 4; i++) {
463
        mvx[i] = s->mv[0][i][0];
464
        mvy[i] = s->mv[0][i][1];
465
        intra[i] = v->mb_type[0][s->block_index[i]];
466
    }
467

    
468
    /* calculate chroma MV vector from four luma MVs */
469
    idx = (intra[3] << 3) | (intra[2] << 2) | (intra[1] << 1) | intra[0];
470
    if(!idx) { // all blocks are inter
471
        tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
472
        ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
473
    } else if(count[idx] == 1) { // 3 inter blocks
474
        switch(idx) {
475
        case 0x1:
476
            tx = mid_pred(mvx[1], mvx[2], mvx[3]);
477
            ty = mid_pred(mvy[1], mvy[2], mvy[3]);
478
            break;
479
        case 0x2:
480
            tx = mid_pred(mvx[0], mvx[2], mvx[3]);
481
            ty = mid_pred(mvy[0], mvy[2], mvy[3]);
482
            break;
483
        case 0x4:
484
            tx = mid_pred(mvx[0], mvx[1], mvx[3]);
485
            ty = mid_pred(mvy[0], mvy[1], mvy[3]);
486
            break;
487
        case 0x8:
488
            tx = mid_pred(mvx[0], mvx[1], mvx[2]);
489
            ty = mid_pred(mvy[0], mvy[1], mvy[2]);
490
            break;
491
        }
492
    } else if(count[idx] == 2) {
493
        int t1 = 0, t2 = 0;
494
        for(i=0; i<3;i++) if(!intra[i]) {t1 = i; break;}
495
        for(i= t1+1; i<4; i++)if(!intra[i]) {t2 = i; break;}
496
        tx = (mvx[t1] + mvx[t2]) / 2;
497
        ty = (mvy[t1] + mvy[t2]) / 2;
498
    } else {
499
        s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
500
        s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
501
        return; //no need to do MC for inter blocks
502
    }
503

    
504
    s->current_picture.motion_val[1][s->block_index[0]][0] = tx;
505
    s->current_picture.motion_val[1][s->block_index[0]][1] = ty;
506
    uvmx = (tx + ((tx&3) == 3)) >> 1;
507
    uvmy = (ty + ((ty&3) == 3)) >> 1;
508
    if(v->fastuvmc) {
509
        uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
510
        uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
511
    }
512

    
513
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
514
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
515

    
516
    if(v->profile != PROFILE_ADVANCED){
517
        uvsrc_x = av_clip(uvsrc_x,  -8, s->mb_width  *  8);
518
        uvsrc_y = av_clip(uvsrc_y,  -8, s->mb_height *  8);
519
    }else{
520
        uvsrc_x = av_clip(uvsrc_x,  -8, s->avctx->coded_width  >> 1);
521
        uvsrc_y = av_clip(uvsrc_y,  -8, s->avctx->coded_height >> 1);
522
    }
523

    
524
    srcU = s->last_picture.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
525
    srcV = s->last_picture.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
526
    if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
527
       || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
528
       || (unsigned)uvsrc_y > (s->v_edge_pos >> 1) - 9){
529
        ff_emulated_edge_mc(s->edge_emu_buffer     , srcU, s->uvlinesize, 8+1, 8+1,
530
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
531
        ff_emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize, 8+1, 8+1,
532
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
533
        srcU = s->edge_emu_buffer;
534
        srcV = s->edge_emu_buffer + 16;
535

    
536
        /* if we deal with range reduction we need to scale source blocks */
537
        if(v->rangeredfrm) {
538
            int i, j;
539
            uint8_t *src, *src2;
540

    
541
            src = srcU; src2 = srcV;
542
            for(j = 0; j < 9; j++) {
543
                for(i = 0; i < 9; i++) {
544
                    src[i] = ((src[i] - 128) >> 1) + 128;
545
                    src2[i] = ((src2[i] - 128) >> 1) + 128;
546
                }
547
                src += s->uvlinesize;
548
                src2 += s->uvlinesize;
549
            }
550
        }
551
        /* if we deal with intensity compensation we need to scale source blocks */
552
        if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
553
            int i, j;
554
            uint8_t *src, *src2;
555

    
556
            src = srcU; src2 = srcV;
557
            for(j = 0; j < 9; j++) {
558
                for(i = 0; i < 9; i++) {
559
                    src[i] = v->lutuv[src[i]];
560
                    src2[i] = v->lutuv[src2[i]];
561
                }
562
                src += s->uvlinesize;
563
                src2 += s->uvlinesize;
564
            }
565
        }
566
    }
567

    
568
    /* Chroma MC always uses qpel bilinear */
569
    uvmx = (uvmx&3)<<1;
570
    uvmy = (uvmy&3)<<1;
571
    if(!v->rnd){
572
        dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
573
        dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
574
    }else{
575
        dsp->put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
576
        dsp->put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
577
    }
578
}
579

    
580
/***********************************************************************/
581
/**
582
 * @defgroup vc1block VC-1 Block-level functions
583
 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
584
 * @{
585
 */
586

    
587
/**
588
 * @def GET_MQUANT
589
 * @brief Get macroblock-level quantizer scale
590
 */
591
#define GET_MQUANT()                                           \
592
  if (v->dquantfrm)                                            \
593
  {                                                            \
594
    int edges = 0;                                             \
595
    if (v->dqprofile == DQPROFILE_ALL_MBS)                     \
596
    {                                                          \
597
      if (v->dqbilevel)                                        \
598
      {                                                        \
599
        mquant = (get_bits1(gb)) ? v->altpq : v->pq;           \
600
      }                                                        \
601
      else                                                     \
602
      {                                                        \
603
        mqdiff = get_bits(gb, 3);                              \
604
        if (mqdiff != 7) mquant = v->pq + mqdiff;              \
605
        else mquant = get_bits(gb, 5);                         \
606
      }                                                        \
607
    }                                                          \
608
    if(v->dqprofile == DQPROFILE_SINGLE_EDGE)                  \
609
        edges = 1 << v->dqsbedge;                              \
610
    else if(v->dqprofile == DQPROFILE_DOUBLE_EDGES)            \
611
        edges = (3 << v->dqsbedge) % 15;                       \
612
    else if(v->dqprofile == DQPROFILE_FOUR_EDGES)              \
613
        edges = 15;                                            \
614
    if((edges&1) && !s->mb_x)                                  \
615
        mquant = v->altpq;                                     \
616
    if((edges&2) && s->first_slice_line)                       \
617
        mquant = v->altpq;                                     \
618
    if((edges&4) && s->mb_x == (s->mb_width - 1))              \
619
        mquant = v->altpq;                                     \
620
    if((edges&8) && s->mb_y == (s->mb_height - 1))             \
621
        mquant = v->altpq;                                     \
622
  }
623

    
624
/**
625
 * @def GET_MVDATA(_dmv_x, _dmv_y)
626
 * @brief Get MV differentials
627
 * @see MVDATA decoding from 8.3.5.2, p(1)20
628
 * @param _dmv_x Horizontal differential for decoded MV
629
 * @param _dmv_y Vertical differential for decoded MV
630
 */
631
#define GET_MVDATA(_dmv_x, _dmv_y)                                  \
632
  index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table,\
633
                       VC1_MV_DIFF_VLC_BITS, 2);                    \
634
  if (index > 36)                                                   \
635
  {                                                                 \
636
    mb_has_coeffs = 1;                                              \
637
    index -= 37;                                                    \
638
  }                                                                 \
639
  else mb_has_coeffs = 0;                                           \
640
  s->mb_intra = 0;                                                  \
641
  if (!index) { _dmv_x = _dmv_y = 0; }                              \
642
  else if (index == 35)                                             \
643
  {                                                                 \
644
    _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample);          \
645
    _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample);          \
646
  }                                                                 \
647
  else if (index == 36)                                             \
648
  {                                                                 \
649
    _dmv_x = 0;                                                     \
650
    _dmv_y = 0;                                                     \
651
    s->mb_intra = 1;                                                \
652
  }                                                                 \
653
  else                                                              \
654
  {                                                                 \
655
    index1 = index%6;                                               \
656
    if (!s->quarter_sample && index1 == 5) val = 1;                 \
657
    else                                   val = 0;                 \
658
    if(size_table[index1] - val > 0)                                \
659
        val = get_bits(gb, size_table[index1] - val);               \
660
    else                                   val = 0;                 \
661
    sign = 0 - (val&1);                                             \
662
    _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign;     \
663
                                                                    \
664
    index1 = index/6;                                               \
665
    if (!s->quarter_sample && index1 == 5) val = 1;                 \
666
    else                                   val = 0;                 \
667
    if(size_table[index1] - val > 0)                                \
668
        val = get_bits(gb, size_table[index1] - val);               \
669
    else                                   val = 0;                 \
670
    sign = 0 - (val&1);                                             \
671
    _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign;     \
672
  }
673

    
674
/** Predict and set motion vector
675
 */
676
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)
677
{
678
    int xy, wrap, off = 0;
679
    int16_t *A, *B, *C;
680
    int px, py;
681
    int sum;
682

    
683
    /* scale MV difference to be quad-pel */
684
    dmv_x <<= 1 - s->quarter_sample;
685
    dmv_y <<= 1 - s->quarter_sample;
686

    
687
    wrap = s->b8_stride;
688
    xy = s->block_index[n];
689

    
690
    if(s->mb_intra){
691
        s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
692
        s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
693
        s->current_picture.motion_val[1][xy][0] = 0;
694
        s->current_picture.motion_val[1][xy][1] = 0;
695
        if(mv1) { /* duplicate motion data for 1-MV block */
696
            s->current_picture.motion_val[0][xy + 1][0] = 0;
697
            s->current_picture.motion_val[0][xy + 1][1] = 0;
698
            s->current_picture.motion_val[0][xy + wrap][0] = 0;
699
            s->current_picture.motion_val[0][xy + wrap][1] = 0;
700
            s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
701
            s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
702
            s->current_picture.motion_val[1][xy + 1][0] = 0;
703
            s->current_picture.motion_val[1][xy + 1][1] = 0;
704
            s->current_picture.motion_val[1][xy + wrap][0] = 0;
705
            s->current_picture.motion_val[1][xy + wrap][1] = 0;
706
            s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
707
            s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
708
        }
709
        return;
710
    }
711

    
712
    C = s->current_picture.motion_val[0][xy - 1];
713
    A = s->current_picture.motion_val[0][xy - wrap];
714
    if(mv1)
715
        off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
716
    else {
717
        //in 4-MV mode different blocks have different B predictor position
718
        switch(n){
719
        case 0:
720
            off = (s->mb_x > 0) ? -1 : 1;
721
            break;
722
        case 1:
723
            off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
724
            break;
725
        case 2:
726
            off = 1;
727
            break;
728
        case 3:
729
            off = -1;
730
        }
731
    }
732
    B = s->current_picture.motion_val[0][xy - wrap + off];
733

    
734
    if(!s->first_slice_line || (n==2 || n==3)) { // predictor A is not out of bounds
735
        if(s->mb_width == 1) {
736
            px = A[0];
737
            py = A[1];
738
        } else {
739
            px = mid_pred(A[0], B[0], C[0]);
740
            py = mid_pred(A[1], B[1], C[1]);
741
        }
742
    } else if(s->mb_x || (n==1 || n==3)) { // predictor C is not out of bounds
743
        px = C[0];
744
        py = C[1];
745
    } else {
746
        px = py = 0;
747
    }
748
    /* Pullback MV as specified in 8.3.5.3.4 */
749
    {
750
        int qx, qy, X, Y;
751
        qx = (s->mb_x << 6) + ((n==1 || n==3) ? 32 : 0);
752
        qy = (s->mb_y << 6) + ((n==2 || n==3) ? 32 : 0);
753
        X = (s->mb_width << 6) - 4;
754
        Y = (s->mb_height << 6) - 4;
755
        if(mv1) {
756
            if(qx + px < -60) px = -60 - qx;
757
            if(qy + py < -60) py = -60 - qy;
758
        } else {
759
            if(qx + px < -28) px = -28 - qx;
760
            if(qy + py < -28) py = -28 - qy;
761
        }
762
        if(qx + px > X) px = X - qx;
763
        if(qy + py > Y) py = Y - qy;
764
    }
765
    /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
766
    if((!s->first_slice_line || (n==2 || n==3)) && (s->mb_x || (n==1 || n==3))) {
767
        if(is_intra[xy - wrap])
768
            sum = FFABS(px) + FFABS(py);
769
        else
770
            sum = FFABS(px - A[0]) + FFABS(py - A[1]);
771
        if(sum > 32) {
772
            if(get_bits1(&s->gb)) {
773
                px = A[0];
774
                py = A[1];
775
            } else {
776
                px = C[0];
777
                py = C[1];
778
            }
779
        } else {
780
            if(is_intra[xy - 1])
781
                sum = FFABS(px) + FFABS(py);
782
            else
783
                sum = FFABS(px - C[0]) + FFABS(py - C[1]);
784
            if(sum > 32) {
785
                if(get_bits1(&s->gb)) {
786
                    px = A[0];
787
                    py = A[1];
788
                } else {
789
                    px = C[0];
790
                    py = C[1];
791
                }
792
            }
793
        }
794
    }
795
    /* store MV using signed modulus of MV range defined in 4.11 */
796
    s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
797
    s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
798
    if(mv1) { /* duplicate motion data for 1-MV block */
799
        s->current_picture.motion_val[0][xy + 1][0] = s->current_picture.motion_val[0][xy][0];
800
        s->current_picture.motion_val[0][xy + 1][1] = s->current_picture.motion_val[0][xy][1];
801
        s->current_picture.motion_val[0][xy + wrap][0] = s->current_picture.motion_val[0][xy][0];
802
        s->current_picture.motion_val[0][xy + wrap][1] = s->current_picture.motion_val[0][xy][1];
803
        s->current_picture.motion_val[0][xy + wrap + 1][0] = s->current_picture.motion_val[0][xy][0];
804
        s->current_picture.motion_val[0][xy + wrap + 1][1] = s->current_picture.motion_val[0][xy][1];
805
    }
806
}
807

    
808
/** Motion compensation for direct or interpolated blocks in B-frames
809
 */
810
static void vc1_interp_mc(VC1Context *v)
811
{
812
    MpegEncContext *s = &v->s;
813
    DSPContext *dsp = &v->s.dsp;
814
    uint8_t *srcY, *srcU, *srcV;
815
    int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
816

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

    
819
    mx = s->mv[1][0][0];
820
    my = s->mv[1][0][1];
821
    uvmx = (mx + ((mx & 3) == 3)) >> 1;
822
    uvmy = (my + ((my & 3) == 3)) >> 1;
823
    if(v->fastuvmc) {
824
        uvmx = uvmx + ((uvmx<0)?-(uvmx&1):(uvmx&1));
825
        uvmy = uvmy + ((uvmy<0)?-(uvmy&1):(uvmy&1));
826
    }
827
    srcY = s->next_picture.data[0];
828
    srcU = s->next_picture.data[1];
829
    srcV = s->next_picture.data[2];
830

    
831
    src_x = s->mb_x * 16 + (mx >> 2);
832
    src_y = s->mb_y * 16 + (my >> 2);
833
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
834
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
835

    
836
    if(v->profile != PROFILE_ADVANCED){
837
        src_x   = av_clip(  src_x, -16, s->mb_width  * 16);
838
        src_y   = av_clip(  src_y, -16, s->mb_height * 16);
839
        uvsrc_x = av_clip(uvsrc_x,  -8, s->mb_width  *  8);
840
        uvsrc_y = av_clip(uvsrc_y,  -8, s->mb_height *  8);
841
    }else{
842
        src_x   = av_clip(  src_x, -17, s->avctx->coded_width);
843
        src_y   = av_clip(  src_y, -18, s->avctx->coded_height + 1);
844
        uvsrc_x = av_clip(uvsrc_x,  -8, s->avctx->coded_width  >> 1);
845
        uvsrc_y = av_clip(uvsrc_y,  -8, s->avctx->coded_height >> 1);
846
    }
847

    
848
    srcY += src_y * s->linesize + src_x;
849
    srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
850
    srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
851

    
852
    /* for grayscale we should not try to read from unknown area */
853
    if(s->flags & CODEC_FLAG_GRAY) {
854
        srcU = s->edge_emu_buffer + 18 * s->linesize;
855
        srcV = s->edge_emu_buffer + 18 * s->linesize;
856
    }
857

    
858
    if(v->rangeredfrm
859
       || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel*3
860
       || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 16 - s->mspel*3){
861
        uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
862

    
863
        srcY -= s->mspel * (1 + s->linesize);
864
        ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2,
865
                            src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
866
        srcY = s->edge_emu_buffer;
867
        ff_emulated_edge_mc(uvbuf     , srcU, s->uvlinesize, 8+1, 8+1,
868
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
869
        ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
870
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
871
        srcU = uvbuf;
872
        srcV = uvbuf + 16;
873
        /* if we deal with range reduction we need to scale source blocks */
874
        if(v->rangeredfrm) {
875
            int i, j;
876
            uint8_t *src, *src2;
877

    
878
            src = srcY;
879
            for(j = 0; j < 17 + s->mspel*2; j++) {
880
                for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
881
                src += s->linesize;
882
            }
883
            src = srcU; src2 = srcV;
884
            for(j = 0; j < 9; j++) {
885
                for(i = 0; i < 9; i++) {
886
                    src[i] = ((src[i] - 128) >> 1) + 128;
887
                    src2[i] = ((src2[i] - 128) >> 1) + 128;
888
                }
889
                src += s->uvlinesize;
890
                src2 += s->uvlinesize;
891
            }
892
        }
893
        srcY += s->mspel * (1 + s->linesize);
894
    }
895

    
896
    if(s->mspel) {
897
        dxy = ((my & 3) << 2) | (mx & 3);
898
        dsp->avg_vc1_mspel_pixels_tab[dxy](s->dest[0]    , srcY    , s->linesize, v->rnd);
899
        dsp->avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
900
        srcY += s->linesize * 8;
901
        dsp->avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize    , srcY    , s->linesize, v->rnd);
902
        dsp->avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
903
    } else { // hpel mc
904
        dxy = (my & 2) | ((mx & 2) >> 1);
905

    
906
        if(!v->rnd)
907
            dsp->avg_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
908
        else
909
            dsp->avg_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
910
    }
911

    
912
    if(s->flags & CODEC_FLAG_GRAY) return;
913
    /* Chroma MC always uses qpel blilinear */
914
    uvmx = (uvmx&3)<<1;
915
    uvmy = (uvmy&3)<<1;
916
    if(!v->rnd){
917
        dsp->avg_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
918
        dsp->avg_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
919
    }else{
920
        dsp->avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
921
        dsp->avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
922
    }
923
}
924

    
925
static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
926
{
927
    int n = bfrac;
928

    
929
#if B_FRACTION_DEN==256
930
    if(inv)
931
        n -= 256;
932
    if(!qs)
933
        return 2 * ((value * n + 255) >> 9);
934
    return (value * n + 128) >> 8;
935
#else
936
    if(inv)
937
        n -= B_FRACTION_DEN;
938
    if(!qs)
939
        return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
940
    return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
941
#endif
942
}
943

    
944
/** Reconstruct motion vector for B-frame and do motion compensation
945
 */
946
static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mode)
947
{
948
    if(v->use_ic) {
949
        v->mv_mode2 = v->mv_mode;
950
        v->mv_mode = MV_PMODE_INTENSITY_COMP;
951
    }
952
    if(direct) {
953
        vc1_mc_1mv(v, 0);
954
        vc1_interp_mc(v);
955
        if(v->use_ic) v->mv_mode = v->mv_mode2;
956
        return;
957
    }
958
    if(mode == BMV_TYPE_INTERPOLATED) {
959
        vc1_mc_1mv(v, 0);
960
        vc1_interp_mc(v);
961
        if(v->use_ic) v->mv_mode = v->mv_mode2;
962
        return;
963
    }
964

    
965
    if(v->use_ic && (mode == BMV_TYPE_BACKWARD)) v->mv_mode = v->mv_mode2;
966
    vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
967
    if(v->use_ic) v->mv_mode = v->mv_mode2;
968
}
969

    
970
static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mvtype)
971
{
972
    MpegEncContext *s = &v->s;
973
    int xy, wrap, off = 0;
974
    int16_t *A, *B, *C;
975
    int px, py;
976
    int sum;
977
    int r_x, r_y;
978
    const uint8_t *is_intra = v->mb_type[0];
979

    
980
    r_x = v->range_x;
981
    r_y = v->range_y;
982
    /* scale MV difference to be quad-pel */
983
    dmv_x[0] <<= 1 - s->quarter_sample;
984
    dmv_y[0] <<= 1 - s->quarter_sample;
985
    dmv_x[1] <<= 1 - s->quarter_sample;
986
    dmv_y[1] <<= 1 - s->quarter_sample;
987

    
988
    wrap = s->b8_stride;
989
    xy = s->block_index[0];
990

    
991
    if(s->mb_intra) {
992
        s->current_picture.motion_val[0][xy][0] =
993
        s->current_picture.motion_val[0][xy][1] =
994
        s->current_picture.motion_val[1][xy][0] =
995
        s->current_picture.motion_val[1][xy][1] = 0;
996
        return;
997
    }
998
    s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
999
    s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
1000
    s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
1001
    s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
1002

    
1003
    /* Pullback predicted motion vectors as specified in 8.4.5.4 */
1004
    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));
1005
    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));
1006
    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));
1007
    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));
1008
    if(direct) {
1009
        s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
1010
        s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
1011
        s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
1012
        s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
1013
        return;
1014
    }
1015

    
1016
    if((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
1017
        C = s->current_picture.motion_val[0][xy - 2];
1018
        A = s->current_picture.motion_val[0][xy - wrap*2];
1019
        off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1020
        B = s->current_picture.motion_val[0][xy - wrap*2 + off];
1021

    
1022
        if(!s->mb_x) C[0] = C[1] = 0;
1023
        if(!s->first_slice_line) { // predictor A is not out of bounds
1024
            if(s->mb_width == 1) {
1025
                px = A[0];
1026
                py = A[1];
1027
            } else {
1028
                px = mid_pred(A[0], B[0], C[0]);
1029
                py = mid_pred(A[1], B[1], C[1]);
1030
            }
1031
        } else if(s->mb_x) { // predictor C is not out of bounds
1032
            px = C[0];
1033
            py = C[1];
1034
        } else {
1035
            px = py = 0;
1036
        }
1037
        /* Pullback MV as specified in 8.3.5.3.4 */
1038
        {
1039
            int qx, qy, X, Y;
1040
            if(v->profile < PROFILE_ADVANCED) {
1041
                qx = (s->mb_x << 5);
1042
                qy = (s->mb_y << 5);
1043
                X = (s->mb_width << 5) - 4;
1044
                Y = (s->mb_height << 5) - 4;
1045
                if(qx + px < -28) px = -28 - qx;
1046
                if(qy + py < -28) py = -28 - qy;
1047
                if(qx + px > X) px = X - qx;
1048
                if(qy + py > Y) py = Y - qy;
1049
            } else {
1050
                qx = (s->mb_x << 6);
1051
                qy = (s->mb_y << 6);
1052
                X = (s->mb_width << 6) - 4;
1053
                Y = (s->mb_height << 6) - 4;
1054
                if(qx + px < -60) px = -60 - qx;
1055
                if(qy + py < -60) py = -60 - qy;
1056
                if(qx + px > X) px = X - qx;
1057
                if(qy + py > Y) py = Y - qy;
1058
            }
1059
        }
1060
        /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
1061
        if(0 && !s->first_slice_line && s->mb_x) {
1062
            if(is_intra[xy - wrap])
1063
                sum = FFABS(px) + FFABS(py);
1064
            else
1065
                sum = FFABS(px - A[0]) + FFABS(py - A[1]);
1066
            if(sum > 32) {
1067
                if(get_bits1(&s->gb)) {
1068
                    px = A[0];
1069
                    py = A[1];
1070
                } else {
1071
                    px = C[0];
1072
                    py = C[1];
1073
                }
1074
            } else {
1075
                if(is_intra[xy - 2])
1076
                    sum = FFABS(px) + FFABS(py);
1077
                else
1078
                    sum = FFABS(px - C[0]) + FFABS(py - C[1]);
1079
                if(sum > 32) {
1080
                    if(get_bits1(&s->gb)) {
1081
                        px = A[0];
1082
                        py = A[1];
1083
                    } else {
1084
                        px = C[0];
1085
                        py = C[1];
1086
                    }
1087
                }
1088
            }
1089
        }
1090
        /* store MV using signed modulus of MV range defined in 4.11 */
1091
        s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
1092
        s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
1093
    }
1094
    if((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
1095
        C = s->current_picture.motion_val[1][xy - 2];
1096
        A = s->current_picture.motion_val[1][xy - wrap*2];
1097
        off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1098
        B = s->current_picture.motion_val[1][xy - wrap*2 + off];
1099

    
1100
        if(!s->mb_x) C[0] = C[1] = 0;
1101
        if(!s->first_slice_line) { // predictor A is not out of bounds
1102
            if(s->mb_width == 1) {
1103
                px = A[0];
1104
                py = A[1];
1105
            } else {
1106
                px = mid_pred(A[0], B[0], C[0]);
1107
                py = mid_pred(A[1], B[1], C[1]);
1108
            }
1109
        } else if(s->mb_x) { // predictor C is not out of bounds
1110
            px = C[0];
1111
            py = C[1];
1112
        } else {
1113
            px = py = 0;
1114
        }
1115
        /* Pullback MV as specified in 8.3.5.3.4 */
1116
        {
1117
            int qx, qy, X, Y;
1118
            if(v->profile < PROFILE_ADVANCED) {
1119
                qx = (s->mb_x << 5);
1120
                qy = (s->mb_y << 5);
1121
                X = (s->mb_width << 5) - 4;
1122
                Y = (s->mb_height << 5) - 4;
1123
                if(qx + px < -28) px = -28 - qx;
1124
                if(qy + py < -28) py = -28 - qy;
1125
                if(qx + px > X) px = X - qx;
1126
                if(qy + py > Y) py = Y - qy;
1127
            } else {
1128
                qx = (s->mb_x << 6);
1129
                qy = (s->mb_y << 6);
1130
                X = (s->mb_width << 6) - 4;
1131
                Y = (s->mb_height << 6) - 4;
1132
                if(qx + px < -60) px = -60 - qx;
1133
                if(qy + py < -60) py = -60 - qy;
1134
                if(qx + px > X) px = X - qx;
1135
                if(qy + py > Y) py = Y - qy;
1136
            }
1137
        }
1138
        /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
1139
        if(0 && !s->first_slice_line && s->mb_x) {
1140
            if(is_intra[xy - wrap])
1141
                sum = FFABS(px) + FFABS(py);
1142
            else
1143
                sum = FFABS(px - A[0]) + FFABS(py - A[1]);
1144
            if(sum > 32) {
1145
                if(get_bits1(&s->gb)) {
1146
                    px = A[0];
1147
                    py = A[1];
1148
                } else {
1149
                    px = C[0];
1150
                    py = C[1];
1151
                }
1152
            } else {
1153
                if(is_intra[xy - 2])
1154
                    sum = FFABS(px) + FFABS(py);
1155
                else
1156
                    sum = FFABS(px - C[0]) + FFABS(py - C[1]);
1157
                if(sum > 32) {
1158
                    if(get_bits1(&s->gb)) {
1159
                        px = A[0];
1160
                        py = A[1];
1161
                    } else {
1162
                        px = C[0];
1163
                        py = C[1];
1164
                    }
1165
                }
1166
            }
1167
        }
1168
        /* store MV using signed modulus of MV range defined in 4.11 */
1169

    
1170
        s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
1171
        s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
1172
    }
1173
    s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
1174
    s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
1175
    s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
1176
    s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
1177
}
1178

    
1179
/** Get predicted DC value for I-frames only
1180
 * prediction dir: left=0, top=1
1181
 * @param s MpegEncContext
1182
 * @param overlap flag indicating that overlap filtering is used
1183
 * @param pq integer part of picture quantizer
1184
 * @param[in] n block index in the current MB
1185
 * @param dc_val_ptr Pointer to DC predictor
1186
 * @param dir_ptr Prediction direction for use in AC prediction
1187
 */
1188
static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
1189
                              int16_t **dc_val_ptr, int *dir_ptr)
1190
{
1191
    int a, b, c, wrap, pred, scale;
1192
    int16_t *dc_val;
1193
    static const uint16_t dcpred[32] = {
1194
    -1, 1024,  512,  341,  256,  205,  171,  146,  128,
1195
         114,  102,   93,   85,   79,   73,   68,   64,
1196
          60,   57,   54,   51,   49,   47,   45,   43,
1197
          41,   39,   38,   37,   35,   34,   33
1198
    };
1199

    
1200
    /* find prediction - wmv3_dc_scale always used here in fact */
1201
    if (n < 4)     scale = s->y_dc_scale;
1202
    else           scale = s->c_dc_scale;
1203

    
1204
    wrap = s->block_wrap[n];
1205
    dc_val= s->dc_val[0] + s->block_index[n];
1206

    
1207
    /* B A
1208
     * C X
1209
     */
1210
    c = dc_val[ - 1];
1211
    b = dc_val[ - 1 - wrap];
1212
    a = dc_val[ - wrap];
1213

    
1214
    if (pq < 9 || !overlap)
1215
    {
1216
        /* Set outer values */
1217
        if (s->first_slice_line && (n!=2 && n!=3)) b=a=dcpred[scale];
1218
        if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=dcpred[scale];
1219
    }
1220
    else
1221
    {
1222
        /* Set outer values */
1223
        if (s->first_slice_line && (n!=2 && n!=3)) b=a=0;
1224
        if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=0;
1225
    }
1226

    
1227
    if (abs(a - b) <= abs(b - c)) {
1228
        pred = c;
1229
        *dir_ptr = 1;//left
1230
    } else {
1231
        pred = a;
1232
        *dir_ptr = 0;//top
1233
    }
1234

    
1235
    /* update predictor */
1236
    *dc_val_ptr = &dc_val[0];
1237
    return pred;
1238
}
1239

    
1240

    
1241
/** Get predicted DC value
1242
 * prediction dir: left=0, top=1
1243
 * @param s MpegEncContext
1244
 * @param overlap flag indicating that overlap filtering is used
1245
 * @param pq integer part of picture quantizer
1246
 * @param[in] n block index in the current MB
1247
 * @param a_avail flag indicating top block availability
1248
 * @param c_avail flag indicating left block availability
1249
 * @param dc_val_ptr Pointer to DC predictor
1250
 * @param dir_ptr Prediction direction for use in AC prediction
1251
 */
1252
static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
1253
                              int a_avail, int c_avail,
1254
                              int16_t **dc_val_ptr, int *dir_ptr)
1255
{
1256
    int a, b, c, wrap, pred;
1257
    int16_t *dc_val;
1258
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1259
    int q1, q2 = 0;
1260

    
1261
    wrap = s->block_wrap[n];
1262
    dc_val= s->dc_val[0] + s->block_index[n];
1263

    
1264
    /* B A
1265
     * C X
1266
     */
1267
    c = dc_val[ - 1];
1268
    b = dc_val[ - 1 - wrap];
1269
    a = dc_val[ - wrap];
1270
    /* scale predictors if needed */
1271
    q1 = s->current_picture.qscale_table[mb_pos];
1272
    if(c_avail && (n!= 1 && n!=3)) {
1273
        q2 = s->current_picture.qscale_table[mb_pos - 1];
1274
        if(q2 && q2 != q1)
1275
            c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
1276
    }
1277
    if(a_avail && (n!= 2 && n!=3)) {
1278
        q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
1279
        if(q2 && q2 != q1)
1280
            a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
1281
    }
1282
    if(a_avail && c_avail && (n!=3)) {
1283
        int off = mb_pos;
1284
        if(n != 1) off--;
1285
        if(n != 2) off -= s->mb_stride;
1286
        q2 = s->current_picture.qscale_table[off];
1287
        if(q2 && q2 != q1)
1288
            b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
1289
    }
1290

    
1291
    if(a_avail && c_avail) {
1292
        if(abs(a - b) <= abs(b - c)) {
1293
            pred = c;
1294
            *dir_ptr = 1;//left
1295
        } else {
1296
            pred = a;
1297
            *dir_ptr = 0;//top
1298
        }
1299
    } else if(a_avail) {
1300
        pred = a;
1301
        *dir_ptr = 0;//top
1302
    } else if(c_avail) {
1303
        pred = c;
1304
        *dir_ptr = 1;//left
1305
    } else {
1306
        pred = 0;
1307
        *dir_ptr = 1;//left
1308
    }
1309

    
1310
    /* update predictor */
1311
    *dc_val_ptr = &dc_val[0];
1312
    return pred;
1313
}
1314

    
1315
/** @} */ // Block group
1316

    
1317
/**
1318
 * @defgroup vc1_std_mb VC1 Macroblock-level functions in Simple/Main Profiles
1319
 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1320
 * @{
1321
 */
1322

    
1323
static inline int vc1_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
1324
{
1325
    int xy, wrap, pred, a, b, c;
1326

    
1327
    xy = s->block_index[n];
1328
    wrap = s->b8_stride;
1329

    
1330
    /* B C
1331
     * A X
1332
     */
1333
    a = s->coded_block[xy - 1       ];
1334
    b = s->coded_block[xy - 1 - wrap];
1335
    c = s->coded_block[xy     - wrap];
1336

    
1337
    if (b == c) {
1338
        pred = a;
1339
    } else {
1340
        pred = c;
1341
    }
1342

    
1343
    /* store value */
1344
    *coded_block_ptr = &s->coded_block[xy];
1345

    
1346
    return pred;
1347
}
1348

    
1349
/**
1350
 * Decode one AC coefficient
1351
 * @param v The VC1 context
1352
 * @param last Last coefficient
1353
 * @param skip How much zero coefficients to skip
1354
 * @param value Decoded AC coefficient value
1355
 * @param codingset set of VLC to decode data
1356
 * @see 8.1.3.4
1357
 */
1358
static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip, int *value, int codingset)
1359
{
1360
    GetBitContext *gb = &v->s.gb;
1361
    int index, escape, run = 0, level = 0, lst = 0;
1362

    
1363
    index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
1364
    if (index != vc1_ac_sizes[codingset] - 1) {
1365
        run = vc1_index_decode_table[codingset][index][0];
1366
        level = vc1_index_decode_table[codingset][index][1];
1367
        lst = index >= vc1_last_decode_table[codingset];
1368
        if(get_bits1(gb))
1369
            level = -level;
1370
    } else {
1371
        escape = decode210(gb);
1372
        if (escape != 2) {
1373
            index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
1374
            run = vc1_index_decode_table[codingset][index][0];
1375
            level = vc1_index_decode_table[codingset][index][1];
1376
            lst = index >= vc1_last_decode_table[codingset];
1377
            if(escape == 0) {
1378
                if(lst)
1379
                    level += vc1_last_delta_level_table[codingset][run];
1380
                else
1381
                    level += vc1_delta_level_table[codingset][run];
1382
            } else {
1383
                if(lst)
1384
                    run += vc1_last_delta_run_table[codingset][level] + 1;
1385
                else
1386
                    run += vc1_delta_run_table[codingset][level] + 1;
1387
            }
1388
            if(get_bits1(gb))
1389
                level = -level;
1390
        } else {
1391
            int sign;
1392
            lst = get_bits1(gb);
1393
            if(v->s.esc3_level_length == 0) {
1394
                if(v->pq < 8 || v->dquantfrm) { // table 59
1395
                    v->s.esc3_level_length = get_bits(gb, 3);
1396
                    if(!v->s.esc3_level_length)
1397
                        v->s.esc3_level_length = get_bits(gb, 2) + 8;
1398
                } else { //table 60
1399
                    v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
1400
                }
1401
                v->s.esc3_run_length = 3 + get_bits(gb, 2);
1402
            }
1403
            run = get_bits(gb, v->s.esc3_run_length);
1404
            sign = get_bits1(gb);
1405
            level = get_bits(gb, v->s.esc3_level_length);
1406
            if(sign)
1407
                level = -level;
1408
        }
1409
    }
1410

    
1411
    *last = lst;
1412
    *skip = run;
1413
    *value = level;
1414
}
1415

    
1416
/** Decode intra block in intra frames - should be faster than decode_intra_block
1417
 * @param v VC1Context
1418
 * @param block block to decode
1419
 * @param[in] n subblock index
1420
 * @param coded are AC coeffs present or not
1421
 * @param codingset set of VLC to decode data
1422
 */
1423
static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset)
1424
{
1425
    GetBitContext *gb = &v->s.gb;
1426
    MpegEncContext *s = &v->s;
1427
    int dc_pred_dir = 0; /* Direction of the DC prediction used */
1428
    int i;
1429
    int16_t *dc_val;
1430
    int16_t *ac_val, *ac_val2;
1431
    int dcdiff;
1432

    
1433
    /* Get DC differential */
1434
    if (n < 4) {
1435
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1436
    } else {
1437
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1438
    }
1439
    if (dcdiff < 0){
1440
        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
1441
        return -1;
1442
    }
1443
    if (dcdiff)
1444
    {
1445
        if (dcdiff == 119 /* ESC index value */)
1446
        {
1447
            /* TODO: Optimize */
1448
            if (v->pq == 1) dcdiff = get_bits(gb, 10);
1449
            else if (v->pq == 2) dcdiff = get_bits(gb, 9);
1450
            else dcdiff = get_bits(gb, 8);
1451
        }
1452
        else
1453
        {
1454
            if (v->pq == 1)
1455
                dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
1456
            else if (v->pq == 2)
1457
                dcdiff = (dcdiff<<1) + get_bits1(gb)   - 1;
1458
        }
1459
        if (get_bits1(gb))
1460
            dcdiff = -dcdiff;
1461
    }
1462

    
1463
    /* Prediction */
1464
    dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
1465
    *dc_val = dcdiff;
1466

    
1467
    /* Store the quantized DC coeff, used for prediction */
1468
    if (n < 4) {
1469
        block[0] = dcdiff * s->y_dc_scale;
1470
    } else {
1471
        block[0] = dcdiff * s->c_dc_scale;
1472
    }
1473
    /* Skip ? */
1474
    if (!coded) {
1475
        goto not_coded;
1476
    }
1477

    
1478
    //AC Decoding
1479
    i = 1;
1480

    
1481
    {
1482
        int last = 0, skip, value;
1483
        const int8_t *zz_table;
1484
        int scale;
1485
        int k;
1486

    
1487
        scale = v->pq * 2 + v->halfpq;
1488

    
1489
        if(v->s.ac_pred) {
1490
            if(!dc_pred_dir)
1491
                zz_table = wmv1_scantable[2];
1492
            else
1493
                zz_table = wmv1_scantable[3];
1494
        } else
1495
            zz_table = wmv1_scantable[1];
1496

    
1497
        ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1498
        ac_val2 = ac_val;
1499
        if(dc_pred_dir) //left
1500
            ac_val -= 16;
1501
        else //top
1502
            ac_val -= 16 * s->block_wrap[n];
1503

    
1504
        while (!last) {
1505
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1506
            i += skip;
1507
            if(i > 63)
1508
                break;
1509
            block[zz_table[i++]] = value;
1510
        }
1511

    
1512
        /* apply AC prediction if needed */
1513
        if(s->ac_pred) {
1514
            if(dc_pred_dir) { //left
1515
                for(k = 1; k < 8; k++)
1516
                    block[k << 3] += ac_val[k];
1517
            } else { //top
1518
                for(k = 1; k < 8; k++)
1519
                    block[k] += ac_val[k + 8];
1520
            }
1521
        }
1522
        /* save AC coeffs for further prediction */
1523
        for(k = 1; k < 8; k++) {
1524
            ac_val2[k] = block[k << 3];
1525
            ac_val2[k + 8] = block[k];
1526
        }
1527

    
1528
        /* scale AC coeffs */
1529
        for(k = 1; k < 64; k++)
1530
            if(block[k]) {
1531
                block[k] *= scale;
1532
                if(!v->pquantizer)
1533
                    block[k] += (block[k] < 0) ? -v->pq : v->pq;
1534
            }
1535

    
1536
        if(s->ac_pred) i = 63;
1537
    }
1538

    
1539
not_coded:
1540
    if(!coded) {
1541
        int k, scale;
1542
        ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1543
        ac_val2 = ac_val;
1544

    
1545
        i = 0;
1546
        scale = v->pq * 2 + v->halfpq;
1547
        memset(ac_val2, 0, 16 * 2);
1548
        if(dc_pred_dir) {//left
1549
            ac_val -= 16;
1550
            if(s->ac_pred)
1551
                memcpy(ac_val2, ac_val, 8 * 2);
1552
        } else {//top
1553
            ac_val -= 16 * s->block_wrap[n];
1554
            if(s->ac_pred)
1555
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1556
        }
1557

    
1558
        /* apply AC prediction if needed */
1559
        if(s->ac_pred) {
1560
            if(dc_pred_dir) { //left
1561
                for(k = 1; k < 8; k++) {
1562
                    block[k << 3] = ac_val[k] * scale;
1563
                    if(!v->pquantizer && block[k << 3])
1564
                        block[k << 3] += (block[k << 3] < 0) ? -v->pq : v->pq;
1565
                }
1566
            } else { //top
1567
                for(k = 1; k < 8; k++) {
1568
                    block[k] = ac_val[k + 8] * scale;
1569
                    if(!v->pquantizer && block[k])
1570
                        block[k] += (block[k] < 0) ? -v->pq : v->pq;
1571
                }
1572
            }
1573
            i = 63;
1574
        }
1575
    }
1576
    s->block_last_index[n] = i;
1577

    
1578
    return 0;
1579
}
1580

    
1581
/** Decode intra block in intra frames - should be faster than decode_intra_block
1582
 * @param v VC1Context
1583
 * @param block block to decode
1584
 * @param[in] n subblock number
1585
 * @param coded are AC coeffs present or not
1586
 * @param codingset set of VLC to decode data
1587
 * @param mquant quantizer value for this macroblock
1588
 */
1589
static int vc1_decode_i_block_adv(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset, int mquant)
1590
{
1591
    GetBitContext *gb = &v->s.gb;
1592
    MpegEncContext *s = &v->s;
1593
    int dc_pred_dir = 0; /* Direction of the DC prediction used */
1594
    int i;
1595
    int16_t *dc_val;
1596
    int16_t *ac_val, *ac_val2;
1597
    int dcdiff;
1598
    int a_avail = v->a_avail, c_avail = v->c_avail;
1599
    int use_pred = s->ac_pred;
1600
    int scale;
1601
    int q1, q2 = 0;
1602
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1603

    
1604
    /* Get DC differential */
1605
    if (n < 4) {
1606
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1607
    } else {
1608
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1609
    }
1610
    if (dcdiff < 0){
1611
        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
1612
        return -1;
1613
    }
1614
    if (dcdiff)
1615
    {
1616
        if (dcdiff == 119 /* ESC index value */)
1617
        {
1618
            /* TODO: Optimize */
1619
            if (mquant == 1) dcdiff = get_bits(gb, 10);
1620
            else if (mquant == 2) dcdiff = get_bits(gb, 9);
1621
            else dcdiff = get_bits(gb, 8);
1622
        }
1623
        else
1624
        {
1625
            if (mquant == 1)
1626
                dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
1627
            else if (mquant == 2)
1628
                dcdiff = (dcdiff<<1) + get_bits1(gb)   - 1;
1629
        }
1630
        if (get_bits1(gb))
1631
            dcdiff = -dcdiff;
1632
    }
1633

    
1634
    /* Prediction */
1635
    dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
1636
    *dc_val = dcdiff;
1637

    
1638
    /* Store the quantized DC coeff, used for prediction */
1639
    if (n < 4) {
1640
        block[0] = dcdiff * s->y_dc_scale;
1641
    } else {
1642
        block[0] = dcdiff * s->c_dc_scale;
1643
    }
1644

    
1645
    //AC Decoding
1646
    i = 1;
1647

    
1648
    /* check if AC is needed at all */
1649
    if(!a_avail && !c_avail) use_pred = 0;
1650
    ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1651
    ac_val2 = ac_val;
1652

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

    
1655
    if(dc_pred_dir) //left
1656
        ac_val -= 16;
1657
    else //top
1658
        ac_val -= 16 * s->block_wrap[n];
1659

    
1660
    q1 = s->current_picture.qscale_table[mb_pos];
1661
    if(dc_pred_dir && c_avail && mb_pos) q2 = s->current_picture.qscale_table[mb_pos - 1];
1662
    if(!dc_pred_dir && a_avail && mb_pos >= s->mb_stride) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
1663
    if(dc_pred_dir && n==1) q2 = q1;
1664
    if(!dc_pred_dir && n==2) q2 = q1;
1665
    if(n==3) q2 = q1;
1666

    
1667
    if(coded) {
1668
        int last = 0, skip, value;
1669
        const int8_t *zz_table;
1670
        int k;
1671

    
1672
        if(v->s.ac_pred) {
1673
            if(!dc_pred_dir)
1674
                zz_table = wmv1_scantable[2];
1675
            else
1676
                zz_table = wmv1_scantable[3];
1677
        } else
1678
            zz_table = wmv1_scantable[1];
1679

    
1680
        while (!last) {
1681
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1682
            i += skip;
1683
            if(i > 63)
1684
                break;
1685
            block[zz_table[i++]] = value;
1686
        }
1687

    
1688
        /* apply AC prediction if needed */
1689
        if(use_pred) {
1690
            /* scale predictors if needed*/
1691
            if(q2 && q1!=q2) {
1692
                q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1693
                q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1694

    
1695
                if(dc_pred_dir) { //left
1696
                    for(k = 1; k < 8; k++)
1697
                        block[k << 3] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1698
                } else { //top
1699
                    for(k = 1; k < 8; k++)
1700
                        block[k] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1701
                }
1702
            } else {
1703
                if(dc_pred_dir) { //left
1704
                    for(k = 1; k < 8; k++)
1705
                        block[k << 3] += ac_val[k];
1706
                } else { //top
1707
                    for(k = 1; k < 8; k++)
1708
                        block[k] += ac_val[k + 8];
1709
                }
1710
            }
1711
        }
1712
        /* save AC coeffs for further prediction */
1713
        for(k = 1; k < 8; k++) {
1714
            ac_val2[k] = block[k << 3];
1715
            ac_val2[k + 8] = block[k];
1716
        }
1717

    
1718
        /* scale AC coeffs */
1719
        for(k = 1; k < 64; k++)
1720
            if(block[k]) {
1721
                block[k] *= scale;
1722
                if(!v->pquantizer)
1723
                    block[k] += (block[k] < 0) ? -mquant : mquant;
1724
            }
1725

    
1726
        if(use_pred) i = 63;
1727
    } else { // no AC coeffs
1728
        int k;
1729

    
1730
        memset(ac_val2, 0, 16 * 2);
1731
        if(dc_pred_dir) {//left
1732
            if(use_pred) {
1733
                memcpy(ac_val2, ac_val, 8 * 2);
1734
                if(q2 && q1!=q2) {
1735
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1736
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1737
                    for(k = 1; k < 8; k++)
1738
                        ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1739
                }
1740
            }
1741
        } else {//top
1742
            if(use_pred) {
1743
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1744
                if(q2 && q1!=q2) {
1745
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1746
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1747
                    for(k = 1; k < 8; k++)
1748
                        ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1749
                }
1750
            }
1751
        }
1752

    
1753
        /* apply AC prediction if needed */
1754
        if(use_pred) {
1755
            if(dc_pred_dir) { //left
1756
                for(k = 1; k < 8; k++) {
1757
                    block[k << 3] = ac_val2[k] * scale;
1758
                    if(!v->pquantizer && block[k << 3])
1759
                        block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
1760
                }
1761
            } else { //top
1762
                for(k = 1; k < 8; k++) {
1763
                    block[k] = ac_val2[k + 8] * scale;
1764
                    if(!v->pquantizer && block[k])
1765
                        block[k] += (block[k] < 0) ? -mquant : mquant;
1766
                }
1767
            }
1768
            i = 63;
1769
        }
1770
    }
1771
    s->block_last_index[n] = i;
1772

    
1773
    return 0;
1774
}
1775

    
1776
/** Decode intra block in inter frames - more generic version than vc1_decode_i_block
1777
 * @param v VC1Context
1778
 * @param block block to decode
1779
 * @param[in] n subblock index
1780
 * @param coded are AC coeffs present or not
1781
 * @param mquant block quantizer
1782
 * @param codingset set of VLC to decode data
1783
 */
1784
static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n, int coded, int mquant, int codingset)
1785
{
1786
    GetBitContext *gb = &v->s.gb;
1787
    MpegEncContext *s = &v->s;
1788
    int dc_pred_dir = 0; /* Direction of the DC prediction used */
1789
    int i;
1790
    int16_t *dc_val;
1791
    int16_t *ac_val, *ac_val2;
1792
    int dcdiff;
1793
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1794
    int a_avail = v->a_avail, c_avail = v->c_avail;
1795
    int use_pred = s->ac_pred;
1796
    int scale;
1797
    int q1, q2 = 0;
1798

    
1799
    s->dsp.clear_block(block);
1800

    
1801
    /* XXX: Guard against dumb values of mquant */
1802
    mquant = (mquant < 1) ? 0 : ( (mquant>31) ? 31 : mquant );
1803

    
1804
    /* Set DC scale - y and c use the same */
1805
    s->y_dc_scale = s->y_dc_scale_table[mquant];
1806
    s->c_dc_scale = s->c_dc_scale_table[mquant];
1807

    
1808
    /* Get DC differential */
1809
    if (n < 4) {
1810
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1811
    } else {
1812
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1813
    }
1814
    if (dcdiff < 0){
1815
        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
1816
        return -1;
1817
    }
1818
    if (dcdiff)
1819
    {
1820
        if (dcdiff == 119 /* ESC index value */)
1821
        {
1822
            /* TODO: Optimize */
1823
            if (mquant == 1) dcdiff = get_bits(gb, 10);
1824
            else if (mquant == 2) dcdiff = get_bits(gb, 9);
1825
            else dcdiff = get_bits(gb, 8);
1826
        }
1827
        else
1828
        {
1829
            if (mquant == 1)
1830
                dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
1831
            else if (mquant == 2)
1832
                dcdiff = (dcdiff<<1) + get_bits1(gb)   - 1;
1833
        }
1834
        if (get_bits1(gb))
1835
            dcdiff = -dcdiff;
1836
    }
1837

    
1838
    /* Prediction */
1839
    dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
1840
    *dc_val = dcdiff;
1841

    
1842
    /* Store the quantized DC coeff, used for prediction */
1843

    
1844
    if (n < 4) {
1845
        block[0] = dcdiff * s->y_dc_scale;
1846
    } else {
1847
        block[0] = dcdiff * s->c_dc_scale;
1848
    }
1849

    
1850
    //AC Decoding
1851
    i = 1;
1852

    
1853
    /* check if AC is needed at all and adjust direction if needed */
1854
    if(!a_avail) dc_pred_dir = 1;
1855
    if(!c_avail) dc_pred_dir = 0;
1856
    if(!a_avail && !c_avail) use_pred = 0;
1857
    ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1858
    ac_val2 = ac_val;
1859

    
1860
    scale = mquant * 2 + v->halfpq;
1861

    
1862
    if(dc_pred_dir) //left
1863
        ac_val -= 16;
1864
    else //top
1865
        ac_val -= 16 * s->block_wrap[n];
1866

    
1867
    q1 = s->current_picture.qscale_table[mb_pos];
1868
    if(dc_pred_dir && c_avail && mb_pos) q2 = s->current_picture.qscale_table[mb_pos - 1];
1869
    if(!dc_pred_dir && a_avail && mb_pos >= s->mb_stride) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
1870
    if(dc_pred_dir && n==1) q2 = q1;
1871
    if(!dc_pred_dir && n==2) q2 = q1;
1872
    if(n==3) q2 = q1;
1873

    
1874
    if(coded) {
1875
        int last = 0, skip, value;
1876
        const int8_t *zz_table;
1877
        int k;
1878

    
1879
        zz_table = wmv1_scantable[0];
1880

    
1881
        while (!last) {
1882
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1883
            i += skip;
1884
            if(i > 63)
1885
                break;
1886
            block[zz_table[i++]] = value;
1887
        }
1888

    
1889
        /* apply AC prediction if needed */
1890
        if(use_pred) {
1891
            /* scale predictors if needed*/
1892
            if(q2 && q1!=q2) {
1893
                q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1894
                q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1895

    
1896
                if(dc_pred_dir) { //left
1897
                    for(k = 1; k < 8; k++)
1898
                        block[k << 3] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1899
                } else { //top
1900
                    for(k = 1; k < 8; k++)
1901
                        block[k] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1902
                }
1903
            } else {
1904
                if(dc_pred_dir) { //left
1905
                    for(k = 1; k < 8; k++)
1906
                        block[k << 3] += ac_val[k];
1907
                } else { //top
1908
                    for(k = 1; k < 8; k++)
1909
                        block[k] += ac_val[k + 8];
1910
                }
1911
            }
1912
        }
1913
        /* save AC coeffs for further prediction */
1914
        for(k = 1; k < 8; k++) {
1915
            ac_val2[k] = block[k << 3];
1916
            ac_val2[k + 8] = block[k];
1917
        }
1918

    
1919
        /* scale AC coeffs */
1920
        for(k = 1; k < 64; k++)
1921
            if(block[k]) {
1922
                block[k] *= scale;
1923
                if(!v->pquantizer)
1924
                    block[k] += (block[k] < 0) ? -mquant : mquant;
1925
            }
1926

    
1927
        if(use_pred) i = 63;
1928
    } else { // no AC coeffs
1929
        int k;
1930

    
1931
        memset(ac_val2, 0, 16 * 2);
1932
        if(dc_pred_dir) {//left
1933
            if(use_pred) {
1934
                memcpy(ac_val2, ac_val, 8 * 2);
1935
                if(q2 && q1!=q2) {
1936
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1937
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1938
                    for(k = 1; k < 8; k++)
1939
                        ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1940
                }
1941
            }
1942
        } else {//top
1943
            if(use_pred) {
1944
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1945
                if(q2 && q1!=q2) {
1946
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1947
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1948
                    for(k = 1; k < 8; k++)
1949
                        ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1950
                }
1951
            }
1952
        }
1953

    
1954
        /* apply AC prediction if needed */
1955
        if(use_pred) {
1956
            if(dc_pred_dir) { //left
1957
                for(k = 1; k < 8; k++) {
1958
                    block[k << 3] = ac_val2[k] * scale;
1959
                    if(!v->pquantizer && block[k << 3])
1960
                        block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
1961
                }
1962
            } else { //top
1963
                for(k = 1; k < 8; k++) {
1964
                    block[k] = ac_val2[k + 8] * scale;
1965
                    if(!v->pquantizer && block[k])
1966
                        block[k] += (block[k] < 0) ? -mquant : mquant;
1967
                }
1968
            }
1969
            i = 63;
1970
        }
1971
    }
1972
    s->block_last_index[n] = i;
1973

    
1974
    return 0;
1975
}
1976

    
1977
/** Decode P block
1978
 */
1979
static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n, int mquant, int ttmb, int first_block,
1980
                              uint8_t *dst, int linesize, int skip_block, int apply_filter, int cbp_top, int cbp_left)
1981
{
1982
    MpegEncContext *s = &v->s;
1983
    GetBitContext *gb = &s->gb;
1984
    int i, j;
1985
    int subblkpat = 0;
1986
    int scale, off, idx, last, skip, value;
1987
    int ttblk = ttmb & 7;
1988
    int pat = 0;
1989

    
1990
    s->dsp.clear_block(block);
1991

    
1992
    if(ttmb == -1) {
1993
        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)];
1994
    }
1995
    if(ttblk == TT_4X4) {
1996
        subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
1997
    }
1998
    if((ttblk != TT_8X8 && ttblk != TT_4X4) && (v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))) {
1999
        subblkpat = decode012(gb);
2000
        if(subblkpat) subblkpat ^= 3; //swap decoded pattern bits
2001
        if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) ttblk = TT_8X4;
2002
        if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) ttblk = TT_4X8;
2003
    }
2004
    scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
2005

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

    
2131
/** @} */ // Macroblock group
2132

    
2133
static const int size_table  [6] = { 0, 2, 3, 4,  5,  8 };
2134
static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
2135

    
2136
/** Decode one P-frame MB (in Simple/Main profile)
2137
 */
2138
static int vc1_decode_p_mb(VC1Context *v)
2139
{
2140
    MpegEncContext *s = &v->s;
2141
    GetBitContext *gb = &s->gb;
2142
    int i, j;
2143
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2144
    int cbp; /* cbp decoding stuff */
2145
    int mqdiff, mquant; /* MB quantization */
2146
    int ttmb = v->ttfrm; /* MB Transform type */
2147

    
2148
    int mb_has_coeffs = 1; /* last_flag */
2149
    int dmv_x, dmv_y; /* Differential MV components */
2150
    int index, index1; /* LUT indexes */
2151
    int val, sign; /* temp values */
2152
    int first_block = 1;
2153
    int dst_idx, off;
2154
    int skipped, fourmv;
2155
    int block_cbp = 0, pat;
2156
    int apply_loop_filter;
2157

    
2158
    mquant = v->pq; /* Loosy initialization */
2159

    
2160
    if (v->mv_type_is_raw)
2161
        fourmv = get_bits1(gb);
2162
    else
2163
        fourmv = v->mv_type_mb_plane[mb_pos];
2164
    if (v->skip_is_raw)
2165
        skipped = get_bits1(gb);
2166
    else
2167
        skipped = v->s.mbskip_table[mb_pos];
2168

    
2169
    apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY);
2170
    if (!fourmv) /* 1MV mode */
2171
    {
2172
        if (!skipped)
2173
        {
2174
            GET_MVDATA(dmv_x, dmv_y);
2175

    
2176
            if (s->mb_intra) {
2177
                s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2178
                s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2179
            }
2180
            s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
2181
            vc1_pred_mv(s, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
2182

    
2183
            /* FIXME Set DC val for inter block ? */
2184
            if (s->mb_intra && !mb_has_coeffs)
2185
            {
2186
                GET_MQUANT();
2187
                s->ac_pred = get_bits1(gb);
2188
                cbp = 0;
2189
            }
2190
            else if (mb_has_coeffs)
2191
            {
2192
                if (s->mb_intra) s->ac_pred = get_bits1(gb);
2193
                cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2194
                GET_MQUANT();
2195
            }
2196
            else
2197
            {
2198
                mquant = v->pq;
2199
                cbp = 0;
2200
            }
2201
            s->current_picture.qscale_table[mb_pos] = mquant;
2202

    
2203
            if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
2204
                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
2205
                                VC1_TTMB_VLC_BITS, 2);
2206
            if(!s->mb_intra) vc1_mc_1mv(v, 0);
2207
            dst_idx = 0;
2208
            for (i=0; i<6; i++)
2209
            {
2210
                s->dc_val[0][s->block_index[i]] = 0;
2211
                dst_idx += i >> 2;
2212
                val = ((cbp >> (5 - i)) & 1);
2213
                off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2214
                v->mb_type[0][s->block_index[i]] = s->mb_intra;
2215
                if(s->mb_intra) {
2216
                    /* check if prediction blocks A and C are available */
2217
                    v->a_avail = v->c_avail = 0;
2218
                    if(i == 2 || i == 3 || !s->first_slice_line)
2219
                        v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2220
                    if(i == 1 || i == 3 || s->mb_x)
2221
                        v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2222

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

    
2354
                    vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant, (i&4)?v->codingset2:v->codingset);
2355
                    if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
2356
                    s->dsp.vc1_inv_trans_8x8(s->block[i]);
2357
                    if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
2358
                    s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
2359
                    if(v->pq >= 9 && v->overlap) {
2360
                        if(v->c_avail)
2361
                            s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
2362
                        if(v->a_avail)
2363
                            s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
2364
                    }
2365
                    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)){
2366
                        int left_cbp, top_cbp;
2367
                        if(i & 4){
2368
                            left_cbp = v->cbp[s->mb_x - 1]            >> (i * 4);
2369
                            top_cbp  = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
2370
                        }else{
2371
                            left_cbp = (i & 1) ? (cbp >> ((i-1)*4)) : (v->cbp[s->mb_x - 1]           >> ((i+1)*4));
2372
                            top_cbp  = (i & 2) ? (cbp >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));
2373
                        }
2374
                        if(left_cbp & 0xC)
2375
                            s->dsp.vc1_v_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2376
                        if(top_cbp  & 0xA)
2377
                            s->dsp.vc1_h_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2378
                    }
2379
                    block_cbp |= 0xF << (i << 2);
2380
                } else if(is_coded[i]) {
2381
                    int left_cbp = 0, top_cbp = 0, filter = 0;
2382
                    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)){
2383
                        filter = 1;
2384
                        if(i & 4){
2385
                            left_cbp = v->cbp[s->mb_x - 1]            >> (i * 4);
2386
                            top_cbp  = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
2387
                        }else{
2388
                            left_cbp = (i & 1) ? (cbp >> ((i-1)*4)) : (v->cbp[s->mb_x - 1]           >> ((i+1)*4));
2389
                            top_cbp  = (i & 2) ? (cbp >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));
2390
                        }
2391
                        if(left_cbp & 0xC)
2392
                            s->dsp.vc1_v_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2393
                        if(top_cbp  & 0xA)
2394
                            s->dsp.vc1_h_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2395
                    }
2396
                    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);
2397
                    block_cbp |= pat << (i << 2);
2398
                    if(!v->ttmbf && ttmb < 8) ttmb = -1;
2399
                    first_block = 0;
2400
                }
2401
            }
2402
            return 0;
2403
        }
2404
        else //Skipped MB
2405
        {
2406
            s->mb_intra = 0;
2407
            s->current_picture.qscale_table[mb_pos] = 0;
2408
            for (i=0; i<6; i++) {
2409
                v->mb_type[0][s->block_index[i]] = 0;
2410
                s->dc_val[0][s->block_index[i]] = 0;
2411
            }
2412
            for (i=0; i<4; i++)
2413
            {
2414
                vc1_pred_mv(s, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0]);
2415
                vc1_mc_4mv_luma(v, i);
2416
            }
2417
            vc1_mc_4mv_chroma(v);
2418
            s->current_picture.qscale_table[mb_pos] = 0;
2419
            return 0;
2420
        }
2421
    }
2422
    v->cbp[s->mb_x] = block_cbp;
2423

    
2424
    /* Should never happen */
2425
    return -1;
2426
}
2427

    
2428
/** Decode one B-frame MB (in Main profile)
2429
 */
2430
static void vc1_decode_b_mb(VC1Context *v)
2431
{
2432
    MpegEncContext *s = &v->s;
2433
    GetBitContext *gb = &s->gb;
2434
    int i, j;
2435
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2436
    int cbp = 0; /* cbp decoding stuff */
2437
    int mqdiff, mquant; /* MB quantization */
2438
    int ttmb = v->ttfrm; /* MB Transform type */
2439
    int mb_has_coeffs = 0; /* last_flag */
2440
    int index, index1; /* LUT indexes */
2441
    int val, sign; /* temp values */
2442
    int first_block = 1;
2443
    int dst_idx, off;
2444
    int skipped, direct;
2445
    int dmv_x[2], dmv_y[2];
2446
    int bmvtype = BMV_TYPE_BACKWARD;
2447

    
2448
    mquant = v->pq; /* Loosy initialization */
2449
    s->mb_intra = 0;
2450

    
2451
    if (v->dmb_is_raw)
2452
        direct = get_bits1(gb);
2453
    else
2454
        direct = v->direct_mb_plane[mb_pos];
2455
    if (v->skip_is_raw)
2456
        skipped = get_bits1(gb);
2457
    else
2458
        skipped = v->s.mbskip_table[mb_pos];
2459

    
2460
    dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
2461
    for(i = 0; i < 6; i++) {
2462
        v->mb_type[0][s->block_index[i]] = 0;
2463
        s->dc_val[0][s->block_index[i]] = 0;
2464
    }
2465
    s->current_picture.qscale_table[mb_pos] = 0;
2466

    
2467
    if (!direct) {
2468
        if (!skipped) {
2469
            GET_MVDATA(dmv_x[0], dmv_y[0]);
2470
            dmv_x[1] = dmv_x[0];
2471
            dmv_y[1] = dmv_y[0];
2472
        }
2473
        if(skipped || !s->mb_intra) {
2474
            bmvtype = decode012(gb);
2475
            switch(bmvtype) {
2476
            case 0:
2477
                bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
2478
                break;
2479
            case 1:
2480
                bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
2481
                break;
2482
            case 2:
2483
                bmvtype = BMV_TYPE_INTERPOLATED;
2484
                dmv_x[0] = dmv_y[0] = 0;
2485
            }
2486
        }
2487
    }
2488
    for(i = 0; i < 6; i++)
2489
        v->mb_type[0][s->block_index[i]] = s->mb_intra;
2490

    
2491
    if (skipped) {
2492
        if(direct) bmvtype = BMV_TYPE_INTERPOLATED;
2493
        vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2494
        vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2495
        return;
2496
    }
2497
    if (direct) {
2498
        cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2499
        GET_MQUANT();
2500
        s->mb_intra = 0;
2501
        s->current_picture.qscale_table[mb_pos] = mquant;
2502
        if(!v->ttmbf)
2503
            ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2504
        dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
2505
        vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2506
        vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2507
    } else {
2508
        if(!mb_has_coeffs && !s->mb_intra) {
2509
            /* no coded blocks - effectively skipped */
2510
            vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2511
            vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2512
            return;
2513
        }
2514
        if(s->mb_intra && !mb_has_coeffs) {
2515
            GET_MQUANT();
2516
            s->current_picture.qscale_table[mb_pos] = mquant;
2517
            s->ac_pred = get_bits1(gb);
2518
            cbp = 0;
2519
            vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2520
        } else {
2521
            if(bmvtype == BMV_TYPE_INTERPOLATED) {
2522
                GET_MVDATA(dmv_x[0], dmv_y[0]);
2523
                if(!mb_has_coeffs) {
2524
                    /* interpolated skipped block */
2525
                    vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2526
                    vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2527
                    return;
2528
                }
2529
            }
2530
            vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2531
            if(!s->mb_intra) {
2532
                vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2533
            }
2534
            if(s->mb_intra)
2535
                s->ac_pred = get_bits1(gb);
2536
            cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2537
            GET_MQUANT();
2538
            s->current_picture.qscale_table[mb_pos] = mquant;
2539
            if(!v->ttmbf && !s->mb_intra && mb_has_coeffs)
2540
                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2541
        }
2542
    }
2543
    dst_idx = 0;
2544
    for (i=0; i<6; i++)
2545
    {
2546
        s->dc_val[0][s->block_index[i]] = 0;
2547
        dst_idx += i >> 2;
2548
        val = ((cbp >> (5 - i)) & 1);
2549
        off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2550
        v->mb_type[0][s->block_index[i]] = s->mb_intra;
2551
        if(s->mb_intra) {
2552
            /* check if prediction blocks A and C are available */
2553
            v->a_avail = v->c_avail = 0;
2554
            if(i == 2 || i == 3 || !s->first_slice_line)
2555
                v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2556
            if(i == 1 || i == 3 || s->mb_x)
2557
                v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2558

    
2559
            vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
2560
            if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
2561
            s->dsp.vc1_inv_trans_8x8(s->block[i]);
2562
            if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
2563
            s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
2564
        } else if(val) {
2565
            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);
2566
            if(!v->ttmbf && ttmb < 8) ttmb = -1;
2567
            first_block = 0;
2568
        }
2569
    }
2570
}
2571

    
2572
/** Decode blocks of I-frame
2573
 */
2574
static void vc1_decode_i_blocks(VC1Context *v)
2575
{
2576
    int k, j;
2577
    MpegEncContext *s = &v->s;
2578
    int cbp, val;
2579
    uint8_t *coded_val;
2580
    int mb_pos;
2581

    
2582
    /* select codingmode used for VLC tables selection */
2583
    switch(v->y_ac_table_index){
2584
    case 0:
2585
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2586
        break;
2587
    case 1:
2588
        v->codingset = CS_HIGH_MOT_INTRA;
2589
        break;
2590
    case 2:
2591
        v->codingset = CS_MID_RATE_INTRA;
2592
        break;
2593
    }
2594

    
2595
    switch(v->c_ac_table_index){
2596
    case 0:
2597
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2598
        break;
2599
    case 1:
2600
        v->codingset2 = CS_HIGH_MOT_INTER;
2601
        break;
2602
    case 2:
2603
        v->codingset2 = CS_MID_RATE_INTER;
2604
        break;
2605
    }
2606

    
2607
    /* Set DC scale - y and c use the same */
2608
    s->y_dc_scale = s->y_dc_scale_table[v->pq];
2609
    s->c_dc_scale = s->c_dc_scale_table[v->pq];
2610

    
2611
    //do frame decode
2612
    s->mb_x = s->mb_y = 0;
2613
    s->mb_intra = 1;
2614
    s->first_slice_line = 1;
2615
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2616
        s->mb_x = 0;
2617
        ff_init_block_index(s);
2618
        for(; s->mb_x < s->mb_width; s->mb_x++) {
2619
            ff_update_block_index(s);
2620
            s->dsp.clear_blocks(s->block[0]);
2621
            mb_pos = s->mb_x + s->mb_y * s->mb_width;
2622
            s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2623
            s->current_picture.qscale_table[mb_pos] = v->pq;
2624
            s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2625
            s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2626

    
2627
            // do actual MB decoding and displaying
2628
            cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2629
            v->s.ac_pred = get_bits1(&v->s.gb);
2630

    
2631
            for(k = 0; k < 6; k++) {
2632
                val = ((cbp >> (5 - k)) & 1);
2633

    
2634
                if (k < 4) {
2635
                    int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2636
                    val = val ^ pred;
2637
                    *coded_val = val;
2638
                }
2639
                cbp |= val << (5 - k);
2640

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

    
2643
                s->dsp.vc1_inv_trans_8x8(s->block[k]);
2644
                if(v->pq >= 9 && v->overlap) {
2645
                    for(j = 0; j < 64; j++) s->block[k][j] += 128;
2646
                }
2647
            }
2648

    
2649
            vc1_put_block(v, s->block);
2650
            if(v->pq >= 9 && v->overlap) {
2651
                if(s->mb_x) {
2652
                    s->dsp.vc1_h_overlap(s->dest[0], s->linesize);
2653
                    s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2654
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
2655
                        s->dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
2656
                        s->dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
2657
                    }
2658
                }
2659
                s->dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
2660
                s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2661
                if(!s->first_slice_line) {
2662
                    s->dsp.vc1_v_overlap(s->dest[0], s->linesize);
2663
                    s->dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
2664
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
2665
                        s->dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
2666
                        s->dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
2667
                    }
2668
                }
2669
                s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2670
                s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2671
            }
2672
            if(v->s.loop_filter) vc1_loop_filter_iblk(s, v->pq);
2673

    
2674
            if(get_bits_count(&s->gb) > v->bits) {
2675
                ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
2676
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
2677
                return;
2678
            }
2679
        }
2680
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
2681
        s->first_slice_line = 0;
2682
    }
2683
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2684
}
2685

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

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

    
2713
    switch(v->c_ac_table_index){
2714
    case 0:
2715
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2716
        break;
2717
    case 1:
2718
        v->codingset2 = CS_HIGH_MOT_INTER;
2719
        break;
2720
    case 2:
2721
        v->codingset2 = CS_MID_RATE_INTER;
2722
        break;
2723
    }
2724

    
2725
    //do frame decode
2726
    s->mb_x = s->mb_y = 0;
2727
    s->mb_intra = 1;
2728
    s->first_slice_line = 1;
2729
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2730
        s->mb_x = 0;
2731
        ff_init_block_index(s);
2732
        for(;s->mb_x < s->mb_width; s->mb_x++) {
2733
            ff_update_block_index(s);
2734
            s->dsp.clear_blocks(s->block[0]);
2735
            mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2736
            s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2737
            s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2738
            s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2739

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

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

    
2755
            GET_MQUANT();
2756

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

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

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

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

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

    
2777
                s->dsp.vc1_inv_trans_8x8(s->block[k]);
2778
                for(j = 0; j < 64; j++) s->block[k][j] += 128;
2779
            }
2780

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

    
2806
            if(get_bits_count(&s->gb) > v->bits) {
2807
                ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
2808
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
2809
                return;
2810
            }
2811
        }
2812
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
2813
        s->first_slice_line = 0;
2814
    }
2815
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2816
}
2817

    
2818
static void vc1_decode_p_blocks(VC1Context *v)
2819
{
2820
    MpegEncContext *s = &v->s;
2821

    
2822
    /* select codingmode used for VLC tables selection */
2823
    switch(v->c_ac_table_index){
2824
    case 0:
2825
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2826
        break;
2827
    case 1:
2828
        v->codingset = CS_HIGH_MOT_INTRA;
2829
        break;
2830
    case 2:
2831
        v->codingset = CS_MID_RATE_INTRA;
2832
        break;
2833
    }
2834

    
2835
    switch(v->c_ac_table_index){
2836
    case 0:
2837
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2838
        break;
2839
    case 1:
2840
        v->codingset2 = CS_HIGH_MOT_INTER;
2841
        break;
2842
    case 2:
2843
        v->codingset2 = CS_MID_RATE_INTER;
2844
        break;
2845
    }
2846

    
2847
    s->first_slice_line = 1;
2848
    memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
2849
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2850
        s->mb_x = 0;
2851
        ff_init_block_index(s);
2852
        for(; s->mb_x < s->mb_width; s->mb_x++) {
2853
            ff_update_block_index(s);
2854

    
2855
            vc1_decode_p_mb(v);
2856
            if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2857
                ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
2858
                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);
2859
                return;
2860
            }
2861
        }
2862
        memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0])*s->mb_stride);
2863
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
2864
        s->first_slice_line = 0;
2865
    }
2866
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2867
}
2868

    
2869
static void vc1_decode_b_blocks(VC1Context *v)
2870
{
2871
    MpegEncContext *s = &v->s;
2872

    
2873
    /* select codingmode used for VLC tables selection */
2874
    switch(v->c_ac_table_index){
2875
    case 0:
2876
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2877
        break;
2878
    case 1:
2879
        v->codingset = CS_HIGH_MOT_INTRA;
2880
        break;
2881
    case 2:
2882
        v->codingset = CS_MID_RATE_INTRA;
2883
        break;
2884
    }
2885

    
2886
    switch(v->c_ac_table_index){
2887
    case 0:
2888
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2889
        break;
2890
    case 1:
2891
        v->codingset2 = CS_HIGH_MOT_INTER;
2892
        break;
2893
    case 2:
2894
        v->codingset2 = CS_MID_RATE_INTER;
2895
        break;
2896
    }
2897

    
2898
    s->first_slice_line = 1;
2899
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2900
        s->mb_x = 0;
2901
        ff_init_block_index(s);
2902
        for(; s->mb_x < s->mb_width; s->mb_x++) {
2903
            ff_update_block_index(s);
2904

    
2905
            vc1_decode_b_mb(v);
2906
            if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2907
                ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
2908
                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);
2909
                return;
2910
            }
2911
            if(v->s.loop_filter) vc1_loop_filter_iblk(s, v->pq);
2912
        }
2913
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
2914
        s->first_slice_line = 0;
2915
    }
2916
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2917
}
2918

    
2919
static void vc1_decode_skip_blocks(VC1Context *v)
2920
{
2921
    MpegEncContext *s = &v->s;
2922

    
2923
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2924
    s->first_slice_line = 1;
2925
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2926
        s->mb_x = 0;
2927
        ff_init_block_index(s);
2928
        ff_update_block_index(s);
2929
        memcpy(s->dest[0], s->last_picture.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
2930
        memcpy(s->dest[1], s->last_picture.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
2931
        memcpy(s->dest[2], s->last_picture.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
2932
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
2933
        s->first_slice_line = 0;
2934
    }
2935
    s->pict_type = FF_P_TYPE;
2936
}
2937

    
2938
static void vc1_decode_blocks(VC1Context *v)
2939
{
2940

    
2941
    v->s.esc3_level_length = 0;
2942
    if(v->x8_type){
2943
        ff_intrax8_decode_picture(&v->x8, 2*v->pq+v->halfpq, v->pq*(!v->pquantizer) );
2944
    }else{
2945

    
2946
        switch(v->s.pict_type) {
2947
        case FF_I_TYPE:
2948
            if(v->profile == PROFILE_ADVANCED)
2949
                vc1_decode_i_blocks_adv(v);
2950
            else
2951
                vc1_decode_i_blocks(v);
2952
            break;
2953
        case FF_P_TYPE:
2954
            if(v->p_frame_skipped)
2955
                vc1_decode_skip_blocks(v);
2956
            else
2957
                vc1_decode_p_blocks(v);
2958
            break;
2959
        case FF_B_TYPE:
2960
            if(v->bi_type){
2961
                if(v->profile == PROFILE_ADVANCED)
2962
                    vc1_decode_i_blocks_adv(v);
2963
                else
2964
                    vc1_decode_i_blocks(v);
2965
            }else
2966
                vc1_decode_b_blocks(v);
2967
            break;
2968
        }
2969
    }
2970
}
2971

    
2972
/** Initialize a VC1/WMV3 decoder
2973
 * @todo TODO: Handle VC-1 IDUs (Transport level?)
2974
 * @todo TODO: Decypher remaining bits in extra_data
2975
 */
2976
static av_cold int vc1_decode_init(AVCodecContext *avctx)
2977
{
2978
    VC1Context *v = avctx->priv_data;
2979
    MpegEncContext *s = &v->s;
2980
    GetBitContext gb;
2981

    
2982
    if (!avctx->extradata_size || !avctx->extradata) return -1;
2983
    if (!(avctx->flags & CODEC_FLAG_GRAY))
2984
        avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
2985
    else
2986
        avctx->pix_fmt = PIX_FMT_GRAY8;
2987
    avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
2988
    v->s.avctx = avctx;
2989
    avctx->flags |= CODEC_FLAG_EMU_EDGE;
2990
    v->s.flags |= CODEC_FLAG_EMU_EDGE;
2991

    
2992
    if(avctx->idct_algo==FF_IDCT_AUTO){
2993
        avctx->idct_algo=FF_IDCT_WMV2;
2994
    }
2995

    
2996
    if(ff_h263_decode_init(avctx) < 0)
2997
        return -1;
2998
    if (vc1_init_common(v) < 0) return -1;
2999
    // only for ff_msmp4_mb_i_table
3000
    if (ff_msmpeg4_decode_init(s) < 0) return -1;
3001

    
3002
    avctx->coded_width = avctx->width;
3003
    avctx->coded_height = avctx->height;
3004
    if (avctx->codec_id == CODEC_ID_WMV3)
3005
    {
3006
        int count = 0;
3007

    
3008
        // looks like WMV3 has a sequence header stored in the extradata
3009
        // advanced sequence header may be before the first frame
3010
        // the last byte of the extradata is a version number, 1 for the
3011
        // samples we can decode
3012

    
3013
        init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
3014

    
3015
        if (vc1_decode_sequence_header(avctx, v, &gb) < 0)
3016
          return -1;
3017

    
3018
        count = avctx->extradata_size*8 - get_bits_count(&gb);
3019
        if (count>0)
3020
        {
3021
            av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
3022
                   count, get_bits(&gb, count));
3023
        }
3024
        else if (count < 0)
3025
        {
3026
            av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
3027
        }
3028
    } else { // VC1/WVC1
3029
        const uint8_t *start = avctx->extradata;
3030
        uint8_t *end = avctx->extradata + avctx->extradata_size;
3031
        const uint8_t *next;
3032
        int size, buf2_size;
3033
        uint8_t *buf2 = NULL;
3034
        int seq_initialized = 0, ep_initialized = 0;
3035

    
3036
        if(avctx->extradata_size < 16) {
3037
            av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
3038
            return -1;
3039
        }
3040

    
3041
        buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
3042
        start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
3043
        next = start;
3044
        for(; next < end; start = next){
3045
            next = find_next_marker(start + 4, end);
3046
            size = next - start - 4;
3047
            if(size <= 0) continue;
3048
            buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
3049
            init_get_bits(&gb, buf2, buf2_size * 8);
3050
            switch(AV_RB32(start)){
3051
            case VC1_CODE_SEQHDR:
3052
                if(vc1_decode_sequence_header(avctx, v, &gb) < 0){
3053
                    av_free(buf2);
3054
                    return -1;
3055
                }
3056
                seq_initialized = 1;
3057
                break;
3058
            case VC1_CODE_ENTRYPOINT:
3059
                if(vc1_decode_entry_point(avctx, v, &gb) < 0){
3060
                    av_free(buf2);
3061
                    return -1;
3062
                }
3063
                ep_initialized = 1;
3064
                break;
3065
            }
3066
        }
3067
        av_free(buf2);
3068
        if(!seq_initialized || !ep_initialized){
3069
            av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
3070
            return -1;
3071
        }
3072
    }
3073
    avctx->has_b_frames= !!(avctx->max_b_frames);
3074
    s->low_delay = !avctx->has_b_frames;
3075

    
3076
    s->mb_width = (avctx->coded_width+15)>>4;
3077
    s->mb_height = (avctx->coded_height+15)>>4;
3078

    
3079
    /* Allocate mb bitplanes */
3080
    v->mv_type_mb_plane = av_malloc(s->mb_stride * s->mb_height);
3081
    v->direct_mb_plane = av_malloc(s->mb_stride * s->mb_height);
3082
    v->acpred_plane = av_malloc(s->mb_stride * s->mb_height);
3083
    v->over_flags_plane = av_malloc(s->mb_stride * s->mb_height);
3084

    
3085
    v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
3086
    v->cbp = v->cbp_base + s->mb_stride;
3087

    
3088
    /* allocate block type info in that way so it could be used with s->block_index[] */
3089
    v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
3090
    v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
3091
    v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
3092
    v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
3093

    
3094
    /* Init coded blocks info */
3095
    if (v->profile == PROFILE_ADVANCED)
3096
    {
3097
//        if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
3098
//            return -1;
3099
//        if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
3100
//            return -1;
3101
    }
3102

    
3103
    ff_intrax8_common_init(&v->x8,s);
3104
    return 0;
3105
}
3106

    
3107

    
3108
/** Decode a VC1/WMV3 frame
3109
 * @todo TODO: Handle VC-1 IDUs (Transport level?)
3110
 */
3111
static int vc1_decode_frame(AVCodecContext *avctx,
3112
                            void *data, int *data_size,
3113
                            AVPacket *avpkt)
3114
{
3115
    const uint8_t *buf = avpkt->data;
3116
    int buf_size = avpkt->size;
3117
    VC1Context *v = avctx->priv_data;
3118
    MpegEncContext *s = &v->s;
3119
    AVFrame *pict = data;
3120
    uint8_t *buf2 = NULL;
3121
    const uint8_t *buf_start = buf;
3122

    
3123
    /* no supplementary picture */
3124
    if (buf_size == 0) {
3125
        /* special case for last picture */
3126
        if (s->low_delay==0 && s->next_picture_ptr) {
3127
            *pict= *(AVFrame*)s->next_picture_ptr;
3128
            s->next_picture_ptr= NULL;
3129

    
3130
            *data_size = sizeof(AVFrame);
3131
        }
3132

    
3133
        return 0;
3134
    }
3135

    
3136
    /* We need to set current_picture_ptr before reading the header,
3137
     * otherwise we cannot store anything in there. */
3138
    if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
3139
        int i= ff_find_unused_picture(s, 0);
3140
        s->current_picture_ptr= &s->picture[i];
3141
    }
3142

    
3143
    if (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){
3144
        if (v->profile < PROFILE_ADVANCED)
3145
            avctx->pix_fmt = PIX_FMT_VDPAU_WMV3;
3146
        else
3147
            avctx->pix_fmt = PIX_FMT_VDPAU_VC1;
3148
    }
3149

    
3150
    //for advanced profile we may need to parse and unescape data
3151
    if (avctx->codec_id == CODEC_ID_VC1) {
3152
        int buf_size2 = 0;
3153
        buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
3154

    
3155
        if(IS_MARKER(AV_RB32(buf))){ /* frame starts with marker and needs to be parsed */
3156
            const uint8_t *start, *end, *next;
3157
            int size;
3158

    
3159
            next = buf;
3160
            for(start = buf, end = buf + buf_size; next < end; start = next){
3161
                next = find_next_marker(start + 4, end);
3162
                size = next - start - 4;
3163
                if(size <= 0) continue;
3164
                switch(AV_RB32(start)){
3165
                case VC1_CODE_FRAME:
3166
                    if (avctx->hwaccel ||
3167
                        s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
3168
                        buf_start = start;
3169
                    buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
3170
                    break;
3171
                case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
3172
                    buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
3173
                    init_get_bits(&s->gb, buf2, buf_size2*8);
3174
                    vc1_decode_entry_point(avctx, v, &s->gb);
3175
                    break;
3176
                case VC1_CODE_SLICE:
3177
                    av_log(avctx, AV_LOG_ERROR, "Sliced decoding is not implemented (yet)\n");
3178
                    av_free(buf2);
3179
                    return -1;
3180
                }
3181
            }
3182
        }else if(v->interlace && ((buf[0] & 0xC0) == 0xC0)){ /* WVC1 interlaced stores both fields divided by marker */
3183
            const uint8_t *divider;
3184

    
3185
            divider = find_next_marker(buf, buf + buf_size);
3186
            if((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD){
3187
                av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
3188
                av_free(buf2);
3189
                return -1;
3190
            }
3191

    
3192
            buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
3193
            // TODO
3194
            av_free(buf2);return -1;
3195
        }else{
3196
            buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
3197
        }
3198
        init_get_bits(&s->gb, buf2, buf_size2*8);
3199
    } else
3200
        init_get_bits(&s->gb, buf, buf_size*8);
3201
    // do parse frame header
3202
    if(v->profile < PROFILE_ADVANCED) {
3203
        if(vc1_parse_frame_header(v, &s->gb) == -1) {
3204
            av_free(buf2);
3205
            return -1;
3206
        }
3207
    } else {
3208
        if(vc1_parse_frame_header_adv(v, &s->gb) == -1) {
3209
            av_free(buf2);
3210
            return -1;
3211
        }
3212
    }
3213

    
3214
    if(s->pict_type != FF_I_TYPE && !v->res_rtm_flag){
3215
        av_free(buf2);
3216
        return -1;
3217
    }
3218

    
3219
    // for hurry_up==5
3220
    s->current_picture.pict_type= s->pict_type;
3221
    s->current_picture.key_frame= s->pict_type == FF_I_TYPE;
3222

    
3223
    /* skip B-frames if we don't have reference frames */
3224
    if(s->last_picture_ptr==NULL && (s->pict_type==FF_B_TYPE || s->dropable)){
3225
        av_free(buf2);
3226
        return -1;//buf_size;
3227
    }
3228
    /* skip b frames if we are in a hurry */
3229
    if(avctx->hurry_up && s->pict_type==FF_B_TYPE) return -1;//buf_size;
3230
    if(   (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==FF_B_TYPE)
3231
       || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=FF_I_TYPE)
3232
       ||  avctx->skip_frame >= AVDISCARD_ALL) {
3233
        av_free(buf2);
3234
        return buf_size;
3235
    }
3236
    /* skip everything if we are in a hurry>=5 */
3237
    if(avctx->hurry_up>=5) {
3238
        av_free(buf2);
3239
        return -1;//buf_size;
3240
    }
3241

    
3242
    if(s->next_p_frame_damaged){
3243
        if(s->pict_type==FF_B_TYPE)
3244
            return buf_size;
3245
        else
3246
            s->next_p_frame_damaged=0;
3247
    }
3248

    
3249
    if(MPV_frame_start(s, avctx) < 0) {
3250
        av_free(buf2);
3251
        return -1;
3252
    }
3253

    
3254
    s->me.qpel_put= s->dsp.put_qpel_pixels_tab;
3255
    s->me.qpel_avg= s->dsp.avg_qpel_pixels_tab;
3256

    
3257
    if ((CONFIG_VC1_VDPAU_DECODER)
3258
        &&s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
3259
        ff_vdpau_vc1_decode_picture(s, buf_start, (buf + buf_size) - buf_start);
3260
    else if (avctx->hwaccel) {
3261
        if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
3262
            return -1;
3263
        if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
3264
            return -1;
3265
        if (avctx->hwaccel->end_frame(avctx) < 0)
3266
            return -1;
3267
    } else {
3268
        ff_er_frame_start(s);
3269

    
3270
        v->bits = buf_size * 8;
3271
        vc1_decode_blocks(v);
3272
//av_log(s->avctx, AV_LOG_INFO, "Consumed %i/%i bits\n", get_bits_count(&s->gb), buf_size*8);
3273
//  if(get_bits_count(&s->gb) > buf_size * 8)
3274
//      return -1;
3275
        ff_er_frame_end(s);
3276
    }
3277

    
3278
    MPV_frame_end(s);
3279

    
3280
assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
3281
assert(s->current_picture.pict_type == s->pict_type);
3282
    if (s->pict_type == FF_B_TYPE || s->low_delay) {
3283
        *pict= *(AVFrame*)s->current_picture_ptr;
3284
    } else if (s->last_picture_ptr != NULL) {
3285
        *pict= *(AVFrame*)s->last_picture_ptr;
3286
    }
3287

    
3288
    if(s->last_picture_ptr || s->low_delay){
3289
        *data_size = sizeof(AVFrame);
3290
        ff_print_debug_info(s, pict);
3291
    }
3292

    
3293
    av_free(buf2);
3294
    return buf_size;
3295
}
3296

    
3297

    
3298
/** Close a VC1/WMV3 decoder
3299
 * @warning Initial try at using MpegEncContext stuff
3300
 */
3301
static av_cold int vc1_decode_end(AVCodecContext *avctx)
3302
{
3303
    VC1Context *v = avctx->priv_data;
3304

    
3305
    av_freep(&v->hrd_rate);
3306
    av_freep(&v->hrd_buffer);
3307
    MPV_common_end(&v->s);
3308
    av_freep(&v->mv_type_mb_plane);
3309
    av_freep(&v->direct_mb_plane);
3310
    av_freep(&v->acpred_plane);
3311
    av_freep(&v->over_flags_plane);
3312
    av_freep(&v->mb_type_base);
3313
    av_freep(&v->cbp_base);
3314
    ff_intrax8_common_end(&v->x8);
3315
    return 0;
3316
}
3317

    
3318

    
3319
AVCodec vc1_decoder = {
3320
    "vc1",
3321
    CODEC_TYPE_VIDEO,
3322
    CODEC_ID_VC1,
3323
    sizeof(VC1Context),
3324
    vc1_decode_init,
3325
    NULL,
3326
    vc1_decode_end,
3327
    vc1_decode_frame,
3328
    CODEC_CAP_DR1 | CODEC_CAP_DELAY,
3329
    NULL,
3330
    .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
3331
    .pix_fmts = ff_hwaccel_pixfmt_list_420
3332
};
3333

    
3334
#if CONFIG_WMV3_DECODER
3335
AVCodec wmv3_decoder = {
3336
    "wmv3",
3337
    CODEC_TYPE_VIDEO,
3338
    CODEC_ID_WMV3,
3339
    sizeof(VC1Context),
3340
    vc1_decode_init,
3341
    NULL,
3342
    vc1_decode_end,
3343
    vc1_decode_frame,
3344
    CODEC_CAP_DR1 | CODEC_CAP_DELAY,
3345
    NULL,
3346
    .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
3347
    .pix_fmts = ff_hwaccel_pixfmt_list_420
3348
};
3349
#endif
3350

    
3351
#if CONFIG_WMV3_VDPAU_DECODER
3352
AVCodec wmv3_vdpau_decoder = {
3353
    "wmv3_vdpau",
3354
    CODEC_TYPE_VIDEO,
3355
    CODEC_ID_WMV3,
3356
    sizeof(VC1Context),
3357
    vc1_decode_init,
3358
    NULL,
3359
    vc1_decode_end,
3360
    vc1_decode_frame,
3361
    CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
3362
    NULL,
3363
    .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
3364
    .pix_fmts = (const enum PixelFormat[]){PIX_FMT_VDPAU_WMV3, PIX_FMT_NONE}
3365
};
3366
#endif
3367

    
3368
#if CONFIG_VC1_VDPAU_DECODER
3369
AVCodec vc1_vdpau_decoder = {
3370
    "vc1_vdpau",
3371
    CODEC_TYPE_VIDEO,
3372
    CODEC_ID_VC1,
3373
    sizeof(VC1Context),
3374
    vc1_decode_init,
3375
    NULL,
3376
    vc1_decode_end,
3377
    vc1_decode_frame,
3378
    CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
3379
    NULL,
3380
    .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
3381
    .pix_fmts = (const enum PixelFormat[]){PIX_FMT_VDPAU_VC1, PIX_FMT_NONE}
3382
};
3383
#endif