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