Skip to content

Commit 793c876

Browse files
committed
Merge branch 'main' of github.com:pytorch/torchcodec into renamingzzzz
2 parents 55a5840 + 0ad3f01 commit 793c876

File tree

6 files changed

+30
-13
lines changed

6 files changed

+30
-13
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: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,9 @@ void VideoDecoder::createFilterGraph(
439439
}
440440

441441
int VideoDecoder::getBestStreamIndex(AVMediaType mediaType) {
442-
AVCodecPtr codec = nullptr;
442+
AVCodecOnlyUseForCallingAVFindBestStream avCodec = nullptr;
443443
int streamIndex =
444-
av_find_best_stream(formatContext_.get(), mediaType, -1, -1, &codec, 0);
444+
av_find_best_stream(formatContext_.get(), mediaType, -1, -1, &avCodec, 0);
445445
return streamIndex;
446446
}
447447

@@ -455,18 +455,18 @@ void VideoDecoder::addVideoStreamDecoder(
455455
}
456456
TORCH_CHECK(formatContext_.get() != nullptr);
457457

458-
AVCodecPtr codec = nullptr;
458+
AVCodecOnlyUseForCallingAVFindBestStream avCodec = nullptr;
459459
int streamIndex = av_find_best_stream(
460460
formatContext_.get(),
461461
AVMEDIA_TYPE_VIDEO,
462462
preferredStreamIndex,
463463
-1,
464-
&codec,
464+
&avCodec,
465465
0);
466466
if (streamIndex < 0) {
467467
throw std::invalid_argument("No valid stream found in input file.");
468468
}
469-
TORCH_CHECK(codec != nullptr);
469+
TORCH_CHECK(avCodec != nullptr);
470470

471471
StreamMetadata& streamMetadata =
472472
containerMetadata_.streamMetadatas[streamIndex];
@@ -489,13 +489,13 @@ void VideoDecoder::addVideoStreamDecoder(
489489
}
490490

491491
if (videoStreamOptions.device.type() == torch::kCUDA) {
492-
codec =
492+
avCodec = makeAVCodecOnlyUseForCallingAVFindBestStream(
493493
findCudaCodec(
494494
videoStreamOptions.device, streamInfo.stream->codecpar->codec_id)
495-
.value_or(codec);
495+
.value_or(avCodec));
496496
}
497497

498-
AVCodecContext* codecContext = avcodec_alloc_context3(codec);
498+
AVCodecContext* codecContext = avcodec_alloc_context3(avCodec);
499499
TORCH_CHECK(codecContext != nullptr);
500500
codecContext->thread_count = videoStreamOptions.ffmpegThreadCount.value_or(0);
501501
streamInfo.codecContext.reset(codecContext);
@@ -513,7 +513,7 @@ void VideoDecoder::addVideoStreamDecoder(
513513
false, "Invalid device type: " + videoStreamOptions.device.str());
514514
}
515515

516-
retVal = avcodec_open2(streamInfo.codecContext.get(), codec, nullptr);
516+
retVal = avcodec_open2(streamInfo.codecContext.get(), avCodec, nullptr);
517517
if (retVal < AVSUCCESS) {
518518
throw std::invalid_argument(getFFMPEGErrorStringFromErrorCode(retVal));
519519
}

test/decoders/test_video_decoder.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,9 @@ def test_get_frames_at_fails(self, device, seek_mode):
428428

429429
@pytest.mark.parametrize("device", cpu_and_cuda())
430430
def test_get_frame_at_av1(self, device):
431+
if in_fbcode() and device == "cuda":
432+
return
433+
431434
decoder = VideoDecoder(AV1_VIDEO.path, device=device)
432435
ref_frame10 = AV1_VIDEO.get_frame_data_by_index(10)
433436
ref_frame_info10 = AV1_VIDEO.get_frame_info(10)

0 commit comments

Comments
 (0)