]> git.sesse.net Git - kdenlive/blob - src/v4l/src.c
9cbc27fbc2d1cf5b055fc73c1995e182e227db7a
[kdenlive] / src / v4l / src.c
1 /* fswebcam - FireStorm.cx's webcam generator                 */
2 /*============================================================*/
3 /* Copyright (C)2005-2010 Philip Heron <phil@sanslogic.co.uk> */
4 /*                                                            */
5 /* This program is distributed under the terms of the GNU     */
6 /* General Public License, version 2. You may use, modify,    */
7 /* and redistribute it under the terms of this license. A     */
8 /* copy should be included with this source.                  */
9
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
13
14 #include <stdlib.h>
15 #include <time.h>
16 #include <string.h>
17 #include <sys/stat.h>
18 #include <errno.h>
19 #include "src.h"
20
21 #ifdef HAVE_V4L2
22 extern src_mod_t src_v4l2;
23 #endif
24 #ifdef HAVE_V4L1
25 extern src_mod_t src_v4l1;
26 #endif
27
28 /* Supported palette types. */
29 src_palette_t src_palette[] = {
30         { "PNG" },
31         { "JPEG" },
32         { "MJPEG" },
33         { "S561" },
34         { "RGB32" },
35         { "BGR32" },
36         { "RGB24" },
37         { "BGR24" },
38         { "YUYV" },
39         { "UYVY" },
40         { "YUV420P" },
41         { "NV12MB" },
42         { "BAYER" },
43         { "SGBRG8" },
44         { "SGRBG8" },
45         { "RGB565" },
46         { "RGB555" },
47         { "Y16" },
48         { "GREY" },
49         { NULL }
50 };
51
52
53 int src_open(src_t *src, char *source)
54 {
55         int i = 0;
56         struct stat st;
57         
58         if(!source)
59         {
60                 fprintf(stderr, "No source was specified.......");
61                 return(-1);
62         }
63         src->source = source;
64         
65         i = 0;
66         int r = src_v4l2.flags;
67         if(S_ISCHR(st.st_mode) && r & SRC_TYPE_DEVICE) r = -1;
68         else if(!S_ISCHR(st.st_mode) && r & SRC_TYPE_FILE) r = -1;
69         else r = 0;
70         src->type = 0;
71         r = src_v4l2.open(src);
72         if(r == -2) return(-1);
73
74         /*
75         int frame;
76         for(frame = 0; frame < config->skipframes; frame++)
77             if(src_grab(src) == -1) break;*/
78
79         return 0;
80 }
81
82 const char *src_query(src_t *src, char *source, uint *width, uint *height, char **pixelformatdescription)
83 {
84     src->source = source;
85     return src_v4l2.query(src, width, height, pixelformatdescription);
86 }
87
88 int src_close(src_t *src)
89 {
90         int r;
91         
92         if(src->captured_frames)
93         {
94                 double seconds =
95                    (src->tv_last.tv_sec  + src->tv_last.tv_usec  / 1000000.0) -
96                    (src->tv_first.tv_sec + src->tv_first.tv_usec / 1000000.0);
97                 
98                 /* Display FPS if enough frames where captured. */
99                 if(src->captured_frames == 1)
100                 {
101                         /*MSG("Captured frame in %0.2f seconds.", seconds);*/
102                 }
103                 else if(src->captured_frames < 3)
104                 {
105                         /*MSG("Captured %i frames in %0.2f seconds.",
106                             src->captured_frames, seconds);*/
107                 }
108                 else
109                 {
110                         /*MSG("Captured %i frames in %0.2f seconds. (%i fps)",
111                             src->captured_frames, seconds,
112                             (int) (src->captured_frames / seconds));*/
113                 }
114         }
115         
116         r = src_v4l2.close(src);
117         
118         if(src->source) free(src->source);
119         
120         return(r);
121 }
122
123 int src_grab(src_t *src)
124 {
125         int r = src_v4l2.grab(src);
126         
127         if(!r)
128         {
129                 if(!src->captured_frames) gettimeofday(&src->tv_first, NULL);
130                 gettimeofday(&src->tv_last, NULL);
131                 
132                 src->captured_frames++;
133         }
134         
135         return(r);
136 }
137
138 /* Pointers are great things. Terrible things yes, but great. */
139 /* These work but are very ugly and will be re-written soon. */
140
141 int src_set_option(src_option_t ***options, char *name, char *value)
142 {
143         src_option_t **opts, *opt;
144         int count;
145         
146         if(!options) return(-1);
147         if(!*options)
148         {
149                 *options = malloc(sizeof(src_option_t *));
150                 if(!*options)
151                 {
152                         /*ERROR("Out of memory.");*/
153                         return(-1);
154                 }
155                 
156                 *options[0] = NULL;
157         }
158         
159         count = 0;
160         opts = *options;
161         while(*opts)
162         {
163                 if((*opts)->name) if(!strcasecmp(name, (*opts)->name)) break;
164                 opts++;
165                 count++;
166         }
167         
168         if(!*opts)
169         {
170                 void *new;
171                 
172                 opt = (src_option_t *) malloc(sizeof(src_option_t));
173                 if(!opt)
174                 {
175                         /*ERROR("Out of memory.");*/
176                         return(-1);
177                 }
178                 
179                 new = realloc(*options, sizeof(src_option_t *) * (count + 2));
180                 if(!new)
181                 {
182                         free(opt);
183                         /*ERROR("Out of memory.");*/
184                         return(-1);
185                 }
186                 
187                 *options = (src_option_t **) new;
188                 (*options)[count++] = opt;
189                 (*options)[count++] = NULL;
190                 
191                 opt->name = strdup(name);
192                 opt->value = NULL;
193         }
194         else opt = *opts;
195         
196         if(opt->value)
197         {
198                 free(opt->value);
199                 opt->value = NULL;
200         }
201         if(value) opt->value = strdup(value);
202         
203         return(0);
204 }
205
206 int src_get_option_by_number(src_option_t **opt, int number,
207                              char **name, char **value)
208 {
209         int i;
210         
211         if(!opt || !name || !value) return(-1);
212         
213         i = 0;
214         while(*opt)
215         {
216                 if(i == number)
217                 {
218                         *name  = (*opt)->name;
219                         *value = (*opt)->value;
220                         return(0);
221                 }
222                 
223                 i++;
224         }
225         
226         return(-1);
227 }
228
229 int src_get_option_by_name(src_option_t **opt, char *name, char **value)
230 {
231         if(!opt || !name || !value) return(-1);
232         
233         while(*opt)
234         {
235                 if((*opt)->name)
236                 {
237                         if(!strcasecmp(name, (*opt)->name))
238                         {
239                                 *value = (*opt)->value;
240                                 return(0);
241                         }
242                 }
243                 
244                 opt++;
245         }
246         
247         return(-1);
248 }
249
250 int src_free_options(src_option_t ***options)
251 {
252         src_option_t **opts;
253         
254         if(!options || !*options) return(-1);
255         
256         opts = *options;
257         while(*opts)
258         {
259                 if((*opts)->name)  free((*opts)->name);
260                 if((*opts)->value) free((*opts)->value);
261                 
262                 free(*opts);
263                 
264                 opts++;
265         }
266         
267         free(*options);
268         *options = NULL;
269         
270         return(0);
271 }
272