void video_thread_periodic(void)
{
  struct image_t img;
  image_create(&img, 320, 240, IMAGE_YUV422);
  int i, j;
  uint8_t u, v;

#ifdef SMARTUAV_SIMULATOR
  SMARTUAV_IMPORT(&img);
#else
  if (video_thread.is_running) {
    u = 0;
    v = 255;
  } else {
    u = 255;
    v = 0;
  }
  uint8_t *p = (uint8_t *) img.buf;
  for (j = 0; j < img.h; j++) {
    for (i = 0; i < img.w; i += 2) {
      *p++ = u;
      *p++ = j;
      *p++ = v;
      *p++ = j;
    }
  }
  video_thread.is_running = ! video_thread.is_running;
#endif

  cv_run(&img);

  image_free(&img);
}
示例#2
0
/**
 * Handles all the video streaming and saving of the image shots
 * This is a sepereate thread, so it needs to be thread safe!
 */
static void *video_thread_function(void *data)
{
  struct video_config_t *vid = (struct video_config_t *)&(VIDEO_THREAD_CAMERA);

  struct image_t img_jpeg;
  struct image_t img_color;

  // create the images
  if (vid->filters) {
    // fixme: don't hardcode size, works for bebop front camera for now
#define IMG_FLT_SIZE 272
    image_create(&img_color, IMG_FLT_SIZE, IMG_FLT_SIZE, IMAGE_YUV422);
    image_create(&img_jpeg, IMG_FLT_SIZE, IMG_FLT_SIZE, IMAGE_JPEG);
  }
  else {
    image_create(&img_jpeg, vid->w, vid->h, IMAGE_JPEG);
  }

  // Start the streaming of the V4L2 device
  if (!v4l2_start_capture(video_thread.dev)) {
    printf("[video_thread-thread] Could not start capture of %s.\n", video_thread.dev->name);
    return 0;
  }

  // be nice to the more important stuff
  set_nice_level(10);

  // Initialize timing
  struct timespec time_now;
  struct timespec time_prev;
  clock_gettime(CLOCK_MONOTONIC, &time_prev);

  // Start streaming
  video_thread.is_running = true;
  while (video_thread.is_running) {

    // get time in us since last run
    clock_gettime(CLOCK_MONOTONIC, &time_now);
    unsigned int dt_us = sys_time_elapsed_us(&time_prev, &time_now);
    time_prev = time_now;

    // sleep remaining time to limit to specified fps
    uint32_t fps_period_us = (uint32_t)(1000000. / (float)video_thread.fps);
    if (dt_us < fps_period_us) {
      usleep(fps_period_us - dt_us);
    }
    else {
      fprintf(stderr, "video_thread: desired %i fps, only managing %.1f fps\n",
              video_thread.fps, 1000000.f / dt_us);
    }

    // Wait for a new frame (blocking)
    struct image_t img;
    v4l2_image_get(video_thread.dev, &img);

    // pointer to the final image to pass for saving and further processing
    struct image_t *img_final = &img;

    // run selected filters
    if (vid->filters) {
      if (vid->filters & VIDEO_FILTER_DEBAYER) {
        BayerToYUV(&img, &img_color, 0, 0);
      }
      // use color image for further processing
      img_final = &img_color;
    }

    // Check if we need to take a shot
    if (video_thread.take_shot) {
      video_thread_save_shot(img_final, &img_jpeg);
      video_thread.take_shot = false;
    }

    // Run processing if required
    cv_run(img_final);

    // Free the image
    v4l2_image_free(video_thread.dev, &img);
  }

  image_free(&img_jpeg);
  image_free(&img_color);

  return 0;
}