xref: /OK3568_Linux_fs/external/mpp/mpp/hal/common/jpeg/hal_jpege_api_v2.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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_jpege_api_v2"
18 
19 #include <stdio.h>
20 #include <string.h>
21 #include <unistd.h>
22 
23 #include "mpp_env.h"
24 #include "mpp_mem.h"
25 #include "mpp_platform.h"
26 
27 #include "mpp_enc_hal.h"
28 #include "vepu5xx.h"
29 #include "hal_jpege_debug.h"
30 #include "hal_jpege_api_v2.h"
31 #include "hal_jpege_vepu1_v2.h"
32 #include "hal_jpege_vepu2_v2.h"
33 #include "hal_jpege_vepu540c.h"
34 
35 typedef struct HaljpegeCtx_t {
36     const MppEncHalApi  *api;
37     void                *hw_ctx;
38 } HaljpegeCtx;
39 
40 RK_U32 hal_jpege_debug = 0;
41 
hal_jpege_init(void * hal,MppEncHalCfg * cfg)42 static MPP_RET hal_jpege_init(void *hal, MppEncHalCfg *cfg)
43 {
44     HaljpegeCtx *ctx = (HaljpegeCtx *)hal;
45     const MppEncHalApi *api = NULL;
46     void *hw_ctx = NULL;
47     MPP_RET ret = MPP_OK;
48     RK_U32 vcodec_type = mpp_get_vcodec_type();
49 
50     mpp_env_get_u32("hal_jpege_debug", &hal_jpege_debug, 0);
51 
52     if ((vcodec_type & HAVE_RKVENC) &&
53         (HWID_VEPU540C == mpp_get_client_hw_id(VPU_CLIENT_RKVENC))) {
54         api = &hal_jpege_vepu540c;
55     } else if (vcodec_type & (HAVE_VEPU2 | HAVE_VEPU2_JPEG)) {
56         api = &hal_jpege_vepu2;
57     } else if (vcodec_type & HAVE_VEPU1) {
58         api = &hal_jpege_vepu1;
59     } else {
60         mpp_err("vcodec type %08x can not find JPEG encoder device\n",
61                 vcodec_type);
62         ret = MPP_NOK;
63     }
64 
65     mpp_assert(api);
66 
67     if (!ret)
68         hw_ctx = mpp_calloc_size(void, api->ctx_size);
69 
70     ctx->api = api;
71     ctx->hw_ctx = hw_ctx;
72 
73     if (ret)
74         return ret;
75 
76     ret = api->init(hw_ctx, cfg);
77     return ret;
78 }
79 
hal_jpege_deinit(void * hal)80 static MPP_RET hal_jpege_deinit(void *hal)
81 {
82     HaljpegeCtx *ctx = (HaljpegeCtx *)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_JPEGE_FUNC(func) \
96     static MPP_RET hal_jpege_##func(void *hal)                      \
97     {                                                               \
98         HaljpegeCtx *ctx = (HaljpegeCtx *)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_JPEGE_TASK_FUNC(func) \
109     static MPP_RET hal_jpege_##func(void *hal, HalEncTask *task)    \
110     {                                                               \
111         HaljpegeCtx *ctx = (HaljpegeCtx *)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_JPEGE_FUNC(prepare)
122 HAL_JPEGE_TASK_FUNC(get_task)
123 HAL_JPEGE_TASK_FUNC(gen_regs)
124 HAL_JPEGE_TASK_FUNC(start)
125 HAL_JPEGE_TASK_FUNC(wait)
126 HAL_JPEGE_TASK_FUNC(part_start)
127 HAL_JPEGE_TASK_FUNC(part_wait)
128 HAL_JPEGE_TASK_FUNC(ret_task)
129 
130 const MppEncHalApi hal_api_jpege_v2 = {
131     .name       = "hal_jpege",
132     .coding     = MPP_VIDEO_CodingMJPEG,
133     .ctx_size   = sizeof(HaljpegeCtx),
134     .flag       = 0,
135     .init       = hal_jpege_init,
136     .deinit     = hal_jpege_deinit,
137     .prepare    = hal_jpege_prepare,
138     .get_task   = hal_jpege_get_task,
139     .gen_regs   = hal_jpege_gen_regs,
140     .start      = hal_jpege_start,
141     .wait       = hal_jpege_wait,
142     .part_start = hal_jpege_part_start,
143     .part_wait  = hal_jpege_part_wait,
144     .ret_task   = hal_jpege_ret_task,
145 };
146