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
36 RK_U32 avs2d_hal_debug = 0;
37
explain_input_buffer(Avs2dHalCtx_t * p_hal,HalDecTask * task)38 static void explain_input_buffer(Avs2dHalCtx_t *p_hal, HalDecTask *task)
39 {
40 memcpy(&p_hal->syntax, task->syntax.data, sizeof(Avs2dSyntax_t));
41 }
42
hal_avs2d_deinit(void * hal)43 MPP_RET hal_avs2d_deinit(void *hal)
44 {
45 MPP_RET ret = MPP_OK;
46 Avs2dHalCtx_t *p_hal = (Avs2dHalCtx_t *)hal;
47
48 AVS2D_HAL_TRACE("In.");
49 INP_CHECK(ret, NULL == hal);
50
51 FUN_CHECK(ret = p_hal->hal_api.deinit(hal));
52
53 if (p_hal->buf_group) {
54 FUN_CHECK(ret = mpp_buffer_group_put(p_hal->buf_group));
55 }
56
57 //!< mpp_device_init
58 if (p_hal->dev) {
59 ret = mpp_dev_deinit(p_hal->dev);
60 if (ret)
61 mpp_err("mpp_device_deinit failed. ret: %d\n", ret);
62 }
63
64 __RETURN:
65 AVS2D_HAL_TRACE("Out.");
66 return ret;
67 __FAILED:
68 return ret;
69 }
70
hal_avs2d_init(void * hal,MppHalCfg * cfg)71 MPP_RET hal_avs2d_init(void *hal, MppHalCfg *cfg)
72 {
73 MPP_RET ret = MPP_OK;
74 Avs2dHalCtx_t *p_hal = NULL;
75 MppHalApi *p_api = NULL;
76
77 AVS2D_HAL_TRACE("In.");
78 INP_CHECK(ret, NULL == hal);
79
80 mpp_env_get_u32("avs2d_debug", &avs2d_hal_debug, 0);
81
82 p_hal = (Avs2dHalCtx_t *)hal;
83 memset(p_hal, 0, sizeof(Avs2dHalCtx_t));
84
85
86 RK_U32 hw_id = mpp_get_client_hw_id(VPU_CLIENT_RKVDEC);
87
88 p_api = &p_hal->hal_api;
89 if (hw_id == HWID_VDPU382_RK3528) {
90 p_api->init = hal_avs2d_vdpu382_init;
91 p_api->deinit = hal_avs2d_vdpu382_deinit;
92 p_api->reg_gen = hal_avs2d_vdpu382_gen_regs;
93 p_api->start = hal_avs2d_vdpu382_start;
94 p_api->wait = hal_avs2d_vdpu382_wait;
95 } else {
96 p_api->init = hal_avs2d_rkv_init;
97 p_api->deinit = hal_avs2d_rkv_deinit;
98 p_api->reg_gen = hal_avs2d_rkv_gen_regs;
99 p_api->start = hal_avs2d_rkv_start;
100 p_api->wait = hal_avs2d_rkv_wait;
101 }
102
103 p_api->reset = NULL;
104 p_api->flush = NULL;
105 p_api->control = NULL;
106
107 ret = mpp_dev_init(&cfg->dev, VPU_CLIENT_RKVDEC);
108 if (ret) {
109 mpp_err("mpp_dev_init failed. ret: %d\n", ret);
110 return ret;
111 }
112
113 p_hal->cfg = cfg->cfg;
114 p_hal->dev = cfg->dev;
115 p_hal->dec_cb = cfg->dec_cb;
116 p_hal->frame_slots = cfg->frame_slots;
117 p_hal->packet_slots = cfg->packet_slots;
118 p_hal->fast_mode = cfg->cfg->base.fast_parse;
119
120 //< get buffer group
121 if (p_hal->buf_group == NULL)
122 FUN_CHECK(ret = mpp_buffer_group_get_internal(&p_hal->buf_group, MPP_BUFFER_TYPE_ION));
123
124 //!< run init funtion
125 FUN_CHECK(ret = p_api->init(hal, cfg));
126
127 __RETURN:
128 AVS2D_HAL_TRACE("Out.");
129 return ret;
130 __FAILED:
131 hal_avs2d_deinit(hal);
132 return ret;
133 }
134
hal_avs2d_gen_regs(void * hal,HalTaskInfo * task)135 MPP_RET hal_avs2d_gen_regs(void *hal, HalTaskInfo *task)
136 {
137 Avs2dHalCtx_t *p_hal = (Avs2dHalCtx_t *)hal;
138
139 explain_input_buffer(hal, &task->dec);
140 return p_hal->hal_api.reg_gen(hal, task);
141 }
142
hal_avs2d_start(void * hal,HalTaskInfo * task)143 MPP_RET hal_avs2d_start(void *hal, HalTaskInfo *task)
144 {
145 Avs2dHalCtx_t *p_hal = (Avs2dHalCtx_t *)hal;
146
147 return p_hal->hal_api.start(hal, task);
148
149 }
150
hal_avs2d_wait(void * hal,HalTaskInfo * task)151 MPP_RET hal_avs2d_wait(void *hal, HalTaskInfo *task)
152 {
153 Avs2dHalCtx_t *p_hal = (Avs2dHalCtx_t *)hal;
154
155 return p_hal->hal_api.wait(hal, task);
156 }
157
158 const MppHalApi hal_api_avs2d = {
159 .name = "avs2d_rkdec",
160 .type = MPP_CTX_DEC,
161 .coding = MPP_VIDEO_CodingAVS2,
162 .ctx_size = sizeof(Avs2dHalCtx_t),
163 .flag = 0,
164 .init = hal_avs2d_init,
165 .deinit = hal_avs2d_deinit,
166 .reg_gen = hal_avs2d_gen_regs,
167 .start = hal_avs2d_start,
168 .wait = hal_avs2d_wait,
169 .reset = NULL,
170 .flush = NULL,
171 .control = NULL,
172 };
173