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 47 typedef 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 58 typedef 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 */ 86 typedef struct RcFpsCfg_t { 87 RK_S32 fps_in_flex; 88 RK_S32 fps_in_num; 89 RK_S32 fps_in_denorm; 90 RK_S32 fps_out_flex; 91 RK_S32 fps_out_num; 92 RK_S32 fps_out_denorm; 93 } RcFpsCfg; 94 95 typedef struct RcSuperframeCfg_t { 96 MppEncRcSuperFrameMode super_mode; 97 RK_U32 super_i_thd; 98 RK_U32 super_p_thd; 99 MppEncRcPriority rc_priority; 100 } RcSuperframeCfg; 101 102 typedef struct RcDebreathCfg_t { 103 RK_U32 enable; 104 RK_U32 strength; 105 } RcDebreathCfg; 106 107 typedef struct RcHierQPCfg_t { 108 RK_S32 hier_qp_en; 109 RK_S32 hier_qp_delta[4]; 110 RK_S32 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 */ 118 typedef struct RcCfg_s { 119 /* encode image size */ 120 RK_S32 width; 121 RK_S32 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 RK_S32 igop; 131 /* visual gop len */ 132 RK_S32 vgop; 133 134 /* bitrate parameter */ 135 RK_S32 bps_min; 136 RK_S32 bps_target; 137 RK_S32 bps_max; 138 RK_S32 stats_time; 139 140 /* max I frame bit ratio to P frame bit */ 141 RK_S32 max_i_bit_prop; 142 RK_S32 min_i_bit_prop; 143 RK_S32 init_ip_ratio; 144 /* layer bitrate proportion */ 145 RK_S32 layer_bit_prop[4]; 146 147 /* quality parameter */ 148 RK_S32 init_quality; 149 RK_S32 max_quality; 150 RK_S32 min_quality; 151 RK_S32 max_i_quality; 152 RK_S32 min_i_quality; 153 RK_S32 i_quality_delta; 154 RK_S32 vi_quality_delta; 155 /* layer quality proportion */ 156 RK_S32 layer_quality_delta[4]; 157 158 /* reencode parameter */ 159 RK_S32 max_reencode_times; 160 161 /* still / motion desision parameter */ 162 RK_S32 min_still_prop; 163 RK_S32 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 RK_S32 vbr_hi_prop; 172 RK_S32 vbr_lo_prop; 173 174 MppEncRcDropFrmMode drop_mode; 175 RK_U32 drop_thd; 176 RK_U32 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 */ 186 typedef struct RcImplApi_t { 187 char *name; 188 MppCodingType type; 189 RK_U32 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 */ 219 typedef struct RcApiBrief_t { 220 const char *name; 221 MppCodingType type; 222 } RcApiBrief; 223 224 typedef struct RcApiQueryAll_t { 225 /* input param for query */ 226 RcApiBrief *brief; 227 RK_S32 max_count; 228 229 /* output query count */ 230 RK_S32 count; 231 } RcApiQueryAll; 232 233 typedef struct RcApiQueryType_t { 234 /* input param for query */ 235 RcApiBrief *brief; 236 RK_S32 max_count; 237 MppCodingType type; 238 239 /* output query count */ 240 RK_S32 count; 241 } RcApiQueryType; 242 243 #ifdef __cplusplus 244 extern "C" { 245 #endif 246 247 MPP_RET rc_api_add(const RcImplApi *api); 248 MPP_RET rc_brief_get_all(RcApiQueryAll *query); 249 MPP_RET rc_brief_get_by_type(RcApiQueryType *query); 250 251 #ifdef __cplusplus 252 } 253 #endif 254 255 #endif /* __MPP_RC_API_H__ */