Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/videoio/src/cap_mjpeg_decoder.cpp
16354 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2015, OpenCV Foundation, all rights reserved.
14
// Third party copyrights are property of their respective owners.
15
//
16
// Redistribution and use in source and binary forms, with or without modification,
17
// are permitted provided that the following conditions are met:
18
//
19
// * Redistribution's of source code must retain the above copyright notice,
20
// this list of conditions and the following disclaimer.
21
//
22
// * Redistribution's in binary form must reproduce the above copyright notice,
23
// this list of conditions and the following disclaimer in the documentation
24
// and/or other materials provided with the distribution.
25
//
26
// * The name of Intel Corporation may not be used to endorse or promote products
27
// derived from this software without specific prior written permission.
28
//
29
// This software is provided by the copyright holders and contributors "as is" and
30
// any express or implied warranties, including, but not limited to, the implied
31
// warranties of merchantability and fitness for a particular purpose are disclaimed.
32
// In no event shall the Intel Corporation or contributors be liable for any direct,
33
// indirect, incidental, special, exemplary, or consequential damages
34
// (including, but not limited to, procurement of substitute goods or services;
35
// loss of use, data, or profits; or business interruption) however caused
36
// and on any theory of liability, whether in contract, strict liability,
37
// or tort (including negligence or otherwise) arising in any way out of
38
// the use of this software, even if advised of the possibility of such damage.
39
//
40
//M*/
41
42
#include "precomp.hpp"
43
#include "opencv2/videoio/container_avi.private.hpp"
44
45
namespace cv
46
{
47
48
class MotionJpegCapture: public IVideoCapture
49
{
50
public:
51
virtual ~MotionJpegCapture() CV_OVERRIDE;
52
virtual double getProperty(int) const CV_OVERRIDE;
53
virtual bool setProperty(int, double) CV_OVERRIDE;
54
virtual bool grabFrame() CV_OVERRIDE;
55
virtual bool retrieveFrame(int, OutputArray) CV_OVERRIDE;
56
virtual bool isOpened() const CV_OVERRIDE;
57
virtual int getCaptureDomain() CV_OVERRIDE { return CAP_OPENCV_MJPEG; }
58
MotionJpegCapture(const String&);
59
60
bool open(const String&);
61
void close();
62
protected:
63
64
inline uint64_t getFramePos() const;
65
66
Ptr<AVIReadContainer> m_avi_container;
67
bool m_is_first_frame;
68
frame_list m_mjpeg_frames;
69
70
frame_iterator m_frame_iterator;
71
Mat m_current_frame;
72
73
//frame width/height and fps could be different for
74
//each frame/stream. At the moment we suppose that they
75
//stays the same within single avi file.
76
uint32_t m_frame_width;
77
uint32_t m_frame_height;
78
double m_fps;
79
};
80
81
uint64_t MotionJpegCapture::getFramePos() const
82
{
83
if(m_is_first_frame)
84
return 0;
85
86
if(m_frame_iterator == m_mjpeg_frames.end())
87
return m_mjpeg_frames.size();
88
89
return m_frame_iterator - m_mjpeg_frames.begin() + 1;
90
}
91
92
bool MotionJpegCapture::setProperty(int property, double value)
93
{
94
if(property == CAP_PROP_POS_FRAMES)
95
{
96
if(int(value) == 0)
97
{
98
m_is_first_frame = true;
99
m_frame_iterator = m_mjpeg_frames.end();
100
return true;
101
}
102
else if(m_mjpeg_frames.size() > value)
103
{
104
m_frame_iterator = m_mjpeg_frames.begin() + int(value - 1);
105
m_is_first_frame = false;
106
return true;
107
}
108
}
109
110
return false;
111
}
112
113
double MotionJpegCapture::getProperty(int property) const
114
{
115
switch(property)
116
{
117
case CAP_PROP_POS_FRAMES:
118
return (double)getFramePos();
119
case CAP_PROP_POS_AVI_RATIO:
120
return double(getFramePos())/m_mjpeg_frames.size();
121
case CAP_PROP_FRAME_WIDTH:
122
return (double)m_frame_width;
123
case CAP_PROP_FRAME_HEIGHT:
124
return (double)m_frame_height;
125
case CAP_PROP_FPS:
126
return m_fps;
127
case CAP_PROP_FOURCC:
128
return (double)CV_FOURCC('M','J','P','G');
129
case CAP_PROP_FRAME_COUNT:
130
return (double)m_mjpeg_frames.size();
131
case CAP_PROP_FORMAT:
132
return 0;
133
default:
134
return 0;
135
}
136
}
137
138
bool MotionJpegCapture::grabFrame()
139
{
140
if(isOpened())
141
{
142
if(m_is_first_frame)
143
{
144
m_is_first_frame = false;
145
m_frame_iterator = m_mjpeg_frames.begin();
146
}
147
else
148
{
149
if (m_frame_iterator == m_mjpeg_frames.end())
150
return false;
151
152
++m_frame_iterator;
153
}
154
}
155
156
return m_frame_iterator != m_mjpeg_frames.end();
157
}
158
159
bool MotionJpegCapture::retrieveFrame(int, OutputArray output_frame)
160
{
161
if(m_frame_iterator != m_mjpeg_frames.end())
162
{
163
std::vector<char> data = m_avi_container->readFrame(m_frame_iterator);
164
165
if(data.size())
166
{
167
m_current_frame = imdecode(data, CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_COLOR | IMREAD_IGNORE_ORIENTATION);
168
}
169
170
m_current_frame.copyTo(output_frame);
171
172
return true;
173
}
174
175
return false;
176
}
177
178
MotionJpegCapture::~MotionJpegCapture()
179
{
180
close();
181
}
182
183
MotionJpegCapture::MotionJpegCapture(const String& filename)
184
{
185
m_avi_container = makePtr<AVIReadContainer>();
186
m_avi_container->initStream(filename);
187
open(filename);
188
}
189
190
bool MotionJpegCapture::isOpened() const
191
{
192
return m_mjpeg_frames.size() > 0;
193
}
194
195
void MotionJpegCapture::close()
196
{
197
m_avi_container->close();
198
m_frame_iterator = m_mjpeg_frames.end();
199
}
200
201
bool MotionJpegCapture::open(const String& filename)
202
{
203
close();
204
205
m_avi_container = makePtr<AVIReadContainer>();
206
m_avi_container->initStream(filename);
207
208
m_frame_iterator = m_mjpeg_frames.end();
209
m_is_first_frame = true;
210
211
if(!m_avi_container->parseRiff(m_mjpeg_frames))
212
{
213
close();
214
} else
215
{
216
m_frame_width = m_avi_container->getWidth();
217
m_frame_height = m_avi_container->getHeight();
218
m_fps = m_avi_container->getFps();
219
}
220
221
return isOpened();
222
}
223
224
Ptr<IVideoCapture> createMotionJpegCapture(const String& filename)
225
{
226
Ptr<MotionJpegCapture> mjdecoder(new MotionJpegCapture(filename));
227
if( mjdecoder->isOpened() )
228
return mjdecoder;
229
return Ptr<MotionJpegCapture>();
230
}
231
232
}
233
234