3.4.3 JDK API Descriptions
1.Data Type Definitions (data_type
)
1.1 Enum: media_type
Device media type enumeration:
Value | Description |
---|---|
MEDIA_TYPE_CANT_STAT | Cannot retrieve device status |
MEDIA_TYPE_UNKNOWN | Unknown |
MEDIA_TYPE_VIDEO | Video |
MEDIA_TYPE_VBI | VBI (Vertical Blanking Interval) |
MEDIA_TYPE_RADIO | Radio |
MEDIA_TYPE_SDR | SDR (Software Defined Radio) |
MEDIA_TYPE_TOUCH | Touch input |
MEDIA_TYPE_SUBDEV | Subdevice |
MEDIA_TYPE_DVB_FRONTEND | Digital TV frontend |
MEDIA_TYPE_DVB_DEMUX | Digital TV demux |
MEDIA_TYPE_DVB_DVR | Digital TV DVR |
MEDIA_TYPE_DVB_NET | Digital TV network |
MEDIA_TYPE_DTV_CA | Digital TV conditional access |
MEDIA_TYPE_MEDIA | Media device |
1.2 Enum: codec_type
Indicates whether the device/context is for encoding or decoding:
Value | Meaning |
---|---|
NOT_CODEC | Not codec |
CODEC_DEC | Decoder |
CODEC_ENC | Encoder |
1.3 Struct: v4l2_ctx
Structure for V4L2 capture and encoding context:
struct v4l2_ctx {
int fd; // Device file handle
unsigned int width; // Video width
unsigned int height; // Video height
unsigned int pixelformat; // Input the pixel format
unsigned int out_pixelformat; // Output pixel format
int nplanes; // Input the number of planes
int out_nplanes; // Number of output planes
struct buffer* cap_buffers; // Capture buffer arrays
struct buffer* out_buffers; // Output buffer array
__u32 bytesperline[VIDEO_MAX_PLANES]; // The number of bytes of each input plane line
__u32 out_bytesperline[VIDEO_MAX_PLANES]; // The number of bytes in each output plane row
FILE* file[2]; // Input/output file pointer
int verbose; // Detailed level of the log
enum codec_type ctype; // Encoding/decoding type
};
2.Core Interfaces
2.1 JdkFrame
: Image Frame Wrapper Class
class JdkFrame {
public:
JdkFrame(int dma_fd_, size_t size_, int w, int h);
~JdkFrame();
// Map the DMA buffer to the CPU memory and return the pointer
unsigned char* toHost() const;
// Clone to return a copy of the data
std::vector<unsigned char> Clone() const;
// Save as NV12 format .yuv file
bool saveToFile(const std::string& filename) const;
// Load data from the file (used in pairing with saveToFile)
bool loadFromFile(const std::string& filename, size_t expected_size);
// Get the underlying DMA FD
int getDMAFd() const;
// Get the size of the buffer
size_t getSize() const { return size_; }
// Get the resolution
int getWidth() const { return width_; }
int getHeight() const { return height_; }
// Copy the original NALU data to the internal buffer (such as coding and writing)
// Offset: target buffer offset
int MemCopy(const uint8_t* nalu, int nalu_size, int offset = 0);
private:
size_t size_; // The total size of the buffer
int width_;
int height_;
JdkDma dma_; // DMA synchronization assistance
std::shared_ptr<JdkDmaBuffer> data; // Underlying DMA buffer
};
using JdkFramePtr = std::shared_ptr<JdkFrame>;
2.2JdkDma
& JdkDmaBuffer
: DMA Memory and Async Transfer Classes
class JdkDmaBuffer {
public:
// Construct and allocate DMA buffers
explicit JdkDmaBuffer(size_t size);
~JdkDmaBuffer();
// Return the mapped user space address
void* data() const;
// The whole filling value
void fill(uint8_t val);
// Get the physical address (need to call map_phys_addr first)
void map_phys_addr();
// Public field (read-only)
size_t m_size;
uint64_t m_phys;
};
class JdkDma {
public:
// Copy data asynchronously through the DMA engine
int Asyn(const JdkDmaBuffer& dst, const JdkDmaBuffer& src, size_t size);
// DMA replication between FDs
int Asyn(const int& dst_fd, const int& src_fd, size_t size);
};
2.3JdkCamera
: Camera Capture Interface
class JdkCamera {
public:
/**
* Create and open the V4L2 device
* @param device device path (e.g. "/dev/video0")
* @param width Expected collection width
* @param height Expected collection height
* @param pixfmt V4L2 pixel format (e.g. V4L2_PIX_FMT_NV12)
* The number of buffers requested by @param req_count (default 4)
* @return successfully returns JdkCameraPtr, otherwise return nullptr
*/
static std::shared_ptr<JdkCamera> create(const std::string& device,
int width,
int height,
__u32 pixfmt,
int req_count = 4);
/** Get a frame of image (blocking) */
JdkFramePtr getFrame();
~JdkCamera();
private:
explicit JdkCamera(const std::string& device);
class Impl;
std::unique_ptr<Impl> impl_;
};
using JdkCameraPtr = std::shared_ptr<JdkCamera>;
2.4JdkDecoder
: Hardware Video Decoder
class JdkDecoder {
public:
/**
* Initialize the hardware decoder
* @param width output resolution width
* @param height Output resolution height
* @param payload Enter the code stream type (see MppCodingType)
* @param Format output pixel format (default NV12)
*/
JdkDecoder(int width, int height,
MppCodingType payload,
MppPixelFormat Format = PIXEL_FORMAT_NV12);
~JdkDecoder();
/** Decoding, decoding from the encapsulated frame */
std::shared_ptr<JdkFrame> Decode(std::shared_ptr<JdkFrame> frame);
/** Decoding, decoding from bare NALU data */
std::shared_ptr<JdkFrame> Decode(const uint8_t* nalu, int nalu_size);
private:
int width_;
int height_;
MppCodingType payload_;
int format_;
int channel_id_;
MppVdecCtx* pVdecCtx = nullptr;
};
2.5JdkEncoder
: Hardware Video Encoder
class JdkEncoder {
public:
/**
* Initialize the hardware encoder
* @param width Input resolution width
* @param height Input resolution height
* @param payload output code stream type (see MppCodingType)
* @param Format Input pixel format (default NV12)
*/
JdkEncoder(int width, int height,
MppCodingType payload,
MppPixelFormat Format = PIXEL_FORMAT_NV12);
~JdkEncoder();
/** Encoding, encoding the original frame into a compressed code stream */
std::shared_ptr<JdkFrame> Encode(std::shared_ptr<JdkFrame> frame);
private:
int width_;
int height_;
MppCodingType payload_;
int format_;
int encoder_id_ = 0;
MppVencCtx* pVencCtx = nullptr;
};
2.6JdkDrm
: DRM-Based Video Output Interface
/** Supported pixel formats */
enum class PixelFmt : uint32_t {
NV12 = DRM_FORMAT_NV12
};
class JdkDrm {
public:
/**
* Open the DRM device and initialize it
* @param width display width
* @param height Display height
* @param stride row span (bytes)
* @param fmt pixel format
* @param device DRM device path (default "/dev/dri/card0")
*/
JdkDrm(int width, int height, int stride,
PixelFmt fmt = PixelFmt::NV12,
const char* device = "/dev/dri/card0");
~JdkDrm();
/** Send a frame to the DRM screen */
int sendFrame(std::shared_ptr<JdkFrame> frame);
/** Destroy the specified framebuffer */
void destroyFb(uint32_t fb, uint32_t handle);
/** Turn on the DRM device*/
int openCard(const char* dev);
/** Automatically choose the appropriate connector/crtc/plane*/
int pickConnectorCrtcPlane();
/**Import DMA FD as DRM framebuffer */
int importFb(int dma_fd, uint32_t& fb_id, uint32_t& handle);
private:
struct LastFB {
uint32_t fb_id;
uint32_t handle;
int dma_fd;
} last_;
};
2.7JdkV2D
: Image Processing (Resize, Convert, Overlay)
/** Supported target pixel format (please refer to the complete header file for the enumeration value) */
enum V2DFormat {
// For example: V2D_NV12, V2D_RGB888, ......
};
/** Rectangular area */
struct V2DRect {
int x, y, width, height;
};
class JdkV2D {
public:
JdkV2D() = default;
~JdkV2D() = default;
/**Format conversion*/
JdkFramePtr convert_format(const JdkFramePtr& input,
V2DFormat out_format);
/**Zoom */
JdkFramePtr resize(const JdkFramePtr& input,
int out_width, int out_height);
/** Scaling and format conversion at the same time */
JdkFramePtr resize_and_convert(const JdkFramePtr& input,
int out_width, int out_height,
V2DFormat out_format);
/** Fill the rectangular area */
bool fill_rect(const JdkFramePtr& image,
const V2DRect& rect,
uint32_t rgba_color);
/** Draw a rectangular border */
bool draw_rect(const JdkFramePtr& image,
const V2DRect& rect,
uint32_t rgba_color,
int thickness = 2);
/** Draw multiple rectangles */
bool draw_rects(const JdkFramePtr& image,
const std::vector<V2DRect>& rects,
uint32_t rgba_color,
int thickness = 2);
/** Image fusion (top overlay on bottom) */
JdkFramePtr blend(const JdkFramePtr& bottom,
const JdkFramePtr& top);
};
2.8dkVo
: Video Output (VO) Interface
class JdkVo {
public:
/**
* Initialize Vo output
* @param width output width
* @param height Output height
* @param Format Pixel Format (default NV12)
*/
JdkVo(int width, int height,
MppPixelFormat Format = PIXEL_FORMAT_NV12);
~JdkVo();
/** Send a frame to Vo hardware output */
int sendFrame(std::shared_ptr<JdkFrame> frame);
private:
int width_;
int height_;
MppPixelFormat format_;
int channel_id_;
MppVoCtx* pVoCtx = nullptr;
};
Appendix: Development Tips
- Please confirm whether the
DRM/VO/V4L2
module is supported according to the actual situation of the platform.- All resource interfaces use
std::shared_ptr
for automatic memory management.- It is recommended to use RAII mode to manage decoders, encoders and other module resources.
- In actual use, the return value should be handled incorrectly to avoid wild pointers or memory leakage.