1 /***************************************************************************
2 * Copyright (C) 2010 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
35 #include "v4lcapture.h"
36 #include "kdenlivesettings.h"
41 class MyDisplay : public QLabel
44 MyDisplay(QWidget *parent = 0);
45 void setImage(QImage img);
46 virtual void paintEvent(QPaintEvent *);
47 virtual void resizeEvent(QResizeEvent *);
54 MyDisplay::MyDisplay(QWidget *parent):
58 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
59 setAttribute(Qt::WA_PaintOnScreen);
60 setAttribute(Qt::WA_OpaquePaintEvent);
63 void MyDisplay::resizeEvent(QResizeEvent *)
68 void MyDisplay::paintEvent(QPaintEvent *)
72 // widget resized, cleanup
73 p.fillRect(0, 0, width(), height(), palette().background());
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);
82 void MyDisplay::setImage(QImage img)
90 V4lCaptureHandler::V4lCaptureHandler(QVBoxLayout *lay, QWidget *parent):
91 CaptureHandler(lay, parent)
94 if (lay == NULL) return;
95 m_display = new MyDisplay;
96 lay->addWidget(m_display);
99 QString V4lCaptureHandler::getDeviceName(QString input, int *width, int *height)
101 fswebcam_config_t *config;
102 /* Prepare the configuration structure. */
103 config = (fswebcam_config_t *) calloc(sizeof(fswebcam_config_t), 1);
105 /*WARN("Out of memory.");*/
106 fprintf(stderr, "Out of MEM....");
110 /* Set the defaults. */
113 config->background = 0;
114 config->pidfile = NULL;
115 config->logfile = NULL;
118 config->device = strdup(input.toUtf8().constData());
119 config->input = NULL;
121 config->frequency = 0;
123 config->use_read = 0;
126 config->height = 288;
129 config->skipframes = 0;
130 config->palette = SRC_PAL_ANY;
131 config->option = NULL;
132 config->dumpframe = NULL;
136 /* Set defaults and parse the command line. */
137 /*if(fswc_getopts(config, argc, argv)) return(-1);*/
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 QString deviceName = src_query(&v4lsrc, source, width, height);
158 kDebug() << "DEVIE NAME: " << deviceName << ".";
159 return deviceName.isEmpty() ? input : deviceName;
162 void V4lCaptureHandler::startPreview(int /*deviceId*/, int /*captureMode*/, bool)
164 m_display->setHidden(false);
165 fswebcam_config_t *config;
166 /* Prepare the configuration structure. */
167 config = (fswebcam_config_t *) calloc(sizeof(fswebcam_config_t), 1);
169 /*WARN("Out of memory.");*/
170 fprintf(stderr, "Out of MEM....");
174 /* Set the defaults. */
177 config->background = 0;
178 config->pidfile = NULL;
179 config->logfile = NULL;
182 config->device = strdup(KdenliveSettings::video4vdevice().toUtf8().constData());
183 config->input = NULL;
185 config->frequency = 0;
187 config->use_read = 0;
189 config->width = KdenliveSettings::video4size().section("x", 0, 0).toInt();/*384;*/
190 config->height = KdenliveSettings::video4size().section("x", -1).toInt();/*288;*/
193 config->skipframes = 0;
194 config->palette = SRC_PAL_ANY;
195 config->option = NULL;
196 config->dumpframe = NULL;
200 /* Set defaults and parse the command line. */
201 /*if(fswc_getopts(config, argc, argv)) return(-1);*/
204 /* Record the start time. */
205 config->start = time(NULL);
206 /* Set source options... */
207 memset(&v4lsrc, 0, sizeof(v4lsrc));
208 v4lsrc.input = config->input;
209 v4lsrc.tuner = config->tuner;
210 v4lsrc.frequency = config->frequency;
211 v4lsrc.delay = config->delay;
212 v4lsrc.timeout = 10; /* seconds */
213 v4lsrc.use_read = config->use_read;
214 v4lsrc.list = config->list;
215 v4lsrc.palette = config->palette;
216 v4lsrc.width = config->width;
217 v4lsrc.height = config->height;
218 v4lsrc.fps = config->fps;
219 v4lsrc.option = config->option;
220 char *source = config->device;
222 if (src_open(&v4lsrc, source) != 0) return;
224 QTimer::singleShot(200, this, SLOT(slotUpdate()));
227 V4lCaptureHandler::~V4lCaptureHandler()
232 void V4lCaptureHandler::slotUpdate()
234 if (!m_update) return;
236 uint8_t *img = (uint8_t *) v4lsrc.img;
237 uint32_t i = v4lsrc.width * v4lsrc.height;
239 if (v4lsrc.length << 2 < i) return;
241 QImage qimg(v4lsrc.width, v4lsrc.height, QImage::Format_RGB32);
242 //Format_ARGB32_Premultiplied
243 //convert from uyvy422 to rgba
244 CaptureHandler::yuv2rgb((uchar *)img, (uchar *)qimg.bits(), v4lsrc.width, v4lsrc.height);
245 if (!m_captureFramePath.isEmpty()) {
246 qimg.save(m_captureFramePath);
247 emit frameSaved(m_captureFramePath);
248 m_captureFramePath.clear();
250 if (!m_overlayImage.isNull()) {
254 p.drawImage(0, 0, m_overlayImage);
257 m_display->setImage(qimg);
258 if (m_update) QTimer::singleShot(200, this, SLOT(slotUpdate()));
261 void V4lCaptureHandler::startCapture(const QString &/*path*/)
265 void V4lCaptureHandler::stopCapture()
269 void V4lCaptureHandler::captureFrame(const QString &fname)
271 m_captureFramePath = fname;
274 void V4lCaptureHandler::showOverlay(QImage img, bool /*transparent*/)
276 m_overlayImage = img;
279 void V4lCaptureHandler::hideOverlay()
281 m_overlayImage = QImage();
284 void V4lCaptureHandler::hidePreview(bool hide)
286 m_display->setHidden(hide);
289 void V4lCaptureHandler::stopPreview()
291 if (!m_update) return;