xref: /rockchip-linux_mpp/mpp/hal/common/jpeg/hal_jpege_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_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 #include "hal_jpege_vpu720.h"
35 #include "hal_jpege_vepu511.h"
36 
37 typedef struct HaljpegeCtx_t {
38     const MppEncHalApi  *api;
39     void                *hw_ctx;
40 } HaljpegeCtx;
41 
42 RK_U32 hal_jpege_debug = 0;
43 
hal_jpege_init(void * hal,MppEncHalCfg * cfg)44 static MPP_RET hal_jpege_init(void *hal, MppEncHalCfg *cfg)
45 {
46     HaljpegeCtx *ctx = (HaljpegeCtx *)hal;
47     const MppEncHalApi *api = NULL;
48     void *hw_ctx = NULL;
49     MPP_RET ret = MPP_OK;
50     RK_U32 vcodec_type = mpp_get_vcodec_type();
51 
52     mpp_env_get_u32("hal_jpege_debug", &hal_jpege_debug, 0);
53 
54     if (vcodec_type & (HAVE_VEPU2 | HAVE_VEPU2_JPEG)) {
55         api = &hal_jpege_vepu2;
56     } else if (vcodec_type & HAVE_VEPU1) {
57         api = &hal_jpege_vepu1;
58     } else if (vcodec_type & HAVE_JPEG_ENC) {
59         api = &hal_jpege_vpu720;
60     } else if (vcodec_type & HAVE_RKVENC) {
61         RK_U32 hw_id = mpp_get_client_hw_id(VPU_CLIENT_RKVENC);
62 
63         if (hw_id == HWID_VEPU540C)
64             api = &hal_jpege_vepu540c;
65         else if (hw_id == HWID_VEPU511)
66             api = &hal_jpege_vepu511;
67     }
68 
69     if (!api) {
70         mpp_err("vcodec type %08x can not find JPEG encoder device\n", vcodec_type);
71         return MPP_NOK;
72     }
73 
74     hw_ctx = mpp_calloc_size(void, api->ctx_size);
75     ctx->api = api;
76     ctx->hw_ctx = hw_ctx;
77 
78     ret = api->init(hw_ctx, cfg);
79     return ret;
80 }
81 
hal_jpege_deinit(void * hal)82 static MPP_RET hal_jpege_deinit(void *hal)
83 {
84     HaljpegeCtx *ctx = (HaljpegeCtx *)hal;
85     const MppEncHalApi *api = ctx->api;
86     void *hw_ctx = ctx->hw_ctx;
87     MPP_RET ret = MPP_OK;
88 
89     if (!hw_ctx || !api || !api->deinit)
90         return MPP_OK;
91 
92     ret = api->deinit(hw_ctx);
93     MPP_FREE(hw_ctx);
94     return ret;
95 }
96 
97 #define HAL_JPEGE_FUNC(func) \
98     static MPP_RET hal_jpege_##func(void *hal)                      \
99     {                                                               \
100         HaljpegeCtx *ctx = (HaljpegeCtx *)hal;                      \
101         const MppEncHalApi *api = ctx->api;                         \
102         void *hw_ctx = ctx->hw_ctx;                                 \
103                                                                     \
104         if (!hw_ctx || !api || !api->func)                          \
105             return MPP_OK;                                          \
106                                                                     \
107         return api->func(hw_ctx);                                   \
108     }
109 
110 #define HAL_JPEGE_TASK_FUNC(func) \
111     static MPP_RET hal_jpege_##func(void *hal, HalEncTask *task)    \
112     {                                                               \
113         HaljpegeCtx *ctx = (HaljpegeCtx *)hal;                      \
114         const MppEncHalApi *api = ctx->api;                         \
115         void *hw_ctx = ctx->hw_ctx;                                 \
116                                                                     \
117         if (!hw_ctx || !api || !api->func)                          \
118             return MPP_OK;                                          \
119                                                                     \
120         return api->func(hw_ctx, task);                             \
121     }
122 
123 HAL_JPEGE_FUNC(prepare)
124 HAL_JPEGE_TASK_FUNC(get_task)
125 HAL_JPEGE_TASK_FUNC(gen_regs)
126 HAL_JPEGE_TASK_FUNC(start)
127 HAL_JPEGE_TASK_FUNC(wait)
128 HAL_JPEGE_TASK_FUNC(part_start)
129 HAL_JPEGE_TASK_FUNC(part_wait)
130 HAL_JPEGE_TASK_FUNC(ret_task)
131 
132 const MppEncHalApi hal_api_jpege_v2 = {
133     .name       = "hal_jpege",
134     .coding     = MPP_VIDEO_CodingMJPEG,
135     .ctx_size   = sizeof(HaljpegeCtx),
136     .flag       = 0,
137     .init       = hal_jpege_init,
138     .deinit     = hal_jpege_deinit,
139     .prepare    = hal_jpege_prepare,
140     .get_task   = hal_jpege_get_task,
141     .gen_regs   = hal_jpege_gen_regs,
142     .start      = hal_jpege_start,
143     .wait       = hal_jpege_wait,
144     .part_start = hal_jpege_part_start,
145     .part_wait  = hal_jpege_part_wait,
146     .ret_task   = hal_jpege_ret_task,
147 };
148