From e1d294619a1b76bfbe90add6400cbeb825b16cc4 Mon Sep 17 00:00:00 2001 From: Francisco Massa Date: Tue, 24 Mar 2020 07:17:04 -0700 Subject: [PATCH 1/2] Fix C++ lint --- torchvision/csrc/cpu/decoder/defs.h | 9 ++-- .../csrc/cpu/decoder/seekable_buffer.cpp | 5 +- torchvision/csrc/cpu/decoder/util.cpp | 33 ++++++------ torchvision/csrc/cpu/decoder/util_test.cpp | 52 ++++++++++--------- .../csrc/cpu/video_reader/VideoReader.cpp | 4 +- 5 files changed, 54 insertions(+), 49 deletions(-) diff --git a/torchvision/csrc/cpu/decoder/defs.h b/torchvision/csrc/cpu/decoder/defs.h index a53f2aeb2b0..5db17b3e92c 100644 --- a/torchvision/csrc/cpu/decoder/defs.h +++ b/torchvision/csrc/cpu/decoder/defs.h @@ -53,9 +53,11 @@ struct VideoFormat { When width = 0, height = 0, minDimension = 0, and maxDimension = 0, keep the orignal frame resolution When width = 0, height = 0, minDimension != 0, and maxDimension = 0, - keep the aspect ratio and resize the frame so that shorter edge size is minDimension + keep the aspect ratio and resize the frame so that shorter edge size is + minDimension When width = 0, height = 0, minDimension = 0, and maxDimension != 0, - keep the aspect ratio and resize the frame so that longer edge size is maxDimension + keep the aspect ratio and resize the frame so that longer edge size is + maxDimension When width = 0, height = 0, minDimension != 0, and maxDimension != 0, resize the frame so that shorter edge size is minDimension, and longer edge size is maxDimension. The aspect ratio may not be preserved @@ -64,7 +66,8 @@ struct VideoFormat { When width != 0, height = 0, minDimension = 0, and maxDimension = 0, keep the aspect ratio and resize the frame so that frame width is $width When width != 0, height != 0, minDimension = 0, and maxDimension = 0, - resize the frame so that frame width and height are set to $width and $height, + resize the frame so that frame width and height are set to $width and + $height, respectively */ size_t width{0}; // width in pixels diff --git a/torchvision/csrc/cpu/decoder/seekable_buffer.cpp b/torchvision/csrc/cpu/decoder/seekable_buffer.cpp index 0d7ec7236a2..70c01ad0e72 100644 --- a/torchvision/csrc/cpu/decoder/seekable_buffer.cpp +++ b/torchvision/csrc/cpu/decoder/seekable_buffer.cpp @@ -96,9 +96,8 @@ void SeekableBuffer::setImageType(ImageType* type) { buffer_[3] == 'G') { *type = ImageType::PNG; } else if ( - buffer_.size() > 1 && - ((buffer_[0] == 0x49 && buffer_[1] == 0x49) || - (buffer_[0] == 0x4D && buffer_[1] == 0x4D))) { + buffer_.size() > 1 && ((buffer_[0] == 0x49 && buffer_[1] == 0x49) || + (buffer_[0] == 0x4D && buffer_[1] == 0x4D))) { *type = ImageType::TIFF; } else { *type = ImageType::UNKNOWN; diff --git a/torchvision/csrc/cpu/decoder/util.cpp b/torchvision/csrc/cpu/decoder/util.cpp index 9ca7246bbaf..fcc6800247c 100644 --- a/torchvision/csrc/cpu/decoder/util.cpp +++ b/torchvision/csrc/cpu/decoder/util.cpp @@ -88,8 +88,8 @@ inline bool serializeItem( size_t len, size_t& pos, const AVSubtitleRect& src) { - auto rectSerialize = - [](uint8_t* d, size_t l, size_t& p, const AVSubtitleRect& x) -> size_t { + auto rectSerialize = []( + uint8_t* d, size_t l, size_t& p, const AVSubtitleRect& x) -> size_t { switch (x.type) { case SUBTITLE_BITMAP: for (int i = 0; i < x.nb_colors; ++i) { @@ -146,8 +146,8 @@ inline bool serializeItem( size_t len, size_t& pos, const AVSubtitle& src) { - auto rectSerialize = - [](uint8_t* d, size_t l, size_t& p, const AVSubtitle& x) -> bool { + auto rectSerialize = []( + uint8_t* d, size_t l, size_t& p, const AVSubtitle& x) -> bool { bool res = serializeItem(d, l, p, x.num_rects); for (unsigned i = 0; res && i < x.num_rects; ++i) { res = serializeItem(d, l, p, *(x.rects[i])); @@ -167,8 +167,8 @@ inline bool deserializeItem( size_t len, size_t& pos, AVSubtitleRect& dest) { - auto rectDeserialize = - [](const uint8_t* y, size_t l, size_t& p, AVSubtitleRect& x) -> bool { + auto rectDeserialize = []( + const uint8_t* y, size_t l, size_t& p, AVSubtitleRect& x) -> bool { switch (x.type) { case SUBTITLE_BITMAP: for (int i = 0; i < x.nb_colors; ++i) { @@ -231,8 +231,8 @@ inline bool deserializeItem( size_t len, size_t& pos, AVSubtitle& dest) { - auto rectDeserialize = - [](const uint8_t* y, size_t l, size_t& p, AVSubtitle& x) -> bool { + auto rectDeserialize = []( + const uint8_t* y, size_t l, size_t& p, AVSubtitle& x) -> bool { bool res = deserializeItem(y, l, p, x.num_rects); if (res && x.num_rects) { x.rects = @@ -284,6 +284,7 @@ size_t size(const AVSubtitle& sub) { } bool validateVideoFormat(const VideoFormat& f) { + // clang-format off /* Valid parameters values for decoder ____________________________________________________________________________________ @@ -307,10 +308,13 @@ bool validateVideoFormat(const VideoFormat& f) { |_____|_____|______________|______________|___________|____________________________| */ + // clang-format on return (f.width == 0 && // #1, #6, #7 and #8 - f.height == 0 && f.cropImage == 0) || + f.height == 0 && + f.cropImage == 0) || (f.width != 0 && // #4 and #5 - f.height != 0 && f.minDimension == 0 && f.maxDimension == 0) || + f.height != 0 && + f.minDimension == 0 && f.maxDimension == 0) || (((f.width != 0 && // #2 f.height == 0) || (f.width == 0 && // #3 @@ -346,8 +350,7 @@ void setFormatDimensions( destW = minDimension; destH = round(double(srcH * minDimension) / srcW); } - } - else if (minDimension == 0 && maxDimension > 0) { // #7 + } else if (minDimension == 0 && maxDimension > 0) { // #7 if (srcW > srcH) { // landscape destW = maxDimension; @@ -357,8 +360,7 @@ void setFormatDimensions( destH = maxDimension; destW = round(double(srcW * maxDimension) / srcH); } - } - else if (minDimension > 0 && maxDimension > 0) { // #8 + } else if (minDimension > 0 && maxDimension > 0) { // #8 if (srcW > srcH) { // landscape destW = maxDimension; @@ -368,8 +370,7 @@ void setFormatDimensions( destW = minDimension; destH = maxDimension; } - } - else { // #1 + } else { // #1 destW = srcW; destH = srcH; } diff --git a/torchvision/csrc/cpu/decoder/util_test.cpp b/torchvision/csrc/cpu/decoder/util_test.cpp index 80a316af5c9..78de08b7139 100644 --- a/torchvision/csrc/cpu/decoder/util_test.cpp +++ b/torchvision/csrc/cpu/decoder/util_test.cpp @@ -4,30 +4,32 @@ #include "util.h" TEST(Util, TestSetFormatDimensions) { - const size_t test_cases[][9] = { - // (userW, userH, srcW, srcH, minDimension, maxDimension, cropImage, destW, destH) - {0, 0, 172, 128, 0, 0, 0, 172, 128}, // #1 - {86, 0, 172, 128, 0, 0, 0, 86, 64}, // #2 - {64, 0, 128, 172, 0, 0, 0, 64, 86}, // #2 - {0, 32, 172, 128, 0, 0, 0, 43, 32}, // #3 - {32, 0, 128, 172, 0, 0, 0, 32, 43}, // #3 - {60, 50, 172, 128, 0, 0, 0, 60, 50}, // #4 - {50, 60, 128, 172, 0, 0, 0, 50, 60}, // #4 - {86, 40, 172, 128, 0, 0, 1, 86, 64}, // #5 - {86, 92, 172, 128, 0, 0, 1, 124, 92}, // #5 - {0, 0, 172, 128, 256, 0, 0, 344, 256}, // #6 - {0, 0, 128, 172, 256, 0, 0, 256, 344}, // #6 - {0, 0, 128, 172, 0, 344, 0, 256, 344}, // #7 - {0, 0, 172, 128, 0, 344, 0, 344, 256}, // #7 - {0, 0, 172, 128, 100, 344, 0, 344, 100},// #8 - {0, 0, 128, 172, 100, 344, 0, 100, 344} // #8 - }; + // clang-format off + const size_t test_cases[][9] = { + // (userW, userH, srcW, srcH, minDimension, maxDimension, cropImage, destW, destH) + {0, 0, 172, 128, 0, 0, 0, 172, 128}, // #1 + {86, 0, 172, 128, 0, 0, 0, 86, 64}, // #2 + {64, 0, 128, 172, 0, 0, 0, 64, 86}, // #2 + {0, 32, 172, 128, 0, 0, 0, 43, 32}, // #3 + {32, 0, 128, 172, 0, 0, 0, 32, 43}, // #3 + {60, 50, 172, 128, 0, 0, 0, 60, 50}, // #4 + {50, 60, 128, 172, 0, 0, 0, 50, 60}, // #4 + {86, 40, 172, 128, 0, 0, 1, 86, 64}, // #5 + {86, 92, 172, 128, 0, 0, 1, 124, 92}, // #5 + {0, 0, 172, 128, 256, 0, 0, 344, 256}, // #6 + {0, 0, 128, 172, 256, 0, 0, 256, 344}, // #6 + {0, 0, 128, 172, 0, 344, 0, 256, 344}, // #7 + {0, 0, 172, 128, 0, 344, 0, 344, 256}, // #7 + {0, 0, 172, 128, 100, 344, 0, 344, 100},// #8 + {0, 0, 128, 172, 100, 344, 0, 100, 344} // #8 + }; + // clang-format onn - for (const auto& tc : test_cases) { - size_t destW = 0; - size_t destH = 0; - ffmpeg::Util::setFormatDimensions(destW, destH, tc[0], tc[1], tc[2], tc[3], tc[4], tc[5], tc[6]); - CHECK(destW == tc[7]); - CHECK(destH == tc[8]); - } + for (const auto& tc : test_cases) { + size_t destW = 0; + size_t destH = 0; + ffmpeg::Util::setFormatDimensions(destW, destH, tc[0], tc[1], tc[2], tc[3], tc[4], tc[5], tc[6]); + CHECK(destW == tc[7]); + CHECK(destH == tc[8]); + } } diff --git a/torchvision/csrc/cpu/video_reader/VideoReader.cpp b/torchvision/csrc/cpu/video_reader/VideoReader.cpp index 57801930926..e8328e97ad3 100644 --- a/torchvision/csrc/cpu/video_reader/VideoReader.cpp +++ b/torchvision/csrc/cpu/video_reader/VideoReader.cpp @@ -234,7 +234,7 @@ torch::List readVideo( readAudioStream, // readAudioStream audioSamples, // audioSamples audioChannels // audioChannels - ); + ); SyncDecoder decoder; std::vector audioMessages, videoMessages; @@ -531,7 +531,7 @@ torch::List probeVideo( 1, // readAudioStream 0, // audioSamples 0 // audioChannels - ); + ); SyncDecoder decoder; DecoderInCallback callback = nullptr; From 836921d43ddeb4aa96a71964ec62432dab53d47b Mon Sep 17 00:00:00 2001 From: Francisco Massa Date: Tue, 24 Mar 2020 08:23:13 -0700 Subject: [PATCH 2/2] More fixes --- .../csrc/cpu/decoder/seekable_buffer.cpp | 5 +++-- torchvision/csrc/cpu/decoder/util.cpp | 22 +++++++++---------- .../csrc/cpu/video_reader/VideoReader.cpp | 4 ++-- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/torchvision/csrc/cpu/decoder/seekable_buffer.cpp b/torchvision/csrc/cpu/decoder/seekable_buffer.cpp index 70c01ad0e72..0d7ec7236a2 100644 --- a/torchvision/csrc/cpu/decoder/seekable_buffer.cpp +++ b/torchvision/csrc/cpu/decoder/seekable_buffer.cpp @@ -96,8 +96,9 @@ void SeekableBuffer::setImageType(ImageType* type) { buffer_[3] == 'G') { *type = ImageType::PNG; } else if ( - buffer_.size() > 1 && ((buffer_[0] == 0x49 && buffer_[1] == 0x49) || - (buffer_[0] == 0x4D && buffer_[1] == 0x4D))) { + buffer_.size() > 1 && + ((buffer_[0] == 0x49 && buffer_[1] == 0x49) || + (buffer_[0] == 0x4D && buffer_[1] == 0x4D))) { *type = ImageType::TIFF; } else { *type = ImageType::UNKNOWN; diff --git a/torchvision/csrc/cpu/decoder/util.cpp b/torchvision/csrc/cpu/decoder/util.cpp index fcc6800247c..0dbcf885cf5 100644 --- a/torchvision/csrc/cpu/decoder/util.cpp +++ b/torchvision/csrc/cpu/decoder/util.cpp @@ -88,8 +88,8 @@ inline bool serializeItem( size_t len, size_t& pos, const AVSubtitleRect& src) { - auto rectSerialize = []( - uint8_t* d, size_t l, size_t& p, const AVSubtitleRect& x) -> size_t { + auto rectSerialize = + [](uint8_t* d, size_t l, size_t& p, const AVSubtitleRect& x) -> size_t { switch (x.type) { case SUBTITLE_BITMAP: for (int i = 0; i < x.nb_colors; ++i) { @@ -146,8 +146,8 @@ inline bool serializeItem( size_t len, size_t& pos, const AVSubtitle& src) { - auto rectSerialize = []( - uint8_t* d, size_t l, size_t& p, const AVSubtitle& x) -> bool { + auto rectSerialize = + [](uint8_t* d, size_t l, size_t& p, const AVSubtitle& x) -> bool { bool res = serializeItem(d, l, p, x.num_rects); for (unsigned i = 0; res && i < x.num_rects; ++i) { res = serializeItem(d, l, p, *(x.rects[i])); @@ -167,8 +167,8 @@ inline bool deserializeItem( size_t len, size_t& pos, AVSubtitleRect& dest) { - auto rectDeserialize = []( - const uint8_t* y, size_t l, size_t& p, AVSubtitleRect& x) -> bool { + auto rectDeserialize = + [](const uint8_t* y, size_t l, size_t& p, AVSubtitleRect& x) -> bool { switch (x.type) { case SUBTITLE_BITMAP: for (int i = 0; i < x.nb_colors; ++i) { @@ -231,8 +231,8 @@ inline bool deserializeItem( size_t len, size_t& pos, AVSubtitle& dest) { - auto rectDeserialize = []( - const uint8_t* y, size_t l, size_t& p, AVSubtitle& x) -> bool { + auto rectDeserialize = + [](const uint8_t* y, size_t l, size_t& p, AVSubtitle& x) -> bool { bool res = deserializeItem(y, l, p, x.num_rects); if (res && x.num_rects) { x.rects = @@ -310,11 +310,9 @@ bool validateVideoFormat(const VideoFormat& f) { */ // clang-format on return (f.width == 0 && // #1, #6, #7 and #8 - f.height == 0 && - f.cropImage == 0) || + f.height == 0 && f.cropImage == 0) || (f.width != 0 && // #4 and #5 - f.height != 0 && - f.minDimension == 0 && f.maxDimension == 0) || + f.height != 0 && f.minDimension == 0 && f.maxDimension == 0) || (((f.width != 0 && // #2 f.height == 0) || (f.width == 0 && // #3 diff --git a/torchvision/csrc/cpu/video_reader/VideoReader.cpp b/torchvision/csrc/cpu/video_reader/VideoReader.cpp index e8328e97ad3..57801930926 100644 --- a/torchvision/csrc/cpu/video_reader/VideoReader.cpp +++ b/torchvision/csrc/cpu/video_reader/VideoReader.cpp @@ -234,7 +234,7 @@ torch::List readVideo( readAudioStream, // readAudioStream audioSamples, // audioSamples audioChannels // audioChannels - ); + ); SyncDecoder decoder; std::vector audioMessages, videoMessages; @@ -531,7 +531,7 @@ torch::List probeVideo( 1, // readAudioStream 0, // audioSamples 0 // audioChannels - ); + ); SyncDecoder decoder; DecoderInCallback callback = nullptr;