PARP Research Group University of Murcia, Spain


src/qvio/qvyuv4mpeg2recorder.cpp

Go to the documentation of this file.
00001 /*
00002  *      Copyright (C) 2007, 2008. PARP Research Group.
00003  *      <http://perception.inf.um.es>
00004  *      University of Murcia, Spain.
00005  *
00006  *      This file is part of the QVision library.
00007  *
00008  *      QVision is free software: you can redistribute it and/or modify
00009  *      it under the terms of the GNU Lesser General Public License as
00010  *      published by the Free Software Foundation, version 3 of the License.
00011  *
00012  *      QVision is distributed in the hope that it will be useful,
00013  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *      GNU Lesser General Public License for more details.
00016  *
00017  *      You should have received a copy of the GNU Lesser General Public
00018  *      License along with QVision. If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 
00024 
00025 #include <QVYUV4MPEG2Recorder>
00026 #include <qvipp.h>
00027 
00028 QVYUV4MPEG2Recorder::~QVYUV4MPEG2Recorder()
00029         {
00030         videoFile.close();
00031         }
00032 
00033 QVYUV4MPEG2Recorder::QVYUV4MPEG2Recorder(QString name, const QString fileName, const int fps, const bool recording): QVWorker(name),
00034         initiated(false), rgbMode(true), recording(recording)
00035         {
00036         addProperty<int>("FPS", inputFlag, fps);
00037         addProperty<bool>("RealTime", inputFlag, FALSE, "If the recorder should work in real time mode");
00038         realTimeMode = getPropertyValue<bool>("RealTime");
00039 
00040         addProperty< QVImage<uChar,3> >("Input image RGB", inputFlag|outputFlag);
00041         addProperty< QVImage<uChar,1> >("Input image Y", inputFlag|outputFlag);
00042 
00043         addTrigger("Start recording");
00044         addTrigger("Pause recording");
00045         addTrigger("Grab single frame");
00046 
00047         addProperty< QString >("File", inputFlag, fileName);
00048 
00049         // If the camera is in real time mode, set minimum delay sleep between iterations regarding the fps.
00050         if (realTimeMode)
00051                 setMinimumDelay((int)(1000/(double) getPropertyValue<int>("FPS")));
00052         }
00053  
00054 void QVYUV4MPEG2Recorder::processTrigger(QString name)
00055         {
00056         if (name == "Start recording")
00057                 recording = true;
00058         else if (name == "Pause recording")
00059                 recording = false;
00060         else if (name == "Grab single frame" && recording == false)
00061                 grabFrame();
00062         }
00063 
00064 bool QVYUV4MPEG2Recorder::linkUnspecifiedInputProperty(QVPropertyContainer *sourceContainer, QString sourceProperty, LinkType linkType)
00065         {
00066         LinkType actualLinkType = linkType;
00067 
00068         // Select actual linking type, regarding if the recorder is opened in real time mode.
00069         if (!realTimeMode && actualLinkType == AsynchronousLink)
00070                 {
00071                 std::cout << "Warning @ QVYUV4MPEG2Recorder: tried to stablish an asynchronous link to a not real time recorder." << std::endl;
00072                 actualLinkType = SynchronousLink;
00073                 }
00074         else if (realTimeMode && actualLinkType == SynchronousLink)
00075                 {
00076                 std::cout << "Warning @ QVYUV4MPEG2Recorder: tried to stablish a synchronous link to a real time recorder." << std::endl;
00077                 actualLinkType = AsynchronousLink;
00078                 }
00079 
00080         // Link the source image property, depending on the channel number
00081         if (sourceContainer->isType< QVImage<uChar, 3> >(sourceProperty))
00082                 {
00083                 rgbMode = true;
00084                 return sourceContainer->linkProperty(sourceProperty, this, "Input image RGB", linkType);
00085                 }
00086         else if (sourceContainer->isType< QVImage<uChar, 1> >(sourceProperty))
00087                 {
00088                 rgbMode = false;
00089                 return sourceContainer->linkProperty(sourceProperty, this, "Input image Y", linkType);
00090                 }
00091         std::cout << "QVYUV4MPEG2Recorder::linkProperty(): error, can't link property " << qPrintable(sourceProperty) << "." << std::endl;
00092         return false;
00093         }
00094 
00095 void QVYUV4MPEG2Recorder::iterate()
00096         {
00097         if (!recording)
00098                 return;
00099         grabFrame();
00100         }
00101 
00102 void QVYUV4MPEG2Recorder::grabFrame()
00103         {
00104         if (!initiated)
00105                 {
00106                 if (rgbMode)
00107                         {
00108                         const QVImage<uChar, 3> imageRGB = getPropertyValue< QVImage<uChar, 3> >("Input image RGB");
00109                         cols = imageRGB.getCols();
00110                         rows = imageRGB.getRows();
00111                         }
00112                 else    {
00113                         const QVImage<uChar, 1> imageY = getPropertyValue< QVImage<uChar, 1> >("Input image Y");
00114                         cols = imageY.getCols();
00115                         rows = imageY.getRows();
00116                         }
00117 
00118                 if (cols > 1 && rows > 1)
00119                         {
00120                         videoFile.setFileName(getPropertyValue<QString>("File"));
00121                         videoFile.open(QIODevice::WriteOnly|QIODevice::Truncate);
00122                         writeYUV4MPEG2Header(videoFile, cols, rows, getPropertyValue<int>("FPS"));
00123                         initiated = true;
00124                         }
00125                 }
00126 
00127         if (rgbMode)
00128                 {
00129                 const QVImage<uChar, 3> imageRGB = getPropertyValue< QVImage<uChar, 3> >("Input image RGB");
00130                 if (imageRGB.getCols() != cols)
00131                         std::cout       << "QVYUV4MPEG2Recorder::grabFrame(): image has " << imageRGB.getCols()
00132                                         << ", but video file is set to " << cols << " columns." << std::endl;
00133                 if (imageRGB.getRows() != rows)
00134                         std::cout       << "QVYUV4MPEG2Recorder::grabFrame(): image has " << imageRGB.getRows()
00135                                         << ", but video file is set to " << rows << " rows." << std::endl;
00136 
00137                 writeYUV4MPEG2Frame(videoFile, imageRGB);
00138                 }
00139         else    {
00140                 const QVImage<uChar, 1> imageY = getPropertyValue< QVImage<uChar, 1> >("Input image Y");
00141                 const QVImage<uChar, 3> imageRGB = imageY;
00142 
00143                 if (imageRGB.getCols() != cols)
00144                         std::cout       << "QVYUV4MPEG2Recorder::grabFrame(): image has " << imageRGB.getCols()
00145                                         << ", but video file is set to " << cols << " columns." << std::endl;
00146                 if (imageRGB.getRows() != rows)
00147                         std::cout       << "QVYUV4MPEG2Recorder::grabFrame(): image has " << imageRGB.getRows()
00148                                         << ", but video file is set to " << rows << " rows." << std::endl;
00149 
00150                 writeYUV4MPEG2Frame(videoFile, imageRGB);
00151                 }
00152         }



QVision framework. PARP research group, copyright 2007, 2008.