-
Notifications
You must be signed in to change notification settings - Fork 630
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for memory video file in FramesDecoder #4184
Changes from 4 commits
58feb8f
4ec4f2d
52ab57f
a8feec0
54a74a9
4b42178
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ extern "C" { | |
#include <vector> | ||
#include <string> | ||
#include <memory> | ||
#include <optional> | ||
|
||
#include "dali/core/common.h" | ||
|
||
|
@@ -70,6 +71,23 @@ struct AvState { | |
} | ||
}; | ||
|
||
/** | ||
* @brief Helper representing video file kept in memory. Allows reading and seeking. | ||
* | ||
*/ | ||
struct MemoryVideoFile { | ||
MemoryVideoFile(const char *data, int64_t size) | ||
: data_(data), size_(size), position_(0) {} | ||
|
||
int Read(unsigned char *buffer, int buffer_size); | ||
|
||
int64_t Seek(int64_t new_position, int origin); | ||
|
||
const char *data_; | ||
const int64_t size_; | ||
int64_t position_; | ||
}; | ||
|
||
/** | ||
* @brief Object representing a video file. Allows access to frames and seeking. | ||
* | ||
|
@@ -85,6 +103,18 @@ class DLL_PUBLIC FramesDecoder { | |
*/ | ||
explicit FramesDecoder(const std::string &filename); | ||
|
||
|
||
/** | ||
* @brief Construct a new FramesDecoder object. | ||
* | ||
* @param memory_file Pointer to memory with video file data. | ||
* @param memory_file_size Size of memory_file in bytes. | ||
* | ||
* @note This constructor assumes that the `memory_file` and | ||
* `memory_file_size` arguments cover the entire video file, including the header. | ||
*/ | ||
FramesDecoder(const char *memory_file, int memory_file_size); | ||
|
||
/** | ||
* @brief Number of frames in the video | ||
* | ||
|
@@ -215,9 +245,15 @@ class DLL_PUBLIC FramesDecoder { | |
|
||
int channels_ = 3; | ||
bool flush_state_ = false; | ||
std::string filename_; | ||
bool is_vfr_ = false; | ||
|
||
std::string filename_ = ""; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about making this string also optional? The rationale would be that when we have a video from memory it would not have a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. After the constructor we use it only to report errors to let user know which file caused the error. I refactored this code a bit, so now we have a function that returns name of the file or "memory file" instead. |
||
std::optional<MemoryVideoFile> memory_video_file_ = {}; | ||
|
||
// Default size of the buffer used to load video files from memory to FFMPEG | ||
const int default_av_buffer_size = (1 << 15); | ||
}; | ||
|
||
} // namespace dali | ||
|
||
#endif // DALI_OPERATORS_READER_LOADER_VIDEO_FRAMES_DECODER_H_ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,56 +77,67 @@ cudaVideoCodec FramesDecoderGpu::GetCodecType() { | |
return cudaVideoCodec_H264; | ||
} | ||
|
||
void FramesDecoderGpu::InitGpuDecoder() { | ||
nvdecode_state_ = std::make_unique<NvDecodeState>(); | ||
|
||
InitBitStreamFilter(); | ||
|
||
filtered_packet_ = av_packet_alloc(); | ||
DALI_ENFORCE(filtered_packet_, "Could not allocate av packet"); | ||
|
||
auto codec_type = GetCodecType(); | ||
|
||
// Create nv decoder | ||
CUVIDDECODECREATEINFO decoder_info; | ||
memset(&decoder_info, 0, sizeof(CUVIDDECODECREATEINFO)); | ||
|
||
decoder_info.bitDepthMinus8 = 0; | ||
decoder_info.ChromaFormat = cudaVideoChromaFormat_420; | ||
decoder_info.CodecType = codec_type; | ||
decoder_info.ulHeight = Height(); | ||
decoder_info.ulWidth = Width(); | ||
decoder_info.ulMaxHeight = Height(); | ||
decoder_info.ulMaxWidth = Width(); | ||
decoder_info.ulTargetHeight = Height(); | ||
decoder_info.ulTargetWidth = Width(); | ||
decoder_info.ulNumDecodeSurfaces = num_decode_surfaces_; | ||
decoder_info.ulNumOutputSurfaces = 2; | ||
|
||
CUDA_CALL(cuvidCreateDecoder(&nvdecode_state_->decoder, &decoder_info)); | ||
|
||
// Create nv parser | ||
CUVIDPARSERPARAMS parser_info; | ||
memset(&parser_info, 0, sizeof(CUVIDPARSERPARAMS)); | ||
parser_info.CodecType = codec_type; | ||
parser_info.ulMaxNumDecodeSurfaces = num_decode_surfaces_; | ||
parser_info.ulMaxDisplayDelay = 0; | ||
parser_info.pUserData = this; | ||
parser_info.pfnSequenceCallback = detail::process_video_sequence; | ||
parser_info.pfnDecodePicture = detail::process_picture_decode; | ||
parser_info.pfnDisplayPicture = nullptr; | ||
|
||
CUDA_CALL(cuvidCreateVideoParser(&nvdecode_state_->parser, &parser_info)); | ||
|
||
// Init internal frame buffer | ||
// TODO(awolant): Check, if continuous buffer would be faster | ||
for (size_t i = 0; i < frame_buffer_.size(); ++i) { | ||
frame_buffer_[i].frame_.resize(FrameSize()); | ||
frame_buffer_[i].pts_ = -1; | ||
} | ||
} | ||
|
||
Comment on lines
+80
to
+128
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added new constructor in this PR. This is a part that is common for both constructors, so I extracted it to a separate method. |
||
FramesDecoderGpu::FramesDecoderGpu(const std::string &filename, cudaStream_t stream) : | ||
FramesDecoder(filename), | ||
frame_buffer_(num_decode_surfaces_), | ||
stream_(stream) { | ||
nvdecode_state_ = std::make_unique<NvDecodeState>(); | ||
|
||
InitBitStreamFilter(); | ||
|
||
filtered_packet_ = av_packet_alloc(); | ||
DALI_ENFORCE(filtered_packet_, "Could not allocate av packet"); | ||
|
||
auto codec_type = GetCodecType(); | ||
|
||
// Create nv decoder | ||
CUVIDDECODECREATEINFO decoder_info; | ||
memset(&decoder_info, 0, sizeof(CUVIDDECODECREATEINFO)); | ||
|
||
decoder_info.bitDepthMinus8 = 0; | ||
decoder_info.ChromaFormat = cudaVideoChromaFormat_420; | ||
decoder_info.CodecType = codec_type; | ||
decoder_info.ulHeight = Height(); | ||
decoder_info.ulWidth = Width(); | ||
decoder_info.ulMaxHeight = Height(); | ||
decoder_info.ulMaxWidth = Width(); | ||
decoder_info.ulTargetHeight = Height(); | ||
decoder_info.ulTargetWidth = Width(); | ||
decoder_info.ulNumDecodeSurfaces = num_decode_surfaces_; | ||
decoder_info.ulNumOutputSurfaces = 2; | ||
|
||
CUDA_CALL(cuvidCreateDecoder(&nvdecode_state_->decoder, &decoder_info)); | ||
|
||
// Create nv parser | ||
CUVIDPARSERPARAMS parser_info; | ||
memset(&parser_info, 0, sizeof(CUVIDPARSERPARAMS)); | ||
parser_info.CodecType = codec_type; | ||
parser_info.ulMaxNumDecodeSurfaces = num_decode_surfaces_; | ||
parser_info.ulMaxDisplayDelay = 0; | ||
parser_info.pUserData = this; | ||
parser_info.pfnSequenceCallback = detail::process_video_sequence; | ||
parser_info.pfnDecodePicture = detail::process_picture_decode; | ||
parser_info.pfnDisplayPicture = nullptr; | ||
|
||
CUDA_CALL(cuvidCreateVideoParser(&nvdecode_state_->parser, &parser_info)); | ||
|
||
// Init internal frame buffer | ||
// TODO(awolant): Check, if continuous buffer would be faster | ||
for (size_t i = 0; i < frame_buffer_.size(); ++i) { | ||
frame_buffer_[i].frame_.resize(FrameSize()); | ||
frame_buffer_[i].pts_ = -1; | ||
} | ||
InitGpuDecoder(); | ||
} | ||
|
||
FramesDecoderGpu::FramesDecoderGpu(const char *memory_file, int memory_file_size, cudaStream_t stream) : | ||
FramesDecoder(memory_file, memory_file_size), | ||
frame_buffer_(num_decode_surfaces_), | ||
stream_(stream) { | ||
InitGpuDecoder(); | ||
} | ||
|
||
int FramesDecoderGpu::ProcessPictureDecode(void *user_data, CUVIDPICPARAMS *picture_params) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any short docu explaining what is the second argument?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.