Skip to content

Commit 0fc81d6

Browse files
committed
don't use pointers, only refs to unique_ptr
1 parent 22f6c2f commit 0fc81d6

File tree

5 files changed

+32
-34
lines changed

5 files changed

+32
-34
lines changed

src/torchcodec/decoders/_core/CudaDevice.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ void convertAVFrameToFrameOutputOnCuda(
197197
avFrame->format == AV_PIX_FMT_CUDA,
198198
"Expected format to be AV_PIX_FMT_CUDA, got " +
199199
std::string(av_get_pix_fmt_name((AVPixelFormat)avFrame->format)));
200-
auto frameDims = getHeightAndWidthFromOptionsOrAVFrame(
201-
videoStreamOptions, avFrame);
200+
auto frameDims =
201+
getHeightAndWidthFromOptionsOrAVFrame(videoStreamOptions, avFrame);
202202
int height = frameDims.height;
203203
int width = frameDims.width;
204204
torch::Tensor& dst = frameOutput.data;

src/torchcodec/decoders/_core/FFMPEGCommon.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,11 @@ std::string getFFMPEGErrorStringFromErrorCode(int errorCode) {
4848
return std::string(errorBuffer);
4949
}
5050

51-
int64_t getDuration(const UniqueAVFrame& frame) {
52-
return getDuration(frame.get());
53-
}
54-
55-
int64_t getDuration(const AVFrame* frame) {
51+
int64_t getDuration(const UniqueAVFrame& avFrame) {
5652
#if LIBAVUTIL_VERSION_MAJOR < 58
57-
return frame->pkt_duration;
53+
return avFrame->pkt_duration;
5854
#else
59-
return frame->duration;
55+
return avFrame->duration;
6056
#endif
6157
}
6258

src/torchcodec/decoders/_core/FFMPEGCommon.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ std::string getFFMPEGErrorStringFromErrorCode(int errorCode);
140140
// struct member representing duration has changed across the versions we
141141
// support.
142142
int64_t getDuration(const UniqueAVFrame& frame);
143-
int64_t getDuration(const AVFrame* frame);
144143

145144
int getNumChannels(const UniqueAVFrame& avFrame);
146145
int getNumChannels(const UniqueAVCodecContext& avCodecContext);

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ VideoDecoder::FrameOutput VideoDecoder::getNextFrameInternal(
584584
std::optional<torch::Tensor> preAllocatedOutputTensor) {
585585
validateActiveStream();
586586
UniqueAVFrame avFrame = decodeAVFrame(
587-
[this](AVFrame* avFrame) { return avFrame->pts >= cursor_; });
587+
[this](const UniqueAVFrame& avFrame) { return avFrame->pts >= cursor_; });
588588
return convertAVFrameToFrameOutput(avFrame, preAllocatedOutputTensor);
589589
}
590590

@@ -715,23 +715,24 @@ VideoDecoder::FrameOutput VideoDecoder::getFramePlayedAt(double seconds) {
715715
}
716716

717717
setCursorPtsInSeconds(seconds);
718-
UniqueAVFrame avFrame = decodeAVFrame([seconds, this](AVFrame* avFrame) {
719-
StreamInfo& streamInfo = streamInfos_[activeStreamIndex_];
720-
double frameStartTime = ptsToSeconds(avFrame->pts, streamInfo.timeBase);
721-
double frameEndTime =
722-
ptsToSeconds(avFrame->pts + getDuration(avFrame), streamInfo.timeBase);
723-
if (frameStartTime > seconds) {
724-
// FFMPEG seeked past the frame we are looking for even though we
725-
// set max_ts to be our needed timestamp in avformat_seek_file()
726-
// in maybeSeekToBeforeDesiredPts().
727-
// This could be a bug in FFMPEG: https://trac.ffmpeg.org/ticket/11137
728-
// In this case we return the very next frame instead of throwing an
729-
// exception.
730-
// TODO: Maybe log to stderr for Debug builds?
731-
return true;
732-
}
733-
return seconds >= frameStartTime && seconds < frameEndTime;
734-
});
718+
UniqueAVFrame avFrame =
719+
decodeAVFrame([seconds, this](const UniqueAVFrame& avFrame) {
720+
StreamInfo& streamInfo = streamInfos_[activeStreamIndex_];
721+
double frameStartTime = ptsToSeconds(avFrame->pts, streamInfo.timeBase);
722+
double frameEndTime = ptsToSeconds(
723+
avFrame->pts + getDuration(avFrame), streamInfo.timeBase);
724+
if (frameStartTime > seconds) {
725+
// FFMPEG seeked past the frame we are looking for even though we
726+
// set max_ts to be our needed timestamp in avformat_seek_file()
727+
// in maybeSeekToBeforeDesiredPts().
728+
// This could be a bug in FFMPEG: https://trac.ffmpeg.org/ticket/11137
729+
// In this case we return the very next frame instead of throwing an
730+
// exception.
731+
// TODO: Maybe log to stderr for Debug builds?
732+
return true;
733+
}
734+
return seconds >= frameStartTime && seconds < frameEndTime;
735+
});
735736

736737
// Convert the frame to tensor.
737738
FrameOutput frameOutput = convertAVFrameToFrameOutput(avFrame);
@@ -890,9 +891,10 @@ VideoDecoder::AudioFramesOutput VideoDecoder::getFramesPlayedInRangeAudio(
890891
auto finished = false;
891892
while (!finished) {
892893
try {
893-
UniqueAVFrame avFrame = decodeAVFrame([startPts](AVFrame* avFrame) {
894-
return startPts < avFrame->pts + getDuration(avFrame);
895-
});
894+
UniqueAVFrame avFrame =
895+
decodeAVFrame([startPts](const UniqueAVFrame& avFrame) {
896+
return startPts < avFrame->pts + getDuration(avFrame);
897+
});
896898
// TODO: it's not great that we are getting a FrameOutput, which is
897899
// intended for videos. We should consider bypassing
898900
// convertAVFrameToFrameOutput and directly call
@@ -1035,7 +1037,7 @@ void VideoDecoder::maybeSeekToBeforeDesiredPts() {
10351037
// --------------------------------------------------------------------------
10361038

10371039
UniqueAVFrame VideoDecoder::decodeAVFrame(
1038-
std::function<bool(AVFrame*)> filterFunction) {
1040+
std::function<bool(const UniqueAVFrame&)> filterFunction) {
10391041
validateActiveStream();
10401042

10411043
resetDecodeStats();
@@ -1063,7 +1065,7 @@ UniqueAVFrame VideoDecoder::decodeAVFrame(
10631065

10641066
decodeStats_.numFramesReceivedByDecoder++;
10651067
// Is this the kind of frame we're looking for?
1066-
if (status == AVSUCCESS && filterFunction(avFrame.get())) {
1068+
if (status == AVSUCCESS && filterFunction(avFrame)) {
10671069
// Yes, this is the frame we'll return; break out of the decoding loop.
10681070
break;
10691071
} else if (status == AVSUCCESS) {

src/torchcodec/decoders/_core/VideoDecoder.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,8 @@ class VideoDecoder {
359359

360360
void maybeSeekToBeforeDesiredPts();
361361

362-
UniqueAVFrame decodeAVFrame(std::function<bool(AVFrame*)> filterFunction);
362+
UniqueAVFrame decodeAVFrame(
363+
std::function<bool(const UniqueAVFrame&)> filterFunction);
363364

364365
FrameOutput getNextFrameInternal(
365366
std::optional<torch::Tensor> preAllocatedOutputTensor = std::nullopt);

0 commit comments

Comments
 (0)