MrawvDecoder.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 "Media/MrawvDecoder.H"
00039 #include "Util/FileUtil.H"
00040 #include "Util/StringConversions.H"
00041 #include "Video/VideoFrame.H"
00042 #include "rutz/bzip2stream.h"
00043 #include "rutz/error_context.h"
00044 #include "rutz/gzstreambuf.h"
00045 #include "rutz/sfmt.h"
00046 #include "rutz/shared_ptr.h"
00047 #include "rutz/trace.h"
00048 #include <fstream>
00049
00050
00051 namespace
00052 {
00053 enum CompType
00054 {
00055 COMP_NONE,
00056 COMP_GZIP,
00057 COMP_BZIP2
00058 };
00059 }
00060
00061
00062 struct MrawvDecoder::Rep
00063 {
00064 Rep(const std::string& fname_) :
00065 fname(fname_), dims(), ct(COMP_NONE), vf(VIDFMT_AUTO), strm()
00066 {}
00067
00068 const std::string fname;
00069 Dims dims;
00070 CompType ct;
00071 VideoFormat vf;
00072 rutz::shared_ptr<std::istream> strm;
00073 };
00074
00075
00076 MrawvDecoder::MrawvDecoder(const std::string& fname)
00077 :
00078 rep(new Rep(fname))
00079 {
00080
00081 std::string base;
00082 std::string ext = nodotExtension(fname, &base);
00083
00084 if (ext.compare("gz") == 0)
00085 {
00086 ext = nodotExtension(base, &base);
00087 rep->ct = COMP_GZIP;
00088 rep->strm = rutz::igzopen(fname.c_str());
00089 }
00090 else if (ext.compare("bz2") == 0)
00091 {
00092 ext = nodotExtension(base, &base);
00093 rep->ct = COMP_BZIP2;
00094 rep->strm = rutz::ibzip2open(fname.c_str());
00095 }
00096 else
00097 {
00098 std::ifstream *ifs = new std::ifstream(fname.c_str());
00099 if (ifs->is_open() == false)
00100 {
00101 delete ifs;
00102 LFATAL("Could not open '%s' for reading", fname.c_str());
00103 }
00104 rep->strm.reset(ifs);
00105 }
00106
00107 const std::string dimsstr = nodotExtension(base);
00108
00109 LDEBUG("dimsstr is '%s'", dimsstr.c_str());
00110
00111 if (dimsstr.size() == 0)
00112 LFATAL("expected a filename like stem.WWWxHHH.ext[.gz|.bz2], "
00113 "but got '%s' (missing WWWxHHH dims?)", fname.c_str());
00114
00115 rep->dims = fromStr<Dims>(dimsstr);
00116 LDEBUG("parsed dims as %dx%d", rep->dims.w(), rep->dims.h());
00117
00118 if (rep->dims.isEmpty())
00119 LFATAL("expected non-empty dims in filename '%s', but got %dx%d",
00120 fname.c_str(), rep->dims.w(), rep->dims.h());
00121
00122
00123 if (ext[0] != 'm')
00124 LFATAL("Invalid extension of '%s', must start with 'm'.", fname.c_str());
00125
00126
00127 rep->vf = fromStr<VideoFormat>(ext.erase(0,1));
00128 }
00129
00130
00131 MrawvDecoder::~MrawvDecoder()
00132 {
00133 delete rep;
00134 }
00135
00136
00137 GenericFrame MrawvDecoder::readFrame()
00138 {
00139 const bool byteswap = false;
00140
00141 if (rep->ct == COMP_GZIP)
00142 GVX_ERR_CONTEXT(rutz::sfmt("reading gzipped video frame from file '%s'",
00143 rep->fname.c_str()));
00144 else if (rep->ct == COMP_BZIP2)
00145 GVX_ERR_CONTEXT(rutz::sfmt("reading bzip2-compressed "
00146 "video frame from file '%s'",
00147 rep->fname.c_str()));
00148 else
00149 GVX_ERR_CONTEXT(rutz::sfmt("reading raw video frame from file '%s'",
00150 rep->fname.c_str()));
00151
00152 return GenericFrame
00153 (VideoFrame::fromStream(*(rep->strm), rep->dims, rep->vf,
00154 byteswap, false));
00155 }
00156
00157
00158
00159
00160
00161
00162