{"record":{"id":"efaaf2906dcc2fd7","repo":"stride3d/stride","slug":"could-not-initialize-the-conversion-context","errorCode":null,"errorMessage":"Could not initialize the conversion context.","messagePattern":"Could not initialize the conversion context\\.","errorType":"exception","errorClass":"ApplicationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Video/FFmpeg/FFmpegMedia.cs","lineNumber":414,"sourceCode":"                    {\n                        // the frame is coming from the GPU\n                        ret = ffmpeg.av_hwframe_transfer_data(pCpuCopyFrame, pDecodedFrame, 0);\n                        if (ret < 0)\n                            throw new ApplicationException(\"Couldn't transfer frame data from GPU to CPU\");\n\n                        pFrame = pCpuCopyFrame;\n                    }\n                    else\n                    {\n                        pFrame = pDecodedFrame;\n                    }\n\n                    var width = pCodecContext->width;\n                    var height = pCodecContext->height;\n                    var sourcePixFmt = (AVPixelFormat)pFrame->format;\n                    pConvertContext = ffmpeg.sws_getCachedContext(pConvertContext, width, height, sourcePixFmt, width, height, DestinationPixelFormat, ffmpeg.SWS_FAST_BILINEAR, null, null, null);\n                    if (pConvertContext == null)\n                        throw new ApplicationException(\"Could not initialize the conversion context.\");\n\n                    ffmpeg.sws_scale(pConvertContext, pFrame->data, pFrame->linesize, 0, outputImage.Height, dstData, dstLinesize);\n                    outputImage.Timestamp = pDecodedFrame->pts;\n\n                    return FrameExtractionStatus.Succeeded;\n                }\n                finally\n                {\n                    ffmpeg.av_packet_unref(pPacket);\n                    ffmpeg.av_frame_unref(pDecodedFrame);\n                }\n            }\n        }\n\n        private void ClearStreamInfo()\n        {\n            foreach (var stream in currentStreams.Values)\n            {","sourceCodeStart":396,"sourceCodeEnd":432,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Video/FFmpeg/FFmpegMedia.cs#L396-L432","documentation":"ExtractNextImage converts each decoded frame to the destination pixel format using libswscale. sws_getCachedContext returns null if the source format/size combination cannot be handled, and this ApplicationException is thrown in that case.","triggerScenarios":"A decoded frame has a pixel format or dimensions that sws_getCachedContext cannot build a conversion context for — e.g. exotic hardware frame formats that were not transferred to CPU, zero width/height in the codec context, or unsupported source pixel format.","commonSituations":"Videos with unusual chroma subsampling or 10/12-bit formats on FFmpeg builds lacking those swscale converters; hardware frames not correctly copied to CPU before scaling; corrupt streams yielding zero dimensions.","solutions":["Ensure hardware frames are transferred to CPU (av_hwframe_transfer_data) before sws_scale so the format is a normal software pixel format","Use an FFmpeg build with full swscale format support (or remux/transcode the source video to a standard format like yuv420p)","Validate codec width/height are non-zero before extraction","Log the sourcePixFmt value to confirm which format fails and compare against DestinationPixelFormat"],"exampleFix":"// before\nvar sourcePixFmt = (AVPixelFormat)pFrame->format;\npConvertContext = ffmpeg.sws_getCachedContext(...);\n// after\nif (pFrame->format == (int)streamInfo.Codec.HardwarePixelFormat)\n    throw new ApplicationException(\"Hardware frame reached scaler without CPU transfer\");\nvar sourcePixFmt = (AVPixelFormat)pFrame->format;\npConvertContext = ffmpeg.sws_getCachedContext(...); // now sees a software format","handlingStrategy":"validation","validationCode":"// check codec dimensions and that frames are CPU-side\nif (codecContext.width == 0 || codecContext.height == 0)\n    throw new InvalidOperationException(\"Video stream has invalid dimensions\");","typeGuard":null,"tryCatchPattern":"try { status = media.ExtractNextImage(...); }\ncatch (ApplicationException ex) when (ex.Message.Contains(\"conversion context\"))\n{\n    // fall back: transcode source to yuv420p, or skip frame\n}","preventionTips":["Transcode unusual sources (10-bit, odd chroma) to yuv420p beforehand","Ensure hw frames are CPU-transferred before scaling","Use full-featured FFmpeg swscale builds"],"tags":["ffmpeg","pixel-format","swscale","video"],"backgroundTag":"unsupported-operation","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}