video.h 2.03 KB
Newer Older
1
2
#pragma once

3
#include <torch/types.h>
4

5
6
7
#include "../decoder/defs.h"
#include "../decoder/memory_buffer.h"
#include "../decoder/sync_decoder.h"
8

9
10
11
namespace vision {
namespace video {

12
13
14
15
16
struct Video : torch::CustomClassHolder {
  std::tuple<std::string, long> current_stream; // stream type, id
  // global video metadata
  c10::Dict<std::string, c10::Dict<std::string, std::vector<double>>>
      streamsMetadata;
17
  int64_t numThreads_{0};
18
19

 public:
20
21
22
23
24
25
26
27
28
29
30
31
32
  Video(
      std::string videoPath = std::string(),
      std::string stream = std::string("video"),
      int64_t numThreads = 0);
  void initFromFile(
      std::string videoPath,
      std::string stream,
      int64_t numThreads);
  void initFromMemory(
      torch::Tensor videoTensor,
      std::string stream,
      int64_t numThreads);

33
34
35
  std::tuple<std::string, int64_t> getCurrentStream() const;
  c10::Dict<std::string, c10::Dict<std::string, std::vector<double>>>
  getStreamMetadata() const;
36
  void Seek(double ts, bool fastSeek);
37
  bool setCurrentStream(std::string stream);
38
39
40
41
42
  std::tuple<torch::Tensor, double> Next();

 private:
  bool succeeded = false; // decoder init flag
  // seekTS and doSeek act as a flag - if it's not set, next function simply
43
44
  // returns the next frame. If it's set, we look at the global seek
  // time in combination with any_frame settings
45
46
  double seekTS = -1;

47
48
49
50
51
52
  bool initialized = false;

  void _init(
      std::string stream,
      int64_t numThreads); // expects params.uri OR callback to be set

53
54
55
56
  void _getDecoderParams(
      double videoStartS,
      int64_t getPtsOnly,
      std::string stream,
57
      long stream_id,
58
      bool fastSeek,
59
      bool all_streams,
60
      int64_t num_threads,
61
      double seekFrameMarginUs); // this needs to be improved
62
63
64

  std::map<std::string, std::vector<double>> streamTimeBase; // not used

65
66
  ffmpeg::DecoderInCallback callback = nullptr;
  std::vector<ffmpeg::DecoderMetadata> metadata;
67
68

 protected:
69
70
  ffmpeg::SyncDecoder decoder;
  ffmpeg::DecoderParameters params;
71
72

}; // struct Video
73
74
75

} // namespace video
} // namespace vision