qv4l2: add a menu option to turn off the Capture window.
authorHans Verkuil <hans.verkuil@cisco.com>
Thu, 18 Aug 2011 13:43:47 +0000 (15:43 +0200)
committerHans Verkuil <hans.verkuil@cisco.com>
Thu, 18 Aug 2011 13:43:47 +0000 (15:43 +0200)
Converting frames into a format suitable for displaying can take time.
If you just want to test the framerate of the capture device then it is
useful to be able to just capture without doing any format conversions
and displaying the frames.

Add a new option in the menu to do just this.

Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
utils/qv4l2/capture-win.cpp
utils/qv4l2/capture-win.h
utils/qv4l2/qv4l2.cpp
utils/qv4l2/qv4l2.h
utils/qv4l2/qv4l2.qrc

index c2ba105..6798252 100644 (file)
@@ -30,7 +30,6 @@ CaptureWin::CaptureWin()
        QVBoxLayout *vbox = new QVBoxLayout(this);
 
        setWindowTitle("V4L2 Capture");
-       m_frame = 0;
        m_label = new QLabel();
        m_msg = new QLabel("No frame");
 
@@ -38,32 +37,10 @@ CaptureWin::CaptureWin()
        vbox->addWidget(m_msg);
 }
 
-QString CaptureWin::setImage(const QImage &image, bool init)
+void CaptureWin::setImage(const QImage &image, const QString &status)
 {
-       QString txt;
-
        m_label->setPixmap(QPixmap::fromImage(image));
-       if (init) {
-               m_frame = m_lastFrame = m_fps = 0;
-               txt = "No frame";
-               m_msg->setText(txt);
-       } else {
-               struct timeval tv, res;
-
-               if (m_frame == 0)
-                       gettimeofday(&m_tv, NULL);
-               gettimeofday(&tv, NULL);
-               timersub(&tv, &m_tv, &res);
-               if (res.tv_sec) {
-                       m_fps = (100 * (m_frame - m_lastFrame)) /
-                               (res.tv_sec * 100 + res.tv_usec / 10000);
-                       m_lastFrame = m_frame;
-                       m_tv = tv;
-               }
-               txt = QString("Frame: %1 Fps: %2").arg(++m_frame).arg(m_fps);
-               m_msg->setText(txt);
-       }
-       return txt;
+       m_msg->setText(status);
 }
 
 void CaptureWin::closeEvent(QCloseEvent *event)
index 1626741..e861b12 100644 (file)
@@ -34,8 +34,7 @@ public:
        CaptureWin();
        virtual ~CaptureWin() {}
 
-       QString setImage(const QImage &image, bool init = false);
-       unsigned frame() const { return m_frame; }
+       void setImage(const QImage &image, const QString &status);
 
 protected:
        virtual void closeEvent(QCloseEvent *event);
@@ -46,10 +45,6 @@ signals:
 private:
        QLabel *m_label;
        QLabel *m_msg;
-       unsigned m_frame;
-       unsigned m_lastFrame;
-       unsigned m_fps;
-       struct timeval m_tv;
 };
 
 #endif
index bf4a29f..8be2938 100644 (file)
@@ -77,6 +77,11 @@ ApplicationWindow::ApplicationWindow() :
        m_capStartAct->setDisabled(true);
        connect(m_capStartAct, SIGNAL(toggled(bool)), this, SLOT(capStart(bool)));
 
+       m_showFramesAct = new QAction(QIcon(":/video-television.png"), "Show &frames", this);
+       m_showFramesAct->setStatusTip("Only show captured frames if set.");
+       m_showFramesAct->setCheckable(true);
+       m_showFramesAct->setChecked(true);
+
        QAction *closeAct = new QAction(QIcon(":/fileclose.png"), "&Close", this);
        closeAct->setStatusTip("Close");
        closeAct->setShortcut(Qt::CTRL+Qt::Key_W);
@@ -92,6 +97,7 @@ ApplicationWindow::ApplicationWindow() :
        fileMenu->addAction(openRawAct);
        fileMenu->addAction(closeAct);
        fileMenu->addAction(m_capStartAct);
+       fileMenu->addAction(m_showFramesAct);
        fileMenu->addSeparator();
        fileMenu->addAction(quitAct);
 
@@ -173,7 +179,6 @@ void ApplicationWindow::openrawdev()
 void ApplicationWindow::capFrame()
 {
        v4l2_buffer buf;
-       unsigned i;
        int s = 0;
        int err = 0;
        bool again;
@@ -188,6 +193,8 @@ void ApplicationWindow::capFrame()
                        }
                        return;
                }
+               if (!m_showFrames)
+                       break;
                if (m_mustConvert)
                        err = v4lconvert_convert(m_convertData, &m_capSrcFormat, &m_capDestFormat,
                                m_frameData, s,
@@ -205,13 +212,17 @@ void ApplicationWindow::capFrame()
                if (again)
                        return;
 
-               if (m_mustConvert)
-                       err = v4lconvert_convert(m_convertData, &m_capSrcFormat, &m_capDestFormat,
-                               (unsigned char *)m_buffers[buf.index].start, buf.bytesused,
-                               m_capImage->bits(), m_capDestFormat.fmt.pix.sizeimage);
-               else
-                       memcpy(m_capImage->bits(), (unsigned char *)m_buffers[buf.index].start,
-                                       buf.bytesused);
+               if (m_showFrames) {
+                       if (m_mustConvert)
+                               err = v4lconvert_convert(m_convertData,
+                                       &m_capSrcFormat, &m_capDestFormat,
+                                       (unsigned char *)m_buffers[buf.index].start, buf.bytesused,
+                                       m_capImage->bits(), m_capDestFormat.fmt.pix.sizeimage);
+                       else
+                               memcpy(m_capImage->bits(),
+                                      (unsigned char *)m_buffers[buf.index].start,
+                                      buf.bytesused);
+               }
 
                qbuf(buf);
                break;
@@ -225,18 +236,16 @@ void ApplicationWindow::capFrame()
                if (again)
                        return;
 
-               for (i = 0; i < m_nbuffers; ++i)
-                       if (buf.m.userptr == (unsigned long)m_buffers[i].start
-                                       && buf.length == m_buffers[i].length)
-                               break;
-
-               if (m_mustConvert)
-                       err = v4lconvert_convert(m_convertData, &m_capSrcFormat, &m_capDestFormat,
-                               (unsigned char *)buf.m.userptr, buf.bytesused,
-                               m_capImage->bits(), m_capDestFormat.fmt.pix.sizeimage);
-               else
-                       memcpy(m_capImage->bits(), (unsigned char *)buf.m.userptr,
+               if (m_showFrames) {
+                       if (m_mustConvert)
+                               err = v4lconvert_convert(m_convertData,
+                                       &m_capSrcFormat, &m_capDestFormat,
+                                       (unsigned char *)buf.m.userptr, buf.bytesused,
+                                       m_capImage->bits(), m_capDestFormat.fmt.pix.sizeimage);
+                       else
+                               memcpy(m_capImage->bits(), (unsigned char *)buf.m.userptr,
                                        buf.bytesused);
+               }
 
                qbuf(buf);
                break;
@@ -244,9 +253,24 @@ void ApplicationWindow::capFrame()
        if (err == -1)
                error(v4lconvert_get_error_message(m_convertData));
 
-       QString status = m_capture->setImage(*m_capImage);
+       QString status;
+       struct timeval tv, res;
+
+       if (m_frame == 0)
+               gettimeofday(&m_tv, NULL);
+       gettimeofday(&tv, NULL);
+       timersub(&tv, &m_tv, &res);
+       if (res.tv_sec) {
+               m_fps = (100 * (m_frame - m_lastFrame)) /
+                       (res.tv_sec * 100 + res.tv_usec / 10000);
+               m_lastFrame = m_frame;
+               m_tv = tv;
+       }
+       status = QString("Frame: %1 Fps: %2").arg(++m_frame).arg(m_fps);
+       if (m_showFrames)
+               m_capture->setImage(*m_capImage, status);
        statusBar()->showMessage(status);
-       if (m_capture->frame() == 1)
+       if (m_frame == 1)
                refresh();
 }
 
@@ -437,35 +461,40 @@ void ApplicationWindow::capStart(bool start)
                delete m_capImage;
                return;
        }
+       m_showFrames = m_showFramesAct->isChecked();
+       m_frame = m_lastFrame = m_fps = 0;
        m_capMethod = m_genTab->capMethod();
        g_fmt_cap(m_capSrcFormat);
-       m_frameData = new unsigned char[m_capSrcFormat.fmt.pix.sizeimage];
-       m_capDestFormat = m_capSrcFormat;
-       m_capDestFormat.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
-
-       m_mustConvert = true;
-       for (int i = 0; supported_fmts[i].v4l2_pixfmt; i++) {
-               if (supported_fmts[i].v4l2_pixfmt == m_capSrcFormat.fmt.pix.pixelformat) {
-                       m_capDestFormat.fmt.pix.pixelformat = supported_fmts[i].v4l2_pixfmt;
-                       dstFmt = supported_fmts[i].qt_pixfmt;
-                       m_mustConvert = false;
-                       break;
+
+       m_mustConvert = m_showFrames;
+       if (m_showFrames) {
+               m_frameData = new unsigned char[m_capSrcFormat.fmt.pix.sizeimage];
+               m_capDestFormat = m_capSrcFormat;
+               m_capDestFormat.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
+
+               for (int i = 0; supported_fmts[i].v4l2_pixfmt; i++) {
+                       if (supported_fmts[i].v4l2_pixfmt == m_capSrcFormat.fmt.pix.pixelformat) {
+                               m_capDestFormat.fmt.pix.pixelformat = supported_fmts[i].v4l2_pixfmt;
+                               dstFmt = supported_fmts[i].qt_pixfmt;
+                               m_mustConvert = false;
+                               break;
+                       }
                }
+               if (m_mustConvert) {
+                       v4lconvert_try_format(m_convertData, &m_capDestFormat, &m_capSrcFormat);
+                       // v4lconvert_try_format sometimes modifies the source format if it thinks
+                       // that there is a better format available. Restore our selected source
+                       // format since we do not want that happening.
+                       g_fmt_cap(m_capSrcFormat);
+               }
+
+               m_capture->setMinimumSize(m_capDestFormat.fmt.pix.width, m_capDestFormat.fmt.pix.height);
+               m_capImage = new QImage(m_capDestFormat.fmt.pix.width, m_capDestFormat.fmt.pix.height, dstFmt);
+               m_capImage->fill(0);
+               m_capture->setImage(*m_capImage, "No frame");
+               m_capture->show();
        }
-       if (m_mustConvert) {
-               v4lconvert_try_format(m_convertData, &m_capDestFormat, &m_capSrcFormat);
-               // v4lconvert_try_format sometimes modifies the source format if it thinks
-               // that there is a better format available. Restore our selected source
-               // format since we do not want that happening.
-               g_fmt_cap(m_capSrcFormat);
-       }
-       
-       m_capture->setMinimumSize(m_capDestFormat.fmt.pix.width, m_capDestFormat.fmt.pix.height);
-       m_capImage = new QImage(m_capDestFormat.fmt.pix.width, m_capDestFormat.fmt.pix.height, dstFmt);
-       m_capImage->fill(0);
-       QString status = m_capture->setImage(*m_capImage, true);
-       statusBar()->showMessage(status);
-       m_capture->show();
+       statusBar()->showMessage("No frame");
        if (startCapture(m_capSrcFormat.fmt.pix.sizeimage)) {
                m_capNotifier = new QSocketNotifier(fd(), QSocketNotifier::Read, m_tabs);
                connect(m_capNotifier, SIGNAL(activated(int)), this, SLOT(capFrame()));
index 1cbbbb3..8169b27 100644 (file)
@@ -147,6 +147,7 @@ private:
 
        GeneralTab *m_genTab;
        QAction *m_capStartAct;
+       QAction *m_showFramesAct;
        QString m_filename;
        QSignalMapper *m_sigMapper;
        QTabWidget *m_tabs;
@@ -157,6 +158,11 @@ private:
        WidgetMap m_widgetMap;
        ClassMap m_classMap;
        bool m_haveExtendedUserCtrls;
+       bool m_showFrames;
+       unsigned m_frame;
+       unsigned m_lastFrame;
+       unsigned m_fps;
+       struct timeval m_tv;
 };
 
 extern ApplicationWindow *g_mw;
index 495d725..ec7259c 100644 (file)
@@ -4,6 +4,7 @@
        <file>fileopen.png</file>
        <file>fileclose.png</file>
        <file>record.png</file>
+       <file>video-television.png</file>
        <file>qv4l2.png</file>
     </qresource>
 </RCC>