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 total_frames = 0;
00033 total_time = 0.0;
00034 current_time = 0.0;
00035
00036
00037 out_rows = rows;
00038 out_cols = cols;
00039
00040
00041 realTime = getPropertyValue<bool>("RealTime");
00042
00043
00044 videoFile.setFileName(fileName);
00045 if (!videoFile.exists())
00046 {
00047 QString msg = QString("QVYUV4MPEG2CameraWorker::openCam: File '") + fileName + "' doesn't seem to exist." ;
00048 qWarning() << msg;
00049 setLastError(msg);
00050 return FALSE;
00051 }
00052 else if (!videoFile.open(QIODevice::ReadOnly))
00053 {
00054 QString msg = QString("QVYUV4MPEG2CameraWorker::openCam: Can't open file '") + fileName + "' in read mode." ;
00055 qWarning() << msg;
00056 setLastError(msg);
00057 return FALSE;
00058 }
00059 else if (!readYUV4MPEG2Header(videoFile, cols, rows, fps))
00060 {
00061 QString msg = QString("QVYUV4MPEG2CameraWorker::openCam: File '") + fileName + "' doesn't seem to be a valid yuv4mpeg2 video file.";
00062 qWarning() << msg;
00063 setLastError(msg);
00064 return FALSE;
00065 }
00066 else
00067 {
00068
00069 header_size = videoFile.pos();
00070
00071
00072 inp_cols = cols;
00073 inp_rows = rows;
00074
00075
00076 if(out_cols != 0 and out_rows != 0)
00077 {
00078
00079 out_cols = out_cols + (out_cols%2);
00080 out_rows = out_rows + (out_rows%2);
00081 }
00082 else
00083 {
00084 out_cols = inp_cols;
00085 out_rows = inp_rows;
00086 }
00087
00088
00089 rescale = (inp_cols != out_cols) or (inp_rows != out_rows);
00090
00091
00092 cols = out_cols; rows = out_rows;
00093
00094
00095 out_fps = fps;
00096
00097
00098
00099 total_frames = (videoFile.size()-header_size) / (inp_cols*inp_rows + inp_cols*inp_rows/2 + 6);
00100
00101
00102 total_time = static_cast<double>(total_frames) / out_fps;
00103
00104 return TRUE;
00105 }
00106 }
00107
00108 void QVYUV4MPEG2CameraWorker::closeCam()
00109 {
00110 videoFile.close();
00111 }
00112
00113 bool QVYUV4MPEG2CameraWorker::grab(QVImage<uChar,1> &imgY, QVImage<uChar,1> &imgU, QVImage<uChar,1> &imgV)
00114 {
00115 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);
00116 bool ret_value = FALSE;
00117
00118
00119 static QTime last_time;
00120 if(realTime)
00121 {
00122 int msecs_to_wait = (int)(1000/(double)out_fps) - last_time.elapsed();
00123 if((msecs_to_wait > 0) and (msecs_to_wait < (int)(1000/(double)out_fps)))
00124 msleep(msecs_to_wait);
00125 }
00126
00127 if (readYUV4MPEG2Frame(videoFile, inp_imgY, inp_imgU, inp_imgV))
00128 {
00129 ret_value = TRUE;
00130 }
00131 else if (videoFile.atEnd())
00132 {
00133 if (noLoop)
00134 return FALSE;
00135 else
00136 {
00137 qDebug() << "QVYUV4MPEG2CameraWorker::grabFrame(): Reseting the video at frame " << getPropertyValue<int>("Frames");
00138 videoFile.reset();
00139 readYUV4MPEG2Frame(videoFile, inp_imgY, inp_imgU, inp_imgV);
00140 ret_value = TRUE;
00141 }
00142 }
00143 else {
00144 qFatal("QVYUV4MPEG2CameraWorker::grab(): readYUV4MPEG2Frame() returned FALSE, and videoFile is not at end... aborting\n");
00145 }
00146
00147
00148 if(rescale)
00149 {
00150 Resize(inp_imgY,imgY);
00151 Resize(inp_imgU,imgU);
00152 Resize(inp_imgV,imgV);
00153 }
00154 else
00155 {
00156 imgY = inp_imgY;
00157 imgU = inp_imgU;
00158 imgV = inp_imgV;
00159 }
00160
00161 if(realTime)
00162 last_time.start();
00163
00164 return ret_value;
00165 }
00166
00167 double QVYUV4MPEG2CameraWorker::lengthOfVideo()
00168 {
00169 return total_time;
00170 }
00171
00172 double QVYUV4MPEG2CameraWorker::currentPos()
00173 {
00174 return total_time*videoFile.pos()/videoFile.size();
00175 }
00176
00177 void QVYUV4MPEG2CameraWorker::setCurrentPos(double time_pos)
00178 {
00179 int frame = (int)((time_pos*total_frames)/total_time);
00180 videoFile.seek(header_size+frame*(inp_cols*inp_rows + inp_cols*inp_rows/2 + 6));
00181 }