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 "mpp_hal"
18
19 #include "mpp_mem.h"
20 #include "mpp_log.h"
21 #include "mpp_common.h"
22
23 #include "mpp.h"
24 #include "mpp_hal.h"
25 #include "mpp_frame_impl.h"
26
27 #include "hal_h263d_api.h"
28 #include "hal_h264d_api.h"
29 #include "hal_h265d_api.h"
30 #include "hal_vp8d_api.h"
31 #include "hal_vp9d_api.h"
32 #include "hal_avsd_api.h"
33 #include "hal_avs2d_api.h"
34 #include "hal_m2vd_api.h"
35 #include "hal_mpg4d_api.h"
36 #include "hal_jpegd_api.h"
37 #include "hal_av1d_api.h"
38
39 // for test and demo
40 #include "hal_dummy_dec_api.h"
41 #include "hal_dummy_enc_api.h"
42
43 /*
44 * all hardware api static register here
45 */
46 static const MppHalApi *hw_apis[] = {
47 #if HAVE_AVSD
48 &hal_api_avsd,
49 &hal_api_avsd_plus,
50 #endif
51 #if HAVE_AVS2D
52 &hal_api_avs2d,
53 #endif
54 #if HAVE_H263D
55 &hal_api_h263d,
56 #endif
57 #if HAVE_H264D
58 &hal_api_h264d,
59 #endif
60 #if HAVE_H265D
61 &hal_api_h265d,
62 #endif
63 #if HAVE_MPEG2D
64 &hal_api_m2vd,
65 #endif
66 #if HAVE_MPEG4D
67 &hal_api_mpg4d,
68 #endif
69 #if HAVE_VP8D
70 &hal_api_vp8d,
71 #endif
72 #if HAVE_VP9D
73 &hal_api_vp9d,
74 #endif
75 #if HAVE_JPEGD
76 &hal_api_jpegd,
77 #endif
78 #if HAVE_AV1D
79 &hal_api_av1d,
80 #endif
81 &hal_api_dummy_dec,
82 &hal_api_dummy_enc,
83 };
84
85 typedef struct MppHalImpl_t {
86 void *ctx;
87 const MppHalApi *api;
88
89 HalTaskGroup tasks;
90 } MppHalImpl;
91
92
mpp_hal_init(MppHal * ctx,MppHalCfg * cfg)93 MPP_RET mpp_hal_init(MppHal *ctx, MppHalCfg *cfg)
94 {
95 if (NULL == ctx || NULL == cfg) {
96 mpp_err_f("found NULL input ctx %p cfg %p\n", ctx, cfg);
97 return MPP_ERR_NULL_PTR;
98 }
99 *ctx = NULL;
100
101 MppHalImpl *p = mpp_calloc(MppHalImpl, 1);
102 if (NULL == p) {
103 mpp_err_f("malloc failed\n");
104 return MPP_ERR_MALLOC;
105 }
106
107 RK_U32 i;
108 for (i = 0; i < MPP_ARRAY_ELEMS(hw_apis); i++) {
109 if (cfg->type == hw_apis[i]->type &&
110 cfg->coding == hw_apis[i]->coding) {
111 p->api = hw_apis[i];
112 p->ctx = mpp_calloc_size(void, p->api->ctx_size);
113
114 MPP_RET ret = p->api->init(p->ctx, cfg);
115 if (ret) {
116 mpp_err_f("hal %s init failed ret %d\n", hw_apis[i]->name, ret);
117 break;
118 }
119
120 *ctx = p;
121 return MPP_OK;
122 }
123 }
124
125 mpp_err_f("could not found coding type %d\n", cfg->coding);
126 mpp_free(p->ctx);
127 mpp_free(p);
128
129 return MPP_NOK;
130 }
131
mpp_hal_deinit(MppHal ctx)132 MPP_RET mpp_hal_deinit(MppHal ctx)
133 {
134 if (NULL == ctx) {
135 mpp_err_f("found NULL input\n");
136 return MPP_ERR_NULL_PTR;
137 }
138
139 MppHalImpl *p = (MppHalImpl*)ctx;
140 p->api->deinit(p->ctx);
141 mpp_free(p->ctx);
142 mpp_free(p);
143 return MPP_OK;
144 }
145
mpp_hal_reg_gen(MppHal ctx,HalTaskInfo * task)146 MPP_RET mpp_hal_reg_gen(MppHal ctx, HalTaskInfo *task)
147 {
148 if (NULL == ctx || NULL == task) {
149 mpp_err_f("found NULL input ctx %p task %p\n", ctx, task);
150 return MPP_ERR_NULL_PTR;
151 }
152
153 MppHalImpl *p = (MppHalImpl*)ctx;
154 return p->api->reg_gen(p->ctx, task);
155 }
156
mpp_hal_hw_start(MppHal ctx,HalTaskInfo * task)157 MPP_RET mpp_hal_hw_start(MppHal ctx, HalTaskInfo *task)
158 {
159 if (NULL == ctx || NULL == task) {
160 mpp_err_f("found NULL input ctx %p task %p\n", ctx, task);
161 return MPP_ERR_NULL_PTR;
162 }
163
164 MppHalImpl *p = (MppHalImpl*)ctx;
165 return p->api->start(p->ctx, task);
166 }
167
mpp_hal_hw_wait(MppHal ctx,HalTaskInfo * task)168 MPP_RET mpp_hal_hw_wait(MppHal ctx, HalTaskInfo *task)
169 {
170 if (NULL == ctx || NULL == task) {
171 mpp_err_f("found NULL input ctx %p task %p\n", ctx, task);
172 return MPP_ERR_NULL_PTR;
173 }
174
175 MppHalImpl *p = (MppHalImpl*)ctx;
176 return p->api->wait(p->ctx, task);
177 }
178
mpp_hal_reset(MppHal ctx)179 MPP_RET mpp_hal_reset(MppHal ctx)
180 {
181 if (NULL == ctx) {
182 mpp_err_f("found NULL input\n");
183 return MPP_ERR_NULL_PTR;
184 }
185
186 MppHalImpl *p = (MppHalImpl*)ctx;
187
188 if (NULL == p->api || NULL == p->api->reset)
189 return MPP_OK;
190
191 return p->api->reset(p->ctx);
192 }
193
mpp_hal_flush(MppHal ctx)194 MPP_RET mpp_hal_flush(MppHal ctx)
195 {
196 if (NULL == ctx) {
197 mpp_err_f("found NULL input\n");
198 return MPP_ERR_NULL_PTR;
199 }
200
201 MppHalImpl *p = (MppHalImpl*)ctx;
202
203 if (NULL == p->api || NULL == p->api->flush)
204 return MPP_OK;
205
206 return p->api->flush(p->ctx);
207 }
208
mpp_hal_control(MppHal ctx,MpiCmd cmd,void * param)209 MPP_RET mpp_hal_control(MppHal ctx, MpiCmd cmd, void *param)
210 {
211 if (NULL == ctx) {
212 mpp_err_f("found NULL input\n");
213 return MPP_ERR_NULL_PTR;
214 }
215
216 MppHalImpl *p = (MppHalImpl*)ctx;
217
218 if (NULL == p->api || NULL == p->api->control)
219 return MPP_OK;
220
221 return p->api->control(p->ctx, cmd, param);
222 }
223