Merge commit '1542ec96389f32e5081c6c607e4b6f5e257ccdf2'
authorHendrik Leppkes <h.leppkes@gmail.com>
Mon, 10 Aug 2015 09:16:27 +0000 (11:16 +0200)
committerHendrik Leppkes <h.leppkes@gmail.com>
Mon, 10 Aug 2015 09:16:27 +0000 (11:16 +0200)
* commit '1542ec96389f32e5081c6c607e4b6f5e257ccdf2':
  cosmetics: Drop spurious spaces from if clauses

Conflicts:
libavcodec/vc1_block.c

Merged-by: Hendrik Leppkes <h.leppkes@gmail.com>
1  2 
libavcodec/vc1_block.c

diff --combined libavcodec/vc1_block.c
index 287d81dc98729100715cc30f627271b6cd5ba78b,4588af4100fab2ce55c4778cf0369105d2b6d0fe..255ba1da70a8da1f616d95bdb116188bb2c7529f
@@@ -4,20 -4,20 +4,20 @@@
   * Copyright (c) 2006-2007 Konstantin Shishkov
   * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
   *
 - * This file is part of Libav.
 + * This file is part of FFmpeg.
   *
 - * Libav is free software; you can redistribute it and/or
 + * FFmpeg is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
   * version 2.1 of the License, or (at your option) any later version.
   *
 - * Libav is distributed in the hope that it will be useful,
 + * FFmpeg is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
 - * License along with Libav; if not, write to the Free Software
 + * License along with FFmpeg; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
   */
  
  #define DC_VLC_BITS 9
  
  // offset tables for interlaced picture MVDATA decoding
 -static const int offset_table1[9] = {  0,  1,  2,  4,  8, 16, 32,  64, 128 };
 -static const int offset_table2[9] = {  0,  1,  3,  7, 15, 31, 63, 127, 255 };
 +static const uint8_t offset_table[2][9] = {
 +    {  0,  1,  2,  4,  8, 16, 32,  64, 128 },
 +    {  0,  1,  3,  7, 15, 31, 63, 127, 255 },
 +};
  
  /***********************************************************************/
  /**
   * @{
   */
  
 -/**
 - * Imode types
 - * @{
 - */
 -enum Imode {
 -    IMODE_RAW,
 -    IMODE_NORM2,
 -    IMODE_DIFF2,
 -    IMODE_NORM6,
 -    IMODE_DIFF6,
 -    IMODE_ROWSKIP,
 -    IMODE_COLSKIP
 -};
 -/** @} */ //imode defines
  
 -static void init_block_index(VC1Context *v)
 +static inline void init_block_index(VC1Context *v)
  {
      MpegEncContext *s = &v->s;
      ff_init_block_index(s);
@@@ -99,14 -111,12 +99,14 @@@ static void vc1_put_signed_blocks_clamp
              s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][3],
                                                s->dest[0] - v_dist * s->linesize - 8,
                                                stride_y);
 +            if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
              s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][4],
                                                s->dest[1] - 8 * s->uvlinesize - 8,
                                                s->uvlinesize);
              s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][5],
                                                s->dest[2] - 8 * s->uvlinesize - 8,
                                                s->uvlinesize);
 +            }
          }
          if (s->mb_x == s->mb_width - 1) {
              top_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x;
              s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][3],
                                                s->dest[0] - v_dist * s->linesize + 8,
                                                stride_y);
 +            if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
              s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][4],
                                                s->dest[1] - 8 * s->uvlinesize,
                                                s->uvlinesize);
              s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][5],
                                                s->dest[2] - 8 * s->uvlinesize,
                                                s->uvlinesize);
 +            }
          }
      }
  
          s->mb_intra = 1;                                                \
      } else {                                                            \
          index1 = index % 6;                                             \
 -        if (!s->quarter_sample && index1 == 5) val = 1;                 \
 -        else                                   val = 0;                 \
 -        if (size_table[index1] - val > 0)                               \
 -            val = get_bits(gb, size_table[index1] - val);               \
 -        else                                   val = 0;                 \
 -        sign = 0 - (val&1);                                             \
 -        _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign;     \
 +        _dmv_x = offset_table[1][index1];                               \
 +        val = size_table[index1] - (!s->quarter_sample && index1 == 5); \
 +        if (val > 0) {                                                  \
 +            val = get_bits(gb, val);                                    \
 +            sign = 0 - (val & 1);                                       \
 +            _dmv_x = (sign ^ ((val >> 1) + _dmv_x)) - sign;             \
 +        }                                                               \
                                                                          \
          index1 = index / 6;                                             \
 -        if (!s->quarter_sample && index1 == 5) val = 1;                 \
 -        else                                   val = 0;                 \
 -        if (size_table[index1] - val > 0)                               \
 -            val = get_bits(gb, size_table[index1] - val);               \
 -        else                                   val = 0;                 \
 -        sign = 0 - (val & 1);                                           \
 -        _dmv_y = (sign ^ ((val >> 1) + offset_table[index1])) - sign;   \
 +        _dmv_y = offset_table[1][index1];                               \
 +        val = size_table[index1] - (!s->quarter_sample && index1 == 5); \
 +        if (val > 0) {                                                  \
 +            val = get_bits(gb, val);                                    \
 +            sign = 0 - (val & 1);                                       \
 +            _dmv_y = (sign ^ ((val >> 1) + _dmv_y)) - sign;             \
 +        }                                                               \
      }
  
  static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
                                                     int *dmv_y, int *pred_flag)
  {
      int index, index1;
 -    int extend_x = 0, extend_y = 0;
 +    int extend_x, extend_y;
      GetBitContext *gb = &v->s.gb;
      int bits, esc;
      int val, sign;
 -    const int* offs_tab;
  
      if (v->numref) {
          bits = VC1_2REF_MVDATA_VLC_BITS;
          bits = VC1_1REF_MVDATA_VLC_BITS;
          esc  = 71;
      }
 -    switch (v->dmvrange) {
 -    case 1:
 -        extend_x = 1;
 -        break;
 -    case 2:
 -        extend_y = 1;
 -        break;
 -    case 3:
 -        extend_x = extend_y = 1;
 -        break;
 -    }
 +    extend_x = v->dmvrange & 1;
 +    extend_y = (v->dmvrange >> 1) & 1;
      index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
      if (index == esc) {
          *dmv_x = get_bits(gb, v->k_x);
          *dmv_y = get_bits(gb, v->k_y);
          if (v->numref) {
 -            if (pred_flag) {
 +            if (pred_flag)
                  *pred_flag = *dmv_y & 1;
 -                *dmv_y     = (*dmv_y + *pred_flag) >> 1;
 -            } else {
 -                *dmv_y     = (*dmv_y + (*dmv_y & 1)) >> 1;
 -            }
 +            *dmv_y = (*dmv_y + (*dmv_y & 1)) >> 1;
          }
      }
      else {
 -        if (extend_x)
 -            offs_tab = offset_table2;
 -        else
 -            offs_tab = offset_table1;
 +        av_assert0(index < esc);
          index1 = (index + 1) % 9;
          if (index1 != 0) {
              val    = get_bits(gb, index1 + extend_x);
 -            sign   = 0 -(val & 1);
 -            *dmv_x = (sign ^ ((val >> 1) + offs_tab[index1])) - sign;
 +            sign   = 0 - (val & 1);
 +            *dmv_x = (sign ^ ((val >> 1) + offset_table[extend_x][index1])) - sign;
          } else
              *dmv_x = 0;
 -        if (extend_y)
 -            offs_tab = offset_table2;
 -        else
 -            offs_tab = offset_table1;
          index1 = (index + 1) / 9;
          if (index1 > v->numref) {
 -            val    = get_bits(gb, (index1 + (extend_y << v->numref)) >> v->numref);
 +            val    = get_bits(gb, (index1 >> v->numref) + extend_y);
              sign   = 0 - (val & 1);
 -            *dmv_y = (sign ^ ((val >> 1) + offs_tab[index1 >> v->numref])) - sign;
 +            *dmv_y = (sign ^ ((val >> 1) + offset_table[extend_y][index1 >> v->numref])) - sign;
          } else
              *dmv_y = 0;
          if (v->numref && pred_flag)
@@@ -392,12 -420,6 +392,12 @@@ static inline int ff_vc1_pred_dc(MpegEn
      int q1, q2 = 0;
      int dqscale_index;
  
 +    /* scale predictors if needed */
 +    q1 = s->current_picture.qscale_table[mb_pos];
 +    dqscale_index = s->y_dc_scale_table[q1] - 1;
 +    if (dqscale_index < 0)
 +        return 0;
 +
      wrap = s->block_wrap[n];
      dc_val = s->dc_val[0] + s->block_index[n];
  
      c = dc_val[ - 1];
      b = dc_val[ - 1 - wrap];
      a = dc_val[ - wrap];
 -    /* scale predictors if needed */
 -    q1 = s->current_picture.qscale_table[mb_pos];
 -    dqscale_index = s->y_dc_scale_table[q1] - 1;
 -    if (dqscale_index < 0)
 -        return 0;
 +
      if (c_avail && (n != 1 && n != 3)) {
          q2 = s->current_picture.qscale_table[mb_pos - 1];
          if (q2 && q2 != q1)
              b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
      }
  
 -    if (a_avail && c_avail) {
 -        if (abs(a - b) <= abs(b - c)) {
 -            pred     = c;
 -            *dir_ptr = 1; // left
 -        } else {
 -            pred     = a;
 -            *dir_ptr = 0; // top
 -        }
 +    if (c_avail && (!a_avail || abs(a - b) <= abs(b - c))) {
 +        pred     = c;
 +        *dir_ptr = 1; // left
      } else if (a_avail) {
          pred     = a;
          *dir_ptr = 0; // top
 -    } else if (c_avail) {
 -        pred     = c;
 -        *dir_ptr = 1; // left
      } else {
          pred     = 0;
          *dir_ptr = 1; // left
@@@ -493,16 -527,17 +493,16 @@@ static void vc1_decode_ac_coeff(VC1Cont
                                  int *value, int codingset)
  {
      GetBitContext *gb = &v->s.gb;
 -    int index, escape, run = 0, level = 0, lst = 0;
 +    int index, run, level, lst, sign;
  
      index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
      if (index != ff_vc1_ac_sizes[codingset] - 1) {
          run   = vc1_index_decode_table[codingset][index][0];
          level = vc1_index_decode_table[codingset][index][1];
          lst   = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
 -        if (get_bits1(gb))
 -            level = -level;
 +        sign  = get_bits1(gb);
      } else {
 -        escape = decode210(gb);
 +        int escape = decode210(gb);
          if (escape != 2) {
              index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
              run   = vc1_index_decode_table[codingset][index][0];
                  else
                      run += vc1_delta_run_table[codingset][level] + 1;
              }
 -            if (get_bits1(gb))
 -                level = -level;
 +            sign = get_bits1(gb);
          } else {
 -            int sign;
              lst = get_bits1(gb);
              if (v->s.esc3_level_length == 0) {
                  if (v->pq < 8 || v->dquantfrm) { // table 59
              run   = get_bits(gb, v->s.esc3_run_length);
              sign  = get_bits1(gb);
              level = get_bits(gb, v->s.esc3_level_length);
 -            if (sign)
 -                level = -level;
          }
      }
  
      *last  = lst;
      *skip  = run;
 -    *value = level;
 +    *value = (level ^ -sign) + sign;
  }
  
  /** Decode intra block in intra frames - should be faster than decode_intra_block
@@@ -559,7 -598,7 +559,7 @@@ static int vc1_decode_i_block(VC1Contex
      int i;
      int16_t *dc_val;
      int16_t *ac_val, *ac_val2;
 -    int dcdiff;
 +    int dcdiff, scale;
  
      /* Get DC differential */
      if (n < 4) {
          return -1;
      }
      if (dcdiff) {
 +        const int m = (v->pq == 1 || v->pq == 2) ? 3 - v->pq : 0;
          if (dcdiff == 119 /* ESC index value */) {
 -            /* TODO: Optimize */
 -            if (v->pq == 1)      dcdiff = get_bits(gb, 10);
 -            else if (v->pq == 2) dcdiff = get_bits(gb, 9);
 -            else                 dcdiff = get_bits(gb, 8);
 +            dcdiff = get_bits(gb, 8 + m);
          } else {
 -            if (v->pq == 1)
 -                dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
 -            else if (v->pq == 2)
 -                dcdiff = (dcdiff << 1) + get_bits1(gb)   - 1;
 +            if (m)
 +                dcdiff = (dcdiff << m) + get_bits(gb, m) - ((1 << m) - 1);
          }
          if (get_bits1(gb))
              dcdiff = -dcdiff;
      *dc_val = dcdiff;
  
      /* Store the quantized DC coeff, used for prediction */
 -    if (n < 4) {
 -        block[0] = dcdiff * s->y_dc_scale;
 -    } else {
 -        block[0] = dcdiff * s->c_dc_scale;
 -    }
 -    /* Skip ? */
 -    if (!coded) {
 -        goto not_coded;
 -    }
 +    if (n < 4)
 +        scale = s->y_dc_scale;
 +    else
 +        scale = s->c_dc_scale;
 +    block[0] = dcdiff * scale;
  
 -    // AC Decoding
 -    i = 1;
 +    ac_val  = s->ac_val[0][0] + s->block_index[n] * 16;
 +    ac_val2 = ac_val;
 +    if (dc_pred_dir) // left
 +        ac_val -= 16;
 +    else // top
 +        ac_val -= 16 * s->block_wrap[n];
  
 -    {
 +    scale = v->pq * 2 + v->halfpq;
 +
 +    //AC Decoding
 +    i = !!coded;
 +
 +    if (coded) {
          int last = 0, skip, value;
          const uint8_t *zz_table;
 -        int scale;
          int k;
  
 -        scale = v->pq * 2 + v->halfpq;
 -
          if (v->s.ac_pred) {
              if (!dc_pred_dir)
                  zz_table = v->zz_8x8[2];
          } else
              zz_table = v->zz_8x8[1];
  
 -        ac_val  = s->ac_val[0][0] + s->block_index[n] * 16;
 -        ac_val2 = ac_val;
 -        if (dc_pred_dir) // left
 -            ac_val -= 16;
 -        else // top
 -            ac_val -= 16 * s->block_wrap[n];
 -
          while (!last) {
              vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
              i += skip;
  
          /* apply AC prediction if needed */
          if (s->ac_pred) {
 +            int sh;
              if (dc_pred_dir) { // left
 -                for (k = 1; k < 8; k++)
 -                    block[k << v->left_blk_sh] += ac_val[k];
 +                sh = v->left_blk_sh;
              } else { // top
 -                for (k = 1; k < 8; k++)
 -                    block[k << v->top_blk_sh] += ac_val[k + 8];
 +                sh = v->top_blk_sh;
 +                ac_val += 8;
              }
 +            for (k = 1; k < 8; k++)
 +                block[k << sh] += ac_val[k];
          }
          /* save AC coeffs for further prediction */
          for (k = 1; k < 8; k++) {
                      block[k] += (block[k] < 0) ? -v->pq : v->pq;
              }
  
 -        if (s->ac_pred) i = 63;
 -    }
 -
 -not_coded:
 -    if (!coded) {
 -        int k, scale;
 -        ac_val  = s->ac_val[0][0] + s->block_index[n] * 16;
 -        ac_val2 = ac_val;
 +    } else {
 +        int k;
  
 -        i = 0;
 -        scale = v->pq * 2 + v->halfpq;
          memset(ac_val2, 0, 16 * 2);
 -        if (dc_pred_dir) { // left
 -            ac_val -= 16;
 -            if (s->ac_pred)
 -                memcpy(ac_val2, ac_val, 8 * 2);
 -        } else { // top
 -            ac_val -= 16 * s->block_wrap[n];
 -            if (s->ac_pred)
 -                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
 -        }
  
          /* apply AC prediction if needed */
          if (s->ac_pred) {
 +            int sh;
              if (dc_pred_dir) { //left
 -                for (k = 1; k < 8; k++) {
 -                    block[k << v->left_blk_sh] = ac_val[k] * scale;
 -                    if (!v->pquantizer && block[k << v->left_blk_sh])
 -                        block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
 -                }
 +                sh = v->left_blk_sh;
              } else { // top
 -                for (k = 1; k < 8; k++) {
 -                    block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
 -                    if (!v->pquantizer && block[k << v->top_blk_sh])
 -                        block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
 -                }
 +                sh = v->top_blk_sh;
 +                ac_val  += 8;
 +                ac_val2 += 8;
 +            }
 +            memcpy(ac_val2, ac_val, 8 * 2);
 +            for (k = 1; k < 8; k++) {
 +                block[k << sh] = ac_val[k] * scale;
 +                if (!v->pquantizer && block[k << sh])
 +                    block[k << sh] += (block[k << sh] < 0) ? -v->pq : v->pq;
              }
 -            i = 63;
          }
      }
 +    if (s->ac_pred) i = 63;
      s->block_last_index[n] = i;
  
      return 0;
@@@ -697,7 -759,7 +697,7 @@@ static int vc1_decode_i_block_adv(VC1Co
      MpegEncContext *s = &v->s;
      int dc_pred_dir = 0; /* Direction of the DC prediction used */
      int i;
 -    int16_t *dc_val;
 +    int16_t *dc_val = NULL;
      int16_t *ac_val, *ac_val2;
      int dcdiff;
      int a_avail = v->a_avail, c_avail = v->c_avail;
          return -1;
      }
      if (dcdiff) {
 +        const int m = (mquant == 1 || mquant == 2) ? 3 - mquant : 0;
          if (dcdiff == 119 /* ESC index value */) {
 -            /* TODO: Optimize */
 -            if (mquant == 1)      dcdiff = get_bits(gb, 10);
 -            else if (mquant == 2) dcdiff = get_bits(gb, 9);
 -            else                  dcdiff = get_bits(gb, 8);
 +            dcdiff = get_bits(gb, 8 + m);
          } else {
 -            if (mquant == 1)
 -                dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
 -            else if (mquant == 2)
 -                dcdiff = (dcdiff << 1) + get_bits1(gb)   - 1;
 +            if (m)
 +                dcdiff = (dcdiff << m) + get_bits(gb, m) - ((1 << m) - 1);
          }
          if (get_bits1(gb))
              dcdiff = -dcdiff;
      *dc_val = dcdiff;
  
      /* Store the quantized DC coeff, used for prediction */
 -    if (n < 4) {
 -        block[0] = dcdiff * s->y_dc_scale;
 -    } else {
 -        block[0] = dcdiff * s->c_dc_scale;
 -    }
 -
 -    //AC Decoding
 -    i = 1;
 +    if (n < 4)
 +        scale = s->y_dc_scale;
 +    else
 +        scale = s->c_dc_scale;
 +    block[0] = dcdiff * scale;
  
      /* check if AC is needed at all */
      if (!a_avail && !c_avail)
          use_pred = 0;
 -    ac_val  = s->ac_val[0][0] + s->block_index[n] * 16;
 -    ac_val2 = ac_val;
  
      scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
  
 +    ac_val  = s->ac_val[0][0] + s->block_index[n] * 16;
 +    ac_val2 = ac_val;
      if (dc_pred_dir) // left
          ac_val -= 16;
      else // top
          ac_val -= 16 * s->block_wrap[n];
  
      q1 = s->current_picture.qscale_table[mb_pos];
 -    if (dc_pred_dir && c_avail && mb_pos)
 -        q2 = s->current_picture.qscale_table[mb_pos - 1];
 -    if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
 -        q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
 -    if (dc_pred_dir && n == 1)
 -        q2 = q1;
 -    if (!dc_pred_dir && n == 2)
 -        q2 = q1;
      if (n == 3)
          q2 = q1;
 +    else if (dc_pred_dir) {
 +        if (n == 1)
 +            q2 = q1;
 +        else if (c_avail && mb_pos)
 +            q2 = s->current_picture.qscale_table[mb_pos - 1];
 +    } else {
 +        if (n == 2)
 +            q2 = q1;
 +        else if (a_avail && mb_pos >= s->mb_stride)
 +            q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
 +    }
 +
 +    //AC Decoding
 +    i = 1;
  
      if (coded) {
          int last = 0, skip, value;
  
          /* apply AC prediction if needed */
          if (use_pred) {
 +            int sh;
 +            if (dc_pred_dir) { // left
 +                sh = v->left_blk_sh;
 +            } else { // top
 +                sh = v->top_blk_sh;
 +                ac_val += 8;
 +            }
              /* scale predictors if needed*/
              if (q2 && q1 != q2) {
                  q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
 -                q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
 -
                  if (q1 < 1)
                      return AVERROR_INVALIDDATA;
 -                if (dc_pred_dir) { // left
 -                    for (k = 1; k < 8; k++)
 -                        block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
 -                } else { // top
 -                    for (k = 1; k < 8; k++)
 -                        block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
 -                }
 +                q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
 +                for (k = 1; k < 8; k++)
 +                    block[k << sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
              } else {
 -                if (dc_pred_dir) { //left
 -                    for (k = 1; k < 8; k++)
 -                        block[k << v->left_blk_sh] += ac_val[k];
 -                } else { //top
 -                    for (k = 1; k < 8; k++)
 -                        block[k << v->top_blk_sh] += ac_val[k + 8];
 -                }
 +                for (k = 1; k < 8; k++)
 +                    block[k << sh] += ac_val[k];
              }
          }
          /* save AC coeffs for further prediction */
                      block[k] += (block[k] < 0) ? -mquant : mquant;
              }
  
 -        if (use_pred) i = 63;
      } else { // no AC coeffs
          int k;
  
          memset(ac_val2, 0, 16 * 2);
 -        if (dc_pred_dir) { // left
 -            if (use_pred) {
 -                memcpy(ac_val2, ac_val, 8 * 2);
 -                if (q2 && q1 != q2) {
 -                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
 -                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
 -                    if (q1 < 1)
 -                        return AVERROR_INVALIDDATA;
 -                    for (k = 1; k < 8; k++)
 -                        ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
 -                }
 -            }
 -        } else { // top
 -            if (use_pred) {
 -                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
 -                if (q2 && q1 != q2) {
 -                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
 -                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
 -                    if (q1 < 1)
 -                        return AVERROR_INVALIDDATA;
 -                    for (k = 1; k < 8; k++)
 -                        ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
 -                }
 -            }
 -        }
  
          /* apply AC prediction if needed */
          if (use_pred) {
 +            int sh;
              if (dc_pred_dir) { // left
 -                for (k = 1; k < 8; k++) {
 -                    block[k << v->left_blk_sh] = ac_val2[k] * scale;
 -                    if (!v->pquantizer && block[k << v->left_blk_sh])
 -                        block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
 -                }
 +                sh = v->left_blk_sh;
              } else { // top
 -                for (k = 1; k < 8; k++) {
 -                    block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
 -                    if (!v->pquantizer && block[k << v->top_blk_sh])
 -                        block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
 -                }
 +                sh = v->top_blk_sh;
 +                ac_val  += 8;
 +                ac_val2 += 8;
 +            }
 +            memcpy(ac_val2, ac_val, 8 * 2);
 +            if (q2 && q1 != q2) {
 +                q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
 +                q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
 +                if (q1 < 1)
 +                    return AVERROR_INVALIDDATA;
 +                for (k = 1; k < 8; k++)
 +                    ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
 +            }
 +            for (k = 1; k < 8; k++) {
 +                block[k << sh] = ac_val2[k] * scale;
 +                if (!v->pquantizer && block[k << sh])
 +                    block[k << sh] += (block[k << sh] < 0) ? -mquant : mquant;
              }
 -            i = 63;
          }
      }
 +    if (use_pred) i = 63;
      s->block_last_index[n] = i;
  
      return 0;
@@@ -887,7 -971,7 +887,7 @@@ static int vc1_decode_intra_block(VC1Co
      MpegEncContext *s = &v->s;
      int dc_pred_dir = 0; /* Direction of the DC prediction used */
      int i;
 -    int16_t *dc_val;
 +    int16_t *dc_val = NULL;
      int16_t *ac_val, *ac_val2;
      int dcdiff;
      int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
      s->bdsp.clear_block(block);
  
      /* XXX: Guard against dumb values of mquant */
 -    mquant = (mquant < 1) ? 0 : ((mquant > 31) ? 31 : mquant);
 +    mquant = av_clip_uintp2(mquant, 5);
  
      /* Set DC scale - y and c use the same */
      s->y_dc_scale = s->y_dc_scale_table[mquant];
          return -1;
      }
      if (dcdiff) {
 +        const int m = (mquant == 1 || mquant == 2) ? 3 - mquant : 0;
          if (dcdiff == 119 /* ESC index value */) {
 -            /* TODO: Optimize */
 -            if (mquant == 1)      dcdiff = get_bits(gb, 10);
 -            else if (mquant == 2) dcdiff = get_bits(gb, 9);
 -            else                  dcdiff = get_bits(gb, 8);
 +            dcdiff = get_bits(gb, 8 + m);
          } else {
 -            if (mquant == 1)
 -                dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
 -            else if (mquant == 2)
 -                dcdiff = (dcdiff << 1) + get_bits1(gb)   - 1;
 +            if (m)
 +                dcdiff = (dcdiff << m) + get_bits(gb, m) - ((1 << m) - 1);
          }
          if (get_bits1(gb))
              dcdiff = -dcdiff;
          q2 = s->current_picture.qscale_table[mb_pos - 1];
      if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
          q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
-     if ( dc_pred_dir && n == 1)
+     if (dc_pred_dir && n == 1)
          q2 = q1;
      if (!dc_pred_dir && n == 2)
          q2 = q1;
@@@ -1245,7 -1333,8 +1245,7 @@@ static int vc1_decode_p_block(VC1Contex
  
  /** @} */ // Macroblock group
  
 -static const int size_table  [6] = { 0, 2, 3, 4,  5,  8 };
 -static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
 +static const uint8_t size_table[6] = { 0, 2, 3, 4,  5,  8 };
  
  /** Decode one P-frame MB
   */
@@@ -1327,7 -1416,7 +1327,7 @@@ static int vc1_decode_p_mb(VC1Context *
  
                      vc1_decode_intra_block(v, s->block[i], i, val, mquant,
                                             (i & 4) ? v->codingset2 : v->codingset);
 -                    if ((i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
 +                    if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
                          continue;
                      v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
                      if (v->rangeredfrm)
                  } else if (val) {
                      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->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
 +                                             CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
                      block_cbp |= pat << (i << 2);
                      if (!v->ttmbf && ttmb < 8)
                          ttmb = -1;
  
                      vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
                                             (i & 4) ? v->codingset2 : v->codingset);
 -                    if ((i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
 +                    if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
                          continue;
                      v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
                      if (v->rangeredfrm)
                      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->avctx->flags & AV_CODEC_FLAG_GRAY),
 +                                             CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY),
                                               &block_tt);
                      block_cbp |= pat << (i << 2);
                      if (!v->ttmbf && ttmb < 8)
@@@ -1513,7 -1602,7 +1513,7 @@@ static int vc1_decode_p_mb_intfr(VC1Con
      int idx_mbmode = 0, mvbp;
      int stride_y, fieldtx;
  
 -    mquant = v->pq; /* Loosy initialization */
 +    mquant = v->pq; /* Lossy initialization */
  
      if (v->skip_is_raw)
          skipped = get_bits1(gb);
  
                  vc1_decode_intra_block(v, s->block[i], i, val, mquant,
                                         (i & 4) ? v->codingset2 : v->codingset);
 -                if ((i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
 +                if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
                      continue;
                  v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
                  if (i < 4) {
              dst_idx = 0;
              if (fourmv) {
                  mvbp = v->fourmvbp;
 -                for (i = 0; i < 6; i++) {
 -                    if (i < 4) {
 -                        dmv_x = dmv_y = 0;
 -                        val   = ((mvbp >> (3 - i)) & 1);
 -                        if (val) {
 -                            get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
 -                        }
 -                        ff_vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0);
 -                        ff_vc1_mc_4mv_luma(v, i, 0, 0);
 -                    } else if (i == 4) {
 -                        ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
 -                    }
 +                for (i = 0; i < 4; i++) {
 +                    dmv_x = dmv_y = 0;
 +                    if (mvbp & (8 >> i))
 +                        get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
 +                    ff_vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0);
 +                    ff_vc1_mc_4mv_luma(v, i, 0, 0);
                  }
 +                ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
              } else if (twomv) {
                  mvbp  = v->twomvbp;
                  dmv_x = dmv_y = 0;
                      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->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
 +                                             CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
                      block_cbp |= pat << (i << 2);
                      if (!v->ttmbf && ttmb < 8)
                          ttmb = -1;
@@@ -1716,11 -1810,11 +1716,11 @@@ static int vc1_decode_p_mb_intfi(VC1Con
      int val; /* temp values */
      int first_block = 1;
      int dst_idx, off;
 -    int pred_flag;
 +    int pred_flag = 0;
      int block_cbp = 0, pat, block_tt = 0;
      int idx_mbmode = 0;
  
 -    mquant = v->pq; /* Loosy initialization */
 +    mquant = v->pq; /* Lossy initialization */
  
      idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
      if (idx_mbmode <= 1) { // intra MB
  
              vc1_decode_intra_block(v, s->block[i], i, val, mquant,
                                     (i & 4) ? v->codingset2 : v->codingset);
 -            if ((i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
 +            if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
                  continue;
              v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
              off  = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
      } else {
          s->mb_intra = v->is_intra[s->mb_x] = 0;
          s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
 -        for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
 +        for (i = 0; i < 6; i++)
 +            v->mb_type[0][s->block_index[i]] = 0;
          if (idx_mbmode <= 5) { // 1-MV
              dmv_x = dmv_y = pred_flag = 0;
              if (idx_mbmode & 1) {
              mb_has_coeffs = !(idx_mbmode & 2);
          } else { // 4-MV
              v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
 -            for (i = 0; i < 6; i++) {
 -                if (i < 4) {
 -                    dmv_x = dmv_y = pred_flag = 0;
 -                    val   = ((v->fourmvbp >> (3 - i)) & 1);
 -                    if (val) {
 -                        get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
 -                    }
 -                    ff_vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
 -                    ff_vc1_mc_4mv_luma(v, i, 0, 0);
 -                } else if (i == 4)
 -                    ff_vc1_mc_4mv_chroma(v, 0);
 +            for (i = 0; i < 4; i++) {
 +                dmv_x = dmv_y = pred_flag = 0;
 +                if (v->fourmvbp & (8 >> i))
 +                    get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
 +                ff_vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
 +                ff_vc1_mc_4mv_luma(v, i, 0, 0);
              }
 +            ff_vc1_mc_4mv_chroma(v, 0);
              mb_has_coeffs = idx_mbmode & 1;
          }
          if (mb_has_coeffs)
                  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->avctx->flags & AV_CODEC_FLAG_GRAY),
 +                                         CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY),
                                           &block_tt);
                  block_cbp |= pat << (i << 2);
 -                if (!v->ttmbf && ttmb < 8) ttmb = -1;
 +                if (!v->ttmbf && ttmb < 8)
 +                    ttmb = -1;
                  first_block = 0;
              }
          }
@@@ -1953,7 -2049,7 +1953,7 @@@ static void vc1_decode_b_mb(VC1Context 
  
              vc1_decode_intra_block(v, s->block[i], i, val, mquant,
                                     (i & 4) ? v->codingset2 : v->codingset);
 -            if ((i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
 +            if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
                  continue;
              v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
              if (v->rangeredfrm)
              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->avctx->flags & AV_CODEC_FLAG_GRAY), NULL);
 +                               CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), NULL);
              if (!v->ttmbf && ttmb < 8)
                  ttmb = -1;
              first_block = 0;
@@@ -1993,9 -2089,9 +1993,9 @@@ static void vc1_decode_b_mb_intfi(VC1Co
      int fwd;
      int dmv_x[2], dmv_y[2], pred_flag[2];
      int bmvtype = BMV_TYPE_BACKWARD;
 -    int idx_mbmode, interpmvp;
 +    int idx_mbmode;
  
 -    mquant      = v->pq; /* Loosy initialization */
 +    mquant      = v->pq; /* Lossy initialization */
      s->mb_intra = 0;
  
      idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
  
              vc1_decode_intra_block(v, s->block[i], i, val, mquant,
                                     (i & 4) ? v->codingset2 : v->codingset);
 -            if ((i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
 +            if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
                  continue;
              v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
              if (v->rangeredfrm)
      } else {
          s->mb_intra = v->is_intra[s->mb_x] = 0;
          s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
 -        for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
 +        for (i = 0; i < 6; i++)
 +            v->mb_type[0][s->block_index[i]] = 0;
          if (v->fmb_is_raw)
              fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
          else
              fwd = v->forward_mb_plane[mb_pos];
          if (idx_mbmode <= 5) { // 1-MV
 +            int interpmvp = 0;
              dmv_x[0]     = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
              pred_flag[0] = pred_flag[1] = 0;
              if (fwd)
              if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
                  get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
              }
 -            if (bmvtype == BMV_TYPE_INTERPOLATED && interpmvp) {
 +            if (interpmvp) {
                  get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
              }
              if (bmvtype == BMV_TYPE_DIRECT) {
                  dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
                  dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
 +                if (!s->next_picture_ptr->field_picture) {
 +                    av_log(s->avctx, AV_LOG_ERROR, "Mixed field/frame direct mode not supported\n");
 +                    return;
 +                }
              }
              ff_vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
              vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
                  bmvtype = BMV_TYPE_FORWARD;
              v->bmvtype  = bmvtype;
              v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
 -            for (i = 0; i < 6; i++) {
 -                if (i < 4) {
 -                    dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
 -                    dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
 -                    val = ((v->fourmvbp >> (3 - i)) & 1);
 -                    if (val) {
 -                        get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
 -                                                 &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
 -                                             &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
 -                    }
 -                    ff_vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
 -                    ff_vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD, 0);
 -                } else if (i == 4)
 -                    ff_vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
 +            for (i = 0; i < 4; i++) {
 +                dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
 +                dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
 +                if (v->fourmvbp & (8 >> i)) {
 +                    get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
 +                                             &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
 +                                         &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
 +                }
 +                ff_vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
 +                ff_vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD, 0);
              }
 +            ff_vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
              mb_has_coeffs = idx_mbmode & 1;
          }
          if (mb_has_coeffs)
                  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->avctx->flags & AV_CODEC_FLAG_GRAY), NULL);
 +                                   CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), NULL);
                  if (!v->ttmbf && ttmb < 8)
                      ttmb = -1;
                  first_block = 0;
@@@ -2188,8 -2281,6 +2188,8 @@@ static int vc1_decode_b_mb_intfr(VC1Con
          direct = v->direct_mb_plane[mb_pos];
  
      if (direct) {
 +        if (s->next_picture_ptr->field_picture)
 +            av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
          s->mv[0][0][0] = s->current_picture.motion_val[0][s->block_index[0]][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0]][0], v->bfraction, 0, s->quarter_sample);
          s->mv[0][0][1] = s->current_picture.motion_val[0][s->block_index[0]][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0]][1], v->bfraction, 0, s->quarter_sample);
          s->mv[1][0][0] = s->current_picture.motion_val[1][s->block_index[0]][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0]][0], v->bfraction, 1, s->quarter_sample);
  
              vc1_decode_intra_block(v, s->block[i], i, val, mquant,
                                     (i & 4) ? v->codingset2 : v->codingset);
 -            if (i > 3 && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
 +            if (CONFIG_GRAY && i > 3 && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
                  continue;
              v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
              if (i < 4) {
                      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->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
 +                                             CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
                      block_cbp |= pat << (i << 2);
                      if (!v->ttmbf && ttmb < 8)
                          ttmb = -1;
@@@ -2560,7 -2651,7 +2560,7 @@@ static void vc1_decode_i_blocks(VC1Cont
  
                  vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
  
 -                if (k > 3 && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
 +                if (CONFIG_GRAY && k > 3 && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
                      continue;
                  v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
                  if (v->pq >= 9 && v->overlap) {
                  if (s->mb_x) {
                      v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
                      v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
 -                    if (!(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
 +                    if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
                          v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
                          v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
                      }
                  if (!s->first_slice_line) {
                      v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
                      v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
 -                    if (!(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
 +                    if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
                          v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
                          v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
                      }
@@@ -2692,7 -2783,7 +2692,7 @@@ static void vc1_decode_i_blocks_adv(VC1
              if (v->fieldtx_is_raw)
                  v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
              cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
-             if ( v->acpred_is_raw)
+             if (v->acpred_is_raw)
                  v->s.ac_pred = get_bits1(&v->s.gb);
              else
                  v->s.ac_pred = v->acpred_plane[mb_pos];
                  vc1_decode_i_block_adv(v, block[k], k, val,
                                         (k < 4) ? v->codingset : v->codingset2, mquant);
  
 -                if (k > 3 && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
 +                if (CONFIG_GRAY && k > 3 && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
                      continue;
                  v->vc1dsp.vc1_inv_trans_8x8(block[k]);
              }
      /* raw bottom MB row */
      s->mb_x = 0;
      init_block_index(v);
 -
 -    for (;s->mb_x < s->mb_width; s->mb_x++) {
 +    for (; s->mb_x < s->mb_width; s->mb_x++) {
          ff_update_block_index(s);
          vc1_put_signed_blocks_clamped(v);
          if (v->s.loop_filter)
              ff_vc1_loop_filter_iblk_delayed(v, v->pq);
      }
      if (v->s.loop_filter)
 -        ff_mpeg_draw_horiz_band(s, (s->end_mb_y-1)*16, 16);
 +        ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
      ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
                      (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
  }
@@@ -2822,8 -2914,7 +2822,8 @@@ static void vc1_decode_p_blocks(VC1Cont
          memmove(v->ttblk_base,    v->ttblk,    sizeof(v->ttblk_base[0])    * s->mb_stride);
          memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
          memmove(v->luma_mv_base,  v->luma_mv,  sizeof(v->luma_mv_base[0])  * s->mb_stride);
 -        if (s->mb_y != s->start_mb_y) ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
 +        if (s->mb_y != s->start_mb_y)
 +            ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
          s->first_slice_line = 0;
      }
      if (apply_loop_filter) {