{"record":{"id":"e53d81b0192f503f","repo":"stride3d/stride","slug":"could-not-open-codec-error-code-ret-tostring-x8","errorCode":null,"errorMessage":"Could not open codec. Error code={ret.ToString(\"X8\")}","messagePattern":"Could not open codec\\. Error code=(.+?)","errorType":"exception","errorClass":"ApplicationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Video/FFmpeg/FFmpegCodec.cs","lineNumber":57,"sourceCode":"                // TODO: log?\n                throw new ApplicationException(\"Unsupported codec.\");\n\n            int ret;\n            var pCodecContext = ffmpeg.avcodec_alloc_context3(pCodec);\n\n            ret = ffmpeg.avcodec_parameters_to_context(pCodecContext, originalCodecpar);\n            if (ret < 0)\n                // TODO: log?\n                throw new ApplicationException($\"Could not fill codec parameters. Error code={ret.ToString(\"X8\")}\");\n\n            SetupHardwareAcceleration(pCodecContext);\n\n            if (ffmpeg.avcodec_is_open(pCodecContext) == 0)\n            {\n                ret = ffmpeg.avcodec_open2(pCodecContext, pCodec, null);\n                if (ret < 0)\n                    // TODO: log?\n                    throw new ApplicationException($\"Could not open codec. Error code={ret.ToString(\"X8\")}\");\n            }\n\n            pAVCodecContext = pCodecContext;\n        }\n\n        /// <summary>Initializes the platform-specific hardware decode context, when supported.</summary>\n        partial void SetupHardwareAcceleration(AVCodecContext* pCodecContext);\n\n        /// <summary>Restores the platform-specific get_format callback after <see cref=\"ffmpeg.avcodec_flush_buffers\"/>.</summary>\n        partial void RestoreGetFormatAfterFlush();\n\n        /// <summary>Disposes the platform-specific hardware decode resources.</summary>\n        partial void DisposeHardwareAcceleration();\n\n        private static AVPixelFormat FindPixelFormat(AVHWDeviceType deviceType)\n        {\n            if (deviceType == AVHWDeviceType.AV_HWDEVICE_TYPE_D3D11VA)\n                return AVPixelFormat.AV_PIX_FMT_D3D11;","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Video/FFmpeg/FFmpegCodec.cs#L39-L75","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["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.","Verify the FFmpeg native binaries for the platform include the decoder for the stream's codec.","Check that the AVCodec and AVCodecContext were set up from the stream's codecpar before calling open.","Ensure FFmpegUtils.EnsurePlatformSupport() passes on the target OS and the correct native libs are deployed."],"exampleFix":"// before\nvar codec = ffmpeg.avcodec_find_decoder(stream->codecpar->codec_id);\ncontext = ffmpeg.avcodec_alloc_context3(codec);\nffmpeg.avcodec_open2(context, codec, null); // fails: codecpar not copied\n// after\nvar codec = ffmpeg.avcodec_find_decoder(stream->codecpar->codec_id);\ncontext = ffmpeg.avcodec_alloc_context3(codec);\nffmpeg.avcodec_parameters_to_context(context, stream->codecpar);\nffmpeg.avcodec_open2(context, codec, null);","handlingStrategy":"try-catch","validationCode":"// ensure native FFmpeg support before constructing a codec\nFFmpegUtils.EnsurePlatformSupport();","typeGuard":null,"tryCatchPattern":"try\n{\n    var codec = new FFmpegCodec(...);\n}\ncatch (ApplicationException ex)\n{\n    // log ex.Message containing the hex AVERROR code; fall back to software decoding\n    Logger.Error($\"Codec open failed: {ex.Message}\");\n}","preventionTips":["Always call FFmpegUtils.EnsurePlatformSupport() before decoding.","Copy stream codecpar into the codec context before opening.","Deploy the correct FFmpeg native binaries per platform.","Log and map hex AVERROR codes for diagnostics."],"tags":["ffmpeg","video-decoding","native-interop"],"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"}