hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * Copyright 2015 Rockchip Electronics Co. LTD
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#define  MODULE_TAG "mpp_hal"
 
#include "mpp_mem.h"
#include "mpp_log.h"
#include "mpp_common.h"
 
#include "mpp.h"
#include "mpp_hal.h"
#include "mpp_frame_impl.h"
 
#include "hal_h263d_api.h"
#include "hal_h264d_api.h"
#include "hal_h265d_api.h"
#include "hal_vp8d_api.h"
#include "hal_vp9d_api.h"
#include "hal_avsd_api.h"
#include "hal_m2vd_api.h"
#include "hal_mpg4d_api.h"
#include "hal_jpegd_api.h"
#include "hal_av1d_api.h"
 
// for test and demo
#include "hal_dummy_dec_api.h"
#include "hal_dummy_enc_api.h"
 
/*
 * all hardware api static register here
 */
static const MppHalApi *hw_apis[] = {
#if HAVE_AVSD
    &hal_api_avsd,
#endif
#if HAVE_H263D
    &hal_api_h263d,
#endif
#if HAVE_H264D
    &hal_api_h264d,
#endif
#if HAVE_H265D
    &hal_api_h265d,
#endif
#if HAVE_MPEG2D
    &hal_api_m2vd,
#endif
#if HAVE_MPEG4D
    &hal_api_mpg4d,
#endif
#if HAVE_VP8D
    &hal_api_vp8d,
#endif
#if HAVE_VP9D
    &hal_api_vp9d,
#endif
#if HAVE_JPEGD
    &hal_api_jpegd,
#endif
#if HAVE_AV1D
    &hal_api_av1d,
#endif
    &hal_api_dummy_dec,
    &hal_api_dummy_enc,
};
 
typedef struct MppHalImpl_t {
    void            *ctx;
    const MppHalApi *api;
 
    HalTaskGroup    tasks;
} MppHalImpl;
 
 
MPP_RET mpp_hal_init(MppHal *ctx, MppHalCfg *cfg)
{
    if (NULL == ctx || NULL == cfg) {
        mpp_err_f("found NULL input ctx %p cfg %p\n", ctx, cfg);
        return MPP_ERR_NULL_PTR;
    }
    *ctx = NULL;
 
    MppHalImpl *p = mpp_calloc(MppHalImpl, 1);
    if (NULL == p) {
        mpp_err_f("malloc failed\n");
        return MPP_ERR_MALLOC;
    }
 
    RK_U32 i;
    for (i = 0; i < MPP_ARRAY_ELEMS(hw_apis); i++) {
        if (cfg->type   == hw_apis[i]->type &&
            cfg->coding == hw_apis[i]->coding) {
            p->api  = hw_apis[i];
            p->ctx  = mpp_calloc_size(void, p->api->ctx_size);
 
            MPP_RET ret = p->api->init(p->ctx, cfg);
            if (ret) {
                mpp_err_f("hal %s init failed ret %d\n", hw_apis[i]->name, ret);
                break;
            }
 
            *ctx = p;
            return MPP_OK;
        }
    }
 
    mpp_err_f("could not found coding type %d\n", cfg->coding);
    mpp_free(p->ctx);
    mpp_free(p);
 
    return MPP_NOK;
}
 
MPP_RET mpp_hal_deinit(MppHal ctx)
{
    if (NULL == ctx) {
        mpp_err_f("found NULL input\n");
        return MPP_ERR_NULL_PTR;
    }
 
    MppHalImpl *p = (MppHalImpl*)ctx;
    p->api->deinit(p->ctx);
    mpp_free(p->ctx);
    mpp_free(p);
    return MPP_OK;
}
 
MPP_RET mpp_hal_reg_gen(MppHal ctx, HalTaskInfo *task)
{
    if (NULL == ctx || NULL == task) {
        mpp_err_f("found NULL input ctx %p task %p\n", ctx, task);
        return MPP_ERR_NULL_PTR;
    }
 
    MppHalImpl *p = (MppHalImpl*)ctx;
    return p->api->reg_gen(p->ctx, task);
}
 
MPP_RET mpp_hal_hw_start(MppHal ctx, HalTaskInfo *task)
{
    if (NULL == ctx || NULL == task) {
        mpp_err_f("found NULL input ctx %p task %p\n", ctx, task);
        return MPP_ERR_NULL_PTR;
    }
 
    MppHalImpl *p = (MppHalImpl*)ctx;
    return p->api->start(p->ctx, task);
}
 
MPP_RET mpp_hal_hw_wait(MppHal ctx, HalTaskInfo *task)
{
    if (NULL == ctx || NULL == task) {
        mpp_err_f("found NULL input ctx %p task %p\n", ctx, task);
        return MPP_ERR_NULL_PTR;
    }
 
    MppHalImpl *p = (MppHalImpl*)ctx;
    return p->api->wait(p->ctx, task);
}
 
MPP_RET mpp_hal_reset(MppHal ctx)
{
    if (NULL == ctx) {
        mpp_err_f("found NULL input\n");
        return MPP_ERR_NULL_PTR;
    }
 
    MppHalImpl *p = (MppHalImpl*)ctx;
 
    if (NULL == p->api || NULL == p->api->reset)
        return MPP_OK;
 
    return p->api->reset(p->ctx);
}
 
MPP_RET mpp_hal_flush(MppHal ctx)
{
    if (NULL == ctx) {
        mpp_err_f("found NULL input\n");
        return MPP_ERR_NULL_PTR;
    }
 
    MppHalImpl *p = (MppHalImpl*)ctx;
 
    if (NULL == p->api || NULL == p->api->flush)
        return MPP_OK;
 
    return p->api->flush(p->ctx);
}
 
MPP_RET mpp_hal_control(MppHal ctx, MpiCmd cmd, void *param)
{
    if (NULL == ctx) {
        mpp_err_f("found NULL input\n");
        return MPP_ERR_NULL_PTR;
    }
 
    MppHalImpl *p = (MppHalImpl*)ctx;
 
    if (NULL == p->api || NULL == p->api->control)
        return MPP_OK;
 
    return p->api->control(p->ctx, cmd, param);
}