Statistics
| Branch: | Revision:

ffmpeg / libavcodec / vc1dec.c @ 1da6ea39

History | View | Annotate | Download (123 KB)

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

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

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

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

    
50

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

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

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

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

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

    
135
    return 0;
136
}
137

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

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

    
160

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

    
163
static void vc1_loop_filter_iblk(MpegEncContext *s, int pq)
164
{
165
    int j;
166
    if (!s->first_slice_line) {
167
        s->dsp.vc1_v_loop_filter16(s->dest[0], s->linesize, pq);
168
        if (s->mb_x)
169
            s->dsp.vc1_h_loop_filter16(s->dest[0] - 16*s->linesize, s->linesize, pq);
170
        s->dsp.vc1_h_loop_filter16(s->dest[0] - 16*s->linesize+8, s->linesize, pq);
171
        for(j = 0; j < 2; j++){
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]-8*s->uvlinesize, s->uvlinesize, pq);
175
        }
176
    }
177
    s->dsp.vc1_v_loop_filter16(s->dest[0] + 8*s->linesize, s->linesize, pq);
178

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

    
189
/** Put block onto picture
190
 */
191
static void vc1_put_block(VC1Context *v, DCTELEM block[6][64])
192
{
193
    uint8_t *Y;
194
    int ys, us, vs;
195
    DSPContext *dsp = &v->s.dsp;
196

    
197
    if(v->rangeredfrm) {
198
        int i, j, k;
199
        for(k = 0; k < 6; k++)
200
            for(j = 0; j < 8; j++)
201
                for(i = 0; i < 8; i++)
202
                    block[k][i + j*8] = ((block[k][i + j*8] - 128) << 1) + 128;
203

    
204
    }
205
    ys = v->s.current_picture.linesize[0];
206
    us = v->s.current_picture.linesize[1];
207
    vs = v->s.current_picture.linesize[2];
208
    Y = v->s.dest[0];
209

    
210
    dsp->put_pixels_clamped(block[0], Y, ys);
211
    dsp->put_pixels_clamped(block[1], Y + 8, ys);
212
    Y += ys * 8;
213
    dsp->put_pixels_clamped(block[2], Y, ys);
214
    dsp->put_pixels_clamped(block[3], Y + 8, ys);
215

    
216
    if(!(v->s.flags & CODEC_FLAG_GRAY)) {
217
        dsp->put_pixels_clamped(block[4], v->s.dest[1], us);
218
        dsp->put_pixels_clamped(block[5], v->s.dest[2], vs);
219
    }
220
}
221

    
222
/** Do motion compensation over 1 macroblock
223
 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
224
 */
225
static void vc1_mc_1mv(VC1Context *v, int dir)
226
{
227
    MpegEncContext *s = &v->s;
228
    DSPContext *dsp = &v->s.dsp;
229
    uint8_t *srcY, *srcU, *srcV;
230
    int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
231

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

    
234
    mx = s->mv[dir][0][0];
235
    my = s->mv[dir][0][1];
236

    
237
    // store motion vectors for further use in B frames
238
    if(s->pict_type == FF_P_TYPE) {
239
        s->current_picture.motion_val[1][s->block_index[0]][0] = mx;
240
        s->current_picture.motion_val[1][s->block_index[0]][1] = my;
241
    }
242
    uvmx = (mx + ((mx & 3) == 3)) >> 1;
243
    uvmy = (my + ((my & 3) == 3)) >> 1;
244
    if(v->fastuvmc) {
245
        uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
246
        uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
247
    }
248
    if(!dir) {
249
        srcY = s->last_picture.data[0];
250
        srcU = s->last_picture.data[1];
251
        srcV = s->last_picture.data[2];
252
    } else {
253
        srcY = s->next_picture.data[0];
254
        srcU = s->next_picture.data[1];
255
        srcV = s->next_picture.data[2];
256
    }
257

    
258
    src_x = s->mb_x * 16 + (mx >> 2);
259
    src_y = s->mb_y * 16 + (my >> 2);
260
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
261
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
262

    
263
    if(v->profile != PROFILE_ADVANCED){
264
        src_x   = av_clip(  src_x, -16, s->mb_width  * 16);
265
        src_y   = av_clip(  src_y, -16, s->mb_height * 16);
266
        uvsrc_x = av_clip(uvsrc_x,  -8, s->mb_width  *  8);
267
        uvsrc_y = av_clip(uvsrc_y,  -8, s->mb_height *  8);
268
    }else{
269
        src_x   = av_clip(  src_x, -17, s->avctx->coded_width);
270
        src_y   = av_clip(  src_y, -18, s->avctx->coded_height + 1);
271
        uvsrc_x = av_clip(uvsrc_x,  -8, s->avctx->coded_width  >> 1);
272
        uvsrc_y = av_clip(uvsrc_y,  -8, s->avctx->coded_height >> 1);
273
    }
274

    
275
    srcY += src_y * s->linesize + src_x;
276
    srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
277
    srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
278

    
279
    /* for grayscale we should not try to read from unknown area */
280
    if(s->flags & CODEC_FLAG_GRAY) {
281
        srcU = s->edge_emu_buffer + 18 * s->linesize;
282
        srcV = s->edge_emu_buffer + 18 * s->linesize;
283
    }
284

    
285
    if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
286
       || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel*3
287
       || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 16 - s->mspel*3){
288
        uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
289

    
290
        srcY -= s->mspel * (1 + s->linesize);
291
        s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2,
292
                            src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
293
        srcY = s->edge_emu_buffer;
294
        s->dsp.emulated_edge_mc(uvbuf     , srcU, s->uvlinesize, 8+1, 8+1,
295
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
296
        s->dsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
297
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
298
        srcU = uvbuf;
299
        srcV = uvbuf + 16;
300
        /* if we deal with range reduction we need to scale source blocks */
301
        if(v->rangeredfrm) {
302
            int i, j;
303
            uint8_t *src, *src2;
304

    
305
            src = srcY;
306
            for(j = 0; j < 17 + s->mspel*2; j++) {
307
                for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
308
                src += s->linesize;
309
            }
310
            src = srcU; src2 = srcV;
311
            for(j = 0; j < 9; j++) {
312
                for(i = 0; i < 9; i++) {
313
                    src[i] = ((src[i] - 128) >> 1) + 128;
314
                    src2[i] = ((src2[i] - 128) >> 1) + 128;
315
                }
316
                src += s->uvlinesize;
317
                src2 += s->uvlinesize;
318
            }
319
        }
320
        /* if we deal with intensity compensation we need to scale source blocks */
321
        if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
322
            int i, j;
323
            uint8_t *src, *src2;
324

    
325
            src = srcY;
326
            for(j = 0; j < 17 + s->mspel*2; j++) {
327
                for(i = 0; i < 17 + s->mspel*2; i++) src[i] = v->luty[src[i]];
328
                src += s->linesize;
329
            }
330
            src = srcU; src2 = srcV;
331
            for(j = 0; j < 9; j++) {
332
                for(i = 0; i < 9; i++) {
333
                    src[i] = v->lutuv[src[i]];
334
                    src2[i] = v->lutuv[src2[i]];
335
                }
336
                src += s->uvlinesize;
337
                src2 += s->uvlinesize;
338
            }
339
        }
340
        srcY += s->mspel * (1 + s->linesize);
341
    }
342

    
343
    if(s->mspel) {
344
        dxy = ((my & 3) << 2) | (mx & 3);
345
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0]    , srcY    , s->linesize, v->rnd);
346
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
347
        srcY += s->linesize * 8;
348
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize    , srcY    , s->linesize, v->rnd);
349
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
350
    } else { // hpel mc - always used for luma
351
        dxy = (my & 2) | ((mx & 2) >> 1);
352

    
353
        if(!v->rnd)
354
            dsp->put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
355
        else
356
            dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
357
    }
358

    
359
    if(s->flags & CODEC_FLAG_GRAY) return;
360
    /* Chroma MC always uses qpel bilinear */
361
    uvmx = (uvmx&3)<<1;
362
    uvmy = (uvmy&3)<<1;
363
    if(!v->rnd){
364
        dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
365
        dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
366
    }else{
367
        dsp->put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
368
        dsp->put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
369
    }
370
}
371

    
372
/** Do motion compensation for 4-MV macroblock - luminance block
373
 */
374
static void vc1_mc_4mv_luma(VC1Context *v, int n)
375
{
376
    MpegEncContext *s = &v->s;
377
    DSPContext *dsp = &v->s.dsp;
378
    uint8_t *srcY;
379
    int dxy, mx, my, src_x, src_y;
380
    int off;
381

    
382
    if(!v->s.last_picture.data[0])return;
383
    mx = s->mv[0][n][0];
384
    my = s->mv[0][n][1];
385
    srcY = s->last_picture.data[0];
386

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

    
389
    src_x = s->mb_x * 16 + (n&1) * 8 + (mx >> 2);
390
    src_y = s->mb_y * 16 + (n&2) * 4 + (my >> 2);
391

    
392
    if(v->profile != PROFILE_ADVANCED){
393
        src_x   = av_clip(  src_x, -16, s->mb_width  * 16);
394
        src_y   = av_clip(  src_y, -16, s->mb_height * 16);
395
    }else{
396
        src_x   = av_clip(  src_x, -17, s->avctx->coded_width);
397
        src_y   = av_clip(  src_y, -18, s->avctx->coded_height + 1);
398
    }
399

    
400
    srcY += src_y * s->linesize + src_x;
401

    
402
    if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
403
       || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 8 - s->mspel*2
404
       || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 8 - s->mspel*2){
405
        srcY -= s->mspel * (1 + s->linesize);
406
        s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 9+s->mspel*2, 9+s->mspel*2,
407
                            src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
408
        srcY = s->edge_emu_buffer;
409
        /* if we deal with range reduction we need to scale source blocks */
410
        if(v->rangeredfrm) {
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] = ((src[i] - 128) >> 1) + 128;
417
                src += s->linesize;
418
            }
419
        }
420
        /* if we deal with intensity compensation we need to scale source blocks */
421
        if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
422
            int i, j;
423
            uint8_t *src;
424

    
425
            src = srcY;
426
            for(j = 0; j < 9 + s->mspel*2; j++) {
427
                for(i = 0; i < 9 + s->mspel*2; i++) src[i] = v->luty[src[i]];
428
                src += s->linesize;
429
            }
430
        }
431
        srcY += s->mspel * (1 + s->linesize);
432
    }
433

    
434
    if(s->mspel) {
435
        dxy = ((my & 3) << 2) | (mx & 3);
436
        dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, v->rnd);
437
    } else { // hpel mc - always used for luma
438
        dxy = (my & 2) | ((mx & 2) >> 1);
439
        if(!v->rnd)
440
            dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
441
        else
442
            dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
443
    }
444
}
445

    
446
static inline int median4(int a, int b, int c, int d)
447
{
448
    if(a < b) {
449
        if(c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
450
        else      return (FFMIN(b, c) + FFMAX(a, d)) / 2;
451
    } else {
452
        if(c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
453
        else      return (FFMIN(a, c) + FFMAX(b, d)) / 2;
454
    }
455
}
456

    
457

    
458
/** Do motion compensation for 4-MV macroblock - both chroma blocks
459
 */
460
static void vc1_mc_4mv_chroma(VC1Context *v)
461
{
462
    MpegEncContext *s = &v->s;
463
    DSPContext *dsp = &v->s.dsp;
464
    uint8_t *srcU, *srcV;
465
    int uvmx, uvmy, uvsrc_x, uvsrc_y;
466
    int i, idx, tx = 0, ty = 0;
467
    int mvx[4], mvy[4], intra[4];
468
    static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
469

    
470
    if(!v->s.last_picture.data[0])return;
471
    if(s->flags & CODEC_FLAG_GRAY) return;
472

    
473
    for(i = 0; i < 4; i++) {
474
        mvx[i] = s->mv[0][i][0];
475
        mvy[i] = s->mv[0][i][1];
476
        intra[i] = v->mb_type[0][s->block_index[i]];
477
    }
478

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

    
515
    s->current_picture.motion_val[1][s->block_index[0]][0] = tx;
516
    s->current_picture.motion_val[1][s->block_index[0]][1] = ty;
517
    uvmx = (tx + ((tx&3) == 3)) >> 1;
518
    uvmy = (ty + ((ty&3) == 3)) >> 1;
519
    if(v->fastuvmc) {
520
        uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
521
        uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
522
    }
523

    
524
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
525
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
526

    
527
    if(v->profile != PROFILE_ADVANCED){
528
        uvsrc_x = av_clip(uvsrc_x,  -8, s->mb_width  *  8);
529
        uvsrc_y = av_clip(uvsrc_y,  -8, s->mb_height *  8);
530
    }else{
531
        uvsrc_x = av_clip(uvsrc_x,  -8, s->avctx->coded_width  >> 1);
532
        uvsrc_y = av_clip(uvsrc_y,  -8, s->avctx->coded_height >> 1);
533
    }
534

    
535
    srcU = s->last_picture.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
536
    srcV = s->last_picture.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
537
    if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
538
       || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
539
       || (unsigned)uvsrc_y > (s->v_edge_pos >> 1) - 9){
540
        s->dsp.emulated_edge_mc(s->edge_emu_buffer     , srcU, s->uvlinesize, 8+1, 8+1,
541
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
542
        s->dsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize, 8+1, 8+1,
543
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
544
        srcU = s->edge_emu_buffer;
545
        srcV = s->edge_emu_buffer + 16;
546

    
547
        /* if we deal with range reduction we need to scale source blocks */
548
        if(v->rangeredfrm) {
549
            int i, j;
550
            uint8_t *src, *src2;
551

    
552
            src = srcU; src2 = srcV;
553
            for(j = 0; j < 9; j++) {
554
                for(i = 0; i < 9; i++) {
555
                    src[i] = ((src[i] - 128) >> 1) + 128;
556
                    src2[i] = ((src2[i] - 128) >> 1) + 128;
557
                }
558
                src += s->uvlinesize;
559
                src2 += s->uvlinesize;
560
            }
561
        }
562
        /* if we deal with intensity compensation we need to scale source blocks */
563
        if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
564
            int i, j;
565
            uint8_t *src, *src2;
566

    
567
            src = srcU; src2 = srcV;
568
            for(j = 0; j < 9; j++) {
569
                for(i = 0; i < 9; i++) {
570
                    src[i] = v->lutuv[src[i]];
571
                    src2[i] = v->lutuv[src2[i]];
572
                }
573
                src += s->uvlinesize;
574
                src2 += s->uvlinesize;
575
            }
576
        }
577
    }
578

    
579
    /* Chroma MC always uses qpel bilinear */
580
    uvmx = (uvmx&3)<<1;
581
    uvmy = (uvmy&3)<<1;
582
    if(!v->rnd){
583
        dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
584
        dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
585
    }else{
586
        dsp->put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
587
        dsp->put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
588
    }
589
}
590

    
591
/***********************************************************************/
592
/**
593
 * @defgroup vc1block VC-1 Block-level functions
594
 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
595
 * @{
596
 */
597

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

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

    
685
/** Predict and set motion vector
686
 */
687
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)
688
{
689
    int xy, wrap, off = 0;
690
    int16_t *A, *B, *C;
691
    int px, py;
692
    int sum;
693

    
694
    /* scale MV difference to be quad-pel */
695
    dmv_x <<= 1 - s->quarter_sample;
696
    dmv_y <<= 1 - s->quarter_sample;
697

    
698
    wrap = s->b8_stride;
699
    xy = s->block_index[n];
700

    
701
    if(s->mb_intra){
702
        s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
703
        s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
704
        s->current_picture.motion_val[1][xy][0] = 0;
705
        s->current_picture.motion_val[1][xy][1] = 0;
706
        if(mv1) { /* duplicate motion data for 1-MV block */
707
            s->current_picture.motion_val[0][xy + 1][0] = 0;
708
            s->current_picture.motion_val[0][xy + 1][1] = 0;
709
            s->current_picture.motion_val[0][xy + wrap][0] = 0;
710
            s->current_picture.motion_val[0][xy + wrap][1] = 0;
711
            s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
712
            s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
713
            s->current_picture.motion_val[1][xy + 1][0] = 0;
714
            s->current_picture.motion_val[1][xy + 1][1] = 0;
715
            s->current_picture.motion_val[1][xy + wrap][0] = 0;
716
            s->current_picture.motion_val[1][xy + wrap][1] = 0;
717
            s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
718
            s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
719
        }
720
        return;
721
    }
722

    
723
    C = s->current_picture.motion_val[0][xy - 1];
724
    A = s->current_picture.motion_val[0][xy - wrap];
725
    if(mv1)
726
        off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
727
    else {
728
        //in 4-MV mode different blocks have different B predictor position
729
        switch(n){
730
        case 0:
731
            off = (s->mb_x > 0) ? -1 : 1;
732
            break;
733
        case 1:
734
            off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
735
            break;
736
        case 2:
737
            off = 1;
738
            break;
739
        case 3:
740
            off = -1;
741
        }
742
    }
743
    B = s->current_picture.motion_val[0][xy - wrap + off];
744

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

    
819
/** Motion compensation for direct or interpolated blocks in B-frames
820
 */
821
static void vc1_interp_mc(VC1Context *v)
822
{
823
    MpegEncContext *s = &v->s;
824
    DSPContext *dsp = &v->s.dsp;
825
    uint8_t *srcY, *srcU, *srcV;
826
    int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
827

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

    
830
    mx = s->mv[1][0][0];
831
    my = s->mv[1][0][1];
832
    uvmx = (mx + ((mx & 3) == 3)) >> 1;
833
    uvmy = (my + ((my & 3) == 3)) >> 1;
834
    if(v->fastuvmc) {
835
        uvmx = uvmx + ((uvmx<0)?-(uvmx&1):(uvmx&1));
836
        uvmy = uvmy + ((uvmy<0)?-(uvmy&1):(uvmy&1));
837
    }
838
    srcY = s->next_picture.data[0];
839
    srcU = s->next_picture.data[1];
840
    srcV = s->next_picture.data[2];
841

    
842
    src_x = s->mb_x * 16 + (mx >> 2);
843
    src_y = s->mb_y * 16 + (my >> 2);
844
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
845
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
846

    
847
    if(v->profile != PROFILE_ADVANCED){
848
        src_x   = av_clip(  src_x, -16, s->mb_width  * 16);
849
        src_y   = av_clip(  src_y, -16, s->mb_height * 16);
850
        uvsrc_x = av_clip(uvsrc_x,  -8, s->mb_width  *  8);
851
        uvsrc_y = av_clip(uvsrc_y,  -8, s->mb_height *  8);
852
    }else{
853
        src_x   = av_clip(  src_x, -17, s->avctx->coded_width);
854
        src_y   = av_clip(  src_y, -18, s->avctx->coded_height + 1);
855
        uvsrc_x = av_clip(uvsrc_x,  -8, s->avctx->coded_width  >> 1);
856
        uvsrc_y = av_clip(uvsrc_y,  -8, s->avctx->coded_height >> 1);
857
    }
858

    
859
    srcY += src_y * s->linesize + src_x;
860
    srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
861
    srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
862

    
863
    /* for grayscale we should not try to read from unknown area */
864
    if(s->flags & CODEC_FLAG_GRAY) {
865
        srcU = s->edge_emu_buffer + 18 * s->linesize;
866
        srcV = s->edge_emu_buffer + 18 * s->linesize;
867
    }
868

    
869
    if(v->rangeredfrm
870
       || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel*3
871
       || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 16 - s->mspel*3){
872
        uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
873

    
874
        srcY -= s->mspel * (1 + s->linesize);
875
        s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2,
876
                            src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
877
        srcY = s->edge_emu_buffer;
878
        s->dsp.emulated_edge_mc(uvbuf     , srcU, s->uvlinesize, 8+1, 8+1,
879
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
880
        s->dsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
881
                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
882
        srcU = uvbuf;
883
        srcV = uvbuf + 16;
884
        /* if we deal with range reduction we need to scale source blocks */
885
        if(v->rangeredfrm) {
886
            int i, j;
887
            uint8_t *src, *src2;
888

    
889
            src = srcY;
890
            for(j = 0; j < 17 + s->mspel*2; j++) {
891
                for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
892
                src += s->linesize;
893
            }
894
            src = srcU; src2 = srcV;
895
            for(j = 0; j < 9; j++) {
896
                for(i = 0; i < 9; i++) {
897
                    src[i] = ((src[i] - 128) >> 1) + 128;
898
                    src2[i] = ((src2[i] - 128) >> 1) + 128;
899
                }
900
                src += s->uvlinesize;
901
                src2 += s->uvlinesize;
902
            }
903
        }
904
        srcY += s->mspel * (1 + s->linesize);
905
    }
906

    
907
    if(s->mspel) {
908
        dxy = ((my & 3) << 2) | (mx & 3);
909
        dsp->avg_vc1_mspel_pixels_tab[dxy](s->dest[0]    , srcY    , s->linesize, v->rnd);
910
        dsp->avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
911
        srcY += s->linesize * 8;
912
        dsp->avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize    , srcY    , s->linesize, v->rnd);
913
        dsp->avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
914
    } else { // hpel mc
915
        dxy = (my & 2) | ((mx & 2) >> 1);
916

    
917
        if(!v->rnd)
918
            dsp->avg_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
919
        else
920
            dsp->avg_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
921
    }
922

    
923
    if(s->flags & CODEC_FLAG_GRAY) return;
924
    /* Chroma MC always uses qpel blilinear */
925
    uvmx = (uvmx&3)<<1;
926
    uvmy = (uvmy&3)<<1;
927
    if(!v->rnd){
928
        dsp->avg_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
929
        dsp->avg_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
930
    }else{
931
        dsp->avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
932
        dsp->avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
933
    }
934
}
935

    
936
static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
937
{
938
    int n = bfrac;
939

    
940
#if B_FRACTION_DEN==256
941
    if(inv)
942
        n -= 256;
943
    if(!qs)
944
        return 2 * ((value * n + 255) >> 9);
945
    return (value * n + 128) >> 8;
946
#else
947
    if(inv)
948
        n -= B_FRACTION_DEN;
949
    if(!qs)
950
        return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
951
    return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
952
#endif
953
}
954

    
955
/** Reconstruct motion vector for B-frame and do motion compensation
956
 */
957
static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mode)
958
{
959
    if(v->use_ic) {
960
        v->mv_mode2 = v->mv_mode;
961
        v->mv_mode = MV_PMODE_INTENSITY_COMP;
962
    }
963
    if(direct) {
964
        vc1_mc_1mv(v, 0);
965
        vc1_interp_mc(v);
966
        if(v->use_ic) v->mv_mode = v->mv_mode2;
967
        return;
968
    }
969
    if(mode == BMV_TYPE_INTERPOLATED) {
970
        vc1_mc_1mv(v, 0);
971
        vc1_interp_mc(v);
972
        if(v->use_ic) v->mv_mode = v->mv_mode2;
973
        return;
974
    }
975

    
976
    if(v->use_ic && (mode == BMV_TYPE_BACKWARD)) v->mv_mode = v->mv_mode2;
977
    vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
978
    if(v->use_ic) v->mv_mode = v->mv_mode2;
979
}
980

    
981
static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mvtype)
982
{
983
    MpegEncContext *s = &v->s;
984
    int xy, wrap, off = 0;
985
    int16_t *A, *B, *C;
986
    int px, py;
987
    int sum;
988
    int r_x, r_y;
989
    const uint8_t *is_intra = v->mb_type[0];
990

    
991
    r_x = v->range_x;
992
    r_y = v->range_y;
993
    /* scale MV difference to be quad-pel */
994
    dmv_x[0] <<= 1 - s->quarter_sample;
995
    dmv_y[0] <<= 1 - s->quarter_sample;
996
    dmv_x[1] <<= 1 - s->quarter_sample;
997
    dmv_y[1] <<= 1 - s->quarter_sample;
998

    
999
    wrap = s->b8_stride;
1000
    xy = s->block_index[0];
1001

    
1002
    if(s->mb_intra) {
1003
        s->current_picture.motion_val[0][xy][0] =
1004
        s->current_picture.motion_val[0][xy][1] =
1005
        s->current_picture.motion_val[1][xy][0] =
1006
        s->current_picture.motion_val[1][xy][1] = 0;
1007
        return;
1008
    }
1009
    s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
1010
    s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
1011
    s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
1012
    s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
1013

    
1014
    /* Pullback predicted motion vectors as specified in 8.4.5.4 */
1015
    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));
1016
    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));
1017
    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));
1018
    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));
1019
    if(direct) {
1020
        s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
1021
        s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
1022
        s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
1023
        s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
1024
        return;
1025
    }
1026

    
1027
    if((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
1028
        C = s->current_picture.motion_val[0][xy - 2];
1029
        A = s->current_picture.motion_val[0][xy - wrap*2];
1030
        off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1031
        B = s->current_picture.motion_val[0][xy - wrap*2 + off];
1032

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

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

    
1181
        s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
1182
        s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
1183
    }
1184
    s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
1185
    s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
1186
    s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
1187
    s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
1188
}
1189

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

    
1211
    /* find prediction - wmv3_dc_scale always used here in fact */
1212
    if (n < 4)     scale = s->y_dc_scale;
1213
    else           scale = s->c_dc_scale;
1214

    
1215
    wrap = s->block_wrap[n];
1216
    dc_val= s->dc_val[0] + s->block_index[n];
1217

    
1218
    /* B A
1219
     * C X
1220
     */
1221
    c = dc_val[ - 1];
1222
    b = dc_val[ - 1 - wrap];
1223
    a = dc_val[ - wrap];
1224

    
1225
    if (pq < 9 || !overlap)
1226
    {
1227
        /* Set outer values */
1228
        if (s->first_slice_line && (n!=2 && n!=3)) b=a=dcpred[scale];
1229
        if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=dcpred[scale];
1230
    }
1231
    else
1232
    {
1233
        /* Set outer values */
1234
        if (s->first_slice_line && (n!=2 && n!=3)) b=a=0;
1235
        if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=0;
1236
    }
1237

    
1238
    if (abs(a - b) <= abs(b - c)) {
1239
        pred = c;
1240
        *dir_ptr = 1;//left
1241
    } else {
1242
        pred = a;
1243
        *dir_ptr = 0;//top
1244
    }
1245

    
1246
    /* update predictor */
1247
    *dc_val_ptr = &dc_val[0];
1248
    return pred;
1249
}
1250

    
1251

    
1252
/** Get predicted DC value
1253
 * prediction dir: left=0, top=1
1254
 * @param s MpegEncContext
1255
 * @param overlap flag indicating that overlap filtering is used
1256
 * @param pq integer part of picture quantizer
1257
 * @param[in] n block index in the current MB
1258
 * @param a_avail flag indicating top block availability
1259
 * @param c_avail flag indicating left block availability
1260
 * @param dc_val_ptr Pointer to DC predictor
1261
 * @param dir_ptr Prediction direction for use in AC prediction
1262
 */
1263
static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
1264
                              int a_avail, int c_avail,
1265
                              int16_t **dc_val_ptr, int *dir_ptr)
1266
{
1267
    int a, b, c, wrap, pred;
1268
    int16_t *dc_val;
1269
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1270
    int q1, q2 = 0;
1271

    
1272
    wrap = s->block_wrap[n];
1273
    dc_val= s->dc_val[0] + s->block_index[n];
1274

    
1275
    /* B A
1276
     * C X
1277
     */
1278
    c = dc_val[ - 1];
1279
    b = dc_val[ - 1 - wrap];
1280
    a = dc_val[ - wrap];
1281
    /* scale predictors if needed */
1282
    q1 = s->current_picture.qscale_table[mb_pos];
1283
    if(c_avail && (n!= 1 && n!=3)) {
1284
        q2 = s->current_picture.qscale_table[mb_pos - 1];
1285
        if(q2 && q2 != q1)
1286
            c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
1287
    }
1288
    if(a_avail && (n!= 2 && n!=3)) {
1289
        q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
1290
        if(q2 && q2 != q1)
1291
            a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
1292
    }
1293
    if(a_avail && c_avail && (n!=3)) {
1294
        int off = mb_pos;
1295
        if(n != 1) off--;
1296
        if(n != 2) off -= s->mb_stride;
1297
        q2 = s->current_picture.qscale_table[off];
1298
        if(q2 && q2 != q1)
1299
            b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
1300
    }
1301

    
1302
    if(a_avail && c_avail) {
1303
        if(abs(a - b) <= abs(b - c)) {
1304
            pred = c;
1305
            *dir_ptr = 1;//left
1306
        } else {
1307
            pred = a;
1308
            *dir_ptr = 0;//top
1309
        }
1310
    } else if(a_avail) {
1311
        pred = a;
1312
        *dir_ptr = 0;//top
1313
    } else if(c_avail) {
1314
        pred = c;
1315
        *dir_ptr = 1;//left
1316
    } else {
1317
        pred = 0;
1318
        *dir_ptr = 1;//left
1319
    }
1320

    
1321
    /* update predictor */
1322
    *dc_val_ptr = &dc_val[0];
1323
    return pred;
1324
}
1325

    
1326
/** @} */ // Block group
1327

    
1328
/**
1329
 * @defgroup vc1_std_mb VC1 Macroblock-level functions in Simple/Main Profiles
1330
 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1331
 * @{
1332
 */
1333

    
1334
static inline int vc1_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
1335
{
1336
    int xy, wrap, pred, a, b, c;
1337

    
1338
    xy = s->block_index[n];
1339
    wrap = s->b8_stride;
1340

    
1341
    /* B C
1342
     * A X
1343
     */
1344
    a = s->coded_block[xy - 1       ];
1345
    b = s->coded_block[xy - 1 - wrap];
1346
    c = s->coded_block[xy     - wrap];
1347

    
1348
    if (b == c) {
1349
        pred = a;
1350
    } else {
1351
        pred = c;
1352
    }
1353

    
1354
    /* store value */
1355
    *coded_block_ptr = &s->coded_block[xy];
1356

    
1357
    return pred;
1358
}
1359

    
1360
/**
1361
 * Decode one AC coefficient
1362
 * @param v The VC1 context
1363
 * @param last Last coefficient
1364
 * @param skip How much zero coefficients to skip
1365
 * @param value Decoded AC coefficient value
1366
 * @param codingset set of VLC to decode data
1367
 * @see 8.1.3.4
1368
 */
1369
static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip, int *value, int codingset)
1370
{
1371
    GetBitContext *gb = &v->s.gb;
1372
    int index, escape, run = 0, level = 0, lst = 0;
1373

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

    
1422
    *last = lst;
1423
    *skip = run;
1424
    *value = level;
1425
}
1426

    
1427
/** Decode intra block in intra frames - should be faster than decode_intra_block
1428
 * @param v VC1Context
1429
 * @param block block to decode
1430
 * @param[in] n subblock index
1431
 * @param coded are AC coeffs present or not
1432
 * @param codingset set of VLC to decode data
1433
 */
1434
static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset)
1435
{
1436
    GetBitContext *gb = &v->s.gb;
1437
    MpegEncContext *s = &v->s;
1438
    int dc_pred_dir = 0; /* Direction of the DC prediction used */
1439
    int i;
1440
    int16_t *dc_val;
1441
    int16_t *ac_val, *ac_val2;
1442
    int dcdiff;
1443

    
1444
    /* Get DC differential */
1445
    if (n < 4) {
1446
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1447
    } else {
1448
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1449
    }
1450
    if (dcdiff < 0){
1451
        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
1452
        return -1;
1453
    }
1454
    if (dcdiff)
1455
    {
1456
        if (dcdiff == 119 /* ESC index value */)
1457
        {
1458
            /* TODO: Optimize */
1459
            if (v->pq == 1) dcdiff = get_bits(gb, 10);
1460
            else if (v->pq == 2) dcdiff = get_bits(gb, 9);
1461
            else dcdiff = get_bits(gb, 8);
1462
        }
1463
        else
1464
        {
1465
            if (v->pq == 1)
1466
                dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
1467
            else if (v->pq == 2)
1468
                dcdiff = (dcdiff<<1) + get_bits1(gb)   - 1;
1469
        }
1470
        if (get_bits1(gb))
1471
            dcdiff = -dcdiff;
1472
    }
1473

    
1474
    /* Prediction */
1475
    dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
1476
    *dc_val = dcdiff;
1477

    
1478
    /* Store the quantized DC coeff, used for prediction */
1479
    if (n < 4) {
1480
        block[0] = dcdiff * s->y_dc_scale;
1481
    } else {
1482
        block[0] = dcdiff * s->c_dc_scale;
1483
    }
1484
    /* Skip ? */
1485
    if (!coded) {
1486
        goto not_coded;
1487
    }
1488

    
1489
    //AC Decoding
1490
    i = 1;
1491

    
1492
    {
1493
        int last = 0, skip, value;
1494
        const uint8_t *zz_table;
1495
        int scale;
1496
        int k;
1497

    
1498
        scale = v->pq * 2 + v->halfpq;
1499

    
1500
        if(v->s.ac_pred) {
1501
            if(!dc_pred_dir)
1502
                zz_table = v->zz_8x8[2];
1503
            else
1504
                zz_table = v->zz_8x8[3];
1505
        } else
1506
            zz_table = v->zz_8x8[1];
1507

    
1508
        ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1509
        ac_val2 = ac_val;
1510
        if(dc_pred_dir) //left
1511
            ac_val -= 16;
1512
        else //top
1513
            ac_val -= 16 * s->block_wrap[n];
1514

    
1515
        while (!last) {
1516
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1517
            i += skip;
1518
            if(i > 63)
1519
                break;
1520
            block[zz_table[i++]] = value;
1521
        }
1522

    
1523
        /* apply AC prediction if needed */
1524
        if(s->ac_pred) {
1525
            if(dc_pred_dir) { //left
1526
                for(k = 1; k < 8; k++)
1527
                    block[k] += ac_val[k];
1528
            } else { //top
1529
                for(k = 1; k < 8; k++)
1530
                    block[k << 3] += ac_val[k + 8];
1531
            }
1532
        }
1533
        /* save AC coeffs for further prediction */
1534
        for(k = 1; k < 8; k++) {
1535
            ac_val2[k] = block[k];
1536
            ac_val2[k + 8] = block[k << 3];
1537
        }
1538

    
1539
        /* scale AC coeffs */
1540
        for(k = 1; k < 64; k++)
1541
            if(block[k]) {
1542
                block[k] *= scale;
1543
                if(!v->pquantizer)
1544
                    block[k] += (block[k] < 0) ? -v->pq : v->pq;
1545
            }
1546

    
1547
        if(s->ac_pred) i = 63;
1548
    }
1549

    
1550
not_coded:
1551
    if(!coded) {
1552
        int k, scale;
1553
        ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1554
        ac_val2 = ac_val;
1555

    
1556
        i = 0;
1557
        scale = v->pq * 2 + v->halfpq;
1558
        memset(ac_val2, 0, 16 * 2);
1559
        if(dc_pred_dir) {//left
1560
            ac_val -= 16;
1561
            if(s->ac_pred)
1562
                memcpy(ac_val2, ac_val, 8 * 2);
1563
        } else {//top
1564
            ac_val -= 16 * s->block_wrap[n];
1565
            if(s->ac_pred)
1566
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1567
        }
1568

    
1569
        /* apply AC prediction if needed */
1570
        if(s->ac_pred) {
1571
            if(dc_pred_dir) { //left
1572
                for(k = 1; k < 8; k++) {
1573
                    block[k] = ac_val[k] * scale;
1574
                    if(!v->pquantizer && block[k])
1575
                        block[k] += (block[k] < 0) ? -v->pq : v->pq;
1576
                }
1577
            } else { //top
1578
                for(k = 1; k < 8; k++) {
1579
                    block[k << 3] = ac_val[k + 8] * scale;
1580
                    if(!v->pquantizer && block[k << 3])
1581
                        block[k << 3] += (block[k << 3] < 0) ? -v->pq : v->pq;
1582
                }
1583
            }
1584
            i = 63;
1585
        }
1586
    }
1587
    s->block_last_index[n] = i;
1588

    
1589
    return 0;
1590
}
1591

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

    
1615
    /* Get DC differential */
1616
    if (n < 4) {
1617
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1618
    } else {
1619
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1620
    }
1621
    if (dcdiff < 0){
1622
        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
1623
        return -1;
1624
    }
1625
    if (dcdiff)
1626
    {
1627
        if (dcdiff == 119 /* ESC index value */)
1628
        {
1629
            /* TODO: Optimize */
1630
            if (mquant == 1) dcdiff = get_bits(gb, 10);
1631
            else if (mquant == 2) dcdiff = get_bits(gb, 9);
1632
            else dcdiff = get_bits(gb, 8);
1633
        }
1634
        else
1635
        {
1636
            if (mquant == 1)
1637
                dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
1638
            else if (mquant == 2)
1639
                dcdiff = (dcdiff<<1) + get_bits1(gb)   - 1;
1640
        }
1641
        if (get_bits1(gb))
1642
            dcdiff = -dcdiff;
1643
    }
1644

    
1645
    /* Prediction */
1646
    dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
1647
    *dc_val = dcdiff;
1648

    
1649
    /* Store the quantized DC coeff, used for prediction */
1650
    if (n < 4) {
1651
        block[0] = dcdiff * s->y_dc_scale;
1652
    } else {
1653
        block[0] = dcdiff * s->c_dc_scale;
1654
    }
1655

    
1656
    //AC Decoding
1657
    i = 1;
1658

    
1659
    /* check if AC is needed at all */
1660
    if(!a_avail && !c_avail) use_pred = 0;
1661
    ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1662
    ac_val2 = ac_val;
1663

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

    
1666
    if(dc_pred_dir) //left
1667
        ac_val -= 16;
1668
    else //top
1669
        ac_val -= 16 * s->block_wrap[n];
1670

    
1671
    q1 = s->current_picture.qscale_table[mb_pos];
1672
    if(dc_pred_dir && c_avail && mb_pos) q2 = s->current_picture.qscale_table[mb_pos - 1];
1673
    if(!dc_pred_dir && a_avail && mb_pos >= s->mb_stride) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
1674
    if(dc_pred_dir && n==1) q2 = q1;
1675
    if(!dc_pred_dir && n==2) q2 = q1;
1676
    if(n==3) q2 = q1;
1677

    
1678
    if(coded) {
1679
        int last = 0, skip, value;
1680
        const uint8_t *zz_table;
1681
        int k;
1682

    
1683
        if(v->s.ac_pred) {
1684
            if(!dc_pred_dir)
1685
                zz_table = v->zz_8x8[2];
1686
            else
1687
                zz_table = v->zz_8x8[3];
1688
        } else
1689
            zz_table = v->zz_8x8[1];
1690

    
1691
        while (!last) {
1692
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1693
            i += skip;
1694
            if(i > 63)
1695
                break;
1696
            block[zz_table[i++]] = value;
1697
        }
1698

    
1699
        /* apply AC prediction if needed */
1700
        if(use_pred) {
1701
            /* scale predictors if needed*/
1702
            if(q2 && q1!=q2) {
1703
                q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1704
                q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1705

    
1706
                if(dc_pred_dir) { //left
1707
                    for(k = 1; k < 8; k++)
1708
                        block[k] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1709
                } else { //top
1710
                    for(k = 1; k < 8; k++)
1711
                        block[k << 3] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1712
                }
1713
            } else {
1714
                if(dc_pred_dir) { //left
1715
                    for(k = 1; k < 8; k++)
1716
                        block[k] += ac_val[k];
1717
                } else { //top
1718
                    for(k = 1; k < 8; k++)
1719
                        block[k << 3] += ac_val[k + 8];
1720
                }
1721
            }
1722
        }
1723
        /* save AC coeffs for further prediction */
1724
        for(k = 1; k < 8; k++) {
1725
            ac_val2[k] = block[k];
1726
            ac_val2[k + 8] = block[k << 3];
1727
        }
1728

    
1729
        /* scale AC coeffs */
1730
        for(k = 1; k < 64; k++)
1731
            if(block[k]) {
1732
                block[k] *= scale;
1733
                if(!v->pquantizer)
1734
                    block[k] += (block[k] < 0) ? -mquant : mquant;
1735
            }
1736

    
1737
        if(use_pred) i = 63;
1738
    } else { // no AC coeffs
1739
        int k;
1740

    
1741
        memset(ac_val2, 0, 16 * 2);
1742
        if(dc_pred_dir) {//left
1743
            if(use_pred) {
1744
                memcpy(ac_val2, ac_val, 8 * 2);
1745
                if(q2 && q1!=q2) {
1746
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1747
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1748
                    for(k = 1; k < 8; k++)
1749
                        ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1750
                }
1751
            }
1752
        } else {//top
1753
            if(use_pred) {
1754
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1755
                if(q2 && q1!=q2) {
1756
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1757
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1758
                    for(k = 1; k < 8; k++)
1759
                        ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1760
                }
1761
            }
1762
        }
1763

    
1764
        /* apply AC prediction if needed */
1765
        if(use_pred) {
1766
            if(dc_pred_dir) { //left
1767
                for(k = 1; k < 8; k++) {
1768
                    block[k] = ac_val2[k] * scale;
1769
                    if(!v->pquantizer && block[k])
1770
                        block[k] += (block[k] < 0) ? -mquant : mquant;
1771
                }
1772
            } else { //top
1773
                for(k = 1; k < 8; k++) {
1774
                    block[k << 3] = ac_val2[k + 8] * scale;
1775
                    if(!v->pquantizer && block[k << 3])
1776
                        block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
1777
                }
1778
            }
1779
            i = 63;
1780
        }
1781
    }
1782
    s->block_last_index[n] = i;
1783

    
1784
    return 0;
1785
}
1786

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

    
1810
    s->dsp.clear_block(block);
1811

    
1812
    /* XXX: Guard against dumb values of mquant */
1813
    mquant = (mquant < 1) ? 0 : ( (mquant>31) ? 31 : mquant );
1814

    
1815
    /* Set DC scale - y and c use the same */
1816
    s->y_dc_scale = s->y_dc_scale_table[mquant];
1817
    s->c_dc_scale = s->c_dc_scale_table[mquant];
1818

    
1819
    /* Get DC differential */
1820
    if (n < 4) {
1821
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1822
    } else {
1823
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1824
    }
1825
    if (dcdiff < 0){
1826
        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
1827
        return -1;
1828
    }
1829
    if (dcdiff)
1830
    {
1831
        if (dcdiff == 119 /* ESC index value */)
1832
        {
1833
            /* TODO: Optimize */
1834
            if (mquant == 1) dcdiff = get_bits(gb, 10);
1835
            else if (mquant == 2) dcdiff = get_bits(gb, 9);
1836
            else dcdiff = get_bits(gb, 8);
1837
        }
1838
        else
1839
        {
1840
            if (mquant == 1)
1841
                dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
1842
            else if (mquant == 2)
1843
                dcdiff = (dcdiff<<1) + get_bits1(gb)   - 1;
1844
        }
1845
        if (get_bits1(gb))
1846
            dcdiff = -dcdiff;
1847
    }
1848

    
1849
    /* Prediction */
1850
    dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
1851
    *dc_val = dcdiff;
1852

    
1853
    /* Store the quantized DC coeff, used for prediction */
1854

    
1855
    if (n < 4) {
1856
        block[0] = dcdiff * s->y_dc_scale;
1857
    } else {
1858
        block[0] = dcdiff * s->c_dc_scale;
1859
    }
1860

    
1861
    //AC Decoding
1862
    i = 1;
1863

    
1864
    /* check if AC is needed at all and adjust direction if needed */
1865
    if(!a_avail) dc_pred_dir = 1;
1866
    if(!c_avail) dc_pred_dir = 0;
1867
    if(!a_avail && !c_avail) use_pred = 0;
1868
    ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1869
    ac_val2 = ac_val;
1870

    
1871
    scale = mquant * 2 + v->halfpq;
1872

    
1873
    if(dc_pred_dir) //left
1874
        ac_val -= 16;
1875
    else //top
1876
        ac_val -= 16 * s->block_wrap[n];
1877

    
1878
    q1 = s->current_picture.qscale_table[mb_pos];
1879
    if(dc_pred_dir && c_avail && mb_pos) q2 = s->current_picture.qscale_table[mb_pos - 1];
1880
    if(!dc_pred_dir && a_avail && mb_pos >= s->mb_stride) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
1881
    if(dc_pred_dir && n==1) q2 = q1;
1882
    if(!dc_pred_dir && n==2) q2 = q1;
1883
    if(n==3) q2 = q1;
1884

    
1885
    if(coded) {
1886
        int last = 0, skip, value;
1887
        int k;
1888

    
1889
        while (!last) {
1890
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1891
            i += skip;
1892
            if(i > 63)
1893
                break;
1894
            block[v->zz_8x8[0][i++]] = value;
1895
        }
1896

    
1897
        /* apply AC prediction if needed */
1898
        if(use_pred) {
1899
            /* scale predictors if needed*/
1900
            if(q2 && q1!=q2) {
1901
                q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1902
                q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1903

    
1904
                if(dc_pred_dir) { //left
1905
                    for(k = 1; k < 8; k++)
1906
                        block[k] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1907
                } else { //top
1908
                    for(k = 1; k < 8; k++)
1909
                        block[k << 3] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1910
                }
1911
            } else {
1912
                if(dc_pred_dir) { //left
1913
                    for(k = 1; k < 8; k++)
1914
                        block[k] += ac_val[k];
1915
                } else { //top
1916
                    for(k = 1; k < 8; k++)
1917
                        block[k << 3] += ac_val[k + 8];
1918
                }
1919
            }
1920
        }
1921
        /* save AC coeffs for further prediction */
1922
        for(k = 1; k < 8; k++) {
1923
            ac_val2[k] = block[k];
1924
            ac_val2[k + 8] = block[k << 3];
1925
        }
1926

    
1927
        /* scale AC coeffs */
1928
        for(k = 1; k < 64; k++)
1929
            if(block[k]) {
1930
                block[k] *= scale;
1931
                if(!v->pquantizer)
1932
                    block[k] += (block[k] < 0) ? -mquant : mquant;
1933
            }
1934

    
1935
        if(use_pred) i = 63;
1936
    } else { // no AC coeffs
1937
        int k;
1938

    
1939
        memset(ac_val2, 0, 16 * 2);
1940
        if(dc_pred_dir) {//left
1941
            if(use_pred) {
1942
                memcpy(ac_val2, ac_val, 8 * 2);
1943
                if(q2 && q1!=q2) {
1944
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1945
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1946
                    for(k = 1; k < 8; k++)
1947
                        ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1948
                }
1949
            }
1950
        } else {//top
1951
            if(use_pred) {
1952
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1953
                if(q2 && q1!=q2) {
1954
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1955
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1956
                    for(k = 1; k < 8; k++)
1957
                        ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1958
                }
1959
            }
1960
        }
1961

    
1962
        /* apply AC prediction if needed */
1963
        if(use_pred) {
1964
            if(dc_pred_dir) { //left
1965
                for(k = 1; k < 8; k++) {
1966
                    block[k] = ac_val2[k] * scale;
1967
                    if(!v->pquantizer && block[k])
1968
                        block[k] += (block[k] < 0) ? -mquant : mquant;
1969
                }
1970
            } else { //top
1971
                for(k = 1; k < 8; k++) {
1972
                    block[k << 3] = ac_val2[k + 8] * scale;
1973
                    if(!v->pquantizer && block[k << 3])
1974
                        block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
1975
                }
1976
            }
1977
            i = 63;
1978
        }
1979
    }
1980
    s->block_last_index[n] = i;
1981

    
1982
    return 0;
1983
}
1984

    
1985
/** Decode P block
1986
 */
1987
static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n, int mquant, int ttmb, int first_block,
1988
                              uint8_t *dst, int linesize, int skip_block, int apply_filter, int cbp_top, int cbp_left)
1989
{
1990
    MpegEncContext *s = &v->s;
1991
    GetBitContext *gb = &s->gb;
1992
    int i, j;
1993
    int subblkpat = 0;
1994
    int scale, off, idx, last, skip, value;
1995
    int ttblk = ttmb & 7;
1996
    int pat = 0;
1997

    
1998
    s->dsp.clear_block(block);
1999

    
2000
    if(ttmb == -1) {
2001
        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)];
2002
    }
2003
    if(ttblk == TT_4X4) {
2004
        subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
2005
    }
2006
    if((ttblk != TT_8X8 && ttblk != TT_4X4)
2007
        && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
2008
            || (!v->res_rtm_flag && !first_block))) {
2009
        subblkpat = decode012(gb);
2010
        if(subblkpat) subblkpat ^= 3; //swap decoded pattern bits
2011
        if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) ttblk = TT_8X4;
2012
        if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) ttblk = TT_4X8;
2013
    }
2014
    scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
2015

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

    
2141
/** @} */ // Macroblock group
2142

    
2143
static const int size_table  [6] = { 0, 2, 3, 4,  5,  8 };
2144
static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
2145

    
2146
/** Decode one P-frame MB (in Simple/Main profile)
2147
 */
2148
static int vc1_decode_p_mb(VC1Context *v)
2149
{
2150
    MpegEncContext *s = &v->s;
2151
    GetBitContext *gb = &s->gb;
2152
    int i, j;
2153
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2154
    int cbp; /* cbp decoding stuff */
2155
    int mqdiff, mquant; /* MB quantization */
2156
    int ttmb = v->ttfrm; /* MB Transform type */
2157

    
2158
    int mb_has_coeffs = 1; /* last_flag */
2159
    int dmv_x, dmv_y; /* Differential MV components */
2160
    int index, index1; /* LUT indexes */
2161
    int val, sign; /* temp values */
2162
    int first_block = 1;
2163
    int dst_idx, off;
2164
    int skipped, fourmv;
2165
    int block_cbp = 0, pat;
2166
    int apply_loop_filter;
2167

    
2168
    mquant = v->pq; /* Loosy initialization */
2169

    
2170
    if (v->mv_type_is_raw)
2171
        fourmv = get_bits1(gb);
2172
    else
2173
        fourmv = v->mv_type_mb_plane[mb_pos];
2174
    if (v->skip_is_raw)
2175
        skipped = get_bits1(gb);
2176
    else
2177
        skipped = v->s.mbskip_table[mb_pos];
2178

    
2179
    apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY);
2180
    if (!fourmv) /* 1MV mode */
2181
    {
2182
        if (!skipped)
2183
        {
2184
            GET_MVDATA(dmv_x, dmv_y);
2185

    
2186
            if (s->mb_intra) {
2187
                s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2188
                s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2189
            }
2190
            s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
2191
            vc1_pred_mv(s, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
2192

    
2193
            /* FIXME Set DC val for inter block ? */
2194
            if (s->mb_intra && !mb_has_coeffs)
2195
            {
2196
                GET_MQUANT();
2197
                s->ac_pred = get_bits1(gb);
2198
                cbp = 0;
2199
            }
2200
            else if (mb_has_coeffs)
2201
            {
2202
                if (s->mb_intra) s->ac_pred = get_bits1(gb);
2203
                cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2204
                GET_MQUANT();
2205
            }
2206
            else
2207
            {
2208
                mquant = v->pq;
2209
                cbp = 0;
2210
            }
2211
            s->current_picture.qscale_table[mb_pos] = mquant;
2212

    
2213
            if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
2214
                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
2215
                                VC1_TTMB_VLC_BITS, 2);
2216
            if(!s->mb_intra) vc1_mc_1mv(v, 0);
2217
            dst_idx = 0;
2218
            for (i=0; i<6; i++)
2219
            {
2220
                s->dc_val[0][s->block_index[i]] = 0;
2221
                dst_idx += i >> 2;
2222
                val = ((cbp >> (5 - i)) & 1);
2223
                off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2224
                v->mb_type[0][s->block_index[i]] = s->mb_intra;
2225
                if(s->mb_intra) {
2226
                    /* check if prediction blocks A and C are available */
2227
                    v->a_avail = v->c_avail = 0;
2228
                    if(i == 2 || i == 3 || !s->first_slice_line)
2229
                        v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2230
                    if(i == 1 || i == 3 || s->mb_x)
2231
                        v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2232

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

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

    
2434
    /* Should never happen */
2435
    return -1;
2436
}
2437

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

    
2458
    mquant = v->pq; /* Loosy initialization */
2459
    s->mb_intra = 0;
2460

    
2461
    if (v->dmb_is_raw)
2462
        direct = get_bits1(gb);
2463
    else
2464
        direct = v->direct_mb_plane[mb_pos];
2465
    if (v->skip_is_raw)
2466
        skipped = get_bits1(gb);
2467
    else
2468
        skipped = v->s.mbskip_table[mb_pos];
2469

    
2470
    dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
2471
    for(i = 0; i < 6; i++) {
2472
        v->mb_type[0][s->block_index[i]] = 0;
2473
        s->dc_val[0][s->block_index[i]] = 0;
2474
    }
2475
    s->current_picture.qscale_table[mb_pos] = 0;
2476

    
2477
    if (!direct) {
2478
        if (!skipped) {
2479
            GET_MVDATA(dmv_x[0], dmv_y[0]);
2480
            dmv_x[1] = dmv_x[0];
2481
            dmv_y[1] = dmv_y[0];
2482
        }
2483
        if(skipped || !s->mb_intra) {
2484
            bmvtype = decode012(gb);
2485
            switch(bmvtype) {
2486
            case 0:
2487
                bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
2488
                break;
2489
            case 1:
2490
                bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
2491
                break;
2492
            case 2:
2493
                bmvtype = BMV_TYPE_INTERPOLATED;
2494
                dmv_x[0] = dmv_y[0] = 0;
2495
            }
2496
        }
2497
    }
2498
    for(i = 0; i < 6; i++)
2499
        v->mb_type[0][s->block_index[i]] = s->mb_intra;
2500

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

    
2569
            vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
2570
            if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
2571
            s->dsp.vc1_inv_trans_8x8(s->block[i]);
2572
            if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
2573
            s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
2574
        } else if(val) {
2575
            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);
2576
            if(!v->ttmbf && ttmb < 8) ttmb = -1;
2577
            first_block = 0;
2578
        }
2579
    }
2580
}
2581

    
2582
/** Decode blocks of I-frame
2583
 */
2584
static void vc1_decode_i_blocks(VC1Context *v)
2585
{
2586
    int k, j;
2587
    MpegEncContext *s = &v->s;
2588
    int cbp, val;
2589
    uint8_t *coded_val;
2590
    int mb_pos;
2591

    
2592
    /* select codingmode used for VLC tables selection */
2593
    switch(v->y_ac_table_index){
2594
    case 0:
2595
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2596
        break;
2597
    case 1:
2598
        v->codingset = CS_HIGH_MOT_INTRA;
2599
        break;
2600
    case 2:
2601
        v->codingset = CS_MID_RATE_INTRA;
2602
        break;
2603
    }
2604

    
2605
    switch(v->c_ac_table_index){
2606
    case 0:
2607
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2608
        break;
2609
    case 1:
2610
        v->codingset2 = CS_HIGH_MOT_INTER;
2611
        break;
2612
    case 2:
2613
        v->codingset2 = CS_MID_RATE_INTER;
2614
        break;
2615
    }
2616

    
2617
    /* Set DC scale - y and c use the same */
2618
    s->y_dc_scale = s->y_dc_scale_table[v->pq];
2619
    s->c_dc_scale = s->c_dc_scale_table[v->pq];
2620

    
2621
    //do frame decode
2622
    s->mb_x = s->mb_y = 0;
2623
    s->mb_intra = 1;
2624
    s->first_slice_line = 1;
2625
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2626
        s->mb_x = 0;
2627
        ff_init_block_index(s);
2628
        for(; s->mb_x < s->mb_width; s->mb_x++) {
2629
            ff_update_block_index(s);
2630
            s->dsp.clear_blocks(s->block[0]);
2631
            mb_pos = s->mb_x + s->mb_y * s->mb_width;
2632
            s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2633
            s->current_picture.qscale_table[mb_pos] = v->pq;
2634
            s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2635
            s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2636

    
2637
            // do actual MB decoding and displaying
2638
            cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2639
            v->s.ac_pred = get_bits1(&v->s.gb);
2640

    
2641
            for(k = 0; k < 6; k++) {
2642
                val = ((cbp >> (5 - k)) & 1);
2643

    
2644
                if (k < 4) {
2645
                    int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2646
                    val = val ^ pred;
2647
                    *coded_val = val;
2648
                }
2649
                cbp |= val << (5 - k);
2650

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

    
2653
                s->dsp.vc1_inv_trans_8x8(s->block[k]);
2654
                if(v->pq >= 9 && v->overlap) {
2655
                    for(j = 0; j < 64; j++) s->block[k][j] += 128;
2656
                }
2657
            }
2658

    
2659
            vc1_put_block(v, s->block);
2660
            if(v->pq >= 9 && v->overlap) {
2661
                if(s->mb_x) {
2662
                    s->dsp.vc1_h_overlap(s->dest[0], s->linesize);
2663
                    s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2664
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
2665
                        s->dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
2666
                        s->dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
2667
                    }
2668
                }
2669
                s->dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
2670
                s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2671
                if(!s->first_slice_line) {
2672
                    s->dsp.vc1_v_overlap(s->dest[0], s->linesize);
2673
                    s->dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
2674
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
2675
                        s->dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
2676
                        s->dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
2677
                    }
2678
                }
2679
                s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2680
                s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2681
            }
2682
            if(v->s.loop_filter) vc1_loop_filter_iblk(s, v->pq);
2683

    
2684
            if(get_bits_count(&s->gb) > v->bits) {
2685
                ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
2686
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
2687
                return;
2688
            }
2689
        }
2690
        if (!v->s.loop_filter)
2691
            ff_draw_horiz_band(s, s->mb_y * 16, 16);
2692
        else if (s->mb_y)
2693
            ff_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
2694

    
2695
        s->first_slice_line = 0;
2696
    }
2697
    if (v->s.loop_filter)
2698
        ff_draw_horiz_band(s, (s->mb_height-1)*16, 16);
2699
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2700
}
2701

    
2702
/** Decode blocks of I-frame for advanced profile
2703
 */
2704
static void vc1_decode_i_blocks_adv(VC1Context *v)
2705
{
2706
    int k, j;
2707
    MpegEncContext *s = &v->s;
2708
    int cbp, val;
2709
    uint8_t *coded_val;
2710
    int mb_pos;
2711
    int mquant = v->pq;
2712
    int mqdiff;
2713
    int overlap;
2714
    GetBitContext *gb = &s->gb;
2715

    
2716
    /* select codingmode used for VLC tables selection */
2717
    switch(v->y_ac_table_index){
2718
    case 0:
2719
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2720
        break;
2721
    case 1:
2722
        v->codingset = CS_HIGH_MOT_INTRA;
2723
        break;
2724
    case 2:
2725
        v->codingset = CS_MID_RATE_INTRA;
2726
        break;
2727
    }
2728

    
2729
    switch(v->c_ac_table_index){
2730
    case 0:
2731
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2732
        break;
2733
    case 1:
2734
        v->codingset2 = CS_HIGH_MOT_INTER;
2735
        break;
2736
    case 2:
2737
        v->codingset2 = CS_MID_RATE_INTER;
2738
        break;
2739
    }
2740

    
2741
    //do frame decode
2742
    s->mb_x = s->mb_y = 0;
2743
    s->mb_intra = 1;
2744
    s->first_slice_line = 1;
2745
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2746
        s->mb_x = 0;
2747
        ff_init_block_index(s);
2748
        for(;s->mb_x < s->mb_width; s->mb_x++) {
2749
            ff_update_block_index(s);
2750
            s->dsp.clear_blocks(s->block[0]);
2751
            mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2752
            s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2753
            s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2754
            s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2755

    
2756
            // do actual MB decoding and displaying
2757
            cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2758
            if(v->acpred_is_raw)
2759
                v->s.ac_pred = get_bits1(&v->s.gb);
2760
            else
2761
                v->s.ac_pred = v->acpred_plane[mb_pos];
2762

    
2763
            if(v->condover == CONDOVER_SELECT) {
2764
                if(v->overflg_is_raw)
2765
                    overlap = get_bits1(&v->s.gb);
2766
                else
2767
                    overlap = v->over_flags_plane[mb_pos];
2768
            } else
2769
                overlap = (v->condover == CONDOVER_ALL);
2770

    
2771
            GET_MQUANT();
2772

    
2773
            s->current_picture.qscale_table[mb_pos] = mquant;
2774
            /* Set DC scale - y and c use the same */
2775
            s->y_dc_scale = s->y_dc_scale_table[mquant];
2776
            s->c_dc_scale = s->c_dc_scale_table[mquant];
2777

    
2778
            for(k = 0; k < 6; k++) {
2779
                val = ((cbp >> (5 - k)) & 1);
2780

    
2781
                if (k < 4) {
2782
                    int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2783
                    val = val ^ pred;
2784
                    *coded_val = val;
2785
                }
2786
                cbp |= val << (5 - k);
2787

    
2788
                v->a_avail = !s->first_slice_line || (k==2 || k==3);
2789
                v->c_avail = !!s->mb_x || (k==1 || k==3);
2790

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

    
2793
                s->dsp.vc1_inv_trans_8x8(s->block[k]);
2794
                for(j = 0; j < 64; j++) s->block[k][j] += 128;
2795
            }
2796

    
2797
            vc1_put_block(v, s->block);
2798
            if(overlap) {
2799
                if(s->mb_x) {
2800
                    s->dsp.vc1_h_overlap(s->dest[0], s->linesize);
2801
                    s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2802
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
2803
                        s->dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
2804
                        s->dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
2805
                    }
2806
                }
2807
                s->dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
2808
                s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2809
                if(!s->first_slice_line) {
2810
                    s->dsp.vc1_v_overlap(s->dest[0], s->linesize);
2811
                    s->dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
2812
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
2813
                        s->dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
2814
                        s->dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
2815
                    }
2816
                }
2817
                s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2818
                s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2819
            }
2820
            if(v->s.loop_filter) vc1_loop_filter_iblk(s, v->pq);
2821

    
2822
            if(get_bits_count(&s->gb) > v->bits) {
2823
                ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
2824
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
2825
                return;
2826
            }
2827
        }
2828
        if (!v->s.loop_filter)
2829
            ff_draw_horiz_band(s, s->mb_y * 16, 16);
2830
        else if (s->mb_y)
2831
            ff_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
2832
        s->first_slice_line = 0;
2833
    }
2834
    if (v->s.loop_filter)
2835
        ff_draw_horiz_band(s, (s->mb_height-1)*16, 16);
2836
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2837
}
2838

    
2839
static void vc1_decode_p_blocks(VC1Context *v)
2840
{
2841
    MpegEncContext *s = &v->s;
2842

    
2843
    /* select codingmode used for VLC tables selection */
2844
    switch(v->c_ac_table_index){
2845
    case 0:
2846
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2847
        break;
2848
    case 1:
2849
        v->codingset = CS_HIGH_MOT_INTRA;
2850
        break;
2851
    case 2:
2852
        v->codingset = CS_MID_RATE_INTRA;
2853
        break;
2854
    }
2855

    
2856
    switch(v->c_ac_table_index){
2857
    case 0:
2858
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2859
        break;
2860
    case 1:
2861
        v->codingset2 = CS_HIGH_MOT_INTER;
2862
        break;
2863
    case 2:
2864
        v->codingset2 = CS_MID_RATE_INTER;
2865
        break;
2866
    }
2867

    
2868
    s->first_slice_line = 1;
2869
    memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
2870
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2871
        s->mb_x = 0;
2872
        ff_init_block_index(s);
2873
        for(; s->mb_x < s->mb_width; s->mb_x++) {
2874
            ff_update_block_index(s);
2875

    
2876
            vc1_decode_p_mb(v);
2877
            if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2878
                ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
2879
                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);
2880
                return;
2881
            }
2882
        }
2883
        memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0])*s->mb_stride);
2884
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
2885
        s->first_slice_line = 0;
2886
    }
2887
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2888
}
2889

    
2890
static void vc1_decode_b_blocks(VC1Context *v)
2891
{
2892
    MpegEncContext *s = &v->s;
2893

    
2894
    /* select codingmode used for VLC tables selection */
2895
    switch(v->c_ac_table_index){
2896
    case 0:
2897
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2898
        break;
2899
    case 1:
2900
        v->codingset = CS_HIGH_MOT_INTRA;
2901
        break;
2902
    case 2:
2903
        v->codingset = CS_MID_RATE_INTRA;
2904
        break;
2905
    }
2906

    
2907
    switch(v->c_ac_table_index){
2908
    case 0:
2909
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2910
        break;
2911
    case 1:
2912
        v->codingset2 = CS_HIGH_MOT_INTER;
2913
        break;
2914
    case 2:
2915
        v->codingset2 = CS_MID_RATE_INTER;
2916
        break;
2917
    }
2918

    
2919
    s->first_slice_line = 1;
2920
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2921
        s->mb_x = 0;
2922
        ff_init_block_index(s);
2923
        for(; s->mb_x < s->mb_width; s->mb_x++) {
2924
            ff_update_block_index(s);
2925

    
2926
            vc1_decode_b_mb(v);
2927
            if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2928
                ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
2929
                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);
2930
                return;
2931
            }
2932
            if(v->s.loop_filter) vc1_loop_filter_iblk(s, v->pq);
2933
        }
2934
        if (!v->s.loop_filter)
2935
            ff_draw_horiz_band(s, s->mb_y * 16, 16);
2936
        else if (s->mb_y)
2937
            ff_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
2938
        s->first_slice_line = 0;
2939
    }
2940
    if (v->s.loop_filter)
2941
        ff_draw_horiz_band(s, (s->mb_height-1)*16, 16);
2942
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2943
}
2944

    
2945
static void vc1_decode_skip_blocks(VC1Context *v)
2946
{
2947
    MpegEncContext *s = &v->s;
2948

    
2949
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2950
    s->first_slice_line = 1;
2951
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2952
        s->mb_x = 0;
2953
        ff_init_block_index(s);
2954
        ff_update_block_index(s);
2955
        memcpy(s->dest[0], s->last_picture.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
2956
        memcpy(s->dest[1], s->last_picture.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
2957
        memcpy(s->dest[2], s->last_picture.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
2958
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
2959
        s->first_slice_line = 0;
2960
    }
2961
    s->pict_type = FF_P_TYPE;
2962
}
2963

    
2964
static void vc1_decode_blocks(VC1Context *v)
2965
{
2966

    
2967
    v->s.esc3_level_length = 0;
2968
    if(v->x8_type){
2969
        ff_intrax8_decode_picture(&v->x8, 2*v->pq+v->halfpq, v->pq*(!v->pquantizer) );
2970
    }else{
2971

    
2972
        switch(v->s.pict_type) {
2973
        case FF_I_TYPE:
2974
            if(v->profile == PROFILE_ADVANCED)
2975
                vc1_decode_i_blocks_adv(v);
2976
            else
2977
                vc1_decode_i_blocks(v);
2978
            break;
2979
        case FF_P_TYPE:
2980
            if(v->p_frame_skipped)
2981
                vc1_decode_skip_blocks(v);
2982
            else
2983
                vc1_decode_p_blocks(v);
2984
            break;
2985
        case FF_B_TYPE:
2986
            if(v->bi_type){
2987
                if(v->profile == PROFILE_ADVANCED)
2988
                    vc1_decode_i_blocks_adv(v);
2989
                else
2990
                    vc1_decode_i_blocks(v);
2991
            }else
2992
                vc1_decode_b_blocks(v);
2993
            break;
2994
        }
2995
    }
2996
}
2997

    
2998
/** Initialize a VC1/WMV3 decoder
2999
 * @todo TODO: Handle VC-1 IDUs (Transport level?)
3000
 * @todo TODO: Decypher remaining bits in extra_data
3001
 */
3002
static av_cold int vc1_decode_init(AVCodecContext *avctx)
3003
{
3004
    VC1Context *v = avctx->priv_data;
3005
    MpegEncContext *s = &v->s;
3006
    GetBitContext gb;
3007
    int i;
3008

    
3009
    if (!avctx->extradata_size || !avctx->extradata) return -1;
3010
    if (!(avctx->flags & CODEC_FLAG_GRAY))
3011
        avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
3012
    else
3013
        avctx->pix_fmt = PIX_FMT_GRAY8;
3014
    avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
3015
    v->s.avctx = avctx;
3016
    avctx->flags |= CODEC_FLAG_EMU_EDGE;
3017
    v->s.flags |= CODEC_FLAG_EMU_EDGE;
3018

    
3019
    if(avctx->idct_algo==FF_IDCT_AUTO){
3020
        avctx->idct_algo=FF_IDCT_WMV2;
3021
    }
3022

    
3023
    if(ff_msmpeg4_decode_init(avctx) < 0)
3024
        return -1;
3025
    if (vc1_init_common(v) < 0) return -1;
3026
    for (i = 0; i < 64;  i++) {
3027
#define transpose(x) ((x>>3) | ((x&7)<<3))
3028
        v->zz_8x8[0][i] = transpose(wmv1_scantable[0][i]);
3029
        v->zz_8x8[1][i] = transpose(wmv1_scantable[1][i]);
3030
        v->zz_8x8[2][i] = transpose(wmv1_scantable[2][i]);
3031
        v->zz_8x8[3][i] = transpose(wmv1_scantable[3][i]);
3032
    }
3033

    
3034
    avctx->coded_width = avctx->width;
3035
    avctx->coded_height = avctx->height;
3036
    if (avctx->codec_id == CODEC_ID_WMV3)
3037
    {
3038
        int count = 0;
3039

    
3040
        // looks like WMV3 has a sequence header stored in the extradata
3041
        // advanced sequence header may be before the first frame
3042
        // the last byte of the extradata is a version number, 1 for the
3043
        // samples we can decode
3044

    
3045
        init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
3046

    
3047
        if (vc1_decode_sequence_header(avctx, v, &gb) < 0)
3048
          return -1;
3049

    
3050
        count = avctx->extradata_size*8 - get_bits_count(&gb);
3051
        if (count>0)
3052
        {
3053
            av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
3054
                   count, get_bits(&gb, count));
3055
        }
3056
        else if (count < 0)
3057
        {
3058
            av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
3059
        }
3060
    } else { // VC1/WVC1
3061
        const uint8_t *start = avctx->extradata;
3062
        uint8_t *end = avctx->extradata + avctx->extradata_size;
3063
        const uint8_t *next;
3064
        int size, buf2_size;
3065
        uint8_t *buf2 = NULL;
3066
        int seq_initialized = 0, ep_initialized = 0;
3067

    
3068
        if(avctx->extradata_size < 16) {
3069
            av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
3070
            return -1;
3071
        }
3072

    
3073
        buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
3074
        start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
3075
        next = start;
3076
        for(; next < end; start = next){
3077
            next = find_next_marker(start + 4, end);
3078
            size = next - start - 4;
3079
            if(size <= 0) continue;
3080
            buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
3081
            init_get_bits(&gb, buf2, buf2_size * 8);
3082
            switch(AV_RB32(start)){
3083
            case VC1_CODE_SEQHDR:
3084
                if(vc1_decode_sequence_header(avctx, v, &gb) < 0){
3085
                    av_free(buf2);
3086
                    return -1;
3087
                }
3088
                seq_initialized = 1;
3089
                break;
3090
            case VC1_CODE_ENTRYPOINT:
3091
                if(vc1_decode_entry_point(avctx, v, &gb) < 0){
3092
                    av_free(buf2);
3093
                    return -1;
3094
                }
3095
                ep_initialized = 1;
3096
                break;
3097
            }
3098
        }
3099
        av_free(buf2);
3100
        if(!seq_initialized || !ep_initialized){
3101
            av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
3102
            return -1;
3103
        }
3104
    }
3105
    avctx->has_b_frames= !!(avctx->max_b_frames);
3106
    s->low_delay = !avctx->has_b_frames;
3107

    
3108
    s->mb_width = (avctx->coded_width+15)>>4;
3109
    s->mb_height = (avctx->coded_height+15)>>4;
3110

    
3111
    /* Allocate mb bitplanes */
3112
    v->mv_type_mb_plane = av_malloc(s->mb_stride * s->mb_height);
3113
    v->direct_mb_plane = av_malloc(s->mb_stride * s->mb_height);
3114
    v->acpred_plane = av_malloc(s->mb_stride * s->mb_height);
3115
    v->over_flags_plane = av_malloc(s->mb_stride * s->mb_height);
3116

    
3117
    v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
3118
    v->cbp = v->cbp_base + s->mb_stride;
3119

    
3120
    /* allocate block type info in that way so it could be used with s->block_index[] */
3121
    v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
3122
    v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
3123
    v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
3124
    v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
3125

    
3126
    /* Init coded blocks info */
3127
    if (v->profile == PROFILE_ADVANCED)
3128
    {
3129
//        if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
3130
//            return -1;
3131
//        if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
3132
//            return -1;
3133
    }
3134

    
3135
    ff_intrax8_common_init(&v->x8,s);
3136
    return 0;
3137
}
3138

    
3139

    
3140
/** Decode a VC1/WMV3 frame
3141
 * @todo TODO: Handle VC-1 IDUs (Transport level?)
3142
 */
3143
static int vc1_decode_frame(AVCodecContext *avctx,
3144
                            void *data, int *data_size,
3145
                            AVPacket *avpkt)
3146
{
3147
    const uint8_t *buf = avpkt->data;
3148
    int buf_size = avpkt->size;
3149
    VC1Context *v = avctx->priv_data;
3150
    MpegEncContext *s = &v->s;
3151
    AVFrame *pict = data;
3152
    uint8_t *buf2 = NULL;
3153
    const uint8_t *buf_start = buf;
3154

    
3155
    /* no supplementary picture */
3156
    if (buf_size == 0) {
3157
        /* special case for last picture */
3158
        if (s->low_delay==0 && s->next_picture_ptr) {
3159
            *pict= *(AVFrame*)s->next_picture_ptr;
3160
            s->next_picture_ptr= NULL;
3161

    
3162
            *data_size = sizeof(AVFrame);
3163
        }
3164

    
3165
        return 0;
3166
    }
3167

    
3168
    /* We need to set current_picture_ptr before reading the header,
3169
     * otherwise we cannot store anything in there. */
3170
    if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
3171
        int i= ff_find_unused_picture(s, 0);
3172
        s->current_picture_ptr= &s->picture[i];
3173
    }
3174

    
3175
    if (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){
3176
        if (v->profile < PROFILE_ADVANCED)
3177
            avctx->pix_fmt = PIX_FMT_VDPAU_WMV3;
3178
        else
3179
            avctx->pix_fmt = PIX_FMT_VDPAU_VC1;
3180
    }
3181

    
3182
    //for advanced profile we may need to parse and unescape data
3183
    if (avctx->codec_id == CODEC_ID_VC1) {
3184
        int buf_size2 = 0;
3185
        buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
3186

    
3187
        if(IS_MARKER(AV_RB32(buf))){ /* frame starts with marker and needs to be parsed */
3188
            const uint8_t *start, *end, *next;
3189
            int size;
3190

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

    
3217
            divider = find_next_marker(buf, buf + buf_size);
3218
            if((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD){
3219
                av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
3220
                av_free(buf2);
3221
                return -1;
3222
            }
3223

    
3224
            buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
3225
            // TODO
3226
            if(!v->warn_interlaced++)
3227
                av_log(v->s.avctx, AV_LOG_ERROR, "Interlaced WVC1 support is not implemented\n");
3228
            av_free(buf2);return -1;
3229
        }else{
3230
            buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
3231
        }
3232
        init_get_bits(&s->gb, buf2, buf_size2*8);
3233
    } else
3234
        init_get_bits(&s->gb, buf, buf_size*8);
3235
    // do parse frame header
3236
    if(v->profile < PROFILE_ADVANCED) {
3237
        if(vc1_parse_frame_header(v, &s->gb) == -1) {
3238
            av_free(buf2);
3239
            return -1;
3240
        }
3241
    } else {
3242
        if(vc1_parse_frame_header_adv(v, &s->gb) == -1) {
3243
            av_free(buf2);
3244
            return -1;
3245
        }
3246
    }
3247

    
3248
    if(v->res_sprite && (s->pict_type!=FF_I_TYPE)){
3249
        av_free(buf2);
3250
        return -1;
3251
    }
3252

    
3253
    // for hurry_up==5
3254
    s->current_picture.pict_type= s->pict_type;
3255
    s->current_picture.key_frame= s->pict_type == FF_I_TYPE;
3256

    
3257
    /* skip B-frames if we don't have reference frames */
3258
    if(s->last_picture_ptr==NULL && (s->pict_type==FF_B_TYPE || s->dropable)){
3259
        av_free(buf2);
3260
        return -1;//buf_size;
3261
    }
3262
    /* skip b frames if we are in a hurry */
3263
    if(avctx->hurry_up && s->pict_type==FF_B_TYPE) return -1;//buf_size;
3264
    if(   (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==FF_B_TYPE)
3265
       || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=FF_I_TYPE)
3266
       ||  avctx->skip_frame >= AVDISCARD_ALL) {
3267
        av_free(buf2);
3268
        return buf_size;
3269
    }
3270
    /* skip everything if we are in a hurry>=5 */
3271
    if(avctx->hurry_up>=5) {
3272
        av_free(buf2);
3273
        return -1;//buf_size;
3274
    }
3275

    
3276
    if(s->next_p_frame_damaged){
3277
        if(s->pict_type==FF_B_TYPE)
3278
            return buf_size;
3279
        else
3280
            s->next_p_frame_damaged=0;
3281
    }
3282

    
3283
    if(MPV_frame_start(s, avctx) < 0) {
3284
        av_free(buf2);
3285
        return -1;
3286
    }
3287

    
3288
    s->me.qpel_put= s->dsp.put_qpel_pixels_tab;
3289
    s->me.qpel_avg= s->dsp.avg_qpel_pixels_tab;
3290

    
3291
    if ((CONFIG_VC1_VDPAU_DECODER)
3292
        &&s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
3293
        ff_vdpau_vc1_decode_picture(s, buf_start, (buf + buf_size) - buf_start);
3294
    else if (avctx->hwaccel) {
3295
        if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
3296
            return -1;
3297
        if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
3298
            return -1;
3299
        if (avctx->hwaccel->end_frame(avctx) < 0)
3300
            return -1;
3301
    } else {
3302
        ff_er_frame_start(s);
3303

    
3304
        v->bits = buf_size * 8;
3305
        vc1_decode_blocks(v);
3306
//av_log(s->avctx, AV_LOG_INFO, "Consumed %i/%i bits\n", get_bits_count(&s->gb), buf_size*8);
3307
//  if(get_bits_count(&s->gb) > buf_size * 8)
3308
//      return -1;
3309
        ff_er_frame_end(s);
3310
    }
3311

    
3312
    MPV_frame_end(s);
3313

    
3314
assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
3315
assert(s->current_picture.pict_type == s->pict_type);
3316
    if (s->pict_type == FF_B_TYPE || s->low_delay) {
3317
        *pict= *(AVFrame*)s->current_picture_ptr;
3318
    } else if (s->last_picture_ptr != NULL) {
3319
        *pict= *(AVFrame*)s->last_picture_ptr;
3320
    }
3321

    
3322
    if(s->last_picture_ptr || s->low_delay){
3323
        *data_size = sizeof(AVFrame);
3324
        ff_print_debug_info(s, pict);
3325
    }
3326

    
3327
    av_free(buf2);
3328
    return buf_size;
3329
}
3330

    
3331

    
3332
/** Close a VC1/WMV3 decoder
3333
 * @warning Initial try at using MpegEncContext stuff
3334
 */
3335
static av_cold int vc1_decode_end(AVCodecContext *avctx)
3336
{
3337
    VC1Context *v = avctx->priv_data;
3338

    
3339
    av_freep(&v->hrd_rate);
3340
    av_freep(&v->hrd_buffer);
3341
    MPV_common_end(&v->s);
3342
    av_freep(&v->mv_type_mb_plane);
3343
    av_freep(&v->direct_mb_plane);
3344
    av_freep(&v->acpred_plane);
3345
    av_freep(&v->over_flags_plane);
3346
    av_freep(&v->mb_type_base);
3347
    av_freep(&v->cbp_base);
3348
    ff_intrax8_common_end(&v->x8);
3349
    return 0;
3350
}
3351

    
3352

    
3353
AVCodec ff_vc1_decoder = {
3354
    "vc1",
3355
    AVMEDIA_TYPE_VIDEO,
3356
    CODEC_ID_VC1,
3357
    sizeof(VC1Context),
3358
    vc1_decode_init,
3359
    NULL,
3360
    vc1_decode_end,
3361
    vc1_decode_frame,
3362
    CODEC_CAP_DR1 | CODEC_CAP_DELAY,
3363
    NULL,
3364
    .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
3365
    .pix_fmts = ff_hwaccel_pixfmt_list_420
3366
};
3367

    
3368
#if CONFIG_WMV3_DECODER
3369
AVCodec ff_wmv3_decoder = {
3370
    "wmv3",
3371
    AVMEDIA_TYPE_VIDEO,
3372
    CODEC_ID_WMV3,
3373
    sizeof(VC1Context),
3374
    vc1_decode_init,
3375
    NULL,
3376
    vc1_decode_end,
3377
    vc1_decode_frame,
3378
    CODEC_CAP_DR1 | CODEC_CAP_DELAY,
3379
    NULL,
3380
    .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
3381
    .pix_fmts = ff_hwaccel_pixfmt_list_420
3382
};
3383
#endif
3384

    
3385
#if CONFIG_WMV3_VDPAU_DECODER
3386
AVCodec ff_wmv3_vdpau_decoder = {
3387
    "wmv3_vdpau",
3388
    AVMEDIA_TYPE_VIDEO,
3389
    CODEC_ID_WMV3,
3390
    sizeof(VC1Context),
3391
    vc1_decode_init,
3392
    NULL,
3393
    vc1_decode_end,
3394
    vc1_decode_frame,
3395
    CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
3396
    NULL,
3397
    .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
3398
    .pix_fmts = (const enum PixelFormat[]){PIX_FMT_VDPAU_WMV3, PIX_FMT_NONE}
3399
};
3400
#endif
3401

    
3402
#if CONFIG_VC1_VDPAU_DECODER
3403
AVCodec ff_vc1_vdpau_decoder = {
3404
    "vc1_vdpau",
3405
    AVMEDIA_TYPE_VIDEO,
3406
    CODEC_ID_VC1,
3407
    sizeof(VC1Context),
3408
    vc1_decode_init,
3409
    NULL,
3410
    vc1_decode_end,
3411
    vc1_decode_frame,
3412
    CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
3413
    NULL,
3414
    .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
3415
    .pix_fmts = (const enum PixelFormat[]){PIX_FMT_VDPAU_VC1, PIX_FMT_NONE}
3416
};
3417
#endif