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 QImage add_image_png(src_t *src)
44 im.loadFromData((uchar *)src->img, src->length, "PNG");
48 QImage add_image_jpeg(src_t *src)
54 /* MJPEG data may lack the DHT segment required for decoding... */
55 verify_jpeg_dht((uint8_t *) src->img, src->length, &himg, &hlength);
57 im.loadFromData(himg, hlength, "JPG");
61 class MyDisplay : public QLabel
64 MyDisplay(QWidget *parent = 0);
65 void setImage(QImage img);
66 virtual void paintEvent(QPaintEvent *);
67 virtual void resizeEvent(QResizeEvent *);
74 MyDisplay::MyDisplay(QWidget *parent):
78 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
79 setAttribute(Qt::WA_PaintOnScreen);
80 setAttribute(Qt::WA_OpaquePaintEvent);
83 void MyDisplay::resizeEvent(QResizeEvent *)
88 void MyDisplay::paintEvent(QPaintEvent *)
92 // widget resized, cleanup
93 p.fillRect(0, 0, width(), height(), palette().background());
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);
102 void MyDisplay::setImage(QImage img)
110 V4lCaptureHandler::V4lCaptureHandler(QVBoxLayout *lay, QWidget *parent):
111 CaptureHandler(lay, parent)
113 , m_device(KdenliveSettings::video4vdevice())
117 if (lay == NULL) return;
118 m_display = new MyDisplay;
119 lay->addWidget(m_display);
122 QStringList V4lCaptureHandler::getDeviceName(QString input)
124 fswebcam_config_t *config;
125 /* Prepare the configuration structure. */
126 config = (fswebcam_config_t *) calloc(sizeof(fswebcam_config_t), 1);
128 /*WARN("Out of memory.");*/
129 fprintf(stderr, "Out of MEM....");
130 return QStringList() << input;
133 /* Set the defaults. */
136 config->background = 0;
137 config->pidfile = NULL;
138 config->logfile = NULL;
141 config->device = strdup(input.toUtf8().constData());
142 config->input = NULL;
144 config->frequency = 0;
146 config->use_read = 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;
154 config->skipframes = 0;
155 config->palette = SRC_PAL_ANY;
156 config->option = NULL;
157 config->dumpframe = NULL;
161 /* Set defaults and parse the command line. */
162 /*if(fswc_getopts(config, argc, argv)) return(-1);*/
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;
184 char *pixelformatdescription;
185 QString deviceName(src_query(&v4lsrc, source, &width, &height, &pixelformatdescription));
188 result << (deviceName.isEmpty() ? input : deviceName) << (width == 0 ? QString() : QString("%1x%2").arg(width).arg(height)) << QString(pixelformatdescription);
192 void V4lCaptureHandler::setDevice(const QString input, QString size)
195 if (!size.isEmpty()) {
196 m_width = size.section('x', 0, 0).toInt();
197 m_height = size.section('x', -1).toInt();
202 void V4lCaptureHandler::startPreview(int /*deviceId*/, int /*captureMode*/, bool)
204 m_display->setHidden(false);
205 fswebcam_config_t *config;
206 /* Prepare the configuration structure. */
207 config = (fswebcam_config_t *) calloc(sizeof(fswebcam_config_t), 1);
209 /*WARN("Out of memory.");*/
210 fprintf(stderr, "Out of MEM....");
214 /* Set the defaults. */
217 config->background = 0;
218 config->pidfile = NULL;
219 config->logfile = NULL;
222 config->device = strdup(m_device.toUtf8().constData());
223 config->input = NULL;
225 config->frequency = 0;
227 config->use_read = 0;
229 if (m_width > 0) config->width = m_width;
230 else config->width = KdenliveSettings::video4size().section("x", 0, 0).toInt();/*384;*/
231 if (m_height > 0) config->height = m_height;
232 else config->height = KdenliveSettings::video4size().section("x", -1).toInt();/*288;*/
235 config->skipframes = 0;
236 config->palette = SRC_PAL_ANY;
237 config->option = NULL;
238 config->dumpframe = NULL;
242 /* Set defaults and parse the command line. */
243 /*if(fswc_getopts(config, argc, argv)) return(-1);*/
246 /* Record the start time. */
247 config->start = time(NULL);
248 /* Set source options... */
249 memset(&v4lsrc, 0, sizeof(v4lsrc));
250 v4lsrc.input = config->input;
251 v4lsrc.tuner = config->tuner;
252 v4lsrc.frequency = config->frequency;
253 v4lsrc.delay = config->delay;
254 v4lsrc.timeout = 10; /* seconds */
255 v4lsrc.use_read = config->use_read;
256 v4lsrc.list = config->list;
257 v4lsrc.palette = config->palette;
258 v4lsrc.width = config->width;
259 v4lsrc.height = config->height;
260 v4lsrc.fps = config->fps;
261 v4lsrc.option = config->option;
262 char *source = config->device;
264 if (src_open(&v4lsrc, source) != 0) return;
267 QTimer::singleShot(200, this, SLOT(slotUpdate()));
270 V4lCaptureHandler::~V4lCaptureHandler()
275 void V4lCaptureHandler::slotUpdate()
277 if (!m_update) return;
279 QImage qimg(v4lsrc.width, v4lsrc.height, QImage::Format_RGB888);
280 switch (v4lsrc.palette) {
282 qimg = add_image_png(&v4lsrc);
286 qimg = add_image_jpeg(&v4lsrc);
289 fswc_add_image_s561(qimg.bits(), (uchar *)v4lsrc.img, v4lsrc.length, v4lsrc.width, v4lsrc.height, v4lsrc.palette);
292 fswc_add_image_rgb32(&v4lsrc, qimg.bits());
295 fswc_add_image_bgr32(&v4lsrc, qimg.bits());
298 fswc_add_image_rgb24(&v4lsrc, qimg.bits());
301 fswc_add_image_bgr24(&v4lsrc, qimg.bits());
306 fswc_add_image_bayer(qimg.bits(), (uchar *)v4lsrc.img, v4lsrc.length, v4lsrc.width, v4lsrc.height, v4lsrc.palette);
310 fswc_add_image_yuyv(&v4lsrc, (avgbmp_t *)qimg.bits());
312 case SRC_PAL_YUV420P:
313 fswc_add_image_yuv420p(&v4lsrc, qimg.bits());
316 fswc_add_image_nv12mb(&v4lsrc, qimg.bits());
319 fswc_add_image_rgb565(&v4lsrc, qimg.bits());
322 fswc_add_image_rgb555(&v4lsrc, qimg.bits());
325 fswc_add_image_y16(&v4lsrc, qimg.bits());
328 fswc_add_image_grey(&v4lsrc, qimg.bits());
332 if (!m_captureFramePath.isEmpty()) {
333 qimg.save(m_captureFramePath);
334 emit frameSaved(m_captureFramePath);
335 m_captureFramePath.clear();
337 if (!m_overlayImage.isNull()) {
341 p.drawImage(0, 0, m_overlayImage);
344 m_display->setImage(qimg);
345 if (m_update) QTimer::singleShot(200, this, SLOT(slotUpdate()));
348 void V4lCaptureHandler::startCapture(const QString &/*path*/)
352 void V4lCaptureHandler::stopCapture()
356 void V4lCaptureHandler::captureFrame(const QString &fname)
358 m_captureFramePath = fname;
361 void V4lCaptureHandler::showOverlay(QImage img, bool /*transparent*/)
363 m_overlayImage = img;
366 void V4lCaptureHandler::hideOverlay()
368 m_overlayImage = QImage();
371 void V4lCaptureHandler::hidePreview(bool hide)
373 m_display->setHidden(hide);
376 void V4lCaptureHandler::stopPreview()
378 m_display->setHidden(true);
379 if (!m_update) return;