1 /******************************************************************************
2 *
3 * Copyright (C) 2022 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *****************************************************************************
18 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19 */
20
21 /*****************************************************************************/
22 /* Includes */
23 /*****************************************************************************/
24
25 /* System include files */
26 #include "stdio.h"
27
28 /* User include files */
29 #include "irc_datatypes.h"
30 #include "irc_common.h"
31 #include "irc_cntrl_param.h"
32 #include "irc_mem_req_and_acq.h"
33 #include "irc_rd_model.h"
34 #include "irc_est_sad.h"
35 #include "irc_fixed_point_error_bits.h"
36 #include "irc_vbr_storage_vbv.h"
37 #include "irc_picture_type.h"
38 #include "irc_bit_allocation.h"
39 #include "irc_mb_model_based.h"
40 #include "irc_cbr_buffer_control.h"
41 #include "irc_vbr_str_prms.h"
42 #include "irc_rate_control_api.h"
43 #include "irc_rate_control_api_structs.h"
44 #include "irc_trace_support.h"
45
46 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
47 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
48
49 #define DEV_Q 4 /*Q format(Shift) for Deviation range factor */
50 #define HI_DEV_FCTR 22 /* 1.4*16 */
51 #define LO_DEV_FCTR 12 /* 0.75*16 */
52 #define GET_HI_DEV_QP(Qprev) ((((WORD32) Qprev) * HI_DEV_FCTR + (1 << (DEV_Q - 1))) >> DEV_Q)
53 #define GET_LO_DEV_QP(Qprev) ((((WORD32) Qprev) * LO_DEV_FCTR + (1 << (DEV_Q - 1))) >> DEV_Q)
54 #define CLIP_QP(Qc, hi_d, lo_d) (((Qc) < (lo_d)) ? ((lo_d)) : (((Qc) > (hi_d)) ? (hi_d) : (Qc)))
55
56 /*******************************************************************************
57 * Description : Gets the frame level qp for the given picture type
58 * based on bits per pixel and gradient per pixel
59 ******************************************************************************/
60 /* Get frame level QP based on BPP and GPP */
irc_get_frame_level_init_qp(rate_control_handle * ps_rate_control_api,rc_type_e e_rc_type,picture_type_e e_pic_type,DOUBLE d_bpp,DOUBLE d_gpp)61 UWORD8 irc_get_frame_level_init_qp(rate_control_handle *ps_rate_control_api, rc_type_e e_rc_type,
62 picture_type_e e_pic_type, DOUBLE d_bpp, DOUBLE d_gpp)
63 {
64 DOUBLE d_frame_qp;
65
66 UWORD8 u1_min_qp =
67 ((rate_control_api_t *) (ps_rate_control_api))->au1_min_max_avc_qp[(e_pic_type << 1)];
68 UWORD8 u1_max_qp =
69 ((rate_control_api_t *) (ps_rate_control_api))->au1_min_max_avc_qp[(e_pic_type << 1) + 1];
70
71 if((e_rc_type != VBR_STORAGE) && (e_rc_type != VBR_STORAGE_DVD_COMP) &&
72 (e_rc_type != CBR_NLDRC) && (e_rc_type != CONST_QP) && (e_rc_type != VBR_STREAMING))
73 {
74 TRACE_PRINTF(
75 (const WORD8 *) (const WORD8 *) " Only VBR,NLDRC and CONST QP supported for now \n");
76 return (0);
77 }
78
79 if(d_bpp <= 0.18)
80 {
81 d_frame_qp = 43.49 + (0.59 * d_gpp) - (106.45 * d_bpp);
82 }
83 else if(d_bpp <= 0.6)
84 {
85 d_frame_qp = 25.12 + (0.69 * d_gpp) - (29.23 * (d_bpp - 0.18));
86 }
87 else
88 {
89 d_frame_qp = 13.93 + (0.74 * d_gpp) - (18.4 * (d_bpp - 0.6));
90 }
91
92 /* Truncating the QP to the Max and Min Qp values possible */
93 if(d_frame_qp < u1_min_qp) d_frame_qp = u1_min_qp;
94 if(d_frame_qp > u1_max_qp) d_frame_qp = u1_max_qp;
95
96 return ((UWORD8) (d_frame_qp + 0.5));
97 }
98
irc_change_qp_constraints(rate_control_api_t * ps_rate_control_api,UWORD8 * pu1_min_max_qp,UWORD8 * pu1_min_max_avc_qp)99 void irc_change_qp_constraints(rate_control_api_t *ps_rate_control_api, UWORD8 *pu1_min_max_qp,
100 UWORD8 *pu1_min_max_avc_qp)
101 {
102 WORD32 i;
103
104 for(i = 0; i < MAX_PIC_TYPE; i++)
105 {
106 ps_rate_control_api->au1_min_max_qp[(i << 1)] = pu1_min_max_qp[(i << 1)];
107 ps_rate_control_api->au1_min_max_qp[(i << 1) + 1] = pu1_min_max_qp[(i << 1) + 1];
108 ps_rate_control_api->au1_min_max_avc_qp[(i << 1)] = pu1_min_max_avc_qp[(i << 1)];
109 ps_rate_control_api->au1_min_max_avc_qp[(i << 1) + 1] = pu1_min_max_avc_qp[(i << 1) + 1];
110 }
111 }
112
irc_is_scenecut(rate_control_api_t * ps_rate_control_api)113 UWORD8 irc_is_scenecut(rate_control_api_t *ps_rate_control_api)
114 {
115 return ((rate_control_api_t *) (ps_rate_control_api))->u1_scd_detected;
116 }
117