Skip to content

Make C++ compilation strict, treat warnings as errors #452

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 18 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions src/torchcodec/decoders/_core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.18)
project(TorchCodec)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_compile_options(-Wall -Wextra -pedantic -Werror)
Copy link
Member

@NicolasHug NicolasHug Jan 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a non-exhaustive google search and it looks like add_compile_options is similar to setting CMAKE_CXX_FLAGS (as we do below line 7-8), with a different scope.
While some sources suggest that add_compile_options is better scoped, I feel like we should stick to setting CMAKE_CXX_FLAGS here. Because a) this is what we already do, so it's best to only have one way to set flags, and b) we want to set the flags of the entire project anyway, for all targets, which is what CMAKE_CXX_FLAGS does.


find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
Expand Down
14 changes: 7 additions & 7 deletions src/torchcodec/decoders/_core/CPUOnlyDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,28 @@ namespace facebook::torchcodec {

void convertAVFrameToDecodedOutputOnCuda(
const torch::Device& device,
const VideoDecoder::VideoStreamDecoderOptions& options,
VideoDecoder::RawDecodedOutput& rawOutput,
VideoDecoder::DecodedOutput& output,
std::optional<torch::Tensor> preAllocatedOutputTensor) {
[[maybe_unused]] const VideoDecoder::VideoStreamDecoderOptions& options,
[[maybe_unused]] VideoDecoder::RawDecodedOutput& rawOutput,
[[maybe_unused]] VideoDecoder::DecodedOutput& output,
[[maybe_unused]] std::optional<torch::Tensor> preAllocatedOutputTensor) {
throwUnsupportedDeviceError(device);
}

void initializeContextOnCuda(
const torch::Device& device,
AVCodecContext* codecContext) {
[[maybe_unused]] AVCodecContext* codecContext) {
throwUnsupportedDeviceError(device);
}

void releaseContextOnCuda(
const torch::Device& device,
AVCodecContext* codecContext) {
[[maybe_unused]] AVCodecContext* codecContext) {
throwUnsupportedDeviceError(device);
}

std::optional<AVCodecPtr> findCudaCodec(
const torch::Device& device,
const AVCodecID& codecId) {
[[maybe_unused]] const AVCodecID& codecId) {
throwUnsupportedDeviceError(device);
}

Expand Down
6 changes: 3 additions & 3 deletions src/torchcodec/decoders/_core/CudaDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,10 @@ std::optional<AVCodecPtr> findCudaCodec(
const AVCodecID& codecId) {
throwErrorIfNonCudaDevice(device);

void* i = NULL;
void* i = nullptr;

AVCodecPtr c;
while (c = av_codec_iterate(&i)) {
const AVCodecPtr c;
while ((c = av_codec_iterate(&i))) {
const AVCodecHWConfig* config;

if (c->id != codecId || !av_codec_is_decoder(c)) {
Expand Down
5 changes: 3 additions & 2 deletions src/torchcodec/decoders/_core/FFMPEGCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ int64_t getDuration(const UniqueAVFrame& frame) {
}

int64_t getDuration(const AVFrame* frame) {
#if LIBAVUTIL_VERSION_MAJOR < 59
#if LIBAVUTIL_VERSION_MAJOR < 58
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we just had this wrong. It was deprecated in 58.

return frame->pkt_duration;
#else
return frame->duration;
Expand Down Expand Up @@ -75,7 +75,8 @@ int AVIOBytesContext::read(void* opaque, uint8_t* buf, int buf_size) {
bufferData->current,
", size=",
bufferData->size);
buf_size = FFMIN(buf_size, bufferData->size - bufferData->current);
buf_size =
FFMIN(buf_size, static_cast<int>(bufferData->size - bufferData->current));
TORCH_CHECK(
buf_size >= 0,
"Tried to read negative bytes: buf_size=",
Expand Down
26 changes: 14 additions & 12 deletions src/torchcodec/decoders/_core/VideoDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ VideoDecoder::BatchDecodedOutput::BatchDecodedOutput(

bool VideoDecoder::DecodedFrameContext::operator==(
const VideoDecoder::DecodedFrameContext& other) {
return decodedWidth == other.decodedWidth && decodedHeight == decodedHeight &&
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the stricter compilation revealed a real logic error here, as we had the code decodedHeight == decodedHeight.

return decodedWidth == other.decodedWidth &&
decodedHeight == other.decodedHeight &&
decodedFormat == other.decodedFormat &&
expectedWidth == other.expectedWidth &&
expectedHeight == other.expectedHeight;
Expand Down Expand Up @@ -249,12 +250,12 @@ void VideoDecoder::initializeDecoder() {
getFFMPEGErrorStringFromErrorCode(ffmpegStatus));
}

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

TORCH_CHECK(
i == stream->index,
static_cast<int>(i) == stream->index,
"Our stream index, " + std::to_string(i) +
", does not match AVStream's index, " +
std::to_string(stream->index) + ".");
Expand Down Expand Up @@ -577,7 +578,7 @@ void VideoDecoder::scanFileAndUpdateMetadataAndIndex() {
}
streams_[streamIndex].allFrames.push_back(frameInfo);
}
for (int i = 0; i < containerMetadata_.streams.size(); ++i) {
for (size_t i = 0; i < containerMetadata_.streams.size(); ++i) {
auto& streamMetadata = containerMetadata_.streams[i];
auto stream = formatContext_->streams[i];
if (streamMetadata.minPtsFromScan.has_value()) {
Expand Down Expand Up @@ -610,7 +611,7 @@ void VideoDecoder::scanFileAndUpdateMetadataAndIndex() {
return frameInfo1.pts < frameInfo2.pts;
});

for (int i = 0; i < stream.allFrames.size(); ++i) {
for (size_t i = 0; i < stream.allFrames.size(); ++i) {
if (i + 1 < stream.allFrames.size()) {
stream.allFrames[i].nextPts = stream.allFrames[i + 1].pts;
}
Expand Down Expand Up @@ -1050,8 +1051,8 @@ VideoDecoder::DecodedOutput VideoDecoder::getFramePlayedAtTimestampNoDemux(
return output;
}

void VideoDecoder::validateUserProvidedStreamIndex(uint64_t streamIndex) {
size_t streamsSize = containerMetadata_.streams.size();
void VideoDecoder::validateUserProvidedStreamIndex(int streamIndex) {
int streamsSize = static_cast<int>(containerMetadata_.streams.size());
TORCH_CHECK(
streamIndex >= 0 && streamIndex < streamsSize,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we had declared streamIndex as uint64_t, this check for streamIndex >= 0 was trivially always true.

"Invalid stream index=" + std::to_string(streamIndex) +
Expand All @@ -1074,7 +1075,7 @@ void VideoDecoder::validateFrameIndex(
const StreamInfo& stream,
int64_t frameIndex) {
TORCH_CHECK(
frameIndex >= 0 && frameIndex < stream.allFrames.size(),
frameIndex >= 0 && frameIndex < static_cast<int>(stream.allFrames.size()),
"Invalid frame index=" + std::to_string(frameIndex) +
" for streamIndex=" + std::to_string(stream.streamIndex) +
" numFrames=" + std::to_string(stream.allFrames.size()));
Expand Down Expand Up @@ -1134,10 +1135,11 @@ VideoDecoder::BatchDecodedOutput VideoDecoder::getFramesAtIndices(
BatchDecodedOutput output(frameIndices.size(), options, streamMetadata);

auto previousIndexInVideo = -1;
for (auto f = 0; f < frameIndices.size(); ++f) {
for (size_t f = 0; f < frameIndices.size(); ++f) {
auto indexInOutput = indicesAreSorted ? f : argsort[f];
auto indexInVideo = frameIndices[indexInOutput];
if (indexInVideo < 0 || indexInVideo >= stream.allFrames.size()) {
if (indexInVideo < 0 ||
indexInVideo >= static_cast<int>(stream.allFrames.size())) {
throw std::runtime_error(
"Invalid frame index=" + std::to_string(indexInVideo));
}
Expand Down Expand Up @@ -1180,7 +1182,7 @@ VideoDecoder::BatchDecodedOutput VideoDecoder::getFramesPlayedByTimestamps(
double maxSeconds = streamMetadata.maxPtsSecondsFromScan.value();

std::vector<int64_t> frameIndices(timestamps.size());
for (auto i = 0; i < timestamps.size(); ++i) {
for (size_t i = 0; i < timestamps.size(); ++i) {
auto framePts = timestamps[i];
TORCH_CHECK(
framePts >= minSeconds && framePts < maxSeconds,
Expand Down Expand Up @@ -1215,7 +1217,7 @@ VideoDecoder::BatchDecodedOutput VideoDecoder::getFramesInRange(
TORCH_CHECK(
start >= 0, "Range start, " + std::to_string(start) + " is less than 0.");
TORCH_CHECK(
stop <= stream.allFrames.size(),
stop <= static_cast<int64_t>(stream.allFrames.size()),
"Range stop, " + std::to_string(stop) +
", is more than the number of frames, " +
std::to_string(stream.allFrames.size()));
Expand Down
2 changes: 1 addition & 1 deletion src/torchcodec/decoders/_core/VideoDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ class VideoDecoder {
// for more details about the heuristics.
int getBestStreamIndex(AVMediaType mediaType);
void initializeDecoder();
void validateUserProvidedStreamIndex(uint64_t streamIndex);
void validateUserProvidedStreamIndex(int streamIndex);
void validateScannedAllStreams(const std::string& msg);
void validateFrameIndex(const StreamInfo& stream, int64_t frameIndex);
// Creates and initializes a filter graph for a stream. The filter graph can
Expand Down
3 changes: 2 additions & 1 deletion src/torchcodec/decoders/_core/VideoDecoderOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ std::string get_stream_json_metadata(
int64_t stream_index) {
auto videoDecoder = unwrapTensorToGetDecoder(decoder);
auto streams = videoDecoder->getContainerMetadata().streams;
if (stream_index < 0 || stream_index >= streams.size()) {
if (stream_index < 0 ||
stream_index >= static_cast<int64_t>(streams.size())) {
throw std::out_of_range(
"stream_index out of bounds: " + std::to_string(stream_index));
}
Expand Down
Loading