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