00001 /*!@file Media/MrawvDecoder.C decoder for various raw video formats */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005 // 00005 // by the University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Rob Peters <rjpeters at usc dot edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Media/MrawvDecoder.C $ 00035 // $Id: MrawvDecoder.C 9108 2007-12-30 06:14:30Z rjpeters $ 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 // decode the extension and open the stream: 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 // strip first char of extension: 00123 if (ext[0] != 'm') 00124 LFATAL("Invalid extension of '%s', must start with 'm'.", fname.c_str()); 00125 00126 // get the video format: 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; // not handled for now... 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)); // fail is fatal here, just EOF 00155 } 00156 00157 // ###################################################################### 00158 /* So things look consistent in everyone's emacs... */ 00159 /* Local Variables: */ 00160 /* mode: c++ */ 00161 /* indent-tabs-mode: nil */ 00162 /* End: */