controllerInputFrames.cpp 7.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm

#include "controllerInputFrames.hpp"

#include "gpu/allocator.hpp"
#include "gpu/buffer.hpp"
#include "gpu/memcpy.hpp"

#include "image/unpack.hpp"

namespace VideoStitch {
namespace Core {

namespace {
static Status videoLoadStatus(const Input::ReadStatus& readStatus) {
  switch (readStatus.getCode()) {
    case Input::ReadStatusCode::Ok:
      return Status::OK();
    case Input::ReadStatusCode::ErrorWithStatus:
      return Status{Origin::Input, ErrType::RuntimeError, "Could not load input frames", readStatus.getStatus()};
    case Input::ReadStatusCode::EndOfFile:
      return Status{Origin::Input, ErrType::RuntimeError, "Could not load input frame, reader reported end of stream"};
    case Input::ReadStatusCode::TryAgain:
      return Status{Origin::Input, ErrType::RuntimeError, "Could not load input frame, reader starved"};
  }
  assert(false);
  return Status{Origin::Input, ErrType::ImplementationError, "Could not load input frames, unknown error code"};
}
}  // namespace

template <>
Status ControllerInputFrames<PixelFormat::RGBA, uint32_t>::processFrame(Buffer readerFrame,
                                                                        GPU::HostBuffer<uint32_t> readbackDestination,
                                                                        readerid_t readerID) {
  GPU::Buffer<unsigned char> tmp;
  switch (readerFrame.addressSpace()) {
    case Host:
      FAIL_RETURN(GPU::memcpyAsync(devBuffer, readerFrame.hostBuffer(), readerFrame.hostBuffer().byteSize(), stream));
      tmp = devBuffer;
      break;
    case Device:
      tmp = readerFrame.deviceBuffer();
      break;
  }

  auto reader = readerController->getReader(readerID);
  auto spec = reader->getSpec();
  FAIL_RETURN(Image::unpackCommonPixelFormat(spec.format, *surf->pimpl->surface, tmp.as_const(), spec.width,
                                             spec.height, stream));
  FAIL_RETURN(GPU::memcpyAsync(readbackDestination.hostPtr(), *surf->pimpl->surface, stream));

  return stream.synchronize();
}

template <>
Status ControllerInputFrames<PixelFormat::Grayscale, unsigned char>::processFrame(
    Buffer readerFrame, GPU::HostBuffer<unsigned char> readbackDestination, readerid_t readerID) {
  GPU::Buffer<unsigned char> tmp;
  switch (readerFrame.addressSpace()) {
    case Host:
      FAIL_RETURN(GPU::memcpyAsync(devBuffer, readerFrame.hostBuffer(), readerFrame.hostBuffer().byteSize(), stream));
      tmp = devBuffer;
      break;
    case Device:
      tmp = readerFrame.deviceBuffer();
      break;
  }

  auto reader = readerController->getReader(readerID);
  auto spec = reader->getSpec();
  FAIL_RETURN(Image::unpackCommonPixelFormat(spec.format, *surf->pimpl->surface, tmp.as_const(), spec.width,
                                             spec.height, stream));
  // convert from RGBA into grayscale
  Image::unpackGrayscale(grayscale, *surf->pimpl->surface, spec.width, spec.height, stream);
  FAIL_RETURN(GPU::memcpyAsync(readbackDestination, grayscale, stream));
  return stream.synchronize();
}

template <PixelFormat destinationColor, typename readbackType>
Status ControllerInputFrames<destinationColor, readbackType>::load(
    std::map<readerid_t, PotentialValue<GPU::HostBuffer<readbackType>>>& processedFrames, mtime_t* date) {
  std::map<readerid_t, Input::PotentialFrame> framesFromReader;
  processedFrames.clear();
85
  Audio::audioBlocks_t audioBlocks;
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
  mtime_t tempDate = 0;
  Input::MetadataChunk metadata;
  auto loadStatus = readerController->load(tempDate, framesFromReader, audioBlocks, metadata);
  FAIL_RETURN(videoLoadStatus(std::get<0>(loadStatus)));
  if (date) {
    *date = tempDate;
  }

  for (auto inputFrame : framesFromReader) {
    if (inputFrame.second.status.ok()) {
      auto processStatus = processFrame(inputFrame.second.frame, readbackFrames[inputFrame.first], inputFrame.first);
      processedFrames.insert({inputFrame.first, {processStatus, readbackFrames[inputFrame.first]}});
    } else {
      processedFrames.insert({inputFrame.first, videoLoadStatus(inputFrame.second.status)});
    }
  }

  readerController->releaseBuffer(framesFromReader);
  return Status::OK();
}

template <PixelFormat destinationColor, typename readbackType>
Potential<ControllerInputFrames<destinationColor, readbackType>>
ControllerInputFrames<destinationColor, readbackType>::create(const Core::PanoDefinition* pano) {
  auto cif = new ControllerInputFrames();
  Status initStatus = cif->init(pano);

  if (!initStatus.ok()) {
    delete cif;
    return initStatus;
  }

  return cif;
}

template <PixelFormat destinationColor, typename readbackType>
Status ControllerInputFrames<destinationColor, readbackType>::init(const Core::PanoDefinition* pano) {
  FAIL_RETURN(GPU::useDefaultBackendDevice());

  auto potStream = GPU::Stream::create();
  FAIL_RETURN(potStream.status());
  stream = potStream.value();

  std::unique_ptr<Core::AudioPipeDefinition> audioPipe(Core::AudioPipeDefinition::createDefault());
  auto potReaderController = ReaderController::create(*pano, *audioPipe, new Input::DefaultReaderFactory(0, -1), 0);

  if (!potReaderController.ok()) {
    readerController = nullptr;
    return potReaderController.status();
  }

  readerController = potReaderController.release();

  FAIL_RETURN(readerController->setupReaders());

  // Allocate buffers for readback.
  int64_t height = readerController->getReaderSpec(0).height;
  int64_t width = readerController->getReaderSpec(0).width;
  for (int k = 0; k < (int)pano->numInputs(); ++k) {
    if (!pano->getInput(k).getIsVideoEnabled()) {
      continue;
    }
    const Input::VideoReader::Spec& spec = readerController->getReaderSpec(k);
    auto potReadback = GPU::HostBuffer<readbackType>::allocate(spec.width * spec.height, "ControllerInputFrames");
    FAIL_RETURN(potReadback.status());
    readbackFrames.push_back(potReadback.value());
    if ((spec.height != height) || (spec.width != width)) {
      return {Origin::Input, ErrType::InvalidConfiguration, "All inputs must have the same size"};
    }
  }

  auto potDevBuffer =
      GPU::Buffer<unsigned char>::allocate(readerController->getReaderSpec(0).frameDataSize, "ControllerInputFrames");
  FAIL_RETURN(potDevBuffer.status());
  devBuffer = potDevBuffer.value();

  PotentialValue<GPU::Buffer2D> potGray = GPU::Buffer2D::allocate(
      readerController->getReaderSpec(0).width, readerController->getReaderSpec(0).height, "Grayscale image");
  FAIL_RETURN(potGray.status());
  grayscale = potGray.value();

  Potential<SourceSurface> potSurf = OffscreenAllocator::createSourceSurface(width, height, "ControllerInputFrames");
  FAIL_RETURN(potSurf.status());
  surf = potSurf.release();

  return Status::OK();
}

template <PixelFormat destinationColor, typename readbackType>
ControllerInputFrames<destinationColor, readbackType>::~ControllerInputFrames() {
  if (readerController) {
    readerController->cleanReaders();
    delete readerController;
  }

  if (devBuffer.wasAllocated()) {
    devBuffer.release();
  }

  grayscale.release();

  if (surf) {
    delete surf;
  }

  for (auto hostBuf : readbackFrames) {
    if (hostBuf.byteSize()) {
      hostBuf.release();
    }
  }

  stream.destroy();
}

template <PixelFormat destinationColor, typename readbackType>
Status ControllerInputFrames<destinationColor, readbackType>::seek(frameid_t fr) {
  return readerController->seekFrame(fr);
}

template class ControllerInputFrames<PixelFormat::RGBA, uint32_t>;
template class ControllerInputFrames<PixelFormat::Grayscale, unsigned char>;

}  // namespace Core
}  // namespace VideoStitch