SubtitleEdit/subtitleedit · error · Exception

MXF KLV packet - stream does not have 16 bytes available for

Error message

MXF KLV packet - stream does not have 16 bytes available for key

What it means

KlvPacket's constructor reads a 16-byte SMPTE KLV key from the stream; if Stream.Read returns fewer than 16 bytes the stream is truncated or out of sync, so parsing cannot continue and the constructor throws. This typically fires when an MXF parser is pointed at a position that is not actually the start of a KLV triplet.

Source

Thrown at src/libse/ContainerFormats/MaterialExchangeFormat/KlvPacket.cs:37

        public static readonly byte[] DataDefinitionAudio = { 0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x01, 0x01, 0x03, 0x02, 0x02, 0x02, 0xff, 0xff, 0x00 };
        public static readonly byte[] Primer = { 0x06, 0xe, 0x2b, 0x34, 0x02, 0x05, 0x1, 0xff, 0x0d, 0x01, 0x02, 0x01, 0x01, 0x05, 0x01, 0xff };

        private const int KeySize = 16;

        public byte[] Key;
        public long DataSize;
        public long TotalSize;
        public long DataPosition;
        public PartitionStatus PartionStatus = PartitionStatus.Unknown;

        public KlvPacket(Stream stream)
        {
            // read 16-bytes key
            Key = new byte[KeySize];
            int read = stream.Read(Key, 0, Key.Length);
            if (read < Key.Length)
            {
                throw new Exception("MXF KLV packet - stream does not have 16 bytes available for key");
            }
            int lengthSize;
            DataSize = GetBasicEncodingRuleLength(stream, out lengthSize);
            DataPosition = stream.Position;
            TotalSize = KeySize + lengthSize + DataSize;
            if (Key[14] >= 1 && Key[14] <= 4)
            {
                PartionStatus = (PartitionStatus)Key[14];
            }
        }

        /// <summary>
        /// Read length - never be more than 9 bytes in size (which means max 8 bytes of payload length)
        /// There are four kinds of encoding for the Length field: 1-byte, 2-byte, 4-byte
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="bytesInLength"></param>
        /// <returns></returns>

View on GitHub (pinned to 17a9f07487)

Solutions

  1. Check stream.Length - stream.Position >= 16 before constructing KlvPacket.
  2. Wrap the iteration in try/catch and treat a short key as end-of-index rather than fatal.
  3. Validate the file signature (MXF header partition pack Universal Label) before iterating.
  4. Re-transfer the MXF from the source if it is truncated.

Example fix

// before
Key = new byte[KeySize];
int read = stream.Read(Key, 0, Key.Length);
if (read < Key.Length) throw new Exception("MXF KLV packet - stream does not have 16 bytes available for key");

// after
Key = new byte[KeySize];
int read = stream.Read(Key, 0, Key.Length);
if (read < Key.Length) throw new InvalidDataException($"Short KLV key: read {read} of {Key.Length} bytes at offset {stream.Position}");
Defensive patterns

Strategy: validation

Validate before calling

if (stream == null || !stream.CanRead) throw new ArgumentException("stream must be readable");
if (stream.Length - stream.Position < 16) throw new InvalidDataException("Fewer than 16 bytes remain for a KLV key");

Try / catch

try { var pkt = new KlvPacket(stream); }
catch (Exception ex) when (ex.Message.Contains("16 bytes")) { /* end of index */ }

Prevention

When it happens

Trigger: Calling new KlvPacket(stream) when the stream position is mid-record or past EOF; corrupted/truncated MXF file; wrong partition offset assumed by the caller; stream wrapping a non-MXF payload.

Common situations: Seeking to an offset derived from a corrupt header pack; iterating KLV triplets until EOF where the last packet is incomplete; reading an MXF captured mid-write; mismatch between expected and actual MXF flavor.

Related errors


AI-assisted analysis of SubtitleEdit/subtitleedit@17a9f07487 (2026-08-13). Data as JSON: /api/errors/c00c4d2148f0b3ba. Report an issue: GitHub.