]> git.sesse.net Git - kdenlive/blob - src/v4l/v4lcapture.cpp
fix v4l memleak
[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     free(config);
187     QStringList result;
188     result << (deviceName.isEmpty() ? input : deviceName) << (width == 0 ? QString() : QString("%1x%2").arg(width).arg(height)) << QString(pixelformatdescription);
189     return result;
190 }
191
192 void V4lCaptureHandler::setDevice(const QString input, QString size)
193 {
194     m_device = input;
195     if (!size.isEmpty()) {
196         m_width = size.section('x', 0, 0).toInt();
197         m_height = size.section('x', -1).toInt();
198
199     }
200 }
201
202 void V4lCaptureHandler::startPreview(int /*deviceId*/, int /*captureMode*/, bool)
203 {
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);
208     if (!config) {
209         /*WARN("Out of memory.");*/
210         fprintf(stderr, "Out of MEM....");
211         return;
212     }
213
214     /* Set the defaults. */
215     config->loop = 0;
216     config->offset = 0;
217     config->background = 0;
218     config->pidfile = NULL;
219     config->logfile = NULL;
220     config->gmt = 0;
221     config->start = 0;
222     config->device = strdup(m_device.toUtf8().constData());
223     config->input = NULL;
224     config->tuner = 0;
225     config->frequency = 0;
226     config->delay = 0;
227     config->use_read = 0;
228     config->list = 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;*/
233     config->fps = 0;
234     config->frames = 1;
235     config->skipframes = 0;
236     config->palette = SRC_PAL_ANY;
237     config->option = NULL;
238     config->dumpframe = NULL;
239     config->jobs = 0;
240     config->job = NULL;
241
242     /* Set defaults and parse the command line. */
243     /*if(fswc_getopts(config, argc, argv)) return(-1);*/
244
245
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;
263
264     if (src_open(&v4lsrc, source) != 0) return;
265     m_update = true;
266     free(config);
267     QTimer::singleShot(200, this, SLOT(slotUpdate()));
268 }
269
270 V4lCaptureHandler::~V4lCaptureHandler()
271 {
272     stopCapture();
273 }
274
275 void V4lCaptureHandler::slotUpdate()
276 {
277     if (!m_update) return;
278     src_grab(&v4lsrc);
279     QImage qimg(v4lsrc.width, v4lsrc.height, QImage::Format_RGB888);
280     switch (v4lsrc.palette) {
281     case SRC_PAL_PNG:
282         qimg = add_image_png(&v4lsrc);
283         break;
284     case SRC_PAL_JPEG:
285     case SRC_PAL_MJPEG:
286         qimg = add_image_jpeg(&v4lsrc);
287         break;
288     case SRC_PAL_S561:
289         fswc_add_image_s561(qimg.bits(), (uchar *)v4lsrc.img, v4lsrc.length, v4lsrc.width, v4lsrc.height, v4lsrc.palette);
290         break;
291     case SRC_PAL_RGB32:
292         fswc_add_image_rgb32(&v4lsrc, qimg.bits());
293         break;
294     case SRC_PAL_BGR32:
295         fswc_add_image_bgr32(&v4lsrc, qimg.bits());
296         break;
297     case SRC_PAL_RGB24:
298         fswc_add_image_rgb24(&v4lsrc, qimg.bits());
299         break;
300     case SRC_PAL_BGR24:
301         fswc_add_image_bgr24(&v4lsrc, qimg.bits());
302         break;
303     case SRC_PAL_BAYER:
304     case SRC_PAL_SGBRG8:
305     case SRC_PAL_SGRBG8:
306         fswc_add_image_bayer(qimg.bits(), (uchar *)v4lsrc.img, v4lsrc.length, v4lsrc.width, v4lsrc.height, v4lsrc.palette);
307         break;
308     case SRC_PAL_YUYV:
309     case SRC_PAL_UYVY:
310         fswc_add_image_yuyv(&v4lsrc, (avgbmp_t *)qimg.bits());
311         break;
312     case SRC_PAL_YUV420P:
313         fswc_add_image_yuv420p(&v4lsrc, qimg.bits());
314         break;
315     case SRC_PAL_NV12MB:
316         fswc_add_image_nv12mb(&v4lsrc, qimg.bits());
317         break;
318     case SRC_PAL_RGB565:
319         fswc_add_image_rgb565(&v4lsrc, qimg.bits());
320         break;
321     case SRC_PAL_RGB555:
322         fswc_add_image_rgb555(&v4lsrc, qimg.bits());
323         break;
324     case SRC_PAL_Y16:
325         fswc_add_image_y16(&v4lsrc, qimg.bits());
326         break;
327     case SRC_PAL_GREY:
328         fswc_add_image_grey(&v4lsrc, qimg.bits());
329         break;
330     }
331
332     if (!m_captureFramePath.isEmpty()) {
333         qimg.save(m_captureFramePath);
334         emit frameSaved(m_captureFramePath);
335         m_captureFramePath.clear();
336     }
337     if (!m_overlayImage.isNull()) {
338         // overlay image
339         QPainter p(&qimg);
340         p.setOpacity(0.5);
341         p.drawImage(0, 0, m_overlayImage);
342         p.end();
343     }
344     m_display->setImage(qimg);
345     if (m_update) QTimer::singleShot(200, this, SLOT(slotUpdate()));
346 }
347
348 void V4lCaptureHandler::startCapture(const QString &/*path*/)
349 {
350 }
351
352 void V4lCaptureHandler::stopCapture()
353 {
354 }
355
356 void V4lCaptureHandler::captureFrame(const QString &fname)
357 {
358     m_captureFramePath = fname;
359 }
360
361 void V4lCaptureHandler::showOverlay(QImage img, bool /*transparent*/)
362 {
363     m_overlayImage = img;
364 }
365
366 void V4lCaptureHandler::hideOverlay()
367 {
368     m_overlayImage = QImage();
369 }
370
371 void V4lCaptureHandler::hidePreview(bool hide)
372 {
373     m_display->setHidden(hide);
374 }
375
376 void V4lCaptureHandler::stopPreview()
377 {
378     m_display->setHidden(true);
379     if (!m_update) return;
380     m_update = false;
381     src_close(&v4lsrc);
382 }
383
384