{"record":{"id":"0192707f87c4e47a","repo":"SubtitleEdit/subtitleedit","slug":"problem-reading-avi-header","errorCode":null,"errorMessage":"Problem reading AVI header.","messagePattern":"Problem reading AVI header\\.","errorType":"exception","errorClass":"RiffParserException","httpStatus":null,"severity":"error","filePath":"src/libse/ContainerFormats/RiffDecodeHeader.cs","lineNumber":243,"sourceCode":"\n        public static short GetShort(byte[] buffer, int index)\n        {\n            byte[] bytes = { buffer[index + 0], buffer[index + 1] };\n\n            if (!BitConverter.IsLittleEndian)\n            {\n                Array.Reverse(bytes);\n            }\n\n            return BitConverter.ToInt16(bytes, 0);\n        }\n\n        private void DecodeAviHeader(RiffParser rp, int length)\n        {\n            var ba = new byte[length];\n            if (rp.ReadData(ba, 0, length) != length)\n            {\n                throw new RiffParserException(\"Problem reading AVI header.\");\n            }\n\n            _frameRate = GetInt(ba, 0);\n            _maxBitRate = GetInt(ba, 4);\n            _totalFrames = GetInt(ba, 16);\n            _numStreams = GetInt(ba, 24);\n            _width = GetInt(ba, 32);\n            _height = GetInt(ba, 36);\n        }\n\n        private void DecodeAviStream(RiffParser rp, int length)\n        {\n            var ba = new byte[length];\n            if (rp.ReadData(ba, 0, length) != length)\n            {\n                throw new RiffParserException(\"Problem reading AVI header.\");\n            }\n","sourceCodeStart":225,"sourceCodeEnd":261,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/libse/ContainerFormats/RiffDecodeHeader.cs#L225-L261","documentation":"DecodeAviHeader reads 'length' bytes for the AVIMAINHEADER chunk via RiffParser.ReadData; if fewer than 'length' bytes are returned the main AVI header is incomplete and cannot be decoded, so a RiffParserException is thrown. The downstream fields (frame rate, dimensions, stream count) all depend on a complete 56-byte header.","triggerScenarios":"AVI file truncated inside the 'avih' chunk; RIFF nesting where the declared chunk size exceeds remaining data; RiffParser handed a stream positioned past the header.","commonSituations":"Partially downloaded AVI; AVI produced by a buggy encoder that wrote a short 'avih'; file written but not flushed/closed properly.","solutions":["Re-acquire the full AVI file from source.","Before decoding, validate the declared 'avih' size is at least 56 bytes and matches remaining stream bytes.","Catch RiffParserException and report a corrupt-file error to the user.","Use a different container parser as a cross-check if the file must be salvaged."],"exampleFix":"// before\nvar ba = new byte[length];\nif (rp.ReadData(ba, 0, length) != length) throw new RiffParserException(\"Problem reading AVI header.\");\n\n// after\nvar ba = new byte[length];\nint got = rp.ReadData(ba, 0, length);\nif (got != length) throw new RiffParserException($\"Short AVI header: read {got} of {length} bytes\");","handlingStrategy":"try-catch","validationCode":"if (length < 56 || rp.BytesRemaining < length) throw new InvalidDataException($\"AVI main header too short: {length}B\");","typeGuard":null,"tryCatchPattern":"try { header.DecodeAviHeader(rp, length); }\ncatch (RiffParserException ex) when (ex.Message.Contains(\"AVI header\")) { /* corrupt; bail or salvage */ }","preventionTips":["Re-acquire AVI from source when the main header is short.","Pre-validate 'avih' chunk size before decoding."],"tags":["avi","riff","container-format","truncated","header"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}