00001 #include "PatGen.hpp"
00002 #include "MPGStream.hpp"
00003 #include <stdlib.h>
00004 #include <stdio.h>
00005 #include <sys/types.h>
00006 #include <sys/stat.h>
00007 #include <sys/time.h>
00008 #include <fcntl.h>
00009 #ifdef WIN32
00010 #include <io.h>
00011 #else
00012 #include <unistd.h>
00013 #include <qmutex.h>
00014 #include <qwaitcondition.h>
00015 #endif
00016 #include <iostream>
00017 #include <fstream>
00018 #include <qdatastream.h>
00019
00020 PatGen::~PatGen () {
00021 delete yuv;
00022 delete mpg;
00023 }
00024
00025 PatGen::PatGen (const QString & yuvfname, const QString & mpgfname,
00026 int x, int y, uint fpp) {
00027 this->init (yuvfname, NULL, mpgfname, x, y, fpp);
00028 }
00029
00030
00031 PatGen::PatGen (const QString & yuvfname, const QString & orgfname,
00032 const QString & mpgfname, int x, int y, uint fpp) {
00033
00034 this->init (yuvfname, orgfname, mpgfname, x, y, fpp);
00035 }
00036
00037
00038 void PatGen::init (const QString & yuvfname, const QString & orgfname,
00039 const QString & mpgfname, int x, int y, uint fpp) {
00040
00041 MPGGOP *mpgpat = NULL;
00042 YUVGOP *yuvpat = NULL;
00043 YUVGOP *orgpat = NULL;
00044 uint fcnt;
00045 FrameType type;
00046 YUVFrame *yuvfr = NULL;
00047 YUVFrame *orgfr = NULL;
00048 MPGFrame *mpgfr = NULL;
00049
00050 uint iCount = 0;
00051 uint pCount = 0;
00052 uint bCount = 0;
00053
00054 uint sumI = 0;
00055 uint sumP = 0;
00056 uint sumB = 0;
00057
00058 framesPerGOP = fpp;
00059
00060
00061 this->mpg = new MPGStream (mpgfname, false);
00062 this->yuv = new YUVStream (yuvfname, x, y, false, fpp);
00063 if (orgfname) {
00064 dmsg (0, "org file name given!");
00065 this->org = new YUVStream (orgfname, x, y, false, fpp);
00066 }
00067
00068 width = x;
00069 height = y;
00070
00071
00072 while ((mpgpat = (MPGGOP*) mpg->addNextGOP (fpp)) != NULL) {
00073 if (((yuvpat=(YUVGOP*) yuv->addNextGOP(mpgpat->getFrameCount ()))==NULL)
00074 || (yuvpat->getFrameCount () != mpgpat->getFrameCount ())) {
00075 emsg ("YUV Stream does not match MPEG-4 Stream!");
00076 break;
00077 }
00078 if (orgfname) {
00079 if (((orgpat = (YUVGOP*) org->addNextGOP (mpgpat->getFrameCount ()))
00080 == NULL) || (orgpat->getFrameCount () != mpgpat->getFrameCount ())) {
00081 fmsg ("Original YUV Stream does not match MPEG-4 Stream!");
00082 break;
00083 }
00084 }
00085 if (orgfname) {
00086 yuvpat->setOrgGOP (orgpat);
00087 yuvpat->getStream ()->fillBuffer (yuvpat);
00088 orgpat->getStream ()->fillBuffer (orgpat);
00089 yuvpat->setPSNRmp (yuvpat->computePSNR (orgpat));
00090 yuvpat->getStream ()->freeBuffer (yuvpat);
00091 orgpat->getStream ()->freeBuffer (orgpat);
00092 }
00093
00094 uint baselayerSize = 0;
00095 uint compFrSize;
00096
00097
00098 for (fcnt = 0; fcnt < mpgpat->getFrameCount (); fcnt++) {
00099 mpgfr = (MPGFrame *) mpgpat->getFrame (fcnt);
00100 yuvfr = (YUVFrame *) yuvpat->getFrame (fcnt);
00101 if (orgfname)
00102 orgfr = (YUVFrame *) yuvpat->getFrame (fcnt);
00103
00104 ASSERT (yuvfr != NULL);
00105 ASSERT (mpgfr != NULL);
00106 if (orgfname)
00107 ASSERT (orgfr != NULL);
00108
00109 mpgfr->setYUVFrame (yuvfr);
00110 yuvfr->setMPGFrame (mpgfr);
00111 type = mpgfr->getType ();
00112 yuvfr->setType (type);
00113 compFrSize = mpgfr->getByteSize ();
00114 yuvfr->setCompressedByteSize (compFrSize);
00115
00116
00117 yuvfr->setNoInStream (mpgfr->getNoInStream ());
00118 if (orgfname)
00119 orgfr->setNoInStream (mpgfr->
00120 getNoInStream ());
00121
00122 if (type == I_VOP) {
00123 iCount++;
00124 sumI += compFrSize;
00125 mpgfr->setPriority (1);
00126 yuvfr->setPriority (1);
00127 }
00128 else if (type == P_VOP) {
00129 pCount++;
00130 sumP += compFrSize;
00131 mpgfr->setPriority (2);
00132 yuvfr->setPriority (2);
00133 }
00134 else if (type == B_VOP) {
00135 bCount++;
00136 sumB += compFrSize;
00137 }
00138
00139 if ((type == I_VOP) || (type == P_VOP))
00140 baselayerSize += compFrSize;
00141 #if MYDEBUG >= 4
00142 printf ("set YUV frame %i type %s MPGFrame %i compressedSize %i\n", fcnt, (type == I_VOP) ? "I" : (type == P_VOP) ? "P" : (type == B_VOP) ? "B" : "U", mpgfr->getNoInDisplay (), yuvfr->getCompressedByteSize ());
00143 #endif
00144 }
00145 yuvpat->setCompressedByteSize (mpgpat->getByteSize ());
00146 yuvpat->setCompressedBaselayerSize (baselayerSize);
00147
00148 if ((yuvpat->getGOPNo () == 0)||(yuv->getMinCompressedPatSize () >
00149 yuvpat->getCompressedByteSize ()))
00150 yuv->setMinCompressedPatSize (yuvpat->
00151 getCompressedByteSize
00152 ());
00153
00154 if ((yuvpat->getGOPNo () == 0) ||
00155 (yuv->getMinCompressedBaselayerSize () >
00156 yuvpat->getCompressedBaselayerSize ()))
00157 yuv->setMinCompressedBaselayerSize (yuvpat->
00158 getCompressedBaselayerSize
00159 ());
00160
00161 if (yuv->getMaxCompressedPatSize () <
00162 yuvpat->getCompressedByteSize ())
00163 yuv->setMaxCompressedPatSize (yuvpat->
00164 getCompressedByteSize
00165 ());
00166
00167 if (yuv->getMaxCompressedBaselayerSize () <
00168 yuvpat->getCompressedBaselayerSize ())
00169 yuv->setMaxCompressedBaselayerSize (yuvpat->
00170 getCompressedBaselayerSize
00171 ());
00172 }
00173 }
00174
00175
00176 PatGen::PatGen (const QString & name) {
00177
00178 QFile file;
00179 file.setName(name);
00180 file.open(IO_ReadOnly);
00181
00182 QDataStream in (&file);
00183
00184
00185 char *yuvName, *mpgName;
00186 int width, height;
00187 uint fpg;
00188
00189 in >> yuvName >> mpgName >> width >> height >> fpg;
00190
00191 this->init (yuvName, NULL, mpgName, width, height, fpg);
00192 }
00193
00194 int PatGen::getWidth() { return width; }
00195
00196 int PatGen::getHeight() { return height; }
00197
00198 YUVStream* PatGen::getYUVStream() { return yuv; }
00199
00200 YUVStream* PatGen::getOrgStream() { return org; }
00201
00202 MPGStream* PatGen::getMPGStream() { return mpg; }