stride3d/stride · error · ApplicationException

Could not open codec. Error code=

Error message

Could not open codec. Error code={ret.ToString("X8")}

What it means

FFmpegCodec throws this ApplicationException when ffmpeg.avcodec_open2 fails to open the selected decoder on the allocated codec context. avcodec_open2 returning a negative value means FFmpeg rejected the codec open (bad codec, unsupported parameters, or a hardware decoder issue). The hex code in the message is the raw FFmpeg AVERROR value.

Solutions

  1. Read the hex error code from the message and map it to the FFmpeg AVERROR constant (e.g. ENOMEM, EINVAL) via av_strerror to identify the root cause.
  2. Verify the FFmpeg native binaries for the platform include the decoder for the stream's codec.
  3. Check that the AVCodec and AVCodecContext were set up from the stream's codecpar before calling open.
  4. Ensure FFmpegUtils.EnsurePlatformSupport() passes on the target OS and the correct native libs are deployed.

Example fix

// before
var codec = ffmpeg.avcodec_find_decoder(stream->codecpar->codec_id);
context = ffmpeg.avcodec_alloc_context3(codec);
ffmpeg.avcodec_open2(context, codec, null); // fails: codecpar not copied
// after
var codec = ffmpeg.avcodec_find_decoder(stream->codecpar->codec_id);
context = ffmpeg.avcodec_alloc_context3(codec);
ffmpeg.avcodec_parameters_to_context(context, stream->codecpar);
ffmpeg.avcodec_open2(context, codec, null);
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure native FFmpeg support before constructing a codec
FFmpegUtils.EnsurePlatformSupport();

Try / catch

try
{
    var codec = new FFmpegCodec(...);
}
catch (ApplicationException ex)
{
    // log ex.Message containing the hex AVERROR code; fall back to software decoding
    Logger.Error($"Codec open failed: {ex.Message}");
}

Prevention

When it happens

Trigger: Constructing an FFmpegCodec whose constructor calls avcodec_open2 on a codec context; ret < 0 from FFmpeg (e.g. codec not found for the stream, invalid codec parameters, hardware decoder initialization failure).

Common situations: Decoding a video with a codec whose parameters FFmpeg cannot initialize; running on a platform where the FFmpeg build lacks the required decoder; passing a codec/context pair that does not match the stream.

Related errors


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

Appendix: source

Thrown at sources/engine/Stride.Video/FFmpeg/FFmpegCodec.cs:57

                // TODO: log?
                throw new ApplicationException("Unsupported codec.");

            int ret;
            var pCodecContext = ffmpeg.avcodec_alloc_context3(pCodec);

            ret = ffmpeg.avcodec_parameters_to_context(pCodecContext, originalCodecpar);
            if (ret < 0)
                // TODO: log?
                throw new ApplicationException($"Could not fill codec parameters. Error code={ret.ToString("X8")}");

            SetupHardwareAcceleration(pCodecContext);

            if (ffmpeg.avcodec_is_open(pCodecContext) == 0)
            {
                ret = ffmpeg.avcodec_open2(pCodecContext, pCodec, null);
                if (ret < 0)
                    // TODO: log?
                    throw new ApplicationException($"Could not open codec. Error code={ret.ToString("X8")}");
            }

            pAVCodecContext = pCodecContext;
        }

        /// <summary>Initializes the platform-specific hardware decode context, when supported.</summary>
        partial void SetupHardwareAcceleration(AVCodecContext* pCodecContext);

        /// <summary>Restores the platform-specific get_format callback after <see cref="ffmpeg.avcodec_flush_buffers"/>.</summary>
        partial void RestoreGetFormatAfterFlush();

        /// <summary>Disposes the platform-specific hardware decode resources.</summary>
        partial void DisposeHardwareAcceleration();

        private static AVPixelFormat FindPixelFormat(AVHWDeviceType deviceType)
        {
            if (deviceType == AVHWDeviceType.AV_HWDEVICE_TYPE_D3D11VA)
                return AVPixelFormat.AV_PIX_FMT_D3D11;

View on GitHub (pinned to 96fad776d2)