torchcodec 1.0.0__py3-none-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- torchcodec/__init__.py +50 -0
- torchcodec/_core/AVIOContextHolder.cpp +85 -0
- torchcodec/_core/AVIOContextHolder.h +44 -0
- torchcodec/_core/AudioCommon.cpp +101 -0
- torchcodec/_core/AudioCommon.h +48 -0
- torchcodec/_core/AudioConverter.cpp +130 -0
- torchcodec/_core/AudioConverter.h +59 -0
- torchcodec/_core/BetaCudaDeviceInterface.cpp +1635 -0
- torchcodec/_core/BetaCudaDeviceInterface.h +364 -0
- torchcodec/_core/CMakeLists.txt +661 -0
- torchcodec/_core/CUDACommon.cpp +129 -0
- torchcodec/_core/CUDACommon.h +115 -0
- torchcodec/_core/Cache.h +126 -0
- torchcodec/_core/CodecParameters.cpp +98 -0
- torchcodec/_core/CodecParameters.h +59 -0
- torchcodec/_core/ColorConverter.cpp +92 -0
- torchcodec/_core/ColorConverter.h +40 -0
- torchcodec/_core/CpuDeviceInterface.cpp +654 -0
- torchcodec/_core/CpuDeviceInterface.h +153 -0
- torchcodec/_core/CudaDeviceInterface.cpp +519 -0
- torchcodec/_core/CudaDeviceInterface.h +92 -0
- torchcodec/_core/DecodeAvif.cpp +284 -0
- torchcodec/_core/DecodeAvif.h +28 -0
- torchcodec/_core/DecodeGif.cpp +488 -0
- torchcodec/_core/DecodeGif.h +32 -0
- torchcodec/_core/DecodeHeic.cpp +306 -0
- torchcodec/_core/DecodeHeic.h +18 -0
- torchcodec/_core/DecodeJpeg.cpp +489 -0
- torchcodec/_core/DecodeJpeg.h +17 -0
- torchcodec/_core/DecodeJpegCuda.cpp +638 -0
- torchcodec/_core/DecodeJpegCuda.h +92 -0
- torchcodec/_core/DecodePng.cpp +569 -0
- torchcodec/_core/DecodePng.h +31 -0
- torchcodec/_core/DecodeWebp.cpp +238 -0
- torchcodec/_core/DecodeWebp.h +17 -0
- torchcodec/_core/Demuxer.cpp +415 -0
- torchcodec/_core/Demuxer.h +158 -0
- torchcodec/_core/DeviceInterface.cpp +152 -0
- torchcodec/_core/DeviceInterface.h +255 -0
- torchcodec/_core/EncodeJpeg.cpp +223 -0
- torchcodec/_core/EncodeJpeg.h +19 -0
- torchcodec/_core/EncodeJpegCuda.cpp +272 -0
- torchcodec/_core/EncodeJpegCuda.h +56 -0
- torchcodec/_core/EncodePng.cpp +207 -0
- torchcodec/_core/EncodePng.h +19 -0
- torchcodec/_core/Encoder.cpp +1023 -0
- torchcodec/_core/Encoder.h +144 -0
- torchcodec/_core/Exif.h +241 -0
- torchcodec/_core/FFMPEGCommon.cpp +1159 -0
- torchcodec/_core/FFMPEGCommon.h +506 -0
- torchcodec/_core/FileIO.cpp +78 -0
- torchcodec/_core/FileIO.h +34 -0
- torchcodec/_core/FileLikeIO.cpp +76 -0
- torchcodec/_core/FileLikeIO.h +68 -0
- torchcodec/_core/FilterGraph.cpp +211 -0
- torchcodec/_core/FilterGraph.h +59 -0
- torchcodec/_core/Frame.cpp +75 -0
- torchcodec/_core/Frame.h +76 -0
- torchcodec/_core/IOInterface.h +44 -0
- torchcodec/_core/ImageCommon.h +113 -0
- torchcodec/_core/Logging.cpp +42 -0
- torchcodec/_core/Logging.h +38 -0
- torchcodec/_core/Metadata.cpp +312 -0
- torchcodec/_core/Metadata.h +115 -0
- torchcodec/_core/NVCUVIDRuntimeLoader.cpp +324 -0
- torchcodec/_core/NVCUVIDRuntimeLoader.h +14 -0
- torchcodec/_core/NVDECCache.cpp +111 -0
- torchcodec/_core/NVDECCache.h +136 -0
- torchcodec/_core/NVDECCacheConfig.cpp +61 -0
- torchcodec/_core/NVDECCacheConfig.h +30 -0
- torchcodec/_core/PacketDecoder.cpp +358 -0
- torchcodec/_core/PacketDecoder.h +140 -0
- torchcodec/_core/SingleStreamDecoder.cpp +1856 -0
- torchcodec/_core/SingleStreamDecoder.h +424 -0
- torchcodec/_core/StableABICompat.h +213 -0
- torchcodec/_core/StreamOptions.h +93 -0
- torchcodec/_core/SwScale.cpp +124 -0
- torchcodec/_core/SwScale.h +67 -0
- torchcodec/_core/TensorIO.cpp +136 -0
- torchcodec/_core/TensorIO.h +53 -0
- torchcodec/_core/Transform.cpp +207 -0
- torchcodec/_core/Transform.h +125 -0
- torchcodec/_core/ValidationUtils.cpp +65 -0
- torchcodec/_core/ValidationUtils.h +29 -0
- torchcodec/_core/WavDecoder.cpp +494 -0
- torchcodec/_core/WavDecoder.h +73 -0
- torchcodec/_core/__init__.py +45 -0
- torchcodec/_core/_decoder_utils.py +258 -0
- torchcodec/_core/_ffmpeg_op_names.py +95 -0
- torchcodec/_core/_ffmpeg_ops.py +699 -0
- torchcodec/_core/_file_like.py +221 -0
- torchcodec/_core/_metadata.py +402 -0
- torchcodec/_core/color_conversion.cpp +316 -0
- torchcodec/_core/color_conversion.cu +391 -0
- torchcodec/_core/color_conversion.h +150 -0
- torchcodec/_core/custom_ops.cpp +2438 -0
- torchcodec/_core/fetch_and_expose_non_gpl_ffmpeg_libs.cmake +192 -0
- torchcodec/_core/fetch_avif_from_s3.cmake +81 -0
- torchcodec/_core/giflib/README +28 -0
- torchcodec/_core/giflib/dgif_lib.c +1313 -0
- torchcodec/_core/giflib/gif_hash.c +129 -0
- torchcodec/_core/giflib/gif_hash.h +43 -0
- torchcodec/_core/giflib/gif_lib.h +292 -0
- torchcodec/_core/giflib/gif_lib_private.h +73 -0
- torchcodec/_core/giflib/gifalloc.c +426 -0
- torchcodec/_core/giflib/openbsd-reallocarray.c +74 -0
- torchcodec/_core/heic_custom_ops.cpp +20 -0
- torchcodec/_core/image_custom_ops.cpp +137 -0
- torchcodec/_core/nvcuvid_include/cuviddec.h +1374 -0
- torchcodec/_core/nvcuvid_include/nvcuvid.h +610 -0
- torchcodec/_core/ops.py +103 -0
- torchcodec/_core/sources.bzl +113 -0
- torchcodec/_frame.py +146 -0
- torchcodec/_internally_replaced_utils.py +129 -0
- torchcodec/_logging.py +59 -0
- torchcodec/decoders/__init__.py +50 -0
- torchcodec/decoders/_audio_decoder.py +183 -0
- torchcodec/decoders/_blocks/__init__.py +48 -0
- torchcodec/decoders/_blocks/_audio_converter.py +155 -0
- torchcodec/decoders/_blocks/_codec_parameters.py +229 -0
- torchcodec/decoders/_blocks/_color_converter.py +107 -0
- torchcodec/decoders/_blocks/_demuxer.py +678 -0
- torchcodec/decoders/_blocks/_frame.py +577 -0
- torchcodec/decoders/_blocks/_helpers.py +53 -0
- torchcodec/decoders/_blocks/_packet_decoder.py +357 -0
- torchcodec/decoders/_decoder_utils.py +128 -0
- torchcodec/decoders/_image_decoders.py +1575 -0
- torchcodec/decoders/_video_decoder.py +592 -0
- torchcodec/decoders/_wav_decoder.py +131 -0
- torchcodec/encoders/__init__.py +4 -0
- torchcodec/encoders/_audio_encoder.py +167 -0
- torchcodec/encoders/_image_encoders.py +181 -0
- torchcodec/encoders/_multi_stream_encoder.py +272 -0
- torchcodec/encoders/_video_encoder.py +227 -0
- torchcodec/jpeg8.dll +0 -0
- torchcodec/libavif.dll +0 -0
- torchcodec/libpng16.dll +0 -0
- torchcodec/libsharpyuv.dll +0 -0
- torchcodec/libtorchcodec_core4.dll +0 -0
- torchcodec/libtorchcodec_core5.dll +0 -0
- torchcodec/libtorchcodec_core6.dll +0 -0
- torchcodec/libtorchcodec_core7.dll +0 -0
- torchcodec/libtorchcodec_core8.dll +0 -0
- torchcodec/libtorchcodec_core9.dll +0 -0
- torchcodec/libtorchcodec_custom_ops4.dll +0 -0
- torchcodec/libtorchcodec_custom_ops5.dll +0 -0
- torchcodec/libtorchcodec_custom_ops6.dll +0 -0
- torchcodec/libtorchcodec_custom_ops7.dll +0 -0
- torchcodec/libtorchcodec_custom_ops8.dll +0 -0
- torchcodec/libtorchcodec_custom_ops9.dll +0 -0
- torchcodec/libtorchcodec_heic.dll +0 -0
- torchcodec/libtorchcodec_image.dll +0 -0
- torchcodec/libwebp.dll +0 -0
- torchcodec/libwebpdecoder.dll +0 -0
- torchcodec/libwebpdemux.dll +0 -0
- torchcodec/libwebpmux.dll +0 -0
- torchcodec/libzstd.dll +0 -0
- torchcodec/py.typed +0 -0
- torchcodec/samplers/__init__.py +2 -0
- torchcodec/samplers/_common.py +83 -0
- torchcodec/samplers/_index_based.py +287 -0
- torchcodec/samplers/_time_based.py +360 -0
- torchcodec/share/cmake/TorchCodec/TorchCodecConfig.cmake +76 -0
- torchcodec/share/cmake/TorchCodec/ffmpeg_versions.cmake +128 -0
- torchcodec/transforms/__init__.py +12 -0
- torchcodec/transforms/_decoder_transforms.py +375 -0
- torchcodec/version.py +1 -0
- torchcodec/zlib.dll +0 -0
- torchcodec-1.0.0.dist-info/METADATA +275 -0
- torchcodec-1.0.0.dist-info/RECORD +179 -0
- torchcodec-1.0.0.dist-info/WHEEL +5 -0
- torchcodec-1.0.0.dist-info/licenses/LICENSE +28 -0
- torchcodec-1.0.0.dist-info/licenses/third_party/COPYING.dav1d +23 -0
- torchcodec-1.0.0.dist-info/licenses/third_party/LICENSE.libavif +387 -0
- torchcodec-1.0.0.dist-info/licenses/third_party/LICENSE.libjpeg-turbo +135 -0
- torchcodec-1.0.0.dist-info/licenses/third_party/LICENSE.libpng +134 -0
- torchcodec-1.0.0.dist-info/licenses/third_party/LICENSE.libwebp +30 -0
- torchcodec-1.0.0.dist-info/licenses/third_party/LICENSE.libyuv +29 -0
- torchcodec-1.0.0.dist-info/licenses/third_party/LICENSE.zlib +22 -0
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
// All rights reserved.
|
|
3
|
+
//
|
|
4
|
+
// This source code is licensed under the BSD-style license found in the
|
|
5
|
+
// LICENSE file in the root directory of this source tree.
|
|
6
|
+
|
|
7
|
+
// NVDEC CUDA device interface that provides direct control over NVDEC
|
|
8
|
+
// while keeping FFmpeg for demuxing. A lot of the logic, particularly the use
|
|
9
|
+
// of a cache for the decoders, is inspired by DALI's implementation which is
|
|
10
|
+
// APACHE 2.0:
|
|
11
|
+
// https://github.com/NVIDIA/DALI/blob/c7539676a24a8e9e99a6e8665e277363c5445259/dali/operators/video/frames_decoder_gpu.cc#L1
|
|
12
|
+
//
|
|
13
|
+
// NVDEC / NVCUVID docs:
|
|
14
|
+
// https://docs.nvidia.com/video-technologies/video-codec-sdk/13.0/nvdec-video-decoder-api-prog-guide/index.html#using-nvidia-video-decoder-nvdecode-api
|
|
15
|
+
|
|
16
|
+
#pragma once
|
|
17
|
+
|
|
18
|
+
#include "CUDACommon.h"
|
|
19
|
+
#include "Cache.h"
|
|
20
|
+
#include "DeviceInterface.h"
|
|
21
|
+
#include "FFMPEGCommon.h"
|
|
22
|
+
#include "NVDECCache.h"
|
|
23
|
+
#include "Transform.h"
|
|
24
|
+
#include "color_conversion.h"
|
|
25
|
+
|
|
26
|
+
#include <deque>
|
|
27
|
+
#include <memory>
|
|
28
|
+
#include <mutex>
|
|
29
|
+
#include <queue>
|
|
30
|
+
#include <unordered_map>
|
|
31
|
+
#include <unordered_set>
|
|
32
|
+
#include <vector>
|
|
33
|
+
|
|
34
|
+
#include "nvcuvid_include/cuviddec.h"
|
|
35
|
+
#include "nvcuvid_include/nvcuvid.h"
|
|
36
|
+
|
|
37
|
+
namespace facebook::torchcodec {
|
|
38
|
+
// The buffer a frame owns its samples in, hung off the AVFrame as opaque data.
|
|
39
|
+
struct OwnedFrameStorage {
|
|
40
|
+
torch::stable::Tensor storage;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
struct GpuFrameAndStorage {
|
|
44
|
+
UniqueAVFrame av_frame;
|
|
45
|
+
torch::stable::Tensor storage;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
class BetaCudaDeviceInterface : public DeviceInterface {
|
|
49
|
+
public:
|
|
50
|
+
explicit BetaCudaDeviceInterface(const StableDevice& device);
|
|
51
|
+
virtual ~BetaCudaDeviceInterface();
|
|
52
|
+
|
|
53
|
+
void initialize(const SharedAVCodecContext& codec_context) override;
|
|
54
|
+
|
|
55
|
+
using DeviceInterface::initialize_video_decoding;
|
|
56
|
+
void initialize_video_decoding(
|
|
57
|
+
const CodecParameters& codec_parameters,
|
|
58
|
+
const VideoStreamOptions& video_stream_options) override;
|
|
59
|
+
|
|
60
|
+
void initialize_color_conversion(
|
|
61
|
+
const VideoStreamOptions& video_stream_options,
|
|
62
|
+
const std::vector<std::unique_ptr<Transform>>& transforms,
|
|
63
|
+
const std::optional<FrameDims>& resized_output_dims) override;
|
|
64
|
+
|
|
65
|
+
OutputDtype get_pre_allocation_dtype(
|
|
66
|
+
[[maybe_unused]] OutputDtype requested_dtype) const override;
|
|
67
|
+
|
|
68
|
+
void convert_av_frame_to_frame_output(
|
|
69
|
+
const AVFrame& av_frame,
|
|
70
|
+
FrameOutput& frame_output,
|
|
71
|
+
std::optional<torch::stable::Tensor> pre_allocated_output_tensor)
|
|
72
|
+
override;
|
|
73
|
+
|
|
74
|
+
int send_packet(const AVPacket& packet) override;
|
|
75
|
+
int send_eof_packet() override;
|
|
76
|
+
int receive_frame(UniqueAVFrame& av_frame) override;
|
|
77
|
+
void flush() override;
|
|
78
|
+
|
|
79
|
+
// NVDEC callback functions (must be public for C callbacks)
|
|
80
|
+
int stream_property_change(CUVIDEOFORMAT* video_format);
|
|
81
|
+
int frame_ready_for_decoding(CUVIDPICPARAMS* pic_params);
|
|
82
|
+
int frame_ready_in_display_order(CUVIDPARSERDISPINFO* disp_info);
|
|
83
|
+
|
|
84
|
+
std::string get_details() override;
|
|
85
|
+
|
|
86
|
+
private:
|
|
87
|
+
enum class Mode { Uninitialized, DecoderOnly, ColorConverterOnly, Both };
|
|
88
|
+
Mode mode() const;
|
|
89
|
+
|
|
90
|
+
int send_cuvid_packet(CUVIDSOURCEDATAPACKET& cuvid_packet);
|
|
91
|
+
|
|
92
|
+
void send_seqhdr_packet();
|
|
93
|
+
|
|
94
|
+
void initialize_bsf(
|
|
95
|
+
const AVCodecParameters* codec_par,
|
|
96
|
+
const AVInputFormat* container_format);
|
|
97
|
+
// Apply the bitstream filter. Returns nullptr when there is no filter to
|
|
98
|
+
// apply and the packet can be sent as-is.
|
|
99
|
+
UniqueAVPacket apply_bsf(const AVPacket& packet);
|
|
100
|
+
|
|
101
|
+
CUdeviceptr previously_mapped_frame_ = 0;
|
|
102
|
+
void unmap_previous_frame();
|
|
103
|
+
|
|
104
|
+
cudaStream_t nvdec_output_stream_ = nullptr;
|
|
105
|
+
|
|
106
|
+
// Marks the point in nvdec_output_stream_ where the mapping of the
|
|
107
|
+
// currently-mapped surface was enqueued. Consumers of that surface running on
|
|
108
|
+
// another stream wait on it. Re-recorded by every mapping, which is safe:
|
|
109
|
+
// NVDEC has a single output surface, so a frame is always consumed before the
|
|
110
|
+
// next one is mapped.
|
|
111
|
+
CudaEvent nvdec_surface_ready_;
|
|
112
|
+
|
|
113
|
+
// NVDEC gives us a single output surface, so every mapped frame lives at the
|
|
114
|
+
// same address and a new mapping overwrites whatever the previous frame's
|
|
115
|
+
// consumer is reading. These track that read so the next mapping, in
|
|
116
|
+
// receive_frame(), can be ordered after it.
|
|
117
|
+
CudaEvent surface_read_done_;
|
|
118
|
+
void record_surface_read(cudaStream_t stream);
|
|
119
|
+
|
|
120
|
+
UniqueAVFrame convert_cuda_frame_to_av_frame(
|
|
121
|
+
CUdeviceptr frame_ptr,
|
|
122
|
+
unsigned int pitch,
|
|
123
|
+
const CUVIDPARSERDISPINFO& disp_info);
|
|
124
|
+
|
|
125
|
+
// Height of the surfaces NVDEC outputs, i.e. the coded height, which is
|
|
126
|
+
// taller than the frames we hand out. See Note: [NVDEC surface dimensions and
|
|
127
|
+
// cropping].
|
|
128
|
+
int surface_height() const {
|
|
129
|
+
return static_cast<int>(video_format_.coded_height);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
struct CropOffsets {
|
|
133
|
+
unsigned int luma;
|
|
134
|
+
unsigned int chroma;
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
CropOffsets get_crop_offsets(unsigned int pitch) const;
|
|
138
|
+
|
|
139
|
+
void make_frame_standalone(UniqueAVFrame& av_frame) override;
|
|
140
|
+
|
|
141
|
+
std::optional<torch::stable::Tensor> get_frame_storage(
|
|
142
|
+
const AVFrame& av_frame) const override;
|
|
143
|
+
|
|
144
|
+
GpuFrameAndStorage upload_cpu_frame_to_gpu(
|
|
145
|
+
const AVFrame& cpu_frame,
|
|
146
|
+
cudaStream_t stream);
|
|
147
|
+
|
|
148
|
+
torch::stable::Tensor copy_nvdec_surface(
|
|
149
|
+
UniqueAVFrame& av_frame,
|
|
150
|
+
cudaStream_t stream);
|
|
151
|
+
|
|
152
|
+
void apply_rotation(
|
|
153
|
+
FrameOutput& frame_output,
|
|
154
|
+
std::optional<torch::stable::Tensor> pre_allocated_output_tensor);
|
|
155
|
+
|
|
156
|
+
CUvideoparser video_parser_ = nullptr;
|
|
157
|
+
UniqueCUvideodecoder decoder_;
|
|
158
|
+
CUVIDEOFORMAT video_format_ = {};
|
|
159
|
+
CUVIDEOFORMATEX parser_ext_info_ = {};
|
|
160
|
+
|
|
161
|
+
std::deque<CUVIDPARSERDISPINFO> ready_frames_;
|
|
162
|
+
|
|
163
|
+
// Whether we track the pts ourselves, instead of relying on the NVCUVID
|
|
164
|
+
// parser.
|
|
165
|
+
bool track_pts_ourselves_ = false;
|
|
166
|
+
std::queue<int64_t>
|
|
167
|
+
pending_pts_; // only used when track_pts_ourselves_ is true
|
|
168
|
+
|
|
169
|
+
// The packets flagged AV_PKT_FLAG_DISCARD must be decoded, but their frames
|
|
170
|
+
// must not be returned (that's how libavcodec does it). We track the
|
|
171
|
+
// timestamps of those packets and drop the corresponding frames in
|
|
172
|
+
// receive_frame(). We rely on the packet's pts to identify it: it's not
|
|
173
|
+
// ideal, the pts may be non-unique or missing. But that's working so far.
|
|
174
|
+
// Unfortuntely, NVCUVID doesn't give us any other way to pass down that info.
|
|
175
|
+
std::unordered_set<CUvideotimestamp> discarded_timestamps_;
|
|
176
|
+
|
|
177
|
+
bool eof_sent_ = false;
|
|
178
|
+
|
|
179
|
+
AVRational time_base_ = {0, 1};
|
|
180
|
+
AVRational frame_rate_avg_from_ffmpeg_ = {0, 1};
|
|
181
|
+
|
|
182
|
+
UniqueAVBSFContext bitstream_filter_;
|
|
183
|
+
|
|
184
|
+
bool decoding_initialized_ = false;
|
|
185
|
+
bool color_conversion_initialized_ = false;
|
|
186
|
+
|
|
187
|
+
std::unique_ptr<DeviceInterface> cpu_interface_;
|
|
188
|
+
// Whether this instance decodes on CPU because NVDEC can't handle the stream.
|
|
189
|
+
bool decoding_on_cpu_ = false;
|
|
190
|
+
bool nvcuvid_available_ = false;
|
|
191
|
+
UniqueSwsContext sws_context_;
|
|
192
|
+
|
|
193
|
+
SwsConfig prev_sws_config_;
|
|
194
|
+
Rotation rotation_ = Rotation::NONE;
|
|
195
|
+
OutputDtype output_dtype_ = OutputDtype::UINT8;
|
|
196
|
+
cudaVideoSurfaceFormat surface_format_ = cudaVideoSurfaceFormat_NV12;
|
|
197
|
+
|
|
198
|
+
CachedColorMatrix cached_color_matrix_;
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
} // namespace facebook::torchcodec
|
|
202
|
+
|
|
203
|
+
/* clang-format off */
|
|
204
|
+
// Note: [General design, sendPacket, receiveFrame, frame ordering and NVCUVID callbacks]
|
|
205
|
+
//
|
|
206
|
+
// At a high level, this decoding interface mimics the FFmpeg send/receive
|
|
207
|
+
// architecture:
|
|
208
|
+
// - sendPacket(AVPacket) sends an AVPacket from the FFmpeg demuxer to the
|
|
209
|
+
// NVCUVID parser.
|
|
210
|
+
// - receiveFrame(AVFrame) is a non-blocking call:
|
|
211
|
+
// - if a frame is ready **in display order**, it must return it. By display
|
|
212
|
+
// order, we mean that receiveFrame() must return frames with increasing pts
|
|
213
|
+
// values when called successively.
|
|
214
|
+
// - if no frame is ready, it must return AVERROR(EAGAIN) to indicate the
|
|
215
|
+
// caller should send more packets.
|
|
216
|
+
//
|
|
217
|
+
// The rest of this note assumes you have a reasonable level of familiarity with
|
|
218
|
+
// the sendPacket/receiveFrame calling pattern. If you don't, look up the core
|
|
219
|
+
// decoding loop in SingleVideoDecoder.
|
|
220
|
+
//
|
|
221
|
+
// The frame re-ordering problem:
|
|
222
|
+
// Depending on the codec and on the encoding parameters, a packet from a video
|
|
223
|
+
// stream may contain exactly one frame, more than one frame, or a fraction of a
|
|
224
|
+
// frame. And, there may be non-linear frame dependencies because of B-frames,
|
|
225
|
+
// which need both past *and* future frames to be decoded. Consider the
|
|
226
|
+
// following stream, with frames presented in display order: I0 B1 P2 B3 P4 ...
|
|
227
|
+
// - I0 is an I-frame (also key frame, can be decoded independently)
|
|
228
|
+
// - B1 is a B-frame (bi-directional) which needs both I0 and P2 to be decoded
|
|
229
|
+
// - P2 is a P-frame (predicted frame) which only needs I0 to be decodec.
|
|
230
|
+
//
|
|
231
|
+
// Because B1 needs both I0 and P2 to be properly decoded, the decode order
|
|
232
|
+
// (packet order), defined by the encoder, must be: I0 P2 B1 P4 B3 ... which is
|
|
233
|
+
// different from the display order.
|
|
234
|
+
//
|
|
235
|
+
// SendPacket(AVPacket)'s job is just to pass down the packet to the NVCUVID
|
|
236
|
+
// parser by calling cuvidParseVideoData(packet). When
|
|
237
|
+
// cuvidParseVideoData(packet) is called, it may trigger callbacks,
|
|
238
|
+
// particularly:
|
|
239
|
+
// - streamPropertyChange(videoFormat): triggered once at the start of the
|
|
240
|
+
// stream, and possibly later if the stream properties change (e.g.
|
|
241
|
+
// resolution).
|
|
242
|
+
// - frameReadyForDecoding(picParams)): triggered **in decode order** when the
|
|
243
|
+
// parser has accumulated enough data to decode a frame. We send that frame to
|
|
244
|
+
// the NVDEC hardware for **async** decoding.
|
|
245
|
+
// - frameReadyInDisplayOrder(dispInfo)): triggered **in display order** when a
|
|
246
|
+
// frame is ready to be "displayed" (returned). At that point, the parser also
|
|
247
|
+
// gives us the pts of that frame. We store (a reference to) that frame in a
|
|
248
|
+
// FIFO queue: readyFrames_.
|
|
249
|
+
//
|
|
250
|
+
// When receiveFrame(AVFrame) is called, if readyFrames_ is not empty, we pop
|
|
251
|
+
// the front of the queue, which is the next frame in display order, and map it
|
|
252
|
+
// to an AVFrame by calling cuvidMapVideoFrame(). If readyFrames_ is empty we
|
|
253
|
+
// return EAGAIN to indicate the caller should send more packets.
|
|
254
|
+
//
|
|
255
|
+
// There is potentially a small inefficiency due to the callback design: in
|
|
256
|
+
// order for us to know that a frame is ready in display order, we need the
|
|
257
|
+
// frameReadyInDisplayOrder callback to be triggered. This can only happen
|
|
258
|
+
// within cuvidParseVideoData(packet) in sendPacket(). This means there may be
|
|
259
|
+
// the following sequence of calls:
|
|
260
|
+
//
|
|
261
|
+
// sendPacket(relevantAVPacket)
|
|
262
|
+
// cuvidParseVideoData(relevantAVPacket)
|
|
263
|
+
// frameReadyForDecoding()
|
|
264
|
+
// cuvidDecodePicture() Send frame to NVDEC for async decoding
|
|
265
|
+
//
|
|
266
|
+
// receiveFrame() -> EAGAIN Frame is potentially already decoded
|
|
267
|
+
// and could be returned, but we don't
|
|
268
|
+
// know because frameReadyInDisplayOrder
|
|
269
|
+
// hasn't been triggered yet. We'll only
|
|
270
|
+
// know after sending another,
|
|
271
|
+
// potentially irrelevant packet.
|
|
272
|
+
//
|
|
273
|
+
// sendPacket(irrelevantAVPacket)
|
|
274
|
+
// cuvidParseVideoData(irrelevantAVPacket)
|
|
275
|
+
// frameReadyInDisplayOrder() Only now do we know that our target
|
|
276
|
+
// frame is ready.
|
|
277
|
+
//
|
|
278
|
+
// receiveFrame() return target frame
|
|
279
|
+
//
|
|
280
|
+
// How much this matters in practice is unclear, but probably negligible in
|
|
281
|
+
// general. Particularly when frames are decoded consecutively anyway, the
|
|
282
|
+
// "irrelevantPacket" is actually relevant for a future target frame.
|
|
283
|
+
//
|
|
284
|
+
// Note that the alternative is to *not* rely on the frameReadyInDisplayOrder
|
|
285
|
+
// callback. It's technically possible, but it would mean we now have to solve
|
|
286
|
+
// two hard, *codec-dependent* problems that the callback was solving for us:
|
|
287
|
+
// - we have to guess the frame's pts ourselves
|
|
288
|
+
// - we have to re-order the frames ourselves to preserve display order.
|
|
289
|
+
//
|
|
290
|
+
//
|
|
291
|
+
//
|
|
292
|
+
// Note: [NVDEC surface dimensions and cropping]
|
|
293
|
+
//
|
|
294
|
+
// Different sets of dimensions are involved when decoding with NVDEC, and
|
|
295
|
+
// mixing them up silently corrupts frames:
|
|
296
|
+
//
|
|
297
|
+
// 0 32 1248 1280
|
|
298
|
+
// 0 +-----+------------------------+---+
|
|
299
|
+
// | top crop, 16 rows |
|
|
300
|
+
// 16 +-----+------------------------+---+ ---
|
|
301
|
+
// | | | | |
|
|
302
|
+
// |left | display area 1216x512 |rgt| |
|
|
303
|
+
// |crop | (what users get) |crp| | 512
|
|
304
|
+
// | 32 | | 32| |
|
|
305
|
+
// | | | | |
|
|
306
|
+
// 528 +-----+------------------------+---+ ---
|
|
307
|
+
// | bottom crop, 16 rows |
|
|
308
|
+
// 544 +----------------------------------+
|
|
309
|
+
// coded frame 1280x544
|
|
310
|
+
//
|
|
311
|
+
// Dimensions reported by the format:
|
|
312
|
+
//
|
|
313
|
+
// - The true frame dimensions of the frame that is actually being displayed,
|
|
314
|
+
// and what we return to users as a HxW frame. Those are described by the
|
|
315
|
+
// CUVIDEOFORMAT.display_area (or video_format_.display_area) fields. Codecs
|
|
316
|
+
// typically don't encode exactly those dimensions: they're padded to
|
|
317
|
+
// macroblock dims, i.e. the coded dimensions:
|
|
318
|
+
//
|
|
319
|
+
// - The *coded* dimensions: CUVIDEOFORMAT.coded_width / coded_height, which the
|
|
320
|
+
// NVCUVID parser gives us. Larger than (or equal to) the display area: a
|
|
321
|
+
// 1280x530 video is coded as 1280x544.
|
|
322
|
+
//
|
|
323
|
+
// Dimensions we choose:
|
|
324
|
+
//
|
|
325
|
+
// - There's another 'display' area, which (unsurprisingly), has a completely
|
|
326
|
+
// different meaning to the one above: it defines the area within the coded
|
|
327
|
+
// dimensions that gets scaled into the output surface. We set it in
|
|
328
|
+
// CUVIDDECODECREATEINFO.display_area when creating the decoder.
|
|
329
|
+
//
|
|
330
|
+
// - The *surface* dimensions. The surface is the buffer that
|
|
331
|
+
// cuvidMapVideoFrame() gives us for a decoded frame. Its size is whatever we
|
|
332
|
+
// asked for in CUVIDDECODECREATEINFO.ulTargetWidth / ulTargetHeight when
|
|
333
|
+
// creating the decoder, and NVDEC scales the source rectangle we also asked
|
|
334
|
+
// for, CUVIDDECODECREATEINFO.display_area, into it.
|
|
335
|
+
//
|
|
336
|
+
// In create_decoder(), at decoder creation, we set EVERYTHING to the coded
|
|
337
|
+
// dimensions:
|
|
338
|
+
//
|
|
339
|
+
// ulWidth, ulHeight <- coded_width, coded_height
|
|
340
|
+
// ulMaxWidth, ulMaxHeight <- coded_width, coded_height
|
|
341
|
+
// CUVIDDECODECREATEINFO.display_area <- the whole coded frame, i.e. no crop
|
|
342
|
+
// ulTargetWidth, ulTargetHeight <- coded_width, coded_height
|
|
343
|
+
//
|
|
344
|
+
// Which means the decoder will always fill a surface of size coded_width x
|
|
345
|
+
// coded_height, and it's up to us to crop it to the originally reported
|
|
346
|
+
// CUVIDEOFORMAT.display_area. The crop never involves a copy: just pointer /
|
|
347
|
+
// stride arithmetic to offset planes.
|
|
348
|
+
//
|
|
349
|
+
// Why do we handle the crop ourselves, when NVCUVID can do it for us (and we
|
|
350
|
+
// previously let it do it)? It's to optimize cache hits in the decoder cache.
|
|
351
|
+
// The cache key is based on the coded dimensions, not on the true display
|
|
352
|
+
// dimensions, so we can re-use a decoder for a stream that has the same coded
|
|
353
|
+
// dimensions but different display areas. If we wanted to let NVCUVID crop the
|
|
354
|
+
// surface, we'd have to add the display area to the cache key, which would
|
|
355
|
+
// enforce the exact same display area for a decoder to be re-usable, reducing
|
|
356
|
+
// cache hits.
|
|
357
|
+
//
|
|
358
|
+
// The cost for this optimized cache hit is that we handle the crop complexity
|
|
359
|
+
// ourselves. But it's 2026 and agents are good at pointer arithmetic.
|
|
360
|
+
//
|
|
361
|
+
// Finally, this crop has nothing to do with the one in convert_yuv_to_rgb()
|
|
362
|
+
// [color_conversion.cpp], which trims the output of a color-conversion kernel
|
|
363
|
+
// that ran on even-rounded dimensions.
|
|
364
|
+
/* clang-format on */
|