hw: add draft rockchip-mpp hw video encoding
This commit is contained in:
82
external/mpp/mpp.go
vendored
Normal file
82
external/mpp/mpp.go
vendored
Normal file
@@ -0,0 +1,82 @@
|
||||
package mpp
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -I/usr/include/rockchip/mpp
|
||||
#cgo LDFLAGS: -L/usr/lib -lv4l2 -lrockchip_mpp
|
||||
#include "mpp.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <linux/videodev2.h>
|
||||
#include <rockchip/mpp_buffer.h>
|
||||
#include <rockchip/mpp_err.h>
|
||||
#include <rockchip/mpp_frame.h>
|
||||
#include <rockchip/mpp_packet.h>
|
||||
#include <rockchip/rk_mpi.h>
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
var mpp *MPP
|
||||
|
||||
type MPP struct {
|
||||
}
|
||||
|
||||
func New(device string, fps int, bitrate int) (*MPP, error) {
|
||||
cDevice := C.CString(device)
|
||||
defer C.free(unsafe.Pointer(cDevice))
|
||||
|
||||
if C.video_init(cDevice) != 0 {
|
||||
return nil, errors.New("failed to initialize video capture")
|
||||
}
|
||||
if C.mpp_init_wrapper(C.int(fps), C.int(bitrate)) != 0 {
|
||||
return nil, errors.New("failed to initialize MPP")
|
||||
}
|
||||
return &MPP{}, nil
|
||||
}
|
||||
|
||||
func GetSPS() ([]byte, error) {
|
||||
output := make([]byte, 64) // Allocate buffer for SPS
|
||||
var outputLen C.int
|
||||
|
||||
ret := C.get_sps((*C.uchar)(unsafe.Pointer(&output[0])), &outputLen)
|
||||
if ret != 0 {
|
||||
return nil, fmt.Errorf("failed to retrieve SPS: %v", ret)
|
||||
}
|
||||
|
||||
return output[:outputLen], nil
|
||||
}
|
||||
|
||||
func (m *MPP) CaptureAndEncode() ([]byte, error) {
|
||||
output := make([]byte, 1024*1024) // 1MB buffer
|
||||
var outputLen C.int
|
||||
|
||||
ret := C.capture_and_encode((*C.uchar)(unsafe.Pointer(&output[0])), &outputLen)
|
||||
if ret != 0 {
|
||||
return nil, errors.New("failed to capture and encode frame")
|
||||
}
|
||||
|
||||
return output[:outputLen], nil
|
||||
}
|
||||
|
||||
func (m *MPP) Close() {
|
||||
C.cleanup()
|
||||
}
|
||||
|
||||
func NewTest(device string) error {
|
||||
var err error
|
||||
mpp, err = New(device, 60, 10000*1000)
|
||||
return err
|
||||
}
|
||||
|
||||
func GetInstance() *MPP {
|
||||
return mpp
|
||||
}
|
||||
Reference in New Issue
Block a user