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));
187 result << (deviceName.isEmpty() ? input : deviceName) << (width == 0 ? QString() : QString("%1x%2").arg(width).arg(height)) << QString(pixelformatdescription);
191 void V4lCaptureHandler::setDevice(const QString input, QString size)
194 if (!size.isEmpty()) {
195 m_width = size.section('x', 0, 0).toInt();
196 m_height = size.section('x', -1).toInt();
201 void V4lCaptureHandler::startPreview(int /*deviceId*/, int /*captureMode*/, bool)
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);
208 /*WARN("Out of memory.");*/
209 fprintf(stderr, "Out of MEM....");
213 /* Set the defaults. */
216 config->background = 0;
217 config->pidfile = NULL;
218 config->logfile = NULL;
221 config->device = strdup(m_device.toUtf8().constData());
222 config->input = NULL;
224 config->frequency = 0;
226 config->use_read = 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;*/
234 config->skipframes = 0;
235 config->palette = SRC_PAL_ANY;
236 config->option = NULL;
237 config->dumpframe = NULL;
241 /* Set defaults and parse the command line. */
242 /*if(fswc_getopts(config, argc, argv)) return(-1);*/
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;
263 if (src_open(&v4lsrc, source) != 0) return;
265 QTimer::singleShot(200, this, SLOT(slotUpdate()));
268 V4lCaptureHandler::~V4lCaptureHandler()
273 void V4lCaptureHandler::slotUpdate()
275 if (!m_update) return;
277 QImage qimg(v4lsrc.width, v4lsrc.height, QImage::Format_RGB888);
278 switch (v4lsrc.palette) {
280 qimg = add_image_png(&v4lsrc);
284 qimg = add_image_jpeg(&v4lsrc);
287 fswc_add_image_s561(qimg.bits(), (uchar *)v4lsrc.img, v4lsrc.length, v4lsrc.width, v4lsrc.height, v4lsrc.palette);
290 fswc_add_image_rgb32(&v4lsrc, qimg.bits());
293 fswc_add_image_bgr32(&v4lsrc, qimg.bits());
296 fswc_add_image_rgb24(&v4lsrc, qimg.bits());
299 fswc_add_image_bgr24(&v4lsrc, qimg.bits());
304 fswc_add_image_bayer(qimg.bits(), (uchar *)v4lsrc.img, v4lsrc.length, v4lsrc.width, v4lsrc.height, v4lsrc.palette);
308 fswc_add_image_yuyv(&v4lsrc, (avgbmp_t *)qimg.bits());
310 case SRC_PAL_YUV420P:
311 fswc_add_image_yuv420p(&v4lsrc, qimg.bits());
314 fswc_add_image_nv12mb(&v4lsrc, qimg.bits());
317 fswc_add_image_rgb565(&v4lsrc, qimg.bits());
320 fswc_add_image_rgb555(&v4lsrc, qimg.bits());
323 fswc_add_image_y16(&v4lsrc, qimg.bits());
326 fswc_add_image_grey(&v4lsrc, qimg.bits());
330 if (!m_captureFramePath.isEmpty()) {
331 qimg.save(m_captureFramePath);
332 emit frameSaved(m_captureFramePath);
333 m_captureFramePath.clear();
335 if (!m_overlayImage.isNull()) {
339 p.drawImage(0, 0, m_overlayImage);
342 m_display->setImage(qimg);
343 if (m_update) QTimer::singleShot(200, this, SLOT(slotUpdate()));
346 void V4lCaptureHandler::startCapture(const QString &/*path*/)
350 void V4lCaptureHandler::stopCapture()
354 void V4lCaptureHandler::captureFrame(const QString &fname)
356 m_captureFramePath = fname;
359 void V4lCaptureHandler::showOverlay(QImage img, bool /*transparent*/)
361 m_overlayImage = img;
364 void V4lCaptureHandler::hideOverlay()
366 m_overlayImage = QImage();
369 void V4lCaptureHandler::hidePreview(bool hide)
371 m_display->setHidden(hide);
374 void V4lCaptureHandler::stopPreview()
376 m_display->setHidden(true);
377 if (!m_update) return;