forked from meta-pytorch/torchcodec
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcustom_ops.cpp
More file actions
962 lines (868 loc) · 37.1 KB
/
custom_ops.cpp
File metadata and controls
962 lines (868 loc) · 37.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
// Copyright (c) Meta Platforms, Inc. and affiliates.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
#include <pybind11/pybind11.h>
#include <cstdint>
#include <sstream>
#include <string>
#include "c10/core/SymIntArrayRef.h"
#include "c10/util/Exception.h"
#include "src/torchcodec/_core/AVIOFileLikeContext.h"
#include "src/torchcodec/_core/AVIOTensorContext.h"
#include "src/torchcodec/_core/Encoder.h"
#include "src/torchcodec/_core/SingleStreamDecoder.h"
#include "src/torchcodec/_core/ValidationUtils.h"
#include "torch/library.h"
namespace facebook::torchcodec {
// ==============================
// Define the operators
// ==============================
// All instances of accepting the decoder as a tensor must be annotated with
// `Tensor(a!)`. The `(a!)` part normally indicates that the tensor is being
// mutated in place. We need it to make sure that torch.compile does not reorder
// calls to these functions. For more detail, see:
// https://github.com/pytorch/pytorch/tree/main/aten/src/ATen/native#readme
TORCH_LIBRARY(torchcodec_ns, m) {
// m.impl_abstract_pystub(
// "torchcodec._core.ops", "//pytorch/torchcodec:torchcodec");
m.def("create_from_file(str filename, str? seek_mode=None) -> Tensor");
m.def(
"encode_audio_to_file(Tensor samples, int sample_rate, str filename, int? bit_rate=None, int? num_channels=None, int? desired_sample_rate=None) -> ()");
m.def(
"encode_audio_to_tensor(Tensor samples, int sample_rate, str format, int? bit_rate=None, int? num_channels=None, int? desired_sample_rate=None) -> Tensor");
m.def(
"_encode_audio_to_file_like(Tensor samples, int sample_rate, str format, int file_like_context, int? bit_rate=None, int? num_channels=None, int? desired_sample_rate=None) -> ()");
m.def(
"encode_video_to_file(Tensor frames, int frame_rate, str filename, int? crf=None) -> ()");
m.def(
"encode_video_to_tensor(Tensor frames, int frame_rate, str format, int? crf=None) -> Tensor");
m.def(
"_encode_video_to_file_like(Tensor frames, int frame_rate, str format, int file_like_context, int? crf=None) -> ()");
m.def(
"create_from_tensor(Tensor video_tensor, str? seek_mode=None) -> Tensor");
m.def(
"_create_from_file_like(int file_like_context, str? seek_mode=None) -> Tensor");
m.def(
"_add_video_stream(Tensor(a!) decoder, *, int? num_threads=None, str? dimension_order=None, int? stream_index=None, str device=\"cpu\", str device_variant=\"ffmpeg\", str transform_specs=\"\", (Tensor, Tensor, Tensor)? custom_frame_mappings=None, str? color_conversion_library=None) -> ()");
m.def(
"add_video_stream(Tensor(a!) decoder, *, int? num_threads=None, str? dimension_order=None, int? stream_index=None, str device=\"cpu\", str device_variant=\"ffmpeg\", str transform_specs=\"\", (Tensor, Tensor, Tensor)? custom_frame_mappings=None) -> ()");
m.def(
"add_audio_stream(Tensor(a!) decoder, *, int? stream_index=None, int? sample_rate=None, int? num_channels=None) -> ()");
m.def("seek_to_pts(Tensor(a!) decoder, float seconds) -> ()");
m.def("get_next_frame(Tensor(a!) decoder) -> (Tensor, Tensor, Tensor)");
m.def(
"get_frame_at_pts(Tensor(a!) decoder, float seconds) -> (Tensor, Tensor, Tensor)");
m.def(
"get_frame_at_index(Tensor(a!) decoder, *, int frame_index) -> (Tensor, Tensor, Tensor)");
m.def(
"get_frames_at_indices(Tensor(a!) decoder, *, Tensor frame_indices) -> (Tensor, Tensor, Tensor)");
m.def(
"get_frames_in_range(Tensor(a!) decoder, *, int start, int stop, int? step=None) -> (Tensor, Tensor, Tensor)");
m.def(
"get_frames_by_pts_in_range(Tensor(a!) decoder, *, float start_seconds, float stop_seconds) -> (Tensor, Tensor, Tensor)");
m.def(
"get_frames_by_pts_in_range_audio(Tensor(a!) decoder, *, float start_seconds, float? stop_seconds) -> (Tensor, Tensor)");
m.def(
"get_frames_by_pts(Tensor(a!) decoder, *, Tensor timestamps) -> (Tensor, Tensor, Tensor)");
m.def("_get_key_frame_indices(Tensor(a!) decoder) -> Tensor");
m.def("get_json_metadata(Tensor(a!) decoder) -> str");
m.def("get_container_json_metadata(Tensor(a!) decoder) -> str");
m.def(
"get_stream_json_metadata(Tensor(a!) decoder, int stream_index) -> str");
m.def("_get_json_ffmpeg_library_versions() -> str");
m.def("_get_backend_details(Tensor(a!) decoder) -> str");
m.def(
"_test_frame_pts_equality(Tensor(a!) decoder, *, int frame_index, float pts_seconds_to_test) -> bool");
m.def("scan_all_streams_to_update_metadata(Tensor(a!) decoder) -> ()");
}
namespace {
at::Tensor wrapDecoderPointerToTensor(
std::unique_ptr<SingleStreamDecoder> uniqueDecoder) {
SingleStreamDecoder* decoder = uniqueDecoder.release();
auto deleter = [decoder](void*) { delete decoder; };
at::Tensor tensor = at::from_blob(
decoder, {sizeof(SingleStreamDecoder*)}, deleter, {at::kLong});
auto videoDecoder =
static_cast<SingleStreamDecoder*>(tensor.mutable_data_ptr());
// TORCH_CHECK_EQ(videoDecoder, decoder) << "videoDecoder=" << videoDecoder;
return tensor;
}
SingleStreamDecoder* unwrapTensorToGetDecoder(at::Tensor& tensor) {
TORCH_CHECK(
tensor.is_contiguous(),
"fake decoder tensor must be contiguous! This is an internal error, please report on the torchcodec issue tracker.");
void* buffer = tensor.mutable_data_ptr();
SingleStreamDecoder* decoder = static_cast<SingleStreamDecoder*>(buffer);
return decoder;
}
// The elements of this tuple are all tensors that represent a single frame:
// 1. The frame data, which is a multidimensional tensor.
// 2. A single float value for the pts in seconds.
// 3. A single float value for the duration in seconds.
// The reason we use Tensors for the second and third values is so we can run
// under torch.compile().
using OpsFrameOutput = std::tuple<at::Tensor, at::Tensor, at::Tensor>;
OpsFrameOutput makeOpsFrameOutput(FrameOutput& frame) {
// return std::make_tuple(
// frame.data,
// torch::tensor(frame.ptsSeconds, torch::dtype(torch::kFloat64)),
// torch::tensor(frame.durationSeconds, torch::dtype(torch::kFloat64)));
return std::make_tuple(
frame.data,
torch::full({}, frame.ptsSeconds, torch::kFloat64),
torch::full({}, frame.durationSeconds, torch::kFloat64));
}
SingleStreamDecoder::FrameMappings makeFrameMappings(
std::tuple<at::Tensor, at::Tensor, at::Tensor> custom_frame_mappings) {
return SingleStreamDecoder::FrameMappings{
std::move(std::get<0>(custom_frame_mappings)),
std::move(std::get<1>(custom_frame_mappings)),
std::move(std::get<2>(custom_frame_mappings))};
}
// All elements of this tuple are tensors of the same leading dimension. The
// tuple represents the frames for N total frames, where N is the dimension of
// each stacked tensor. The elments are:
// 1. Stacked tensor of data for all N frames. Each frame is also a
// multidimensional tensor.
// 2. Tensor of N pts values in seconds, where each pts is a single
// float.
// 3. Tensor of N durationis in seconds, where each duration is a
// single float.
using OpsFrameBatchOutput = std::tuple<at::Tensor, at::Tensor, at::Tensor>;
OpsFrameBatchOutput makeOpsFrameBatchOutput(FrameBatchOutput& batch) {
return std::make_tuple(batch.data, batch.ptsSeconds, batch.durationSeconds);
}
// The elements of this tuple are all tensors that represent the concatenation
// of multiple audio frames:
// 1. The frames data (concatenated)
// 2. A single float value for the pts of the first frame, in seconds.
using OpsAudioFramesOutput = std::tuple<at::Tensor, at::Tensor>;
OpsAudioFramesOutput makeOpsAudioFramesOutput(AudioFramesOutput& audioFrames) {
// return std::make_tuple(
// audioFrames.data,
// torch::tensor(audioFrames.ptsSeconds, torch::dtype(torch::kFloat64)));
return std::make_tuple(
audioFrames.data,
torch::full({}, audioFrames.ptsSeconds, torch::kFloat64));
}
std::string quoteValue(const std::string& value) {
return "\"" + value + "\"";
}
std::string mapToJson(const std::map<std::string, std::string>& metadataMap) {
std::stringstream ss;
ss << "{\n";
auto it = metadataMap.begin();
while (it != metadataMap.end()) {
ss << "\"" << it->first << "\": " << it->second;
++it;
if (it != metadataMap.end()) {
ss << ",\n";
} else {
ss << "\n";
}
}
ss << "}";
return ss.str();
}
SingleStreamDecoder::SeekMode seekModeFromString(std::string_view seekMode) {
if (seekMode == "exact") {
return SingleStreamDecoder::SeekMode::exact;
} else if (seekMode == "approximate") {
return SingleStreamDecoder::SeekMode::approximate;
} else if (seekMode == "custom_frame_mappings") {
return SingleStreamDecoder::SeekMode::custom_frame_mappings;
} else {
TORCH_CHECK(false, "Invalid seek mode: " + std::string(seekMode));
}
}
int checkedToPositiveInt(const std::string& str) {
int ret = 0;
try {
ret = std::stoi(str);
} catch (const std::invalid_argument&) {
TORCH_CHECK(false, "String cannot be converted to an int:" + str);
} catch (const std::out_of_range&) {
TORCH_CHECK(false, "String would become integer out of range:" + str);
}
TORCH_CHECK(ret > 0, "String must be a positive integer:" + str);
return ret;
}
// Resize transform specs take the form:
//
// "resize, <height>, <width>"
//
// Where "resize" is the string literal and <height> and <width> are positive
// integers.
Transform* makeResizeTransform(
const std::vector<std::string>& resizeTransformSpec) {
TORCH_CHECK(
resizeTransformSpec.size() == 3,
"resizeTransformSpec must have 3 elements including its name");
int height = checkedToPositiveInt(resizeTransformSpec[1]);
int width = checkedToPositiveInt(resizeTransformSpec[2]);
return new ResizeTransform(FrameDims(height, width));
}
// Crop transform specs take the form:
//
// "crop, <height>, <width>, <x>, <y>"
//
// Where "crop" is the string literal and <height>, <width>, <x> and <y> are
// positive integers. <x> and <y> are the x and y coordinates of the top left
// corner of the crop. Note that we follow the PyTorch convention of (height,
// width) for specifying image dimensions; FFmpeg uses (width, height).
Transform* makeCropTransform(
const std::vector<std::string>& cropTransformSpec) {
TORCH_CHECK(
cropTransformSpec.size() == 5,
"cropTransformSpec must have 5 elements including its name");
int height = checkedToPositiveInt(cropTransformSpec[1]);
int width = checkedToPositiveInt(cropTransformSpec[2]);
int x = checkedToPositiveInt(cropTransformSpec[3]);
int y = checkedToPositiveInt(cropTransformSpec[4]);
return new CropTransform(FrameDims(height, width), x, y);
}
std::vector<std::string> split(const std::string& str, char delimiter) {
std::vector<std::string> tokens;
std::string token;
std::istringstream tokenStream(str);
while (std::getline(tokenStream, token, delimiter)) {
tokens.push_back(token);
}
return tokens;
}
// The transformSpecsRaw string is always in the format:
//
// "name1, param1, param2, ...; name2, param1, param2, ...; ..."
//
// Where "nameX" is the name of the transform, and "paramX" are the parameters.
std::vector<Transform*> makeTransforms(const std::string& transformSpecsRaw) {
std::vector<Transform*> transforms;
std::vector<std::string> transformSpecs = split(transformSpecsRaw, ';');
for (const std::string& transformSpecRaw : transformSpecs) {
std::vector<std::string> transformSpec = split(transformSpecRaw, ',');
TORCH_CHECK(
transformSpec.size() >= 1,
"Invalid transform spec: " + transformSpecRaw);
auto name = transformSpec[0];
if (name == "resize") {
transforms.push_back(makeResizeTransform(transformSpec));
} else if (name == "crop") {
transforms.push_back(makeCropTransform(transformSpec));
} else {
TORCH_CHECK(false, "Invalid transform name: " + name);
}
}
return transforms;
}
} // namespace
// ==============================
// Implementations for the operators
// ==============================
// Create a SingleStreamDecoder from file and wrap the pointer in a tensor.
at::Tensor create_from_file(
std::string_view filename,
std::optional<std::string_view> seek_mode = std::nullopt) {
std::string filenameStr(filename);
SingleStreamDecoder::SeekMode realSeek = SingleStreamDecoder::SeekMode::exact;
if (seek_mode.has_value()) {
realSeek = seekModeFromString(seek_mode.value());
}
std::unique_ptr<SingleStreamDecoder> uniqueDecoder =
std::make_unique<SingleStreamDecoder>(filenameStr, realSeek);
return wrapDecoderPointerToTensor(std::move(uniqueDecoder));
}
// Create a SingleStreamDecoder from the actual bytes of a video and wrap the
// pointer in a tensor. The SingleStreamDecoder will decode the provided bytes.
at::Tensor create_from_tensor(
at::Tensor video_tensor,
std::optional<std::string_view> seek_mode = std::nullopt) {
TORCH_CHECK(video_tensor.is_contiguous(), "video_tensor must be contiguous");
TORCH_CHECK(
video_tensor.scalar_type() == torch::kUInt8,
"video_tensor must be kUInt8");
SingleStreamDecoder::SeekMode realSeek = SingleStreamDecoder::SeekMode::exact;
if (seek_mode.has_value()) {
realSeek = seekModeFromString(seek_mode.value());
}
auto avioContextHolder =
std::make_unique<AVIOFromTensorContext>(video_tensor);
std::unique_ptr<SingleStreamDecoder> uniqueDecoder =
std::make_unique<SingleStreamDecoder>(
std::move(avioContextHolder), realSeek);
return wrapDecoderPointerToTensor(std::move(uniqueDecoder));
}
at::Tensor _create_from_file_like(
int64_t file_like_context,
std::optional<std::string_view> seek_mode) {
auto fileLikeContext =
reinterpret_cast<AVIOFileLikeContext*>(file_like_context);
TORCH_CHECK(
fileLikeContext != nullptr, "file_like_context must be a valid pointer");
std::unique_ptr<AVIOFileLikeContext> avioContextHolder(fileLikeContext);
SingleStreamDecoder::SeekMode realSeek = SingleStreamDecoder::SeekMode::exact;
if (seek_mode.has_value()) {
realSeek = seekModeFromString(seek_mode.value());
}
std::unique_ptr<SingleStreamDecoder> uniqueDecoder =
std::make_unique<SingleStreamDecoder>(
std::move(avioContextHolder), realSeek);
return wrapDecoderPointerToTensor(std::move(uniqueDecoder));
}
void _add_video_stream(
at::Tensor& decoder,
std::optional<int64_t> num_threads = std::nullopt,
std::optional<std::string_view> dimension_order = std::nullopt,
std::optional<int64_t> stream_index = std::nullopt,
std::string_view device = "cpu",
std::string_view device_variant = "ffmpeg",
std::string_view transform_specs = "",
std::optional<std::tuple<at::Tensor, at::Tensor, at::Tensor>>
custom_frame_mappings = std::nullopt,
std::optional<std::string_view> color_conversion_library = std::nullopt) {
VideoStreamOptions videoStreamOptions;
videoStreamOptions.ffmpegThreadCount = num_threads;
if (dimension_order.has_value()) {
std::string stdDimensionOrder{dimension_order.value()};
TORCH_CHECK(stdDimensionOrder == "NHWC" || stdDimensionOrder == "NCHW");
videoStreamOptions.dimensionOrder = stdDimensionOrder;
}
if (color_conversion_library.has_value()) {
std::string stdColorConversionLibrary{color_conversion_library.value()};
if (stdColorConversionLibrary == "filtergraph") {
videoStreamOptions.colorConversionLibrary =
ColorConversionLibrary::FILTERGRAPH;
} else if (stdColorConversionLibrary == "swscale") {
videoStreamOptions.colorConversionLibrary =
ColorConversionLibrary::SWSCALE;
} else {
TORCH_CHECK(
false,
"Invalid color_conversion_library=",
stdColorConversionLibrary,
". color_conversion_library must be either filtergraph or swscale.");
}
}
validateDeviceInterface(std::string(device), std::string(device_variant));
videoStreamOptions.device = torch::Device(std::string(device));
videoStreamOptions.deviceVariant = device_variant;
std::vector<Transform*> transforms =
makeTransforms(std::string(transform_specs));
std::optional<SingleStreamDecoder::FrameMappings> converted_mappings =
custom_frame_mappings.has_value()
? std::make_optional(makeFrameMappings(custom_frame_mappings.value()))
: std::nullopt;
auto videoDecoder = unwrapTensorToGetDecoder(decoder);
videoDecoder->addVideoStream(
stream_index.value_or(-1),
transforms,
videoStreamOptions,
converted_mappings);
}
// Add a new video stream at `stream_index` using the provided options.
void add_video_stream(
at::Tensor& decoder,
std::optional<int64_t> num_threads = std::nullopt,
std::optional<std::string_view> dimension_order = std::nullopt,
std::optional<int64_t> stream_index = std::nullopt,
std::string_view device = "cpu",
std::string_view device_variant = "ffmpeg",
std::string_view transform_specs = "",
const std::optional<std::tuple<at::Tensor, at::Tensor, at::Tensor>>&
custom_frame_mappings = std::nullopt) {
_add_video_stream(
decoder,
num_threads,
dimension_order,
stream_index,
device,
device_variant,
transform_specs,
custom_frame_mappings);
}
void add_audio_stream(
at::Tensor& decoder,
std::optional<int64_t> stream_index = std::nullopt,
std::optional<int64_t> sample_rate = std::nullopt,
std::optional<int64_t> num_channels = std::nullopt) {
AudioStreamOptions audioStreamOptions;
audioStreamOptions.sampleRate = sample_rate;
audioStreamOptions.numChannels = num_channels;
auto videoDecoder = unwrapTensorToGetDecoder(decoder);
videoDecoder->addAudioStream(stream_index.value_or(-1), audioStreamOptions);
}
// Seek to a particular presentation timestamp in the video in seconds.
void seek_to_pts(at::Tensor& decoder, double seconds) {
auto videoDecoder =
static_cast<SingleStreamDecoder*>(decoder.mutable_data_ptr());
videoDecoder->setCursorPtsInSeconds(seconds);
}
// Get the next frame from the video as a tuple that has the frame data, pts and
// duration as tensors.
OpsFrameOutput get_next_frame(at::Tensor& decoder) {
auto videoDecoder = unwrapTensorToGetDecoder(decoder);
FrameOutput result;
try {
result = videoDecoder->getNextFrame();
} catch (const SingleStreamDecoder::EndOfFileException& e) {
// C10_THROW_ERROR(IndexError, e.what());
throw pybind11::index_error(e.what());
}
return makeOpsFrameOutput(result);
}
// Return the frame that is visible at a given timestamp in seconds. Each frame
// in FFMPEG has a presentation timestamp and a duration. The frame visible at a
// given timestamp T has T >= PTS and T < PTS + Duration.
OpsFrameOutput get_frame_at_pts(at::Tensor& decoder, double seconds) {
auto videoDecoder = unwrapTensorToGetDecoder(decoder);
FrameOutput result;
try {
result = videoDecoder->getFramePlayedAt(seconds);
} catch (const SingleStreamDecoder::EndOfFileException& e) {
// C10_THROW_ERROR(IndexError, e.what());
throw pybind11::index_error(e.what());
}
return makeOpsFrameOutput(result);
}
// Return the frame that is visible at a given index in the video.
OpsFrameOutput get_frame_at_index(at::Tensor& decoder, int64_t frame_index) {
auto videoDecoder = unwrapTensorToGetDecoder(decoder);
auto result = videoDecoder->getFrameAtIndex(frame_index);
return makeOpsFrameOutput(result);
}
// Return the frames at given indices for a given stream
OpsFrameBatchOutput get_frames_at_indices(
at::Tensor& decoder,
const at::Tensor& frame_indices) {
auto videoDecoder = unwrapTensorToGetDecoder(decoder);
auto result = videoDecoder->getFramesAtIndices(frame_indices);
return makeOpsFrameBatchOutput(result);
}
// Return the frames inside a range as a single stacked Tensor. The range is
// defined as [start, stop).
OpsFrameBatchOutput get_frames_in_range(
at::Tensor& decoder,
int64_t start,
int64_t stop,
std::optional<int64_t> step = std::nullopt) {
auto videoDecoder = unwrapTensorToGetDecoder(decoder);
auto result = videoDecoder->getFramesInRange(start, stop, step.value_or(1));
return makeOpsFrameBatchOutput(result);
}
// Return the frames at given ptss for a given stream
OpsFrameBatchOutput get_frames_by_pts(
at::Tensor& decoder,
const at::Tensor& timestamps) {
auto videoDecoder = unwrapTensorToGetDecoder(decoder);
auto result = videoDecoder->getFramesPlayedAt(timestamps);
return makeOpsFrameBatchOutput(result);
}
// Return the frames inside the range as a single stacked Tensor. The range is
// defined as [start_seconds, stop_seconds). The frames are stacked in pts
// order.
OpsFrameBatchOutput get_frames_by_pts_in_range(
at::Tensor& decoder,
double start_seconds,
double stop_seconds) {
auto videoDecoder = unwrapTensorToGetDecoder(decoder);
auto result =
videoDecoder->getFramesPlayedInRange(start_seconds, stop_seconds);
return makeOpsFrameBatchOutput(result);
}
OpsAudioFramesOutput get_frames_by_pts_in_range_audio(
at::Tensor& decoder,
double start_seconds,
std::optional<double> stop_seconds = std::nullopt) {
auto videoDecoder = unwrapTensorToGetDecoder(decoder);
auto result =
videoDecoder->getFramesPlayedInRangeAudio(start_seconds, stop_seconds);
return makeOpsAudioFramesOutput(result);
}
void encode_audio_to_file(
const at::Tensor& samples,
int64_t sample_rate,
std::string_view file_name,
std::optional<int64_t> bit_rate = std::nullopt,
std::optional<int64_t> num_channels = std::nullopt,
std::optional<int64_t> desired_sample_rate = std::nullopt) {
AudioStreamOptions audioStreamOptions;
audioStreamOptions.bitRate = validateOptionalInt64ToInt(bit_rate, "bit_rate");
audioStreamOptions.numChannels =
validateOptionalInt64ToInt(num_channels, "num_channels");
audioStreamOptions.sampleRate =
validateOptionalInt64ToInt(desired_sample_rate, "desired_sample_rate");
AudioEncoder(
samples,
validateInt64ToInt(sample_rate, "sample_rate"),
file_name,
audioStreamOptions)
.encode();
}
at::Tensor encode_audio_to_tensor(
const at::Tensor& samples,
int64_t sample_rate,
std::string_view format,
std::optional<int64_t> bit_rate = std::nullopt,
std::optional<int64_t> num_channels = std::nullopt,
std::optional<int64_t> desired_sample_rate = std::nullopt) {
auto avioContextHolder = std::make_unique<AVIOToTensorContext>();
AudioStreamOptions audioStreamOptions;
audioStreamOptions.bitRate = validateOptionalInt64ToInt(bit_rate, "bit_rate");
audioStreamOptions.numChannels =
validateOptionalInt64ToInt(num_channels, "num_channels");
audioStreamOptions.sampleRate =
validateOptionalInt64ToInt(desired_sample_rate, "desired_sample_rate");
return AudioEncoder(
samples,
validateInt64ToInt(sample_rate, "sample_rate"),
format,
std::move(avioContextHolder),
audioStreamOptions)
.encodeToTensor();
}
void _encode_audio_to_file_like(
const at::Tensor& samples,
int64_t sample_rate,
std::string_view format,
int64_t file_like_context,
std::optional<int64_t> bit_rate = std::nullopt,
std::optional<int64_t> num_channels = std::nullopt,
std::optional<int64_t> desired_sample_rate = std::nullopt) {
auto fileLikeContext =
reinterpret_cast<AVIOFileLikeContext*>(file_like_context);
TORCH_CHECK(
fileLikeContext != nullptr, "file_like_context must be a valid pointer");
std::unique_ptr<AVIOFileLikeContext> avioContextHolder(fileLikeContext);
AudioStreamOptions audioStreamOptions;
audioStreamOptions.bitRate = validateOptionalInt64ToInt(bit_rate, "bit_rate");
audioStreamOptions.numChannels =
validateOptionalInt64ToInt(num_channels, "num_channels");
audioStreamOptions.sampleRate =
validateOptionalInt64ToInt(desired_sample_rate, "desired_sample_rate");
AudioEncoder encoder(
samples,
validateInt64ToInt(sample_rate, "sample_rate"),
format,
std::move(avioContextHolder),
audioStreamOptions);
encoder.encode();
}
void encode_video_to_file(
const at::Tensor& frames,
int64_t frame_rate,
std::string_view file_name,
std::optional<int64_t> crf = std::nullopt) {
VideoStreamOptions videoStreamOptions;
videoStreamOptions.crf = crf;
VideoEncoder(
frames,
validateInt64ToInt(frame_rate, "frame_rate"),
file_name,
videoStreamOptions)
.encode();
}
at::Tensor encode_video_to_tensor(
const at::Tensor& frames,
int64_t frame_rate,
std::string_view format,
std::optional<int64_t> crf = std::nullopt) {
auto avioContextHolder = std::make_unique<AVIOToTensorContext>();
VideoStreamOptions videoStreamOptions;
videoStreamOptions.crf = crf;
return VideoEncoder(
frames,
validateInt64ToInt(frame_rate, "frame_rate"),
format,
std::move(avioContextHolder),
videoStreamOptions)
.encodeToTensor();
}
void _encode_video_to_file_like(
const at::Tensor& frames,
int64_t frame_rate,
std::string_view format,
int64_t file_like_context,
std::optional<int64_t> crf = std::nullopt) {
auto fileLikeContext =
reinterpret_cast<AVIOFileLikeContext*>(file_like_context);
TORCH_CHECK(
fileLikeContext != nullptr, "file_like_context must be a valid pointer");
std::unique_ptr<AVIOFileLikeContext> avioContextHolder(fileLikeContext);
VideoStreamOptions videoStreamOptions;
videoStreamOptions.crf = crf;
VideoEncoder encoder(
frames,
validateInt64ToInt(frame_rate, "frame_rate"),
format,
std::move(avioContextHolder),
videoStreamOptions);
encoder.encode();
}
// For testing only. We need to implement this operation as a core library
// function because what we're testing is round-tripping pts values as
// double-precision floating point numbers from C++ to Python and back to C++.
// We want to make sure that the value is preserved exactly, bit-for-bit, during
// this process.
//
// Returns true if for the given decoder, the pts
// value when converted to seconds as a double is exactly pts_seconds_to_test.
// Returns false otherwise.
bool _test_frame_pts_equality(
at::Tensor& decoder,
int64_t frame_index,
double pts_seconds_to_test) {
auto videoDecoder = unwrapTensorToGetDecoder(decoder);
return pts_seconds_to_test ==
videoDecoder->getPtsSecondsForFrame(frame_index);
}
torch::Tensor _get_key_frame_indices(at::Tensor& decoder) {
auto videoDecoder = unwrapTensorToGetDecoder(decoder);
return videoDecoder->getKeyFrameIndices();
}
// Get the metadata from the video as a string.
std::string get_json_metadata(at::Tensor& decoder) {
auto videoDecoder = unwrapTensorToGetDecoder(decoder);
ContainerMetadata videoMetadata = videoDecoder->getContainerMetadata();
auto maybeBestVideoStreamIndex = videoMetadata.bestVideoStreamIndex;
std::map<std::string, std::string> metadataMap;
// serialize the metadata into a string std::stringstream ss;
double durationSecondsFromHeader = 0;
if (maybeBestVideoStreamIndex.has_value() &&
videoMetadata.allStreamMetadata[*maybeBestVideoStreamIndex]
.durationSecondsFromHeader.has_value()) {
durationSecondsFromHeader =
videoMetadata.allStreamMetadata[*maybeBestVideoStreamIndex]
.durationSecondsFromHeader.value_or(0);
} else {
// Fallback to container-level duration if stream duration is not found.
durationSecondsFromHeader =
videoMetadata.durationSecondsFromHeader.value_or(0);
}
metadataMap["durationSecondsFromHeader"] =
std::to_string(durationSecondsFromHeader);
if (videoMetadata.bitRate.has_value()) {
metadataMap["bitRate"] = std::to_string(videoMetadata.bitRate.value());
}
if (maybeBestVideoStreamIndex.has_value()) {
auto streamMetadata =
videoMetadata.allStreamMetadata[*maybeBestVideoStreamIndex];
if (streamMetadata.numFramesFromContent.has_value()) {
metadataMap["numFramesFromHeader"] =
std::to_string(*streamMetadata.numFramesFromContent);
} else if (streamMetadata.numFramesFromHeader.has_value()) {
metadataMap["numFramesFromHeader"] =
std::to_string(*streamMetadata.numFramesFromHeader);
}
if (streamMetadata.beginStreamPtsSecondsFromContent.has_value()) {
metadataMap["beginStreamSecondsFromContent"] =
std::to_string(*streamMetadata.beginStreamPtsSecondsFromContent);
}
if (streamMetadata.endStreamPtsSecondsFromContent.has_value()) {
metadataMap["endStreamSecondsFromContent"] =
std::to_string(*streamMetadata.endStreamPtsSecondsFromContent);
}
if (streamMetadata.codecName.has_value()) {
metadataMap["codec"] = quoteValue(streamMetadata.codecName.value());
}
if (streamMetadata.width.has_value()) {
metadataMap["width"] = std::to_string(*streamMetadata.width);
}
if (streamMetadata.height.has_value()) {
metadataMap["height"] = std::to_string(*streamMetadata.height);
}
if (streamMetadata.averageFpsFromHeader.has_value()) {
metadataMap["averageFpsFromHeader"] =
std::to_string(*streamMetadata.averageFpsFromHeader);
}
}
if (videoMetadata.bestVideoStreamIndex.has_value()) {
metadataMap["bestVideoStreamIndex"] =
std::to_string(*videoMetadata.bestVideoStreamIndex);
}
if (videoMetadata.bestAudioStreamIndex.has_value()) {
metadataMap["bestAudioStreamIndex"] =
std::to_string(*videoMetadata.bestAudioStreamIndex);
}
return mapToJson(metadataMap);
}
// Get the container metadata as a string.
std::string get_container_json_metadata(at::Tensor& decoder) {
auto videoDecoder = unwrapTensorToGetDecoder(decoder);
auto containerMetadata = videoDecoder->getContainerMetadata();
std::map<std::string, std::string> map;
if (containerMetadata.durationSecondsFromHeader.has_value()) {
map["durationSecondsFromHeader"] =
std::to_string(*containerMetadata.durationSecondsFromHeader);
}
if (containerMetadata.bitRate.has_value()) {
map["bitRate"] = std::to_string(*containerMetadata.bitRate);
}
if (containerMetadata.bestVideoStreamIndex.has_value()) {
map["bestVideoStreamIndex"] =
std::to_string(*containerMetadata.bestVideoStreamIndex);
}
if (containerMetadata.bestAudioStreamIndex.has_value()) {
map["bestAudioStreamIndex"] =
std::to_string(*containerMetadata.bestAudioStreamIndex);
}
map["numStreams"] =
std::to_string(containerMetadata.allStreamMetadata.size());
return mapToJson(map);
}
// Get the stream metadata as a string.
std::string get_stream_json_metadata(
at::Tensor& decoder,
int64_t stream_index) {
auto videoDecoder = unwrapTensorToGetDecoder(decoder);
auto allStreamMetadata =
videoDecoder->getContainerMetadata().allStreamMetadata;
if (stream_index < 0 ||
stream_index >= static_cast<int64_t>(allStreamMetadata.size())) {
throw std::out_of_range(
"stream_index out of bounds: " + std::to_string(stream_index));
}
auto streamMetadata = allStreamMetadata[stream_index];
std::map<std::string, std::string> map;
if (streamMetadata.durationSecondsFromHeader.has_value()) {
map["durationSecondsFromHeader"] =
std::to_string(*streamMetadata.durationSecondsFromHeader);
}
if (streamMetadata.bitRate.has_value()) {
map["bitRate"] = std::to_string(*streamMetadata.bitRate);
}
if (streamMetadata.numFramesFromContent.has_value()) {
map["numFramesFromContent"] =
std::to_string(*streamMetadata.numFramesFromContent);
}
if (streamMetadata.numFramesFromHeader.has_value()) {
map["numFramesFromHeader"] =
std::to_string(*streamMetadata.numFramesFromHeader);
}
if (streamMetadata.beginStreamSecondsFromHeader.has_value()) {
map["beginStreamSecondsFromHeader"] =
std::to_string(*streamMetadata.beginStreamSecondsFromHeader);
}
if (streamMetadata.beginStreamPtsSecondsFromContent.has_value()) {
map["beginStreamSecondsFromContent"] =
std::to_string(*streamMetadata.beginStreamPtsSecondsFromContent);
}
if (streamMetadata.endStreamPtsSecondsFromContent.has_value()) {
map["endStreamSecondsFromContent"] =
std::to_string(*streamMetadata.endStreamPtsSecondsFromContent);
}
if (streamMetadata.codecName.has_value()) {
map["codec"] = quoteValue(streamMetadata.codecName.value());
}
if (streamMetadata.width.has_value()) {
map["width"] = std::to_string(*streamMetadata.width);
}
if (streamMetadata.height.has_value()) {
map["height"] = std::to_string(*streamMetadata.height);
}
if (streamMetadata.sampleAspectRatio.has_value()) {
map["sampleAspectRatioNum"] =
std::to_string((*streamMetadata.sampleAspectRatio).num);
map["sampleAspectRatioDen"] =
std::to_string((*streamMetadata.sampleAspectRatio).den);
}
if (streamMetadata.averageFpsFromHeader.has_value()) {
map["averageFpsFromHeader"] =
std::to_string(*streamMetadata.averageFpsFromHeader);
}
if (streamMetadata.sampleRate.has_value()) {
map["sampleRate"] = std::to_string(*streamMetadata.sampleRate);
}
if (streamMetadata.numChannels.has_value()) {
map["numChannels"] = std::to_string(*streamMetadata.numChannels);
}
if (streamMetadata.sampleFormat.has_value()) {
map["sampleFormat"] = quoteValue(streamMetadata.sampleFormat.value());
}
if (streamMetadata.mediaType == AVMEDIA_TYPE_VIDEO) {
map["mediaType"] = quoteValue("video");
} else if (streamMetadata.mediaType == AVMEDIA_TYPE_AUDIO) {
map["mediaType"] = quoteValue("audio");
} else {
map["mediaType"] = quoteValue("other");
}
return mapToJson(map);
}
// Returns version information about the various FFMPEG libraries that are
// loaded in the program's address space.
// TODO: ideally we'd have a more robust way of getting the ffmpeg version,
// we're using av_version_info() which is not standardized and shouldn't be
// parsed by code (which we do!). See
// https://github.com/pytorch/torchcodec/issues/100
std::string _get_json_ffmpeg_library_versions() {
std::stringstream ss;
ss << "{\n";
unsigned int version = avfilter_version();
ss << "\"libavfilter\": [" << AV_VERSION_MAJOR(version) << ", "
<< AV_VERSION_MINOR(version) << ", " << AV_VERSION_MICRO(version)
<< "],\n";
version = avutil_version();
ss << "\"libavutil\": [" << AV_VERSION_MAJOR(version) << ", "
<< AV_VERSION_MINOR(version) << ", " << AV_VERSION_MICRO(version)
<< "],\n";
version = avcodec_version();
ss << "\"libavcodec\": [" << AV_VERSION_MAJOR(version) << ", "
<< AV_VERSION_MINOR(version) << ", " << AV_VERSION_MICRO(version)
<< "],\n";
version = avformat_version();
ss << "\"libavformat\": [" << AV_VERSION_MAJOR(version) << ", "
<< AV_VERSION_MINOR(version) << ", " << AV_VERSION_MICRO(version)
<< "],\n";
ss << "\"ffmpeg_version\": \"" << av_version_info() << "\"\n";
ss << "}\n";
return ss.str();
}
std::string get_backend_details(at::Tensor& decoder) {
auto videoDecoder = unwrapTensorToGetDecoder(decoder);
return videoDecoder->getDeviceInterfaceDetails();
}
// Scans video packets to get more accurate metadata like frame count, exact
// keyframe positions, etc. Exact keyframe positions are useful for efficient
// accurate seeking. Note that this function reads the entire video but it does
// not decode frames. Reading a video file is much cheaper than decoding it.
void scan_all_streams_to_update_metadata(at::Tensor& decoder) {
auto videoDecoder = unwrapTensorToGetDecoder(decoder);
videoDecoder->scanFileAndUpdateMetadataAndIndex();
}
TORCH_LIBRARY_IMPL(torchcodec_ns, BackendSelect, m) {
m.impl("create_from_file", &create_from_file);
m.impl("create_from_tensor", &create_from_tensor);
m.impl("_create_from_file_like", &_create_from_file_like);
m.impl(
"_get_json_ffmpeg_library_versions", &_get_json_ffmpeg_library_versions);
}
TORCH_LIBRARY_IMPL(torchcodec_ns, CPU, m) {
m.impl("encode_audio_to_file", &encode_audio_to_file);
m.impl("encode_audio_to_tensor", &encode_audio_to_tensor);
m.impl("_encode_audio_to_file_like", &_encode_audio_to_file_like);
m.impl("encode_video_to_file", &encode_video_to_file);
m.impl("encode_video_to_tensor", &encode_video_to_tensor);
m.impl("_encode_video_to_file_like", &_encode_video_to_file_like);
m.impl("seek_to_pts", &seek_to_pts);
m.impl("add_video_stream", &add_video_stream);
m.impl("_add_video_stream", &_add_video_stream);
m.impl("add_audio_stream", &add_audio_stream);
m.impl("get_next_frame", &get_next_frame);
m.impl("_get_key_frame_indices", &_get_key_frame_indices);
m.impl("get_json_metadata", &get_json_metadata);
m.impl("get_container_json_metadata", &get_container_json_metadata);
m.impl("get_stream_json_metadata", &get_stream_json_metadata);
m.impl("get_frame_at_pts", &get_frame_at_pts);
m.impl("get_frame_at_index", &get_frame_at_index);
m.impl("get_frames_at_indices", &get_frames_at_indices);
m.impl("get_frames_in_range", &get_frames_in_range);
m.impl("get_frames_by_pts_in_range", &get_frames_by_pts_in_range);
m.impl("get_frames_by_pts_in_range_audio", &get_frames_by_pts_in_range_audio);
m.impl("get_frames_by_pts", &get_frames_by_pts);
m.impl("_test_frame_pts_equality", &_test_frame_pts_equality);
m.impl(
"scan_all_streams_to_update_metadata",
&scan_all_streams_to_update_metadata);
m.impl("_get_backend_details", &get_backend_details);
}
} // namespace facebook::torchcodec