iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
VideoWriterBase.H
Go to the documentation of this file.
1 /*! @file
2  @author Randolph Voorhies
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 
36 #ifndef INCLUDE_NRT_IMAGEPROC_IO_IMAGESINK_VIDEOWRITERS_DETAILS_VIDEOWRITERBASE_H
37 #define INCLUDE_NRT_IMAGEPROC_IO_IMAGESINK_VIDEOWRITERS_DETAILS_VIDEOWRITERBASE_H
38 
39 #include <nrt/config.h>
42 
43 namespace nrt
44 {
45  //! A base class for video writers
46  /*! VideoWriterBase is an abstract base class for video file writers. A class which inherits from VideoWriterBase can be registered with
47  the VideoSink (and thus, the ImageSink) framework, allowing users to easily write video files handled by your class.
48 
49  In order to make a new video file writer, you should derive from this class (implementing all of the necessary pure-virtual methods)
50  and then register the class using the NRT_REGISTER_VIDEO_READER(CLASSNAME) macro. Additionally, you must implement:
51 
52  \li A static std::string id() method which returns the file extensions that your video writer can encode. Multiple choices can be
53  specified by separating them with a '|' character, e.g. "mpg|mpeg".
54 
55  \li A static std::string description() method which returns a helpful description of your video writer/format.
56 
57  Optionally, you should also consider implementing a reader class which derives from VideoReaderBase so that users can read as well as
58  write your new video format.
59 
60  \see VideoSink
61  \see ImageSink */
63  {
64  public:
65  virtual ~VideoWriterBase() {};
66 
67  //! Open the given filename, and prepare to append frames
68  /*! The given quality should be between [1..10], and describes the requested tradeoff between file size and image quality. Requesting
69  a quality of 1 should result in the smallest possible file size, at the cost of poor image quality. A quality of 10 should result
70  in the best possible quality video, at the possible expense of a large file. */
71  virtual void open(std::string const& filename, int quality=7) = 0;
72 
73  //! Append a frame to the current running video.
74  virtual void appendFrame(GenericImage img) = 0;
75 
76  //! Close the file, and prepare to be open()'d again.
77  virtual void close() = 0;
78  };
79 
80  //! A Singleton Factory used to keep track of video writers used by VideoSink.
81  /*! Developers who wish to create their own video writer classes should
82  register them with the NRT_REGISTER_VIDEO_WRITER(CLASSNAME) macro so that they may be
83  recognized by VideoSink
84 
85  \see VideoReaderBase
86  \see VideoSink
87  \see ImageSink
88  */
89  typedef Factory<
90  std::shared_ptr<VideoWriterBase>,
91  std::string,
92  std::function<std::shared_ptr<VideoWriterBase>()>>
94 
95  /*! \def NRT_REGISTER_VIDEO_WRITER(CLASSNAME, EXTENSIONS)
96  Register a video writer with a global Factory.
97 
98  When creating a new video writer class that inherits from VideoWriterBase to
99  encode a video file type, you should register that class with the
100  VideoWriterFactory using this macro so that your new parser will be called
101  whenever users try to encode your associated video type with an ImageSink or
102  a VideoSink.
103 
104  \see VideoWriterBase
105  \see VideoSink
106  \see ImageSink
107  */
108 #define NRT_REGISTER_VIDEO_WRITER(CLASSNAME) \
109  std::shared_ptr<nrt::VideoWriterBase> create##CLASSNAME() \
110  { return std::shared_ptr<nrt::VideoWriterBase>(new CLASSNAME); } \
111  bool videoWriterRegister##CLASSNAME() \
112  { \
113  std::map<std::string,std::string> metadata; \
114  metadata["id"] = CLASSNAME::id(); \
115  metadata["description"] = CLASSNAME::description(); \
116  nrt::VideoWriterFactory::instance().registerType( \
117  metadata["id"], \
118  create##CLASSNAME, \
119  metadata \
120  ); \
121  return true; \
122  } \
123  const bool CLASSNAME##Registered __attribute__ ((unused)) = \
124  videoWriterRegister##CLASSNAME();
125 }
126 
127 #endif // INCLUDE_NRT_IMAGEPROC_IO_IMAGESINK_VIDEOWRITERS_DETAILS_VIDEOWRITERBASE_H
128