xref: /rockchip-linux_mpp/mpp/hal/vpu/vp8e/hal_vp8e_api_v2.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_vp8e_api_v2"
18 
19 #include <string.h>
20 
21 #include "mpp_env.h"
22 #include "mpp_mem.h"
23 #include "mpp_common.h"
24 
25 #include "mpp_enc_hal.h"
26 #include "mpp_platform.h"
27 
28 //#include "hal_vp8e_base.h"
29 #include "hal_vp8e_vepu2_v2.h"
30 #include "hal_vp8e_vepu1_v2.h"
31 #include "hal_vp8e_debug.h"
32 #include "hal_vp8e_api_v2.h"
33 
34 typedef struct Halvp8eCtx_t {
35     const MppEncHalApi  *api;
36     void                *hw_ctx;
37 } Halvp8eCtx;
38 
39 RK_U32 vp8e_hal_debug = 0;
40 
hal_vp8e_init(void * hal,MppEncHalCfg * cfg)41 static MPP_RET hal_vp8e_init(void *hal, MppEncHalCfg *cfg)
42 {
43     const MppEncHalApi  *p_api = NULL;
44     Halvp8eCtx *ctx = (Halvp8eCtx *)hal;
45     MppClientType type = VPU_CLIENT_BUTT;
46     void* hw_ctx = NULL;
47 
48     memset(ctx, 0, sizeof(Halvp8eCtx));
49 
50     mpp_env_get_u32("vp8e_hal_debug", &vp8e_hal_debug, 0);
51 
52     {
53         RK_U32 hw_flag = mpp_get_vcodec_type();
54         if (hw_flag & HAVE_VEPU2) {
55             p_api = &hal_vp8e_vepu2;
56             type = VPU_CLIENT_VEPU2;
57         } else if (hw_flag & HAVE_VEPU1) {
58             p_api = &hal_vp8e_vepu1;
59             type = VPU_CLIENT_VEPU1;
60         } else {
61             mpp_err_f("Failed to init due to unsupported hard mode, hw_flag = %d\n", hw_flag);
62             return MPP_ERR_INIT;
63         }
64     }
65 
66     mpp_assert(p_api);
67     mpp_assert(type != VPU_CLIENT_BUTT);
68 
69     hw_ctx = mpp_calloc_size(void, p_api->ctx_size);
70     if (NULL == hw_ctx)
71         return MPP_ERR_MALLOC;
72 
73     ctx->hw_ctx = hw_ctx;
74     ctx->api = p_api;
75     cfg->type = type;
76 
77     return p_api->init(hw_ctx, cfg);
78 }
79 
hal_vp8e_deinit(void * hal)80 static MPP_RET hal_vp8e_deinit(void *hal)
81 {
82     Halvp8eCtx *ctx = (Halvp8eCtx *)hal;
83     const MppEncHalApi *api = ctx->api;
84     void *hw_ctx = ctx->hw_ctx;
85     MPP_RET ret = MPP_OK;
86 
87     if (!hw_ctx || !api || !api->deinit)
88         return MPP_OK;
89 
90     ret = api->deinit(hw_ctx);
91     MPP_FREE(hw_ctx);
92     return ret;
93 }
94 
95 #define HAL_VP8E_FUNC(func) \
96     static MPP_RET hal_vp8e_##func(void *hal)                       \
97     {                                                               \
98         Halvp8eCtx *ctx = (Halvp8eCtx *)hal;                        \
99         const MppEncHalApi *api = ctx->api;                         \
100         void *hw_ctx = ctx->hw_ctx;                                 \
101                                                                     \
102         if (!hw_ctx || !api || !api->func)                          \
103             return MPP_OK;                                          \
104                                                                     \
105         return api->func(hw_ctx);                                   \
106     }
107 
108 #define HAL_VP8E_TASK_FUNC(func) \
109     static MPP_RET hal_vp8e_##func(void *hal, HalEncTask *task)     \
110     {                                                               \
111         Halvp8eCtx *ctx = (Halvp8eCtx *)hal;                        \
112         const MppEncHalApi *api = ctx->api;                         \
113         void *hw_ctx = ctx->hw_ctx;                                 \
114                                                                     \
115         if (!hw_ctx || !api || !api->func)                          \
116             return MPP_OK;                                          \
117                                                                     \
118         return api->func(hw_ctx, task);                             \
119     }
120 
121 HAL_VP8E_FUNC(prepare)
122 HAL_VP8E_TASK_FUNC(get_task)
123 HAL_VP8E_TASK_FUNC(gen_regs)
124 HAL_VP8E_TASK_FUNC(start)
125 HAL_VP8E_TASK_FUNC(wait)
126 HAL_VP8E_TASK_FUNC(ret_task)
127 
128 const MppEncHalApi hal_api_vp8e_v2 = {
129     .name       = "hal_vp8e",
130     .coding     = MPP_VIDEO_CodingVP8,
131     .ctx_size   = sizeof(Halvp8eCtx),
132     .flag       = 0,
133     .init       = hal_vp8e_init,
134     .deinit     = hal_vp8e_deinit,
135     .prepare    = hal_vp8e_prepare,
136     .get_task   = hal_vp8e_get_task,
137     .gen_regs   = hal_vp8e_gen_regs,
138     .start      = hal_vp8e_start,
139     .wait       = hal_vp8e_wait,
140     .part_start = NULL,
141     .part_wait  = NULL,
142     .ret_task   = hal_vp8e_ret_task,
143 };
144