{"record":{"id":"989bdf2bb2a263be","repo":"stride3d/stride","slug":"couldn-t-allocate-a-frame-for-hardware-decoding","errorCode":null,"errorMessage":"Couldn't allocate a frame for hardware decoding.","messagePattern":"Couldn't allocate a frame for hardware decoding\\.","errorType":"exception","errorClass":"ApplicationException","httpStatus":null,"severity":"critical","filePath":"sources/engine/Stride.Video/FFmpeg/FFmpegMedia.cs","lineNumber":165,"sourceCode":"\n            AVFormatContext = pFormatContext;\n            Duration = TimeSpan.FromSeconds((double)AVFormatContext->duration / ffmpeg.AV_TIME_BASE);\n            Url = url;\n\n            // Get the streams\n            for (int i = 0; i < pFormatContext->nb_streams; i++)\n            {\n                var stream = FFmpegStream.Create(pFormatContext->streams[i], this);\n                streams.Add(stream);\n            }\n\n            pDecodedFrame = ffmpeg.av_frame_alloc();\n            if (pDecodedFrame == null)\n                throw new ApplicationException(\"Couldn't allocate a frame for decoding.\");\n\n            pCpuCopyFrame = ffmpeg.av_frame_alloc();\n            if (pCpuCopyFrame == null)\n                throw new ApplicationException(\"Couldn't allocate a frame for hardware decoding.\");\n\n            // dispose cached video image from previous file\n            ClearStreamInfo();\n        }\n\n        public bool SeekToTime([NotNull] FFmpegStream stream, long timestamp)\n        {\n            FFmpegUtils.EnsurePlatformSupport();\n\n            if (currentStreams.TryGetValue(stream, out var streamInfo))\n            {\n                // flush the codec buffered images\n                streamInfo.Codec.Flush(pDecodedFrame);\n            }\n\n            // flush the format buffered images\n            ffmpeg.avformat_flush(AVFormatContext);\n","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Video/FFmpeg/FFmpegMedia.cs#L147-L183","documentation":"FFmpegMedia.Open also allocates pCpuCopyFrame, used as the CPU-side target when frames come from GPU (hardware) decoding. This error is thrown when that second av_frame_alloc() returns null — same allocation failure mode but for the hardware-decode copy buffer.","triggerScenarios":"Calling Open() on a media file when the process cannot allocate the second AVFrame (out of memory); commonly occurs when hardware decoding paths (e.g. D3D11VA) are exercised and an extra CPU copy frame must exist.","commonSituations":"Memory pressure while playing videos with hardware decoding; fragmented/native heap exhaustion in long-running game processes; opening many FFmpegMedia instances simultaneously.","solutions":["Free memory or reduce concurrently open videos before calling Open","Verify native FFmpeg binaries are intact and process is 64-bit","Avoid unnecessary hardware-decoding setups if CPU copy frames cannot be allocated","Wrap Open in try/catch for ApplicationException/OutOfMemoryException and retry once after cleanup"],"exampleFix":"// before\nmedia.Open(url);\n// after\ntry { media.Open(url); }\ncatch (ApplicationException ex) when (ex.Message.Contains(\"hardware decoding\"))\n{\n    GC.Collect();\n    media.Open(url); // retry after reclaiming memory\n}","handlingStrategy":"try-catch","validationCode":"FFmpegUtils.EnsurePlatformSupport();\nGC.Collect(); // reduce memory pressure before opening large videos","typeGuard":null,"tryCatchPattern":"try { media.Open(url); }\ncatch (ApplicationException ex) when (ex.Message.Contains(\"hardware decoding\"))\n{\n    // free memory, optionally disable hw decoding, retry once\n}","preventionTips":["Limit concurrently open videos","Keep native FFmpeg binaries intact and matched","Monitor native memory in long-running sessions"],"tags":["ffmpeg","memory-allocation","hardware-decoding","video"],"backgroundTag":"module-init-failed","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}