Debian Codegraph @

cvd/videofilebuffer.h

raw symbols SCIP
1
2 #ifndef CVD_VIDEOFILEBUFFER_H
3 #define CVD_VIDEOFILEBUFFER_H
4
5 #include <errno.h>
6 #include <fstream>
7 #include <memory>
8 #include <string>
9 #include <vector>
10
11 #include <cvd/image_io.h>
12 #include <cvd/localvideobuffer.h>
13 #include <cvd/videobufferflags.h>
14 #include <cvd/videofilebuffer_frame.h>
15
16 #include <cvd/byte.h>
17 #include <cvd/rgb.h>
18
19 namespace CVD
20 {
21 namespace Exceptions
22 {
23 /// %Exceptions specific to VideoFileBuffer
24 /// @ingroup gException
25 namespace VideoFileBuffer
26 {
27 /// Base class for all VideoFileBuffer exceptions
28 /// @ingroup gException
29 struct All : public CVD::Exceptions::VideoBuffer::All
30 {
31 using CVD::Exceptions::VideoBuffer::All::All;
32 };
33 /// Unable to open the file as a video stream, for various reasons
34 /// @ingroup gException
35 struct FileOpen : public All
36 {
37 FileOpen(const std::string& file, const std::string& error); ///< Construt from filename and error message
38 };
39 /// Unable to open allocate a video frame
40 /// @ingroup gException
41 struct BadFrameAlloc : public All
42 {
43 BadFrameAlloc();
44 };
45 /// Unable to decode the video frame
46 /// @ingroup gException
47 struct BadDecode : public All
48 {
49 BadDecode(double t, const std::string& = ""); ///< Construt from frame timestamp
50 };
51 /// get_frame() was called when at the end of the buffer
52 /// @ingroup gException
53 struct EndOfFile : public All
54 {
55 EndOfFile();
56 };
57 /// seek_to() was called for an invalid timestamp
58 /// @ingroup gException
59 struct BadSeek : public All
60 {
61 BadSeek(double t, const std::string& = ""); ///< Construt from timestamp
62 };
63 }
64 }
65
66 /// Internal VideoFileBuffer helpers
67 namespace VFB
68 {
69
70 #ifndef DOXYGEN_IGNORE_INTERNAL
71 template <class C>
72 struct rgb
73 {
74 static const bool p = C::Error__type_not_valid___Use_byte_or_rgb_of_byte;
75 };
76
77 template <>
78 struct rgb<CVD::byte>
79 {
80 static const bool p = false;
81 };
82
83 template <>
84 struct rgb<CVD::Rgb<CVD::byte>>
85 {
86 static const bool p = true;
87 };
88 #endif
89
90 class A_Frame;
91 class RawVideoFileBufferPIMPL;
92
93 /// Internal (non type-safe) class used by VideoFileBuffer
94 /// This does the real interfacing with the ffmpeg library
95 class RawVideoFileBuffer
96 {
97 public:
98 /// Construct a video buffer to play this file
99 /// @param file The path to the video file
100 /// @param is_rgb Is RGB data wanted?
101 RawVideoFileBuffer(const std::string& file, const std::string& formatname, bool is_rgb, bool verbose, const std::map<std::string, std::string>&);
102 ~RawVideoFileBuffer();
103
104 /// The size of the VideoFrames returned by this buffer
105 ImageRef size();
106
107 /// Returns the next frame from the buffer. This function blocks until a frame is ready.
108 void* get_frame();
109 /// Tell the buffer that you are finished with this frame.
110 /// \param f The frame that you are finished with.
111 void put_frame(void* f);
112
113 /// Is there a frame waiting in the buffer? This function does not block.
114 bool frame_pending();
115
116 /// Go to a particular point in the video buffer (only implemented in buffers of recorded video)
117 /// \param t The frame time in seconds
118 void seek_to(double t);
119
120 /// What should the buffer do when it reaches the end of the list of files?
121 /// @param behaviour The desired behaviour
122 void on_end_of_buffer(VideoBufferFlags::OnEndOfBuffer behaviour);
123
124 /// What is the (expected) frame rate of this video buffer, in frames per second?
125 double frames_per_second();
126
127 /// What is the path to the video file?
128 std::string file_name();
129
130 private:
131 std::unique_ptr<RawVideoFileBufferPIMPL> p;
132 };
133 }
134
135 /// A video buffer to play frames from a video file.
136 /// This uses the ffmpeg library (http://ffmpeg.sourceforge.net/) to play
137 /// a wide range of video formats, including MPEG (1, 2 and 4) and AVI (including
138 /// DivX and DV) files.
139 /// Provides frames of type CVD::VideoFileFrame and throws exceptions of type
140 /// CVD::Exceptions::VideoFileBuffer
141 /// @param T The pixel type of the video frames. Currently only <code>CVD::Rgb<CVD::byte> ></code> and
142 /// <code>CVD::byte></code> are supported.
143 /// @ingroup gVideoBuffer
144 template <typename T>
145 class VideoFileBuffer : public CVD::LocalVideoBuffer<T>
146 {
147 private:
148 VFB::RawVideoFileBuffer vf;
149
150 public:
151 /// Construct a VideoFileBuffer to play this file
152 /// @param file The path to the video file
153 VideoFileBuffer(const std::string& file, bool verbose = false)
154 : LocalVideoBuffer<T>(VideoBufferType::NotLive)
155 , vf(file, "", VFB::rgb<T>::p, verbose, std::map<std::string, std::string> {})
156 {
157 }
158
159 VideoFileBuffer(const std::string& file, const std::string& fmt, bool verbose = false)
160 : LocalVideoBuffer<T>(VideoBufferType::NotLive)
161 , vf(file, fmt, VFB::rgb<T>::p, verbose, std::map<std::string, std::string> {})
162 {
163 }
164
165 VideoFileBuffer(const std::string& file, const std::string& fmt, bool verbose, const std::map<std::string, std::string>& o)
166 : LocalVideoBuffer<T>(VideoBufferType::NotLive)
167 , vf(file, fmt, VFB::rgb<T>::p, verbose, o)
168 {
169 }
170
171 ~VideoFileBuffer()
172 {
173 }
174
175 virtual ImageRef size()
176 {
177 return vf.size();
178 }
179
180 virtual bool frame_pending()
181 {
182 return vf.frame_pending();
183 }
184
185 /// What should the buffer do when it reaches the end of the list of files?
186 /// @param behaviour The desired behaviour
187 virtual void on_end_of_buffer(VideoBufferFlags::OnEndOfBuffer behaviour)
188 {
189 vf.on_end_of_buffer(behaviour);
190 }
191
192 virtual void seek_to(double t)
193 {
194 vf.seek_to(t);
195 }
196
197 virtual VideoFileFrame<T>* get_frame()
198 {
199 return reinterpret_cast<VideoFileFrame<T>*>(vf.get_frame());
200 }
201
202 virtual void put_frame(VideoFrame<T>* f)
203 {
204 vf.put_frame(f);
205 }
206
207 // This class additions
208
209 double frame_rate()
210 {
211 return vf.frames_per_second();
212 }
213
214 /// What is the path to the video file?
215 std::string file_name()
216 {
217 return vf.file_name();
218 }
219
220 private:
221 };
222 }
223
224 #endif