Adding image_types.h and supporting reader code.

Change-Id: Ia0fb1a19ce9e992d2a9337ffbfaf8440f11d4e38
diff --git a/aos/vision/events/BUILD b/aos/vision/events/BUILD
index 3a11256..609a864 100644
--- a/aos/vision/events/BUILD
+++ b/aos/vision/events/BUILD
@@ -1,3 +1,5 @@
+package(default_visibility = ["//visibility:public"])
+
 cc_library(
   name = 'epoll_events',
   srcs = ['epoll_events.cc'],
diff --git a/aos/vision/image/BUILD b/aos/vision/image/BUILD
new file mode 100644
index 0000000..662addc
--- /dev/null
+++ b/aos/vision/image/BUILD
@@ -0,0 +1,39 @@
+package(default_visibility = ['//visibility:public'])
+
+cc_library(
+  name = 'image_types',
+  hdrs = ['image_types.h'],
+  deps = [
+    '//aos/common/logging:logging',
+  ],
+)
+
+cc_library(
+  name = 'reader',
+  srcs = ['reader.cc'],
+  hdrs = ['V4L2.h', 'reader.h'],
+  deps = [
+    '//aos/common:time',
+    '//aos/common/logging:logging',
+    ':image_types',
+  ],
+)
+
+cc_library(
+  name = 'jpeg_routines',
+  srcs = ['jpeg_routines.cc'],
+  hdrs = ['jpeg_routines.h'],
+  deps = [
+    '//third_party/libjpeg',
+    '//aos/common/logging:logging',
+    ':image_types'
+  ],
+)
+
+cc_library(name = 'image_stream',
+  hdrs = ['image_stream.h'],
+  deps = [
+    '//aos/vision/events:epoll_events',
+    '//aos/vision/image:reader'
+  ]
+)
diff --git a/aos/vision/image/V4L2.h b/aos/vision/image/V4L2.h
new file mode 100644
index 0000000..58e5161
--- /dev/null
+++ b/aos/vision/image/V4L2.h
@@ -0,0 +1,24 @@
+#ifndef AOS_LINUX_CODE_CAMREA_V4L2_H_
+#define AOS_LINUX_CODE_CAMREA_V4L2_H_
+
+// This file handles including everything needed to use V4L2 and has some
+// utility functions.
+
+#include <sys/ioctl.h>
+
+#include <asm/types.h> /* for videodev2.h */
+#include <linux/videodev2.h>
+
+namespace camera {
+
+static inline int xioctl(int fd, int request, void *arg) {
+  int r;
+  do {
+    r = ioctl(fd, request, reinterpret_cast<uintptr_t>(arg));
+  } while (r == -1 && errno == EINTR);
+  return r;
+}
+
+}  // namespace camera
+
+#endif
diff --git a/aos/vision/image/image_stream.h b/aos/vision/image/image_stream.h
new file mode 100644
index 0000000..ca2d8be
--- /dev/null
+++ b/aos/vision/image/image_stream.h
@@ -0,0 +1,50 @@
+#ifndef _AOS_VISION_IMAGE_IMAGE_STREAM_H_
+#define _AOS_VISION_IMAGE_IMAGE_STREAM_H_
+
+#include "aos/vision/events/epoll_events.h"
+#include "aos/vision/image/reader.h"
+
+#include <memory>
+
+namespace aos {
+namespace vision {
+
+class ImageStreamEvent : public ::aos::events::EpollEvent {
+ public:
+  static std::unique_ptr<::camera::Reader> GetCamera(
+      const std::string &fname, ImageStreamEvent *obj,
+      camera::CameraParams params) {
+    using namespace std::placeholders;
+    std::unique_ptr<::camera::Reader> camread(new ::camera::Reader(
+        fname,
+        std::bind(&ImageStreamEvent::ProcessHelper, obj, _1, _2), params);
+    camread->StartAsync();
+    return camread;
+  }
+
+  explicit ImageStreamEvent(std::unique_ptr<::camera::Reader> reader)
+      : ::aos::events::EpollEvent(reader->fd()), reader_(reader) {}
+
+  explicit ImageStreamEvent(const std::string &fname,
+                            camera::CameraParams params)
+      : ImageStreamEvent(GetCamera(fname, this, params)) {}
+
+  void ProcessHelper(DataRef data, uint64_t timestamp) {
+    if (data.size() < 300) {
+      LOG(INFO, "got bad img: %d of size(%lu)\n", (int)timestamp, data.size());
+      return;
+    }
+    ProcessImage(data, timestamp);
+  }
+  virtual void ProcessImage(DataRef data, uint64_t timestamp) = 0;
+
+  void ReadEvent(Context /*ctx*/) override { reader_->HandleFrame(); }
+
+ private:
+  std::unique_ptr<::camera::Reader> reader_;
+};
+
+}  // namespace vision
+}  // namespace aos
+
+#endif  // _AOS_VISION_DEBUG_IMAGE_STREAM_H_
diff --git a/aos/vision/image/image_types.h b/aos/vision/image/image_types.h
new file mode 100644
index 0000000..2c9ba56
--- /dev/null
+++ b/aos/vision/image/image_types.h
@@ -0,0 +1,104 @@
+#ifndef _AOS_VISION_IMAGE_IMAGE_TYPES_H_
+#define _AOS_VISION_IMAGE_IMAGE_TYPES_H_
+
+#include <stdint.h>
+#include <string.h>
+#include <memory>
+#include <sstream>
+
+#include <experimental/string_view>
+#include "aos/common/logging/logging.h"
+
+namespace aos {
+namespace vision {
+
+// This will go into c++17. No sense writing my own version.
+using DataRef = std::experimental::string_view;
+
+// Represents the dimensions of an image.
+struct ImageFormat {
+  ImageFormat() : w(0), h(0) {}
+  ImageFormat(int nw, int nh) : w(nw), h(nh) {}
+  std::string ToString() {
+    std::ostringstream s;
+    s << "ImageFormat {" << w << ", " << h << "}";
+    return s.str();
+  }
+  int ImgSize() const { return w * h; }
+  bool Equals(const ImageFormat &other) const {
+    return w == other.w && h == other.h;
+  }
+
+  int w;
+  int h;
+};
+
+// Alias for RGB triple. Should be align 1 size 3.
+struct PixelRef {
+  uint8_t r;
+  uint8_t g;
+  uint8_t b;
+};
+
+// Just to be extra safe.
+static_assert(sizeof(PixelRef) == 3, "Problem with cows in fields!");
+static_assert(alignof(PixelRef) == 1, "Problem with cows in fields!");
+
+// ptr version of a ValueArray. Allows operations on buffers owned by other
+// entities. Prefer this for const-ref versions.
+//
+// Templatized because there was a grayscale version, and cairo buffers are
+// only RGBA.
+template <typename ImageType>
+class Array2dPtr {
+ public:
+  Array2dPtr() : Array2dPtr({0, 0}, nullptr) {}
+  Array2dPtr(ImageFormat fmt, ImageType *data) : fmt_(fmt), data_(data) {}
+  ImageType &get_px(int x, int y) const {
+#ifndef NDEBUG
+    if (x < 0 || x >= fmt_.w || y < 0 || y >= fmt_.h) {
+      LOG(FATAL, "%d, %d out of range [%dx %d]\n", x, y, fmt_.w, fmt_.h);
+    }
+#endif  // NBOUNDSCHECK
+    return data_[(x + y * fmt_.w)];
+  }
+  void CopyFrom(const Array2dPtr &other) const {
+    memcpy(data_, other.data_, sizeof(ImageType) * fmt_.ImgSize());
+  }
+
+  const ImageFormat &fmt() const { return fmt_; }
+  ImageType *data() const { return data_; }
+
+ private:
+  ImageFormat fmt_;
+  ImageType *data_;
+};
+
+// unique_ptr version of above.
+template <typename ImageType>
+class ValueArray2d {
+ public:
+  ValueArray2d() : fmt_({0, 0}) {}
+  explicit ValueArray2d(ImageFormat fmt) : fmt_(fmt) {
+    data_.reset(new ImageType[fmt.ImgSize()]);
+  }
+
+  Array2dPtr<ImageType> get() {
+    return Array2dPtr<ImageType>{fmt_, data_.get()};
+  }
+
+  const ImageFormat &fmt() const { return fmt_; }
+  ImageType *data() { return data_.get(); }
+
+ private:
+  ImageFormat fmt_;
+  std::unique_ptr<ImageType[]> data_;
+};
+
+using ImagePtr = Array2dPtr<PixelRef>;
+using ImageValue = ValueArray2d<PixelRef>;
+
+}  // namespace vision
+}  // namespace aos
+
+#endif  // _AOS_VISION_IMAGE_IMAGE_TYPES_H_
diff --git a/aos/vision/image/jpeg_routines.cc b/aos/vision/image/jpeg_routines.cc
new file mode 100644
index 0000000..e0c93dc
--- /dev/null
+++ b/aos/vision/image/jpeg_routines.cc
@@ -0,0 +1,249 @@
+#include "aos/vision/image/jpeg_routines.h"
+
+#include <errno.h>
+#include <setjmp.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <unistd.h>
+#include <cstring>
+
+#include "aos/common/logging/logging.h"
+#include "third_party/libjpeg/jpeglib.h"
+
+namespace aos {
+namespace vision {
+
+namespace {
+
+void decompress_add_huff_table(j_decompress_ptr cinfo, JHUFF_TBL **htblptr,
+                               const UINT8 *bits, const UINT8 *val);
+
+void standard_huff_tables(j_decompress_ptr cinfo);
+
+// Error handling form libjpeg
+struct JpegErrorManager {
+  /// "public" fields
+  struct jpeg_error_mgr pub;
+  // for return to caller
+  jmp_buf setjmp_buffer;
+};
+
+char JpegLastErrorMsg[JMSG_LENGTH_MAX];
+
+// TODO(parker): Error handling needs to be investigated bettter.
+void JpegErrorExit(j_common_ptr cinfo) {
+  JpegErrorManager myerr;
+  // cinfo->err actually points to a JpegErrorManager struct
+  ::std::memcpy(&myerr, cinfo->err, sizeof(myerr));
+  // JpegErrorManager* myerr = (JpegErrorManager*) cinfo->err;
+  // note : *(cinfo->err) is now equivalent to myerr->pub
+
+  // output_message is a method to print an error message
+  //(* (cinfo->err->output_message) ) (cinfo);
+
+  // Create the message
+  (*(cinfo->err->format_message))(cinfo, JpegLastErrorMsg);
+
+  // Jump to the setjmp point
+  longjmp(myerr.setjmp_buffer, 1);
+}
+
+// This is also adapted from libjpeg to be used on decompression tables rather
+// than compression tables as it was originally intended.
+void decompress_add_huff_table(j_decompress_ptr cinfo, JHUFF_TBL **htblptr,
+                               const UINT8 *bits, const UINT8 *val) {
+  if (*htblptr == NULL) *htblptr = jpeg_alloc_huff_table((j_common_ptr)cinfo);
+
+  // Copy the number-of-symbols-of-each-code-length counts.
+  memcpy((*htblptr)->bits, bits, sizeof((*htblptr)->bits));
+
+  // Validate the counts.  We do this here mainly so we can copy the right
+  // number of symbols from the val[] array, without risking marching off
+  // the end of memory.  jchuff.c will do a more thorough test later.
+  int nsymbols = 0;
+  for (int len = 1; len <= 16; len++) nsymbols += bits[len];
+  if (nsymbols < 1 || nsymbols > 256) {
+    LOG(FATAL, "%s:%d: Error, bad huffman table", __FILE__, __LINE__);
+  }
+
+  memcpy((*htblptr)->huffval, val, nsymbols * sizeof(uint8_t));
+}
+
+// standard_huff_tables is taken from libjpeg compression stuff
+// and is here used to set up the same tables in the decompression structure.
+// Set up the standard Huffman tables (cf. JPEG standard section K.3)
+// IMPORTANT: these are only valid for 8-bit data precision!
+void standard_huff_tables(j_decompress_ptr cinfo) {
+  /* 0-base on first 0, */
+  static const UINT8 bits_dc_luminance[17] = {0, 0, 1, 5, 1, 1, 1, 1, 1,
+                                              1, 0, 0, 0, 0, 0, 0, 0};
+  static const UINT8 val_dc_luminance[] = {0, 1, 2, 3, 4,  5,
+                                           6, 7, 8, 9, 10, 11};
+
+  /* 0-base on first 0 */
+  static const UINT8 bits_dc_chrominance[17] = {0, 0, 3, 1, 1, 1, 1, 1, 1,
+                                                1, 1, 1, 0, 0, 0, 0, 0};
+  static const UINT8 val_dc_chrominance[] = {0, 1, 2, 3, 4,  5,
+                                             6, 7, 8, 9, 10, 11};
+
+  /* 0-base on first 0 */
+  static const UINT8 bits_ac_luminance[17] = {0, 0, 2, 1, 3, 3, 2, 4,   3,
+                                              5, 5, 4, 4, 0, 0, 1, 0x7d};
+  static const UINT8 val_ac_luminance[] = {
+      0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06,
+      0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
+      0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62, 0x72,
+      0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
+      0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45,
+      0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
+      0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75,
+      0x76, 0x77, 0x78, 0x79, 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
+      0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3,
+      0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
+      0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9,
+      0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
+      0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3, 0xf4,
+      0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa};
+
+  /* 0-base on first 0 */
+  static const UINT8 bits_ac_chrominance[17] = {0, 0, 2, 1, 2, 4, 4, 3,   4,
+                                                7, 5, 4, 4, 0, 1, 2, 0x77};
+  static const UINT8 val_ac_chrominance[] = {
+      0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41,
+      0x51, 0x07, 0x61, 0x71, 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
+      0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1,
+      0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
+      0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44,
+      0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
+      0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74,
+      0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
+      0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a,
+      0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
+      0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
+      0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
+      0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4,
+      0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa};
+
+  decompress_add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[0],
+                            bits_dc_luminance, val_dc_luminance);
+  decompress_add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[0],
+                            bits_ac_luminance, val_ac_luminance);
+  decompress_add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[1],
+                            bits_dc_chrominance, val_dc_chrominance);
+  decompress_add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[1],
+                            bits_ac_chrominance, val_ac_chrominance);
+}
+
+void local_emit_message(jpeg_common_struct * /*cinfo*/, int /*msg_level*/) {
+  return;
+}
+
+}  // namespace
+
+ImageFormat GetFmt(DataRef data) {
+  ImageFormat fmt;
+  struct jpeg_decompress_struct cinfo;
+  struct jpeg_error_mgr jerr;
+
+  cinfo.err = jpeg_std_error(&jerr);
+  jerr.emit_message = local_emit_message;
+
+  cinfo.out_color_space = JCS_RGB;
+  jpeg_create_decompress(&cinfo);
+
+  jpeg_mem_src(&cinfo, reinterpret_cast<unsigned char *>(
+                           const_cast<char *>(data.data())),
+               data.size());
+
+  jpeg_read_header(&cinfo, TRUE);
+  fmt.w = cinfo.image_width;
+  fmt.h = cinfo.image_height;
+  jpeg_destroy_decompress(&cinfo);
+  return fmt;
+}
+
+// Returns true if successful false if an error was encountered.
+bool ProcessJpeg(DataRef data, PixelRef *out) {
+  /*
+  TODO(parker): sort of error handling.
+  struct jpeg_decompress_struct cinfo;
+  struct jpeg_error_mgr jerr;
+
+  cinfo.err = jpeg_std_error( &jerr );
+  jerr.emit_message   = local_emit_message;
+
+  cinfo.out_color_space = JCS_RGB;
+  jpeg_create_decompress( &cinfo );
+  */
+
+  static bool lost_camera_connect = false;
+  struct jpeg_decompress_struct cinfo;
+
+  // We set up the normal JPEG error routines, then override error_exit.
+  JpegErrorManager jerr;
+  cinfo.err = jpeg_std_error(&jerr.pub);
+  jerr.pub.emit_message = local_emit_message;
+  jerr.pub.error_exit = JpegErrorExit;
+  // Establish the setjmp return context for my_error_exit to use.
+  if (setjmp(jerr.setjmp_buffer)) {
+    // If we get here, the JPEG code has signaled an error.
+    if (!lost_camera_connect) {
+      printf(
+          "Lost camera connection in process_jpeg.\nLooking for reconnect "
+          "...\n");
+      fflush(stdout);
+      lost_camera_connect = true;
+    }
+    jpeg_destroy_decompress(&cinfo);
+    return false;
+  }
+
+  cinfo.out_color_space = JCS_RGB;
+  jpeg_create_decompress(&cinfo);
+
+  jpeg_mem_src(&cinfo, reinterpret_cast<unsigned char *>(
+                           const_cast<char *>(data.data())),
+               data.size());
+
+  jpeg_read_header(&cinfo, TRUE);
+  standard_huff_tables(&cinfo);
+
+  /*printf( "JPEG File Information: \n" );
+  printf( "Image width and height: %d pixels and %d pixels.\n",
+  cinfo.image_width, cinfo.image_height );
+  printf( "Color components per pixel: %d.\n", cinfo.num_components );
+  printf( "Color space: %d.\n", cinfo.jpeg_color_space );
+  printf("JpegDecompressed\n");*/
+
+  jpeg_start_decompress(&cinfo);
+
+  int offset = 0;
+  int step = cinfo.num_components * cinfo.image_width;
+  unsigned char *buffers[cinfo.image_height];
+  for (size_t i = 0; i < cinfo.image_height; ++i) {
+    buffers[i] = reinterpret_cast<unsigned char *>(&out[offset]);
+    offset += step;
+  }
+
+  while (cinfo.output_scanline < cinfo.image_height) {
+    jpeg_read_scanlines(&cinfo, &buffers[cinfo.output_scanline],
+                        cinfo.image_height - cinfo.output_scanline);
+  }
+
+  jpeg_finish_decompress(&cinfo);
+  jpeg_destroy_decompress(&cinfo);
+
+  if (lost_camera_connect) {
+    printf("Camera connection restablished.\n");
+    fflush(stdout);
+    lost_camera_connect = false;
+  }
+
+  return true;
+}
+
+}  // namespace vision
+}  // namespace aos
diff --git a/aos/vision/image/jpeg_routines.h b/aos/vision/image/jpeg_routines.h
new file mode 100644
index 0000000..7c8fd67
--- /dev/null
+++ b/aos/vision/image/jpeg_routines.h
@@ -0,0 +1,36 @@
+#ifndef _AOS_VISION_IMAGE_JPEGROUTINES_H_
+#define _AOS_VISION_IMAGE_JPEGROUTINES_H_
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include "aos/vision/image/image_types.h"
+
+namespace aos {
+namespace vision {
+
+// Returns true if successful false if an error was encountered.
+// Will decompress data into out. Out must be of the right size
+// as determined below.
+bool ProcessJpeg(DataRef data, PixelRef *out);
+
+// Gets the format for the particular jpeg.
+ImageFormat GetFmt(DataRef data);
+
+// Decodes jpeg from data. Will resize if necessary.
+// (Should not be necessary in most normal cases).
+//
+// Consider this the canonical way to decode jpegs if no other
+// choice is given.
+inline bool DecodeJpeg(DataRef data, ImageValue *value) {
+  auto fmt = GetFmt(data);
+  if (!value->fmt().Equals(fmt)) {
+    *value = ImageValue(fmt);
+  }
+  return ProcessJpeg(data, value->data());
+}
+
+}  // namespace vision
+}  // namespace aos
+
+#endif  // _AOS_VISION_IMAGE_JPEGROUTINES_H_
diff --git a/aos/vision/image/reader.cc b/aos/vision/image/reader.cc
new file mode 100644
index 0000000..95729da
--- /dev/null
+++ b/aos/vision/image/reader.cc
@@ -0,0 +1,273 @@
+#include "aos/common/time.h"
+
+#include "aos/common/logging/logging.h"
+#include "aos/vision/image/reader.h"
+
+#define CLEAR(x) memset(&(x), 0, sizeof(x))
+
+namespace camera {
+
+struct Reader::Buffer {
+  void *start;
+  size_t length;  // for munmap
+};
+
+Reader::Reader(const std::string &dev_name, ProcessCb process,
+               CameraParams params)
+    : dev_name_(dev_name), process_(std::move(process)), params_(params) {
+  struct stat st;
+  if (stat(dev_name.c_str(), &st) == -1) {
+    PLOG(FATAL, "Cannot identify '%s'", dev_name.c_str());
+  }
+  if (!S_ISCHR(st.st_mode)) {
+    PLOG(FATAL, "%s is no device\n", dev_name.c_str());
+  }
+
+  fd_ = open(dev_name.c_str(), O_RDWR /* required */ | O_NONBLOCK, 0);
+  if (fd_ == -1) {
+    PLOG(FATAL, "Cannot open '%s'", dev_name.c_str());
+  }
+
+  Init();
+}
+
+void Reader::QueueBuffer(v4l2_buffer *buf) {
+  if (xioctl(fd_, VIDIOC_QBUF, buf) == -1) {
+    PLOG(WARNING,
+         "ioctl VIDIOC_QBUF(%d, %p)."
+         " losing buf #%" PRIu32 "\n",
+         fd_, &buf, buf->index);
+  } else {
+    //    LOG(DEBUG, "put buf #%" PRIu32 " into driver's queue\n", buf->index);
+    ++queued_;
+  }
+}
+
+void Reader::HandleFrame() {
+  v4l2_buffer buf;
+  CLEAR(buf);
+  buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+  buf.memory = V4L2_MEMORY_MMAP;
+
+  if (xioctl(fd_, VIDIOC_DQBUF, &buf) == -1) {
+    if (errno != EAGAIN) {
+      PLOG(ERROR, "ioctl VIDIOC_DQBUF(%d, %p)", fd_, &buf);
+    }
+    return;
+  }
+  --queued_;
+
+  // Get a timestamp now as proxy for when the image was taken
+  // TODO(ben): the image should come with a timestamp, parker
+  // will know how to get it.
+  auto time = aos::monotonic_clock::now();
+
+  process_(aos::vision::DataRef(
+               reinterpret_cast<const char *>(buffers_[buf.index].start),
+               buf.bytesused),
+           time);
+  QueueBuffer(&buf);
+}
+
+void Reader::MMapBuffers() {
+  buffers_ = new Buffer[kNumBuffers];
+  v4l2_buffer buf;
+  for (unsigned int n = 0; n < kNumBuffers; ++n) {
+    memset(&buf, 0, sizeof(buf));
+    buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+    buf.memory = V4L2_MEMORY_MMAP;
+    buf.index = n;
+    if (xioctl(fd_, VIDIOC_QUERYBUF, &buf) == -1) {
+      PLOG(FATAL, "ioctl VIDIOC_QUERYBUF(%d, %p)", fd_, &buf);
+    }
+    buffers_[n].length = buf.length;
+    buffers_[n].start = mmap(NULL, buf.length, PROT_READ | PROT_WRITE,
+                             MAP_SHARED, fd_, buf.m.offset);
+    if (buffers_[n].start == MAP_FAILED) {
+      PLOG(FATAL,
+           "mmap(NULL, %zd, PROT_READ | PROT_WRITE, MAP_SHARED, %d, %jd)",
+           (size_t)buf.length, fd_, static_cast<intmax_t>(buf.m.offset));
+    }
+  }
+}
+
+void Reader::InitMMap() {
+  v4l2_requestbuffers req;
+  CLEAR(req);
+  req.count = kNumBuffers;
+  req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+  req.memory = V4L2_MEMORY_MMAP;
+  if (xioctl(fd_, VIDIOC_REQBUFS, &req) == -1) {
+    if (EINVAL == errno) {
+      LOG(FATAL, "%s does not support memory mapping\n", dev_name_.c_str());
+    } else {
+      PLOG(FATAL, "ioctl VIDIOC_REQBUFS(%d, %p)\n", fd_, &req);
+    }
+  }
+  queued_ = kNumBuffers;
+  if (req.count < kNumBuffers) {
+    LOG(FATAL, "Insufficient buffer memory on %s\n", dev_name_.c_str());
+  }
+}
+
+// Sets one of the camera's user-control values.
+// Prints the old and new values.
+// Just prints a message if the camera doesn't support this control or value.
+bool Reader::SetCameraControl(uint32_t id, const char *name, int value) {
+  struct v4l2_control getArg = {id, 0U};
+  int r = xioctl(fd_, VIDIOC_G_CTRL, &getArg);
+  if (r == 0) {
+    if (getArg.value == value) {
+      LOG(DEBUG, "Camera control %s was already %d\n", name, getArg.value);
+      return true;
+    }
+  } else if (errno == EINVAL) {
+    LOG(DEBUG, "Camera control %s is invalid\n", name);
+    errno = 0;
+    return false;
+  }
+
+  struct v4l2_control setArg = {id, value};
+  r = xioctl(fd_, VIDIOC_S_CTRL, &setArg);
+  if (r == 0) {
+    LOG(DEBUG, "Set camera control %s from %d to %d\n", name, getArg.value,
+        value);
+    return true;
+  }
+
+  LOG(DEBUG, "Couldn't set camera control %s to %d", name, value);
+  errno = 0;
+  return false;
+}
+
+void Reader::Init() {
+  v4l2_capability cap;
+  if (xioctl(fd_, VIDIOC_QUERYCAP, &cap) == -1) {
+    if (EINVAL == errno) {
+      LOG(FATAL, "%s is no V4L2 device\n", dev_name_.c_str());
+    } else {
+      PLOG(FATAL, "ioctl VIDIOC_QUERYCAP(%d, %p)", fd_, &cap);
+    }
+  }
+  if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
+    LOG(FATAL, "%s is no video capture device\n", dev_name_.c_str());
+  }
+  if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
+    LOG(FATAL, "%s does not support streaming i/o\n", dev_name_.c_str());
+  }
+
+  /* Select video input, video standard and tune here. */
+
+  v4l2_cropcap cropcap;
+  CLEAR(cropcap);
+  cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+  if (xioctl(fd_, VIDIOC_CROPCAP, &cropcap) == 0) {
+    v4l2_crop crop;
+    crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+    crop.c = cropcap.defrect; /* reset to default */
+
+    if (xioctl(fd_, VIDIOC_S_CROP, &crop) == -1) {
+      switch (errno) {
+        case EINVAL:
+          /* Cropping not supported. */
+          break;
+        default:
+          /* Errors ignored. */
+          PLOG(WARNING, "xioctl VIDIOC_S_CROP");
+          break;
+      }
+    }
+  } else {
+    PLOG(WARNING, "xioctl VIDIOC_CROPCAP");
+  }
+
+  v4l2_format fmt;
+  CLEAR(fmt);
+  fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+  fmt.fmt.pix.width = params_.width;
+  fmt.fmt.pix.height = params_.height;
+  fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
+  fmt.fmt.pix.field = V4L2_FIELD_ANY;
+  // fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
+  // fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
+  if (xioctl(fd_, VIDIOC_S_FMT, &fmt) == -1) {
+    LOG(FATAL, "ioctl VIDIC_S_FMT(%d, %p) failed with %d: %s\n", fd_, &fmt,
+        errno, strerror(errno));
+  }
+  /* Note VIDIOC_S_FMT may change width and height. */
+
+  /* Buggy driver paranoia. */
+  unsigned int min = fmt.fmt.pix.width * 2;
+  if (fmt.fmt.pix.bytesperline < min) fmt.fmt.pix.bytesperline = min;
+  min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
+  if (fmt.fmt.pix.sizeimage < min) fmt.fmt.pix.sizeimage = min;
+
+  if (!SetCameraControl(V4L2_CID_EXPOSURE_AUTO, "V4L2_CID_EXPOSURE_AUTO",
+                        V4L2_EXPOSURE_MANUAL)) {
+    LOG(FATAL, "Failed to set exposure\n");
+  }
+
+  if (!SetCameraControl(V4L2_CID_EXPOSURE_ABSOLUTE,
+                        "V4L2_CID_EXPOSURE_ABSOLUTE", params_.exposure)) {
+    LOG(FATAL, "Failed to set exposure\n");
+  }
+
+  if (!SetCameraControl(V4L2_CID_BRIGHTNESS, "V4L2_CID_BRIGHTNESS",
+                        params_.brightness)) {
+    LOG(FATAL, "Failed to set up camera\n");
+  }
+
+  if (!SetCameraControl(V4L2_CID_GAIN, "V4L2_CID_GAIN", params_.gain)) {
+    LOG(FATAL, "Failed to set up camera\n");
+  }
+
+  // #if 0
+  // set framerate
+  struct v4l2_streamparm *setfps;
+  setfps = (struct v4l2_streamparm *)calloc(1, sizeof(struct v4l2_streamparm));
+  memset(setfps, 0, sizeof(struct v4l2_streamparm));
+  setfps->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+  setfps->parm.capture.timeperframe.numerator = 1;
+  setfps->parm.capture.timeperframe.denominator = params_.fps;
+  if (xioctl(fd_, VIDIOC_S_PARM, setfps) == -1) {
+    PLOG(FATAL, "ioctl VIDIOC_S_PARM(%d, %p)\n", fd_, setfps);
+  }
+  LOG(INFO, "framerate ended up at %d/%d\n",
+      setfps->parm.capture.timeperframe.numerator,
+      setfps->parm.capture.timeperframe.denominator);
+  // #endif
+
+  InitMMap();
+  LOG(INFO, "Bat Vision Successfully Initialized.\n");
+}
+
+aos::vision::ImageFormat Reader::get_format() {
+  struct v4l2_format fmt;
+  fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+  if (xioctl(fd_, VIDIOC_G_FMT, &fmt) == -1) {
+    PLOG(FATAL, "ioctl VIDIC_G_FMT(%d, %p)\n", fd_, &fmt);
+  }
+
+  return aos::vision::ImageFormat{(int)fmt.fmt.pix.width,
+                                  (int)fmt.fmt.pix.height};
+}
+
+void Reader::Start() {
+  LOG(DEBUG, "queueing buffers for the first time\n");
+  v4l2_buffer buf;
+  for (unsigned int i = 0; i < kNumBuffers; ++i) {
+    CLEAR(buf);
+    buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+    buf.memory = V4L2_MEMORY_MMAP;
+    buf.index = i;
+    QueueBuffer(&buf);
+  }
+  LOG(DEBUG, "done with first queue\n");
+
+  v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+  if (xioctl(fd_, VIDIOC_STREAMON, &type) == -1) {
+    PLOG(FATAL, "ioctl VIDIOC_STREAMON(%d, %p)\n", fd_, &type);
+  }
+}
+
+}  // namespace camera
diff --git a/aos/vision/image/reader.h b/aos/vision/image/reader.h
new file mode 100644
index 0000000..eabb80a
--- /dev/null
+++ b/aos/vision/image/reader.h
@@ -0,0 +1,78 @@
+#ifndef AOS_VISION_IMAGE_READER_H_
+#define AOS_VISION_IMAGE_READER_H_
+#include <errno.h>
+#include <fcntl.h>
+#include <malloc.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <inttypes.h>
+#include <functional>
+#include <string>
+
+#include "aos/common/time.h"
+#include "aos/vision/image/V4L2.h"
+#include "aos/vision/image/image_types.h"
+
+namespace camera {
+
+struct CameraParams {
+  int32_t width;
+  int32_t height;
+  int32_t exposure;
+  int32_t brightness;
+  int32_t gain;
+  int32_t fps;
+};
+
+class Reader {
+ public:
+  using ProcessCb = std::function<void(
+      aos::vision::DataRef data, aos::monotonic_clock::time_point timestamp)>;
+  Reader(const std::string &dev_name, ProcessCb process, CameraParams params);
+
+  aos::vision::ImageFormat get_format();
+
+  void HandleFrame();
+  void StartAsync() {
+    Start();
+    MMapBuffers();
+  }
+  int fd() { return fd_; }
+
+ private:
+  void QueueBuffer(v4l2_buffer *buf);
+  void InitMMap();
+  bool SetCameraControl(uint32_t id, const char *name, int value);
+  void Init();
+  void Start();
+  void MMapBuffers();
+  // File descriptor of the camera
+  int fd_;
+  // Name of the camera device.
+  std::string dev_name_;
+
+  ProcessCb process_;
+
+  // The number of buffers currently queued in v4l2.
+  uint32_t queued_;
+  struct Buffer;
+  // TODO(parker): This should be a smart pointer, but it cannot
+  // because the buffers are not ummapped.
+  Buffer *buffers_;
+
+  static const unsigned int kNumBuffers = 10;
+
+  // set only at initialize
+  CameraParams params_;
+};
+
+}  // namespace camera
+
+#endif  // AOS_VISION_IMAGE_READER_H_