Boolean MatroskaFileParser::parseEBMLVal_unsigned(EBMLDataSize& size, unsigned& result) {
  if (size.val() > 4) return False; // size too large

  u_int64_t result64;
  if (!parseEBMLVal_unsigned64(size, result64)) return False;

  result = (unsigned)result64;

  return True;
}
Boolean MatroskaFileParser::parseEBMLVal_float(EBMLDataSize& size, float& result) {
  switch (size.val()) {
    case 4: {
      unsigned resultAsUnsigned;

      if (!parseEBMLVal_unsigned(size, resultAsUnsigned)) return False;
      result = *(float*)&resultAsUnsigned;
      return True;
    }
    case 8: {
      u_int64_t resultAsU64;

      if (!parseEBMLVal_unsigned64(size, resultAsU64)) return False;
      result = (float)*(double*)&resultAsU64;
      return True;
    }
    default: {
      return False;
    }
  }
}
Boolean MatroskaFileParser::parseCues() {
#if defined(DEBUG) || defined(DEBUG_CUES)
  fprintf(stderr, "parsing Cues\n");
#endif
  EBMLId id;
  EBMLDataSize size;

  // Read the next header, which should be MATROSKA_ID_CUES:
  if (!parseEBMLIdAndSize(id, size) || id != MATROSKA_ID_CUES) return True; // The header wasn't what we expected, so we're done
  fLimitOffsetInFile = fCurOffsetInFile + size.val(); // Make sure we don't read past the end of this header

  double currentCueTime = 0.0;
  u_int64_t currentClusterOffsetInFile = 0;

  while (fCurOffsetInFile < fLimitOffsetInFile) {
    while (!parseEBMLIdAndSize(id, size)) {}
#ifdef DEBUG_CUES
    if (id == MATROSKA_ID_CUE_POINT) fprintf(stderr, "\n"); // makes debugging output easier to read
    fprintf(stderr, "MatroskaFileParser::parseCues(): Parsed id 0x%s (%s), size: %lld\n", id.hexString(), id.stringName(), size.val());
#endif
    switch (id.val()) {
      case MATROSKA_ID_CUE_POINT: { // 'Cue Point' header: enter this
	break;
      }
      case MATROSKA_ID_CUE_TIME: { // 'Cue Time' header: get this value
	unsigned cueTime;
	if (parseEBMLVal_unsigned(size, cueTime)) {
	  currentCueTime = cueTime*(fOurFile.fTimecodeScale/1000000000.0);
#ifdef DEBUG_CUES
	  fprintf(stderr, "\tCue Time %d (== %f seconds)\n", cueTime, currentCueTime);
#endif
	}
	break;
      }
      case MATROSKA_ID_CUE_TRACK_POSITIONS: { // 'Cue Track Positions' header: enter this
	break;
      }
      case MATROSKA_ID_CUE_TRACK: { // 'Cue Track' header: get this value (but only for debugging; we don't do anything with it)
	unsigned cueTrack;
	if (parseEBMLVal_unsigned(size, cueTrack)) {
#ifdef DEBUG_CUES
	  fprintf(stderr, "\tCue Track %d\n", cueTrack);
#endif
	}
	break;
      }
      case MATROSKA_ID_CUE_CLUSTER_POSITION: { // 'Cue Cluster Position' header: get this value
	u_int64_t cueClusterPosition;
	if (parseEBMLVal_unsigned64(size, cueClusterPosition)) {
	  currentClusterOffsetInFile = fOurFile.fSegmentDataOffset + cueClusterPosition;
#ifdef DEBUG_CUES
	  fprintf(stderr, "\tCue Cluster Position %llu (=> offset within the file: %llu (0x%llx))\n", cueClusterPosition, currentClusterOffsetInFile, currentClusterOffsetInFile);
#endif
	  // Record this cue point:
	  fOurFile.addCuePoint(currentCueTime, currentClusterOffsetInFile, 1/*default block number within cluster*/);
	}
	break;
      }
      case MATROSKA_ID_CUE_BLOCK_NUMBER: { // 'Cue Block Number' header: get this value
	unsigned cueBlockNumber;
	if (parseEBMLVal_unsigned(size, cueBlockNumber) && cueBlockNumber != 0) {
#ifdef DEBUG_CUES
	  fprintf(stderr, "\tCue Block Number %d\n", cueBlockNumber);
#endif
	  // Record this cue point (overwriting any existing entry for this cue time):
	  fOurFile.addCuePoint(currentCueTime, currentClusterOffsetInFile, cueBlockNumber);
	}
	break;
      }
      default: { // We don't process this header, so just skip over it:
	skipHeader(size);
#ifdef DEBUG_CUES
	fprintf(stderr, "\tskipped %lld bytes\n", size.val());
#endif
	break;
      }
    }
    setParseState();
  }

  fLimitOffsetInFile = 0; // reset
#if defined(DEBUG) || defined(DEBUG_CUES)
  fprintf(stderr, "done parsing Cues\n");
#endif
#ifdef DEBUG_CUES
  fprintf(stderr, "Cue Point tree: ");
  fOurFile.printCuePoints(stderr);
  fprintf(stderr, "\n");
#endif
  return True; // we're done parsing Cues
}
void MatroskaFileParser::lookForNextTrack() {
#ifdef DEBUG
  fprintf(stderr, "looking for Track\n");
#endif
  EBMLId id;
  EBMLDataSize size;

  // Read and skip over (or enter) each Matroska header, until we get to a 'Track'.
  while (fCurrentParseState == LOOKING_FOR_TRACKS) {
    while (!parseEBMLIdAndSize(id, size)) {}
#ifdef DEBUG
    fprintf(stderr, "MatroskaFileParser::lookForNextTrack(): Parsed id 0x%s (%s), size: %lld\n", id.hexString(), id.stringName(), size.val());
#endif
    switch (id.val()) {
      case MATROSKA_ID_SEGMENT: { // 'Segment' header: enter this
	// Remember the position, within the file, of the start of Segment data, because Seek Positions are relative to this:
	fOurFile.fSegmentDataOffset = fCurOffsetInFile;
	break;
      }
      case MATROSKA_ID_SEEK_HEAD: { // 'Seek Head' header: enter this
	break;
      }
      case MATROSKA_ID_SEEK: { // 'Seek' header: enter this
	break;
      }
      case MATROSKA_ID_SEEK_ID: { // 'Seek ID' header: get this value
	if (parseEBMLNumber(fLastSeekId)) {
#ifdef DEBUG
	  fprintf(stderr, "\tSeek ID 0x%s:\t%s\n", fLastSeekId.hexString(), fLastSeekId.stringName());
#endif
	}
	break;
      }
      case MATROSKA_ID_SEEK_POSITION: { // 'Seek Position' header: get this value
	u_int64_t seekPosition;
	if (parseEBMLVal_unsigned64(size, seekPosition)) {
	  u_int64_t offsetInFile = fOurFile.fSegmentDataOffset + seekPosition;
#ifdef DEBUG
	  fprintf(stderr, "\tSeek Position %llu (=> offset within the file: %llu (0x%llx))\n", seekPosition, offsetInFile, offsetInFile);
#endif
	  // The only 'Seek Position's that we care about are for 'Cluster' and 'Cues':
	  if (fLastSeekId == MATROSKA_ID_CLUSTER) {
	    fOurFile.fClusterOffset = offsetInFile;
	  } else if (fLastSeekId == MATROSKA_ID_CUES) {
	    fOurFile.fCuesOffset = offsetInFile;
	  }
	}
	break;
      }
      case MATROSKA_ID_INFO: { // 'Segment Info' header: enter this
	break;
      }
      case MATROSKA_ID_TIMECODE_SCALE: { // 'Timecode Scale' header: get this value
        unsigned timecodeScale;
	if (parseEBMLVal_unsigned(size, timecodeScale) && timecodeScale > 0) {
	  fOurFile.fTimecodeScale = timecodeScale;
#ifdef DEBUG
	  fprintf(stderr, "\tTimecode Scale %u ns (=> Segment Duration == %f seconds)\n", fOurFile.timecodeScale(), fOurFile.fileDuration());
#endif
	}
	break;
      }
      case MATROSKA_ID_DURATION: { // 'Segment Duration' header: get this value
	if (parseEBMLVal_float(size, fOurFile.fSegmentDuration)) {
#ifdef DEBUG
	  fprintf(stderr, "\tSegment Duration %f (== %f seconds)\n", fOurFile.segmentDuration(), fOurFile.fileDuration());
#endif
	}
	break;
      }
      case MATROSKA_ID_TRACKS: { // enter this, and move on to parsing 'Tracks'
	fLimitOffsetInFile = fCurOffsetInFile + size.val(); // Make sure we don't read past the end of this header
	fCurrentParseState = PARSING_TRACK;
	break;
      }
      default: { // skip over this header
	skipHeader(size);
#ifdef DEBUG
	fprintf(stderr, "\tskipped %lld bytes\n", size.val());
#endif
	break;
      }
    }
    setParseState();
  }
}