blob: 09b50509733fa2d51b294051fc004918a66f8cd8 [file] [log] [blame]
Éanna Ó Catháin919c14e2020-09-14 17:36:49 +01001//
2// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6
7#include "CvVideoFrameReader.hpp"
8
9namespace od
10{
11
12std::shared_ptr<cv::Mat> CvVideoFrameReader::ReadFrame()
13{
14 // opencv copies data anyway
15 cv::Mat captureFrame;
16 m_capture.read(captureFrame);
17 return std::make_shared<cv::Mat>(std::move(captureFrame));
18}
19
20bool CvVideoFrameReader::IsExhausted(const std::shared_ptr<cv::Mat>& frame) const
21{
22 assert(frame!=nullptr);
23 return frame->empty();
24}
25
26void CvVideoFrameReader::CheckIsOpen(const std::string& source)
27{
28 if (!m_capture.isOpened())
29 {
30 throw std::runtime_error("Failed to open video capture for the source = " + source);
31 }
32}
33
34void CvVideoFrameReader::Init(const std::string& source)
35{
36 m_capture.open(source);
37 CheckIsOpen(source);
38}
39
40int CvVideoFrameReader::GetSourceWidth() const
41{
42 return static_cast<int>(lround(m_capture.get(cv::CAP_PROP_FRAME_WIDTH)));
43}
44
45int CvVideoFrameReader::GetSourceHeight() const
46{
47 return static_cast<int>(lround(m_capture.get(cv::CAP_PROP_FRAME_HEIGHT)));
48}
49
50double CvVideoFrameReader::GetSourceFps() const
51{
52 return m_capture.get(cv::CAP_PROP_FPS);
53}
54
55bool CvVideoFrameReader::ConvertToRGB()
56{
57 m_capture.set(cv::CAP_PROP_CONVERT_RGB, 1.0);
58 return static_cast<bool>(m_capture.get(cv::CAP_PROP_CONVERT_RGB));
59}
60
61std::string CvVideoFrameReader::GetSourceEncoding() const
62{
63 char fourccStr[5];
64 auto fourcc = (int)m_capture.get(cv::CAP_PROP_FOURCC);
65 sprintf(fourccStr,"%c%c%c%c",fourcc & 0xFF, (fourcc >> 8) & 0xFF, (fourcc >> 16) & 0xFF, (fourcc >> 24) & 0xFF);
66 return fourccStr;
67}
68
69int CvVideoFrameReader::GetSourceEncodingInt() const
70{
71 return (int)m_capture.get(cv::CAP_PROP_FOURCC);
72}
73
74int CvVideoFrameReader::GetFrameCount() const
75{
76 return static_cast<int>(lround(m_capture.get(cv::CAP_PROP_FRAME_COUNT)));
77};
78
79std::shared_ptr<cv::Mat> CvVideoFrameReaderRgbWrapper::ReadFrame()
80{
81 auto framePtr = m_reader->ReadFrame();
82 if (!IsExhausted(framePtr))
83 {
84 cv::cvtColor(*framePtr, *framePtr, cv::COLOR_BGR2RGB);
85 }
86 return framePtr;
87}
88
89bool CvVideoFrameReaderRgbWrapper::IsExhausted(const std::shared_ptr<cv::Mat>& frame) const
90{
91 return m_reader->IsExhausted(frame);
92}
93
94CvVideoFrameReaderRgbWrapper::CvVideoFrameReaderRgbWrapper(std::unique_ptr<od::CvVideoFrameReader> reader):
95 m_reader(std::move(reader))
96{}
97
98}// namespace od