Skip to content

Commit d26bfbc

Browse files
authored
Make C++ compilation strict, treat warnings as errors (#452)
1 parent d8dde5c commit d26bfbc

File tree

9 files changed

+39
-41
lines changed

9 files changed

+39
-41
lines changed

.github/workflows/docs.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ jobs:
112112
run: |
113113
cd docs
114114
${CONDA_RUN} make html
115-
- uses: actions/upload-artifact@v3
115+
- uses: actions/upload-artifact@v4
116116
with:
117117
name: Built-Docs
118118
path: docs/build/html/

src/torchcodec/decoders/_core/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set(CMAKE_CXX_STANDARD 17)
44
set(CMAKE_CXX_STANDARD_REQUIRED ON)
55

66
find_package(Torch REQUIRED)
7-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
7+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -Werror ${TORCH_CXX_FLAGS}")
88
find_package(Python3 ${PYTHON_VERSION} EXACT COMPONENTS Development)
99

1010
function(make_torchcodec_library library_name ffmpeg_target)

src/torchcodec/decoders/_core/CPUOnlyDevice.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,28 @@ namespace facebook::torchcodec {
1616

1717
void convertAVFrameToDecodedOutputOnCuda(
1818
const torch::Device& device,
19-
const VideoDecoder::VideoStreamDecoderOptions& options,
20-
VideoDecoder::RawDecodedOutput& rawOutput,
21-
VideoDecoder::DecodedOutput& output,
22-
std::optional<torch::Tensor> preAllocatedOutputTensor) {
19+
[[maybe_unused]] const VideoDecoder::VideoStreamDecoderOptions& options,
20+
[[maybe_unused]] VideoDecoder::RawDecodedOutput& rawOutput,
21+
[[maybe_unused]] VideoDecoder::DecodedOutput& output,
22+
[[maybe_unused]] std::optional<torch::Tensor> preAllocatedOutputTensor) {
2323
throwUnsupportedDeviceError(device);
2424
}
2525

2626
void initializeContextOnCuda(
2727
const torch::Device& device,
28-
AVCodecContext* codecContext) {
28+
[[maybe_unused]] AVCodecContext* codecContext) {
2929
throwUnsupportedDeviceError(device);
3030
}
3131

3232
void releaseContextOnCuda(
3333
const torch::Device& device,
34-
AVCodecContext* codecContext) {
34+
[[maybe_unused]] AVCodecContext* codecContext) {
3535
throwUnsupportedDeviceError(device);
3636
}
3737

3838
std::optional<AVCodecPtr> findCudaCodec(
3939
const torch::Device& device,
40-
const AVCodecID& codecId) {
40+
[[maybe_unused]] const AVCodecID& codecId) {
4141
throwUnsupportedDeviceError(device);
4242
}
4343

src/torchcodec/decoders/_core/CudaDevice.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "src/torchcodec/decoders/_core/VideoDecoder.h"
1010

1111
extern "C" {
12-
#include <libavcodec/avcodec.h>
1312
#include <libavutil/hwcontext_cuda.h>
1413
#include <libavutil/pixdesc.h>
1514
}
@@ -110,7 +109,7 @@ AVBufferRef* getFFMPEGContextFromExistingCudaContext(
110109
#else
111110

112111
AVBufferRef* getFFMPEGContextFromNewCudaContext(
113-
const torch::Device& device,
112+
[[maybe_unused]] const torch::Device& device,
114113
torch::DeviceIndex nonNegativeDeviceIndex,
115114
enum AVHWDeviceType type) {
116115
AVBufferRef* hw_device_ctx = nullptr;
@@ -260,24 +259,23 @@ void convertAVFrameToDecodedOutputOnCuda(
260259
// we have to do this because of an FFmpeg bug where hardware decoding is not
261260
// appropriately set, so we just go off and find the matching codec for the CUDA
262261
// device
263-
std::optional<AVCodecPtr> findCudaCodec(
262+
std::optional<const AVCodec*> findCudaCodec(
264263
const torch::Device& device,
265264
const AVCodecID& codecId) {
266265
throwErrorIfNonCudaDevice(device);
267266

268-
void* i = NULL;
269-
270-
AVCodecPtr c;
271-
while (c = av_codec_iterate(&i)) {
272-
const AVCodecHWConfig* config;
273-
274-
if (c->id != codecId || !av_codec_is_decoder(c)) {
267+
void* i = nullptr;
268+
const AVCodec* codec = nullptr;
269+
while ((codec = av_codec_iterate(&i)) != nullptr) {
270+
if (codec->id != codecId || !av_codec_is_decoder(codec)) {
275271
continue;
276272
}
277273

278-
for (int j = 0; config = avcodec_get_hw_config(c, j); j++) {
274+
const AVCodecHWConfig* config = nullptr;
275+
for (int j = 0; (config = avcodec_get_hw_config(codec, j)) != nullptr;
276+
++j) {
279277
if (config->device_type == AV_HWDEVICE_TYPE_CUDA) {
280-
return c;
278+
return codec;
281279
}
282280
}
283281
}

src/torchcodec/decoders/_core/DeviceInterface.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
#include "FFMPEGCommon.h"
1414
#include "src/torchcodec/decoders/_core/VideoDecoder.h"
1515

16-
extern "C" {
17-
#include <libavcodec/avcodec.h>
18-
}
19-
2016
namespace facebook::torchcodec {
2117

2218
// Note that all these device functions should only be called if the device is

src/torchcodec/decoders/_core/FFMPEGCommon.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ int64_t getDuration(const UniqueAVFrame& frame) {
2121
}
2222

2323
int64_t getDuration(const AVFrame* frame) {
24-
#if LIBAVUTIL_VERSION_MAJOR < 59
24+
#if LIBAVUTIL_VERSION_MAJOR < 58
2525
return frame->pkt_duration;
2626
#else
2727
return frame->duration;
@@ -75,7 +75,8 @@ int AVIOBytesContext::read(void* opaque, uint8_t* buf, int buf_size) {
7575
bufferData->current,
7676
", size=",
7777
bufferData->size);
78-
buf_size = FFMIN(buf_size, bufferData->size - bufferData->current);
78+
buf_size =
79+
FFMIN(buf_size, static_cast<int>(bufferData->size - bufferData->current));
7980
TORCH_CHECK(
8081
buf_size >= 0,
8182
"Tried to read negative bytes: buf_size=",

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ VideoDecoder::BatchDecodedOutput::BatchDecodedOutput(
206206

207207
bool VideoDecoder::DecodedFrameContext::operator==(
208208
const VideoDecoder::DecodedFrameContext& other) {
209-
return decodedWidth == other.decodedWidth && decodedHeight == decodedHeight &&
209+
return decodedWidth == other.decodedWidth &&
210+
decodedHeight == other.decodedHeight &&
210211
decodedFormat == other.decodedFormat &&
211212
expectedWidth == other.expectedWidth &&
212213
expectedHeight == other.expectedHeight;
@@ -249,12 +250,12 @@ void VideoDecoder::initializeDecoder() {
249250
getFFMPEGErrorStringFromErrorCode(ffmpegStatus));
250251
}
251252

252-
for (int i = 0; i < formatContext_->nb_streams; i++) {
253+
for (unsigned int i = 0; i < formatContext_->nb_streams; i++) {
253254
AVStream* stream = formatContext_->streams[i];
254255
StreamMetadata meta;
255256

256257
TORCH_CHECK(
257-
i == stream->index,
258+
static_cast<int>(i) == stream->index,
258259
"Our stream index, " + std::to_string(i) +
259260
", does not match AVStream's index, " +
260261
std::to_string(stream->index) + ".");
@@ -577,7 +578,7 @@ void VideoDecoder::scanFileAndUpdateMetadataAndIndex() {
577578
}
578579
streams_[streamIndex].allFrames.push_back(frameInfo);
579580
}
580-
for (int i = 0; i < containerMetadata_.streams.size(); ++i) {
581+
for (size_t i = 0; i < containerMetadata_.streams.size(); ++i) {
581582
auto& streamMetadata = containerMetadata_.streams[i];
582583
auto stream = formatContext_->streams[i];
583584
if (streamMetadata.minPtsFromScan.has_value()) {
@@ -610,7 +611,7 @@ void VideoDecoder::scanFileAndUpdateMetadataAndIndex() {
610611
return frameInfo1.pts < frameInfo2.pts;
611612
});
612613

613-
for (int i = 0; i < stream.allFrames.size(); ++i) {
614+
for (size_t i = 0; i < stream.allFrames.size(); ++i) {
614615
if (i + 1 < stream.allFrames.size()) {
615616
stream.allFrames[i].nextPts = stream.allFrames[i + 1].pts;
616617
}
@@ -1050,8 +1051,8 @@ VideoDecoder::DecodedOutput VideoDecoder::getFramePlayedAtTimestampNoDemux(
10501051
return output;
10511052
}
10521053

1053-
void VideoDecoder::validateUserProvidedStreamIndex(uint64_t streamIndex) {
1054-
size_t streamsSize = containerMetadata_.streams.size();
1054+
void VideoDecoder::validateUserProvidedStreamIndex(int streamIndex) {
1055+
int streamsSize = static_cast<int>(containerMetadata_.streams.size());
10551056
TORCH_CHECK(
10561057
streamIndex >= 0 && streamIndex < streamsSize,
10571058
"Invalid stream index=" + std::to_string(streamIndex) +
@@ -1074,7 +1075,7 @@ void VideoDecoder::validateFrameIndex(
10741075
const StreamInfo& stream,
10751076
int64_t frameIndex) {
10761077
TORCH_CHECK(
1077-
frameIndex >= 0 && frameIndex < stream.allFrames.size(),
1078+
frameIndex >= 0 && frameIndex < static_cast<int>(stream.allFrames.size()),
10781079
"Invalid frame index=" + std::to_string(frameIndex) +
10791080
" for streamIndex=" + std::to_string(stream.streamIndex) +
10801081
" numFrames=" + std::to_string(stream.allFrames.size()));
@@ -1134,10 +1135,11 @@ VideoDecoder::BatchDecodedOutput VideoDecoder::getFramesAtIndices(
11341135
BatchDecodedOutput output(frameIndices.size(), options, streamMetadata);
11351136

11361137
auto previousIndexInVideo = -1;
1137-
for (auto f = 0; f < frameIndices.size(); ++f) {
1138+
for (size_t f = 0; f < frameIndices.size(); ++f) {
11381139
auto indexInOutput = indicesAreSorted ? f : argsort[f];
11391140
auto indexInVideo = frameIndices[indexInOutput];
1140-
if (indexInVideo < 0 || indexInVideo >= stream.allFrames.size()) {
1141+
if (indexInVideo < 0 ||
1142+
indexInVideo >= static_cast<int>(stream.allFrames.size())) {
11411143
throw std::runtime_error(
11421144
"Invalid frame index=" + std::to_string(indexInVideo));
11431145
}
@@ -1180,7 +1182,7 @@ VideoDecoder::BatchDecodedOutput VideoDecoder::getFramesPlayedByTimestamps(
11801182
double maxSeconds = streamMetadata.maxPtsSecondsFromScan.value();
11811183

11821184
std::vector<int64_t> frameIndices(timestamps.size());
1183-
for (auto i = 0; i < timestamps.size(); ++i) {
1185+
for (size_t i = 0; i < timestamps.size(); ++i) {
11841186
auto framePts = timestamps[i];
11851187
TORCH_CHECK(
11861188
framePts >= minSeconds && framePts < maxSeconds,
@@ -1215,7 +1217,7 @@ VideoDecoder::BatchDecodedOutput VideoDecoder::getFramesInRange(
12151217
TORCH_CHECK(
12161218
start >= 0, "Range start, " + std::to_string(start) + " is less than 0.");
12171219
TORCH_CHECK(
1218-
stop <= stream.allFrames.size(),
1220+
stop <= static_cast<int64_t>(stream.allFrames.size()),
12191221
"Range stop, " + std::to_string(stop) +
12201222
", is more than the number of frames, " +
12211223
std::to_string(stream.allFrames.size()));

src/torchcodec/decoders/_core/VideoDecoder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ class VideoDecoder {
366366
// for more details about the heuristics.
367367
int getBestStreamIndex(AVMediaType mediaType);
368368
void initializeDecoder();
369-
void validateUserProvidedStreamIndex(uint64_t streamIndex);
369+
void validateUserProvidedStreamIndex(int streamIndex);
370370
void validateScannedAllStreams(const std::string& msg);
371371
void validateFrameIndex(const StreamInfo& stream, int64_t frameIndex);
372372
// Creates and initializes a filter graph for a stream. The filter graph can

src/torchcodec/decoders/_core/VideoDecoderOps.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,8 @@ std::string get_stream_json_metadata(
396396
int64_t stream_index) {
397397
auto videoDecoder = unwrapTensorToGetDecoder(decoder);
398398
auto streams = videoDecoder->getContainerMetadata().streams;
399-
if (stream_index < 0 || stream_index >= streams.size()) {
399+
if (stream_index < 0 ||
400+
stream_index >= static_cast<int64_t>(streams.size())) {
400401
throw std::out_of_range(
401402
"stream_index out of bounds: " + std::to_string(stream_index));
402403
}

0 commit comments

Comments
 (0)