00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00033 out_rows = rows;
00034 out_cols = cols;
00035
00036
00037 realTime = getPropertyValue<bool>("RealTime");
00038
00039
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
00063 {
00064
00065 inp_cols = cols;
00066 inp_rows = rows;
00067
00068
00069 if(out_cols != 0 and out_rows != 0)
00070 {
00071
00072 out_cols = out_cols + (out_cols%2);
00073 out_rows = out_rows + (out_rows%2);
00074 }
00075 else
00076 {
00077 out_cols = inp_cols;
00078 out_rows = inp_rows;
00079 }
00080
00081
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
00087 cols = out_cols; rows = out_rows;
00088
00089
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
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
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 }