Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
video_capture.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015
3  *
4  * This file is part of Paparazzi.
5  *
6  * Paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * Paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, see
18  * <http://www.gnu.org/licenses/>.
19  *
20  */
21 
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <time.h>
30 
33 
34 #include "lib/encoding/jpeg.h"
35 
36 // Note: this define is set automatically when the video_exif module is included,
37 // and exposes functions to write data in the image exif headers.
38 #if JPEG_WITH_EXIF_HEADER
39 #include "lib/exif/exif_module.h"
40 #endif
41 
42 #ifndef VIDEO_CAPTURE_PATH
43 #define VIDEO_CAPTURE_PATH /data/video/images
44 #endif
45 
46 #ifndef VIDEO_CAPTURE_JPEG_QUALITY
47 #define VIDEO_CAPTURE_JPEG_QUALITY 99
48 #endif
49 
50 #ifndef VIDEO_CAPTURE_FPS
51 #define VIDEO_CAPTURE_FPS 0
52 #endif
54 
55 // Module settings
56 bool video_capture_take_shot = false; // Capture single images
57 bool video_capture_record_video = false; // Capture video
59 
60 // Save directory
61 static char save_dir[256];
62 
63 // Forward function declarations
64 struct image_t *video_capture_func(struct image_t *img, uint8_t camera_id);
65 void video_capture_save(struct image_t *img);
66 
67 
69 {
70  // Create images directory with timestamp
71  struct timeval tv;
72  struct tm *tm;
73  gettimeofday(&tv, NULL);
74  tm = localtime(&tv.tv_sec);
75 
76  sprintf(save_dir, "%s/%04d%02d%02d-%02d%02d%02d", STRINGIFY(VIDEO_CAPTURE_PATH),
77  tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
78  tm->tm_hour, tm->tm_min, tm->tm_sec);
79  // Folder creation delayed until capture starts, see video_capture_save.
80  // This prevents empty folders if nothing is actually recorded.
81 
82  // Add function to computer vision pipeline
83  cv_add_to_device(&VIDEO_CAPTURE_CAMERA, video_capture_func, VIDEO_CAPTURE_FPS, 0);
84 }
85 
86 
87 struct image_t *video_capture_func(struct image_t *img, uint8_t camera_id __attribute__((unused)))
88 {
89  // If take_shot bool is set, save the image
91  video_capture_save(img);
93  }
94 
95  // No modification to image
96  return NULL;
97 }
98 
99 
101 {
102  // Set take_shot bool to true
104 }
105 
108 }
109 
112 }
113 
114 void video_capture_save(struct image_t *img)
115 {
116  // Create output folder if necessary
117  if (access(save_dir, F_OK)) {
118  char save_dir_cmd[266]; // write 10b + [0:256]
119  sprintf(save_dir_cmd, "mkdir -p %s", save_dir);
120  if (system(save_dir_cmd) != 0) {
121  printf("[video_capture] Could not create images directory %s.\n", save_dir);
122  return;
123  }
124  }
125 
126  // Declare storage for image location
127  char save_name[266]; // write 10b + [0-256]
128 
129  // Generate image filename from image timestamp
130  sprintf(save_name, "%s/%u.jpg", save_dir, img->pprz_ts);
131  printf("[video_capture] Saving image to %s.\n", save_name);
132 
133  // Create jpg image from raw frame
134  struct image_t img_jpeg;
135  image_create(&img_jpeg, img->w, img->h, IMAGE_JPEG);
136  jpeg_encode_image(img, &img_jpeg, VIDEO_CAPTURE_JPEG_QUALITY, true);
137 
138 #if JPEG_WITH_EXIF_HEADER
139  write_exif_jpeg(save_name, img_jpeg.buf, img_jpeg.buf_size, img_jpeg.w, img_jpeg.h);
140 #else
141  // Open file
142  FILE *fp = fopen(save_name, "w");
143  if (fp == NULL) {
144  printf("[video_capture] Could not write shot %s.\n", save_name);
145  return;
146  }
147 
148  // Save it to the file and close it
149  fwrite(img_jpeg.buf, sizeof(uint8_t), img_jpeg.buf_size, fp);
150  fclose(fp);
151 #endif
152 
153  // Free image
154  image_free(&img_jpeg);
155 }
struct video_listener * cv_add_to_device(struct video_config_t *device, cv_function func, uint16_t fps, uint8_t id)
Definition: cv.c:46
Computer vision framework for onboard processing.
int write_exif_jpeg(char *filename, const unsigned char *image_jpg, const unsigned int image_jpg_len, const unsigned int image_jpg_x, const unsigned int image_jpg_y)
Definition: exif_module.c:146
Write JPEG images containing EXIF headers with GPS coordinates.
void image_free(struct image_t *img)
Free the image.
Definition: image.c:75
void image_create(struct image_t *img, uint16_t width, uint16_t height, enum image_type type)
Create a new image.
Definition: image.c:43
uint32_t buf_size
The buffer size.
Definition: image.h:53
void * buf
Image buffer (depending on the image_type)
Definition: image.h:54
uint32_t pprz_ts
The timestamp in us since system startup.
Definition: image.h:50
uint16_t h
Image height.
Definition: image.h:47
uint16_t w
Image width.
Definition: image.h:46
@ IMAGE_JPEG
An JPEG encoded image (not per pixel encoded)
Definition: image.h:38
Definition: image.h:44
void jpeg_encode_image(struct image_t *in, struct image_t *out, uint32_t quality_factor, bool add_dri_header)
Encode an YUV422 image.
Definition: jpeg.c:408
Encode images with the use of the JPEG encoding.
#define false
Definition: microrl.h:7
PRINT_CONFIG_VAR(ONELOOP_ANDI_FILT_CUTOFF)
static char save_dir[256]
Definition: video_capture.c:61
bool video_capture_record_video
Definition: video_capture.c:57
bool video_capture_take_shot
Definition: video_capture.c:56
#define VIDEO_CAPTURE_JPEG_QUALITY
Definition: video_capture.c:47
#define VIDEO_CAPTURE_PATH
Definition: video_capture.c:43
struct image_t * video_capture_func(struct image_t *img, uint8_t camera_id)
Definition: video_capture.c:87
#define VIDEO_CAPTURE_FPS
Default FPS (zero means run at camera fps)
Definition: video_capture.c:51
void video_capture_save(struct image_t *img)
void video_capture_start_capture(void)
int video_capture_index
Definition: video_capture.c:58
void video_capture_stop_capture(void)
void video_capture_shoot(void)
void video_capture_init(void)
Definition: video_capture.c:68
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98