hc
2024-05-10 23fa18eaa71266feff7ba8d83022d9e1cc83c65a
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
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * Copyright 2016 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 "rc"
 
#include <math.h>
#include <memory.h>
 
#include "mpp_env.h"
#include "mpp_mem.h"
#include "mpp_list.h"
#include "mpp_common.h"
 
#include "rc_debug.h"
#include "rc.h"
#include "rc_impl.h"
#include "rc_base.h"
 
typedef struct MppRcImpl_t {
    void            *ctx;
    const RcImplApi *api;
    RcCfg           cfg;
 
    RcFpsCfg        fps;
    RK_S32          frm_cnt;
 
    RK_U32          frm_send;
    RK_U32          frm_done;
} MppRcImpl;
 
RK_U32 rc_debug = 0;
 
const static char default_rc_api[] = "default";
 
MPP_RET rc_init(RcCtx *ctx, MppCodingType type, const char **request_name)
{
    MPP_RET ret = MPP_NOK;
    MppRcImpl *p = NULL;
    const char *name = NULL;
 
    mpp_env_get_u32("rc_debug", &rc_debug, 0);
 
    if (NULL == request_name || NULL == *request_name)
        name = default_rc_api;
    else
        name = *request_name;
 
    rc_dbg_func("enter type %x name %s\n", type, name);
 
    RcImplApi *api = RcImplApiService::get_instance()->api_get(type, name);
 
    mpp_assert(api);
 
    if (api) {
        void *rc_ctx = mpp_calloc_size(void, api->ctx_size);
        p = mpp_calloc(MppRcImpl, 1);
 
        if (NULL == p || NULL == rc_ctx) {
            mpp_err_f("failed to create context size %d\n", api->ctx_size);
            MPP_FREE(p);
            MPP_FREE(rc_ctx);
            ret = MPP_ERR_MALLOC;
        } else {
            p->ctx = rc_ctx;
            p->api = api;
            p->frm_cnt = -1;
            if (request_name && *request_name)
                mpp_log("using rc impl %s\n", api->name);
            ret = MPP_OK;
        }
    }
 
    *ctx = p;
    if (request_name)
        *request_name = name;
 
    rc_dbg_func("leave %p\n", p);
 
    return ret;
}
 
MPP_RET rc_deinit(RcCtx ctx)
{
    MppRcImpl *p = (MppRcImpl *)ctx;
    const RcImplApi *api = p->api;
    MPP_RET ret = MPP_OK;
 
    rc_dbg_func("enter %p\n", ctx);
 
    if (api && api->deinit && p->ctx) {
        ret = api->deinit(p->ctx);
        MPP_FREE(p->ctx);
    }
 
    MPP_FREE(p);
 
    rc_dbg_func("leave %p\n", ctx);
 
    return ret;
}
 
MPP_RET rc_update_usr_cfg(RcCtx ctx, RcCfg *cfg)
{
    MppRcImpl *p = (MppRcImpl *)ctx;
    const RcImplApi *api = p->api;
    MPP_RET ret = MPP_OK;
 
    rc_dbg_func("enter %p\n", ctx);
 
    p->cfg = *cfg;
    p->fps = cfg->fps;
 
    if (api && api->init && p->ctx)
        api->init(p->ctx, &p->cfg);
 
    rc_dbg_func("leave %p\n", ctx);
 
    return ret;
}
 
MPP_RET rc_frm_check_drop(RcCtx ctx, EncRcTask *task)
{
    MppRcImpl *p = (MppRcImpl *)ctx;
    const RcImplApi *api = p->api;
    MPP_RET ret = MPP_OK;
 
    rc_dbg_func("enter %p\n", ctx);
 
    if (api && api->check_drop && p->ctx && task) {
        ret = api->check_drop(p->ctx, task);
        return ret;
    } else {
        RcFpsCfg *cfg = &p->fps;
        RK_S32 frm_cnt  = p->frm_cnt;
        RK_S32 rate_in  = cfg->fps_in_num * cfg->fps_out_denorm;
        RK_S32 rate_out = cfg->fps_out_num * cfg->fps_in_denorm;
        RK_S32 drop = 0;
 
        mpp_assert(cfg->fps_in_denorm >= 1);
        mpp_assert(cfg->fps_out_denorm >= 1);
        mpp_assert(rate_in >= rate_out);
 
        // frame counter is inited to (rate_in - rate_out)  to encode first frame
        if (frm_cnt < 0)
            frm_cnt = rate_in - rate_out;
 
        frm_cnt += rate_out;
 
        if (frm_cnt < rate_in)
            drop = 1;
        else
            frm_cnt -= rate_in;
 
        p->frm_cnt = frm_cnt;
        task->frm.drop = drop;
    }
 
    rc_dbg_func("leave %p drop %d\n", ctx, task->frm.drop);
 
    return ret;
}
 
MPP_RET rc_frm_check_reenc(RcCtx ctx, EncRcTask *task)
{
    MppRcImpl *p = (MppRcImpl *)ctx;
    const RcImplApi *api = p->api;
 
    if (!api || !api->check_reenc || !p->ctx || !task)
        return MPP_OK;
 
    return api->check_reenc(p->ctx, task);
}
 
MPP_RET rc_frm_start(RcCtx ctx, EncRcTask *task)
{
    MppRcImpl *p = (MppRcImpl *)ctx;
    const RcImplApi *api = p->api;
 
    if (!api || !api->frm_start || !p->ctx || !task)
        return MPP_OK;
 
    return api->frm_start(p->ctx, task);
}
 
MPP_RET rc_frm_end(RcCtx ctx, EncRcTask *task)
{
    MppRcImpl *p = (MppRcImpl *)ctx;
    const RcImplApi *api = p->api;
 
    if (!api || !api->frm_end || !p->ctx || !task)
        return MPP_OK;
 
    return api->frm_end(p->ctx, task);
}
 
MPP_RET rc_hal_start(RcCtx ctx, EncRcTask *task)
{
    MppRcImpl *p = (MppRcImpl *)ctx;
    const RcImplApi *api = p->api;
 
    if (!api || !api->hal_start || !p->ctx || !task)
        return MPP_OK;
 
    return api->hal_start(p->ctx, task);
}
 
MPP_RET rc_hal_end(RcCtx ctx, EncRcTask *task)
{
    MppRcImpl *p = (MppRcImpl *)ctx;
    const RcImplApi *api = p->api;
 
    if (!api || !api->hal_end || !p->ctx || !task)
        return MPP_OK;
 
    return api->hal_end(p->ctx, task);
}