Skip to content

Remove use of AVCodecPtr in interface definition #453

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 5 commits into from
Jan 22, 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 @@ -35,7 +35,7 @@ void releaseContextOnCuda(
throwUnsupportedDeviceError(device);
}

std::optional<AVCodecPtr> findCudaCodec(
std::optional<const AVCodec*> findCudaCodec(
const torch::Device& device,
[[maybe_unused]] const AVCodecID& codecId) {
throwUnsupportedDeviceError(device);
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 @@ -40,7 +40,7 @@ void releaseContextOnCuda(
const torch::Device& device,
AVCodecContext* codecContext);

std::optional<AVCodecPtr> findCudaCodec(
std::optional<const AVCodec*> findCudaCodec(
const torch::Device& device,
const AVCodecID& codecId);

Expand Down
9 changes: 9 additions & 0 deletions src/torchcodec/decoders/_core/FFMPEGCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@

namespace facebook::torchcodec {

AVCodecOnlyUseForCallingAVFindBestStream
makeAVCodecOnlyUseForCallingAVFindBestStream(const AVCodec* codec) {
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 18, 100)
return const_cast<AVCodec*>(codec);
#else
return codec;
#endif
}

std::string getFFMPEGErrorStringFromErrorCode(int errorCode) {
char errorBuffer[AV_ERROR_MAX_STRING_SIZE] = {0};
av_strerror(errorCode, errorBuffer, AV_ERROR_MAX_STRING_SIZE);
Expand Down
9 changes: 7 additions & 2 deletions src/torchcodec/decoders/_core/FFMPEGCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,17 @@ using UniqueSwsContext =
// which was released in FFMPEG version=5.0.3
// with libavcodec's version=59.18.100
// (https://www.ffmpeg.org/olddownload.html).
// Note that the alias is so-named so that it is only used when interacting with
// av_find_best_stream(). It is not needed elsewhere.
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 18, 100)
using AVCodecPtr = AVCodec*;
using AVCodecOnlyUseForCallingAVFindBestStream = AVCodec*;
#else
using AVCodecPtr = const AVCodec*;
using AVCodecOnlyUseForCallingAVFindBestStream = const AVCodec*;
#endif

AVCodecOnlyUseForCallingAVFindBestStream
makeAVCodecOnlyUseForCallingAVFindBestStream(const AVCodec* codec);

// Success code from FFMPEG is just a 0. We define it to make the code more
// readable.
const int AVSUCCESS = 0;
Expand Down
9 changes: 5 additions & 4 deletions src/torchcodec/decoders/_core/VideoDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ void VideoDecoder::createFilterGraph(
}

int VideoDecoder::getBestStreamIndex(AVMediaType mediaType) {
AVCodecPtr codec = nullptr;
AVCodecOnlyUseForCallingAVFindBestStream codec = nullptr;
int streamNumber =
av_find_best_stream(formatContext_.get(), mediaType, -1, -1, &codec, 0);
return streamNumber;
Expand All @@ -441,7 +441,7 @@ void VideoDecoder::addVideoStreamDecoder(
" is already active.");
}
TORCH_CHECK(formatContext_.get() != nullptr);
AVCodecPtr codec = nullptr;
AVCodecOnlyUseForCallingAVFindBestStream codec = nullptr;
int streamNumber = av_find_best_stream(
formatContext_.get(),
AVMEDIA_TYPE_VIDEO,
Expand All @@ -464,8 +464,9 @@ void VideoDecoder::addVideoStreamDecoder(
}

if (options.device.type() == torch::kCUDA) {
codec = findCudaCodec(options.device, streamInfo.stream->codecpar->codec_id)
.value_or(codec);
codec = makeAVCodecOnlyUseForCallingAVFindBestStream(
findCudaCodec(options.device, streamInfo.stream->codecpar->codec_id)
.value_or(codec));
}

AVCodecContext* codecContext = avcodec_alloc_context3(codec);
Expand Down
Loading