05Performance Optimisation

FFmpeg libswscale Optimization

#ModelScoreStepsIn tokOut tokAvg costAvg time
1Claude Fable 5.1
0.4013
±0.328
857366.7M1.3M$214.089.9h
2GLM-5.3
0.1309
±0.035
1,449618.7M1.7M$173.6016.0h
3Kimi K3
0.0790
±0.052
1,044437.6M1.2M$155.2319.0h
4DeepSeek V4 Flash Exp
0.0478
±0.028
1,399618.9M1.2M$10.627.3h
5Qwen3.8-Max
0.0122
±0.016
494165.8M2.3M$56.2718.8h
6Grok 4.6
0.0072
±0.016
1,482417.5M1.6M$424.4711.9h
7Gemini 3.7 Flash
0.0031
±0.005
580192.7M881k$25.553.5h
8GPT-5.6
0.0000
±0.000
37498.4M204k$92.642.6h
9Inkling
0.0000
±0.000
21519.4M41k$18.610.6h
10Muse Spark 1.2
0.0000
±0.000
312120.6M519k$21.591.7h

Background

libswscale is FFmpeg's image scaling and pixel-format conversion library: the code that turns YUV into RGB, packs and unpacks planes, and resamples frames, in some of the hottest loops in video processing. Re-implementing it means reproducing the numerics of decades-old scalar C (coefficient tables, rounding, chroma subsampling) closely enough to pass per-plane PSNR bars, across dozens of format pairs and three scaling algorithms, and then making it substantially faster.

The rewrite must be genuinely new work: it is written in Zig behind a small custom C ABI, FFmpeg's own code cannot be wrapped, linked, loaded, or embedded, and inline assembly is banned; the speed has to come from portable SIMD (Zig's @Vector) and better algorithms, not from recalling or repackaging the original.

Task

Build libswscale_candidate.so from the sources in /app/swscale-impl/, exporting the three C-linkage functions in /app/swscale_api.h (swscale_create / swscale_process / swscale_destroy), covering ten pixel formats and three scaling algorithms.

  • The correctness contract is 42 same-size conversions plus 16 scaling configurations (58 in all), checked plane by plane against the FFmpeg scalar reference: PSNR of at least 60 dB for same-size conversion and 40 dB for scaling. All 58 must pass; a library fast on 57 of 58 counts the same as an empty scaffold.
  • The contract fixes format pairs and algorithms, not frame sizes, so the implementation has to generalize rather than special-case the visible workloads.
  • Performance is single-threaded measured work, fully modeled into /app/perf-check; the goal is beating the reference's per-workload numbers.
  • Zig 0.14.0 only, no inline assembly, no dlopen/dlsym, and no FFmpeg code in any form; target x86-64-v3 (AVX2, FMA, BMI2) and no higher, since the measurement simulator has no AVX-512.
  • Changes are confined to /app/swscale-impl/.

What The Agent Gets

A Zig starter scaffold at /app/scaffold/zig/ that already exports the three functions, the ABI header, and /app/libswscale_public_baseline.so: FFmpeg's scalar swscale (built with --disable-asm) behind the same ABI, the reference the output is compared against and whose work is being beaten. /app/perf-check rebuilds, compares output against the unmodified reference, and measures (--contract for the full 58-conversion sweep, --quick to skip measurement, or a named workload); reference numbers are baked in /app/baseline-work.json. The /app/driver C program that loads and exercises a library is usable under gdb/perf, the measurement stack is readable at /app/performance/, and FFmpeg's libswscale/libavutil C sources are at /reference/ffmpeg-src/ for study. The driver synthesizes deterministic source pixels, so there are no image files and every run sees the same input. The machine is offline.

Verification

A clean-room verifier, separate from the agent's environment, first strips every FFmpeg source and archive from the container, rebuilds the submission from source as a non-root user, and runs the full 58-conversion contract plus ten hidden held-out benchmark workloads: the same conversion families and size classes as the public set, at dimensions and format pairs the agent has not seen. Speed is not wall-clock: each workload's work is measured by callgrind-based per-opcode instruction pricing (a deterministic cost model derived from uops.info throughput data), so the score is independent of host speed.

  • Reward is gated on build success, provenance, and full correctness, then scored as 2**u - 1 where u scales the geometric-mean work reduction across held-out workloads, reaching full credit at 20x; matching the baseline or regressing scores zero.
  • Provenance checks reject FFmpeg delegation in any form: linked library names are restricted to a C-runtime allowlist, RPATH/RUNPATH and dynamic-loading imports are rejected, and the built objects are scanned for FFmpeg symbols, build strings, inline assembly, and large binary blobs.
  • The driver installs a seccomp filter that blocks fork/exec before the candidate loads, sources are perturbed between runs to defeat cached outputs, and iteration-identity and work-linearity gates catch skipped work.
  • A crash or hang on a measured workload counts as a correctness failure; if fewer than 75% of workloads could be measured the trial is marked invalid rather than scored.

Environment

Base imageubuntu:24.04
Tools availableZig, FFmpeg n7.1 sources and compiled scalar baseline, C/C++ toolchain, Python 3
Compute8 CPUs · 32 GB RAM
Time limit20h

Changes From V1

This task is an improved revision of FFmpeg libswscale Re-implementation from FrontierSWE V1.

  • The language was narrowed from "Zig or Rust" to Zig 0.14.0 only, making @Vector the portable-SIMD surface under test, with an explicit x86-64-v3 ceiling.
  • The metric moved from wall-clock benchmark timing to deterministic per-opcode instruction pricing (callgrind-based, uops.info-derived), and the reward is now normalized to [0,1] behind the correctness gate; V1 reported the raw geometric-mean speedup, where 1.0 meant parity with the baseline.
  • The correctness contract became explicit and agent-checkable: 58 enumerated conversions via perf-check --contract, with held-out sizes still hidden; V1 checked hidden workloads against the same PSNR bars without a visible contract list.
  • Verification moved into a separate clean-room container with a pinned image. V1's verifier ran inside the agent's environment, deleting the reference material in place and importing helper code from /app; the V2 scorer never imports agent code.
  • Anti-delegation was hardened: a seccomp filter blocks fork/exec before the candidate loads, linked libraries are allowlisted, RPATH is banned, binaries are scanned for FFmpeg symbols and blobs, and source perturbation plus work-linearity gates defeat cached or skipped runs.
  • The agent budget grew from 8 to 20 hours, and V1's full ASM-enabled FFmpeg binary (provided for generating golden outputs) is gone; only the C sources and the scalar baseline library remain.

References

  • FFmpeg (LGPL-2.1-or-later (with GPL-2.0-or-later components as configured)): The libswscale/libavutil/compat source subtrees at tag n7.1, cloned at image build (nothing vendored) and baked read-only at /reference/ffmpeg-src/, plus a --disable-asm scalar build wrapped by a task-authored shim into the baseline shared library. No FFmpeg code reaches the driver, measurement, scaffolds, or verifier.
  • uops.info instruction data (Derived aggregate; format via andreas-abel/XED-to-XML (Apache-2.0)): The per-opcode cost table used by the measurement stack is a derived aggregate of reciprocal-throughput measurements from uops.info (Abel & Reineke, ASPLOS 2019), generated once offline; it is not a copy of the dataset.