1/*
2 * Copyright (c) 2021 Rockchip Electronics Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef __MPP_RC_API_H__
17#define __MPP_RC_API_H__
18
19#include "mpp_err.h"
20#include "rk_venc_rc.h"
21#include "mpp_rc_defs.h"
22
23/*
24 * Mpp rate control has three parts:
25 *
26 * 1. MPI user config module
27 *    MppEncRcCfg structure is provided to user for overall rate control config
28 *    Mpp will receive MppEncRcCfg from user, check parameter and set it to
29 *    encoder.
30 *
31 * 2. Encoder rate control module
32 *    Encoder will implement the rate control strategy required by users
33 *    including CBR, VBR, AVBR and so on.
34 *    This module only implement the target bit calculation behavior and
35 *    quality restriction. And the quality level will be controlled by hal.
36 *
37 * 3. Hal rate control module
38 *    Hal will implement the rate control on hardware. Hal will calculate the
39 *    QP parameter for hardware according to the frame level target bit
40 *    specified by the encoder. And the report the real bitrate and quality to
41 *    encoder.
42 *
43 * The header defines the communication interfaces and structures used between
44 * MPI, encoder and hal.
45 */
46
47typedef enum RcMode_e {
48    RC_VBR,
49    RC_CBR,
50    RC_FIXQP,
51    RC_AVBR,
52    RC_CVBR,
53    RC_QVBR,
54    RC_LEARNING,
55    RC_MODE_BUTT,
56} RcMode;
57
58typedef enum GopMode_e {
59    NORMAL_P,
60    SMART_P,
61} GopMode;
62
63/*
64 * frame rate parameters have great effect on rate control
65 *
66 * fps_in_flex
67 * 0 - fix input frame rate
68 * 1 - variable input frame rate
69 *
70 * fps_in_num
71 * input frame rate numerator, if 0 then default 30
72 *
73 * fps_in_denorm
74 * input frame rate denorminator, if 0 then default 1
75 *
76 * fps_out_flex
77 * 0 - fix output frame rate
78 * 1 - variable output frame rate
79 *
80 * fps_out_num
81 * output frame rate numerator, if 0 then default 30
82 *
83 * fps_out_denorm
84 * output frame rate denorminator, if 0 then default 1
85 */
86typedef struct RcFpsCfg_t {
87    signed int fps_in_flex;
88    signed int fps_in_num;
89    signed int fps_in_denorm;
90    signed int fps_out_flex;
91    signed int fps_out_num;
92    signed int fps_out_denorm;
93} RcFpsCfg;
94
95typedef struct RcSuperframeCfg_t {
96    MppEncRcSuperFrameMode super_mode;
97    unsigned int super_i_thd;
98    unsigned int super_p_thd;
99    MppEncRcPriority rc_priority;
100} RcSuperframeCfg;
101
102typedef struct RcDebreathCfg_t {
103    unsigned int enable;
104    unsigned int strength;
105} RcDebreathCfg;
106
107typedef struct RcHierQPCfg_t {
108    signed int hier_qp_en;
109    signed int hier_qp_delta[4];
110    signed int hier_frame_num[4];
111} RcHierQPCfg;
112
113/*
114 * Control parameter from external config
115 *
116 * It will be updated on rc/prep/gopref config changed.
117 */
118typedef struct RcCfg_s {
119    /* encode image size */
120    signed int width;
121    signed int height;
122
123    /* Use rc_mode to find different api */
124    RcMode mode;
125
126    RcFpsCfg fps;
127
128    GopMode gop_mode;
129    /* I frame gop len */
130    signed int igop;
131    /* visual gop len */
132    signed int vgop;
133
134    /* bitrate parameter */
135    signed int bps_min;
136    signed int bps_target;
137    signed int bps_max;
138    signed int stats_time;
139
140    /* max I frame bit ratio to P frame bit */
141    signed int max_i_bit_prop;
142    signed int min_i_bit_prop;
143    signed int init_ip_ratio;
144    /* layer bitrate proportion */
145    signed int layer_bit_prop[4];
146
147    /* quality parameter */
148    signed int init_quality;
149    signed int max_quality;
150    signed int min_quality;
151    signed int max_i_quality;
152    signed int min_i_quality;
153    signed int i_quality_delta;
154    signed int vi_quality_delta;
155    /* layer quality proportion */
156    signed int layer_quality_delta[4];
157
158    /* reencode parameter */
159    signed int max_reencode_times;
160
161    /* still / motion desision parameter */
162    signed int min_still_prop;
163    signed int max_still_quality;
164
165    /*
166     * vbr parameter
167     *
168     * vbr_hi_prop  - high proportion bitrate for reduce quality
169     * vbr_lo_prop  - low proportion bitrate for increase quality
170     */
171    signed int vbr_hi_prop;
172    signed int vbr_lo_prop;
173
174    MppEncRcDropFrmMode drop_mode;
175    unsigned int drop_thd;
176    unsigned int drop_gap;
177
178    RcSuperframeCfg super_cfg;
179    RcDebreathCfg debreath_cfg;
180    RcHierQPCfg hier_qp_cfg;
181} RcCfg;
182
183/*
184 * Different rate control strategy will be implemented by different API config
185 */
186typedef struct RcImplApi_t {
187    char *name;
188    MppCodingType type;
189    unsigned int ctx_size;
190
191    MPP_RET (*init)(void *ctx, RcCfg *cfg);
192    MPP_RET (*deinit)(void *ctx);
193
194    MPP_RET (*check_drop)(void *ctx, EncRcTask *task);
195    MPP_RET (*check_reenc)(void *ctx, EncRcTask *task);
196
197    /*
198     * frm_start -  frame level rate control frm_start.
199     *              The EncRcTaskInfo will be output to hal for hardware to implement.
200     * frm_end   -  frame level rate control frm_end.
201     *              The EncRcTaskInfo is returned for real quality and bitrate.
202     */
203    MPP_RET (*frm_start)(void *ctx, EncRcTask *task);
204    MPP_RET (*frm_end)(void *ctx, EncRcTask *task);
205
206    /*
207     * hal_start -  hardware level rate control start.
208     *              The EncRcTaskInfo will be output to hal for hardware to implement.
209     * hal_end   -  hardware level rate control end.
210     *              The EncRcTaskInfo is returned for real quality and bitrate.
211     */
212    MPP_RET (*hal_start)(void *ctx, EncRcTask *task);
213    MPP_RET (*hal_end)(void *ctx, EncRcTask *task);
214} RcImplApi;
215
216/*
217 * structures for RC API register and query
218 */
219typedef struct RcApiBrief_t {
220    const char *name;
221    MppCodingType type;
222} RcApiBrief;
223
224typedef struct RcApiQueryAll_t {
225    /* input param for query */
226    RcApiBrief *brief;
227    signed int max_count;
228
229    /* output query count */
230    signed int count;
231} RcApiQueryAll;
232
233typedef struct RcApiQueryType_t {
234    /* input param for query */
235    RcApiBrief *brief;
236    signed int max_count;
237    MppCodingType type;
238
239    /* output query count */
240    signed int count;
241} RcApiQueryType;
242
243#ifdef __cplusplus
244extern "C" {
245#endif
246
247MPP_RET rc_api_add(const RcImplApi *api);
248MPP_RET rc_brief_get_all(RcApiQueryAll *query);
249MPP_RET rc_brief_get_by_type(RcApiQueryType *query);
250
251#ifdef __cplusplus
252}
253#endif
254
255#endif /* __MPP_RC_API_H__ */