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