Skip to content

Remove AVFrameStream struct and remove raw AVFrame* pointers #572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/torchcodec/decoders/_core/CPUOnlyDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace facebook::torchcodec {
void convertAVFrameToFrameOutputOnCuda(
const torch::Device& device,
[[maybe_unused]] const VideoDecoder::VideoStreamOptions& videoStreamOptions,
[[maybe_unused]] VideoDecoder::AVFrameStream& avFrameStream,
[[maybe_unused]] UniqueAVFrame& avFrame,
[[maybe_unused]] VideoDecoder::FrameOutput& frameOutput,
[[maybe_unused]] std::optional<torch::Tensor> preAllocatedOutputTensor) {
throwUnsupportedDeviceError(device);
Expand Down
6 changes: 2 additions & 4 deletions src/torchcodec/decoders/_core/CudaDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,17 +190,15 @@ void initializeContextOnCuda(
void convertAVFrameToFrameOutputOnCuda(
const torch::Device& device,
const VideoDecoder::VideoStreamOptions& videoStreamOptions,
VideoDecoder::AVFrameStream& avFrameStream,
UniqueAVFrame& avFrame,
VideoDecoder::FrameOutput& frameOutput,
std::optional<torch::Tensor> preAllocatedOutputTensor) {
AVFrame* avFrame = avFrameStream.avFrame.get();

TORCH_CHECK(
avFrame->format == AV_PIX_FMT_CUDA,
"Expected format to be AV_PIX_FMT_CUDA, got " +
std::string(av_get_pix_fmt_name((AVPixelFormat)avFrame->format)));
auto frameDims =
getHeightAndWidthFromOptionsOrAVFrame(videoStreamOptions, *avFrame);
getHeightAndWidthFromOptionsOrAVFrame(videoStreamOptions, avFrame);
int height = frameDims.height;
int width = frameDims.width;
torch::Tensor& dst = frameOutput.data;
Expand Down
2 changes: 1 addition & 1 deletion src/torchcodec/decoders/_core/DeviceInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void initializeContextOnCuda(
void convertAVFrameToFrameOutputOnCuda(
const torch::Device& device,
const VideoDecoder::VideoStreamOptions& videoStreamOptions,
VideoDecoder::AVFrameStream& avFrameStream,
UniqueAVFrame& avFrame,
VideoDecoder::FrameOutput& frameOutput,
std::optional<torch::Tensor> preAllocatedOutputTensor = std::nullopt);

Expand Down
10 changes: 3 additions & 7 deletions src/torchcodec/decoders/_core/FFMPEGCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,11 @@ std::string getFFMPEGErrorStringFromErrorCode(int errorCode) {
return std::string(errorBuffer);
}

int64_t getDuration(const UniqueAVFrame& frame) {
return getDuration(frame.get());
}

int64_t getDuration(const AVFrame* frame) {
int64_t getDuration(const UniqueAVFrame& avFrame) {
#if LIBAVUTIL_VERSION_MAJOR < 58
return frame->pkt_duration;
return avFrame->pkt_duration;
#else
return frame->duration;
return avFrame->duration;
#endif
}

Expand Down
1 change: 0 additions & 1 deletion src/torchcodec/decoders/_core/FFMPEGCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ std::string getFFMPEGErrorStringFromErrorCode(int errorCode);
// struct member representing duration has changed across the versions we
// support.
int64_t getDuration(const UniqueAVFrame& frame);
int64_t getDuration(const AVFrame* frame);

int getNumChannels(const UniqueAVFrame& avFrame);
int getNumChannels(const UniqueAVCodecContext& avCodecContext);
Expand Down
73 changes: 36 additions & 37 deletions src/torchcodec/decoders/_core/VideoDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,9 +583,9 @@ VideoDecoder::FrameOutput VideoDecoder::getNextFrame() {
VideoDecoder::FrameOutput VideoDecoder::getNextFrameInternal(
std::optional<torch::Tensor> preAllocatedOutputTensor) {
validateActiveStream();
AVFrameStream avFrameStream = decodeAVFrame(
[this](AVFrame* avFrame) { return avFrame->pts >= cursor_; });
return convertAVFrameToFrameOutput(avFrameStream, preAllocatedOutputTensor);
UniqueAVFrame avFrame = decodeAVFrame(
[this](const UniqueAVFrame& avFrame) { return avFrame->pts >= cursor_; });
return convertAVFrameToFrameOutput(avFrame, preAllocatedOutputTensor);
}

VideoDecoder::FrameOutput VideoDecoder::getFrameAtIndex(int64_t frameIndex) {
Expand Down Expand Up @@ -715,8 +715,8 @@ VideoDecoder::FrameOutput VideoDecoder::getFramePlayedAt(double seconds) {
}

setCursorPtsInSeconds(seconds);
AVFrameStream avFrameStream =
decodeAVFrame([seconds, this](AVFrame* avFrame) {
UniqueAVFrame avFrame =
decodeAVFrame([seconds, this](const UniqueAVFrame& avFrame) {
StreamInfo& streamInfo = streamInfos_[activeStreamIndex_];
double frameStartTime = ptsToSeconds(avFrame->pts, streamInfo.timeBase);
double frameEndTime = ptsToSeconds(
Expand All @@ -735,7 +735,7 @@ VideoDecoder::FrameOutput VideoDecoder::getFramePlayedAt(double seconds) {
});

// Convert the frame to tensor.
FrameOutput frameOutput = convertAVFrameToFrameOutput(avFrameStream);
FrameOutput frameOutput = convertAVFrameToFrameOutput(avFrame);
frameOutput.data = maybePermuteHWC2CHW(frameOutput.data);
return frameOutput;
}
Expand Down Expand Up @@ -891,14 +891,15 @@ VideoDecoder::AudioFramesOutput VideoDecoder::getFramesPlayedInRangeAudio(
auto finished = false;
while (!finished) {
try {
AVFrameStream avFrameStream = decodeAVFrame([startPts](AVFrame* avFrame) {
return startPts < avFrame->pts + getDuration(avFrame);
});
UniqueAVFrame avFrame =
decodeAVFrame([startPts](const UniqueAVFrame& avFrame) {
return startPts < avFrame->pts + getDuration(avFrame);
});
// TODO: it's not great that we are getting a FrameOutput, which is
// intended for videos. We should consider bypassing
// convertAVFrameToFrameOutput and directly call
// convertAudioAVFrameToFrameOutputOnCPU.
auto frameOutput = convertAVFrameToFrameOutput(avFrameStream);
auto frameOutput = convertAVFrameToFrameOutput(avFrame);
firstFramePtsSeconds =
std::min(firstFramePtsSeconds, frameOutput.ptsSeconds);
frames.push_back(frameOutput.data);
Expand Down Expand Up @@ -1035,8 +1036,8 @@ void VideoDecoder::maybeSeekToBeforeDesiredPts() {
// LOW-LEVEL DECODING
// --------------------------------------------------------------------------

VideoDecoder::AVFrameStream VideoDecoder::decodeAVFrame(
std::function<bool(AVFrame*)> filterFunction) {
UniqueAVFrame VideoDecoder::decodeAVFrame(
std::function<bool(const UniqueAVFrame&)> filterFunction) {
validateActiveStream();

resetDecodeStats();
Expand Down Expand Up @@ -1064,7 +1065,7 @@ VideoDecoder::AVFrameStream VideoDecoder::decodeAVFrame(

decodeStats_.numFramesReceivedByDecoder++;
// Is this the kind of frame we're looking for?
if (status == AVSUCCESS && filterFunction(avFrame.get())) {
if (status == AVSUCCESS && filterFunction(avFrame)) {
// Yes, this is the frame we'll return; break out of the decoding loop.
break;
} else if (status == AVSUCCESS) {
Expand Down Expand Up @@ -1150,37 +1151,36 @@ VideoDecoder::AVFrameStream VideoDecoder::decodeAVFrame(
streamInfo.lastDecodedAvFramePts = avFrame->pts;
streamInfo.lastDecodedAvFrameDuration = getDuration(avFrame);

return AVFrameStream(std::move(avFrame), activeStreamIndex_);
return avFrame;
}

// --------------------------------------------------------------------------
// AVFRAME <-> FRAME OUTPUT CONVERSION
// --------------------------------------------------------------------------

VideoDecoder::FrameOutput VideoDecoder::convertAVFrameToFrameOutput(
VideoDecoder::AVFrameStream& avFrameStream,
UniqueAVFrame& avFrame,
std::optional<torch::Tensor> preAllocatedOutputTensor) {
// Convert the frame to tensor.
FrameOutput frameOutput;
int streamIndex = avFrameStream.streamIndex;
AVFrame* avFrame = avFrameStream.avFrame.get();
frameOutput.streamIndex = streamIndex;
auto& streamInfo = streamInfos_[streamIndex];
frameOutput.streamIndex = activeStreamIndex_;
auto& streamInfo = streamInfos_[activeStreamIndex_];
frameOutput.ptsSeconds = ptsToSeconds(
avFrame->pts, formatContext_->streams[streamIndex]->time_base);
avFrame->pts, formatContext_->streams[activeStreamIndex_]->time_base);
frameOutput.durationSeconds = ptsToSeconds(
getDuration(avFrame), formatContext_->streams[streamIndex]->time_base);
getDuration(avFrame),
formatContext_->streams[activeStreamIndex_]->time_base);
if (streamInfo.avMediaType == AVMEDIA_TYPE_AUDIO) {
convertAudioAVFrameToFrameOutputOnCPU(
avFrameStream, frameOutput, preAllocatedOutputTensor);
avFrame, frameOutput, preAllocatedOutputTensor);
} else if (streamInfo.videoStreamOptions.device.type() == torch::kCPU) {
convertAVFrameToFrameOutputOnCPU(
avFrameStream, frameOutput, preAllocatedOutputTensor);
avFrame, frameOutput, preAllocatedOutputTensor);
} else if (streamInfo.videoStreamOptions.device.type() == torch::kCUDA) {
convertAVFrameToFrameOutputOnCuda(
streamInfo.videoStreamOptions.device,
streamInfo.videoStreamOptions,
avFrameStream,
avFrame,
frameOutput,
preAllocatedOutputTensor);
} else {
Expand All @@ -1201,14 +1201,13 @@ VideoDecoder::FrameOutput VideoDecoder::convertAVFrameToFrameOutput(
// Dimension order of the preAllocatedOutputTensor must be HWC, regardless of
// `dimension_order` parameter. It's up to callers to re-shape it if needed.
void VideoDecoder::convertAVFrameToFrameOutputOnCPU(
VideoDecoder::AVFrameStream& avFrameStream,
UniqueAVFrame& avFrame,
FrameOutput& frameOutput,
std::optional<torch::Tensor> preAllocatedOutputTensor) {
AVFrame* avFrame = avFrameStream.avFrame.get();
auto& streamInfo = streamInfos_[activeStreamIndex_];

auto frameDims = getHeightAndWidthFromOptionsOrAVFrame(
streamInfo.videoStreamOptions, *avFrame);
streamInfo.videoStreamOptions, avFrame);
int expectedOutputHeight = frameDims.height;
int expectedOutputWidth = frameDims.width;

Expand Down Expand Up @@ -1302,7 +1301,7 @@ void VideoDecoder::convertAVFrameToFrameOutputOnCPU(
}

int VideoDecoder::convertAVFrameToTensorUsingSwsScale(
const AVFrame* avFrame,
const UniqueAVFrame& avFrame,
torch::Tensor& outputTensor) {
StreamInfo& activeStreamInfo = streamInfos_[activeStreamIndex_];
SwsContext* swsContext = activeStreamInfo.swsContext.get();
Expand All @@ -1322,11 +1321,11 @@ int VideoDecoder::convertAVFrameToTensorUsingSwsScale(
}

torch::Tensor VideoDecoder::convertAVFrameToTensorUsingFilterGraph(
const AVFrame* avFrame) {
const UniqueAVFrame& avFrame) {
FilterGraphContext& filterGraphContext =
streamInfos_[activeStreamIndex_].filterGraphContext;
int status =
av_buffersrc_write_frame(filterGraphContext.sourceContext, avFrame);
av_buffersrc_write_frame(filterGraphContext.sourceContext, avFrame.get());
if (status < AVSUCCESS) {
throw std::runtime_error("Failed to add frame to buffer source context");
}
Expand All @@ -1350,25 +1349,25 @@ torch::Tensor VideoDecoder::convertAVFrameToTensorUsingFilterGraph(
}

void VideoDecoder::convertAudioAVFrameToFrameOutputOnCPU(
VideoDecoder::AVFrameStream& avFrameStream,
UniqueAVFrame& srcAVFrame,
FrameOutput& frameOutput,
std::optional<torch::Tensor> preAllocatedOutputTensor) {
TORCH_CHECK(
!preAllocatedOutputTensor.has_value(),
"pre-allocated audio tensor not supported yet.");

AVSampleFormat sourceSampleFormat =
static_cast<AVSampleFormat>(avFrameStream.avFrame->format);
static_cast<AVSampleFormat>(srcAVFrame->format);
AVSampleFormat desiredSampleFormat = AV_SAMPLE_FMT_FLTP;

UniqueAVFrame convertedAVFrame;
if (sourceSampleFormat != desiredSampleFormat) {
convertedAVFrame = convertAudioAVFrameSampleFormat(
avFrameStream.avFrame, sourceSampleFormat, desiredSampleFormat);
srcAVFrame, sourceSampleFormat, desiredSampleFormat);
}
const UniqueAVFrame& avFrame = (sourceSampleFormat != desiredSampleFormat)
? convertedAVFrame
: avFrameStream.avFrame;
: srcAVFrame;

AVSampleFormat format = static_cast<AVSampleFormat>(avFrame->format);
TORCH_CHECK(
Expand Down Expand Up @@ -1944,10 +1943,10 @@ FrameDims getHeightAndWidthFromOptionsOrMetadata(

FrameDims getHeightAndWidthFromOptionsOrAVFrame(
const VideoDecoder::VideoStreamOptions& videoStreamOptions,
const AVFrame& avFrame) {
const UniqueAVFrame& avFrame) {
return FrameDims(
videoStreamOptions.height.value_or(avFrame.height),
videoStreamOptions.width.value_or(avFrame.width));
videoStreamOptions.height.value_or(avFrame->height),
videoStreamOptions.width.value_or(avFrame->width));
}

} // namespace facebook::torchcodec
33 changes: 9 additions & 24 deletions src/torchcodec/decoders/_core/VideoDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,23 +244,6 @@ class VideoDecoder {
// These are APIs that should be private, but that are effectively exposed for
// practical reasons, typically for testing purposes.

// This struct is needed because AVFrame doesn't retain the streamIndex. Only
// the AVPacket knows its stream. This is what the low-level private decoding
// entry points return. The AVFrameStream is then converted to a FrameOutput
// with convertAVFrameToFrameOutput. It should be private, but is currently
// used by DeviceInterface.
struct AVFrameStream {
// The actual decoded output as a unique pointer to an AVFrame.
// Usually, this is a YUV frame. It'll be converted to RGB in
// convertAVFrameToFrameOutput.
UniqueAVFrame avFrame;
// The stream index of the decoded frame.
int streamIndex;

explicit AVFrameStream(UniqueAVFrame&& a, int s)
: avFrame(std::move(a)), streamIndex(s) {}
};

// Once getFrameAtIndex supports the preAllocatedOutputTensor parameter, we
// can move it back to private.
FrameOutput getFrameAtIndexInternal(
Expand Down Expand Up @@ -376,31 +359,33 @@ class VideoDecoder {

void maybeSeekToBeforeDesiredPts();

AVFrameStream decodeAVFrame(std::function<bool(AVFrame*)> filterFunction);
UniqueAVFrame decodeAVFrame(
std::function<bool(const UniqueAVFrame&)> filterFunction);

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

torch::Tensor maybePermuteHWC2CHW(torch::Tensor& hwcTensor);

FrameOutput convertAVFrameToFrameOutput(
AVFrameStream& avFrameStream,
UniqueAVFrame& avFrame,
std::optional<torch::Tensor> preAllocatedOutputTensor = std::nullopt);

void convertAVFrameToFrameOutputOnCPU(
AVFrameStream& avFrameStream,
UniqueAVFrame& avFrame,
FrameOutput& frameOutput,
std::optional<torch::Tensor> preAllocatedOutputTensor = std::nullopt);

void convertAudioAVFrameToFrameOutputOnCPU(
AVFrameStream& avFrameStream,
UniqueAVFrame& srcAVFrame,
FrameOutput& frameOutput,
std::optional<torch::Tensor> preAllocatedOutputTensor = std::nullopt);

torch::Tensor convertAVFrameToTensorUsingFilterGraph(const AVFrame* avFrame);
torch::Tensor convertAVFrameToTensorUsingFilterGraph(
const UniqueAVFrame& avFrame);

int convertAVFrameToTensorUsingSwsScale(
const AVFrame* avFrame,
const UniqueAVFrame& avFrame,
torch::Tensor& outputTensor);

UniqueAVFrame convertAudioAVFrameSampleFormat(
Expand Down Expand Up @@ -568,7 +553,7 @@ FrameDims getHeightAndWidthFromOptionsOrMetadata(

FrameDims getHeightAndWidthFromOptionsOrAVFrame(
const VideoDecoder::VideoStreamOptions& videoStreamOptions,
const AVFrame& avFrame);
const UniqueAVFrame& avFrame);

torch::Tensor allocateEmptyHWCTensor(
int height,
Expand Down
Loading