PARP Research Group University of Murcia, Spain


src/qvio/qvyuv4mpeg2cameraworker.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 <QVYUV4MPEG2CameraWorker>
00026 #include <qvipp.h>
00027 
00028 #include <qvdefines.h>
00029 
00030 bool QVYUV4MPEG2CameraWorker::openCam(QString fileName, int &cols, int &rows, int &fps)
00031         {
00032         // We initially save requested number or rows and cols:
00033         out_rows = rows;
00034         out_cols = cols;
00035 
00036         // RealTime flag:
00037         realTime = getPropertyValue<bool>("RealTime");
00038 
00039         // Try to open file, and read yuv4mpeg2 header.
00040         videoFile.setFileName(fileName);
00041         if (!videoFile.exists())
00042                 {
00043                 QString msg = QString("QVYUV4MPEG2CameraWorker::openCam: File '") + fileName + "' doesn't seem to exist." ;
00044                 qWarning() << msg;
00045                 setLastError(msg);
00046                 return FALSE;
00047                 }
00048         else if (!videoFile.open(QIODevice::ReadOnly))
00049                 {
00050                 QString msg = QString("QVYUV4MPEG2CameraWorker::openCam: Can't open file '") + fileName + "' in read mode." ;
00051                 qWarning() << msg;
00052                 setLastError(msg);
00053                 return FALSE;
00054                 }
00055         else if (!readYUV4MPEG2Header(videoFile, cols, rows, fps))
00056                 {
00057                 QString msg = QString("QVYUV4MPEG2CameraWorker::openCam: File '") + fileName + "' doesn't seem to be a valid yuv4mpeg2 video file.";
00058                 qWarning() << msg;
00059                 setLastError(msg);
00060                 return FALSE;
00061                 }
00062         else    // Everything correct.
00063                 {
00064                 // cols, rows and fps were passed by variable (so they have now the real, input source video file values.
00065                 inp_cols = cols;
00066                 inp_rows = rows;
00067 
00068                 // Output cols and rows:
00069                 if(out_cols != 0 and out_rows != 0) 
00070                         {
00071                         // Even rounding of out_rows and out_cols:
00072                         out_cols = out_cols + (out_cols%2);
00073                         out_rows = out_rows + (out_rows%2);
00074                         }
00075                 else // cols==0 or rows==0 means default (input) size:
00076                         {
00077                         out_cols = inp_cols;
00078                         out_rows = inp_rows;
00079                         }
00080 
00081                 // If requested number of cols or rows is different from original input video size, we will have to rescale:
00082                 rescale = (inp_cols != out_cols) or (inp_rows != out_rows);
00083 
00084                 std::cout << inp_cols <<" "<< inp_rows<<" "<< out_cols<<" "<< out_rows<<"\n";
00085 
00086                 // Finally, output cols and rows are "returned" to the caller:
00087                 cols = out_cols; rows = out_rows;
00088 
00089                 // We save FPS (useful for real time cameras):
00090                 out_fps = fps;
00091 
00092                 return TRUE;
00093                 }
00094         }
00095 
00096 void QVYUV4MPEG2CameraWorker::closeCam()
00097         {
00098         videoFile.close();
00099         }
00100 
00101 bool QVYUV4MPEG2CameraWorker::grab(QVImage<uChar,1> &imgY, QVImage<uChar,1> &imgU, QVImage<uChar,1> &imgV)
00102         {
00103         QVImage<uChar,1> inp_imgY(inp_cols,inp_rows), inp_imgU(inp_cols/2,inp_rows/2), inp_imgV(inp_cols/2,inp_rows/2);
00104         bool ret_value = FALSE;
00105 
00106         // Simulated read delay for real time cameras:
00107         static QTime last_time;
00108         if(realTime)
00109                 {
00110                         int msecs_to_wait = (int)(1000/(double)out_fps) - last_time.elapsed();
00111                         if((msecs_to_wait > 0) and (msecs_to_wait < (int)(1000/(double)out_fps)))
00112                                 msleep(msecs_to_wait);
00113                 }
00114 
00115         if (readYUV4MPEG2Frame(videoFile, inp_imgY, inp_imgU, inp_imgV))
00116                 {
00117                 ret_value = TRUE;
00118                 }
00119         else if (videoFile.atEnd())
00120                 {
00121                 if (noLoop)
00122                         return FALSE;
00123                 else
00124                         {
00125                         qDebug() << "QVYUV4MPEG2CameraWorker::grabFrame(): Reseting the video at frame " << getPropertyValue<int>("Frames");
00126                         videoFile.reset();
00127                         readYUV4MPEG2Frame(videoFile, inp_imgY, inp_imgU, inp_imgV);
00128                         ret_value = TRUE;
00129                         }
00130                 }
00131         else    {
00132                 qFatal("QVYUV4MPEG2CameraWorker::grab(): readYUV4MPEG2Frame() returned FALSE, and videoFile is not at end... aborting\n");
00133                 }
00134         
00135         // Possible rescaling:
00136         if(rescale)
00137                 {
00138                 Resize(inp_imgY,imgY);
00139                 Resize(inp_imgU,imgU);
00140                 Resize(inp_imgV,imgV);
00141                 }
00142         else
00143                 {
00144                 imgY = inp_imgY;
00145                 imgU = inp_imgU;
00146                 imgV = inp_imgV;
00147                 }
00148 
00149         if(realTime)
00150                 last_time.start();
00151 
00152         return ret_value;
00153         }



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