usb_cam.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*********************************************************************
  2. *
  3. * Software License Agreement (BSD License)
  4. *
  5. * Copyright (c) 2014, Robert Bosch LLC.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * * Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer in the documentation and/or other materials provided
  17. * with the distribution.
  18. * * Neither the name of the Robert Bosch nor the names of its
  19. * contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. *********************************************************************/
  36. #ifndef USB_CAM_USB_CAM_H
  37. #define USB_CAM_USB_CAM_H
  38. #include <asm/types.h> /* for videodev2.h */
  39. extern "C"
  40. {
  41. #include <linux/videodev2.h>
  42. #include <libavcodec/avcodec.h>
  43. #include <libswscale/swscale.h>
  44. #include <libavutil/mem.h>
  45. }
  46. // legacy reasons
  47. #include <libavcodec/version.h>
  48. #if LIBAVCODEC_VERSION_MAJOR < 55
  49. #define AV_CODEC_ID_MJPEG CODEC_ID_MJPEG
  50. #endif
  51. #include <string>
  52. #include <sstream>
  53. //#include <sensor_msgs/Image.h>
  54. namespace usb_cam {
  55. class UsbCam {
  56. public:
  57. typedef enum
  58. {
  59. IO_METHOD_READ, IO_METHOD_MMAP, IO_METHOD_USERPTR, IO_METHOD_UNKNOWN,
  60. } io_method;
  61. typedef enum
  62. {
  63. PIXEL_FORMAT_YUYV, PIXEL_FORMAT_UYVY, PIXEL_FORMAT_MJPEG, PIXEL_FORMAT_YUVMONO10, PIXEL_FORMAT_RGB24, PIXEL_FORMAT_GREY, PIXEL_FORMAT_UNKNOWN
  64. } pixel_format;
  65. UsbCam();
  66. ~UsbCam();
  67. // start camera
  68. void start(const std::string& dev, io_method io, pixel_format pf,
  69. int image_width, int image_height, int framerate);
  70. // shutdown camera
  71. void shutdown(void);
  72. // grabs a new image from the camera
  73. // void grab_image(sensor_msgs::Image* image);
  74. void grab_image(char * strdata,int * pnlen,const int nsize);
  75. // enables/disable auto focus
  76. void set_auto_focus(int value);
  77. // Set video device parameters
  78. void set_v4l_parameter(const std::string& param, int value);
  79. void set_v4l_parameter(const std::string& param, const std::string& value);
  80. static io_method io_method_from_string(const std::string& str);
  81. static pixel_format pixel_format_from_string(const std::string& str);
  82. void stop_capturing(void);
  83. void start_capturing(void);
  84. bool is_capturing();
  85. void set_useRawMJPEG(bool bUse);
  86. private:
  87. typedef struct
  88. {
  89. int width;
  90. int height;
  91. int bytes_per_pixel;
  92. int image_size;
  93. char *image;
  94. int is_new;
  95. } camera_image_t;
  96. struct buffer
  97. {
  98. void * start;
  99. size_t length;
  100. };
  101. int init_mjpeg_decoder(int image_width, int image_height);
  102. void mjpeg2rgb(char *MJPEG, int len, char *RGB, int NumPixels);
  103. void process_image(const void * src, int len, camera_image_t *dest);
  104. int read_frame();
  105. void uninit_device(void);
  106. void init_read(unsigned int buffer_size);
  107. void init_mmap(void);
  108. void init_userp(unsigned int buffer_size);
  109. void init_device(int image_width, int image_height, int framerate);
  110. void close_device(void);
  111. void open_device(void);
  112. void grab_image();
  113. bool is_capturing_;
  114. std::string camera_dev_;
  115. unsigned int pixelformat_;
  116. bool monochrome_;
  117. io_method io_;
  118. int fd_;
  119. buffer * buffers_;
  120. unsigned int n_buffers_;
  121. AVFrame *avframe_camera_;
  122. AVFrame *avframe_rgb_;
  123. AVCodec *avcodec_;
  124. AVDictionary *avoptions_;
  125. AVCodecContext *avcodec_context_;
  126. int avframe_camera_size_;
  127. int avframe_rgb_size_;
  128. struct SwsContext *video_sws_;
  129. camera_image_t *image_;
  130. bool mbNotDecodeJpeg = true;
  131. };
  132. }
  133. #endif