1 /*
2 * Copyright 2021 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_avs2d_api"
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include "rk_type.h"
24 #include "mpp_device.h"
25 #include "mpp_platform.h"
26 #include "mpp_log.h"
27 #include "mpp_err.h"
28 #include "mpp_mem.h"
29 #include "mpp_env.h"
30 #include "mpp_common.h"
31
32 #include "hal_avs2d_api.h"
33 #include "hal_avs2d_rkv.h"
34 #include "hal_avs2d_vdpu382.h"
35 #include "hal_avs2d_vdpu383.h"
36
37 RK_U32 avs2d_hal_debug = 0;
38
explain_input_buffer(Avs2dHalCtx_t * p_hal,HalDecTask * task)39 static void explain_input_buffer(Avs2dHalCtx_t *p_hal, HalDecTask *task)
40 {
41 memcpy(&p_hal->syntax, task->syntax.data, sizeof(Avs2dSyntax_t));
42 }
43
hal_avs2d_deinit(void * hal)44 MPP_RET hal_avs2d_deinit(void *hal)
45 {
46 MPP_RET ret = MPP_OK;
47 Avs2dHalCtx_t *p_hal = (Avs2dHalCtx_t *)hal;
48
49 AVS2D_HAL_TRACE("In.");
50 INP_CHECK(ret, NULL == hal);
51
52 FUN_CHECK(ret = p_hal->hal_api->deinit(hal));
53
54 if (p_hal->buf_group) {
55 FUN_CHECK(ret = mpp_buffer_group_put(p_hal->buf_group));
56 }
57
58 //!< mpp_device_init
59 if (p_hal->dev) {
60 ret = mpp_dev_deinit(p_hal->dev);
61 if (ret)
62 mpp_err("mpp_device_deinit failed. ret: %d\n", ret);
63 }
64
65 __RETURN:
66 AVS2D_HAL_TRACE("Out.");
67 return ret;
68 __FAILED:
69 return ret;
70 }
71
hal_avs2d_init(void * hal,MppHalCfg * cfg)72 MPP_RET hal_avs2d_init(void *hal, MppHalCfg *cfg)
73 {
74 MPP_RET ret = MPP_OK;
75 Avs2dHalCtx_t *p_hal = NULL;
76 const MppHalApi *api;
77
78 AVS2D_HAL_TRACE("In.");
79 INP_CHECK(ret, NULL == hal);
80
81 mpp_env_get_u32("avs2d_debug", &avs2d_hal_debug, 0);
82
83 p_hal = (Avs2dHalCtx_t *)hal;
84 memset(p_hal, 0, sizeof(Avs2dHalCtx_t));
85
86
87 RK_U32 hw_id = mpp_get_client_hw_id(VPU_CLIENT_RKVDEC);
88
89 switch (hw_id) {
90 case HWID_VDPU383 : {
91 api = &hal_avs2d_vdpu383;
92 } break;
93 case HWID_VDPU382_RK3528 : {
94 api = &hal_avs2d_vdpu382;
95 } break;
96 default : {
97 api = &hal_avs2d_rkvdpu;
98 } break;
99 }
100
101 ret = mpp_dev_init(&cfg->dev, VPU_CLIENT_RKVDEC);
102 if (ret) {
103 mpp_err("mpp_dev_init failed. ret: %d\n", ret);
104 return ret;
105 }
106 cfg->hw_info = mpp_get_dec_hw_info_by_client_type(VPU_CLIENT_RKVDEC);
107 p_hal->hw_info = cfg->hw_info;
108 cfg->support_fast_mode = 1;
109
110 p_hal->cfg = cfg->cfg;
111 p_hal->dev = cfg->dev;
112 p_hal->dec_cb = cfg->dec_cb;
113 p_hal->frame_slots = cfg->frame_slots;
114 p_hal->packet_slots = cfg->packet_slots;
115 p_hal->fast_mode = cfg->cfg->base.fast_parse && cfg->support_fast_mode;
116
117 //< get buffer group
118 if (p_hal->buf_group == NULL)
119 FUN_CHECK(ret = mpp_buffer_group_get_internal(&p_hal->buf_group, MPP_BUFFER_TYPE_ION));
120
121 //!< run init funtion
122 FUN_CHECK(ret = api->init(hal, cfg));
123
124 p_hal->hal_api = api;
125
126 __RETURN:
127 AVS2D_HAL_TRACE("Out.");
128 return ret;
129 __FAILED:
130 hal_avs2d_deinit(hal);
131 return ret;
132 }
133
hal_avs2d_gen_regs(void * hal,HalTaskInfo * task)134 MPP_RET hal_avs2d_gen_regs(void *hal, HalTaskInfo *task)
135 {
136 Avs2dHalCtx_t *p_hal = (Avs2dHalCtx_t *)hal;
137
138 explain_input_buffer(hal, &task->dec);
139 if (!p_hal || !p_hal->hal_api || !p_hal->hal_api->reg_gen)
140 return MPP_NOK;
141
142 return p_hal->hal_api->reg_gen(hal, task);
143 }
144
145 #define HAL_AVS2D_TASK_FUNC(func) \
146 static MPP_RET hal_avs2d_##func(void *hal, HalTaskInfo *task) \
147 { \
148 Avs2dHalCtx_t *p_hal = (Avs2dHalCtx_t *)hal; \
149 \
150 if (!p_hal || !p_hal->hal_api || !p_hal->hal_api->func) \
151 return MPP_OK; \
152 \
153 return p_hal->hal_api->func(hal, task); \
154 }
155
156 HAL_AVS2D_TASK_FUNC(start);
157 HAL_AVS2D_TASK_FUNC(wait);
158
159 const MppHalApi hal_api_avs2d = {
160 .name = "avs2d_rkdec",
161 .type = MPP_CTX_DEC,
162 .coding = MPP_VIDEO_CodingAVS2,
163 .ctx_size = sizeof(Avs2dHalCtx_t),
164 .flag = 0,
165 .init = hal_avs2d_init,
166 .deinit = hal_avs2d_deinit,
167 .reg_gen = hal_avs2d_gen_regs,
168 .start = hal_avs2d_start,
169 .wait = hal_avs2d_wait,
170 .reset = NULL,
171 .flush = NULL,
172 .control = NULL,
173 };
174