2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2016-09-05 12:00:04 -07:00
|
|
|
|
2017-08-25 17:48:06 -07:00
|
|
|
#ifndef CSCORE_FRAME_H_
|
|
|
|
|
#define CSCORE_FRAME_H_
|
2016-09-05 12:00:04 -07:00
|
|
|
|
|
|
|
|
#include <atomic>
|
2016-10-13 00:16:24 -07:00
|
|
|
#include <memory>
|
2017-08-25 17:48:06 -07:00
|
|
|
#include <string>
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <string_view>
|
2017-08-25 17:48:06 -07:00
|
|
|
#include <utility>
|
|
|
|
|
#include <vector>
|
2016-09-05 12:00:04 -07:00
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
#include <wpi/SmallVector.h>
|
|
|
|
|
#include <wpi/mutex.h>
|
2016-09-05 12:00:04 -07:00
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
#include "Image.h"
|
2017-08-25 17:48:06 -07:00
|
|
|
#include "cscore_cpp.h"
|
2016-09-05 12:00:04 -07:00
|
|
|
|
|
|
|
|
namespace cs {
|
|
|
|
|
|
|
|
|
|
class SourceImpl;
|
|
|
|
|
|
2024-02-12 23:42:17 -08:00
|
|
|
std::unique_ptr<Image> CreateImageFromBGRA(cs::SourceImpl* source, size_t width,
|
|
|
|
|
size_t height, size_t stride,
|
|
|
|
|
const uint8_t* data);
|
|
|
|
|
|
2016-09-05 12:00:04 -07:00
|
|
|
class Frame {
|
|
|
|
|
friend class SourceImpl;
|
|
|
|
|
|
2016-10-13 00:16:24 -07:00
|
|
|
public:
|
2018-09-26 00:09:25 -07:00
|
|
|
using Time = uint64_t;
|
2016-10-13 00:16:24 -07:00
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
private:
|
|
|
|
|
struct Impl {
|
2017-08-25 17:48:06 -07:00
|
|
|
explicit Impl(SourceImpl& source_) : source(source_) {}
|
2016-10-13 00:16:24 -07:00
|
|
|
|
2017-11-13 09:51:26 -08:00
|
|
|
wpi::recursive_mutex mutex;
|
2016-09-05 12:00:04 -07:00
|
|
|
std::atomic_int refcount{0};
|
2016-12-20 20:48:31 -08:00
|
|
|
Time time{0};
|
2025-01-07 09:33:20 -07:00
|
|
|
WPI_TimestampSource timeSource{WPI_TIMESRC_UNKNOWN};
|
2016-12-20 20:48:31 -08:00
|
|
|
SourceImpl& source;
|
|
|
|
|
std::string error;
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::SmallVector<Image*, 4> images;
|
2016-12-20 20:48:31 -08:00
|
|
|
std::vector<int> compressionParams;
|
2016-09-05 12:00:04 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public:
|
2020-12-28 00:37:33 -08:00
|
|
|
Frame() noexcept = default;
|
2016-09-16 18:46:36 -07:00
|
|
|
|
2025-01-07 09:33:20 -07:00
|
|
|
Frame(SourceImpl& source, std::string_view error, Time time,
|
|
|
|
|
WPI_TimestampSource timeSrc);
|
2016-12-20 20:48:31 -08:00
|
|
|
|
2025-01-07 09:33:20 -07:00
|
|
|
Frame(SourceImpl& source, std::unique_ptr<Image> image, Time time,
|
|
|
|
|
WPI_TimestampSource timeSrc);
|
2016-09-05 12:00:04 -07:00
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
Frame(const Frame& frame) noexcept : m_impl{frame.m_impl} {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_impl) {
|
|
|
|
|
++m_impl->refcount;
|
|
|
|
|
}
|
2016-09-05 12:00:04 -07:00
|
|
|
}
|
|
|
|
|
|
2016-09-16 18:46:36 -07:00
|
|
|
Frame(Frame&& other) noexcept : Frame() { swap(*this, other); }
|
2016-09-05 12:00:04 -07:00
|
|
|
|
|
|
|
|
~Frame() { DecRef(); }
|
|
|
|
|
|
2016-09-16 18:46:36 -07:00
|
|
|
Frame& operator=(Frame other) noexcept {
|
|
|
|
|
swap(*this, other);
|
2016-09-05 12:00:04 -07:00
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
explicit operator bool() const { return m_impl && m_impl->error.empty(); }
|
2016-09-05 12:00:04 -07:00
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
friend void swap(Frame& first, Frame& second) noexcept {
|
|
|
|
|
using std::swap;
|
|
|
|
|
swap(first.m_impl, second.m_impl);
|
2016-10-13 00:16:24 -07:00
|
|
|
}
|
|
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
Time GetTime() const { return m_impl ? m_impl->time : 0; }
|
2025-01-07 09:33:20 -07:00
|
|
|
WPI_TimestampSource GetTimeSource() const {
|
|
|
|
|
return m_impl ? m_impl->timeSource : WPI_TIMESRC_UNKNOWN;
|
|
|
|
|
}
|
2016-12-20 20:48:31 -08:00
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
std::string_view GetError() const {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!m_impl) {
|
|
|
|
|
return {};
|
|
|
|
|
}
|
2016-12-20 20:48:31 -08:00
|
|
|
return m_impl->error;
|
2016-09-05 12:00:04 -07:00
|
|
|
}
|
|
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
int GetOriginalWidth() const {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!m_impl) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_impl->mutex);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_impl->images.empty()) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2016-12-20 20:48:31 -08:00
|
|
|
return m_impl->images[0]->width;
|
2016-10-13 00:16:24 -07:00
|
|
|
}
|
|
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
int GetOriginalHeight() const {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!m_impl) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_impl->mutex);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_impl->images.empty()) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2016-12-20 20:48:31 -08:00
|
|
|
return m_impl->images[0]->height;
|
2016-11-10 00:00:20 -08:00
|
|
|
}
|
|
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
int GetOriginalPixelFormat() const {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!m_impl) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_impl->mutex);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_impl->images.empty()) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2016-12-20 20:48:31 -08:00
|
|
|
return m_impl->images[0]->pixelFormat;
|
2016-09-05 12:00:04 -07:00
|
|
|
}
|
|
|
|
|
|
2018-07-27 21:51:40 -07:00
|
|
|
int GetOriginalJpegQuality() const {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!m_impl) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_impl->mutex);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_impl->images.empty()) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2018-07-27 21:51:40 -07:00
|
|
|
return m_impl->images[0]->jpegQuality;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-25 17:48:06 -07:00
|
|
|
Image* GetExistingImage(size_t i = 0) const {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!m_impl) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_impl->mutex);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (i >= m_impl->images.size()) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2016-12-20 20:48:31 -08:00
|
|
|
return m_impl->images[i];
|
2016-11-10 00:00:20 -08:00
|
|
|
}
|
|
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
Image* GetExistingImage(int width, int height) const {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!m_impl) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_impl->mutex);
|
2016-12-20 20:48:31 -08:00
|
|
|
for (auto i : m_impl->images) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (i->Is(width, height)) {
|
|
|
|
|
return i;
|
|
|
|
|
}
|
2016-12-20 20:48:31 -08:00
|
|
|
}
|
|
|
|
|
return nullptr;
|
2016-11-10 00:00:20 -08:00
|
|
|
}
|
|
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
Image* GetExistingImage(int width, int height,
|
|
|
|
|
VideoMode::PixelFormat pixelFormat) const {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!m_impl) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_impl->mutex);
|
2016-12-20 20:48:31 -08:00
|
|
|
for (auto i : m_impl->images) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (i->Is(width, height, pixelFormat)) {
|
|
|
|
|
return i;
|
|
|
|
|
}
|
2016-12-20 20:48:31 -08:00
|
|
|
}
|
|
|
|
|
return nullptr;
|
2016-09-05 12:00:04 -07:00
|
|
|
}
|
|
|
|
|
|
2018-07-27 21:51:40 -07:00
|
|
|
Image* GetExistingImage(int width, int height,
|
|
|
|
|
VideoMode::PixelFormat pixelFormat,
|
|
|
|
|
int jpegQuality) const {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!m_impl) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_impl->mutex);
|
2018-07-27 21:51:40 -07:00
|
|
|
for (auto i : m_impl->images) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (i->Is(width, height, pixelFormat, jpegQuality)) {
|
|
|
|
|
return i;
|
|
|
|
|
}
|
2018-07-27 21:51:40 -07:00
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-20 20:48:31 -08:00
|
|
|
Image* GetNearestImage(int width, int height) const;
|
|
|
|
|
Image* GetNearestImage(int width, int height,
|
2018-07-27 21:51:40 -07:00
|
|
|
VideoMode::PixelFormat pixelFormat,
|
|
|
|
|
int jpegQuality = -1) const;
|
2016-12-20 20:48:31 -08:00
|
|
|
|
2018-07-27 21:51:40 -07:00
|
|
|
Image* Convert(Image* image, VideoMode::PixelFormat pixelFormat) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (pixelFormat == VideoMode::kMJPEG) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2018-07-27 21:51:40 -07:00
|
|
|
return ConvertImpl(image, pixelFormat, -1, 80);
|
|
|
|
|
}
|
|
|
|
|
Image* ConvertToMJPEG(Image* image, int requiredQuality,
|
|
|
|
|
int defaultQuality = 80) {
|
|
|
|
|
return ConvertImpl(image, VideoMode::kMJPEG, requiredQuality,
|
|
|
|
|
defaultQuality);
|
|
|
|
|
}
|
2016-12-20 20:48:31 -08:00
|
|
|
Image* ConvertMJPEGToBGR(Image* image);
|
2016-12-23 21:01:21 -08:00
|
|
|
Image* ConvertMJPEGToGray(Image* image);
|
2016-12-20 20:48:31 -08:00
|
|
|
Image* ConvertYUYVToBGR(Image* image);
|
2022-12-07 10:36:40 -08:00
|
|
|
Image* ConvertYUYVToGray(Image* image);
|
2022-11-23 22:00:31 -08:00
|
|
|
Image* ConvertUYVYToBGR(Image* image);
|
2022-12-07 10:36:40 -08:00
|
|
|
Image* ConvertUYVYToGray(Image* image);
|
2016-12-20 20:48:31 -08:00
|
|
|
Image* ConvertBGRToRGB565(Image* image);
|
|
|
|
|
Image* ConvertRGB565ToBGR(Image* image);
|
2016-12-23 21:01:21 -08:00
|
|
|
Image* ConvertBGRToGray(Image* image);
|
|
|
|
|
Image* ConvertGrayToBGR(Image* image);
|
2016-12-20 20:48:31 -08:00
|
|
|
Image* ConvertBGRToMJPEG(Image* image, int quality);
|
2016-12-23 21:01:21 -08:00
|
|
|
Image* ConvertGrayToMJPEG(Image* image, int quality);
|
2022-11-24 09:06:06 -08:00
|
|
|
Image* ConvertGrayToY16(Image* image);
|
|
|
|
|
Image* ConvertY16ToGray(Image* image);
|
2024-02-12 23:42:17 -08:00
|
|
|
Image* ConvertBGRToBGRA(Image* image);
|
2016-12-20 20:48:31 -08:00
|
|
|
|
2018-07-27 21:51:40 -07:00
|
|
|
Image* GetImage(int width, int height, VideoMode::PixelFormat pixelFormat) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (pixelFormat == VideoMode::kMJPEG) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2018-07-27 21:51:40 -07:00
|
|
|
return GetImageImpl(width, height, pixelFormat, -1, 80);
|
|
|
|
|
}
|
|
|
|
|
Image* GetImageMJPEG(int width, int height, int requiredQuality,
|
|
|
|
|
int defaultQuality = 80) {
|
|
|
|
|
return GetImageImpl(width, height, VideoMode::kMJPEG, requiredQuality,
|
|
|
|
|
defaultQuality);
|
|
|
|
|
}
|
2016-12-20 20:48:31 -08:00
|
|
|
|
2023-11-22 14:35:42 -05:00
|
|
|
bool GetCv(cv::Mat& image, VideoMode::PixelFormat pixelFormat) {
|
|
|
|
|
return GetCv(image, GetOriginalWidth(), GetOriginalHeight(), pixelFormat);
|
2016-09-16 18:46:36 -07:00
|
|
|
}
|
2023-11-22 14:35:42 -05:00
|
|
|
bool GetCv(cv::Mat& image, int width, int height,
|
|
|
|
|
VideoMode::PixelFormat pixelFormat);
|
2016-09-16 18:46:36 -07:00
|
|
|
|
2016-09-05 12:00:04 -07:00
|
|
|
private:
|
2018-07-27 21:51:40 -07:00
|
|
|
Image* ConvertImpl(Image* image, VideoMode::PixelFormat pixelFormat,
|
|
|
|
|
int requiredJpegQuality, int defaultJpegQuality);
|
|
|
|
|
Image* GetImageImpl(int width, int height, VideoMode::PixelFormat pixelFormat,
|
|
|
|
|
int requiredJpegQuality, int defaultJpegQuality);
|
2016-09-05 12:00:04 -07:00
|
|
|
void DecRef() {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_impl && --(m_impl->refcount) == 0) {
|
|
|
|
|
ReleaseFrame();
|
|
|
|
|
}
|
2016-09-05 12:00:04 -07:00
|
|
|
}
|
|
|
|
|
void ReleaseFrame();
|
|
|
|
|
|
2020-12-28 00:28:23 -08:00
|
|
|
Impl* m_impl{nullptr};
|
2016-09-05 12:00:04 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace cs
|
|
|
|
|
|
2017-08-25 17:48:06 -07:00
|
|
|
#endif // CSCORE_FRAME_H_
|