| 1 |
#ifndef CVD_DISKBUFFER2_H |
| 2 |
#define CVD_DISKBUFFER2_H |
| 3 |
|
| 4 |
#include <errno.h> |
| 5 |
#include <fstream> |
| 6 |
#include <string> |
| 7 |
#include <utility> |
| 8 |
#include <vector> |
| 9 |
|
| 10 |
#include <cvd/diskbuffer2_frame.h> |
| 11 |
#include <cvd/image_io.h> |
| 12 |
#include <cvd/localvideobuffer.h> |
| 13 |
#include <cvd/videobufferflags.h> |
| 14 |
|
| 15 |
namespace CVD |
| 16 |
{ |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
std::vector<std::string> globlist(const std::string& gl); |
| 25 |
|
| 26 |
namespace Exceptions |
| 27 |
{ |
| 28 |
|
| 29 |
|
| 30 |
namespace DiskBuffer2 |
| 31 |
{ |
| 32 |
|
| 33 |
|
| 34 |
struct All : public CVD::Exceptions::VideoBuffer::All |
| 35 |
{ |
| 36 |
using CVD::Exceptions::VideoBuffer::All::All; |
| 37 |
}; |
| 38 |
|
| 39 |
|
| 40 |
struct NoFiles : public All |
| 41 |
{ |
| 42 |
NoFiles(); |
| 43 |
}; |
| 44 |
|
| 45 |
|
| 46 |
struct BadFile : public All |
| 47 |
{ |
| 48 |
BadFile(const std::string&, int); |
| 49 |
}; |
| 50 |
|
| 51 |
|
| 52 |
struct BadImage : public All |
| 53 |
{ |
| 54 |
BadImage(const std::string& file, const std::string& error); |
| 55 |
}; |
| 56 |
|
| 57 |
|
| 58 |
struct BadImageSize : public All |
| 59 |
{ |
| 60 |
BadImageSize(const std::string& file); |
| 61 |
}; |
| 62 |
|
| 63 |
|
| 64 |
struct EndOfBuffer : public All |
| 65 |
{ |
| 66 |
EndOfBuffer(); |
| 67 |
}; |
| 68 |
|
| 69 |
|
| 70 |
struct BadSeek : public All |
| 71 |
{ |
| 72 |
BadSeek(double t); |
| 73 |
}; |
| 74 |
|
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
template <typename T> |
| 86 |
class DiskBuffer2 : public CVD::LocalVideoBuffer<T> |
| 87 |
{ |
| 88 |
public: |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
DiskBuffer2(const std::vector<std::string>& names, double fps, VideoBufferFlags::OnEndOfBuffer eob = VideoBufferFlags::RepeatLastFrame); |
| 96 |
|
| 97 |
virtual ImageRef size() { return my_size; } |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
virtual bool frame_pending() { return frame_ready; } |
| 103 |
|
| 104 |
virtual DiskBuffer2Frame<T>* get_frame(); |
| 105 |
virtual void put_frame(VideoFrame<T>* f); |
| 106 |
virtual void seek_to(double t); |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
virtual void on_end_of_buffer(VideoBufferFlags::OnEndOfBuffer eob) |
| 111 |
{ |
| 112 |
end_of_buffer_behaviour = eob; |
| 113 |
} |
| 114 |
|
| 115 |
virtual double frame_rate() |
| 116 |
{ |
| 117 |
return frames_per_sec; |
| 118 |
} |
| 119 |
|
| 120 |
protected: |
| 121 |
ImageRef my_size; |
| 122 |
int next_frame; |
| 123 |
double start_time; |
| 124 |
double time_per_frame, frames_per_sec; |
| 125 |
bool frame_ready; |
| 126 |
std::vector<std::string> file_names; |
| 127 |
VideoBufferFlags::OnEndOfBuffer end_of_buffer_behaviour; |
| 128 |
}; |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
template <typename T> |
| 134 |
inline DiskBuffer2<T>::DiskBuffer2(const std::vector<std::string>& names, double fps, VideoBufferFlags::OnEndOfBuffer eob) |
| 135 |
: LocalVideoBuffer<T>(VideoBufferType::NotLive) |
| 136 |
, end_of_buffer_behaviour(eob) |
| 137 |
{ |
| 138 |
frames_per_sec = fps; |
| 139 |
|
| 140 |
start_time = 0; |
| 141 |
next_frame = 0; |
| 142 |
time_per_frame = 1 / fps; |
| 143 |
|
| 144 |
file_names = names; |
| 145 |
|
| 146 |
if(file_names.size() == 0) |
| 147 |
throw Exceptions::DiskBuffer2::NoFiles(); |
| 148 |
|
| 149 |
Image<T> foo; |
| 150 |
std::ifstream im; |
| 151 |
im.open(names[0].c_str(), std::ios::in | std::ios::binary); |
| 152 |
|
| 153 |
if(!im.good()) |
| 154 |
throw Exceptions::DiskBuffer2::BadFile(names[0], errno); |
| 155 |
|
| 156 |
try |
| 157 |
{ |
| 158 |
img_load(foo, im); |
| 159 |
} |
| 160 |
catch(Exceptions::Image_IO::All err) |
| 161 |
{ |
| 162 |
throw Exceptions::DiskBuffer2::BadImage(names[0], err.what()); |
| 163 |
} |
| 164 |
|
| 165 |
my_size = foo.size(); |
| 166 |
frame_ready = true; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
template <typename T> |
| 173 |
inline DiskBuffer2Frame<T>* DiskBuffer2<T>::get_frame() |
| 174 |
{ |
| 175 |
if(next_frame < 0) |
| 176 |
next_frame = 0; |
| 177 |
|
| 178 |
if(!frame_pending()) |
| 179 |
throw Exceptions::DiskBuffer2::EndOfBuffer(); |
| 180 |
|
| 181 |
Image<T> foo(my_size); |
| 182 |
|
| 183 |
std::ifstream im_file(file_names[next_frame].c_str(), std::ios::in | std::ios::binary); |
| 184 |
|
| 185 |
if(!im_file.good()) |
| 186 |
throw Exceptions::DiskBuffer2::BadFile(file_names[next_frame], errno); |
| 187 |
|
| 188 |
try |
| 189 |
{ |
| 190 |
img_load(foo, im_file); |
| 191 |
} |
| 192 |
catch(CVD::Exceptions::Image_IO::All err) |
| 193 |
{ |
| 194 |
throw Exceptions::DiskBuffer2::BadImage(file_names[next_frame], err.what()); |
| 195 |
} |
| 196 |
|
| 197 |
DiskBuffer2Frame<T>* vf = new DiskBuffer2Frame<T>(next_frame * time_per_frame + start_time, std::move(foo), file_names[next_frame]); |
| 198 |
|
| 199 |
next_frame++; |
| 200 |
|
| 201 |
if(next_frame > (int)file_names.size() - 1) |
| 202 |
{ |
| 203 |
switch(end_of_buffer_behaviour) |
| 204 |
{ |
| 205 |
case VideoBufferFlags::RepeatLastFrame: |
| 206 |
next_frame = file_names.size() - 1; |
| 207 |
break; |
| 208 |
|
| 209 |
case VideoBufferFlags::UnsetPending: |
| 210 |
frame_ready = false; |
| 211 |
break; |
| 212 |
|
| 213 |
case VideoBufferFlags::Loop: |
| 214 |
next_frame = 0; |
| 215 |
break; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
return vf; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
template <typename T> |
| 226 |
inline void DiskBuffer2<T>::put_frame(VideoFrame<T>* f) |
| 227 |
{ |
| 228 |
|
| 229 |
DiskBuffer2Frame<T>* db2f = dynamic_cast<DiskBuffer2Frame<T>*>(f); |
| 230 |
|
| 231 |
if(db2f == NULL) |
| 232 |
throw CVD::Exceptions::VideoBuffer::BadPutFrame(); |
| 233 |
else |
| 234 |
delete db2f; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
template <typename T> |
| 241 |
inline void DiskBuffer2<T>::seek_to(double t) |
| 242 |
{ |
| 243 |
|
| 244 |
|
| 245 |
int frameno = static_cast<int>((t - start_time) / time_per_frame + 0.5); |
| 246 |
if(frameno < 0 || static_cast<unsigned int>(frameno) > (file_names.size() - 1)) |
| 247 |
throw Exceptions::DiskBuffer2::BadSeek(t); |
| 248 |
next_frame = frameno; |
| 249 |
frame_ready = true; |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
#endif |