High-performance video decoding on Apple Silicon (M1) using VideoToolbox and Metal
devFor users running QtScrcpy on macOS with Apple Silicon (M1/M2/M3), the recommended high-performance architecture (Scheme C) utilizes VideoToolbox for hardware-accelerated H.264 decoding and Metal for zero-copy GPU rendering. This approach significantly reduces CPU usage (from ~68% down to ~5-8%) by avoiding FFmpeg software decoding and bypassing the OpenGL-to-Metal translation layer.
Key Performance Benefits:
- Hardware Decoding: Uses the M1 Media Engine via
VTDecompressionSession, resulting in ~0% CPU decoding load. - Zero-Copy Data Flow: Leverages Unified Memory Architecture (UMA) by passing
CVPixelBufferRef(IOSurface) directly from the decoder to Metal textures, eliminating CPU-to-GPU memory copies. - Native Rendering: Uses Metal shaders to convert NV12 YUV data to BT.709 RGB, avoiding the slow paths associated with
GL_LUMINANCEin OpenGL.
Architecture Overview
The data flow follows this pipeline:
- Demuxer Thread: Receives
AVPacket$\rightarrow$VTDecoder::decode$\rightarrow$ createsCMSampleBuffer$\rightarrow$ hardware decodes viaVTDecompressionSessionDecodeFrame$\rightarrow$ returnsCVPixelBufferRef. - Main (GUI) Thread: Receives
CVPixelBufferRef$\rightarrow$CVMetalTextureCacheCreateTextureFromImage(creates zero-copyMTLTexture) $\rightarrow$MTLCommandBufferexecutes shaders $\rightarrow$ renders to aQWidgetcontainer.