Most online AI image and video upscalers follow the same pattern: upload a 200 MB file to a cloud bucket, wait in a queue, pay per credit, and download a watermarked result.
When building Upsampler — a free, browser-local 2x AI upscaler for photos, anime art, and MP4 videos—I wanted to see how far modern browser APIs could go if zero bytes ever left the user's machine. No server GPU bills, no upload latency, and complete privacy for personal photos or NDA client footage.
It turns out desktop browsers can now handle real neural 2x super-resolution on both images and MP4 video streams natively—if you get WebGPU texture handoffs and WebCodecs backpressure right. Here are the architecture notes and hard-won lessons from wiring this pipeline together.
High-Level Architecture
For static images, running a lightweight CNN on WebGPU is straightforward: upload the bitmap to a GPU texture, dispatch the layers, and read back or export from the canvas.
Video is where things get interesting. Decoding a 1080p MP4 into RAM all at once will crash a browser tab in seconds. Instead, the MP4 video upscaling export runs inside a Web Worker with an OffscreenCanvas, structured as a streaming pipeline:
MP4 File │ ├──(Video Track)──► web-demuxer (WASM) │ │ EncodedVideoChunk │ ▼ │ DemuxerTrackingStream (highWaterMark: 20) │ │ │ ▼ │ VideoDecoderStream (WebCodecs, cap decodeQueueSize < 18) │ │ VideoFrame │ ▼ │ VideoUpscaleStream (WebGPU CNN -> OffscreenCanvas, highWaterMark: 5) │ │ 2x VideoFrame (close original immediately!) │ ▼ │ VideoEncoderStream (WebCodecs H.264, cap encodeQueueSize < 18) │ │ EncodedVideoChunk │ ▼ └──(Audio Track)──► mp4-muxer (lossless EncodedAudioChunk pass-through) │ ▼ 2x Upscaled MP4 1. WebGPU Neural Inference Without Heavy ML Runtimes
Instead of shipping a massive general-purpose ML runtime bundle, the upscaling engine compiles compact Anime4K-style CNN graphs directly onto WebGPU:
- Compute passes for feature extraction: Intermediate
conv2dand concatenation layers run as WebGPU compute shaders backed byGPUBufferUsage.STORAGEbuffers and reusableGPUTextureallocations (TEXTURE_BINDING | COPY_DST | RENDER_ATTACHMENT). - Direct canvas reconstruction: The final display layer writes straight to
GPUCanvasContext.getCurrentTexture()usingnavigator.gpu.getPreferredCanvasFormat(), avoiding expensive GPU-to-CPU readbacks during live preview. - Content-specialized weights: Shipping 9 compact weight profiles (Photo, Anime, and 3D × 3 network depths) lets users balance throughput vs. fine detail on integrated vs. discrete GPUs.
- Real-time video before/after preview: Before committing to a full export,
HTMLVideoElement.requestVideoFrameCallback()drivesfeedForward()per presented frame. That makes it possible to drag a split-screen comparison slider or inspect pixels through a 2x loupe while the video plays.
2. Backpressure Is Everything in WebCodecs + WebGPU
The single biggest stability issue in early prototypes was unbounded queue growth. Hardware VideoDecoder instances are fast—often much faster than a multi-pass neural shader at 4K output resolution. If you blindly feed chunks into decoder.decode(), decoded VideoFrame objects pile up in GPU/shared memory until the renderer tab freezes or gets killed by the OS.
Pairing TransformStream highWaterMark limits with explicit WebCodecs queue checks fixed this completely:
// Inside VideoDecoderStream.transform() while (decoder.decodeQueueSize >= 18) { await sleep(10); } while (controller.desiredSize !== null && controller.desiredSize < 0) { await sleep(10); } decoder.decode(item.chunk); We apply the same pattern on the encode side (encoder.encodeQueueSize >= 18) and keep the GPU upscale stage intentionally tight (highWaterMark: 5). That creates natural end-to-end backpressure all the way back to the WASM demuxer.
3. Never Forget VideoFrame.close() (Especially on Cancel)
WebCodecs VideoFrame objects hold native media/GPU memory that JavaScript garbage collection will not reclaim quickly enough under load.
Two rules saved us from intermittent tab crashes on longer clips:
- Close immediately after handoff: In
VideoUpscaleStream, once the WebGPU pass flushes to theOffscreenCanvasand we construct the new 2xVideoFrame, immediately callframe.close()on the original decoded frame. InVideoEncoderStream, callitem.frame.close()right afterencoder.encode(). - Close inside cancellation paths: When a user cancels an export halfway through, make sure every in-flight
VideoFrameis explicitly closed before terminating the stream controller.
const upscaledFrame = new VideoFrame(upscaledCanvas, { timestamp: frame.timestamp, duration: frame.duration ?? undefined, alpha: "discard", }); frame.close(); controller.enqueue({ frame: upscaledFrame, index }); Setting alpha: "discard" also avoids unnecessary alpha-channel overhead when feeding frames into an H.264 VideoEncoder.
4. Preserving Original Audio Without Re-Encoding
Re-encoding audio in the browser is slow and introduces generation loss. Because web-demuxer exposes raw EncodedAudioChunk streams and mp4-muxer accepts them directly, we simply mux the original AAC audio chunks straight into the output container after the video pass finishes:
- Use
firstTimestampBehavior: "offset"andfastStart: "in-memory"inmp4-muxerso the resulting MP4 starts smoothly and is web-streamable. - Guard against negative timestamps (
if (chunk.timestamp >= 0)) that frequently show up in FFmpeg-trimmed clips. - Scale the H.264 target bitrate proportionally to the 4x pixel area increase (2x width × 2x height) and force a keyframe every 60 frames (
keyFrame: index % 60 === 0) for clean seeking.
Try It & Trade Notes
If you have a WebGPU-capable desktop browser (Chrome, Edge, or Safari 26+), you can test the pipeline directly on Upsampler—drop in a photo, anime screenshot, or short MP4 clip and watch it upscale locally on your GPU without uploading anything.
If you're also building browser-local media pipelines with WebGPU and WebCodecs, I'd love to compare notes in the comments:
- Are you using
importExternalTexturefor zero-copyVideoFrameingestion across Safari/Chromium? - What bitrate heuristics and codec profiles have given you the best compatibility across macOS QuickTime and Windows Media Player?

