HDMI输入直至Yolov5识别全流程代码

This commit is contained in:
zhangpeng
2025-04-28 14:48:28 +08:00
commit 31940162fc
112 changed files with 222134 additions and 0 deletions

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,34 @@
import cv2
import numpy as np
cap = cv2.VideoCapture('/dev/video0', cv2.CAP_V4L2)
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('B', 'G', 'R', '3'))
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
ret, frame = cap.read()
if ret:
print(f"Captured frame shape: {frame.shape}")
# 解决形状问题reshape frames to (720, 1280, 3)
frame = frame.reshape((720, 1280, 3)) # ensure the correct shape if frame is 1D
# 确保数据是有效的8位无符号整型
if frame.dtype != np.uint8:
frame = frame.astype(np.uint8)
# 输出调试信息
print(f"Frame dtype: {frame.dtype}, Frame shape after reshape: {frame.shape}")
# 尝试保存为JPEG
cv2.imwrite("hdmi_frame.jpg", frame)
print("Frame saved as JPEG")
# 可选保存RAW数据
with open("hdmi_frame.raw", "wb") as f:
f.write(frame.tobytes())
print("RAW data saved")
else:
print("Capture failed")
cap.release()

View File

@@ -0,0 +1,24 @@
cap = cv2.VideoCapture('/dev/video10')
frames, loopTime, initTime = 0, time.time(), time.time()
fps = 0
while True:
frames += 1
ret, frame = cap.read()
if not ret:
break
# 现在应该可以直接使用frame不需要reshape
if frames % 30 == 0:
print("30帧平均帧率:\t", 30 / (time.time() - loopTime), "")
fps = 30 / (time.time() - loopTime)
loopTime = time.time()
cv2.putText(frame, "FPS: {:.2f}".format(fps), (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
cv2.imshow("MIPI Camera", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()

BIN
NpuYoloV5/04_yolov5/bus.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 177 KiB

View File

@@ -0,0 +1,41 @@
import cv2
import time
import numpy as np
def main():
cap = cv2.VideoCapture('/dev/video10')
frames, loopTime, initTime = 0, time.time(), time.time()
fps = 0
while True:
ret, frame = cap.read()
if not ret:
print("Failed to read frame")
break
# 检查帧数据是否需要重塑
if frame.size == 1280 * 720 * 3: # 检查是否为扁平化数据
frame = frame.reshape((720, 1280, 3)).astype(np.uint8)
else:
print(f"Unexpected frame shape: {frame.shape}")
# 显示帧
frames += 1
if frames % 30 == 0:
fps = 30 / (time.time() - loopTime)
print(f"30帧平均帧率: {fps:.2f}")
loopTime = time.time()
cv2.putText(frame, f"FPS: {fps:.2f}", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
cv2.imshow("MIPI Camera", frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
print(f"总平均帧率: {frames / (time.time() - initTime):.2f}")
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1 @@
bus.jpg

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 189 KiB

Binary file not shown.

319
NpuYoloV5/04_yolov5/test.py Normal file
View File

@@ -0,0 +1,319 @@
import os
import urllib
import traceback
import time
import sys
import numpy as np
import cv2
from rknn.api import RKNN
ONNX_MODEL = 'yolov5s.onnx'
RKNN_MODEL = 'yolov5s.rknn'
IMG_PATH = './bus.jpg'
DATASET = './dataset.txt'
QUANTIZE_ON = True
OBJ_THRESH = 0.25
NMS_THRESH = 0.45
IMG_SIZE = 640
CLASSES = ("person", "bicycle", "car", "motorbike ", "aeroplane ", "bus ", "train", "truck ", "boat", "traffic light",
"fire hydrant", "stop sign ", "parking meter", "bench", "bird", "cat", "dog ", "horse ", "sheep", "cow", "elephant",
"bear", "zebra ", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite",
"baseball bat", "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup", "fork", "knife ",
"spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza ", "donut", "cake", "chair", "sofa",
"pottedplant", "bed", "diningtable", "toilet ", "tvmonitor", "laptop ", "mouse ", "remote ", "keyboard ", "cell phone", "microwave ",
"oven ", "toaster", "sink", "refrigerator ", "book", "clock", "vase", "scissors ", "teddy bear ", "hair drier", "toothbrush ")
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def xywh2xyxy(x):
# Convert [x, y, w, h] to [x1, y1, x2, y2]
y = np.copy(x)
y[:, 0] = x[:, 0] - x[:, 2] / 2 # top left x
y[:, 1] = x[:, 1] - x[:, 3] / 2 # top left y
y[:, 2] = x[:, 0] + x[:, 2] / 2 # bottom right x
y[:, 3] = x[:, 1] + x[:, 3] / 2 # bottom right y
return y
def process(input, mask, anchors):
anchors = [anchors[i] for i in mask]
grid_h, grid_w = map(int, input.shape[0:2])
box_confidence = sigmoid(input[..., 4])
box_confidence = np.expand_dims(box_confidence, axis=-1)
box_class_probs = sigmoid(input[..., 5:])
box_xy = sigmoid(input[..., :2])*2 - 0.5
col = np.tile(np.arange(0, grid_w), grid_w).reshape(-1, grid_w)
row = np.tile(np.arange(0, grid_h).reshape(-1, 1), grid_h)
col = col.reshape(grid_h, grid_w, 1, 1).repeat(3, axis=-2)
row = row.reshape(grid_h, grid_w, 1, 1).repeat(3, axis=-2)
grid = np.concatenate((col, row), axis=-1)
box_xy += grid
box_xy *= int(IMG_SIZE/grid_h)
box_wh = pow(sigmoid(input[..., 2:4])*2, 2)
box_wh = box_wh * anchors
box = np.concatenate((box_xy, box_wh), axis=-1)
return box, box_confidence, box_class_probs
def filter_boxes(boxes, box_confidences, box_class_probs):
"""Filter boxes with box threshold. It's a bit different with origin yolov5 post process!
# Arguments
boxes: ndarray, boxes of objects.
box_confidences: ndarray, confidences of objects.
box_class_probs: ndarray, class_probs of objects.
# Returns
boxes: ndarray, filtered boxes.
classes: ndarray, classes for boxes.
scores: ndarray, scores for boxes.
"""
boxes = boxes.reshape(-1, 4)
box_confidences = box_confidences.reshape(-1)
box_class_probs = box_class_probs.reshape(-1, box_class_probs.shape[-1])
_box_pos = np.where(box_confidences >= OBJ_THRESH)
boxes = boxes[_box_pos]
box_confidences = box_confidences[_box_pos]
box_class_probs = box_class_probs[_box_pos]
class_max_score = np.max(box_class_probs, axis=-1)
classes = np.argmax(box_class_probs, axis=-1)
_class_pos = np.where(class_max_score >= OBJ_THRESH)
boxes = boxes[_class_pos]
classes = classes[_class_pos]
scores = (class_max_score* box_confidences)[_class_pos]
return boxes, classes, scores
def nms_boxes(boxes, scores):
"""Suppress non-maximal boxes.
# Arguments
boxes: ndarray, boxes of objects.
scores: ndarray, scores of objects.
# Returns
keep: ndarray, index of effective boxes.
"""
x = boxes[:, 0]
y = boxes[:, 1]
w = boxes[:, 2] - boxes[:, 0]
h = boxes[:, 3] - boxes[:, 1]
areas = w * h
order = scores.argsort()[::-1]
keep = []
while order.size > 0:
i = order[0]
keep.append(i)
xx1 = np.maximum(x[i], x[order[1:]])
yy1 = np.maximum(y[i], y[order[1:]])
xx2 = np.minimum(x[i] + w[i], x[order[1:]] + w[order[1:]])
yy2 = np.minimum(y[i] + h[i], y[order[1:]] + h[order[1:]])
w1 = np.maximum(0.0, xx2 - xx1 + 0.00001)
h1 = np.maximum(0.0, yy2 - yy1 + 0.00001)
inter = w1 * h1
ovr = inter / (areas[i] + areas[order[1:]] - inter)
inds = np.where(ovr <= NMS_THRESH)[0]
order = order[inds + 1]
keep = np.array(keep)
return keep
def yolov5_post_process(input_data):
masks = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
anchors = [[10, 13], [16, 30], [33, 23], [30, 61], [62, 45],
[59, 119], [116, 90], [156, 198], [373, 326]]
boxes, classes, scores = [], [], []
for input, mask in zip(input_data, masks):
b, c, s = process(input, mask, anchors)
b, c, s = filter_boxes(b, c, s)
boxes.append(b)
classes.append(c)
scores.append(s)
boxes = np.concatenate(boxes)
boxes = xywh2xyxy(boxes)
classes = np.concatenate(classes)
scores = np.concatenate(scores)
nboxes, nclasses, nscores = [], [], []
for c in set(classes):
inds = np.where(classes == c)
b = boxes[inds]
c = classes[inds]
s = scores[inds]
keep = nms_boxes(b, s)
nboxes.append(b[keep])
nclasses.append(c[keep])
nscores.append(s[keep])
if not nclasses and not nscores:
return None, None, None
boxes = np.concatenate(nboxes)
classes = np.concatenate(nclasses)
scores = np.concatenate(nscores)
return boxes, classes, scores
def draw(image, boxes, scores, classes):
"""Draw the boxes on the image.
# Argument:
image: original image.
boxes: ndarray, boxes of objects.
classes: ndarray, classes of objects.
scores: ndarray, scores of objects.
all_classes: all classes name.
"""
for box, score, cl in zip(boxes, scores, classes):
top, left, right, bottom = box
print('class: {}, score: {}'.format(CLASSES[cl], score))
print('box coordinate left,top,right,down: [{}, {}, {}, {}]'.format(top, left, right, bottom))
top = int(top)
left = int(left)
right = int(right)
bottom = int(bottom)
cv2.rectangle(image, (top, left), (right, bottom), (255, 0, 0), 2)
cv2.putText(image, '{0} {1:.2f}'.format(CLASSES[cl], score),
(top, left - 6),
cv2.FONT_HERSHEY_SIMPLEX,
0.6, (0, 0, 255), 2)
def letterbox(im, new_shape=(640, 640), color=(0, 0, 0)):
# Resize and pad image while meeting stride-multiple constraints
shape = im.shape[:2] # current shape [height, width]
if isinstance(new_shape, int):
new_shape = (new_shape, new_shape)
# Scale ratio (new / old)
r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
# Compute padding
ratio = r, r # width, height ratios
new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding
dw /= 2 # divide padding into 2 sides
dh /= 2
if shape[::-1] != new_unpad: # resize
im = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR)
top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border
return im, ratio, (dw, dh)
if __name__ == '__main__':
# Create RKNN object
rknn = RKNN(verbose=True)
# pre-process config
print('--> Config model')
rknn.config(mean_values=[[0, 0, 0]], std_values=[[255, 255, 255]], target_platform="rk3588")
print('done')
# Load ONNX model
print('--> Loading model')
ret = rknn.load_onnx(model=ONNX_MODEL)
if ret != 0:
print('Load model failed!')
exit(ret)
print('done')
# Build model
print('--> Building model')
ret = rknn.build(do_quantization=QUANTIZE_ON, dataset=DATASET)
if ret != 0:
print('Build model failed!')
exit(ret)
print('done')
# Export RKNN model
print('--> Export rknn model')
ret = rknn.export_rknn(RKNN_MODEL)
if ret != 0:
print('Export rknn model failed!')
exit(ret)
print('done')
# Init runtime environment
print('--> Init runtime environment')
ret = rknn.init_runtime(target="rk3588")
# ret = rknn.init_runtime('rk3566')
if ret != 0:
print('Init runtime environment failed!')
exit(ret)
print('done')
# Set inputs
img = cv2.imread(IMG_PATH)
# img, ratio, (dw, dh) = letterbox(img, new_shape=(IMG_SIZE, IMG_SIZE))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
# Inference
print('--> Running model')
outputs = rknn.inference(inputs=[img])
np.save('./onnx_yolov5_0.npy', outputs[0])
np.save('./onnx_yolov5_1.npy', outputs[1])
np.save('./onnx_yolov5_2.npy', outputs[2])
print('done')
# post process
input0_data = outputs[0]
input1_data = outputs[1]
input2_data = outputs[2]
input0_data = input0_data.reshape([3, -1]+list(input0_data.shape[-2:]))
input1_data = input1_data.reshape([3, -1]+list(input1_data.shape[-2:]))
input2_data = input2_data.reshape([3, -1]+list(input2_data.shape[-2:]))
input_data = list()
input_data.append(np.transpose(input0_data, (2, 3, 0, 1)))
input_data.append(np.transpose(input1_data, (2, 3, 0, 1)))
input_data.append(np.transpose(input2_data, (2, 3, 0, 1)))
boxes, classes, scores = yolov5_post_process(input_data)
img_1 = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
if boxes is not None:
draw(img_1, boxes, scores, classes)
cv2.imwrite('result.jpg', img_1)
print('Save results to result.jpg!')
# show output
# cv2.imshow("post process result", img_1)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
rknn.release()

View File

@@ -0,0 +1,292 @@
import os
import urllib
import traceback
import time
import sys
import numpy as np
import cv2
from rknn.api import RKNN
ONNX_MODEL = 'yolov5s.onnx'
RKNN_MODEL = 'yolov5s.rknn'
IMG_PATH = './bus.jpg'
DATASET = './dataset.txt'
QUANTIZE_ON = True
OBJ_THRESH = 0.25
NMS_THRESH = 0.45
IMG_SIZE = 640
CLASSES = ("person", "bicycle", "car", "motorbike ", "aeroplane ", "bus ", "train", "truck ", "boat", "traffic light",
"fire hydrant", "stop sign ", "parking meter", "bench", "bird", "cat", "dog ", "horse ", "sheep", "cow", "elephant",
"bear", "zebra ", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite",
"baseball bat", "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup", "fork", "knife ",
"spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza ", "donut", "cake", "chair", "sofa",
"pottedplant", "bed", "diningtable", "toilet ", "tvmonitor", "laptop ", "mouse ", "remote ", "keyboard ", "cell phone", "microwave ",
"oven ", "toaster", "sink", "refrigerator ", "book", "clock", "vase", "scissors ", "teddy bear ", "hair drier", "toothbrush ")
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def xywh2xyxy(x):
# Convert [x, y, w, h] to [x1, y1, x2, y2]
y = np.copy(x)
y[:, 0] = x[:, 0] - x[:, 2] / 2 # top left x
y[:, 1] = x[:, 1] - x[:, 3] / 2 # top left y
y[:, 2] = x[:, 0] + x[:, 2] / 2 # bottom right x
y[:, 3] = x[:, 1] + x[:, 3] / 2 # bottom right y
return y
def process(input, mask, anchors):
anchors = [anchors[i] for i in mask]
grid_h, grid_w = map(int, input.shape[0:2])
box_confidence = sigmoid(input[..., 4])
box_confidence = np.expand_dims(box_confidence, axis=-1)
box_class_probs = sigmoid(input[..., 5:])
box_xy = sigmoid(input[..., :2])*2 - 0.5
col = np.tile(np.arange(0, grid_w), grid_w).reshape(-1, grid_w)
row = np.tile(np.arange(0, grid_h).reshape(-1, 1), grid_h)
col = col.reshape(grid_h, grid_w, 1, 1).repeat(3, axis=-2)
row = row.reshape(grid_h, grid_w, 1, 1).repeat(3, axis=-2)
grid = np.concatenate((col, row), axis=-1)
box_xy += grid
box_xy *= int(IMG_SIZE/grid_h)
box_wh = pow(sigmoid(input[..., 2:4])*2, 2)
box_wh = box_wh * anchors
box = np.concatenate((box_xy, box_wh), axis=-1)
return box, box_confidence, box_class_probs
def filter_boxes(boxes, box_confidences, box_class_probs):
"""Filter boxes with box threshold. It's a bit different with origin yolov5 post process!
# Arguments
boxes: ndarray, boxes of objects.
box_confidences: ndarray, confidences of objects.
box_class_probs: ndarray, class_probs of objects.
# Returns
boxes: ndarray, filtered boxes.
classes: ndarray, classes for boxes.
scores: ndarray, scores for boxes.
"""
boxes = boxes.reshape(-1, 4)
box_confidences = box_confidences.reshape(-1)
box_class_probs = box_class_probs.reshape(-1, box_class_probs.shape[-1])
_box_pos = np.where(box_confidences >= OBJ_THRESH)
boxes = boxes[_box_pos]
box_confidences = box_confidences[_box_pos]
box_class_probs = box_class_probs[_box_pos]
class_max_score = np.max(box_class_probs, axis=-1)
classes = np.argmax(box_class_probs, axis=-1)
_class_pos = np.where(class_max_score >= OBJ_THRESH)
boxes = boxes[_class_pos]
classes = classes[_class_pos]
scores = (class_max_score* box_confidences)[_class_pos]
return boxes, classes, scores
def nms_boxes(boxes, scores):
"""Suppress non-maximal boxes.
# Arguments
boxes: ndarray, boxes of objects.
scores: ndarray, scores of objects.
# Returns
keep: ndarray, index of effective boxes.
"""
x = boxes[:, 0]
y = boxes[:, 1]
w = boxes[:, 2] - boxes[:, 0]
h = boxes[:, 3] - boxes[:, 1]
areas = w * h
order = scores.argsort()[::-1]
keep = []
while order.size > 0:
i = order[0]
keep.append(i)
xx1 = np.maximum(x[i], x[order[1:]])
yy1 = np.maximum(y[i], y[order[1:]])
xx2 = np.minimum(x[i] + w[i], x[order[1:]] + w[order[1:]])
yy2 = np.minimum(y[i] + h[i], y[order[1:]] + h[order[1:]])
w1 = np.maximum(0.0, xx2 - xx1 + 0.00001)
h1 = np.maximum(0.0, yy2 - yy1 + 0.00001)
inter = w1 * h1
ovr = inter / (areas[i] + areas[order[1:]] - inter)
inds = np.where(ovr <= NMS_THRESH)[0]
order = order[inds + 1]
keep = np.array(keep)
return keep
def yolov5_post_process(input_data):
masks = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
anchors = [[10, 13], [16, 30], [33, 23], [30, 61], [62, 45],
[59, 119], [116, 90], [156, 198], [373, 326]]
boxes, classes, scores = [], [], []
for input, mask in zip(input_data, masks):
b, c, s = process(input, mask, anchors)
b, c, s = filter_boxes(b, c, s)
boxes.append(b)
classes.append(c)
scores.append(s)
boxes = np.concatenate(boxes)
boxes = xywh2xyxy(boxes)
classes = np.concatenate(classes)
scores = np.concatenate(scores)
nboxes, nclasses, nscores = [], [], []
for c in set(classes):
inds = np.where(classes == c)
b = boxes[inds]
c = classes[inds]
s = scores[inds]
keep = nms_boxes(b, s)
nboxes.append(b[keep])
nclasses.append(c[keep])
nscores.append(s[keep])
if not nclasses and not nscores:
return None, None, None
boxes = np.concatenate(nboxes)
classes = np.concatenate(nclasses)
scores = np.concatenate(nscores)
return boxes, classes, scores
def draw(image, boxes, scores, classes):
"""Draw the boxes on the image.
# Argument:
image: original image.
boxes: ndarray, boxes of objects.
classes: ndarray, classes of objects.
scores: ndarray, scores of objects.
all_classes: all classes name.
"""
for box, score, cl in zip(boxes, scores, classes):
top, left, right, bottom = box
print('class: {}, score: {}'.format(CLASSES[cl], score))
print('box coordinate left,top,right,down: [{}, {}, {}, {}]'.format(top, left, right, bottom))
top = int(top)
left = int(left)
right = int(right)
bottom = int(bottom)
cv2.rectangle(image, (top, left), (right, bottom), (255, 0, 0), 2)
cv2.putText(image, '{0} {1:.2f}'.format(CLASSES[cl], score),
(top, left - 6),
cv2.FONT_HERSHEY_SIMPLEX,
0.6, (0, 0, 255), 2)
def letterbox(im, new_shape=(640, 640), color=(0, 0, 0)):
# Resize and pad image while meeting stride-multiple constraints
shape = im.shape[:2] # current shape [height, width]
if isinstance(new_shape, int):
new_shape = (new_shape, new_shape)
# Scale ratio (new / old)
r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
# Compute padding
ratio = r, r # width, height ratios
new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding
dw /= 2 # divide padding into 2 sides
dh /= 2
if shape[::-1] != new_unpad: # resize
im = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR)
top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border
return im, ratio, (dw, dh)
if __name__ == '__main__':
# Create RKNN object
rknn = RKNN(verbose=True)
rknn.load_rknn(path="./yolov5s.rknn")
# Init runtime environment
print('--> Init runtime environment')
ret = rknn.init_runtime(target="rk3588")
# ret = rknn.init_runtime('rk3566')
if ret != 0:
print('Init runtime environment failed!')
exit(ret)
print('done')
# Set inputs
img = cv2.imread(IMG_PATH)
# img, ratio, (dw, dh) = letterbox(img, new_shape=(IMG_SIZE, IMG_SIZE))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
# Inference
print('--> Running model')
outputs = rknn.inference(inputs=[img])
np.save('./onnx_yolov5_0.npy', outputs[0])
np.save('./onnx_yolov5_1.npy', outputs[1])
np.save('./onnx_yolov5_2.npy', outputs[2])
print('done')
# post process
input0_data = outputs[0]
input1_data = outputs[1]
input2_data = outputs[2]
input0_data = input0_data.reshape([3, -1]+list(input0_data.shape[-2:]))
input1_data = input1_data.reshape([3, -1]+list(input1_data.shape[-2:]))
input2_data = input2_data.reshape([3, -1]+list(input2_data.shape[-2:]))
input_data = list()
input_data.append(np.transpose(input0_data, (2, 3, 0, 1)))
input_data.append(np.transpose(input1_data, (2, 3, 0, 1)))
input_data.append(np.transpose(input2_data, (2, 3, 0, 1)))
boxes, classes, scores = yolov5_post_process(input_data)
img_1 = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
if boxes is not None:
draw(img_1, boxes, scores, classes)
cv2.imwrite('result.jpg', img_1)
print('Save results to result.jpg!')
# show output
# cv2.imshow("post process result", img_1)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
rknn.release()

View File

@@ -0,0 +1,292 @@
import os
import urllib
import traceback
import time
import sys
import numpy as np
import cv2
from rknnlite.api import RKNNLite
ONNX_MODEL = 'yolov5s.onnx'
RKNN_MODEL = 'yolov5s.rknn'
IMG_PATH = './bus.jpg'
DATASET = './dataset.txt'
QUANTIZE_ON = True
OBJ_THRESH = 0.25
NMS_THRESH = 0.45
IMG_SIZE = 640
CLASSES = ("person", "bicycle", "car", "motorbike ", "aeroplane ", "bus ", "train", "truck ", "boat", "traffic light",
"fire hydrant", "stop sign ", "parking meter", "bench", "bird", "cat", "dog ", "horse ", "sheep", "cow", "elephant",
"bear", "zebra ", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite",
"baseball bat", "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup", "fork", "knife ",
"spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza ", "donut", "cake", "chair", "sofa",
"pottedplant", "bed", "diningtable", "toilet ", "tvmonitor", "laptop ", "mouse ", "remote ", "keyboard ", "cell phone", "microwave ",
"oven ", "toaster", "sink", "refrigerator ", "book", "clock", "vase", "scissors ", "teddy bear ", "hair drier", "toothbrush ")
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def xywh2xyxy(x):
# Convert [x, y, w, h] to [x1, y1, x2, y2]
y = np.copy(x)
y[:, 0] = x[:, 0] - x[:, 2] / 2 # top left x
y[:, 1] = x[:, 1] - x[:, 3] / 2 # top left y
y[:, 2] = x[:, 0] + x[:, 2] / 2 # bottom right x
y[:, 3] = x[:, 1] + x[:, 3] / 2 # bottom right y
return y
def process(input, mask, anchors):
anchors = [anchors[i] for i in mask]
grid_h, grid_w = map(int, input.shape[0:2])
box_confidence = sigmoid(input[..., 4])
box_confidence = np.expand_dims(box_confidence, axis=-1)
box_class_probs = sigmoid(input[..., 5:])
box_xy = sigmoid(input[..., :2])*2 - 0.5
col = np.tile(np.arange(0, grid_w), grid_w).reshape(-1, grid_w)
row = np.tile(np.arange(0, grid_h).reshape(-1, 1), grid_h)
col = col.reshape(grid_h, grid_w, 1, 1).repeat(3, axis=-2)
row = row.reshape(grid_h, grid_w, 1, 1).repeat(3, axis=-2)
grid = np.concatenate((col, row), axis=-1)
box_xy += grid
box_xy *= int(IMG_SIZE/grid_h)
box_wh = pow(sigmoid(input[..., 2:4])*2, 2)
box_wh = box_wh * anchors
box = np.concatenate((box_xy, box_wh), axis=-1)
return box, box_confidence, box_class_probs
def filter_boxes(boxes, box_confidences, box_class_probs):
"""Filter boxes with box threshold. It's a bit different with origin yolov5 post process!
# Arguments
boxes: ndarray, boxes of objects.
box_confidences: ndarray, confidences of objects.
box_class_probs: ndarray, class_probs of objects.
# Returns
boxes: ndarray, filtered boxes.
classes: ndarray, classes for boxes.
scores: ndarray, scores for boxes.
"""
boxes = boxes.reshape(-1, 4)
box_confidences = box_confidences.reshape(-1)
box_class_probs = box_class_probs.reshape(-1, box_class_probs.shape[-1])
_box_pos = np.where(box_confidences >= OBJ_THRESH)
boxes = boxes[_box_pos]
box_confidences = box_confidences[_box_pos]
box_class_probs = box_class_probs[_box_pos]
class_max_score = np.max(box_class_probs, axis=-1)
classes = np.argmax(box_class_probs, axis=-1)
_class_pos = np.where(class_max_score >= OBJ_THRESH)
boxes = boxes[_class_pos]
classes = classes[_class_pos]
scores = (class_max_score* box_confidences)[_class_pos]
return boxes, classes, scores
def nms_boxes(boxes, scores):
"""Suppress non-maximal boxes.
# Arguments
boxes: ndarray, boxes of objects.
scores: ndarray, scores of objects.
# Returns
keep: ndarray, index of effective boxes.
"""
x = boxes[:, 0]
y = boxes[:, 1]
w = boxes[:, 2] - boxes[:, 0]
h = boxes[:, 3] - boxes[:, 1]
areas = w * h
order = scores.argsort()[::-1]
keep = []
while order.size > 0:
i = order[0]
keep.append(i)
xx1 = np.maximum(x[i], x[order[1:]])
yy1 = np.maximum(y[i], y[order[1:]])
xx2 = np.minimum(x[i] + w[i], x[order[1:]] + w[order[1:]])
yy2 = np.minimum(y[i] + h[i], y[order[1:]] + h[order[1:]])
w1 = np.maximum(0.0, xx2 - xx1 + 0.00001)
h1 = np.maximum(0.0, yy2 - yy1 + 0.00001)
inter = w1 * h1
ovr = inter / (areas[i] + areas[order[1:]] - inter)
inds = np.where(ovr <= NMS_THRESH)[0]
order = order[inds + 1]
keep = np.array(keep)
return keep
def yolov5_post_process(input_data):
masks = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
anchors = [[10, 13], [16, 30], [33, 23], [30, 61], [62, 45],
[59, 119], [116, 90], [156, 198], [373, 326]]
boxes, classes, scores = [], [], []
for input, mask in zip(input_data, masks):
b, c, s = process(input, mask, anchors)
b, c, s = filter_boxes(b, c, s)
boxes.append(b)
classes.append(c)
scores.append(s)
boxes = np.concatenate(boxes)
boxes = xywh2xyxy(boxes)
classes = np.concatenate(classes)
scores = np.concatenate(scores)
nboxes, nclasses, nscores = [], [], []
for c in set(classes):
inds = np.where(classes == c)
b = boxes[inds]
c = classes[inds]
s = scores[inds]
keep = nms_boxes(b, s)
nboxes.append(b[keep])
nclasses.append(c[keep])
nscores.append(s[keep])
if not nclasses and not nscores:
return None, None, None
boxes = np.concatenate(nboxes)
classes = np.concatenate(nclasses)
scores = np.concatenate(nscores)
return boxes, classes, scores
def draw(image, boxes, scores, classes):
"""Draw the boxes on the image.
# Argument:
image: original image.
boxes: ndarray, boxes of objects.
classes: ndarray, classes of objects.
scores: ndarray, scores of objects.
all_classes: all classes name.
"""
for box, score, cl in zip(boxes, scores, classes):
top, left, right, bottom = box
print('class: {}, score: {}'.format(CLASSES[cl], score))
print('box coordinate left,top,right,down: [{}, {}, {}, {}]'.format(top, left, right, bottom))
top = int(top)
left = int(left)
right = int(right)
bottom = int(bottom)
cv2.rectangle(image, (top, left), (right, bottom), (255, 0, 0), 2)
cv2.putText(image, '{0} {1:.2f}'.format(CLASSES[cl], score),
(top, left - 6),
cv2.FONT_HERSHEY_SIMPLEX,
0.6, (0, 0, 255), 2)
def letterbox(im, new_shape=(640, 640), color=(0, 0, 0)):
# Resize and pad image while meeting stride-multiple constraints
shape = im.shape[:2] # current shape [height, width]
if isinstance(new_shape, int):
new_shape = (new_shape, new_shape)
# Scale ratio (new / old)
r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
# Compute padding
ratio = r, r # width, height ratios
new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding
dw /= 2 # divide padding into 2 sides
dh /= 2
if shape[::-1] != new_unpad: # resize
im = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR)
top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border
return im, ratio, (dw, dh)
if __name__ == '__main__':
# Create RKNN object
rknn = RKNNLite(verbose=True)
rknn.load_rknn(path="./yolov5s.rknn")
# Init runtime environment
print('--> Init runtime environment')
ret = rknn.init_runtime()
# ret = rknn.init_runtime('rk3566')
if ret != 0:
print('Init runtime environment failed!')
exit(ret)
print('done')
# Set inputs
img = cv2.imread(IMG_PATH)
# img, ratio, (dw, dh) = letterbox(img, new_shape=(IMG_SIZE, IMG_SIZE))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
# Inference
print('--> Running model')
outputs = rknn.inference(inputs=[img])
np.save('./onnx_yolov5_0.npy', outputs[0])
np.save('./onnx_yolov5_1.npy', outputs[1])
np.save('./onnx_yolov5_2.npy', outputs[2])
print('done')
# post process
input0_data = outputs[0]
input1_data = outputs[1]
input2_data = outputs[2]
input0_data = input0_data.reshape([3, -1]+list(input0_data.shape[-2:]))
input1_data = input1_data.reshape([3, -1]+list(input1_data.shape[-2:]))
input2_data = input2_data.reshape([3, -1]+list(input2_data.shape[-2:]))
input_data = list()
input_data.append(np.transpose(input0_data, (2, 3, 0, 1)))
input_data.append(np.transpose(input1_data, (2, 3, 0, 1)))
input_data.append(np.transpose(input2_data, (2, 3, 0, 1)))
boxes, classes, scores = yolov5_post_process(input_data)
img_1 = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
if boxes is not None:
draw(img_1, boxes, scores, classes)
cv2.imwrite('result.jpg', img_1)
print('Save results to result.jpg!')
# show output
# cv2.imshow("post process result", img_1)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
rknn.release()

View File

@@ -0,0 +1,312 @@
import os
import urllib
import traceback
import time
import sys
import numpy as np
import cv2
from rknnlite.api import RKNNLite
ONNX_MODEL = 'yolov5s.onnx'
RKNN_MODEL = 'yolov5s.rknn'
IMG_PATH = './bus.jpg'
DATASET = './dataset.txt'
QUANTIZE_ON = True
OBJ_THRESH = 0.25
NMS_THRESH = 0.45
IMG_SIZE = 640
CLASSES = ("person", "bicycle", "car", "motorbike ", "aeroplane ", "bus ", "train", "truck ", "boat", "traffic light",
"fire hydrant", "stop sign ", "parking meter", "bench", "bird", "cat", "dog ", "horse ", "sheep", "cow", "elephant",
"bear", "zebra ", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite",
"baseball bat", "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup", "fork", "knife ",
"spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza ", "donut", "cake", "chair", "sofa",
"pottedplant", "bed", "diningtable", "toilet ", "tvmonitor", "laptop ", "mouse ", "remote ", "keyboard ", "cell phone", "microwave ",
"oven ", "toaster", "sink", "refrigerator ", "book", "clock", "vase", "scissors ", "teddy bear ", "hair drier", "toothbrush ")
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def xywh2xyxy(x):
# Convert [x, y, w, h] to [x1, y1, x2, y2]
y = np.copy(x)
y[:, 0] = x[:, 0] - x[:, 2] / 2 # top left x
y[:, 1] = x[:, 1] - x[:, 3] / 2 # top left y
y[:, 2] = x[:, 0] + x[:, 2] / 2 # bottom right x
y[:, 3] = x[:, 1] + x[:, 3] / 2 # bottom right y
return y
def process(input, mask, anchors):
anchors = [anchors[i] for i in mask]
grid_h, grid_w = map(int, input.shape[0:2])
box_confidence = sigmoid(input[..., 4])
box_confidence = np.expand_dims(box_confidence, axis=-1)
box_class_probs = sigmoid(input[..., 5:])
box_xy = sigmoid(input[..., :2])*2 - 0.5
col = np.tile(np.arange(0, grid_w), grid_w).reshape(-1, grid_w)
row = np.tile(np.arange(0, grid_h).reshape(-1, 1), grid_h)
col = col.reshape(grid_h, grid_w, 1, 1).repeat(3, axis=-2)
row = row.reshape(grid_h, grid_w, 1, 1).repeat(3, axis=-2)
grid = np.concatenate((col, row), axis=-1)
box_xy += grid
box_xy *= int(IMG_SIZE/grid_h)
box_wh = pow(sigmoid(input[..., 2:4])*2, 2)
box_wh = box_wh * anchors
box = np.concatenate((box_xy, box_wh), axis=-1)
return box, box_confidence, box_class_probs
def filter_boxes(boxes, box_confidences, box_class_probs):
"""Filter boxes with box threshold. It's a bit different with origin yolov5 post process!
# Arguments
boxes: ndarray, boxes of objects.
box_confidences: ndarray, confidences of objects.
box_class_probs: ndarray, class_probs of objects.
# Returns
boxes: ndarray, filtered boxes.
classes: ndarray, classes for boxes.
scores: ndarray, scores for boxes.
"""
boxes = boxes.reshape(-1, 4)
box_confidences = box_confidences.reshape(-1)
box_class_probs = box_class_probs.reshape(-1, box_class_probs.shape[-1])
_box_pos = np.where(box_confidences >= OBJ_THRESH)
boxes = boxes[_box_pos]
box_confidences = box_confidences[_box_pos]
box_class_probs = box_class_probs[_box_pos]
class_max_score = np.max(box_class_probs, axis=-1)
classes = np.argmax(box_class_probs, axis=-1)
_class_pos = np.where(class_max_score >= OBJ_THRESH)
boxes = boxes[_class_pos]
classes = classes[_class_pos]
scores = (class_max_score* box_confidences)[_class_pos]
return boxes, classes, scores
def nms_boxes(boxes, scores):
"""Suppress non-maximal boxes.
# Arguments
boxes: ndarray, boxes of objects.
scores: ndarray, scores of objects.
# Returns
keep: ndarray, index of effective boxes.
"""
x = boxes[:, 0]
y = boxes[:, 1]
w = boxes[:, 2] - boxes[:, 0]
h = boxes[:, 3] - boxes[:, 1]
areas = w * h
order = scores.argsort()[::-1]
keep = []
while order.size > 0:
i = order[0]
keep.append(i)
xx1 = np.maximum(x[i], x[order[1:]])
yy1 = np.maximum(y[i], y[order[1:]])
xx2 = np.minimum(x[i] + w[i], x[order[1:]] + w[order[1:]])
yy2 = np.minimum(y[i] + h[i], y[order[1:]] + h[order[1:]])
w1 = np.maximum(0.0, xx2 - xx1 + 0.00001)
h1 = np.maximum(0.0, yy2 - yy1 + 0.00001)
inter = w1 * h1
ovr = inter / (areas[i] + areas[order[1:]] - inter)
inds = np.where(ovr <= NMS_THRESH)[0]
order = order[inds + 1]
keep = np.array(keep)
return keep
def yolov5_post_process(input_data):
masks = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
anchors = [[10, 13], [16, 30], [33, 23], [30, 61], [62, 45],
[59, 119], [116, 90], [156, 198], [373, 326]]
boxes, classes, scores = [], [], []
for input, mask in zip(input_data, masks):
b, c, s = process(input, mask, anchors)
b, c, s = filter_boxes(b, c, s)
boxes.append(b)
classes.append(c)
scores.append(s)
boxes = np.concatenate(boxes)
boxes = xywh2xyxy(boxes)
classes = np.concatenate(classes)
scores = np.concatenate(scores)
nboxes, nclasses, nscores = [], [], []
for c in set(classes):
inds = np.where(classes == c)
b = boxes[inds]
c = classes[inds]
s = scores[inds]
keep = nms_boxes(b, s)
nboxes.append(b[keep])
nclasses.append(c[keep])
nscores.append(s[keep])
if not nclasses and not nscores:
return None, None, None
boxes = np.concatenate(nboxes)
classes = np.concatenate(nclasses)
scores = np.concatenate(nscores)
return boxes, classes, scores
def draw(image, boxes, scores, classes):
"""Draw the boxes on the image.
# Argument:
image: original image.
boxes: ndarray, boxes of objects.
classes: ndarray, classes of objects.
scores: ndarray, scores of objects.
all_classes: all classes name.
"""
for box, score, cl in zip(boxes, scores, classes):
top, left, right, bottom = box
# print('class: {}, score: {}'.format(CLASSES[cl], score))
# print('box coordinate left,top,right,down: [{}, {}, {}, {}]'.format(top, left, right, bottom))
top = int(top)
left = int(left)
right = int(right)
bottom = int(bottom)
cv2.rectangle(image, (top, left), (right, bottom), (255, 0, 0), 2)
cv2.putText(image, '{0} {1:.2f}'.format(CLASSES[cl], score),
(top, left - 6),
cv2.FONT_HERSHEY_SIMPLEX,
0.6, (0, 0, 255), 2)
def letterbox(im, new_shape=(IMG_SIZE, IMG_SIZE), color=(0, 0, 0)):
# Resize and pad image while meeting stride-multiple constraints
shape = im.shape[:2] # current shape [height, width]
if isinstance(new_shape, int):
new_shape = (new_shape, new_shape)
# Scale ratio (new / old)
r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
# Compute padding
ratio = r, r # width, height ratios
new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding
dw /= 2 # divide padding into 2 sides
dh /= 2
if shape[::-1] != new_unpad: # resize
im = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR)
top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border
return im, ratio, (dw, dh)
if __name__ == '__main__':
# Create RKNN object
rknn = RKNNLite(verbose=False)
rknn.load_rknn(path="./yolov5s.rknn")
# Init runtime environment
print('--> Init runtime environment')
ret = rknn.init_runtime(core_mask=7)
# ret = rknn.init_runtime('rk3566')
if ret != 0:
print('Init runtime environment failed!')
exit(ret)
print('done')
# # Set inputs
# img = cv2.imread(IMG_PATH)
# # img, ratio, (dw, dh) = letterbox(img, new_shape=(IMG_SIZE, IMG_SIZE))
# img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
cap = cv2.VideoCapture('test.mp4')
frames, loopTime, initTime = 0, time.time(), time.time()
fps = 0
while True:
frames += 1
# 从摄像头捕获帧
ret, img = cap.read()
# 重塑视频帧,只有自己虚拟的才需要
# img = img.reshape((720, 1280, 3)).astype(np.uint8)
# 如果捕获到帧,则显示它
if ret:
# Inference
# print('--> Running model')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
outputs = rknn.inference(inputs=[img])
# np.save('./onnx_yolov5_0.npy', outputs[0])
# np.save('./onnx_yolov5_1.npy', outputs[1])
# np.save('./onnx_yolov5_2.npy', outputs[2])
# print('done')
# post process
input0_data = outputs[0]
input1_data = outputs[1]
input2_data = outputs[2]
input0_data = input0_data.reshape([3, -1]+list(input0_data.shape[-2:]))
input1_data = input1_data.reshape([3, -1]+list(input1_data.shape[-2:]))
input2_data = input2_data.reshape([3, -1]+list(input2_data.shape[-2:]))
input_data = list()
input_data.append(np.transpose(input0_data, (2, 3, 0, 1)))
input_data.append(np.transpose(input1_data, (2, 3, 0, 1)))
input_data.append(np.transpose(input2_data, (2, 3, 0, 1)))
boxes, classes, scores = yolov5_post_process(input_data)
img_1 = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
if boxes is not None:
draw(img_1, boxes, scores, classes)
if frames % 30 == 0:
print("30帧平均帧率:\t", 30 / (time.time() - loopTime), "")
fps = 30 / (time.time() - loopTime)
loopTime = time.time()
cv2.putText(img_1, "FPS: {:.2f}".format(fps), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255),
2) # 在图像上显示帧率
cv2.imshow("MIPI Camera", img_1)
# 按下'q'键退出循环
if cv2.waitKey(1) & 0xFF == ord("q"):
break
# show output
# cv2.imshow("post process result", img_1)
cv2.waitKey(0)
cv2.destroyAllWindows()
rknn.release()

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,162 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
*.mp4

View File

@@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View File

@@ -0,0 +1,36 @@
# 简介
* 使用多线程异步操作rknn模型, 提高rk3588/rk3588s的NPU使用率, 进而提高推理帧数(rk3568之类修改后应该也能使用, 但是作者本人并没有rk3568开发板......)
* 此分支使用模型[yolov5s_relu_tk2_RK3588_i8.rknn](https://github.com/airockchip/rknn_model_zoo), 将yolov5s模型的激活函数silu修改为为relu,在损失一点精度的情况下获得较大性能提升,详情见于[rknn_model_zoo](https://github.com/airockchip/rknn_model_zoo/tree/main/models/CV/object_detection/yolo)
* 此项目的[c++](https://github.com/leafqycc/rknn-cpp-Multithreading)实现
# 更新说明
*
# 使用说明
### 演示
* 将仓库拉取至本地, 并将Releases中的演示视频放于项目根目录下, 运行main.py查看演示示例
* 切换至root用户运行performance.sh可以进行定频操作(约等于开启性能模式)
* 运行rkcat.sh可以查看当前温度与NPU占用
### 部署应用
* 修改main.py下的modelPath为你自己的模型所在路径
* 修改main.py下的cap为你想要运行的视频/摄像头
* 修改main.py下的TPEs为你想要的线程数, 具体可参考下表
* 修改func.py为你自己需要的推理函数, 具体可查看myFunc函数
# 多线程模型帧率测试
* 使用performance.sh进行CPU/NPU定频尽量减少误差
* 测试模型为[yolov5s_relu_tk2_RK3588_i8.rknn](https://github.com/airockchip/rknn_model_zoo)
* 测试视频见于Releases
| 模型\线程数 | 1 | 2 | 3 | 4 | 5 | 6 |
| ---- | ---- | ---- | ---- | ---- | ---- | ---- |
| yolov5s | 27.4491 | 49.0747 | 65.3673 | 63.3204 | 71.8407 | 72.0590 |
# 补充
* 多线程下CPU, NPU占用较高, **核心温度相应增高**, 请做好散热。推荐开1, 2, 3线程, 实测小铜片散热下运行三分钟温度约为56°, 64°, 69°
# Acknowledgements
* https://github.com/ultralytics/yolov5
* https://github.com/rockchip-linux/rknn-toolkit2
* https://github.com/airockchip/rknn_model_zoo

View File

@@ -0,0 +1,221 @@
#以下代码改自https://github.com/rockchip-linux/rknn-toolkit2/tree/master/examples/onnx/yolov5
import cv2
import numpy as np
OBJ_THRESH, NMS_THRESH, IMG_SIZE = 0.25, 0.45, 640
CLASSES = ("person", "bicycle", "car", "motorbike ", "aeroplane ", "bus ", "train", "truck ", "boat", "traffic light",
"fire hydrant", "stop sign ", "parking meter", "bench", "bird", "cat", "dog ", "horse ", "sheep", "cow", "elephant",
"bear", "zebra ", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite",
"baseball bat", "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup", "fork", "knife ",
"spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza ", "donut", "cake", "chair", "sofa",
"pottedplant", "bed", "diningtable", "toilet ", "tvmonitor", "laptop ", "mouse ", "remote ", "keyboard ", "cell phone", "microwave ",
"oven ", "toaster", "sink", "refrigerator ", "book", "clock", "vase", "scissors ", "teddy bear ", "hair drier", "toothbrush ")
#
# def sigmoid(x):
# return 1 / (1 + np.exp(-x))
def xywh2xyxy(x):
# Convert [x, y, w, h] to [x1, y1, x2, y2]
y = np.copy(x)
y[:, 0] = x[:, 0] - x[:, 2] / 2 # top left x
y[:, 1] = x[:, 1] - x[:, 3] / 2 # top left y
y[:, 2] = x[:, 0] + x[:, 2] / 2 # bottom right x
y[:, 3] = x[:, 1] + x[:, 3] / 2 # bottom right y
return y
def process(input, mask, anchors):
anchors = [anchors[i] for i in mask]
grid_h, grid_w = map(int, input.shape[0:2])
box_confidence = input[..., 4]
box_confidence = np.expand_dims(box_confidence, axis=-1)
box_class_probs = input[..., 5:]
box_xy = input[..., :2] *2 - 0.5
col = np.tile(np.arange(0, grid_w), grid_w).reshape(-1, grid_w)
row = np.tile(np.arange(0, grid_h).reshape(-1, 1), grid_h)
col = col.reshape(grid_h, grid_w, 1, 1).repeat(3, axis=-2)
row = row.reshape(grid_h, grid_w, 1, 1).repeat(3, axis=-2)
grid = np.concatenate((col, row), axis=-1)
box_xy += grid
box_xy *= int(IMG_SIZE/grid_h)
box_wh = pow(input[..., 2:4] *2, 2)
box_wh = box_wh * anchors
return np.concatenate((box_xy, box_wh), axis=-1), box_confidence, box_class_probs
def filter_boxes(boxes, box_confidences, box_class_probs):
"""Filter boxes with box threshold. It's a bit different with origin yolov5 post process!
# Arguments
boxes: ndarray, boxes of objects.
box_confidences: ndarray, confidences of objects.
box_class_probs: ndarray, class_probs of objects.
# Returns
boxes: ndarray, filtered boxes.
classes: ndarray, classes for boxes.
scores: ndarray, scores for boxes.
"""
boxes = boxes.reshape(-1, 4)
box_confidences = box_confidences.reshape(-1)
box_class_probs = box_class_probs.reshape(-1, box_class_probs.shape[-1])
_box_pos = np.where(box_confidences >= OBJ_THRESH)
boxes = boxes[_box_pos]
box_confidences = box_confidences[_box_pos]
box_class_probs = box_class_probs[_box_pos]
class_max_score = np.max(box_class_probs, axis=-1)
classes = np.argmax(box_class_probs, axis=-1)
_class_pos = np.where(class_max_score >= OBJ_THRESH)
return boxes[_class_pos], classes[_class_pos], (class_max_score * box_confidences)[_class_pos]
def nms_boxes(boxes, scores):
"""Suppress non-maximal boxes.
# Arguments
boxes: ndarray, boxes of objects.
scores: ndarray, scores of objects.
# Returns
keep: ndarray, index of effective boxes.
"""
x = boxes[:, 0]
y = boxes[:, 1]
w = boxes[:, 2] - boxes[:, 0]
h = boxes[:, 3] - boxes[:, 1]
areas = w * h
order = scores.argsort()[::-1]
keep = []
while order.size > 0:
i = order[0]
keep.append(i)
xx1 = np.maximum(x[i], x[order[1:]])
yy1 = np.maximum(y[i], y[order[1:]])
xx2 = np.minimum(x[i] + w[i], x[order[1:]] + w[order[1:]])
yy2 = np.minimum(y[i] + h[i], y[order[1:]] + h[order[1:]])
w1 = np.maximum(0.0, xx2 - xx1 + 0.00001)
h1 = np.maximum(0.0, yy2 - yy1 + 0.00001)
inter = w1 * h1
ovr = inter / (areas[i] + areas[order[1:]] - inter)
inds = np.where(ovr <= NMS_THRESH)[0]
order = order[inds + 1]
return np.array(keep)
def yolov5_post_process(input_data):
masks = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
anchors = [[10, 13], [16, 30], [33, 23], [30, 61], [62, 45],
[59, 119], [116, 90], [156, 198], [373, 326]]
boxes, classes, scores = [], [], []
for input, mask in zip(input_data, masks):
b, c, s = process(input, mask, anchors)
b, c, s = filter_boxes(b, c, s)
boxes.append(b)
classes.append(c)
scores.append(s)
boxes = np.concatenate(boxes)
boxes = xywh2xyxy(boxes)
classes = np.concatenate(classes)
scores = np.concatenate(scores)
nboxes, nclasses, nscores = [], [], []
for c in set(classes):
inds = np.where(classes == c)
b = boxes[inds]
c = classes[inds]
s = scores[inds]
keep = nms_boxes(b, s)
nboxes.append(b[keep])
nclasses.append(c[keep])
nscores.append(s[keep])
if not nclasses and not nscores:
return None, None, None
return np.concatenate(nboxes), np.concatenate(nclasses), np.concatenate(nscores)
def draw(image, boxes, scores, classes):
for box, score, cl in zip(boxes, scores, classes):
top, left, right, bottom = box
# print('class: {}, score: {}'.format(CLASSES[cl], score))
# print('box coordinate left,top,right,down: [{}, {}, {}, {}]'.format(top, left, right, bottom))
top = int(top)
left = int(left)
cv2.rectangle(image, (top, left), (int(right), int(bottom)), (255, 0, 0), 2)
cv2.putText(image, '{0} {1:.2f}'.format(CLASSES[cl], score),
(top, left - 6),
cv2.FONT_HERSHEY_SIMPLEX,
0.6, (0, 0, 255), 2)
def letterbox(im, new_shape=(640, 640), color=(0, 0, 0)):
shape = im.shape[:2] # current shape [height, width]
if isinstance(new_shape, int):
new_shape = (new_shape, new_shape)
r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
ratio = r, r # width, height ratios
new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - \
new_unpad[1] # wh padding
dw /= 2 # divide padding into 2 sides
dh /= 2
if shape[::-1] != new_unpad: # resize
im = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR)
top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
im = cv2.copyMakeBorder(im, top, bottom, left, right,
cv2.BORDER_CONSTANT, value=color) # add border
return im
# return im, ratio, (dw, dh)
def myFunc(rknn_lite, IMG):
IMG = cv2.cvtColor(IMG, cv2.COLOR_BGR2RGB)
# 等比例缩放
# IMG = letterbox(IMG)
# 强制放缩
IMG = cv2.resize(IMG, (IMG_SIZE, IMG_SIZE))
outputs = rknn_lite.inference(inputs=[IMG])
input0_data = outputs[0].reshape([3, -1]+list(outputs[0].shape[-2:]))
input1_data = outputs[1].reshape([3, -1]+list(outputs[1].shape[-2:]))
input2_data = outputs[2].reshape([3, -1]+list(outputs[2].shape[-2:]))
input_data = list()
input_data.append(np.transpose(input0_data, (2, 3, 0, 1)))
input_data.append(np.transpose(input1_data, (2, 3, 0, 1)))
input_data.append(np.transpose(input2_data, (2, 3, 0, 1)))
boxes, classes, scores = yolov5_post_process(input_data)
IMG = cv2.cvtColor(IMG, cv2.COLOR_RGB2BGR)
if boxes is not None:
draw(IMG, boxes, scores, classes)
return IMG

View File

@@ -0,0 +1,49 @@
import cv2
import time
from rknnpool import rknnPoolExecutor
# 图像处理函数,实际应用过程中需要自行修改
from func import myFunc
cap = cv2.VideoCapture('./test.mp4')
# cap = cv2.VideoCapture(0)
modelPath = "./rknnModel/yolov5s_relu_tk2_RK3588_i8.rknn"
# 线程数, 增大可提高帧率
TPEs = 3
# 初始化rknn池
pool = rknnPoolExecutor(
rknnModel=modelPath,
TPEs=TPEs,
func=myFunc)
# 初始化异步所需要的帧
if (cap.isOpened()):
for i in range(TPEs + 1):
ret, frame = cap.read()
if not ret:
cap.release()
del pool
exit(-1)
pool.put(frame)
frames, loopTime, initTime = 0, time.time(), time.time()
while (cap.isOpened()):
frames += 1
ret, frame = cap.read()
if not ret:
break
pool.put(frame)
frame, flag = pool.get()
if flag == False:
break
cv2.imshow('test', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if frames % 30 == 0:
print("30帧平均帧率:\t", 30 / (time.time() - loopTime), "")
loopTime = time.time()
print("总平均帧率\t", frames / (time.time() - initTime))
# 释放cap和rknn线程池
cap.release()
cv2.destroyAllWindows()
pool.release()

View File

@@ -0,0 +1,60 @@
import cv2
import time
import numpy as np
from rknnpool import rknnPoolExecutor
# 图像处理函数,实际应用过程中需要自行修改
from func import myFunc
cap = cv2.VideoCapture('/dev/video10')
# cap = cv2.VideoCapture(0)
modelPath = "./rknnModel/yolov5s_relu_tk2_RK3588_i8.rknn"
# 线程数, 增大可提高帧率
TPEs = 3
# 初始化rknn池
pool = rknnPoolExecutor(
rknnModel=modelPath,
TPEs=TPEs,
func=myFunc)
# 初始化异步所需要的帧
if (cap.isOpened()):
for i in range(TPEs + 1):
ret, frame = cap.read()
# 检查帧数据是否需要重塑
if frame.size == 1280 * 720 * 3: # 检查是否为扁平化数据
frame = frame.reshape((720, 1280, 3)).astype(np.uint8)
else:
print(f"Unexpected frame shape: {frame.shape}")
if not ret:
cap.release()
del pool
exit(-1)
pool.put(frame)
frames, loopTime, initTime = 0, time.time(), time.time()
while (cap.isOpened()):
frames += 1
ret, frame = cap.read()
# 检查帧数据是否需要重塑
if frame.size == 1280 * 720 * 3: # 检查是否为扁平化数据
frame = frame.reshape((720, 1280, 3)).astype(np.uint8)
else:
print(f"Unexpected frame shape: {frame.shape}")
if not ret:
break
pool.put(frame)
frame, flag = pool.get()
if flag == False:
break
cv2.imshow('test', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if frames % 30 == 0:
print("30帧平均帧率:\t", 30 / (time.time() - loopTime), "")
loopTime = time.time()
print("总平均帧率\t", frames / (time.time() - initTime))
# 释放cap和rknn线程池
cap.release()
cv2.destroyAllWindows()
pool.release()

View File

@@ -0,0 +1,31 @@
# 请切换到root用户
# CPU定频
echo "CPU0-3 可用频率:"
sudo cat /sys/devices/system/cpu/cpufreq/policy0/scaling_available_frequencies
sudo echo userspace > /sys/devices/system/cpu/cpufreq/policy0/scaling_governor
sudo echo 1800000 > /sys/devices/system/cpu/cpufreq/policy0/scaling_setspeed
echo "CPU0-3 当前频率:"
sudo cat /sys/devices/system/cpu/cpufreq/policy0/cpuinfo_cur_freq
echo "CPU4-5 可用频率:"
sudo cat /sys/devices/system/cpu/cpufreq/policy4/scaling_available_frequencies
sudo echo userspace > /sys/devices/system/cpu/cpufreq/policy4/scaling_governor
sudo echo 2400000 > /sys/devices/system/cpu/cpufreq/policy4/scaling_setspeed
echo "CPU4-5 当前频率:"
sudo cat /sys/devices/system/cpu/cpufreq/policy4/cpuinfo_cur_freq
echo "CPU6-7 可用频率:"
sudo cat /sys/devices/system/cpu/cpufreq/policy6/scaling_available_frequencies
sudo echo userspace > /sys/devices/system/cpu/cpufreq/policy6/scaling_governor
sudo echo 2400000 > /sys/devices/system/cpu/cpufreq/policy6/scaling_setspeed
echo "CPU6-7 当前频率:"
sudo cat /sys/devices/system/cpu/cpufreq/policy6/cpuinfo_cur_freq
# NPU定频
echo "NPU 可用频率:"
sudo cat /sys/class/devfreq/fdab0000.npu/available_frequencies
sudo echo userspace > /sys/class/devfreq/fdab0000.npu/governor
sudo echo 1000000000 > /sys/class/devfreq/fdab0000.npu/userspace/set_freq
echo "NPU 当前频率:"
sudo cat /sys/class/devfreq/fdab0000.npu/cur_freq

View File

@@ -0,0 +1,5 @@
# 查看温度
sensors
# 查看NPU占用
echo "当前NPU占用:"
sudo cat /sys/kernel/debug/rknpu/load

View File

@@ -0,0 +1,59 @@
from queue import Queue
from rknnlite.api import RKNNLite
from concurrent.futures import ThreadPoolExecutor, as_completed
def initRKNN(rknnModel="./rknnModel/yolov5s.rknn", id=0):
rknn_lite = RKNNLite()
ret = rknn_lite.load_rknn(rknnModel)
if ret != 0:
print("Load RKNN rknnModel failed")
exit(ret)
if id == 0:
ret = rknn_lite.init_runtime(core_mask=RKNNLite.NPU_CORE_0)
elif id == 1:
ret = rknn_lite.init_runtime(core_mask=RKNNLite.NPU_CORE_1)
elif id == 2:
ret = rknn_lite.init_runtime(core_mask=RKNNLite.NPU_CORE_2)
elif id == -1:
ret = rknn_lite.init_runtime(core_mask=RKNNLite.NPU_CORE_0_1_2)
else:
ret = rknn_lite.init_runtime()
if ret != 0:
print("Init runtime environment failed")
exit(ret)
print(rknnModel, "\t\tdone")
return rknn_lite
def initRKNNs(rknnModel="./rknnModel/yolov5s.rknn", TPEs=1):
rknn_list = []
for i in range(TPEs):
rknn_list.append(initRKNN(rknnModel, i % 3))
return rknn_list
class rknnPoolExecutor():
def __init__(self, rknnModel, TPEs, func):
self.TPEs = TPEs
self.queue = Queue()
self.rknnPool = initRKNNs(rknnModel, TPEs)
self.pool = ThreadPoolExecutor(max_workers=TPEs)
self.func = func
self.num = 0
def put(self, frame):
self.queue.put(self.pool.submit(
self.func, self.rknnPool[self.num % self.TPEs], frame))
self.num += 1
def get(self):
if self.queue.empty():
return None, False
fut = self.queue.get()
return fut.result(), True
def release(self):
self.pool.shutdown()
for rknn_lite in self.rknnPool:
rknn_lite.release()

View File

@@ -0,0 +1,35 @@
# Prerequisites
*.d
# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch
*.pch
# Compiled Dynamic libraries
*.dylib
*.dll
# Fortran module files
*.mod
*.smod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Executables
*.exe
*.out
*.app
*.mp4
/build
/install

View File

@@ -0,0 +1,56 @@
cmake_minimum_required(VERSION 3.4.1)
project(rknn_yolov5_demo)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "-pthread")
# skip 3rd-party lib dependencies
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--allow-shlib-undefined")
# install target and libraries
set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/install/rknn_yolov5_demo_${CMAKE_SYSTEM_NAME})
set(CMAKE_SKIP_INSTALL_RPATH FALSE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
# rknn api
set(RKNN_API_PATH ${CMAKE_SOURCE_DIR}/../../runtime/RK3588/${CMAKE_SYSTEM_NAME}/librknn_api)
set(LIB_ARCH aarch64)
set(RKNN_RT_LIB ${CMAKE_SOURCE_DIR}/include/librknnrt.so)
include_directories(${RKNN_API_PATH}/include)
include_directories(${CMAKE_SOURCE_DIR}/include/3rdparty)
# opencv
find_package(OpenCV REQUIRED)
#rga
set(RGA_PATH ${CMAKE_SOURCE_DIR}/include/3rdparty/rga/RK3588)
set(LIB_ARCH aarch64)
set(RGA_LIB ${RGA_PATH}/lib/Linux/${LIB_ARCH}/librga.so)
include_directories( ${RGA_PATH}/include)
set(CMAKE_INSTALL_RPATH "lib")
# rknn_yolov5_demo
include_directories( ${CMAKE_SOURCE_DIR}/include)
add_executable(rknn_yolov5_demo
src/main.cc
src/postprocess.cc
)
target_link_libraries(rknn_yolov5_demo
${RKNN_RT_LIB}
${RGA_LIB}
${OpenCV_LIBS}
)
# install target and libraries
set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/install/rknn_yolov5_demo_${CMAKE_SYSTEM_NAME})
install(TARGETS rknn_yolov5_demo DESTINATION ./)
install(PROGRAMS ${RKNN_RT_LIB} DESTINATION lib)
install(PROGRAMS ${RGA_LIB} DESTINATION lib)
install(DIRECTORY model DESTINATION ./)

View File

@@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View File

@@ -0,0 +1,43 @@
# 简介
* 此仓库为c++实现, 大体改自[rknpu2](https://github.com/rockchip-linux/rknpu2), python快速部署见于[rknn-multi-threaded](https://github.com/leafqycc/rknn-multi-threaded)
* 使用[线程池](https://github.com/senlinzhan/dpool)异步操作rknn模型, 提高rk3588/rk3588s的NPU使用率, 进而提高推理帧数
* [yolov5s](https://github.com/rockchip-linux/rknpu2/tree/master/examples/rknn_yolov5_demo/model/RK3588)使用relu激活函数进行优化,提高量化能力
* **rk3568等**请自行修改include/rknnPool.hpp下的rknn_lite类和rknnPool的构造函数
# 更新说明
* 修复了cmake找不到pthread的问题
* 新建nosigmoid分支,使用[rknn_model_zoo](https://github.com/airockchip/rknn_model_zoo/tree/main/models)下的模型以达到极限性能提升
# 使用说明
### 演示
* 系统需安装有**OpenCV**
* 下载Releases中的测试视频于项目根目录,运行build-linux_RK3588.sh
* 可切换至root用户运行performance.sh定频提高性能和稳定性
* 编译完成后进入install运行命令./rknn_yolov5_demo **模型所在路径** **视频所在路径/摄像头序号**
### 部署应用
* 修改include/rknnPool.hpp中的rknn_lite类
* 修改inclue/rknnPool.hpp中的rknnPool类的构造函数
# 多线程模型帧率测试
* 使用performance.sh进行CPU/NPU定频尽量减少误差
* 测试模型来源:
* [yolov5s-silu](https://github.com/rockchip-linux/rknn-toolkit2/tree/master/examples/onnx/yolov5)
* [yolov5s-relu](https://github.com/rockchip-linux/rknpu2/tree/master/examples/rknn_yolov5_demo/model/RK3588)
* 测试视频可见于 [bilibili](https://www.bilibili.com/video/BV1zo4y1x7aE/?spm_id_from=333.999.0.0)
| 模型\线程数 | 1 | 2 | 3 | 4 | 5 | 6 | 12 |
| ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- |
| Yolov5s - silu | 15.9269 | 32.9192 | 52.8330 | 46.6782 | 58.2921 | 71.8070 | |
| Yolov5s - relu | 26.8601 | 58.0305 | 77.6904 | 80.7144 | 93.9126 | 101.1400 | 122.7334 |
# 补充
* 异常处理尚未完善, 目前仅支持rk3588/rk3588s下的运行
* relu版本相较于silu有着较大性能提升, 以及存在一些精度损失, 详情见于[rknn_model_zoo](https://github.com/airockchip/rknn_model_zoo/tree/main/models/CV/object_detection/yolo)
# Acknowledgements
* https://github.com/rockchip-linux/rknpu2
* https://github.com/senlinzhan/dpool
* https://github.com/ultralytics/yolov5
* https://github.com/airockchip/rknn_model_zoo
* https://github.com/rockchip-linux/rknn-toolkit2

View File

@@ -0,0 +1,31 @@
set -e
# TARGET_SOC="rk3588"
GCC_COMPILER=aarch64-linux-gnu
export LD_LIBRARY_PATH=${TOOL_CHAIN}/lib64:$LD_LIBRARY_PATH
export CC=${GCC_COMPILER}-gcc
export CXX=${GCC_COMPILER}-g++
ROOT_PWD=$( cd "$( dirname $0 )" && cd -P "$( dirname "$SOURCE" )" && pwd )
# build
BUILD_DIR=${ROOT_PWD}/build/build_linux_aarch64
if [ ! -d "${BUILD_DIR}" ]; then
mkdir -p ${BUILD_DIR}
fi
cd ${BUILD_DIR}
cmake ../.. -DCMAKE_SYSTEM_NAME=Linux
make -j8
make install
cd -
# relu版本
cd install/rknn_yolov5_demo_Linux/ && ./rknn_yolov5_demo ./model/RK3588/yolov5s-640-640.rknn ../../720p60hz.mp4
# silu版本
# cd install/rknn_yolov5_demo_Linux/ && ./rknn_yolov5_demo ./model/RK3588/yolov5s.rknn ../../720p60hz.mp4
# 使用摄像头
# cd install/rknn_yolov5_demo_Linux/ && ./rknn_yolov5_demo ./model/RK3588/yolov5s-640-640.rknn 0

View File

@@ -0,0 +1,59 @@
/*
* Copyright (C) 2016 Rockchip Electronics Co., Ltd.
* Authors:
* Zhiqin Wei <wzq@rock-chips.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _rk_graphic_buffer_h_
#define _rk_graphic_buffer_h_
#ifdef ANDROID
#include <stdint.h>
#include <vector>
#include <sys/types.h>
#include <system/graphics.h>
#include <utils/Thread.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <sys/mman.h>
#include <linux/stddef.h>
#include <utils/Atomic.h>
#include <utils/Errors.h>
#include <android/log.h>
#include <utils/Log.h>
#include <log/log_main.h>
#include "drmrga.h"
#include "rga.h"
// -------------------------------------------------------------------------------
int RkRgaGetHandleFd(buffer_handle_t handle, int *fd);
int RkRgaGetHandleAttributes(buffer_handle_t handle,
std::vector<int> *attrs);
int RkRgaGetHandleMapAddress(buffer_handle_t handle,
void **buf);
#endif //Android
#endif //_rk_graphic_buffer_h_

View File

@@ -0,0 +1,81 @@
/*
* Copyright (C) 2016 Rockchip Electronics Co., Ltd.
* Authors:
* Zhiqin Wei <wzq@rock-chips.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _rockchip_rga_c_h_
#define _rockchip_rga_c_h_
#include <stdint.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <sys/mman.h>
#include <linux/stddef.h>
#include "drmrga.h"
#include "rga.h"
#ifdef __cplusplus
extern "C"{
#endif
/*
* Compatible with the old version of C interface.The new
* version of the C interface no longer requires users to
* initialize rga, so RgaInit and RgaDeInit are just for
* compatibility with the old C interface, so please do
* not use ctx, because it is usually a NULL.
*/
#define RgaInit(ctx) ({ \
int ret = 0; \
ret = c_RkRgaInit(); \
c_RkRgaGetContext(ctx); \
ret;\
})
#define RgaDeInit(ctx) { \
(void)ctx; /* unused */ \
c_RkRgaDeInit(); \
}
#define RgaBlit(...) c_RkRgaBlit(__VA_ARGS__)
#define RgaCollorFill(...) c_RkRgaColorFill(__VA_ARGS__)
#define RgaFlush() c_RkRgaFlush()
int c_RkRgaInit();
void c_RkRgaDeInit();
void c_RkRgaGetContext(void **ctx);
int c_RkRgaBlit(rga_info_t *src, rga_info_t *dst, rga_info_t *src1);
int c_RkRgaColorFill(rga_info_t *dst);
int c_RkRgaFlush();
#ifndef ANDROID /* linux */
int c_RkRgaGetAllocBuffer(bo_t *bo_info, int width, int height, int bpp);
int c_RkRgaGetAllocBufferCache(bo_t *bo_info, int width, int height, int bpp);
int c_RkRgaGetMmap(bo_t *bo_info);
int c_RkRgaUnmap(bo_t *bo_info);
int c_RkRgaFree(bo_t *bo_info);
int c_RkRgaGetBufferFd(bo_t *bo_info, int *fd);
#endif /* #ifndef ANDROID */
#ifdef __cplusplus
}
#endif
#endif /* #ifndef _rockchip_rga_c_h_ */

View File

@@ -0,0 +1,193 @@
/*
* Copyright (C) 2020 Rockchip Electronics Co., Ltd.
* Authors:
* PutinLee <putin.lee@rock-chips.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _LIBS_RGA_MUTEX_H
#define _LIBS_RGA_MUTEX_H
#ifndef ANDROID
#include <stdint.h>
#include <sys/types.h>
#include <time.h>
#include <pthread.h>
// Enable thread safety attributes only with clang.
// The attributes can be safely erased when compiling with other compilers.
#if defined(__clang__) && (!defined(SWIG))
#define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
#else
#define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op
#endif
#define CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(capability(x))
#define SCOPED_CAPABILITY THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
#define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
#define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x))
#define ACQUIRED_BEFORE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__))
#define ACQUIRED_AFTER(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__))
#define REQUIRES(...) THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__))
#define REQUIRES_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(__VA_ARGS__))
#define ACQUIRE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(__VA_ARGS__))
#define ACQUIRE_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(__VA_ARGS__))
#define RELEASE(...) THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__))
#define RELEASE_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(__VA_ARGS__))
#define TRY_ACQUIRE(...) THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__))
#define TRY_ACQUIRE_SHARED(...) \
THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(__VA_ARGS__))
#define EXCLUDES(...) THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__))
#define ASSERT_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x))
#define ASSERT_SHARED_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x))
#define RETURN_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))
#define NO_THREAD_SAFETY_ANALYSIS THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)
class Condition;
/*
* NOTE: This class is for code that builds on Win32. Its usage is
* deprecated for code which doesn't build for Win32. New code which
* doesn't build for Win32 should use std::mutex and std::lock_guard instead.
*
* Simple mutex class. The implementation is system-dependent.
*
* The mutex must be unlocked by the thread that locked it. They are not
* recursive, i.e. the same thread can't lock it multiple times.
*/
class CAPABILITY("mutex") Mutex {
public:
enum {
PRIVATE = 0,
SHARED = 1
};
Mutex();
explicit Mutex(const char* name);
explicit Mutex(int type, const char* name = nullptr);
~Mutex();
// lock or unlock the mutex
int32_t lock() ACQUIRE();
void unlock() RELEASE();
// lock if possible; returns 0 on success, error otherwise
int32_t tryLock() TRY_ACQUIRE(0);
int32_t timedLock(int64_t timeoutNs) TRY_ACQUIRE(0);
// Manages the mutex automatically. It'll be locked when Autolock is
// constructed and released when Autolock goes out of scope.
class SCOPED_CAPABILITY Autolock {
public:
inline explicit Autolock(Mutex& mutex) ACQUIRE(mutex) : mLock(mutex) {
mLock.lock();
}
inline explicit Autolock(Mutex* mutex) ACQUIRE(mutex) : mLock(*mutex) {
mLock.lock();
}
inline ~Autolock() RELEASE() {
mLock.unlock();
}
private:
Mutex& mLock;
// Cannot be copied or moved - declarations only
Autolock(const Autolock&);
Autolock& operator=(const Autolock&);
};
private:
friend class Condition;
// A mutex cannot be copied
Mutex(const Mutex&);
Mutex& operator=(const Mutex&);
pthread_mutex_t mMutex;
};
// ---------------------------------------------------------------------------
inline Mutex::Mutex() {
pthread_mutex_init(&mMutex, nullptr);
}
inline Mutex::Mutex(__attribute__((unused)) const char* name) {
pthread_mutex_init(&mMutex, nullptr);
}
inline Mutex::Mutex(int type, __attribute__((unused)) const char* name) {
if (type == SHARED) {
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
pthread_mutex_init(&mMutex, &attr);
pthread_mutexattr_destroy(&attr);
} else {
pthread_mutex_init(&mMutex, nullptr);
}
}
inline Mutex::~Mutex() {
pthread_mutex_destroy(&mMutex);
}
inline int32_t Mutex::lock() {
return -pthread_mutex_lock(&mMutex);
}
inline void Mutex::unlock() {
pthread_mutex_unlock(&mMutex);
}
inline int32_t Mutex::tryLock() {
return -pthread_mutex_trylock(&mMutex);
}
inline int32_t Mutex::timedLock(int64_t timeoutNs) {
timespec now;
clock_gettime(CLOCK_REALTIME, &now);
timeoutNs += now.tv_sec*1000000000 + now.tv_nsec;
const struct timespec ts = {
/* .tv_sec = */ static_cast<time_t>(timeoutNs / 1000000000),
/* .tv_nsec = */ static_cast<long>(timeoutNs % 1000000000),
};
return -pthread_mutex_timedlock(&mMutex, &ts);
}
// ---------------------------------------------------------------------------
/*
* Automatic mutex. Declare one of these at the top of a function.
* When the function returns, it will go out of scope, and release the
* mutex.
*/
typedef Mutex::Autolock AutoMutex;
#endif // __ANDROID_VNDK__
#endif // _LIBS_RGA_MUTEX_H

View File

@@ -0,0 +1,70 @@
/*
* Copyright (C) 2016 Rockchip Electronics Co., Ltd.
* Authors:
* Zhiqin Wei <wzq@rock-chips.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _LIBS_RGA_SINGLETON_H
#define _LIBS_RGA_SINGLETON_H
#ifndef ANDROID
#include "RgaMutex.h"
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundefined-var-template"
#endif
template <typename TYPE>
class Singleton {
public:
static TYPE& getInstance() {
Mutex::Autolock _l(sLock);
TYPE* instance = sInstance;
if (instance == nullptr) {
instance = new TYPE();
sInstance = instance;
}
return *instance;
}
static bool hasInstance() {
Mutex::Autolock _l(sLock);
return sInstance != nullptr;
}
protected:
~Singleton() { }
Singleton() { }
private:
Singleton(const Singleton&);
Singleton& operator = (const Singleton&);
static Mutex sLock;
static TYPE* sInstance;
};
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
#define RGA_SINGLETON_STATIC_INSTANCE(TYPE) \
template<> ::Mutex \
(::Singleton< TYPE >::sLock)(::Mutex::PRIVATE); \
template<> TYPE* ::Singleton< TYPE >::sInstance(nullptr); /* NOLINT */ \
template class ::Singleton< TYPE >;
#endif //ANDROID
#endif //_LIBS_RGA_SINGLETON_H

View File

@@ -0,0 +1,31 @@
/*
* Copyright (C) 2016 Rockchip Electronics Co., Ltd.
* Authors:
* Zhiqin Wei <wzq@rock-chips.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _rga_utils_h_
#define _rga_utils_h_
// -------------------------------------------------------------------------------
float get_bpp_from_format(int format);
int get_perPixel_stride_from_format(int format);
int get_buf_from_file(void *buf, int f, int sw, int sh, int index);
int output_buf_data_to_file(void *buf, int f, int sw, int sh, int index);
const char *translate_format_str(int format);
int get_buf_from_file_FBC(void *buf, int f, int sw, int sh, int index);
int output_buf_data_to_file_FBC(void *buf, int f, int sw, int sh, int index);
#endif

View File

@@ -0,0 +1,110 @@
/*
* Copyright (C) 2016 Rockchip Electronics Co., Ltd.
* Authors:
* Zhiqin Wei <wzq@rock-chips.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _rockchip_rga_h_
#define _rockchip_rga_h_
#include <stdint.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <sys/mman.h>
#include <linux/stddef.h>
#include "drmrga.h"
#include "GrallocOps.h"
#include "RgaUtils.h"
#include "rga.h"
//////////////////////////////////////////////////////////////////////////////////
#ifndef ANDROID
#include "RgaSingleton.h"
#endif
#ifdef ANDROID
#include <utils/Singleton.h>
#include <utils/Thread.h>
#include <hardware/hardware.h>
namespace android {
#endif
class RockchipRga :public Singleton<RockchipRga> {
public:
static inline RockchipRga& get() {
return getInstance();
}
int RkRgaInit();
void RkRgaDeInit();
void RkRgaGetContext(void **ctx);
#ifndef ANDROID /* LINUX */
int RkRgaAllocBuffer(int drm_fd /* input */, bo_t *bo_info,
int width, int height, int bpp, int flags);
int RkRgaFreeBuffer(int drm_fd /* input */, bo_t *bo_info);
int RkRgaGetAllocBuffer(bo_t *bo_info, int width, int height, int bpp);
int RkRgaGetAllocBufferExt(bo_t *bo_info, int width, int height, int bpp, int flags);
int RkRgaGetAllocBufferCache(bo_t *bo_info, int width, int height, int bpp);
int RkRgaGetMmap(bo_t *bo_info);
int RkRgaUnmap(bo_t *bo_info);
int RkRgaFree(bo_t *bo_info);
int RkRgaGetBufferFd(bo_t *bo_info, int *fd);
#else
int RkRgaGetBufferFd(buffer_handle_t handle, int *fd);
int RkRgaGetHandleMapCpuAddress(buffer_handle_t handle, void **buf);
#endif
int RkRgaBlit(rga_info *src, rga_info *dst, rga_info *src1);
int RkRgaCollorFill(rga_info *dst);
int RkRgaCollorPalette(rga_info *src, rga_info *dst, rga_info *lut);
int RkRgaFlush();
void RkRgaSetLogOnceFlag(int log) {
mLogOnce = log;
}
void RkRgaSetAlwaysLogFlag(bool log) {
mLogAlways = log;
}
void RkRgaLogOutRgaReq(struct rga_req rgaReg);
int RkRgaLogOutUserPara(rga_info *rgaInfo);
inline bool RkRgaIsReady() {
return mSupportRga;
}
RockchipRga();
~RockchipRga();
private:
bool mSupportRga;
int mLogOnce;
int mLogAlways;
void * mContext;
friend class Singleton<RockchipRga>;
};
#ifdef ANDROID
}; // namespace android
#endif
#endif

View File

@@ -0,0 +1,242 @@
/*
* Copyright (C) 2016 Rockchip Electronics Co., Ltd.
* Authors:
* Zhiqin Wei <wzq@rock-chips.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _rk_drm_rga_
#define _rk_drm_rga_
#include <stdint.h>
#include <errno.h>
#include <sys/cdefs.h>
#include "rga.h"
#ifdef ANDROID
#define DRMRGA_HARDWARE_MODULE_ID "librga"
#include <hardware/gralloc.h>
#include <hardware/hardware.h>
#include <system/graphics.h>
#include <cutils/native_handle.h>
#ifdef ANDROID_12
#include <hardware/hardware_rockchip.h>
#endif
#endif
#ifndef ANDROID /* LINUX */
/* flip source image horizontally (around the vertical axis) */
#define HAL_TRANSFORM_FLIP_H 0x01
/* flip source image vertically (around the horizontal axis)*/
#define HAL_TRANSFORM_FLIP_V 0x02
/* rotate source image 90 degrees clockwise */
#define HAL_TRANSFORM_ROT_90 0x04
/* rotate source image 180 degrees */
#define HAL_TRANSFORM_ROT_180 0x03
/* rotate source image 270 degrees clockwise */
#define HAL_TRANSFORM_ROT_270 0x07
#endif
#define HAL_TRANSFORM_FLIP_H_V 0x08
/*****************************************************************************/
/* for compatibility */
#define DRM_RGA_MODULE_API_VERSION HWC_MODULE_API_VERSION_0_1
#define DRM_RGA_DEVICE_API_VERSION HWC_DEVICE_API_VERSION_0_1
#define DRM_RGA_API_VERSION HWC_DEVICE_API_VERSION
#define DRM_RGA_TRANSFORM_ROT_MASK 0x0000000F
#define DRM_RGA_TRANSFORM_ROT_0 0x00000000
#define DRM_RGA_TRANSFORM_ROT_90 HAL_TRANSFORM_ROT_90
#define DRM_RGA_TRANSFORM_ROT_180 HAL_TRANSFORM_ROT_180
#define DRM_RGA_TRANSFORM_ROT_270 HAL_TRANSFORM_ROT_270
#define DRM_RGA_TRANSFORM_FLIP_MASK 0x00000003
#define DRM_RGA_TRANSFORM_FLIP_H HAL_TRANSFORM_FLIP_H
#define DRM_RGA_TRANSFORM_FLIP_V HAL_TRANSFORM_FLIP_V
enum {
AWIDTH = 0,
AHEIGHT,
ASTRIDE,
AFORMAT,
ASIZE,
ATYPE,
};
/*****************************************************************************/
#ifndef ANDROID
/* memory type definitions. */
enum drm_rockchip_gem_mem_type {
/* Physically Continuous memory and used as default. */
ROCKCHIP_BO_CONTIG = 1 << 0,
/* cachable mapping. */
ROCKCHIP_BO_CACHABLE = 1 << 1,
/* write-combine mapping. */
ROCKCHIP_BO_WC = 1 << 2,
ROCKCHIP_BO_SECURE = 1 << 3,
ROCKCHIP_BO_MASK = ROCKCHIP_BO_CONTIG | ROCKCHIP_BO_CACHABLE |
ROCKCHIP_BO_WC | ROCKCHIP_BO_SECURE
};
typedef struct bo {
int fd;
void *ptr;
size_t size;
size_t offset;
size_t pitch;
unsigned handle;
} bo_t;
#endif
/*
@value size: user not need care about.For avoid read/write out of memory
*/
typedef struct rga_rect {
int xoffset;
int yoffset;
int width;
int height;
int wstride;
int hstride;
int format;
int size;
} rga_rect_t;
typedef struct rga_nn {
int nn_flag;
int scale_r;
int scale_g;
int scale_b;
int offset_r;
int offset_g;
int offset_b;
} rga_nn_t;
typedef struct rga_dither {
int enable;
int mode;
int lut0_l;
int lut0_h;
int lut1_l;
int lut1_h;
} rga_dither_t;
/*
@value fd: use fd to share memory, it can be ion shard fd,and dma fd.
@value virAddr:userspace address
@value phyAddr:use phy address
@value hnd: use buffer_handle_t
*/
typedef struct rga_info {
int fd;
void *virAddr;
void *phyAddr;
#ifndef ANDROID /* LINUX */
unsigned hnd;
#else /* Android */
buffer_handle_t hnd;
#endif
int format;
rga_rect_t rect;
unsigned int blend;
int bufferSize;
int rotation;
int color;
int testLog;
int mmuFlag;
int colorkey_en;
int colorkey_mode;
int colorkey_max;
int colorkey_min;
int scale_mode;
int color_space_mode;
int sync_mode;
rga_nn_t nn;
rga_dither_t dither;
int rop_code;
int rd_mode;
unsigned short is_10b_compact;
unsigned short is_10b_endian;
int in_fence_fd;
int out_fence_fd;
int core;
int priority;
unsigned short enable;
int handle;
struct rga_mosaic_info mosaic_info;
struct rga_osd_info osd_info;
struct rga_pre_intr_info pre_intr;
int mpi_mode;
int ctx_id;
char reserve[402];
} rga_info_t;
typedef struct drm_rga {
rga_rect_t src;
rga_rect_t dst;
} drm_rga_t;
/*
@fun rga_set_rect:For use to set the rects esayly
@param rect:The rect user want to set,like setting the src rect:
drm_rga_t rects;
rga_set_rect(rects.src,0,0,1920,1080,1920,NV12);
mean to set the src rect to the value.
*/
static inline int rga_set_rect(rga_rect_t *rect,
int x, int y, int w, int h, int sw, int sh, int f) {
if (!rect)
return -EINVAL;
rect->xoffset = x;
rect->yoffset = y;
rect->width = w;
rect->height = h;
rect->wstride = sw;
rect->hstride = sh;
rect->format = f;
return 0;
}
#ifndef ANDROID /* LINUX */
static inline void rga_set_rotation(rga_info_t *info, int angle) {
if (angle == 90)
info->rotation = HAL_TRANSFORM_ROT_90;
else if (angle == 180)
info->rotation = HAL_TRANSFORM_ROT_180;
else if (angle == 270)
info->rotation = HAL_TRANSFORM_ROT_270;
}
#endif
/*****************************************************************************/
#endif

View File

@@ -0,0 +1,763 @@
/*
* Copyright (C) 2020 Rockchip Electronics Co., Ltd.
* Authors:
* PutinLee <putin.lee@rock-chips.com>
* Cerf Yu <cerf.yu@rock-chips.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _im2d_h_
#define _im2d_h_
#ifdef __cplusplus
extern "C" {
#endif
#include "im2d_version.h"
#include "im2d_type.h"
#ifndef IM_API
#define IM_API /* define API export as needed */
#endif
#define RGA_GET_MIN(n1, n2) ((n1) < (n2) ? (n1) : (n2))
/*
* @return error message string
*/
#define imStrError(...) \
({ \
const char* im2d_api_err; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
im2d_api_err = imStrError_t(IM_STATUS_INVALID_PARAM); \
} else if (__argc == 1){ \
im2d_api_err = imStrError_t((IM_STATUS)__args[0]); \
} else { \
im2d_api_err = ("Fatal error, imStrError() too many parameters\n"); \
printf("Fatal error, imStrError() too many parameters\n"); \
} \
im2d_api_err; \
})
IM_API const char* imStrError_t(IM_STATUS status);
/*
* Import external buffers into RGA driver.
*
* @param fd/va/pa
* Select dma_fd/virtual_address/physical_address by buffer type
* @param param
* Configure buffer parameters
*
* @return rga_buffer_handle_t
*/
IM_API rga_buffer_handle_t importbuffer_fd(int fd, im_handle_param_t *param);
IM_API rga_buffer_handle_t importbuffer_virtualaddr(void *va, im_handle_param_t *param);
IM_API rga_buffer_handle_t importbuffer_physicaladdr(uint64_t pa, im_handle_param_t *param);
/*
* Import external buffers into RGA driver.
*
* @param handle
* rga buffer handle
*
* @return success or else negative error code.
*/
IM_API IM_STATUS releasebuffer_handle(rga_buffer_handle_t handle);
/*
* Wrap image Parameters.
*
* @param handle
* RGA buffer handle.
* @param width
* Width of image manipulation area.
* @param height
* Height of image manipulation area.
* @param wstride
* Width pixel stride, default (width = wstride).
* @param hstride
* Height pixel stride, default (height = hstride).
* @param format
* Image format.
*
* @return rga_buffer_t
*/
#define wrapbuffer_handle(handle, width, height, format, ...) \
({ \
rga_buffer_t im2d_api_buffer; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
im2d_api_buffer = wrapbuffer_handle_t(handle, width, height, width, height, format); \
} else if (__argc == 2){ \
im2d_api_buffer = wrapbuffer_handle_t(handle, width, height, __args[0], __args[1], format); \
} else { \
printf("invalid parameter\n"); \
} \
im2d_api_buffer; \
})
IM_API rga_buffer_t wrapbuffer_handle_t(rga_buffer_handle_t handle, int width, int height, int wstride, int hstride, int format);
/* For legarcy. */
#define wrapbuffer_virtualaddr(vir_addr, width, height, format, ...) \
({ \
rga_buffer_t im2d_api_buffer; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
im2d_api_buffer = wrapbuffer_virtualaddr_t(vir_addr, width, height, width, height, format); \
} else if (__argc == 2){ \
im2d_api_buffer = wrapbuffer_virtualaddr_t(vir_addr, width, height, __args[0], __args[1], format); \
} else { \
printf("invalid parameter\n"); \
} \
im2d_api_buffer; \
})
#define wrapbuffer_physicaladdr(phy_addr, width, height, format, ...) \
({ \
rga_buffer_t im2d_api_buffer; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
im2d_api_buffer = wrapbuffer_physicaladdr_t(phy_addr, width, height, width, height, format); \
} else if (__argc == 2){ \
im2d_api_buffer = wrapbuffer_physicaladdr_t(phy_addr, width, height, __args[0], __args[1], format); \
} else { \
printf("invalid parameter\n"); \
} \
im2d_api_buffer; \
})
#define wrapbuffer_fd(fd, width, height, format, ...) \
({ \
rga_buffer_t im2d_api_buffer; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
im2d_api_buffer = wrapbuffer_fd_t(fd, width, height, width, height, format); \
} else if (__argc == 2){ \
im2d_api_buffer = wrapbuffer_fd_t(fd, width, height, __args[0], __args[1], format); \
} else { \
printf("invalid parameter\n"); \
} \
im2d_api_buffer; \
})
IM_API rga_buffer_t wrapbuffer_virtualaddr_t(void* vir_addr, int width, int height, int wstride, int hstride, int format);
IM_API rga_buffer_t wrapbuffer_physicaladdr_t(void* phy_addr, int width, int height, int wstride, int hstride, int format);
IM_API rga_buffer_t wrapbuffer_fd_t(int fd, int width, int height, int wstride, int hstride, int format);
/*
* Query RGA basic information, supported resolution, supported format, etc.
*
* @param name
* RGA_VENDOR
* RGA_VERSION
* RGA_MAX_INPUT
* RGA_MAX_OUTPUT
* RGA_INPUT_FORMAT
* RGA_OUTPUT_FORMAT
* RGA_EXPECTED
* RGA_ALL
*
* @returns a string describing properties of RGA.
*/
IM_API const char* querystring(int name);
/*
* check RGA basic information, supported resolution, supported format, etc.
*
* @param src
* @param dst
* @param src_rect
* @param dst_rect
* @param mode_usage
*
* @returns no error or else negative error code.
*/
#define imcheck(src, dst, src_rect, dst_rect, ...) \
({ \
IM_STATUS __ret = IM_STATUS_NOERROR; \
rga_buffer_t __pat; \
im_rect __pat_rect; \
memset(&__pat, 0, sizeof(rga_buffer_t)); \
memset(&__pat_rect, 0, sizeof(im_rect)); \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
rga_check_perpare((rga_buffer_t *)(&src), (rga_buffer_t *)(&dst), (rga_buffer_t *)(&__pat), \
(im_rect *)(&src_rect), (im_rect *)(&dst_rect), (im_rect *)(&__pat_rect), 0); \
__ret = imcheck_t(src, dst, __pat, src_rect, dst_rect, __pat_rect, 0); \
} else if (__argc == 1){ \
rga_check_perpare((rga_buffer_t *)(&src), (rga_buffer_t *)(&dst), (rga_buffer_t *)(&__pat), \
(im_rect *)(&src_rect), (im_rect *)(&dst_rect), (im_rect *)(&__pat_rect), __args[0]); \
__ret = imcheck_t(src, dst, __pat, src_rect, dst_rect, __pat_rect, __args[0]); \
} else { \
__ret = IM_STATUS_FAILED; \
printf("check failed\n"); \
} \
__ret; \
})
#define imcheck_composite(src, dst, pat, src_rect, dst_rect, pat_rect, ...) \
({ \
IM_STATUS __ret = IM_STATUS_NOERROR; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
rga_check_perpare((rga_buffer_t *)(&src), (rga_buffer_t *)(&dst), (rga_buffer_t *)(&pat), \
(im_rect *)(&src_rect), (im_rect *)(&dst_rect), (im_rect *)(&pat_rect), 0); \
__ret = imcheck_t(src, dst, pat, src_rect, dst_rect, pat_rect, 0); \
} else if (__argc == 1){ \
rga_check_perpare((rga_buffer_t *)(&src), (rga_buffer_t *)(&dst), (rga_buffer_t *)(&pat), \
(im_rect *)(&src_rect), (im_rect *)(&dst_rect), (im_rect *)(&pat_rect), __args[0]); \
__ret = imcheck_t(src, dst, pat, src_rect, dst_rect, pat_rect, __args[0]); \
} else { \
__ret = IM_STATUS_FAILED; \
printf("check failed\n"); \
} \
__ret; \
})
IM_API void rga_check_perpare(rga_buffer_t *src, rga_buffer_t *dst, rga_buffer_t *pat,
im_rect *src_rect, im_rect *dst_rect, im_rect *pat_rect, int mode_usage);
IM_API IM_STATUS imcheck_t(const rga_buffer_t src, const rga_buffer_t dst, const rga_buffer_t pat,
const im_rect src_rect, const im_rect dst_rect, const im_rect pat_rect, const int mode_usage);
/*
* Resize
*
* @param src
* @param dst
* @param fx
* @param fy
* @param interpolation
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#define imresize(src, dst, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
double __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(double); \
if (__argc == 0) { \
__ret = imresize_t(src, dst, 0, 0, INTER_LINEAR, 1); \
} else if (__argc == 2){ \
__ret = imresize_t(src, dst, __args[RGA_GET_MIN(__argc, 0)], __args[RGA_GET_MIN(__argc, 1)], INTER_LINEAR, 1); \
} else if (__argc == 3){ \
__ret = imresize_t(src, dst, __args[RGA_GET_MIN(__argc, 0)], __args[RGA_GET_MIN(__argc, 1)], (int)__args[RGA_GET_MIN(__argc, 2)], 1); \
} else if (__argc == 4){ \
__ret = imresize_t(src, dst, __args[RGA_GET_MIN(__argc, 0)], __args[RGA_GET_MIN(__argc, 1)], (int)__args[RGA_GET_MIN(__argc, 2)], (int)__args[RGA_GET_MIN(__argc, 3)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
#define impyramid(src, dst, direction) \
imresize_t(src, \
dst, \
direction == IM_UP_SCALE ? 0.5 : 2, \
direction == IM_UP_SCALE ? 0.5 : 2, \
INTER_LINEAR, 1)
IM_API IM_STATUS imresize_t(const rga_buffer_t src, rga_buffer_t dst, double fx, double fy, int interpolation, int sync);
/*
* Crop
*
* @param src
* @param dst
* @param rect
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#define imcrop(src, dst, rect, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = imcrop_t(src, dst, rect, 1); \
} else if (__argc == 1){ \
__ret = imcrop_t(src, dst, rect, (int)__args[RGA_GET_MIN(__argc, 0)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
IM_API IM_STATUS imcrop_t(const rga_buffer_t src, rga_buffer_t dst, im_rect rect, int sync);
/*
* rotation
*
* @param src
* @param dst
* @param rotation
* IM_HAL_TRANSFORM_ROT_90
* IM_HAL_TRANSFORM_ROT_180
* IM_HAL_TRANSFORM_ROT_270
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#define imrotate(src, dst, rotation, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = imrotate_t(src, dst, rotation, 1); \
} else if (__argc == 1){ \
__ret = imrotate_t(src, dst, rotation, (int)__args[RGA_GET_MIN(__argc, 0)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
IM_API IM_STATUS imrotate_t(const rga_buffer_t src, rga_buffer_t dst, int rotation, int sync);
/*
* flip
*
* @param src
* @param dst
* @param mode
* IM_HAL_TRANSFORM_FLIP_H
* IM_HAL_TRANSFORM_FLIP_V
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#define imflip(src, dst, mode, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = imflip_t(src, dst, mode, 1); \
} else if (__argc == 1){ \
__ret = imflip_t(src, dst, mode, (int)__args[RGA_GET_MIN(__argc, 0)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
IM_API IM_STATUS imflip_t (const rga_buffer_t src, rga_buffer_t dst, int mode, int sync);
/*
* fill/reset/draw
*
* @param src
* @param dst
* @param rect
* @param color
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#define imfill(buf, rect, color, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = imfill_t(buf, rect, color, 1); \
} else if (__argc == 1){ \
__ret = imfill_t(buf, rect, color, (int)__args[RGA_GET_MIN(__argc, 0)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
#define imreset(buf, rect, color, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = imfill_t(buf, rect, color, 1); \
} else if (__argc == 1){ \
__ret = imfill_t(buf, rect, color, (int)__args[RGA_GET_MIN(__argc, 0)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
#define imdraw(buf, rect, color, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = imfill_t(buf, rect, color, 1); \
} else if (__argc == 1){ \
__ret = imfill_t(buf, rect, color, (int)__args[RGA_GET_MIN(__argc, 0)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
IM_API IM_STATUS imfill_t(rga_buffer_t dst, im_rect rect, int color, int sync);
/*
* palette
*
* @param src
* @param dst
* @param lut
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#define impalette(src, dst, lut, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = impalette_t(src, dst, lut, 1); \
} else if (__argc == 1){ \
__ret = impalette_t(src, dst, lut, (int)__args[RGA_GET_MIN(__argc, 0)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
IM_API IM_STATUS impalette_t(rga_buffer_t src, rga_buffer_t dst, rga_buffer_t lut, int sync);
/*
* translate
*
* @param src
* @param dst
* @param x
* @param y
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#define imtranslate(src, dst, x, y, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = imtranslate_t(src, dst, x, y, 1); \
} else if (__argc == 1){ \
__ret = imtranslate_t(src, dst, x, y, (int)__args[RGA_GET_MIN(__argc, 0)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
IM_API IM_STATUS imtranslate_t(const rga_buffer_t src, rga_buffer_t dst, int x, int y, int sync);
/*
* copy
*
* @param src
* @param dst
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#define imcopy(src, dst, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = imcopy_t(src, dst, 1); \
} else if (__argc == 1){ \
__ret = imcopy_t(src, dst, (int)__args[RGA_GET_MIN(__argc, 0)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
IM_API IM_STATUS imcopy_t(const rga_buffer_t src, rga_buffer_t dst, int sync);
/*
* blend (SRC + DST -> DST or SRCA + SRCB -> DST)
*
* @param srcA
* @param srcB can be NULL.
* @param dst
* @param mode
* IM_ALPHA_BLEND_MODE
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#define imblend(srcA, dst, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
rga_buffer_t srcB; \
memset(&srcB, 0x00, sizeof(rga_buffer_t)); \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = imblend_t(srcA, srcB, dst, IM_ALPHA_BLEND_SRC_OVER, 1); \
} else if (__argc == 1){ \
__ret = imblend_t(srcA, srcB, dst, (int)__args[RGA_GET_MIN(__argc, 0)], 1); \
} else if (__argc == 2){ \
__ret = imblend_t(srcA, srcB, dst, (int)__args[RGA_GET_MIN(__argc, 0)], (int)__args[RGA_GET_MIN(__argc, 1)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
#define imcomposite(srcA, srcB, dst, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = imblend_t(srcA, srcB, dst, IM_ALPHA_BLEND_SRC_OVER, 1); \
} else if (__argc == 1){ \
__ret = imblend_t(srcA, srcB, dst, (int)__args[RGA_GET_MIN(__argc, 0)], 1); \
} else if (__argc == 2){ \
__ret = imblend_t(srcA, srcB, dst, (int)__args[RGA_GET_MIN(__argc, 0)], (int)__args[RGA_GET_MIN(__argc, 1)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
IM_API IM_STATUS imblend_t(const rga_buffer_t srcA, const rga_buffer_t srcB, rga_buffer_t dst, int mode, int sync);
/*
* color key
*
* @param src
* @param dst
* @param colorkey_range
* max color
* min color
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#define imcolorkey(src, dst, range, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = imcolorkey_t(src, dst, range, IM_ALPHA_COLORKEY_NORMAL, 1); \
} else if (__argc == 1){ \
__ret = imcolorkey_t(src, dst, range, (int)__args[RGA_GET_MIN(__argc, 0)], 1); \
} else if (__argc == 2){ \
__ret = imcolorkey_t(src, dst, range, (int)__args[RGA_GET_MIN(__argc, 0)], (int)__args[RGA_GET_MIN(__argc, 1)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
IM_API IM_STATUS imcolorkey_t(const rga_buffer_t src, rga_buffer_t dst, im_colorkey_range range, int mode, int sync);
/*
* format convert
*
* @param src
* @param dst
* @param sfmt
* @param dfmt
* @param mode
* color space mode: IM_COLOR_SPACE_MODE
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#define imcvtcolor(src, dst, sfmt, dfmt, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = imcvtcolor_t(src, dst, sfmt, dfmt, IM_COLOR_SPACE_DEFAULT, 1); \
} else if (__argc == 1){ \
__ret = imcvtcolor_t(src, dst, sfmt, dfmt, (int)__args[RGA_GET_MIN(__argc, 0)], 1); \
} else if (__argc == 2){ \
__ret = imcvtcolor_t(src, dst, sfmt, dfmt, (int)__args[RGA_GET_MIN(__argc, 0)], (int)__args[RGA_GET_MIN(__argc, 1)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
IM_API IM_STATUS imcvtcolor_t(rga_buffer_t src, rga_buffer_t dst, int sfmt, int dfmt, int mode, int sync);
/*
* nn quantize
*
* @param src
* @param dst
* @param nninfo
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#define imquantize(src, dst, nn_info, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = imquantize_t(src, dst, nn_info, 1); \
} else if (__argc == 1){ \
__ret = imquantize_t(src, dst, nn_info, (int)__args[RGA_GET_MIN(__argc, 0)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
IM_API IM_STATUS imquantize_t(const rga_buffer_t src, rga_buffer_t dst, im_nn_t nn_info, int sync);
/*
* ROP
*
* @param src
* @param dst
* @param rop_code
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#define imrop(src, dst, rop_code, ...) \
({ \
IM_STATUS __ret = IM_STATUS_SUCCESS; \
int __args[] = {__VA_ARGS__}; \
int __argc = sizeof(__args)/sizeof(int); \
if (__argc == 0) { \
__ret = imrop_t(src, dst, rop_code, 1); \
} else if (__argc == 1){ \
__ret = imrop_t(src, dst, rop_code, (int)__args[RGA_GET_MIN(__argc, 0)]); \
} else { \
__ret = IM_STATUS_INVALID_PARAM; \
printf("invalid parameter\n"); \
} \
__ret; \
})
IM_API IM_STATUS imrop_t(const rga_buffer_t src, rga_buffer_t dst, int rop_code, int sync);
/*
* MOSAIC
*
* @param src
* @param dst
* @param mosaic_mode
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS immosaic(const rga_buffer_t image, im_rect rect, int mosaic_mode, int sync);
/*
* OSD
*
* @param osd
* osd block
* @param dst
* background image
* @param osd_rect
* @param osd_config
* osd mode config
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imosd(const rga_buffer_t osd,const rga_buffer_t dst,
const im_rect osd_rect, im_osd_t *osd_config, int sync);
/*
* process
*
* @param src
* @param dst
* @param usage
* @param ...
* wait until operation complete
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS improcess(rga_buffer_t src, rga_buffer_t dst, rga_buffer_t pat,
im_rect srect, im_rect drect, im_rect prect, int usage);
/*
* block until all execution is complete
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imsync(int out_fence_fd);
/*
* config
*
* @param name
* enum IM_CONFIG_NAME
* @param value
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imconfig(IM_CONFIG_NAME name, uint64_t value);
IM_API im_ctx_id_t imbegin(uint32_t flags);
IM_API IM_STATUS imcancel(im_ctx_id_t id);
#ifdef __cplusplus
}
#endif
#endif /* _im2d_h_ */

View File

@@ -0,0 +1,343 @@
/*
* Copyright (C) 2020 Rockchip Electronics Co., Ltd.
* Authors:
* PutinLee <putin.lee@rock-chips.com>
* Cerf Yu <cerf.yu@rock-chips.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _im2d_hpp_
#define _im2d_hpp_
#include "im2d.h"
#include "RgaUtils.h"
#ifdef ANDROID
#include <ui/GraphicBuffer.h>
using namespace android;
#endif
/*
* Import external buffers into RGA driver.
*
* @param fd/va/pa
* Select dma_fd/virtual_address/physical_address by buffer type
* @param size
* Describes the size of the image buffer
*
* @return rga_buffer_handle_t
*/
IM_API rga_buffer_handle_t importbuffer_fd(int fd, int size);
IM_API rga_buffer_handle_t importbuffer_virtualaddr(void *va, int size);
IM_API rga_buffer_handle_t importbuffer_physicaladdr(uint64_t pa, int size);
/*
* Import external buffers into RGA driver.
*
* @param fd/va/pa
* Select dma_fd/virtual_address/physical_address by buffer type
* @param width
* Describes the pixel width stride of the image buffer
* @param height
* Describes the pixel height stride of the image buffer
* @param format
* Describes the pixel format of the image buffer
*
* @return rga_buffer_handle_t
*/
IM_API rga_buffer_handle_t importbuffer_fd(int fd, int width, int height, int format);
IM_API rga_buffer_handle_t importbuffer_virtualaddr(void *va, int width, int height, int format);
IM_API rga_buffer_handle_t importbuffer_physicaladdr(uint64_t pa, int width, int height, int format);
#undef wrapbuffer_handle
IM_API rga_buffer_t wrapbuffer_handle(rga_buffer_handle_t handle,
int width, int height,
int wstride, int hstride,
int format);
IM_API rga_buffer_t wrapbuffer_handle(rga_buffer_handle_t handle,
int width, int height,
int format);
#if ANDROID
IM_API rga_buffer_handle_t importbuffer_GraphicBuffer_handle(buffer_handle_t hnd);
IM_API rga_buffer_handle_t importbuffer_GraphicBuffer(sp<GraphicBuffer> buf);
IM_API rga_buffer_t wrapbuffer_handle(buffer_handle_t hnd);
IM_API rga_buffer_t wrapbuffer_GraphicBuffer(sp<GraphicBuffer> buf);
#if USE_AHARDWAREBUFFER
#include <android/hardware_buffer.h>
IM_API rga_buffer_handle_t importbuffer_AHardwareBuffer(AHardwareBuffer *buf);
IM_API rga_buffer_t wrapbuffer_AHardwareBuffer(AHardwareBuffer *buf);
#endif /* USE_AHARDWAREBUFFER */
#endif /* ANDROID */
/*
* Resize
*
* @param src
* @param dst
* @param fx
* @param fy
* @param interpolation
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#undef imresize
IM_API IM_STATUS imresize(const rga_buffer_t src, rga_buffer_t dst, double fx = 0, double fy = 0, int interpolation = 0, int sync = 1, int *release_fence_fd = NULL);
/*
* Crop
*
* @param src
* @param dst
* @param rect
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#undef imcrop
IM_API IM_STATUS imcrop(const rga_buffer_t src, rga_buffer_t dst, im_rect rect, int sync = 1, int *release_fence_fd = NULL);
/*
* rotation
*
* @param src
* @param dst
* @param rotation
* IM_HAL_TRANSFORM_ROT_90
* IM_HAL_TRANSFORM_ROT_180
* IM_HAL_TRANSFORM_ROT_270
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#undef imrotate
IM_API IM_STATUS imrotate(const rga_buffer_t src, rga_buffer_t dst, int rotation, int sync = 1, int *release_fence_fd = NULL);
/*
* flip
*
* @param src
* @param dst
* @param mode
* IM_HAL_TRANSFORM_FLIP_H
* IM_HAL_TRANSFORM_FLIP_V
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#undef imflip
IM_API IM_STATUS imflip(const rga_buffer_t src, rga_buffer_t dst, int mode, int sync = 1, int *release_fence_fd = NULL);
/*
* fill/reset/draw
*
* @param src
* @param dst
* @param rect
* @param color
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#undef imfill
IM_API IM_STATUS imfill(rga_buffer_t dst, im_rect rect, int color, int sync = 1, int *release_fence_fd = NULL);
/*
* palette
*
* @param src
* @param dst
* @param lut
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#undef impalette
IM_API IM_STATUS impalette(rga_buffer_t src, rga_buffer_t dst, rga_buffer_t lut, int sync = 1, int *release_fence_fd = NULL);
/*
* translate
*
* @param src
* @param dst
* @param x
* @param y
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#undef imtranslate
IM_API IM_STATUS imtranslate(const rga_buffer_t src, rga_buffer_t dst, int x, int y, int sync = 1, int *release_fence_fd = NULL);
/*
* copy
*
* @param src
* @param dst
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#undef imcopy
IM_API IM_STATUS imcopy(const rga_buffer_t src, rga_buffer_t dst, int sync = 1, int *release_fence_fd = NULL);
/*
* blend (SRC + DST -> DST or SRCA + SRCB -> DST)
*
* @param srcA
* @param srcB can be NULL.
* @param dst
* @param mode
* IM_ALPHA_BLEND_MODE
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#undef imblend
IM_API IM_STATUS imblend(const rga_buffer_t src, rga_buffer_t dst, int mode = IM_ALPHA_BLEND_SRC_OVER, int sync = 1, int *release_fence_fd = NULL);
#undef imcomposite
IM_API IM_STATUS imcomposite(const rga_buffer_t srcA, const rga_buffer_t srcB, rga_buffer_t dst, int mode = IM_ALPHA_BLEND_SRC_OVER, int sync = 1, int *release_fence_fd = NULL);
/*
* color key
*
* @param src
* @param dst
* @param colorkey_range
* max color
* min color
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#undef imcolorkey
IM_API IM_STATUS imcolorkey(const rga_buffer_t src, rga_buffer_t dst, im_colorkey_range range, int mode = IM_ALPHA_COLORKEY_NORMAL, int sync = 1, int *release_fence_fd = NULL);
/*
* format convert
*
* @param src
* @param dst
* @param sfmt
* @param dfmt
* @param mode
* color space mode: IM_COLOR_SPACE_MODE
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#undef imcvtcolor
IM_API IM_STATUS imcvtcolor(rga_buffer_t src, rga_buffer_t dst, int sfmt, int dfmt, int mode = IM_COLOR_SPACE_DEFAULT, int sync = 1, int *release_fence_fd = NULL);
/*
* nn quantize
*
* @param src
* @param dst
* @param nninfo
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#undef imquantize
IM_API IM_STATUS imquantize(const rga_buffer_t src, rga_buffer_t dst, im_nn_t nn_info, int sync = 1, int *release_fence_fd = NULL);
/*
* ROP
*
* @param src
* @param dst
* @param rop_code
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
#undef imrop
IM_API IM_STATUS imrop(const rga_buffer_t src, rga_buffer_t dst, int rop_code, int sync = 1, int *release_fence_fd = NULL);
/*
* MOSAIC
*
* @param src
* @param dst
* @param mosaic_mode
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS immosaic(const rga_buffer_t image, im_rect rect, int mosaic_mode, int sync = 1, int *release_fence_fd = NULL);
/*
* OSD
*
* @param osd
* osd block
* @param dst
* background image
* @param osd_rect
* @param osd_config
* osd mode config
* @param sync
* wait until operation complete
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS imosd(const rga_buffer_t osd,const rga_buffer_t dst,
const im_rect osd_rect, im_osd_t *osd_config,
int sync = 1, int *release_fence_fd = NULL);
/*
* process
*
* @param src
* @param dst
* @param pat
* @param srect
* @param drect
* @param prect
* @param acquire_fence_fd
* @param release_fence_fd
* @param opt
* @param usage
*
* @returns success or else negative error code.
*/
IM_API IM_STATUS improcess(rga_buffer_t src, rga_buffer_t dst, rga_buffer_t pat,
im_rect srect, im_rect drect, im_rect prect,
int acquire_fence_fd, int *release_fence_fd, im_opt_t *opt_ptr, int usage);
IM_API IM_STATUS improcess(rga_buffer_t src, rga_buffer_t dst, rga_buffer_t pat,
im_rect srect, im_rect drect, im_rect prect,
int acquire_fence_fd, int *release_fence_fd, im_opt_t *opt, int usage, im_ctx_id_t ctx_id);
#endif /* _im2d_hpp_ */

View File

@@ -0,0 +1,312 @@
/*
* Copyright (C) 2021 Rockchip Electronics Co., Ltd.
* Authors:
* Cerf Yu <cerf.yu@rock-chips.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _RGA_IM2D_HARDWARE_H_
#define _RGA_IM2D_HARDWARE_H_
#include "rga.h"
typedef enum {
IM_RGA_HW_VERSION_RGA_V_ERR_INDEX = 0x0,
IM_RGA_HW_VERSION_RGA_1_INDEX,
IM_RGA_HW_VERSION_RGA_1_PLUS_INDEX,
IM_RGA_HW_VERSION_RGA_2_INDEX,
IM_RGA_HW_VERSION_RGA_2_LITE0_INDEX,
IM_RGA_HW_VERSION_RGA_2_LITE1_INDEX,
IM_RGA_HW_VERSION_RGA_2_ENHANCE_INDEX,
IM_RGA_HW_VERSION_RGA_3_INDEX,
IM_RGA_HW_VERSION_MASK_INDEX,
} IM_RGA_HW_VERSION_INDEX;
typedef enum {
IM_RGA_HW_VERSION_RGA_V_ERR = 1 << IM_RGA_HW_VERSION_RGA_V_ERR_INDEX,
IM_RGA_HW_VERSION_RGA_1 = 1 << IM_RGA_HW_VERSION_RGA_1_INDEX,
IM_RGA_HW_VERSION_RGA_1_PLUS = 1 << IM_RGA_HW_VERSION_RGA_1_PLUS_INDEX,
IM_RGA_HW_VERSION_RGA_2 = 1 << IM_RGA_HW_VERSION_RGA_2_INDEX,
IM_RGA_HW_VERSION_RGA_2_LITE0 = 1 << IM_RGA_HW_VERSION_RGA_2_LITE0_INDEX,
IM_RGA_HW_VERSION_RGA_2_LITE1 = 1 << IM_RGA_HW_VERSION_RGA_2_LITE1_INDEX,
IM_RGA_HW_VERSION_RGA_2_ENHANCE = 1 << IM_RGA_HW_VERSION_RGA_2_ENHANCE_INDEX,
IM_RGA_HW_VERSION_RGA_3 = 1 << IM_RGA_HW_VERSION_RGA_3_INDEX,
IM_RGA_HW_VERSION_MASK = ~((~(unsigned int)0x0 << IM_RGA_HW_VERSION_MASK_INDEX) | 1),
}IM_RGA_HW_VERSION;
typedef enum {
IM_RGA_SUPPORT_FORMAT_ERROR_INDEX = 0,
IM_RGA_SUPPORT_FORMAT_RGB_INDEX,
IM_RGA_SUPPORT_FORMAT_RGB_OTHER_INDEX,
IM_RGA_SUPPORT_FORMAT_BPP_INDEX,
IM_RGA_SUPPORT_FORMAT_YUV_420_SEMI_PLANNER_8_BIT_INDEX,
IM_RGA_SUPPORT_FORMAT_YUV_420_SEMI_PLANNER_10_BIT_INDEX,
IM_RGA_SUPPORT_FORMAT_YUV_420_PLANNER_8_BIT_INDEX,
IM_RGA_SUPPORT_FORMAT_YUV_420_PLANNER_10_BIT_INDEX,
IM_RGA_SUPPORT_FORMAT_YUV_422_SEMI_PLANNER_8_BIT_INDEX,
IM_RGA_SUPPORT_FORMAT_YUV_422_SEMI_PLANNER_10_BIT_INDEX,
IM_RGA_SUPPORT_FORMAT_YUV_422_PLANNER_8_BIT_INDEX,
IM_RGA_SUPPORT_FORMAT_YUV_422_PLANNER_10_BIT_INDEX,
IM_RGA_SUPPORT_FORMAT_YUYV_420_INDEX,
IM_RGA_SUPPORT_FORMAT_YUYV_422_INDEX,
IM_RGA_SUPPORT_FORMAT_YUV_400_INDEX,
IM_RGA_SUPPORT_FORMAT_Y4_INDEX,
IM_RGA_SUPPORT_FORMAT_RGBA2BPP_INDEX,
IM_RGA_SUPPORT_FORMAT_MASK_INDEX,
} IM_RGA_SUPPORT_FORMAT_INDEX;
typedef enum {
IM_RGA_SUPPORT_FORMAT_ERROR = 1 << IM_RGA_SUPPORT_FORMAT_ERROR_INDEX,
IM_RGA_SUPPORT_FORMAT_RGB = 1 << IM_RGA_SUPPORT_FORMAT_RGB_INDEX,
IM_RGA_SUPPORT_FORMAT_RGB_OTHER = 1 << IM_RGA_SUPPORT_FORMAT_RGB_OTHER_INDEX,
IM_RGA_SUPPORT_FORMAT_BPP = 1 << IM_RGA_SUPPORT_FORMAT_BPP_INDEX,
IM_RGA_SUPPORT_FORMAT_YUV_420_SEMI_PLANNER_8_BIT = 1 << IM_RGA_SUPPORT_FORMAT_YUV_420_SEMI_PLANNER_8_BIT_INDEX,
IM_RGA_SUPPORT_FORMAT_YUV_420_SEMI_PLANNER_10_BIT = 1 << IM_RGA_SUPPORT_FORMAT_YUV_420_SEMI_PLANNER_10_BIT_INDEX,
IM_RGA_SUPPORT_FORMAT_YUV_420_PLANNER_8_BIT = 1 << IM_RGA_SUPPORT_FORMAT_YUV_420_PLANNER_8_BIT_INDEX,
IM_RGA_SUPPORT_FORMAT_YUV_420_PLANNER_10_BIT = 1 << IM_RGA_SUPPORT_FORMAT_YUV_420_PLANNER_10_BIT_INDEX,
IM_RGA_SUPPORT_FORMAT_YUV_422_SEMI_PLANNER_8_BIT = 1 << IM_RGA_SUPPORT_FORMAT_YUV_422_SEMI_PLANNER_8_BIT_INDEX,
IM_RGA_SUPPORT_FORMAT_YUV_422_SEMI_PLANNER_10_BIT = 1 << IM_RGA_SUPPORT_FORMAT_YUV_422_SEMI_PLANNER_10_BIT_INDEX,
IM_RGA_SUPPORT_FORMAT_YUV_422_PLANNER_8_BIT = 1 << IM_RGA_SUPPORT_FORMAT_YUV_422_PLANNER_8_BIT_INDEX,
IM_RGA_SUPPORT_FORMAT_YUV_422_PLANNER_10_BIT = 1 << IM_RGA_SUPPORT_FORMAT_YUV_422_PLANNER_10_BIT_INDEX,
IM_RGA_SUPPORT_FORMAT_YUYV_420 = 1 << IM_RGA_SUPPORT_FORMAT_YUYV_420_INDEX,
IM_RGA_SUPPORT_FORMAT_YUYV_422 = 1 << IM_RGA_SUPPORT_FORMAT_YUYV_422_INDEX,
IM_RGA_SUPPORT_FORMAT_YUV_400 = 1 << IM_RGA_SUPPORT_FORMAT_YUV_400_INDEX,
IM_RGA_SUPPORT_FORMAT_Y4 = 1 << IM_RGA_SUPPORT_FORMAT_Y4_INDEX,
IM_RGA_SUPPORT_FORMAT_RGBA2BPP = 1 << IM_RGA_SUPPORT_FORMAT_RGBA2BPP_INDEX,
IM_RGA_SUPPORT_FORMAT_MASK = ~((~(unsigned int)0x0 << IM_RGA_SUPPORT_FORMAT_MASK_INDEX) | 1),
} IM_RGA_SUPPORT_FORMAT;
typedef enum {
IM_RGA_SUPPORT_FEATURE_ERROR_INDEX = 0,
IM_RGA_SUPPORT_FEATURE_COLOR_FILL_INDEX,
IM_RGA_SUPPORT_FEATURE_COLOR_PALETTE_INDEX,
IM_RGA_SUPPORT_FEATURE_ROP_INDEX,
IM_RGA_SUPPORT_FEATURE_QUANTIZE_INDEX,
IM_RGA_SUPPORT_FEATURE_SRC1_R2Y_CSC_INDEX,
IM_RGA_SUPPORT_FEATURE_DST_FULL_CSC_INDEX,
IM_RGA_SUPPORT_FEATURE_FBC_INDEX,
IM_RGA_SUPPORT_FEATURE_BLEND_YUV_INDEX,
IM_RGA_SUPPORT_FEATURE_BT2020_INDEX,
IM_RGA_SUPPORT_FEATURE_MOSAIC_INDEX,
IM_RGA_SUPPORT_FEATURE_OSD_INDEX,
IM_RGA_SUPPORT_FEATURE_PRE_INTR_INDEX,
IM_RGA_SUPPORT_FEATURE_MASK_INDEX,
} IM_RGA_SUPPORT_FEATURE_INDEX;
typedef enum {
IM_RGA_SUPPORT_FEATURE_ERROR = 1 << IM_RGA_SUPPORT_FEATURE_ERROR_INDEX,
IM_RGA_SUPPORT_FEATURE_COLOR_FILL = 1 << IM_RGA_SUPPORT_FEATURE_COLOR_FILL_INDEX,
IM_RGA_SUPPORT_FEATURE_COLOR_PALETTE = 1 << IM_RGA_SUPPORT_FEATURE_COLOR_PALETTE_INDEX,
IM_RGA_SUPPORT_FEATURE_ROP = 1 << IM_RGA_SUPPORT_FEATURE_ROP_INDEX,
IM_RGA_SUPPORT_FEATURE_QUANTIZE = 1 << IM_RGA_SUPPORT_FEATURE_QUANTIZE_INDEX,
IM_RGA_SUPPORT_FEATURE_SRC1_R2Y_CSC = 1 << IM_RGA_SUPPORT_FEATURE_SRC1_R2Y_CSC_INDEX,
IM_RGA_SUPPORT_FEATURE_DST_FULL_CSC = 1 << IM_RGA_SUPPORT_FEATURE_DST_FULL_CSC_INDEX,
IM_RGA_SUPPORT_FEATURE_FBC = 1 << IM_RGA_SUPPORT_FEATURE_FBC_INDEX,
IM_RGA_SUPPORT_FEATURE_BLEND_YUV = 1 << IM_RGA_SUPPORT_FEATURE_BLEND_YUV_INDEX,
IM_RGA_SUPPORT_FEATURE_BT2020 = 1 << IM_RGA_SUPPORT_FEATURE_BT2020_INDEX,
IM_RGA_SUPPORT_FEATURE_MOSAIC = 1 << IM_RGA_SUPPORT_FEATURE_MOSAIC_INDEX,
IM_RGA_SUPPORT_FEATURE_OSD = 1 << IM_RGA_SUPPORT_FEATURE_OSD_INDEX,
IM_RGA_SUPPORT_FEATURE_PRE_INTR = 1 << IM_RGA_SUPPORT_FEATURE_PRE_INTR_INDEX,
IM_RGA_SUPPORT_FEATURE_MASK = ~((~(unsigned int)0x0 << IM_RGA_SUPPORT_FEATURE_MASK_INDEX) | 1),
} IM_RGA_SUPPORT_FEATURE;
typedef struct {
unsigned int version;
unsigned int input_resolution;
unsigned int output_resolution;
unsigned int byte_stride;
unsigned int scale_limit;
unsigned int performance;
unsigned int input_format;
unsigned int output_format;
unsigned int feature;
char reserved[24];
} rga_info_table_entry;
typedef struct {
struct rga_version_t user;
struct rga_version_t driver;
} rga_dirver_bind_table_entry;
const rga_info_table_entry hw_info_table[] = {
{ IM_RGA_HW_VERSION_RGA_V_ERR , 0, 0, 0, 0, 0, 0, 0, 0, {0} },
{ IM_RGA_HW_VERSION_RGA_1 , 8192, 2048, 4, 8, 1,
/* input format */
IM_RGA_SUPPORT_FORMAT_RGB |
IM_RGA_SUPPORT_FORMAT_RGB_OTHER |
IM_RGA_SUPPORT_FORMAT_BPP |
IM_RGA_SUPPORT_FORMAT_YUV_420_SEMI_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_420_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_SEMI_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_PLANNER_8_BIT,
/* output format */
IM_RGA_SUPPORT_FORMAT_RGB |
IM_RGA_SUPPORT_FORMAT_RGB_OTHER |
IM_RGA_SUPPORT_FORMAT_YUV_420_SEMI_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_420_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_SEMI_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_PLANNER_8_BIT,
/* feature */
IM_RGA_SUPPORT_FEATURE_COLOR_FILL |
IM_RGA_SUPPORT_FEATURE_COLOR_PALETTE |
IM_RGA_SUPPORT_FEATURE_ROP,
/* reserved */
{0} },
{ IM_RGA_HW_VERSION_RGA_1_PLUS , 8192, 2048, 4, 8, 1,
/* input format */
IM_RGA_SUPPORT_FORMAT_RGB |
IM_RGA_SUPPORT_FORMAT_RGB_OTHER |
IM_RGA_SUPPORT_FORMAT_BPP |
IM_RGA_SUPPORT_FORMAT_YUV_420_SEMI_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_420_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_SEMI_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_PLANNER_8_BIT,
/* output format */
IM_RGA_SUPPORT_FORMAT_RGB |
IM_RGA_SUPPORT_FORMAT_RGB_OTHER |
IM_RGA_SUPPORT_FORMAT_YUV_420_SEMI_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_420_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_SEMI_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_PLANNER_8_BIT,
/* feature */
IM_RGA_SUPPORT_FEATURE_COLOR_FILL |
IM_RGA_SUPPORT_FEATURE_COLOR_PALETTE,
/* reserved */
{0} },
{ IM_RGA_HW_VERSION_RGA_2 , 8192, 4096, 4, 16, 2,
/* input format */
IM_RGA_SUPPORT_FORMAT_RGB |
IM_RGA_SUPPORT_FORMAT_RGB_OTHER |
IM_RGA_SUPPORT_FORMAT_YUV_420_SEMI_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_420_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_SEMI_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_PLANNER_8_BIT,
/* output format */
IM_RGA_SUPPORT_FORMAT_RGB |
IM_RGA_SUPPORT_FORMAT_RGB_OTHER |
IM_RGA_SUPPORT_FORMAT_YUV_420_SEMI_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_420_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_SEMI_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_PLANNER_8_BIT,
/* feature */
IM_RGA_SUPPORT_FEATURE_COLOR_FILL |
IM_RGA_SUPPORT_FEATURE_COLOR_PALETTE |
IM_RGA_SUPPORT_FEATURE_ROP,
/* reserved */
{0} },
{ IM_RGA_HW_VERSION_RGA_2_LITE0 , 8192, 4096, 4, 8, 2,
/* input format */
IM_RGA_SUPPORT_FORMAT_RGB |
IM_RGA_SUPPORT_FORMAT_RGB_OTHER |
IM_RGA_SUPPORT_FORMAT_YUV_420_SEMI_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_420_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_SEMI_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_PLANNER_8_BIT,
/* output format */
IM_RGA_SUPPORT_FORMAT_RGB |
IM_RGA_SUPPORT_FORMAT_RGB_OTHER |
IM_RGA_SUPPORT_FORMAT_YUV_420_SEMI_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_420_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_SEMI_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_PLANNER_8_BIT,
/* feature */
IM_RGA_SUPPORT_FEATURE_COLOR_FILL |
IM_RGA_SUPPORT_FEATURE_COLOR_PALETTE |
IM_RGA_SUPPORT_FEATURE_ROP,
/* reserved */
{0} },
{ IM_RGA_HW_VERSION_RGA_2_LITE1 , 8192, 4096, 4, 8, 2,
/* input format */
IM_RGA_SUPPORT_FORMAT_RGB |
IM_RGA_SUPPORT_FORMAT_RGB_OTHER |
IM_RGA_SUPPORT_FORMAT_YUV_420_SEMI_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_420_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_SEMI_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_420_SEMI_PLANNER_10_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_420_PLANNER_10_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_SEMI_PLANNER_10_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_PLANNER_10_BIT,
/* output format */
IM_RGA_SUPPORT_FORMAT_RGB |
IM_RGA_SUPPORT_FORMAT_RGB_OTHER |
IM_RGA_SUPPORT_FORMAT_YUV_420_SEMI_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_420_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_SEMI_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_PLANNER_8_BIT,
/* feature */
IM_RGA_SUPPORT_FEATURE_COLOR_FILL |
IM_RGA_SUPPORT_FEATURE_COLOR_PALETTE,
/* reserved */
{0} },
{ IM_RGA_HW_VERSION_RGA_2_ENHANCE , 8192, 4096, 4, 16, 2,
/* input format */
IM_RGA_SUPPORT_FORMAT_RGB |
IM_RGA_SUPPORT_FORMAT_RGB_OTHER |
IM_RGA_SUPPORT_FORMAT_YUV_420_SEMI_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_420_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_SEMI_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_420_SEMI_PLANNER_10_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_420_PLANNER_10_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_SEMI_PLANNER_10_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_PLANNER_10_BIT,
/* output format */
IM_RGA_SUPPORT_FORMAT_RGB |
IM_RGA_SUPPORT_FORMAT_RGB_OTHER |
IM_RGA_SUPPORT_FORMAT_YUV_420_SEMI_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_420_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_SEMI_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUYV_420 |
IM_RGA_SUPPORT_FORMAT_YUYV_422,
/* feature */
IM_RGA_SUPPORT_FEATURE_COLOR_FILL |
IM_RGA_SUPPORT_FEATURE_COLOR_PALETTE |
IM_RGA_SUPPORT_FEATURE_ROP,
/* reserved */
{0} },
{ IM_RGA_HW_VERSION_RGA_3 , 8176, 8128, 16, 8, 4,
/* input format */
IM_RGA_SUPPORT_FORMAT_RGB |
IM_RGA_SUPPORT_FORMAT_YUV_420_SEMI_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_SEMI_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_420_SEMI_PLANNER_10_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_SEMI_PLANNER_10_BIT |
IM_RGA_SUPPORT_FORMAT_YUYV_422,
/* output format */
IM_RGA_SUPPORT_FORMAT_RGB |
IM_RGA_SUPPORT_FORMAT_YUV_420_SEMI_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_SEMI_PLANNER_8_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_420_SEMI_PLANNER_10_BIT |
IM_RGA_SUPPORT_FORMAT_YUV_422_SEMI_PLANNER_10_BIT |
IM_RGA_SUPPORT_FORMAT_YUYV_422,
/* feature */
IM_RGA_SUPPORT_FEATURE_FBC |
IM_RGA_SUPPORT_FEATURE_BLEND_YUV |
IM_RGA_SUPPORT_FEATURE_BT2020,
/* reserved */
{0} },
};
/* The range of the version is [min, max), that is version >= min, version < max. */
const rga_dirver_bind_table_entry driver_bind_table[] = {
{ { 0, 0, 0, "0.0.0" }, {0, 0, 0, "0.0.0" } },
{ { 1, 0, 3, "1.0.3" }, {0, 0, 0, "0.0.0" } },
{ { 1, 6, 0, "1.6.0" }, {1, 1, 5, "1.1.5" } },
{ { 1, 7, 2, "1.7.2" }, {1, 2, 0, "1.2.0" } },
{ { 1, 7, 3, "1.7.3" }, {1, 2, 4, "1.2.4" } },
};
#endif

View File

@@ -0,0 +1,417 @@
/*
* Copyright (C) 2022 Rockchip Electronics Co., Ltd.
* Authors:
* Cerf Yu <cerf.yu@rock-chips.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _RGA_IM2D_TYPE_H_
#define _RGA_IM2D_TYPE_H_
#include <stdint.h>
typedef enum {
/* Rotation */
IM_HAL_TRANSFORM_ROT_90 = 1 << 0,
IM_HAL_TRANSFORM_ROT_180 = 1 << 1,
IM_HAL_TRANSFORM_ROT_270 = 1 << 2,
IM_HAL_TRANSFORM_FLIP_H = 1 << 3,
IM_HAL_TRANSFORM_FLIP_V = 1 << 4,
IM_HAL_TRANSFORM_FLIP_H_V = 1 << 5,
IM_HAL_TRANSFORM_MASK = 0x3f,
/*
* Blend
* Additional blend usage, can be used with both source and target configs.
* If none of the below is set, the default "SRC over DST" is applied.
*/
IM_ALPHA_BLEND_SRC_OVER = 1 << 6, /* Default, Porter-Duff "SRC over DST" */
IM_ALPHA_BLEND_SRC = 1 << 7, /* Porter-Duff "SRC" */
IM_ALPHA_BLEND_DST = 1 << 8, /* Porter-Duff "DST" */
IM_ALPHA_BLEND_SRC_IN = 1 << 9, /* Porter-Duff "SRC in DST" */
IM_ALPHA_BLEND_DST_IN = 1 << 10, /* Porter-Duff "DST in SRC" */
IM_ALPHA_BLEND_SRC_OUT = 1 << 11, /* Porter-Duff "SRC out DST" */
IM_ALPHA_BLEND_DST_OUT = 1 << 12, /* Porter-Duff "DST out SRC" */
IM_ALPHA_BLEND_DST_OVER = 1 << 13, /* Porter-Duff "DST over SRC" */
IM_ALPHA_BLEND_SRC_ATOP = 1 << 14, /* Porter-Duff "SRC ATOP" */
IM_ALPHA_BLEND_DST_ATOP = 1 << 15, /* Porter-Duff "DST ATOP" */
IM_ALPHA_BLEND_XOR = 1 << 16, /* Xor */
IM_ALPHA_BLEND_MASK = 0x1ffc0,
IM_ALPHA_COLORKEY_NORMAL = 1 << 17,
IM_ALPHA_COLORKEY_INVERTED = 1 << 18,
IM_ALPHA_COLORKEY_MASK = 0x60000,
IM_SYNC = 1 << 19,
IM_CROP = 1 << 20, /* Unused */
IM_COLOR_FILL = 1 << 21,
IM_COLOR_PALETTE = 1 << 22,
IM_NN_QUANTIZE = 1 << 23,
IM_ROP = 1 << 24,
IM_ALPHA_BLEND_PRE_MUL = 1 << 25,
IM_ASYNC = 1 << 26,
IM_MOSAIC = 1 << 27,
IM_OSD = 1 << 28,
IM_PRE_INTR = 1 << 29,
} IM_USAGE;
typedef enum {
IM_RASTER_MODE = 1 << 0,
IM_FBC_MODE = 1 << 1,
IM_TILE_MODE = 1 << 2,
} IM_RD_MODE;
typedef enum {
IM_SCHEDULER_RGA3_CORE0 = 1 << 0,
IM_SCHEDULER_RGA3_CORE1 = 1 << 1,
IM_SCHEDULER_RGA2_CORE0 = 1 << 2,
IM_SCHEDULER_RGA3_DEFAULT = IM_SCHEDULER_RGA3_CORE0,
IM_SCHEDULER_RGA2_DEFAULT = IM_SCHEDULER_RGA2_CORE0,
IM_SCHEDULER_MASK = 0x7,
IM_SCHEDULER_DEFAULT = 0,
} IM_SCHEDULER_CORE;
typedef enum {
IM_ROP_AND = 0x88,
IM_ROP_OR = 0xee,
IM_ROP_NOT_DST = 0x55,
IM_ROP_NOT_SRC = 0x33,
IM_ROP_XOR = 0xf6,
IM_ROP_NOT_XOR = 0xf9,
} IM_ROP_CODE;
typedef enum {
IM_MOSAIC_8 = 0x0,
IM_MOSAIC_16 = 0x1,
IM_MOSAIC_32 = 0x2,
IM_MOSAIC_64 = 0x3,
IM_MOSAIC_128 = 0x4,
} IM_MOSAIC_MODE;
/* Status codes, returned by any blit function */
typedef enum {
IM_YUV_TO_RGB_BT601_LIMIT = 1 << 0,
IM_YUV_TO_RGB_BT601_FULL = 2 << 0,
IM_YUV_TO_RGB_BT709_LIMIT = 3 << 0,
IM_YUV_TO_RGB_MASK = 3 << 0,
IM_RGB_TO_YUV_BT601_FULL = 1 << 2,
IM_RGB_TO_YUV_BT601_LIMIT = 2 << 2,
IM_RGB_TO_YUV_BT709_LIMIT = 3 << 2,
IM_RGB_TO_YUV_MASK = 3 << 2,
IM_RGB_TO_Y4 = 1 << 4,
IM_RGB_TO_Y4_DITHER = 2 << 4,
IM_RGB_TO_Y1_DITHER = 3 << 4,
IM_Y4_MASK = 3 << 4,
IM_RGB_FULL = 1 << 8,
IM_RGB_CLIP = 2 << 8,
IM_YUV_BT601_LIMIT_RANGE = 3 << 8,
IM_YUV_BT601_FULL_RANGE = 4 << 8,
IM_YUV_BT709_LIMIT_RANGE = 5 << 8,
IM_YUV_BT709_FULL_RANGE = 6 << 8,
IM_FULL_CSC_MASK = 0xf << 8,
IM_COLOR_SPACE_DEFAULT = 0,
} IM_COLOR_SPACE_MODE;
typedef enum {
IM_UP_SCALE,
IM_DOWN_SCALE,
} IM_SCALE;
typedef enum {
INTER_NEAREST,
INTER_LINEAR,
INTER_CUBIC,
} IM_SCALE_MODE;
typedef enum {
IM_CONFIG_SCHEDULER_CORE,
IM_CONFIG_PRIORITY,
IM_CONFIG_CHECK,
} IM_CONFIG_NAME;
typedef enum {
IM_OSD_MODE_STATISTICS = 0x1 << 0,
IM_OSD_MODE_AUTO_INVERT = 0x1 << 1,
} IM_OSD_MODE;
typedef enum {
IM_OSD_INVERT_CHANNEL_NONE = 0x0,
IM_OSD_INVERT_CHANNEL_Y_G = 0x1 << 0,
IM_OSD_INVERT_CHANNEL_C_RB = 0x1 << 1,
IM_OSD_INVERT_CHANNEL_ALPHA = 0x1 << 2,
IM_OSD_INVERT_CHANNEL_COLOR = IM_OSD_INVERT_CHANNEL_Y_G |
IM_OSD_INVERT_CHANNEL_C_RB,
IM_OSD_INVERT_CHANNEL_BOTH = IM_OSD_INVERT_CHANNEL_COLOR |
IM_OSD_INVERT_CHANNEL_ALPHA,
} IM_OSD_INVERT_CHANNEL;
typedef enum {
IM_OSD_FLAGS_INTERNAL = 0,
IM_OSD_FLAGS_EXTERNAL,
} IM_OSD_FLAGS_MODE;
typedef enum {
IM_OSD_INVERT_USE_FACTOR,
IM_OSD_INVERT_USE_SWAP,
} IM_OSD_INVERT_MODE;
typedef enum {
IM_OSD_BACKGROUND_DEFAULT_BRIGHT = 0,
IM_OSD_BACKGROUND_DEFAULT_DARK,
} IM_OSD_BACKGROUND_DEFAULT;
typedef enum {
IM_OSD_BLOCK_MODE_NORMAL = 0,
IM_OSD_BLOCK_MODE_DIFFERENT,
} IM_OSD_BLOCK_WIDTH_MODE;
typedef enum {
IM_OSD_MODE_HORIZONTAL,
IM_OSD_MODE_VERTICAL,
} IM_OSD_DIRECTION;
typedef enum {
IM_OSD_COLOR_PIXEL,
IM_OSD_COLOR_EXTERNAL,
} IM_OSD_COLOR_MODE;
typedef enum {
IM_INTR_READ_INTR = 1 << 0,
IM_INTR_READ_HOLD = 1 << 1,
IM_INTR_WRITE_INTR = 1 << 2,
} IM_PRE_INTR_FLAGS;
typedef enum {
IM_CONTEXT_NONE = 0x0,
IM_CONTEXT_SRC_FIX_ENABLE = 0x1 << 0, // Enable kernel to modify the image parameters of the channel.
IM_CONTEXT_SRC_CACHE_INFO = 0x1 << 1, // It will replace the parameters in ctx with the modified parameters.
IM_CONTEXT_SRC1_FIX_ENABLE = 0x1 << 2,
IM_CONTEXT_SRC1_CACHE_INFO = 0x1 << 3,
IM_CONTEXT_DST_FIX_ENABLE = 0x1 << 4,
IM_CONTEXT_DST_CACHE_INFO = 0x1 << 5,
} IM_CONTEXT_FLAGS;
/* Get RGA basic information index */
typedef enum {
RGA_VENDOR = 0,
RGA_VERSION,
RGA_MAX_INPUT,
RGA_MAX_OUTPUT,
RGA_BYTE_STRIDE,
RGA_SCALE_LIMIT,
RGA_INPUT_FORMAT,
RGA_OUTPUT_FORMAT,
RGA_FEATURE,
RGA_EXPECTED,
RGA_ALL,
} IM_INFORMATION;
/* Status codes, returned by any blit function */
typedef enum {
IM_STATUS_NOERROR = 2,
IM_STATUS_SUCCESS = 1,
IM_STATUS_NOT_SUPPORTED = -1,
IM_STATUS_OUT_OF_MEMORY = -2,
IM_STATUS_INVALID_PARAM = -3,
IM_STATUS_ILLEGAL_PARAM = -4,
IM_STATUS_ERROR_VERSION = -5,
IM_STATUS_FAILED = 0,
} IM_STATUS;
typedef uint32_t im_api_version_t;
typedef uint32_t im_ctx_id_t;
typedef uint32_t rga_buffer_handle_t;
/* Rectangle definition */
typedef struct {
int x; /* upper-left x */
int y; /* upper-left y */
int width; /* width */
int height; /* height */
} im_rect;
typedef struct {
int max; /* The Maximum value of the color key */
int min; /* The minimum value of the color key */
} im_colorkey_range;
typedef struct im_nn {
int scale_r; /* scaling factor on R channal */
int scale_g; /* scaling factor on G channal */
int scale_b; /* scaling factor on B channal */
int offset_r; /* offset on R channal */
int offset_g; /* offset on G channal */
int offset_b; /* offset on B channal */
} im_nn_t;
/* im_info definition */
typedef struct {
void* vir_addr; /* virtual address */
void* phy_addr; /* physical address */
int fd; /* shared fd */
int width; /* width */
int height; /* height */
int wstride; /* wstride */
int hstride; /* hstride */
int format; /* format */
int color_space_mode; /* color_space_mode */
int global_alpha; /* global_alpha */
int rd_mode;
/* legarcy */
int color; /* color, used by color fill */
im_colorkey_range colorkey_range; /* range value of color key */
im_nn_t nn;
int rop_code;
rga_buffer_handle_t handle; /* buffer handle */
} rga_buffer_t;
typedef struct im_color {
union {
struct {
uint8_t red;
uint8_t green;
uint8_t blue;
uint8_t alpha;
};
uint32_t value;
};
} im_color_t;
typedef struct im_osd_invert_factor {
uint8_t alpha_max;
uint8_t alpha_min;
uint8_t yg_max;
uint8_t yg_min;
uint8_t crb_max;
uint8_t crb_min;
} im_osd_invert_factor_t;
typedef struct im_osd_bpp2 {
uint8_t ac_swap; // ac swap flag
// 0: CA
// 1: AC
uint8_t endian_swap; // rgba2bpp endian swap
// 0: Big endian
// 1: Little endian
im_color_t color0;
im_color_t color1;
} im_osd_bpp2_t;
typedef struct im_osd_block {
int width_mode; // normal or different
// IM_OSD_BLOCK_MODE_NORMAL
// IM_OSD_BLOCK_MODE_DIFFERENT
union {
int width; // normal_mode block width
int width_index; // different_mode block width index in RAM
};
int block_count; // block count
int background_config; // background config is bright or dark
// IM_OSD_BACKGROUND_DEFAULT_BRIGHT
// IM_OSD_BACKGROUND_DEFAULT_DARK
int direction; // osd block direction
// IM_OSD_MODE_HORIZONTAL
// IM_OSD_MODE_VERTICAL
int color_mode; // using src1 color or config color
// IM_OSD_COLOR_PIXEL
// IM_OSD_COLOR_EXTERNAL
im_color_t normal_color; // config color: normal
im_color_t invert_color; // config color: invert
} im_osd_block_t;
typedef struct im_osd_invert {
int invert_channel; // invert channel config:
// IM_OSD_INVERT_CHANNEL_NONE
// IM_OSD_INVERT_CHANNEL_Y_G
// IM_OSD_INVERT_CHANNEL_C_RB
// IM_OSD_INVERT_CHANNEL_ALPHA
// IM_OSD_INVERT_CHANNEL_COLOR
// IM_OSD_INVERT_CHANNEL_BOTH
int flags_mode; // use external or inertnal RAM invert flags
// IM_OSD_FLAGS_EXTERNAL
// IM_OSD_FLAGS_INTERNAL
int flags_index; // flags index when using internal RAM invert flags
uint64_t invert_flags; // external invert flags
uint64_t current_flags; // current flags
int invert_mode; // invert use swap or factor
// IM_OSD_INVERT_USE_FACTOR
// IM_OSD_INVERT_USE_SWAP
im_osd_invert_factor_t factor;
int threash;
} im_osd_invert_t;
typedef struct im_osd {
int osd_mode; // osd mode: statistics or auto_invert
// IM_OSD_MODE_STATISTICS
// IM_OSD_MODE_AUTO_INVERT
im_osd_block_t block_parm; // osd block info config
im_osd_invert_t invert_config;
im_osd_bpp2_t bpp2_info;
} im_osd_t;
typedef struct im_intr_config {
uint32_t flags;
int read_threshold;
int write_start;
int write_step;
} im_intr_config_t;
typedef struct im_opt {
im_api_version_t version;
int color; /* color, used by color fill */
im_colorkey_range colorkey_range; /* range value of color key */
im_nn_t nn;
int rop_code;
int priority;
int core;
int mosaic_mode;
im_osd_t osd_config;
im_intr_config_t intr_config;
char reserve[128];
} im_opt_t;
typedef struct im_context {
int priority;
IM_SCHEDULER_CORE core;
int check_mode;
} im_context_t;
typedef struct im_handle_param {
uint32_t width;
uint32_t height;
uint32_t format;
}im_handle_param_t;
#endif /* _RGA_IM2D_TYPE_H_ */

View File

@@ -0,0 +1,45 @@
/*
* Copyright (C) 2022 Rockchip Electronics Co., Ltd.
* Authors:
* Cerf Yu <cerf.yu@rock-chips.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _RGA_IM2D_VERSION_H_
#define _RGA_IM2D_VERSION_H_
#define RGA_VERSION_STR_HELPER(x) #x
#define RGA_VERSION_STR(x) RGA_VERSION_STR_HELPER(x)
/* RGA im2d api verison */
#define RGA_API_MAJOR_VERSION 1
#define RGA_API_MINOR_VERSION 8
#define RGA_API_REVISION_VERSION 0
#define RGA_API_BUILD_VERSION 0
#define RGA_API_VERSION \
RGA_VERSION_STR(RGA_API_MAJOR_VERSION) "." \
RGA_VERSION_STR(RGA_API_MINOR_VERSION) "." \
RGA_VERSION_STR(RGA_API_REVISION_VERSION) "_[" \
RGA_VERSION_STR(RGA_API_BUILD_VERSION) "]"
#define RGA_API_FULL_VERSION "rga_api version " RGA_API_VERSION
#define RGA_SET_CURRENT_API_VERISON (\
(RGA_API_MAJOR_VERSION & 0xff) << 24 | \
(RGA_API_MINOR_VERSION & 0xff) << 16 | \
(RGA_API_REVISION_VERSION & 0xff) << 8 | \
(RGA_API_BUILD_VERSION & 0xff)\
)
#endif /* _RGA_IM2D_VERSION_H_ */

View File

@@ -0,0 +1,589 @@
/*
* Copyright (C) 2016 Rockchip Electronics Co., Ltd.
* Authors:
* Zhiqin Wei <wzq@rock-chips.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _RGA_DRIVER_H_
#define _RGA_DRIVER_H_
#include <asm/ioctl.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C"
{
#endif
/* Use 'r' as magic number */
#define RGA_IOC_MAGIC 'r'
#define RGA_IOW(nr, type) _IOW(RGA_IOC_MAGIC, nr, type)
#define RGA_IOR(nr, type) _IOR(RGA_IOC_MAGIC, nr, type)
#define RGA_IOWR(nr, type) _IOWR(RGA_IOC_MAGIC, nr, type)
#define RGA_IOC_GET_DRVIER_VERSION RGA_IOR(0x1, struct rga_version_t)
#define RGA_IOC_GET_HW_VERSION RGA_IOR(0x2, struct rga_hw_versions_t)
#define RGA_IOC_IMPORT_BUFFER RGA_IOWR(0x3, struct rga_buffer_pool)
#define RGA_IOC_RELEASE_BUFFER RGA_IOW(0x4, struct rga_buffer_pool)
#define RGA_START_CONFIG RGA_IOR(0x5, uint32_t)
#define RGA_END_CONFIG RGA_IOWR(0x6, struct rga_user_ctx_t)
#define RGA_CMD_CONFIG RGA_IOWR(0x7, struct rga_user_ctx_t)
#define RGA_CANCEL_CONFIG RGA_IOWR(0x8, uint32_t)
#define RGA_BLIT_SYNC 0x5017
#define RGA_BLIT_ASYNC 0x5018
#define RGA_FLUSH 0x5019
#define RGA_GET_RESULT 0x501a
#define RGA_GET_VERSION 0x501b
#define RGA2_BLIT_SYNC 0x6017
#define RGA2_BLIT_ASYNC 0x6018
#define RGA2_FLUSH 0x6019
#define RGA2_GET_RESULT 0x601a
#define RGA2_GET_VERSION 0x601b
#define RGA_REG_CTRL_LEN 0x8 /* 8 */
#define RGA_REG_CMD_LEN 0x1c /* 28 */
#define RGA_CMD_BUF_SIZE 0x700 /* 16*28*4 */
#ifndef ENABLE
#define ENABLE 1
#endif
#ifndef DISABLE
#define DISABLE 0
#endif
enum rga_memory_type {
RGA_DMA_BUFFER = 0,
RGA_VIRTUAL_ADDRESS,
RGA_PHYSICAL_ADDRESS
};
/* RGA process mode enum */
enum {
bitblt_mode = 0x0,
color_palette_mode = 0x1,
color_fill_mode = 0x2,
line_point_drawing_mode = 0x3,
blur_sharp_filter_mode = 0x4,
pre_scaling_mode = 0x5,
update_palette_table_mode = 0x6,
update_patten_buff_mode = 0x7,
};
enum {
rop_enable_mask = 0x2,
dither_enable_mask = 0x8,
fading_enable_mask = 0x10,
PD_enbale_mask = 0x20,
};
enum {
yuv2rgb_mode0 = 0x0, /* BT.601 MPEG */
yuv2rgb_mode1 = 0x1, /* BT.601 JPEG */
yuv2rgb_mode2 = 0x2, /* BT.709 */
rgb2yuv_601_full = 0x1 << 8,
rgb2yuv_709_full = 0x2 << 8,
yuv2yuv_601_limit_2_709_limit = 0x3 << 8,
yuv2yuv_601_limit_2_709_full = 0x4 << 8,
yuv2yuv_709_limit_2_601_limit = 0x5 << 8,
yuv2yuv_709_limit_2_601_full = 0x6 << 8, //not support
yuv2yuv_601_full_2_709_limit = 0x7 << 8,
yuv2yuv_601_full_2_709_full = 0x8 << 8, //not support
yuv2yuv_709_full_2_601_limit = 0x9 << 8, //not support
yuv2yuv_709_full_2_601_full = 0xa << 8, //not support
full_csc_mask = 0xf00,
};
/* RGA rotate mode */
enum {
rotate_mode0 = 0x0, /* no rotate */
rotate_mode1 = 0x1, /* rotate */
rotate_mode2 = 0x2, /* x_mirror */
rotate_mode3 = 0x3, /* y_mirror */
};
enum {
color_palette_mode0 = 0x0, /* 1K */
color_palette_mode1 = 0x1, /* 2K */
color_palette_mode2 = 0x2, /* 4K */
color_palette_mode3 = 0x3, /* 8K */
};
enum {
BB_BYPASS = 0x0, /* no rotate */
BB_ROTATE = 0x1, /* rotate */
BB_X_MIRROR = 0x2, /* x_mirror */
BB_Y_MIRROR = 0x3 /* y_mirror */
};
enum {
nearby = 0x0, /* no rotate */
bilinear = 0x1, /* rotate */
bicubic = 0x2, /* x_mirror */
};
#define RGA_SCHED_PRIORITY_DEFAULT 0
#define RGA_SCHED_PRIORITY_MAX 6
enum {
RGA3_SCHEDULER_CORE0 = 1 << 0,
RGA3_SCHEDULER_CORE1 = 1 << 1,
RGA2_SCHEDULER_CORE0 = 1 << 2,
};
/*
// Alpha Red Green Blue
{ 4, 32, {{32,24, 8, 0, 16, 8, 24,16 }}, GGL_RGBA }, // RK_FORMAT_RGBA_8888
{ 4, 24, {{ 0, 0, 8, 0, 16, 8, 24,16 }}, GGL_RGB }, // RK_FORMAT_RGBX_8888
{ 3, 24, {{ 0, 0, 8, 0, 16, 8, 24,16 }}, GGL_RGB }, // RK_FORMAT_RGB_888
{ 4, 32, {{32,24, 24,16, 16, 8, 8, 0 }}, GGL_BGRA }, // RK_FORMAT_BGRA_8888
{ 2, 16, {{ 0, 0, 16,11, 11, 5, 5, 0 }}, GGL_RGB }, // RK_FORMAT_RGB_565
{ 2, 16, {{ 1, 0, 16,11, 11, 6, 6, 1 }}, GGL_RGBA }, // RK_FORMAT_RGBA_5551
{ 2, 16, {{ 4, 0, 16,12, 12, 8, 8, 4 }}, GGL_RGBA }, // RK_FORMAT_RGBA_4444
{ 3, 24, {{ 0, 0, 24,16, 16, 8, 8, 0 }}, GGL_BGR }, // RK_FORMAT_BGB_888
*/
/* In order to be compatible with RK_FORMAT_XX and HAL_PIXEL_FORMAT_XX,
* RK_FORMAT_XX is shifted to the left by 8 bits to distinguish. */
typedef enum _Rga_SURF_FORMAT {
RK_FORMAT_RGBA_8888 = 0x0 << 8,
RK_FORMAT_RGBX_8888 = 0x1 << 8,
RK_FORMAT_RGB_888 = 0x2 << 8,
RK_FORMAT_BGRA_8888 = 0x3 << 8,
RK_FORMAT_RGB_565 = 0x4 << 8,
RK_FORMAT_RGBA_5551 = 0x5 << 8,
RK_FORMAT_RGBA_4444 = 0x6 << 8,
RK_FORMAT_BGR_888 = 0x7 << 8,
RK_FORMAT_YCbCr_422_SP = 0x8 << 8,
RK_FORMAT_YCbCr_422_P = 0x9 << 8,
RK_FORMAT_YCbCr_420_SP = 0xa << 8,
RK_FORMAT_YCbCr_420_P = 0xb << 8,
RK_FORMAT_YCrCb_422_SP = 0xc << 8,
RK_FORMAT_YCrCb_422_P = 0xd << 8,
RK_FORMAT_YCrCb_420_SP = 0xe << 8,
RK_FORMAT_YCrCb_420_P = 0xf << 8,
RK_FORMAT_BPP1 = 0x10 << 8,
RK_FORMAT_BPP2 = 0x11 << 8,
RK_FORMAT_BPP4 = 0x12 << 8,
RK_FORMAT_BPP8 = 0x13 << 8,
RK_FORMAT_Y4 = 0x14 << 8,
RK_FORMAT_YCbCr_400 = 0x15 << 8,
RK_FORMAT_BGRX_8888 = 0x16 << 8,
RK_FORMAT_YVYU_422 = 0x18 << 8,
RK_FORMAT_YVYU_420 = 0x19 << 8,
RK_FORMAT_VYUY_422 = 0x1a << 8,
RK_FORMAT_VYUY_420 = 0x1b << 8,
RK_FORMAT_YUYV_422 = 0x1c << 8,
RK_FORMAT_YUYV_420 = 0x1d << 8,
RK_FORMAT_UYVY_422 = 0x1e << 8,
RK_FORMAT_UYVY_420 = 0x1f << 8,
RK_FORMAT_YCbCr_420_SP_10B = 0x20 << 8,
RK_FORMAT_YCrCb_420_SP_10B = 0x21 << 8,
RK_FORMAT_YCbCr_422_SP_10B = 0x22 << 8,
RK_FORMAT_YCrCb_422_SP_10B = 0x23 << 8,
/* For compatibility with misspellings */
RK_FORMAT_YCbCr_422_10b_SP = RK_FORMAT_YCbCr_422_SP_10B,
RK_FORMAT_YCrCb_422_10b_SP = RK_FORMAT_YCrCb_422_SP_10B,
RK_FORMAT_BGR_565 = 0x24 << 8,
RK_FORMAT_BGRA_5551 = 0x25 << 8,
RK_FORMAT_BGRA_4444 = 0x26 << 8,
RK_FORMAT_ARGB_8888 = 0x28 << 8,
RK_FORMAT_XRGB_8888 = 0x29 << 8,
RK_FORMAT_ARGB_5551 = 0x2a << 8,
RK_FORMAT_ARGB_4444 = 0x2b << 8,
RK_FORMAT_ABGR_8888 = 0x2c << 8,
RK_FORMAT_XBGR_8888 = 0x2d << 8,
RK_FORMAT_ABGR_5551 = 0x2e << 8,
RK_FORMAT_ABGR_4444 = 0x2f << 8,
RK_FORMAT_RGBA2BPP = 0x30 << 8,
RK_FORMAT_UNKNOWN = 0x100 << 8,
} RgaSURF_FORMAT;
/* RGA3 rd_mode */
enum
{
raster_mode = 0x1 << 0,
fbc_mode = 0x1 << 1,
tile_mode = 0x1 << 2,
};
typedef struct rga_img_info_t {
uint64_t yrgb_addr; /* yrgb mem addr */
uint64_t uv_addr; /* cb/cr mem addr */
uint64_t v_addr; /* cr mem addr */
uint32_t format; //definition by RK_FORMAT
uint16_t act_w;
uint16_t act_h;
uint16_t x_offset;
uint16_t y_offset;
uint16_t vir_w;
uint16_t vir_h;
uint16_t endian_mode; //for BPP
uint16_t alpha_swap;
//used by RGA3
uint16_t rotate_mode;
uint16_t rd_mode;
uint16_t is_10b_compact;
uint16_t is_10b_endian;
uint16_t enable;
}
rga_img_info_t;
typedef struct POINT {
uint16_t x;
uint16_t y;
}
POINT;
typedef struct RECT {
uint16_t xmin;
uint16_t xmax; // width - 1
uint16_t ymin;
uint16_t ymax; // height - 1
} RECT;
typedef struct MMU {
uint8_t mmu_en;
uint64_t base_addr;
uint32_t mmu_flag; /* [0] mmu enable [1] src_flush [2] dst_flush [3] CMD_flush [4~5] page size*/
} MMU;
typedef struct COLOR_FILL {
int16_t gr_x_a;
int16_t gr_y_a;
int16_t gr_x_b;
int16_t gr_y_b;
int16_t gr_x_g;
int16_t gr_y_g;
int16_t gr_x_r;
int16_t gr_y_r;
//u8 cp_gr_saturation;
}
COLOR_FILL;
typedef struct FADING {
uint8_t b;
uint8_t g;
uint8_t r;
uint8_t res;
}
FADING;
typedef struct line_draw_t {
POINT start_point; /* LineDraw_start_point */
POINT end_point; /* LineDraw_end_point */
uint32_t color; /* LineDraw_color */
uint32_t flag; /* (enum) LineDrawing mode sel */
uint32_t line_width; /* range 1~16 */
}
line_draw_t;
/* color space convert coefficient. */
typedef struct csc_coe_t {
int16_t r_v;
int16_t g_y;
int16_t b_u;
int32_t off;
} csc_coe_t;
typedef struct full_csc_t {
uint8_t flag;
csc_coe_t coe_y;
csc_coe_t coe_u;
csc_coe_t coe_v;
} full_csc_t;
struct rga_mosaic_info {
uint8_t enable;
uint8_t mode;
};
struct rga_pre_intr_info {
uint8_t enable;
uint8_t read_intr_en;
uint8_t write_intr_en;
uint8_t read_hold_en;
uint32_t read_threshold;
uint32_t write_start;
uint32_t write_step;
};
/* MAX(min, (max - channel_value)) */
struct rga_osd_invert_factor {
uint8_t alpha_max;
uint8_t alpha_min;
uint8_t yg_max;
uint8_t yg_min;
uint8_t crb_max;
uint8_t crb_min;
};
struct rga_color {
union {
struct {
uint8_t red;
uint8_t green;
uint8_t blue;
uint8_t alpha;
};
uint32_t value;
};
};
struct rga_osd_bpp2 {
uint8_t ac_swap; // ac swap flag
// 0: CA
// 1: AC
uint8_t endian_swap; // rgba2bpp endian swap
// 0: Big endian
// 1: Little endian
struct rga_color color0;
struct rga_color color1;
};
struct rga_osd_mode_ctrl {
uint8_t mode; // OSD cal mode:
// 0b'1: statistics mode
// 1b'1: auto inversion overlap mode
uint8_t direction_mode; // horizontal or vertical
// 0: horizontal
// 1: vertical
uint8_t width_mode; // using @fix_width or LUT width
// 0: fix width
// 1: LUT width
uint16_t block_fix_width; // OSD block fixed width
// real width = (fix_width + 1) * 2
uint8_t block_num; // OSD block num
uint16_t flags_index; // auto invert flags index
/* invertion config */
uint8_t color_mode; // selete color
// 0: src1 color
// 1: config data color
uint8_t invert_flags_mode; // invert flag selete
// 0: use RAM flag
// 1: usr last result
uint8_t default_color_sel; // default color mode
// 0: default is bright
// 1: default is dark
uint8_t invert_enable; // invert channel enable
// 1 << 0: aplha enable
// 1 << 1: Y/G disable
// 1 << 2: C/RB disable
uint8_t invert_mode; // invert cal mode
// 0: normal(max-data)
// 1: swap
uint8_t invert_thresh; // if luma > thresh, osd_flag to be 1
uint8_t unfix_index; // OSD width config index
};
struct rga_osd_info {
uint8_t enable;
struct rga_osd_mode_ctrl mode_ctrl;
struct rga_osd_invert_factor cal_factor;
struct rga_osd_bpp2 bpp2_info;
union {
struct {
uint32_t last_flags1;
uint32_t last_flags0;
};
uint64_t last_flags;
};
union {
struct {
uint32_t cur_flags1;
uint32_t cur_flags0;
};
uint64_t cur_flags;
};
};
#define RGA_VERSION_SIZE 16
#define RGA_HW_SIZE 5
struct rga_version_t {
uint32_t major;
uint32_t minor;
uint32_t revision;
uint8_t str[RGA_VERSION_SIZE];
};
struct rga_hw_versions_t {
struct rga_version_t version[RGA_HW_SIZE];
uint32_t size;
};
struct rga_memory_parm {
uint32_t width;
uint32_t height;
uint32_t format;
uint32_t size;
};
struct rga_external_buffer {
uint64_t memory;
uint32_t type;
uint32_t handle;
struct rga_memory_parm memory_info;
uint8_t reserve[252];
};
struct rga_buffer_pool {
uint64_t buffers;
uint32_t size;
};
struct rga_req {
uint8_t render_mode; /* (enum) process mode sel */
rga_img_info_t src; /* src image info */
rga_img_info_t dst; /* dst image info */
rga_img_info_t pat; /* patten image info */
uint64_t rop_mask_addr; /* rop4 mask addr */
uint64_t LUT_addr; /* LUT addr */
RECT clip; /* dst clip window default value is dst_vir */
/* value from [0, w-1] / [0, h-1]*/
int32_t sina; /* dst angle default value 0 16.16 scan from table */
int32_t cosa; /* dst angle default value 0 16.16 scan from table */
uint16_t alpha_rop_flag; /* alpha rop process flag */
/* ([0] = 1 alpha_rop_enable) */
/* ([1] = 1 rop enable) */
/* ([2] = 1 fading_enable) */
/* ([3] = 1 PD_enable) */
/* ([4] = 1 alpha cal_mode_sel) */
/* ([5] = 1 dither_enable) */
/* ([6] = 1 gradient fill mode sel) */
/* ([7] = 1 AA_enable) */
/* ([8] = 1 nn_quantize) */
/* ([9] = 1 Real color mode) */
uint8_t scale_mode; /* 0 nearst / 1 bilnear / 2 bicubic */
uint32_t color_key_max; /* color key max */
uint32_t color_key_min; /* color key min */
uint32_t fg_color; /* foreground color */
uint32_t bg_color; /* background color */
COLOR_FILL gr_color; /* color fill use gradient */
line_draw_t line_draw_info;
FADING fading;
uint8_t PD_mode; /* porter duff alpha mode sel */
uint8_t alpha_global_value; /* global alpha value */
uint16_t rop_code; /* rop2/3/4 code scan from rop code table*/
uint8_t bsfilter_flag; /* [2] 0 blur 1 sharp / [1:0] filter_type*/
uint8_t palette_mode; /* (enum) color palatte 0/1bpp, 1/2bpp 2/4bpp 3/8bpp*/
uint8_t yuv2rgb_mode; /* (enum) BT.601 MPEG / BT.601 JPEG / BT.709 */
uint8_t endian_mode; /* 0/big endian 1/little endian*/
uint8_t rotate_mode; /* (enum) rotate mode */
/* 0x0, no rotate */
/* 0x1, rotate */
/* 0x2, x_mirror */
/* 0x3, y_mirror */
uint8_t color_fill_mode; /* 0 solid color / 1 patten color */
MMU mmu_info; /* mmu information */
uint8_t alpha_rop_mode; /* ([0~1] alpha mode) */
/* ([2~3] rop mode) */
/* ([4] zero mode en) */
/* ([5] dst alpha mode) (RGA1) */
uint8_t src_trans_mode;
uint8_t dither_mode;
full_csc_t full_csc; /* full color space convert */
int32_t in_fence_fd;
uint8_t core;
uint8_t priority;
int32_t out_fence_fd;
uint8_t handle_flag;
/* RGA2 1106 add */
struct rga_mosaic_info mosaic_info;
uint8_t uvhds_mode;
uint8_t uvvds_mode;
struct rga_osd_info osd_info;
struct rga_pre_intr_info pre_intr_info;
uint8_t reservr[59];
};
struct rga_user_ctx_t {
uint64_t cmd_ptr;
uint32_t cmd_num;
uint32_t id;
uint32_t sync_mode;
uint32_t out_fence_fd;
uint32_t mpi_config_flags;
uint8_t reservr[124];
};
#ifdef __cplusplus
}
#endif
#endif /*_RK29_IPP_DRIVER_H_*/

View File

@@ -0,0 +1,168 @@
/*
* Copyright 2020 Rockchip Electronics Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef __RK_MPI_MMZ_H__
#define __RK_MPI_MMZ_H__
#include <stdint.h>
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif /* End of #ifdef __cplusplus */
typedef void* MB_BLK;
typedef int RK_S32;
typedef uint32_t RK_U32;
typedef uint64_t RK_U64;
typedef void RK_VOID;
#define RK_MMZ_ALLOC_TYPE_IOMMU 0x00000000
#define RK_MMZ_ALLOC_TYPE_CMA 0x00000001
#define RK_MMZ_ALLOC_CACHEABLE 0x00000000
#define RK_MMZ_ALLOC_UNCACHEABLE 0x00000010
#define RK_MMZ_SYNC_READONLY 0x00000000
#define RK_MMZ_SYNC_WRITEONLY 0x00000001
#define RK_MMZ_SYNC_RW 0x00000002
/*
申请buffer
pBlk 返回分配的buffer信息
u32Len 申请buffer的大小
u32Flags 申请buffer类型
成功 返回0
失败 返回负值
*/
RK_S32 RK_MPI_MMZ_Alloc(MB_BLK *pBlk, RK_U32 u32Len, RK_U32 u32Flags);
/*
释放buffer
*/
RK_S32 RK_MPI_MMZ_Free(MB_BLK mb);
/*
获取物理地址
对于物理连续内存,返回其物理地址
对于非物理连续内存,返回-1
*/
RK_U64 RK_MPI_MMZ_Handle2PhysAddr(MB_BLK mb);
/*
获取用户空间虚拟地址
失败返回NULL
*/
RK_VOID *RK_MPI_MMZ_Handle2VirAddr(MB_BLK mb);
/*
获取buffer的fd
失败返回-1
*/
RK_S32 RK_MPI_MMZ_Handle2Fd(MB_BLK mb);
/*
获取buffer大小
失败返回 (RK_U64)-1
*/
RK_U64 RK_MPI_MMZ_GetSize(MB_BLK mb);
/*
通过fd查找到对应的buffer
成功 返回mb
失败 返回NULL
*/
MB_BLK RK_MPI_MMZ_Fd2Handle(RK_S32 fd);
/*
通过vaddr查找到对应的buffer
成功 返回mb
失败 返回NULL
*/
MB_BLK RK_MPI_MMZ_VirAddr2Handle(RK_VOID *pstVirAddr);
/*
通过paddr查找到对应的buffer
成功 返回mb
失败 返回NULL
*/
MB_BLK RK_MPI_MMZ_PhyAddr2Handle(RK_U64 paddr);
/*
查询buffer是否cacheable
是 返回1
否 返回0
不确定 返回-1
*/
RK_S32 RK_MPI_MMZ_IsCacheable(MB_BLK mb);
/*
flush cache, 在cpu访问前调用
当offset和length都等于0时候执行full sync否则执行partial sync
成功 返回0
失败 返回负值
*/
RK_S32 RK_MPI_MMZ_FlushCacheStart(MB_BLK mb, RK_U32 offset, RK_U32 length, RK_U32 flags);
/*
flush cache, 在cpu访问结束后调用
当offset和length都等于0时候执行full sync否则执行partial sync
成功 返回0
失败 返回负值
*/
RK_S32 RK_MPI_MMZ_FlushCacheEnd(MB_BLK mb, RK_U32 offset, RK_U32 length, RK_U32 flags);
/*
flush cache, 在cpu访问前调用
指定待刷新内存的虚拟地址及其长度只支持partial sync
成功 返回0
失败 返回负值
*/
RK_S32 RK_MPI_MMZ_FlushCacheVaddrStart(RK_VOID* vaddr, RK_U32 length, RK_U32 flags);
/*
flush cache, 在cpu访问结束后调用
指定待刷新内存的虚拟地址及其长度只支持partial sync
成功 返回0
失败 返回负值
*/
RK_S32 RK_MPI_MMZ_FlushCacheVaddrEnd(RK_VOID* vaddr, RK_U32 length, RK_U32 flags);
/*
flush cache, 在cpu访问前调用
指定待刷新内存的物理地址及其长度只支持partial sync
成功 返回0
失败 返回负值
*/
RK_S32 RK_MPI_MMZ_FlushCachePaddrStart(RK_U64 vaddr, RK_U32 length, RK_U32 flags);
/*
flush cache, 在cpu访问结束后调用
指定待刷新内存的物理地址及其长度只支持partial sync
成功 返回0
失败 返回负值
*/
RK_S32 RK_MPI_MMZ_FlushCachePaddrEnd(RK_U64 vaddr, RK_U32 length, RK_U32 flags);
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* End of #ifdef __cplusplus */
#endif /* __RK_MPI_MMZ_H__ */

View File

@@ -0,0 +1 @@
version:1.6

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,168 @@
#ifndef THREADPOOL_H
#define THREADPOOL_H
#include <cassert>
#include <condition_variable>
#include <functional>
#include <future>
#include <memory>
#include <mutex>
#include <queue>
#include <thread>
#include <unordered_map>
namespace dpool
{
class ThreadPool
{
public:
using MutexGuard = std::lock_guard<std::mutex>;
using UniqueLock = std::unique_lock<std::mutex>;
using Thread = std::thread;
using ThreadID = std::thread::id;
using Task = std::function<void()>;
ThreadPool()
: ThreadPool(Thread::hardware_concurrency())
{
}
explicit ThreadPool(size_t maxThreads)
: quit_(false),
currentThreads_(0),
idleThreads_(0),
maxThreads_(maxThreads)
{
}
// disable the copy operations
ThreadPool(const ThreadPool &) = delete;
ThreadPool &operator=(const ThreadPool &) = delete;
~ThreadPool()
{
{
MutexGuard guard(mutex_);
quit_ = true;
}
cv_.notify_all();
for (auto &elem : threads_)
{
assert(elem.second.joinable());
elem.second.join();
}
}
template <typename Func, typename... Ts>
auto submit(Func &&func, Ts &&...params)
-> std::future<typename std::result_of<Func(Ts...)>::type>
{
auto execute = std::bind(std::forward<Func>(func), std::forward<Ts>(params)...);
using ReturnType = typename std::result_of<Func(Ts...)>::type;
using PackagedTask = std::packaged_task<ReturnType()>;
auto task = std::make_shared<PackagedTask>(std::move(execute));
auto result = task->get_future();
MutexGuard guard(mutex_);
assert(!quit_);
tasks_.emplace([task]()
{ (*task)(); });
if (idleThreads_ > 0)
{
cv_.notify_one();
}
else if (currentThreads_ < maxThreads_)
{
Thread t(&ThreadPool::worker, this);
assert(threads_.find(t.get_id()) == threads_.end());
threads_[t.get_id()] = std::move(t);
++currentThreads_;
}
return result;
}
size_t threadsNum() const
{
MutexGuard guard(mutex_);
return currentThreads_;
}
private:
void worker()
{
while (true)
{
Task task;
{
UniqueLock uniqueLock(mutex_);
++idleThreads_;
auto hasTimedout = !cv_.wait_for(uniqueLock,
std::chrono::seconds(WAIT_SECONDS),
[this]()
{
return quit_ || !tasks_.empty();
});
--idleThreads_;
if (tasks_.empty())
{
if (quit_)
{
--currentThreads_;
return;
}
if (hasTimedout)
{
--currentThreads_;
joinFinishedThreads();
finishedThreadIDs_.emplace(std::this_thread::get_id());
return;
}
}
task = std::move(tasks_.front());
tasks_.pop();
}
task();
}
}
void joinFinishedThreads()
{
while (!finishedThreadIDs_.empty())
{
auto id = std::move(finishedThreadIDs_.front());
finishedThreadIDs_.pop();
auto iter = threads_.find(id);
assert(iter != threads_.end());
assert(iter->second.joinable());
iter->second.join();
threads_.erase(iter);
}
}
static constexpr size_t WAIT_SECONDS = 2;
bool quit_;
size_t currentThreads_;
size_t idleThreads_;
size_t maxThreads_;
mutable std::mutex mutex_;
std::condition_variable cv_;
std::queue<Task> tasks_;
std::queue<ThreadID> finishedThreadIDs_;
std::unordered_map<ThreadID, Thread> threads_;
};
constexpr size_t ThreadPool::WAIT_SECONDS;
} // namespace dpool
#endif /* THREADPOOL_H */

View File

@@ -0,0 +1,53 @@
#ifndef __DRM_FUNC_H__
#define __DRM_FUNC_H__
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/fcntl.h>// open function
#include <unistd.h> // close function
#include <errno.h>
#include <sys/mman.h>
#include <linux/input.h>
#include "libdrm/drm_fourcc.h"
#include "xf86drm.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef int (* FUNC_DRM_IOCTL)(int fd, unsigned long request, void *arg);
typedef struct _drm_context{
void *drm_handle;
FUNC_DRM_IOCTL io_func;
} drm_context;
/* memory type definitions. */
enum drm_rockchip_gem_mem_type
{
/* Physically Continuous memory and used as default. */
ROCKCHIP_BO_CONTIG = 1 << 0,
/* cachable mapping. */
ROCKCHIP_BO_CACHABLE = 1 << 1,
/* write-combine mapping. */
ROCKCHIP_BO_WC = 1 << 2,
ROCKCHIP_BO_SECURE = 1 << 3,
ROCKCHIP_BO_MASK = ROCKCHIP_BO_CONTIG | ROCKCHIP_BO_CACHABLE |
ROCKCHIP_BO_WC | ROCKCHIP_BO_SECURE
};
int drm_init(drm_context *drm_ctx);
void* drm_buf_alloc(drm_context *drm_ctx,int drm_fd, int TexWidth, int TexHeight,int bpp,int *fd,unsigned int *handle,size_t *actual_size);
int drm_buf_destroy(drm_context *drm_ctx,int drm_fd,int buf_fd, int handle,void *drm_buf,size_t size);
void drm_deinit(drm_context *drm_ctx, int drm_fd);
#ifdef __cplusplus
}
#endif
#endif /*__DRM_FUNC_H__*/

View File

@@ -0,0 +1,42 @@
#ifndef _RKNN_ZERO_COPY_DEMO_POSTPROCESS_H_
#define _RKNN_ZERO_COPY_DEMO_POSTPROCESS_H_
#include <stdint.h>
#include <vector>
#define OBJ_NAME_MAX_SIZE 16
#define OBJ_NUMB_MAX_SIZE 64
#define OBJ_CLASS_NUM 80
#define NMS_THRESH 0.45
#define BOX_THRESH 0.25
#define PROP_BOX_SIZE (5+OBJ_CLASS_NUM)
typedef struct _BOX_RECT
{
int left;
int right;
int top;
int bottom;
} BOX_RECT;
typedef struct __detect_result_t
{
char name[OBJ_NAME_MAX_SIZE];
BOX_RECT box;
float prop;
} detect_result_t;
typedef struct _detect_result_group_t
{
int id;
int count;
detect_result_t results[OBJ_NUMB_MAX_SIZE];
} detect_result_group_t;
int post_process(int8_t *input0, int8_t *input1, int8_t *input2, int model_in_h, int model_in_w,
float conf_threshold, float nms_threshold, float scale_w, float scale_h,
std::vector<int32_t> &qnt_zps, std::vector<float> &qnt_scales,
detect_result_group_t *group);
void deinitPostProcess();
#endif //_RKNN_ZERO_COPY_DEMO_POSTPROCESS_H_

View File

@@ -0,0 +1,33 @@
#ifndef __RGA_FUNC_H__
#define __RGA_FUNC_H__
#include <dlfcn.h>
#include "RgaApi.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef int(* FUNC_RGA_INIT)();
typedef void(* FUNC_RGA_DEINIT)();
typedef int(* FUNC_RGA_BLIT)(rga_info_t *, rga_info_t *, rga_info_t *);
typedef struct _rga_context{
void *rga_handle;
FUNC_RGA_INIT init_func;
FUNC_RGA_DEINIT deinit_func;
FUNC_RGA_BLIT blit_func;
} rga_context;
int RGA_init(rga_context* rga_ctx);
void img_resize_fast(rga_context *rga_ctx, int src_fd, int src_w, int src_h, uint64_t dst_phys, int dst_w, int dst_h);
void img_resize_slow(rga_context *rga_ctx, void *src_virt, int src_w, int src_h, void *dst_virt, int dst_w, int dst_h);
int RGA_deinit(rga_context* rga_ctx);
#ifdef __cplusplus
}
#endif
#endif/*__RGA_FUNC_H__*/

View File

@@ -0,0 +1,291 @@
#ifndef _rknnPool_H
#define _rknnPool_H
#include <queue>
#include <vector>
#include <iostream>
#include "rga.h"
#include "im2d.h"
#include "RgaUtils.h"
#include "rknn_api.h"
#include "postprocess.h"
#include "opencv2/core/core.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"
#include "ThreadPool.hpp"
using cv::Mat;
using std::queue;
using std::vector;
static unsigned char *load_data(FILE *fp, size_t ofst, size_t sz);
static unsigned char *load_model(const char *filename, int *model_size);
class rknn_lite
{
private:
rknn_context rkModel;
unsigned char *model_data;
rknn_sdk_version version;
rknn_input_output_num io_num;
rknn_tensor_attr *input_attrs;
rknn_tensor_attr *output_attrs;
rknn_input inputs[1];
int ret;
int channel = 3;
int width = 0;
int height = 0;
public:
Mat ori_img;
int interf();
rknn_lite(char *dst, int n);
~rknn_lite();
};
rknn_lite::rknn_lite(char *model_name, int n)
{
/* Create the neural network */
printf("Loading mode...\n");
int model_data_size = 0;
// 读取模型文件数据
model_data = load_model(model_name, &model_data_size);
// 通过模型文件初始化rknn类
ret = rknn_init(&rkModel, model_data, model_data_size, 0, NULL);
if (ret < 0)
{
printf("rknn_init error ret=%d\n", ret);
exit(-1);
}
//
rknn_core_mask core_mask;
if (n == 0)
core_mask = RKNN_NPU_CORE_0;
else if(n == 1)
core_mask = RKNN_NPU_CORE_1;
else
core_mask = RKNN_NPU_CORE_2;
int ret = rknn_set_core_mask(rkModel, core_mask);
if (ret < 0)
{
printf("rknn_init core error ret=%d\n", ret);
exit(-1);
}
// 初始化rknn类的版本
ret = rknn_query(rkModel, RKNN_QUERY_SDK_VERSION, &version, sizeof(rknn_sdk_version));
if (ret < 0)
{
printf("rknn_init error ret=%d\n", ret);
exit(-1);
}
// 获取模型的输入参数
ret = rknn_query(rkModel, RKNN_QUERY_IN_OUT_NUM, &io_num, sizeof(io_num));
if (ret < 0)
{
printf("rknn_init error ret=%d\n", ret);
exit(-1);
}
// 设置输入数组
input_attrs = new rknn_tensor_attr[io_num.n_input];
memset(input_attrs, 0, sizeof(input_attrs));
for (int i = 0; i < io_num.n_input; i++)
{
input_attrs[i].index = i;
ret = rknn_query(rkModel, RKNN_QUERY_INPUT_ATTR, &(input_attrs[i]), sizeof(rknn_tensor_attr));
if (ret < 0)
{
printf("rknn_init error ret=%d\n", ret);
exit(-1);
}
}
// 设置输出数组
output_attrs = new rknn_tensor_attr[io_num.n_output];
memset(output_attrs, 0, sizeof(output_attrs) );
for (int i = 0; i < io_num.n_output; i++)
{
output_attrs[i].index = i;
ret = rknn_query(rkModel, RKNN_QUERY_OUTPUT_ATTR, &(output_attrs[i]), sizeof(rknn_tensor_attr));
}
// 设置输入参数
if (input_attrs[0].fmt == RKNN_TENSOR_NCHW)
{
printf("model is NCHW input fmt\n");
channel = input_attrs[0].dims[1];
height = input_attrs[0].dims[2];
width = input_attrs[0].dims[3];
}
else
{
printf("model is NHWC input fmt\n");
height = input_attrs[0].dims[1];
width = input_attrs[0].dims[2];
channel = input_attrs[0].dims[3];
}
memset(inputs, 0, sizeof(inputs));
inputs[0].index = 0;
inputs[0].type = RKNN_TENSOR_UINT8;
inputs[0].size = width * height * channel;
inputs[0].fmt = RKNN_TENSOR_NHWC;
inputs[0].pass_through = 0;
}
rknn_lite::~rknn_lite()
{
ret = rknn_destroy(rkModel);
delete[] input_attrs;
delete[] output_attrs;
if (model_data)
free(model_data);
}
int rknn_lite::interf()
{
cv::Mat img;
// 获取图像宽高
int img_width = ori_img.cols;
int img_height = ori_img.rows;
cv::cvtColor(ori_img, img, cv::COLOR_BGR2RGB);
// init rga context
// rga是rk自家的绘图库,绘图效率高于OpenCV
rga_buffer_t src;
rga_buffer_t dst;
memset(&src, 0, sizeof(src));
memset(&dst, 0, sizeof(dst));
im_rect src_rect;
im_rect dst_rect;
memset(&src_rect, 0, sizeof(src_rect));
memset(&dst_rect, 0, sizeof(dst_rect));
// You may not need resize when src resulotion equals to dst resulotion
void *resize_buf = nullptr;
// 如果输入图像不是指定格式
if (img_width != width || img_height != height)
{
resize_buf = malloc( height * width * channel);
memset(resize_buf, 0x00, height * width * channel);
src = wrapbuffer_virtualaddr((void *)img.data, img_width, img_height, RK_FORMAT_RGB_888);
dst = wrapbuffer_virtualaddr((void *)resize_buf, width, height, RK_FORMAT_RGB_888);
ret = imcheck(src, dst, src_rect, dst_rect);
if (IM_STATUS_NOERROR != ret)
{
printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS) ret));
exit(-1);
}
IM_STATUS STATUS = imresize(src, dst);
cv::Mat resize_img(cv::Size( width, height), CV_8UC3, resize_buf);
inputs[0].buf = resize_buf;
}
else
inputs[0].buf = (void *)img.data;
// 设置rknn的输入数据
rknn_inputs_set( rkModel, io_num.n_input, inputs);
// 设置输出
rknn_output outputs[ io_num.n_output];
memset(outputs, 0, sizeof(outputs));
for (int i = 0; i < io_num.n_output; i++)
outputs[i].want_float = 0;
// 调用npu进行推演
ret = rknn_run( rkModel, NULL);
// 获取npu的推演输出结果
ret = rknn_outputs_get( rkModel, io_num.n_output, outputs, NULL);
// 总之就是绘图部分
// post process
// width是模型需要的输入宽度, img_width是图片的实际宽度
const float nms_threshold = NMS_THRESH;
const float box_conf_threshold = BOX_THRESH;
float scale_w = (float) width / img_width;
float scale_h = (float) height / img_height;
detect_result_group_t detect_result_group;
std::vector<float> out_scales;
std::vector<int32_t> out_zps;
for (int i = 0; i < io_num.n_output; ++i)
{
out_scales.push_back( output_attrs[i].scale);
out_zps.push_back( output_attrs[i].zp);
}
post_process((int8_t *)outputs[0].buf, (int8_t *)outputs[1].buf, (int8_t *)outputs[2].buf, height, width,
box_conf_threshold, nms_threshold, scale_w, scale_h, out_zps, out_scales, &detect_result_group);
// Draw Objects
char text[256];
for (int i = 0; i < detect_result_group.count; i++)
{
detect_result_t *det_result = &(detect_result_group.results[i]);
sprintf(text, "%s %.1f%%", det_result->name, det_result->prop * 100);
int x1 = det_result->box.left;
int y1 = det_result->box.top;
rectangle(ori_img, cv::Point(x1, y1), cv::Point(det_result->box.right, det_result->box.bottom), cv::Scalar(0, 0, 255, 0), 3);
putText(ori_img, text, cv::Point(x1, y1 + 12), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(255, 255, 255));
}
ret = rknn_outputs_release( rkModel, io_num.n_output, outputs);
if (resize_buf)
{
free(resize_buf);
}
return 0;
}
static unsigned char *load_data(FILE *fp, size_t ofst, size_t sz)
{
unsigned char *data;
int ret;
data = NULL;
if (NULL == fp)
{
return NULL;
}
ret = fseek(fp, ofst, SEEK_SET);
if (ret != 0)
{
printf("blob seek failure.\n");
return NULL;
}
data = (unsigned char *)malloc(sz);
if (data == NULL)
{
printf("buffer malloc failure.\n");
return NULL;
}
ret = fread(data, 1, sz, fp);
return data;
}
static unsigned char *load_model(const char *filename, int *model_size)
{
FILE *fp;
unsigned char *data;
fp = fopen(filename, "rb");
if (NULL == fp)
{
printf("Open file %s failed.\n", filename);
return NULL;
}
fseek(fp, 0, SEEK_END);
int size = ftell(fp);
data = load_data(fp, 0, size);
fclose(fp);
*model_size = size;
return data;
}
#endif

View File

@@ -0,0 +1,668 @@
/****************************************************************************
*
* Copyright (c) 2017 - 2022 by Rockchip Corp. All rights reserved.
*
* The material in this file is confidential and contains trade secrets
* of Rockchip Corporation. This is proprietary information owned by
* Rockchip Corporation. No part of this work may be disclosed,
* reproduced, copied, transmitted, or used in any way for any purpose,
* without the express written permission of Rockchip Corporation.
*
*****************************************************************************/
#ifndef _RKNN_API_H
#define _RKNN_API_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
/*
Definition of extended flag for rknn_init.
*/
/* set high priority context. */
#define RKNN_FLAG_PRIOR_HIGH 0x00000000
/* set medium priority context */
#define RKNN_FLAG_PRIOR_MEDIUM 0x00000001
/* set low priority context. */
#define RKNN_FLAG_PRIOR_LOW 0x00000002
/* asynchronous mode.
when enable, rknn_outputs_get will not block for too long because it directly retrieves the result of
the previous frame which can increase the frame rate on single-threaded mode, but at the cost of
rknn_outputs_get not retrieves the result of the current frame.
in multi-threaded mode you do not need to turn this mode on. */
#define RKNN_FLAG_ASYNC_MASK 0x00000004
/* collect performance mode.
when enable, you can get detailed performance reports via rknn_query(ctx, RKNN_QUERY_PERF_DETAIL, ...),
but it will reduce the frame rate. */
#define RKNN_FLAG_COLLECT_PERF_MASK 0x00000008
/* allocate all memory in outside, includes weight/internal/inputs/outputs */
#define RKNN_FLAG_MEM_ALLOC_OUTSIDE 0x00000010
/* weight sharing with the same network structure */
#define RKNN_FLAG_SHARE_WEIGHT_MEM 0x00000020
/* send fence fd from outside */
#define RKNN_FLAG_FENCE_IN_OUTSIDE 0x00000040
/* get fence fd from inside */
#define RKNN_FLAG_FENCE_OUT_OUTSIDE 0x00000080
/*
Error code returned by the RKNN API.
*/
#define RKNN_SUCC 0 /* execute succeed. */
#define RKNN_ERR_FAIL -1 /* execute failed. */
#define RKNN_ERR_TIMEOUT -2 /* execute timeout. */
#define RKNN_ERR_DEVICE_UNAVAILABLE -3 /* device is unavailable. */
#define RKNN_ERR_MALLOC_FAIL -4 /* memory malloc fail. */
#define RKNN_ERR_PARAM_INVALID -5 /* parameter is invalid. */
#define RKNN_ERR_MODEL_INVALID -6 /* model is invalid. */
#define RKNN_ERR_CTX_INVALID -7 /* context is invalid. */
#define RKNN_ERR_INPUT_INVALID -8 /* input is invalid. */
#define RKNN_ERR_OUTPUT_INVALID -9 /* output is invalid. */
#define RKNN_ERR_DEVICE_UNMATCH -10 /* the device is unmatch, please update rknn sdk
and npu driver/firmware. */
#define RKNN_ERR_INCOMPATILE_PRE_COMPILE_MODEL -11 /* This RKNN model use pre_compile mode, but not compatible with current driver. */
#define RKNN_ERR_INCOMPATILE_OPTIMIZATION_LEVEL_VERSION -12 /* This RKNN model set optimization level, but not compatible with current driver. */
#define RKNN_ERR_TARGET_PLATFORM_UNMATCH -13 /* This RKNN model set target platform, but not compatible with current platform. */
/*
Definition for tensor
*/
#define RKNN_MAX_DIMS 16 /* maximum dimension of tensor. */
#define RKNN_MAX_NUM_CHANNEL 15 /* maximum channel number of input tensor. */
#define RKNN_MAX_NAME_LEN 256 /* maximum name lenth of tensor. */
#ifdef __arm__
typedef uint32_t rknn_context;
#else
typedef uint64_t rknn_context;
#endif
/*
The query command for rknn_query
*/
typedef enum _rknn_query_cmd {
RKNN_QUERY_IN_OUT_NUM = 0, /* query the number of input & output tensor. */
RKNN_QUERY_INPUT_ATTR = 1, /* query the attribute of input tensor. */
RKNN_QUERY_OUTPUT_ATTR = 2, /* query the attribute of output tensor. */
RKNN_QUERY_PERF_DETAIL = 3, /* query the detail performance, need set
RKNN_FLAG_COLLECT_PERF_MASK when call rknn_init,
this query needs to be valid after rknn_outputs_get. */
RKNN_QUERY_PERF_RUN = 4, /* query the time of run,
this query needs to be valid after rknn_outputs_get. */
RKNN_QUERY_SDK_VERSION = 5, /* query the sdk & driver version */
RKNN_QUERY_MEM_SIZE = 6, /* query the weight & internal memory size */
RKNN_QUERY_CUSTOM_STRING = 7, /* query the custom string */
RKNN_QUERY_NATIVE_INPUT_ATTR = 8, /* query the attribute of native input tensor. */
RKNN_QUERY_NATIVE_OUTPUT_ATTR = 9, /* query the attribute of native output tensor. */
RKNN_QUERY_NATIVE_NC1HWC2_INPUT_ATTR = 8, /* query the attribute of native input tensor. */
RKNN_QUERY_NATIVE_NC1HWC2_OUTPUT_ATTR = 9, /* query the attribute of native output tensor. */
RKNN_QUERY_NATIVE_NHWC_INPUT_ATTR = 10, /* query the attribute of native input tensor. */
RKNN_QUERY_NATIVE_NHWC_OUTPUT_ATTR = 11, /* query the attribute of native output tensor. */
RKNN_QUERY_DEVICE_MEM_INFO = 12, /* query the attribute of rknn memory information. */
RKNN_QUERY_CMD_MAX
} rknn_query_cmd;
/*
the tensor data type.
*/
typedef enum _rknn_tensor_type {
RKNN_TENSOR_FLOAT32 = 0, /* data type is float32. */
RKNN_TENSOR_FLOAT16, /* data type is float16. */
RKNN_TENSOR_INT8, /* data type is int8. */
RKNN_TENSOR_UINT8, /* data type is uint8. */
RKNN_TENSOR_INT16, /* data type is int16. */
RKNN_TENSOR_UINT16, /* data type is uint16. */
RKNN_TENSOR_INT32, /* data type is int32. */
RKNN_TENSOR_UINT32, /* data type is uint32. */
RKNN_TENSOR_INT64, /* data type is int64. */
RKNN_TENSOR_BOOL,
RKNN_TENSOR_TYPE_MAX
} rknn_tensor_type;
inline static const char* get_type_string(rknn_tensor_type type)
{
switch(type) {
case RKNN_TENSOR_FLOAT32: return "FP32";
case RKNN_TENSOR_FLOAT16: return "FP16";
case RKNN_TENSOR_INT8: return "INT8";
case RKNN_TENSOR_UINT8: return "UINT8";
case RKNN_TENSOR_INT16: return "INT16";
case RKNN_TENSOR_UINT16: return "UINT16";
case RKNN_TENSOR_INT32: return "INT32";
case RKNN_TENSOR_UINT32: return "UINT32";
case RKNN_TENSOR_INT64: return "INT64";
case RKNN_TENSOR_BOOL: return "BOOL";
default: return "UNKNOW";
}
}
/*
the quantitative type.
*/
typedef enum _rknn_tensor_qnt_type {
RKNN_TENSOR_QNT_NONE = 0, /* none. */
RKNN_TENSOR_QNT_DFP, /* dynamic fixed point. */
RKNN_TENSOR_QNT_AFFINE_ASYMMETRIC, /* asymmetric affine. */
RKNN_TENSOR_QNT_MAX
} rknn_tensor_qnt_type;
inline static const char* get_qnt_type_string(rknn_tensor_qnt_type type)
{
switch(type) {
case RKNN_TENSOR_QNT_NONE: return "NONE";
case RKNN_TENSOR_QNT_DFP: return "DFP";
case RKNN_TENSOR_QNT_AFFINE_ASYMMETRIC: return "AFFINE";
default: return "UNKNOW";
}
}
/*
the tensor data format.
*/
typedef enum _rknn_tensor_format {
RKNN_TENSOR_NCHW = 0, /* data format is NCHW. */
RKNN_TENSOR_NHWC, /* data format is NHWC. */
RKNN_TENSOR_NC1HWC2, /* data format is NC1HWC2. */
RKNN_TENSOR_UNDEFINED,
RKNN_TENSOR_FORMAT_MAX
} rknn_tensor_format;
/*
the mode of running on target NPU core.
*/
typedef enum _rknn_core_mask {
RKNN_NPU_CORE_AUTO = 0, /* default, run on NPU core randomly. */
RKNN_NPU_CORE_0 = 1, /* run on NPU core 0. */
RKNN_NPU_CORE_1 = 2, /* run on NPU core 1. */
RKNN_NPU_CORE_2 = 4, /* run on NPU core 2. */
RKNN_NPU_CORE_0_1 = RKNN_NPU_CORE_0 | RKNN_NPU_CORE_1, /* run on NPU core 1 and core 2. */
RKNN_NPU_CORE_0_1_2 = RKNN_NPU_CORE_0_1 | RKNN_NPU_CORE_2, /* run on NPU core 1 and core 2 and core 3. */
RKNN_NPU_CORE_UNDEFINED,
} rknn_core_mask;
inline static const char* get_format_string(rknn_tensor_format fmt)
{
switch(fmt) {
case RKNN_TENSOR_NCHW: return "NCHW";
case RKNN_TENSOR_NHWC: return "NHWC";
case RKNN_TENSOR_NC1HWC2: return "NC1HWC2";
case RKNN_TENSOR_UNDEFINED: return "UNDEFINED";
default: return "UNKNOW";
}
}
/*
the information for RKNN_QUERY_IN_OUT_NUM.
*/
typedef struct _rknn_input_output_num {
uint32_t n_input; /* the number of input. */
uint32_t n_output; /* the number of output. */
} rknn_input_output_num;
/*
the information for RKNN_QUERY_INPUT_ATTR / RKNN_QUERY_OUTPUT_ATTR.
*/
typedef struct _rknn_tensor_attr {
uint32_t index; /* input parameter, the index of input/output tensor,
need set before call rknn_query. */
uint32_t n_dims; /* the number of dimensions. */
uint32_t dims[RKNN_MAX_DIMS]; /* the dimensions array. */
char name[RKNN_MAX_NAME_LEN]; /* the name of tensor. */
uint32_t n_elems; /* the number of elements. */
uint32_t size; /* the bytes size of tensor. */
rknn_tensor_format fmt; /* the data format of tensor. */
rknn_tensor_type type; /* the data type of tensor. */
rknn_tensor_qnt_type qnt_type; /* the quantitative type of tensor. */
int8_t fl; /* fractional length for RKNN_TENSOR_QNT_DFP. */
int32_t zp; /* zero point for RKNN_TENSOR_QNT_AFFINE_ASYMMETRIC. */
float scale; /* scale for RKNN_TENSOR_QNT_AFFINE_ASYMMETRIC. */
uint32_t w_stride; /* the stride of tensor along the width dimention of input,
Note: it is read-only, 0 means equal to width. */
uint32_t size_with_stride; /* the bytes size of tensor with stride. */
uint8_t pass_through; /* pass through mode, for rknn_set_io_mem interface.
if TRUE, the buf data is passed directly to the input node of the rknn model
without any conversion. the following variables do not need to be set.
if FALSE, the buf data is converted into an input consistent with the model
according to the following type and fmt. so the following variables
need to be set.*/
uint32_t h_stride; /* the stride along the height dimention of input,
Note: it is write-only, if it was set to 0, h_stride = height. */
} rknn_tensor_attr;
/*
the information for RKNN_QUERY_PERF_DETAIL.
*/
typedef struct _rknn_perf_detail {
char* perf_data; /* the string pointer of perf detail. don't need free it by user. */
uint64_t data_len; /* the string length. */
} rknn_perf_detail;
/*
the information for RKNN_QUERY_PERF_RUN.
*/
typedef struct _rknn_perf_run {
int64_t run_duration; /* real inference time (us) */
} rknn_perf_run;
/*
the information for RKNN_QUERY_SDK_VERSION.
*/
typedef struct _rknn_sdk_version {
char api_version[256]; /* the version of rknn api. */
char drv_version[256]; /* the version of rknn driver. */
} rknn_sdk_version;
/*
the information for RKNN_QUERY_MEM_SIZE.
*/
typedef struct _rknn_mem_size {
uint32_t total_weight_size; /* the weight memory size */
uint32_t total_internal_size; /* the internal memory size, exclude inputs/outputs */
uint64_t total_dma_allocated_size; /* total dma memory allocated size */
uint32_t total_sram_size; /* total system sram size reserved for rknn */
uint32_t free_sram_size; /* free system sram size reserved for rknn */
uint32_t reserved[10]; /* reserved */
} rknn_mem_size;
/*
the information for RKNN_QUERY_CUSTOM_STRING.
*/
typedef struct _rknn_custom_string {
char string[1024]; /* the string of custom, lengths max to 1024 bytes */
} rknn_custom_string;
/*
The flags of rknn_tensor_mem.
*/
typedef enum _rknn_tensor_mem_flags {
RKNN_TENSOR_MEMORY_FLAGS_ALLOC_INSIDE = 1, /*Used to mark in rknn_destroy_mem() whether it is necessary to release the "mem" pointer itself.
If the flag RKNN_TENSOR_MEMORY_FLAGS_ALLOC_INSIDE is set, rknn_destroy_mem() will call free(mem).*/
RKNN_TENSOR_MEMORY_FLAGS_FROM_FD = 2, /*Used to mark in rknn_create_mem_from_fd() whether it is necessary to release the "mem" pointer itself.
If the flag RKNN_TENSOR_MEMORY_FLAGS_FROM_FD is set, rknn_destroy_mem() will call free(mem).*/
RKNN_TENSOR_MEMORY_FLAGS_FROM_PHYS = 3, /*Used to mark in rknn_create_mem_from_phys() whether it is necessary to release the "mem" pointer itself.
If the flag RKNN_TENSOR_MEMORY_FLAGS_FROM_PHYS is set, rknn_destroy_mem() will call free(mem).*/
RKNN_TENSOR_MEMORY_FLAGS_UNKNOWN
} rknn_tensor_mem_flags;
/*
the memory information of tensor.
*/
typedef struct _rknn_tensor_memory {
void* virt_addr; /* the virtual address of tensor buffer. */
uint64_t phys_addr; /* the physical address of tensor buffer. */
int32_t fd; /* the fd of tensor buffer. */
int32_t offset; /* indicates the offset of the memory. */
uint32_t size; /* the size of tensor buffer. */
uint32_t flags; /* the flags of tensor buffer, reserved */
void * priv_data; /* the private data of tensor buffer. */
} rknn_tensor_mem;
/*
the input information for rknn_input_set.
*/
typedef struct _rknn_input {
uint32_t index; /* the input index. */
void* buf; /* the input buf for index. */
uint32_t size; /* the size of input buf. */
uint8_t pass_through; /* pass through mode.
if TRUE, the buf data is passed directly to the input node of the rknn model
without any conversion. the following variables do not need to be set.
if FALSE, the buf data is converted into an input consistent with the model
according to the following type and fmt. so the following variables
need to be set.*/
rknn_tensor_type type; /* the data type of input buf. */
rknn_tensor_format fmt; /* the data format of input buf.
currently the internal input format of NPU is NCHW by default.
so entering NCHW data can avoid the format conversion in the driver. */
} rknn_input;
/*
the output information for rknn_outputs_get.
*/
typedef struct _rknn_output {
uint8_t want_float; /* want transfer output data to float */
uint8_t is_prealloc; /* whether buf is pre-allocated.
if TRUE, the following variables need to be set.
if FALSE, the following variables do not need to be set. */
uint32_t index; /* the output index. */
void* buf; /* the output buf for index.
when is_prealloc = FALSE and rknn_outputs_release called,
this buf pointer will be free and don't use it anymore. */
uint32_t size; /* the size of output buf. */
} rknn_output;
/*
the extend information for rknn_init.
*/
typedef struct _rknn_init_extend {
rknn_context ctx; /* rknn context */
int32_t real_model_offset; /* real rknn model file size, only valid when init context with rknn file path */
uint32_t real_model_size; /* real rknn model file offset, only valid when init context with rknn file path */
uint8_t reserved[120]; /* reserved */
} rknn_init_extend;
/*
the extend information for rknn_run.
*/
typedef struct _rknn_run_extend {
uint64_t frame_id; /* output parameter, indicate current frame id of run. */
int32_t non_block; /* block flag of run, 0 is block else 1 is non block */
int32_t timeout_ms; /* timeout for block mode, in milliseconds */
int32_t fence_fd; /* fence fd from other unit */
} rknn_run_extend;
/*
the extend information for rknn_outputs_get.
*/
typedef struct _rknn_output_extend {
uint64_t frame_id; /* output parameter, indicate the frame id of outputs, corresponds to
struct rknn_run_extend.frame_id.*/
} rknn_output_extend;
/* rknn_init
initial the context and load the rknn model.
input:
rknn_context* context the pointer of context handle.
void* model if size > 0, pointer to the rknn model, if size = 0, filepath to the rknn model.
uint32_t size the size of rknn model.
uint32_t flag extend flag, see the define of RKNN_FLAG_XXX_XXX.
rknn_init_extend* extend the extend information of init.
return:
int error code.
*/
int rknn_init(rknn_context* context, void* model, uint32_t size, uint32_t flag, rknn_init_extend* extend);
/* rknn_dup_context
initial the context and load the rknn model.
input:
rknn_context* context_in the pointer of context in handle.
rknn_context* context_out the pointer of context out handle.
return:
int error code.
*/
int rknn_dup_context(rknn_context* context_in, rknn_context* context_out);
/* rknn_destroy
unload the rknn model and destroy the context.
input:
rknn_context context the handle of context.
return:
int error code.
*/
int rknn_destroy(rknn_context context);
/* rknn_query
query the information about model or others. see rknn_query_cmd.
input:
rknn_context context the handle of context.
rknn_query_cmd cmd the command of query.
void* info the buffer point of information.
uint32_t size the size of information.
return:
int error code.
*/
int rknn_query(rknn_context context, rknn_query_cmd cmd, void* info, uint32_t size);
/* rknn_inputs_set
set inputs information by input index of rknn model.
inputs information see rknn_input.
input:
rknn_context context the handle of context.
uint32_t n_inputs the number of inputs.
rknn_input inputs[] the arrays of inputs information, see rknn_input.
return:
int error code
*/
int rknn_inputs_set(rknn_context context, uint32_t n_inputs, rknn_input inputs[]);
/*
rknn_set_batch_core_num
set rknn batch core_num.
input:
rknn_context context the handle of context.
int core_num the core number.
return:
int error code.
*/
int rknn_set_batch_core_num(rknn_context context, int core_num);
/* rknn_set_core_mask
set rknn core mask.(only supported on RK3588 now)
RKNN_NPU_CORE_AUTO: auto mode, default value
RKNN_NPU_CORE_0: core 0 mode
RKNN_NPU_CORE_1: core 1 mode
RKNN_NPU_CORE_2: core 2 mode
RKNN_NPU_CORE_0_1: combine core 0/1 mode
RKNN_NPU_CORE_0_1_2: combine core 0/1/2 mode
input:
rknn_context context the handle of context.
rknn_core_mask core_mask the core mask.
return:
int error code.
*/
int rknn_set_core_mask(rknn_context context, rknn_core_mask core_mask);
/* rknn_run
run the model to execute inference.
input:
rknn_context context the handle of context.
rknn_run_extend* extend the extend information of run.
return:
int error code.
*/
int rknn_run(rknn_context context, rknn_run_extend* extend);
/* rknn_wait
wait the model after execute inference.
input:
rknn_context context the handle of context.
rknn_run_extend* extend the extend information of run.
return:
int error code.
*/
int rknn_wait(rknn_context context, rknn_run_extend* extend);
/* rknn_outputs_get
wait the inference to finish and get the outputs.
this function will block until inference finish.
the results will set to outputs[].
input:
rknn_context context the handle of context.
uint32_t n_outputs the number of outputs.
rknn_output outputs[] the arrays of output, see rknn_output.
rknn_output_extend* the extend information of output.
return:
int error code.
*/
int rknn_outputs_get(rknn_context context, uint32_t n_outputs, rknn_output outputs[], rknn_output_extend* extend);
/* rknn_outputs_release
release the outputs that get by rknn_outputs_get.
after called, the rknn_output[x].buf get from rknn_outputs_get will
also be free when rknn_output[x].is_prealloc = FALSE.
input:
rknn_context context the handle of context.
uint32_t n_ouputs the number of outputs.
rknn_output outputs[] the arrays of output.
return:
int error code
*/
int rknn_outputs_release(rknn_context context, uint32_t n_ouputs, rknn_output outputs[]);
/* new api for zero copy */
/* rknn_create_mem_from_phys (memory allocated outside)
initialize tensor memory from physical address.
input:
rknn_context ctx the handle of context.
uint64_t phys_addr physical address.
void *virt_addr virtual address.
uint32_t size the size of tensor buffer.
return:
rknn_tensor_mem the pointer of tensor memory information.
*/
rknn_tensor_mem* rknn_create_mem_from_phys(rknn_context ctx, uint64_t phys_addr, void *virt_addr, uint32_t size);
/* rknn_create_mem_from_fd (memory allocated outside)
initialize tensor memory from file description.
input:
rknn_context ctx the handle of context.
int32_t fd file description.
void *virt_addr virtual address.
uint32_t size the size of tensor buffer.
int32_t offset indicates the offset of the memory (virt_addr without offset).
return:
rknn_tensor_mem the pointer of tensor memory information.
*/
rknn_tensor_mem* rknn_create_mem_from_fd(rknn_context ctx, int32_t fd, void *virt_addr, uint32_t size, int32_t offset);
/* rknn_create_mem_from_mb_blk (memory allocated outside)
create tensor memory from mb_blk.
input:
rknn_context ctx the handle of context.
void *mb_blk mb_blk allocate from system api.
int32_t offset indicates the offset of the memory.
return:
rknn_tensor_mem the pointer of tensor memory information.
*/
rknn_tensor_mem* rknn_create_mem_from_mb_blk(rknn_context ctx, void *mb_blk, int32_t offset);
/* rknn_create_mem (memory allocated inside)
create tensor memory.
input:
rknn_context ctx the handle of context.
uint32_t size the size of tensor buffer.
return:
rknn_tensor_mem the pointer of tensor memory information.
*/
rknn_tensor_mem* rknn_create_mem(rknn_context ctx, uint32_t size);
/* rknn_destroy_mem (support allocate inside and outside)
destroy tensor memory.
input:
rknn_context ctx the handle of context.
rknn_tensor_mem *mem the pointer of tensor memory information.
return:
int error code
*/
int rknn_destroy_mem(rknn_context ctx, rknn_tensor_mem *mem);
/* rknn_set_weight_mem
set the weight memory.
input:
rknn_context ctx the handle of context.
rknn_tensor_mem *mem the array of tensor memory information
return:
int error code.
*/
int rknn_set_weight_mem(rknn_context ctx, rknn_tensor_mem *mem);
/* rknn_set_internal_mem
set the internal memory.
input:
rknn_context ctx the handle of context.
rknn_tensor_mem *mem the array of tensor memory information
return:
int error code.
*/
int rknn_set_internal_mem(rknn_context ctx, rknn_tensor_mem *mem);
/* rknn_set_io_mem
set the input and output tensors buffer.
input:
rknn_context ctx the handle of context.
rknn_tensor_mem *mem the array of tensor memory information.
rknn_tensor_attr *attr the attribute of input or output tensor buffer.
return:
int error code.
*/
int rknn_set_io_mem(rknn_context ctx, rknn_tensor_mem *mem, rknn_tensor_attr *attr);
#ifdef __cplusplus
} //extern "C"
#endif
#endif //_RKNN_API_H

View File

@@ -0,0 +1,80 @@
person
bicycle
car
motorcycle
airplane
bus
train
truck
boat
traffic light
fire hydrant
stop sign
parking meter
bench
bird
cat
dog
horse
sheep
cow
elephant
bear
zebra
giraffe
backpack
umbrella
handbag
tie
suitcase
frisbee
skis
snowboard
sports ball
kite
baseball bat
baseball glove
skateboard
surfboard
tennis racket
bottle
wine glass
cup
fork
knife
spoon
bowl
banana
apple
sandwich
orange
broccoli
carrot
hot dog
pizza
donut
cake
chair
couch
potted plant
bed
dining table
toilet
tv
laptop
mouse
remote
keyboard
cell phone
microwave
oven
toaster
sink
refrigerator
book
clock
vase
scissors
teddy bear
hair drier
toothbrush

View File

@@ -0,0 +1,31 @@
# 请切换到root用户
# CPU定频
echo "CPU0-3 可用频率:"
sudo cat /sys/devices/system/cpu/cpufreq/policy0/scaling_available_frequencies
sudo echo userspace > /sys/devices/system/cpu/cpufreq/policy0/scaling_governor
sudo echo 1800000 > /sys/devices/system/cpu/cpufreq/policy0/scaling_setspeed
echo "CPU0-3 当前频率:"
sudo cat /sys/devices/system/cpu/cpufreq/policy0/cpuinfo_cur_freq
echo "CPU4-5 可用频率:"
sudo cat /sys/devices/system/cpu/cpufreq/policy4/scaling_available_frequencies
sudo echo userspace > /sys/devices/system/cpu/cpufreq/policy4/scaling_governor
sudo echo 2400000 > /sys/devices/system/cpu/cpufreq/policy4/scaling_setspeed
echo "CPU4-5 当前频率:"
sudo cat /sys/devices/system/cpu/cpufreq/policy4/cpuinfo_cur_freq
echo "CPU6-7 可用频率:"
sudo cat /sys/devices/system/cpu/cpufreq/policy6/scaling_available_frequencies
sudo echo userspace > /sys/devices/system/cpu/cpufreq/policy6/scaling_governor
sudo echo 2400000 > /sys/devices/system/cpu/cpufreq/policy6/scaling_setspeed
echo "CPU6-7 当前频率:"
sudo cat /sys/devices/system/cpu/cpufreq/policy6/cpuinfo_cur_freq
# NPU定频
echo "NPU 可用频率:"
sudo cat /sys/class/devfreq/fdab0000.npu/available_frequencies
sudo echo userspace > /sys/class/devfreq/fdab0000.npu/governor
sudo echo 1000000000 > /sys/class/devfreq/fdab0000.npu/userspace/set_freq
echo "NPU 当前频率:"
sudo cat /sys/class/devfreq/fdab0000.npu/cur_freq

View File

@@ -0,0 +1,114 @@
// Copyright (c) 2021 by Rockchip Electronics Co., Ltd. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*-------------------------------------------
Includes
-------------------------------------------*/
#include <stdio.h>
#include <sys/time.h>
#include <thread>
#include <queue>
#include <vector>
#define _BASETSD_H
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "rknnPool.hpp"
#include "ThreadPool.hpp"
using std::queue;
using std::time;
using std::time_t;
using std::vector;
int main(int argc, char **argv)
{
char *model_name = NULL;
if (argc != 3)
{
printf("Usage: %s <rknn model> <jpg> \n", argv[0]);
return -1;
}
model_name = (char *)argv[1]; // 参数二,模型所在路径
char *image_name = argv[2]; // 参数三, 视频/摄像头
printf("模型名称:\t%s\n", model_name);
cv::VideoCapture capture;
cv::namedWindow("Camera FPS");
if (strlen(image_name) == 1)
capture.open((int)(image_name[0] - '0'));
else
capture.open(image_name);
// 设置线程数
int n = 6, frames = 0;
printf("线程数:\t%d\n", n);
// 类似于多个rk模型的集合?
vector<rknn_lite *> rkpool;
// 线程池
dpool::ThreadPool pool(n);
// 线程队列
queue<std::future<int>> futs;
//初始化
for (int i = 0; i < n; i++)
{
rknn_lite *ptr = new rknn_lite(model_name, i % 3);
rkpool.push_back(ptr);
capture >> ptr->ori_img;
futs.push(pool.submit(&rknn_lite::interf, &(*ptr)));
}
struct timeval time;
gettimeofday(&time, nullptr);
auto initTime = time.tv_sec * 1000 + time.tv_usec / 1000;
gettimeofday(&time, nullptr);
long tmpTime, lopTime = time.tv_sec * 1000 + time.tv_usec / 1000;
while (capture.isOpened())
{
if (futs.front().get() != 0)
break;
futs.pop();
cv::imshow("Camera FPS", rkpool[frames % n]->ori_img);
if (cv::waitKey(1) == 'q') // 延时1毫秒,按q键退出
break;
if(!capture.read(rkpool[frames % n]->ori_img))
break;
futs.push(pool.submit(&rknn_lite::interf, &(*rkpool[frames++ % n])));
if(frames % 60 == 0){
gettimeofday(&time, nullptr);
tmpTime = time.tv_sec * 1000 + time.tv_usec / 1000;
printf("60帧平均帧率:\t%f帧\n", 60000.0 / (float)(tmpTime - lopTime));
lopTime = tmpTime;
}
}
gettimeofday(&time, nullptr);
printf("\n平均帧率:\t%f帧\n", float(frames) / (float)(time.tv_sec * 1000 + time.tv_usec / 1000 - initTime + 0.0001) * 1000.0);
// 释放剩下的资源
while (!futs.empty())
{
if (futs.front().get())
break;
futs.pop();
}
for (int i = 0; i < n; i++)
delete rkpool[i];
capture.release();
cv::destroyAllWindows();
return 0;
}

View File

@@ -0,0 +1,345 @@
// Copyright (c) 2021 by Rockchip Electronics Co., Ltd. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "postprocess.h"
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <set>
#include <vector>
#define LABEL_NALE_TXT_PATH "./model/coco_80_labels_list.txt"
static char* labels[OBJ_CLASS_NUM];
const int anchor0[6] = {10, 13, 16, 30, 33, 23};
const int anchor1[6] = {30, 61, 62, 45, 59, 119};
const int anchor2[6] = {116, 90, 156, 198, 373, 326};
inline static int clamp(float val, int min, int max) { return val > min ? (val < max ? val : max) : min; }
char* readLine(FILE* fp, char* buffer, int* len)
{
int ch;
int i = 0;
size_t buff_len = 0;
buffer = (char*)malloc(buff_len + 1);
if (!buffer)
return NULL; // Out of memory
while ((ch = fgetc(fp)) != '\n' && ch != EOF) {
buff_len++;
void* tmp = realloc(buffer, buff_len + 1);
if (tmp == NULL) {
free(buffer);
return NULL; // Out of memory
}
buffer = (char*)tmp;
buffer[i] = (char)ch;
i++;
}
buffer[i] = '\0';
*len = buff_len;
// Detect end
if (ch == EOF && (i == 0 || ferror(fp))) {
free(buffer);
return NULL;
}
return buffer;
}
int readLines(const char* fileName, char* lines[], int max_line)
{
FILE* file = fopen(fileName, "r");
char* s;
int i = 0;
int n = 0;
if (file == NULL) {
printf("Open %s fail!\n", fileName);
return -1;
}
while ((s = readLine(file, s, &n)) != NULL) {
lines[i++] = s;
if (i >= max_line)
break;
}
fclose(file);
return i;
}
int loadLabelName(const char* locationFilename, char* label[])
{
printf("loadLabelName %s\n", locationFilename);
readLines(locationFilename, label, OBJ_CLASS_NUM);
return 0;
}
static float CalculateOverlap(float xmin0, float ymin0, float xmax0, float ymax0, float xmin1, float ymin1, float xmax1,
float ymax1)
{
float w = fmax(0.f, fmin(xmax0, xmax1) - fmax(xmin0, xmin1) + 1.0);
float h = fmax(0.f, fmin(ymax0, ymax1) - fmax(ymin0, ymin1) + 1.0);
float i = w * h;
float u = (xmax0 - xmin0 + 1.0) * (ymax0 - ymin0 + 1.0) + (xmax1 - xmin1 + 1.0) * (ymax1 - ymin1 + 1.0) - i;
return u <= 0.f ? 0.f : (i / u);
}
static int nms(int validCount, std::vector<float>& outputLocations, std::vector<int> classIds, std::vector<int>& order,
int filterId, float threshold)
{
for (int i = 0; i < validCount; ++i) {
if (order[i] == -1 || classIds[i] != filterId) {
continue;
}
int n = order[i];
for (int j = i + 1; j < validCount; ++j) {
int m = order[j];
if (m == -1 || classIds[i] != filterId) {
continue;
}
float xmin0 = outputLocations[n * 4 + 0];
float ymin0 = outputLocations[n * 4 + 1];
float xmax0 = outputLocations[n * 4 + 0] + outputLocations[n * 4 + 2];
float ymax0 = outputLocations[n * 4 + 1] + outputLocations[n * 4 + 3];
float xmin1 = outputLocations[m * 4 + 0];
float ymin1 = outputLocations[m * 4 + 1];
float xmax1 = outputLocations[m * 4 + 0] + outputLocations[m * 4 + 2];
float ymax1 = outputLocations[m * 4 + 1] + outputLocations[m * 4 + 3];
float iou = CalculateOverlap(xmin0, ymin0, xmax0, ymax0, xmin1, ymin1, xmax1, ymax1);
if (iou > threshold) {
order[j] = -1;
}
}
}
return 0;
}
static int quick_sort_indice_inverse(std::vector<float>& input, int left, int right, std::vector<int>& indices)
{
float key;
int key_index;
int low = left;
int high = right;
if (left < right) {
key_index = indices[left];
key = input[left];
while (low < high) {
while (low < high && input[high] <= key) {
high--;
}
input[low] = input[high];
indices[low] = indices[high];
while (low < high && input[low] >= key) {
low++;
}
input[high] = input[low];
indices[high] = indices[low];
}
input[low] = key;
indices[low] = key_index;
quick_sort_indice_inverse(input, left, low - 1, indices);
quick_sort_indice_inverse(input, low + 1, right, indices);
}
return low;
}
static float sigmoid(float x) { return 1.0 / (1.0 + expf(-x)); }
static float unsigmoid(float y) { return -1.0 * logf((1.0 / y) - 1.0); }
inline static int32_t __clip(float val, float min, float max)
{
float f = val <= min ? min : (val >= max ? max : val);
return f;
}
static int8_t qnt_f32_to_affine(float f32, int32_t zp, float scale)
{
float dst_val = (f32 / scale) + zp;
int8_t res = (int8_t)__clip(dst_val, -128, 127);
return res;
}
static float deqnt_affine_to_f32(int8_t qnt, int32_t zp, float scale) { return ((float)qnt - (float)zp) * scale; }
static int process(int8_t* input, int* anchor, int grid_h, int grid_w, int height, int width, int stride,
std::vector<float>& boxes, std::vector<float>& objProbs, std::vector<int>& classId, float threshold,
int32_t zp, float scale)
{
int validCount = 0;
int grid_len = grid_h * grid_w;
float thres = unsigmoid(threshold);
int8_t thres_i8 = qnt_f32_to_affine(thres, zp, scale);
for (int a = 0; a < 3; a++) {
for (int i = 0; i < grid_h; i++) {
for (int j = 0; j < grid_w; j++) {
int8_t box_confidence = input[(PROP_BOX_SIZE * a + 4) * grid_len + i * grid_w + j];
if (box_confidence >= thres_i8) {
int offset = (PROP_BOX_SIZE * a) * grid_len + i * grid_w + j;
int8_t* in_ptr = input + offset;
float box_x = sigmoid(deqnt_affine_to_f32(*in_ptr, zp, scale)) * 2.0 - 0.5;
float box_y = sigmoid(deqnt_affine_to_f32(in_ptr[grid_len], zp, scale)) * 2.0 - 0.5;
float box_w = sigmoid(deqnt_affine_to_f32(in_ptr[2 * grid_len], zp, scale)) * 2.0;
float box_h = sigmoid(deqnt_affine_to_f32(in_ptr[3 * grid_len], zp, scale)) * 2.0;
box_x = (box_x + j) * (float)stride;
box_y = (box_y + i) * (float)stride;
box_w = box_w * box_w * (float)anchor[a * 2];
box_h = box_h * box_h * (float)anchor[a * 2 + 1];
box_x -= (box_w / 2.0);
box_y -= (box_h / 2.0);
int8_t maxClassProbs = in_ptr[5 * grid_len];
int maxClassId = 0;
for (int k = 1; k < OBJ_CLASS_NUM; ++k) {
int8_t prob = in_ptr[(5 + k) * grid_len];
if (prob > maxClassProbs) {
maxClassId = k;
maxClassProbs = prob;
}
}
if (maxClassProbs>thres_i8){
objProbs.push_back(sigmoid(deqnt_affine_to_f32(maxClassProbs, zp, scale))* sigmoid(deqnt_affine_to_f32(box_confidence, zp, scale)));
classId.push_back(maxClassId);
validCount++;
boxes.push_back(box_x);
boxes.push_back(box_y);
boxes.push_back(box_w);
boxes.push_back(box_h);
}
}
}
}
}
return validCount;
}
int post_process(int8_t* input0, int8_t* input1, int8_t* input2, int model_in_h, int model_in_w, float conf_threshold,
float nms_threshold, float scale_w, float scale_h, std::vector<int32_t>& qnt_zps,
std::vector<float>& qnt_scales, detect_result_group_t* group)
{
static int init = -1;
if (init == -1) {
int ret = 0;
ret = loadLabelName(LABEL_NALE_TXT_PATH, labels);
if (ret < 0) {
return -1;
}
init = 0;
}
memset(group, 0, sizeof(detect_result_group_t));
std::vector<float> filterBoxes;
std::vector<float> objProbs;
std::vector<int> classId;
// stride 8
int stride0 = 8;
int grid_h0 = model_in_h / stride0;
int grid_w0 = model_in_w / stride0;
int validCount0 = 0;
validCount0 = process(input0, (int*)anchor0, grid_h0, grid_w0, model_in_h, model_in_w, stride0, filterBoxes, objProbs,
classId, conf_threshold, qnt_zps[0], qnt_scales[0]);
// stride 16
int stride1 = 16;
int grid_h1 = model_in_h / stride1;
int grid_w1 = model_in_w / stride1;
int validCount1 = 0;
validCount1 = process(input1, (int*)anchor1, grid_h1, grid_w1, model_in_h, model_in_w, stride1, filterBoxes, objProbs,
classId, conf_threshold, qnt_zps[1], qnt_scales[1]);
// stride 32
int stride2 = 32;
int grid_h2 = model_in_h / stride2;
int grid_w2 = model_in_w / stride2;
int validCount2 = 0;
validCount2 = process(input2, (int*)anchor2, grid_h2, grid_w2, model_in_h, model_in_w, stride2, filterBoxes, objProbs,
classId, conf_threshold, qnt_zps[2], qnt_scales[2]);
int validCount = validCount0 + validCount1 + validCount2;
// no object detect
if (validCount <= 0) {
return 0;
}
std::vector<int> indexArray;
for (int i = 0; i < validCount; ++i) {
indexArray.push_back(i);
}
quick_sort_indice_inverse(objProbs, 0, validCount - 1, indexArray);
std::set<int> class_set(std::begin(classId), std::end(classId));
for (auto c : class_set) {
nms(validCount, filterBoxes, classId, indexArray, c, nms_threshold);
}
int last_count = 0;
group->count = 0;
/* box valid detect target */
for (int i = 0; i < validCount; ++i) {
if (indexArray[i] == -1 || last_count >= OBJ_NUMB_MAX_SIZE) {
continue;
}
int n = indexArray[i];
float x1 = filterBoxes[n * 4 + 0];
float y1 = filterBoxes[n * 4 + 1];
float x2 = x1 + filterBoxes[n * 4 + 2];
float y2 = y1 + filterBoxes[n * 4 + 3];
int id = classId[n];
float obj_conf = objProbs[i];
group->results[last_count].box.left = (int)(clamp(x1, 0, model_in_w) / scale_w);
group->results[last_count].box.top = (int)(clamp(y1, 0, model_in_h) / scale_h);
group->results[last_count].box.right = (int)(clamp(x2, 0, model_in_w) / scale_w);
group->results[last_count].box.bottom = (int)(clamp(y2, 0, model_in_h) / scale_h);
group->results[last_count].prop = obj_conf;
char* label = labels[id];
strncpy(group->results[last_count].name, label, OBJ_NAME_MAX_SIZE);
// printf("result %2d: (%4d, %4d, %4d, %4d), %s\n", i, group->results[last_count].box.left,
// group->results[last_count].box.top,
// group->results[last_count].box.right, group->results[last_count].box.bottom, label);
last_count++;
}
group->count = last_count;
return 0;
}
void deinitPostProcess()
{
for (int i = 0; i < OBJ_CLASS_NUM; i++) {
if (labels[i] != nullptr) {
free(labels[i]);
labels[i] = nullptr;
}
}
}

7
NpuYoloV5/REDME.md Normal file
View File

@@ -0,0 +1,7 @@
./01_miniconda_arm64 保存conda安装脚本
./02_packages 保存RKNPU的安装包
./03_rk3588_hdmi_in_out_py HDMI IN的测试代码
./04_yolov5 YOLOV5的测试代码
./05_rknn-multi-threaded-nosigmoid 大佬的YOLOV5的python代码
./06_rknn-cpp-Multithreading-main 大佬的YOLOV5的C++代码
./install YOLOV5的识别程序由06_rknn-cpp-Multithreading-main文件夹中的代码编译而成

Binary file not shown.

View File

@@ -0,0 +1,80 @@
person
bicycle
car
motorcycle
airplane
bus
train
truck
boat
traffic light
fire hydrant
stop sign
parking meter
bench
bird
cat
dog
horse
sheep
cow
elephant
bear
zebra
giraffe
backpack
umbrella
handbag
tie
suitcase
frisbee
skis
snowboard
sports ball
kite
baseball bat
baseball glove
skateboard
surfboard
tennis racket
bottle
wine glass
cup
fork
knife
spoon
bowl
banana
apple
sandwich
orange
broccoli
carrot
hot dog
pizza
donut
cake
chair
couch
potted plant
bed
dining table
toilet
tv
laptop
mouse
remote
keyboard
cell phone
microwave
oven
toaster
sink
refrigerator
book
clock
vase
scissors
teddy bear
hair drier
toothbrush

2
REDME.md Normal file
View File

@@ -0,0 +1,2 @@
./hdmiIn 文件夹用来保存HDMI输入的代码
./NpuYoloV5 文件夹用来保存NPU推理的代码

1
hdmiIn/REDME.md Normal file
View File

@@ -0,0 +1 @@
./rk3588_hdmi_in_out-in_and_out 目录用来保存hdmi输入虚拟出video输出的代码

Binary file not shown.

View File

@@ -0,0 +1,226 @@
---
Language: Cpp
# BasedOnStyle: Google
AccessModifierOffset: -1
AlignAfterOpenBracket: Align
AlignArrayOfStructures: None
AlignConsecutiveMacros: None
AlignConsecutiveAssignments: None
AlignConsecutiveBitFields: None
AlignConsecutiveDeclarations: None
AlignEscapedNewlines: Left
AlignOperands: Align
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortEnumsOnASingleLine: true
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: WithoutElse
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: Yes
AttributeMacros:
- __capability
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeConceptDeclarations: true
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
QualifierAlignment: Leave
CompactNamespaces: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DeriveLineEnding: true
DerivePointerAlignment: true
DisableFormat: false
EmptyLineAfterAccessModifier: Never
EmptyLineBeforeAccessModifier: LogicalBlock
ExperimentalAutoDetectBinPacking: false
PackConstructorInitializers: NextLine
BasedOnStyle: ''
ConstructorInitializerAllOnOneLineOrOnePerLine: false
AllowAllConstructorInitializersOnNextLine: true
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IfMacros:
- KJ_IF_MAYBE
IncludeBlocks: Regroup
IncludeCategories:
- Regex: '^<ext/.*\.h>'
Priority: 2
SortPriority: 0
CaseSensitive: false
- Regex: '^<.*\.h>'
Priority: 1
SortPriority: 0
CaseSensitive: false
- Regex: '^<.*'
Priority: 2
SortPriority: 0
CaseSensitive: false
- Regex: '.*'
Priority: 3
SortPriority: 0
CaseSensitive: false
IncludeIsMainRegex: '([-_](test|unittest))?$'
IncludeIsMainSourceRegex: ''
IndentAccessModifiers: false
IndentCaseLabels: true
IndentCaseBlocks: false
IndentGotoLabels: true
IndentPPDirectives: None
IndentExternBlock: AfterExternBlock
IndentRequires: false
IndentWidth: 2
IndentWrappedFunctionNames: false
InsertTrailingCommas: None
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
LambdaBodyIndentation: Signature
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Never
ObjCBlockIndentWidth: 2
ObjCBreakBeforeNestedBlockParam: true
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakOpenParenthesis: 0
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PenaltyIndentedWhitespace: 0
PointerAlignment: Left
PPIndentWidth: -1
RawStringFormats:
- Language: Cpp
Delimiters:
- cc
- CC
- cpp
- Cpp
- CPP
- 'c++'
- 'C++'
CanonicalDelimiter: ''
BasedOnStyle: google
- Language: TextProto
Delimiters:
- pb
- PB
- proto
- PROTO
EnclosingFunctions:
- EqualsProto
- EquivToProto
- PARSE_PARTIAL_TEXT_PROTO
- PARSE_TEST_PROTO
- PARSE_TEXT_PROTO
- ParseTextOrDie
- ParseTextProtoOrDie
- ParseTestProto
- ParsePartialTestProto
CanonicalDelimiter: pb
BasedOnStyle: google
ReferenceAlignment: Pointer
ReflowComments: true
RemoveBracesLLVM: false
SeparateDefinitionBlocks: Leave
ShortNamespaceLines: 1
SortIncludes: CaseSensitive
SortJavaStaticImport: Before
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeParensOptions:
AfterControlStatements: true
AfterForeachMacros: true
AfterFunctionDefinitionName: false
AfterFunctionDeclarationName: false
AfterIfMacros: true
AfterOverloadedOperator: false
BeforeNonEmptyParentheses: false
SpaceAroundPointerQualifiers: Default
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: Never
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInLineCommentPrefix:
Minimum: 1
Maximum: -1
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
BitFieldColonSpacing: Both
Standard: Auto
StatementAttributeLikeMacros:
- Q_EMIT
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 8
UseCRLF: false
UseTab: Never
WhitespaceSensitiveMacros:
- STRINGIZE
- PP_STRINGIZE
- BOOST_PP_STRINGIZE
- NS_SWIFT_NAME
- CF_SWIFT_NAME
...

View File

@@ -0,0 +1,3 @@
cmake-build-*/
.idea
build

View File

@@ -0,0 +1,33 @@
cmake_minimum_required(VERSION 3.21)
project(rk3588_hdmi_in)
set(CMAKE_CXX_STANDARD 17)
# 查找所需的库
find_package(OpenCV REQUIRED)
find_package(kaylordut REQUIRED)
# 明确列出所有源文件
set(SOURCES
main.cpp
src/fps_counter.cpp
src/hdmi_in.cpp
src/framebuffer.cpp
src/video_saver.cpp
src/virtual_device.cpp
)
# 创建可执行文件
add_executable(rk3588_hdmi_in ${SOURCES})
# 包含头文件
target_include_directories(rk3588_hdmi_in PRIVATE
include
${OpenCV_INCLUDE_DIRS}
)
# 链接库
target_link_libraries(rk3588_hdmi_in PRIVATE
${OpenCV_LIBS}
${kaylordut_LIBS}
)

View File

@@ -0,0 +1,18 @@
#ifndef FPS_COUNTER_H
#define FPS_COUNTER_H
#include <chrono>
#include <deque>
class FPSCounter {
public:
FPSCounter(size_t window_size = 10);
double update();
private:
size_t window_size_;
std::deque<std::chrono::steady_clock::time_point> time_points_;
std::deque<double> fps_values_;
};
#endif // FPS_COUNTER_H

Some files were not shown because too many files have changed in this diff Show More