UVCCallBack.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/time.h>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <vector>
  7. #include "../libuvc/libuvc.h"
  8. #include "UVCDeviceManager.h"
  9. #include "../MppDecoder/MppDecoder.h"
  10. #include <opencv4/opencv2/opencv.hpp>
  11. #include <opencv4/opencv2/imgcodecs.hpp>
  12. struct timeval pre_frame_time;
  13. /* This callback function runs once per frame. Use it to perform any
  14. * quick processing you need, or have it put the frame into your application's
  15. * input queue. If this function takes too long, you'll start losing frames. */
  16. void cbSaveToLocal(uvc_frame_t *frame, void *ptr)
  17. {
  18. uvc_error_t ret;
  19. int *pindex = (int *)ptr;
  20. int index = *pindex;
  21. FILE *fp;
  22. static int jpeg_count[3] = {0};
  23. static int jpeg_group[3] = {0};
  24. static const char *H264_FILE = "iOSDevLog.h264";
  25. static const char *MJPEG_FILE = ".jpeg";
  26. uvc_frame_t *bgr;
  27. char filename[264];
  28. unsigned char *rgb_ptr{nullptr};
  29. std::vector<unsigned char> jpeg_buffer;
  30. cv::Mat jpegMat;
  31. cv::Mat img;
  32. std::fstream log_file("CallBackLog.txt", std::ios::app);
  33. auto start_time = std::chrono::high_resolution_clock::now();
  34. auto end_time = std::chrono::high_resolution_clock::now();
  35. bgr = uvc_allocate_frame(frame->width * frame->height * 3);
  36. if (!bgr)
  37. {
  38. printf("unable to allocate bgr frame!\n");
  39. return;
  40. }
  41. switch (frame->frame_format)
  42. {
  43. case UVC_FRAME_FORMAT_H264:
  44. /* use `ffplay H264_FILE` to play */
  45. fp = fopen(H264_FILE, "a");
  46. fwrite(frame->data, 1, frame->data_bytes, fp);
  47. fclose(fp);
  48. break;
  49. case UVC_COLOR_FORMAT_MJPEG:
  50. if (!g_gpioExplorer->getFailingStatus(index))
  51. {
  52. std::cout << "Pass this wave" << std::endl;
  53. break;
  54. }
  55. g_gpioExplorer->resetFailingStatus(index);
  56. if (jpeg_count[index] / 12 >= 1)
  57. jpeg_group[index]++;
  58. jpeg_count[index] = jpeg_count[index] % 12;
  59. std::cout << "Current File Device " << index << " Index : " << jpeg_count[index] << std::endl;
  60. sprintf(filename, "./data/%d/%d-%d%s", index, jpeg_group[index], jpeg_count[index]++, MJPEG_FILE);
  61. int frame_width, frame_height;
  62. // jpeg_buffer = std::vector<uint8_t>((unsigned char*)frame->data, (unsigned char*)frame->data + frame->data_bytes);
  63. // record decode time spend
  64. start_time = std::chrono::high_resolution_clock::now();
  65. // uvc_any2rgb(frame, bgr);
  66. std::cout << "Decode frame byte size" << frame->data_bytes << std::endl;
  67. MppDecoder::getInstance().decodeJpegToRgb((const char *)frame->data, frame->data_bytes, &rgb_ptr, &frame_width, &frame_height);
  68. RgaColorTransfer::getInstance()->setSrc(rgb_ptr, frame_width, frame_height, IM_YUV_TO_RGB_BT709_LIMIT);
  69. RgaColorTransfer::getInstance()->setDst(resize_ptr, resize_width, resize_height, IM_RGB_TO_RGB_BT709_LIMIT);
  70. end_time = std::chrono::high_resolution_clock::now();
  71. std::cout
  72. << index << " Decode time: " << std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count() << " milliseconds" << std::endl;
  73. break;
  74. case UVC_COLOR_FORMAT_YUYV:
  75. /* Do the BGR conversion */
  76. // ret = uvc_any2bgr(frame, bgr);
  77. // if (ret)
  78. // {
  79. // uvc_perror(ret, "uvc_any2bgr");
  80. // uvc_free_frame(bgr);
  81. // return;
  82. // }
  83. break;
  84. default:
  85. break;
  86. }
  87. if (frame->sequence % 30 == 0)
  88. {
  89. printf(" * got image %u\n", frame->sequence);
  90. }
  91. uvc_free_frame(bgr);
  92. }
  93. // call back function
  94. void cbDoNothing(uvc_frame_t *frame, void *ptr)
  95. {
  96. printf("callback! frame_format = %d, width = %d, height = %d, length = %lu, ptr = %p\n",
  97. frame->frame_format, frame->width, frame->height, frame->data_bytes, ptr);
  98. }