]> git.sesse.net Git - kdenlive/blob - src/v4l/v4lcapture.cpp
Small improvements to stop motion widget
[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
38
39 static src_t v4lsrc;
40
41 class MyDisplay : public QLabel
42 {
43 public:
44     MyDisplay(QWidget *parent = 0);
45     void setImage(QImage img);
46     virtual void paintEvent(QPaintEvent *);
47     virtual void resizeEvent(QResizeEvent *);
48
49 private:
50     QImage m_img;
51     bool m_clear;
52 };
53
54 MyDisplay::MyDisplay(QWidget *parent):
55     QLabel(parent)
56     , m_clear(false)
57 {
58     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
59     setAttribute(Qt::WA_PaintOnScreen);
60     setAttribute(Qt::WA_OpaquePaintEvent);
61 }
62
63 void MyDisplay::resizeEvent(QResizeEvent *)
64 {
65     m_clear = true;
66 }
67
68 void MyDisplay::paintEvent(QPaintEvent *)
69 {
70     QPainter p(this);
71     if (m_clear) {
72         // widget resized, cleanup
73         p.fillRect(0, 0, width(), height(), palette().background());
74         m_clear = false;
75     }
76     if (m_img.isNull()) return;
77     QImage img = m_img.scaled(width(), height(), Qt::KeepAspectRatio);
78     p.drawImage((width() - img.width()) / 2, (height() - img.height()) / 2, img);
79     p.end();
80 }
81
82 void MyDisplay::setImage(QImage img)
83 {
84     m_img = img;
85     update();
86 }
87
88
89
90 V4lCaptureHandler::V4lCaptureHandler(QVBoxLayout *lay, QWidget *parent):
91     CaptureHandler(lay, parent)
92     , m_update(false)
93 {
94     m_display = new MyDisplay;
95     lay->addWidget(m_display);
96 }
97
98 void V4lCaptureHandler::startPreview(int /*deviceId*/, int /*captureMode*/)
99 {
100     m_display->setHidden(false);
101     fswebcam_config_t *config;
102     /* Prepare the configuration structure. */
103     config = (fswebcam_config_t *) calloc(sizeof(fswebcam_config_t), 1);
104     if (!config) {
105         /*WARN("Out of memory.");*/
106         fprintf(stderr, "Out of MEM....");
107         return;
108     }
109
110     /* Set the defaults. */
111     config->loop = 0;
112     config->offset = 0;
113     config->background = 0;
114     config->pidfile = NULL;
115     config->logfile = NULL;
116     config->gmt = 0;
117     config->start = 0;
118     config->device = strdup(KdenliveSettings::video4vdevice().toUtf8().constData());
119     config->input = NULL;
120     config->tuner = 0;
121     config->frequency = 0;
122     config->delay = 0;
123     config->use_read = 0;
124     config->list = 0;
125     config->width = KdenliveSettings::video4size().section("x", 0, 0).toInt();/*384;*/
126     config->height = KdenliveSettings::video4size().section("x", -1).toInt();/*288;*/
127     config->fps = 0;
128     config->frames = 1;
129     config->skipframes = 0;
130     config->palette = SRC_PAL_ANY;
131     config->option = NULL;
132     config->dumpframe = NULL;
133     config->jobs = 0;
134     config->job = NULL;
135
136     /* Set defaults and parse the command line. */
137     /*if(fswc_getopts(config, argc, argv)) return(-1);*/
138
139
140     /* Record the start time. */
141     config->start = time(NULL);
142     /* Set source options... */
143     memset(&v4lsrc, 0, sizeof(v4lsrc));
144     v4lsrc.input      = config->input;
145     v4lsrc.tuner      = config->tuner;
146     v4lsrc.frequency  = config->frequency;
147     v4lsrc.delay      = config->delay;
148     v4lsrc.timeout    = 10; /* seconds */
149     v4lsrc.use_read   = config->use_read;
150     v4lsrc.list       = config->list;
151     v4lsrc.palette    = config->palette;
152     v4lsrc.width      = config->width;
153     v4lsrc.height     = config->height;
154     v4lsrc.fps        = config->fps;
155     v4lsrc.option     = config->option;
156     char *source = config->device;
157
158     if (src_open(&v4lsrc, source) != 0) return;
159     m_update = true;
160     QTimer::singleShot(200, this, SLOT(slotUpdate()));
161 }
162
163 V4lCaptureHandler::~V4lCaptureHandler()
164 {
165     stopCapture();
166 }
167
168 void V4lCaptureHandler::slotUpdate()
169 {
170     if (!m_update) return;
171     src_grab(&v4lsrc);
172     uint8_t *img = (uint8_t *) v4lsrc.img;
173     uint32_t i = v4lsrc.width * v4lsrc.height;
174
175     if (v4lsrc.length << 2 < i) return;
176
177     QImage qimg(v4lsrc.width, v4lsrc.height, QImage::Format_RGB32);
178     //Format_ARGB32_Premultiplied
179     //convert from uyvy422 to rgba
180     CaptureHandler::yuv2rgb((uchar *)img, (uchar *)qimg.bits(), v4lsrc.width, v4lsrc.height);
181     if (!m_captureFramePath.isEmpty()) {
182         qimg.save(m_captureFramePath);
183         emit frameSaved(m_captureFramePath);
184         m_captureFramePath.clear();
185     }
186     if (!m_overlayImage.isNull()) {
187         // overlay image
188         QPainter p(&qimg);
189         p.setOpacity(0.5);
190         p.drawImage(0, 0, m_overlayImage);
191         p.end();
192     }
193     m_display->setImage(qimg);
194     if (m_update) QTimer::singleShot(200, this, SLOT(slotUpdate()));
195 }
196
197 void V4lCaptureHandler::startCapture(const QString &/*path*/)
198 {
199 }
200
201 void V4lCaptureHandler::stopCapture()
202 {
203 }
204
205 void V4lCaptureHandler::captureFrame(const QString &fname)
206 {
207     m_captureFramePath = fname;
208 }
209
210 void V4lCaptureHandler::showOverlay(QImage img, bool /*transparent*/)
211 {
212     m_overlayImage = img;
213 }
214
215 void V4lCaptureHandler::hideOverlay()
216 {
217     m_overlayImage = QImage();
218 }
219
220 void V4lCaptureHandler::hidePreview(bool hide)
221 {
222     m_display->setHidden(hide);
223 }
224
225 void V4lCaptureHandler::stopPreview()
226 {
227     if (!m_update) return;
228     m_update = false;
229     src_close(&v4lsrc);
230 }