2026-06-26 17:55:15 +08:00

64 lines
2.0 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef DRONESCREW_MJPEG_ENCODER_H
#define DRONESCREW_MJPEG_ENCODER_H
#include <cstdint>
#include <cstddef>
#include <vector>
#include <mutex>
// 基于 Rockchip MPP 硬件 MJPEG 的单帧编码器Mono8 → JPEG 字节。
// 用于检测/图传时把裸 Mono8 帧压缩后再走 ZMQ显著降低网络带宽。
//
// 仅在 Linux/RK3588 平台真正可用;其它平台 EncodeMono8 始终返回 false
// 调用方据此回退为发送原始数据。
//
// 内部按 (width,height) 懒初始化/重建编码器EncodeMono8 线程安全。
class MppMjpegEncoder
{
public:
MppMjpegEncoder() = default;
~MppMjpegEncoder();
MppMjpegEncoder(const MppMjpegEncoder&) = delete;
MppMjpegEncoder& operator=(const MppMjpegEncoder&) = delete;
/**
* @brief 将一帧 Mono8紧凑排列stride=width编码为整张 JPEG
* @param mono Mono8 数据指针
* @param monoSize 数据字节数(须 >= width*height
* @param width 图像宽
* @param height 图像高
* @param jpeg 输出:完整 JPEG 字节
* @return true 成功false 失败(调用方应回退原始数据)
*/
bool EncodeMono8(const uint8_t* mono, size_t monoSize,
int width, int height,
std::vector<uint8_t>& jpeg);
/**
* @brief 设置 JPEG 质量因子1~99越大越清晰、体积越大
* 改变后下一帧会自动重建编码器。默认 85接近视觉无损兼顾带宽
*/
void SetQuality(int q);
private:
#if defined(__linux__)
bool ensureInited(int width, int height);
void release();
void* m_ctx{nullptr}; // MppCtx
void* m_mpi{nullptr}; // MppApi*
void* m_grp{nullptr}; // MppBufferGroup
int m_w{0};
int m_h{0};
int m_hor{0};
int m_ver{0};
int m_quality{85};
std::mutex m_mutex;
#else
int m_quality{85};
#endif
};
#endif // DRONESCREW_MJPEG_ENCODER_H