YuvParser.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "Raster/YuvParser.H"
00039
00040 #include "Raster/GenericFrame.H"
00041 #include "Util/FileUtil.H"
00042 #include "Util/StringConversions.H"
00043 #include "Video/VideoFrame.H"
00044 #include "rutz/bzip2stream.h"
00045 #include "rutz/error_context.h"
00046 #include "rutz/gzstreambuf.h"
00047 #include "rutz/sfmt.h"
00048 #include "rutz/shared_ptr.h"
00049 #include "rutz/trace.h"
00050
00051
00052 namespace dummy_namespace_to_avoid_gcc411_bug_YuvParser_C
00053 {
00054 Dims defaultDims(640, 480);
00055 bool strictLength = true;
00056
00057 enum CompType
00058 {
00059 COMP_NONE,
00060 COMP_GZIP,
00061 COMP_BZIP2
00062 };
00063
00064 struct VideoFileInfo
00065 {
00066 VideoFormat format;
00067 Dims dims;
00068 CompType ctype;
00069 bool beStrict;
00070 };
00071
00072 VideoFileInfo getVideoFileInfoFromFilename(const std::string& fname)
00073 {
00074 VideoFileInfo result;
00075
00076 std::string base;
00077 std::string ext = nodotExtension(fname, &base);
00078
00079 LDEBUG("ext is %s", ext.c_str());
00080
00081 if (ext.compare("gz") == 0)
00082 {
00083 ext = nodotExtension(base, &base);
00084 LDEBUG("new ext is %s", ext.c_str());
00085 result.ctype = COMP_GZIP;
00086 }
00087 else if (ext.compare("bz2") == 0)
00088 {
00089 ext = nodotExtension(base, &base);
00090 LDEBUG("new ext is %s", ext.c_str());
00091 result.ctype = COMP_BZIP2;
00092 }
00093 else
00094 {
00095 result.ctype = COMP_NONE;
00096 }
00097
00098 const std::string dimsstr = nodotExtension(base);
00099
00100 LDEBUG("dimsstr is '%s'", dimsstr.c_str());
00101
00102 if (dimsstr.size() == 0)
00103 {
00104 LERROR("no <width>x<height> specification found in '%s'; "
00105 "assuming default dims of %dx%d instead",
00106 fname.c_str(), defaultDims.w(), defaultDims.h());
00107 result.dims = defaultDims;
00108
00109
00110
00111
00112 result.beStrict = strictLength;
00113 }
00114 else
00115 {
00116 result.dims = fromStr<Dims>(dimsstr);
00117 LDEBUG("parsed dims as %dx%d",
00118 result.dims.w(), result.dims.h());
00119
00120
00121
00122
00123 result.beStrict = false;
00124 }
00125
00126 result.format = fromStr<VideoFormat>(ext);
00127
00128 return result;
00129 }
00130 }
00131
00132 using namespace dummy_namespace_to_avoid_gcc411_bug_YuvParser_C;
00133
00134
00135 struct YuvParser::Rep
00136 {
00137 Rep(const char* fname_) :
00138 fname(fname_),
00139 info(getVideoFileInfoFromFilename(fname))
00140 {}
00141
00142 const std::string fname;
00143 const VideoFileInfo info;
00144 };
00145
00146
00147 YuvParser::YuvParser(const char* fname) :
00148 rep(new Rep(fname))
00149 {
00150 GVX_TRACE(__PRETTY_FUNCTION__);
00151 }
00152
00153
00154 YuvParser::~YuvParser()
00155 {
00156 GVX_TRACE(__PRETTY_FUNCTION__);
00157 delete rep;
00158 }
00159
00160
00161 void YuvParser::setDefaultDims(const Dims& d)
00162 {
00163 GVX_TRACE(__PRETTY_FUNCTION__);
00164 defaultDims = d;
00165 }
00166
00167
00168 Dims YuvParser::getDefaultDims()
00169 {
00170 GVX_TRACE(__PRETTY_FUNCTION__);
00171 return defaultDims;
00172 }
00173
00174
00175 void YuvParser::setStrictDims(bool v)
00176 {
00177 GVX_TRACE(__PRETTY_FUNCTION__);
00178 strictLength = v;
00179 }
00180
00181
00182 bool getStrictDims()
00183 {
00184 GVX_TRACE(__PRETTY_FUNCTION__);
00185 return strictLength;
00186 }
00187
00188
00189 GenericFrameSpec YuvParser::getFrameSpec() const
00190 {
00191 GenericFrameSpec result;
00192
00193 result.nativeType = GenericFrame::VIDEO;
00194 result.videoFormat = rep->info.format;
00195 result.videoByteSwap = false;
00196 result.dims = rep->info.dims;
00197 result.floatFlags = 0;
00198
00199 return result;
00200 }
00201
00202
00203 std::string YuvParser::getComments() const
00204 { return ""; }
00205
00206
00207 uint YuvParser::getTagCount() const
00208 { return 0; }
00209
00210
00211 bool YuvParser::getTag(uint tag, std::string &name, std::string &value) const
00212 { return false; }
00213
00214
00215 GenericFrame YuvParser::getFrame()
00216 {
00217 GVX_TRACE(__PRETTY_FUNCTION__);
00218 const bool byteswap = false;
00219
00220 const VideoFormat vf = rep->info.format;
00221
00222 if (rep->info.ctype == COMP_GZIP)
00223 {
00224 GVX_ERR_CONTEXT(rutz::sfmt("reading gzipped video file '%s'",
00225 rep->fname.c_str()));
00226
00227 rutz::shared_ptr<std::istream> strm =
00228 rutz::igzopen(rep->fname.c_str());
00229
00230 return GenericFrame
00231 (VideoFrame::fromStream(*strm, rep->info.dims, vf, byteswap));
00232 }
00233
00234 else if (rep->info.ctype == COMP_BZIP2)
00235 {
00236 GVX_ERR_CONTEXT(rutz::sfmt("reading bzip2-compressed "
00237 "video file '%s'",
00238 rep->fname.c_str()));
00239
00240 rutz::shared_ptr<std::istream> strm =
00241 rutz::ibzip2open(rep->fname.c_str());
00242
00243 return GenericFrame
00244 (VideoFrame::fromStream(*strm, rep->info.dims, vf, byteswap));
00245 }
00246
00247
00248 GVX_ERR_CONTEXT(rutz::sfmt("reading raw video file '%s'",
00249 rep->fname.c_str()));
00250
00251 return GenericFrame
00252 (VideoFrame::fromFile(rep->fname.c_str(), rep->info.dims,
00253 vf, byteswap, rep->info.beStrict));
00254 }
00255
00256
00257
00258
00259
00260
00261