commaai/openpilot · error · VideoFileInvalid

invalid exponential-golomb code

Error message

invalid exponential-golomb code

What it means

vidindex parses HEVC SPS/PPS NAL units by hand, reading exp-golomb codes bit by bit. If no valid code terminator is found within the data (prefix/suffix never balance), the bitstream is malformed at that point and VideoFileInvalid('invalid exponential-golomb code') is raised. It means the camera stream is corrupt, not a code bug.

Source

Thrown at openpilot/tools/lib/vidindex.py:148

    j = 7
    while j >= 0:
      if skip_bits > 0:
        skip_bits -= 1
      elif prefix_val == 0:
        prefix_val = (dat[i] >> j) & 1
        prefix_len += 1
      else:
        suffix_val = (suffix_val << 1) | ((dat[i] >> j) & 1)
        suffix_len += 1
      j -= 1

      if prefix_val == 1 and prefix_len - 1 == suffix_len:
        val = int(2**(prefix_len-1) - 1 + suffix_val)
        size = prefix_len + suffix_len
        return val, size
    i += 1

  raise VideoFileInvalid("invalid exponential-golomb code")

def require_nal_unit_start(dat: bytes, nal_unit_start: int) -> None:
  if nal_unit_start < 1:
    raise ValueError("start index must be greater than zero")

  if dat[nal_unit_start:nal_unit_start + NAL_UNIT_START_CODE_SIZE] != NAL_UNIT_START_CODE:
    raise VideoFileInvalid("data must begin with start code")

def get_hevc_nal_unit_length(dat: bytes, nal_unit_start: int) -> int:
  try:
    pos = dat.index(NAL_UNIT_START_CODE, nal_unit_start + NAL_UNIT_START_CODE_SIZE)
  except ValueError:
    pos = -1

  # length of NAL unit is byte count up to next NAL unit start index
  nal_unit_len = (pos if pos != -1 else len(dat)) - nal_unit_start
  if DEBUG:
    print("  nal_unit_len:", nal_unit_len)

View on GitHub (pinned to 516ec1e682)

Solutions

  1. Confirm the file decodes with the reference decoder: ffmpeg -v error -i file.hevc -f null - ; if ffmpeg also fails, the data is corrupt
  2. Re-download the segment; if it is corrupt at the source, skip that segment in your analysis
  3. Catch VideoFileInvalid per-segment in batch jobs and continue with other segments

Example fix

# before
index, l, prefix = hevc_index(fn)

# after
from openpilot.tools.lib.vidindex import VideoFileInvalid
try:
    index, l, prefix = hevc_index(fn)
except VideoFileInvalid:
    print(f'{fn}: corrupt HEVC, skipping segment'); continue
Defensive patterns

Strategy: try-catch

Validate before calling

import subprocess

def hevc_decodes(fn: str) -> bool:
    with FileReader(fn) as f:
        head = f.read(1 << 20)
    return subprocess.run(['ffmpeg', '-v', 'error', '-f', 'hevc', '-i', 'pipe:0', '-f', 'null', '-'], input=head, capture_output=True).returncode == 0

Try / catch

from openpilot.tools.lib.vidindex import VideoFileInvalid
try:
    frame_types, dat_len, prefix = hevc_index(fn)
except VideoFileInvalid:
    mark_segment_corrupt(fn); continue

Prevention

When it happens

Trigger: hevc_index()/get_video_index() on a truncated or corrupt camera file; video from a device whose encoder crashed mid-segment; data corrupted in transfer; a file that is not actually HEVC.

Common situations: Dashcam power loss mid-encode producing torn frames; partial segment uploads; bit flips in cheap storage; processing foreign video claiming to be hevc.

Related errors


AI-assisted analysis of commaai/openpilot@516ec1e682 (2026-08-15). Data as JSON: /api/errors/5e7e8f225fc7bfc7. Report an issue: GitHub.