stride3d/stride · error · ApplicationException

Could not initialize the conversion context.

Error message

Could not initialize the conversion context.

What it means

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.

Solutions

  1. Ensure hardware frames are transferred to CPU (av_hwframe_transfer_data) before sws_scale so the format is a normal software pixel format
  2. Use an FFmpeg build with full swscale format support (or remux/transcode the source video to a standard format like yuv420p)
  3. Validate codec width/height are non-zero before extraction
  4. Log the sourcePixFmt value to confirm which format fails and compare against DestinationPixelFormat

Example fix

// before
var sourcePixFmt = (AVPixelFormat)pFrame->format;
pConvertContext = ffmpeg.sws_getCachedContext(...);
// after
if (pFrame->format == (int)streamInfo.Codec.HardwarePixelFormat)
    throw new ApplicationException("Hardware frame reached scaler without CPU transfer");
var sourcePixFmt = (AVPixelFormat)pFrame->format;
pConvertContext = ffmpeg.sws_getCachedContext(...); // now sees a software format
Defensive patterns

Strategy: validation

Validate before calling

// check codec dimensions and that frames are CPU-side
if (codecContext.width == 0 || codecContext.height == 0)
    throw new InvalidOperationException("Video stream has invalid dimensions");

Try / catch

try { status = media.ExtractNextImage(...); }
catch (ApplicationException ex) when (ex.Message.Contains("conversion context"))
{
    // fall back: transcode source to yuv420p, or skip frame
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/efaaf2906dcc2fd7. Report an issue: GitHub.

Appendix: source

Thrown at sources/engine/Stride.Video/FFmpeg/FFmpegMedia.cs:414

                    {
                        // the frame is coming from the GPU
                        ret = ffmpeg.av_hwframe_transfer_data(pCpuCopyFrame, pDecodedFrame, 0);
                        if (ret < 0)
                            throw new ApplicationException("Couldn't transfer frame data from GPU to CPU");

                        pFrame = pCpuCopyFrame;
                    }
                    else
                    {
                        pFrame = pDecodedFrame;
                    }

                    var width = pCodecContext->width;
                    var height = pCodecContext->height;
                    var sourcePixFmt = (AVPixelFormat)pFrame->format;
                    pConvertContext = ffmpeg.sws_getCachedContext(pConvertContext, width, height, sourcePixFmt, width, height, DestinationPixelFormat, ffmpeg.SWS_FAST_BILINEAR, null, null, null);
                    if (pConvertContext == null)
                        throw new ApplicationException("Could not initialize the conversion context.");

                    ffmpeg.sws_scale(pConvertContext, pFrame->data, pFrame->linesize, 0, outputImage.Height, dstData, dstLinesize);
                    outputImage.Timestamp = pDecodedFrame->pts;

                    return FrameExtractionStatus.Succeeded;
                }
                finally
                {
                    ffmpeg.av_packet_unref(pPacket);
                    ffmpeg.av_frame_unref(pDecodedFrame);
                }
            }
        }

        private void ClearStreamInfo()
        {
            foreach (var stream in currentStreams.Values)
            {

View on GitHub (pinned to 96fad776d2)