1 /*
2 * Copyright 2015 Rockchip Electronics Co. LTD
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define MODULE_TAG "hal_h265e_api_v2"
18
19 #include <stdio.h>
20 #include <string.h>
21 #include <unistd.h>
22
23 #include "mpp_env.h"
24 #include "mpp_mem.h"
25 #include "mpp_debug.h"
26
27 #include "mpp_platform.h"
28 #include "vepu5xx.h"
29 #include "hal_h265e_api_v2.h"
30 #include "hal_h265e_vepu541.h"
31 #include "hal_h265e_vepu580.h"
32 #include "hal_h265e_vepu540c.h"
33 #include "hal_h265e_vepu510.h"
34 #include "hal_h265e_vepu511.h"
35
36 typedef struct HalH265eV2Ctx_t {
37 const MppEncHalApi *api;
38 void *hw_ctx;
39 } HalH265eV2Ctx;
40
41 RK_U32 hal_h265e_debug = 0;
42
hal_h265ev2_init(void * hal,MppEncHalCfg * cfg)43 static MPP_RET hal_h265ev2_init(void *hal, MppEncHalCfg *cfg)
44 {
45 HalH265eV2Ctx *ctx = (HalH265eV2Ctx *)hal;
46 const MppEncHalApi *api = NULL;
47 void *hw_ctx = NULL;
48 MPP_RET ret = MPP_OK;
49 RK_U32 vcodec_type = mpp_get_vcodec_type();
50
51 if (vcodec_type & HAVE_RKVENC) {
52 RK_U32 hw_id = mpp_get_client_hw_id(VPU_CLIENT_RKVENC);
53
54 switch (hw_id) {
55 case HWID_VEPU58X : {
56 api = &hal_h265e_vepu580;
57 } break;
58 case HWID_VEPU540C : {
59 api = &hal_h265e_vepu540c;
60 } break;
61 case HWID_VEPU510 : {
62 api = &hal_h265e_vepu510;
63 } break;
64 case HWID_VEPU511 : {
65 api = &hal_h265e_vepu511;
66 } break;
67 default : {
68 api = &hal_h265e_vepu541;
69 } break;
70 }
71 } else {
72 mpp_err("vcodec type %08x can not find H.265 encoder device\n",
73 vcodec_type);
74 ret = MPP_NOK;
75 }
76
77 mpp_assert(api);
78
79 if (!ret)
80 hw_ctx = mpp_calloc_size(void, api->ctx_size);
81
82 ctx->api = api;
83 ctx->hw_ctx = hw_ctx;
84
85 if (ret)
86 return ret;
87
88 ret = api->init(hw_ctx, cfg);
89 return ret;
90 }
91
hal_h265ev2_deinit(void * hal)92 static MPP_RET hal_h265ev2_deinit(void *hal)
93 {
94 HalH265eV2Ctx *ctx = (HalH265eV2Ctx *)hal;
95 const MppEncHalApi *api = ctx->api;
96 void *hw_ctx = ctx->hw_ctx;
97 MPP_RET ret = MPP_OK;
98
99 if (!hw_ctx || !api || !api->deinit)
100 return MPP_OK;
101
102 ret = api->deinit(hw_ctx);
103 MPP_FREE(hw_ctx);
104 return ret;
105 }
106
107 #define HAL_H265E_FUNC(func) \
108 static MPP_RET hal_h265ev2_##func(void *hal) \
109 { \
110 HalH265eV2Ctx *ctx = (HalH265eV2Ctx *)hal; \
111 const MppEncHalApi *api = ctx->api; \
112 void *hw_ctx = ctx->hw_ctx; \
113 \
114 if (!hw_ctx || !api || !api->func) \
115 return MPP_OK; \
116 \
117 return api->func(hw_ctx); \
118 }
119
120 #define HAL_H265E_TASK_FUNC(func) \
121 static MPP_RET hal_h265ev2_##func(void *hal, HalEncTask *task) \
122 { \
123 HalH265eV2Ctx *ctx = (HalH265eV2Ctx *)hal; \
124 const MppEncHalApi *api = ctx->api; \
125 void *hw_ctx = ctx->hw_ctx; \
126 \
127 if (!hw_ctx || !api || !api->func) \
128 return MPP_OK; \
129 \
130 return api->func(hw_ctx, task); \
131 }
132
133 HAL_H265E_FUNC(prepare)
134 HAL_H265E_TASK_FUNC(get_task)
135 HAL_H265E_TASK_FUNC(gen_regs)
136 HAL_H265E_TASK_FUNC(start)
137 HAL_H265E_TASK_FUNC(wait)
138 HAL_H265E_TASK_FUNC(part_start)
139 HAL_H265E_TASK_FUNC(part_wait)
140 HAL_H265E_TASK_FUNC(ret_task)
141
142 const MppEncHalApi hal_api_h265e_v2 = {
143 .name = "hal_h265e",
144 .coding = MPP_VIDEO_CodingHEVC,
145 .ctx_size = sizeof(HalH265eV2Ctx),
146 .flag = 0,
147 .init = hal_h265ev2_init,
148 .deinit = hal_h265ev2_deinit,
149 .prepare = hal_h265ev2_prepare,
150 .get_task = hal_h265ev2_get_task,
151 .gen_regs = hal_h265ev2_gen_regs,
152 .start = hal_h265ev2_start,
153 .wait = hal_h265ev2_wait,
154 .part_start = hal_h265ev2_part_start,
155 .part_wait = hal_h265ev2_part_wait,
156 .ret_task = hal_h265ev2_ret_task,
157 };
158