std::shared_ptr TMKFeatureVectors::readFromInputStream()

in tmk/cpp/algo/tmkfv.cpp [445:542]


std::shared_ptr<TMKFeatureVectors> TMKFeatureVectors::readFromInputStream(
    FILE* fp,
    const char* programName) {
  io::FeatureVectorFileHeader header;
  bool read_rc;

  facebook::tmk::io::TMKFramewiseAlgorithm algorithm;
  if (!facebook::tmk::io::readFeatureVectorFileHeader(
          fp, &header, algorithm, programName)) {
    return nullptr;
  }

  if (algorithm == facebook::tmk::io::TMKFramewiseAlgorithm::UNRECOGNIZED) {
    fprintf(stderr, "%s: failed to recognized algorithm.\n", programName);
    return nullptr;
  }
  int framesPerSecond = header.framesPerSecond;
  int numPeriods = header.numPeriods;
  int numFourierCoefficients = header.numFourierCoefficients;
  int frameFeatureDimension = header.frameFeatureDimension;
  int frameFeatureCount = header.frameFeatureCount;
  Periods periods(numPeriods);
  FourierCoefficients fourierCoefficients(numFourierCoefficients);
  FrameFeature pureAverageFeature(frameFeatureDimension);

  // TODO(T25190142): include frameCount

  bool eofUnusedHere = false;

  read_rc = facebook::tmk::io::readIntVector(periods, fp);
  if (!read_rc) {
    fprintf(stderr, "%s: failed to read periods vector.\n", programName);
    return nullptr;
  }

  read_rc = facebook::tmk::io::readFloatVector(
      fourierCoefficients, fp, eofUnusedHere);
  if (!read_rc) {
    fprintf(
        stderr,
        "%s: failed to read fourier-coefficients feature vector.\n",
        programName);
    return nullptr;
  }

  read_rc =
      facebook::tmk::io::readFloatVector(pureAverageFeature, fp, eofUnusedHere);
  if (!read_rc) {
    fprintf(
        stderr,
        "%s: failed to read pure-average feature vector.\n",
        programName);
    return nullptr;
  }

  FeaturesByPeriodsAndFourierCoefficients cosFeatures =
      facebook::tmk::libvec::allocateRank3(
          numPeriods, numFourierCoefficients, frameFeatureDimension);
  FeaturesByPeriodsAndFourierCoefficients sinFeatures =
      facebook::tmk::libvec::allocateRank3(
          numPeriods, numFourierCoefficients, frameFeatureDimension);

  //  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  for (int i = 0; i < cosFeatures.size(); i++) {
    for (int j = 0; j < cosFeatures[i].size(); j++) {
      read_rc = facebook::tmk::io::readFloatVector(
          cosFeatures[i][j], fp, eofUnusedHere);
      if (!read_rc) {
        fprintf(
            stderr, "%s: failed to read feature vector %d.\n", programName, 0);
        return nullptr;
      }
    }
  }

  //  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  for (int i = 0; i < sinFeatures.size(); i++) {
    for (int j = 0; j < sinFeatures[i].size(); j++) {
      read_rc = facebook::tmk::io::readFloatVector(
          sinFeatures[i][j], fp, eofUnusedHere);
      if (!read_rc) {
        fprintf(
            stderr, "%s: failed to read feature vector %d.\n", programName, 0);
        return nullptr;
      }
    }
  }

  return tryCreateFromPrecomputed(
      algorithm,
      framesPerSecond,
      frameFeatureCount,
      periods,
      fourierCoefficients,
      pureAverageFeature,
      cosFeatures,
      sinFeatures);
}