Skip to content

Compress

dnjulek edited this page Jun 23, 2026 · 1 revision

Adds MPEG-2 / JPEG compression artifacts to 8-bit Gray and YUV clips: 8x8 blocking, ringing and quantization banding — the look of a heavily re-encoded I-frame or a low-quality JPEG.

It does not encode a real bitstream. For every 8x8 block of every processed plane it runs the genuine FFmpeg intra pipeline:

pixels -> forward DCT -> quantize -> dequantize -> inverse DCT -> pixels

The whole degradation comes from the quantization round-trip, exactly as it would for an MPEG-2 I-frame (codec=0) or a baseline JPEG (codec=1). The DSP routines are ported bit-faithfully from libavcodec:

stage FFmpeg source
forward DCT ff_jpeg_fdct_islow_8 (jfdctint_template.c)
quantize dct_quantize_c intra + ff_convert_matrix
dequantize dct_unquantize_mpeg2_intra_c
inverse DCT ff_simple_idct 8-bit (simple_idct_template.c)

Usage

vszip.Compress(vnode clip[, int codec=0, int qscale=8, int quality=50, int dc_prec=0, bool chroma=True])

Parameters:

  • clip:
    8-bit integer Gray or YUV clip. Any other format is rejected.
  • codec: (Default: 0)
    0 = MPEG-2, 1 = JPEG.
  • qscale: (Default: 8)
    MPEG-2 quantizer scale 1..31 (higher → coarser → more artifacts). Ignored when codec=1.
  • quality: (Default: 50)
    JPEG quality 1..100 (lower → coarser → more artifacts). Ignored when codec=0.
  • dc_prec: (Default: 0)
    MPEG-2 intra_dc_precision 0..3 (higher → finer DC, less banding). Ignored when codec=1.
  • chroma: (Default: True)
    YUV only: False leaves the chroma planes untouched (luma-only artifacts).

Examples

# MPEG-2 (default). qscale 1..31, higher = more artifacts.
clip = core.vszip.Compress(clip, codec=0, qscale=8)

# JPEG. quality 1..100, lower = more artifacts.
clip = core.vszip.Compress(clip, codec=1, quality=25)

Clone this wiki locally