torchcodec 0.7.0__cp310-cp310-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.
Potentially problematic release.
This version of torchcodec might be problematic. Click here for more details.
- torchcodec/__init__.py +16 -0
- torchcodec/_core/AVIOContextHolder.cpp +60 -0
- torchcodec/_core/AVIOContextHolder.h +64 -0
- torchcodec/_core/AVIOFileLikeContext.cpp +98 -0
- torchcodec/_core/AVIOFileLikeContext.h +55 -0
- torchcodec/_core/AVIOTensorContext.cpp +123 -0
- torchcodec/_core/AVIOTensorContext.h +43 -0
- torchcodec/_core/CMakeLists.txt +292 -0
- torchcodec/_core/Cache.h +138 -0
- torchcodec/_core/CpuDeviceInterface.cpp +266 -0
- torchcodec/_core/CpuDeviceInterface.h +70 -0
- torchcodec/_core/CudaDeviceInterface.cpp +514 -0
- torchcodec/_core/CudaDeviceInterface.h +37 -0
- torchcodec/_core/DeviceInterface.cpp +79 -0
- torchcodec/_core/DeviceInterface.h +67 -0
- torchcodec/_core/Encoder.cpp +514 -0
- torchcodec/_core/Encoder.h +123 -0
- torchcodec/_core/FFMPEGCommon.cpp +421 -0
- torchcodec/_core/FFMPEGCommon.h +227 -0
- torchcodec/_core/FilterGraph.cpp +142 -0
- torchcodec/_core/FilterGraph.h +45 -0
- torchcodec/_core/Frame.cpp +32 -0
- torchcodec/_core/Frame.h +118 -0
- torchcodec/_core/Metadata.h +72 -0
- torchcodec/_core/SingleStreamDecoder.cpp +1715 -0
- torchcodec/_core/SingleStreamDecoder.h +380 -0
- torchcodec/_core/StreamOptions.h +53 -0
- torchcodec/_core/ValidationUtils.cpp +35 -0
- torchcodec/_core/ValidationUtils.h +21 -0
- torchcodec/_core/__init__.py +40 -0
- torchcodec/_core/_metadata.py +317 -0
- torchcodec/_core/custom_ops.cpp +727 -0
- torchcodec/_core/fetch_and_expose_non_gpl_ffmpeg_libs.cmake +300 -0
- torchcodec/_core/ops.py +455 -0
- torchcodec/_core/pybind_ops.cpp +87 -0
- torchcodec/_frame.py +145 -0
- torchcodec/_internally_replaced_utils.py +67 -0
- torchcodec/_samplers/__init__.py +7 -0
- torchcodec/_samplers/video_clip_sampler.py +430 -0
- torchcodec/decoders/__init__.py +11 -0
- torchcodec/decoders/_audio_decoder.py +177 -0
- torchcodec/decoders/_decoder_utils.py +52 -0
- torchcodec/decoders/_video_decoder.py +464 -0
- torchcodec/encoders/__init__.py +1 -0
- torchcodec/encoders/_audio_encoder.py +150 -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_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_pybind_ops4.pyd +0 -0
- torchcodec/libtorchcodec_pybind_ops5.pyd +0 -0
- torchcodec/libtorchcodec_pybind_ops6.pyd +0 -0
- torchcodec/libtorchcodec_pybind_ops7.pyd +0 -0
- torchcodec/samplers/__init__.py +2 -0
- torchcodec/samplers/_common.py +84 -0
- torchcodec/samplers/_index_based.py +287 -0
- torchcodec/samplers/_time_based.py +350 -0
- torchcodec/version.py +2 -0
- torchcodec-0.7.0.dist-info/METADATA +242 -0
- torchcodec-0.7.0.dist-info/RECORD +67 -0
- torchcodec-0.7.0.dist-info/WHEEL +5 -0
- torchcodec-0.7.0.dist-info/licenses/LICENSE +28 -0
- torchcodec-0.7.0.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,380 @@
|
|
|
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
|
+
#pragma once
|
|
8
|
+
|
|
9
|
+
#include <torch/types.h>
|
|
10
|
+
#include <cstdint>
|
|
11
|
+
#include <memory>
|
|
12
|
+
#include <ostream>
|
|
13
|
+
#include <string_view>
|
|
14
|
+
|
|
15
|
+
#include "src/torchcodec/_core/AVIOContextHolder.h"
|
|
16
|
+
#include "src/torchcodec/_core/DeviceInterface.h"
|
|
17
|
+
#include "src/torchcodec/_core/FFMPEGCommon.h"
|
|
18
|
+
#include "src/torchcodec/_core/Frame.h"
|
|
19
|
+
#include "src/torchcodec/_core/StreamOptions.h"
|
|
20
|
+
|
|
21
|
+
namespace facebook::torchcodec {
|
|
22
|
+
|
|
23
|
+
// The SingleStreamDecoder class can be used to decode video frames to Tensors.
|
|
24
|
+
// Note that SingleStreamDecoder is not thread-safe.
|
|
25
|
+
// Do not call non-const APIs concurrently on the same object.
|
|
26
|
+
class SingleStreamDecoder {
|
|
27
|
+
public:
|
|
28
|
+
// --------------------------------------------------------------------------
|
|
29
|
+
// CONSTRUCTION API
|
|
30
|
+
// --------------------------------------------------------------------------
|
|
31
|
+
|
|
32
|
+
enum class SeekMode { exact, approximate, custom_frame_mappings };
|
|
33
|
+
|
|
34
|
+
// Creates a SingleStreamDecoder from the video at videoFilePath.
|
|
35
|
+
explicit SingleStreamDecoder(
|
|
36
|
+
const std::string& videoFilePath,
|
|
37
|
+
SeekMode seekMode = SeekMode::exact);
|
|
38
|
+
|
|
39
|
+
// Creates a SingleStreamDecoder using the provided AVIOContext inside the
|
|
40
|
+
// AVIOContextHolder. The AVIOContextHolder is the base class, and the
|
|
41
|
+
// derived class will have specialized how the custom read, seek and writes
|
|
42
|
+
// work.
|
|
43
|
+
explicit SingleStreamDecoder(
|
|
44
|
+
std::unique_ptr<AVIOContextHolder> context,
|
|
45
|
+
SeekMode seekMode = SeekMode::exact);
|
|
46
|
+
|
|
47
|
+
// --------------------------------------------------------------------------
|
|
48
|
+
// VIDEO METADATA QUERY API
|
|
49
|
+
// --------------------------------------------------------------------------
|
|
50
|
+
|
|
51
|
+
// Updates the metadata of the video to accurate values obtained by scanning
|
|
52
|
+
// the contents of the video file. Also updates each StreamInfo's index, i.e.
|
|
53
|
+
// the allFrames and keyFrames vectors.
|
|
54
|
+
void scanFileAndUpdateMetadataAndIndex();
|
|
55
|
+
|
|
56
|
+
// Sorts the keyFrames and allFrames vectors in each StreamInfo by pts.
|
|
57
|
+
void sortAllFrames();
|
|
58
|
+
|
|
59
|
+
// Returns the metadata for the container.
|
|
60
|
+
ContainerMetadata getContainerMetadata() const;
|
|
61
|
+
|
|
62
|
+
// Returns the key frame indices as a tensor. The tensor is 1D and contains
|
|
63
|
+
// int64 values, where each value is the frame index for a key frame.
|
|
64
|
+
torch::Tensor getKeyFrameIndices();
|
|
65
|
+
|
|
66
|
+
// FrameMappings is used for the custom_frame_mappings seek mode to store
|
|
67
|
+
// metadata of frames in a stream. The size of all tensors in this struct must
|
|
68
|
+
// match.
|
|
69
|
+
|
|
70
|
+
// --------------------------------------------------------------------------
|
|
71
|
+
// ADDING STREAMS API
|
|
72
|
+
// --------------------------------------------------------------------------
|
|
73
|
+
struct FrameMappings {
|
|
74
|
+
// 1D tensor of int64, each value is the PTS of a frame in timebase units.
|
|
75
|
+
torch::Tensor all_frames;
|
|
76
|
+
// 1D tensor of bool, each value indicates if the corresponding frame in
|
|
77
|
+
// all_frames is a key frame.
|
|
78
|
+
torch::Tensor is_key_frame;
|
|
79
|
+
// 1D tensor of int64, each value is the duration of the corresponding frame
|
|
80
|
+
// in all_frames in timebase units.
|
|
81
|
+
torch::Tensor duration;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
void addVideoStream(
|
|
85
|
+
int streamIndex,
|
|
86
|
+
const VideoStreamOptions& videoStreamOptions = VideoStreamOptions(),
|
|
87
|
+
std::optional<FrameMappings> customFrameMappings = std::nullopt);
|
|
88
|
+
void addAudioStream(
|
|
89
|
+
int streamIndex,
|
|
90
|
+
const AudioStreamOptions& audioStreamOptions = AudioStreamOptions());
|
|
91
|
+
|
|
92
|
+
// --------------------------------------------------------------------------
|
|
93
|
+
// DECODING AND SEEKING APIs
|
|
94
|
+
// --------------------------------------------------------------------------
|
|
95
|
+
|
|
96
|
+
// Places the cursor at the first frame on or after the position in seconds.
|
|
97
|
+
// Calling getNextFrame() will return the first frame at
|
|
98
|
+
// or after this position.
|
|
99
|
+
void setCursorPtsInSeconds(double seconds);
|
|
100
|
+
|
|
101
|
+
// Decodes the frame where the current cursor position is. It also advances
|
|
102
|
+
// the cursor to the next frame.
|
|
103
|
+
FrameOutput getNextFrame();
|
|
104
|
+
|
|
105
|
+
FrameOutput getFrameAtIndex(int64_t frameIndex);
|
|
106
|
+
|
|
107
|
+
// Returns frames at the given indices for a given stream as a single stacked
|
|
108
|
+
// Tensor.
|
|
109
|
+
FrameBatchOutput getFramesAtIndices(const std::vector<int64_t>& frameIndices);
|
|
110
|
+
|
|
111
|
+
// Returns frames within a given range. The range is defined by [start, stop).
|
|
112
|
+
// The values retrieved from the range are: [start, start+step,
|
|
113
|
+
// start+(2*step), start+(3*step), ..., stop). The default for step is 1.
|
|
114
|
+
FrameBatchOutput getFramesInRange(int64_t start, int64_t stop, int64_t step);
|
|
115
|
+
|
|
116
|
+
// Decodes the first frame in any added stream that is visible at a given
|
|
117
|
+
// timestamp. Frames in the video have a presentation timestamp and a
|
|
118
|
+
// duration. For example, if a frame has presentation timestamp of 5.0s and a
|
|
119
|
+
// duration of 1.0s, it will be visible in the timestamp range [5.0, 6.0).
|
|
120
|
+
// i.e. it will be returned when this function is called with seconds=5.0 or
|
|
121
|
+
// seconds=5.999, etc.
|
|
122
|
+
FrameOutput getFramePlayedAt(double seconds);
|
|
123
|
+
|
|
124
|
+
FrameBatchOutput getFramesPlayedAt(const std::vector<double>& timestamps);
|
|
125
|
+
|
|
126
|
+
// Returns frames within a given pts range. The range is defined by
|
|
127
|
+
// [startSeconds, stopSeconds) with respect to the pts values for frames. The
|
|
128
|
+
// returned frames are in pts order.
|
|
129
|
+
//
|
|
130
|
+
// Note that while stopSeconds is excluded in the half open range, this really
|
|
131
|
+
// only makes a difference when stopSeconds is exactly the pts value for a
|
|
132
|
+
// frame. Otherwise, the moment in time immediately before stopSeconds is in
|
|
133
|
+
// the range, and that time maps to the same frame as stopSeconds.
|
|
134
|
+
//
|
|
135
|
+
// The frames returned are the frames that would be played by our abstract
|
|
136
|
+
// player. Our abstract player displays frames based on pts only. It displays
|
|
137
|
+
// frame i starting at the pts for frame i, and stops at the pts for frame
|
|
138
|
+
// i+1. This model ignores a frame's reported duration.
|
|
139
|
+
//
|
|
140
|
+
// Valid values for startSeconds and stopSeconds are:
|
|
141
|
+
//
|
|
142
|
+
// [beginStreamPtsSecondsFromContent, endStreamPtsSecondsFromContent)
|
|
143
|
+
FrameBatchOutput getFramesPlayedInRange(
|
|
144
|
+
double startSeconds,
|
|
145
|
+
double stopSeconds);
|
|
146
|
+
|
|
147
|
+
AudioFramesOutput getFramesPlayedInRangeAudio(
|
|
148
|
+
double startSeconds,
|
|
149
|
+
std::optional<double> stopSecondsOptional = std::nullopt);
|
|
150
|
+
|
|
151
|
+
class EndOfFileException : public std::runtime_error {
|
|
152
|
+
public:
|
|
153
|
+
explicit EndOfFileException(const std::string& msg)
|
|
154
|
+
: std::runtime_error(msg) {}
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
// --------------------------------------------------------------------------
|
|
158
|
+
// MORALLY PRIVATE APIS
|
|
159
|
+
// --------------------------------------------------------------------------
|
|
160
|
+
// These are APIs that should be private, but that are effectively exposed for
|
|
161
|
+
// practical reasons, typically for testing purposes.
|
|
162
|
+
|
|
163
|
+
// Once getFrameAtIndex supports the preAllocatedOutputTensor parameter, we
|
|
164
|
+
// can move it back to private.
|
|
165
|
+
FrameOutput getFrameAtIndexInternal(
|
|
166
|
+
int64_t frameIndex,
|
|
167
|
+
std::optional<torch::Tensor> preAllocatedOutputTensor = std::nullopt);
|
|
168
|
+
|
|
169
|
+
// Exposed for _test_frame_pts_equality, which is used to test non-regression
|
|
170
|
+
// of pts resolution (64 to 32 bit floats)
|
|
171
|
+
double getPtsSecondsForFrame(int64_t frameIndex);
|
|
172
|
+
|
|
173
|
+
// Exposed for performance testing.
|
|
174
|
+
struct DecodeStats {
|
|
175
|
+
int64_t numSeeksAttempted = 0;
|
|
176
|
+
int64_t numSeeksDone = 0;
|
|
177
|
+
int64_t numSeeksSkipped = 0;
|
|
178
|
+
int64_t numPacketsRead = 0;
|
|
179
|
+
int64_t numPacketsSentToDecoder = 0;
|
|
180
|
+
int64_t numFramesReceivedByDecoder = 0;
|
|
181
|
+
int64_t numFlushes = 0;
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
DecodeStats getDecodeStats() const;
|
|
185
|
+
void resetDecodeStats();
|
|
186
|
+
|
|
187
|
+
private:
|
|
188
|
+
// --------------------------------------------------------------------------
|
|
189
|
+
// STREAMINFO AND ASSOCIATED STRUCTS
|
|
190
|
+
// --------------------------------------------------------------------------
|
|
191
|
+
|
|
192
|
+
struct FrameInfo {
|
|
193
|
+
int64_t pts = 0;
|
|
194
|
+
|
|
195
|
+
// The value of the nextPts default is important: the last frame's nextPts
|
|
196
|
+
// will be INT64_MAX, which ensures that the allFrames vec contains
|
|
197
|
+
// FrameInfo structs with *increasing* nextPts values. That's a necessary
|
|
198
|
+
// condition for the binary searches on those values to work properly (as
|
|
199
|
+
// typically done during pts -> index conversions).
|
|
200
|
+
// TODO: This field is unset (left to the default) for entries in the
|
|
201
|
+
// keyFrames vec!
|
|
202
|
+
int64_t nextPts = INT64_MAX;
|
|
203
|
+
|
|
204
|
+
// Note that frameIndex is ALWAYS the index into all of the frames in that
|
|
205
|
+
// stream, even when the FrameInfo is part of the key frame index. Given a
|
|
206
|
+
// FrameInfo for a key frame, the frameIndex allows us to know which frame
|
|
207
|
+
// that is in the stream.
|
|
208
|
+
int64_t frameIndex = 0;
|
|
209
|
+
|
|
210
|
+
// Indicates whether a frame is a key frame. It may appear redundant as it's
|
|
211
|
+
// only true for FrameInfos in the keyFrames index, but it is needed to
|
|
212
|
+
// correctly map frames between allFrames and keyFrames during the scan.
|
|
213
|
+
bool isKeyFrame = false;
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
struct StreamInfo {
|
|
217
|
+
int streamIndex = -1;
|
|
218
|
+
AVStream* stream = nullptr;
|
|
219
|
+
AVMediaType avMediaType = AVMEDIA_TYPE_UNKNOWN;
|
|
220
|
+
|
|
221
|
+
AVRational timeBase = {};
|
|
222
|
+
UniqueAVCodecContext codecContext;
|
|
223
|
+
|
|
224
|
+
// The FrameInfo indices we built when scanFileAndUpdateMetadataAndIndex was
|
|
225
|
+
// called.
|
|
226
|
+
std::vector<FrameInfo> keyFrames;
|
|
227
|
+
std::vector<FrameInfo> allFrames;
|
|
228
|
+
|
|
229
|
+
// TODO since the decoder is single-stream, these should be decoder fields,
|
|
230
|
+
// not streamInfo fields. And they should be defined right next to
|
|
231
|
+
// `cursor_`, with joint documentation.
|
|
232
|
+
int64_t lastDecodedAvFramePts = 0;
|
|
233
|
+
int64_t lastDecodedAvFrameDuration = 0;
|
|
234
|
+
VideoStreamOptions videoStreamOptions;
|
|
235
|
+
AudioStreamOptions audioStreamOptions;
|
|
236
|
+
|
|
237
|
+
// color-conversion fields. Only one of FilterGraphContext and
|
|
238
|
+
// UniqueSwsContext should be non-null.
|
|
239
|
+
UniqueSwrContext swrContext;
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
// --------------------------------------------------------------------------
|
|
243
|
+
// INITIALIZERS
|
|
244
|
+
// --------------------------------------------------------------------------
|
|
245
|
+
|
|
246
|
+
void initializeDecoder();
|
|
247
|
+
|
|
248
|
+
// Reads the user provided frame index and updates each StreamInfo's index,
|
|
249
|
+
// i.e. the allFrames and keyFrames vectors, and
|
|
250
|
+
// endStreamPtsSecondsFromContent
|
|
251
|
+
void readCustomFrameMappingsUpdateMetadataAndIndex(
|
|
252
|
+
int streamIndex,
|
|
253
|
+
FrameMappings customFrameMappings);
|
|
254
|
+
// --------------------------------------------------------------------------
|
|
255
|
+
// DECODING APIS AND RELATED UTILS
|
|
256
|
+
// --------------------------------------------------------------------------
|
|
257
|
+
|
|
258
|
+
void setCursor(int64_t pts);
|
|
259
|
+
void setCursor(double) = delete; // prevent calls with doubles and floats
|
|
260
|
+
bool canWeAvoidSeeking() const;
|
|
261
|
+
|
|
262
|
+
void maybeSeekToBeforeDesiredPts();
|
|
263
|
+
|
|
264
|
+
UniqueAVFrame decodeAVFrame(
|
|
265
|
+
std::function<bool(const UniqueAVFrame&)> filterFunction);
|
|
266
|
+
|
|
267
|
+
FrameOutput getNextFrameInternal(
|
|
268
|
+
std::optional<torch::Tensor> preAllocatedOutputTensor = std::nullopt);
|
|
269
|
+
|
|
270
|
+
torch::Tensor maybePermuteHWC2CHW(torch::Tensor& hwcTensor);
|
|
271
|
+
|
|
272
|
+
FrameOutput convertAVFrameToFrameOutput(
|
|
273
|
+
UniqueAVFrame& avFrame,
|
|
274
|
+
std::optional<torch::Tensor> preAllocatedOutputTensor = std::nullopt);
|
|
275
|
+
|
|
276
|
+
void convertAVFrameToFrameOutputOnCPU(
|
|
277
|
+
UniqueAVFrame& avFrame,
|
|
278
|
+
FrameOutput& frameOutput,
|
|
279
|
+
std::optional<torch::Tensor> preAllocatedOutputTensor = std::nullopt);
|
|
280
|
+
|
|
281
|
+
void convertAudioAVFrameToFrameOutputOnCPU(
|
|
282
|
+
UniqueAVFrame& srcAVFrame,
|
|
283
|
+
FrameOutput& frameOutput);
|
|
284
|
+
|
|
285
|
+
torch::Tensor convertAVFrameToTensorUsingFilterGraph(
|
|
286
|
+
const UniqueAVFrame& avFrame);
|
|
287
|
+
|
|
288
|
+
int convertAVFrameToTensorUsingSwsScale(
|
|
289
|
+
const UniqueAVFrame& avFrame,
|
|
290
|
+
torch::Tensor& outputTensor);
|
|
291
|
+
|
|
292
|
+
std::optional<torch::Tensor> maybeFlushSwrBuffers();
|
|
293
|
+
|
|
294
|
+
// --------------------------------------------------------------------------
|
|
295
|
+
// PTS <-> INDEX CONVERSIONS
|
|
296
|
+
// --------------------------------------------------------------------------
|
|
297
|
+
|
|
298
|
+
int getKeyFrameIndexForPts(int64_t pts) const;
|
|
299
|
+
|
|
300
|
+
// Returns the key frame index of the presentation timestamp using our index.
|
|
301
|
+
// We build this index by scanning the file in
|
|
302
|
+
// scanFileAndUpdateMetadataAndIndex
|
|
303
|
+
int getKeyFrameIndexForPtsUsingScannedIndex(
|
|
304
|
+
const std::vector<SingleStreamDecoder::FrameInfo>& keyFrames,
|
|
305
|
+
int64_t pts) const;
|
|
306
|
+
|
|
307
|
+
int64_t secondsToIndexLowerBound(double seconds);
|
|
308
|
+
|
|
309
|
+
int64_t secondsToIndexUpperBound(double seconds);
|
|
310
|
+
|
|
311
|
+
int64_t getPts(int64_t frameIndex);
|
|
312
|
+
|
|
313
|
+
// --------------------------------------------------------------------------
|
|
314
|
+
// STREAM AND METADATA APIS
|
|
315
|
+
// --------------------------------------------------------------------------
|
|
316
|
+
|
|
317
|
+
void addStream(
|
|
318
|
+
int streamIndex,
|
|
319
|
+
AVMediaType mediaType,
|
|
320
|
+
const torch::Device& device = torch::kCPU,
|
|
321
|
+
std::optional<int> ffmpegThreadCount = std::nullopt);
|
|
322
|
+
|
|
323
|
+
// Returns the "best" stream index for a given media type. The "best" is
|
|
324
|
+
// determined by various heuristics in FFMPEG.
|
|
325
|
+
// See
|
|
326
|
+
// https://ffmpeg.org/doxygen/trunk/group__lavf__decoding.html#ga757780d38f482deb4d809c6c521fbcc2
|
|
327
|
+
// for more details about the heuristics.
|
|
328
|
+
// Returns the key frame index of the presentation timestamp using FFMPEG's
|
|
329
|
+
// index. Note that this index may be truncated for some files.
|
|
330
|
+
int getBestStreamIndex(AVMediaType mediaType);
|
|
331
|
+
|
|
332
|
+
std::optional<int64_t> getNumFrames(const StreamMetadata& streamMetadata);
|
|
333
|
+
double getMinSeconds(const StreamMetadata& streamMetadata);
|
|
334
|
+
std::optional<double> getMaxSeconds(const StreamMetadata& streamMetadata);
|
|
335
|
+
|
|
336
|
+
// --------------------------------------------------------------------------
|
|
337
|
+
// VALIDATION UTILS
|
|
338
|
+
// --------------------------------------------------------------------------
|
|
339
|
+
|
|
340
|
+
void validateActiveStream(
|
|
341
|
+
std::optional<AVMediaType> avMediaType = std::nullopt);
|
|
342
|
+
void validateScannedAllStreams(const std::string& msg);
|
|
343
|
+
void validateFrameIndex(
|
|
344
|
+
const StreamMetadata& streamMetadata,
|
|
345
|
+
int64_t frameIndex);
|
|
346
|
+
|
|
347
|
+
// --------------------------------------------------------------------------
|
|
348
|
+
// ATTRIBUTES
|
|
349
|
+
// --------------------------------------------------------------------------
|
|
350
|
+
|
|
351
|
+
SeekMode seekMode_;
|
|
352
|
+
ContainerMetadata containerMetadata_;
|
|
353
|
+
UniqueDecodingAVFormatContext formatContext_;
|
|
354
|
+
std::unique_ptr<DeviceInterface> deviceInterface_;
|
|
355
|
+
std::map<int, StreamInfo> streamInfos_;
|
|
356
|
+
const int NO_ACTIVE_STREAM = -2;
|
|
357
|
+
int activeStreamIndex_ = NO_ACTIVE_STREAM;
|
|
358
|
+
|
|
359
|
+
bool cursorWasJustSet_ = false;
|
|
360
|
+
// The desired position of the cursor in the stream. We send frames >= this
|
|
361
|
+
// pts to the user when they request a frame.
|
|
362
|
+
int64_t cursor_ = INT64_MIN;
|
|
363
|
+
// Stores various internal decoding stats.
|
|
364
|
+
DecodeStats decodeStats_;
|
|
365
|
+
// Stores the AVIOContext for the input buffer.
|
|
366
|
+
std::unique_ptr<AVIOContextHolder> avioContextHolder_;
|
|
367
|
+
// Whether or not we have already scanned all streams to update the metadata.
|
|
368
|
+
bool scannedAllStreams_ = false;
|
|
369
|
+
// Tracks that we've already been initialized.
|
|
370
|
+
bool initialized_ = false;
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
// Prints the SingleStreamDecoder::DecodeStats to the ostream.
|
|
374
|
+
std::ostream& operator<<(
|
|
375
|
+
std::ostream& os,
|
|
376
|
+
const SingleStreamDecoder::DecodeStats& stats);
|
|
377
|
+
|
|
378
|
+
SingleStreamDecoder::SeekMode seekModeFromString(std::string_view seekMode);
|
|
379
|
+
|
|
380
|
+
} // namespace facebook::torchcodec
|
|
@@ -0,0 +1,53 @@
|
|
|
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
|
+
#pragma once
|
|
8
|
+
|
|
9
|
+
#include <torch/types.h>
|
|
10
|
+
#include <optional>
|
|
11
|
+
#include <string>
|
|
12
|
+
|
|
13
|
+
namespace facebook::torchcodec {
|
|
14
|
+
|
|
15
|
+
enum ColorConversionLibrary {
|
|
16
|
+
// TODO: Add an AUTO option later.
|
|
17
|
+
// Use the libavfilter library for color conversion.
|
|
18
|
+
FILTERGRAPH,
|
|
19
|
+
// Use the libswscale library for color conversion.
|
|
20
|
+
SWSCALE
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
struct VideoStreamOptions {
|
|
24
|
+
VideoStreamOptions() {}
|
|
25
|
+
|
|
26
|
+
// Number of threads we pass to FFMPEG for decoding.
|
|
27
|
+
// 0 means FFMPEG will choose the number of threads automatically to fully
|
|
28
|
+
// utilize all cores. If not set, it will be the default FFMPEG behavior for
|
|
29
|
+
// the given codec.
|
|
30
|
+
std::optional<int> ffmpegThreadCount;
|
|
31
|
+
// Currently the dimension order can be either NHWC or NCHW.
|
|
32
|
+
// H=height, W=width, C=channel.
|
|
33
|
+
std::string dimensionOrder = "NCHW";
|
|
34
|
+
// The output height and width of the frame. If not specified, the output
|
|
35
|
+
// is the same as the original video.
|
|
36
|
+
std::optional<int> width;
|
|
37
|
+
std::optional<int> height;
|
|
38
|
+
std::optional<ColorConversionLibrary> colorConversionLibrary;
|
|
39
|
+
// By default we use CPU for decoding for both C++ and python users.
|
|
40
|
+
torch::Device device = torch::kCPU;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
struct AudioStreamOptions {
|
|
44
|
+
AudioStreamOptions() {}
|
|
45
|
+
|
|
46
|
+
// Encoding only
|
|
47
|
+
std::optional<int> bitRate;
|
|
48
|
+
// Decoding and encoding:
|
|
49
|
+
std::optional<int> numChannels;
|
|
50
|
+
std::optional<int> sampleRate;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
} // namespace facebook::torchcodec
|
|
@@ -0,0 +1,35 @@
|
|
|
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
|
+
#include "src/torchcodec/_core/ValidationUtils.h"
|
|
8
|
+
#include <limits>
|
|
9
|
+
#include "c10/util/Exception.h"
|
|
10
|
+
|
|
11
|
+
namespace facebook::torchcodec {
|
|
12
|
+
|
|
13
|
+
int validateInt64ToInt(int64_t value, const std::string& parameterName) {
|
|
14
|
+
TORCH_CHECK(
|
|
15
|
+
value >= std::numeric_limits<int>::min() &&
|
|
16
|
+
value <= std::numeric_limits<int>::max(),
|
|
17
|
+
parameterName,
|
|
18
|
+
"=",
|
|
19
|
+
value,
|
|
20
|
+
" is out of range for int type.");
|
|
21
|
+
|
|
22
|
+
return static_cast<int>(value);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
std::optional<int> validateOptionalInt64ToInt(
|
|
26
|
+
const std::optional<int64_t>& value,
|
|
27
|
+
const std::string& parameterName) {
|
|
28
|
+
if (value.has_value()) {
|
|
29
|
+
return validateInt64ToInt(value.value(), parameterName);
|
|
30
|
+
} else {
|
|
31
|
+
return std::nullopt;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
} // namespace facebook::torchcodec
|
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
#pragma once
|
|
8
|
+
|
|
9
|
+
#include <cstdint>
|
|
10
|
+
#include <optional>
|
|
11
|
+
#include <string>
|
|
12
|
+
|
|
13
|
+
namespace facebook::torchcodec {
|
|
14
|
+
|
|
15
|
+
int validateInt64ToInt(int64_t value, const std::string& parameterName);
|
|
16
|
+
|
|
17
|
+
std::optional<int> validateOptionalInt64ToInt(
|
|
18
|
+
const std::optional<int64_t>& value,
|
|
19
|
+
const std::string& parameterName);
|
|
20
|
+
|
|
21
|
+
} // namespace facebook::torchcodec
|
|
@@ -0,0 +1,40 @@
|
|
|
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
|
+
|
|
8
|
+
from ._metadata import (
|
|
9
|
+
AudioStreamMetadata,
|
|
10
|
+
ContainerMetadata,
|
|
11
|
+
get_container_metadata,
|
|
12
|
+
get_container_metadata_from_header,
|
|
13
|
+
VideoStreamMetadata,
|
|
14
|
+
)
|
|
15
|
+
from .ops import (
|
|
16
|
+
_add_video_stream,
|
|
17
|
+
_get_key_frame_indices,
|
|
18
|
+
_test_frame_pts_equality,
|
|
19
|
+
add_audio_stream,
|
|
20
|
+
add_video_stream,
|
|
21
|
+
create_from_bytes,
|
|
22
|
+
create_from_file,
|
|
23
|
+
create_from_file_like,
|
|
24
|
+
create_from_tensor,
|
|
25
|
+
encode_audio_to_file,
|
|
26
|
+
encode_audio_to_file_like,
|
|
27
|
+
encode_audio_to_tensor,
|
|
28
|
+
get_ffmpeg_library_versions,
|
|
29
|
+
get_frame_at_index,
|
|
30
|
+
get_frame_at_pts,
|
|
31
|
+
get_frames_at_indices,
|
|
32
|
+
get_frames_by_pts,
|
|
33
|
+
get_frames_by_pts_in_range,
|
|
34
|
+
get_frames_by_pts_in_range_audio,
|
|
35
|
+
get_frames_in_range,
|
|
36
|
+
get_json_metadata,
|
|
37
|
+
get_next_frame,
|
|
38
|
+
scan_all_streams_to_update_metadata,
|
|
39
|
+
seek_to_pts,
|
|
40
|
+
)
|