Skip to content

Commit 64d1b0d

Browse files
authored
Remove use of AVCodecPtr in interface definition (#453)
1 parent b7f8e0c commit 64d1b0d

File tree

5 files changed

+23
-8
lines changed

5 files changed

+23
-8
lines changed

src/torchcodec/decoders/_core/CPUOnlyDevice.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void releaseContextOnCuda(
3535
throwUnsupportedDeviceError(device);
3636
}
3737

38-
std::optional<AVCodecPtr> findCudaCodec(
38+
std::optional<const AVCodec*> findCudaCodec(
3939
const torch::Device& device,
4040
[[maybe_unused]] const AVCodecID& codecId) {
4141
throwUnsupportedDeviceError(device);

src/torchcodec/decoders/_core/DeviceInterface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void releaseContextOnCuda(
4040
const torch::Device& device,
4141
AVCodecContext* codecContext);
4242

43-
std::optional<AVCodecPtr> findCudaCodec(
43+
std::optional<const AVCodec*> findCudaCodec(
4444
const torch::Device& device,
4545
const AVCodecID& codecId);
4646

src/torchcodec/decoders/_core/FFMPEGCommon.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@
1010

1111
namespace facebook::torchcodec {
1212

13+
AVCodecOnlyUseForCallingAVFindBestStream
14+
makeAVCodecOnlyUseForCallingAVFindBestStream(const AVCodec* codec) {
15+
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 18, 100)
16+
return const_cast<AVCodec*>(codec);
17+
#else
18+
return codec;
19+
#endif
20+
}
21+
1322
std::string getFFMPEGErrorStringFromErrorCode(int errorCode) {
1423
char errorBuffer[AV_ERROR_MAX_STRING_SIZE] = {0};
1524
av_strerror(errorCode, errorBuffer, AV_ERROR_MAX_STRING_SIZE);

src/torchcodec/decoders/_core/FFMPEGCommon.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,17 @@ using UniqueSwsContext =
7575
// which was released in FFMPEG version=5.0.3
7676
// with libavcodec's version=59.18.100
7777
// (https://www.ffmpeg.org/olddownload.html).
78+
// Note that the alias is so-named so that it is only used when interacting with
79+
// av_find_best_stream(). It is not needed elsewhere.
7880
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 18, 100)
79-
using AVCodecPtr = AVCodec*;
81+
using AVCodecOnlyUseForCallingAVFindBestStream = AVCodec*;
8082
#else
81-
using AVCodecPtr = const AVCodec*;
83+
using AVCodecOnlyUseForCallingAVFindBestStream = const AVCodec*;
8284
#endif
8385

86+
AVCodecOnlyUseForCallingAVFindBestStream
87+
makeAVCodecOnlyUseForCallingAVFindBestStream(const AVCodec* codec);
88+
8489
// Success code from FFMPEG is just a 0. We define it to make the code more
8590
// readable.
8691
const int AVSUCCESS = 0;

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ void VideoDecoder::createFilterGraph(
426426
}
427427

428428
int VideoDecoder::getBestStreamIndex(AVMediaType mediaType) {
429-
AVCodecPtr codec = nullptr;
429+
AVCodecOnlyUseForCallingAVFindBestStream codec = nullptr;
430430
int streamNumber =
431431
av_find_best_stream(formatContext_.get(), mediaType, -1, -1, &codec, 0);
432432
return streamNumber;
@@ -441,7 +441,7 @@ void VideoDecoder::addVideoStreamDecoder(
441441
" is already active.");
442442
}
443443
TORCH_CHECK(formatContext_.get() != nullptr);
444-
AVCodecPtr codec = nullptr;
444+
AVCodecOnlyUseForCallingAVFindBestStream codec = nullptr;
445445
int streamNumber = av_find_best_stream(
446446
formatContext_.get(),
447447
AVMEDIA_TYPE_VIDEO,
@@ -464,8 +464,9 @@ void VideoDecoder::addVideoStreamDecoder(
464464
}
465465

466466
if (options.device.type() == torch::kCUDA) {
467-
codec = findCudaCodec(options.device, streamInfo.stream->codecpar->codec_id)
468-
.value_or(codec);
467+
codec = makeAVCodecOnlyUseForCallingAVFindBestStream(
468+
findCudaCodec(options.device, streamInfo.stream->codecpar->codec_id)
469+
.value_or(codec));
469470
}
470471

471472
AVCodecContext* codecContext = avcodec_alloc_context3(codec);

0 commit comments

Comments
 (0)