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
34 typedef struct HalH265eV2Ctx_t {
35 const MppEncHalApi *api;
36 void *hw_ctx;
37 } HalH265eV2Ctx;
38
39 RK_U32 hal_h265e_debug = 0;
40
hal_h265ev2_init(void * hal,MppEncHalCfg * cfg)41 static MPP_RET hal_h265ev2_init(void *hal, MppEncHalCfg *cfg)
42 {
43 HalH265eV2Ctx *ctx = (HalH265eV2Ctx *)hal;
44 const MppEncHalApi *api = NULL;
45 void *hw_ctx = NULL;
46 MPP_RET ret = MPP_OK;
47 RK_U32 vcodec_type = mpp_get_vcodec_type();
48
49 if (vcodec_type & HAVE_RKVENC) {
50 RK_U32 hw_id = mpp_get_client_hw_id(VPU_CLIENT_RKVENC);
51
52 switch (hw_id) {
53 case HWID_VEPU58X : {
54 api = &hal_h265e_vepu580;
55 } break;
56 case HWID_VEPU540C : {
57 api = &hal_h265e_vepu540c;
58 } break;
59 default : {
60 api = &hal_h265e_vepu541;
61 } break;
62 }
63 } else {
64 mpp_err("vcodec type %08x can not find H.265 encoder device\n",
65 vcodec_type);
66 ret = MPP_NOK;
67 }
68
69 mpp_assert(api);
70
71 if (!ret)
72 hw_ctx = mpp_calloc_size(void, api->ctx_size);
73
74 ctx->api = api;
75 ctx->hw_ctx = hw_ctx;
76
77 if (ret)
78 return ret;
79
80 ret = api->init(hw_ctx, cfg);
81 return ret;
82 }
83
hal_h265ev2_deinit(void * hal)84 static MPP_RET hal_h265ev2_deinit(void *hal)
85 {
86 HalH265eV2Ctx *ctx = (HalH265eV2Ctx *)hal;
87 const MppEncHalApi *api = ctx->api;
88 void *hw_ctx = ctx->hw_ctx;
89 MPP_RET ret = MPP_OK;
90
91 if (!hw_ctx || !api || !api->deinit)
92 return MPP_OK;
93
94 ret = api->deinit(hw_ctx);
95 MPP_FREE(hw_ctx);
96 return ret;
97 }
98
99 #define HAL_H265E_FUNC(func) \
100 static MPP_RET hal_h265ev2_##func(void *hal) \
101 { \
102 HalH265eV2Ctx *ctx = (HalH265eV2Ctx *)hal; \
103 const MppEncHalApi *api = ctx->api; \
104 void *hw_ctx = ctx->hw_ctx; \
105 \
106 if (!hw_ctx || !api || !api->func) \
107 return MPP_OK; \
108 \
109 return api->func(hw_ctx); \
110 }
111
112 #define HAL_H265E_TASK_FUNC(func) \
113 static MPP_RET hal_h265ev2_##func(void *hal, HalEncTask *task) \
114 { \
115 HalH265eV2Ctx *ctx = (HalH265eV2Ctx *)hal; \
116 const MppEncHalApi *api = ctx->api; \
117 void *hw_ctx = ctx->hw_ctx; \
118 \
119 if (!hw_ctx || !api || !api->func) \
120 return MPP_OK; \
121 \
122 return api->func(hw_ctx, task); \
123 }
124
125 HAL_H265E_FUNC(prepare)
126 HAL_H265E_TASK_FUNC(get_task)
127 HAL_H265E_TASK_FUNC(gen_regs)
128 HAL_H265E_TASK_FUNC(start)
129 HAL_H265E_TASK_FUNC(wait)
130 HAL_H265E_TASK_FUNC(part_start)
131 HAL_H265E_TASK_FUNC(part_wait)
132 HAL_H265E_TASK_FUNC(ret_task)
133
134 const MppEncHalApi hal_api_h265e_v2 = {
135 .name = "hal_h265e",
136 .coding = MPP_VIDEO_CodingHEVC,
137 .ctx_size = sizeof(HalH265eV2Ctx),
138 .flag = 0,
139 .init = hal_h265ev2_init,
140 .deinit = hal_h265ev2_deinit,
141 .prepare = hal_h265ev2_prepare,
142 .get_task = hal_h265ev2_get_task,
143 .gen_regs = hal_h265ev2_gen_regs,
144 .start = hal_h265ev2_start,
145 .wait = hal_h265ev2_wait,
146 .part_start = hal_h265ev2_part_start,
147 .part_wait = hal_h265ev2_part_wait,
148 .ret_task = hal_h265ev2_ret_task,
149 };
150