PR #26353 opened by sjpotter
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26353
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26353.patch

VC-1 AC coefficients were decoded in several steps: a VLC lookup gave a table 
index, then the run, the level and the last flag came from three more tables, 
with a branch for the escape code in the common path. In a single-thread 
profile of progressive Blu-ray content on an Apple M3 Max, 
`vc1_decode_ac_coeff()` alone took about a quarter of the decode time. This is 
plain C, so it applies to every architecture, and it covers WMV3 and MSS2 too, 
since they share `vc1_block.c`.

The series has two commits:

1. **Run-level VLC.** At static init, the existing AC VLC tables are converted 
in place into `RL_VLC_ELEM` tables (run, level, and the last flag in bit 7 of 
run, plus reserved run values for the escape code and invalid codes). 
`vc1_decode_ac_coeff()` then decodes them with `GET_RL_VLC`, as mpeg1/2, mpeg4, 
h263, msmpeg4 and wmv2 already do. The common path is inline and reads the sign 
bit while the bit reader is still open; escape codes go to a separate 
out-of-line function, which is otherwise unchanged. The conversion runs inside 
`vc1_init_static()`, which already runs once under `ff_thread_once()`, so it 
needs no new tables, allocations or locking.
2. **Simpler dequantization and scan table selection.** AC dequantization 
becomes one expression: the scaled value plus a select of the offset, where the 
offset is 0 for the uniform quantizer. The intra dequantization loop has no 
data-dependent branches left and can be vectorized. In `vc1_decode_p_block()` 
the scan table is picked once per transform type instead of testing `fcm` for 
every coefficient.

Neither commit changes the output.

The code was written with AI assistance and tested as described below.

### Why

These changes came out of testing the VC-1 frame-threading PR 
([#26273](https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26273)) on a low-power 
ARM TV. Even with 3 or 4 frame threads that TV can't decode some VC-1 Blu-ray 
content in real time (1080i interlaced reached about 0.83-0.91x real time with 
4 threads), so threading alone isn't enough there and the per-thread cost of 
the decoder has to come down too. This series doesn't depend on #26273 and 
applies to master on its own; all numbers below were measured on master. The 
test builds (both master and patched) also had the VC-1 parser fix from the 
separate parser PR applied, because without it some of the interlaced clips 
don't decode correctly on master; it doesn't touch block decoding.

The single-thread profiling that found this overhead was done on an Apple M3 
Max, and the change was then tested on the TV to see whether the gain carries 
over. The two are very different machines (a wide out-of-order core with large 
caches, against an in-order Cortex-A53 with 32 KB of L1 data cache per core and 
a 512 KB L2 shared by all four cores), so a change that looks good on the Mac 
can gain less on the TV, or more. Both sets of numbers are given below. For 
this series the gains carried over.

### Apple M3 Max (aarch64), single thread

Decode-only (`-threads 1`, `-f null`, first 800 frames), CPU cycles and 
instructions from `/usr/bin/time -l`, best of 3 interleaved runs, all three 
builds in one session. Speedup is base cycles / patched cycles; instructions 
are relative to master.

| clip | commit 1 speedup | commit 1+2 speedup | commit 1+2 instructions |
|---|---:|---:|---:|
| progressive 1080p (DarkKnight) | 1.077x | 1.169x | 0.852 |
| progressive 1080p (Contact) | 1.078x | 1.126x | 0.880 |
| progressive 1080p (HP1) | 1.043x | 1.103x | 0.868 |
| progressive 1080p (HumanPlanet) | 1.073x | 1.200x | 0.810 |
| interlaced 1080i (Diana) | 1.025x | 1.074x | 0.935 |
| interlaced 1080i (NIN peak) | 1.012x | 1.089x | 0.951 |
| interlaced 1080i (NIN mid) | 1.036x | 1.022x | 0.976 |
| interlaced 1080i (PlanetEarth peak) | 1.041x | 1.076x | 0.899 |
| interlaced 1080i (PlanetEarth mid) | 1.032x | 1.089x | 0.912 |

Progressive gets +10% to +20%, interlaced +2% to +9%. Interlaced gains less 
because motion compensation and the loop filter take a larger share there. 
Cycles vary by about ±2-4% between sessions on this machine (an earlier session 
of the same comparison gave DarkKnight 1.167x and NIN mid 1.019x), so 
differences of a couple of percent between clips are not meaningful.

### Sony BRAVIA TV (Cortex-A53, 32-bit), single thread

MediaTek MT5891, 4x Cortex-A53 @ 1.0 GHz, 32-bit Android 9, NDK clang, armv7-a 
with NEON. Decode-only (`-threads 1`, `-f null`, whole clip, 2156-2696 frames), 
median of 3 runs. CPU time is user+sys.

| clip | master fps | patched fps | fps | CPU time |
|---|---:|---:|---:|---:|
| progressive 1080p (DarkKnight) | 11.40 | 13.33 | +16.9% | -14.2% |
| progressive 1080p (Contact) | 11.81 | 13.59 | +15.1% | -12.8% |
| progressive 1080p (HP1) | 12.02 | 13.85 | +15.2% | -13.0% |
| progressive 1080p (HumanPlanet) | 10.63 | 13.07 | +23.0% | -18.3% |
| interlaced 1080i (Diana) | 8.48 | 9.06 | +6.8% | -6.4% |
| interlaced 1080i (NIN peak) | 9.82 | 10.27 | +4.6% | -4.2% |
| interlaced 1080i (NIN mid) | 9.52 | 9.83 | +3.3% | -3.1% |
| interlaced 1080i (PlanetEarth peak) | 6.74 | 7.60 | +12.8% | -11.2% |
| interlaced 1080i (PlanetEarth mid) | 7.14 | 7.88 | +10.4% | -9.4% |

Run-to-run spread on the TV was under 1%, so every clip here is a real gain. 
The TV gains are about the same as the Mac ones on progressive content and 
larger on the two PlanetEarth interlaced clips. Only the full series was 
measured on the TV, not commit 1 alone.

The clips are commercial and can't be shared.

### Testing

- `make fate-vc1 fate-wmv3-drm fate-mss2 fate-checkasm-vc1dsp` with a full 
macOS (aarch64) build, passing after each of the two commits. fate-vc1 includes 
the WMV3 `.rcv` samples, and the MSS2 tests go through the same block decoding.
- Decoded 31 VC-1 Blu-ray clips (progressive and interlaced, including the 9 
above) on macOS with `-threads 1 -f framemd5`: bit-exact against reference 
decodes from an unpatched decoder. The 9 benchmark clips were also checked 
after commit 1 alone.
- Decoded the 9 benchmark clips on the TV with `-threads 1 -f framemd5` using 
an armv7-a build of this branch: bit-exact against the same reference decodes. 
This also exercises `GET_RL_VLC` with the 32-bit bit reader cache.
- Builds cleanly for macOS (aarch64) and with the Android NDK for armv7-a and 
aarch64.



From 36ec8b71c3733e9f0f35cf70cb8f413785b1395f Mon Sep 17 00:00:00 2001
From: Shaya Potter <[email protected]>
Date: Wed, 25 Nov 2026 10:59:01 +0200
Subject: [PATCH 1/2] avcodec/vc1_block: decode AC coefficients with a
 run-level VLC

vc1_decode_ac_coeff() decoded a VLC index and then looked up the run,
the level and the last flag in three more tables, branching on the
escape code in the common path. In a single-thread profile on an
Apple M3 Max it was about a quarter of the decode time on progressive
content.

Convert the AC VLC tables at static init into RL_VLC_ELEM tables
holding the run, the level and the last flag (bit 7 of run), with
reserved run values for the escape code and invalid codes, and decode
them with GET_RL_VLC like the other mpegvideo-style decoders do. The
common path is now inline and keeps the bit reader open for the sign
bit; escape codes go to a separate out of line function.

The tables are converted in place inside vc1_init_static(), which runs
once under ff_thread_once(), so no new tables or locking are needed.
The output is unchanged.
---
 libavcodec/vc1_block.c | 127 ++++++++++++++++++++++++-----------------
 libavcodec/vc1data.c   |   2 +-
 libavcodec/vc1data.h   |   6 +-
 libavcodec/vc1dec.c    |  35 ++++++++++++
 4 files changed, 116 insertions(+), 54 deletions(-)

diff --git a/libavcodec/vc1_block.c b/libavcodec/vc1_block.c
index a84c853b6f1..706e14ac863 100644
--- a/libavcodec/vc1_block.c
+++ b/libavcodec/vc1_block.c
@@ -502,6 +502,63 @@ static inline int vc1_coded_block_pred(MPVContext *const 
s, int n, int diff)
     return pred ^ diff;
 }
 
+/**
+ * Decode the rest of an escape coded AC coefficient
+ * @see vc1_decode_ac_coeff()
+ */
+static av_noinline int vc1_decode_ac_coeff_escape(VC1Context *v, int *last, 
int *skip,
+                                                  int *value, int codingset)
+{
+    GetBitContext *const gb = &v->gb;
+    int escape = decode210(gb);
+    int run, level, lst, sign;
+
+    if (escape != 2) {
+        OPEN_READER(re, gb);
+        UPDATE_CACHE(re, gb);
+        GET_RL_VLC(level, run, re, gb, ff_vc1_ac_coeff_table[codingset],
+                   AC_VLC_BITS, 3, 1);
+        CLOSE_READER(re, gb);
+        if (run >= VC1_AC_RUN_INVALID)
+            return AVERROR_INVALIDDATA;
+        lst  = run >> 7;
+        run &= 0x7F;
+        if (escape == 0) {
+            if (lst)
+                level += vc1_last_delta_level_table[codingset][run];
+            else
+                level += vc1_delta_level_table[codingset][run];
+        } else {
+            if (lst)
+                run += vc1_last_delta_run_table[codingset][level] + 1;
+            else
+                run += vc1_delta_run_table[codingset][level] + 1;
+        }
+        sign = get_bits1(gb);
+    } else {
+        lst = get_bits1(gb);
+        if (v->esc3_level_length == 0) {
+            if (v->pq < 8 || v->dquantfrm) { // table 59
+                v->esc3_level_length = get_bits(gb, 3);
+                if (!v->esc3_level_length)
+                    v->esc3_level_length = get_bits(gb, 2) + 8;
+            } else { // table 60
+                v->esc3_level_length = get_unary(gb, 1, 6) + 2;
+            }
+            v->esc3_run_length = 3 + get_bits(gb, 2);
+        }
+        run   = get_bits(gb, v->esc3_run_length);
+        sign  = get_bits1(gb);
+        level = get_bits(gb, v->esc3_level_length);
+    }
+
+    *last  = lst;
+    *skip  = run;
+    *value = (level ^ -sign) + sign;
+
+    return 0;
+}
+
 /**
  * Decode one AC coefficient
  * @param v The VC1 context
@@ -511,62 +568,28 @@ static inline int vc1_coded_block_pred(MPVContext *const 
s, int n, int diff)
  * @param codingset set of VLC to decode data
  * @see 8.1.3.4
  */
-static int vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
-                                int *value, int codingset)
+static av_always_inline int vc1_decode_ac_coeff(VC1Context *v, int *last, int 
*skip,
+                                                int *value, int codingset)
 {
     GetBitContext *const gb = &v->gb;
-    int index, run, level, lst, sign;
+    int level, run, sign;
 
-    index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset], AC_VLC_BITS, 3);
-    if (index < 0)
-        return index;
-    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;
-        sign  = get_bits1(gb);
-    } else {
-        int escape = decode210(gb);
-        if (escape != 2) {
-            index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset], 
AC_VLC_BITS, 3);
-            if (index >= ff_vc1_ac_sizes[codingset] - 1U)
-                return AVERROR_INVALIDDATA;
-            run   = vc1_index_decode_table[codingset][index][0];
-            level = vc1_index_decode_table[codingset][index][1];
-            lst   = index >= vc1_last_decode_table[codingset];
-            if (escape == 0) {
-                if (lst)
-                    level += vc1_last_delta_level_table[codingset][run];
-                else
-                    level += vc1_delta_level_table[codingset][run];
-            } else {
-                if (lst)
-                    run += vc1_last_delta_run_table[codingset][level] + 1;
-                else
-                    run += vc1_delta_run_table[codingset][level] + 1;
-            }
-            sign = get_bits1(gb);
-        } else {
-            lst = get_bits1(gb);
-            if (v->esc3_level_length == 0) {
-                if (v->pq < 8 || v->dquantfrm) { // table 59
-                    v->esc3_level_length = get_bits(gb, 3);
-                    if (!v->esc3_level_length)
-                        v->esc3_level_length = get_bits(gb, 2) + 8;
-                } else { // table 60
-                    v->esc3_level_length = get_unary(gb, 1, 6) + 2;
-                }
-                v->esc3_run_length = 3 + get_bits(gb, 2);
-            }
-            run   = get_bits(gb, v->esc3_run_length);
-            sign  = get_bits1(gb);
-            level = get_bits(gb, v->esc3_level_length);
-        }
+    OPEN_READER(re, gb);
+    UPDATE_CACHE(re, gb);
+    GET_RL_VLC(level, run, re, gb, ff_vc1_ac_coeff_table[codingset],
+               AC_VLC_BITS, 3, 1);
+    if (run >= VC1_AC_RUN_INVALID) {
+        CLOSE_READER(re, gb);
+        if (run == VC1_AC_RUN_INVALID)
+            return AVERROR_INVALIDDATA;
+        return vc1_decode_ac_coeff_escape(v, last, skip, value, codingset);
     }
-
-    *last  = lst;
-    *skip  = run;
-    *value = (level ^ -sign) + sign;
+    *last = run >> 7 | (BITS_LEFT(re, gb) < 0);
+    *skip = run & 0x7F;
+    sign  = SHOW_SBITS(re, gb, 1);
+    LAST_SKIP_BITS(re, gb, 1);
+    CLOSE_READER(re, gb);
+    *value = (level ^ sign) - sign;
 
     return 0;
 }
diff --git a/libavcodec/vc1data.c b/libavcodec/vc1data.c
index 4046ea042fc..214b0495ce9 100644
--- a/libavcodec/vc1data.c
+++ b/libavcodec/vc1data.c
@@ -121,7 +121,7 @@ const VLCElem *ff_vc1_if_1mv_mbmode_vlc[8];
 const VLCElem *ff_vc1_1ref_mvdata_vlc[4];
 const VLCElem *ff_vc1_2ref_mvdata_vlc[8];
 
-const VLCElem *ff_vc1_ac_coeff_table[8];
+const RL_VLC_ELEM *ff_vc1_ac_coeff_table[8];
 
 //@}
 
diff --git a/libavcodec/vc1data.h b/libavcodec/vc1data.h
index e71593d023f..fa3e01dfd48 100644
--- a/libavcodec/vc1data.h
+++ b/libavcodec/vc1data.h
@@ -91,7 +91,11 @@ extern const VLCElem *ff_vc1_1ref_mvdata_vlc[4];
 #define VC1_2REF_MVDATA_VLC_BITS 9
 extern const VLCElem *ff_vc1_2ref_mvdata_vlc[8];
 
-extern const VLCElem *ff_vc1_ac_coeff_table[8];
+/* AC coefficient tables for GET_RL_VLC: level is the unsigned level and
+ * bit 7 of run is set for the last coefficient of a block. */
+#define VC1_AC_RUN_INVALID 0xFE
+#define VC1_AC_RUN_ESCAPE  0xFF
+extern const RL_VLC_ELEM *ff_vc1_ac_coeff_table[8];
 
 #define VC1_IF_MBMODE_VLC_BITS 5    // as a placeholder for 
VC1_IF_MMV_MBMODE_VLC_BITS
                                     // or VC1_IF_1MV_MBMODE_VLC_BITS since 
they are the same
diff --git a/libavcodec/vc1dec.c b/libavcodec/vc1dec.c
index a4d465dd0b3..5715089e736 100644
--- a/libavcodec/vc1dec.c
+++ b/libavcodec/vc1dec.c
@@ -43,6 +43,7 @@
 #include "profiles.h"
 #include "simple_idct.h"
 #include "vc1.h"
+#include "vc1acdata.h"
 #include "vc1data.h"
 #include "vc1_vlc_data.h"
 #include "libavutil/attributes.h"
@@ -491,6 +492,38 @@ av_cold void ff_vc1_init_transposed_scantables(VC1Context 
*v)
     v->top_blk_sh  = 3;
 }
 
+/**
+ * Convert an AC coefficient VLC table in place for use with GET_RL_VLC.
+ */
+static av_cold void vc1_init_ac_rl_vlc(RL_VLC_ELEM *table, int size, int 
codingset)
+{
+    const int escape = ff_vc1_ac_sizes[codingset] - 1;
+
+    for (int i = 0; i < size; i++) {
+        int sym = table[i].sym, len = table[i].len;
+        int level, run;
+
+        if (len == 0) {         // invalid code
+            level = 0;
+            run   = VC1_AC_RUN_INVALID;
+        } else if (len < 0) {   // subtable, sym is its offset
+            level = sym;
+            run   = 0;
+        } else if (sym == escape) {
+            level = 0;
+            run   = VC1_AC_RUN_ESCAPE;
+        } else {
+            level = vc1_index_decode_table[codingset][sym][1];
+            run   = vc1_index_decode_table[codingset][sym][0];
+            if (sym >= vc1_last_decode_table[codingset])
+                run |= 0x80;
+        }
+        table[i].level = level;
+        table[i].len8  = len;
+        table[i].run   = run;
+    }
+}
+
 static av_cold void vc1_init_static(void)
 {
     static VLCElem vlc_table[32372];
@@ -554,10 +587,12 @@ static av_cold void vc1_init_static(void)
                                vc1_2mv_block_pattern_codes[i], 1, 1, 0);
     }
     for (int i = 0; i < 8; i++) {
+        RL_VLC_ELEM *ac_table = state.table;
         ff_vc1_ac_coeff_table[i] =
             ff_vlc_init_tables(&state, AC_VLC_BITS, ff_vc1_ac_sizes[i],
                                &vc1_ac_tables[i][0][1], 8, 4,
                                &vc1_ac_tables[i][0][0], 8, 4, 0);
+        vc1_init_ac_rl_vlc(ac_table, state.table - ac_table, i);
         /* initialize interlaced MVDATA tables (2-Ref) */
         ff_vc1_2ref_mvdata_vlc[i] =
             ff_vlc_init_tables(&state, VC1_2REF_MVDATA_VLC_BITS, 126,
-- 
2.54.1


From 2011c0d153e7656deb1b797b7e5cb7b209c04d9c Mon Sep 17 00:00:00 2001
From: Shaya Potter <[email protected]>
Date: Wed, 25 Nov 2026 11:02:27 +0200
Subject: [PATCH 2/2] avcodec/vc1_block: simplify AC dequantization and scan
 table selection

Dequantize AC coefficients with a single expression instead of a
multiply, a store and a conditional read-modify-write of the
coefficient. For the non-uniform quantizer the offset is added with a
select, and for the uniform quantizer the offset is zero, so the
pquantizer test moves out of the loops. In the intra block functions
the dequantization loop has no data-dependent branches left and can
be vectorized.

In vc1_decode_p_block(), pick the scan table once per transform type
instead of testing fcm for every coefficient.

The output is unchanged.
---
 libavcodec/vc1_block.c | 78 +++++++++++++++++-------------------------
 1 file changed, 32 insertions(+), 46 deletions(-)

diff --git a/libavcodec/vc1_block.c b/libavcodec/vc1_block.c
index 706e14ac863..bd8a6f7ea51 100644
--- a/libavcodec/vc1_block.c
+++ b/libavcodec/vc1_block.c
@@ -594,6 +594,20 @@ static av_always_inline int vc1_decode_ac_coeff(VC1Context 
*v, int *last, int *s
     return 0;
 }
 
+/* quant is 0 for the uniform quantizer */
+static av_always_inline int vc1_scale_ac_coeff(int value, int scale, int quant)
+{
+    int16_t t = value * scale;
+    return t + (t < 0 ? -quant : quant);
+}
+
+/* Dequantize AC coefficients 1..63, leaving zero coefficients alone */
+static av_always_inline void vc1_scale_ac(int16_t block[64], int scale, int 
quant)
+{
+    for (int k = 1; k < 64; k++)
+        block[k] = block[k] ? vc1_scale_ac_coeff(block[k], scale, quant) : 0;
+}
+
 /** Decode intra block in intra frames - should be faster than 
decode_intra_block
  * @param v VC1Context
  * @param block block to decode
@@ -686,12 +700,7 @@ static int vc1_decode_i_block(VC1Context *v, int16_t 
block[64], int n,
         }
 
         /* scale AC coeffs */
-        for (k = 1; k < 64; k++)
-            if (block[k]) {
-                block[k] *= scale;
-                if (!v->pquantizer)
-                    block[k] += (block[k] < 0) ? -v->pq : v->pq;
-            }
+        vc1_scale_ac(block, scale, v->pquantizer ? 0 : v->pq);
 
     } else {
         int k;
@@ -857,12 +866,7 @@ static int vc1_decode_i_block_adv(VC1Context *v, int16_t 
block[64], int n,
         }
 
         /* scale AC coeffs */
-        for (k = 1; k < 64; k++)
-            if (block[k]) {
-                block[k] *= scale;
-                if (!v->pquantizer)
-                    block[k] += (block[k] < 0) ? -quant : quant;
-            }
+        vc1_scale_ac(block, scale, v->pquantizer ? 0 : quant);
 
     } else { // no AC coeffs
         int k;
@@ -1039,12 +1043,7 @@ static int vc1_decode_intra_block(VC1Context *v, int16_t 
block[64], int n,
         }
 
         /* scale AC coeffs */
-        for (k = 1; k < 64; k++)
-            if (block[k]) {
-                block[k] *= scale;
-                if (!v->pquantizer)
-                    block[k] += (block[k] < 0) ? -quant : quant;
-            }
+        vc1_scale_ac(block, scale, v->pquantizer ? 0 : quant);
     } else { // no AC coeffs
         int k;
 
@@ -1113,6 +1112,8 @@ static int vc1_decode_p_block(VC1Context *v, int16_t 
block[64], int n,
     int ttblk = ttmb & 7;
     int pat = 0;
     int quant = FFABS(mquant);
+    int dq;
+    const uint8_t *zz;
 
     s->bdsp.clear_block(block);
 
@@ -1134,6 +1135,7 @@ static int vc1_decode_p_block(VC1Context *v, int16_t 
block[64], int n,
             ttblk = TT_4X8;
     }
     scale = quant * 2 + ((mquant < 0) ? 0 : v->halfpq);
+    dq    = v->pquantizer ? 0 : quant;
 
     // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
     if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
@@ -1149,6 +1151,7 @@ static int vc1_decode_p_block(VC1Context *v, int16_t 
block[64], int n,
         pat  = 0xF;
         i    = 0;
         last = 0;
+        zz   = !v->fcm ? v->zz_8x8[0] : v->zzi_8x8;
         while (!last) {
             int ret = vc1_decode_ac_coeff(v, &last, &skip, &value, 
v->codingset2);
             if (ret < 0)
@@ -1156,13 +1159,8 @@ static int vc1_decode_p_block(VC1Context *v, int16_t 
block[64], int n,
             i += skip;
             if (i > 63)
                 break;
-            if (!v->fcm)
-                idx = v->zz_8x8[0][i++];
-            else
-                idx = v->zzi_8x8[i++];
-            block[idx] = value * scale;
-            if (!v->pquantizer)
-                block[idx] += (block[idx] < 0) ? -quant : quant;
+            idx = zz[i++];
+            block[idx] = vc1_scale_ac_coeff(value, scale, dq);
         }
         if (!skip_block) {
             if (i == 1)
@@ -1175,6 +1173,7 @@ static int vc1_decode_p_block(VC1Context *v, int16_t 
block[64], int n,
         break;
     case TT_4X4:
         pat = ~subblkpat & 0xF;
+        zz  = !v->fcm ? ff_vc1_simple_progressive_4x4_zz : 
ff_vc1_adv_interlaced_4x4_zz;
         for (j = 0; j < 4; j++) {
             last = subblkpat & (1 << (3 - j));
             i    = 0;
@@ -1186,13 +1185,8 @@ static int vc1_decode_p_block(VC1Context *v, int16_t 
block[64], int n,
                 i += skip;
                 if (i > 15)
                     break;
-                if (!v->fcm)
-                    idx = ff_vc1_simple_progressive_4x4_zz[i++];
-                else
-                    idx = ff_vc1_adv_interlaced_4x4_zz[i++];
-                block[idx + off] = value * scale;
-                if (!v->pquantizer)
-                    block[idx + off] += (block[idx + off] < 0) ? -quant : 
quant;
+                idx = zz[i++];
+                block[idx + off] = vc1_scale_ac_coeff(value, scale, dq);
             }
             if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
                 if (i == 1)
@@ -1204,6 +1198,7 @@ static int vc1_decode_p_block(VC1Context *v, int16_t 
block[64], int n,
         break;
     case TT_8X4:
         pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
+        zz  = !v->fcm ? v->zz_8x4 : ff_vc1_adv_interlaced_8x4_zz;
         for (j = 0; j < 2; j++) {
             last = subblkpat & (1 << (1 - j));
             i    = 0;
@@ -1215,13 +1210,8 @@ static int vc1_decode_p_block(VC1Context *v, int16_t 
block[64], int n,
                 i += skip;
                 if (i > 31)
                     break;
-                if (!v->fcm)
-                    idx = v->zz_8x4[i++] + off;
-                else
-                    idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
-                block[idx] = value * scale;
-                if (!v->pquantizer)
-                    block[idx] += (block[idx] < 0) ? -quant : quant;
+                idx = zz[i++] + off;
+                block[idx] = vc1_scale_ac_coeff(value, scale, dq);
             }
             if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
                 if (i == 1)
@@ -1233,6 +1223,7 @@ static int vc1_decode_p_block(VC1Context *v, int16_t 
block[64], int n,
         break;
     case TT_4X8:
         pat = ~(subblkpat * 5) & 0xF;
+        zz  = !v->fcm ? v->zz_4x8 : ff_vc1_adv_interlaced_4x8_zz;
         for (j = 0; j < 2; j++) {
             last = subblkpat & (1 << (1 - j));
             i    = 0;
@@ -1244,13 +1235,8 @@ static int vc1_decode_p_block(VC1Context *v, int16_t 
block[64], int n,
                 i += skip;
                 if (i > 31)
                     break;
-                if (!v->fcm)
-                    idx = v->zz_4x8[i++] + off;
-                else
-                    idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
-                block[idx] = value * scale;
-                if (!v->pquantizer)
-                    block[idx] += (block[idx] < 0) ? -quant : quant;
+                idx = zz[i++] + off;
+                block[idx] = vc1_scale_ac_coeff(value, scale, dq);
             }
             if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
                 if (i == 1)
-- 
2.54.1

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to