Skip to content

Commit f0e2cdd

Browse files
committed
Use optional
1 parent bf9aed2 commit f0e2cdd

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -951,9 +951,9 @@ VideoDecoder::AudioFramesOutput VideoDecoder::getFramesPlayedInRangeAudio(
951951
(stopPts <= lastDecodedAvFrameEnd);
952952
}
953953

954-
torch::Tensor lastSamples = maybeFlushSwrBuffers();
955-
if (lastSamples.numel() > 0) {
956-
frames.push_back(lastSamples);
954+
auto lastSamples = maybeFlushSwrBuffers();
955+
if (lastSamples.has_value()) {
956+
frames.push_back(*lastSamples);
957957
}
958958

959959
return AudioFramesOutput{torch::cat(frames, 1), firstFramePtsSeconds};
@@ -1505,21 +1505,21 @@ UniqueAVFrame VideoDecoder::convertAudioAVFrameSampleFormatAndSampleRate(
15051505
return convertedAVFrame;
15061506
}
15071507

1508-
torch::Tensor VideoDecoder::maybeFlushSwrBuffers() {
1508+
std::optional<torch::Tensor> VideoDecoder::maybeFlushSwrBuffers() {
15091509
// When sample rate conversion is involved, swresample buffers some of the
15101510
// samples in-between calls to swr_convert (see the libswresample docs).
15111511
// That's because the last few samples in a given frame require future samples
15121512
// from the next frame to be properly converted. This function flushes out the
15131513
// samples that are stored in swresample's buffers.
15141514
auto& streamInfo = streamInfos_[activeStreamIndex_];
15151515
if (!streamInfo.swrContext) {
1516-
return torch::empty({0, 0});
1516+
return std::nullopt;
15171517
}
15181518
auto numRemainingSamples = // this is an upper bound
15191519
swr_get_out_samples(streamInfo.swrContext.get(), 0);
15201520

15211521
if (numRemainingSamples == 0) {
1522-
return torch::empty({0, 0});
1522+
return std::nullopt;
15231523
}
15241524

15251525
torch::Tensor lastSamples = torch::empty(

src/torchcodec/decoders/_core/VideoDecoder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ class VideoDecoder {
408408
int sourceSampleRate,
409409
int desiredSampleRate);
410410

411-
torch::Tensor maybeFlushSwrBuffers();
411+
std::optional<torch::Tensor> maybeFlushSwrBuffers();
412412

413413
// --------------------------------------------------------------------------
414414
// COLOR CONVERSION LIBRARIES HANDLERS CREATION

0 commit comments

Comments
 (0)