video.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #pragma once
  2. #include <torch/types.h>
  3. #include "../decoder/defs.h"
  4. #include "../decoder/memory_buffer.h"
  5. #include "../decoder/sync_decoder.h"
  6. using namespace ffmpeg;
  7. namespace vision {
  8. namespace video {
  9. struct Video : torch::CustomClassHolder {
  10. std::tuple<std::string, long> current_stream; // stream type, id
  11. // global video metadata
  12. c10::Dict<std::string, c10::Dict<std::string, std::vector<double>>>
  13. streamsMetadata;
  14. int64_t numThreads_{0};
  15. public:
  16. Video(
  17. std::string videoPath = std::string(),
  18. std::string stream = std::string("video"),
  19. int64_t numThreads = 0);
  20. void initFromFile(
  21. std::string videoPath,
  22. std::string stream,
  23. int64_t numThreads);
  24. void initFromMemory(
  25. torch::Tensor videoTensor,
  26. std::string stream,
  27. int64_t numThreads);
  28. std::tuple<std::string, int64_t> getCurrentStream() const;
  29. c10::Dict<std::string, c10::Dict<std::string, std::vector<double>>>
  30. getStreamMetadata() const;
  31. void Seek(double ts, bool fastSeek);
  32. bool setCurrentStream(std::string stream);
  33. std::tuple<torch::Tensor, double> Next();
  34. private:
  35. bool succeeded = false; // decoder init flag
  36. // seekTS and doSeek act as a flag - if it's not set, next function simply
  37. // returns the next frame. If it's set, we look at the global seek
  38. // time in combination with any_frame settings
  39. double seekTS = -1;
  40. bool initialized = false;
  41. void _init(
  42. std::string stream,
  43. int64_t numThreads); // expects params.uri OR callback to be set
  44. void _getDecoderParams(
  45. double videoStartS,
  46. int64_t getPtsOnly,
  47. std::string stream,
  48. long stream_id,
  49. bool fastSeek,
  50. bool all_streams,
  51. int64_t num_threads,
  52. double seekFrameMarginUs); // this needs to be improved
  53. std::map<std::string, std::vector<double>> streamTimeBase; // not used
  54. DecoderInCallback callback = nullptr;
  55. std::vector<DecoderMetadata> metadata;
  56. protected:
  57. SyncDecoder decoder;
  58. DecoderParameters params;
  59. }; // struct Video
  60. } // namespace video
  61. } // namespace vision