Skip to content

Commit 24d0035

Browse files
committed
use avFrameWithStreamIndex
1 parent 62818a7 commit 24d0035

File tree

5 files changed

+27
-26
lines changed

5 files changed

+27
-26
lines changed

src/torchcodec/decoders/_core/CPUOnlyDevice.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ namespace facebook::torchcodec {
1717
void convertAVFrameToFrameOutputOnCuda(
1818
const torch::Device& device,
1919
[[maybe_unused]] const VideoDecoder::VideoStreamOptions& videoStreamOptions,
20-
[[maybe_unused]] VideoDecoder::AVFrameWithStreamIndex& rawOutput,
20+
[[maybe_unused]] VideoDecoder::AVFrameWithStreamIndex&
21+
avFrameWithStreamIndex,
2122
[[maybe_unused]] VideoDecoder::FrameOutput& frameOutput,
2223
[[maybe_unused]] std::optional<torch::Tensor> preAllocatedOutputTensor) {
2324
throwUnsupportedDeviceError(device);

src/torchcodec/decoders/_core/CudaDevice.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,10 @@ void initializeContextOnCuda(
186186
void convertAVFrameToFrameOutputOnCuda(
187187
const torch::Device& device,
188188
const VideoDecoder::VideoStreamOptions& videoStreamOptions,
189-
VideoDecoder::AVFrameWithStreamIndex& rawOutput,
189+
VideoDecoder::AVFrameWithStreamIndex& avFrameWithStreamIndex,
190190
VideoDecoder::FrameOutput& frameOutput,
191191
std::optional<torch::Tensor> preAllocatedOutputTensor) {
192-
AVFrame* avFrame = rawOutput.avFrame.get();
192+
AVFrame* avFrame = avFrameWithStreamIndex.avFrame.get();
193193

194194
TORCH_CHECK(
195195
avFrame->format == AV_PIX_FMT_CUDA,

src/torchcodec/decoders/_core/DeviceInterface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void initializeContextOnCuda(
3232
void convertAVFrameToFrameOutputOnCuda(
3333
const torch::Device& device,
3434
const VideoDecoder::VideoStreamOptions& videoStreamOptions,
35-
VideoDecoder::AVFrameWithStreamIndex& rawOutput,
35+
VideoDecoder::AVFrameWithStreamIndex& avFrameWithStreamIndex,
3636
VideoDecoder::FrameOutput& frameOutput,
3737
std::optional<torch::Tensor> preAllocatedOutputTensor = std::nullopt);
3838

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -913,19 +913,19 @@ VideoDecoder::getAVFrameUsingFilterFunction(
913913
StreamInfo& activeStreamInfo = streamInfos_[frameStreamIndex];
914914
activeStreamInfo.currentPts = avFrame->pts;
915915
activeStreamInfo.currentDuration = getDuration(avFrame);
916-
AVFrameWithStreamIndex rawOutput;
917-
rawOutput.streamIndex = frameStreamIndex;
918-
rawOutput.avFrame = std::move(avFrame);
919-
return rawOutput;
916+
AVFrameWithStreamIndex avFrameWithStreamIndex;
917+
avFrameWithStreamIndex.streamIndex = frameStreamIndex;
918+
avFrameWithStreamIndex.avFrame = std::move(avFrame);
919+
return avFrameWithStreamIndex;
920920
}
921921

922922
VideoDecoder::FrameOutput VideoDecoder::convertAVFrameToFrameOutput(
923-
VideoDecoder::AVFrameWithStreamIndex& rawOutput,
923+
VideoDecoder::AVFrameWithStreamIndex& avFrameWithStreamIndex,
924924
std::optional<torch::Tensor> preAllocatedOutputTensor) {
925925
// Convert the frame to tensor.
926926
FrameOutput frameOutput;
927-
int streamIndex = rawOutput.streamIndex;
928-
AVFrame* avFrame = rawOutput.avFrame.get();
927+
int streamIndex = avFrameWithStreamIndex.streamIndex;
928+
AVFrame* avFrame = avFrameWithStreamIndex.avFrame.get();
929929
frameOutput.streamIndex = streamIndex;
930930
auto& streamInfo = streamInfos_[streamIndex];
931931
TORCH_CHECK(streamInfo.stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO);
@@ -936,12 +936,12 @@ VideoDecoder::FrameOutput VideoDecoder::convertAVFrameToFrameOutput(
936936
// TODO: we should fold preAllocatedOutputTensor into AVFrameWithStreamIndex.
937937
if (streamInfo.videoStreamOptions.device.type() == torch::kCPU) {
938938
convertAVFrameToFrameOutputOnCPU(
939-
rawOutput, frameOutput, preAllocatedOutputTensor);
939+
avFrameWithStreamIndex, frameOutput, preAllocatedOutputTensor);
940940
} else if (streamInfo.videoStreamOptions.device.type() == torch::kCUDA) {
941941
convertAVFrameToFrameOutputOnCuda(
942942
streamInfo.videoStreamOptions.device,
943943
streamInfo.videoStreamOptions,
944-
rawOutput,
944+
avFrameWithStreamIndex,
945945
frameOutput,
946946
preAllocatedOutputTensor);
947947
} else {
@@ -962,11 +962,11 @@ VideoDecoder::FrameOutput VideoDecoder::convertAVFrameToFrameOutput(
962962
// Dimension order of the preAllocatedOutputTensor must be HWC, regardless of
963963
// `dimension_order` parameter. It's up to callers to re-shape it if needed.
964964
void VideoDecoder::convertAVFrameToFrameOutputOnCPU(
965-
VideoDecoder::AVFrameWithStreamIndex& rawOutput,
965+
VideoDecoder::AVFrameWithStreamIndex& avFrameWithStreamIndex,
966966
FrameOutput& output,
967967
std::optional<torch::Tensor> preAllocatedOutputTensor) {
968-
int streamIndex = rawOutput.streamIndex;
969-
AVFrame* avFrame = rawOutput.avFrame.get();
968+
int streamIndex = avFrameWithStreamIndex.streamIndex;
969+
AVFrame* avFrame = avFrameWithStreamIndex.avFrame.get();
970970
auto& streamInfo = streamInfos_[streamIndex];
971971

972972
auto frameDims = getHeightAndWidthFromOptionsOrAVFrame(
@@ -1080,7 +1080,7 @@ VideoDecoder::FrameOutput VideoDecoder::getFramePlayedAtTimestampNoDemux(
10801080
}
10811081

10821082
setCursorPtsInSeconds(seconds);
1083-
AVFrameWithStreamIndex rawOutput = getAVFrameUsingFilterFunction(
1083+
AVFrameWithStreamIndex avFrameWithStreamIndex = getAVFrameUsingFilterFunction(
10841084
[seconds, this](int frameStreamIndex, AVFrame* avFrame) {
10851085
StreamInfo& streamInfo = streamInfos_[frameStreamIndex];
10861086
double frameStartTime = ptsToSeconds(avFrame->pts, streamInfo.timeBase);
@@ -1100,7 +1100,7 @@ VideoDecoder::FrameOutput VideoDecoder::getFramePlayedAtTimestampNoDemux(
11001100
});
11011101

11021102
// Convert the frame to tensor.
1103-
FrameOutput frameOutput = convertAVFrameToFrameOutput(rawOutput);
1103+
FrameOutput frameOutput = convertAVFrameToFrameOutput(avFrameWithStreamIndex);
11041104
frameOutput.data =
11051105
maybePermuteHWC2CHW(frameOutput.streamIndex, frameOutput.data);
11061106
return frameOutput;
@@ -1473,14 +1473,13 @@ VideoDecoder::FrameBatchOutput VideoDecoder::getFramesPlayedByTimestampInRange(
14731473
return frameBatchOutput;
14741474
}
14751475

1476-
VideoDecoder::AVFrameWithStreamIndex
1477-
VideoDecoder::getNextAVFrameNoDemux() {
1478-
auto rawOutput = getAVFrameUsingFilterFunction(
1476+
VideoDecoder::AVFrameWithStreamIndex VideoDecoder::getNextAVFrameNoDemux() {
1477+
auto avFrameWithStreamIndex = getAVFrameUsingFilterFunction(
14791478
[this](int frameStreamIndex, AVFrame* avFrame) {
14801479
StreamInfo& activeStreamInfo = streamInfos_[frameStreamIndex];
14811480
return avFrame->pts >= activeStreamInfo.discardFramesBeforePts;
14821481
});
1483-
return rawOutput;
1482+
return avFrameWithStreamIndex;
14841483
}
14851484

14861485
VideoDecoder::FrameOutput VideoDecoder::getNextFrameNoDemux() {
@@ -1491,8 +1490,9 @@ VideoDecoder::FrameOutput VideoDecoder::getNextFrameNoDemux() {
14911490

14921491
VideoDecoder::FrameOutput VideoDecoder::getNextFrameNoDemuxInternal(
14931492
std::optional<torch::Tensor> preAllocatedOutputTensor) {
1494-
auto rawOutput = getNextAVFrameNoDemux();
1495-
return convertAVFrameToFrameOutput(rawOutput, preAllocatedOutputTensor);
1493+
auto avFrameWithStreamIndex = getNextAVFrameNoDemux();
1494+
return convertAVFrameToFrameOutput(
1495+
avFrameWithStreamIndex, preAllocatedOutputTensor);
14961496
}
14971497

14981498
void VideoDecoder::setCursorPtsInSeconds(double seconds) {

src/torchcodec/decoders/_core/VideoDecoder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,10 +405,10 @@ class VideoDecoder {
405405
const AVFrame* avFrame,
406406
torch::Tensor& outputTensor);
407407
FrameOutput convertAVFrameToFrameOutput(
408-
AVFrameWithStreamIndex& rawOutput,
408+
AVFrameWithStreamIndex& avFrameWithStreamIndex,
409409
std::optional<torch::Tensor> preAllocatedOutputTensor = std::nullopt);
410410
void convertAVFrameToFrameOutputOnCPU(
411-
AVFrameWithStreamIndex& rawOutput,
411+
AVFrameWithStreamIndex& avFrameWithStreamIndex,
412412
FrameOutput& frameOutput,
413413
std::optional<torch::Tensor> preAllocatedOutputTensor = std::nullopt);
414414

0 commit comments

Comments
 (0)