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