跳到主要内容

Decoder对象

Decoder对象实现了对视频数据的解码功能,包含了decodeset_imgget_imgclose等几种方法,详细说明如下:

decode

【功能描述】

使能decode解码模块,并对视频文件进行解码

【函数声明】
Decoder.decode(file, video_chn, decode_type, width, height)
【参数描述】
参数名称描述取值范围
file需要解码的文件名
video_chn指定视频解码器的通道号范围0~31
decode_type视频解码类型范围1~3,分别对应H264H265MJPEG
width输入解码模块的图像宽度不超过4096
height输入解码模块的图像高度不超过4096
【使用方法】
#create decode object
decode = libsrcampy.Decoder()

#enable decode channel 0, solution: 1080p, format: H264
ret = dec.decode("encode.h264", 0, 1, 1920, 1080)
【返回值】

返回值为2个成员的list数据

返回值定义描述
list[0]0:解码成功,-1:解码失败
list[1]输入码流文件的帧数,解码成功时有效
【注意事项】

【参考代码】

get_img

【功能描述】

获取解码模块的输出结果

【函数声明】
Decoder.get_img()
【参数描述】

【使用方法】
ret = dec.decode("encode.h264", 0, 1, 1920, 1080)
print ("Decoder return:%d frame count: %d" %(ret[0], ret[1]))

img = dec.get_img()
【返回值】
返回值定义描述
-1解码数据
【注意事项】

该接口需要在调用Decoder.decode()创建解码通道后使用

【参考代码】
import sys, os, time

import numpy as np
import cv2
from hobot_vio import libsrcampy

def test_decode():
#create decode object
dec = libsrcampy.Decoder()

#enable decode function
#decode input: encode.h264, solution: 1080p, format: h264
ret = dec.decode("encode.h264", 0, 1, 1920, 1080)
print ("Decoder return:%d frame count: %d" %(ret[0], ret[1]))

#get decoder output
img = dec.get_img()
if img is not None:
#save file
fo = open("output.img", "wb")
fo.write(img)
fo.close()
print("decode save img file success")
else:
print("decode save img file failed")

dec.close()
print("test_decode done!!!")

test_decode()

set_img

【功能描述】

将单帧编码数据送入解码模块,并进行解码

【函数声明】
Decoder.set_img(img, chn, eos)
【参数描述】
参数名称定义描述取值范围
img需要解码的单帧数据
chn解码器通道号范围0~31
eos解码数据是否结束0:未结束,1:结束
【使用方法】

【返回值】
返回值描述
0成功
-1失败
【注意事项】

该接口需要在调用Decoder.decode()创建解码通道后使用,且解码通道创建时入参file置空

【参考代码】
import sys, os, time

import numpy as np
import cv2
from hobot_vio import libsrcampy

def test_cam_bind_encode_decode_bind_display():
#camera start
cam = libsrcampy.Camera()
# If you know the pipe_id and video_index, you can specify the first two arguments.
# ret = cam.open_cam(0, 1, 30, [1920, 1280], [1080, 720])

# If you do not know the pipe_id and video_index, you can use the following
# code to detect them, and it will default to using the first detected camera.
ret = cam.open_cam(0, -1, 30, [1920, 1280], [1080, 720])
print("Camera open_cam return:%d" % ret)

#enable encoder
enc = libsrcampy.Encoder()
ret = enc.encode(0, 1, 1920, 1080)
print("Encoder encode return:%d" % ret)

#enable decoder
dec = libsrcampy.Decoder()
ret = dec.decode("", 0, 1, 1920, 1080)
print ("Decoder return:%d frame count: %d" %(ret[0], ret[1]))

ret = libsrcampy.bind(cam, enc)
print("libsrcampy bind return:%d" % ret)

a = 0
while a < 100:
#get encode image from encoder
img = enc.get_img()
if img is not None:
#send encode image to decoder
dec.set_img(img)
print("encode get image success count: %d" % a)
else:
print("encode get image failed count: %d" % a)
a = a + 1

ret = libsrcampy.unbind(cam, enc)
dec.close()
enc.close()
cam.close_cam()
print("test_cam_bind_encode_decode_bind_display done!!!")

test_cam_bind_encode_decode_bind_display()

close

【功能描述】

关闭解码模块

【函数声明】
Decoder.close()
【参数描述】

【使用方法】

【返回值】
返回值定义描述
0成功
-1失败
【注意事项】

退出程序时需要调用close接口以释放资源。

【参考代码】