]> git.sesse.net Git - kdenlive/blob - src/v4l/src.c
Improve stopmotion widget:
[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         size_t sl;
57         char *s;
58         struct stat st;
59         
60         if(!source)
61         {
62                 fprintf(stderr, "No source was specified.......");
63                 return(-1);
64         }
65         
66         sl = strlen(source) + 1;
67         s = malloc(sl);
68         if(!s)
69         {
70                 fprintf(stderr, "Out of memory.");
71                 return(-1);
72         }
73         
74         src->source = source;
75         
76         i = 0;
77         int r = src_v4l2.flags;
78         if(S_ISCHR(st.st_mode) && r & SRC_TYPE_DEVICE) r = -1;
79         else if(!S_ISCHR(st.st_mode) && r & SRC_TYPE_FILE) r = -1;
80         else r = 0;
81         src->type = 0;
82         r = src_v4l2.open(src);
83         if(r == -2) return(-1);
84
85         /*
86         int frame;
87         for(frame = 0; frame < config->skipframes; frame++)
88             if(src_grab(src) == -1) break;*/
89
90         return 0;
91 }
92
93 int src_close(src_t *src)
94 {
95         int r;
96         
97         if(src->captured_frames)
98         {
99                 double seconds =
100                    (src->tv_last.tv_sec  + src->tv_last.tv_usec  / 1000000.0) -
101                    (src->tv_first.tv_sec + src->tv_first.tv_usec / 1000000.0);
102                 
103                 /* Display FPS if enough frames where captured. */
104                 if(src->captured_frames == 1)
105                 {
106                         /*MSG("Captured frame in %0.2f seconds.", seconds);*/
107                 }
108                 else if(src->captured_frames < 3)
109                 {
110                         /*MSG("Captured %i frames in %0.2f seconds.",
111                             src->captured_frames, seconds);*/
112                 }
113                 else
114                 {
115                         /*MSG("Captured %i frames in %0.2f seconds. (%i fps)",
116                             src->captured_frames, seconds,
117                             (int) (src->captured_frames / seconds));*/
118                 }
119         }
120         
121         r = src_v4l2.close(src);
122         
123         if(src->source) free(src->source);
124         
125         return(r);
126 }
127
128 int src_grab(src_t *src)
129 {
130         int r = src_v4l2.grab(src);
131         
132         if(!r)
133         {
134                 if(!src->captured_frames) gettimeofday(&src->tv_first, NULL);
135                 gettimeofday(&src->tv_last, NULL);
136                 
137                 src->captured_frames++;
138         }
139         
140         return(r);
141 }
142
143 /* Pointers are great things. Terrible things yes, but great. */
144 /* These work but are very ugly and will be re-written soon. */
145
146 int src_set_option(src_option_t ***options, char *name, char *value)
147 {
148         src_option_t **opts, *opt;
149         int count;
150         
151         if(!options) return(-1);
152         if(!*options)
153         {
154                 *options = malloc(sizeof(src_option_t *));
155                 if(!*options)
156                 {
157                         /*ERROR("Out of memory.");*/
158                         return(-1);
159                 }
160                 
161                 *options[0] = NULL;
162         }
163         
164         count = 0;
165         opts = *options;
166         while(*opts)
167         {
168                 if((*opts)->name) if(!strcasecmp(name, (*opts)->name)) break;
169                 opts++;
170                 count++;
171         }
172         
173         if(!*opts)
174         {
175                 void *new;
176                 
177                 opt = (src_option_t *) malloc(sizeof(src_option_t));
178                 if(!opt)
179                 {
180                         /*ERROR("Out of memory.");*/
181                         return(-1);
182                 }
183                 
184                 new = realloc(*options, sizeof(src_option_t *) * (count + 2));
185                 if(!new)
186                 {
187                         free(opt);
188                         /*ERROR("Out of memory.");*/
189                         return(-1);
190                 }
191                 
192                 *options = (src_option_t **) new;
193                 (*options)[count++] = opt;
194                 (*options)[count++] = NULL;
195                 
196                 opt->name = strdup(name);
197                 opt->value = NULL;
198         }
199         else opt = *opts;
200         
201         if(opt->value)
202         {
203                 free(opt->value);
204                 opt->value = NULL;
205         }
206         if(value) opt->value = strdup(value);
207         
208         return(0);
209 }
210
211 int src_get_option_by_number(src_option_t **opt, int number,
212                              char **name, char **value)
213 {
214         int i;
215         
216         if(!opt || !name || !value) return(-1);
217         
218         i = 0;
219         while(*opt)
220         {
221                 if(i == number)
222                 {
223                         *name  = (*opt)->name;
224                         *value = (*opt)->value;
225                         return(0);
226                 }
227                 
228                 i++;
229         }
230         
231         return(-1);
232 }
233
234 int src_get_option_by_name(src_option_t **opt, char *name, char **value)
235 {
236         if(!opt || !name || !value) return(-1);
237         
238         while(*opt)
239         {
240                 if((*opt)->name)
241                 {
242                         if(!strcasecmp(name, (*opt)->name))
243                         {
244                                 *value = (*opt)->value;
245                                 return(0);
246                         }
247                 }
248                 
249                 opt++;
250         }
251         
252         return(-1);
253 }
254
255 int src_free_options(src_option_t ***options)
256 {
257         src_option_t **opts;
258         
259         if(!options || !*options) return(-1);
260         
261         opts = *options;
262         while(*opts)
263         {
264                 if((*opts)->name)  free((*opts)->name);
265                 if((*opts)->value) free((*opts)->value);
266                 
267                 free(*opts);
268                 
269                 opts++;
270         }
271         
272         free(*options);
273         *options = NULL;
274         
275         return(0);
276 }
277