跳转至

VSR SDK集成

主要介绍个平台下vsr库接口调用步骤展示

通用接口集成步骤

适用平台:window、Android、iOS、MAC

//日志回调函数
static void SuperResolutionLog_Callback(VSRLogLevel leve, const char* func, int line, const char* fmt, ...){}

//callback
ImgoVsr_SetLogCallback(&SuperResolutionLog_Callback);
printf("version: %s\n", ImgoVsr_GetVersion());

//init 
VSRDeviceInfo info;
VSRResult ret = ImgoVsr_InitRuningEnvironment(&info, 3);
printf("ImgoVsr_InitRuningEnvironment: %d\n", ret);

//create object
ImgoVsrObject object;
ret = ImgoVsr_CreateObject(0, 0, 0, &object);
if (VSR_SUCCESS != ret)
{
    printf("create vsr object failed!, %d\n", ret);
    return -1;
}

//read image
cv1::Mat img = cv1::imread(img_path, cv1::IMREAD_GRAYSCALE);
int src_w = img.cols;
int src_h = img.rows;
int dst_w, dst_h;
ImgoVsr_GetWidthAndHeight(object, src_w, src_h, &dst_w, &dst_h);

//create new mat
cv1::Mat out_img(dst_h, dst_w, 1);

//excute sr
VSRRuningInfo run;
for(int i = 0; i < 30; i++)
    ImgoVsr_ExecuteY(object, img.data, src_w, src_h, src_w, out_img.data, dst_w);

ImgoVsr_GetRuningInfo(object, &run);
printf("Execute y time: %fms\n", run.current_time);

//save out img
cv1::imwrite("./out_img.png", out_img);

//destroy
ret = ImgoVsr_DestroyObject(object);
printf("ImgoVsr_DestroyObject: %d\n", ret);

//uninit
ImgoVsr_ReleaseRuningEnvironment();

vulkan接口集成步骤

适用平台:window、Android

//日志回调函数
static void SuperResolutionLog_Callback(VSRLogLevel leve, const char* func, int line, const char* fmt, ...){}

//callback
ImgoRender_SetLogCallback(&SuperResolutionLog_Callback);
printf("version: %s\n", ImgoVsr_GetVersion());

//window
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
GLFWwindow* window = glfwCreateWindow(1920, 1080, "Vulkan", nullptr, nullptr);
HWND hWnd = glfwGetWin32Window(window);

//init 
VSRResult ret = ImgoRender_InitializeEnvironment();
printf("ImgoRender_InitializeEnvironment: %d\n", ret);

//create render
ImgoRenderObject object;
RenderParams params;
params.windows = hWnd;
params.window_width = 1920;
params.window_height = 1080;
params.image_width = 960;
params.image_height = 540;
ret = ImgoRender_CreateRender(&params, &object);
if (VSR_SUCCESS != ret)
{
    printf("create vsr object failed!, %d\n", ret);
    return -1;
}

//read image
cv1::Mat img = cv1::imread(img_path, cv1::IMREAD_GRAYSCALE);

//excute display
unsigned char* y = input.data;
unsigned char* u = y + frame.cols * frame.rows;
unsigned char* v = y + frame.cols * frame.rows + (frame.cols * frame.rows / 4);

DisplayParams param;
param.format = RENDER_FORMAT_YUV_420;
param.data[0] = y;
param.data[1] = u;
param.data[2] = v;
param.stride[0] = frame.cols;
param.stride[1] = frame.cols >> 1;
param.stride[2] = frame.cols >> 1;
param.width = frame.cols;
param.height = frame.rows;
param.runing_3dlut = true;
param.runing_vsr = true;

ret = ImgoRender_Display(object, &param);
if (VSR_SUCCESS != ret)
{
    printf("ImgoRender_Display failed!, %d\n", ret);
    return -1;
}

VSRRuningInfo vsrinfo;
RenderRuningInfo renderinfo;
ImgoRender_GetRuningInfo(object, &vsrinfo, &renderinfo);
printf("Execute y time: %fms\n", vsrinfo.current_time);

//destroy
ret = ImgoRender_DestroyObject(object);
printf("ImgoRender_DestroyObject: %d\n", ret);

//uninit
ImgoRender_ReleaseEnvironment();

metal接口集成步骤

适用平台:iOS、MAC

//日志回调函数
static void SuperResolutionLog_Callback(VSRLogLevel leve, const char* func, int line, const char* fmt, ...){}

id<MTLDevice> device = MTLCreateSystemDefaultDevice();
if(device == nil)
{
    printf("device is empty!\n");
    return -1;
}

std::string img_path = "/Users/jack/Desktop/xiaoji/img.png";

//callback
ImgoVsr_MtlSetLogCallback(&SuperResolutionLog_Callback);
printf("version: %s\n", ImgoVsr_MtlGetVersion());

//init
VSRResult ret = ImgoVsr_MtlInitRuningEnvironment((__bridge void*)device, "/Users/jack/imgovsr/build/src/imgovsr.metallib");
printf("ImgoVsr_MtlInitRuningEnvironment: %d\n", ret);

//create object
ImgoVsrObject object = ImgoVsr_MtlCreateObject(0, 0);
if (NULL == object)
{
    printf("create vsr object failed\n");
    return -1;
}

//read image
cv1::Mat img = cv1::imread(img_path, cv1::IMREAD_GRAYSCALE);
int src_w = img.cols;
int src_h = img.rows;
int dst_w, dst_h;
ImgoVsr_MtlGetWidthAndHeight(object, src_w, src_h, &dst_w, &dst_h);

//create new mat
cv1::Mat out_img(dst_h, dst_w, 1);
cv1::Mat in_uv(dst_h, dst_w, 1);
cv1::Mat out_uv(dst_h, dst_w, 1);

//input texture
id<MTLTexture> input = create_Texture(img, src_w, src_h, device);

//output texture
id<MTLTexture> output = create_Texture_Empty(dst_w, dst_h, device);

//excute sr
VSRRuningInfo run;
for(int i = 0; i < 300; i++)
{
    //ImgoVsr_ExecuteY(object, img.data, src_w, src_h, src_w, out_img.data, dst_w);
    ImgoVsr_MtlExecuteY(object, (__bridge void*)input, src_w, src_h, (__bridge void*)output);
    ImgoVsr_MtlGetRuningInfo(object, &run);
    printf("Execute y time: %fms\n", run.current_time);
    //break;
}
printf("Execute y %d-%d->%d-%d avg time: %fms, min time: %fms, max time: %fms\n", src_w, src_h, dst_w, dst_h, run.avg_time, run.min_time, run.max_time);

if(output == nil) printf("output is empty!\n");
//save out img
[output getBytes: out_img.data bytesPerRow: dst_w fromRegion: MTLRegionMake2D(0, 0, dst_w, dst_h) mipmapLevel: 0];
cv1::imwrite("/Users/jack/Desktop/xiaoji/out_img.png", out_img);

//destroy
ret = ImgoVsr_MtlDestroyObject(object);
printf("ImgoVsr_MtlDestroyObject: %d\n", ret);

//uninit
ImgoVsr_MtlReleaseRuningEnvironment();
printf("ImgoVsr_MtlReleaseRuningEnvironment\n");