input.hpp 19.2 KB
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 85 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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm

#pragma once

#include "config.hpp"
#include "status.hpp"
#include "audio.hpp"
#include "frame.hpp"
#include "imuData.hpp"
#include "orah/exposureData.hpp"
#include "sink.hpp"

#include <stdint.h>
#include <cstdlib>
#include <limits>
#include <iosfwd>
#include <memory>
#include <mutex>

namespace VideoStitch {

namespace GPU {

template <typename T>
class Buffer;
class Surface;
class Stream;
}  // namespace GPU

namespace Input {

class VideoReader;
class AudioReader;
class MetadataReader;
class SinkReader;

enum class ReadStatusCode {
  // generic states
  Ok,
  ErrorWithStatus,
  // custom reader states
  EndOfFile,
  // the call is marked non-blocking and the requested operation would block, or:
  // there's no data available yet
  TryAgain,
};

typedef Result<ReadStatusCode> ReadStatus;

/**
 * @brief The base pointer for audio and video readers.
 */
class VS_EXPORT Reader {
 public:
  virtual ~Reader();

  // The index of this reader in the InputDefinition list of the project.
  // Makes the bond between the configuration values and the realtime objects.
  const readerid_t id;  ///< id

  /**
   * Type-casting to video reader
   * Might return null for readers with restricted capabilities.
   */
  VideoReader* getVideoReader() const;
  /**
   * Type-casting to audio reader
   * Might return null for readers with restricted capabilities.
   */
  AudioReader* getAudioReader() const;

  MetadataReader* getMetadataReader() const;

  SinkReader* getSinkReader() const;

  mtime_t getLatency();
  void setLatency(mtime_t); /* force new latency value */
  /*
   * update latency only if it increase and return true
   * return false if latency unmodified
   */
  bool updateLatency(mtime_t);

 protected:
  /**
   * @param id id
   */
  explicit Reader(readerid_t id);

 private:
  mtime_t latency;
  std::mutex latencyMutex;
};

/**
 * @brief The common interface to be implemented by all input video readers.
 *
 * Note that this class is performance-critical, so if you think about implementing
 * a custom reader you should be aware of general computing performance issues and read
 * the design docs of the input library.
 *
 * In particular, most reader will be I/O bound, and therefore the design is such
 * that it's better to do heavy processing in:
 *  - initialization methods
 *  - readFrame()
 *
 * Any other method is usually trivial.
 *
 * Note that readers should avoid being asynchronous and doing work in threads since the lib
 * already handles handles that at a higher level and strives to reuse buffers to limit memory usage.
 *
 * The pipeline works as follow: first, the frame data, with size getFrameDataSize(),
 * is read using readFrame() and transmitted to the GPU. readFrame() should never write
 * more than getFrameDataSize() bytes into the output buffer.
 * Loading immediately begins for the next image in parallel.
 * At the same time, the getFrameDataSize() of data is transmitted to the GPU.
 * If the pixel format is unknown to VideoStitch, unpackDevBuffer() is then called on the GPU device buffer data.
 * At this point, the data must be in packed 32-bit RGBA form on the GPU.
 * Then the reader is left alone and the mapping is started.
 *
 * In particular, procedural readers should strive to do all the work on the GPU
 * (i.e. in unpackDevBuffer).
 *
 * NOTE: By convention, frames are zero-based.
 */
class VS_EXPORT VideoReader : public virtual Reader {
 public:
  /**
   * @brief A class that holds time-constant specifications.
   */
  struct VS_EXPORT Spec {
    /**
     * Constructs a spec.
     * @param width The reader width.
     * @param height The reader height.
     * @param frameDataSize Size in bytes of a single frame.
     * @param pixelFormat The pixel format.
     * @param frameNum The number of frames for this reader.
     * @param frameRate Frame rate, in frames per second. Negative means unknown.
     * @param frameRateIsProcedural True if the frame rate is procedural, False otherwise, meaning it does not come from
     * an actual reader that needs to work with other readers sharing the same frame rate.
     * @param maskHostBuffer Mask data.
     * @param flags Reserved. Must be 0.
     */
    Spec(int64_t width, int64_t height, int64_t frameDataSize, VideoStitch::PixelFormat pixelFormat,
         AddressSpace addressspace, int frameNum, FrameRate frameRate, bool frameRateIsProcedural,
         const unsigned char* maskHostBuffer, int flags = 0);

    Spec();

    /**
     * Copy from @a spec.
     */
    Spec(const Spec& spec);

    ~Spec();

    /**
     * The reader width.
     */
    const int64_t width;
    /**
     * The reader height.
     */
    const int64_t height;
    /**
     * Size in bytes of a single frame.
     */
    const int64_t frameDataSize;
    /**
     * The pixel format.
     */
    const VideoStitch::PixelFormat format;
    /**
     * Input frames location.
     */
    const AddressSpace addressSpace;
    /**
     * Number of frames for this reader.
     */
    const int frameNum;
    /**
     * Frame rate, in frames per second.
     * Negative values mean that the reader has no known frame rate.
     */
    const FrameRate frameRate;
    /**
     * Flag whether the frame rate is procedural or not
     * meaning it does not come from an actual reader that needs to work with other readers sharing the same frame rate
     */
    const bool frameRateIsProcedural;
    /**
     * Flags. Reserved.
     */
    const int flags;
    /**
     * Host buffer containing input mask, of size width * height.
     * NULL means no mask. Not owned.
     */
    const unsigned char* const maskHostBuffer;

    /**
     * Returns a display name for the reader. Never assume any format for that. EVER.
     * That means that the only thing that's allowed with that is to diplay it to the user.
     * No testing for equality, parsing...
     * Thread safe.
     * @param os Sink for the display name.
     */
    void getDisplayName(std::ostream& os) const;

    /**
     * Reader implementors: use that to set the display name. You're free to put anything, and change it. Make it small.
     * @param name The display name to set.
     */
    void setDisplayName(const char* name);

   private:
    Spec& operator=(const Spec&) = delete;

    class Impl;
    Impl* const pimpl;
  };

 public:
  virtual ~VideoReader();

  /**
   * Read a frame into videoFrame and advance to the next frame.
   * Optionally read an audio packet synchronized with this frame.
   * It is the Reader's job to ensure correct synchronization.
   *
   * @param date The capture/decode timestamp in microseconds.
   * @param videoFrame Where to write the video frame.
   *                   The provided buffer is of size at least getFrameDataSize().
   * @return a success status.
   */
  virtual ReadStatus readFrame(mtime_t& date, unsigned char* videoFrame) = 0;

  /**
   * Seek to the given frame.
   * @param frame Where to seek. Starts at the reader's first frame.
   * @return False on failure. In that case, the reader is free to leave the
   *                           current frame is in an undetermined state.
   *                           The client can call getCurFrame() or call
   *                           seekFrame() again to seek to another frame.
   * @note for implementors: On failure, it's advised to do the most efficient
   *                         thing given that the next call will probably be a
   *                         seek to the previous 'current' frame.
   */
  virtual Status seekFrame(frameid_t frame) = 0;

  /**
   * Returns the first frame in the sequence (inclusive).
   */
  frameid_t getFirstFrame() const;
  /**
   * Returns the last frame in the sequence (inclusive), or NO_LAST_FRAME.
   */
  frameid_t getLastFrame() const;

  /**
   * Convert the device memory from the internal pixel format to RGBA.
   * Use this function if your PixelFormat is Unknown to VideoStitch.
   *
   * Note that to be efficient, implementations of this method are not allowed
   * to do I/O. It is even encouraged that this method only contains a kernel call.
   * Implementations of this functions must be stateless (i.e. they cannot touch any
   * non-const state in the class).
   * The function should return only when the data in the buffer is ready to be
   * processed, or push a kernel call to the given CUDA stream.
   * The kernel call should use stream (cudaStream_t)stream, and should be asynchronous
   * for better performance.
   * @param dst Destination buffer
   * @param src source buffer
   * @param stream GPU stream on which the computations will run.
   */
  virtual Status unpackDevBuffer(GPU::Surface& dst, const GPU::Buffer<const unsigned char>& src,
                                 GPU::Stream& stream) const;

  /**
   * Returns the width of the reader.
   */
  int64_t getWidth() const;

  /**
   * Returns the height of the reader.
   */
  int64_t getHeight() const;

  /**
   * Returns the size of the data for one frame that
   * should be transfered over to the GPU.
   */
  int64_t getFrameDataSize() const;

  /**
   * Returns the (const) reader spec.
   */
  const Spec& getSpec() const;

  /**
   * Returns the reader spec.
   */
  Spec& getSpec();

  /**
   * Some readers need to allocate GPU memory behind the scene. Since GPU allocations are tied to threads,
   * any allocation that is done by the reader (including, but not restricted to, readFrame() or unpackDevBuffer())
   * will need to be done in the same thread that called these methods.
   * The controller will take care of this when the stitchers are released if implementors cleanup memory in this
   * method. You can skip implementing this method if you don't do such allocations.
   */
  virtual Status perThreadInit();

  /**
   * Performs per-thread cleanup. See perThreadInit().
   */
  virtual void perThreadCleanup();

  /**
   * Convert the device memory from a given known pixel format to RGBA.
   *
   * @param fmt Current pixel format of src Buffer, to be unpacked to RGBA.
   * @param dst Destination buffer
   * @param src read-only source buffer
   * @param width Frame width
   * @param height Frame height
   * @param stream GPU stream used for computations.
   */
  static Status unpackDevBuffer(VideoStitch::PixelFormat fmt, GPU::Surface& dst,
                                const GPU::Buffer<const unsigned char>& src, uint64_t width, uint64_t height,
                                GPU::Stream& stream);

 protected:
  /**
   * Constructor.
   * @param width The reader width. Readers cannot be resized.
   * @param height The reader height. Readers cannot be resized.
   * @param frameDataSize Size in pixels.
   * @param format Native pixel format of the Reader before unpacking.
   * @param addressSpace Address space of the produced frames.
   * @param frameRate Frame rate, in frames per second. Negative means unknown.
   * @param firstFrame First available frame.
   * @param lastFrame Last available frame.
   * @param isProcedural boolean whether the reader is procedural or not
   * @param maskHostBuffer Mask data.
   * @param flags Reserved. Must be 0.
   * @note Everything must be known at creation time.
   */
  VideoReader(int64_t width, int64_t height, int64_t frameDataSize, VideoStitch::PixelFormat format,
              AddressSpace addressSpace, FrameRate frameRate, frameid_t firstFrame, frameid_t lastFrame,
              bool isProcedural, const unsigned char* maskHostBuffer, int flags = 0);

 private:
  VideoReader();
  explicit VideoReader(const VideoReader&);

  /**
   * First available frame.
   */
  const unsigned firstFrame;

  /**
   * Last available frame.
   */
  const unsigned lastFrame;

  Spec spec;
};

/**
 * @brief The common interface to be implemented by all input audio readers.
 */
class VS_EXPORT AudioReader : public virtual Reader {
 public:
  /**
   * @brief A class that holds time-constant specifications.
   */
  struct VS_EXPORT Spec {
    /**
     * Constructs a spec.
     * @param layout Audio channels layout.
     * @param sampleRate Audio sampling frequency.
     * @param sampleDepth Format of the audio samples.
     */
    Spec(Audio::ChannelLayout layout, Audio::SamplingRate sampleRate, Audio::SamplingDepth sampleDepth);

    Spec();

    /**
     * Copy from @a spec.
     */
    Spec(const Spec& spec);

    ~Spec();

    /**
     * Audio channels layout
     */
    Audio::ChannelLayout layout;

    /**
     * Audio sampling rate.
     */
    Audio::SamplingRate sampleRate;

    /**
     * Audio channel depth and layout.
     */
    Audio::SamplingDepth sampleDepth;

    /**
     * Returns a display name for the reader. Never assume any format for that. EVER.
     * That means that the only thing that's allowed with that is to diplay it to the user.
     * No testing for equality, parsing...
     * Thread safe.
     * @param os Sink for the display name.
     */
    void getDisplayName(std::ostream& os) const;

    /**
     * Reader implementors: use that to set the display name. You're free to put anything, and change it. Make it small.
     * @param name The display name to set.
     */
    void setDisplayName(const char* name);

   private:
    Spec& operator=(const Spec&) = delete;

    class Impl;
    Impl* const pimpl;
  };

 public:
  virtual ~AudioReader();

  /**
   * Read a frame of audio samples and advance to the next frame.
   *
   * @param nbSamples    Read a maximum of 'nbSamples' samples of audio into buffer.
   * @param audioSamples Where the audio samples channel planes will be written.
   *                     Up to 16 planar channels are supported.
   *                     Audio channel samples are either interleaved into a sample
   *                     frame or planar, either contiguous or padded (see Spec).
   *                     The memory of each plane is not allocated.
   *                     The ownership is transfered to the stitcher.
   *                     Also contains the timestamp of the first sample of the
   *                     buffer in microseconds.
   * @return a success status.
   */
  virtual ReadStatus readSamples(size_t nbSamples, Audio::Samples& audioSamples) = 0;

  /**
   * Seek to the given time.
   * @param date Where to seek.
   * @return False on failure. In that case, the reader is free to leave the
   *                           current frame is in an undetermined state.
   *                           The client can call getCurFrame() or call
   *                           seekFrame() again to seek to another frame.
   * @note for implementors: On failure, it's advised to do the most efficient
   *                         thing given that the next call will probably be a
   *                         seek to the previous 'current' frame.
   */
  virtual Status seekFrame(mtime_t date) = 0;

  /**
   * Returns the number of samples (per channel) currently available in the
   * reader's buffer.
   * @return number of samples (per channel) available to be read
   */
  virtual size_t available() = 0;

  /**
   * Used to determine if the end of stream has been reached.
   * @return true if the end of stream is reached, false otherwise.
   */
  virtual bool eos() = 0;

  /**
   * Returns the (const) reader spec.
   */
  const Spec& getSpec() const;

  /**
   * Returns the reader spec.
   */
  Spec& getSpec();

 protected:
  /**
   * Constructor.
   * @param layout Audio channels layout.
   * @param sampleRate Audio sample rate
   * @param sampleDepth Audio sample depth
   * @note Everything must be known at creation time.
   */
  AudioReader(Audio::ChannelLayout layout, Audio::SamplingRate sampleRate, Audio::SamplingDepth sampleDepth);

 private:
  AudioReader();
  explicit AudioReader(const AudioReader&);

  Spec spec;
};

static inline std::shared_ptr<AudioReader> getAudioSharedReader(std::shared_ptr<Reader> reader) {
  std::shared_ptr<AudioReader> audio = std::dynamic_pointer_cast<AudioReader>(reader);
  if (audio) {
    AudioReader::Spec spec = audio->getSpec();
    if (spec.layout != Audio::UNKNOWN && spec.sampleRate != Audio::SamplingRate::SR_NONE &&
        spec.sampleDepth != Audio::SamplingDepth::SD_NONE) {
      return audio;
    }
  }
  return nullptr;
}

static inline std::shared_ptr<VideoReader> getVideoSharedReader(std::shared_ptr<Reader> reader) {
  return std::dynamic_pointer_cast<VideoReader>(reader);
}

static inline std::shared_ptr<MetadataReader> getMetadataSharedReader(std::shared_ptr<Reader> reader) {
  return std::dynamic_pointer_cast<MetadataReader>(reader);
}

static inline std::shared_ptr<SinkReader> getSinkSharedReader(std::shared_ptr<Reader> reader) {
  return std::dynamic_pointer_cast<SinkReader>(reader);
}
/////////////////////////////

class VS_EXPORT MetadataReader : public virtual Reader {
 public:
  enum class MetadataReadStatusCode {
    // generic states
    Ok,
    ErrorWithStatus,
    // custom reader states
    EndOfFile,
    MoreDataAvailable,
  };

  typedef Result<MetadataReadStatusCode> MetadataReadStatus;

  /**
   * @brief A class that holds time-constant specifications.
   */
  struct VS_EXPORT Spec {
    /**
     * Constructs a spec.
     * @param framerate
     */
    explicit Spec(FrameRate framerate);

    Spec();

    /**
     * Copy from @a spec.
     */
    explicit Spec(const Spec& spec);

    ~Spec();

    /**
     * IMU frame rate
     */
    const FrameRate frameRate;

    /**
     * Returns a display name for the reader. Never assume any format for that. EVER.
     * That means that the only thing that's allowed with that is to diplay it to the user.
     * No testing for equality, parsing...
     * Thread safe.
     * @param os Sink for the display name.
     */
    void getDisplayName(std::ostream& os) const;

    /**
     * Reader implementors: use that to set the display name. You're free to put anything, and change it. Make it small.
     * @param name The display name to set.
     */
    void setDisplayName(const char* name);

   private:
    Spec& operator=(const Spec&) = delete;

    class Impl;
    Impl* const pimpl;
  };

 public:
  virtual ~MetadataReader();

  virtual Status readIMUSamples(std::vector<VideoStitch::IMU::Measure>& imuData) = 0;

  /**
   * Exposure, WhiteBalance, ToneCurve return values:
   * `Ok` if one sample was read, and there are no more samples available in the reader
   * `ErrorWithStatus` if a generic error was encountered
   * `EndOfFile` if the stream has stopped and there are no more samples available in the reader
   * `MoreDataAvailable` if one sample was read, and more data is available. the read function should be called again.
   */
  virtual MetadataReadStatus readExposure(std::map<videoreaderid_t, Metadata::Exposure>& exposure) = 0;
  virtual MetadataReadStatus readWhiteBalance(std::map<videoreaderid_t, Metadata::WhiteBalance>& whiteBalance) = 0;
  virtual MetadataReadStatus readToneCurve(std::map<videoreaderid_t, Metadata::ToneCurve>& toneCurve) = 0;

  /**
   * Returns the (const) reader spec.
   */
  const Spec& getSpec() const { return spec; }

  /**
   * Returns the reader spec.
   */
  Spec& getSpec() { return spec; }

 protected:
  /**
   * Constructor.
   * @param framerate
   * @note Everything must be known at creation time.
   */
  explicit MetadataReader(FrameRate framerate);

 private:
  MetadataReader();
  explicit MetadataReader(const MetadataReader&);

  Spec spec;
};

class VS_EXPORT SinkReader : public virtual IO::Sink<Reader> {
 protected:
  explicit SinkReader();
};
}  // namespace Input
}  // namespace VideoStitch