1 /******************************************************************************
2 *
3 * Copyright (C) 2015 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 Includes */
23 /*****************************************************************************/
24
25 /* User include files */
26 #include "irc_datatypes.h"
27 #include "irc_cntrl_param.h"
28 #include "irc_mem_req_and_acq.h"
29 #include "irc_mb_model_based.h"
30
31 typedef struct mb_rate_control_t
32 {
33 /* Frame Qp */
34 UWORD8 u1_frm_qp;
35
36 /*
37 * Estimated average activity for the current frame (updated with the
38 * previous frame activity since it is independent of picture type whether
39 * it is I or P)
40 */
41 WORD32 i4_avg_activity;
42
43 } mb_rate_control_t;
44
irc_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)45 WORD32 irc_mbrc_num_fill_use_free_memtab(mb_rate_control_t **pps_mb_rate_control,
46 itt_memtab_t *ps_memtab,
47 ITT_FUNC_TYPE_E e_func_type)
48 {
49 WORD32 i4_mem_tab_idx = 0;
50 mb_rate_control_t s_mb_rate_control_temp;
51
52 /*
53 * Hack for al alloc, during which we don't have any state memory.
54 * Dereferencing can cause issues
55 */
56 if(e_func_type == GET_NUM_MEMTAB || e_func_type == FILL_MEMTAB)
57 {
58 (*pps_mb_rate_control) = &s_mb_rate_control_temp;
59 }
60
61 /*For src rate control state structure*/
62 if(e_func_type != GET_NUM_MEMTAB)
63 {
64 fill_memtab(&ps_memtab[i4_mem_tab_idx], sizeof(mb_rate_control_t),
65 ALIGN_128_BYTE, PERSISTENT, DDR);
66 use_or_fill_base(&ps_memtab[0], (void**)pps_mb_rate_control,
67 e_func_type);
68 }
69 i4_mem_tab_idx++;
70
71 return (i4_mem_tab_idx);
72 }
73
74 /*******************************************************************************
75 MB LEVEL API FUNCTIONS
76 ******************************************************************************/
77
78 /******************************************************************************
79 Description : Initialize the mb model and the average activity to default
80 values
81 ******************************************************************************/
irc_init_mb_level_rc(mb_rate_control_t * ps_mb_rate_control)82 void irc_init_mb_level_rc(mb_rate_control_t *ps_mb_rate_control)
83 {
84 /* Set values to default */
85 ps_mb_rate_control->i4_avg_activity = 0;
86 }
87
88 /******************************************************************************
89 Description : Initialize the mb state with frame level decisions
90 *********************************************************************************/
irc_mb_init_frame_level(mb_rate_control_t * ps_mb_rate_control,UWORD8 u1_frame_qp)91 void irc_mb_init_frame_level(mb_rate_control_t *ps_mb_rate_control,
92 UWORD8 u1_frame_qp)
93 {
94 /* Update frame level QP */
95 ps_mb_rate_control->u1_frm_qp = u1_frame_qp;
96 }
97
98 /******************************************************************************
99 Description : Reset the mb activity - Whenever there is SCD
100 the mb activity is reset
101 *********************************************************************************/
irc_reset_mb_activity(mb_rate_control_t * ps_mb_rate_control)102 void irc_reset_mb_activity(mb_rate_control_t *ps_mb_rate_control)
103 {
104 ps_mb_rate_control->i4_avg_activity = 0;
105 }
106
107 /******************************************************************************
108 Description : Calculates the mb level qp
109 *********************************************************************************/
irc_get_mb_qp(mb_rate_control_t * ps_mb_rate_control,WORD32 i4_cur_mb_activity,WORD32 * pi4_mb_qp)110 void irc_get_mb_qp(mb_rate_control_t *ps_mb_rate_control,
111 WORD32 i4_cur_mb_activity,
112 WORD32 *pi4_mb_qp)
113 {
114 WORD32 i4_qp;
115 /* Initialize the mb level qp with the frame level qp */
116 i4_qp = ps_mb_rate_control->u1_frm_qp;
117
118 /*
119 * Store the model based QP - This is used for updating the rate control model
120 */
121 pi4_mb_qp[0] = i4_qp;
122
123 /* Modulate the Qp based on the activity */
124 if((ps_mb_rate_control->i4_avg_activity) && (i4_qp < 100))
125 {
126 i4_qp =((((2 * i4_cur_mb_activity))
127 + ps_mb_rate_control->i4_avg_activity)* i4_qp
128 + ((i4_cur_mb_activity + 2 * ps_mb_rate_control->i4_avg_activity)
129 >> 1))/ (i4_cur_mb_activity + 2 * ps_mb_rate_control->i4_avg_activity);
130
131 if(i4_qp > ((3 * ps_mb_rate_control->u1_frm_qp) >> 1))
132 {
133 i4_qp = ((3 * ps_mb_rate_control->u1_frm_qp) >> 1);
134 }
135 }
136
137 /* Store the qp modulated by mb activity - This is used for encoding the MB */
138 pi4_mb_qp[1] = i4_qp;
139 }
140
141 /*******************************************************************************
142 Description : Returns the stored frame level QP
143 ******************************************************************************/
irc_get_frm_level_qp(mb_rate_control_t * ps_mb_rate_control)144 UWORD8 irc_get_frm_level_qp(mb_rate_control_t *ps_mb_rate_control)
145 {
146 return (ps_mb_rate_control->u1_frm_qp);
147 }
148
149 /*******************************************************************************
150 Description : Update the frame level info collected
151 ******************************************************************************/
irc_mb_update_frame_level(mb_rate_control_t * ps_mb_rate_control,WORD32 i4_avg_activity)152 void irc_mb_update_frame_level(mb_rate_control_t *ps_mb_rate_control,
153 WORD32 i4_avg_activity)
154 {
155 /* Update the Average Activity */
156 ps_mb_rate_control->i4_avg_activity = i4_avg_activity;
157 }
158