[02/15] vc1dec: Redesign the intensity compensation

Message ID 1369729571-75967-2-git-send-email-martin@martin.st
State Superseded
Headers show

Commit Message

Martin Storsjö May 28, 2013, 8:25 a.m.
From: Michael Niedermayer <michaelni@gmx.at>

The existing implementation had little to do with VC1.
This could be implemented by adjusting the reference frames
ithemselfs but that would make frame multi-threading difficult.
---
 libavcodec/vc1.c    |   91 ++++++++++++++++++++++++++++++++++++++++-----------
 libavcodec/vc1.h    |    8 +++--
 libavcodec/vc1dec.c |   49 ++++++++++++++++++---------
 3 files changed, 111 insertions(+), 37 deletions(-)

Comments

Kostya Shishkov May 28, 2013, 9:02 a.m. | #1
On Tue, May 28, 2013 at 11:25:58AM +0300, Martin Storsjö wrote:
> From: Michael Niedermayer <michaelni@gmx.at>
> 
> The existing implementation had little to do with VC1.

http://26-26-54.hardwarebug.org/33

> This could be implemented by adjusting the reference frames
> ithemselfs but that would make frame multi-threading difficult.
> ---
>  libavcodec/vc1.c    |   91 ++++++++++++++++++++++++++++++++++++++++-----------
>  libavcodec/vc1.h    |    8 +++--
>  libavcodec/vc1dec.c |   49 ++++++++++++++++++---------
>  3 files changed, 111 insertions(+), 37 deletions(-)

In general looks WTFy. Maybe (I cannot say even with 80% certainity) it tries
to deal with the fact there's intensity compensation defined in 8.3.8 and
10.3.8 and it mandates that intensity-compensated reference frame is used for
the subsequent fields/B-frames too but that's not obvious from the code or commit
message.
Martin Storsjö May 28, 2013, 9:21 a.m. | #2
On Tue, 28 May 2013, Kostya Shishkov wrote:

> On Tue, May 28, 2013 at 11:25:58AM +0300, Martin Storsjö wrote:
>> From: Michael Niedermayer <michaelni@gmx.at>
>>
>> The existing implementation had little to do with VC1.
>
> http://26-26-54.hardwarebug.org/33
>
>> This could be implemented by adjusting the reference frames
>> ithemselfs but that would make frame multi-threading difficult.
>> ---
>>  libavcodec/vc1.c    |   91 ++++++++++++++++++++++++++++++++++++++++-----------
>>  libavcodec/vc1.h    |    8 +++--
>>  libavcodec/vc1dec.c |   49 ++++++++++++++++++---------
>>  3 files changed, 111 insertions(+), 37 deletions(-)
>
> In general looks WTFy. Maybe (I cannot say even with 80% certainity) it tries
> to deal with the fact there's intensity compensation defined in 8.3.8 and
> 10.3.8 and it mandates that intensity-compensated reference frame is used for
> the subsequent fields/B-frames too but that's not obvious from the code or commit
> message.

Ok, so what would be the right course of action? Patches 2-3 that you ok'd 
depend pretty much on this change. Should I try to split this patch into 
smaller pieces to get the parts that patches 2-3 depend on separated, or 
would this one just be less WTFy with a seriously amended commit message?

// Martin
Kostya Shishkov May 28, 2013, 9:28 a.m. | #3
On Tue, May 28, 2013 at 12:21:44PM +0300, Martin Storsjö wrote:
> On Tue, 28 May 2013, Kostya Shishkov wrote:
> 
> >On Tue, May 28, 2013 at 11:25:58AM +0300, Martin Storsjö wrote:
> >>From: Michael Niedermayer <michaelni@gmx.at>
> >>
> >>The existing implementation had little to do with VC1.
> >
> >http://26-26-54.hardwarebug.org/33
> >
> >>This could be implemented by adjusting the reference frames
> >>ithemselfs but that would make frame multi-threading difficult.
> >>---
> >> libavcodec/vc1.c    |   91 ++++++++++++++++++++++++++++++++++++++++-----------
> >> libavcodec/vc1.h    |    8 +++--
> >> libavcodec/vc1dec.c |   49 ++++++++++++++++++---------
> >> 3 files changed, 111 insertions(+), 37 deletions(-)
> >
> >In general looks WTFy. Maybe (I cannot say even with 80% certainity) it tries
> >to deal with the fact there's intensity compensation defined in 8.3.8 and
> >10.3.8 and it mandates that intensity-compensated reference frame is used for
> >the subsequent fields/B-frames too but that's not obvious from the code or commit
> >message.
> 
> Ok, so what would be the right course of action? Patches 2-3 that
> you ok'd depend pretty much on this change. Should I try to split
> this patch into smaller pieces to get the parts that patches 2-3
> depend on separated, or would this one just be less WTFy with a
> seriously amended commit message?

The latter would do for me.
Martin Storsjö May 28, 2013, 9:32 a.m. | #4
On Tue, 28 May 2013, Kostya Shishkov wrote:

> On Tue, May 28, 2013 at 12:21:44PM +0300, Martin Storsjö wrote:
>> On Tue, 28 May 2013, Kostya Shishkov wrote:
>>
>>> On Tue, May 28, 2013 at 11:25:58AM +0300, Martin Storsjö wrote:
>>>> From: Michael Niedermayer <michaelni@gmx.at>
>>>>
>>>> The existing implementation had little to do with VC1.
>>>
>>> http://26-26-54.hardwarebug.org/33
>>>
>>>> This could be implemented by adjusting the reference frames
>>>> ithemselfs but that would make frame multi-threading difficult.
>>>> ---
>>>> libavcodec/vc1.c    |   91 ++++++++++++++++++++++++++++++++++++++++-----------
>>>> libavcodec/vc1.h    |    8 +++--
>>>> libavcodec/vc1dec.c |   49 ++++++++++++++++++---------
>>>> 3 files changed, 111 insertions(+), 37 deletions(-)
>>>
>>> In general looks WTFy. Maybe (I cannot say even with 80% certainity) it tries
>>> to deal with the fact there's intensity compensation defined in 8.3.8 and
>>> 10.3.8 and it mandates that intensity-compensated reference frame is used for
>>> the subsequent fields/B-frames too but that's not obvious from the code or commit
>>> message.
>>
>> Ok, so what would be the right course of action? Patches 2-3 that
>> you ok'd depend pretty much on this change. Should I try to split
>> this patch into smaller pieces to get the parts that patches 2-3
>> depend on separated, or would this one just be less WTFy with a
>> seriously amended commit message?
>
> The latter would do for me.

Right, so e.g.:

---8<---
vc1dec: Redesign the intensity compensation

Use the intensity-compensated reference frame for subsequent
fields/B-frames.

This could be implemented by adjusting the reference frames
themselves but that would make frame multi-threading difficult.
---8<---

// Martin
Kostya Shishkov May 28, 2013, 9:39 a.m. | #5
On Tue, May 28, 2013 at 12:32:13PM +0300, Martin Storsjö wrote:
> On Tue, 28 May 2013, Kostya Shishkov wrote:
> 
> >On Tue, May 28, 2013 at 12:21:44PM +0300, Martin Storsjö wrote:
> >>On Tue, 28 May 2013, Kostya Shishkov wrote:
> >>
> >>>On Tue, May 28, 2013 at 11:25:58AM +0300, Martin Storsjö wrote:
> >>>>From: Michael Niedermayer <michaelni@gmx.at>
> >>>>
> >>>>The existing implementation had little to do with VC1.
> >>>
> >>>http://26-26-54.hardwarebug.org/33
> >>>
> >>>>This could be implemented by adjusting the reference frames
> >>>>ithemselfs but that would make frame multi-threading difficult.
> >>>>---
> >>>>libavcodec/vc1.c    |   91 ++++++++++++++++++++++++++++++++++++++++-----------
> >>>>libavcodec/vc1.h    |    8 +++--
> >>>>libavcodec/vc1dec.c |   49 ++++++++++++++++++---------
> >>>>3 files changed, 111 insertions(+), 37 deletions(-)
> >>>
> >>>In general looks WTFy. Maybe (I cannot say even with 80% certainity) it tries
> >>>to deal with the fact there's intensity compensation defined in 8.3.8 and
> >>>10.3.8 and it mandates that intensity-compensated reference frame is used for
> >>>the subsequent fields/B-frames too but that's not obvious from the code or commit
> >>>message.
> >>
> >>Ok, so what would be the right course of action? Patches 2-3 that
> >>you ok'd depend pretty much on this change. Should I try to split
> >>this patch into smaller pieces to get the parts that patches 2-3
> >>depend on separated, or would this one just be less WTFy with a
> >>seriously amended commit message?
> >
> >The latter would do for me.
> 
> Right, so e.g.:
> 
> ---8<---
> vc1dec: Redesign the intensity compensation
> 
> Use the intensity-compensated reference frame for subsequent
> fields/B-frames.

Since we currently don't change the reference frame we have to maintain lookup
tables for intensity compensation in the following dependent frames.
Diego Biurrun May 28, 2013, 11:40 a.m. | #6
On 2013-05-28 11:02, Kostya Shishkov wrote:
> On Tue, May 28, 2013 at 11:25:58AM +0300, Martin Storsjö wrote:
>> From: Michael Niedermayer <michaelni@gmx.at>
>>
>> The existing implementation had little to do with VC1.
>
> http://26-26-54.hardwarebug.org/33

Haha, I'm famous now ;) - should I have managed to make the second 
memorable joke after the convergence of the number of BSD developers and 
the number of BSD forks? :)

Diego
Kostya Shishkov May 28, 2013, 11:51 a.m. | #7
On Tue, May 28, 2013 at 01:40:11PM +0200, Diego Biurrun wrote:
> On 2013-05-28 11:02, Kostya Shishkov wrote:
> >On Tue, May 28, 2013 at 11:25:58AM +0300, Martin Storsjö wrote:
> >>From: Michael Niedermayer <michaelni@gmx.at>
> >>
> >>The existing implementation had little to do with VC1.
> >
> >http://26-26-54.hardwarebug.org/33
> 
> Haha, I'm famous now ;) - should I have managed to make the second
> memorable joke after the convergence of the number of BSD developers
> and the number of BSD forks? :)

What's so funny about Diego's law?
Diego Biurrun May 28, 2013, 11:52 a.m. | #8
On 2013-05-28 13:51, Kostya Shishkov wrote:
> On Tue, May 28, 2013 at 01:40:11PM +0200, Diego Biurrun wrote:
>> On 2013-05-28 11:02, Kostya Shishkov wrote:
>>> On Tue, May 28, 2013 at 11:25:58AM +0300, Martin Storsjö wrote:
>>>> From: Michael Niedermayer <michaelni@gmx.at>
>>>>
>>>> The existing implementation had little to do with VC1.
>>>
>>> http://26-26-54.hardwarebug.org/33
>>
>> Haha, I'm famous now ;) - should I have managed to make the second
>> memorable joke after the convergence of the number of BSD developers
>> and the number of BSD forks? :)
>
> What's so funny about Diego's law?

The fact that I created another memorable quote amuses me.

Diego
Diego Biurrun May 28, 2013, 11:53 a.m. | #9
On 2013-05-28 13:52, Diego Biurrun wrote:
> On 2013-05-28 13:51, Kostya Shishkov wrote:
>> On Tue, May 28, 2013 at 01:40:11PM +0200, Diego Biurrun wrote:
>>> On 2013-05-28 11:02, Kostya Shishkov wrote:
>>>> On Tue, May 28, 2013 at 11:25:58AM +0300, Martin Storsjö wrote:
>>>>> From: Michael Niedermayer <michaelni@gmx.at>
>>>>>
>>>>> The existing implementation had little to do with VC1.
>>>>
>>>> http://26-26-54.hardwarebug.org/33
>>>
>>> Haha, I'm famous now ;) - should I have managed to make the second
>>> memorable joke after the convergence of the number of BSD developers
>>> and the number of BSD forks? :)
>>
>> What's so funny about Diego's law?
>
> The fact that I created another memorable quote amuses me.

And "Diego's law" made the BSD dev I told it to laugh ...

Diego

Patch

diff --git a/libavcodec/vc1.c b/libavcodec/vc1.c
index 23012cd..5566e2e 100644
--- a/libavcodec/vc1.c
+++ b/libavcodec/vc1.c
@@ -571,7 +571,7 @@  int ff_vc1_decode_entry_point(AVCodecContext *avctx, VC1Context *v, GetBitContex
 }
 
 /* fill lookup tables for intensity compensation */
-#define INIT_LUT(lumscale, lumshift, luty, lutuv) do {                        \
+#define INIT_LUT(lumscale, lumshift, luty, lutuv, chain) do {                 \
         int scale, shift, i;                                                  \
         if (!lumscale) {                                                      \
             scale = -64;                                                      \
@@ -586,11 +586,32 @@  int ff_vc1_decode_entry_point(AVCodecContext *avctx, VC1Context *v, GetBitContex
                 shift = lumshift << 6;                                        \
         }                                                                     \
         for (i = 0; i < 256; i++) {                                           \
-            luty[i]  = av_clip_uint8((scale * i + shift + 32) >> 6);          \
-            lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6); \
+            int iy = chain ? luty[i]  : i;                                    \
+            int iu = chain ? lutuv[i] : i;                                    \
+            luty[i]  = av_clip_uint8((scale * iy + shift + 32) >> 6);         \
+            lutuv[i] = av_clip_uint8((scale * (iu - 128) + 128*64 + 32) >> 6);\
         }                                                                     \
     } while(0)
 
+static void rotate_luts(VC1Context *v)
+{
+#define ROTATE(DEF, L, N, C, A) do {                          \
+        if (v->s.pict_type == AV_PICTURE_TYPE_BI || v->s.pict_type == AV_PICTURE_TYPE_B) { \
+            C = A;                                            \
+        } else {                                              \
+            DEF;                                              \
+            memcpy(&tmp, &L  , sizeof(tmp));                  \
+            memcpy(&L  , &N  , sizeof(tmp));                  \
+            memcpy(&N  , &tmp, sizeof(tmp));                  \
+            C = N;                                            \
+        }                                                     \
+    } while(0)
+
+    ROTATE(int tmp            , v->last_use_ic, v->next_use_ic, v->curr_use_ic, v->aux_use_ic);
+    ROTATE(uint8_t tmp[2][256], v->last_luty,   v->next_luty,   v->curr_luty,   v->aux_luty);
+    ROTATE(uint8_t tmp[2][256], v->last_lutuv,  v->next_lutuv,  v->curr_lutuv,  v->aux_lutuv);
+}
+
 int ff_vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
 {
     int pqindex, lowquant, status;
@@ -677,8 +698,12 @@  int ff_vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
             (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);
 
-    if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_P)
-        v->use_ic = 0;
+    if (v->first_pic_header_flag) {
+        rotate_luts(v);
+        INIT_LUT(32, 0, v->curr_luty[0], v->curr_lutuv[0], 0);
+        INIT_LUT(32, 0, v->curr_luty[1], v->curr_lutuv[1], 0);
+        v->curr_use_ic = 0;
+    }
 
     switch (v->s.pict_type) {
     case AV_PICTURE_TYPE_P:
@@ -692,9 +717,10 @@  int ff_vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
             v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
             v->lumscale = get_bits(gb, 6);
             v->lumshift = get_bits(gb, 6);
-            v->use_ic   = 1;
+            v->last_use_ic = 1;
             /* fill lookup tables for intensity compensation */
-            INIT_LUT(v->lumscale, v->lumshift, v->luty, v->lutuv);
+            INIT_LUT(v->lumscale, v->lumshift, v->last_luty[0], v->last_lutuv[0], 1);
+            INIT_LUT(v->lumscale, v->lumshift, v->last_luty[1], v->last_lutuv[1], 1);
         }
         v->qs_last = v->s.quarter_sample;
         if (v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
@@ -942,12 +968,16 @@  int ff_vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb)
     if (v->postprocflag)
         v->postproc = get_bits(gb, 2);
 
-    if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_P)
-        v->use_ic = 0;
-
     if (v->parse_only)
         return 0;
 
+    if (v->first_pic_header_flag) {
+        rotate_luts(v);
+        INIT_LUT(32, 0, v->curr_luty[0], v->curr_lutuv[0], 0);
+        INIT_LUT(32, 0, v->curr_luty[1], v->curr_lutuv[1], 0);
+        v->curr_use_ic = 0;
+    }
+
     switch (v->s.pict_type) {
     case AV_PICTURE_TYPE_I:
     case AV_PICTURE_TYPE_BI:
@@ -998,7 +1028,9 @@  int ff_vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb)
                 if (v->intcomp) {
                     v->lumscale = get_bits(gb, 6);
                     v->lumshift = get_bits(gb, 6);
-                    INIT_LUT(v->lumscale, v->lumshift, v->luty, v->lutuv);
+                    INIT_LUT(v->lumscale, v->lumshift, v->last_luty[0], v->last_lutuv[0], 1);
+                    INIT_LUT(v->lumscale, v->lumshift, v->last_luty[1], v->last_lutuv[1], 1);
+                    v->last_use_ic = 1;
                 }
                 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
                 av_log(v->s.avctx, AV_LOG_DEBUG, "SKIPMB plane encoding: "
@@ -1041,17 +1073,38 @@  int ff_vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb)
                 int mvmode2;
                 mvmode2 = get_unary(gb, 1, 3);
                 v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][mvmode2];
-                if (v->field_mode)
-                    v->intcompfield = decode210(gb);
-                v->lumscale = get_bits(gb, 6);
-                v->lumshift = get_bits(gb, 6);
-                INIT_LUT(v->lumscale, v->lumshift, v->luty, v->lutuv);
-                if ((v->field_mode) && !v->intcompfield) {
+                if (v->field_mode) {
+                    v->intcompfield = decode210(gb)^3;
+                } else
+                    v->intcompfield = 3;
+
+                v->lumscale2 = v->lumscale = 32;
+                v->lumshift2 = v->lumshift =  0;
+                if (v->intcompfield & 1) {
+                    v->lumscale = get_bits(gb, 6);
+                    v->lumshift = get_bits(gb, 6);
+                }
+                if ((v->intcompfield & 2) && v->field_mode) {
                     v->lumscale2 = get_bits(gb, 6);
                     v->lumshift2 = get_bits(gb, 6);
-                    INIT_LUT(v->lumscale2, v->lumshift2, v->luty2, v->lutuv2);
+                } else if(!v->field_mode) {
+                    v->lumscale2 = v->lumscale;
+                    v->lumshift2 = v->lumshift;
+                }
+                if (v->field_mode && v->second_field) {
+                    if (v->cur_field_type) {
+                        INIT_LUT(v->lumscale , v->lumshift , v->curr_luty[v->cur_field_type^1], v->curr_lutuv[v->cur_field_type^1], 0);
+                        INIT_LUT(v->lumscale2, v->lumshift2, v->last_luty[v->cur_field_type  ], v->last_lutuv[v->cur_field_type  ], 1);
+                    } else {
+                        INIT_LUT(v->lumscale2, v->lumshift2, v->curr_luty[v->cur_field_type^1], v->curr_lutuv[v->cur_field_type^1], 0);
+                        INIT_LUT(v->lumscale , v->lumshift , v->last_luty[v->cur_field_type  ], v->last_lutuv[v->cur_field_type  ], 1);
+                    }
+                    v->curr_use_ic = 1;
+                } else {
+                    INIT_LUT(v->lumscale , v->lumshift , v->last_luty[0], v->last_lutuv[0], 1);
+                    INIT_LUT(v->lumscale2, v->lumshift2, v->last_luty[1], v->last_lutuv[1], 1);
                 }
-                v->use_ic = 1;
+                v->last_use_ic = 1;
             }
             v->qs_last = v->s.quarter_sample;
             if (v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
diff --git a/libavcodec/vc1.h b/libavcodec/vc1.h
index 88980d4..1883f11 100644
--- a/libavcodec/vc1.h
+++ b/libavcodec/vc1.h
@@ -298,8 +298,11 @@  typedef struct VC1Context{
     int dmb_is_raw;                 ///< direct mb plane is raw
     int fmb_is_raw;                 ///< forward mb plane is raw
     int skip_is_raw;                ///< skip mb plane is not coded
-    uint8_t luty[256], lutuv[256];  ///< lookup tables used for intensity compensation
-    int use_ic;                     ///< use intensity compensation in B-frames
+    uint8_t last_luty[2][256], last_lutuv[2][256];  ///< lookup tables used for intensity compensation
+    uint8_t  aux_luty[2][256],  aux_lutuv[2][256];  ///< lookup tables used for intensity compensation
+    uint8_t next_luty[2][256], next_lutuv[2][256];  ///< lookup tables used for intensity compensation
+    uint8_t (*curr_luty)[256]  ,(*curr_lutuv)[256];
+    int last_use_ic, curr_use_ic, next_use_ic, aux_use_ic;
     int rnd;                        ///< rounding control
 
     /** Frame decoding info for S/M profiles only */
@@ -342,7 +345,6 @@  typedef struct VC1Context{
     int intcomp;
     uint8_t lumscale2;  ///< for interlaced field P picture
     uint8_t lumshift2;
-    uint8_t luty2[256], lutuv2[256]; // lookup tables used for intensity compensation
     VLC* mbmode_vlc;
     VLC* imv_vlc;
     VLC* twomvbp_vlc;
diff --git a/libavcodec/vc1dec.c b/libavcodec/vc1dec.c
index d42573d..73e1f3b 100644
--- a/libavcodec/vc1dec.c
+++ b/libavcodec/vc1dec.c
@@ -348,6 +348,7 @@  static void vc1_mc_1mv(VC1Context *v, int dir)
     int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
     int v_edge_pos = s->v_edge_pos >> v->field_mode;
     int i;
+    const uint8_t *luty, *lutuv;
 
     if ((!v->field_mode ||
          (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
@@ -386,15 +387,21 @@  static void vc1_mc_1mv(VC1Context *v, int dir)
             srcY = s->current_picture.f.data[0];
             srcU = s->current_picture.f.data[1];
             srcV = s->current_picture.f.data[2];
+            luty  = v->curr_luty [v->ref_field_type[dir]];
+            lutuv = v->curr_lutuv[v->ref_field_type[dir]];
         } else {
             srcY = s->last_picture.f.data[0];
             srcU = s->last_picture.f.data[1];
             srcV = s->last_picture.f.data[2];
+            luty  = v->last_luty [v->ref_field_type[dir]];
+            lutuv = v->last_lutuv[v->ref_field_type[dir]];
         }
     } else {
         srcY = s->next_picture.f.data[0];
         srcU = s->next_picture.f.data[1];
         srcV = s->next_picture.f.data[2];
+        luty  = v->next_luty [v->ref_field_type[dir]];
+        lutuv = v->next_lutuv[v->ref_field_type[dir]];
     }
 
     src_x   = s->mb_x * 16 + (mx   >> 2);
@@ -478,15 +485,15 @@  static void vc1_mc_1mv(VC1Context *v, int dir)
             src = srcY;
             for (j = 0; j < 17 + s->mspel * 2; j++) {
                 for (i = 0; i < 17 + s->mspel * 2; i++)
-                    src[i] = v->luty[src[i]];
+                    src[i] = luty[src[i]];
                 src += s->linesize;
             }
             src  = srcU;
             src2 = srcV;
             for (j = 0; j < 9; j++) {
                 for (i = 0; i < 9; i++) {
-                    src[i]  = v->lutuv[src[i]];
-                    src2[i] = v->lutuv[src2[i]];
+                    src[i]  = lutuv[src[i]];
+                    src2[i] = lutuv[src2[i]];
                 }
                 src  += s->uvlinesize;
                 src2 += s->uvlinesize;
@@ -544,6 +551,7 @@  static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir, int avg)
     int off;
     int fieldmv = (v->fcm == ILACE_FRAME) ? v->blk_mv_type[s->block_index[n]] : 0;
     int v_edge_pos = s->v_edge_pos >> v->field_mode;
+    const uint8_t *luty;
 
     if ((!v->field_mode ||
          (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
@@ -556,10 +564,15 @@  static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir, int avg)
     if (!dir) {
         if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
             srcY = s->current_picture.f.data[0];
-        } else
+            luty = v->curr_luty[v->ref_field_type[dir]];
+        } else {
             srcY = s->last_picture.f.data[0];
-    } else
+            luty = v->last_luty[v->ref_field_type[dir]];
+        }
+    } else {
         srcY = s->next_picture.f.data[0];
+        luty = v->next_luty[v->ref_field_type[dir]];
+    }
 
     if (v->field_mode) {
         if (v->cur_field_type != v->ref_field_type[dir])
@@ -686,7 +699,7 @@  static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir, int avg)
             src = srcY;
             for (j = 0; j < 9 + s->mspel * 2; j++) {
                 for (i = 0; i < 9 + s->mspel * 2; i++)
-                    src[i] = v->luty[src[i]];
+                    src[i] = luty[src[i]];
                 src += s->linesize << fieldmv;
             }
         }
@@ -774,6 +787,7 @@  static void vc1_mc_4mv_chroma(VC1Context *v, int dir)
     int valid_count;
     int chroma_ref_type = v->cur_field_type;
     int v_edge_pos = s->v_edge_pos >> v->field_mode;
+    const uint8_t *lutuv;
 
     if (!v->field_mode && !v->s.last_picture.f.data[0])
         return;
@@ -839,13 +853,16 @@  static void vc1_mc_4mv_chroma(VC1Context *v, int dir)
         if (v->field_mode && (v->cur_field_type != chroma_ref_type) && v->second_field) {
             srcU = s->current_picture.f.data[1];
             srcV = s->current_picture.f.data[2];
+            lutuv = v->curr_lutuv[chroma_ref_type];
         } else {
             srcU = s->last_picture.f.data[1];
             srcV = s->last_picture.f.data[2];
+            lutuv = v->last_lutuv[chroma_ref_type];
         }
     } else {
         srcU = s->next_picture.f.data[1];
         srcV = s->next_picture.f.data[2];
+        lutuv = v->next_lutuv[chroma_ref_type];
     }
 
     srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
@@ -896,8 +913,8 @@  static void vc1_mc_4mv_chroma(VC1Context *v, int dir)
             src2 = srcV;
             for (j = 0; j < 9; j++) {
                 for (i = 0; i < 9; i++) {
-                    src[i]  = v->lutuv[src[i]];
-                    src2[i] = v->lutuv[src2[i]];
+                    src[i]  = lutuv[src[i]];
+                    src2[i] = lutuv[src2[i]];
                 }
                 src  += s->uvlinesize;
                 src2 += s->uvlinesize;
@@ -980,13 +997,14 @@  static void vc1_mc_4mv_chroma4(VC1Context *v)
             if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
                 int i, j;
                 uint8_t *src, *src2;
+                const uint8_t *lutuv = v->last_lutuv[v->ref_field_type[0]];
 
                 src  = srcU;
                 src2 = srcV;
                 for (j = 0; j < 5; j++) {
                     for (i = 0; i < 5; i++) {
-                        src[i]  = v->lutuv[src[i]];
-                        src2[i] = v->lutuv[src2[i]];
+                        src[i]  = lutuv[src[i]];
+                        src2[i] = lutuv[src2[i]];
                     }
                     src  += s->uvlinesize << 1;
                     src2 += s->uvlinesize << 1;
@@ -1978,29 +1996,30 @@  static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
                             int direct, int mode)
 {
-    if (v->use_ic) {
+    int use_ic = v->next_use_ic || v->curr_use_ic || v->last_use_ic;
+    if (use_ic) {
         v->mv_mode2 = v->mv_mode;
         v->mv_mode  = MV_PMODE_INTENSITY_COMP;
     }
     if (direct) {
         vc1_mc_1mv(v, 0);
         vc1_interp_mc(v);
-        if (v->use_ic)
+        if (use_ic)
             v->mv_mode = v->mv_mode2;
         return;
     }
     if (mode == BMV_TYPE_INTERPOLATED) {
         vc1_mc_1mv(v, 0);
         vc1_interp_mc(v);
-        if (v->use_ic)
+        if (use_ic)
             v->mv_mode = v->mv_mode2;
         return;
     }
 
-    if (v->use_ic && (mode == BMV_TYPE_BACKWARD))
+    if (use_ic && (mode == BMV_TYPE_BACKWARD))
         v->mv_mode = v->mv_mode2;
     vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
-    if (v->use_ic)
+    if (use_ic)
         v->mv_mode = v->mv_mode2;
 }