1 /******************************************************************************
2 *
3 * Copyright (C) 2018 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 * \file mb_model_based.c
23 *
24 * \brief
25 * This file contain mb level API functions
26 *
27 * \date
28 *
29 * \author
30 * ittiam
31 *
32 ******************************************************************************
33 */
34 /*****************************************************************************/
35 /* File Includes */
36 /*****************************************************************************/
37 /* User include files */
38 #include "ittiam_datatypes.h"
39 #include "rc_common.h"
40 #include "rc_cntrl_param.h"
41 #include "var_q_operator.h"
42 #include "mem_req_and_acq.h"
43 #include "mb_model_based.h"
44
45 typedef struct mb_rate_control_t
46 {
47 /* Frame Qp */
48 UWORD8 u1_frm_qp;
49 /* Estimated average activity for the current frame (updated with the previous
50 frame activity since it is independent of picture type whether it is I or P) */
51 WORD32 i4_avg_activity;
52 } mb_rate_control_t;
53
mbrc_num_fill_use_free_memtab(mb_rate_control_t ** pps_mb_rate_control,itt_memtab_t * ps_memtab,ITT_FUNC_TYPE_E e_func_type)54 WORD32 mbrc_num_fill_use_free_memtab(
55 mb_rate_control_t **pps_mb_rate_control, itt_memtab_t *ps_memtab, ITT_FUNC_TYPE_E e_func_type)
56 {
57 WORD32 i4_mem_tab_idx = 0;
58 static mb_rate_control_t s_mb_rate_control_temp;
59
60 /* Hack for al alloc, during which we dont have any state memory.
61 Dereferencing can cause issues */
62 if(e_func_type == GET_NUM_MEMTAB || e_func_type == FILL_MEMTAB)
63 (*pps_mb_rate_control) = &s_mb_rate_control_temp;
64
65 /*for src rate control state structure*/
66 if(e_func_type != GET_NUM_MEMTAB)
67 {
68 fill_memtab(
69 &ps_memtab[i4_mem_tab_idx],
70 sizeof(mb_rate_control_t),
71 MEM_TAB_ALIGNMENT,
72 PERSISTENT,
73 DDR);
74 use_or_fill_base(&ps_memtab[0], (void **)pps_mb_rate_control, e_func_type);
75 }
76 i4_mem_tab_idx++;
77
78 return (i4_mem_tab_idx);
79 }
80
81 /********************************************************************************
82 MB LEVEL API FUNCTIONS
83 ********************************************************************************/
84 /******************************************************************************
85 Function Name : init_mb_level_rc
86 Description : Initialise the mb model and the average activity to default values
87 Arguments :
88 Return Values : void
89 Revision History:
90 13 03 2008 KJN Creation
91 *********************************************************************************/
init_mb_level_rc(mb_rate_control_t * ps_mb_rate_control)92 void init_mb_level_rc(mb_rate_control_t *ps_mb_rate_control)
93 {
94 /* Set values to default */
95 ps_mb_rate_control->i4_avg_activity = 0;
96 }
97 /******************************************************************************
98 Function Name : mb_init_frame_level
99 Description : Initialise the mb state with frame level decisions
100 Arguments : u1_frame_qp - Frame level qp
101 Return Values : void
102 Revision History:
103 13 03 2008 KJN Creation
104 *********************************************************************************/
mb_init_frame_level(mb_rate_control_t * ps_mb_rate_control,UWORD8 u1_frame_qp)105 void mb_init_frame_level(mb_rate_control_t *ps_mb_rate_control, UWORD8 u1_frame_qp)
106 {
107 /* Update frame level QP */
108 ps_mb_rate_control->u1_frm_qp = u1_frame_qp;
109 }
110 /******************************************************************************
111 Function Name : reset_mb_activity
112 Description : Reset the mb activity - Whenever there is SCD
113 the mb activity is reset
114 Arguments :
115 Return Values : void
116 Revision History:
117 13 03 2008 KJN Creation
118 *********************************************************************************/
reset_mb_activity(mb_rate_control_t * ps_mb_rate_control)119 void reset_mb_activity(mb_rate_control_t *ps_mb_rate_control)
120 {
121 ps_mb_rate_control->i4_avg_activity = 0;
122 }
123
124 /******************************************************************************
125 Function Name : get_mb_qp
126 Description : Calculates the mb level qp
127 Arguments : i4_cur_mb_activity - current frame mb activity
128 pi4_mb_qp - Array of 2 values for before and after mb activity
129 modulation
130 Return Values : void
131
132 Revision History:
133 13 03 2008 KJN Creation
134 *********************************************************************************/
get_mb_qp(mb_rate_control_t * ps_mb_rate_control,WORD32 i4_cur_mb_activity,WORD32 * pi4_mb_qp)135 void get_mb_qp(mb_rate_control_t *ps_mb_rate_control, WORD32 i4_cur_mb_activity, WORD32 *pi4_mb_qp)
136 {
137 WORD32 i4_qp;
138 /* Initialise the mb level qp with the frame level qp */
139 i4_qp = ps_mb_rate_control->u1_frm_qp;
140
141 /* Store the model based QP - This is used for updating the rate control model */
142 pi4_mb_qp[0] = i4_qp;
143
144 /* Modulate the Qp based on the activity */
145 if((ps_mb_rate_control->i4_avg_activity) && (i4_qp < 100))
146 {
147 i4_qp = ((((2 * i4_cur_mb_activity)) + ps_mb_rate_control->i4_avg_activity) * i4_qp +
148 ((i4_cur_mb_activity + 2 * ps_mb_rate_control->i4_avg_activity) >> 1)) /
149 (i4_cur_mb_activity + 2 * ps_mb_rate_control->i4_avg_activity);
150
151 if(i4_qp > ((3 * ps_mb_rate_control->u1_frm_qp) >> 1))
152 i4_qp = ((3 * ps_mb_rate_control->u1_frm_qp) >> 1);
153 }
154
155 /* Store the qp modulated by mb activity - This is used for encoding the MB */
156 pi4_mb_qp[1] = i4_qp;
157 }
158 /******************************************************************************
159 Function Name : get_frm_level_qp
160 Description : Returns the stored frame level QP
161 Arguments :
162 Revision History:
163 13 03 2008 KJN Creation
164 *********************************************************************************/
get_frm_level_qp(mb_rate_control_t * ps_mb_rate_control)165 UWORD8 get_frm_level_qp(mb_rate_control_t *ps_mb_rate_control)
166 {
167 return (ps_mb_rate_control->u1_frm_qp);
168 }
169 /******************************************************************************
170 Function Name : mb_update_frame_level
171 Description : Update the frame level info collected
172 Arguments : i4_avg_activity - Average activity fot frame
173 Return Values :
174 Revision History:
175 13 03 2008 KJN Creation
176 *********************************************************************************/
mb_update_frame_level(mb_rate_control_t * ps_mb_rate_control,WORD32 i4_avg_activity)177 void mb_update_frame_level(mb_rate_control_t *ps_mb_rate_control, WORD32 i4_avg_activity)
178 {
179 /*****************************************************************************
180 Update the Average Activity
181 *****************************************************************************/
182 ps_mb_rate_control->i4_avg_activity = i4_avg_activity;
183 }
184