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_h264e_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_platform.h"
26
27 #include "hal_h264e_debug.h"
28 #include "h264e_syntax.h"
29 #include "vepu5xx.h"
30 #include "hal_h264e_api_v2.h"
31 #include "hal_h264e_vepu1_v2.h"
32 #include "hal_h264e_vepu2_v2.h"
33 #include "hal_h264e_vepu541.h"
34 #include "hal_h264e_vepu580.h"
35 #include "hal_h264e_vepu540c.h"
36 #include "hal_h264e_vepu510.h"
37 #include "hal_h264e_vepu511.h"
38
39 typedef struct HalH264eCtx_t {
40 const MppEncHalApi *api;
41 void *hw_ctx;
42 } HalH264eCtx;
43
44 RK_U32 hal_h264e_debug = 0;
45
hal_h264e_init(void * hal,MppEncHalCfg * cfg)46 static MPP_RET hal_h264e_init(void *hal, MppEncHalCfg *cfg)
47 {
48 HalH264eCtx *ctx = (HalH264eCtx *)hal;
49 const MppEncHalApi *api = NULL;
50 void *hw_ctx = NULL;
51 MPP_RET ret = MPP_OK;
52 RK_U32 vcodec_type = mpp_get_vcodec_type();
53
54 mpp_env_get_u32("hal_h264e_debug", &hal_h264e_debug, 0);
55
56 if (vcodec_type & HAVE_RKVENC) {
57 RK_U32 hw_id = mpp_get_client_hw_id(VPU_CLIENT_RKVENC);
58
59 switch (hw_id) {
60 case HWID_VEPU58X : {
61 api = &hal_h264e_vepu580;
62 } break;
63 case HWID_VEPU540C : {
64 api = &hal_h264e_vepu540c;
65 } break;
66 case HWID_VEPU510 : {
67 api = &hal_h264e_vepu510;
68 } break;
69 case HWID_VEPU511 : {
70 api = &hal_h264e_vepu511;
71 } break;
72 default : {
73 api = &hal_h264e_vepu541;
74 } break;
75 }
76 } else if (vcodec_type & HAVE_VEPU2) {
77 api = &hal_h264e_vepu2;
78 } else if (vcodec_type & HAVE_VEPU1) {
79 api = &hal_h264e_vepu1;
80 } else {
81 mpp_err("vcodec type %08x can not find H.264 encoder device\n",
82 vcodec_type);
83 ret = MPP_NOK;
84 }
85
86 mpp_assert(api);
87
88 if (!ret)
89 hw_ctx = mpp_calloc_size(void, api->ctx_size);
90
91 ctx->api = api;
92 ctx->hw_ctx = hw_ctx;
93
94 if (ret)
95 return ret;
96
97 ret = api->init(hw_ctx, cfg);
98 return ret;
99 }
100
hal_h264e_deinit(void * hal)101 static MPP_RET hal_h264e_deinit(void *hal)
102 {
103 HalH264eCtx *ctx = (HalH264eCtx *)hal;
104 const MppEncHalApi *api = ctx->api;
105 void *hw_ctx = ctx->hw_ctx;
106 MPP_RET ret = MPP_OK;
107
108 if (!hw_ctx || !api || !api->deinit)
109 return MPP_OK;
110
111 ret = api->deinit(hw_ctx);
112 MPP_FREE(hw_ctx);
113 return ret;
114 }
115
116 #define HAL_H264E_FUNC(func) \
117 static MPP_RET hal_h264e_##func(void *hal) \
118 { \
119 HalH264eCtx *ctx = (HalH264eCtx *)hal; \
120 const MppEncHalApi *api = ctx->api; \
121 void *hw_ctx = ctx->hw_ctx; \
122 \
123 if (!hw_ctx || !api || !api->func) \
124 return MPP_OK; \
125 \
126 return api->func(hw_ctx); \
127 }
128
129 #define HAL_H264E_TASK_FUNC(func) \
130 static MPP_RET hal_h264e_##func(void *hal, HalEncTask *task) \
131 { \
132 HalH264eCtx *ctx = (HalH264eCtx *)hal; \
133 const MppEncHalApi *api = ctx->api; \
134 void *hw_ctx = ctx->hw_ctx; \
135 \
136 if (!hw_ctx || !api || !api->func) \
137 return MPP_OK; \
138 \
139 return api->func(hw_ctx, task); \
140 }
141
142 HAL_H264E_FUNC(prepare)
143 HAL_H264E_TASK_FUNC(get_task)
144 HAL_H264E_TASK_FUNC(gen_regs)
145 HAL_H264E_TASK_FUNC(start)
146 HAL_H264E_TASK_FUNC(wait)
147 HAL_H264E_TASK_FUNC(part_start)
148 HAL_H264E_TASK_FUNC(part_wait)
149 HAL_H264E_TASK_FUNC(ret_task)
150
151 const MppEncHalApi hal_api_h264e_v2 = {
152 .name = "hal_h264e",
153 .coding = MPP_VIDEO_CodingAVC,
154 .ctx_size = sizeof(HalH264eCtx),
155 .flag = 0,
156 .init = hal_h264e_init,
157 .deinit = hal_h264e_deinit,
158 .prepare = hal_h264e_prepare,
159 .get_task = hal_h264e_get_task,
160 .gen_regs = hal_h264e_gen_regs,
161 .start = hal_h264e_start,
162 .wait = hal_h264e_wait,
163 .part_start = hal_h264e_part_start,
164 .part_wait = hal_h264e_part_wait,
165 .ret_task = hal_h264e_ret_task,
166 };
167