00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00024
00025 #include <QVYUV4MPEG2WriterBlock>
00026 #include <qvimageio.h>
00027
00028 #ifdef QVIPP
00029 #include <qvipp.h>
00030 #endif
00031
00032 QVYUV4MPEG2WriterBlock::~QVYUV4MPEG2WriterBlock()
00033 {
00034 videoFile.close();
00035 }
00036
00037 QVYUV4MPEG2WriterBlock::QVYUV4MPEG2WriterBlock(QString name, const QString fileName, const int fps, const bool recording): QVProcessingBlock(name),
00038 initiated(false), rgbMode(true), recording(recording)
00039 {
00040 addProperty<int>("FPS", inputFlag, fps);
00041 addProperty<bool>("RealTime", inputFlag, FALSE, "If the recorder should work in real time mode");
00042 realTimeMode = getPropertyValue<bool>("RealTime");
00043
00044 #ifdef QVIPP
00045 addProperty< QVImage<uChar,3> >("Input image RGB", inputFlag|outputFlag);
00046 #endif
00047
00048 addProperty< QVImage<uChar,1> >("Input image Y", inputFlag|outputFlag);
00049
00050 addTrigger("Start recording");
00051 addTrigger("Pause recording");
00052 addTrigger("Grab single frame");
00053
00054 addProperty< QString >("File", inputFlag, fileName);
00055
00056
00057 if (realTimeMode)
00058 setMinimumDelay((int)(1000/(double) getPropertyValue<int>("FPS")));
00059 }
00060
00061 void QVYUV4MPEG2WriterBlock::processTrigger(QString name)
00062 {
00063 if (name == "Start recording")
00064 recording = true;
00065 else if (name == "Pause recording")
00066 recording = false;
00067 else if (name == "Grab single frame" && recording == false)
00068 grabFrame();
00069 }
00070
00071 bool QVYUV4MPEG2WriterBlock::linkUnspecifiedInputProperty(QVPropertyContainer *sourceContainer, QString sourceProperty, LinkType linkType)
00072 {
00073 LinkType actualLinkType = linkType;
00074
00075
00076 if (!realTimeMode && actualLinkType == AsynchronousLink)
00077 {
00078 std::cout << "Warning @ QVYUV4MPEG2WriterBlock: tried to establish an asynchronous link to a not real time recorder." << std::endl;
00079 actualLinkType = SynchronousLink;
00080 }
00081 else if (realTimeMode && actualLinkType == SynchronousLink)
00082 {
00083 std::cout << "Warning @ QVYUV4MPEG2WriterBlock: tried to establish a synchronous link to a real time recorder." << std::endl;
00084 actualLinkType = AsynchronousLink;
00085 }
00086
00087
00088 if (sourceContainer->isType< QVImage<uChar, 1> >(sourceProperty))
00089 {
00090 rgbMode = false;
00091 return sourceContainer->linkProperty(sourceProperty, this, "Input image Y", linkType);
00092 }
00093 #ifdef QVIPP
00094 else if (sourceContainer->isType< QVImage<uChar, 3> >(sourceProperty))
00095 {
00096 rgbMode = true;
00097 return sourceContainer->linkProperty(sourceProperty, this, "Input image RGB", linkType);
00098 }
00099 #endif
00100
00101 std::cout << "QVYUV4MPEG2WriterBlock::linkProperty(): error, can't link property " << qPrintable(sourceProperty) << "." << std::endl;
00102 return false;
00103 }
00104
00105 void QVYUV4MPEG2WriterBlock::iterate()
00106 {
00107 if (!recording)
00108 return;
00109 grabFrame();
00110 }
00111
00112 void QVYUV4MPEG2WriterBlock::grabFrame()
00113 {
00114 if (!initiated)
00115 {
00116 if (not rgbMode)
00117 {
00118 const QVImage<uChar, 1> imageY = getPropertyValue< QVImage<uChar, 1> >("Input image Y");
00119 cols = imageY.getCols();
00120 rows = imageY.getRows();
00121 }
00122 #ifdef QVIPP
00123 else {
00124 const QVImage<uChar, 3> imageRGB = getPropertyValue< QVImage<uChar, 3> >("Input image RGB");
00125 cols = imageRGB.getCols();
00126 rows = imageRGB.getRows();
00127 }
00128 #endif
00129
00130 if (cols > 1 && rows > 1)
00131 {
00132 videoFile.setFileName(getPropertyValue<QString>("File"));
00133 videoFile.open(QIODevice::WriteOnly|QIODevice::Truncate);
00134 writeYUV4MPEG2Header(videoFile, cols, rows, getPropertyValue<int>("FPS"));
00135 initiated = true;
00136 }
00137 }
00138
00139 if (not rgbMode)
00140 {
00141 const QVImage<uChar, 1> imageY = getPropertyValue< QVImage<uChar, 1> >("Input image Y");
00142
00143 if (imageY.getCols() > cols)
00144 std::cout << "QVYUV4MPEG2WriterBlock::grabFrame(): image has " << imageY.getCols()
00145 << ", but video file is set to " << cols << " columns." << std::endl;
00146 if (imageY.getRows() > rows)
00147 std::cout << "QVYUV4MPEG2WriterBlock::grabFrame(): image has " << imageY.getRows()
00148 << ", but video file is set to " << rows << " rows." << std::endl;
00149
00150 QVImage<uChar, 1> storedImageY(cols, rows);
00151
00152 Set(0, storedImageY);
00153 #ifdef QVIPP
00154 Copy(imageY, storedImageY);
00155 #else
00156 storedImageY = imageY;
00157 #endif
00158
00159 writeYUV4MPEG2Frame(videoFile, imageY);
00160 }
00161 #ifdef QVIPP
00162 else {
00163 const QVImage<uChar, 3> imageRGB = getPropertyValue< QVImage<uChar, 3> >("Input image RGB");
00164
00165 if (imageRGB.getCols() > cols)
00166 std::cout << "QVYUV4MPEG2WriterBlock::grabFrame(): image has " << imageRGB.getCols()
00167 << ", but video file is set to " << cols << " columns." << std::endl;
00168 if (imageRGB.getRows() > rows)
00169 std::cout << "QVYUV4MPEG2WriterBlock::grabFrame(): image has " << imageRGB.getRows()
00170 << ", but video file is set to " << rows << " rows." << std::endl;
00171
00172 QVImage<uChar, 3> storedImage(cols, rows);
00173 const uChar zero[3] = {0,0,0};
00174
00175 Set(zero, storedImage);
00176 Copy(imageRGB, storedImage);
00177
00178 writeYUV4MPEG2Frame(videoFile, storedImage);
00179 }
00180 #endif
00181
00182 }