xref: /rockchip-linux_mpp/mpp/hal/rkdec/avsd/hal_avsd_api.c (revision 437bfbeb9567cca9cd9080e3f6954aa9d6a94f18)
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_avsd_api"
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 
23 #include "rk_mpi_cmd.h"
24 #include "mpp_debug.h"
25 #include "mpp_mem.h"
26 #include "mpp_env.h"
27 #include "mpp_common.h"
28 #include "mpp_platform.h"
29 #include "mpp_device.h"
30 
31 #include "mpp_dec_cb_param.h"
32 #include "hal_avsd_api.h"
33 #include "hal_avsd_base.h"
34 #include "hal_avsd_vdpu1.h"
35 #include "hal_avsd_vdpu2.h"
36 #include "hal_avsd_plus.h"
37 
init_hard_platform(AvsdHalCtx_t * p_hal,MppCodingType coding)38 static MPP_RET init_hard_platform(AvsdHalCtx_t *p_hal, MppCodingType coding)
39 {
40     MppHalApi *p_api = &p_hal->hal_api;
41     RK_U32 vcodec_type = mpp_get_vcodec_type();
42     MppClientType client_type = VPU_CLIENT_BUTT;
43     MPP_RET ret = MPP_ERR_UNKNOW;
44 
45     if (coding == MPP_VIDEO_CodingAVSPLUS) {
46         if (!(vcodec_type & HAVE_AVSDEC))
47             mpp_err("coding %x vcodec_type %x do not found avs hw %x\n",
48                     coding, vcodec_type, HAVE_AVSDEC);
49     } else {
50         RK_U32 hw_flag = HAVE_VDPU1 | HAVE_VDPU2 | HAVE_VDPU1_PP | HAVE_VDPU2_PP;
51 
52         if (!(vcodec_type & hw_flag ))
53             mpp_err("coding %x vcodec_type %x do not found avs hw %x\n",
54                     coding, vcodec_type, hw_flag);
55     }
56 
57     if ((coding == MPP_VIDEO_CodingAVSPLUS) &&
58         (vcodec_type & HAVE_AVSDEC)) {
59         p_api->init    = hal_avsd_plus_init;
60         p_api->deinit  = hal_avsd_plus_deinit;
61         p_api->reg_gen = hal_avsd_plus_gen_regs;
62         p_api->start   = hal_avsd_plus_start;
63         p_api->wait    = hal_avsd_plus_wait;
64         p_api->reset   = hal_avsd_plus_reset;
65         p_api->flush   = hal_avsd_plus_flush;
66         p_api->control = hal_avsd_plus_control;
67         client_type    = VPU_CLIENT_AVSPLUS_DEC;
68     } else if ((coding == MPP_VIDEO_CodingAVS) &&
69                (vcodec_type & (HAVE_VDPU1 | HAVE_VDPU1_PP))) {
70         p_api->init    = hal_avsd_vdpu1_init;
71         p_api->deinit  = hal_avsd_vdpu1_deinit;
72         p_api->reg_gen = hal_avsd_vdpu1_gen_regs;
73         p_api->start   = hal_avsd_vdpu1_start;
74         p_api->wait    = hal_avsd_vdpu1_wait;
75         p_api->reset   = hal_avsd_vdpu1_reset;
76         p_api->flush   = hal_avsd_vdpu1_flush;
77         p_api->control = hal_avsd_vdpu1_control;
78         client_type    = VPU_CLIENT_VDPU1;
79     } else if ((coding == MPP_VIDEO_CodingAVS) &&
80                (vcodec_type & (HAVE_VDPU2 | HAVE_VDPU2_PP))) {
81         p_api->init    = hal_avsd_vdpu2_init;
82         p_api->deinit  = hal_avsd_vdpu2_deinit;
83         p_api->reg_gen = hal_avsd_vdpu2_gen_regs;
84         p_api->start   = hal_avsd_vdpu2_start;
85         p_api->wait    = hal_avsd_vdpu2_wait;
86         p_api->reset   = hal_avsd_vdpu2_reset;
87         p_api->flush   = hal_avsd_vdpu2_flush;
88         p_api->control = hal_avsd_vdpu2_control;
89         client_type    = VPU_CLIENT_VDPU2;
90     } else {
91         ret = MPP_NOK;
92         goto __FAILED;
93     }
94     p_hal->coding = coding;
95     AVSD_HAL_DBG(AVSD_DBG_HARD_MODE, "hw_spt %08x, coding %d\n", vcodec_type, coding);
96 
97     ret = mpp_dev_init(&p_hal->dev, client_type);
98     if (ret) {
99         mpp_err("mpp_device_init failed. ret: %d\n", ret);
100         return ret;
101 
102     }
103     return ret = MPP_OK;
104 __FAILED:
105     return ret;
106 }
107 
108 /*!
109  ***********************************************************************
110  * \brief
111  *    deinit
112  ***********************************************************************
113  */
114 //extern "C"
hal_avsd_deinit(void * decoder)115 MPP_RET hal_avsd_deinit(void *decoder)
116 {
117     MPP_RET ret = MPP_ERR_UNKNOW;
118     AvsdHalCtx_t *p_hal = (AvsdHalCtx_t *)decoder;
119 
120     AVSD_HAL_TRACE("In.");
121     INP_CHECK(ret, NULL == decoder);
122 
123     FUN_CHECK(ret = p_hal->hal_api.deinit(decoder));
124     //!< mpp_dev_init
125     if (p_hal->dev) {
126         ret = mpp_dev_deinit(p_hal->dev);
127         if (ret)
128             mpp_err("mpp_dev_deinit failed. ret: %d\n", ret);
129     }
130 
131     if (p_hal->buf_group) {
132         FUN_CHECK(ret = mpp_buffer_group_put(p_hal->buf_group));
133     }
134 __RETURN:
135     AVSD_HAL_TRACE("Out.");
136 
137     return ret = MPP_OK;
138 __FAILED:
139     return ret;
140 }
141 
142 /*!
143  ***********************************************************************
144  * \brief
145  *    init
146  ***********************************************************************
147  */
148 //extern "C"
hal_avsd_init(void * decoder,MppHalCfg * cfg)149 MPP_RET hal_avsd_init(void *decoder, MppHalCfg *cfg)
150 {
151     MPP_RET ret = MPP_ERR_UNKNOW;
152     AvsdHalCtx_t *p_hal = (AvsdHalCtx_t *)decoder;
153 
154     AVSD_HAL_TRACE("In.");
155     INP_CHECK(ret, NULL == decoder);
156 
157     memset(p_hal, 0, sizeof(AvsdHalCtx_t));
158     p_hal->frame_slots = cfg->frame_slots;
159     p_hal->packet_slots = cfg->packet_slots;
160 
161     //!< callback function to parser module
162     p_hal->dec_cb = cfg->dec_cb;
163     mpp_env_get_u32("avsd_debug", &avsd_hal_debug, 0);
164     //< get buffer group
165     FUN_CHECK(ret = mpp_buffer_group_get_internal(&p_hal->buf_group, MPP_BUFFER_TYPE_ION));
166 
167     FUN_CHECK(ret = init_hard_platform(p_hal, cfg->coding));
168     cfg->dev = p_hal->dev;
169     p_hal->dec_cfg = cfg->cfg;
170 
171     //!< run init funtion
172     FUN_CHECK(ret = p_hal->hal_api.init(decoder, cfg));
173 
174 __RETURN:
175     AVSD_HAL_TRACE("Out.");
176 
177     return ret = MPP_OK;
178 __FAILED:
179     hal_avsd_deinit(decoder);
180     return ret;
181 }
182 
183 /*!
184  ***********************************************************************
185  * \brief
186  *    generate register
187  ***********************************************************************
188  */
189 //extern "C"
hal_avsd_gen_regs(void * decoder,HalTaskInfo * task)190 MPP_RET hal_avsd_gen_regs(void *decoder, HalTaskInfo *task)
191 {
192     MPP_RET ret = MPP_ERR_UNKNOW;
193     MppCodingType coding = MPP_VIDEO_CodingUnused;
194     AvsdHalCtx_t *p_hal = (AvsdHalCtx_t *)decoder;
195 
196     memcpy(&p_hal->syn, task->dec.syntax.data, sizeof(AvsdSyntax_t));
197     // check coding
198     coding = (p_hal->syn.pp.profileId == 0x48) ? MPP_VIDEO_CodingAVSPLUS : p_hal->coding;
199     if (coding != p_hal->coding) {
200         if (p_hal->dev) {
201             ret = mpp_dev_deinit(p_hal->dev);
202             if (ret)
203                 mpp_err("mpp_dev_deinit failed. ret: %d\n", ret);
204 
205             p_hal->dev = NULL;
206         }
207 
208         ret = p_hal->hal_api.deinit(decoder);
209         if (ret) {
210             mpp_err_f("deinit decoder failed, ret %d\n", ret);
211             return ret;
212         }
213 
214         ret = init_hard_platform(p_hal, coding);
215         if (ret) {
216             mpp_err_f("change paltform %x -> %x error\n", p_hal->coding, coding);
217             return ret;
218         }
219 
220         ret = p_hal->hal_api.init(decoder, p_hal->cfg);
221         if (ret) {
222             mpp_err_f("init decoder failed, ret %d\n", ret);
223             return ret;
224         }
225     }
226 
227     p_hal->frame_no++;
228 
229     return p_hal->hal_api.reg_gen(decoder, task);
230 }
231 /*!
232  ***********************************************************************
233  * \brief h
234  *    start hard
235  ***********************************************************************
236  */
237 //extern "C"
hal_avsd_start(void * decoder,HalTaskInfo * task)238 MPP_RET hal_avsd_start(void *decoder, HalTaskInfo *task)
239 {
240     AvsdHalCtx_t *p_hal = (AvsdHalCtx_t *)decoder;
241 
242     return p_hal->hal_api.start(decoder, task);
243 }
244 /*!
245  ***********************************************************************
246  * \brief
247  *    wait hard
248  ***********************************************************************
249  */
250 //extern "C"
hal_avsd_wait(void * decoder,HalTaskInfo * task)251 MPP_RET hal_avsd_wait(void *decoder, HalTaskInfo *task)
252 {
253     AvsdHalCtx_t *p_hal = (AvsdHalCtx_t *)decoder;
254 
255     return p_hal->hal_api.wait(decoder, task);
256 }
257 /*!
258  ***********************************************************************
259  * \brief
260  *    reset
261  ***********************************************************************
262  */
263 //extern "C"
hal_avsd_reset(void * decoder)264 MPP_RET hal_avsd_reset(void *decoder)
265 {
266     AvsdHalCtx_t *p_hal = (AvsdHalCtx_t *)decoder;
267 
268     return p_hal->hal_api.reset(p_hal);
269 }
270 /*!
271  ***********************************************************************
272  * \brief
273  *    flush
274  ***********************************************************************
275  */
276 //extern "C"
hal_avsd_flush(void * decoder)277 MPP_RET hal_avsd_flush(void *decoder)
278 {
279     AvsdHalCtx_t *p_hal = (AvsdHalCtx_t *)decoder;
280 
281     return p_hal->hal_api.flush(p_hal);
282 }
283 /*!
284  ***********************************************************************
285  * \brief
286  *    control
287  ***********************************************************************
288  */
289 //extern "C"
hal_avsd_control(void * decoder,MpiCmd cmd_type,void * param)290 MPP_RET hal_avsd_control(void *decoder, MpiCmd cmd_type, void *param)
291 {
292     AvsdHalCtx_t *p_hal = (AvsdHalCtx_t *)decoder;
293 
294     return p_hal->hal_api.control(decoder, cmd_type, param);
295 }
296 
297 const MppHalApi hal_api_avsd = {
298     .name = "avsd_vdpu",
299     .type = MPP_CTX_DEC,
300     .coding = MPP_VIDEO_CodingAVS,
301     .ctx_size = sizeof(AvsdHalCtx_t),
302     .flag = 0,
303     .init = hal_avsd_init,
304     .deinit = hal_avsd_deinit,
305     .reg_gen = hal_avsd_gen_regs,
306     .start = hal_avsd_start,
307     .wait = hal_avsd_wait,
308     .reset = hal_avsd_reset,
309     .flush = hal_avsd_flush,
310     .control = hal_avsd_control,
311 };
312 
313 const MppHalApi hal_api_avsd_plus = {
314     .name = "avsd_plus",
315     .type = MPP_CTX_DEC,
316     .coding = MPP_VIDEO_CodingAVSPLUS,
317     .ctx_size = sizeof(AvsdHalCtx_t),
318     .flag = 0,
319     .init = hal_avsd_init,
320     .deinit = hal_avsd_deinit,
321     .reg_gen = hal_avsd_gen_regs,
322     .start =  hal_avsd_start,
323     .wait = hal_avsd_wait,
324     .reset = hal_avsd_reset,
325     .flush = hal_avsd_flush,
326     .control = hal_avsd_control,
327 };
328