]> git.sesse.net Git - kdenlive/blob - src/v4l/v4lcapture.cpp
53ee5cf06ecdf272eeb303d775113ba3b8b6fb6b
[kdenlive] / src / v4l / v4lcapture.cpp
1 /***************************************************************************
2  *   Copyright (C) 2010 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <pthread.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26
27 #include <QDebug>
28 #include <QImage>
29 #include <QTimer>
30 #include <QPainter>
31
32 #include <KDebug>
33 #include <KLocale>
34
35 #include "v4lcapture.h"
36 #include "kdenlivesettings.h"
37 #include "dec.h"
38
39 static src_t v4lsrc;
40
41 QImage add_image_png(src_t *src)
42 {
43     QImage im;
44     im.loadFromData((uchar *)src->img, src->length, "PNG");
45     return im;
46 }
47
48 QImage add_image_jpeg(src_t *src)
49 {
50     uint32_t hlength;
51     uint8_t *himg = NULL;
52     QImage im;
53
54     /* MJPEG data may lack the DHT segment required for decoding... */
55     verify_jpeg_dht((uint8_t *) src->img, src->length, &himg, &hlength);
56
57     im.loadFromData(himg, hlength, "JPG");
58     return im;
59 }
60
61 class MyDisplay : public QLabel
62 {
63 public:
64     MyDisplay(QWidget *parent = 0);
65     void setImage(QImage img);
66     virtual void paintEvent(QPaintEvent *);
67     virtual void resizeEvent(QResizeEvent *);
68
69 private:
70     QImage m_img;
71     bool m_clear;
72 };
73
74 MyDisplay::MyDisplay(QWidget *parent):
75     QLabel(parent)
76     , m_clear(false)
77 {
78     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
79     setAttribute(Qt::WA_PaintOnScreen);
80     setAttribute(Qt::WA_OpaquePaintEvent);
81 }
82
83 void MyDisplay::resizeEvent(QResizeEvent *)
84 {
85     m_clear = true;
86 }
87
88 void MyDisplay::paintEvent(QPaintEvent *)
89 {
90     QPainter p(this);
91     if (m_clear) {
92         // widget resized, cleanup
93         p.fillRect(0, 0, width(), height(), palette().background());
94         m_clear = false;
95     }
96     if (m_img.isNull()) return;
97     QImage img = m_img.scaled(width(), height(), Qt::KeepAspectRatio);
98     p.drawImage((width() - img.width()) / 2, (height() - img.height()) / 2, img);
99     p.end();
100 }
101
102 void MyDisplay::setImage(QImage img)
103 {
104     m_img = img;
105     update();
106 }
107
108
109
110 V4lCaptureHandler::V4lCaptureHandler(QVBoxLayout *lay, QWidget *parent):
111     CaptureHandler(lay, parent)
112     , m_update(false)
113     , m_device(KdenliveSettings::video4vdevice())
114     , m_width(-1)
115     , m_height(-1)
116 {
117     if (lay == NULL) return;
118     m_display = new MyDisplay;
119     lay->addWidget(m_display);
120 }
121
122 QStringList V4lCaptureHandler::getDeviceName(QString input)
123 {
124     fswebcam_config_t *config;
125     /* Prepare the configuration structure. */
126     config = (fswebcam_config_t *) calloc(sizeof(fswebcam_config_t), 1);
127     if (!config) {
128         /*WARN("Out of memory.");*/
129         fprintf(stderr, "Out of MEM....");
130         return QStringList() << input;
131     }
132
133     /* Set the defaults. */
134     config->loop = 0;
135     config->offset = 0;
136     config->background = 0;
137     config->pidfile = NULL;
138     config->logfile = NULL;
139     config->gmt = 0;
140     config->start = 0;
141     config->device = strdup(input.toUtf8().constData());
142     config->input = NULL;
143     config->tuner = 0;
144     config->frequency = 0;
145     config->delay = 0;
146     config->use_read = 0;
147     config->list = 0;
148     if (m_width > 0) config->width = m_width;
149     else config->width = 384;
150     if (m_height > 0) config->height = m_height;
151     else config->height = 288;
152     config->fps = 0;
153     config->frames = 1;
154     config->skipframes = 0;
155     config->palette = SRC_PAL_ANY;
156     config->option = NULL;
157     config->dumpframe = NULL;
158     config->jobs = 0;
159     config->job = NULL;
160
161     /* Set defaults and parse the command line. */
162     /*if(fswc_getopts(config, argc, argv)) return(-1);*/
163
164
165     /* Record the start time. */
166     config->start = time(NULL);
167     /* Set source options... */
168     memset(&v4lsrc, 0, sizeof(v4lsrc));
169     v4lsrc.input      = config->input;
170     v4lsrc.tuner      = config->tuner;
171     v4lsrc.frequency  = config->frequency;
172     v4lsrc.delay      = config->delay;
173     v4lsrc.timeout    = 10; /* seconds */
174     v4lsrc.use_read   = config->use_read;
175     v4lsrc.list       = config->list;
176     v4lsrc.palette    = config->palette;
177     v4lsrc.width      = config->width;
178     v4lsrc.height     = config->height;
179     v4lsrc.fps        = config->fps;
180     v4lsrc.option     = config->option;
181     char *source = config->device;
182     uint width = 0;
183     uint height = 0;
184     char *pixelformatdescription;
185     QString deviceName(src_query(&v4lsrc, source, &width, &height, &pixelformatdescription));
186     QStringList result;
187     result << (deviceName.isEmpty() ? input : deviceName) << (width == 0 ? QString() : QString("%1x%2").arg(width).arg(height)) << QString(pixelformatdescription);
188     return result;
189 }
190
191 void V4lCaptureHandler::setDevice(const QString input, QString size)
192 {
193     m_device = input;
194     if (!size.isEmpty()) {
195         m_width = size.section('x', 0, 0).toInt();
196         m_height = size.section('x', -1).toInt();
197
198     }
199 }
200
201 void V4lCaptureHandler::startPreview(int /*deviceId*/, int /*captureMode*/, bool)
202 {
203     m_display->setHidden(false);
204     fswebcam_config_t *config;
205     /* Prepare the configuration structure. */
206     config = (fswebcam_config_t *) calloc(sizeof(fswebcam_config_t), 1);
207     if (!config) {
208         /*WARN("Out of memory.");*/
209         fprintf(stderr, "Out of MEM....");
210         return;
211     }
212
213     /* Set the defaults. */
214     config->loop = 0;
215     config->offset = 0;
216     config->background = 0;
217     config->pidfile = NULL;
218     config->logfile = NULL;
219     config->gmt = 0;
220     config->start = 0;
221     config->device = strdup(m_device.toUtf8().constData());
222     config->input = NULL;
223     config->tuner = 0;
224     config->frequency = 0;
225     config->delay = 0;
226     config->use_read = 0;
227     config->list = 0;
228     if (m_width > 0) config->width = m_width;
229     else config->width = KdenliveSettings::video4size().section("x", 0, 0).toInt();/*384;*/
230     if (m_height > 0) config->height = m_height;
231     else config->height = KdenliveSettings::video4size().section("x", -1).toInt();/*288;*/
232     config->fps = 0;
233     config->frames = 1;
234     config->skipframes = 0;
235     config->palette = SRC_PAL_ANY;
236     config->option = NULL;
237     config->dumpframe = NULL;
238     config->jobs = 0;
239     config->job = NULL;
240
241     /* Set defaults and parse the command line. */
242     /*if(fswc_getopts(config, argc, argv)) return(-1);*/
243
244
245     /* Record the start time. */
246     config->start = time(NULL);
247     /* Set source options... */
248     memset(&v4lsrc, 0, sizeof(v4lsrc));
249     v4lsrc.input      = config->input;
250     v4lsrc.tuner      = config->tuner;
251     v4lsrc.frequency  = config->frequency;
252     v4lsrc.delay      = config->delay;
253     v4lsrc.timeout    = 10; /* seconds */
254     v4lsrc.use_read   = config->use_read;
255     v4lsrc.list       = config->list;
256     v4lsrc.palette    = config->palette;
257     v4lsrc.width      = config->width;
258     v4lsrc.height     = config->height;
259     v4lsrc.fps        = config->fps;
260     v4lsrc.option     = config->option;
261     char *source = config->device;
262
263     if (src_open(&v4lsrc, source) != 0) return;
264     m_update = true;
265     QTimer::singleShot(200, this, SLOT(slotUpdate()));
266 }
267
268 V4lCaptureHandler::~V4lCaptureHandler()
269 {
270     stopCapture();
271 }
272
273 void V4lCaptureHandler::slotUpdate()
274 {
275     if (!m_update) return;
276     src_grab(&v4lsrc);
277     QImage qimg(v4lsrc.width, v4lsrc.height, QImage::Format_RGB888);
278     switch (v4lsrc.palette) {
279     case SRC_PAL_PNG:
280         qimg = add_image_png(&v4lsrc);
281         break;
282     case SRC_PAL_JPEG:
283     case SRC_PAL_MJPEG:
284         qimg = add_image_jpeg(&v4lsrc);
285         break;
286     case SRC_PAL_S561:
287         fswc_add_image_s561(qimg.bits(), (uchar *)v4lsrc.img, v4lsrc.length, v4lsrc.width, v4lsrc.height, v4lsrc.palette);
288         break;
289     case SRC_PAL_RGB32:
290         fswc_add_image_rgb32(&v4lsrc, qimg.bits());
291         break;
292     case SRC_PAL_BGR32:
293         fswc_add_image_bgr32(&v4lsrc, qimg.bits());
294         break;
295     case SRC_PAL_RGB24:
296         fswc_add_image_rgb24(&v4lsrc, qimg.bits());
297         break;
298     case SRC_PAL_BGR24:
299         fswc_add_image_bgr24(&v4lsrc, qimg.bits());
300         break;
301     case SRC_PAL_BAYER:
302     case SRC_PAL_SGBRG8:
303     case SRC_PAL_SGRBG8:
304         fswc_add_image_bayer(qimg.bits(), (uchar *)v4lsrc.img, v4lsrc.length, v4lsrc.width, v4lsrc.height, v4lsrc.palette);
305         break;
306     case SRC_PAL_YUYV:
307     case SRC_PAL_UYVY:
308         fswc_add_image_yuyv(&v4lsrc, (avgbmp_t *)qimg.bits());
309         break;
310     case SRC_PAL_YUV420P:
311         fswc_add_image_yuv420p(&v4lsrc, qimg.bits());
312         break;
313     case SRC_PAL_NV12MB:
314         fswc_add_image_nv12mb(&v4lsrc, qimg.bits());
315         break;
316     case SRC_PAL_RGB565:
317         fswc_add_image_rgb565(&v4lsrc, qimg.bits());
318         break;
319     case SRC_PAL_RGB555:
320         fswc_add_image_rgb555(&v4lsrc, qimg.bits());
321         break;
322     case SRC_PAL_Y16:
323         fswc_add_image_y16(&v4lsrc, qimg.bits());
324         break;
325     case SRC_PAL_GREY:
326         fswc_add_image_grey(&v4lsrc, qimg.bits());
327         break;
328     }
329
330     if (!m_captureFramePath.isEmpty()) {
331         qimg.save(m_captureFramePath);
332         emit frameSaved(m_captureFramePath);
333         m_captureFramePath.clear();
334     }
335     if (!m_overlayImage.isNull()) {
336         // overlay image
337         QPainter p(&qimg);
338         p.setOpacity(0.5);
339         p.drawImage(0, 0, m_overlayImage);
340         p.end();
341     }
342     m_display->setImage(qimg);
343     if (m_update) QTimer::singleShot(200, this, SLOT(slotUpdate()));
344 }
345
346 void V4lCaptureHandler::startCapture(const QString &/*path*/)
347 {
348 }
349
350 void V4lCaptureHandler::stopCapture()
351 {
352 }
353
354 void V4lCaptureHandler::captureFrame(const QString &fname)
355 {
356     m_captureFramePath = fname;
357 }
358
359 void V4lCaptureHandler::showOverlay(QImage img, bool /*transparent*/)
360 {
361     m_overlayImage = img;
362 }
363
364 void V4lCaptureHandler::hideOverlay()
365 {
366     m_overlayImage = QImage();
367 }
368
369 void V4lCaptureHandler::hidePreview(bool hide)
370 {
371     m_display->setHidden(hide);
372 }
373
374 void V4lCaptureHandler::stopPreview()
375 {
376     m_display->setHidden(true);
377     if (!m_update) return;
378     m_update = false;
379     src_close(&v4lsrc);
380 }
381
382