123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/time.h>
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include "../libuvc/libuvc.h"
- #include "UVCDeviceManager.h"
- #include "../MppDecoder/MppDecoder.h"
- #include <opencv4/opencv2/opencv.hpp>
- #include <opencv4/opencv2/imgcodecs.hpp>
- struct timeval pre_frame_time;
- /* This callback function runs once per frame. Use it to perform any
- * quick processing you need, or have it put the frame into your application's
- * input queue. If this function takes too long, you'll start losing frames. */
- void cbSaveToLocal(uvc_frame_t *frame, void *ptr)
- {
- uvc_error_t ret;
- int *pindex = (int *)ptr;
- int index = *pindex;
- FILE *fp;
- static int jpeg_count[3] = {0};
- static int jpeg_group[3] = {0};
- static const char *H264_FILE = "iOSDevLog.h264";
- static const char *MJPEG_FILE = ".jpeg";
- uvc_frame_t *bgr;
- char filename[264];
- unsigned char *rgb_ptr{nullptr};
- std::vector<unsigned char> jpeg_buffer;
- cv::Mat jpegMat;
- cv::Mat img;
- std::fstream log_file("CallBackLog.txt", std::ios::app);
- auto start_time = std::chrono::high_resolution_clock::now();
- auto end_time = std::chrono::high_resolution_clock::now();
- bgr = uvc_allocate_frame(frame->width * frame->height * 3);
- if (!bgr)
- {
- printf("unable to allocate bgr frame!\n");
- return;
- }
- switch (frame->frame_format)
- {
- case UVC_FRAME_FORMAT_H264:
- /* use `ffplay H264_FILE` to play */
- fp = fopen(H264_FILE, "a");
- fwrite(frame->data, 1, frame->data_bytes, fp);
- fclose(fp);
- break;
- case UVC_COLOR_FORMAT_MJPEG:
- if (!g_gpioExplorer->getFailingStatus(index))
- {
- std::cout << "Pass this wave" << std::endl;
- break;
- }
- g_gpioExplorer->resetFailingStatus(index);
- if (jpeg_count[index] / 12 >= 1)
- jpeg_group[index]++;
- jpeg_count[index] = jpeg_count[index] % 12;
- std::cout << "Current File Device " << index << " Index : " << jpeg_count[index] << std::endl;
- sprintf(filename, "./data/%d/%d-%d%s", index, jpeg_group[index], jpeg_count[index]++, MJPEG_FILE);
- int frame_width, frame_height;
- // jpeg_buffer = std::vector<uint8_t>((unsigned char*)frame->data, (unsigned char*)frame->data + frame->data_bytes);
- // record decode time spend
- start_time = std::chrono::high_resolution_clock::now();
- // uvc_any2rgb(frame, bgr);
- std::cout << "Decode frame byte size" << frame->data_bytes << std::endl;
- MppDecoder::getInstance().decodeJpegToRgb((const char *)frame->data, frame->data_bytes, &rgb_ptr, &frame_width, &frame_height);
- RgaColorTransfer::getInstance()->setSrc(rgb_ptr, frame_width, frame_height, IM_YUV_TO_RGB_BT709_LIMIT);
- RgaColorTransfer::getInstance()->setDst(resize_ptr, resize_width, resize_height, IM_RGB_TO_RGB_BT709_LIMIT);
- end_time = std::chrono::high_resolution_clock::now();
- std::cout
- << index << " Decode time: " << std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count() << " milliseconds" << std::endl;
- break;
- case UVC_COLOR_FORMAT_YUYV:
- /* Do the BGR conversion */
- // ret = uvc_any2bgr(frame, bgr);
- // if (ret)
- // {
- // uvc_perror(ret, "uvc_any2bgr");
- // uvc_free_frame(bgr);
- // return;
- // }
- break;
- default:
- break;
- }
- if (frame->sequence % 30 == 0)
- {
- printf(" * got image %u\n", frame->sequence);
- }
- uvc_free_frame(bgr);
- }
- // call back function
- void cbDoNothing(uvc_frame_t *frame, void *ptr)
- {
- printf("callback! frame_format = %d, width = %d, height = %d, length = %lu, ptr = %p\n",
- frame->frame_format, frame->width, frame->height, frame->data_bytes, ptr);
- }
|