Statistics
| Branch: | Revision:

ffmpeg / libavcodec / vc1dec.c @ 768c5251

History | View | Annotate | Download (132 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 Libav.
7
 *
8
 * Libav 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
 * Libav 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 Libav; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22

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

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

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

    
50

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

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

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

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

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

    
135
    return 0;
136
}
137

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

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

    
160

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
370
    srcY += src_y * s->linesize + src_x;
371

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

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

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

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

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

    
427

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

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

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

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

    
486
    s->current_picture.motion_val[1][s->block_index[0]][0] = tx;
487
    s->current_picture.motion_val[1][s->block_index[0]][1] = ty;
488
    uvmx = (tx + ((tx&3) == 3)) >> 1;
489
    uvmy = (ty + ((ty&3) == 3)) >> 1;
490
    v->luma_mv[s->mb_x][0] = uvmx;
491
    v->luma_mv[s->mb_x][1] = uvmy;
492
    if(v->fastuvmc) {
493
        uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
494
        uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
495
    }
496

    
497
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
498
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
499

    
500
    if(v->profile != PROFILE_ADVANCED){
501
        uvsrc_x = av_clip(uvsrc_x,  -8, s->mb_width  *  8);
502
        uvsrc_y = av_clip(uvsrc_y,  -8, s->mb_height *  8);
503
    }else{
504
        uvsrc_x = av_clip(uvsrc_x,  -8, s->avctx->coded_width  >> 1);
505
        uvsrc_y = av_clip(uvsrc_y,  -8, s->avctx->coded_height >> 1);
506
    }
507

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

    
520
        /* if we deal with range reduction we need to scale source blocks */
521
        if(v->rangeredfrm) {
522
            int i, j;
523
            uint8_t *src, *src2;
524

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

    
540
            src = srcU; src2 = srcV;
541
            for(j = 0; j < 9; j++) {
542
                for(i = 0; i < 9; i++) {
543
                    src[i] = v->lutuv[src[i]];
544
                    src2[i] = v->lutuv[src2[i]];
545
                }
546
                src += s->uvlinesize;
547
                src2 += s->uvlinesize;
548
            }
549
        }
550
    }
551

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

    
564
/***********************************************************************/
565
/**
566
 * @defgroup vc1block VC-1 Block-level functions
567
 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
568
 * @{
569
 */
570

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

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

    
658
/** Predict and set motion vector
659
 */
660
static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y, int mv1, int r_x, int r_y, uint8_t* is_intra)
661
{
662
    MpegEncContext *s = &v->s;
663
    int xy, wrap, off = 0;
664
    int16_t *A, *B, *C;
665
    int px, py;
666
    int sum;
667

    
668
    /* scale MV difference to be quad-pel */
669
    dmv_x <<= 1 - s->quarter_sample;
670
    dmv_y <<= 1 - s->quarter_sample;
671

    
672
    wrap = s->b8_stride;
673
    xy = s->block_index[n];
674

    
675
    if(s->mb_intra){
676
        s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
677
        s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
678
        s->current_picture.motion_val[1][xy][0] = 0;
679
        s->current_picture.motion_val[1][xy][1] = 0;
680
        if(mv1) { /* duplicate motion data for 1-MV block */
681
            s->current_picture.motion_val[0][xy + 1][0] = 0;
682
            s->current_picture.motion_val[0][xy + 1][1] = 0;
683
            s->current_picture.motion_val[0][xy + wrap][0] = 0;
684
            s->current_picture.motion_val[0][xy + wrap][1] = 0;
685
            s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
686
            s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
687
            v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
688
            s->current_picture.motion_val[1][xy + 1][0] = 0;
689
            s->current_picture.motion_val[1][xy + 1][1] = 0;
690
            s->current_picture.motion_val[1][xy + wrap][0] = 0;
691
            s->current_picture.motion_val[1][xy + wrap][1] = 0;
692
            s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
693
            s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
694
        }
695
        return;
696
    }
697

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

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

    
794
/** Motion compensation for direct or interpolated blocks in B-frames
795
 */
796
static void vc1_interp_mc(VC1Context *v)
797
{
798
    MpegEncContext *s = &v->s;
799
    DSPContext *dsp = &v->s.dsp;
800
    uint8_t *srcY, *srcU, *srcV;
801
    int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
802

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

    
805
    mx = s->mv[1][0][0];
806
    my = s->mv[1][0][1];
807
    uvmx = (mx + ((mx & 3) == 3)) >> 1;
808
    uvmy = (my + ((my & 3) == 3)) >> 1;
809
    if(v->fastuvmc) {
810
        uvmx = uvmx + ((uvmx<0)?-(uvmx&1):(uvmx&1));
811
        uvmy = uvmy + ((uvmy<0)?-(uvmy&1):(uvmy&1));
812
    }
813
    srcY = s->next_picture.data[0];
814
    srcU = s->next_picture.data[1];
815
    srcV = s->next_picture.data[2];
816

    
817
    src_x = s->mb_x * 16 + (mx >> 2);
818
    src_y = s->mb_y * 16 + (my >> 2);
819
    uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
820
    uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
821

    
822
    if(v->profile != PROFILE_ADVANCED){
823
        src_x   = av_clip(  src_x, -16, s->mb_width  * 16);
824
        src_y   = av_clip(  src_y, -16, s->mb_height * 16);
825
        uvsrc_x = av_clip(uvsrc_x,  -8, s->mb_width  *  8);
826
        uvsrc_y = av_clip(uvsrc_y,  -8, s->mb_height *  8);
827
    }else{
828
        src_x   = av_clip(  src_x, -17, s->avctx->coded_width);
829
        src_y   = av_clip(  src_y, -18, s->avctx->coded_height + 1);
830
        uvsrc_x = av_clip(uvsrc_x,  -8, s->avctx->coded_width  >> 1);
831
        uvsrc_y = av_clip(uvsrc_y,  -8, s->avctx->coded_height >> 1);
832
    }
833

    
834
    srcY += src_y * s->linesize + src_x;
835
    srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
836
    srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
837

    
838
    /* for grayscale we should not try to read from unknown area */
839
    if(s->flags & CODEC_FLAG_GRAY) {
840
        srcU = s->edge_emu_buffer + 18 * s->linesize;
841
        srcV = s->edge_emu_buffer + 18 * s->linesize;
842
    }
843

    
844
    if(v->rangeredfrm
845
       || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel*3
846
       || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 16 - s->mspel*3){
847
        uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
848

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

    
864
            src = srcY;
865
            for(j = 0; j < 17 + s->mspel*2; j++) {
866
                for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
867
                src += s->linesize;
868
            }
869
            src = srcU; src2 = srcV;
870
            for(j = 0; j < 9; j++) {
871
                for(i = 0; i < 9; i++) {
872
                    src[i] = ((src[i] - 128) >> 1) + 128;
873
                    src2[i] = ((src2[i] - 128) >> 1) + 128;
874
                }
875
                src += s->uvlinesize;
876
                src2 += s->uvlinesize;
877
            }
878
        }
879
        srcY += s->mspel * (1 + s->linesize);
880
    }
881

    
882
    if(s->mspel) {
883
        dxy = ((my & 3) << 2) | (mx & 3);
884
        v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0]    , srcY    , s->linesize, v->rnd);
885
        v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
886
        srcY += s->linesize * 8;
887
        v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize    , srcY    , s->linesize, v->rnd);
888
        v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
889
    } else { // hpel mc
890
        dxy = (my & 2) | ((mx & 2) >> 1);
891

    
892
        if(!v->rnd)
893
            dsp->avg_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
894
        else
895
            dsp->avg_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
896
    }
897

    
898
    if(s->flags & CODEC_FLAG_GRAY) return;
899
    /* Chroma MC always uses qpel blilinear */
900
    uvmx = (uvmx&3)<<1;
901
    uvmy = (uvmy&3)<<1;
902
    if(!v->rnd){
903
        dsp->avg_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
904
        dsp->avg_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
905
    }else{
906
        v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
907
        v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
908
    }
909
}
910

    
911
static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
912
{
913
    int n = bfrac;
914

    
915
#if B_FRACTION_DEN==256
916
    if(inv)
917
        n -= 256;
918
    if(!qs)
919
        return 2 * ((value * n + 255) >> 9);
920
    return (value * n + 128) >> 8;
921
#else
922
    if(inv)
923
        n -= B_FRACTION_DEN;
924
    if(!qs)
925
        return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
926
    return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
927
#endif
928
}
929

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

    
951
    if(v->use_ic && (mode == BMV_TYPE_BACKWARD)) v->mv_mode = v->mv_mode2;
952
    vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
953
    if(v->use_ic) v->mv_mode = v->mv_mode2;
954
}
955

    
956
static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mvtype)
957
{
958
    MpegEncContext *s = &v->s;
959
    int xy, wrap, off = 0;
960
    int16_t *A, *B, *C;
961
    int px, py;
962
    int sum;
963
    int r_x, r_y;
964
    const uint8_t *is_intra = v->mb_type[0];
965

    
966
    r_x = v->range_x;
967
    r_y = v->range_y;
968
    /* scale MV difference to be quad-pel */
969
    dmv_x[0] <<= 1 - s->quarter_sample;
970
    dmv_y[0] <<= 1 - s->quarter_sample;
971
    dmv_x[1] <<= 1 - s->quarter_sample;
972
    dmv_y[1] <<= 1 - s->quarter_sample;
973

    
974
    wrap = s->b8_stride;
975
    xy = s->block_index[0];
976

    
977
    if(s->mb_intra) {
978
        s->current_picture.motion_val[0][xy][0] =
979
        s->current_picture.motion_val[0][xy][1] =
980
        s->current_picture.motion_val[1][xy][0] =
981
        s->current_picture.motion_val[1][xy][1] = 0;
982
        return;
983
    }
984
    s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
985
    s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
986
    s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
987
    s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
988

    
989
    /* Pullback predicted motion vectors as specified in 8.4.5.4 */
990
    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));
991
    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));
992
    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));
993
    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));
994
    if(direct) {
995
        s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
996
        s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
997
        s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
998
        s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
999
        return;
1000
    }
1001

    
1002
    if((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
1003
        C = s->current_picture.motion_val[0][xy - 2];
1004
        A = s->current_picture.motion_val[0][xy - wrap*2];
1005
        off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1006
        B = s->current_picture.motion_val[0][xy - wrap*2 + off];
1007

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

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

    
1156
        s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
1157
        s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
1158
    }
1159
    s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
1160
    s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
1161
    s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
1162
    s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
1163
}
1164

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

    
1186
    /* find prediction - wmv3_dc_scale always used here in fact */
1187
    if (n < 4)     scale = s->y_dc_scale;
1188
    else           scale = s->c_dc_scale;
1189

    
1190
    wrap = s->block_wrap[n];
1191
    dc_val= s->dc_val[0] + s->block_index[n];
1192

    
1193
    /* B A
1194
     * C X
1195
     */
1196
    c = dc_val[ - 1];
1197
    b = dc_val[ - 1 - wrap];
1198
    a = dc_val[ - wrap];
1199

    
1200
    if (pq < 9 || !overlap)
1201
    {
1202
        /* Set outer values */
1203
        if (s->first_slice_line && (n!=2 && n!=3)) b=a=dcpred[scale];
1204
        if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=dcpred[scale];
1205
    }
1206
    else
1207
    {
1208
        /* Set outer values */
1209
        if (s->first_slice_line && (n!=2 && n!=3)) b=a=0;
1210
        if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=0;
1211
    }
1212

    
1213
    if (abs(a - b) <= abs(b - c)) {
1214
        pred = c;
1215
        *dir_ptr = 1;//left
1216
    } else {
1217
        pred = a;
1218
        *dir_ptr = 0;//top
1219
    }
1220

    
1221
    /* update predictor */
1222
    *dc_val_ptr = &dc_val[0];
1223
    return pred;
1224
}
1225

    
1226

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

    
1247
    wrap = s->block_wrap[n];
1248
    dc_val= s->dc_val[0] + s->block_index[n];
1249

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

    
1277
    if(a_avail && c_avail) {
1278
        if(abs(a - b) <= abs(b - c)) {
1279
            pred = c;
1280
            *dir_ptr = 1;//left
1281
        } else {
1282
            pred = a;
1283
            *dir_ptr = 0;//top
1284
        }
1285
    } else if(a_avail) {
1286
        pred = a;
1287
        *dir_ptr = 0;//top
1288
    } else if(c_avail) {
1289
        pred = c;
1290
        *dir_ptr = 1;//left
1291
    } else {
1292
        pred = 0;
1293
        *dir_ptr = 1;//left
1294
    }
1295

    
1296
    /* update predictor */
1297
    *dc_val_ptr = &dc_val[0];
1298
    return pred;
1299
}
1300

    
1301
/** @} */ // Block group
1302

    
1303
/**
1304
 * @defgroup vc1_std_mb VC1 Macroblock-level functions in Simple/Main Profiles
1305
 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1306
 * @{
1307
 */
1308

    
1309
static inline int vc1_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
1310
{
1311
    int xy, wrap, pred, a, b, c;
1312

    
1313
    xy = s->block_index[n];
1314
    wrap = s->b8_stride;
1315

    
1316
    /* B C
1317
     * A X
1318
     */
1319
    a = s->coded_block[xy - 1       ];
1320
    b = s->coded_block[xy - 1 - wrap];
1321
    c = s->coded_block[xy     - wrap];
1322

    
1323
    if (b == c) {
1324
        pred = a;
1325
    } else {
1326
        pred = c;
1327
    }
1328

    
1329
    /* store value */
1330
    *coded_block_ptr = &s->coded_block[xy];
1331

    
1332
    return pred;
1333
}
1334

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

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

    
1397
    *last = lst;
1398
    *skip = run;
1399
    *value = level;
1400
}
1401

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

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

    
1449
    /* Prediction */
1450
    dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
1451
    *dc_val = dcdiff;
1452

    
1453
    /* Store the quantized DC coeff, used for prediction */
1454
    if (n < 4) {
1455
        block[0] = dcdiff * s->y_dc_scale;
1456
    } else {
1457
        block[0] = dcdiff * s->c_dc_scale;
1458
    }
1459
    /* Skip ? */
1460
    if (!coded) {
1461
        goto not_coded;
1462
    }
1463

    
1464
    //AC Decoding
1465
    i = 1;
1466

    
1467
    {
1468
        int last = 0, skip, value;
1469
        const uint8_t *zz_table;
1470
        int scale;
1471
        int k;
1472

    
1473
        scale = v->pq * 2 + v->halfpq;
1474

    
1475
        if(v->s.ac_pred) {
1476
            if(!dc_pred_dir)
1477
                zz_table = v->zz_8x8[2];
1478
            else
1479
                zz_table = v->zz_8x8[3];
1480
        } else
1481
            zz_table = v->zz_8x8[1];
1482

    
1483
        ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1484
        ac_val2 = ac_val;
1485
        if(dc_pred_dir) //left
1486
            ac_val -= 16;
1487
        else //top
1488
            ac_val -= 16 * s->block_wrap[n];
1489

    
1490
        while (!last) {
1491
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1492
            i += skip;
1493
            if(i > 63)
1494
                break;
1495
            block[zz_table[i++]] = value;
1496
        }
1497

    
1498
        /* apply AC prediction if needed */
1499
        if(s->ac_pred) {
1500
            if(dc_pred_dir) { //left
1501
                for(k = 1; k < 8; k++)
1502
                    block[k] += ac_val[k];
1503
            } else { //top
1504
                for(k = 1; k < 8; k++)
1505
                    block[k << 3] += ac_val[k + 8];
1506
            }
1507
        }
1508
        /* save AC coeffs for further prediction */
1509
        for(k = 1; k < 8; k++) {
1510
            ac_val2[k] = block[k];
1511
            ac_val2[k + 8] = block[k << 3];
1512
        }
1513

    
1514
        /* scale AC coeffs */
1515
        for(k = 1; k < 64; k++)
1516
            if(block[k]) {
1517
                block[k] *= scale;
1518
                if(!v->pquantizer)
1519
                    block[k] += (block[k] < 0) ? -v->pq : v->pq;
1520
            }
1521

    
1522
        if(s->ac_pred) i = 63;
1523
    }
1524

    
1525
not_coded:
1526
    if(!coded) {
1527
        int k, scale;
1528
        ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1529
        ac_val2 = ac_val;
1530

    
1531
        i = 0;
1532
        scale = v->pq * 2 + v->halfpq;
1533
        memset(ac_val2, 0, 16 * 2);
1534
        if(dc_pred_dir) {//left
1535
            ac_val -= 16;
1536
            if(s->ac_pred)
1537
                memcpy(ac_val2, ac_val, 8 * 2);
1538
        } else {//top
1539
            ac_val -= 16 * s->block_wrap[n];
1540
            if(s->ac_pred)
1541
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1542
        }
1543

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

    
1564
    return 0;
1565
}
1566

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

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

    
1620
    /* Prediction */
1621
    dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
1622
    *dc_val = dcdiff;
1623

    
1624
    /* Store the quantized DC coeff, used for prediction */
1625
    if (n < 4) {
1626
        block[0] = dcdiff * s->y_dc_scale;
1627
    } else {
1628
        block[0] = dcdiff * s->c_dc_scale;
1629
    }
1630

    
1631
    //AC Decoding
1632
    i = 1;
1633

    
1634
    /* check if AC is needed at all */
1635
    if(!a_avail && !c_avail) use_pred = 0;
1636
    ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1637
    ac_val2 = ac_val;
1638

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

    
1641
    if(dc_pred_dir) //left
1642
        ac_val -= 16;
1643
    else //top
1644
        ac_val -= 16 * s->block_wrap[n];
1645

    
1646
    q1 = s->current_picture.qscale_table[mb_pos];
1647
    if(dc_pred_dir && c_avail && mb_pos) q2 = s->current_picture.qscale_table[mb_pos - 1];
1648
    if(!dc_pred_dir && a_avail && mb_pos >= s->mb_stride) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
1649
    if(dc_pred_dir && n==1) q2 = q1;
1650
    if(!dc_pred_dir && n==2) q2 = q1;
1651
    if(n==3) q2 = q1;
1652

    
1653
    if(coded) {
1654
        int last = 0, skip, value;
1655
        const uint8_t *zz_table;
1656
        int k;
1657

    
1658
        if(v->s.ac_pred) {
1659
            if(!dc_pred_dir)
1660
                zz_table = v->zz_8x8[2];
1661
            else
1662
                zz_table = v->zz_8x8[3];
1663
        } else
1664
            zz_table = v->zz_8x8[1];
1665

    
1666
        while (!last) {
1667
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1668
            i += skip;
1669
            if(i > 63)
1670
                break;
1671
            block[zz_table[i++]] = value;
1672
        }
1673

    
1674
        /* apply AC prediction if needed */
1675
        if(use_pred) {
1676
            /* scale predictors if needed*/
1677
            if(q2 && q1!=q2) {
1678
                q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1679
                q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1680

    
1681
                if(dc_pred_dir) { //left
1682
                    for(k = 1; k < 8; k++)
1683
                        block[k] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1684
                } else { //top
1685
                    for(k = 1; k < 8; k++)
1686
                        block[k << 3] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1687
                }
1688
            } else {
1689
                if(dc_pred_dir) { //left
1690
                    for(k = 1; k < 8; k++)
1691
                        block[k] += ac_val[k];
1692
                } else { //top
1693
                    for(k = 1; k < 8; k++)
1694
                        block[k << 3] += ac_val[k + 8];
1695
                }
1696
            }
1697
        }
1698
        /* save AC coeffs for further prediction */
1699
        for(k = 1; k < 8; k++) {
1700
            ac_val2[k] = block[k];
1701
            ac_val2[k + 8] = block[k << 3];
1702
        }
1703

    
1704
        /* scale AC coeffs */
1705
        for(k = 1; k < 64; k++)
1706
            if(block[k]) {
1707
                block[k] *= scale;
1708
                if(!v->pquantizer)
1709
                    block[k] += (block[k] < 0) ? -mquant : mquant;
1710
            }
1711

    
1712
        if(use_pred) i = 63;
1713
    } else { // no AC coeffs
1714
        int k;
1715

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

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

    
1759
    return 0;
1760
}
1761

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

    
1785
    s->dsp.clear_block(block);
1786

    
1787
    /* XXX: Guard against dumb values of mquant */
1788
    mquant = (mquant < 1) ? 0 : ( (mquant>31) ? 31 : mquant );
1789

    
1790
    /* Set DC scale - y and c use the same */
1791
    s->y_dc_scale = s->y_dc_scale_table[mquant];
1792
    s->c_dc_scale = s->c_dc_scale_table[mquant];
1793

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

    
1824
    /* Prediction */
1825
    dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
1826
    *dc_val = dcdiff;
1827

    
1828
    /* Store the quantized DC coeff, used for prediction */
1829

    
1830
    if (n < 4) {
1831
        block[0] = dcdiff * s->y_dc_scale;
1832
    } else {
1833
        block[0] = dcdiff * s->c_dc_scale;
1834
    }
1835

    
1836
    //AC Decoding
1837
    i = 1;
1838

    
1839
    /* check if AC is needed at all and adjust direction if needed */
1840
    if(!a_avail) dc_pred_dir = 1;
1841
    if(!c_avail) dc_pred_dir = 0;
1842
    if(!a_avail && !c_avail) use_pred = 0;
1843
    ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1844
    ac_val2 = ac_val;
1845

    
1846
    scale = mquant * 2 + v->halfpq;
1847

    
1848
    if(dc_pred_dir) //left
1849
        ac_val -= 16;
1850
    else //top
1851
        ac_val -= 16 * s->block_wrap[n];
1852

    
1853
    q1 = s->current_picture.qscale_table[mb_pos];
1854
    if(dc_pred_dir && c_avail && mb_pos) q2 = s->current_picture.qscale_table[mb_pos - 1];
1855
    if(!dc_pred_dir && a_avail && mb_pos >= s->mb_stride) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
1856
    if(dc_pred_dir && n==1) q2 = q1;
1857
    if(!dc_pred_dir && n==2) q2 = q1;
1858
    if(n==3) q2 = q1;
1859

    
1860
    if(coded) {
1861
        int last = 0, skip, value;
1862
        int k;
1863

    
1864
        while (!last) {
1865
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1866
            i += skip;
1867
            if(i > 63)
1868
                break;
1869
            block[v->zz_8x8[0][i++]] = value;
1870
        }
1871

    
1872
        /* apply AC prediction if needed */
1873
        if(use_pred) {
1874
            /* scale predictors if needed*/
1875
            if(q2 && q1!=q2) {
1876
                q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1877
                q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1878

    
1879
                if(dc_pred_dir) { //left
1880
                    for(k = 1; k < 8; k++)
1881
                        block[k] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1882
                } else { //top
1883
                    for(k = 1; k < 8; k++)
1884
                        block[k << 3] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1885
                }
1886
            } else {
1887
                if(dc_pred_dir) { //left
1888
                    for(k = 1; k < 8; k++)
1889
                        block[k] += ac_val[k];
1890
                } else { //top
1891
                    for(k = 1; k < 8; k++)
1892
                        block[k << 3] += ac_val[k + 8];
1893
                }
1894
            }
1895
        }
1896
        /* save AC coeffs for further prediction */
1897
        for(k = 1; k < 8; k++) {
1898
            ac_val2[k] = block[k];
1899
            ac_val2[k + 8] = block[k << 3];
1900
        }
1901

    
1902
        /* scale AC coeffs */
1903
        for(k = 1; k < 64; k++)
1904
            if(block[k]) {
1905
                block[k] *= scale;
1906
                if(!v->pquantizer)
1907
                    block[k] += (block[k] < 0) ? -mquant : mquant;
1908
            }
1909

    
1910
        if(use_pred) i = 63;
1911
    } else { // no AC coeffs
1912
        int k;
1913

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

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

    
1957
    return 0;
1958
}
1959

    
1960
/** Decode P block
1961
 */
1962
static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n, int mquant, int ttmb, int first_block,
1963
                              uint8_t *dst, int linesize, int skip_block, int *ttmb_out)
1964
{
1965
    MpegEncContext *s = &v->s;
1966
    GetBitContext *gb = &s->gb;
1967
    int i, j;
1968
    int subblkpat = 0;
1969
    int scale, off, idx, last, skip, value;
1970
    int ttblk = ttmb & 7;
1971
    int pat = 0;
1972

    
1973
    s->dsp.clear_block(block);
1974

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

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

    
2101
/** @} */ // Macroblock group
2102

    
2103
static const int size_table  [6] = { 0, 2, 3, 4,  5,  8 };
2104
static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
2105

    
2106
static av_always_inline void vc1_apply_p_v_loop_filter(VC1Context *v, int block_num)
2107
{
2108
    MpegEncContext *s = &v->s;
2109
    int mb_cbp         = v->cbp[s->mb_x - s->mb_stride],
2110
        block_cbp      = mb_cbp      >> (block_num * 4), bottom_cbp,
2111
        mb_is_intra    = v->is_intra[s->mb_x - s->mb_stride],
2112
        block_is_intra = mb_is_intra >> (block_num * 4), bottom_is_intra;
2113
    int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
2114
    uint8_t *dst;
2115

    
2116
    if(block_num > 3) {
2117
        dst      = s->dest[block_num - 3];
2118
    } else {
2119
        dst      = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 8) * linesize;
2120
    }
2121
    if (s->mb_y != s->mb_height || block_num < 2) {
2122
        int16_t (*mv)[2];
2123
        int mv_stride;
2124

    
2125
        if(block_num > 3) {
2126
            bottom_cbp      = v->cbp[s->mb_x]      >> (block_num * 4);
2127
            bottom_is_intra = v->is_intra[s->mb_x] >> (block_num * 4);
2128
            mv              = &v->luma_mv[s->mb_x - s->mb_stride];
2129
            mv_stride       = s->mb_stride;
2130
        } else {
2131
            bottom_cbp      = (block_num < 2) ? (mb_cbp               >> ((block_num + 2) * 4)) :
2132
                                                (v->cbp[s->mb_x]      >> ((block_num - 2) * 4));
2133
            bottom_is_intra = (block_num < 2) ? (mb_is_intra          >> ((block_num + 2) * 4)) :
2134
                                                (v->is_intra[s->mb_x] >> ((block_num - 2) * 4));
2135
            mv_stride       = s->b8_stride;
2136
            mv              = &s->current_picture.motion_val[0][s->block_index[block_num] - 2 * mv_stride];
2137
        }
2138

    
2139
        if (bottom_is_intra & 1 || block_is_intra & 1 ||
2140
            mv[0][0] != mv[mv_stride][0] || mv[0][1] != mv[mv_stride][1]) {
2141
            v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
2142
        } else {
2143
            idx = ((bottom_cbp >> 2) | block_cbp) & 3;
2144
            if(idx == 3) {
2145
                v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
2146
            } else if (idx) {
2147
                if (idx == 1)
2148
                    v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
2149
                else
2150
                    v->vc1dsp.vc1_v_loop_filter4(dst,     linesize, v->pq);
2151
            }
2152
        }
2153
    }
2154

    
2155
    dst -= 4 * linesize;
2156
    ttblk = (v->ttblk[s->mb_x - s->mb_stride] >> (block_num * 4)) & 0xf;
2157
    if (ttblk == TT_4X4 || ttblk == TT_8X4) {
2158
        idx = (block_cbp | (block_cbp >> 2)) & 3;
2159
        if (idx == 3) {
2160
            v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
2161
        } else if (idx) {
2162
            if (idx == 1)
2163
                v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
2164
            else
2165
                v->vc1dsp.vc1_v_loop_filter4(dst,     linesize, v->pq);
2166
        }
2167
    }
2168
}
2169

    
2170
static av_always_inline void vc1_apply_p_h_loop_filter(VC1Context *v, int block_num)
2171
{
2172
    MpegEncContext *s = &v->s;
2173
    int mb_cbp         = v->cbp[s->mb_x - 1 - s->mb_stride],
2174
        block_cbp      = mb_cbp      >> (block_num * 4), right_cbp,
2175
        mb_is_intra    = v->is_intra[s->mb_x - 1 - s->mb_stride],
2176
        block_is_intra = mb_is_intra >> (block_num * 4), right_is_intra;
2177
    int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
2178
    uint8_t *dst;
2179

    
2180
    if (block_num > 3) {
2181
        dst = s->dest[block_num - 3] - 8 * linesize;
2182
    } else {
2183
        dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 16) * linesize - 8;
2184
    }
2185

    
2186
    if (s->mb_x != s->mb_width || !(block_num & 5)) {
2187
        int16_t (*mv)[2];
2188

    
2189
        if(block_num > 3) {
2190
            right_cbp      = v->cbp[s->mb_x - s->mb_stride] >> (block_num * 4);
2191
            right_is_intra = v->is_intra[s->mb_x - s->mb_stride] >> (block_num * 4);
2192
            mv             = &v->luma_mv[s->mb_x - s->mb_stride - 1];
2193
        }else{
2194
            right_cbp      = (block_num & 1) ? (v->cbp[s->mb_x - s->mb_stride]      >> ((block_num - 1) * 4)) :
2195
                                               (mb_cbp                              >> ((block_num + 1) * 4));
2196
            right_is_intra = (block_num & 1) ? (v->is_intra[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4)) :
2197
                                               (mb_is_intra                         >> ((block_num + 1) * 4));
2198
            mv             = &s->current_picture.motion_val[0][s->block_index[block_num] - s->b8_stride * 2 - 2];
2199
        }
2200
        if (block_is_intra & 1 || right_is_intra & 1 || mv[0][0] != mv[1][0] || mv[0][1] != mv[1][1]) {
2201
            v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
2202
        } else {
2203
            idx = ((right_cbp >> 1) | block_cbp) & 5; // FIXME check
2204
            if (idx == 5) {
2205
                v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
2206
            } else if (idx) {
2207
                if (idx == 1)
2208
                    v->vc1dsp.vc1_h_loop_filter4(dst+4*linesize, linesize, v->pq);
2209
                else
2210
                    v->vc1dsp.vc1_h_loop_filter4(dst,            linesize, v->pq);
2211
            }
2212
        }
2213
    }
2214

    
2215
    dst -= 4;
2216
    ttblk = (v->ttblk[s->mb_x - s->mb_stride - 1] >> (block_num * 4)) & 0xf;
2217
    if (ttblk == TT_4X4 || ttblk == TT_4X8) {
2218
        idx = (block_cbp | (block_cbp >> 1)) & 5;
2219
        if (idx == 5) {
2220
            v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
2221
        } else if (idx) {
2222
            if (idx == 1)
2223
                v->vc1dsp.vc1_h_loop_filter4(dst + linesize*4, linesize, v->pq);
2224
            else
2225
                v->vc1dsp.vc1_h_loop_filter4(dst,              linesize, v->pq);
2226
        }
2227
    }
2228
}
2229

    
2230
static void vc1_apply_p_loop_filter(VC1Context *v)
2231
{
2232
    MpegEncContext *s = &v->s;
2233
    int i;
2234

    
2235
    for (i = 0; i < 6; i++) {
2236
        vc1_apply_p_v_loop_filter(v, i);
2237
    }
2238

    
2239
    /* V always preceedes H, therefore we run H one MB before V;
2240
     * at the end of a row, we catch up to complete the row */
2241
    if (s->mb_x) {
2242
        for (i = 0; i < 6; i++) {
2243
            vc1_apply_p_h_loop_filter(v, i);
2244
        }
2245
        if (s->mb_x == s->mb_width - 1) {
2246
            s->mb_x++;
2247
            ff_update_block_index(s);
2248
            for (i = 0; i < 6; i++) {
2249
                vc1_apply_p_h_loop_filter(v, i);
2250
            }
2251
        }
2252
    }
2253
}
2254

    
2255
/** Decode one P-frame MB (in Simple/Main profile)
2256
 */
2257
static int vc1_decode_p_mb(VC1Context *v)
2258
{
2259
    MpegEncContext *s = &v->s;
2260
    GetBitContext *gb = &s->gb;
2261
    int i;
2262
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2263
    int cbp; /* cbp decoding stuff */
2264
    int mqdiff, mquant; /* MB quantization */
2265
    int ttmb = v->ttfrm; /* MB Transform type */
2266

    
2267
    int mb_has_coeffs = 1; /* last_flag */
2268
    int dmv_x, dmv_y; /* Differential MV components */
2269
    int index, index1; /* LUT indexes */
2270
    int val, sign; /* temp values */
2271
    int first_block = 1;
2272
    int dst_idx, off;
2273
    int skipped, fourmv;
2274
    int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
2275

    
2276
    mquant = v->pq; /* Loosy initialization */
2277

    
2278
    if (v->mv_type_is_raw)
2279
        fourmv = get_bits1(gb);
2280
    else
2281
        fourmv = v->mv_type_mb_plane[mb_pos];
2282
    if (v->skip_is_raw)
2283
        skipped = get_bits1(gb);
2284
    else
2285
        skipped = v->s.mbskip_table[mb_pos];
2286

    
2287
    if (!fourmv) /* 1MV mode */
2288
    {
2289
        if (!skipped)
2290
        {
2291
            vc1_idct_func idct8x8_fn;
2292

    
2293
            GET_MVDATA(dmv_x, dmv_y);
2294

    
2295
            if (s->mb_intra) {
2296
                s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2297
                s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2298
            }
2299
            s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
2300
            vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
2301

    
2302
            /* FIXME Set DC val for inter block ? */
2303
            if (s->mb_intra && !mb_has_coeffs)
2304
            {
2305
                GET_MQUANT();
2306
                s->ac_pred = get_bits1(gb);
2307
                cbp = 0;
2308
            }
2309
            else if (mb_has_coeffs)
2310
            {
2311
                if (s->mb_intra) s->ac_pred = get_bits1(gb);
2312
                cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2313
                GET_MQUANT();
2314
            }
2315
            else
2316
            {
2317
                mquant = v->pq;
2318
                cbp = 0;
2319
            }
2320
            s->current_picture.qscale_table[mb_pos] = mquant;
2321

    
2322
            if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
2323
                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
2324
                                VC1_TTMB_VLC_BITS, 2);
2325
            if(!s->mb_intra) vc1_mc_1mv(v, 0);
2326
            dst_idx = 0;
2327
            idct8x8_fn = v->vc1dsp.vc1_inv_trans_8x8_put_signed[!!v->rangeredfrm];
2328
            for (i=0; i<6; i++)
2329
            {
2330
                s->dc_val[0][s->block_index[i]] = 0;
2331
                dst_idx += i >> 2;
2332
                val = ((cbp >> (5 - i)) & 1);
2333
                off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2334
                v->mb_type[0][s->block_index[i]] = s->mb_intra;
2335
                if(s->mb_intra) {
2336
                    /* check if prediction blocks A and C are available */
2337
                    v->a_avail = v->c_avail = 0;
2338
                    if(i == 2 || i == 3 || !s->first_slice_line)
2339
                        v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2340
                    if(i == 1 || i == 3 || s->mb_x)
2341
                        v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2342

    
2343
                    vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
2344
                    if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
2345
                    idct8x8_fn(s->dest[dst_idx] + off,
2346
                               i & 4 ? s->uvlinesize : s->linesize,
2347
                               s->block[i]);
2348
                    if(v->pq >= 9 && v->overlap) {
2349
                        if(v->c_avail)
2350
                            v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
2351
                        if(v->a_avail)
2352
                            v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
2353
                    }
2354
                    block_cbp |= 0xF << (i << 2);
2355
                    block_intra |= 1 << i;
2356
                } else if(val) {
2357
                    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), &block_tt);
2358
                    block_cbp |= pat << (i << 2);
2359
                    if(!v->ttmbf && ttmb < 8) ttmb = -1;
2360
                    first_block = 0;
2361
                }
2362
            }
2363
        }
2364
        else //Skipped
2365
        {
2366
            s->mb_intra = 0;
2367
            for(i = 0; i < 6; i++) {
2368
                v->mb_type[0][s->block_index[i]] = 0;
2369
                s->dc_val[0][s->block_index[i]] = 0;
2370
            }
2371
            s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
2372
            s->current_picture.qscale_table[mb_pos] = 0;
2373
            vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);
2374
            vc1_mc_1mv(v, 0);
2375
        }
2376
    } //1MV mode
2377
    else //4MV mode
2378
    {
2379
        if (!skipped /* unskipped MB */)
2380
        {
2381
            int intra_count = 0, coded_inter = 0;
2382
            int is_intra[6], is_coded[6];
2383
            vc1_idct_func idct8x8_fn;
2384
            /* Get CBPCY */
2385
            cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2386
            for (i=0; i<6; i++)
2387
            {
2388
                val = ((cbp >> (5 - i)) & 1);
2389
                s->dc_val[0][s->block_index[i]] = 0;
2390
                s->mb_intra = 0;
2391
                if(i < 4) {
2392
                    dmv_x = dmv_y = 0;
2393
                    s->mb_intra = 0;
2394
                    mb_has_coeffs = 0;
2395
                    if(val) {
2396
                        GET_MVDATA(dmv_x, dmv_y);
2397
                    }
2398
                    vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);
2399
                    if(!s->mb_intra) vc1_mc_4mv_luma(v, i);
2400
                    intra_count += s->mb_intra;
2401
                    is_intra[i] = s->mb_intra;
2402
                    is_coded[i] = mb_has_coeffs;
2403
                }
2404
                if(i&4){
2405
                    is_intra[i] = (intra_count >= 3);
2406
                    is_coded[i] = val;
2407
                }
2408
                if(i == 4) vc1_mc_4mv_chroma(v);
2409
                v->mb_type[0][s->block_index[i]] = is_intra[i];
2410
                if(!coded_inter) coded_inter = !is_intra[i] & is_coded[i];
2411
            }
2412
            // if there are no coded blocks then don't do anything more
2413
            dst_idx = 0;
2414
            if(!intra_count && !coded_inter)
2415
                goto end;
2416
            GET_MQUANT();
2417
            s->current_picture.qscale_table[mb_pos] = mquant;
2418
            /* test if block is intra and has pred */
2419
            {
2420
                int intrapred = 0;
2421
                for(i=0; i<6; i++)
2422
                    if(is_intra[i]) {
2423
                        if(((!s->first_slice_line || (i==2 || i==3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
2424
                            || ((s->mb_x || (i==1 || i==3)) && v->mb_type[0][s->block_index[i] - 1])) {
2425
                            intrapred = 1;
2426
                            break;
2427
                        }
2428
                    }
2429
                if(intrapred)s->ac_pred = get_bits1(gb);
2430
                else s->ac_pred = 0;
2431
            }
2432
            if (!v->ttmbf && coded_inter)
2433
                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2434
            idct8x8_fn = v->vc1dsp.vc1_inv_trans_8x8_put_signed[!!v->rangeredfrm];
2435
            for (i=0; i<6; i++)
2436
            {
2437
                dst_idx += i >> 2;
2438
                off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2439
                s->mb_intra = is_intra[i];
2440
                if (is_intra[i]) {
2441
                    /* check if prediction blocks A and C are available */
2442
                    v->a_avail = v->c_avail = 0;
2443
                    if(i == 2 || i == 3 || !s->first_slice_line)
2444
                        v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2445
                    if(i == 1 || i == 3 || s->mb_x)
2446
                        v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2447

    
2448
                    vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant, (i&4)?v->codingset2:v->codingset);
2449
                    if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
2450
                    idct8x8_fn(s->dest[dst_idx] + off,
2451
                               (i&4)?s->uvlinesize:s->linesize,
2452
                               s->block[i]);
2453
                    if(v->pq >= 9 && v->overlap) {
2454
                        if(v->c_avail)
2455
                            v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
2456
                        if(v->a_avail)
2457
                            v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
2458
                    }
2459
                    block_cbp |= 0xF << (i << 2);
2460
                    block_intra |= 1 << i;
2461
                } else if(is_coded[i]) {
2462
                    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), &block_tt);
2463
                    block_cbp |= pat << (i << 2);
2464
                    if(!v->ttmbf && ttmb < 8) ttmb = -1;
2465
                    first_block = 0;
2466
                }
2467
            }
2468
        }
2469
        else //Skipped MB
2470
        {
2471
            s->mb_intra = 0;
2472
            s->current_picture.qscale_table[mb_pos] = 0;
2473
            for (i=0; i<6; i++) {
2474
                v->mb_type[0][s->block_index[i]] = 0;
2475
                s->dc_val[0][s->block_index[i]] = 0;
2476
            }
2477
            for (i=0; i<4; i++)
2478
            {
2479
                vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0]);
2480
                vc1_mc_4mv_luma(v, i);
2481
            }
2482
            vc1_mc_4mv_chroma(v);
2483
            s->current_picture.qscale_table[mb_pos] = 0;
2484
        }
2485
    }
2486
end:
2487
    v->cbp[s->mb_x] = block_cbp;
2488
    v->ttblk[s->mb_x] = block_tt;
2489
    v->is_intra[s->mb_x] = block_intra;
2490

    
2491
    return 0;
2492
}
2493

    
2494
/** Decode one B-frame MB (in Main profile)
2495
 */
2496
static void vc1_decode_b_mb(VC1Context *v)
2497
{
2498
    MpegEncContext *s = &v->s;
2499
    GetBitContext *gb = &s->gb;
2500
    int i;
2501
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2502
    int cbp = 0; /* cbp decoding stuff */
2503
    int mqdiff, mquant; /* MB quantization */
2504
    int ttmb = v->ttfrm; /* MB Transform type */
2505
    int mb_has_coeffs = 0; /* last_flag */
2506
    int index, index1; /* LUT indexes */
2507
    int val, sign; /* temp values */
2508
    int first_block = 1;
2509
    int dst_idx, off;
2510
    int skipped, direct;
2511
    int dmv_x[2], dmv_y[2];
2512
    int bmvtype = BMV_TYPE_BACKWARD;
2513
    vc1_idct_func idct8x8_fn;
2514

    
2515
    mquant = v->pq; /* Loosy initialization */
2516
    s->mb_intra = 0;
2517

    
2518
    if (v->dmb_is_raw)
2519
        direct = get_bits1(gb);
2520
    else
2521
        direct = v->direct_mb_plane[mb_pos];
2522
    if (v->skip_is_raw)
2523
        skipped = get_bits1(gb);
2524
    else
2525
        skipped = v->s.mbskip_table[mb_pos];
2526

    
2527
    dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
2528
    for(i = 0; i < 6; i++) {
2529
        v->mb_type[0][s->block_index[i]] = 0;
2530
        s->dc_val[0][s->block_index[i]] = 0;
2531
    }
2532
    s->current_picture.qscale_table[mb_pos] = 0;
2533

    
2534
    if (!direct) {
2535
        if (!skipped) {
2536
            GET_MVDATA(dmv_x[0], dmv_y[0]);
2537
            dmv_x[1] = dmv_x[0];
2538
            dmv_y[1] = dmv_y[0];
2539
        }
2540
        if(skipped || !s->mb_intra) {
2541
            bmvtype = decode012(gb);
2542
            switch(bmvtype) {
2543
            case 0:
2544
                bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
2545
                break;
2546
            case 1:
2547
                bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
2548
                break;
2549
            case 2:
2550
                bmvtype = BMV_TYPE_INTERPOLATED;
2551
                dmv_x[0] = dmv_y[0] = 0;
2552
            }
2553
        }
2554
    }
2555
    for(i = 0; i < 6; i++)
2556
        v->mb_type[0][s->block_index[i]] = s->mb_intra;
2557

    
2558
    if (skipped) {
2559
        if(direct) bmvtype = BMV_TYPE_INTERPOLATED;
2560
        vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2561
        vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2562
        return;
2563
    }
2564
    if (direct) {
2565
        cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2566
        GET_MQUANT();
2567
        s->mb_intra = 0;
2568
        s->current_picture.qscale_table[mb_pos] = mquant;
2569
        if(!v->ttmbf)
2570
            ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2571
        dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
2572
        vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2573
        vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2574
    } else {
2575
        if(!mb_has_coeffs && !s->mb_intra) {
2576
            /* no coded blocks - effectively skipped */
2577
            vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2578
            vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2579
            return;
2580
        }
2581
        if(s->mb_intra && !mb_has_coeffs) {
2582
            GET_MQUANT();
2583
            s->current_picture.qscale_table[mb_pos] = mquant;
2584
            s->ac_pred = get_bits1(gb);
2585
            cbp = 0;
2586
            vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2587
        } else {
2588
            if(bmvtype == BMV_TYPE_INTERPOLATED) {
2589
                GET_MVDATA(dmv_x[0], dmv_y[0]);
2590
                if(!mb_has_coeffs) {
2591
                    /* interpolated skipped block */
2592
                    vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2593
                    vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2594
                    return;
2595
                }
2596
            }
2597
            vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2598
            if(!s->mb_intra) {
2599
                vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2600
            }
2601
            if(s->mb_intra)
2602
                s->ac_pred = get_bits1(gb);
2603
            cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2604
            GET_MQUANT();
2605
            s->current_picture.qscale_table[mb_pos] = mquant;
2606
            if(!v->ttmbf && !s->mb_intra && mb_has_coeffs)
2607
                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2608
        }
2609
    }
2610
    dst_idx = 0;
2611
    idct8x8_fn = v->vc1dsp.vc1_inv_trans_8x8_put_signed[!!v->rangeredfrm];
2612
    for (i=0; i<6; i++)
2613
    {
2614
        s->dc_val[0][s->block_index[i]] = 0;
2615
        dst_idx += i >> 2;
2616
        val = ((cbp >> (5 - i)) & 1);
2617
        off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2618
        v->mb_type[0][s->block_index[i]] = s->mb_intra;
2619
        if(s->mb_intra) {
2620
            /* check if prediction blocks A and C are available */
2621
            v->a_avail = v->c_avail = 0;
2622
            if(i == 2 || i == 3 || !s->first_slice_line)
2623
                v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2624
            if(i == 1 || i == 3 || s->mb_x)
2625
                v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2626

    
2627
            vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
2628
            if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
2629
            idct8x8_fn(s->dest[dst_idx] + off,
2630
                       i & 4 ? s->uvlinesize : s->linesize,
2631
                       s->block[i]);
2632
        } else if(val) {
2633
            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), NULL);
2634
            if(!v->ttmbf && ttmb < 8) ttmb = -1;
2635
            first_block = 0;
2636
        }
2637
    }
2638
}
2639

    
2640
/** Decode blocks of I-frame
2641
 */
2642
static void vc1_decode_i_blocks(VC1Context *v)
2643
{
2644
    int k;
2645
    MpegEncContext *s = &v->s;
2646
    int cbp, val;
2647
    uint8_t *coded_val;
2648
    int mb_pos;
2649
    vc1_idct_func idct8x8_fn;
2650

    
2651
    /* select codingmode used for VLC tables selection */
2652
    switch(v->y_ac_table_index){
2653
    case 0:
2654
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2655
        break;
2656
    case 1:
2657
        v->codingset = CS_HIGH_MOT_INTRA;
2658
        break;
2659
    case 2:
2660
        v->codingset = CS_MID_RATE_INTRA;
2661
        break;
2662
    }
2663

    
2664
    switch(v->c_ac_table_index){
2665
    case 0:
2666
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2667
        break;
2668
    case 1:
2669
        v->codingset2 = CS_HIGH_MOT_INTER;
2670
        break;
2671
    case 2:
2672
        v->codingset2 = CS_MID_RATE_INTER;
2673
        break;
2674
    }
2675

    
2676
    /* Set DC scale - y and c use the same */
2677
    s->y_dc_scale = s->y_dc_scale_table[v->pq];
2678
    s->c_dc_scale = s->c_dc_scale_table[v->pq];
2679

    
2680
    //do frame decode
2681
    s->mb_x = s->mb_y = 0;
2682
    s->mb_intra = 1;
2683
    s->first_slice_line = 1;
2684
    if(v->pq >= 9 && v->overlap) {
2685
        idct8x8_fn = v->vc1dsp.vc1_inv_trans_8x8_put_signed[!!v->rangeredfrm];
2686
    } else
2687
        idct8x8_fn = v->vc1dsp.vc1_inv_trans_8x8_put[!!v->rangeredfrm];
2688
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2689
        s->mb_x = 0;
2690
        ff_init_block_index(s);
2691
        for(; s->mb_x < s->mb_width; s->mb_x++) {
2692
            uint8_t *dst[6];
2693
            ff_update_block_index(s);
2694
            dst[0] = s->dest[0];
2695
            dst[1] = dst[0] + 8;
2696
            dst[2] = s->dest[0] + s->linesize * 8;
2697
            dst[3] = dst[2] + 8;
2698
            dst[4] = s->dest[1];
2699
            dst[5] = s->dest[2];
2700
            s->dsp.clear_blocks(s->block[0]);
2701
            mb_pos = s->mb_x + s->mb_y * s->mb_width;
2702
            s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2703
            s->current_picture.qscale_table[mb_pos] = v->pq;
2704
            s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2705
            s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2706

    
2707
            // do actual MB decoding and displaying
2708
            cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2709
            v->s.ac_pred = get_bits1(&v->s.gb);
2710

    
2711
            for(k = 0; k < 6; k++) {
2712
                val = ((cbp >> (5 - k)) & 1);
2713

    
2714
                if (k < 4) {
2715
                    int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2716
                    val = val ^ pred;
2717
                    *coded_val = val;
2718
                }
2719
                cbp |= val << (5 - k);
2720

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

    
2723
                if (k > 3 && (s->flags & CODEC_FLAG_GRAY)) continue;
2724
                idct8x8_fn(dst[k],
2725
                           k & 4 ? s->uvlinesize : s->linesize,
2726
                           s->block[k]);
2727
            }
2728

    
2729
            if(v->pq >= 9 && v->overlap) {
2730
                if(s->mb_x) {
2731
                    v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
2732
                    v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2733
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
2734
                        v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
2735
                        v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
2736
                    }
2737
                }
2738
                v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
2739
                v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2740
                if(!s->first_slice_line) {
2741
                    v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
2742
                    v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
2743
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
2744
                        v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
2745
                        v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
2746
                    }
2747
                }
2748
                v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2749
                v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2750
            }
2751
            if(v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
2752

    
2753
            if(get_bits_count(&s->gb) > v->bits) {
2754
                ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
2755
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
2756
                return;
2757
            }
2758
        }
2759
        if (!v->s.loop_filter)
2760
            ff_draw_horiz_band(s, s->mb_y * 16, 16);
2761
        else if (s->mb_y)
2762
            ff_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
2763

    
2764
        s->first_slice_line = 0;
2765
    }
2766
    if (v->s.loop_filter)
2767
        ff_draw_horiz_band(s, (s->mb_height-1)*16, 16);
2768
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2769
}
2770

    
2771
/** Decode blocks of I-frame for advanced profile
2772
 */
2773
static void vc1_decode_i_blocks_adv(VC1Context *v, int mby_start, int mby_end)
2774
{
2775
    int k;
2776
    MpegEncContext *s = &v->s;
2777
    int cbp, val;
2778
    uint8_t *coded_val;
2779
    int mb_pos;
2780
    int mquant = v->pq;
2781
    int mqdiff;
2782
    int overlap;
2783
    GetBitContext *gb = &s->gb;
2784
    vc1_idct_func idct8x8_fn;
2785

    
2786
    /* select codingmode used for VLC tables selection */
2787
    switch(v->y_ac_table_index){
2788
    case 0:
2789
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2790
        break;
2791
    case 1:
2792
        v->codingset = CS_HIGH_MOT_INTRA;
2793
        break;
2794
    case 2:
2795
        v->codingset = CS_MID_RATE_INTRA;
2796
        break;
2797
    }
2798

    
2799
    switch(v->c_ac_table_index){
2800
    case 0:
2801
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2802
        break;
2803
    case 1:
2804
        v->codingset2 = CS_HIGH_MOT_INTER;
2805
        break;
2806
    case 2:
2807
        v->codingset2 = CS_MID_RATE_INTER;
2808
        break;
2809
    }
2810

    
2811
    //do frame decode
2812
    s->mb_x = s->mb_y = 0;
2813
    s->mb_intra = 1;
2814
    s->first_slice_line = 1;
2815
    s->mb_y = mby_start;
2816
    if (mby_start) {
2817
        s->mb_x = 0;
2818
        ff_init_block_index(s);
2819
        memset(&s->coded_block[s->block_index[0]-s->b8_stride], 0,
2820
               s->b8_stride * sizeof(*s->coded_block));
2821
    }
2822
    idct8x8_fn = v->vc1dsp.vc1_inv_trans_8x8_put_signed[0];
2823
    for(; s->mb_y < mby_end; s->mb_y++) {
2824
        s->mb_x = 0;
2825
        ff_init_block_index(s);
2826
        for(;s->mb_x < s->mb_width; s->mb_x++) {
2827
            uint8_t *dst[6];
2828
            ff_update_block_index(s);
2829
            dst[0] = s->dest[0];
2830
            dst[1] = dst[0] + 8;
2831
            dst[2] = s->dest[0] + s->linesize * 8;
2832
            dst[3] = dst[2] + 8;
2833
            dst[4] = s->dest[1];
2834
            dst[5] = s->dest[2];
2835
            s->dsp.clear_blocks(s->block[0]);
2836
            mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2837
            s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2838
            s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2839
            s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2840

    
2841
            // do actual MB decoding and displaying
2842
            cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2843
            if(v->acpred_is_raw)
2844
                v->s.ac_pred = get_bits1(&v->s.gb);
2845
            else
2846
                v->s.ac_pred = v->acpred_plane[mb_pos];
2847

    
2848
            if(v->condover == CONDOVER_SELECT) {
2849
                if(v->overflg_is_raw)
2850
                    overlap = get_bits1(&v->s.gb);
2851
                else
2852
                    overlap = v->over_flags_plane[mb_pos];
2853
            } else
2854
                overlap = (v->condover == CONDOVER_ALL);
2855

    
2856
            GET_MQUANT();
2857

    
2858
            s->current_picture.qscale_table[mb_pos] = mquant;
2859
            /* Set DC scale - y and c use the same */
2860
            s->y_dc_scale = s->y_dc_scale_table[mquant];
2861
            s->c_dc_scale = s->c_dc_scale_table[mquant];
2862

    
2863
            for(k = 0; k < 6; k++) {
2864
                val = ((cbp >> (5 - k)) & 1);
2865

    
2866
                if (k < 4) {
2867
                    int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2868
                    val = val ^ pred;
2869
                    *coded_val = val;
2870
                }
2871
                cbp |= val << (5 - k);
2872

    
2873
                v->a_avail = !s->first_slice_line || (k==2 || k==3);
2874
                v->c_avail = !!s->mb_x || (k==1 || k==3);
2875

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

    
2878
                if (k > 3 && (s->flags & CODEC_FLAG_GRAY)) continue;
2879
                idct8x8_fn(dst[k],
2880
                           k & 4 ? s->uvlinesize : s->linesize,
2881
                           s->block[k]);
2882
            }
2883

    
2884
            if(overlap) {
2885
                if(s->mb_x) {
2886
                    v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
2887
                    v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2888
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
2889
                        v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
2890
                        v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
2891
                    }
2892
                }
2893
                v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
2894
                v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2895
                if(!s->first_slice_line) {
2896
                    v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
2897
                    v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
2898
                    if(!(s->flags & CODEC_FLAG_GRAY)) {
2899
                        v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
2900
                        v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
2901
                    }
2902
                }
2903
                v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2904
                v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2905
            }
2906
            if(v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
2907

    
2908
            if(get_bits_count(&s->gb) > v->bits) {
2909
                ff_er_add_slice(s, 0, mby_start, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
2910
                av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
2911
                return;
2912
            }
2913
        }
2914
        if (!v->s.loop_filter)
2915
            ff_draw_horiz_band(s, s->mb_y * 16, 16);
2916
        else if (s->mb_y)
2917
            ff_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
2918
        s->first_slice_line = 0;
2919
    }
2920
    if (v->s.loop_filter)
2921
        ff_draw_horiz_band(s, (s->mb_height-1)*16, 16);
2922
    ff_er_add_slice(s, 0, mby_start, s->mb_width - 1, mby_end - 1, (AC_END|DC_END|MV_END));
2923
}
2924

    
2925
static void vc1_decode_p_blocks(VC1Context *v, int mby_start, int mby_end)
2926
{
2927
    MpegEncContext *s = &v->s;
2928
    int apply_loop_filter;
2929

    
2930
    /* select codingmode used for VLC tables selection */
2931
    switch(v->c_ac_table_index){
2932
    case 0:
2933
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2934
        break;
2935
    case 1:
2936
        v->codingset = CS_HIGH_MOT_INTRA;
2937
        break;
2938
    case 2:
2939
        v->codingset = CS_MID_RATE_INTRA;
2940
        break;
2941
    }
2942

    
2943
    switch(v->c_ac_table_index){
2944
    case 0:
2945
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2946
        break;
2947
    case 1:
2948
        v->codingset2 = CS_HIGH_MOT_INTER;
2949
        break;
2950
    case 2:
2951
        v->codingset2 = CS_MID_RATE_INTER;
2952
        break;
2953
    }
2954

    
2955
    apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY);
2956
    s->first_slice_line = 1;
2957
    memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
2958
    for(s->mb_y = mby_start; s->mb_y < mby_end; s->mb_y++) {
2959
        s->mb_x = 0;
2960
        ff_init_block_index(s);
2961
        for(; s->mb_x < s->mb_width; s->mb_x++) {
2962
            ff_update_block_index(s);
2963

    
2964
            vc1_decode_p_mb(v);
2965
            if (s->mb_y != mby_start && apply_loop_filter)
2966
                vc1_apply_p_loop_filter(v);
2967
            if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2968
                ff_er_add_slice(s, 0, mby_start, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
2969
                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);
2970
                return;
2971
            }
2972
        }
2973
        memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0])*s->mb_stride);
2974
        memmove(v->ttblk_base, v->ttblk, sizeof(v->ttblk_base[0])*s->mb_stride);
2975
        memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
2976
        memmove(v->luma_mv_base, v->luma_mv, sizeof(v->luma_mv_base[0])*s->mb_stride);
2977
        if (s->mb_y != mby_start) ff_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
2978
        s->first_slice_line = 0;
2979
    }
2980
    if (apply_loop_filter) {
2981
        s->mb_x = 0;
2982
        ff_init_block_index(s);
2983
        for (; s->mb_x < s->mb_width; s->mb_x++) {
2984
            ff_update_block_index(s);
2985
            vc1_apply_p_loop_filter(v);
2986
        }
2987
    }
2988
    if (mby_end >= mby_start)
2989
        ff_draw_horiz_band(s, (mby_end-1) * 16, 16);
2990
    ff_er_add_slice(s, 0, mby_start, s->mb_width - 1, mby_end - 1, (AC_END|DC_END|MV_END));
2991
}
2992

    
2993
static void vc1_decode_b_blocks(VC1Context *v, int mby_start, int mby_end)
2994
{
2995
    MpegEncContext *s = &v->s;
2996

    
2997
    /* select codingmode used for VLC tables selection */
2998
    switch(v->c_ac_table_index){
2999
    case 0:
3000
        v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
3001
        break;
3002
    case 1:
3003
        v->codingset = CS_HIGH_MOT_INTRA;
3004
        break;
3005
    case 2:
3006
        v->codingset = CS_MID_RATE_INTRA;
3007
        break;
3008
    }
3009

    
3010
    switch(v->c_ac_table_index){
3011
    case 0:
3012
        v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
3013
        break;
3014
    case 1:
3015
        v->codingset2 = CS_HIGH_MOT_INTER;
3016
        break;
3017
    case 2:
3018
        v->codingset2 = CS_MID_RATE_INTER;
3019
        break;
3020
    }
3021

    
3022
    s->first_slice_line = 1;
3023
    for(s->mb_y = mby_start; s->mb_y < mby_end; s->mb_y++) {
3024
        s->mb_x = 0;
3025
        ff_init_block_index(s);
3026
        for(; s->mb_x < s->mb_width; s->mb_x++) {
3027
            ff_update_block_index(s);
3028

    
3029
            vc1_decode_b_mb(v);
3030
            if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
3031
                ff_er_add_slice(s, 0, mby_start, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
3032
                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);
3033
                return;
3034
            }
3035
            if(v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
3036
        }
3037
        if (!v->s.loop_filter)
3038
            ff_draw_horiz_band(s, s->mb_y * 16, 16);
3039
        else if (s->mb_y)
3040
            ff_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
3041
        s->first_slice_line = 0;
3042
    }
3043
    if (v->s.loop_filter)
3044
        ff_draw_horiz_band(s, (s->mb_height-1)*16, 16);
3045
    ff_er_add_slice(s, 0, mby_start, s->mb_width - 1, mby_end - 1, (AC_END|DC_END|MV_END));
3046
}
3047

    
3048
static void vc1_decode_skip_blocks(VC1Context *v)
3049
{
3050
    MpegEncContext *s = &v->s;
3051

    
3052
    ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3053
    s->first_slice_line = 1;
3054
    for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3055
        s->mb_x = 0;
3056
        ff_init_block_index(s);
3057
        ff_update_block_index(s);
3058
        memcpy(s->dest[0], s->last_picture.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
3059
        memcpy(s->dest[1], s->last_picture.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
3060
        memcpy(s->dest[2], s->last_picture.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
3061
        ff_draw_horiz_band(s, s->mb_y * 16, 16);
3062
        s->first_slice_line = 0;
3063
    }
3064
    s->pict_type = FF_P_TYPE;
3065
}
3066

    
3067
static void vc1_decode_blocks(VC1Context *v, int mby_start, int mby_end)
3068
{
3069

    
3070
    v->s.esc3_level_length = 0;
3071
    if(v->x8_type){
3072
        ff_intrax8_decode_picture(&v->x8, 2*v->pq+v->halfpq, v->pq*(!v->pquantizer) );
3073
    }else{
3074
        switch(v->s.pict_type) {
3075
        case FF_I_TYPE:
3076
            if(v->profile == PROFILE_ADVANCED)
3077
                vc1_decode_i_blocks_adv(v, mby_start, mby_end);
3078
            else
3079
                vc1_decode_i_blocks(v);
3080
            break;
3081
        case FF_P_TYPE:
3082
            if(v->p_frame_skipped)
3083
                vc1_decode_skip_blocks(v);
3084
            else
3085
                vc1_decode_p_blocks(v, mby_start, mby_end);
3086
            break;
3087
        case FF_B_TYPE:
3088
            if(v->bi_type){
3089
                if(v->profile == PROFILE_ADVANCED)
3090
                    vc1_decode_i_blocks_adv(v, mby_start, mby_end);
3091
                else
3092
                    vc1_decode_i_blocks(v);
3093
            }else
3094
                vc1_decode_b_blocks(v, mby_start, mby_end);
3095
            break;
3096
        }
3097
    }
3098
}
3099

    
3100
static inline float get_float_val(GetBitContext* gb)
3101
{
3102
    return (float)get_bits_long(gb, 30) / (1<<15) - (1<<14);
3103
}
3104

    
3105
static void vc1_sprite_parse_transform(VC1Context *v, GetBitContext* gb, float c[7])
3106
{
3107
    c[1] = c[3] = 0.0f;
3108

    
3109
    switch (get_bits(gb, 2)) {
3110
    case 0:
3111
        c[0] = 1.0f;
3112
        c[2] = get_float_val(gb);
3113
        c[4] = 1.0f;
3114
        break;
3115
    case 1:
3116
        c[0] = c[4] = get_float_val(gb);
3117
        c[2] = get_float_val(gb);
3118
        break;
3119
    case 2:
3120
        c[0] = get_float_val(gb);
3121
        c[2] = get_float_val(gb);
3122
        c[4] = get_float_val(gb);
3123
        break;
3124
    case 3:
3125
        av_log_ask_for_sample(v->s.avctx, NULL);
3126
        c[0] = get_float_val(gb);
3127
        c[1] = get_float_val(gb);
3128
        c[2] = get_float_val(gb);
3129
        c[3] = get_float_val(gb);
3130
        c[4] = get_float_val(gb);
3131
        break;
3132
    }
3133
    c[5] = get_float_val(gb);
3134
    if (get_bits1(gb))
3135
        c[6] = get_float_val(gb);
3136
    else
3137
        c[6] = 1.0f;
3138
}
3139

    
3140
static void vc1_parse_sprites(VC1Context *v, GetBitContext* gb)
3141
{
3142
    int effect_type, effect_flag, effect_pcount1, effect_pcount2, i;
3143
    float effect_params1[14], effect_params2[10];
3144

    
3145
    float coefs[2][7];
3146
    vc1_sprite_parse_transform(v, gb, coefs[0]);
3147
    av_log(v->s.avctx, AV_LOG_DEBUG, "S1:");
3148
    for (i = 0; i < 7; i++)
3149
        av_log(v->s.avctx, AV_LOG_DEBUG, " %.3f", coefs[0][i]);
3150
    av_log(v->s.avctx, AV_LOG_DEBUG, "\n");
3151

    
3152
    if (v->two_sprites) {
3153
        vc1_sprite_parse_transform(v, gb, coefs[1]);
3154
        av_log(v->s.avctx, AV_LOG_DEBUG, "S2:");
3155
        for (i = 0; i < 7; i++)
3156
            av_log(v->s.avctx, AV_LOG_DEBUG, " %.3f", coefs[1][i]);
3157
        av_log(v->s.avctx, AV_LOG_DEBUG, "\n");
3158
    }
3159
    skip_bits(gb, 2);
3160
    if (effect_type = get_bits_long(gb, 30)){
3161
        switch (effect_pcount1 = get_bits(gb, 4)) {
3162
        case 2:
3163
            effect_params1[0] = get_float_val(gb);
3164
            effect_params1[1] = get_float_val(gb);
3165
            break;
3166
        case 7:
3167
            vc1_sprite_parse_transform(v, gb, effect_params1);
3168
            break;
3169
        case 14:
3170
            vc1_sprite_parse_transform(v, gb, effect_params1);
3171
            vc1_sprite_parse_transform(v, gb, &effect_params1[7]);
3172
            break;
3173
        default:
3174
            av_log_ask_for_sample(v->s.avctx, NULL);
3175
            return;
3176
        }
3177
        if (effect_type != 13 || effect_params1[0] != coefs[0][6]) {
3178
            // effect 13 is simple alpha blending and matches the opacity above
3179
            av_log(v->s.avctx, AV_LOG_DEBUG, "Effect: %d; params: ", effect_type);
3180
            for (i = 0; i < effect_pcount1; i++)
3181
                av_log(v->s.avctx, AV_LOG_DEBUG, " %.3f", effect_params1[i]);
3182
            av_log(v->s.avctx, AV_LOG_DEBUG, "\n");
3183
        }
3184

    
3185
        effect_pcount2 = get_bits(gb, 16);
3186
        if (effect_pcount2 > 10) {
3187
            av_log(v->s.avctx, AV_LOG_ERROR, "Too many effect parameters\n");
3188
            return;
3189
        } else if (effect_pcount2) {
3190
            i = 0;
3191
            av_log(v->s.avctx, AV_LOG_DEBUG, "Effect params 2: ");
3192
            while (i < effect_pcount2){
3193
                effect_params2[i] = get_float_val(gb);
3194
                av_log(v->s.avctx, AV_LOG_DEBUG, " %.3f", effect_params2[i]);
3195
                i++;
3196
            }
3197
            av_log(v->s.avctx, AV_LOG_DEBUG, "\n");
3198
        }
3199
    }
3200
    if (effect_flag = get_bits1(gb))
3201
        av_log(v->s.avctx, AV_LOG_DEBUG, "Effect flag set\n");
3202

    
3203
    if (get_bits_count(gb) >= gb->size_in_bits +
3204
       (v->s.avctx->codec_id == CODEC_ID_WMV3 ? 64 : 0))
3205
        av_log(v->s.avctx, AV_LOG_ERROR, "Buffer overrun\n");
3206
    if (get_bits_count(gb) < gb->size_in_bits - 8)
3207
        av_log(v->s.avctx, AV_LOG_WARNING, "Buffer not fully read\n");
3208
}
3209

    
3210
/** Initialize a VC1/WMV3 decoder
3211
 * @todo TODO: Handle VC-1 IDUs (Transport level?)
3212
 * @todo TODO: Decypher remaining bits in extra_data
3213
 */
3214
static av_cold int vc1_decode_init(AVCodecContext *avctx)
3215
{
3216
    VC1Context *v = avctx->priv_data;
3217
    MpegEncContext *s = &v->s;
3218
    GetBitContext gb;
3219
    int i;
3220

    
3221
    if (!avctx->extradata_size || !avctx->extradata) return -1;
3222
    if (!(avctx->flags & CODEC_FLAG_GRAY))
3223
        avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
3224
    else
3225
        avctx->pix_fmt = PIX_FMT_GRAY8;
3226
    avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
3227
    v->s.avctx = avctx;
3228
    avctx->flags |= CODEC_FLAG_EMU_EDGE;
3229
    v->s.flags |= CODEC_FLAG_EMU_EDGE;
3230

    
3231
    if(avctx->idct_algo==FF_IDCT_AUTO){
3232
        avctx->idct_algo=FF_IDCT_WMV2;
3233
    }
3234

    
3235
    if(ff_msmpeg4_decode_init(avctx) < 0)
3236
        return -1;
3237
    if (vc1_init_common(v) < 0) return -1;
3238
    ff_vc1dsp_init(&v->vc1dsp);
3239
    for (i = 0; i < 64;  i++) {
3240
#define transpose(x) ((x>>3) | ((x&7)<<3))
3241
        v->zz_8x8[0][i] = transpose(wmv1_scantable[0][i]);
3242
        v->zz_8x8[1][i] = transpose(wmv1_scantable[1][i]);
3243
        v->zz_8x8[2][i] = transpose(wmv1_scantable[2][i]);
3244
        v->zz_8x8[3][i] = transpose(wmv1_scantable[3][i]);
3245
    }
3246

    
3247
    avctx->coded_width = avctx->width;
3248
    avctx->coded_height = avctx->height;
3249
    if (avctx->codec_id == CODEC_ID_WMV3)
3250
    {
3251
        int count = 0;
3252

    
3253
        // looks like WMV3 has a sequence header stored in the extradata
3254
        // advanced sequence header may be before the first frame
3255
        // the last byte of the extradata is a version number, 1 for the
3256
        // samples we can decode
3257

    
3258
        init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
3259

    
3260
        if (vc1_decode_sequence_header(avctx, v, &gb) < 0)
3261
          return -1;
3262

    
3263
        count = avctx->extradata_size*8 - get_bits_count(&gb);
3264
        if (count>0)
3265
        {
3266
            av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
3267
                   count, get_bits(&gb, count));
3268
        }
3269
        else if (count < 0)
3270
        {
3271
            av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
3272
        }
3273
    } else { // VC1/WVC1/WVP2
3274
        const uint8_t *start = avctx->extradata;
3275
        uint8_t *end = avctx->extradata + avctx->extradata_size;
3276
        const uint8_t *next;
3277
        int size, buf2_size;
3278
        uint8_t *buf2 = NULL;
3279
        int seq_initialized = 0, ep_initialized = 0;
3280

    
3281
        if(avctx->extradata_size < 16) {
3282
            av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
3283
            return -1;
3284
        }
3285

    
3286
        buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
3287
        start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
3288
        next = start;
3289
        for(; next < end; start = next){
3290
            next = find_next_marker(start + 4, end);
3291
            size = next - start - 4;
3292
            if(size <= 0) continue;
3293
            buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
3294
            init_get_bits(&gb, buf2, buf2_size * 8);
3295
            switch(AV_RB32(start)){
3296
            case VC1_CODE_SEQHDR:
3297
                if(vc1_decode_sequence_header(avctx, v, &gb) < 0){
3298
                    av_free(buf2);
3299
                    return -1;
3300
                }
3301
                seq_initialized = 1;
3302
                break;
3303
            case VC1_CODE_ENTRYPOINT:
3304
                if(vc1_decode_entry_point(avctx, v, &gb) < 0){
3305
                    av_free(buf2);
3306
                    return -1;
3307
                }
3308
                ep_initialized = 1;
3309
                break;
3310
            }
3311
        }
3312
        av_free(buf2);
3313
        if(!seq_initialized || !ep_initialized){
3314
            av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
3315
            return -1;
3316
        }
3317
        v->res_sprite = (avctx->codec_tag == MKTAG('W','V','P','2'));
3318
    }
3319
    avctx->profile = v->profile;
3320
    if (v->profile == PROFILE_ADVANCED)
3321
        avctx->level = v->level;
3322

    
3323
    avctx->has_b_frames= !!(avctx->max_b_frames);
3324
    s->low_delay = !avctx->has_b_frames;
3325

    
3326
    s->mb_width = (avctx->coded_width+15)>>4;
3327
    s->mb_height = (avctx->coded_height+15)>>4;
3328

    
3329
    /* Allocate mb bitplanes */
3330
    v->mv_type_mb_plane = av_malloc(s->mb_stride * s->mb_height);
3331
    v->direct_mb_plane = av_malloc(s->mb_stride * s->mb_height);
3332
    v->acpred_plane = av_malloc(s->mb_stride * s->mb_height);
3333
    v->over_flags_plane = av_malloc(s->mb_stride * s->mb_height);
3334

    
3335
    v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
3336
    v->cbp = v->cbp_base + s->mb_stride;
3337
    v->ttblk_base = av_malloc(sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
3338
    v->ttblk = v->ttblk_base + s->mb_stride;
3339
    v->is_intra_base = av_malloc(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
3340
    v->is_intra = v->is_intra_base + s->mb_stride;
3341
    v->luma_mv_base = av_malloc(sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
3342
    v->luma_mv = v->luma_mv_base + s->mb_stride;
3343

    
3344
    /* allocate block type info in that way so it could be used with s->block_index[] */
3345
    v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
3346
    v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
3347
    v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
3348
    v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
3349

    
3350
    /* Init coded blocks info */
3351
    if (v->profile == PROFILE_ADVANCED)
3352
    {
3353
//        if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
3354
//            return -1;
3355
//        if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
3356
//            return -1;
3357
    }
3358

    
3359
    ff_intrax8_common_init(&v->x8,s);
3360
    return 0;
3361
}
3362

    
3363

    
3364
/** Decode a VC1/WMV3 frame
3365
 * @todo TODO: Handle VC-1 IDUs (Transport level?)
3366
 */
3367
static int vc1_decode_frame(AVCodecContext *avctx,
3368
                            void *data, int *data_size,
3369
                            AVPacket *avpkt)
3370
{
3371
    const uint8_t *buf = avpkt->data;
3372
    int buf_size = avpkt->size, n_slices = 0, i;
3373
    VC1Context *v = avctx->priv_data;
3374
    MpegEncContext *s = &v->s;
3375
    AVFrame *pict = data;
3376
    uint8_t *buf2 = NULL;
3377
    const uint8_t *buf_start = buf;
3378
    struct {
3379
        uint8_t *buf;
3380
        GetBitContext gb;
3381
        int mby_start;
3382
    } *slices = NULL;
3383

    
3384
    /* no supplementary picture */
3385
    if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {
3386
        /* special case for last picture */
3387
        if (s->low_delay==0 && s->next_picture_ptr) {
3388
            *pict= *(AVFrame*)s->next_picture_ptr;
3389
            s->next_picture_ptr= NULL;
3390

    
3391
            *data_size = sizeof(AVFrame);
3392
        }
3393

    
3394
        return 0;
3395
    }
3396

    
3397
    /* We need to set current_picture_ptr before reading the header,
3398
     * otherwise we cannot store anything in there. */
3399
    if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
3400
        int i= ff_find_unused_picture(s, 0);
3401
        s->current_picture_ptr= &s->picture[i];
3402
    }
3403

    
3404
    if (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){
3405
        if (v->profile < PROFILE_ADVANCED)
3406
            avctx->pix_fmt = PIX_FMT_VDPAU_WMV3;
3407
        else
3408
            avctx->pix_fmt = PIX_FMT_VDPAU_VC1;
3409
    }
3410

    
3411
    //for advanced profile we may need to parse and unescape data
3412
    if (avctx->codec_id == CODEC_ID_VC1) {
3413
        int buf_size2 = 0;
3414
        buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
3415

    
3416
        if(IS_MARKER(AV_RB32(buf))){ /* frame starts with marker and needs to be parsed */
3417
            const uint8_t *start, *end, *next;
3418
            int size;
3419

    
3420
            next = buf;
3421
            for(start = buf, end = buf + buf_size; next < end; start = next){
3422
                next = find_next_marker(start + 4, end);
3423
                size = next - start - 4;
3424
                if(size <= 0) continue;
3425
                switch(AV_RB32(start)){
3426
                case VC1_CODE_FRAME:
3427
                    if (avctx->hwaccel ||
3428
                        s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
3429
                        buf_start = start;
3430
                    buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
3431
                    break;
3432
                case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
3433
                    buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
3434
                    init_get_bits(&s->gb, buf2, buf_size2*8);
3435
                    vc1_decode_entry_point(avctx, v, &s->gb);
3436
                    break;
3437
                case VC1_CODE_SLICE: {
3438
                    int buf_size3;
3439
                    slices = av_realloc(slices, sizeof(*slices) * (n_slices+1));
3440
                    if (!slices) goto err;
3441
                    slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
3442
                    if (!slices[n_slices].buf) goto err;
3443
                    buf_size3 = vc1_unescape_buffer(start + 4, size,
3444
                                                    slices[n_slices].buf);
3445
                    init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
3446
                                  buf_size3 << 3);
3447
                    slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9);
3448
                    n_slices++;
3449
                    break;
3450
                }
3451
                }
3452
            }
3453
        }else if(v->interlace && ((buf[0] & 0xC0) == 0xC0)){ /* WVC1 interlaced stores both fields divided by marker */
3454
            const uint8_t *divider;
3455

    
3456
            divider = find_next_marker(buf, buf + buf_size);
3457
            if((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD){
3458
                av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
3459
                goto err;
3460
            }
3461

    
3462
            buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
3463
            // TODO
3464
            if(!v->warn_interlaced++)
3465
                av_log(v->s.avctx, AV_LOG_ERROR, "Interlaced WVC1 support is not implemented\n");
3466
            goto err;
3467
        }else{
3468
            buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
3469
        }
3470
        init_get_bits(&s->gb, buf2, buf_size2*8);
3471
    } else
3472
        init_get_bits(&s->gb, buf, buf_size*8);
3473

    
3474
    if (v->res_sprite) {
3475
        v->new_sprite = !get_bits1(&s->gb);
3476
        v->two_sprites = get_bits1(&s->gb);
3477
        if (!v->new_sprite)
3478
            goto end;
3479
    }
3480

    
3481
    // do parse frame header
3482
    if(v->profile < PROFILE_ADVANCED) {
3483
        if(vc1_parse_frame_header(v, &s->gb) == -1) {
3484
            goto err;
3485
        }
3486
    } else {
3487
        if(vc1_parse_frame_header_adv(v, &s->gb) == -1) {
3488
            goto err;
3489
        }
3490
    }
3491

    
3492
    if (v->res_sprite && s->pict_type!=FF_I_TYPE) {
3493
        av_log(v->s.avctx, AV_LOG_WARNING, "Sprite decoder: expected I-frame\n");
3494
    }
3495

    
3496
    // for skipping the frame
3497
    s->current_picture.pict_type= s->pict_type;
3498
    s->current_picture.key_frame= s->pict_type == FF_I_TYPE;
3499

    
3500
    /* skip B-frames if we don't have reference frames */
3501
    if(s->last_picture_ptr==NULL && (s->pict_type==FF_B_TYPE || s->dropable)){
3502
        goto err;
3503
    }
3504
#if FF_API_HURRY_UP
3505
    /* skip b frames if we are in a hurry */
3506
    if(avctx->hurry_up && s->pict_type==FF_B_TYPE) return -1;//buf_size;
3507
#endif
3508
    if(   (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==FF_B_TYPE)
3509
       || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=FF_I_TYPE)
3510
       ||  avctx->skip_frame >= AVDISCARD_ALL) {
3511
        goto end;
3512
    }
3513
#if FF_API_HURRY_UP
3514
    /* skip everything if we are in a hurry>=5 */
3515
    if(avctx->hurry_up>=5) {
3516
        goto err;
3517
    }
3518
#endif
3519

    
3520
    if(s->next_p_frame_damaged){
3521
        if(s->pict_type==FF_B_TYPE)
3522
            goto end;
3523
        else
3524
            s->next_p_frame_damaged=0;
3525
    }
3526

    
3527
    if(MPV_frame_start(s, avctx) < 0) {
3528
        goto err;
3529
    }
3530

    
3531
    s->me.qpel_put= s->dsp.put_qpel_pixels_tab;
3532
    s->me.qpel_avg= s->dsp.avg_qpel_pixels_tab;
3533

    
3534
    if ((CONFIG_VC1_VDPAU_DECODER)
3535
        &&s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
3536
        ff_vdpau_vc1_decode_picture(s, buf_start, (buf + buf_size) - buf_start);
3537
    else if (avctx->hwaccel) {
3538
        if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
3539
            goto err;
3540
        if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
3541
            goto err;
3542
        if (avctx->hwaccel->end_frame(avctx) < 0)
3543
            goto err;
3544
    } else {
3545
        ff_er_frame_start(s);
3546

    
3547
        v->bits = buf_size * 8;
3548
        for (i = 0; i <= n_slices; i++) {
3549
            if (i && get_bits1(&s->gb))
3550
                vc1_parse_frame_header_adv(v, &s->gb);
3551
            vc1_decode_blocks(v, i == 0 ? 0 : FFMAX(0, slices[i-1].mby_start),
3552
                i == n_slices ? s->mb_height : FFMIN(s->mb_height, slices[i].mby_start));
3553
            if (i != n_slices) s->gb = slices[i].gb;
3554
        }
3555
//av_log(s->avctx, AV_LOG_INFO, "Consumed %i/%i bits\n", get_bits_count(&s->gb), s->gb.size_in_bits);
3556
//  if(get_bits_count(&s->gb) > buf_size * 8)
3557
//      return -1;
3558
        ff_er_frame_end(s);
3559
    }
3560

    
3561
    MPV_frame_end(s);
3562

    
3563
assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
3564
assert(s->current_picture.pict_type == s->pict_type);
3565
    if (s->pict_type == FF_B_TYPE || s->low_delay) {
3566
        *pict= *(AVFrame*)s->current_picture_ptr;
3567
    } else if (s->last_picture_ptr != NULL) {
3568
        *pict= *(AVFrame*)s->last_picture_ptr;
3569
    }
3570

    
3571
    if(s->last_picture_ptr || s->low_delay){
3572
        *data_size = sizeof(AVFrame);
3573
        ff_print_debug_info(s, pict);
3574
    }
3575

    
3576
end:
3577
    if (v->res_sprite)
3578
        vc1_parse_sprites(v, &s->gb);
3579
    av_free(buf2);
3580
    for (i = 0; i < n_slices; i++)
3581
        av_free(slices[i].buf);
3582
    av_free(slices);
3583
    return buf_size;
3584

    
3585
err:
3586
    av_free(buf2);
3587
    for (i = 0; i < n_slices; i++)
3588
        av_free(slices[i].buf);
3589
    av_free(slices);
3590
    return -1;
3591
}
3592

    
3593

    
3594
/** Close a VC1/WMV3 decoder
3595
 * @warning Initial try at using MpegEncContext stuff
3596
 */
3597
static av_cold int vc1_decode_end(AVCodecContext *avctx)
3598
{
3599
    VC1Context *v = avctx->priv_data;
3600

    
3601
    av_freep(&v->hrd_rate);
3602
    av_freep(&v->hrd_buffer);
3603
    MPV_common_end(&v->s);
3604
    av_freep(&v->mv_type_mb_plane);
3605
    av_freep(&v->direct_mb_plane);
3606
    av_freep(&v->acpred_plane);
3607
    av_freep(&v->over_flags_plane);
3608
    av_freep(&v->mb_type_base);
3609
    av_freep(&v->cbp_base);
3610
    av_freep(&v->ttblk_base);
3611
    av_freep(&v->is_intra_base); // FIXME use v->mb_type[]
3612
    av_freep(&v->luma_mv_base);
3613
    ff_intrax8_common_end(&v->x8);
3614
    return 0;
3615
}
3616

    
3617
static const AVProfile profiles[] = {
3618
    { FF_PROFILE_VC1_SIMPLE,   "Simple"   },
3619
    { FF_PROFILE_VC1_MAIN,     "Main"     },
3620
    { FF_PROFILE_VC1_COMPLEX,  "Complex"  },
3621
    { FF_PROFILE_VC1_ADVANCED, "Advanced" },
3622
    { FF_PROFILE_UNKNOWN },
3623
};
3624

    
3625
AVCodec ff_vc1_decoder = {
3626
    "vc1",
3627
    AVMEDIA_TYPE_VIDEO,
3628
    CODEC_ID_VC1,
3629
    sizeof(VC1Context),
3630
    vc1_decode_init,
3631
    NULL,
3632
    vc1_decode_end,
3633
    vc1_decode_frame,
3634
    CODEC_CAP_DR1 | CODEC_CAP_DELAY,
3635
    NULL,
3636
    .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
3637
    .pix_fmts = ff_hwaccel_pixfmt_list_420,
3638
    .profiles = NULL_IF_CONFIG_SMALL(profiles)
3639
};
3640

    
3641
#if CONFIG_WMV3_DECODER
3642
AVCodec ff_wmv3_decoder = {
3643
    "wmv3",
3644
    AVMEDIA_TYPE_VIDEO,
3645
    CODEC_ID_WMV3,
3646
    sizeof(VC1Context),
3647
    vc1_decode_init,
3648
    NULL,
3649
    vc1_decode_end,
3650
    vc1_decode_frame,
3651
    CODEC_CAP_DR1 | CODEC_CAP_DELAY,
3652
    NULL,
3653
    .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
3654
    .pix_fmts = ff_hwaccel_pixfmt_list_420,
3655
    .profiles = NULL_IF_CONFIG_SMALL(profiles)
3656
};
3657
#endif
3658

    
3659
#if CONFIG_WMV3_VDPAU_DECODER
3660
AVCodec ff_wmv3_vdpau_decoder = {
3661
    "wmv3_vdpau",
3662
    AVMEDIA_TYPE_VIDEO,
3663
    CODEC_ID_WMV3,
3664
    sizeof(VC1Context),
3665
    vc1_decode_init,
3666
    NULL,
3667
    vc1_decode_end,
3668
    vc1_decode_frame,
3669
    CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
3670
    NULL,
3671
    .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
3672
    .pix_fmts = (const enum PixelFormat[]){PIX_FMT_VDPAU_WMV3, PIX_FMT_NONE},
3673
    .profiles = NULL_IF_CONFIG_SMALL(profiles)
3674
};
3675
#endif
3676

    
3677
#if CONFIG_VC1_VDPAU_DECODER
3678
AVCodec ff_vc1_vdpau_decoder = {
3679
    "vc1_vdpau",
3680
    AVMEDIA_TYPE_VIDEO,
3681
    CODEC_ID_VC1,
3682
    sizeof(VC1Context),
3683
    vc1_decode_init,
3684
    NULL,
3685
    vc1_decode_end,
3686
    vc1_decode_frame,
3687
    CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
3688
    NULL,
3689
    .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
3690
    .pix_fmts = (const enum PixelFormat[]){PIX_FMT_VDPAU_VC1, PIX_FMT_NONE},
3691
    .profiles = NULL_IF_CONFIG_SMALL(profiles)
3692
};
3693
#endif