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 "mpp_enc_hal"
18
19 #include "mpp_mem.h"
20 #include "mpp_log.h"
21 #include "mpp_common.h"
22
23 #include "mpp.h"
24 #include "mpp_enc_hal.h"
25 #include "mpp_frame_impl.h"
26
27 #include "hal_h264e_api_v2.h"
28 #include "hal_h265e_api_v2.h"
29 #include "hal_jpege_api_v2.h"
30 #include "hal_vp8e_api_v2.h"
31
32 static const MppEncHalApi *hw_enc_apis[] = {
33 #if HAVE_H264E
34 &hal_api_h264e_v2,
35 #endif
36 #if HAVE_H265E
37 &hal_api_h265e_v2,
38 #endif
39 #if HAVE_JPEGE
40 &hal_api_jpege_v2,
41 #endif
42 #if HAVE_VP8E
43 &hal_api_vp8e_v2,
44 #endif
45 };
46
47 typedef struct MppEncHalImpl_t {
48 MppCodingType coding;
49
50 void *ctx;
51 const MppEncHalApi *api;
52
53 HalTaskGroup tasks;
54 } MppEncHalImpl;
55
mpp_enc_hal_init(MppEncHal * ctx,MppEncHalCfg * cfg)56 MPP_RET mpp_enc_hal_init(MppEncHal *ctx, MppEncHalCfg *cfg)
57 {
58 if (NULL == ctx || NULL == cfg) {
59 mpp_err_f("found NULL input ctx %p cfg %p\n", ctx, cfg);
60 return MPP_ERR_NULL_PTR;
61 }
62 *ctx = NULL;
63
64 MppEncHalImpl *p = mpp_calloc(MppEncHalImpl, 1);
65 if (NULL == p) {
66 mpp_err_f("malloc failed\n");
67 return MPP_ERR_MALLOC;
68 }
69
70 RK_U32 i;
71 for (i = 0; i < MPP_ARRAY_ELEMS(hw_enc_apis); i++) {
72 if (cfg->coding == hw_enc_apis[i]->coding) {
73 p->coding = cfg->coding;
74 p->api = hw_enc_apis[i];
75 p->ctx = mpp_calloc_size(void, p->api->ctx_size);
76
77 MPP_RET ret = p->api->init(p->ctx, cfg);
78 if (ret) {
79 mpp_err_f("hal %s init failed ret %d\n", hw_enc_apis[i]->name, ret);
80 break;
81 }
82
83 ret = hal_task_group_init(&p->tasks, TASK_BUTT, cfg->task_cnt,
84 sizeof(EncAsyncTaskInfo));
85 if (ret) {
86 mpp_err_f("hal_task_group_init failed ret %d\n", ret);
87 break;
88 }
89
90 cfg->tasks = p->tasks;
91 *ctx = p;
92 return MPP_OK;
93 }
94 }
95
96 mpp_err_f("could not found coding type %d\n", cfg->coding);
97 mpp_free(p->ctx);
98 mpp_free(p);
99
100 return MPP_NOK;
101 }
102
mpp_enc_hal_deinit(MppEncHal ctx)103 MPP_RET mpp_enc_hal_deinit(MppEncHal ctx)
104 {
105 if (NULL == ctx) {
106 mpp_err_f("found NULL input\n");
107 return MPP_ERR_NULL_PTR;
108 }
109
110 MppEncHalImpl *p = (MppEncHalImpl*)ctx;
111 p->api->deinit(p->ctx);
112 mpp_free(p->ctx);
113 if (p->tasks)
114 hal_task_group_deinit(p->tasks);
115 mpp_free(p);
116 return MPP_OK;
117 }
118
mpp_enc_hal_prepare(void * hal)119 MPP_RET mpp_enc_hal_prepare(void *hal)
120 {
121 if (NULL == hal) {
122 mpp_err_f("found NULL input ctx %p\n", hal);
123 return MPP_ERR_NULL_PTR;
124 }
125
126 MppEncHalImpl *p = (MppEncHalImpl*)hal;
127 if (!p->api || !p->api->prepare)
128 return MPP_OK;
129
130 return p->api->prepare(p->ctx);
131 }
132
mpp_enc_hal_check_part_mode(MppEncHal ctx)133 MPP_RET mpp_enc_hal_check_part_mode(MppEncHal ctx)
134 {
135 MppEncHalImpl *p = (MppEncHalImpl*)ctx;
136
137 if (p && p->api && p->api->part_start && p->api->part_wait)
138 return MPP_OK;
139
140 return MPP_NOK;
141 }
142
143 #define MPP_ENC_HAL_TASK_FUNC(func) \
144 MPP_RET mpp_enc_hal_##func(void *hal, HalEncTask *task) \
145 { \
146 if (NULL == hal || NULL == task) { \
147 mpp_err_f("found NULL input ctx %p task %p\n", hal, task); \
148 return MPP_ERR_NULL_PTR; \
149 } \
150 \
151 MppEncHalImpl *p = (MppEncHalImpl*)hal; \
152 if (!p->api || !p->api->func) \
153 return MPP_OK; \
154 \
155 return p->api->func(p->ctx, task); \
156 }
157
158 MPP_ENC_HAL_TASK_FUNC(get_task)
159 MPP_ENC_HAL_TASK_FUNC(gen_regs)
160 MPP_ENC_HAL_TASK_FUNC(start)
161 MPP_ENC_HAL_TASK_FUNC(wait)
162 MPP_ENC_HAL_TASK_FUNC(part_start)
163 MPP_ENC_HAL_TASK_FUNC(part_wait)
164 MPP_ENC_HAL_TASK_FUNC(ret_task)
165