Correctly initialize legacy v4l2 reader image size
Fixes v4l2 crash for raspberry pis.
Signed-off-by: milind-u <milind.upadhyay@gmail.com>
Change-Id: I6aaee295af7dadda01ad5470a8af596473425749
diff --git a/frc971/vision/v4l2_reader.cc b/frc971/vision/v4l2_reader.cc
index e549ae5..e30fa34 100644
--- a/frc971/vision/v4l2_reader.cc
+++ b/frc971/vision/v4l2_reader.cc
@@ -285,18 +285,21 @@
memset(&format, 0, sizeof(format));
format.type = multiplanar() ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
: V4L2_BUF_TYPE_VIDEO_CAPTURE;
- format.fmt.pix.width = cols_;
- format.fmt.pix.height = rows_;
+
+ constexpr int kWidth = 640;
+ constexpr int kHeight = 480;
+ format.fmt.pix.width = kWidth;
+ format.fmt.pix.height = kHeight;
format.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
// This means we want to capture from a progressive (non-interlaced)
// source.
format.fmt.pix.field = V4L2_FIELD_NONE;
PCHECK(Ioctl(VIDIOC_S_FMT, &format) == 0);
- CHECK_EQ(static_cast<int>(format.fmt.pix.width), cols_);
- CHECK_EQ(static_cast<int>(format.fmt.pix.height), rows_);
+ CHECK_EQ(static_cast<int>(format.fmt.pix.width), kWidth);
+ CHECK_EQ(static_cast<int>(format.fmt.pix.height), kHeight);
CHECK_EQ(static_cast<int>(format.fmt.pix.bytesperline),
- cols_ * 2 /* bytes per pixel */);
- CHECK_EQ(format.fmt.pix.sizeimage, ImageSize());
+ kWidth * 2 /* bytes per pixel */);
+ CHECK_EQ(format.fmt.pix.sizeimage, ImageSize(kHeight, kWidth));
StreamOn();
}
diff --git a/frc971/vision/v4l2_reader.h b/frc971/vision/v4l2_reader.h
index 334ad81..85ca475 100644
--- a/frc971/vision/v4l2_reader.h
+++ b/frc971/vision/v4l2_reader.h
@@ -61,7 +61,10 @@
// TODO(Brian): This concept won't exist once we start using variable-size
// H.264 frames.
- size_t ImageSize() const { return rows_ * cols_ * 2 /* bytes per pixel */; }
+ size_t ImageSize() const { return ImageSize(rows_, cols_); }
+ static size_t ImageSize(int rows, int cols) {
+ return rows * cols * 2 /* bytes per pixel */;
+ }
const aos::ScopedFD &fd() { return fd_; };
@@ -132,10 +135,6 @@
class V4L2Reader : public V4L2ReaderBase {
public:
V4L2Reader(aos::EventLoop *event_loop, const std::string &device_name);
-
- private:
- const int rows_ = 480;
- const int cols_ = 640;
};
// Rockpi specific v4l2 reader. This assumes that the media device has been