Browse code

Merge remote-tracking branch 'qatar/master'

* qatar/master: (34 commits)
h264: reset h->ref_count in case of errors in ff_h264_decode_ref_pic_list_reordering()
error_resilience: fix the check for missing references in ff_er_frame_end() for H264
4xm: prevent NULL dereference with invalid huffman table
4xmdemux: prevent use of uninitialized memory
4xm: clear FF_INPUT_BUFFER_PADDING_SIZE bytes in temporary buffers
ptx: check for out of bound reads
tiffdec: fix out of bound reads/writes
eacmv: check for out of bound reads
eacmv: fix potential pointer arithmetic overflows
adpcm: fix out of bound reads due to integer overflow
anm: prevent infinite loop
avsdemux: check for out of bound writes
avs: check for out of bound reads
avsdemux: check for corrupted data
AVOptions: refactor set_number/write_number
AVOptions: cosmetics, rename static av_set_number2() to write_number().
AVOptions: cosmetics, move and rename static av_set_number().
AVOptions: split av_set_string3 into opt type-specific functions
avidec: fix signed overflow in avi_sync()
mxfdec: Fix some buffer overreads caused by the misuse of AVPacket related functions.
...

Conflicts:
Changelog
configure
libavcodec/ptx.c
libavcodec/ra144.c
libavcodec/vaapi_vc1.c
libavcodec/vc1.c
libavcodec/version.h
libavformat/4xm.c
libavformat/avidec.c

Merged-by: Michael Niedermayer <michaelni@gmx.at>

Michael Niedermayer authored on 2011/10/11 09:39:50
Showing 18 changed files
... ...
@@ -62,6 +62,7 @@ easier to use. The changes are:
62 62
 - CELT in Ogg demuxing
63 63
 - G.723.1 demuxer and decoder
64 64
 - libmodplug support (--enable-libmodplug)
65
+- VC-1 interlaced decoding
65 66
 
66 67
 
67 68
 version 0.8:
... ...
@@ -3163,6 +3163,7 @@ check_cflags -Wtype-limits
3163 3163
 check_cflags -Wundef
3164 3164
 check_cflags -Wmissing-prototypes
3165 3165
 check_cflags -Wno-pointer-to-int-cast
3166
+check_cflags -Wstrict-prototypes
3166 3167
 enabled extra_warnings && check_cflags -Winline
3167 3168
 
3168 3169
 # add some linker flags
... ...
@@ -60,7 +60,6 @@ static int ptx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
60 60
 
61 61
     avctx->pix_fmt = PIX_FMT_RGB555;
62 62
 
63
-
64 63
     if (buf_end - buf < offset)
65 64
         return AVERROR_INVALIDDATA;
66 65
     if (offset != 0x2c)
... ...
@@ -1544,22 +1544,22 @@ void ff_copy_and_dup(int16_t *target, const int16_t *source, int offset)
1544 1544
 int ff_eval_refl(int *refl, const int16_t *coefs, AVCodecContext *avctx)
1545 1545
 {
1546 1546
     int b, i, j;
1547
-    int buffer1[10];
1548
-    int buffer2[10];
1547
+    int buffer1[LPC_ORDER];
1548
+    int buffer2[LPC_ORDER];
1549 1549
     int *bp1 = buffer1;
1550 1550
     int *bp2 = buffer2;
1551 1551
 
1552
-    for (i=0; i < 10; i++)
1552
+    for (i=0; i < LPC_ORDER; i++)
1553 1553
         buffer2[i] = coefs[i];
1554 1554
 
1555
-    refl[9] = bp2[9];
1555
+    refl[LPC_ORDER-1] = bp2[LPC_ORDER-1];
1556 1556
 
1557
-    if ((unsigned) bp2[9] + 0x1000 > 0x1fff) {
1557
+    if ((unsigned) bp2[LPC_ORDER-1] + 0x1000 > 0x1fff) {
1558 1558
         av_log(avctx, AV_LOG_ERROR, "Overflow. Broken sample?\n");
1559 1559
         return 1;
1560 1560
     }
1561 1561
 
1562
-    for (i=8; i >= 0; i--) {
1562
+    for (i = LPC_ORDER-2; i >= 0; i--) {
1563 1563
         b = 0x1000-((bp2[i+1] * bp2[i+1]) >> 12);
1564 1564
 
1565 1565
         if (!b)
... ...
@@ -1584,12 +1584,12 @@ int ff_eval_refl(int *refl, const int16_t *coefs, AVCodecContext *avctx)
1584 1584
  */
1585 1585
 void ff_eval_coefs(int *coefs, const int *refl)
1586 1586
 {
1587
-    int buffer[10];
1587
+    int buffer[LPC_ORDER];
1588 1588
     int *b1 = buffer;
1589 1589
     int *b2 = coefs;
1590 1590
     int i, j;
1591 1591
 
1592
-    for (i=0; i < 10; i++) {
1592
+    for (i=0; i < LPC_ORDER; i++) {
1593 1593
         b1[i] = refl[i] << 4;
1594 1594
 
1595 1595
         for (j=0; j < i; j++)
... ...
@@ -1598,7 +1598,7 @@ void ff_eval_coefs(int *coefs, const int *refl)
1598 1598
         FFSWAP(int *, b1, b2);
1599 1599
     }
1600 1600
 
1601
-    for (i=0; i < 10; i++)
1601
+    for (i=0; i < LPC_ORDER; i++)
1602 1602
         coefs[i] >>= 4;
1603 1603
 }
1604 1604
 
... ...
@@ -1606,7 +1606,7 @@ void ff_int_to_int16(int16_t *out, const int *inp)
1606 1606
 {
1607 1607
     int i;
1608 1608
 
1609
-    for (i=0; i < 10; i++)
1609
+    for (i = 0; i < LPC_ORDER; i++)
1610 1610
         *out++ = *inp++;
1611 1611
 }
1612 1612
 
... ...
@@ -1629,9 +1629,9 @@ unsigned int ff_rms(const int *data)
1629 1629
 {
1630 1630
     int i;
1631 1631
     unsigned int res = 0x10000;
1632
-    int b = 10;
1632
+    int b = LPC_ORDER;
1633 1633
 
1634
-    for (i=0; i < 10; i++) {
1634
+    for (i = 0; i < LPC_ORDER; i++) {
1635 1635
         res = (((0x1000000 - data[i]*data[i]) >> 12) * res) >> 12;
1636 1636
 
1637 1637
         if (res == 0)
... ...
@@ -1648,13 +1648,13 @@ unsigned int ff_rms(const int *data)
1648 1648
 
1649 1649
 int ff_interp(RA144Context *ractx, int16_t *out, int a, int copyold, int energy)
1650 1650
 {
1651
-    int work[10];
1651
+    int work[LPC_ORDER];
1652 1652
     int b = NBLOCKS - a;
1653 1653
     int i;
1654 1654
 
1655 1655
     // Interpolate block coefficients from the this frame's forth block and
1656 1656
     // last frame's forth block.
1657
-    for (i=0; i<10; i++)
1657
+    for (i = 0; i < LPC_ORDER; i++)
1658 1658
         out[i] = (a * ractx->lpc_coef[0][i] + b * ractx->lpc_coef[1][i])>> 2;
1659 1659
 
1660 1660
     if (ff_eval_refl(work, out, ractx->avctx)) {
... ...
@@ -1690,7 +1690,7 @@ void ff_subblock_synthesis(RA144Context *ractx, const uint16_t *lpc_coefs,
1690 1690
                            int cba_idx, int cb1_idx, int cb2_idx,
1691 1691
                            int gval, int gain)
1692 1692
 {
1693
-    uint16_t buffer_a[40];
1693
+    uint16_t buffer_a[BLOCKSIZE];
1694 1694
     uint16_t *block;
1695 1695
     int m[3];
1696 1696
 
... ...
@@ -1711,10 +1711,10 @@ void ff_subblock_synthesis(RA144Context *ractx, const uint16_t *lpc_coefs,
1711 1711
     ff_add_wav(block, gain, cba_idx, m, cba_idx? buffer_a: NULL,
1712 1712
                ff_cb1_vects[cb1_idx], ff_cb2_vects[cb2_idx]);
1713 1713
 
1714
-    memcpy(ractx->curr_sblock, ractx->curr_sblock + 40,
1715
-           10*sizeof(*ractx->curr_sblock));
1714
+    memcpy(ractx->curr_sblock, ractx->curr_sblock + BLOCKSIZE,
1715
+           LPC_ORDER*sizeof(*ractx->curr_sblock));
1716 1716
 
1717
-    if (ff_celp_lp_synthesis_filter(ractx->curr_sblock + 10, lpc_coefs,
1718
-                                    block, BLOCKSIZE, 10, 1, 0, 0xfff))
1719
-        memset(ractx->curr_sblock, 0, 50*sizeof(*ractx->curr_sblock));
1717
+    if (ff_celp_lp_synthesis_filter(ractx->curr_sblock + LPC_ORDER, lpc_coefs,
1718
+                                    block, BLOCKSIZE, LPC_ORDER, 1, 0, 0xfff))
1719
+        memset(ractx->curr_sblock, 0, (LPC_ORDER+BLOCKSIZE)*sizeof(*ractx->curr_sblock));
1720 1720
 }
... ...
@@ -59,29 +59,33 @@ static int ra144_decode_frame(AVCodecContext * avctx, void *vdata,
59 59
 {
60 60
     const uint8_t *buf = avpkt->data;
61 61
     int buf_size = avpkt->size;
62
-    static const uint8_t sizes[10] = {6, 5, 5, 4, 4, 3, 3, 3, 3, 2};
63
-    unsigned int refl_rms[4];    // RMS of the reflection coefficients
64
-    uint16_t block_coefs[4][10]; // LPC coefficients of each sub-block
65
-    unsigned int lpc_refl[10];   // LPC reflection coefficients of the frame
62
+    static const uint8_t sizes[LPC_ORDER] = {6, 5, 5, 4, 4, 3, 3, 3, 3, 2};
63
+    unsigned int refl_rms[NBLOCKS];           // RMS of the reflection coefficients
64
+    uint16_t block_coefs[NBLOCKS][LPC_ORDER]; // LPC coefficients of each sub-block
65
+    unsigned int lpc_refl[LPC_ORDER];         // LPC reflection coefficients of the frame
66 66
     int i, j;
67
+    int out_size;
67 68
     int16_t *data = vdata;
68 69
     unsigned int energy;
69 70
 
70 71
     RA144Context *ractx = avctx->priv_data;
71 72
     GetBitContext gb;
72 73
 
73
-    if (*data_size < 2*160)
74
-        return -1;
74
+    out_size = NBLOCKS * BLOCKSIZE * av_get_bytes_per_sample(avctx->sample_fmt);
75
+    if (*data_size < out_size) {
76
+        av_log(avctx, AV_LOG_ERROR, "Output buffer is too small\n");
77
+        return AVERROR(EINVAL);
78
+    }
75 79
 
76
-    if(buf_size < 20) {
80
+    if(buf_size < FRAMESIZE) {
77 81
         av_log(avctx, AV_LOG_ERROR,
78 82
                "Frame too small (%d bytes). Truncated file?\n", buf_size);
79 83
         *data_size = 0;
80 84
         return buf_size;
81 85
     }
82
-    init_get_bits(&gb, buf, 20 * 8);
86
+    init_get_bits(&gb, buf, FRAMESIZE * 8);
83 87
 
84
-    for (i=0; i<10; i++)
88
+    for (i = 0; i < LPC_ORDER; i++)
85 89
         lpc_refl[i] = ff_lpc_refl_cb[i][get_bits(&gb, sizes[i])];
86 90
 
87 91
     ff_eval_coefs(ractx->lpc_coef[0], lpc_refl);
... ...
@@ -98,7 +102,7 @@ static int ra144_decode_frame(AVCodecContext * avctx, void *vdata,
98 98
 
99 99
     ff_int_to_int16(block_coefs[3], ractx->lpc_coef[0]);
100 100
 
101
-    for (i=0; i < 4; i++) {
101
+    for (i=0; i < NBLOCKS; i++) {
102 102
         do_output_subblock(ractx, block_coefs[i], refl_rms[i], &gb);
103 103
 
104 104
         for (j=0; j < BLOCKSIZE; j++)
... ...
@@ -110,8 +114,8 @@ static int ra144_decode_frame(AVCodecContext * avctx, void *vdata,
110 110
 
111 111
     FFSWAP(unsigned int *, ractx->lpc_coef[0], ractx->lpc_coef[1]);
112 112
 
113
-    *data_size = 2*160;
114
-    return 20;
113
+    *data_size = out_size;
114
+    return FRAMESIZE;
115 115
 }
116 116
 
117 117
 AVCodec ff_ra_144_decoder = {
... ...
@@ -31,6 +31,9 @@
31 31
 #define MAX_BACKWARD_FILTER_LEN    40
32 32
 #define MAX_BACKWARD_FILTER_NONREC 35
33 33
 
34
+#define RA288_BLOCK_SIZE        5
35
+#define RA288_BLOCKS_PER_FRAME 32
36
+
34 37
 typedef struct {
35 38
     float sp_lpc[36];      ///< LPC coefficients for speech data (spec: A)
36 39
     float gain_lpc[10];    ///< LPC coefficients for gain        (spec: GB)
... ...
@@ -165,7 +168,7 @@ static int ra288_decode_frame(AVCodecContext * avctx, void *data,
165 165
     const uint8_t *buf = avpkt->data;
166 166
     int buf_size = avpkt->size;
167 167
     float *out = data;
168
-    int i, j;
168
+    int i, j, out_size;
169 169
     RA288Context *ractx = avctx->priv_data;
170 170
     GetBitContext gb;
171 171
 
... ...
@@ -176,18 +179,22 @@ static int ra288_decode_frame(AVCodecContext * avctx, void *data,
176 176
         return 0;
177 177
     }
178 178
 
179
-    if (*data_size < 32*5*4)
180
-        return -1;
179
+    out_size = RA288_BLOCK_SIZE * RA288_BLOCKS_PER_FRAME *
180
+               av_get_bytes_per_sample(avctx->sample_fmt);
181
+    if (*data_size < out_size) {
182
+        av_log(avctx, AV_LOG_ERROR, "Output buffer is too small\n");
183
+        return AVERROR(EINVAL);
184
+    }
181 185
 
182 186
     init_get_bits(&gb, buf, avctx->block_align * 8);
183 187
 
184
-    for (i=0; i < 32; i++) {
188
+    for (i=0; i < RA288_BLOCKS_PER_FRAME; i++) {
185 189
         float gain = amptable[get_bits(&gb, 3)];
186 190
         int cb_coef = get_bits(&gb, 6 + (i&1));
187 191
 
188 192
         decode(ractx, gain, cb_coef);
189 193
 
190
-        for (j=0; j < 5; j++)
194
+        for (j=0; j < RA288_BLOCK_SIZE; j++)
191 195
             *(out++) = ractx->sp_hist[70 + 36 + j];
192 196
 
193 197
         if ((i & 7) == 3) {
... ...
@@ -199,7 +206,7 @@ static int ra288_decode_frame(AVCodecContext * avctx, void *data,
199 199
         }
200 200
     }
201 201
 
202
-    *data_size = (char *)out - (char *)data;
202
+    *data_size = out_size;
203 203
     return avctx->block_align;
204 204
 }
205 205
 
... ...
@@ -1,5 +1,6 @@
1 1
 /*
2 2
  * VC-1 and WMV3 decoder common code
3
+ * Copyright (c) 2011 Mashiat Sarker Shakkhar
3 4
  * Copyright (c) 2006-2007 Konstantin Shishkov
4 5
  * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
5 6
  *
... ...
@@ -25,6 +26,7 @@
25 25
  * VC-1 and WMV3 decoder common code
26 26
  *
27 27
  */
28
+
28 29
 #include "internal.h"
29 30
 #include "dsputil.h"
30 31
 #include "avcodec.h"
... ...
@@ -66,14 +68,16 @@ enum Imode {
66 66
  * @param[in] height Height of this buffer
67 67
  * @param[in] stride of this buffer
68 68
  */
69
-static void decode_rowskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
69
+static void decode_rowskip(uint8_t* plane, int width, int height, int stride,
70
+                           GetBitContext *gb)
71
+{
70 72
     int x, y;
71 73
 
72
-    for (y=0; y<height; y++){
74
+    for (y = 0; y < height; y++) {
73 75
         if (!get_bits1(gb)) //rowskip
74 76
             memset(plane, 0, width);
75 77
         else
76
-            for (x=0; x<width; x++)
78
+            for (x = 0; x < width; x++)
77 79
                 plane[x] = get_bits1(gb);
78 80
         plane += stride;
79 81
     }
... ...
@@ -86,15 +90,17 @@ static void decode_rowskip(uint8_t* plane, int width, int height, int stride, Ge
86 86
  * @param[in] stride of this buffer
87 87
  * @todo FIXME: Optimize
88 88
  */
89
-static void decode_colskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
89
+static void decode_colskip(uint8_t* plane, int width, int height, int stride,
90
+                           GetBitContext *gb)
91
+{
90 92
     int x, y;
91 93
 
92
-    for (x=0; x<width; x++){
94
+    for (x = 0; x < width; x++) {
93 95
         if (!get_bits1(gb)) //colskip
94
-            for (y=0; y<height; y++)
96
+            for (y = 0; y < height; y++)
95 97
                 plane[y*stride] = 0;
96 98
         else
97
-            for (y=0; y<height; y++)
99
+            for (y = 0; y < height; y++)
98 100
                 plane[y*stride] = get_bits1(gb);
99 101
         plane ++;
100 102
     }
... ...
@@ -115,76 +121,76 @@ static int bitplane_decoding(uint8_t* data, int *raw_flag, VC1Context *v)
115 115
     uint8_t invert, *planep = data;
116 116
     int width, height, stride;
117 117
 
118
-    width = v->s.mb_width;
119
-    height = v->s.mb_height;
118
+    width  = v->s.mb_width;
119
+    height = v->s.mb_height >> v->field_mode;
120 120
     stride = v->s.mb_stride;
121 121
     invert = get_bits1(gb);
122 122
     imode = get_vlc2(gb, ff_vc1_imode_vlc.table, VC1_IMODE_VLC_BITS, 1);
123 123
 
124 124
     *raw_flag = 0;
125
-    switch (imode)
126
-    {
125
+    switch (imode) {
127 126
     case IMODE_RAW:
128 127
         //Data is actually read in the MB layer (same for all tests == "raw")
129 128
         *raw_flag = 1; //invert ignored
130 129
         return invert;
131 130
     case IMODE_DIFF2:
132 131
     case IMODE_NORM2:
133
-        if ((height * width) & 1)
134
-        {
132
+        if ((height * width) & 1) {
135 133
             *planep++ = get_bits1(gb);
136
-            offset = 1;
134
+            offset    = 1;
137 135
         }
138
-        else offset = 0;
136
+        else
137
+            offset = 0;
139 138
         // decode bitplane as one long line
140 139
         for (y = offset; y < height * width; y += 2) {
141 140
             code = get_vlc2(gb, ff_vc1_norm2_vlc.table, VC1_NORM2_VLC_BITS, 1);
142 141
             *planep++ = code & 1;
143 142
             offset++;
144
-            if(offset == width) {
145
-                offset = 0;
143
+            if (offset == width) {
144
+                offset  = 0;
146 145
                 planep += stride - width;
147 146
             }
148 147
             *planep++ = code >> 1;
149 148
             offset++;
150
-            if(offset == width) {
151
-                offset = 0;
149
+            if (offset == width) {
150
+                offset  = 0;
152 151
                 planep += stride - width;
153 152
             }
154 153
         }
155 154
         break;
156 155
     case IMODE_DIFF6:
157 156
     case IMODE_NORM6:
158
-        if(!(height % 3) && (width % 3)) { // use 2x3 decoding
159
-            for(y = 0; y < height; y+= 3) {
160
-                for(x = width & 1; x < width; x += 2) {
157
+        if (!(height % 3) && (width % 3)) { // use 2x3 decoding
158
+            for (y = 0; y < height; y += 3) {
159
+                for (x = width & 1; x < width; x += 2) {
161 160
                     code = get_vlc2(gb, ff_vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
162
-                    if(code < 0){
161
+                    if (code < 0) {
163 162
                         av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
164 163
                         return -1;
165 164
                     }
166
-                    planep[x + 0] = (code >> 0) & 1;
167
-                    planep[x + 1] = (code >> 1) & 1;
168
-                    planep[x + 0 + stride] = (code >> 2) & 1;
169
-                    planep[x + 1 + stride] = (code >> 3) & 1;
165
+                    planep[x + 0]              = (code >> 0) & 1;
166
+                    planep[x + 1]              = (code >> 1) & 1;
167
+                    planep[x + 0 + stride]     = (code >> 2) & 1;
168
+                    planep[x + 1 + stride]     = (code >> 3) & 1;
170 169
                     planep[x + 0 + stride * 2] = (code >> 4) & 1;
171 170
                     planep[x + 1 + stride * 2] = (code >> 5) & 1;
172 171
                 }
173 172
                 planep += stride * 3;
174 173
             }
175
-            if(width & 1) decode_colskip(data, 1, height, stride, &v->s.gb);
174
+            if (width & 1)
175
+                decode_colskip(data, 1, height, stride, &v->s.gb);
176 176
         } else { // 3x2
177 177
             planep += (height & 1) * stride;
178
-            for(y = height & 1; y < height; y += 2) {
179
-                for(x = width % 3; x < width; x += 3) {
178
+            for (y = height & 1; y < height; y += 2) {
179
+                for (x = width % 3; x < width; x += 3) {
180 180
                     code = get_vlc2(gb, ff_vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
181
-                    if(code < 0){
181
+                    if (code < 0) {
182 182
                         av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
183 183
                         return -1;
184 184
                     }
185
-                    planep[x + 0] = (code >> 0) & 1;
186
-                    planep[x + 1] = (code >> 1) & 1;
187
-                    planep[x + 2] = (code >> 2) & 1;
185
+                    planep[x + 0]          = (code >> 0) & 1;
186
+                    planep[x + 1]          = (code >> 1) & 1;
187
+                    planep[x + 2]          = (code >> 2) & 1;
188 188
                     planep[x + 0 + stride] = (code >> 3) & 1;
189 189
                     planep[x + 1 + stride] = (code >> 4) & 1;
190 190
                     planep[x + 2 + stride] = (code >> 5) & 1;
... ...
@@ -192,8 +198,10 @@ static int bitplane_decoding(uint8_t* data, int *raw_flag, VC1Context *v)
192 192
                 planep += stride * 2;
193 193
             }
194 194
             x = width % 3;
195
-            if(x) decode_colskip(data  ,             x, height    , stride, &v->s.gb);
196
-            if(height & 1) decode_rowskip(data+x, width - x, 1, stride, &v->s.gb);
195
+            if (x)
196
+                decode_colskip(data,             x, height, stride, &v->s.gb);
197
+            if (height & 1)
198
+                decode_rowskip(data + x, width - x,      1, stride, &v->s.gb);
197 199
         }
198 200
         break;
199 201
     case IMODE_ROWSKIP:
... ...
@@ -202,33 +210,30 @@ static int bitplane_decoding(uint8_t* data, int *raw_flag, VC1Context *v)
202 202
     case IMODE_COLSKIP:
203 203
         decode_colskip(data, width, height, stride, &v->s.gb);
204 204
         break;
205
-    default: break;
205
+    default:
206
+        break;
206 207
     }
207 208
 
208 209
     /* Applying diff operator */
209
-    if (imode == IMODE_DIFF2 || imode == IMODE_DIFF6)
210
-    {
210
+    if (imode == IMODE_DIFF2 || imode == IMODE_DIFF6) {
211 211
         planep = data;
212 212
         planep[0] ^= invert;
213
-        for (x=1; x<width; x++)
213
+        for (x = 1; x < width; x++)
214 214
             planep[x] ^= planep[x-1];
215
-        for (y=1; y<height; y++)
216
-        {
215
+        for (y = 1; y < height; y++) {
217 216
             planep += stride;
218 217
             planep[0] ^= planep[-stride];
219
-            for (x=1; x<width; x++)
220
-            {
218
+            for (x = 1; x < width; x++) {
221 219
                 if (planep[x-1] != planep[x-stride]) planep[x] ^= invert;
222 220
                 else                                 planep[x] ^= planep[x-1];
223 221
             }
224 222
         }
225
-    }
226
-    else if (invert)
227
-    {
223
+    } else if (invert) {
228 224
         planep = data;
229
-        for (x=0; x<stride*height; x++) planep[x] = !planep[x]; //FIXME stride
225
+        for (x = 0; x < stride * height; x++)
226
+            planep[x] = !planep[x]; //FIXME stride
230 227
     }
231
-    return (imode<<1) + invert;
228
+    return (imode << 1) + invert;
232 229
 }
233 230
 
234 231
 /** @} */ //Bitplane group
... ...
@@ -243,35 +248,34 @@ static int vop_dquant_decoding(VC1Context *v)
243 243
     int pqdiff;
244 244
 
245 245
     //variable size
246
-    if (v->dquant == 2)
247
-    {
246
+    if (v->dquant == 2) {
248 247
         pqdiff = get_bits(gb, 3);
249
-        if (pqdiff == 7) v->altpq = get_bits(gb, 5);
250
-        else v->altpq = v->pq + pqdiff + 1;
251
-    }
252
-    else
253
-    {
248
+        if (pqdiff == 7)
249
+            v->altpq = get_bits(gb, 5);
250
+        else
251
+            v->altpq = v->pq + pqdiff + 1;
252
+    } else {
254 253
         v->dquantfrm = get_bits1(gb);
255
-        if ( v->dquantfrm )
256
-        {
254
+        if (v->dquantfrm) {
257 255
             v->dqprofile = get_bits(gb, 2);
258
-            switch (v->dqprofile)
259
-            {
256
+            switch (v->dqprofile) {
260 257
             case DQPROFILE_SINGLE_EDGE:
261 258
             case DQPROFILE_DOUBLE_EDGES:
262 259
                 v->dqsbedge = get_bits(gb, 2);
263 260
                 break;
264 261
             case DQPROFILE_ALL_MBS:
265 262
                 v->dqbilevel = get_bits1(gb);
266
-                if(!v->dqbilevel)
263
+                if (!v->dqbilevel)
267 264
                     v->halfpq = 0;
268
-            default: break; //Forbidden ?
265
+            default:
266
+                break; //Forbidden ?
269 267
             }
270
-            if (v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS)
271
-            {
268
+            if (v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS) {
272 269
                 pqdiff = get_bits(gb, 3);
273
-                if (pqdiff == 7) v->altpq = get_bits(gb, 5);
274
-                else v->altpq = v->pq + pqdiff + 1;
270
+                if (pqdiff == 7)
271
+                    v->altpq = get_bits(gb, 5);
272
+                else
273
+                    v->altpq = v->pq + pqdiff + 1;
275 274
             }
276 275
         }
277 276
     }
... ...
@@ -291,25 +295,20 @@ int vc1_decode_sequence_header(AVCodecContext *avctx, VC1Context *v, GetBitConte
291 291
 {
292 292
     av_log(avctx, AV_LOG_DEBUG, "Header: %0X\n", show_bits(gb, 32));
293 293
     v->profile = get_bits(gb, 2);
294
-    if (v->profile == PROFILE_COMPLEX)
295
-    {
294
+    if (v->profile == PROFILE_COMPLEX) {
296 295
         av_log(avctx, AV_LOG_WARNING, "WMV3 Complex Profile is not fully supported\n");
297 296
     }
298 297
 
299
-    if (v->profile == PROFILE_ADVANCED)
300
-    {
298
+    if (v->profile == PROFILE_ADVANCED) {
301 299
         v->zz_8x4 = ff_vc1_adv_progressive_8x4_zz;
302 300
         v->zz_4x8 = ff_vc1_adv_progressive_4x8_zz;
303 301
         return decode_sequence_header_adv(v, gb);
304
-    }
305
-    else
306
-    {
302
+    } else {
307 303
         v->zz_8x4 = wmv2_scantableA;
308 304
         v->zz_4x8 = wmv2_scantableB;
309 305
         v->res_y411   = get_bits1(gb);
310 306
         v->res_sprite = get_bits1(gb);
311
-        if (v->res_y411)
312
-        {
307
+        if (v->res_y411) {
313 308
             av_log(avctx, AV_LOG_ERROR,
314 309
                    "Old interlaced mode is not supported\n");
315 310
             return -1;
... ...
@@ -320,48 +319,45 @@ int vc1_decode_sequence_header(AVCodecContext *avctx, VC1Context *v, GetBitConte
320 320
     v->frmrtq_postproc = get_bits(gb, 3); //common
321 321
     // (bitrate-32kbps)/64kbps
322 322
     v->bitrtq_postproc = get_bits(gb, 5); //common
323
-    v->s.loop_filter = get_bits1(gb); //common
324
-    if(v->s.loop_filter == 1 && v->profile == PROFILE_SIMPLE)
325
-    {
323
+    v->s.loop_filter   = get_bits1(gb); //common
324
+    if (v->s.loop_filter == 1 && v->profile == PROFILE_SIMPLE) {
326 325
         av_log(avctx, AV_LOG_ERROR,
327 326
                "LOOPFILTER shall not be enabled in Simple Profile\n");
328 327
     }
329
-    if(v->s.avctx->skip_loop_filter >= AVDISCARD_ALL)
328
+    if (v->s.avctx->skip_loop_filter >= AVDISCARD_ALL)
330 329
         v->s.loop_filter = 0;
331 330
 
332
-    v->res_x8 = get_bits1(gb); //reserved
333
-    v->multires = get_bits1(gb);
334
-    v->res_fasttx = get_bits1(gb);
335
-    if (!v->res_fasttx)
336
-    {
337
-        v->vc1dsp.vc1_inv_trans_8x8 = ff_simple_idct_8;
338
-        v->vc1dsp.vc1_inv_trans_8x4 = ff_simple_idct84_add;
339
-        v->vc1dsp.vc1_inv_trans_4x8 = ff_simple_idct48_add;
340
-        v->vc1dsp.vc1_inv_trans_4x4 = ff_simple_idct44_add;
331
+    v->res_x8          = get_bits1(gb); //reserved
332
+    v->multires        = get_bits1(gb);
333
+    v->res_fasttx      = get_bits1(gb);
334
+    if (!v->res_fasttx) {
335
+        v->vc1dsp.vc1_inv_trans_8x8    = ff_simple_idct_8;
336
+        v->vc1dsp.vc1_inv_trans_8x4    = ff_simple_idct84_add;
337
+        v->vc1dsp.vc1_inv_trans_4x8    = ff_simple_idct48_add;
338
+        v->vc1dsp.vc1_inv_trans_4x4    = ff_simple_idct44_add;
341 339
         v->vc1dsp.vc1_inv_trans_8x8_dc = ff_simple_idct_add_8;
342 340
         v->vc1dsp.vc1_inv_trans_8x4_dc = ff_simple_idct84_add;
343 341
         v->vc1dsp.vc1_inv_trans_4x8_dc = ff_simple_idct48_add;
344 342
         v->vc1dsp.vc1_inv_trans_4x4_dc = ff_simple_idct44_add;
345 343
     }
346 344
 
347
-    v->fastuvmc =  get_bits1(gb); //common
348
-    if (!v->profile && !v->fastuvmc)
349
-    {
345
+    v->fastuvmc        = get_bits1(gb); //common
346
+    if (!v->profile && !v->fastuvmc) {
350 347
         av_log(avctx, AV_LOG_ERROR,
351 348
                "FASTUVMC unavailable in Simple Profile\n");
352 349
         return -1;
353 350
     }
354
-    v->extended_mv =  get_bits1(gb); //common
351
+    v->extended_mv     = get_bits1(gb); //common
355 352
     if (!v->profile && v->extended_mv)
356 353
     {
357 354
         av_log(avctx, AV_LOG_ERROR,
358 355
                "Extended MVs unavailable in Simple Profile\n");
359 356
         return -1;
360 357
     }
361
-    v->dquant =  get_bits(gb, 2); //common
362
-    v->vstransform =  get_bits1(gb); //common
358
+    v->dquant          = get_bits(gb, 2); //common
359
+    v->vstransform     = get_bits1(gb); //common
363 360
 
364
-    v->res_transtab = get_bits1(gb);
361
+    v->res_transtab    = get_bits1(gb);
365 362
     if (v->res_transtab)
366 363
     {
367 364
         av_log(avctx, AV_LOG_ERROR,
... ...
@@ -369,12 +365,11 @@ int vc1_decode_sequence_header(AVCodecContext *avctx, VC1Context *v, GetBitConte
369 369
         return -1;
370 370
     }
371 371
 
372
-    v->overlap = get_bits1(gb); //common
372
+    v->overlap         = get_bits1(gb); //common
373 373
 
374 374
     v->s.resync_marker = get_bits1(gb);
375
-    v->rangered = get_bits1(gb);
376
-    if (v->rangered && v->profile == PROFILE_SIMPLE)
377
-    {
375
+    v->rangered        = get_bits1(gb);
376
+    if (v->rangered && v->profile == PROFILE_SIMPLE) {
378 377
         av_log(avctx, AV_LOG_INFO,
379 378
                "RANGERED should be set to 0 in Simple Profile\n");
380 379
     }
... ...
@@ -399,8 +394,7 @@ int vc1_decode_sequence_header(AVCodecContext *avctx, VC1Context *v, GetBitConte
399 399
     } else {
400 400
         v->res_rtm_flag = get_bits1(gb); //reserved
401 401
     }
402
-    if (!v->res_rtm_flag)
403
-    {
402
+    if (!v->res_rtm_flag) {
404 403
 //            av_log(avctx, AV_LOG_ERROR,
405 404
 //                   "0 for reserved RES_RTM_FLAG is forbidden\n");
406 405
         av_log(avctx, AV_LOG_ERROR,
... ...
@@ -408,17 +402,17 @@ int vc1_decode_sequence_header(AVCodecContext *avctx, VC1Context *v, GetBitConte
408 408
         //return -1;
409 409
     }
410 410
     //TODO: figure out what they mean (always 0x402F)
411
-    if(!v->res_fasttx) skip_bits(gb, 16);
411
+    if (!v->res_fasttx)
412
+        skip_bits(gb, 16);
412 413
     av_log(avctx, AV_LOG_DEBUG,
413
-               "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
414
-               "LoopFilter=%i, MultiRes=%i, FastUVMC=%i, Extended MV=%i\n"
415
-               "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
416
-               "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
417
-               v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
418
-               v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
419
-               v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
420
-               v->dquant, v->quantizer_mode, avctx->max_b_frames
421
-               );
414
+           "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
415
+           "LoopFilter=%i, MultiRes=%i, FastUVMC=%i, Extended MV=%i\n"
416
+           "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
417
+           "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
418
+           v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
419
+           v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
420
+           v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
421
+           v->dquant, v->quantizer_mode, avctx->max_b_frames);
422 422
     return 0;
423 423
 }
424 424
 
... ...
@@ -427,59 +421,56 @@ static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb)
427 427
     int w, h;
428 428
     v->res_rtm_flag = 1;
429 429
     v->level = get_bits(gb, 3);
430
-    if(v->level >= 5)
431
-    {
430
+    if (v->level >= 5) {
432 431
         av_log(v->s.avctx, AV_LOG_ERROR, "Reserved LEVEL %i\n",v->level);
433 432
     }
434 433
     v->chromaformat = get_bits(gb, 2);
435
-    if (v->chromaformat != 1)
436
-    {
434
+    if (v->chromaformat != 1) {
437 435
         av_log(v->s.avctx, AV_LOG_ERROR,
438 436
                "Only 4:2:0 chroma format supported\n");
439 437
         return -1;
440 438
     }
441 439
 
442 440
     // (fps-2)/4 (->30)
443
-    v->frmrtq_postproc = get_bits(gb, 3); //common
441
+    v->frmrtq_postproc       = get_bits(gb, 3); //common
444 442
     // (bitrate-32kbps)/64kbps
445
-    v->bitrtq_postproc = get_bits(gb, 5); //common
446
-    v->postprocflag = get_bits1(gb); //common
443
+    v->bitrtq_postproc       = get_bits(gb, 5); //common
444
+    v->postprocflag          = get_bits1(gb);   //common
447 445
 
448 446
     w = (get_bits(gb, 12) + 1) << 1;
449 447
     h = (get_bits(gb, 12) + 1) << 1;
450 448
     avcodec_set_dimensions(v->s.avctx, w, h);
451
-    v->broadcast = get_bits1(gb);
452
-    v->interlace = get_bits1(gb);
453
-    v->tfcntrflag = get_bits1(gb);
454
-    v->finterpflag = get_bits1(gb);
449
+    v->broadcast             = get_bits1(gb);
450
+    v->interlace             = get_bits1(gb);
451
+    v->tfcntrflag            = get_bits1(gb);
452
+    v->finterpflag           = get_bits1(gb);
455 453
     skip_bits1(gb); // reserved
456 454
 
457 455
     av_log(v->s.avctx, AV_LOG_DEBUG,
458
-               "Advanced Profile level %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
459
-               "LoopFilter=%i, ChromaFormat=%i, Pulldown=%i, Interlace: %i\n"
460
-               "TFCTRflag=%i, FINTERPflag=%i\n",
461
-               v->level, v->frmrtq_postproc, v->bitrtq_postproc,
462
-               v->s.loop_filter, v->chromaformat, v->broadcast, v->interlace,
463
-               v->tfcntrflag, v->finterpflag
464
-               );
456
+           "Advanced Profile level %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
457
+           "LoopFilter=%i, ChromaFormat=%i, Pulldown=%i, Interlace: %i\n"
458
+           "TFCTRflag=%i, FINTERPflag=%i\n",
459
+           v->level, v->frmrtq_postproc, v->bitrtq_postproc,
460
+           v->s.loop_filter, v->chromaformat, v->broadcast, v->interlace,
461
+           v->tfcntrflag, v->finterpflag);
465 462
 
466 463
     v->psf = get_bits1(gb);
467
-    if(v->psf) { //PsF, 6.1.13
464
+    if (v->psf) { //PsF, 6.1.13
468 465
         av_log(v->s.avctx, AV_LOG_ERROR, "Progressive Segmented Frame mode: not supported (yet)\n");
469 466
         return -1;
470 467
     }
471 468
     v->s.max_b_frames = v->s.avctx->max_b_frames = 7;
472
-    if(get_bits1(gb)) { //Display Info - decoding is not affected by it
469
+    if (get_bits1(gb)) { //Display Info - decoding is not affected by it
473 470
         int w, h, ar = 0;
474 471
         av_log(v->s.avctx, AV_LOG_DEBUG, "Display extended info:\n");
475 472
         w = get_bits(gb, 14) + 1;
476 473
         h = get_bits(gb, 14) + 1;
477 474
         av_log(v->s.avctx, AV_LOG_DEBUG, "Display dimensions: %ix%i\n", w, h);
478
-        if(get_bits1(gb))
475
+        if (get_bits1(gb))
479 476
             ar = get_bits(gb, 4);
480
-        if(ar && ar < 14){
477
+        if (ar && ar < 14) {
481 478
             v->s.avctx->sample_aspect_ratio = ff_vc1_pixel_aspect[ar];
482
-        }else if(ar == 15){
479
+        } else if (ar == 15) {
483 480
             w = get_bits(gb, 8) + 1;
484 481
             h = get_bits(gb, 8) + 1;
485 482
             v->s.avctx->sample_aspect_ratio = (AVRational){w, h};
... ...
@@ -488,43 +479,45 @@ static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb)
488 488
                       &v->s.avctx->sample_aspect_ratio.den,
489 489
                       v->s.avctx->height * w,
490 490
                       v->s.avctx->width * h,
491
-                      1<<30);
491
+                      1 << 30);
492 492
         }
493
-        av_log(v->s.avctx, AV_LOG_DEBUG, "Aspect: %i:%i\n", v->s.avctx->sample_aspect_ratio.num, v->s.avctx->sample_aspect_ratio.den);
493
+        av_log(v->s.avctx, AV_LOG_DEBUG, "Aspect: %i:%i\n",
494
+               v->s.avctx->sample_aspect_ratio.num,
495
+               v->s.avctx->sample_aspect_ratio.den);
494 496
 
495
-        if(get_bits1(gb)){ //framerate stuff
496
-            if(get_bits1(gb)) {
497
+        if (get_bits1(gb)) { //framerate stuff
498
+            if (get_bits1(gb)) {
497 499
                 v->s.avctx->time_base.num = 32;
498 500
                 v->s.avctx->time_base.den = get_bits(gb, 16) + 1;
499 501
             } else {
500 502
                 int nr, dr;
501 503
                 nr = get_bits(gb, 8);
502 504
                 dr = get_bits(gb, 4);
503
-                if(nr && nr < 8 && dr && dr < 3){
505
+                if (nr && nr < 8 && dr && dr < 3) {
504 506
                     v->s.avctx->time_base.num = ff_vc1_fps_dr[dr - 1];
505 507
                     v->s.avctx->time_base.den = ff_vc1_fps_nr[nr - 1] * 1000;
506 508
                 }
507 509
             }
508
-            if(v->broadcast) { // Pulldown may be present
509
-                v->s.avctx->time_base.den *= 2;
510
+            if (v->broadcast) { // Pulldown may be present
511
+                v->s.avctx->time_base.den  *= 2;
510 512
                 v->s.avctx->ticks_per_frame = 2;
511 513
             }
512 514
         }
513 515
 
514
-        if(get_bits1(gb)){
515
-            v->color_prim = get_bits(gb, 8);
516
+        if (get_bits1(gb)) {
517
+            v->color_prim    = get_bits(gb, 8);
516 518
             v->transfer_char = get_bits(gb, 8);
517
-            v->matrix_coef = get_bits(gb, 8);
519
+            v->matrix_coef   = get_bits(gb, 8);
518 520
         }
519 521
     }
520 522
 
521 523
     v->hrd_param_flag = get_bits1(gb);
522
-    if(v->hrd_param_flag) {
524
+    if (v->hrd_param_flag) {
523 525
         int i;
524 526
         v->hrd_num_leaky_buckets = get_bits(gb, 5);
525 527
         skip_bits(gb, 4); //bitrate exponent
526 528
         skip_bits(gb, 4); //buffer size exponent
527
-        for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
529
+        for (i = 0; i < v->hrd_num_leaky_buckets; i++) {
528 530
             skip_bits(gb, 16); //hrd_rate[n]
529 531
             skip_bits(gb, 16); //hrd_buffer[n]
530 532
         }
... ...
@@ -537,20 +530,20 @@ int vc1_decode_entry_point(AVCodecContext *avctx, VC1Context *v, GetBitContext *
537 537
     int i;
538 538
 
539 539
     av_log(avctx, AV_LOG_DEBUG, "Entry point: %08X\n", show_bits_long(gb, 32));
540
-    v->broken_link = get_bits1(gb);
541
-    v->closed_entry = get_bits1(gb);
542
-    v->panscanflag = get_bits1(gb);
543
-    v->refdist_flag = get_bits1(gb);
544
-    v->s.loop_filter = get_bits1(gb);
545
-    v->fastuvmc = get_bits1(gb);
546
-    v->extended_mv = get_bits1(gb);
547
-    v->dquant = get_bits(gb, 2);
548
-    v->vstransform = get_bits1(gb);
549
-    v->overlap = get_bits1(gb);
540
+    v->broken_link    = get_bits1(gb);
541
+    v->closed_entry   = get_bits1(gb);
542
+    v->panscanflag    = get_bits1(gb);
543
+    v->refdist_flag   = get_bits1(gb);
544
+    v->s.loop_filter  = get_bits1(gb);
545
+    v->fastuvmc       = get_bits1(gb);
546
+    v->extended_mv    = get_bits1(gb);
547
+    v->dquant         = get_bits(gb, 2);
548
+    v->vstransform    = get_bits1(gb);
549
+    v->overlap        = get_bits1(gb);
550 550
     v->quantizer_mode = get_bits(gb, 2);
551 551
 
552
-    if(v->hrd_param_flag){
553
-        for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
552
+    if (v->hrd_param_flag) {
553
+        for (i = 0; i < v->hrd_num_leaky_buckets; i++) {
554 554
             skip_bits(gb, 8); //hrd_full[n]
555 555
         }
556 556
     }
... ...
@@ -560,23 +553,23 @@ int vc1_decode_entry_point(AVCodecContext *avctx, VC1Context *v, GetBitContext *
560 560
         int h = (get_bits(gb, 12)+1)<<1;
561 561
         avcodec_set_dimensions(avctx, w, h);
562 562
     }
563
-    if(v->extended_mv)
563
+    if (v->extended_mv)
564 564
         v->extended_dmv = get_bits1(gb);
565
-    if((v->range_mapy_flag = get_bits1(gb))) {
565
+    if ((v->range_mapy_flag = get_bits1(gb))) {
566 566
         av_log(avctx, AV_LOG_ERROR, "Luma scaling is not supported, expect wrong picture\n");
567 567
         v->range_mapy = get_bits(gb, 3);
568 568
     }
569
-    if((v->range_mapuv_flag = get_bits1(gb))) {
569
+    if ((v->range_mapuv_flag = get_bits1(gb))) {
570 570
         av_log(avctx, AV_LOG_ERROR, "Chroma scaling is not supported, expect wrong picture\n");
571 571
         v->range_mapuv = get_bits(gb, 3);
572 572
     }
573 573
 
574 574
     av_log(avctx, AV_LOG_DEBUG, "Entry point info:\n"
575
-        "BrokenLink=%i, ClosedEntry=%i, PanscanFlag=%i\n"
576
-        "RefDist=%i, Postproc=%i, FastUVMC=%i, ExtMV=%i\n"
577
-        "DQuant=%i, VSTransform=%i, Overlap=%i, Qmode=%i\n",
578
-        v->broken_link, v->closed_entry, v->panscanflag, v->refdist_flag, v->s.loop_filter,
579
-        v->fastuvmc, v->extended_mv, v->dquant, v->vstransform, v->overlap, v->quantizer_mode);
575
+           "BrokenLink=%i, ClosedEntry=%i, PanscanFlag=%i\n"
576
+           "RefDist=%i, Postproc=%i, FastUVMC=%i, ExtMV=%i\n"
577
+           "DQuant=%i, VSTransform=%i, Overlap=%i, Qmode=%i\n",
578
+           v->broken_link, v->closed_entry, v->panscanflag, v->refdist_flag, v->s.loop_filter,
579
+           v->fastuvmc, v->extended_mv, v->dquant, v->vstransform, v->overlap, v->quantizer_mode);
580 580
 
581 581
     return 0;
582 582
 }
... ...
@@ -585,41 +578,48 @@ int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
585 585
 {
586 586
     int pqindex, lowquant, status;
587 587
 
588
-    if(v->finterpflag) v->interpfrm = get_bits1(gb);
588
+    if (v->finterpflag)
589
+        v->interpfrm = get_bits1(gb);
589 590
     skip_bits(gb, 2); //framecnt unused
590 591
     v->rangeredfrm = 0;
591
-    if (v->rangered) v->rangeredfrm = get_bits1(gb);
592
+    if (v->rangered)
593
+        v->rangeredfrm = get_bits1(gb);
592 594
     v->s.pict_type = get_bits1(gb);
593 595
     if (v->s.avctx->max_b_frames) {
594 596
         if (!v->s.pict_type) {
595
-            if (get_bits1(gb)) v->s.pict_type = AV_PICTURE_TYPE_I;
596
-            else v->s.pict_type = AV_PICTURE_TYPE_B;
597
-        } else v->s.pict_type = AV_PICTURE_TYPE_P;
598
-    } else v->s.pict_type = v->s.pict_type ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
597
+            if (get_bits1(gb))
598
+                v->s.pict_type = AV_PICTURE_TYPE_I;
599
+            else
600
+                v->s.pict_type = AV_PICTURE_TYPE_B;
601
+        } else
602
+            v->s.pict_type = AV_PICTURE_TYPE_P;
603
+    } else
604
+        v->s.pict_type = v->s.pict_type ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
599 605
 
600 606
     v->bi_type = 0;
601
-    if(v->s.pict_type == AV_PICTURE_TYPE_B) {
607
+    if (v->s.pict_type == AV_PICTURE_TYPE_B) {
602 608
         v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
603
-        v->bfraction = ff_vc1_bfraction_lut[v->bfraction_lut_index];
604
-        if(v->bfraction == 0) {
609
+        v->bfraction           = ff_vc1_bfraction_lut[v->bfraction_lut_index];
610
+        if (v->bfraction == 0) {
605 611
             v->s.pict_type = AV_PICTURE_TYPE_BI;
606 612
         }
607 613
     }
608
-    if(v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
614
+    if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
609 615
         skip_bits(gb, 7); // skip buffer fullness
610 616
 
611
-    if(v->parse_only)
617
+    if (v->parse_only)
612 618
         return 0;
613 619
 
614 620
     /* calculate RND */
615
-    if(v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
621
+    if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
616 622
         v->rnd = 1;
617
-    if(v->s.pict_type == AV_PICTURE_TYPE_P)
623
+    if (v->s.pict_type == AV_PICTURE_TYPE_P)
618 624
         v->rnd ^= 1;
619 625
 
620 626
     /* Quantizer stuff */
621 627
     pqindex = get_bits(gb, 5);
622
-    if(!pqindex) return -1;
628
+    if (!pqindex)
629
+        return -1;
623 630
     if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
624 631
         v->pq = ff_vc1_pquant_table[0][pqindex];
625 632
     else
... ...
@@ -631,63 +631,69 @@ int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
631 631
     if (v->quantizer_mode == QUANT_NON_UNIFORM)
632 632
         v->pquantizer = 0;
633 633
     v->pqindex = pqindex;
634
-    if (pqindex < 9) v->halfpq = get_bits1(gb);
635
-    else v->halfpq = 0;
634
+    if (pqindex < 9)
635
+        v->halfpq = get_bits1(gb);
636
+    else
637
+        v->halfpq = 0;
636 638
     if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
637 639
         v->pquantizer = get_bits1(gb);
638 640
     v->dquantfrm = 0;
639
-    if (v->extended_mv == 1) v->mvrange = get_unary(gb, 0, 3);
641
+    if (v->extended_mv == 1)
642
+        v->mvrange = get_unary(gb, 0, 3);
640 643
     v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
641 644
     v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
642 645
     v->range_x = 1 << (v->k_x - 1);
643 646
     v->range_y = 1 << (v->k_y - 1);
644
-    if (v->multires && v->s.pict_type != AV_PICTURE_TYPE_B) v->respic = get_bits(gb, 2);
647
+    if (v->multires && v->s.pict_type != AV_PICTURE_TYPE_B)
648
+        v->respic = get_bits(gb, 2);
645 649
 
646
-    if(v->res_x8 && (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)){
650
+    if (v->res_x8 && (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)) {
647 651
         v->x8_type = get_bits1(gb);
648
-    }else v->x8_type = 0;
652
+    } else
653
+        v->x8_type = 0;
649 654
 //av_log(v->s.avctx, AV_LOG_INFO, "%c Frame: QP=[%i]%i (+%i/2) %i\n",
650 655
 //        (v->s.pict_type == AV_PICTURE_TYPE_P) ? 'P' : ((v->s.pict_type == AV_PICTURE_TYPE_I) ? 'I' : 'B'), pqindex, v->pq, v->halfpq, v->rangeredfrm);
651 656
 
652
-    if(v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_P) v->use_ic = 0;
657
+    if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_P)
658
+        v->use_ic = 0;
653 659
 
654
-    switch(v->s.pict_type) {
660
+    switch (v->s.pict_type) {
655 661
     case AV_PICTURE_TYPE_P:
656
-        if (v->pq < 5) v->tt_index = 0;
657
-        else if(v->pq < 13) v->tt_index = 1;
658
-        else v->tt_index = 2;
662
+        if (v->pq < 5)       v->tt_index = 0;
663
+        else if (v->pq < 13) v->tt_index = 1;
664
+        else                 v->tt_index = 2;
659 665
 
660 666
         lowquant = (v->pq > 12) ? 0 : 1;
661 667
         v->mv_mode = ff_vc1_mv_pmode_table[lowquant][get_unary(gb, 1, 4)];
662
-        if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
663
-        {
668
+        if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
664 669
             int scale, shift, i;
665 670
             v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
666 671
             v->lumscale = get_bits(gb, 6);
667 672
             v->lumshift = get_bits(gb, 6);
668
-            v->use_ic = 1;
673
+            v->use_ic   = 1;
669 674
             /* fill lookup tables for intensity compensation */
670
-            if(!v->lumscale) {
675
+            if (!v->lumscale) {
671 676
                 scale = -64;
672 677
                 shift = (255 - v->lumshift * 2) << 6;
673
-                if(v->lumshift > 31)
678
+                if (v->lumshift > 31)
674 679
                     shift += 128 << 6;
675 680
             } else {
676 681
                 scale = v->lumscale + 32;
677
-                if(v->lumshift > 31)
682
+                if (v->lumshift > 31)
678 683
                     shift = (v->lumshift - 64) << 6;
679 684
                 else
680 685
                     shift = v->lumshift << 6;
681 686
             }
682
-            for(i = 0; i < 256; i++) {
683
-                v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
687
+            for (i = 0; i < 256; i++) {
688
+                v->luty[i]  = av_clip_uint8((scale * i + shift + 32) >> 6);
684 689
                 v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
685 690
             }
686 691
         }
687
-        if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
692
+        v->qs_last = v->s.quarter_sample;
693
+        if (v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
688 694
             v->s.quarter_sample = 0;
689
-        else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
690
-            if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
695
+        else if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
696
+            if (v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
691 697
                 v->s.quarter_sample = 0;
692 698
             else
693 699
                 v->s.quarter_sample = 1;
... ...
@@ -695,12 +701,12 @@ int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
695 695
             v->s.quarter_sample = 1;
696 696
         v->s.mspel = !(v->mv_mode == MV_PMODE_1MV_HPEL_BILIN || (v->mv_mode == MV_PMODE_INTENSITY_COMP && v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN));
697 697
 
698
-        if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
699
-                 v->mv_mode2 == MV_PMODE_MIXED_MV)
700
-                || v->mv_mode == MV_PMODE_MIXED_MV)
701
-        {
698
+        if ((v->mv_mode  == MV_PMODE_INTENSITY_COMP &&
699
+             v->mv_mode2 == MV_PMODE_MIXED_MV)      ||
700
+            v->mv_mode   == MV_PMODE_MIXED_MV) {
702 701
             status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
703
-            if (status < 0) return -1;
702
+            if (status < 0)
703
+                return -1;
704 704
             av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
705 705
                    "Imode: %i, Invert: %i\n", status>>1, status&1);
706 706
         } else {
... ...
@@ -708,7 +714,8 @@ int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
708 708
             memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
709 709
         }
710 710
         status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
711
-        if (status < 0) return -1;
711
+        if (status < 0)
712
+            return -1;
712 713
         av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
713 714
                "Imode: %i, Invert: %i\n", status>>1, status&1);
714 715
 
... ...
@@ -716,18 +723,15 @@ int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
716 716
         v->s.mv_table_index = get_bits(gb, 2); //but using ff_vc1_ tables
717 717
         v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
718 718
 
719
-        if (v->dquant)
720
-        {
719
+        if (v->dquant) {
721 720
             av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
722 721
             vop_dquant_decoding(v);
723 722
         }
724 723
 
725 724
         v->ttfrm = 0; //FIXME Is that so ?
726
-        if (v->vstransform)
727
-        {
725
+        if (v->vstransform) {
728 726
             v->ttmbf = get_bits1(gb);
729
-            if (v->ttmbf)
730
-            {
727
+            if (v->ttmbf) {
731 728
                 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
732 729
             }
733 730
         } else {
... ...
@@ -736,38 +740,38 @@ int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
736 736
         }
737 737
         break;
738 738
     case AV_PICTURE_TYPE_B:
739
-        if (v->pq < 5) v->tt_index = 0;
740
-        else if(v->pq < 13) v->tt_index = 1;
741
-        else v->tt_index = 2;
739
+        if (v->pq < 5)       v->tt_index = 0;
740
+        else if (v->pq < 13) v->tt_index = 1;
741
+        else                 v->tt_index = 2;
742 742
 
743
-        v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
743
+        v->mv_mode          = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
744
+        v->qs_last          = v->s.quarter_sample;
744 745
         v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
745
-        v->s.mspel = v->s.quarter_sample;
746
+        v->s.mspel          = v->s.quarter_sample;
746 747
 
747 748
         status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
748
-        if (status < 0) return -1;
749
+        if (status < 0)
750
+            return -1;
749 751
         av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
750 752
                "Imode: %i, Invert: %i\n", status>>1, status&1);
751 753
         status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
752
-        if (status < 0) return -1;
754
+        if (status < 0)
755
+            return -1;
753 756
         av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
754 757
                "Imode: %i, Invert: %i\n", status>>1, status&1);
755 758
 
756 759
         v->s.mv_table_index = get_bits(gb, 2);
757
-        v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
760
+        v->cbpcy_vlc        = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
758 761
 
759
-        if (v->dquant)
760
-        {
762
+        if (v->dquant) {
761 763
             av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
762 764
             vop_dquant_decoding(v);
763 765
         }
764 766
 
765 767
         v->ttfrm = 0;
766
-        if (v->vstransform)
767
-        {
768
+        if (v->vstransform) {
768 769
             v->ttmbf = get_bits1(gb);
769
-            if (v->ttmbf)
770
-            {
770
+            if (v->ttmbf) {
771 771
                 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
772 772
             }
773 773
         } else {
... ...
@@ -777,88 +781,154 @@ int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
777 777
         break;
778 778
     }
779 779
 
780
-    if(!v->x8_type)
781
-    {
780
+    if (!v->x8_type) {
782 781
         /* AC Syntax */
783 782
         v->c_ac_table_index = decode012(gb);
784
-        if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
785
-        {
783
+        if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI) {
786 784
             v->y_ac_table_index = decode012(gb);
787 785
         }
788 786
         /* DC Syntax */
789 787
         v->s.dc_table_index = get_bits1(gb);
790 788
     }
791 789
 
792
-    if(v->s.pict_type == AV_PICTURE_TYPE_BI) {
790
+    if (v->s.pict_type == AV_PICTURE_TYPE_BI) {
793 791
         v->s.pict_type = AV_PICTURE_TYPE_B;
794
-        v->bi_type = 1;
792
+        v->bi_type     = 1;
795 793
     }
796 794
     return 0;
797 795
 }
798 796
 
797
+/* fill lookup tables for intensity compensation */
798
+#define INIT_LUT(lumscale, lumshift, luty, lutuv)   \
799
+    if (!lumscale) {                                \
800
+        scale = -64;                                \
801
+        shift = (255 - lumshift * 2) << 6;          \
802
+        if (lumshift > 31)                          \
803
+            shift += 128 << 6;                      \
804
+    } else {                                        \
805
+        scale = lumscale + 32;                      \
806
+        if (lumshift > 31)                          \
807
+            shift = (lumshift - 64) << 6;           \
808
+        else                                        \
809
+            shift = lumshift << 6;                  \
810
+    }                                               \
811
+    for (i = 0; i < 256; i++) {                     \
812
+        luty[i]  = av_clip_uint8((scale * i + shift + 32) >> 6);           \
813
+        lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);  \
814
+    }
815
+
799 816
 int vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb)
800 817
 {
801 818
     int pqindex, lowquant;
802 819
     int status;
820
+    int mbmodetab, imvtab, icbptab, twomvbptab, fourmvbptab; /* useful only for debugging */
821
+    int scale, shift, i; /* for initializing LUT for intensity compensation */
803 822
 
823
+    v->numref=0;
804 824
     v->p_frame_skipped = 0;
825
+    if (v->second_field) {
826
+        v->s.pict_type = (v->fptype & 1) ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
827
+        if (v->fptype & 4)
828
+            v->s.pict_type = (v->fptype & 1) ? AV_PICTURE_TYPE_BI : AV_PICTURE_TYPE_B;
829
+        v->s.current_picture_ptr->f.pict_type = v->s.pict_type;
830
+        if (!v->pic_header_flag)
831
+            goto parse_common_info;
832
+    }
805 833
 
806
-    if(v->interlace){
834
+    if (v->interlace) {
807 835
         v->fcm = decode012(gb);
808
-        if(v->fcm){
809
-            if(!v->warn_interlaced++)
810
-                av_log(v->s.avctx, AV_LOG_ERROR, "Interlaced frames/fields support is not implemented\n");
811
-            return -1;
836
+        if (v->fcm) {
837
+            if (v->fcm == 2)
838
+                v->field_mode = 1;
839
+            else
840
+                v->field_mode = 0;
841
+            if (!v->warn_interlaced++)
842
+                av_log(v->s.avctx, AV_LOG_ERROR,
843
+                       "Interlaced frames/fields support is incomplete\n");
812 844
         }
813 845
     }
814
-    switch(get_unary(gb, 0, 4)) {
815
-    case 0:
816
-        v->s.pict_type = AV_PICTURE_TYPE_P;
817
-        break;
818
-    case 1:
819
-        v->s.pict_type = AV_PICTURE_TYPE_B;
820
-        break;
821
-    case 2:
822
-        v->s.pict_type = AV_PICTURE_TYPE_I;
823
-        break;
824
-    case 3:
825
-        v->s.pict_type = AV_PICTURE_TYPE_BI;
826
-        break;
827
-    case 4:
828
-        v->s.pict_type = AV_PICTURE_TYPE_P; // skipped pic
829
-        v->p_frame_skipped = 1;
830
-        break;
846
+
847
+    if (v->field_mode) {
848
+        v->fptype = get_bits(gb, 3);
849
+        v->s.pict_type = (v->fptype & 2) ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
850
+        if (v->fptype & 4) // B-picture
851
+            v->s.pict_type = (v->fptype & 2) ? AV_PICTURE_TYPE_BI : AV_PICTURE_TYPE_B;
852
+    } else {
853
+        switch (get_unary(gb, 0, 4)) {
854
+        case 0:
855
+            v->s.pict_type = AV_PICTURE_TYPE_P;
856
+            break;
857
+        case 1:
858
+            v->s.pict_type = AV_PICTURE_TYPE_B;
859
+            break;
860
+        case 2:
861
+            v->s.pict_type = AV_PICTURE_TYPE_I;
862
+            break;
863
+        case 3:
864
+            v->s.pict_type = AV_PICTURE_TYPE_BI;
865
+            break;
866
+        case 4:
867
+            v->s.pict_type = AV_PICTURE_TYPE_P; // skipped pic
868
+            v->p_frame_skipped = 1;
869
+            break;
870
+        }
831 871
     }
832
-    if(v->tfcntrflag)
872
+    if (v->tfcntrflag)
833 873
         skip_bits(gb, 8);
834
-    if(v->broadcast) {
835
-        if(!v->interlace || v->psf) {
874
+    if (v->broadcast) {
875
+        if (!v->interlace || v->psf) {
836 876
             v->rptfrm = get_bits(gb, 2);
837 877
         } else {
838 878
             v->tff = get_bits1(gb);
839 879
             v->rff = get_bits1(gb);
840 880
         }
841 881
     }
842
-    if(v->panscanflag) {
882
+    if (v->panscanflag) {
843 883
         av_log_missing_feature(v->s.avctx, "Pan-scan", 0);
844 884
         //...
845 885
     }
846
-    if(v->p_frame_skipped) {
886
+    if (v->p_frame_skipped) {
847 887
         return 0;
848 888
     }
849 889
     v->rnd = get_bits1(gb);
850
-    if(v->interlace)
890
+    if (v->interlace)
851 891
         v->uvsamp = get_bits1(gb);
852
-    if(v->finterpflag) v->interpfrm = get_bits1(gb);
853
-    if(v->s.pict_type == AV_PICTURE_TYPE_B) {
892
+    if (v->field_mode) {
893
+        if (!v->refdist_flag)
894
+            v->refdist = 0;
895
+        else {
896
+            if ((v->s.pict_type != AV_PICTURE_TYPE_B)
897
+                && (v->s.pict_type != AV_PICTURE_TYPE_BI)) {
898
+                v->refdist = get_bits(gb, 2);
899
+                if (v->refdist == 3)
900
+                    v->refdist += get_unary(gb, 0, 16);
901
+            } else {
902
+                v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
903
+                v->bfraction           = ff_vc1_bfraction_lut[v->bfraction_lut_index];
904
+                v->frfd = (v->bfraction * v->refdist) >> 8;
905
+                v->brfd = v->refdist - v->frfd - 1;
906
+                if (v->brfd < 0)
907
+                    v->brfd = 0;
908
+            }
909
+        }
910
+        goto parse_common_info;
911
+    }
912
+    if (v->finterpflag)
913
+        v->interpfrm = get_bits1(gb);
914
+    if (v->s.pict_type == AV_PICTURE_TYPE_B) {
854 915
         v->bfraction_lut_index = get_vlc2(gb, ff_vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
855
-        v->bfraction = ff_vc1_bfraction_lut[v->bfraction_lut_index];
856
-        if(v->bfraction == 0) {
916
+        v->bfraction           = ff_vc1_bfraction_lut[v->bfraction_lut_index];
917
+        if (v->bfraction == 0) {
857 918
             v->s.pict_type = AV_PICTURE_TYPE_BI; /* XXX: should not happen here */
858 919
         }
859 920
     }
921
+
922
+    parse_common_info:
923
+    if (v->field_mode)
924
+        v->cur_field_type = !(v->tff ^ v->second_field);
860 925
     pqindex = get_bits(gb, 5);
861
-    if(!pqindex) return -1;
926
+    if (!pqindex)
927
+        return -1;
862 928
     v->pqindex = pqindex;
863 929
     if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
864 930
         v->pq = ff_vc1_pquant_table[0][pqindex];
... ...
@@ -871,118 +941,193 @@ int vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb)
871 871
     if (v->quantizer_mode == QUANT_NON_UNIFORM)
872 872
         v->pquantizer = 0;
873 873
     v->pqindex = pqindex;
874
-    if (pqindex < 9) v->halfpq = get_bits1(gb);
875
-    else v->halfpq = 0;
874
+    if (pqindex < 9)
875
+        v->halfpq = get_bits1(gb);
876
+    else
877
+        v->halfpq = 0;
876 878
     if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
877 879
         v->pquantizer = get_bits1(gb);
878
-    if(v->postprocflag)
880
+    if (v->postprocflag)
879 881
         v->postproc = get_bits(gb, 2);
880 882
 
881
-    if(v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_P) v->use_ic = 0;
883
+    if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_P)
884
+        v->use_ic = 0;
882 885
 
883
-    if(v->parse_only)
886
+    if (v->parse_only)
884 887
         return 0;
885 888
 
886
-    switch(v->s.pict_type) {
889
+    switch (v->s.pict_type) {
887 890
     case AV_PICTURE_TYPE_I:
888 891
     case AV_PICTURE_TYPE_BI:
892
+        if (v->fcm == 1) { //interlace frame picture
893
+            status = bitplane_decoding(v->fieldtx_plane, &v->fieldtx_is_raw, v);
894
+            if (status < 0)
895
+                return -1;
896
+            av_log(v->s.avctx, AV_LOG_DEBUG, "FIELDTX plane encoding: "
897
+                   "Imode: %i, Invert: %i\n", status>>1, status&1);
898
+        }
889 899
         status = bitplane_decoding(v->acpred_plane, &v->acpred_is_raw, v);
890
-        if (status < 0) return -1;
900
+        if (status < 0)
901
+            return -1;
891 902
         av_log(v->s.avctx, AV_LOG_DEBUG, "ACPRED plane encoding: "
892
-                "Imode: %i, Invert: %i\n", status>>1, status&1);
903
+               "Imode: %i, Invert: %i\n", status>>1, status&1);
893 904
         v->condover = CONDOVER_NONE;
894
-        if(v->overlap && v->pq <= 8) {
905
+        if (v->overlap && v->pq <= 8) {
895 906
             v->condover = decode012(gb);
896
-            if(v->condover == CONDOVER_SELECT) {
907
+            if (v->condover == CONDOVER_SELECT) {
897 908
                 status = bitplane_decoding(v->over_flags_plane, &v->overflg_is_raw, v);
898
-                if (status < 0) return -1;
909
+                if (status < 0)
910
+                    return -1;
899 911
                 av_log(v->s.avctx, AV_LOG_DEBUG, "CONDOVER plane encoding: "
900
-                        "Imode: %i, Invert: %i\n", status>>1, status&1);
912
+                       "Imode: %i, Invert: %i\n", status>>1, status&1);
901 913
             }
902 914
         }
903 915
         break;
904 916
     case AV_PICTURE_TYPE_P:
905
-        if (v->extended_mv) v->mvrange = get_unary(gb, 0, 3);
906
-        else v->mvrange = 0;
917
+        if (v->field_mode) {
918
+            v->numref = get_bits1(gb);
919
+            if (!v->numref) {
920
+                v->reffield          = get_bits1(gb);
921
+                v->ref_field_type[0] = v->reffield ^ !v->cur_field_type;
922
+            }
923
+        }
924
+        if (v->extended_mv)
925
+            v->mvrange = get_unary(gb, 0, 3);
926
+        else
927
+            v->mvrange = 0;
928
+        if (v->interlace) {
929
+            if (v->extended_dmv)
930
+                v->dmvrange = get_unary(gb, 0, 3);
931
+            else
932
+                v->dmvrange = 0;
933
+            if (v->fcm == 1) { // interlaced frame picture
934
+                v->fourmvswitch = get_bits1(gb);
935
+                v->intcomp      = get_bits1(gb);
936
+                if (v->intcomp) {
937
+                    v->lumscale = get_bits(gb, 6);
938
+                    v->lumshift = get_bits(gb, 6);
939
+                    INIT_LUT(v->lumscale, v->lumshift, v->luty, v->lutuv);
940
+                }
941
+                status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
942
+                av_log(v->s.avctx, AV_LOG_DEBUG, "SKIPMB plane encoding: "
943
+                       "Imode: %i, Invert: %i\n", status>>1, status&1);
944
+                mbmodetab = get_bits(gb, 2);
945
+                if (v->fourmvswitch)
946
+                    v->mbmode_vlc = &ff_vc1_intfr_4mv_mbmode_vlc[mbmodetab];
947
+                else
948
+                    v->mbmode_vlc = &ff_vc1_intfr_non4mv_mbmode_vlc[mbmodetab];
949
+                imvtab         = get_bits(gb, 2);
950
+                v->imv_vlc     = &ff_vc1_1ref_mvdata_vlc[imvtab];
951
+                // interlaced p-picture cbpcy range is [1, 63]
952
+                icbptab        = get_bits(gb, 3);
953
+                v->cbpcy_vlc   = &ff_vc1_icbpcy_vlc[icbptab];
954
+                twomvbptab     = get_bits(gb, 2);
955
+                v->twomvbp_vlc = &ff_vc1_2mv_block_pattern_vlc[twomvbptab];
956
+                if (v->fourmvswitch) {
957
+                    fourmvbptab     = get_bits(gb, 2);
958
+                    v->fourmvbp_vlc = &ff_vc1_4mv_block_pattern_vlc[fourmvbptab];
959
+                }
960
+            }
961
+        }
907 962
         v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
908 963
         v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
909 964
         v->range_x = 1 << (v->k_x - 1);
910 965
         v->range_y = 1 << (v->k_y - 1);
911 966
 
912
-        if (v->pq < 5) v->tt_index = 0;
913
-        else if(v->pq < 13) v->tt_index = 1;
914
-        else v->tt_index = 2;
915
-
916
-        lowquant = (v->pq > 12) ? 0 : 1;
917
-        v->mv_mode = ff_vc1_mv_pmode_table[lowquant][get_unary(gb, 1, 4)];
918
-        if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
919
-        {
920
-            int scale, shift, i;
921
-            v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
922
-            v->lumscale = get_bits(gb, 6);
923
-            v->lumshift = get_bits(gb, 6);
924
-            /* fill lookup tables for intensity compensation */
925
-            if(!v->lumscale) {
926
-                scale = -64;
927
-                shift = (255 - v->lumshift * 2) << 6;
928
-                if(v->lumshift > 31)
929
-                    shift += 128 << 6;
930
-            } else {
931
-                scale = v->lumscale + 32;
932
-                if(v->lumshift > 31)
933
-                    shift = (v->lumshift - 64) << 6;
934
-                else
935
-                    shift = v->lumshift << 6;
936
-            }
937
-            for(i = 0; i < 256; i++) {
938
-                v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
939
-                v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
967
+        if (v->pq < 5)
968
+            v->tt_index = 0;
969
+        else if (v->pq < 13)
970
+            v->tt_index = 1;
971
+        else
972
+            v->tt_index = 2;
973
+        if (v->fcm != 1) {
974
+            int mvmode;
975
+            mvmode     = get_unary(gb, 1, 4);
976
+            lowquant   = (v->pq > 12) ? 0 : 1;
977
+            v->mv_mode = ff_vc1_mv_pmode_table[lowquant][mvmode];
978
+            if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
979
+                int mvmode2;
980
+                mvmode2 = get_unary(gb, 1, 3);
981
+                v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][mvmode2];
982
+                if (v->field_mode)
983
+                    v->intcompfield = decode210(gb);
984
+                v->lumscale = get_bits(gb, 6);
985
+                v->lumshift = get_bits(gb, 6);
986
+                INIT_LUT(v->lumscale, v->lumshift, v->luty, v->lutuv);
987
+                if ((v->field_mode) && !v->intcompfield) {
988
+                    v->lumscale2 = get_bits(gb, 6);
989
+                    v->lumshift2 = get_bits(gb, 6);
990
+                    INIT_LUT(v->lumscale2, v->lumshift2, v->luty2, v->lutuv2);
991
+                }
992
+                v->use_ic = 1;
940 993
             }
941
-            v->use_ic = 1;
942
-        }
943
-        if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
944
-            v->s.quarter_sample = 0;
945
-        else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
946
-            if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
994
+            v->qs_last = v->s.quarter_sample;
995
+            if (v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
947 996
                 v->s.quarter_sample = 0;
948
-            else
997
+            else if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
998
+                if (v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
999
+                    v->s.quarter_sample = 0;
1000
+                else
1001
+                    v->s.quarter_sample = 1;
1002
+            } else
949 1003
                 v->s.quarter_sample = 1;
950
-        } else
951
-            v->s.quarter_sample = 1;
952
-        v->s.mspel = !(v->mv_mode == MV_PMODE_1MV_HPEL_BILIN || (v->mv_mode == MV_PMODE_INTENSITY_COMP && v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN));
953
-
954
-        if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
1004
+            v->s.mspel = !(v->mv_mode == MV_PMODE_1MV_HPEL_BILIN
1005
+                           || (v->mv_mode == MV_PMODE_INTENSITY_COMP
1006
+                               && v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN));
1007
+        }
1008
+        if (v->fcm == 0) { // progressive
1009
+            if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
955 1010
                  v->mv_mode2 == MV_PMODE_MIXED_MV)
956
-                || v->mv_mode == MV_PMODE_MIXED_MV)
957
-        {
958
-            status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
959
-            if (status < 0) return -1;
960
-            av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
1011
+                || v->mv_mode == MV_PMODE_MIXED_MV) {
1012
+                status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
1013
+                if (status < 0)
1014
+                    return -1;
1015
+                av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
1016
+                       "Imode: %i, Invert: %i\n", status>>1, status&1);
1017
+            } else {
1018
+                v->mv_type_is_raw = 0;
1019
+                memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
1020
+            }
1021
+            status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1022
+            if (status < 0)
1023
+                return -1;
1024
+            av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
961 1025
                    "Imode: %i, Invert: %i\n", status>>1, status&1);
962
-        } else {
963
-            v->mv_type_is_raw = 0;
964
-            memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
965
-        }
966
-        status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
967
-        if (status < 0) return -1;
968
-        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
969
-               "Imode: %i, Invert: %i\n", status>>1, status&1);
970 1026
 
971
-        /* Hopefully this is correct for P frames */
972
-        v->s.mv_table_index = get_bits(gb, 2); //but using ff_vc1_ tables
973
-        v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
974
-        if (v->dquant)
975
-        {
1027
+            /* Hopefully this is correct for P frames */
1028
+            v->s.mv_table_index = get_bits(gb, 2); //but using ff_vc1_ tables
1029
+            v->cbpcy_vlc        = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1030
+        } else if (v->fcm == 1) { // frame interlaced
1031
+            v->qs_last          = v->s.quarter_sample;
1032
+            v->s.quarter_sample = 1;
1033
+            v->s.mspel          = 1;
1034
+        } else {    // field interlaced
1035
+            mbmodetab = get_bits(gb, 3);
1036
+            imvtab = get_bits(gb, 2 + v->numref);
1037
+            if (!v->numref)
1038
+                v->imv_vlc = &ff_vc1_1ref_mvdata_vlc[imvtab];
1039
+            else
1040
+                v->imv_vlc = &ff_vc1_2ref_mvdata_vlc[imvtab];
1041
+            icbptab = get_bits(gb, 3);
1042
+            v->cbpcy_vlc = &ff_vc1_icbpcy_vlc[icbptab];
1043
+            if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
1044
+                v->mv_mode2 == MV_PMODE_MIXED_MV) || v->mv_mode == MV_PMODE_MIXED_MV) {
1045
+                fourmvbptab     = get_bits(gb, 2);
1046
+                v->fourmvbp_vlc = &ff_vc1_4mv_block_pattern_vlc[fourmvbptab];
1047
+                v->mbmode_vlc = &ff_vc1_if_mmv_mbmode_vlc[mbmodetab];
1048
+            } else {
1049
+                v->mbmode_vlc = &ff_vc1_if_1mv_mbmode_vlc[mbmodetab];
1050
+            }
1051
+        }
1052
+        if (v->dquant) {
976 1053
             av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
977 1054
             vop_dquant_decoding(v);
978 1055
         }
979 1056
 
980 1057
         v->ttfrm = 0; //FIXME Is that so ?
981
-        if (v->vstransform)
982
-        {
1058
+        if (v->vstransform) {
983 1059
             v->ttmbf = get_bits1(gb);
984
-            if (v->ttmbf)
985
-            {
1060
+            if (v->ttmbf) {
986 1061
                 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
987 1062
             }
988 1063
         } else {
... ...
@@ -991,45 +1136,82 @@ int vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb)
991 991
         }
992 992
         break;
993 993
     case AV_PICTURE_TYPE_B:
994
-        if (v->extended_mv) v->mvrange = get_unary(gb, 0, 3);
995
-        else v->mvrange = 0;
996
-        v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
997
-        v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
994
+        // TODO: implement interlaced frame B picture decoding
995
+        if (v->fcm == 1)
996
+            return -1;
997
+        if (v->extended_mv)
998
+            v->mvrange = get_unary(gb, 0, 3);
999
+        else
1000
+            v->mvrange = 0;
1001
+        v->k_x     = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
1002
+        v->k_y     = v->mvrange + 8; //k_y can be 8 9 10 11
998 1003
         v->range_x = 1 << (v->k_x - 1);
999 1004
         v->range_y = 1 << (v->k_y - 1);
1000 1005
 
1001
-        if (v->pq < 5) v->tt_index = 0;
1002
-        else if(v->pq < 13) v->tt_index = 1;
1003
-        else v->tt_index = 2;
1004
-
1005
-        v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
1006
-        v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
1007
-        v->s.mspel = v->s.quarter_sample;
1008
-
1009
-        status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
1010
-        if (status < 0) return -1;
1011
-        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
1012
-               "Imode: %i, Invert: %i\n", status>>1, status&1);
1013
-        status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1014
-        if (status < 0) return -1;
1015
-        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1016
-               "Imode: %i, Invert: %i\n", status>>1, status&1);
1017
-
1018
-        v->s.mv_table_index = get_bits(gb, 2);
1019
-        v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1006
+        if (v->pq < 5)
1007
+            v->tt_index = 0;
1008
+        else if (v->pq < 13)
1009
+            v->tt_index = 1;
1010
+        else
1011
+            v->tt_index = 2;
1012
+
1013
+        if (v->field_mode) {
1014
+            int mvmode;
1015
+            if (v->extended_dmv)
1016
+                v->dmvrange = get_unary(gb, 0, 3);
1017
+            mvmode = get_unary(gb, 1, 3);
1018
+            lowquant = (v->pq > 12) ? 0 : 1;
1019
+            v->mv_mode          = ff_vc1_mv_pmode_table2[lowquant][mvmode];
1020
+            v->qs_last          = v->s.quarter_sample;
1021
+            v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV || v->mv_mode == MV_PMODE_MIXED_MV);
1022
+            v->s.mspel          = !(v->mv_mode == MV_PMODE_1MV_HPEL_BILIN || v->mv_mode == MV_PMODE_1MV_HPEL);
1023
+            status = bitplane_decoding(v->forward_mb_plane, &v->fmb_is_raw, v);
1024
+            if (status < 0)
1025
+                return -1;
1026
+            av_log(v->s.avctx, AV_LOG_DEBUG, "MB Forward Type plane encoding: "
1027
+                   "Imode: %i, Invert: %i\n", status>>1, status&1);
1028
+            mbmodetab = get_bits(gb, 3);
1029
+            if (v->mv_mode == MV_PMODE_MIXED_MV)
1030
+                v->mbmode_vlc = &ff_vc1_if_mmv_mbmode_vlc[mbmodetab];
1031
+            else
1032
+                v->mbmode_vlc = &ff_vc1_if_1mv_mbmode_vlc[mbmodetab];
1033
+            imvtab       = get_bits(gb, 3);
1034
+            v->imv_vlc   = &ff_vc1_2ref_mvdata_vlc[imvtab];
1035
+            icbptab      = get_bits(gb, 3);
1036
+            v->cbpcy_vlc = &ff_vc1_icbpcy_vlc[icbptab];
1037
+            if (v->mv_mode == MV_PMODE_MIXED_MV) {
1038
+                fourmvbptab     = get_bits(gb, 2);
1039
+                v->fourmvbp_vlc = &ff_vc1_4mv_block_pattern_vlc[fourmvbptab];
1040
+            }
1041
+            v->numref = 1; // interlaced field B pictures are always 2-ref
1042
+        } else {
1043
+            v->mv_mode          = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
1044
+            v->qs_last          = v->s.quarter_sample;
1045
+            v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
1046
+            v->s.mspel          = v->s.quarter_sample;
1047
+            status              = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
1048
+            if (status < 0)
1049
+                return -1;
1050
+            av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
1051
+                   "Imode: %i, Invert: %i\n", status>>1, status&1);
1052
+            status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1053
+            if (status < 0)
1054
+                return -1;
1055
+            av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1056
+                   "Imode: %i, Invert: %i\n", status>>1, status&1);
1057
+            v->s.mv_table_index = get_bits(gb, 2);
1058
+            v->cbpcy_vlc = &ff_vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1059
+        }
1020 1060
 
1021
-        if (v->dquant)
1022
-        {
1061
+        if (v->dquant) {
1023 1062
             av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1024 1063
             vop_dquant_decoding(v);
1025 1064
         }
1026 1065
 
1027 1066
         v->ttfrm = 0;
1028
-        if (v->vstransform)
1029
-        {
1067
+        if (v->vstransform) {
1030 1068
             v->ttmbf = get_bits1(gb);
1031
-            if (v->ttmbf)
1032
-            {
1069
+            if (v->ttmbf) {
1033 1070
                 v->ttfrm = ff_vc1_ttfrm_to_tt[get_bits(gb, 2)];
1034 1071
             }
1035 1072
         } else {
... ...
@@ -1041,19 +1223,19 @@ int vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb)
1041 1041
 
1042 1042
     /* AC Syntax */
1043 1043
     v->c_ac_table_index = decode012(gb);
1044
-    if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
1045
-    {
1044
+    if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI) {
1046 1045
         v->y_ac_table_index = decode012(gb);
1047 1046
     }
1048 1047
     /* DC Syntax */
1049 1048
     v->s.dc_table_index = get_bits1(gb);
1050
-    if ((v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI) && v->dquant) {
1049
+    if ((v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_BI)
1050
+        && v->dquant) {
1051 1051
         av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1052 1052
         vop_dquant_decoding(v);
1053 1053
     }
1054 1054
 
1055 1055
     v->bi_type = 0;
1056
-    if(v->s.pict_type == AV_PICTURE_TYPE_BI) {
1056
+    if (v->s.pict_type == AV_PICTURE_TYPE_BI) {
1057 1057
         v->s.pict_type = AV_PICTURE_TYPE_B;
1058 1058
         v->bi_type = 1;
1059 1059
     }
... ...
@@ -105,12 +105,25 @@ enum MVModes {
105 105
 };
106 106
 //@}
107 107
 
108
+/** MBMODE for interlaced frame P-picture */
109
+//@{
110
+enum MBModesIntfr {
111
+    MV_PMODE_INTFR_1MV,
112
+    MV_PMODE_INTFR_2MV_FIELD,
113
+    MV_PMODE_INTFR_2MV,
114
+    MV_PMODE_INTFR_4MV_FIELD,
115
+    MV_PMODE_INTFR_4MV,
116
+    MV_PMODE_INTFR_INTRA,
117
+};
118
+//@}
119
+
108 120
 /** @name MV types for B frames */
109 121
 //@{
110 122
 enum BMVTypes {
111 123
     BMV_TYPE_BACKWARD,
112 124
     BMV_TYPE_FORWARD,
113
-    BMV_TYPE_INTERPOLATED
125
+    BMV_TYPE_INTERPOLATED,
126
+    BMV_TYPE_DIRECT
114 127
 };
115 128
 //@}
116 129
 
... ...
@@ -260,16 +273,18 @@ typedef struct VC1Context{
260 260
      * -# 2 -> [-512, 511.f] x [-128, 127.f]
261 261
      * -# 3 -> [-1024, 1023.f] x [-256, 255.f]
262 262
      */
263
-    uint8_t mvrange;
263
+    uint8_t mvrange;              ///< Extended MV range flag
264 264
     uint8_t pquantizer;           ///< Uniform (over sequence) quantizer in use
265 265
     VLC *cbpcy_vlc;               ///< CBPCY VLC table
266
-    int tt_index;                 ///< Index for Transform Type tables
266
+    int tt_index;                 ///< Index for Transform Type tables (to decode TTMB)
267 267
     uint8_t* mv_type_mb_plane;    ///< bitplane for mv_type == (4MV)
268 268
     uint8_t* direct_mb_plane;     ///< bitplane for "direct" MBs
269
+    uint8_t* forward_mb_plane;    ///< bitplane for "forward" MBs
269 270
     int mv_type_is_raw;           ///< mv type mb plane is not coded
270 271
     int dmb_is_raw;               ///< direct mb plane is raw
272
+    int fmb_is_raw;               ///< forward mb plane is raw
271 273
     int skip_is_raw;              ///< skip mb plane is not coded
272
-    uint8_t luty[256], lutuv[256]; // lookup tables used for intensity compensation
274
+    uint8_t luty[256], lutuv[256];///< lookup tables used for intensity compensation
273 275
     int use_ic;                   ///< use intensity compensation in B-frames
274 276
     int rnd;                      ///< rounding control
275 277
 
... ...
@@ -307,6 +322,44 @@ typedef struct VC1Context{
307 307
     uint8_t range_mapuv;
308 308
     //@}
309 309
 
310
+    /** Frame decoding info for interlaced picture */
311
+    uint8_t dmvrange;   ///< Extended differential MV range flag
312
+    int fourmvswitch;
313
+    int intcomp;
314
+    uint8_t lumscale2;  ///< for interlaced field P picture
315
+    uint8_t lumshift2;
316
+    uint8_t luty2[256], lutuv2[256]; // lookup tables used for intensity compensation
317
+    VLC* mbmode_vlc;
318
+    VLC* imv_vlc;
319
+    VLC* twomvbp_vlc;
320
+    VLC* fourmvbp_vlc;
321
+    uint8_t twomvbp;
322
+    uint8_t fourmvbp;
323
+    uint8_t* fieldtx_plane;
324
+    int fieldtx_is_raw;
325
+    int8_t zzi_8x8[64];
326
+    uint8_t *blk_mv_type_base, *blk_mv_type;    ///< 0: frame MV, 1: field MV (interlaced frame)
327
+    uint8_t *mv_f_base, *mv_f[2];               ///< 0: MV obtained from same field, 1: opposite field
328
+    uint8_t *mv_f_last_base, *mv_f_last[2];
329
+    uint8_t *mv_f_next_base, *mv_f_next[2];
330
+    int field_mode;     ///< 1 for interlaced field pictures
331
+    int fptype;
332
+    int second_field;
333
+    int refdist;        ///< distance of the current picture from reference
334
+    int numref;         ///< number of past field pictures used as reference
335
+                        // 0 corresponds to 1 and 1 corresponds to 2 references
336
+    int reffield;       ///< if numref = 0 (1 reference) then reffield decides which
337
+                        // field to use among the two fields from previous frame
338
+    int intcompfield;   ///< which of the two fields to be intensity compensated
339
+                        // 0: both fields, 1: bottom field, 2: top field
340
+    int cur_field_type;     ///< 0: top, 1: bottom
341
+    int ref_field_type[2];  ///< forward and backward reference field type (top or bottom)
342
+    int blocks_off, mb_off;
343
+    int qs_last;        ///< if qpel has been used in the previous (tr.) picture
344
+    int bmvtype;
345
+    int frfd, brfd;     ///< reference frame distance (forward or backward)
346
+    int pic_header_flag;
347
+
310 348
     /** Frame decoding info for sprite modes */
311 349
     //@{
312 350
     int new_sprite;
... ...
@@ -1,5 +1,6 @@
1 1
 /*
2 2
  * VC-1 and WMV3 decoder
3
+ * copyright (c) 2011 Mashiat Sarker Shakkhar
3 4
  * copyright (c) 2006 Konstantin Shishkov
4 5
  * (c) 2005 anonymous, Alex Beregszaszi, Michael Niedermayer
5 6
  *
... ...
@@ -48,6 +49,40 @@ const uint8_t ff_vc1_mv_pmode_table2[2][4] = {
48 48
   { MV_PMODE_1MV, MV_PMODE_MIXED_MV, MV_PMODE_1MV_HPEL, MV_PMODE_1MV_HPEL_BILIN }
49 49
 };
50 50
 
51
+/* MBMODE table for interlaced frame P-picture */
52
+const uint8_t ff_vc1_mbmode_intfrp[2][15][4] = {
53
+    { /* 1: 4-MV, 0: non-4-MV */
54
+        /* Type, FIELDTX, 1-MV Differential present, Residuals (CBP) present */
55
+        /* Table 164 - Table 167 */
56
+        { MV_PMODE_INTFR_1MV        , 0, 1, 1},
57
+        { MV_PMODE_INTFR_1MV        , 1, 1, 1},
58
+        { MV_PMODE_INTFR_1MV        , 0, 1, 0},
59
+        { MV_PMODE_INTFR_1MV        , 0, 0, 1},
60
+        { MV_PMODE_INTFR_1MV        , 1, 0, 1},
61
+        { MV_PMODE_INTFR_2MV_FIELD  , 0, 0, 1},
62
+        { MV_PMODE_INTFR_2MV_FIELD  , 1, 0, 1},
63
+        { MV_PMODE_INTFR_2MV_FIELD  , 0, 0, 0},
64
+        { MV_PMODE_INTFR_INTRA      , 0, 0, 0} },
65
+    {
66
+        /* Table 160 - Table 163 */
67
+        { MV_PMODE_INTFR_1MV        , 0, 1, 1 },
68
+        { MV_PMODE_INTFR_1MV        , 1, 1, 1 },
69
+        { MV_PMODE_INTFR_1MV        , 0, 1, 0 },
70
+        { MV_PMODE_INTFR_1MV        , 0, 0, 1 },
71
+        { MV_PMODE_INTFR_1MV        , 1, 0, 1 },
72
+        { MV_PMODE_INTFR_2MV_FIELD  , 0, 0, 1 },
73
+        { MV_PMODE_INTFR_2MV_FIELD  , 1, 0, 1 },
74
+        { MV_PMODE_INTFR_2MV_FIELD  , 0, 0, 0 },
75
+        { MV_PMODE_INTFR_4MV        , 0, 0, 1 },
76
+        { MV_PMODE_INTFR_4MV        , 1, 0, 1 },
77
+        { MV_PMODE_INTFR_4MV        , 0, 0, 0 },
78
+        { MV_PMODE_INTFR_4MV_FIELD  , 0, 0, 1 },
79
+        { MV_PMODE_INTFR_4MV_FIELD  , 1, 0, 1 },
80
+        { MV_PMODE_INTFR_4MV_FIELD  , 0, 0, 0 },
81
+        { MV_PMODE_INTFR_INTRA      , 0, 0, 0 }
82
+    }
83
+};
84
+
51 85
 const int ff_vc1_fps_nr[5] = { 24, 25, 30, 50, 60 },
52 86
   ff_vc1_fps_dr[2] = { 1000, 1001 };
53 87
 const uint8_t ff_vc1_pquant_table[3][32] = {
... ...
@@ -84,14 +119,33 @@ VLC ff_vc1_ttmb_vlc[3];
84 84
 VLC ff_vc1_mv_diff_vlc[4];
85 85
 #define VC1_CBPCY_P_VLC_BITS 9 //14
86 86
 VLC ff_vc1_cbpcy_p_vlc[4];
87
+#define VC1_ICBPCY_VLC_BITS 9
88
+VLC ff_vc1_icbpcy_vlc[8];
87 89
 #define VC1_4MV_BLOCK_PATTERN_VLC_BITS 6
88 90
 VLC ff_vc1_4mv_block_pattern_vlc[4];
91
+#define VC1_2MV_BLOCK_PATTERN_VLC_BITS 3
92
+VLC ff_vc1_2mv_block_pattern_vlc[4];
89 93
 #define VC1_TTBLK_VLC_BITS 5
90 94
 VLC ff_vc1_ttblk_vlc[3];
91 95
 #define VC1_SUBBLKPAT_VLC_BITS 6
92 96
 VLC ff_vc1_subblkpat_vlc[3];
97
+#define VC1_INTFR_4MV_MBMODE_VLC_BITS 9
98
+VLC ff_vc1_intfr_4mv_mbmode_vlc[4];
99
+#define VC1_INTFR_NON4MV_MBMODE_VLC_BITS 6
100
+VLC ff_vc1_intfr_non4mv_mbmode_vlc[4];
101
+#define VC1_IF_MMV_MBMODE_VLC_BITS 5
102
+VLC ff_vc1_if_mmv_mbmode_vlc[8];
103
+#define VC1_IF_1MV_MBMODE_VLC_BITS 5
104
+VLC ff_vc1_if_1mv_mbmode_vlc[8];
105
+#define VC1_1REF_MVDATA_VLC_BITS 9
106
+VLC ff_vc1_1ref_mvdata_vlc[4];
107
+#define VC1_2REF_MVDATA_VLC_BITS 9
108
+VLC ff_vc1_2ref_mvdata_vlc[8];
93 109
 
94 110
 VLC ff_vc1_ac_coeff_table[8];
111
+
112
+#define VC1_IF_MBMODE_VLC_BITS 5    // as a placeholder for VC1_IF_MMV_MBMODE_VLC_BITS
113
+                                    // or VC1_IF_1MV_MBMODE_VLC_BITS since they are the same
95 114
 //@}
96 115
 
97 116
 
... ...
@@ -202,6 +256,273 @@ const uint8_t ff_vc1_4mv_block_pattern_bits[4][16] = {
202 202
   { 2, 4, 4, 4, 4, 4, 5, 5, 4, 5, 4, 5, 4, 5, 5, 4}
203 203
 };
204 204
 
205
+/* 2MV Block pattern VLC tables */
206
+const uint8_t ff_vc1_2mv_block_pattern_codes[4][4] = {
207
+  { 2, 1, 0, 3}, { 1, 0, 2, 3}, { 2, 0, 3, 1}, { 1, 3, 2, 0}
208
+};
209
+
210
+const uint8_t ff_vc1_2mv_block_pattern_bits[4][4] = {
211
+  { 2, 2, 2, 2}, { 1, 2, 3, 3}, { 3, 2, 3, 1}, { 1, 3, 3, 2}
212
+};
213
+
214
+/* Interlaced frame picture 4MV MBMODE VLC tables (p. 246, p. 360) */
215
+const uint16_t ff_vc1_intfr_4mv_mbmode_codes[4][15] = {
216
+  {   22,   17,    0,   47,   32,   10,    1,    3,   67,  133,  132,   92,   19,   93,   18},
217
+  {    3,   45,    0,    7,   23,    6,    1,    2,   10,   39,   44,    8,   18,   77,   76},
218
+  {   15,    6,   28,    9,   41,    6,    2,   15,   14,    8,   40,   29,    0,   21,   11},
219
+  {    7,  198,    1,    2,  193,   13,   25,    0,   97, 1599,   98,  398,  798,  192, 1598}
220
+};
221
+
222
+const uint8_t ff_vc1_intfr_4mv_mbmode_bits[4][15] = {
223
+  {   5,   5,   2,   6,   6,   4,   2,   2,   7,   8,   8,   7,   5,   7,   5},
224
+  {   3,   6,   3,   3,   5,   3,   3,   3,   4,   6,   6,   4,   5,   7,   7},
225
+  {   4,   3,   5,   5,   7,   4,   2,   5,   5,   5,   7,   5,   2,   6,   5},
226
+  {   4,   9,   1,   3,   9,   5,   6,   2,   8,  12,   8,  10,  11,   9,  12}
227
+};
228
+
229
+/* Interlaced frame picture NON-4MV MBMODE VLC tables (p. 363) */
230
+const uint8_t ff_vc1_intfr_non4mv_mbmode_codes[4][9] = {
231
+  {  9, 22,  0, 17, 16, 10,  1,  3, 23},
232
+  {  7,  0,  5,  2,  1,  1,  6,  3,  4},
233
+  {  1,  0, 10, 23, 44,  8,  3,  9, 45},
234
+  {  7, 97,  1,  2, 49, 13, 25,  0, 96}
235
+};
236
+
237
+const uint8_t ff_vc1_intfr_non4mv_mbmode_bits[4][9] = {
238
+  {  4,  5,  2,  5,  5,  4,  2,  2,  5},
239
+  {  3,  4,  6,  2,  3,  2,  3,  5,  6},
240
+  {  2,  2,  4,  5,  6,  4,  2,  4,  6},
241
+  {  4,  8,  1,  3,  7,  5,  6,  2,  8}
242
+};
243
+
244
+/* Interlaced field picture MBMODE VLC tables (p. 356 - 11.4.1, 11.4.2) */
245
+/* mixed-MV */
246
+const uint8_t ff_vc1_if_mmv_mbmode_codes[8][8] = {
247
+  { 16, 17,  3,  3,  0,  5,  9,  2 },
248
+  {  8,  9,  3,  6,  7,  0,  5,  2 },
249
+  { 16, 17,  5,  3,  0,  3,  9,  2 },
250
+  { 56, 57, 15,  4,  5,  6, 29,  0 },
251
+  { 52, 53, 27, 14, 15,  2, 12,  0 },
252
+  { 56, 57, 29,  5,  6,  0, 15,  4 },
253
+  { 16, 17,  6,  7,  0,  1,  9,  5 },
254
+  { 56, 57,  0,  5,  6, 29,  4, 15 }
255
+};
256
+const uint8_t ff_vc1_if_mmv_mbmode_bits[8][8] = {
257
+  { 6, 6, 2, 3, 2, 4, 5, 2 },
258
+  { 5, 5, 3, 3, 3, 2, 4, 2 },
259
+  { 6, 6, 4, 3, 2, 2, 5, 2 },
260
+  { 6, 6, 4, 3, 3, 3, 5, 1 },
261
+  { 6, 6, 5, 4, 4, 2, 4, 1 },
262
+  { 6, 6, 5, 3, 3, 1, 4, 3 },
263
+  { 5, 5, 3, 3, 2, 2, 4, 3 },
264
+  { 6, 6, 1, 3, 3, 5, 3, 4 }
265
+};
266
+/* 1MV */
267
+const uint8_t ff_vc1_if_1mv_mbmode_codes[8][6] = {
268
+  {  0,  1,  1,  1,  1,  1 },
269
+  {  0,  1,  1,  1,  1,  1 },
270
+  { 16, 17,  3,  0,  9,  5 },
271
+  { 20, 21,  3, 11,  0,  4 },
272
+  {  4,  5,  2,  3,  3,  0 },
273
+  {  4,  5,  3,  2,  0,  3 },
274
+  {  0,  1,  1,  1,  1,  1 },
275
+  { 16, 17,  9,  5,  3,  0 }
276
+};
277
+const uint8_t ff_vc1_if_1mv_mbmode_bits[8][6] = {
278
+  { 5, 5, 1, 3, 2, 4 },
279
+  { 5, 5, 1, 2, 3, 4 },
280
+  { 5, 5, 2, 1, 4, 3 },
281
+  { 5, 5, 2, 4, 1, 3 },
282
+  { 4, 4, 2, 3, 2, 2 },
283
+  { 4, 4, 3, 2, 2, 2 },
284
+  { 5, 5, 3, 4, 1, 2 },
285
+  { 5, 5, 4, 3, 2, 1 }
286
+};
287
+
288
+/* Interlaced frame/field picture MVDATA VLC tables */
289
+
290
+/* 1-reference tables */
291
+const uint32_t ff_vc1_1ref_mvdata_codes[4][72] = { /* uint32_t may be too big */
292
+  {5, 12, 30, 18, 12, 52, 117, 112, 0, 8, 27, 8, 29, 124, 214, 478, 431, 5, 27, 38, 30, 18, 118, 77,
293
+   502, 500, 57, 127, 39, 106, 113, 53, 113, 104, 476, 39, 115, 255, 232, 233, 126, 505, 501, 509, 62, 458, 1017, 76,
294
+   105, 506, 479, 503, 112, 477, 3661, 1831, 914, 456, 459, 1016, 430, 504, 507, 58574, 58575, 29280, 29281, 29282, 29283, 29284, 29285, 29286},
295
+  {7, 1, 7, 22, 1, 69, 24, 694, 6, 4, 23, 16, 41, 44, 346, 102, 414, 9, 40, 23, 0, 42, 4, 91,
296
+   181, 206, 6, 68, 15, 70, 14, 172, 50, 55, 4587, 10, 26, 287, 22, 20, 43, 360, 85, 9173, 87, 47, 54, 46,
297
+   361, 84, 1147, 415, 11133, 142, 2782, 1145, 1390, 2292, 5567, 1144, 9172, 44529, 22265, 712462, 712463, 356224, 356225, 356226, 356227, 356228, 356229, 356230},
298
+  {2, 6, 7, 13, 7, 48, 255, 496, 2, 0, 5, 25, 30, 7, 99, 253, 35, 14, 27, 26, 6, 9, 24, 197,
299
+   51, 497, 2, 1019, 499, 34, 508, 66, 1571, 131, 1568, 125, 64, 67, 996, 997, 401, 4073, 261, 520, 252, 1572, 1570, 400,
300
+   1574, 2037, 3147, 8144, 4173, 101, 3138, 201, 1575, 3139, 3146, 4174, 8145, 4175, 1042, 66766, 66767, 33376, 33377, 33378, 33379, 33380, 33381, 33382},
301
+  {13, 1, 4, 0, 23, 5, 127, 77, 3, 17, 62, 59, 23, 103, 74, 195, 242, 10, 44, 50, 61, 21, 40, 147,
302
+   204, 150, 3, 117, 32, 45, 33, 41, 144, 464, 507, 28, 76, 96, 9, 8, 45, 159, 506, 317, 49, 252, 88, 146,
303
+   240, 241, 205, 389, 357, 78, 145, 233, 388, 465, 486, 151, 487, 179, 316, 5710, 5711, 2848, 2849, 2850, 2851, 2852, 2853, 2854}
304
+};
305
+
306
+const uint8_t ff_vc1_1ref_mvdata_bits[4][72] = {
307
+  {3, 4, 5, 5, 5, 6, 7, 7, 2, 4, 5, 5, 6, 7, 8, 9, 9, 4, 6, 6, 6, 6, 7, 8,
308
+   9, 9, 6, 8, 7, 7, 7, 7, 8, 8, 9, 6, 8, 8, 8, 8, 8, 9, 9, 9, 7, 10, 10, 8,
309
+   8, 9, 9, 9, 8, 9, 13, 12, 11, 10, 10, 10, 9, 9, 9, 17, 17, 16, 16, 16, 16, 16, 16, 16},
310
+  {3, 3, 4, 5, 5, 7, 8, 10, 3, 4, 5, 5, 6, 7, 9, 10, 12, 4, 6, 6, 5, 6, 6, 8,
311
+   9, 11, 4, 7, 7, 7, 7, 8, 9, 9, 13, 5, 8, 9, 8, 8, 9, 10, 10, 14, 7, 9, 9, 9,
312
+   10, 10, 11, 12, 14, 8, 12, 11, 11, 12, 13, 11, 14, 16, 15, 20, 20, 19, 19, 19, 19, 19, 19, 19},
313
+  {3, 4, 4, 4, 5, 6, 8, 9, 2, 4, 5, 5, 5, 6, 7, 8, 8, 4, 7, 7, 6, 6, 7, 8,
314
+   8, 9, 5, 10, 9, 8, 9, 9, 11, 10, 11, 7, 9, 9, 10, 10, 11, 12, 11, 12, 8, 11, 11, 11,
315
+   11, 11, 12, 13, 15, 9, 12, 10, 11, 12, 12, 15, 13, 15, 13, 19, 19, 18, 18, 18, 18, 18, 18, 18},
316
+  {4, 4, 4, 4, 5, 5, 7, 7, 3, 5, 6, 6, 6, 7, 7, 8, 8, 4, 6, 6, 6, 6, 7, 8,
317
+   8, 8, 4, 7, 6, 6, 6, 7, 8, 9, 9, 5, 7, 7, 6, 6, 7, 8, 9, 9, 6, 8, 8, 8,
318
+   8, 8, 8, 9, 10, 7, 8, 8, 9, 9, 9, 8, 9, 9, 9, 14, 14, 13, 13, 13, 13, 13, 13, 13}
319
+};
320
+
321
+/* 2-reference tables */
322
+const uint32_t ff_vc1_2ref_mvdata_codes[8][126] = { /* table 132 - table 139 */
323
+  {12, 28, 11, 0, 14, 42, 80, 872, 2, 26, 4, 58, 29, 108,
324
+   239, 444, 351, 15, 3, 28, 13, 11, 62, 167, 326, 409, 6, 31,
325
+   4, 60, 7, 446, 139, 44, 1971, 5, 219, 86, 236, 82, 445, 120,
326
+   207, 1395, 9, 35, 237, 24, 6, 68, 245, 121, 1746, 110, 43, 349,
327
+   23, 895, 324, 206, 40, 171, 16, 437, 247, 166, 123, 40, 493, 489,
328
+   1789, 4, 245, 41, 650, 651, 655, 3577, 821, 7813, 238, 701, 43, 984,
329
+   977, 408, 489, 1309, 180, 63, 1109, 555, 553, 1105, 1400, 1970, 1392, 341,
330
+   50, 976, 84, 1747, 1393, 1108, 820, 7153, 183, 41, 7812, 364, 411, 7152,
331
+   1401, 3907, 181, 2209, 42, 365, 2208, 1952, 977, 2789, 340, 2788, 2617, 2616},
332
+  {3, 9, 22, 16, 215, 821, 1396, 1365, 0, 29, 9, 23, 44, 173,
333
+   884, 1715, 1399, 15, 24, 10, 46, 34, 380, 3707, 7049, 5592, 8, 52,
334
+   109, 35, 450, 886, 723, 7242, 13066, 20, 106, 114, 108, 227, 411, 1855,
335
+   7408, 2881, 50, 230, 224, 207, 171, 412, 683, 3627, 5593, 111, 451, 175,
336
+   191, 172, 381, 1763, 3625, 6532, 84, 181, 378, 429, 409, 376, 856, 722,
337
+   7243, 91, 680, 817, 904, 907, 880, 1811, 3267, 7409, 441, 1519, 1848, 754,
338
+   827, 697, 1771, 1392, 3620, 925, 1442, 1443, 3709, 1518, 1849, 1364, 2725, 2724,
339
+   887, 7413, 3022, 3705, 1632, 1652, 1770, 3708, 3429, 758, 5594, 7048, 1441, 7412,
340
+   1510, 3624, 1397, 3428, 820, 13067, 5595, 2880, 3023, 3525, 3626, 1653, 1393, 1363},
341
+  {4, 2, 16, 3, 23, 69, 62, 126, 3, 2, 40, 30, 21, 71,
342
+   2, 333, 96, 11, 38, 36, 20, 50, 111, 195, 1329, 1765, 21, 63,
343
+   45, 1, 318, 221, 246, 773, 817, 14, 3, 52, 51, 26, 330, 197,
344
+   244, 1764, 1, 60, 125, 141, 157, 49, 110, 662, 205, 37, 329, 50,
345
+   137, 54, 136, 111, 3, 797, 14, 426, 638, 97, 334, 335, 103, 255,
346
+   387, 54, 855, 245, 198, 194, 665, 281, 561, 848, 44, 399, 1328, 663,
347
+   4, 440, 192, 634, 785, 156, 1569, 409, 796, 247, 995, 854, 393, 5,
348
+   107, 2242, 816, 1279, 1264, 849, 1266, 498, 883, 0, 3137, 2243, 2540, 994,
349
+   772, 1271, 1265, 496, 328, 3136, 2541, 2240, 2241, 1267, 1278, 254, 499, 425},
350
+  {0, 4, 47, 82, 16, 173, 1291, 400, 3, 22, 7, 13, 187, 371,
351
+   201, 1295, 5932, 3, 17, 5, 67, 35, 75, 814, 11867, 1154, 9, 42,
352
+   20, 42, 264, 1482, 1626, 8502, 8498, 11, 19, 65, 184, 372, 256, 5338,
353
+   16462, 5175, 43, 133, 167, 160, 332, 666, 812, 8499, 5162, 81, 644, 172,
354
+   258, 69, 68, 2075, 1630, 3255, 24, 1292, 530, 740, 515, 148, 290, 2074,
355
+   1621, 51, 698, 582, 578, 2670, 1036, 2056, 8500, 16463, 373, 1029, 583, 298,
356
+   2580, 699, 401, 2127, 5176, 175, 2967, 1155, 5179, 811, 579, 5163, 2392, 10687,
357
+   73, 2668, 5339, 1197, 5342, 2126, 5172, 599, 11866, 519, 5173, 5177, 3254, 5178,
358
+   404, 1620, 8501, 21372, 348, 576, 4114, 21373, 2393, 4248, 5174, 1631, 8230, 8503},
359
+  {5, 25, 22, 17, 62, 94, 239, 226, 0, 57, 43, 38, 40, 18,
360
+   194, 237, 285, 13, 49, 42, 37, 32, 92, 493, 589, 1904, 6, 122,
361
+   96, 79, 72, 57, 390, 531, 3782, 15, 38, 95, 117, 112, 39, 475,
362
+   966, 1935, 63, 166, 240, 58, 82, 78, 227, 473, 783, 16, 477, 167,
363
+   247, 34, 146, 964, 751, 1890, 121, 143, 474, 135, 232, 186, 374, 238,
364
+   944, 133, 281, 782, 264, 466, 268, 1907, 1060, 1076, 113, 1501, 449, 935,
365
+   295, 141, 539, 1970, 479, 984, 1892, 3812, 947, 1869, 472, 1500, 2122, 1177,
366
+   965, 7566, 1893, 1077, 1905, 450, 280, 956, 897, 903, 31539, 4247, 4246, 7885,
367
+   3737, 3868, 3869, 3813, 284, 31538, 15768, 7567, 3736, 3943, 957, 896, 1176, 902},
368
+  {13, 16, 46, 57, 13, 116, 237, 182, 1, 2, 0, 48, 41, 112,
369
+   243, 140, 358, 9, 51, 120, 6, 196, 11, 355, 204, 1470, 31, 47,
370
+   100, 24, 198, 10, 354, 704, 3827, 7, 15, 227, 202, 178, 399, 942,
371
+   1887, 3153, 21, 71, 238, 226, 234, 9, 362, 707, 1437, 61, 8, 473,
372
+   50, 14, 366, 812, 1627, 6507, 2, 15, 472, 141, 180, 484, 103, 791,
373
+   1940, 34, 958, 789, 52, 55, 734, 108, 3838, 1644, 40, 971, 940, 53,
374
+   363, 957, 705, 1580, 7678, 14, 1438, 1471, 218, 1577, 1412, 3767, 2826, 1645,
375
+   12, 1918, 1436, 1912, 1886, 1882, 1581, 823, 820, 407, 7767, 7652, 6506, 7766,
376
+   3152, 2879, 7764, 2827, 398, 438, 7765, 3252, 2878, 3766, 7653, 7679, 821, 439},
377
+  {1, 11, 25, 111, 42, 117, 2027, 355, 1, 14, 26, 62, 28, 45,
378
+   356, 2028, 357, 4, 6, 54, 127, 174, 344, 348, 1389, 1037584, 0, 4,
379
+   123, 243, 59, 2029, 691, 716, 1390, 24, 62, 23, 30, 175, 1015, 1391,
380
+   717, 1037585, 20, 173, 170, 20, 168, 339, 232, 510, 3535, 120, 440, 338,
381
+   254, 689, 349, 352, 1037586, 1037587, 122, 688, 485, 233, 252, 1766, 3528, 1412,
382
+   1037588, 171, 3550, 345, 1012, 3529, 3530, 506, 1037589, 1037590, 252, 511, 484, 175,
383
+   346, 359, 3531, 1413, 1037591, 1015, 16213, 1037592, 3548, 1414, 16214, 1037593, 16215, 1037594,
384
+   442, 1415, 1416, 3551, 690, 1037595, 3534, 1014, 1037596, 4052, 1037597, 1037598, 1037599, 518784,
385
+   518785, 1388, 518786, 518787, 886, 1417, 1418, 518788, 518789, 3549, 518790, 518791, 1419, 32425},
386
+  {3, 14, 15, 126, 98, 198, 3289, 1598, 2, 2, 0, 24, 12, 105,
387
+   57, 1799, 3198, 2, 13, 27, 15, 410, 1607, 6711, 214724, 13421, 1, 30,
388
+   127, 10, 225, 1633, 3300, 214725, 214726, 29, 48, 13, 203, 409, 800, 142,
389
+   25902, 214727, 62, 57, 53, 51, 415, 448, 3290, 214728, 214729, 11, 208, 414,
390
+   34, 56, 398, 798, 12948, 572, 50, 18, 19, 113, 413, 32, 3207, 3264,
391
+   214730, 824, 1619, 418, 810, 802, 3303, 132, 287, 214731, 805, 1609, 811, 119,
392
+   1608, 1602, 3206, 3212, 214732, 58, 6583, 67, 807, 140, 141, 3213, 214733, 214734,
393
+   823, 3301, 133, 806, 839, 3236, 3199, 3354, 214735, 808, 107360, 107361, 3288, 1676,
394
+   12949, 12950, 25903, 26328, 817, 1798, 573, 118, 3265, 898, 3302, 26329, 26330, 26331}
395
+};
396
+
397
+const uint8_t ff_vc1_2ref_mvdata_bits[8][126] = {
398
+  {4, 5, 5, 5, 6, 7, 8, 10, 2, 5, 5, 6, 6, 7,
399
+   8, 9, 10, 4, 5, 6, 6, 7, 8, 9, 10, 11, 4, 6,
400
+   6, 7, 7, 9, 9, 10, 12, 5, 8, 8, 8, 8, 9, 9,
401
+   10, 12, 5, 7, 8, 7, 7, 8, 9, 9, 11, 7, 9, 10,
402
+   9, 10, 10, 10, 10, 12, 6, 9, 9, 9, 9, 9, 10, 10,
403
+   11, 7, 10, 10, 11, 11, 11, 12, 12, 14, 8, 11, 10, 11,
404
+   11, 11, 11, 12, 12, 8, 12, 11, 11, 12, 12, 12, 12, 13,
405
+   8, 12, 11, 11, 12, 12, 12, 13, 12, 9, 14, 13, 11, 13,
406
+   12, 13, 12, 13, 9, 13, 13, 12, 12, 13, 13, 13, 13, 13},
407
+  {3, 4, 5, 6, 8, 10, 11, 11, 2, 5, 5, 6, 7, 8,
408
+   10, 11, 11, 4, 5, 5, 6, 7, 9, 12, 13, 13, 4, 6,
409
+   7, 7, 9, 10, 11, 13, 14, 5, 7, 7, 7, 8, 9, 11,
410
+   13, 13, 6, 8, 8, 8, 8, 9, 10, 12, 13, 7, 9, 8,
411
+   8, 8, 9, 11, 12, 13, 7, 9, 9, 9, 9, 9, 10, 11,
412
+   13, 8, 10, 10, 10, 10, 10, 11, 12, 13, 9, 11, 11, 10,
413
+   10, 10, 11, 11, 12, 10, 12, 12, 12, 11, 11, 11, 12, 12,
414
+   10, 13, 12, 12, 11, 11, 11, 12, 12, 10, 13, 13, 12, 13,
415
+   11, 12, 11, 12, 10, 14, 13, 13, 12, 12, 12, 11, 11, 11},
416
+  {4, 4, 5, 5, 6, 7, 8, 9, 2, 5, 6, 6, 6, 7,
417
+   7, 9, 9, 4, 6, 6, 6, 7, 8, 9, 11, 12, 5, 7,
418
+   7, 7, 9, 9, 10, 11, 12, 5, 7, 7, 7, 7, 9, 9,
419
+   10, 12, 5, 8, 8, 8, 8, 8, 9, 10, 10, 6, 9, 8,
420
+   8, 8, 8, 9, 9, 11, 6, 10, 10, 9, 9, 9, 9, 10,
421
+   10, 7, 11, 10, 9, 9, 10, 9, 10, 11, 7, 10, 11, 10,
422
+   10, 10, 9, 10, 11, 8, 12, 11, 11, 10, 11, 11, 10, 10,
423
+   8, 12, 12, 11, 11, 11, 11, 10, 11, 8, 13, 12, 12, 11,
424
+   11, 11, 11, 10, 9, 13, 12, 12, 12, 11, 11, 10, 10, 10},
425
+  {3, 4, 6, 7, 7, 9, 11, 11, 2, 5, 5, 6, 8, 9,
426
+   10, 11, 13, 3, 5, 5, 7, 8, 9, 12, 14, 13, 4, 6,
427
+   6, 7, 9, 11, 13, 14, 14, 5, 7, 7, 8, 9, 9, 13,
428
+   15, 13, 6, 8, 8, 8, 9, 10, 12, 14, 13, 7, 10, 9,
429
+   9, 9, 9, 12, 13, 14, 7, 11, 10, 10, 10, 10, 11, 12,
430
+   13, 8, 11, 12, 12, 12, 11, 12, 14, 15, 9, 11, 12, 11,
431
+   12, 11, 11, 12, 13, 9, 12, 13, 13, 12, 12, 13, 14, 14,
432
+   9, 12, 13, 13, 13, 12, 13, 12, 14, 10, 13, 13, 14, 13,
433
+   11, 13, 14, 15, 10, 12, 13, 15, 14, 13, 13, 13, 14, 14},
434
+  {4, 5, 5, 5, 6, 7, 8, 8, 2, 6, 6, 6, 6, 6,
435
+   8, 9, 10, 4, 6, 6, 6, 6, 7, 9, 10, 11, 4, 7,
436
+   7, 7, 7, 7, 9, 10, 12, 5, 7, 7, 7, 7, 7, 9,
437
+   10, 11, 6, 8, 8, 7, 7, 7, 8, 9, 10, 6, 9, 8,
438
+   8, 7, 8, 10, 10, 11, 7, 9, 9, 8, 8, 8, 9, 9,
439
+   10, 8, 10, 10, 9, 9, 9, 11, 11, 11, 8, 11, 10, 10,
440
+   9, 9, 10, 11, 10, 10, 12, 12, 11, 11, 10, 11, 12, 11,
441
+   10, 13, 12, 11, 11, 10, 10, 11, 11, 11, 15, 13, 13, 13,
442
+   12, 12, 12, 12, 10, 15, 14, 13, 12, 12, 11, 11, 11, 11},
443
+  {4, 5, 6, 6, 6, 7, 8, 8, 2, 4, 5, 6, 6, 7,
444
+   8, 8, 9, 4, 6, 7, 7, 8, 8, 9, 10, 11, 5, 6,
445
+   7, 7, 8, 8, 9, 10, 12, 5, 7, 8, 8, 8, 9, 10,
446
+   11, 12, 5, 7, 8, 8, 8, 8, 9, 10, 11, 6, 8, 9,
447
+   8, 8, 9, 10, 11, 13, 5, 8, 9, 8, 8, 9, 9, 10,
448
+   11, 6, 10, 10, 9, 9, 10, 10, 12, 13, 6, 10, 10, 9,
449
+   9, 10, 10, 11, 13, 7, 11, 11, 11, 11, 11, 12, 12, 13,
450
+   7, 11, 11, 11, 11, 11, 11, 12, 12, 9, 13, 13, 13, 13,
451
+   12, 12, 13, 12, 9, 12, 13, 12, 12, 12, 13, 13, 12, 12},
452
+  {3, 5, 6, 8, 9, 10, 12, 12, 1, 5, 6, 7, 8, 9,
453
+   12, 12, 12, 4, 6, 7, 8, 9, 12, 12, 14, 21, 4, 6,
454
+   8, 9, 9, 12, 13, 13, 14, 6, 9, 8, 8, 9, 13, 14,
455
+   13, 21, 6, 9, 9, 8, 9, 10, 11, 12, 13, 8, 10, 10,
456
+   11, 11, 12, 12, 21, 21, 8, 11, 10, 11, 11, 12, 13, 14,
457
+   21, 9, 13, 10, 11, 13, 13, 12, 21, 21, 9, 12, 10, 11,
458
+   12, 12, 13, 14, 21, 11, 15, 21, 13, 14, 15, 21, 15, 21,
459
+   10, 14, 14, 13, 13, 21, 13, 13, 21, 13, 21, 21, 21, 20,
460
+   20, 14, 20, 20, 11, 14, 14, 20, 20, 13, 20, 20, 14, 16},
461
+  {2, 5, 6, 8, 9, 10, 13, 13, 2, 4, 5, 6, 8, 9,
462
+   10, 13, 14, 3, 5, 7, 8, 10, 12, 15, 20, 16, 4, 6,
463
+   8, 8, 10, 12, 13, 20, 20, 7, 8, 8, 9, 10, 11, 12,
464
+   16, 20, 7, 8, 8, 8, 10, 11, 13, 20, 20, 8, 10, 10,
465
+   10, 10, 11, 12, 15, 14, 8, 9, 9, 9, 10, 10, 13, 13,
466
+   20, 11, 12, 11, 11, 11, 13, 12, 13, 20, 11, 12, 11, 11,
467
+   12, 12, 13, 13, 20, 10, 14, 11, 11, 12, 12, 13, 20, 20,
468
+   11, 13, 12, 11, 12, 13, 14, 14, 20, 11, 19, 19, 13, 13,
469
+   15, 15, 16, 16, 11, 13, 14, 11, 13, 12, 13, 16, 16, 16}
470
+};
471
+
205 472
 const uint8_t wmv3_dc_scale_table[32]={
206 473
     0, 2, 4, 8, 8, 8, 9, 9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21
207 474
 };
... ...
@@ -262,6 +583,112 @@ const uint8_t ff_vc1_cbpcy_p_bits[4][64] = {
262 262
   }
263 263
 };
264 264
 
265
+/* Interlaced CBPCY VLC tables (Table 124 - Table 131) */
266
+
267
+const uint16_t ff_vc1_icbpcy_p_codes[8][63] = {
268
+  {
269
+    12058, 12059, 6028, 144, 680, 681, 3015, 145, 682, 683, 1504, 74, 150,
270
+    151, 189, 146, 684, 685, 1505, 152, 306, 307, 377, 308, 618, 619, 764,
271
+    78, 64, 65, 43, 147, 686, 687, 1506, 310, 622, 623, 765, 158, 318,
272
+    319, 383, 80, 66, 67, 44, 81, 164, 165, 190, 83, 68, 69, 45,
273
+    84, 70, 71, 46, 3, 0, 1, 1
274
+  }, {
275
+    65, 66, 256, 67, 136, 137, 257, 69, 140, 141, 258, 16, 34,
276
+    35, 36, 71, 16, 17, 259, 37, 88, 89, 90, 91, 90, 91, 92,
277
+    12, 48, 49, 25, 9, 20, 21, 44, 92, 93, 94, 95, 38, 93,
278
+    94, 95, 13, 52, 53, 27, 20, 39, 42, 43, 14, 56, 57, 29,
279
+    15, 60, 61, 31, 5, 9, 0, 3
280
+  }, {
281
+    50, 51, 26, 38, 228, 229, 486, 39, 230, 231, 487, 14, 99,
282
+    108, 119, 40, 232, 233, 488, 123, 218, 219, 236, 245, 440, 441, 474,
283
+    33, 75, 84, 43, 41, 234, 235, 489, 74, 442, 443, 475, 32, 222,
284
+    223, 242, 34, 85, 88, 45, 15, 112, 113, 120, 35, 89, 92, 47,
285
+    36, 93, 98, 48, 2, 31, 6, 0
286
+  }, {
287
+    40, 41, 157, 0, 490, 491, 492, 1, 493, 494, 495, 5, 240,
288
+    241, 59, 2, 496, 497, 498, 63, 348, 349, 153, 16, 976, 977, 304,
289
+    15, 158, 159, 251, 3, 499, 500, 501, 17, 978, 979, 305, 9, 350,
290
+    351, 156, 16, 168, 169, 56, 6, 242, 243, 77, 17, 170, 171, 57,
291
+    18, 172, 173, 58, 6, 22, 23, 14
292
+  }, {
293
+    60, 61, 31, 10, 97, 98, 2, 11, 99, 100, 3, 7, 3,
294
+    4, 11, 12, 101, 102, 4, 18, 10, 11, 20, 27, 24, 25, 52,
295
+    44, 103, 104, 53, 13, 105, 108, 5, 96, 26, 27, 53, 19, 14,
296
+    15, 21, 45, 109, 110, 56, 8, 8, 9, 12, 46, 111, 114, 58,
297
+    47, 115, 0, 59, 7, 20, 21, 4
298
+  }, {
299
+    56, 57, 157, 10, 145, 146, 147, 11, 148, 149, 150, 3, 238,
300
+    239, 54, 12, 151, 152, 153, 8, 484, 485, 106, 24, 972, 973, 214,
301
+    14, 158, 159, 245, 13, 154, 155, 156, 25, 974, 975, 215, 9, 488,
302
+    489, 144, 15, 232, 233, 246, 5, 240, 241, 55, 16, 234, 235, 247,
303
+    17, 236, 237, 52, 0, 62, 63, 2
304
+  }, {
305
+    60, 61, 463, 0, 191, 224, 508, 1, 225, 226, 509, 9, 497,
306
+    498, 499, 2, 227, 228, 510, 17, 1006, 1007, 1008, 33, 2018, 2019, 2020,
307
+    24, 1015, 1022, 1023, 3, 229, 230, 128, 46, 2021, 2022, 2023, 22, 1012,
308
+    1013, 1014, 25, 258, 259, 260, 10, 500, 501, 502, 26, 261, 262, 263,
309
+    27, 376, 377, 462, 29, 189, 190, 496
310
+  }, {
311
+    3, 4, 438, 4, 46, 47, 14, 5, 48, 49, 15, 3, 10,
312
+    11, 20, 6, 50, 51, 16, 5, 48, 49, 50, 9, 102, 103, 104,
313
+    29, 439, 440, 441, 7, 52, 53, 17, 22, 105, 106, 107, 10, 54,
314
+    55, 216, 30, 442, 443, 444, 4, 21, 22, 23, 31, 445, 446, 447,
315
+    0, 16, 17, 18, 28, 217, 218, 19
316
+  }
317
+};
318
+
319
+const uint8_t ff_vc1_icbpcy_p_bits[8][63] = {
320
+  {
321
+    15, 15, 14, 9, 11, 11, 13, 9, 11, 11, 12, 8, 9,
322
+    9, 9, 9, 11, 11, 12, 9, 10, 10, 10, 10, 11, 11, 11,
323
+    8, 8, 8, 7, 9, 11, 11, 12, 10, 11, 11, 11, 9, 10,
324
+    10, 10, 8, 8, 8, 7, 8, 9, 9, 9, 8, 8, 8, 7,
325
+    8, 8, 8, 7, 3, 3, 3, 1
326
+  }, {
327
+    7, 7, 9, 7, 8, 8, 9, 7, 8, 8, 9, 6, 7,
328
+    7, 7, 7, 7, 7, 9, 7, 8, 8, 8, 8, 9, 9, 9,
329
+    6, 7, 7, 6, 6, 7, 7, 8, 8, 9, 9, 9, 7, 8,
330
+    8, 8, 6, 7, 7, 6, 6, 7, 7, 7, 6, 7, 7, 6,
331
+    6, 7, 7, 6, 3, 4, 3, 2
332
+  }, {
333
+    6, 6, 5, 6, 8, 8, 9, 6, 8, 8, 9, 5, 7,
334
+    7, 7, 6, 8, 8, 9, 7, 8, 8, 8, 8, 9, 9, 9,
335
+    6, 7, 7, 6, 6, 8, 8, 9, 7, 9, 9, 9, 6, 8,
336
+    8, 8, 6, 7, 7, 6, 5, 7, 7, 7, 6, 7, 7, 6,
337
+    6, 7, 7, 6, 3, 5, 4, 2
338
+  }, {
339
+    6, 6, 8, 4, 9, 9, 9, 4, 9, 9, 9, 4, 8,
340
+    8, 7, 4, 9, 9, 9, 6, 9, 9, 8, 6, 10, 10, 9,
341
+    5, 8, 8, 8, 4, 9, 9, 9, 6, 10, 10, 9, 5, 9,
342
+    9, 8, 5, 8, 8, 7, 4, 8, 8, 7, 5, 8, 8, 7,
343
+    5, 8, 8, 7, 3, 5, 5, 4
344
+  }, {
345
+    6, 6, 5, 5, 7, 7, 7, 5, 7, 7, 7, 5, 6,
346
+    6, 6, 5, 7, 7, 7, 6, 7, 7, 7, 7, 8, 8, 8,
347
+    6, 7, 7, 6, 5, 7, 7, 7, 7, 8, 8, 8, 6, 7,
348
+    7, 7, 6, 7, 7, 6, 5, 6, 6, 6, 6, 7, 7, 6,
349
+    6, 7, 6, 6, 4, 5, 5, 3
350
+  }, {
351
+    6, 6, 8, 4, 8, 8, 8, 4, 8, 8, 8, 4, 8,
352
+    8, 7, 4, 8, 8, 8, 5, 9, 9, 8, 6, 10, 10, 9,
353
+    5, 8, 8, 8, 4, 8, 8, 8, 6, 10, 10, 9, 5, 9,
354
+    9, 8, 5, 8, 8, 8, 4, 8, 8, 7, 5, 8, 8, 8,
355
+    5, 8, 8, 7, 3, 6, 6, 4
356
+  }, {
357
+    6, 6, 9, 3, 8, 8, 9, 3, 8, 8, 9, 4, 9,
358
+    9, 9, 3, 8, 8, 9, 5, 10, 10, 10, 6, 11, 11, 11,
359
+    5, 10, 10, 10, 3, 8, 8, 8, 6, 11, 11, 11, 5, 10,
360
+    10, 10, 5, 9, 9, 9, 4, 9, 9, 9, 5, 9, 9, 9,
361
+    5, 9, 9, 9, 5, 8, 8, 9
362
+  }, {
363
+    6, 6, 10, 3, 7, 7, 7, 3, 7, 7, 7, 4, 8,
364
+    8, 8, 3, 7, 7, 7, 5, 9, 9, 9, 6, 10, 10, 10,
365
+    6, 10, 10, 10, 3, 7, 7, 7, 6, 10, 10, 10, 5, 9,
366
+    9, 9, 6, 10, 10, 10, 4, 8, 8, 8, 6, 10, 10, 10,
367
+    5, 9, 9, 9, 6, 9, 9, 9
368
+  }
369
+};
370
+
265 371
 /* MacroBlock Transform Type: 7.1.3.11, p89
266 372
  * 8x8:B
267 373
  * 8x4:B:btm  8x4:B:top  8x4:B:both,
... ...
@@ -520,3 +947,38 @@ const int32_t ff_vc1_dqscale[63] = {
520 520
     0x14E6, 0x147B, 0x1414, 0x13B1, 0x1352, 0x12F7, 0x129E, 0x1249,
521 521
     0x11F7, 0x11A8, 0x115B, 0x1111, 0x10C9, 0x1084, 0x1000
522 522
 };
523
+
524
+/* P Interlaced field picture MV predictor scaling values (Table 114) */
525
+const uint16_t vc1_field_mvpred_scales[2][7][4] = {
526
+// Refdist  0       1       2       3 or greater
527
+  { // current field is first
528
+    { 128,    192,    213,    224},   // SCALEOPP
529
+    { 512,    341,    307,    293},   // SCALESAME1
530
+    { 219,    236,    242,    245},   // SCALESAME2
531
+    {  32,     48,     53,     56},   // SCALEZONE1_X
532
+    {   8,     12,     13,     14},   // SCALEZONE1_Y
533
+    {  37,     20,     14,     11},   // ZONE1OFFSET_X
534
+    {  10,      5,      4,      3}    // ZONE1OFFSET_Y
535
+  },
536
+  { // current field is second
537
+    { 128,     64,     43,     32},   // SCALEOPP
538
+    { 512,   1024,   1536,   2048},   // SCALESAME1
539
+    { 219,    204,    200,    198},   // SCALESAME2
540
+    {  32,     16,     11,      8},   // SCALEZONE1_X
541
+    {   8,      4,      3,      2},   // SCALEZONE1_Y
542
+    {  37,     52,     56,     58},   // ZONE1OFFSET_X
543
+    {  10,     13,     14,     15}    // ZONE1OFFSET_Y
544
+  }
545
+};
546
+
547
+/* B Interlaced field picture backward MV predictor scaling values for first field (Table 115) */
548
+const uint16_t vc1_b_field_mvpred_scales[7][4] = {
549
+// BRFD     0       1       2       3 or greater
550
+  { 171,    205,    219,    228},   // SCALESAME
551
+  { 384,    320,    299,    288},   // SCALEOPP1
552
+  { 230,    239,    244,    246},   // SCALEOPP2
553
+  {  43,     51,     55,     57},   // SCALEZONE1_X
554
+  {  11,     13,     14,     14},   // SCALEZONE1_Y
555
+  {  26,     17,     12,     10},   // ZONE1OFFSET_X
556
+  {   7,      4,      3,      3}    // ZONE1OFFSET_Y
557
+};
... ...
@@ -44,6 +44,9 @@ extern const uint8_t ff_vc1_mv_pmode_table2[2][4];
44 44
 extern const int ff_vc1_fps_nr[5], ff_vc1_fps_dr[2];
45 45
 extern const uint8_t ff_vc1_pquant_table[3][32];
46 46
 
47
+/* MBMODE table for interlaced frame P-picture */
48
+extern const uint8_t ff_vc1_mbmode_intfrp[2][15][4];
49
+
47 50
 /** @name VC-1 VLC tables and defines
48 51
  *  @todo TODO move this into the context
49 52
  */
... ...
@@ -63,14 +66,32 @@ extern VLC ff_vc1_ttmb_vlc[3];
63 63
 extern VLC ff_vc1_mv_diff_vlc[4];
64 64
 #define VC1_CBPCY_P_VLC_BITS 9 //14
65 65
 extern VLC ff_vc1_cbpcy_p_vlc[4];
66
+#define VC1_ICBPCY_VLC_BITS 9
67
+extern VLC ff_vc1_icbpcy_vlc[8];
66 68
 #define VC1_4MV_BLOCK_PATTERN_VLC_BITS 6
67 69
 extern VLC ff_vc1_4mv_block_pattern_vlc[4];
70
+#define VC1_2MV_BLOCK_PATTERN_VLC_BITS 3
71
+extern VLC ff_vc1_2mv_block_pattern_vlc[4];
68 72
 #define VC1_TTBLK_VLC_BITS 5
69 73
 extern VLC ff_vc1_ttblk_vlc[3];
70 74
 #define VC1_SUBBLKPAT_VLC_BITS 6
71 75
 extern VLC ff_vc1_subblkpat_vlc[3];
76
+#define VC1_INTFR_4MV_MBMODE_VLC_BITS 9
77
+extern VLC ff_vc1_intfr_4mv_mbmode_vlc[4];
78
+#define VC1_INTFR_NON4MV_MBMODE_VLC_BITS 6
79
+extern VLC ff_vc1_intfr_non4mv_mbmode_vlc[4];
80
+#define VC1_IF_MMV_MBMODE_VLC_BITS 5
81
+extern VLC ff_vc1_if_mmv_mbmode_vlc[8];
82
+#define VC1_IF_1MV_MBMODE_VLC_BITS 5
83
+extern VLC ff_vc1_if_1mv_mbmode_vlc[8];
84
+#define VC1_1REF_MVDATA_VLC_BITS 9
85
+extern VLC ff_vc1_1ref_mvdata_vlc[4];
86
+#define VC1_2REF_MVDATA_VLC_BITS 9
87
+extern VLC ff_vc1_2ref_mvdata_vlc[8];
72 88
 
73 89
 extern VLC ff_vc1_ac_coeff_table[8];
90
+
91
+#define VC1_IF_MBMODE_VLC_BITS 5
74 92
 //@}
75 93
 
76 94
 
... ...
@@ -101,12 +122,20 @@ extern const uint8_t ff_vc1_norm6_spec[64][5];
101 101
 extern const uint8_t ff_vc1_4mv_block_pattern_codes[4][16];
102 102
 extern const uint8_t ff_vc1_4mv_block_pattern_bits[4][16];
103 103
 
104
+/* 2MV Block pattern VLC tables */
105
+extern const uint8_t ff_vc1_2mv_block_pattern_codes[4][4];
106
+extern const uint8_t ff_vc1_2mv_block_pattern_bits[4][4];
107
+
104 108
 extern const uint8_t wmv3_dc_scale_table[32];
105 109
 
106 110
 /* P-Picture CBPCY VLC tables */
107 111
 extern const uint16_t ff_vc1_cbpcy_p_codes[4][64];
108 112
 extern const uint8_t ff_vc1_cbpcy_p_bits[4][64];
109 113
 
114
+/* Interlaced CBPCY VLC tables (Table 124 - Table 131) */
115
+extern const uint16_t ff_vc1_icbpcy_p_codes[8][63];
116
+extern const uint8_t ff_vc1_icbpcy_p_bits[8][63];
117
+
110 118
 /* MacroBlock Transform Type: 7.1.3.11, p89
111 119
  * 8x8:B
112 120
  * 8x4:B:btm  8x4:B:top  8x4:B:both,
... ...
@@ -131,6 +160,26 @@ extern const uint8_t ff_vc1_subblkpat_bits[3][15];
131 131
 extern const uint16_t ff_vc1_mv_diff_codes[4][73];
132 132
 extern const uint8_t ff_vc1_mv_diff_bits[4][73];
133 133
 
134
+/* Interlaced frame picture MBMODE VLC tables (p. 246, p. 360) */
135
+extern const uint16_t ff_vc1_intfr_4mv_mbmode_codes[4][15];
136
+extern const uint8_t ff_vc1_intfr_4mv_mbmode_bits[4][15];
137
+extern const uint8_t ff_vc1_intfr_non4mv_mbmode_codes[4][9];
138
+extern const uint8_t ff_vc1_intfr_non4mv_mbmode_bits[4][9];
139
+
140
+/* Interlaced field picture MBMODE VLC tables (p. 356 - 11.4.1, 11.4.2) */
141
+extern const uint8_t ff_vc1_if_mmv_mbmode_codes[8][8];
142
+extern const uint8_t ff_vc1_if_mmv_mbmode_bits[8][8];
143
+extern const uint8_t ff_vc1_if_1mv_mbmode_codes[8][6];
144
+extern const uint8_t ff_vc1_if_1mv_mbmode_bits[8][6];
145
+
146
+/* Interlaced frame/field picture MVDATA VLC tables */
147
+/* 1-reference tables */
148
+extern const uint32_t ff_vc1_1ref_mvdata_codes[4][72];
149
+extern const uint8_t ff_vc1_1ref_mvdata_bits[4][72];
150
+/* 2-reference tables */
151
+extern const uint32_t ff_vc1_2ref_mvdata_codes[8][126];
152
+extern const uint8_t ff_vc1_2ref_mvdata_bits[8][126];
153
+
134 154
 /* DC differentials low+hi-mo, p217 are the same as in msmpeg4data .h */
135 155
 
136 156
 /* Scantables/ZZ scan are at 11.9 (p262) and 8.1.1.12 (p10) */
... ...
@@ -141,8 +190,14 @@ extern const int8_t ff_vc1_adv_interlaced_8x8_zz [64];
141 141
 extern const int8_t ff_vc1_adv_interlaced_8x4_zz [32];
142 142
 extern const int8_t ff_vc1_adv_interlaced_4x8_zz [32];
143 143
 extern const int8_t ff_vc1_adv_interlaced_4x4_zz [16];
144
+extern const int8_t ff_vc1_intra_horz_8x8_zz [64];
145
+extern const int8_t ff_vc1_intra_vert_8x8_zz [64];
144 146
 
145 147
 /* DQScale as specified in 8.1.3.9 - almost identical to 0x40000/i */
146 148
 extern const int32_t ff_vc1_dqscale[63];
147 149
 
150
+/* P Interlaced field picture MV predictor scaling values (Table 114) */
151
+extern const uint16_t vc1_field_mvpred_scales[2][7][4];
152
+/* B Interlaced field picture backward MV predictor scaling values for first field (Table 115) */
153
+extern const uint16_t vc1_b_field_mvpred_scales[7][4];
148 154
 #endif /* AVCODEC_VC1DATA_H */
... ...
@@ -1,5 +1,6 @@
1 1
 /*
2 2
  * VC-1 and WMV3 decoder
3
+ * Copyright (c) 2011 Mashiat Sarker Shakkhar
3 4
  * Copyright (c) 2006-2007 Konstantin Shishkov
4 5
  * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
5 6
  *
... ...
@@ -48,11 +49,19 @@
48 48
 
49 49
 
50 50
 static const uint16_t vlc_offs[] = {
51
-       0,   520,   552,   616,  1128,  1160, 1224, 1740, 1772, 1836, 1900, 2436,
52
-    2986,  3050,  3610,  4154,  4218,  4746, 5326, 5390, 5902, 6554, 7658, 8620,
53
-    9262, 10202, 10756, 11310, 12228, 15078
51
+        0,   520,   552,   616,  1128,  1160,  1224,  1740,  1772,  1836,  1900,  2436,
52
+     2986,  3050,  3610,  4154,  4218,  4746,  5326,  5390,  5902,  6554,  7658,  8342,
53
+     9304,  9988, 10630, 11234, 12174, 13006, 13560, 14232, 14786, 15432, 16350, 17522,
54
+    20372, 21818, 22330, 22394, 23166, 23678, 23742, 24820, 25332, 25396, 26460, 26980,
55
+    27048, 27592, 27600, 27608, 27616, 27624, 28224, 28258, 28290, 28802, 28834, 28866,
56
+    29378, 29412, 29444, 29960, 29994, 30026, 30538, 30572, 30604, 31120, 31154, 31186,
57
+    31714, 31746, 31778, 32306, 32340, 32372
54 58
 };
55 59
 
60
+// offset tables for interlaced picture MVDATA decoding
61
+static const int offset_table1[9] = {   0,   1,   2,   4,   8,  16,  32,  64, 128};
62
+static const int offset_table2[9] = {   0,   1,   3,   7,  15,  31,  63, 127, 255};
63
+
56 64
 /**
57 65
  * Init VC-1 specific tables and VC1Context members
58 66
  * @param v The VC1Context to initialize
... ...
@@ -62,7 +71,7 @@ static int vc1_init_common(VC1Context *v)
62 62
 {
63 63
     static int done = 0;
64 64
     int i = 0;
65
-    static VLC_TYPE vlc_table[15078][2];
65
+    static VLC_TYPE vlc_table[32372][2];
66 66
 
67 67
     v->hrd_rate = v->hrd_buffer = NULL;
68 68
 
... ...
@@ -118,11 +127,64 @@ static int vc1_init_common(VC1Context *v)
118 118
                      ff_vc1_mv_diff_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
119 119
         }
120 120
         for(i=0; i<8; i++){
121
-            ff_vc1_ac_coeff_table[i].table = &vlc_table[vlc_offs[i+21]];
122
-            ff_vc1_ac_coeff_table[i].table_allocated = vlc_offs[i+22] - vlc_offs[i+21];
121
+            ff_vc1_ac_coeff_table[i].table = &vlc_table[vlc_offs[i*2+21]];
122
+            ff_vc1_ac_coeff_table[i].table_allocated = vlc_offs[i*2+22] - vlc_offs[i*2+21];
123 123
             init_vlc(&ff_vc1_ac_coeff_table[i], AC_VLC_BITS, vc1_ac_sizes[i],
124 124
                      &vc1_ac_tables[i][0][1], 8, 4,
125 125
                      &vc1_ac_tables[i][0][0], 8, 4, INIT_VLC_USE_NEW_STATIC);
126
+            /* initialize interlaced MVDATA tables (2-Ref) */
127
+            ff_vc1_2ref_mvdata_vlc[i].table = &vlc_table[vlc_offs[i*2+22]];
128
+            ff_vc1_2ref_mvdata_vlc[i].table_allocated = vlc_offs[i*2+23] - vlc_offs[i*2+22];
129
+            init_vlc(&ff_vc1_2ref_mvdata_vlc[i], VC1_2REF_MVDATA_VLC_BITS, 126,
130
+                     ff_vc1_2ref_mvdata_bits[i], 1, 1,
131
+                     ff_vc1_2ref_mvdata_codes[i], 4, 4, INIT_VLC_USE_NEW_STATIC);
132
+        }
133
+        for (i=0; i<4; i++) {
134
+            /* initialize 4MV MBMODE VLC tables for interlaced frame P picture */
135
+            ff_vc1_intfr_4mv_mbmode_vlc[i].table = &vlc_table[vlc_offs[i*3+37]];
136
+            ff_vc1_intfr_4mv_mbmode_vlc[i].table_allocated = vlc_offs[i*3+38] - vlc_offs[i*3+37];
137
+            init_vlc(&ff_vc1_intfr_4mv_mbmode_vlc[i], VC1_INTFR_4MV_MBMODE_VLC_BITS, 15,
138
+                     ff_vc1_intfr_4mv_mbmode_bits[i], 1, 1,
139
+                     ff_vc1_intfr_4mv_mbmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
140
+            /* initialize NON-4MV MBMODE VLC tables for the same */
141
+            ff_vc1_intfr_non4mv_mbmode_vlc[i].table = &vlc_table[vlc_offs[i*3+38]];
142
+            ff_vc1_intfr_non4mv_mbmode_vlc[i].table_allocated = vlc_offs[i*3+39] - vlc_offs[i*3+38];
143
+            init_vlc(&ff_vc1_intfr_non4mv_mbmode_vlc[i], VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 9,
144
+                     ff_vc1_intfr_non4mv_mbmode_bits[i], 1, 1,
145
+                     ff_vc1_intfr_non4mv_mbmode_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
146
+            /* initialize interlaced MVDATA tables (1-Ref) */
147
+            ff_vc1_1ref_mvdata_vlc[i].table = &vlc_table[vlc_offs[i*3+39]];
148
+            ff_vc1_1ref_mvdata_vlc[i].table_allocated = vlc_offs[i*3+40] - vlc_offs[i*3+39];
149
+            init_vlc(&ff_vc1_1ref_mvdata_vlc[i], VC1_1REF_MVDATA_VLC_BITS, 72,
150
+                     ff_vc1_1ref_mvdata_bits[i], 1, 1,
151
+                     ff_vc1_1ref_mvdata_codes[i], 4, 4, INIT_VLC_USE_NEW_STATIC);
152
+        }
153
+        for (i=0; i<4; i++) {
154
+            /* Initialize 2MV Block pattern VLC tables */
155
+            ff_vc1_2mv_block_pattern_vlc[i].table = &vlc_table[vlc_offs[i+49]];
156
+            ff_vc1_2mv_block_pattern_vlc[i].table_allocated = vlc_offs[i+50] - vlc_offs[i+49];
157
+            init_vlc(&ff_vc1_2mv_block_pattern_vlc[i], VC1_2MV_BLOCK_PATTERN_VLC_BITS, 4,
158
+                    ff_vc1_2mv_block_pattern_bits[i], 1, 1,
159
+                    ff_vc1_2mv_block_pattern_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
160
+        }
161
+        for (i=0; i<8; i++) {
162
+            /* Initialize interlaced CBPCY VLC tables (Table 124 - Table 131) */
163
+            ff_vc1_icbpcy_vlc[i].table = &vlc_table[vlc_offs[i*3+53]];
164
+            ff_vc1_icbpcy_vlc[i].table_allocated = vlc_offs[i*3+54] - vlc_offs[i*3+53];
165
+            init_vlc(&ff_vc1_icbpcy_vlc[i], VC1_ICBPCY_VLC_BITS, 63,
166
+                    ff_vc1_icbpcy_p_bits[i], 1, 1,
167
+                    ff_vc1_icbpcy_p_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
168
+            /* Initialize interlaced field picture MBMODE VLC tables */
169
+            ff_vc1_if_mmv_mbmode_vlc[i].table = &vlc_table[vlc_offs[i*3+54]];
170
+            ff_vc1_if_mmv_mbmode_vlc[i].table_allocated = vlc_offs[i*3+55] - vlc_offs[i*3+54];
171
+            init_vlc(&ff_vc1_if_mmv_mbmode_vlc[i], VC1_IF_MMV_MBMODE_VLC_BITS, 8,
172
+                    ff_vc1_if_mmv_mbmode_bits[i], 1, 1,
173
+                    ff_vc1_if_mmv_mbmode_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
174
+            ff_vc1_if_1mv_mbmode_vlc[i].table = &vlc_table[vlc_offs[i*3+55]];
175
+            ff_vc1_if_1mv_mbmode_vlc[i].table_allocated = vlc_offs[i*3+56] - vlc_offs[i*3+55];
176
+            init_vlc(&ff_vc1_if_1mv_mbmode_vlc[i], VC1_IF_1MV_MBMODE_VLC_BITS, 6,
177
+                    ff_vc1_if_1mv_mbmode_bits[i], 1, 1,
178
+                    ff_vc1_if_1mv_mbmode_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
126 179
         }
127 180
         done = 1;
128 181
     }
... ...
@@ -162,6 +224,9 @@ enum Imode {
162 162
 static void vc1_put_signed_blocks_clamped(VC1Context *v)
163 163
 {
164 164
     MpegEncContext *s = &v->s;
165
+    int topleft_mb_pos, top_mb_pos;
166
+    int stride_y, fieldtx;
167
+    int v_dist;
165 168
 
166 169
     /* The put pixels loop is always one MB row behind the decoding loop,
167 170
      * because we can only put pixels when overlap filtering is done, and
... ...
@@ -172,18 +237,22 @@ static void vc1_put_signed_blocks_clamped(VC1Context *v)
172 172
      * of the right MB edge, we need the next MB present. */
173 173
     if (!s->first_slice_line) {
174 174
         if (s->mb_x) {
175
+            topleft_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x - 1;
176
+            fieldtx = v->fieldtx_plane[topleft_mb_pos];
177
+            stride_y = (s->linesize) << fieldtx;
178
+            v_dist = (16 - fieldtx) >> (fieldtx == 0);
175 179
             s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][0],
176 180
                                              s->dest[0] - 16 * s->linesize - 16,
177
-                                             s->linesize);
181
+                                             stride_y);
178 182
             s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][1],
179 183
                                              s->dest[0] - 16 * s->linesize - 8,
180
-                                             s->linesize);
184
+                                             stride_y);
181 185
             s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][2],
182
-                                             s->dest[0] - 8 * s->linesize - 16,
183
-                                             s->linesize);
186
+                                             s->dest[0] - v_dist * s->linesize - 16,
187
+                                             stride_y);
184 188
             s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][3],
185
-                                             s->dest[0] - 8 * s->linesize - 8,
186
-                                             s->linesize);
189
+                                             s->dest[0] - v_dist * s->linesize - 8,
190
+                                             stride_y);
187 191
             s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][4],
188 192
                                              s->dest[1] - 8 * s->uvlinesize - 8,
189 193
                                              s->uvlinesize);
... ...
@@ -192,18 +261,22 @@ static void vc1_put_signed_blocks_clamped(VC1Context *v)
192 192
                                              s->uvlinesize);
193 193
         }
194 194
         if (s->mb_x == s->mb_width - 1) {
195
+            top_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x;
196
+            fieldtx = v->fieldtx_plane[top_mb_pos];
197
+            stride_y = s->linesize << fieldtx;
198
+            v_dist = fieldtx ? 15 : 8;
195 199
             s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][0],
196 200
                                              s->dest[0] - 16 * s->linesize,
197
-                                             s->linesize);
201
+                                             stride_y);
198 202
             s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][1],
199 203
                                              s->dest[0] - 16 * s->linesize + 8,
200
-                                             s->linesize);
204
+                                             stride_y);
201 205
             s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][2],
202
-                                             s->dest[0] - 8 * s->linesize,
203
-                                             s->linesize);
206
+                                             s->dest[0] - v_dist * s->linesize,
207
+                                             stride_y);
204 208
             s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][3],
205
-                                             s->dest[0] - 8 * s->linesize + 8,
206
-                                             s->linesize);
209
+                                             s->dest[0] - v_dist * s->linesize + 8,
210
+                                             stride_y);
207 211
             s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][4],
208 212
                                              s->dest[1] - 8 * s->uvlinesize,
209 213
                                              s->uvlinesize);
... ...
@@ -406,33 +479,61 @@ static void vc1_mc_1mv(VC1Context *v, int dir)
406 406
     DSPContext *dsp = &v->s.dsp;
407 407
     uint8_t *srcY, *srcU, *srcV;
408 408
     int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
409
-
410
-    if(!v->s.last_picture.f.data[0])return;
409
+    int off, off_uv;
410
+    int v_edge_pos = s->v_edge_pos >> v->field_mode;
411
+    if (!v->field_mode && !v->s.last_picture.f.data[0])
412
+        return;
411 413
 
412 414
     mx = s->mv[dir][0][0];
413 415
     my = s->mv[dir][0][1];
414 416
 
415 417
     // store motion vectors for further use in B frames
416 418
     if(s->pict_type == AV_PICTURE_TYPE_P) {
417
-        s->current_picture.f.motion_val[1][s->block_index[0]][0] = mx;
418
-        s->current_picture.f.motion_val[1][s->block_index[0]][1] = my;
419
+        s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = mx;
420
+        s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = my;
419 421
     }
422
+
420 423
     uvmx = (mx + ((mx & 3) == 3)) >> 1;
421 424
     uvmy = (my + ((my & 3) == 3)) >> 1;
422 425
     v->luma_mv[s->mb_x][0] = uvmx;
423 426
     v->luma_mv[s->mb_x][1] = uvmy;
424
-    if(v->fastuvmc) {
427
+
428
+    if (v->field_mode &&
429
+        v->cur_field_type != v->ref_field_type[dir]) {
430
+        my = my - 2 + 4 * v->cur_field_type;
431
+        uvmy = uvmy - 2 + 4 * v->cur_field_type;
432
+    }
433
+
434
+    if(v->fastuvmc && (v->fcm != 1)) { // fastuvmc shall be ignored for interlaced frame picture
425 435
         uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
426 436
         uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
427 437
     }
428
-    if(!dir) {
429
-        srcY = s->last_picture.f.data[0];
430
-        srcU = s->last_picture.f.data[1];
431
-        srcV = s->last_picture.f.data[2];
438
+    if (v->field_mode) { // interlaced field picture
439
+        if (!dir) {
440
+            if ((v->cur_field_type != v->ref_field_type[dir]) && v->cur_field_type) {
441
+                srcY = s->current_picture.f.data[0];
442
+                srcU = s->current_picture.f.data[1];
443
+                srcV = s->current_picture.f.data[2];
444
+            } else {
445
+                srcY = s->last_picture.f.data[0];
446
+                srcU = s->last_picture.f.data[1];
447
+                srcV = s->last_picture.f.data[2];
448
+            }
449
+        } else {
450
+            srcY = s->next_picture.f.data[0];
451
+            srcU = s->next_picture.f.data[1];
452
+            srcV = s->next_picture.f.data[2];
453
+        }
432 454
     } else {
433
-        srcY = s->next_picture.f.data[0];
434
-        srcU = s->next_picture.f.data[1];
435
-        srcV = s->next_picture.f.data[2];
455
+        if(!dir) {
456
+            srcY = s->last_picture.f.data[0];
457
+            srcU = s->last_picture.f.data[1];
458
+            srcV = s->last_picture.f.data[2];
459
+        } else {
460
+            srcY = s->next_picture.f.data[0];
461
+            srcU = s->next_picture.f.data[1];
462
+            srcV = s->next_picture.f.data[2];
463
+        }
436 464
     }
437 465
 
438 466
     src_x = s->mb_x * 16 + (mx >> 2);
... ...
@@ -456,6 +557,12 @@ static void vc1_mc_1mv(VC1Context *v, int dir)
456 456
     srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
457 457
     srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
458 458
 
459
+    if (v->field_mode && v->ref_field_type[dir]) {
460
+        srcY += s->current_picture_ptr->f.linesize[0];
461
+        srcU += s->current_picture_ptr->f.linesize[1];
462
+        srcV += s->current_picture_ptr->f.linesize[2];
463
+    }
464
+
459 465
     /* for grayscale we should not try to read from unknown area */
460 466
     if(s->flags & CODEC_FLAG_GRAY) {
461 467
         srcU = s->edge_emu_buffer + 18 * s->linesize;
... ...
@@ -464,17 +571,17 @@ static void vc1_mc_1mv(VC1Context *v, int dir)
464 464
 
465 465
     if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
466 466
        || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel*3
467
-       || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 16 - s->mspel*3){
467
+       || (unsigned)(src_y - s->mspel) > v_edge_pos - (my&3) - 16 - s->mspel*3){
468 468
         uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
469 469
 
470 470
         srcY -= s->mspel * (1 + s->linesize);
471 471
         s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2,
472
-                            src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
472
+                            src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, v_edge_pos);
473 473
         srcY = s->edge_emu_buffer;
474 474
         s->dsp.emulated_edge_mc(uvbuf     , srcU, s->uvlinesize, 8+1, 8+1,
475
-                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
475
+                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
476 476
         s->dsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
477
-                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
477
+                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
478 478
         srcU = uvbuf;
479 479
         srcV = uvbuf + 16;
480 480
         /* if we deal with range reduction we need to scale source blocks */
... ...
@@ -520,20 +627,26 @@ static void vc1_mc_1mv(VC1Context *v, int dir)
520 520
         srcY += s->mspel * (1 + s->linesize);
521 521
     }
522 522
 
523
+    if (v->field_mode && v->cur_field_type) {
524
+        off    = s->current_picture_ptr->f.linesize[0];
525
+        off_uv = s->current_picture_ptr->f.linesize[1];
526
+    } else {
527
+        off    = 0;
528
+        off_uv = 0;
529
+    }
523 530
     if(s->mspel) {
524 531
         dxy = ((my & 3) << 2) | (mx & 3);
525
-        v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0]    , srcY    , s->linesize, v->rnd);
526
-        v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
532
+        v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off    , srcY    , s->linesize, v->rnd);
533
+        v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
527 534
         srcY += s->linesize * 8;
528
-        v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize    , srcY    , s->linesize, v->rnd);
529
-        v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
535
+        v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize    , srcY    , s->linesize, v->rnd);
536
+        v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
530 537
     } else { // hpel mc - always used for luma
531 538
         dxy = (my & 2) | ((mx & 2) >> 1);
532
-
533 539
         if(!v->rnd)
534
-            dsp->put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
540
+            dsp->put_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
535 541
         else
536
-            dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
542
+            dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
537 543
     }
538 544
 
539 545
     if(s->flags & CODEC_FLAG_GRAY) return;
... ...
@@ -541,50 +654,147 @@ static void vc1_mc_1mv(VC1Context *v, int dir)
541 541
     uvmx = (uvmx&3)<<1;
542 542
     uvmy = (uvmy&3)<<1;
543 543
     if(!v->rnd){
544
-        dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
545
-        dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
544
+        dsp->put_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
545
+        dsp->put_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
546 546
     }else{
547
-        v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
548
-        v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
547
+        v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
548
+        v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
549
+    }
550
+}
551
+
552
+static inline int median4(int a, int b, int c, int d)
553
+{
554
+    if(a < b) {
555
+        if(c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
556
+        else      return (FFMIN(b, c) + FFMAX(a, d)) / 2;
557
+    } else {
558
+        if(c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
559
+        else      return (FFMIN(a, c) + FFMAX(b, d)) / 2;
549 560
     }
550 561
 }
551 562
 
552 563
 /** Do motion compensation for 4-MV macroblock - luminance block
553 564
  */
554
-static void vc1_mc_4mv_luma(VC1Context *v, int n)
565
+static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir)
555 566
 {
556 567
     MpegEncContext *s = &v->s;
557 568
     DSPContext *dsp = &v->s.dsp;
558 569
     uint8_t *srcY;
559 570
     int dxy, mx, my, src_x, src_y;
560 571
     int off;
572
+    int fieldmv = (v->fcm == 1) ? v->blk_mv_type[s->block_index[n]] : 0;
573
+    int v_edge_pos = s->v_edge_pos >> v->field_mode;
574
+    if(!v->field_mode && !v->s.last_picture.f.data[0])return;
575
+    mx = s->mv[dir][n][0];
576
+    my = s->mv[dir][n][1];
577
+
578
+    if (!dir) {
579
+        if (v->field_mode) {
580
+            if ((v->cur_field_type != v->ref_field_type[dir]) && v->cur_field_type)
581
+                srcY = s->current_picture.f.data[0];
582
+            else
583
+                srcY = s->last_picture.f.data[0];
584
+        } else
585
+            srcY = s->last_picture.f.data[0];
586
+    } else
587
+        srcY = s->next_picture.f.data[0];
561 588
 
562
-    if(!v->s.last_picture.f.data[0])return;
563
-    mx = s->mv[0][n][0];
564
-    my = s->mv[0][n][1];
565
-    srcY = s->last_picture.f.data[0];
566
-
567
-    off = s->linesize * 4 * (n&2) + (n&1) * 8;
589
+    if (v->field_mode) {
590
+        if (v->cur_field_type != v->ref_field_type[dir])
591
+            my = my - 2 + 4 * v->cur_field_type;
592
+    }
593
+
594
+    if (s->pict_type == AV_PICTURE_TYPE_P && n == 3 && v->field_mode) {
595
+        int same_count = 0, opp_count = 0, k;
596
+        int chosen_mv[2][4][2], f;
597
+        int tx, ty;
598
+        for (k = 0; k < 4; k++) {
599
+            f = v->mv_f[0][s->block_index[k] + v->blocks_off];
600
+            chosen_mv[f][f ? opp_count : same_count][0] = s->mv[0][k][0];
601
+            chosen_mv[f][f ? opp_count : same_count][1] = s->mv[0][k][1];
602
+            opp_count += f;
603
+            same_count += 1 - f;
604
+        }
605
+        f = opp_count > same_count;
606
+        switch (f ? opp_count : same_count) {
607
+        case 4:
608
+            tx = median4(chosen_mv[f][0][0], chosen_mv[f][1][0], chosen_mv[f][2][0], chosen_mv[f][3][0]);
609
+            ty = median4(chosen_mv[f][0][1], chosen_mv[f][1][1], chosen_mv[f][2][1], chosen_mv[f][3][1]);
610
+            break;
611
+        case 3:
612
+            tx = mid_pred(chosen_mv[f][0][0], chosen_mv[f][1][0], chosen_mv[f][2][0]);
613
+            ty = mid_pred(chosen_mv[f][0][1], chosen_mv[f][1][1], chosen_mv[f][2][1]);
614
+            break;
615
+        case 2:
616
+            tx = (chosen_mv[f][0][0] + chosen_mv[f][1][0]) / 2;
617
+            ty = (chosen_mv[f][0][1] + chosen_mv[f][1][1]) / 2;
618
+            break;
619
+        }
620
+        s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
621
+        s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
622
+        for (k = 0; k < 4; k++) v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
623
+    }
624
+
625
+    if (v->fcm == 1) {  // not sure if needed for other types of picture
626
+        int qx, qy;
627
+        int width = s->avctx->coded_width;
628
+        int height = s->avctx->coded_height >> 1;
629
+        qx = (s->mb_x * 16) + (mx >> 2);
630
+        qy = (s->mb_y *  8) + (my >> 3);
631
+
632
+        if (qx < -17)
633
+            mx -= 4 * (qx + 17);
634
+        else if (qx > width)
635
+            mx -= 4 * (qx - width);
636
+        if (qy < -18)
637
+            my -= 8 * (qy + 18);
638
+        else if (qy > height + 1)
639
+            my -= 8 * (qy - height - 1);
640
+    }
641
+
642
+    if ((v->fcm == 1) && fieldmv)
643
+        off = ((n>1) ? s->linesize : 0) + (n&1) * 8;
644
+    else
645
+        off = s->linesize * 4 * (n&2) + (n&1) * 8;
646
+    if (v->field_mode && v->cur_field_type)
647
+        off += s->current_picture_ptr->f.linesize[0];
568 648
 
569 649
     src_x = s->mb_x * 16 + (n&1) * 8 + (mx >> 2);
570
-    src_y = s->mb_y * 16 + (n&2) * 4 + (my >> 2);
650
+    if (!fieldmv)
651
+        src_y = s->mb_y * 16 + (n&2) * 4 + (my >> 2);
652
+    else
653
+        src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2);
571 654
 
572 655
     if(v->profile != PROFILE_ADVANCED){
573 656
         src_x   = av_clip(  src_x, -16, s->mb_width  * 16);
574 657
         src_y   = av_clip(  src_y, -16, s->mb_height * 16);
575 658
     }else{
576 659
         src_x   = av_clip(  src_x, -17, s->avctx->coded_width);
577
-        src_y   = av_clip(  src_y, -18, s->avctx->coded_height + 1);
660
+        if (v->fcm == 1) {
661
+            if (src_y & 1)
662
+                src_y   = av_clip(  src_y, -17, s->avctx->coded_height + 1);
663
+            else
664
+                src_y   = av_clip(  src_y, -18, s->avctx->coded_height);
665
+        } else {
666
+            src_y   = av_clip(  src_y, -18, s->avctx->coded_height + 1);
667
+        }
578 668
     }
579 669
 
580 670
     srcY += src_y * s->linesize + src_x;
671
+    if (v->field_mode && v->ref_field_type[dir])
672
+        srcY += s->current_picture_ptr->f.linesize[0];
581 673
 
674
+    if (fieldmv && !(src_y & 1))
675
+        v_edge_pos--;
676
+    if (fieldmv && (src_y & 1) && src_y < 4)
677
+        src_y--;
582 678
     if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
583 679
        || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 8 - s->mspel*2
584
-       || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 8 - s->mspel*2){
585
-        srcY -= s->mspel * (1 + s->linesize);
586
-        s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 9+s->mspel*2, 9+s->mspel*2,
587
-                            src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
680
+       || (unsigned)(src_y - (s->mspel<<fieldmv)) > v_edge_pos - (my&3) - ((8 + s->mspel*2)<<fieldmv)){
681
+        srcY -= s->mspel * (1 + (s->linesize << fieldmv));
682
+        /* check emulate edge stride and offset */
683
+        s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 9+s->mspel*2, (9+s->mspel*2) << fieldmv, src_x - s->mspel,
684
+                src_y - (s->mspel << fieldmv), s->h_edge_pos, v_edge_pos);
588 685
         srcY = s->edge_emu_buffer;
589 686
         /* if we deal with range reduction we need to scale source blocks */
590 687
         if(v->rangeredfrm) {
... ...
@@ -594,7 +804,7 @@ static void vc1_mc_4mv_luma(VC1Context *v, int n)
594 594
             src = srcY;
595 595
             for(j = 0; j < 9 + s->mspel*2; j++) {
596 596
                 for(i = 0; i < 9 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
597
-                src += s->linesize;
597
+                src += s->linesize << fieldmv;
598 598
             }
599 599
         }
600 600
         /* if we deal with intensity compensation we need to scale source blocks */
... ...
@@ -605,15 +815,15 @@ static void vc1_mc_4mv_luma(VC1Context *v, int n)
605 605
             src = srcY;
606 606
             for(j = 0; j < 9 + s->mspel*2; j++) {
607 607
                 for(i = 0; i < 9 + s->mspel*2; i++) src[i] = v->luty[src[i]];
608
-                src += s->linesize;
608
+                src += s->linesize << fieldmv;
609 609
             }
610 610
         }
611
-        srcY += s->mspel * (1 + s->linesize);
611
+        srcY += s->mspel * (1 + (s->linesize << fieldmv));
612 612
     }
613 613
 
614 614
     if(s->mspel) {
615 615
         dxy = ((my & 3) << 2) | (mx & 3);
616
-        v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, v->rnd);
616
+        v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
617 617
     } else { // hpel mc - always used for luma
618 618
         dxy = (my & 2) | ((mx & 2) >> 1);
619 619
         if(!v->rnd)
... ...
@@ -623,86 +833,108 @@ static void vc1_mc_4mv_luma(VC1Context *v, int n)
623 623
     }
624 624
 }
625 625
 
626
-static inline int median4(int a, int b, int c, int d)
626
+static av_always_inline int get_chroma_mv(int *mvx, int *mvy, int *a, int flag, int *tx, int *ty)
627 627
 {
628
-    if(a < b) {
629
-        if(c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
630
-        else      return (FFMIN(b, c) + FFMAX(a, d)) / 2;
628
+    int idx, i;
629
+    static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
630
+    idx = ((a[3] != flag) << 3) | ((a[2] != flag) << 2) | ((a[1] != flag) << 1) | (a[0] != flag);
631
+    if(!idx) {
632
+        *tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
633
+        *ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
634
+        return 4;
635
+    } else if(count[idx] == 1) {
636
+        switch(idx) {
637
+        case 0x1:
638
+            *tx = mid_pred(mvx[1], mvx[2], mvx[3]);
639
+            *ty = mid_pred(mvy[1], mvy[2], mvy[3]);
640
+            return 3;
641
+        case 0x2:
642
+            *tx = mid_pred(mvx[0], mvx[2], mvx[3]);
643
+            *ty = mid_pred(mvy[0], mvy[2], mvy[3]);
644
+            return 3;
645
+        case 0x4:
646
+            *tx = mid_pred(mvx[0], mvx[1], mvx[3]);
647
+            *ty = mid_pred(mvy[0], mvy[1], mvy[3]);
648
+            return 3;
649
+        case 0x8:
650
+            *tx = mid_pred(mvx[0], mvx[1], mvx[2]);
651
+            *ty = mid_pred(mvy[0], mvy[1], mvy[2]);
652
+            return 3;
653
+        }
654
+    } else if(count[idx] == 2) {
655
+        int t1 = 0, t2 = 0;
656
+        for (i = 0; i < 3; i++)
657
+            if (!a[i]) {
658
+                t1 = i;
659
+                break;
660
+            }
661
+        for (i = t1 + 1; i < 4; i++)
662
+            if (!a[i]) {
663
+                t2 = i;
664
+                break;
665
+            }
666
+        *tx = (mvx[t1] + mvx[t2]) / 2;
667
+        *ty = (mvy[t1] + mvy[t2]) / 2;
668
+        return 2;
631 669
     } else {
632
-        if(c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
633
-        else      return (FFMIN(a, c) + FFMAX(b, d)) / 2;
670
+        return 0;
634 671
     }
672
+    return -1;
635 673
 }
636 674
 
637
-
638 675
 /** Do motion compensation for 4-MV macroblock - both chroma blocks
639 676
  */
640
-static void vc1_mc_4mv_chroma(VC1Context *v)
677
+static void vc1_mc_4mv_chroma(VC1Context *v, int dir)
641 678
 {
642 679
     MpegEncContext *s = &v->s;
643 680
     DSPContext *dsp = &v->s.dsp;
644 681
     uint8_t *srcU, *srcV;
645 682
     int uvmx, uvmy, uvsrc_x, uvsrc_y;
646
-    int i, idx, tx = 0, ty = 0;
647
-    int mvx[4], mvy[4], intra[4];
648
-    static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
683
+    int k, tx = 0, ty = 0;
684
+    int mvx[4], mvy[4], intra[4], mv_f[4];
685
+    int valid_count;
686
+    int chroma_ref_type = v->cur_field_type, off = 0;
687
+    int v_edge_pos = s->v_edge_pos >> v->field_mode;
649 688
 
650
-    if(!v->s.last_picture.f.data[0])return;
689
+    if(!v->field_mode && !v->s.last_picture.f.data[0])return;
651 690
     if(s->flags & CODEC_FLAG_GRAY) return;
652 691
 
653
-    for(i = 0; i < 4; i++) {
654
-        mvx[i] = s->mv[0][i][0];
655
-        mvy[i] = s->mv[0][i][1];
656
-        intra[i] = v->mb_type[0][s->block_index[i]];
692
+    for(k = 0; k < 4; k++) {
693
+        mvx[k] = s->mv[dir][k][0];
694
+        mvy[k] = s->mv[dir][k][1];
695
+        intra[k] = v->mb_type[0][s->block_index[k]];
696
+        if (v->field_mode)
697
+            mv_f[k] = v->mv_f[dir][s->block_index[k] + v->blocks_off];
657 698
     }
658 699
 
659 700
     /* calculate chroma MV vector from four luma MVs */
660
-    idx = (intra[3] << 3) | (intra[2] << 2) | (intra[1] << 1) | intra[0];
661
-    if(!idx) { // all blocks are inter
662
-        tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
663
-        ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
664
-    } else if(count[idx] == 1) { // 3 inter blocks
665
-        switch(idx) {
666
-        case 0x1:
667
-            tx = mid_pred(mvx[1], mvx[2], mvx[3]);
668
-            ty = mid_pred(mvy[1], mvy[2], mvy[3]);
669
-            break;
670
-        case 0x2:
671
-            tx = mid_pred(mvx[0], mvx[2], mvx[3]);
672
-            ty = mid_pred(mvy[0], mvy[2], mvy[3]);
673
-            break;
674
-        case 0x4:
675
-            tx = mid_pred(mvx[0], mvx[1], mvx[3]);
676
-            ty = mid_pred(mvy[0], mvy[1], mvy[3]);
677
-            break;
678
-        case 0x8:
679
-            tx = mid_pred(mvx[0], mvx[1], mvx[2]);
680
-            ty = mid_pred(mvy[0], mvy[1], mvy[2]);
681
-            break;
701
+    if (!v->field_mode || (v->field_mode && !v->numref)) {
702
+        valid_count = get_chroma_mv(mvx, mvy, intra, 0, &tx, &ty);
703
+        if (!valid_count) {
704
+            v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
705
+            return; //no need to do MC for intra blocks
682 706
         }
683
-    } else if(count[idx] == 2) {
684
-        int t1 = 0, t2 = 0;
685
-        for(i=0; i<3;i++) if(!intra[i]) {t1 = i; break;}
686
-        for(i= t1+1; i<4; i++)if(!intra[i]) {t2 = i; break;}
687
-        tx = (mvx[t1] + mvx[t2]) / 2;
688
-        ty = (mvy[t1] + mvy[t2]) / 2;
689 707
     } else {
690
-        s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
691
-        s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
692
-        v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
693
-        return; //no need to do MC for inter blocks
708
+        int dominant = 0;
709
+        if (mv_f[0] + mv_f[1] + mv_f[2] + mv_f[3] > 2)
710
+            dominant = 1;
711
+        valid_count = get_chroma_mv(mvx, mvy, mv_f, dominant, &tx, &ty);
712
+        if (dominant)
713
+            chroma_ref_type = !v->cur_field_type;
694 714
     }
695
-
696
-    s->current_picture.f.motion_val[1][s->block_index[0]][0] = tx;
697
-    s->current_picture.f.motion_val[1][s->block_index[0]][1] = ty;
698 715
     uvmx = (tx + ((tx&3) == 3)) >> 1;
699 716
     uvmy = (ty + ((ty&3) == 3)) >> 1;
717
+
700 718
     v->luma_mv[s->mb_x][0] = uvmx;
701 719
     v->luma_mv[s->mb_x][1] = uvmy;
720
+
702 721
     if(v->fastuvmc) {
703 722
         uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
704 723
         uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
705 724
     }
725
+    // Field conversion bias
726
+    if (v->cur_field_type != chroma_ref_type)
727
+        uvmy += 2 - 4 * chroma_ref_type;
706 728
 
707 729
     uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
708 730
     uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
... ...
@@ -715,15 +947,39 @@ static void vc1_mc_4mv_chroma(VC1Context *v)
715 715
         uvsrc_y = av_clip(uvsrc_y,  -8, s->avctx->coded_height >> 1);
716 716
     }
717 717
 
718
-    srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
719
-    srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
718
+    if (!dir) {
719
+        if (v->field_mode) {
720
+            if ((v->cur_field_type != chroma_ref_type) && v->cur_field_type) {
721
+                srcU = s->current_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
722
+                srcV = s->current_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
723
+            } else {
724
+                srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
725
+                srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
726
+            }
727
+        } else {
728
+            srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
729
+            srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
730
+        }
731
+    } else {
732
+        srcU = s->next_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
733
+        srcV = s->next_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
734
+    }
735
+
736
+    if (v->field_mode) {
737
+        if (chroma_ref_type) {
738
+            srcU += s->current_picture_ptr->f.linesize[1];
739
+            srcV += s->current_picture_ptr->f.linesize[2];
740
+        }
741
+        off = v->cur_field_type ? s->current_picture_ptr->f.linesize[1] : 0;
742
+    }
743
+
720 744
     if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
721 745
        || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
722
-       || (unsigned)uvsrc_y > (s->v_edge_pos >> 1) - 9){
746
+       || (unsigned)uvsrc_y > (v_edge_pos >> 1) - 9){
723 747
         s->dsp.emulated_edge_mc(s->edge_emu_buffer     , srcU, s->uvlinesize, 8+1, 8+1,
724
-                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
748
+                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
725 749
         s->dsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize, 8+1, 8+1,
726
-                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
750
+                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
727 751
         srcU = s->edge_emu_buffer;
728 752
         srcV = s->edge_emu_buffer + 16;
729 753
 
... ...
@@ -763,11 +1019,89 @@ static void vc1_mc_4mv_chroma(VC1Context *v)
763 763
     uvmx = (uvmx&3)<<1;
764 764
     uvmy = (uvmy&3)<<1;
765 765
     if(!v->rnd){
766
-        dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
767
-        dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
766
+        dsp->put_h264_chroma_pixels_tab[0](s->dest[1] + off, srcU, s->uvlinesize, 8, uvmx, uvmy);
767
+        dsp->put_h264_chroma_pixels_tab[0](s->dest[2] + off, srcV, s->uvlinesize, 8, uvmx, uvmy);
768 768
     }else{
769
-        v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
770
-        v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
769
+        v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off, srcU, s->uvlinesize, 8, uvmx, uvmy);
770
+        v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off, srcV, s->uvlinesize, 8, uvmx, uvmy);
771
+    }
772
+}
773
+
774
+/** Do motion compensation for 4-MV field chroma macroblock (both U and V)
775
+ */
776
+static void vc1_mc_4mv_chroma4(VC1Context *v)
777
+{
778
+    MpegEncContext *s = &v->s;
779
+    DSPContext *dsp = &v->s.dsp;
780
+    uint8_t *srcU, *srcV;
781
+    int uvsrc_x, uvsrc_y;
782
+    int uvmx_field[4], uvmy_field[4];
783
+    int i, off, tx, ty;
784
+    int fieldmv = v->blk_mv_type[s->block_index[0]];
785
+    static const int s_rndtblfield[16] = {0, 0, 1, 2, 4, 4, 5, 6, 2, 2, 3, 8, 6, 6, 7, 12};
786
+    int v_dist = fieldmv ? 1 : 4; // vertical offset for lower sub-blocks
787
+    int v_edge_pos = s->v_edge_pos >> 1;
788
+
789
+    if (!v->s.last_picture.f.data[0]) return;
790
+    if (s->flags & CODEC_FLAG_GRAY)   return;
791
+
792
+    for (i = 0; i < 4; i++) {
793
+        tx = s->mv[0][i][0];
794
+        uvmx_field[i] = (tx + ((tx & 3) == 3)) >> 1;
795
+        ty = s->mv[0][i][1];
796
+        if (fieldmv)
797
+            uvmy_field[i] = (ty >> 4) * 8 + s_rndtblfield[ty & 0xF];
798
+        else
799
+            uvmy_field[i] = (ty + ((ty & 3) == 3)) >> 1;
800
+    }
801
+
802
+    for (i = 0; i < 4; i++) {
803
+        off = (i & 1) * 4 + ((i & 2) ? v_dist * s->uvlinesize : 0);
804
+        uvsrc_x = s->mb_x * 8 + (i & 1) * 4 + (uvmx_field[i] >> 2);
805
+        uvsrc_y = s->mb_y * 8 + ((i & 2) ? v_dist : 0) + (uvmy_field[i] >> 2);
806
+        // FIXME: implement proper pull-back (see vc1cropmv.c, vc1CROPMV_ChromaPullBack())
807
+        uvsrc_x = av_clip(uvsrc_x,  -8, s->avctx->coded_width  >> 1);
808
+        uvsrc_y = av_clip(uvsrc_y,  -8, s->avctx->coded_height >> 1);
809
+        srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
810
+        srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
811
+        uvmx_field[i] = (uvmx_field[i] & 3) << 1;
812
+        uvmy_field[i] = (uvmy_field[i] & 3) << 1;
813
+
814
+        if (fieldmv && !(uvsrc_y & 1))
815
+            v_edge_pos--;
816
+        if (fieldmv && (uvsrc_y & 1) && uvsrc_y < 2)
817
+            uvsrc_y--;
818
+        if((v->mv_mode == MV_PMODE_INTENSITY_COMP)
819
+            || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
820
+            || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)){
821
+            s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcU, s->uvlinesize, 5, (5 << fieldmv), uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos);
822
+            s->dsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize, 5, (5 << fieldmv), uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos);
823
+            srcU = s->edge_emu_buffer;
824
+            srcV = s->edge_emu_buffer + 16;
825
+
826
+            /* if we deal with intensity compensation we need to scale source blocks */
827
+            if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
828
+                int i, j;
829
+                uint8_t *src, *src2;
830
+
831
+                src = srcU; src2 = srcV;
832
+                for(j = 0; j < 5; j++) {
833
+                    for(i = 0; i < 5; i++) {
834
+                        src[i] = v->lutuv[src[i]];
835
+                        src2[i] = v->lutuv[src2[i]];
836
+                    }
837
+                    src += s->uvlinesize << 1;
838
+                    src2 += s->uvlinesize << 1;
839
+                }
840
+            }
841
+        }
842
+        if (!v->rnd) {
843
+            dsp->put_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
844
+            dsp->put_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
845
+        } else {
846
+            v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
847
+            v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
848
+        }
771 849
     }
772 850
 }
773 851
 
... ...
@@ -865,16 +1199,249 @@ static void vc1_mc_4mv_chroma(VC1Context *v)
865 865
     _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign;     \
866 866
   }
867 867
 
868
+static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x, int *dmv_y, int *pred_flag)
869
+{
870
+    int index, index1;
871
+    int extend_x = 0, extend_y = 0;
872
+    GetBitContext *gb = &v->s.gb;
873
+    int bits, esc;
874
+    int val, sign;
875
+    const int* offs_tab;
876
+
877
+    if (v->numref) {
878
+        bits = VC1_2REF_MVDATA_VLC_BITS;
879
+        esc = 125;
880
+    } else {
881
+        bits = VC1_1REF_MVDATA_VLC_BITS;
882
+        esc = 71;
883
+    }
884
+    switch (v->dmvrange) {
885
+    case 1:
886
+        extend_x = 1;
887
+        break;
888
+    case 2:
889
+        extend_y = 1;
890
+        break;
891
+    case 3:
892
+        extend_x = extend_y = 1;
893
+        break;
894
+    }
895
+    index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
896
+    if (index == esc) {
897
+        *dmv_x = get_bits(gb, v->k_x);
898
+        *dmv_y = get_bits(gb, v->k_y);
899
+        if (v->numref) {
900
+            *pred_flag = *dmv_y & 1;
901
+            *dmv_y = (*dmv_y + *pred_flag) >> 1;
902
+        }
903
+    }
904
+    else {
905
+        if (extend_x)
906
+            offs_tab = offset_table2;
907
+        else
908
+            offs_tab = offset_table1;
909
+        index1 = (index + 1) % 9;
910
+        if (index1 != 0) {
911
+            val = get_bits(gb, index1 + extend_x);
912
+            sign = 0 -(val & 1);
913
+            *dmv_x = (sign ^ ((val >> 1) + offs_tab[index1])) - sign;
914
+        } else
915
+            *dmv_x = 0;
916
+        if (extend_y)
917
+            offs_tab = offset_table2;
918
+        else
919
+            offs_tab = offset_table1;
920
+        index1 = (index + 1) / 9;
921
+        if (index1 > v->numref) {
922
+            val = get_bits(gb, (index1 + (extend_y << v->numref)) >> v->numref);
923
+            sign = 0 - (val & 1);
924
+            *dmv_y = (sign ^ ((val >> 1) + offs_tab[index1 >> v->numref])) - sign;
925
+        } else
926
+            *dmv_y = 0;
927
+        if (v->numref)
928
+            *pred_flag = index1 & 1;
929
+    }
930
+}
931
+
932
+static av_always_inline int scaleforsame_x(VC1Context *v, int n /* MV */, int dir)
933
+{
934
+    int scaledvalue, refdist;
935
+    int scalesame1, scalesame2;
936
+    int scalezone1_x, zone1offset_x;
937
+
938
+    if (v->s.pict_type != AV_PICTURE_TYPE_B)
939
+        refdist = v->refdist;
940
+    else
941
+        refdist = dir ? v->brfd : v->frfd;
942
+    if (refdist > 3)
943
+        refdist = 3;
944
+    scalesame1 = vc1_field_mvpred_scales[v->second_field][1][refdist];
945
+    scalesame2 = vc1_field_mvpred_scales[v->second_field][2][refdist];
946
+    scalezone1_x  = vc1_field_mvpred_scales[v->second_field][3][refdist];
947
+    zone1offset_x = vc1_field_mvpred_scales[v->second_field][5][refdist];
948
+
949
+    if (FFABS(n) > 255)
950
+        scaledvalue = n;
951
+    else {
952
+        if (FFABS(n) < scalezone1_x)
953
+            scaledvalue = (n * scalesame1) >> 8;
954
+        else {
955
+            if (n < 0)
956
+                scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
957
+            else
958
+                scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
959
+        }
960
+    }
961
+    return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
962
+}
963
+
964
+static av_always_inline int scaleforsame_y(VC1Context *v, int i, int n /* MV */, int dir)
965
+{
966
+    int scaledvalue, refdist;
967
+    int scalesame1, scalesame2;
968
+    int scalezone1_y, zone1offset_y;
969
+
970
+    if (v->s.pict_type != AV_PICTURE_TYPE_B)
971
+        refdist = v->refdist;
972
+    else
973
+        refdist = dir ? v->brfd : v->frfd;
974
+    if (refdist > 3)
975
+        refdist = 3;
976
+    scalesame1 = vc1_field_mvpred_scales[v->second_field][1][refdist];
977
+    scalesame2 = vc1_field_mvpred_scales[v->second_field][2][refdist];
978
+    scalezone1_y  = vc1_field_mvpred_scales[v->second_field][4][refdist];
979
+    zone1offset_y = vc1_field_mvpred_scales[v->second_field][6][refdist];
980
+
981
+    if (FFABS(n) > 63)
982
+        scaledvalue = n;
983
+    else {
984
+        if (FFABS(n) < scalezone1_y)
985
+            scaledvalue = (n * scalesame1) >> 8;
986
+        else {
987
+            if (n < 0)
988
+                scaledvalue = ((n * scalesame2) >> 8) - zone1offset_y;
989
+            else
990
+                scaledvalue = ((n * scalesame2) >> 8) + zone1offset_y;
991
+        }
992
+    }
993
+
994
+    if (v->cur_field_type && !v->ref_field_type[dir])
995
+        return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
996
+    else
997
+        return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
998
+}
999
+
1000
+static av_always_inline int scaleforopp_x(VC1Context *v, int n /* MV */)
1001
+{
1002
+    int scalezone1_x, zone1offset_x;
1003
+    int scaleopp1, scaleopp2, brfd;
1004
+    int scaledvalue;
1005
+
1006
+    brfd = FFMIN(v->brfd, 3);
1007
+    scalezone1_x  = vc1_b_field_mvpred_scales[3][brfd];
1008
+    zone1offset_x = vc1_b_field_mvpred_scales[5][brfd];
1009
+    scaleopp1 = vc1_b_field_mvpred_scales[1][brfd];
1010
+    scaleopp2 = vc1_b_field_mvpred_scales[2][brfd];
1011
+
1012
+    if (FFABS(n) > 255)
1013
+        scaledvalue = n;
1014
+    else {
1015
+        if (FFABS(n) < scalezone1_x)
1016
+            scaledvalue = (n * scaleopp1) >> 8;
1017
+        else {
1018
+            if (n < 0)
1019
+                scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_x;
1020
+            else
1021
+                scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_x;
1022
+        }
1023
+    }
1024
+    return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1025
+}
1026
+
1027
+static av_always_inline int scaleforopp_y(VC1Context *v, int n /* MV */, int dir)
1028
+{
1029
+    int scalezone1_y, zone1offset_y;
1030
+    int scaleopp1, scaleopp2, brfd;
1031
+    int scaledvalue;
1032
+
1033
+    brfd = FFMIN(v->brfd, 3);
1034
+    scalezone1_y  = vc1_b_field_mvpred_scales[4][brfd];
1035
+    zone1offset_y = vc1_b_field_mvpred_scales[6][brfd];
1036
+    scaleopp1 = vc1_b_field_mvpred_scales[1][brfd];
1037
+    scaleopp2 = vc1_b_field_mvpred_scales[2][brfd];
1038
+
1039
+    if (FFABS(n) > 63)
1040
+        scaledvalue = n;
1041
+    else {
1042
+        if (FFABS(n) < scalezone1_y)
1043
+            scaledvalue = (n * scaleopp1) >> 8;
1044
+        else {
1045
+            if (n < 0)
1046
+                scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_y;
1047
+            else
1048
+                scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_y;
1049
+        }
1050
+    }
1051
+    if (v->cur_field_type && !v->ref_field_type[dir]) {
1052
+        return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1053
+    } else {
1054
+        return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1055
+    }
1056
+}
1057
+
1058
+static av_always_inline int scaleforsame(VC1Context *v, int i, int n /* MV */, int dim, int dir)
1059
+{
1060
+    int brfd, scalesame;
1061
+    if (v->s.pict_type != AV_PICTURE_TYPE_B || v->second_field || !dir) {
1062
+        if (dim)
1063
+            return scaleforsame_y(v, i, n, dir);
1064
+        else
1065
+            return scaleforsame_x(v, n, dir);
1066
+    }
1067
+    brfd = FFMIN(v->brfd, 3);
1068
+    scalesame = vc1_b_field_mvpred_scales[0][brfd];
1069
+    return(n * scalesame >> 8);
1070
+}
1071
+
1072
+static av_always_inline int scaleforopp(VC1Context *v, int n /* MV */, int dim, int dir)
1073
+{
1074
+    int refdist, scaleopp;
1075
+    if (v->s.pict_type == AV_PICTURE_TYPE_B && !v->second_field && dir == 1) {
1076
+        if (dim)
1077
+            return scaleforopp_y(v, n, dir);
1078
+        else
1079
+            return scaleforopp_x(v, n);
1080
+    }
1081
+    if (v->s.pict_type != AV_PICTURE_TYPE_B)
1082
+        refdist = FFMIN(v->refdist, 3);
1083
+    else
1084
+        refdist = dir ? v->brfd : v->frfd;
1085
+    scaleopp = vc1_field_mvpred_scales[v->second_field][0][refdist];
1086
+    return(n * scaleopp >> 8);
1087
+}
1088
+
868 1089
 /** Predict and set motion vector
869 1090
  */
870
-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)
1091
+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, int pred_flag, int dir)
871 1092
 {
872 1093
     MpegEncContext *s = &v->s;
873 1094
     int xy, wrap, off = 0;
874 1095
     int16_t *A, *B, *C;
875 1096
     int px, py;
876 1097
     int sum;
877
-
1098
+    int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
1099
+    int opposit, f;
1100
+    int16_t samefield_pred[2], oppfield_pred[2];
1101
+    int16_t samefield_predA[2], oppfield_predA[2];
1102
+    int16_t samefield_predB[2], oppfield_predB[2];
1103
+    int16_t samefield_predC[2], oppfield_predC[2];
1104
+    int16_t *predA, *predC;
1105
+    int a_valid, b_valid, c_valid;
1106
+    int hybridmv_thresh, y_bias = 0;
1107
+
1108
+    if (v->mv_mode == MV_PMODE_MIXED_MV ||
1109
+        ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV))) mixedmv_pic = 1;
1110
+    else mixedmv_pic = 0;
878 1111
     /* scale MV difference to be quad-pel */
879 1112
     dmv_x <<= 1 - s->quarter_sample;
880 1113
     dmv_y <<= 1 - s->quarter_sample;
... ...
@@ -883,35 +1450,38 @@ static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y, int m
883 883
     xy = s->block_index[n];
884 884
 
885 885
     if(s->mb_intra){
886
-        s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy][0] = 0;
887
-        s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy][1] = 0;
888
-        s->current_picture.f.motion_val[1][xy][0] = 0;
889
-        s->current_picture.f.motion_val[1][xy][1] = 0;
886
+        s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = 0;
887
+        s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = 0;
888
+        s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = 0;
889
+        s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
890 890
         if(mv1) { /* duplicate motion data for 1-MV block */
891
-            s->current_picture.f.motion_val[0][xy + 1][0]        = 0;
892
-            s->current_picture.f.motion_val[0][xy + 1][1]        = 0;
893
-            s->current_picture.f.motion_val[0][xy + wrap][0]     = 0;
894
-            s->current_picture.f.motion_val[0][xy + wrap][1]     = 0;
895
-            s->current_picture.f.motion_val[0][xy + wrap + 1][0] = 0;
896
-            s->current_picture.f.motion_val[0][xy + wrap + 1][1] = 0;
891
+            s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][0] = 0;
892
+            s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][1] = 0;
893
+            s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][0] = 0;
894
+            s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][1] = 0;
895
+            s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
896
+            s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
897 897
             v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
898
-            s->current_picture.f.motion_val[1][xy + 1][0]        = 0;
899
-            s->current_picture.f.motion_val[1][xy + 1][1]        = 0;
900
-            s->current_picture.f.motion_val[1][xy + wrap][0]     = 0;
901
-            s->current_picture.f.motion_val[1][xy + wrap][1]     = 0;
902
-            s->current_picture.f.motion_val[1][xy + wrap + 1][0] = 0;
903
-            s->current_picture.f.motion_val[1][xy + wrap + 1][1] = 0;
898
+            s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][0] = 0;
899
+            s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][1] = 0;
900
+            s->current_picture.f.motion_val[1][xy + wrap][0] = 0;
901
+            s->current_picture.f.motion_val[1][xy + wrap + v->blocks_off][1] = 0;
902
+            s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
903
+            s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
904 904
         }
905 905
         return;
906 906
     }
907 907
 
908
-    C = s->current_picture.f.motion_val[0][xy - 1];
909
-    A = s->current_picture.f.motion_val[0][xy - wrap];
910
-    if(mv1)
911
-        off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
912
-    else {
908
+    C = s->current_picture.f.motion_val[dir][xy - 1 + v->blocks_off];
909
+    A = s->current_picture.f.motion_val[dir][xy - wrap + v->blocks_off];
910
+    if(mv1) {
911
+        if (v->field_mode && mixedmv_pic)
912
+            off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
913
+        else
914
+            off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
915
+    } else {
913 916
         //in 4-MV mode different blocks have different B predictor position
914
-        switch(n){
917
+        switch (n) {
915 918
         case 0:
916 919
             off = (s->mb_x > 0) ? -1 : 1;
917 920
             break;
... ...
@@ -925,24 +1495,135 @@ static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y, int m
925 925
             off = -1;
926 926
         }
927 927
     }
928
-    B = s->current_picture.f.motion_val[0][xy - wrap + off];
929
-
930
-    if(!s->first_slice_line || (n==2 || n==3)) { // predictor A is not out of bounds
931
-        if(s->mb_width == 1) {
932
-            px = A[0];
933
-            py = A[1];
928
+    B = s->current_picture.f.motion_val[dir][xy - wrap + off + v->blocks_off];
929
+
930
+    a_valid = !s->first_slice_line || (n==2 || n==3);
931
+    b_valid = a_valid && (s->mb_width > 1);
932
+    c_valid = s->mb_x || (n==1 || n==3);
933
+    if (v->field_mode) {
934
+        a_valid = a_valid && !is_intra[xy - wrap];
935
+        b_valid = b_valid && !is_intra[xy - wrap + off];
936
+        c_valid = c_valid && !is_intra[xy - 1];
937
+    }
938
+
939
+    if (a_valid) {
940
+        f = v->mv_f[dir][xy - wrap + v->blocks_off];
941
+        num_oppfield += f;
942
+        num_samefield += 1 - f;
943
+        if (f) {
944
+            oppfield_predA[0] = A[0];
945
+            oppfield_predA[1] = A[1];
946
+            samefield_predA[0] = scaleforsame(v, 0, A[0], 0, dir);
947
+            samefield_predA[1] = scaleforsame(v, n, A[1], 1, dir);
948
+        } else {
949
+            samefield_predA[0] = A[0];
950
+            samefield_predA[1] = A[1];
951
+            if (v->numref)
952
+                oppfield_predA[0] = scaleforopp(v, A[0], 0, dir);
953
+            if (v->numref)
954
+                oppfield_predA[1] = scaleforopp(v, A[1], 1, dir);
955
+        }
956
+    } else {
957
+        samefield_predA[0] = samefield_predA[1] = 0;
958
+        oppfield_predA[0] = oppfield_predA[1] = 0;
959
+    }
960
+    if (c_valid) {
961
+        f = v->mv_f[dir][xy - 1 + v->blocks_off];
962
+        num_oppfield += f;
963
+        num_samefield += 1 - f;
964
+        if (f) {
965
+            oppfield_predC[0] = C[0];
966
+            oppfield_predC[1] = C[1];
967
+            samefield_predC[0] = scaleforsame(v, 0, C[0], 0, dir);
968
+            samefield_predC[1] = scaleforsame(v, n, C[1], 1, dir);
934 969
         } else {
935
-            px = mid_pred(A[0], B[0], C[0]);
936
-            py = mid_pred(A[1], B[1], C[1]);
970
+            samefield_predC[0] = C[0];
971
+            samefield_predC[1] = C[1];
972
+            if (v->numref)
973
+                oppfield_predC[0] = scaleforopp(v, C[0], 0, dir);
974
+            if (v->numref)
975
+                oppfield_predC[1] = scaleforopp(v, C[1], 1, dir);
937 976
         }
938
-    } else if(s->mb_x || (n==1 || n==3)) { // predictor C is not out of bounds
939
-        px = C[0];
940
-        py = C[1];
941 977
     } else {
942
-        px = py = 0;
978
+        samefield_predC[0] = samefield_predC[1] = 0;
979
+        oppfield_predC[0] = oppfield_predC[1] = 0;
980
+    }
981
+    if (b_valid) {
982
+        f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
983
+        num_oppfield += f;
984
+        num_samefield += 1 - f;
985
+        if (f) {
986
+            oppfield_predB[0] = B[0];
987
+            oppfield_predB[1] = B[1];
988
+            samefield_predB[0] = scaleforsame(v, 0, B[0], 0, dir);
989
+            samefield_predB[1] = scaleforsame(v, n, B[1], 1, dir);
990
+        } else {
991
+            samefield_predB[0] = B[0];
992
+            samefield_predB[1] = B[1];
993
+            if (v->numref)
994
+                oppfield_predB[0] = scaleforopp(v, B[0], 0, dir);
995
+            if (v->numref)
996
+                oppfield_predB[1] = scaleforopp(v, B[1], 1, dir);
997
+        }
998
+    } else {
999
+        samefield_predB[0] = samefield_predB[1] = 0;
1000
+        oppfield_predB[0] = oppfield_predB[1] = 0;
1001
+    }
1002
+
1003
+    if (a_valid) {
1004
+        samefield_pred[0] = samefield_predA[0];
1005
+        samefield_pred[1] = samefield_predA[1];
1006
+        oppfield_pred[0] = oppfield_predA[0];
1007
+        oppfield_pred[1] = oppfield_predA[1];
1008
+    } else if (c_valid) {
1009
+        samefield_pred[0] = samefield_predC[0];
1010
+        samefield_pred[1] = samefield_predC[1];
1011
+        oppfield_pred[0] = oppfield_predC[0];
1012
+        oppfield_pred[1] = oppfield_predC[1];
1013
+    } else if (b_valid) {
1014
+        samefield_pred[0] = samefield_predB[0];
1015
+        samefield_pred[1] = samefield_predB[1];
1016
+        oppfield_pred[0] = oppfield_predB[0];
1017
+        oppfield_pred[1] = oppfield_predB[1];
1018
+    } else {
1019
+        samefield_pred[0] = samefield_pred[1] = 0;
1020
+        oppfield_pred[0] = oppfield_pred[1] = 0;
1021
+    }
1022
+
1023
+    if (num_samefield + num_oppfield > 1) {
1024
+        samefield_pred[0] = mid_pred(samefield_predA[0], samefield_predB[0], samefield_predC[0]);
1025
+        samefield_pred[1] = mid_pred(samefield_predA[1], samefield_predB[1], samefield_predC[1]);
1026
+        if (v->numref)
1027
+            oppfield_pred[0] = mid_pred(oppfield_predA[0], oppfield_predB[0], oppfield_predC[0]);
1028
+        if (v->numref)
1029
+            oppfield_pred[1] = mid_pred(oppfield_predA[1], oppfield_predB[1], oppfield_predC[1]);
1030
+    }
1031
+
1032
+    if (v->field_mode) {
1033
+        if (num_samefield <= num_oppfield)
1034
+            opposit = 1 - pred_flag;
1035
+        else
1036
+            opposit = pred_flag;
1037
+    } else
1038
+        opposit = 0;
1039
+    if (opposit) {
1040
+        px = oppfield_pred[0];
1041
+        py = oppfield_pred[1];
1042
+        predA = oppfield_predA;
1043
+        predC = oppfield_predC;
1044
+        v->mv_f[dir][xy + v->blocks_off] = f = 1;
1045
+        v->ref_field_type[dir] = !v->cur_field_type;
1046
+    } else {
1047
+        px = samefield_pred[0];
1048
+        py = samefield_pred[1];
1049
+        predA = samefield_predA;
1050
+        predC = samefield_predC;
1051
+        v->mv_f[dir][xy + v->blocks_off] = f = 0;
1052
+        v->ref_field_type[dir] = v->cur_field_type;
943 1053
     }
1054
+
944 1055
     /* Pullback MV as specified in 8.3.5.3.4 */
945
-    {
1056
+    if (!v->field_mode) {
946 1057
         int qx, qy, X, Y;
947 1058
         qx = (s->mb_x << 6) + ((n==1 || n==3) ? 32 : 0);
948 1059
         qy = (s->mb_y << 6) + ((n==2 || n==3) ? 32 : 0);
... ...
@@ -958,46 +1639,285 @@ static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y, int m
958 958
         if(qx + px > X) px = X - qx;
959 959
         if(qy + py > Y) py = Y - qy;
960 960
     }
961
-    /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
962
-    if((!s->first_slice_line || (n==2 || n==3)) && (s->mb_x || (n==1 || n==3))) {
963
-        if(is_intra[xy - wrap])
964
-            sum = FFABS(px) + FFABS(py);
961
+
962
+    if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
963
+        /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
964
+        if (v->field_mode && !s->quarter_sample)
965
+            hybridmv_thresh = 16;
965 966
         else
966
-            sum = FFABS(px - A[0]) + FFABS(py - A[1]);
967
-        if(sum > 32) {
968
-            if(get_bits1(&s->gb)) {
969
-                px = A[0];
970
-                py = A[1];
967
+            hybridmv_thresh = 32;
968
+        if (a_valid && c_valid) {
969
+            if (is_intra[xy - wrap])
970
+                sum = FFABS(px) + FFABS(py);
971
+            else
972
+                sum = FFABS(px - predA[0]) + FFABS(py - predA[1]);
973
+            if (sum > hybridmv_thresh) {
974
+                if (get_bits1(&s->gb)) {     // read HYBRIDPRED bit
975
+                    px = predA[0];
976
+                    py = predA[1];
977
+                } else {
978
+                    px = predC[0];
979
+                    py = predC[1];
980
+                }
971 981
             } else {
972
-                px = C[0];
973
-                py = C[1];
982
+                if (is_intra[xy - 1])
983
+                    sum = FFABS(px) + FFABS(py);
984
+                else
985
+                    sum = FFABS(px - predC[0]) + FFABS(py - predC[1]);
986
+                if (sum > hybridmv_thresh) {
987
+                    if(get_bits1(&s->gb)) {
988
+                        px = predA[0];
989
+                        py = predA[1];
990
+                    } else {
991
+                        px = predC[0];
992
+                        py = predC[1];
993
+                    }
994
+                }
995
+            }
996
+        }
997
+    }
998
+
999
+    if (v->field_mode && !s->quarter_sample) {
1000
+        r_x <<= 1;
1001
+        r_y <<= 1;
1002
+    }
1003
+    if (v->field_mode && v->numref)
1004
+        r_y >>= 1;
1005
+    if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
1006
+        y_bias = 1;
1007
+    /* store MV using signed modulus of MV range defined in 4.11 */
1008
+    s->mv[dir][n][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1009
+    s->mv[dir][n][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1] = ((py + dmv_y + r_y - y_bias) & ((r_y << 1) - 1)) - r_y + y_bias;
1010
+    if(mv1) { /* duplicate motion data for 1-MV block */
1011
+        s->current_picture.f.motion_val[dir][xy + 1 + v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0];
1012
+        s->current_picture.f.motion_val[dir][xy + 1 + v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1];
1013
+        s->current_picture.f.motion_val[dir][xy + wrap + v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0];
1014
+        s->current_picture.f.motion_val[dir][xy + wrap + v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1];
1015
+        s->current_picture.f.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0];
1016
+        s->current_picture.f.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1];
1017
+        v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
1018
+        v->mv_f[dir][xy + wrap + v->blocks_off] = v->mv_f[dir][xy + wrap + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
1019
+    }
1020
+}
1021
+
1022
+/** Predict and set motion vector for interlaced frame picture MBs
1023
+ */
1024
+static inline void vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y, int mvn, int r_x, int r_y, uint8_t* is_intra)
1025
+{
1026
+    MpegEncContext *s = &v->s;
1027
+    int xy, wrap, off = 0;
1028
+    int A[2], B[2], C[2];
1029
+    int px, py;
1030
+    int a_valid = 0, b_valid = 0, c_valid = 0;
1031
+    int field_a, field_b, field_c; // 0: same, 1: opposit
1032
+    int total_valid, num_samefield, num_oppfield;
1033
+    int pos_c, pos_b, n_adj;
1034
+
1035
+    wrap = s->b8_stride;
1036
+    xy = s->block_index[n];
1037
+
1038
+    if(s->mb_intra){
1039
+        s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy][0] = 0;
1040
+        s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy][1] = 0;
1041
+        s->current_picture.f.motion_val[1][xy][0] = 0;
1042
+        s->current_picture.f.motion_val[1][xy][1] = 0;
1043
+        if(mvn == 1) { /* duplicate motion data for 1-MV block */
1044
+            s->current_picture.f.motion_val[0][xy + 1][0] = 0;
1045
+            s->current_picture.f.motion_val[0][xy + 1][1] = 0;
1046
+            s->current_picture.f.motion_val[0][xy + wrap][0] = 0;
1047
+            s->current_picture.f.motion_val[0][xy + wrap][1] = 0;
1048
+            s->current_picture.f.motion_val[0][xy + wrap + 1][0] = 0;
1049
+            s->current_picture.f.motion_val[0][xy + wrap + 1][1] = 0;
1050
+            v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1051
+            s->current_picture.f.motion_val[1][xy + 1][0] = 0;
1052
+            s->current_picture.f.motion_val[1][xy + 1][1] = 0;
1053
+            s->current_picture.f.motion_val[1][xy + wrap][0] = 0;
1054
+            s->current_picture.f.motion_val[1][xy + wrap][1] = 0;
1055
+            s->current_picture.f.motion_val[1][xy + wrap + 1][0] = 0;
1056
+            s->current_picture.f.motion_val[1][xy + wrap + 1][1] = 0;
1057
+        }
1058
+        return;
1059
+    }
1060
+
1061
+    off = ((n == 0) || (n == 1)) ? 1 : -1;
1062
+    /* predict A */
1063
+    if (s->mb_x || (n == 1) || (n == 3)) {
1064
+        if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
1065
+                || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
1066
+            A[0] = s->current_picture.f.motion_val[0][xy - 1][0];
1067
+            A[1] = s->current_picture.f.motion_val[0][xy - 1][1];
1068
+            a_valid = 1;
1069
+        } else { // current block has frame mv and cand. has field MV (so average)
1070
+            A[0] = (s->current_picture.f.motion_val[0][xy - 1][0]
1071
+                        + s->current_picture.f.motion_val[0][xy - 1 + off*wrap][0] + 1) >> 1;
1072
+            A[1] = (s->current_picture.f.motion_val[0][xy - 1][1]
1073
+                        + s->current_picture.f.motion_val[0][xy - 1 + off*wrap][1] + 1) >> 1;
1074
+            a_valid = 1;
1075
+        }
1076
+        if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
1077
+            a_valid = 0;
1078
+            A[0] = A[1] = 0;
1079
+        }
1080
+    } else A[0] = A[1] = 0;
1081
+    /* Predict B and C */
1082
+    B[0] = B[1] = C[0] = C[1] = 0;
1083
+    if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
1084
+        if (!s->first_slice_line) {
1085
+            if (!v->is_intra[s->mb_x - s->mb_stride]) {
1086
+                b_valid = 1;
1087
+                n_adj = n | 2;
1088
+                pos_b = s->block_index[n_adj] - 2 * wrap;
1089
+                if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
1090
+                    n_adj = (n & 2) | (n & 1);
1091
+                }
1092
+                B[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][0];
1093
+                B[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][1];
1094
+                if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
1095
+                    B[0] = (1 + B[0] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][0]) >> 1;
1096
+                    B[1] = (1 + B[1] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][1]) >> 1;
1097
+                }
974 1098
             }
1099
+            if (s->mb_width > 1) {
1100
+                if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
1101
+                    c_valid = 1;
1102
+                    n_adj = 2;
1103
+                    pos_c = s->block_index[2] - 2*wrap + 2;
1104
+                    if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1105
+                        n_adj = n & 2;
1106
+                    }
1107
+                    C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2*wrap + 2][0];
1108
+                    C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2*wrap + 2][1];
1109
+                    if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1110
+                        C[0] = (1 + C[0] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
1111
+                        C[1] = (1 + C[1] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
1112
+                    }
1113
+                    if (s->mb_x == s->mb_width - 1) {
1114
+                        if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
1115
+                            c_valid = 1;
1116
+                            n_adj = 3;
1117
+                            pos_c = s->block_index[3] - 2*wrap - 2;
1118
+                            if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1119
+                                n_adj = n | 1;
1120
+                            }
1121
+                            C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2*wrap - 2][0];
1122
+                            C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2*wrap - 2][1];
1123
+                            if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1124
+                                C[0] = (1 + C[0] + s->current_picture.f.motion_val[0][s->block_index[1] - 2*wrap - 2][0]) >> 1;
1125
+                                C[1] = (1 + C[1] + s->current_picture.f.motion_val[0][s->block_index[1] - 2*wrap - 2][1]) >> 1;
1126
+                            }
1127
+                        } else c_valid = 0;
1128
+                    }
1129
+                }
1130
+            }
1131
+        }
1132
+    } else {
1133
+        pos_b = s->block_index[1];
1134
+        b_valid = 1;
1135
+        B[0] = s->current_picture.f.motion_val[0][pos_b][0];
1136
+        B[1] = s->current_picture.f.motion_val[0][pos_b][1];
1137
+        pos_c = s->block_index[0];
1138
+        c_valid = 1;
1139
+        C[0] = s->current_picture.f.motion_val[0][pos_c][0];
1140
+        C[1] = s->current_picture.f.motion_val[0][pos_c][1];
1141
+    }
1142
+
1143
+    total_valid = a_valid + b_valid + c_valid;
1144
+    // check if predictor A is out of bounds
1145
+    if (!s->mb_x && !(n==1 || n==3)) {
1146
+        A[0] = A[1] = 0;
1147
+    }
1148
+    // check if predictor B is out of bounds
1149
+    if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
1150
+        B[0] = B[1] = C[0] = C[1] = 0;
1151
+    }
1152
+    if (!v->blk_mv_type[xy]) {
1153
+        if(s->mb_width == 1) {
1154
+            px = B[0];
1155
+            py = B[1];
975 1156
         } else {
976
-            if(is_intra[xy - 1])
977
-                sum = FFABS(px) + FFABS(py);
978
-            else
979
-                sum = FFABS(px - C[0]) + FFABS(py - C[1]);
980
-            if(sum > 32) {
981
-                if(get_bits1(&s->gb)) {
1157
+            if (total_valid >= 2) {
1158
+                px = mid_pred(A[0], B[0], C[0]);
1159
+                py = mid_pred(A[1], B[1], C[1]);
1160
+            } else if (total_valid) {
1161
+                if (a_valid) { px = A[0]; py = A[1]; }
1162
+                if (b_valid) { px = B[0]; py = B[1]; }
1163
+                if (c_valid) { px = C[0]; py = C[1]; }
1164
+            } else px = py = 0;
1165
+        }
1166
+    } else {
1167
+        if (a_valid)
1168
+            field_a = (A[1] & 4) ? 1 : 0;
1169
+        else
1170
+            field_a = 0;
1171
+        if (b_valid)
1172
+            field_b = (B[1] & 4) ? 1 : 0;
1173
+        else
1174
+            field_b = 0;
1175
+        if (c_valid)
1176
+            field_c = (C[1] & 4) ? 1 : 0;
1177
+        else
1178
+            field_c = 0;
1179
+
1180
+        num_oppfield = field_a + field_b + field_c;
1181
+        num_samefield = total_valid - num_oppfield;
1182
+        if (total_valid == 3) {
1183
+            if ((num_samefield == 3) || (num_oppfield == 3)) {
1184
+                px = mid_pred(A[0], B[0], C[0]);
1185
+                py = mid_pred(A[1], B[1], C[1]);
1186
+            } else if (num_samefield >= num_oppfield) {
1187
+                /* take one MV from same field set depending on priority
1188
+                the check for B may not be necessary */
1189
+                px = (!field_a) ? A[0] : B[0];
1190
+                py = (!field_a) ? A[1] : B[1];
1191
+            } else {
1192
+                px = (field_a) ? A[0] : B[0];
1193
+                py = (field_a) ? A[1] : B[1];
1194
+            }
1195
+        } else if (total_valid == 2) {
1196
+            if (num_samefield >= num_oppfield) {
1197
+                if (!field_a && a_valid) {
982 1198
                     px = A[0];
983 1199
                     py = A[1];
984
-                } else {
1200
+                } else if (!field_b && b_valid) {
1201
+                    px = B[0];
1202
+                    py = B[1];
1203
+                } else if (c_valid) {
1204
+                    px = C[0];
1205
+                    py = C[1];
1206
+                } else px = py = 0;
1207
+            } else {
1208
+                if (field_a && a_valid) {
1209
+                    px = A[0];
1210
+                    py = A[1];
1211
+                } else if (field_b && b_valid) {
1212
+                    px = B[0];
1213
+                    py = B[1];
1214
+                } else if (c_valid) {
985 1215
                     px = C[0];
986 1216
                     py = C[1];
987 1217
                 }
988 1218
             }
989
-        }
1219
+        } else if (total_valid == 1) {
1220
+            px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
1221
+            py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
1222
+        } else px = py = 0;
990 1223
     }
1224
+
991 1225
     /* store MV using signed modulus of MV range defined in 4.11 */
992 1226
     s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
993 1227
     s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
994
-    if(mv1) { /* duplicate motion data for 1-MV block */
995
-        s->current_picture.f.motion_val[0][xy + 1][0]        = s->current_picture.f.motion_val[0][xy][0];
996
-        s->current_picture.f.motion_val[0][xy + 1][1]        = s->current_picture.f.motion_val[0][xy][1];
997
-        s->current_picture.f.motion_val[0][xy + wrap][0]     = s->current_picture.f.motion_val[0][xy][0];
998
-        s->current_picture.f.motion_val[0][xy + wrap][1]     = s->current_picture.f.motion_val[0][xy][1];
1228
+    if(mvn == 1) { /* duplicate motion data for 1-MV block */
1229
+        s->current_picture.f.motion_val[0][xy + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1230
+        s->current_picture.f.motion_val[0][xy + 1][1] = s->current_picture.f.motion_val[0][xy][1];
1231
+        s->current_picture.f.motion_val[0][xy + wrap][0] = s->current_picture.f.motion_val[0][xy][0];
1232
+        s->current_picture.f.motion_val[0][xy + wrap][1] = s->current_picture.f.motion_val[0][xy][1];
999 1233
         s->current_picture.f.motion_val[0][xy + wrap + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1000 1234
         s->current_picture.f.motion_val[0][xy + wrap + 1][1] = s->current_picture.f.motion_val[0][xy][1];
1235
+    } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
1236
+        s->current_picture.f.motion_val[0][xy + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1237
+        s->current_picture.f.motion_val[0][xy + 1][1] = s->current_picture.f.motion_val[0][xy][1];
1238
+        s->mv[0][n + 1][0] = s->mv[0][n][0];
1239
+        s->mv[0][n + 1][1] = s->mv[0][n][1];
1001 1240
     }
1002 1241
 }
1003 1242
 
... ...
@@ -1009,13 +1929,21 @@ static void vc1_interp_mc(VC1Context *v)
1009 1009
     DSPContext *dsp = &v->s.dsp;
1010 1010
     uint8_t *srcY, *srcU, *srcV;
1011 1011
     int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1012
+    int off, off_uv;
1013
+    int v_edge_pos = s->v_edge_pos >> v->field_mode;
1012 1014
 
1013
-    if(!v->s.next_picture.f.data[0])return;
1015
+    if (!v->field_mode && !v->s.next_picture.f.data[0])
1016
+        return;
1014 1017
 
1015 1018
     mx = s->mv[1][0][0];
1016 1019
     my = s->mv[1][0][1];
1017 1020
     uvmx = (mx + ((mx & 3) == 3)) >> 1;
1018 1021
     uvmy = (my + ((my & 3) == 3)) >> 1;
1022
+    if (v->field_mode) {
1023
+        if (v->cur_field_type != v->ref_field_type[1])
1024
+            my = my - 2 + 4 * v->cur_field_type;
1025
+            uvmy = uvmy - 2 + 4 * v->cur_field_type;
1026
+    }
1019 1027
     if(v->fastuvmc) {
1020 1028
         uvmx = uvmx + ((uvmx<0)?-(uvmx&1):(uvmx&1));
1021 1029
         uvmy = uvmy + ((uvmy<0)?-(uvmy&1):(uvmy&1));
... ...
@@ -1045,6 +1973,12 @@ static void vc1_interp_mc(VC1Context *v)
1045 1045
     srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1046 1046
     srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1047 1047
 
1048
+    if (v->field_mode && v->ref_field_type[1]) {
1049
+        srcY += s->current_picture_ptr->f.linesize[0];
1050
+        srcU += s->current_picture_ptr->f.linesize[1];
1051
+        srcV += s->current_picture_ptr->f.linesize[2];
1052
+    }
1053
+
1048 1054
     /* for grayscale we should not try to read from unknown area */
1049 1055
     if(s->flags & CODEC_FLAG_GRAY) {
1050 1056
         srcU = s->edge_emu_buffer + 18 * s->linesize;
... ...
@@ -1053,17 +1987,17 @@ static void vc1_interp_mc(VC1Context *v)
1053 1053
 
1054 1054
     if(v->rangeredfrm
1055 1055
        || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel*3
1056
-       || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 16 - s->mspel*3){
1056
+       || (unsigned)(src_y - s->mspel) > v_edge_pos - (my&3) - 16 - s->mspel*3){
1057 1057
         uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
1058 1058
 
1059 1059
         srcY -= s->mspel * (1 + s->linesize);
1060 1060
         s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2,
1061
-                            src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
1061
+                            src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, v_edge_pos);
1062 1062
         srcY = s->edge_emu_buffer;
1063 1063
         s->dsp.emulated_edge_mc(uvbuf     , srcU, s->uvlinesize, 8+1, 8+1,
1064
-                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
1064
+                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1065 1065
         s->dsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
1066
-                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
1066
+                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1067 1067
         srcU = uvbuf;
1068 1068
         srcV = uvbuf + 16;
1069 1069
         /* if we deal with range reduction we need to scale source blocks */
... ...
@@ -1089,20 +2023,28 @@ static void vc1_interp_mc(VC1Context *v)
1089 1089
         srcY += s->mspel * (1 + s->linesize);
1090 1090
     }
1091 1091
 
1092
+    if (v->field_mode && v->cur_field_type) {
1093
+        off = s->current_picture_ptr->f.linesize[0];
1094
+        off_uv = s->current_picture_ptr->f.linesize[1];
1095
+    } else {
1096
+        off = 0;
1097
+        off_uv = 0;
1098
+    }
1099
+
1092 1100
     if(s->mspel) {
1093 1101
         dxy = ((my & 3) << 2) | (mx & 3);
1094
-        v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0]    , srcY    , s->linesize, v->rnd);
1095
-        v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
1102
+        v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off    , srcY    , s->linesize, v->rnd);
1103
+        v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
1096 1104
         srcY += s->linesize * 8;
1097
-        v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize    , srcY    , s->linesize, v->rnd);
1098
-        v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
1105
+        v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize    , srcY    , s->linesize, v->rnd);
1106
+        v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
1099 1107
     } else { // hpel mc
1100 1108
         dxy = (my & 2) | ((mx & 2) >> 1);
1101 1109
 
1102 1110
         if(!v->rnd)
1103
-            dsp->avg_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
1111
+            dsp->avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
1104 1112
         else
1105
-            dsp->avg_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
1113
+            dsp->avg_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
1106 1114
     }
1107 1115
 
1108 1116
     if(s->flags & CODEC_FLAG_GRAY) return;
... ...
@@ -1110,11 +2052,11 @@ static void vc1_interp_mc(VC1Context *v)
1110 1110
     uvmx = (uvmx&3)<<1;
1111 1111
     uvmy = (uvmy&3)<<1;
1112 1112
     if(!v->rnd){
1113
-        dsp->avg_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
1114
-        dsp->avg_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
1113
+        dsp->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
1114
+        dsp->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
1115 1115
     }else{
1116
-        v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
1117
-        v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
1116
+        v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
1117
+        v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
1118 1118
     }
1119 1119
 }
1120 1120
 
... ...
@@ -1137,6 +2079,19 @@ static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
1137 1137
 #endif
1138 1138
 }
1139 1139
 
1140
+static av_always_inline int scale_mv_intfi(int value, int bfrac, int inv, int qs, int qs_last)
1141
+{
1142
+    int n = bfrac;
1143
+
1144
+    if (inv)
1145
+        n -= 256;
1146
+    n <<= !qs_last;
1147
+    if (!qs)
1148
+        return (value * n + 255) >> 9;
1149
+    else
1150
+        return (value * n + 128) >> 8;
1151
+}
1152
+
1140 1153
 /** Reconstruct motion vector for B-frame and do motion compensation
1141 1154
  */
1142 1155
 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mode)
... ...
@@ -1185,27 +2140,29 @@ static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2], int
1185 1185
     xy = s->block_index[0];
1186 1186
 
1187 1187
     if(s->mb_intra) {
1188
-        s->current_picture.f.motion_val[0][xy][0] =
1189
-        s->current_picture.f.motion_val[0][xy][1] =
1190
-        s->current_picture.f.motion_val[1][xy][0] =
1191
-        s->current_picture.f.motion_val[1][xy][1] = 0;
1188
+        s->current_picture.f.motion_val[0][xy + v->blocks_off][0] =
1189
+        s->current_picture.f.motion_val[0][xy + v->blocks_off][1] =
1190
+        s->current_picture.f.motion_val[1][xy + v->blocks_off][0] =
1191
+        s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
1192 1192
         return;
1193 1193
     }
1194
-    s->mv[0][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
1195
-    s->mv[0][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
1196
-    s->mv[1][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
1197
-    s->mv[1][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
1194
+    if (!v->field_mode) {
1195
+        s->mv[0][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
1196
+        s->mv[0][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
1197
+        s->mv[1][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
1198
+        s->mv[1][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
1198 1199
 
1199
-    /* Pullback predicted motion vectors as specified in 8.4.5.4 */
1200
-    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));
1201
-    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));
1202
-    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));
1203
-    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));
1200
+        /* Pullback predicted motion vectors as specified in 8.4.5.4 */
1201
+        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));
1202
+        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));
1203
+        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));
1204
+        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));
1205
+    }
1204 1206
     if(direct) {
1205
-        s->current_picture.f.motion_val[0][xy][0] = s->mv[0][0][0];
1206
-        s->current_picture.f.motion_val[0][xy][1] = s->mv[0][0][1];
1207
-        s->current_picture.f.motion_val[1][xy][0] = s->mv[1][0][0];
1208
-        s->current_picture.f.motion_val[1][xy][1] = s->mv[1][0][1];
1207
+        s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = s->mv[0][0][0];
1208
+        s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = s->mv[0][0][1];
1209
+        s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = s->mv[1][0][0];
1210
+        s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = s->mv[1][0][1];
1209 1211
         return;
1210 1212
     }
1211 1213
 
... ...
@@ -1372,6 +2329,59 @@ static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2], int
1372 1372
     s->current_picture.f.motion_val[1][xy][1] = s->mv[1][0][1];
1373 1373
 }
1374 1374
 
1375
+static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
1376
+{
1377
+    int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
1378
+    MpegEncContext *s = &v->s;
1379
+    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1380
+
1381
+    if (v->bmvtype == BMV_TYPE_DIRECT) {
1382
+        int total_opp, k, f;
1383
+        if (s->next_picture.f.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
1384
+            s->mv[0][0][0] = scale_mv_intfi(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0], v->bfraction, 0, s->quarter_sample, v->qs_last);
1385
+            s->mv[0][0][1] = scale_mv_intfi(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1], v->bfraction, 0, s->quarter_sample, v->qs_last);
1386
+            s->mv[1][0][0] = scale_mv_intfi(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0], v->bfraction, 1, s->quarter_sample, v->qs_last);
1387
+            s->mv[1][0][1] = scale_mv_intfi(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1], v->bfraction, 1, s->quarter_sample, v->qs_last);
1388
+
1389
+            total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
1390
+                      + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
1391
+                      + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
1392
+                      + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
1393
+            f = (total_opp > 2) ? 1 : 0;
1394
+        } else {
1395
+            s->mv[0][0][0] = s->mv[0][0][1] = 0;
1396
+            s->mv[1][0][0] = s->mv[1][0][1] = 0;
1397
+            f = 0;
1398
+        }
1399
+        v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
1400
+        for (k = 0; k < 4; k++) {
1401
+            s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
1402
+            s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
1403
+            s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
1404
+            s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
1405
+            v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
1406
+            v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
1407
+        }
1408
+        return;
1409
+    }
1410
+    if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
1411
+        vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0],   1, v->range_x, v->range_y, v->mb_type[0], pred_flag[0], 0);
1412
+        vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1],   1, v->range_x, v->range_y, v->mb_type[0], pred_flag[1], 1);
1413
+        return;
1414
+    }
1415
+    if (dir) { // backward
1416
+        vc1_pred_mv(v, n, dmv_x[1], dmv_y[1], mv1, v->range_x, v->range_y, v->mb_type[0], pred_flag[1], 1);
1417
+        if (n == 3 || mv1) {
1418
+            vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0],   1, v->range_x, v->range_y, v->mb_type[0],            0, 0);
1419
+        }
1420
+    } else { // forward
1421
+        vc1_pred_mv(v, n, dmv_x[0], dmv_y[0], mv1, v->range_x, v->range_y, v->mb_type[0], pred_flag[0], 0);
1422
+        if (n == 3 || mv1) {
1423
+            vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1],   1, v->range_x, v->range_y, v->mb_type[0],            0, 1);
1424
+        }
1425
+    }
1426
+}
1427
+
1375 1428
 /** Get predicted DC value for I-frames only
1376 1429
  * prediction dir: left=0, top=1
1377 1430
  * @param s MpegEncContext
... ...
@@ -1866,12 +2876,20 @@ static int vc1_decode_i_block_adv(VC1Context *v, DCTELEM block[64], int n, int c
1866 1866
         int k;
1867 1867
 
1868 1868
         if(v->s.ac_pred) {
1869
-            if(!dc_pred_dir)
1870
-                zz_table = v->zz_8x8[2];
1869
+            if (!use_pred && v->fcm == 1) {
1870
+                zz_table = v->zzi_8x8;
1871
+            } else {
1872
+                if(!dc_pred_dir) //top
1873
+                    zz_table = v->zz_8x8[2];
1874
+                else //left
1875
+                    zz_table = v->zz_8x8[3];
1876
+            }
1877
+        } else {
1878
+            if (v->fcm != 1)
1879
+                zz_table = v->zz_8x8[1];
1871 1880
             else
1872
-                zz_table = v->zz_8x8[3];
1873
-        } else
1874
-            zz_table = v->zz_8x8[1];
1881
+                zz_table = v->zzi_8x8;
1882
+        }
1875 1883
 
1876 1884
         while (!last) {
1877 1885
             vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
... ...
@@ -2076,7 +3094,18 @@ static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n, int c
2076 2076
             i += skip;
2077 2077
             if(i > 63)
2078 2078
                 break;
2079
-            block[v->zz_8x8[0][i++]] = value;
2079
+            if (v->fcm == 0)
2080
+                block[v->zz_8x8[0][i++]] = value;
2081
+            else {
2082
+                if(use_pred && (v->fcm == 1)) {
2083
+                    if(!dc_pred_dir) //top
2084
+                        block[v->zz_8x8[2][i++]] = value;
2085
+                    else //left
2086
+                        block[v->zz_8x8[3][i++]] = value;
2087
+                } else {
2088
+                    block[v->zzi_8x8[i++]] = value;
2089
+                }
2090
+            }
2080 2091
         }
2081 2092
 
2082 2093
         /* apply AC prediction if needed */
... ...
@@ -2217,7 +3246,10 @@ static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n, int mquan
2217 2217
             i += skip;
2218 2218
             if(i > 63)
2219 2219
                 break;
2220
-            idx = v->zz_8x8[0][i++];
2220
+            if (!v->interlace)
2221
+                idx = v->zz_8x8[0][i++];
2222
+            else
2223
+                idx = v->zzi_8x8[i++];
2221 2224
             block[idx] = value * scale;
2222 2225
             if(!v->pquantizer)
2223 2226
                 block[idx] += (block[idx] < 0) ? -mquant : mquant;
... ...
@@ -2242,7 +3274,10 @@ static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n, int mquan
2242 2242
                 i += skip;
2243 2243
                 if(i > 15)
2244 2244
                     break;
2245
-                idx = ff_vc1_simple_progressive_4x4_zz[i++];
2245
+                if (!v->interlace)
2246
+                    idx = ff_vc1_simple_progressive_4x4_zz[i++];
2247
+                else
2248
+                    idx = ff_vc1_adv_interlaced_4x4_zz[i++];
2246 2249
                 block[idx + off] = value * scale;
2247 2250
                 if(!v->pquantizer)
2248 2251
                     block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
... ...
@@ -2266,7 +3301,10 @@ static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n, int mquan
2266 2266
                 i += skip;
2267 2267
                 if(i > 31)
2268 2268
                     break;
2269
-                idx = v->zz_8x4[i++]+off;
2269
+                if (!v->interlace)
2270
+                    idx = v->zz_8x4[i++] + off;
2271
+                else
2272
+                    idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
2270 2273
                 block[idx] = value * scale;
2271 2274
                 if(!v->pquantizer)
2272 2275
                     block[idx] += (block[idx] < 0) ? -mquant : mquant;
... ...
@@ -2290,7 +3328,10 @@ static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n, int mquan
2290 2290
                 i += skip;
2291 2291
                 if(i > 31)
2292 2292
                     break;
2293
-                idx = v->zz_4x8[i++]+off;
2293
+                if (!v->interlace)
2294
+                    idx = v->zz_4x8[i++] + off;
2295
+                else
2296
+                    idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
2294 2297
                 block[idx] = value * scale;
2295 2298
                 if(!v->pquantizer)
2296 2299
                     block[idx] += (block[idx] < 0) ? -mquant : mquant;
... ...
@@ -2463,7 +3504,7 @@ static void vc1_apply_p_loop_filter(VC1Context *v)
2463 2463
     }
2464 2464
 }
2465 2465
 
2466
-/** Decode one P-frame MB (in Simple/Main profile)
2466
+/** Decode one P-frame MB
2467 2467
  */
2468 2468
 static int vc1_decode_p_mb(VC1Context *v)
2469 2469
 {
... ...
@@ -2506,7 +3547,7 @@ static int vc1_decode_p_mb(VC1Context *v)
2506 2506
                 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
2507 2507
             }
2508 2508
             s->current_picture.f.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
2509
-            vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
2509
+            vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
2510 2510
 
2511 2511
             /* FIXME Set DC val for inter block ? */
2512 2512
             if (s->mb_intra && !mb_has_coeffs)
... ...
@@ -2517,7 +3558,8 @@ static int vc1_decode_p_mb(VC1Context *v)
2517 2517
             }
2518 2518
             else if (mb_has_coeffs)
2519 2519
             {
2520
-                if (s->mb_intra) s->ac_pred = get_bits1(gb);
2520
+                if (s->mb_intra)
2521
+                    s->ac_pred = get_bits1(gb);
2521 2522
                 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2522 2523
                 GET_MQUANT();
2523 2524
             }
... ...
@@ -2578,7 +3620,7 @@ static int vc1_decode_p_mb(VC1Context *v)
2578 2578
             }
2579 2579
             s->current_picture.f.mb_type[mb_pos] = MB_TYPE_SKIP;
2580 2580
             s->current_picture.f.qscale_table[mb_pos] = 0;
2581
-            vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);
2581
+            vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
2582 2582
             vc1_mc_1mv(v, 0);
2583 2583
         }
2584 2584
     } //1MV mode
... ...
@@ -2602,8 +3644,8 @@ static int vc1_decode_p_mb(VC1Context *v)
2602 2602
                     if(val) {
2603 2603
                         GET_MVDATA(dmv_x, dmv_y);
2604 2604
                     }
2605
-                    vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);
2606
-                    if(!s->mb_intra) vc1_mc_4mv_luma(v, i);
2605
+                    vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
2606
+                    if(!s->mb_intra) vc1_mc_4mv_luma(v, i, 0);
2607 2607
                     intra_count += s->mb_intra;
2608 2608
                     is_intra[i] = s->mb_intra;
2609 2609
                     is_coded[i] = mb_has_coeffs;
... ...
@@ -2612,7 +3654,7 @@ static int vc1_decode_p_mb(VC1Context *v)
2612 2612
                     is_intra[i] = (intra_count >= 3);
2613 2613
                     is_coded[i] = val;
2614 2614
                 }
2615
-                if(i == 4) vc1_mc_4mv_chroma(v);
2615
+                if(i == 4) vc1_mc_4mv_chroma(v, 0);
2616 2616
                 v->mb_type[0][s->block_index[i]] = is_intra[i];
2617 2617
                 if(!coded_inter) coded_inter = !is_intra[i] & is_coded[i];
2618 2618
             }
... ...
@@ -2682,10 +3724,10 @@ static int vc1_decode_p_mb(VC1Context *v)
2682 2682
             }
2683 2683
             for (i=0; i<4; i++)
2684 2684
             {
2685
-                vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0]);
2686
-                vc1_mc_4mv_luma(v, i);
2685
+                vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
2686
+                vc1_mc_4mv_luma(v, i, 0);
2687 2687
             }
2688
-            vc1_mc_4mv_chroma(v);
2688
+            vc1_mc_4mv_chroma(v, 0);
2689 2689
             s->current_picture.f.qscale_table[mb_pos] = 0;
2690 2690
         }
2691 2691
     }
... ...
@@ -2697,6 +3739,324 @@ end:
2697 2697
     return 0;
2698 2698
 }
2699 2699
 
2700
+/* Decode one macroblock in an interlaced frame p picture */
2701
+
2702
+static int vc1_decode_p_mb_intfr(VC1Context *v)
2703
+{
2704
+    MpegEncContext *s = &v->s;
2705
+    GetBitContext *gb = &s->gb;
2706
+    int i;
2707
+    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2708
+    int cbp = 0; /* cbp decoding stuff */
2709
+    int mqdiff, mquant; /* MB quantization */
2710
+    int ttmb = v->ttfrm; /* MB Transform type */
2711
+
2712
+    int mb_has_coeffs = 1; /* last_flag */
2713
+    int dmv_x, dmv_y; /* Differential MV components */
2714
+    int val; /* temp value */
2715
+    int first_block = 1;
2716
+    int dst_idx, off;
2717
+    int skipped, fourmv = 0, twomv = 0;
2718
+    int block_cbp = 0, pat, block_tt = 0;
2719
+    int idx_mbmode = 0, mvbp;
2720
+    int stride_y, fieldtx;
2721
+
2722
+    mquant = v->pq; /* Loosy initialization */
2723
+
2724
+    if (v->skip_is_raw)
2725
+        skipped = get_bits1(gb);
2726
+    else
2727
+        skipped = v->s.mbskip_table[mb_pos];
2728
+    if (!skipped) {
2729
+        if (v->fourmvswitch)
2730
+            idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
2731
+        else
2732
+            idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2);              // in a single line
2733
+        switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
2734
+        /* store the motion vector type in a flag (useful later) */
2735
+        case MV_PMODE_INTFR_4MV:
2736
+            fourmv = 1;
2737
+            v->blk_mv_type[s->block_index[0]] = 0;
2738
+            v->blk_mv_type[s->block_index[1]] = 0;
2739
+            v->blk_mv_type[s->block_index[2]] = 0;
2740
+            v->blk_mv_type[s->block_index[3]] = 0;
2741
+            break;
2742
+        case MV_PMODE_INTFR_4MV_FIELD:
2743
+            fourmv = 1;
2744
+            v->blk_mv_type[s->block_index[0]] = 1;
2745
+            v->blk_mv_type[s->block_index[1]] = 1;
2746
+            v->blk_mv_type[s->block_index[2]] = 1;
2747
+            v->blk_mv_type[s->block_index[3]] = 1;
2748
+            break;
2749
+        case MV_PMODE_INTFR_2MV_FIELD:
2750
+            twomv = 1;
2751
+            v->blk_mv_type[s->block_index[0]] = 1;
2752
+            v->blk_mv_type[s->block_index[1]] = 1;
2753
+            v->blk_mv_type[s->block_index[2]] = 1;
2754
+            v->blk_mv_type[s->block_index[3]] = 1;
2755
+            break;
2756
+        case MV_PMODE_INTFR_1MV:
2757
+            v->blk_mv_type[s->block_index[0]] = 0;
2758
+            v->blk_mv_type[s->block_index[1]] = 0;
2759
+            v->blk_mv_type[s->block_index[2]] = 0;
2760
+            v->blk_mv_type[s->block_index[3]] = 0;
2761
+            break;
2762
+        }
2763
+        if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
2764
+            s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
2765
+            s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
2766
+            s->current_picture.f.mb_type[mb_pos] = MB_TYPE_INTRA;
2767
+            s->mb_intra = v->is_intra[s->mb_x] = 1;
2768
+            for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 1;
2769
+            fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
2770
+            mb_has_coeffs = get_bits1(gb);
2771
+            if (mb_has_coeffs)
2772
+                cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2773
+            v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
2774
+            GET_MQUANT();
2775
+            s->current_picture.f.qscale_table[mb_pos] = mquant;
2776
+            /* Set DC scale - y and c use the same (not sure if necessary here) */
2777
+            s->y_dc_scale = s->y_dc_scale_table[mquant];
2778
+            s->c_dc_scale = s->c_dc_scale_table[mquant];
2779
+            dst_idx = 0;
2780
+            for (i=0; i<6; i++) {
2781
+                s->dc_val[0][s->block_index[i]] = 0;
2782
+                dst_idx += i >> 2;
2783
+                val = ((cbp >> (5 - i)) & 1);
2784
+                v->mb_type[0][s->block_index[i]] = s->mb_intra;
2785
+                v->a_avail = v->c_avail = 0;
2786
+                if(i == 2 || i == 3 || !s->first_slice_line)
2787
+                    v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2788
+                if(i == 1 || i == 3 || s->mb_x)
2789
+                    v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2790
+
2791
+                vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
2792
+                if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
2793
+                v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2794
+                if (i < 4) {
2795
+                    stride_y = s->linesize << fieldtx;
2796
+                    off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
2797
+                } else {
2798
+                    stride_y = s->uvlinesize;
2799
+                    off = 0;
2800
+                }
2801
+                s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
2802
+                //TODO: loop filter
2803
+            }
2804
+
2805
+        } else {    //inter MB
2806
+            mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
2807
+            if (mb_has_coeffs)
2808
+                cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2809
+            if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
2810
+                v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
2811
+            } else {
2812
+                if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
2813
+                        || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
2814
+                    v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
2815
+                }
2816
+            }
2817
+            s->mb_intra = v->is_intra[s->mb_x] = 0;
2818
+            for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
2819
+            fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
2820
+            /* for all motion vector read MVDATA and motion compensate each block */
2821
+            dst_idx = 0;
2822
+            if (fourmv) {
2823
+                mvbp = v->fourmvbp;
2824
+                for (i=0; i<6; i++) {
2825
+                    if (i < 4) {
2826
+                        dmv_x = dmv_y = 0;
2827
+                        val = ((mvbp >> (3 - i)) & 1);
2828
+                        if(val) {
2829
+                            get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2830
+                        }
2831
+                        vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);
2832
+                        vc1_mc_4mv_luma(v, i, 0);
2833
+                    } else if (i == 4) {
2834
+                        vc1_mc_4mv_chroma4(v);
2835
+                    }
2836
+                }
2837
+            } else if (twomv) {
2838
+                mvbp = v->twomvbp;
2839
+                dmv_x = dmv_y = 0;
2840
+                if (mvbp & 2) {
2841
+                    get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2842
+                }
2843
+                vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0]);
2844
+                vc1_mc_4mv_luma(v, 0, 0);
2845
+                vc1_mc_4mv_luma(v, 1, 0);
2846
+                dmv_x = dmv_y = 0;
2847
+                if (mvbp & 1) {
2848
+                    get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2849
+                }
2850
+                vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0]);
2851
+                vc1_mc_4mv_luma(v, 2, 0);
2852
+                vc1_mc_4mv_luma(v, 3, 0);
2853
+                vc1_mc_4mv_chroma4(v);
2854
+            } else {
2855
+                mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
2856
+                if (mvbp) {
2857
+                    get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2858
+                }
2859
+                vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
2860
+                vc1_mc_1mv(v, 0);
2861
+            }
2862
+            if (cbp)
2863
+                GET_MQUANT();  // p. 227
2864
+            s->current_picture.f.qscale_table[mb_pos] = mquant;
2865
+            if (!v->ttmbf && cbp)
2866
+                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2867
+            for (i=0; i<6; i++) {
2868
+                s->dc_val[0][s->block_index[i]] = 0;
2869
+                dst_idx += i >> 2;
2870
+                val = ((cbp >> (5 - i)) & 1);
2871
+                if (!fieldtx)
2872
+                    off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2873
+                else
2874
+                    off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
2875
+                if (val) {
2876
+                    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 << fieldtx), (i&4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
2877
+                    block_cbp |= pat << (i << 2);
2878
+                    if (!v->ttmbf && ttmb < 8)
2879
+                        ttmb = -1;
2880
+                    first_block = 0;
2881
+                }
2882
+            }
2883
+        }
2884
+    } else { // skipped
2885
+        s->mb_intra = v->is_intra[s->mb_x] = 0;
2886
+        for(i = 0; i < 6; i++) {
2887
+            v->mb_type[0][s->block_index[i]] = 0;
2888
+            s->dc_val[0][s->block_index[i]] = 0;
2889
+        }
2890
+        s->current_picture.f.mb_type[mb_pos] = MB_TYPE_SKIP;
2891
+        s->current_picture.f.qscale_table[mb_pos] = 0;
2892
+        v->blk_mv_type[s->block_index[0]] = 0;
2893
+        v->blk_mv_type[s->block_index[1]] = 0;
2894
+        v->blk_mv_type[s->block_index[2]] = 0;
2895
+        v->blk_mv_type[s->block_index[3]] = 0;
2896
+        vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);
2897
+        vc1_mc_1mv(v, 0);
2898
+    }
2899
+    if (s->mb_x == s->mb_width - 1)
2900
+        memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
2901
+    return 0;
2902
+}
2903
+
2904
+static int vc1_decode_p_mb_intfi(VC1Context *v)
2905
+{
2906
+    MpegEncContext *s = &v->s;
2907
+    GetBitContext *gb = &s->gb;
2908
+    int i;
2909
+    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2910
+    int cbp = 0; /* cbp decoding stuff */
2911
+    int mqdiff, mquant; /* MB quantization */
2912
+    int ttmb = v->ttfrm; /* MB Transform type */
2913
+
2914
+    int mb_has_coeffs = 1; /* last_flag */
2915
+    int dmv_x, dmv_y; /* Differential MV components */
2916
+    int val; /* temp values */
2917
+    int first_block = 1;
2918
+    int dst_idx, off;
2919
+    int pred_flag;
2920
+    int block_cbp = 0, pat, block_tt = 0;
2921
+    int idx_mbmode = 0;
2922
+
2923
+    mquant = v->pq; /* Loosy initialization */
2924
+
2925
+    idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
2926
+    if (idx_mbmode <= 1) { // intra MB
2927
+        s->mb_intra = v->is_intra[s->mb_x] = 1;
2928
+        s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
2929
+        s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
2930
+        s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
2931
+        GET_MQUANT();
2932
+        s->current_picture.f.qscale_table[mb_pos] = mquant;
2933
+        /* Set DC scale - y and c use the same (not sure if necessary here) */
2934
+        s->y_dc_scale = s->y_dc_scale_table[mquant];
2935
+        s->c_dc_scale = s->c_dc_scale_table[mquant];
2936
+        v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
2937
+        mb_has_coeffs = idx_mbmode & 1;
2938
+        if (mb_has_coeffs)
2939
+            cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
2940
+        dst_idx = 0;
2941
+        for (i=0; i<6; i++) {
2942
+            s->dc_val[0][s->block_index[i]] = 0;
2943
+            v->mb_type[0][s->block_index[i]] = 1;
2944
+            dst_idx += i >> 2;
2945
+            val = ((cbp >> (5 - i)) & 1);
2946
+            v->a_avail = v->c_avail = 0;
2947
+            if(i == 2 || i == 3 || !s->first_slice_line)
2948
+                v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2949
+            if(i == 1 || i == 3 || s->mb_x)
2950
+                v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2951
+
2952
+            vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
2953
+            if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
2954
+            v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2955
+            off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2956
+            off += v->cur_field_type ? ((i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0]) : 0;
2957
+            s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
2958
+            //TODO: loop filter
2959
+        }
2960
+    } else {
2961
+        s->mb_intra = v->is_intra[s->mb_x] = 0;
2962
+        s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
2963
+        for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
2964
+        if (idx_mbmode <= 5) { // 1-MV
2965
+            dmv_x = dmv_y = 0;
2966
+            if (idx_mbmode & 1) {
2967
+                get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
2968
+            }
2969
+            vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
2970
+            vc1_mc_1mv(v, 0);
2971
+            mb_has_coeffs = !(idx_mbmode & 2);
2972
+        } else { // 4-MV
2973
+            v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
2974
+            for (i=0; i<6; i++) {
2975
+                if (i < 4) {
2976
+                    dmv_x = dmv_y = pred_flag = 0;
2977
+                    val = ((v->fourmvbp >> (3 - i)) & 1);
2978
+                    if(val) {
2979
+                        get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
2980
+                    }
2981
+                    vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
2982
+                    vc1_mc_4mv_luma(v, i, 0);
2983
+                } else if (i == 4)
2984
+                    vc1_mc_4mv_chroma(v, 0);
2985
+            }
2986
+            mb_has_coeffs = idx_mbmode & 1;
2987
+        }
2988
+        if (mb_has_coeffs)
2989
+            cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2990
+        if (cbp) {
2991
+            GET_MQUANT();
2992
+        }
2993
+        s->current_picture.f.qscale_table[mb_pos] = mquant;
2994
+        if (!v->ttmbf && cbp) {
2995
+            ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2996
+        }
2997
+        dst_idx = 0;
2998
+        for (i=0; i<6; i++) {
2999
+            s->dc_val[0][s->block_index[i]] = 0;
3000
+            dst_idx += i >> 2;
3001
+            val = ((cbp >> (5 - i)) & 1);
3002
+            off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
3003
+            if (v->cur_field_type)
3004
+                off += (i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0];
3005
+            if(val) {
3006
+                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);
3007
+                block_cbp |= pat << (i << 2);
3008
+                if(!v->ttmbf && ttmb < 8) ttmb = -1;
3009
+                first_block = 0;
3010
+            }
3011
+        }
3012
+    }
3013
+    if (s->mb_x == s->mb_width - 1)
3014
+        memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
3015
+    return 0;
3016
+}
3017
+
2700 3018
 /** Decode one B-frame MB (in Main profile)
2701 3019
  */
2702 3020
 static void vc1_decode_b_mb(VC1Context *v)
... ...
@@ -2841,6 +4201,153 @@ static void vc1_decode_b_mb(VC1Context *v)
2841 2841
     }
2842 2842
 }
2843 2843
 
2844
+/** Decode one B-frame MB (in interlaced field B picture)
2845
+ */
2846
+static void vc1_decode_b_mb_intfi(VC1Context *v)
2847
+{
2848
+    MpegEncContext *s = &v->s;
2849
+    GetBitContext *gb = &s->gb;
2850
+    int i, j;
2851
+    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2852
+    int cbp = 0; /* cbp decoding stuff */
2853
+    int mqdiff, mquant; /* MB quantization */
2854
+    int ttmb = v->ttfrm; /* MB Transform type */
2855
+    int mb_has_coeffs = 0; /* last_flag */
2856
+    int val; /* temp value */
2857
+    int first_block = 1;
2858
+    int dst_idx, off;
2859
+    int fwd;
2860
+    int dmv_x[2], dmv_y[2], pred_flag[2];
2861
+    int bmvtype = BMV_TYPE_BACKWARD;
2862
+    int idx_mbmode, interpmvp;
2863
+
2864
+    mquant = v->pq; /* Loosy initialization */
2865
+    s->mb_intra = 0;
2866
+
2867
+    idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
2868
+    if (idx_mbmode <= 1) { // intra MB
2869
+        s->mb_intra = v->is_intra[s->mb_x] = 1;
2870
+        s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
2871
+        s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
2872
+        s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
2873
+        GET_MQUANT();
2874
+        s->current_picture.f.qscale_table[mb_pos] = mquant;
2875
+        /* Set DC scale - y and c use the same (not sure if necessary here) */
2876
+        s->y_dc_scale = s->y_dc_scale_table[mquant];
2877
+        s->c_dc_scale = s->c_dc_scale_table[mquant];
2878
+        v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
2879
+        mb_has_coeffs = idx_mbmode & 1;
2880
+        if (mb_has_coeffs)
2881
+            cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
2882
+        dst_idx = 0;
2883
+        for (i=0; i<6; i++) {
2884
+            s->dc_val[0][s->block_index[i]] = 0;
2885
+            dst_idx += i >> 2;
2886
+            val = ((cbp >> (5 - i)) & 1);
2887
+            v->mb_type[0][s->block_index[i]] = s->mb_intra;
2888
+            v->a_avail = v->c_avail = 0;
2889
+            if(i == 2 || i == 3 || !s->first_slice_line)
2890
+                v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2891
+            if(i == 1 || i == 3 || s->mb_x)
2892
+                v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2893
+
2894
+            vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
2895
+            if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
2896
+            v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2897
+            if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
2898
+            off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2899
+            off += v->cur_field_type ? ((i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0]) : 0;
2900
+            s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
2901
+            //TODO: yet to perform loop filter
2902
+        }
2903
+    } else {
2904
+        s->mb_intra = v->is_intra[s->mb_x] = 0;
2905
+        s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
2906
+        for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
2907
+        if (v->fmb_is_raw)
2908
+            fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
2909
+        else
2910
+            fwd = v->forward_mb_plane[mb_pos];
2911
+        if (idx_mbmode <= 5) { // 1-MV
2912
+            dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
2913
+            pred_flag[0] = pred_flag[1] = 0;
2914
+            if (fwd)
2915
+                bmvtype = BMV_TYPE_FORWARD;
2916
+            else {
2917
+                bmvtype = decode012(gb);
2918
+                switch(bmvtype) {
2919
+                case 0:
2920
+                    bmvtype = BMV_TYPE_BACKWARD;
2921
+                    break;
2922
+                case 1:
2923
+                    bmvtype = BMV_TYPE_DIRECT;
2924
+                    break;
2925
+                case 2:
2926
+                    bmvtype = BMV_TYPE_INTERPOLATED;
2927
+                    interpmvp = get_bits1(gb);
2928
+                }
2929
+            }
2930
+            v->bmvtype = bmvtype;
2931
+            if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
2932
+                get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
2933
+            }
2934
+            if (bmvtype == BMV_TYPE_INTERPOLATED && interpmvp) {
2935
+                get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
2936
+            }
2937
+            if (bmvtype == BMV_TYPE_DIRECT) {
2938
+                dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
2939
+                dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
2940
+            }
2941
+            vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
2942
+            vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
2943
+            mb_has_coeffs = !(idx_mbmode & 2);
2944
+        } else { // 4-MV
2945
+            if (fwd)
2946
+                bmvtype = BMV_TYPE_FORWARD;
2947
+            v->bmvtype = bmvtype;
2948
+            v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
2949
+            for (i=0; i<6; i++) {
2950
+                if (i < 4) {
2951
+                    dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
2952
+                    dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
2953
+                    val = ((v->fourmvbp >> (3 - i)) & 1);
2954
+                    if(val) {
2955
+                        get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
2956
+                    }
2957
+                    vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
2958
+                    vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD);
2959
+                } else if (i == 4)
2960
+                    vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
2961
+            }
2962
+            mb_has_coeffs = idx_mbmode & 1;
2963
+        }
2964
+        if (mb_has_coeffs)
2965
+            cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2966
+        if (cbp) {
2967
+            GET_MQUANT();
2968
+        }
2969
+        s->current_picture.f.qscale_table[mb_pos] = mquant;
2970
+        if (!v->ttmbf && cbp) {
2971
+            ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2972
+        }
2973
+        dst_idx = 0;
2974
+        for (i=0; i<6; i++) {
2975
+            s->dc_val[0][s->block_index[i]] = 0;
2976
+            dst_idx += i >> 2;
2977
+            val = ((cbp >> (5 - i)) & 1);
2978
+            off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
2979
+            if (v->cur_field_type)
2980
+                off += (i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0];
2981
+            if(val) {
2982
+                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);
2983
+                if(!v->ttmbf && ttmb < 8)
2984
+                    ttmb = -1;
2985
+                first_block = 0;
2986
+            }
2987
+        }
2988
+    }
2989
+}
2990
+
2844 2991
 /** Decode blocks of I-frame
2845 2992
  */
2846 2993
 static void vc1_decode_i_blocks(VC1Context *v)
... ...
@@ -3029,13 +4536,15 @@ static void vc1_decode_i_blocks_adv(VC1Context *v)
3029 3029
             ff_update_block_index(s);
3030 3030
             s->dsp.clear_blocks(block[0]);
3031 3031
             mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3032
-            s->current_picture.f.mb_type[mb_pos] = MB_TYPE_INTRA;
3033
-            s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
3034
-            s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
3032
+            s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
3033
+            s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
3034
+            s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
3035 3035
 
3036 3036
             // do actual MB decoding and displaying
3037
+            if (v->fieldtx_is_raw)
3038
+                v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
3037 3039
             cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
3038
-            if(v->acpred_is_raw)
3040
+            if( v->acpred_is_raw)
3039 3041
                 v->s.ac_pred = get_bits1(&v->s.gb);
3040 3042
             else
3041 3043
                 v->s.ac_pred = v->acpred_plane[mb_pos];
... ...
@@ -3074,6 +4583,7 @@ static void vc1_decode_i_blocks_adv(VC1Context *v)
3074 3074
             if(v->s.loop_filter) vc1_loop_filter_iblk_delayed(v, v->pq);
3075 3075
 
3076 3076
             if(get_bits_count(&s->gb) > v->bits) {
3077
+                // TODO: may need modification to handle slice coding
3077 3078
                 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
3078 3079
                 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
3079 3080
                 return;
... ...
@@ -3096,7 +4606,7 @@ static void vc1_decode_i_blocks_adv(VC1Context *v)
3096 3096
     }
3097 3097
     if (v->s.loop_filter)
3098 3098
         ff_draw_horiz_band(s, (s->end_mb_y-1)*16, 16);
3099
-    ff_er_add_slice(s, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, (AC_END|DC_END|MV_END));
3099
+    ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1, (s->end_mb_y << v->field_mode) - 1, (AC_END|DC_END|MV_END));
3100 3100
 }
3101 3101
 
3102 3102
 static void vc1_decode_p_blocks(VC1Context *v)
... ...
@@ -3138,10 +4648,15 @@ static void vc1_decode_p_blocks(VC1Context *v)
3138 3138
         for(; s->mb_x < s->mb_width; s->mb_x++) {
3139 3139
             ff_update_block_index(s);
3140 3140
 
3141
-            vc1_decode_p_mb(v);
3142
-            if (s->mb_y != s->start_mb_y && apply_loop_filter)
3141
+            if (v->fcm == 2)
3142
+                vc1_decode_p_mb_intfi(v);
3143
+            else if (v->fcm == 1)
3144
+                vc1_decode_p_mb_intfr(v);
3145
+            else vc1_decode_p_mb(v);
3146
+            if (s->mb_y != s->start_mb_y && apply_loop_filter && v->fcm == 0)
3143 3147
                 vc1_apply_p_loop_filter(v);
3144 3148
             if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
3149
+                // TODO: may need modification to handle slice coding
3145 3150
                 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
3146 3151
                 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);
3147 3152
                 return;
... ...
@@ -3164,7 +4679,7 @@ static void vc1_decode_p_blocks(VC1Context *v)
3164 3164
     }
3165 3165
     if (s->end_mb_y >= s->start_mb_y)
3166 3166
         ff_draw_horiz_band(s, (s->end_mb_y-1) * 16, 16);
3167
-    ff_er_add_slice(s, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, (AC_END|DC_END|MV_END));
3167
+    ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1, (s->end_mb_y << v->field_mode) - 1, (AC_END|DC_END|MV_END));
3168 3168
 }
3169 3169
 
3170 3170
 static void vc1_decode_b_blocks(VC1Context *v)
... ...
@@ -3203,8 +4718,12 @@ static void vc1_decode_b_blocks(VC1Context *v)
3203 3203
         for(; s->mb_x < s->mb_width; s->mb_x++) {
3204 3204
             ff_update_block_index(s);
3205 3205
 
3206
-            vc1_decode_b_mb(v);
3206
+            if (v->fcm == 2)
3207
+                vc1_decode_b_mb_intfi(v);
3208
+            else
3209
+                vc1_decode_b_mb(v);
3207 3210
             if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
3211
+                // TODO: may need modification to handle slice coding
3208 3212
                 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
3209 3213
                 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);
3210 3214
                 return;
... ...
@@ -3219,7 +4738,7 @@ static void vc1_decode_b_blocks(VC1Context *v)
3219 3219
     }
3220 3220
     if (v->s.loop_filter)
3221 3221
         ff_draw_horiz_band(s, (s->end_mb_y-1)*16, 16);
3222
-    ff_er_add_slice(s, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, (AC_END|DC_END|MV_END));
3222
+    ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1, (s->end_mb_y << v->field_mode) - 1, (AC_END|DC_END|MV_END));
3223 3223
 }
3224 3224
 
3225 3225
 static void vc1_decode_skip_blocks(VC1Context *v)
... ...
@@ -3559,6 +5078,8 @@ static av_cold int vc1_decode_init_alloc_tables(VC1Context *v)
3559 3559
     /* Allocate mb bitplanes */
3560 3560
     v->mv_type_mb_plane = av_malloc(s->mb_stride * s->mb_height);
3561 3561
     v->direct_mb_plane = av_malloc(s->mb_stride * s->mb_height);
3562
+    v->forward_mb_plane = av_malloc(s->mb_stride * s->mb_height);
3563
+    v->fieldtx_plane = av_mallocz(s->mb_stride * s->mb_height);
3562 3564
     v->acpred_plane = av_malloc(s->mb_stride * s->mb_height);
3563 3565
     v->over_flags_plane = av_malloc(s->mb_stride * s->mb_height);
3564 3566
 
... ...
@@ -3568,7 +5089,7 @@ static av_cold int vc1_decode_init_alloc_tables(VC1Context *v)
3568 3568
     v->cbp = v->cbp_base + s->mb_stride;
3569 3569
     v->ttblk_base = av_malloc(sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
3570 3570
     v->ttblk = v->ttblk_base + s->mb_stride;
3571
-    v->is_intra_base = av_malloc(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
3571
+    v->is_intra_base = av_mallocz(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
3572 3572
     v->is_intra = v->is_intra_base + s->mb_stride;
3573 3573
     v->luma_mv_base = av_malloc(sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
3574 3574
     v->luma_mv = v->luma_mv_base + s->mb_stride;
... ...
@@ -3579,6 +5100,19 @@ static av_cold int vc1_decode_init_alloc_tables(VC1Context *v)
3579 3579
     v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
3580 3580
     v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
3581 3581
 
3582
+    /* allocate memory to store block level MV info */
3583
+    v->blk_mv_type_base = av_mallocz(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
3584
+    v->blk_mv_type = v->blk_mv_type_base + s->b8_stride + 1;
3585
+    v->mv_f_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
3586
+    v->mv_f[0] = v->mv_f_base + s->b8_stride + 1;
3587
+    v->mv_f[1] = v->mv_f[0] + (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
3588
+    v->mv_f_last_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
3589
+    v->mv_f_last[0] = v->mv_f_last_base + s->b8_stride + 1;
3590
+    v->mv_f_last[1] = v->mv_f_last[0] + (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
3591
+    v->mv_f_next_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
3592
+    v->mv_f_next[0] = v->mv_f_next_base + s->b8_stride + 1;
3593
+    v->mv_f_next[1] = v->mv_f_next[0] + (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
3594
+
3582 3595
     /* Init coded blocks info */
3583 3596
     if (v->profile == PROFILE_ADVANCED)
3584 3597
     {
... ...
@@ -3722,6 +5256,7 @@ static av_cold int vc1_decode_init(AVCodecContext *avctx)
3722 3722
             v->zz_8x8[1][i] = transpose(wmv1_scantable[1][i]);
3723 3723
             v->zz_8x8[2][i] = transpose(wmv1_scantable[2][i]);
3724 3724
             v->zz_8x8[3][i] = transpose(wmv1_scantable[3][i]);
3725
+            v->zzi_8x8[i] = transpose(ff_vc1_adv_interlaced_8x8_zz[i]);
3725 3726
         }
3726 3727
         v->left_blk_sh = 0;
3727 3728
         v->top_blk_sh  = 3;
... ...
@@ -3765,9 +5300,15 @@ static av_cold int vc1_decode_end(AVCodecContext *avctx)
3765 3765
     MPV_common_end(&v->s);
3766 3766
     av_freep(&v->mv_type_mb_plane);
3767 3767
     av_freep(&v->direct_mb_plane);
3768
+    av_freep(&v->forward_mb_plane);
3769
+    av_freep(&v->fieldtx_plane);
3768 3770
     av_freep(&v->acpred_plane);
3769 3771
     av_freep(&v->over_flags_plane);
3770 3772
     av_freep(&v->mb_type_base);
3773
+    av_freep(&v->blk_mv_type_base);
3774
+    av_freep(&v->mv_f_base);
3775
+    av_freep(&v->mv_f_last_base);
3776
+    av_freep(&v->mv_f_next_base);
3771 3777
     av_freep(&v->block);
3772 3778
     av_freep(&v->cbp_base);
3773 3779
     av_freep(&v->ttblk_base);
... ...
@@ -3791,7 +5332,9 @@ static int vc1_decode_frame(AVCodecContext *avctx,
3791 3791
     MpegEncContext *s = &v->s;
3792 3792
     AVFrame *pict = data;
3793 3793
     uint8_t *buf2 = NULL;
3794
+    uint8_t *buf_field2 = NULL;
3794 3795
     const uint8_t *buf_start = buf;
3796
+    int mb_height, n_slices1=-1;
3795 3797
     struct {
3796 3798
         uint8_t *buf;
3797 3799
         GetBitContext gb;
... ...
@@ -3839,6 +5382,26 @@ static int vc1_decode_frame(AVCodecContext *avctx,
3839 3839
                         buf_start = start;
3840 3840
                     buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
3841 3841
                     break;
3842
+                case VC1_CODE_FIELD: {
3843
+                    int buf_size3;
3844
+                    slices = av_realloc(slices, sizeof(*slices) * (n_slices+1));
3845
+                    if (!slices) goto err;
3846
+                    slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
3847
+                    if (!slices[n_slices].buf) goto err;
3848
+                    buf_size3 = vc1_unescape_buffer(start + 4, size,
3849
+                                                    slices[n_slices].buf);
3850
+                    init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
3851
+                                  buf_size3 << 3);
3852
+                    /* assuming that the field marker is at the exact middle,
3853
+                       hope it's correct */
3854
+                    slices[n_slices].mby_start = s->mb_height >> 1;
3855
+                    n_slices1 = n_slices - 1; // index of the last slice of the first field
3856
+                    n_slices++;
3857
+                    // not necessary, ad hoc until I find a way to handle WVC1i
3858
+                    buf_field2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
3859
+                    vc1_unescape_buffer(start + 4, size, buf_field2);
3860
+                    break;
3861
+                }
3842 3862
                 case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
3843 3863
                     buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
3844 3864
                     init_get_bits(&s->gb, buf2, buf_size2*8);
... ...
@@ -3867,13 +5430,11 @@ static int vc1_decode_frame(AVCodecContext *avctx,
3867 3867
             if((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD){
3868 3868
                 av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
3869 3869
                 goto err;
3870
+            } else { // found field marker, unescape second field
3871
+                buf_field2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
3872
+                vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, buf_field2);
3870 3873
             }
3871
-
3872 3874
             buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
3873
-            // TODO
3874
-            if(!v->warn_interlaced++)
3875
-                av_log(v->s.avctx, AV_LOG_ERROR, "Interlaced WVC1 support is not implemented\n");
3876
-            goto err;
3877 3875
         }else{
3878 3876
             buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
3879 3877
         }
... ...
@@ -3925,6 +5486,7 @@ static int vc1_decode_frame(AVCodecContext *avctx,
3925 3925
     }
3926 3926
 
3927 3927
     // do parse frame header
3928
+    v->pic_header_flag = 0;
3928 3929
     if(v->profile < PROFILE_ADVANCED) {
3929 3930
         if(vc1_parse_frame_header(v, &s->gb) == -1) {
3930 3931
             goto err;
... ...
@@ -3995,13 +5557,65 @@ static int vc1_decode_frame(AVCodecContext *avctx,
3995 3995
         ff_er_frame_start(s);
3996 3996
 
3997 3997
         v->bits = buf_size * 8;
3998
+        if (v->field_mode) {
3999
+            uint8_t *tmp[2];
4000
+            s->current_picture.f.linesize[0] <<= 1;
4001
+            s->current_picture.f.linesize[1] <<= 1;
4002
+            s->current_picture.f.linesize[2] <<= 1;
4003
+            s->linesize   <<= 1;
4004
+            s->uvlinesize <<= 1;
4005
+            tmp[0] = v->mv_f_last[0];
4006
+            tmp[1] = v->mv_f_last[1];
4007
+            v->mv_f_last[0] = v->mv_f_next[0];
4008
+            v->mv_f_last[1] = v->mv_f_next[1];
4009
+            v->mv_f_next[0] = v->mv_f[0];
4010
+            v->mv_f_next[1] = v->mv_f[1];
4011
+            v->mv_f[0] = tmp[0];
4012
+            v->mv_f[1] = tmp[1];
4013
+        }
4014
+        mb_height = s->mb_height >> v->field_mode;
3998 4015
         for (i = 0; i <= n_slices; i++) {
3999
-            if (i && get_bits1(&s->gb))
4000
-                vc1_parse_frame_header_adv(v, &s->gb);
4001
-            s->start_mb_y = (i == 0)        ? 0 : FFMAX(0, slices[i-1].mby_start);
4002
-            s->end_mb_y   = (i == n_slices) ? s->mb_height : FFMIN(s->mb_height, slices[i].mby_start);
4016
+            if (i > 0 &&  slices[i - 1].mby_start >= mb_height) {
4017
+                v->second_field = 1;
4018
+                v->blocks_off   = s->mb_width * s->mb_height << 1;
4019
+                v->mb_off       = s->mb_stride * s->mb_height >> 1;
4020
+            } else {
4021
+                v->second_field = 0;
4022
+                v->blocks_off   = 0;
4023
+                v->mb_off       = 0;
4024
+            }
4025
+            if (i) {
4026
+                v->pic_header_flag = 0;
4027
+                if (v->field_mode && i == n_slices1 + 2)
4028
+                    vc1_parse_frame_header_adv(v, &s->gb);
4029
+                else if (get_bits1(&s->gb)) {
4030
+                    v->pic_header_flag = 1;
4031
+                    vc1_parse_frame_header_adv(v, &s->gb);
4032
+                }
4033
+            }
4034
+            s->start_mb_y = (i == 0) ? 0 : FFMAX(0, slices[i-1].mby_start % mb_height);
4035
+            if (!v->field_mode || v->second_field)
4036
+                s->end_mb_y = (i == n_slices     ) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
4037
+            else
4038
+                s->end_mb_y = (i == n_slices1 + 1) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
4003 4039
             vc1_decode_blocks(v);
4004
-            if (i != n_slices) s->gb = slices[i].gb;
4040
+            if (i != n_slices)
4041
+                s->gb = slices[i].gb;
4042
+        }
4043
+        if (v->field_mode) {
4044
+            av_free(buf_field2);
4045
+            v->second_field = 0;
4046
+        }
4047
+        if(v->field_mode){
4048
+            if (s->pict_type == AV_PICTURE_TYPE_B) {
4049
+                memcpy(v->mv_f_base, v->mv_f_next_base,
4050
+                       2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
4051
+            }
4052
+            s->current_picture.f.linesize[0] >>= 1;
4053
+            s->current_picture.f.linesize[1] >>= 1;
4054
+            s->current_picture.f.linesize[2] >>= 1;
4055
+            s->linesize   >>= 1;
4056
+            s->uvlinesize >>= 1;
4005 4057
         }
4006 4058
 //av_log(s->avctx, AV_LOG_INFO, "Consumed %i/%i bits\n", get_bits_count(&s->gb), s->gb.size_in_bits);
4007 4059
 //  if(get_bits_count(&s->gb) > buf_size * 8)
... ...
@@ -4011,9 +5625,6 @@ static int vc1_decode_frame(AVCodecContext *avctx,
4011 4011
 
4012 4012
     MPV_frame_end(s);
4013 4013
 
4014
-assert(s->current_picture.f.pict_type == s->current_picture_ptr->f.pict_type);
4015
-assert(s->current_picture.f.pict_type == s->pict_type);
4016
-
4017 4014
     if (avctx->codec_id == CODEC_ID_WMV3IMAGE || avctx->codec_id == CODEC_ID_VC1IMAGE) {
4018 4015
 image:
4019 4016
         avctx->width  = avctx->coded_width  = v->output_width;
... ...
@@ -4051,6 +5662,7 @@ err:
4051 4051
     for (i = 0; i < n_slices; i++)
4052 4052
         av_free(slices[i].buf);
4053 4053
     av_free(slices);
4054
+    av_free(buf_field2);
4054 4055
     return -1;
4055 4056
 }
4056 4057
 
... ...
@@ -688,6 +688,26 @@ static void put_no_rnd_vc1_chroma_mc8_c(uint8_t *dst/*align 8*/, uint8_t *src/*a
688 688
     }
689 689
 }
690 690
 
691
+static void put_no_rnd_vc1_chroma_mc4_c(uint8_t *dst, uint8_t *src, int stride, int h, int x, int y){
692
+    const int A=(8-x)*(8-y);
693
+    const int B=(  x)*(8-y);
694
+    const int C=(8-x)*(  y);
695
+    const int D=(  x)*(  y);
696
+    int i;
697
+
698
+    assert(x<8 && y<8 && x>=0 && y>=0);
699
+
700
+    for(i=0; i<h; i++)
701
+    {
702
+        dst[0] = (A*src[0] + B*src[1] + C*src[stride+0] + D*src[stride+1] + 32 - 4) >> 6;
703
+        dst[1] = (A*src[1] + B*src[2] + C*src[stride+1] + D*src[stride+2] + 32 - 4) >> 6;
704
+        dst[2] = (A*src[2] + B*src[3] + C*src[stride+2] + D*src[stride+3] + 32 - 4) >> 6;
705
+        dst[3] = (A*src[3] + B*src[4] + C*src[stride+3] + D*src[stride+4] + 32 - 4) >> 6;
706
+        dst+= stride;
707
+        src+= stride;
708
+    }
709
+}
710
+
691 711
 #define avg2(a,b) ((a+b+1)>>1)
692 712
 static void avg_no_rnd_vc1_chroma_mc8_c(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int stride, int h, int x, int y){
693 713
     const int A=(8-x)*(8-y);
... ...
@@ -829,6 +849,7 @@ av_cold void ff_vc1dsp_init(VC1DSPContext* dsp) {
829 829
 
830 830
     dsp->put_no_rnd_vc1_chroma_pixels_tab[0]= put_no_rnd_vc1_chroma_mc8_c;
831 831
     dsp->avg_no_rnd_vc1_chroma_pixels_tab[0]= avg_no_rnd_vc1_chroma_mc8_c;
832
+    dsp->put_no_rnd_vc1_chroma_pixels_tab[1] = put_no_rnd_vc1_chroma_mc4_c;
832 833
 
833 834
 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
834 835
     dsp->sprite_h = sprite_h_c;
... ...
@@ -22,7 +22,7 @@
22 22
 
23 23
 #define LIBAVCODEC_VERSION_MAJOR 53
24 24
 #define LIBAVCODEC_VERSION_MINOR 20
25
-#define LIBAVCODEC_VERSION_MICRO  0
25
+#define LIBAVCODEC_VERSION_MICRO  1
26 26
 
27 27
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
28 28
                                                LIBAVCODEC_VERSION_MINOR, \
... ...
@@ -177,7 +177,7 @@ static int fourxm_read_header(AVFormatContext *s,
177 177
                                               sizeof(AudioTrack),
178 178
                                               current_track + 1);
179 179
                 if (!fourxm->tracks) {
180
-                    ret=  AVERROR(ENOMEM);
180
+                    ret = AVERROR(ENOMEM);
181 181
                     goto fail;
182 182
                 }
183 183
                 memset(&fourxm->tracks[fourxm->track_count], 0,
... ...
@@ -874,12 +874,13 @@ static int avi_sync(AVFormatContext *s, int exit_early)
874 874
 {
875 875
     AVIContext *avi = s->priv_data;
876 876
     AVIOContext *pb = s->pb;
877
-    int n, d[8];
877
+    int n;
878
+    unsigned int d[8];
878 879
     unsigned int size;
879 880
     int64_t i, sync;
880 881
 
881 882
 start_sync:
882
-    memset(d, -1, sizeof(int)*8);
883
+    memset(d, -1, sizeof(d));
883 884
     for(i=sync=avio_tell(pb); !url_feof(pb); i++) {
884 885
         int j;
885 886
 
... ...
@@ -891,7 +892,7 @@ start_sync:
891 891
 
892 892
         n= get_stream_idx(d+2);
893 893
 //av_log(s, AV_LOG_DEBUG, "%X %X %X %X %X %X %X %X %"PRId64" %d %d\n", d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], i, size, n);
894
-        if(i + (uint64_t)size > avi->fsize || d[0]<0)
894
+        if(i + (uint64_t)size > avi->fsize || d[0] > 127)
895 895
             continue;
896 896
 
897 897
         //parse ix##
... ...
@@ -256,12 +256,13 @@ static int mxf_get_d10_aes3_packet(AVIOContext *pb, AVStream *st, AVPacket *pkt,
256 256
 
257 257
     if (length > 61444) /* worst case PAL 1920 samples 8 channels */
258 258
         return -1;
259
-    av_new_packet(pkt, length);
260
-    avio_read(pb, pkt->data, length);
259
+    length = av_get_packet(pb, pkt, length);
260
+    if (length < 0)
261
+        return length;
261 262
     data_ptr = pkt->data;
262 263
     end_ptr = pkt->data + length;
263 264
     buf_ptr = pkt->data + 4; /* skip SMPTE 331M header */
264
-    for (; buf_ptr < end_ptr; ) {
265
+    for (; buf_ptr + st->codec->channels*4 < end_ptr; ) {
265 266
         for (i = 0; i < st->codec->channels; i++) {
266 267
             uint32_t sample = bytestream_get_le32(&buf_ptr);
267 268
             if (st->codec->bits_per_coded_sample == 24)
... ...
@@ -271,7 +272,7 @@ static int mxf_get_d10_aes3_packet(AVIOContext *pb, AVStream *st, AVPacket *pkt,
271 271
         }
272 272
         buf_ptr += 32 - st->codec->channels*4; // always 8 channels stored SMPTE 331M
273 273
     }
274
-    pkt->size = data_ptr - pkt->data;
274
+    av_shrink_packet(pkt, data_ptr - pkt->data);
275 275
     return 0;
276 276
 }
277 277
 
... ...
@@ -323,12 +324,16 @@ static int mxf_decrypt_triplet(AVFormatContext *s, AVPacket *pkt, KLVPacket *klv
323 323
     if (memcmp(tmpbuf, checkv, 16))
324 324
         av_log(s, AV_LOG_ERROR, "probably incorrect decryption key\n");
325 325
     size -= 32;
326
-    av_get_packet(pb, pkt, size);
326
+    size = av_get_packet(pb, pkt, size);
327
+    if (size < 0)
328
+        return size;
329
+    else if (size < plaintext_size)
330
+        return AVERROR_INVALIDDATA;
327 331
     size -= plaintext_size;
328 332
     if (mxf->aesc)
329 333
         av_aes_crypt(mxf->aesc, &pkt->data[plaintext_size],
330 334
                      &pkt->data[plaintext_size], size >> 4, ivec, 1);
331
-    pkt->size = orig_size;
335
+    av_shrink_packet(pkt, orig_size);
332 336
     pkt->stream_index = index;
333 337
     avio_skip(pb, end - avio_tell(pb));
334 338
     return 0;
... ...
@@ -365,8 +370,11 @@ static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt)
365 365
                     av_log(s, AV_LOG_ERROR, "error reading D-10 aes3 frame\n");
366 366
                     return -1;
367 367
                 }
368
-            } else
369
-                av_get_packet(s->pb, pkt, klv.length);
368
+            } else {
369
+                int ret = av_get_packet(s->pb, pkt, klv.length);
370
+                if (ret < 0)
371
+                    return ret;
372
+            }
370 373
             pkt->stream_index = index;
371 374
             pkt->pos = klv.offset;
372 375
             return 0;
... ...
@@ -53,22 +53,13 @@ const AVOption *av_next_option(void *obj, const AVOption *last)
53 53
     else                      return (*(AVClass**)obj)->option;
54 54
 }
55 55
 
56
-static int av_set_number2(void *obj, const char *name, double num, int den, int64_t intnum, const AVOption **o_out)
56
+static int write_number(void *obj, const AVOption *o, void *dst, double num, int den, int64_t intnum)
57 57
 {
58
-    const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
59
-    void *dst;
60
-    if (o_out)
61
-        *o_out= o;
62
-    if (!o)
63
-        return AVERROR_OPTION_NOT_FOUND;
64
-
65 58
     if (o->max*den < num*intnum || o->min*den > num*intnum) {
66
-        av_log(obj, AV_LOG_ERROR, "Value %lf for parameter '%s' out of range\n", num, name);
59
+        av_log(obj, AV_LOG_ERROR, "Value %lf for parameter '%s' out of range\n", num, o->name);
67 60
         return AVERROR(ERANGE);
68 61
     }
69 62
 
70
-    dst= ((uint8_t*)obj) + o->offset;
71
-
72 63
     switch (o->type) {
73 64
     case FF_OPT_TYPE_FLAGS:
74 65
     case FF_OPT_TYPE_INT:   *(int       *)dst= llrint(num/den)*intnum; break;
... ...
@@ -85,15 +76,6 @@ static int av_set_number2(void *obj, const char *name, double num, int den, int6
85 85
     return 0;
86 86
 }
87 87
 
88
-static const AVOption *av_set_number(void *obj, const char *name, double num, int den, int64_t intnum)
89
-{
90
-    const AVOption *o = NULL;
91
-    if (av_set_number2(obj, name, num, den, intnum, &o) < 0)
92
-        return NULL;
93
-    else
94
-        return o;
95
-}
96
-
97 88
 static const double const_values[] = {
98 89
     M_PI,
99 90
     M_E,
... ...
@@ -115,10 +97,98 @@ static int hexchar2int(char c) {
115 115
     return -1;
116 116
 }
117 117
 
118
+static int set_string_binary(void *obj, const AVOption *o, const char *val, uint8_t **dst)
119
+{
120
+    int *lendst = (int *)(dst + 1);
121
+    uint8_t *bin, *ptr;
122
+    int len = strlen(val);
123
+
124
+    av_freep(dst);
125
+    *lendst = 0;
126
+
127
+    if (len & 1)
128
+        return AVERROR(EINVAL);
129
+    len /= 2;
130
+
131
+    ptr = bin = av_malloc(len);
132
+    while (*val) {
133
+        int a = hexchar2int(*val++);
134
+        int b = hexchar2int(*val++);
135
+        if (a < 0 || b < 0) {
136
+            av_free(bin);
137
+            return AVERROR(EINVAL);
138
+        }
139
+        *ptr++ = (a << 4) | b;
140
+    }
141
+    *dst = bin;
142
+    *lendst = len;
143
+
144
+    return 0;
145
+}
146
+
147
+static int set_string(void *obj, const AVOption *o, const char *val, uint8_t **dst)
148
+{
149
+    av_freep(dst);
150
+    *dst = av_strdup(val);
151
+    return 0;
152
+}
153
+
154
+static int set_string_number(void *obj, const AVOption *o, const char *val, void *dst)
155
+{
156
+    int ret = 0, notfirst = 0;
157
+    for (;;) {
158
+        int i;
159
+        char buf[256];
160
+        int cmd = 0;
161
+        double d;
162
+
163
+        if (*val == '+' || *val == '-')
164
+            cmd = *(val++);
165
+
166
+        for (i = 0; i < sizeof(buf) - 1 && val[i] && val[i] != '+' && val[i] != '-'; i++)
167
+            buf[i] = val[i];
168
+        buf[i] = 0;
169
+
170
+        {
171
+            const AVOption *o_named = av_opt_find(obj, buf, o->unit, 0, 0);
172
+            if (o_named && o_named->type == FF_OPT_TYPE_CONST)
173
+                d = o_named->default_val.dbl;
174
+            else if (!strcmp(buf, "default")) d = o->default_val.dbl;
175
+            else if (!strcmp(buf, "max"    )) d = o->max;
176
+            else if (!strcmp(buf, "min"    )) d = o->min;
177
+            else if (!strcmp(buf, "none"   )) d = 0;
178
+            else if (!strcmp(buf, "all"    )) d = ~0;
179
+            else {
180
+                int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
181
+                if (res < 0) {
182
+                    av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
183
+                    return res;
184
+                }
185
+            }
186
+        }
187
+        if (o->type == FF_OPT_TYPE_FLAGS) {
188
+            if      (cmd == '+') d = av_get_int(obj, o->name, NULL) | (int64_t)d;
189
+            else if (cmd == '-') d = av_get_int(obj, o->name, NULL) &~(int64_t)d;
190
+        } else {
191
+            if      (cmd == '+') d = notfirst*av_get_double(obj, o->name, NULL) + d;
192
+            else if (cmd == '-') d = notfirst*av_get_double(obj, o->name, NULL) - d;
193
+        }
194
+
195
+        if ((ret = write_number(obj, o, dst, d, 1, 1)) < 0)
196
+            return ret;
197
+        val += i;
198
+        if (!*val)
199
+            return 0;
200
+        notfirst = 1;
201
+    }
202
+
203
+    return 0;
204
+}
205
+
118 206
 int av_set_string3(void *obj, const char *name, const char *val, int alloc, const AVOption **o_out)
119 207
 {
120
-    int ret;
121 208
     const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
209
+    void *dst;
122 210
     if (o_out)
123 211
         *o_out = o;
124 212
     if (!o)
... ...
@@ -126,100 +196,50 @@ int av_set_string3(void *obj, const char *name, const char *val, int alloc, cons
126 126
     if (!val && o->type != FF_OPT_TYPE_STRING)
127 127
         return AVERROR(EINVAL);
128 128
 
129
-    if (o->type == FF_OPT_TYPE_BINARY) {
130
-        uint8_t **dst = (uint8_t **)(((uint8_t*)obj) + o->offset);
131
-        int *lendst = (int *)(dst + 1);
132
-        uint8_t *bin, *ptr;
133
-        int len = strlen(val);
134
-        av_freep(dst);
135
-        *lendst = 0;
136
-        if (len & 1) return AVERROR(EINVAL);
137
-        len /= 2;
138
-        ptr = bin = av_malloc(len);
139
-        while (*val) {
140
-            int a = hexchar2int(*val++);
141
-            int b = hexchar2int(*val++);
142
-            if (a < 0 || b < 0) {
143
-                av_free(bin);
144
-                return AVERROR(EINVAL);
145
-            }
146
-            *ptr++ = (a << 4) | b;
147
-        }
148
-        *dst = bin;
149
-        *lendst = len;
150
-        return 0;
129
+    dst = ((uint8_t*)obj) + o->offset;
130
+    switch (o->type) {
131
+    case FF_OPT_TYPE_STRING:   return set_string(obj, o, val, dst);
132
+    case FF_OPT_TYPE_BINARY:   return set_string_binary(obj, o, val, dst);
133
+    case FF_OPT_TYPE_FLAGS:
134
+    case FF_OPT_TYPE_INT:
135
+    case FF_OPT_TYPE_INT64:
136
+    case FF_OPT_TYPE_FLOAT:
137
+    case FF_OPT_TYPE_DOUBLE:
138
+    case FF_OPT_TYPE_RATIONAL: return set_string_number(obj, o, val, dst);
151 139
     }
152
-    if (o->type != FF_OPT_TYPE_STRING) {
153
-        int notfirst=0;
154
-        for (;;) {
155
-            int i;
156
-            char buf[256];
157
-            int cmd=0;
158
-            double d;
159
-
160
-            if (*val == '+' || *val == '-')
161
-                cmd= *(val++);
162
-
163
-            for (i=0; i<sizeof(buf)-1 && val[i] && val[i]!='+' && val[i]!='-'; i++)
164
-                buf[i]= val[i];
165
-            buf[i]=0;
166
-
167
-            {
168
-                const AVOption *o_named = av_opt_find(obj, buf, o->unit, 0, 0);
169
-                if (o_named && o_named->type == FF_OPT_TYPE_CONST)
170
-                    d= o_named->default_val.dbl;
171
-                else if (!strcmp(buf, "default")) d= o->default_val.dbl;
172
-                else if (!strcmp(buf, "max"    )) d= o->max;
173
-                else if (!strcmp(buf, "min"    )) d= o->min;
174
-                else if (!strcmp(buf, "none"   )) d= 0;
175
-                else if (!strcmp(buf, "all"    )) d= ~0;
176
-                else {
177
-                    int res = av_expr_parse_and_eval(&d, buf, const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, obj);
178
-                    if (res < 0) {
179
-                        av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\"\n", val);
180
-                        return res;
181
-                    }
182
-                }
183
-            }
184
-            if (o->type == FF_OPT_TYPE_FLAGS) {
185
-                if      (cmd=='+') d= av_get_int(obj, name, NULL) | (int64_t)d;
186
-                else if (cmd=='-') d= av_get_int(obj, name, NULL) &~(int64_t)d;
187
-            } else {
188
-                if      (cmd=='+') d= notfirst*av_get_double(obj, name, NULL) + d;
189
-                else if (cmd=='-') d= notfirst*av_get_double(obj, name, NULL) - d;
190
-            }
191 140
 
192
-            if ((ret = av_set_number2(obj, name, d, 1, 1, o_out)) < 0)
193
-                return ret;
194
-            val+= i;
195
-            if (!*val)
196
-                return 0;
197
-            notfirst=1;
198
-        }
199
-    }
141
+    av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
142
+    return AVERROR(EINVAL);
143
+}
200 144
 
201
-    if (alloc) {
202
-        av_free(*(void**)(((uint8_t*)obj) + o->offset));
203
-        val= av_strdup(val);
204
-    }
145
+static const AVOption *set_number(void *obj, const char *name, double num, int den, int64_t intnum)
146
+{
147
+    const AVOption *o = av_opt_find(obj, name, NULL, 0, 0);
148
+    void *dst;
205 149
 
206
-    memcpy(((uint8_t*)obj) + o->offset, &val, sizeof(val));
207
-    return 0;
150
+    if (!o)
151
+        return NULL;
152
+
153
+    dst = ((uint8_t*)obj) + o->offset;
154
+    if (write_number(obj, o, dst, num, den, intnum) < 0)
155
+        return NULL;
156
+    else
157
+        return o;
208 158
 }
209 159
 
210 160
 const AVOption *av_set_double(void *obj, const char *name, double n)
211 161
 {
212
-    return av_set_number(obj, name, n, 1, 1);
162
+    return set_number(obj, name, n, 1, 1);
213 163
 }
214 164
 
215 165
 const AVOption *av_set_q(void *obj, const char *name, AVRational n)
216 166
 {
217
-    return av_set_number(obj, name, n.num, n.den, 1);
167
+    return set_number(obj, name, n.num, n.den, 1);
218 168
 }
219 169
 
220 170
 const AVOption *av_set_int(void *obj, const char *name, int64_t n)
221 171
 {
222
-    return av_set_number(obj, name, 1, 1, n);
172
+    return set_number(obj, name, 1, 1, n);
223 173
 }
224 174
 
225 175
 /**
... ...
@@ -129,9 +129,7 @@ const AVOption *av_find_opt(void *obj, const char *name, const char *unit, int m
129 129
  * similarly, '-' unsets a flag.
130 130
  * @param[out] o_out if non-NULL put here a pointer to the AVOption
131 131
  * found
132
- * @param alloc when 1 then the old value will be av_freed() and the
133
- *                     new av_strduped()
134
- *              when 0 then no av_free() nor av_strdup() will be used
132
+ * @param alloc this parameter is currently ignored
135 133
  * @return 0 if the value has been set, or an AVERROR code in case of
136 134
  * error:
137 135
  * AVERROR_OPTION_NOT_FOUND if no matching option exists