| 1 |
|
| 2 |
#ifndef CVD_INCLUDE_DEINTERLACEBUFFER_H |
| 3 |
#define CVD_INCLUDE_DEINTERLACEBUFFER_H |
| 4 |
|
| 5 |
#include <cvd/deinterlaceframe.h> |
| 6 |
#include <cvd/internal/pixel_operations.h> |
| 7 |
#include <cvd/internal/rgb_components.h> |
| 8 |
#include <cvd/videobuffer.h> |
| 9 |
|
| 10 |
namespace CVD |
| 11 |
{ |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
namespace Exceptions |
| 16 |
{ |
| 17 |
|
| 18 |
|
| 19 |
namespace DeinterlaceBuffer |
| 20 |
{ |
| 21 |
|
| 22 |
|
| 23 |
struct All : public CVD::Exceptions::VideoBuffer::All |
| 24 |
{ |
| 25 |
using CVD::Exceptions::VideoBuffer::All::All; |
| 26 |
}; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
struct OddNumberOfLines : public All |
| 31 |
{ |
| 32 |
OddNumberOfLines(); |
| 33 |
}; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
struct DeinterlaceBufferFields |
| 65 |
{ |
| 66 |
enum Fields |
| 67 |
{ |
| 68 |
OddOnly, |
| 69 |
EvenOnly, |
| 70 |
OddEven, |
| 71 |
EvenOdd |
| 72 |
}; |
| 73 |
}; |
| 74 |
|
| 75 |
template <typename T> |
| 76 |
class DeinterlaceBuffer : public VideoBuffer<T> |
| 77 |
{ |
| 78 |
public: |
| 79 |
typedef DeinterlaceBufferFields Fields; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
DeinterlaceBuffer(CVD::VideoBuffer<T>& buf, Fields::Fields fields = Fields::OddEven, bool line_double = false); |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
ImageRef size(); |
| 88 |
|
| 89 |
CVD::VideoFrame<T>* get_frame(); |
| 90 |
|
| 91 |
virtual RawVideoBuffer* source_buffer() |
| 92 |
{ |
| 93 |
return &m_vidbuf; |
| 94 |
} |
| 95 |
|
| 96 |
void put_frame(CVD::VideoFrame<T>* f); |
| 97 |
|
| 98 |
virtual bool frame_pending() |
| 99 |
{ |
| 100 |
return m_vidbuf.frame_pending(); |
| 101 |
} |
| 102 |
|
| 103 |
virtual void seek_to(double t) |
| 104 |
{ |
| 105 |
return m_vidbuf.seek_to(t); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
virtual double frame_rate() |
| 112 |
{ |
| 113 |
if(m_fields == Fields::OddOnly || m_fields == Fields::EvenOnly) |
| 114 |
return m_vidbuf.frame_rate(); |
| 115 |
else |
| 116 |
return m_vidbuf.frame_rate() * 2.0; |
| 117 |
} |
| 118 |
|
| 119 |
private: |
| 120 |
CVD::VideoFrame<T>* my_realframe; |
| 121 |
CVD::VideoBuffer<T>& m_vidbuf; |
| 122 |
Fields::Fields m_fields; |
| 123 |
bool m_loadnewframe; |
| 124 |
ImageRef m_size; |
| 125 |
long m_linebytes; |
| 126 |
bool line_double; |
| 127 |
}; |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
template <typename T> |
| 133 |
DeinterlaceBuffer<T>::DeinterlaceBuffer(CVD::VideoBuffer<T>& vidbuf, Fields::Fields fields, bool l) |
| 134 |
: VideoBuffer<T>(vidbuf.type()) |
| 135 |
, m_vidbuf(vidbuf) |
| 136 |
, m_fields(fields) |
| 137 |
, m_loadnewframe(true) |
| 138 |
, line_double(l) |
| 139 |
{ |
| 140 |
|
| 141 |
if(m_vidbuf.size().y % 2 != 0) |
| 142 |
throw Exceptions::DeinterlaceBuffer::OddNumberOfLines(); |
| 143 |
|
| 144 |
if(line_double == false) |
| 145 |
m_size = ImageRef(m_vidbuf.size().x, m_vidbuf.size().y / 2); |
| 146 |
else |
| 147 |
m_size = m_vidbuf.size(); |
| 148 |
|
| 149 |
m_linebytes = sizeof(T) * m_size.x; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
template <typename T> |
| 156 |
VideoFrame<T>* DeinterlaceBuffer<T>::get_frame() |
| 157 |
{ |
| 158 |
if(m_loadnewframe) |
| 159 |
{ |
| 160 |
|
| 161 |
my_realframe = m_vidbuf.get_frame(); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
double time = my_realframe->timestamp(); |
| 167 |
|
| 168 |
|
| 169 |
if(!m_loadnewframe) |
| 170 |
time += 0.5 / frame_rate(); |
| 171 |
|
| 172 |
DeinterlaceFrame<T>* frame = new DeinterlaceFrame<T>(time, Image<T>(size())); |
| 173 |
|
| 174 |
if(m_fields == Fields::OddOnly || (m_fields == Fields::OddEven && m_loadnewframe) || (m_fields == Fields::EvenOdd && !m_loadnewframe)) |
| 175 |
{ |
| 176 |
|
| 177 |
|
| 178 |
if(line_double) |
| 179 |
{ |
| 180 |
frame->zero(); |
| 181 |
for(int y = 1; y < m_size.y; y += 2) |
| 182 |
for(int x = 0; x < m_size.x; x++) |
| 183 |
(*frame)[y][x] = (*my_realframe)[y][x]; |
| 184 |
|
| 185 |
for(int y = 2; y < m_size.y - 1; y += 2) |
| 186 |
for(int x = 0; x < m_size.x; x++) |
| 187 |
for(unsigned int i = 0; i < Pixel::Component<T>::count; i++) |
| 188 |
Pixel::Component<T>::get((*frame)[y][x], i) = static_cast<typename Pixel::Component<T>::type>((Pixel::Component<T>::get((*my_realframe)[y - 1][x], i) + Pixel::Component<T>::get((*my_realframe)[y + 1][x], i)) / 2); |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
} |
| 206 |
else |
| 207 |
{ |
| 208 |
for(int y = 0; y < m_size.y; y++) |
| 209 |
for(int x = 0; x < m_size.x; x++) |
| 210 |
(*frame)[y][x] = (*my_realframe)[2 * y + 1][x]; |
| 211 |
} |
| 212 |
} |
| 213 |
else |
| 214 |
{ |
| 215 |
|
| 216 |
|
| 217 |
if(line_double) |
| 218 |
{ |
| 219 |
frame->zero(); |
| 220 |
for(int y = 0; y < m_size.y; y += 2) |
| 221 |
for(int x = 0; x < m_size.x; x++) |
| 222 |
(*frame)[y][x] = (*my_realframe)[y][x]; |
| 223 |
|
| 224 |
for(int y = 1; y < m_size.y - 1; y += 2) |
| 225 |
for(int x = 0; x < m_size.x; x++) |
| 226 |
for(unsigned int i = 0; i < Pixel::Component<T>::count; i++) |
| 227 |
Pixel::Component<T>::get((*frame)[y][x], i) = static_cast<typename Pixel::Component<T>::type>((Pixel::Component<T>::get((*my_realframe)[y - 1][x], i) + Pixel::Component<T>::get((*my_realframe)[y + 1][x], i)) / 2); |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
} |
| 243 |
else |
| 244 |
{ |
| 245 |
for(int y = 0; y < m_size.y; y++) |
| 246 |
for(int x = 0; x < m_size.x; x++) |
| 247 |
(*frame)[y][x] = (*my_realframe)[2 * y][x]; |
| 248 |
} |
| 249 |
} |
| 250 |
frame->real_frame = my_realframe; |
| 251 |
|
| 252 |
if(m_fields == Fields::OddEven || m_fields == Fields::EvenOdd) |
| 253 |
{ |
| 254 |
|
| 255 |
m_loadnewframe = !m_loadnewframe; |
| 256 |
} |
| 257 |
|
| 258 |
return frame; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
template <typename T> |
| 265 |
ImageRef DeinterlaceBuffer<T>::size() |
| 266 |
{ |
| 267 |
return m_size; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
template <typename T> |
| 274 |
void DeinterlaceBuffer<T>::put_frame(CVD::VideoFrame<T>* frame) |
| 275 |
{ |
| 276 |
if(m_loadnewframe) |
| 277 |
{ |
| 278 |
|
| 279 |
m_vidbuf.put_frame(my_realframe); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
delete dynamic_cast<DeinterlaceFrame<T>*>(frame); |
| 284 |
} |
| 285 |
|
| 286 |
} |
| 287 |
#endif |